Skip to content

Commit

Permalink
improve debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
keks committed Aug 21, 2024
1 parent 97e6e62 commit 7f4ce60
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 20 deletions.
34 changes: 19 additions & 15 deletions hpke-rs-tests/src/test_hpke_kat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub fn kat<Crypto: HpkeCrypto + 'static>(tests: Vec<HpkeTestVector>) {
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()
Expand All @@ -75,7 +75,7 @@ pub fn kat<Crypto: HpkeCrypto + 'static>(tests: Vec<HpkeTestVector>) {
}

if Crypto::supports_aead(aead_id).is_err() {
log::trace!(
println!(
" > AEAD {:?} not implemented yet for {}",
aead_id,
Crypto::name()
Expand All @@ -84,20 +84,17 @@ pub fn kat<Crypto: HpkeCrypto + 'static>(tests: Vec<HpkeTestVector>) {
}

if Crypto::supports_kdf(kdf_id).is_err() {
log::trace!(
println!(
" > KDF {:?} not implemented yet for {}",
kdf_id,
Crypto::name()
);
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.
Expand Down Expand Up @@ -145,7 +142,7 @@ pub fn kat<Crypto: HpkeCrypto + 'static>(tests: Vec<HpkeTestVector>) {
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.
Expand All @@ -163,22 +160,28 @@ pub fn kat<Crypto: HpkeCrypto + 'static>(tests: Vec<HpkeTestVector>) {
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::<Crypto>::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");
Expand Down Expand Up @@ -221,7 +224,9 @@ pub fn kat<Crypto: HpkeCrypto + 'static>(tests: Vec<HpkeTestVector>) {

// 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
Expand Down Expand Up @@ -256,7 +261,6 @@ pub fn kat<Crypto: HpkeCrypto + 'static>(tests: Vec<HpkeTestVector>) {
}

pub fn test_kat<Crypto: HpkeCrypto + 'static>() {
let _ = pretty_env_logger::try_init();
let mut reader = TEST_JSON;
let tests: Vec<HpkeTestVector> = match serde_json::from_reader(&mut reader) {
Ok(r) => r,
Expand Down
2 changes: 1 addition & 1 deletion rust_crypto_provider/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![doc = include_str!("../Readme.md")]
#![cfg_attr(not(test), no_std)]
//#![cfg_attr(not(test), no_std)]

extern crate alloc;

Expand Down
9 changes: 6 additions & 3 deletions src/dh_kem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,15 @@ pub(super) fn auth_decaps<Crypto: HpkeCrypto>(
suite_id: &[u8],
) -> Result<Vec<u8>, 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]);

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ impl<Crypto: HpkeCrypto> Hpke<Crypto> {
psk_id: Option<&[u8]>,
pk_s: Option<&HpkePublicKey>,
) -> Result<Plaintext, HpkeError> {
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)
}

Expand Down

0 comments on commit 7f4ce60

Please sign in to comment.