aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorame <[email protected]>2024-04-15 10:38:52 -0500
committerame <[email protected]>2024-04-15 10:38:52 -0500
commit3fe8ac768632d8f61da0052b1fd34c04123747b2 (patch)
tree8f94d2d285a9e92d41e59ef7a4272180ac632521
parentcff21457076875546664bb7fc5b1a2a79d9babaa (diff)
blake2b (blake2 done:3)
-rw-r--r--docs/crypto.md2
-rw-r--r--src/crypto.h4
-rw-r--r--src/hash/blake2.c204
-rw-r--r--src/hash/blake2.h7
4 files changed, 177 insertions, 40 deletions
diff --git a/docs/crypto.md b/docs/crypto.md
index 4286f19..f82a006 100644
--- a/docs/crypto.md
+++ b/docs/crypto.md
@@ -61,7 +61,7 @@ insane amount anyways). i likely will go back and rewrite all of these to fix bo
| spookyhash64_v2 | 64 | *seed | | n |
| spookyhash32_v1 | 32 | *seed | | n |
| spookyhash32_v2 | 32 | *seed | | n |
-| blake2b | length of arg 2 * 8 | *output len (default is 64), *key | | todo |
+| blake2b | length of arg 2 * 8 | *output len (default is 64), *key | | y |
| blake2s | length of arg 2 * 8 | *output len (default is 32), *key | | y |
| blake256 | 256 | nil | | y |
| blake224 | 224 | nil | | y |
diff --git a/src/crypto.h b/src/crypto.h
index 3fb0e58..410b282 100644
--- a/src/crypto.h
+++ b/src/crypto.h
@@ -123,8 +123,6 @@ 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},
- {"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},
@@ -159,6 +157,8 @@ static const luaL_Reg crypto_function_list [] = {
{"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},
{"blake2s", l_blake2s}, {"blake2s_init", l_blake2s_init}, {"blake2s_update", l_blake2s_update}, {"blake2s_final", l_blake2s_final},
+ {"blake2b", l_blake2b}, {"blake2b_init", l_blake2b_init}, {"blake2b_update", l_blake2b_update}, {"blake2b_final", l_blake2b_final},
+
{"uuencode",l_uuencode},
{"uudecode",l_uudecode},
diff --git a/src/hash/blake2.c b/src/hash/blake2.c
index be1cb51..61c79ca 100644
--- a/src/hash/blake2.c
+++ b/src/hash/blake2.c
@@ -123,50 +123,128 @@ void compress2s(uint32_t* hash, uint8_t* inp, uint32_t compressed, int final){
}
}
-void blake2b(char* inp, int inp_len, char* key, int key_len, int dig_len, char* buffer){
- uint64_t hash[8];
-
- uint64_t iv0 = hash[0] = sha512_iv.h0;
- uint64_t iv1 = hash[1] = sha512_iv.h1;
- uint64_t iv2 = hash[2] = sha512_iv.h2;
- uint64_t iv3 = hash[3] = sha512_iv.h3;
- uint64_t iv4 = hash[4] = sha512_iv.h4;
- uint64_t iv5 = hash[5] = sha512_iv.h5;
- uint64_t iv6 = hash[6] = sha512_iv.h6;
- uint64_t iv7 = hash[7] = sha512_iv.h7;
+struct blake2b_hash {
+ uint8_t *buffer, *key;
+ size_t bufflen, keylen;
+ uint64_t total, *hash;
+ uint64_t compressed, digest_len;
+};
- uint64_t alen = inter(inp_len, 128) + 128;
+void blake2b_round(struct blake2b_hash* hash, int last){
+ compress2b(hash->hash, hash->buffer, hash->compressed, last);
+}
-
- //add padding
- char* padded = calloc(alen + (128 * (key_len > 0)), sizeof * padded);
+#define bs_2 128
+struct blake2b_hash blake2b_init(char* key, int key_len, int digest_len){
+ struct blake2b_hash a = {.bufflen = key_len, .total = 0, .compressed = 0, .keylen = key_len, .digest_len = digest_len};
+ a.buffer = calloc(sizeof * a.buffer, bs_2);
+ a.hash = calloc(sizeof * a.hash, 8);
+ a.key = calloc(sizeof* a.key, key_len);
+ memcpy(a.key, key, key_len);
+ memcpy(a.buffer, key, key_len);
- if(key_len > 0){
- memcpy(padded, key, key_len);
- inp_len += 128;
+ a.hash[0] = sha512_iv.h0;
+ a.hash[0] ^= digest_len;
+ a.hash[0] ^= key_len << 8;
+ a.hash[0] ^= 0x01010000;
+ a.hash[1] = sha512_iv.h1;
+ a.hash[2] = sha512_iv.h2;
+ a.hash[3] = sha512_iv.h3;
+ a.hash[4] = sha512_iv.h4;
+ a.hash[5] = sha512_iv.h5;
+ a.hash[6] = sha512_iv.h6;
+ a.hash[7] = sha512_iv.h7;
+ if(key_len != 0){
+ a.compressed = 128;
+ blake2b_round(&a, 0);
+ memset(a.buffer, 0, bs_2);
+ a.bufflen = 0;
}
+ return a;
+}
- memcpy(padded + (128 * (key_len > 0)), inp, inp_len - (128 * (key_len > 0)));
+struct blake2b_hash blake2b_init_l(lua_State* L, char* key, int key_len, int digest_len){
+ struct blake2b_hash a = {.bufflen = key_len, .total = 0, .compressed = 0, .keylen = key_len, .digest_len = digest_len};
+ a.buffer = lua_newuserdata(L, sizeof * a.buffer * bs_2);
+ a.hash = lua_newuserdata(L, sizeof * a.hash * 8);
+ a.key = lua_newuserdata(L, sizeof* a.key * key_len);
+ memset(a.buffer, 0, bs_2);
+ memcpy(a.key, key, key_len);
+ memcpy(a.buffer, key, key_len);
+
+ a.hash[0] = sha512_iv.h0;
+ a.hash[0] ^= digest_len;
+ a.hash[0] ^= key_len << 8;
+ a.hash[0] ^= 0x01010000;
+ a.hash[1] = sha512_iv.h1;
+ a.hash[2] = sha512_iv.h2;
+ a.hash[3] = sha512_iv.h3;
+ a.hash[4] = sha512_iv.h4;
+ a.hash[5] = sha512_iv.h5;
+ a.hash[6] = sha512_iv.h6;
+ a.hash[7] = sha512_iv.h7;
+ if(key_len != 0){
+ a.compressed = 128;
+ blake2b_round(&a, 0);
+ memset(a.buffer, 0, bs_2);
+ a.bufflen = 0;
+ }
+ return a;
+}
- hash[0] ^= dig_len;
- hash[0] ^= key_len << 8;
- hash[0] ^= 0x01010000;
+void blake2b_update(uint8_t* input, size_t len, struct blake2b_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;
+ }
- uint64_t compressed = 0, bytes_remaining = inp_len;
+ 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 += 64;
+ blake2b_round(hash, 0);
+ }
- int i = 0;
- for(;bytes_remaining > 128; i += 2){
- bytes_remaining -= 128;
- compressed += 128;
+ memset(hash->buffer, 0, bs_2);
- compress2b(hash, (uint8_t*)padded, compressed, 0);
- padded += 128;
+ if(0 != total_add){
+ memcpy(hash->buffer, input + read, total_add);
+ hash->bufflen = total_add;
}
+}
- compressed += bytes_remaining;
+void blake2b_final(struct blake2b_hash* hash, char* out_stream){
+ uint8_t old[bs_2];
+ uint32_t hashh[8];
+ struct blake2b_hash old_hash;
+ memcpy(&old_hash, hash, sizeof * hash);
+ memcpy(old, hash->buffer, bs_2);
+ memcpy(hashh, hash->hash, 8 * sizeof * hashh);
+
+ hash->compressed += hash->bufflen;
- compress2b(hash, (uint8_t*)padded, compressed, 1);
- for(int i = 0; i != dig_len; i++)sprintf(buffer, "%s%02x", buffer, (((uint8_t*)hash)[i]));
+ blake2b_round(hash, 1);
+
+ for(int i = 0; i != hash->digest_len; i++)sprintf(out_stream, "%s%02x", out_stream, (((uint8_t*)hash->hash)[i]));
+
+ memcpy(hash, &old_hash, sizeof * hash);
+ memcpy(hash->buffer, old, bs_2);
+ memcpy(hash->hash, hashh, 8 * sizeof * hashh);
+}
+
+void blake2b(uint8_t* inp, int len, char* key, int key_len, int dig_len, char* out){
+ struct blake2b_hash aa = blake2b_init(key, key_len, dig_len);
+ blake2b_update(inp, len, &aa);
+ blake2b_final(&aa, out);
+ free(aa.buffer);
+ free(aa.hash);
+ free(aa.key);
}
struct blake2s_hash {
@@ -301,18 +379,74 @@ void blake2s(uint8_t* inp, int len, char* key, int key_len, int dig_len, char* o
free(aa.key);
}
+int l_blake2b_clone(lua_State* L){
+
+ struct blake2b_hash* a = (struct blake2b_hash*)lua_touserdata(L, -1);
+
+ lua_pushinteger(L, a->digest_len);
+ lua_pushlstring(L, (char*)a->key, a->keylen);
+ l_blake2b_init(L);
+
+ struct blake2b_hash* b = (struct blake2b_hash*)lua_touserdata(L, -1);
+
+ memcpy(b->hash, a->hash, 8 * sizeof * b->hash);
+ memcpy(b->buffer, a->buffer, bs * sizeof * b->buffer);
+
+ b->keylen = a->keylen;
+ b->digest_len = a->digest_len;
+ b->keylen = a->keylen;
+ b->total = a->total;
+ b->bufflen = a->bufflen;
+ b->compressed = a->compressed;
+ b->total = a->total;
+ return 1;
+}
+
+lua_common_hash_meta(blake2b);
+
+int l_blake2b_init(lua_State* L){
+ char* key = NULL;
+ size_t keylen = 0, outlen = 64;
+
+ if(lua_gettop(L) > 1){
+ key = (char*)luaL_checklstring(L, -1, &keylen);
+ outlen = luaL_checkinteger(L, -2);
+ } else if(lua_gettop(L) > 0) outlen = luaL_checkinteger(L, -1);
+
+ struct blake2b_hash* a = (struct blake2b_hash*)lua_newuserdata(L, sizeof * a);
+ int ud = lua_gettop(L);
+ *a = blake2b_init_l(L, key, keylen, outlen);
+ lua_common_hash_meta_def(blake2b);
+ lua_pushvalue(L, ud);
+ return 1;
+}
+
+lua_common_hash_update(blake2b, blake2b);
+
+int l_blake2b_final(lua_State* L){
+ struct blake2b_hash* a = (struct blake2b_hash*)lua_touserdata(L, -1);
+
+ char digest[a->digest_len * 8];
+ memset(digest, 0, a->digest_len * 8);
+ blake2b_final(a, digest);
+
+ lua_pushstring(L, digest);
+ return 1;
+}
+
int l_blake2b(lua_State* L){
+ if(lua_gettop(L) == 0 || lua_type(L, 1) == LUA_TNUMBER) return l_blake2b_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);
- uint64_t out_len = 64;
+ uint32_t out_len = 64;
if(argv > 1) out_len = luaL_checkinteger(L, 2);
char* key = NULL;
size_t key_len = 0;
if(argv > 2) key = (char*)luaL_checklstring(L, 3, &key_len);
-
+
char digest[out_len * 8];
memset(digest, 0, out_len * 8);
diff --git a/src/hash/blake2.h b/src/hash/blake2.h
index 91c69f6..0b221de 100644
--- a/src/hash/blake2.h
+++ b/src/hash/blake2.h
@@ -15,11 +15,14 @@ static const uint8_t blake2b_sigma[10][16] = {
};
//void blake2s(char*, int, char*, int, int, char*);
-void blake2b(char*, int, char*, int, int, char*);
+//void blake2b(char*, int, char*, int, int, char*);
int l_blake2s(lua_State*);
int l_blake2s_init(lua_State*);
int l_blake2s_update(lua_State*);
int l_blake2s_final(lua_State*);
-int l_blake2b(lua_State*); \ No newline at end of file
+int l_blake2b(lua_State*);
+int l_blake2b_init(lua_State*);
+int l_blake2b_update(lua_State*);
+int l_blake2b_final(lua_State*); \ No newline at end of file