2 * Copyright 1995-2025 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 "crypto/sparse_array.h"
19 #include "rsa_local.h"
20 #include "internal/constant_time.h"
21 #if defined(OPENSSL_SYS_TANDEM)
22 # include "internal/tsan_assist.h"
23 # include "internal/threads_common.h"
25 #include <openssl/evp.h>
26 #include <openssl/sha.h>
27 #include <openssl/hmac.h>
29 DEFINE_SPARSE_ARRAY_OF(BN_BLINDING
);
31 static int rsa_ossl_public_encrypt(int flen
, const unsigned char *from
,
32 unsigned char *to
, RSA
*rsa
, int padding
);
33 static int rsa_ossl_private_encrypt(int flen
, const unsigned char *from
,
34 unsigned char *to
, RSA
*rsa
, int padding
);
35 static int rsa_ossl_public_decrypt(int flen
, const unsigned char *from
,
36 unsigned char *to
, RSA
*rsa
, int padding
);
37 static int rsa_ossl_private_decrypt(int flen
, const unsigned char *from
,
38 unsigned char *to
, RSA
*rsa
, int padding
);
39 static int rsa_ossl_mod_exp(BIGNUM
*r0
, const BIGNUM
*i
, RSA
*rsa
,
41 static int rsa_ossl_init(RSA
*rsa
);
42 static int rsa_ossl_finish(RSA
*rsa
);
44 static int rsa_ossl_s390x_mod_exp(BIGNUM
*r0
, const BIGNUM
*i
, RSA
*rsa
,
46 static RSA_METHOD rsa_pkcs1_ossl_meth
= {
48 rsa_ossl_public_encrypt
,
49 rsa_ossl_public_decrypt
, /* signature verification */
50 rsa_ossl_private_encrypt
, /* signing */
51 rsa_ossl_private_decrypt
,
52 rsa_ossl_s390x_mod_exp
,
56 RSA_FLAG_FIPS_METHOD
, /* flags */
60 NULL
, /* rsa_keygen */
61 NULL
/* rsa_multi_prime_keygen */
64 static RSA_METHOD rsa_pkcs1_ossl_meth
= {
66 rsa_ossl_public_encrypt
,
67 rsa_ossl_public_decrypt
, /* signature verification */
68 rsa_ossl_private_encrypt
, /* signing */
69 rsa_ossl_private_decrypt
,
71 BN_mod_exp_mont
, /* XXX probably we should not use Montgomery
75 RSA_FLAG_FIPS_METHOD
, /* flags */
79 NULL
, /* rsa_keygen */
80 NULL
/* rsa_multi_prime_keygen */
84 static const RSA_METHOD
*default_RSA_meth
= &rsa_pkcs1_ossl_meth
;
86 void RSA_set_default_method(const RSA_METHOD
*meth
)
88 default_RSA_meth
= meth
;
91 const RSA_METHOD
*RSA_get_default_method(void)
93 return default_RSA_meth
;
96 const RSA_METHOD
*RSA_PKCS1_OpenSSL(void)
98 return &rsa_pkcs1_ossl_meth
;
101 const RSA_METHOD
*RSA_null_method(void)
106 static int rsa_ossl_public_encrypt(int flen
, const unsigned char *from
,
107 unsigned char *to
, RSA
*rsa
, int padding
)
110 int i
, num
= 0, r
= -1;
111 unsigned char *buf
= NULL
;
114 if (BN_num_bits(rsa
->n
) > OPENSSL_RSA_MAX_MODULUS_BITS
) {
115 ERR_raise(ERR_LIB_RSA
, RSA_R_MODULUS_TOO_LARGE
);
119 if (BN_ucmp(rsa
->n
, rsa
->e
) <= 0) {
120 ERR_raise(ERR_LIB_RSA
, RSA_R_BAD_E_VALUE
);
124 /* for large moduli, enforce exponent limit */
125 if (BN_num_bits(rsa
->n
) > OPENSSL_RSA_SMALL_MODULUS_BITS
) {
126 if (BN_num_bits(rsa
->e
) > OPENSSL_RSA_MAX_PUBEXP_BITS
) {
127 ERR_raise(ERR_LIB_RSA
, RSA_R_BAD_E_VALUE
);
132 if ((ctx
= BN_CTX_new_ex(rsa
->libctx
)) == NULL
)
136 ret
= BN_CTX_get(ctx
);
137 num
= BN_num_bytes(rsa
->n
);
138 buf
= OPENSSL_malloc(num
);
139 if (ret
== NULL
|| buf
== NULL
)
143 case RSA_PKCS1_PADDING
:
144 i
= ossl_rsa_padding_add_PKCS1_type_2_ex(rsa
->libctx
, buf
, num
,
147 case RSA_PKCS1_OAEP_PADDING
:
148 i
= ossl_rsa_padding_add_PKCS1_OAEP_mgf1_ex(rsa
->libctx
, buf
, num
,
153 i
= RSA_padding_add_none(buf
, num
, from
, flen
);
156 ERR_raise(ERR_LIB_RSA
, RSA_R_UNKNOWN_PADDING_TYPE
);
162 if (BN_bin2bn(buf
, num
, f
) == NULL
)
167 * See SP800-56Br2, section 7.1.1.1
168 * RSAEP: 1 < f < (n – 1).
169 * (where f is the plaintext).
171 if (padding
== RSA_NO_PADDING
) {
172 BIGNUM
*nminus1
= BN_CTX_get(ctx
);
174 if (BN_ucmp(f
, BN_value_one()) <= 0) {
175 ERR_raise(ERR_LIB_RSA
, RSA_R_DATA_TOO_SMALL
);
179 || BN_copy(nminus1
, rsa
->n
) == NULL
180 || !BN_sub_word(nminus1
, 1))
182 if (BN_ucmp(f
, nminus1
) >= 0) {
183 ERR_raise(ERR_LIB_RSA
, RSA_R_DATA_TOO_LARGE_FOR_MODULUS
);
189 if (BN_ucmp(f
, rsa
->n
) >= 0) {
190 /* usually the padding functions would catch this */
191 ERR_raise(ERR_LIB_RSA
, RSA_R_DATA_TOO_LARGE_FOR_MODULUS
);
196 if (rsa
->flags
& RSA_FLAG_CACHE_PUBLIC
)
197 if (!BN_MONT_CTX_set_locked(&rsa
->_method_mod_n
, rsa
->lock
,
201 if (!rsa
->meth
->bn_mod_exp(ret
, f
, rsa
->e
, rsa
->n
, ctx
,
206 * BN_bn2binpad puts in leading 0 bytes if the number is less than
207 * the length of the modulus.
209 r
= BN_bn2binpad(ret
, to
, num
);
213 OPENSSL_clear_free(buf
, num
);
217 #if defined(OPENSSL_SYS_TANDEM)
218 static TSAN_QUALIFIER
uint64_t tsan_thread_id
= 1;
221 static uintptr_t get_unique_thread_id(void)
223 #if defined(OPENSSL_SYS_TANDEM)
224 uintptr_t thread_id
= (uintptr_t)CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_TANDEM_ID_KEY
,
227 if (thread_id
== 0) {
228 thread_id
= tsan_counter(&tsan_thread_id
);
229 CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_TANDEM_ID_KEY
, NULL
, (void *)thread_id
);
233 return (uintptr_t)CRYPTO_THREAD_get_current_id();
237 static void free_bn_blinding(ossl_uintmax_t idx
, BN_BLINDING
*b
, void *arg
)
242 void ossl_rsa_free_blinding(RSA
*rsa
)
244 SPARSE_ARRAY_OF(BN_BLINDING
) *blindings
= rsa
->blindings_sa
;
246 ossl_sa_BN_BLINDING_doall_arg(blindings
, free_bn_blinding
, NULL
);
247 ossl_sa_BN_BLINDING_free(blindings
);
250 void *ossl_rsa_alloc_blinding(void)
252 return ossl_sa_BN_BLINDING_new();
255 static BN_BLINDING
*ossl_rsa_get_thread_bn_blinding(RSA
*rsa
)
257 SPARSE_ARRAY_OF(BN_BLINDING
) *blindings
= rsa
->blindings_sa
;
258 uintptr_t tid
= get_unique_thread_id();
260 return ossl_sa_BN_BLINDING_get(blindings
, tid
);
263 static int ossl_rsa_set_thread_bn_blinding(RSA
*rsa
, BN_BLINDING
*b
)
265 SPARSE_ARRAY_OF(BN_BLINDING
) *blindings
= rsa
->blindings_sa
;
266 uintptr_t tid
= get_unique_thread_id();
268 return ossl_sa_BN_BLINDING_set(blindings
, tid
, b
);
271 static BN_BLINDING
*rsa_get_blinding(RSA
*rsa
, BN_CTX
*ctx
)
275 if (!CRYPTO_THREAD_read_lock(rsa
->lock
))
278 ret
= ossl_rsa_get_thread_bn_blinding(rsa
);
279 CRYPTO_THREAD_unlock(rsa
->lock
);
282 ret
= RSA_setup_blinding(rsa
, ctx
);
283 if (!CRYPTO_THREAD_write_lock(rsa
->lock
)) {
284 BN_BLINDING_free(ret
);
287 if (!ossl_rsa_set_thread_bn_blinding(rsa
, ret
)) {
288 BN_BLINDING_free(ret
);
291 CRYPTO_THREAD_unlock(rsa
->lock
);
298 static int rsa_blinding_convert(BN_BLINDING
*b
, BIGNUM
*f
, BN_CTX
*ctx
)
301 * Local blinding: store the unblinding factor in BN_BLINDING.
303 return BN_BLINDING_convert_ex(f
, NULL
, b
, ctx
);
306 static int rsa_blinding_invert(BN_BLINDING
*b
, BIGNUM
*f
, BN_CTX
*ctx
)
309 * For local blinding, unblind is set to NULL, and BN_BLINDING_invert_ex
310 * will use the unblinding factor stored in BN_BLINDING. If BN_BLINDING
311 * is shared between threads, unblind must be non-null:
312 * BN_BLINDING_invert_ex will then use the local unblinding factor, and
313 * will only read the modulus from BN_BLINDING. In both cases it's safe
314 * to access the blinding without a lock.
316 BN_set_flags(f
, BN_FLG_CONSTTIME
);
317 return BN_BLINDING_invert_ex(f
, NULL
, b
, ctx
);
321 static int rsa_ossl_private_encrypt(int flen
, const unsigned char *from
,
322 unsigned char *to
, RSA
*rsa
, int padding
)
324 BIGNUM
*f
, *ret
, *res
;
325 int i
, num
= 0, r
= -1;
326 unsigned char *buf
= NULL
;
328 BN_BLINDING
*blinding
= NULL
;
330 if ((ctx
= BN_CTX_new_ex(rsa
->libctx
)) == NULL
)
334 ret
= BN_CTX_get(ctx
);
335 num
= BN_num_bytes(rsa
->n
);
336 buf
= OPENSSL_malloc(num
);
337 if (ret
== NULL
|| buf
== NULL
)
341 case RSA_PKCS1_PADDING
:
342 i
= RSA_padding_add_PKCS1_type_1(buf
, num
, from
, flen
);
344 case RSA_X931_PADDING
:
345 i
= RSA_padding_add_X931(buf
, num
, from
, flen
);
348 i
= RSA_padding_add_none(buf
, num
, from
, flen
);
351 ERR_raise(ERR_LIB_RSA
, RSA_R_UNKNOWN_PADDING_TYPE
);
357 if (BN_bin2bn(buf
, num
, f
) == NULL
)
360 if (BN_ucmp(f
, rsa
->n
) >= 0) {
361 /* usually the padding functions would catch this */
362 ERR_raise(ERR_LIB_RSA
, RSA_R_DATA_TOO_LARGE_FOR_MODULUS
);
366 if (rsa
->flags
& RSA_FLAG_CACHE_PUBLIC
)
367 if (!BN_MONT_CTX_set_locked(&rsa
->_method_mod_n
, rsa
->lock
,
371 if (!(rsa
->flags
& RSA_FLAG_NO_BLINDING
)) {
372 blinding
= rsa_get_blinding(rsa
, ctx
);
373 if (blinding
== NULL
) {
374 ERR_raise(ERR_LIB_RSA
, ERR_R_INTERNAL_ERROR
);
378 if (!rsa_blinding_convert(blinding
, f
, ctx
))
382 if ((rsa
->flags
& RSA_FLAG_EXT_PKEY
) ||
383 (rsa
->version
== RSA_ASN1_VERSION_MULTI
) ||
386 (rsa
->dmp1
!= NULL
) && (rsa
->dmq1
!= NULL
) && (rsa
->iqmp
!= NULL
))) {
387 if (!rsa
->meth
->rsa_mod_exp(ret
, f
, rsa
, ctx
))
390 BIGNUM
*d
= BN_new();
392 ERR_raise(ERR_LIB_RSA
, ERR_R_BN_LIB
);
395 if (rsa
->d
== NULL
) {
396 ERR_raise(ERR_LIB_RSA
, RSA_R_MISSING_PRIVATE_KEY
);
400 BN_with_flags(d
, rsa
->d
, BN_FLG_CONSTTIME
);
402 if (!rsa
->meth
->bn_mod_exp(ret
, f
, d
, rsa
->n
, ctx
,
403 rsa
->_method_mod_n
)) {
407 /* We MUST free d before any further use of rsa->d */
412 if (!rsa_blinding_invert(blinding
, ret
, ctx
))
415 if (padding
== RSA_X931_PADDING
) {
416 if (!BN_sub(f
, rsa
->n
, ret
))
418 if (BN_cmp(ret
, f
) > 0)
427 * BN_bn2binpad puts in leading 0 bytes if the number is less than
428 * the length of the modulus.
430 r
= BN_bn2binpad(res
, to
, num
);
434 OPENSSL_clear_free(buf
, num
);
438 static int derive_kdk(int flen
, const unsigned char *from
, RSA
*rsa
,
439 unsigned char *buf
, int num
, unsigned char *kdk
)
442 HMAC_CTX
*hmac
= NULL
;
444 unsigned int md_len
= SHA256_DIGEST_LENGTH
;
445 unsigned char d_hash
[SHA256_DIGEST_LENGTH
] = {0};
447 * because we use d as a handle to rsa->d we need to keep it local and
448 * free before any further use of rsa->d
450 BIGNUM
*d
= BN_new();
453 ERR_raise(ERR_LIB_RSA
, ERR_R_CRYPTO_LIB
);
456 if (rsa
->d
== NULL
) {
457 ERR_raise(ERR_LIB_RSA
, RSA_R_MISSING_PRIVATE_KEY
);
461 BN_with_flags(d
, rsa
->d
, BN_FLG_CONSTTIME
);
462 if (BN_bn2binpad(d
, buf
, num
) < 0) {
463 ERR_raise(ERR_LIB_RSA
, ERR_R_INTERNAL_ERROR
);
470 * we use hardcoded hash so that migrating between versions that use
471 * different hash doesn't provide a Bleichenbacher oracle:
472 * if the attacker can see that different versions return different
473 * messages for the same ciphertext, they'll know that the message is
474 * synthetically generated, which means that the padding check failed
476 md
= EVP_MD_fetch(rsa
->libctx
, "sha256", NULL
);
478 ERR_raise(ERR_LIB_RSA
, ERR_R_FETCH_FAILED
);
482 if (EVP_Digest(buf
, num
, d_hash
, NULL
, md
, NULL
) <= 0) {
483 ERR_raise(ERR_LIB_RSA
, ERR_R_INTERNAL_ERROR
);
487 hmac
= HMAC_CTX_new();
489 ERR_raise(ERR_LIB_RSA
, ERR_R_CRYPTO_LIB
);
493 if (HMAC_Init_ex(hmac
, d_hash
, sizeof(d_hash
), md
, NULL
) <= 0) {
494 ERR_raise(ERR_LIB_RSA
, ERR_R_INTERNAL_ERROR
);
499 memset(buf
, 0, num
- flen
);
500 if (HMAC_Update(hmac
, buf
, num
- flen
) <= 0) {
501 ERR_raise(ERR_LIB_RSA
, ERR_R_INTERNAL_ERROR
);
505 if (HMAC_Update(hmac
, from
, flen
) <= 0) {
506 ERR_raise(ERR_LIB_RSA
, ERR_R_INTERNAL_ERROR
);
510 md_len
= SHA256_DIGEST_LENGTH
;
511 if (HMAC_Final(hmac
, kdk
, &md_len
) <= 0) {
512 ERR_raise(ERR_LIB_RSA
, ERR_R_INTERNAL_ERROR
);
523 static int rsa_ossl_private_decrypt(int flen
, const unsigned char *from
,
524 unsigned char *to
, RSA
*rsa
, int padding
)
527 int j
, num
= 0, r
= -1;
528 unsigned char *buf
= NULL
;
529 unsigned char kdk
[SHA256_DIGEST_LENGTH
] = {0};
531 BN_BLINDING
*blinding
= NULL
;
534 * we need the value of the private exponent to perform implicit rejection
536 if ((rsa
->flags
& RSA_FLAG_EXT_PKEY
) && (padding
== RSA_PKCS1_PADDING
))
537 padding
= RSA_PKCS1_NO_IMPLICIT_REJECT_PADDING
;
539 if ((ctx
= BN_CTX_new_ex(rsa
->libctx
)) == NULL
)
543 ret
= BN_CTX_get(ctx
);
545 ERR_raise(ERR_LIB_RSA
, ERR_R_BN_LIB
);
548 num
= BN_num_bytes(rsa
->n
);
549 buf
= OPENSSL_malloc(num
);
554 * This check was for equality but PGP does evil things and chops off the
558 ERR_raise(ERR_LIB_RSA
, RSA_R_DATA_GREATER_THAN_MOD_LEN
);
563 ERR_raise(ERR_LIB_RSA
, RSA_R_DATA_TOO_SMALL
);
567 /* make data into a big number */
568 if (BN_bin2bn(from
, (int)flen
, f
) == NULL
)
573 * See SP800-56Br2, section 7.1.2.1
574 * RSADP: 1 < f < (n – 1)
575 * (where f is the ciphertext).
577 if (padding
== RSA_NO_PADDING
) {
578 BIGNUM
*nminus1
= BN_CTX_get(ctx
);
580 if (BN_ucmp(f
, BN_value_one()) <= 0) {
581 ERR_raise(ERR_LIB_RSA
, RSA_R_DATA_TOO_SMALL
);
585 || BN_copy(nminus1
, rsa
->n
) == NULL
586 || !BN_sub_word(nminus1
, 1))
588 if (BN_ucmp(f
, nminus1
) >= 0) {
589 ERR_raise(ERR_LIB_RSA
, RSA_R_DATA_TOO_LARGE_FOR_MODULUS
);
595 if (BN_ucmp(f
, rsa
->n
) >= 0) {
596 ERR_raise(ERR_LIB_RSA
, RSA_R_DATA_TOO_LARGE_FOR_MODULUS
);
600 if (rsa
->flags
& RSA_FLAG_CACHE_PUBLIC
)
601 if (!BN_MONT_CTX_set_locked(&rsa
->_method_mod_n
, rsa
->lock
,
605 if (!(rsa
->flags
& RSA_FLAG_NO_BLINDING
)) {
606 blinding
= rsa_get_blinding(rsa
, ctx
);
607 if (blinding
== NULL
) {
608 ERR_raise(ERR_LIB_RSA
, ERR_R_INTERNAL_ERROR
);
612 if (!rsa_blinding_convert(blinding
, f
, ctx
))
617 if ((rsa
->flags
& RSA_FLAG_EXT_PKEY
) ||
618 (rsa
->version
== RSA_ASN1_VERSION_MULTI
) ||
621 (rsa
->dmp1
!= NULL
) && (rsa
->dmq1
!= NULL
) && (rsa
->iqmp
!= NULL
))) {
622 if (!rsa
->meth
->rsa_mod_exp(ret
, f
, rsa
, ctx
))
625 BIGNUM
*d
= BN_new();
627 ERR_raise(ERR_LIB_RSA
, ERR_R_BN_LIB
);
630 if (rsa
->d
== NULL
) {
631 ERR_raise(ERR_LIB_RSA
, RSA_R_MISSING_PRIVATE_KEY
);
635 BN_with_flags(d
, rsa
->d
, BN_FLG_CONSTTIME
);
636 if (!rsa
->meth
->bn_mod_exp(ret
, f
, d
, rsa
->n
, ctx
,
637 rsa
->_method_mod_n
)) {
641 /* We MUST free d before any further use of rsa->d */
646 if (!rsa_blinding_invert(blinding
, ret
, ctx
))
650 * derive the Key Derivation Key from private exponent and public
653 if (padding
== RSA_PKCS1_PADDING
) {
654 if (derive_kdk(flen
, from
, rsa
, buf
, num
, kdk
) == 0)
658 j
= BN_bn2binpad(ret
, buf
, num
);
663 case RSA_PKCS1_NO_IMPLICIT_REJECT_PADDING
:
664 r
= RSA_padding_check_PKCS1_type_2(to
, num
, buf
, j
, num
);
666 case RSA_PKCS1_PADDING
:
667 r
= ossl_rsa_padding_check_PKCS1_type_2(rsa
->libctx
, to
, num
, buf
, j
, num
, kdk
);
669 case RSA_PKCS1_OAEP_PADDING
:
670 r
= RSA_padding_check_PKCS1_OAEP(to
, num
, buf
, j
, num
, NULL
, 0);
673 memcpy(to
, buf
, (r
= j
));
676 ERR_raise(ERR_LIB_RSA
, RSA_R_UNKNOWN_PADDING_TYPE
);
681 * This trick doesn't work in the FIPS provider because libcrypto manages
682 * the error stack. Instead we opt not to put an error on the stack at all
683 * in case of padding failure in the FIPS provider.
685 ERR_raise(ERR_LIB_RSA
, RSA_R_PADDING_CHECK_FAILED
);
686 err_clear_last_constant_time(1 & ~constant_time_msb(r
));
692 OPENSSL_clear_free(buf
, num
);
696 /* signature verification */
697 static int rsa_ossl_public_decrypt(int flen
, const unsigned char *from
,
698 unsigned char *to
, RSA
*rsa
, int padding
)
701 int i
, num
= 0, r
= -1;
702 unsigned char *buf
= NULL
;
705 if (BN_num_bits(rsa
->n
) > OPENSSL_RSA_MAX_MODULUS_BITS
) {
706 ERR_raise(ERR_LIB_RSA
, RSA_R_MODULUS_TOO_LARGE
);
710 if (BN_ucmp(rsa
->n
, rsa
->e
) <= 0) {
711 ERR_raise(ERR_LIB_RSA
, RSA_R_BAD_E_VALUE
);
715 /* for large moduli, enforce exponent limit */
716 if (BN_num_bits(rsa
->n
) > OPENSSL_RSA_SMALL_MODULUS_BITS
) {
717 if (BN_num_bits(rsa
->e
) > OPENSSL_RSA_MAX_PUBEXP_BITS
) {
718 ERR_raise(ERR_LIB_RSA
, RSA_R_BAD_E_VALUE
);
723 if ((ctx
= BN_CTX_new_ex(rsa
->libctx
)) == NULL
)
727 ret
= BN_CTX_get(ctx
);
729 ERR_raise(ERR_LIB_RSA
, ERR_R_BN_LIB
);
732 num
= BN_num_bytes(rsa
->n
);
733 buf
= OPENSSL_malloc(num
);
738 * This check was for equality but PGP does evil things and chops off the
742 ERR_raise(ERR_LIB_RSA
, RSA_R_DATA_GREATER_THAN_MOD_LEN
);
746 if (BN_bin2bn(from
, flen
, f
) == NULL
)
749 if (BN_ucmp(f
, rsa
->n
) >= 0) {
750 ERR_raise(ERR_LIB_RSA
, RSA_R_DATA_TOO_LARGE_FOR_MODULUS
);
754 if (rsa
->flags
& RSA_FLAG_CACHE_PUBLIC
)
755 if (!BN_MONT_CTX_set_locked(&rsa
->_method_mod_n
, rsa
->lock
,
759 if (!rsa
->meth
->bn_mod_exp(ret
, f
, rsa
->e
, rsa
->n
, ctx
,
763 /* For X9.31: Assuming e is odd it does a 12 mod 16 test */
764 if ((padding
== RSA_X931_PADDING
) && ((bn_get_words(ret
)[0] & 0xf) != 12))
765 if (!BN_sub(ret
, rsa
->n
, ret
))
768 i
= BN_bn2binpad(ret
, buf
, num
);
773 case RSA_PKCS1_PADDING
:
774 r
= RSA_padding_check_PKCS1_type_1(to
, num
, buf
, i
, num
);
776 case RSA_X931_PADDING
:
777 r
= RSA_padding_check_X931(to
, num
, buf
, i
, num
);
780 memcpy(to
, buf
, (r
= i
));
783 ERR_raise(ERR_LIB_RSA
, RSA_R_UNKNOWN_PADDING_TYPE
);
787 ERR_raise(ERR_LIB_RSA
, RSA_R_PADDING_CHECK_FAILED
);
792 OPENSSL_clear_free(buf
, num
);
796 static int rsa_ossl_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
, RSA
*rsa
, BN_CTX
*ctx
)
798 BIGNUM
*r1
, *m1
, *vrfy
;
799 int ret
= 0, smooth
= 0;
801 BIGNUM
*r2
, *m
[RSA_MAX_PRIME_NUM
- 2];
802 int i
, ex_primes
= 0;
803 RSA_PRIME_INFO
*pinfo
;
808 r1
= BN_CTX_get(ctx
);
810 r2
= BN_CTX_get(ctx
);
812 m1
= BN_CTX_get(ctx
);
813 vrfy
= BN_CTX_get(ctx
);
818 if (rsa
->version
== RSA_ASN1_VERSION_MULTI
819 && ((ex_primes
= sk_RSA_PRIME_INFO_num(rsa
->prime_infos
)) <= 0
820 || ex_primes
> RSA_MAX_PRIME_NUM
- 2))
824 if (rsa
->flags
& RSA_FLAG_CACHE_PRIVATE
) {
825 BIGNUM
*factor
= BN_new();
831 * Make sure BN_mod_inverse in Montgomery initialization uses the
832 * BN_FLG_CONSTTIME flag
834 if (!(BN_with_flags(factor
, rsa
->p
, BN_FLG_CONSTTIME
),
835 BN_MONT_CTX_set_locked(&rsa
->_method_mod_p
, rsa
->lock
,
837 || !(BN_with_flags(factor
, rsa
->q
, BN_FLG_CONSTTIME
),
838 BN_MONT_CTX_set_locked(&rsa
->_method_mod_q
, rsa
->lock
,
844 for (i
= 0; i
< ex_primes
; i
++) {
845 pinfo
= sk_RSA_PRIME_INFO_value(rsa
->prime_infos
, i
);
846 BN_with_flags(factor
, pinfo
->r
, BN_FLG_CONSTTIME
);
847 if (!BN_MONT_CTX_set_locked(&pinfo
->m
, rsa
->lock
, factor
, ctx
)) {
854 * We MUST free |factor| before any further use of the prime factors
858 smooth
= (rsa
->meth
->bn_mod_exp
== BN_mod_exp_mont
)
862 && (BN_num_bits(rsa
->q
) == BN_num_bits(rsa
->p
));
865 if (rsa
->flags
& RSA_FLAG_CACHE_PUBLIC
)
866 if (!BN_MONT_CTX_set_locked(&rsa
->_method_mod_n
, rsa
->lock
,
872 * Conversion from Montgomery domain, a.k.a. Montgomery reduction,
873 * accepts values in [0-m*2^w) range. w is m's bit width rounded up
874 * to limb width. So that at the very least if |I| is fully reduced,
875 * i.e. less than p*q, we can count on from-to round to perform
876 * below modulo operations on |I|. Unlike BN_mod it's constant time.
878 if (/* m1 = I moq q */
879 !bn_from_mont_fixed_top(m1
, I
, rsa
->_method_mod_q
, ctx
)
880 || !bn_to_mont_fixed_top(m1
, m1
, rsa
->_method_mod_q
, ctx
)
882 || !bn_from_mont_fixed_top(r1
, I
, rsa
->_method_mod_p
, ctx
)
883 || !bn_to_mont_fixed_top(r1
, r1
, rsa
->_method_mod_p
, ctx
)
885 * Use parallel exponentiations optimization if possible,
886 * otherwise fallback to two sequential exponentiations:
890 || !BN_mod_exp_mont_consttime_x2(m1
, m1
, rsa
->dmq1
, rsa
->q
,
892 r1
, r1
, rsa
->dmp1
, rsa
->p
,
895 /* r1 = (r1 - m1) mod p */
897 * bn_mod_sub_fixed_top is not regular modular subtraction,
898 * it can tolerate subtrahend to be larger than modulus, but
899 * not bit-wise wider. This makes up for uncommon q>p case,
900 * when |m1| can be larger than |rsa->p|.
902 || !bn_mod_sub_fixed_top(r1
, r1
, m1
, rsa
->p
)
904 /* r1 = r1 * iqmp mod p */
905 || !bn_to_mont_fixed_top(r1
, r1
, rsa
->_method_mod_p
, ctx
)
906 || !bn_mul_mont_fixed_top(r1
, r1
, rsa
->iqmp
, rsa
->_method_mod_p
,
908 /* r0 = r1 * q + m1 */
909 || !bn_mul_fixed_top(r0
, r1
, rsa
->q
, ctx
)
910 || !bn_mod_add_fixed_top(r0
, r0
, m1
, rsa
->n
))
916 /* compute I mod q */
918 BIGNUM
*c
= BN_new();
921 BN_with_flags(c
, I
, BN_FLG_CONSTTIME
);
923 if (!BN_mod(r1
, c
, rsa
->q
, ctx
)) {
929 BIGNUM
*dmq1
= BN_new();
934 BN_with_flags(dmq1
, rsa
->dmq1
, BN_FLG_CONSTTIME
);
936 /* compute r1^dmq1 mod q */
937 if (!rsa
->meth
->bn_mod_exp(m1
, r1
, dmq1
, rsa
->q
, ctx
,
938 rsa
->_method_mod_q
)) {
943 /* We MUST free dmq1 before any further use of rsa->dmq1 */
947 /* compute I mod p */
948 if (!BN_mod(r1
, c
, rsa
->p
, ctx
)) {
952 /* We MUST free c before any further use of I */
957 BIGNUM
*dmp1
= BN_new();
960 BN_with_flags(dmp1
, rsa
->dmp1
, BN_FLG_CONSTTIME
);
962 /* compute r1^dmp1 mod p */
963 if (!rsa
->meth
->bn_mod_exp(r0
, r1
, dmp1
, rsa
->p
, ctx
,
964 rsa
->_method_mod_p
)) {
968 /* We MUST free dmp1 before any further use of rsa->dmp1 */
974 BIGNUM
*di
= BN_new(), *cc
= BN_new();
976 if (cc
== NULL
|| di
== NULL
) {
982 for (i
= 0; i
< ex_primes
; i
++) {
984 if ((m
[i
] = BN_CTX_get(ctx
)) == NULL
) {
990 pinfo
= sk_RSA_PRIME_INFO_value(rsa
->prime_infos
, i
);
992 /* prepare c and d_i */
993 BN_with_flags(cc
, I
, BN_FLG_CONSTTIME
);
994 BN_with_flags(di
, pinfo
->d
, BN_FLG_CONSTTIME
);
996 if (!BN_mod(r1
, cc
, pinfo
->r
, ctx
)) {
1001 /* compute r1 ^ d_i mod r_i */
1002 if (!rsa
->meth
->bn_mod_exp(m
[i
], r1
, di
, pinfo
->r
, ctx
, pinfo
->m
)) {
1014 if (!BN_sub(r0
, r0
, m1
))
1017 * This will help stop the size of r0 increasing, which does affect the
1018 * multiply if it optimised for a power of 2 size
1020 if (BN_is_negative(r0
))
1021 if (!BN_add(r0
, r0
, rsa
->p
))
1024 if (!BN_mul(r1
, r0
, rsa
->iqmp
, ctx
))
1028 BIGNUM
*pr1
= BN_new();
1031 BN_with_flags(pr1
, r1
, BN_FLG_CONSTTIME
);
1033 if (!BN_mod(r0
, pr1
, rsa
->p
, ctx
)) {
1037 /* We MUST free pr1 before any further use of r1 */
1042 * If p < q it is occasionally possible for the correction of adding 'p'
1043 * if r0 is negative above to leave the result still negative. This can
1044 * break the private key operations: the following second correction
1045 * should *always* correct this rare occurrence. This will *never* happen
1046 * with OpenSSL generated keys because they ensure p > q [steve]
1048 if (BN_is_negative(r0
))
1049 if (!BN_add(r0
, r0
, rsa
->p
))
1051 if (!BN_mul(r1
, r0
, rsa
->q
, ctx
))
1053 if (!BN_add(r0
, r1
, m1
))
1057 /* add m_i to m in multi-prime case */
1058 if (ex_primes
> 0) {
1059 BIGNUM
*pr2
= BN_new();
1064 for (i
= 0; i
< ex_primes
; i
++) {
1065 pinfo
= sk_RSA_PRIME_INFO_value(rsa
->prime_infos
, i
);
1066 if (!BN_sub(r1
, m
[i
], r0
)) {
1071 if (!BN_mul(r2
, r1
, pinfo
->t
, ctx
)) {
1076 BN_with_flags(pr2
, r2
, BN_FLG_CONSTTIME
);
1078 if (!BN_mod(r1
, pr2
, pinfo
->r
, ctx
)) {
1083 if (BN_is_negative(r1
))
1084 if (!BN_add(r1
, r1
, pinfo
->r
)) {
1088 if (!BN_mul(r1
, r1
, pinfo
->pp
, ctx
)) {
1092 if (!BN_add(r0
, r0
, r1
)) {
1102 if (rsa
->e
&& rsa
->n
) {
1103 if (rsa
->meth
->bn_mod_exp
== BN_mod_exp_mont
) {
1104 if (!BN_mod_exp_mont(vrfy
, r0
, rsa
->e
, rsa
->n
, ctx
,
1105 rsa
->_method_mod_n
))
1109 if (!rsa
->meth
->bn_mod_exp(vrfy
, r0
, rsa
->e
, rsa
->n
, ctx
,
1110 rsa
->_method_mod_n
))
1114 * If 'I' was greater than (or equal to) rsa->n, the operation will
1115 * be equivalent to using 'I mod n'. However, the result of the
1116 * verify will *always* be less than 'n' so we don't check for
1117 * absolute equality, just congruency.
1119 if (!BN_sub(vrfy
, vrfy
, I
))
1121 if (BN_is_zero(vrfy
)) {
1124 goto err
; /* not actually error */
1126 if (!BN_mod(vrfy
, vrfy
, rsa
->n
, ctx
))
1128 if (BN_is_negative(vrfy
))
1129 if (!BN_add(vrfy
, vrfy
, rsa
->n
))
1131 if (!BN_is_zero(vrfy
)) {
1133 * 'I' and 'vrfy' aren't congruent mod n. Don't leak
1134 * miscalculated CRT output, just do a raw (slower) mod_exp and
1135 * return that instead.
1138 BIGNUM
*d
= BN_new();
1141 BN_with_flags(d
, rsa
->d
, BN_FLG_CONSTTIME
);
1143 if (!rsa
->meth
->bn_mod_exp(r0
, I
, d
, rsa
->n
, ctx
,
1144 rsa
->_method_mod_n
)) {
1148 /* We MUST free d before any further use of rsa->d */
1153 * It's unfortunate that we have to bn_correct_top(r0). What hopefully
1154 * saves the day is that correction is highly unlike, and private key
1155 * operations are customarily performed on blinded message. Which means
1156 * that attacker won't observe correlation with chosen plaintext.
1157 * Secondly, remaining code would still handle it in same computational
1158 * time and even conceal memory access pattern around corrected top.
1167 static int rsa_ossl_init(RSA
*rsa
)
1169 rsa
->flags
|= RSA_FLAG_CACHE_PUBLIC
| RSA_FLAG_CACHE_PRIVATE
;
1173 static int rsa_ossl_finish(RSA
*rsa
)
1177 RSA_PRIME_INFO
*pinfo
;
1179 for (i
= 0; i
< sk_RSA_PRIME_INFO_num(rsa
->prime_infos
); i
++) {
1180 pinfo
= sk_RSA_PRIME_INFO_value(rsa
->prime_infos
, i
);
1181 BN_MONT_CTX_free(pinfo
->m
);
1185 BN_MONT_CTX_free(rsa
->_method_mod_n
);
1186 BN_MONT_CTX_free(rsa
->_method_mod_p
);
1187 BN_MONT_CTX_free(rsa
->_method_mod_q
);
1191 #ifdef S390X_MOD_EXP
1192 static int rsa_ossl_s390x_mod_exp(BIGNUM
*r0
, const BIGNUM
*i
, RSA
*rsa
,
1195 if (rsa
->version
!= RSA_ASN1_VERSION_MULTI
) {
1196 if (s390x_crt(r0
, i
, rsa
->p
, rsa
->q
, rsa
->dmp1
, rsa
->dmq1
, rsa
->iqmp
) == 1)
1199 return rsa_ossl_mod_exp(r0
, i
, rsa
, ctx
);