| 1 |
|
| 2 |
|
| 3 |
#include <inttypes.h> |
| 4 |
#include <string.h> |
| 5 |
#include "crypto_xof.h" |
| 6 |
|
| 7 |
static inline uint64_t ROL(uint64_t x,int b) |
| 8 |
{ |
| 9 |
return (x<<b)|(x>>(64-b)); |
| 10 |
} |
| 11 |
|
| 12 |
static inline uint64_t load64(const unsigned char *x) |
| 13 |
{ |
| 14 |
uint64_t result = 0; |
| 15 |
int i; |
| 16 |
|
| 17 |
for (i = 0;i < 8;++i) |
| 18 |
result |= ((uint64_t) x[i])<<(8*i); |
| 19 |
return result; |
| 20 |
} |
| 21 |
|
| 22 |
#include "keccak.inc" |
| 23 |
|
| 24 |
#define ratebytes 136 |
| 25 |
#define padding 31 |
| 26 |
|
| 27 |
void crypto_xof( |
| 28 |
unsigned char *h,long long hlen, |
| 29 |
const unsigned char *m,long long mlen |
| 30 |
) |
| 31 |
{ |
| 32 |
uint64_t state[25]; |
| 33 |
unsigned char t[ratebytes]; |
| 34 |
int i; |
| 35 |
|
| 36 |
memset(state,0,sizeof state); |
| 37 |
|
| 38 |
while (mlen >= ratebytes) { |
| 39 |
for (i = 0;i < ratebytes/8;++i) |
| 40 |
state[i] ^= load64(m+8*i); |
| 41 |
keccak(state); |
| 42 |
m += ratebytes; mlen -= ratebytes; |
| 43 |
} |
| 44 |
|
| 45 |
memset(t,0,sizeof t); |
| 46 |
memcpy(t,m,mlen); |
| 47 |
t[mlen] = padding; |
| 48 |
t[ratebytes-1] |= 128; |
| 49 |
for (i = 0;i < ratebytes/8;++i) |
| 50 |
state[i] ^= load64(t+8*i); |
| 51 |
|
| 52 |
while (hlen >= ratebytes) { |
| 53 |
keccak(state); |
| 54 |
for (i = 0;i < ratebytes;++i) |
| 55 |
h[i] = state[i/8]>>(8*(i%8)); |
| 56 |
h += ratebytes; |
| 57 |
hlen -= ratebytes; |
| 58 |
} |
| 59 |
|
| 60 |
if (hlen > 0) { |
| 61 |
keccak(state); |
| 62 |
for (i = 0;i < hlen;++i) |
| 63 |
h[i] = state[i/8]>>(8*(i%8)); |
| 64 |
} |
| 65 |
} |