umbera_core/devlink/
comm.rs1use hkdf::Hkdf;
14use sha2::Sha256;
15
16use super::{LinkError, LR};
17
18const SALT_V2: &[u8] = b"umbera-comm-vault-v2";
19const SALT_V3: &[u8] = b"umbera-comm-vault-v3";
20
21#[derive(uniffi::Record, Clone, Debug, PartialEq, Eq)]
22pub struct CommVaultRoots {
23 pub v3: Vec<u8>,
24 pub v2: Vec<u8>,
25}
26
27#[derive(uniffi::Record, Clone, Debug, PartialEq, Eq)]
29pub struct CommVaultKeys {
30 pub id: Vec<u8>,
31 pub auth_seed: Vec<u8>,
32 pub enc_key: Vec<u8>,
33}
34
35fn extract(salt: &[u8], ikm: &[u8]) -> Vec<u8> {
36 let (prk, _) = Hkdf::<Sha256>::extract(Some(salt), ikm);
37 prk.to_vec()
38}
39
40fn keys(root: &[u8], suffix: &str) -> LR<CommVaultKeys> {
41 if root.len() != 32 {
42 return Err(LinkError::Crypto("vault root must be 32 bytes".into()));
43 }
44 let h = Hkdf::<Sha256>::from_prk(root).map_err(|_| LinkError::Crypto("vault root must be 32 bytes".into()))?;
45 let one = |label: &str| {
46 let mut o = vec![0u8; 32];
47 h.expand(format!("{label}{suffix}").as_bytes(), &mut o).expect("hkdf len");
48 o
49 };
50 Ok(CommVaultKeys { id: one("vault-id"), auth_seed: one("vault-auth"), enc_key: one("vault-key") })
51}
52
53#[uniffi::export]
55pub fn comm_vault_roots(account_x25519_private: Vec<u8>, account_mlkem_seed: Vec<u8>) -> LR<CommVaultRoots> {
56 if account_x25519_private.len() != 32 || account_mlkem_seed.len() != 64 {
57 return Err(LinkError::Crypto("vault inputs must be 32 + 64 bytes".into()));
58 }
59 let ikm = zeroize::Zeroizing::new([account_x25519_private, account_mlkem_seed].concat());
60 Ok(CommVaultRoots { v3: extract(SALT_V3, &ikm), v2: extract(SALT_V2, &ikm) })
61}
62
63#[uniffi::export]
65pub fn comm_vault_epoch_keys(root_v3: Vec<u8>, epoch: u32) -> LR<CommVaultKeys> {
66 keys(&root_v3, &format!("/{epoch}"))
67}
68
69#[uniffi::export]
71pub fn comm_vault_legacy_keys(root_v2: Vec<u8>) -> LR<CommVaultKeys> {
72 keys(&root_v2, "")
73}
74
75#[uniffi::export]
77pub fn comm_vault_record_body(roots: CommVaultRoots) -> LR<Vec<u8>> {
78 if roots.v3.len() != 32 || roots.v2.len() != 32 {
79 return Err(LinkError::Malformed("vault roots must be 32 bytes each".into()));
80 }
81 Ok([roots.v3, roots.v2].concat())
82}
83
84#[uniffi::export]
85pub fn comm_vault_record_parse(body: Vec<u8>) -> LR<CommVaultRoots> {
86 if body.len() != 64 {
87 return Err(LinkError::Malformed("vault record must be 64 bytes".into()));
88 }
89 Ok(CommVaultRoots { v3: body[..32].to_vec(), v2: body[32..].to_vec() })
90}