webmcp
changeset 506:83b3882dc31b
New functions moonhash.shake128(data, len, alphabet), moonhash.shake256(data, len, alphabet)
Removed moonhash.shake128_128(...)
Removed moonhash.shake128_128(...)
author | jbe |
---|---|
date | Wed Aug 16 00:31:11 2017 +0200 (2017-08-16) |
parents | b53dee61a930 |
children | ac5f7a40b8c4 |
files | libraries/moonhash/moonhash.autodoc.lua libraries/moonhash/moonhash.c |
line diff
1.1 --- a/libraries/moonhash/moonhash.autodoc.lua Tue Aug 15 21:00:51 2017 +0200 1.2 +++ b/libraries/moonhash/moonhash.autodoc.lua Wed Aug 16 00:31:11 2017 +0200 1.3 @@ -51,15 +51,31 @@ 1.4 1.5 1.6 --[[-- 1.7 -hash = -- 128 bits of SHAKE128 digest (in hex notation) of input string 1.8 -moonhash.shake128_128( 1.9 - data -- input string 1.10 +hash = -- SHAKE128 digest of input string 1.11 +moonhash.shake128( 1.12 + input_data, -- input string 1.13 + output_length, -- number of characters (not bytes or bits) in output, defaults to 32 1.14 + output_alphabet -- characters for encoding, defaults to "0123456789abcdef" for hex encoding 1.15 ) 1.16 1.17 -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. 1.18 +Calculates a SHAKE128 digest (FIPS 202) with a security of math.min(128, math.log(#output_alphabet, 2) * output_length/2) for collision attacks and math.min(128, math.log(#output_alphabet, 2) * output_length) for preimage and second preimage attacks. If #output_alphabet is a power of 2, a direct base-N encoding is performed. Otherwise, a base-N encoding with N equal to the next higher power of 2 is performed, and all character values smaller than or equal to #output_alphabet are discarded from the stream (the process is repeated until the hash length reaches the required output_length). 1.19 1.20 --]]-- 1.21 -- Implemented in moonhash.c and moonhash_sha3.c 1.22 --//-- 1.23 1.24 1.25 +--[[-- 1.26 +hash = -- SHAKE256 digest of input string 1.27 +moonhash.shake256( 1.28 + input_data, -- input string 1.29 + output_length, -- number of characters (not bytes or bits) in output, defaults to 64 1.30 + output_alphabet -- characters for encoding, defaults to "0123456789abcdef" for hex encoding 1.31 +) 1.32 + 1.33 +Calculates a SHAKE256 digest (FIPS 202) with a security of math.min(256, math.log(#output_alphabet, 2) * output_length/2) for collision attacks and math.min(256, math.log(#output_alphabet, 2) * output_length) for preimage and second preimage attacks. If #output_alphabet is a power of 2, a direct base-N encoding is performed. Otherwise, a base-N encoding with N equal to the next higher power of 2 is performed, and all character values smaller than or equal to #output_alphabet are discarded from the stream (the process is repeated until the hash length reaches the required output_length). 1.34 + 1.35 +--]]-- 1.36 +-- Implemented in moonhash.c and moonhash_sha3.c 1.37 +--//-- 1.38 +
2.1 --- a/libraries/moonhash/moonhash.c Tue Aug 15 21:00:51 2017 +0200 2.2 +++ b/libraries/moonhash/moonhash.c Wed Aug 16 00:31:11 2017 +0200 2.3 @@ -42,18 +42,59 @@ 2.4 return moonhash_sha3(L, FIPS202_SHA3_512, 512/8); 2.5 } 2.6 2.7 -int moonhash_shake(lua_State *L, moonhash_shake_fptr shakefunc, int len) { 2.8 +int moonhash_shake(lua_State *L, int R, lua_Integer deflen) { 2.9 const char *input; 2.10 size_t inputlen; 2.11 - unsigned char output[len]; 2.12 + lua_Integer outputlen; 2.13 + const char *alphabet; 2.14 + size_t alen; 2.15 + int abits = 0; 2.16 + uint8_t s[200]; 2.17 + luaL_Buffer luabuf; 2.18 + uint8_t *output; 2.19 + int readpos = 0; 2.20 + lua_Integer writepos = 0; 2.21 + int rbits = 0; 2.22 + int rbuf = 0; 2.23 + int rvalue; 2.24 input = luaL_checklstring(L, 1, &inputlen); 2.25 - shakefunc((const uint8_t *)input, inputlen, output, len); 2.26 - moonhash_push_hex(L, output, len); 2.27 + outputlen = luaL_optinteger(L, 2, deflen); 2.28 + alphabet = luaL_optlstring(L, 3, "0123456789abcdef", &alen); 2.29 + luaL_argcheck(L, alen>1, 3, "too few characters in alphabet"); 2.30 + { 2.31 + size_t t = alen-1; 2.32 + while (t) { 2.33 + abits++; 2.34 + if (abits > 8) luaL_argcheck(L, 0, 3, "too many characters in alphabet"); 2.35 + t >>= 1; 2.36 + } 2.37 + } 2.38 + KeccakF1600Init(s, R, (const uint8_t *)input, inputlen, 0x1F); 2.39 + output = (uint8_t *)luaL_buffinitsize(L, &luabuf, outputlen); 2.40 + while (writepos < outputlen) { 2.41 + if (rbits < abits) { 2.42 + if (readpos == R) { 2.43 + KeccakF1600(s); 2.44 + readpos = 0; 2.45 + } 2.46 + rbuf = (rbuf << 8) | s[readpos++]; 2.47 + rbits += 8; 2.48 + } 2.49 + rbits -= abits; 2.50 + rvalue = rbuf >> rbits; 2.51 + rbuf &= (1<<rbits)-1; 2.52 + if (rvalue < alen) output[writepos++] = alphabet[rvalue]; 2.53 + } 2.54 + luaL_pushresultsize(&luabuf, outputlen); 2.55 return 1; 2.56 } 2.57 2.58 -int moonhash_shake128_128(lua_State *L) { 2.59 - return moonhash_shake(L, FIPS202_SHAKE128, 128/8); 2.60 +int moonhash_shake128(lua_State *L) { 2.61 + return moonhash_shake(L, (1600-2*128)/8, 128/4); 2.62 +} 2.63 + 2.64 +int moonhash_shake256(lua_State *L) { 2.65 + return moonhash_shake(L, (1600-2*256)/8, 256/4); 2.66 } 2.67 2.68 static const struct luaL_Reg moonhash_module_functions[] = { 2.69 @@ -61,7 +102,8 @@ 2.70 {"sha3_256", moonhash_sha3_256}, 2.71 {"sha3_384", moonhash_sha3_384}, 2.72 {"sha3_512", moonhash_sha3_512}, 2.73 - {"shake128_128", moonhash_shake128_128}, 2.74 + {"shake128", moonhash_shake128}, 2.75 + {"shake256", moonhash_shake256}, 2.76 {NULL, NULL} 2.77 }; 2.78