Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use zeroize crate #39

Merged
merged 1 commit into from Nov 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 3 additions & 4 deletions Cargo.toml
Expand Up @@ -29,13 +29,12 @@ travis-ci = { repository = "dalek-cryptography/x25519-dalek", branch = "master"}
features = ["nightly"]

[dependencies]
curve25519-dalek = { version = "2.0.0-alpha.0", default-features = false }
curve25519-dalek = { version = "2", default-features = false }
rand_core = { version = "0.3", default-features = false }
clear_on_drop = { version = "0.2" }
# `serde` is renamed to `our_serde` in order to avoid a name collision between
# importing the serde dependency and enabling the curve25519-dalek/serde feature
our_serde = { package = "serde", version = "1", default-features = false, optional = true, features = ["derive"] }
zeroize = { version = "1", default-features = false }
zeroize = { version = "1", default-features = false, features = ["zeroize_derive"] }

[dev-dependencies]
bincode = "1"
Expand All @@ -50,6 +49,6 @@ harness = false
default = ["std", "u64_backend"]
serde = ["our_serde", "curve25519-dalek/serde"]
std = ["curve25519-dalek/std"]
nightly = ["curve25519-dalek/nightly", "clear_on_drop/nightly"]
nightly = ["curve25519-dalek/nightly"]
u64_backend = ["curve25519-dalek/u64_backend"]
u32_backend = ["curve25519-dalek/u32_backend"]
4 changes: 2 additions & 2 deletions src/lib.rs
Expand Up @@ -24,12 +24,12 @@
//! Note that docs will only build on nightly Rust until
//! `feature(external_doc)` is stabilized.

extern crate clear_on_drop;

extern crate curve25519_dalek;

extern crate rand_core;

extern crate zeroize;

#[cfg(test)]
extern crate rand_os;

Expand Down
32 changes: 8 additions & 24 deletions src/x25519.rs
Expand Up @@ -14,15 +14,15 @@
//! This implements x25519 key exchange as specified by Mike Hamburg
//! and Adam Langley in [RFC7748](https://tools.ietf.org/html/rfc7748).

use clear_on_drop::clear::Clear;

use curve25519_dalek::constants::ED25519_BASEPOINT_TABLE;
use curve25519_dalek::montgomery::MontgomeryPoint;
use curve25519_dalek::scalar::Scalar;

use rand_core::CryptoRng;
use rand_core::RngCore;

use zeroize::Zeroize;

/// A `PublicKey` is the corresponding public key converted from
/// an `EphemeralSecret` or a `StaticSecret` key.
#[cfg_attr(feature = "serde", serde(crate = "our_serde"))]
Expand Down Expand Up @@ -50,15 +50,10 @@ impl PublicKey {

/// A `EphemeralSecret` is a short lived Diffie-Hellman secret key
/// used to create a `SharedSecret` when given their `PublicKey`.
#[derive(Zeroize)]
#[zeroize(drop)]
pub struct EphemeralSecret(pub(crate) Scalar);

/// Overwrite ephemeral secret key material with null bytes when it goes out of scope.
impl Drop for EphemeralSecret {
fn drop(&mut self) {
self.0.clear();
}
}

impl EphemeralSecret {
/// Perform a Diffie-Hellman key agreement between `self` and
/// `their_public` key to produce a `SharedSecret`.
Expand Down Expand Up @@ -95,18 +90,12 @@ impl<'a> From<&'a EphemeralSecret> for PublicKey {
feature = "serde",
derive(our_serde::Serialize, our_serde::Deserialize)
)]
#[derive(Clone)]
#[derive(Clone, Zeroize)]
#[zeroize(drop)]
pub struct StaticSecret(
#[cfg_attr(feature = "serde", serde(with = "AllowUnreducedScalarBytes"))] pub(crate) Scalar,
);

/// Overwrite static secret key material with null bytes when it goes out of scope.
impl Drop for StaticSecret {
fn drop(&mut self) {
self.0.clear();
}
}

impl StaticSecret {
/// Perform a Diffie-Hellman key agreement between `self` and
/// `their_public` key to produce a `SharedSecret`.
Expand Down Expand Up @@ -149,15 +138,10 @@ impl<'a> From<&'a StaticSecret> for PublicKey {

/// A `SharedSecret` is a Diffie-Hellman shared secret that’s generated
/// from your `EphemeralSecret` or `StaticSecret` and their `PublicKey`.
#[derive(Zeroize)]
#[zeroize(drop)]
pub struct SharedSecret(pub(crate) MontgomeryPoint);

/// Overwrite shared secret material with null bytes when it goes out of scope.
impl Drop for SharedSecret {
fn drop(&mut self) {
self.0.clear();
}
}

impl SharedSecret {
/// View this shared secret key as a byte array.
#[inline]
Expand Down