Skip to main content

weirdnet_keys/
profile.rs

1//! Label profiles: every domain label a key operation binds to.
2
3/// The labels key operations bind to. Two profiles with different labels derive different
4/// keys from the same material, and an envelope written under one is refused by the other.
5///
6/// [`Profile::WEIRDNET`] is the default for new data. An application with existing data
7/// defines its own profile holding its existing labels and passes it explicitly; nothing is
8/// converted between profiles.
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub struct Profile {
11    /// Recorded in new envelopes (unless `None`, which keeps envelopes that never had one).
12    pub name: Option<&'static str>,
13    /// The envelope `format` this profile writes and accepts.
14    pub envelope_format: u32,
15    /// Prefix for HKDF infos and associated data: `{label}/service/v{n}`, `{label}/kek`,
16    /// `{label}/dek` and `{label}/service-key/{db id}/v{n}`.
17    pub label: &'static str,
18    /// The first field of a shard's text form.
19    pub shard_prefix: &'static str,
20    /// The fixed Argon2id salt of password claim tags.
21    pub claim_salt: &'static [u8],
22    /// Associated data of a sealed dark shard: `{dark_shard_label}|{set}|{index}`.
23    pub dark_shard_label: &'static str,
24}
25
26impl Profile {
27    pub const WEIRDNET: Profile = Profile {
28        name: Some("weirdnet/keys/v1"),
29        envelope_format: 2,
30        label: "weirdnet/keys/v1",
31        shard_prefix: "weirdnet-shard-v1",
32        claim_salt: b"weirdnet/keys/v1/shard-claim",
33        dark_shard_label: "weirdnet/keys/v1/dark-shard",
34    };
35
36    pub fn service_info(&self, version: u32) -> String {
37        format!("{}/service/v{version}", self.label)
38    }
39    pub fn kek_info(&self) -> String {
40        format!("{}/kek", self.label)
41    }
42    pub fn dek_aad(&self) -> Vec<u8> {
43        format!("{}/dek", self.label).into_bytes()
44    }
45    pub fn service_key_aad(&self, db_id: &str, version: u32) -> Vec<u8> {
46        format!("{}/service-key/{db_id}/v{version}", self.label).into_bytes()
47    }
48    pub fn dark_shard_aad(&self, set: &str, index: u32) -> Vec<u8> {
49        format!("{}|{set}|{index}", self.dark_shard_label).into_bytes()
50    }
51}
52
53impl Default for Profile {
54    fn default() -> Self {
55        Profile::WEIRDNET
56    }
57}