From dbd630f2bf86d3fb97049eb1c9e1af3af12590b6 Mon Sep 17 00:00:00 2001 From: ame Date: Thu, 11 Apr 2024 11:59:56 -0500 Subject: blake1 complete:3 --- docs/crypto.md | 10 +- src/crypto.h | 8 +- src/hash/blake.c | 528 ++++++++++++++++++++++++++++++++++++++++++++++--------- src/hash/blake.h | 17 +- 4 files changed, 476 insertions(+), 87 deletions(-) diff --git a/docs/crypto.md b/docs/crypto.md index 673ec9c..d812a4b 100644 --- a/docs/crypto.md +++ b/docs/crypto.md @@ -63,10 +63,10 @@ insane amount anyways). i likely will go back and rewrite all of these to fix bo | spookyhash32_v2 | 32 | *seed | | n | | blake2b | length of arg 2 * 8 | *output len (default is 64), *key | | todo | | blake2s | length of arg 2 * 8 | *output len (default is 32), *key | | todo | -| blake256 | 256 | nil | | todo | -| blake224 | 224 | nil | | todo | -| blake512 | 512 | nil | | todo | -| blake384 | 384 | nil | | todo | +| blake256 | 256 | nil | | y | +| blake224 | 224 | nil | | y | +| blake512 | 512 | nil | | y | +| blake384 | 384 | nil | | y | ### usage @@ -75,7 +75,7 @@ llib.crypto.sha512("meow") -- e88348269bad036160f0d9558b7c5de68163b50e1a6ce46e85 llib.crypto.sha512_t("meow", 224) -- would be sha512/224 - ad5e403e0d74532187f4e1665c7e705ab5eb3c2fe07ae73a3ff998b2 ``` -functions supporting updates (listed with %, see note above) can be used like so: +functions supporting updates (see above) can be used like so: ```lua obj = llib.crypto.adler32() --adler32_init is equivilant to adler32 with no params diff --git a/src/crypto.h b/src/crypto.h index 54020a2..fe8cdd7 100644 --- a/src/crypto.h +++ b/src/crypto.h @@ -123,8 +123,8 @@ static const luaL_Reg crypto_function_list [] = { {"spookyhash128_v1", l_spookyhash128_v1}, {"spookyhash128_v2", l_spookyhash128_v2}, {"spookyhash64_v1", l_spookyhash64_v1}, {"spookyhash64_v2", l_spookyhash64_v2}, {"spookyhash32_v1", l_spookyhash32_v1}, {"spookyhash32_v2", l_spookyhash32_v2}, - {"blake2b", l_blake2b}, {"blake2s", l_blake2s}, {"blake256", l_blake256}, - {"blake224", l_blake224}, {"blake512", l_blake512}, {"blake384", l_blake384}, + {"blake2b", l_blake2b}, {"blake2s", l_blake2s}, + {"blake512", l_blake512}, {"blake384", l_blake384}, {"adler32",l_adler32}, {"adler32_init",l_adler32_init}, {"adler32_update",l_adler32_update}, {"adler32_final",l_adler32_final}, {"bsdchecksum",l_bsdchecksum}, {"bsdchecksum_init",l_bsdchecksum_init}, {"bsdchecksum_update",l_bsdchecksum_update}, {"bsdchecksum_final",l_bsdchecksum_final}, @@ -154,6 +154,10 @@ static const luaL_Reg crypto_function_list [] = { {"sha512_t", l_sha512_t}, {"sha512_t_init", l_sha512_t_init}, {"sha512_t_update", l_sha512_t_update}, {"sha512_t_final", l_sha512_t_final}, {"sha256",l_sha256}, {"sha256_init",l_sha256_init}, {"sha256_update",l_sha256_update}, {"sha256_final",l_sha256_final}, {"sha224",l_sha224}, {"sha224_init",l_sha224_init}, {"sha224_update",l_sha224_update}, {"sha224_final",l_sha224_final}, + {"blake256", l_blake256}, {"blake256_init", l_blake256_init}, {"blake256_update", l_blake256_update}, {"blake256_final", l_blake256_final}, + {"blake224", l_blake224}, {"blake224_init", l_blake224_init}, {"blake224_update", l_blake224_update}, {"blake224_final", l_blake224_final}, + {"blake512", l_blake512}, {"blake512_init", l_blake512_init}, {"blake512_update", l_blake512_update}, {"blake512_final", l_blake512_final}, + {"blake384", l_blake384}, {"blake384_init", l_blake384_init}, {"blake384_update", l_blake384_update}, {"blake384_final", l_blake384_final}, {"uuencode",l_uuencode}, {"uudecode",l_uudecode}, diff --git a/src/hash/blake.c b/src/hash/blake.c index e027b10..096c951 100644 --- a/src/hash/blake.c +++ b/src/hash/blake.c @@ -82,50 +82,183 @@ void compress256(uint32_t* hash, char *block, uint64_t compressed){ } for(int i = 0; i < 16; i++) hash[i % 8] ^= v[i]; + + //for(int i = 0; i != 8; i++) printf("%lx ", hash[i]); + //printf("\n"); } -void blake256(char *out, char *in, uint64_t inlen, enum blake256_v v){ - uint32_t hash[8] = {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, - 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19}; - if(v == b224){ - hash[0] = 0xc1059ed8; - hash[1] = 0x367cd507; - hash[2] = 0x3070dd17; - hash[3] = 0xf70e5939; - hash[4] = 0xffc00b31; - hash[5] = 0x68581511; - hash[6] = 0x64f98fa7; - hash[7] = 0xbefa4fa4; - } +struct blake256_hash { + uint8_t* buffer; + size_t bufflen; + uint32_t total, *hash; + uint64_t compressed; +}; +#define blake224_hash blake256_hash + +#define bs 64 +struct blake256_hash blake256_init(){ + struct blake256_hash a = {.bufflen = 0, .total = 0, .compressed = 0}; + a.buffer = calloc(sizeof * a.buffer, bs); + a.hash = calloc(sizeof * a.hash, 8); + a.hash[0] = 0x6a09e667; + a.hash[1] = 0xbb67ae85; + a.hash[2] = 0x3c6ef372; + a.hash[3] = 0xa54ff53a; + a.hash[4] = 0x510e527f; + a.hash[5] = 0x9b05688c; + a.hash[6] = 0x1f83d9ab; + a.hash[7] = 0x5be0cd19; + return a; +} - int aw = inter(inlen, 64); - uint64_t compressed = 0; - char* owo = calloc(aw, sizeof * owo); - memcpy(owo, in, inlen); +struct blake256_hash blake256_init_l(lua_State* L){ + struct blake256_hash a = {.bufflen = 0, .total = 0, .compressed = 0}; + a.buffer = lua_newuserdata(L, sizeof * a.buffer * bs); + a.hash = lua_newuserdata(L, sizeof * a.hash * 8); + memset(a.buffer, 0, bs); + a.hash[0] = 0x6a09e667; + a.hash[1] = 0xbb67ae85; + a.hash[2] = 0x3c6ef372; + a.hash[3] = 0xa54ff53a; + a.hash[4] = 0x510e527f; + a.hash[5] = 0x9b05688c; + a.hash[6] = 0x1f83d9ab; + a.hash[7] = 0x5be0cd19; + return a; +} - owo[inlen] = 0x80; +struct blake256_hash blake224_init(){ + struct blake256_hash a = {.bufflen = 0, .total = 0, .compressed = 0}; + a.buffer = calloc(sizeof * a.buffer, bs); + a.hash = calloc(sizeof * a.hash, 8); + a.hash[0] = 0xc1059ed8; + a.hash[1] = 0x367cd507; + a.hash[2] = 0x3070dd17; + a.hash[3] = 0xf70e5939; + a.hash[4] = 0xffc00b31; + a.hash[5] = 0x68581511; + a.hash[6] = 0x64f98fa7; + a.hash[7] = 0xbefa4fa4; + return a; +} - if(inlen == 55){ - owo[inlen] = v==b256?0x81:0x80; - } else { - owo[aw - 9] = v==b256?0x01:0x00; - } +struct blake256_hash blake224_init_l(lua_State* L){ + struct blake256_hash a = {.bufflen = 0, .total = 0, .compressed = 0}; + a.buffer = lua_newuserdata(L, sizeof * a.buffer * bs); + a.hash = lua_newuserdata(L, sizeof * a.hash * 8); + memset(a.buffer, 0, bs); + a.hash[0] = 0xc1059ed8; + a.hash[1] = 0x367cd507; + a.hash[2] = 0x3070dd17; + a.hash[3] = 0xf70e5939; + a.hash[4] = 0xffc00b31; + a.hash[5] = 0x68581511; + a.hash[6] = 0x64f98fa7; + a.hash[7] = 0xbefa4fa4; + return a; +} - U32TO8_BIG(owo + aw - 8, 0x0); - U32TO8_BIG(owo + aw - 4, inlen << 3); +void blake256_round(struct blake256_hash* hash){ + compress256(hash->hash, (char*)hash->buffer, hash->compressed * 8); +} + +#define blake224_update blake256_update +void blake256_update(uint8_t* input, size_t len, struct blake256_hash* hash){ + hash->total += len; + size_t total_add = len + hash->bufflen; + size_t read = 0; + if(total_add < bs){ + memcpy(hash->buffer + hash->bufflen, input, len); + hash->bufflen += len; + return; + } - for(; aw >= 64;){ - compressed += 64; - if(aw == 64) compressed = inlen; + for(; total_add >= bs;){ + memcpy(hash->buffer + hash->bufflen, input + read, bs - hash->bufflen); + total_add -= bs; + hash->bufflen = 0; + read += bs; + hash->compressed += 64; + blake256_round(hash); + } - compress256(hash, owo, compressed * 8); - aw -= 64; - owo += 64; - } + memset(hash->buffer, 0, bs); - for(int i = 0; i != (v==b256?8:7); i++){ - sprintf(out, "%s%08x",out,(hash)[i]); - } + if(0 != total_add){ + memcpy(hash->buffer, input + read, total_add); + hash->bufflen = total_add; + } +} + +void _blake256_final(struct blake256_hash* hash, char* out_stream){ + hash->compressed += hash->bufflen; + + hash->buffer[hash->bufflen] = 0x80; + + if(hash->bufflen > 55) { + //too large, needs another buffer + memset(hash->buffer + hash->bufflen + 1, 0, 64 - hash->bufflen); + blake256_round(hash); + hash->compressed = 0; + memset(hash->buffer, 0, 64); + } + + size_t lhhh = 8*hash->total; + U32TO8_BIG(hash->buffer + bs - 8, 0x0); + U32TO8_BIG(hash->buffer + bs - 4, hash->total << 3); + /*for(int i = 0; i != bs; i++) printf("%x ", hash->buffer[i]); + printf("\n");*/ + blake256_round(hash); +} + +void blake256_final(struct blake256_hash* hash, char* out_stream){ + uint8_t old[bs]; + struct blake256_hash old_hash; + memcpy(&old_hash, hash, sizeof * hash); + memcpy(old, hash->buffer, bs); + + if(hash->bufflen == 55) hash->buffer[hash->bufflen] = 0x81; + else hash->buffer[bs - 9] = 0x01; + + _blake256_final(hash, out_stream); + + for(int i = 0; i != 8; i++){ + sprintf(out_stream, "%s%08x",out_stream,(hash->hash)[i]); + } + + memcpy(hash, &old_hash, sizeof * hash); + memcpy(hash->buffer, old, bs); +} + +void blake224_final(struct blake256_hash* hash, char* out_stream){ + uint8_t old[bs]; + struct blake256_hash old_hash; + memcpy(&old_hash, hash, sizeof * hash); + memcpy(old, hash->buffer, bs); + + if(hash->bufflen == 55) hash->buffer[hash->bufflen] = 0x80; + else hash->buffer[bs - 9] = 0x00; + + _blake256_final(hash, out_stream); + + for(int i = 0; i != 7; i++){ + sprintf(out_stream, "%s%08x",out_stream,(hash->hash)[i]); + } + + memcpy(hash, &old_hash, sizeof * hash); + memcpy(hash->buffer, old, bs); +} + +void blake256(char *out, char *in, uint64_t inlen){ + struct blake256_hash a = blake256_init(); + blake256_update((uint8_t*)in, inlen, &a); + blake256_final(&a, out); +} + +void blake224(char *out, char *in, uint64_t inlen){ + struct blake224_hash a = blake224_init(); + blake224_update((uint8_t*)in, inlen, &a); + blake224_final(&a, out); } #define blake_round_512(a,b,c,d,e) \ @@ -170,101 +303,338 @@ void compress512(uint64_t* hash, uint8_t *block, uint64_t compressed){ for(int i = 0; i < 16; i++) hash[i % 8] ^= v[i]; } -void blake512(char *out, char *in, uint64_t inlen, enum blake512_v v){ - uint64_t hash[8] = {0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL, 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL, - 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL, 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL}; - - if(v == b384){ - hash[0] = 0xcbbb9d5dc1059ed8ULL; - hash[1] = 0x629a292a367cd507ULL; - hash[2] = 0x9159015a3070dd17ULL; - hash[3] = 0x152fecd8f70e5939ULL; - hash[4] = 0x67332667ffc00b31ULL; - hash[5] = 0x8eb44a8768581511ULL; - hash[6] = 0xdb0c2e0d64f98fa7ULL; - hash[7] = 0x47b5481dbefa4fa4ULL; - } +struct blake512_hash { + uint8_t* buffer; + size_t bufflen; + uint64_t total, *hash; + uint64_t compressed; +}; +#define blake384_hash blake512_hash +//#undef bs +#define bs_2 128 + +struct blake512_hash blake512_init(){ + struct blake512_hash a = {.bufflen = 0, .total = 0, .compressed = 0}; + a.buffer = calloc(sizeof * a.buffer, bs_2); + a.hash = calloc(sizeof * a.hash, 8); + a.hash[0] = 0x6a09e667f3bcc908ULL; + a.hash[1] = 0xbb67ae8584caa73bULL; + a.hash[2] = 0x3c6ef372fe94f82bULL; + a.hash[3] = 0xa54ff53a5f1d36f1ULL; + a.hash[4] = 0x510e527fade682d1ULL; + a.hash[5] = 0x9b05688c2b3e6c1fULL; + a.hash[6] = 0x1f83d9abfb41bd6bULL; + a.hash[7] = 0x5be0cd19137e2179ULL; + return a; +} - int aw = inter(inlen, 128); - uint64_t compressed = 0; - uint8_t* owo = calloc(aw, sizeof * owo); - memcpy(owo, in, inlen); +struct blake512_hash blake512_init_l(lua_State* L){ + struct blake512_hash a = {.bufflen = 0, .total = 0, .compressed = 0}; + a.buffer = lua_newuserdata(L, sizeof * a.buffer * bs_2); + a.hash = lua_newuserdata(L, sizeof * a.hash * 8); + memset(a.buffer, 0, bs_2); + a.hash[0] = 0x6a09e667f3bcc908ULL; + a.hash[1] = 0xbb67ae8584caa73bULL; + a.hash[2] = 0x3c6ef372fe94f82bULL; + a.hash[3] = 0xa54ff53a5f1d36f1ULL; + a.hash[4] = 0x510e527fade682d1ULL; + a.hash[5] = 0x9b05688c2b3e6c1fULL; + a.hash[6] = 0x1f83d9abfb41bd6bULL; + a.hash[7] = 0x5be0cd19137e2179ULL; + return a; +} - owo[inlen] = 0x80; +struct blake384_hash blake384_init(){ + struct blake384_hash a = {.bufflen = 0, .total = 0, .compressed = 0}; + a.buffer = calloc(sizeof * a.buffer, bs_2); + a.hash = calloc(sizeof * a.hash, 8); + a.hash[0] = 0xcbbb9d5dc1059ed8ULL; + a.hash[1] = 0x629a292a367cd507ULL; + a.hash[2] = 0x9159015a3070dd17ULL; + a.hash[3] = 0x152fecd8f70e5939ULL; + a.hash[4] = 0x67332667ffc00b31ULL; + a.hash[5] = 0x8eb44a8768581511ULL; + a.hash[6] = 0xdb0c2e0d64f98fa7ULL; + a.hash[7] = 0x47b5481dbefa4fa4ULL; + return a; +} - if(inlen == 111){ - owo[inlen] = v==b512?0x81:0x80; - } else { - owo[aw - 17] = v==b512?0x01:0x00; - } +struct blake384_hash blake384_init_l(lua_State* L){ + struct blake384_hash a = {.bufflen = 0, .total = 0, .compressed = 0}; + a.buffer = lua_newuserdata(L, sizeof * a.buffer * bs_2); + a.hash = lua_newuserdata(L, sizeof * a.hash * 8); + memset(a.buffer, 0, bs_2); + a.hash[0] = 0xcbbb9d5dc1059ed8ULL; + a.hash[1] = 0x629a292a367cd507ULL; + a.hash[2] = 0x9159015a3070dd17ULL; + a.hash[3] = 0x152fecd8f70e5939ULL; + a.hash[4] = 0x67332667ffc00b31ULL; + a.hash[5] = 0x8eb44a8768581511ULL; + a.hash[6] = 0xdb0c2e0d64f98fa7ULL; + a.hash[7] = 0x47b5481dbefa4fa4ULL; + return a; +} + +void blake512_round(struct blake512_hash* hash){ + compress512(hash->hash, hash->buffer, hash->compressed * 8); +} + +#define blake384_update blake512_update +void blake512_update(uint8_t* input, size_t len, struct blake512_hash* hash){ + hash->total += len; + size_t total_add = len + hash->bufflen; + size_t read = 0; + if(total_add < bs_2){ + memcpy(hash->buffer + hash->bufflen, input, len); + hash->bufflen += len; + return; + } + + for(; total_add >= bs_2;){ + memcpy(hash->buffer + hash->bufflen, input + read, bs_2 - hash->bufflen); + total_add -= bs_2; + hash->bufflen = 0; + read += bs_2; + hash->compressed += 128; + blake512_round(hash); + } + + memset(hash->buffer, 0, bs_2); + + if(0 != total_add){ + memcpy(hash->buffer, input + read, total_add); + hash->bufflen = total_add; + } +} - U64TO8_BIG(owo + aw - 8, inlen << 3); +void _blake512_final(struct blake512_hash* hash, char* out_stream){ + hash->compressed += hash->bufflen; - for(; aw >= 128;){ - compressed += 128; - if(aw == 128) compressed = inlen; + hash->buffer[hash->bufflen] = 0x80; - compress512(hash, owo, compressed * 8); - aw -= 128; - owo += 128; + if(hash->bufflen > bs_2 - 16) { + //too large, needs another buffer + memset(hash->buffer + hash->bufflen + 1, 0, 64 - hash->bufflen); + blake512_round(hash); + hash->compressed = 0; + memset(hash->buffer, 0, 64); + } + + size_t lhhh = 8*hash->total; + U64TO8_BIG(hash->buffer + bs_2 - 8, hash->total << 3); + + blake512_round(hash); +} + +void blake512_final(struct blake512_hash* hash, char* out_stream){ + uint8_t old[bs_2]; + struct blake512_hash old_hash; + memcpy(&old_hash, hash, sizeof * hash); + memcpy(old, hash->buffer, bs_2); + + if(hash->bufflen == 111) hash->buffer[hash->bufflen] = 0x81; + else hash->buffer[bs_2 - 17] = 0x01; + + _blake512_final(hash, out_stream); + + for(int i = 0; i != 8; i++){ + sprintf(out_stream, "%s%016llx",out_stream, (hash->hash)[i]); } - for(int i = 0; i != (v==b512?8:6); i++){ - sprintf(out, "%s%016llx",out, (hash)[i]); + memcpy(hash, &old_hash, sizeof * hash); + memcpy(hash->buffer, old, bs_2); +} + +void blake512(uint8_t* in, size_t len, char* out){ + struct blake512_hash a = blake512_init(); + blake512_update(in, len, &a); + blake512_final(&a, out); +} + +void blake384_final(struct blake384_hash* hash, char* out_stream){ + uint8_t old[bs_2]; + struct blake384_hash old_hash; + memcpy(&old_hash, hash, sizeof * hash); + memcpy(old, hash->buffer, bs_2); + + if(hash->bufflen == 111) hash->buffer[hash->bufflen] = 0x80; + else hash->buffer[bs_2 - 17] = 0x00; + + _blake512_final(hash, out_stream); + + for(int i = 0; i != 6; i++){ + sprintf(out_stream, "%s%016llx",out_stream, (hash->hash)[i]); } + + memcpy(hash, &old_hash, sizeof * hash); + memcpy(hash->buffer, old, bs_2); +} + +void blake384(uint8_t* in, size_t len, char* out){ + struct blake384_hash a = blake384_init(); + blake384_update(in, len, &a); + blake384_final(&a, out); +} + +int l_blake256_clone(lua_State* L){ + struct blake256_hash* a = (struct blake256_hash*)lua_touserdata(L, -1); + l_blake256_init(L); + struct blake256_hash* b = (struct blake256_hash*)lua_touserdata(L, -1); + + memcpy(b->hash, a->hash, 8 * sizeof * b->hash); + memcpy(b->buffer, a->buffer, bs * sizeof * b->buffer); + b->total = a->total; + b->bufflen = a->bufflen; + b->compressed = a->compressed; + b->total = a->total; + return 1; +} + +common_hash_init_update(blake256); + +int l_blake256_final(lua_State* L){ + struct blake256_hash* a = (struct blake256_hash*)lua_touserdata(L, 1); + + char digest[257] = {0}; + blake256_final(a, digest); + + lua_pushstring(L, digest); + return 1; } int l_blake256(lua_State* L){ + if(lua_gettop(L) == 0) return l_blake256_init(L); size_t len = 0; - char* a = (char*)luaL_checklstring(L, 1, &len); + uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); int argv = lua_gettop(L); char digest[257] = {0}; - memset(digest, 0, 257); - blake256(digest, a, len, b256); + blake256(digest, (char*)a, len); + lua_pushstring(L, digest); return 1; } +int l_blake224_clone(lua_State* L){ + struct blake224_hash* a = (struct blake224_hash*)lua_touserdata(L, -1); + l_blake224_init(L); + struct blake224_hash* b = (struct blake224_hash*)lua_touserdata(L, -1); + + memcpy(b->hash, a->hash, 8 * sizeof * b->hash); + memcpy(b->buffer, a->buffer, bs * sizeof * b->buffer); + b->total = a->total; + b->bufflen = a->bufflen; + b->compressed = a->compressed; + b->total = a->total; + return 1; +} + +common_hash_init_update(blake224); + +int l_blake224_final(lua_State* L){ + struct blake224_hash* a = (struct blake224_hash*)lua_touserdata(L, 1); + + char digest[257] = {0}; + blake224_final(a, digest); + + lua_pushstring(L, digest); + return 1; +} + int l_blake224(lua_State* L){ + if(lua_gettop(L) == 0) return l_blake224_init(L); size_t len = 0; char* a = (char*)luaL_checklstring(L, 1, &len); int argv = lua_gettop(L); char digest[257] = {0}; - memset(digest, 0, 257); - blake256(digest, a, len, b224); + blake224(digest, (char*)a, len); + lua_pushstring(L, digest); return 1; } +int l_blake512_clone(lua_State* L){ + struct blake512_hash* a = (struct blake512_hash*)lua_touserdata(L, -1); + l_blake512_init(L); + struct blake512_hash* b = (struct blake512_hash*)lua_touserdata(L, -1); + + memcpy(b->hash, a->hash, 8 * sizeof * b->hash); + memcpy(b->buffer, a->buffer, bs_2 * sizeof * b->buffer); + b->total = a->total; + b->bufflen = a->bufflen; + b->compressed = a->compressed; + b->total = a->total; + return 1; +} + +common_hash_init_update(blake512); + +int l_blake512_final(lua_State* L){ + struct blake512_hash* a = (struct blake512_hash*)lua_touserdata(L, 1); + + char digest[513] = {0}; + blake512_final(a, digest); + + lua_pushstring(L, digest); + return 1; +} + int l_blake512(lua_State* L){ + if(lua_gettop(L) == 0) return l_blake512_init(L); size_t len = 0; - char* a = (char*)luaL_checklstring(L, 1, &len); + uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); int argv = lua_gettop(L); char digest[513] = {0}; - memset(digest, 0, 513); + //memset(digest, 0, 513); - blake512(digest, a, len, b512); + //blake512(digest, a, len, b512); + blake512(a, len, digest); lua_pushstring(L, digest); return 1; } +int l_blake384_clone(lua_State* L){ + struct blake384_hash* a = (struct blake384_hash*)lua_touserdata(L, -1); + l_blake384_init(L); + struct blake384_hash* b = (struct blake384_hash*)lua_touserdata(L, -1); + + memcpy(b->hash, a->hash, 8 * sizeof * b->hash); + memcpy(b->buffer, a->buffer, bs_2 * sizeof * b->buffer); + b->total = a->total; + b->bufflen = a->bufflen; + b->compressed = a->compressed; + b->total = a->total; + return 1; +} + +common_hash_init_update(blake384); + +int l_blake384_final(lua_State* L){ + struct blake384_hash* a = (struct blake384_hash*)lua_touserdata(L, 1); + + char digest[513] = {0}; + blake384_final(a, digest); + + lua_pushstring(L, digest); + return 1; +} + int l_blake384(lua_State* L){ + if(lua_gettop(L) == 0) return l_blake384_init(L); size_t len = 0; - char* a = (char*)luaL_checklstring(L, 1, &len); + uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); int argv = lua_gettop(L); char digest[513] = {0}; - memset(digest, 0, 513); - blake512(digest, a, len, b384); + blake384(a, len, digest); lua_pushstring(L, digest); return 1; diff --git a/src/hash/blake.h b/src/hash/blake.h index 05bdaab..a08a31f 100644 --- a/src/hash/blake.h +++ b/src/hash/blake.h @@ -27,6 +27,21 @@ enum blake512_v { }; int l_blake256(lua_State* L); +int l_blake256_init(lua_State* L); +int l_blake256_update(lua_State* L); +int l_blake256_final(lua_State* L); + int l_blake224(lua_State* L); +int l_blake224_init(lua_State* L); +int l_blake224_update(lua_State* L); +int l_blake224_final(lua_State* L); + int l_blake512(lua_State* L); -int l_blake384(lua_State* L); \ No newline at end of file +int l_blake512_init(lua_State* L); +int l_blake512_update(lua_State* L); +int l_blake512_final(lua_State* L); + +int l_blake384(lua_State* L); +int l_blake384_init(lua_State* L); +int l_blake384_update(lua_State* L); +int l_blake384_final(lua_State* L); \ No newline at end of file -- cgit v1.2.3