aws_lc_rs/
signature.rs

1// Copyright 2015-2017 Brian Smith.
2// SPDX-License-Identifier: ISC
3// Modifications copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4// SPDX-License-Identifier: Apache-2.0 OR ISC
5
6//! Public key signatures: signing and verification.
7//!
8//! Use the `verify` function to verify signatures, passing a reference to the
9//! algorithm that identifies the algorithm. See the documentation for `verify`
10//! for examples.
11//!
12//! For signature verification, this API treats each combination of parameters
13//! as a separate algorithm. For example, instead of having a single "RSA"
14//! algorithm with a verification function that takes a bunch of parameters,
15//! there are `RSA_PKCS1_2048_8192_SHA256`, `RSA_PKCS1_2048_8192_SHA384`, etc.,
16//! which encode sets of parameter choices into objects. This is designed to
17//! reduce the risks of algorithm agility and to provide consistency with ECDSA
18//! and `EdDSA`.
19//!
20//! Currently this module does not support digesting the message to be signed
21//! separately from the public key operation, as it is currently being
22//! optimized for Ed25519 and for the implementation of protocols that do not
23//! requiring signing large messages. An interface for efficiently supporting
24//! larger messages may be added later.
25//!
26//!
27//! # Algorithm Details
28//!
29//! ## `ECDSA_*_ASN1` Details: ASN.1-encoded ECDSA Signatures
30//!
31//! The signature is a ASN.1 DER-encoded `Ecdsa-Sig-Value` as described in
32//! [RFC 3279 Section 2.2.3]. This is the form of ECDSA signature used in
33//! X.509-related structures and in TLS's `ServerKeyExchange` messages.
34//!
35//! The public key is encoding in uncompressed form using the
36//! Octet-String-to-Elliptic-Curve-Point algorithm in
37//! [SEC 1: Elliptic Curve Cryptography, Version 2.0].
38//!
39//! During verification, the public key is validated using the ECC Partial
40//! Public-Key Validation Routine from Section 5.6.2.3.3 of
41//! [NIST Special Publication 800-56A, revision 2] and Appendix A.3 of the
42//! NSA's [Suite B implementer's guide to FIPS 186-3]. Note that, as explained
43//! in the NSA guide, ECC Partial Public-Key Validation is equivalent to ECC
44//! Full Public-Key Validation for prime-order curves like this one.
45//!
46//! ## `ECDSA_*_FIXED` Details: Fixed-length (PKCS#11-style) ECDSA Signatures
47//!
48//! The signature is *r*||*s*, where || denotes concatenation, and where both
49//! *r* and *s* are both big-endian-encoded values that are left-padded to the
50//! maximum length. A P-256 signature will be 64 bytes long (two 32-byte
51//! components) and a P-384 signature will be 96 bytes long (two 48-byte
52//! components). This is the form of ECDSA signature used PKCS#11 and DNSSEC.
53//!
54//! The public key is encoding in uncompressed form using the
55//! Octet-String-to-Elliptic-Curve-Point algorithm in
56//! [SEC 1: Elliptic Curve Cryptography, Version 2.0].
57//!
58//! During verification, the public key is validated using the ECC Partial
59//! Public-Key Validation Routine from Section 5.6.2.3.3 of
60//! [NIST Special Publication 800-56A, revision 2] and Appendix A.3 of the
61//! NSA's [Suite B implementer's guide to FIPS 186-3]. Note that, as explained
62//! in the NSA guide, ECC Partial Public-Key Validation is equivalent to ECC
63//! Full Public-Key Validation for prime-order curves like this one.
64//!
65//! ## `RSA_PKCS1_*` Details: RSA PKCS#1 1.5 Signatures
66//!
67//! The signature is an RSASSA-PKCS1-v1_5 signature as described in
68//! [RFC 3447 Section 8.2].
69//!
70//! The public key is encoded as an ASN.1 `RSAPublicKey` as described in
71//! [RFC 3447 Appendix-A.1.1]. The public key modulus length, rounded *up* to
72//! the nearest (larger) multiple of 8 bits, must be in the range given in the
73//! name of the algorithm. The public exponent must be an odd integer of 2-33
74//! bits, inclusive.
75//!
76//!
77//! ## `RSA_PSS_*` Details: RSA PSS Signatures
78//!
79//! The signature is an RSASSA-PSS signature as described in
80//! [RFC 3447 Section 8.1].
81//!
82//! The public key is encoded as an ASN.1 `RSAPublicKey` as described in
83//! [RFC 3447 Appendix-A.1.1]. The public key modulus length, rounded *up* to
84//! the nearest (larger) multiple of 8 bits, must be in the range given in the
85//! name of the algorithm. The public exponent must be an odd integer of 2-33
86//! bits, inclusive.
87//!
88//! During verification, signatures will only be accepted if the MGF1 digest
89//! algorithm is the same as the message digest algorithm and if the salt
90//! length is the same length as the message digest. This matches the
91//! requirements in TLS 1.3 and other recent specifications.
92//!
93//! During signing, the message digest algorithm will be used as the MGF1
94//! digest algorithm. The salt will be the same length as the message digest.
95//! This matches the requirements in TLS 1.3 and other recent specifications.
96//! Additionally, the entire salt is randomly generated separately for each
97//! signature using the secure random number generator passed to `sign()`.
98//!
99//!
100//! [SEC 1: Elliptic Curve Cryptography, Version 2.0]:
101//!     http://www.secg.org/sec1-v2.pdf
102//! [NIST Special Publication 800-56A, revision 2]:
103//!     http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-56Ar2.pdf
104//! [Suite B implementer's guide to FIPS 186-3]:
105//!     https://github.com/briansmith/ring/blob/main/doc/ecdsa.pdf
106//! [RFC 3279 Section 2.2.3]:
107//!     https://tools.ietf.org/html/rfc3279#section-2.2.3
108//! [RFC 3447 Section 8.2]:
109//!     https://tools.ietf.org/html/rfc3447#section-7.2
110//! [RFC 3447 Section 8.1]:
111//!     https://tools.ietf.org/html/rfc3447#section-8.1
112//! [RFC 3447 Appendix-A.1.1]:
113//!     https://tools.ietf.org/html/rfc3447#appendix-A.1.1
114//!
115//!
116//! # Examples
117//!
118//! ## Signing and verifying with Ed25519
119//!
120//! ```
121//! use aws_lc_rs::{
122//!     rand,
123//!     signature::{self, KeyPair},
124//! };
125//!
126//! fn main() -> Result<(), aws_lc_rs::error::Unspecified> {
127//!     // Generate a new key pair for Ed25519.
128//!     let key_pair = signature::Ed25519KeyPair::generate()?;
129//!
130//!     // Sign the message "hello, world".
131//!     const MESSAGE: &[u8] = b"hello, world";
132//!     let sig = key_pair.sign(MESSAGE);
133//!
134//!     // Normally an application would extract the bytes of the signature and
135//!     // send them in a protocol message to the peer(s). Here we just get the
136//!     // public key key directly from the key pair.
137//!     let peer_public_key_bytes = key_pair.public_key().as_ref();
138//!
139//!     // Verify the signature of the message using the public key. Normally the
140//!     // verifier of the message would parse the inputs to this code out of the
141//!     // protocol message(s) sent by the signer.
142//!     let peer_public_key =
143//!         signature::UnparsedPublicKey::new(&signature::ED25519, peer_public_key_bytes);
144//!     peer_public_key.verify(MESSAGE, sig.as_ref())?;
145//!
146//!     Ok(())
147//! }
148//! ```
149//!
150//! ## Signing and verifying with RSA (PKCS#1 1.5 padding)
151//!
152//! By default OpenSSL writes RSA public keys in `SubjectPublicKeyInfo` format,
153//! not `RSAPublicKey` format, and Base64-encodes them (“PEM” format).
154//!
155//! To convert the PEM `SubjectPublicKeyInfo` format (“BEGIN PUBLIC KEY”) to the
156//! binary `RSAPublicKey` format needed by `verify()`, use:
157//!
158//! ```sh
159//! openssl rsa -pubin \
160//!             -in public_key.pem \
161//!             -inform PEM \
162//!             -RSAPublicKey_out \
163//!             -outform DER \
164//!             -out public_key.der
165//! ```
166//!
167//! To extract the RSAPublicKey-formatted public key from an ASN.1 (binary)
168//! DER-encoded `RSAPrivateKey` format private key file, use:
169//!
170//! ```sh
171//! openssl rsa -in private_key.der \
172//!             -inform DER \
173//!             -RSAPublicKey_out \
174//!             -outform DER \
175//!             -out public_key.der
176//! ```
177//!
178//! ```
179//! use aws_lc_rs::{rand, signature};
180//!
181//! fn sign_and_verify_rsa(
182//!     private_key_path: &std::path::Path,
183//!     public_key_path: &std::path::Path,
184//! ) -> Result<(), MyError> {
185//!     // Create an `RsaKeyPair` from the DER-encoded bytes. This example uses
186//!     // a 2048-bit key, but larger keys are also supported.
187//!     let private_key_der = read_file(private_key_path)?;
188//!     let key_pair = signature::RsaKeyPair::from_der(&private_key_der)
189//!         .map_err(|_| MyError::BadPrivateKey)?;
190//!
191//!     // Sign the message "hello, world", using PKCS#1 v1.5 padding and the
192//!     // SHA256 digest algorithm.
193//!     const MESSAGE: &'static [u8] = b"hello, world";
194//!     let rng = rand::SystemRandom::new();
195//!     let mut signature = vec![0; key_pair.public_modulus_len()];
196//!     key_pair
197//!         .sign(&signature::RSA_PKCS1_SHA256, &rng, MESSAGE, &mut signature)
198//!         .map_err(|_| MyError::OOM)?;
199//!
200//!     // Verify the signature.
201//!     let public_key = signature::UnparsedPublicKey::new(
202//!         &signature::RSA_PKCS1_2048_8192_SHA256,
203//!         read_file(public_key_path)?,
204//!     );
205//!     public_key
206//!         .verify(MESSAGE, &signature)
207//!         .map_err(|_| MyError::BadSignature)
208//! }
209//!
210//! #[derive(Debug)]
211//! enum MyError {
212//!     IO(std::io::Error),
213//!     BadPrivateKey,
214//!     OOM,
215//!     BadSignature,
216//! }
217//!
218//! fn read_file(path: &std::path::Path) -> Result<Vec<u8>, MyError> {
219//!     use std::io::Read;
220//!
221//!     let mut file = std::fs::File::open(path).map_err(|e| MyError::IO(e))?;
222//!     let mut contents: Vec<u8> = Vec::new();
223//!     file.read_to_end(&mut contents)
224//!         .map_err(|e| MyError::IO(e))?;
225//!     Ok(contents)
226//! }
227//!
228//! fn main() {
229//!     let private_key_path =
230//!         std::path::Path::new("tests/data/signature_rsa_example_private_key.der");
231//!     let public_key_path =
232//!         std::path::Path::new("tests/data/signature_rsa_example_public_key.der");
233//!     sign_and_verify_rsa(&private_key_path, &public_key_path).unwrap()
234//! }
235//! ```
236use crate::aws_lc::EVP_PKEY;
237pub use crate::rsa::signature::{RsaEncoding, RsaSignatureEncoding};
238pub use crate::rsa::{
239    KeyPair as RsaKeyPair, PublicKey as RsaSubjectPublicKey,
240    PublicKeyComponents as RsaPublicKeyComponents, RsaParameters,
241};
242use core::fmt::{Debug, Formatter};
243use std::any::{Any, TypeId};
244#[cfg(feature = "ring-sig-verify")]
245use untrusted::Input;
246
247use crate::rsa::signature::RsaSigningAlgorithmId;
248use crate::rsa::RsaVerificationAlgorithmId;
249
250pub use crate::ec::key_pair::{EcdsaKeyPair, PrivateKey as EcdsaPrivateKey};
251use crate::ec::signature::EcdsaSignatureFormat;
252pub use crate::ec::signature::{
253    EcdsaSigningAlgorithm, EcdsaVerificationAlgorithm, PublicKey as EcdsaPublicKey,
254};
255pub use crate::ed25519::{
256    Ed25519KeyPair, EdDSAParameters, PublicKey as Ed25519PublicKey, Seed as Ed25519Seed,
257    ED25519_PUBLIC_KEY_LEN,
258};
259
260use crate::digest::Digest;
261use crate::ec::encoding::parse_ec_public_key;
262use crate::ed25519::parse_ed25519_public_key;
263use crate::error::KeyRejected;
264#[cfg(all(feature = "unstable", not(feature = "fips")))]
265use crate::pqdsa::{parse_pqdsa_public_key, signature::PqdsaVerificationAlgorithm};
266use crate::ptr::LcPtr;
267use crate::rsa::key::parse_rsa_public_key;
268use crate::{digest, ec, error, hex, rsa, sealed};
269
270/// The longest signature is for ML-DSA-87
271pub(crate) const MAX_LEN: usize = 4627;
272
273/// A public key signature returned from a signing operation.
274#[derive(Clone, Copy)]
275pub struct Signature {
276    value: [u8; MAX_LEN],
277    len: usize,
278}
279
280impl Signature {
281    // Panics if `value` is too long.
282    pub(crate) fn new<F>(fill: F) -> Self
283    where
284        F: FnOnce(&mut [u8; MAX_LEN]) -> usize,
285    {
286        let mut r = Self {
287            value: [0; MAX_LEN],
288            len: 0,
289        };
290        r.len = fill(&mut r.value);
291        r
292    }
293}
294
295impl AsRef<[u8]> for Signature {
296    #[inline]
297    fn as_ref(&self) -> &[u8] {
298        &self.value[..self.len]
299    }
300}
301
302/// Key pairs for signing messages (private key and public key).
303pub trait KeyPair: Debug + Send + Sized + Sync {
304    /// The type of the public key.
305    type PublicKey: AsRef<[u8]> + Debug + Clone + Send + Sized + Sync;
306
307    /// The public key for the key pair.
308    fn public_key(&self) -> &Self::PublicKey;
309}
310
311// Private trait
312pub(crate) trait ParsedVerificationAlgorithm: Debug + Sync {
313    fn parsed_verify_sig(
314        &self,
315        public_key: &ParsedPublicKey,
316        msg: &[u8],
317        signature: &[u8],
318    ) -> Result<(), error::Unspecified>;
319
320    fn parsed_verify_digest_sig(
321        &self,
322        public_key: &ParsedPublicKey,
323        digest: &Digest,
324        signature: &[u8],
325    ) -> Result<(), error::Unspecified>;
326}
327
328/// A signature verification algorithm.
329pub trait VerificationAlgorithm: Debug + Sync + Any + sealed::Sealed {
330    /// Verify the signature `signature` of message `msg` with the public key
331    /// `public_key`.
332    ///
333    // # FIPS
334    // The following conditions must be met:
335    // * RSA Key Sizes: 1024, 2048, 3072, 4096
336    // * NIST Elliptic Curves: P256, P384, P521
337    // * Digest Algorithms: SHA1, SHA256, SHA384, SHA512
338    //
339    /// # Errors
340    /// `error::Unspecified` if inputs not verified.
341    #[cfg(feature = "ring-sig-verify")]
342    #[deprecated(note = "please use `VerificationAlgorithm::verify_sig` instead")]
343    fn verify(
344        &self,
345        public_key: Input<'_>,
346        msg: Input<'_>,
347        signature: Input<'_>,
348    ) -> Result<(), error::Unspecified>;
349
350    /// Verify the signature `signature` of message `msg` with the public key
351    /// `public_key`.
352    ///
353    // # FIPS
354    // The following conditions must be met:
355    // * RSA Key Sizes: 1024, 2048, 3072, 4096
356    // * NIST Elliptic Curves: P256, P384, P521
357    // * Digest Algorithms: SHA1, SHA256, SHA384, SHA512
358    //
359    /// # Errors
360    /// `error::Unspecified` if inputs not verified.
361    fn verify_sig(
362        &self,
363        public_key: &[u8],
364        msg: &[u8],
365        signature: &[u8],
366    ) -> Result<(), error::Unspecified>;
367
368    /// Verify the signature `signature` of `digest` with the `public_key`.
369    ///
370    // # FIPS
371    // Not approved.
372    //
373    /// # Errors
374    /// `error::Unspecified` if inputs not verified.
375    fn verify_digest_sig(
376        &self,
377        public_key: &[u8],
378        digest: &Digest,
379        signature: &[u8],
380    ) -> Result<(), error::Unspecified>;
381}
382
383/// An unparsed, possibly malformed, public key for signature verification.
384#[derive(Clone)]
385pub struct UnparsedPublicKey<B: AsRef<[u8]>> {
386    algorithm: &'static dyn VerificationAlgorithm,
387    bytes: B,
388}
389/// A parsed public key for signature verification.
390///
391/// A `ParsedPublicKey` can be created in two ways:
392/// - Directly from public key bytes using [`ParsedPublicKey::new`]
393/// - By parsing an `UnparsedPublicKey` using [`UnparsedPublicKey::parse`]
394///
395/// This pre-validates the public key format and stores the parsed key material,
396/// allowing for more efficient signature verification operations compared to
397/// parsing the key on each verification.
398///
399/// See the [`crate::signature`] module-level documentation for examples.
400pub struct ParsedPublicKey {
401    algorithm: &'static dyn VerificationAlgorithm,
402    parsed_algorithm: &'static dyn ParsedVerificationAlgorithm,
403    key: LcPtr<EVP_PKEY>,
404}
405
406impl ParsedPublicKey {
407    /// Creates a new `ParsedPublicKey` directly from public key bytes.
408    ///
409    /// This method validates the public key format and creates a `ParsedPublicKey`
410    /// that can be used for efficient signature verification operations.
411    ///
412    /// # Errors
413    /// `KeyRejected` if the public key bytes are malformed or incompatible
414    /// with the specified algorithm.
415    ///
416    /// # Examples
417    ///
418    /// ```
419    /// use aws_lc_rs::signature::{self, ParsedPublicKey};
420    ///
421    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
422    ///     let parsed_key = ParsedPublicKey::new(&signature::ED25519, include_bytes!("../tests/data/ed25519_test_public_key.bin"))?;
423    ///     let signature = [
424    ///         0xED, 0xDB, 0x67, 0xE9, 0xF7, 0x8C, 0x9A, 0x0, 0xFD, 0xEE, 0x2D, 0x22, 0x21, 0xA3, 0x9A,
425    ///         0x8A, 0x79, 0xF2, 0x53, 0x88, 0x78, 0xF0, 0xA0, 0x1, 0x80, 0xA, 0x49, 0xA4, 0x17, 0x88,
426    ///         0xAB, 0x44, 0x4B, 0xD2, 0x58, 0xB0, 0x3B, 0x51, 0x8A, 0x1B, 0x61, 0x24, 0x52, 0x78, 0x48,
427    ///         0x58, 0x40, 0x5, 0xB5, 0x45, 0x22, 0xB6, 0x40, 0xBD, 0x14, 0x47, 0xB1, 0xF0, 0xDC, 0x13,
428    ///         0xB3, 0xE9, 0xD0, 0x6,
429    ///     ];
430    ///     assert!(parsed_key.verify_sig(b"hello world!", &signature).is_ok());
431    ///     assert!(parsed_key.verify_sig(b"hello world.", &signature).is_err());
432    /// # Ok(())
433    /// # }
434    /// ```
435    pub fn new<B: AsRef<[u8]>>(
436        algorithm: &'static dyn VerificationAlgorithm,
437        bytes: B,
438    ) -> Result<Self, KeyRejected> {
439        parse_public_key(bytes.as_ref(), algorithm)
440    }
441
442    /// Returns the algorithm used by this public key.
443    #[must_use]
444    pub fn algorithm(&self) -> &'static dyn VerificationAlgorithm {
445        self.algorithm
446    }
447
448    pub(crate) fn key(&self) -> &LcPtr<EVP_PKEY> {
449        &self.key
450    }
451
452    /// Uses the public key to verify that `signature` is a valid signature of
453    /// `message`.
454    ///
455    /// This method is more efficient than [`UnparsedPublicKey::verify`] when
456    /// performing multiple signature verifications with the same public key,
457    /// as the key parsing overhead is avoided.
458    ///
459    /// See the [`crate::signature`] module-level documentation for examples.
460    ///
461    // # FIPS
462    // The following conditions must be met:
463    // * RSA Key Sizes: 1024, 2048, 3072, 4096
464    // * NIST Elliptic Curves: P256, P384, P521
465    // * Digest Algorithms: SHA1, SHA256, SHA384, SHA512
466    //
467    /// # Errors
468    /// `error::Unspecified` if the signature is invalid or verification fails.
469    #[inline]
470    pub fn verify_sig(&self, message: &[u8], signature: &[u8]) -> Result<(), error::Unspecified> {
471        self.parsed_algorithm
472            .parsed_verify_sig(self, message, signature)
473    }
474
475    /// Uses the public key to verify that `signature` is a valid signature of
476    /// `digest`.
477    ///
478    /// This method is more efficient than [`UnparsedPublicKey::verify_digest`] when
479    /// performing multiple signature verifications with the same public key,
480    /// as the key parsing overhead is avoided.
481    ///
482    /// See the [`crate::signature`] module-level documentation for examples.
483    ///
484    // # FIPS
485    // Not allowed
486    //
487    /// # Errors
488    /// `error::Unspecified` if the signature is invalid or verification fails.
489    #[inline]
490    pub fn verify_digest_sig(
491        &self,
492        digest: &Digest,
493        signature: &[u8],
494    ) -> Result<(), error::Unspecified> {
495        self.parsed_algorithm
496            .parsed_verify_digest_sig(self, digest, signature)
497    }
498}
499
500impl Debug for ParsedPublicKey {
501    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
502        f.write_str(&format!(
503            "ParsedPublicKey {{ algorithm: {:?}, }}",
504            self.algorithm,
505        ))
506    }
507}
508
509impl<B: AsRef<[u8]>> AsRef<[u8]> for UnparsedPublicKey<B> {
510    #[inline]
511    fn as_ref(&self) -> &[u8] {
512        self.bytes.as_ref()
513    }
514}
515
516impl<B: Copy + AsRef<[u8]>> Copy for UnparsedPublicKey<B> {}
517
518impl<B: AsRef<[u8]>> Debug for UnparsedPublicKey<B> {
519    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
520        f.write_str(&format!(
521            "UnparsedPublicKey {{ algorithm: {:?}, bytes: \"{}\" }}",
522            self.algorithm,
523            hex::encode(self.bytes.as_ref())
524        ))
525    }
526}
527
528impl<B: AsRef<[u8]>> UnparsedPublicKey<B> {
529    /// Construct a new `UnparsedPublicKey`.
530    ///
531    /// No validation of `bytes` is done until `verify()` is called.
532    #[inline]
533    pub fn new(algorithm: &'static dyn VerificationAlgorithm, bytes: B) -> Self {
534        Self { algorithm, bytes }
535    }
536
537    /// Parses the public key and verifies `signature` is a valid signature of
538    /// `message` using it.
539    ///
540    /// See the [`crate::signature`] module-level documentation for examples.
541    ///
542    // # FIPS
543    // The following conditions must be met:
544    // * RSA Key Sizes: 1024, 2048, 3072, 4096
545    // * NIST Elliptic Curves: P256, P384, P521
546    // * Digest Algorithms: SHA1, SHA256, SHA384, SHA512
547    //
548    /// # Errors
549    /// `error::Unspecified` if inputs not verified.
550    #[inline]
551    pub fn verify(&self, message: &[u8], signature: &[u8]) -> Result<(), error::Unspecified> {
552        self.algorithm
553            .verify_sig(self.bytes.as_ref(), message, signature)
554    }
555
556    /// Parses the public key and verifies `signature` is a valid signature of
557    /// `digest` using it.
558    ///
559    /// See the [`crate::signature`] module-level documentation for examples.
560    ///
561    // # FIPS
562    // Not allowed
563    //
564    /// # Errors
565    /// `error::Unspecified` if inputs not verified.
566    #[inline]
567    pub fn verify_digest(
568        &self,
569        digest: &Digest,
570        signature: &[u8],
571    ) -> Result<(), error::Unspecified> {
572        self.algorithm
573            .verify_digest_sig(self.bytes.as_ref(), digest, signature)
574    }
575
576    /// Parses the public key bytes and returns a `ParsedPublicKey`.
577    ///
578    /// This method validates the public key format and creates a `ParsedPublicKey`
579    /// that can be used for more efficient signature verification operations.
580    /// The parsing overhead is incurred once, making subsequent verifications
581    /// faster compared to using `UnparsedPublicKey::verify` directly.
582    ///
583    /// This is equivalent to calling [`ParsedPublicKey::new`] with the same
584    /// algorithm and bytes.
585    ///
586    /// # Errors
587    /// `KeyRejected` if the public key bytes are malformed or incompatible
588    /// with the specified algorithm.
589    pub fn parse(&self) -> Result<ParsedPublicKey, KeyRejected> {
590        parse_public_key(self.bytes.as_ref(), self.algorithm)
591    }
592}
593
594pub(crate) fn parse_public_key(
595    bytes: &[u8],
596    algorithm: &'static dyn VerificationAlgorithm,
597) -> Result<ParsedPublicKey, KeyRejected> {
598    let parsed_algorithm: &'static dyn ParsedVerificationAlgorithm;
599
600    let key = if algorithm.type_id() == TypeId::of::<EcdsaVerificationAlgorithm>() {
601        #[allow(clippy::cast_ptr_alignment)]
602        let ec_alg = unsafe {
603            &*(algorithm as *const dyn VerificationAlgorithm).cast::<EcdsaVerificationAlgorithm>()
604        };
605        parsed_algorithm = ec_alg;
606        parse_ec_public_key(bytes, ec_alg.id.nid())?
607    } else if algorithm.type_id() == TypeId::of::<EdDSAParameters>() {
608        #[allow(clippy::cast_ptr_alignment)]
609        let ed_alg =
610            unsafe { &*(algorithm as *const dyn VerificationAlgorithm).cast::<EdDSAParameters>() };
611        parsed_algorithm = ed_alg;
612        parse_ed25519_public_key(bytes)?
613    } else if algorithm.type_id() == TypeId::of::<RsaParameters>() {
614        #[allow(clippy::cast_ptr_alignment)]
615        let rsa_alg =
616            unsafe { &*(algorithm as *const dyn VerificationAlgorithm).cast::<RsaParameters>() };
617        parsed_algorithm = rsa_alg;
618        parse_rsa_public_key(bytes)?
619    } else {
620        #[cfg(all(feature = "unstable", not(feature = "fips")))]
621        if algorithm.type_id() == TypeId::of::<PqdsaVerificationAlgorithm>() {
622            #[allow(clippy::cast_ptr_alignment)]
623            let pqdsa_alg = unsafe {
624                &*(algorithm as *const dyn VerificationAlgorithm)
625                    .cast::<PqdsaVerificationAlgorithm>()
626            };
627            parsed_algorithm = pqdsa_alg;
628            parse_pqdsa_public_key(bytes, pqdsa_alg.id)?
629        } else {
630            unreachable!()
631        }
632        #[cfg(any(not(feature = "unstable"), feature = "fips"))]
633        unreachable!()
634    };
635
636    Ok(ParsedPublicKey {
637        algorithm,
638        parsed_algorithm,
639        key,
640    })
641}
642
643/// Verification of signatures using RSA keys of 1024-8192 bits, PKCS#1.5 padding, and SHA-1.
644pub static RSA_PKCS1_1024_8192_SHA1_FOR_LEGACY_USE_ONLY: RsaParameters = RsaParameters::new(
645    &digest::SHA1_FOR_LEGACY_USE_ONLY,
646    &rsa::signature::RsaPadding::RSA_PKCS1_PADDING,
647    1024..=8192,
648    &RsaVerificationAlgorithmId::RSA_PKCS1_1024_8192_SHA1_FOR_LEGACY_USE_ONLY,
649);
650
651/// Verification of signatures using RSA keys of 1024-8192 bits, PKCS#1.5 padding, and SHA-256.
652pub static RSA_PKCS1_1024_8192_SHA256_FOR_LEGACY_USE_ONLY: RsaParameters = RsaParameters::new(
653    &digest::SHA256,
654    &rsa::signature::RsaPadding::RSA_PKCS1_PADDING,
655    1024..=8192,
656    &RsaVerificationAlgorithmId::RSA_PKCS1_1024_8192_SHA256_FOR_LEGACY_USE_ONLY,
657);
658
659/// Verification of signatures using RSA keys of 1024-8192 bits, PKCS#1.5 padding, and SHA-512.
660pub static RSA_PKCS1_1024_8192_SHA512_FOR_LEGACY_USE_ONLY: RsaParameters = RsaParameters::new(
661    &digest::SHA512,
662    &rsa::signature::RsaPadding::RSA_PKCS1_PADDING,
663    1024..=8192,
664    &RsaVerificationAlgorithmId::RSA_PKCS1_1024_8192_SHA512_FOR_LEGACY_USE_ONLY,
665);
666
667/// Verification of signatures using RSA keys of 2048-8192 bits, PKCS#1.5 padding, and SHA-1.
668pub static RSA_PKCS1_2048_8192_SHA1_FOR_LEGACY_USE_ONLY: RsaParameters = RsaParameters::new(
669    &digest::SHA1_FOR_LEGACY_USE_ONLY,
670    &rsa::signature::RsaPadding::RSA_PKCS1_PADDING,
671    2048..=8192,
672    &RsaVerificationAlgorithmId::RSA_PKCS1_2048_8192_SHA1_FOR_LEGACY_USE_ONLY,
673);
674
675/// Verification of signatures using RSA keys of 2048-8192 bits, PKCS#1.5 padding, and SHA-256.
676pub static RSA_PKCS1_2048_8192_SHA256: RsaParameters = RsaParameters::new(
677    &digest::SHA256,
678    &rsa::signature::RsaPadding::RSA_PKCS1_PADDING,
679    2048..=8192,
680    &RsaVerificationAlgorithmId::RSA_PKCS1_2048_8192_SHA256,
681);
682
683/// Verification of signatures using RSA keys of 2048-8192 bits, PKCS#1.5 padding, and SHA-384.
684pub static RSA_PKCS1_2048_8192_SHA384: RsaParameters = RsaParameters::new(
685    &digest::SHA384,
686    &rsa::signature::RsaPadding::RSA_PKCS1_PADDING,
687    2048..=8192,
688    &RsaVerificationAlgorithmId::RSA_PKCS1_2048_8192_SHA384,
689);
690
691/// Verification of signatures using RSA keys of 2048-8192 bits, PKCS#1.5 padding, and SHA-512.
692pub static RSA_PKCS1_2048_8192_SHA512: RsaParameters = RsaParameters::new(
693    &digest::SHA512,
694    &rsa::signature::RsaPadding::RSA_PKCS1_PADDING,
695    2048..=8192,
696    &RsaVerificationAlgorithmId::RSA_PKCS1_2048_8192_SHA512,
697);
698
699/// Verification of signatures using RSA keys of 3072-8192 bits, PKCS#1.5 padding, and SHA-384.
700pub static RSA_PKCS1_3072_8192_SHA384: RsaParameters = RsaParameters::new(
701    &digest::SHA384,
702    &rsa::signature::RsaPadding::RSA_PKCS1_PADDING,
703    3072..=8192,
704    &RsaVerificationAlgorithmId::RSA_PKCS1_3072_8192_SHA384,
705);
706
707/// Verification of signatures using RSA keys of 2048-8192 bits, PSS padding, and SHA-256.
708pub static RSA_PSS_2048_8192_SHA256: RsaParameters = RsaParameters::new(
709    &digest::SHA256,
710    &rsa::signature::RsaPadding::RSA_PKCS1_PSS_PADDING,
711    2048..=8192,
712    &RsaVerificationAlgorithmId::RSA_PSS_2048_8192_SHA256,
713);
714
715/// Verification of signatures using RSA keys of 2048-8192 bits, PSS padding, and SHA-384.
716pub static RSA_PSS_2048_8192_SHA384: RsaParameters = RsaParameters::new(
717    &digest::SHA384,
718    &rsa::signature::RsaPadding::RSA_PKCS1_PSS_PADDING,
719    2048..=8192,
720    &RsaVerificationAlgorithmId::RSA_PSS_2048_8192_SHA384,
721);
722
723/// Verification of signatures using RSA keys of 2048-8192 bits, PSS padding, and SHA-512.
724pub static RSA_PSS_2048_8192_SHA512: RsaParameters = RsaParameters::new(
725    &digest::SHA512,
726    &rsa::signature::RsaPadding::RSA_PKCS1_PSS_PADDING,
727    2048..=8192,
728    &RsaVerificationAlgorithmId::RSA_PSS_2048_8192_SHA512,
729);
730
731/// RSA PSS padding using SHA-256 for RSA signatures.
732pub static RSA_PSS_SHA256: RsaSignatureEncoding = RsaSignatureEncoding::new(
733    &digest::SHA256,
734    &rsa::signature::RsaPadding::RSA_PKCS1_PSS_PADDING,
735    &RsaSigningAlgorithmId::RSA_PSS_SHA256,
736);
737
738/// RSA PSS padding using SHA-384 for RSA signatures.
739pub static RSA_PSS_SHA384: RsaSignatureEncoding = RsaSignatureEncoding::new(
740    &digest::SHA384,
741    &rsa::signature::RsaPadding::RSA_PKCS1_PSS_PADDING,
742    &RsaSigningAlgorithmId::RSA_PSS_SHA384,
743);
744
745/// RSA PSS padding using SHA-512 for RSA signatures.
746pub static RSA_PSS_SHA512: RsaSignatureEncoding = RsaSignatureEncoding::new(
747    &digest::SHA512,
748    &rsa::signature::RsaPadding::RSA_PKCS1_PSS_PADDING,
749    &RsaSigningAlgorithmId::RSA_PSS_SHA512,
750);
751
752/// PKCS#1 1.5 padding using SHA-256 for RSA signatures.
753pub static RSA_PKCS1_SHA256: RsaSignatureEncoding = RsaSignatureEncoding::new(
754    &digest::SHA256,
755    &rsa::signature::RsaPadding::RSA_PKCS1_PADDING,
756    &RsaSigningAlgorithmId::RSA_PKCS1_SHA256,
757);
758
759/// PKCS#1 1.5 padding using SHA-384 for RSA signatures.
760pub static RSA_PKCS1_SHA384: RsaSignatureEncoding = RsaSignatureEncoding::new(
761    &digest::SHA384,
762    &rsa::signature::RsaPadding::RSA_PKCS1_PADDING,
763    &RsaSigningAlgorithmId::RSA_PKCS1_SHA384,
764);
765
766/// PKCS#1 1.5 padding using SHA-512 for RSA signatures.
767pub static RSA_PKCS1_SHA512: RsaSignatureEncoding = RsaSignatureEncoding::new(
768    &digest::SHA512,
769    &rsa::signature::RsaPadding::RSA_PKCS1_PADDING,
770    &RsaSigningAlgorithmId::RSA_PKCS1_SHA512,
771);
772
773/// Verification of fixed-length (PKCS#11 style) ECDSA signatures using the P-256 curve and SHA-256.
774pub static ECDSA_P256_SHA256_FIXED: EcdsaVerificationAlgorithm = EcdsaVerificationAlgorithm {
775    id: &ec::signature::AlgorithmID::ECDSA_P256,
776    digest: &digest::SHA256,
777    sig_format: EcdsaSignatureFormat::Fixed,
778};
779
780/// Verification of fixed-length (PKCS#11 style) ECDSA signatures using the P-384 curve and SHA-384.
781pub static ECDSA_P384_SHA384_FIXED: EcdsaVerificationAlgorithm = EcdsaVerificationAlgorithm {
782    id: &ec::signature::AlgorithmID::ECDSA_P384,
783    digest: &digest::SHA384,
784    sig_format: EcdsaSignatureFormat::Fixed,
785};
786
787/// Verification of fixed-length (PKCS#11 style) ECDSA signatures using the P-384 curve and SHA3-384.
788pub static ECDSA_P384_SHA3_384_FIXED: EcdsaVerificationAlgorithm = EcdsaVerificationAlgorithm {
789    id: &ec::signature::AlgorithmID::ECDSA_P384,
790    digest: &digest::SHA3_384,
791    sig_format: EcdsaSignatureFormat::Fixed,
792};
793
794/// Verification of fixed-length (PKCS#11 style) ECDSA signatures using the P-521 curve and SHA-1.
795pub static ECDSA_P521_SHA1_FIXED: EcdsaVerificationAlgorithm = EcdsaVerificationAlgorithm {
796    id: &ec::signature::AlgorithmID::ECDSA_P521,
797    digest: &digest::SHA1_FOR_LEGACY_USE_ONLY,
798    sig_format: EcdsaSignatureFormat::Fixed,
799};
800
801/// Verification of fixed-length (PKCS#11 style) ECDSA signatures using the P-521 curve and SHA-224.
802pub static ECDSA_P521_SHA224_FIXED: EcdsaVerificationAlgorithm = EcdsaVerificationAlgorithm {
803    id: &ec::signature::AlgorithmID::ECDSA_P521,
804    digest: &digest::SHA224,
805    sig_format: EcdsaSignatureFormat::Fixed,
806};
807
808/// Verification of fixed-length (PKCS#11 style) ECDSA signatures using the P-521 curve and SHA-256.
809pub static ECDSA_P521_SHA256_FIXED: EcdsaVerificationAlgorithm = EcdsaVerificationAlgorithm {
810    id: &ec::signature::AlgorithmID::ECDSA_P521,
811    digest: &digest::SHA256,
812    sig_format: EcdsaSignatureFormat::Fixed,
813};
814
815/// Verification of fixed-length (PKCS#11 style) ECDSA signatures using the P-521 curve and SHA-384.
816pub static ECDSA_P521_SHA384_FIXED: EcdsaVerificationAlgorithm = EcdsaVerificationAlgorithm {
817    id: &ec::signature::AlgorithmID::ECDSA_P521,
818    digest: &digest::SHA384,
819    sig_format: EcdsaSignatureFormat::Fixed,
820};
821
822/// Verification of fixed-length (PKCS#11 style) ECDSA signatures using the P-521 curve and SHA-512.
823pub static ECDSA_P521_SHA512_FIXED: EcdsaVerificationAlgorithm = EcdsaVerificationAlgorithm {
824    id: &ec::signature::AlgorithmID::ECDSA_P521,
825    digest: &digest::SHA512,
826    sig_format: EcdsaSignatureFormat::Fixed,
827};
828
829/// Verification of fixed-length (PKCS#11 style) ECDSA signatures using the P-521 curve and SHA3-512.
830pub static ECDSA_P521_SHA3_512_FIXED: EcdsaVerificationAlgorithm = EcdsaVerificationAlgorithm {
831    id: &ec::signature::AlgorithmID::ECDSA_P521,
832    digest: &digest::SHA3_512,
833    sig_format: EcdsaSignatureFormat::Fixed,
834};
835
836/// Verification of fixed-length (PKCS#11 style) ECDSA signatures using the P-256K1 curve and SHA-256.
837pub static ECDSA_P256K1_SHA256_FIXED: EcdsaVerificationAlgorithm = EcdsaVerificationAlgorithm {
838    id: &ec::signature::AlgorithmID::ECDSA_P256K1,
839    digest: &digest::SHA256,
840    sig_format: EcdsaSignatureFormat::Fixed,
841};
842
843/// Verification of fixed-length (PKCS#11 style) ECDSA signatures using the P-256K1 curve and SHA3-256.
844pub static ECDSA_P256K1_SHA3_256_FIXED: EcdsaVerificationAlgorithm = EcdsaVerificationAlgorithm {
845    id: &ec::signature::AlgorithmID::ECDSA_P256K1,
846    digest: &digest::SHA3_256,
847    sig_format: EcdsaSignatureFormat::Fixed,
848};
849
850/// Verification of ASN.1 DER-encoded ECDSA signatures using the P-256 curve and SHA-256.
851pub static ECDSA_P256_SHA256_ASN1: EcdsaVerificationAlgorithm = EcdsaVerificationAlgorithm {
852    id: &ec::signature::AlgorithmID::ECDSA_P256,
853    digest: &digest::SHA256,
854    sig_format: EcdsaSignatureFormat::ASN1,
855};
856
857/// *Not recommended.* Verification of ASN.1 DER-encoded ECDSA signatures using the P-256 curve and SHA-384.
858pub static ECDSA_P256_SHA384_ASN1: EcdsaVerificationAlgorithm = EcdsaVerificationAlgorithm {
859    id: &ec::signature::AlgorithmID::ECDSA_P256,
860    digest: &digest::SHA384,
861    sig_format: EcdsaSignatureFormat::ASN1,
862};
863
864/// *Not recommended.* Verification of ASN.1 DER-encoded ECDSA signatures using the P-256 curve and SHA-512.
865pub static ECDSA_P256_SHA512_ASN1: EcdsaVerificationAlgorithm = EcdsaVerificationAlgorithm {
866    id: &ec::signature::AlgorithmID::ECDSA_P256,
867    digest: &digest::SHA512,
868    sig_format: EcdsaSignatureFormat::ASN1,
869};
870
871/// *Not recommended.* Verification of ASN.1 DER-encoded ECDSA signatures using the P-384 curve and SHA-256.
872pub static ECDSA_P384_SHA256_ASN1: EcdsaVerificationAlgorithm = EcdsaVerificationAlgorithm {
873    id: &ec::signature::AlgorithmID::ECDSA_P384,
874    digest: &digest::SHA256,
875    sig_format: EcdsaSignatureFormat::ASN1,
876};
877
878/// Verification of ASN.1 DER-encoded ECDSA signatures using the P-384 curve and SHA-384.
879pub static ECDSA_P384_SHA384_ASN1: EcdsaVerificationAlgorithm = EcdsaVerificationAlgorithm {
880    id: &ec::signature::AlgorithmID::ECDSA_P384,
881    digest: &digest::SHA384,
882    sig_format: EcdsaSignatureFormat::ASN1,
883};
884
885/// *Not recommended.* Verification of ASN.1 DER-encoded ECDSA signatures using the P-384 curve and SHA-512.
886pub static ECDSA_P384_SHA512_ASN1: EcdsaVerificationAlgorithm = EcdsaVerificationAlgorithm {
887    id: &ec::signature::AlgorithmID::ECDSA_P384,
888    digest: &digest::SHA512,
889    sig_format: EcdsaSignatureFormat::ASN1,
890};
891
892/// Verification of ASN.1 DER-encoded ECDSA signatures using the P-384 curve and SHA3-384.
893pub static ECDSA_P384_SHA3_384_ASN1: EcdsaVerificationAlgorithm = EcdsaVerificationAlgorithm {
894    id: &ec::signature::AlgorithmID::ECDSA_P384,
895    digest: &digest::SHA3_384,
896    sig_format: EcdsaSignatureFormat::ASN1,
897};
898
899/// Verification of ASN.1 DER-encoded ECDSA signatures using the P-521 curve and SHA-1.
900pub static ECDSA_P521_SHA1_ASN1: EcdsaVerificationAlgorithm = EcdsaVerificationAlgorithm {
901    id: &ec::signature::AlgorithmID::ECDSA_P521,
902    digest: &digest::SHA1_FOR_LEGACY_USE_ONLY,
903    sig_format: EcdsaSignatureFormat::ASN1,
904};
905
906/// Verification of ASN.1 DER-encoded ECDSA signatures using the P-521 curve and SHA-224.
907pub static ECDSA_P521_SHA224_ASN1: EcdsaVerificationAlgorithm = EcdsaVerificationAlgorithm {
908    id: &ec::signature::AlgorithmID::ECDSA_P521,
909    digest: &digest::SHA224,
910    sig_format: EcdsaSignatureFormat::ASN1,
911};
912
913/// Verification of ASN.1 DER-encoded ECDSA signatures using the P-521 curve and SHA-256.
914pub static ECDSA_P521_SHA256_ASN1: EcdsaVerificationAlgorithm = EcdsaVerificationAlgorithm {
915    id: &ec::signature::AlgorithmID::ECDSA_P521,
916    digest: &digest::SHA256,
917    sig_format: EcdsaSignatureFormat::ASN1,
918};
919
920/// Verification of ASN.1 DER-encoded ECDSA signatures using the P-521 curve and SHA-384.
921pub static ECDSA_P521_SHA384_ASN1: EcdsaVerificationAlgorithm = EcdsaVerificationAlgorithm {
922    id: &ec::signature::AlgorithmID::ECDSA_P521,
923    digest: &digest::SHA384,
924    sig_format: EcdsaSignatureFormat::ASN1,
925};
926
927/// Verification of ASN.1 DER-encoded ECDSA signatures using the P-521 curve and SHA-512.
928pub static ECDSA_P521_SHA512_ASN1: EcdsaVerificationAlgorithm = EcdsaVerificationAlgorithm {
929    id: &ec::signature::AlgorithmID::ECDSA_P521,
930    digest: &digest::SHA512,
931    sig_format: EcdsaSignatureFormat::ASN1,
932};
933
934/// Verification of ASN.1 DER-encoded ECDSA signatures using the P-521 curve and SHA3-512.
935pub static ECDSA_P521_SHA3_512_ASN1: EcdsaVerificationAlgorithm = EcdsaVerificationAlgorithm {
936    id: &ec::signature::AlgorithmID::ECDSA_P521,
937    digest: &digest::SHA3_512,
938    sig_format: EcdsaSignatureFormat::ASN1,
939};
940
941/// Verification of ASN.1 DER-encoded ECDSA signatures using the P-256K1 curve and SHA-256.
942pub static ECDSA_P256K1_SHA256_ASN1: EcdsaVerificationAlgorithm = EcdsaVerificationAlgorithm {
943    id: &ec::signature::AlgorithmID::ECDSA_P256K1,
944    digest: &digest::SHA256,
945    sig_format: EcdsaSignatureFormat::ASN1,
946};
947
948/// Verification of ASN.1 DER-encoded ECDSA signatures using the P-256K1 curve and SHA3-256.
949pub static ECDSA_P256K1_SHA3_256_ASN1: EcdsaVerificationAlgorithm = EcdsaVerificationAlgorithm {
950    id: &ec::signature::AlgorithmID::ECDSA_P256K1,
951    digest: &digest::SHA3_256,
952    sig_format: EcdsaSignatureFormat::ASN1,
953};
954
955/// Signing of fixed-length (PKCS#11 style) ECDSA signatures using the P-256 curve and SHA-256.
956pub static ECDSA_P256_SHA256_FIXED_SIGNING: EcdsaSigningAlgorithm =
957    EcdsaSigningAlgorithm(&ECDSA_P256_SHA256_FIXED);
958
959/// Signing of fixed-length (PKCS#11 style) ECDSA signatures using the P-384 curve and SHA-384.
960pub static ECDSA_P384_SHA384_FIXED_SIGNING: EcdsaSigningAlgorithm =
961    EcdsaSigningAlgorithm(&ECDSA_P384_SHA384_FIXED);
962
963/// Signing of fixed-length (PKCS#11 style) ECDSA signatures using the P-384 curve and SHA3-384.
964pub static ECDSA_P384_SHA3_384_FIXED_SIGNING: EcdsaSigningAlgorithm =
965    EcdsaSigningAlgorithm(&ECDSA_P384_SHA3_384_FIXED);
966
967/// Signing of fixed-length (PKCS#11 style) ECDSA signatures using the P-521 curve and SHA-224.
968/// # ⚠️ Warning
969/// The security design strength of SHA-224 digests is less then security strength of P-521.
970/// This scheme should only be used for backwards compatibility purposes.
971pub static ECDSA_P521_SHA224_FIXED_SIGNING: EcdsaSigningAlgorithm =
972    EcdsaSigningAlgorithm(&ECDSA_P521_SHA224_FIXED);
973
974/// Signing of fixed-length (PKCS#11 style) ECDSA signatures using the P-521 curve and SHA-256.
975/// # ⚠️ Warning
976/// The security design strength of SHA-256 digests is less then security strength of P-521.
977/// This scheme should only be used for backwards compatibility purposes.
978pub static ECDSA_P521_SHA256_FIXED_SIGNING: EcdsaSigningAlgorithm =
979    EcdsaSigningAlgorithm(&ECDSA_P521_SHA256_FIXED);
980
981/// Signing of fixed-length (PKCS#11 style) ECDSA signatures using the P-521 curve and SHA-384.
982/// # ⚠️ Warning
983/// The security design strength of SHA-384 digests is less then security strength of P-521.
984/// This scheme should only be used for backwards compatibility purposes.
985pub static ECDSA_P521_SHA384_FIXED_SIGNING: EcdsaSigningAlgorithm =
986    EcdsaSigningAlgorithm(&ECDSA_P521_SHA384_FIXED);
987
988/// Signing of fixed-length (PKCS#11 style) ECDSA signatures using the P-521 curve and SHA-512.
989pub static ECDSA_P521_SHA512_FIXED_SIGNING: EcdsaSigningAlgorithm =
990    EcdsaSigningAlgorithm(&ECDSA_P521_SHA512_FIXED);
991
992/// Signing of fixed-length (PKCS#11 style) ECDSA signatures using the P-521 curve and SHA3-512.
993pub static ECDSA_P521_SHA3_512_FIXED_SIGNING: EcdsaSigningAlgorithm =
994    EcdsaSigningAlgorithm(&ECDSA_P521_SHA3_512_FIXED);
995
996/// Signing of fixed-length (PKCS#11 style) ECDSA signatures using the P-256K1 curve and SHA-256.
997pub static ECDSA_P256K1_SHA256_FIXED_SIGNING: EcdsaSigningAlgorithm =
998    EcdsaSigningAlgorithm(&ECDSA_P256K1_SHA256_FIXED);
999
1000/// Signing of fixed-length (PKCS#11 style) ECDSA signatures using the P-256K1 curve and SHA3-256.
1001pub static ECDSA_P256K1_SHA3_256_FIXED_SIGNING: EcdsaSigningAlgorithm =
1002    EcdsaSigningAlgorithm(&ECDSA_P256K1_SHA3_256_FIXED);
1003
1004/// Signing of ASN.1 DER-encoded ECDSA signatures using the P-256 curve and SHA-256.
1005pub static ECDSA_P256_SHA256_ASN1_SIGNING: EcdsaSigningAlgorithm =
1006    EcdsaSigningAlgorithm(&ECDSA_P256_SHA256_ASN1);
1007
1008/// Signing of ASN.1 DER-encoded ECDSA signatures using the P-384 curve and SHA-384.
1009pub static ECDSA_P384_SHA384_ASN1_SIGNING: EcdsaSigningAlgorithm =
1010    EcdsaSigningAlgorithm(&ECDSA_P384_SHA384_ASN1);
1011
1012/// Signing of ASN.1 DER-encoded ECDSA signatures using the P-384 curve and SHA3-384.
1013pub static ECDSA_P384_SHA3_384_ASN1_SIGNING: EcdsaSigningAlgorithm =
1014    EcdsaSigningAlgorithm(&ECDSA_P384_SHA3_384_ASN1);
1015
1016/// Signing of ASN.1 DER-encoded ECDSA signatures using the P-521 curve and SHA-224.
1017/// # ⚠️ Warning
1018/// The security design strength of SHA-224 digests is less then security strength of P-521.
1019/// This scheme should only be used for backwards compatibility purposes.
1020pub static ECDSA_P521_SHA224_ASN1_SIGNING: EcdsaSigningAlgorithm =
1021    EcdsaSigningAlgorithm(&ECDSA_P521_SHA224_ASN1);
1022
1023/// Signing of ASN.1 DER-encoded ECDSA signatures using the P-521 curve and SHA-256.
1024/// # ⚠️ Warning
1025/// The security design strength of SHA-256 digests is less then security strength of P-521.
1026/// This scheme should only be used for backwards compatibility purposes.
1027pub static ECDSA_P521_SHA256_ASN1_SIGNING: EcdsaSigningAlgorithm =
1028    EcdsaSigningAlgorithm(&ECDSA_P521_SHA256_ASN1);
1029
1030/// Signing of ASN.1 DER-encoded ECDSA signatures using the P-521 curve and SHA-384.
1031/// # ⚠️ Warning
1032/// The security design strength of SHA-384 digests is less then security strength of P-521.
1033/// This scheme should only be used for backwards compatibility purposes.
1034pub static ECDSA_P521_SHA384_ASN1_SIGNING: EcdsaSigningAlgorithm =
1035    EcdsaSigningAlgorithm(&ECDSA_P521_SHA384_ASN1);
1036
1037/// Signing of ASN.1 DER-encoded ECDSA signatures using the P-521 curve and SHA-512.
1038pub static ECDSA_P521_SHA512_ASN1_SIGNING: EcdsaSigningAlgorithm =
1039    EcdsaSigningAlgorithm(&ECDSA_P521_SHA512_ASN1);
1040
1041/// Signing of ASN.1 DER-encoded ECDSA signatures using the P-521 curve and SHA3-512.
1042pub static ECDSA_P521_SHA3_512_ASN1_SIGNING: EcdsaSigningAlgorithm =
1043    EcdsaSigningAlgorithm(&ECDSA_P521_SHA3_512_ASN1);
1044
1045/// Signing of ASN.1 DER-encoded ECDSA signatures using the P-256K1 curve and SHA-256.
1046pub static ECDSA_P256K1_SHA256_ASN1_SIGNING: EcdsaSigningAlgorithm =
1047    EcdsaSigningAlgorithm(&ECDSA_P256K1_SHA256_ASN1);
1048
1049/// Signing of ASN.1 DER-encoded ECDSA signatures using the P-256K1 curve and SHA3-256.
1050pub static ECDSA_P256K1_SHA3_256_ASN1_SIGNING: EcdsaSigningAlgorithm =
1051    EcdsaSigningAlgorithm(&ECDSA_P256K1_SHA3_256_ASN1);
1052
1053/// Verification of Ed25519 signatures.
1054pub static ED25519: EdDSAParameters = EdDSAParameters {};
1055
1056#[cfg(test)]
1057mod tests {
1058    use regex::Regex;
1059
1060    use crate::rand::{generate, SystemRandom};
1061    use crate::signature::{UnparsedPublicKey, ED25519};
1062
1063    #[cfg(feature = "fips")]
1064    mod fips;
1065
1066    #[test]
1067    fn test_unparsed_public_key() {
1068        let random_pubkey: [u8; 32] = generate(&SystemRandom::new()).unwrap().expose();
1069        let unparsed_pubkey = UnparsedPublicKey::new(&ED25519, random_pubkey);
1070        let unparsed_pubkey_debug = format!("{:?}", &unparsed_pubkey);
1071
1072        #[allow(clippy::clone_on_copy)]
1073        let unparsed_pubkey_clone = unparsed_pubkey.clone();
1074        assert_eq!(unparsed_pubkey_debug, format!("{unparsed_pubkey_clone:?}"));
1075        let pubkey_re = Regex::new(
1076            "UnparsedPublicKey \\{ algorithm: EdDSAParameters, bytes: \"[0-9a-f]{64}\" \\}",
1077        )
1078        .unwrap();
1079
1080        assert!(pubkey_re.is_match(&unparsed_pubkey_debug));
1081    }
1082}