OXYGENHUB

Coverage / crates/spectra-core/src/proof.rs 95.9%

70 of 73 lines run, 21 of 29 functions · run #5 on main · run missed

1
//! Signed requests and the proofs events carry (spec §8.5, §9.5).
2
//!
3
//! A principal signs `"spectra1 request " || method || 0x00 || BLAKE3(body)
4
//! || time (8 bytes) || nonce (16 bytes)` with its key, through `oxy-core`'s
5
//! challenge signing. The signature, with what it covered, is the proof
6
//! kept on every event the request causes, so a verifier can check later,
7
//! from the ledger alone, which key asked for it.
8
//!
9
//! The CLI, writing to the ledger itself, signs with `method = "cli"` and
10
//! `body` = the event's canonical payload, so its proof also binds the
11
//! event's content.
12
13
use crate::keys::KeyPair;
14
use oxy_core::cbor::{self, Value};
15
use oxy_core::identity::{Identity, Signature};
16
use rand::RngCore;
17
18
/// How far a request's time may be from the server's clock (spec §8.5).
19
pub const WINDOW_MS: u64 = 5 * 60 * 1000;
20
21
/// A signature with everything it covered except the body itself.
22
#[derive(Clone, Debug, PartialEq, Eq)]
23
pub struct Proof {
24
    pub sig: Signature,
25
    pub method: String,
26
    pub body_hash: [u8; 32],
27
    pub time_ms: u64,
28
    pub nonce: [u8; 16],
29
}
30
31
impl Proof {
32
    /// Sign `method` and `body` now with `pair`.
3359×
    pub fn sign(pair: &KeyPair, method: &str, body: &[u8]) -> Proof {
3459×
        let mut nonce = [0u8; 16];
3559×
        rand::rngs::OsRng.fill_bytes(&mut nonce);
3659×
        Proof::sign_at(pair, method, body, crate::ids::now_ms(), nonce)
3759×
    }
38
3960×
    pub fn sign_at(pair: &KeyPair, method: &str, body: &[u8], time_ms: u64, nonce: [u8; 16]) -> Proof {
4060×
        let body_hash = *blake3::hash(body).as_bytes();
4160×
        let sig = pair.sign(&message(method, &body_hash, time_ms, &nonce));
4260×
        Proof { sig, method: method.into(), body_hash, time_ms, nonce }
4360×
    }
44
45
    /// The signing key's KeyId.
46102×
    pub fn key(&self) -> [u8; 32] {
47102×
        self.sig.key.0
48102×
    }
49
50
    /// Check the signature against `identity` and, when given, the body.
51103×
    pub fn verify(&self, identity: &Identity, body: Option<&[u8]>) -> Result<(), String> {
52103×
        if let Some(b) = body {
53100×
            if *blake3::hash(b).as_bytes() != self.body_hash {
543×
                return Err("the signature covers a different body".into());
5597×
            }
563×
        }
57100×
        let msg = message(&self.method, &self.body_hash, self.time_ms, &self.nonce);
58100×
        oxy_core::identity::verify_challenge(identity, &self.sig, &msg).map_err(|e| format!("signature does not verify: {e}"))
59103×
    }
60
6155×
    pub fn encode(&self) -> Vec<u8> {
6255×
        cbor::encode(&Value::map([
6355×
            (0, self.sig.to_value()),
6455×
            (1, Value::Text(self.method.clone())),
6555×
            (2, Value::Bytes(self.body_hash.to_vec())),
6655×
            (3, Value::Uint(self.time_ms)),
6755×
            (4, Value::Bytes(self.nonce.to_vec())),
6855×
        ]))
6955×
    }
70
7193×
    pub fn decode(b: &[u8]) -> Result<Proof, String> {
7293×
        let v = cbor::decode(b).map_err(|e| format!("malformed proof: {e}"))?;
7393×
        let m = v.as_map().ok_or("malformed proof")?;
74465×
        let get = |k: u64| m.get(&k).ok_or_else(|| format!("proof lacks field {k}"));
75
        Ok(Proof {
7693×
            sig: Signature::from_value(get(0)?).map_err(|e| format!("malformed signature: {e}"))?,
7793×
            method: get(1)?.as_text().ok_or("proof method")?.to_string(),
7893×
            body_hash: get(2)?.as_bytes().and_then(|b| b.try_into().ok()).ok_or("proof body hash")?,
7993×
            time_ms: get(3)?.as_uint().ok_or("proof time")?,
8093×
            nonce: get(4)?.as_bytes().and_then(|b| b.try_into().ok()).ok_or("proof nonce")?,
81
        })
8293×
    }
83
}
84
85
/// `"spectra1 request " || method || 0 || body_hash || time || nonce`.
86160×
pub fn message(method: &str, body_hash: &[u8; 32], time_ms: u64, nonce: &[u8; 16]) -> Vec<u8> {
87160×
    let mut m = b"spectra1 request ".to_vec();
88160×
    m.extend_from_slice(method.as_bytes());
89160×
    m.push(0);
90160×
    m.extend_from_slice(body_hash);
91160×
    m.extend_from_slice(&time_ms.to_be_bytes());
92160×
    m.extend_from_slice(nonce);
93160×
    m
94160×
}
95
96
#[cfg(test)]
97
mod tests {
98
    use super::*;
99
100
    #[test]
1011×
    fn a_proof_round_trips_and_binds_key_method_body_and_time() {
1021×
        let a = KeyPair::generate("a", "");
1031×
        let b = KeyPair::generate("b", "");
1041×
        let p = Proof::sign(&a, "POST /mcp", b"{\"x\":1}");
1051×
        let q = Proof::decode(&p.encode()).unwrap();
1061×
        assert_eq!(p, q);
1071×
        q.verify(&a.identity, Some(b"{\"x\":1}")).unwrap();
1081×
        assert!(q.verify(&a.identity, Some(b"{\"x\":2}")).is_err(), "another body");
1091×
        assert!(q.verify(&b.identity, None).is_err(), "another key");
1101×
        let mut r = q.clone();
1111×
        r.method = "POST /api".into();
1121×
        assert!(r.verify(&a.identity, None).is_err(), "another method");
1131×
        let mut t = q.clone();
1141×
        t.time_ms += 1;
1151×
        assert!(t.verify(&a.identity, None).is_err(), "another time");
1161×
    }
117
}