311 lines
11 KiB
Rust
311 lines
11 KiB
Rust
//! Embedded known-answer self-test (KAT) for every algorithm the provider
|
|
//! offers. An algorithm without a passing KAT is never marked available.
|
|
//!
|
|
//! Sources (hex vectors shared with the RustCrypto and wolfSSL self-tests):
|
|
//!
|
|
//! - AES-256-GCM: NIST SP 800-38D test case 16.
|
|
//! - AES-256-GCM-SIV: RFC 8452 appendix C.2.
|
|
//! - ChaCha20-Poly1305: RFC 8439 §2.8.2.
|
|
//! - SHA-2 / SHA-3: FIPS 180-4 / FIPS 202 "abc".
|
|
//! - HMAC: RFC 4231 test case 2.
|
|
//! - HKDF: RFC 5869 test cases 1 and 3 (SHA-256), and the case 1 inputs for
|
|
//! SHA-384/512, checked against OpenSSL 3.6 and the other providers.
|
|
|
|
use ccc_crypto_core::{
|
|
algorithms::{AeadAlgorithm, HashAlgorithm, KdfAlgorithm, MacAlgorithm},
|
|
types::{AlgoTestResult, SelfTestReport},
|
|
};
|
|
|
|
use crate::{aead, hash, kdf, mac, PROVIDER_NAME};
|
|
|
|
type Check = fn() -> Result<(), String>;
|
|
|
|
/// Every KAT: (algorithm ID, test name, check).
|
|
const KATS: &[(u32, &str, Check)] = &[
|
|
(
|
|
AeadAlgorithm::AesGcm256 as u32,
|
|
"AES-256-GCM SP 800-38D TC16",
|
|
aes_gcm,
|
|
),
|
|
(
|
|
AeadAlgorithm::AesGcmSiv256 as u32,
|
|
"AES-256-GCM-SIV RFC 8452 C.2",
|
|
aes_gcm_siv,
|
|
),
|
|
(
|
|
AeadAlgorithm::ChaCha20Poly1305 as u32,
|
|
"ChaCha20-Poly1305 RFC 8439 2.8.2",
|
|
chacha,
|
|
),
|
|
(HashAlgorithm::Sha256 as u32, "SHA-256 abc", || {
|
|
digest(HashAlgorithm::Sha256, SHA256_ABC)
|
|
}),
|
|
(HashAlgorithm::Sha384 as u32, "SHA-384 abc", || {
|
|
digest(HashAlgorithm::Sha384, SHA384_ABC)
|
|
}),
|
|
(HashAlgorithm::Sha512 as u32, "SHA-512 abc", || {
|
|
digest(HashAlgorithm::Sha512, SHA512_ABC)
|
|
}),
|
|
(HashAlgorithm::Sha3_256 as u32, "SHA3-256 abc", || {
|
|
digest(HashAlgorithm::Sha3_256, SHA3_256_ABC)
|
|
}),
|
|
(HashAlgorithm::Sha3_512 as u32, "SHA3-512 abc", || {
|
|
digest(HashAlgorithm::Sha3_512, SHA3_512_ABC)
|
|
}),
|
|
(
|
|
MacAlgorithm::HmacSha256 as u32,
|
|
"HMAC-SHA-256 RFC 4231 TC2",
|
|
|| hmac(MacAlgorithm::HmacSha256, HMAC256_TC2),
|
|
),
|
|
(
|
|
MacAlgorithm::HmacSha384 as u32,
|
|
"HMAC-SHA-384 RFC 4231 TC2",
|
|
|| hmac(MacAlgorithm::HmacSha384, HMAC384_TC2),
|
|
),
|
|
(
|
|
MacAlgorithm::HmacSha512 as u32,
|
|
"HMAC-SHA-512 RFC 4231 TC2",
|
|
|| hmac(MacAlgorithm::HmacSha512, HMAC512_TC2),
|
|
),
|
|
(
|
|
KdfAlgorithm::Sha256 as u32,
|
|
"HKDF-SHA-256 RFC 5869 TC1",
|
|
|| hkdf(KdfAlgorithm::Sha256, HKDF256_TC1),
|
|
),
|
|
(
|
|
KdfAlgorithm::Sha256 as u32,
|
|
"HKDF-SHA-256 RFC 5869 TC3 (empty salt)",
|
|
hkdf_empty_salt,
|
|
),
|
|
(
|
|
KdfAlgorithm::Sha384 as u32,
|
|
"HKDF-SHA-384 TC1 inputs",
|
|
|| hkdf(KdfAlgorithm::Sha384, HKDF384_TC1),
|
|
),
|
|
(
|
|
KdfAlgorithm::Sha512 as u32,
|
|
"HKDF-SHA-512 TC1 inputs",
|
|
|| hkdf(KdfAlgorithm::Sha512, HKDF512_TC1),
|
|
),
|
|
];
|
|
|
|
/// Run every KAT. Never panics; failures are reported per algorithm.
|
|
pub fn run() -> SelfTestReport {
|
|
let results = KATS
|
|
.iter()
|
|
.map(|(id, name, check)| {
|
|
let outcome = check();
|
|
AlgoTestResult {
|
|
algo_id: *id,
|
|
algo_name: (*name).into(),
|
|
passed: outcome.is_ok(),
|
|
error_message: outcome.err(),
|
|
}
|
|
})
|
|
.collect();
|
|
SelfTestReport::finalise(PROVIDER_NAME, results)
|
|
}
|
|
|
|
// ── Helpers ──────────────────────────────────────────────────────────────────
|
|
|
|
fn hx(s: &str) -> Vec<u8> {
|
|
(0..s.len() / 2)
|
|
.map(|i| u8::from_str_radix(&s[2 * i..2 * i + 2], 16).unwrap_or(0))
|
|
.collect()
|
|
}
|
|
|
|
fn expect(what: &str, got: &[u8], want_hex: &str) -> Result<(), String> {
|
|
if got == hx(want_hex).as_slice() {
|
|
Ok(())
|
|
} else {
|
|
Err(format!("{what}: output mismatch"))
|
|
}
|
|
}
|
|
|
|
fn e<E: core::fmt::Display>(err: E) -> String {
|
|
err.to_string()
|
|
}
|
|
|
|
// ── AEAD ─────────────────────────────────────────────────────────────────────
|
|
|
|
fn aead_kat(
|
|
algo: AeadAlgorithm,
|
|
key: &str,
|
|
nonce: &str,
|
|
aad: &str,
|
|
pt: &str,
|
|
ct: &str,
|
|
) -> Result<(), String> {
|
|
let (key, nonce, aad, pt) = (hx(key), hx(nonce), hx(aad), hx(pt));
|
|
let sealed = aead::encrypt(algo, &key, &nonce, &pt, &aad).map_err(e)?;
|
|
expect("seal", &sealed, ct)?;
|
|
let opened = aead::decrypt(algo, &key, &nonce, &sealed, &aad).map_err(e)?;
|
|
if opened != pt {
|
|
return Err("open: plaintext mismatch".into());
|
|
}
|
|
let mut bad = sealed;
|
|
let last = bad.len() - 1;
|
|
bad[last] ^= 0x80;
|
|
if aead::decrypt(algo, &key, &nonce, &bad, &aad).is_ok() {
|
|
return Err("open accepted a tampered tag".into());
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn aes_gcm() -> Result<(), String> {
|
|
aead_kat(
|
|
AeadAlgorithm::AesGcm256,
|
|
"feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
|
|
"cafebabefacedbaddecaf888",
|
|
"feedfacedeadbeeffeedfacedeadbeefabaddad2",
|
|
"d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a72\
|
|
1c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
|
|
"522dc1f099567d07f47f37a32a84427d643a8cdcbfe5c0c97598a2bd2555d1aa\
|
|
8cb08e48590dbb3da7b08b1056828838c5f61e6393ba7a0abcc9f662\
|
|
76fc6ece0f4e1768cddf8853bb2d551b",
|
|
)
|
|
}
|
|
|
|
fn aes_gcm_siv() -> Result<(), String> {
|
|
let key = "0100000000000000000000000000000000000000000000000000000000000000";
|
|
let nonce = "030000000000000000000000";
|
|
aead_kat(
|
|
AeadAlgorithm::AesGcmSiv256,
|
|
key,
|
|
nonce,
|
|
"",
|
|
"",
|
|
"07f5f4169bbf55a8400cd47ea6fd400f",
|
|
)?;
|
|
aead_kat(
|
|
AeadAlgorithm::AesGcmSiv256,
|
|
key,
|
|
nonce,
|
|
"",
|
|
"0100000000000000",
|
|
"c2ef328e5c71c83b843122130f7364b761e0b97427e3df28",
|
|
)
|
|
}
|
|
|
|
fn chacha() -> Result<(), String> {
|
|
aead_kat(
|
|
AeadAlgorithm::ChaCha20Poly1305,
|
|
"808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f",
|
|
"070000004041424344454647",
|
|
"50515253c0c1c2c3c4c5c6c7",
|
|
"4c616469657320616e642047656e746c656d656e206f662074686520636c6173\
|
|
73206f66202739393a204966204920636f756c64206f6666657220796f75206f6e6c79206f6e6520746970\
|
|
20666f7220746865206675747572652c2073756e73637265656e20776f756c642062652069742e",
|
|
"d31a8d34648e60db7b86afbc53ef7ec2a4aded51296e08fea9e2b5a736ee62d63dbea45e8ca967128\
|
|
2fafb69da92728b1a71de0a9e060b2905d6a5b67ecd3b3692ddbd7f2d778b8c9803aee328091b58fa\
|
|
b324e4fad675945585808b4831d7bc3ff4def08e4b7a9de576d26586cec64b6116\
|
|
1ae10b594f09e26a7e902ecbd0600691",
|
|
)
|
|
}
|
|
|
|
// ── Hash ─────────────────────────────────────────────────────────────────────
|
|
|
|
const SHA256_ABC: &str = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad";
|
|
const SHA384_ABC: &str = "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed\
|
|
8086072ba1e7cc2358baeca134c825a7";
|
|
const SHA512_ABC: &str = "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a\
|
|
2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f";
|
|
const SHA3_256_ABC: &str = "3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532";
|
|
const SHA3_512_ABC: &str = "b751850b1a57168a5693cd924b6b096e08f621827444f70d884f5d0240d2712e\
|
|
10e116e9192af3c91a7ec57647e3934057340b4cf408d5a56592f8274eec53f0";
|
|
|
|
fn digest(algo: HashAlgorithm, want: &str) -> Result<(), String> {
|
|
expect("digest", &hash::hash(algo, b"abc").map_err(e)?, want)
|
|
}
|
|
|
|
// ── MAC ──────────────────────────────────────────────────────────────────────
|
|
|
|
const HMAC256_TC2: &str = "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843";
|
|
const HMAC384_TC2: &str = "af45d2e376484031617f78d2b58a6b1b9c7ef464f5a01b47e42ec3736322445e\
|
|
8e2240ca5e69e2c78b3239ecfab21649";
|
|
const HMAC512_TC2: &str = "164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea250554\
|
|
9758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737";
|
|
|
|
fn hmac(algo: MacAlgorithm, want: &str) -> Result<(), String> {
|
|
let (key, data) = (b"Jefe", b"what do ya want for nothing?");
|
|
let tag = mac::compute_mac(algo, key, data).map_err(e)?;
|
|
expect("tag", &tag, want)?;
|
|
if !mac::verify_mac(algo, key, data, &tag).map_err(e)? {
|
|
return Err("verify rejected a valid tag".into());
|
|
}
|
|
let mut bad = tag;
|
|
bad[0] ^= 1;
|
|
if mac::verify_mac(algo, key, data, &bad).map_err(e)? {
|
|
return Err("verify accepted a tampered tag".into());
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
// ── KDF ──────────────────────────────────────────────────────────────────────
|
|
|
|
const HKDF256_TC1: &str = "3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf\
|
|
34007208d5b887185865";
|
|
const HKDF384_TC1: &str = "9b5097a86038b805309076a44b3a9f38063e25b516dcbf369f394cfab43685f7\
|
|
48b6457763e4f0204fc5";
|
|
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)
|
|
}
|
|
|
|
/// The empty-salt rule the cascade and HPKE schedules rely on.
|
|
fn hkdf_empty_salt() -> Result<(), String> {
|
|
let okm = kdf::derive_key(
|
|
KdfAlgorithm::Sha256,
|
|
&hx("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"),
|
|
b"",
|
|
b"",
|
|
42,
|
|
)
|
|
.map_err(e)?;
|
|
expect(
|
|
"okm",
|
|
&okm,
|
|
"8da4e775a563c18f715f802a063c5a31b8a11f5c5ee1879ec3454e5f3c738d2d\
|
|
9d201395faa4b61a96c8",
|
|
)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn all_kats_pass() {
|
|
let report = run();
|
|
for r in &report.results {
|
|
assert!(r.passed, "{}: {:?}", r.algo_name, r.error_message);
|
|
}
|
|
assert!(report.all_passed);
|
|
assert_eq!(report.provider_name, "awslc");
|
|
}
|
|
|
|
#[test]
|
|
fn kat_detects_a_wrong_answer() {
|
|
assert!(expect("x", b"abc", "616264").is_err());
|
|
assert!(expect("x", b"abc", "616263").is_ok());
|
|
}
|
|
|
|
#[test]
|
|
fn every_offered_algorithm_has_a_kat() {
|
|
let ids: Vec<u32> = KATS.iter().map(|k| k.0).collect();
|
|
for id in [12u32, 13, 16, 1, 2, 3, 30, 31, 32, 40, 41, 42, 44, 45] {
|
|
assert!(ids.contains(&id), "no KAT for {id}");
|
|
}
|
|
}
|
|
}
|