1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#![allow(non_snake_case)]
use {bits, der, error};
use untrusted;
#[cfg(feature = "rsa_signing")]
use limb;
mod padding;
#[cfg(feature = "rsa_signing")]
pub use self::padding::RSAEncoding;
pub use self::padding::{
RSA_PKCS1_SHA256,
RSA_PKCS1_SHA384,
RSA_PKCS1_SHA512,
RSA_PSS_SHA256,
RSA_PSS_SHA384,
RSA_PSS_SHA512
};
const PUBLIC_KEY_PUBLIC_MODULUS_MAX_LEN: usize = 8192 / 8;
#[cfg(feature = "rsa_signing")]
const PRIVATE_KEY_PUBLIC_MODULUS_MAX_BITS: bits::BitLength =
bits::BitLength(4096);
#[cfg(feature = "rsa_signing")]
const PRIVATE_KEY_PUBLIC_MODULUS_MAX_LIMBS: usize =
(4096 + limb::LIMB_BITS - 1) / limb::LIMB_BITS;
pub struct RSAParameters {
padding_alg: &'static padding::RSAVerification,
min_bits: bits::BitLength,
id: RSAParametersID,
}
#[allow(non_camel_case_types)]
enum RSAParametersID {
RSA_PKCS1_2048_8192_SHA1,
RSA_PKCS1_2048_8192_SHA256,
RSA_PKCS1_2048_8192_SHA384,
RSA_PKCS1_2048_8192_SHA512,
RSA_PKCS1_3072_8192_SHA384,
RSA_PSS_2048_8192_SHA256,
RSA_PSS_2048_8192_SHA384,
RSA_PSS_2048_8192_SHA512,
}
fn parse_public_key(input: untrusted::Input)
-> Result<(untrusted::Input, untrusted::Input),
error::Unspecified> {
input.read_all(error::Unspecified, |input| {
der::nested(input, der::Tag::Sequence, error::Unspecified, |input| {
let n = der::positive_integer(input)?;
let e = der::positive_integer(input)?;
Ok((n, e))
})
})
}
fn check_public_modulus_and_exponent(
n: bigint::Positive, e: bigint::Positive, n_min_bits: bits::BitLength,
n_max_bits: bits::BitLength, e_min_bits: bits::BitLength)
-> Result<(bigint::OddPositive, bigint::PublicExponent),
error::Unspecified> {
let n = n.into_odd_positive()?;
let e = e.into_odd_positive()?;
const N_MIN_BITS: bits::BitLength = bits::BitLength(2048);
assert!(n_min_bits >= N_MIN_BITS);
let n_bits = n.bit_length();
let n_bits_rounded_up =
bits::BitLength::from_usize_bytes(n_bits.as_usize_bytes_rounded_up())?;
if n_bits_rounded_up < n_min_bits {
return Err(error::Unspecified);
}
if n_bits > n_max_bits {
return Err(error::Unspecified);
}
debug_assert!(e_min_bits >= bits::BitLength::from_usize_bits(2));
let e_bits = e.bit_length();
if e_bits < e_min_bits {
return Err(error::Unspecified);
}
let e = e.into_public_exponent()?;
debug_assert!(e_min_bits < bigint::PUBLIC_EXPONENT_MAX_BITS);
debug_assert!(bigint::PUBLIC_EXPONENT_MAX_BITS < N_MIN_BITS);
Ok((n, e))
}
pub enum N {}
pub mod verification;
#[cfg(feature = "rsa_signing")]
pub mod signing;
mod bigint;
#[cfg(feature = "rsa_signing")]
mod blinding;
#[cfg(feature = "rsa_signing")]
mod random;