Coverage Report

Created: 2025-12-04 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/crypto/rsa/rsa_ossl.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 *
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
8
 */
9
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
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"
24
#endif
25
#include <openssl/evp.h>
26
#include <openssl/sha.h>
27
#include <openssl/hmac.h>
28
29
DEFINE_SPARSE_ARRAY_OF(BN_BLINDING);
30
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,
40
                           BN_CTX *ctx);
41
static int rsa_ossl_init(RSA *rsa);
42
static int rsa_ossl_finish(RSA *rsa);
43
#ifdef S390X_MOD_EXP
44
static int rsa_ossl_s390x_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa,
45
                                BN_CTX *ctx);
46
static RSA_METHOD rsa_pkcs1_ossl_meth = {
47
    "OpenSSL PKCS#1 RSA",
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,
53
    s390x_mod_exp,
54
    rsa_ossl_init,
55
    rsa_ossl_finish,
56
    RSA_FLAG_FIPS_METHOD,       /* flags */
57
    NULL,
58
    0,                          /* rsa_sign */
59
    0,                          /* rsa_verify */
60
    NULL,                       /* rsa_keygen */
61
    NULL                        /* rsa_multi_prime_keygen */
62
};
63
#else
64
static RSA_METHOD rsa_pkcs1_ossl_meth = {
65
    "OpenSSL PKCS#1 RSA",
66
    rsa_ossl_public_encrypt,
67
    rsa_ossl_public_decrypt,     /* signature verification */
68
    rsa_ossl_private_encrypt,    /* signing */
69
    rsa_ossl_private_decrypt,
70
    rsa_ossl_mod_exp,
71
    BN_mod_exp_mont,            /* XXX probably we should not use Montgomery
72
                                 * if e == 3 */
73
    rsa_ossl_init,
74
    rsa_ossl_finish,
75
    RSA_FLAG_FIPS_METHOD,       /* flags */
76
    NULL,
77
    0,                          /* rsa_sign */
78
    0,                          /* rsa_verify */
79
    NULL,                       /* rsa_keygen */
80
    NULL                        /* rsa_multi_prime_keygen */
81
};
82
#endif
83
84
static const RSA_METHOD *default_RSA_meth = &rsa_pkcs1_ossl_meth;
85
86
void RSA_set_default_method(const RSA_METHOD *meth)
87
0
{
88
0
    default_RSA_meth = meth;
89
0
}
90
91
const RSA_METHOD *RSA_get_default_method(void)
92
634k
{
93
634k
    return default_RSA_meth;
94
634k
}
95
96
const RSA_METHOD *RSA_PKCS1_OpenSSL(void)
97
215k
{
98
215k
    return &rsa_pkcs1_ossl_meth;
99
215k
}
100
101
const RSA_METHOD *RSA_null_method(void)
102
0
{
103
0
    return NULL;
104
0
}
105
106
static int rsa_ossl_public_encrypt(int flen, const unsigned char *from,
107
                                  unsigned char *to, RSA *rsa, int padding)
108
5.40k
{
109
5.40k
    BIGNUM *f, *ret;
110
5.40k
    int i, num = 0, r = -1;
111
5.40k
    unsigned char *buf = NULL;
112
5.40k
    BN_CTX *ctx = NULL;
113
114
5.40k
    if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
115
0
        ERR_raise(ERR_LIB_RSA, RSA_R_MODULUS_TOO_LARGE);
116
0
        return -1;
117
0
    }
118
119
5.40k
    if (BN_ucmp(rsa->n, rsa->e) <= 0) {
120
9
        ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
121
9
        return -1;
122
9
    }
123
124
    /* for large moduli, enforce exponent limit */
125
5.39k
    if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
126
6
        if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
127
0
            ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
128
0
            return -1;
129
0
        }
130
6
    }
131
132
5.39k
    if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
133
0
        goto err;
134
5.39k
    BN_CTX_start(ctx);
135
5.39k
    f = BN_CTX_get(ctx);
136
5.39k
    ret = BN_CTX_get(ctx);
137
5.39k
    num = BN_num_bytes(rsa->n);
138
5.39k
    buf = OPENSSL_malloc(num);
139
5.39k
    if (ret == NULL || buf == NULL)
140
0
        goto err;
141
142
5.39k
    switch (padding) {
143
5.39k
    case RSA_PKCS1_PADDING:
144
5.39k
        i = ossl_rsa_padding_add_PKCS1_type_2_ex(rsa->libctx, buf, num,
145
5.39k
                                                 from, flen);
146
5.39k
        break;
147
0
    case RSA_PKCS1_OAEP_PADDING:
148
0
        i = ossl_rsa_padding_add_PKCS1_OAEP_mgf1_ex(rsa->libctx, buf, num,
149
0
                                                    from, flen, NULL, 0,
150
0
                                                    NULL, NULL);
151
0
        break;
152
0
    case RSA_NO_PADDING:
153
0
        i = RSA_padding_add_none(buf, num, from, flen);
154
0
        break;
155
0
    default:
156
0
        ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
157
0
        goto err;
158
5.39k
    }
159
5.39k
    if (i <= 0)
160
38
        goto err;
161
162
5.35k
    if (BN_bin2bn(buf, num, f) == NULL)
163
0
        goto err;
164
165
#ifdef FIPS_MODULE
166
    /*
167
     * See SP800-56Br2, section 7.1.1.1
168
     * RSAEP: 1 < f < (n – 1).
169
     * (where f is the plaintext).
170
     */
171
    if (padding == RSA_NO_PADDING) {
172
        BIGNUM *nminus1 = BN_CTX_get(ctx);
173
174
        if (BN_ucmp(f, BN_value_one()) <= 0) {
175
            ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_SMALL);
176
            goto err;
177
        }
178
        if (nminus1 == NULL
179
                || BN_copy(nminus1, rsa->n) == NULL
180
                || !BN_sub_word(nminus1, 1))
181
            goto err;
182
        if (BN_ucmp(f, nminus1) >= 0) {
183
            ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
184
            goto err;
185
        }
186
    } else
187
#endif
188
5.35k
    {
189
5.35k
        if (BN_ucmp(f, rsa->n) >= 0) {
190
            /* usually the padding functions would catch this */
191
0
            ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
192
0
            goto err;
193
0
        }
194
5.35k
    }
195
196
5.35k
    if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
197
5.35k
        if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
198
5.35k
                                    rsa->n, ctx))
199
608
            goto err;
200
201
4.75k
    if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
202
4.75k
                               rsa->_method_mod_n))
203
0
        goto err;
204
205
    /*
206
     * BN_bn2binpad puts in leading 0 bytes if the number is less than
207
     * the length of the modulus.
208
     */
209
4.75k
    r = BN_bn2binpad(ret, to, num);
210
5.39k
 err:
211
5.39k
    BN_CTX_end(ctx);
212
5.39k
    BN_CTX_free(ctx);
213
5.39k
    OPENSSL_clear_free(buf, num);
214
5.39k
    return r;
215
4.75k
}
216
217
#if defined(OPENSSL_SYS_TANDEM)
218
static TSAN_QUALIFIER uint64_t tsan_thread_id = 1;
219
#endif
220
221
static uintptr_t get_unique_thread_id(void)
222
9.62k
{
223
#if defined(OPENSSL_SYS_TANDEM)
224
    uintptr_t thread_id = (uintptr_t)CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_TANDEM_ID_KEY,
225
                                                                NULL);
226
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);
230
    }
231
    return thread_id;
232
#else
233
9.62k
    return (uintptr_t)CRYPTO_THREAD_get_current_id();
234
9.62k
#endif
235
9.62k
}
236
237
static void free_bn_blinding(ossl_uintmax_t idx, BN_BLINDING *b, void *arg)
238
4.81k
{
239
4.81k
    BN_BLINDING_free(b);
240
4.81k
}
241
242
void ossl_rsa_free_blinding(RSA *rsa)
243
161k
{
244
161k
    SPARSE_ARRAY_OF(BN_BLINDING) *blindings = rsa->blindings_sa;
245
246
161k
    ossl_sa_BN_BLINDING_doall_arg(blindings, free_bn_blinding, NULL);
247
161k
    ossl_sa_BN_BLINDING_free(blindings);
248
161k
}
249
250
void *ossl_rsa_alloc_blinding(void)
251
161k
{
252
161k
    return ossl_sa_BN_BLINDING_new();
253
161k
}
254
255
static BN_BLINDING *ossl_rsa_get_thread_bn_blinding(RSA *rsa)
256
4.81k
{
257
4.81k
    SPARSE_ARRAY_OF(BN_BLINDING) *blindings = rsa->blindings_sa;
258
4.81k
    uintptr_t tid = get_unique_thread_id();
259
260
4.81k
    return ossl_sa_BN_BLINDING_get(blindings, tid);
261
4.81k
}
262
263
static int ossl_rsa_set_thread_bn_blinding(RSA *rsa, BN_BLINDING *b)
264
4.81k
{
265
4.81k
    SPARSE_ARRAY_OF(BN_BLINDING) *blindings = rsa->blindings_sa;
266
4.81k
    uintptr_t tid = get_unique_thread_id();
267
268
4.81k
    return ossl_sa_BN_BLINDING_set(blindings, tid, b);
269
4.81k
}
270
271
static BN_BLINDING *rsa_get_blinding(RSA *rsa, BN_CTX *ctx)
272
4.81k
{
273
4.81k
    BN_BLINDING *ret;
274
275
4.81k
    if (!CRYPTO_THREAD_read_lock(rsa->lock))
276
0
        return NULL;
277
278
4.81k
    ret = ossl_rsa_get_thread_bn_blinding(rsa);
279
4.81k
    CRYPTO_THREAD_unlock(rsa->lock);
280
281
4.81k
    if (ret == NULL) {
282
4.81k
        ret = RSA_setup_blinding(rsa, ctx);
283
4.81k
        if (!CRYPTO_THREAD_write_lock(rsa->lock)) {
284
0
            BN_BLINDING_free(ret);
285
0
            ret = NULL;
286
4.81k
        } else {
287
4.81k
            if (!ossl_rsa_set_thread_bn_blinding(rsa, ret)) {
288
0
                BN_BLINDING_free(ret);
289
0
                ret = NULL;
290
0
            }
291
4.81k
            CRYPTO_THREAD_unlock(rsa->lock);
292
4.81k
        }
293
4.81k
    }
294
295
4.81k
    return ret;
296
4.81k
}
297
298
static int rsa_blinding_convert(BN_BLINDING *b, BIGNUM *f, BN_CTX *ctx)
299
4.81k
{
300
    /*
301
     * Local blinding: store the unblinding factor in BN_BLINDING.
302
     */
303
4.81k
    return BN_BLINDING_convert_ex(f, NULL, b, ctx);
304
4.81k
}
305
306
static int rsa_blinding_invert(BN_BLINDING *b, BIGNUM *f, BN_CTX *ctx)
307
16.3k
{
308
    /*
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.
315
     */
316
16.3k
    BN_set_flags(f, BN_FLG_CONSTTIME);
317
16.3k
    return BN_BLINDING_invert_ex(f, NULL, b, ctx);
318
16.3k
}
319
320
/* signing */
321
static int rsa_ossl_private_encrypt(int flen, const unsigned char *from,
322
                                   unsigned char *to, RSA *rsa, int padding)
323
2.18k
{
324
2.18k
    BIGNUM *f, *ret, *res;
325
2.18k
    int i, num = 0, r = -1;
326
2.18k
    unsigned char *buf = NULL;
327
2.18k
    BN_CTX *ctx = NULL;
328
2.18k
    BN_BLINDING *blinding = NULL;
329
330
2.18k
    if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
331
0
        goto err;
332
2.18k
    BN_CTX_start(ctx);
333
2.18k
    f = BN_CTX_get(ctx);
334
2.18k
    ret = BN_CTX_get(ctx);
335
2.18k
    num = BN_num_bytes(rsa->n);
336
2.18k
    buf = OPENSSL_malloc(num);
337
2.18k
    if (ret == NULL || buf == NULL)
338
0
        goto err;
339
340
2.18k
    switch (padding) {
341
1.56k
    case RSA_PKCS1_PADDING:
342
1.56k
        i = RSA_padding_add_PKCS1_type_1(buf, num, from, flen);
343
1.56k
        break;
344
0
    case RSA_X931_PADDING:
345
0
        i = RSA_padding_add_X931(buf, num, from, flen);
346
0
        break;
347
622
    case RSA_NO_PADDING:
348
622
        i = RSA_padding_add_none(buf, num, from, flen);
349
622
        break;
350
0
    default:
351
0
        ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
352
0
        goto err;
353
2.18k
    }
354
2.18k
    if (i <= 0)
355
0
        goto err;
356
357
2.18k
    if (BN_bin2bn(buf, num, f) == NULL)
358
0
        goto err;
359
360
2.18k
    if (BN_ucmp(f, rsa->n) >= 0) {
361
        /* usually the padding functions would catch this */
362
0
        ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
363
0
        goto err;
364
0
    }
365
366
2.18k
    if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
367
2.18k
        if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
368
2.18k
                                    rsa->n, ctx))
369
0
            goto err;
370
371
2.18k
    if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
372
2.18k
        blinding = rsa_get_blinding(rsa, ctx);
373
2.18k
        if (blinding == NULL) {
374
0
            ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
375
0
            goto err;
376
0
        }
377
378
2.18k
        if (!rsa_blinding_convert(blinding, f, ctx))
379
0
            goto err;
380
2.18k
    }
381
382
2.18k
    if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
383
2.18k
        (rsa->version == RSA_ASN1_VERSION_MULTI) ||
384
2.18k
        ((rsa->p != NULL) &&
385
2.18k
         (rsa->q != NULL) &&
386
2.18k
         (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
387
2.18k
        if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
388
0
            goto err;
389
2.18k
    } else {
390
0
        BIGNUM *d = BN_new();
391
0
        if (d == NULL) {
392
0
            ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
393
0
            goto err;
394
0
        }
395
0
        if (rsa->d == NULL) {
396
0
            ERR_raise(ERR_LIB_RSA, RSA_R_MISSING_PRIVATE_KEY);
397
0
            BN_free(d);
398
0
            goto err;
399
0
        }
400
0
        BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
401
402
0
        if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
403
0
                                   rsa->_method_mod_n)) {
404
0
            BN_free(d);
405
0
            goto err;
406
0
        }
407
        /* We MUST free d before any further use of rsa->d */
408
0
        BN_free(d);
409
0
    }
410
411
2.18k
    if (blinding)
412
2.18k
        if (!rsa_blinding_invert(blinding, ret, ctx))
413
0
            goto err;
414
415
2.18k
    if (padding == RSA_X931_PADDING) {
416
0
        if (!BN_sub(f, rsa->n, ret))
417
0
            goto err;
418
0
        if (BN_cmp(ret, f) > 0)
419
0
            res = f;
420
0
        else
421
0
            res = ret;
422
2.18k
    } else {
423
2.18k
        res = ret;
424
2.18k
    }
425
426
    /*
427
     * BN_bn2binpad puts in leading 0 bytes if the number is less than
428
     * the length of the modulus.
429
     */
430
2.18k
    r = BN_bn2binpad(res, to, num);
431
2.18k
 err:
432
2.18k
    BN_CTX_end(ctx);
433
2.18k
    BN_CTX_free(ctx);
434
2.18k
    OPENSSL_clear_free(buf, num);
435
2.18k
    return r;
436
2.18k
}
437
438
static int derive_kdk(int flen, const unsigned char *from, RSA *rsa,
439
                      unsigned char *buf, int num, unsigned char *kdk)
440
0
{
441
0
    int ret = 0;
442
0
    HMAC_CTX *hmac = NULL;
443
0
    EVP_MD *md = NULL;
444
0
    unsigned int md_len = SHA256_DIGEST_LENGTH;
445
0
    unsigned char d_hash[SHA256_DIGEST_LENGTH] = {0};
446
    /*
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
449
     */
450
0
    BIGNUM *d = BN_new();
451
452
0
    if (d == NULL) {
453
0
        ERR_raise(ERR_LIB_RSA, ERR_R_CRYPTO_LIB);
454
0
        goto err;
455
0
    }
456
0
    if (rsa->d == NULL) {
457
0
        ERR_raise(ERR_LIB_RSA, RSA_R_MISSING_PRIVATE_KEY);
458
0
        BN_free(d);
459
0
        goto err;
460
0
    }
461
0
    BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
462
0
    if (BN_bn2binpad(d, buf, num) < 0) {
463
0
        ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
464
0
        BN_free(d);
465
0
        goto err;
466
0
    }
467
0
    BN_free(d);
468
469
    /*
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
475
     */
476
0
    md = EVP_MD_fetch(rsa->libctx, "sha256", NULL);
477
0
    if (md == NULL) {
478
0
        ERR_raise(ERR_LIB_RSA, ERR_R_FETCH_FAILED);
479
0
        goto err;
480
0
    }
481
482
0
    if (EVP_Digest(buf, num, d_hash, NULL, md, NULL) <= 0) {
483
0
        ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
484
0
        goto err;
485
0
    }
486
487
0
    hmac = HMAC_CTX_new();
488
0
    if (hmac == NULL) {
489
0
        ERR_raise(ERR_LIB_RSA, ERR_R_CRYPTO_LIB);
490
0
        goto err;
491
0
    }
492
493
0
    if (HMAC_Init_ex(hmac, d_hash, sizeof(d_hash), md, NULL) <= 0) {
494
0
        ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
495
0
        goto err;
496
0
    }
497
498
0
    if (flen < num) {
499
0
        memset(buf, 0, num - flen);
500
0
        if (HMAC_Update(hmac, buf, num - flen) <= 0) {
501
0
            ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
502
0
            goto err;
503
0
        }
504
0
    }
505
0
    if (HMAC_Update(hmac, from, flen) <= 0) {
506
0
        ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
507
0
        goto err;
508
0
    }
509
510
0
    md_len = SHA256_DIGEST_LENGTH;
511
0
    if (HMAC_Final(hmac, kdk, &md_len) <= 0) {
512
0
        ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
513
0
        goto err;
514
0
    }
515
0
    ret = 1;
516
517
0
 err:
518
0
    HMAC_CTX_free(hmac);
519
0
    EVP_MD_free(md);
520
0
    return ret;
521
0
}
522
523
static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
524
                                   unsigned char *to, RSA *rsa, int padding)
525
2.63k
{
526
2.63k
    BIGNUM *f, *ret;
527
2.63k
    int j, num = 0, r = -1;
528
2.63k
    unsigned char *buf = NULL;
529
2.63k
    unsigned char kdk[SHA256_DIGEST_LENGTH] = {0};
530
2.63k
    BN_CTX *ctx = NULL;
531
2.63k
    BN_BLINDING *blinding = NULL;
532
533
    /*
534
     * we need the value of the private exponent to perform implicit rejection
535
     */
536
2.63k
    if ((rsa->flags & RSA_FLAG_EXT_PKEY) && (padding == RSA_PKCS1_PADDING))
537
0
        padding = RSA_PKCS1_NO_IMPLICIT_REJECT_PADDING;
538
539
2.63k
    if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
540
0
        goto err;
541
2.63k
    BN_CTX_start(ctx);
542
2.63k
    f = BN_CTX_get(ctx);
543
2.63k
    ret = BN_CTX_get(ctx);
544
2.63k
    if (ret == NULL) {
545
0
        ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
546
0
        goto err;
547
0
    }
548
2.63k
    num = BN_num_bytes(rsa->n);
549
2.63k
    buf = OPENSSL_malloc(num);
550
2.63k
    if (buf == NULL)
551
0
        goto err;
552
553
    /*
554
     * This check was for equality but PGP does evil things and chops off the
555
     * top '0' bytes
556
     */
557
2.63k
    if (flen > num) {
558
0
        ERR_raise(ERR_LIB_RSA, RSA_R_DATA_GREATER_THAN_MOD_LEN);
559
0
        goto err;
560
0
    }
561
562
2.63k
    if (flen < 1) {
563
6
        ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_SMALL);
564
6
        goto err;
565
6
    }
566
567
    /* make data into a big number */
568
2.62k
    if (BN_bin2bn(from, (int)flen, f) == NULL)
569
0
        goto err;
570
571
#ifdef FIPS_MODULE
572
    /*
573
     * See SP800-56Br2, section 7.1.2.1
574
     * RSADP: 1 < f < (n – 1)
575
     * (where f is the ciphertext).
576
     */
577
    if (padding == RSA_NO_PADDING) {
578
        BIGNUM *nminus1 = BN_CTX_get(ctx);
579
580
        if (BN_ucmp(f, BN_value_one()) <= 0) {
581
            ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_SMALL);
582
            goto err;
583
        }
584
        if (nminus1 == NULL
585
                || BN_copy(nminus1, rsa->n) == NULL
586
                || !BN_sub_word(nminus1, 1))
587
            goto err;
588
        if (BN_ucmp(f, nminus1) >= 0) {
589
            ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
590
            goto err;
591
        }
592
    } else
593
#endif
594
2.62k
    {
595
2.62k
        if (BN_ucmp(f, rsa->n) >= 0) {
596
3
            ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
597
3
            goto err;
598
3
        }
599
2.62k
    }
600
2.62k
    if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
601
2.62k
        if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
602
2.62k
                                    rsa->n, ctx))
603
0
            goto err;
604
605
2.62k
    if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
606
2.62k
        blinding = rsa_get_blinding(rsa, ctx);
607
2.62k
        if (blinding == NULL) {
608
0
            ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
609
0
            goto err;
610
0
        }
611
612
2.62k
        if (!rsa_blinding_convert(blinding, f, ctx))
613
0
            goto err;
614
2.62k
    }
615
616
    /* do the decrypt */
617
2.62k
    if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
618
2.62k
        (rsa->version == RSA_ASN1_VERSION_MULTI) ||
619
2.62k
        ((rsa->p != NULL) &&
620
2.62k
         (rsa->q != NULL) &&
621
2.62k
         (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
622
2.62k
        if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
623
0
            goto err;
624
2.62k
    } else {
625
0
        BIGNUM *d = BN_new();
626
0
        if (d == NULL) {
627
0
            ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
628
0
            goto err;
629
0
        }
630
0
        if (rsa->d == NULL) {
631
0
            ERR_raise(ERR_LIB_RSA, RSA_R_MISSING_PRIVATE_KEY);
632
0
            BN_free(d);
633
0
            goto err;
634
0
        }
635
0
        BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
636
0
        if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
637
0
                                   rsa->_method_mod_n)) {
638
0
            BN_free(d);
639
0
            goto err;
640
0
        }
641
        /* We MUST free d before any further use of rsa->d */
642
0
        BN_free(d);
643
0
    }
644
645
2.62k
    if (blinding)
646
2.62k
        if (!rsa_blinding_invert(blinding, ret, ctx))
647
0
            goto err;
648
649
    /*
650
     * derive the Key Derivation Key from private exponent and public
651
     * ciphertext
652
     */
653
2.62k
    if (padding == RSA_PKCS1_PADDING) {
654
0
        if (derive_kdk(flen, from, rsa, buf, num, kdk) == 0)
655
0
            goto err;
656
0
    }
657
658
2.62k
    j = BN_bn2binpad(ret, buf, num);
659
2.62k
    if (j < 0)
660
0
        goto err;
661
662
2.62k
    switch (padding) {
663
0
    case RSA_PKCS1_NO_IMPLICIT_REJECT_PADDING:
664
0
        r = RSA_padding_check_PKCS1_type_2(to, num, buf, j, num);
665
0
        break;
666
0
    case RSA_PKCS1_PADDING:
667
0
        r = ossl_rsa_padding_check_PKCS1_type_2(rsa->libctx, to, num, buf, j, num, kdk);
668
0
        break;
669
0
    case RSA_PKCS1_OAEP_PADDING:
670
0
        r = RSA_padding_check_PKCS1_OAEP(to, num, buf, j, num, NULL, 0);
671
0
        break;
672
2.62k
    case RSA_NO_PADDING:
673
2.62k
        memcpy(to, buf, (r = j));
674
2.62k
        break;
675
0
    default:
676
0
        ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
677
0
        goto err;
678
2.62k
    }
679
2.62k
#ifndef FIPS_MODULE
680
    /*
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.
684
     */
685
2.62k
    ERR_raise(ERR_LIB_RSA, RSA_R_PADDING_CHECK_FAILED);
686
2.62k
    err_clear_last_constant_time(1 & ~constant_time_msb(r));
687
2.62k
#endif
688
689
2.63k
 err:
690
2.63k
    BN_CTX_end(ctx);
691
2.63k
    BN_CTX_free(ctx);
692
2.63k
    OPENSSL_clear_free(buf, num);
693
2.63k
    return r;
694
2.62k
}
695
696
/* signature verification */
697
static int rsa_ossl_public_decrypt(int flen, const unsigned char *from,
698
                                  unsigned char *to, RSA *rsa, int padding)
699
28.9k
{
700
28.9k
    BIGNUM *f, *ret;
701
28.9k
    int i, num = 0, r = -1;
702
28.9k
    unsigned char *buf = NULL;
703
28.9k
    BN_CTX *ctx = NULL;
704
705
28.9k
    if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
706
0
        ERR_raise(ERR_LIB_RSA, RSA_R_MODULUS_TOO_LARGE);
707
0
        return -1;
708
0
    }
709
710
28.9k
    if (BN_ucmp(rsa->n, rsa->e) <= 0) {
711
51
        ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
712
51
        return -1;
713
51
    }
714
715
    /* for large moduli, enforce exponent limit */
716
28.9k
    if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
717
23
        if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
718
0
            ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
719
0
            return -1;
720
0
        }
721
23
    }
722
723
28.9k
    if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
724
0
        goto err;
725
28.9k
    BN_CTX_start(ctx);
726
28.9k
    f = BN_CTX_get(ctx);
727
28.9k
    ret = BN_CTX_get(ctx);
728
28.9k
    if (ret == NULL) {
729
0
        ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
730
0
        goto err;
731
0
    }
732
28.9k
    num = BN_num_bytes(rsa->n);
733
28.9k
    buf = OPENSSL_malloc(num);
734
28.9k
    if (buf == NULL)
735
0
        goto err;
736
737
    /*
738
     * This check was for equality but PGP does evil things and chops off the
739
     * top '0' bytes
740
     */
741
28.9k
    if (flen > num) {
742
1.70k
        ERR_raise(ERR_LIB_RSA, RSA_R_DATA_GREATER_THAN_MOD_LEN);
743
1.70k
        goto err;
744
1.70k
    }
745
746
27.2k
    if (BN_bin2bn(from, flen, f) == NULL)
747
0
        goto err;
748
749
27.2k
    if (BN_ucmp(f, rsa->n) >= 0) {
750
1.25k
        ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
751
1.25k
        goto err;
752
1.25k
    }
753
754
25.9k
    if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
755
25.9k
        if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
756
25.9k
                                    rsa->n, ctx))
757
2.59k
            goto err;
758
759
23.3k
    if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
760
23.3k
                               rsa->_method_mod_n))
761
0
        goto err;
762
763
    /* For X9.31: Assuming e is odd it does a 12 mod 16 test */
764
23.3k
    if ((padding == RSA_X931_PADDING) && ((bn_get_words(ret)[0] & 0xf) != 12))
765
0
        if (!BN_sub(ret, rsa->n, ret))
766
0
            goto err;
767
768
23.3k
    i = BN_bn2binpad(ret, buf, num);
769
23.3k
    if (i < 0)
770
0
        goto err;
771
772
23.3k
    switch (padding) {
773
5.74k
    case RSA_PKCS1_PADDING:
774
5.74k
        r = RSA_padding_check_PKCS1_type_1(to, num, buf, i, num);
775
5.74k
        break;
776
0
    case RSA_X931_PADDING:
777
0
        r = RSA_padding_check_X931(to, num, buf, i, num);
778
0
        break;
779
17.6k
    case RSA_NO_PADDING:
780
17.6k
        memcpy(to, buf, (r = i));
781
17.6k
        break;
782
0
    default:
783
0
        ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
784
0
        goto err;
785
23.3k
    }
786
23.3k
    if (r < 0)
787
23.3k
        ERR_raise(ERR_LIB_RSA, RSA_R_PADDING_CHECK_FAILED);
788
789
28.9k
 err:
790
28.9k
    BN_CTX_end(ctx);
791
28.9k
    BN_CTX_free(ctx);
792
28.9k
    OPENSSL_clear_free(buf, num);
793
28.9k
    return r;
794
23.3k
}
795
796
static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
797
16.3k
{
798
16.3k
    BIGNUM *r1, *m1, *vrfy;
799
16.3k
    int ret = 0, smooth = 0;
800
16.3k
#ifndef FIPS_MODULE
801
16.3k
    BIGNUM *r2, *m[RSA_MAX_PRIME_NUM - 2];
802
16.3k
    int i, ex_primes = 0;
803
16.3k
    RSA_PRIME_INFO *pinfo;
804
16.3k
#endif
805
806
16.3k
    BN_CTX_start(ctx);
807
808
16.3k
    r1 = BN_CTX_get(ctx);
809
16.3k
#ifndef FIPS_MODULE
810
16.3k
    r2 = BN_CTX_get(ctx);
811
16.3k
#endif
812
16.3k
    m1 = BN_CTX_get(ctx);
813
16.3k
    vrfy = BN_CTX_get(ctx);
814
16.3k
    if (vrfy == NULL)
815
0
        goto err;
816
817
16.3k
#ifndef FIPS_MODULE
818
16.3k
    if (rsa->version == RSA_ASN1_VERSION_MULTI
819
0
        && ((ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) <= 0
820
0
             || ex_primes > RSA_MAX_PRIME_NUM - 2))
821
0
        goto err;
822
16.3k
#endif
823
824
16.3k
    if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {
825
16.3k
        BIGNUM *factor = BN_new();
826
827
16.3k
        if (factor == NULL)
828
0
            goto err;
829
830
        /*
831
         * Make sure BN_mod_inverse in Montgomery initialization uses the
832
         * BN_FLG_CONSTTIME flag
833
         */
834
16.3k
        if (!(BN_with_flags(factor, rsa->p, BN_FLG_CONSTTIME),
835
16.3k
              BN_MONT_CTX_set_locked(&rsa->_method_mod_p, rsa->lock,
836
16.3k
                                     factor, ctx))
837
16.3k
            || !(BN_with_flags(factor, rsa->q, BN_FLG_CONSTTIME),
838
16.3k
                 BN_MONT_CTX_set_locked(&rsa->_method_mod_q, rsa->lock,
839
16.3k
                                        factor, ctx))) {
840
0
            BN_free(factor);
841
0
            goto err;
842
0
        }
843
16.3k
#ifndef FIPS_MODULE
844
16.3k
        for (i = 0; i < ex_primes; i++) {
845
0
            pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
846
0
            BN_with_flags(factor, pinfo->r, BN_FLG_CONSTTIME);
847
0
            if (!BN_MONT_CTX_set_locked(&pinfo->m, rsa->lock, factor, ctx)) {
848
0
                BN_free(factor);
849
0
                goto err;
850
0
            }
851
0
        }
852
16.3k
#endif
853
        /*
854
         * We MUST free |factor| before any further use of the prime factors
855
         */
856
16.3k
        BN_free(factor);
857
858
16.3k
        smooth = (rsa->meth->bn_mod_exp == BN_mod_exp_mont)
859
16.3k
#ifndef FIPS_MODULE
860
16.3k
                 && (ex_primes == 0)
861
16.3k
#endif
862
16.3k
                 && (BN_num_bits(rsa->q) == BN_num_bits(rsa->p));
863
16.3k
    }
864
865
16.3k
    if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
866
16.3k
        if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
867
16.3k
                                    rsa->n, ctx))
868
0
            goto err;
869
870
16.3k
    if (smooth) {
871
        /*
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.
877
         */
878
16.3k
        if (/* m1 = I moq q */
879
16.3k
            !bn_from_mont_fixed_top(m1, I, rsa->_method_mod_q, ctx)
880
16.3k
            || !bn_to_mont_fixed_top(m1, m1, rsa->_method_mod_q, ctx)
881
            /* r1 = I mod p */
882
16.3k
            || !bn_from_mont_fixed_top(r1, I, rsa->_method_mod_p, ctx)
883
16.3k
            || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)
884
            /*
885
             * Use parallel exponentiations optimization if possible,
886
             * otherwise fallback to two sequential exponentiations:
887
             *    m1 = m1^dmq1 mod q
888
             *    r1 = r1^dmp1 mod p
889
             */
890
16.3k
            || !BN_mod_exp_mont_consttime_x2(m1, m1, rsa->dmq1, rsa->q,
891
16.3k
                                             rsa->_method_mod_q,
892
16.3k
                                             r1, r1, rsa->dmp1, rsa->p,
893
16.3k
                                             rsa->_method_mod_p,
894
16.3k
                                             ctx)
895
            /* r1 = (r1 - m1) mod p */
896
            /*
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|.
901
             */
902
16.3k
            || !bn_mod_sub_fixed_top(r1, r1, m1, rsa->p)
903
904
            /* r1 = r1 * iqmp mod p */
905
16.3k
            || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)
906
16.3k
            || !bn_mul_mont_fixed_top(r1, r1, rsa->iqmp, rsa->_method_mod_p,
907
16.3k
                                      ctx)
908
            /* r0 = r1 * q + m1 */
909
16.3k
            || !bn_mul_fixed_top(r0, r1, rsa->q, ctx)
910
16.3k
            || !bn_mod_add_fixed_top(r0, r0, m1, rsa->n))
911
0
            goto err;
912
913
16.3k
        goto tail;
914
16.3k
    }
915
916
    /* compute I mod q */
917
0
    {
918
0
        BIGNUM *c = BN_new();
919
0
        if (c == NULL)
920
0
            goto err;
921
0
        BN_with_flags(c, I, BN_FLG_CONSTTIME);
922
923
0
        if (!BN_mod(r1, c, rsa->q, ctx)) {
924
0
            BN_free(c);
925
0
            goto err;
926
0
        }
927
928
0
        {
929
0
            BIGNUM *dmq1 = BN_new();
930
0
            if (dmq1 == NULL) {
931
0
                BN_free(c);
932
0
                goto err;
933
0
            }
934
0
            BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
935
936
            /* compute r1^dmq1 mod q */
937
0
            if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx,
938
0
                                       rsa->_method_mod_q)) {
939
0
                BN_free(c);
940
0
                BN_free(dmq1);
941
0
                goto err;
942
0
            }
943
            /* We MUST free dmq1 before any further use of rsa->dmq1 */
944
0
            BN_free(dmq1);
945
0
        }
946
947
        /* compute I mod p */
948
0
        if (!BN_mod(r1, c, rsa->p, ctx)) {
949
0
            BN_free(c);
950
0
            goto err;
951
0
        }
952
        /* We MUST free c before any further use of I */
953
0
        BN_free(c);
954
0
    }
955
956
0
    {
957
0
        BIGNUM *dmp1 = BN_new();
958
0
        if (dmp1 == NULL)
959
0
            goto err;
960
0
        BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
961
962
        /* compute r1^dmp1 mod p */
963
0
        if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx,
964
0
                                   rsa->_method_mod_p)) {
965
0
            BN_free(dmp1);
966
0
            goto err;
967
0
        }
968
        /* We MUST free dmp1 before any further use of rsa->dmp1 */
969
0
        BN_free(dmp1);
970
0
    }
971
972
0
#ifndef FIPS_MODULE
973
0
    if (ex_primes > 0) {
974
0
        BIGNUM *di = BN_new(), *cc = BN_new();
975
976
0
        if (cc == NULL || di == NULL) {
977
0
            BN_free(cc);
978
0
            BN_free(di);
979
0
            goto err;
980
0
        }
981
982
0
        for (i = 0; i < ex_primes; i++) {
983
            /* prepare m_i */
984
0
            if ((m[i] = BN_CTX_get(ctx)) == NULL) {
985
0
                BN_free(cc);
986
0
                BN_free(di);
987
0
                goto err;
988
0
            }
989
990
0
            pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
991
992
            /* prepare c and d_i */
993
0
            BN_with_flags(cc, I, BN_FLG_CONSTTIME);
994
0
            BN_with_flags(di, pinfo->d, BN_FLG_CONSTTIME);
995
996
0
            if (!BN_mod(r1, cc, pinfo->r, ctx)) {
997
0
                BN_free(cc);
998
0
                BN_free(di);
999
0
                goto err;
1000
0
            }
1001
            /* compute r1 ^ d_i mod r_i */
1002
0
            if (!rsa->meth->bn_mod_exp(m[i], r1, di, pinfo->r, ctx, pinfo->m)) {
1003
0
                BN_free(cc);
1004
0
                BN_free(di);
1005
0
                goto err;
1006
0
            }
1007
0
        }
1008
1009
0
        BN_free(cc);
1010
0
        BN_free(di);
1011
0
    }
1012
0
#endif
1013
1014
0
    if (!BN_sub(r0, r0, m1))
1015
0
        goto err;
1016
    /*
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
1019
     */
1020
0
    if (BN_is_negative(r0))
1021
0
        if (!BN_add(r0, r0, rsa->p))
1022
0
            goto err;
1023
1024
0
    if (!BN_mul(r1, r0, rsa->iqmp, ctx))
1025
0
        goto err;
1026
1027
0
    {
1028
0
        BIGNUM *pr1 = BN_new();
1029
0
        if (pr1 == NULL)
1030
0
            goto err;
1031
0
        BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
1032
1033
0
        if (!BN_mod(r0, pr1, rsa->p, ctx)) {
1034
0
            BN_free(pr1);
1035
0
            goto err;
1036
0
        }
1037
        /* We MUST free pr1 before any further use of r1 */
1038
0
        BN_free(pr1);
1039
0
    }
1040
1041
    /*
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]
1047
     */
1048
0
    if (BN_is_negative(r0))
1049
0
        if (!BN_add(r0, r0, rsa->p))
1050
0
            goto err;
1051
0
    if (!BN_mul(r1, r0, rsa->q, ctx))
1052
0
        goto err;
1053
0
    if (!BN_add(r0, r1, m1))
1054
0
        goto err;
1055
1056
0
#ifndef FIPS_MODULE
1057
    /* add m_i to m in multi-prime case */
1058
0
    if (ex_primes > 0) {
1059
0
        BIGNUM *pr2 = BN_new();
1060
1061
0
        if (pr2 == NULL)
1062
0
            goto err;
1063
1064
0
        for (i = 0; i < ex_primes; i++) {
1065
0
            pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
1066
0
            if (!BN_sub(r1, m[i], r0)) {
1067
0
                BN_free(pr2);
1068
0
                goto err;
1069
0
            }
1070
1071
0
            if (!BN_mul(r2, r1, pinfo->t, ctx)) {
1072
0
                BN_free(pr2);
1073
0
                goto err;
1074
0
            }
1075
1076
0
            BN_with_flags(pr2, r2, BN_FLG_CONSTTIME);
1077
1078
0
            if (!BN_mod(r1, pr2, pinfo->r, ctx)) {
1079
0
                BN_free(pr2);
1080
0
                goto err;
1081
0
            }
1082
1083
0
            if (BN_is_negative(r1))
1084
0
                if (!BN_add(r1, r1, pinfo->r)) {
1085
0
                    BN_free(pr2);
1086
0
                    goto err;
1087
0
                }
1088
0
            if (!BN_mul(r1, r1, pinfo->pp, ctx)) {
1089
0
                BN_free(pr2);
1090
0
                goto err;
1091
0
            }
1092
0
            if (!BN_add(r0, r0, r1)) {
1093
0
                BN_free(pr2);
1094
0
                goto err;
1095
0
            }
1096
0
        }
1097
0
        BN_free(pr2);
1098
0
    }
1099
0
#endif
1100
1101
16.3k
 tail:
1102
16.3k
    if (rsa->e && rsa->n) {
1103
16.3k
        if (rsa->meth->bn_mod_exp == BN_mod_exp_mont) {
1104
16.3k
            if (!BN_mod_exp_mont(vrfy, r0, rsa->e, rsa->n, ctx,
1105
16.3k
                                 rsa->_method_mod_n))
1106
0
                goto err;
1107
16.3k
        } else {
1108
0
            bn_correct_top(r0);
1109
0
            if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,
1110
0
                                       rsa->_method_mod_n))
1111
0
                goto err;
1112
0
        }
1113
        /*
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.
1118
         */
1119
16.3k
        if (!BN_sub(vrfy, vrfy, I))
1120
0
            goto err;
1121
16.3k
        if (BN_is_zero(vrfy)) {
1122
16.3k
            bn_correct_top(r0);
1123
16.3k
            ret = 1;
1124
16.3k
            goto err;   /* not actually error */
1125
16.3k
        }
1126
0
        if (!BN_mod(vrfy, vrfy, rsa->n, ctx))
1127
0
            goto err;
1128
0
        if (BN_is_negative(vrfy))
1129
0
            if (!BN_add(vrfy, vrfy, rsa->n))
1130
0
                goto err;
1131
0
        if (!BN_is_zero(vrfy)) {
1132
            /*
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.
1136
             */
1137
1138
0
            BIGNUM *d = BN_new();
1139
0
            if (d == NULL)
1140
0
                goto err;
1141
0
            BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
1142
1143
0
            if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx,
1144
0
                                       rsa->_method_mod_n)) {
1145
0
                BN_free(d);
1146
0
                goto err;
1147
0
            }
1148
            /* We MUST free d before any further use of rsa->d */
1149
0
            BN_free(d);
1150
0
        }
1151
0
    }
1152
    /*
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.
1159
     */
1160
0
    bn_correct_top(r0);
1161
0
    ret = 1;
1162
16.3k
 err:
1163
16.3k
    BN_CTX_end(ctx);
1164
16.3k
    return ret;
1165
0
}
1166
1167
static int rsa_ossl_init(RSA *rsa)
1168
634k
{
1169
634k
    rsa->flags |= RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE;
1170
634k
    return 1;
1171
634k
}
1172
1173
static int rsa_ossl_finish(RSA *rsa)
1174
634k
{
1175
634k
#ifndef FIPS_MODULE
1176
634k
    int i;
1177
634k
    RSA_PRIME_INFO *pinfo;
1178
1179
971k
    for (i = 0; i < sk_RSA_PRIME_INFO_num(rsa->prime_infos); i++) {
1180
336k
        pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
1181
336k
        BN_MONT_CTX_free(pinfo->m);
1182
336k
    }
1183
634k
#endif
1184
1185
634k
    BN_MONT_CTX_free(rsa->_method_mod_n);
1186
634k
    BN_MONT_CTX_free(rsa->_method_mod_p);
1187
634k
    BN_MONT_CTX_free(rsa->_method_mod_q);
1188
634k
    return 1;
1189
634k
}
1190
1191
#ifdef S390X_MOD_EXP
1192
static int rsa_ossl_s390x_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa,
1193
                                BN_CTX *ctx)
1194
{
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)
1197
            return 1;
1198
    }
1199
    return rsa_ossl_mod_exp(r0, i, rsa, ctx);
1200
}
1201
1202
#endif