Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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<Vec<u8>>;
/// Export the raw components of the keypair.
fn raw_components(&self) -> Result<EcKeyComponents>;
/// For downcasting purposes.
fn as_any(&self) -> &dyn Any;
}
Expand Down
17 changes: 9 additions & 8 deletions src/crypto/openssl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ impl OpenSSLLocalKeyPair {
ec_key: private_key,
})
}

#[cfg(test)]
pub fn raw_components(&self) -> Result<EcKeyComponents> {
let private_key = self.ec_key.private_key();
Ok(EcKeyComponents::new(
private_key.to_vec(),
self.pub_as_raw()?,
))
}
}

impl LocalKeyPair for OpenSSLLocalKeyPair {
Expand All @@ -107,14 +116,6 @@ impl LocalKeyPair for OpenSSLLocalKeyPair {
Ok(uncompressed)
}

fn raw_components(&self) -> Result<EcKeyComponents> {
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
}
Expand Down
11 changes: 10 additions & 1 deletion src/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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::<OpenSSLLocalKeyPair>()
.unwrap()
.raw_components()?,
&auth,
&encoded,
)?;
assert_eq!(decoded, plaintext.to_vec());
Ok(())
}
Expand Down
25 changes: 22 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ fn generate_keys() -> Result<(Box<dyn LocalKeyPair>, Box<dyn LocalKeyPair>)> {
#[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)]
Expand Down Expand Up @@ -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::<OpenSSLLocalKeyPair>()
.unwrap()
.raw_components()
.unwrap(),
&auth_secret,
&ciphertext,
)
Expand All @@ -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::<OpenSSLLocalKeyPair>()
.unwrap()
.raw_components()
.unwrap(),
&auth_secret,
&ciphertext,
)
Expand Down Expand Up @@ -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::<OpenSSLLocalKeyPair>()
.unwrap()
.raw_components()?,
&auth,
&encoded,
)?;
assert_eq!(decoded, plaintext.to_vec());
Ok(())
}
Expand Down