webmcp

annotate libraries/moonhash/moonhash.c @ 507:ac5f7a40b8c4

Extended documentation (mostly on "Configuration, pre-fork and post-fork initializers")
author jbe
date Sun Aug 20 23:12:16 2017 +0200 (2017-08-20)
parents 83b3882dc31b
children
rev   line source
jbe@499 1 #include <lua.h>
jbe@499 2 #include <lauxlib.h>
jbe@499 3 #include "moonhash_sha3.c"
jbe@499 4
jbe@499 5 void moonhash_push_hex(lua_State *L, unsigned char *buf, int len) {
jbe@499 6 int i;
jbe@499 7 unsigned char n;
jbe@499 8 char str[2*len+1];
jbe@499 9 for (i=0; i<len; i++) {
jbe@499 10 n = buf[i] >> 4;
jbe@499 11 str[2*i+0] = n + ((n < 10) ? '0' : ('a' - 10));
jbe@499 12 n = buf[i] & 0x0f;
jbe@499 13 str[2*i+1] = n + ((n < 10) ? '0' : ('a' - 10));
jbe@499 14 }
jbe@499 15 str[2*len] = 0;
jbe@499 16 lua_pushstring(L, str);
jbe@499 17 }
jbe@499 18
jbe@499 19 typedef void (*moonhash_sha3_fptr)(const uint8_t *, uint64_t, uint8_t *);
jbe@499 20 typedef void (*moonhash_shake_fptr)(const uint8_t *, uint64_t, uint8_t *, uint64_t);
jbe@499 21
jbe@499 22 int moonhash_sha3(lua_State *L, moonhash_sha3_fptr hashfunc, int len) {
jbe@499 23 const char *input;
jbe@499 24 size_t inputlen;
jbe@499 25 unsigned char output[len];
jbe@499 26 input = luaL_checklstring(L, 1, &inputlen);
jbe@499 27 hashfunc((const uint8_t *)input, inputlen, output);
jbe@499 28 moonhash_push_hex(L, output, len);
jbe@499 29 return 1;
jbe@499 30 }
jbe@499 31
jbe@499 32 int moonhash_sha3_224(lua_State *L) {
jbe@499 33 return moonhash_sha3(L, FIPS202_SHA3_224, 224/8);
jbe@499 34 }
jbe@499 35 int moonhash_sha3_256(lua_State *L) {
jbe@499 36 return moonhash_sha3(L, FIPS202_SHA3_256, 256/8);
jbe@499 37 }
jbe@499 38 int moonhash_sha3_384(lua_State *L) {
jbe@499 39 return moonhash_sha3(L, FIPS202_SHA3_384, 384/8);
jbe@499 40 }
jbe@499 41 int moonhash_sha3_512(lua_State *L) {
jbe@499 42 return moonhash_sha3(L, FIPS202_SHA3_512, 512/8);
jbe@499 43 }
jbe@499 44
jbe@506 45 int moonhash_shake(lua_State *L, int R, lua_Integer deflen) {
jbe@499 46 const char *input;
jbe@499 47 size_t inputlen;
jbe@506 48 lua_Integer outputlen;
jbe@506 49 const char *alphabet;
jbe@506 50 size_t alen;
jbe@506 51 int abits = 0;
jbe@506 52 uint8_t s[200];
jbe@506 53 luaL_Buffer luabuf;
jbe@506 54 uint8_t *output;
jbe@506 55 int readpos = 0;
jbe@506 56 lua_Integer writepos = 0;
jbe@506 57 int rbits = 0;
jbe@506 58 int rbuf = 0;
jbe@506 59 int rvalue;
jbe@499 60 input = luaL_checklstring(L, 1, &inputlen);
jbe@506 61 outputlen = luaL_optinteger(L, 2, deflen);
jbe@506 62 alphabet = luaL_optlstring(L, 3, "0123456789abcdef", &alen);
jbe@506 63 luaL_argcheck(L, alen>1, 3, "too few characters in alphabet");
jbe@506 64 {
jbe@506 65 size_t t = alen-1;
jbe@506 66 while (t) {
jbe@506 67 abits++;
jbe@506 68 if (abits > 8) luaL_argcheck(L, 0, 3, "too many characters in alphabet");
jbe@506 69 t >>= 1;
jbe@506 70 }
jbe@506 71 }
jbe@506 72 KeccakF1600Init(s, R, (const uint8_t *)input, inputlen, 0x1F);
jbe@506 73 output = (uint8_t *)luaL_buffinitsize(L, &luabuf, outputlen);
jbe@506 74 while (writepos < outputlen) {
jbe@506 75 if (rbits < abits) {
jbe@506 76 if (readpos == R) {
jbe@506 77 KeccakF1600(s);
jbe@506 78 readpos = 0;
jbe@506 79 }
jbe@506 80 rbuf = (rbuf << 8) | s[readpos++];
jbe@506 81 rbits += 8;
jbe@506 82 }
jbe@506 83 rbits -= abits;
jbe@506 84 rvalue = rbuf >> rbits;
jbe@506 85 rbuf &= (1<<rbits)-1;
jbe@506 86 if (rvalue < alen) output[writepos++] = alphabet[rvalue];
jbe@506 87 }
jbe@506 88 luaL_pushresultsize(&luabuf, outputlen);
jbe@499 89 return 1;
jbe@499 90 }
jbe@499 91
jbe@506 92 int moonhash_shake128(lua_State *L) {
jbe@506 93 return moonhash_shake(L, (1600-2*128)/8, 128/4);
jbe@506 94 }
jbe@506 95
jbe@506 96 int moonhash_shake256(lua_State *L) {
jbe@506 97 return moonhash_shake(L, (1600-2*256)/8, 256/4);
jbe@499 98 }
jbe@499 99
jbe@499 100 static const struct luaL_Reg moonhash_module_functions[] = {
jbe@499 101 {"sha3_224", moonhash_sha3_224},
jbe@499 102 {"sha3_256", moonhash_sha3_256},
jbe@499 103 {"sha3_384", moonhash_sha3_384},
jbe@499 104 {"sha3_512", moonhash_sha3_512},
jbe@506 105 {"shake128", moonhash_shake128},
jbe@506 106 {"shake256", moonhash_shake256},
jbe@499 107 {NULL, NULL}
jbe@499 108 };
jbe@499 109
jbe@499 110 int luaopen_moonhash(lua_State *L) {
jbe@499 111 lua_newtable(L);
jbe@499 112 luaL_setfuncs(L, moonhash_module_functions, 0);
jbe@499 113 return 1;
jbe@499 114 }

Impressum / About Us