| 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 | }; |
| 27 | |
| 28 | pub(crate) mod padding; |
| 29 | |
| 30 | // Maximum RSA modulus size supported for signature verification (in bytes). |
| 31 | const PUBLIC_KEY_PUBLIC_MODULUS_MAX_LEN: usize = |
| 32 | bits::BitLength::from_bits(8192).as_usize_bytes_rounded_up(); |
| 33 | |
| 34 | // Keep in sync with the documentation comment for `KeyPair`. |
| 35 | const PRIVATE_KEY_PUBLIC_MODULUS_MAX_BITS: bits::BitLength = bits::BitLength::from_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 | |
| 44 | fn parse_public_key( |
| 45 | input: untrusted::Input, |
| 46 | ) -> Result<(io::Positive, io::Positive), error::Unspecified> { |
| 47 | input.read_all(incomplete_read:error::Unspecified, |input: &mut Reader<'_>| { |
| 48 | der::nested(input, der::Tag::Sequence, error:error::Unspecified, |input: &mut Reader<'_>| { |
| 49 | let n: Positive<'_> = der::positive_integer(input)?; |
| 50 | let e: Positive<'_> = 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 | enum N {} |
| 59 | |
| 60 | impl bigint::PublicModulus for N {} |
| 61 | |
| 62 | mod keypair; |
| 63 | mod keypair_components; |
| 64 | mod public_exponent; |
| 65 | mod public_key; |
| 66 | mod public_key_components; |
| 67 | mod public_modulus; |
| 68 | |
| 69 | pub(crate) mod verification; |
| 70 | |
| 71 | use self::{public_exponent::PublicExponent, public_modulus::PublicModulus}; |
| 72 | |
| 73 | pub use self::{ |
| 74 | keypair::KeyPair, keypair_components::KeyPairComponents, public_key::PublicKey, |
| 75 | public_key_components::PublicKeyComponents, |
| 76 | }; |
| 77 | |