/
wiki.go
160 lines (131 loc) · 2.97 KB
/
wiki.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Program grabs the AES SBox Wiki page and checks whether the published
// SBox/Inverse Table matches the published SBox/Inverse byte values.
package main
import (
"bufio"
"bytes"
"encoding/hex"
"fmt"
"strings"
"github.com/PuerkitoBio/goquery"
)
type PreType int
const PreCount = 4
const (
SBoxTable PreType = iota
SBoxBytes
SBoxInvTable
SBoxInvBytes
)
func (p PreType) String() string {
switch p {
case 0:
return "SBoxTable"
case 1:
return "SBoxBytes"
case 2:
return "SBoxInvTable"
case 3:
return "SBoxInvBytes"
default:
return "Unknown"
}
}
var (
url = "https://en.wikipedia.org/wiki/Rijndael_S-box"
prees = make(map[PreType]string)
)
func parseSBoxTable(table string) []byte {
// Ignore 1st and 2nd rows.
// Loop over each subsequent line:
// 1. Split on '|' and ignore everything before
// 2. Split on ' ' and convert each element to a
tr := strings.NewReader(table)
scanner := bufio.NewScanner(tr)
b := make([]byte, 0, 256)
i := 0
for scanner.Scan() {
if i < 2 {
// Skip over the first 2 rows
i++
continue
}
rawLine := scanner.Text()
rawSplit := strings.Split(rawLine, "|")
if len(rawSplit) != 2 {
return nil
}
byteLine := strings.Replace(rawSplit[1], " ", "", -1)
bytes, err := hex.DecodeString(byteLine)
if err != nil || len(bytes) != 16 {
return nil
}
b = append(b, bytes...)
}
if err := scanner.Err(); err != nil {
return nil
}
return b
}
func parseSBoxBytes(table string) []byte {
// Ignore 1st and 2nd rows
// Skip last row '};'
// Every other row:
// 1. Trimspace
// 2. Replace ',' -> ''
// 3. Replace ' ' -> ''
// 3. Replace '0x' -> ''
tr := strings.NewReader(table)
scanner := bufio.NewScanner(tr)
b := make([]byte, 0, 256)
i := 0
for scanner.Scan() {
if i < 2 {
// Skip the first 2 rows
i++
continue
}
line := scanner.Text()
if strings.Contains(line, "};") {
// Skip the last line
continue
}
line = strings.TrimSpace(line)
line = strings.Replace(line, ",", "", -1)
line = strings.Replace(line, " ", "", -1)
line = strings.Replace(line, "0x", "", -1)
bytes, err := hex.DecodeString(line)
if err != nil || len(bytes) != 16 {
return nil
}
b = append(b, bytes...)
}
if err := scanner.Err(); err != nil {
return nil
}
return b
}
func main() {
doc, err := goquery.NewDocument(url)
if err != nil {
panic(err)
}
preElems := doc.Find("#mw-content-text > pre")
if preElems.Length() != PreCount {
panic("Did not find enough elements on the page.")
}
preElems.Each(func(i int, sel *goquery.Selection) {
prees[PreType(i)] = sel.Text()
})
tableB := parseSBoxTable(prees[SBoxTable])
bytesB := parseSBoxBytes(prees[SBoxBytes])
if !bytes.Equal(tableB, bytesB) {
panic("SBoxTable and SBoxBytes do not match")
}
iTableB := parseSBoxTable(prees[SBoxInvTable])
iBytesB := parseSBoxBytes(prees[SBoxInvBytes])
if !bytes.Equal(iTableB, iBytesB) {
panic("SBoxInvTable and SBoxInvBytes do not match.")
}
fmt.Println("Everything matches.")
}