Update derived traits, add ability to zeroize on drop, and refactor
This commit is contained in:
parent
78f077b3b4
commit
082adec86b
4 changed files with 28 additions and 18 deletions
27
src/lib.rs
27
src/lib.rs
|
|
@ -6,11 +6,11 @@ pub mod edwards;
|
|||
pub mod montgomery;
|
||||
pub mod traits;
|
||||
|
||||
use crypto_bigint::subtle::{Choice, ConstantTimeEq};
|
||||
pub use crypto_bigint::{ConstChoice, Encoding, NonZero, U256};
|
||||
use std::fmt::{Display, Formatter};
|
||||
|
||||
#[cfg(feature = "rand")]
|
||||
use crypto_bigint::{Random, rand_core::OsRng};
|
||||
use crypto_bigint::{rand_core::OsRng, Random};
|
||||
|
||||
use crate::traits::{Curve, Point};
|
||||
pub(crate) use montgomery::*;
|
||||
|
|
@ -40,6 +40,7 @@ impl Public {
|
|||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
#[cfg_attr(feature = "zeroize", derive(Zeroize, ZeroizeOnDrop))]
|
||||
pub struct Secret(U256);
|
||||
|
||||
impl Secret {
|
||||
|
|
@ -54,16 +55,6 @@ impl Secret {
|
|||
|
||||
SharedSecret(p.get_x().to_le_bytes())
|
||||
}
|
||||
|
||||
pub fn to_bytes(&self) -> [u8; 32] {
|
||||
self.0.to_le_bytes()
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Secret {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_fmt(format_args!("{}", self.0))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<U256> for Secret {
|
||||
|
|
@ -72,7 +63,7 @@ impl From<U256> for Secret {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "zeroize", derive(Zeroize, ZeroizeOnDrop))]
|
||||
pub struct SharedSecret([u8; 32]);
|
||||
|
||||
impl SharedSecret {
|
||||
|
|
@ -81,6 +72,12 @@ impl SharedSecret {
|
|||
}
|
||||
}
|
||||
|
||||
impl ConstantTimeEq for SharedSecret {
|
||||
fn ct_eq(&self, other: &Self) -> Choice {
|
||||
self.0.ct_eq(&other.0)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
@ -90,7 +87,7 @@ mod tests {
|
|||
fn random_secret() {
|
||||
let secret = Secret::random();
|
||||
|
||||
assert_ne!(secret.0, U256::ZERO);
|
||||
assert!(secret.0 < CURVE_25519.p);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -128,6 +125,6 @@ mod tests {
|
|||
let sa = a.diffie_hellman(&pb);
|
||||
let sb = b.diffie_hellman(&pa);
|
||||
|
||||
assert_eq!(sa, sb);
|
||||
assert!(bool::from(sa.ct_eq(&sb)));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue