start implementing default lengths

fixup syle in few places
This commit is contained in:
Jakub Sztandera 2016-07-26 16:53:52 +01:00
parent 0eafc13839
commit b154a83b35
6 changed files with 89 additions and 11 deletions

View file

@ -44,7 +44,7 @@ test: $(TEST_BINS)
@for t in $^; do \ @for t in $^; do \
echo; \ echo; \
echo '***' "$$t.c" '***'; \ echo '***' "$$t.c" '***'; \
"./$$t"; \ "./$$t" || printf "\n!!! TEST FAILURE !!!\n"; \
done done
tests/c/test_%.o: tests/c/test_%.c tests/c/test_%.o: tests/c/test_%.c
@ -66,4 +66,7 @@ clean:
dist-clean: clean dist-clean: clean
PHONY += build
build: all
.PHONY: $(PHONY) .PHONY: $(PHONY)

View file

@ -15,11 +15,12 @@ typedef enum {
MH_H_BLAKE2B, MH_H_BLAKE2B,
MH_H_BLAKE2S, MH_H_BLAKE2S,
HM_H_COUNT // number of hash functions MH_H_COUNT // number of hash functions
} mh_hash; } mh_hash;
// returns default size in bytes of given hash function // returns default length in bytes of given hash function
int mh_hash_default_size(mh_hash hash); // or -1 in case of invalid arguemnt
int mh_hash_default_length(mh_hash hash);
// returns wire format code of given hash function // returns wire format code of given hash function
int mh_hash_code(mh_hash hash); int mh_hash_code(mh_hash hash);

View file

@ -1,2 +1,47 @@
/* vim: set ts=8 sw=8 noexpandtab: */ /* vim: set ts=8 sw=8 noexpandtab: */
#include "mh/hashes.h" #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
*/

View file

@ -9,8 +9,8 @@ static char *mu_all_tests(void);
int main(void) { int main(void) {
char *result = mu_all_tests(); char *result = mu_all_tests();
if (result != 0) { if (result != NULL) {
printf("%s\n", result); printf("error: %s\n", result);
} }
else { else {
printf("ALL TESTS PASSED\n"); printf("ALL TESTS PASSED\n");

View file

@ -1,3 +1,4 @@
/* vim: set ts=8 sw=8 noexpandtab: */
#include <string.h> #include <string.h>
#include "minunit.h" #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))); mu_assert(error_buf, strlen(mh_error_string((mh_error) i)));
} }
return 0; return NULL;
} }
static char *mu_all_tests(void) { static char *mu_all_tests(void) {
mu_run_test(test_error_messages_exist); mu_run_test(test_error_messages_exist);
return 0; return NULL;
} }

View file

@ -0,0 +1,28 @@
/* vim: set ts=8 sw=8 noexpandtab: */
#include <string.h>
#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;
}