Skip to content

Commit

Permalink
Merge branch 'main' into release/2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rozbb committed Mar 20, 2023
2 parents 987f7d6 + 97d6d55 commit d6c3cbf
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/x25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ impl PublicKey {
}
}

impl AsRef<[u8]> for PublicKey {
/// View this public key as a byte array.
#[inline]
fn as_ref(&self) -> &[u8] {
self.as_bytes()
}
}

/// A short-lived Diffie-Hellman secret key that can only be used to compute a single
/// [`SharedSecret`].
///
Expand Down Expand Up @@ -182,9 +190,16 @@ impl StaticSecret {
}

/// Extract this key's bytes for serialization.
#[inline]
pub fn to_bytes(&self) -> [u8; 32] {
self.0.to_bytes()
}

/// View this key as a byte array.
#[inline]
pub fn as_bytes(&self) -> &[u8; 32] {
self.0.as_bytes()
}
}

impl From<[u8; 32]> for StaticSecret {
Expand All @@ -201,6 +216,14 @@ impl<'a> From<&'a StaticSecret> for PublicKey {
}
}

impl AsRef<[u8]> for StaticSecret {
/// View this key as a byte array.
#[inline]
fn as_ref(&self) -> &[u8] {
self.as_bytes()
}
}

/// The result of a Diffie-Hellman key exchange.
///
/// Each party computes this using their [`EphemeralSecret`] or [`StaticSecret`] and their
Expand All @@ -226,9 +249,9 @@ impl SharedSecret {
/// key exchange with non-contributory behaviour.
///
/// In some more exotic protocols which need to guarantee "contributory"
/// behaviour for both parties, that is, that each party contibuted a public
/// behaviour for both parties, that is, that each party contributed a public
/// value which increased the security of the resulting shared secret.
/// To take an example protocol attack where this could lead to undesireable
/// To take an example protocol attack where this could lead to undesirable
/// results [from Thái "thaidn" Dương](https://vnhacker.blogspot.com/2015/09/why-not-validating-curve25519-public.html):
///
/// > If Mallory replaces Alice's and Bob's public keys with zero, which is
Expand Down Expand Up @@ -261,6 +284,14 @@ impl SharedSecret {
}
}

impl AsRef<[u8]> for SharedSecret {
/// View this shared secret key as a byte array.
#[inline]
fn as_ref(&self) -> &[u8] {
self.as_bytes()
}
}

/// The bare, byte-oriented x25519 function, exactly as specified in RFC7748.
///
/// This can be used with [`X25519_BASEPOINT_BYTES`] for people who
Expand Down

0 comments on commit d6c3cbf

Please sign in to comment.