Skip to main content

capsule_core/
cohort.rs

1//! Device-cohort hash — the advisory session-grouping aid (SSoT:
2//! [Authentication — Device Cohorts]).
3//!
4//! One deterministic hash groups a physical device's sessions across app
5//! reinstalls (reset-stability is platform-limited and documented honestly in
6//! the owner doc). The construction is domain-separated canonical CBOR — never
7//! naive concatenation — and folds the `user_id` in so the same device under
8//! two accounts yields unlinkable hashes.
9//!
10//! **Advisory-only invariant:** the value is client-asserted and unverifiable;
11//! no authorization or capability decision may ever read it.
12//!
13//! [Authentication — Device Cohorts]: https://docs/design/authentication/#device-cohorts
14
15use ciborium::value::Value;
16use uuid::Uuid;
17
18use crate::cbor;
19use crate::crypto::hash::{Hash32, hash_bytes};
20
21/// Domain-separation label (versioned; a construction change bumps the suffix).
22pub const DEVICE_COHORT_V1: &str = "capsule-device-cohort/v1";
23
24/// Closed platform tag (wire value is the lowercase name; a new platform is an
25/// additive protocol change).
26#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
27pub enum PlatformTag {
28    Ios,
29    Android,
30    Macos,
31    Windows,
32    Linux,
33}
34
35impl PlatformTag {
36    pub fn as_str(self) -> &'static str {
37        match self {
38            PlatformTag::Ios => "ios",
39            PlatformTag::Android => "android",
40            PlatformTag::Macos => "macos",
41            PlatformTag::Windows => "windows",
42            PlatformTag::Linux => "linux",
43        }
44    }
45}
46
47/// The cohort hash:
48/// `SHA-256( canonical-CBOR([ "capsule-device-cohort/v1", user_id, platform_tag, primary_id ]) )`.
49///
50/// `primary_id` is the platform's single primary identifier per the owner doc's
51/// table (Keychain seed / SSAID / IOPlatformUUID / MachineGuid / machine-id) —
52/// exactly one input per platform, chosen for stability, never a concatenated
53/// fingerprint.
54pub fn cohort_hash(user_id: Uuid, platform: PlatformTag, primary_id: &str) -> Hash32 {
55    let array = Value::Array(vec![
56        Value::Text(DEVICE_COHORT_V1.to_string()),
57        Value::Bytes(user_id.as_bytes().to_vec()),
58        Value::Text(platform.as_str().to_string()),
59        Value::Text(primary_id.to_string()),
60    ]);
61    hash_bytes(&cbor::value_to_canonical_vec(&array))
62}
63
64#[cfg(test)]
65mod tests;