Skip to main content

qualia_core_db/crypto/network/cose/
binding.rs

1//! Canonical binding and role-bound public-key references.
2use super::cbor::{Reader, Writer};
3use super::{
4    Binding, KeyIds, ED25519, MAX_AUDIENCE_BYTES, MAX_BINDING_BYTES, MAX_PURPOSE_BYTES, ML_DSA_65,
5    PROFILE, SCHEMA_VERSION,
6};
7use crate::crypto::network::errors::CryptoError;
8use crate::crypto::network::types::{ED25519_PK_LEN, ML_DSA_65_PK_LEN};
9use sha2::{Digest, Sha384};
10
11const KEY_ID_DOMAIN: &[u8] = b"QBDL-COSE-KEY-ID-V1\0";
12// Canonical public COSE_Key maps followed by raw key bytes. NOT thumbprints.
13const MLDSA_KEY_PREFIX: &[u8] = &[0xa3, 1, 7, 3, 0x38, 0x30, 0x20, 0x59, 0x07, 0xa0];
14const ED_KEY_PREFIX: &[u8] = &[0xa4, 1, 1, 3, 0x32, 0x20, 6, 0x21, 0x58, 0x20];
15
16/// Borrowed primitive encodings; the caller separately admits trusted authority.
17pub struct PairedPublicKeys<'a> {
18    pub ml_dsa_65: &'a [u8; ML_DSA_65_PK_LEN],
19    pub ed25519: &'a [u8; ED25519_PK_LEN],
20}
21impl PairedPublicKeys<'_> {
22    pub fn key_ids(&self) -> KeyIds {
23        KeyIds {
24            ml_dsa_65: key_id(MLDSA_KEY_PREFIX, self.ml_dsa_65),
25            ed25519: key_id(ED_KEY_PREFIX, self.ed25519),
26        }
27    }
28}
29fn key_id(prefix: &[u8], key: &[u8]) -> [u8; 48] {
30    let mut hash = Sha384::new();
31    hash.update(KEY_ID_DOMAIN);
32    hash.update(prefix);
33    hash.update(key);
34    hash.finalize().into()
35}
36pub(super) fn validate(binding: &Binding<'_>) -> Result<(), CryptoError> {
37    if binding.purpose.is_empty() || binding.audience.is_empty() {
38        return Err(CryptoError::Malformed);
39    }
40    if binding.purpose.len() > MAX_PURPOSE_BYTES || binding.audience.len() > MAX_AUDIENCE_BYTES {
41        return Err(CryptoError::Capacity);
42    }
43    Ok(())
44}
45
46/// Encode the seven-field binding. Output is invalid on any error.
47pub fn encode_binding(binding: &Binding<'_>, out: &mut [u8]) -> Result<usize, CryptoError> {
48    validate(binding)?;
49    let cap = out.len().min(MAX_BINDING_BYTES);
50    let mut w = Writer::new(&mut out[..cap]);
51    w.head(5, 7)?;
52    w.head(0, 0)?;
53    w.text(PROFILE)?;
54    w.head(0, 1)?;
55    w.head(0, SCHEMA_VERSION)?;
56    w.head(0, 2)?;
57    w.bytes(binding.descriptor_digest)?;
58    w.head(0, 3)?;
59    w.head(4, 2)?;
60    for (alg, kid) in [
61        (ML_DSA_65, &binding.key_ids.ml_dsa_65),
62        (ED25519, &binding.key_ids.ed25519),
63    ] {
64        w.head(4, 2)?;
65        w.integer(alg)?;
66        w.bytes(kid)?;
67    }
68    w.head(0, 4)?;
69    w.head(0, binding.controller_epoch)?;
70    w.head(0, 5)?;
71    w.bytes(binding.purpose)?;
72    w.head(0, 6)?;
73    w.bytes(binding.audience)?;
74    Ok(w.len())
75}
76
77/// Parse canonical syntax only. Returned fields are explicitly UNVERIFIED.
78pub fn decode_binding(input: &[u8]) -> Result<Binding<'_>, CryptoError> {
79    if input.len() > MAX_BINDING_BYTES {
80        return Err(CryptoError::Capacity);
81    }
82    let mut r = Reader::new(input);
83    r.expect(5, 7)?;
84    r.expect(0, 0)?;
85    r.text_eq(PROFILE)?;
86    r.expect(0, 1)?;
87    if r.head(0)? != SCHEMA_VERSION {
88        return Err(CryptoError::UnknownProfile);
89    }
90    r.expect(0, 2)?;
91    let descriptor_digest = array48(r.bytes(48)?)?;
92    r.expect(0, 3)?;
93    r.expect(4, 2)?;
94    r.expect(4, 2)?;
95    r.negative(ML_DSA_65)?;
96    let ml_dsa_65 = *array48(r.bytes(48)?)?;
97    r.expect(4, 2)?;
98    r.negative(ED25519)?;
99    let ed25519 = *array48(r.bytes(48)?)?;
100    r.expect(0, 4)?;
101    let controller_epoch = r.head(0)?;
102    r.expect(0, 5)?;
103    let purpose = r.bytes(MAX_PURPOSE_BYTES)?;
104    r.expect(0, 6)?;
105    let audience = r.bytes(MAX_AUDIENCE_BYTES)?;
106    r.finish()?;
107    let binding = Binding {
108        descriptor_digest,
109        key_ids: KeyIds { ml_dsa_65, ed25519 },
110        controller_epoch,
111        purpose,
112        audience,
113    };
114    validate(&binding)?;
115    Ok(binding)
116}
117fn array48(bytes: &[u8]) -> Result<&[u8; 48], CryptoError> {
118    bytes.try_into().map_err(|_| CryptoError::Malformed)
119}