2 * Copyright (C) 2015-2016 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
5 * Based on the implementation by the Keccak, Keyak and Ketje Teams, namely,
6 * Guido Bertoni, Joan Daemen, Michaël Peeters, Gilles Van Assche and
7 * Ronny Van Keer, hereby denoted as "the implementer".
9 * To the extent possible under law, the implementer has waived all copyright
10 * and related or neighboring rights to the source code in this file.
11 * http://creativecommons.org/publicdomain/zero/1.0/
16 #include "sha3_keccak.h"
18 typedef struct private_sha3_keccak_t private_sha3_keccak_t
;
20 #define KECCAK_STATE_SIZE 200 /* bytes */
21 #define KECCAK_MAX_RATE 168 /* bytes */
23 static const uint64_t round_constants
[] = {
24 0x0000000000000001ULL
,
25 0x0000000000008082ULL
,
26 0x800000000000808aULL
,
27 0x8000000080008000ULL
,
28 0x000000000000808bULL
,
29 0x0000000080000001ULL
,
30 0x8000000080008081ULL
,
31 0x8000000000008009ULL
,
32 0x000000000000008aULL
,
33 0x0000000000000088ULL
,
34 0x0000000080008009ULL
,
35 0x000000008000000aULL
,
36 0x000000008000808bULL
,
37 0x800000000000008bULL
,
38 0x8000000000008089ULL
,
39 0x8000000000008003ULL
,
40 0x8000000000008002ULL
,
41 0x8000000000000080ULL
,
42 0x000000000000800aULL
,
43 0x800000008000000aULL
,
44 0x8000000080008081ULL
,
45 0x8000000000008080ULL
,
46 0x0000000080000001ULL
,
51 * Private data structure with hashing context for SHA-3
53 struct private_sha3_keccak_t
{
56 * Public interface for this hasher.
61 * Internal state of 1600 bits as defined by FIPS-202
63 uint8_t state
[KECCAK_STATE_SIZE
];
73 uint8_t rate_buffer
[KECCAK_MAX_RATE
];
76 * Index pointing to the current position in the rate buffer
81 * Suffix delimiting the input message
83 uint8_t delimited_suffix
;
87 #if BYTE_ORDER != LITTLE_ENDIAN
89 * Function to load a 64-bit value using the little-endian (LE) convention.
90 * On a LE platform, this could be greatly simplified using a cast.
92 static uint64_t load64(const uint8_t *x
)
97 for (i
= 7; i
>= 0; --i
)
106 * Function to store a 64-bit value using the little-endian (LE) convention.
107 * On a LE platform, this could be greatly simplified using a cast.
109 static void store64(uint8_t *x
, uint64_t u
)
113 for (i
= 0; i
< 8; ++i
)
121 * Function to XOR into a 64-bit value using the little-endian (LE) convention.
122 * On a LE platform, this could be greatly simplified using a cast.
124 static void xor64(uint8_t *x
, uint64_t u
)
128 for (i
= 0; i
< 8; ++i
)
137 * Some macros used by the Keccak-f[1600] permutation.
139 #define ROL64(a, offset) ((((uint64_t)a) << offset) ^ (((uint64_t)a) >> (64-offset)))
141 #if BYTE_ORDER == LITTLE_ENDIAN
142 #define readLane(i) (((uint64_t*)state)[i])
143 #define writeLane(i, lane) (((uint64_t*)state)[i]) = (lane)
144 #define XORLane(i, lane) (((uint64_t*)state)[i]) ^= (lane)
145 #elif BYTE_ORDER == BIG_ENDIAN
146 #define readLane(i) load64((uint8_t*)state+sizeof(uint64_t)*i)
147 #define writeLane(i, lane) store64((uint8_t*)state+sizeof(uint64_t)*i, lane)
148 #define XORLane(i, lane) xor64((uint8_t*)state+sizeof(uint64_t)*i, lane)
152 * Function that computes the Keccak-f[1600] permutation on the given state.
154 static void keccak_f1600_state_permute(void *state
)
158 for (round
= 0; round
< 24; round
++)
160 { /* θ step (see [Keccak Reference, Section 2.3.2]) */
164 /* Compute the parity of the columns */
165 C
[0] = readLane(0) ^ readLane( 5) ^ readLane(10)
166 ^ readLane(15) ^ readLane(20);
167 C
[1] = readLane(1) ^ readLane( 6) ^ readLane(11)
168 ^ readLane(16) ^ readLane(21);
169 C
[2] = readLane(2) ^ readLane( 7) ^ readLane(12)
170 ^ readLane(17) ^ readLane(22);
171 C
[3] = readLane(3) ^ readLane( 8) ^ readLane(13)
172 ^ readLane(18) ^ readLane(23);
173 C
[4] = readLane(4) ^ readLane( 9) ^ readLane(14)
174 ^ readLane(19) ^ readLane(24);
176 /* Compute and add the θ effect to the whole column */
177 D
= C
[4] ^ ROL64(C
[1], 1);
184 D
= C
[0] ^ ROL64(C
[2], 1);
191 D
= C
[1] ^ ROL64(C
[3], 1);
198 D
= C
[2] ^ ROL64(C
[4], 1);
205 D
= C
[3] ^ ROL64(C
[0], 1);
213 { /* ρ and π steps (see [Keccak Reference, Sections 2.3.3 and 2.3.4]) */
220 writeLane(10, ROL64(t1
, 1));
223 writeLane( 7, ROL64(t2
, 3));
226 writeLane(11, ROL64(t1
, 6));
229 writeLane(17, ROL64(t2
, 10));
232 writeLane(18, ROL64(t1
, 15));
235 writeLane( 3, ROL64(t2
, 21));
238 writeLane( 5, ROL64(t1
, 28));
241 writeLane(16, ROL64(t2
, 36));
244 writeLane( 8, ROL64(t1
, 45));
247 writeLane(21, ROL64(t2
, 55));
250 writeLane(24, ROL64(t1
, 2));
253 writeLane( 4, ROL64(t2
, 14));
256 writeLane(15, ROL64(t1
, 27));
259 writeLane(23, ROL64(t2
, 41));
262 writeLane(19, ROL64(t1
, 56));
265 writeLane(13, ROL64(t2
, 8));
268 writeLane(12, ROL64(t1
, 25));
271 writeLane( 2, ROL64(t2
, 43));
274 writeLane(20, ROL64(t1
, 62));
277 writeLane(14, ROL64(t2
, 18));
280 writeLane(22, ROL64(t1
, 39));
283 writeLane( 9, ROL64(t2
, 61));
286 writeLane( 6, ROL64(t1
, 20));
288 writeLane( 1, ROL64(t2
, 44));
291 { /* χ step (see [Keccak Reference, Section 2.3.1]) */
301 writeLane(0, t
[0] ^ ((~t
[1]) & t
[2]));
302 writeLane(1, t
[1] ^ ((~t
[2]) & t
[3]));
303 writeLane(2, t
[2] ^ ((~t
[3]) & t
[4]));
304 writeLane(3, t
[3] ^ ((~t
[4]) & t
[0]));
305 writeLane(4, t
[4] ^ ((~t
[0]) & t
[1]));
313 writeLane(5, t
[0] ^ ((~t
[1]) & t
[2]));
314 writeLane(6, t
[1] ^ ((~t
[2]) & t
[3]));
315 writeLane(7, t
[2] ^ ((~t
[3]) & t
[4]));
316 writeLane(8, t
[3] ^ ((~t
[4]) & t
[0]));
317 writeLane(9, t
[4] ^ ((~t
[0]) & t
[1]));
325 writeLane(10, t
[0] ^ ((~t
[1]) & t
[2]));
326 writeLane(11, t
[1] ^ ((~t
[2]) & t
[3]));
327 writeLane(12, t
[2] ^ ((~t
[3]) & t
[4]));
328 writeLane(13, t
[3] ^ ((~t
[4]) & t
[0]));
329 writeLane(14, t
[4] ^ ((~t
[0]) & t
[1]));
337 writeLane(15, t
[0] ^ ((~t
[1]) & t
[2]));
338 writeLane(16, t
[1] ^ ((~t
[2]) & t
[3]));
339 writeLane(17, t
[2] ^ ((~t
[3]) & t
[4]));
340 writeLane(18, t
[3] ^ ((~t
[4]) & t
[0]));
341 writeLane(19, t
[4] ^ ((~t
[0]) & t
[1]));
349 writeLane(20, t
[0] ^ ((~t
[1]) & t
[2]));
350 writeLane(21, t
[1] ^ ((~t
[2]) & t
[3]));
351 writeLane(22, t
[2] ^ ((~t
[3]) & t
[4]));
352 writeLane(23, t
[3] ^ ((~t
[4]) & t
[0]));
353 writeLane(24, t
[4] ^ ((~t
[0]) & t
[1]));
356 { /* ι step (see [Keccak Reference, Section 2.3.5]) */
358 XORLane(0, round_constants
[round
]);
363 METHOD(sha3_keccak_t
, get_rate
, u_int
,
364 private_sha3_keccak_t
*this)
369 METHOD(sha3_keccak_t
, reset
, void,
370 private_sha3_keccak_t
*this)
372 memset(this->state
, 0x00, KECCAK_STATE_SIZE
);
373 this->rate_index
= 0;
377 METHOD(sha3_keccak_t
, absorb
, void,
378 private_sha3_keccak_t
*this, chunk_t data
)
380 uint64_t *buffer_lanes
, *state_lanes
;
381 size_t len
, rate_lanes
;
384 buffer_lanes
= (uint64_t*)this->rate_buffer
;
385 state_lanes
= (uint64_t*)this->state
;
386 rate_lanes
= this->rate
/ sizeof(uint64_t);
390 len
= min(data
.len
, this->rate
- this->rate_index
);
391 memcpy(this->rate_buffer
+ this->rate_index
, data
.ptr
, len
);
392 this->rate_index
+= len
;
396 if (this->rate_index
== this->rate
)
398 for (i
= 0; i
< rate_lanes
; i
++)
400 state_lanes
[i
] ^= buffer_lanes
[i
];
402 this->rate_index
= 0;
404 keccak_f1600_state_permute(this->state
);
409 METHOD(sha3_keccak_t
, finalize
, void,
410 private_sha3_keccak_t
*this)
412 uint64_t *buffer_lanes
, *state_lanes
;
413 size_t rate_lanes
, remainder
;
416 /* Add the delimitedSuffix as the first bit of padding */
417 this->rate_buffer
[this->rate_index
++] = this->delimited_suffix
;
419 buffer_lanes
= (uint64_t*)this->rate_buffer
;
420 state_lanes
= (uint64_t*)this->state
;
421 rate_lanes
= this->rate_index
/ sizeof(uint64_t);
423 remainder
= this->rate_index
- rate_lanes
* sizeof(uint64_t);
426 memset(this->rate_buffer
+ this->rate_index
, 0x00,
427 sizeof(uint64_t) - remainder
);
430 for (i
= 0; i
< rate_lanes
; i
++)
432 state_lanes
[i
] ^= buffer_lanes
[i
];
435 /* Add the second bit of padding */
436 this->state
[this->rate
- 1] ^= 0x80;
438 /* Switch to the squeezing phase */
439 keccak_f1600_state_permute(this->state
);
440 this->rate_index
= 0;
443 METHOD(sha3_keccak_t
, squeeze
, void,
444 private_sha3_keccak_t
*this, size_t out_len
, uint8_t *out
)
446 size_t index
= 0, len
;
448 while (index
< out_len
)
450 if (this->rate_index
== this->rate
)
452 keccak_f1600_state_permute(this->state
);
453 this->rate_index
= 0;
455 len
= min(out_len
- index
, this->rate
- this->rate_index
);
456 memcpy(out
, &this->state
[this->rate_index
], len
);
459 this->rate_index
+= len
;
463 METHOD(sha3_keccak_t
, destroy
, void,
464 private_sha3_keccak_t
*this)
470 * Described in header.
472 sha3_keccak_t
*sha3_keccak_create(u_int capacity
, uint8_t delimited_suffix
)
474 private_sha3_keccak_t
*this;
477 rate
= KECCAK_STATE_SIZE
- capacity
;
479 if (rate
<= 0 || rate
> KECCAK_MAX_RATE
)
486 .get_rate
= _get_rate
,
489 .finalize
= _finalize
,
494 .delimited_suffix
= delimited_suffix
,
497 return &this->public;