lum_ccc_rust/crates/ccc-crypto-wolfssl/src/self_test.rs

114 lines
4.9 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! Embedded NIST / RFC test vectors for the power-on self-test.
//!
//! These are checked at runtime by [`WolfSslProvider::self_test()`]; failures
//! gate the provider from being marked `available` in the capability catalog.
use ccc_crypto_core::{
algorithms::AeadAlgorithm,
types::{AlgoTestResult, SelfTestReport},
};
use crate::aead;
// ──────────────────────────────────────────────────────────────────────────────
// AEAD vectors
// ──────────────────────────────────────────────────────────────────────────────
struct AeadVector {
algo: AeadAlgorithm,
key: &'static str,
nonce: &'static str,
aad: &'static str,
pt: &'static str,
ct_tag: &'static str, // ciphertext || tag, hex-encoded
}
/// NIST SP 800-38D and RFC 8439 test vectors.
static AEAD_VECTORS: &[AeadVector] = &[
// ── AES-256-GCM: NIST SP 800-38D Test Case 16 ───────────────────────
// Key: feffe9...308308 × 2 (AES-256)
// Nonce: cafebabefacedbaddecaf888
// AAD: feedfacedeadbeeffeedfacedeadbeefabaddad2 (20 bytes)
// PT: d9313225...637b39 (60 bytes)
// ct_tag = ciphertext (60 bytes) || GHASH tag (16 bytes) = 76 bytes
AeadVector {
algo: AeadAlgorithm::AesGcm256,
key: "feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308",
nonce: "cafebabefacedbaddecaf888",
aad: "feedfacedeadbeeffeedfacedeadbeefabaddad2",
pt: "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a72\
1c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39",
ct_tag: "522dc1f099567d07f47f37a32a84427d643a8cdcbfe5c0c97598a2bd2555d1aa\
8cb08e48590dbb3da7b08b1056828838c5f61e6393ba7a0abcc9f662\
76fc6ece0f4e1768cddf8853bb2d551b",
},
// ── ChaCha20-Poly1305: RFC 8439 §2.8.2 ──────────────────────────────
// PT: "Ladies and Gentlemen of the class of '99" (40 bytes)
// ct_tag = ciphertext (40 bytes) || Poly1305 tag (16 bytes) = 56 bytes
AeadVector {
algo: AeadAlgorithm::ChaCha20Poly1305,
key: "808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f",
nonce: "070000004041424344454647",
aad: "50515253c0c1c2c3c4c5c6c7",
pt: "4c616469657320616e642047656e746c656d656e206f662074686520636c617373\
206f6620273939",
ct_tag: "d31a8d34648e60db7b86afbc53ef7ec2a4aded51296e08fea9e2b5a736ee62d6\
3dbea45e8ca96712f180d4e9016c65a7dde15e3106075ebd",
},
];
// ──────────────────────────────────────────────────────────────────────────────
// Runner
// ──────────────────────────────────────────────────────────────────────────────
/// Run all embedded self-test vectors and return a [`SelfTestReport`].
pub(crate) fn run() -> SelfTestReport {
let mut results: Vec<AlgoTestResult> = Vec::new();
for v in AEAD_VECTORS {
results.push(run_aead_vector(v));
}
SelfTestReport::finalise("wolfssl", results)
}
fn run_aead_vector(v: &AeadVector) -> AlgoTestResult {
let test_name = format!("{} NIST/RFC vector", v.algo.name());
let decode = |hex: &str| -> Vec<u8> {
(0..hex.len())
.step_by(2)
.map(|i| u8::from_str_radix(&hex[i..i + 2], 16).unwrap_or(0))
.collect()
};
let key = decode(v.key);
let nonce = decode(v.nonce);
let aad = decode(v.aad);
let pt = decode(v.pt);
let expected = decode(v.ct_tag);
match aead::encrypt(v.algo, &key, &nonce, &pt, &aad) {
Ok(ct_tag) if ct_tag == expected => AlgoTestResult {
algo_id: v.algo as u32,
algo_name: test_name,
passed: true,
error_message: None,
},
Ok(ct_tag) => AlgoTestResult {
algo_id: v.algo as u32,
algo_name: test_name.clone(),
passed: false,
error_message: Some(format!(
"output mismatch: got {} bytes, expected {} bytes",
ct_tag.len(),
expected.len()
)),
},
Err(e) => AlgoTestResult {
algo_id: v.algo as u32,
algo_name: test_name,
passed: false,
error_message: Some(e.to_string()),
},
}
}