Files
crypto/tests/integration_test.rs
T
zooqueen afe5434dc4 give the crate a workspace root, and the pqc callers the api that exists
cargo could not read the manifest at all:

  error inheriting `aes-gcm` from workspace root manifest's
  workspace.dependencies.aes-gcm: failed to find a workspace root

All 25 dependencies are declared `workspace = true`, which resolves only
against a [workspace.dependencies] table, and the crate was extracted from a
monorepo without one. The versions here are the ones Cargo.lock had already
resolved, so this records what the crate was building against and chooses
nothing new — `cargo build --locked` leaves Cargo.lock untouched.

That got the manifest parsed and the library building, which exposed three
callers written against an ml-kem/ml-dsa that predates the pinned versions:

  Encapsulate and Decapsulate are private at ml_kem's root and public at
    ml_kem::kem, and MlKem768::generate needs KemCore in scope.

  ml-dsa has no Ml_Dsa65, no VerificationKey and no SigningKey::generate.
    Key generation is the KeyGen trait (MlDsa65::key_gen), the pair hands out
    signing_key()/verifying_key(), and signing goes through signature::Signer
    and Verifier.

  ml-dsa is on rand_core 0.9 while ml-kem is on 0.6, so one thread_rng cannot
    serve both. The ml-dsa side takes OsRng from ml-dsa's own re-export, via
    unwrap_err, because OsRng is fallible in 0.9 and key_gen wants an
    infallible CryptoRng. No new dependency.

  integration_test's message list mixed [u8; 9], [u8; 27], [u8; 0] and
    [u8; 1000] in one untyped array, which has no single element type. Typed
    as [&[u8]; 4].

Both examples RUN, not merely compile: ML-KEM encapsulation round-trips,
ML-DSA signatures verify, HQC round-trips. 12 tests pass.

Worth knowing separately: src/ml_kem.rs, src/ml_dsa.rs, src/hqc.rs, src/pqc/
and src/hybrid/ are in no build target — lib.rs declares only symmetric, hash,
error and prelude. Those wrappers are dead files and would not compile as they
stand (src/ml_dsa.rs imports the same Ml_Dsa65 and VerificationKey that do not
exist). Post-quantum is reachable today only as the re-exported upstream
crates, without this crate's SecurityLevel/zeroizing layer over it.
2026-08-10 21:15:45 -07:00

146 lines
4.1 KiB
Rust

//! Integration tests for Hanzo Crypto
use hanzo_crypto::{ml_kem, ml_dsa, blake3, chacha20poly1305, aes_gcm};
use pqcrypto_traits::kem::SharedSecret;
use rand::thread_rng;
#[test]
fn test_ml_kem_integration() {
use ml_kem::kem::{Decapsulate, Encapsulate};
use ml_kem::{KemCore, MlKem768};
let mut rng = thread_rng();
let (dk, ek) = MlKem768::generate(&mut rng);
// Test multiple encapsulations
for _ in 0..10 {
let (ct, ss_sender) = ek.encapsulate(&mut rng).unwrap();
let ss_receiver = dk.decapsulate(&ct).unwrap();
assert_eq!(ss_sender, ss_receiver);
}
}
#[test]
fn test_ml_dsa_integration() {
// ml-dsa is on rand_core 0.9 where ml-kem above is on 0.6, so this test
// takes its RNG from ml-dsa's own re-export. OsRng is fallible there;
// unwrap_err gives the infallible CryptoRng key_gen asks for.
use ml_dsa::signature::rand_core::{OsRng, TryRngCore};
use ml_dsa::signature::{Signer, Verifier};
use ml_dsa::{KeyGen, MlDsa65};
let mut rng = OsRng.unwrap_err();
let keypair = MlDsa65::key_gen(&mut rng);
let signing_key = keypair.signing_key();
let verification_key = keypair.verifying_key();
// Test multiple messages
// Typed as slices: each literal is its own [u8; N], so an untyped array of
// them has no single element type and never compiled.
let messages: [&[u8]; 4] = [
b"Message 1",
b"Message 2 with more content",
b"",
&[0u8; 1000],
];
for msg in &messages {
let signature = signing_key.sign(*msg);
verification_key.verify(*msg, &signature).unwrap();
}
}
#[test]
fn test_blake3_integration() {
let data1 = b"Test data 1";
let data2 = b"Test data 2";
let hash1a = blake3::hash(data1);
let hash1b = blake3::hash(data1);
let hash2 = blake3::hash(data2);
// Same input produces same output
assert_eq!(hash1a, hash1b);
// Different input produces different output
assert_ne!(hash1a, hash2);
}
#[test]
fn test_chacha20_poly1305_integration() {
use chacha20poly1305::{
aead::{Aead, AeadCore, KeyInit, OsRng},
ChaCha20Poly1305,
};
let key = ChaCha20Poly1305::generate_key(&mut OsRng);
let cipher = ChaCha20Poly1305::new(&key);
// Test various message sizes
let messages = vec![
vec![0u8; 0],
vec![1u8; 1],
vec![2u8; 16],
vec![3u8; 1024],
vec![4u8; 65536],
];
for msg in messages {
let nonce = ChaCha20Poly1305::generate_nonce(&mut OsRng);
let ct = cipher.encrypt(&nonce, msg.as_ref()).unwrap();
let pt = cipher.decrypt(&nonce, ct.as_ref()).unwrap();
assert_eq!(msg, pt);
}
}
#[test]
fn test_aes_256_gcm_integration() {
use aes_gcm::{
aead::{Aead, AeadCore, KeyInit, OsRng},
Aes256Gcm,
};
let key = Aes256Gcm::generate_key(&mut OsRng);
let cipher = Aes256Gcm::new(&key);
// Test with associated data
let nonce = Aes256Gcm::generate_nonce(&mut OsRng);
let plaintext = b"Secret message";
let ciphertext = cipher.encrypt(&nonce, plaintext.as_ref()).unwrap();
let decrypted = cipher.decrypt(&nonce, ciphertext.as_ref()).unwrap();
assert_eq!(plaintext.as_ref(), decrypted);
// Verify tampering is detected
let mut tampered = ciphertext.clone();
tampered[0] ^= 0xFF;
assert!(cipher.decrypt(&nonce, tampered.as_ref()).is_err());
}
#[test]
fn test_hqc_all_levels() {
use hanzo_crypto::hqc::{hqc128, hqc192, hqc256};
// Test HQC-128
{
let (pk, sk) = hqc128::keypair();
let (ss1, ct) = hqc128::encapsulate(&pk);
let ss2 = hqc128::decapsulate(&ct, &sk);
assert_eq!(ss1.as_bytes(), ss2.as_bytes());
}
// Test HQC-192
{
let (pk, sk) = hqc192::keypair();
let (ss1, ct) = hqc192::encapsulate(&pk);
let ss2 = hqc192::decapsulate(&ct, &sk);
assert_eq!(ss1.as_bytes(), ss2.as_bytes());
}
// Test HQC-256
{
let (pk, sk) = hqc256::keypair();
let (ss1, ct) = hqc256::encapsulate(&pk);
let ss2 = hqc256::decapsulate(&ct, &sk);
assert_eq!(ss1.as_bytes(), ss2.as_bytes());
}
}