aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorame <[email protected]>2024-04-05 13:12:50 -0500
committerame <[email protected]>2024-04-05 13:12:50 -0500
commit7e6d58c143e1f3b1384ca2e798825ff3ed9469ec (patch)
tree7308903e7962fe2bfb0cf2461779e68d6da264ff
parented04c58675cfaf41b14958141f13c6a94b2d1a23 (diff)
fixes n stuff:3
-rw-r--r--docs/crypto.md8
-rw-r--r--src/crypto.h4
-rw-r--r--src/hash/md5.c23
-rw-r--r--src/hash/sha01.c14
-rw-r--r--src/hash/sha2-256.c377
-rw-r--r--src/hash/sha2-256.h27
-rw-r--r--tests/hash.lua50
7 files changed, 336 insertions, 167 deletions
diff --git a/docs/crypto.md b/docs/crypto.md
index 82d9e4e..d46a7ad 100644
--- a/docs/crypto.md
+++ b/docs/crypto.md
@@ -9,8 +9,6 @@ this is a pretty big problem meaning the input must be given at once, this is be
but bad for big files. because of this, i decided not to support inputs over 2^64 characters (which is an
insane amount anyways). i likely will go back and rewrite all of these to fix both of these issues.
-remaining to fix (inc. variants): 15
-
|name|out len|other args|extra|incremental|
|--|--|--|--|--|
| adler32 | 32 | nil | | y |
@@ -54,9 +52,9 @@ remaining to fix (inc. variants): 15
| murmur2_32 | 32 | *seed | | n |
| pjw | 32 | nil | | y |
| sdbm | 64 | nil | | y |
-| sha512 | 512 | nil | | todo |
-| sha384 | 384 | nil | | todo |
-| sha512_t | length of arg 2 | t (bit length) | bit length range is 0 < t <= 512 (this isnt checked, and it should accept any value) | todo |
+| sha512 | 512 | nil | | y |
+| sha384 | 384 | nil | | y |
+| sha512_t | length of arg 2 | t (bit length) | bit length range is 0 < t <= 512 (this isnt checked, and it should accept any value) | y |
| spookyhash128_v1 | 128 | *seed | | n |
| spookyhash128_v2 | 128 | *seed | | n |
| spookyhash64_v1 | 64 | *seed | | n |
diff --git a/src/crypto.h b/src/crypto.h
index 5a93cb6..cf7f760 100644
--- a/src/crypto.h
+++ b/src/crypto.h
@@ -105,7 +105,6 @@ static const luaL_Reg crypto_function_list [] = {
{"metrohash64_v1", l_metrohash64_v1}, {"metrohash64_v2", l_metrohash64_v2},
{"metrohash128_v1", l_metrohash128_v1}, {"metrohash128_v2", l_metrohash128_v2},
{"murmur1_32", l_murmur1_32}, {"murmur2_32", l_murmur2_32},
- {"sha512", l_sha512}, {"sha384", l_sha384}, {"sha512_t", l_sha512_t},
{"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},
@@ -135,6 +134,9 @@ static const luaL_Reg crypto_function_list [] = {
{"md5",l_md5}, {"md5_init",l_md5_init}, {"md5_update",l_md5_update}, {"md5_final",l_md5_final},
{"sha0",l_sha0}, {"sha0_init",l_sha0_init}, {"sha0_update",l_sha0_update}, {"sha0_final",l_sha0_final},
{"sha1",l_sha1}, {"sha1_init",l_sha1_init}, {"sha1_update",l_sha1_update}, {"sha1_final",l_sha1_final},
+ {"sha512", l_sha512}, {"sha512_init", l_sha512_init}, {"sha512_update", l_sha512_update}, {"sha512_final", l_sha512_final},
+ {"sha384", l_sha384}, {"sha384_init", l_sha384_init}, {"sha384_update", l_sha384_update}, {"sha384_final", l_sha384_final},
+ {"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},
{"uuencode",l_uuencode},
{"uudecode",l_uudecode},
diff --git a/src/hash/md5.c b/src/hash/md5.c
index f8dec6f..a3f0831 100644
--- a/src/hash/md5.c
+++ b/src/hash/md5.c
@@ -81,17 +81,17 @@ void md5_update(uint8_t* input, size_t len, struct md5_hash* hash){
for(; total_add >= bs;){
memcpy(hash->buffer + hash->bufflen, input + read, bs - hash->bufflen);
- total_add -= bs - hash->bufflen;
- hash->bufflen = 0;
+
+ total_add -= bs;
read += bs;
+ hash->bufflen = 0;
md5_round(hash);
}
memset(hash->buffer, 0, bs);
-
- if(read != total_add){
- memcpy(hash->buffer, input + read, total_add - read);
- hash->bufflen = total_add - read;
+ if(total_add != 0){
+ memcpy(hash->buffer, input + read, total_add);
+ hash->bufflen = total_add;
}
}
@@ -100,11 +100,13 @@ void md5_final(struct md5_hash* hash, char out_stream[64]){
if(hash->bufflen > 55) {
//too large, needs another buffer
+ memset(hash->buffer + hash->bufflen + 1, 0, 64 - hash->bufflen);
md5_round(hash);
+ memset(hash->buffer, 0, 64);
}
uint32_t lhhh = 8*hash->total;
- memcpy(hash->buffer + 56, &lhhh, 1);
+ memcpy(hash->buffer + 56, &lhhh, sizeof(lhhh));
md5_round(hash);
sprintf(out_stream,"%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x",
@@ -112,8 +114,6 @@ void md5_final(struct md5_hash* hash, char out_stream[64]){
((uint8_t*)&hash->b0)[0], ((uint8_t*)&hash->b0)[1], ((uint8_t*)&hash->b0)[2], ((uint8_t*)&hash->b0)[3],
((uint8_t*)&hash->c0)[0], ((uint8_t*)&hash->c0)[1], ((uint8_t*)&hash->c0)[2], ((uint8_t*)&hash->c0)[3],
((uint8_t*)&hash->d0)[0], ((uint8_t*)&hash->d0)[1], ((uint8_t*)&hash->d0)[2], ((uint8_t*)&hash->d0)[3]);
-
- free(hash->buffer);
}
common_hash_init_update(md5);
@@ -124,9 +124,8 @@ int l_md5_final(lua_State* L){
struct md5_hash* a = (struct md5_hash*)lua_touserdata(L, -1);
- char digest[128];
+ char digest[128] = {0};
md5_final(a, digest);
-
lua_pushstring(L, digest);
return 1;
}
@@ -142,7 +141,7 @@ int l_md5(lua_State* L){
size_t len = 0;
uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
- char digest[64];
+ char digest[128] = {0};
md5(a, len, digest);
lua_pushstring(L, digest);
diff --git a/src/hash/sha01.c b/src/hash/sha01.c
index 81a19d6..72932ff 100644
--- a/src/hash/sha01.c
+++ b/src/hash/sha01.c
@@ -88,7 +88,7 @@ void sha01_update(uint8_t* input, size_t len, struct sha01_hash* hash){
for(; total_add >= bs;){
memcpy(hash->buffer + hash->bufflen, input + read, bs - hash->bufflen);
- total_add -= bs - hash->bufflen;
+ total_add -= bs;
hash->bufflen = 0;
read += bs;
sha01_round(hash);
@@ -96,9 +96,9 @@ void sha01_update(uint8_t* input, size_t len, struct sha01_hash* hash){
memset(hash->buffer, 0, bs);
- if(read != total_add){
- memcpy(hash->buffer, input + read, total_add - read);
- hash->bufflen = total_add - read;
+ if(0 != total_add){
+ memcpy(hash->buffer, input + read, total_add);
+ hash->bufflen = total_add;
}
}
@@ -107,7 +107,9 @@ void sha01_final(struct sha01_hash* hash, char* out_stream){
if(hash->bufflen > 55) {
//too large, needs another buffer
+ memset(hash->buffer + hash->bufflen + 1, 0, 64 - hash->bufflen);
sha01_round(hash);
+ memset(hash->buffer, 0, 64);
}
size_t lhhh = 8*hash->total;
@@ -182,7 +184,7 @@ int l_sha1(lua_State* L){
char digest[160];
- sha1(a, len, digest);
+ sha1((uint8_t*)a, len, digest);
lua_pushstring(L, digest);
return 1;
@@ -195,7 +197,7 @@ int l_sha0(lua_State* L){
char digest[160];
- sha0(a, len, digest);
+ sha0((uint8_t*)a, len, digest);
lua_pushstring(L, digest);
return 1;
diff --git a/src/hash/sha2-256.c b/src/hash/sha2-256.c
index d06c4b4..972874f 100644
--- a/src/hash/sha2-256.c
+++ b/src/hash/sha2-256.c
@@ -5,6 +5,23 @@
#include <stdint.h>
#include <stdio.h>
+const uint64_t k[80] = {0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc, 0x3956c25bf348b538,
+ 0x59f111f1b605d019, 0x923f82a4af194f9b, 0xab1c5ed5da6d8118, 0xd807aa98a3030242, 0x12835b0145706fbe,
+ 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2, 0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235,
+ 0xc19bf174cf692694, 0xe49b69c19ef14ad2, 0xefbe4786384f25e3, 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65,
+ 0x2de92c6f592b0275, 0x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5, 0x983e5152ee66dfab,
+ 0xa831c66d2db43210, 0xb00327c898fb213f, 0xbf597fc7beef0ee4, 0xc6e00bf33da88fc2, 0xd5a79147930aa725,
+ 0x06ca6351e003826f, 0x142929670a0e6e70, 0x27b70a8546d22ffc, 0x2e1b21385c26c926, 0x4d2c6dfc5ac42aed,
+ 0x53380d139d95b3df, 0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6, 0x92722c851482353b,
+ 0xa2bfe8a14cf10364, 0xa81a664bbc423001, 0xc24b8b70d0f89791, 0xc76c51a30654be30, 0xd192e819d6ef5218,
+ 0xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8, 0x19a4c116b8d2d0c8, 0x1e376c085141ab53,
+ 0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8, 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb, 0x5b9cca4f7763e373,
+ 0x682e6ff3d6b2b8a3, 0x748f82ee5defb2fc, 0x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec,
+ 0x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915, 0xc67178f2e372532b, 0xca273eceea26619c,
+ 0xd186b8c721c0c207, 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178, 0x06f067aa72176fba, 0x0a637dc5a2c898a6,
+ 0x113f9804bef90dae, 0x1b710b35131c471b, 0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc,
+ 0x431d67c49c100d4c, 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817};
+
void endian_swap128(__uint128_t *x){
uint8_t *y = (uint8_t*)x;
for (size_t low = 0, high = sizeof(__uint128_t) - 1; high > low; low++, high--){
@@ -23,177 +40,281 @@ void endian_swap64(uint64_t *x){
}
}
-void sha512_gen(uint64_t* out_stream, uint8_t* input, size_t len, struct iv sha_iv){
- uint64_t h0 = sha_iv.h0, h1 = sha_iv.h1, h2 = sha_iv.h2, h3 = sha_iv.h3, h4 = sha_iv.h4, h5 = sha_iv.h5, h6 = sha_iv.h6, h7 = sha_iv.h7;
-
- const uint64_t k[80] = {0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc, 0x3956c25bf348b538,
- 0x59f111f1b605d019, 0x923f82a4af194f9b, 0xab1c5ed5da6d8118, 0xd807aa98a3030242, 0x12835b0145706fbe,
- 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2, 0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235,
- 0xc19bf174cf692694, 0xe49b69c19ef14ad2, 0xefbe4786384f25e3, 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65,
- 0x2de92c6f592b0275, 0x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5, 0x983e5152ee66dfab,
- 0xa831c66d2db43210, 0xb00327c898fb213f, 0xbf597fc7beef0ee4, 0xc6e00bf33da88fc2, 0xd5a79147930aa725,
- 0x06ca6351e003826f, 0x142929670a0e6e70, 0x27b70a8546d22ffc, 0x2e1b21385c26c926, 0x4d2c6dfc5ac42aed,
- 0x53380d139d95b3df, 0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6, 0x92722c851482353b,
- 0xa2bfe8a14cf10364, 0xa81a664bbc423001, 0xc24b8b70d0f89791, 0xc76c51a30654be30, 0xd192e819d6ef5218,
- 0xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8, 0x19a4c116b8d2d0c8, 0x1e376c085141ab53,
- 0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8, 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb, 0x5b9cca4f7763e373,
- 0x682e6ff3d6b2b8a3, 0x748f82ee5defb2fc, 0x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec,
- 0x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915, 0xc67178f2e372532b, 0xca273eceea26619c,
- 0xd186b8c721c0c207, 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178, 0x06f067aa72176fba, 0x0a637dc5a2c898a6,
- 0x113f9804bef90dae, 0x1b710b35131c471b, 0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc,
- 0x431d67c49c100d4c, 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817};
-
-
- size_t blen = len*8;
- int ulen = 0;
-
- size_t l = len * 8;
- size_t k2 = (896 - ( (l + 1) % 1024 )) % 1024;
-
- ulen = ((l + k2 + 1) / 8) + 16;
- uint64_t tlen = ulen/128;
- uint8_t* by = calloc(ulen, sizeof * by);
- for (size_t i = 0; i < len; i++)
- by[i] = input[i];
- by[len] = 0x80;
-
- //this part is very lame
- __uint128_t bigL = l;
- endian_swap128(&bigL);
- memcpy(&by[ulen - sizeof(__uint128_t)], &bigL, sizeof(__uint128_t));
-
- uint64_t *msg = ((uint64_t*)&by[0]);
- for (int i = 0; i < tlen * 16; i++)
+#define bs 128
+
+void sha512_round(struct sha512_hash* hash){
+ uint64_t *msg = ((uint64_t*)&hash->buffer[0]);
+ for(int i = 0; i < 16; i++)
endian_swap64(msg++);
- for(int z = 0; z < (int)tlen; z++){
- uint64_t* M = ((uint64_t*)(by));
- uint64_t W[80];
-
- //i dont really understand this 0->16 part
- uint64_t *m = &M[(z * 16)];
- for (int i = 0; i < 16; ++i){
- W[i] = *m;
- ++m;
- }
-
- for (int i = 16; i != 80; i++){
- W[i] = (rotr64(W[i - 2],19) ^ rotr64(W[i - 2], 61) ^ (W[i - 2] >> 6))
- + W[i - 7] + (rotr64(W[i - 15],1) ^ rotr64(W[i - 15],8) ^ (W[i - 15] >> 7)) + W[i - 16];
- }
-
- uint64_t a = h0;
- uint64_t b = h1;
- uint64_t c = h2;
- uint64_t d = h3;
- uint64_t e = h4;
- uint64_t f = h5;
- uint64_t g = h6;
- uint64_t h = h7;
+ uint64_t* M = ((uint64_t*)(hash->buffer));
+ uint64_t W[80];
+
+ //i dont really understand this 0->16 part
+ int z = 0;
+ uint64_t *m = &M[(z * 16)];
+ for(int i = 0; i < 16; ++i){
+ W[i] = *m;
+ m++;
+ }
+
+ for(int i = 16; i != 80; i++){
+ W[i] = (rotr64(W[i - 2],19) ^ rotr64(W[i - 2], 61) ^ (W[i - 2] >> 6))
+ + W[i - 7] + (rotr64(W[i - 15],1) ^ rotr64(W[i - 15],8) ^ (W[i - 15] >> 7)) + W[i - 16];
+ }
+
+ uint64_t a = hash->h0;
+ uint64_t b = hash->h1;
+ uint64_t c = hash->h2;
+ uint64_t d = hash->h3;
+ uint64_t e = hash->h4;
+ uint64_t f = hash->h5;
+ uint64_t g = hash->h6;
+ uint64_t h = hash->h7;
- for(int i = 0; i != 80; i++){
- uint64_t S1 = rotr64(e, 14) ^ rotr64(e, 18) ^ rotr64(e, 41);
- uint64_t ch = (e & f) ^ ((~e) & g);
- uint64_t temp1 = h + S1 + ch + k[i] + W[i];
+ for(int i = 0; i != 80; i++){
+ uint64_t S1 = rotr64(e, 14) ^ rotr64(e, 18) ^ rotr64(e, 41);
+ uint64_t ch = (e & f) ^ ((~e) & g);
+ uint64_t temp1 = h + S1 + ch + k[i] + W[i];
- uint64_t S0 = rotr64(a, 28) ^ rotr64(a, 34) ^ rotr64(a, 39);
- uint64_t maj = (a & b) ^ (a & c) ^ (b & c);
- uint64_t temp2 = S0 + maj;
+ uint64_t S0 = rotr64(a, 28) ^ rotr64(a, 34) ^ rotr64(a, 39);
+ uint64_t maj = (a & b) ^ (a & c) ^ (b & c);
+ uint64_t temp2 = S0 + maj;
- h = g;
- g = f;
- f = e;
- e = d + temp1;
- d = c;
- c = b;
- b = a;
- a = temp1 + temp2;
- }
-
- h0 += a;
- h1 += b;
- h2 += c;
- h3 += d;
- h4 += e;
- h5 += f;
- h6 += g;
- h7 += h;
-
+ h = g;
+ g = f;
+ f = e;
+ e = d + temp1;
+ d = c;
+ c = b;
+ b = a;
+ a = temp1 + temp2;
}
- out_stream[0] = h0;
- out_stream[1] = h1;
- out_stream[2] = h2;
- out_stream[3] = h3;
- out_stream[4] = h4;
- out_stream[5] = h5;
- out_stream[6] = h6;
- out_stream[7] = h7;
- free(by);
+
+ hash->h0 += a;
+ hash->h1 += b;
+ hash->h2 += c;
+ hash->h3 += d;
+ hash->h4 += e;
+ hash->h5 += f;
+ hash->h6 += g;
+ hash->h7 += h;
+}
+
+struct sha512_hash sha512_t_init(struct iv sha_iv){
+ struct sha512_hash a = {.h0 = sha_iv.h0, .h1 = sha_iv.h1, .h2 = sha_iv.h2, .h3 = sha_iv.h3, .h4 = sha_iv.h4, .h5 = sha_iv.h5, .h6 = sha_iv.h6, .h7 = sha_iv.h7,
+ .total = 0, .bufflen = 0};
+ a.buffer = calloc(sizeof * a.buffer, bs);
+ return a;
+}
+
+struct sha512_hash sha512_init(){
+ return sha512_t_init(sha512_iv);
+}
+
+struct sha512_hash sha384_init(){
+ return sha512_t_init(sha384_iv);
+}
+
+void sha512_update(uint8_t* input, size_t len, struct sha512_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(; total_add >= bs;){
+ memcpy(hash->buffer + hash->bufflen, input + read, bs - hash->bufflen);
+ total_add -= bs;
+ hash->bufflen = 0;
+ read += bs;
+ sha512_round(hash);
+ }
+
+ memset(hash->buffer, 0, bs);
+
+ if(0 != total_add){
+ memcpy(hash->buffer, input + read, total_add);
+ hash->bufflen = total_add;
+ }
+}
+
+void _sha512_t_final(struct sha512_hash* hash){
+ hash->buffer[hash->bufflen] = 0x80;
+
+ if(hash->bufflen > bs - 16) {
+ //too large, needs another buffer
+ memset(hash->buffer + hash->bufflen + 1, 0, bs - hash->bufflen);
+ sha512_round(hash);
+ memset(hash->buffer, 0, bs);
+ }
+
+ __uint128_t bigL = hash->total*8;
+ endian_swap128(&bigL);
+ memcpy(&hash->buffer[128 - sizeof(__uint128_t)], &bigL, sizeof(__uint128_t));
+
+ sha512_round(hash);
+}
+
+void sha512_final(struct sha512_hash* hash, char* out_stream){
+ _sha512_t_final(hash);
+
+ sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h0);
+ sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h1);
+ sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h2);
+ sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h3);
+ sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h4);
+ sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h5);
+ sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h6);
+ sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h7);
+
+ free(hash->buffer);
+}
+
+void sha384_final(struct sha512_hash* hash, char* out_stream){
+ _sha512_t_final(hash);
+
+ sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h0);
+ sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h1);
+ sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h2);
+ sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h3);
+ sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h4);
+ sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h5);
+
+ free(hash->buffer);
+}
+
+void sha512(uint8_t* in, size_t len, char* out){
+ struct sha512_hash a = sha512_init();
+ sha512_update(in, len, &a);
+ sha512_final(&a, out);
+}
+
+void sha384(uint8_t* in, size_t len, char* out){
+ struct sha512_hash a = sha384_init();
+ sha384_update(in, len, &a);
+ sha384_final(&a, out);
}
+void sha512_t(uint8_t* in, size_t len, int t, char* out){
+ struct sha512_hash a = sha512_t_init(sha_iv_gen(t));
+ sha512_update(in, len, &a);
+ sha512_final(&a, out);
+ out[t/4] = '\0';
+}
struct iv sha_iv_gen(int i){
struct iv oh = {.h0 = sha512_iv.h0 ^ 0xa5a5a5a5a5a5a5a5, .h1 = sha512_iv.h1 ^ 0xa5a5a5a5a5a5a5a5, .h2 = sha512_iv.h2 ^ 0xa5a5a5a5a5a5a5a5,
.h3 = sha512_iv.h3 ^ 0xa5a5a5a5a5a5a5a5, .h4 = sha512_iv.h4 ^ 0xa5a5a5a5a5a5a5a5, .h5 = sha512_iv.h5 ^ 0xa5a5a5a5a5a5a5a5,
.h6 = sha512_iv.h6 ^ 0xa5a5a5a5a5a5a5a5, .h7 = sha512_iv.h7 ^ 0xa5a5a5a5a5a5a5a5};
- uint64_t nh[8] = {0};
+ uint8_t nh[512] = {0};
uint8_t in[12];
sprintf((char*)in, "SHA-512/%i",i);
- sha512_gen(nh, in, strlen((char*)in), oh);
- return (struct iv){.h0 = nh[0], .h1 = nh[1], .h2 = nh[2], .h3 = nh[3], .h4 = nh[4], .h5 = nh[5], .h6 = nh[6], .h7 = nh[7]};
+ struct sha512_hash a = sha512_t_init(oh);
+ sha512_update(in, strlen((char*)in), &a);
+ sha512_final(&a, nh);
+ return (struct iv){.h0 = a.h0, .h1 = a.h1, .h2 = a.h2, .h3 = a.h3, .h4 = a.h4, .h5 = a.h5, .h6 = a.h6, .h7 = a.h7};
}
-void sha2_512_t(uint8_t* out, uint8_t* in, size_t len, int t){
- if(t%8!=0) return;
- uint64_t out_stream[8] = {0};
- sha512_gen(out_stream, in, len, sha_iv_gen(t));
- for(int i = 0; i != 8; i++) sprintf((char*)out, "%s%016llx", out, out_stream[i]);
- out[t/4] = '\0';
+common_hash_init_update(sha512);
+
+int l_sha512_final(lua_State* L){
+ lua_pushstring(L, "ud");
+ lua_gettable(L, 1);
+
+ struct sha512_hash* a = (struct sha512_hash*)lua_touserdata(L, -1);
+
+ char digest[512] = {0};
+ sha512_final(a, digest);
+
+ lua_pushstring(L, digest);
+ return 1;
}
-void sha2_512(uint8_t* out, uint8_t* in, size_t len){
- uint64_t out_stream[8] = {0};
- sha512_gen(out_stream, in, len, sha512_iv);
- for(int i = 0; i != 8; i++) sprintf((char*)out, "%s%016llx", out, out_stream[i]);
+common_hash_init_update(sha384);
+
+int l_sha384_final(lua_State* L){
+ lua_pushstring(L, "ud");
+ lua_gettable(L, 1);
+
+ struct sha512_hash* a = (struct sha512_hash*)lua_touserdata(L, -1);
+
+ char digest[384] = {0};
+ sha384_final(a, digest);
+
+ lua_pushstring(L, digest);
+ return 1;
}
-void sha2_384(uint8_t* out, uint8_t* in, size_t len){
- uint64_t out_stream[8] = {0};
- sha512_gen(out_stream, in, len, sha384_iv);
- for(int i = 0; i != 6; i++) sprintf((char*)out, "%s%016llx", out, out_stream[i]);
+int l_sha512_t_init(lua_State* L){
+ int tt = luaL_checkinteger(L, 1);
+ lua_newtable(L);
+ int t = lua_gettop(L);
+
+ struct sha512_hash* a = (struct sha512_hash*)lua_newuserdata(L, sizeof * a);\
+ int ud = lua_gettop(L);
+ *a = sha512_t_init(sha_iv_gen(tt));
+ a->t = tt;
+
+ luaI_tsetv(L, t, "ud", ud);
+ luaI_tsetcf(L, t, "update", l_sha512_t_update);
+ luaI_tsetcf(L, t, "final", l_sha512_t_final);
+
+ lua_pushvalue(L, t);
+ return 1;
}
-int l_sha512(lua_State* L){
- size_t len = 0;
- uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
+lua_common_hash_update(sha512, sha512_t);
+
+int l_sha512_t_final(lua_State* L){
+ lua_pushstring(L, "ud");
+ lua_gettable(L, 1);
+
+ struct sha512_hash* a = (struct sha512_hash*)lua_touserdata(L, -1);
char digest[512] = {0};
+ sha512_final(a, digest);
+ digest[a->t/4] = '\0';
- sha2_512((uint8_t*)digest, a, len);
lua_pushstring(L, digest);
return 1;
}
+int l_sha512(lua_State* L){
+ if(lua_gettop(L) == 0) return l_sha512_init(L);
+ size_t len = 0;
+ uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
+ char digest[512] = {0};
+
+ sha512(a, len, digest);
+ lua_pushstring(L, digest);
+ return 1;
+}
+
int l_sha384(lua_State* L){
- size_t len = 0;
- uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
+ if(lua_gettop(L) == 0) return l_sha384_init(L);
+ size_t len = 0;
+ uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
- char digest[384] = {0};
+ char digest[384] = {0};
- sha2_384((uint8_t*)digest, a, len);
- lua_pushstring(L, digest);
- return 1;
+ sha384(a, len, digest);
+ lua_pushstring(L, digest);
+ return 1;
}
int l_sha512_t(lua_State* L){
+ if(lua_gettop(L) == 1) return l_sha512_t_init(L);
size_t len = 0;
uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
uint64_t t = luaL_checkinteger(L, 2);
char digest[512] = {0};
- sha2_512_t((uint8_t*)digest, a, t, len);
+ sha512_t(a, len, t, digest);
lua_pushstring(L, digest);
return 1;
}
diff --git a/src/hash/sha2-256.h b/src/hash/sha2-256.h
index a261819..524693d 100644
--- a/src/hash/sha2-256.h
+++ b/src/hash/sha2-256.h
@@ -11,11 +11,32 @@ static const struct iv sha512_iv = {.h0 = 0x6a09e667f3bcc908, .h1 = 0xbb67ae8584
static const struct iv sha384_iv = {.h0 = 0xcbbb9d5dc1059ed8, .h1 = 0x629a292a367cd507, .h2 = 0x9159015a3070dd17, .h3 = 0x152fecd8f70e5939,
.h4 = 0x67332667ffc00b31, .h5 = 0x8eb44a8768581511, .h6 = 0xdb0c2e0d64f98fa7, .h7 = 0x47b5481dbefa4fa4};
+#define sha384_update sha512_update
+#define sha384_hash sha512_hash
+
+struct sha512_hash {
+ uint8_t* buffer;
+ size_t bufflen;
+ uint64_t total, h0, h1, h2, h3, h4, h5, h6, h7;
+ uint16_t t;
+};
+
struct iv sha_iv_gen(int i);
-void sha2_512_t(uint8_t* out, uint8_t* in, size_t len, int t);
-void sha2_512(uint8_t* out, uint8_t* in, size_t len);
-void sha2_384(uint8_t* out, uint8_t* in, size_t len);
+void sha512_t(uint8_t* in, size_t len, int t, char* out);
+void sha512(uint8_t* in, size_t len, char* out);
+void sha384(uint8_t* in, size_t len, char* out);
int l_sha512(lua_State*);
+int l_sha512_init(lua_State*);
+int l_sha512_update(lua_State*);
+int l_sha512_final(lua_State*);
+
int l_sha384(lua_State*);
+int l_sha384_init(lua_State*);
+int l_sha384_update(lua_State*);
+int l_sha384_final(lua_State*);
+
int l_sha512_t(lua_State*);
+int l_sha512_t_init(lua_State*);
+int l_sha512_t_update(lua_State*);
+int l_sha512_t_final(lua_State*);
diff --git a/tests/hash.lua b/tests/hash.lua
index c994312..7b01bd0 100644
--- a/tests/hash.lua
+++ b/tests/hash.lua
@@ -20,41 +20,53 @@ function test(name,b,exp,oargs)
end
if(llib.crypto[name.."_init"] ~= nil) then
- hash2 = llib.crypto[name]():update(b):final()
+ if(oargs == nil) then
+ hash2 = llib.crypto[name]():update(b):final()
+ else
+ hash2 = llib.crypto[name](table.unpack(oargs)):update(b):final()
+ end
- hash3 = llib.crypto[name]()
+ if(oargs == nil) then
+ hash3 = llib.crypto[name]()
+ else
+ hash3 = llib.crypto[name](table.unpack(oargs))
+ end
b:gsub(".", function(c) hash3:update(c) end)
hash3 = hash3:final()
- hash4 = llib.crypto[name.."_init"]()
+ if(oargs == nil) then
+ hash4 = llib.crypto[name.."_init"]()
+ else
+ hash4 = llib.crypto[name.."_init"](table.unpack(oargs))
+ end
llib.crypto[name.."_update"](hash4, b)
hash4 = llib.crypto[name.."_final"](hash4)
- if(hash2 ~= hash) then
+ if(hash2 ~= exp) then
fail = true
functions_failed=functions_failed + 1
- llib.io.error(name.." alt method not working, got:\n\t"..hash2.." other was:\n\t"..hash)
+ llib.io.error(name.." alt method not working, got:\n\t"..hash2.." other was:\n\t"..exp)
else
functions_working=functions_working + 1
- llib.io.log(name.." alt method working "..hash2.." == "..hash)
+ llib.io.log(name.." alt method working "..hash2.." == "..exp)
end
- if(hash4 ~= hash) then
+ if(hash4 ~= exp) then
fail = true
functions_failed=functions_failed + 1
- llib.io.error(name.." alt method 2 not working, got:\n\t"..hash4.." other was:\n\t"..hash)
+ llib.io.error(name.." alt method 2 not working, got:\n\t"..hash4.." other was:\n\t"..exp)
else
functions_working=functions_working + 1
- llib.io.log(name.." alt method 2 working "..hash4.." == "..hash)
+ llib.io.log(name.." alt method 2 working "..hash4.." == "..exp)
end
- if(hash3 ~= hash) then
+ if(hash3 ~= exp) then
fail = true
functions_failed=functions_failed + 1
- llib.io.error(name.." alt char-b-char method not working, got:\n\t"..hash3.." other was:\n\t"..hash)
+ llib.io.error(name.." alt char-b-char method not working, got:\n\t"..hash3.." other was:\n\t"..exp)
else
functions_working=functions_working + 1
- llib.io.log(name.." alt char-b-char method working "..hash3.." == "..hash)
+ llib.io.log(name.." alt char-b-char method working "..hash3.." == "..exp)
end
end
@@ -112,6 +124,7 @@ test("pjw","meow","00073c67")
test("sdbm","meow","006d50f201921b00")
test("sha512","meow","e88348269bad036160f0d9558b7c5de68163b50e1a6ce46e85ee64692eba074529a4a2b48db4d5c36496e845001e13e6d07c585eacd564defcbf719ec9033e17");
test("sha384","meow","f0bb848a382b5ed5e2f49a46252f6b738c933dc20bb29dc4a5d312e310b395c4fa07f30a8a7380b4a5d367445e0ea8cb")
+test("sha512_t", "meow", "ad5e403e0d74532187f4e1665c7e705ab5eb3c2fe07ae73a3ff998b2", {224})
test("fasthash64","meow","7b9e494cf11ee113")
test("fasthash32","meow","758097c7")
test("metrohash64_v1", "meow", "7435945e80261ed1")
@@ -129,5 +142,18 @@ test("blake224", "meow", "0a099d692027cfbe69d2424a5b2520a7398fa4945e0878f6c541f5
test("blake512", "meow", "09d5abe166c4ad855d4527d0be21df2b1a01c3d7c5637572561ebc247908fd7db30bf342391dd0a834fd35f391807480fb31e8a7ee3b1098e46d996d5601948f")
test("blake384", "meow", "7edb2ff04616f5551a789217029496c3c8601ac7aba2d40d7fcd1ec85fc63f37514e5884f2ebc807b11854247620446c")
+test("md5", "meow meow mrrp meow meow!! mrrp ", "900df0c6e7df499615d588b6e83caf10")
+test("md5", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@", "900bc8ffba6262b4d1dd09b2518c23bd")
+
+test("sha1", "meow meow mrrp meow meow!! mrrp ", "3ea97f1835830d4be6ec86f5382f66f3e0d2f973")
+test("sha1", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@", "d635d131ca7fd03c4c5d0cb5ca9707c3f5494427")
+
+test("sha512", "meow meow mrrp meow meow!! mrrp meow meow mrrp meow meow!! mrrp ", "faecf93fc69a194c3433f179f4d643bfbc30e03cf97ec0bae097b2bfe9f58bf8c96978f551635905862ffcad1c1e92588788dfad99cfad951770226110fbd2fb")
+test("sha512", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@", "8ea73a99c1d773605ac958996ca30b1214f475851d0dc897547a68a3da0cbdfd449344352e8f3c4c96477e5c4d6aa7e54e86f566da0f299899e56a445ccbf5c1")
+--test("sha1", "meow meow mrrp meow meow!! mrrp ", "3ea97f1835830d4be6ec86f5382f66f3e0d2f973")
+--test("sha1", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "014842d480b571495a4a0363793f7367")
+--test("sha512", "meow meow mrrp meow meow!! mrrp ", "eda9416e9bf4b2557279140cf03331d26f30b8294b0296112ec8094811df296b17393b8fb42cd6a84c52a9dcb7b8b4cb699a797aae8726ba306c3b3b79fba9c1")
+--test("sha512", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "014842d480b571495a4a0363793f7367")
+--print(llib.crypto.md5("meow meow mrrp meow meow!! mrrp "))
print(hashes_working.."/"..hashes_failed.." hashes working/failed")
print(functions_working.."/"..functions_failed.." functions working/failed") \ No newline at end of file