pub trait Signature {
type PublicKey: Clone;
type SecretKey: Zeroize + Clone;
type SignatureData: Clone;
type KeyPair;
// Required methods
fn name() -> &'static str;
fn keypair<R>(rng: &mut R) -> Result<Self::KeyPair, Error>
where R: CryptoRng + RngCore;
fn public_key(keypair: &Self::KeyPair) -> Self::PublicKey;
fn secret_key(keypair: &Self::KeyPair) -> Self::SecretKey;
fn sign(
message: &[u8],
secret_key: &Self::SecretKey,
) -> Result<Self::SignatureData, Error>;
fn verify(
message: &[u8],
signature: &Self::SignatureData,
public_key: &Self::PublicKey,
) -> Result<(), Error>;
}Expand description
Core trait for digital signature algorithms
This trait defines the minimal interface that all signature algorithms
must implement. It intentionally does not require AsRef or AsMut
implementations for secret keys to prevent accidental key corruption.
§Type Safety
Secret keys are opaque types that cannot be directly manipulated as bytes. This prevents common security vulnerabilities where keys are accidentally modified or exposed.
§Example Implementation
See the implementation modules for examples of how to implement this trait for specific algorithms like Ed25519, ECDSA, etc.
Required Associated Types§
Sourcetype SecretKey: Zeroize + Clone
type SecretKey: Zeroize + Clone
Secret key type - must be zeroizable but not byte-accessible
§Security Note
This type should not implement AsMut<[u8]> to prevent corruption
of key material. Use explicit serialization methods if needed.
Sourcetype SignatureData: Clone
type SignatureData: Clone
Signature data type
Required Methods§
Sourcefn keypair<R>(rng: &mut R) -> Result<Self::KeyPair, Error>
fn keypair<R>(rng: &mut R) -> Result<Self::KeyPair, Error>
Generate a new key pair using the provided RNG
§Security Requirements
Implementations must use the provided cryptographically secure RNG for all random number generation.
Sourcefn public_key(keypair: &Self::KeyPair) -> Self::PublicKey
fn public_key(keypair: &Self::KeyPair) -> Self::PublicKey
Extract the public key from a key pair
Sourcefn secret_key(keypair: &Self::KeyPair) -> Self::SecretKey
fn secret_key(keypair: &Self::KeyPair) -> Self::SecretKey
Extract the secret key from a key pair
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Source§impl Signature for EcdsaP224
impl Signature for EcdsaP224
type PublicKey = EcdsaP224PublicKey
type SecretKey = EcdsaP224SecretKey
type SignatureData = EcdsaP224Signature
type KeyPair = (<EcdsaP224 as Signature>::PublicKey, <EcdsaP224 as Signature>::SecretKey)
fn name() -> &'static str
fn keypair<R>(rng: &mut R) -> Result<<EcdsaP224 as Signature>::KeyPair, Error>
fn public_key( keypair: &<EcdsaP224 as Signature>::KeyPair, ) -> <EcdsaP224 as Signature>::PublicKey
fn secret_key( keypair: &<EcdsaP224 as Signature>::KeyPair, ) -> <EcdsaP224 as Signature>::SecretKey
fn sign( message: &[u8], secret_key: &<EcdsaP224 as Signature>::SecretKey, ) -> Result<<EcdsaP224 as Signature>::SignatureData, Error>
fn verify( message: &[u8], signature: &<EcdsaP224 as Signature>::SignatureData, public_key: &<EcdsaP224 as Signature>::PublicKey, ) -> Result<(), Error>
Source§impl Signature for EcdsaP256
impl Signature for EcdsaP256
Source§fn keypair<R>(rng: &mut R) -> Result<<EcdsaP256 as Signature>::KeyPair, Error>
fn keypair<R>(rng: &mut R) -> Result<<EcdsaP256 as Signature>::KeyPair, Error>
Generate an ECDSA key pair
Generates a random private key d ∈ [1, n-1] and computes the corresponding public key Q = d·G where G is the base point.
Reference: FIPS 186-4, Appendix B.4.1
Source§fn sign(
message: &[u8],
secret_key: &<EcdsaP256 as Signature>::SecretKey,
) -> Result<<EcdsaP256 as Signature>::SignatureData, Error>
fn sign( message: &[u8], secret_key: &<EcdsaP256 as Signature>::SecretKey, ) -> Result<<EcdsaP256 as Signature>::SignatureData, Error>
Sign a message using ECDSA
Implements the ECDSA signature generation algorithm as specified in FIPS 186-4, Section 6.3, with deterministic nonce generation per RFC 6979.
Algorithm:
- e = HASH(M), where HASH is SHA-256
- z = the leftmost min(N, bitlen(e)) bits of e, where N = 256
- Generate k deterministically per RFC 6979
- (x₁, y₁) = k·G
- r = x₁ mod n; if r = 0, go back to step 3
- s = k⁻¹(z + rd) mod n; if s = 0, go back to step 3
- Return signature (r, s)
Source§fn verify(
message: &[u8],
signature: &<EcdsaP256 as Signature>::SignatureData,
public_key: &<EcdsaP256 as Signature>::PublicKey,
) -> Result<(), Error>
fn verify( message: &[u8], signature: &<EcdsaP256 as Signature>::SignatureData, public_key: &<EcdsaP256 as Signature>::PublicKey, ) -> Result<(), Error>
Verify an ECDSA signature
Implements the ECDSA signature verification algorithm as specified in FIPS 186-4, Section 6.4.
Algorithm:
- Verify that r and s are integers in [1, n-1]
- e = HASH(M), where HASH is SHA-256
- z = the leftmost min(N, bitlen(e)) bits of e, where N = 256
- w = s⁻¹ mod n
- u₁ = zw mod n and u₂ = rw mod n
- (x₁, y₁) = u₁·G + u₂·Q
- If (x₁, y₁) = O, reject the signature
- v = x₁ mod n
- Accept the signature if and only if v = r
type PublicKey = EcdsaP256PublicKey
type SecretKey = EcdsaP256SecretKey
type SignatureData = EcdsaP256Signature
type KeyPair = (<EcdsaP256 as Signature>::PublicKey, <EcdsaP256 as Signature>::SecretKey)
fn name() -> &'static str
fn public_key( keypair: &<EcdsaP256 as Signature>::KeyPair, ) -> <EcdsaP256 as Signature>::PublicKey
fn secret_key( keypair: &<EcdsaP256 as Signature>::KeyPair, ) -> <EcdsaP256 as Signature>::SecretKey
Source§impl Signature for EcdsaP384
impl Signature for EcdsaP384
Source§fn keypair<R>(rng: &mut R) -> Result<<EcdsaP384 as Signature>::KeyPair, Error>
fn keypair<R>(rng: &mut R) -> Result<<EcdsaP384 as Signature>::KeyPair, Error>
Generate an ECDSA key pair
Generates a random private key d ∈ [1, n-1] and computes the corresponding public key Q = d·G where G is the base point.
Reference: FIPS 186-4, Appendix B.4.1
Source§fn sign(
message: &[u8],
secret_key: &<EcdsaP384 as Signature>::SecretKey,
) -> Result<<EcdsaP384 as Signature>::SignatureData, Error>
fn sign( message: &[u8], secret_key: &<EcdsaP384 as Signature>::SecretKey, ) -> Result<<EcdsaP384 as Signature>::SignatureData, Error>
Sign a message using ECDSA
Implements the ECDSA signature generation algorithm as specified in FIPS 186-4, Section 6.3, with deterministic nonce generation per RFC 6979.
Algorithm:
- e = HASH(M), where HASH is SHA-384
- z = the leftmost min(N, bitlen(e)) bits of e, where N = 384
- Generate k deterministically per RFC 6979
- (x₁, y₁) = k·G
- r = x₁ mod n; if r = 0, go back to step 3
- s = k⁻¹(z + rd) mod n; if s = 0, go back to step 3
- Return signature (r, s)
Source§fn verify(
message: &[u8],
signature: &<EcdsaP384 as Signature>::SignatureData,
public_key: &<EcdsaP384 as Signature>::PublicKey,
) -> Result<(), Error>
fn verify( message: &[u8], signature: &<EcdsaP384 as Signature>::SignatureData, public_key: &<EcdsaP384 as Signature>::PublicKey, ) -> Result<(), Error>
Verify an ECDSA signature
Implements the ECDSA signature verification algorithm as specified in FIPS 186-4, Section 6.4.
Algorithm:
- Verify that r and s are integers in [1, n-1]
- e = HASH(M), where HASH is SHA-384
- z = the leftmost min(N, bitlen(e)) bits of e, where N = 384
- w = s⁻¹ mod n
- u₁ = zw mod n and u₂ = rw mod n
- (x₁, y₁) = u₁·G + u₂·Q
- If (x₁, y₁) = O, reject the signature
- v = x₁ mod n
- Accept the signature if and only if v = r
type PublicKey = EcdsaP384PublicKey
type SecretKey = EcdsaP384SecretKey
type SignatureData = EcdsaP384Signature
type KeyPair = (<EcdsaP384 as Signature>::PublicKey, <EcdsaP384 as Signature>::SecretKey)
fn name() -> &'static str
fn public_key( keypair: &<EcdsaP384 as Signature>::KeyPair, ) -> <EcdsaP384 as Signature>::PublicKey
fn secret_key( keypair: &<EcdsaP384 as Signature>::KeyPair, ) -> <EcdsaP384 as Signature>::SecretKey
Source§impl Signature for EcdsaP521
impl Signature for EcdsaP521
Source§fn keypair<R>(rng: &mut R) -> Result<<EcdsaP521 as Signature>::KeyPair, Error>
fn keypair<R>(rng: &mut R) -> Result<<EcdsaP521 as Signature>::KeyPair, Error>
Generate an ECDSA key pair
Generates a random private key d ∈ [1, n-1] and computes the corresponding public key Q = d·G where G is the base point.
Reference: FIPS 186-4, Appendix B.4.1
Source§fn sign(
message: &[u8],
secret_key: &<EcdsaP521 as Signature>::SecretKey,
) -> Result<<EcdsaP521 as Signature>::SignatureData, Error>
fn sign( message: &[u8], secret_key: &<EcdsaP521 as Signature>::SecretKey, ) -> Result<<EcdsaP521 as Signature>::SignatureData, Error>
Sign a message using ECDSA
Implements the ECDSA signature generation algorithm as specified in FIPS 186-4, Section 6.3, with deterministic nonce generation per RFC 6979.
Algorithm:
- e = HASH(M), where HASH is SHA-512
- z = the leftmost min(N, bitlen(e)) bits of e, where N = 521
- Generate k deterministically per RFC 6979
- (x₁, y₁) = k·G
- r = x₁ mod n; if r = 0, go back to step 3
- s = k⁻¹(z + rd) mod n; if s = 0, go back to step 3
- Return signature (r, s)
Source§fn verify(
message: &[u8],
signature: &<EcdsaP521 as Signature>::SignatureData,
public_key: &<EcdsaP521 as Signature>::PublicKey,
) -> Result<(), Error>
fn verify( message: &[u8], signature: &<EcdsaP521 as Signature>::SignatureData, public_key: &<EcdsaP521 as Signature>::PublicKey, ) -> Result<(), Error>
Verify an ECDSA signature
Implements the ECDSA signature verification algorithm as specified in FIPS 186-4, Section 6.4.
Algorithm:
- Verify that r and s are integers in [1, n-1]
- e = HASH(M), where HASH is SHA-512
- z = the leftmost min(N, bitlen(e)) bits of e, where N = 521
- w = s⁻¹ mod n
- u₁ = zw mod n and u₂ = rw mod n
- (x₁, y₁) = u₁·G + u₂·Q
- If (x₁, y₁) = O, reject the signature
- v = x₁ mod n
- Accept the signature if and only if v = r
type PublicKey = EcdsaP521PublicKey
type SecretKey = EcdsaP521SecretKey
type SignatureData = EcdsaP521Signature
type KeyPair = (<EcdsaP521 as Signature>::PublicKey, <EcdsaP521 as Signature>::SecretKey)
fn name() -> &'static str
fn public_key( keypair: &<EcdsaP521 as Signature>::KeyPair, ) -> <EcdsaP521 as Signature>::PublicKey
fn secret_key( keypair: &<EcdsaP521 as Signature>::KeyPair, ) -> <EcdsaP521 as Signature>::SecretKey
Source§impl Signature for Ed25519
impl Signature for Ed25519
Source§fn keypair<R>(rng: &mut R) -> Result<<Ed25519 as Signature>::KeyPair, Error>
fn keypair<R>(rng: &mut R) -> Result<<Ed25519 as Signature>::KeyPair, Error>
Generate a key from caller-provided cryptographic randomness.