2a432f5d86
The mandatory tests of docs/02_signatures.md, against the API the doc sets out (arcanum::sign). They do not compile yet: the module does not exist. 1 kat_mldsa65: ACVP keyGen, sigGen (deterministic and hedged groups, byte for byte, through sign_randomized/sign_mu_randomized fed the vector's rnd), sigVer on the corrected file; Wycheproof sign_seed and verify. 2 kat_ed25519: RFC 8032 section 7.1 and Wycheproof, with verify_strict. 3 sig_composite: the RFC 10073 vectors s and sWithContext verify, the RFC key derives the RFC public key, own signatures verify under an independent rebuild of M', ML-DSA hedged with ctx = Label. 4 sig_halves: either half swapped in from another signature, zeroed or cut, or the halves swapped, is refused; the ML-DSA half fails an empty-ctx verifier. Not SUF-CMA (RFC 10073 9.2) is pinned. 5 sig_every_byte: any flipped byte of message, ctx, public key or signature fails; contexts are prefix-free; key bindings and app messages never verify as each other; every binding field matters. 6 sig_malformed: wrong lengths, weak and non-canonical Ed25519 keys, 0- and 256-byte contexts, a 65536-byte key id, random bytes. 7 sig_hygiene and feature_hygiene: compile-fail checks against a default-features consumer (hazmat raw interface, ml-dsa internals, non-crypto RNG, Clone), ZeroizeOnDrop, Debug; ml-dsa and ed25519-dalek in the normal graph with zeroize and without hazmat or defaults. The allocator probe covers the signing keys. 8 golden_v1.rs is untouched. Adds ml-dsa =0.1.2 (alloc, rand_core, zeroize) and ed25519-dalek =3.0.0 (zeroize) with default features off, base64 for the tests, and bumps the version to 0.2.0. The fuzz target follows with the code.
156 lines
7.4 KiB
Rust
156 lines
7.4 KiB
Rust
//! 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 });
|
|
|
|
// 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(()));
|
|
}
|
|
// Random labels: typed errors or a context, never a panic.
|
|
let _ = AppContext::new(&buf[..len.min(300)]);
|
|
}
|
|
}
|