mirror of
https://github.com/saymrwulf/curve25519-dalek-source.git
synced 2026-09-06 20:41:14 +00:00
impl StaticSecret & create DH method that borrows our StaticSecret
This commit is contained in:
parent
9fe535dcc7
commit
4caffdcca0
1 changed files with 40 additions and 0 deletions
|
|
@ -81,6 +81,46 @@ impl<'a> From<&'a EphemeralSecret> for PublicKey {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A DH static secret key.
|
||||||
|
pub struct StaticSecret(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 {
|
||||||
|
/// Utility function to make it easier to call `x25519()` with
|
||||||
|
/// a static secret key and montegomery point as input and
|
||||||
|
/// a shared secret as the output.
|
||||||
|
pub fn diffie_hellman(&self, their_public: &PublicKey) -> SharedSecret {
|
||||||
|
SharedSecret(&self.0 * their_public.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generate a x25519 `StaticSecret` key.
|
||||||
|
pub fn new<T>(csprng: &mut T) -> Self
|
||||||
|
where T: RngCore + CryptoRng
|
||||||
|
{
|
||||||
|
let mut bytes = [0u8; 32];
|
||||||
|
|
||||||
|
csprng.fill_bytes(&mut bytes);
|
||||||
|
|
||||||
|
StaticSecret(clamp_scalar(bytes))
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> From<&'a StaticSecret> for PublicKey {
|
||||||
|
/// Given an x25519 `StaticSecret` key, compute its corresponding
|
||||||
|
/// `PublicKey` key.
|
||||||
|
fn from(secret: &'a StaticSecret) -> PublicKey {
|
||||||
|
PublicKey((&ED25519_BASEPOINT_TABLE * &secret.0).to_montgomery())
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/// A DH SharedSecret
|
/// A DH SharedSecret
|
||||||
pub struct SharedSecret(pub (crate) MontgomeryPoint);
|
pub struct SharedSecret(pub (crate) MontgomeryPoint);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue