Files
arcanum/tests/malformed.rs
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

182 lines
7.5 KiB
Rust

//! Mandatory test 5: malformed ciphertexts and keys of the wrong length are rejected with typed
//! errors, never a panic. The same entry points are fuzzed by fuzz/fuzz_targets/untrusted_input.rs.
mod common;
use arcanum::xwing::{CIPHERTEXT_LEN, Ciphertext, PUBLIC_KEY_LEN, PublicKey, SECRET_KEY_LEN, SecretKey};
use arcanum::{Error, Input, exchange, seal};
use common::rng;
use rand_core::Rng;
const LENGTHS: [usize; 12] = [0, 1, 31, 32, 33, 64, 1087, 1088, 1119, 1121, 1215, 1217];
#[test]
fn public_key_of_the_wrong_length_is_a_typed_error() {
for len in LENGTHS.into_iter().chain([PUBLIC_KEY_LEN * 2]) {
let err = PublicKey::from_bytes(&vec![0u8; len]).unwrap_err();
assert_eq!(err, Error::InvalidLength { input: Input::PublicKey, expected: PUBLIC_KEY_LEN, actual: len });
}
}
#[test]
fn ciphertext_of_the_wrong_length_is_a_typed_error() {
for len in LENGTHS.into_iter().filter(|&l| l != CIPHERTEXT_LEN).chain([CIPHERTEXT_LEN * 2]) {
let err = Ciphertext::from_bytes(&vec![0u8; len]).unwrap_err();
assert_eq!(err, Error::InvalidLength { input: Input::Ciphertext, expected: CIPHERTEXT_LEN, actual: len });
}
}
#[test]
fn secret_key_of_the_wrong_length_is_a_typed_error() {
for len in LENGTHS.into_iter().filter(|&l| l != SECRET_KEY_LEN) {
let err = SecretKey::from_bytes(&vec![0u8; len]).unwrap_err();
assert_eq!(err, Error::InvalidLength { input: Input::SecretKey, expected: SECRET_KEY_LEN, actual: len });
}
assert!(SecretKey::from_bytes(&[7u8; SECRET_KEY_LEN]).is_ok(), "any 32 bytes are a valid seed");
}
/// An ML-KEM key whose coefficients are not reduced mod q fails FIPS 203 §7.2: all-0xff bytes
/// encode 12-bit values of 4095 > q = 3329.
#[test]
fn ml_kem_key_failing_the_modulus_check_is_a_typed_error() {
let mut bad = [0xffu8; PUBLIC_KEY_LEN];
bad[1184..].copy_from_slice(&[9u8; 32]);
assert_eq!(PublicKey::from_bytes(&bad).unwrap_err(), Error::InvalidPublicKey);
}
#[test]
fn random_bytes_never_panic() {
let mut r = rng(9);
let sk = SecretKey::from_seed([0x01; 32]);
let peer = SecretKey::from_seed([0x02; 32]);
let (honest_ct, _) = peer.public_key().encapsulate(&mut r).unwrap();
for round in 0..512 {
let len = [PUBLIC_KEY_LEN, CIPHERTEXT_LEN, seal::OVERHEAD, seal::OVERHEAD + 40, 0, 17][round % 6];
let mut buf = vec![0u8; len];
r.fill_bytes(&mut buf);
if let Ok(pk) = PublicKey::from_bytes(&buf) {
let _ = exchange::initiate(&sk, &pk, b"ctx", &mut r);
let _ = exchange::respond(&sk, &pk, &honest_ct, b"ctx", &mut r);
}
if let Ok(ct) = Ciphertext::from_bytes(&buf) {
// Random ML-KEM ciphertexts decapsulate (implicit rejection) to some unrelated secret.
let _ = exchange::respond(&sk, peer.public_key(), &ct, b"ctx", &mut r);
let _ = exchange::initiate(&sk, peer.public_key(), b"ctx", &mut r).unwrap().finish(&ct);
}
assert!(seal::open(&sk, &buf, b"aad").is_err(), "random bytes must never open");
}
}
#[test]
fn sealed_box_round_trips() {
let mut r = rng(10);
let sk = SecretKey::generate(&mut r);
for len in [0usize, 1, 15, 16, 17, 1000, 70_000] {
let msg: Vec<u8> = (0..len).map(|i| (i * 31 % 251) as u8).collect();
let sealed = seal::seal(sk.public_key(), &msg, b"custos/v1 item 42", &mut r).unwrap();
assert_eq!(sealed.len(), msg.len() + seal::OVERHEAD);
let opened = seal::open(&sk, &sealed, b"custos/v1 item 42").unwrap();
assert_eq!(opened.as_slice(), msg.as_slice());
}
}
#[test]
fn sealed_box_too_short_is_a_typed_error() {
let sk = SecretKey::from_seed([0x02; 32]);
for len in [0, 1, seal::OVERHEAD - 1] {
let err = seal::open(&sk, &vec![1u8; len], b"").unwrap_err();
assert_eq!(err, Error::TooShort { input: Input::SealedBox, minimum: seal::OVERHEAD, actual: len });
}
}
#[test]
fn sealed_box_with_an_unknown_version_is_a_typed_error() {
let mut r = rng(11);
let sk = SecretKey::generate(&mut r);
let mut sealed = seal::seal(sk.public_key(), b"hello", b"", &mut r).unwrap();
for v in [0u8, 2, 0xff] {
sealed[0] = v;
assert_eq!(seal::open(&sk, &sealed, b"").unwrap_err(), Error::UnsupportedVersion(v));
}
}
#[test]
fn every_tampered_or_truncated_sealed_box_is_refused() {
let mut r = rng(12);
let sk = SecretKey::generate(&mut r);
let sealed = seal::seal(sk.public_key(), b"the household photo key", b"aad", &mut r).unwrap();
for i in 1..sealed.len() {
let mut bad = sealed.clone();
bad[i] ^= 0x80;
let err = seal::open(&sk, &bad, b"aad").unwrap_err();
assert!(matches!(err, Error::DecryptionFailed | Error::NonContributory), "byte {i}: {err:?}");
}
for len in seal::OVERHEAD..sealed.len() {
assert_eq!(seal::open(&sk, &sealed[..len], b"aad").unwrap_err(), Error::DecryptionFailed, "length {len}");
}
}
#[test]
fn sealed_box_needs_the_right_key_and_aad() {
let mut r = rng(13);
let sk = SecretKey::generate(&mut r);
let other = SecretKey::generate(&mut r);
let sealed = seal::seal(sk.public_key(), b"secret", b"aad", &mut r).unwrap();
assert_eq!(seal::open(&other, &sealed, b"aad").unwrap_err(), Error::DecryptionFailed);
assert_eq!(seal::open(&sk, &sealed, b"aaD").unwrap_err(), Error::DecryptionFailed);
assert_eq!(seal::open(&sk, &sealed, b"").unwrap_err(), Error::DecryptionFailed);
}
#[test]
fn errors_display_without_secrets() {
let e = Error::InvalidLength { input: Input::Ciphertext, expected: 1120, actual: 3 };
assert_eq!(e.to_string(), "ciphertext has the wrong length: expected 1120 bytes, got 3");
let _: &dyn std::error::Error = &e;
}
/// QA A-20: the modulus check is not over-eager. The last 32 bytes of the ML-KEM key are the
/// seed rho, where any value is legal.
#[test]
fn ml_kem_key_check_accepts_any_rho() {
let sk = SecretKey::from_seed([0x03; 32]);
let mut ok = *sk.public_key().as_bytes();
ok[1183] ^= 1;
assert!(PublicKey::from_bytes(&ok).is_ok());
}
/// QA A-21: the only stored private-key form is the 32-byte seed; an expanded ML-KEM key or an
/// X-Wing key plus anything else is refused by length.
#[test]
fn only_the_32_byte_seed_is_a_secret_key() {
for len in [64, 96, 2400, 2432] {
assert!(matches!(SecretKey::from_bytes(&vec![0u8; len]), Err(Error::InvalidLength { .. })), "{len}");
}
}
/// QA A-24: a one-byte extension and a splice (header of one box, body of another) are refused.
#[test]
fn sealed_box_extension_and_splice_are_refused() {
let mut r = rng(14);
let sk = SecretKey::generate(&mut r);
let b1 = seal::seal(sk.public_key(), b"one", b"", &mut r).unwrap();
let b2 = seal::seal(sk.public_key(), b"two", b"", &mut r).unwrap();
let mut ext = b1.clone();
ext.push(0);
assert_eq!(seal::open(&sk, &ext, b"").unwrap_err(), Error::DecryptionFailed);
let mut spliced = b1[..1121].to_vec();
spliced.extend_from_slice(&b2[1121..]);
assert_eq!(seal::open(&sk, &spliced, b"").unwrap_err(), Error::DecryptionFailed);
}
/// QA A-26: all randomness comes from the RNG parameter: the same seed gives the same box, a
/// different seed a different one.
#[test]
fn seal_randomness_comes_only_from_the_rng() {
let sk = SecretKey::from_seed([0x04; 32]);
let a = seal::seal(sk.public_key(), b"m", b"", &mut rng(1)).unwrap();
let b = seal::seal(sk.public_key(), b"m", b"", &mut rng(1)).unwrap();
let c = seal::seal(sk.public_key(), b"m", b"", &mut rng(2)).unwrap();
assert_eq!(a, b);
assert_ne!(a, c);
}