-
Notifications
You must be signed in to change notification settings - Fork 25
/
utils.go
173 lines (144 loc) · 4.21 KB
/
utils.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
161
162
163
164
165
166
167
168
169
170
171
172
173
package aria
import (
"crypto/subtle"
"encoding/binary"
)
// Endianness option
const littleEndian bool = false
func keyToUint32s(b []byte) []uint32 {
size := len(b) / 4
dst := make([]uint32, size)
for i := 0; i < size; i++ {
j := i * 4
if littleEndian {
dst[i] = binary.LittleEndian.Uint32(b[j:])
} else {
dst[i] = binary.BigEndian.Uint32(b[j:])
}
}
return dst
}
func bytesToUint32s(inp []byte) []uint32 {
blk := make([]uint32, 4)
if littleEndian {
blk[0] = binary.LittleEndian.Uint32(inp[0:])
blk[1] = binary.LittleEndian.Uint32(inp[4:])
blk[2] = binary.LittleEndian.Uint32(inp[8:])
blk[3] = binary.LittleEndian.Uint32(inp[12:])
} else {
blk[0] = binary.BigEndian.Uint32(inp[0:])
blk[1] = binary.BigEndian.Uint32(inp[4:])
blk[2] = binary.BigEndian.Uint32(inp[8:])
blk[3] = binary.BigEndian.Uint32(inp[12:])
}
return blk
}
func uint32sToBytes(blk []uint32) []byte {
sav := make([]byte, 16)
if littleEndian {
binary.LittleEndian.PutUint32(sav[0:], blk[0])
binary.LittleEndian.PutUint32(sav[4:], blk[1])
binary.LittleEndian.PutUint32(sav[8:], blk[2])
binary.LittleEndian.PutUint32(sav[12:], blk[3])
} else {
binary.BigEndian.PutUint32(sav[0:], blk[0])
binary.BigEndian.PutUint32(sav[4:], blk[1])
binary.BigEndian.PutUint32(sav[8:], blk[2])
binary.BigEndian.PutUint32(sav[12:], blk[3])
}
return sav
}
func copyBytes(xk []uint32, x [16]byte) {
xx := bytesToUint32s(x[:])
copy(xk, xx)
}
func toBytes(u []uint32) (r [16]byte) {
rr := uint32sToBytes(u)
copy(r[:], rr)
return
}
// Round Function Fo
func roundOdd(d, rk [16]byte) [16]byte {
return diffuse(substitute1(xor(d, rk)))
}
// Round Function Fe
func roundEven(d, rk [16]byte) [16]byte {
return diffuse(substitute2(xor(d, rk)))
}
// Substitution Layer SL1
func substitute1(x [16]byte) (y [16]byte) {
var i uint
for i = 0; i < 16; i++ {
switch i%4 {
case 0:
y[i] = sb1[x[i]]
case 1:
y[i] = sb2[x[i]]
case 2:
y[i] = sb3[x[i]]
case 3:
y[i] = sb4[x[i]]
}
}
return
}
// Substitution Layer SL2
func substitute2(x [16]byte) (y [16]byte) {
var i uint
for i = 0; i < 16; i++ {
switch i%4 {
case 0:
y[i] = sb3[x[i]]
case 1:
y[i] = sb4[x[i]]
case 2:
y[i] = sb1[x[i]]
case 3:
y[i] = sb2[x[i]]
}
}
return
}
// Diffuse Layer A
func diffuse(x [16]byte) (y [16]byte) {
y[0] = x[3] ^ x[4] ^ x[6] ^ x[8] ^ x[9] ^ x[13] ^ x[14]
y[1] = x[2] ^ x[5] ^ x[7] ^ x[8] ^ x[9] ^ x[12] ^ x[15]
y[2] = x[1] ^ x[4] ^ x[6] ^ x[10] ^ x[11] ^ x[12] ^ x[15]
y[3] = x[0] ^ x[5] ^ x[7] ^ x[10] ^ x[11] ^ x[13] ^ x[14]
y[4] = x[0] ^ x[2] ^ x[5] ^ x[8] ^ x[11] ^ x[14] ^ x[15]
y[5] = x[1] ^ x[3] ^ x[4] ^ x[9] ^ x[10] ^ x[14] ^ x[15]
y[6] = x[0] ^ x[2] ^ x[7] ^ x[9] ^ x[10] ^ x[12] ^ x[13]
y[7] = x[1] ^ x[3] ^ x[6] ^ x[8] ^ x[11] ^ x[12] ^ x[13]
y[8] = x[0] ^ x[1] ^ x[4] ^ x[7] ^ x[10] ^ x[13] ^ x[15]
y[9] = x[0] ^ x[1] ^ x[5] ^ x[6] ^ x[11] ^ x[12] ^ x[14]
y[10] = x[2] ^ x[3] ^ x[5] ^ x[6] ^ x[8] ^ x[13] ^ x[15]
y[11] = x[2] ^ x[3] ^ x[4] ^ x[7] ^ x[9] ^ x[12] ^ x[14]
y[12] = x[1] ^ x[2] ^ x[6] ^ x[7] ^ x[9] ^ x[11] ^ x[12]
y[13] = x[0] ^ x[3] ^ x[6] ^ x[7] ^ x[8] ^ x[10] ^ x[13]
y[14] = x[0] ^ x[3] ^ x[4] ^ x[5] ^ x[9] ^ x[11] ^ x[14]
y[15] = x[1] ^ x[2] ^ x[4] ^ x[5] ^ x[8] ^ x[10] ^ x[15]
return
}
func xor(a, b [16]byte) (r [16]byte) {
subtle.XORBytes(r[:], a[:], b[:])
return
}
func lrot(x [16]byte, n uint) (y [16]byte) {
q, r := n/8, n%8
s := 8 - r
var i uint
for i = 0; i < 15; i++ {
y[i] = x[(q+i)%16]<<r | x[(q+i+1)%16]>>s
}
y[15] = x[(q+15)%16]<<r | x[q%16]>>s
return
}
func rrot(x [16]byte, n uint) (y [16]byte) {
q, r := n/8%16, n%8
s := 8 - r
var i uint
for i = 0; i < 16; i++ {
y[i] = x[((i+16)-q)%16]>>r | x[((i+15)-q)%16]<<s
}
return
}