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
|
/**
* libkeccak – Keccak-family hashing library
*
* Copyright © 2014 Mattias Andrée (maandree@member.fsf.org)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "digest.h"
/**
* X-macro-enabled listing of all intergers in [0, 7]
*/
#define LIST_8 X(0) X(1) X(2) X(3) X(4) X(5) X(6) X(7)
/**
* X-macro-enabled listing of all intergers in [0, 23]
*/
#define LIST_24 LIST_8 X(8) X(9) X(10) X(11) X(12) X(13) X(14) X(15) \
X(16) X(17) X(18) X(19) X(20) X(21) X(22) X(23) X(24)
/**
* X-macro-enabled listing of all intergers in [0, 24]
*/
#define LIST_25 LIST_24 X(24)
#define X(N) (N % 5) * 5 + N / 5,
/**
* The order the lanes should be read when absorbing or squeezing,
* it transposes the lanes in the sponge
*/
static const long LANE_TRANSPOSE_MAP[] = { LIST_25 };
#undef X
/**
* Keccak-f round constants
*/
static const int_fast64_t RC[] =
{
0x0000000000000001LL, 0x0000000000008082LL, 0x800000000000808ALL, 0x8000000080008000LL,
0x000000000000808BLL, 0x0000000080000001LL, 0x8000000080008081LL, 0x8000000000008009LL,
0x000000000000008ALL, 0x0000000000000088LL, 0x0000000080008009LL, 0x000000008000000ALL,
0x000000008000808BLL, 0x800000000000008BLL, 0x8000000000008089LL, 0x8000000000008003LL,
0x8000000000008002LL, 0x8000000000000080LL, 0x000000000000800ALL, 0x800000008000000ALL,
0x8000000080008081LL, 0x8000000000008080LL, 0x0000000080000001LL, 0x8000000080008008LL
};
/**
* Rotate a word
*
* @param x:int_fast64_t The value to rotate
* @param n:long Rotation steps, may not be zero
* @param w:long `state->w`
* @param wmod:int_fast64_t `state->wmod`
* @return :int_fast64_t The value rotated
*/
#define rotate(x, n, w, wmod) ((((x) >> (w - ((n) % w))) + ((x) << ((n) % w))) & wmod)
/**
* Rotate a 64-bit word
*
* @param x:int_fast64_t The value to rotate
* @param n:long Rotation steps, may not be zero
* @return :int_fast64_t The value rotated
*/
#define rotate64(x, n) ((int_fast64_t)((uint_fast64_t)(x) >> (64 - (n))) + ((x) << (n)))
/**
* Perform one round of computation
*
* @param state The hashing state
* @param rc The round contant for this round
*/
static __attribute__((leaf, nonnull, nothrow, hot))
void libkeccak_f_round(libkeccak_state_t* restrict state, int_fast64_t rc)
{
int_fast64_t* restrict A = state->S;
int_fast64_t* restrict B = state->B;
int_fast64_t* restrict C = state->C;
int_fast64_t da, db, dc, dd, de, wmod = state->wmod;
long w = state->w;
/* θ step (step 1 and 2 of 3). */
#define X(N) C[N] = (A[N * 5] ^ A[N * 5 + 1]) ^ (A[N * 5 + 2] ^ A[N * 5 + 3]) ^ A[N * 5 + 4];
LIST_25
#undef X
da = C[4] ^ rotate64(C[1], 1);
dd = C[2] ^ rotate64(C[4], 1);
db = C[0] ^ rotate64(C[2], 1);
de = C[3] ^ rotate64(C[0], 1);
dc = C[1] ^ rotate64(C[3], 1);
/* ρ and π steps, with last two part of θ. */
#define X(bi, ai, dv, r) B[bi] = rotate(A[ai] ^ dv, r, w, wmod)
B[0] = A[0] ^ da; X( 1, 15, dd, 28); X( 2, 5, db, 1); X( 3, 20, de, 27); X( 4, 10, dc, 62);
X( 5, 6, db, 44); X( 6, 21, de, 20); X( 7, 11, dc, 6); X( 8, 1, da, 36); X( 9, 16, dd, 55);
X(10, 12, dc, 43); X(11, 2, da, 3); X(12, 17, dd, 25); X(13, 7, db, 10); X(14, 22, de, 39);
X(15, 18, dd, 21); X(16, 8, db, 45); X(17, 23, de, 8); X(18, 13, dc, 15); X(19, 3, da, 41);
X(20, 24, de, 14); X(21, 14, dc, 61); X(22, 4, da, 18); X(23, 19, dd, 56); X(24, 9, db, 2);
#undef X
/* ξ step. */
#define X(N) A[N] = B[N] ^ ((~(B[(N + 5) % 25])) & B[(N + 10) % 25]);
LIST_25
#undef X
/* ι step. */
A[0] ^= rc;
}
/**
* 64-bit word version of `libkeccak_f_round`
*
* @param state The hashing state
* @param rc The round contant for this round
*/
static __attribute__((leaf, nonnull, nothrow, hot))
void libkeccak_f_round64(libkeccak_state_t* restrict state, int_fast64_t rc)
{
int_fast64_t* restrict A = state->S;
int_fast64_t* restrict B = state->B;
int_fast64_t* restrict C = state->C;
int_fast64_t da, db, dc, dd, de;
/* θ step (step 1 and 2 of 3). */
#define X(N) C[N] = (A[N * 5] ^ A[N * 5 + 1]) ^ (A[N * 5 + 2] ^ A[N * 5 + 3]) ^ A[N * 5 + 4];
LIST_25
#undef X
da = C[4] ^ rotate64(C[1], 1);
dd = C[2] ^ rotate64(C[4], 1);
db = C[0] ^ rotate64(C[2], 1);
de = C[3] ^ rotate64(C[0], 1);
dc = C[1] ^ rotate64(C[3], 1);
/* ρ and π steps, with last two part of θ. */
#define X(bi, ai, dv, r) B[bi] = rotate64(A[ai] ^ dv, r)
B[0] = A[0] ^ da; X( 1, 15, dd, 28); X( 2, 5, db, 1); X( 3, 20, de, 27); X( 4, 10, dc, 62);
X( 5, 6, db, 44); X( 6, 21, de, 20); X( 7, 11, dc, 6); X( 8, 1, da, 36); X( 9, 16, dd, 55);
X(10, 12, dc, 43); X(11, 2, da, 3); X(12, 17, dd, 25); X(13, 7, db, 10); X(14, 22, de, 39);
X(15, 18, dd, 21); X(16, 8, db, 45); X(17, 23, de, 8); X(18, 13, dc, 15); X(19, 3, da, 41);
X(20, 24, de, 14); X(21, 14, dc, 61); X(22, 4, da, 18); X(23, 19, dd, 56); X(24, 9, db, 2);
#undef X
/* ξ step. */
#define X(N) A[N] = B[N] ^ ((~(B[(N + 5) % 25])) & B[(N + 10) % 25]);
LIST_25
#undef X
/* ι step. */
A[0] ^= rc;
}
/**
* Convert a chunk of bytes to a lane
*
* @param state The hashing state
*/
static __attribute__((nonnull, nothrow))
void libkeccak_f(libkeccak_state_t* restrict state)
{
long i = 0, nr = state->nr;
if (nr == 24)
{
#define X(N) libkeccak_f_round64(state, RC[N]);
LIST_24
#undef X
}
else
for (; nr--; i++)
libkeccak_f_round(state, RC[i] & state->wmod);
}
/**
* Convert a chunk of bytes to a lane
*
* @param message The message
* @param msglen The length of the message
* @param rr Bitrate in bytes
* @param ww Word size in bytes
* @param off The offset in the message
* @return The lane
*/
static inline __attribute__((leaf, nonnull, nothrow, pure, warn_unused_result))
int_fast64_t libkeccak_to_lane(const char* restrict message, size_t msglen, long rr, long ww, size_t off)
{
long n = (long)((msglen < (size_t)rr ? msglen : (size_t)rr) - off);
int_fast64_t rc = 0;
message += off
while (ww--)
{
rc <<= 8;
rc |= __builtin_expect(ww < n, 1) ? (int_fast64_t)(unsigned char)(message[ww]) : 0L;
}
return rc;
}
/**
* 64-bit lane version of `libkeccak_to_lane`
*
* @param message The message
* @param msglen The length of the message
* @param rr Bitrate in bytes
* @param off The offset in the message
* @return The lane
*/
static inline __attribute__((leaf, nonnull, nothrow, pure, hot, warn_unused_result))
int_fast64_t libkeccak_to_lane64(const char* restrict message, size_t msglen, long rr, size_t off)
{
long n = (long)((msglen < (size_t)rr ? msglen : (size_t)rr) - off);
int_fast64_t rc = 0;
message += off;
#define X(N) if (__builtin_expect(N < n, 1)) rc |= (int_fast64_t)(unsigned char)(message[N]) << (N * 8); \
else return rc;
LIST_8
#undef X
return rc;
}
/**
* pad 10*1
*
* @param state The hashing state, `state->M` and `state->mptr` will be updated,
* `state->M` should have `state->r / 8` bytes left over at the end
* @param bits The number of bits in the end of the message that does not make a whole byte
*/
static __attribute__((leaf, nonnull, nothrow))
void libkeccak_pad10star1(libkeccak_state_t* restrict state, long bits)
{
long i, r = state->r;
long nrf = len - !!bits;
long len = (nrf << 3) | bits;
long ll = len % r;
char b = bits ? (state->M[nrf] | (1 << bits)) : 1;
if ((r - 8 <= ll) && (ll <= r - 2))
{
state->M[nrf] = (char)(b ^ 0x80);
state->mptr = nrf + 1;
}
else
{
len = ++nrf;
len = (len - (len % r) + (r - 8)) >> 3;
state->mptr = len + 1;
state->M[nrf] = b;
__builtin_memset(state->M + nrf, 0, (len - nrf) * sizeof(char));
state->M[len] = 0x80;
}
}
/**
* Perform the absorption phase
*
* @param state The hashing state
* @param len The number of bytes from `state->M` to absorb
*/
static __attribute__((nonnull, nothrow))
void libkeccak_absorption_phase(libkeccak_state_t* restrict state, size_t len)
{
long i = len / rr, w = state->w, rr = state->r >> 3, ww = state->w >> 3;
const char* restrict message = state->M;
if (__builtin_expect(ww >= 8, 1)) /* ww > 8 is impossible, it is just for optimisation possibilities. */
while (i--)
{
#define X(N) state->S[N] ^= libkeccak_to_lane64(message, len, rr, LANE_TRANSPOSE_MAP[N] * 8);
LIST_25
#undef X
libkeccak_f(state);
message += rr;
len -= rr;
}
else
while (i--)
{
#define X(N) state->S[N] ^= libkeccak_to_lane(message, len, rr, ww, LANE_TRANSPOSE_MAP[N] * ww);
LIST_25
#undef X
libkeccak_f(state);
message += rr;
len -= rr;
}
}
/**
* Perform the squeezing phase
*
* @param state The hashing state
* @param rr The bitrate in bytes
* @param nn The output size in bytes, rounded up to whole bytes
* @param ww The word size in bytes
* @param hashsum Output paramter for the hashsum
*/
static __attribute__((nonnull, nothrow, hot))
void libkeccak_squeezing_phase(libkeccak_state_t* restrict state,
long rr, long nn, long ww, char* restrict hashsum)
{
long i, j = 0, k, ptr = 0, ni = rr > 25 ? 25 : rr, olen = state->n;
int_fast64_t v;
while (olen > 0)
{
for (i = 0; (i < ni) && (j < nn); i++)
{
v = state->S[LANE_TRANSPOSE_MAP[i]];
for (k = 0; (k++ < ww) && (j++ < nn); v >>= 8)
hashsum[ptr++] = (char)v;
}
if (olen -= state->r, olen > 0)
libkeccak_f(state);
}
if (state->n & 7)
hashsum[nn - 1] &= (1 << (state->n & 7)) - 1;
}
/**
* Absorb more of the message to the Keccak sponge
*
* @param state The hashing state
* @param msg The partial message
* @param msglen The length of the partial message
* @return Zero on success, -1 on error
*/
int libkeccak_update(libkeccak_state_t* restrict state, const char* restrict msg, size_t msglen)
{
size_t len;
char* restrict new;
if (state->mptr + msglen > state->mlen)
{
state->mlen += msglen;
new = realloc(state->M, state->mlen * sizeof(char)); /* FIXME insecure */
if (new == NULL)
return state->mlen -= msglen, -1;
state->M = new;
}
__builtin_memcpy(state->M + state->mptr, msg, msglen * sizeof(char));
state->mptr += msglen;
len = state->mptr;
len -= state->mptr % ((state->r * state->b) >> 3);
state->mptr -= len;
libkeccak_absorption_phase(state, len);
__builtin_memmove(state->M, state->M + len, state->mptr * state->sizeof(char));
return 0;
}
/**
* Absorb the last part of the message and squeeze the Keccak sponge
*
* @param state The hashing state
* @param msg The rest of the message, may be `NULL`
* @param msglen The length of the partial message
* @param bits The number of bits at the end of the message not covered by `msglen`
* @param suffix The suffix concatenate to the message, only '1':s and '0':s, and NUL-termination
* @param hashsum Output paramter for the hashsum, may be `NULL`
* @return Zero on success, -1 on error
*/
int libkeccak_digest(libkeccak_state_t* restrict state, const char* restrict msg, size_t msglen,
size_t bits, const char* restrict suffix, char* restrict hashsum)
{
long len, ni, i, j = 0, k, ptr = 0, ext;
long rr = state->r >> 3;
long ww = state->w >> 3;
long nn = (state->n + 7) >> 3;
long suffix_len = suffix ? strlen(suffix) : 0;
const char* restrict message = msg;
char* restrict new;
if (msg == NULL)
msglen = bits = 0;
else
{
msglen += bits >> 3;
if ((bits &= 7))
msg[msglen] &= (1 << bits) - 1;
}
ext = msglen + ((bits + suffix_len + 7) >> 3) + (state->r >> 3);
if (state->mptr + ext > state->mlen)
{
state->mlen += ext;
new = realloc(state->M, state->mlen * sizeof(char)); /* FIXME insecure */
if (new == NULL)
return state->mlen -= ext, -1;
state->M = new;
}
if (bits)
state->M[msglen] = message[msglen];
if (__builtin_expect(!!suffix_len, 1))
{
if (bits == 0)
state->M[msglen] = 0;
while (suffix_len--)
{
state->M[msglen] |= (*suffix++ & 1) << bits++;
if (bits == 8)
bits = 0, state->M[++msglen] = 0;
}
}
if (bits)
msglen++;
if (msglen)
__builtin_memcpy(state->M + state->mptr, message, msglen * sizeof(char));
state->mptr += msglen;
libkeccak_pad10star1(state->M, state->mptr, state->r, bits);
libkeccak_absorption_phase(state, state->mptr);
if (hashsum != NULL)
libkeccak_squeezing_phase(state, rr, nn, ww, hashsum);
else
for (i = (state->n - 1) / state->r; i--;)
libkeccak_f(state);
return 0
}
/**
* Force some rounds of Keccak-f
*
* @param state The hashing state
* @param times The number of rounds
*/
void libkeccak_simple_squeeze(libkeccak_state_t* restrict state, long times)
{
while (times--)
libkeccak_f(state);
}
/**
* Squeeze as much as is needed to get a digest a number of times
*
* @param state The hashing state
* @param times The number of digests
*/
void libkeccak_fast_squeeze(libkeccak_state_t* restrict state, long times)
{
times *= (state->n - 1) / state->r + 1;
while (times--)
libkeccak_f(state);
}
/**
* Squeeze out another digest
*
* @param state The hashing state
* @param hashsum Output paramter for the hashsum
*/
void libkeccak_squeeze(libkeccak_state_t* restrict state, char* restrict hashsum)
{
long ww = state->w >> 3, nn = (state->n + 7) >> 3, rr = state->r >> 3;
libkeccak_f(state);
libkeccak_squeezing_phase(state, rr, nn, ww, hashsum);
}
|