elliptic_curve::ecdh

Struct SharedSecret

Source
pub struct SharedSecret<C: Curve> { /* private fields */ }
Expand description

Shared secret value computed via ECDH key agreement.

Implementations§

Source§

impl<C: Curve> SharedSecret<C>

Source

pub fn extract<D>(&self, salt: Option<&[u8]>) -> Hkdf<D, SimpleHmac<D>>
where D: BlockSizeUser + Clone + Digest,

Use HKDF (HMAC-based Extract-and-Expand Key Derivation Function) to extract entropy from this shared secret.

This method can be used to transform the shared secret into uniformly random values which are suitable as key material.

The D type parameter is a cryptographic digest function. sha2::Sha256 is a common choice for use with HKDF.

The salt parameter can be used to supply additional randomness. Some examples include:

  • randomly generated (but authenticated) string
  • fixed application-specific value
  • previous shared secret used for rekeying (as in TLS 1.3 and Noise)

After initializing HKDF, use Hkdf::expand to obtain output key material.

Source

pub fn raw_secret_bytes(&self) -> &FieldBytes<C>

This value contains the raw serialized x-coordinate of the elliptic curve point computed from a Diffie-Hellman exchange, serialized as bytes.

When in doubt, use SharedSecret::extract instead.

§⚠️ WARNING: NOT UNIFORMLY RANDOM! ⚠️

This value is not uniformly random and should not be used directly as a cryptographic key for anything which requires that property (e.g. symmetric ciphers).

Instead, the resulting value should be used as input to a Key Derivation Function (KDF) or cryptographic hash function to produce a symmetric key. The SharedSecret::extract function will do this for you.

Trait Implementations§

Source§

impl<C: Curve> Drop for SharedSecret<C>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<C: Curve> From<GenericArray<u8, <<C as Curve>::UInt as ArrayEncoding>::ByteSize>> for SharedSecret<C>

Source§

fn from(secret_bytes: FieldBytes<C>) -> Self

NOTE: this impl is intended to be used by curve implementations to instantiate a SharedSecret value from their respective AffinePoint type.

Curve implementations should provide the field element representing the affine x-coordinate as secret_bytes.

Source§

impl<C: Curve> ZeroizeOnDrop for SharedSecret<C>

Auto Trait Implementations§

§

impl<C> Freeze for SharedSecret<C>
where <<<C as Curve>::UInt as ArrayEncoding>::ByteSize as ArrayLength<u8>>::ArrayType: Freeze,

§

impl<C> RefUnwindSafe for SharedSecret<C>
where <<<C as Curve>::UInt as ArrayEncoding>::ByteSize as ArrayLength<u8>>::ArrayType: RefUnwindSafe,

§

impl<C> Send for SharedSecret<C>

§

impl<C> Sync for SharedSecret<C>

§

impl<C> Unpin for SharedSecret<C>
where <<<C as Curve>::UInt as ArrayEncoding>::ByteSize as ArrayLength<u8>>::ArrayType: Unpin,

§

impl<C> UnwindSafe for SharedSecret<C>
where <<<C as Curve>::UInt as ArrayEncoding>::ByteSize as ArrayLength<u8>>::ArrayType: UnwindSafe,

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> 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> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.