Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/rsa/rsa_ossl.c

raw
1 /*
2 * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 /*
11 * RSA low level APIs are deprecated for public use, but still ok for
12 * internal use.
13 */
14 #include "internal/deprecated.h"
15
16 #include "internal/cryptlib.h"
17 #include "crypto/bn.h"
18 #include "crypto/sparse_array.h"
19 #include "rsa_local.h"
20 #include "internal/constant_time.h"
21 #if defined(OPENSSL_SYS_TANDEM)
22 #include "internal/tsan_assist.h"
23 #include "internal/threads_common.h"
24 #endif
25 #include <openssl/evp.h>
26 #include <openssl/sha.h>
27 #include <openssl/hmac.h>
28
29 DEFINE_SPARSE_ARRAY_OF(BN_BLINDING);
30
31 static int rsa_ossl_public_encrypt(int flen, const unsigned char *from,
32 unsigned char *to, RSA *rsa, int padding);
33 static int rsa_ossl_private_encrypt(int flen, const unsigned char *from,
34 unsigned char *to, RSA *rsa, int padding);
35 static int rsa_ossl_public_decrypt(int flen, const unsigned char *from,
36 unsigned char *to, RSA *rsa, int padding);
37 static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
38 unsigned char *to, RSA *rsa, int padding);
39 static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa,
40 BN_CTX *ctx);
41 static int rsa_ossl_init(RSA *rsa);
42 static int rsa_ossl_finish(RSA *rsa);
43 #ifdef S390X_MOD_EXP
44 static int rsa_ossl_s390x_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa,
45 BN_CTX *ctx);
46 static RSA_METHOD rsa_pkcs1_ossl_meth = {
47 "OpenSSL PKCS#1 RSA",
48 rsa_ossl_public_encrypt,
49 rsa_ossl_public_decrypt, /* signature verification */
50 rsa_ossl_private_encrypt, /* signing */
51 rsa_ossl_private_decrypt,
52 rsa_ossl_s390x_mod_exp,
53 s390x_mod_exp,
54 rsa_ossl_init,
55 rsa_ossl_finish,
56 RSA_FLAG_FIPS_METHOD, /* flags */
57 NULL,
58 0, /* rsa_sign */
59 0, /* rsa_verify */
60 NULL, /* rsa_keygen */
61 NULL /* rsa_multi_prime_keygen */
62 };
63 #else
64 static RSA_METHOD rsa_pkcs1_ossl_meth = {
65 "OpenSSL PKCS#1 RSA",
66 rsa_ossl_public_encrypt,
67 rsa_ossl_public_decrypt, /* signature verification */
68 rsa_ossl_private_encrypt, /* signing */
69 rsa_ossl_private_decrypt,
70 rsa_ossl_mod_exp,
71 BN_mod_exp_mont, /* XXX probably we should not use Montgomery
72 * if e == 3 */
73 rsa_ossl_init,
74 rsa_ossl_finish,
75 RSA_FLAG_FIPS_METHOD, /* flags */
76 NULL,
77 0, /* rsa_sign */
78 0, /* rsa_verify */
79 NULL, /* rsa_keygen */
80 NULL /* rsa_multi_prime_keygen */
81 };
82 #endif
83
84 static const RSA_METHOD *default_RSA_meth = &rsa_pkcs1_ossl_meth;
85
86 void RSA_set_default_method(const RSA_METHOD *meth)
87 {
88 default_RSA_meth = meth;
89 }
90
91 const RSA_METHOD *RSA_get_default_method(void)
92 {
93 return default_RSA_meth;
94 }
95
96 const RSA_METHOD *RSA_PKCS1_OpenSSL(void)
97 {
98 return &rsa_pkcs1_ossl_meth;
99 }
100
101 const RSA_METHOD *RSA_null_method(void)
102 {
103 return NULL;
104 }
105
106 static int rsa_ossl_public_encrypt(int flen, const unsigned char *from,
107 unsigned char *to, RSA *rsa, int padding)
108 {
109 BIGNUM *f, *ret;
110 int i, num = 0, r = -1;
111 unsigned char *buf = NULL;
112 BN_CTX *ctx = NULL;
113
114 if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
115 ERR_raise(ERR_LIB_RSA, RSA_R_MODULUS_TOO_LARGE);
116 return -1;
117 }
118
119 if (BN_ucmp(rsa->n, rsa->e) <= 0) {
120 ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
121 return -1;
122 }
123
124 /* for large moduli, enforce exponent limit */
125 if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
126 if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
127 ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
128 return -1;
129 }
130 }
131
132 if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
133 goto err;
134 BN_CTX_start(ctx);
135 f = BN_CTX_get(ctx);
136 ret = BN_CTX_get(ctx);
137 num = BN_num_bytes(rsa->n);
138 buf = OPENSSL_malloc(num);
139 if (ret == NULL || buf == NULL)
140 goto err;
141
142 switch (padding) {
143 case RSA_PKCS1_PADDING:
144 i = ossl_rsa_padding_add_PKCS1_type_2_ex(rsa->libctx, buf, num,
145 from, flen);
146 break;
147 case RSA_PKCS1_OAEP_PADDING:
148 i = ossl_rsa_padding_add_PKCS1_OAEP_mgf1_ex(rsa->libctx, buf, num,
149 from, flen, NULL, 0,
150 NULL, NULL);
151 break;
152 case RSA_NO_PADDING:
153 i = RSA_padding_add_none(buf, num, from, flen);
154 break;
155 default:
156 ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
157 goto err;
158 }
159 if (i <= 0)
160 goto err;
161
162 if (BN_bin2bn(buf, num, f) == NULL)
163 goto err;
164
165 #ifdef FIPS_MODULE
166 /*
167 * See SP800-56Br2, section 7.1.1.1
168 * RSAEP: 1 < f < (n – 1).
169 * (where f is the plaintext).
170 */
171 if (padding == RSA_NO_PADDING) {
172 BIGNUM *nminus1 = BN_CTX_get(ctx);
173
174 if (BN_ucmp(f, BN_value_one()) <= 0) {
175 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_SMALL);
176 goto err;
177 }
178 if (nminus1 == NULL
179 || BN_copy(nminus1, rsa->n) == NULL
180 || !BN_sub_word(nminus1, 1))
181 goto err;
182 if (BN_ucmp(f, nminus1) >= 0) {
183 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
184 goto err;
185 }
186 } else
187 #endif
188 {
189 if (BN_ucmp(f, rsa->n) >= 0) {
190 /* usually the padding functions would catch this */
191 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
192 goto err;
193 }
194 }
195
196 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
197 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
198 rsa->n, ctx))
199 goto err;
200
201 if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
202 rsa->_method_mod_n))
203 goto err;
204
205 /*
206 * BN_bn2binpad puts in leading 0 bytes if the number is less than
207 * the length of the modulus.
208 */
209 r = BN_bn2binpad(ret, to, num);
210 err:
211 BN_CTX_end(ctx);
212 BN_CTX_free(ctx);
213 OPENSSL_clear_free(buf, num);
214 return r;
215 }
216
217 #if defined(OPENSSL_SYS_TANDEM)
218 static TSAN_QUALIFIER uint64_t tsan_thread_id = 1;
219 #endif
220
221 static uintptr_t get_unique_thread_id(void)
222 {
223 #if defined(OPENSSL_SYS_TANDEM)
224 uintptr_t thread_id = (uintptr_t)CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_TANDEM_ID_KEY,
225 NULL);
226
227 if (thread_id == 0) {
228 thread_id = tsan_counter(&tsan_thread_id);
229 CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_TANDEM_ID_KEY, NULL, (void *)thread_id);
230 }
231 return thread_id;
232 #else
233 return (uintptr_t)CRYPTO_THREAD_get_current_id();
234 #endif
235 }
236
237 static void free_bn_blinding(ossl_uintmax_t idx, BN_BLINDING *b, void *arg)
238 {
239 BN_BLINDING_free(b);
240 }
241
242 void ossl_rsa_free_blinding(RSA *rsa)
243 {
244 SPARSE_ARRAY_OF(BN_BLINDING) *blindings = rsa->blindings_sa;
245
246 ossl_sa_BN_BLINDING_doall_arg(blindings, free_bn_blinding, NULL);
247 ossl_sa_BN_BLINDING_free(blindings);
248 }
249
250 void *ossl_rsa_alloc_blinding(void)
251 {
252 return ossl_sa_BN_BLINDING_new();
253 }
254
255 static BN_BLINDING *ossl_rsa_get_thread_bn_blinding(RSA *rsa)
256 {
257 SPARSE_ARRAY_OF(BN_BLINDING) *blindings = rsa->blindings_sa;
258 uintptr_t tid = get_unique_thread_id();
259
260 return ossl_sa_BN_BLINDING_get(blindings, tid);
261 }
262
263 static int ossl_rsa_set_thread_bn_blinding(RSA *rsa, BN_BLINDING *b)
264 {
265 SPARSE_ARRAY_OF(BN_BLINDING) *blindings = rsa->blindings_sa;
266 uintptr_t tid = get_unique_thread_id();
267
268 return ossl_sa_BN_BLINDING_set(blindings, tid, b);
269 }
270
271 static BN_BLINDING *rsa_get_blinding(RSA *rsa, BN_CTX *ctx)
272 {
273 BN_BLINDING *ret;
274
275 if (!CRYPTO_THREAD_read_lock(rsa->lock))
276 return NULL;
277
278 ret = ossl_rsa_get_thread_bn_blinding(rsa);
279 CRYPTO_THREAD_unlock(rsa->lock);
280
281 if (ret == NULL) {
282 ret = RSA_setup_blinding(rsa, ctx);
283 if (!CRYPTO_THREAD_write_lock(rsa->lock)) {
284 BN_BLINDING_free(ret);
285 ret = NULL;
286 } else {
287 if (!ossl_rsa_set_thread_bn_blinding(rsa, ret)) {
288 BN_BLINDING_free(ret);
289 ret = NULL;
290 }
291 CRYPTO_THREAD_unlock(rsa->lock);
292 }
293 }
294
295 return ret;
296 }
297
298 static int rsa_blinding_convert(BN_BLINDING *b, BIGNUM *f, BN_CTX *ctx)
299 {
300 /*
301 * Local blinding: store the unblinding factor in BN_BLINDING.
302 */
303 return BN_BLINDING_convert_ex(f, NULL, b, ctx);
304 }
305
306 static int rsa_blinding_invert(BN_BLINDING *b, BIGNUM *f, BN_CTX *ctx)
307 {
308 /*
309 * For local blinding, unblind is set to NULL, and BN_BLINDING_invert_ex
310 * will use the unblinding factor stored in BN_BLINDING. If BN_BLINDING
311 * is shared between threads, unblind must be non-null:
312 * BN_BLINDING_invert_ex will then use the local unblinding factor, and
313 * will only read the modulus from BN_BLINDING. In both cases it's safe
314 * to access the blinding without a lock.
315 */
316 BN_set_flags(f, BN_FLG_CONSTTIME);
317 return BN_BLINDING_invert_ex(f, NULL, b, ctx);
318 }
319
320 /* signing */
321 static int rsa_ossl_private_encrypt(int flen, const unsigned char *from,
322 unsigned char *to, RSA *rsa, int padding)
323 {
324 BIGNUM *f, *ret, *res;
325 int i, num = 0, r = -1;
326 unsigned char *buf = NULL;
327 BN_CTX *ctx = NULL;
328 BN_BLINDING *blinding = NULL;
329
330 if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
331 goto err;
332 BN_CTX_start(ctx);
333 f = BN_CTX_get(ctx);
334 ret = BN_CTX_get(ctx);
335 num = BN_num_bytes(rsa->n);
336 buf = OPENSSL_malloc(num);
337 if (ret == NULL || buf == NULL)
338 goto err;
339
340 switch (padding) {
341 case RSA_PKCS1_PADDING:
342 i = RSA_padding_add_PKCS1_type_1(buf, num, from, flen);
343 break;
344 case RSA_X931_PADDING:
345 i = RSA_padding_add_X931(buf, num, from, flen);
346 break;
347 case RSA_NO_PADDING:
348 i = RSA_padding_add_none(buf, num, from, flen);
349 break;
350 default:
351 ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
352 goto err;
353 }
354 if (i <= 0)
355 goto err;
356
357 if (BN_bin2bn(buf, num, f) == NULL)
358 goto err;
359
360 if (BN_ucmp(f, rsa->n) >= 0) {
361 /* usually the padding functions would catch this */
362 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
363 goto err;
364 }
365
366 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
367 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
368 rsa->n, ctx))
369 goto err;
370
371 if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
372 blinding = rsa_get_blinding(rsa, ctx);
373 if (blinding == NULL) {
374 ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
375 goto err;
376 }
377
378 if (!rsa_blinding_convert(blinding, f, ctx))
379 goto err;
380 }
381
382 if ((rsa->flags & RSA_FLAG_EXT_PKEY) || (rsa->version == RSA_ASN1_VERSION_MULTI) || ((rsa->p != NULL) && (rsa->q != NULL) && (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
383 if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
384 goto err;
385 } else {
386 BIGNUM *d = BN_new();
387 if (d == NULL) {
388 ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
389 goto err;
390 }
391 if (rsa->d == NULL) {
392 ERR_raise(ERR_LIB_RSA, RSA_R_MISSING_PRIVATE_KEY);
393 BN_free(d);
394 goto err;
395 }
396 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
397
398 if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
399 rsa->_method_mod_n)) {
400 BN_free(d);
401 goto err;
402 }
403 /* We MUST free d before any further use of rsa->d */
404 BN_free(d);
405 }
406
407 if (blinding)
408 if (!rsa_blinding_invert(blinding, ret, ctx))
409 goto err;
410
411 if (padding == RSA_X931_PADDING) {
412 if (!BN_sub(f, rsa->n, ret))
413 goto err;
414 if (BN_cmp(ret, f) > 0)
415 res = f;
416 else
417 res = ret;
418 } else {
419 res = ret;
420 }
421
422 /*
423 * BN_bn2binpad puts in leading 0 bytes if the number is less than
424 * the length of the modulus.
425 */
426 r = BN_bn2binpad(res, to, num);
427 err:
428 BN_CTX_end(ctx);
429 BN_CTX_free(ctx);
430 OPENSSL_clear_free(buf, num);
431 return r;
432 }
433
434 static int derive_kdk(int flen, const unsigned char *from, RSA *rsa,
435 unsigned char *buf, int num, unsigned char *kdk)
436 {
437 int ret = 0;
438 HMAC_CTX *hmac = NULL;
439 EVP_MD *md = NULL;
440 unsigned int md_len = SHA256_DIGEST_LENGTH;
441 unsigned char d_hash[SHA256_DIGEST_LENGTH] = { 0 };
442 /*
443 * because we use d as a handle to rsa->d we need to keep it local and
444 * free before any further use of rsa->d
445 */
446 BIGNUM *d = BN_new();
447
448 if (d == NULL) {
449 ERR_raise(ERR_LIB_RSA, ERR_R_CRYPTO_LIB);
450 goto err;
451 }
452 if (rsa->d == NULL) {
453 ERR_raise(ERR_LIB_RSA, RSA_R_MISSING_PRIVATE_KEY);
454 BN_free(d);
455 goto err;
456 }
457 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
458 if (BN_bn2binpad(d, buf, num) < 0) {
459 ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
460 BN_free(d);
461 goto err;
462 }
463 BN_free(d);
464
465 /*
466 * we use hardcoded hash so that migrating between versions that use
467 * different hash doesn't provide a Bleichenbacher oracle:
468 * if the attacker can see that different versions return different
469 * messages for the same ciphertext, they'll know that the message is
470 * synthetically generated, which means that the padding check failed
471 */
472 md = EVP_MD_fetch(rsa->libctx, "sha256", NULL);
473 if (md == NULL) {
474 ERR_raise(ERR_LIB_RSA, ERR_R_FETCH_FAILED);
475 goto err;
476 }
477
478 if (EVP_Digest(buf, num, d_hash, NULL, md, NULL) <= 0) {
479 ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
480 goto err;
481 }
482
483 hmac = HMAC_CTX_new();
484 if (hmac == NULL) {
485 ERR_raise(ERR_LIB_RSA, ERR_R_CRYPTO_LIB);
486 goto err;
487 }
488
489 if (HMAC_Init_ex(hmac, d_hash, sizeof(d_hash), md, NULL) <= 0) {
490 ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
491 goto err;
492 }
493
494 if (flen < num) {
495 memset(buf, 0, num - flen);
496 if (HMAC_Update(hmac, buf, num - flen) <= 0) {
497 ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
498 goto err;
499 }
500 }
501 if (HMAC_Update(hmac, from, flen) <= 0) {
502 ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
503 goto err;
504 }
505
506 md_len = SHA256_DIGEST_LENGTH;
507 if (HMAC_Final(hmac, kdk, &md_len) <= 0) {
508 ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
509 goto err;
510 }
511 ret = 1;
512
513 err:
514 HMAC_CTX_free(hmac);
515 EVP_MD_free(md);
516 return ret;
517 }
518
519 static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
520 unsigned char *to, RSA *rsa, int padding)
521 {
522 BIGNUM *f, *ret;
523 int j, num = 0, r = -1;
524 unsigned char *buf = NULL;
525 unsigned char kdk[SHA256_DIGEST_LENGTH] = { 0 };
526 BN_CTX *ctx = NULL;
527 BN_BLINDING *blinding = NULL;
528
529 /*
530 * we need the value of the private exponent to perform implicit rejection
531 */
532 if ((rsa->flags & RSA_FLAG_EXT_PKEY) && (padding == RSA_PKCS1_PADDING))
533 padding = RSA_PKCS1_NO_IMPLICIT_REJECT_PADDING;
534
535 if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
536 goto err;
537 BN_CTX_start(ctx);
538 f = BN_CTX_get(ctx);
539 ret = BN_CTX_get(ctx);
540 if (ret == NULL) {
541 ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
542 goto err;
543 }
544 num = BN_num_bytes(rsa->n);
545 buf = OPENSSL_malloc(num);
546 if (buf == NULL)
547 goto err;
548
549 /*
550 * This check was for equality but PGP does evil things and chops off the
551 * top '0' bytes
552 */
553 if (flen > num) {
554 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_GREATER_THAN_MOD_LEN);
555 goto err;
556 }
557
558 if (flen < 1) {
559 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_SMALL);
560 goto err;
561 }
562
563 /* make data into a big number */
564 if (BN_bin2bn(from, (int)flen, f) == NULL)
565 goto err;
566
567 #ifdef FIPS_MODULE
568 /*
569 * See SP800-56Br2, section 7.1.2.1
570 * RSADP: 1 < f < (n – 1)
571 * (where f is the ciphertext).
572 */
573 if (padding == RSA_NO_PADDING) {
574 BIGNUM *nminus1 = BN_CTX_get(ctx);
575
576 if (BN_ucmp(f, BN_value_one()) <= 0) {
577 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_SMALL);
578 goto err;
579 }
580 if (nminus1 == NULL
581 || BN_copy(nminus1, rsa->n) == NULL
582 || !BN_sub_word(nminus1, 1))
583 goto err;
584 if (BN_ucmp(f, nminus1) >= 0) {
585 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
586 goto err;
587 }
588 } else
589 #endif
590 {
591 if (BN_ucmp(f, rsa->n) >= 0) {
592 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
593 goto err;
594 }
595 }
596 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
597 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
598 rsa->n, ctx))
599 goto err;
600
601 if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
602 blinding = rsa_get_blinding(rsa, ctx);
603 if (blinding == NULL) {
604 ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
605 goto err;
606 }
607
608 if (!rsa_blinding_convert(blinding, f, ctx))
609 goto err;
610 }
611
612 /* do the decrypt */
613 if ((rsa->flags & RSA_FLAG_EXT_PKEY) || (rsa->version == RSA_ASN1_VERSION_MULTI) || ((rsa->p != NULL) && (rsa->q != NULL) && (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
614 if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
615 goto err;
616 } else {
617 BIGNUM *d = BN_new();
618 if (d == NULL) {
619 ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
620 goto err;
621 }
622 if (rsa->d == NULL) {
623 ERR_raise(ERR_LIB_RSA, RSA_R_MISSING_PRIVATE_KEY);
624 BN_free(d);
625 goto err;
626 }
627 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
628 if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
629 rsa->_method_mod_n)) {
630 BN_free(d);
631 goto err;
632 }
633 /* We MUST free d before any further use of rsa->d */
634 BN_free(d);
635 }
636
637 if (blinding)
638 if (!rsa_blinding_invert(blinding, ret, ctx))
639 goto err;
640
641 /*
642 * derive the Key Derivation Key from private exponent and public
643 * ciphertext
644 */
645 if (padding == RSA_PKCS1_PADDING) {
646 if (derive_kdk(flen, from, rsa, buf, num, kdk) == 0)
647 goto err;
648 }
649
650 j = BN_bn2binpad(ret, buf, num);
651 if (j < 0)
652 goto err;
653
654 switch (padding) {
655 case RSA_PKCS1_NO_IMPLICIT_REJECT_PADDING:
656 r = RSA_padding_check_PKCS1_type_2(to, num, buf, j, num);
657 break;
658 case RSA_PKCS1_PADDING:
659 r = ossl_rsa_padding_check_PKCS1_type_2(rsa->libctx, to, num, buf, j, num, kdk);
660 break;
661 case RSA_PKCS1_OAEP_PADDING:
662 r = RSA_padding_check_PKCS1_OAEP(to, num, buf, j, num, NULL, 0);
663 break;
664 case RSA_NO_PADDING:
665 memcpy(to, buf, (r = j));
666 break;
667 default:
668 ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
669 goto err;
670 }
671 #ifndef FIPS_MODULE
672 /*
673 * This trick doesn't work in the FIPS provider because libcrypto manages
674 * the error stack. Instead we opt not to put an error on the stack at all
675 * in case of padding failure in the FIPS provider.
676 */
677 ERR_raise(ERR_LIB_RSA, RSA_R_PADDING_CHECK_FAILED);
678 err_clear_last_constant_time(1 & ~constant_time_msb(r));
679 #endif
680
681 err:
682 BN_CTX_end(ctx);
683 BN_CTX_free(ctx);
684 OPENSSL_clear_free(buf, num);
685 return r;
686 }
687
688 /* signature verification */
689 static int rsa_ossl_public_decrypt(int flen, const unsigned char *from,
690 unsigned char *to, RSA *rsa, int padding)
691 {
692 BIGNUM *f, *ret;
693 int i, num = 0, r = -1;
694 unsigned char *buf = NULL;
695 BN_CTX *ctx = NULL;
696
697 if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
698 ERR_raise(ERR_LIB_RSA, RSA_R_MODULUS_TOO_LARGE);
699 return -1;
700 }
701
702 if (BN_ucmp(rsa->n, rsa->e) <= 0) {
703 ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
704 return -1;
705 }
706
707 /* for large moduli, enforce exponent limit */
708 if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
709 if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
710 ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
711 return -1;
712 }
713 }
714
715 if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
716 goto err;
717 BN_CTX_start(ctx);
718 f = BN_CTX_get(ctx);
719 ret = BN_CTX_get(ctx);
720 if (ret == NULL) {
721 ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
722 goto err;
723 }
724 num = BN_num_bytes(rsa->n);
725 buf = OPENSSL_malloc(num);
726 if (buf == NULL)
727 goto err;
728
729 /*
730 * This check was for equality but PGP does evil things and chops off the
731 * top '0' bytes
732 */
733 if (flen > num) {
734 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_GREATER_THAN_MOD_LEN);
735 goto err;
736 }
737
738 if (BN_bin2bn(from, flen, f) == NULL)
739 goto err;
740
741 if (BN_ucmp(f, rsa->n) >= 0) {
742 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
743 goto err;
744 }
745
746 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
747 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
748 rsa->n, ctx))
749 goto err;
750
751 if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
752 rsa->_method_mod_n))
753 goto err;
754
755 /* For X9.31: Assuming e is odd it does a 12 mod 16 test */
756 if ((padding == RSA_X931_PADDING) && ((bn_get_words(ret)[0] & 0xf) != 12))
757 if (!BN_sub(ret, rsa->n, ret))
758 goto err;
759
760 i = BN_bn2binpad(ret, buf, num);
761 if (i < 0)
762 goto err;
763
764 switch (padding) {
765 case RSA_PKCS1_PADDING:
766 r = RSA_padding_check_PKCS1_type_1(to, num, buf, i, num);
767 break;
768 case RSA_X931_PADDING:
769 r = RSA_padding_check_X931(to, num, buf, i, num);
770 break;
771 case RSA_NO_PADDING:
772 memcpy(to, buf, (r = i));
773 break;
774 default:
775 ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
776 goto err;
777 }
778 if (r < 0)
779 ERR_raise(ERR_LIB_RSA, RSA_R_PADDING_CHECK_FAILED);
780
781 err:
782 BN_CTX_end(ctx);
783 BN_CTX_free(ctx);
784 OPENSSL_clear_free(buf, num);
785 return r;
786 }
787
788 static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
789 {
790 BIGNUM *r1, *m1, *vrfy;
791 int ret = 0, smooth = 0;
792 #ifndef FIPS_MODULE
793 BIGNUM *r2, *m[RSA_MAX_PRIME_NUM - 2];
794 int i, ex_primes = 0;
795 RSA_PRIME_INFO *pinfo;
796 #endif
797
798 BN_CTX_start(ctx);
799
800 r1 = BN_CTX_get(ctx);
801 #ifndef FIPS_MODULE
802 r2 = BN_CTX_get(ctx);
803 #endif
804 m1 = BN_CTX_get(ctx);
805 vrfy = BN_CTX_get(ctx);
806 if (vrfy == NULL)
807 goto err;
808
809 #ifndef FIPS_MODULE
810 if (rsa->version == RSA_ASN1_VERSION_MULTI
811 && ((ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) <= 0
812 || ex_primes > RSA_MAX_PRIME_NUM - 2))
813 goto err;
814 #endif
815
816 if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {
817 BIGNUM *factor = BN_new();
818
819 if (factor == NULL)
820 goto err;
821
822 /*
823 * Make sure BN_mod_inverse in Montgomery initialization uses the
824 * BN_FLG_CONSTTIME flag
825 */
826 if (!(BN_with_flags(factor, rsa->p, BN_FLG_CONSTTIME),
827 BN_MONT_CTX_set_locked(&rsa->_method_mod_p, rsa->lock,
828 factor, ctx))
829 || !(BN_with_flags(factor, rsa->q, BN_FLG_CONSTTIME),
830 BN_MONT_CTX_set_locked(&rsa->_method_mod_q, rsa->lock,
831 factor, ctx))) {
832 BN_free(factor);
833 goto err;
834 }
835 #ifndef FIPS_MODULE
836 for (i = 0; i < ex_primes; i++) {
837 pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
838 BN_with_flags(factor, pinfo->r, BN_FLG_CONSTTIME);
839 if (!BN_MONT_CTX_set_locked(&pinfo->m, rsa->lock, factor, ctx)) {
840 BN_free(factor);
841 goto err;
842 }
843 }
844 #endif
845 /*
846 * We MUST free |factor| before any further use of the prime factors
847 */
848 BN_free(factor);
849
850 smooth = (rsa->meth->bn_mod_exp == BN_mod_exp_mont)
851 #ifndef FIPS_MODULE
852 && (ex_primes == 0)
853 #endif
854 && (BN_num_bits(rsa->q) == BN_num_bits(rsa->p));
855 }
856
857 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
858 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
859 rsa->n, ctx))
860 goto err;
861
862 if (smooth) {
863 /*
864 * Conversion from Montgomery domain, a.k.a. Montgomery reduction,
865 * accepts values in [0-m*2^w) range. w is m's bit width rounded up
866 * to limb width. So that at the very least if |I| is fully reduced,
867 * i.e. less than p*q, we can count on from-to round to perform
868 * below modulo operations on |I|. Unlike BN_mod it's constant time.
869 */
870 if (/* m1 = I moq q */
871 !bn_from_mont_fixed_top(m1, I, rsa->_method_mod_q, ctx)
872 || !bn_to_mont_fixed_top(m1, m1, rsa->_method_mod_q, ctx)
873 /* r1 = I mod p */
874 || !bn_from_mont_fixed_top(r1, I, rsa->_method_mod_p, ctx)
875 || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)
876 /*
877 * Use parallel exponentiations optimization if possible,
878 * otherwise fallback to two sequential exponentiations:
879 * m1 = m1^dmq1 mod q
880 * r1 = r1^dmp1 mod p
881 */
882 || !BN_mod_exp_mont_consttime_x2(m1, m1, rsa->dmq1, rsa->q,
883 rsa->_method_mod_q,
884 r1, r1, rsa->dmp1, rsa->p,
885 rsa->_method_mod_p,
886 ctx)
887 /* r1 = (r1 - m1) mod p */
888 /*
889 * bn_mod_sub_fixed_top is not regular modular subtraction,
890 * it can tolerate subtrahend to be larger than modulus, but
891 * not bit-wise wider. This makes up for uncommon q>p case,
892 * when |m1| can be larger than |rsa->p|.
893 */
894 || !bn_mod_sub_fixed_top(r1, r1, m1, rsa->p)
895
896 /* r1 = r1 * iqmp mod p */
897 || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)
898 || !bn_mul_mont_fixed_top(r1, r1, rsa->iqmp, rsa->_method_mod_p,
899 ctx)
900 /* r0 = r1 * q + m1 */
901 || !bn_mul_fixed_top(r0, r1, rsa->q, ctx)
902 || !bn_mod_add_fixed_top(r0, r0, m1, rsa->n))
903 goto err;
904
905 goto tail;
906 }
907
908 /* compute I mod q */
909 {
910 BIGNUM *c = BN_new();
911 if (c == NULL)
912 goto err;
913 BN_with_flags(c, I, BN_FLG_CONSTTIME);
914
915 if (!BN_mod(r1, c, rsa->q, ctx)) {
916 BN_free(c);
917 goto err;
918 }
919
920 {
921 BIGNUM *dmq1 = BN_new();
922 if (dmq1 == NULL) {
923 BN_free(c);
924 goto err;
925 }
926 BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
927
928 /* compute r1^dmq1 mod q */
929 if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx,
930 rsa->_method_mod_q)) {
931 BN_free(c);
932 BN_free(dmq1);
933 goto err;
934 }
935 /* We MUST free dmq1 before any further use of rsa->dmq1 */
936 BN_free(dmq1);
937 }
938
939 /* compute I mod p */
940 if (!BN_mod(r1, c, rsa->p, ctx)) {
941 BN_free(c);
942 goto err;
943 }
944 /* We MUST free c before any further use of I */
945 BN_free(c);
946 }
947
948 {
949 BIGNUM *dmp1 = BN_new();
950 if (dmp1 == NULL)
951 goto err;
952 BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
953
954 /* compute r1^dmp1 mod p */
955 if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx,
956 rsa->_method_mod_p)) {
957 BN_free(dmp1);
958 goto err;
959 }
960 /* We MUST free dmp1 before any further use of rsa->dmp1 */
961 BN_free(dmp1);
962 }
963
964 #ifndef FIPS_MODULE
965 if (ex_primes > 0) {
966 BIGNUM *di = BN_new(), *cc = BN_new();
967
968 if (cc == NULL || di == NULL) {
969 BN_free(cc);
970 BN_free(di);
971 goto err;
972 }
973
974 for (i = 0; i < ex_primes; i++) {
975 /* prepare m_i */
976 if ((m[i] = BN_CTX_get(ctx)) == NULL) {
977 BN_free(cc);
978 BN_free(di);
979 goto err;
980 }
981
982 pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
983
984 /* prepare c and d_i */
985 BN_with_flags(cc, I, BN_FLG_CONSTTIME);
986 BN_with_flags(di, pinfo->d, BN_FLG_CONSTTIME);
987
988 if (!BN_mod(r1, cc, pinfo->r, ctx)) {
989 BN_free(cc);
990 BN_free(di);
991 goto err;
992 }
993 /* compute r1 ^ d_i mod r_i */
994 if (!rsa->meth->bn_mod_exp(m[i], r1, di, pinfo->r, ctx, pinfo->m)) {
995 BN_free(cc);
996 BN_free(di);
997 goto err;
998 }
999 }
1000
1001 BN_free(cc);
1002 BN_free(di);
1003 }
1004 #endif
1005
1006 if (!BN_sub(r0, r0, m1))
1007 goto err;
1008 /*
1009 * This will help stop the size of r0 increasing, which does affect the
1010 * multiply if it optimised for a power of 2 size
1011 */
1012 if (BN_is_negative(r0))
1013 if (!BN_add(r0, r0, rsa->p))
1014 goto err;
1015
1016 if (!BN_mul(r1, r0, rsa->iqmp, ctx))
1017 goto err;
1018
1019 {
1020 BIGNUM *pr1 = BN_new();
1021 if (pr1 == NULL)
1022 goto err;
1023 BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
1024
1025 if (!BN_mod(r0, pr1, rsa->p, ctx)) {
1026 BN_free(pr1);
1027 goto err;
1028 }
1029 /* We MUST free pr1 before any further use of r1 */
1030 BN_free(pr1);
1031 }
1032
1033 /*
1034 * If p < q it is occasionally possible for the correction of adding 'p'
1035 * if r0 is negative above to leave the result still negative. This can
1036 * break the private key operations: the following second correction
1037 * should *always* correct this rare occurrence. This will *never* happen
1038 * with OpenSSL generated keys because they ensure p > q [steve]
1039 */
1040 if (BN_is_negative(r0))
1041 if (!BN_add(r0, r0, rsa->p))
1042 goto err;
1043 if (!BN_mul(r1, r0, rsa->q, ctx))
1044 goto err;
1045 if (!BN_add(r0, r1, m1))
1046 goto err;
1047
1048 #ifndef FIPS_MODULE
1049 /* add m_i to m in multi-prime case */
1050 if (ex_primes > 0) {
1051 BIGNUM *pr2 = BN_new();
1052
1053 if (pr2 == NULL)
1054 goto err;
1055
1056 for (i = 0; i < ex_primes; i++) {
1057 pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
1058 if (!BN_sub(r1, m[i], r0)) {
1059 BN_free(pr2);
1060 goto err;
1061 }
1062
1063 if (!BN_mul(r2, r1, pinfo->t, ctx)) {
1064 BN_free(pr2);
1065 goto err;
1066 }
1067
1068 BN_with_flags(pr2, r2, BN_FLG_CONSTTIME);
1069
1070 if (!BN_mod(r1, pr2, pinfo->r, ctx)) {
1071 BN_free(pr2);
1072 goto err;
1073 }
1074
1075 if (BN_is_negative(r1))
1076 if (!BN_add(r1, r1, pinfo->r)) {
1077 BN_free(pr2);
1078 goto err;
1079 }
1080 if (!BN_mul(r1, r1, pinfo->pp, ctx)) {
1081 BN_free(pr2);
1082 goto err;
1083 }
1084 if (!BN_add(r0, r0, r1)) {
1085 BN_free(pr2);
1086 goto err;
1087 }
1088 }
1089 BN_free(pr2);
1090 }
1091 #endif
1092
1093 tail:
1094 if (rsa->e && rsa->n) {
1095 if (rsa->meth->bn_mod_exp == BN_mod_exp_mont) {
1096 if (!BN_mod_exp_mont(vrfy, r0, rsa->e, rsa->n, ctx,
1097 rsa->_method_mod_n))
1098 goto err;
1099 } else {
1100 bn_correct_top(r0);
1101 if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,
1102 rsa->_method_mod_n))
1103 goto err;
1104 }
1105 /*
1106 * If 'I' was greater than (or equal to) rsa->n, the operation will
1107 * be equivalent to using 'I mod n'. However, the result of the
1108 * verify will *always* be less than 'n' so we don't check for
1109 * absolute equality, just congruency.
1110 */
1111 if (!BN_sub(vrfy, vrfy, I))
1112 goto err;
1113 if (BN_is_zero(vrfy)) {
1114 bn_correct_top(r0);
1115 ret = 1;
1116 goto err; /* not actually error */
1117 }
1118 if (!BN_mod(vrfy, vrfy, rsa->n, ctx))
1119 goto err;
1120 if (BN_is_negative(vrfy))
1121 if (!BN_add(vrfy, vrfy, rsa->n))
1122 goto err;
1123 if (!BN_is_zero(vrfy)) {
1124 /*
1125 * 'I' and 'vrfy' aren't congruent mod n. Don't leak
1126 * miscalculated CRT output, just do a raw (slower) mod_exp and
1127 * return that instead.
1128 */
1129
1130 BIGNUM *d = BN_new();
1131 if (d == NULL)
1132 goto err;
1133 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
1134
1135 if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx,
1136 rsa->_method_mod_n)) {
1137 BN_free(d);
1138 goto err;
1139 }
1140 /* We MUST free d before any further use of rsa->d */
1141 BN_free(d);
1142 }
1143 }
1144 /*
1145 * It's unfortunate that we have to bn_correct_top(r0). What hopefully
1146 * saves the day is that correction is highly unlike, and private key
1147 * operations are customarily performed on blinded message. Which means
1148 * that attacker won't observe correlation with chosen plaintext.
1149 * Secondly, remaining code would still handle it in same computational
1150 * time and even conceal memory access pattern around corrected top.
1151 */
1152 bn_correct_top(r0);
1153 ret = 1;
1154 err:
1155 BN_CTX_end(ctx);
1156 return ret;
1157 }
1158
1159 static int rsa_ossl_init(RSA *rsa)
1160 {
1161 rsa->flags |= RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE;
1162 return 1;
1163 }
1164
1165 static int rsa_ossl_finish(RSA *rsa)
1166 {
1167 #ifndef FIPS_MODULE
1168 int i;
1169 RSA_PRIME_INFO *pinfo;
1170
1171 for (i = 0; i < sk_RSA_PRIME_INFO_num(rsa->prime_infos); i++) {
1172 pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
1173 BN_MONT_CTX_free(pinfo->m);
1174 }
1175 #endif
1176
1177 BN_MONT_CTX_free(rsa->_method_mod_n);
1178 BN_MONT_CTX_free(rsa->_method_mod_p);
1179 BN_MONT_CTX_free(rsa->_method_mod_q);
1180 return 1;
1181 }
1182
1183 #ifdef S390X_MOD_EXP
1184 static int rsa_ossl_s390x_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa,
1185 BN_CTX *ctx)
1186 {
1187 if (rsa->version != RSA_ASN1_VERSION_MULTI) {
1188 if (s390x_crt(r0, i, rsa->p, rsa->q, rsa->dmp1, rsa->dmq1, rsa->iqmp) == 1)
1189 return 1;
1190 }
1191 return rsa_ossl_mod_exp(r0, i, rsa, ctx);
1192 }
1193
1194 #endif