diff --git a/src/asymmetric_crypto/curve25519.rs b/src/asymmetric_crypto/curve25519.rs index f3efb4a..44a6cd6 100644 --- a/src/asymmetric_crypto/curve25519.rs +++ b/src/asymmetric_crypto/curve25519.rs @@ -34,14 +34,6 @@ pub const X25519_PK_LENGTH: usize = 32; pub struct X25519PrivateKey(Scalar); impl X25519PrivateKey { - /// Generate a new private key. - #[must_use] - pub fn new(rng: &mut R) -> Self { - let mut bytes = [0; 64]; - rng.fill_bytes(&mut bytes); - Self(Scalar::from_bytes_mod_order_wide(&bytes)) - } - /// Convert to bytes without copy. #[inline] #[must_use] @@ -58,6 +50,14 @@ impl X25519PrivateKey { } impl KeyTrait for X25519PrivateKey { + /// Generate a new random key. + #[inline] + fn new(rng: &mut R) -> Self { + let mut bytes = [0; 64]; + rng.fill_bytes(&mut bytes); + Self(Scalar::from_bytes_mod_order_wide(&bytes)) + } + /// Converts the given key into bytes. #[inline] fn to_bytes(&self) -> [u8; Self::LENGTH] { @@ -228,18 +228,15 @@ impl ZeroizeOnDrop for X25519PrivateKey {} #[serde(try_from = "&[u8]", into = "[u8; 32]")] pub struct X25519PublicKey(RistrettoPoint); -impl X25519PublicKey { +impl KeyTrait for X25519PublicKey { /// Generate a new random public key. #[inline] - #[must_use] - pub fn new(rng: &mut R) -> Self { + fn new(rng: &mut R) -> Self { let mut uniform_bytes = [0u8; 64]; rng.fill_bytes(&mut uniform_bytes); Self(RistrettoPoint::from_uniform_bytes(&uniform_bytes)) } -} -impl KeyTrait for X25519PublicKey { /// Converts the given public key into an array of bytes. #[inline] fn to_bytes(&self) -> [u8; Self::LENGTH] { diff --git a/src/asymmetric_crypto/mod.rs b/src/asymmetric_crypto/mod.rs index b6195f5..a37c834 100644 --- a/src/asymmetric_crypto/mod.rs +++ b/src/asymmetric_crypto/mod.rs @@ -1,7 +1,7 @@ use crate::KeyTrait; use core::{ fmt::Debug, - ops::{Add, Mul}, + ops::{Add, Mul, Sub}, }; use rand_core::{CryptoRng, RngCore}; use zeroize::{Zeroize, ZeroizeOnDrop}; @@ -11,9 +11,21 @@ pub mod curve25519; pub trait DhKeyPair<'a, const PK_LENGTH: usize, const SK_LENGTH: usize>: Debug + PartialEq + Eq + Send + Sync + Sized + Clone + Zeroize + ZeroizeOnDrop where - Self::PublicKey: Add + Mul<&'a Self::PrivateKey, Output = Self::PublicKey>, - Self::PrivateKey: 'a + Add, + Self::PublicKey: + Add + Mul<&'a Self::PrivateKey, Output = Self::PublicKey> + From<&'a Self::PrivateKey>, + Self::PrivateKey: 'a, + &'a Self::PrivateKey: Add + + Sub + + Mul, { + /// This is needed to be able to use `{ MyKeyPair::PK_LENGTH }` + /// as associated constant + const PK_LENGTH: usize = PK_LENGTH; + + /// This is needed to be able to use `{ MyKeyPair::SK_LENGTH }` + /// as associated constant + const SK_LENGTH: usize = SK_LENGTH; + /// Public key type PublicKey: KeyTrait; diff --git a/src/lib.rs b/src/lib.rs index 8e682aa..0ea39b0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,6 +18,7 @@ pub mod entropy; pub mod kdf; pub mod symmetric_crypto; +use rand_core::{RngCore, CryptoRng}; use zeroize::{Zeroize, ZeroizeOnDrop}; pub use crate::error::CryptoCoreError; @@ -34,6 +35,10 @@ pub trait KeyTrait: /// Key length const LENGTH: usize = LENGTH; + /// Generate a new random key. + #[must_use] + fn new(rng: &mut R) -> Self; + /// Convert the given key into a vector of bytes. #[must_use] fn to_bytes(&self) -> [u8; LENGTH]; diff --git a/src/symmetric_crypto/aes_256_gcm_pure/mod.rs b/src/symmetric_crypto/aes_256_gcm_pure/mod.rs index fc7aed6..61f2d27 100644 --- a/src/symmetric_crypto/aes_256_gcm_pure/mod.rs +++ b/src/symmetric_crypto/aes_256_gcm_pure/mod.rs @@ -186,6 +186,7 @@ pub fn decrypt_in_place_detached( mod tests { use crate::{ + KeyTrait, entropy::CsRng, symmetric_crypto::{ aes_256_gcm_pure::{ diff --git a/src/symmetric_crypto/key.rs b/src/symmetric_crypto/key.rs index 0be7c83..8fa29d5 100644 --- a/src/symmetric_crypto/key.rs +++ b/src/symmetric_crypto/key.rs @@ -11,16 +11,15 @@ use zeroize::{Zeroize, ZeroizeOnDrop}; #[derive(Debug, Hash, Clone, PartialEq, Eq)] pub struct Key([u8; LENGTH]); -impl Key { +impl KeyTrait for Key { /// Generate a new symmetric random `Key` - pub fn new(rng: &mut R) -> Self { - let mut key = [0; KEY_LENGTH]; + #[inline] + fn new(rng: &mut R) -> Self { + let mut key = [0; LENGTH]; rng.fill_bytes(&mut key); Self(key) } -} -impl KeyTrait for Key { /// Convert the given key into bytes. #[inline] fn to_bytes(&self) -> [u8; LENGTH] { @@ -107,7 +106,7 @@ impl Deref for Key { #[cfg(test)] mod tests { - use crate::{entropy::CsRng, symmetric_crypto::key::Key}; + use crate::{KeyTrait, entropy::CsRng, symmetric_crypto::key::Key}; use core::ops::Deref; const KEY_LENGTH: usize = 32;