Skip to content

eBPF-based security platform

chain: add the history root step of a upatch tree

John Fastabend0b8e95bc5c27
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rst
index ce0fcfdc..9949e724 100644
--- a/Documentation/contribute/testing/proof-coverage.rst
+++ b/Documentation/contribute/testing/proof-coverage.rst
@@ -744,14 +744,15 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.
digits; a refusal marks nothing
* - ``chain-events``
- ``chain_upatch_event()``, ``chain_key_event()``,
- ``chain_tree_event()``, ``chain_fold()``, ``chain_init_event()`` of
- an empty set
+ ``chain_tree_event()``, ``chain_fold()``,
+ ``chain_history_step()``, ``chain_init_event()`` of an empty set
- Names of 0 to 8 and 255 to 257 bytes; every payload, digest,
- sequence number and register
+ sequence number, register and history root
- SHA-256 is given exactly the bytes of the Lean model
``Shirudo.Events``: the tag and its NUL, then the fixed fields, the
name cut at 256 bytes and the sequence number most significant byte
- first; the event is the digest, or zeros with the error
+ first; the event, or the next history root, is the digest, or zeros
+ with the error
* - ``measured-fold-model``
- ``measured_event()``, ``measured_holds()``
- Every sequence of up to six init, load and unload records over two
@@ -948,12 +949,19 @@ the model follows the C.
registers come from the same events, tagged the same, in the same
order; ``register_injective_on``: the same with the hash injective
only on the pairs the two sequences fold; ``register_eq_foldl``: the
- register folds the events in the order they came
+ register folds the events in the order they came;
+ ``history_ignores_outcomes``: a upatch tree's history root is the
+ same on two nodes that processed operations of the same digests,
+ whatever outcome each had; ``processAll_history``,
+ ``historyRoot_append_one``: it takes one step per operation
- The hash is injective on the (register, event) pairs (for
``register_injective_on`` only those folded) and never yields the
initial value; ``register_tagged_injective`` also takes the tagged
- encoding as injective, which ``Shirudo.Events`` proves
- - Review
+ encoding as injective, which ``Shirudo.Events`` proves; none for
+ the history root
+ - Review; the bytes of the history root's step are
+ ``historyBytes`` of ``Shirudo.Events``, which CBMC,
+ ``chain-events``, checks
* - ``Shirudo.Events``, the event encodings of :src:`util/chain.c`
- ``bytes_injective``: two admitted events hash the same bytes only
when they are the same event; ``listing_injective``: the init
diff --git a/testing/proofs/cbmc/chain-events.c b/testing/proofs/cbmc/chain-events.c
index b619f8f1..9e0a05a1 100644
--- a/testing/proofs/cbmc/chain-events.c
+++ b/testing/proofs/cbmc/chain-events.c
@@ -6,9 +6,9 @@
* Shirudo/Events.lean, with SHA-256 modelled as recording what it is
* given: for every name of LEN bytes, payload, digest, sequence number
* and register, chain_upatch_event(), chain_key_event(),
- * chain_tree_event(), chain_fold() and chain_init_event() of an empty set
- * hand SHA-256 exactly the bytes of the model, and give its digest, or
- * zeros and its error.
+ * chain_tree_event(), chain_fold(), chain_history_step() and
+ * chain_init_event() of an empty set hand SHA-256 exactly the bytes of
+ * the model, and give its digest, or zeros and its error.
*/
#include "proof.h"
@@ -233,6 +233,24 @@ static void fold(void)
check_result(ret, 1, chain);
}
+/* historyBytes: the tag and its NUL, the root before and the digest. */
+static void history_step(void)
+{
+ __u8 *prev = any_bytes(32), *operation = any_bytes(32), root[32];
+ size_t at = 0;
+ int ret;
+
+ start();
+ ret = chain_history_step(prev, operation, root);
+ if (!ret) {
+ check_tag("shirudo-tree-history", &at);
+ check_bytes(prev, 32, &at);
+ check_bytes(operation, 32, &at);
+ check_end(at);
+ }
+ check_result(ret, 1, root);
+}
+
/*
* chain_init_event() of an empty set: initBytes, the tag and its NUL, the
* certificate digest, the digest of the empty listing and the tree root.
@@ -270,5 +288,6 @@ void harness(void)
key_event();
tree_event();
fold();
+ history_step();
init_event();
}
diff --git a/testing/proofs/lean4/Shirudo/Chain.lean b/testing/proofs/lean4/Shirudo/Chain.lean
index be18f711..b20bacdc 100644
--- a/testing/proofs/lean4/Shirudo/Chain.lean
+++ b/testing/proofs/lean4/Shirudo/Chain.lean
@@ -13,6 +13,13 @@ measured exactly the events the log shows, in that order.
`register_injective_on` asks the same of `H` only on the pairs the two
sequences fold, which a real hash can meet; `Shirudo.Events` proves the
encodings of util/chain.c injective and builds the end theorem on it.
+
+A upatch tree has two roots of its own. A node's projection root folds
+the digests of the operations that chained on that node, so it differs
+from node to node. The history root takes one `chain_history_step()` for
+every operation, so that nodes of different selectors can compare it:
+`history_ignores_outcomes` shows it depends on the operations' digests
+alone.
-/
namespace Shirudo.Chain
@@ -120,4 +127,55 @@ theorem register_tagged_injective (H : D → D → D) (init : D)
subst hk; subst hp; rfl
exact map_injective _ hinj _ _ (register_injective H init hH hinit _ _ h)
+/-! ### The history root of a upatch tree -/
+
+/-- The history root after the operations whose digests are `digests`,
+oldest first: `chain_history_step()` folded from `zero`. -/
+def historyRoot (step : D → D → D) (zero : D) (digests : List D) : D :=
+ digests.foldl step zero
+
+/-- The history root after an operation is one step from the root before
+it. -/
+theorem historyRoot_append_one (step : D → D → D) (zero : D)
+ (digests : List D) (d : D) :
+ historyRoot step zero (digests ++ [d]) =
+ step (historyRoot step zero digests) d := by
+ simp [historyRoot]
+
+/-- One operation as a node processes it, on its projection root and its
+history root: an operation of digest `op.1` had outcome `op.2` on this
+node. The projection root folds the digest only for an outcome that
+chains; the history root takes its step for every outcome. -/
+def processOne {O : Type} (fold step : D → D → D) (chained : O → Bool)
+ (roots : D × D) (op : D × O) : D × D :=
+ (if chained op.2 then fold roots.1 op.1 else roots.1, step roots.2 op.1)
+
+/-- The two roots after `ops`, oldest first. -/
+def processAll {O : Type} (fold step : D → D → D) (chained : O → Bool)
+ (roots : D × D) (ops : List (D × O)) : D × D :=
+ ops.foldl (processOne fold step chained) roots
+
+/-- The history root a node holds is the one of the operations' digests
+alone. -/
+theorem processAll_history {O : Type} (fold step : D → D → D)
+ (chained : O → Bool) (roots : D × D) (ops : List (D × O)) :
+ (processAll fold step chained roots ops).2 =
+ historyRoot step roots.2 (ops.map Prod.fst) := by
+ induction ops generalizing roots with
+ | nil => rfl
+ | cons op ops ih => exact ih (processOne fold step chained roots op)
+
+/-- The history root does not depend on outcomes: two nodes that start
+from one history root and process operations of the same digests hold the
+same one, whatever outcome each operation had on either and whichever
+outcomes chain into their projection roots. -/
+theorem history_ignores_outcomes {O : Type} (fold step : D → D → D)
+ (chained1 chained2 : O → Bool) (roots1 roots2 : D × D)
+ (ops1 ops2 : List (D × O))
+ (hroot : roots1.2 = roots2.2)
+ (hops : ops1.map Prod.fst = ops2.map Prod.fst) :
+ (processAll fold step chained1 roots1 ops1).2 =
+ (processAll fold step chained2 roots2 ops2).2 := by
+ rw [processAll_history, processAll_history, hroot, hops]
+
end Shirudo.Chain
diff --git a/testing/proofs/lean4/Shirudo/Events.lean b/testing/proofs/lean4/Shirudo/Events.lean
index d088467a..18ce5d59 100644
--- a/testing/proofs/lean4/Shirudo/Events.lean
+++ b/testing/proofs/lean4/Shirudo/Events.lean
@@ -22,6 +22,9 @@ hashes them. Every event is SHA-256 of a tag, its NUL and fixed fields:
`chain_fold()`, and the TPM's NV extend, set the register to SHA-256 of
the register and the event, from 32 zero bytes before the first extend.
+`chain_history_step()` hashes `"shirudo-tree-history"`, its NUL, a
+tree's history root and an operation's digest into the next history
+root (`historyBytes`), a chain beside the register.
The model follows each function's buffer: `Fields.bytes` is what the C
hands SHA-256 for an event whose set is already hashed, `listing` the
@@ -30,7 +33,8 @@ A byte is a `UInt8`, a digest 32 of them, a C string the bytes before its
NUL. testing/proofs/cbmc/chain-events.c checks that the C hashes exactly
`Fields.bytes` for every name of up to 8 bytes and of 255 to 257, every
payload, digest and sequence number, the init event of an empty set
-included, and `fold` for every register and event.
+included, `fold` for every register and event and `historyBytes` for
+every root and digest.
On the domain the callers admit, upatch names that `upatch_name_valid()`
accepts of at most `UPATCH_NAME_MAX` bytes and sequence numbers a
@@ -89,6 +93,7 @@ def tagUpatch : List UInt8 := ascii "shirudo-upatch"
def tagUnload : List UInt8 := ascii "shirudo-unload"
def tagKeyRevoke : List UInt8 := ascii "shirudo-key-revoke"
def tagTree : List UInt8 := ascii "shirudo-tree"
+def tagHistory : List UInt8 := ascii "shirudo-tree-history"
/-- `strnlen(s, max)` of the C string whose bytes, up to its NUL, start
with `s`. -/
@@ -134,6 +139,12 @@ sequence number and the operation's digest. -/
def treeBytes (seq : Int) (operation : Digest) : List UInt8 :=
tagTree ++ [0] ++ be64 (u64OfS64 seq) ++ operation.bytes
+/-- `chain_history_step()`'s buffer: `"shirudo-tree-history"` with its
+NUL, the history root before the operation and the operation's digest.
+It is no register event: a tree's history root is a chain of its own. -/
+def historyBytes (prev operation : Digest) : List UInt8 :=
+ tagHistory ++ [0] ++ prev.bytes ++ operation.bytes
+
/-- What the C hands SHA-256 for an event. -/
def Fields.bytes : Fields → List UInt8
| .init c s r => initBytes c s r
@@ -622,6 +633,11 @@ def Event.digest (e : Event) : Digest := sha (e.fields sha).bytes
64 bytes, hashed. -/
def fold (r e : Digest) : Digest := sha (r.bytes ++ e.bytes)
+/-- `chain_history_step()`: the step `Chain.historyRoot` folds from
+`Digest.zero`. -/
+def historyStep (prev operation : Digest) : Digest :=
+ sha (historyBytes prev operation)
+
/-- The register after `events`, oldest first, as `attest predict` and
`attest verify` replay it and the TPM extends it. -/
def replay (events : List Event) : Digest :=
diff --git a/testing/unit/chain.c b/testing/unit/chain.c
index bc258519..4aea58c7 100644
--- a/testing/unit/chain.c
+++ b/testing/unit/chain.c
@@ -62,12 +62,95 @@ static void chain_fold_extends_like_the_register(void)
EXPECT_MEMEQ(chain, want, sizeof(want));
}
+/*
+ * The operation digests of a three-operation tree, and the history root
+ * after none, one, two and all of them as Python's hashlib gives it:
+ *
+ * root = bytes(32)
+ * for d in digests:
+ * root = sha256(b"shirudo-tree-history\0" + root + d).digest()
+ */
+static const char *const tree_operations[] = {
+ "13b248d4a85d91150717e3e96dc9c2d7a8a43633e3b58f8f6168d408aed26c43",
+ "5ce215313f29f87bfb87aa0cc65c237bf21bdc7702606e9cc29be187f950074b",
+ "bd98b27dd5cfc9deeb0e4f49a7d4bd0f991762ec784be9ea838a59966d9dd0dc",
+};
+
+static const char *const history_roots[] = {
+ "0000000000000000000000000000000000000000000000000000000000000000",
+ "470779d64e1302c4826a4be3222020050e70784ae6016040eabde9d0d8770d80",
+ "889efeb27650c0111168d523c935f051addbcc049d079e440e518dd3939722f0",
+ "bdc9ee89e753beca03d6e9ae89344269cb3a2e8ba3662122f55b1e3f7ff34011",
+};
+
+static void history_root_after(size_t n, char hex[2 * 32 + 1])
+{
+ __u8 root[32] = {}, digest[32];
+ size_t i;
+
+ for (i = 0; i < n; i++) {
+ ASSERT_EQ(hex_decode(tree_operations[i], digest, 32), 0);
+ ASSERT_EQ(chain_history_step(root, digest, root), 0);
+ }
+ hex_encode(root, sizeof(root), hex);
+}
+
+static void history_root_matches_the_reference(void)
+{
+ char hex[2 * 32 + 1];
+
+ history_root_after(0, hex);
+ EXPECT_STREQ(hex, history_roots[0]);
+ history_root_after(1, hex);
+ EXPECT_STREQ(hex, history_roots[1]);
+ history_root_after(3, hex);
+ EXPECT_STREQ(hex, history_roots[3]);
+}
+
+static void history_step_writes_beside_its_input(void)
+{
+ __u8 prev[32] = {}, zero[32] = {}, digest[32], out[32];
+ char hex[2 * 32 + 1];
+
+ ASSERT_EQ(hex_decode(tree_operations[0], digest, 32), 0);
+ ASSERT_EQ(chain_history_step(prev, digest, out), 0);
+ hex_encode(out, sizeof(out), hex);
+ EXPECT_STREQ(hex, history_roots[1]);
+ EXPECT_MEMEQ(prev, zero, sizeof(prev));
+}
+
+static void history_root_differs_from_the_projection_root(void)
+{
+ static const char *const projection_roots[] = {
+ "04f83540c1727aefcf3c53449a99594c8fd1cc15e1466dbdbf3168cc8f9dc136",
+ "a042a582b3a3d8c1b633b8ed0356e0b52f1f5aa25af85b06cc3f481562dcda93",
+ "154522480ff8884daa95589e80f26f20248bbf9c192dfbb3cee04e03b4f136a1",
+ };
+ __u8 history[32] = {}, projection[32] = {}, digest[32];
+ char hex[2 * 32 + 1];
+ size_t i;
+
+ for (i = 0; i < ARRAY_SIZE(tree_operations); i++) {
+ ASSERT_EQ(hex_decode(tree_operations[i], digest, 32), 0);
+ ASSERT_EQ(chain_history_step(history, digest, history), 0);
+ ASSERT_EQ(sha256_2(projection, 32, digest, 32, projection), 0);
+ hex_encode(projection, sizeof(projection), hex);
+ EXPECT_STREQ(hex, projection_roots[i]);
+ hex_encode(history, sizeof(history), hex);
+ EXPECT_STREQ(hex, history_roots[i + 1]);
+ EXPECT_TRUE(memcmp(history, projection, sizeof(history)) != 0);
+ }
+}
+
static const struct unit_case cases[] = {
UNIT_CASE(sha256_matches_fips_180_vector),
UNIT_CASE(sha256_2_hashes_the_concatenation),
UNIT_CASE(sha256_2_reports_a_failure),
UNIT_CASE(chain_events_hash_their_documented_input),
UNIT_CASE(chain_fold_extends_like_the_register),
+ UNIT_CASE(history_root_matches_the_reference),
+ UNIT_CASE(history_step_writes_beside_its_input),
+ UNIT_CASE(history_root_differs_from_the_projection_root),
{}
};
diff --git a/util/chain.c b/util/chain.c
index 91a5310c..073e7ffd 100644
--- a/util/chain.c
+++ b/util/chain.c
@@ -152,6 +152,18 @@ int chain_fold(__u8 chain[32], const __u8 event[32])
return sha256_calc(cat, sizeof(cat), chain);
}
+int chain_history_step(const __u8 prev[32], const __u8 operation_digest[32],
+ __u8 out[32])
+{
+ static const char tag[] = "shirudo-tree-history";
+ __u8 buf[sizeof(tag) + 64];
+
+ memcpy(buf, tag, sizeof(tag));
+ memcpy(buf + sizeof(tag), prev, 32);
+ memcpy(buf + sizeof(tag) + 32, operation_digest, 32);
+ return sha256_calc(buf, sizeof(buf), out);
+}
+
int chain_anchor(const __u8 name[TPM_NAME_LEN],
char *out, size_t cap)
{
diff --git a/util/chain.h b/util/chain.h
index 1ce0ff50..4616ae8b 100644
--- a/util/chain.h
+++ b/util/chain.h
@@ -62,6 +62,9 @@ int chain_upatch_event(const char *tag, const char *name,
int chain_tree_event(__s64 seq, const __u8 operation_digest[32],
__u8 event[32]) __must_check;
int chain_fold(__u8 chain[32], const __u8 event[32]) __must_check;
+/* A step for every operation, whatever its outcome here; out may be prev. */
+int chain_history_step(const __u8 prev[32], const __u8 operation_digest[32],
+ __u8 out[32]) __must_check;
/* The PCR record anchoring a register name: nvpcr-init:shirudo:0x..:<name>. */
int chain_anchor(const __u8 name[TPM_NAME_LEN],