Coverage Report

Created: 2023-06-07 07:24

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