From 81b85b557c308ff743b7b83f2b823e508eef54eb Mon Sep 17 00:00:00 2001 From: ame Date: Wed, 1 May 2024 23:52:54 -0500 Subject: fix some hash ub --- docs/thread.md | 16 ++++++++-------- readme.md | 2 ++ src/hash/blake.c | 12 ++++++------ src/hash/blake2.c | 4 ++-- src/hash/sha2-256.c | 14 +++++++------- 5 files changed, 25 insertions(+), 23 deletions(-) diff --git a/docs/thread.md b/docs/thread.md index 0d96809..6a3b19d 100644 --- a/docs/thread.md +++ b/docs/thread.md @@ -14,11 +14,11 @@ local thread = llib.thread(function(info) end) ``` -### thread function parameters +### thread function parameters ** as used with "info" above -#### info:send() +#### info:send() ** 'takes "any" value @@ -29,13 +29,13 @@ info:send(5) info:send("hello") ``` -#### info:pause() +#### info:pause() ** pauses the thread (must be resumed from outside) -### thread return object +### thread return object ** -#### thread:await() +#### thread:await() ** 'optional timeout @@ -45,7 +45,7 @@ waits for the thread to return, and returns whatever it returned, if timeout is thread:await() -- value of N (above) ``` -#### thread:next() +#### thread:next() ** gets the most oldest value sent using info:send() and pops it @@ -55,10 +55,10 @@ thread:next() -- 5 thread:next() -- "hello" ``` -#### thread:kill() +#### thread:kill() ** kills the thread -#### thread:pause() thread:resume() +#### thread:pause() thread:resume() ** stops or continues the thread diff --git a/readme.md b/readme.md index fcfca65..12bbe63 100644 --- a/readme.md +++ b/readme.md @@ -32,6 +32,8 @@ todo: * threads +* encode tests (and fix sprintf ub) + ---- # credits diff --git a/src/hash/blake.c b/src/hash/blake.c index c6d73ad..76b6680 100644 --- a/src/hash/blake.c +++ b/src/hash/blake.c @@ -215,7 +215,7 @@ void blake256_final(struct blake256_hash* hash, char* out_stream){ _blake256_final(hash, out_stream); for(int i = 0; i != 8; i++){ - sprintf(out_stream, "%s%08x",out_stream,(hash->hash)[i]); + sprintf(out_stream + i * 8, "%08x",(hash->hash)[i]); } memcpy(hash, &old_hash, sizeof * hash); @@ -234,7 +234,7 @@ void blake224_final(struct blake256_hash* hash, char* out_stream){ _blake256_final(hash, out_stream); for(int i = 0; i != 7; i++){ - sprintf(out_stream, "%s%08x",out_stream,(hash->hash)[i]); + sprintf(out_stream + i * 8, "%08x",(hash->hash)[i]); } memcpy(hash, &old_hash, sizeof * hash); @@ -427,8 +427,8 @@ void blake512_final(struct blake512_hash* hash, char* out_stream){ _blake512_final(hash, out_stream); for(int i = 0; i != 8; i++){ - sprintf(out_stream, "%s%016llx",out_stream, (hash->hash)[i]); - } + sprintf(out_stream + 16 * i, "%016llx", (hash->hash)[i]); + } memcpy(hash, &old_hash, sizeof * hash); memcpy(hash->buffer, old, bs_2); @@ -454,8 +454,8 @@ void blake384_final(struct blake384_hash* hash, char* out_stream){ _blake512_final(hash, out_stream); for(int i = 0; i != 6; i++){ - sprintf(out_stream, "%s%016llx",out_stream, (hash->hash)[i]); - } + sprintf(out_stream + 16 * i, "%016llx", (hash->hash)[i]); + } memcpy(hash, &old_hash, sizeof * hash); memcpy(hash->buffer, old, bs_2); diff --git a/src/hash/blake2.c b/src/hash/blake2.c index 61c79ca..9507c96 100644 --- a/src/hash/blake2.c +++ b/src/hash/blake2.c @@ -231,7 +231,7 @@ void blake2b_final(struct blake2b_hash* hash, char* out_stream){ 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])); + for(int i = 0; i != hash->digest_len; i++)sprintf(out_stream + i * 2, "%02x", (((uint8_t*)hash->hash)[i])); memcpy(hash, &old_hash, sizeof * hash); memcpy(hash->buffer, old, bs_2); @@ -363,7 +363,7 @@ void blake2s_final(struct blake2s_hash* hash, char* out_stream){ blake2s_round(hash, 1); - for(int i = 0; i != hash->digest_len; i++)sprintf(out_stream, "%s%02x", out_stream, (((uint8_t*)hash->hash)[i])); + for(int i = 0; i != hash->digest_len; i++)sprintf(out_stream + i * 2, "%02x", (((uint8_t*)hash->hash)[i])); memcpy(hash, &old_hash, sizeof * hash); memcpy(hash->buffer, old, bs); diff --git a/src/hash/sha2-256.c b/src/hash/sha2-256.c index 7e37da9..d4a685a 100644 --- a/src/hash/sha2-256.c +++ b/src/hash/sha2-256.c @@ -175,14 +175,14 @@ 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, "%016llx%016llx%016llx%016llx%016llx%016llx%016llx%016llx", hash->h0, hash->h1, hash->h2, hash->h3, hash->h4, hash->h5, hash->h6, hash->h7); + /*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); + sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h7);*/ memcpy(hash, &old_hash, sizeof * hash); memcpy(hash->buffer, old, bs); @@ -196,12 +196,12 @@ 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, "%016llx%016llx%016llx%016llx%016llx%016llx", hash->h0, hash->h1, hash->h2, hash->h3, hash->h4, hash->h5); + /*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->h5);*/ memcpy(hash, &old_hash, sizeof * hash); memcpy(hash->buffer, old, bs); @@ -236,7 +236,7 @@ struct iv sha_iv_gen(int i){ uint8_t nh[512] = {0}; uint8_t in[12]; - sprintf((char*)in, "SHA-512/%i",i); + sprintf((char*)in, "SHA-512/%i", i); struct sha512_hash a = sha512_t_init(oh); sha512_update(in, strlen((char*)in), &a); _sha512_t_final(&a); -- cgit v1.2.3