]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/rsa/rsa_ossl.c
Update util/analyze-contention-log.sh
[thirdparty/openssl.git] / crypto / rsa / rsa_ossl.c
CommitLineData
2039c421 1/*
7ed6de99 2 * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved.
46a64376 3 *
2a7b6f39 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
2039c421
RS
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
46a64376 8 */
58964a49 9
c5f87134
P
10/*
11 * RSA low level APIs are deprecated for public use, but still ok for
12 * internal use.
13 */
14#include "internal/deprecated.h"
15
b39fc560 16#include "internal/cryptlib.h"
25f2138b 17#include "crypto/bn.h"
902568bb 18#include "crypto/sparse_array.h"
706457b7
DMSP
19#include "rsa_local.h"
20#include "internal/constant_time.h"
7fc67e0a
HK
21#include <openssl/evp.h>
22#include <openssl/sha.h>
23#include <openssl/hmac.h>
58964a49 24
902568bb
NH
25DEFINE_SPARSE_ARRAY_OF(BN_BLINDING);
26
bf160551 27static int rsa_ossl_public_encrypt(int flen, const unsigned char *from,
0f113f3e 28 unsigned char *to, RSA *rsa, int padding);
bf160551 29static int rsa_ossl_private_encrypt(int flen, const unsigned char *from,
0f113f3e 30 unsigned char *to, RSA *rsa, int padding);
bf160551 31static int rsa_ossl_public_decrypt(int flen, const unsigned char *from,
0f113f3e 32 unsigned char *to, RSA *rsa, int padding);
bf160551 33static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
0f113f3e 34 unsigned char *to, RSA *rsa, int padding);
bf160551 35static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa,
0f113f3e 36 BN_CTX *ctx);
bf160551
RS
37static int rsa_ossl_init(RSA *rsa);
38static int rsa_ossl_finish(RSA *rsa);
79040cf2
JC
39#ifdef S390X_MOD_EXP
40static int rsa_ossl_s390x_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa,
41 BN_CTX *ctx);
42static RSA_METHOD rsa_pkcs1_ossl_meth = {
43 "OpenSSL PKCS#1 RSA",
44 rsa_ossl_public_encrypt,
45 rsa_ossl_public_decrypt, /* signature verification */
46 rsa_ossl_private_encrypt, /* signing */
47 rsa_ossl_private_decrypt,
48 rsa_ossl_s390x_mod_exp,
49 s390x_mod_exp,
50 rsa_ossl_init,
51 rsa_ossl_finish,
52 RSA_FLAG_FIPS_METHOD, /* flags */
53 NULL,
54 0, /* rsa_sign */
55 0, /* rsa_verify */
56 NULL, /* rsa_keygen */
57 NULL /* rsa_multi_prime_keygen */
58};
59#else
bf160551 60static RSA_METHOD rsa_pkcs1_ossl_meth = {
076fc555 61 "OpenSSL PKCS#1 RSA",
bf160551
RS
62 rsa_ossl_public_encrypt,
63 rsa_ossl_public_decrypt, /* signature verification */
64 rsa_ossl_private_encrypt, /* signing */
65 rsa_ossl_private_decrypt,
66 rsa_ossl_mod_exp,
0f113f3e
MC
67 BN_mod_exp_mont, /* XXX probably we should not use Montgomery
68 * if e == 3 */
bf160551
RS
69 rsa_ossl_init,
70 rsa_ossl_finish,
0f113f3e
MC
71 RSA_FLAG_FIPS_METHOD, /* flags */
72 NULL,
73 0, /* rsa_sign */
74 0, /* rsa_verify */
665d899f
PY
75 NULL, /* rsa_keygen */
76 NULL /* rsa_multi_prime_keygen */
0f113f3e 77};
79040cf2 78#endif
58964a49 79
076fc555
RS
80static const RSA_METHOD *default_RSA_meth = &rsa_pkcs1_ossl_meth;
81
82void RSA_set_default_method(const RSA_METHOD *meth)
83{
84 default_RSA_meth = meth;
85}
86
87const RSA_METHOD *RSA_get_default_method(void)
88{
89 return default_RSA_meth;
90}
91
b0700d2c 92const RSA_METHOD *RSA_PKCS1_OpenSSL(void)
0f113f3e 93{
bf160551 94 return &rsa_pkcs1_ossl_meth;
0f113f3e 95}
58964a49 96
076fc555
RS
97const RSA_METHOD *RSA_null_method(void)
98{
99 return NULL;
100}
101
bf160551 102static int rsa_ossl_public_encrypt(int flen, const unsigned char *from,
0f113f3e
MC
103 unsigned char *to, RSA *rsa, int padding)
104{
105 BIGNUM *f, *ret;
582ad5d4 106 int i, num = 0, r = -1;
0f113f3e
MC
107 unsigned char *buf = NULL;
108 BN_CTX *ctx = NULL;
109
110 if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
9311d0c4 111 ERR_raise(ERR_LIB_RSA, RSA_R_MODULUS_TOO_LARGE);
0f113f3e
MC
112 return -1;
113 }
114
115 if (BN_ucmp(rsa->n, rsa->e) <= 0) {
9311d0c4 116 ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
0f113f3e
MC
117 return -1;
118 }
119
120 /* for large moduli, enforce exponent limit */
121 if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
122 if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
9311d0c4 123 ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
0f113f3e
MC
124 return -1;
125 }
126 }
127
afb638f1 128 if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
0f113f3e
MC
129 goto err;
130 BN_CTX_start(ctx);
131 f = BN_CTX_get(ctx);
132 ret = BN_CTX_get(ctx);
133 num = BN_num_bytes(rsa->n);
134 buf = OPENSSL_malloc(num);
e077455e 135 if (ret == NULL || buf == NULL)
0f113f3e 136 goto err;
0f113f3e
MC
137
138 switch (padding) {
139 case RSA_PKCS1_PADDING:
23b2fc0b
P
140 i = ossl_rsa_padding_add_PKCS1_type_2_ex(rsa->libctx, buf, num,
141 from, flen);
0f113f3e 142 break;
0f113f3e 143 case RSA_PKCS1_OAEP_PADDING:
23b2fc0b
P
144 i = ossl_rsa_padding_add_PKCS1_OAEP_mgf1_ex(rsa->libctx, buf, num,
145 from, flen, NULL, 0,
146 NULL, NULL);
0f113f3e 147 break;
0f113f3e
MC
148 case RSA_NO_PADDING:
149 i = RSA_padding_add_none(buf, num, from, flen);
150 break;
151 default:
9311d0c4 152 ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
0f113f3e
MC
153 goto err;
154 }
155 if (i <= 0)
156 goto err;
157
158 if (BN_bin2bn(buf, num, f) == NULL)
159 goto err;
160
4514e02c 161#ifdef FIPS_MODULE
162 /*
163 * See SP800-56Br2, section 7.1.1.1
164 * RSAEP: 1 < f < (n – 1).
165 * (where f is the plaintext).
166 */
167 if (padding == RSA_NO_PADDING) {
168 BIGNUM *nminus1 = BN_CTX_get(ctx);
169
170 if (BN_ucmp(f, BN_value_one()) <= 0) {
171 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_SMALL);
172 goto err;
173 }
174 if (nminus1 == NULL
175 || BN_copy(nminus1, rsa->n) == NULL
176 || !BN_sub_word(nminus1, 1))
177 goto err;
178 if (BN_ucmp(f, nminus1) >= 0) {
179 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
180 goto err;
181 }
182 } else
183#endif
184 {
185 if (BN_ucmp(f, rsa->n) >= 0) {
186 /* usually the padding functions would catch this */
187 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
188 goto err;
189 }
0f113f3e
MC
190 }
191
192 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
41bfd5e7
AP
193 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
194 rsa->n, ctx))
0f113f3e
MC
195 goto err;
196
197 if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
198 rsa->_method_mod_n))
199 goto err;
200
201 /*
582ad5d4
AP
202 * BN_bn2binpad puts in leading 0 bytes if the number is less than
203 * the length of the modulus.
0f113f3e 204 */
582ad5d4 205 r = BN_bn2binpad(ret, to, num);
0f113f3e 206 err:
ce1415ed 207 BN_CTX_end(ctx);
23a1d5e9 208 BN_CTX_free(ctx);
4b45c6e5 209 OPENSSL_clear_free(buf, num);
8686c474 210 return r;
0f113f3e 211}
58964a49 212
902568bb 213static void free_bn_blinding(ossl_uintmax_t idx, BN_BLINDING *b, void *arg)
800e400d 214{
902568bb
NH
215 BN_BLINDING_free(b);
216}
0f113f3e 217
902568bb
NH
218void ossl_rsa_free_blinding(RSA *rsa)
219{
220 SPARSE_ARRAY_OF(BN_BLINDING) *blindings = rsa->blindings_sa;
0f113f3e 221
902568bb
NH
222 ossl_sa_BN_BLINDING_doall_arg(blindings, free_bn_blinding, NULL);
223 ossl_sa_BN_BLINDING_free(blindings);
224}
0f113f3e 225
902568bb
NH
226void *ossl_rsa_alloc_blinding(void)
227{
228 return ossl_sa_BN_BLINDING_new();
229}
0f113f3e 230
902568bb
NH
231static BN_BLINDING *ossl_rsa_get_thread_bn_blinding(RSA *rsa)
232{
233 SPARSE_ARRAY_OF(BN_BLINDING) *blindings = rsa->blindings_sa;
234 uintptr_t tid = (uintptr_t)CRYPTO_THREAD_get_current_id();
0f113f3e 235
902568bb
NH
236 return ossl_sa_BN_BLINDING_get(blindings, tid);
237}
5679bcce 238
902568bb
NH
239static int ossl_rsa_set_thread_bn_blinding(RSA *rsa, BN_BLINDING *b)
240{
241 SPARSE_ARRAY_OF(BN_BLINDING) *blindings = rsa->blindings_sa;
242 uintptr_t tid = (uintptr_t)CRYPTO_THREAD_get_current_id();
243
244 return ossl_sa_BN_BLINDING_set(blindings, tid, b);
800e400d 245}
5679bcce 246
902568bb 247static BN_BLINDING *rsa_get_blinding(RSA *rsa, BN_CTX *ctx)
0f113f3e 248{
902568bb 249 BN_BLINDING *ret;
0b1a07c8 250
902568bb
NH
251 if (!CRYPTO_THREAD_read_lock(rsa->lock))
252 return NULL;
aefbcde2 253
902568bb
NH
254 ret = ossl_rsa_get_thread_bn_blinding(rsa);
255 CRYPTO_THREAD_unlock(rsa->lock);
0b1a07c8 256
902568bb
NH
257 if (ret == NULL) {
258 ret = RSA_setup_blinding(rsa, ctx);
259 if (!CRYPTO_THREAD_write_lock(rsa->lock)) {
260 BN_BLINDING_free(ret);
261 ret = NULL;
262 } else {
263 if (!ossl_rsa_set_thread_bn_blinding(rsa, ret)) {
264 BN_BLINDING_free(ret);
265 ret = NULL;
266 }
267 }
268 CRYPTO_THREAD_unlock(rsa->lock);
0f113f3e 269 }
902568bb
NH
270
271 return ret;
272}
273
274static int rsa_blinding_convert(BN_BLINDING *b, BIGNUM *f, BN_CTX *ctx)
275{
276 /*
277 * Local blinding: store the unblinding factor in BN_BLINDING.
278 */
279 return BN_BLINDING_convert_ex(f, NULL, b, ctx);
0f113f3e 280}
e5641d7f 281
902568bb 282static int rsa_blinding_invert(BN_BLINDING *b, BIGNUM *f, BN_CTX *ctx)
0f113f3e
MC
283{
284 /*
285 * For local blinding, unblind is set to NULL, and BN_BLINDING_invert_ex
286 * will use the unblinding factor stored in BN_BLINDING. If BN_BLINDING
287 * is shared between threads, unblind must be non-null:
288 * BN_BLINDING_invert_ex will then use the local unblinding factor, and
289 * will only read the modulus from BN_BLINDING. In both cases it's safe
290 * to access the blinding without a lock.
291 */
f06ef165 292 BN_set_flags(f, BN_FLG_CONSTTIME);
902568bb 293 return BN_BLINDING_invert_ex(f, NULL, b, ctx);
0f113f3e 294}
5679bcce 295
24cff6ce 296/* signing */
bf160551 297static int rsa_ossl_private_encrypt(int flen, const unsigned char *from,
0f113f3e
MC
298 unsigned char *to, RSA *rsa, int padding)
299{
300 BIGNUM *f, *ret, *res;
582ad5d4 301 int i, num = 0, r = -1;
0f113f3e
MC
302 unsigned char *buf = NULL;
303 BN_CTX *ctx = NULL;
0f113f3e
MC
304 BN_BLINDING *blinding = NULL;
305
afb638f1 306 if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
0f113f3e
MC
307 goto err;
308 BN_CTX_start(ctx);
309 f = BN_CTX_get(ctx);
310 ret = BN_CTX_get(ctx);
311 num = BN_num_bytes(rsa->n);
312 buf = OPENSSL_malloc(num);
e077455e 313 if (ret == NULL || buf == NULL)
0f113f3e 314 goto err;
0f113f3e
MC
315
316 switch (padding) {
317 case RSA_PKCS1_PADDING:
318 i = RSA_padding_add_PKCS1_type_1(buf, num, from, flen);
319 break;
320 case RSA_X931_PADDING:
321 i = RSA_padding_add_X931(buf, num, from, flen);
322 break;
323 case RSA_NO_PADDING:
324 i = RSA_padding_add_none(buf, num, from, flen);
325 break;
0f113f3e 326 default:
9311d0c4 327 ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
0f113f3e
MC
328 goto err;
329 }
330 if (i <= 0)
331 goto err;
332
333 if (BN_bin2bn(buf, num, f) == NULL)
334 goto err;
335
336 if (BN_ucmp(f, rsa->n) >= 0) {
337 /* usually the padding functions would catch this */
9311d0c4 338 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
0f113f3e
MC
339 goto err;
340 }
341
2cc3f68c
AP
342 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
343 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
344 rsa->n, ctx))
345 goto err;
346
0f113f3e 347 if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
902568bb 348 blinding = rsa_get_blinding(rsa, ctx);
0f113f3e 349 if (blinding == NULL) {
9311d0c4 350 ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
0f113f3e
MC
351 goto err;
352 }
0f113f3e 353
902568bb 354 if (!rsa_blinding_convert(blinding, f, ctx))
0f113f3e
MC
355 goto err;
356 }
357
358 if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
665d899f 359 (rsa->version == RSA_ASN1_VERSION_MULTI) ||
0f113f3e
MC
360 ((rsa->p != NULL) &&
361 (rsa->q != NULL) &&
362 (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
363 if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
364 goto err;
365 } else {
5584f65a
MC
366 BIGNUM *d = BN_new();
367 if (d == NULL) {
e077455e 368 ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
5584f65a 369 goto err;
fd7d2520 370 }
7408f675 371 if (rsa->d == NULL) {
9311d0c4 372 ERR_raise(ERR_LIB_RSA, RSA_R_MISSING_PRIVATE_KEY);
7408f675
DO
373 BN_free(d);
374 goto err;
375 }
5584f65a 376 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
0f113f3e 377
0f113f3e
MC
378 if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
379 rsa->_method_mod_n)) {
5584f65a 380 BN_free(d);
0f113f3e
MC
381 goto err;
382 }
5584f65a
MC
383 /* We MUST free d before any further use of rsa->d */
384 BN_free(d);
0f113f3e
MC
385 }
386
387 if (blinding)
902568bb 388 if (!rsa_blinding_invert(blinding, ret, ctx))
0f113f3e
MC
389 goto err;
390
391 if (padding == RSA_X931_PADDING) {
3d3cbce5
P
392 if (!BN_sub(f, rsa->n, ret))
393 goto err;
0f113f3e
MC
394 if (BN_cmp(ret, f) > 0)
395 res = f;
396 else
397 res = ret;
90862ab4 398 } else {
0f113f3e 399 res = ret;
90862ab4 400 }
0f113f3e
MC
401
402 /*
582ad5d4
AP
403 * BN_bn2binpad puts in leading 0 bytes if the number is less than
404 * the length of the modulus.
0f113f3e 405 */
582ad5d4 406 r = BN_bn2binpad(res, to, num);
0f113f3e 407 err:
ce1415ed 408 BN_CTX_end(ctx);
23a1d5e9 409 BN_CTX_free(ctx);
4b45c6e5 410 OPENSSL_clear_free(buf, num);
8686c474 411 return r;
0f113f3e 412}
58964a49 413
b1892d21
DB
414static int derive_kdk(int flen, const unsigned char *from, RSA *rsa,
415 unsigned char *buf, int num, unsigned char *kdk)
416{
417 int ret = 0;
418 HMAC_CTX *hmac = NULL;
419 EVP_MD *md = NULL;
420 unsigned int md_len = SHA256_DIGEST_LENGTH;
421 unsigned char d_hash[SHA256_DIGEST_LENGTH] = {0};
422 /*
423 * because we use d as a handle to rsa->d we need to keep it local and
424 * free before any further use of rsa->d
425 */
426 BIGNUM *d = BN_new();
427
428 if (d == NULL) {
429 ERR_raise(ERR_LIB_RSA, ERR_R_CRYPTO_LIB);
430 goto err;
431 }
432 if (rsa->d == NULL) {
433 ERR_raise(ERR_LIB_RSA, RSA_R_MISSING_PRIVATE_KEY);
434 BN_free(d);
435 goto err;
436 }
437 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
438 if (BN_bn2binpad(d, buf, num) < 0) {
439 ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
440 BN_free(d);
441 goto err;
442 }
443 BN_free(d);
444
445 /*
446 * we use hardcoded hash so that migrating between versions that use
447 * different hash doesn't provide a Bleichenbacher oracle:
448 * if the attacker can see that different versions return different
449 * messages for the same ciphertext, they'll know that the message is
eb4129e1 450 * synthetically generated, which means that the padding check failed
b1892d21
DB
451 */
452 md = EVP_MD_fetch(rsa->libctx, "sha256", NULL);
453 if (md == NULL) {
454 ERR_raise(ERR_LIB_RSA, ERR_R_FETCH_FAILED);
455 goto err;
456 }
457
458 if (EVP_Digest(buf, num, d_hash, NULL, md, NULL) <= 0) {
459 ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
460 goto err;
461 }
462
463 hmac = HMAC_CTX_new();
464 if (hmac == NULL) {
465 ERR_raise(ERR_LIB_RSA, ERR_R_CRYPTO_LIB);
466 goto err;
467 }
468
469 if (HMAC_Init_ex(hmac, d_hash, sizeof(d_hash), md, NULL) <= 0) {
470 ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
471 goto err;
472 }
473
474 if (flen < num) {
475 memset(buf, 0, num - flen);
476 if (HMAC_Update(hmac, buf, num - flen) <= 0) {
477 ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
478 goto err;
479 }
480 }
481 if (HMAC_Update(hmac, from, flen) <= 0) {
482 ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
483 goto err;
484 }
485
486 md_len = SHA256_DIGEST_LENGTH;
487 if (HMAC_Final(hmac, kdk, &md_len) <= 0) {
488 ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
489 goto err;
490 }
491 ret = 1;
492
493 err:
494 HMAC_CTX_free(hmac);
495 EVP_MD_free(md);
496 return ret;
497}
498
bf160551 499static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
0f113f3e
MC
500 unsigned char *to, RSA *rsa, int padding)
501{
502 BIGNUM *f, *ret;
503 int j, num = 0, r = -1;
0f113f3e 504 unsigned char *buf = NULL;
7fc67e0a 505 unsigned char kdk[SHA256_DIGEST_LENGTH] = {0};
0f113f3e 506 BN_CTX *ctx = NULL;
0f113f3e
MC
507 BN_BLINDING *blinding = NULL;
508
5ab3ec1b
HK
509 /*
510 * we need the value of the private exponent to perform implicit rejection
511 */
512 if ((rsa->flags & RSA_FLAG_EXT_PKEY) && (padding == RSA_PKCS1_PADDING))
513 padding = RSA_PKCS1_NO_IMPLICIT_REJECT_PADDING;
514
afb638f1 515 if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
0f113f3e
MC
516 goto err;
517 BN_CTX_start(ctx);
518 f = BN_CTX_get(ctx);
519 ret = BN_CTX_get(ctx);
e077455e
RL
520 if (ret == NULL) {
521 ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
522 goto err;
523 }
0f113f3e
MC
524 num = BN_num_bytes(rsa->n);
525 buf = OPENSSL_malloc(num);
e077455e 526 if (buf == NULL)
0f113f3e 527 goto err;
0f113f3e
MC
528
529 /*
530 * This check was for equality but PGP does evil things and chops off the
531 * top '0' bytes
532 */
533 if (flen > num) {
9311d0c4 534 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_GREATER_THAN_MOD_LEN);
0f113f3e
MC
535 goto err;
536 }
537
7fc67e0a
HK
538 if (flen < 1) {
539 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_SMALL);
540 goto err;
541 }
542
0f113f3e
MC
543 /* make data into a big number */
544 if (BN_bin2bn(from, (int)flen, f) == NULL)
545 goto err;
546
4514e02c 547#ifdef FIPS_MODULE
548 /*
549 * See SP800-56Br2, section 7.1.2.1
550 * RSADP: 1 < f < (n – 1)
551 * (where f is the ciphertext).
552 */
553 if (padding == RSA_NO_PADDING) {
554 BIGNUM *nminus1 = BN_CTX_get(ctx);
0f113f3e 555
4514e02c 556 if (BN_ucmp(f, BN_value_one()) <= 0) {
557 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_SMALL);
558 goto err;
559 }
560 if (nminus1 == NULL
561 || BN_copy(nminus1, rsa->n) == NULL
562 || !BN_sub_word(nminus1, 1))
563 goto err;
564 if (BN_ucmp(f, nminus1) >= 0) {
565 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
566 goto err;
567 }
568 } else
569#endif
570 {
571 if (BN_ucmp(f, rsa->n) >= 0) {
572 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
573 goto err;
574 }
575 }
f06ef165
BE
576 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
577 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
578 rsa->n, ctx))
579 goto err;
580
0f113f3e 581 if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
902568bb 582 blinding = rsa_get_blinding(rsa, ctx);
0f113f3e 583 if (blinding == NULL) {
9311d0c4 584 ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
0f113f3e
MC
585 goto err;
586 }
0f113f3e 587
902568bb 588 if (!rsa_blinding_convert(blinding, f, ctx))
0f113f3e
MC
589 goto err;
590 }
591
592 /* do the decrypt */
593 if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
665d899f 594 (rsa->version == RSA_ASN1_VERSION_MULTI) ||
0f113f3e
MC
595 ((rsa->p != NULL) &&
596 (rsa->q != NULL) &&
597 (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
598 if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
599 goto err;
600 } else {
5584f65a
MC
601 BIGNUM *d = BN_new();
602 if (d == NULL) {
e077455e 603 ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
5584f65a 604 goto err;
7408f675
DO
605 }
606 if (rsa->d == NULL) {
9311d0c4 607 ERR_raise(ERR_LIB_RSA, RSA_R_MISSING_PRIVATE_KEY);
7408f675
DO
608 BN_free(d);
609 goto err;
fd7d2520 610 }
5584f65a 611 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
0f113f3e
MC
612 if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
613 rsa->_method_mod_n)) {
5584f65a 614 BN_free(d);
0f113f3e
MC
615 goto err;
616 }
5584f65a
MC
617 /* We MUST free d before any further use of rsa->d */
618 BN_free(d);
0f113f3e
MC
619 }
620
4209ce68 621 if (blinding)
902568bb 622 if (!rsa_blinding_invert(blinding, ret, ctx))
4209ce68
BE
623 goto err;
624
7fc67e0a
HK
625 /*
626 * derive the Key Derivation Key from private exponent and public
627 * ciphertext
628 */
5ab3ec1b 629 if (padding == RSA_PKCS1_PADDING) {
b1892d21 630 if (derive_kdk(flen, from, rsa, buf, num, kdk) == 0)
7fc67e0a 631 goto err;
b1892d21 632 }
7fc67e0a 633
4209ce68
BE
634 j = BN_bn2binpad(ret, buf, num);
635 if (j < 0)
636 goto err;
7fc67e0a 637
0f113f3e 638 switch (padding) {
5ab3ec1b
HK
639 case RSA_PKCS1_NO_IMPLICIT_REJECT_PADDING:
640 r = RSA_padding_check_PKCS1_type_2(to, num, buf, j, num);
641 break;
0f113f3e 642 case RSA_PKCS1_PADDING:
5ab3ec1b 643 r = ossl_rsa_padding_check_PKCS1_type_2(rsa->libctx, to, num, buf, j, num, kdk);
0f113f3e 644 break;
0f113f3e
MC
645 case RSA_PKCS1_OAEP_PADDING:
646 r = RSA_padding_check_PKCS1_OAEP(to, num, buf, j, num, NULL, 0);
647 break;
0f113f3e 648 case RSA_NO_PADDING:
582ad5d4 649 memcpy(to, buf, (r = j));
0f113f3e
MC
650 break;
651 default:
9311d0c4 652 ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
0f113f3e
MC
653 goto err;
654 }
f844f9eb 655#ifndef FIPS_MODULE
afb638f1
MC
656 /*
657 * This trick doesn't work in the FIPS provider because libcrypto manages
658 * the error stack. Instead we opt not to put an error on the stack at all
659 * in case of padding failure in the FIPS provider.
660 */
9311d0c4 661 ERR_raise(ERR_LIB_RSA, RSA_R_PADDING_CHECK_FAILED);
94dc53a3 662 err_clear_last_constant_time(1 & ~constant_time_msb(r));
afb638f1 663#endif
0f113f3e
MC
664
665 err:
ce1415ed 666 BN_CTX_end(ctx);
23a1d5e9 667 BN_CTX_free(ctx);
4b45c6e5 668 OPENSSL_clear_free(buf, num);
8686c474 669 return r;
0f113f3e 670}
58964a49 671
24cff6ce 672/* signature verification */
bf160551 673static int rsa_ossl_public_decrypt(int flen, const unsigned char *from,
0f113f3e
MC
674 unsigned char *to, RSA *rsa, int padding)
675{
676 BIGNUM *f, *ret;
677 int i, num = 0, r = -1;
0f113f3e
MC
678 unsigned char *buf = NULL;
679 BN_CTX *ctx = NULL;
680
681 if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
9311d0c4 682 ERR_raise(ERR_LIB_RSA, RSA_R_MODULUS_TOO_LARGE);
0f113f3e
MC
683 return -1;
684 }
685
686 if (BN_ucmp(rsa->n, rsa->e) <= 0) {
9311d0c4 687 ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
0f113f3e
MC
688 return -1;
689 }
690
691 /* for large moduli, enforce exponent limit */
692 if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
693 if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
9311d0c4 694 ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
0f113f3e
MC
695 return -1;
696 }
697 }
698
afb638f1 699 if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
0f113f3e
MC
700 goto err;
701 BN_CTX_start(ctx);
702 f = BN_CTX_get(ctx);
703 ret = BN_CTX_get(ctx);
e077455e
RL
704 if (ret == NULL) {
705 ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
706 goto err;
707 }
0f113f3e
MC
708 num = BN_num_bytes(rsa->n);
709 buf = OPENSSL_malloc(num);
e077455e 710 if (buf == NULL)
0f113f3e 711 goto err;
0f113f3e
MC
712
713 /*
714 * This check was for equality but PGP does evil things and chops off the
715 * top '0' bytes
716 */
717 if (flen > num) {
9311d0c4 718 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_GREATER_THAN_MOD_LEN);
0f113f3e
MC
719 goto err;
720 }
721
722 if (BN_bin2bn(from, flen, f) == NULL)
723 goto err;
724
725 if (BN_ucmp(f, rsa->n) >= 0) {
9311d0c4 726 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
0f113f3e
MC
727 goto err;
728 }
729
730 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
41bfd5e7
AP
731 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
732 rsa->n, ctx))
0f113f3e
MC
733 goto err;
734
735 if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
736 rsa->_method_mod_n))
737 goto err;
738
07e4d7f4 739 /* For X9.31: Assuming e is odd it does a 12 mod 16 test */
0f113f3e
MC
740 if ((padding == RSA_X931_PADDING) && ((bn_get_words(ret)[0] & 0xf) != 12))
741 if (!BN_sub(ret, rsa->n, ret))
742 goto err;
743
582ad5d4 744 i = BN_bn2binpad(ret, buf, num);
4a3dd629
P
745 if (i < 0)
746 goto err;
0f113f3e
MC
747
748 switch (padding) {
749 case RSA_PKCS1_PADDING:
750 r = RSA_padding_check_PKCS1_type_1(to, num, buf, i, num);
751 break;
752 case RSA_X931_PADDING:
753 r = RSA_padding_check_X931(to, num, buf, i, num);
754 break;
755 case RSA_NO_PADDING:
582ad5d4 756 memcpy(to, buf, (r = i));
0f113f3e
MC
757 break;
758 default:
9311d0c4 759 ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
0f113f3e
MC
760 goto err;
761 }
762 if (r < 0)
9311d0c4 763 ERR_raise(ERR_LIB_RSA, RSA_R_PADDING_CHECK_FAILED);
0f113f3e
MC
764
765 err:
ce1415ed 766 BN_CTX_end(ctx);
23a1d5e9 767 BN_CTX_free(ctx);
4b45c6e5 768 OPENSSL_clear_free(buf, num);
8686c474 769 return r;
0f113f3e 770}
58964a49 771
bf160551 772static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
0f113f3e 773{
afb638f1
MC
774 BIGNUM *r1, *m1, *vrfy;
775 int ret = 0, smooth = 0;
f844f9eb 776#ifndef FIPS_MODULE
afb638f1
MC
777 BIGNUM *r2, *m[RSA_MAX_PRIME_NUM - 2];
778 int i, ex_primes = 0;
665d899f 779 RSA_PRIME_INFO *pinfo;
afb638f1 780#endif
0f113f3e 781
c804d23d
PC
782 BN_CTX_start(ctx);
783
0f113f3e 784 r1 = BN_CTX_get(ctx);
f844f9eb 785#ifndef FIPS_MODULE
665d899f 786 r2 = BN_CTX_get(ctx);
afb638f1 787#endif
0f113f3e
MC
788 m1 = BN_CTX_get(ctx);
789 vrfy = BN_CTX_get(ctx);
5625567f
BE
790 if (vrfy == NULL)
791 goto err;
0f113f3e 792
f844f9eb 793#ifndef FIPS_MODULE
665d899f 794 if (rsa->version == RSA_ASN1_VERSION_MULTI
a1471588
BE
795 && ((ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) <= 0
796 || ex_primes > RSA_MAX_PRIME_NUM - 2))
665d899f 797 goto err;
afb638f1 798#endif
665d899f 799
41bfd5e7
AP
800 if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {
801 BIGNUM *factor = BN_new();
802
803 if (factor == NULL)
804 goto err;
0f113f3e
MC
805
806 /*
0d4fb843 807 * Make sure BN_mod_inverse in Montgomery initialization uses the
5584f65a 808 * BN_FLG_CONSTTIME flag
0f113f3e 809 */
41bfd5e7
AP
810 if (!(BN_with_flags(factor, rsa->p, BN_FLG_CONSTTIME),
811 BN_MONT_CTX_set_locked(&rsa->_method_mod_p, rsa->lock,
812 factor, ctx))
813 || !(BN_with_flags(factor, rsa->q, BN_FLG_CONSTTIME),
814 BN_MONT_CTX_set_locked(&rsa->_method_mod_q, rsa->lock,
815 factor, ctx))) {
816 BN_free(factor);
5584f65a 817 goto err;
0f113f3e 818 }
f844f9eb 819#ifndef FIPS_MODULE
41bfd5e7
AP
820 for (i = 0; i < ex_primes; i++) {
821 pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
822 BN_with_flags(factor, pinfo->r, BN_FLG_CONSTTIME);
823 if (!BN_MONT_CTX_set_locked(&pinfo->m, rsa->lock, factor, ctx)) {
824 BN_free(factor);
0f113f3e
MC
825 goto err;
826 }
827 }
afb638f1 828#endif
fd7d2520 829 /*
41bfd5e7 830 * We MUST free |factor| before any further use of the prime factors
fd7d2520 831 */
41bfd5e7
AP
832 BN_free(factor);
833
afb638f1 834 smooth = (rsa->meth->bn_mod_exp == BN_mod_exp_mont)
f844f9eb 835#ifndef FIPS_MODULE
afb638f1
MC
836 && (ex_primes == 0)
837#endif
41bfd5e7 838 && (BN_num_bits(rsa->q) == BN_num_bits(rsa->p));
0f113f3e
MC
839 }
840
841 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
41bfd5e7
AP
842 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
843 rsa->n, ctx))
844 goto err;
845
846 if (smooth) {
847 /*
848 * Conversion from Montgomery domain, a.k.a. Montgomery reduction,
849 * accepts values in [0-m*2^w) range. w is m's bit width rounded up
850 * to limb width. So that at the very least if |I| is fully reduced,
851 * i.e. less than p*q, we can count on from-to round to perform
852 * below modulo operations on |I|. Unlike BN_mod it's constant time.
853 */
854 if (/* m1 = I moq q */
855 !bn_from_mont_fixed_top(m1, I, rsa->_method_mod_q, ctx)
856 || !bn_to_mont_fixed_top(m1, m1, rsa->_method_mod_q, ctx)
41bfd5e7
AP
857 /* r1 = I mod p */
858 || !bn_from_mont_fixed_top(r1, I, rsa->_method_mod_p, ctx)
859 || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)
c781eb1c
AM
860 /*
861 * Use parallel exponentiations optimization if possible,
862 * otherwise fallback to two sequential exponentiations:
863 * m1 = m1^dmq1 mod q
864 * r1 = r1^dmp1 mod p
865 */
866 || !BN_mod_exp_mont_consttime_x2(m1, m1, rsa->dmq1, rsa->q,
867 rsa->_method_mod_q,
868 r1, r1, rsa->dmp1, rsa->p,
869 rsa->_method_mod_p,
870 ctx)
41bfd5e7
AP
871 /* r1 = (r1 - m1) mod p */
872 /*
873 * bn_mod_sub_fixed_top is not regular modular subtraction,
874 * it can tolerate subtrahend to be larger than modulus, but
875 * not bit-wise wider. This makes up for uncommon q>p case,
876 * when |m1| can be larger than |rsa->p|.
877 */
878 || !bn_mod_sub_fixed_top(r1, r1, m1, rsa->p)
879
d1c008f6 880 /* r1 = r1 * iqmp mod p */
41bfd5e7
AP
881 || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)
882 || !bn_mul_mont_fixed_top(r1, r1, rsa->iqmp, rsa->_method_mod_p,
883 ctx)
d1c008f6 884 /* r0 = r1 * q + m1 */
41bfd5e7
AP
885 || !bn_mul_fixed_top(r0, r1, rsa->q, ctx)
886 || !bn_mod_add_fixed_top(r0, r0, m1, rsa->n))
0f113f3e
MC
887 goto err;
888
41bfd5e7
AP
889 goto tail;
890 }
891
0f113f3e 892 /* compute I mod q */
fd7d2520 893 {
5584f65a
MC
894 BIGNUM *c = BN_new();
895 if (c == NULL)
896 goto err;
897 BN_with_flags(c, I, BN_FLG_CONSTTIME);
898
fd7d2520 899 if (!BN_mod(r1, c, rsa->q, ctx)) {
5584f65a 900 BN_free(c);
0f113f3e 901 goto err;
fd7d2520 902 }
0f113f3e 903
fd7d2520 904 {
5584f65a
MC
905 BIGNUM *dmq1 = BN_new();
906 if (dmq1 == NULL) {
907 BN_free(c);
908 goto err;
fd7d2520 909 }
5584f65a
MC
910 BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
911
912 /* compute r1^dmq1 mod q */
fd7d2520 913 if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx,
41bfd5e7 914 rsa->_method_mod_q)) {
5584f65a
MC
915 BN_free(c);
916 BN_free(dmq1);
fd7d2520
MC
917 goto err;
918 }
5584f65a
MC
919 /* We MUST free dmq1 before any further use of rsa->dmq1 */
920 BN_free(dmq1);
fd7d2520 921 }
0f113f3e 922
fd7d2520
MC
923 /* compute I mod p */
924 if (!BN_mod(r1, c, rsa->p, ctx)) {
5584f65a 925 BN_free(c);
0f113f3e 926 goto err;
fd7d2520 927 }
5584f65a
MC
928 /* We MUST free c before any further use of I */
929 BN_free(c);
0f113f3e
MC
930 }
931
fd7d2520 932 {
5584f65a
MC
933 BIGNUM *dmp1 = BN_new();
934 if (dmp1 == NULL)
935 goto err;
936 BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
937
fd7d2520 938 /* compute r1^dmp1 mod p */
fd7d2520
MC
939 if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx,
940 rsa->_method_mod_p)) {
5584f65a 941 BN_free(dmp1);
fd7d2520
MC
942 goto err;
943 }
5584f65a
MC
944 /* We MUST free dmp1 before any further use of rsa->dmp1 */
945 BN_free(dmp1);
fd7d2520 946 }
0f113f3e 947
f844f9eb 948#ifndef FIPS_MODULE
665d899f
PY
949 if (ex_primes > 0) {
950 BIGNUM *di = BN_new(), *cc = BN_new();
951
952 if (cc == NULL || di == NULL) {
953 BN_free(cc);
954 BN_free(di);
955 goto err;
956 }
957
958 for (i = 0; i < ex_primes; i++) {
959 /* prepare m_i */
960 if ((m[i] = BN_CTX_get(ctx)) == NULL) {
961 BN_free(cc);
962 BN_free(di);
963 goto err;
964 }
965
966 pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
967
968 /* prepare c and d_i */
969 BN_with_flags(cc, I, BN_FLG_CONSTTIME);
970 BN_with_flags(di, pinfo->d, BN_FLG_CONSTTIME);
971
972 if (!BN_mod(r1, cc, pinfo->r, ctx)) {
973 BN_free(cc);
974 BN_free(di);
975 goto err;
976 }
977 /* compute r1 ^ d_i mod r_i */
978 if (!rsa->meth->bn_mod_exp(m[i], r1, di, pinfo->r, ctx, pinfo->m)) {
979 BN_free(cc);
980 BN_free(di);
981 goto err;
982 }
983 }
984
985 BN_free(cc);
986 BN_free(di);
987 }
afb638f1 988#endif
665d899f 989
0f113f3e
MC
990 if (!BN_sub(r0, r0, m1))
991 goto err;
992 /*
993 * This will help stop the size of r0 increasing, which does affect the
994 * multiply if it optimised for a power of 2 size
995 */
996 if (BN_is_negative(r0))
997 if (!BN_add(r0, r0, rsa->p))
998 goto err;
999
1000 if (!BN_mul(r1, r0, rsa->iqmp, ctx))
1001 goto err;
1002
fd7d2520 1003 {
5584f65a
MC
1004 BIGNUM *pr1 = BN_new();
1005 if (pr1 == NULL)
1006 goto err;
1007 BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
1008
fd7d2520 1009 if (!BN_mod(r0, pr1, rsa->p, ctx)) {
5584f65a 1010 BN_free(pr1);
fd7d2520
MC
1011 goto err;
1012 }
5584f65a
MC
1013 /* We MUST free pr1 before any further use of r1 */
1014 BN_free(pr1);
fd7d2520 1015 }
0f113f3e
MC
1016
1017 /*
1018 * If p < q it is occasionally possible for the correction of adding 'p'
1019 * if r0 is negative above to leave the result still negative. This can
1020 * break the private key operations: the following second correction
1021 * should *always* correct this rare occurrence. This will *never* happen
1022 * with OpenSSL generated keys because they ensure p > q [steve]
1023 */
1024 if (BN_is_negative(r0))
1025 if (!BN_add(r0, r0, rsa->p))
1026 goto err;
1027 if (!BN_mul(r1, r0, rsa->q, ctx))
1028 goto err;
1029 if (!BN_add(r0, r1, m1))
1030 goto err;
1031
f844f9eb 1032#ifndef FIPS_MODULE
665d899f
PY
1033 /* add m_i to m in multi-prime case */
1034 if (ex_primes > 0) {
1035 BIGNUM *pr2 = BN_new();
1036
1037 if (pr2 == NULL)
1038 goto err;
1039
1040 for (i = 0; i < ex_primes; i++) {
1041 pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
1042 if (!BN_sub(r1, m[i], r0)) {
1043 BN_free(pr2);
1044 goto err;
1045 }
1046
1047 if (!BN_mul(r2, r1, pinfo->t, ctx)) {
1048 BN_free(pr2);
1049 goto err;
1050 }
1051
1052 BN_with_flags(pr2, r2, BN_FLG_CONSTTIME);
1053
1054 if (!BN_mod(r1, pr2, pinfo->r, ctx)) {
1055 BN_free(pr2);
1056 goto err;
1057 }
1058
1059 if (BN_is_negative(r1))
1060 if (!BN_add(r1, r1, pinfo->r)) {
1061 BN_free(pr2);
1062 goto err;
1063 }
1064 if (!BN_mul(r1, r1, pinfo->pp, ctx)) {
1065 BN_free(pr2);
1066 goto err;
1067 }
1068 if (!BN_add(r0, r0, r1)) {
1069 BN_free(pr2);
1070 goto err;
1071 }
1072 }
1073 BN_free(pr2);
1074 }
afb638f1 1075#endif
665d899f 1076
41bfd5e7 1077 tail:
0f113f3e 1078 if (rsa->e && rsa->n) {
41bfd5e7
AP
1079 if (rsa->meth->bn_mod_exp == BN_mod_exp_mont) {
1080 if (!BN_mod_exp_mont(vrfy, r0, rsa->e, rsa->n, ctx,
1081 rsa->_method_mod_n))
1082 goto err;
1083 } else {
1084 bn_correct_top(r0);
1085 if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,
1086 rsa->_method_mod_n))
1087 goto err;
1088 }
0f113f3e
MC
1089 /*
1090 * If 'I' was greater than (or equal to) rsa->n, the operation will
1091 * be equivalent to using 'I mod n'. However, the result of the
1092 * verify will *always* be less than 'n' so we don't check for
1093 * absolute equality, just congruency.
1094 */
1095 if (!BN_sub(vrfy, vrfy, I))
1096 goto err;
41bfd5e7
AP
1097 if (BN_is_zero(vrfy)) {
1098 bn_correct_top(r0);
1099 ret = 1;
1100 goto err; /* not actually error */
1101 }
0f113f3e
MC
1102 if (!BN_mod(vrfy, vrfy, rsa->n, ctx))
1103 goto err;
1104 if (BN_is_negative(vrfy))
1105 if (!BN_add(vrfy, vrfy, rsa->n))
1106 goto err;
1107 if (!BN_is_zero(vrfy)) {
1108 /*
1109 * 'I' and 'vrfy' aren't congruent mod n. Don't leak
1110 * miscalculated CRT output, just do a raw (slower) mod_exp and
1111 * return that instead.
1112 */
1113
5584f65a
MC
1114 BIGNUM *d = BN_new();
1115 if (d == NULL)
1116 goto err;
1117 BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
0f113f3e 1118
0f113f3e
MC
1119 if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx,
1120 rsa->_method_mod_n)) {
5584f65a 1121 BN_free(d);
0f113f3e
MC
1122 goto err;
1123 }
5584f65a
MC
1124 /* We MUST free d before any further use of rsa->d */
1125 BN_free(d);
0f113f3e
MC
1126 }
1127 }
41bfd5e7
AP
1128 /*
1129 * It's unfortunate that we have to bn_correct_top(r0). What hopefully
1130 * saves the day is that correction is highly unlike, and private key
1131 * operations are customarily performed on blinded message. Which means
1132 * that attacker won't observe correlation with chosen plaintext.
1133 * Secondly, remaining code would still handle it in same computational
1134 * time and even conceal memory access pattern around corrected top.
1135 */
1136 bn_correct_top(r0);
0f113f3e
MC
1137 ret = 1;
1138 err:
0f113f3e 1139 BN_CTX_end(ctx);
8686c474 1140 return ret;
0f113f3e 1141}
58964a49 1142
bf160551 1143static int rsa_ossl_init(RSA *rsa)
0f113f3e
MC
1144{
1145 rsa->flags |= RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE;
8686c474 1146 return 1;
0f113f3e 1147}
58964a49 1148
bf160551 1149static int rsa_ossl_finish(RSA *rsa)
0f113f3e 1150{
f844f9eb 1151#ifndef FIPS_MODULE
665d899f
PY
1152 int i;
1153 RSA_PRIME_INFO *pinfo;
1154
665d899f
PY
1155 for (i = 0; i < sk_RSA_PRIME_INFO_num(rsa->prime_infos); i++) {
1156 pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
1157 BN_MONT_CTX_free(pinfo->m);
1158 }
afb638f1
MC
1159#endif
1160
1161 BN_MONT_CTX_free(rsa->_method_mod_n);
1162 BN_MONT_CTX_free(rsa->_method_mod_p);
1163 BN_MONT_CTX_free(rsa->_method_mod_q);
8686c474 1164 return 1;
0f113f3e 1165}
79040cf2
JC
1166
1167#ifdef S390X_MOD_EXP
1168static int rsa_ossl_s390x_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa,
1169 BN_CTX *ctx)
1170{
1171 if (rsa->version != RSA_ASN1_VERSION_MULTI) {
1172 if (s390x_crt(r0, i, rsa->p, rsa->q, rsa->dmp1, rsa->dmq1, rsa->iqmp) == 1)
1173 return 1;
1174 }
1175 return rsa_ossl_mod_exp(r0, i, rsa, ctx);
1176}
1177
1178#endif