1 /* $OpenBSD: rsa_eay.c,v 1.50 2017/08/28 17:41:59 jsing Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
58 /* ====================================================================
59 * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
87 * 6. Redistributions of any form whatsoever must retain the following
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
115 #include <openssl/opensslconf.h>
117 #include <openssl/bn.h>
118 #include <openssl/err.h>
119 #include <openssl/rsa.h>
123 static int RSA_eay_public_encrypt(int flen
, const unsigned char *from
,
124 unsigned char *to
, RSA
*rsa
, int padding
);
125 static int RSA_eay_private_encrypt(int flen
, const unsigned char *from
,
126 unsigned char *to
, RSA
*rsa
, int padding
);
127 static int RSA_eay_public_decrypt(int flen
, const unsigned char *from
,
128 unsigned char *to
, RSA
*rsa
, int padding
);
129 static int RSA_eay_private_decrypt(int flen
, const unsigned char *from
,
130 unsigned char *to
, RSA
*rsa
, int padding
);
131 static int RSA_eay_mod_exp(BIGNUM
*r0
, const BIGNUM
*i
, RSA
*rsa
, BN_CTX
*ctx
);
132 static int RSA_eay_init(RSA
*rsa
);
133 static int RSA_eay_finish(RSA
*rsa
);
135 static RSA_METHOD rsa_pkcs1_eay_meth
= {
136 .name
= "Eric Young's PKCS#1 RSA",
137 .rsa_pub_enc
= RSA_eay_public_encrypt
,
138 .rsa_pub_dec
= RSA_eay_public_decrypt
, /* signature verification */
139 .rsa_priv_enc
= RSA_eay_private_encrypt
, /* signing */
140 .rsa_priv_dec
= RSA_eay_private_decrypt
,
141 .rsa_mod_exp
= RSA_eay_mod_exp
,
142 .bn_mod_exp
= BN_mod_exp_mont_ct
, /* XXX probably we should not use Montgomery if e == 3 */
143 .init
= RSA_eay_init
,
144 .finish
= RSA_eay_finish
,
148 RSA_PKCS1_SSLeay(void)
150 return &rsa_pkcs1_eay_meth
;
154 RSA_eay_public_encrypt(int flen
, const unsigned char *from
, unsigned char *to
,
155 RSA
*rsa
, int padding
)
158 int i
, j
, k
, num
= 0, r
= -1;
159 unsigned char *buf
= NULL
;
162 if (BN_num_bits(rsa
->n
) > OPENSSL_RSA_MAX_MODULUS_BITS
) {
163 RSAerror(RSA_R_MODULUS_TOO_LARGE
);
167 if (BN_ucmp(rsa
->n
, rsa
->e
) <= 0) {
168 RSAerror(RSA_R_BAD_E_VALUE
);
172 /* for large moduli, enforce exponent limit */
173 if (BN_num_bits(rsa
->n
) > OPENSSL_RSA_SMALL_MODULUS_BITS
) {
174 if (BN_num_bits(rsa
->e
) > OPENSSL_RSA_MAX_PUBEXP_BITS
) {
175 RSAerror(RSA_R_BAD_E_VALUE
);
180 if ((ctx
= BN_CTX_new()) == NULL
)
185 ret
= BN_CTX_get(ctx
);
186 num
= BN_num_bytes(rsa
->n
);
189 if (f
== NULL
|| ret
== NULL
|| buf
== NULL
) {
190 RSAerror(ERR_R_MALLOC_FAILURE
);
195 case RSA_PKCS1_PADDING
:
196 i
= RSA_padding_add_PKCS1_type_2(buf
, num
, from
, flen
);
198 #ifndef OPENSSL_NO_SHA
199 case RSA_PKCS1_OAEP_PADDING
:
200 i
= RSA_padding_add_PKCS1_OAEP(buf
, num
, from
, flen
, NULL
, 0);
204 i
= RSA_padding_add_none(buf
, num
, from
, flen
);
207 RSAerror(RSA_R_UNKNOWN_PADDING_TYPE
);
213 if (BN_bin2bn(buf
, num
, f
) == NULL
)
216 if (BN_ucmp(f
, rsa
->n
) >= 0) {
217 /* usually the padding functions would catch this */
218 RSAerror(RSA_R_DATA_TOO_LARGE_FOR_MODULUS
);
222 if (rsa
->flags
& RSA_FLAG_CACHE_PUBLIC
)
223 if (!BN_MONT_CTX_set_locked(&rsa
->_method_mod_n
,
224 CRYPTO_LOCK_RSA
, rsa
->n
, ctx
))
227 if (!rsa
->meth
->bn_mod_exp(ret
, f
, rsa
->e
, rsa
->n
, ctx
,
231 /* put in leading 0 bytes if the number is less than the
232 * length of the modulus */
233 j
= BN_num_bytes(ret
);
234 i
= BN_bn2bin(ret
, &(to
[num
- j
]));
235 for (k
= 0; k
< num
- i
; k
++)
249 rsa_get_blinding(RSA
*rsa
, int *local
, BN_CTX
*ctx
)
252 int got_write_lock
= 0;
255 CRYPTO_r_lock(CRYPTO_LOCK_RSA
);
257 if (rsa
->blinding
== NULL
) {
258 CRYPTO_r_unlock(CRYPTO_LOCK_RSA
);
259 CRYPTO_w_lock(CRYPTO_LOCK_RSA
);
262 if (rsa
->blinding
== NULL
)
263 rsa
->blinding
= RSA_setup_blinding(rsa
, ctx
);
270 CRYPTO_THREADID_current(&cur
);
271 if (!CRYPTO_THREADID_cmp(&cur
, BN_BLINDING_thread_id(ret
))) {
272 /* rsa->blinding is ours! */
275 /* resort to rsa->mt_blinding instead */
277 * Instruct rsa_blinding_convert(), rsa_blinding_invert()
278 * that the BN_BLINDING is shared, meaning that accesses
279 * require locks, and that the blinding factor must be
280 * stored outside the BN_BLINDING
284 if (rsa
->mt_blinding
== NULL
) {
285 if (!got_write_lock
) {
286 CRYPTO_r_unlock(CRYPTO_LOCK_RSA
);
287 CRYPTO_w_lock(CRYPTO_LOCK_RSA
);
291 if (rsa
->mt_blinding
== NULL
)
292 rsa
->mt_blinding
= RSA_setup_blinding(rsa
, ctx
);
294 ret
= rsa
->mt_blinding
;
299 CRYPTO_w_unlock(CRYPTO_LOCK_RSA
);
301 CRYPTO_r_unlock(CRYPTO_LOCK_RSA
);
306 rsa_blinding_convert(BN_BLINDING
*b
, BIGNUM
*f
, BIGNUM
*unblind
, BN_CTX
*ctx
)
310 * Local blinding: store the unblinding factor
313 return BN_BLINDING_convert_ex(f
, NULL
, b
, ctx
);
316 * Shared blinding: store the unblinding factor
317 * outside BN_BLINDING.
320 CRYPTO_w_lock(CRYPTO_LOCK_RSA_BLINDING
);
321 ret
= BN_BLINDING_convert_ex(f
, unblind
, b
, ctx
);
322 CRYPTO_w_unlock(CRYPTO_LOCK_RSA_BLINDING
);
328 rsa_blinding_invert(BN_BLINDING
*b
, BIGNUM
*f
, BIGNUM
*unblind
, BN_CTX
*ctx
)
331 * For local blinding, unblind is set to NULL, and BN_BLINDING_invert_ex
332 * will use the unblinding factor stored in BN_BLINDING.
333 * If BN_BLINDING is shared between threads, unblind must be non-null:
334 * BN_BLINDING_invert_ex will then use the local unblinding factor,
335 * and will only read the modulus from BN_BLINDING.
336 * In both cases it's safe to access the blinding without a lock.
338 return BN_BLINDING_invert_ex(f
, unblind
, b
, ctx
);
343 RSA_eay_private_encrypt(int flen
, const unsigned char *from
, unsigned char *to
,
344 RSA
*rsa
, int padding
)
346 BIGNUM
*f
, *ret
, *res
;
347 int i
, j
, k
, num
= 0, r
= -1;
348 unsigned char *buf
= NULL
;
350 int local_blinding
= 0;
352 * Used only if the blinding structure is shared. A non-NULL unblind
353 * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
354 * the unblinding factor outside the blinding structure.
356 BIGNUM
*unblind
= NULL
;
357 BN_BLINDING
*blinding
= NULL
;
359 if ((ctx
= BN_CTX_new()) == NULL
)
364 ret
= BN_CTX_get(ctx
);
365 num
= BN_num_bytes(rsa
->n
);
368 if (f
== NULL
|| ret
== NULL
|| buf
== NULL
) {
369 RSAerror(ERR_R_MALLOC_FAILURE
);
374 case RSA_PKCS1_PADDING
:
375 i
= RSA_padding_add_PKCS1_type_1(buf
, num
, from
, flen
);
377 case RSA_X931_PADDING
:
378 i
= RSA_padding_add_X931(buf
, num
, from
, flen
);
381 i
= RSA_padding_add_none(buf
, num
, from
, flen
);
384 RSAerror(RSA_R_UNKNOWN_PADDING_TYPE
);
390 if (BN_bin2bn(buf
, num
, f
) == NULL
)
393 if (BN_ucmp(f
, rsa
->n
) >= 0) {
394 /* usually the padding functions would catch this */
395 RSAerror(RSA_R_DATA_TOO_LARGE_FOR_MODULUS
);
399 if (!(rsa
->flags
& RSA_FLAG_NO_BLINDING
)) {
400 blinding
= rsa_get_blinding(rsa
, &local_blinding
, ctx
);
401 if (blinding
== NULL
) {
402 RSAerror(ERR_R_INTERNAL_ERROR
);
407 if (blinding
!= NULL
) {
408 if (!local_blinding
&& ((unblind
= BN_CTX_get(ctx
)) == NULL
)) {
409 RSAerror(ERR_R_MALLOC_FAILURE
);
412 if (!rsa_blinding_convert(blinding
, f
, unblind
, ctx
))
416 if ((rsa
->flags
& RSA_FLAG_EXT_PKEY
) ||
417 (rsa
->p
!= NULL
&& rsa
->q
!= NULL
&& rsa
->dmp1
!= NULL
&&
418 rsa
->dmq1
!= NULL
&& rsa
->iqmp
!= NULL
)) {
419 if (!rsa
->meth
->rsa_mod_exp(ret
, f
, rsa
, ctx
))
425 BN_with_flags(&d
, rsa
->d
, BN_FLG_CONSTTIME
);
427 if (rsa
->flags
& RSA_FLAG_CACHE_PUBLIC
)
428 if (!BN_MONT_CTX_set_locked(&rsa
->_method_mod_n
,
429 CRYPTO_LOCK_RSA
, rsa
->n
, ctx
))
432 if (!rsa
->meth
->bn_mod_exp(ret
, f
, &d
, rsa
->n
, ctx
,
433 rsa
->_method_mod_n
)) {
439 if (!rsa_blinding_invert(blinding
, ret
, unblind
, ctx
))
442 if (padding
== RSA_X931_PADDING
) {
443 BN_sub(f
, rsa
->n
, ret
);
444 if (BN_cmp(ret
, f
) > 0)
451 /* put in leading 0 bytes if the number is less than the
452 * length of the modulus */
453 j
= BN_num_bytes(res
);
454 i
= BN_bn2bin(res
, &(to
[num
- j
]));
455 for (k
= 0; k
< num
- i
; k
++)
469 RSA_eay_private_decrypt(int flen
, const unsigned char *from
, unsigned char *to
,
470 RSA
*rsa
, int padding
)
473 int j
, num
= 0, r
= -1;
475 unsigned char *buf
= NULL
;
477 int local_blinding
= 0;
479 * Used only if the blinding structure is shared. A non-NULL unblind
480 * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
481 * the unblinding factor outside the blinding structure.
483 BIGNUM
*unblind
= NULL
;
484 BN_BLINDING
*blinding
= NULL
;
486 if ((ctx
= BN_CTX_new()) == NULL
)
491 ret
= BN_CTX_get(ctx
);
492 num
= BN_num_bytes(rsa
->n
);
495 if (!f
|| !ret
|| !buf
) {
496 RSAerror(ERR_R_MALLOC_FAILURE
);
500 /* This check was for equality but PGP does evil things
501 * and chops off the top '0' bytes */
503 RSAerror(RSA_R_DATA_GREATER_THAN_MOD_LEN
);
507 /* make data into a big number */
508 if (BN_bin2bn(from
, (int)flen
, f
) == NULL
)
511 if (BN_ucmp(f
, rsa
->n
) >= 0) {
512 RSAerror(RSA_R_DATA_TOO_LARGE_FOR_MODULUS
);
516 if (!(rsa
->flags
& RSA_FLAG_NO_BLINDING
)) {
517 blinding
= rsa_get_blinding(rsa
, &local_blinding
, ctx
);
518 if (blinding
== NULL
) {
519 RSAerror(ERR_R_INTERNAL_ERROR
);
524 if (blinding
!= NULL
) {
525 if (!local_blinding
&& ((unblind
= BN_CTX_get(ctx
)) == NULL
)) {
526 RSAerror(ERR_R_MALLOC_FAILURE
);
529 if (!rsa_blinding_convert(blinding
, f
, unblind
, ctx
))
534 if ((rsa
->flags
& RSA_FLAG_EXT_PKEY
) ||
535 (rsa
->p
!= NULL
&& rsa
->q
!= NULL
&& rsa
->dmp1
!= NULL
&&
536 rsa
->dmq1
!= NULL
&& rsa
->iqmp
!= NULL
)) {
537 if (!rsa
->meth
->rsa_mod_exp(ret
, f
, rsa
, ctx
))
543 BN_with_flags(&d
, rsa
->d
, BN_FLG_CONSTTIME
);
545 if (rsa
->flags
& RSA_FLAG_CACHE_PUBLIC
)
546 if (!BN_MONT_CTX_set_locked(&rsa
->_method_mod_n
,
547 CRYPTO_LOCK_RSA
, rsa
->n
, ctx
))
550 if (!rsa
->meth
->bn_mod_exp(ret
, f
, &d
, rsa
->n
, ctx
,
551 rsa
->_method_mod_n
)) {
557 if (!rsa_blinding_invert(blinding
, ret
, unblind
, ctx
))
561 j
= BN_bn2bin(ret
, p
); /* j is only used with no-padding mode */
564 case RSA_PKCS1_PADDING
:
565 r
= RSA_padding_check_PKCS1_type_2(to
, num
, buf
, j
, num
);
567 #ifndef OPENSSL_NO_SHA
568 case RSA_PKCS1_OAEP_PADDING
:
569 r
= RSA_padding_check_PKCS1_OAEP(to
, num
, buf
, j
, num
, NULL
, 0);
573 r
= RSA_padding_check_none(to
, num
, buf
, j
, num
);
576 RSAerror(RSA_R_UNKNOWN_PADDING_TYPE
);
580 RSAerror(RSA_R_PADDING_CHECK_FAILED
);
591 /* signature verification */
593 RSA_eay_public_decrypt(int flen
, const unsigned char *from
, unsigned char *to
,
594 RSA
*rsa
, int padding
)
597 int i
, num
= 0, r
= -1;
599 unsigned char *buf
= NULL
;
602 if (BN_num_bits(rsa
->n
) > OPENSSL_RSA_MAX_MODULUS_BITS
) {
603 RSAerror(RSA_R_MODULUS_TOO_LARGE
);
607 if (BN_ucmp(rsa
->n
, rsa
->e
) <= 0) {
608 RSAerror(RSA_R_BAD_E_VALUE
);
612 /* for large moduli, enforce exponent limit */
613 if (BN_num_bits(rsa
->n
) > OPENSSL_RSA_SMALL_MODULUS_BITS
) {
614 if (BN_num_bits(rsa
->e
) > OPENSSL_RSA_MAX_PUBEXP_BITS
) {
615 RSAerror(RSA_R_BAD_E_VALUE
);
620 if ((ctx
= BN_CTX_new()) == NULL
)
625 ret
= BN_CTX_get(ctx
);
626 num
= BN_num_bytes(rsa
->n
);
629 if (!f
|| !ret
|| !buf
) {
630 RSAerror(ERR_R_MALLOC_FAILURE
);
634 /* This check was for equality but PGP does evil things
635 * and chops off the top '0' bytes */
637 RSAerror(RSA_R_DATA_GREATER_THAN_MOD_LEN
);
641 if (BN_bin2bn(from
, flen
, f
) == NULL
)
644 if (BN_ucmp(f
, rsa
->n
) >= 0) {
645 RSAerror(RSA_R_DATA_TOO_LARGE_FOR_MODULUS
);
649 if (rsa
->flags
& RSA_FLAG_CACHE_PUBLIC
)
650 if (!BN_MONT_CTX_set_locked(&rsa
->_method_mod_n
,
651 CRYPTO_LOCK_RSA
, rsa
->n
, ctx
))
654 if (!rsa
->meth
->bn_mod_exp(ret
, f
, rsa
->e
, rsa
->n
, ctx
,
658 if (padding
== RSA_X931_PADDING
&& (ret
->d
[0] & 0xf) != 12)
659 if (!BN_sub(ret
, rsa
->n
, ret
))
663 i
= BN_bn2bin(ret
, p
);
666 case RSA_PKCS1_PADDING
:
667 r
= RSA_padding_check_PKCS1_type_1(to
, num
, buf
, i
, num
);
669 case RSA_X931_PADDING
:
670 r
= RSA_padding_check_X931(to
, num
, buf
, i
, num
);
673 r
= RSA_padding_check_none(to
, num
, buf
, i
, num
);
676 RSAerror(RSA_R_UNKNOWN_PADDING_TYPE
);
680 RSAerror(RSA_R_PADDING_CHECK_FAILED
);
692 RSA_eay_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
, RSA
*rsa
, BN_CTX
*ctx
)
694 BIGNUM
*r1
, *m1
, *vrfy
;
695 BIGNUM dmp1
, dmq1
, c
, pr1
;
699 r1
= BN_CTX_get(ctx
);
700 m1
= BN_CTX_get(ctx
);
701 vrfy
= BN_CTX_get(ctx
);
702 if (r1
== NULL
|| m1
== NULL
|| vrfy
== NULL
) {
703 RSAerror(ERR_R_MALLOC_FAILURE
);
711 * Make sure BN_mod_inverse in Montgomery intialization uses the
712 * BN_FLG_CONSTTIME flag
716 BN_with_flags(&p
, rsa
->p
, BN_FLG_CONSTTIME
);
717 BN_with_flags(&q
, rsa
->q
, BN_FLG_CONSTTIME
);
719 if (rsa
->flags
& RSA_FLAG_CACHE_PRIVATE
) {
720 if (!BN_MONT_CTX_set_locked(&rsa
->_method_mod_p
,
721 CRYPTO_LOCK_RSA
, &p
, ctx
) ||
722 !BN_MONT_CTX_set_locked(&rsa
->_method_mod_q
,
723 CRYPTO_LOCK_RSA
, &q
, ctx
)) {
729 if (rsa
->flags
& RSA_FLAG_CACHE_PUBLIC
)
730 if (!BN_MONT_CTX_set_locked(&rsa
->_method_mod_n
,
731 CRYPTO_LOCK_RSA
, rsa
->n
, ctx
))
734 /* compute I mod q */
736 BN_with_flags(&c
, I
, BN_FLG_CONSTTIME
);
738 if (!BN_mod_ct(r1
, &c
, rsa
->q
, ctx
))
741 /* compute r1^dmq1 mod q */
743 BN_with_flags(&dmq1
, rsa
->dmq1
, BN_FLG_CONSTTIME
);
745 if (!rsa
->meth
->bn_mod_exp(m1
, r1
, &dmq1
, rsa
->q
, ctx
,
749 /* compute I mod p */
750 BN_with_flags(&c
, I
, BN_FLG_CONSTTIME
);
752 if (!BN_mod_ct(r1
, &c
, rsa
->p
, ctx
))
755 /* compute r1^dmp1 mod p */
757 BN_with_flags(&dmp1
, rsa
->dmp1
, BN_FLG_CONSTTIME
);
759 if (!rsa
->meth
->bn_mod_exp(r0
, r1
, &dmp1
, rsa
->p
, ctx
,
763 if (!BN_sub(r0
, r0
, m1
))
767 * This will help stop the size of r0 increasing, which does
768 * affect the multiply if it optimised for a power of 2 size
770 if (BN_is_negative(r0
))
771 if (!BN_add(r0
, r0
, rsa
->p
))
774 if (!BN_mul(r1
, r0
, rsa
->iqmp
, ctx
))
777 /* Turn BN_FLG_CONSTTIME flag on before division operation */
779 BN_with_flags(&pr1
, r1
, BN_FLG_CONSTTIME
);
781 if (!BN_mod_ct(r0
, &pr1
, rsa
->p
, ctx
))
785 * If p < q it is occasionally possible for the correction of
786 * adding 'p' if r0 is negative above to leave the result still
787 * negative. This can break the private key operations: the following
788 * second correction should *always* correct this rare occurrence.
789 * This will *never* happen with OpenSSL generated keys because
790 * they ensure p > q [steve]
792 if (BN_is_negative(r0
))
793 if (!BN_add(r0
, r0
, rsa
->p
))
795 if (!BN_mul(r1
, r0
, rsa
->q
, ctx
))
797 if (!BN_add(r0
, r1
, m1
))
800 if (rsa
->e
&& rsa
->n
) {
801 if (!rsa
->meth
->bn_mod_exp(vrfy
, r0
, rsa
->e
, rsa
->n
, ctx
,
805 * If 'I' was greater than (or equal to) rsa->n, the operation
806 * will be equivalent to using 'I mod n'. However, the result of
807 * the verify will *always* be less than 'n' so we don't check
808 * for absolute equality, just congruency.
810 if (!BN_sub(vrfy
, vrfy
, I
))
812 if (!BN_mod_ct(vrfy
, vrfy
, rsa
->n
, ctx
))
814 if (BN_is_negative(vrfy
))
815 if (!BN_add(vrfy
, vrfy
, rsa
->n
))
817 if (!BN_is_zero(vrfy
)) {
819 * 'I' and 'vrfy' aren't congruent mod n. Don't leak
820 * miscalculated CRT output, just do a raw (slower)
821 * mod_exp and return that instead.
826 BN_with_flags(&d
, rsa
->d
, BN_FLG_CONSTTIME
);
828 if (!rsa
->meth
->bn_mod_exp(r0
, I
, &d
, rsa
->n
, ctx
,
829 rsa
->_method_mod_n
)) {
841 RSA_eay_init(RSA
*rsa
)
843 rsa
->flags
|= RSA_FLAG_CACHE_PUBLIC
| RSA_FLAG_CACHE_PRIVATE
;
848 RSA_eay_finish(RSA
*rsa
)
850 BN_MONT_CTX_free(rsa
->_method_mod_n
);
851 BN_MONT_CTX_free(rsa
->_method_mod_p
);
852 BN_MONT_CTX_free(rsa
->_method_mod_q
);