//! 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]); 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(&pk, 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, &ct, b"ctx"); } 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 = (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; }