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/
19 * This is NOT the hash function which will eventually be part of an osl
20 * release. It will be replaced by whatever git comes up as the successor of
21 * sha1. This hash function is FOR TESTING ONLY.
24 #define FOR(i,n) for(i=0; i<n; ++i)
25 typedef unsigned char u8;
26 typedef unsigned long long int u64;
27 typedef unsigned int ui;
29 static int LFSR86540(u8 *R)
31 *R = ((*R) << 1) ^ (((*R) & 0x80)? 0x71 : 0);
32 return ((*R) & 2 ) >> 1;
35 static u64 load64(const u8 *x)
47 static void store64(u8 *x, u64 u)
57 static void xor64(u8 *x, u64 u)
66 #define rL(x, y) load64((u8*)s + 8 * (x + 5 * y))
67 #define wL(x, y, l) store64((u8*)s + 8 * (x + 5 * y), l)
68 #define XL(x, y, l) xor64((u8*)s + 8 * (x + 5 * y), l)
69 #define ROL(a, o) ((((u64)a) << o) ^ (((u64)a) >> (64 - o)))
71 static void KeccakF1600(void *s)
77 for (i = 0; i < 24; i++) {
79 C[x] = rL(x, 0) ^ rL(x, 1) ^ rL(x, 2) ^ rL(x, 3) ^ rL(x, 4);
81 D = C[(x + 4) % 5] ^ ROL(C[(x + 1) % 5], 1);
90 Y = (2 * x + 3 * y) % 5;
94 wL(x, y, ROL(D, r % 64));
101 wL(x, y, C[x] ^ ((~C[(x + 1) % 5]) & C[(x + 2) % 5]));
105 XL(0, 0, (u64)1 << ((1 << j) - 1));
109 static void Keccak(ui r, const u8 *in, u64 inLen, u8 sfx, u8 *out, u64 outLen)
119 b = (inLen < R)? inLen : R;
131 if ((sfx & 0x80) && (b == (R - 1)))
137 b = (outLen < R)? outLen : R;
148 * Compute the hash value for osl version 2 tables.
150 * \param data Pointer to the data to compute the hash value from.
151 * \param len The length of \a data in bytes.
152 * \param result must point to an area at least 20 bytes large.
154 void sha3_hash(const char *data, unsigned long len, unsigned char *result)
156 Keccak(1152, (const u8*)data, len, 0x06, result, 20);