//! THE IDENTITY OF AN ACCOUNT'S VAULT KEY (VAULT GUARD 2 item 3, 28 Nov 2026): what a phone declares beside a vault_key //! write (`vmk_check`, `apps/business/src/vault_identity.ts`) so the object can tell a wrap of the account's key from a //! wrap of another without opening either. //! //! 16 bytes of HKDF-SHA256 over the key, naming one key at one version, base64url without padding. It hashes the key //! and its version ONLY, never a database id: [`super::chain::kcv`] takes the copy's workspace id, and a local copy //! founded before sign-in names its own local id while every signed-in copy of the account names the object's, so the //! same key would give two values. Here every copy and every device holding one key at one version gives one value. use base64::engine::general_purpose::URL_SAFE_NO_PAD; use base64::Engine as _; use hkdf::Hkdf; use sha2::Sha256; use super::seal::Vmk; const SALT: &[u8] = b"cotton vault key identity v1"; /// The identity of `key` at `version`: 16 bytes, base64url without padding (22 characters). #[must_use] pub fn of(key: &Vmk, version: u32) -> String { let mut out = [0u8; 16]; Hkdf::::new(Some(SALT), key.expose()) .expand(&version.to_be_bytes(), &mut out) // HKDF-SHA256 refuses only an output longer than 255 hashes. .expect("HKDF-SHA256 gives 16 bytes for any input material"); URL_SAFE_NO_PAD.encode(out) } #[cfg(test)] mod tests { use super::*; /// RULE (VAULT GUARD 2 item 3): one key at one version gives one identity wherever it is computed (no copy or /// database id goes in), a different key or version another, and it reads as the object's `vmk_check` expects. #[test] fn one_key_at_one_version_is_one_identity_on_every_copy() { let key = Vmk::from_bytes([7; 32]); let again = Vmk::from_bytes([7; 32]); assert_eq!(of(&key, 1), of(&again, 1), "the same key, held by another device or copy"); assert_ne!(of(&key, 1), of(&Vmk::from_bytes([8; 32]), 1)); assert_ne!(of(&key, 1), of(&key, 2)); let said = of(&key, 1); assert_eq!(said.len(), 22); assert!(said.chars().all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')); assert_ne!(said, super::super::chain::kcv(&key, "a-database", 1), "not the chain's value, which names a database"); } }