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 [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§
Source§impl<C> SecretKey<C>where
C: Curve,
impl<C> SecretKey<C>where
C: Curve,
Sourcepub fn random(rng: impl CryptoRng + RngCore) -> Selfwhere
C: ProjectiveArithmetic,
pub fn random(rng: impl CryptoRng + RngCore) -> Selfwhere
C: ProjectiveArithmetic,
Generate a random SecretKey
.
Sourcepub fn new(scalar: ScalarCore<C>) -> Self
pub fn new(scalar: ScalarCore<C>) -> Self
Create a new secret key from a scalar value.
Sourcepub fn as_scalar_core(&self) -> &ScalarCore<C>
pub fn as_scalar_core(&self) -> &ScalarCore<C>
Borrow the inner secret ScalarCore
value.
§⚠️ Warning
This value is key material.
Please treat it with the care it deserves!
Sourcepub fn to_nonzero_scalar(&self) -> NonZeroScalar<C>where
C: Curve + ProjectiveArithmetic,
pub fn to_nonzero_scalar(&self) -> NonZeroScalar<C>where
C: Curve + ProjectiveArithmetic,
Get the secret NonZeroScalar
value for this key.
§⚠️ Warning
This value is key material.
Please treat it with the care it deserves!
Sourcepub fn public_key(&self) -> PublicKey<C>where
C: Curve + ProjectiveArithmetic,
pub fn public_key(&self) -> PublicKey<C>where
C: Curve + ProjectiveArithmetic,
Get the PublicKey
which corresponds to this secret key
Sourcepub fn from_be_bytes(bytes: &[u8]) -> Result<Self>
pub fn from_be_bytes(bytes: &[u8]) -> Result<Self>
Deserialize raw secret scalar as a big endian integer.
Sourcepub fn to_be_bytes(&self) -> FieldBytes<C>
pub fn to_be_bytes(&self) -> FieldBytes<C>
Serialize raw secret scalar as a big endian integer.
Sourcepub fn from_sec1_der(der_bytes: &[u8]) -> Result<Self>
pub fn from_sec1_der(der_bytes: &[u8]) -> Result<Self>
Deserialize secret key encoded in the SEC1 ASN.1 DER ECPrivateKey
format.
Sourcepub fn to_sec1_der(&self) -> Result<Zeroizing<Vec<u8>>>where
C: Curve + ProjectiveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldSize<C>: ModulusSize,
pub fn to_sec1_der(&self) -> Result<Zeroizing<Vec<u8>>>where
C: Curve + ProjectiveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldSize<C>: ModulusSize,
Serialize secret key in the SEC1 ASN.1 DER ECPrivateKey
format.
Sourcepub fn from_sec1_pem(s: &str) -> Result<Self>
pub fn from_sec1_pem(s: &str) -> Result<Self>
Parse SecretKey
from PEM-encoded SEC1 ECPrivateKey
format.
PEM-encoded SEC1 keys can be identified by the leading delimiter:
-----BEGIN EC PRIVATE KEY-----
Sourcepub fn to_pem(&self, line_ending: LineEnding) -> Result<Zeroizing<String>>where
C: Curve + ProjectiveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldSize<C>: ModulusSize,
pub fn to_pem(&self, line_ending: LineEnding) -> Result<Zeroizing<String>>where
C: Curve + ProjectiveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldSize<C>: ModulusSize,
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.
Trait Implementations§
Source§impl<C> ConstantTimeEq for SecretKey<C>where
C: Curve,
impl<C> ConstantTimeEq for SecretKey<C>where
C: Curve,
Source§impl<C> DecodePrivateKey for SecretKey<C>
impl<C> DecodePrivateKey for SecretKey<C>
Source§impl<C> EncodePrivateKey for SecretKey<C>where
C: Curve + AssociatedOid + ProjectiveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldSize<C>: ModulusSize,
impl<C> EncodePrivateKey for SecretKey<C>where
C: Curve + AssociatedOid + ProjectiveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldSize<C>: ModulusSize,
Source§fn to_pkcs8_der(&self) -> Result<SecretDocument>
fn to_pkcs8_der(&self) -> Result<SecretDocument>
SecretDocument
containing a PKCS#8-encoded private key.Source§fn to_pkcs8_pem(
&self,
line_ending: LineEnding,
) -> Result<Zeroizing<String>, Error>
fn to_pkcs8_pem( &self, line_ending: LineEnding, ) -> Result<Zeroizing<String>, Error>
LineEnding
.Source§impl<C> From<&NonZeroScalar<C>> for SecretKey<C>where
C: Curve + ProjectiveArithmetic,
impl<C> From<&NonZeroScalar<C>> for SecretKey<C>where
C: Curve + ProjectiveArithmetic,
Source§fn from(scalar: &NonZeroScalar<C>) -> SecretKey<C>
fn from(scalar: &NonZeroScalar<C>) -> SecretKey<C>
Source§impl<C> From<&SecretKey<C>> for NonZeroScalar<C>where
C: Curve + ScalarArithmetic,
impl<C> From<&SecretKey<C>> for NonZeroScalar<C>where
C: Curve + ScalarArithmetic,
Source§fn from(sk: &SecretKey<C>) -> NonZeroScalar<C>
fn from(sk: &SecretKey<C>) -> NonZeroScalar<C>
Source§impl<C> From<NonZeroScalar<C>> for SecretKey<C>where
C: Curve + ProjectiveArithmetic,
impl<C> From<NonZeroScalar<C>> for SecretKey<C>where
C: Curve + ProjectiveArithmetic,
Source§fn from(scalar: NonZeroScalar<C>) -> SecretKey<C>
fn from(scalar: NonZeroScalar<C>) -> SecretKey<C>
Source§impl<C> From<SecretKey<C>> for NonZeroScalar<C>where
C: Curve + ScalarArithmetic,
impl<C> From<SecretKey<C>> for NonZeroScalar<C>where
C: Curve + ScalarArithmetic,
Source§fn from(sk: SecretKey<C>) -> NonZeroScalar<C>
fn from(sk: SecretKey<C>) -> NonZeroScalar<C>
Source§impl<C> TryFrom<PrivateKeyInfo<'_>> for SecretKey<C>
impl<C> TryFrom<PrivateKeyInfo<'_>> for SecretKey<C>
impl<C: Curve> Eq for SecretKey<C>
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,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)§impl<T> DecodeEcPrivateKey for Twhere
T: DecodePrivateKey,
impl<T> DecodeEcPrivateKey for Twhere
T: DecodePrivateKey,
§fn from_sec1_der(private_key: &[u8]) -> Result<T, Error>
fn from_sec1_der(private_key: &[u8]) -> Result<T, Error>
§fn from_sec1_pem(s: &str) -> Result<Self, Error>
fn from_sec1_pem(s: &str) -> Result<Self, Error>
§impl<T> EncodeEcPrivateKey for Twhere
T: EncodePrivateKey,
impl<T> EncodeEcPrivateKey for Twhere
T: EncodePrivateKey,
§fn to_sec1_der(&self) -> Result<SecretDocument, Error>
fn to_sec1_der(&self) -> Result<SecretDocument, Error>
SecretDocument
containing a SEC1-encoded private key.§fn to_sec1_pem(
&self,
line_ending: LineEnding,
) -> Result<Zeroizing<String>, Error>
fn to_sec1_pem( &self, line_ending: LineEnding, ) -> Result<Zeroizing<String>, Error>
LineEnding
. Read more