forked from HACKERALERT/serpent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sbox_ref.go
316 lines (296 loc) · 5.97 KB
/
sbox_ref.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright (c) 2016 Andreas Auernhammer. All rights reserved.
// Use of this source code is governed by a license that can be
// found in the LICENSE file.
package serpent
// The linear transformation of serpent
// This version, tries not to minimize the
// number of registers, but maximize parallism.
func linear(v0, v1, v2, v3 *uint32) {
t0 := ((*v0 << 13) | (*v0 >> (32 - 13)))
t2 := ((*v2 << 3) | (*v2 >> (32 - 3)))
t1 := *v1 ^ t0 ^ t2
t3 := *v3 ^ t2 ^ (t0 << 3)
*v1 = (t1 << 1) | (t1 >> (32 - 1))
*v3 = (t3 << 7) | (t3 >> (32 - 7))
t0 ^= *v1 ^ *v3
t2 ^= *v3 ^ (*v1 << 7)
*v0 = (t0 << 5) | (t0 >> (32 - 5))
*v2 = (t2 << 22) | (t2 >> (32 - 22))
}
// The inverse linear transformation of serpent
// This version, tries not to minimize the
// number of registers, but maximize parallism.
func linearInv(v0, v1, v2, v3 *uint32) {
t2 := (*v2 >> 22) | (*v2 << (32 - 22))
t0 := (*v0 >> 5) | (*v0 << (32 - 5))
t2 ^= *v3 ^ (*v1 << 7)
t0 ^= *v1 ^ *v3
t3 := (*v3 >> 7) | (*v3 << (32 - 7))
t1 := (*v1 >> 1) | (*v1 << (32 - 1))
*v3 = t3 ^ t2 ^ (t0 << 3)
*v1 = t1 ^ t0 ^ t2
*v2 = (t2 >> 3) | (t2 << (32 - 3))
*v0 = (t0 >> 13) | (t0 << (32 - 13))
}
// The following functions sb0,sb1, ..., sb7 represent the 8 Serpent S-Boxes.
// sb0Inv til sb7Inv are the inverse functions (e.g. sb0Inv is the Inverse to sb0
// and vice versa).
// The S-Boxes differ from the original Serpent definitions. This is for
// optimisation. The functions use the Serpent S-Box improvements for (non x86)
// from Dr. B. R. Gladman and Sam Simpson.
// S-Box 0
func sb0(r0, r1, r2, r3 *uint32) {
t0 := *r0 ^ *r3
t1 := *r2 ^ t0
t2 := *r1 ^ t1
*r3 = (*r0 & *r3) ^ t2
t3 := *r0 ^ (*r1 & t0)
*r2 = t2 ^ (*r2 | t3)
t4 := *r3 & (t1 ^ t3)
*r1 = (^t1) ^ t4
*r0 = t4 ^ (^t3)
}
// Inverse S-Box 0
func sb0Inv(r0, r1, r2, r3 *uint32) {
t0 := ^(*r0)
t1 := *r0 ^ *r1
t2 := *r3 ^ (t0 | t1)
t3 := *r2 ^ t2
*r2 = t1 ^ t3
t4 := t0 ^ (*r3 & t1)
*r1 = t2 ^ (*r2 & t4)
*r3 = (*r0 & t2) ^ (t3 | *r1)
*r0 = *r3 ^ (t3 ^ t4)
}
// S-Box 1
func sb1(r0, r1, r2, r3 *uint32) {
t0 := *r1 ^ (^(*r0))
t1 := *r2 ^ (*r0 | t0)
*r2 = *r3 ^ t1
t2 := *r1 ^ (*r3 | t0)
t3 := t0 ^ *r2
*r3 = t3 ^ (t1 & t2)
t4 := t1 ^ t2
*r1 = *r3 ^ t4
*r0 = t1 ^ (t3 & t4)
}
// Inverse S-Box 1
func sb1Inv(r0, r1, r2, r3 *uint32) {
t0 := *r1 ^ *r3
t1 := *r0 ^ (*r1 & t0)
t2 := t0 ^ t1
*r3 = *r2 ^ t2
t3 := *r1 ^ (t0 & t1)
t4 := *r3 | t3
*r1 = t1 ^ t4
t5 := ^(*r1)
t6 := *r3 ^ t3
*r0 = t5 ^ t6
*r2 = t2 ^ (t5 | t6)
}
// S-Box 2
func sb2(r0, r1, r2, r3 *uint32) {
v0 := *r0 // save r0
v3 := *r3 // save r3
t0 := ^v0
t1 := *r1 ^ v3
t2 := *r2 & t0
*r0 = t1 ^ t2
t3 := *r2 ^ t0
t4 := *r2 ^ *r0
t5 := *r1 & t4
*r3 = t3 ^ t5
*r2 = v0 ^ ((v3 | t5) & (*r0 | t3))
*r1 = (t1 ^ *r3) ^ (*r2 ^ (v3 | t0))
}
// Inverse S-Box 2
func sb2Inv(r0, r1, r2, r3 *uint32) {
v0 := *r0 // save r0
v3 := *r3 // save r3
t0 := *r1 ^ v3
t1 := ^t0
t2 := v0 ^ *r2
t3 := *r2 ^ t0
t4 := *r1 & t3
*r0 = t2 ^ t4
t5 := v0 | t1
t6 := v3 ^ t5
t7 := t2 | t6
*r3 = t0 ^ t7
t8 := ^t3
t9 := *r0 | *r3
*r1 = t8 ^ t9
*r2 = (v3 & t8) ^ (t2 ^ t9)
}
// S-Box 3
func sb3(r0, r1, r2, r3 *uint32) {
v1 := *r1 // save r1
v3 := *r3 // save r3
t0 := *r0 ^ *r1
t1 := *r0 & *r2
t2 := *r0 | *r3
t3 := *r2 ^ *r3
t4 := t0 & t2
t5 := t1 | t4
*r2 = t3 ^ t5
t6 := *r1 ^ t2
t7 := t5 ^ t6
t8 := t3 & t7
*r0 = t0 ^ t8
t9 := *r2 & *r0
*r1 = t7 ^ t9
*r3 = (v1 | v3) ^ (t3 ^ t9)
}
// Inverse S-Box 3
func sb3Inv(r0, r1, r2, r3 *uint32) {
t0 := *r0 | *r1
t1 := *r1 ^ *r2
t2 := *r1 & t1
t3 := *r0 ^ t2
t4 := *r2 ^ t3
t5 := *r3 | t3
*r0 = t1 ^ t5
t6 := t1 | t5
t7 := *r3 ^ t6
*r2 = t4 ^ t7
t8 := t0 ^ t7
t9 := *r0 & t8
*r3 = t3 ^ t9
*r1 = *r3 ^ (*r0 ^ t8)
}
// S-Box 4
func sb4(r0, r1, r2, r3 *uint32) {
v0 := *r0 // save r0
t0 := v0 ^ *r3
t1 := *r3 & t0
t2 := *r2 ^ t1
t3 := *r1 | t2
*r3 = t0 ^ t3
t4 := ^(*r1)
t5 := t0 | t4
*r0 = t2 ^ t5
t6 := v0 & *r0
t7 := t0 ^ t4
t8 := t3 & t7
*r2 = t6 ^ t8
*r1 = (v0 ^ t2) ^ (t7 & *r2)
}
// Inverse S-Box 4
func sb4Inv(r0, r1, r2, r3 *uint32) {
v3 := *r3 // save r3
t0 := *r2 | v3
t1 := *r0 & t0
t2 := *r1 ^ t1
t3 := *r0 & t2
t4 := *r2 ^ t3
*r1 = v3 ^ t4
t5 := ^(*r0)
t6 := t4 & *r1
*r3 = t2 ^ t6
t7 := *r1 | t5
t8 := v3 ^ t7
*r0 = *r3 ^ t8
*r2 = (t2 & t8) ^ (*r1 ^ t5)
}
// S-Box 5
func sb5(r0, r1, r2, r3 *uint32) {
v1 := *r1 // save r1
t0 := ^(*r0)
t1 := *r0 ^ v1
t2 := *r0 ^ *r3
t3 := *r2 ^ t0
t4 := t1 | t2
*r0 = t3 ^ t4
t5 := *r3 & *r0
t6 := t1 ^ *r0
*r1 = t5 ^ t6
t7 := t0 | *r0
t8 := t1 | t5
t9 := t2 ^ t7
*r2 = t8 ^ t9
*r3 = (v1 ^ t5) ^ (*r1 & t9)
}
// Inverse S-Box 5
func sb5Inv(r0, r1, r2, r3 *uint32) {
v0 := *r0 // save r0
v1 := *r1 // save r1
v3 := *r3 // save r3
t0 := ^(*r2)
t1 := v1 & t0
t2 := v3 ^ t1
t3 := v0 & t2
t4 := v1 ^ t0
*r3 = t3 ^ t4
t5 := v1 | *r3
t6 := v0 & t5
*r1 = t2 ^ t6
t7 := v0 | v3
t8 := t0 ^ t5
*r0 = t7 ^ t8
*r2 = (v1 & t7) ^ (t3 | (v0 ^ *r2))
}
// S-Box 6
func sb6(r0, r1, r2, r3 *uint32) {
t0 := ^(*r0)
t1 := *r0 ^ *r3
t2 := *r1 ^ t1
t3 := t0 | t1
t4 := *r2 ^ t3
*r1 = *r1 ^ t4
t5 := t1 | *r1
t6 := *r3 ^ t5
t7 := t4 & t6
*r2 = t2 ^ t7
t8 := t4 ^ t6
*r0 = *r2 ^ t8
*r3 = (^t4) ^ (t2 & t8)
}
// Inverse S-Box 6
func sb6Inv(r0, r1, r2, r3 *uint32) {
v1 := *r1 // save r1
v3 := *r3 // save r3
t0 := ^(*r0)
t1 := *r0 ^ v1
t2 := *r2 ^ t1
t3 := *r2 | t0
t4 := v3 ^ t3
*r1 = t2 ^ t4
t5 := t2 & t4
t6 := t1 ^ t5
t7 := v1 | t6
*r3 = t4 ^ t7
t8 := v1 | *r3
*r0 = t6 ^ t8
*r2 = (v3 & t0) ^ (t2 ^ t8)
}
// S-Box 7
func sb7(r0, r1, r2, r3 *uint32) {
t0 := *r1 ^ *r2
t1 := *r2 & t0
t2 := *r3 ^ t1
t3 := *r0 ^ t2
t4 := *r3 | t0
t5 := t3 & t4
*r1 = *r1 ^ t5
t6 := t2 | *r1
t7 := *r0 & t3
*r3 = t0 ^ t7
t8 := t3 ^ t6
t9 := *r3 & t8
*r2 = t2 ^ t9
*r0 = (^t8) ^ (*r3 & *r2)
}
// Inverse S-Box 7
func sb7Inv(r0, r1, r2, r3 *uint32) {
v0 := *r0 // save r0
v3 := *r3 // save r3
t0 := *r2 | (v0 & *r1)
t1 := v3 & (v0 | *r1)
*r3 = t0 ^ t1
t2 := ^v3
t3 := *r1 ^ t1
t4 := t3 | (*r3 ^ t2)
*r1 = v0 ^ t4
*r0 = (*r2 ^ t3) ^ (v3 | *r1)
*r2 = (t0 ^ *r1) ^ (*r0 ^ (v0 & *r3))
}