9 #include "../firmware.h"
16 #ifndef KECCAKF_ROUNDS
17 #define KECCAKF_ROUNDS 24
21 #define ROTL64(x, y) (((x) << (y)) | ((x) >> (64 - (y))))
26 static void sha3_keccakf(uint64_t st[25])
29 const uint64_t keccakf_rndc[24] = {
30 0x0000000000000001, 0x0000000000008082, 0x800000000000808a,
31 0x8000000080008000, 0x000000000000808b, 0x0000000080000001,
32 0x8000000080008081, 0x8000000000008009, 0x000000000000008a,
33 0x0000000000000088, 0x0000000080008009, 0x000000008000000a,
34 0x000000008000808b, 0x800000000000008b, 0x8000000000008089,
35 0x8000000000008003, 0x8000000000008002, 0x8000000000000080,
36 0x000000000000800a, 0x800000008000000a, 0x8000000080008081,
37 0x8000000000008080, 0x0000000080000001, 0x8000000080008008
39 const int keccakf_rotc[24] = {
40 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 2, 14,
41 27, 41, 56, 8, 25, 43, 62, 18, 39, 61, 20, 44
43 const int keccakf_piln[24] = {
44 10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4,
45 15, 23, 19, 13, 12, 2, 20, 14, 22, 9, 6, 1
52 #if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
56 for (i = 0; i < 25; i++) {
57 v = (uint8_t *) &st[i];
58 st[i] = ((uint64_t) v[0]) | (((uint64_t) v[1]) << 8) |
59 (((uint64_t) v[2]) << 16) | (((uint64_t) v[3]) << 24) |
60 (((uint64_t) v[4]) << 32) | (((uint64_t) v[5]) << 40) |
61 (((uint64_t) v[6]) << 48) | (((uint64_t) v[7]) << 56);
66 for (r = 0; r < KECCAKF_ROUNDS; r++) {
69 for (i = 0; i < 5; i++)
70 bc[i] = st[i] ^ st[i + 5] ^ st[i + 10] ^ st[i + 15] ^ st[i + 20];
72 for (i = 0; i < 5; i++) {
73 t = bc[(i + 4) % 5] ^ ROTL64(bc[(i + 1) % 5], 1);
74 for (j = 0; j < 25; j += 5)
80 for (i = 0; i < 24; i++) {
83 st[j] = ROTL64(t, keccakf_rotc[i]);
88 for (j = 0; j < 25; j += 5) {
89 for (i = 0; i < 5; i++)
91 for (i = 0; i < 5; i++)
92 st[j + i] ^= (~bc[(i + 1) % 5]) & bc[(i + 2) % 5];
96 st[0] ^= keccakf_rndc[r];
99 #if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
101 for (i = 0; i < 25; i++) {
102 v = (uint8_t *) &st[i];
105 v[1] = (t >> 8) & 0xFF;
106 v[2] = (t >> 16) & 0xFF;
107 v[3] = (t >> 24) & 0xFF;
108 v[4] = (t >> 32) & 0xFF;
109 v[5] = (t >> 40) & 0xFF;
110 v[6] = (t >> 48) & 0xFF;
111 v[7] = (t >> 56) & 0xFF;
120 for (i = 0; i < 25; i++)
124 c->mdlen = output_length;
125 c->rsiz = 200 - 2 * output_length;
135 for (i = 0; i < data_length; i++)
137 c->st.b[j++] ^= data[i];
140 sha3_keccakf(c->st.q);
153 c->st.b[c->pt] ^= 0x1F;
154 c->st.b[c->rsiz - 1] ^= 0x80;
155 sha3_keccakf(c->st.q);
160 for (i = 0; i < c->mdlen; i++)
164 sha3_keccakf(c->st.q);
167 output[i] = c->st.b[j++];
void Util_shake_init(util_shake_ctx_t *c, uint8_t output_length)
void Util_shake_out(util_shake_ctx_t *c, uint8_t *output)
void Util_shake_update(util_shake_ctx_t *c, const uint8_t *data, uint32_t data_length)