Coverage Report

Created: 2025-12-31 06:58

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