Skip to main content

SecretKey

Struct SecretKey 

Source
pub struct SecretKey { /* private fields */ }
Expand description

X25519 Diffie-Hellman key exchange private key (RFC 7748).

§Generating a key

ⓘ
use crypto::curve25519::x25519::SecretKey;

let alice = SecretKey::generate().unwrap();
let bob = SecretKey::generate().unwrap();

§Diffie-Hellman key exchange

ⓘ
use crypto::curve25519::x25519::SecretKey;

let alice = SecretKey::generate().unwrap();
let bob = SecretKey::generate().unwrap();
let alice_shared = alice.ecdh(&bob.public_key()).unwrap();
let bob_shared = bob.ecdh(&alice.public_key()).unwrap();
assert_eq!(alice_shared, bob_shared);

ecdh returns EllipticCurveError::InvalidSharedSecret when the computed shared secret is all zeros. This happens whenever the peer public key is a low-order point (u = 0, u = 1, u = p - 1, or one of the two order-8 points), because the clamped scalar is a multiple of the curve’s cofactor and therefore annihilates such a point.

Security impact: an all-zero shared secret is not secret at all. Every peer that sends a low-order public key — including a malicious one — knows the value, so any key derived from it (even through a KDF) is known to the attacker. This yields a complete confidentiality and authentication break for protocols that do not independently detect the degenerate output. Callers should therefore treat the error as a fatal protocol failure and abort the handshake.

If the caller’s protocol already binds both public keys into the derived key (so that a non-contributory secret cannot be exploited), the error can be deliberately ignored by substituting the all-zero secret:

ⓘ
let shared = alice.ecdh(&bob.public_key()).unwrap_or_default();

§Conversion from Ed25519

An Ed25519 SecretKey can be converted to an X25519 key via From<&ed25519::SecretKey>.

§Security

The shared secret produced by ecdh must not be used directly as an encryption key. Apply a KDF (e.g. HKDF) first.

Implementations§

Source§

impl SecretKey

Source

pub fn generate() -> Result<SecretKey, EllipticCurveError>

Generates a fresh random secret key.

Returns EllipticCurveError::Random when the operating system’s random number generator is unavailable or fails.

Source

pub fn from_bytes(bytes: &[u8; 32]) -> SecretKey

Source

pub fn to_bytes(&self) -> [u8; 32]

Source

pub fn public_key(&self) -> PublicKey

Source

pub fn ecdh(&self, peer: &PublicKey) -> Result<[u8; 32], EllipticCurveError>

Perform a Diffie-Hellman key exchange to derive a shared secret. The shared secret IS NOT SAFE to use directly as an encryption key and an additional key derivation operation must be applied to it.

§Errors

Returns EllipticCurveError::InvalidSharedSecret if the computed shared secret is all zeros, which occurs when peer is a low-order public key. Such a secret is publicly known and must not be used to derive keys unless the protocol independently binds the peer public key into the derivation. See the type-level documentation for details.

Trait Implementations§

Source§

impl Clone for SecretKey

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Drop for SecretKey

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Eq for SecretKey

Source§

impl From<&SecretKey> for SecretKey

Source§

fn from(ed25519_key: &SecretKey) -> Self

Converts to this type from the input type.
Source§

impl From<&[u8; 32]> for SecretKey

Source§

fn from(bytes: &[u8; 32]) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for SecretKey

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for SecretKey

Source§

impl TryFrom<&[u8]> for SecretKey

Source§

type Error = EllipticCurveError

The type returned in the event of a conversion error.
Source§

fn try_from(bytes: &[u8]) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl Zeroize for SecretKey

Source§

fn zeroize(&mut self)

Zero out this object from memory using Rust intrinsics which ensure the zeroization operation is not “optimized away” by the compiler.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Zeroize for T
where T: Zeroize,