rustcrypto: add KDF and KEM self-test vectors
HKDF, Argon2id (RFC 9106), X25519 and X-Wing use published vectors; ML-KEM seed keys are pinned against OpenSSL 3.6 deterministic encaps. Refs: R3-4
This commit is contained in:
parent
6a321e3efc
commit
66ec0f0685
|
|
@ -10,14 +10,21 @@
|
||||||
//! - Ascon-AEAD128: NIST SP 800-232 reference KAT (`LWC_AEAD_KAT_128_128`, count 1059).
|
//! - Ascon-AEAD128: NIST SP 800-232 reference KAT (`LWC_AEAD_KAT_128_128`, count 1059).
|
||||||
//! - SHA-2 / SHA-3: FIPS 180-4 / FIPS 202 "abc"; BLAKE2b: RFC 7693 appendix A
|
//! - SHA-2 / SHA-3: FIPS 180-4 / FIPS 202 "abc"; BLAKE2b: RFC 7693 appendix A
|
||||||
//! and the reference keyed KAT.
|
//! and the reference keyed KAT.
|
||||||
//! - HMAC: RFC 4231 test case 2.
|
//! - HMAC: RFC 4231 test case 2; HKDF: RFC 5869 test case 1 (SHA-256) and
|
||||||
|
//! the same inputs pinned for SHA-512.
|
||||||
|
//! - Argon2id: RFC 9106 §5.3 (the core with secret and associated data).
|
||||||
|
//! - X25519: RFC 7748 §6.1.
|
||||||
|
//! - ML-KEM-768/1024: seed keys from the IETF LAMPS examples (seed 00..3f);
|
||||||
|
//! 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.
|
||||||
|
|
||||||
use ccc_crypto_core::{
|
use ccc_crypto_core::{
|
||||||
algorithms::{AeadAlgorithm, HashAlgorithm, MacAlgorithm},
|
algorithms::{AeadAlgorithm, HashAlgorithm, KdfAlgorithm, KemAlgorithm, MacAlgorithm},
|
||||||
types::{AlgoTestResult, SelfTestReport},
|
types::{AlgoTestResult, SelfTestReport},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{aead, hash, mac};
|
use crate::{aead, hash, kdf, kem, mac};
|
||||||
|
|
||||||
type Check = fn() -> Result<(), String>;
|
type Check = fn() -> Result<(), String>;
|
||||||
|
|
||||||
|
|
@ -91,6 +98,38 @@ const KATS: &[(u32, &str, Check)] = &[
|
||||||
"Poly1305 RFC 8439 2.5.2",
|
"Poly1305 RFC 8439 2.5.2",
|
||||||
poly1305,
|
poly1305,
|
||||||
),
|
),
|
||||||
|
(
|
||||||
|
KdfAlgorithm::Sha256 as u32,
|
||||||
|
"HKDF-SHA-256 RFC 5869 TC1",
|
||||||
|
|| hkdf(KdfAlgorithm::Sha256, HKDF256_TC1),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
KdfAlgorithm::Sha512 as u32,
|
||||||
|
"HKDF-SHA-512 TC1 inputs",
|
||||||
|
|| hkdf(KdfAlgorithm::Sha512, HKDF512_TC1),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
KdfAlgorithm::Blake2b512 as u32,
|
||||||
|
"BLAKE2b-512 KDF",
|
||||||
|
blake2b_kdf,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
KdfAlgorithm::Argon2id as u32,
|
||||||
|
"Argon2id RFC 9106 5.3",
|
||||||
|
argon2id,
|
||||||
|
),
|
||||||
|
(KemAlgorithm::X25519 as u32, "X25519 RFC 7748 6.1", x25519),
|
||||||
|
(
|
||||||
|
KemAlgorithm::MlKem768 as u32,
|
||||||
|
"ML-KEM-768 seed KAT",
|
||||||
|
mlkem768,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
KemAlgorithm::MlKem1024 as u32,
|
||||||
|
"ML-KEM-1024 seed KAT",
|
||||||
|
mlkem1024,
|
||||||
|
),
|
||||||
|
(KemAlgorithm::XWing as u32, "X-Wing draft vector 0", xwing),
|
||||||
];
|
];
|
||||||
|
|
||||||
/// Run every KAT. Never panics; failures are reported per algorithm.
|
/// Run every KAT. Never panics; failures are reported per algorithm.
|
||||||
|
|
@ -126,6 +165,10 @@ fn expect(what: &str, got: &[u8], want_hex: &str) -> Result<(), String> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn sha3(data: &[u8]) -> Vec<u8> {
|
||||||
|
hash::hash(HashAlgorithm::Sha3_256, data).unwrap_or_default()
|
||||||
|
}
|
||||||
|
|
||||||
fn e<E: core::fmt::Display>(err: E) -> String {
|
fn e<E: core::fmt::Display>(err: E) -> String {
|
||||||
err.to_string()
|
err.to_string()
|
||||||
}
|
}
|
||||||
|
|
@ -300,6 +343,158 @@ fn poly1305() -> Result<(), String> {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ── KDF ──────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
const HKDF256_TC1: &str = "3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf\
|
||||||
|
34007208d5b887185865";
|
||||||
|
const HKDF512_TC1: &str = "832390086cda71fb47625bb5ceb168e4c8e26a1a16ed34d9fc7fe92c14815793\
|
||||||
|
38da362cb8d9f925d7cb";
|
||||||
|
|
||||||
|
fn hkdf(algo: KdfAlgorithm, want: &str) -> Result<(), String> {
|
||||||
|
let okm = kdf::derive_key(
|
||||||
|
algo,
|
||||||
|
&hx("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"),
|
||||||
|
&hx("000102030405060708090a0b0c"),
|
||||||
|
&hx("f0f1f2f3f4f5f6f7f8f9"),
|
||||||
|
42,
|
||||||
|
)
|
||||||
|
.map_err(e)?;
|
||||||
|
expect("okm", &okm, want)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn blake2b_kdf() -> Result<(), String> {
|
||||||
|
// BLAKE2b-512(ikm || salt || info) with "a", "b", "c" is BLAKE2b-512("abc").
|
||||||
|
let okm = kdf::derive_key(KdfAlgorithm::Blake2b512, b"a", b"b", b"c", 64).map_err(e)?;
|
||||||
|
expect("okm", &okm, BLAKE2B_ABC)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn argon2id() -> Result<(), String> {
|
||||||
|
use argon2::{Algorithm, Argon2, AssociatedData, ParamsBuilder, Version};
|
||||||
|
let params = ParamsBuilder::new()
|
||||||
|
.m_cost(32)
|
||||||
|
.t_cost(3)
|
||||||
|
.p_cost(4)
|
||||||
|
.data(AssociatedData::new(&[0x04; 12]).map_err(e)?)
|
||||||
|
.output_len(32)
|
||||||
|
.build()
|
||||||
|
.map_err(e)?;
|
||||||
|
let ctx = Argon2::new_with_secret(&[0x03; 8], Algorithm::Argon2id, Version::V0x13, params)
|
||||||
|
.map_err(e)?;
|
||||||
|
let mut out = [0u8; 32];
|
||||||
|
ctx.hash_password_into(&[0x01; 32], &[0x02; 16], &mut out)
|
||||||
|
.map_err(e)?;
|
||||||
|
expect(
|
||||||
|
"tag",
|
||||||
|
&out,
|
||||||
|
"0d640df58d78766c08c037a34a8b53c9d01ef0452d75b65eb52520e96b01e659",
|
||||||
|
)?;
|
||||||
|
// The provider entry point (no secret or AD) must run with small costs.
|
||||||
|
kdf::argon2id(b"password", b"somesalt", 32, 32, 1, 1).map_err(e)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── KEM ──────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
fn x25519() -> Result<(), String> {
|
||||||
|
let alice = hx("77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a");
|
||||||
|
let alice_pub = "8520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a";
|
||||||
|
let bob = hx("5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb");
|
||||||
|
let bob_pub = hx("de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f");
|
||||||
|
let shared = "4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742";
|
||||||
|
|
||||||
|
// Encapsulating with Alice's key as the ephemeral gives her public key as
|
||||||
|
// the ciphertext; Bob decapsulates it.
|
||||||
|
let enc = kem::x25519_encapsulate_deterministic(&alice, &bob_pub).map_err(e)?;
|
||||||
|
expect("ciphertext", &enc.ciphertext, alice_pub)?;
|
||||||
|
expect("encap secret", &enc.shared_secret, shared)?;
|
||||||
|
let ss = kem::decapsulate(KemAlgorithm::X25519, &bob, &enc.ciphertext).map_err(e)?;
|
||||||
|
expect("decap secret", &ss, shared)
|
||||||
|
}
|
||||||
|
|
||||||
|
struct MlKemKat {
|
||||||
|
pk_sha3: &'static str,
|
||||||
|
ct_sha3: &'static str,
|
||||||
|
ss: &'static str,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Encapsulation randomness used with OpenSSL `-pkeyopt hexikme:` for both sets.
|
||||||
|
const MLKEM_M: &str = "4041424344454647484950515253545556575859606162636465666768696a6b";
|
||||||
|
|
||||||
|
fn mlkem_kat(
|
||||||
|
algo: KemAlgorithm,
|
||||||
|
public_key: fn(&[u8]) -> Result<Vec<u8>, ccc_crypto_core::error::CryptoError>,
|
||||||
|
encap: fn(
|
||||||
|
&[u8],
|
||||||
|
&[u8; 32],
|
||||||
|
) -> Result<
|
||||||
|
ccc_crypto_core::types::KemEncapResult,
|
||||||
|
ccc_crypto_core::error::CryptoError,
|
||||||
|
>,
|
||||||
|
kat: &MlKemKat,
|
||||||
|
) -> Result<(), String> {
|
||||||
|
let seed: Vec<u8> = (0u8..64).collect();
|
||||||
|
let pk = public_key(&seed).map_err(e)?;
|
||||||
|
expect("public key", &sha3(&pk), kat.pk_sha3)?;
|
||||||
|
let mut m = [0u8; 32];
|
||||||
|
m.copy_from_slice(&hx(MLKEM_M));
|
||||||
|
let enc = encap(&pk, &m).map_err(e)?;
|
||||||
|
expect("ciphertext", &sha3(&enc.ciphertext), kat.ct_sha3)?;
|
||||||
|
expect("encap secret", &enc.shared_secret, kat.ss)?;
|
||||||
|
let ss = kem::decapsulate(algo, &seed, &enc.ciphertext).map_err(e)?;
|
||||||
|
expect("decap secret", &ss, kat.ss)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn mlkem768() -> Result<(), String> {
|
||||||
|
mlkem_kat(
|
||||||
|
KemAlgorithm::MlKem768,
|
||||||
|
kem::mlkem768::public_key,
|
||||||
|
kem::mlkem768::encapsulate_deterministic,
|
||||||
|
&MlKemKat {
|
||||||
|
pk_sha3: "a24e16d8f8f9383a95b77050f4d9fd2f5733eec1d63ef3c23ebf9918173669a7",
|
||||||
|
ct_sha3: "c5190623f119891d14904e62cd28a79677df85afe5ad9151cb962caa00002de4",
|
||||||
|
ss: "71e6c52c0066736b5d2e9cfe2d9694804a4c6dd57c9ed1d615df96c018782de3",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn mlkem1024() -> Result<(), String> {
|
||||||
|
mlkem_kat(
|
||||||
|
KemAlgorithm::MlKem1024,
|
||||||
|
kem::mlkem1024::public_key,
|
||||||
|
kem::mlkem1024::encapsulate_deterministic,
|
||||||
|
&MlKemKat {
|
||||||
|
pk_sha3: "61349e5c131a7e116a0463861d7d18663c5627c38c7147ddaadfd48acd7a4535",
|
||||||
|
ct_sha3: "6e264ea88c427017fceeebf87f12c106bb2e511843d5ed525fdd4d45a0205daa",
|
||||||
|
ss: "ccff2f30e61151de4c3257ca1d3a8a9de661b81c5ba34efe9b1a4af770caf9df",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn xwing() -> Result<(), String> {
|
||||||
|
let seed = hx("7f9c2ba4e88f827d616045507605853ed73b8093f6efbc88eb1a6eacfa66ef26");
|
||||||
|
let ss_want = "d2df0522128f09dd8e2c92b1e905c793d8f57a54c3da25861f10bf4ca613e384";
|
||||||
|
let pk = kem::xwing_public_key(&seed).map_err(e)?;
|
||||||
|
expect(
|
||||||
|
"public key",
|
||||||
|
&sha3(&pk),
|
||||||
|
"5121745904643ad9dfacca7869292c19a8a69533b53e60666b7db910b4ad6367",
|
||||||
|
)?;
|
||||||
|
let mut eseed = [0u8; 64];
|
||||||
|
eseed.copy_from_slice(&hx(
|
||||||
|
"3cb1eea988004b93103cfb0aeefd2a686e01fa4a58e8a3639ca8a1e3f9ae57e2\
|
||||||
|
35b8cc873c23dc62b8d260169afa2f75ab916a58d974918835d25e6a435085b2",
|
||||||
|
));
|
||||||
|
let enc = kem::xwing_encapsulate_deterministic(&pk, &eseed).map_err(e)?;
|
||||||
|
expect(
|
||||||
|
"ciphertext",
|
||||||
|
&sha3(&enc.ciphertext),
|
||||||
|
"c0abd149f83f45324ac3a7ddc7606c71f257e5ea86113522834a0ee1bcb34e3e",
|
||||||
|
)?;
|
||||||
|
expect("encap secret", &enc.shared_secret, ss_want)?;
|
||||||
|
let ss = kem::decapsulate(KemAlgorithm::XWing, &seed, &enc.ciphertext).map_err(e)?;
|
||||||
|
expect("decap secret", &ss, ss_want)
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue