Skip to content

Commit 35ec00a

Browse files
authored
elliptic-curve: add SecretKey::diffie_hellman (#2248)
I found myself wanting this working on `dhkem`
1 parent ee2a279 commit 35ec00a

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

elliptic-curve/src/secret_key.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing};
1919
#[cfg(feature = "arithmetic")]
2020
use crate::{CurveArithmetic, NonZeroScalar, PublicKey};
2121

22+
#[cfg(feature = "ecdh")]
23+
use crate::ecdh;
24+
2225
#[cfg(feature = "pem")]
2326
use pem_rfc7468::{self as pem, PemLabel};
2427

@@ -172,6 +175,17 @@ where
172175
self.inner.to_bytes()
173176
}
174177

178+
/// Perform Elliptic Curve Diffie-Hellman with the given public key, returning a shared secret.
179+
///
180+
/// See the documentation in the [`ecdh`] module for more information.
181+
#[cfg(feature = "ecdh")]
182+
pub fn diffie_hellman(self, public_key: &PublicKey<C>) -> ecdh::SharedSecret<C>
183+
where
184+
C: CurveArithmetic,
185+
{
186+
ecdh::diffie_hellman(self.to_nonzero_scalar(), public_key.as_affine())
187+
}
188+
175189
/// Deserialize secret key encoded in the SEC1 ASN.1 DER `ECPrivateKey` format.
176190
#[cfg(feature = "sec1")]
177191
pub fn from_sec1_der(der_bytes: &[u8]) -> Result<Self>

0 commit comments

Comments
 (0)