Skip to main content

capsule_core/crypto/
primitives.rs

1//! The single source of truth, in code, for Capsule's cryptographic primitive set.
2//!
3//! Mirrors the inventory in [Cryptography — Primitives]. Every on-disk and on-wire
4//! structure that depends on a primitive carries a [`SuiteId`] (`crypto_suite_id`), so two
5//! structures encrypted under different suite versions can coexist without a flag day.
6//! Retiring a primitive does not edit a row — it adds a new [`SuiteId`] variant.
7//!
8//! [Cryptography — Primitives]: https://docs/design/cryptography/primitives/
9
10/// `crypto_suite_id` of the current primitive bundle (the [`SuiteId::V1`] inventory).
11pub const CRYPTO_SUITE_ID: u16 = 0x0001;
12
13/// The date-based wire `protocol_version` this build writes. Pinned per album at creation.
14pub const PROTOCOL_VERSION: &str = "2026-05-31";
15
16/// Identifies a complete bundle of cryptographic primitives.
17///
18/// A structure declaring a `crypto_suite_id` outside this closed set is rejected
19/// (fail-closed), never best-effort-parsed under a guessed suite.
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
21#[non_exhaustive]
22pub enum SuiteId {
23    /// `0x0001`: SHA-256 · HKDF-SHA512 · Argon2id · AES-256-GCM(+STREAM) ·
24    /// Ed25519+ML-DSA-65 · X-Wing · MLS X-Wing/SHA-256 suite (`0x004D`).
25    V1,
26}
27
28impl SuiteId {
29    /// The suite new writes use.
30    pub const CURRENT: SuiteId = SuiteId::V1;
31
32    /// Map a wire `crypto_suite_id` to its suite, or `None` if this build does not
33    /// implement it (the caller then fails closed — see invariant 2).
34    pub fn from_u16(id: u16) -> Option<Self> {
35        match id {
36            CRYPTO_SUITE_ID => Some(SuiteId::V1),
37            _ => None,
38        }
39    }
40
41    /// The wire `crypto_suite_id` for this suite.
42    pub fn as_u16(self) -> u16 {
43        match self {
44            SuiteId::V1 => CRYPTO_SUITE_ID,
45        }
46    }
47
48    /// SHA-256 digest length under this suite (content-hash / `hash` field length).
49    pub fn hash_len(self) -> usize {
50        match self {
51            SuiteId::V1 => 32,
52        }
53    }
54}
55
56/// Versioned HKDF `info` labels. Including a version string lets the KDF be rotated
57/// later without a flag day; the SSoT for each label is the doc that derives that key.
58pub mod info {
59    /// Per-file asset key: `HKDF(ikm=AMK, salt=file_id || nonce_prefix, info=ASSET_FILE_V1)`.
60    /// The fresh `nonce_prefix` is folded into the salt so a same-epoch `replace` re-rolls
61    /// the key, not merely the nonce (see <https://docs/design/cryptography/encryption/#re-keying-on-rewrite>).
62    pub const ASSET_FILE_V1: &[u8] = b"asset-file/v1";
63    /// Per-metadata-blob key: `HKDF(ikm=AMK, salt=blob_id || nonce, info=METADATA_BLOB_V1)`.
64    /// The blob's fresh `nonce` is folded into the salt, re-rolling the key per write even
65    /// though `blob_id` is constant across a `metadata-update`.
66    pub const METADATA_BLOB_V1: &[u8] = b"metadata-blob/v1";
67    /// Wrap key for an externally-chosen file key (`key_mode = wrapped`; an adopted
68    /// web-upload drop): `HKDF(ikm=AMK, salt=file_id || wrap_nonce, info=ASSET_KEYWRAP_V1)`.
69    /// The sealing/unsealing functions land with the wrapped-key slice; the label is the
70    /// contract (see <https://docs/design/cryptography/encryption/>).
71    pub const ASSET_KEYWRAP_V1: &[u8] = b"asset-keywrap/v1";
72    /// Default-album *identifier* derived from the account master key (an ID, not a key).
73    pub const DEFAULT_ALBUM_ID_V1: &[u8] = b"default-album-id/v1";
74    /// Share-link scope-key wrap: `HKDF(ikm=link_secret, salt=opaque_id,
75    /// info=SHARE_SCOPE_WRAP_V1)` derives the key that encapsulates a share link's scope
76    /// decryption material around the random link secret (SSoT: [Share Links]).
77    ///
78    /// [Share Links]: https://docs/design/share-links/
79    pub const SHARE_SCOPE_WRAP_V1: &[u8] = b"share-scope-wrap/v1";
80    /// Share-link metadata key: `HKDF(ikm=link_secret, salt=opaque_id,
81    /// info=SHARE_METADATA_V1)` derives the key that seals the share metadata record a viewer
82    /// starts from — the blob at the link's `metadata_hash` (SSoT: [Share Links]). A label of its
83    /// own, so the record key and the scope-wrap key never coincide.
84    ///
85    /// [Share Links]: https://docs/design/share-links/
86    pub const SHARE_METADATA_V1: &[u8] = b"share-metadata/v1";
87}
88
89/// Device hardware tier, selecting Argon2id cost parameters at *wrap* time. The chosen
90/// parameters are recorded in the wrapped blob, so unwrap works on any tier.
91#[derive(Debug, Clone, Copy, PartialEq, Eq)]
92pub enum DeviceTier {
93    /// ≤ 2 GiB total RAM (entry-level Android / embedded).
94    LowRam,
95    /// Default for phones and laptops.
96    Normal,
97    /// ≥ 8 GiB; used when wrapping new escrow blobs from a desktop.
98    Desktop,
99}
100
101/// Argon2id cost parameters: memory in KiB, iteration count `t`, parallelism `p`.
102#[derive(Debug, Clone, Copy, PartialEq, Eq)]
103pub struct Argon2Params {
104    /// Memory cost in KiB.
105    pub mem_kib: u32,
106    /// Iteration (time) cost `t`.
107    pub t_cost: u32,
108    /// Degree of parallelism `p`.
109    pub p_cost: u32,
110}
111
112impl DeviceTier {
113    /// Canonical Argon2id parameters for this tier (see the primitives doc's table).
114    pub fn params(self) -> Argon2Params {
115        match self {
116            DeviceTier::LowRam => Argon2Params {
117                mem_kib: 128 * 1024,
118                t_cost: 3,
119                p_cost: 1,
120            },
121            DeviceTier::Normal => Argon2Params {
122                mem_kib: 256 * 1024,
123                t_cost: 3,
124                p_cost: 1,
125            },
126            DeviceTier::Desktop => Argon2Params {
127                mem_kib: 512 * 1024,
128                t_cost: 4,
129                p_cost: 1,
130            },
131        }
132    }
133}
134
135#[cfg(test)]
136mod tests;