Struct elliptic_curve::ecdh::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

§

type Output = T

Should always be Self
source§

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

§

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>,

§

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.