//! 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:<fingerprint>", vmk_version, seed)//! ```//!//! The sealed half's aad is therefore `person-key:<fingerprint>:<vmk_version>`://! 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::Engineas _;use ed25519_dalek::{Signature, Signer, SigningKey, VerifyingKey};use rand::RngCore;use zeroize::Zeroizing;usecrate::vault::seal::{open, seal, Vmk};/// How many characters of the person key a fingerprint is.pubconst 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.pubstructPersonKey { seed: Zeroizing<[u8; 32]>, public: [u8; 32],}implPersonKey {/// A fresh person key, from the OS. Made ONCE per person, at the first run/// of their personal database.pubfn mint() -> PersonKey {letmut seed = Zeroizing::new([0u8; 32]); rand::rngs::OsRng.fill_bytes(seed.as_mut());PersonKey::from_seed(seed) }/// The key a 32-byte seed makes.pubfn 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.pubfn public_key(&self) -> String { URL_SAFE_NO_PAD.encode(self.public) }/// What a screen shows and what the row is keyed by.pubfn fingerprint(&self) -> String {letmut text = self.public_key(); text.truncate(FINGERPRINT_LEN); text }/// Sign a message as this person.pubfn 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`.pubfn seal_private(&self, vmk: &Vmk, vmk_version: u32) -> Option<String> { 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.pubfn sealing(&self) -> (String, Zeroizing<Vec<u8>>) { ( 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`.pubfn open_private( vmk: &Vmk, public_key: &str, vmk_version: u32, sealed: &str, ) -> Option<PersonKey> {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`].pubfn from_opened(public_key: &str, opened: &[u8]) -> Option<PersonKey> {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::DebugforPersonKey {/// 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.pubfn 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.pubfn fingerprint(public_key: &str) -> Option<String> { 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.pubfn verify(public_key: &str, message: &[u8], signature: &[u8]) -> bool {letSome(public) = public_bytes(public_key) else {returnfalse; };letOk(key) = VerifyingKey::from_bytes(&public) else {returnfalse; };letOk(signature) = Signature::from_slice(signature) else {returnfalse; }; 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;