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 //! Low-level RSA primitives.
21
22 use crate::{
23 arithmetic::bigint,
24 bits, error,
25 io::{self, der},
26 limb,
27 };
28
29 mod bounds;
30 pub(crate) mod padding;
31
32 // Maximum RSA modulus size supported for signature verification (in bytes).
33 const PUBLIC_KEY_PUBLIC_MODULUS_MAX_LEN: usize = bigint::MODULUS_MAX_LIMBS * limb::LIMB_BYTES;
34
35 /// Parameters for RSA verification.
36 #[derive(Debug)]
37 pub struct RsaParameters {
38 padding_alg: &'static dyn padding::Verification,
39 min_bits: bits::BitLength,
40 }
41
42 impl Bounds for RsaParameters {
n_min_bits(&self) -> bits::BitLength43 fn n_min_bits(&self) -> bits::BitLength {
44 self.min_bits
45 }
46
n_max_bits(&self) -> bits::BitLength47 fn n_max_bits(&self) -> bits::BitLength {
48 bits::BitLength::from_usize_bytes(PUBLIC_KEY_PUBLIC_MODULUS_MAX_LEN).unwrap()
49 }
50
e_min_value(&self) -> u6451 fn e_min_value(&self) -> u64 {
52 3
53 }
54 }
55
parse_public_key( input: untrusted::Input, ) -> Result<(io::Positive, io::Positive), error::Unspecified>56 fn parse_public_key(
57 input: untrusted::Input,
58 ) -> Result<(io::Positive, io::Positive), error::Unspecified> {
59 input.read_all(error::Unspecified, |input| {
60 der::nested(input, der::Tag::Sequence, error::Unspecified, |input| {
61 let n = der::positive_integer(input)?;
62 let e = der::positive_integer(input)?;
63 Ok((n, e))
64 })
65 })
66 }
67
68 // Type-level representation of an RSA public modulus *n*. See
69 // `super::bigint`'s modulue-level documentation.
70 #[derive(Copy, Clone)]
71 enum N {}
72
73 unsafe impl bigint::PublicModulus for N {}
74
75 pub(crate) mod keypair;
76 pub(crate) mod public;
77
78 pub(crate) mod verification;
79
80 pub use self::{
81 bounds::Bounds,
82 keypair::{Components as RsaKeyPairComponents, RsaKeyPair},
83 padding::{
84 OaepEncoding, RSA_OAEP_2048_8192_SHA1_FOR_LEGACY_USE_ONLY, RSA_OAEP_2048_8192_SHA256,
85 },
86 public::{Components as RsaPublicKeyComponents, Key as RsaPublicKey},
87 };
88