Debian Codegraph @

src/keccak.c

raw symbols SCIP
1 /*
2 * An implementation of the SHA3 (Keccak) hash function family.
3 *
4 * Algorithm specifications: http://keccak.noekeon.org/
5 * NIST Announcement:
6 * http://csrc.nist.gov/groups/ST/hash/sha-3/winner_sha-3.html
7 *
8 * Written in 2013 by Fabrizio Tarizzo <[email protected]>
9 *
10 * ===================================================================
11 * The contents of this file are dedicated to the public domain. To
12 * the extent that dedication to the public domain is not available,
13 * everyone is granted a worldwide, perpetual, royalty-free,
14 * non-exclusive license to exercise all rights associated with the
15 * contents of this file for any purpose whatsoever.
16 * No rights are reserved.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 * ===================================================================
27 */
28
29 #include <string.h>
30 #include <assert.h>
31 #include "common.h"
32 #include "endianess.h"
33
34 FAKE_INIT(keccak)
35
36 #define KECCAK_F1600_STATE 200
37
38 typedef struct
39 {
40 uint64_t state[25];
41
42 /* The buffer is as long as the state,
43 * but only 'rate' bytes will be used.
44 */
45 uint8_t buf[KECCAK_F1600_STATE];
46
47 /* When absorbing, this is the number of bytes in buf that
48 * are coming from the message and outstanding.
49 * When squeezing, this is the remaining number of bytes
50 * that can be used as digest.
51 */
52 unsigned valid_bytes;
53
54 /* All values in bytes */
55 unsigned capacity;
56 unsigned rate;
57
58 uint8_t squeezing;
59 uint8_t rounds;
60 } keccak_state;
61
62 #undef ROL64
63 #define ROL64(x,y) ((((x) << (y)) | (x) >> (64-(y))) & 0xFFFFFFFFFFFFFFFFULL)
64
65 static void keccak_function (uint64_t *state, unsigned rounds);
66
67 EXPORT_SYM int keccak_reset(keccak_state *state)
68 {
69 if (NULL == state)
70 return ERR_NULL;
71
72 memset(state->state, 0, sizeof(state->state));
73 memset(state->buf, 0, sizeof(state->buf));
74 state->valid_bytes = 0;
75 state->squeezing = 0;
76
77 return 0;
78 }
79
80 static void keccak_absorb_internal (keccak_state *self)
81 {
82 unsigned i,j;
83 uint64_t d;
84
85 for (i=j=0; j < self->rate; ++i, j += 8) {
86 d = LOAD_U64_LITTLE(self->buf + j);
87 self->state[i] ^= d;
88 }
89 }
90
91 static void
92 keccak_squeeze_internal (keccak_state *self)
93 {
94 unsigned i, j;
95
96 for (i=j=0; j < self->rate; ++i, j += 8) {
97 STORE_U64_LITTLE(self->buf+j, self->state[i]);
98 }
99 }
100
101 EXPORT_SYM int keccak_init (keccak_state **state,
102 size_t capacity_bytes,
103 uint8_t rounds)
104 {
105 keccak_state *ks;
106
107 if (NULL == state) {
108 return ERR_NULL;
109 }
110
111 *state = ks = (keccak_state*) calloc(1, sizeof(keccak_state));
112 if (NULL == ks)
113 return ERR_MEMORY;
114
115 if (capacity_bytes >= KECCAK_F1600_STATE)
116 return ERR_DIGEST_SIZE;
117
118 if ((rounds != 12) && (rounds != 24))
119 return ERR_NR_ROUNDS;
120
121 ks->capacity = (unsigned)capacity_bytes;
122
123 ks->rate = KECCAK_F1600_STATE - ks->capacity;
124
125 ks->squeezing = 0;
126 ks->rounds = rounds;
127
128 return 0;
129 }
130
131 EXPORT_SYM int keccak_destroy(keccak_state *state)
132 {
133 free(state);
134 return 0;
135 }
136
137 EXPORT_SYM int keccak_absorb (keccak_state *self,
138 const uint8_t *in,
139 size_t length)
140 {
141 if (NULL==self || NULL==in)
142 return ERR_NULL;
143
144 if (self->squeezing != 0)
145 return ERR_UNKNOWN;
146
147 while (length > 0) {
148 unsigned tc;
149 unsigned left;
150
151 left = self->rate - self->valid_bytes;
152 tc = (unsigned) MIN(length, left);
153 memcpy(self->buf + self->valid_bytes, in, tc);
154
155 self->valid_bytes += tc;
156 in += tc;
157 length -= tc;
158
159 if (self->valid_bytes == self->rate) {
160 keccak_absorb_internal (self);
161 keccak_function(self->state, self->rounds);
162 self->valid_bytes = 0;
163 }
164 }
165
166 return 0;
167 }
168
169 static void keccak_finish (keccak_state *self, uint8_t padding)
170 {
171 assert(self->squeezing == 0);
172 assert(self->valid_bytes < self->rate);
173
174 /* Padding */
175 memset(self->buf + self->valid_bytes, 0, self->rate - self->valid_bytes);
176 self->buf[self->valid_bytes] = padding;
177 self->buf[self->rate-1] |= 0x80;
178
179 /* Final absorb */
180 keccak_absorb_internal (self);
181 keccak_function (self->state, self->rounds);
182
183 /* First squeeze */
184 self->squeezing = 1;
185 keccak_squeeze_internal (self);
186 self->valid_bytes = self->rate;
187 }
188
189 EXPORT_SYM int keccak_squeeze (keccak_state *self, uint8_t *out, size_t length, uint8_t padding)
190 {
191 if ((NULL == self) || (NULL == out))
192 return ERR_NULL;
193
194 if (self->squeezing == 0) {
195 keccak_finish (self, padding);
196 }
197
198 assert(self->squeezing == 1);
199 assert(self->valid_bytes > 0);
200 assert(self->valid_bytes <= self->rate);
201
202 while (length > 0) {
203 unsigned tc;
204
205 tc = (unsigned)MIN(self->valid_bytes, length);
206 memcpy(out, self->buf + (self->rate - self->valid_bytes), tc);
207
208 self->valid_bytes -= tc;
209 out += tc;
210 length -= tc;
211
212 if (self->valid_bytes == 0) {
213 keccak_function (self->state, self->rounds);
214 keccak_squeeze_internal (self);
215 self->valid_bytes = self->rate;
216 }
217 }
218
219 return 0;
220 }
221
222 EXPORT_SYM int keccak_digest(keccak_state *state, uint8_t *digest, size_t len, uint8_t padding)
223 {
224 keccak_state tmp;
225
226 if ((NULL==state) || (NULL==digest))
227 return ERR_NULL;
228
229 if (2*len != state->capacity)
230 return ERR_UNKNOWN;
231
232 tmp = *state;
233 return keccak_squeeze(&tmp, digest, len, padding);
234 }
235
236 EXPORT_SYM int keccak_copy(const keccak_state *src, keccak_state *dst)
237 {
238 if (NULL == src || NULL == dst) {
239 return ERR_NULL;
240 }
241
242 *dst = *src;
243 return 0;
244 }
245
246 /* Keccak core function */
247
248 #define KECCAK_ROUNDS 24
249
250 #define ROT_01 36
251 #define ROT_02 3
252 #define ROT_03 41
253 #define ROT_04 18
254 #define ROT_05 1
255 #define ROT_06 44
256 #define ROT_07 10
257 #define ROT_08 45
258 #define ROT_09 2
259 #define ROT_10 62
260 #define ROT_11 6
261 #define ROT_12 43
262 #define ROT_13 15
263 #define ROT_14 61
264 #define ROT_15 28
265 #define ROT_16 55
266 #define ROT_17 25
267 #define ROT_18 21
268 #define ROT_19 56
269 #define ROT_20 27
270 #define ROT_21 20
271 #define ROT_22 39
272 #define ROT_23 8
273 #define ROT_24 14
274
275 static const uint64_t roundconstants[KECCAK_ROUNDS] = {
276 0x0000000000000001ULL,
277 0x0000000000008082ULL,
278 0x800000000000808aULL,
279 0x8000000080008000ULL,
280 0x000000000000808bULL,
281 0x0000000080000001ULL,
282 0x8000000080008081ULL,
283 0x8000000000008009ULL,
284 0x000000000000008aULL,
285 0x0000000000000088ULL,
286 0x0000000080008009ULL,
287 0x000000008000000aULL,
288 0x000000008000808bULL,
289 0x800000000000008bULL,
290 0x8000000000008089ULL,
291 0x8000000000008003ULL,
292 0x8000000000008002ULL,
293 0x8000000000000080ULL,
294 0x000000000000800aULL,
295 0x800000008000000aULL,
296 0x8000000080008081ULL,
297 0x8000000000008080ULL,
298 0x0000000080000001ULL,
299 0x8000000080008008ULL
300 };
301
302 static void keccak_function (uint64_t *state, unsigned rounds)
303 {
304 unsigned i;
305 unsigned start_round;
306
307 /* Temporary variables to avoid indexing overhead */
308 uint64_t a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12;
309 uint64_t a13, a14, a15, a16, a17, a18, a19, a20, a21, a22, a23, a24;
310
311 uint64_t b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12;
312 uint64_t b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23, b24;
313
314 uint64_t c0, c1, c2, c3, c4, d;
315
316 a0 = state[0];
317 a1 = state[1];
318 a2 = state[2];
319 a3 = state[3];
320 a4 = state[4];
321 a5 = state[5];
322 a6 = state[6];
323 a7 = state[7];
324 a8 = state[8];
325 a9 = state[9];
326 a10 = state[10];
327 a11 = state[11];
328 a12 = state[12];
329 a13 = state[13];
330 a14 = state[14];
331 a15 = state[15];
332 a16 = state[16];
333 a17 = state[17];
334 a18 = state[18];
335 a19 = state[19];
336 a20 = state[20];
337 a21 = state[21];
338 a22 = state[22];
339 a23 = state[23];
340 a24 = state[24];
341
342 if (rounds == 24)
343 start_round = 0;
344 else /* rounds == 12 */
345 start_round = 12;
346
347 for (i = start_round; i < KECCAK_ROUNDS; ++i) {
348 /*
349 Uses temporary variables and loop unrolling to
350 avoid array indexing and inner loops overhead
351 */
352
353 /* Prepare column parity for Theta step */
354 c0 = a0 ^ a5 ^ a10 ^ a15 ^ a20;
355 c1 = a1 ^ a6 ^ a11 ^ a16 ^ a21;
356 c2 = a2 ^ a7 ^ a12 ^ a17 ^ a22;
357 c3 = a3 ^ a8 ^ a13 ^ a18 ^ a23;
358 c4 = a4 ^ a9 ^ a14 ^ a19 ^ a24;
359
360 /* Theta + Rho + Pi steps */
361 d = c4 ^ ROL64(c1, 1);
362 b0 = d ^ a0;
363 b16 = ROL64(d ^ a5, ROT_01);
364 b7 = ROL64(d ^ a10, ROT_02);
365 b23 = ROL64(d ^ a15, ROT_03);
366 b14 = ROL64(d ^ a20, ROT_04);
367
368 d = c0 ^ ROL64(c2, 1);
369 b10 = ROL64(d ^ a1, ROT_05);
370 b1 = ROL64(d ^ a6, ROT_06);
371 b17 = ROL64(d ^ a11, ROT_07);
372 b8 = ROL64(d ^ a16, ROT_08);
373 b24 = ROL64(d ^ a21, ROT_09);
374
375 d = c1 ^ ROL64(c3, 1);
376 b20 = ROL64(d ^ a2, ROT_10);
377 b11 = ROL64(d ^ a7, ROT_11);
378 b2 = ROL64(d ^ a12, ROT_12);
379 b18 = ROL64(d ^ a17, ROT_13);
380 b9 = ROL64(d ^ a22, ROT_14);
381
382 d = c2 ^ ROL64(c4, 1);
383 b5 = ROL64(d ^ a3, ROT_15);
384 b21 = ROL64(d ^ a8, ROT_16);
385 b12 = ROL64(d ^ a13, ROT_17);
386 b3 = ROL64(d ^ a18, ROT_18);
387 b19 = ROL64(d ^ a23, ROT_19);
388
389 d = c3 ^ ROL64(c0, 1);
390 b15 = ROL64(d ^ a4, ROT_20);
391 b6 = ROL64(d ^ a9, ROT_21);
392 b22 = ROL64(d ^ a14, ROT_22);
393 b13 = ROL64(d ^ a19, ROT_23);
394 b4 = ROL64(d ^ a24, ROT_24);
395
396 /* Chi + Iota steps */
397 a0 = b0 ^ (~b1 & b2) ^ roundconstants[i];
398 a1 = b1 ^ (~b2 & b3);
399 a2 = b2 ^ (~b3 & b4);
400 a3 = b3 ^ (~b4 & b0);
401 a4 = b4 ^ (~b0 & b1);
402
403 a5 = b5 ^ (~b6 & b7);
404 a6 = b6 ^ (~b7 & b8);
405 a7 = b7 ^ (~b8 & b9);
406 a8 = b8 ^ (~b9 & b5);
407 a9 = b9 ^ (~b5 & b6);
408
409 a10 = b10 ^ (~b11 & b12);
410 a11 = b11 ^ (~b12 & b13);
411 a12 = b12 ^ (~b13 & b14);
412 a13 = b13 ^ (~b14 & b10);
413 a14 = b14 ^ (~b10 & b11);
414
415 a15 = b15 ^ (~b16 & b17);
416 a16 = b16 ^ (~b17 & b18);
417 a17 = b17 ^ (~b18 & b19);
418 a18 = b18 ^ (~b19 & b15);
419 a19 = b19 ^ (~b15 & b16);
420
421 a20 = b20 ^ (~b21 & b22);
422 a21 = b21 ^ (~b22 & b23);
423 a22 = b22 ^ (~b23 & b24);
424 a23 = b23 ^ (~b24 & b20);
425 a24 = b24 ^ (~b20 & b21);
426 }
427
428 state[0] = a0;
429 state[1] = a1;
430 state[2] = a2;
431 state[3] = a3;
432 state[4] = a4;
433 state[5] = a5;
434 state[6] = a6;
435 state[7] = a7;
436 state[8] = a8;
437 state[9] = a9;
438 state[10] = a10;
439 state[11] = a11;
440 state[12] = a12;
441 state[13] = a13;
442 state[14] = a14;
443 state[15] = a15;
444 state[16] = a16;
445 state[17] = a17;
446 state[18] = a18;
447 state[19] = a19;
448 state[20] = a20;
449 state[21] = a21;
450 state[22] = a22;
451 state[23] = a23;
452 state[24] = a24;
453 }
454
455