webmcp

changeset 499:b36e366bba2b

Added SHA-3 hashing functions (using compact Keccak code)
author jbe
date Sun Aug 13 03:22:48 2017 +0200 (2017-08-13)
parents e360b1933c78
children 5e6dbaa3e219
files LICENSE Makefile libraries/moonhash/Keccak-more-compact.c libraries/moonhash/Makefile libraries/moonhash/moonhash.autodoc.lua libraries/moonhash/moonhash.c libraries/moonhash/moonhash_sha3.c
line diff
     1.1 --- a/LICENSE	Sun Jul 23 03:43:49 2017 +0200
     1.2 +++ b/LICENSE	Sun Aug 13 03:22:48 2017 +0200
     1.3 @@ -1,4 +1,4 @@
     1.4 -Copyright (c) 2009-2016 Public Software Group e. V., Berlin, Germany
     1.5 +Copyright (c) 2009-2017 Public Software Group e. V., Berlin, Germany
     1.6  
     1.7  Permission is hereby granted, free of charge, to any person obtaining a
     1.8  copy of this software and associated documentation files (the "Software"),
     1.9 @@ -17,3 +17,8 @@
    1.10  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    1.11  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    1.12  DEALINGS IN THE SOFTWARE.
    1.13 +
    1.14 +
    1.15 +Additional notices:
    1.16 +
    1.17 +File "libraries/moonhash/moonbridge_sha3.c" is derived from file "libraries/moonhash/Keccak-more-compact.c", which has been downloaded from <https://github.com/gvanas/KeccakCodePackage/blob/10856bc1922a1ee2c4d2822a88b9ef8fb5059932/Standalone/CompactFIPS202/Keccak-more-compact.c>. The code in file Keccak-more-compact.c is code that has, according to <https://github.com/gvanas/KeccakCodePackage/blob/10856bc1922a1ee2c4d2822a88b9ef8fb5059932/README.markdown>, been put into the public domain.
     2.1 --- a/Makefile	Sun Jul 23 03:43:49 2017 +0200
     2.2 +++ b/Makefile	Sun Aug 13 03:22:48 2017 +0200
     2.3 @@ -20,6 +20,7 @@
     2.4  	cd libraries/extos; make
     2.5  	cd libraries/json; make
     2.6  	cd libraries/mondelefant; make
     2.7 +	cd libraries/moonhash; make
     2.8  	cd libraries/multirand; make
     2.9  
    2.10  symlinks::
    2.11 @@ -29,6 +30,7 @@
    2.12  	ln -s -f ../../libraries/mondelefant/mondelefant.lua framework/lib/
    2.13  	ln -s -f ../../libraries/mondelefant/mondelefant_native.so framework/lib/
    2.14  	ln -s -f ../../libraries/mondelefant/mondelefant_atom_connector.lua framework/lib/
    2.15 +	ln -s -f ../../libraries/moonhash/moonhash.so framework/lib/
    2.16  	ln -s -f ../../libraries/multirand/multirand.so framework/lib/
    2.17  	ln -s -f ../../libraries/nihil/nihil.lua framework/lib/
    2.18  	ln -s -f ../../libraries/luatex/luatex.lua framework/lib/
    2.19 @@ -50,6 +52,7 @@
    2.20  	rm -f framework/lib/*
    2.21  	cd libraries/extos; make clean
    2.22  	cd libraries/mondelefant; make clean
    2.23 +	cd libraries/moonhash; make clean
    2.24  	cd libraries/multirand; make clean
    2.25  	cd libraries/json; make clean
    2.26  	cd framework/accelerator; make clean
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/libraries/moonhash/Keccak-more-compact.c	Sun Aug 13 03:22:48 2017 +0200
     3.3 @@ -0,0 +1,38 @@
     3.4 +#define FOR(i,n) for(i=0; i<n; ++i)
     3.5 +typedef unsigned char u8;
     3.6 +typedef unsigned long long int u64;
     3.7 +typedef unsigned int ui;
     3.8 +
     3.9 +void Keccak(ui r, ui c, const u8 *in, u64 inLen, u8 sfx, u8 *out, u64 outLen);
    3.10 +void FIPS202_SHAKE128(const u8 *in, u64 inLen, u8 *out, u64 outLen) { Keccak(1344, 256, in, inLen, 0x1F, out, outLen); }
    3.11 +void FIPS202_SHAKE256(const u8 *in, u64 inLen, u8 *out, u64 outLen) { Keccak(1088, 512, in, inLen, 0x1F, out, outLen); }
    3.12 +void FIPS202_SHA3_224(const u8 *in, u64 inLen, u8 *out) { Keccak(1152, 448, in, inLen, 0x06, out, 28); }
    3.13 +void FIPS202_SHA3_256(const u8 *in, u64 inLen, u8 *out) { Keccak(1088, 512, in, inLen, 0x06, out, 32); }
    3.14 +void FIPS202_SHA3_384(const u8 *in, u64 inLen, u8 *out) { Keccak(832, 768, in, inLen, 0x06, out, 48); }
    3.15 +void FIPS202_SHA3_512(const u8 *in, u64 inLen, u8 *out) { Keccak(576, 1024, in, inLen, 0x06, out, 64); }
    3.16 +
    3.17 +int LFSR86540(u8 *R) { (*R)=((*R)<<1)^(((*R)&0x80)?0x71:0); return ((*R)&2)>>1; }
    3.18 +#define ROL(a,o) ((((u64)a)<<o)^(((u64)a)>>(64-o)))
    3.19 +static u64 load64(const u8 *x) { ui i; u64 u=0; FOR(i,8) { u<<=8; u|=x[7-i]; } return u; }
    3.20 +static void store64(u8 *x, u64 u) { ui i; FOR(i,8) { x[i]=u; u>>=8; } }
    3.21 +static void xor64(u8 *x, u64 u) { ui i; FOR(i,8) { x[i]^=u; u>>=8; } }
    3.22 +#define rL(x,y) load64((u8*)s+8*(x+5*y))
    3.23 +#define wL(x,y,l) store64((u8*)s+8*(x+5*y),l)
    3.24 +#define XL(x,y,l) xor64((u8*)s+8*(x+5*y),l)
    3.25 +void KeccakF1600(void *s)
    3.26 +{
    3.27 +    ui r,x,y,i,j,Y; u8 R=0x01; u64 C[5],D;
    3.28 +    for(i=0; i<24; i++) {
    3.29 +        /*θ*/ 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); }
    3.30 +        /*ρπ*/ 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]; }
    3.31 +        /*χ*/ 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])); }
    3.32 +        /*ι*/ FOR(j,7) if (LFSR86540(&R)) XL(0,0,(u64)1<<((1<<j)-1));
    3.33 +    }
    3.34 +}
    3.35 +void Keccak(ui r, ui c, const u8 *in, u64 inLen, u8 sfx, u8 *out, u64 outLen)
    3.36 +{
    3.37 +    /*initialize*/ u8 s[200]; ui R=r/8; ui i,b=0; FOR(i,200) s[i]=0;
    3.38 +    /*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; } }
    3.39 +    /*pad*/ s[b]^=sfx; if((sfx&0x80)&&(b==(R-1))) KeccakF1600(s); s[R-1]^=0x80; KeccakF1600(s);
    3.40 +    /*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); }
    3.41 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/libraries/moonhash/Makefile	Sun Aug 13 03:22:48 2017 +0200
     4.3 @@ -0,0 +1,10 @@
     4.4 +include ../../Makefile.options
     4.5 +
     4.6 +moonhash.so: moonhash.o
     4.7 +	$(LD) $(LDFLAGS) -o moonhash.so moonhash.o
     4.8 +
     4.9 +moonhash.o: moonhash.c moonhash_sha3.c
    4.10 +	$(CC) -c $(CFLAGS) -o moonhash.o moonhash.c
    4.11 +
    4.12 +clean::
    4.13 +	rm -f moonhash.o moonhash.so
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/libraries/moonhash/moonhash.autodoc.lua	Sun Aug 13 03:22:48 2017 +0200
     5.3 @@ -0,0 +1,65 @@
     5.4 +--[[--
     5.5 +hash =              -- SHA3-224 digest (in hex notation) of input string
     5.6 +moonhash.sha3_224(
     5.7 +  data              -- input string
     5.8 +)
     5.9 +
    5.10 +Calculates the SHA3-224 checksum with a security of 112 bits for collision attacks and 224 bits for preimage and second preimage attacks.
    5.11 +
    5.12 +--]]--
    5.13 +-- Implemented in moonhash.c and moonhash_sha3.c
    5.14 +--//--
    5.15 +
    5.16 +
    5.17 +--[[--
    5.18 +hash =              -- SHA3-256 digest (in hex notation) of input string
    5.19 +moonhash.sha3_256(
    5.20 +  data              -- input string
    5.21 +)
    5.22 +
    5.23 +Calculates the SHA3-256 checksum with a security of 128 bits for collision attacks and 256 bits for preimage and second preimage attacks.
    5.24 +
    5.25 +--]]--
    5.26 +-- Implemented in moonhash.c and moonhash_sha3.c
    5.27 +--//--
    5.28 +
    5.29 +
    5.30 +--[[--
    5.31 +hash =              -- SHA3-384 digest (in hex notation) of input string
    5.32 +moonhash.sha3_384(
    5.33 +  data              -- input string
    5.34 +)
    5.35 +
    5.36 +Calculates the SHA3-384 checksum with a security of 192 bits for collision attacks and 384 bits for preimage and second preimage attacks.
    5.37 +
    5.38 +--]]--
    5.39 +-- Implemented in moonhash.c and moonhash_sha3.c
    5.40 +--//--
    5.41 +
    5.42 +
    5.43 +--[[--
    5.44 +hash =              -- SHA3-512 digest (in hex notation) of input string
    5.45 +moonhash.sha3_512(
    5.46 +  data              -- input string
    5.47 +)
    5.48 +
    5.49 +Calculates the SHA3-512 checksum with a security of 512 bits for collision attacks and 256 bits for preimage and second preimage attacks.
    5.50 +
    5.51 +--]]--
    5.52 +-- Implemented in moonhash.c and moonhash_sha3.c
    5.53 +--//--
    5.54 +
    5.55 +
    5.56 +--[[--
    5.57 +hash =              -- 128 bits of SHAKE128 digest (in hex notation) of input string
    5.58 +moonhash.shake128_128(
    5.59 +  data              -- input string
    5.60 +)
    5.61 +
    5.62 +Calculates the first 128 bits of the SHAKE128 digest (FIPS 202) with a security of 64 bits for collision attacks and 128 bits for preimage and second preimage attacks.
    5.63 +
    5.64 +--]]--
    5.65 +-- Implemented in moonhash.c and moonhash_sha3.c
    5.66 +--//--
    5.67 +
    5.68 +
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/libraries/moonhash/moonhash.c	Sun Aug 13 03:22:48 2017 +0200
     6.3 @@ -0,0 +1,72 @@
     6.4 +#include <lua.h>
     6.5 +#include <lauxlib.h>
     6.6 +#include "moonhash_sha3.c"
     6.7 +
     6.8 +void moonhash_push_hex(lua_State *L, unsigned char *buf, int len) {
     6.9 +  int i;
    6.10 +  unsigned char n;
    6.11 +  char str[2*len+1];
    6.12 +  for (i=0; i<len; i++) {
    6.13 +    n = buf[i] >> 4;
    6.14 +    str[2*i+0] = n + ((n < 10) ? '0' : ('a' - 10));
    6.15 +    n = buf[i] & 0x0f;
    6.16 +    str[2*i+1] = n + ((n < 10) ? '0' : ('a' - 10));
    6.17 +  }
    6.18 +  str[2*len] = 0;
    6.19 +  lua_pushstring(L, str);
    6.20 +}
    6.21 +
    6.22 +typedef void (*moonhash_sha3_fptr)(const uint8_t *, uint64_t, uint8_t *);
    6.23 +typedef void (*moonhash_shake_fptr)(const uint8_t *, uint64_t, uint8_t *, uint64_t);
    6.24 +
    6.25 +int moonhash_sha3(lua_State *L, moonhash_sha3_fptr hashfunc, int len) {
    6.26 +  const char *input;
    6.27 +  size_t inputlen;
    6.28 +  unsigned char output[len];
    6.29 +  input = luaL_checklstring(L, 1, &inputlen);
    6.30 +  hashfunc((const uint8_t *)input, inputlen, output);
    6.31 +  moonhash_push_hex(L, output, len);
    6.32 +  return 1;
    6.33 +}
    6.34 +
    6.35 +int moonhash_sha3_224(lua_State *L) {
    6.36 +  return moonhash_sha3(L, FIPS202_SHA3_224, 224/8);
    6.37 +}
    6.38 +int moonhash_sha3_256(lua_State *L) {
    6.39 +  return moonhash_sha3(L, FIPS202_SHA3_256, 256/8);
    6.40 +}
    6.41 +int moonhash_sha3_384(lua_State *L) {
    6.42 +  return moonhash_sha3(L, FIPS202_SHA3_384, 384/8);
    6.43 +}
    6.44 +int moonhash_sha3_512(lua_State *L) {
    6.45 +  return moonhash_sha3(L, FIPS202_SHA3_512, 512/8);
    6.46 +}
    6.47 +
    6.48 +int moonhash_shake(lua_State *L, moonhash_shake_fptr shakefunc, int len) {
    6.49 +  const char *input;
    6.50 +  size_t inputlen;
    6.51 +  unsigned char output[len];
    6.52 +  input = luaL_checklstring(L, 1, &inputlen);
    6.53 +  shakefunc((const uint8_t *)input, inputlen, output, len);
    6.54 +  moonhash_push_hex(L, output, len);
    6.55 +  return 1;
    6.56 +}
    6.57 +
    6.58 +int moonhash_shake128_128(lua_State *L) {
    6.59 +  return moonhash_shake(L, FIPS202_SHAKE128, 128/8);
    6.60 +}
    6.61 +
    6.62 +static const struct luaL_Reg moonhash_module_functions[] = {
    6.63 + {"sha3_224", moonhash_sha3_224},
    6.64 + {"sha3_256", moonhash_sha3_256},
    6.65 + {"sha3_384", moonhash_sha3_384},
    6.66 + {"sha3_512", moonhash_sha3_512},
    6.67 + {"shake128_128", moonhash_shake128_128},
    6.68 + {NULL, NULL}
    6.69 +};
    6.70 +
    6.71 +int luaopen_moonhash(lua_State *L) {
    6.72 +  lua_newtable(L);
    6.73 +  luaL_setfuncs(L, moonhash_module_functions, 0);
    6.74 +  return 1;
    6.75 +}
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/libraries/moonhash/moonhash_sha3.c	Sun Aug 13 03:22:48 2017 +0200
     7.3 @@ -0,0 +1,47 @@
     7.4 +/* 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. */
     7.5 +
     7.6 +#include <stdint.h>
     7.7 +#include <sys/endian.h>
     7.8 +
     7.9 +#define FOR(i,n) for(i=0; i<n; ++i)
    7.10 +typedef uint8_t u8;
    7.11 +typedef uint64_t u64;
    7.12 +typedef unsigned int ui;
    7.13 +
    7.14 +static void Keccak(ui r, ui c, const u8 *in, u64 inLen, u8 sfx, u8 *out, u64 outLen);
    7.15 +void FIPS202_SHAKE128(const u8 *in, u64 inLen, u8 *out, u64 outLen) { Keccak(1344, 256, in, inLen, 0x1F, out, outLen); }
    7.16 +void FIPS202_SHAKE256(const u8 *in, u64 inLen, u8 *out, u64 outLen) { Keccak(1088, 512, in, inLen, 0x1F, out, outLen); }
    7.17 +void FIPS202_SHA3_224(const u8 *in, u64 inLen, u8 *out) { Keccak(1152, 448, in, inLen, 0x06, out, 28); }
    7.18 +void FIPS202_SHA3_256(const u8 *in, u64 inLen, u8 *out) { Keccak(1088, 512, in, inLen, 0x06, out, 32); }
    7.19 +void FIPS202_SHA3_384(const u8 *in, u64 inLen, u8 *out) { Keccak(832, 768, in, inLen, 0x06, out, 48); }
    7.20 +void FIPS202_SHA3_512(const u8 *in, u64 inLen, u8 *out) { Keccak(576, 1024, in, inLen, 0x06, out, 64); }
    7.21 +
    7.22 +static int LFSR86540(u8 *R) { (*R)=((*R)<<1)^(((*R)&0x80)?0x71:0); return ((*R)&2)>>1; }
    7.23 +#define ROL(a,o) ((((u64)a)<<o)^(((u64)a)>>(64-o)))
    7.24 +#define load64 le64dec
    7.25 +#define store64 le64enc
    7.26 +#if _BYTE_ORDER == _LITTLE_ENDIAN
    7.27 +static void xor64(u8 *x, u64 u) { *(u64 *)x ^= u; }
    7.28 +#else
    7.29 +static void xor64(u8 *x, u64 u) { ui i; FOR(i,8) { x[i]^=u; u>>=8; } }
    7.30 +#endif
    7.31 +#define rL(x,y) load64((u8*)s+8*(x+5*y))
    7.32 +#define wL(x,y,l) store64((u8*)s+8*(x+5*y),l)
    7.33 +#define XL(x,y,l) xor64((u8*)s+8*(x+5*y),l)
    7.34 +static void KeccakF1600(void *s)
    7.35 +{
    7.36 +    ui r,x,y,i,j,Y; u8 R=0x01; u64 C[5],D;
    7.37 +    for(i=0; i<24; i++) {
    7.38 +        /*θ*/ 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); }
    7.39 +        /*ρπ*/ 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]; }
    7.40 +        /*χ*/ 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])); }
    7.41 +        /*ι*/ FOR(j,7) if (LFSR86540(&R)) XL(0,0,(u64)1<<((1<<j)-1));
    7.42 +    }
    7.43 +}
    7.44 +static void Keccak(ui r, ui c, const u8 *in, u64 inLen, u8 sfx, u8 *out, u64 outLen)
    7.45 +{
    7.46 +    /*initialize*/ u8 s[200]; ui R=r/8; ui i,b=0; FOR(i,200) s[i]=0;
    7.47 +    /*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; } }
    7.48 +    /*pad*/ s[b]^=sfx; if((sfx&0x80)&&(b==(R-1))) KeccakF1600(s); s[R-1]^=0x80; KeccakF1600(s);
    7.49 +    /*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); }
    7.50 +}

Impressum / About Us