Coverage Report

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