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
);
433 if (rsa
->flags
& RSA_FLAG_CACHE_PUBLIC
)
434 if (!BN_MONT_CTX_set_locked
435 (&rsa
->_method_mod_n
, CRYPTO_LOCK_RSA
, rsa
->n
, ctx
)) {
440 if (!rsa
->meth
->bn_mod_exp(ret
, f
, d
, rsa
->n
, ctx
,
441 rsa
->_method_mod_n
)) {
445 /* We MUST free local_d before any further use of rsa->d */
450 if (!rsa_blinding_invert(blinding
, ret
, unblind
, ctx
))
453 if (padding
== RSA_X931_PADDING
) {
454 BN_sub(f
, rsa
->n
, ret
);
455 if (BN_cmp(ret
, f
) > 0)
463 * put in leading 0 bytes if the number is less than the length of the
466 j
= BN_num_bytes(res
);
467 i
= BN_bn2bin(res
, &(to
[num
- j
]));
468 for (k
= 0; k
< (num
- i
); k
++)
476 OPENSSL_clear_free(buf
, num
);
480 static int rsa_ossl_private_decrypt(int flen
, const unsigned char *from
,
481 unsigned char *to
, RSA
*rsa
, int padding
)
484 int j
, num
= 0, r
= -1;
486 unsigned char *buf
= NULL
;
488 int local_blinding
= 0;
490 * Used only if the blinding structure is shared. A non-NULL unblind
491 * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
492 * the unblinding factor outside the blinding structure.
494 BIGNUM
*unblind
= NULL
;
495 BN_BLINDING
*blinding
= NULL
;
497 if ((ctx
= BN_CTX_new()) == NULL
)
501 ret
= BN_CTX_get(ctx
);
502 num
= BN_num_bytes(rsa
->n
);
503 buf
= OPENSSL_malloc(num
);
504 if (f
== NULL
|| ret
== NULL
|| buf
== NULL
) {
505 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT
, ERR_R_MALLOC_FAILURE
);
510 * This check was for equality but PGP does evil things and chops off the
514 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT
,
515 RSA_R_DATA_GREATER_THAN_MOD_LEN
);
519 /* make data into a big number */
520 if (BN_bin2bn(from
, (int)flen
, f
) == NULL
)
523 if (BN_ucmp(f
, rsa
->n
) >= 0) {
524 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT
,
525 RSA_R_DATA_TOO_LARGE_FOR_MODULUS
);
529 if (!(rsa
->flags
& RSA_FLAG_NO_BLINDING
)) {
530 blinding
= rsa_get_blinding(rsa
, &local_blinding
, ctx
);
531 if (blinding
== NULL
) {
532 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT
, ERR_R_INTERNAL_ERROR
);
537 if (blinding
!= NULL
) {
538 if (!local_blinding
&& ((unblind
= BN_CTX_get(ctx
)) == NULL
)) {
539 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT
, ERR_R_MALLOC_FAILURE
);
542 if (!rsa_blinding_convert(blinding
, f
, unblind
, ctx
))
547 if ((rsa
->flags
& RSA_FLAG_EXT_PKEY
) ||
550 (rsa
->dmp1
!= NULL
) && (rsa
->dmq1
!= NULL
) && (rsa
->iqmp
!= NULL
))) {
551 if (!rsa
->meth
->rsa_mod_exp(ret
, f
, rsa
, ctx
))
554 BIGNUM
*d
= NULL
, *local_d
= NULL
;
556 if (!(rsa
->flags
& RSA_FLAG_NO_CONSTTIME
)) {
557 local_d
= d
= BN_new();
559 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT
, ERR_R_MALLOC_FAILURE
);
562 BN_with_flags(d
, rsa
->d
, BN_FLG_CONSTTIME
);
567 if (rsa
->flags
& RSA_FLAG_CACHE_PUBLIC
)
568 if (!BN_MONT_CTX_set_locked
569 (&rsa
->_method_mod_n
, CRYPTO_LOCK_RSA
, rsa
->n
, ctx
)) {
573 if (!rsa
->meth
->bn_mod_exp(ret
, f
, d
, rsa
->n
, ctx
,
574 rsa
->_method_mod_n
)) {
578 /* We MUST free local_d before any further use of rsa->d */
583 if (!rsa_blinding_invert(blinding
, ret
, unblind
, ctx
))
587 j
= BN_bn2bin(ret
, p
); /* j is only used with no-padding mode */
590 case RSA_PKCS1_PADDING
:
591 r
= RSA_padding_check_PKCS1_type_2(to
, num
, buf
, j
, num
);
593 case RSA_PKCS1_OAEP_PADDING
:
594 r
= RSA_padding_check_PKCS1_OAEP(to
, num
, buf
, j
, num
, NULL
, 0);
596 case RSA_SSLV23_PADDING
:
597 r
= RSA_padding_check_SSLv23(to
, num
, buf
, j
, num
);
600 r
= RSA_padding_check_none(to
, num
, buf
, j
, num
);
603 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT
, RSA_R_UNKNOWN_PADDING_TYPE
);
607 RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT
, RSA_R_PADDING_CHECK_FAILED
);
613 OPENSSL_clear_free(buf
, num
);
617 /* signature verification */
618 static int rsa_ossl_public_decrypt(int flen
, const unsigned char *from
,
619 unsigned char *to
, RSA
*rsa
, int padding
)
622 int i
, num
= 0, r
= -1;
624 unsigned char *buf
= NULL
;
627 if (BN_num_bits(rsa
->n
) > OPENSSL_RSA_MAX_MODULUS_BITS
) {
628 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT
, RSA_R_MODULUS_TOO_LARGE
);
632 if (BN_ucmp(rsa
->n
, rsa
->e
) <= 0) {
633 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT
, RSA_R_BAD_E_VALUE
);
637 /* for large moduli, enforce exponent limit */
638 if (BN_num_bits(rsa
->n
) > OPENSSL_RSA_SMALL_MODULUS_BITS
) {
639 if (BN_num_bits(rsa
->e
) > OPENSSL_RSA_MAX_PUBEXP_BITS
) {
640 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT
, RSA_R_BAD_E_VALUE
);
645 if ((ctx
= BN_CTX_new()) == NULL
)
649 ret
= BN_CTX_get(ctx
);
650 num
= BN_num_bytes(rsa
->n
);
651 buf
= OPENSSL_malloc(num
);
652 if (f
== NULL
|| ret
== NULL
|| buf
== NULL
) {
653 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT
, ERR_R_MALLOC_FAILURE
);
658 * This check was for equality but PGP does evil things and chops off the
662 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT
, RSA_R_DATA_GREATER_THAN_MOD_LEN
);
666 if (BN_bin2bn(from
, flen
, f
) == NULL
)
669 if (BN_ucmp(f
, rsa
->n
) >= 0) {
670 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT
,
671 RSA_R_DATA_TOO_LARGE_FOR_MODULUS
);
675 if (rsa
->flags
& RSA_FLAG_CACHE_PUBLIC
)
676 if (!BN_MONT_CTX_set_locked
677 (&rsa
->_method_mod_n
, CRYPTO_LOCK_RSA
, rsa
->n
, ctx
))
680 if (!rsa
->meth
->bn_mod_exp(ret
, f
, rsa
->e
, rsa
->n
, ctx
,
684 if ((padding
== RSA_X931_PADDING
) && ((bn_get_words(ret
)[0] & 0xf) != 12))
685 if (!BN_sub(ret
, rsa
->n
, ret
))
689 i
= BN_bn2bin(ret
, p
);
692 case RSA_PKCS1_PADDING
:
693 r
= RSA_padding_check_PKCS1_type_1(to
, num
, buf
, i
, num
);
695 case RSA_X931_PADDING
:
696 r
= RSA_padding_check_X931(to
, num
, buf
, i
, num
);
699 r
= RSA_padding_check_none(to
, num
, buf
, i
, num
);
702 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT
, RSA_R_UNKNOWN_PADDING_TYPE
);
706 RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT
, RSA_R_PADDING_CHECK_FAILED
);
712 OPENSSL_clear_free(buf
, num
);
716 static int rsa_ossl_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
, RSA
*rsa
, BN_CTX
*ctx
)
718 BIGNUM
*r1
, *m1
, *vrfy
;
723 r1
= BN_CTX_get(ctx
);
724 m1
= BN_CTX_get(ctx
);
725 vrfy
= BN_CTX_get(ctx
);
728 BIGNUM
*local_p
= NULL
, *local_q
= NULL
;
729 BIGNUM
*p
= NULL
, *q
= NULL
;
732 * Make sure BN_mod_inverse in Montgomery intialization uses the
733 * BN_FLG_CONSTTIME flag (unless RSA_FLAG_NO_CONSTTIME is set)
735 if (!(rsa
->flags
& RSA_FLAG_NO_CONSTTIME
)) {
736 local_p
= p
= BN_new();
739 BN_with_flags(p
, rsa
->p
, BN_FLG_CONSTTIME
);
741 local_q
= q
= BN_new();
746 BN_with_flags(q
, rsa
->q
, BN_FLG_CONSTTIME
);
752 if (rsa
->flags
& RSA_FLAG_CACHE_PRIVATE
) {
753 if (!BN_MONT_CTX_set_locked
754 (&rsa
->_method_mod_p
, CRYPTO_LOCK_RSA
, p
, ctx
)
755 || !BN_MONT_CTX_set_locked(&rsa
->_method_mod_q
,
756 CRYPTO_LOCK_RSA
, q
, ctx
)) {
763 * We MUST free local_p and local_q before any further use of rsa->p and
770 if (rsa
->flags
& RSA_FLAG_CACHE_PUBLIC
)
771 if (!BN_MONT_CTX_set_locked
772 (&rsa
->_method_mod_n
, CRYPTO_LOCK_RSA
, rsa
->n
, ctx
))
775 /* compute I mod q */
777 BIGNUM
*local_c
= NULL
;
779 if (!(rsa
->flags
& RSA_FLAG_NO_CONSTTIME
)) {
783 BN_with_flags(local_c
, I
, BN_FLG_CONSTTIME
);
788 if (!BN_mod(r1
, c
, rsa
->q
, ctx
)) {
794 BIGNUM
*local_dmq1
= NULL
, *dmq1
;
795 /* compute r1^dmq1 mod q */
796 if (!(rsa
->flags
& RSA_FLAG_NO_CONSTTIME
)) {
797 dmq1
= local_dmq1
= BN_new();
798 if (local_dmq1
== NULL
) {
802 BN_with_flags(dmq1
, rsa
->dmq1
, BN_FLG_CONSTTIME
);
806 if (!rsa
->meth
->bn_mod_exp(m1
, r1
, dmq1
, rsa
->q
, ctx
,
807 rsa
->_method_mod_q
)) {
812 /* We MUST free local_dmq1 before any further use of rsa->dmq1 */
816 /* compute I mod p */
817 if (!BN_mod(r1
, c
, rsa
->p
, ctx
)) {
821 /* We MUST free local_c before any further use of I */
826 BIGNUM
*local_dmp1
= NULL
, *dmp1
;
827 /* compute r1^dmp1 mod p */
828 if (!(rsa
->flags
& RSA_FLAG_NO_CONSTTIME
)) {
829 dmp1
= local_dmp1
= BN_new();
830 if (local_dmp1
== NULL
)
832 BN_with_flags(dmp1
, rsa
->dmp1
, BN_FLG_CONSTTIME
);
836 if (!rsa
->meth
->bn_mod_exp(r0
, r1
, dmp1
, rsa
->p
, ctx
,
837 rsa
->_method_mod_p
)) {
841 /* We MUST free local_dmp1 before any further use of rsa->dmp1 */
845 if (!BN_sub(r0
, r0
, m1
))
848 * This will help stop the size of r0 increasing, which does affect the
849 * multiply if it optimised for a power of 2 size
851 if (BN_is_negative(r0
))
852 if (!BN_add(r0
, r0
, rsa
->p
))
855 if (!BN_mul(r1
, r0
, rsa
->iqmp
, ctx
))
859 BIGNUM
*local_r1
= NULL
, *pr1
;
860 /* Turn BN_FLG_CONSTTIME flag on before division operation */
861 if (!(rsa
->flags
& RSA_FLAG_NO_CONSTTIME
)) {
862 pr1
= local_r1
= BN_new();
863 if (local_r1
== NULL
)
865 BN_with_flags(pr1
, r1
, BN_FLG_CONSTTIME
);
869 if (!BN_mod(r0
, pr1
, rsa
->p
, ctx
)) {
873 /* We MUST free local_r1 before any further use of r1 */
878 * If p < q it is occasionally possible for the correction of adding 'p'
879 * if r0 is negative above to leave the result still negative. This can
880 * break the private key operations: the following second correction
881 * should *always* correct this rare occurrence. This will *never* happen
882 * with OpenSSL generated keys because they ensure p > q [steve]
884 if (BN_is_negative(r0
))
885 if (!BN_add(r0
, r0
, rsa
->p
))
887 if (!BN_mul(r1
, r0
, rsa
->q
, ctx
))
889 if (!BN_add(r0
, r1
, m1
))
892 if (rsa
->e
&& rsa
->n
) {
893 if (!rsa
->meth
->bn_mod_exp(vrfy
, r0
, rsa
->e
, rsa
->n
, ctx
,
897 * If 'I' was greater than (or equal to) rsa->n, the operation will
898 * be equivalent to using 'I mod n'. However, the result of the
899 * verify will *always* be less than 'n' so we don't check for
900 * absolute equality, just congruency.
902 if (!BN_sub(vrfy
, vrfy
, I
))
904 if (!BN_mod(vrfy
, vrfy
, rsa
->n
, ctx
))
906 if (BN_is_negative(vrfy
))
907 if (!BN_add(vrfy
, vrfy
, rsa
->n
))
909 if (!BN_is_zero(vrfy
)) {
911 * 'I' and 'vrfy' aren't congruent mod n. Don't leak
912 * miscalculated CRT output, just do a raw (slower) mod_exp and
913 * return that instead.
916 BIGNUM
*local_d
= NULL
;
919 if (!(rsa
->flags
& RSA_FLAG_NO_CONSTTIME
)) {
920 local_d
= d
= BN_new();
923 BN_with_flags(d
, rsa
->d
, BN_FLG_CONSTTIME
);
927 if (!rsa
->meth
->bn_mod_exp(r0
, I
, d
, rsa
->n
, ctx
,
928 rsa
->_method_mod_n
)) {
932 /* We MUST free local_d before any further use of rsa->d */
942 static int rsa_ossl_init(RSA
*rsa
)
944 rsa
->flags
|= RSA_FLAG_CACHE_PUBLIC
| RSA_FLAG_CACHE_PRIVATE
;
948 static int rsa_ossl_finish(RSA
*rsa
)
950 BN_MONT_CTX_free(rsa
->_method_mod_n
);
951 BN_MONT_CTX_free(rsa
->_method_mod_p
);
952 BN_MONT_CTX_free(rsa
->_method_mod_q
);