Skip to main content

vela_protocol/kernel/
detached.rs

1//! Detached artifact signatures: one schema for "a human signed these
2//! exact bytes" (`vela.detached-signature.v0.1`), superseding the
3//! per-script shapes that grew around the fixtures manifest and the
4//! policy ceremony. The subject is signed as raw bytes on disk — no
5//! canonicalization step to drift — and the record is self-describing
6//! (it carries the subject's sha256 so a verifier can name WHICH bytes
7//! it expected when they've changed).
8
9use ed25519_dalek::{Signer, Verifier};
10use serde::{Deserialize, Serialize};
11use sha2::{Digest, Sha256};
12
13pub const DETACHED_SIGNATURE_SCHEMA: &str = "vela.detached-signature.v0.1";
14
15#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
16pub struct DetachedSignature {
17    pub schema: String,
18    /// The subject's file name (informational; the bytes are the truth).
19    pub subject: String,
20    /// sha256 of the exact signed bytes.
21    pub subject_sha256: String,
22    pub signer_pubkey_hex: String,
23    pub signature_hex: String,
24    pub signed_at: String,
25}
26
27/// Sign exact bytes under an Ed25519 key. `signed_at` is caller-supplied
28/// so ceremonies stay reproducible in tests.
29pub fn sign_detached(
30    subject: &str,
31    bytes: &[u8],
32    key: &ed25519_dalek::SigningKey,
33    signed_at: &str,
34) -> DetachedSignature {
35    let sig = key.sign(bytes);
36    DetachedSignature {
37        schema: DETACHED_SIGNATURE_SCHEMA.to_string(),
38        subject: subject.to_string(),
39        subject_sha256: hex::encode(Sha256::digest(bytes)),
40        signer_pubkey_hex: hex::encode(key.verifying_key().to_bytes()),
41        signature_hex: hex::encode(sig.to_bytes()),
42        signed_at: signed_at.to_string(),
43    }
44}
45
46/// Verify a detached signature over exact bytes. Errors name what
47/// diverged: content drift (digest mismatch) is reported distinctly
48/// from a bad signature, because the operator's next step differs
49/// (re-sign deliberately changed bytes vs investigate a forgery).
50pub fn verify_detached(bytes: &[u8], record: &DetachedSignature) -> Result<(), String> {
51    if record.schema != DETACHED_SIGNATURE_SCHEMA {
52        return Err(format!("unknown schema `{}`", record.schema));
53    }
54    let digest = hex::encode(Sha256::digest(bytes));
55    if digest != record.subject_sha256 {
56        return Err(format!(
57            "subject bytes changed since signing: sha256 {} now, {} signed",
58            &digest[..16],
59            &record.subject_sha256[..16.min(record.subject_sha256.len())]
60        ));
61    }
62    let pk: [u8; 32] = hex::decode(&record.signer_pubkey_hex)
63        .map_err(|e| format!("pubkey hex: {e}"))?
64        .try_into()
65        .map_err(|_| "pubkey must be 32 bytes".to_string())?;
66    let vk = ed25519_dalek::VerifyingKey::from_bytes(&pk).map_err(|e| e.to_string())?;
67    let sig: [u8; 64] = hex::decode(&record.signature_hex)
68        .map_err(|e| format!("signature hex: {e}"))?
69        .try_into()
70        .map_err(|_| "signature must be 64 bytes".to_string())?;
71    vk.verify(bytes, &ed25519_dalek::Signature::from_bytes(&sig))
72        .map_err(|_| "signature does not verify over the subject bytes".to_string())
73}
74
75#[cfg(test)]
76mod tests {
77    use super::*;
78
79    #[test]
80    fn round_trip_and_tamper() {
81        let key = ed25519_dalek::SigningKey::from_bytes(&[9u8; 32]);
82        let bytes = b"the exact manifest bytes";
83        let rec = sign_detached("manifest.json", bytes, &key, "2026-07-03T00:00:00Z");
84        verify_detached(bytes, &rec).expect("round trip");
85        let err = verify_detached(b"tampered bytes", &rec).unwrap_err();
86        assert!(err.contains("changed since signing"), "{err}");
87        let mut forged = rec.clone();
88        forged.signature_hex = "00".repeat(64);
89        let err = verify_detached(bytes, &forged).unwrap_err();
90        assert!(err.contains("does not verify"), "{err}");
91    }
92}