diff --git a/common/crypto/src/encryption/mod.rs b/common/crypto/src/encryption/mod.rs index b36ddf1af7..4a38cd89ed 100644 --- a/common/crypto/src/encryption/mod.rs +++ b/common/crypto/src/encryption/mod.rs @@ -1,39 +1,92 @@ use crate::PemStorable; +use curve25519_dalek::montgomery::MontgomeryPoint; +use curve25519_dalek::scalar::Scalar; -pub mod x25519; +// TODO: ensure this is a proper name for this considering we are not implementing entire DH here -pub trait MixnetEncryptionKeyPair -where - Priv: MixnetEncryptionPrivateKey, - Pub: MixnetEncryptionPublicKey, -{ - fn new() -> Self; - fn private_key(&self) -> &Priv; - fn public_key(&self) -> &Pub; - fn from_bytes(priv_bytes: &[u8], pub_bytes: &[u8]) -> Self; +const CURVE_GENERATOR: MontgomeryPoint = curve25519_dalek::constants::X25519_BASEPOINT; - // TODO: encryption related methods +pub struct KeyPair { + pub(crate) private_key: PrivateKey, + pub(crate) public_key: PublicKey, } -pub trait MixnetEncryptionPublicKey: - Sized + PemStorable + for<'a> From<&'a ::PrivateKeyMaterial> -{ - // we need to couple public and private keys together - type PrivateKeyMaterial: MixnetEncryptionPrivateKey; +impl KeyPair { + pub fn new() -> Self { + let mut rng = rand_os::OsRng::new().unwrap(); + let private_key_value = Scalar::random(&mut rng); + let public_key_value = CURVE_GENERATOR * private_key_value; - fn to_bytes(&self) -> Vec; - fn from_bytes(b: &[u8]) -> Self; -} - -pub trait MixnetEncryptionPrivateKey: Sized + PemStorable { - // we need to couple public and private keys together - type PublicKeyMaterial: MixnetEncryptionPublicKey; - - /// Returns the associated public key - fn public_key(&self) -> Self::PublicKeyMaterial { - self.into() + KeyPair { + private_key: PrivateKey(private_key_value), + public_key: PublicKey(public_key_value), + } } - fn to_bytes(&self) -> Vec; - fn from_bytes(b: &[u8]) -> Self; + pub fn private_key(&self) -> &PrivateKey { + &self.private_key + } + + pub fn public_key(&self) -> &PublicKey { + &self.public_key + } + + pub fn from_bytes(priv_bytes: &[u8], pub_bytes: &[u8]) -> Self { + KeyPair { + private_key: PrivateKey::from_bytes(priv_bytes), + public_key: PublicKey::from_bytes(pub_bytes), + } + } +} + +// COPY IS DERIVED ONLY TEMPORARILY UNTIL https://github.com/nymtech/nym/issues/47 is fixed +#[derive(Debug, Clone, Copy, Eq, PartialEq)] +pub struct PrivateKey(pub Scalar); + +impl PrivateKey { + pub fn to_bytes(&self) -> Vec { + self.0.to_bytes().to_vec() + } + + pub fn from_bytes(b: &[u8]) -> Self { + let mut bytes = [0; 32]; + bytes.copy_from_slice(&b[..]); + // due to trait restriction we have no choice but to panic if this fails + let key = Scalar::from_canonical_bytes(bytes).unwrap(); + Self(key) + } +} + +impl PemStorable for PrivateKey { + fn pem_type(&self) -> String { + String::from("X25519 PRIVATE KEY") + } +} + +#[derive(Debug, Clone, Eq, PartialEq)] +pub struct PublicKey(pub MontgomeryPoint); + +impl<'a> From<&'a PrivateKey> for PublicKey { + fn from(pk: &'a PrivateKey) -> Self { + PublicKey(CURVE_GENERATOR * pk.0) + } +} + +impl PublicKey { + pub fn to_bytes(&self) -> Vec { + self.0.to_bytes().to_vec() + } + + pub fn from_bytes(b: &[u8]) -> Self { + let mut bytes = [0; 32]; + bytes.copy_from_slice(&b[..]); + let key = MontgomeryPoint(bytes); + Self(key) + } +} + +impl PemStorable for PublicKey { + fn pem_type(&self) -> String { + String::from("X25519 PUBLIC KEY") + } } diff --git a/common/crypto/src/encryption/x25519.rs b/common/crypto/src/encryption/x25519.rs deleted file mode 100644 index 44d86783da..0000000000 --- a/common/crypto/src/encryption/x25519.rs +++ /dev/null @@ -1,99 +0,0 @@ -use crate::encryption::{ - MixnetEncryptionKeyPair, MixnetEncryptionPrivateKey, MixnetEncryptionPublicKey, -}; -use crate::PemStorable; -use curve25519_dalek::montgomery::MontgomeryPoint; -use curve25519_dalek::scalar::Scalar; - -// TODO: ensure this is a proper name for this considering we are not implementing entire DH here - -const CURVE_GENERATOR: MontgomeryPoint = curve25519_dalek::constants::X25519_BASEPOINT; - -pub struct KeyPair { - pub(crate) private_key: PrivateKey, - pub(crate) public_key: PublicKey, -} - -impl MixnetEncryptionKeyPair for KeyPair { - fn new() -> Self { - let mut rng = rand_os::OsRng::new().unwrap(); - let private_key_value = Scalar::random(&mut rng); - let public_key_value = CURVE_GENERATOR * private_key_value; - - KeyPair { - private_key: PrivateKey(private_key_value), - public_key: PublicKey(public_key_value), - } - } - - fn private_key(&self) -> &PrivateKey { - &self.private_key - } - - fn public_key(&self) -> &PublicKey { - &self.public_key - } - - fn from_bytes(priv_bytes: &[u8], pub_bytes: &[u8]) -> Self { - KeyPair { - private_key: PrivateKey::from_bytes(priv_bytes), - public_key: PublicKey::from_bytes(pub_bytes), - } - } -} - -// COPY IS DERIVED ONLY TEMPORARILY UNTIL https://github.com/nymtech/nym/issues/47 is fixed -#[derive(Debug, Clone, Copy, Eq, PartialEq)] -pub struct PrivateKey(pub Scalar); - -impl MixnetEncryptionPrivateKey for PrivateKey { - type PublicKeyMaterial = PublicKey; - - fn to_bytes(&self) -> Vec { - self.0.to_bytes().to_vec() - } - - fn from_bytes(b: &[u8]) -> Self { - let mut bytes = [0; 32]; - bytes.copy_from_slice(&b[..]); - // due to trait restriction we have no choice but to panic if this fails - let key = Scalar::from_canonical_bytes(bytes).unwrap(); - Self(key) - } -} - -impl PemStorable for PrivateKey { - fn pem_type(&self) -> String { - String::from("X25519 PRIVATE KEY") - } -} - -#[derive(Debug, Clone, Eq, PartialEq)] -pub struct PublicKey(pub MontgomeryPoint); - -impl<'a> From<&'a PrivateKey> for PublicKey { - fn from(pk: &'a PrivateKey) -> Self { - PublicKey(CURVE_GENERATOR * pk.0) - } -} - -impl MixnetEncryptionPublicKey for PublicKey { - type PrivateKeyMaterial = PrivateKey; - - fn to_bytes(&self) -> Vec { - self.0.to_bytes().to_vec() - } - - fn from_bytes(b: &[u8]) -> Self { - let mut bytes = [0; 32]; - bytes.copy_from_slice(&b[..]); - let key = MontgomeryPoint(bytes); - Self(key) - } -} - -impl PemStorable for PublicKey { - fn pem_type(&self) -> String { - String::from("X25519 PUBLIC KEY") - } -} diff --git a/common/crypto/src/identity/mod.rs b/common/crypto/src/identity/mod.rs index a966ec4cb9..270e25dcfb 100644 --- a/common/crypto/src/identity/mod.rs +++ b/common/crypto/src/identity/mod.rs @@ -1,6 +1,3 @@ -use crate::encryption::{ - MixnetEncryptionKeyPair, MixnetEncryptionPrivateKey, MixnetEncryptionPublicKey, -}; use crate::{encryption, PemStorable}; use bs58; use curve25519_dalek::scalar::Scalar; @@ -16,7 +13,7 @@ pub struct MixIdentityKeyPair { impl MixIdentityKeyPair { pub fn new() -> Self { - let keypair = encryption::x25519::KeyPair::new(); + let keypair = encryption::KeyPair::new(); MixIdentityKeyPair { private_key: MixIdentityPrivateKey(keypair.private_key), public_key: MixIdentityPublicKey(keypair.public_key), @@ -40,7 +37,7 @@ impl MixIdentityKeyPair { } #[derive(Debug, Clone, Eq, PartialEq)] -pub struct MixIdentityPublicKey(encryption::x25519::PublicKey); +pub struct MixIdentityPublicKey(encryption::PublicKey); impl MixIdentityPublicKey { pub fn derive_address(&self) -> DestinationAddressBytes { @@ -56,7 +53,7 @@ impl MixIdentityPublicKey { } pub fn from_bytes(b: &[u8]) -> Self { - Self(encryption::x25519::PublicKey::from_bytes(b)) + Self(encryption::PublicKey::from_bytes(b)) } pub fn to_base58_string(&self) -> String { @@ -76,12 +73,12 @@ impl PemStorable for MixIdentityPublicKey { // COPY IS DERIVED ONLY TEMPORARILY UNTIL https://github.com/nymtech/nym/issues/47 is fixed #[derive(Debug, Clone, Copy, Eq, PartialEq)] -pub struct MixIdentityPrivateKey(pub encryption::x25519::PrivateKey); +pub struct MixIdentityPrivateKey(pub encryption::PrivateKey); impl<'a> From<&'a MixIdentityPrivateKey> for MixIdentityPublicKey { fn from(pk: &'a MixIdentityPrivateKey) -> Self { let private_ref = &pk.0; - let public: encryption::x25519::PublicKey = private_ref.into(); + let public: encryption::PublicKey = private_ref.into(); MixIdentityPublicKey(public) } } @@ -92,7 +89,7 @@ impl MixIdentityPrivateKey { } pub fn from_bytes(b: &[u8]) -> Self { - Self(encryption::x25519::PrivateKey::from_bytes(b)) + Self(encryption::PrivateKey::from_bytes(b)) } } diff --git a/common/pemstore/src/pemstore.rs b/common/pemstore/src/pemstore.rs index a8d7b4b239..0fbc38b71f 100644 --- a/common/pemstore/src/pemstore.rs +++ b/common/pemstore/src/pemstore.rs @@ -8,7 +8,7 @@ use std::io::prelude::*; use std::path::PathBuf; #[allow(dead_code)] -pub fn read_mix_encryption_keypair_from_disk(_id: String) -> crypto::encryption::x25519::KeyPair { +pub fn read_mix_encryption_keypair_from_disk(_id: String) -> crypto::encryption::KeyPair { unimplemented!() } @@ -31,8 +31,7 @@ impl PemStore { let private_pem = self.read_pem_file(self.private_mix_key.clone())?; let public_pem = self.read_pem_file(self.public_mix_key.clone())?; - let key_pair = - MixIdentityKeyPair::from_bytes(&private_pem.contents, &public_pem.contents); + let key_pair = MixIdentityKeyPair::from_bytes(&private_pem.contents, &public_pem.contents); if key_pair.private_key().pem_type() != private_pem.tag { return Err(io::Error::new( diff --git a/sfw-provider/src/main.rs b/sfw-provider/src/main.rs index ec2dee141e..408ec221b6 100644 --- a/sfw-provider/src/main.rs +++ b/sfw-provider/src/main.rs @@ -1,6 +1,5 @@ use crate::provider::ServiceProvider; use clap::{App, Arg, ArgMatches, SubCommand}; -use crypto::identity::MixIdentityKeyPair; use log::error; use std::net::ToSocketAddrs; use std::path::PathBuf;