-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathKeccakF-1600-reference32BI.c
497 lines (428 loc) · 15.3 KB
/
KeccakF-1600-reference32BI.c
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
Implementation by the Keccak, Keyak and Ketje Teams, namely, Guido Bertoni,
Joan Daemen, Michaël Peeters, Gilles Van Assche and Ronny Van Keer, hereby
denoted as "the implementer".
For more information, feedback or questions, please refer to our websites:
http://keccak.noekeon.org/
http://keyak.noekeon.org/
http://ketje.noekeon.org/
To the extent possible under law, the implementer has waived all copyright
and related or neighboring rights to the source code in this file.
http://creativecommons.org/publicdomain/zero/1.0/
*/
#include <stdio.h>
#include <string.h>
#include "brg_endian.h"
#include "displayIntermediateValues.h"
#include "KeccakF-1600-interface.h"
typedef unsigned char UINT8;
typedef unsigned int UINT32;
// WARNING: on 8-bit and 16-bit platforms, this should be replaced by:
//typedef unsigned long UINT32;
#define nrRounds 24
UINT32 KeccakRoundConstants[nrRounds][2];
#define nrLanes 25
unsigned int KeccakRhoOffsets[nrLanes];
/* ---------------------------------------------------------------- */
void toBitInterleaving(UINT32 low, UINT32 high, UINT32 *even, UINT32 *odd);
void fromBitInterleaving(UINT32 even, UINT32 odd, UINT32 *low, UINT32 *high);
void toBitInterleaving(UINT32 low, UINT32 high, UINT32 *even, UINT32 *odd)
{
unsigned int i;
*even = 0;
*odd = 0;
for(i=0; i<64; i++) {
unsigned int inBit;
if (i < 32)
inBit = (low >> i) & 1;
else
inBit = (high >> (i-32)) & 1;
if ((i % 2) == 0)
*even |= inBit << (i/2);
else
*odd |= inBit << ((i-1)/2);
}
}
void fromBitInterleaving(UINT32 even, UINT32 odd, UINT32 *low, UINT32 *high)
{
unsigned int i;
*low = 0;
*high = 0;
for(i=0; i<64; i++) {
unsigned int inBit;
if ((i % 2) == 0)
inBit = (even >> (i/2)) & 1;
else
inBit = (odd >> ((i-1)/2)) & 1;
if (i < 32)
*low |= inBit << i;
else
*high |= inBit << (i-32);
}
}
/* ---------------------------------------------------------------- */
void KeccakF1600_InitializeRoundConstants();
void KeccakF1600_InitializeRhoOffsets();
int LFSR86540(UINT8 *LFSR);
void KeccakF1600_Initialize()
{
KeccakF1600_InitializeRoundConstants();
KeccakF1600_InitializeRhoOffsets();
}
void KeccakF1600_InitializeRoundConstants()
{
UINT8 LFSRstate = 0x01;
unsigned int i, j, bitPosition;
UINT32 low, high;
for(i=0; i<nrRounds; i++) {
low = high = 0;
for(j=0; j<7; j++) {
bitPosition = (1<<j)-1; //2^j-1
if (LFSR86540(&LFSRstate)) {
if (bitPosition < 32)
low ^= (UINT32)1 << bitPosition;
else
high ^= (UINT32)1 << (bitPosition-32);
}
}
toBitInterleaving(low, high, &(KeccakRoundConstants[i][0]), &(KeccakRoundConstants[i][1]));
}
}
void KeccakF1600_InitializeRhoOffsets()
{
unsigned int x, y, t, newX, newY;
KeccakRhoOffsets[0] = 0;
x = 1;
y = 0;
for(t=0; t<24; t++) {
KeccakRhoOffsets[5*y+x] = ((t+1)*(t+2)/2) % 64;
newX = (0*x+1*y) % 5;
newY = (2*x+3*y) % 5;
x = newX;
y = newY;
}
}
int LFSR86540(UINT8 *LFSR)
{
int result = ((*LFSR) & 0x01) != 0;
if (((*LFSR) & 0x80) != 0)
// Primitive polynomial over GF(2): x^8+x^6+x^5+x^4+1
(*LFSR) = ((*LFSR) << 1) ^ 0x71;
else
(*LFSR) <<= 1;
return result;
}
/* ---------------------------------------------------------------- */
void KeccakF1600_StateInitialize(void *state)
{
memset(state, 0, KeccakF_width/8);
}
/* ---------------------------------------------------------------- */
void KeccakF1600_StateXORBytesInLane(void *state, unsigned int lanePosition, const unsigned char *data, unsigned int offset, unsigned int length)
{
if ((lanePosition < 25) && (offset < 8) && (offset+length <= 8)) {
UINT8 laneAsBytes[8];
UINT32 low, high;
UINT32 lane[2];
UINT32 *stateAsHalfLanes;
memset(laneAsBytes, 0, 8);
memcpy(laneAsBytes+offset, data, length);
low = laneAsBytes[0]
| ((UINT32)(laneAsBytes[1]) << 8)
| ((UINT32)(laneAsBytes[2]) << 16)
| ((UINT32)(laneAsBytes[3]) << 24);
high = laneAsBytes[4]
| ((UINT32)(laneAsBytes[5]) << 8)
| ((UINT32)(laneAsBytes[6]) << 16)
| ((UINT32)(laneAsBytes[7]) << 24);
toBitInterleaving(low, high, lane, lane+1);
stateAsHalfLanes = (UINT32*)state;
stateAsHalfLanes[lanePosition*2+0] ^= lane[0];
stateAsHalfLanes[lanePosition*2+1] ^= lane[1];
}
}
void KeccakF1600_StateXORBytes(void *state, const unsigned char *data, unsigned int offset, unsigned int length)
{
if ((offset < 200) && (offset+length <= 200)) {
unsigned int lanePosition = offset/8;
unsigned int offsetInLane = offset%8;
while(length > 0) {
unsigned int bytesInLane = 8 - offsetInLane;
if (bytesInLane > length)
bytesInLane = length;
KeccakF1600_StateXORBytesInLane(state, lanePosition, data, offsetInLane, bytesInLane);
length -= bytesInLane;
lanePosition++;
offsetInLane = 0;
data += bytesInLane;
}
}
}
/* ---------------------------------------------------------------- */
void KeccakF1600_StateExtractBytesInLane(const void *state, unsigned int lanePosition, unsigned char *data, unsigned int offset, unsigned int length);
void KeccakF1600_StateOverwriteBytesInLane(void *state, unsigned int lanePosition, const unsigned char *data, unsigned int offset, unsigned int length)
{
if ((lanePosition < 25) && (offset < 8) && (offset+length <= 8)) {
UINT8 laneAsBytes[8];
UINT32 low, high;
UINT32 lane[2];
UINT32 *stateAsHalfLanes;
KeccakF1600_StateExtractBytesInLane(state, lanePosition, laneAsBytes, 0, 8);
memcpy(laneAsBytes+offset, data, length);
low = laneAsBytes[0]
| ((UINT32)(laneAsBytes[1]) << 8)
| ((UINT32)(laneAsBytes[2]) << 16)
| ((UINT32)(laneAsBytes[3]) << 24);
high = laneAsBytes[4]
| ((UINT32)(laneAsBytes[5]) << 8)
| ((UINT32)(laneAsBytes[6]) << 16)
| ((UINT32)(laneAsBytes[7]) << 24);
toBitInterleaving(low, high, lane, lane+1);
stateAsHalfLanes = (UINT32*)state;
stateAsHalfLanes[lanePosition*2+0] = lane[0];
stateAsHalfLanes[lanePosition*2+1] = lane[1];
}
}
void KeccakF1600_StateOverwriteBytes(void *state, const unsigned char *data, unsigned int offset, unsigned int length)
{
if ((offset < 200) && (offset+length <= 200)) {
unsigned int lanePosition = offset/8;
unsigned int offsetInLane = offset%8;
while(length > 0) {
unsigned int bytesInLane = 8 - offsetInLane;
if (bytesInLane > length)
bytesInLane = length;
KeccakF1600_StateOverwriteBytesInLane(state, lanePosition, data, offsetInLane, bytesInLane);
length -= bytesInLane;
lanePosition++;
offsetInLane = 0;
data += bytesInLane;
}
}
}
/* ---------------------------------------------------------------- */
void KeccakF1600_StateOverwriteWithZeroes(void *state, unsigned int byteCount)
{
if (byteCount <= 200) {
UINT8 laneAsBytes[8];
unsigned int lanePosition = 0;
memset(laneAsBytes, 0, 8);
while(byteCount > 0) {
if (byteCount < 8) {
KeccakF1600_StateOverwriteBytesInLane(state, lanePosition, laneAsBytes, 0, byteCount);
byteCount = 0;
}
else {
UINT32 *stateAsHalfLanes = (UINT32*)state;
stateAsHalfLanes[lanePosition*2+0] = 0;
stateAsHalfLanes[lanePosition*2+1] = 0;
byteCount -= 8;
lanePosition++;
}
}
}
}
/* ---------------------------------------------------------------- */
void KeccakF1600_StateComplementBit(void *state, unsigned int position)
{
if (position < 1600) {
UINT32 *stateAsHalfLanes = (UINT32*)state;
unsigned int lanePosition = position/64;
unsigned int zeta = position%2;
unsigned int bitInLane = (position%64)/2;
stateAsHalfLanes[lanePosition*2+zeta] ^= (UINT32)1 << bitInLane;
}
}
/* ---------------------------------------------------------------- */
void KeccakF1600_PermutationOnWords(UINT32 *state);
void theta(UINT32 *A);
void rho(UINT32 *A);
void pi(UINT32 *A);
void chi(UINT32 *A);
void iota(UINT32 *A, unsigned int indexRound);
void KeccakF1600_StateExtractBytes(const void *state, unsigned char *data, unsigned int offset, unsigned int length);
void KeccakF1600_StatePermute(void *state)
{
UINT32 *stateAsHalfLanes = (UINT32*)state;
{
UINT8 stateAsBytes[KeccakF_width/8];
KeccakF1600_StateExtractBytes(state, stateAsBytes, 0, KeccakF_width/8);
displayStateAsBytes(1, "Input of permutation", stateAsBytes);
}
KeccakF1600_PermutationOnWords(stateAsHalfLanes);
{
UINT8 stateAsBytes[KeccakF_width/8];
KeccakF1600_StateExtractBytes(state, stateAsBytes, 0, KeccakF_width/8);
displayStateAsBytes(1, "State after permutation", stateAsBytes);
}
}
void KeccakF1600_PermutationOnWords(UINT32 *state)
{
unsigned int i;
displayStateAs32bitWords(3, "Same, with lanes as pairs of 32-bit words (bit interleaving)", state);
for(i=0; i<nrRounds; i++) {
displayRoundNumber(3, i);
theta(state);
displayStateAs32bitWords(3, "After theta", state);
rho(state);
displayStateAs32bitWords(3, "After rho", state);
pi(state);
displayStateAs32bitWords(3, "After pi", state);
chi(state);
displayStateAs32bitWords(3, "After chi", state);
iota(state, i);
displayStateAs32bitWords(3, "After iota", state);
}
}
#define index(x, y,z) ((((x)%5)+5*((y)%5))*2 + z)
#define ROL32(a, offset) ((offset != 0) ? ((((UINT32)a) << offset) ^ (((UINT32)a) >> (32-offset))) : a)
void ROL64(UINT32 inEven, UINT32 inOdd, UINT32 *outEven, UINT32 *outOdd, unsigned int offset)
{
if ((offset % 2) == 0) {
*outEven = ROL32(inEven, offset/2);
*outOdd = ROL32(inOdd, offset/2);
}
else {
*outEven = ROL32(inOdd, (offset+1)/2);
*outOdd = ROL32(inEven, (offset-1)/2);
}
}
void theta(UINT32 *A)
{
unsigned int x, y, z;
UINT32 C[5][2], D[5][2];
for(x=0; x<5; x++) {
for(z=0; z<2; z++) {
C[x][z] = 0;
for(y=0; y<5; y++)
C[x][z] ^= A[index(x, y, z)];
}
}
for(x=0; x<5; x++) {
ROL64(C[(x+1)%5][0], C[(x+1)%5][1], &(D[x][0]), &(D[x][1]), 1);
for(z=0; z<2; z++)
D[x][z] ^= C[(x+4)%5][z];
}
for(x=0; x<5; x++)
for(y=0; y<5; y++)
for(z=0; z<2; z++)
A[index(x, y, z)] ^= D[x][z];
}
void rho(UINT32 *A)
{
unsigned int x, y;
for(x=0; x<5; x++) for(y=0; y<5; y++)
ROL64(A[index(x, y, 0)], A[index(x, y, 1)], &(A[index(x, y, 0)]), &(A[index(x, y, 1)]), KeccakRhoOffsets[5*y+x]);
}
void pi(UINT32 *A)
{
unsigned int x, y, z;
UINT32 tempA[50];
for(x=0; x<5; x++) for(y=0; y<5; y++) for(z=0; z<2; z++)
tempA[index(x, y, z)] = A[index(x, y, z)];
for(x=0; x<5; x++) for(y=0; y<5; y++) for(z=0; z<2; z++)
A[index(0*x+1*y, 2*x+3*y, z)] = tempA[index(x, y, z)];
}
void chi(UINT32 *A)
{
unsigned int x, y, z;
UINT32 C[5][2];
for(y=0; y<5; y++) {
for(x=0; x<5; x++)
for(z=0; z<2; z++)
C[x][z] = A[index(x, y, z)] ^ ((~A[index(x+1, y, z)]) & A[index(x+2, y, z)]);
for(x=0; x<5; x++)
for(z=0; z<2; z++)
A[index(x, y, z)] = C[x][z];
}
}
void iota(UINT32 *A, unsigned int indexRound)
{
A[index(0, 0, 0)] ^= KeccakRoundConstants[indexRound][0];
A[index(0, 0, 1)] ^= KeccakRoundConstants[indexRound][1];
}
/* ---------------------------------------------------------------- */
void KeccakF1600_StateExtractBytesInLane(const void *state, unsigned int lanePosition, unsigned char *data, unsigned int offset, unsigned int length)
{
if ((lanePosition < 25) && (offset < 8) && (offset+length <= 8)) {
UINT32 *stateAsHalfLanes = (UINT32*)state;
UINT32 lane[2];
UINT8 laneAsBytes[8];
fromBitInterleaving(stateAsHalfLanes[lanePosition*2], stateAsHalfLanes[lanePosition*2+1], lane, lane+1);
laneAsBytes[0] = lane[0] & 0xFF;
laneAsBytes[1] = (lane[0] >> 8) & 0xFF;
laneAsBytes[2] = (lane[0] >> 16) & 0xFF;
laneAsBytes[3] = (lane[0] >> 24) & 0xFF;
laneAsBytes[4] = lane[1] & 0xFF;
laneAsBytes[5] = (lane[1] >> 8) & 0xFF;
laneAsBytes[6] = (lane[1] >> 16) & 0xFF;
laneAsBytes[7] = (lane[1] >> 24) & 0xFF;
memcpy(data, laneAsBytes+offset, length);
}
}
void KeccakF1600_StateExtractBytes(const void *state, unsigned char *data, unsigned int offset, unsigned int length)
{
if ((offset < 200) && (offset+length <= 200)) {
unsigned int lanePosition = offset/8;
unsigned int offsetInLane = offset%8;
while(length > 0) {
unsigned int bytesInLane = 8 - offsetInLane;
if (bytesInLane > length)
bytesInLane = length;
KeccakF1600_StateExtractBytesInLane(state, lanePosition, data, offsetInLane, bytesInLane);
length -= bytesInLane;
lanePosition++;
offsetInLane = 0;
data += bytesInLane;
}
}
}
/* ---------------------------------------------------------------- */
void KeccakF1600_StateExtractAndXORBytesInLane(const void *state, unsigned int lanePosition, unsigned char *data, unsigned int offset, unsigned int length)
{
if ((lanePosition < 25) && (offset < 8) && (offset+length <= 8)) {
UINT8 laneAsBytes[8];
unsigned int i;
KeccakF1600_StateExtractBytesInLane(state, lanePosition, laneAsBytes, offset, length);
for(i=0; i<length; i++)
data[i] ^= laneAsBytes[i];
}
}
void KeccakF1600_StateExtractAndXORBytes(const void *state, unsigned char *data, unsigned int offset, unsigned int length)
{
if ((offset < 200) && (offset+length <= 200)) {
unsigned int lanePosition = offset/8;
unsigned int offsetInLane = offset%8;
while(length > 0) {
unsigned int bytesInLane = 8 - offsetInLane;
if (bytesInLane > length)
bytesInLane = length;
KeccakF1600_StateExtractAndXORBytesInLane(state, lanePosition, data, offsetInLane, bytesInLane);
length -= bytesInLane;
lanePosition++;
offsetInLane = 0;
data += bytesInLane;
}
}
}
/* ---------------------------------------------------------------- */
void displayRoundConstants(FILE *f)
{
unsigned int i;
for(i=0; i<nrRounds; i++) {
fprintf(f, "RC[%02i][0][0] = ", i);
fprintf(f, "%08X:%08X", (unsigned int)(KeccakRoundConstants[i][0]), (unsigned int)(KeccakRoundConstants[i][1]));
fprintf(f, "\n");
}
fprintf(f, "\n");
}
void displayRhoOffsets(FILE *f)
{
unsigned int x, y;
for(y=0; y<5; y++) for(x=0; x<5; x++) {
fprintf(f, "RhoOffset[%i][%i] = ", x, y);
fprintf(f, "%2i", KeccakRhoOffsets[5*y+x]);
fprintf(f, "\n");
}
fprintf(f, "\n");
}