//! 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`.) use arcanum::Error; use arcanum::exchange; use arcanum::xwing::{Ciphertext, PublicKey, SecretKey}; /// 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. const LOW_ORDER: [&str; 7] = [ "0000000000000000000000000000000000000000000000000000000000000000", "0100000000000000000000000000000000000000000000000000000000000000", "e0eb7a7c3b41b8ae1656e3faf19fc46ada098deb9c32b1fd866205165f49b800", "5f9c95bca3508c24b1d0b1559c83ef5b04445cc4581c8e86d8224eddd09f1157", "ecffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f", "edffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f", "eeffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f", ]; fn low_order_points() -> impl Iterator { LOW_ORDER.iter().map(|h| hex::decode(h).unwrap().try_into().unwrap()) } #[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(); for point in low_order_points() { let mut bad = *ct.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)); } } #[test] fn low_order_x25519_public_key_is_refused_by_the_initiator() { let sk = SecretKey::from_seed([0x44; 32]); for point in low_order_points() { let mut bad = *sk.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)); } } /// The honest exchange is not affected by the check: the same keys with honest points still agree. #[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()); }