Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/crypto/rsa/rsa_ossl.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2026 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 "rsa_local.h"
19
#include "internal/constant_time.h"
20
#include <openssl/evp.h>
21
#include <openssl/sha.h>
22
#include <openssl/hmac.h>
23
24
static int rsa_ossl_public_encrypt(int flen, const unsigned char *from,
25
    unsigned char *to, RSA *rsa, int padding);
26
static int rsa_ossl_private_encrypt(int flen, const unsigned char *from,
27
    unsigned char *to, RSA *rsa, int padding);
28
static int rsa_ossl_public_decrypt(int flen, const unsigned char *from,
29
    unsigned char *to, RSA *rsa, int padding);
30
static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
31
    unsigned char *to, RSA *rsa, int padding);
32
static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa,
33
    BN_CTX *ctx);
34
static int rsa_ossl_init(RSA *rsa);
35
static int rsa_ossl_finish(RSA *rsa);
36
#ifdef S390X_MOD_EXP
37
static int rsa_ossl_s390x_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa,
38
    BN_CTX *ctx);
39
static RSA_METHOD rsa_pkcs1_ossl_meth = {
40
    "OpenSSL PKCS#1 RSA",
41
    rsa_ossl_public_encrypt,
42
    rsa_ossl_public_decrypt, /* signature verification */
43
    rsa_ossl_private_encrypt, /* signing */
44
    rsa_ossl_private_decrypt,
45
    rsa_ossl_s390x_mod_exp,
46
    s390x_mod_exp,
47
    rsa_ossl_init,
48
    rsa_ossl_finish,
49
    RSA_FLAG_FIPS_METHOD, /* flags */
50
    NULL,
51
    0, /* rsa_sign */
52
    0, /* rsa_verify */
53
    NULL, /* rsa_keygen */
54
    NULL /* rsa_multi_prime_keygen */
55
};
56
#else
57
static RSA_METHOD rsa_pkcs1_ossl_meth = {
58
    "OpenSSL PKCS#1 RSA",
59
    rsa_ossl_public_encrypt,
60
    rsa_ossl_public_decrypt, /* signature verification */
61
    rsa_ossl_private_encrypt, /* signing */
62
    rsa_ossl_private_decrypt,
63
    rsa_ossl_mod_exp,
64
    BN_mod_exp_mont, /* XXX probably we should not use Montgomery
65
                      * if e == 3 */
66
    rsa_ossl_init,
67
    rsa_ossl_finish,
68
    RSA_FLAG_FIPS_METHOD, /* flags */
69
    NULL,
70
    0, /* rsa_sign */
71
    0, /* rsa_verify */
72
    NULL, /* rsa_keygen */
73
    NULL /* rsa_multi_prime_keygen */
74
};
75
#endif
76
77
static const RSA_METHOD *default_RSA_meth = &rsa_pkcs1_ossl_meth;
78
79
void RSA_set_default_method(const RSA_METHOD *meth)
80
0
{
81
0
    default_RSA_meth = meth;
82
0
}
83
84
const RSA_METHOD *RSA_get_default_method(void)
85
435k
{
86
435k
    return default_RSA_meth;
87
435k
}
88
89
const RSA_METHOD *RSA_PKCS1_OpenSSL(void)
90
168k
{
91
168k
    return &rsa_pkcs1_ossl_meth;
92
168k
}
93
94
const RSA_METHOD *RSA_null_method(void)
95
0
{
96
0
    return NULL;
97
0
}
98
99
static int rsa_ossl_public_encrypt(int flen, const unsigned char *from,
100
    unsigned char *to, RSA *rsa, int padding)
101
7.55k
{
102
7.55k
    BIGNUM *f, *ret;
103
7.55k
    int i, num = 0, r = -1;
104
7.55k
    unsigned char *buf = NULL;
105
7.55k
    BN_CTX *ctx = NULL;
106
107
7.55k
    if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
108
0
        ERR_raise(ERR_LIB_RSA, RSA_R_MODULUS_TOO_LARGE);
109
0
        return -1;
110
0
    }
111
112
7.55k
    if (BN_ucmp(rsa->n, rsa->e) <= 0) {
113
11
        ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
114
11
        return -1;
115
11
    }
116
117
    /* for large moduli, enforce exponent limit */
118
7.54k
    if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
119
101
        if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
120
0
            ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
121
0
            return -1;
122
0
        }
123
101
    }
124
125
7.54k
    if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
126
0
        goto err;
127
7.54k
    BN_CTX_start(ctx);
128
7.54k
    f = BN_CTX_get(ctx);
129
7.54k
    ret = BN_CTX_get(ctx);
130
7.54k
    num = BN_num_bytes(rsa->n);
131
7.54k
    buf = OPENSSL_malloc(num);
132
7.54k
    if (ret == NULL || buf == NULL)
133
0
        goto err;
134
135
7.54k
    switch (padding) {
136
7.54k
    case RSA_PKCS1_PADDING:
137
7.54k
        i = ossl_rsa_padding_add_PKCS1_type_2_ex(rsa->libctx, buf, num,
138
7.54k
            from, flen);
139
7.54k
        break;
140
0
    case RSA_PKCS1_OAEP_PADDING:
141
0
        i = ossl_rsa_padding_add_PKCS1_OAEP_mgf1_ex(rsa->libctx, buf, num,
142
0
            from, flen, NULL, 0,
143
0
            NULL, NULL);
144
0
        break;
145
0
    case RSA_NO_PADDING:
146
0
        i = RSA_padding_add_none(buf, num, from, flen);
147
0
        break;
148
0
    default:
149
0
        ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
150
0
        goto err;
151
7.54k
    }
152
7.54k
    if (i <= 0)
153
33
        goto err;
154
155
7.50k
    if (BN_bin2bn(buf, num, f) == NULL)
156
0
        goto err;
157
158
#ifdef FIPS_MODULE
159
    /*
160
     * See SP800-56Br2, section 7.1.1.1
161
     * RSAEP: 1 < f < (n – 1).
162
     * (where f is the plaintext).
163
     *
164
     * This bound is somewhat overkill here.  RSASVE.GENERATE (7.2.1.2)
165
     * regenerates z until 1 < z < n-1, so on that path the plaintext is in
166
     * range unconditionally.  On the OAEP path the leading 0x00 octet of the
167
     * encoding forces m < n-1 unconditionally, while m = 0 or 1 is only
168
     * cryptographically negligible, not impossible.  The check is kept to
169
     * mirror the RSADP bound in rsa_ossl_private_decrypt() and to keep RSAEP
170
     * faithful to 7.1.1 of the SP; nothing in the SP relies on it here.
171
     */
172
    if (padding == RSA_NO_PADDING) {
173
        BIGNUM *nminus1 = BN_CTX_get(ctx);
174
175
        if (BN_ucmp(f, BN_value_one()) <= 0) {
176
            ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_SMALL);
177
            goto err;
178
        }
179
        if (nminus1 == NULL
180
            || BN_copy(nminus1, rsa->n) == NULL
181
            || !BN_sub_word(nminus1, 1))
182
            goto err;
183
        if (BN_ucmp(f, nminus1) >= 0) {
184
            ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
185
            goto err;
186
        }
187
    } else
188
#endif
189
7.50k
    {
190
7.50k
        if (BN_ucmp(f, rsa->n) >= 0) {
191
            /* usually the padding functions would catch this */
192
0
            ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
193
0
            goto err;
194
0
        }
195
7.50k
    }
196
197
7.50k
    if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
198
7.50k
        if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
199
7.50k
                rsa->n, ctx))
200
589
            goto err;
201
202
6.92k
    if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
203
6.92k
            rsa->_method_mod_n))
204
0
        goto err;
205
206
    /*
207
     * BN_bn2binpad puts in leading 0 bytes if the number is less than
208
     * the length of the modulus.
209
     */
210
6.92k
    r = BN_bn2binpad(ret, to, num);
211
7.54k
err:
212
7.54k
    BN_CTX_end(ctx);
213
7.54k
    BN_CTX_free(ctx);
214
7.54k
    OPENSSL_clear_free(buf, num);
215
7.54k
    return r;
216
6.92k
}
217
218
static BN_BLINDING *rsa_get_blinding(RSA *rsa, int *local, BN_CTX *ctx)
219
6.11k
{
220
6.11k
    BN_BLINDING *ret;
221
222
6.11k
    if (!CRYPTO_THREAD_read_lock(rsa->lock))
223
0
        return NULL;
224
225
6.11k
    if (rsa->blinding == NULL) {
226
        /*
227
         * This dance with upgrading the lock from read to write will be
228
         * slower in cases of a single use RSA object, but should be
229
         * significantly better in multi-thread cases (e.g. servers). It's
230
         * probably worth it.
231
         */
232
6.11k
        CRYPTO_THREAD_unlock(rsa->lock);
233
6.11k
        if (!CRYPTO_THREAD_write_lock(rsa->lock))
234
0
            return NULL;
235
6.11k
        if (rsa->blinding == NULL)
236
6.11k
            rsa->blinding = RSA_setup_blinding(rsa, ctx);
237
6.11k
    }
238
239
6.11k
    ret = rsa->blinding;
240
6.11k
    if (ret == NULL)
241
0
        goto err;
242
243
6.11k
    if (BN_BLINDING_is_current_thread(ret)) {
244
        /* rsa->blinding is ours! */
245
246
6.11k
        *local = 1;
247
6.11k
    } else {
248
        /* resort to rsa->mt_blinding instead */
249
250
        /*
251
         * instructs rsa_blinding_convert(), rsa_blinding_invert() that the
252
         * BN_BLINDING is shared, meaning that accesses require locks, and
253
         * that the blinding factor must be stored outside the BN_BLINDING
254
         */
255
0
        *local = 0;
256
257
0
        if (rsa->mt_blinding == NULL) {
258
0
            CRYPTO_THREAD_unlock(rsa->lock);
259
0
            if (!CRYPTO_THREAD_write_lock(rsa->lock))
260
0
                return NULL;
261
0
            if (rsa->mt_blinding == NULL)
262
0
                rsa->mt_blinding = RSA_setup_blinding(rsa, ctx);
263
0
        }
264
0
        ret = rsa->mt_blinding;
265
0
    }
266
267
6.11k
err:
268
6.11k
    CRYPTO_THREAD_unlock(rsa->lock);
269
6.11k
    return ret;
270
6.11k
}
271
272
static int rsa_blinding_convert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind,
273
    BN_CTX *ctx)
274
6.11k
{
275
6.11k
    if (unblind == NULL) {
276
        /*
277
         * Local blinding: store the unblinding factor in BN_BLINDING.
278
         */
279
6.11k
        return BN_BLINDING_convert_ex(f, NULL, b, ctx);
280
6.11k
    } else {
281
        /*
282
         * Shared blinding: store the unblinding factor outside BN_BLINDING.
283
         */
284
0
        int ret;
285
286
0
        if (!BN_BLINDING_lock(b))
287
0
            return 0;
288
289
0
        ret = BN_BLINDING_convert_ex(f, unblind, b, ctx);
290
0
        BN_BLINDING_unlock(b);
291
292
0
        return ret;
293
0
    }
294
6.11k
}
295
296
static int rsa_blinding_invert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind,
297
    BN_CTX *ctx)
298
13.2k
{
299
    /*
300
     * For local blinding, unblind is set to NULL, and BN_BLINDING_invert_ex
301
     * will use the unblinding factor stored in BN_BLINDING. If BN_BLINDING
302
     * is shared between threads, unblind must be non-null:
303
     * BN_BLINDING_invert_ex will then use the local unblinding factor, and
304
     * will only read the modulus from BN_BLINDING. In both cases it's safe
305
     * to access the blinding without a lock.
306
     */
307
13.2k
    BN_set_flags(f, BN_FLG_CONSTTIME);
308
13.2k
    return BN_BLINDING_invert_ex(f, unblind, b, ctx);
309
13.2k
}
310
311
/* signing */
312
static int rsa_ossl_private_encrypt(int flen, const unsigned char *from,
313
    unsigned char *to, RSA *rsa, int padding)
314
1.70k
{
315
1.70k
    BIGNUM *f, *ret, *res;
316
1.70k
    int i, num = 0, r = -1;
317
1.70k
    unsigned char *buf = NULL;
318
1.70k
    BN_CTX *ctx = NULL;
319
1.70k
    int local_blinding = 0;
320
    /*
321
     * Used only if the blinding structure is shared. A non-NULL unblind
322
     * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
323
     * the unblinding factor outside the blinding structure.
324
     */
325
1.70k
    BIGNUM *unblind = NULL;
326
1.70k
    BN_BLINDING *blinding = NULL;
327
328
1.70k
    if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
329
0
        goto err;
330
1.70k
    BN_CTX_start(ctx);
331
1.70k
    f = BN_CTX_get(ctx);
332
1.70k
    ret = BN_CTX_get(ctx);
333
1.70k
    num = BN_num_bytes(rsa->n);
334
1.70k
    buf = OPENSSL_malloc(num);
335
1.70k
    if (ret == NULL || buf == NULL)
336
0
        goto err;
337
338
1.70k
    switch (padding) {
339
1.25k
    case RSA_PKCS1_PADDING:
340
1.25k
        i = RSA_padding_add_PKCS1_type_1(buf, num, from, flen);
341
1.25k
        break;
342
0
    case RSA_X931_PADDING:
343
0
        i = RSA_padding_add_X931(buf, num, from, flen);
344
0
        break;
345
451
    case RSA_NO_PADDING:
346
451
        i = RSA_padding_add_none(buf, num, from, flen);
347
451
        break;
348
0
    default:
349
0
        ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
350
0
        goto err;
351
1.70k
    }
352
1.70k
    if (i <= 0)
353
0
        goto err;
354
355
1.70k
    if (BN_bin2bn(buf, num, f) == NULL)
356
0
        goto err;
357
358
1.70k
    if (BN_ucmp(f, rsa->n) >= 0) {
359
        /* usually the padding functions would catch this */
360
0
        ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
361
0
        goto err;
362
0
    }
363
364
1.70k
    if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
365
1.70k
        if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
366
1.70k
                rsa->n, ctx))
367
0
            goto err;
368
369
1.70k
    if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
370
1.70k
        blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
371
1.70k
        if (blinding == NULL) {
372
0
            ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
373
0
            goto err;
374
0
        }
375
1.70k
    }
376
377
1.70k
    if (blinding != NULL) {
378
1.70k
        if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {
379
0
            ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
380
0
            goto err;
381
0
        }
382
1.70k
        if (!rsa_blinding_convert(blinding, f, unblind, ctx))
383
0
            goto err;
384
1.70k
    }
385
386
1.70k
    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))) {
387
1.70k
        if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
388
0
            goto err;
389
1.70k
    } 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
1.70k
    if (blinding)
412
1.70k
        if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
413
0
            goto err;
414
415
1.70k
    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
1.70k
    } else {
423
1.70k
        res = ret;
424
1.70k
    }
425
426
    /*
427
     * BN_bn2binpad puts in leading 0 bytes if the number is less than
428
     * the length of the modulus.
429
     */
430
1.70k
    r = BN_bn2binpad(res, to, num);
431
1.70k
err:
432
1.70k
    BN_CTX_end(ctx);
433
1.70k
    BN_CTX_free(ctx);
434
1.70k
    OPENSSL_clear_free(buf, num);
435
1.70k
    return r;
436
1.70k
}
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
4.42k
{
526
4.42k
    BIGNUM *f, *ret;
527
4.42k
    int j, num = 0, r = -1;
528
4.42k
    unsigned char *buf = NULL;
529
4.42k
    unsigned char kdk[SHA256_DIGEST_LENGTH] = { 0 };
530
4.42k
    BN_CTX *ctx = NULL;
531
4.42k
    int local_blinding = 0;
532
    /*
533
     * Used only if the blinding structure is shared. A non-NULL unblind
534
     * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
535
     * the unblinding factor outside the blinding structure.
536
     */
537
4.42k
    BIGNUM *unblind = NULL;
538
4.42k
    BN_BLINDING *blinding = NULL;
539
540
    /*
541
     * we need the value of the private exponent to perform implicit rejection
542
     */
543
4.42k
    if ((rsa->flags & RSA_FLAG_EXT_PKEY) && (padding == RSA_PKCS1_PADDING))
544
0
        padding = RSA_PKCS1_NO_IMPLICIT_REJECT_PADDING;
545
546
4.42k
    if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
547
0
        goto err;
548
4.42k
    BN_CTX_start(ctx);
549
4.42k
    f = BN_CTX_get(ctx);
550
4.42k
    ret = BN_CTX_get(ctx);
551
4.42k
    if (ret == NULL) {
552
0
        ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
553
0
        goto err;
554
0
    }
555
4.42k
    num = BN_num_bytes(rsa->n);
556
4.42k
    buf = OPENSSL_malloc(num);
557
4.42k
    if (buf == NULL)
558
0
        goto err;
559
560
    /*
561
     * This check was for equality but PGP does evil things and chops off the
562
     * top '0' bytes
563
     */
564
4.42k
    if (flen > num) {
565
2
        ERR_raise(ERR_LIB_RSA, RSA_R_DATA_GREATER_THAN_MOD_LEN);
566
2
        goto err;
567
2
    }
568
569
4.42k
    if (flen < 1) {
570
6
        ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_SMALL);
571
6
        goto err;
572
6
    }
573
574
    /* make data into a big number */
575
4.41k
    if (BN_bin2bn(from, (int)flen, f) == NULL)
576
0
        goto err;
577
578
#ifdef FIPS_MODULE
579
    /*
580
     * See SP800-56Br2, section 7.1.2.1
581
     * RSADP: 1 < f < (n – 1)
582
     * (where f is the ciphertext).
583
     *
584
     * Kept under FIPS_MODULE because SP 800-56B KTS-OAEP (section 9.2) also
585
     * decrypts through RSADP and needs this bound in a FIPS build, and there
586
     * is no KTS-OAEP-specific path to attach it to.  The non-FIPS RSASVE path
587
     * applies the same 1 < c < n-1 in rsasve_recover()
588
     * (providers/implementations/kem/rsa_kem.c); keep the two in step.
589
     */
590
    if (padding == RSA_NO_PADDING) {
591
        BIGNUM *nminus1 = BN_CTX_get(ctx);
592
593
        if (BN_ucmp(f, BN_value_one()) <= 0) {
594
            ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_SMALL);
595
            goto err;
596
        }
597
        if (nminus1 == NULL
598
            || BN_copy(nminus1, rsa->n) == NULL
599
            || !BN_sub_word(nminus1, 1))
600
            goto err;
601
        if (BN_ucmp(f, nminus1) >= 0) {
602
            ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
603
            goto err;
604
        }
605
    } else
606
#endif
607
4.41k
    {
608
4.41k
        if (BN_ucmp(f, rsa->n) >= 0) {
609
5
            ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
610
5
            goto err;
611
5
        }
612
4.41k
    }
613
4.41k
    if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
614
4.41k
        if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
615
4.41k
                rsa->n, ctx))
616
0
            goto err;
617
618
4.41k
    if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
619
4.41k
        blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
620
4.41k
        if (blinding == NULL) {
621
0
            ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
622
0
            goto err;
623
0
        }
624
4.41k
    }
625
626
4.41k
    if (blinding != NULL) {
627
4.41k
        if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {
628
0
            ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
629
0
            goto err;
630
0
        }
631
4.41k
        if (!rsa_blinding_convert(blinding, f, unblind, ctx))
632
0
            goto err;
633
4.41k
    }
634
635
    /* do the decrypt */
636
4.41k
    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))) {
637
4.41k
        if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
638
0
            goto err;
639
4.41k
    } else {
640
0
        BIGNUM *d = BN_new();
641
0
        if (d == NULL) {
642
0
            ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
643
0
            goto err;
644
0
        }
645
0
        if (rsa->d == NULL) {
646
0
            ERR_raise(ERR_LIB_RSA, RSA_R_MISSING_PRIVATE_KEY);
647
0
            BN_free(d);
648
0
            goto err;
649
0
        }
650
0
        BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
651
0
        if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
652
0
                rsa->_method_mod_n)) {
653
0
            BN_free(d);
654
0
            goto err;
655
0
        }
656
        /* We MUST free d before any further use of rsa->d */
657
0
        BN_free(d);
658
0
    }
659
660
4.41k
    if (blinding)
661
4.41k
        if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
662
0
            goto err;
663
664
    /*
665
     * derive the Key Derivation Key from private exponent and public
666
     * ciphertext
667
     */
668
4.41k
    if (padding == RSA_PKCS1_PADDING) {
669
0
        if (derive_kdk(flen, from, rsa, buf, num, kdk) == 0)
670
0
            goto err;
671
0
    }
672
673
4.41k
    j = BN_bn2binpad(ret, buf, num);
674
4.41k
    if (j < 0)
675
0
        goto err;
676
677
4.41k
    switch (padding) {
678
0
    case RSA_PKCS1_NO_IMPLICIT_REJECT_PADDING:
679
0
        r = RSA_padding_check_PKCS1_type_2(to, num, buf, j, num);
680
0
        break;
681
0
    case RSA_PKCS1_PADDING:
682
0
        r = ossl_rsa_padding_check_PKCS1_type_2(rsa->libctx, to, num, buf, j, num, kdk);
683
0
        break;
684
0
    case RSA_PKCS1_OAEP_PADDING:
685
0
        r = RSA_padding_check_PKCS1_OAEP(to, num, buf, j, num, NULL, 0);
686
0
        break;
687
4.41k
    case RSA_NO_PADDING:
688
4.41k
        memcpy(to, buf, (r = j));
689
4.41k
        break;
690
0
    default:
691
0
        ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
692
0
        goto err;
693
4.41k
    }
694
4.41k
#ifndef FIPS_MODULE
695
    /*
696
     * This trick doesn't work in the FIPS provider because libcrypto manages
697
     * the error stack. Instead we opt not to put an error on the stack at all
698
     * in case of padding failure in the FIPS provider.
699
     */
700
4.41k
    ERR_raise(ERR_LIB_RSA, RSA_R_PADDING_CHECK_FAILED);
701
4.41k
    err_clear_last_constant_time(1 & ~constant_time_msb(r));
702
4.41k
#endif
703
704
4.42k
err:
705
4.42k
    BN_CTX_end(ctx);
706
4.42k
    BN_CTX_free(ctx);
707
4.42k
    OPENSSL_clear_free(buf, num);
708
4.42k
    return r;
709
4.41k
}
710
711
/* signature verification */
712
static int rsa_ossl_public_decrypt(int flen, const unsigned char *from,
713
    unsigned char *to, RSA *rsa, int padding)
714
22.4k
{
715
22.4k
    BIGNUM *f, *ret;
716
22.4k
    int i, num = 0, r = -1;
717
22.4k
    unsigned char *buf = NULL;
718
22.4k
    BN_CTX *ctx = NULL;
719
720
22.4k
    if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
721
0
        ERR_raise(ERR_LIB_RSA, RSA_R_MODULUS_TOO_LARGE);
722
0
        return -1;
723
0
    }
724
725
22.4k
    if (BN_ucmp(rsa->n, rsa->e) <= 0) {
726
16
        ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
727
16
        return -1;
728
16
    }
729
730
    /* for large moduli, enforce exponent limit */
731
22.4k
    if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
732
15
        if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
733
0
            ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
734
0
            return -1;
735
0
        }
736
15
    }
737
738
22.4k
    if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
739
0
        goto err;
740
22.4k
    BN_CTX_start(ctx);
741
22.4k
    f = BN_CTX_get(ctx);
742
22.4k
    ret = BN_CTX_get(ctx);
743
22.4k
    if (ret == NULL) {
744
0
        ERR_raise(ERR_LIB_RSA, ERR_R_BN_LIB);
745
0
        goto err;
746
0
    }
747
22.4k
    num = BN_num_bytes(rsa->n);
748
22.4k
    buf = OPENSSL_malloc(num);
749
22.4k
    if (buf == NULL)
750
0
        goto err;
751
752
    /*
753
     * This check was for equality but PGP does evil things and chops off the
754
     * top '0' bytes
755
     */
756
22.4k
    if (flen > num) {
757
2.12k
        ERR_raise(ERR_LIB_RSA, RSA_R_DATA_GREATER_THAN_MOD_LEN);
758
2.12k
        goto err;
759
2.12k
    }
760
761
20.2k
    if (BN_bin2bn(from, flen, f) == NULL)
762
0
        goto err;
763
764
20.2k
    if (BN_ucmp(f, rsa->n) >= 0) {
765
695
        ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
766
695
        goto err;
767
695
    }
768
769
19.6k
    if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
770
19.6k
        if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
771
19.6k
                rsa->n, ctx))
772
2.23k
            goto err;
773
774
17.3k
    if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
775
17.3k
            rsa->_method_mod_n))
776
0
        goto err;
777
778
    /* For X9.31: Assuming e is odd it does a 12 mod 16 test */
779
17.3k
    if ((padding == RSA_X931_PADDING) && ((bn_get_words(ret)[0] & 0xf) != 12))
780
0
        if (!BN_sub(ret, rsa->n, ret))
781
0
            goto err;
782
783
17.3k
    i = BN_bn2binpad(ret, buf, num);
784
17.3k
    if (i < 0)
785
0
        goto err;
786
787
17.3k
    switch (padding) {
788
5.23k
    case RSA_PKCS1_PADDING:
789
5.23k
        r = RSA_padding_check_PKCS1_type_1(to, num, buf, i, num);
790
5.23k
        break;
791
0
    case RSA_X931_PADDING:
792
0
        r = RSA_padding_check_X931(to, num, buf, i, num);
793
0
        break;
794
12.1k
    case RSA_NO_PADDING:
795
12.1k
        memcpy(to, buf, (r = i));
796
12.1k
        break;
797
0
    default:
798
0
        ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
799
0
        goto err;
800
17.3k
    }
801
17.3k
    if (r < 0)
802
17.3k
        ERR_raise(ERR_LIB_RSA, RSA_R_PADDING_CHECK_FAILED);
803
804
22.4k
err:
805
22.4k
    BN_CTX_end(ctx);
806
22.4k
    BN_CTX_free(ctx);
807
22.4k
    OPENSSL_clear_free(buf, num);
808
22.4k
    return r;
809
17.3k
}
810
811
static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
812
13.2k
{
813
13.2k
    BIGNUM *r1, *m1, *vrfy;
814
13.2k
    int ret = 0, smooth = 0;
815
13.2k
#ifndef FIPS_MODULE
816
13.2k
    BIGNUM *r2, *m[RSA_MAX_PRIME_NUM - 2];
817
13.2k
    int i, ex_primes = 0;
818
13.2k
    RSA_PRIME_INFO *pinfo;
819
13.2k
#endif
820
821
13.2k
    BN_CTX_start(ctx);
822
823
13.2k
    r1 = BN_CTX_get(ctx);
824
13.2k
#ifndef FIPS_MODULE
825
13.2k
    r2 = BN_CTX_get(ctx);
826
13.2k
#endif
827
13.2k
    m1 = BN_CTX_get(ctx);
828
13.2k
    vrfy = BN_CTX_get(ctx);
829
13.2k
    if (vrfy == NULL)
830
0
        goto err;
831
832
13.2k
#ifndef FIPS_MODULE
833
13.2k
    if (rsa->version == RSA_ASN1_VERSION_MULTI
834
0
        && ((ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) <= 0
835
0
            || ex_primes > RSA_MAX_PRIME_NUM - 2))
836
0
        goto err;
837
13.2k
#endif
838
839
13.2k
    if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {
840
13.2k
        BIGNUM *factor = BN_new();
841
842
13.2k
        if (factor == NULL)
843
0
            goto err;
844
845
        /*
846
         * Make sure BN_mod_inverse in Montgomery initialization uses the
847
         * BN_FLG_CONSTTIME flag
848
         */
849
13.2k
        if (!(BN_with_flags(factor, rsa->p, BN_FLG_CONSTTIME),
850
13.2k
                BN_MONT_CTX_set_locked(&rsa->_method_mod_p, rsa->lock,
851
13.2k
                    factor, ctx))
852
13.2k
            || !(BN_with_flags(factor, rsa->q, BN_FLG_CONSTTIME),
853
13.2k
                BN_MONT_CTX_set_locked(&rsa->_method_mod_q, rsa->lock,
854
13.2k
                    factor, ctx))) {
855
0
            BN_free(factor);
856
0
            goto err;
857
0
        }
858
13.2k
#ifndef FIPS_MODULE
859
13.2k
        for (i = 0; i < ex_primes; i++) {
860
0
            pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
861
0
            BN_with_flags(factor, pinfo->r, BN_FLG_CONSTTIME);
862
0
            if (!BN_MONT_CTX_set_locked(&pinfo->m, rsa->lock, factor, ctx)) {
863
0
                BN_free(factor);
864
0
                goto err;
865
0
            }
866
0
        }
867
13.2k
#endif
868
        /*
869
         * We MUST free |factor| before any further use of the prime factors
870
         */
871
13.2k
        BN_free(factor);
872
873
13.2k
        smooth = (rsa->meth->bn_mod_exp == BN_mod_exp_mont)
874
13.2k
#ifndef FIPS_MODULE
875
13.2k
            && (ex_primes == 0)
876
13.2k
#endif
877
13.2k
            && (BN_num_bits(rsa->q) == BN_num_bits(rsa->p));
878
13.2k
    }
879
880
13.2k
    if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
881
13.2k
        if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
882
13.2k
                rsa->n, ctx))
883
0
            goto err;
884
885
13.2k
    if (smooth) {
886
        /*
887
         * Conversion from Montgomery domain, a.k.a. Montgomery reduction,
888
         * accepts values in [0-m*2^w) range. w is m's bit width rounded up
889
         * to limb width. So that at the very least if |I| is fully reduced,
890
         * i.e. less than p*q, we can count on from-to round to perform
891
         * below modulo operations on |I|. Unlike BN_mod it's constant time.
892
         */
893
13.2k
        if (/* m1 = I moq q */
894
13.2k
            !bn_from_mont_fixed_top(m1, I, rsa->_method_mod_q, ctx)
895
13.2k
            || !bn_to_mont_fixed_top(m1, m1, rsa->_method_mod_q, ctx)
896
            /* r1 = I mod p */
897
13.2k
            || !bn_from_mont_fixed_top(r1, I, rsa->_method_mod_p, ctx)
898
13.2k
            || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)
899
            /*
900
             * Use parallel exponentiations optimization if possible,
901
             * otherwise fallback to two sequential exponentiations:
902
             *    m1 = m1^dmq1 mod q
903
             *    r1 = r1^dmp1 mod p
904
             */
905
13.2k
            || !BN_mod_exp_mont_consttime_x2(m1, m1, rsa->dmq1, rsa->q,
906
13.2k
                rsa->_method_mod_q,
907
13.2k
                r1, r1, rsa->dmp1, rsa->p,
908
13.2k
                rsa->_method_mod_p,
909
13.2k
                ctx)
910
            /* r1 = (r1 - m1) mod p */
911
            /*
912
             * bn_mod_sub_fixed_top is not regular modular subtraction,
913
             * it can tolerate subtrahend to be larger than modulus, but
914
             * not bit-wise wider. This makes up for uncommon q>p case,
915
             * when |m1| can be larger than |rsa->p|.
916
             */
917
13.2k
            || !bn_mod_sub_fixed_top(r1, r1, m1, rsa->p)
918
919
            /* r1 = r1 * iqmp mod p */
920
13.2k
            || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)
921
13.2k
            || !bn_mul_mont_fixed_top(r1, r1, rsa->iqmp, rsa->_method_mod_p,
922
13.2k
                ctx)
923
            /* r0 = r1 * q + m1 */
924
13.2k
            || !bn_mul_fixed_top(r0, r1, rsa->q, ctx)
925
13.2k
            || !bn_mod_add_fixed_top(r0, r0, m1, rsa->n))
926
0
            goto err;
927
928
13.2k
        goto tail;
929
13.2k
    }
930
931
    /* compute I mod q */
932
0
    {
933
0
        BIGNUM *c = BN_new();
934
0
        if (c == NULL)
935
0
            goto err;
936
0
        BN_with_flags(c, I, BN_FLG_CONSTTIME);
937
938
0
        if (!BN_mod(r1, c, rsa->q, ctx)) {
939
0
            BN_free(c);
940
0
            goto err;
941
0
        }
942
943
0
        {
944
0
            BIGNUM *dmq1 = BN_new();
945
0
            if (dmq1 == NULL) {
946
0
                BN_free(c);
947
0
                goto err;
948
0
            }
949
0
            BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
950
951
            /* compute r1^dmq1 mod q */
952
0
            if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx,
953
0
                    rsa->_method_mod_q)) {
954
0
                BN_free(c);
955
0
                BN_free(dmq1);
956
0
                goto err;
957
0
            }
958
            /* We MUST free dmq1 before any further use of rsa->dmq1 */
959
0
            BN_free(dmq1);
960
0
        }
961
962
        /* compute I mod p */
963
0
        if (!BN_mod(r1, c, rsa->p, ctx)) {
964
0
            BN_free(c);
965
0
            goto err;
966
0
        }
967
        /* We MUST free c before any further use of I */
968
0
        BN_free(c);
969
0
    }
970
971
0
    {
972
0
        BIGNUM *dmp1 = BN_new();
973
0
        if (dmp1 == NULL)
974
0
            goto err;
975
0
        BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
976
977
        /* compute r1^dmp1 mod p */
978
0
        if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx,
979
0
                rsa->_method_mod_p)) {
980
0
            BN_free(dmp1);
981
0
            goto err;
982
0
        }
983
        /* We MUST free dmp1 before any further use of rsa->dmp1 */
984
0
        BN_free(dmp1);
985
0
    }
986
987
0
#ifndef FIPS_MODULE
988
0
    if (ex_primes > 0) {
989
0
        BIGNUM *di = BN_new(), *cc = BN_new();
990
991
0
        if (cc == NULL || di == NULL) {
992
0
            BN_free(cc);
993
0
            BN_free(di);
994
0
            goto err;
995
0
        }
996
997
0
        for (i = 0; i < ex_primes; i++) {
998
            /* prepare m_i */
999
0
            if ((m[i] = BN_CTX_get(ctx)) == NULL) {
1000
0
                BN_free(cc);
1001
0
                BN_free(di);
1002
0
                goto err;
1003
0
            }
1004
1005
0
            pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
1006
1007
            /* prepare c and d_i */
1008
0
            BN_with_flags(cc, I, BN_FLG_CONSTTIME);
1009
0
            BN_with_flags(di, pinfo->d, BN_FLG_CONSTTIME);
1010
1011
0
            if (!BN_mod(r1, cc, pinfo->r, ctx)) {
1012
0
                BN_free(cc);
1013
0
                BN_free(di);
1014
0
                goto err;
1015
0
            }
1016
            /* compute r1 ^ d_i mod r_i */
1017
0
            if (!rsa->meth->bn_mod_exp(m[i], r1, di, pinfo->r, ctx, pinfo->m)) {
1018
0
                BN_free(cc);
1019
0
                BN_free(di);
1020
0
                goto err;
1021
0
            }
1022
0
        }
1023
1024
0
        BN_free(cc);
1025
0
        BN_free(di);
1026
0
    }
1027
0
#endif
1028
1029
0
    if (!BN_sub(r0, r0, m1))
1030
0
        goto err;
1031
    /*
1032
     * This will help stop the size of r0 increasing, which does affect the
1033
     * multiply if it optimised for a power of 2 size
1034
     */
1035
0
    if (BN_is_negative(r0))
1036
0
        if (!BN_add(r0, r0, rsa->p))
1037
0
            goto err;
1038
1039
0
    if (!BN_mul(r1, r0, rsa->iqmp, ctx))
1040
0
        goto err;
1041
1042
0
    {
1043
0
        BIGNUM *pr1 = BN_new();
1044
0
        if (pr1 == NULL)
1045
0
            goto err;
1046
0
        BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
1047
1048
0
        if (!BN_mod(r0, pr1, rsa->p, ctx)) {
1049
0
            BN_free(pr1);
1050
0
            goto err;
1051
0
        }
1052
        /* We MUST free pr1 before any further use of r1 */
1053
0
        BN_free(pr1);
1054
0
    }
1055
1056
    /*
1057
     * If p < q it is occasionally possible for the correction of adding 'p'
1058
     * if r0 is negative above to leave the result still negative. This can
1059
     * break the private key operations: the following second correction
1060
     * should *always* correct this rare occurrence. This will *never* happen
1061
     * with OpenSSL generated keys because they ensure p > q [steve]
1062
     */
1063
0
    if (BN_is_negative(r0))
1064
0
        if (!BN_add(r0, r0, rsa->p))
1065
0
            goto err;
1066
0
    if (!BN_mul(r1, r0, rsa->q, ctx))
1067
0
        goto err;
1068
0
    if (!BN_add(r0, r1, m1))
1069
0
        goto err;
1070
1071
0
#ifndef FIPS_MODULE
1072
    /* add m_i to m in multi-prime case */
1073
0
    if (ex_primes > 0) {
1074
0
        BIGNUM *pr2 = BN_new();
1075
1076
0
        if (pr2 == NULL)
1077
0
            goto err;
1078
1079
0
        for (i = 0; i < ex_primes; i++) {
1080
0
            pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
1081
0
            if (!BN_sub(r1, m[i], r0)) {
1082
0
                BN_free(pr2);
1083
0
                goto err;
1084
0
            }
1085
1086
0
            if (!BN_mul(r2, r1, pinfo->t, ctx)) {
1087
0
                BN_free(pr2);
1088
0
                goto err;
1089
0
            }
1090
1091
0
            BN_with_flags(pr2, r2, BN_FLG_CONSTTIME);
1092
1093
0
            if (!BN_mod(r1, pr2, pinfo->r, ctx)) {
1094
0
                BN_free(pr2);
1095
0
                goto err;
1096
0
            }
1097
1098
0
            if (BN_is_negative(r1))
1099
0
                if (!BN_add(r1, r1, pinfo->r)) {
1100
0
                    BN_free(pr2);
1101
0
                    goto err;
1102
0
                }
1103
0
            if (!BN_mul(r1, r1, pinfo->pp, ctx)) {
1104
0
                BN_free(pr2);
1105
0
                goto err;
1106
0
            }
1107
0
            if (!BN_add(r0, r0, r1)) {
1108
0
                BN_free(pr2);
1109
0
                goto err;
1110
0
            }
1111
0
        }
1112
0
        BN_free(pr2);
1113
0
    }
1114
0
#endif
1115
1116
13.2k
tail:
1117
13.2k
    if (rsa->e && rsa->n) {
1118
13.2k
        if (rsa->meth->bn_mod_exp == BN_mod_exp_mont) {
1119
13.2k
            if (!BN_mod_exp_mont(vrfy, r0, rsa->e, rsa->n, ctx,
1120
13.2k
                    rsa->_method_mod_n))
1121
0
                goto err;
1122
13.2k
        } else {
1123
0
            bn_correct_top(r0);
1124
0
            if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,
1125
0
                    rsa->_method_mod_n))
1126
0
                goto err;
1127
0
        }
1128
        /*
1129
         * If 'I' was greater than (or equal to) rsa->n, the operation will
1130
         * be equivalent to using 'I mod n'. However, the result of the
1131
         * verify will *always* be less than 'n' so we don't check for
1132
         * absolute equality, just congruency.
1133
         */
1134
13.2k
        if (!BN_sub(vrfy, vrfy, I))
1135
0
            goto err;
1136
13.2k
        if (BN_is_zero(vrfy)) {
1137
13.2k
            bn_correct_top(r0);
1138
13.2k
            ret = 1;
1139
13.2k
            goto err; /* not actually error */
1140
13.2k
        }
1141
0
        if (!BN_mod(vrfy, vrfy, rsa->n, ctx))
1142
0
            goto err;
1143
0
        if (BN_is_negative(vrfy))
1144
0
            if (!BN_add(vrfy, vrfy, rsa->n))
1145
0
                goto err;
1146
0
        if (!BN_is_zero(vrfy)) {
1147
            /*
1148
             * 'I' and 'vrfy' aren't congruent mod n. Don't leak
1149
             * miscalculated CRT output, just do a raw (slower) mod_exp and
1150
             * return that instead.
1151
             */
1152
1153
0
            BIGNUM *d = BN_new();
1154
0
            if (d == NULL)
1155
0
                goto err;
1156
0
            BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
1157
1158
0
            if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx,
1159
0
                    rsa->_method_mod_n)) {
1160
0
                BN_free(d);
1161
0
                goto err;
1162
0
            }
1163
            /* We MUST free d before any further use of rsa->d */
1164
0
            BN_free(d);
1165
0
        }
1166
0
    }
1167
    /*
1168
     * It's unfortunate that we have to bn_correct_top(r0). What hopefully
1169
     * saves the day is that correction is highly unlike, and private key
1170
     * operations are customarily performed on blinded message. Which means
1171
     * that attacker won't observe correlation with chosen plaintext.
1172
     * Secondly, remaining code would still handle it in same computational
1173
     * time and even conceal memory access pattern around corrected top.
1174
     */
1175
0
    bn_correct_top(r0);
1176
0
    ret = 1;
1177
13.2k
err:
1178
13.2k
    BN_CTX_end(ctx);
1179
13.2k
    return ret;
1180
0
}
1181
1182
static int rsa_ossl_init(RSA *rsa)
1183
435k
{
1184
435k
    rsa->flags |= RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE;
1185
435k
    return 1;
1186
435k
}
1187
1188
static int rsa_ossl_finish(RSA *rsa)
1189
435k
{
1190
435k
#ifndef FIPS_MODULE
1191
435k
    int i;
1192
435k
    RSA_PRIME_INFO *pinfo;
1193
1194
632k
    for (i = 0; i < sk_RSA_PRIME_INFO_num(rsa->prime_infos); i++) {
1195
196k
        pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
1196
196k
        BN_MONT_CTX_free(pinfo->m);
1197
196k
    }
1198
435k
#endif
1199
1200
435k
    BN_MONT_CTX_free(rsa->_method_mod_n);
1201
435k
    BN_MONT_CTX_free(rsa->_method_mod_p);
1202
435k
    BN_MONT_CTX_free(rsa->_method_mod_q);
1203
435k
    return 1;
1204
435k
}
1205
1206
#ifdef S390X_MOD_EXP
1207
static int rsa_ossl_s390x_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa,
1208
    BN_CTX *ctx)
1209
{
1210
    if (rsa->version != RSA_ASN1_VERSION_MULTI) {
1211
        if (s390x_crt(r0, i, rsa->p, rsa->q, rsa->dmp1, rsa->dmq1, rsa->iqmp) == 1)
1212
            return 1;
1213
    }
1214
    return rsa_ossl_mod_exp(r0, i, rsa, ctx);
1215
}
1216
1217
#endif