Skip to content

Commit 8d3b67e

Browse files
authored
p521: add ecdh feature (#954)
Adds a feature for performing elliptic curve Diffie-Hellman
1 parent 3a19153 commit 8d3b67e

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

p521/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ base16ct = "0.2.0"
2727
[dev-dependencies]
2828
hex-literal = "0.4"
2929
primeorder = { version = "0.13.3", features = ["dev"], path = "../primeorder" }
30+
rand_core = { version = "0.6", features = ["getrandom"] }
3031

3132
[features]
3233
default = ["arithmetic", "pem", "std"]
3334
alloc = ["elliptic-curve/alloc"]
3435
std = ["alloc", "elliptic-curve/std"]
3536

3637
arithmetic = ["dep:primeorder"]
38+
ecdh = ["arithmetic", "elliptic-curve/ecdh"]
3739
jwk = ["elliptic-curve/jwk"]
3840
pem = ["elliptic-curve/pem", "pkcs8"]
3941
pkcs8 = ["elliptic-curve/pkcs8"]

p521/src/ecdh.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//! Elliptic Curve Diffie-Hellman (Ephemeral) Support.
2+
//!
3+
//! This module contains a high-level interface for performing ephemeral
4+
//! Diffie-Hellman key exchanges using the secp521r1 elliptic curve.
5+
//!
6+
//! # Usage
7+
//!
8+
//! This usage example is from the perspective of two participants in the
9+
//! exchange, nicknamed "Alice" and "Bob".
10+
//!
11+
//! ```
12+
//! use p521::{EncodedPoint, PublicKey, ecdh::EphemeralSecret};
13+
//! use rand_core::OsRng; // requires 'getrandom' feature
14+
//!
15+
//! // Alice
16+
//! let alice_secret = EphemeralSecret::random(&mut OsRng);
17+
//! let alice_pk_bytes = EncodedPoint::from(alice_secret.public_key());
18+
//!
19+
//! // Bob
20+
//! let bob_secret = EphemeralSecret::random(&mut OsRng);
21+
//! let bob_pk_bytes = EncodedPoint::from(bob_secret.public_key());
22+
//!
23+
//! // Alice decodes Bob's serialized public key and computes a shared secret from it
24+
//! let bob_public = PublicKey::from_sec1_bytes(bob_pk_bytes.as_ref())
25+
//! .expect("bob's public key is invalid!"); // In real usage, don't panic, handle this!
26+
//!
27+
//! let alice_shared = alice_secret.diffie_hellman(&bob_public);
28+
//!
29+
//! // Bob decodes Alice's serialized public key and computes the same shared secret
30+
//! let alice_public = PublicKey::from_sec1_bytes(alice_pk_bytes.as_ref())
31+
//! .expect("alice's public key is invalid!"); // In real usage, don't panic, handle this!
32+
//!
33+
//! let bob_shared = bob_secret.diffie_hellman(&alice_public);
34+
//!
35+
//! // Both participants arrive on the same shared secret
36+
//! assert_eq!(alice_shared.raw_secret_bytes(), bob_shared.raw_secret_bytes());
37+
//! ```
38+
39+
pub use elliptic_curve::ecdh::diffie_hellman;
40+
41+
use crate::NistP521;
42+
43+
/// NIST P-521 Ephemeral Diffie-Hellman Secret.
44+
pub type EphemeralSecret = elliptic_curve::ecdh::EphemeralSecret<NistP521>;
45+
46+
/// Shared secret value computed via ECDH key agreement.
47+
pub type SharedSecret = elliptic_curve::ecdh::SharedSecret<NistP521>;

p521/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
#[cfg(feature = "arithmetic")]
1919
pub mod arithmetic;
2020

21+
#[cfg(feature = "ecdh")]
22+
pub mod ecdh;
23+
2124
#[cfg(any(feature = "test-vectors", test))]
2225
pub mod test_vectors;
2326

@@ -82,6 +85,10 @@ pub type FieldBytes = elliptic_curve::FieldBytes<NistP521>;
8285

8386
impl FieldBytesEncoding<NistP521> for U576 {}
8487

88+
/// NIST P-521 public key.
89+
#[cfg(feature = "arithmetic")]
90+
pub type PublicKey = elliptic_curve::PublicKey<NistP521>;
91+
8592
/// NIST P-521 secret key.
8693
pub type SecretKey = elliptic_curve::SecretKey<NistP521>;
8794

0 commit comments

Comments
 (0)