//! 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]); let mut refused_at_parse = 0; for point in low_order_points() { let mut bad = *r.public_key().as_bytes(); bad[1184..].copy_from_slice(&point); // Non-canonical encodings (bit 255 set, or p and p + 1) are refused when parsed (QA R2-02); // the canonical low-order points parse and are refused when used. let canonical = point[31] & 0x80 == 0 && !(point[31] == 0x7f && point[0] >= 0xed); let pk = match PublicKey::from_bytes(&bad) { Ok(pk) => { assert!(canonical, "a non-canonical pk_X parsed: {}", hex::encode(point)); pk } Err(e) => { assert_eq!(e, Error::InvalidPublicKey); assert!(!canonical, "a canonical pk_X was refused: {}", hex::encode(point)); refused_at_parse += 1; continue; } }; 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)); } // 7 points with bit 255 set, plus p and p + 1. assert_eq!(refused_at_parse, 9); } /// QA R2-02 boundary: p - 1 is the largest canonical u-coordinate (and low order), p is the /// smallest non-canonical one, and an honest key is always canonical. #[test] fn the_canonical_boundary_is_exactly_p() { let r = SecretKey::from_seed([0x44; 32]); let with_x = |x: [u8; 32]| { let mut b = *r.public_key().as_bytes(); b[1184..].copy_from_slice(&x); PublicKey::from_bytes(&b) }; let mut p_minus_1 = [0xff; 32]; p_minus_1[0] = 0xec; p_minus_1[31] = 0x7f; let mut p = p_minus_1; p[0] = 0xed; let mut below = [0xff; 32]; below[31] = 0x7f; below[1] = 0xfe; // 2^255 - 1 - 2^8: below p although its first byte is 0xff assert!(with_x(p_minus_1).is_ok()); assert_eq!(with_x(p).err(), Some(Error::InvalidPublicKey)); assert!(with_x(below).is_ok()); assert!(PublicKey::from_bytes(r.public_key().as_bytes()).is_ok()); } /// 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()); }