From bd5d7066f0bfe7256d49f44ab72fa6d502deeebb Mon Sep 17 00:00:00 2001 From: Chaim Lukas Maier Date: Thu, 21 Aug 2025 01:49:00 +0200 Subject: [PATCH] Moved Key extraction that was only used in tests --- src/crypto/mod.rs | 7 ++++--- src/crypto/openssl.rs | 17 +++++++++-------- src/legacy.rs | 11 ++++++++++- src/lib.rs | 25 ++++++++++++++++++++++--- 4 files changed, 45 insertions(+), 15 deletions(-) diff --git a/src/crypto/mod.rs b/src/crypto/mod.rs index 5986835..e2873a5 100644 --- a/src/crypto/mod.rs +++ b/src/crypto/mod.rs @@ -6,9 +6,12 @@ use crate::error::*; use std::any::Any; pub(crate) mod holder; -#[cfg(feature = "backend-openssl")] +#[cfg(all(not(test), feature = "backend-openssl"))] mod openssl; +#[cfg(all(test, feature = "backend-openssl"))] +pub mod openssl; + #[cfg(not(feature = "backend-openssl"))] pub use holder::{set_boxed_cryptographer, set_cryptographer}; @@ -24,8 +27,6 @@ pub trait LocalKeyPair: Send + Sync + 'static { /// Export the public key component in the /// binary uncompressed point representation. fn pub_as_raw(&self) -> Result>; - /// Export the raw components of the keypair. - fn raw_components(&self) -> Result; /// For downcasting purposes. fn as_any(&self) -> &dyn Any; } diff --git a/src/crypto/openssl.rs b/src/crypto/openssl.rs index 5d7cc0a..16086a3 100644 --- a/src/crypto/openssl.rs +++ b/src/crypto/openssl.rs @@ -94,6 +94,15 @@ impl OpenSSLLocalKeyPair { ec_key: private_key, }) } + + #[cfg(test)] + pub fn raw_components(&self) -> Result { + let private_key = self.ec_key.private_key(); + Ok(EcKeyComponents::new( + private_key.to_vec(), + self.pub_as_raw()?, + )) + } } impl LocalKeyPair for OpenSSLLocalKeyPair { @@ -107,14 +116,6 @@ impl LocalKeyPair for OpenSSLLocalKeyPair { Ok(uncompressed) } - fn raw_components(&self) -> Result { - let private_key = self.ec_key.private_key(); - Ok(EcKeyComponents::new( - private_key.to_vec(), - self.pub_as_raw()?, - )) - } - fn as_any(&self) -> &dyn Any { self } diff --git a/src/legacy.rs b/src/legacy.rs index 3fd96da..f975aa3 100644 --- a/src/legacy.rs +++ b/src/legacy.rs @@ -50,6 +50,7 @@ pub fn decrypt_aesgcm( #[cfg(all(test, feature = "backend-openssl"))] mod aesgcm_tests { use super::*; + use crate::crypto::openssl::OpenSSLLocalKeyPair; use base64::Engine; use hex; @@ -152,7 +153,15 @@ mod aesgcm_tests { let (local_key, auth) = crate::generate_keypair_and_auth_secret()?; let plaintext = b"There was a little ship that had never sailed"; let encoded = encrypt_aesgcm(&local_key.pub_as_raw()?, &auth, plaintext).unwrap(); - let decoded = decrypt_aesgcm(&local_key.raw_components()?, &auth, &encoded)?; + let decoded = decrypt_aesgcm( + &local_key + .as_any() + .downcast_ref::() + .unwrap() + .raw_components()?, + &auth, + &encoded, + )?; assert_eq!(decoded, plaintext.to_vec()); Ok(()) } diff --git a/src/lib.rs b/src/lib.rs index 00955a2..d8ec27e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -77,6 +77,7 @@ fn generate_keys() -> Result<(Box, Box)> { #[cfg(all(test, feature = "backend-openssl"))] mod aes128gcm_tests { use super::common::ECE_TAG_LENGTH; + use super::crypto::openssl::OpenSSLLocalKeyPair; use super::*; #[allow(clippy::too_many_arguments)] @@ -145,7 +146,12 @@ mod aes128gcm_tests { let ciphertext = encrypt(&remote_key.pub_as_raw().unwrap(), &auth_secret, plaintext).unwrap(); let decrypted = decrypt( - &remote_key.raw_components().unwrap(), + &remote_key + .as_any() + .downcast_ref::() + .unwrap() + .raw_components() + .unwrap(), &auth_secret, &ciphertext, ) @@ -160,7 +166,12 @@ mod aes128gcm_tests { let ciphertext = encrypt(&remote_key.pub_as_raw().unwrap(), &auth_secret, &plaintext).unwrap(); let decrypted = decrypt( - &remote_key.raw_components().unwrap(), + &remote_key + .as_any() + .downcast_ref::() + .unwrap() + .raw_components() + .unwrap(), &auth_secret, &ciphertext, ) @@ -212,7 +223,15 @@ mod aes128gcm_tests { let (local_key, auth) = generate_keypair_and_auth_secret()?; let plaintext = b"Mary had a little lamb, with some nice mint jelly"; let encoded = encrypt(&local_key.pub_as_raw()?, &auth, plaintext).unwrap(); - let decoded = decrypt(&local_key.raw_components()?, &auth, &encoded)?; + let decoded = decrypt( + &local_key + .as_any() + .downcast_ref::() + .unwrap() + .raw_components()?, + &auth, + &encoded, + )?; assert_eq!(decoded, plaintext.to_vec()); Ok(()) }