diff --git a/Makefile b/Makefile index 1f63d45..b1cff16 100644 --- a/Makefile +++ b/Makefile @@ -41,10 +41,10 @@ TEST_BINS = $(TEST_OBJS:.o=) PHONY += test test: $(TEST_BINS) - @for t in $^; do \ - echo; \ - echo '***' "$$t.c" '***'; \ - "./$$t"; \ + @for t in $^; do \ + echo; \ + echo '***' "$$t.c" '***'; \ + "./$$t" || printf "\n!!! TEST FAILURE !!!\n"; \ done tests/c/test_%.o: tests/c/test_%.c @@ -66,4 +66,7 @@ clean: dist-clean: clean +PHONY += build +build: all + .PHONY: $(PHONY) diff --git a/include/mh/hashes.h b/include/mh/hashes.h index 1d00ecd..1285679 100644 --- a/include/mh/hashes.h +++ b/include/mh/hashes.h @@ -15,11 +15,12 @@ typedef enum { MH_H_BLAKE2B, MH_H_BLAKE2S, - HM_H_COUNT // number of hash functions + MH_H_COUNT // number of hash functions } mh_hash; -// returns default size in bytes of given hash function -int mh_hash_default_size(mh_hash hash); +// returns default length in bytes of given hash function +// or -1 in case of invalid arguemnt +int mh_hash_default_length(mh_hash hash); // returns wire format code of given hash function int mh_hash_code(mh_hash hash); diff --git a/src/hashes.c b/src/hashes.c index 7be258f..a42768e 100644 --- a/src/hashes.c +++ b/src/hashes.c @@ -1,2 +1,47 @@ /* vim: set ts=8 sw=8 noexpandtab: */ #include "mh/hashes.h" + + +static const int hash_lengths[] = { + 20, // sha1 + 32, // sha2-256 + 64, // sha2-512 + 64, // sha3-512 + 45, // sha3-364 + 32, // sha3-256 + 28, // sha3-224 + 16, // shake-128 + 32, // shake-256 + // TODO(Kubuxu): implement blake lengths +}; + + +int mh_hash_default_length(mh_hash hash) { + if (hash > MH_H_SHAKE_256) + return -2; // TODO(Kubuxu): remove it after blake is implemented + if (hash < 0 || hash >= MH_H_COUNT) + return -1; + + return hash_lengths[0]; +} + + + + + + +/* + MH_H_SHA1, + MH_H_SHA2_256, + MH_H_SHA2_512, + MH_H_SHA3_512, + MH_H_SHA3_384, + MH_H_SHA3_256, + MH_H_SHA3_224, + MH_H_SHAKE_128, + MH_H_SHAKE_256, + MH_H_BLAKE2B, + MH_H_BLAKE2S, + + HM_H_COUNT // number of hash functions + */ diff --git a/tests/c/minunit.h b/tests/c/minunit.h index 52ad29b..4a36485 100644 --- a/tests/c/minunit.h +++ b/tests/c/minunit.h @@ -9,8 +9,8 @@ static char *mu_all_tests(void); int main(void) { char *result = mu_all_tests(); - if (result != 0) { - printf("%s\n", result); + if (result != NULL) { + printf("error: %s\n", result); } else { printf("ALL TESTS PASSED\n"); diff --git a/tests/c/test_error_string.c b/tests/c/test_error_string.c index 1334c3c..79d881e 100644 --- a/tests/c/test_error_string.c +++ b/tests/c/test_error_string.c @@ -1,3 +1,4 @@ +/* vim: set ts=8 sw=8 noexpandtab: */ #include #include "minunit.h" @@ -12,10 +13,10 @@ static char *test_error_messages_exist(void) { mu_assert(error_buf, strlen(mh_error_string((mh_error) i))); } - return 0; + return NULL; } static char *mu_all_tests(void) { mu_run_test(test_error_messages_exist); - return 0; + return NULL; } diff --git a/tests/c/test_hashes_length.c b/tests/c/test_hashes_length.c new file mode 100644 index 0000000..dd702b6 --- /dev/null +++ b/tests/c/test_hashes_length.c @@ -0,0 +1,28 @@ +/* vim: set ts=8 sw=8 noexpandtab: */ +#include +#include "minunit.h" + +#include "mh/hashes.h" + +char error_buf[256]; + +static char *test_all_hashes_have_lengths(void) { + int i = 0; + int length = 0; + for (; i < MH_H_COUNT; i++) { + length = mh_hash_default_length(i); + if (length <= 0) { + sprintf(error_buf, "mh_hash_default_length: hash %d" + " returned invalid (%d) default length", + i, length); + return error_buf; + } + } + return NULL; +} + +static char *mu_all_tests(void) { + mu_run_test(test_all_hashes_have_lengths); + return NULL; +} +