start implementing default lengths

fixup syle in few places
whys-review
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

@ -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)

View File

@ -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);

View File

@ -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
*/

View File

@ -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");

View File

@ -1,3 +1,4 @@
/* vim: set ts=8 sw=8 noexpandtab: */
#include <string.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)));
}
return 0;
return NULL;
}
static char *mu_all_tests(void) {
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;
}