//! A person's member key, the team identity, and sealing the team key to a member (docs/TEAM.md, "The member key", //! "The team vault file"). use std::fmt; use custos_seal::Recipient; use ed25519_dalek::{Signature, Signer, SigningKey, VerifyingKey}; use zeroize::Zeroizing; use crate::{TeamError, hex, sha256}; /// The size of a sealed team key: custos-seal's box around 32 bytes. pub const SEALED_KEY_LEN: usize = 4 + 32 + 24 + 32 + 16; /// The public half of a member key, as the roster and the service keep it. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct MemberPublic { pub ed25519: [u8; 32], pub x25519: [u8; 32], } impl MemberPublic { /// The member id: the first 16 bytes of `SHA-256("custos member v1" ‖ ed25519 ‖ x25519)`, 32 hex. pub fn id(&self) -> String { hex(&self.id_bytes()) } pub fn id_bytes(&self) -> [u8; 16] { let mut m = Vec::with_capacity(16 + 64); m.extend_from_slice(b"custos member v1"); m.extend_from_slice(&self.ed25519); m.extend_from_slice(&self.x25519); sha256(&m)[..16].try_into().expect("16 bytes") } /// The code a joining member's screen shows and the approving member compares: `match_code(x25519)`. pub fn join_code(&self) -> String { custos_seal::match_code(&self.x25519) } /// Both keys must be usable: an Ed25519 point and a contributory X25519 key. pub fn check(&self) -> Result<(), TeamError> { VerifyingKey::from_bytes(&self.ed25519).map_err(|_| TeamError::BadKey)?; custos_seal::seal(&self.x25519, b"check", b"").map_err(|_| TeamError::BadKey)?; Ok(()) } /// Checks an Ed25519 signature by this member over `msg`. pub fn verify(&self, msg: &[u8], sig: &[u8; 64]) -> bool { verify(&self.ed25519, msg, sig) } } pub(crate) fn verify(public: &[u8; 32], msg: &[u8], sig: &[u8; 64]) -> bool { let Ok(key) = VerifyingKey::from_bytes(public) else { return false; }; key.verify_strict(msg, &Signature::from_bytes(sig)).is_ok() } /// The context a team key is sealed under for one member slot. pub fn key_context(team: &str, epoch: u32, member: &str) -> Vec { format!("custos team key v1:{team}:{epoch}:{member}").into_bytes() } /// Seals the 32-byte team key to a member for one team and epoch: the member slot's box ([`SEALED_KEY_LEN`] bytes). pub fn seal_team_key(team_key: &[u8; 32], to: &MemberPublic, team: &str, epoch: u32) -> Result, TeamError> { custos_seal::seal(&to.x25519, &key_context(team, epoch, &to.id()), team_key).map_err(|_| TeamError::BadKey) } /// A person's member key. Lives in their own vault; wiped on drop; prints `[redacted]`. pub struct MemberKey { x: Recipient, ed: SigningKey, } impl MemberKey { /// A new member key from the OS CSPRNG. pub fn generate() -> MemberKey { let ed = custos_seal::random_bytes(); MemberKey { x: Recipient::generate(), ed: SigningKey::from_bytes(&ed) } } /// The key as the member's vault keeps it. pub fn from_secrets(x25519: Zeroizing<[u8; 32]>, ed25519: Zeroizing<[u8; 32]>) -> Result { let x = Recipient::from_secret(x25519).map_err(|_| TeamError::BadKey)?; Ok(MemberKey { x, ed: SigningKey::from_bytes(&ed25519) }) } /// The X25519 secret, for the member's own vault only. pub fn x25519_secret(&self) -> Zeroizing<[u8; 32]> { self.x.secret_bytes() } /// The Ed25519 seed, for the member's own vault only. pub fn ed25519_secret(&self) -> Zeroizing<[u8; 32]> { Zeroizing::new(self.ed.to_bytes()) } pub fn public(&self) -> MemberPublic { MemberPublic { ed25519: self.ed.verifying_key().to_bytes(), x25519: self.x.public() } } pub fn id(&self) -> String { self.public().id() } /// Opens this member's slot of a team vault: the team key. pub fn open_team_key(&self, sealed: &[u8], team: &str, epoch: u32) -> Result, TeamError> { let k = self.x.open(sealed, &key_context(team, epoch, &self.id())).map_err(|_| TeamError::NotAMember)?; let k: [u8; 32] = k.as_slice().try_into().map_err(|_| TeamError::NotAMember)?; Ok(Zeroizing::new(k)) } /// Signs `msg` with the member's Ed25519 key (approval statements only; the domain is the caller's prefix). pub(crate) fn sign(&self, msg: &[u8]) -> [u8; 64] { self.ed.sign(msg).to_bytes() } /// Opens a box sealed to this member's X25519 key (for a joiner: nothing else is ever sealed to it). pub fn recipient(&self) -> &Recipient { &self.x } } impl fmt::Debug for MemberKey { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str("[redacted]") } } /// The team identity: an Ed25519 key in the team body that signs the policy and the proofs, and never rotates. pub struct TeamIdentity(SigningKey); /// Domain prefixes, so a signature of one kind never passes for another. pub const POLICY_DOMAIN: &[u8] = b"custos team policy v1\0"; pub const PROOF_DOMAIN: &[u8] = b"custos team proof v1\0"; impl TeamIdentity { pub fn generate() -> TeamIdentity { TeamIdentity(SigningKey::from_bytes(&custos_seal::random_bytes())) } pub fn from_secret(secret: &[u8; 32]) -> TeamIdentity { TeamIdentity(SigningKey::from_bytes(secret)) } pub fn secret(&self) -> Zeroizing<[u8; 32]> { Zeroizing::new(self.0.to_bytes()) } pub fn public(&self) -> [u8; 32] { self.0.verifying_key().to_bytes() } pub fn sign_policy(&self, text: &str) -> [u8; 64] { self.0.sign(&[POLICY_DOMAIN, text.as_bytes()].concat()).to_bytes() } pub fn sign_proof(&self, text: &str) -> [u8; 64] { self.0.sign(&[PROOF_DOMAIN, text.as_bytes()].concat()).to_bytes() } } impl fmt::Debug for TeamIdentity { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str("[redacted]") } } /// Checks the team identity's signature on a policy text. pub fn verify_policy_sig(identity: &[u8; 32], text: &str, sig: &[u8; 64]) -> bool { verify(identity, &[POLICY_DOMAIN, text.as_bytes()].concat(), sig) } /// Checks the team identity's signature on a proof text. pub fn verify_proof_sig(identity: &[u8; 32], text: &str, sig: &[u8; 64]) -> bool { verify(identity, &[PROOF_DOMAIN, text.as_bytes()].concat(), sig) }