Skip to content

eBPF-based security platform

tpm: encode the NV writer public area and authorization digests

Leonard Cohnen63e658f09d45
diff --git a/include/shirudo/tpm.h b/include/shirudo/tpm.h
new file mode 100644
index 00000000..3980d6ce
--- /dev/null
+++ b/include/shirudo/tpm.h
@@ -0,0 +1,9 @@
+/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
+/* Copyright (c) 2026 Authors of shirudo */
+#ifndef SHIRUDO_TPM_TYPES_H
+#define SHIRUDO_TPM_TYPES_H
+
+#define SHIRUDO_TPM_NV_RSA_BYTES 384
+#define SHIRUDO_TPM_NV_RSA_PUBLIC_AREA_LEN (24 + SHIRUDO_TPM_NV_RSA_BYTES)
+
+#endif /* SHIRUDO_TPM_TYPES_H */
diff --git a/testing/Makefile b/testing/Makefile
index 96bfb92b..73e35dda 100644
--- a/testing/Makefile
+++ b/testing/Makefile
@@ -110,7 +110,8 @@ KERNEL_BUILD_STAMP := $(KERNEL_BUILD_ARTIFACT_DIR)/$(VM_KERNEL_IMAGE_NAME).sha25
TEST_VM_BUNDLE_STAMP := $(TEST_VM_BUNDLE_DIR)/.stamp
TEST_VM_BUNDLE_SOURCES := $(sort $(wildcard \
- $(ROOT)/*.c $(ROOT)/*.h $(ROOT)/include/*.h $(ROOT)/util/*.c $(ROOT)/util/*.h \
+ $(ROOT)/*.c $(ROOT)/*.h $(ROOT)/include/*.h $(ROOT)/include/shirudo/*.h \
+ $(ROOT)/util/*.c $(ROOT)/util/*.h \
$(ROOT)/bpf/*.c $(ROOT)/bpf/*.h $(ROOT)/bpf/lib/*.h \
$(ROOT)/tools/shirudo-build/*.c $(ROOT)/tools/shirudo-build/*.h \
$(ROOT)/vendor/libbpf/bpf/*.c $(ROOT)/vendor/libbpf/bpf/*.h \
diff --git a/testing/helper/test-tpm-wire.c b/testing/helper/test-tpm-wire.c
index cffb5a35..298e57cf 100644
--- a/testing/helper/test-tpm-wire.c
+++ b/testing/helper/test-tpm-wire.c
@@ -37,11 +37,66 @@ static int gethex(const char *hex, __u8 *out, size_t cap, size_t *len)
int main(int argc, char **argv)
{
+ __u8 public[SHIRUDO_TPM_NV_RSA_BYTES];
__u8 a[32], b[32], out[32], buf[2048];
size_t len;
if (argc < 2)
return 2;
+ if (strcmp(argv[1], "writer-public") == 0 && argc == 3) {
+ __u8 area[SHIRUDO_TPM_NV_RSA_PUBLIC_AREA_LEN];
+
+ if (hex_decode(argv[2], buf, SHIRUDO_TPM_NV_RSA_BYTES))
+ return 2;
+ tpm_writer_public(buf, area);
+ puthex(area, sizeof(area));
+ return 0;
+ }
+ if (strcmp(argv[1], "policy-signed") == 0 && argc == 4) {
+ if (hex_decode(argv[2], a, 32) ||
+ hex_decode(argv[3], public, sizeof(public)))
+ return 2;
+ if (tpm_policy_signed(a, public, out))
+ return 1;
+ puthex(out, 32);
+ return 0;
+ }
+ if (strcmp(argv[1], "extend-cphash") == 0 && argc == 4) {
+ if (hex_decode(argv[2], buf, SHIRUDO_TPM_NAME_LEN) ||
+ hex_decode(argv[3], a, 32))
+ return 2;
+ if (tpm_nv_extend_cphash(buf, a, out))
+ return 1;
+ puthex(out, 32);
+ return 0;
+ }
+ if (strcmp(argv[1], "writer-auth") == 0 && argc == 4) {
+ if (gethex(argv[2], buf, 64, &len) ||
+ hex_decode(argv[3], a, 32) ||
+ tpm_writer_auth_hash(buf, len, a, out))
+ return 2;
+ puthex(out, 32);
+ return 0;
+ }
+ if (strcmp(argv[1], "command-code") == 0 && argc == 4) {
+ __u32 command_code = (__u32)strtoul(argv[3], NULL, 0);
+
+ if (hex_decode(argv[2], a, 32))
+ return 2;
+ tpm_policy_command_code(a, command_code, out);
+ puthex(out, 32);
+ return 0;
+ }
+ if (strcmp(argv[1], "register-final") == 0 && argc == 5) {
+ if (hex_decode(argv[2], a, 32) ||
+ hex_decode(argv[3], b, 32) ||
+ hex_decode(argv[4], public, sizeof(public)))
+ return 2;
+ if (tpm_register_policy_final(a, b, public, out))
+ return 1;
+ puthex(out, 32);
+ return 0;
+ }
if (strcmp(argv[1], "nv-written") == 0 && argc == 4) {
if (hex_decode(argv[2], a, 32))
return 2;
diff --git a/testing/tpm/t0033-tpm-wire.sh b/testing/tpm/t0033-tpm-wire.sh
index 1536f408..da560bb5 100644
--- a/testing/tpm/t0033-tpm-wire.sh
+++ b/testing/tpm/t0033-tpm-wire.sh
@@ -13,6 +13,18 @@ H=helper/test-tpm-wire
Z=0000000000000000000000000000000000000000000000000000000000000000
py() { python3 -c "$1"; }
+PUB=$(py 'import struct; print((struct.pack(">HHIHHHHHIH", 1, 11, 0x40000, 0, 16, 20, 11, 3072, 65537, 384) + bytes.fromhex("80" + "00"*382 + "01")).hex())')
+MODULUS=$(py 'print("80" + "00"*382 + "01")')
+
+test_expect_success 'the writer public area fixes RSA-3072, RSASSA and SHA-256' '
+ [ "$(./$H writer-public "$MODULUS")" = "$PUB" ] &&
+ zero=$(py "print(\"00\"*384)") &&
+ even=$(py "print(\"80\" + \"00\"*383)") &&
+ [ "$(./$H writer-public "$zero")" = "$(printf "%s" "$PUB" | cut -c1-48)$zero" ] &&
+ [ "$(./$H writer-public "$even")" = "$(printf "%s" "$PUB" | cut -c1-48)$even" ] &&
+ ! ./$H writer-public 00 &&
+ ! ./$H writer-public "$PUB"
+'
test_expect_success 'PolicyNvWritten(YES) = H(prev || CC || 01)' '
want=$(py "import hashlib; print(hashlib.sha256(bytes(32) + bytes.fromhex(\"0000018f01\")).hexdigest())") &&
@@ -31,16 +43,67 @@ test_expect_success 'PolicyOR starts from zero and folds both branches' '
[ "$(./$H or $a $b)" = "$want" ]
'
-test_expect_success 'register policy = OR(written, unwritten+PCR(H(values)))' '
+test_expect_success 'PolicySigned binds the writer Name and policy reference' '
+ p=$(py "print(\"12\"*32)") &&
+ want=$(py "import hashlib; name=bytes.fromhex(\"000b\")+hashlib.sha256(bytes.fromhex(\"$PUB\")).digest(); d=hashlib.sha256(bytes.fromhex(\"$p\")+bytes.fromhex(\"00000160\")+name).digest(); print(hashlib.sha256(d+b\"shirudo-nv-writer-v1\").hexdigest())") &&
+ [ "$(./$H policy-signed $p $MODULUS)" = "$want" ]
+'
+
+test_expect_success 'PolicyCommandCode folds the authorized command as a u32' '
+ p=$(py "print(\"34\"*32)") &&
+ want=$(py "import hashlib; print(hashlib.sha256(bytes.fromhex(\"34\"*32 + \"0000016c00000136\")).hexdigest())") &&
+ [ "$(./$H command-code $p 0x136)" = "$want" ] &&
+ [ "$(./$H command-code $p 0x137)" != "$want" ]
+'
+
+test_expect_success 'register final policy requires the writer and only NV_Extend' '
+ a=$(py "print(\"11\"*32)") && b=$(py "print(\"22\"*32)") &&
+ want=$(py "
+import hashlib
+digest = hashlib.sha256(bytes(32) + bytes.fromhex(\"00000171\" + \"11\"*32 + \"22\"*32)).digest()
+name = bytes.fromhex(\"000b\") + hashlib.sha256(bytes.fromhex(\"$PUB\")).digest()
+digest = hashlib.sha256(digest + bytes.fromhex(\"00000160\") + name).digest()
+digest = hashlib.sha256(digest + b\"shirudo-nv-writer-v1\").digest()
+print(hashlib.sha256(digest + bytes.fromhex(\"0000016c00000136\")).hexdigest())
+") &&
+ [ "$(./$H register-final $a $b $MODULUS)" = "$want" ] &&
+ [ "$(./$H or $a $b)" != "$want" ]
+'
+
+test_expect_success 'register policy branches match independent vectors' '
v9=$(py "print(\"cd\"*32)") && v11=$(py "print(\"ef\"*32)") &&
out=$(./$H register-policy 0xa00 $v9 $v11) &&
- w=$(echo "$out" | sed -n 1p) && i=$(echo "$out" | sed -n 2p) &&
- p=$(echo "$out" | sed -n 3p) &&
- [ "$w" = "$(./$H nv-written $Z 1)" ] &&
- unwritten=$(./$H nv-written $Z 0) &&
- vd=$(py "import hashlib; print(hashlib.sha256(bytes.fromhex(\"cd\"*32 + \"ef\"*32)).hexdigest())") &&
- [ "$i" = "$(./$H pcr $unwritten 0xa00 $vd)" ] &&
- [ "$p" = "$(./$H or $w $i)" ]
+ want=$(py "
+import hashlib
+def h(data):
+ return hashlib.sha256(data).digest()
+written = h(bytes(32) + bytes.fromhex(\"0000018f01\"))
+unwritten = h(bytes(32) + bytes.fromhex(\"0000018f00\"))
+values = h(bytes.fromhex(\"cd\"*32 + \"ef\"*32))
+init = h(unwritten + bytes.fromhex(\"0000017f00000001000b03000a00\") + values)
+policy = h(bytes(32) + bytes.fromhex(\"00000171\") + written + init)
+print(written.hex())
+print(init.hex())
+print(policy.hex())
+") &&
+ [ "$out" = "$want" ]
+'
+
+test_expect_success 'NV_Extend cpHash includes both Names and the TPM2B event' '
+ name=000b$(py "print(\"ab\"*32)") &&
+ event=$(py "print(\"cd\"*32)") &&
+ want=$(py "import hashlib; print(hashlib.sha256(bytes.fromhex(\"00000136$name$name\"+\"0020\"+\"$event\")).hexdigest())") &&
+ [ "$(./$H extend-cphash "$name" "$event")" = "$want" ]
+'
+
+test_expect_success 'writer authorization covers nonce, zero expiry, cpHash and reference' '
+ cp=$(py "print(\"cd\"*32)") &&
+ for length in 1 32 64; do
+ nonce=$(py "print(\"ab\"*$length)") &&
+ want=$(py "import hashlib; print(hashlib.sha256(bytes.fromhex(\"$nonce\"+\"00000000\"+\"$cp\")+b\"shirudo-nv-writer-v1\").hexdigest())") &&
+ [ "$(./$H writer-auth "$nonce" "$cp")" = "$want" ] || exit 1
+ done &&
+ ! ./$H writer-auth "" "$cp"
'
test_expect_success 'NV name = 000b || H(TPMS_NV_PUBLIC)' '
diff --git a/util/tpm_wire.c b/util/tpm_wire.c
index 27179909..494a052e 100644
--- a/util/tpm_wire.c
+++ b/util/tpm_wire.c
@@ -14,6 +14,8 @@
#include "tpm_wire.h"
#define TPM_CC_POLICY_OR 0x00000171u
+#define TPM_CC_POLICY_COMMAND_CODE 0x0000016Cu
+#define TPM_CC_POLICY_SIGNED 0x00000160u
#define TPM_CC_POLICY_PCR 0x0000017Fu
#define TPM_CC_POLICY_NV_WRITTEN 0x0000018Fu
@@ -148,6 +150,98 @@ void tpm_policy_or(const __u8 a[32], const __u8 b[32],
policy_hash(zero, TPM_CC_POLICY_OR, a, 32, b, 32, out);
}
+/*
+ * This fixed TPMT_PUBLIC adds a 24-byte prefix to the public-key bytes.
+ * It excludes the outer TPM2B_PUBLIC size field.
+ */
+void tpm_writer_public(const __u8 modulus[SHIRUDO_TPM_NV_RSA_BYTES],
+ __u8 out[SHIRUDO_TPM_NV_RSA_PUBLIC_AREA_LEN])
+{
+ tpm_put16(out, SHIRUDO_TPM_ALG_RSA); /* type */
+ tpm_put16(out + 2, SHIRUDO_TPM_ALG_SHA256); /* nameAlg */
+ tpm_put32(out + 4, 0x00040000); /* TPMA_OBJECT_SIGN_ENCRYPT */
+ tpm_put16(out + 8, 0); /* Empty authPolicy: size only. */
+ tpm_put16(out + 10, SHIRUDO_TPM_ALG_NULL); /* symmetric.algorithm */
+ tpm_put16(out + 12, SHIRUDO_TPM_ALG_RSASSA); /* scheme.scheme */
+ tpm_put16(out + 14, SHIRUDO_TPM_ALG_SHA256); /* scheme hash */
+ tpm_put16(out + 16, 3072); /* RSA keyBits */
+ tpm_put32(out + 18, 65537); /* RSA exponent F4: 2^16 + 1. */
+ /* unique.rsa: two-byte size at offset 22, public-key bytes at offset 24. */
+ tpm_put2b(out + 22, modulus, SHIRUDO_TPM_NV_RSA_BYTES);
+}
+
+int tpm_nv_extend_cphash(const __u8 name[SHIRUDO_TPM_NAME_LEN],
+ const __u8 event[32], __u8 out[32])
+{
+ __u8 command[4 + 2 * SHIRUDO_TPM_NAME_LEN + 2 + 32];
+
+ tpm_put32(command, SHIRUDO_TPM_CC_NV_EXTEND);
+ memcpy(command + 4, name, SHIRUDO_TPM_NAME_LEN);
+ memcpy(command + 4 + SHIRUDO_TPM_NAME_LEN, name, SHIRUDO_TPM_NAME_LEN);
+ tpm_put2b(command + 4 + 2 * SHIRUDO_TPM_NAME_LEN, event, 32);
+ return sha256_buffer(command, sizeof(command), out);
+}
+
+int tpm_writer_auth_hash(const __u8 *nonce, size_t nonce_len,
+ const __u8 cphash[32], __u8 out[32])
+{
+ __u8 bytes[64 + 4 + 32 + sizeof(SHIRUDO_TPM_NV_POLICY_REF) - 1];
+
+ if (!nonce_len || nonce_len > 64)
+ return -EINVAL;
+ memcpy(bytes, nonce, nonce_len);
+ tpm_put32(bytes + nonce_len, 0);
+ memcpy(bytes + nonce_len + 4, cphash, 32);
+ memcpy(bytes + nonce_len + 4 + 32, SHIRUDO_TPM_NV_POLICY_REF,
+ sizeof(SHIRUDO_TPM_NV_POLICY_REF) - 1);
+ return sha256_buffer(bytes, nonce_len + 4 + 32 + sizeof(SHIRUDO_TPM_NV_POLICY_REF) - 1,
+ out);
+}
+
+int tpm_policy_signed(const __u8 prev[32],
+ const __u8 writer_public[SHIRUDO_TPM_NV_RSA_BYTES],
+ __u8 out[32])
+{
+ __u8 public[SHIRUDO_TPM_NV_RSA_PUBLIC_AREA_LEN];
+ __u8 bytes[32 + sizeof(SHIRUDO_TPM_NV_POLICY_REF) - 1];
+ __u8 name[SHIRUDO_TPM_NAME_LEN];
+ int rc;
+
+ tpm_writer_public(writer_public, public);
+ rc = tpm_object_name(public, sizeof(public), name);
+ if (rc)
+ return rc;
+ policy_hash(prev, TPM_CC_POLICY_SIGNED, name, sizeof(name), NULL, 0, out);
+ memcpy(bytes, out, 32);
+ memcpy(bytes + 32, SHIRUDO_TPM_NV_POLICY_REF,
+ sizeof(SHIRUDO_TPM_NV_POLICY_REF) - 1);
+ return sha256_buffer(bytes, sizeof(bytes), out);
+}
+
+void tpm_policy_command_code(const __u8 prev[32],
+ __u32 command_code, __u8 out[32])
+{
+ __u8 cc_be[4];
+
+ tpm_put32(cc_be, command_code);
+ policy_hash(prev, TPM_CC_POLICY_COMMAND_CODE, cc_be, sizeof(cc_be), NULL, 0, out);
+}
+
+int tpm_register_policy_final(const __u8 branch_written[32],
+ const __u8 branch_init[32],
+ const __u8 writer_public[SHIRUDO_TPM_NV_RSA_BYTES],
+ __u8 policy[32])
+{
+ int rc;
+
+ tpm_policy_or(branch_written, branch_init, policy);
+ rc = tpm_policy_signed(policy, writer_public, policy);
+ if (rc)
+ return rc;
+ tpm_policy_command_code(policy, SHIRUDO_TPM_CC_NV_EXTEND, policy);
+ return 0;
+}
+
void tpm_register_policy(__u32 pcr_mask,
const __u8 *pcr_values, size_t n_values,
__u8 branch_written[32],
diff --git a/util/tpm_wire.h b/util/tpm_wire.h
index aa7c6c51..0d8a3b79 100644
--- a/util/tpm_wire.h
+++ b/util/tpm_wire.h
@@ -14,6 +14,7 @@
#include <string.h>
#include <shirudo/misc.h>
+#include <shirudo/tpm.h>
static inline void tpm_put16(__u8 *p, __u16 v)
{
@@ -44,6 +45,7 @@ static inline size_t tpm_put2b(__u8 *p, const __u8 *data, size_t len)
#define SHIRUDO_TPM_ALG_SHA256 0x000Bu
#define SHIRUDO_TPM_ALG_ECC 0x0023u
#define SHIRUDO_TPM_ALG_ECDSA 0x0018u
+#define SHIRUDO_TPM_ALG_RSASSA 0x0014u
#define SHIRUDO_TPM_ALG_NULL 0x0010u
#define SHIRUDO_TPM_ALG_CFB 0x0043u
#define SHIRUDO_TPM_ECC_NIST_P256 0x0003u
@@ -52,6 +54,25 @@ static inline size_t tpm_put2b(__u8 *p, const __u8 *data, size_t len)
#define SHIRUDO_TPM_GENERATED 0xFF544347u
#define SHIRUDO_TPM_PCR_SELECT_LEN 3
#define SHIRUDO_TPM_PCR_SELECTION_LEN (7 + SHIRUDO_TPM_PCR_SELECT_LEN)
+#define SHIRUDO_TPM_CC_NV_EXTEND 0x00000136u
+#define SHIRUDO_TPM_NV_POLICY_REF "shirudo-nv-writer-v1"
+
+void tpm_writer_public(const __u8 modulus[SHIRUDO_TPM_NV_RSA_BYTES],
+ __u8 out[SHIRUDO_TPM_NV_RSA_PUBLIC_AREA_LEN]);
+int tpm_nv_extend_cphash(const __u8 name[SHIRUDO_TPM_NAME_LEN],
+ const __u8 event[32], __u8 out[32]);
+int tpm_writer_auth_hash(const __u8 *nonce, size_t nonce_len,
+ const __u8 cphash[32], __u8 out[32]);
+int tpm_policy_signed(const __u8 prev[32],
+ const __u8 writer_public[SHIRUDO_TPM_NV_RSA_BYTES],
+ __u8 out[32]);
+void tpm_policy_command_code(const __u8 prev[SHIRUDO_TPM_DIGEST_LEN],
+ __u32 command_code,
+ __u8 out[SHIRUDO_TPM_DIGEST_LEN]);
+int tpm_register_policy_final(const __u8 branch_written[32],
+ const __u8 branch_init[32],
+ const __u8 writer_public[SHIRUDO_TPM_NV_RSA_BYTES],
+ __u8 policy[32]);
/*
* Attributes of the measurement register: an extend-only NV index