Coverage Report

Created: 2026-09-12 06:55

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