Files
arcanum/tests/binding.rs
T
mnovak 89b5329f06 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.
2026-11-25 07:38:55 +01:00

102 lines
4.5 KiB
Rust

//! Mandatory test 3, through the public API: an attacker can force the X25519 half to zero by
//! 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 (libsodium's
/// blocklist). Each is also used with bit 255 set, which X25519 ignores (RFC 7748 §5).
const LOW_ORDER: [&str; 7] = [
"0000000000000000000000000000000000000000000000000000000000000000",
"0100000000000000000000000000000000000000000000000000000000000000",
"e0eb7a7c3b41b8ae1656e3faf19fc46ada098deb9c32b1fd866205165f49b800",
"5f9c95bca3508c24b1d0b1559c83ef5b04445cc4581c8e86d8224eddd09f1157",
"ecffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f",
"edffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f",
"eeffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f",
];
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() {
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 = *init.ciphertext().as_bytes();
bad[1088..].copy_from_slice(&point);
let bad = Ciphertext::from_bytes(&bad).unwrap();
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() {
let i = SecretKey::from_seed([0x43; 32]);
let r = SecretKey::from_seed([0x44; 32]);
for point in low_order_points() {
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!(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));
}
}
/// 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 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());
}