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.
21
22 use crate::{
23 arithmetic::bigint,
24 bits, error,
25 io::{self, der},
26 limb,
27 };
28
29 pub(crate) mod padding;
30
31 // Maximum RSA modulus size supported for signature verification (in bytes).
32 const PUBLIC_KEY_PUBLIC_MODULUS_MAX_LEN: usize = bigint::MODULUS_MAX_LIMBS * limb::LIMB_BYTES;
33
34 // Keep in sync with the documentation comment for `KeyPair`.
35 const PRIVATE_KEY_PUBLIC_MODULUS_MAX_BITS: bits::BitLength = bits::BitLength::from_usize_bits(4096);
36
37 /// Parameters for RSA verification.
38 #[derive(Debug)]
39 pub struct RsaParameters {
40 padding_alg: &'static dyn padding::Verification,
41 min_bits: bits::BitLength,
42 }
43
parse_public_key( input: untrusted::Input, ) -> Result<(io::Positive, io::Positive), error::Unspecified>44 fn parse_public_key(
45 input: untrusted::Input,
46 ) -> Result<(io::Positive, io::Positive), error::Unspecified> {
47 input.read_all(error::Unspecified, |input| {
48 der::nested(input, der::Tag::Sequence, error::Unspecified, |input| {
49 let n = der::positive_integer(input)?;
50 let e = der::positive_integer(input)?;
51 Ok((n, e))
52 })
53 })
54 }
55
56 // Type-level representation of an RSA public modulus *n*. See
57 // `super::bigint`'s modulue-level documentation.
58 #[derive(Copy, Clone)]
59 enum N {}
60
61 impl bigint::PublicModulus for N {}
62
63 mod keypair;
64 mod keypair_components;
65 mod public_exponent;
66 mod public_key;
67 mod public_key_components;
68 mod public_modulus;
69
70 pub(crate) mod verification;
71
72 use self::{public_exponent::PublicExponent, public_modulus::PublicModulus};
73
74 pub use self::{
75 keypair::KeyPair, keypair_components::KeyPairComponents, public_key::PublicKey,
76 public_key_components::PublicKeyComponents,
77 };
78