Expand description
Public key signatures: signing and verification.
Use the verify
function to verify signatures, passing a reference to the
algorithm that identifies the algorithm. See the documentation for verify
for examples.
For signature verification, this API treats each combination of parameters
as a separate algorithm. For example, instead of having a single “RSA”
algorithm with a verification function that takes a bunch of parameters,
there are RSA_PKCS1_2048_8192_SHA256
, RSA_PKCS1_2048_8192_SHA384
, etc.,
which encode sets of parameter choices into objects. This is designed to
reduce the risks of algorithm agility and to provide consistency with ECDSA
and EdDSA
.
Currently this module does not support digesting the message to be signed separately from the public key operation, as it is currently being optimized for Ed25519 and for the implementation of protocols that do not requiring signing large messages. An interface for efficiently supporting larger messages may be added later.
§Algorithm Details
§ECDSA_*_ASN1
Details: ASN.1-encoded ECDSA Signatures
The signature is a ASN.1 DER-encoded Ecdsa-Sig-Value
as described in
RFC 3279 Section 2.2.3. This is the form of ECDSA signature used in
X.509-related structures and in TLS’s ServerKeyExchange
messages.
The public key is encoding in uncompressed form using the Octet-String-to-Elliptic-Curve-Point algorithm in SEC 1: Elliptic Curve Cryptography, Version 2.0.
During verification, the public key is validated using the ECC Partial Public-Key Validation Routine from Section 5.6.2.3.3 of NIST Special Publication 800-56A, revision 2 and Appendix A.3 of the NSA’s Suite B implementer’s guide to FIPS 186-3. Note that, as explained in the NSA guide, ECC Partial Public-Key Validation is equivalent to ECC Full Public-Key Validation for prime-order curves like this one.
§ECDSA_*_FIXED
Details: Fixed-length (PKCS#11-style) ECDSA Signatures
The signature is r||s, where || denotes concatenation, and where both r and s are both big-endian-encoded values that are left-padded to the maximum length. A P-256 signature will be 64 bytes long (two 32-byte components) and a P-384 signature will be 96 bytes long (two 48-byte components). This is the form of ECDSA signature used PKCS#11 and DNSSEC.
The public key is encoding in uncompressed form using the Octet-String-to-Elliptic-Curve-Point algorithm in SEC 1: Elliptic Curve Cryptography, Version 2.0.
During verification, the public key is validated using the ECC Partial Public-Key Validation Routine from Section 5.6.2.3.3 of NIST Special Publication 800-56A, revision 2 and Appendix A.3 of the NSA’s Suite B implementer’s guide to FIPS 186-3. Note that, as explained in the NSA guide, ECC Partial Public-Key Validation is equivalent to ECC Full Public-Key Validation for prime-order curves like this one.
§RSA_PKCS1_*
Details: RSA PKCS#1 1.5 Signatures
The signature is an RSASSA-PKCS1-v1_5 signature as described in RFC 3447 Section 8.2.
The public key is encoded as an ASN.1 RSAPublicKey
as described in
RFC 3447 Appendix-A.1.1. The public key modulus length, rounded up to
the nearest (larger) multiple of 8 bits, must be in the range given in the
name of the algorithm. The public exponent must be an odd integer of 2-33
bits, inclusive.
§RSA_PSS_*
Details: RSA PSS Signatures
The signature is an RSASSA-PSS signature as described in RFC 3447 Section 8.1.
The public key is encoded as an ASN.1 RSAPublicKey
as described in
RFC 3447 Appendix-A.1.1. The public key modulus length, rounded up to
the nearest (larger) multiple of 8 bits, must be in the range given in the
name of the algorithm. The public exponent must be an odd integer of 2-33
bits, inclusive.
During verification, signatures will only be accepted if the MGF1 digest algorithm is the same as the message digest algorithm and if the salt length is the same length as the message digest. This matches the requirements in TLS 1.3 and other recent specifications.
During signing, the message digest algorithm will be used as the MGF1
digest algorithm. The salt will be the same length as the message digest.
This matches the requirements in TLS 1.3 and other recent specifications.
Additionally, the entire salt is randomly generated separately for each
signature using the secure random number generator passed to sign()
.
§Examples
§Signing and verifying with Ed25519
use aws_lc_rs::{
rand,
signature::{self, KeyPair},
};
fn main() -> Result<(), aws_lc_rs::error::Unspecified> {
// Generate a new key pair for Ed25519.
let key_pair = signature::Ed25519KeyPair::generate()?;
// Sign the message "hello, world".
const MESSAGE: &[u8] = b"hello, world";
let sig = key_pair.sign(MESSAGE);
// Normally an application would extract the bytes of the signature and
// send them in a protocol message to the peer(s). Here we just get the
// public key key directly from the key pair.
let peer_public_key_bytes = key_pair.public_key().as_ref();
// Verify the signature of the message using the public key. Normally the
// verifier of the message would parse the inputs to this code out of the
// protocol message(s) sent by the signer.
let peer_public_key =
signature::UnparsedPublicKey::new(&signature::ED25519, peer_public_key_bytes);
peer_public_key.verify(MESSAGE, sig.as_ref())?;
Ok(())
}
§Signing and verifying with RSA (PKCS#1 1.5 padding)
By default OpenSSL writes RSA public keys in SubjectPublicKeyInfo
format,
not RSAPublicKey
format, and Base64-encodes them (“PEM” format).
To convert the PEM SubjectPublicKeyInfo
format (“BEGIN PUBLIC KEY”) to the
binary RSAPublicKey
format needed by verify()
, use:
openssl rsa -pubin \
-in public_key.pem \
-inform PEM \
-RSAPublicKey_out \
-outform DER \
-out public_key.der
To extract the RSAPublicKey-formatted public key from an ASN.1 (binary)
DER-encoded RSAPrivateKey
format private key file, use:
openssl rsa -in private_key.der \
-inform DER \
-RSAPublicKey_out \
-outform DER \
-out public_key.der
use aws_lc_rs::{rand, signature};
fn sign_and_verify_rsa(
private_key_path: &std::path::Path,
public_key_path: &std::path::Path,
) -> Result<(), MyError> {
// Create an `RsaKeyPair` from the DER-encoded bytes. This example uses
// a 2048-bit key, but larger keys are also supported.
let private_key_der = read_file(private_key_path)?;
let key_pair = signature::RsaKeyPair::from_der(&private_key_der)
.map_err(|_| MyError::BadPrivateKey)?;
// Sign the message "hello, world", using PKCS#1 v1.5 padding and the
// SHA256 digest algorithm.
const MESSAGE: &'static [u8] = b"hello, world";
let rng = rand::SystemRandom::new();
let mut signature = vec![0; key_pair.public_modulus_len()];
key_pair
.sign(&signature::RSA_PKCS1_SHA256, &rng, MESSAGE, &mut signature)
.map_err(|_| MyError::OOM)?;
// Verify the signature.
let public_key = signature::UnparsedPublicKey::new(
&signature::RSA_PKCS1_2048_8192_SHA256,
read_file(public_key_path)?,
);
public_key
.verify(MESSAGE, &signature)
.map_err(|_| MyError::BadSignature)
}
#[derive(Debug)]
enum MyError {
IO(std::io::Error),
BadPrivateKey,
OOM,
BadSignature,
}
fn read_file(path: &std::path::Path) -> Result<Vec<u8>, MyError> {
use std::io::Read;
let mut file = std::fs::File::open(path).map_err(|e| MyError::IO(e))?;
let mut contents: Vec<u8> = Vec::new();
file.read_to_end(&mut contents)
.map_err(|e| MyError::IO(e))?;
Ok(contents)
}
fn main() {
let private_key_path =
std::path::Path::new("tests/data/signature_rsa_example_private_key.der");
let public_key_path =
std::path::Path::new("tests/data/signature_rsa_example_public_key.der");
sign_and_verify_rsa(&private_key_path, &public_key_path).unwrap()
}
Re-exports§
pub use crate::rsa::KeyPair as RsaKeyPair;
pub use crate::rsa::PublicKey as RsaSubjectPublicKey;
pub use crate::rsa::PublicKeyComponents as RsaPublicKeyComponents;
pub use crate::rsa::RsaParameters;
Structs§
- Ecdsa
KeyPair - An ECDSA key pair, used for signing.
- Ecdsa
Private Key - Elliptic curve private key.
- Ecdsa
Public Key - Elliptic curve public key.
- Ecdsa
Signing Algorithm - An ECDSA signing algorithm.
- Ecdsa
Verification Algorithm - An ECDSA verification algorithm.
- Ed25519
KeyPair - An Ed25519 key pair, for signing.
- Ed25519
Public Key - Ed25519 Public Key
- Ed25519
Seed - The seed value for the
EdDSA
signature scheme using Curve25519 - EdDSA
Parameters - Parameters for
EdDSA
signing and verification. - Signature
- A public key signature returned from a signing operation.
- Unparsed
Public Key - An unparsed, possibly malformed, public key for signature verification.
Constants§
- ED25519_
PUBLIC_ KEY_ LEN - The length of an Ed25519 public key.
Statics§
- ECDSA_
P256 K1_ SHA3_ 256_ ASN1 - Verification of ASN.1 DER-encoded ECDSA signatures using the P-256K1 curve and SHA3-256.
- ECDSA_
P256 K1_ SHA3_ 256_ ASN1_ SIGNING - Signing of ASN.1 DER-encoded ECDSA signatures using the P-256K1 curve and SHA3-256.
- ECDSA_
P256 K1_ SHA3_ 256_ FIXED - Verification of fixed-length (PKCS#11 style) ECDSA signatures using the P-256K1 curve and SHA3-256.
- ECDSA_
P256 K1_ SHA3_ 256_ FIXED_ SIGNING - Signing of fixed-length (PKCS#11 style) ECDSA signatures using the P-256K1 curve and SHA3-256.
- ECDSA_
P256 K1_ SHA256_ ASN1 - Verification of ASN.1 DER-encoded ECDSA signatures using the P-256K1 curve and SHA-256.
- ECDSA_
P256 K1_ SHA256_ ASN1_ SIGNING - Signing of ASN.1 DER-encoded ECDSA signatures using the P-256K1 curve and SHA-256.
- ECDSA_
P256 K1_ SHA256_ FIXED - Verification of fixed-length (PKCS#11 style) ECDSA signatures using the P-256K1 curve and SHA-256.
- ECDSA_
P256 K1_ SHA256_ FIXED_ SIGNING - Signing of fixed-length (PKCS#11 style) ECDSA signatures using the P-256K1 curve and SHA-256.
- ECDSA_
P256_ SHA256_ ASN1 - Verification of ASN.1 DER-encoded ECDSA signatures using the P-256 curve and SHA-256.
- ECDSA_
P256_ SHA256_ ASN1_ SIGNING - Signing of ASN.1 DER-encoded ECDSA signatures using the P-256 curve and SHA-256.
- ECDSA_
P256_ SHA256_ FIXED - Verification of fixed-length (PKCS#11 style) ECDSA signatures using the P-256 curve and SHA-256.
- ECDSA_
P256_ SHA256_ FIXED_ SIGNING - Signing of fixed-length (PKCS#11 style) ECDSA signatures using the P-256 curve and SHA-256.
- ECDSA_
P256_ SHA384_ ASN1 - Not recommended. Verification of ASN.1 DER-encoded ECDSA signatures using the P-256 curve and SHA-384.
- ECDSA_
P384_ SHA3_ 384_ ASN1 - Verification of ASN.1 DER-encoded ECDSA signatures using the P-384 curve and SHA3-384.
- ECDSA_
P384_ SHA3_ 384_ ASN1_ SIGNING - Signing of ASN.1 DER-encoded ECDSA signatures using the P-384 curve and SHA3-384.
- ECDSA_
P384_ SHA3_ 384_ FIXED - Verification of fixed-length (PKCS#11 style) ECDSA signatures using the P-384 curve and SHA3-384.
- ECDSA_
P384_ SHA3_ 384_ FIXED_ SIGNING - Signing of fixed-length (PKCS#11 style) ECDSA signatures using the P-384 curve and SHA3-384.
- ECDSA_
P384_ SHA256_ ASN1 - Not recommended. Verification of ASN.1 DER-encoded ECDSA signatures using the P-384 curve and SHA-256.
- ECDSA_
P384_ SHA384_ ASN1 - Verification of ASN.1 DER-encoded ECDSA signatures using the P-384 curve and SHA-384.
- ECDSA_
P384_ SHA384_ ASN1_ SIGNING - Signing of ASN.1 DER-encoded ECDSA signatures using the P-384 curve and SHA-384.
- ECDSA_
P384_ SHA384_ FIXED - Verification of fixed-length (PKCS#11 style) ECDSA signatures using the P-384 curve and SHA-384.
- ECDSA_
P384_ SHA384_ FIXED_ SIGNING - Signing of fixed-length (PKCS#11 style) ECDSA signatures using the P-384 curve and SHA-384.
- ECDSA_
P521_ SHA1_ ASN1 - Verification of ASN.1 DER-encoded ECDSA signatures using the P-521 curve and SHA-1.
- ECDSA_
P521_ SHA1_ FIXED - Verification of fixed-length (PKCS#11 style) ECDSA signatures using the P-521 curve and SHA-1.
- ECDSA_
P521_ SHA3_ 512_ ASN1 - Verification of ASN.1 DER-encoded ECDSA signatures using the P-521 curve and SHA3-512.
- ECDSA_
P521_ SHA3_ 512_ ASN1_ SIGNING - Signing of ASN.1 DER-encoded ECDSA signatures using the P-521 curve and SHA3-512.
- ECDSA_
P521_ SHA3_ 512_ FIXED - Verification of fixed-length (PKCS#11 style) ECDSA signatures using the P-521 curve and SHA3-512.
- ECDSA_
P521_ SHA3_ 512_ FIXED_ SIGNING - Signing of fixed-length (PKCS#11 style) ECDSA signatures using the P-521 curve and SHA3-512.
- ECDSA_
P521_ SHA224_ ASN1 - Verification of ASN.1 DER-encoded ECDSA signatures using the P-521 curve and SHA-224.
- ECDSA_
P521_ SHA224_ ASN1_ SIGNING - Signing of ASN.1 DER-encoded ECDSA signatures using the P-521 curve and SHA-224.
- ECDSA_
P521_ SHA224_ FIXED - Verification of fixed-length (PKCS#11 style) ECDSA signatures using the P-521 curve and SHA-224.
- ECDSA_
P521_ SHA224_ FIXED_ SIGNING - Signing of fixed-length (PKCS#11 style) ECDSA signatures using the P-521 curve and SHA-224.
- ECDSA_
P521_ SHA256_ ASN1 - Verification of ASN.1 DER-encoded ECDSA signatures using the P-521 curve and SHA-256.
- ECDSA_
P521_ SHA256_ ASN1_ SIGNING - Signing of ASN.1 DER-encoded ECDSA signatures using the P-521 curve and SHA-256.
- ECDSA_
P521_ SHA256_ FIXED - Verification of fixed-length (PKCS#11 style) ECDSA signatures using the P-521 curve and SHA-256.
- ECDSA_
P521_ SHA256_ FIXED_ SIGNING - Signing of fixed-length (PKCS#11 style) ECDSA signatures using the P-521 curve and SHA-256.
- ECDSA_
P521_ SHA384_ ASN1 - Verification of ASN.1 DER-encoded ECDSA signatures using the P-521 curve and SHA-384.
- ECDSA_
P521_ SHA384_ ASN1_ SIGNING - Signing of ASN.1 DER-encoded ECDSA signatures using the P-521 curve and SHA-384.
- ECDSA_
P521_ SHA384_ FIXED - Verification of fixed-length (PKCS#11 style) ECDSA signatures using the P-521 curve and SHA-384.
- ECDSA_
P521_ SHA384_ FIXED_ SIGNING - Signing of fixed-length (PKCS#11 style) ECDSA signatures using the P-521 curve and SHA-384.
- ECDSA_
P521_ SHA512_ ASN1 - Verification of ASN.1 DER-encoded ECDSA signatures using the P-521 curve and SHA-512.
- ECDSA_
P521_ SHA512_ ASN1_ SIGNING - Signing of ASN.1 DER-encoded ECDSA signatures using the P-521 curve and SHA-512.
- ECDSA_
P521_ SHA512_ FIXED - Verification of fixed-length (PKCS#11 style) ECDSA signatures using the P-521 curve and SHA-512.
- ECDSA_
P521_ SHA512_ FIXED_ SIGNING - Signing of fixed-length (PKCS#11 style) ECDSA signatures using the P-521 curve and SHA-512.
- ED25519
- Verification of Ed25519 signatures.
- RSA_
PKCS1_ 1024_ 8192_ SHA1_ FOR_ LEGACY_ USE_ ONLY - Verification of signatures using RSA keys of 1024-8192 bits, PKCS#1.5 padding, and SHA-1.
- RSA_
PKCS1_ 1024_ 8192_ SHA256_ FOR_ LEGACY_ USE_ ONLY - Verification of signatures using RSA keys of 1024-8192 bits, PKCS#1.5 padding, and SHA-256.
- RSA_
PKCS1_ 1024_ 8192_ SHA512_ FOR_ LEGACY_ USE_ ONLY - Verification of signatures using RSA keys of 1024-8192 bits, PKCS#1.5 padding, and SHA-512.
- RSA_
PKCS1_ 2048_ 8192_ SHA1_ FOR_ LEGACY_ USE_ ONLY - Verification of signatures using RSA keys of 2048-8192 bits, PKCS#1.5 padding, and SHA-1.
- RSA_
PKCS1_ 2048_ 8192_ SHA256 - Verification of signatures using RSA keys of 2048-8192 bits, PKCS#1.5 padding, and SHA-256.
- RSA_
PKCS1_ 2048_ 8192_ SHA384 - Verification of signatures using RSA keys of 2048-8192 bits, PKCS#1.5 padding, and SHA-384.
- RSA_
PKCS1_ 2048_ 8192_ SHA512 - Verification of signatures using RSA keys of 2048-8192 bits, PKCS#1.5 padding, and SHA-512.
- RSA_
PKCS1_ 3072_ 8192_ SHA384 - Verification of signatures using RSA keys of 3072-8192 bits, PKCS#1.5 padding, and SHA-384.
- RSA_
PKCS1_ SHA256 - PKCS#1 1.5 padding using SHA-256 for RSA signatures.
- RSA_
PKCS1_ SHA384 - PKCS#1 1.5 padding using SHA-384 for RSA signatures.
- RSA_
PKCS1_ SHA512 - PKCS#1 1.5 padding using SHA-512 for RSA signatures.
- RSA_
PSS_ 2048_ 8192_ SHA256 - Verification of signatures using RSA keys of 2048-8192 bits, PSS padding, and SHA-256.
- RSA_
PSS_ 2048_ 8192_ SHA384 - Verification of signatures using RSA keys of 2048-8192 bits, PSS padding, and SHA-384.
- RSA_
PSS_ 2048_ 8192_ SHA512 - Verification of signatures using RSA keys of 2048-8192 bits, PSS padding, and SHA-512.
- RSA_
PSS_ SHA256 - RSA PSS padding using SHA-256 for RSA signatures.
- RSA_
PSS_ SHA384 - RSA PSS padding using SHA-384 for RSA signatures.
- RSA_
PSS_ SHA512 - RSA PSS padding using SHA-512 for RSA signatures.
Traits§
- KeyPair
- Key pairs for signing messages (private key and public key).
- RsaEncoding
- An RSA signature encoding as described in RFC 3447 Section 8.
- Verification
Algorithm - A signature verification algorithm.