Struct elliptic_curve::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.
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::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
This is supported on crate feature arithmetic
only.
arithmetic
only.Generate a random SecretKey
.
Create a new secret key from a scalar value.
Borrow the inner secret ScalarCore
value.
⚠️ Warning
This value is key material.
Please treat it with the care it deserves!
This is supported on crate feature arithmetic
only.
arithmetic
only.Get the secret NonZeroScalar
value for this key.
⚠️ Warning
This value is key material.
Please treat it with the care it deserves!
This is supported on crate feature arithmetic
only.
arithmetic
only.Get the PublicKey
which corresponds to this secret key
Deserialize raw secret scalar as a big endian integer.
Serialize raw secret scalar as a big endian integer.
pub fn from_sec1_der(der_bytes: &[u8]) -> Result<Self> where
C: Curve + ValidatePublicKey,
FieldSize<C>: ModulusSize,
This is supported on crate feature sec1
only.
pub fn from_sec1_der(der_bytes: &[u8]) -> Result<Self> where
C: Curve + ValidatePublicKey,
FieldSize<C>: ModulusSize,
sec1
only.Deserialize secret key encoded in the SEC1 ASN.1 DER ECPrivateKey
format.
pub fn to_sec1_der(&self) -> Result<Zeroizing<Vec<u8>>> where
C: Curve + ProjectiveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldSize<C>: ModulusSize,
This is supported on crate features alloc
and arithmetic
and sec1
only.
pub fn to_sec1_der(&self) -> Result<Zeroizing<Vec<u8>>> where
C: Curve + ProjectiveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldSize<C>: ModulusSize,
alloc
and arithmetic
and sec1
only.Serialize secret key in the SEC1 ASN.1 DER ECPrivateKey
format.
pub fn from_sec1_pem(s: &str) -> Result<Self> where
C: Curve + ValidatePublicKey,
FieldSize<C>: ModulusSize,
This is supported on crate feature pem
only.
pub fn from_sec1_pem(s: &str) -> Result<Self> where
C: Curve + ValidatePublicKey,
FieldSize<C>: ModulusSize,
pem
only.Parse SecretKey
from PEM-encoded SEC1 ECPrivateKey
format.
PEM-encoded SEC1 keys can be identified by the leading delimiter:
-----BEGIN EC PRIVATE KEY-----
pub fn to_pem(&self, line_ending: LineEnding) -> Result<Zeroizing<String>> where
C: Curve + ProjectiveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldSize<C>: ModulusSize,
This is supported on crate feature pem
only.
pub fn to_pem(&self, line_ending: LineEnding) -> Result<Zeroizing<String>> where
C: Curve + ProjectiveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldSize<C>: ModulusSize,
pem
only.Serialize private key as self-zeroizing PEM-encoded SEC1 ECPrivateKey
with the given pem::LineEnding
.
Pass Default::default()
to use the OS’s native line endings.
pub fn from_jwk(jwk: &JwkEcKey) -> Result<Self> where
C: JwkParameters + ValidatePublicKey,
FieldSize<C>: ModulusSize,
This is supported on crate feature jwk
only.
pub fn from_jwk(jwk: &JwkEcKey) -> Result<Self> where
C: JwkParameters + ValidatePublicKey,
FieldSize<C>: ModulusSize,
jwk
only.pub fn from_jwk_str(jwk: &str) -> Result<Self> where
C: JwkParameters + ValidatePublicKey,
FieldSize<C>: ModulusSize,
This is supported on crate feature jwk
only.
pub fn from_jwk_str(jwk: &str) -> Result<Self> where
C: JwkParameters + ValidatePublicKey,
FieldSize<C>: ModulusSize,
jwk
only.Parse a string containing a JSON Web Key (JWK) into a SecretKey
.
pub fn to_jwk(&self) -> JwkEcKey where
C: Curve + JwkParameters + ProjectiveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldSize<C>: ModulusSize,
This is supported on crate features arithmetic
and jwk
only.
pub fn to_jwk(&self) -> JwkEcKey where
C: Curve + JwkParameters + ProjectiveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldSize<C>: ModulusSize,
arithmetic
and jwk
only.Serialize this secret key as JwkEcKey
JSON Web Key (JWK).
pub fn to_jwk_string(&self) -> Zeroizing<String> where
C: Curve + JwkParameters + ProjectiveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldSize<C>: ModulusSize,
This is supported on crate features arithmetic
and jwk
only.
pub fn to_jwk_string(&self) -> Zeroizing<String> where
C: Curve + JwkParameters + ProjectiveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldSize<C>: ModulusSize,
arithmetic
and jwk
only.Serialize this secret key as JSON Web Key (JWK) string.
Trait Implementations
impl<C> DecodePrivateKey for SecretKey<C> where
C: Curve + AlgorithmParameters + ValidatePublicKey,
FieldSize<C>: ModulusSize,
This is supported on crate feature pkcs8
only.
impl<C> DecodePrivateKey for SecretKey<C> where
C: Curve + AlgorithmParameters + ValidatePublicKey,
FieldSize<C>: ModulusSize,
pkcs8
only.Deserialize PKCS#8 private key from ASN.1 DER-encoded data (binary format). Read more
Deserialize PKCS#8 private key from a PrivateKeyDocument
.
impl<C> EncodePrivateKey for SecretKey<C> where
C: Curve + AlgorithmParameters + ProjectiveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldSize<C>: ModulusSize,
This is supported on crate features arithmetic
and pem
only.
impl<C> EncodePrivateKey for SecretKey<C> where
C: Curve + AlgorithmParameters + ProjectiveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldSize<C>: ModulusSize,
arithmetic
and pem
only.Serialize a PrivateKeyDocument
containing a PKCS#8-encoded private key.
Serialize this private key as PEM-encoded PKCS#8 with the given LineEnding
.
impl<C> From<&'_ NonZeroScalar<C>> for SecretKey<C> where
C: Curve + ProjectiveArithmetic,
This is supported on crate feature arithmetic
only.
impl<C> From<&'_ NonZeroScalar<C>> for SecretKey<C> where
C: Curve + ProjectiveArithmetic,
arithmetic
only.Performs the conversion.
Performs the conversion.
impl<C> From<&'_ SecretKey<C>> for JwkEcKey where
C: Curve + JwkParameters + ProjectiveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldSize<C>: ModulusSize,
This is supported on crate features arithmetic
and jwk
only.
impl<C> From<&'_ SecretKey<C>> for JwkEcKey where
C: Curve + JwkParameters + ProjectiveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldSize<C>: ModulusSize,
arithmetic
and jwk
only.impl<C> From<NonZeroScalar<C>> for SecretKey<C> where
C: Curve + ProjectiveArithmetic,
This is supported on crate feature arithmetic
only.
impl<C> From<NonZeroScalar<C>> for SecretKey<C> where
C: Curve + ProjectiveArithmetic,
arithmetic
only.Performs the conversion.
Performs the conversion.
impl<C> From<SecretKey<C>> for JwkEcKey where
C: Curve + JwkParameters + ProjectiveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldSize<C>: ModulusSize,
This is supported on crate features arithmetic
and jwk
only.
impl<C> From<SecretKey<C>> for JwkEcKey where
C: Curve + JwkParameters + ProjectiveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldSize<C>: ModulusSize,
arithmetic
and jwk
only.impl<C> FromStr for SecretKey<C> where
C: Curve + AlgorithmParameters + ValidatePublicKey,
FieldSize<C>: ModulusSize,
This is supported on crate feature pem
only.
impl<C> FromStr for SecretKey<C> where
C: Curve + AlgorithmParameters + ValidatePublicKey,
FieldSize<C>: ModulusSize,
pem
only.impl<C> TryFrom<&'_ JwkEcKey> for SecretKey<C> where
C: Curve + JwkParameters + ValidatePublicKey,
FieldSize<C>: ModulusSize,
This is supported on crate feature jwk
only.
impl<C> TryFrom<&'_ JwkEcKey> for SecretKey<C> where
C: Curve + JwkParameters + ValidatePublicKey,
FieldSize<C>: ModulusSize,
jwk
only.impl<C> TryFrom<EcPrivateKey<'_>> for SecretKey<C> where
C: Curve + ValidatePublicKey,
FieldSize<C>: ModulusSize,
This is supported on crate feature sec1
only.
impl<C> TryFrom<EcPrivateKey<'_>> for SecretKey<C> where
C: Curve + ValidatePublicKey,
FieldSize<C>: ModulusSize,
sec1
only.impl<C> TryFrom<JwkEcKey> for SecretKey<C> where
C: Curve + JwkParameters + ValidatePublicKey,
FieldSize<C>: ModulusSize,
This is supported on crate feature jwk
only.
impl<C> TryFrom<JwkEcKey> for SecretKey<C> where
C: Curve + JwkParameters + ValidatePublicKey,
FieldSize<C>: ModulusSize,
jwk
only.impl<C> TryFrom<PrivateKeyInfo<'_>> for SecretKey<C> where
C: Curve + AlgorithmParameters + ValidatePublicKey,
FieldSize<C>: ModulusSize,
This is supported on crate feature pkcs8
only.
impl<C> TryFrom<PrivateKeyInfo<'_>> for SecretKey<C> where
C: Curve + AlgorithmParameters + ValidatePublicKey,
FieldSize<C>: ModulusSize,
pkcs8
only.Auto Trait Implementations
impl<C> RefUnwindSafe for SecretKey<C> where
<C as Curve>::UInt: RefUnwindSafe,
impl<C> UnwindSafe for SecretKey<C> where
<C as Curve>::UInt: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more
Serialize a EcPrivateKeyDocument
containing a SEC1-encoded private key.
Serialize this private key as PEM-encoded SEC1 with the given LineEnding
. Read more