diff options
author | Dimitri Staessens <dimitri@ouroboros.rocks> | 2024-02-19 16:05:59 +0100 |
---|---|---|
committer | Sander Vrijders <sander@ouroboros.rocks> | 2024-02-19 16:06:34 +0100 |
commit | c58cc09cf50a18058efb59e1b4fb73ab1e3a45a2 (patch) | |
tree | 7674e6939fc30eb3b052686ea2ee95bde3580bca | |
parent | d2556ebc3fbc59736863e53f5b2c35e72eb09ae8 (diff) | |
download | ouroboros-c58cc09cf50a18058efb59e1b4fb73ab1e3a45a2.tar.gz ouroboros-c58cc09cf50a18058efb59e1b4fb73ab1e3a45a2.zip |
lib: Fix CRC32 hashing0.21.1
I assumed that all hashes were stored in Big Endian, but apparently
the CRC32 was still in machine endianness when calculated by the
rhash implementation. Fixed and updated hash tests a bit.
Signed-off-by: Dimitri Staessens <dimitri@ouroboros.rocks>
Signed-off-by: Sander Vrijders <sander@ouroboros.rocks>
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | include/ouroboros/test.h | 33 | ||||
-rw-r--r-- | src/lib/hash.c | 2 | ||||
-rw-r--r-- | src/lib/tests/hash_test.c | 48 |
4 files changed, 55 insertions, 30 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index bc89d2d5..7bed8396 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,7 @@ include(GNUInstallDirs) set(PACKAGE_VERSION_MAJOR 0) set(PACKAGE_VERSION_MINOR 21) -set(PACKAGE_VERSION_PATCH 0) +set(PACKAGE_VERSION_PATCH 1) set(PACKAGE_NAME "${CMAKE_PROJECT_NAME}") set(PACKAGE_DESCRIPTION "The Ouroboros prototype") diff --git a/include/ouroboros/test.h b/include/ouroboros/test.h index a88b5548..096e145c 100644 --- a/include/ouroboros/test.h +++ b/include/ouroboros/test.h @@ -29,22 +29,25 @@ #include <unistd.h> #include <sys/wait.h> -#define TEST_START() \ - do { \ - printf("%s started.\n", __func__); \ - fflush(stdout); \ - } while(0) -#define TEST_SUCCESS() \ - do { \ - printf("%s succeeded.\n", __func__); \ - fflush(stdout); \ - } while(0) +#define TEST_START() \ + do { \ + printf("%s started.\n", __func__); \ + fflush(stdout); \ + } while (0) +#define TEST_SUCCESS() \ + do { \ + printf("%s succeeded.\n", __func__); \ + fflush(stdout); \ + } while (0) -#define TEST_FAIL() \ - do { \ - printf("%s failed.\n", __func__); \ - fflush(stdout); \ - } while(0) +#define TEST_FAIL() \ + do { \ + printf("%s failed.\n", __func__); \ + fflush(stdout); \ + } while (0) + +#define TEST_END(result) \ + do { if (result == 0) TEST_SUCCESS(); else TEST_FAIL(); } while (0) static int __attribute__((unused)) test_assert_fail(int(* testfunc)(void)) { diff --git a/src/lib/hash.c b/src/lib/hash.c index 995ba0d2..b465f894 100644 --- a/src/lib/hash.c +++ b/src/lib/hash.c @@ -29,6 +29,7 @@ #include "config.h" +#include <ouroboros/endian.h> #include <ouroboros/hash.h> #ifdef HAVE_LIBGCRYPT @@ -90,6 +91,7 @@ void mem_hash(enum hash_algo algo, case HASH_CRC32: memset(dst, 0, CRC32_HASH_LEN); crc32((uint32_t *) dst, buf, len); + *(uint32_t *) dst = htobe32(*(uint32_t *) dst); break; case HASH_MD5: rhash_md5_init(&md5_ctx); diff --git a/src/lib/tests/hash_test.c b/src/lib/tests/hash_test.c index 1e618e48..970d9185 100644 --- a/src/lib/tests/hash_test.c +++ b/src/lib/tests/hash_test.c @@ -21,6 +21,7 @@ */ #include <ouroboros/hash.h> +#include <ouroboros/test.h> #include <stdlib.h> #include <stdint.h> @@ -40,6 +41,8 @@ struct vec_entry { static int test_crc32(void) { + int ret = 0; + struct vec_entry vec [] = { { "0", "f4dbdf21" }, { "123456789", "cbf43926" }, @@ -49,6 +52,8 @@ static int test_crc32(void) struct vec_entry * cur = vec; + TEST_START(); + while (cur->in != NULL) { uint8_t crc[4]; char res[9]; @@ -57,18 +62,22 @@ static int test_crc32(void) sprintf(res, HASH_FMT32, HASH_VAL32(crc)); if (strcmp(res, cur->out) != 0) { - printf("Hash failed %s != %s", res, cur->out); - return -1; + printf("Hash failed %s != %s.\n", res, cur->out); + ret |= -1; } ++cur; } - return 0; + TEST_END(ret); + + return ret; } static int test_md5(void) { + int ret = 0; + struct vec_entry vec [] = {{ "abc", "900150983cd24fb0d6963f7d28e17f72" @@ -85,6 +94,9 @@ static int test_md5(void) struct vec_entry * cur = vec; + TEST_START(); + + while (cur->in != NULL) { uint8_t md5[16]; char res[33]; @@ -93,18 +105,22 @@ static int test_md5(void) sprintf(res, HASH_FMT128, HASH_VAL128(md5)); if (strcmp(res, cur->out) != 0) { - printf("Hash failed %s != %s", res, cur->out); - return -1; + printf("Hash failed %s != %s.\n", res, cur->out); + ret |= -1; } ++cur; } - return 0; + TEST_END(ret); + + return ret; } static int test_sha3(void) { + int ret = 0; + uint8_t sha3[64]; char res[129]; @@ -114,12 +130,14 @@ static int test_sha3(void) "e642824c3f8cf24ad09234ee7d3c766f" "c9a3a5168d0c94ad73b46fdf"; + TEST_START(); + str_hash(HASH_SHA3_224, sha3, in); sprintf(res, HASH_FMT224, HASH_VAL224(sha3)); if (strcmp(res, out) != 0) { printf("SHA3-224 failed %s != %s", res, out); - return -1; + ret |= -1; } out = @@ -130,8 +148,8 @@ static int test_sha3(void) sprintf(res, HASH_FMT256, HASH_VAL256(sha3)); if (strcmp(res, out) != 0) { - printf("SHA3-256 failed %s != %s", res, out); - return -1; + printf("SHA3-256 failed %s != %s.\n", res, out); + ret |= -1; } out = @@ -143,8 +161,8 @@ static int test_sha3(void) sprintf(res, HASH_FMT384, HASH_VAL384(sha3)); if (strcmp(res, out) != 0) { - printf("SHA3-384failed %s != %s", res, out); - return -1; + printf("SHA3-384failed %s != %s.'n", res, out); + ret |= -1; } out = @@ -157,11 +175,13 @@ static int test_sha3(void) sprintf(res, HASH_FMT512, HASH_VAL512(sha3)); if (strcmp(res, out) != 0) { - printf("SHA3-512 failed %s != %s", res, out); - return -1; + printf("SHA3-512 failed %s != %s.\n", res, out); + ret |= -1; } - return 0; + TEST_END(ret); + + return ret; } int hash_test(int argc, |