/src/boringssl/crypto/fipsmodule/rsa/rsa.cc.inc
Line | Count | Source |
1 | | // Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include <openssl/rsa.h> |
16 | | |
17 | | #include <assert.h> |
18 | | #include <limits.h> |
19 | | #include <string.h> |
20 | | |
21 | | #include <iterator> |
22 | | |
23 | | #include <openssl/bn.h> |
24 | | #include <openssl/digest.h> |
25 | | #include <openssl/engine.h> |
26 | | #include <openssl/err.h> |
27 | | #include <openssl/ex_data.h> |
28 | | #include <openssl/md5.h> |
29 | | #include <openssl/mem.h> |
30 | | #include <openssl/nid.h> |
31 | | |
32 | | #include "../../internal.h" |
33 | | #include "../../mem_internal.h" |
34 | | #include "../bcm_interface.h" |
35 | | #include "../bn/internal.h" |
36 | | #include "../delocate.h" |
37 | | #include "internal.h" |
38 | | |
39 | | |
40 | | using namespace bssl; |
41 | | |
42 | | // RSA_R_BLOCK_TYPE_IS_NOT_02 is part of the legacy SSLv23 padding scheme. |
43 | | // Cryptography.io depends on this error code. |
44 | | OPENSSL_DECLARE_ERROR_REASON(RSA, BLOCK_TYPE_IS_NOT_02) |
45 | | |
46 | | DEFINE_STATIC_EX_DATA_CLASS(g_rsa_ex_data_class) |
47 | | |
48 | 0 | static int bn_dup_into(UniquePtr<BIGNUM> *dst, const BIGNUM *src) { |
49 | 0 | if (src == nullptr) { |
50 | 0 | OPENSSL_PUT_ERROR(RSA, ERR_R_PASSED_NULL_PARAMETER); |
51 | 0 | return 0; |
52 | 0 | } |
53 | | |
54 | 0 | dst->reset(BN_dup(src)); |
55 | 0 | return *dst != nullptr; |
56 | 0 | } |
57 | | |
58 | 0 | RSA *RSA_new_public_key(const BIGNUM *n, const BIGNUM *e) { |
59 | 0 | RSAImpl *rsa = FromOpaque(RSA_new()); |
60 | 0 | if (rsa == nullptr || // |
61 | 0 | !bn_dup_into(&rsa->n, n) || // |
62 | 0 | !bn_dup_into(&rsa->e, e) || // |
63 | 0 | !RSA_check_key(rsa)) { |
64 | 0 | RSA_free(rsa); |
65 | 0 | return nullptr; |
66 | 0 | } |
67 | | |
68 | 0 | return rsa; |
69 | 0 | } |
70 | | |
71 | | RSA *RSA_new_private_key(const BIGNUM *n, const BIGNUM *e, const BIGNUM *d, |
72 | | const BIGNUM *p, const BIGNUM *q, const BIGNUM *dmp1, |
73 | 0 | const BIGNUM *dmq1, const BIGNUM *iqmp) { |
74 | 0 | RSAImpl *rsa = FromOpaque(RSA_new()); |
75 | 0 | if (rsa == nullptr || // |
76 | 0 | !bn_dup_into(&rsa->n, n) || // |
77 | 0 | !bn_dup_into(&rsa->e, e) || // |
78 | 0 | !bn_dup_into(&rsa->d, d) || // |
79 | 0 | !bn_dup_into(&rsa->p, p) || // |
80 | 0 | !bn_dup_into(&rsa->q, q) || // |
81 | 0 | !bn_dup_into(&rsa->dmp1, dmp1) || // |
82 | 0 | !bn_dup_into(&rsa->dmq1, dmq1) || // |
83 | 0 | !bn_dup_into(&rsa->iqmp, iqmp) || // |
84 | 0 | !RSA_check_key(rsa)) { |
85 | 0 | RSA_free(rsa); |
86 | 0 | return nullptr; |
87 | 0 | } |
88 | | |
89 | 0 | return rsa; |
90 | 0 | } |
91 | | |
92 | | RSA *RSA_new_private_key_no_crt(const BIGNUM *n, const BIGNUM *e, |
93 | 0 | const BIGNUM *d) { |
94 | 0 | RSAImpl *rsa = FromOpaque(RSA_new()); |
95 | 0 | if (rsa == nullptr || // |
96 | 0 | !bn_dup_into(&rsa->n, n) || // |
97 | 0 | !bn_dup_into(&rsa->e, e) || // |
98 | 0 | !bn_dup_into(&rsa->d, d) || // |
99 | 0 | !RSA_check_key(rsa)) { |
100 | 0 | RSA_free(rsa); |
101 | 0 | return nullptr; |
102 | 0 | } |
103 | | |
104 | 0 | return rsa; |
105 | 0 | } |
106 | | |
107 | 0 | RSA *RSA_new_private_key_no_e(const BIGNUM *n, const BIGNUM *d) { |
108 | 0 | RSAImpl *rsa = FromOpaque(RSA_new()); |
109 | 0 | if (rsa == nullptr) { |
110 | 0 | return nullptr; |
111 | 0 | } |
112 | | |
113 | 0 | rsa->flags |= RSA_FLAG_NO_PUBLIC_EXPONENT; |
114 | 0 | if (!bn_dup_into(&rsa->n, n) || // |
115 | 0 | !bn_dup_into(&rsa->d, d) || // |
116 | 0 | !RSA_check_key(rsa)) { |
117 | 0 | RSA_free(rsa); |
118 | 0 | return nullptr; |
119 | 0 | } |
120 | | |
121 | 0 | return rsa; |
122 | 0 | } |
123 | | |
124 | 0 | RSA *RSA_new_public_key_large_e(const BIGNUM *n, const BIGNUM *e) { |
125 | 0 | RSAImpl *rsa = FromOpaque(RSA_new()); |
126 | 0 | if (rsa == nullptr) { |
127 | 0 | return nullptr; |
128 | 0 | } |
129 | | |
130 | 0 | rsa->flags |= RSA_FLAG_LARGE_PUBLIC_EXPONENT; |
131 | 0 | if (!bn_dup_into(&rsa->n, n) || // |
132 | 0 | !bn_dup_into(&rsa->e, e) || // |
133 | 0 | !RSA_check_key(rsa)) { |
134 | 0 | RSA_free(rsa); |
135 | 0 | return nullptr; |
136 | 0 | } |
137 | | |
138 | 0 | return rsa; |
139 | 0 | } |
140 | | |
141 | | RSA *RSA_new_private_key_large_e(const BIGNUM *n, const BIGNUM *e, |
142 | | const BIGNUM *d, const BIGNUM *p, |
143 | | const BIGNUM *q, const BIGNUM *dmp1, |
144 | 0 | const BIGNUM *dmq1, const BIGNUM *iqmp) { |
145 | 0 | RSAImpl *rsa = FromOpaque(RSA_new()); |
146 | 0 | if (rsa == nullptr) { |
147 | 0 | return nullptr; |
148 | 0 | } |
149 | | |
150 | 0 | rsa->flags |= RSA_FLAG_LARGE_PUBLIC_EXPONENT; |
151 | 0 | if (!bn_dup_into(&rsa->n, n) || // |
152 | 0 | !bn_dup_into(&rsa->e, e) || // |
153 | 0 | !bn_dup_into(&rsa->d, d) || // |
154 | 0 | !bn_dup_into(&rsa->p, p) || // |
155 | 0 | !bn_dup_into(&rsa->q, q) || // |
156 | 0 | !bn_dup_into(&rsa->dmp1, dmp1) || // |
157 | 0 | !bn_dup_into(&rsa->dmq1, dmq1) || // |
158 | 0 | !bn_dup_into(&rsa->iqmp, iqmp) || // |
159 | 0 | !RSA_check_key(rsa)) { |
160 | 0 | RSA_free(rsa); |
161 | 0 | return nullptr; |
162 | 0 | } |
163 | | |
164 | 0 | return rsa; |
165 | 0 | } |
166 | | |
167 | | RSAImpl::RSAImpl(const ENGINE *engine) |
168 | 135k | : RefCounted(CheckSubClass()), |
169 | 135k | meth(engine ? ENGINE_get_RSA_method(engine) : nullptr) { |
170 | 135k | if (meth == nullptr) { |
171 | 135k | meth = const_cast<RSA_METHOD *>(RSA_default_method()); |
172 | 135k | } |
173 | 135k | METHOD_ref(meth); |
174 | 135k | flags = meth->flags; |
175 | 135k | CRYPTO_new_ex_data(&ex_data); |
176 | 135k | } |
177 | | |
178 | 135k | RSA *RSA_new() { return RSA_new_method(nullptr); } |
179 | | |
180 | 135k | RSA *RSA_new_method(const ENGINE *engine) { |
181 | 135k | UniquePtr<RSAImpl> rsa(New<RSAImpl>(engine)); |
182 | 135k | if (rsa == nullptr) { |
183 | 0 | return nullptr; |
184 | 0 | } |
185 | | |
186 | 135k | if (rsa->meth->init && !rsa->meth->init(rsa.get())) { |
187 | 0 | METHOD_unref(rsa->meth); |
188 | 0 | rsa->meth = nullptr; |
189 | 0 | return nullptr; |
190 | 0 | } |
191 | | |
192 | 135k | return rsa.release(); |
193 | 135k | } |
194 | | |
195 | 0 | RSA *RSA_new_method_no_e(const ENGINE *engine, const BIGNUM *n) { |
196 | 0 | RSAImpl *rsa = FromOpaque(RSA_new_method(engine)); |
197 | 0 | if (rsa == nullptr || !bn_dup_into(&rsa->n, n)) { |
198 | 0 | RSA_free(rsa); |
199 | 0 | return nullptr; |
200 | 0 | } |
201 | 0 | rsa->flags |= RSA_FLAG_NO_PUBLIC_EXPONENT; |
202 | 0 | return rsa; |
203 | 0 | } |
204 | | |
205 | 135k | RSAImpl::~RSAImpl() { |
206 | 135k | if (meth != nullptr && meth->finish != nullptr) { |
207 | 0 | meth->finish(this); |
208 | 0 | } |
209 | 135k | METHOD_unref(meth); |
210 | | |
211 | 135k | CRYPTO_free_ex_data(g_rsa_ex_data_class_bss_get(), &ex_data); |
212 | 135k | } |
213 | | |
214 | 162k | void RSA_free(RSA *rsa) { |
215 | 162k | if (rsa != nullptr) { |
216 | 135k | FromOpaque(rsa)->DecRefInternal(); |
217 | 135k | } |
218 | 162k | } |
219 | | |
220 | 0 | int RSA_up_ref(RSA *rsa) { |
221 | 0 | FromOpaque(rsa)->UpRefInternal(); |
222 | 0 | return 1; |
223 | 0 | } |
224 | | |
225 | 15.6k | unsigned RSA_bits(const RSA *rsa) { |
226 | 15.6k | return BN_num_bits(FromOpaque(rsa)->n.get()); |
227 | 15.6k | } |
228 | | |
229 | 23.9k | const BIGNUM *RSA_get0_n(const RSA *rsa) { return FromOpaque(rsa)->n.get(); } |
230 | | |
231 | 23.4k | const BIGNUM *RSA_get0_e(const RSA *rsa) { return FromOpaque(rsa)->e.get(); } |
232 | | |
233 | 4 | const BIGNUM *RSA_get0_d(const RSA *rsa) { return FromOpaque(rsa)->d.get(); } |
234 | | |
235 | 2 | const BIGNUM *RSA_get0_p(const RSA *rsa) { return FromOpaque(rsa)->p.get(); } |
236 | | |
237 | 2 | const BIGNUM *RSA_get0_q(const RSA *rsa) { return FromOpaque(rsa)->q.get(); } |
238 | | |
239 | 2 | const BIGNUM *RSA_get0_dmp1(const RSA *rsa) { |
240 | 2 | return FromOpaque(rsa)->dmp1.get(); |
241 | 2 | } |
242 | | |
243 | 2 | const BIGNUM *RSA_get0_dmq1(const RSA *rsa) { |
244 | 2 | return FromOpaque(rsa)->dmq1.get(); |
245 | 2 | } |
246 | | |
247 | 2 | const BIGNUM *RSA_get0_iqmp(const RSA *rsa) { |
248 | 2 | return FromOpaque(rsa)->iqmp.get(); |
249 | 2 | } |
250 | | |
251 | | void RSA_get0_key(const RSA *rsa, const BIGNUM **out_n, const BIGNUM **out_e, |
252 | 0 | const BIGNUM **out_d) { |
253 | 0 | auto *impl = FromOpaque(rsa); |
254 | 0 | if (out_n != nullptr) { |
255 | 0 | *out_n = impl->n.get(); |
256 | 0 | } |
257 | 0 | if (out_e != nullptr) { |
258 | 0 | *out_e = impl->e.get(); |
259 | 0 | } |
260 | 0 | if (out_d != nullptr) { |
261 | 0 | *out_d = impl->d.get(); |
262 | 0 | } |
263 | 0 | } |
264 | | |
265 | | void RSA_get0_factors(const RSA *rsa, const BIGNUM **out_p, |
266 | 0 | const BIGNUM **out_q) { |
267 | 0 | auto *impl = FromOpaque(rsa); |
268 | 0 | if (out_p != nullptr) { |
269 | 0 | *out_p = impl->p.get(); |
270 | 0 | } |
271 | 0 | if (out_q != nullptr) { |
272 | 0 | *out_q = impl->q.get(); |
273 | 0 | } |
274 | 0 | } |
275 | | |
276 | | void RSA_get0_crt_params(const RSA *rsa, const BIGNUM **out_dmp1, |
277 | 0 | const BIGNUM **out_dmq1, const BIGNUM **out_iqmp) { |
278 | 0 | auto *impl = FromOpaque(rsa); |
279 | 0 | if (out_dmp1 != nullptr) { |
280 | 0 | *out_dmp1 = impl->dmp1.get(); |
281 | 0 | } |
282 | 0 | if (out_dmq1 != nullptr) { |
283 | 0 | *out_dmq1 = impl->dmq1.get(); |
284 | 0 | } |
285 | 0 | if (out_iqmp != nullptr) { |
286 | 0 | *out_iqmp = impl->iqmp.get(); |
287 | 0 | } |
288 | 0 | } |
289 | | |
290 | 0 | int RSA_set0_key(RSA *rsa, BIGNUM *n, BIGNUM *e, BIGNUM *d) { |
291 | 0 | auto *impl = FromOpaque(rsa); |
292 | |
|
293 | 0 | if ((impl->n == nullptr && n == nullptr) || |
294 | 0 | (impl->e == nullptr && e == nullptr)) { |
295 | 0 | return 0; |
296 | 0 | } |
297 | | |
298 | 0 | if (n != nullptr) { |
299 | 0 | impl->n.reset(n); |
300 | 0 | } |
301 | 0 | if (e != nullptr) { |
302 | 0 | impl->e.reset(e); |
303 | 0 | } |
304 | 0 | if (d != nullptr) { |
305 | 0 | impl->d.reset(d); |
306 | 0 | } |
307 | |
|
308 | 0 | rsa_invalidate_key(rsa); |
309 | 0 | return 1; |
310 | 0 | } |
311 | | |
312 | 0 | int RSA_set0_factors(RSA *rsa, BIGNUM *p, BIGNUM *q) { |
313 | 0 | auto *impl = FromOpaque(rsa); |
314 | |
|
315 | 0 | if ((impl->p == nullptr && p == nullptr) || |
316 | 0 | (impl->q == nullptr && q == nullptr)) { |
317 | 0 | return 0; |
318 | 0 | } |
319 | | |
320 | 0 | if (p != nullptr) { |
321 | 0 | impl->p.reset(p); |
322 | 0 | } |
323 | 0 | if (q != nullptr) { |
324 | 0 | impl->q.reset(q); |
325 | 0 | } |
326 | |
|
327 | 0 | rsa_invalidate_key(rsa); |
328 | 0 | return 1; |
329 | 0 | } |
330 | | |
331 | 0 | int RSA_set0_crt_params(RSA *rsa, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp) { |
332 | 0 | auto *impl = FromOpaque(rsa); |
333 | |
|
334 | 0 | if ((impl->dmp1 == nullptr && dmp1 == nullptr) || |
335 | 0 | (impl->dmq1 == nullptr && dmq1 == nullptr) || |
336 | 0 | (impl->iqmp == nullptr && iqmp == nullptr)) { |
337 | 0 | return 0; |
338 | 0 | } |
339 | | |
340 | 0 | if (dmp1 != nullptr) { |
341 | 0 | impl->dmp1.reset(dmp1); |
342 | 0 | } |
343 | 0 | if (dmq1 != nullptr) { |
344 | 0 | impl->dmq1.reset(dmq1); |
345 | 0 | } |
346 | 0 | if (iqmp != nullptr) { |
347 | 0 | impl->iqmp.reset(iqmp); |
348 | 0 | } |
349 | |
|
350 | 0 | rsa_invalidate_key(rsa); |
351 | 0 | return 1; |
352 | 0 | } |
353 | | |
354 | | static int rsa_sign_raw_no_self_test(RSA *rsa, size_t *out_len, uint8_t *out, |
355 | | size_t max_out, const uint8_t *in, |
356 | 18.7k | size_t in_len, int padding) { |
357 | 18.7k | auto *impl = FromOpaque(rsa); |
358 | | |
359 | 18.7k | if (impl->meth->sign_raw) { |
360 | 0 | return impl->meth->sign_raw(rsa, out_len, out, max_out, in, in_len, |
361 | 0 | padding); |
362 | 0 | } |
363 | | |
364 | 18.7k | return rsa_default_sign_raw(rsa, out_len, out, max_out, in, in_len, padding); |
365 | 18.7k | } |
366 | | |
367 | | int RSA_sign_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out, |
368 | 12.9k | const uint8_t *in, size_t in_len, int padding) { |
369 | 12.9k | boringssl_ensure_rsa_sign_self_test(); |
370 | 12.9k | return rsa_sign_raw_no_self_test(rsa, out_len, out, max_out, in, in_len, |
371 | 12.9k | padding); |
372 | 12.9k | } |
373 | | |
374 | 210k | unsigned RSA_size(const RSA *rsa) { |
375 | 210k | return BN_num_bytes(FromOpaque(rsa)->n.get()); |
376 | 210k | } |
377 | | |
378 | 11.4k | int RSA_is_opaque(const RSA *rsa) { |
379 | 11.4k | auto *impl = FromOpaque(rsa); |
380 | 11.4k | return impl->meth && (impl->meth->flags & RSA_FLAG_OPAQUE); |
381 | 11.4k | } |
382 | | |
383 | | int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused, |
384 | 0 | CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) { |
385 | 0 | return CRYPTO_get_ex_new_index_ex(g_rsa_ex_data_class_bss_get(), argl, argp, |
386 | 0 | free_func); |
387 | 0 | } |
388 | | |
389 | 0 | int RSA_set_ex_data(RSA *rsa, int idx, void *arg) { |
390 | 0 | auto *impl = FromOpaque(rsa); |
391 | 0 | return CRYPTO_set_ex_data(&impl->ex_data, idx, arg); |
392 | 0 | } |
393 | | |
394 | 0 | void *RSA_get_ex_data(const RSA *rsa, int idx) { |
395 | 0 | auto *impl = FromOpaque(rsa); |
396 | 0 | return CRYPTO_get_ex_data(&impl->ex_data, idx); |
397 | 0 | } |
398 | | |
399 | | // SSL_SIG_LENGTH is the size of an SSL/TLS (prior to TLS 1.2) signature: it's |
400 | | // the length of an MD5 and SHA1 hash. |
401 | | static const unsigned SSL_SIG_LENGTH = 36; |
402 | | |
403 | | // pkcs1_sig_prefix contains the ASN.1, DER encoded prefix for a hash that is |
404 | | // to be signed with PKCS#1. |
405 | | struct pkcs1_sig_prefix { |
406 | | // nid identifies the hash function. |
407 | | int nid; |
408 | | // hash_len is the expected length of the hash function. |
409 | | uint8_t hash_len; |
410 | | // len is the number of bytes of `bytes` which are valid. |
411 | | uint8_t len; |
412 | | // bytes contains the DER bytes. |
413 | | uint8_t bytes[19]; |
414 | | }; |
415 | | |
416 | | // kPKCS1SigPrefixes contains the ASN.1 prefixes for PKCS#1 signatures with |
417 | | // different hash functions. |
418 | | static const struct pkcs1_sig_prefix kPKCS1SigPrefixes[] = { |
419 | | { |
420 | | NID_md5, |
421 | | MD5_DIGEST_LENGTH, |
422 | | 18, |
423 | | {0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, |
424 | | 0x02, 0x05, 0x05, 0x00, 0x04, 0x10}, |
425 | | }, |
426 | | { |
427 | | NID_sha1, |
428 | | SHA_DIGEST_LENGTH, |
429 | | 15, |
430 | | {0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, |
431 | | 0x00, 0x04, 0x14}, |
432 | | }, |
433 | | { |
434 | | NID_sha224, |
435 | | SHA224_DIGEST_LENGTH, |
436 | | 19, |
437 | | {0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, |
438 | | 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1c}, |
439 | | }, |
440 | | { |
441 | | NID_sha256, |
442 | | SHA256_DIGEST_LENGTH, |
443 | | 19, |
444 | | {0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, |
445 | | 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20}, |
446 | | }, |
447 | | { |
448 | | NID_sha384, |
449 | | SHA384_DIGEST_LENGTH, |
450 | | 19, |
451 | | {0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, |
452 | | 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30}, |
453 | | }, |
454 | | { |
455 | | NID_sha512, |
456 | | SHA512_DIGEST_LENGTH, |
457 | | 19, |
458 | | {0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, |
459 | | 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40}, |
460 | | }, |
461 | | { |
462 | | NID_undef, |
463 | | 0, |
464 | | 0, |
465 | | {0}, |
466 | | }, |
467 | | }; |
468 | | |
469 | 10.2k | static int rsa_check_digest_size(int hash_nid, size_t digest_len) { |
470 | 10.2k | if (hash_nid == NID_md5_sha1) { |
471 | 2.56k | if (digest_len != SSL_SIG_LENGTH) { |
472 | 0 | OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH); |
473 | 0 | return 0; |
474 | 0 | } |
475 | 2.56k | return 1; |
476 | 2.56k | } |
477 | | |
478 | 27.6k | for (size_t i = 0; kPKCS1SigPrefixes[i].nid != NID_undef; i++) { |
479 | 27.6k | const struct pkcs1_sig_prefix *sig_prefix = &kPKCS1SigPrefixes[i]; |
480 | 27.6k | if (sig_prefix->nid == hash_nid) { |
481 | 7.65k | if (digest_len != sig_prefix->hash_len) { |
482 | 0 | OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH); |
483 | 0 | return 0; |
484 | 0 | } |
485 | 7.65k | return 1; |
486 | 7.65k | } |
487 | 27.6k | } |
488 | | |
489 | 0 | OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_ALGORITHM_TYPE); |
490 | 0 | return 0; |
491 | 7.65k | } |
492 | | |
493 | | int RSA_add_pkcs1_prefix(uint8_t **out_msg, size_t *out_msg_len, |
494 | | int *is_alloced, int hash_nid, const uint8_t *digest, |
495 | 10.2k | size_t digest_len) { |
496 | 10.2k | if (!rsa_check_digest_size(hash_nid, digest_len)) { |
497 | 0 | return 0; |
498 | 0 | } |
499 | | |
500 | 10.2k | if (hash_nid == NID_md5_sha1) { |
501 | | // The length should already have been checked. |
502 | 2.56k | assert(digest_len == SSL_SIG_LENGTH); |
503 | 2.56k | *out_msg = (uint8_t *)digest; |
504 | 2.56k | *out_msg_len = digest_len; |
505 | 2.56k | *is_alloced = 0; |
506 | 2.56k | return 1; |
507 | 2.56k | } |
508 | | |
509 | 27.6k | for (size_t i = 0; kPKCS1SigPrefixes[i].nid != NID_undef; i++) { |
510 | 27.6k | const struct pkcs1_sig_prefix *sig_prefix = &kPKCS1SigPrefixes[i]; |
511 | 27.6k | if (sig_prefix->nid != hash_nid) { |
512 | 20.0k | continue; |
513 | 20.0k | } |
514 | | |
515 | | // The length should already have been checked. |
516 | 27.6k | assert(digest_len == sig_prefix->hash_len); |
517 | 7.65k | const uint8_t *prefix = sig_prefix->bytes; |
518 | 7.65k | size_t prefix_len = sig_prefix->len; |
519 | 7.65k | size_t signed_msg_len = prefix_len + digest_len; |
520 | 7.65k | if (signed_msg_len < prefix_len) { |
521 | 0 | OPENSSL_PUT_ERROR(RSA, RSA_R_TOO_LONG); |
522 | 0 | return 0; |
523 | 0 | } |
524 | | |
525 | 7.65k | uint8_t *signed_msg = |
526 | 7.65k | reinterpret_cast<uint8_t *>(OPENSSL_malloc(signed_msg_len)); |
527 | 7.65k | if (!signed_msg) { |
528 | 0 | return 0; |
529 | 0 | } |
530 | | |
531 | 7.65k | OPENSSL_memcpy(signed_msg, prefix, prefix_len); |
532 | 7.65k | OPENSSL_memcpy(signed_msg + prefix_len, digest, digest_len); |
533 | | |
534 | 7.65k | *out_msg = signed_msg; |
535 | 7.65k | *out_msg_len = signed_msg_len; |
536 | 7.65k | *is_alloced = 1; |
537 | | |
538 | 7.65k | return 1; |
539 | 7.65k | } |
540 | | |
541 | 0 | OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_ALGORITHM_TYPE); |
542 | 0 | return 0; |
543 | 7.65k | } |
544 | | |
545 | | int bssl::rsa_sign_no_self_test(int hash_nid, const uint8_t *digest, |
546 | | size_t digest_len, uint8_t *out, |
547 | 5.80k | unsigned *out_len, RSA *rsa) { |
548 | 5.80k | auto *impl = FromOpaque(rsa); |
549 | | |
550 | 5.80k | if (impl->meth->sign) { |
551 | 0 | if (!rsa_check_digest_size(hash_nid, digest_len)) { |
552 | 0 | return 0; |
553 | 0 | } |
554 | | // All supported digest lengths fit in `unsigned`. |
555 | 0 | assert(digest_len <= EVP_MAX_MD_SIZE); |
556 | 0 | static_assert(EVP_MAX_MD_SIZE <= UINT_MAX, "digest too long"); |
557 | 0 | return impl->meth->sign(hash_nid, digest, (unsigned)digest_len, out, |
558 | 0 | out_len, rsa); |
559 | 0 | } |
560 | | |
561 | 5.80k | const unsigned rsa_size = RSA_size(rsa); |
562 | 5.80k | int ret = 0; |
563 | 5.80k | uint8_t *signed_msg = nullptr; |
564 | 5.80k | size_t signed_msg_len = 0; |
565 | 5.80k | int signed_msg_is_alloced = 0; |
566 | 5.80k | size_t size_t_out_len; |
567 | 5.80k | if (!RSA_add_pkcs1_prefix(&signed_msg, &signed_msg_len, |
568 | 5.80k | &signed_msg_is_alloced, hash_nid, digest, |
569 | 5.80k | digest_len) || |
570 | 5.80k | !rsa_sign_raw_no_self_test(rsa, &size_t_out_len, out, rsa_size, |
571 | 5.80k | signed_msg, signed_msg_len, |
572 | 5.80k | RSA_PKCS1_PADDING)) { |
573 | 0 | goto err; |
574 | 0 | } |
575 | | |
576 | 5.80k | if (size_t_out_len > UINT_MAX) { |
577 | 0 | OPENSSL_PUT_ERROR(RSA, ERR_R_OVERFLOW); |
578 | 0 | goto err; |
579 | 0 | } |
580 | | |
581 | 5.80k | *out_len = (unsigned)size_t_out_len; |
582 | 5.80k | ret = 1; |
583 | | |
584 | 5.80k | err: |
585 | 5.80k | if (signed_msg_is_alloced) { |
586 | 3.94k | OPENSSL_free(signed_msg); |
587 | 3.94k | } |
588 | 5.80k | return ret; |
589 | 5.80k | } |
590 | | |
591 | | int RSA_sign(int hash_nid, const uint8_t *digest, size_t digest_len, |
592 | 5.80k | uint8_t *out, unsigned *out_len, RSA *rsa) { |
593 | 5.80k | boringssl_ensure_rsa_sign_self_test(); |
594 | | |
595 | 5.80k | return rsa_sign_no_self_test(hash_nid, digest, digest_len, out, out_len, rsa); |
596 | 5.80k | } |
597 | | |
598 | | int RSA_sign_pss_mgf1(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out, |
599 | | const uint8_t *digest, size_t digest_len, |
600 | 12.9k | const EVP_MD *md, const EVP_MD *mgf1_md, int salt_len) { |
601 | 12.9k | if (digest_len != EVP_MD_size(md)) { |
602 | 0 | OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH); |
603 | 0 | return 0; |
604 | 0 | } |
605 | | |
606 | 12.9k | size_t padded_len = RSA_size(rsa); |
607 | 12.9k | uint8_t *padded = reinterpret_cast<uint8_t *>(OPENSSL_malloc(padded_len)); |
608 | 12.9k | if (padded == nullptr) { |
609 | 0 | return 0; |
610 | 0 | } |
611 | | |
612 | 12.9k | int ret = RSA_padding_add_PKCS1_PSS_mgf1(rsa, padded, digest, md, mgf1_md, |
613 | 12.9k | salt_len) && |
614 | 12.9k | RSA_sign_raw(rsa, out_len, out, max_out, padded, padded_len, |
615 | 12.9k | RSA_NO_PADDING); |
616 | 12.9k | OPENSSL_free(padded); |
617 | 12.9k | return ret; |
618 | 12.9k | } |
619 | | |
620 | | int bssl::rsa_verify_no_self_test(int hash_nid, const uint8_t *digest, |
621 | | size_t digest_len, const uint8_t *sig, |
622 | 18.4k | size_t sig_len, RSA *rsa) { |
623 | 18.4k | auto *impl = FromOpaque(rsa); |
624 | 18.4k | if (impl->n == nullptr || impl->e == nullptr) { |
625 | 0 | OPENSSL_PUT_ERROR(RSA, RSA_R_VALUE_MISSING); |
626 | 0 | return 0; |
627 | 0 | } |
628 | | |
629 | 18.4k | const size_t rsa_size = RSA_size(rsa); |
630 | 18.4k | uint8_t *buf = nullptr; |
631 | 18.4k | int ret = 0; |
632 | 18.4k | uint8_t *signed_msg = nullptr; |
633 | 18.4k | size_t signed_msg_len = 0, len; |
634 | 18.4k | int signed_msg_is_alloced = 0; |
635 | | |
636 | 18.4k | if (hash_nid == NID_md5_sha1 && digest_len != SSL_SIG_LENGTH) { |
637 | 0 | OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH); |
638 | 0 | return 0; |
639 | 0 | } |
640 | | |
641 | 18.4k | buf = reinterpret_cast<uint8_t *>(OPENSSL_malloc(rsa_size)); |
642 | 18.4k | if (!buf) { |
643 | 0 | return 0; |
644 | 0 | } |
645 | | |
646 | 18.4k | if (!rsa_verify_raw_no_self_test(rsa, &len, buf, rsa_size, sig, sig_len, |
647 | 18.4k | RSA_PKCS1_PADDING) || |
648 | 4.41k | !RSA_add_pkcs1_prefix(&signed_msg, &signed_msg_len, |
649 | 4.41k | &signed_msg_is_alloced, hash_nid, digest, |
650 | 14.0k | digest_len)) { |
651 | 14.0k | goto out; |
652 | 14.0k | } |
653 | | |
654 | | // Check that no other information follows the hash value (FIPS 186-5 Section |
655 | | // 5.4) and it matches the expected hash. |
656 | 4.41k | if (len != signed_msg_len || OPENSSL_memcmp(buf, signed_msg, len) != 0) { |
657 | 4.41k | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_SIGNATURE); |
658 | 4.41k | goto out; |
659 | 4.41k | } |
660 | | |
661 | 0 | ret = 1; |
662 | |
|
663 | 18.4k | out: |
664 | 18.4k | OPENSSL_free(buf); |
665 | 18.4k | if (signed_msg_is_alloced) { |
666 | 3.70k | OPENSSL_free(signed_msg); |
667 | 3.70k | } |
668 | 18.4k | return ret; |
669 | 0 | } |
670 | | |
671 | | int RSA_verify(int hash_nid, const uint8_t *digest, size_t digest_len, |
672 | 18.4k | const uint8_t *sig, size_t sig_len, RSA *rsa) { |
673 | 18.4k | boringssl_ensure_rsa_verify_self_test(); |
674 | 18.4k | return rsa_verify_no_self_test(hash_nid, digest, digest_len, sig, sig_len, |
675 | 18.4k | rsa); |
676 | 18.4k | } |
677 | | |
678 | | int RSA_verify_pss_mgf1(RSA *rsa, const uint8_t *digest, size_t digest_len, |
679 | | const EVP_MD *md, const EVP_MD *mgf1_md, int salt_len, |
680 | 2.20k | const uint8_t *sig, size_t sig_len) { |
681 | 2.20k | if (digest_len != EVP_MD_size(md)) { |
682 | 0 | OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH); |
683 | 0 | return 0; |
684 | 0 | } |
685 | | |
686 | 2.20k | size_t em_len = RSA_size(rsa); |
687 | 2.20k | uint8_t *em = reinterpret_cast<uint8_t *>(OPENSSL_malloc(em_len)); |
688 | 2.20k | if (em == nullptr) { |
689 | 0 | return 0; |
690 | 0 | } |
691 | | |
692 | 2.20k | int ret = 0; |
693 | 2.20k | if (!RSA_verify_raw(rsa, &em_len, em, em_len, sig, sig_len, RSA_NO_PADDING)) { |
694 | 61 | goto err; |
695 | 61 | } |
696 | | |
697 | 2.14k | if (em_len != RSA_size(rsa)) { |
698 | 0 | OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR); |
699 | 0 | goto err; |
700 | 0 | } |
701 | | |
702 | 2.14k | ret = RSA_verify_PKCS1_PSS_mgf1(rsa, digest, md, mgf1_md, em, salt_len); |
703 | | |
704 | 2.20k | err: |
705 | 2.20k | OPENSSL_free(em); |
706 | 2.20k | return ret; |
707 | 2.14k | } |
708 | | |
709 | | static int check_mod_inverse(int *out_ok, const BIGNUM *a, const BIGNUM *ainv, |
710 | | const BIGNUM *m, unsigned m_min_bits, |
711 | 558 | BN_CTX *ctx) { |
712 | 558 | if (BN_is_negative(ainv) || |
713 | 558 | constant_time_declassify_int(BN_cmp(ainv, m) >= 0)) { |
714 | 66 | *out_ok = 0; |
715 | 66 | return 1; |
716 | 66 | } |
717 | | |
718 | | // Note `bn_mul_consttime` and `bn_div_consttime` do not scale linearly, but |
719 | | // checking `ainv` is in range bounds the running time, assuming `m`'s bounds |
720 | | // were checked by the caller. |
721 | 492 | BN_CTXScope scope(ctx); |
722 | 492 | BIGNUM *tmp = BN_CTX_get(ctx); |
723 | 492 | if (tmp == nullptr || // |
724 | 492 | !bn_mul_consttime(tmp, a, ainv, ctx) || |
725 | 492 | !bn_div_consttime(nullptr, tmp, tmp, m, m_min_bits, ctx)) { |
726 | 0 | return 0; |
727 | 0 | } |
728 | 492 | *out_ok = constant_time_declassify_int(BN_is_one(tmp)); |
729 | 492 | return 1; |
730 | 492 | } |
731 | | |
732 | 109k | int RSA_check_key(const RSA *key) { |
733 | | // TODO(davidben): RSA key initialization is spread across |
734 | | // `rsa_check_public_key`, `RSA_check_key`, `freeze_private_key`, and |
735 | | // `BN_MONT_CTX_set_locked` as a result of API issues. See |
736 | | // https://crbug.com/boringssl/316. As a result, we inconsistently check RSA |
737 | | // invariants. We should fix this and integrate that logic. |
738 | | |
739 | 109k | if (!rsa_check_public_key(key)) { |
740 | 1.83k | return 0; |
741 | 1.83k | } |
742 | | |
743 | 107k | auto *impl = FromOpaque(key); |
744 | 107k | if ((impl->p != nullptr) != (impl->q != nullptr)) { |
745 | 0 | OPENSSL_PUT_ERROR(RSA, RSA_R_ONLY_ONE_OF_P_Q_GIVEN); |
746 | 0 | return 0; |
747 | 0 | } |
748 | | |
749 | | // `impl->d` must be bounded by `impl->n`. This ensures bounds on `RSA_bits` |
750 | | // translate to bounds on the running time of private key operations. |
751 | 107k | if (impl->d != nullptr && (BN_is_negative(impl->d.get()) || |
752 | 504 | BN_cmp(impl->d.get(), impl->n.get()) >= 0)) { |
753 | 8 | OPENSSL_PUT_ERROR(RSA, RSA_R_D_OUT_OF_RANGE); |
754 | 8 | return 0; |
755 | 8 | } |
756 | | |
757 | 107k | if (impl->d == nullptr || impl->p == nullptr) { |
758 | | // For a public key, or without p and q, there's nothing that can be |
759 | | // checked. |
760 | 106k | return 1; |
761 | 106k | } |
762 | | |
763 | 496 | BN_CTX *ctx = BN_CTX_new(); |
764 | 496 | if (ctx == nullptr) { |
765 | 0 | return 0; |
766 | 0 | } |
767 | | |
768 | 496 | BIGNUM tmp, de, pm1, qm1, dmp1, dmq1; |
769 | 496 | int ok = 0; |
770 | 496 | bool has_crt_values; |
771 | 496 | unsigned pm1_bits, qm1_bits; |
772 | 496 | BN_init(&tmp); |
773 | 496 | BN_init(&de); |
774 | 496 | BN_init(&pm1); |
775 | 496 | BN_init(&qm1); |
776 | 496 | BN_init(&dmp1); |
777 | 496 | BN_init(&dmq1); |
778 | | |
779 | | // Check that p * q == n. Before we multiply, we check that p and q are in |
780 | | // bounds, to avoid a DoS vector in `bn_mul_consttime` below. Note that |
781 | | // n was bound by `rsa_check_public_key`. This also implicitly checks p and q |
782 | | // are odd, which is a necessary condition for Montgomery reduction. |
783 | 496 | if (BN_is_negative(impl->p.get()) || |
784 | 496 | constant_time_declassify_int(BN_cmp(impl->p.get(), impl->n.get()) >= 0) || |
785 | 490 | BN_is_negative(impl->q.get()) || |
786 | 490 | constant_time_declassify_int(BN_cmp(impl->q.get(), impl->n.get()) >= 0)) { |
787 | 14 | OPENSSL_PUT_ERROR(RSA, RSA_R_N_NOT_EQUAL_P_Q); |
788 | 14 | goto out; |
789 | 14 | } |
790 | 482 | if (!bn_mul_consttime(&tmp, impl->p.get(), impl->q.get(), ctx)) { |
791 | 0 | OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN); |
792 | 0 | goto out; |
793 | 0 | } |
794 | 482 | if (BN_cmp(&tmp, impl->n.get()) != 0) { |
795 | 99 | OPENSSL_PUT_ERROR(RSA, RSA_R_N_NOT_EQUAL_P_Q); |
796 | 99 | goto out; |
797 | 99 | } |
798 | | |
799 | | // d must be an inverse of e mod the Carmichael totient, lcm(p-1, q-1), but it |
800 | | // may be unreduced because other implementations use the Euler totient. We |
801 | | // simply check that d * e is one mod p-1 and mod q-1. Note d and e were bound |
802 | | // by earlier checks in this function. |
803 | 383 | if (!bn_usub_consttime(&pm1, impl->p.get(), BN_value_one()) || |
804 | 383 | !bn_usub_consttime(&qm1, impl->q.get(), BN_value_one())) { |
805 | 0 | OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN); |
806 | 0 | goto out; |
807 | 0 | } |
808 | 383 | pm1_bits = BN_num_bits(&pm1); |
809 | 383 | qm1_bits = BN_num_bits(&qm1); |
810 | 383 | if (!bn_mul_consttime(&de, impl->d.get(), impl->e.get(), ctx) || |
811 | 383 | !bn_div_consttime(nullptr, &tmp, &de, &pm1, pm1_bits, ctx) || |
812 | 383 | !bn_div_consttime(nullptr, &de, &de, &qm1, qm1_bits, ctx)) { |
813 | 0 | OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN); |
814 | 0 | goto out; |
815 | 0 | } |
816 | | |
817 | 383 | if (constant_time_declassify_int(!BN_is_one(&tmp)) || |
818 | 197 | constant_time_declassify_int(!BN_is_one(&de))) { |
819 | 197 | OPENSSL_PUT_ERROR(RSA, RSA_R_D_E_NOT_CONGRUENT_TO_1); |
820 | 197 | goto out; |
821 | 197 | } |
822 | | |
823 | 186 | has_crt_values = impl->dmp1 != nullptr; |
824 | 186 | if (has_crt_values != (impl->dmq1 != nullptr) || |
825 | 186 | has_crt_values != (impl->iqmp != nullptr)) { |
826 | 0 | OPENSSL_PUT_ERROR(RSA, RSA_R_INCONSISTENT_SET_OF_CRT_VALUES); |
827 | 0 | goto out; |
828 | 0 | } |
829 | | |
830 | 186 | if (has_crt_values) { |
831 | 186 | int dmp1_ok, dmq1_ok, iqmp_ok; |
832 | 186 | if (!check_mod_inverse(&dmp1_ok, impl->e.get(), impl->dmp1.get(), &pm1, |
833 | 186 | pm1_bits, ctx) || |
834 | 186 | !check_mod_inverse(&dmq1_ok, impl->e.get(), impl->dmq1.get(), &qm1, |
835 | 186 | qm1_bits, ctx) || |
836 | | // `p` is odd, so `pm1` and `p` have the same bit width. If they didn't, |
837 | | // we only need a lower bound anyway. |
838 | 186 | !check_mod_inverse(&iqmp_ok, impl->q.get(), impl->iqmp.get(), |
839 | 186 | impl->p.get(), pm1_bits, ctx)) { |
840 | 0 | OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN); |
841 | 0 | goto out; |
842 | 0 | } |
843 | | |
844 | 186 | if (!dmp1_ok || !dmq1_ok || !iqmp_ok) { |
845 | 162 | OPENSSL_PUT_ERROR(RSA, RSA_R_CRT_VALUES_INCORRECT); |
846 | 162 | goto out; |
847 | 162 | } |
848 | 186 | } |
849 | | |
850 | 24 | ok = 1; |
851 | | |
852 | 496 | out: |
853 | 496 | BN_free(&tmp); |
854 | 496 | BN_free(&de); |
855 | 496 | BN_free(&pm1); |
856 | 496 | BN_free(&qm1); |
857 | 496 | BN_free(&dmp1); |
858 | 496 | BN_free(&dmq1); |
859 | 496 | BN_CTX_free(ctx); |
860 | | |
861 | 496 | return ok; |
862 | 24 | } |
863 | | |
864 | | |
865 | | // This is the product of the 132 smallest odd primes, from 3 to 751. |
866 | | static const BN_ULONG kSmallFactorsLimbs[] = {TOBN(0xc4309333, 0x3ef4e3e1), |
867 | | TOBN(0x71161eb6, 0xcd2d655f), |
868 | | TOBN(0x95e2238c, 0x0bf94862), |
869 | | TOBN(0x3eb233d3, 0x24f7912b), |
870 | | TOBN(0x6b55514b, 0xbf26c483), |
871 | | TOBN(0x0a84d817, 0x5a144871), |
872 | | TOBN(0x77d12fee, 0x9b82210a), |
873 | | TOBN(0xdb5b93c2, 0x97f050b3), |
874 | | TOBN(0x4acad6b9, 0x4d6c026b), |
875 | | TOBN(0xeb7751f3, 0x54aec893), |
876 | | TOBN(0xdba53368, 0x36bc85c4), |
877 | | TOBN(0xd85a1b28, 0x7f5ec78e), |
878 | | TOBN(0x2eb072d8, 0x6b322244), |
879 | | TOBN(0xbba51112, 0x5e2b3aea), |
880 | | TOBN(0x36ed1a6c, 0x0e2486bf), |
881 | | TOBN(0x5f270460, 0xec0c5727), |
882 | | 0x000017b1}; |
883 | | |
884 | 0 | DEFINE_LOCAL_DATA(BIGNUM, g_small_factors) { |
885 | 0 | out->d = const_cast<BN_ULONG *>(kSmallFactorsLimbs); |
886 | 0 | out->width = std::size(kSmallFactorsLimbs); |
887 | 0 | out->dmax = out->width; |
888 | 0 | out->neg = 0; |
889 | 0 | out->flags = BN_FLG_STATIC_DATA; |
890 | 0 | } |
891 | | |
892 | 0 | int RSA_check_fips(RSA *key) { |
893 | 0 | if (!RSA_check_key(key)) { |
894 | 0 | return 0; |
895 | 0 | } |
896 | | |
897 | 0 | BN_CTX *ctx = BN_CTX_new(); |
898 | 0 | if (ctx == nullptr) { |
899 | 0 | return 0; |
900 | 0 | } |
901 | | |
902 | 0 | BIGNUM small_gcd; |
903 | 0 | BN_init(&small_gcd); |
904 | |
|
905 | 0 | int ret = 1; |
906 | | |
907 | | // Perform partial public key validation of RSA keys (SP 800-89 5.3.3). |
908 | | // Although this is not for primality testing, SP 800-89 cites an RSA |
909 | | // primality testing algorithm, so we use `BN_prime_checks_for_generation` to |
910 | | // match. This is only a plausibility test and we expect the value to be |
911 | | // composite, so too few iterations will cause us to reject the key, not use |
912 | | // an implausible one. |
913 | | // |
914 | | // `key->e` may be nullptr if created with `RSA_new_private_key_no_e`. |
915 | 0 | enum bn_primality_result_t primality_result; |
916 | 0 | auto *impl = FromOpaque(key); |
917 | 0 | if (impl->e.get() == nullptr || // |
918 | 0 | BN_num_bits(impl->e.get()) <= 16 || // |
919 | 0 | BN_num_bits(impl->e.get()) > 256 || // |
920 | 0 | !BN_is_odd(impl->n.get()) || // |
921 | 0 | !BN_is_odd(impl->e.get()) || |
922 | 0 | !BN_gcd(&small_gcd, impl->n.get(), g_small_factors(), ctx) || |
923 | 0 | !BN_is_one(&small_gcd) || |
924 | 0 | !BN_enhanced_miller_rabin_primality_test(&primality_result, impl->n.get(), |
925 | 0 | BN_prime_checks_for_generation, |
926 | 0 | ctx, nullptr) || |
927 | 0 | primality_result != bn_non_prime_power_composite) { |
928 | 0 | OPENSSL_PUT_ERROR(RSA, RSA_R_PUBLIC_KEY_VALIDATION_FAILED); |
929 | 0 | ret = 0; |
930 | 0 | } |
931 | |
|
932 | 0 | BN_free(&small_gcd); |
933 | 0 | BN_CTX_free(ctx); |
934 | |
|
935 | 0 | if (!ret || impl->d == nullptr || impl->p == nullptr) { |
936 | | // On a failure or on only a public key, there's nothing else can be |
937 | | // checked. |
938 | 0 | return ret; |
939 | 0 | } |
940 | | |
941 | | // FIPS pairwise consistency test (FIPS 140-2 4.9.2). Per FIPS 140-2 IG, |
942 | | // section 9.9, it is not known whether `rsa` will be used for signing or |
943 | | // encryption, so either pair-wise consistency self-test is acceptable. We |
944 | | // perform a signing test. |
945 | 0 | uint8_t data[32] = {0}; |
946 | 0 | unsigned sig_len = RSA_size(key); |
947 | 0 | uint8_t *sig = reinterpret_cast<uint8_t *>(OPENSSL_malloc(sig_len)); |
948 | 0 | if (sig == nullptr) { |
949 | 0 | return 0; |
950 | 0 | } |
951 | | |
952 | 0 | if (!RSA_sign(NID_sha256, data, sizeof(data), sig, &sig_len, key)) { |
953 | 0 | OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR); |
954 | 0 | ret = 0; |
955 | 0 | goto cleanup; |
956 | 0 | } |
957 | 0 | if (boringssl_fips_break_test("RSA_PWCT")) { |
958 | 0 | data[0] = ~data[0]; |
959 | 0 | } |
960 | 0 | if (!RSA_verify(NID_sha256, data, sizeof(data), sig, sig_len, key)) { |
961 | 0 | OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR); |
962 | 0 | ret = 0; |
963 | 0 | } |
964 | |
|
965 | 0 | cleanup: |
966 | 0 | OPENSSL_free(sig); |
967 | |
|
968 | 0 | return ret; |
969 | 0 | } |
970 | | |
971 | | int bssl::rsa_private_transform_no_self_test(RSA *rsa, uint8_t *out, |
972 | 18.8k | const uint8_t *in, size_t len) { |
973 | 18.8k | auto *impl = FromOpaque(rsa); |
974 | | |
975 | 18.8k | if (impl->meth->private_transform) { |
976 | 0 | return impl->meth->private_transform(rsa, out, in, len); |
977 | 0 | } |
978 | | |
979 | 18.8k | return rsa_default_private_transform(rsa, out, in, len); |
980 | 18.8k | } |
981 | | |
982 | | int bssl::rsa_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in, |
983 | 86 | size_t len) { |
984 | 86 | boringssl_ensure_rsa_sign_self_test(); |
985 | 86 | return rsa_private_transform_no_self_test(rsa, out, in, len); |
986 | 86 | } |
987 | | |
988 | 0 | int RSA_flags(const RSA *rsa) { |
989 | 0 | auto *impl = FromOpaque(rsa); |
990 | 0 | return impl->flags; |
991 | 0 | } |
992 | | |
993 | 0 | int RSA_test_flags(const RSA *rsa, int flags) { |
994 | 0 | auto *impl = FromOpaque(rsa); |
995 | 0 | return impl->flags & flags; |
996 | 0 | } |