1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
57 /* ====================================================================
58 * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
86 * 6. Redistributions of any form whatsoever must retain the following
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
111 #include "internal/cryptlib.h"
112 #include "internal/bn_int.h"
113 #include <openssl/rsa.h>
114 #include <openssl/rand.h>
118 static int rsa_ossl_public_encrypt(int flen
, const unsigned char *from
,
119 unsigned char *to
, RSA
*rsa
, int padding
);
120 static int rsa_ossl_private_encrypt(int flen
, const unsigned char *from
,
121 unsigned char *to
, RSA
*rsa
, int padding
);
122 static int rsa_ossl_public_decrypt(int flen
, const unsigned char *from
,
123 unsigned char *to
, RSA
*rsa
, int padding
);
124 static int rsa_ossl_private_decrypt(int flen
, const unsigned char *from
,
125 unsigned char *to
, RSA
*rsa
, int padding
);
126 static int rsa_ossl_mod_exp(BIGNUM
*r0
, const BIGNUM
*i
, RSA
*rsa
,
128 static int rsa_ossl_init(RSA
*rsa
);
129 static int rsa_ossl_finish(RSA
*rsa
);
130 static RSA_METHOD rsa_pkcs1_ossl_meth
= {
131 "OpenSSL PKCS#1 RSA (from Eric Young)",
132 rsa_ossl_public_encrypt
,
133 rsa_ossl_public_decrypt
, /* signature verification */
134 rsa_ossl_private_encrypt
, /* signing */
135 rsa_ossl_private_decrypt
,
137 BN_mod_exp_mont
, /* XXX probably we should not use Montgomery
141 RSA_FLAG_FIPS_METHOD
, /* flags */
145 NULL
/* rsa_keygen */
148 const RSA_METHOD
*RSA_PKCS1_OpenSSL(void)
150 return &rsa_pkcs1_ossl_meth
;
153 static int rsa_ossl_public_encrypt(int flen
, const unsigned char *from
,
154 unsigned char *to
, RSA
*rsa
, int padding
)
157 int i
, j
, k
, num
= 0, r
= -1;
158 unsigned char *buf
= NULL
;
161 if (BN_num_bits(rsa
->n
) > OPENSSL_RSA_MAX_MODULUS_BITS
) {
162 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT
, RSA_R_MODULUS_TOO_LARGE
);
166 if (BN_ucmp(rsa
->n
, rsa
->e
) <= 0) {
167 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT
, RSA_R_BAD_E_VALUE
);
171 /* for large moduli, enforce exponent limit */
172 if (BN_num_bits(rsa
->n
) > OPENSSL_RSA_SMALL_MODULUS_BITS
) {
173 if (BN_num_bits(rsa
->e
) > OPENSSL_RSA_MAX_PUBEXP_BITS
) {
174 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT
, RSA_R_BAD_E_VALUE
);
179 if ((ctx
= BN_CTX_new()) == NULL
)
183 ret
= BN_CTX_get(ctx
);
184 num
= BN_num_bytes(rsa
->n
);
185 buf
= OPENSSL_malloc(num
);
186 if (f
== NULL
|| ret
== NULL
|| buf
== NULL
) {
187 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT
, ERR_R_MALLOC_FAILURE
);
192 case RSA_PKCS1_PADDING
:
193 i
= RSA_padding_add_PKCS1_type_2(buf
, num
, from
, flen
);
195 case RSA_PKCS1_OAEP_PADDING
:
196 i
= RSA_padding_add_PKCS1_OAEP(buf
, num
, from
, flen
, NULL
, 0);
198 case RSA_SSLV23_PADDING
:
199 i
= RSA_padding_add_SSLv23(buf
, num
, from
, flen
);
202 i
= RSA_padding_add_none(buf
, num
, from
, flen
);
205 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT
, RSA_R_UNKNOWN_PADDING_TYPE
);
211 if (BN_bin2bn(buf
, num
, f
) == NULL
)
214 if (BN_ucmp(f
, rsa
->n
) >= 0) {
215 /* usually the padding functions would catch this */
216 RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT
,
217 RSA_R_DATA_TOO_LARGE_FOR_MODULUS
);
221 if (rsa
->flags
& RSA_FLAG_CACHE_PUBLIC
)
222 if (!BN_MONT_CTX_set_locked
223 (&rsa
->_method_mod_n
, CRYPTO_LOCK_RSA
, rsa
->n
, ctx
))
226 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 length of the
234 j
= BN_num_bytes(ret
);
235 i
= BN_bn2bin(ret
, &(to
[num
- j
]));
236 for (k
= 0; k
< (num
- i
); k
++)
244 OPENSSL_clear_free(buf
, num
);
248 static BN_BLINDING
*rsa_get_blinding(RSA
*rsa
, int *local
, BN_CTX
*ctx
)
251 int got_write_lock
= 0;
254 CRYPTO_r_lock(CRYPTO_LOCK_RSA
);
256 if (rsa
->blinding
== NULL
) {
257 CRYPTO_r_unlock(CRYPTO_LOCK_RSA
);
258 CRYPTO_w_lock(CRYPTO_LOCK_RSA
);
261 if (rsa
->blinding
== NULL
)
262 rsa
->blinding
= RSA_setup_blinding(rsa
, ctx
);
269 CRYPTO_THREADID_current(&cur
);
270 if (!CRYPTO_THREADID_cmp(&cur
, BN_BLINDING_thread_id(ret
))) {
271 /* rsa->blinding is ours! */
275 /* resort to rsa->mt_blinding instead */
278 * instructs rsa_blinding_convert(), rsa_blinding_invert() that the
279 * BN_BLINDING is shared, meaning that accesses require locks, and
280 * that the blinding factor must be 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
);
305 static int rsa_blinding_convert(BN_BLINDING
*b
, BIGNUM
*f
, BIGNUM
*unblind
,
310 * Local blinding: store the unblinding factor in BN_BLINDING.
312 return BN_BLINDING_convert_ex(f
, NULL
, b
, ctx
);
315 * Shared blinding: store the unblinding factor outside BN_BLINDING.
318 CRYPTO_w_lock(CRYPTO_LOCK_RSA_BLINDING
);
319 ret
= BN_BLINDING_convert_ex(f
, unblind
, b
, ctx
);
320 CRYPTO_w_unlock(CRYPTO_LOCK_RSA_BLINDING
);
325 static int rsa_blinding_invert(BN_BLINDING
*b
, BIGNUM
*f
, BIGNUM
*unblind
,
329 * For local blinding, unblind is set to NULL, and BN_BLINDING_invert_ex
330 * will use the unblinding factor stored in BN_BLINDING. If BN_BLINDING
331 * is shared between threads, unblind must be non-null:
332 * BN_BLINDING_invert_ex will then use the local unblinding factor, and
333 * will only read the modulus from BN_BLINDING. In both cases it's safe
334 * to access the blinding without a lock.
336 return BN_BLINDING_invert_ex(f
, unblind
, b
, ctx
);
340 static int rsa_ossl_private_encrypt(int flen
, const unsigned char *from
,
341 unsigned char *to
, RSA
*rsa
, int padding
)
343 BIGNUM
*f
, *ret
, *res
;
344 int i
, j
, k
, num
= 0, r
= -1;
345 unsigned char *buf
= NULL
;
347 int local_blinding
= 0;
349 * Used only if the blinding structure is shared. A non-NULL unblind
350 * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
351 * the unblinding factor outside the blinding structure.
353 BIGNUM
*unblind
= NULL
;
354 BN_BLINDING
*blinding
= NULL
;
356 if ((ctx
= BN_CTX_new()) == NULL
)
360 ret
= BN_CTX_get(ctx
);
361 num
= BN_num_bytes(rsa
->n
);
362 buf
= OPENSSL_malloc(num
);
363 if (f
== NULL
|| ret
== NULL
|| buf
== NULL
) {
364 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT
, ERR_R_MALLOC_FAILURE
);
369 case RSA_PKCS1_PADDING
:
370 i
= RSA_padding_add_PKCS1_type_1(buf
, num
, from
, flen
);
372 case RSA_X931_PADDING
:
373 i
= RSA_padding_add_X931(buf
, num
, from
, flen
);
376 i
= RSA_padding_add_none(buf
, num
, from
, flen
);
378 case RSA_SSLV23_PADDING
:
380 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT
, RSA_R_UNKNOWN_PADDING_TYPE
);
386 if (BN_bin2bn(buf
, num
, f
) == NULL
)
389 if (BN_ucmp(f
, rsa
->n
) >= 0) {
390 /* usually the padding functions would catch this */
391 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT
,
392 RSA_R_DATA_TOO_LARGE_FOR_MODULUS
);
396 if (!(rsa
->flags
& RSA_FLAG_NO_BLINDING
)) {
397 blinding
= rsa_get_blinding(rsa
, &local_blinding
, ctx
);
398 if (blinding
== NULL
) {
399 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT
, ERR_R_INTERNAL_ERROR
);
404 if (blinding
!= NULL
) {
405 if (!local_blinding
&& ((unblind
= BN_CTX_get(ctx
)) == NULL
)) {
406 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT
, ERR_R_MALLOC_FAILURE
);
409 if (!rsa_blinding_convert(blinding
, f
, unblind
, ctx
))
413 if ((rsa
->flags
& RSA_FLAG_EXT_PKEY
) ||
416 (rsa
->dmp1
!= NULL
) && (rsa
->dmq1
!= NULL
) && (rsa
->iqmp
!= NULL
))) {
417 if (!rsa
->meth
->rsa_mod_exp(ret
, f
, rsa
, ctx
))
420 BIGNUM
*d
= NULL
, *local_d
= NULL
;
422 if (!(rsa
->flags
& RSA_FLAG_NO_CONSTTIME
)) {
423 local_d
= d
= BN_new();
425 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT
, ERR_R_MALLOC_FAILURE
);
428 BN_with_flags(d
, rsa
->d
, BN_FLG_CONSTTIME
);
432 if (rsa
->flags
& RSA_FLAG_CACHE_PUBLIC
)
433 if (!BN_MONT_CTX_set_locked
434 (&rsa
->_method_mod_n
, CRYPTO_LOCK_RSA
, rsa
->n
, ctx
)) {
439 if (!rsa
->meth
->bn_mod_exp(ret
, f
, d
, rsa
->n
, ctx
,
440 rsa
->_method_mod_n
)) {
448 if (!rsa_blinding_invert(blinding
, ret
, unblind
, ctx
))
451 if (padding
== RSA_X931_PADDING
) {
452 BN_sub(f
, rsa
->n
, ret
);
453 if (BN_cmp(ret
, f
) > 0)
461 * put in leading 0 bytes if the number is less than the length of the
464 j
= BN_num_bytes(res
);
465 i
= BN_bn2bin(res
, &(to
[num
- j
]));
466 for (k
= 0; k
< (num
- i
); k
++)
474 OPENSSL_clear_free(buf
, num
);
478 static int rsa_ossl_private_decrypt(int flen
, const unsigned char *from
,
479 unsigned char *to
, RSA
*rsa
, int padding
)
482 int j
, num
= 0, r
= -1;
484 unsigned char *buf
= NULL
;
486 int local_blinding
= 0;
488 * Used only if the blinding structure is shared. A non-NULL unblind
489 * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
490 * the unblinding factor outside the blinding structure.
492 BIGNUM
*unblind
= NULL
;
493 BN_BLINDING
*blinding
= NULL
;
495 if ((ctx
= BN_CTX_new()) == NULL
)
499 ret
= BN_CTX_get(ctx
);
500 num
= BN_num_bytes(rsa
->n
);
501 buf
= OPENSSL_malloc(num
);
502 if (f
== NULL
|| ret
== NULL
|| buf
== NULL
) {
503 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT
, ERR_R_MALLOC_FAILURE
);
508 * This check was for equality but PGP does evil things and chops off the
512 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT
,
513 RSA_R_DATA_GREATER_THAN_MOD_LEN
);
517 /* make data into a big number */
518 if (BN_bin2bn(from
, (int)flen
, f
) == NULL
)
521 if (BN_ucmp(f
, rsa
->n
) >= 0) {
522 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT
,
523 RSA_R_DATA_TOO_LARGE_FOR_MODULUS
);
527 if (!(rsa
->flags
& RSA_FLAG_NO_BLINDING
)) {
528 blinding
= rsa_get_blinding(rsa
, &local_blinding
, ctx
);
529 if (blinding
== NULL
) {
530 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT
, ERR_R_INTERNAL_ERROR
);
535 if (blinding
!= NULL
) {
536 if (!local_blinding
&& ((unblind
= BN_CTX_get(ctx
)) == NULL
)) {
537 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT
, ERR_R_MALLOC_FAILURE
);
540 if (!rsa_blinding_convert(blinding
, f
, unblind
, ctx
))
545 if ((rsa
->flags
& RSA_FLAG_EXT_PKEY
) ||
548 (rsa
->dmp1
!= NULL
) && (rsa
->dmq1
!= NULL
) && (rsa
->iqmp
!= NULL
))) {
549 if (!rsa
->meth
->rsa_mod_exp(ret
, f
, rsa
, ctx
))
552 BIGNUM
*d
= NULL
, *local_d
= NULL
;
554 if (!(rsa
->flags
& RSA_FLAG_NO_CONSTTIME
)) {
555 local_d
= d
= BN_new();
557 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT
, ERR_R_MALLOC_FAILURE
);
560 BN_with_flags(d
, rsa
->d
, BN_FLG_CONSTTIME
);
564 if (rsa
->flags
& RSA_FLAG_CACHE_PUBLIC
)
565 if (!BN_MONT_CTX_set_locked
566 (&rsa
->_method_mod_n
, CRYPTO_LOCK_RSA
, rsa
->n
, ctx
)) {
570 if (!rsa
->meth
->bn_mod_exp(ret
, f
, d
, rsa
->n
, ctx
,
571 rsa
->_method_mod_n
)) {
579 if (!rsa_blinding_invert(blinding
, ret
, unblind
, ctx
))
583 j
= BN_bn2bin(ret
, p
); /* j is only used with no-padding mode */
586 case RSA_PKCS1_PADDING
:
587 r
= RSA_padding_check_PKCS1_type_2(to
, num
, buf
, j
, num
);
589 case RSA_PKCS1_OAEP_PADDING
:
590 r
= RSA_padding_check_PKCS1_OAEP(to
, num
, buf
, j
, num
, NULL
, 0);
592 case RSA_SSLV23_PADDING
:
593 r
= RSA_padding_check_SSLv23(to
, num
, buf
, j
, num
);
596 r
= RSA_padding_check_none(to
, num
, buf
, j
, num
);
599 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT
, RSA_R_UNKNOWN_PADDING_TYPE
);
603 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT
, RSA_R_PADDING_CHECK_FAILED
);
609 OPENSSL_clear_free(buf
, num
);
613 /* signature verification */
614 static int rsa_ossl_public_decrypt(int flen
, const unsigned char *from
,
615 unsigned char *to
, RSA
*rsa
, int padding
)
618 int i
, num
= 0, r
= -1;
620 unsigned char *buf
= NULL
;
623 if (BN_num_bits(rsa
->n
) > OPENSSL_RSA_MAX_MODULUS_BITS
) {
624 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT
, RSA_R_MODULUS_TOO_LARGE
);
628 if (BN_ucmp(rsa
->n
, rsa
->e
) <= 0) {
629 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT
, RSA_R_BAD_E_VALUE
);
633 /* for large moduli, enforce exponent limit */
634 if (BN_num_bits(rsa
->n
) > OPENSSL_RSA_SMALL_MODULUS_BITS
) {
635 if (BN_num_bits(rsa
->e
) > OPENSSL_RSA_MAX_PUBEXP_BITS
) {
636 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT
, RSA_R_BAD_E_VALUE
);
641 if ((ctx
= BN_CTX_new()) == NULL
)
645 ret
= BN_CTX_get(ctx
);
646 num
= BN_num_bytes(rsa
->n
);
647 buf
= OPENSSL_malloc(num
);
648 if (f
== NULL
|| ret
== NULL
|| buf
== NULL
) {
649 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT
, ERR_R_MALLOC_FAILURE
);
654 * This check was for equality but PGP does evil things and chops off the
658 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT
, RSA_R_DATA_GREATER_THAN_MOD_LEN
);
662 if (BN_bin2bn(from
, flen
, f
) == NULL
)
665 if (BN_ucmp(f
, rsa
->n
) >= 0) {
666 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT
,
667 RSA_R_DATA_TOO_LARGE_FOR_MODULUS
);
671 if (rsa
->flags
& RSA_FLAG_CACHE_PUBLIC
)
672 if (!BN_MONT_CTX_set_locked
673 (&rsa
->_method_mod_n
, CRYPTO_LOCK_RSA
, rsa
->n
, ctx
))
676 if (!rsa
->meth
->bn_mod_exp(ret
, f
, rsa
->e
, rsa
->n
, ctx
,
680 if ((padding
== RSA_X931_PADDING
) && ((bn_get_words(ret
)[0] & 0xf) != 12))
681 if (!BN_sub(ret
, rsa
->n
, ret
))
685 i
= BN_bn2bin(ret
, p
);
688 case RSA_PKCS1_PADDING
:
689 r
= RSA_padding_check_PKCS1_type_1(to
, num
, buf
, i
, num
);
691 case RSA_X931_PADDING
:
692 r
= RSA_padding_check_X931(to
, num
, buf
, i
, num
);
695 r
= RSA_padding_check_none(to
, num
, buf
, i
, num
);
698 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT
, RSA_R_UNKNOWN_PADDING_TYPE
);
702 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT
, RSA_R_PADDING_CHECK_FAILED
);
708 OPENSSL_clear_free(buf
, num
);
712 static int rsa_ossl_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
, RSA
*rsa
, BN_CTX
*ctx
)
714 BIGNUM
*r1
, *m1
, *vrfy
;
715 BIGNUM
*local_dmp1
, *local_dmq1
, *local_c
, *local_r1
;
716 BIGNUM
*dmp1
, *dmq1
, *c
, *pr1
;
721 local_dmp1
= BN_new();
722 local_dmq1
= BN_new();
725 if (local_dmp1
== NULL
726 || local_dmq1
== NULL
|| local_c
== NULL
|| local_r1
== NULL
)
729 r1
= BN_CTX_get(ctx
);
730 m1
= BN_CTX_get(ctx
);
731 vrfy
= BN_CTX_get(ctx
);
734 BIGNUM
*local_p
= NULL
, *local_q
= NULL
;
735 BIGNUM
*p
= NULL
, *q
= NULL
;
738 * Make sure BN_mod_inverse in Montgomery intialization uses the
739 * BN_FLG_CONSTTIME flag (unless RSA_FLAG_NO_CONSTTIME is set)
741 if (!(rsa
->flags
& RSA_FLAG_NO_CONSTTIME
)) {
742 local_p
= p
= BN_new();
745 BN_with_flags(p
, rsa
->p
, BN_FLG_CONSTTIME
);
747 local_q
= q
= BN_new();
752 BN_with_flags(q
, rsa
->q
, BN_FLG_CONSTTIME
);
758 if (rsa
->flags
& RSA_FLAG_CACHE_PRIVATE
) {
759 if (!BN_MONT_CTX_set_locked
760 (&rsa
->_method_mod_p
, CRYPTO_LOCK_RSA
, p
, ctx
)
761 || !BN_MONT_CTX_set_locked(&rsa
->_method_mod_q
,
762 CRYPTO_LOCK_RSA
, q
, ctx
)) {
772 if (rsa
->flags
& RSA_FLAG_CACHE_PUBLIC
)
773 if (!BN_MONT_CTX_set_locked
774 (&rsa
->_method_mod_n
, CRYPTO_LOCK_RSA
, rsa
->n
, ctx
))
777 /* compute I mod q */
778 if (!(rsa
->flags
& RSA_FLAG_NO_CONSTTIME
)) {
780 BN_with_flags(c
, I
, BN_FLG_CONSTTIME
);
781 if (!BN_mod(r1
, c
, rsa
->q
, ctx
))
784 if (!BN_mod(r1
, I
, rsa
->q
, ctx
))
788 /* compute r1^dmq1 mod q */
789 if (!(rsa
->flags
& RSA_FLAG_NO_CONSTTIME
)) {
791 BN_with_flags(dmq1
, rsa
->dmq1
, BN_FLG_CONSTTIME
);
794 if (!rsa
->meth
->bn_mod_exp(m1
, r1
, dmq1
, rsa
->q
, ctx
, rsa
->_method_mod_q
))
797 /* compute I mod p */
798 if (!(rsa
->flags
& RSA_FLAG_NO_CONSTTIME
)) {
800 BN_with_flags(c
, I
, BN_FLG_CONSTTIME
);
801 if (!BN_mod(r1
, c
, rsa
->p
, ctx
))
804 if (!BN_mod(r1
, I
, rsa
->p
, ctx
))
808 /* compute r1^dmp1 mod p */
809 if (!(rsa
->flags
& RSA_FLAG_NO_CONSTTIME
)) {
811 BN_with_flags(dmp1
, rsa
->dmp1
, BN_FLG_CONSTTIME
);
814 if (!rsa
->meth
->bn_mod_exp(r0
, r1
, dmp1
, rsa
->p
, ctx
, rsa
->_method_mod_p
))
817 if (!BN_sub(r0
, r0
, m1
))
820 * This will help stop the size of r0 increasing, which does affect the
821 * multiply if it optimised for a power of 2 size
823 if (BN_is_negative(r0
))
824 if (!BN_add(r0
, r0
, rsa
->p
))
827 if (!BN_mul(r1
, r0
, rsa
->iqmp
, ctx
))
830 /* Turn BN_FLG_CONSTTIME flag on before division operation */
831 if (!(rsa
->flags
& RSA_FLAG_NO_CONSTTIME
)) {
833 BN_with_flags(pr1
, r1
, BN_FLG_CONSTTIME
);
836 if (!BN_mod(r0
, pr1
, rsa
->p
, ctx
))
840 * If p < q it is occasionally possible for the correction of adding 'p'
841 * if r0 is negative above to leave the result still negative. This can
842 * break the private key operations: the following second correction
843 * should *always* correct this rare occurrence. This will *never* happen
844 * with OpenSSL generated keys because they ensure p > q [steve]
846 if (BN_is_negative(r0
))
847 if (!BN_add(r0
, r0
, rsa
->p
))
849 if (!BN_mul(r1
, r0
, rsa
->q
, ctx
))
851 if (!BN_add(r0
, r1
, m1
))
854 if (rsa
->e
&& rsa
->n
) {
855 if (!rsa
->meth
->bn_mod_exp(vrfy
, r0
, rsa
->e
, rsa
->n
, ctx
,
859 * If 'I' was greater than (or equal to) rsa->n, the operation will
860 * be equivalent to using 'I mod n'. However, the result of the
861 * verify will *always* be less than 'n' so we don't check for
862 * absolute equality, just congruency.
864 if (!BN_sub(vrfy
, vrfy
, I
))
866 if (!BN_mod(vrfy
, vrfy
, rsa
->n
, ctx
))
868 if (BN_is_negative(vrfy
))
869 if (!BN_add(vrfy
, vrfy
, rsa
->n
))
871 if (!BN_is_zero(vrfy
)) {
873 * 'I' and 'vrfy' aren't congruent mod n. Don't leak
874 * miscalculated CRT output, just do a raw (slower) mod_exp and
875 * return that instead.
878 BIGNUM
*local_d
= NULL
;
881 if (!(rsa
->flags
& RSA_FLAG_NO_CONSTTIME
)) {
882 local_d
= d
= BN_new();
885 BN_with_flags(d
, rsa
->d
, BN_FLG_CONSTTIME
);
888 if (!rsa
->meth
->bn_mod_exp(r0
, I
, d
, rsa
->n
, ctx
,
889 rsa
->_method_mod_n
)) {
907 static int rsa_ossl_init(RSA
*rsa
)
909 rsa
->flags
|= RSA_FLAG_CACHE_PUBLIC
| RSA_FLAG_CACHE_PRIVATE
;
913 static int rsa_ossl_finish(RSA
*rsa
)
915 BN_MONT_CTX_free(rsa
->_method_mod_n
);
916 BN_MONT_CTX_free(rsa
->_method_mod_p
);
917 BN_MONT_CTX_free(rsa
->_method_mod_q
);