2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
10 #include "internal/cryptlib.h"
11 #include "internal/bn_int.h"
14 static int rsa_ossl_public_encrypt(int flen, const unsigned char *from,
15 unsigned char *to, RSA *rsa, int padding);
16 static int rsa_ossl_private_encrypt(int flen, const unsigned char *from,
17 unsigned char *to, RSA *rsa, int padding);
18 static int rsa_ossl_public_decrypt(int flen, const unsigned char *from,
19 unsigned char *to, RSA *rsa, int padding);
20 static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
21 unsigned char *to, RSA *rsa, int padding);
22 static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa,
24 static int rsa_ossl_init(RSA *rsa);
25 static int rsa_ossl_finish(RSA *rsa);
26 static RSA_METHOD rsa_pkcs1_ossl_meth = {
28 rsa_ossl_public_encrypt,
29 rsa_ossl_public_decrypt, /* signature verification */
30 rsa_ossl_private_encrypt, /* signing */
31 rsa_ossl_private_decrypt,
33 BN_mod_exp_mont, /* XXX probably we should not use Montgomery
37 RSA_FLAG_FIPS_METHOD, /* flags */
41 NULL, /* rsa_keygen */
42 NULL /* rsa_multi_prime_keygen */
45 static const RSA_METHOD *default_RSA_meth = &rsa_pkcs1_ossl_meth;
47 void RSA_set_default_method(const RSA_METHOD *meth)
49 default_RSA_meth = meth;
52 const RSA_METHOD *RSA_get_default_method(void)
54 return default_RSA_meth;
57 const RSA_METHOD *RSA_PKCS1_OpenSSL(void)
59 return &rsa_pkcs1_ossl_meth;
62 const RSA_METHOD *RSA_null_method(void)
67 static int rsa_ossl_public_encrypt(int flen, const unsigned char *from,
68 unsigned char *to, RSA *rsa, int padding)
71 int i, j, k, num = 0, r = -1;
72 unsigned char *buf = NULL;
75 if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
76 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_MODULUS_TOO_LARGE);
80 if (BN_ucmp(rsa->n, rsa->e) <= 0) {
81 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);
85 /* for large moduli, enforce exponent limit */
86 if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
87 if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
88 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);
93 if ((ctx = BN_CTX_new()) == NULL)
97 ret = BN_CTX_get(ctx);
98 num = BN_num_bytes(rsa->n);
99 buf = OPENSSL_malloc(num);
100 if (ret == NULL || buf == NULL) {
101 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, ERR_R_MALLOC_FAILURE);
106 case RSA_PKCS1_PADDING:
107 i = RSA_padding_add_PKCS1_type_2(buf, num, from, flen);
109 case RSA_PKCS1_OAEP_PADDING:
110 i = RSA_padding_add_PKCS1_OAEP(buf, num, from, flen, NULL, 0);
112 case RSA_SSLV23_PADDING:
113 i = RSA_padding_add_SSLv23(buf, num, from, flen);
116 i = RSA_padding_add_none(buf, num, from, flen);
119 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
125 if (BN_bin2bn(buf, num, f) == NULL)
128 if (BN_ucmp(f, rsa->n) >= 0) {
129 /* usually the padding functions would catch this */
130 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT,
131 RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
135 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
136 if (!BN_MONT_CTX_set_locked
137 (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx))
140 if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
145 * put in leading 0 bytes if the number is less than the length of the
148 j = BN_num_bytes(ret);
149 i = BN_bn2bin(ret, &(to[num - j]));
150 for (k = 0; k < (num - i); k++)
158 OPENSSL_clear_free(buf, num);
162 static BN_BLINDING *rsa_get_blinding(RSA *rsa, int *local, BN_CTX *ctx)
166 CRYPTO_THREAD_write_lock(rsa->lock);
168 if (rsa->blinding == NULL) {
169 rsa->blinding = RSA_setup_blinding(rsa, ctx);
176 if (BN_BLINDING_is_current_thread(ret)) {
177 /* rsa->blinding is ours! */
181 /* resort to rsa->mt_blinding instead */
184 * instructs rsa_blinding_convert(), rsa_blinding_invert() that the
185 * BN_BLINDING is shared, meaning that accesses require locks, and
186 * that the blinding factor must be stored outside the BN_BLINDING
190 if (rsa->mt_blinding == NULL) {
191 rsa->mt_blinding = RSA_setup_blinding(rsa, ctx);
193 ret = rsa->mt_blinding;
197 CRYPTO_THREAD_unlock(rsa->lock);
201 static int rsa_blinding_convert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind,
204 if (unblind == NULL) {
206 * Local blinding: store the unblinding factor in BN_BLINDING.
208 return BN_BLINDING_convert_ex(f, NULL, b, ctx);
211 * Shared blinding: store the unblinding factor outside BN_BLINDING.
216 ret = BN_BLINDING_convert_ex(f, unblind, b, ctx);
217 BN_BLINDING_unlock(b);
223 static int rsa_blinding_invert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind,
227 * For local blinding, unblind is set to NULL, and BN_BLINDING_invert_ex
228 * will use the unblinding factor stored in BN_BLINDING. If BN_BLINDING
229 * is shared between threads, unblind must be non-null:
230 * BN_BLINDING_invert_ex will then use the local unblinding factor, and
231 * will only read the modulus from BN_BLINDING. In both cases it's safe
232 * to access the blinding without a lock.
234 return BN_BLINDING_invert_ex(f, unblind, b, ctx);
238 static int rsa_ossl_private_encrypt(int flen, const unsigned char *from,
239 unsigned char *to, RSA *rsa, int padding)
241 BIGNUM *f, *ret, *res;
242 int i, j, k, num = 0, r = -1;
243 unsigned char *buf = NULL;
245 int local_blinding = 0;
247 * Used only if the blinding structure is shared. A non-NULL unblind
248 * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
249 * the unblinding factor outside the blinding structure.
251 BIGNUM *unblind = NULL;
252 BN_BLINDING *blinding = NULL;
254 if ((ctx = BN_CTX_new()) == NULL)
258 ret = BN_CTX_get(ctx);
259 num = BN_num_bytes(rsa->n);
260 buf = OPENSSL_malloc(num);
261 if (ret == NULL || buf == NULL) {
262 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
267 case RSA_PKCS1_PADDING:
268 i = RSA_padding_add_PKCS1_type_1(buf, num, from, flen);
270 case RSA_X931_PADDING:
271 i = RSA_padding_add_X931(buf, num, from, flen);
274 i = RSA_padding_add_none(buf, num, from, flen);
276 case RSA_SSLV23_PADDING:
278 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, 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 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT,
290 RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
294 if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
295 blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
296 if (blinding == NULL) {
297 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, ERR_R_INTERNAL_ERROR);
302 if (blinding != NULL) {
303 if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {
304 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
307 if (!rsa_blinding_convert(blinding, f, unblind, ctx))
311 if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
312 (rsa->version == RSA_ASN1_VERSION_MULTI) ||
315 (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
316 if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
319 BIGNUM *d = BN_new();
321 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
324 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
326 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
327 if (!BN_MONT_CTX_set_locked
328 (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx)) {
333 if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
334 rsa->_method_mod_n)) {
338 /* We MUST free d before any further use of rsa->d */
343 if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
346 if (padding == RSA_X931_PADDING) {
347 BN_sub(f, rsa->n, ret);
348 if (BN_cmp(ret, f) > 0)
357 * put in leading 0 bytes if the number is less than the length of the
360 j = BN_num_bytes(res);
361 i = BN_bn2bin(res, &(to[num - j]));
362 for (k = 0; k < (num - i); k++)
370 OPENSSL_clear_free(buf, num);
374 static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
375 unsigned char *to, RSA *rsa, int padding)
378 int j, num = 0, r = -1;
380 unsigned char *buf = NULL;
382 int local_blinding = 0;
384 * Used only if the blinding structure is shared. A non-NULL unblind
385 * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
386 * the unblinding factor outside the blinding structure.
388 BIGNUM *unblind = NULL;
389 BN_BLINDING *blinding = NULL;
391 if ((ctx = BN_CTX_new()) == NULL)
395 ret = BN_CTX_get(ctx);
396 num = BN_num_bytes(rsa->n);
397 buf = OPENSSL_malloc(num);
398 if (ret == NULL || buf == NULL) {
399 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE);
404 * This check was for equality but PGP does evil things and chops off the
408 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT,
409 RSA_R_DATA_GREATER_THAN_MOD_LEN);
413 /* make data into a big number */
414 if (BN_bin2bn(from, (int)flen, f) == NULL)
417 if (BN_ucmp(f, rsa->n) >= 0) {
418 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT,
419 RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
423 if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
424 blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
425 if (blinding == NULL) {
426 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_INTERNAL_ERROR);
431 if (blinding != NULL) {
432 if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {
433 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE);
436 if (!rsa_blinding_convert(blinding, f, unblind, ctx))
441 if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
442 (rsa->version == RSA_ASN1_VERSION_MULTI) ||
445 (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
446 if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
449 BIGNUM *d = BN_new();
451 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE);
454 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
456 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
457 if (!BN_MONT_CTX_set_locked
458 (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx)) {
462 if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
463 rsa->_method_mod_n)) {
467 /* We MUST free d before any further use of rsa->d */
472 if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
476 j = BN_bn2bin(ret, p); /* j is only used with no-padding mode */
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);
485 case RSA_SSLV23_PADDING:
486 r = RSA_padding_check_SSLv23(to, num, buf, j, num);
489 r = RSA_padding_check_none(to, num, buf, j, num);
492 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
496 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, RSA_R_PADDING_CHECK_FAILED);
502 OPENSSL_clear_free(buf, num);
506 /* signature verification */
507 static int rsa_ossl_public_decrypt(int flen, const unsigned char *from,
508 unsigned char *to, RSA *rsa, int padding)
511 int i, num = 0, r = -1;
513 unsigned char *buf = NULL;
516 if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
517 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_MODULUS_TOO_LARGE);
521 if (BN_ucmp(rsa->n, rsa->e) <= 0) {
522 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
526 /* for large moduli, enforce exponent limit */
527 if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
528 if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
529 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
534 if ((ctx = BN_CTX_new()) == NULL)
538 ret = BN_CTX_get(ctx);
539 num = BN_num_bytes(rsa->n);
540 buf = OPENSSL_malloc(num);
541 if (ret == NULL || buf == NULL) {
542 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, ERR_R_MALLOC_FAILURE);
547 * This check was for equality but PGP does evil things and chops off the
551 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_DATA_GREATER_THAN_MOD_LEN);
555 if (BN_bin2bn(from, flen, f) == NULL)
558 if (BN_ucmp(f, rsa->n) >= 0) {
559 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT,
560 RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
564 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
565 if (!BN_MONT_CTX_set_locked
566 (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx))
569 if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
573 if ((padding == RSA_X931_PADDING) && ((bn_get_words(ret)[0] & 0xf) != 12))
574 if (!BN_sub(ret, rsa->n, ret))
578 i = BN_bn2bin(ret, p);
581 case RSA_PKCS1_PADDING:
582 r = RSA_padding_check_PKCS1_type_1(to, num, buf, i, num);
584 case RSA_X931_PADDING:
585 r = RSA_padding_check_X931(to, num, buf, i, num);
588 r = RSA_padding_check_none(to, num, buf, i, num);
591 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
595 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_PADDING_CHECK_FAILED);
601 OPENSSL_clear_free(buf, num);
605 static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
607 BIGNUM *r1, *m1, *vrfy, *r2, *m[RSA_MAX_PRIME_NUM];
608 int ret = 0, i, ex_primes = 0;
609 RSA_PRIME_INFO *pinfo;
613 r1 = BN_CTX_get(ctx);
614 r2 = BN_CTX_get(ctx);
615 m1 = BN_CTX_get(ctx);
616 vrfy = BN_CTX_get(ctx);
620 if (rsa->version == RSA_ASN1_VERSION_MULTI
621 && (ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) <= 0)
625 BIGNUM *p = BN_new(), *q = BN_new();
628 * Make sure BN_mod_inverse in Montgomery initialization uses the
629 * BN_FLG_CONSTTIME flag
631 if (p == NULL || q == NULL) {
636 BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
637 BN_with_flags(q, rsa->q, BN_FLG_CONSTTIME);
639 if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {
640 if (!BN_MONT_CTX_set_locked
641 (&rsa->_method_mod_p, rsa->lock, p, ctx)
642 || !BN_MONT_CTX_set_locked(&rsa->_method_mod_q,
643 rsa->lock, q, ctx)) {
649 /* cache BN_MONT_CTX for other primes */
650 BIGNUM *r = BN_new();
658 for (i = 0; i < ex_primes; i++) {
659 pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
660 BN_with_flags(r, pinfo->r, BN_FLG_CONSTTIME);
661 if (!BN_MONT_CTX_set_locked(&pinfo->m, rsa->lock, r, ctx)) {
672 * We MUST free p and q before any further use of rsa->p and rsa->q
678 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
679 if (!BN_MONT_CTX_set_locked
680 (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx))
683 /* compute I mod q */
685 BIGNUM *c = BN_new();
688 BN_with_flags(c, I, BN_FLG_CONSTTIME);
690 if (!BN_mod(r1, c, rsa->q, ctx)) {
696 BIGNUM *dmq1 = BN_new();
701 BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
703 /* compute r1^dmq1 mod q */
704 if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx,
705 rsa->_method_mod_q)) {
710 /* We MUST free dmq1 before any further use of rsa->dmq1 */
714 /* compute I mod p */
715 if (!BN_mod(r1, c, rsa->p, ctx)) {
719 /* We MUST free c before any further use of I */
724 BIGNUM *dmp1 = BN_new();
727 BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
729 /* compute r1^dmp1 mod p */
730 if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx,
731 rsa->_method_mod_p)) {
735 /* We MUST free dmp1 before any further use of rsa->dmp1 */
740 * calculate m_i in multi-prime case
743 * 1. squash the following two loops and calculate |m_i| there.
744 * 2. remove cc and reuse |c|.
745 * 3. remove |dmq1| and |dmp1| in previous block and use |di|.
747 * If these things are done, the code will be more readable.
750 BIGNUM *di = BN_new(), *cc = BN_new();
752 if (cc == NULL || di == NULL) {
758 for (i = 0; i < ex_primes; i++) {
760 if ((m[i] = BN_CTX_get(ctx)) == NULL) {
766 pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
768 /* prepare c and d_i */
769 BN_with_flags(cc, I, BN_FLG_CONSTTIME);
770 BN_with_flags(di, pinfo->d, BN_FLG_CONSTTIME);
772 if (!BN_mod(r1, cc, pinfo->r, ctx)) {
777 /* compute r1 ^ d_i mod r_i */
778 if (!rsa->meth->bn_mod_exp(m[i], r1, di, pinfo->r, ctx, pinfo->m)) {
789 if (!BN_sub(r0, r0, m1))
792 * This will help stop the size of r0 increasing, which does affect the
793 * multiply if it optimised for a power of 2 size
795 if (BN_is_negative(r0))
796 if (!BN_add(r0, r0, rsa->p))
799 if (!BN_mul(r1, r0, rsa->iqmp, ctx))
803 BIGNUM *pr1 = BN_new();
806 BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
808 if (!BN_mod(r0, pr1, rsa->p, ctx)) {
812 /* We MUST free pr1 before any further use of r1 */
817 * If p < q it is occasionally possible for the correction of adding 'p'
818 * if r0 is negative above to leave the result still negative. This can
819 * break the private key operations: the following second correction
820 * should *always* correct this rare occurrence. This will *never* happen
821 * with OpenSSL generated keys because they ensure p > q [steve]
823 if (BN_is_negative(r0))
824 if (!BN_add(r0, r0, rsa->p))
826 if (!BN_mul(r1, r0, rsa->q, ctx))
828 if (!BN_add(r0, r1, m1))
831 /* add m_i to m in multi-prime case */
833 BIGNUM *pr2 = BN_new();
838 for (i = 0; i < ex_primes; i++) {
839 pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
840 if (!BN_sub(r1, m[i], r0)) {
845 if (!BN_mul(r2, r1, pinfo->t, ctx)) {
850 BN_with_flags(pr2, r2, BN_FLG_CONSTTIME);
852 if (!BN_mod(r1, pr2, pinfo->r, ctx)) {
857 if (BN_is_negative(r1))
858 if (!BN_add(r1, r1, pinfo->r)) {
862 if (!BN_mul(r1, r1, pinfo->pp, ctx)) {
866 if (!BN_add(r0, r0, r1)) {
874 if (rsa->e && rsa->n) {
875 if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,
879 * If 'I' was greater than (or equal to) rsa->n, the operation will
880 * be equivalent to using 'I mod n'. However, the result of the
881 * verify will *always* be less than 'n' so we don't check for
882 * absolute equality, just congruency.
884 if (!BN_sub(vrfy, vrfy, I))
886 if (!BN_mod(vrfy, vrfy, rsa->n, ctx))
888 if (BN_is_negative(vrfy))
889 if (!BN_add(vrfy, vrfy, rsa->n))
891 if (!BN_is_zero(vrfy)) {
893 * 'I' and 'vrfy' aren't congruent mod n. Don't leak
894 * miscalculated CRT output, just do a raw (slower) mod_exp and
895 * return that instead.
898 BIGNUM *d = BN_new();
901 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
903 if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx,
904 rsa->_method_mod_n)) {
908 /* We MUST free d before any further use of rsa->d */
918 static int rsa_ossl_init(RSA *rsa)
920 rsa->flags |= RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE;
924 static int rsa_ossl_finish(RSA *rsa)
927 RSA_PRIME_INFO *pinfo;
929 BN_MONT_CTX_free(rsa->_method_mod_n);
930 BN_MONT_CTX_free(rsa->_method_mod_p);
931 BN_MONT_CTX_free(rsa->_method_mod_q);
932 for (i = 0; i < sk_RSA_PRIME_INFO_num(rsa->prime_infos); i++) {
933 pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
934 BN_MONT_CTX_free(pinfo->m);