Skip to content

eBPF-based security platform

testing: unit test util/hex.c and the chain helpers

Daniel Borkmannbd0311f22ff9
diff --git a/Makefile.shirudo b/Makefile.shirudo
index c4ca2050..48daa44f 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -600,7 +600,6 @@ TEST_HELPERS := $(VERITY_TEST_HELPERS) \
testing/helper/test-conditions \
testing/helper/test-config-image \
testing/helper/test-harden \
- testing/helper/test-chain \
testing/helper/test-tpm-wire \
testing/helper/test-register \
testing/helper/test-register-cli \
@@ -641,8 +640,8 @@ testing/helper/test-harden: testing/helper/test-harden.c util/harden.o util/log.
@printf ' CC $@\n'
$(Q)$(CC) $(CFLAGS) -o $@ $^ $(LIBCRYPTO_LIBS)
-# chain.o's read/extend paths reference util/tpm (libtss2); the tests
-# only exercise the pure helpers, but the symbols must still resolve.
+# chain.o's read/extend paths reference util/tpm (libtss2), so the
+# symbols must resolve even for a helper that never extends.
# util/malloc.o brings in x*/libc_free that chain.o/tpm.o use.
# util/config.o resolves config_load(), called by tpm.o.
# The register code pulls in the eventlog, which pulls in the seal
@@ -692,10 +691,6 @@ testing/helper/test-eventlog: testing/helper/test-eventlog.c $(REGISTER_OBJS)
@printf ' CC $@\n'
$(Q)$(CC) $(CFLAGS) -o $@ $^ $(REGISTER_LIBS)
-testing/helper/test-chain: testing/helper/test-chain.c $(REGISTER_OBJS)
- @printf ' CC $@\n'
- $(Q)$(CC) $(CFLAGS) -o $@ $^ $(REGISTER_LIBS)
-
testing/helper/test-tpm-wire: testing/helper/test-tpm-wire.c util/tpm_wire.o util/hex.o \
util/crypto_openssl.o util/io.o util/seal.o util/malloc.o util/log.o \
util/string.o
diff --git a/testing/helper/test-chain.c b/testing/helper/test-chain.c
deleted file mode 100644
index 23a3f53f..00000000
--- a/testing/helper/test-chain.c
+++ /dev/null
@@ -1,64 +0,0 @@
-// SPDX-License-Identifier: BSD-2-Clause
-/*
- * Copyright (c) 2026 Authors of shirudo
- *
- * Harness for util/chain's pure helpers (hex, sha256, constant-time
- * compare). Subcommand on argv[1]; inputs as argv. Prints the result
- * so a shell test can assert on it.
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <shirudo/misc.h>
-
-#include "chain.h"
-
-static void puthex(const __u8 *b, size_t n)
-{
- char h[2 * 64 + 1];
-
- hex_encode(b, n, h);
- printf("%s\n", h);
-}
-
-int main(int argc, char **argv)
-{
- if (argc < 2)
- return 2;
-
- if (strcmp(argv[1], "sha256") == 0 && argc == 3) {
- __u8 d[32];
-
- shirudo_sha256(argv[2], strlen(argv[2]), d);
- puthex(d, 32);
- return 0;
- }
- if (strcmp(argv[1], "decode") == 0 && argc == 4) {
- size_t n = (size_t)strtoul(argv[3], NULL, 10);
- __u8 buf[256];
- int rc;
-
- if (n > sizeof(buf))
- return 2;
- rc = hex_decode(argv[2], buf, n);
- printf("rc=%d\n", rc);
- if (rc == 0)
- puthex(buf, n);
- return 0;
- }
- if (strcmp(argv[1], "cteq") == 0 && argc == 4) {
- __u8 a[256], b[256];
- size_t na = strlen(argv[2]) / 2, nb = strlen(argv[3]) / 2;
-
- if (na != nb || na > sizeof(a))
- return 2;
- if (hex_decode(argv[2], a, na) ||
- hex_decode(argv[3], b, nb))
- return 2;
- printf("%d\n", ct_eq(a, b, na));
- return 0;
- }
- return 2;
-}
diff --git a/testing/tpm/t0003-chain.sh b/testing/tpm/t0003-chain.sh
deleted file mode 100644
index ccfc8bce..00000000
--- a/testing/tpm/t0003-chain.sh
+++ /dev/null
@@ -1,38 +0,0 @@
-#!/bin/sh
-# SPDX-License-Identifier: BSD-2-Clause
-# Copyright (c) 2026 Authors of shirudo
-#
-# t0003 -- util/chain pure helpers: sha256, hex encode/decode bounds
-# and constant-time compare. These underpin the measurement register;
-# a drift here silently breaks attestation.
-
-cd "$(dirname "$0")/.."
-. ./test-lib.sh
-
-H=helper/test-chain
-
-test_expect_success 'sha256("abc") matches the known FIPS-180 vector' '
- [ "$(./$H sha256 abc)" = \
- "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" ]
-'
-
-test_expect_success 'hex_decode accepts exact length, lower and upper case' '
- ./$H decode 0aFf 2 | grep -qx "rc=0" &&
- [ "$(./$H decode DEadBE 3 | tail -1)" = "deadbe" ]
-'
-
-test_expect_success 'hex_decode rejects wrong length -> EINVAL' '
- ./$H decode abc 1 | grep -qx "rc=-22" &&
- ./$H decode abcd 1 | grep -qx "rc=-22"
-'
-
-test_expect_success 'hex_decode rejects non-hex chars -> EINVAL' '
- ./$H decode zz 1 | grep -qx "rc=-22"
-'
-
-test_expect_success 'ct_eq: equal buffers -> 1, one-bit diff -> 0' '
- [ "$(./$H cteq 00ff 00ff)" = "1" ] &&
- [ "$(./$H cteq 00ff 01ff)" = "0" ]
-'
-
-test_done
diff --git a/testing/unit/chain.c b/testing/unit/chain.c
new file mode 100644
index 00000000..c59dc130
--- /dev/null
+++ b/testing/unit/chain.c
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+#include "chain.h"
+#include "hex.h"
+#include "unit.h"
+
+static void sha256_matches_fips_180_vector(void)
+{
+ static const char want[] =
+ "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad";
+ char hex[2 * 32 + 1];
+ __u8 d[32];
+
+ ASSERT_EQ(shirudo_sha256("abc", 3, d), 0);
+ hex_encode(d, sizeof(d), hex);
+ EXPECT_STREQ(hex, want);
+}
+
+static void ct_eq_tells_one_bit_apart(void)
+{
+ static const __u8 a[] = { 0x00, 0xff }, b[] = { 0x00, 0xff };
+ static const __u8 c[] = { 0x01, 0xff };
+
+ EXPECT_EQ(ct_eq(a, b, sizeof(a)), 1);
+ EXPECT_EQ(ct_eq(a, c, sizeof(a)), 0);
+}
+
+static const struct unit_case cases[] = {
+ UNIT_CASE(sha256_matches_fips_180_vector),
+ UNIT_CASE(ct_eq_tells_one_bit_apart),
+ {}
+};
+
+UNIT_SUITE(chain, cases);
diff --git a/testing/unit/hex.c b/testing/unit/hex.c
new file mode 100644
index 00000000..799ebf9f
--- /dev/null
+++ b/testing/unit/hex.c
@@ -0,0 +1,53 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+#include <errno.h>
+
+#include "hex.h"
+#include "unit.h"
+
+static void encode_is_lower_case(void)
+{
+ static const __u8 in[] = { 0x00, 0x0a, 0xde, 0xad, 0xbe, 0xff };
+ char out[2 * sizeof(in) + 1];
+
+ hex_encode(in, sizeof(in), out);
+ EXPECT_STREQ(out, "000adeadbeff");
+}
+
+static void decode_takes_either_case(void)
+{
+ static const __u8 want[] = { 0xde, 0xad, 0xbe };
+ __u8 out[3];
+
+ ASSERT_EQ(hex_decode("0aFf", out, 2), 0);
+ EXPECT_EQ(out[0], 0x0a);
+ EXPECT_EQ(out[1], 0xff);
+ ASSERT_EQ(hex_decode("DEadBE", out, 3), 0);
+ EXPECT_MEMEQ(out, want, sizeof(want));
+}
+
+static void decode_wants_the_exact_length(void)
+{
+ __u8 out[2];
+
+ EXPECT_EQ(hex_decode("abc", out, 1), -EINVAL);
+ EXPECT_EQ(hex_decode("abcd", out, 1), -EINVAL);
+}
+
+static void decode_refuses_non_hex(void)
+{
+ __u8 out[1];
+
+ EXPECT_EQ(hex_decode("zz", out, 1), -EINVAL);
+}
+
+static const struct unit_case cases[] = {
+ UNIT_CASE(encode_is_lower_case),
+ UNIT_CASE(decode_takes_either_case),
+ UNIT_CASE(decode_wants_the_exact_length),
+ UNIT_CASE(decode_refuses_non_hex),
+ {}
+};
+
+UNIT_SUITE(hex, cases);
diff --git a/testing/vm/build-bundle.sh b/testing/vm/build-bundle.sh
index f51709b1..d21be3f9 100755
--- a/testing/vm/build-bundle.sh
+++ b/testing/vm/build-bundle.sh
@@ -321,7 +321,6 @@ install -d "$TMP_DIR/test-helpers/testing/helper" \
while IFS= read -r helper; do
install -m 0755 "$REPO_ROOT/$helper" "$TMP_DIR/test-helpers/$helper"
done <<'EOF'
-testing/helper/test-chain
testing/helper/test-register
testing/helper/test-register-cli
testing/helper/test-config-attest