2 Implementation by the Keccak, Keyak and Ketje Teams, namely, Guido Bertoni,
3 Joan Daemen, Michaƫl Peeters, Gilles Van Assche and Ronny Van Keer, hereby
4 denoted as "the implementer".
6 For more information, feedback or questions, please refer to our websites:
7 http://keccak.noekeon.org/
8 http://keyak.noekeon.org/
9 http://ketje.noekeon.org/
11 To the extent possible under law, the implementer has waived all copyright
12 and related or neighboring rights to the source code in this file.
13 http://creativecommons.org/publicdomain/zero/1.0/
16 #define FOR(i,n) for(i=0; i<n; ++i)
17 typedef unsigned char u8;
18 typedef unsigned long long int u64;
19 typedef unsigned int ui;
21 static int LFSR86540(u8 *R)
23 *R = ((*R) << 1) ^ (((*R) & 0x80)? 0x71 : 0);
24 return ((*R) & 2 ) >> 1;
27 static u64 load64(const u8 *x)
39 static void store64(u8 *x, u64 u)
49 static void xor64(u8 *x, u64 u)
58 #define rL(x, y) load64((u8*)s + 8 * (x + 5 * y))
59 #define wL(x, y, l) store64((u8*)s + 8 * (x + 5 * y), l)
60 #define XL(x, y, l) xor64((u8*)s + 8 * (x + 5 * y), l)
61 #define ROL(a, o) ((((u64)a) << o) ^ (((u64)a) >> (64 - o)))
63 static void KeccakF1600(void *s)
69 for (i = 0; i < 24; i++) {
71 C[x] = rL(x, 0) ^ rL(x, 1) ^ rL(x, 2) ^ rL(x, 3) ^ rL(x, 4);
73 D = C[(x + 4) % 5] ^ ROL(C[(x + 1) % 5], 1);
82 Y = (2 * x + 3 * y) % 5;
86 wL(x, y, ROL(D, r % 64));
93 wL(x, y, C[x] ^ ((~C[(x + 1) % 5]) & C[(x + 2) % 5]));
97 XL(0, 0, (u64)1 << ((1 << j) - 1));
101 static void Keccak(ui r, const u8 *in, u64 inLen, u8 sfx, u8 *out, u64 outLen)
111 b = (inLen < R)? inLen : R;
123 if ((sfx & 0x80) && (b == (R - 1)))
129 b = (outLen < R)? outLen : R;
140 * Compute the hash value for osl version 2 tables.
142 * \param data Pointer to the data to compute the hash value from.
143 * \param len The length of \a data in bytes.
144 * \param result must point to an area at least 20 bytes large.
146 void sha3_hash(const char *data, unsigned long len, unsigned char *result)
148 Keccak(1152, (const u8*)data, len, 0x06, result, 20);