//! Mandatory test 6 (v0.2.0): malformed signature input gives typed errors, never a panic. //! Wrong lengths, weak and non-canonical Ed25519 keys, contexts of 0 and 256 bytes, an over-long //! key id, random bytes. The same entry points are fuzzed by fuzz/fuzz_targets/signatures.rs. mod common; use arcanum::sign::{ self, AppContext, MAX_APP_LABEL_LEN, SIGNATURE_LEN, SIGNING_KEY_LEN, Signature, SigningKey, VERIFYING_KEY_LEN, VerifyingKey, }; use arcanum::{Error, Input, xwing}; use common::{hex, rng}; use rand_core::Rng; const LENGTHS: [usize; 14] = [0, 1, 31, 32, 33, 63, 64, 65, 1952, 1983, 1985, 3309, 3372, 3374]; #[test] fn wrong_lengths_are_typed_errors() { for len in LENGTHS { let b = vec![0u8; len]; if len != VERIFYING_KEY_LEN { assert_eq!( VerifyingKey::from_bytes(&b).unwrap_err(), Error::InvalidLength { input: Input::VerifyingKey, expected: VERIFYING_KEY_LEN, actual: len } ); } if len != SIGNATURE_LEN { assert_eq!( Signature::from_bytes(&b).unwrap_err(), Error::InvalidLength { input: Input::Signature, expected: SIGNATURE_LEN, actual: len } ); } if len != SIGNING_KEY_LEN { assert_eq!( SigningKey::from_bytes(&b).unwrap_err(), Error::InvalidLength { input: Input::SigningKey, expected: SIGNING_KEY_LEN, actual: len } ); } } assert!(SigningKey::from_bytes(&[7u8; SIGNING_KEY_LEN]).is_ok(), "any two 32-byte seeds are a key"); assert!(Signature::from_bytes(&[0u8; SIGNATURE_LEN]).is_ok(), "any 3373 bytes parse; verify decides"); } /// The small-order points of edwards25519 (the 8-torsion), canonical and non-canonical /// encodings, and encodings of y >= p. All must be refused as an Ed25519 half. const WEAK_OR_NON_CANONICAL: [&str; 10] = [ "0100000000000000000000000000000000000000000000000000000000000000", // identity "ecffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f", // order 2 "0000000000000000000000000000000000000000000000000000000000000000", // order 4 "0000000000000000000000000000000000000000000000000000000000000080", // order 4 "26e8958fc2b227b045c3f489f2ef98f0d5dfac05d3c63339b13802886d53fc05", // order 8 "26e8958fc2b227b045c3f489f2ef98f0d5dfac05d3c63339b13802886d53fc85", // order 8 "c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac037a", // order 8 "c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac03fa", // order 8 "0100000000000000000000000000000000000000000000000000000000000080", // identity, sign bit set "eeffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f", // y = p + 1 (non-canonical 1) ]; fn with_ed_half(base: &[u8; VERIFYING_KEY_LEN], ed: &[u8]) -> [u8; VERIFYING_KEY_LEN] { let mut b = *base; b[VERIFYING_KEY_LEN - 32..].copy_from_slice(ed); b } #[test] fn weak_and_non_canonical_ed25519_keys_are_refused() { let base = *SigningKey::generate(&mut rng(60)).verifying_key().as_bytes(); for h in WEAK_OR_NON_CANONICAL { let b = with_ed_half(&base, &hex(h)); assert_eq!(VerifyingKey::from_bytes(&b).unwrap_err(), Error::InvalidVerifyingKey, "{h}"); } // Every y >= p (19 values, sign bit clear and set) is a non-canonical encoding. for low in 0xedu8..=0xff { for top in [0x7f, 0xff] { let mut e = [0xffu8; 32]; e[0] = low; e[31] = top; assert_eq!(VerifyingKey::from_bytes(&with_ed_half(&base, &e)).unwrap_err(), Error::InvalidVerifyingKey); } } // An honest key with its sign bit flipped is another valid point: it parses, but cannot verify. let sk = SigningKey::generate(&mut rng(61)); let c = AppContext::new(b"t").unwrap(); let sig = sk.sign(b"m", &c, &mut rng(62)).unwrap(); let mut b = *sk.verifying_key().as_bytes(); b[VERIFYING_KEY_LEN - 1] ^= 0x80; if let Ok(vk) = VerifyingKey::from_bytes(&b) { assert_eq!(vk.verify(b"m", &c, &sig), Err(Error::InvalidSignature)); } } #[test] fn contexts_of_the_wrong_length_are_typed_errors() { let sk = SigningKey::generate(&mut rng(63)); let vk = sk.verifying_key(); assert_eq!(MAX_APP_LABEL_LEN, 255 - b"domi/arcanum/v2/sig/app/".len()); assert!(AppContext::new(&[b'x'; MAX_APP_LABEL_LEN]).is_ok()); assert_eq!(AppContext::new(&[b'x'; MAX_APP_LABEL_LEN]).unwrap().as_bytes().len(), 255); assert_eq!( AppContext::new(&[b'x'; MAX_APP_LABEL_LEN + 1]).unwrap_err(), Error::TooLong { input: Input::Context, maximum: MAX_APP_LABEL_LEN, actual: MAX_APP_LABEL_LEN + 1 } ); assert_eq!(AppContext::new(b"").unwrap_err(), Error::TooShort { input: Input::Context, minimum: 1, actual: 0 }); // QA S-09: the constructor is a `const fn`, so a bad label can fail the consumer's build. const OK: Result = AppContext::new(b"const"); const EMPTY: Result = AppContext::new(b""); assert_eq!(OK.unwrap().as_bytes(), b"domi/arcanum/v2/sig/app/const"); assert!(EMPTY.is_err()); // The raw RFC interface: 255 bytes is the most, 256 is a typed error (sign and verify). let sig = sign::sign_composite_raw(&sk, b"m", &[1; 255], &mut rng(64)).unwrap(); assert_eq!(sign::verify_composite_raw(vk, b"m", &[1; 255], &sig), Ok(())); let too_long = Error::TooLong { input: Input::Context, maximum: 255, actual: 256 }; assert_eq!(sign::sign_composite_raw(&sk, b"m", &[1; 256], &mut rng(65)).unwrap_err(), too_long); assert_eq!(sign::verify_composite_raw(vk, b"m", &[1; 256], &sig), Err(too_long)); } #[test] fn an_over_long_key_id_is_a_typed_error() { let mut r = rng(66); let sk = SigningKey::generate(&mut r); let kem = xwing::SecretKey::generate(&mut r); let id = vec![0u8; 65_536]; let err = Error::TooLong { input: Input::KeyId, maximum: 65_535, actual: 65_536 }; assert_eq!(sign::bind_kem_key(&sk, &id, kem.public_key(), (0, 1), &mut r), Err(err.clone())); let sig = sign::bind_kem_key(&sk, &id[..65_535], kem.public_key(), (0, 1), &mut r).unwrap(); assert_eq!(sign::verify_binding(sk.verifying_key(), &id[..65_535], kem.public_key(), (0, 1), 0, &sig), Ok(())); assert_eq!(sign::verify_binding(sk.verifying_key(), &id, kem.public_key(), (0, 1), 0, &sig), Err(err)); // The empty id is allowed (a key bound to "no name" is still bound to this signer). let sig = sign::bind_kem_key(&sk, b"", kem.public_key(), (0, 1), &mut r).unwrap(); assert_eq!(sign::verify_binding(sk.verifying_key(), b"", kem.public_key(), (0, 1), 1, &sig), Ok(())); } #[test] fn random_bytes_never_panic() { let mut r = rng(67); let sk = SigningKey::generate(&mut r); let c = AppContext::new(b"random").unwrap(); let good = sk.sign(b"m", &c, &mut r).unwrap(); let kem = xwing::SecretKey::generate(&mut r); for round in 0..256 { let len = [VERIFYING_KEY_LEN, SIGNATURE_LEN, SIGNING_KEY_LEN, 0, 17][round % 5]; let mut buf = vec![0u8; len]; r.fill_bytes(&mut buf); if let Ok(vk) = VerifyingKey::from_bytes(&buf) { assert!(vk.verify(b"m", &c, &good).is_err()); assert!(sign::verify_binding(&vk, b"id", kem.public_key(), (0, 9), 1, &good).is_err()); } if let Ok(sig) = Signature::from_bytes(&buf) { assert_eq!(sk.verifying_key().verify(b"m", &c, &sig), Err(Error::InvalidSignature)); let _ = sign::verify_composite_raw(sk.verifying_key(), &buf, &buf[..len.min(255)], &sig); } if let Ok(k) = SigningKey::from_bytes(&buf) { let sig = k.sign(&buf, &c, &mut r).unwrap(); assert_eq!(k.verifying_key().verify(&buf, &c, &sig), Ok(())); } } } /// QA S-05 (their `qa_s07`, inverted): an ML-DSA-65 public key with t1 = 0 is the ML-DSA analogue /// of a weak Ed25519 key. The component crate accepts it, and its ML-DSA half can be computed by /// anyone, with no secret (s1 = s2 = t0 = 0); Arcanum refuses the key at parse. #[test] #[allow(deprecated)] fn an_ml_dsa_key_with_zero_t1_is_refused() { use ml_dsa::common::array::Array; use shake::Update as _; let rho = [0x5au8; 32]; let mut ml_pk = rho.to_vec(); ml_pk.extend_from_slice(&[0u8; 1920]); // t1 = 0 let ed = ed25519_dalek::SigningKey::from_bytes(&[0x22; 32]); // a throwaway Ed25519 key let mut pk = ml_pk.clone(); pk.extend_from_slice(ed.verifying_key().as_bytes()); assert_eq!(VerifyingKey::from_bytes(&pk).unwrap_err(), Error::InvalidVerifyingKey); // Control: one non-zero t1 byte, anywhere, and the key parses. for i in [32, 1000, 1951] { let mut p = pk.clone(); p[i] = 1; assert!(VerifyingKey::from_bytes(&p).is_ok(), "t1 byte {i} set"); } // rho alone may be anything, including zero: only t1 = 0 is weak. let mut p = pk.clone(); p[..32].fill(0); p[100] = 1; assert!(VerifyingKey::from_bytes(&p).is_ok()); // Why: with the component crate alone, a secret-free key signs for this public key. The expanded // key packs eta - 0 = 4 into 4 bits (0x44 per byte) for s1 and s2, and 2^12 - 0 into 13 bits for // t0; K is public (zero) and tr is unused by sign_mu. let mut sk = rho.to_vec(); sk.extend_from_slice(&[0u8; 32 + 64]); sk.extend(std::iter::repeat_n(0x44u8, 640 + 768)); let mut t0 = vec![0u8; 2496]; for i in 0..(6 * 256) { let bit = 13 * i + 12; t0[bit / 8] |= 1 << (bit % 8); } sk.extend_from_slice(&t0); assert_eq!(sk.len(), 4032); let ml_sk = ml_dsa::ExpandedSigningKey::::from_expanded(&Array::try_from(sk.as_slice()).unwrap()); let ml_vk = ml_dsa::VerifyingKey::::decode(&Array::try_from(ml_pk.as_slice()).unwrap()); let mu = ml_vk .compute_mu( |h| { h.update(b"any message at all"); Ok(()) }, b"", ) .unwrap(); let half = ml_sk.sign_mu_randomized(&mu, &mut rng(68)).unwrap(); assert!(ml_vk.verify_mu(&mu, &half), "the component crate accepts a secret-free signature"); }