ratchet: hybrid ML-KEM steps (KDF_RK v2, 2312-byte header, state v2)
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.
This commit is contained in:
parent
fec68e31ff
commit
b22abd7b2b
7 changed files with 1198 additions and 168 deletions
130
src/node.rs
130
src/node.rs
|
|
@ -2326,6 +2326,7 @@ fn bootstrap_ratchet(
|
|||
keys: &Keys,
|
||||
peer_pk: &[u8],
|
||||
peer_prekey: &[u8],
|
||||
hybrid: bool,
|
||||
) -> Result<RatchetState, String> {
|
||||
let peer_pk: [u8; 32] = peer_pk
|
||||
.try_into()
|
||||
|
|
@ -2343,7 +2344,20 @@ fn bootstrap_ratchet(
|
|||
let local = RatchetKeyPair::from_secret(ratchet_seed(&hk, &keys.identity_pk)?);
|
||||
let remote = ratchet_seed(&hk, &peer_pk)?;
|
||||
let remote_pub = X25519Public::from(&StaticSecret::from(remote)).to_bytes();
|
||||
Ok(RatchetState::bootstrap(root_key, local, remote_pub))
|
||||
// The peer's initial ratchet keypair is derived, so its ML-KEM half is too:
|
||||
// a hybrid friend needs no extra wire field at bootstrap. Epoch 0 itself
|
||||
// stays classical; the first rotation is the first hybrid step.
|
||||
let remote_kem = if hybrid {
|
||||
Some(RatchetKeyPair::from_secret(remote).kem_public().to_vec())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
Ok(RatchetState::bootstrap(
|
||||
root_key,
|
||||
local,
|
||||
remote_pub,
|
||||
remote_kem.as_deref(),
|
||||
))
|
||||
}
|
||||
|
||||
/// `HKDF(info = "ratchet-key" || pubkey)` — the initial ratchet secret both
|
||||
|
|
@ -2366,7 +2380,20 @@ fn ratchet_seed(hk: &Hkdf<Sha256>, pubkey: &[u8; 32]) -> Result<[u8; 32], String
|
|||
/// session behind.
|
||||
fn ratchet_for(store: &Store, keys: &Keys, friend_pk: &[u8]) -> Result<RatchetState, String> {
|
||||
if let Some(blob) = store.load_ratchet(friend_pk).map_err(|e| e.to_string())? {
|
||||
return RatchetState::from_bytes(&blob).map_err(|e| format!("ratchet state: {e}"));
|
||||
match RatchetState::from_bytes(&blob) {
|
||||
Ok(state) => return Ok(state),
|
||||
// Hard cut, exactly like F8: the *pre-hybrid* v1 blob has no KEM
|
||||
// ratchet key and is dropped, never half-migrated. The bootstrap
|
||||
// epoch is a pure function of the (unchanged) prekeys, so it is
|
||||
// rebuilt. A newer version is not an old format and must not be
|
||||
// reset silently.
|
||||
Err(crate::ratchet::Error::UnsupportedStateVersion(1)) => {
|
||||
store.clear_ratchet(friend_pk).map_err(|e| e.to_string())?;
|
||||
}
|
||||
// A corrupt *current* state, or one written by a newer client, is
|
||||
// surfaced rather than silently reset.
|
||||
Err(e) => return Err(format!("ratchet state: {e}")),
|
||||
}
|
||||
}
|
||||
let f = store
|
||||
.get_friend(friend_pk)
|
||||
|
|
@ -2375,7 +2402,7 @@ fn ratchet_for(store: &Store, keys: &Keys, friend_pk: &[u8]) -> Result<RatchetSt
|
|||
if f.prekey.len() != 32 {
|
||||
return Err("friend missing prekey".into());
|
||||
}
|
||||
bootstrap_ratchet(keys, friend_pk, &f.prekey)
|
||||
bootstrap_ratchet(keys, friend_pk, &f.prekey, f.kem_pk.is_some())
|
||||
}
|
||||
|
||||
/// Encode one chat send as a `rat ` frame, then persist the advanced state.
|
||||
|
|
@ -2908,6 +2935,103 @@ pub(crate) mod tests {
|
|||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_v1_ratchet_blob_is_dropped_and_rebuilt_not_misread() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let node = offline_node(&dir.path().join("bob"), Arc::new(NoDial));
|
||||
let peer = Keys::generate();
|
||||
{
|
||||
let store = node.store.lock().unwrap();
|
||||
befriend(&store, &peer.identity_pk, &peer.prekey_pk, "peer.onion");
|
||||
store
|
||||
.set_friend_kem(&peer.identity_pk, Some(&peer.kem_pk))
|
||||
.expect("friend kem");
|
||||
// Exactly the pre-hybrid on-disk shape: version 0x01, 210 bytes.
|
||||
let mut legacy = vec![0x01u8];
|
||||
legacy.resize(210, 0x5a);
|
||||
store
|
||||
.save_ratchet(&peer.identity_pk, &legacy)
|
||||
.expect("legacy row");
|
||||
}
|
||||
|
||||
let (frame, blob) = {
|
||||
let store = node.store.lock().unwrap();
|
||||
let keys = node.keys.lock().unwrap();
|
||||
let frame = encode_chat_frame(&store, &keys, &peer.identity_pk, b"after the cut")
|
||||
.expect("a pre-hybrid state is dropped and rebuilt, not fatal");
|
||||
let blob = store
|
||||
.load_ratchet(&peer.identity_pk)
|
||||
.expect("load ratchet")
|
||||
.expect("the rebuilt state was persisted");
|
||||
(frame, blob)
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
frame[4],
|
||||
crate::ratchet::FRAME_VERSION,
|
||||
"the rebuilt epoch 0 is classical, one message before the first step"
|
||||
);
|
||||
assert_eq!(
|
||||
blob[0],
|
||||
crate::ratchet::STATE_VERSION,
|
||||
"the store now holds a v2 state"
|
||||
);
|
||||
assert!(
|
||||
blob.len() > 210,
|
||||
"the rebuilt state carries the peer's KEM ratchet key"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_corrupt_current_ratchet_blob_is_an_error_not_a_silent_drop() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let node = offline_node(&dir.path().join("bob"), Arc::new(NoDial));
|
||||
let peer = Keys::generate();
|
||||
let store = node.store.lock().unwrap();
|
||||
befriend(&store, &peer.identity_pk, &peer.prekey_pk, "peer.onion");
|
||||
store
|
||||
.set_friend_kem(&peer.identity_pk, Some(&peer.kem_pk))
|
||||
.expect("friend kem");
|
||||
// A v2 blob missing its last byte: a hard cut must not paper over this
|
||||
// one, because a corrupt current state is not an old format.
|
||||
let mut corrupt = vec![crate::ratchet::STATE_VERSION];
|
||||
corrupt.resize(300, 0x5a);
|
||||
store
|
||||
.save_ratchet(&peer.identity_pk, &corrupt)
|
||||
.expect("row");
|
||||
let keys = node.keys.lock().unwrap();
|
||||
let err = encode_chat_frame(&store, &keys, &peer.identity_pk, b"no")
|
||||
.expect_err("a corrupt current state must fail, not re-bootstrap");
|
||||
assert!(
|
||||
err.contains("ratchet state"),
|
||||
"expected a state error, got: {err}"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_future_ratchet_blob_is_an_error_not_a_silent_downgrade() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let node = offline_node(&dir.path().join("bob"), Arc::new(NoDial));
|
||||
let peer = Keys::generate();
|
||||
let store = node.store.lock().unwrap();
|
||||
befriend(&store, &peer.identity_pk, &peer.prekey_pk, "peer.onion");
|
||||
store
|
||||
.set_friend_kem(&peer.identity_pk, Some(&peer.kem_pk))
|
||||
.expect("friend kem");
|
||||
// Only the *old* layout is a hard cut. A newer state must not be
|
||||
// silently reset to epoch 0 by an older client.
|
||||
let mut future = vec![crate::ratchet::STATE_VERSION + 1];
|
||||
future.resize(300, 0x5a);
|
||||
store.save_ratchet(&peer.identity_pk, &future).expect("row");
|
||||
let keys = node.keys.lock().unwrap();
|
||||
let err = encode_chat_frame(&store, &keys, &peer.identity_pk, b"no")
|
||||
.expect_err("a future state version must not be downgraded silently");
|
||||
assert!(
|
||||
err.contains("unsupported ratchet state version"),
|
||||
"expected a version error, got: {err}"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_prelude_less_session_against_a_kem_friend_is_rejected() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
|
|
|
|||
Loading…
Reference in a new issue