Coverage Report

Created: 2026-08-17 07:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/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 const 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 const 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
53.0k
{
93
53.0k
    return default_RSA_meth;
94
53.0k
}
95
96
const RSA_METHOD *RSA_PKCS1_OpenSSL(void)
97
0
{
98
0
    return &rsa_pkcs1_ossl_meth;
99
0
}
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
0
{
109
0
    BIGNUM *f, *ret;
110
0
    int i, num = 0, r = -1;
111
0
    unsigned char *buf = NULL;
112
0
    BN_CTX *ctx = NULL;
113
114
0
    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
0
    if (BN_ucmp(rsa->n, rsa->e) <= 0) {
120
0
        ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
121
0
        return -1;
122
0
    }
123
124
    /* for large moduli, enforce exponent limit */
125
0
    if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
126
0
        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
0
    }
131
132
0
    if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
133
0
        goto err;
134
0
    BN_CTX_start(ctx);
135
0
    f = BN_CTX_get(ctx);
136
0
    ret = BN_CTX_get(ctx);
137
0
    num = BN_num_bytes(rsa->n);
138
0
    buf = OPENSSL_malloc(num);
139
0
    if (ret == NULL || buf == NULL)
140
0
        goto err;
141
142
0
    switch (padding) {
143
0
    case RSA_PKCS1_PADDING:
144
0
        i = ossl_rsa_padding_add_PKCS1_type_2_ex(rsa->libctx, buf, num,
145
0
            from, flen);
146
0
        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
0
    }
159
0
    if (i <= 0)
160
0
        goto err;
161
162
0
    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
0
    {
189
0
        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
0
    }
195
196
0
    if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
197
0
        if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
198
0
                rsa->n, ctx))
199
0
            goto err;
200
201
0
    if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
202
0
            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
0
    r = BN_bn2binpad(ret, to, num);
210
0
err:
211
0
    BN_CTX_end(ctx);
212
0
    BN_CTX_free(ctx);
213
0
    OPENSSL_clear_free(buf, num);
214
0
    return r;
215
0
}
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
0
{
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
0
    return (uintptr_t)CRYPTO_THREAD_get_current_id();
234
0
#endif
235
0
}
236
237
static void free_bn_blinding(ossl_uintmax_t idx, BN_BLINDING *b, void *arg)
238
0
{
239
0
    BN_BLINDING_free(b);
240
0
}
241
242
void ossl_rsa_free_blinding(RSA *rsa)
243
53.0k
{
244
53.0k
    SPARSE_ARRAY_OF(BN_BLINDING) *blindings = rsa->blindings_sa;
245
246
53.0k
    ossl_sa_BN_BLINDING_doall_arg(blindings, free_bn_blinding, NULL);
247
53.0k
    ossl_sa_BN_BLINDING_free(blindings);
248
53.0k
}
249
250
void *ossl_rsa_alloc_blinding(void)
251
53.0k
{
252
53.0k
    return ossl_sa_BN_BLINDING_new();
253
53.0k
}
254
255
static BN_BLINDING *ossl_rsa_get_thread_bn_blinding(RSA *rsa)
256
0
{
257
0
    SPARSE_ARRAY_OF(BN_BLINDING) *blindings = rsa->blindings_sa;
258
0
    uintptr_t tid = get_unique_thread_id();
259
260
0
    return ossl_sa_BN_BLINDING_get(blindings, tid);
261
0
}
262
263
static int ossl_rsa_set_thread_bn_blinding(RSA *rsa, BN_BLINDING *b)
264
0
{
265
0
    SPARSE_ARRAY_OF(BN_BLINDING) *blindings = rsa->blindings_sa;
266
0
    uintptr_t tid = get_unique_thread_id();
267
268
0
    return ossl_sa_BN_BLINDING_set(blindings, tid, b);
269
0
}
270
271
static BN_BLINDING *rsa_get_blinding(RSA *rsa, BN_CTX *ctx)
272
0
{
273
0
    BN_BLINDING *ret;
274
275
0
    if (!CRYPTO_THREAD_read_lock(rsa->lock))
276
0
        return NULL;
277
278
0
    ret = ossl_rsa_get_thread_bn_blinding(rsa);
279
0
    CRYPTO_THREAD_unlock(rsa->lock);
280
281
0
    if (ret == NULL) {
282
0
        ret = RSA_setup_blinding(rsa, ctx);
283
0
        if (!CRYPTO_THREAD_write_lock(rsa->lock)) {
284
0
            BN_BLINDING_free(ret);
285
0
            ret = NULL;
286
0
        } else {
287
0
            if (!ossl_rsa_set_thread_bn_blinding(rsa, ret)) {
288
0
                BN_BLINDING_free(ret);
289
0
                ret = NULL;
290
0
            }
291
0
            CRYPTO_THREAD_unlock(rsa->lock);
292
0
        }
293
0
    }
294
295
0
    return ret;
296
0
}
297
298
static int rsa_blinding_convert(BN_BLINDING *b, BIGNUM *f, BN_CTX *ctx)
299
0
{
300
    /*
301
     * Local blinding: store the unblinding factor in BN_BLINDING.
302
     */
303
0
    return BN_BLINDING_convert_ex(f, NULL, b, ctx);
304
0
}
305
306
static int rsa_blinding_invert(BN_BLINDING *b, BIGNUM *f, BN_CTX *ctx)
307
0
{
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
0
    BN_set_flags(f, BN_FLG_CONSTTIME);
317
0
    return BN_BLINDING_invert_ex(f, NULL, b, ctx);
318
0
}
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
0
{
324
0
    BIGNUM *f, *ret, *res;
325
0
    int i, num = 0, r = -1;
326
0
    unsigned char *buf = NULL;
327
0
    BN_CTX *ctx = NULL;
328
0
    BN_BLINDING *blinding = NULL;
329
330
0
    if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
331
0
        goto err;
332
0
    BN_CTX_start(ctx);
333
0
    f = BN_CTX_get(ctx);
334
0
    ret = BN_CTX_get(ctx);
335
0
    num = BN_num_bytes(rsa->n);
336
0
    buf = OPENSSL_malloc(num);
337
0
    if (ret == NULL || buf == NULL)
338
0
        goto err;
339
340
0
    switch (padding) {
341
0
    case RSA_PKCS1_PADDING:
342
0
        i = RSA_padding_add_PKCS1_type_1(buf, num, from, flen);
343
0
        break;
344
0
    case RSA_X931_PADDING:
345
0
        i = RSA_padding_add_X931(buf, num, from, flen);
346
0
        break;
347
0
    case RSA_NO_PADDING:
348
0
        i = RSA_padding_add_none(buf, num, from, flen);
349
0
        break;
350
0
    default:
351
0
        ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
352
0
        goto err;
353
0
    }
354
0
    if (i <= 0)
355
0
        goto err;
356
357
0
    if (BN_bin2bn(buf, num, f) == NULL)
358
0
        goto err;
359
360
0
    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
0
    if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
367
0
        if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
368
0
                rsa->n, ctx))
369
0
            goto err;
370
371
0
    if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
372
0
        blinding = rsa_get_blinding(rsa, ctx);
373
0
        if (blinding == NULL) {
374
0
            ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
375
0
            goto err;
376
0
        }
377
378
0
        if (!rsa_blinding_convert(blinding, f, ctx))
379
0
            goto err;
380
0
    }
381
382
0
    if ((rsa->flags & RSA_FLAG_EXT_PKEY) || (rsa->version == RSA_ASN1_VERSION_MULTI) || ((rsa->p != NULL) && (rsa->q != NULL) && (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
383
0
        if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
384
0
            goto err;
385
0
    } else {
386
0
        BIGNUM *d = BN_new();
387
0
        if (d == NULL) {
388
0
            ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
389
0
            goto err;
390
0
        }
391
0
        if (rsa->d == NULL) {
392
0
            ERR_raise(ERR_LIB_RSA, RSA_R_MISSING_PRIVATE_KEY);
393
0
            BN_free(d);
394
0
            goto err;
395
0
        }
396
0
        BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
397
398
0
        if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
399
0
                rsa->_method_mod_n)) {
400
0
            BN_free(d);
401
0
            goto err;
402
0
        }
403
        /* We MUST free d before any further use of rsa->d */
404
0
        BN_free(d);
405
0
    }
406
407
0
    if (blinding)
408
0
        if (!rsa_blinding_invert(blinding, ret, ctx))
409
0
            goto err;
410
411
0
    if (padding == RSA_X931_PADDING) {
412
0
        if (!BN_sub(f, rsa->n, ret))
413
0
            goto err;
414
0
        if (BN_cmp(ret, f) > 0)
415
0
            res = f;
416
0
        else
417
0
            res = ret;
418
0
    } else {
419
0
        res = ret;
420
0
    }
421
422
    /*
423
     * BN_bn2binpad puts in leading 0 bytes if the number is less than
424
     * the length of the modulus.
425
     */
426
0
    r = BN_bn2binpad(res, to, num);
427
0
err:
428
0
    BN_CTX_end(ctx);
429
0
    BN_CTX_free(ctx);
430
0
    OPENSSL_clear_free(buf, num);
431
0
    return r;
432
0
}
433
434
static int derive_kdk(int flen, const unsigned char *from, RSA *rsa,
435
    unsigned char *buf, int num, unsigned char *kdk)
436
0
{
437
0
    int ret = 0;
438
0
    HMAC_CTX *hmac = NULL;
439
0
    EVP_MD *md = NULL;
440
0
    unsigned int md_len = SHA256_DIGEST_LENGTH;
441
0
    unsigned char d_hash[SHA256_DIGEST_LENGTH] = { 0 };
442
    /*
443
     * because we use d as a handle to rsa->d we need to keep it local and
444
     * free before any further use of rsa->d
445
     */
446
0
    BIGNUM *d = BN_new();
447
448
0
    if (d == NULL) {
449
0
        ERR_raise(ERR_LIB_RSA, ERR_R_CRYPTO_LIB);
450
0
        goto err;
451
0
    }
452
0
    if (rsa->d == NULL) {
453
0
        ERR_raise(ERR_LIB_RSA, RSA_R_MISSING_PRIVATE_KEY);
454
0
        BN_free(d);
455
0
        goto err;
456
0
    }
457
0
    BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
458
0
    if (BN_bn2binpad(d, buf, num) < 0) {
459
0
        ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
460
0
        BN_free(d);
461
0
        goto err;
462
0
    }
463
0
    BN_free(d);
464
465
    /*
466
     * we use hardcoded hash so that migrating between versions that use
467
     * different hash doesn't provide a Bleichenbacher oracle:
468
     * if the attacker can see that different versions return different
469
     * messages for the same ciphertext, they'll know that the message is
470
     * synthetically generated, which means that the padding check failed
471
     */
472
0
    md = EVP_MD_fetch(rsa->libctx, "sha256", NULL);
473
0
    if (md == NULL) {
474
0
        ERR_raise(ERR_LIB_RSA, ERR_R_FETCH_FAILED);
475
0
        goto err;
476
0
    }
477
478
0
    if (EVP_Digest(buf, num, d_hash, NULL, md, NULL) <= 0) {
479
0
        ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
480
0
        goto err;
481
0
    }
482
483
0
    hmac = HMAC_CTX_new();
484
0
    if (hmac == NULL) {
485
0
        ERR_raise(ERR_LIB_RSA, ERR_R_CRYPTO_LIB);
486
0
        goto err;
487
0
    }
488
489
0
    if (HMAC_Init_ex(hmac, d_hash, sizeof(d_hash), md, NULL) <= 0) {
490
0
        ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
491
0
        goto err;
492
0
    }
493
494
0
    if (flen < num) {
495
0
        memset(buf, 0, num - flen);
496
0
        if (HMAC_Update(hmac, buf, num - flen) <= 0) {
497
0
            ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
498
0
            goto err;
499
0
        }
500
0
    }
501
0
    if (HMAC_Update(hmac, from, flen) <= 0) {
502
0
        ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
503
0
        goto err;
504
0
    }
505
506
0
    md_len = SHA256_DIGEST_LENGTH;
507
0
    if (HMAC_Final(hmac, kdk, &md_len) <= 0) {
508
0
        ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
509
0
        goto err;
510
0
    }
511
0
    ret = 1;
512
513
0
err:
514
0
    HMAC_CTX_free(hmac);
515
0
    EVP_MD_free(md);
516
0
    return ret;
517
0
}
518
519
static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
520
    unsigned char *to, RSA *rsa, int padding)
521
0
{
522
0
    BIGNUM *f, *ret;
523
0
    int j, num = 0, r = -1;
524
0
    unsigned char *buf = NULL;
525
0
    unsigned char kdk[SHA256_DIGEST_LENGTH] = { 0 };
526
0
    BN_CTX *ctx = NULL;
527
0
    BN_BLINDING *blinding = NULL;
528
529
    /*
530
     * we need the value of the private exponent to perform implicit rejection
531
     */
532
0
    if ((rsa->flags & RSA_FLAG_EXT_PKEY) && (padding == RSA_PKCS1_PADDING))
533
0
        padding = RSA_PKCS1_NO_IMPLICIT_REJECT_PADDING;
534
535
0
    if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
536
0
        goto err;
537
0
    BN_CTX_start(ctx);
538
0
    f = BN_CTX_get(ctx);
539
0
    ret = BN_CTX_get(ctx);
540
0
    if (ret == NULL) {
541
0
        ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
542
0
        goto err;
543
0
    }
544
0
    num = BN_num_bytes(rsa->n);
545
0
    buf = OPENSSL_malloc(num);
546
0
    if (buf == NULL)
547
0
        goto err;
548
549
    /*
550
     * This check was for equality but PGP does evil things and chops off the
551
     * top '0' bytes
552
     */
553
0
    if (flen > num) {
554
0
        ERR_raise(ERR_LIB_RSA, RSA_R_DATA_GREATER_THAN_MOD_LEN);
555
0
        goto err;
556
0
    }
557
558
0
    if (flen < 1) {
559
0
        ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_SMALL);
560
0
        goto err;
561
0
    }
562
563
    /* make data into a big number */
564
0
    if (BN_bin2bn(from, (int)flen, f) == NULL)
565
0
        goto err;
566
567
    /*
568
     * See SP800-56Br2, section 7.1.2.1
569
     * RSADP: 1 < f < (n – 1)
570
     * (where f is the ciphertext).
571
     */
572
0
    if (padding == RSA_NO_PADDING) {
573
0
        BIGNUM *nminus1 = BN_CTX_get(ctx);
574
575
0
        if (BN_ucmp(f, BN_value_one()) <= 0) {
576
0
            ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_SMALL);
577
0
            goto err;
578
0
        }
579
0
        if (nminus1 == NULL
580
0
            || BN_copy(nminus1, rsa->n) == NULL
581
0
            || !BN_sub_word(nminus1, 1))
582
0
            goto err;
583
0
        if (BN_ucmp(f, nminus1) >= 0) {
584
0
            ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
585
0
            goto err;
586
0
        }
587
0
    } else {
588
0
        if (BN_ucmp(f, rsa->n) >= 0) {
589
0
            ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
590
0
            goto err;
591
0
        }
592
0
    }
593
0
    if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
594
0
        if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
595
0
                rsa->n, ctx))
596
0
            goto err;
597
598
0
    if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
599
0
        blinding = rsa_get_blinding(rsa, ctx);
600
0
        if (blinding == NULL) {
601
0
            ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
602
0
            goto err;
603
0
        }
604
605
0
        if (!rsa_blinding_convert(blinding, f, ctx))
606
0
            goto err;
607
0
    }
608
609
    /* do the decrypt */
610
0
    if ((rsa->flags & RSA_FLAG_EXT_PKEY) || (rsa->version == RSA_ASN1_VERSION_MULTI) || ((rsa->p != NULL) && (rsa->q != NULL) && (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
611
0
        if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
612
0
            goto err;
613
0
    } else {
614
0
        BIGNUM *d = BN_new();
615
0
        if (d == NULL) {
616
0
            ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
617
0
            goto err;
618
0
        }
619
0
        if (rsa->d == NULL) {
620
0
            ERR_raise(ERR_LIB_RSA, RSA_R_MISSING_PRIVATE_KEY);
621
0
            BN_free(d);
622
0
            goto err;
623
0
        }
624
0
        BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
625
0
        if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
626
0
                rsa->_method_mod_n)) {
627
0
            BN_free(d);
628
0
            goto err;
629
0
        }
630
        /* We MUST free d before any further use of rsa->d */
631
0
        BN_free(d);
632
0
    }
633
634
0
    if (blinding)
635
0
        if (!rsa_blinding_invert(blinding, ret, ctx))
636
0
            goto err;
637
638
    /*
639
     * derive the Key Derivation Key from private exponent and public
640
     * ciphertext
641
     */
642
0
    if (padding == RSA_PKCS1_PADDING) {
643
0
        if (derive_kdk(flen, from, rsa, buf, num, kdk) == 0)
644
0
            goto err;
645
0
    }
646
647
0
    j = BN_bn2binpad(ret, buf, num);
648
0
    if (j < 0)
649
0
        goto err;
650
651
0
    switch (padding) {
652
0
    case RSA_PKCS1_NO_IMPLICIT_REJECT_PADDING:
653
0
        r = RSA_padding_check_PKCS1_type_2(to, num, buf, j, num);
654
0
        break;
655
0
    case RSA_PKCS1_PADDING:
656
0
        r = ossl_rsa_padding_check_PKCS1_type_2(rsa->libctx, to, num, buf, j, num, kdk);
657
0
        break;
658
0
    case RSA_PKCS1_OAEP_PADDING:
659
0
        r = RSA_padding_check_PKCS1_OAEP(to, num, buf, j, num, NULL, 0);
660
0
        break;
661
0
    case RSA_NO_PADDING:
662
0
        memcpy(to, buf, (r = j));
663
0
        break;
664
0
    default:
665
0
        ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
666
0
        goto err;
667
0
    }
668
0
#ifndef FIPS_MODULE
669
    /*
670
     * This trick doesn't work in the FIPS provider because libcrypto manages
671
     * the error stack. Instead we opt not to put an error on the stack at all
672
     * in case of padding failure in the FIPS provider.
673
     */
674
0
    ERR_raise(ERR_LIB_RSA, RSA_R_PADDING_CHECK_FAILED);
675
0
    err_clear_last_constant_time(1 & ~constant_time_msb(r));
676
0
#endif
677
678
0
err:
679
0
    BN_CTX_end(ctx);
680
0
    BN_CTX_free(ctx);
681
0
    OPENSSL_clear_free(buf, num);
682
0
    return r;
683
0
}
684
685
/* signature verification */
686
static int rsa_ossl_public_decrypt(int flen, const unsigned char *from,
687
    unsigned char *to, RSA *rsa, int padding)
688
0
{
689
0
    BIGNUM *f, *ret;
690
0
    int i, num = 0, r = -1;
691
0
    unsigned char *buf = NULL;
692
0
    BN_CTX *ctx = NULL;
693
694
0
    if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
695
0
        ERR_raise(ERR_LIB_RSA, RSA_R_MODULUS_TOO_LARGE);
696
0
        return -1;
697
0
    }
698
699
0
    if (BN_ucmp(rsa->n, rsa->e) <= 0) {
700
0
        ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
701
0
        return -1;
702
0
    }
703
704
    /* for large moduli, enforce exponent limit */
705
0
    if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
706
0
        if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
707
0
            ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
708
0
            return -1;
709
0
        }
710
0
    }
711
712
0
    if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
713
0
        goto err;
714
0
    BN_CTX_start(ctx);
715
0
    f = BN_CTX_get(ctx);
716
0
    ret = BN_CTX_get(ctx);
717
0
    if (ret == NULL) {
718
0
        ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
719
0
        goto err;
720
0
    }
721
0
    num = BN_num_bytes(rsa->n);
722
0
    buf = OPENSSL_malloc(num);
723
0
    if (buf == NULL)
724
0
        goto err;
725
726
    /*
727
     * This check was for equality but PGP does evil things and chops off the
728
     * top '0' bytes
729
     */
730
0
    if (flen > num) {
731
0
        ERR_raise(ERR_LIB_RSA, RSA_R_DATA_GREATER_THAN_MOD_LEN);
732
0
        goto err;
733
0
    }
734
735
0
    if (BN_bin2bn(from, flen, f) == NULL)
736
0
        goto err;
737
738
0
    if (BN_ucmp(f, rsa->n) >= 0) {
739
0
        ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
740
0
        goto err;
741
0
    }
742
743
0
    if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
744
0
        if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
745
0
                rsa->n, ctx))
746
0
            goto err;
747
748
0
    if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
749
0
            rsa->_method_mod_n))
750
0
        goto err;
751
752
    /* For X9.31: Assuming e is odd it does a 12 mod 16 test */
753
0
    if ((padding == RSA_X931_PADDING) && ((bn_get_words(ret)[0] & 0xf) != 12))
754
0
        if (!BN_sub(ret, rsa->n, ret))
755
0
            goto err;
756
757
0
    i = BN_bn2binpad(ret, buf, num);
758
0
    if (i < 0)
759
0
        goto err;
760
761
0
    switch (padding) {
762
0
    case RSA_PKCS1_PADDING:
763
0
        r = RSA_padding_check_PKCS1_type_1(to, num, buf, i, num);
764
0
        break;
765
0
    case RSA_X931_PADDING:
766
0
        r = RSA_padding_check_X931(to, num, buf, i, num);
767
0
        break;
768
0
    case RSA_NO_PADDING:
769
0
        memcpy(to, buf, (r = i));
770
0
        break;
771
0
    default:
772
0
        ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
773
0
        goto err;
774
0
    }
775
0
    if (r < 0)
776
0
        ERR_raise(ERR_LIB_RSA, RSA_R_PADDING_CHECK_FAILED);
777
778
0
err:
779
0
    BN_CTX_end(ctx);
780
0
    BN_CTX_free(ctx);
781
0
    OPENSSL_clear_free(buf, num);
782
0
    return r;
783
0
}
784
785
static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
786
0
{
787
0
    BIGNUM *r1, *m1, *vrfy;
788
0
    int ret = 0, smooth = 0;
789
0
#ifndef FIPS_MODULE
790
0
    BIGNUM *r2, *m[RSA_MAX_PRIME_NUM - 2];
791
0
    int i, ex_primes = 0;
792
0
    RSA_PRIME_INFO *pinfo;
793
0
#endif
794
795
0
    BN_CTX_start(ctx);
796
797
0
    r1 = BN_CTX_get(ctx);
798
0
#ifndef FIPS_MODULE
799
0
    r2 = BN_CTX_get(ctx);
800
0
#endif
801
0
    m1 = BN_CTX_get(ctx);
802
0
    vrfy = BN_CTX_get(ctx);
803
0
    if (vrfy == NULL)
804
0
        goto err;
805
806
0
#ifndef FIPS_MODULE
807
0
    if (rsa->version == RSA_ASN1_VERSION_MULTI
808
0
        && ((ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) <= 0
809
0
            || ex_primes > RSA_MAX_PRIME_NUM - 2))
810
0
        goto err;
811
0
#endif
812
813
0
    if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {
814
0
        BIGNUM *factor = BN_new();
815
816
0
        if (factor == NULL)
817
0
            goto err;
818
819
        /*
820
         * Make sure BN_mod_inverse in Montgomery initialization uses the
821
         * BN_FLG_CONSTTIME flag
822
         */
823
0
        if (!(BN_with_flags(factor, rsa->p, BN_FLG_CONSTTIME),
824
0
                BN_MONT_CTX_set_locked(&rsa->_method_mod_p, rsa->lock,
825
0
                    factor, ctx))
826
0
            || !(BN_with_flags(factor, rsa->q, BN_FLG_CONSTTIME),
827
0
                BN_MONT_CTX_set_locked(&rsa->_method_mod_q, rsa->lock,
828
0
                    factor, ctx))) {
829
0
            BN_free(factor);
830
0
            goto err;
831
0
        }
832
0
#ifndef FIPS_MODULE
833
0
        for (i = 0; i < ex_primes; i++) {
834
0
            pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
835
0
            BN_with_flags(factor, pinfo->r, BN_FLG_CONSTTIME);
836
0
            if (!BN_MONT_CTX_set_locked(&pinfo->m, rsa->lock, factor, ctx)) {
837
0
                BN_free(factor);
838
0
                goto err;
839
0
            }
840
0
        }
841
0
#endif
842
        /*
843
         * We MUST free |factor| before any further use of the prime factors
844
         */
845
0
        BN_free(factor);
846
847
0
        smooth = (rsa->meth->bn_mod_exp == BN_mod_exp_mont)
848
0
#ifndef FIPS_MODULE
849
0
            && (ex_primes == 0)
850
0
#endif
851
0
            && (BN_num_bits(rsa->q) == BN_num_bits(rsa->p));
852
0
    }
853
854
0
    if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
855
0
        if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
856
0
                rsa->n, ctx))
857
0
            goto err;
858
859
0
    if (smooth) {
860
        /*
861
         * Conversion from Montgomery domain, a.k.a. Montgomery reduction,
862
         * accepts values in [0-m*2^w) range. w is m's bit width rounded up
863
         * to limb width. So that at the very least if |I| is fully reduced,
864
         * i.e. less than p*q, we can count on from-to round to perform
865
         * below modulo operations on |I|. Unlike BN_mod it's constant time.
866
         */
867
0
        if (/* m1 = I moq q */
868
0
            !bn_from_mont_fixed_top(m1, I, rsa->_method_mod_q, ctx)
869
0
            || !bn_to_mont_fixed_top(m1, m1, rsa->_method_mod_q, ctx)
870
            /* r1 = I mod p */
871
0
            || !bn_from_mont_fixed_top(r1, I, rsa->_method_mod_p, ctx)
872
0
            || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)
873
            /*
874
             * Use parallel exponentiations optimization if possible,
875
             * otherwise fallback to two sequential exponentiations:
876
             *    m1 = m1^dmq1 mod q
877
             *    r1 = r1^dmp1 mod p
878
             */
879
0
            || !BN_mod_exp_mont_consttime_x2(m1, m1, rsa->dmq1, rsa->q,
880
0
                rsa->_method_mod_q,
881
0
                r1, r1, rsa->dmp1, rsa->p,
882
0
                rsa->_method_mod_p,
883
0
                ctx)
884
            /* r1 = (r1 - m1) mod p */
885
            /*
886
             * bn_mod_sub_fixed_top is not regular modular subtraction,
887
             * it can tolerate subtrahend to be larger than modulus, but
888
             * not bit-wise wider. This makes up for uncommon q>p case,
889
             * when |m1| can be larger than |rsa->p|.
890
             */
891
0
            || !bn_mod_sub_fixed_top(r1, r1, m1, rsa->p)
892
893
            /* r1 = r1 * iqmp mod p */
894
0
            || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)
895
0
            || !bn_mul_mont_fixed_top(r1, r1, rsa->iqmp, rsa->_method_mod_p,
896
0
                ctx)
897
            /* r0 = r1 * q + m1 */
898
0
            || !bn_mul_fixed_top(r0, r1, rsa->q, ctx)
899
0
            || !bn_mod_add_fixed_top(r0, r0, m1, rsa->n))
900
0
            goto err;
901
902
0
        goto tail;
903
0
    }
904
905
    /* compute I mod q */
906
0
    {
907
0
        BIGNUM *c = BN_new();
908
0
        if (c == NULL)
909
0
            goto err;
910
0
        BN_with_flags(c, I, BN_FLG_CONSTTIME);
911
912
0
        if (!BN_mod(r1, c, rsa->q, ctx)) {
913
0
            BN_free(c);
914
0
            goto err;
915
0
        }
916
917
0
        {
918
0
            BIGNUM *dmq1 = BN_new();
919
0
            if (dmq1 == NULL) {
920
0
                BN_free(c);
921
0
                goto err;
922
0
            }
923
0
            BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
924
925
            /* compute r1^dmq1 mod q */
926
0
            if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx,
927
0
                    rsa->_method_mod_q)) {
928
0
                BN_free(c);
929
0
                BN_free(dmq1);
930
0
                goto err;
931
0
            }
932
            /* We MUST free dmq1 before any further use of rsa->dmq1 */
933
0
            BN_free(dmq1);
934
0
        }
935
936
        /* compute I mod p */
937
0
        if (!BN_mod(r1, c, rsa->p, ctx)) {
938
0
            BN_free(c);
939
0
            goto err;
940
0
        }
941
        /* We MUST free c before any further use of I */
942
0
        BN_free(c);
943
0
    }
944
945
0
    {
946
0
        BIGNUM *dmp1 = BN_new();
947
0
        if (dmp1 == NULL)
948
0
            goto err;
949
0
        BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
950
951
        /* compute r1^dmp1 mod p */
952
0
        if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx,
953
0
                rsa->_method_mod_p)) {
954
0
            BN_free(dmp1);
955
0
            goto err;
956
0
        }
957
        /* We MUST free dmp1 before any further use of rsa->dmp1 */
958
0
        BN_free(dmp1);
959
0
    }
960
961
0
#ifndef FIPS_MODULE
962
0
    if (ex_primes > 0) {
963
0
        BIGNUM *di = BN_new(), *cc = BN_new();
964
965
0
        if (cc == NULL || di == NULL) {
966
0
            BN_free(cc);
967
0
            BN_free(di);
968
0
            goto err;
969
0
        }
970
971
0
        for (i = 0; i < ex_primes; i++) {
972
            /* prepare m_i */
973
0
            if ((m[i] = BN_CTX_get(ctx)) == NULL) {
974
0
                BN_free(cc);
975
0
                BN_free(di);
976
0
                goto err;
977
0
            }
978
979
0
            pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
980
981
            /* prepare c and d_i */
982
0
            BN_with_flags(cc, I, BN_FLG_CONSTTIME);
983
0
            BN_with_flags(di, pinfo->d, BN_FLG_CONSTTIME);
984
985
0
            if (!BN_mod(r1, cc, pinfo->r, ctx)) {
986
0
                BN_free(cc);
987
0
                BN_free(di);
988
0
                goto err;
989
0
            }
990
            /* compute r1 ^ d_i mod r_i */
991
0
            if (!rsa->meth->bn_mod_exp(m[i], r1, di, pinfo->r, ctx, pinfo->m)) {
992
0
                BN_free(cc);
993
0
                BN_free(di);
994
0
                goto err;
995
0
            }
996
0
        }
997
998
0
        BN_free(cc);
999
0
        BN_free(di);
1000
0
    }
1001
0
#endif
1002
1003
0
    if (!BN_sub(r0, r0, m1))
1004
0
        goto err;
1005
    /*
1006
     * This will help stop the size of r0 increasing, which does affect the
1007
     * multiply if it optimised for a power of 2 size
1008
     */
1009
0
    if (BN_is_negative(r0))
1010
0
        if (!BN_add(r0, r0, rsa->p))
1011
0
            goto err;
1012
1013
0
    if (!BN_mul(r1, r0, rsa->iqmp, ctx))
1014
0
        goto err;
1015
1016
0
    {
1017
0
        BIGNUM *pr1 = BN_new();
1018
0
        if (pr1 == NULL)
1019
0
            goto err;
1020
0
        BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
1021
1022
0
        if (!BN_mod(r0, pr1, rsa->p, ctx)) {
1023
0
            BN_free(pr1);
1024
0
            goto err;
1025
0
        }
1026
        /* We MUST free pr1 before any further use of r1 */
1027
0
        BN_free(pr1);
1028
0
    }
1029
1030
    /*
1031
     * If p < q it is occasionally possible for the correction of adding 'p'
1032
     * if r0 is negative above to leave the result still negative. This can
1033
     * break the private key operations: the following second correction
1034
     * should *always* correct this rare occurrence. This will *never* happen
1035
     * with OpenSSL generated keys because they ensure p > q [steve]
1036
     */
1037
0
    if (BN_is_negative(r0))
1038
0
        if (!BN_add(r0, r0, rsa->p))
1039
0
            goto err;
1040
0
    if (!BN_mul(r1, r0, rsa->q, ctx))
1041
0
        goto err;
1042
0
    if (!BN_add(r0, r1, m1))
1043
0
        goto err;
1044
1045
0
#ifndef FIPS_MODULE
1046
    /* add m_i to m in multi-prime case */
1047
0
    if (ex_primes > 0) {
1048
0
        BIGNUM *pr2 = BN_new();
1049
1050
0
        if (pr2 == NULL)
1051
0
            goto err;
1052
1053
0
        for (i = 0; i < ex_primes; i++) {
1054
0
            pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
1055
0
            if (!BN_sub(r1, m[i], r0)) {
1056
0
                BN_free(pr2);
1057
0
                goto err;
1058
0
            }
1059
1060
0
            if (!BN_mul(r2, r1, pinfo->t, ctx)) {
1061
0
                BN_free(pr2);
1062
0
                goto err;
1063
0
            }
1064
1065
0
            BN_with_flags(pr2, r2, BN_FLG_CONSTTIME);
1066
1067
0
            if (!BN_mod(r1, pr2, pinfo->r, ctx)) {
1068
0
                BN_free(pr2);
1069
0
                goto err;
1070
0
            }
1071
1072
0
            if (BN_is_negative(r1))
1073
0
                if (!BN_add(r1, r1, pinfo->r)) {
1074
0
                    BN_free(pr2);
1075
0
                    goto err;
1076
0
                }
1077
0
            if (!BN_mul(r1, r1, pinfo->pp, ctx)) {
1078
0
                BN_free(pr2);
1079
0
                goto err;
1080
0
            }
1081
0
            if (!BN_add(r0, r0, r1)) {
1082
0
                BN_free(pr2);
1083
0
                goto err;
1084
0
            }
1085
0
        }
1086
0
        BN_free(pr2);
1087
0
    }
1088
0
#endif
1089
1090
0
tail:
1091
0
    if (rsa->e && rsa->n) {
1092
0
        if (rsa->meth->bn_mod_exp == BN_mod_exp_mont) {
1093
0
            if (!BN_mod_exp_mont(vrfy, r0, rsa->e, rsa->n, ctx,
1094
0
                    rsa->_method_mod_n))
1095
0
                goto err;
1096
0
        } else {
1097
0
            bn_correct_top(r0);
1098
0
            if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,
1099
0
                    rsa->_method_mod_n))
1100
0
                goto err;
1101
0
        }
1102
        /*
1103
         * If 'I' was greater than (or equal to) rsa->n, the operation will
1104
         * be equivalent to using 'I mod n'. However, the result of the
1105
         * verify will *always* be less than 'n' so we don't check for
1106
         * absolute equality, just congruency.
1107
         */
1108
0
        if (!BN_sub(vrfy, vrfy, I))
1109
0
            goto err;
1110
0
        if (BN_is_zero(vrfy)) {
1111
0
            bn_correct_top(r0);
1112
0
            ret = 1;
1113
0
            goto err; /* not actually error */
1114
0
        }
1115
0
        if (!BN_mod(vrfy, vrfy, rsa->n, ctx))
1116
0
            goto err;
1117
0
        if (BN_is_negative(vrfy))
1118
0
            if (!BN_add(vrfy, vrfy, rsa->n))
1119
0
                goto err;
1120
0
        if (!BN_is_zero(vrfy)) {
1121
            /*
1122
             * 'I' and 'vrfy' aren't congruent mod n. Don't leak
1123
             * miscalculated CRT output, just do a raw (slower) mod_exp and
1124
             * return that instead.
1125
             */
1126
1127
0
            BIGNUM *d = BN_new();
1128
0
            if (d == NULL)
1129
0
                goto err;
1130
0
            BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
1131
1132
0
            if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx,
1133
0
                    rsa->_method_mod_n)) {
1134
0
                BN_free(d);
1135
0
                goto err;
1136
0
            }
1137
            /* We MUST free d before any further use of rsa->d */
1138
0
            BN_free(d);
1139
0
        }
1140
0
    }
1141
    /*
1142
     * It's unfortunate that we have to bn_correct_top(r0). What hopefully
1143
     * saves the day is that correction is highly unlike, and private key
1144
     * operations are customarily performed on blinded message. Which means
1145
     * that attacker won't observe correlation with chosen plaintext.
1146
     * Secondly, remaining code would still handle it in same computational
1147
     * time and even conceal memory access pattern around corrected top.
1148
     */
1149
0
    bn_correct_top(r0);
1150
0
    ret = 1;
1151
0
err:
1152
0
    BN_CTX_end(ctx);
1153
0
    return ret;
1154
0
}
1155
1156
static int rsa_ossl_init(RSA *rsa)
1157
53.0k
{
1158
53.0k
    rsa->flags |= RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE;
1159
53.0k
    return 1;
1160
53.0k
}
1161
1162
static int rsa_ossl_finish(RSA *rsa)
1163
53.0k
{
1164
53.0k
#ifndef FIPS_MODULE
1165
53.0k
    int i;
1166
53.0k
    RSA_PRIME_INFO *pinfo;
1167
1168
53.0k
    for (i = 0; i < sk_RSA_PRIME_INFO_num(rsa->prime_infos); i++) {
1169
0
        pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
1170
0
        BN_MONT_CTX_free(pinfo->m);
1171
0
    }
1172
53.0k
#endif
1173
1174
53.0k
    BN_MONT_CTX_free(rsa->_method_mod_n);
1175
53.0k
    BN_MONT_CTX_free(rsa->_method_mod_p);
1176
53.0k
    BN_MONT_CTX_free(rsa->_method_mod_q);
1177
53.0k
    return 1;
1178
53.0k
}
1179
1180
#ifdef S390X_MOD_EXP
1181
static int rsa_ossl_s390x_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa,
1182
    BN_CTX *ctx)
1183
{
1184
    int rc;
1185
1186
    if (rsa->version != RSA_ASN1_VERSION_MULTI) {
1187
        rc = s390x_crt(r0, i, rsa->p, rsa->q, rsa->dmp1, rsa->dmq1, rsa->iqmp);
1188
        if (rc >= 0)
1189
            return rc;
1190
    }
1191
    return rsa_ossl_mod_exp(r0, i, rsa, ctx);
1192
}
1193
1194
#endif