test: Host-Testumgebung mit Unity, SHA256 und CRC
This commit is contained in:
@@ -0,0 +1,25 @@
|
|||||||
|
#include "util/nl_crc.h"
|
||||||
|
|
||||||
|
uint32_t nl_crc32(uint32_t crc, const void *data, size_t len)
|
||||||
|
{
|
||||||
|
const uint8_t *p = (const uint8_t *)data;
|
||||||
|
crc = ~crc;
|
||||||
|
while (len--) {
|
||||||
|
crc ^= *p++;
|
||||||
|
for (int k = 0; k < 8; k++)
|
||||||
|
crc = (crc >> 1) ^ (0xEDB88320u & (0u - (crc & 1u)));
|
||||||
|
}
|
||||||
|
return ~crc;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t nl_crc8(const void *data, size_t len)
|
||||||
|
{
|
||||||
|
const uint8_t *p = (const uint8_t *)data;
|
||||||
|
uint8_t crc = 0;
|
||||||
|
while (len--) {
|
||||||
|
crc ^= *p++;
|
||||||
|
for (int k = 0; k < 8; k++)
|
||||||
|
crc = (uint8_t)((crc & 0x80u) ? (crc << 1) ^ 0x07u : (crc << 1));
|
||||||
|
}
|
||||||
|
return crc;
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
#ifndef NL_CRC_H
|
||||||
|
#define NL_CRC_H
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
/* CRC-32 (IEEE 802.3, reflected, poly 0xEDB88320). Start with crc = 0; chainable. */
|
||||||
|
uint32_t nl_crc32(uint32_t crc, const void *data, size_t len);
|
||||||
|
|
||||||
|
/* CRC-8 (poly 0x07, init 0x00, no reflection). */
|
||||||
|
uint8_t nl_crc8(const void *data, size_t len);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,96 @@
|
|||||||
|
#include "util/nl_sha256.h"
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define ROR(x, n) (((x) >> (n)) | ((x) << (32 - (n))))
|
||||||
|
|
||||||
|
static const uint32_t K[64] = {
|
||||||
|
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
||||||
|
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
||||||
|
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
||||||
|
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
||||||
|
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
||||||
|
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
||||||
|
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
||||||
|
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};
|
||||||
|
|
||||||
|
static void transform(nl_sha256_ctx *c, const uint8_t *p)
|
||||||
|
{
|
||||||
|
uint32_t w[64];
|
||||||
|
uint32_t a, b, cc, d, e, f, g, h;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < 16; i++)
|
||||||
|
w[i] = ((uint32_t)p[4 * i] << 24) | ((uint32_t)p[4 * i + 1] << 16) |
|
||||||
|
((uint32_t)p[4 * i + 2] << 8) | (uint32_t)p[4 * i + 3];
|
||||||
|
for (i = 16; i < 64; i++) {
|
||||||
|
uint32_t s0 = ROR(w[i - 15], 7) ^ ROR(w[i - 15], 18) ^ (w[i - 15] >> 3);
|
||||||
|
uint32_t s1 = ROR(w[i - 2], 17) ^ ROR(w[i - 2], 19) ^ (w[i - 2] >> 10);
|
||||||
|
w[i] = w[i - 16] + s0 + w[i - 7] + s1;
|
||||||
|
}
|
||||||
|
|
||||||
|
a = c->state[0]; b = c->state[1]; cc = c->state[2]; d = c->state[3];
|
||||||
|
e = c->state[4]; f = c->state[5]; g = c->state[6]; h = c->state[7];
|
||||||
|
|
||||||
|
for (i = 0; i < 64; i++) {
|
||||||
|
uint32_t S1 = ROR(e, 6) ^ ROR(e, 11) ^ ROR(e, 25);
|
||||||
|
uint32_t ch = (e & f) ^ (~e & g);
|
||||||
|
uint32_t t1 = h + S1 + ch + K[i] + w[i];
|
||||||
|
uint32_t S0 = ROR(a, 2) ^ ROR(a, 13) ^ ROR(a, 22);
|
||||||
|
uint32_t maj = (a & b) ^ (a & cc) ^ (b & cc);
|
||||||
|
uint32_t t2 = S0 + maj;
|
||||||
|
h = g; g = f; f = e; e = d + t1;
|
||||||
|
d = cc; cc = b; b = a; a = t1 + t2;
|
||||||
|
}
|
||||||
|
|
||||||
|
c->state[0] += a; c->state[1] += b; c->state[2] += cc; c->state[3] += d;
|
||||||
|
c->state[4] += e; c->state[5] += f; c->state[6] += g; c->state[7] += h;
|
||||||
|
}
|
||||||
|
|
||||||
|
void nl_sha256_init(nl_sha256_ctx *c)
|
||||||
|
{
|
||||||
|
static const uint32_t H0[8] = {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
|
||||||
|
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};
|
||||||
|
memcpy(c->state, H0, sizeof H0);
|
||||||
|
c->bitlen = 0;
|
||||||
|
c->buflen = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void nl_sha256_update(nl_sha256_ctx *c, const void *data, size_t len)
|
||||||
|
{
|
||||||
|
const uint8_t *p = (const uint8_t *)data;
|
||||||
|
while (len--) {
|
||||||
|
c->buf[c->buflen++] = *p++;
|
||||||
|
if (c->buflen == 64) {
|
||||||
|
transform(c, c->buf);
|
||||||
|
c->bitlen += 512;
|
||||||
|
c->buflen = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void nl_sha256_final(nl_sha256_ctx *c, uint8_t out[32])
|
||||||
|
{
|
||||||
|
uint32_t i = c->buflen;
|
||||||
|
|
||||||
|
c->bitlen += (uint64_t)c->buflen * 8u;
|
||||||
|
c->buf[i++] = 0x80;
|
||||||
|
if (i > 56) {
|
||||||
|
while (i < 64)
|
||||||
|
c->buf[i++] = 0;
|
||||||
|
transform(c, c->buf);
|
||||||
|
i = 0;
|
||||||
|
}
|
||||||
|
while (i < 56)
|
||||||
|
c->buf[i++] = 0;
|
||||||
|
for (int k = 0; k < 8; k++)
|
||||||
|
c->buf[63 - k] = (uint8_t)(c->bitlen >> (8 * k));
|
||||||
|
transform(c, c->buf);
|
||||||
|
|
||||||
|
for (int k = 0; k < 8; k++) {
|
||||||
|
out[4 * k] = (uint8_t)(c->state[k] >> 24);
|
||||||
|
out[4 * k + 1] = (uint8_t)(c->state[k] >> 16);
|
||||||
|
out[4 * k + 2] = (uint8_t)(c->state[k] >> 8);
|
||||||
|
out[4 * k + 3] = (uint8_t)(c->state[k]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
#ifndef NL_SHA256_H
|
||||||
|
#define NL_SHA256_H
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint32_t state[8];
|
||||||
|
uint64_t bitlen;
|
||||||
|
uint8_t buf[64];
|
||||||
|
uint32_t buflen;
|
||||||
|
} nl_sha256_ctx;
|
||||||
|
|
||||||
|
void nl_sha256_init(nl_sha256_ctx *c);
|
||||||
|
void nl_sha256_update(nl_sha256_ctx *c, const void *data, size_t len);
|
||||||
|
void nl_sha256_final(nl_sha256_ctx *c, uint8_t out[32]);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.20)
|
||||||
|
project(ninlight_host_tests C)
|
||||||
|
|
||||||
|
set(CMAKE_C_STANDARD 11)
|
||||||
|
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
|
include(FetchContent)
|
||||||
|
FetchContent_Declare(unity
|
||||||
|
GIT_REPOSITORY https://github.com/ThrowTheSwitch/Unity.git
|
||||||
|
GIT_TAG v2.6.0)
|
||||||
|
FetchContent_MakeAvailable(unity)
|
||||||
|
|
||||||
|
enable_testing()
|
||||||
|
set(NL_SRC ${CMAKE_CURRENT_SOURCE_DIR}/../src)
|
||||||
|
|
||||||
|
function(nl_test name)
|
||||||
|
add_executable(${name} ${ARGN})
|
||||||
|
target_include_directories(${name} PRIVATE ${NL_SRC} ${CMAKE_CURRENT_SOURCE_DIR})
|
||||||
|
target_compile_options(${name} PRIVATE -Wall -Wextra -Werror)
|
||||||
|
target_compile_definitions(${name} PRIVATE NINLIGHT_HOST_TEST=1)
|
||||||
|
target_link_libraries(${name} PRIVATE unity m)
|
||||||
|
add_test(NAME ${name} COMMAND ${name})
|
||||||
|
endfunction()
|
||||||
|
|
||||||
|
nl_test(test_sha256 test_sha256.c ${NL_SRC}/util/nl_sha256.c)
|
||||||
|
nl_test(test_crc test_crc.c ${NL_SRC}/util/nl_crc.c)
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
#include "unity.h"
|
||||||
|
#include "util/nl_crc.h"
|
||||||
|
|
||||||
|
void setUp(void) {}
|
||||||
|
void tearDown(void) {}
|
||||||
|
|
||||||
|
static void test_crc32_check_value(void)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_EQUAL_HEX32(0xCBF43926u, nl_crc32(0, "123456789", 9));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_crc32_empty_is_zero(void)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_EQUAL_HEX32(0u, nl_crc32(0, "", 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_crc32_chaining(void)
|
||||||
|
{
|
||||||
|
uint32_t c = nl_crc32(0, "1234", 4);
|
||||||
|
TEST_ASSERT_EQUAL_HEX32(0xCBF43926u, nl_crc32(c, "56789", 5));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_crc8_check_value(void)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_EQUAL_HEX8(0xF4, nl_crc8("123456789", 9));
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
UNITY_BEGIN();
|
||||||
|
RUN_TEST(test_crc32_check_value);
|
||||||
|
RUN_TEST(test_crc32_empty_is_zero);
|
||||||
|
RUN_TEST(test_crc32_chaining);
|
||||||
|
RUN_TEST(test_crc8_check_value);
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "unity.h"
|
||||||
|
#include "util/nl_sha256.h"
|
||||||
|
|
||||||
|
void setUp(void) {}
|
||||||
|
void tearDown(void) {}
|
||||||
|
|
||||||
|
static void digest_hex(const void *msg, size_t len, char out[65])
|
||||||
|
{
|
||||||
|
uint8_t d[32];
|
||||||
|
nl_sha256_ctx c;
|
||||||
|
nl_sha256_init(&c);
|
||||||
|
nl_sha256_update(&c, msg, len);
|
||||||
|
nl_sha256_final(&c, d);
|
||||||
|
for (int i = 0; i < 32; i++)
|
||||||
|
sprintf(out + 2 * i, "%02x", d[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_empty(void)
|
||||||
|
{
|
||||||
|
char h[65];
|
||||||
|
digest_hex("", 0, h);
|
||||||
|
TEST_ASSERT_EQUAL_STRING("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", h);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_abc(void)
|
||||||
|
{
|
||||||
|
char h[65];
|
||||||
|
digest_hex("abc", 3, h);
|
||||||
|
TEST_ASSERT_EQUAL_STRING("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", h);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_two_blocks(void)
|
||||||
|
{
|
||||||
|
const char *m = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
|
||||||
|
char h[65];
|
||||||
|
digest_hex(m, strlen(m), h);
|
||||||
|
TEST_ASSERT_EQUAL_STRING("248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1", h);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_million_a(void)
|
||||||
|
{
|
||||||
|
uint8_t d[32];
|
||||||
|
char h[65];
|
||||||
|
nl_sha256_ctx c;
|
||||||
|
nl_sha256_init(&c);
|
||||||
|
for (int i = 0; i < 1000000; i++)
|
||||||
|
nl_sha256_update(&c, "a", 1);
|
||||||
|
nl_sha256_final(&c, d);
|
||||||
|
for (int i = 0; i < 32; i++)
|
||||||
|
sprintf(h + 2 * i, "%02x", d[i]);
|
||||||
|
TEST_ASSERT_EQUAL_STRING("cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0", h);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_streaming_equals_oneshot(void)
|
||||||
|
{
|
||||||
|
uint8_t buf[1000], a[32], b[32];
|
||||||
|
nl_sha256_ctx c;
|
||||||
|
for (int i = 0; i < 1000; i++)
|
||||||
|
buf[i] = (uint8_t)(i * 7);
|
||||||
|
nl_sha256_init(&c);
|
||||||
|
nl_sha256_update(&c, buf, sizeof buf);
|
||||||
|
nl_sha256_final(&c, a);
|
||||||
|
nl_sha256_init(&c);
|
||||||
|
for (size_t off = 0; off < sizeof buf; off += 37) {
|
||||||
|
size_t n = sizeof buf - off < 37 ? sizeof buf - off : 37;
|
||||||
|
nl_sha256_update(&c, buf + off, n);
|
||||||
|
}
|
||||||
|
nl_sha256_final(&c, b);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8_ARRAY(a, b, 32);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
UNITY_BEGIN();
|
||||||
|
RUN_TEST(test_empty);
|
||||||
|
RUN_TEST(test_abc);
|
||||||
|
RUN_TEST(test_two_blocks);
|
||||||
|
RUN_TEST(test_million_a);
|
||||||
|
RUN_TEST(test_streaming_equals_oneshot);
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
Executable
+7
@@ -0,0 +1,7 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# Builds and runs all host unit tests.
|
||||||
|
set -euo pipefail
|
||||||
|
ROOT=$(cd "$(dirname "$0")/.." && pwd)
|
||||||
|
cmake -S "$ROOT/test" -B "$ROOT/build/host" -G Ninja >/dev/null
|
||||||
|
cmake --build "$ROOT/build/host"
|
||||||
|
ctest --test-dir "$ROOT/build/host" --output-on-failure "$@"
|
||||||
Reference in New Issue
Block a user