2016-08-17 23:20:01 -10:00
|
|
|
// Copyright 2015-2016 Brian Smith.
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
2018-11-13 11:41:36 -10:00
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
|
2016-08-17 23:20:01 -10:00
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
|
|
|
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
|
|
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
2018-11-15 15:37:51 -10:00
|
|
|
use super::{
|
|
|
|
bigint::{self, Prime},
|
|
|
|
verification, N,
|
|
|
|
};
|
|
|
|
use arithmetic::montgomery::R;
|
2016-08-17 23:20:01 -10:00
|
|
|
/// RSA PKCS#1 1.5 signatures.
|
2018-11-27 15:13:26 -10:00
|
|
|
use crate::{
|
|
|
|
bits, der, digest,
|
|
|
|
error::{self, KeyRejected},
|
|
|
|
pkcs8, rand,
|
|
|
|
};
|
2016-08-17 23:20:01 -10:00
|
|
|
use std;
|
|
|
|
use untrusted;
|
|
|
|
|
|
|
|
/// An RSA key pair, used for signing. Feature: `rsa_signing`.
|
2016-08-18 09:30:50 -10:00
|
|
|
///
|
|
|
|
/// After constructing an `RSAKeyPair`, construct one or more
|
|
|
|
/// `RSASigningState`s that reference the `RSAKeyPair` and use
|
|
|
|
/// `RSASigningState::sign()` to generate signatures. See `ring::signature`'s
|
|
|
|
/// module-level documentation for an example.
|
2018-05-28 12:50:55 -10:00
|
|
|
pub struct KeyPair {
|
2017-02-20 12:03:27 -10:00
|
|
|
p: PrivatePrime<P>,
|
|
|
|
q: PrivatePrime<Q>,
|
2016-12-27 23:23:26 -10:00
|
|
|
qInv: bigint::Elem<P, R>,
|
2016-11-28 20:17:25 -10:00
|
|
|
qq: bigint::Modulus<QQ>,
|
2016-12-27 23:23:26 -10:00
|
|
|
q_mod_n: bigint::Elem<N, R>,
|
2018-05-28 10:32:08 -10:00
|
|
|
public_key: verification::Key,
|
2016-08-17 23:20:01 -10:00
|
|
|
}
|
|
|
|
|
2018-10-08 09:55:09 +00:00
|
|
|
derive_debug_via_self!(KeyPair, self.public_key);
|
2018-05-28 10:43:05 -10:00
|
|
|
|
2018-05-28 12:50:55 -10:00
|
|
|
impl KeyPair {
|
2017-04-29 14:28:25 -10:00
|
|
|
/// Parses an unencrypted PKCS#8-encoded RSA private key.
|
2016-08-17 23:20:01 -10:00
|
|
|
///
|
2017-04-29 14:28:25 -10:00
|
|
|
/// Only two-prime (not multi-prime) keys are supported. The public modulus
|
|
|
|
/// (n) must be at least 2047 bits. The public modulus must be no larger
|
|
|
|
/// than 4096 bits. It is recommended that the public modulus be exactly
|
|
|
|
/// 2048 or 3072 bits. The public exponent must be at least 65537.
|
2016-08-17 23:20:01 -10:00
|
|
|
///
|
2017-04-29 14:28:25 -10:00
|
|
|
/// This will generate a 2048-bit RSA private key of the correct form using
|
|
|
|
/// OpenSSL's command line tool:
|
2016-08-17 23:20:01 -10:00
|
|
|
///
|
|
|
|
/// ```sh
|
2017-04-29 14:28:25 -10:00
|
|
|
/// openssl genpkey -algorithm RSA \
|
|
|
|
/// -pkeyopt rsa_keygen_bits:2048 \
|
|
|
|
/// -pkeyopt rsa_keygen_pubexp:65537 | \
|
|
|
|
/// openssl pkcs8 -topk8 -nocrypt -outform der > rsa-2048-private-key.pk8
|
2016-08-17 23:20:01 -10:00
|
|
|
/// ```
|
|
|
|
///
|
2017-04-29 14:28:25 -10:00
|
|
|
/// This will generate a 3072-bit RSA private key of the correct form:
|
2016-08-17 23:20:01 -10:00
|
|
|
///
|
|
|
|
/// ```sh
|
2017-04-29 14:28:25 -10:00
|
|
|
/// openssl genpkey -algorithm RSA \
|
|
|
|
/// -pkeyopt rsa_keygen_bits:2048 \
|
|
|
|
/// -pkeyopt rsa_keygen_pubexp:65537 | \
|
|
|
|
/// openssl pkcs8 -topk8 -nocrypt -outform der > rsa-2048-private-key.pk8
|
2016-08-17 23:20:01 -10:00
|
|
|
/// ```
|
|
|
|
///
|
2017-04-29 14:28:25 -10:00
|
|
|
/// Often, keys generated for use in OpenSSL-based software are stored in
|
|
|
|
/// the Base64 “PEM” format without the PKCS#8 wrapper. Such keys can be
|
|
|
|
/// converted to binary PKCS#8 form using the OpenSSL command line tool like
|
|
|
|
/// this:
|
|
|
|
///
|
|
|
|
/// ```sh
|
|
|
|
/// openssl pkcs8 -topk8 -nocrypt -outform der \
|
|
|
|
/// -in rsa-2048-private-key.pem > rsa-2048-private-key.pk8
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Base64 (“PEM”) PKCS#8-encoded keys can be converted to the binary PKCS#8
|
|
|
|
/// form like this:
|
|
|
|
///
|
|
|
|
/// ```sh
|
|
|
|
/// openssl pkcs8 -nocrypt -outform der \
|
|
|
|
/// -in rsa-2048-private-key.pem > rsa-2048-private-key.pk8
|
|
|
|
/// ```
|
2016-08-17 23:20:01 -10:00
|
|
|
///
|
2016-12-23 14:44:55 -08:00
|
|
|
/// The private key is validated according to [NIST SP-800-56B rev. 1]
|
|
|
|
/// section 6.4.1.4.3, crt_pkv (Intended Exponent-Creation Method Unknown),
|
|
|
|
/// with the following exceptions:
|
2017-03-19 10:14:09 -10:00
|
|
|
///
|
|
|
|
/// * Section 6.4.1.2.1, Step 1: Neither a target security level nor an
|
2016-12-23 14:44:55 -08:00
|
|
|
/// expected modulus length is provided as a parameter, so checks
|
|
|
|
/// regarding these expectations are not done.
|
2017-03-19 10:14:09 -10:00
|
|
|
/// * Section 6.4.1.2.1, Step 3: Since neither the public key nor the
|
2016-12-23 14:44:55 -08:00
|
|
|
/// expected modulus length is provided as a parameter, the consistency
|
2018-11-15 15:37:51 -10:00
|
|
|
/// check between these values and the private key's value of n isn't
|
|
|
|
/// done.
|
2017-03-19 10:14:09 -10:00
|
|
|
/// * Section 6.4.1.2.1, Step 5: No primality tests are done, both for
|
2016-12-23 14:44:55 -08:00
|
|
|
/// performance reasons and to avoid any side channels that such tests
|
|
|
|
/// would provide.
|
2017-03-19 10:14:09 -10:00
|
|
|
/// * Section 6.4.1.2.1, Step 6, and 6.4.1.4.3, Step 7:
|
|
|
|
/// * *ring* has a slightly looser lower bound for the values of `p`
|
2017-02-22 13:36:21 -10:00
|
|
|
/// and `q` than what the NIST document specifies. This looser lower
|
|
|
|
/// bound matches what most other crypto libraries do. The check might
|
2018-05-09 13:53:39 -10:00
|
|
|
/// be tightened to meet NIST's requirements in the future. Similarly,
|
|
|
|
/// the check that `p` and `q` are not too close together is skipped
|
|
|
|
/// currently, but may be added in the future.
|
2017-02-19 17:56:33 -08:00
|
|
|
/// - The validity of the mathematical relationship of `dP`, `dQ`, `e`
|
2017-02-22 13:36:21 -10:00
|
|
|
/// and `n` is verified only during signing. Some size checks of `d`,
|
|
|
|
/// `dP` and `dQ` are performed at construction, but some NIST checks
|
|
|
|
/// are skipped because they would be expensive and/or they would leak
|
|
|
|
/// information through side channels. If a preemptive check of the
|
|
|
|
/// consistency of `dP`, `dQ`, `e` and `n` with each other is
|
|
|
|
/// necessary, that can be done by signing any message with the key
|
2017-02-19 17:56:33 -08:00
|
|
|
/// pair.
|
2017-03-19 10:14:09 -10:00
|
|
|
///
|
|
|
|
/// * `d` is not fully validated, neither at construction nor during
|
2017-02-22 13:36:21 -10:00
|
|
|
/// signing. This is OK as far as *ring*'s usage of the key is
|
|
|
|
/// concerned because *ring* never uses the value of `d` (*ring* always
|
|
|
|
/// uses `p`, `q`, `dP` and `dQ` via the Chinese Remainder Theorem,
|
|
|
|
/// instead). However, *ring*'s checks would not be sufficient for
|
|
|
|
/// validating a key pair for use by some other system; that other
|
|
|
|
/// system must check the value of `d` itself if `d` is to be used.
|
2016-12-23 14:44:55 -08:00
|
|
|
///
|
2017-02-22 13:36:21 -10:00
|
|
|
/// In addition to the NIST requirements, *ring* requires that `p > q` and
|
|
|
|
/// that `e` must be no more than 33 bits.
|
2016-12-23 14:44:55 -08:00
|
|
|
///
|
2017-04-29 14:28:25 -10:00
|
|
|
/// See [RFC 5958] and [RFC 3447 Appendix A.1.2] for more details of the
|
|
|
|
/// encoding of the key.
|
|
|
|
///
|
|
|
|
/// [NIST SP-800-56B rev. 1]:
|
|
|
|
/// http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-56Br1.pdf
|
|
|
|
///
|
|
|
|
/// [RFC 3447 Appendix A.1.2]:
|
|
|
|
/// https://tools.ietf.org/html/rfc3447#appendix-A.1.2
|
|
|
|
///
|
|
|
|
/// [RFC 5958]:
|
|
|
|
/// https://tools.ietf.org/html/rfc5958
|
2018-11-27 15:13:26 -10:00
|
|
|
pub fn from_pkcs8(input: untrusted::Input) -> Result<Self, KeyRejected> {
|
2018-11-15 15:37:51 -10:00
|
|
|
const RSA_ENCRYPTION: &[u8] = include_bytes!("../data/alg-rsa-encryption.der");
|
|
|
|
let (der, _) = pkcs8::unwrap_key_(&RSA_ENCRYPTION, pkcs8::Version::V1Only, input)?;
|
2017-04-29 14:28:25 -10:00
|
|
|
Self::from_der(der)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses an RSA private key that is not inside a PKCS#8 wrapper.
|
|
|
|
///
|
|
|
|
/// The private key must be encoded as a binary DER-encoded ASN.1
|
|
|
|
/// `RSAPrivateKey` as described in [RFC 3447 Appendix A.1.2]). In all other
|
2018-05-28 12:50:55 -10:00
|
|
|
/// respects, this is just like `from_pkcs8()`. See the documentation for
|
|
|
|
/// `from_pkcs8()` for more details.
|
2017-04-29 14:28:25 -10:00
|
|
|
///
|
2018-05-28 12:50:55 -10:00
|
|
|
/// It is recommended to use `from_pkcs8()` (with a PKCS#8-encoded key)
|
|
|
|
/// instead.
|
2017-04-29 14:28:25 -10:00
|
|
|
///
|
2016-08-17 23:20:01 -10:00
|
|
|
/// [RFC 3447 Appendix A.1.2]:
|
|
|
|
/// https://tools.ietf.org/html/rfc3447#appendix-A.1.2
|
2016-12-23 14:44:55 -08:00
|
|
|
///
|
|
|
|
/// [NIST SP-800-56B rev. 1]:
|
|
|
|
/// http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-56Br1.pdf
|
2018-11-27 15:13:26 -10:00
|
|
|
pub fn from_der(input: untrusted::Input) -> Result<Self, KeyRejected> {
|
|
|
|
input.read_all(KeyRejected::invalid_encoding(), |input| {
|
2018-11-27 14:33:03 -10:00
|
|
|
der::nested(
|
|
|
|
input,
|
|
|
|
der::Tag::Sequence,
|
2018-11-27 15:13:26 -10:00
|
|
|
error::KeyRejected::invalid_encoding(),
|
2018-11-27 14:33:03 -10:00
|
|
|
Self::from_der_reader,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-11-27 15:13:26 -10:00
|
|
|
fn from_der_reader(input: &mut untrusted::Reader) -> Result<Self, KeyRejected> {
|
|
|
|
let version = der::small_nonnegative_integer(input)
|
|
|
|
.map_err(|error::Unspecified| KeyRejected::invalid_encoding())?;
|
2018-11-27 14:33:03 -10:00
|
|
|
if version != 0 {
|
2018-11-27 15:13:26 -10:00
|
|
|
return Err(KeyRejected::version_not_supported());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn positive_integer<'a>(
|
|
|
|
input: &mut untrusted::Reader<'a>,
|
|
|
|
) -> Result<untrusted::Input<'a>, KeyRejected> {
|
|
|
|
der::positive_integer(input)
|
|
|
|
.map_err(|error::Unspecified| KeyRejected::invalid_encoding())
|
2018-11-27 14:33:03 -10:00
|
|
|
}
|
2018-11-27 15:13:26 -10:00
|
|
|
|
|
|
|
let n = positive_integer(input)?;
|
|
|
|
let e = positive_integer(input)?;
|
|
|
|
let d = positive_integer(input)?;
|
|
|
|
let p = positive_integer(input)?;
|
|
|
|
let q = positive_integer(input)?;
|
|
|
|
let dP = positive_integer(input)?;
|
|
|
|
let dQ = positive_integer(input)?;
|
|
|
|
let qInv = positive_integer(input)?;
|
|
|
|
|
|
|
|
let (p, p_bits) = bigint::Nonnegative::from_be_bytes_with_bit_length(p)
|
|
|
|
.map_err(|error::Unspecified| KeyRejected::invalid_encoding())?;
|
|
|
|
let (q, q_bits) = bigint::Nonnegative::from_be_bytes_with_bit_length(q)
|
|
|
|
.map_err(|error::Unspecified| KeyRejected::invalid_encoding())?;
|
2018-11-27 14:33:03 -10:00
|
|
|
|
2018-11-27 15:27:55 -10:00
|
|
|
// Our implementation of CRT-based modular exponentiation used requires
|
|
|
|
// that `p > q` so swap them if `p < q`. If swapped, `qInv` is
|
|
|
|
// recalculated below. `p != q` is verified implicitly below, e.g. when
|
|
|
|
// `q_mod_p` is constructed.
|
2018-11-27 14:33:03 -10:00
|
|
|
let ((p, p_bits, dP), (q, q_bits, dQ, qInv)) = match q.verify_less_than(&p) {
|
|
|
|
Ok(_) => ((p, p_bits, dP), (q, q_bits, dQ, Some(qInv))),
|
2018-11-27 15:13:26 -10:00
|
|
|
Err(error::Unspecified) => {
|
2018-11-27 14:33:03 -10:00
|
|
|
// TODO: verify `q` and `qInv` are inverses (mod p).
|
|
|
|
((q, q_bits, dQ), (p, p_bits, dP, None))
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-11-27 15:27:55 -10:00
|
|
|
// XXX: Some steps are done out of order, but the NIST steps are worded
|
|
|
|
// in such a way that it is clear that NIST intends for them to be done
|
|
|
|
// in order. TODO: Does this matter at all?
|
2018-11-27 14:33:03 -10:00
|
|
|
|
|
|
|
// 6.4.1.4.3/6.4.1.2.1 - Step 1.
|
|
|
|
|
|
|
|
// Step 1.a is omitted, as explained above.
|
|
|
|
|
2018-11-27 15:27:55 -10:00
|
|
|
// Step 1.b is omitted per above. Instead, we check that the public
|
|
|
|
// modulus is 2048 to `PRIVATE_KEY_PUBLIC_MODULUS_MAX_BITS` bits.
|
|
|
|
// XXX: The maximum limit of 4096 bits is primarily due to lack of
|
|
|
|
// testing of larger key sizes; see, in particular,
|
2018-11-27 14:33:03 -10:00
|
|
|
// https://www.mail-archive.com/openssl-dev@openssl.org/msg44586.html
|
|
|
|
// and
|
|
|
|
// https://www.mail-archive.com/openssl-dev@openssl.org/msg44759.html.
|
2018-11-27 15:27:55 -10:00
|
|
|
// Also, this limit might help with memory management decisions later.
|
2018-11-27 14:33:03 -10:00
|
|
|
|
|
|
|
// Step 1.c. We validate e >= 65537.
|
|
|
|
let public_key = verification::Key::from_modulus_and_exponent(
|
|
|
|
n,
|
|
|
|
e,
|
|
|
|
bits::BitLength::from_usize_bits(2048),
|
|
|
|
super::PRIVATE_KEY_PUBLIC_MODULUS_MAX_BITS,
|
|
|
|
65537,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// 6.4.1.4.3 says to skip 6.4.1.2.1 Step 2.
|
|
|
|
|
|
|
|
// 6.4.1.4.3 Step 3.
|
|
|
|
|
|
|
|
// Step 3.a is done below, out of order.
|
|
|
|
// Step 3.b is unneeded since `n_bits` is derived here from `n`.
|
|
|
|
|
2018-11-27 15:27:55 -10:00
|
|
|
// 6.4.1.4.3 says to skip 6.4.1.2.1 Step 4. (We don't need to recover
|
|
|
|
// the prime factors since they are already given.)
|
2018-11-27 14:33:03 -10:00
|
|
|
|
|
|
|
// 6.4.1.4.3 - Step 5.
|
|
|
|
|
|
|
|
// Steps 5.a and 5.b are omitted, as explained above.
|
|
|
|
|
|
|
|
// Step 5.c.
|
|
|
|
//
|
|
|
|
// TODO: First, stop if `p < (√2) * 2**((nBits/2) - 1)`.
|
|
|
|
//
|
|
|
|
// Second, stop if `p > 2**(nBits/2) - 1`.
|
|
|
|
let half_n_bits = public_key.n_bits.half_rounded_up();
|
|
|
|
if p_bits != half_n_bits {
|
2018-11-27 15:13:26 -10:00
|
|
|
return Err(KeyRejected::inconsistent_components());
|
2018-11-27 14:33:03 -10:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Step 5.d: Verify GCD(p - 1, e) == 1.
|
|
|
|
|
|
|
|
// Steps 5.e and 5.f are omitted as explained above.
|
|
|
|
|
|
|
|
// Step 5.g.
|
|
|
|
//
|
|
|
|
// TODO: First, stop if `q < (√2) * 2**((nBits/2) - 1)`.
|
|
|
|
//
|
|
|
|
// Second, stop if `q > 2**(nBits/2) - 1`.
|
|
|
|
if p_bits != q_bits {
|
2018-11-27 15:13:26 -10:00
|
|
|
return Err(KeyRejected::inconsistent_components());
|
2018-11-27 14:33:03 -10:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Step 5.h: Verify GCD(p - 1, e) == 1.
|
|
|
|
|
2018-11-27 15:13:26 -10:00
|
|
|
let q_mod_n_decoded = q
|
|
|
|
.to_elem(&public_key.n)
|
|
|
|
.map_err(|error::Unspecified| KeyRejected::inconsistent_components())?;
|
2018-11-27 14:33:03 -10:00
|
|
|
|
|
|
|
// TODO: Step 5.i
|
|
|
|
//
|
|
|
|
// 3.b is unneeded since `n_bits` is derived here from `n`.
|
|
|
|
|
|
|
|
// 6.4.1.4.3 - Step 3.a (out of order).
|
|
|
|
//
|
|
|
|
// Verify that p * q == n. We restrict ourselves to modular
|
|
|
|
// multiplication. We rely on the fact that we've verified
|
2018-11-27 15:27:55 -10:00
|
|
|
// 0 < q < p < n. We check that q and p are close to sqrt(n) and then
|
|
|
|
// assume that these preconditions are enough to let us assume that
|
|
|
|
// checking p * q == 0 (mod n) is equivalent to checking p * q == n.
|
2018-11-27 14:33:03 -10:00
|
|
|
let q_mod_n = bigint::elem_mul(
|
|
|
|
public_key.n.oneRR().as_ref(),
|
|
|
|
q_mod_n_decoded.clone(),
|
|
|
|
&public_key.n,
|
|
|
|
);
|
2018-11-27 15:13:26 -10:00
|
|
|
let p_mod_n = p
|
|
|
|
.to_elem(&public_key.n)
|
|
|
|
.map_err(|error::Unspecified| KeyRejected::inconsistent_components())?;
|
2018-11-27 14:33:03 -10:00
|
|
|
let pq_mod_n = bigint::elem_mul(&q_mod_n, p_mod_n, &public_key.n);
|
|
|
|
if !pq_mod_n.is_zero() {
|
2018-11-27 15:13:26 -10:00
|
|
|
return Err(KeyRejected::inconsistent_components());
|
2018-11-27 14:33:03 -10:00
|
|
|
}
|
|
|
|
|
|
|
|
// 6.4.1.4.3/6.4.1.2.1 - Step 6.
|
|
|
|
|
|
|
|
// Step 6.a, partial.
|
|
|
|
//
|
2018-11-27 15:27:55 -10:00
|
|
|
// First, validate `2**half_n_bits < d`. Since 2**half_n_bits has a bit
|
|
|
|
// length of half_n_bits + 1, this check gives us 2**half_n_bits <= d,
|
|
|
|
// and knowing d is odd makes the inequality strict.
|
2018-11-27 15:13:26 -10:00
|
|
|
let (d, d_bits) = bigint::Nonnegative::from_be_bytes_with_bit_length(d)
|
|
|
|
.map_err(|_| error::KeyRejected::invalid_encoding())?;
|
2018-11-27 14:33:03 -10:00
|
|
|
if !(half_n_bits < d_bits) {
|
2018-11-27 15:13:26 -10:00
|
|
|
return Err(KeyRejected::inconsistent_components());
|
2018-11-27 14:33:03 -10:00
|
|
|
}
|
2018-11-27 15:27:55 -10:00
|
|
|
// XXX: This check should be `d < LCM(p - 1, q - 1)`, but we don't have
|
|
|
|
// a good way of calculating LCM, so it is omitted, as explained above.
|
2018-11-27 15:13:26 -10:00
|
|
|
d.verify_less_than_modulus(&public_key.n)
|
|
|
|
.map_err(|error::Unspecified| KeyRejected::inconsistent_components())?;
|
2018-11-27 14:33:03 -10:00
|
|
|
if !d.is_odd() {
|
2018-11-27 15:13:26 -10:00
|
|
|
return Err(KeyRejected::invalid_component());
|
2018-11-27 14:33:03 -10:00
|
|
|
}
|
|
|
|
|
|
|
|
// Step 6.b is omitted as explained above.
|
|
|
|
|
|
|
|
// 6.4.1.4.3 - Step 7.
|
|
|
|
|
|
|
|
// Step 7.a.
|
|
|
|
let p = PrivatePrime::new(p, dP)?;
|
|
|
|
|
|
|
|
// Step 7.b.
|
|
|
|
let q = PrivatePrime::new(q, dQ)?;
|
|
|
|
|
|
|
|
let q_mod_p = q.modulus.to_elem(&p.modulus);
|
|
|
|
|
|
|
|
// Step 7.c.
|
|
|
|
let qInv = if let Some(qInv) = qInv {
|
2018-11-27 15:13:26 -10:00
|
|
|
bigint::Elem::from_be_bytes_padded(qInv, &p.modulus)
|
|
|
|
.map_err(|error::Unspecified| KeyRejected::invalid_component())?
|
2018-11-27 14:33:03 -10:00
|
|
|
} else {
|
2018-11-27 15:27:55 -10:00
|
|
|
// We swapped `p` and `q` above, so we need to calculate `qInv`.
|
|
|
|
// Step 7.f below will verify `qInv` is correct.
|
2018-11-27 14:33:03 -10:00
|
|
|
let q_mod_p = bigint::elem_mul(p.modulus.oneRR().as_ref(), q_mod_p.clone(), &p.modulus);
|
2018-11-27 15:13:26 -10:00
|
|
|
bigint::elem_inverse_consttime(q_mod_p, &p.modulus)
|
|
|
|
.map_err(|error::Unspecified| KeyRejected::unexpected_error())?
|
2018-11-27 14:33:03 -10:00
|
|
|
};
|
|
|
|
|
2018-11-27 15:27:55 -10:00
|
|
|
// Steps 7.d and 7.e are omitted per the documentation above, and
|
|
|
|
// because we don't (in the long term) have a good way to do modulo
|
|
|
|
// with an even modulus.
|
2018-11-27 14:33:03 -10:00
|
|
|
|
|
|
|
// Step 7.f.
|
|
|
|
let qInv = bigint::elem_mul(p.modulus.oneRR().as_ref(), qInv, &p.modulus);
|
2018-11-27 15:13:26 -10:00
|
|
|
bigint::verify_inverses_consttime(&qInv, q_mod_p, &p.modulus)
|
|
|
|
.map_err(|error::Unspecified| KeyRejected::inconsistent_components())?;
|
2018-11-27 14:33:03 -10:00
|
|
|
|
|
|
|
let qq = bigint::elem_mul(&q_mod_n, q_mod_n_decoded, &public_key.n).into_modulus::<QQ>()?;
|
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
p,
|
|
|
|
q,
|
|
|
|
qInv,
|
|
|
|
q_mod_n,
|
|
|
|
qq,
|
|
|
|
public_key,
|
2016-08-17 23:20:01 -10:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the length in bytes of the key pair's public modulus.
|
|
|
|
///
|
|
|
|
/// A signature has the same length as the public modulus.
|
2018-11-15 15:37:51 -10:00
|
|
|
pub fn public_modulus_len(&self) -> usize { self.public_key.modulus_len() }
|
2016-08-18 09:30:50 -10:00
|
|
|
}
|
|
|
|
|
2017-02-20 12:03:27 -10:00
|
|
|
struct PrivatePrime<M: Prime> {
|
|
|
|
modulus: bigint::Modulus<M>,
|
2018-05-07 08:34:32 -10:00
|
|
|
exponent: bigint::PrivateExponent<M>,
|
2017-02-20 12:03:27 -10:00
|
|
|
}
|
|
|
|
|
2018-05-06 09:35:21 -10:00
|
|
|
impl<M: Prime + Clone> PrivatePrime<M> {
|
2017-02-20 12:03:27 -10:00
|
|
|
/// Constructs a `PrivatePrime` from the private prime `p` and `dP` where
|
|
|
|
/// dP == d % (p - 1).
|
2018-11-27 15:13:26 -10:00
|
|
|
fn new(p: bigint::Nonnegative, dP: untrusted::Input) -> Result<Self, KeyRejected> {
|
2018-05-10 18:28:43 -10:00
|
|
|
let p = bigint::Modulus::from(p)?;
|
2018-05-07 08:34:32 -10:00
|
|
|
|
2017-02-20 12:03:27 -10:00
|
|
|
// [NIST SP-800-56B rev. 1] 6.4.1.4.3 - Steps 7.a & 7.b.
|
2018-11-27 15:13:26 -10:00
|
|
|
let dP = bigint::PrivateExponent::from_be_bytes_padded(dP, &p)
|
|
|
|
.map_err(|error::Unspecified| KeyRejected::invalid_component())?;
|
2017-02-20 12:03:27 -10:00
|
|
|
|
|
|
|
// XXX: Steps 7.d and 7.e are omitted. We don't check that
|
|
|
|
// `dP == d % (p - 1)` because we don't (in the long term) have a good
|
|
|
|
// way to do modulo with an even modulus. Instead we just check that
|
|
|
|
// `1 <= dP < p - 1`. We'll check it, to some unknown extent, when we
|
|
|
|
// do the private key operation, since we verify that the result of the
|
|
|
|
// private key operation using the CRT parameters is consistent with `n`
|
|
|
|
// and `e`. TODO: Either prove that what we do is sufficient, or make
|
|
|
|
// it so.
|
|
|
|
|
2018-11-13 19:07:26 -10:00
|
|
|
Ok(PrivatePrime {
|
2017-02-20 12:03:27 -10:00
|
|
|
modulus: p,
|
|
|
|
exponent: dP,
|
2018-11-13 19:07:26 -10:00
|
|
|
})
|
2017-02-20 12:03:27 -10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-15 15:37:51 -10:00
|
|
|
fn elem_exp_consttime<M, MM>(
|
|
|
|
c: &bigint::Elem<MM>, p: &PrivatePrime<M>,
|
|
|
|
) -> Result<bigint::Elem<M>, error::Unspecified>
|
|
|
|
where
|
|
|
|
M: bigint::NotMuchSmallerModulus<MM>,
|
|
|
|
M: Prime,
|
|
|
|
{
|
2017-05-17 21:35:53 -10:00
|
|
|
let c_mod_m = bigint::elem_reduced(c, &p.modulus)?;
|
2018-11-13 19:07:26 -10:00
|
|
|
// We could precompute `oneRRR = elem_squared(&p.oneRR`) as mentioned
|
|
|
|
// in the Smooth CRT-RSA paper.
|
2018-11-13 20:08:46 -10:00
|
|
|
let c_mod_m = bigint::elem_mul(p.modulus.oneRR().as_ref(), c_mod_m, &p.modulus);
|
|
|
|
let c_mod_m = bigint::elem_mul(p.modulus.oneRR().as_ref(), c_mod_m, &p.modulus);
|
|
|
|
bigint::elem_exp_consttime(c_mod_m, &p.exponent, &p.modulus)
|
2017-02-20 12:03:27 -10:00
|
|
|
}
|
|
|
|
|
2016-12-30 20:55:13 -10:00
|
|
|
// Type-level representations of the different moduli used in RSA signing, in
|
|
|
|
// addition to `super::N`. See `super::bigint`'s modulue-level documentation.
|
2016-12-24 06:24:51 -10:00
|
|
|
|
2018-05-06 09:35:21 -10:00
|
|
|
#[derive(Copy, Clone)]
|
2016-11-26 00:47:38 -10:00
|
|
|
enum P {}
|
2017-02-20 12:03:27 -10:00
|
|
|
unsafe impl Prime for P {}
|
2016-12-24 06:24:51 -10:00
|
|
|
unsafe impl bigint::SmallerModulus<N> for P {}
|
|
|
|
unsafe impl bigint::NotMuchSmallerModulus<N> for P {}
|
2016-11-28 02:16:59 -10:00
|
|
|
|
2018-05-06 09:35:21 -10:00
|
|
|
#[derive(Copy, Clone)]
|
2016-12-24 06:24:51 -10:00
|
|
|
enum QQ {}
|
|
|
|
unsafe impl bigint::SmallerModulus<N> for QQ {}
|
|
|
|
unsafe impl bigint::NotMuchSmallerModulus<N> for QQ {}
|
2016-11-26 00:47:38 -10:00
|
|
|
|
2016-12-28 20:04:34 -10:00
|
|
|
// `q < p < 2*q` since `q` is slightly smaller than `p` (see below). Thus:
|
|
|
|
//
|
|
|
|
// q < p < 2*q
|
|
|
|
// q*q < p*q < 2*q*q.
|
|
|
|
// q**2 < n < 2*(q**2).
|
|
|
|
unsafe impl bigint::SlightlySmallerModulus<N> for QQ {}
|
|
|
|
|
2018-05-06 09:35:21 -10:00
|
|
|
#[derive(Copy, Clone)]
|
2016-12-24 06:24:51 -10:00
|
|
|
enum Q {}
|
2017-02-20 12:03:27 -10:00
|
|
|
unsafe impl Prime for Q {}
|
2016-12-24 06:24:51 -10:00
|
|
|
unsafe impl bigint::SmallerModulus<N> for Q {}
|
|
|
|
unsafe impl bigint::SmallerModulus<P> for Q {}
|
2016-12-28 20:04:34 -10:00
|
|
|
|
|
|
|
// q < p && `p.bit_length() == q.bit_length()` implies `q < p < 2*q`.
|
|
|
|
unsafe impl bigint::SlightlySmallerModulus<P> for Q {}
|
|
|
|
|
2016-12-24 06:24:51 -10:00
|
|
|
unsafe impl bigint::SmallerModulus<QQ> for Q {}
|
|
|
|
unsafe impl bigint::NotMuchSmallerModulus<QQ> for Q {}
|
2016-11-12 12:04:32 -10:00
|
|
|
|
2016-08-18 09:30:50 -10:00
|
|
|
/// State used for RSA Signing. Feature: `rsa_signing`.
|
2018-05-18 17:27:11 -10:00
|
|
|
//
|
|
|
|
// TODO: Remove this; it's not needed if we don't have RSA blinding.
|
2018-05-28 12:50:55 -10:00
|
|
|
pub struct SigningState {
|
|
|
|
key_pair: std::sync::Arc<KeyPair>,
|
2016-08-18 09:30:50 -10:00
|
|
|
}
|
|
|
|
|
2018-05-28 12:50:55 -10:00
|
|
|
impl SigningState {
|
|
|
|
/// Construct a signing state appropriate for use with the given key pair.
|
2018-11-15 15:37:51 -10:00
|
|
|
pub fn new(key_pair: std::sync::Arc<KeyPair>) -> Result<Self, error::Unspecified> {
|
|
|
|
Ok(SigningState { key_pair })
|
2016-08-18 09:30:50 -10:00
|
|
|
}
|
|
|
|
|
2018-05-28 12:50:55 -10:00
|
|
|
/// The key pair. This can be used, for example, to access the key pair's
|
|
|
|
/// public key.
|
|
|
|
pub fn key_pair(&self) -> &KeyPair { self.key_pair.as_ref() }
|
2016-08-17 23:20:01 -10:00
|
|
|
|
|
|
|
/// Sign `msg`. `msg` is digested using the digest algorithm from
|
|
|
|
/// `padding_alg` and the digest is then padded using the padding algorithm
|
|
|
|
/// from `padding_alg`. The signature it written into `signature`;
|
|
|
|
/// `signature`'s length must be exactly the length returned by
|
2018-05-18 17:27:11 -10:00
|
|
|
/// `public_modulus_len()`. `rng` may be used to randomize the padding
|
|
|
|
/// (e.g. for PSS).
|
2016-08-17 23:20:01 -10:00
|
|
|
///
|
|
|
|
/// Many other crypto libraries have signing functions that takes a
|
|
|
|
/// precomputed digest as input, instead of the message to digest. This
|
|
|
|
/// function does *not* take a precomputed digest; instead, `sign`
|
|
|
|
/// calculates the digest itself.
|
|
|
|
///
|
|
|
|
/// Lots of effort has been made to make the signing operations close to
|
|
|
|
/// constant time to protect the private key from side channel attacks. On
|
|
|
|
/// x86-64, this is done pretty well, but not perfectly. On other
|
2018-05-18 17:27:11 -10:00
|
|
|
/// platforms, it is done less perfectly.
|
2018-11-15 15:37:51 -10:00
|
|
|
pub fn sign(
|
|
|
|
&mut self, padding_alg: &'static ::signature::RSAEncoding, rng: &rand::SecureRandom,
|
|
|
|
msg: &[u8], signature: &mut [u8],
|
|
|
|
) -> Result<(), error::Unspecified> {
|
2018-05-28 10:32:08 -10:00
|
|
|
let mod_bits = self.key_pair.public_key.n_bits;
|
2016-11-14 11:13:43 -10:00
|
|
|
if signature.len() != mod_bits.as_usize_bytes_rounded_up() {
|
2016-08-17 23:20:01 -10:00
|
|
|
return Err(error::Unspecified);
|
|
|
|
}
|
|
|
|
|
2018-05-28 12:50:55 -10:00
|
|
|
let SigningState { key_pair: key } = self;
|
2016-12-10 14:13:35 -10:00
|
|
|
|
2016-11-23 11:35:30 -10:00
|
|
|
let m_hash = digest::digest(padding_alg.digest_alg(), msg);
|
2017-05-17 21:35:53 -10:00
|
|
|
padding_alg.encode(&m_hash, signature, mod_bits, rng)?;
|
2016-12-24 06:24:51 -10:00
|
|
|
|
|
|
|
// RFC 8017 Section 5.1.2: RSADP, using the Chinese Remainder Theorem
|
|
|
|
// with Garner's algorithm.
|
|
|
|
|
2018-05-28 10:32:08 -10:00
|
|
|
let n = &key.public_key.n;
|
|
|
|
|
2016-12-24 06:24:51 -10:00
|
|
|
// Step 1. The value zero is also rejected.
|
2018-11-15 15:37:51 -10:00
|
|
|
let base = bigint::Elem::from_be_bytes_padded(untrusted::Input::from(signature), n)?;
|
2016-11-29 14:39:12 -10:00
|
|
|
|
2018-05-18 17:27:11 -10:00
|
|
|
// Step 2
|
|
|
|
let c = base;
|
|
|
|
|
|
|
|
// Step 2.b.i.
|
|
|
|
let m_1 = elem_exp_consttime(&c, &key.p)?;
|
|
|
|
let c_mod_qq = bigint::elem_reduced_once(&c, &key.qq);
|
|
|
|
let m_2 = elem_exp_consttime(&c_mod_qq, &key.q)?;
|
|
|
|
|
|
|
|
// Step 2.b.ii isn't needed since there are only two primes.
|
|
|
|
|
|
|
|
// Step 2.b.iii.
|
|
|
|
let p = &key.p.modulus;
|
2018-05-18 20:50:33 -10:00
|
|
|
let m_2 = bigint::elem_widen(m_2, p);
|
2018-05-18 17:27:11 -10:00
|
|
|
let m_1_minus_m_2 = bigint::elem_sub(m_1, &m_2, p);
|
|
|
|
let h = bigint::elem_mul(&key.qInv, m_1_minus_m_2, p);
|
|
|
|
|
|
|
|
// Step 2.b.iv. The reduction in the modular multiplication isn't
|
|
|
|
// necessary because `h < p` and `p * q == n` implies `h * q < n`.
|
|
|
|
// Modular arithmetic is used simply to avoid implementing
|
|
|
|
// non-modular arithmetic.
|
2018-05-28 10:32:08 -10:00
|
|
|
let h = bigint::elem_widen(h, n);
|
|
|
|
let q_times_h = bigint::elem_mul(&key.q_mod_n, h, n);
|
|
|
|
let m_2 = bigint::elem_widen(m_2, n);
|
|
|
|
let m = bigint::elem_add(m_2, q_times_h, n);
|
2018-05-18 17:27:11 -10:00
|
|
|
|
|
|
|
// Step 2.b.v isn't needed since there are only two primes.
|
|
|
|
|
|
|
|
// Verify the result to protect against fault attacks as described
|
|
|
|
// in "On the Importance of Checking Cryptographic Protocols for
|
|
|
|
// Faults" by Dan Boneh, Richard A. DeMillo, and Richard J. Lipton.
|
2018-05-28 12:50:55 -10:00
|
|
|
// This check is cheap assuming `e` is small, which is ensured during
|
|
|
|
// `KeyPair` construction. Note that this is the only validation of `e`
|
|
|
|
// that is done other than basic checks on its size, oddness, and
|
|
|
|
// minimum value, since the relationship of `e` to `d`, `p`, and `q` is
|
|
|
|
// not verified during `KeyPair` construction.
|
2018-05-18 17:27:11 -10:00
|
|
|
{
|
2018-11-15 15:37:51 -10:00
|
|
|
let verify = bigint::elem_exp_vartime(m.clone(), key.public_key.e, n);
|
2018-05-28 10:32:08 -10:00
|
|
|
let verify = verify.into_unencoded(n);
|
2017-05-17 21:35:53 -10:00
|
|
|
bigint::elem_verify_equal_consttime(&verify, &c)?;
|
2018-05-18 17:27:11 -10:00
|
|
|
}
|
2016-12-20 19:41:04 -10:00
|
|
|
|
2018-05-18 17:27:11 -10:00
|
|
|
// Step 3.
|
2018-05-18 20:57:58 -10:00
|
|
|
//
|
|
|
|
// See Falko Strenzke, "Manger's Attack revisited", ICICS 2010.
|
2018-05-18 17:27:11 -10:00
|
|
|
m.fill_be_bytes(signature);
|
2017-03-19 11:45:38 -10:00
|
|
|
|
|
|
|
Ok(())
|
2016-08-17 23:20:01 -10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2016-11-14 15:58:16 -10:00
|
|
|
// We intentionally avoid `use super::*` so that we are sure to use only
|
|
|
|
// the public API; this ensures that enough of the API is public.
|
2018-11-09 13:56:51 -10:00
|
|
|
use crate::{rand, signature};
|
2016-08-17 23:20:01 -10:00
|
|
|
use std;
|
|
|
|
use untrusted;
|
|
|
|
|
2018-05-28 12:50:55 -10:00
|
|
|
// `KeyPair::sign` requires that the output buffer is the same length as
|
2016-08-17 23:20:01 -10:00
|
|
|
// the public key modulus. Test what happens when it isn't the same length.
|
|
|
|
#[test]
|
|
|
|
fn test_signature_rsa_pkcs1_sign_output_buffer_len() {
|
|
|
|
// Sign the message "hello, world", using PKCS#1 v1.5 padding and the
|
|
|
|
// SHA256 digest algorithm.
|
2018-11-03 13:32:36 -10:00
|
|
|
const MESSAGE: &[u8] = b"hello, world";
|
2016-08-17 23:20:01 -10:00
|
|
|
let rng = rand::SystemRandom::new();
|
|
|
|
|
|
|
|
const PRIVATE_KEY_DER: &'static [u8] =
|
|
|
|
include_bytes!("signature_rsa_example_private_key.der");
|
|
|
|
let key_bytes_der = untrusted::Input::from(PRIVATE_KEY_DER);
|
2016-11-14 15:58:16 -10:00
|
|
|
let key_pair = signature::RSAKeyPair::from_der(key_bytes_der).unwrap();
|
2016-08-18 09:30:50 -10:00
|
|
|
let key_pair = std::sync::Arc::new(key_pair);
|
2018-11-15 15:37:51 -10:00
|
|
|
let mut signing_state = signature::RSASigningState::new(key_pair).unwrap();
|
2016-08-17 23:20:01 -10:00
|
|
|
|
|
|
|
// The output buffer is one byte too short.
|
2018-11-15 15:37:51 -10:00
|
|
|
let mut signature = vec![0; signing_state.key_pair().public_modulus_len() - 1];
|
2016-08-18 09:30:50 -10:00
|
|
|
|
2018-11-15 15:37:51 -10:00
|
|
|
assert!(signing_state
|
|
|
|
.sign(&signature::RSA_PKCS1_SHA256, &rng, MESSAGE, &mut signature)
|
|
|
|
.is_err());
|
2016-08-17 23:20:01 -10:00
|
|
|
|
|
|
|
// The output buffer is the right length.
|
|
|
|
signature.push(0);
|
2018-11-15 15:37:51 -10:00
|
|
|
assert!(signing_state
|
|
|
|
.sign(&signature::RSA_PKCS1_SHA256, &rng, MESSAGE, &mut signature)
|
|
|
|
.is_ok());
|
2016-08-17 23:20:01 -10:00
|
|
|
|
|
|
|
// The output buffer is one byte too long.
|
|
|
|
signature.push(0);
|
2018-11-15 15:37:51 -10:00
|
|
|
assert!(signing_state
|
|
|
|
.sign(&signature::RSA_PKCS1_SHA256, &rng, MESSAGE, &mut signature)
|
|
|
|
.is_err());
|
2016-08-17 23:20:01 -10:00
|
|
|
}
|
|
|
|
}
|