From 7f4ce600846e4cc79c286edc4a6aa4f2146e102b Mon Sep 17 00:00:00 2001 From: "Jan Winkelmann (keks)" Date: Wed, 21 Aug 2024 16:55:11 +0200 Subject: [PATCH] improve debugging --- hpke-rs-tests/src/test_hpke_kat.rs | 34 +++++++++++++++++------------- rust_crypto_provider/src/lib.rs | 2 +- src/dh_kem.rs | 9 +++++--- src/lib.rs | 2 +- 4 files changed, 27 insertions(+), 20 deletions(-) diff --git a/hpke-rs-tests/src/test_hpke_kat.rs b/hpke-rs-tests/src/test_hpke_kat.rs index 5f80cc5..44f6500 100755 --- a/hpke-rs-tests/src/test_hpke_kat.rs +++ b/hpke-rs-tests/src/test_hpke_kat.rs @@ -66,7 +66,7 @@ pub fn kat(tests: Vec) { let aead_id: AeadAlgorithm = test.aead_id.try_into().unwrap(); if Crypto::supports_kem(kem_id).is_err() { - log::trace!( + println!( " > KEM {:?} not implemented yet for {}", kem_id, Crypto::name() @@ -75,7 +75,7 @@ pub fn kat(tests: Vec) { } if Crypto::supports_aead(aead_id).is_err() { - log::trace!( + println!( " > AEAD {:?} not implemented yet for {}", aead_id, Crypto::name() @@ -84,7 +84,7 @@ pub fn kat(tests: Vec) { } if Crypto::supports_kdf(kdf_id).is_err() { - log::trace!( + println!( " > KDF {:?} not implemented yet for {}", kdf_id, Crypto::name() @@ -92,12 +92,9 @@ pub fn kat(tests: Vec) { return; } - log::trace!( + println!( "Testing mode {:?} with ciphersuite {:?}_{:?}_{:?}", - mode, - kem_id, - kdf_id, - aead_id + mode, kem_id, kdf_id, aead_id ); // Init HPKE with the given mode and ciphersuite. @@ -145,7 +142,7 @@ pub fn kat(tests: Vec) { psk.unwrap_or_default(), psk_id.unwrap_or_default(), ) - .unwrap(); + .unwrap_or_else(|err| panic!("key schedule failed with {ciphersuite_string}: {err}")); // Check setup info // Note that key and nonce are empty for exporter only key derivation. @@ -163,22 +160,28 @@ pub fn kat(tests: Vec) { assert_eq!(pk_em, my_pk_e); if let (Some(sk_sm), Some(pk_sm)) = (sk_sm, pk_sm) { let (my_sk_s, my_pk_s) = hpke.derive_key_pair(&ikm_s).unwrap().into_keys(); - assert_eq!(sk_sm, &my_sk_s); - assert_eq!(pk_sm, &my_pk_s); + assert_eq!( + sk_sm, &my_sk_s, + "derive key returned different sks for {ciphersuite_string}" + ); + assert_eq!( + pk_sm, &my_pk_s, + "derive key returned different pks for {ciphersuite_string}" + ); } // Setup KAT receiver. let kat_enc = hex_to_bytes(&test.enc); let mut receiver_context_kat = hpke .setup_receiver(&kat_enc, &sk_rm, &info, psk, psk_id, pk_sm) - .unwrap(); + .unwrap_or_else(|err| panic!("setup_receiver failed for {ciphersuite_string}: {err}")); // Setup sender and receiver with KAT randomness. // We first have to inject the randomness (ikmE). #[cfg(feature = "prng")] { - log::trace!("Testing with known ikmE ..."); + println!("Testing with known ikmE ..."); let mut hpke_sender = Hpke::::new(mode, kem_id, kdf_id, aead_id); // This only works when seeding the PRNG with ikmE. hpke_sender.seed(&ikm_e).expect("Error injecting ikm_e"); @@ -221,7 +224,9 @@ pub fn kat(tests: Vec) { // Test context API self-test let ctxt_out = sender_context.seal(&aad, &ptxt).unwrap(); - let ptxt_out = receiver_context.open(&aad, &ctxt_out).unwrap(); + let ptxt_out = receiver_context + .open(&aad, &ctxt_out) + .unwrap_or_else(|err| panic!("open failed for {ciphersuite_string}: {err}")); assert_eq!(ptxt_out, ptxt); // Test single-shot API self-test @@ -256,7 +261,6 @@ pub fn kat(tests: Vec) { } pub fn test_kat() { - let _ = pretty_env_logger::try_init(); let mut reader = TEST_JSON; let tests: Vec = match serde_json::from_reader(&mut reader) { Ok(r) => r, diff --git a/rust_crypto_provider/src/lib.rs b/rust_crypto_provider/src/lib.rs index 90c006d..76b30de 100644 --- a/rust_crypto_provider/src/lib.rs +++ b/rust_crypto_provider/src/lib.rs @@ -1,5 +1,5 @@ #![doc = include_str!("../Readme.md")] -#![cfg_attr(not(test), no_std)] +//#![cfg_attr(not(test), no_std)] extern crate alloc; diff --git a/src/dh_kem.rs b/src/dh_kem.rs index dfdaab5..00dd018 100755 --- a/src/dh_kem.rs +++ b/src/dh_kem.rs @@ -166,12 +166,15 @@ pub(super) fn auth_decaps( suite_id: &[u8], ) -> Result, Error> { let pk_e = deserialize(enc); + std::println!("alg: {alg:?}"); + std::println!("pk_e: {pk_e:?}"); + std::println!("pk_s: {pk_s:?}"); let dh_pk = concat(&[ - &Crypto::kem_derive(alg, &pk_e, sk_r)?, - &Crypto::kem_derive(alg, pk_s, sk_r)?, + &Crypto::kem_derive(alg, &pk_e, sk_r).unwrap(), + &Crypto::kem_derive(alg, pk_s, sk_r).unwrap(), ]); - let pk_rm = serialize(&Crypto::kem_derive_base(alg, sk_r)?); + let pk_rm = serialize(&Crypto::kem_derive_base(alg, sk_r).unwrap()); let pk_sm = serialize(pk_s); let kem_context = concat(&[enc, &pk_rm, &pk_sm]); diff --git a/src/lib.rs b/src/lib.rs index 6517298..83f168a 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -523,7 +523,7 @@ impl Hpke { psk_id: Option<&[u8]>, pk_s: Option<&HpkePublicKey>, ) -> Result { - let mut context = self.setup_receiver(enc, sk_r, info, psk, psk_id, pk_s)?; + let mut context = self.setup_receiver(enc, sk_r, info, psk, psk_id, pk_s).unwrap(); context.open(aad, ct) }