nucleo-l053r8-benches/crates/algorithms/algorithms-c/test/hash/test_sha2.c
2025-07-17 17:14:11 +02:00

60 lines
2 KiB
C

#include "sha2.h"
#include "trace.h"
#include "unity.h"
#include <stddef.h>
#include <stdint.h>
#include <string.h>
void setUp(void) {}
void tearDown(void) {}
#define HEXARR(...) \
(uint8_t[SHA2_256_HashBytes]) { __VA_ARGS__ }
// BUG: may drop the null at end of strings
#define SAMPLE(INPUT, ...) {(uint8_t[]){INPUT "\0"}, HEXARR(__VA_ARGS__)}
#define TEST_VALUES_LEN 3
uint8_t *test_values[TEST_VALUES_LEN][2] = {
SAMPLE("AAAA", 0x63, 0xc1, 0xdd, 0x95, 0x1f, 0xfe, 0xdf, 0x6f, 0x7f, 0xd9,
0x68, 0xad, 0x4e, 0xfa, 0x39, 0xb8, 0xed, 0x58, 0x4f, 0x16, 0x2f,
0x46, 0xe7, 0x15, 0x11, 0x4e, 0xe1, 0x84, 0xf8, 0xde, 0x92, 0x01, ),
SAMPLE("", 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4,
0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b,
0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, ),
SAMPLE("BAAA", 0x49, 0xe3, 0xcd, 0x45, 0x27, 0xc9, 0x6c, 0xdc, 0x01, 0x01,
0x60, 0xff, 0x08, 0x52, 0x0e, 0x0c, 0xb6, 0x3c, 0x6e, 0xf8, 0xc4,
0xe7, 0xd4, 0x86, 0x08, 0x99, 0x53, 0x43, 0x7f, 0x83, 0xa1, 0x59, )};
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("Hash 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:");
dump_data(test_values[i][1], SHA2_256_HashBytes);
TRACELN("Hash is:");
dump_data((uint8_t *)digest, SHA2_256_HashBytes);
TEST_ASSERT_EQUAL_MEMORY(test_values[i][1], digest, SHA2_256_HashBytes);
}
TRACELN("ALL TESTS DONE")
}