//! The person key — an Ed25519 pair made once per person. //! //! ```text //! person key = base64url_nopad(public half) what a fact names //! fingerprint = the first 8 characters of the person key what a screen shows, and the row's id //! sealed half = vault::seal(vmk, "person-key:", vmk_version, seed) //! ``` //! //! The sealed half's aad is therefore `person-key::`: //! it opens only as THIS person's key under THIS version of the personal //! database's vault key, so a sealed value copied onto another row, or left //! behind by a rotation, fails at the open instead of signing as somebody. //! //! **The private half has three ways out of this type and no fourth:** //! [`PersonKey::seal_private`] (into `cotton_person_keys.sealed_private`), //! [`PersonKey::sign`] (into a signature), and the drop that wipes it. It is //! not `Clone`, not `Serialize`, and its `Debug` prints the fingerprint only. use base64::engine::general_purpose::URL_SAFE_NO_PAD; use base64::Engine as _; use ed25519_dalek::{Signature, Signer, SigningKey, VerifyingKey}; use rand::RngCore; use zeroize::Zeroizing; use crate::vault::seal::{open, seal, Vmk}; /// How many characters of the person key a fingerprint is. pub const FINGERPRINT_LEN: usize = 8; /// The prefix of the id a person key's private half is sealed under. const SEAL_ID_PREFIX: &str = "person-key:"; /// A person key with its private half in memory. pub struct PersonKey { seed: Zeroizing<[u8; 32]>, public: [u8; 32], } impl PersonKey { /// A fresh person key, from the OS. Made ONCE per person, at the first run /// of their personal database. pub fn mint() -> PersonKey { let mut seed = Zeroizing::new([0u8; 32]); rand::rngs::OsRng.fill_bytes(seed.as_mut()); PersonKey::from_seed(seed) } /// The key a 32-byte seed makes. pub fn from_seed(seed: Zeroizing<[u8; 32]>) -> PersonKey { let public = SigningKey::from_bytes(&seed).verifying_key().to_bytes(); PersonKey { seed, public } } /// The person key as a fact names it: the public half, base64url. pub fn public_key(&self) -> String { URL_SAFE_NO_PAD.encode(self.public) } /// What a screen shows and what the row is keyed by. pub fn fingerprint(&self) -> String { let mut text = self.public_key(); text.truncate(FINGERPRINT_LEN); text } /// Sign a message as this person. pub fn sign(&self, message: &[u8]) -> [u8; 64] { SigningKey::from_bytes(&self.seed).sign(message).to_bytes() } /// The private half, sealed for `cotton_person_keys.sealed_private`. pub fn seal_private(&self, vmk: &Vmk, vmk_version: u32) -> Option { seal(vmk, &seal_id(&self.fingerprint()), vmk_version, self.seed.as_ref()) } /// The private half, as a rail that seals by id takes it: the id to seal /// under and the bytes. For the server, whose vault key is behind a rail. pub fn sealing(&self) -> (String, Zeroizing>) { ( seal_id(&self.fingerprint()), Zeroizing::new(self.seed.to_vec()), ) } /// Open a sealed private half. `None` for a wrong vault key, a wrong /// version, a tampered value — and for a value that opens to a key whose /// public half is NOT the one the row names, so a row cannot be made to /// sign as another person by an edit to `public_key`. pub fn open_private( vmk: &Vmk, public_key: &str, vmk_version: u32, sealed: &str, ) -> Option { let opened = Zeroizing::new(open( vmk, &seal_id(&fingerprint(public_key)?), vmk_version, sealed, )?); PersonKey::from_opened(public_key, &opened) } /// [`PersonKey::open_private`] for a caller whose rail has already opened /// the bytes under [`seal_id`]. pub fn from_opened(public_key: &str, opened: &[u8]) -> Option { let seed: [u8; 32] = opened.try_into().ok()?; let key = PersonKey::from_seed(Zeroizing::new(seed)); (key.public_key() == public_key).then_some(key) } } impl std::fmt::Debug for PersonKey { /// Never the seed. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "PersonKey({})", self.fingerprint()) } } /// The id a person key's private half is sealed under. pub fn seal_id(fingerprint: &str) -> String { format!("{SEAL_ID_PREFIX}{fingerprint}") } /// The fingerprint of a person key. `None` unless the text is the base64url of /// a 32-byte Ed25519 public key. pub fn fingerprint(public_key: &str) -> Option { public_bytes(public_key)?; Some(public_key[..FINGERPRINT_LEN].to_string()) } /// Does this signature verify as this person's, over this message? /// /// `false` for a person key that is not a key and a signature that is not 64 /// bytes, as for a signature that does not verify: the caller refuses in every /// case. pub fn verify(public_key: &str, message: &[u8], signature: &[u8]) -> bool { let Some(public) = public_bytes(public_key) else { return false; }; let Ok(key) = VerifyingKey::from_bytes(&public) else { return false; }; let Ok(signature) = Signature::from_slice(signature) else { return false; }; key.verify_strict(message, &signature).is_ok() } fn public_bytes(public_key: &str) -> Option<[u8; 32]> { let bytes = URL_SAFE_NO_PAD.decode(public_key).ok()?; let bytes: [u8; 32] = bytes.try_into().ok()?; VerifyingKey::from_bytes(&bytes).ok()?; Some(bytes) } #[cfg(test)] #[path = "person_tests.rs"] mod tests;