Struct SigningKey

Source
pub struct SigningKey<C>{ /* private fields */ }
Available on crate feature signing only.
Expand description

ECDSA secret key used for signing. Generic over prime order elliptic curves (e.g. NIST P-curves)

Requires an elliptic_curve::CurveArithmetic impl on the curve, and a SignPrimitive impl on its associated Scalar type.

§Usage

The signature crate defines the following traits which are the primary API for signing:

See the p256 crate for examples of using this type with a concrete elliptic curve.

Implementations§

Source§

impl<C> SigningKey<C>

Source

pub fn sign_prehash_recoverable( &self, prehash: &[u8], ) -> Result<(Signature<C>, RecoveryId)>

Sign the given message prehash, returning a signature and recovery ID.

Source

pub fn sign_digest_recoverable<D>( &self, msg_digest: D, ) -> Result<(Signature<C>, RecoveryId)>
where D: Digest,

Sign the given message digest, returning a signature and recovery ID.

Source

pub fn sign_recoverable(&self, msg: &[u8]) -> Result<(Signature<C>, RecoveryId)>

Sign the given message, hashing it with the curve’s default digest function, and returning a signature and recovery ID.

Source§

impl<C> SigningKey<C>

Source

pub fn random(rng: &mut impl CryptoRngCore) -> Self

Generate a cryptographically random SigningKey.

Source

pub fn from_bytes(bytes: &FieldBytes<C>) -> Result<Self>

Initialize signing key from a raw scalar serialized as a byte array.

Source

pub fn from_slice(bytes: &[u8]) -> Result<Self>

Initialize signing key from a raw scalar serialized as a byte slice.

Source

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

Serialize this SigningKey as bytes

Source

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

Borrow the secret NonZeroScalar value for this key.

§⚠️ Warning

This value is key material.

Please treat it with the care it deserves!

Source

pub fn verifying_key(&self) -> &VerifyingKey<C>

Available on crate feature verifying only.

Get the VerifyingKey which corresponds to this SigningKey.

Trait Implementations§

Source§

impl<C> AsRef<VerifyingKey<C>> for SigningKey<C>

Available on crate feature verifying only.
Source§

fn as_ref(&self) -> &VerifyingKey<C>

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<C> AssociatedAlgorithmIdentifier for SigningKey<C>

Available on crate feature pkcs8 only.
Source§

const ALGORITHM_IDENTIFIER: AlgorithmIdentifier<ObjectIdentifier> = SecretKey<C>::ALGORITHM_IDENTIFIER

AlgorithmIdentifier for this structure.
Source§

type Params = ObjectIdentifier

Algorithm parameters.
Source§

impl<C> Clone for SigningKey<C>

Source§

fn clone(&self) -> SigningKey<C>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<C> ConstantTimeEq for SigningKey<C>

Source§

fn ct_eq(&self, other: &Self) -> Choice

Determine if two items are equal. Read more
Source§

fn ct_ne(&self, other: &Self) -> Choice

Determine if two items are NOT equal. Read more
Source§

impl<C> Debug for SigningKey<C>

Source§

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

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

impl<C, D> DigestSigner<D, (Signature<C>, RecoveryId)> for SigningKey<C>

Source§

fn try_sign_digest(&self, msg_digest: D) -> Result<(Signature<C>, RecoveryId)>

Attempt to sign the given prehashed message Digest, returning a digital signature on success, or an error if something went wrong.
Source§

fn sign_digest(&self, digest: D) -> S

Sign the given prehashed message Digest, returning a signature. Read more
Source§

impl<C, D> DigestSigner<D, Signature<C>> for SigningKey<C>

Sign message digest using a deterministic ephemeral scalar (k) computed using the algorithm described in RFC6979 § 3.2.

Source§

fn try_sign_digest(&self, msg_digest: D) -> Result<Signature<C>>

Attempt to sign the given prehashed message Digest, returning a digital signature on success, or an error if something went wrong.
Source§

fn sign_digest(&self, digest: D) -> S

Sign the given prehashed message Digest, returning a signature. Read more
Source§

impl<C, D> DigestSigner<D, SignatureWithOid<C>> for SigningKey<C>

Source§

fn try_sign_digest(&self, msg_digest: D) -> Result<SignatureWithOid<C>>

Attempt to sign the given prehashed message Digest, returning a digital signature on success, or an error if something went wrong.
Source§

fn sign_digest(&self, digest: D) -> S

Sign the given prehashed message Digest, returning a signature. Read more
Source§

impl<C> Drop for SigningKey<C>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<C> EncodePrivateKey for SigningKey<C>

Available on crate feature pem only.
Source§

fn to_pkcs8_der(&self) -> Result<SecretDocument>

Serialize a SecretDocument containing a PKCS#8-encoded private key.
Source§

fn to_pkcs8_pem( &self, line_ending: LineEnding, ) -> Result<Zeroizing<String>, Error>

Serialize this private key as PEM-encoded PKCS#8 with the given LineEnding.
Source§

fn write_pkcs8_der_file(&self, path: impl AsRef<Path>) -> Result<(), Error>

Write ASN.1 DER-encoded PKCS#8 private key to the given path
Source§

fn write_pkcs8_pem_file( &self, path: impl AsRef<Path>, line_ending: LineEnding, ) -> Result<(), Error>

Write ASN.1 DER-encoded PKCS#8 private key to the given path
Source§

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

Source§

fn from(secret_key: &SecretKey<C>) -> Self

Converts to this type from the input type.
Source§

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

Source§

fn from(secret_key: &SigningKey<C>) -> Self

Converts to this type from the input type.
Source§

impl<C> From<&SigningKey<C>> for VerifyingKey<C>

Available on crate feature verifying only.
Source§

fn from(signing_key: &SigningKey<C>) -> VerifyingKey<C>

Converts to this type from the input type.
Source§

impl<C> From<NonZeroScalar<C>> for SigningKey<C>

Source§

fn from(secret_scalar: NonZeroScalar<C>) -> Self

Converts to this type from the input type.
Source§

impl<C> From<SecretKey<C>> for SigningKey<C>

Source§

fn from(secret_key: SecretKey<C>) -> Self

Converts to this type from the input type.
Source§

impl<C> From<SigningKey<C>> for SecretKey<C>

Source§

fn from(key: SigningKey<C>) -> Self

Converts to this type from the input type.
Source§

impl<C> From<SigningKey<C>> for VerifyingKey<C>

Available on crate feature verifying only.
Source§

fn from(signing_key: SigningKey<C>) -> VerifyingKey<C>

Converts to this type from the input type.
Source§

impl<C> FromStr for SigningKey<C>

Available on crate feature pem only.
Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self>

Parses a string s to return a value of this type. Read more
Source§

impl<C> KeypairRef for SigningKey<C>

Available on crate feature verifying only.
Source§

type VerifyingKey = VerifyingKey<C>

Verifying key type for this keypair.
Source§

impl<C> PartialEq for SigningKey<C>

Source§

fn eq(&self, other: &SigningKey<C>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<C> PrehashSigner<(Signature<C>, RecoveryId)> for SigningKey<C>

Source§

fn sign_prehash(&self, prehash: &[u8]) -> Result<(Signature<C>, RecoveryId)>

Attempt to sign the given message digest, returning a digital signature on success, or an error if something went wrong. Read more
Source§

impl<C> PrehashSigner<Signature<C>> for SigningKey<C>

Sign message prehash using a deterministic ephemeral scalar (k) computed using the algorithm described in RFC6979 § 3.2.

Source§

fn sign_prehash(&self, prehash: &[u8]) -> Result<Signature<C>>

Attempt to sign the given message digest, returning a digital signature on success, or an error if something went wrong. Read more
Source§

impl<C> PrehashSigner<Signature<C>> for SigningKey<C>

Available on crate feature der only.
Source§

fn sign_prehash(&self, prehash: &[u8]) -> Result<Signature<C>>

Attempt to sign the given message digest, returning a digital signature on success, or an error if something went wrong. Read more
Source§

impl<C, D> RandomizedDigestSigner<D, Signature<C>> for SigningKey<C>

Source§

fn try_sign_digest_with_rng( &self, rng: &mut impl CryptoRngCore, msg_digest: D, ) -> Result<Signature<C>>

Attempt to sign the given prehashed message Digest, returning a digital signature on success, or an error if something went wrong.
Source§

fn sign_digest_with_rng(&self, rng: &mut impl CryptoRngCore, digest: D) -> S

Sign the given prehashed message Digest, returning a signature. Read more
Source§

impl<C, D> RandomizedDigestSigner<D, Signature<C>> for SigningKey<C>

Available on crate feature der only.
Source§

fn try_sign_digest_with_rng( &self, rng: &mut impl CryptoRngCore, msg_digest: D, ) -> Result<Signature<C>>

Attempt to sign the given prehashed message Digest, returning a digital signature on success, or an error if something went wrong.
Source§

fn sign_digest_with_rng(&self, rng: &mut impl CryptoRngCore, digest: D) -> S

Sign the given prehashed message Digest, returning a signature. Read more
Source§

impl<C> RandomizedPrehashSigner<Signature<C>> for SigningKey<C>

Source§

fn sign_prehash_with_rng( &self, rng: &mut impl CryptoRngCore, prehash: &[u8], ) -> Result<Signature<C>>

Attempt to sign the given message digest, returning a digital signature on success, or an error if something went wrong. Read more
Source§

impl<C> RandomizedPrehashSigner<Signature<C>> for SigningKey<C>

Available on crate feature der only.
Source§

fn sign_prehash_with_rng( &self, rng: &mut impl CryptoRngCore, prehash: &[u8], ) -> Result<Signature<C>>

Attempt to sign the given message digest, returning a digital signature on success, or an error if something went wrong. Read more
Source§

impl<C> RandomizedSigner<Signature<C>> for SigningKey<C>

Source§

fn try_sign_with_rng( &self, rng: &mut impl CryptoRngCore, msg: &[u8], ) -> Result<Signature<C>>

Attempt to sign the given message, returning a digital signature on success, or an error if something went wrong. Read more
Source§

fn sign_with_rng(&self, rng: &mut impl CryptoRngCore, msg: &[u8]) -> S

Sign the given message and return a digital signature
Source§

impl<C> RandomizedSigner<Signature<C>> for SigningKey<C>

Available on crate feature der only.
Source§

fn try_sign_with_rng( &self, rng: &mut impl CryptoRngCore, msg: &[u8], ) -> Result<Signature<C>>

Attempt to sign the given message, returning a digital signature on success, or an error if something went wrong. Read more
Source§

fn sign_with_rng(&self, rng: &mut impl CryptoRngCore, msg: &[u8]) -> S

Sign the given message and return a digital signature
Source§

impl<C> SignatureAlgorithmIdentifier for SigningKey<C>

Available on crate feature pkcs8 only.
Source§

const SIGNATURE_ALGORITHM_IDENTIFIER: AlgorithmIdentifier<Self::Params> = Signature<C>::ALGORITHM_IDENTIFIER

AlgorithmIdentifier for the corresponding singature system.
Source§

type Params = AnyRef<'static>

Algorithm parameters.
Source§

impl<C> Signer<(Signature<C>, RecoveryId)> for SigningKey<C>

Source§

fn try_sign(&self, msg: &[u8]) -> Result<(Signature<C>, RecoveryId)>

Attempt to sign the given message, returning a digital signature on success, or an error if something went wrong. Read more
Source§

fn sign(&self, msg: &[u8]) -> S

Sign the given message and return a digital signature
Source§

impl<C> Signer<Signature<C>> for SigningKey<C>

Sign message using a deterministic ephemeral scalar (k) computed using the algorithm described in RFC6979 § 3.2.

Source§

fn try_sign(&self, msg: &[u8]) -> Result<Signature<C>>

Attempt to sign the given message, returning a digital signature on success, or an error if something went wrong. Read more
Source§

fn sign(&self, msg: &[u8]) -> S

Sign the given message and return a digital signature
Source§

impl<C> Signer<Signature<C>> for SigningKey<C>

Available on crate feature der only.
Source§

fn try_sign(&self, msg: &[u8]) -> Result<Signature<C>>

Attempt to sign the given message, returning a digital signature on success, or an error if something went wrong. Read more
Source§

fn sign(&self, msg: &[u8]) -> S

Sign the given message and return a digital signature
Source§

impl<C> Signer<SignatureWithOid<C>> for SigningKey<C>

Source§

fn try_sign(&self, msg: &[u8]) -> Result<SignatureWithOid<C>>

Attempt to sign the given message, returning a digital signature on success, or an error if something went wrong. Read more
Source§

fn sign(&self, msg: &[u8]) -> S

Sign the given message and return a digital signature
Source§

impl<C> TryFrom<&[u8]> for SigningKey<C>

Source§

type Error = Error

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

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

Performs the conversion.
Source§

impl<C> TryFrom<PrivateKeyInfo<'_>> for SigningKey<C>

Available on crate feature pkcs8 only.
Source§

type Error = Error

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

fn try_from(private_key_info: PrivateKeyInfo<'_>) -> Result<Self>

Performs the conversion.
Source§

impl<C> Eq for SigningKey<C>

Constant-time comparison

Source§

impl<C> ZeroizeOnDrop for SigningKey<C>

Auto Trait Implementations§

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> DecodeEcPrivateKey for T
where T: for<'a> TryFrom<PrivateKeyInfo<'a>, Error = Error>,

Source§

fn from_sec1_der(private_key: &[u8]) -> Result<T, Error>

Deserialize SEC1 private key from ASN.1 DER-encoded data (binary format).
Source§

fn from_sec1_pem(s: &str) -> Result<Self, Error>

Deserialize SEC1-encoded private key from PEM. Read more
Source§

fn read_sec1_der_file(path: impl AsRef<Path>) -> Result<Self, Error>

Load SEC1 private key from an ASN.1 DER-encoded file on the local filesystem (binary format).
Source§

fn read_sec1_pem_file(path: impl AsRef<Path>) -> Result<Self, Error>

Load SEC1 private key from a PEM-encoded file on the local filesystem.
Source§

impl<T> DecodePrivateKey for T
where T: for<'a> TryFrom<PrivateKeyInfo<'a>, Error = Error>,

Source§

fn from_pkcs8_der(bytes: &[u8]) -> Result<T, Error>

Deserialize PKCS#8 private key from ASN.1 DER-encoded data (binary format).
Source§

fn from_pkcs8_pem(s: &str) -> Result<Self, Error>

Deserialize PKCS#8-encoded private key from PEM. Read more
Source§

fn read_pkcs8_der_file(path: impl AsRef<Path>) -> Result<Self, Error>

Load PKCS#8 private key from an ASN.1 DER-encoded file on the local filesystem (binary format).
Source§

fn read_pkcs8_pem_file(path: impl AsRef<Path>) -> Result<Self, Error>

Load PKCS#8 private key from a PEM-encoded file on the local filesystem.
Source§

impl<T> DynAssociatedAlgorithmIdentifier for T

Source§

fn algorithm_identifier(&self) -> Result<AlgorithmIdentifier<Any>, Error>

AlgorithmIdentifier for this structure.
Source§

impl<T> DynSignatureAlgorithmIdentifier for T

Source§

fn signature_algorithm_identifier( &self, ) -> Result<AlgorithmIdentifier<Any>, Error>

AlgorithmIdentifier for the corresponding singature system.
Source§

impl<T> EncodeEcPrivateKey for T

Source§

fn to_sec1_der(&self) -> Result<SecretDocument, Error>

Serialize a SecretDocument containing a SEC1-encoded private key.
Source§

fn to_sec1_pem( &self, line_ending: LineEnding, ) -> Result<Zeroizing<String>, Error>

Serialize this private key as PEM-encoded SEC1 with the given LineEnding. Read more
Source§

fn write_sec1_der_file(&self, path: impl AsRef<Path>) -> Result<(), Error>

Write ASN.1 DER-encoded SEC1 private key to the given path.
Source§

fn write_sec1_pem_file( &self, path: impl AsRef<Path>, line_ending: LineEnding, ) -> Result<(), Error>

Write ASN.1 DER-encoded SEC1 private key to the given path.
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<K> Keypair for K
where K: KeypairRef,

Source§

type VerifyingKey = <K as KeypairRef>::VerifyingKey

Verifying key type for this keypair.
Source§

fn verifying_key(&self) -> <K as Keypair>::VerifyingKey

Get the verifying key which can verify signatures produced by the signing key portion of this keypair.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<S, T> SignerMut<S> for T
where T: Signer<S>,

Source§

fn try_sign(&mut self, msg: &[u8]) -> Result<S, Error>

Attempt to sign the given message, updating the state, and returning a digital signature on success, or an error if something went wrong. Read more
Source§

fn sign(&mut self, msg: &[u8]) -> S

Sign the given message, update the state, and return a digital signature.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.