interplane.Get Omega
Read in your language

synapse

A component of the AienOS ecosystem.

Brought by drakestapleton

branch main Branches Tags History

synapse / crates / synapse-store / src / seal.rs

107 lines · 4457 bytes Raw Blame History
1//! Keyed seal over revision records (issue #101).
2//!
3//! The WAL frame chain (CRC32C plus an unkeyed SHA-256 chain) and the checkpoint state root
4//! detect corruption, not forgery: anyone who can rewrite the whole log can recompute both.
5//! Kinds 7 and 8 carry a `chain` field that no signature in the record covers, so such a
6//! rewrite could swap an admission's chain for links the forger minted and then sign kind 9
7//! moves under them. A store opened with a [`RevisionSealKey`] (the server's machine key)
8//! closes that: every revision record it writes (kinds 7, 8, 9, 10, and 11) carries a
9//! [`RevisionSeal`], an Ed25519 signature by that key over a running head that chains the
10//! record's `WalSeq`, its full record digest (every field, chain included), and the head of
11//! the previous sealed record. Replay applies a revision record only when its seal verifies
12//! against the head so far, so a changed, inserted, reordered, or removed revision record
13//! breaks the chain from that point on. Strict recovery refuses such a store
14//! (`RevisionRecordUnsealed`).
15//!
16//! Layout (ADR 0003 principle P5): the seal is an optional trailer after the record body,
17//! `1 u8 | server_key [32] | signature [64]`; a record without it ends at its body.
18//!
19//! * head 0 = 32 zero bytes;
20//! * head n = `SHA-256(DOMAIN_REVISION_SEAL_HEAD_V1 || head n-1 || seq u64le || record digest)`;
21//! * signature n = Ed25519 over `DOMAIN_REVISION_SEAL_SIGN_V1 || head n`, checked with
22//! `verify_strict` under the configured key, which must not be a small-order key.
23
24use ed25519_dalek::{Signature, Signer, SigningKey, VerifyingKey};
25use sha2::{Digest, Sha256};
26use synapse_core::WalSeq;
27
28/// Domain tag of the running seal head.
29pub const DOMAIN_REVISION_SEAL_HEAD_V1: &[u8] = b"SYNAPSE_REVISION_SEAL_HEAD_V1\0";
30/// Domain tag of the server's signature over one seal head.
31pub const DOMAIN_REVISION_SEAL_SIGN_V1: &[u8] = b"SYNAPSE_REVISION_SEAL_SIGN_V1\0";
32/// Tag byte that opens the seal trailer of a revision record.
33pub const REVISION_SEAL_TAG: u8 = 1;
34
35/// The server's seal on one revision record: the key that signed and its signature over the
36/// running head at that record.
37#[derive(Debug, Clone, Copy, PartialEq, Eq)]
38pub struct RevisionSeal {
39 pub server_key: [u8; 32],
40 pub signature: [u8; 64],
41}
42
43/// The server machine key a store seals revision records with (and verifies them against on
44/// replay). The key never comes from the log: the operator hands it to
45/// [`crate::SynapseStore::open_sealed`].
46#[derive(Clone)]
47pub struct RevisionSealKey(SigningKey);
48
49impl RevisionSealKey {
50 pub fn new(key: SigningKey) -> Self {
51 Self(key)
52 }
53
54 /// The public half: what a seal must name and verify under.
55 pub fn public_key(&self) -> [u8; 32] {
56 self.0.verifying_key().to_bytes()
57 }
58
59 pub(crate) fn seal(&self, head: &[u8; 32]) -> RevisionSeal {
60 RevisionSeal {
61 server_key: self.public_key(),
62 signature: self.0.sign(&signable(head)).to_bytes(),
63 }
64 }
65}
66
67impl std::fmt::Debug for RevisionSealKey {
68 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
69 f.debug_tuple("RevisionSealKey")
70 .field(&hex::encode(self.public_key()))
71 .finish()
72 }
73}
74
75fn signable(head: &[u8; 32]) -> Vec<u8> {
76 let mut buf = Vec::with_capacity(DOMAIN_REVISION_SEAL_SIGN_V1.len() + 32);
77 buf.extend_from_slice(DOMAIN_REVISION_SEAL_SIGN_V1);
78 buf.extend_from_slice(head);
79 buf
80}
81
82/// The head after the record at `seq` with digest `record_digest`, following `prev`.
83pub(crate) fn next_head(prev: &[u8; 32], seq: WalSeq, record_digest: &[u8; 32]) -> [u8; 32] {
84 let mut h = Sha256::new();
85 h.update(DOMAIN_REVISION_SEAL_HEAD_V1);
86 h.update(prev);
87 h.update(seq.0.to_le_bytes());
88 h.update(record_digest);
89 h.finalize().into()
90}
91
92impl RevisionSeal {
93 /// True when the seal names `expected_key` (not a small-order key) and its signature over
94 /// `head` verifies strictly.
95 pub(crate) fn verifies(&self, expected_key: &[u8; 32], head: &[u8; 32]) -> bool {
96 if &self.server_key != expected_key {
97 return false;
98 }
99 let Ok(key) = VerifyingKey::from_bytes(expected_key) else {
100 return false;
101 };
102 !key.is_weak()
103 && key
104 .verify_strict(&signable(head), &Signature::from_bytes(&self.signature))
105 .is_ok()
106 }
107}