Skip to main content

zaun_community_protocol/crypto/voice/
identity.rs

1//! A participant's X-Wing key pair, made fresh for each voice session.
2
3use std::fmt;
4
5use hpke::kem::XWing;
6use hpke::{Deserializable, Kem, Serializable};
7use zeroize::Zeroizing;
8
9use super::{KEM_PUBLIC_LEN, VoiceError};
10use crate::ids::fill_random;
11
12pub(super) type XWingSecret = <XWing as Kem>::PrivateKey;
13pub(super) type XWingPublic = <XWing as Kem>::PublicKey;
14
15/// An X-Wing encapsulation key (ML-KEM-768 ‖ X25519, 1 216 bytes), validated on parse.
16#[derive(Clone, PartialEq, Eq)]
17pub struct VoicePublicKey {
18    bytes: [u8; KEM_PUBLIC_LEN],
19    key: XWingPublic,
20}
21
22impl VoicePublicKey {
23    /// Parses and validates the encoding (the ML-KEM-768 part must be canonical).
24    ///
25    /// # Errors
26    /// [`VoiceError::PublicKey`] for an invalid key, [`VoiceError::Length`] for a wrong length.
27    pub fn from_bytes(bytes: &[u8]) -> Result<Self, VoiceError> {
28        let arr: [u8; KEM_PUBLIC_LEN] = bytes
29            .try_into()
30            .map_err(|_| VoiceError::Length("voice public key"))?;
31        let key = XWingPublic::from_bytes(&arr).map_err(|_| VoiceError::PublicKey)?;
32        Ok(Self { bytes: arr, key })
33    }
34
35    /// The 1 216-byte encoding.
36    #[must_use]
37    pub const fn as_bytes(&self) -> &[u8; KEM_PUBLIC_LEN] {
38        &self.bytes
39    }
40
41    pub(super) const fn key(&self) -> &XWingPublic {
42        &self.key
43    }
44}
45
46impl fmt::Debug for VoicePublicKey {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        let h = crate::crypto::content_hash(&self.bytes);
49        write!(f, "VoicePublicKey(sha3:{})", crate::b64::encode(&h[..8]))
50    }
51}
52
53/// A voice session's X-Wing key pair. The secret is the 32-byte X-Wing seed, zeroized on drop
54/// and never printed. It lives only as long as the voice session; it is never stored.
55pub struct VoiceIdentity {
56    secret: XWingSecret,
57    public: VoicePublicKey,
58}
59
60impl VoiceIdentity {
61    /// A fresh key pair from the OS CSPRNG.
62    ///
63    /// # Errors
64    /// [`VoiceError::Rng`] when the CSPRNG fails.
65    pub fn generate() -> Result<Self, VoiceError> {
66        let mut seed = Zeroizing::new([0u8; 32]);
67        fill_random(seed.as_mut())?;
68        Self::from_seed(&seed)
69    }
70
71    /// The key pair of an X-Wing seed (the X-Wing decapsulation key, draft §5.2). Tests and
72    /// known-answer vectors only; real sessions use [`Self::generate`].
73    ///
74    /// # Errors
75    /// [`VoiceError::PublicKey`] if the derived public key does not re-parse (never expected).
76    pub fn from_seed(seed: &[u8; 32]) -> Result<Self, VoiceError> {
77        let secret = XWingSecret::from_bytes(seed).map_err(|_| VoiceError::PublicKey)?;
78        let public = XWing::sk_to_pk(&secret);
79        let public = VoicePublicKey::from_bytes(&public.to_bytes())?;
80        Ok(Self { secret, public })
81    }
82
83    /// The public half, published in the key package.
84    #[must_use]
85    pub const fn public_key(&self) -> &VoicePublicKey {
86        &self.public
87    }
88
89    pub(super) const fn secret(&self) -> &XWingSecret {
90        &self.secret
91    }
92}
93
94impl fmt::Debug for VoiceIdentity {
95    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
96        f.debug_struct("VoiceIdentity")
97            .field("public", &self.public)
98            .finish_non_exhaustive()
99    }
100}