Skip to main content

umbera_core/devlink/
comm.rs

1// Communities on a linked device: the membership vault root instead of account keys.
2//
3// The phone derives the vault keys (communities/app CommCrypto.vaultKeys / vaultKeysEpoch) as
4//   HKDF-SHA256(IKM = account_X25519 ‖ account_ML-KEM_seed, salt, info)
5//   = HKDF-Expand(HKDF-Extract(salt, IKM), info)          (RFC 5869)
6// Hence PRK = HKDF-Extract(salt, IKM) is the "vault root": from it the address, signing key
7// and encryption key of the vault for ANY epoch are derived, byte for byte as on the phone, while the account
8// private keys cannot be recovered from it (HMAC is one-way). The desktop receives two roots:
9// v3 (per-epoch vault — the primary one) and v2 (old epoch-less vault — read-only during migration).
10// Channel pseudonym keys live in the vault itself, so the desktop joins channels under the same
11// pseudonyms as the phone, and the Communities server still does not know whose vault it is.
12
13use 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/// The same three keys as CommCrypto.VaultKeys.
28#[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/// Phone: vault roots from the account private keys (X25519 32 B, ML-KEM seed 64 B).
54#[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/// Vault keys for epoch `epoch` from the v3 root (= CommCrypto.vaultKeysEpoch).
64#[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/// Keys of the old epoch-less vault from the v2 root (= CommCrypto.vaultKeys).
70#[uniffi::export]
71pub fn comm_vault_legacy_keys(root_v2: Vec<u8>) -> LR<CommVaultKeys> {
72    keys(&root_v2, "")
73}
74
75/// SECTION_COMM_VAULT record body: v3 ‖ v2 (64 B).
76#[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}