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,
impl<C> SecretKey<C>where
C: Curve,
pub fn random(rng: &mut impl CryptoRngCore) -> SecretKey<C>where
C: CurveArithmetic,
pub fn random(rng: &mut impl CryptoRngCore) -> SecretKey<C>where
C: CurveArithmetic,
Generate a random SecretKey.
pub fn new(scalar: ScalarPrimitive<C>) -> SecretKey<C>
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>
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,
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,
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>
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>
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>
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>
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> ConstantTimeEq for SecretKey<C>where
C: Curve,
impl<C> ConstantTimeEq for SecretKey<C>where
C: Curve,
§impl<C> From<&NonZeroScalar<C>> for SecretKey<C>where
C: CurveArithmetic,
Available on crate feature arithmetic only.
impl<C> From<&NonZeroScalar<C>> for SecretKey<C>where
C: CurveArithmetic,
arithmetic only.§fn from(scalar: &NonZeroScalar<C>) -> SecretKey<C>
fn from(scalar: &NonZeroScalar<C>) -> SecretKey<C>
§impl<C> From<&SecretKey<C>> for NonZeroScalar<C>where
C: CurveArithmetic,
impl<C> From<&SecretKey<C>> for NonZeroScalar<C>where
C: CurveArithmetic,
§fn from(sk: &SecretKey<C>) -> NonZeroScalar<C>
fn from(sk: &SecretKey<C>) -> NonZeroScalar<C>
§impl<C> From<&SecretKey<C>> for SigningKey<C>where
C: PrimeCurve + CurveArithmetic,
<C as CurveArithmetic>::Scalar: Invert<Output = CtOption<<C as CurveArithmetic>::Scalar>> + SignPrimitive<C>,
<<C as Curve>::FieldBytesSize as Add>::Output: ArrayLength<u8>,
impl<C> From<&SecretKey<C>> for SigningKey<C>where
C: PrimeCurve + CurveArithmetic,
<C as CurveArithmetic>::Scalar: Invert<Output = CtOption<<C as CurveArithmetic>::Scalar>> + SignPrimitive<C>,
<<C as Curve>::FieldBytesSize as Add>::Output: ArrayLength<u8>,
§impl<C> From<&SigningKey<C>> for SecretKey<C>where
C: PrimeCurve + CurveArithmetic,
<C as CurveArithmetic>::Scalar: Invert<Output = CtOption<<C as CurveArithmetic>::Scalar>> + SignPrimitive<C>,
<<C as Curve>::FieldBytesSize as Add>::Output: ArrayLength<u8>,
impl<C> From<&SigningKey<C>> for SecretKey<C>where
C: PrimeCurve + CurveArithmetic,
<C as CurveArithmetic>::Scalar: Invert<Output = CtOption<<C as CurveArithmetic>::Scalar>> + SignPrimitive<C>,
<<C as Curve>::FieldBytesSize as Add>::Output: ArrayLength<u8>,
§impl<C> From<NonZeroScalar<C>> for SecretKey<C>where
C: CurveArithmetic,
Available on crate feature arithmetic only.
impl<C> From<NonZeroScalar<C>> for SecretKey<C>where
C: CurveArithmetic,
arithmetic only.§fn from(scalar: NonZeroScalar<C>) -> SecretKey<C>
fn from(scalar: NonZeroScalar<C>) -> SecretKey<C>
§impl<C> From<SecretKey<C>> for NonZeroScalar<C>where
C: CurveArithmetic,
impl<C> From<SecretKey<C>> for NonZeroScalar<C>where
C: CurveArithmetic,
§fn from(sk: SecretKey<C>) -> NonZeroScalar<C>
fn from(sk: SecretKey<C>) -> NonZeroScalar<C>
§impl<C> From<SecretKey<C>> for SigningKey<C>where
C: PrimeCurve + CurveArithmetic,
<C as CurveArithmetic>::Scalar: Invert<Output = CtOption<<C as CurveArithmetic>::Scalar>> + SignPrimitive<C>,
<<C as Curve>::FieldBytesSize as Add>::Output: ArrayLength<u8>,
impl<C> From<SecretKey<C>> for SigningKey<C>where
C: PrimeCurve + CurveArithmetic,
<C as CurveArithmetic>::Scalar: Invert<Output = CtOption<<C as CurveArithmetic>::Scalar>> + SignPrimitive<C>,
<<C as Curve>::FieldBytesSize as Add>::Output: ArrayLength<u8>,
§impl<C> From<SigningKey<C>> for SecretKey<C>where
C: PrimeCurve + CurveArithmetic,
<C as CurveArithmetic>::Scalar: Invert<Output = CtOption<<C as CurveArithmetic>::Scalar>> + SignPrimitive<C>,
<<C as Curve>::FieldBytesSize as Add>::Output: ArrayLength<u8>,
impl<C> From<SigningKey<C>> for SecretKey<C>where
C: PrimeCurve + CurveArithmetic,
<C as CurveArithmetic>::Scalar: Invert<Output = CtOption<<C as CurveArithmetic>::Scalar>> + SignPrimitive<C>,
<<C as Curve>::FieldBytesSize as Add>::Output: ArrayLength<u8>,
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>
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> UnwindSafe for SecretKey<C>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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