feat: X-Wing (draft -11), mutual exchange, WireGuard PSK per epoch, sealed box
X-Wing is composed from ml-kem, x25519-dalek, sha3 and shake and matches the draft's vectors. The PSK comes from a mutual exchange (each peer encapsulates to the other, QA A-15) bound to the full transcript in Arcanum's own HKDF layer (QA D-QA-1). Low-order X25519 values are refused. Deterministic entry points sit behind the off-by-default hazmat feature, enabled only for the tests.
This commit is contained in:
+65
-22
@@ -1,14 +1,17 @@
|
||||
//! Mandatory test 3, through the public API: an attacker can force the X25519 half to zero by
|
||||
//! sending a low-order point. Arcanum must refuse that with a typed error instead of producing a
|
||||
//! secret that only one half protects. (The combiner-level half of test 3, which zeroes each
|
||||
//! component directly, is `src/binding_tests.rs`.)
|
||||
//! sending a low-order point. Arcanum refuses that with a typed error instead of producing a
|
||||
//! secret that only one half protects (QA A-16, A-17). The combiner-level half of test 3, which
|
||||
//! zeroes each component directly, is `src/binding_tests.rs`. Also QA A-04 (raw ct_X is hashed).
|
||||
|
||||
mod common;
|
||||
|
||||
use arcanum::Error;
|
||||
use arcanum::exchange;
|
||||
use arcanum::xwing::{Ciphertext, PublicKey, SecretKey};
|
||||
use common::rng;
|
||||
|
||||
/// Curve25519 u-coordinates of small order, and non-canonical encodings of them (the list
|
||||
/// libsodium rejects). X25519 of any of these with any clamped scalar is all zeros.
|
||||
/// Curve25519 u-coordinates of small order and non-canonical encodings of them (libsodium's
|
||||
/// blocklist). Each is also used with bit 255 set, which X25519 ignores (RFC 7748 §5).
|
||||
const LOW_ORDER: [&str; 7] = [
|
||||
"0000000000000000000000000000000000000000000000000000000000000000",
|
||||
"0100000000000000000000000000000000000000000000000000000000000000",
|
||||
@@ -19,40 +22,80 @@ const LOW_ORDER: [&str; 7] = [
|
||||
"eeffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f",
|
||||
];
|
||||
|
||||
fn low_order_points() -> impl Iterator<Item = [u8; 32]> {
|
||||
LOW_ORDER.iter().map(|h| hex::decode(h).unwrap().try_into().unwrap())
|
||||
fn low_order_points() -> Vec<[u8; 32]> {
|
||||
LOW_ORDER
|
||||
.iter()
|
||||
.flat_map(|h| {
|
||||
let p: [u8; 32] = hex::decode(h).unwrap().try_into().unwrap();
|
||||
let mut high = p;
|
||||
high[31] |= 0x80;
|
||||
[p, high]
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Guards the fixture: if a byte above were mistyped, the point would not be low order.
|
||||
#[test]
|
||||
fn the_fixture_points_really_are_low_order() {
|
||||
let s = x25519_dalek::StaticSecret::from([0x5a; 32]);
|
||||
for p in low_order_points() {
|
||||
assert!(!s.diffie_hellman(&p.into()).was_contributory(), "{}", hex::encode(p));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn low_order_x25519_ciphertext_is_refused_by_the_responder() {
|
||||
let sk = SecretKey::from_seed([0x44; 32]);
|
||||
let (ct, _) = exchange::initiate_derand(sk.public_key(), b"ctx", &[0x55; 64]).unwrap();
|
||||
fn low_order_x25519_ciphertext_is_refused() {
|
||||
let i = SecretKey::from_seed([0x43; 32]);
|
||||
let r = SecretKey::from_seed([0x44; 32]);
|
||||
let init = exchange::initiate_derand(&i, r.public_key(), b"ctx", &[0x55; 64]).unwrap();
|
||||
let (ct_ri, _) = exchange::respond_derand(&r, i.public_key(), init.ciphertext(), b"ctx", &[0x56; 64]).unwrap();
|
||||
for point in low_order_points() {
|
||||
let mut bad = *ct.as_bytes();
|
||||
let mut bad = *init.ciphertext().as_bytes();
|
||||
bad[1088..].copy_from_slice(&point);
|
||||
let bad = Ciphertext::from_bytes(&bad).unwrap();
|
||||
assert_eq!(exchange::respond(&sk, &bad, b"ctx").err(), Some(Error::NonContributory));
|
||||
assert_eq!(sk.decapsulate(&bad).err(), Some(Error::NonContributory));
|
||||
assert_eq!(r.decapsulate(&bad).err(), Some(Error::NonContributory));
|
||||
let resp = exchange::respond_derand(&r, i.public_key(), &bad, b"ctx", &[0x56; 64]);
|
||||
assert_eq!(resp.err(), Some(Error::NonContributory));
|
||||
|
||||
let mut bad = *ct_ri.as_bytes();
|
||||
bad[1088..].copy_from_slice(&point);
|
||||
let again = exchange::initiate_derand(&i, r.public_key(), b"ctx", &[0x55; 64]).unwrap();
|
||||
assert_eq!(again.finish(&Ciphertext::from_bytes(&bad).unwrap()).err(), Some(Error::NonContributory));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn low_order_x25519_public_key_is_refused_by_the_initiator() {
|
||||
let sk = SecretKey::from_seed([0x44; 32]);
|
||||
fn low_order_x25519_public_key_is_refused() {
|
||||
let i = SecretKey::from_seed([0x43; 32]);
|
||||
let r = SecretKey::from_seed([0x44; 32]);
|
||||
for point in low_order_points() {
|
||||
let mut bad = *sk.public_key().as_bytes();
|
||||
let mut bad = *r.public_key().as_bytes();
|
||||
bad[1184..].copy_from_slice(&point);
|
||||
let pk = PublicKey::from_bytes(&bad).expect("the ML-KEM part is still valid");
|
||||
assert_eq!(exchange::initiate_derand(&pk, b"ctx", &[0x55; 64]).err(), Some(Error::NonContributory));
|
||||
assert_eq!(pk.encapsulate_derand(&[0x55; 64]).err(), Some(Error::NonContributory));
|
||||
assert_eq!(exchange::initiate_derand(&i, &pk, b"ctx", &[0x55; 64]).err(), Some(Error::NonContributory));
|
||||
assert_eq!(arcanum::seal::seal(&pk, b"x", b"", &mut rng(1)).err(), Some(Error::NonContributory));
|
||||
}
|
||||
}
|
||||
|
||||
/// The honest exchange is not affected by the check: the same keys with honest points still agree.
|
||||
/// QA A-04: X25519 ignores bit 255 of ct_X, so ct_X and ct_X ^ 0x80 give the same ss_X. The
|
||||
/// output must still differ, because the combiner hashes the raw bytes.
|
||||
#[test]
|
||||
fn the_high_bit_of_ct_x_is_bound() {
|
||||
let r = SecretKey::from_seed([0x44; 32]);
|
||||
let (ct, ss) = r.public_key().encapsulate(&mut rng(4)).unwrap();
|
||||
let mut b = *ct.as_bytes();
|
||||
b[1119] ^= 0x80;
|
||||
let out = r.decapsulate(&Ciphertext::from_bytes(&b).unwrap()).unwrap();
|
||||
assert_ne!(out.as_bytes(), ss.as_bytes());
|
||||
}
|
||||
|
||||
/// The honest exchange is not affected by the check.
|
||||
#[test]
|
||||
fn honest_points_pass_the_check() {
|
||||
let sk = SecretKey::from_seed([0x44; 32]);
|
||||
let (ct, a) = exchange::initiate_derand(sk.public_key(), b"ctx", &[0x55; 64]).unwrap();
|
||||
let b = exchange::respond(&sk, &ct, b"ctx").unwrap();
|
||||
assert_eq!(a.wireguard_psk(3).as_bytes(), b.wireguard_psk(3).as_bytes());
|
||||
let i = SecretKey::from_seed([0x43; 32]);
|
||||
let r = SecretKey::from_seed([0x44; 32]);
|
||||
let init = exchange::initiate_derand(&i, r.public_key(), b"ctx", &[0x55; 64]).unwrap();
|
||||
let (ct_ri, rs) = exchange::respond_derand(&r, i.public_key(), init.ciphertext(), b"ctx", &[0x56; 64]).unwrap();
|
||||
assert_eq!(init.finish(&ct_ri).unwrap().wireguard_psk(3).as_bytes(), rs.wireguard_psk(3).as_bytes());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user