Skip to content

eBPF-based security platform

tpm_wire: encode and decode big-endian with <shirudo/byteorder.h>

Daniel Borkmann779044f38a8c
diff --git a/include/shirudo/byteorder.h b/include/shirudo/byteorder.h
index 7990e75f..76cd4698 100644
--- a/include/shirudo/byteorder.h
+++ b/include/shirudo/byteorder.h
@@ -2,7 +2,7 @@
/*
* Copyright (c) 2026 Authors of shirudo
*
- * Little-endian conversions under the kernel's names, for userspace.
+ * Byte-order conversions under the kernel's names, for userspace.
*/
#ifndef SHIRUDO_BYTEORDER_H
@@ -15,8 +15,8 @@
#include <shirudo/types.h>
/*
- * Between host order and the restricted __le types; the __force casts
- * are the one place sparse lets a value change between the two.
+ * Between host order and the restricted __le and __be types; the __force
+ * casts are the one place sparse lets a value change between the two.
*/
static inline __le16 cpu_to_le16(__u16 v)
{
@@ -48,8 +48,29 @@ static inline __u64 le64_to_cpu(__le64 v)
return le64toh((__force __u64)v);
}
+static inline __be16 cpu_to_be16(__u16 v)
+{
+ return (__force __be16)htobe16(v);
+}
+
+static inline __be32 cpu_to_be32(__u32 v)
+{
+ return (__force __be32)htobe32(v);
+}
+
+static inline __u16 be16_to_cpu(__be16 v)
+{
+ return be16toh((__force __u16)v);
+}
+
+static inline __u32 be32_to_cpu(__be32 v)
+{
+ return be32toh((__force __u32)v);
+}
+
/*
- * From bytes as they lie in a file, at any alignment: the copy is right
+ * From and into bytes as they lie in a file or on the TPM's wire, at any
+ * alignment: the copy is right
* on every architecture, and the compiler makes it one load where the
* target allows a misaligned one.
*/
@@ -77,4 +98,34 @@ static inline __u64 get_le64(const void *p)
return le64_to_cpu(v);
}
+static inline __u16 get_be16(const void *p)
+{
+ __be16 v;
+
+ memcpy(&v, p, sizeof(v));
+ return be16_to_cpu(v);
+}
+
+static inline __u32 get_be32(const void *p)
+{
+ __be32 v;
+
+ memcpy(&v, p, sizeof(v));
+ return be32_to_cpu(v);
+}
+
+static inline void put_be16(void *p, __u16 v)
+{
+ __be16 b = cpu_to_be16(v);
+
+ memcpy(p, &b, sizeof(b));
+}
+
+static inline void put_be32(void *p, __u32 v)
+{
+ __be32 b = cpu_to_be32(v);
+
+ memcpy(p, &b, sizeof(b));
+}
+
#endif /* SHIRUDO_BYTEORDER_H */
diff --git a/testing/helper/test-nv-writer.c b/testing/helper/test-nv-writer.c
index ed16f023..0106c32e 100644
--- a/testing/helper/test-nv-writer.c
+++ b/testing/helper/test-nv-writer.c
@@ -149,10 +149,10 @@ static int write_extend(int fd, const char *device, enum extend_probe probe)
index++;
if (probe == EXTEND_SHORT)
size = 10;
- tpm_put32(command + 2, (__u32)size);
- tpm_put32(command + 6, TPM_CC_NV_EXTEND);
- tpm_put32(command + 10, index);
- tpm_put32(command + 14, index);
+ put_be32(command + 2, (__u32)size);
+ put_be32(command + 6, TPM_CC_NV_EXTEND);
+ put_be32(command + 10, index);
+ put_be32(command + 14, index);
errno = 0;
written = write(fd, command, size);
saved_errno = errno;
diff --git a/testing/helper/test-tpm-preopened.c b/testing/helper/test-tpm-preopened.c
index 6b36a92e..1058e7dc 100644
--- a/testing/helper/test-tpm-preopened.c
+++ b/testing/helper/test-tpm-preopened.c
@@ -29,10 +29,10 @@ static int write_extend(int fd)
ssize_t written;
int saved_errno;
- tpm_put32(command + 2, (__u32)sizeof(command));
- tpm_put32(command + 6, TPM_CC_NV_EXTEND);
- tpm_put32(command + 10, NV_INDEX);
- tpm_put32(command + 14, NV_INDEX);
+ put_be32(command + 2, (__u32)sizeof(command));
+ put_be32(command + 6, TPM_CC_NV_EXTEND);
+ put_be32(command + 10, NV_INDEX);
+ put_be32(command + 14, NV_INDEX);
errno = 0;
written = write(fd, command, sizeof(command));
saved_errno = errno;
diff --git a/util/tpm.c b/util/tpm.c
index dd3c587a..d5126e4f 100644
--- a/util/tpm.c
+++ b/util/tpm.c
@@ -909,9 +909,9 @@ static int marshal_signature(const TPMT_SIGNATURE *sig, __u8 *out,
if (sig->sigAlg != TPM2_ALG_ECDSA || e->signatureR.size > 64 ||
e->signatureS.size > 64 || cap < 8u + 128u)
return -EINVAL;
- tpm_put16(out + off, sig->sigAlg);
+ put_be16(out + off, sig->sigAlg);
off += 2;
- tpm_put16(out + off, e->hash);
+ put_be16(out + off, e->hash);
off += 2;
off += tpm_put2b(out + off, e->signatureR.buffer, e->signatureR.size);
off += tpm_put2b(out + off, e->signatureS.buffer, e->signatureS.size);
diff --git a/util/tpm_wire.c b/util/tpm_wire.c
index c75f9d7c..a46bd02b 100644
--- a/util/tpm_wire.c
+++ b/util/tpm_wire.c
@@ -38,7 +38,7 @@ static int get16(struct reader *r, __u16 *v)
{
if (r->left < 2)
return -EINVAL;
- *v = (__u16)(((__u16)r->p[0] << 8) | r->p[1]);
+ *v = get_be16(r->p);
r->p += 2;
r->left -= 2;
return 0;
@@ -48,8 +48,7 @@ static int get32(struct reader *r, __u32 *v)
{
if (r->left < 4)
return -EINVAL;
- *v = ((__u32)r->p[0] << 24) | ((__u32)r->p[1] << 16) |
- ((__u32)r->p[2] << 8) | r->p[3];
+ *v = get_be32(r->p);
r->p += 4;
r->left -= 4;
return 0;
@@ -98,7 +97,7 @@ static int policy_hash(const __u8 prev[32], __u32 cc,
__u8 cc_be[4];
/* prev and out may be one buffer; out changes only once done. */
- tpm_put32(cc_be, cc);
+ put_be32(cc_be, cc);
if (sha256_init(&ctx) != 0) {
memset(out, 0, 32);
return -EIO;
@@ -128,8 +127,8 @@ static void
shirudo_tpm_pcr_selection(__u32 pcr_mask,
__u8 out[TPM_PCR_SELECTION_LEN])
{
- tpm_put32(out, 1);
- tpm_put16(out + 4, TPM_ALG_SHA256);
+ put_be32(out, 1);
+ put_be16(out + 4, TPM_ALG_SHA256);
out[6] = TPM_PCR_SELECT_LEN;
out[7] = (__u8)pcr_mask;
out[8] = (__u8)(pcr_mask >> 8);
@@ -160,15 +159,15 @@ int tpm_policy_or(const __u8 a[32], const __u8 b[32], __u8 out[32])
void tpm_writer_public(const __u8 modulus[TPM_NV_RSA_BYTES],
__u8 out[TPM_NV_RSA_PUBLIC_AREA_LEN])
{
- tpm_put16(out, TPM_ALG_RSA); /* type */
- tpm_put16(out + 2, TPM_ALG_SHA256); /* nameAlg */
- tpm_put32(out + 4, TPM_WRITER_ATTRS);
- tpm_put16(out + 8, 0); /* Empty authPolicy: size only. */
- tpm_put16(out + 10, TPM_ALG_NULL); /* symmetric.algorithm */
- tpm_put16(out + 12, TPM_ALG_RSASSA); /* scheme.scheme */
- tpm_put16(out + 14, TPM_ALG_SHA256); /* scheme hash */
- tpm_put16(out + 16, 3072); /* RSA keyBits */
- tpm_put32(out + 18, 65537); /* RSA exponent F4: 2^16 + 1. */
+ put_be16(out, TPM_ALG_RSA); /* type */
+ put_be16(out + 2, TPM_ALG_SHA256); /* nameAlg */
+ put_be32(out + 4, TPM_WRITER_ATTRS);
+ put_be16(out + 8, 0); /* Empty authPolicy: size only. */
+ put_be16(out + 10, TPM_ALG_NULL); /* symmetric.algorithm */
+ put_be16(out + 12, TPM_ALG_RSASSA); /* scheme.scheme */
+ put_be16(out + 14, TPM_ALG_SHA256); /* scheme hash */
+ put_be16(out + 16, 3072); /* RSA keyBits */
+ put_be32(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, TPM_NV_RSA_BYTES);
}
@@ -178,7 +177,7 @@ int tpm_nv_extend_cphash(const __u8 name[TPM_NAME_LEN],
{
__u8 command[4 + 2 * TPM_NAME_LEN + 2 + 32];
- tpm_put32(command, TPM_CC_NV_EXTEND);
+ put_be32(command, TPM_CC_NV_EXTEND);
memcpy(command + 4, name, TPM_NAME_LEN);
memcpy(command + 4 + TPM_NAME_LEN, name, TPM_NAME_LEN);
tpm_put2b(command + 4 + 2 * TPM_NAME_LEN, event, 32);
@@ -193,7 +192,7 @@ int tpm_writer_auth_hash(const __u8 *nonce, size_t nonce_len,
if (!nonce_len || nonce_len > 64)
return -EINVAL;
memcpy(bytes, nonce, nonce_len);
- tpm_put32(bytes + nonce_len, 0);
+ put_be32(bytes + nonce_len, 0);
memcpy(bytes + nonce_len + 4, cphash, 32);
memcpy(bytes + nonce_len + 4 + 32, TPM_NV_POLICY_REF,
sizeof(TPM_NV_POLICY_REF) - 1);
@@ -229,7 +228,7 @@ int tpm_policy_command_code(const __u8 prev[32],
{
__u8 cc_be[4];
- tpm_put32(cc_be, command_code);
+ put_be32(cc_be, command_code);
return policy_hash(prev, TPM_CC_POLICY_COMMAND_CODE, cc_be,
sizeof(cc_be), NULL, 0, out);
}
@@ -280,13 +279,13 @@ void tpm_nv_name(__u32 index, __u32 attrs,
{
__u8 pub[4 + 2 + 4 + 2 + 32 + 2];
- tpm_put32(pub, index);
- tpm_put16(pub + 4, TPM_ALG_SHA256);
- tpm_put32(pub + 6, attrs);
- tpm_put16(pub + 10, 32);
+ put_be32(pub, index);
+ put_be16(pub + 4, TPM_ALG_SHA256);
+ put_be32(pub + 6, attrs);
+ put_be16(pub + 10, 32);
memcpy(pub + 12, policy, 32);
- tpm_put16(pub + 44, data_size);
- tpm_put16(name, TPM_ALG_SHA256);
+ put_be16(pub + 44, data_size);
+ put_be16(name, TPM_ALG_SHA256);
if (sha256_buffer(pub, sizeof(pub), name + 2))
memset(name, 0, TPM_NAME_LEN);
}
@@ -300,7 +299,7 @@ int tpm_object_name(const __u8 *public, size_t len,
if (get16(&r, &type) || get16(&r, &alg) ||
alg != TPM_ALG_SHA256)
return -EINVAL;
- tpm_put16(name, TPM_ALG_SHA256);
+ put_be16(name, TPM_ALG_SHA256);
sha256_buffer(public, len, name + 2);
return 0;
}
@@ -311,9 +310,9 @@ void tpm_qualified_name(__u32 hierarchy,
{
__u8 buf[4 + TPM_NAME_LEN];
- tpm_put32(buf, hierarchy);
+ put_be32(buf, hierarchy);
memcpy(buf + 4, name, TPM_NAME_LEN);
- tpm_put16(out, TPM_ALG_SHA256);
+ put_be16(out, TPM_ALG_SHA256);
sha256_buffer(buf, sizeof(buf), out + 2);
}
@@ -394,35 +393,35 @@ int tpm_public_marshal(const struct shirudo_tpm_public *pub,
return -EINVAL;
if (cap < need)
return -E2BIG;
- tpm_put16(out + off, pub->type);
+ put_be16(out + off, pub->type);
off += 2;
- tpm_put16(out + off, TPM_ALG_SHA256);
+ put_be16(out + off, TPM_ALG_SHA256);
off += 2;
- tpm_put32(out + off, pub->attrs);
+ put_be32(out + off, pub->attrs);
off += 4;
off += tpm_put2b(out + off, pub->policy, pub->policy_len);
- tpm_put16(out + off, pub->sym_alg);
+ put_be16(out + off, pub->sym_alg);
off += 2;
if (pub->sym_alg != TPM_ALG_NULL) {
- tpm_put16(out + off, pub->sym_bits);
- tpm_put16(out + off + 2, pub->sym_mode);
+ put_be16(out + off, pub->sym_bits);
+ put_be16(out + off + 2, pub->sym_mode);
off += 4;
}
- tpm_put16(out + off, pub->scheme);
+ put_be16(out + off, pub->scheme);
off += 2;
if (pub->scheme != TPM_ALG_NULL) {
- tpm_put16(out + off, pub->scheme_hash);
+ put_be16(out + off, pub->scheme_hash);
off += 2;
}
if (pub->type == TPM_ALG_ECC) {
- tpm_put16(out + off, pub->curve);
- tpm_put16(out + off + 2, TPM_ALG_NULL);
+ put_be16(out + off, pub->curve);
+ put_be16(out + off + 2, TPM_ALG_NULL);
off += 4;
off += tpm_put2b(out + off, pub->x, 32);
off += tpm_put2b(out + off, pub->y, 32);
} else {
- tpm_put16(out + off, pub->rsa_bits);
- tpm_put32(out + off + 2, pub->rsa_exponent);
+ put_be16(out + off, pub->rsa_bits);
+ put_be32(out + off + 2, pub->rsa_exponent);
off += 6;
off += tpm_put2b(out + off, pub->n, pub->n_len);
}
@@ -466,7 +465,7 @@ int tpm_kdfa(const __u8 *key, size_t key_len, const char *label,
v_len > TPM_NAME_LEN || out_len > 4096)
return -EINVAL;
for (i = 1; done < out_len; i++) {
- tpm_put32(buf, i);
+ put_be32(buf, i);
off = 4;
memcpy(buf + off, label, label_len);
off += label_len;
@@ -477,7 +476,7 @@ int tpm_kdfa(const __u8 *key, size_t key_len, const char *label,
if (v_len)
memcpy(buf + off, ctx_v, v_len);
off += v_len;
- tpm_put32(buf + off, (__u32)(out_len * 8));
+ put_be32(buf + off, (__u32)(out_len * 8));
off += 4;
if (hmac_sha256(key, key_len, buf, off, block))
return -EIO;
@@ -500,7 +499,7 @@ void tpm_kdfe(const __u8 z[32], const char *label,
memset(out, 0, 32);
return;
}
- tpm_put32(buf, 1);
+ put_be32(buf, 1);
memcpy(buf + off, z, 32);
off += 32;
memcpy(buf + off, label, label_len);
@@ -529,7 +528,7 @@ int tpm_credential_protect(const __u8 seed[32],
if (tpm_kdfa(seed, 32, "STORAGE", name, TPM_NAME_LEN,
NULL, 0, sym_key, sizeof(sym_key)))
goto out;
- tpm_put16(plain, TPM_CREDENTIAL_LEN);
+ put_be16(plain, TPM_CREDENTIAL_LEN);
memcpy(plain + 2, credential, TPM_CREDENTIAL_LEN);
if (aes128_cfb_encrypt(sym_key, plain, sizeof(plain), enc) ||
tpm_kdfa(seed, 32, "INTEGRITY", NULL, 0, NULL, 0, hmac_key,
@@ -540,7 +539,7 @@ int tpm_credential_protect(const __u8 seed[32],
if (hmac_sha256(hmac_key, sizeof(hmac_key), mac_in,
sizeof(mac_in), mac))
goto out;
- tpm_put16(out, 32);
+ put_be16(out, 32);
memcpy(out + 2, mac, 32);
memcpy(out + 34, enc, sizeof(enc));
rc = 0;
diff --git a/util/tpm_wire.h b/util/tpm_wire.h
index ba8e4ca6..a4b60ea2 100644
--- a/util/tpm_wire.h
+++ b/util/tpm_wire.h
@@ -13,27 +13,14 @@
#include <stddef.h>
#include <string.h>
+#include <shirudo/byteorder.h>
#include <shirudo/misc.h>
#include <shirudo/tpm.h>
-static inline void tpm_put16(__u8 *p, __u16 v)
-{
- p[0] = (__u8)(v >> 8);
- p[1] = (__u8)v;
-}
-
-static inline void tpm_put32(__u8 *p, __u32 v)
-{
- p[0] = (__u8)(v >> 24);
- p[1] = (__u8)(v >> 16);
- p[2] = (__u8)(v >> 8);
- p[3] = (__u8)v;
-}
-
/* A TPM2B: the size, then the bytes. */
static inline size_t tpm_put2b(__u8 *p, const __u8 *data, size_t len)
{
- tpm_put16(p, (__u16)len);
+ put_be16(p, (__u16)len);
memcpy(p + 2, data, len);
return 2 + len;
}