nucleo-l053r8-benches/crates/algorithms/algorithms-c/test/hash/test_sha2.c

56 lines
1.6 KiB
C

#include "hash.h"
#include "sha2.h"
#include "trace.h"
#include "unity.h"
#include <stddef.h>
#include <stdint.h>
#include <string.h>
void setUp(void) {}
void tearDown(void) {}
// BUG: may drop the null at end of strings
#define SAMPLE(INPUT, HASH) \
{(uint8_t[]){INPUT "\0"}, (uint8_t *)(uint32_t[])HASH}
#define TEST_VALUES_LEN 2
uint8_t *test_values[TEST_VALUES_LEN][2] = {
SAMPLE("AAAA", HASH(0x63c1dd95, 0x1ffedf6f, 0x7fd968ad, 0x4efa39b8,
0xed584f16, 0x2f46e715, 0x114ee184, 0xf8de9201)),
SAMPLE("BAAA", HASH(0x49e3cd45, 0x27c96cdc, 0x010160ff, 0x08520e0c,
0xb63c6ef8, 0xc4e7d486, 0x08995343, 0x7f83a159)),
};
void test_sha2_check(void) {
SHA2Digest digest;
SHA2Result res;
uint8_t *input;
size_t len;
size_t i;
for (i = 0; i < TEST_VALUES_LEN; i++) {
TRACE("Hash iteration %d\n", i);
input = test_values[i][0];
len = strlen((char *)input);
TRACELN("Input:");
dump_data(input, len);
TRACELN("Call oneshot");
res = sha2_256_oneshot(input, len, digest);
TEST_ASSERT_EQUAL_MESSAGE(
shaSuccess, res,
"sha2_256_oneshot exited with a code that is not shaSuccess");
// NOTE: If it segfaults here, then the sha2_oneshot function is at fault
// anyways!!!!!!!!!!!!
TRACELN("Hash should be:");
print_digest(test_values[i][1]); // idk how to fix this warning
TRACELN("Hash is:");
print_digest(digest);
TEST_ASSERT_EQUAL_HEX32_ARRAY(test_values[i][1], digest,
SHA2_256_HashParts);
}
}