2 * Copyright 1995-2020 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 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_MODULUS_TOO_LARGE);
87 if (BN_ucmp(rsa->n, rsa->e) <= 0) {
88 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, 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 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, 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 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, 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 case RSA_SSLV23_PADDING:
124 i = ossl_rsa_padding_add_SSLv23_ex(rsa->libctx, buf, num, from, flen);
128 i = RSA_padding_add_none(buf, num, from, flen);
131 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
137 if (BN_bin2bn(buf, num, f) == NULL)
140 if (BN_ucmp(f, rsa->n) >= 0) {
141 /* usually the padding functions would catch this */
142 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT,
143 RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
147 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
148 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
152 if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
157 * BN_bn2binpad puts in leading 0 bytes if the number is less than
158 * the length of the modulus.
160 r = BN_bn2binpad(ret, to, num);
164 OPENSSL_clear_free(buf, num);
168 static BN_BLINDING *rsa_get_blinding(RSA *rsa, int *local, BN_CTX *ctx)
172 CRYPTO_THREAD_write_lock(rsa->lock);
174 if (rsa->blinding == NULL) {
175 rsa->blinding = RSA_setup_blinding(rsa, ctx);
182 if (BN_BLINDING_is_current_thread(ret)) {
183 /* rsa->blinding is ours! */
187 /* resort to rsa->mt_blinding instead */
190 * instructs rsa_blinding_convert(), rsa_blinding_invert() that the
191 * BN_BLINDING is shared, meaning that accesses require locks, and
192 * that the blinding factor must be stored outside the BN_BLINDING
196 if (rsa->mt_blinding == NULL) {
197 rsa->mt_blinding = RSA_setup_blinding(rsa, ctx);
199 ret = rsa->mt_blinding;
203 CRYPTO_THREAD_unlock(rsa->lock);
207 static int rsa_blinding_convert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind,
210 if (unblind == NULL) {
212 * Local blinding: store the unblinding factor in BN_BLINDING.
214 return BN_BLINDING_convert_ex(f, NULL, b, ctx);
217 * Shared blinding: store the unblinding factor outside BN_BLINDING.
222 ret = BN_BLINDING_convert_ex(f, unblind, b, ctx);
223 BN_BLINDING_unlock(b);
229 static int rsa_blinding_invert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind,
233 * For local blinding, unblind is set to NULL, and BN_BLINDING_invert_ex
234 * will use the unblinding factor stored in BN_BLINDING. If BN_BLINDING
235 * is shared between threads, unblind must be non-null:
236 * BN_BLINDING_invert_ex will then use the local unblinding factor, and
237 * will only read the modulus from BN_BLINDING. In both cases it's safe
238 * to access the blinding without a lock.
240 return BN_BLINDING_invert_ex(f, unblind, b, ctx);
244 static int rsa_ossl_private_encrypt(int flen, const unsigned char *from,
245 unsigned char *to, RSA *rsa, int padding)
247 BIGNUM *f, *ret, *res;
248 int i, num = 0, r = -1;
249 unsigned char *buf = NULL;
251 int local_blinding = 0;
253 * Used only if the blinding structure is shared. A non-NULL unblind
254 * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
255 * the unblinding factor outside the blinding structure.
257 BIGNUM *unblind = NULL;
258 BN_BLINDING *blinding = NULL;
260 if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
264 ret = BN_CTX_get(ctx);
265 num = BN_num_bytes(rsa->n);
266 buf = OPENSSL_malloc(num);
267 if (ret == NULL || buf == NULL) {
268 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
273 case RSA_PKCS1_PADDING:
274 i = RSA_padding_add_PKCS1_type_1(buf, num, from, flen);
276 case RSA_X931_PADDING:
277 i = RSA_padding_add_X931(buf, num, from, flen);
280 i = RSA_padding_add_none(buf, num, from, flen);
282 case RSA_SSLV23_PADDING:
284 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
290 if (BN_bin2bn(buf, num, f) == NULL)
293 if (BN_ucmp(f, rsa->n) >= 0) {
294 /* usually the padding functions would catch this */
295 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT,
296 RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
300 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
301 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
305 if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
306 blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
307 if (blinding == NULL) {
308 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, ERR_R_INTERNAL_ERROR);
313 if (blinding != NULL) {
314 if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {
315 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
318 if (!rsa_blinding_convert(blinding, f, unblind, ctx))
322 if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
323 (rsa->version == RSA_ASN1_VERSION_MULTI) ||
326 (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
327 if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
330 BIGNUM *d = BN_new();
332 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
335 if (rsa->d == NULL) {
336 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, RSA_R_MISSING_PRIVATE_KEY);
340 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
342 if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
343 rsa->_method_mod_n)) {
347 /* We MUST free d before any further use of rsa->d */
352 if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
355 if (padding == RSA_X931_PADDING) {
356 if (!BN_sub(f, rsa->n, ret))
358 if (BN_cmp(ret, f) > 0)
367 * BN_bn2binpad puts in leading 0 bytes if the number is less than
368 * the length of the modulus.
370 r = BN_bn2binpad(res, to, num);
374 OPENSSL_clear_free(buf, num);
378 static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
379 unsigned char *to, RSA *rsa, int padding)
382 int j, num = 0, r = -1;
383 unsigned char *buf = NULL;
385 int local_blinding = 0;
387 * Used only if the blinding structure is shared. A non-NULL unblind
388 * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
389 * the unblinding factor outside the blinding structure.
391 BIGNUM *unblind = NULL;
392 BN_BLINDING *blinding = NULL;
394 if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
398 ret = BN_CTX_get(ctx);
399 num = BN_num_bytes(rsa->n);
400 buf = OPENSSL_malloc(num);
401 if (ret == NULL || buf == NULL) {
402 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE);
407 * This check was for equality but PGP does evil things and chops off the
411 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT,
412 RSA_R_DATA_GREATER_THAN_MOD_LEN);
416 /* make data into a big number */
417 if (BN_bin2bn(from, (int)flen, f) == NULL)
420 if (BN_ucmp(f, rsa->n) >= 0) {
421 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT,
422 RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
426 if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
427 blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
428 if (blinding == NULL) {
429 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_INTERNAL_ERROR);
434 if (blinding != NULL) {
435 if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {
436 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE);
439 if (!rsa_blinding_convert(blinding, f, unblind, ctx))
444 if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
445 (rsa->version == RSA_ASN1_VERSION_MULTI) ||
448 (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
449 if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
452 BIGNUM *d = BN_new();
454 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE);
457 if (rsa->d == NULL) {
458 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, RSA_R_MISSING_PRIVATE_KEY);
462 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
464 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
465 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
470 if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
471 rsa->_method_mod_n)) {
475 /* We MUST free d before any further use of rsa->d */
480 if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
483 j = BN_bn2binpad(ret, buf, num);
488 case RSA_PKCS1_PADDING:
489 r = RSA_padding_check_PKCS1_type_2(to, num, buf, j, num);
491 case RSA_PKCS1_OAEP_PADDING:
492 r = RSA_padding_check_PKCS1_OAEP(to, num, buf, j, num, NULL, 0);
495 case RSA_SSLV23_PADDING:
496 r = RSA_padding_check_SSLv23(to, num, buf, j, num);
500 memcpy(to, buf, (r = j));
503 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
508 * This trick doesn't work in the FIPS provider because libcrypto manages
509 * the error stack. Instead we opt not to put an error on the stack at all
510 * in case of padding failure in the FIPS provider.
512 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, RSA_R_PADDING_CHECK_FAILED);
513 err_clear_last_constant_time(1 & ~constant_time_msb(r));
519 OPENSSL_clear_free(buf, num);
523 /* signature verification */
524 static int rsa_ossl_public_decrypt(int flen, const unsigned char *from,
525 unsigned char *to, RSA *rsa, int padding)
528 int i, num = 0, r = -1;
529 unsigned char *buf = NULL;
532 if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
533 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_MODULUS_TOO_LARGE);
537 if (BN_ucmp(rsa->n, rsa->e) <= 0) {
538 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
542 /* for large moduli, enforce exponent limit */
543 if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
544 if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
545 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
550 if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
554 ret = BN_CTX_get(ctx);
555 num = BN_num_bytes(rsa->n);
556 buf = OPENSSL_malloc(num);
557 if (ret == NULL || buf == NULL) {
558 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, ERR_R_MALLOC_FAILURE);
563 * This check was for equality but PGP does evil things and chops off the
567 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_DATA_GREATER_THAN_MOD_LEN);
571 if (BN_bin2bn(from, flen, f) == NULL)
574 if (BN_ucmp(f, rsa->n) >= 0) {
575 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT,
576 RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
580 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
581 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
585 if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
589 if ((padding == RSA_X931_PADDING) && ((bn_get_words(ret)[0] & 0xf) != 12))
590 if (!BN_sub(ret, rsa->n, ret))
593 i = BN_bn2binpad(ret, buf, num);
598 case RSA_PKCS1_PADDING:
599 r = RSA_padding_check_PKCS1_type_1(to, num, buf, i, num);
601 case RSA_X931_PADDING:
602 r = RSA_padding_check_X931(to, num, buf, i, num);
605 memcpy(to, buf, (r = i));
608 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
612 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_PADDING_CHECK_FAILED);
617 OPENSSL_clear_free(buf, num);
621 static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
623 BIGNUM *r1, *m1, *vrfy;
624 int ret = 0, smooth = 0;
626 BIGNUM *r2, *m[RSA_MAX_PRIME_NUM - 2];
627 int i, ex_primes = 0;
628 RSA_PRIME_INFO *pinfo;
633 r1 = BN_CTX_get(ctx);
635 r2 = BN_CTX_get(ctx);
637 m1 = BN_CTX_get(ctx);
638 vrfy = BN_CTX_get(ctx);
643 if (rsa->version == RSA_ASN1_VERSION_MULTI
644 && ((ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) <= 0
645 || ex_primes > RSA_MAX_PRIME_NUM - 2))
649 if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {
650 BIGNUM *factor = BN_new();
656 * Make sure BN_mod_inverse in Montgomery initialization uses the
657 * BN_FLG_CONSTTIME flag
659 if (!(BN_with_flags(factor, rsa->p, BN_FLG_CONSTTIME),
660 BN_MONT_CTX_set_locked(&rsa->_method_mod_p, rsa->lock,
662 || !(BN_with_flags(factor, rsa->q, BN_FLG_CONSTTIME),
663 BN_MONT_CTX_set_locked(&rsa->_method_mod_q, rsa->lock,
669 for (i = 0; i < ex_primes; i++) {
670 pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
671 BN_with_flags(factor, pinfo->r, BN_FLG_CONSTTIME);
672 if (!BN_MONT_CTX_set_locked(&pinfo->m, rsa->lock, factor, ctx)) {
679 * We MUST free |factor| before any further use of the prime factors
683 smooth = (rsa->meth->bn_mod_exp == BN_mod_exp_mont)
687 && (BN_num_bits(rsa->q) == BN_num_bits(rsa->p));
690 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
691 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
697 * Conversion from Montgomery domain, a.k.a. Montgomery reduction,
698 * accepts values in [0-m*2^w) range. w is m's bit width rounded up
699 * to limb width. So that at the very least if |I| is fully reduced,
700 * i.e. less than p*q, we can count on from-to round to perform
701 * below modulo operations on |I|. Unlike BN_mod it's constant time.
703 if (/* m1 = I moq q */
704 !bn_from_mont_fixed_top(m1, I, rsa->_method_mod_q, ctx)
705 || !bn_to_mont_fixed_top(m1, m1, rsa->_method_mod_q, ctx)
706 /* m1 = m1^dmq1 mod q */
707 || !BN_mod_exp_mont_consttime(m1, m1, rsa->dmq1, rsa->q, ctx,
710 || !bn_from_mont_fixed_top(r1, I, rsa->_method_mod_p, ctx)
711 || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)
712 /* r1 = r1^dmp1 mod p */
713 || !BN_mod_exp_mont_consttime(r1, r1, rsa->dmp1, rsa->p, ctx,
715 /* r1 = (r1 - m1) mod p */
717 * bn_mod_sub_fixed_top is not regular modular subtraction,
718 * it can tolerate subtrahend to be larger than modulus, but
719 * not bit-wise wider. This makes up for uncommon q>p case,
720 * when |m1| can be larger than |rsa->p|.
722 || !bn_mod_sub_fixed_top(r1, r1, m1, rsa->p)
724 /* r1 = r1 * iqmp mod p */
725 || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)
726 || !bn_mul_mont_fixed_top(r1, r1, rsa->iqmp, rsa->_method_mod_p,
728 /* r0 = r1 * q + m1 */
729 || !bn_mul_fixed_top(r0, r1, rsa->q, ctx)
730 || !bn_mod_add_fixed_top(r0, r0, m1, rsa->n))
736 /* compute I mod q */
738 BIGNUM *c = BN_new();
741 BN_with_flags(c, I, BN_FLG_CONSTTIME);
743 if (!BN_mod(r1, c, rsa->q, ctx)) {
749 BIGNUM *dmq1 = BN_new();
754 BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
756 /* compute r1^dmq1 mod q */
757 if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx,
758 rsa->_method_mod_q)) {
763 /* We MUST free dmq1 before any further use of rsa->dmq1 */
767 /* compute I mod p */
768 if (!BN_mod(r1, c, rsa->p, ctx)) {
772 /* We MUST free c before any further use of I */
777 BIGNUM *dmp1 = BN_new();
780 BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
782 /* compute r1^dmp1 mod p */
783 if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx,
784 rsa->_method_mod_p)) {
788 /* We MUST free dmp1 before any further use of rsa->dmp1 */
794 * calculate m_i in multi-prime case
797 * 1. squash the following two loops and calculate |m_i| there.
798 * 2. remove cc and reuse |c|.
799 * 3. remove |dmq1| and |dmp1| in previous block and use |di|.
801 * If these things are done, the code will be more readable.
804 BIGNUM *di = BN_new(), *cc = BN_new();
806 if (cc == NULL || di == NULL) {
812 for (i = 0; i < ex_primes; i++) {
814 if ((m[i] = BN_CTX_get(ctx)) == NULL) {
820 pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
822 /* prepare c and d_i */
823 BN_with_flags(cc, I, BN_FLG_CONSTTIME);
824 BN_with_flags(di, pinfo->d, BN_FLG_CONSTTIME);
826 if (!BN_mod(r1, cc, pinfo->r, ctx)) {
831 /* compute r1 ^ d_i mod r_i */
832 if (!rsa->meth->bn_mod_exp(m[i], r1, di, pinfo->r, ctx, pinfo->m)) {
844 if (!BN_sub(r0, r0, m1))
847 * This will help stop the size of r0 increasing, which does affect the
848 * multiply if it optimised for a power of 2 size
850 if (BN_is_negative(r0))
851 if (!BN_add(r0, r0, rsa->p))
854 if (!BN_mul(r1, r0, rsa->iqmp, ctx))
858 BIGNUM *pr1 = BN_new();
861 BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
863 if (!BN_mod(r0, pr1, rsa->p, ctx)) {
867 /* We MUST free pr1 before any further use of r1 */
872 * If p < q it is occasionally possible for the correction of adding 'p'
873 * if r0 is negative above to leave the result still negative. This can
874 * break the private key operations: the following second correction
875 * should *always* correct this rare occurrence. This will *never* happen
876 * with OpenSSL generated keys because they ensure p > q [steve]
878 if (BN_is_negative(r0))
879 if (!BN_add(r0, r0, rsa->p))
881 if (!BN_mul(r1, r0, rsa->q, ctx))
883 if (!BN_add(r0, r1, m1))
887 /* add m_i to m in multi-prime case */
889 BIGNUM *pr2 = BN_new();
894 for (i = 0; i < ex_primes; i++) {
895 pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
896 if (!BN_sub(r1, m[i], r0)) {
901 if (!BN_mul(r2, r1, pinfo->t, ctx)) {
906 BN_with_flags(pr2, r2, BN_FLG_CONSTTIME);
908 if (!BN_mod(r1, pr2, pinfo->r, ctx)) {
913 if (BN_is_negative(r1))
914 if (!BN_add(r1, r1, pinfo->r)) {
918 if (!BN_mul(r1, r1, pinfo->pp, ctx)) {
922 if (!BN_add(r0, r0, r1)) {
932 if (rsa->e && rsa->n) {
933 if (rsa->meth->bn_mod_exp == BN_mod_exp_mont) {
934 if (!BN_mod_exp_mont(vrfy, r0, rsa->e, rsa->n, ctx,
939 if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,
944 * If 'I' was greater than (or equal to) rsa->n, the operation will
945 * be equivalent to using 'I mod n'. However, the result of the
946 * verify will *always* be less than 'n' so we don't check for
947 * absolute equality, just congruency.
949 if (!BN_sub(vrfy, vrfy, I))
951 if (BN_is_zero(vrfy)) {
954 goto err; /* not actually error */
956 if (!BN_mod(vrfy, vrfy, rsa->n, ctx))
958 if (BN_is_negative(vrfy))
959 if (!BN_add(vrfy, vrfy, rsa->n))
961 if (!BN_is_zero(vrfy)) {
963 * 'I' and 'vrfy' aren't congruent mod n. Don't leak
964 * miscalculated CRT output, just do a raw (slower) mod_exp and
965 * return that instead.
968 BIGNUM *d = BN_new();
971 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
973 if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx,
974 rsa->_method_mod_n)) {
978 /* We MUST free d before any further use of rsa->d */
983 * It's unfortunate that we have to bn_correct_top(r0). What hopefully
984 * saves the day is that correction is highly unlike, and private key
985 * operations are customarily performed on blinded message. Which means
986 * that attacker won't observe correlation with chosen plaintext.
987 * Secondly, remaining code would still handle it in same computational
988 * time and even conceal memory access pattern around corrected top.
997 static int rsa_ossl_init(RSA *rsa)
999 rsa->flags |= RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE;
1003 static int rsa_ossl_finish(RSA *rsa)
1007 RSA_PRIME_INFO *pinfo;
1009 for (i = 0; i < sk_RSA_PRIME_INFO_num(rsa->prime_infos); i++) {
1010 pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
1011 BN_MONT_CTX_free(pinfo->m);
1015 BN_MONT_CTX_free(rsa->_method_mod_n);
1016 BN_MONT_CTX_free(rsa->_method_mod_p);
1017 BN_MONT_CTX_free(rsa->_method_mod_q);