rustcrypto: add McEliece 460896f self-test, mark it available
A seeded KAT pins keygen, encapsulation and decapsulation, so a broken build fails closed instead of offering the KEM. Refs: R3-9
This commit is contained in:
parent
7ba9f6c996
commit
ec3db2efac
|
|
@ -49,11 +49,13 @@ const HASHES: [HashAlgorithm; 6] = [
|
|||
HashAlgorithm::Sha3_512,
|
||||
];
|
||||
|
||||
const KEMS: [KemAlgorithm; 4] = [
|
||||
const KEMS: &[KemAlgorithm] = &[
|
||||
KemAlgorithm::X25519,
|
||||
KemAlgorithm::MlKem768,
|
||||
KemAlgorithm::MlKem1024,
|
||||
KemAlgorithm::XWing,
|
||||
#[cfg(feature = "mceliece")]
|
||||
KemAlgorithm::ClassicMcEliece460896f,
|
||||
];
|
||||
|
||||
fn kat_passed(report: &SelfTestReport, id: u32) -> bool {
|
||||
|
|
@ -91,6 +93,11 @@ fn kdf_probe(algo: KdfAlgorithm) -> bool {
|
|||
}
|
||||
|
||||
fn kem_probe(algo: KemAlgorithm) -> bool {
|
||||
if algo == KemAlgorithm::ClassicMcEliece460896f {
|
||||
// Its KAT already ran keygen, encapsulation and decapsulation through
|
||||
// the same code; a second ~200 ms keygen here would only slow start-up.
|
||||
return true;
|
||||
}
|
||||
let Ok(kp) = kem::generate_keypair(algo) else {
|
||||
return false;
|
||||
};
|
||||
|
|
@ -138,7 +145,7 @@ pub fn probe_capabilities(
|
|||
let ok = hash::hash(algo, b"probe").is_ok() && kat_passed(self_test, algo as u32);
|
||||
caps = caps.with_hash(algo, cap(ok, true, 90));
|
||||
}
|
||||
for algo in KEMS {
|
||||
for &algo in KEMS {
|
||||
let ok = kem_probe(algo) && kat_passed(self_test, algo as u32);
|
||||
// Key generation and encapsulation are randomised.
|
||||
caps = caps.with_kem(algo, cap(ok, false, 80));
|
||||
|
|
@ -146,6 +153,7 @@ pub fn probe_capabilities(
|
|||
for algo in [
|
||||
KemAlgorithm::X448,
|
||||
KemAlgorithm::ClassicMcEliece460896,
|
||||
#[cfg(not(feature = "mceliece"))]
|
||||
KemAlgorithm::ClassicMcEliece460896f,
|
||||
] {
|
||||
caps = caps.with_kem(algo, AlgorithmCapability::unavailable());
|
||||
|
|
@ -218,6 +226,23 @@ mod tests {
|
|||
assert!(HASHES.iter().all(|a| caps.hash[a].available));
|
||||
assert!(KEMS.iter().all(|a| caps.kem[a].available));
|
||||
assert!(!caps.kem[&KemAlgorithm::X448].available);
|
||||
assert_eq!(
|
||||
caps.kem[&KemAlgorithm::ClassicMcEliece460896f].available,
|
||||
cfg!(feature = "mceliece")
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(feature = "mceliece")]
|
||||
#[test]
|
||||
fn failed_mceliece_kat_makes_it_unavailable() {
|
||||
let mut report = crate::self_test::run();
|
||||
for r in &mut report.results {
|
||||
if r.algo_id == KemAlgorithm::ClassicMcEliece460896f as u32 {
|
||||
r.passed = false;
|
||||
}
|
||||
}
|
||||
let caps = probe_capabilities(&report, None);
|
||||
assert!(!caps.kem[&KemAlgorithm::ClassicMcEliece460896f].available);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
|
|
@ -76,6 +76,56 @@ impl RngCore for OsRng {
|
|||
|
||||
impl CryptoRng for OsRng {}
|
||||
|
||||
/// Deterministic randomness for the known-answer test, never for real keys:
|
||||
/// the stream `SHA3-256(label || be64(0)) || SHA3-256(label || be64(1)) || ...`,
|
||||
/// consumed in order by key generation and then encapsulation. Simple to
|
||||
/// rebuild elsewhere, so other implementations can reproduce the vector.
|
||||
pub(crate) struct KatRng {
|
||||
label: &'static [u8],
|
||||
counter: u64,
|
||||
block: [u8; 32],
|
||||
used: usize,
|
||||
}
|
||||
|
||||
impl KatRng {
|
||||
pub(crate) fn new(label: &'static [u8]) -> Self {
|
||||
Self { label, counter: 0, block: [0; 32], used: 32 }
|
||||
}
|
||||
}
|
||||
|
||||
impl RngCore for KatRng {
|
||||
fn next_u32(&mut self) -> u32 {
|
||||
rand_core::impls::next_u32_via_fill(self)
|
||||
}
|
||||
|
||||
fn next_u64(&mut self) -> u64 {
|
||||
rand_core::impls::next_u64_via_fill(self)
|
||||
}
|
||||
|
||||
fn fill_bytes(&mut self, dest: &mut [u8]) {
|
||||
use sha3::Digest;
|
||||
for b in dest {
|
||||
if self.used == 32 {
|
||||
let mut h = sha3::Sha3_256::new();
|
||||
h.update(self.label);
|
||||
h.update(self.counter.to_be_bytes());
|
||||
self.block.copy_from_slice(&h.finalize());
|
||||
self.counter += 1;
|
||||
self.used = 0;
|
||||
}
|
||||
*b = self.block[self.used];
|
||||
self.used += 1;
|
||||
}
|
||||
}
|
||||
|
||||
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
|
||||
self.fill_bytes(dest);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl CryptoRng for KatRng {}
|
||||
|
||||
fn boxed<const N: usize>(bytes: &[u8], what: &str) -> Result<Box<[u8; N]>, CryptoError> {
|
||||
if bytes.len() != N {
|
||||
return Err(CryptoError::InvalidKey(format!(
|
||||
|
|
|
|||
|
|
@ -18,6 +18,11 @@
|
|||
//! public keys, deterministic-encapsulation ciphertexts and secrets checked
|
||||
//! against OpenSSL 3.6 (`ikme`). Large values are pinned as SHA3-256.
|
||||
//! - X-Wing: draft-connolly-cfrg-xwing-kem test vector 0.
|
||||
//! - Classic McEliece 460896f: no published vector uses an RNG we can
|
||||
//! rebuild cheaply, so the randomness is the SHA3-256 counter stream of
|
||||
//! `mceliece::KatRng` (label `ccc-kat/mceliece460896f`). The values were
|
||||
//! produced by `classic-mceliece-rust` 3.1.0 and reproduced by liboqs
|
||||
//! 0.13.0 fed the same stream (the `ccc-crypto-oqs` cross-check test).
|
||||
|
||||
use ccc_crypto_core::{
|
||||
algorithms::{AeadAlgorithm, HashAlgorithm, KdfAlgorithm, KemAlgorithm, MacAlgorithm},
|
||||
|
|
@ -135,6 +140,12 @@ const KATS: &[(u32, &str, Check)] = &[
|
|||
mlkem1024,
|
||||
),
|
||||
(KemAlgorithm::XWing as u32, "X-Wing draft vector 0", xwing),
|
||||
#[cfg(feature = "mceliece")]
|
||||
(
|
||||
KemAlgorithm::ClassicMcEliece460896f as u32,
|
||||
"Classic-McEliece-460896f seeded KAT",
|
||||
mceliece460896f,
|
||||
),
|
||||
];
|
||||
|
||||
/// Run every KAT. Never panics; failures are reported per algorithm.
|
||||
|
|
@ -502,6 +513,34 @@ fn xwing() -> Result<(), String> {
|
|||
expect("decap secret", &ss, ss_want)
|
||||
}
|
||||
|
||||
/// Keygen then encapsulation from one seeded stream; values pinned as SHA3-256.
|
||||
#[cfg(feature = "mceliece")]
|
||||
fn mceliece460896f() -> Result<(), String> {
|
||||
use crate::mceliece;
|
||||
let mut rng = mceliece::KatRng::new(b"ccc-kat/mceliece460896f");
|
||||
let kp = mceliece::generate_with(&mut rng).map_err(e)?;
|
||||
expect("public key", &sha3(&kp.public_key), MCE_PK_SHA3)?;
|
||||
expect("private key", &sha3(&kp.private_key), MCE_SK_SHA3)?;
|
||||
let enc = mceliece::encapsulate_with(&kp.public_key, &mut rng).map_err(e)?;
|
||||
expect("ciphertext", &sha3(&enc.ciphertext), MCE_CT_SHA3)?;
|
||||
expect("encap secret", &enc.shared_secret, MCE_SS)?;
|
||||
let ss = mceliece::decapsulate(&kp.private_key, &enc.ciphertext).map_err(e)?;
|
||||
expect("decap secret", &ss, MCE_SS)
|
||||
}
|
||||
|
||||
#[cfg(feature = "mceliece")]
|
||||
const MCE_PK_SHA3: &str =
|
||||
"e0e4f4faf27adcfedb35b8822a05f7938e80c62fd492f726e6ff76a549b43194";
|
||||
#[cfg(feature = "mceliece")]
|
||||
const MCE_SK_SHA3: &str =
|
||||
"59bf486dc9b6c93f6eb9854821eb237000b421c92710a2172c10570a6a014cf4";
|
||||
#[cfg(feature = "mceliece")]
|
||||
const MCE_CT_SHA3: &str =
|
||||
"a69376ae13d09fe536c544ea6049ee22f64d73761d209e3b44528f646d52a358";
|
||||
#[cfg(feature = "mceliece")]
|
||||
const MCE_SS: &str =
|
||||
"ce2f2240ab5e51e313b7a1ee3c30e2f1882e37fbba0c8f5cb337b2e01d3e4cf3";
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
|
|||
Loading…
Reference in New Issue