2 * Copyright 1995-2018 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 = RSA_padding_add_PKCS1_type_2(buf, num, from, flen);
116 case RSA_PKCS1_OAEP_PADDING:
117 i = RSA_padding_add_PKCS1_OAEP(buf, num, from, flen, NULL, 0);
120 case RSA_SSLV23_PADDING:
121 i = RSA_padding_add_SSLv23(buf, num, from, flen);
125 i = RSA_padding_add_none(buf, num, from, flen);
128 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
134 if (BN_bin2bn(buf, num, f) == NULL)
137 if (BN_ucmp(f, rsa->n) >= 0) {
138 /* usually the padding functions would catch this */
139 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT,
140 RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
144 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
145 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
149 if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
154 * BN_bn2binpad puts in leading 0 bytes if the number is less than
155 * the length of the modulus.
157 r = BN_bn2binpad(ret, to, num);
161 OPENSSL_clear_free(buf, num);
165 static BN_BLINDING *rsa_get_blinding(RSA *rsa, int *local, BN_CTX *ctx)
169 CRYPTO_THREAD_write_lock(rsa->lock);
171 if (rsa->blinding == NULL) {
172 rsa->blinding = RSA_setup_blinding(rsa, ctx);
179 if (BN_BLINDING_is_current_thread(ret)) {
180 /* rsa->blinding is ours! */
184 /* resort to rsa->mt_blinding instead */
187 * instructs rsa_blinding_convert(), rsa_blinding_invert() that the
188 * BN_BLINDING is shared, meaning that accesses require locks, and
189 * that the blinding factor must be stored outside the BN_BLINDING
193 if (rsa->mt_blinding == NULL) {
194 rsa->mt_blinding = RSA_setup_blinding(rsa, ctx);
196 ret = rsa->mt_blinding;
200 CRYPTO_THREAD_unlock(rsa->lock);
204 static int rsa_blinding_convert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind,
207 if (unblind == NULL) {
209 * Local blinding: store the unblinding factor in BN_BLINDING.
211 return BN_BLINDING_convert_ex(f, NULL, b, ctx);
214 * Shared blinding: store the unblinding factor outside BN_BLINDING.
219 ret = BN_BLINDING_convert_ex(f, unblind, b, ctx);
220 BN_BLINDING_unlock(b);
226 static int rsa_blinding_invert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind,
230 * For local blinding, unblind is set to NULL, and BN_BLINDING_invert_ex
231 * will use the unblinding factor stored in BN_BLINDING. If BN_BLINDING
232 * is shared between threads, unblind must be non-null:
233 * BN_BLINDING_invert_ex will then use the local unblinding factor, and
234 * will only read the modulus from BN_BLINDING. In both cases it's safe
235 * to access the blinding without a lock.
237 return BN_BLINDING_invert_ex(f, unblind, b, ctx);
241 static int rsa_ossl_private_encrypt(int flen, const unsigned char *from,
242 unsigned char *to, RSA *rsa, int padding)
244 BIGNUM *f, *ret, *res;
245 int i, num = 0, r = -1;
246 unsigned char *buf = NULL;
248 int local_blinding = 0;
250 * Used only if the blinding structure is shared. A non-NULL unblind
251 * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
252 * the unblinding factor outside the blinding structure.
254 BIGNUM *unblind = NULL;
255 BN_BLINDING *blinding = NULL;
257 if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
261 ret = BN_CTX_get(ctx);
262 num = BN_num_bytes(rsa->n);
263 buf = OPENSSL_malloc(num);
264 if (ret == NULL || buf == NULL) {
265 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
270 case RSA_PKCS1_PADDING:
271 i = RSA_padding_add_PKCS1_type_1(buf, num, from, flen);
273 case RSA_X931_PADDING:
274 i = RSA_padding_add_X931(buf, num, from, flen);
277 i = RSA_padding_add_none(buf, num, from, flen);
279 case RSA_SSLV23_PADDING:
281 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
287 if (BN_bin2bn(buf, num, f) == NULL)
290 if (BN_ucmp(f, rsa->n) >= 0) {
291 /* usually the padding functions would catch this */
292 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT,
293 RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
297 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
298 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
302 if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
303 blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
304 if (blinding == NULL) {
305 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, ERR_R_INTERNAL_ERROR);
310 if (blinding != NULL) {
311 if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {
312 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
315 if (!rsa_blinding_convert(blinding, f, unblind, ctx))
319 if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
320 (rsa->version == RSA_ASN1_VERSION_MULTI) ||
323 (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
324 if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
327 BIGNUM *d = BN_new();
329 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
332 if (rsa->d == NULL) {
333 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, RSA_R_MISSING_PRIVATE_KEY);
337 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
339 if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
340 rsa->_method_mod_n)) {
344 /* We MUST free d before any further use of rsa->d */
349 if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
352 if (padding == RSA_X931_PADDING) {
353 if (!BN_sub(f, rsa->n, ret))
355 if (BN_cmp(ret, f) > 0)
364 * BN_bn2binpad puts in leading 0 bytes if the number is less than
365 * the length of the modulus.
367 r = BN_bn2binpad(res, to, num);
371 OPENSSL_clear_free(buf, num);
375 static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
376 unsigned char *to, RSA *rsa, int padding)
379 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_ex(rsa->libctx)) == 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 if (rsa->d == NULL) {
455 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, RSA_R_MISSING_PRIVATE_KEY);
459 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
461 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
462 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
467 if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
468 rsa->_method_mod_n)) {
472 /* We MUST free d before any further use of rsa->d */
477 if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
480 j = BN_bn2binpad(ret, buf, num);
485 case RSA_PKCS1_PADDING:
486 r = RSA_padding_check_PKCS1_type_2(to, num, buf, j, num);
488 case RSA_PKCS1_OAEP_PADDING:
489 r = RSA_padding_check_PKCS1_OAEP(to, num, buf, j, num, NULL, 0);
492 case RSA_SSLV23_PADDING:
493 r = RSA_padding_check_SSLv23(to, num, buf, j, num);
497 memcpy(to, buf, (r = j));
500 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
505 * This trick doesn't work in the FIPS provider because libcrypto manages
506 * the error stack. Instead we opt not to put an error on the stack at all
507 * in case of padding failure in the FIPS provider.
509 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, RSA_R_PADDING_CHECK_FAILED);
510 err_clear_last_constant_time(1 & ~constant_time_msb(r));
516 OPENSSL_clear_free(buf, num);
520 /* signature verification */
521 static int rsa_ossl_public_decrypt(int flen, const unsigned char *from,
522 unsigned char *to, RSA *rsa, int padding)
525 int i, num = 0, r = -1;
526 unsigned char *buf = NULL;
529 if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
530 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_MODULUS_TOO_LARGE);
534 if (BN_ucmp(rsa->n, rsa->e) <= 0) {
535 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
539 /* for large moduli, enforce exponent limit */
540 if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
541 if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
542 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
547 if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
551 ret = BN_CTX_get(ctx);
552 num = BN_num_bytes(rsa->n);
553 buf = OPENSSL_malloc(num);
554 if (ret == NULL || buf == NULL) {
555 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, ERR_R_MALLOC_FAILURE);
560 * This check was for equality but PGP does evil things and chops off the
564 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_DATA_GREATER_THAN_MOD_LEN);
568 if (BN_bin2bn(from, flen, f) == NULL)
571 if (BN_ucmp(f, rsa->n) >= 0) {
572 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT,
573 RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
577 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
578 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
582 if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
586 if ((padding == RSA_X931_PADDING) && ((bn_get_words(ret)[0] & 0xf) != 12))
587 if (!BN_sub(ret, rsa->n, ret))
590 i = BN_bn2binpad(ret, buf, num);
595 case RSA_PKCS1_PADDING:
596 r = RSA_padding_check_PKCS1_type_1(to, num, buf, i, num);
598 case RSA_X931_PADDING:
599 r = RSA_padding_check_X931(to, num, buf, i, num);
602 memcpy(to, buf, (r = i));
605 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
609 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_PADDING_CHECK_FAILED);
614 OPENSSL_clear_free(buf, num);
618 static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
620 BIGNUM *r1, *m1, *vrfy;
621 int ret = 0, smooth = 0;
623 BIGNUM *r2, *m[RSA_MAX_PRIME_NUM - 2];
624 int i, ex_primes = 0;
625 RSA_PRIME_INFO *pinfo;
630 r1 = BN_CTX_get(ctx);
632 r2 = BN_CTX_get(ctx);
634 m1 = BN_CTX_get(ctx);
635 vrfy = BN_CTX_get(ctx);
640 if (rsa->version == RSA_ASN1_VERSION_MULTI
641 && ((ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) <= 0
642 || ex_primes > RSA_MAX_PRIME_NUM - 2))
646 if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {
647 BIGNUM *factor = BN_new();
653 * Make sure BN_mod_inverse in Montgomery initialization uses the
654 * BN_FLG_CONSTTIME flag
656 if (!(BN_with_flags(factor, rsa->p, BN_FLG_CONSTTIME),
657 BN_MONT_CTX_set_locked(&rsa->_method_mod_p, rsa->lock,
659 || !(BN_with_flags(factor, rsa->q, BN_FLG_CONSTTIME),
660 BN_MONT_CTX_set_locked(&rsa->_method_mod_q, rsa->lock,
666 for (i = 0; i < ex_primes; i++) {
667 pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
668 BN_with_flags(factor, pinfo->r, BN_FLG_CONSTTIME);
669 if (!BN_MONT_CTX_set_locked(&pinfo->m, rsa->lock, factor, ctx)) {
676 * We MUST free |factor| before any further use of the prime factors
680 smooth = (rsa->meth->bn_mod_exp == BN_mod_exp_mont)
684 && (BN_num_bits(rsa->q) == BN_num_bits(rsa->p));
687 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
688 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
694 * Conversion from Montgomery domain, a.k.a. Montgomery reduction,
695 * accepts values in [0-m*2^w) range. w is m's bit width rounded up
696 * to limb width. So that at the very least if |I| is fully reduced,
697 * i.e. less than p*q, we can count on from-to round to perform
698 * below modulo operations on |I|. Unlike BN_mod it's constant time.
700 if (/* m1 = I moq q */
701 !bn_from_mont_fixed_top(m1, I, rsa->_method_mod_q, ctx)
702 || !bn_to_mont_fixed_top(m1, m1, rsa->_method_mod_q, ctx)
703 /* m1 = m1^dmq1 mod q */
704 || !BN_mod_exp_mont_consttime(m1, m1, rsa->dmq1, rsa->q, ctx,
707 || !bn_from_mont_fixed_top(r1, I, rsa->_method_mod_p, ctx)
708 || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)
709 /* r1 = r1^dmp1 mod p */
710 || !BN_mod_exp_mont_consttime(r1, r1, rsa->dmp1, rsa->p, ctx,
712 /* r1 = (r1 - m1) mod p */
714 * bn_mod_sub_fixed_top is not regular modular subtraction,
715 * it can tolerate subtrahend to be larger than modulus, but
716 * not bit-wise wider. This makes up for uncommon q>p case,
717 * when |m1| can be larger than |rsa->p|.
719 || !bn_mod_sub_fixed_top(r1, r1, m1, rsa->p)
721 /* r1 = r1 * iqmp mod p */
722 || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)
723 || !bn_mul_mont_fixed_top(r1, r1, rsa->iqmp, rsa->_method_mod_p,
725 /* r0 = r1 * q + m1 */
726 || !bn_mul_fixed_top(r0, r1, rsa->q, ctx)
727 || !bn_mod_add_fixed_top(r0, r0, m1, rsa->n))
733 /* compute I mod q */
735 BIGNUM *c = BN_new();
738 BN_with_flags(c, I, BN_FLG_CONSTTIME);
740 if (!BN_mod(r1, c, rsa->q, ctx)) {
746 BIGNUM *dmq1 = BN_new();
751 BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
753 /* compute r1^dmq1 mod q */
754 if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx,
755 rsa->_method_mod_q)) {
760 /* We MUST free dmq1 before any further use of rsa->dmq1 */
764 /* compute I mod p */
765 if (!BN_mod(r1, c, rsa->p, ctx)) {
769 /* We MUST free c before any further use of I */
774 BIGNUM *dmp1 = BN_new();
777 BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
779 /* compute r1^dmp1 mod p */
780 if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx,
781 rsa->_method_mod_p)) {
785 /* We MUST free dmp1 before any further use of rsa->dmp1 */
791 * calculate m_i in multi-prime case
794 * 1. squash the following two loops and calculate |m_i| there.
795 * 2. remove cc and reuse |c|.
796 * 3. remove |dmq1| and |dmp1| in previous block and use |di|.
798 * If these things are done, the code will be more readable.
801 BIGNUM *di = BN_new(), *cc = BN_new();
803 if (cc == NULL || di == NULL) {
809 for (i = 0; i < ex_primes; i++) {
811 if ((m[i] = BN_CTX_get(ctx)) == NULL) {
817 pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
819 /* prepare c and d_i */
820 BN_with_flags(cc, I, BN_FLG_CONSTTIME);
821 BN_with_flags(di, pinfo->d, BN_FLG_CONSTTIME);
823 if (!BN_mod(r1, cc, pinfo->r, ctx)) {
828 /* compute r1 ^ d_i mod r_i */
829 if (!rsa->meth->bn_mod_exp(m[i], r1, di, pinfo->r, ctx, pinfo->m)) {
841 if (!BN_sub(r0, r0, m1))
844 * This will help stop the size of r0 increasing, which does affect the
845 * multiply if it optimised for a power of 2 size
847 if (BN_is_negative(r0))
848 if (!BN_add(r0, r0, rsa->p))
851 if (!BN_mul(r1, r0, rsa->iqmp, ctx))
855 BIGNUM *pr1 = BN_new();
858 BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
860 if (!BN_mod(r0, pr1, rsa->p, ctx)) {
864 /* We MUST free pr1 before any further use of r1 */
869 * If p < q it is occasionally possible for the correction of adding 'p'
870 * if r0 is negative above to leave the result still negative. This can
871 * break the private key operations: the following second correction
872 * should *always* correct this rare occurrence. This will *never* happen
873 * with OpenSSL generated keys because they ensure p > q [steve]
875 if (BN_is_negative(r0))
876 if (!BN_add(r0, r0, rsa->p))
878 if (!BN_mul(r1, r0, rsa->q, ctx))
880 if (!BN_add(r0, r1, m1))
884 /* add m_i to m in multi-prime case */
886 BIGNUM *pr2 = BN_new();
891 for (i = 0; i < ex_primes; i++) {
892 pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
893 if (!BN_sub(r1, m[i], r0)) {
898 if (!BN_mul(r2, r1, pinfo->t, ctx)) {
903 BN_with_flags(pr2, r2, BN_FLG_CONSTTIME);
905 if (!BN_mod(r1, pr2, pinfo->r, ctx)) {
910 if (BN_is_negative(r1))
911 if (!BN_add(r1, r1, pinfo->r)) {
915 if (!BN_mul(r1, r1, pinfo->pp, ctx)) {
919 if (!BN_add(r0, r0, r1)) {
929 if (rsa->e && rsa->n) {
930 if (rsa->meth->bn_mod_exp == BN_mod_exp_mont) {
931 if (!BN_mod_exp_mont(vrfy, r0, rsa->e, rsa->n, ctx,
936 if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,
941 * If 'I' was greater than (or equal to) rsa->n, the operation will
942 * be equivalent to using 'I mod n'. However, the result of the
943 * verify will *always* be less than 'n' so we don't check for
944 * absolute equality, just congruency.
946 if (!BN_sub(vrfy, vrfy, I))
948 if (BN_is_zero(vrfy)) {
951 goto err; /* not actually error */
953 if (!BN_mod(vrfy, vrfy, rsa->n, ctx))
955 if (BN_is_negative(vrfy))
956 if (!BN_add(vrfy, vrfy, rsa->n))
958 if (!BN_is_zero(vrfy)) {
960 * 'I' and 'vrfy' aren't congruent mod n. Don't leak
961 * miscalculated CRT output, just do a raw (slower) mod_exp and
962 * return that instead.
965 BIGNUM *d = BN_new();
968 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
970 if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx,
971 rsa->_method_mod_n)) {
975 /* We MUST free d before any further use of rsa->d */
980 * It's unfortunate that we have to bn_correct_top(r0). What hopefully
981 * saves the day is that correction is highly unlike, and private key
982 * operations are customarily performed on blinded message. Which means
983 * that attacker won't observe correlation with chosen plaintext.
984 * Secondly, remaining code would still handle it in same computational
985 * time and even conceal memory access pattern around corrected top.
994 static int rsa_ossl_init(RSA *rsa)
996 rsa->flags |= RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE;
1000 static int rsa_ossl_finish(RSA *rsa)
1004 RSA_PRIME_INFO *pinfo;
1006 for (i = 0; i < sk_RSA_PRIME_INFO_num(rsa->prime_infos); i++) {
1007 pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
1008 BN_MONT_CTX_free(pinfo->m);
1012 BN_MONT_CTX_free(rsa->_method_mod_n);
1013 BN_MONT_CTX_free(rsa->_method_mod_p);
1014 BN_MONT_CTX_free(rsa->_method_mod_q);