jbe@499: /* This file is derived from "Keccak-more-compact.c", which has been downloaded from . The original file "Keccak-more-compact.c" has, according to , been put into the public domain. */ jbe@499: jbe@499: #include jbe@499: jbe@501: #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) jbe@501: #include jbe@501: #if defined(_BYTE_ORDER) && defined(_LITTLE_ENDIAN) && _BYTE_ORDER == _LITTLE_ENDIAN jbe@501: #define MOONHASH_LITTLE_ENDIAN jbe@501: #endif jbe@501: #elif defined(__linux__) jbe@501: #include jbe@501: #if defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && __BYTE_ORDER == __LITTLE_ENDIAN jbe@501: #define MOONHASH_LITTLE_ENDIAN jbe@501: #endif jbe@501: #else jbe@501: #warning Could not determine endianess, revert to fail safe. jbe@501: #endif jbe@501: jbe@499: #define FOR(i,n) for(i=0; i>1; } jbe@499: #define ROL(a,o) ((((u64)a)<>(64-o))) jbe@501: #ifdef MOONHASH_LITTLE_ENDIAN jbe@501: #define load64(src) (*(uint64_t *)(src)) jbe@501: #define store64(dst, src) do { *(uint64_t *)(dst) = src; } while (0) jbe@501: #define xor64(dst, src) do { *(uint64_t *)(dst) ^= src; } while (0) jbe@501: #else jbe@500: static u64 load64(const u8 *x) { ui i; u64 u=0; FOR(i,8) { u<<=8; u|=x[7-i]; } return u; } jbe@500: static void store64(u8 *x, u64 u) { ui i; FOR(i,8) { x[i]=u; u>>=8; } } jbe@499: static void xor64(u8 *x, u64 u) { ui i; FOR(i,8) { x[i]^=u; u>>=8; } } jbe@501: #endif jbe@499: #define rL(x,y) load64((u8*)s+8*(x+5*y)) jbe@499: #define wL(x,y,l) store64((u8*)s+8*(x+5*y),l) jbe@499: #define XL(x,y,l) xor64((u8*)s+8*(x+5*y),l) jbe@499: static void KeccakF1600(void *s) jbe@499: { jbe@499: ui r,x,y,i,j,Y; u8 R=0x01; u64 C[5],D; jbe@499: for(i=0; i<24; i++) { jbe@499: /*θ*/ FOR(x,5) C[x]=rL(x,0)^rL(x,1)^rL(x,2)^rL(x,3)^rL(x,4); FOR(x,5) { D=C[(x+4)%5]^ROL(C[(x+1)%5],1); FOR(y,5) XL(x,y,D); } jbe@499: /*ρπ*/ x=1; y=r=0; D=rL(x,y); FOR(j,24) { r+=j+1; Y=(2*x+3*y)%5; x=y; y=Y; C[0]=rL(x,y); wL(x,y,ROL(D,r%64)); D=C[0]; } jbe@499: /*χ*/ FOR(y,5) { FOR(x,5) C[x]=rL(x,y); FOR(x,5) wL(x,y,C[x]^((~C[(x+1)%5])&C[(x+2)%5])); } jbe@499: /*ι*/ FOR(j,7) if (LFSR86540(&R)) XL(0,0,(u64)1<<((1<0) { b=(inLen0) { b=(outLen0) KeccakF1600(s); } jbe@499: }