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: &mut impl CryptoRngCore) -> Selfwhere
C: CurveArithmetic,
pub fn random(rng: &mut impl CryptoRngCore) -> Selfwhere
C: CurveArithmetic,
Generate a random SecretKey
.
sourcepub fn new(scalar: ScalarPrimitive<C>) -> Self
pub fn new(scalar: ScalarPrimitive<C>) -> Self
Create a new secret key from a scalar value.
sourcepub 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!
sourcepub 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!
sourcepub 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
sourcepub fn from_bytes(bytes: &FieldBytes<C>) -> Result<Self>
pub fn from_bytes(bytes: &FieldBytes<C>) -> Result<Self>
Deserialize secret key from an encoded secret scalar.
sourcepub fn from_slice(slice: &[u8]) -> Result<Self>
pub fn from_slice(slice: &[u8]) -> Result<Self>
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.
sourcepub fn to_bytes(&self) -> FieldBytes<C>
pub fn to_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: CurveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldBytesSize<C>: ModulusSize,
pub fn to_sec1_der(&self) -> Result<Zeroizing<Vec<u8>>>where
C: CurveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldBytesSize<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_sec1_pem(&self, line_ending: LineEnding) -> Result<Zeroizing<String>>where
C: CurveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldBytesSize<C>: ModulusSize,
pub fn to_sec1_pem(&self, line_ending: LineEnding) -> Result<Zeroizing<String>>where
C: CurveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldBytesSize<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> AssociatedAlgorithmIdentifier for SecretKey<C>where
C: AssociatedOid + Curve,
impl<C> AssociatedAlgorithmIdentifier for SecretKey<C>where
C: AssociatedOid + Curve,
source§const ALGORITHM_IDENTIFIER: AlgorithmIdentifier<ObjectIdentifier> = _
const ALGORITHM_IDENTIFIER: AlgorithmIdentifier<ObjectIdentifier> = _
AlgorithmIdentifier
for this structure.source§type Params = ObjectIdentifier
type Params = ObjectIdentifier
source§impl<C> ConstantTimeEq for SecretKey<C>where
C: Curve,
impl<C> ConstantTimeEq for SecretKey<C>where
C: Curve,
source§impl<C> EncodePrivateKey for SecretKey<C>where
C: AssociatedOid + CurveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldBytesSize<C>: ModulusSize,
impl<C> EncodePrivateKey for SecretKey<C>where
C: AssociatedOid + CurveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldBytesSize<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§fn write_pkcs8_der_file(&self, path: impl AsRef<Path>) -> Result<(), Error>
fn write_pkcs8_der_file(&self, path: impl AsRef<Path>) -> Result<(), Error>
source§fn write_pkcs8_pem_file(
&self,
path: impl AsRef<Path>,
line_ending: LineEnding,
) -> Result<(), Error>
fn write_pkcs8_pem_file( &self, path: impl AsRef<Path>, line_ending: LineEnding, ) -> Result<(), Error>
source§impl<C> From<&NonZeroScalar<C>> for SecretKey<C>where
C: CurveArithmetic,
impl<C> From<&NonZeroScalar<C>> for SecretKey<C>where
C: CurveArithmetic,
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: CurveArithmetic,
impl<C> From<&SecretKey<C>> for NonZeroScalar<C>where
C: CurveArithmetic,
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: CurveArithmetic,
impl<C> From<NonZeroScalar<C>> for SecretKey<C>where
C: CurveArithmetic,
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: CurveArithmetic,
impl<C> From<SecretKey<C>> for NonZeroScalar<C>where
C: CurveArithmetic,
source§fn from(sk: SecretKey<C>) -> NonZeroScalar<C>
fn from(sk: SecretKey<C>) -> NonZeroScalar<C>
source§impl<C> TryFrom<EcPrivateKey<'_>> for SecretKey<C>
impl<C> TryFrom<EcPrivateKey<'_>> for SecretKey<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
)source§impl<T> DecodeEcPrivateKey for T
impl<T> DecodeEcPrivateKey for T
source§fn from_sec1_der(private_key: &[u8]) -> Result<T, Error>
fn from_sec1_der(private_key: &[u8]) -> Result<T, Error>
source§fn from_sec1_pem(s: &str) -> Result<Self, Error>
fn from_sec1_pem(s: &str) -> Result<Self, Error>
source§impl<T> DecodePrivateKey for T
impl<T> DecodePrivateKey for T
source§fn from_pkcs8_der(bytes: &[u8]) -> Result<T, Error>
fn from_pkcs8_der(bytes: &[u8]) -> Result<T, Error>
source§fn from_pkcs8_pem(s: &str) -> Result<Self, Error>
fn from_pkcs8_pem(s: &str) -> Result<Self, Error>
source§impl<T> DynAssociatedAlgorithmIdentifier for Twhere
T: AssociatedAlgorithmIdentifier,
impl<T> DynAssociatedAlgorithmIdentifier for Twhere
T: AssociatedAlgorithmIdentifier,
source§fn algorithm_identifier(&self) -> Result<AlgorithmIdentifier<Any>, Error>
fn algorithm_identifier(&self) -> Result<AlgorithmIdentifier<Any>, Error>
AlgorithmIdentifier
for this structure.source§impl<T> EncodeEcPrivateKey for Twhere
T: EncodePrivateKey,
impl<T> EncodeEcPrivateKey for Twhere
T: EncodePrivateKey,
source§fn to_sec1_der(&self) -> Result<SecretDocument, Error>
fn to_sec1_der(&self) -> Result<SecretDocument, Error>
SecretDocument
containing a SEC1-encoded private key.source§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