Every ratchet keypair is {x25519, ml-kem-768}. On a rotation the stepper
encapsulates to the receiver's current ratchet KEM key; both the X25519 DH
and the ML-KEM secret feed KDF_RK under the new onionwire-ratchet-v2-kdfrk
domain, so a later X25519 break does not open a chain established by a
hybrid step.
Epoch 0 stays classical by construction: the bootstrap root must be a pure
function of the two long-lived prekeys for both-send-first convergence, and
a per-session ss_kem differs for every one-shot Noise dial. The first
rotation (including the bootstrap tie-break rotation) is hybrid, so the
residual HNDL window is exactly the epoch-0 chain. docs/THREAT_MODEL.md
states that window; no full PQ forward secrecy is claimed.
Header 0x02 adds kem_pub[1184] || kem_ct[1088] to the 40-byte classical
header and is used on every frame of a hybrid chain, so a lost first frame
is harmless. A step with no KEM section is rejected (hybrid step required)
and a hybrid step at a classical friend is rejected (unexpected hybrid
step): no classical fallback.
State moves to blob v0.2: fixed 211-byte prefix plus flagged KEM fields.
A v1 blob is dropped and re-bootstrapped (hard cut, like F8); a corrupt v2
or a newer version is an error, not a silent reset.
Tests: hybrid root agreement on both sides, both-send-first convergence
with hybrid keys, no-downgrade, tampered kem_pub/kem_ct fail closed,
MAX_SKIP on a hybrid chain, classical friends still step classically,
state freeze at v0.2 and the node-level hard cut.
106 lines
3.5 KiB
Rust
106 lines
3.5 KiB
Rust
//! ML-KEM-768 (FIPS 203) install key.
|
|
//!
|
|
//! The decapsulation key is stored as the 64-byte seed (`DecapsulationKey::to_seed`),
|
|
//! the form the crate prefers. The 1184-byte encapsulation key is what an invite
|
|
//! carries. Ciphertext is 1088 bytes; the shared secret is 32.
|
|
|
|
use ml_kem::kem::{Decapsulate, Encapsulate, Kem};
|
|
use ml_kem::{DecapsulationKey768, EncapsulationKey768, KeyExport, MlKem768, Seed};
|
|
use zeroize::Zeroizing;
|
|
|
|
pub const PK_LEN: usize = 1184;
|
|
pub const CT_LEN: usize = 1088;
|
|
pub const SS_LEN: usize = 32;
|
|
pub const SEED_LEN: usize = 64;
|
|
|
|
pub type Result<T> = std::result::Result<T, Error>;
|
|
|
|
#[derive(Debug)]
|
|
pub struct Error(String);
|
|
|
|
impl std::fmt::Display for Error {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
self.0.fmt(f)
|
|
}
|
|
}
|
|
|
|
impl std::error::Error for Error {}
|
|
|
|
/// A freshly generated install key. `seed` wipes on drop.
|
|
pub struct Keypair {
|
|
pub seed: Zeroizing<[u8; SEED_LEN]>,
|
|
pub pk: [u8; PK_LEN],
|
|
}
|
|
|
|
pub fn generate() -> Keypair {
|
|
let (dk, ek) = MlKem768::generate_keypair();
|
|
let seed_arr = dk.to_seed().expect("generate_keypair yields a seed");
|
|
let pk_arr = ek.to_bytes();
|
|
let mut seed = Zeroizing::new([0u8; SEED_LEN]);
|
|
let mut pk = [0u8; PK_LEN];
|
|
seed.copy_from_slice(seed_arr.as_slice());
|
|
pk.copy_from_slice(pk_arr.as_slice());
|
|
Keypair { seed, pk }
|
|
}
|
|
|
|
/// Canonical encapsulation-key bytes. Rejects a length or a key the crate
|
|
/// will not load, so a garbage `kem=` never becomes a friend row.
|
|
pub fn encode_pk(pk: &[u8]) -> Result<Vec<u8>> {
|
|
let ek = encapsulation_key(pk)?;
|
|
Ok(ek.to_bytes().as_slice().to_vec())
|
|
}
|
|
|
|
pub fn decode_pk(bytes: &[u8]) -> Result<Vec<u8>> {
|
|
encode_pk(bytes)
|
|
}
|
|
|
|
pub fn encapsulate(pk: &[u8]) -> Result<(Vec<u8>, Vec<u8>)> {
|
|
let ek = encapsulation_key(pk)?;
|
|
let (ct, ss) = ek.encapsulate();
|
|
Ok((ct.as_slice().to_vec(), ss.as_slice().to_vec()))
|
|
}
|
|
|
|
/// Ciphertext bytes. Length is the only check: ML-KEM decapsulation of a
|
|
/// wrong ciphertext does not error (implicit rejection), so a bad ct is
|
|
/// caught by the handshake prologue, not here.
|
|
pub fn encode_ct(ct: &[u8]) -> Result<Vec<u8>> {
|
|
if ct.len() != CT_LEN {
|
|
return Err(Error("ct length".into()));
|
|
}
|
|
Ok(ct.to_vec())
|
|
}
|
|
|
|
pub fn decode_ct(bytes: &[u8]) -> Result<Vec<u8>> {
|
|
encode_ct(bytes)
|
|
}
|
|
|
|
pub fn decapsulate(seed: &[u8], ct: &[u8]) -> Result<Vec<u8>> {
|
|
if ct.len() != CT_LEN {
|
|
return Err(Error("ct length".into()));
|
|
}
|
|
let seed_arr = Seed::try_from(seed).map_err(|_| Error("seed length".into()))?;
|
|
let dk = DecapsulationKey768::from_seed(seed_arr);
|
|
let ss = dk
|
|
.decapsulate_slice(ct)
|
|
.map_err(|_| Error("ct length".into()))?;
|
|
Ok(ss.as_slice().to_vec())
|
|
}
|
|
|
|
/// Canonical encapsulation key for a stored 64-byte decapsulation seed.
|
|
///
|
|
/// The ratchet derives its KEM half from the X25519 ratchet secret, so it never
|
|
/// stores the ML-KEM seed separately; this recomputes the public half.
|
|
pub fn pk_from_seed(seed: &[u8]) -> Result<Vec<u8>> {
|
|
let seed_arr = Seed::try_from(seed).map_err(|_| Error("seed length".into()))?;
|
|
let dk = DecapsulationKey768::from_seed(seed_arr);
|
|
Ok(dk.encapsulation_key().to_bytes().as_slice().to_vec())
|
|
}
|
|
|
|
fn encapsulation_key(pk: &[u8]) -> Result<EncapsulationKey768> {
|
|
if pk.len() != PK_LEN {
|
|
return Err(Error("pk length".into()));
|
|
}
|
|
let key = ml_kem::kem::Key::<EncapsulationKey768>::try_from(pk)
|
|
.map_err(|_| Error("pk length".into()))?;
|
|
EncapsulationKey768::new(&key).map_err(|_| Error("invalid encapsulation key".into()))
|
|
}
|