2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
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
11 * RSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
16 #include "internal/cryptlib.h"
17 #include "crypto/bn.h"
18 #include "rsa_local.h"
19 #include "internal/constant_time.h"
21 static int rsa_ossl_public_encrypt(int flen, const unsigned char *from,
22 unsigned char *to, RSA *rsa, int padding);
23 static int rsa_ossl_private_encrypt(int flen, const unsigned char *from,
24 unsigned char *to, RSA *rsa, int padding);
25 static int rsa_ossl_public_decrypt(int flen, const unsigned char *from,
26 unsigned char *to, RSA *rsa, int padding);
27 static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
28 unsigned char *to, RSA *rsa, int padding);
29 static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa,
31 static int rsa_ossl_init(RSA *rsa);
32 static int rsa_ossl_finish(RSA *rsa);
33 static RSA_METHOD rsa_pkcs1_ossl_meth = {
35 rsa_ossl_public_encrypt,
36 rsa_ossl_public_decrypt, /* signature verification */
37 rsa_ossl_private_encrypt, /* signing */
38 rsa_ossl_private_decrypt,
40 BN_mod_exp_mont, /* XXX probably we should not use Montgomery
44 RSA_FLAG_FIPS_METHOD, /* flags */
48 NULL, /* rsa_keygen */
49 NULL /* rsa_multi_prime_keygen */
52 static const RSA_METHOD *default_RSA_meth = &rsa_pkcs1_ossl_meth;
54 void RSA_set_default_method(const RSA_METHOD *meth)
56 default_RSA_meth = meth;
59 const RSA_METHOD *RSA_get_default_method(void)
61 return default_RSA_meth;
64 const RSA_METHOD *RSA_PKCS1_OpenSSL(void)
66 return &rsa_pkcs1_ossl_meth;
69 const RSA_METHOD *RSA_null_method(void)
74 static int rsa_ossl_public_encrypt(int flen, const unsigned char *from,
75 unsigned char *to, RSA *rsa, int padding)
78 int i, num = 0, r = -1;
79 unsigned char *buf = NULL;
82 if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
83 ERR_raise(ERR_LIB_RSA, RSA_R_MODULUS_TOO_LARGE);
87 if (BN_ucmp(rsa->n, rsa->e) <= 0) {
88 ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
92 /* for large moduli, enforce exponent limit */
93 if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
94 if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
95 ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
100 if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
104 ret = BN_CTX_get(ctx);
105 num = BN_num_bytes(rsa->n);
106 buf = OPENSSL_malloc(num);
107 if (ret == NULL || buf == NULL) {
108 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
113 case RSA_PKCS1_PADDING:
114 i = ossl_rsa_padding_add_PKCS1_type_2_ex(rsa->libctx, buf, num,
117 case RSA_PKCS1_OAEP_PADDING:
118 i = ossl_rsa_padding_add_PKCS1_OAEP_mgf1_ex(rsa->libctx, buf, num,
123 i = RSA_padding_add_none(buf, num, from, flen);
126 ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
132 if (BN_bin2bn(buf, num, f) == NULL)
135 if (BN_ucmp(f, rsa->n) >= 0) {
136 /* usually the padding functions would catch this */
137 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
141 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
142 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
146 if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
151 * BN_bn2binpad puts in leading 0 bytes if the number is less than
152 * the length of the modulus.
154 r = BN_bn2binpad(ret, to, num);
158 OPENSSL_clear_free(buf, num);
162 static BN_BLINDING *rsa_get_blinding(RSA *rsa, int *local, BN_CTX *ctx)
166 if (!CRYPTO_THREAD_write_lock(rsa->lock))
169 if (rsa->blinding == NULL) {
170 rsa->blinding = RSA_setup_blinding(rsa, ctx);
177 if (BN_BLINDING_is_current_thread(ret)) {
178 /* rsa->blinding is ours! */
182 /* resort to rsa->mt_blinding instead */
185 * instructs rsa_blinding_convert(), rsa_blinding_invert() that the
186 * BN_BLINDING is shared, meaning that accesses require locks, and
187 * that the blinding factor must be stored outside the BN_BLINDING
191 if (rsa->mt_blinding == NULL) {
192 rsa->mt_blinding = RSA_setup_blinding(rsa, ctx);
194 ret = rsa->mt_blinding;
198 CRYPTO_THREAD_unlock(rsa->lock);
202 static int rsa_blinding_convert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind,
205 if (unblind == NULL) {
207 * Local blinding: store the unblinding factor in BN_BLINDING.
209 return BN_BLINDING_convert_ex(f, NULL, b, ctx);
212 * Shared blinding: store the unblinding factor outside BN_BLINDING.
217 ret = BN_BLINDING_convert_ex(f, unblind, b, ctx);
218 BN_BLINDING_unlock(b);
224 static int rsa_blinding_invert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind,
228 * For local blinding, unblind is set to NULL, and BN_BLINDING_invert_ex
229 * will use the unblinding factor stored in BN_BLINDING. If BN_BLINDING
230 * is shared between threads, unblind must be non-null:
231 * BN_BLINDING_invert_ex will then use the local unblinding factor, and
232 * will only read the modulus from BN_BLINDING. In both cases it's safe
233 * to access the blinding without a lock.
235 return BN_BLINDING_invert_ex(f, unblind, b, ctx);
239 static int rsa_ossl_private_encrypt(int flen, const unsigned char *from,
240 unsigned char *to, RSA *rsa, int padding)
242 BIGNUM *f, *ret, *res;
243 int i, num = 0, r = -1;
244 unsigned char *buf = NULL;
246 int local_blinding = 0;
248 * Used only if the blinding structure is shared. A non-NULL unblind
249 * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
250 * the unblinding factor outside the blinding structure.
252 BIGNUM *unblind = NULL;
253 BN_BLINDING *blinding = NULL;
255 if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
259 ret = BN_CTX_get(ctx);
260 num = BN_num_bytes(rsa->n);
261 buf = OPENSSL_malloc(num);
262 if (ret == NULL || buf == NULL) {
263 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
268 case RSA_PKCS1_PADDING:
269 i = RSA_padding_add_PKCS1_type_1(buf, num, from, flen);
271 case RSA_X931_PADDING:
272 i = RSA_padding_add_X931(buf, num, from, flen);
275 i = RSA_padding_add_none(buf, num, from, flen);
278 ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
284 if (BN_bin2bn(buf, num, f) == NULL)
287 if (BN_ucmp(f, rsa->n) >= 0) {
288 /* usually the padding functions would catch this */
289 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
293 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
294 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
298 if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
299 blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
300 if (blinding == NULL) {
301 ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
306 if (blinding != NULL) {
307 if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {
308 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
311 if (!rsa_blinding_convert(blinding, f, unblind, ctx))
315 if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
316 (rsa->version == RSA_ASN1_VERSION_MULTI) ||
319 (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
320 if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
323 BIGNUM *d = BN_new();
325 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
328 if (rsa->d == NULL) {
329 ERR_raise(ERR_LIB_RSA, RSA_R_MISSING_PRIVATE_KEY);
333 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
335 if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
336 rsa->_method_mod_n)) {
340 /* We MUST free d before any further use of rsa->d */
345 if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
348 if (padding == RSA_X931_PADDING) {
349 if (!BN_sub(f, rsa->n, ret))
351 if (BN_cmp(ret, f) > 0)
360 * BN_bn2binpad puts in leading 0 bytes if the number is less than
361 * the length of the modulus.
363 r = BN_bn2binpad(res, to, num);
367 OPENSSL_clear_free(buf, num);
371 static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
372 unsigned char *to, RSA *rsa, int padding)
375 int j, num = 0, r = -1;
376 unsigned char *buf = NULL;
378 int local_blinding = 0;
380 * Used only if the blinding structure is shared. A non-NULL unblind
381 * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
382 * the unblinding factor outside the blinding structure.
384 BIGNUM *unblind = NULL;
385 BN_BLINDING *blinding = NULL;
387 if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
391 ret = BN_CTX_get(ctx);
392 num = BN_num_bytes(rsa->n);
393 buf = OPENSSL_malloc(num);
394 if (ret == NULL || buf == NULL) {
395 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
400 * This check was for equality but PGP does evil things and chops off the
404 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_GREATER_THAN_MOD_LEN);
408 /* make data into a big number */
409 if (BN_bin2bn(from, (int)flen, f) == NULL)
412 if (BN_ucmp(f, rsa->n) >= 0) {
413 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
417 if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
418 blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
419 if (blinding == NULL) {
420 ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
425 if (blinding != NULL) {
426 if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {
427 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
430 if (!rsa_blinding_convert(blinding, f, unblind, ctx))
435 if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
436 (rsa->version == RSA_ASN1_VERSION_MULTI) ||
439 (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
440 if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
443 BIGNUM *d = BN_new();
445 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
448 if (rsa->d == NULL) {
449 ERR_raise(ERR_LIB_RSA, RSA_R_MISSING_PRIVATE_KEY);
453 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
455 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
456 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
461 if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
462 rsa->_method_mod_n)) {
466 /* We MUST free d before any further use of rsa->d */
471 if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
474 j = BN_bn2binpad(ret, buf, num);
479 case RSA_PKCS1_PADDING:
480 r = RSA_padding_check_PKCS1_type_2(to, num, buf, j, num);
482 case RSA_PKCS1_OAEP_PADDING:
483 r = RSA_padding_check_PKCS1_OAEP(to, num, buf, j, num, NULL, 0);
486 memcpy(to, buf, (r = j));
489 ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
494 * This trick doesn't work in the FIPS provider because libcrypto manages
495 * the error stack. Instead we opt not to put an error on the stack at all
496 * in case of padding failure in the FIPS provider.
498 ERR_raise(ERR_LIB_RSA, RSA_R_PADDING_CHECK_FAILED);
499 err_clear_last_constant_time(1 & ~constant_time_msb(r));
505 OPENSSL_clear_free(buf, num);
509 /* signature verification */
510 static int rsa_ossl_public_decrypt(int flen, const unsigned char *from,
511 unsigned char *to, RSA *rsa, int padding)
514 int i, num = 0, r = -1;
515 unsigned char *buf = NULL;
518 if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
519 ERR_raise(ERR_LIB_RSA, RSA_R_MODULUS_TOO_LARGE);
523 if (BN_ucmp(rsa->n, rsa->e) <= 0) {
524 ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
528 /* for large moduli, enforce exponent limit */
529 if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
530 if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
531 ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
536 if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
540 ret = BN_CTX_get(ctx);
541 num = BN_num_bytes(rsa->n);
542 buf = OPENSSL_malloc(num);
543 if (ret == NULL || buf == NULL) {
544 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
549 * This check was for equality but PGP does evil things and chops off the
553 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_GREATER_THAN_MOD_LEN);
557 if (BN_bin2bn(from, flen, f) == NULL)
560 if (BN_ucmp(f, rsa->n) >= 0) {
561 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
565 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
566 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
570 if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
574 if ((padding == RSA_X931_PADDING) && ((bn_get_words(ret)[0] & 0xf) != 12))
575 if (!BN_sub(ret, rsa->n, ret))
578 i = BN_bn2binpad(ret, buf, num);
583 case RSA_PKCS1_PADDING:
584 r = RSA_padding_check_PKCS1_type_1(to, num, buf, i, num);
586 case RSA_X931_PADDING:
587 r = RSA_padding_check_X931(to, num, buf, i, num);
590 memcpy(to, buf, (r = i));
593 ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
597 ERR_raise(ERR_LIB_RSA, RSA_R_PADDING_CHECK_FAILED);
602 OPENSSL_clear_free(buf, num);
606 static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
608 BIGNUM *r1, *m1, *vrfy;
609 int ret = 0, smooth = 0;
611 BIGNUM *r2, *m[RSA_MAX_PRIME_NUM - 2];
612 int i, ex_primes = 0;
613 RSA_PRIME_INFO *pinfo;
618 r1 = BN_CTX_get(ctx);
620 r2 = BN_CTX_get(ctx);
622 m1 = BN_CTX_get(ctx);
623 vrfy = BN_CTX_get(ctx);
628 if (rsa->version == RSA_ASN1_VERSION_MULTI
629 && ((ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) <= 0
630 || ex_primes > RSA_MAX_PRIME_NUM - 2))
634 if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {
635 BIGNUM *factor = BN_new();
641 * Make sure BN_mod_inverse in Montgomery initialization uses the
642 * BN_FLG_CONSTTIME flag
644 if (!(BN_with_flags(factor, rsa->p, BN_FLG_CONSTTIME),
645 BN_MONT_CTX_set_locked(&rsa->_method_mod_p, rsa->lock,
647 || !(BN_with_flags(factor, rsa->q, BN_FLG_CONSTTIME),
648 BN_MONT_CTX_set_locked(&rsa->_method_mod_q, rsa->lock,
654 for (i = 0; i < ex_primes; i++) {
655 pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
656 BN_with_flags(factor, pinfo->r, BN_FLG_CONSTTIME);
657 if (!BN_MONT_CTX_set_locked(&pinfo->m, rsa->lock, factor, ctx)) {
664 * We MUST free |factor| before any further use of the prime factors
668 smooth = (rsa->meth->bn_mod_exp == BN_mod_exp_mont)
672 && (BN_num_bits(rsa->q) == BN_num_bits(rsa->p));
675 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
676 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
682 * Conversion from Montgomery domain, a.k.a. Montgomery reduction,
683 * accepts values in [0-m*2^w) range. w is m's bit width rounded up
684 * to limb width. So that at the very least if |I| is fully reduced,
685 * i.e. less than p*q, we can count on from-to round to perform
686 * below modulo operations on |I|. Unlike BN_mod it's constant time.
688 if (/* m1 = I moq q */
689 !bn_from_mont_fixed_top(m1, I, rsa->_method_mod_q, ctx)
690 || !bn_to_mont_fixed_top(m1, m1, rsa->_method_mod_q, ctx)
692 || !bn_from_mont_fixed_top(r1, I, rsa->_method_mod_p, ctx)
693 || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)
695 * Use parallel exponentiations optimization if possible,
696 * otherwise fallback to two sequential exponentiations:
700 || !BN_mod_exp_mont_consttime_x2(m1, m1, rsa->dmq1, rsa->q,
702 r1, r1, rsa->dmp1, rsa->p,
705 /* r1 = (r1 - m1) mod p */
707 * bn_mod_sub_fixed_top is not regular modular subtraction,
708 * it can tolerate subtrahend to be larger than modulus, but
709 * not bit-wise wider. This makes up for uncommon q>p case,
710 * when |m1| can be larger than |rsa->p|.
712 || !bn_mod_sub_fixed_top(r1, r1, m1, rsa->p)
714 /* r1 = r1 * iqmp mod p */
715 || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)
716 || !bn_mul_mont_fixed_top(r1, r1, rsa->iqmp, rsa->_method_mod_p,
718 /* r0 = r1 * q + m1 */
719 || !bn_mul_fixed_top(r0, r1, rsa->q, ctx)
720 || !bn_mod_add_fixed_top(r0, r0, m1, rsa->n))
726 /* compute I mod q */
728 BIGNUM *c = BN_new();
731 BN_with_flags(c, I, BN_FLG_CONSTTIME);
733 if (!BN_mod(r1, c, rsa->q, ctx)) {
739 BIGNUM *dmq1 = BN_new();
744 BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
746 /* compute r1^dmq1 mod q */
747 if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx,
748 rsa->_method_mod_q)) {
753 /* We MUST free dmq1 before any further use of rsa->dmq1 */
757 /* compute I mod p */
758 if (!BN_mod(r1, c, rsa->p, ctx)) {
762 /* We MUST free c before any further use of I */
767 BIGNUM *dmp1 = BN_new();
770 BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
772 /* compute r1^dmp1 mod p */
773 if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx,
774 rsa->_method_mod_p)) {
778 /* We MUST free dmp1 before any further use of rsa->dmp1 */
784 BIGNUM *di = BN_new(), *cc = BN_new();
786 if (cc == NULL || di == NULL) {
792 for (i = 0; i < ex_primes; i++) {
794 if ((m[i] = BN_CTX_get(ctx)) == NULL) {
800 pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
802 /* prepare c and d_i */
803 BN_with_flags(cc, I, BN_FLG_CONSTTIME);
804 BN_with_flags(di, pinfo->d, BN_FLG_CONSTTIME);
806 if (!BN_mod(r1, cc, pinfo->r, ctx)) {
811 /* compute r1 ^ d_i mod r_i */
812 if (!rsa->meth->bn_mod_exp(m[i], r1, di, pinfo->r, ctx, pinfo->m)) {
824 if (!BN_sub(r0, r0, m1))
827 * This will help stop the size of r0 increasing, which does affect the
828 * multiply if it optimised for a power of 2 size
830 if (BN_is_negative(r0))
831 if (!BN_add(r0, r0, rsa->p))
834 if (!BN_mul(r1, r0, rsa->iqmp, ctx))
838 BIGNUM *pr1 = BN_new();
841 BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
843 if (!BN_mod(r0, pr1, rsa->p, ctx)) {
847 /* We MUST free pr1 before any further use of r1 */
852 * If p < q it is occasionally possible for the correction of adding 'p'
853 * if r0 is negative above to leave the result still negative. This can
854 * break the private key operations: the following second correction
855 * should *always* correct this rare occurrence. This will *never* happen
856 * with OpenSSL generated keys because they ensure p > q [steve]
858 if (BN_is_negative(r0))
859 if (!BN_add(r0, r0, rsa->p))
861 if (!BN_mul(r1, r0, rsa->q, ctx))
863 if (!BN_add(r0, r1, m1))
867 /* add m_i to m in multi-prime case */
869 BIGNUM *pr2 = BN_new();
874 for (i = 0; i < ex_primes; i++) {
875 pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
876 if (!BN_sub(r1, m[i], r0)) {
881 if (!BN_mul(r2, r1, pinfo->t, ctx)) {
886 BN_with_flags(pr2, r2, BN_FLG_CONSTTIME);
888 if (!BN_mod(r1, pr2, pinfo->r, ctx)) {
893 if (BN_is_negative(r1))
894 if (!BN_add(r1, r1, pinfo->r)) {
898 if (!BN_mul(r1, r1, pinfo->pp, ctx)) {
902 if (!BN_add(r0, r0, r1)) {
912 if (rsa->e && rsa->n) {
913 if (rsa->meth->bn_mod_exp == BN_mod_exp_mont) {
914 if (!BN_mod_exp_mont(vrfy, r0, rsa->e, rsa->n, ctx,
919 if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,
924 * If 'I' was greater than (or equal to) rsa->n, the operation will
925 * be equivalent to using 'I mod n'. However, the result of the
926 * verify will *always* be less than 'n' so we don't check for
927 * absolute equality, just congruency.
929 if (!BN_sub(vrfy, vrfy, I))
931 if (BN_is_zero(vrfy)) {
934 goto err; /* not actually error */
936 if (!BN_mod(vrfy, vrfy, rsa->n, ctx))
938 if (BN_is_negative(vrfy))
939 if (!BN_add(vrfy, vrfy, rsa->n))
941 if (!BN_is_zero(vrfy)) {
943 * 'I' and 'vrfy' aren't congruent mod n. Don't leak
944 * miscalculated CRT output, just do a raw (slower) mod_exp and
945 * return that instead.
948 BIGNUM *d = BN_new();
951 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
953 if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx,
954 rsa->_method_mod_n)) {
958 /* We MUST free d before any further use of rsa->d */
963 * It's unfortunate that we have to bn_correct_top(r0). What hopefully
964 * saves the day is that correction is highly unlike, and private key
965 * operations are customarily performed on blinded message. Which means
966 * that attacker won't observe correlation with chosen plaintext.
967 * Secondly, remaining code would still handle it in same computational
968 * time and even conceal memory access pattern around corrected top.
977 static int rsa_ossl_init(RSA *rsa)
979 rsa->flags |= RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE;
983 static int rsa_ossl_finish(RSA *rsa)
987 RSA_PRIME_INFO *pinfo;
989 for (i = 0; i < sk_RSA_PRIME_INFO_num(rsa->prime_infos); i++) {
990 pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
991 BN_MONT_CTX_free(pinfo->m);
995 BN_MONT_CTX_free(rsa->_method_mod_n);
996 BN_MONT_CTX_free(rsa->_method_mod_p);
997 BN_MONT_CTX_free(rsa->_method_mod_q);