Coverage Report

Created: 2025-07-11 06:57

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