1use super::{parse_public_key, RsaParameters, N, PUBLIC_KEY_PUBLIC_MODULUS_MAX_LEN};
18use crate::{
19 arithmetic::{bigint, montgomery::Unencoded},
20 bits, cpu, digest, error,
21 limb::LIMB_BYTES,
22 sealed, signature,
23};
24
25#[derive(Debug)]
26pub struct Key {
27 pub n: bigint::Modulus<N>,
28 pub e: bigint::PublicExponent,
29 pub n_bits: bits::BitLength,
30}
31
32impl Key {
33 pub fn from_modulus_and_exponent(
34 n: untrusted::Input,
35 e: untrusted::Input,
36 n_min_bits: bits::BitLength,
37 n_max_bits: bits::BitLength,
38 e_min_value: u64,
39 ) -> Result<Self, error::KeyRejected> {
40 let (n, n_bits) = bigint::Modulus::from_be_bytes_with_bit_length(n)?;
51
52 const N_MIN_BITS: bits::BitLength = bits::BitLength::from_usize_bits(1024);
56
57 assert!(n_min_bits >= N_MIN_BITS);
61 let n_bits_rounded_up =
62 bits::BitLength::from_usize_bytes(n_bits.as_usize_bytes_rounded_up())
63 .map_err(|error::Unspecified| error::KeyRejected::unexpected_error())?;
64 if n_bits_rounded_up < n_min_bits {
65 return Err(error::KeyRejected::too_small());
66 }
67 if n_bits > n_max_bits {
68 return Err(error::KeyRejected::too_large());
69 }
70
71 let e = bigint::PublicExponent::from_be_bytes(e, e_min_value)?;
74
75 Ok(Self { n, e, n_bits })
83 }
84}
85
86impl signature::VerificationAlgorithm for RsaParameters {
87 fn verify(
88 &self,
89 public_key: untrusted::Input,
90 msg: untrusted::Input,
91 signature: untrusted::Input,
92 ) -> Result<(), error::Unspecified> {
93 let (n, e) = parse_public_key(public_key)?;
94 verify_rsa_(
95 self,
96 (
97 n.big_endian_without_leading_zero_as_input(),
98 e.big_endian_without_leading_zero_as_input(),
99 ),
100 msg,
101 signature,
102 )
103 }
104}
105
106impl sealed::Sealed for RsaParameters {}
107
108macro_rules! rsa_params {
109 ( $VERIFY_ALGORITHM:ident, $min_bits:expr, $PADDING_ALGORITHM:expr,
110 $doc_str:expr ) => {
111 #[doc=$doc_str]
112 pub static $VERIFY_ALGORITHM: RsaParameters = RsaParameters {
115 padding_alg: $PADDING_ALGORITHM,
116 min_bits: bits::BitLength::from_usize_bits($min_bits),
117 };
118 };
119}
120
121rsa_params!(
122 RSA_PKCS1_1024_8192_SHA1_FOR_LEGACY_USE_ONLY,
123 1024,
124 &super::padding::RSA_PKCS1_SHA1_FOR_LEGACY_USE_ONLY,
125 "Verification of signatures using RSA keys of 1024-8192 bits,
126 PKCS#1.5 padding, and SHA-1.\n\nSee \"`RSA_PKCS1_*` Details\" in
127 `ring::signature`'s module-level documentation for more details."
128);
129rsa_params!(
130 RSA_PKCS1_2048_8192_SHA1_FOR_LEGACY_USE_ONLY,
131 2048,
132 &super::padding::RSA_PKCS1_SHA1_FOR_LEGACY_USE_ONLY,
133 "Verification of signatures using RSA keys of 2048-8192 bits,
134 PKCS#1.5 padding, and SHA-1.\n\nSee \"`RSA_PKCS1_*` Details\" in
135 `ring::signature`'s module-level documentation for more details."
136);
137rsa_params!(
138 RSA_PKCS1_1024_8192_SHA256_FOR_LEGACY_USE_ONLY,
139 1024,
140 &super::RSA_PKCS1_SHA256,
141 "Verification of signatures using RSA keys of 1024-8192 bits,
142 PKCS#1.5 padding, and SHA-256.\n\nSee \"`RSA_PKCS1_*` Details\" in
143 `ring::signature`'s module-level documentation for more details."
144);
145rsa_params!(
146 RSA_PKCS1_2048_8192_SHA256,
147 2048,
148 &super::RSA_PKCS1_SHA256,
149 "Verification of signatures using RSA keys of 2048-8192 bits,
150 PKCS#1.5 padding, and SHA-256.\n\nSee \"`RSA_PKCS1_*` Details\" in
151 `ring::signature`'s module-level documentation for more details."
152);
153rsa_params!(
154 RSA_PKCS1_2048_8192_SHA384,
155 2048,
156 &super::RSA_PKCS1_SHA384,
157 "Verification of signatures using RSA keys of 2048-8192 bits,
158 PKCS#1.5 padding, and SHA-384.\n\nSee \"`RSA_PKCS1_*` Details\" in
159 `ring::signature`'s module-level documentation for more details."
160);
161rsa_params!(
162 RSA_PKCS1_2048_8192_SHA512,
163 2048,
164 &super::RSA_PKCS1_SHA512,
165 "Verification of signatures using RSA keys of 2048-8192 bits,
166 PKCS#1.5 padding, and SHA-512.\n\nSee \"`RSA_PKCS1_*` Details\" in
167 `ring::signature`'s module-level documentation for more details."
168);
169rsa_params!(
170 RSA_PKCS1_1024_8192_SHA512_FOR_LEGACY_USE_ONLY,
171 1024,
172 &super::RSA_PKCS1_SHA512,
173 "Verification of signatures using RSA keys of 1024-8192 bits,
174 PKCS#1.5 padding, and SHA-512.\n\nSee \"`RSA_PKCS1_*` Details\" in
175 `ring::signature`'s module-level documentation for more details."
176);
177rsa_params!(
178 RSA_PKCS1_3072_8192_SHA384,
179 3072,
180 &super::RSA_PKCS1_SHA384,
181 "Verification of signatures using RSA keys of 3072-8192 bits,
182 PKCS#1.5 padding, and SHA-384.\n\nSee \"`RSA_PKCS1_*` Details\" in
183 `ring::signature`'s module-level documentation for more details."
184);
185
186rsa_params!(
187 RSA_PSS_2048_8192_SHA256,
188 2048,
189 &super::RSA_PSS_SHA256,
190 "Verification of signatures using RSA keys of 2048-8192 bits,
191 PSS padding, and SHA-256.\n\nSee \"`RSA_PSS_*` Details\" in
192 `ring::signature`'s module-level documentation for more details."
193);
194rsa_params!(
195 RSA_PSS_2048_8192_SHA384,
196 2048,
197 &super::RSA_PSS_SHA384,
198 "Verification of signatures using RSA keys of 2048-8192 bits,
199 PSS padding, and SHA-384.\n\nSee \"`RSA_PSS_*` Details\" in
200 `ring::signature`'s module-level documentation for more details."
201);
202rsa_params!(
203 RSA_PSS_2048_8192_SHA512,
204 2048,
205 &super::RSA_PSS_SHA512,
206 "Verification of signatures using RSA keys of 2048-8192 bits,
207 PSS padding, and SHA-512.\n\nSee \"`RSA_PSS_*` Details\" in
208 `ring::signature`'s module-level documentation for more details."
209);
210
211#[derive(Debug)]
229pub struct RsaPublicKeyComponents<B: AsRef<[u8]> + core::fmt::Debug> {
230 pub n: B,
232
233 pub e: B,
235}
236
237impl<B: Copy> Copy for RsaPublicKeyComponents<B> where B: AsRef<[u8]> + core::fmt::Debug {}
238
239impl<B: Clone> Clone for RsaPublicKeyComponents<B>
240where
241 B: AsRef<[u8]> + core::fmt::Debug,
242{
243 fn clone(&self) -> Self {
244 Self {
245 n: self.n.clone(),
246 e: self.e.clone(),
247 }
248 }
249}
250
251impl<B> RsaPublicKeyComponents<B>
252where
253 B: AsRef<[u8]> + core::fmt::Debug,
254{
255 pub fn verify(
260 &self,
261 params: &RsaParameters,
262 message: &[u8],
263 signature: &[u8],
264 ) -> Result<(), error::Unspecified> {
265 let _ = cpu::features();
266 verify_rsa_(
267 params,
268 (
269 untrusted::Input::from(self.n.as_ref()),
270 untrusted::Input::from(self.e.as_ref()),
271 ),
272 untrusted::Input::from(message),
273 untrusted::Input::from(signature),
274 )
275 }
276}
277
278pub(crate) fn verify_rsa_(
279 params: &RsaParameters,
280 (n, e): (untrusted::Input, untrusted::Input),
281 msg: untrusted::Input,
282 signature: untrusted::Input,
283) -> Result<(), error::Unspecified> {
284 let max_bits = bits::BitLength::from_usize_bytes(PUBLIC_KEY_PUBLIC_MODULUS_MAX_LEN)?;
285
286 let Key { n, e, n_bits } = Key::from_modulus_and_exponent(n, e, params.min_bits, max_bits, 3)?;
291
292 if signature.len() != n_bits.as_usize_bytes_rounded_up() {
294 return Err(error::Unspecified);
295 }
296
297 let s = bigint::Elem::from_be_bytes_padded(signature, &n)?;
301 if s.is_zero() {
302 return Err(error::Unspecified);
303 }
304
305 let m = bigint::elem_exp_vartime(s, e, &n);
307 let m = m.into_unencoded(&n);
308
309 let mut decoded = [0u8; PUBLIC_KEY_PUBLIC_MODULUS_MAX_LEN];
311 let decoded = fill_be_bytes_n(m, n_bits, &mut decoded);
312
313 let m_hash = digest::digest(params.padding_alg.digest_alg(), msg.as_slice_less_safe());
315 untrusted::Input::from(decoded).read_all(error::Unspecified, |m| {
316 params.padding_alg.verify(&m_hash, m, n_bits)
317 })
318}
319
320fn fill_be_bytes_n(
326 elem: bigint::Elem<N, Unencoded>,
327 n_bits: bits::BitLength,
328 out: &mut [u8; PUBLIC_KEY_PUBLIC_MODULUS_MAX_LEN],
329) -> &[u8] {
330 let n_bytes = n_bits.as_usize_bytes_rounded_up();
331 let n_bytes_padded = ((n_bytes + (LIMB_BYTES - 1)) / LIMB_BYTES) * LIMB_BYTES;
332 let out = &mut out[..n_bytes_padded];
333 elem.fill_be_bytes(out);
334 let (padding, out) = out.split_at(n_bytes_padded - n_bytes);
335 assert!(padding.iter().all(|&b| b == 0));
336 out
337}