|
| 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>; |
0 commit comments