Coverage Report

Created: 2025-12-31 06:58

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