ring/
rsa.rs

1// Copyright 2015-2016 Brian Smith.
2//
3// Permission to use, copy, modify, and/or distribute this software for any
4// purpose with or without fee is hereby granted, provided that the above
5// copyright notice and this permission notice appear in all copies.
6//
7// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
8// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
10// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
15// *R* and *r* in Montgomery math refer to different things, so we always use
16// `R` to refer to *R* to avoid confusion, even when that's against the normal
17// naming conventions. Also the standard camelCase names are used for `KeyPair`
18// components.
19
20/// RSA signatures.
21use crate::{
22    arithmetic::bigint,
23    bits, error,
24    io::{self, der},
25    limb,
26};
27
28mod padding;
29
30// `RSA_PKCS1_SHA1` is intentionally not exposed.
31pub use self::padding::{
32    RsaEncoding, RSA_PKCS1_SHA256, RSA_PKCS1_SHA384, RSA_PKCS1_SHA512, RSA_PSS_SHA256,
33    RSA_PSS_SHA384, RSA_PSS_SHA512,
34};
35
36// Maximum RSA modulus size supported for signature verification (in bytes).
37const PUBLIC_KEY_PUBLIC_MODULUS_MAX_LEN: usize = bigint::MODULUS_MAX_LIMBS * limb::LIMB_BYTES;
38
39// Keep in sync with the documentation comment for `KeyPair`.
40const PRIVATE_KEY_PUBLIC_MODULUS_MAX_BITS: bits::BitLength = bits::BitLength::from_usize_bits(4096);
41
42/// Parameters for RSA verification.
43#[derive(Debug)]
44pub struct RsaParameters {
45    padding_alg: &'static dyn padding::Verification,
46    min_bits: bits::BitLength,
47}
48
49fn parse_public_key(
50    input: untrusted::Input,
51) -> Result<(io::Positive, io::Positive), error::Unspecified> {
52    input.read_all(error::Unspecified, |input| {
53        der::nested(input, der::Tag::Sequence, error::Unspecified, |input| {
54            let n = der::positive_integer(input)?;
55            let e = der::positive_integer(input)?;
56            Ok((n, e))
57        })
58    })
59}
60
61// Type-level representation of an RSA public modulus *n*. See
62// `super::bigint`'s modulue-level documentation.
63#[derive(Copy, Clone)]
64pub enum N {}
65
66unsafe impl bigint::PublicModulus for N {}
67
68pub mod verification;
69
70pub mod signing;