Skip to content

Commit f110174

Browse files
authored
p224: add ecdh feature (RustCrypto#814)
Adds a feature for performing elliptic curve Diffie-Hellman similar to the same feature in the `p256` and `p384` crates.
1 parent 02dac21 commit f110174

File tree

6 files changed

+59
-2
lines changed

6 files changed

+59
-2
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

p224/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ primeorder = { version = "0.13", optional = true, path = "../primeorder" }
2525
[dev-dependencies]
2626
hex-literal = "0.4"
2727
primeorder = { version = "0.13", features = ["dev"], path = "../primeorder" }
28+
rand_core = { version = "0.6", features = ["getrandom"] }
2829

2930
[features]
3031
default = ["pem", "std"]
3132
alloc = ["elliptic-curve/alloc"]
3233
std = ["alloc", "elliptic-curve/std"]
3334

35+
ecdh = ["wip-arithmetic-do-not-use", "elliptic-curve/ecdh"]
3436
pem = ["elliptic-curve/pem", "pkcs8"]
3537
pkcs8 = ["elliptic-curve/pkcs8"]
3638
test-vectors = ["dep:hex-literal"]

p224/src/ecdh.rs

+47
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 secp224r1 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 p224::{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::NistP224;
42+
43+
/// NIST P-224 Ephemeral Diffie-Hellman Secret.
44+
pub type EphemeralSecret = elliptic_curve::ecdh::EphemeralSecret<NistP224>;
45+
46+
/// Shared secret value computed via ECDH key agreement.
47+
pub type SharedSecret = elliptic_curve::ecdh::SharedSecret<NistP224>;

p224/src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
#[cfg(feature = "wip-arithmetic-do-not-use")]
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

@@ -92,6 +95,10 @@ pub type FieldBytes = elliptic_curve::FieldBytes<NistP224>;
9295

9396
impl FieldBytesEncoding<NistP224> for Uint {}
9497

98+
/// NIST P-224 public key.
99+
#[cfg(feature = "wip-arithmetic-do-not-use")]
100+
pub type PublicKey = elliptic_curve::PublicKey<NistP224>;
101+
95102
/// NIST P-224 secret key.
96103
pub type SecretKey = elliptic_curve::SecretKey<NistP224>;
97104

p256/src/ecdh.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Elliptic Curve Diffie-Hellman (Ephemeral) Support.
22
//!
33
//! This module contains a high-level interface for performing ephemeral
4-
//! Diffie-Hellman key exchanges using the secp256k1 elliptic curve.
4+
//! Diffie-Hellman key exchanges using the secp256r1 elliptic curve.
55
//!
66
//! # Usage
77
//!

p384/src/ecdh.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Elliptic Curve Diffie-Hellman (Ephemeral) Support.
22
//!
33
//! This module contains a high-level interface for performing ephemeral
4-
//! Diffie-Hellman key exchanges using the secp384 elliptic curve.
4+
//! Diffie-Hellman key exchanges using the secp384r1 elliptic curve.
55
//!
66
//! # Usage
77
//!

0 commit comments

Comments
 (0)