//! The recovery key as a wrap recipient (spec §2.3). //! //! The Mac already mints an age X25519 identity for the backup //! (`cotton-client`'s `backup::age_key::ensure_identity`, at //! `/identity/backup.age`). The vault takes the SAME key as one more //! recipient: a `cotton_vault_keys` row with `device_id = 'recovery'`, holding //! the vault master key wrapped to that public half. One recovery key, one //! sentence to the person — *this opens your backup and your passwords* — and //! no second ceremony. //! //! An age key is bech32 and nothing else: the recipient is the 32-byte X25519 //! public key under the human-readable part `age`, and the identity is the //! 32-byte private scalar under `age-secret-key-`, shown upper-case. Those are //! filippo.io/age's own formats, so what these read is what `age-keygen` //! writes and what a person pastes back in. //! //! **Reading only.** Nothing here mints a key: the Mac's `ensure_identity` is //! the one place that does, and a second minting path would give a person two //! recovery keys, one of which silently opens half of what they think it does. use bech32::primitives::decode::CheckedHrpstring; use bech32::Bech32; use zeroize::Zeroizing; /// The human-readable part of an identity — `AGE-SECRET-KEY-1…` upper-cased. const IDENTITY_HRP: &str = "age-secret-key-"; /// The human-readable part of a recipient — `age1…`. const RECIPIENT_HRP: &str = "age"; /// The 32-byte X25519 public key inside an `age1…` recipient. /// /// `None` for anything that is not one: a wrong prefix, a bad checksum, a /// payload that is not 32 bytes. A recipient that does not parse is a recipient /// nothing is wrapped for, which is the safe end of the trade — the alternative /// is a wrap nobody can open, stored as if it worked. pub fn recipient_from_age_public(recipient: &str) -> Option<[u8; 32]> { decode(recipient, RECIPIENT_HRP).map(|bytes| *bytes) } /// The 32-byte private scalar inside an `AGE-SECRET-KEY-1…` identity. /// /// Returned inside [`Zeroizing`] so a caller that drops it without storing it /// leaves nothing behind. pub fn secret_from_age_identity(identity: &str) -> Option> { decode(&identity.to_lowercase(), IDENTITY_HRP) } /// The recipient an identity belongs to — how a restore checks that the key a /// person typed is the key their rows were wrapped to, without the caller ever /// handling the scalar. pub fn recipient_of_identity(identity: &str) -> Option<[u8; 32]> { let secret = secret_from_age_identity(identity)?; Some(super::wrap::public_of(&secret)) } fn decode(value: &str, hrp: &str) -> Option> { let checked = CheckedHrpstring::new::(value).ok()?; if !checked.hrp().as_str().eq_ignore_ascii_case(hrp) { return None; } let bytes: Vec = checked.byte_iter().collect(); let mut out = Zeroizing::new([0u8; 32]); if bytes.len() != out.len() { return None; } out.copy_from_slice(&bytes); Some(out) } #[cfg(test)] #[path = "recovery_tests.rs"] mod tests;