Skip to content
Merged
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
2 changes: 1 addition & 1 deletion rustls-wolfcrypt-provider/src/hmac/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ impl WCHmacKey {
let ret = unsafe { wc_HmacFinal(hmac_ptr, digest.as_mut_ptr()) };
check_if_zero(ret).unwrap();

// Free the heap-allocated Hmac struct.
unsafe {
wc_HmacFree(hmac_ptr);
drop(Box::from_raw(hmac_ptr));
}

Expand Down
6 changes: 4 additions & 2 deletions rustls-wolfcrypt-provider/src/hmac/sha256hmac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ impl WCHmac256Key {
let ret = unsafe { wc_HmacFinal(hmac_ptr, digest.as_mut_ptr()) };
check_if_zero(ret).unwrap();

// Free the heap-allocated Hmac struct.
unsafe { drop(Box::from_raw(hmac_ptr)); }
unsafe {
wc_HmacFree(hmac_ptr);
drop(Box::from_raw(hmac_ptr));
}

digest
}
Expand Down
6 changes: 4 additions & 2 deletions rustls-wolfcrypt-provider/src/hmac/sha384hmac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ impl WCHmac384Key {

check_if_zero(ret).unwrap();

// Free the heap-allocated Hmac struct.
unsafe { drop(Box::from_raw(hmac_ptr)); }
unsafe {
wc_HmacFree(hmac_ptr);
drop(Box::from_raw(hmac_ptr));
}

digest
}
Expand Down
26 changes: 18 additions & 8 deletions rustls-wolfcrypt-provider/src/kx/x25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ impl KeyExchangeX25519 {
}
}

pub fn derive_shared_secret(&self, peer_pub_key: &[u8]) -> Box<[u8]> {
pub fn derive_shared_secret(&self, peer_pub_key: &[u8]) -> Result<Box<[u8]>, rustls::Error> {
if peer_pub_key.len() != 32 {
return Err(rustls::Error::General(
"Invalid peer public key length".into(),
));
}

let mut ret;
let endian: u32 = EC25519_LITTLE_ENDIAN;
let mut pub_key_provided: curve25519_key = unsafe { mem::zeroed() };
Expand All @@ -66,7 +72,11 @@ impl KeyExchangeX25519 {
// This function checks that a public key buffer holds a valid
// Curve25519 key value given the endian ordering.
ret = unsafe {
wc_curve25519_check_public(peer_pub_key.as_ptr(), 32, endian.try_into().unwrap())
wc_curve25519_check_public(
peer_pub_key.as_ptr(),
peer_pub_key.len() as word32,
endian.try_into().unwrap(),
)
};
check_if_zero(ret).unwrap();

Expand All @@ -78,7 +88,7 @@ impl KeyExchangeX25519 {
ret = unsafe {
wc_curve25519_import_public_ex(
peer_pub_key.as_ptr(),
32,
peer_pub_key.len() as word32,
&mut pub_key_provided,
endian.try_into().unwrap(),
)
Expand All @@ -93,7 +103,7 @@ impl KeyExchangeX25519 {
ret = unsafe {
wc_curve25519_import_private_ex(
self.priv_key_bytes.as_ptr(),
32,
self.priv_key_bytes.len() as word32,
private_key_object.as_ptr(),
endian.try_into().unwrap(),
)
Expand All @@ -113,7 +123,7 @@ impl KeyExchangeX25519 {
};
check_if_zero(ret).unwrap();

Box::new(out)
Ok(Box::new(out))
}
}

Expand All @@ -124,7 +134,7 @@ impl rustls::crypto::ActiveKeyExchange for KeyExchangeX25519 {
) -> Result<rustls::crypto::SharedSecret, rustls::Error> {
// We derive the shared secret with our private key and
// the received public key.
let secret = self.derive_shared_secret(peer_pub_key);
let secret = self.derive_shared_secret(peer_pub_key)?;

Ok(rustls::crypto::SharedSecret::from(&*secret))
}
Expand All @@ -149,8 +159,8 @@ mod tests {
let bob = Box::new(KeyExchangeX25519::use_curve25519());

assert_eq!(
alice.derive_shared_secret(bob.pub_key().try_into().unwrap()),
bob.derive_shared_secret(alice.pub_key().try_into().unwrap()),
alice.derive_shared_secret(bob.pub_key()).unwrap(),
bob.derive_shared_secret(alice.pub_key()).unwrap(),
)
}
}
7 changes: 3 additions & 4 deletions rustls-wolfcrypt-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,9 @@ struct Provider;

impl rustls::crypto::SecureRandom for Provider {
fn fill(&self, bytes: &mut [u8]) -> Result<(), rustls::crypto::GetRandomFailed> {
if let Err(error::WCError::Failure) = random::wolfcrypt_random_buffer_generator(bytes) {
Err(rustls::crypto::GetRandomFailed)
} else {
Ok(())
match random::wolfcrypt_random_buffer_generator(bytes) {
Ok(()) => Ok(()),
Err(_) => Err(rustls::crypto::GetRandomFailed),
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion rustls-wolfcrypt-provider/src/sign/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use alloc::format;
use alloc::sync::Arc;
use alloc::vec;
use alloc::vec::Vec;
use core::fmt;
use core::mem;
use core::ptr;
use foreign_types::ForeignType;
Expand All @@ -19,7 +20,7 @@ use zeroize::Zeroizing;
/// A unified ECDSA signing key that supports P-256, P-384, P-521.
/// Internally, we store the raw private key bytes plus
/// which scheme we should use (determined by WolfSSL after decode).
#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct EcdsaSigningKey {
/// Raw private key bytes exported from WolfSSL (`wc_ecc_export_private_only`)
/// in big-endian format.
Expand All @@ -28,6 +29,14 @@ pub struct EcdsaSigningKey {
scheme: SignatureScheme,
}

impl fmt::Debug for EcdsaSigningKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("EcdsaSigningKey")
.field("scheme", &self.scheme)
.finish_non_exhaustive()
}
}

impl TryFrom<&PrivateKeyDer<'_>> for EcdsaSigningKey {
type Error = rustls::Error;

Expand Down
21 changes: 19 additions & 2 deletions rustls-wolfcrypt-provider/src/sign/eddsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use alloc::boxed::Box;
use alloc::format;
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::fmt;
use core::mem;
use foreign_types::ForeignType;
use rustls::pki_types::PrivateKeyDer;
Expand All @@ -15,13 +16,21 @@ use zeroize::Zeroizing;

const ALL_EDDSA_SCHEMES: &[SignatureScheme] = &[SignatureScheme::ED25519];

#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct Ed25519PrivateKey {
priv_key: Arc<Zeroizing<Vec<u8>>>,
pub_key: Arc<Vec<u8>>,
algo: SignatureAlgorithm,
}

impl fmt::Debug for Ed25519PrivateKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Ed25519PrivateKey")
.field("algo", &self.algo)
.finish_non_exhaustive()
}
}

impl TryFrom<&PrivateKeyDer<'_>> for Ed25519PrivateKey {
type Error = rustls::Error;

Expand Down Expand Up @@ -112,13 +121,21 @@ impl SigningKey for Ed25519PrivateKey {
}
}

#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct Ed25519Signer {
priv_key: Arc<Zeroizing<Vec<u8>>>,
pub_key: Arc<Vec<u8>>,
scheme: SignatureScheme,
}

impl fmt::Debug for Ed25519Signer {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Ed25519Signer")
.field("scheme", &self.scheme)
.finish_non_exhaustive()
}
}

impl Signer for Ed25519Signer {
fn sign(&self, message: &[u8]) -> Result<Vec<u8>, rustls::Error> {
let mut ret;
Expand Down
Loading
Loading