Skip to main content

weirdnet_sync/
seal.rs

1//! `SyncKey` derivation and page sealing.
2//!
3//! `SyncKey(type) = HKDF-SHA256(seed, salt = user id, info = weirdnet-sync/v1/<type>)`,
4//! or `weirdnet-sync/v1/<type>/e<n>` after a revocation bumps the epoch. After a
5//! rotation ([`crate::rotation`]) the key comes from the random epoch key instead:
6//! `HKDF-SHA256(epoch key, salt = user id, info = weirdnet-sync/v1/<type>/e<n>/k<id>)`.
7use crate::error::Result;
8use weirdnet_keys::{Key32, Sealed, kdf, open, seal};
9
10/// Common record types; the wire also accepts any other application string.
11pub mod typ {
12    pub const BOOKMARKS: &str = "bookmarks";
13    pub const HISTORY: &str = "history";
14    pub const SETTINGS: &str = "settings";
15}
16
17pub use typ as SyncType;
18
19/// A derived page-sealing key. Zeroized on drop via [`Key32`].
20pub type SyncKey = Key32;
21
22/// HKDF info string for this type and epoch (`e0` is omitted: `weirdnet-sync/v1/<type>`).
23pub fn sync_key_info(typ: &str, epoch: u32) -> String {
24    if epoch == 0 { format!("weirdnet-sync/v1/{typ}") } else { format!("weirdnet-sync/v1/{typ}/e{epoch}") }
25}
26
27/// Derive `SyncKey(type)` from the identity seed. The seed is not stored.
28pub fn sync_key(seed: &[u8], user_id: &str, typ: &str, epoch: u32) -> Result<SyncKey> {
29    let info = sync_key_info(typ, epoch);
30    Ok(kdf::hkdf(seed, Some(user_id.as_bytes()), info.as_bytes())?)
31}
32
33/// Associated data of a sealed page: type, epoch and the asking peer, so a page cannot
34/// be replayed under another type, epoch or peer.
35pub fn page_aad(typ: &str, epoch: u32, peer: &str) -> Vec<u8> {
36    format!("weirdnet-sync/v1|{typ}|e{epoch}|{peer}").into_bytes()
37}
38
39/// `SyncKey(type)` under a rotated epoch key (rotation `id`).
40pub fn rotated_sync_key(epoch_key: &Key32, user_id: &str, typ: &str, epoch: u32, id: &str) -> Result<SyncKey> {
41    let info = format!("weirdnet-sync/v1/{typ}/e{epoch}/k{id}");
42    Ok(kdf::hkdf(&epoch_key.0, Some(user_id.as_bytes()), info.as_bytes())?)
43}
44
45/// Associated data of a page sealed under a rotated key: also binds the rotation id.
46pub fn page_aad_keyed(typ: &str, epoch: u32, id: &str, peer: &str) -> Vec<u8> {
47    format!("weirdnet-sync/v1|{typ}|e{epoch}|k{id}|{peer}").into_bytes()
48}
49
50pub fn seal_page(key: &SyncKey, typ: &str, epoch: u32, peer: &str, plaintext: &[u8]) -> Result<Sealed> {
51    Ok(seal(key, plaintext, &page_aad(typ, epoch, peer))?)
52}
53
54pub fn open_page(key: &SyncKey, typ: &str, epoch: u32, peer: &str, sealed: &Sealed) -> Result<zeroize::Zeroizing<Vec<u8>>> {
55    Ok(open(key, sealed, &page_aad(typ, epoch, peer))?)
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    const SEED: [u8; 32] = *b"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
63
64    #[test]
65    fn typed_keys_differ_by_type_user_epoch_and_seed() {
66        let a = sync_key(&SEED, "user-1", typ::BOOKMARKS, 0).unwrap();
67        let b = sync_key(&SEED, "user-1", typ::HISTORY, 0).unwrap();
68        let c = sync_key(&SEED, "user-2", typ::BOOKMARKS, 0).unwrap();
69        let d = sync_key(&SEED, "user-1", typ::BOOKMARKS, 1).unwrap();
70        let e = sync_key(b"BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB", "user-1", typ::BOOKMARKS, 0).unwrap();
71        assert_ne!(a.0, b.0);
72        assert_ne!(a.0, c.0);
73        assert_ne!(a.0, d.0);
74        assert_ne!(a.0, e.0);
75        assert_eq!(sync_key_info(typ::BOOKMARKS, 0), "weirdnet-sync/v1/bookmarks");
76        assert_eq!(sync_key_info(typ::BOOKMARKS, 3), "weirdnet-sync/v1/bookmarks/e3");
77        // Same inputs, same key (two devices with the same seed).
78        assert_eq!(sync_key(&SEED, "user-1", typ::BOOKMARKS, 0).unwrap().0, a.0);
79    }
80
81    #[test]
82    fn a_page_opens_only_with_the_matching_key_aad() {
83        let key = sync_key(&SEED, "user-1", typ::BOOKMARKS, 0).unwrap();
84        let sealed = seal_page(&key, typ::BOOKMARKS, 0, "peer-b", b"ops").unwrap();
85        let plain = open_page(&key, typ::BOOKMARKS, 0, "peer-b", &sealed).unwrap();
86        assert_eq!(&plain[..], b"ops");
87        assert!(open_page(&key, typ::HISTORY, 0, "peer-b", &sealed).is_err());
88        assert!(open_page(&key, typ::BOOKMARKS, 0, "peer-c", &sealed).is_err());
89        assert!(open_page(&key, typ::BOOKMARKS, 1, "peer-b", &sealed).is_err());
90        let other = sync_key(&SEED, "user-1", typ::BOOKMARKS, 1).unwrap();
91        assert!(open_page(&other, typ::BOOKMARKS, 1, "peer-b", &sealed).is_err());
92    }
93}