Skip to main content

Signature

Trait Signature 

Source
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§

Source

type PublicKey: Clone

Public key type for this algorithm

Source

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.

Source

type SignatureData: Clone

Signature data type

Source

type KeyPair

Key pair type (typically a tuple of public and secret keys)

Required Methods§

Source

fn name() -> &'static str

Returns the name of this signature algorithm

Source

fn keypair<R>(rng: &mut R) -> Result<Self::KeyPair, Error>
where R: CryptoRng + RngCore,

Generate a new key pair using the provided RNG

§Security Requirements

Implementations must use the provided cryptographically secure RNG for all random number generation.

Source

fn public_key(keypair: &Self::KeyPair) -> Self::PublicKey

Extract the public key from a key pair

Source

fn secret_key(keypair: &Self::KeyPair) -> Self::SecretKey

Extract the secret key from a key pair

Source

fn sign( message: &[u8], secret_key: &Self::SecretKey, ) -> Result<Self::SignatureData, Error>

Sign a message with the given secret key

§Security Requirements
  • Implementations should be deterministic when possible (e.g., Ed25519)
  • Must not leak information about the secret key through timing
Source

fn verify( message: &[u8], signature: &Self::SignatureData, public_key: &Self::PublicKey, ) -> Result<(), Error>

Verify a signature against a message and public key

§Security Requirements
  • Must be constant-time with respect to the signature value
  • Should validate all inputs before processing

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

Source§

impl Signature for EcdsaP256

Source§

fn keypair<R>(rng: &mut R) -> Result<<EcdsaP256 as Signature>::KeyPair, Error>
where R: CryptoRng + RngCore,

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>

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:

  1. e = HASH(M), where HASH is SHA-256
  2. z = the leftmost min(N, bitlen(e)) bits of e, where N = 256
  3. Generate k deterministically per RFC 6979
  4. (x₁, y₁) = k·G
  5. r = x₁ mod n; if r = 0, go back to step 3
  6. s = k⁻¹(z + rd) mod n; if s = 0, go back to step 3
  7. Return signature (r, s)
Source§

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:

  1. Verify that r and s are integers in [1, n-1]
  2. e = HASH(M), where HASH is SHA-256
  3. z = the leftmost min(N, bitlen(e)) bits of e, where N = 256
  4. w = s⁻¹ mod n
  5. u₁ = zw mod n and u₂ = rw mod n
  6. (x₁, y₁) = u₁·G + u₂·Q
  7. If (x₁, y₁) = O, reject the signature
  8. v = x₁ mod n
  9. Accept the signature if and only if v = r
Source§

type PublicKey = EcdsaP256PublicKey

Source§

type SecretKey = EcdsaP256SecretKey

Source§

type SignatureData = EcdsaP256Signature

Source§

type KeyPair = (<EcdsaP256 as Signature>::PublicKey, <EcdsaP256 as Signature>::SecretKey)

Source§

fn name() -> &'static str

Source§

fn public_key( keypair: &<EcdsaP256 as Signature>::KeyPair, ) -> <EcdsaP256 as Signature>::PublicKey

Source§

fn secret_key( keypair: &<EcdsaP256 as Signature>::KeyPair, ) -> <EcdsaP256 as Signature>::SecretKey

Source§

impl Signature for EcdsaP384

Source§

fn keypair<R>(rng: &mut R) -> Result<<EcdsaP384 as Signature>::KeyPair, Error>
where R: CryptoRng + RngCore,

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>

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:

  1. e = HASH(M), where HASH is SHA-384
  2. z = the leftmost min(N, bitlen(e)) bits of e, where N = 384
  3. Generate k deterministically per RFC 6979
  4. (x₁, y₁) = k·G
  5. r = x₁ mod n; if r = 0, go back to step 3
  6. s = k⁻¹(z + rd) mod n; if s = 0, go back to step 3
  7. Return signature (r, s)
Source§

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:

  1. Verify that r and s are integers in [1, n-1]
  2. e = HASH(M), where HASH is SHA-384
  3. z = the leftmost min(N, bitlen(e)) bits of e, where N = 384
  4. w = s⁻¹ mod n
  5. u₁ = zw mod n and u₂ = rw mod n
  6. (x₁, y₁) = u₁·G + u₂·Q
  7. If (x₁, y₁) = O, reject the signature
  8. v = x₁ mod n
  9. Accept the signature if and only if v = r
Source§

type PublicKey = EcdsaP384PublicKey

Source§

type SecretKey = EcdsaP384SecretKey

Source§

type SignatureData = EcdsaP384Signature

Source§

type KeyPair = (<EcdsaP384 as Signature>::PublicKey, <EcdsaP384 as Signature>::SecretKey)

Source§

fn name() -> &'static str

Source§

fn public_key( keypair: &<EcdsaP384 as Signature>::KeyPair, ) -> <EcdsaP384 as Signature>::PublicKey

Source§

fn secret_key( keypair: &<EcdsaP384 as Signature>::KeyPair, ) -> <EcdsaP384 as Signature>::SecretKey

Source§

impl Signature for EcdsaP521

Source§

fn keypair<R>(rng: &mut R) -> Result<<EcdsaP521 as Signature>::KeyPair, Error>
where R: CryptoRng + RngCore,

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>

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:

  1. e = HASH(M), where HASH is SHA-512
  2. z = the leftmost min(N, bitlen(e)) bits of e, where N = 521
  3. Generate k deterministically per RFC 6979
  4. (x₁, y₁) = k·G
  5. r = x₁ mod n; if r = 0, go back to step 3
  6. s = k⁻¹(z + rd) mod n; if s = 0, go back to step 3
  7. Return signature (r, s)
Source§

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:

  1. Verify that r and s are integers in [1, n-1]
  2. e = HASH(M), where HASH is SHA-512
  3. z = the leftmost min(N, bitlen(e)) bits of e, where N = 521
  4. w = s⁻¹ mod n
  5. u₁ = zw mod n and u₂ = rw mod n
  6. (x₁, y₁) = u₁·G + u₂·Q
  7. If (x₁, y₁) = O, reject the signature
  8. v = x₁ mod n
  9. Accept the signature if and only if v = r
Source§

type PublicKey = EcdsaP521PublicKey

Source§

type SecretKey = EcdsaP521SecretKey

Source§

type SignatureData = EcdsaP521Signature

Source§

type KeyPair = (<EcdsaP521 as Signature>::PublicKey, <EcdsaP521 as Signature>::SecretKey)

Source§

fn name() -> &'static str

Source§

fn public_key( keypair: &<EcdsaP521 as Signature>::KeyPair, ) -> <EcdsaP521 as Signature>::PublicKey

Source§

fn secret_key( keypair: &<EcdsaP521 as Signature>::KeyPair, ) -> <EcdsaP521 as Signature>::SecretKey

Source§

impl Signature for Ed25519

Source§

fn keypair<R>(rng: &mut R) -> Result<<Ed25519 as Signature>::KeyPair, Error>
where R: CryptoRng + RngCore,

Generate a key from caller-provided cryptographic randomness.

Source§

type PublicKey = Ed25519PublicKey

Source§

type SecretKey = Ed25519SecretKey

Source§

type SignatureData = Ed25519Signature

Source§

type KeyPair = (<Ed25519 as Signature>::PublicKey, <Ed25519 as Signature>::SecretKey)

Source§

fn name() -> &'static str

Source§

fn public_key( keypair: &<Ed25519 as Signature>::KeyPair, ) -> <Ed25519 as Signature>::PublicKey

Source§

fn secret_key( keypair: &<Ed25519 as Signature>::KeyPair, ) -> <Ed25519 as Signature>::SecretKey

Source§

fn sign( message: &[u8], secret_key: &<Ed25519 as Signature>::SecretKey, ) -> Result<<Ed25519 as Signature>::SignatureData, Error>

Source§

fn verify( message: &[u8], signature: &<Ed25519 as Signature>::SignatureData, public_key: &<Ed25519 as Signature>::PublicKey, ) -> Result<(), Error>

Implementors§