Move enc::sym::Secret to enc::Secret

Signed-off-by: Luca Fulchir <luca.fulchir@runesauth.com>
This commit is contained in:
Luca Fulchir 2023-06-01 12:56:52 +02:00
parent 5b338c8758
commit 9634fbba31
Signed by: luca.fulchir
GPG Key ID: 8F6440603D13A78E
6 changed files with 50 additions and 47 deletions

View File

@ -15,8 +15,8 @@ use crate::{
enc::{ enc::{
asym::{ExchangePubKey, KeyExchangeKind, KeyID}, asym::{ExchangePubKey, KeyExchangeKind, KeyID},
hkdf::HkdfKind, hkdf::HkdfKind,
sym::{CipherKind, HeadLen, Secret, TagLen}, sym::{CipherKind, HeadLen, TagLen},
Random, Random, Secret,
}, },
}; };

View File

@ -5,7 +5,7 @@ use ::num_traits::FromPrimitive;
use super::Error; use super::Error;
use crate::{ use crate::{
config::Config, config::Config,
enc::{sym::Secret, Random}, enc::{Random, Secret},
}; };
/// Public key ID /// Public key ID

View File

@ -4,7 +4,7 @@
use ::sha3::Sha3_256; use ::sha3::Sha3_256;
use ::zeroize::Zeroize; use ::zeroize::Zeroize;
use crate::{config::Config, enc::sym::Secret}; use crate::{config::Config, enc::Secret};
/// Kind of HKDF /// Kind of HKDF
#[derive(Debug, Copy, Clone, PartialEq, ::num_derive::FromPrimitive)] #[derive(Debug, Copy, Clone, PartialEq, ::num_derive::FromPrimitive)]

View File

@ -8,6 +8,7 @@ pub mod sym;
pub use errors::Error; pub use errors::Error;
use ::ring::rand::SecureRandom; use ::ring::rand::SecureRandom;
use ::zeroize::Zeroize;
/// wrapper where we implement whatever random traint stuff each library needs /// wrapper where we implement whatever random traint stuff each library needs
pub struct Random { pub struct Random {
@ -72,3 +73,42 @@ impl ::rand_core::RngCore for &Random {
} }
} }
impl ::rand_core::CryptoRng for &Random {} impl ::rand_core::CryptoRng for &Random {}
/// Secret, used for keys.
/// Grants that on drop() we will zero out memory
#[derive(Zeroize, Clone)]
#[zeroize(drop)]
pub struct Secret([u8; 32]);
// Fake debug implementation to avoid leaking secrets
impl ::core::fmt::Debug for Secret {
fn fmt(
&self,
f: &mut core::fmt::Formatter<'_>,
) -> Result<(), ::std::fmt::Error> {
::core::fmt::Debug::fmt("[hidden secret]", f)
}
}
impl Secret {
/// New randomly generated secret
pub fn new_rand(rand: &Random) -> Self {
let mut ret = Self([0; 32]);
rand.fill(&mut ret.0);
ret
}
/// return a reference to the secret
pub fn as_ref(&self) -> &[u8; 32] {
&self.0
}
}
impl From<[u8; 32]> for Secret {
fn from(shared_secret: [u8; 32]) -> Self {
Self(shared_secret)
}
}
impl From<::x25519_dalek::SharedSecret> for Secret {
fn from(shared_secret: ::x25519_dalek::SharedSecret) -> Self {
Self(shared_secret.to_bytes())
}
}

View File

@ -1,49 +1,12 @@
//! Symmetric cypher stuff //! Symmetric cypher stuff
use super::Error; use super::Error;
use crate::{config::Config, enc::Random}; use crate::{
config::Config,
enc::{Random, Secret},
};
use ::zeroize::Zeroize; use ::zeroize::Zeroize;
/// Secret, used for keys.
/// Grants that on drop() we will zero out memory
#[derive(Zeroize, Clone)]
#[zeroize(drop)]
pub struct Secret([u8; 32]);
// Fake debug implementation to avoid leaking secrets
impl ::core::fmt::Debug for Secret {
fn fmt(
&self,
f: &mut core::fmt::Formatter<'_>,
) -> Result<(), ::std::fmt::Error> {
::core::fmt::Debug::fmt("[hidden secret]", f)
}
}
impl Secret {
/// New randomly generated secret
pub fn new_rand(rand: &Random) -> Self {
let mut ret = Self([0; 32]);
rand.fill(&mut ret.0);
ret
}
/// return a reference to the secret
pub fn as_ref(&self) -> &[u8; 32] {
&self.0
}
}
impl From<[u8; 32]> for Secret {
fn from(shared_secret: [u8; 32]) -> Self {
Self(shared_secret)
}
}
impl From<::x25519_dalek::SharedSecret> for Secret {
fn from(shared_secret: ::x25519_dalek::SharedSecret) -> Self {
Self(shared_secret.to_bytes())
}
}
/// List of possible Ciphers /// List of possible Ciphers
#[derive(Debug, Copy, Clone, PartialEq, ::num_derive::FromPrimitive)] #[derive(Debug, Copy, Clone, PartialEq, ::num_derive::FromPrimitive)]
#[repr(u8)] #[repr(u8)]

View File

@ -16,8 +16,8 @@ use crate::{
enc::{ enc::{
asym::{self, PrivKey, PubKey}, asym::{self, PrivKey, PubKey},
hkdf::{self, Hkdf, HkdfKind}, hkdf::{self, Hkdf, HkdfKind},
sym::{self, Secret}, sym::{self},
Random, Random, Secret,
}, },
inner::{HandshakeAction, HandshakeTracker, ThreadTracker}, inner::{HandshakeAction, HandshakeTracker, ThreadTracker},
}; };