Skip to main content

SecretKey

Struct SecretKey 

Source
pub struct SecretKey<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 (when the zeroize feature of this crate is enabled).

§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 [elliptic_curve::pkcs8::FromPrivateKey][FromPrivateKey] 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§

Source§

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

Source

pub fn random(rng: impl CryptoRng + RngCore) -> Self

Generate a random SecretKey

Source

pub fn new(scalar: ScalarBytes<C>) -> Self

Create a new secret key from a serialized scalar value

Source

pub fn from_bytes(bytes: impl AsRef<[u8]>) -> Result<Self>

Deserialize raw private scalar as a big endian integer

Source

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

Expose the byte serialization of the value this SecretKey wraps

Source

pub fn as_scalar_bytes(&self) -> &ScalarBytes<C>

Borrow the inner secret ScalarBytes value.

§Warning

This value is key material.

Please treat it with the care it deserves!

Source

pub fn to_secret_scalar(&self) -> NonZeroScalar<C>

Get the secret scalar value for this key..

Source

pub fn public_key(&self) -> PublicKey<C>

Get the PublicKey which corresponds to this secret key

Trait Implementations§

Source§

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

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<C> Debug for SecretKey<C>
where C: Curve,

Source§

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

Formats the value using the given formatter. Read more
Source§

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

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<C> From<&NonZeroScalar<C>> for SecretKey<C>

Available on crate feature arithmetic only.
Source§

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

Converts to this type from the input type.
Source§

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

Available on crate feature zeroize only.
Source§

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

Converts to this type from the input type.
Source§

impl<C> From<NonZeroScalar<C>> for SecretKey<C>

Available on crate feature arithmetic only.
Source§

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

Converts to this type from the input type.
Source§

impl<C> From<SecretKey<C>> for NonZeroScalar<C>

Available on crate feature zeroize only.
Source§

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

Converts to this type from the input type.
Source§

impl<C> TryFrom<&[u8]> for SecretKey<C>
where C: Curve,

Source§

type Error = Error

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

fn try_from(slice: &[u8]) -> Result<Self>

Performs the conversion.

Auto Trait Implementations§

§

impl<C> Freeze for SecretKey<C>

§

impl<C> RefUnwindSafe for SecretKey<C>

§

impl<C> Send for SecretKey<C>

§

impl<C> Sync for SecretKey<C>

§

impl<C> Unpin for SecretKey<C>

§

impl<C> UnsafeUnpin for SecretKey<C>

§

impl<C> UnwindSafe for SecretKey<C>

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> 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 = !

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.