Skip to main content

gent_crypto/
configuration_log.rs

1//! Admission to an opaque ordered log. This authenticates possession of the
2//! current write capability, not node policy or encrypted state contents.
3//! Endpoints must additionally verify the node signature and prior authority.
4use anyhow::{Result, ensure};
5use base64::{Engine, engine::general_purpose::STANDARD};
6use ed25519_dalek::{Signature, Signer, SigningKey, VerifyingKey};
7use gent_protocol::configuration_log::{Commit, Head, Header, MAX_CIPHERTEXT_BYTES, Receipt};
8use sha2::{Digest, Sha256};
9use zeroize::Zeroizing;
10
11const ADMISSION_DOMAIN: &[u8] = b"gent/configuration-admission/v1\0";
12const RECORD_DOMAIN: &[u8] = b"gent/configuration-record/v1\0";
13const INTENT_DOMAIN: &[u8] = b"gent/configuration-intent/v1\0";
14
15/// Short-lived capability, never exposed through Debug or implicit serialization.
16pub struct WriteCapability(SigningKey);
17
18impl WriteCapability {
19    pub fn generate() -> Result<Self> {
20        let mut seed = Zeroizing::new([0; 32]);
21        getrandom::fill(seed.as_mut())?;
22        Ok(Self::from_secret(&seed))
23    }
24
25    pub fn from_secret(seed: &[u8; 32]) -> Self {
26        Self(SigningKey::from_bytes(seed))
27    }
28
29    /// Endpoint encrypted storage/envelopes only.
30    pub fn secret(&self) -> Zeroizing<[u8; 32]> {
31        Zeroizing::new(*self.0.as_bytes())
32    }
33
34    pub fn public_key(&self) -> [u8; 32] {
35        self.0.verifying_key().to_bytes()
36    }
37
38    pub fn seal(&self, header: Header, ciphertext: &[u8]) -> Result<Commit> {
39        ensure!(
40            header.previous.write_key == self.public_key(),
41            "wrong write capability"
42        );
43        ensure!(header.valid(), "invalid configuration header");
44        ensure!(
45            (16..=MAX_CIPHERTEXT_BYTES).contains(&ciphertext.len()),
46            "invalid ciphertext size"
47        );
48        let mut commit = Commit {
49            header,
50            ciphertext: STANDARD.encode(ciphertext),
51            signature: Vec::new(),
52        };
53        commit.signature = self.0.sign(&admission_input(&commit)?).to_bytes().to_vec();
54        verify(&commit)?;
55        Ok(commit)
56    }
57}
58
59/// The caller persists a fresh random salt with the pending operation and
60/// includes it only in endpoint-encrypted content. Reuse it for every rebase.
61pub fn intent(salt: &[u8; 32], operation: &[u8]) -> [u8; 32] {
62    let mut hash = Sha256::new();
63    hash.update(INTENT_DOMAIN);
64    hash.update(salt);
65    hash.update(operation);
66    hash.finalize().into()
67}
68
69fn ciphertext(commit: &Commit) -> Result<Vec<u8>> {
70    ensure!(
71        commit.ciphertext.len() <= MAX_CIPHERTEXT_BYTES.div_ceil(3) * 4,
72        "ciphertext too large"
73    );
74    let bytes = STANDARD.decode(&commit.ciphertext)?;
75    ensure!(
76        (16..=MAX_CIPHERTEXT_BYTES).contains(&bytes.len())
77            && STANDARD.encode(&bytes) == commit.ciphertext,
78        "invalid ciphertext encoding"
79    );
80    Ok(bytes)
81}
82
83fn admission_input(commit: &Commit) -> Result<Vec<u8>> {
84    let mut input = ADMISSION_DOMAIN.to_vec();
85    input.extend(serde_jcs::to_vec(&commit.header)?);
86    input.extend(Sha256::digest(ciphertext(commit)?));
87    Ok(input)
88}
89
90pub fn validate_write_key(key: &[u8; 32]) -> Result<()> {
91    let key = VerifyingKey::from_bytes(key)?;
92    ensure!(!key.is_weak(), "weak configuration admission key");
93    Ok(())
94}
95
96/// Verify against the header's key. The service must separately compare the
97/// *entire* previous head with its durable head within serialized admission.
98pub fn verify(commit: &Commit) -> Result<Receipt> {
99    ensure!(commit.header.valid(), "invalid configuration header");
100    validate_write_key(&commit.header.next_write_key)?;
101    let key = VerifyingKey::from_bytes(&commit.header.previous.write_key)?;
102    key.verify_strict(
103        &admission_input(commit)?,
104        &Signature::from_slice(&commit.signature)?,
105    )?;
106    let mut hash = Sha256::new();
107    hash.update(RECORD_DOMAIN);
108    hash.update(serde_jcs::to_vec(commit)?);
109    Ok(Receipt {
110        cluster: commit.header.cluster.clone(),
111        origin: commit.header.origin.clone(),
112        operation: commit.header.operation.clone(),
113        intent: commit.header.intent,
114        head: Head {
115            sequence: commit.header.previous.sequence + 1,
116            digest: hash.finalize().into(),
117            write_key: commit.header.next_write_key,
118        },
119    })
120}