Module ring::signature [] [src]

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.

Examples

Signing and verifying with Ed25519

extern crate ring;
extern crate untrusted;

use ring::{rand, signature};

// Generate a key pair.
let rng = rand::SystemRandom::new();
let (generated, generated_bytes) =
    try!(signature::Ed25519KeyPair::generate_serializable(&rng));

// Normally after generating the key pair, the application would extract
// the private and public components and store them persistently for future
// use.

// Normally the application would later deserialize the private and public
// key from storage and then create an `Ed25519KeyPair` from the
// deserialized bytes.
let key_pair =
   try!(signature::Ed25519KeyPair::from_bytes(&generated_bytes.private_key,
                                              &generated_bytes.public_key));

// Sign the message "hello, world".
const MESSAGE: &'static [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 use the
// public key from the private key we just generated.
let peer_public_key_bytes = &generated_bytes.public_key;
let sig_bytes = sig.as_slice();

// Verify the signature of the message using the public key. Normally the
// verifier of the message would parse the inputs to `signature::verify`
// out of the protocol message(s) sent by the signer.
let peer_public_key = untrusted::Input::from(peer_public_key_bytes);
let msg = untrusted::Input::from(MESSAGE);
let sig = untrusted::Input::from(sig_bytes);

try!(signature::verify(&signature::ED25519, peer_public_key, msg, sig));

Signing and verifying with RSA (PKCS#1 1.5 padding)

RSA signing (but not verification) requires the rsa_signing feature to be enabled.

extern crate ring;
extern crate untrusted;

use ring::{rand, signature};


// Create an `RSAKeyPair` from the DER-encoded bytes. This example uses
// a 2048-bit key, but larger keys are also supported.
let key_bytes_der =
   untrusted::Input::from(
        include_bytes!("src/rsa/signature_rsa_example_private_key.der"));
let key_pair =
   try!(signature::RSAKeyPair::from_der(key_bytes_der));

// Create a signing state.
let key_pair = std::sync::Arc::new(key_pair);
let mut signing_state = try!(signature::RSASigningState::new(key_pair));

// 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; signing_state.key_pair().public_modulus_len()];
try!(signing_state.sign(&signature::RSA_PKCS1_SHA256, &rng, MESSAGE,
                        &mut signature));

// Verify the signature.
let public_key_bytes_der =
    untrusted::Input::from(
        include_bytes!("src/rsa/signature_rsa_example_public_key.der"));
let message = untrusted::Input::from(MESSAGE);
let signature = untrusted::Input::from(&signature);
try!(signature::verify(&signature::RSA_PKCS1_2048_8192_SHA256,
                       public_key_bytes_der, message, signature));

Modules

primitive

Lower-level verification primitives. Usage of ring::signature::verify() is preferred when the public key and signature are encoded in standard formats, as it also handles the parsing.

Structs

ECDSAParameters

Parameters for ECDSA signing and verification.

Ed25519KeyPair

An Ed25519 key pair, for signing.

Ed25519KeyPairBytes

The raw bytes of the Ed25519 key pair, for serialization.

EdDSAParameters

Parameters for EdDSA signing and verification.

RSAParameters

Parameters for RSA signing and verification.

Signature

A public key signature returned from a signing operation.

Statics

ECDSA_P256_SHA1_ASN1

Verification of ECDSA signatures using the P-256 curve and SHA-1.

ECDSA_P256_SHA256_ASN1

Verification of ECDSA signatures using the P-256 curve and SHA-256.

ECDSA_P256_SHA384_ASN1

Verification of ECDSA signatures using the P-256 curve and SHA-384.

ECDSA_P256_SHA512_ASN1

Verification of ECDSA signatures using the P-256 curve and SHA-512.

ECDSA_P384_SHA1_ASN1

Verification of ECDSA signatures using the P-384 curve and SHA-1.

ECDSA_P384_SHA256_ASN1

Verification of ECDSA signatures using the P-384 curve and SHA-256.

ECDSA_P384_SHA384_ASN1

Verification of ECDSA signatures using the P-384 curve and SHA-384.

ECDSA_P384_SHA512_ASN1

Verification of ECDSA signatures using the P-384 curve and SHA-512.

ED25519

Verification of Ed25519 signatures.

RSA_PKCS1_2048_8192_SHA1

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.

Traits

VerificationAlgorithm

A signature verification algorithm.

Functions

verify

Verify the signature signature of message msg with the public key public_key using the algorithm alg.