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