Skip to main content

SecretKey

Struct SecretKey 

pub struct SecretKey<C>
where C: Curve,
{ /* private fields */ }
Expand description

Elliptic curve secret keys.

This type wraps a secret scalar value, helping to prevent accidental exposure and securely erasing the value from memory when dropped.

§Parsing PKCS#8 Keys

PKCS#8 is a commonly used format for encoding secret keys (especially ones generated by OpenSSL).

Keys in PKCS#8 format are either binary (ASN.1 BER/DER), or PEM encoded (ASCII) and begin with the following:

-----BEGIN PRIVATE KEY-----

To decode an elliptic curve private key from PKCS#8, enable the pkcs8 feature of this crate (or the pkcs8 feature of a specific RustCrypto elliptic curve crate) and use the [DecodePrivateKey] trait to parse it.

When the pem feature of this crate (or a specific RustCrypto elliptic curve crate) is enabled, a [FromStr] impl is also available.

Implementations§

§

impl<C> SecretKey<C>
where C: Curve,

pub fn random(rng: &mut impl CryptoRngCore) -> SecretKey<C>
where C: CurveArithmetic,

Generate a random SecretKey.

pub fn new(scalar: ScalarPrimitive<C>) -> SecretKey<C>

Create a new secret key from a scalar value.

pub fn as_scalar_primitive(&self) -> &ScalarPrimitive<C>

Borrow the inner secret ScalarPrimitive value.

§⚠️ Warning

This value is key material.

Please treat it with the care it deserves!

pub fn to_nonzero_scalar(&self) -> NonZeroScalar<C>
where C: CurveArithmetic,

Get the secret NonZeroScalar value for this key.

§⚠️ Warning

This value is key material.

Please treat it with the care it deserves!

pub fn public_key(&self) -> PublicKey<C>
where C: CurveArithmetic,

Get the PublicKey which corresponds to this secret key

pub fn from_bytes( bytes: &GenericArray<u8, <C as Curve>::FieldBytesSize>, ) -> Result<SecretKey<C>, Error>

Deserialize secret key from an encoded secret scalar.

pub fn from_slice(slice: &[u8]) -> Result<SecretKey<C>, Error>

Deserialize secret key from an encoded secret scalar passed as a byte slice.

The slice is expected to be a minimum of 24-bytes (192-byts) and at most C::FieldBytesSize bytes in length.

Byte slices shorter than the field size are handled by zero padding the input.

pub fn to_bytes(&self) -> GenericArray<u8, <C as Curve>::FieldBytesSize>

Serialize raw secret scalar as a big endian integer.

pub fn from_sec1_der(der_bytes: &[u8]) -> Result<SecretKey<C>, Error>

Deserialize secret key encoded in the SEC1 ASN.1 DER ECPrivateKey format.

Trait Implementations§

§

impl<C> Clone for SecretKey<C>
where C: Clone + Curve,

§

fn clone(&self) -> SecretKey<C>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
§

impl<C> ConstantTimeEq for SecretKey<C>
where C: Curve,

§

fn ct_eq(&self, other: &SecretKey<C>) -> Choice

Determine if two items are equal. Read more
Source§

fn ct_ne(&self, other: &Self) -> Choice

Determine if two items are NOT equal. Read more
§

impl<C> Debug for SecretKey<C>
where C: Curve,

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl<C> Drop for SecretKey<C>
where C: Curve,

§

fn drop(&mut self)

Executes the destructor for this type. Read more
§

impl<C> From<&NonZeroScalar<C>> for SecretKey<C>
where C: CurveArithmetic,

Available on crate feature arithmetic only.
§

fn from(scalar: &NonZeroScalar<C>) -> SecretKey<C>

Converts to this type from the input type.
§

impl<C> From<&SecretKey<C>> for NonZeroScalar<C>
where C: CurveArithmetic,

§

fn from(sk: &SecretKey<C>) -> NonZeroScalar<C>

Converts to this type from the input type.
§

impl<C> From<&SecretKey<C>> for SigningKey<C>

§

fn from(secret_key: &SecretKey<C>) -> SigningKey<C>

Converts to this type from the input type.
§

impl From<&SecretKey<NistP256>> for Scalar

§

fn from(secret_key: &SecretKey<NistP256>) -> Scalar

Converts to this type from the input type.
§

impl<C> From<&SigningKey<C>> for SecretKey<C>

§

fn from(secret_key: &SigningKey<C>) -> SecretKey<C>

Converts to this type from the input type.
§

impl<C> From<NonZeroScalar<C>> for SecretKey<C>
where C: CurveArithmetic,

Available on crate feature arithmetic only.
§

fn from(scalar: NonZeroScalar<C>) -> SecretKey<C>

Converts to this type from the input type.
§

impl<C> From<SecretKey<C>> for NonZeroScalar<C>
where C: CurveArithmetic,

§

fn from(sk: SecretKey<C>) -> NonZeroScalar<C>

Converts to this type from the input type.
§

impl<C> From<SecretKey<C>> for SigningKey<C>

§

fn from(secret_key: SecretKey<C>) -> SigningKey<C>

Converts to this type from the input type.
§

impl<C> From<SigningKey<C>> for SecretKey<C>

§

fn from(key: SigningKey<C>) -> SecretKey<C>

Converts to this type from the input type.
§

impl<C> PartialEq for SecretKey<C>
where C: Curve,

§

fn eq(&self, other: &SecretKey<C>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
§

impl<C> TryFrom<EcPrivateKey<'_>> for SecretKey<C>

Available on crate feature sec1 only.
§

type Error = Error

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

fn try_from(sec1_private_key: EcPrivateKey<'_>) -> Result<SecretKey<C>, Error>

Performs the conversion.
§

impl<C> Eq for SecretKey<C>
where C: Curve,

§

impl<C> ZeroizeOnDrop for SecretKey<C>
where C: Curve,

Auto Trait Implementations§

§

impl<C> Freeze for SecretKey<C>
where <C as Curve>::Uint: Freeze,

§

impl<C> RefUnwindSafe for SecretKey<C>
where <C as Curve>::Uint: RefUnwindSafe,

§

impl<C> Send for SecretKey<C>

§

impl<C> Sync for SecretKey<C>

§

impl<C> Unpin for SecretKey<C>
where <C as Curve>::Uint: Unpin,

§

impl<C> UnwindSafe for SecretKey<C>
where <C as Curve>::Uint: 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> 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
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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 = 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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V