# HG changeset patch # User jbe # Date 1502587368 -7200 # Node ID b36e366bba2bc2e0d50fa0f049d8ade043112c1c # Parent e360b1933c78babff0348f1939d564ae990cdc4a Added SHA-3 hashing functions (using compact Keccak code) diff -r e360b1933c78 -r b36e366bba2b LICENSE --- a/LICENSE Sun Jul 23 03:43:49 2017 +0200 +++ b/LICENSE Sun Aug 13 03:22:48 2017 +0200 @@ -1,4 +1,4 @@ -Copyright (c) 2009-2016 Public Software Group e. V., Berlin, Germany +Copyright (c) 2009-2017 Public Software Group e. V., Berlin, Germany Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), @@ -17,3 +17,8 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + +Additional notices: + +File "libraries/moonhash/moonbridge_sha3.c" is derived from file "libraries/moonhash/Keccak-more-compact.c", which has been downloaded from . The code in file Keccak-more-compact.c is code that has, according to , been put into the public domain. diff -r e360b1933c78 -r b36e366bba2b Makefile --- a/Makefile Sun Jul 23 03:43:49 2017 +0200 +++ b/Makefile Sun Aug 13 03:22:48 2017 +0200 @@ -20,6 +20,7 @@ cd libraries/extos; make cd libraries/json; make cd libraries/mondelefant; make + cd libraries/moonhash; make cd libraries/multirand; make symlinks:: @@ -29,6 +30,7 @@ ln -s -f ../../libraries/mondelefant/mondelefant.lua framework/lib/ ln -s -f ../../libraries/mondelefant/mondelefant_native.so framework/lib/ ln -s -f ../../libraries/mondelefant/mondelefant_atom_connector.lua framework/lib/ + ln -s -f ../../libraries/moonhash/moonhash.so framework/lib/ ln -s -f ../../libraries/multirand/multirand.so framework/lib/ ln -s -f ../../libraries/nihil/nihil.lua framework/lib/ ln -s -f ../../libraries/luatex/luatex.lua framework/lib/ @@ -50,6 +52,7 @@ rm -f framework/lib/* cd libraries/extos; make clean cd libraries/mondelefant; make clean + cd libraries/moonhash; make clean cd libraries/multirand; make clean cd libraries/json; make clean cd framework/accelerator; make clean diff -r e360b1933c78 -r b36e366bba2b libraries/moonhash/Keccak-more-compact.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libraries/moonhash/Keccak-more-compact.c Sun Aug 13 03:22:48 2017 +0200 @@ -0,0 +1,38 @@ +#define FOR(i,n) for(i=0; i>1; } +#define ROL(a,o) ((((u64)a)<>(64-o))) +static u64 load64(const u8 *x) { ui i; u64 u=0; FOR(i,8) { u<<=8; u|=x[7-i]; } return u; } +static void store64(u8 *x, u64 u) { ui i; FOR(i,8) { x[i]=u; u>>=8; } } +static void xor64(u8 *x, u64 u) { ui i; FOR(i,8) { x[i]^=u; u>>=8; } } +#define rL(x,y) load64((u8*)s+8*(x+5*y)) +#define wL(x,y,l) store64((u8*)s+8*(x+5*y),l) +#define XL(x,y,l) xor64((u8*)s+8*(x+5*y),l) +void KeccakF1600(void *s) +{ + ui r,x,y,i,j,Y; u8 R=0x01; u64 C[5],D; + for(i=0; i<24; i++) { + /*θ*/ 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); } + /*ρπ*/ 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]; } + /*χ*/ 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])); } + /*ι*/ FOR(j,7) if (LFSR86540(&R)) XL(0,0,(u64)1<<((1<0) { b=(inLen0) { b=(outLen0) KeccakF1600(s); } +} diff -r e360b1933c78 -r b36e366bba2b libraries/moonhash/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libraries/moonhash/Makefile Sun Aug 13 03:22:48 2017 +0200 @@ -0,0 +1,10 @@ +include ../../Makefile.options + +moonhash.so: moonhash.o + $(LD) $(LDFLAGS) -o moonhash.so moonhash.o + +moonhash.o: moonhash.c moonhash_sha3.c + $(CC) -c $(CFLAGS) -o moonhash.o moonhash.c + +clean:: + rm -f moonhash.o moonhash.so diff -r e360b1933c78 -r b36e366bba2b libraries/moonhash/moonhash.autodoc.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libraries/moonhash/moonhash.autodoc.lua Sun Aug 13 03:22:48 2017 +0200 @@ -0,0 +1,65 @@ +--[[-- +hash = -- SHA3-224 digest (in hex notation) of input string +moonhash.sha3_224( + data -- input string +) + +Calculates the SHA3-224 checksum with a security of 112 bits for collision attacks and 224 bits for preimage and second preimage attacks. + +--]]-- +-- Implemented in moonhash.c and moonhash_sha3.c +--//-- + + +--[[-- +hash = -- SHA3-256 digest (in hex notation) of input string +moonhash.sha3_256( + data -- input string +) + +Calculates the SHA3-256 checksum with a security of 128 bits for collision attacks and 256 bits for preimage and second preimage attacks. + +--]]-- +-- Implemented in moonhash.c and moonhash_sha3.c +--//-- + + +--[[-- +hash = -- SHA3-384 digest (in hex notation) of input string +moonhash.sha3_384( + data -- input string +) + +Calculates the SHA3-384 checksum with a security of 192 bits for collision attacks and 384 bits for preimage and second preimage attacks. + +--]]-- +-- Implemented in moonhash.c and moonhash_sha3.c +--//-- + + +--[[-- +hash = -- SHA3-512 digest (in hex notation) of input string +moonhash.sha3_512( + data -- input string +) + +Calculates the SHA3-512 checksum with a security of 512 bits for collision attacks and 256 bits for preimage and second preimage attacks. + +--]]-- +-- Implemented in moonhash.c and moonhash_sha3.c +--//-- + + +--[[-- +hash = -- 128 bits of SHAKE128 digest (in hex notation) of input string +moonhash.shake128_128( + data -- input string +) + +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. + +--]]-- +-- Implemented in moonhash.c and moonhash_sha3.c +--//-- + + diff -r e360b1933c78 -r b36e366bba2b libraries/moonhash/moonhash.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libraries/moonhash/moonhash.c Sun Aug 13 03:22:48 2017 +0200 @@ -0,0 +1,72 @@ +#include +#include +#include "moonhash_sha3.c" + +void moonhash_push_hex(lua_State *L, unsigned char *buf, int len) { + int i; + unsigned char n; + char str[2*len+1]; + for (i=0; i> 4; + str[2*i+0] = n + ((n < 10) ? '0' : ('a' - 10)); + n = buf[i] & 0x0f; + str[2*i+1] = n + ((n < 10) ? '0' : ('a' - 10)); + } + str[2*len] = 0; + lua_pushstring(L, str); +} + +typedef void (*moonhash_sha3_fptr)(const uint8_t *, uint64_t, uint8_t *); +typedef void (*moonhash_shake_fptr)(const uint8_t *, uint64_t, uint8_t *, uint64_t); + +int moonhash_sha3(lua_State *L, moonhash_sha3_fptr hashfunc, int len) { + const char *input; + size_t inputlen; + unsigned char output[len]; + input = luaL_checklstring(L, 1, &inputlen); + hashfunc((const uint8_t *)input, inputlen, output); + moonhash_push_hex(L, output, len); + return 1; +} + +int moonhash_sha3_224(lua_State *L) { + return moonhash_sha3(L, FIPS202_SHA3_224, 224/8); +} +int moonhash_sha3_256(lua_State *L) { + return moonhash_sha3(L, FIPS202_SHA3_256, 256/8); +} +int moonhash_sha3_384(lua_State *L) { + return moonhash_sha3(L, FIPS202_SHA3_384, 384/8); +} +int moonhash_sha3_512(lua_State *L) { + return moonhash_sha3(L, FIPS202_SHA3_512, 512/8); +} + +int moonhash_shake(lua_State *L, moonhash_shake_fptr shakefunc, int len) { + const char *input; + size_t inputlen; + unsigned char output[len]; + input = luaL_checklstring(L, 1, &inputlen); + shakefunc((const uint8_t *)input, inputlen, output, len); + moonhash_push_hex(L, output, len); + return 1; +} + +int moonhash_shake128_128(lua_State *L) { + return moonhash_shake(L, FIPS202_SHAKE128, 128/8); +} + +static const struct luaL_Reg moonhash_module_functions[] = { + {"sha3_224", moonhash_sha3_224}, + {"sha3_256", moonhash_sha3_256}, + {"sha3_384", moonhash_sha3_384}, + {"sha3_512", moonhash_sha3_512}, + {"shake128_128", moonhash_shake128_128}, + {NULL, NULL} +}; + +int luaopen_moonhash(lua_State *L) { + lua_newtable(L); + luaL_setfuncs(L, moonhash_module_functions, 0); + return 1; +} diff -r e360b1933c78 -r b36e366bba2b libraries/moonhash/moonhash_sha3.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libraries/moonhash/moonhash_sha3.c Sun Aug 13 03:22:48 2017 +0200 @@ -0,0 +1,47 @@ +/* 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. */ + +#include +#include + +#define FOR(i,n) for(i=0; i>1; } +#define ROL(a,o) ((((u64)a)<>(64-o))) +#define load64 le64dec +#define store64 le64enc +#if _BYTE_ORDER == _LITTLE_ENDIAN +static void xor64(u8 *x, u64 u) { *(u64 *)x ^= u; } +#else +static void xor64(u8 *x, u64 u) { ui i; FOR(i,8) { x[i]^=u; u>>=8; } } +#endif +#define rL(x,y) load64((u8*)s+8*(x+5*y)) +#define wL(x,y,l) store64((u8*)s+8*(x+5*y),l) +#define XL(x,y,l) xor64((u8*)s+8*(x+5*y),l) +static void KeccakF1600(void *s) +{ + ui r,x,y,i,j,Y; u8 R=0x01; u64 C[5],D; + for(i=0; i<24; i++) { + /*θ*/ 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); } + /*ρπ*/ 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]; } + /*χ*/ 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])); } + /*ι*/ FOR(j,7) if (LFSR86540(&R)) XL(0,0,(u64)1<<((1<0) { b=(inLen0) { b=(outLen0) KeccakF1600(s); } +}