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 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 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 ERR_raise(ERR_LIB_RSA
, 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 ERR_raise(ERR_LIB_RSA
, RSA_R_DATA_TOO_LARGE_FOR_MODULUS
);
146 if (rsa
->flags
& RSA_FLAG_CACHE_PUBLIC
)
147 if (!BN_MONT_CTX_set_locked(&rsa
->_method_mod_n
, rsa
->lock
,
151 if (!rsa
->meth
->bn_mod_exp(ret
, f
, rsa
->e
, rsa
->n
, ctx
,
156 * BN_bn2binpad puts in leading 0 bytes if the number is less than
157 * the length of the modulus.
159 r
= BN_bn2binpad(ret
, to
, num
);
163 OPENSSL_clear_free(buf
, num
);
167 static BN_BLINDING
*rsa_get_blinding(RSA
*rsa
, int *local
, BN_CTX
*ctx
)
171 CRYPTO_THREAD_write_lock(rsa
->lock
);
173 if (rsa
->blinding
== NULL
) {
174 rsa
->blinding
= RSA_setup_blinding(rsa
, ctx
);
181 if (BN_BLINDING_is_current_thread(ret
)) {
182 /* rsa->blinding is ours! */
186 /* resort to rsa->mt_blinding instead */
189 * instructs rsa_blinding_convert(), rsa_blinding_invert() that the
190 * BN_BLINDING is shared, meaning that accesses require locks, and
191 * that the blinding factor must be stored outside the BN_BLINDING
195 if (rsa
->mt_blinding
== NULL
) {
196 rsa
->mt_blinding
= RSA_setup_blinding(rsa
, ctx
);
198 ret
= rsa
->mt_blinding
;
202 CRYPTO_THREAD_unlock(rsa
->lock
);
206 static int rsa_blinding_convert(BN_BLINDING
*b
, BIGNUM
*f
, BIGNUM
*unblind
,
209 if (unblind
== NULL
) {
211 * Local blinding: store the unblinding factor in BN_BLINDING.
213 return BN_BLINDING_convert_ex(f
, NULL
, b
, ctx
);
216 * Shared blinding: store the unblinding factor outside BN_BLINDING.
221 ret
= BN_BLINDING_convert_ex(f
, unblind
, b
, ctx
);
222 BN_BLINDING_unlock(b
);
228 static int rsa_blinding_invert(BN_BLINDING
*b
, BIGNUM
*f
, BIGNUM
*unblind
,
232 * For local blinding, unblind is set to NULL, and BN_BLINDING_invert_ex
233 * will use the unblinding factor stored in BN_BLINDING. If BN_BLINDING
234 * is shared between threads, unblind must be non-null:
235 * BN_BLINDING_invert_ex will then use the local unblinding factor, and
236 * will only read the modulus from BN_BLINDING. In both cases it's safe
237 * to access the blinding without a lock.
239 return BN_BLINDING_invert_ex(f
, unblind
, b
, ctx
);
243 static int rsa_ossl_private_encrypt(int flen
, const unsigned char *from
,
244 unsigned char *to
, RSA
*rsa
, int padding
)
246 BIGNUM
*f
, *ret
, *res
;
247 int i
, num
= 0, r
= -1;
248 unsigned char *buf
= NULL
;
250 int local_blinding
= 0;
252 * Used only if the blinding structure is shared. A non-NULL unblind
253 * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
254 * the unblinding factor outside the blinding structure.
256 BIGNUM
*unblind
= NULL
;
257 BN_BLINDING
*blinding
= NULL
;
259 if ((ctx
= BN_CTX_new_ex(rsa
->libctx
)) == NULL
)
263 ret
= BN_CTX_get(ctx
);
264 num
= BN_num_bytes(rsa
->n
);
265 buf
= OPENSSL_malloc(num
);
266 if (ret
== NULL
|| buf
== NULL
) {
267 ERR_raise(ERR_LIB_RSA
, ERR_R_MALLOC_FAILURE
);
272 case RSA_PKCS1_PADDING
:
273 i
= RSA_padding_add_PKCS1_type_1(buf
, num
, from
, flen
);
275 case RSA_X931_PADDING
:
276 i
= RSA_padding_add_X931(buf
, num
, from
, flen
);
279 i
= RSA_padding_add_none(buf
, num
, from
, flen
);
281 case RSA_SSLV23_PADDING
:
283 ERR_raise(ERR_LIB_RSA
, RSA_R_UNKNOWN_PADDING_TYPE
);
289 if (BN_bin2bn(buf
, num
, f
) == NULL
)
292 if (BN_ucmp(f
, rsa
->n
) >= 0) {
293 /* usually the padding functions would catch this */
294 ERR_raise(ERR_LIB_RSA
, RSA_R_DATA_TOO_LARGE_FOR_MODULUS
);
298 if (rsa
->flags
& RSA_FLAG_CACHE_PUBLIC
)
299 if (!BN_MONT_CTX_set_locked(&rsa
->_method_mod_n
, rsa
->lock
,
303 if (!(rsa
->flags
& RSA_FLAG_NO_BLINDING
)) {
304 blinding
= rsa_get_blinding(rsa
, &local_blinding
, ctx
);
305 if (blinding
== NULL
) {
306 ERR_raise(ERR_LIB_RSA
, ERR_R_INTERNAL_ERROR
);
311 if (blinding
!= NULL
) {
312 if (!local_blinding
&& ((unblind
= BN_CTX_get(ctx
)) == NULL
)) {
313 ERR_raise(ERR_LIB_RSA
, ERR_R_MALLOC_FAILURE
);
316 if (!rsa_blinding_convert(blinding
, f
, unblind
, ctx
))
320 if ((rsa
->flags
& RSA_FLAG_EXT_PKEY
) ||
321 (rsa
->version
== RSA_ASN1_VERSION_MULTI
) ||
324 (rsa
->dmp1
!= NULL
) && (rsa
->dmq1
!= NULL
) && (rsa
->iqmp
!= NULL
))) {
325 if (!rsa
->meth
->rsa_mod_exp(ret
, f
, rsa
, ctx
))
328 BIGNUM
*d
= BN_new();
330 ERR_raise(ERR_LIB_RSA
, ERR_R_MALLOC_FAILURE
);
333 if (rsa
->d
== NULL
) {
334 ERR_raise(ERR_LIB_RSA
, RSA_R_MISSING_PRIVATE_KEY
);
338 BN_with_flags(d
, rsa
->d
, BN_FLG_CONSTTIME
);
340 if (!rsa
->meth
->bn_mod_exp(ret
, f
, d
, rsa
->n
, ctx
,
341 rsa
->_method_mod_n
)) {
345 /* We MUST free d before any further use of rsa->d */
350 if (!rsa_blinding_invert(blinding
, ret
, unblind
, ctx
))
353 if (padding
== RSA_X931_PADDING
) {
354 if (!BN_sub(f
, rsa
->n
, ret
))
356 if (BN_cmp(ret
, f
) > 0)
365 * BN_bn2binpad puts in leading 0 bytes if the number is less than
366 * the length of the modulus.
368 r
= BN_bn2binpad(res
, to
, num
);
372 OPENSSL_clear_free(buf
, num
);
376 static int rsa_ossl_private_decrypt(int flen
, const unsigned char *from
,
377 unsigned char *to
, RSA
*rsa
, int padding
)
380 int j
, num
= 0, r
= -1;
381 unsigned char *buf
= NULL
;
383 int local_blinding
= 0;
385 * Used only if the blinding structure is shared. A non-NULL unblind
386 * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
387 * the unblinding factor outside the blinding structure.
389 BIGNUM
*unblind
= NULL
;
390 BN_BLINDING
*blinding
= NULL
;
392 if ((ctx
= BN_CTX_new_ex(rsa
->libctx
)) == NULL
)
396 ret
= BN_CTX_get(ctx
);
397 num
= BN_num_bytes(rsa
->n
);
398 buf
= OPENSSL_malloc(num
);
399 if (ret
== NULL
|| buf
== NULL
) {
400 ERR_raise(ERR_LIB_RSA
, ERR_R_MALLOC_FAILURE
);
405 * This check was for equality but PGP does evil things and chops off the
409 ERR_raise(ERR_LIB_RSA
, 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 ERR_raise(ERR_LIB_RSA
, RSA_R_DATA_TOO_LARGE_FOR_MODULUS
);
422 if (!(rsa
->flags
& RSA_FLAG_NO_BLINDING
)) {
423 blinding
= rsa_get_blinding(rsa
, &local_blinding
, ctx
);
424 if (blinding
== NULL
) {
425 ERR_raise(ERR_LIB_RSA
, ERR_R_INTERNAL_ERROR
);
430 if (blinding
!= NULL
) {
431 if (!local_blinding
&& ((unblind
= BN_CTX_get(ctx
)) == NULL
)) {
432 ERR_raise(ERR_LIB_RSA
, ERR_R_MALLOC_FAILURE
);
435 if (!rsa_blinding_convert(blinding
, f
, unblind
, ctx
))
440 if ((rsa
->flags
& RSA_FLAG_EXT_PKEY
) ||
441 (rsa
->version
== RSA_ASN1_VERSION_MULTI
) ||
444 (rsa
->dmp1
!= NULL
) && (rsa
->dmq1
!= NULL
) && (rsa
->iqmp
!= NULL
))) {
445 if (!rsa
->meth
->rsa_mod_exp(ret
, f
, rsa
, ctx
))
448 BIGNUM
*d
= BN_new();
450 ERR_raise(ERR_LIB_RSA
, ERR_R_MALLOC_FAILURE
);
453 if (rsa
->d
== NULL
) {
454 ERR_raise(ERR_LIB_RSA
, RSA_R_MISSING_PRIVATE_KEY
);
458 BN_with_flags(d
, rsa
->d
, BN_FLG_CONSTTIME
);
460 if (rsa
->flags
& RSA_FLAG_CACHE_PUBLIC
)
461 if (!BN_MONT_CTX_set_locked(&rsa
->_method_mod_n
, rsa
->lock
,
466 if (!rsa
->meth
->bn_mod_exp(ret
, f
, d
, rsa
->n
, ctx
,
467 rsa
->_method_mod_n
)) {
471 /* We MUST free d before any further use of rsa->d */
476 if (!rsa_blinding_invert(blinding
, ret
, unblind
, ctx
))
479 j
= BN_bn2binpad(ret
, buf
, num
);
484 case RSA_PKCS1_PADDING
:
485 r
= RSA_padding_check_PKCS1_type_2(to
, num
, buf
, j
, num
);
487 case RSA_PKCS1_OAEP_PADDING
:
488 r
= RSA_padding_check_PKCS1_OAEP(to
, num
, buf
, j
, num
, NULL
, 0);
491 case RSA_SSLV23_PADDING
:
492 r
= RSA_padding_check_SSLv23(to
, num
, buf
, j
, num
);
496 memcpy(to
, buf
, (r
= j
));
499 ERR_raise(ERR_LIB_RSA
, RSA_R_UNKNOWN_PADDING_TYPE
);
504 * This trick doesn't work in the FIPS provider because libcrypto manages
505 * the error stack. Instead we opt not to put an error on the stack at all
506 * in case of padding failure in the FIPS provider.
508 ERR_raise(ERR_LIB_RSA
, RSA_R_PADDING_CHECK_FAILED
);
509 err_clear_last_constant_time(1 & ~constant_time_msb(r
));
515 OPENSSL_clear_free(buf
, num
);
519 /* signature verification */
520 static int rsa_ossl_public_decrypt(int flen
, const unsigned char *from
,
521 unsigned char *to
, RSA
*rsa
, int padding
)
524 int i
, num
= 0, r
= -1;
525 unsigned char *buf
= NULL
;
528 if (BN_num_bits(rsa
->n
) > OPENSSL_RSA_MAX_MODULUS_BITS
) {
529 ERR_raise(ERR_LIB_RSA
, RSA_R_MODULUS_TOO_LARGE
);
533 if (BN_ucmp(rsa
->n
, rsa
->e
) <= 0) {
534 ERR_raise(ERR_LIB_RSA
, RSA_R_BAD_E_VALUE
);
538 /* for large moduli, enforce exponent limit */
539 if (BN_num_bits(rsa
->n
) > OPENSSL_RSA_SMALL_MODULUS_BITS
) {
540 if (BN_num_bits(rsa
->e
) > OPENSSL_RSA_MAX_PUBEXP_BITS
) {
541 ERR_raise(ERR_LIB_RSA
, RSA_R_BAD_E_VALUE
);
546 if ((ctx
= BN_CTX_new_ex(rsa
->libctx
)) == NULL
)
550 ret
= BN_CTX_get(ctx
);
551 num
= BN_num_bytes(rsa
->n
);
552 buf
= OPENSSL_malloc(num
);
553 if (ret
== NULL
|| buf
== NULL
) {
554 ERR_raise(ERR_LIB_RSA
, ERR_R_MALLOC_FAILURE
);
559 * This check was for equality but PGP does evil things and chops off the
563 ERR_raise(ERR_LIB_RSA
, RSA_R_DATA_GREATER_THAN_MOD_LEN
);
567 if (BN_bin2bn(from
, flen
, f
) == NULL
)
570 if (BN_ucmp(f
, rsa
->n
) >= 0) {
571 ERR_raise(ERR_LIB_RSA
, RSA_R_DATA_TOO_LARGE_FOR_MODULUS
);
575 if (rsa
->flags
& RSA_FLAG_CACHE_PUBLIC
)
576 if (!BN_MONT_CTX_set_locked(&rsa
->_method_mod_n
, rsa
->lock
,
580 if (!rsa
->meth
->bn_mod_exp(ret
, f
, rsa
->e
, rsa
->n
, ctx
,
584 if ((padding
== RSA_X931_PADDING
) && ((bn_get_words(ret
)[0] & 0xf) != 12))
585 if (!BN_sub(ret
, rsa
->n
, ret
))
588 i
= BN_bn2binpad(ret
, buf
, num
);
593 case RSA_PKCS1_PADDING
:
594 r
= RSA_padding_check_PKCS1_type_1(to
, num
, buf
, i
, num
);
596 case RSA_X931_PADDING
:
597 r
= RSA_padding_check_X931(to
, num
, buf
, i
, num
);
600 memcpy(to
, buf
, (r
= i
));
603 ERR_raise(ERR_LIB_RSA
, RSA_R_UNKNOWN_PADDING_TYPE
);
607 ERR_raise(ERR_LIB_RSA
, RSA_R_PADDING_CHECK_FAILED
);
612 OPENSSL_clear_free(buf
, num
);
616 static int rsa_ossl_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
, RSA
*rsa
, BN_CTX
*ctx
)
618 BIGNUM
*r1
, *m1
, *vrfy
;
619 int ret
= 0, smooth
= 0;
621 BIGNUM
*r2
, *m
[RSA_MAX_PRIME_NUM
- 2];
622 int i
, ex_primes
= 0;
623 RSA_PRIME_INFO
*pinfo
;
628 r1
= BN_CTX_get(ctx
);
630 r2
= BN_CTX_get(ctx
);
632 m1
= BN_CTX_get(ctx
);
633 vrfy
= BN_CTX_get(ctx
);
638 if (rsa
->version
== RSA_ASN1_VERSION_MULTI
639 && ((ex_primes
= sk_RSA_PRIME_INFO_num(rsa
->prime_infos
)) <= 0
640 || ex_primes
> RSA_MAX_PRIME_NUM
- 2))
644 if (rsa
->flags
& RSA_FLAG_CACHE_PRIVATE
) {
645 BIGNUM
*factor
= BN_new();
651 * Make sure BN_mod_inverse in Montgomery initialization uses the
652 * BN_FLG_CONSTTIME flag
654 if (!(BN_with_flags(factor
, rsa
->p
, BN_FLG_CONSTTIME
),
655 BN_MONT_CTX_set_locked(&rsa
->_method_mod_p
, rsa
->lock
,
657 || !(BN_with_flags(factor
, rsa
->q
, BN_FLG_CONSTTIME
),
658 BN_MONT_CTX_set_locked(&rsa
->_method_mod_q
, rsa
->lock
,
664 for (i
= 0; i
< ex_primes
; i
++) {
665 pinfo
= sk_RSA_PRIME_INFO_value(rsa
->prime_infos
, i
);
666 BN_with_flags(factor
, pinfo
->r
, BN_FLG_CONSTTIME
);
667 if (!BN_MONT_CTX_set_locked(&pinfo
->m
, rsa
->lock
, factor
, ctx
)) {
674 * We MUST free |factor| before any further use of the prime factors
678 smooth
= (rsa
->meth
->bn_mod_exp
== BN_mod_exp_mont
)
682 && (BN_num_bits(rsa
->q
) == BN_num_bits(rsa
->p
));
685 if (rsa
->flags
& RSA_FLAG_CACHE_PUBLIC
)
686 if (!BN_MONT_CTX_set_locked(&rsa
->_method_mod_n
, rsa
->lock
,
692 * Conversion from Montgomery domain, a.k.a. Montgomery reduction,
693 * accepts values in [0-m*2^w) range. w is m's bit width rounded up
694 * to limb width. So that at the very least if |I| is fully reduced,
695 * i.e. less than p*q, we can count on from-to round to perform
696 * below modulo operations on |I|. Unlike BN_mod it's constant time.
698 if (/* m1 = I moq q */
699 !bn_from_mont_fixed_top(m1
, I
, rsa
->_method_mod_q
, ctx
)
700 || !bn_to_mont_fixed_top(m1
, m1
, rsa
->_method_mod_q
, ctx
)
701 /* m1 = m1^dmq1 mod q */
702 || !BN_mod_exp_mont_consttime(m1
, m1
, rsa
->dmq1
, rsa
->q
, ctx
,
705 || !bn_from_mont_fixed_top(r1
, I
, rsa
->_method_mod_p
, ctx
)
706 || !bn_to_mont_fixed_top(r1
, r1
, rsa
->_method_mod_p
, ctx
)
707 /* r1 = r1^dmp1 mod p */
708 || !BN_mod_exp_mont_consttime(r1
, r1
, rsa
->dmp1
, rsa
->p
, ctx
,
710 /* r1 = (r1 - m1) mod p */
712 * bn_mod_sub_fixed_top is not regular modular subtraction,
713 * it can tolerate subtrahend to be larger than modulus, but
714 * not bit-wise wider. This makes up for uncommon q>p case,
715 * when |m1| can be larger than |rsa->p|.
717 || !bn_mod_sub_fixed_top(r1
, r1
, m1
, rsa
->p
)
719 /* r1 = r1 * iqmp mod p */
720 || !bn_to_mont_fixed_top(r1
, r1
, rsa
->_method_mod_p
, ctx
)
721 || !bn_mul_mont_fixed_top(r1
, r1
, rsa
->iqmp
, rsa
->_method_mod_p
,
723 /* r0 = r1 * q + m1 */
724 || !bn_mul_fixed_top(r0
, r1
, rsa
->q
, ctx
)
725 || !bn_mod_add_fixed_top(r0
, r0
, m1
, rsa
->n
))
731 /* compute I mod q */
733 BIGNUM
*c
= BN_new();
736 BN_with_flags(c
, I
, BN_FLG_CONSTTIME
);
738 if (!BN_mod(r1
, c
, rsa
->q
, ctx
)) {
744 BIGNUM
*dmq1
= BN_new();
749 BN_with_flags(dmq1
, rsa
->dmq1
, BN_FLG_CONSTTIME
);
751 /* compute r1^dmq1 mod q */
752 if (!rsa
->meth
->bn_mod_exp(m1
, r1
, dmq1
, rsa
->q
, ctx
,
753 rsa
->_method_mod_q
)) {
758 /* We MUST free dmq1 before any further use of rsa->dmq1 */
762 /* compute I mod p */
763 if (!BN_mod(r1
, c
, rsa
->p
, ctx
)) {
767 /* We MUST free c before any further use of I */
772 BIGNUM
*dmp1
= BN_new();
775 BN_with_flags(dmp1
, rsa
->dmp1
, BN_FLG_CONSTTIME
);
777 /* compute r1^dmp1 mod p */
778 if (!rsa
->meth
->bn_mod_exp(r0
, r1
, dmp1
, rsa
->p
, ctx
,
779 rsa
->_method_mod_p
)) {
783 /* We MUST free dmp1 before any further use of rsa->dmp1 */
789 * calculate m_i in multi-prime case
792 * 1. squash the following two loops and calculate |m_i| there.
793 * 2. remove cc and reuse |c|.
794 * 3. remove |dmq1| and |dmp1| in previous block and use |di|.
796 * If these things are done, the code will be more readable.
799 BIGNUM
*di
= BN_new(), *cc
= BN_new();
801 if (cc
== NULL
|| di
== NULL
) {
807 for (i
= 0; i
< ex_primes
; i
++) {
809 if ((m
[i
] = BN_CTX_get(ctx
)) == NULL
) {
815 pinfo
= sk_RSA_PRIME_INFO_value(rsa
->prime_infos
, i
);
817 /* prepare c and d_i */
818 BN_with_flags(cc
, I
, BN_FLG_CONSTTIME
);
819 BN_with_flags(di
, pinfo
->d
, BN_FLG_CONSTTIME
);
821 if (!BN_mod(r1
, cc
, pinfo
->r
, ctx
)) {
826 /* compute r1 ^ d_i mod r_i */
827 if (!rsa
->meth
->bn_mod_exp(m
[i
], r1
, di
, pinfo
->r
, ctx
, pinfo
->m
)) {
839 if (!BN_sub(r0
, r0
, m1
))
842 * This will help stop the size of r0 increasing, which does affect the
843 * multiply if it optimised for a power of 2 size
845 if (BN_is_negative(r0
))
846 if (!BN_add(r0
, r0
, rsa
->p
))
849 if (!BN_mul(r1
, r0
, rsa
->iqmp
, ctx
))
853 BIGNUM
*pr1
= BN_new();
856 BN_with_flags(pr1
, r1
, BN_FLG_CONSTTIME
);
858 if (!BN_mod(r0
, pr1
, rsa
->p
, ctx
)) {
862 /* We MUST free pr1 before any further use of r1 */
867 * If p < q it is occasionally possible for the correction of adding 'p'
868 * if r0 is negative above to leave the result still negative. This can
869 * break the private key operations: the following second correction
870 * should *always* correct this rare occurrence. This will *never* happen
871 * with OpenSSL generated keys because they ensure p > q [steve]
873 if (BN_is_negative(r0
))
874 if (!BN_add(r0
, r0
, rsa
->p
))
876 if (!BN_mul(r1
, r0
, rsa
->q
, ctx
))
878 if (!BN_add(r0
, r1
, m1
))
882 /* add m_i to m in multi-prime case */
884 BIGNUM
*pr2
= BN_new();
889 for (i
= 0; i
< ex_primes
; i
++) {
890 pinfo
= sk_RSA_PRIME_INFO_value(rsa
->prime_infos
, i
);
891 if (!BN_sub(r1
, m
[i
], r0
)) {
896 if (!BN_mul(r2
, r1
, pinfo
->t
, ctx
)) {
901 BN_with_flags(pr2
, r2
, BN_FLG_CONSTTIME
);
903 if (!BN_mod(r1
, pr2
, pinfo
->r
, ctx
)) {
908 if (BN_is_negative(r1
))
909 if (!BN_add(r1
, r1
, pinfo
->r
)) {
913 if (!BN_mul(r1
, r1
, pinfo
->pp
, ctx
)) {
917 if (!BN_add(r0
, r0
, r1
)) {
927 if (rsa
->e
&& rsa
->n
) {
928 if (rsa
->meth
->bn_mod_exp
== BN_mod_exp_mont
) {
929 if (!BN_mod_exp_mont(vrfy
, r0
, rsa
->e
, rsa
->n
, ctx
,
934 if (!rsa
->meth
->bn_mod_exp(vrfy
, r0
, rsa
->e
, rsa
->n
, ctx
,
939 * If 'I' was greater than (or equal to) rsa->n, the operation will
940 * be equivalent to using 'I mod n'. However, the result of the
941 * verify will *always* be less than 'n' so we don't check for
942 * absolute equality, just congruency.
944 if (!BN_sub(vrfy
, vrfy
, I
))
946 if (BN_is_zero(vrfy
)) {
949 goto err
; /* not actually error */
951 if (!BN_mod(vrfy
, vrfy
, rsa
->n
, ctx
))
953 if (BN_is_negative(vrfy
))
954 if (!BN_add(vrfy
, vrfy
, rsa
->n
))
956 if (!BN_is_zero(vrfy
)) {
958 * 'I' and 'vrfy' aren't congruent mod n. Don't leak
959 * miscalculated CRT output, just do a raw (slower) mod_exp and
960 * return that instead.
963 BIGNUM
*d
= BN_new();
966 BN_with_flags(d
, rsa
->d
, BN_FLG_CONSTTIME
);
968 if (!rsa
->meth
->bn_mod_exp(r0
, I
, d
, rsa
->n
, ctx
,
969 rsa
->_method_mod_n
)) {
973 /* We MUST free d before any further use of rsa->d */
978 * It's unfortunate that we have to bn_correct_top(r0). What hopefully
979 * saves the day is that correction is highly unlike, and private key
980 * operations are customarily performed on blinded message. Which means
981 * that attacker won't observe correlation with chosen plaintext.
982 * Secondly, remaining code would still handle it in same computational
983 * time and even conceal memory access pattern around corrected top.
992 static int rsa_ossl_init(RSA
*rsa
)
994 rsa
->flags
|= RSA_FLAG_CACHE_PUBLIC
| RSA_FLAG_CACHE_PRIVATE
;
998 static int rsa_ossl_finish(RSA
*rsa
)
1002 RSA_PRIME_INFO
*pinfo
;
1004 for (i
= 0; i
< sk_RSA_PRIME_INFO_num(rsa
->prime_infos
); i
++) {
1005 pinfo
= sk_RSA_PRIME_INFO_value(rsa
->prime_infos
, i
);
1006 BN_MONT_CTX_free(pinfo
->m
);
1010 BN_MONT_CTX_free(rsa
->_method_mod_n
);
1011 BN_MONT_CTX_free(rsa
->_method_mod_p
);
1012 BN_MONT_CTX_free(rsa
->_method_mod_q
);