Skip to content

Commit

Permalink
Draft: Use generic KeyPair in CoverCrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
tbrezot committed Sep 7, 2022
1 parent 62515e9 commit 1d67232
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 22 deletions.
23 changes: 10 additions & 13 deletions src/asymmetric_crypto/curve25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<R: RngCore + CryptoRng>(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]
Expand All @@ -58,6 +50,14 @@ impl X25519PrivateKey {
}

impl KeyTrait<X25519_SK_LENGTH> for X25519PrivateKey {
/// Generate a new random key.
#[inline]
fn new<R: RngCore + CryptoRng>(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] {
Expand Down Expand Up @@ -228,18 +228,15 @@ impl ZeroizeOnDrop for X25519PrivateKey {}
#[serde(try_from = "&[u8]", into = "[u8; 32]")]
pub struct X25519PublicKey(RistrettoPoint);

impl X25519PublicKey {
impl KeyTrait<X25519_PK_LENGTH> for X25519PublicKey {
/// Generate a new random public key.
#[inline]
#[must_use]
pub fn new<R: RngCore + CryptoRng>(rng: &mut R) -> Self {
fn new<R: RngCore + CryptoRng>(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<X25519_PK_LENGTH> for X25519PublicKey {
/// Converts the given public key into an array of bytes.
#[inline]
fn to_bytes(&self) -> [u8; Self::LENGTH] {
Expand Down
18 changes: 15 additions & 3 deletions src/asymmetric_crypto/mod.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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<Output = Self::PrivateKey>
+ Sub<Output = Self::PrivateKey>
+ Mul<Output = Self::PrivateKey>,
{
/// 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<PK_LENGTH>;

Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,6 +35,10 @@ pub trait KeyTrait<const LENGTH: usize>:
/// Key length
const LENGTH: usize = LENGTH;

/// Generate a new random key.
#[must_use]
fn new<R: RngCore + CryptoRng>(rng: &mut R) -> Self;

/// Convert the given key into a vector of bytes.
#[must_use]
fn to_bytes(&self) -> [u8; LENGTH];
Expand Down
1 change: 1 addition & 0 deletions src/symmetric_crypto/aes_256_gcm_pure/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ pub fn decrypt_in_place_detached(
mod tests {

use crate::{
KeyTrait,
entropy::CsRng,
symmetric_crypto::{
aes_256_gcm_pure::{
Expand Down
11 changes: 5 additions & 6 deletions src/symmetric_crypto/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@ use zeroize::{Zeroize, ZeroizeOnDrop};
#[derive(Debug, Hash, Clone, PartialEq, Eq)]
pub struct Key<const LENGTH: usize>([u8; LENGTH]);

impl<const KEY_LENGTH: usize> Key<KEY_LENGTH> {
impl<const LENGTH: usize> KeyTrait<LENGTH> for Key<LENGTH> {
/// Generate a new symmetric random `Key`
pub fn new<R: RngCore + CryptoRng>(rng: &mut R) -> Self {
let mut key = [0; KEY_LENGTH];
#[inline]
fn new<R: RngCore + CryptoRng>(rng: &mut R) -> Self {
let mut key = [0; LENGTH];
rng.fill_bytes(&mut key);
Self(key)
}
}

impl<const LENGTH: usize> KeyTrait<LENGTH> for Key<LENGTH> {
/// Convert the given key into bytes.
#[inline]
fn to_bytes(&self) -> [u8; LENGTH] {
Expand Down Expand Up @@ -107,7 +106,7 @@ impl<const KEY_LENGTH: usize> Deref for Key<KEY_LENGTH> {
#[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;
Expand Down

0 comments on commit 1d67232

Please sign in to comment.