webmcp

annotate libraries/moonhash/moonhash_sha3.c @ 569:5b19007574de

New argument active_link_attr for env.ui.paginate{...}
author jbe
date Wed Oct 13 17:21:44 2021 +0200 (2021-10-13)
parents b53dee61a930
children
rev   line source
jbe@499 1 /* This file is derived from "Keccak-more-compact.c", which has been downloaded from <https://github.com/gvanas/KeccakCodePackage/blob/10856bc1922a1ee2c4d2822a88b9ef8fb5059932/Standalone/CompactFIPS202/Keccak-more-compact.c>. The original file "Keccak-more-compact.c" has, according to <https://github.com/gvanas/KeccakCodePackage/blob/10856bc1922a1ee2c4d2822a88b9ef8fb5059932/README.markdown>, been put into the public domain. */
jbe@499 2
jbe@499 3 #include <stdint.h>
jbe@502 4 #include "byteorder.h"
jbe@501 5
jbe@499 6 #define FOR(i,n) for(i=0; i<n; ++i)
jbe@499 7 typedef uint8_t u8;
jbe@499 8 typedef uint64_t u64;
jbe@499 9 typedef unsigned int ui;
jbe@499 10
jbe@504 11 static void Keccak(ui r, ui c, const u8 *in, u64 inLen, u8 sfx, u8 *out, u64 outLen);
jbe@504 12 void FIPS202_SHAKE128(const u8 *in, u64 inLen, u8 *out, u64 outLen) { Keccak(1344, 256, in, inLen, 0x1F, out, outLen); }
jbe@504 13 void FIPS202_SHAKE256(const u8 *in, u64 inLen, u8 *out, u64 outLen) { Keccak(1088, 512, in, inLen, 0x1F, out, outLen); }
jbe@504 14 void FIPS202_SHA3_224(const u8 *in, u64 inLen, u8 *out) { Keccak(1152, 448, in, inLen, 0x06, out, 28); }
jbe@504 15 void FIPS202_SHA3_256(const u8 *in, u64 inLen, u8 *out) { Keccak(1088, 512, in, inLen, 0x06, out, 32); }
jbe@504 16 void FIPS202_SHA3_384(const u8 *in, u64 inLen, u8 *out) { Keccak(832, 768, in, inLen, 0x06, out, 48); }
jbe@504 17 void FIPS202_SHA3_512(const u8 *in, u64 inLen, u8 *out) { Keccak(576, 1024, in, inLen, 0x06, out, 64); }
jbe@499 18
jbe@499 19 static int LFSR86540(u8 *R) { (*R)=((*R)<<1)^(((*R)&0x80)?0x71:0); return ((*R)&2)>>1; }
jbe@499 20 #define ROL(a,o) ((((u64)a)<<o)^(((u64)a)>>(64-o)))
jbe@502 21 #ifdef LITTLE_ENDIAN_DETECTED
jbe@501 22 #define load64(src) (*(uint64_t *)(src))
jbe@501 23 #define store64(dst, src) do { *(uint64_t *)(dst) = src; } while (0)
jbe@501 24 #define xor64(dst, src) do { *(uint64_t *)(dst) ^= src; } while (0)
jbe@501 25 #else
jbe@500 26 static u64 load64(const u8 *x) { ui i; u64 u=0; FOR(i,8) { u<<=8; u|=x[7-i]; } return u; }
jbe@500 27 static void store64(u8 *x, u64 u) { ui i; FOR(i,8) { x[i]=u; u>>=8; } }
jbe@499 28 static void xor64(u8 *x, u64 u) { ui i; FOR(i,8) { x[i]^=u; u>>=8; } }
jbe@501 29 #endif
jbe@499 30 #define rL(x,y) load64((u8*)s+8*(x+5*y))
jbe@499 31 #define wL(x,y,l) store64((u8*)s+8*(x+5*y),l)
jbe@499 32 #define XL(x,y,l) xor64((u8*)s+8*(x+5*y),l)
jbe@499 33 static void KeccakF1600(void *s)
jbe@499 34 {
jbe@499 35 ui r,x,y,i,j,Y; u8 R=0x01; u64 C[5],D;
jbe@499 36 for(i=0; i<24; i++) {
jbe@499 37 /*θ*/ 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 38 /*ρπ*/ 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 39 /*χ*/ 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 40 /*ι*/ FOR(j,7) if (LFSR86540(&R)) XL(0,0,(u64)1<<((1<<j)-1));
jbe@499 41 }
jbe@499 42 }
jbe@505 43 static void KeccakF1600Init(u8 *s, ui R, const u8 *in, u64 inLen, u8 sfx) {
jbe@505 44 /*initialize*/ ui i,b=0; FOR(i,200) s[i]=0;
jbe@505 45 /*absorb*/ while(inLen>0) { b=(inLen<R)?inLen:R; FOR(i,b) s[i]^=in[i]; in+=b; inLen-=b; if (b==R) { KeccakF1600(s); b=0; } }
jbe@505 46 /*pad*/ s[b]^=sfx; if((sfx&0x80)&&(b==(R-1))) KeccakF1600(s); s[R-1]^=0x80; KeccakF1600(s);
jbe@505 47 }
jbe@504 48 static void Keccak(ui r, ui c, const u8 *in, u64 inLen, u8 sfx, u8 *out, u64 outLen)
jbe@499 49 {
jbe@505 50 /*initialize*/ u8 s[200]; ui R=r/8; ui i,b; KeccakF1600Init(s, R, in, inLen, sfx);
jbe@499 51 /*squeeze*/ while(outLen>0) { b=(outLen<R)?outLen:R; FOR(i,b) out[i]=s[i]; out+=b; outLen-=b; if(outLen>0) KeccakF1600(s); }
jbe@499 52 }

Impressum / About Us