-
Notifications
You must be signed in to change notification settings - Fork 133
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
Create new types for keys and clear values on drop #15
Conversation
…phemeral type; implement Mul for Ephemeral; create SharedSecret type & implement drop; change docs to reflect new methods on the Ephemeral type
Could the build failure be related to rust-random/rand#645 ? |
Moving the protocol flow into types looks great! One thing I notice is that the ephemeral secret key (a Then the |
Co-Authored-By: DebugSteven <debugsteven@gmail.com>
…or EphemeralSecret
src/x25519.rs
Outdated
fn mul(self, secret: &'b EphemeralSecret) -> EphemeralPublic { | ||
EphemeralPublic(self.0 * secret.0) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason to expose this trait impl if the diffie_hellman
function exists? My guess is that it's probably not necessary.
src/x25519.rs
Outdated
|
||
/// A DH SharedSecret | ||
#[repr(C)] | ||
#[derive(Default)] // we derive Default in order to use the clear() method in Drop |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it really necessary to derive Default
(instead of just calling self.0.clear()
in the Drop
impl)?
It seems like it would be better not to implement Default
, so that the only way to create a SharedSecret
is as the output of a DH function.
src/x25519.rs
Outdated
|
||
/// View this shared secret key as a byte array. | ||
#[inline] | ||
pub fn as_bytes<'a>(&'a self) -> &'a [u8; 32] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I think these lifetime specifiers might be redundant
src/x25519.rs
Outdated
/// an ephemeral secret key and montegomery point as input and | ||
/// a shared secret as the output. | ||
pub fn diffie_hellman(&self, their_public: &EphemeralPublic) -> SharedSecret { | ||
SharedSecret(x25519(&self.0, &MontgomeryPoint(*their_public.0.as_bytes()))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that there's an EphemeralPublic
type (which already holds a MontgomeryPoint
), there's no need to copy it into a new point and call the x25519
function ... in fact the entire x25519
function could be removed and combined into the diffie_hellman
function.
Also, if diffie_hellman
takes self
rather than &self
, then an EphemeralSecret
can't be used more than once.
Hey, sorry for not having bandwidth for this until now. I think that the ephemeral DH API looks good. You asked (out-of-band) about whether the function should be provided as in the RFC or whether the same functionality should be provided by an API which is different than the way that the RFC is written. One point is that the ephemeral DH API, as you've implemented it, forces the user to actually do ephemeral DH, only using the secret key once (since it's consumed by the DH function). This is stricter than what the
Then this separation allows a few simplifications:
|
note: with those changes the RFC tests need to be using the bare |
Co-Authored-By: DebugSteven <debugsteven@gmail.com>
Co-Authored-By: DebugSteven <debugsteven@gmail.com>
Co-Authored-By: DebugSteven <debugsteven@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
PR for issue #9
Created
Ephemeral
&SharedSecret
types to use for keys.diffie_hellman
,generate_public
, &generate_secret
are now methods onEphemeral
.Both types use
clear
in their respectivedrop
implementations &Ephemeral
has an implementation forMul
.