Skip to content

Commit

Permalink
Improve key exchange zeroizing and test coverage
Browse files Browse the repository at this point in the history
fmt
  • Loading branch information
AaronFeickert committed Aug 16, 2023
1 parent 2de9dc2 commit 8f99926
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions src/dhke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
use core::ops::Mul;

use zeroize::Zeroize;
use zeroize::{Zeroize, ZeroizeOnDrop};

use crate::keys::PublicKey;

/// A type to hold a DH secret key.
#[derive(Zeroize, ZeroizeOnDrop)]
pub struct DiffieHellmanSharedSecret<P>(P)
where P: Zeroize;

Expand All @@ -35,20 +36,31 @@ where
}
}

impl<P> Zeroize for DiffieHellmanSharedSecret<P>
where P: Zeroize
{
/// Zeroize the shared secret's underlying public key
fn zeroize(&mut self) {
self.0.zeroize();
}
}
#[cfg(test)]
mod test {
use rand_core::OsRng;

impl<P> Drop for DiffieHellmanSharedSecret<P>
where P: Zeroize
{
/// Zeroize the shared secret when out of scope or otherwise dropped
fn drop(&mut self) {
self.zeroize();
use super::DiffieHellmanSharedSecret;
use crate::{
keys::{PublicKey, SecretKey},
ristretto::{RistrettoPublicKey, RistrettoSecretKey},
};

#[test]
fn test_dhke() {
// Generate two key pairs
let mut rng = OsRng;

let sk1 = RistrettoSecretKey::random(&mut rng);
let pk1 = RistrettoPublicKey::from_secret_key(&sk1);

let sk2 = RistrettoSecretKey::random(&mut rng);
let pk2 = RistrettoPublicKey::from_secret_key(&sk2);

// Assert that both sides of a key exchange match
let left = DiffieHellmanSharedSecret::<RistrettoPublicKey>::new(&sk1, &pk2);
let right = DiffieHellmanSharedSecret::<RistrettoPublicKey>::new(&sk2, &pk1);

assert_eq!(left.as_bytes(), right.as_bytes());
}
}

0 comments on commit 8f99926

Please sign in to comment.