2 * Copyright 1995-2018 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
, 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(&rsa
->_method_mod_n
, rsa
->lock
,
140 if (!rsa
->meth
->bn_mod_exp(ret
, f
, rsa
->e
, rsa
->n
, ctx
,
145 * BN_bn2binpad puts in leading 0 bytes if the number is less than
146 * the length of the modulus.
148 r
= BN_bn2binpad(ret
, to
, num
);
153 OPENSSL_clear_free(buf
, num
);
157 static BN_BLINDING
*rsa_get_blinding(RSA
*rsa
, int *local
, BN_CTX
*ctx
)
161 CRYPTO_THREAD_write_lock(rsa
->lock
);
163 if (rsa
->blinding
== NULL
) {
164 rsa
->blinding
= RSA_setup_blinding(rsa
, ctx
);
171 if (BN_BLINDING_is_current_thread(ret
)) {
172 /* rsa->blinding is ours! */
176 /* resort to rsa->mt_blinding instead */
179 * instructs rsa_blinding_convert(), rsa_blinding_invert() that the
180 * BN_BLINDING is shared, meaning that accesses require locks, and
181 * that the blinding factor must be stored outside the BN_BLINDING
185 if (rsa
->mt_blinding
== NULL
) {
186 rsa
->mt_blinding
= RSA_setup_blinding(rsa
, ctx
);
188 ret
= rsa
->mt_blinding
;
192 CRYPTO_THREAD_unlock(rsa
->lock
);
196 static int rsa_blinding_convert(BN_BLINDING
*b
, BIGNUM
*f
, BIGNUM
*unblind
,
199 if (unblind
== NULL
) {
201 * Local blinding: store the unblinding factor in BN_BLINDING.
203 return BN_BLINDING_convert_ex(f
, NULL
, b
, ctx
);
206 * Shared blinding: store the unblinding factor outside BN_BLINDING.
211 ret
= BN_BLINDING_convert_ex(f
, unblind
, b
, ctx
);
212 BN_BLINDING_unlock(b
);
218 static int rsa_blinding_invert(BN_BLINDING
*b
, BIGNUM
*f
, BIGNUM
*unblind
,
222 * For local blinding, unblind is set to NULL, and BN_BLINDING_invert_ex
223 * will use the unblinding factor stored in BN_BLINDING. If BN_BLINDING
224 * is shared between threads, unblind must be non-null:
225 * BN_BLINDING_invert_ex will then use the local unblinding factor, and
226 * will only read the modulus from BN_BLINDING. In both cases it's safe
227 * to access the blinding without a lock.
229 return BN_BLINDING_invert_ex(f
, unblind
, b
, ctx
);
233 static int rsa_ossl_private_encrypt(int flen
, const unsigned char *from
,
234 unsigned char *to
, RSA
*rsa
, int padding
)
236 BIGNUM
*f
, *ret
, *res
;
237 int i
, num
= 0, r
= -1;
238 unsigned char *buf
= NULL
;
240 int local_blinding
= 0;
242 * Used only if the blinding structure is shared. A non-NULL unblind
243 * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
244 * the unblinding factor outside the blinding structure.
246 BIGNUM
*unblind
= NULL
;
247 BN_BLINDING
*blinding
= NULL
;
249 if ((ctx
= BN_CTX_new()) == NULL
)
253 ret
= BN_CTX_get(ctx
);
254 num
= BN_num_bytes(rsa
->n
);
255 buf
= OPENSSL_malloc(num
);
256 if (ret
== NULL
|| buf
== NULL
) {
257 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT
, ERR_R_MALLOC_FAILURE
);
262 case RSA_PKCS1_PADDING
:
263 i
= RSA_padding_add_PKCS1_type_1(buf
, num
, from
, flen
);
265 case RSA_X931_PADDING
:
266 i
= RSA_padding_add_X931(buf
, num
, from
, flen
);
269 i
= RSA_padding_add_none(buf
, num
, from
, flen
);
271 case RSA_SSLV23_PADDING
:
273 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT
, RSA_R_UNKNOWN_PADDING_TYPE
);
279 if (BN_bin2bn(buf
, num
, f
) == NULL
)
282 if (BN_ucmp(f
, rsa
->n
) >= 0) {
283 /* usually the padding functions would catch this */
284 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT
,
285 RSA_R_DATA_TOO_LARGE_FOR_MODULUS
);
289 if (!(rsa
->flags
& RSA_FLAG_NO_BLINDING
)) {
290 blinding
= rsa_get_blinding(rsa
, &local_blinding
, ctx
);
291 if (blinding
== NULL
) {
292 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT
, ERR_R_INTERNAL_ERROR
);
297 if (blinding
!= NULL
) {
298 if (!local_blinding
&& ((unblind
= BN_CTX_get(ctx
)) == NULL
)) {
299 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT
, ERR_R_MALLOC_FAILURE
);
302 if (!rsa_blinding_convert(blinding
, f
, unblind
, ctx
))
306 if ((rsa
->flags
& RSA_FLAG_EXT_PKEY
) ||
307 (rsa
->version
== RSA_ASN1_VERSION_MULTI
) ||
310 (rsa
->dmp1
!= NULL
) && (rsa
->dmq1
!= NULL
) && (rsa
->iqmp
!= NULL
))) {
311 if (!rsa
->meth
->rsa_mod_exp(ret
, f
, rsa
, ctx
))
314 BIGNUM
*d
= BN_new();
316 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT
, ERR_R_MALLOC_FAILURE
);
319 BN_with_flags(d
, rsa
->d
, BN_FLG_CONSTTIME
);
321 if (rsa
->flags
& RSA_FLAG_CACHE_PUBLIC
)
322 if (!BN_MONT_CTX_set_locked(&rsa
->_method_mod_n
, rsa
->lock
,
328 if (!rsa
->meth
->bn_mod_exp(ret
, f
, d
, rsa
->n
, ctx
,
329 rsa
->_method_mod_n
)) {
333 /* We MUST free d before any further use of rsa->d */
338 if (!rsa_blinding_invert(blinding
, ret
, unblind
, ctx
))
341 if (padding
== RSA_X931_PADDING
) {
342 if (!BN_sub(f
, rsa
->n
, ret
))
344 if (BN_cmp(ret
, f
) > 0)
353 * BN_bn2binpad puts in leading 0 bytes if the number is less than
354 * the length of the modulus.
356 r
= BN_bn2binpad(res
, to
, num
);
361 OPENSSL_clear_free(buf
, num
);
365 static int rsa_ossl_private_decrypt(int flen
, const unsigned char *from
,
366 unsigned char *to
, RSA
*rsa
, int padding
)
369 int j
, num
= 0, r
= -1;
370 unsigned char *buf
= NULL
;
372 int local_blinding
= 0;
374 * Used only if the blinding structure is shared. A non-NULL unblind
375 * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
376 * the unblinding factor outside the blinding structure.
378 BIGNUM
*unblind
= NULL
;
379 BN_BLINDING
*blinding
= NULL
;
381 if ((ctx
= BN_CTX_new()) == NULL
)
385 ret
= BN_CTX_get(ctx
);
386 num
= BN_num_bytes(rsa
->n
);
387 buf
= OPENSSL_malloc(num
);
388 if (ret
== NULL
|| buf
== NULL
) {
389 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT
, ERR_R_MALLOC_FAILURE
);
394 * This check was for equality but PGP does evil things and chops off the
398 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT
,
399 RSA_R_DATA_GREATER_THAN_MOD_LEN
);
403 /* make data into a big number */
404 if (BN_bin2bn(from
, (int)flen
, f
) == NULL
)
407 if (BN_ucmp(f
, rsa
->n
) >= 0) {
408 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT
,
409 RSA_R_DATA_TOO_LARGE_FOR_MODULUS
);
413 if (!(rsa
->flags
& RSA_FLAG_NO_BLINDING
)) {
414 blinding
= rsa_get_blinding(rsa
, &local_blinding
, ctx
);
415 if (blinding
== NULL
) {
416 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT
, ERR_R_INTERNAL_ERROR
);
421 if (blinding
!= NULL
) {
422 if (!local_blinding
&& ((unblind
= BN_CTX_get(ctx
)) == NULL
)) {
423 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT
, ERR_R_MALLOC_FAILURE
);
426 if (!rsa_blinding_convert(blinding
, f
, unblind
, ctx
))
431 if ((rsa
->flags
& RSA_FLAG_EXT_PKEY
) ||
432 (rsa
->version
== RSA_ASN1_VERSION_MULTI
) ||
435 (rsa
->dmp1
!= NULL
) && (rsa
->dmq1
!= NULL
) && (rsa
->iqmp
!= NULL
))) {
436 if (!rsa
->meth
->rsa_mod_exp(ret
, f
, rsa
, ctx
))
439 BIGNUM
*d
= BN_new();
441 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT
, ERR_R_MALLOC_FAILURE
);
444 BN_with_flags(d
, rsa
->d
, BN_FLG_CONSTTIME
);
446 if (rsa
->flags
& RSA_FLAG_CACHE_PUBLIC
)
447 if (!BN_MONT_CTX_set_locked(&rsa
->_method_mod_n
, rsa
->lock
,
452 if (!rsa
->meth
->bn_mod_exp(ret
, f
, d
, rsa
->n
, ctx
,
453 rsa
->_method_mod_n
)) {
457 /* We MUST free d before any further use of rsa->d */
462 if (!rsa_blinding_invert(blinding
, ret
, unblind
, ctx
))
465 j
= BN_bn2binpad(ret
, buf
, num
);
468 case RSA_PKCS1_PADDING
:
469 r
= RSA_padding_check_PKCS1_type_2(to
, num
, buf
, j
, num
);
471 case RSA_PKCS1_OAEP_PADDING
:
472 r
= RSA_padding_check_PKCS1_OAEP(to
, num
, buf
, j
, num
, NULL
, 0);
474 case RSA_SSLV23_PADDING
:
475 r
= RSA_padding_check_SSLv23(to
, num
, buf
, j
, num
);
478 memcpy(to
, buf
, (r
= j
));
481 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT
, RSA_R_UNKNOWN_PADDING_TYPE
);
485 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT
, RSA_R_PADDING_CHECK_FAILED
);
491 OPENSSL_clear_free(buf
, num
);
495 /* signature verification */
496 static int rsa_ossl_public_decrypt(int flen
, const unsigned char *from
,
497 unsigned char *to
, RSA
*rsa
, int padding
)
500 int i
, num
= 0, r
= -1;
501 unsigned char *buf
= NULL
;
504 if (BN_num_bits(rsa
->n
) > OPENSSL_RSA_MAX_MODULUS_BITS
) {
505 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT
, RSA_R_MODULUS_TOO_LARGE
);
509 if (BN_ucmp(rsa
->n
, rsa
->e
) <= 0) {
510 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT
, RSA_R_BAD_E_VALUE
);
514 /* for large moduli, enforce exponent limit */
515 if (BN_num_bits(rsa
->n
) > OPENSSL_RSA_SMALL_MODULUS_BITS
) {
516 if (BN_num_bits(rsa
->e
) > OPENSSL_RSA_MAX_PUBEXP_BITS
) {
517 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT
, RSA_R_BAD_E_VALUE
);
522 if ((ctx
= BN_CTX_new()) == NULL
)
526 ret
= BN_CTX_get(ctx
);
527 num
= BN_num_bytes(rsa
->n
);
528 buf
= OPENSSL_malloc(num
);
529 if (ret
== NULL
|| buf
== NULL
) {
530 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT
, ERR_R_MALLOC_FAILURE
);
535 * This check was for equality but PGP does evil things and chops off the
539 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT
, RSA_R_DATA_GREATER_THAN_MOD_LEN
);
543 if (BN_bin2bn(from
, flen
, f
) == NULL
)
546 if (BN_ucmp(f
, rsa
->n
) >= 0) {
547 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT
,
548 RSA_R_DATA_TOO_LARGE_FOR_MODULUS
);
552 if (rsa
->flags
& RSA_FLAG_CACHE_PUBLIC
)
553 if (!BN_MONT_CTX_set_locked(&rsa
->_method_mod_n
, rsa
->lock
,
557 if (!rsa
->meth
->bn_mod_exp(ret
, f
, rsa
->e
, rsa
->n
, ctx
,
561 if ((padding
== RSA_X931_PADDING
) && ((bn_get_words(ret
)[0] & 0xf) != 12))
562 if (!BN_sub(ret
, rsa
->n
, ret
))
565 i
= BN_bn2binpad(ret
, buf
, num
);
568 case RSA_PKCS1_PADDING
:
569 r
= RSA_padding_check_PKCS1_type_1(to
, num
, buf
, i
, num
);
571 case RSA_X931_PADDING
:
572 r
= RSA_padding_check_X931(to
, num
, buf
, i
, num
);
575 memcpy(to
, buf
, (r
= i
));
578 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT
, RSA_R_UNKNOWN_PADDING_TYPE
);
582 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT
, RSA_R_PADDING_CHECK_FAILED
);
588 OPENSSL_clear_free(buf
, num
);
592 static int rsa_ossl_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
, RSA
*rsa
, BN_CTX
*ctx
)
594 BIGNUM
*r1
, *m1
, *vrfy
, *r2
, *m
[RSA_MAX_PRIME_NUM
- 2];
595 int ret
= 0, i
, ex_primes
= 0, smooth
= 0;
596 RSA_PRIME_INFO
*pinfo
;
600 r1
= BN_CTX_get(ctx
);
601 r2
= BN_CTX_get(ctx
);
602 m1
= BN_CTX_get(ctx
);
603 vrfy
= BN_CTX_get(ctx
);
607 if (rsa
->version
== RSA_ASN1_VERSION_MULTI
608 && ((ex_primes
= sk_RSA_PRIME_INFO_num(rsa
->prime_infos
)) <= 0
609 || ex_primes
> RSA_MAX_PRIME_NUM
- 2))
612 if (rsa
->flags
& RSA_FLAG_CACHE_PRIVATE
) {
613 BIGNUM
*factor
= BN_new();
619 * Make sure BN_mod_inverse in Montgomery initialization uses the
620 * BN_FLG_CONSTTIME flag
622 if (!(BN_with_flags(factor
, rsa
->p
, BN_FLG_CONSTTIME
),
623 BN_MONT_CTX_set_locked(&rsa
->_method_mod_p
, rsa
->lock
,
625 || !(BN_with_flags(factor
, rsa
->q
, BN_FLG_CONSTTIME
),
626 BN_MONT_CTX_set_locked(&rsa
->_method_mod_q
, rsa
->lock
,
631 for (i
= 0; i
< ex_primes
; i
++) {
632 pinfo
= sk_RSA_PRIME_INFO_value(rsa
->prime_infos
, i
);
633 BN_with_flags(factor
, pinfo
->r
, BN_FLG_CONSTTIME
);
634 if (!BN_MONT_CTX_set_locked(&pinfo
->m
, rsa
->lock
, factor
, ctx
)) {
640 * We MUST free |factor| before any further use of the prime factors
644 smooth
= (ex_primes
== 0)
645 && (rsa
->meth
->bn_mod_exp
== BN_mod_exp_mont
)
646 && (BN_num_bits(rsa
->q
) == BN_num_bits(rsa
->p
));
649 if (rsa
->flags
& RSA_FLAG_CACHE_PUBLIC
)
650 if (!BN_MONT_CTX_set_locked(&rsa
->_method_mod_n
, rsa
->lock
,
656 * Conversion from Montgomery domain, a.k.a. Montgomery reduction,
657 * accepts values in [0-m*2^w) range. w is m's bit width rounded up
658 * to limb width. So that at the very least if |I| is fully reduced,
659 * i.e. less than p*q, we can count on from-to round to perform
660 * below modulo operations on |I|. Unlike BN_mod it's constant time.
662 if (/* m1 = I moq q */
663 !bn_from_mont_fixed_top(m1
, I
, rsa
->_method_mod_q
, ctx
)
664 || !bn_to_mont_fixed_top(m1
, m1
, rsa
->_method_mod_q
, ctx
)
665 /* m1 = m1^dmq1 mod q */
666 || !BN_mod_exp_mont_consttime(m1
, m1
, rsa
->dmq1
, rsa
->q
, ctx
,
669 || !bn_from_mont_fixed_top(r1
, I
, rsa
->_method_mod_p
, ctx
)
670 || !bn_to_mont_fixed_top(r1
, r1
, rsa
->_method_mod_p
, ctx
)
671 /* r1 = r1^dmp1 mod p */
672 || !BN_mod_exp_mont_consttime(r1
, r1
, rsa
->dmp1
, rsa
->p
, ctx
,
674 /* r1 = (r1 - m1) mod p */
676 * bn_mod_sub_fixed_top is not regular modular subtraction,
677 * it can tolerate subtrahend to be larger than modulus, but
678 * not bit-wise wider. This makes up for uncommon q>p case,
679 * when |m1| can be larger than |rsa->p|.
681 || !bn_mod_sub_fixed_top(r1
, r1
, m1
, rsa
->p
)
683 /* r1 = r1 * iqmp mod p */
684 || !bn_to_mont_fixed_top(r1
, r1
, rsa
->_method_mod_p
, ctx
)
685 || !bn_mul_mont_fixed_top(r1
, r1
, rsa
->iqmp
, rsa
->_method_mod_p
,
687 /* r0 = r1 * q + m1 */
688 || !bn_mul_fixed_top(r0
, r1
, rsa
->q
, ctx
)
689 || !bn_mod_add_fixed_top(r0
, r0
, m1
, rsa
->n
))
695 /* compute I mod q */
697 BIGNUM
*c
= BN_new();
700 BN_with_flags(c
, I
, BN_FLG_CONSTTIME
);
702 if (!BN_mod(r1
, c
, rsa
->q
, ctx
)) {
708 BIGNUM
*dmq1
= BN_new();
713 BN_with_flags(dmq1
, rsa
->dmq1
, BN_FLG_CONSTTIME
);
715 /* compute r1^dmq1 mod q */
716 if (!rsa
->meth
->bn_mod_exp(m1
, r1
, dmq1
, rsa
->q
, ctx
,
717 rsa
->_method_mod_q
)) {
722 /* We MUST free dmq1 before any further use of rsa->dmq1 */
726 /* compute I mod p */
727 if (!BN_mod(r1
, c
, rsa
->p
, ctx
)) {
731 /* We MUST free c before any further use of I */
736 BIGNUM
*dmp1
= BN_new();
739 BN_with_flags(dmp1
, rsa
->dmp1
, BN_FLG_CONSTTIME
);
741 /* compute r1^dmp1 mod p */
742 if (!rsa
->meth
->bn_mod_exp(r0
, r1
, dmp1
, rsa
->p
, ctx
,
743 rsa
->_method_mod_p
)) {
747 /* We MUST free dmp1 before any further use of rsa->dmp1 */
752 * calculate m_i in multi-prime case
755 * 1. squash the following two loops and calculate |m_i| there.
756 * 2. remove cc and reuse |c|.
757 * 3. remove |dmq1| and |dmp1| in previous block and use |di|.
759 * If these things are done, the code will be more readable.
762 BIGNUM
*di
= BN_new(), *cc
= BN_new();
764 if (cc
== NULL
|| di
== NULL
) {
770 for (i
= 0; i
< ex_primes
; i
++) {
772 if ((m
[i
] = BN_CTX_get(ctx
)) == NULL
) {
778 pinfo
= sk_RSA_PRIME_INFO_value(rsa
->prime_infos
, i
);
780 /* prepare c and d_i */
781 BN_with_flags(cc
, I
, BN_FLG_CONSTTIME
);
782 BN_with_flags(di
, pinfo
->d
, BN_FLG_CONSTTIME
);
784 if (!BN_mod(r1
, cc
, pinfo
->r
, ctx
)) {
789 /* compute r1 ^ d_i mod r_i */
790 if (!rsa
->meth
->bn_mod_exp(m
[i
], r1
, di
, pinfo
->r
, ctx
, pinfo
->m
)) {
801 if (!BN_sub(r0
, r0
, m1
))
804 * This will help stop the size of r0 increasing, which does affect the
805 * multiply if it optimised for a power of 2 size
807 if (BN_is_negative(r0
))
808 if (!BN_add(r0
, r0
, rsa
->p
))
811 if (!BN_mul(r1
, r0
, rsa
->iqmp
, ctx
))
815 BIGNUM
*pr1
= BN_new();
818 BN_with_flags(pr1
, r1
, BN_FLG_CONSTTIME
);
820 if (!BN_mod(r0
, pr1
, rsa
->p
, ctx
)) {
824 /* We MUST free pr1 before any further use of r1 */
829 * If p < q it is occasionally possible for the correction of adding 'p'
830 * if r0 is negative above to leave the result still negative. This can
831 * break the private key operations: the following second correction
832 * should *always* correct this rare occurrence. This will *never* happen
833 * with OpenSSL generated keys because they ensure p > q [steve]
835 if (BN_is_negative(r0
))
836 if (!BN_add(r0
, r0
, rsa
->p
))
838 if (!BN_mul(r1
, r0
, rsa
->q
, ctx
))
840 if (!BN_add(r0
, r1
, m1
))
843 /* add m_i to m in multi-prime case */
845 BIGNUM
*pr2
= BN_new();
850 for (i
= 0; i
< ex_primes
; i
++) {
851 pinfo
= sk_RSA_PRIME_INFO_value(rsa
->prime_infos
, i
);
852 if (!BN_sub(r1
, m
[i
], r0
)) {
857 if (!BN_mul(r2
, r1
, pinfo
->t
, ctx
)) {
862 BN_with_flags(pr2
, r2
, BN_FLG_CONSTTIME
);
864 if (!BN_mod(r1
, pr2
, pinfo
->r
, ctx
)) {
869 if (BN_is_negative(r1
))
870 if (!BN_add(r1
, r1
, pinfo
->r
)) {
874 if (!BN_mul(r1
, r1
, pinfo
->pp
, ctx
)) {
878 if (!BN_add(r0
, r0
, r1
)) {
887 if (rsa
->e
&& rsa
->n
) {
888 if (rsa
->meth
->bn_mod_exp
== BN_mod_exp_mont
) {
889 if (!BN_mod_exp_mont(vrfy
, r0
, rsa
->e
, rsa
->n
, ctx
,
894 if (!rsa
->meth
->bn_mod_exp(vrfy
, r0
, rsa
->e
, rsa
->n
, ctx
,
899 * If 'I' was greater than (or equal to) rsa->n, the operation will
900 * be equivalent to using 'I mod n'. However, the result of the
901 * verify will *always* be less than 'n' so we don't check for
902 * absolute equality, just congruency.
904 if (!BN_sub(vrfy
, vrfy
, I
))
906 if (BN_is_zero(vrfy
)) {
909 goto err
; /* not actually error */
911 if (!BN_mod(vrfy
, vrfy
, rsa
->n
, ctx
))
913 if (BN_is_negative(vrfy
))
914 if (!BN_add(vrfy
, vrfy
, rsa
->n
))
916 if (!BN_is_zero(vrfy
)) {
918 * 'I' and 'vrfy' aren't congruent mod n. Don't leak
919 * miscalculated CRT output, just do a raw (slower) mod_exp and
920 * return that instead.
923 BIGNUM
*d
= BN_new();
926 BN_with_flags(d
, rsa
->d
, BN_FLG_CONSTTIME
);
928 if (!rsa
->meth
->bn_mod_exp(r0
, I
, d
, rsa
->n
, ctx
,
929 rsa
->_method_mod_n
)) {
933 /* We MUST free d before any further use of rsa->d */
938 * It's unfortunate that we have to bn_correct_top(r0). What hopefully
939 * saves the day is that correction is highly unlike, and private key
940 * operations are customarily performed on blinded message. Which means
941 * that attacker won't observe correlation with chosen plaintext.
942 * Secondly, remaining code would still handle it in same computational
943 * time and even conceal memory access pattern around corrected top.
952 static int rsa_ossl_init(RSA
*rsa
)
954 rsa
->flags
|= RSA_FLAG_CACHE_PUBLIC
| RSA_FLAG_CACHE_PRIVATE
;
958 static int rsa_ossl_finish(RSA
*rsa
)
961 RSA_PRIME_INFO
*pinfo
;
963 BN_MONT_CTX_free(rsa
->_method_mod_n
);
964 BN_MONT_CTX_free(rsa
->_method_mod_p
);
965 BN_MONT_CTX_free(rsa
->_method_mod_q
);
966 for (i
= 0; i
< sk_RSA_PRIME_INFO_num(rsa
->prime_infos
); i
++) {
967 pinfo
= sk_RSA_PRIME_INFO_value(rsa
->prime_infos
, i
);
968 BN_MONT_CTX_free(pinfo
->m
);