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
10 #include "internal/cryptlib.h"
11 #include "crypto/bn.h"
12 #include "rsa_local.h"
13 #include "internal/constant_time.h"
15 static int rsa_ossl_public_encrypt(int flen, const unsigned char *from,
16 unsigned char *to, RSA *rsa, int padding);
17 static int rsa_ossl_private_encrypt(int flen, const unsigned char *from,
18 unsigned char *to, RSA *rsa, int padding);
19 static int rsa_ossl_public_decrypt(int flen, const unsigned char *from,
20 unsigned char *to, RSA *rsa, int padding);
21 static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
22 unsigned char *to, RSA *rsa, int padding);
23 static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa,
25 static int rsa_ossl_init(RSA *rsa);
26 static int rsa_ossl_finish(RSA *rsa);
27 static RSA_METHOD rsa_pkcs1_ossl_meth = {
29 rsa_ossl_public_encrypt,
30 rsa_ossl_public_decrypt, /* signature verification */
31 rsa_ossl_private_encrypt, /* signing */
32 rsa_ossl_private_decrypt,
34 BN_mod_exp_mont, /* XXX probably we should not use Montgomery
38 RSA_FLAG_FIPS_METHOD, /* flags */
42 NULL, /* rsa_keygen */
43 NULL /* rsa_multi_prime_keygen */
46 static const RSA_METHOD *default_RSA_meth = &rsa_pkcs1_ossl_meth;
48 void RSA_set_default_method(const RSA_METHOD *meth)
50 default_RSA_meth = meth;
53 const RSA_METHOD *RSA_get_default_method(void)
55 return default_RSA_meth;
58 const RSA_METHOD *RSA_PKCS1_OpenSSL(void)
60 return &rsa_pkcs1_ossl_meth;
63 const RSA_METHOD *RSA_null_method(void)
68 static int rsa_ossl_public_encrypt(int flen, const unsigned char *from,
69 unsigned char *to, RSA *rsa, int padding)
72 int i, num = 0, r = -1;
73 unsigned char *buf = NULL;
76 if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
77 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_MODULUS_TOO_LARGE);
81 if (BN_ucmp(rsa->n, rsa->e) <= 0) {
82 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);
86 /* for large moduli, enforce exponent limit */
87 if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
88 if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
89 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);
94 if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
98 ret = BN_CTX_get(ctx);
99 num = BN_num_bytes(rsa->n);
100 buf = OPENSSL_malloc(num);
101 if (ret == NULL || buf == NULL) {
102 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, ERR_R_MALLOC_FAILURE);
107 case RSA_PKCS1_PADDING:
108 i = RSA_padding_add_PKCS1_type_2(buf, num, from, flen);
110 case RSA_PKCS1_OAEP_PADDING:
111 i = RSA_padding_add_PKCS1_OAEP(buf, num, from, flen, NULL, 0);
114 case RSA_SSLV23_PADDING:
115 i = RSA_padding_add_SSLv23(buf, num, from, flen);
119 i = RSA_padding_add_none(buf, num, from, flen);
122 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
128 if (BN_bin2bn(buf, num, f) == NULL)
131 if (BN_ucmp(f, rsa->n) >= 0) {
132 /* usually the padding functions would catch this */
133 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT,
134 RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
138 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
139 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
143 if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
148 * BN_bn2binpad puts in leading 0 bytes if the number is less than
149 * the length of the modulus.
151 r = BN_bn2binpad(ret, to, num);
155 OPENSSL_clear_free(buf, num);
159 static BN_BLINDING *rsa_get_blinding(RSA *rsa, int *local, BN_CTX *ctx)
163 CRYPTO_THREAD_write_lock(rsa->lock);
165 if (rsa->blinding == NULL) {
166 rsa->blinding = RSA_setup_blinding(rsa, ctx);
173 if (BN_BLINDING_is_current_thread(ret)) {
174 /* rsa->blinding is ours! */
178 /* resort to rsa->mt_blinding instead */
181 * instructs rsa_blinding_convert(), rsa_blinding_invert() that the
182 * BN_BLINDING is shared, meaning that accesses require locks, and
183 * that the blinding factor must be stored outside the BN_BLINDING
187 if (rsa->mt_blinding == NULL) {
188 rsa->mt_blinding = RSA_setup_blinding(rsa, ctx);
190 ret = rsa->mt_blinding;
194 CRYPTO_THREAD_unlock(rsa->lock);
198 static int rsa_blinding_convert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind,
201 if (unblind == NULL) {
203 * Local blinding: store the unblinding factor in BN_BLINDING.
205 return BN_BLINDING_convert_ex(f, NULL, b, ctx);
208 * Shared blinding: store the unblinding factor outside BN_BLINDING.
213 ret = BN_BLINDING_convert_ex(f, unblind, b, ctx);
214 BN_BLINDING_unlock(b);
220 static int rsa_blinding_invert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind,
224 * For local blinding, unblind is set to NULL, and BN_BLINDING_invert_ex
225 * will use the unblinding factor stored in BN_BLINDING. If BN_BLINDING
226 * is shared between threads, unblind must be non-null:
227 * BN_BLINDING_invert_ex will then use the local unblinding factor, and
228 * will only read the modulus from BN_BLINDING. In both cases it's safe
229 * to access the blinding without a lock.
231 return BN_BLINDING_invert_ex(f, unblind, b, ctx);
235 static int rsa_ossl_private_encrypt(int flen, const unsigned char *from,
236 unsigned char *to, RSA *rsa, int padding)
238 BIGNUM *f, *ret, *res;
239 int i, num = 0, r = -1;
240 unsigned char *buf = NULL;
242 int local_blinding = 0;
244 * Used only if the blinding structure is shared. A non-NULL unblind
245 * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
246 * the unblinding factor outside the blinding structure.
248 BIGNUM *unblind = NULL;
249 BN_BLINDING *blinding = NULL;
251 if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
255 ret = BN_CTX_get(ctx);
256 num = BN_num_bytes(rsa->n);
257 buf = OPENSSL_malloc(num);
258 if (ret == NULL || buf == NULL) {
259 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
264 case RSA_PKCS1_PADDING:
265 i = RSA_padding_add_PKCS1_type_1(buf, num, from, flen);
267 case RSA_X931_PADDING:
268 i = RSA_padding_add_X931(buf, num, from, flen);
271 i = RSA_padding_add_none(buf, num, from, flen);
273 case RSA_SSLV23_PADDING:
275 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
281 if (BN_bin2bn(buf, num, f) == NULL)
284 if (BN_ucmp(f, rsa->n) >= 0) {
285 /* usually the padding functions would catch this */
286 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT,
287 RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
291 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
292 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
296 if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
297 blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
298 if (blinding == NULL) {
299 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, ERR_R_INTERNAL_ERROR);
304 if (blinding != NULL) {
305 if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {
306 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
309 if (!rsa_blinding_convert(blinding, f, unblind, ctx))
313 if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
314 (rsa->version == RSA_ASN1_VERSION_MULTI) ||
317 (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
318 if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
321 BIGNUM *d = BN_new();
323 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
326 if (rsa->d == NULL) {
327 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, RSA_R_MISSING_PRIVATE_KEY);
331 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
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 if (!BN_sub(f, rsa->n, ret))
349 if (BN_cmp(ret, f) > 0)
358 * BN_bn2binpad puts in leading 0 bytes if the number is less than
359 * the length of the modulus.
361 r = BN_bn2binpad(res, to, num);
365 OPENSSL_clear_free(buf, num);
369 static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
370 unsigned char *to, RSA *rsa, int padding)
373 int j, num = 0, r = -1;
374 unsigned char *buf = NULL;
376 int local_blinding = 0;
378 * Used only if the blinding structure is shared. A non-NULL unblind
379 * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
380 * the unblinding factor outside the blinding structure.
382 BIGNUM *unblind = NULL;
383 BN_BLINDING *blinding = NULL;
385 if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
389 ret = BN_CTX_get(ctx);
390 num = BN_num_bytes(rsa->n);
391 buf = OPENSSL_malloc(num);
392 if (ret == NULL || buf == NULL) {
393 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE);
398 * This check was for equality but PGP does evil things and chops off the
402 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT,
403 RSA_R_DATA_GREATER_THAN_MOD_LEN);
407 /* make data into a big number */
408 if (BN_bin2bn(from, (int)flen, f) == NULL)
411 if (BN_ucmp(f, rsa->n) >= 0) {
412 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT,
413 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 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_INTERNAL_ERROR);
425 if (blinding != NULL) {
426 if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {
427 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, 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 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE);
448 if (rsa->d == NULL) {
449 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, 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 case RSA_SSLV23_PADDING:
487 r = RSA_padding_check_SSLv23(to, num, buf, j, num);
491 memcpy(to, buf, (r = j));
494 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
499 * This trick doesn't work in the FIPS provider because libcrypto manages
500 * the error stack. Instead we opt not to put an error on the stack at all
501 * in case of padding failure in the FIPS provider.
503 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, RSA_R_PADDING_CHECK_FAILED);
504 err_clear_last_constant_time(1 & ~constant_time_msb(r));
510 OPENSSL_clear_free(buf, num);
514 /* signature verification */
515 static int rsa_ossl_public_decrypt(int flen, const unsigned char *from,
516 unsigned char *to, RSA *rsa, int padding)
519 int i, num = 0, r = -1;
520 unsigned char *buf = NULL;
523 if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
524 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_MODULUS_TOO_LARGE);
528 if (BN_ucmp(rsa->n, rsa->e) <= 0) {
529 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
533 /* for large moduli, enforce exponent limit */
534 if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
535 if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
536 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
541 if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
545 ret = BN_CTX_get(ctx);
546 num = BN_num_bytes(rsa->n);
547 buf = OPENSSL_malloc(num);
548 if (ret == NULL || buf == NULL) {
549 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, ERR_R_MALLOC_FAILURE);
554 * This check was for equality but PGP does evil things and chops off the
558 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_DATA_GREATER_THAN_MOD_LEN);
562 if (BN_bin2bn(from, flen, f) == NULL)
565 if (BN_ucmp(f, rsa->n) >= 0) {
566 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT,
567 RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
571 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
572 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
576 if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
580 if ((padding == RSA_X931_PADDING) && ((bn_get_words(ret)[0] & 0xf) != 12))
581 if (!BN_sub(ret, rsa->n, ret))
584 i = BN_bn2binpad(ret, buf, num);
589 case RSA_PKCS1_PADDING:
590 r = RSA_padding_check_PKCS1_type_1(to, num, buf, i, num);
592 case RSA_X931_PADDING:
593 r = RSA_padding_check_X931(to, num, buf, i, num);
596 memcpy(to, buf, (r = i));
599 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
603 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_PADDING_CHECK_FAILED);
608 OPENSSL_clear_free(buf, num);
612 static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
614 BIGNUM *r1, *m1, *vrfy;
615 int ret = 0, smooth = 0;
617 BIGNUM *r2, *m[RSA_MAX_PRIME_NUM - 2];
618 int i, ex_primes = 0;
619 RSA_PRIME_INFO *pinfo;
624 r1 = BN_CTX_get(ctx);
626 r2 = BN_CTX_get(ctx);
628 m1 = BN_CTX_get(ctx);
629 vrfy = BN_CTX_get(ctx);
634 if (rsa->version == RSA_ASN1_VERSION_MULTI
635 && ((ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) <= 0
636 || ex_primes > RSA_MAX_PRIME_NUM - 2))
640 if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {
641 BIGNUM *factor = BN_new();
647 * Make sure BN_mod_inverse in Montgomery initialization uses the
648 * BN_FLG_CONSTTIME flag
650 if (!(BN_with_flags(factor, rsa->p, BN_FLG_CONSTTIME),
651 BN_MONT_CTX_set_locked(&rsa->_method_mod_p, rsa->lock,
653 || !(BN_with_flags(factor, rsa->q, BN_FLG_CONSTTIME),
654 BN_MONT_CTX_set_locked(&rsa->_method_mod_q, rsa->lock,
660 for (i = 0; i < ex_primes; i++) {
661 pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
662 BN_with_flags(factor, pinfo->r, BN_FLG_CONSTTIME);
663 if (!BN_MONT_CTX_set_locked(&pinfo->m, rsa->lock, factor, ctx)) {
670 * We MUST free |factor| before any further use of the prime factors
674 smooth = (rsa->meth->bn_mod_exp == BN_mod_exp_mont)
678 && (BN_num_bits(rsa->q) == BN_num_bits(rsa->p));
681 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
682 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
688 * Conversion from Montgomery domain, a.k.a. Montgomery reduction,
689 * accepts values in [0-m*2^w) range. w is m's bit width rounded up
690 * to limb width. So that at the very least if |I| is fully reduced,
691 * i.e. less than p*q, we can count on from-to round to perform
692 * below modulo operations on |I|. Unlike BN_mod it's constant time.
694 if (/* m1 = I moq q */
695 !bn_from_mont_fixed_top(m1, I, rsa->_method_mod_q, ctx)
696 || !bn_to_mont_fixed_top(m1, m1, rsa->_method_mod_q, ctx)
697 /* m1 = m1^dmq1 mod q */
698 || !BN_mod_exp_mont_consttime(m1, m1, rsa->dmq1, rsa->q, ctx,
701 || !bn_from_mont_fixed_top(r1, I, rsa->_method_mod_p, ctx)
702 || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)
703 /* r1 = r1^dmp1 mod p */
704 || !BN_mod_exp_mont_consttime(r1, r1, rsa->dmp1, rsa->p, ctx,
706 /* r1 = (r1 - m1) mod p */
708 * bn_mod_sub_fixed_top is not regular modular subtraction,
709 * it can tolerate subtrahend to be larger than modulus, but
710 * not bit-wise wider. This makes up for uncommon q>p case,
711 * when |m1| can be larger than |rsa->p|.
713 || !bn_mod_sub_fixed_top(r1, r1, m1, rsa->p)
715 /* r1 = r1 * iqmp mod p */
716 || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)
717 || !bn_mul_mont_fixed_top(r1, r1, rsa->iqmp, rsa->_method_mod_p,
719 /* r0 = r1 * q + m1 */
720 || !bn_mul_fixed_top(r0, r1, rsa->q, ctx)
721 || !bn_mod_add_fixed_top(r0, r0, m1, rsa->n))
727 /* compute I mod q */
729 BIGNUM *c = BN_new();
732 BN_with_flags(c, I, BN_FLG_CONSTTIME);
734 if (!BN_mod(r1, c, rsa->q, ctx)) {
740 BIGNUM *dmq1 = BN_new();
745 BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
747 /* compute r1^dmq1 mod q */
748 if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx,
749 rsa->_method_mod_q)) {
754 /* We MUST free dmq1 before any further use of rsa->dmq1 */
758 /* compute I mod p */
759 if (!BN_mod(r1, c, rsa->p, ctx)) {
763 /* We MUST free c before any further use of I */
768 BIGNUM *dmp1 = BN_new();
771 BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
773 /* compute r1^dmp1 mod p */
774 if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx,
775 rsa->_method_mod_p)) {
779 /* We MUST free dmp1 before any further use of rsa->dmp1 */
785 * calculate m_i in multi-prime case
788 * 1. squash the following two loops and calculate |m_i| there.
789 * 2. remove cc and reuse |c|.
790 * 3. remove |dmq1| and |dmp1| in previous block and use |di|.
792 * If these things are done, the code will be more readable.
795 BIGNUM *di = BN_new(), *cc = BN_new();
797 if (cc == NULL || di == NULL) {
803 for (i = 0; i < ex_primes; i++) {
805 if ((m[i] = BN_CTX_get(ctx)) == NULL) {
811 pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
813 /* prepare c and d_i */
814 BN_with_flags(cc, I, BN_FLG_CONSTTIME);
815 BN_with_flags(di, pinfo->d, BN_FLG_CONSTTIME);
817 if (!BN_mod(r1, cc, pinfo->r, ctx)) {
822 /* compute r1 ^ d_i mod r_i */
823 if (!rsa->meth->bn_mod_exp(m[i], r1, di, pinfo->r, ctx, pinfo->m)) {
835 if (!BN_sub(r0, r0, m1))
838 * This will help stop the size of r0 increasing, which does affect the
839 * multiply if it optimised for a power of 2 size
841 if (BN_is_negative(r0))
842 if (!BN_add(r0, r0, rsa->p))
845 if (!BN_mul(r1, r0, rsa->iqmp, ctx))
849 BIGNUM *pr1 = BN_new();
852 BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
854 if (!BN_mod(r0, pr1, rsa->p, ctx)) {
858 /* We MUST free pr1 before any further use of r1 */
863 * If p < q it is occasionally possible for the correction of adding 'p'
864 * if r0 is negative above to leave the result still negative. This can
865 * break the private key operations: the following second correction
866 * should *always* correct this rare occurrence. This will *never* happen
867 * with OpenSSL generated keys because they ensure p > q [steve]
869 if (BN_is_negative(r0))
870 if (!BN_add(r0, r0, rsa->p))
872 if (!BN_mul(r1, r0, rsa->q, ctx))
874 if (!BN_add(r0, r1, m1))
878 /* add m_i to m in multi-prime case */
880 BIGNUM *pr2 = BN_new();
885 for (i = 0; i < ex_primes; i++) {
886 pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
887 if (!BN_sub(r1, m[i], r0)) {
892 if (!BN_mul(r2, r1, pinfo->t, ctx)) {
897 BN_with_flags(pr2, r2, BN_FLG_CONSTTIME);
899 if (!BN_mod(r1, pr2, pinfo->r, ctx)) {
904 if (BN_is_negative(r1))
905 if (!BN_add(r1, r1, pinfo->r)) {
909 if (!BN_mul(r1, r1, pinfo->pp, ctx)) {
913 if (!BN_add(r0, r0, r1)) {
923 if (rsa->e && rsa->n) {
924 if (rsa->meth->bn_mod_exp == BN_mod_exp_mont) {
925 if (!BN_mod_exp_mont(vrfy, r0, rsa->e, rsa->n, ctx,
930 if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,
935 * If 'I' was greater than (or equal to) rsa->n, the operation will
936 * be equivalent to using 'I mod n'. However, the result of the
937 * verify will *always* be less than 'n' so we don't check for
938 * absolute equality, just congruency.
940 if (!BN_sub(vrfy, vrfy, I))
942 if (BN_is_zero(vrfy)) {
945 goto err; /* not actually error */
947 if (!BN_mod(vrfy, vrfy, rsa->n, ctx))
949 if (BN_is_negative(vrfy))
950 if (!BN_add(vrfy, vrfy, rsa->n))
952 if (!BN_is_zero(vrfy)) {
954 * 'I' and 'vrfy' aren't congruent mod n. Don't leak
955 * miscalculated CRT output, just do a raw (slower) mod_exp and
956 * return that instead.
959 BIGNUM *d = BN_new();
962 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
964 if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx,
965 rsa->_method_mod_n)) {
969 /* We MUST free d before any further use of rsa->d */
974 * It's unfortunate that we have to bn_correct_top(r0). What hopefully
975 * saves the day is that correction is highly unlike, and private key
976 * operations are customarily performed on blinded message. Which means
977 * that attacker won't observe correlation with chosen plaintext.
978 * Secondly, remaining code would still handle it in same computational
979 * time and even conceal memory access pattern around corrected top.
988 static int rsa_ossl_init(RSA *rsa)
990 rsa->flags |= RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE;
994 static int rsa_ossl_finish(RSA *rsa)
998 RSA_PRIME_INFO *pinfo;
1000 for (i = 0; i < sk_RSA_PRIME_INFO_num(rsa->prime_infos); i++) {
1001 pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
1002 BN_MONT_CTX_free(pinfo->m);
1006 BN_MONT_CTX_free(rsa->_method_mod_n);
1007 BN_MONT_CTX_free(rsa->_method_mod_p);
1008 BN_MONT_CTX_free(rsa->_method_mod_q);