Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/rsa/rsa_ossl.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
/*
11
 * RSA low level APIs are deprecated for public use, but still ok for
12
 * internal use.
13
 */
14
#include "internal/deprecated.h"
15
16
#include "internal/cryptlib.h"
17
#include "crypto/bn.h"
18
#include "rsa_local.h"
19
#include "internal/constant_time.h"
20
21
static int rsa_ossl_public_encrypt(int flen, const unsigned char *from,
22
    unsigned char *to, RSA *rsa, int padding);
23
static int rsa_ossl_private_encrypt(int flen, const unsigned char *from,
24
    unsigned char *to, RSA *rsa, int padding);
25
static int rsa_ossl_public_decrypt(int flen, const unsigned char *from,
26
    unsigned char *to, RSA *rsa, int padding);
27
static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
28
    unsigned char *to, RSA *rsa, int padding);
29
static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa,
30
    BN_CTX *ctx);
31
static int rsa_ossl_init(RSA *rsa);
32
static int rsa_ossl_finish(RSA *rsa);
33
static RSA_METHOD rsa_pkcs1_ossl_meth = {
34
    "OpenSSL PKCS#1 RSA",
35
    rsa_ossl_public_encrypt,
36
    rsa_ossl_public_decrypt, /* signature verification */
37
    rsa_ossl_private_encrypt, /* signing */
38
    rsa_ossl_private_decrypt,
39
    rsa_ossl_mod_exp,
40
    BN_mod_exp_mont, /* XXX probably we should not use Montgomery
41
                      * if e == 3 */
42
    rsa_ossl_init,
43
    rsa_ossl_finish,
44
    RSA_FLAG_FIPS_METHOD, /* flags */
45
    NULL,
46
    0, /* rsa_sign */
47
    0, /* rsa_verify */
48
    NULL, /* rsa_keygen */
49
    NULL /* rsa_multi_prime_keygen */
50
};
51
52
static const RSA_METHOD *default_RSA_meth = &rsa_pkcs1_ossl_meth;
53
54
void RSA_set_default_method(const RSA_METHOD *meth)
55
0
{
56
0
    default_RSA_meth = meth;
57
0
}
58
59
const RSA_METHOD *RSA_get_default_method(void)
60
592k
{
61
592k
    return default_RSA_meth;
62
592k
}
63
64
const RSA_METHOD *RSA_PKCS1_OpenSSL(void)
65
201k
{
66
201k
    return &rsa_pkcs1_ossl_meth;
67
201k
}
68
69
const RSA_METHOD *RSA_null_method(void)
70
0
{
71
0
    return NULL;
72
0
}
73
74
static int rsa_ossl_public_encrypt(int flen, const unsigned char *from,
75
    unsigned char *to, RSA *rsa, int padding)
76
6.25k
{
77
6.25k
    BIGNUM *f, *ret;
78
6.25k
    int i, num = 0, r = -1;
79
6.25k
    unsigned char *buf = NULL;
80
6.25k
    BN_CTX *ctx = NULL;
81
82
6.25k
    if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
83
0
        ERR_raise(ERR_LIB_RSA, RSA_R_MODULUS_TOO_LARGE);
84
0
        return -1;
85
0
    }
86
87
6.25k
    if (BN_ucmp(rsa->n, rsa->e) <= 0) {
88
9
        ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
89
9
        return -1;
90
9
    }
91
92
    /* for large moduli, enforce exponent limit */
93
6.25k
    if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
94
25
        if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
95
0
            ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
96
0
            return -1;
97
0
        }
98
25
    }
99
100
6.25k
    if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
101
0
        goto err;
102
6.25k
    BN_CTX_start(ctx);
103
6.25k
    f = BN_CTX_get(ctx);
104
6.25k
    ret = BN_CTX_get(ctx);
105
6.25k
    num = BN_num_bytes(rsa->n);
106
6.25k
    buf = OPENSSL_malloc(num);
107
6.25k
    if (ret == NULL || buf == NULL) {
108
0
        ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
109
0
        goto err;
110
0
    }
111
112
6.25k
    switch (padding) {
113
6.25k
    case RSA_PKCS1_PADDING:
114
6.25k
        i = ossl_rsa_padding_add_PKCS1_type_2_ex(rsa->libctx, buf, num,
115
6.25k
            from, flen);
116
6.25k
        break;
117
0
    case RSA_PKCS1_OAEP_PADDING:
118
0
        i = ossl_rsa_padding_add_PKCS1_OAEP_mgf1_ex(rsa->libctx, buf, num,
119
0
            from, flen, NULL, 0,
120
0
            NULL, NULL);
121
0
        break;
122
0
    case RSA_NO_PADDING:
123
0
        i = RSA_padding_add_none(buf, num, from, flen);
124
0
        break;
125
0
    default:
126
0
        ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
127
0
        goto err;
128
6.25k
    }
129
6.25k
    if (i <= 0)
130
50
        goto err;
131
132
6.20k
    if (BN_bin2bn(buf, num, f) == NULL)
133
0
        goto err;
134
135
6.20k
    if (BN_ucmp(f, rsa->n) >= 0) {
136
        /* usually the padding functions would catch this */
137
0
        ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
138
0
        goto err;
139
0
    }
140
141
6.20k
    if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
142
6.20k
        if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
143
6.20k
                rsa->n, ctx))
144
675
            goto err;
145
146
5.52k
    if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
147
5.52k
            rsa->_method_mod_n))
148
0
        goto err;
149
150
    /*
151
     * BN_bn2binpad puts in leading 0 bytes if the number is less than
152
     * the length of the modulus.
153
     */
154
5.52k
    r = BN_bn2binpad(ret, to, num);
155
6.25k
err:
156
6.25k
    BN_CTX_end(ctx);
157
6.25k
    BN_CTX_free(ctx);
158
6.25k
    OPENSSL_clear_free(buf, num);
159
6.25k
    return r;
160
5.52k
}
161
162
static BN_BLINDING *rsa_get_blinding(RSA *rsa, int *local, BN_CTX *ctx)
163
1.15k
{
164
1.15k
    BN_BLINDING *ret;
165
166
1.15k
    if (!CRYPTO_THREAD_write_lock(rsa->lock))
167
0
        return NULL;
168
169
1.15k
    if (rsa->blinding == NULL) {
170
1.15k
        rsa->blinding = RSA_setup_blinding(rsa, ctx);
171
1.15k
    }
172
173
1.15k
    ret = rsa->blinding;
174
1.15k
    if (ret == NULL)
175
0
        goto err;
176
177
1.15k
    if (BN_BLINDING_is_current_thread(ret)) {
178
        /* rsa->blinding is ours! */
179
180
1.15k
        *local = 1;
181
1.15k
    } else {
182
        /* resort to rsa->mt_blinding instead */
183
184
        /*
185
         * instructs rsa_blinding_convert(), rsa_blinding_invert() that the
186
         * BN_BLINDING is shared, meaning that accesses require locks, and
187
         * that the blinding factor must be stored outside the BN_BLINDING
188
         */
189
0
        *local = 0;
190
191
0
        if (rsa->mt_blinding == NULL) {
192
0
            rsa->mt_blinding = RSA_setup_blinding(rsa, ctx);
193
0
        }
194
0
        ret = rsa->mt_blinding;
195
0
    }
196
197
1.15k
err:
198
1.15k
    CRYPTO_THREAD_unlock(rsa->lock);
199
1.15k
    return ret;
200
1.15k
}
201
202
static int rsa_blinding_convert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind,
203
    BN_CTX *ctx)
204
10.6k
{
205
10.6k
    if (unblind == NULL) {
206
        /*
207
         * Local blinding: store the unblinding factor in BN_BLINDING.
208
         */
209
10.6k
        return BN_BLINDING_convert_ex(f, NULL, b, ctx);
210
10.6k
    } else {
211
        /*
212
         * Shared blinding: store the unblinding factor outside BN_BLINDING.
213
         */
214
0
        int ret;
215
216
0
        if (!BN_BLINDING_lock(b))
217
0
            return 0;
218
219
0
        ret = BN_BLINDING_convert_ex(f, unblind, b, ctx);
220
0
        BN_BLINDING_unlock(b);
221
222
0
        return ret;
223
0
    }
224
10.6k
}
225
226
static int rsa_blinding_invert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind,
227
    BN_CTX *ctx)
228
16.3k
{
229
    /*
230
     * For local blinding, unblind is set to NULL, and BN_BLINDING_invert_ex
231
     * will use the unblinding factor stored in BN_BLINDING. If BN_BLINDING
232
     * is shared between threads, unblind must be non-null:
233
     * BN_BLINDING_invert_ex will then use the local unblinding factor, and
234
     * will only read the modulus from BN_BLINDING. In both cases it's safe
235
     * to access the blinding without a lock.
236
     */
237
16.3k
    BN_set_flags(f, BN_FLG_CONSTTIME);
238
16.3k
    return BN_BLINDING_invert_ex(f, unblind, b, ctx);
239
16.3k
}
240
241
/* signing */
242
static int rsa_ossl_private_encrypt(int flen, const unsigned char *from,
243
    unsigned char *to, RSA *rsa, int padding)
244
3.48k
{
245
3.48k
    BIGNUM *f, *ret, *res;
246
3.48k
    int i, num = 0, r = -1;
247
3.48k
    unsigned char *buf = NULL;
248
3.48k
    BN_CTX *ctx = NULL;
249
3.48k
    int local_blinding = 0;
250
    /*
251
     * Used only if the blinding structure is shared. A non-NULL unblind
252
     * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
253
     * the unblinding factor outside the blinding structure.
254
     */
255
3.48k
    BIGNUM *unblind = NULL;
256
3.48k
    BN_BLINDING *blinding = NULL;
257
258
3.48k
    if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
259
0
        goto err;
260
3.48k
    BN_CTX_start(ctx);
261
3.48k
    f = BN_CTX_get(ctx);
262
3.48k
    ret = BN_CTX_get(ctx);
263
3.48k
    num = BN_num_bytes(rsa->n);
264
3.48k
    buf = OPENSSL_malloc(num);
265
3.48k
    if (ret == NULL || buf == NULL) {
266
0
        ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
267
0
        goto err;
268
0
    }
269
270
3.48k
    switch (padding) {
271
2.80k
    case RSA_PKCS1_PADDING:
272
2.80k
        i = RSA_padding_add_PKCS1_type_1(buf, num, from, flen);
273
2.80k
        break;
274
0
    case RSA_X931_PADDING:
275
0
        i = RSA_padding_add_X931(buf, num, from, flen);
276
0
        break;
277
677
    case RSA_NO_PADDING:
278
677
        i = RSA_padding_add_none(buf, num, from, flen);
279
677
        break;
280
0
    default:
281
0
        ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
282
0
        goto err;
283
3.48k
    }
284
3.48k
    if (i <= 0)
285
0
        goto err;
286
287
3.48k
    if (BN_bin2bn(buf, num, f) == NULL)
288
0
        goto err;
289
290
3.48k
    if (BN_ucmp(f, rsa->n) >= 0) {
291
        /* usually the padding functions would catch this */
292
0
        ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
293
0
        goto err;
294
0
    }
295
296
3.48k
    if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
297
3.48k
        if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
298
3.48k
                rsa->n, ctx))
299
0
            goto err;
300
301
3.48k
    if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
302
3.48k
        blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
303
3.48k
        if (blinding == NULL) {
304
0
            ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
305
0
            goto err;
306
0
        }
307
3.48k
    }
308
309
3.48k
    if (blinding != NULL) {
310
3.48k
        if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {
311
0
            ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
312
0
            goto err;
313
0
        }
314
3.48k
        if (!rsa_blinding_convert(blinding, f, unblind, ctx))
315
0
            goto err;
316
3.48k
    }
317
318
3.48k
    if ((rsa->flags & RSA_FLAG_EXT_PKEY) || (rsa->version == RSA_ASN1_VERSION_MULTI) || ((rsa->p != NULL) && (rsa->q != NULL) && (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
319
3.48k
        if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
320
0
            goto err;
321
3.48k
    } else {
322
0
        BIGNUM *d = BN_new();
323
0
        if (d == NULL) {
324
0
            ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
325
0
            goto err;
326
0
        }
327
0
        if (rsa->d == NULL) {
328
0
            ERR_raise(ERR_LIB_RSA, RSA_R_MISSING_PRIVATE_KEY);
329
0
            BN_free(d);
330
0
            goto err;
331
0
        }
332
0
        BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
333
334
0
        if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
335
0
                rsa->_method_mod_n)) {
336
0
            BN_free(d);
337
0
            goto err;
338
0
        }
339
        /* We MUST free d before any further use of rsa->d */
340
0
        BN_free(d);
341
0
    }
342
343
3.48k
    if (blinding)
344
3.48k
        if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
345
0
            goto err;
346
347
3.48k
    if (padding == RSA_X931_PADDING) {
348
0
        if (!BN_sub(f, rsa->n, ret))
349
0
            goto err;
350
0
        if (BN_cmp(ret, f) > 0)
351
0
            res = f;
352
0
        else
353
0
            res = ret;
354
3.48k
    } else {
355
3.48k
        res = ret;
356
3.48k
    }
357
358
    /*
359
     * BN_bn2binpad puts in leading 0 bytes if the number is less than
360
     * the length of the modulus.
361
     */
362
3.48k
    r = BN_bn2binpad(res, to, num);
363
3.48k
err:
364
3.48k
    BN_CTX_end(ctx);
365
3.48k
    BN_CTX_free(ctx);
366
3.48k
    OPENSSL_clear_free(buf, num);
367
3.48k
    return r;
368
3.48k
}
369
370
static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
371
    unsigned char *to, RSA *rsa, int padding)
372
538
{
373
538
    BIGNUM *f, *ret;
374
538
    int j, num = 0, r = -1;
375
538
    unsigned char *buf = NULL;
376
538
    BN_CTX *ctx = NULL;
377
538
    int local_blinding = 0;
378
    /*
379
     * Used only if the blinding structure is shared. A non-NULL unblind
380
     * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
381
     * the unblinding factor outside the blinding structure.
382
     */
383
538
    BIGNUM *unblind = NULL;
384
538
    BN_BLINDING *blinding = NULL;
385
386
538
    if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
387
0
        goto err;
388
538
    BN_CTX_start(ctx);
389
538
    f = BN_CTX_get(ctx);
390
538
    ret = BN_CTX_get(ctx);
391
538
    num = BN_num_bytes(rsa->n);
392
538
    buf = OPENSSL_malloc(num);
393
538
    if (ret == NULL || buf == NULL) {
394
0
        ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
395
0
        goto err;
396
0
    }
397
398
    /*
399
     * This check was for equality but PGP does evil things and chops off the
400
     * top '0' bytes
401
     */
402
538
    if (flen > num) {
403
0
        ERR_raise(ERR_LIB_RSA, RSA_R_DATA_GREATER_THAN_MOD_LEN);
404
0
        goto err;
405
0
    }
406
407
    /* make data into a big number */
408
538
    if (BN_bin2bn(from, (int)flen, f) == NULL)
409
0
        goto err;
410
411
538
    if (BN_ucmp(f, rsa->n) >= 0) {
412
1
        ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
413
1
        goto err;
414
1
    }
415
416
537
    if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
417
537
        if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
418
537
                rsa->n, ctx))
419
0
            goto err;
420
421
537
    if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
422
537
        blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
423
537
        if (blinding == NULL) {
424
0
            ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
425
0
            goto err;
426
0
        }
427
537
    }
428
429
537
    if (blinding != NULL) {
430
537
        if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {
431
0
            ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
432
0
            goto err;
433
0
        }
434
537
        if (!rsa_blinding_convert(blinding, f, unblind, ctx))
435
0
            goto err;
436
537
    }
437
438
    /* do the decrypt */
439
537
    if ((rsa->flags & RSA_FLAG_EXT_PKEY) || (rsa->version == RSA_ASN1_VERSION_MULTI) || ((rsa->p != NULL) && (rsa->q != NULL) && (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
440
537
        if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
441
0
            goto err;
442
537
    } else {
443
0
        BIGNUM *d = BN_new();
444
0
        if (d == NULL) {
445
0
            ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
446
0
            goto err;
447
0
        }
448
0
        if (rsa->d == NULL) {
449
0
            ERR_raise(ERR_LIB_RSA, RSA_R_MISSING_PRIVATE_KEY);
450
0
            BN_free(d);
451
0
            goto err;
452
0
        }
453
0
        BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
454
0
        if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
455
0
                rsa->_method_mod_n)) {
456
0
            BN_free(d);
457
0
            goto err;
458
0
        }
459
        /* We MUST free d before any further use of rsa->d */
460
0
        BN_free(d);
461
0
    }
462
463
537
    if (blinding)
464
537
        if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
465
0
            goto err;
466
467
537
    j = BN_bn2binpad(ret, buf, num);
468
537
    if (j < 0)
469
0
        goto err;
470
471
537
    switch (padding) {
472
0
    case RSA_PKCS1_PADDING:
473
0
        r = RSA_padding_check_PKCS1_type_2(to, num, buf, j, num);
474
0
        break;
475
0
    case RSA_PKCS1_OAEP_PADDING:
476
0
        r = RSA_padding_check_PKCS1_OAEP(to, num, buf, j, num, NULL, 0);
477
0
        break;
478
537
    case RSA_NO_PADDING:
479
537
        memcpy(to, buf, (r = j));
480
537
        break;
481
0
    default:
482
0
        ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
483
0
        goto err;
484
537
    }
485
537
#ifndef FIPS_MODULE
486
    /*
487
     * This trick doesn't work in the FIPS provider because libcrypto manages
488
     * the error stack. Instead we opt not to put an error on the stack at all
489
     * in case of padding failure in the FIPS provider.
490
     */
491
537
    ERR_raise(ERR_LIB_RSA, RSA_R_PADDING_CHECK_FAILED);
492
537
    err_clear_last_constant_time(1 & ~constant_time_msb(r));
493
537
#endif
494
495
538
err:
496
538
    BN_CTX_end(ctx);
497
538
    BN_CTX_free(ctx);
498
538
    OPENSSL_clear_free(buf, num);
499
538
    return r;
500
537
}
501
502
/* signature verification */
503
static int rsa_ossl_public_decrypt(int flen, const unsigned char *from,
504
    unsigned char *to, RSA *rsa, int padding)
505
1.65k
{
506
1.65k
    BIGNUM *f, *ret;
507
1.65k
    int i, num = 0, r = -1;
508
1.65k
    unsigned char *buf = NULL;
509
1.65k
    BN_CTX *ctx = NULL;
510
511
1.65k
    if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
512
0
        ERR_raise(ERR_LIB_RSA, RSA_R_MODULUS_TOO_LARGE);
513
0
        return -1;
514
0
    }
515
516
1.65k
    if (BN_ucmp(rsa->n, rsa->e) <= 0) {
517
9
        ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
518
9
        return -1;
519
9
    }
520
521
    /* for large moduli, enforce exponent limit */
522
1.64k
    if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
523
0
        if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
524
0
            ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
525
0
            return -1;
526
0
        }
527
0
    }
528
529
1.64k
    if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
530
0
        goto err;
531
1.64k
    BN_CTX_start(ctx);
532
1.64k
    f = BN_CTX_get(ctx);
533
1.64k
    ret = BN_CTX_get(ctx);
534
1.64k
    num = BN_num_bytes(rsa->n);
535
1.64k
    buf = OPENSSL_malloc(num);
536
1.64k
    if (ret == NULL || buf == NULL) {
537
0
        ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
538
0
        goto err;
539
0
    }
540
541
    /*
542
     * This check was for equality but PGP does evil things and chops off the
543
     * top '0' bytes
544
     */
545
1.64k
    if (flen > num) {
546
17
        ERR_raise(ERR_LIB_RSA, RSA_R_DATA_GREATER_THAN_MOD_LEN);
547
17
        goto err;
548
17
    }
549
550
1.62k
    if (BN_bin2bn(from, flen, f) == NULL)
551
0
        goto err;
552
553
1.62k
    if (BN_ucmp(f, rsa->n) >= 0) {
554
24
        ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
555
24
        goto err;
556
24
    }
557
558
1.60k
    if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
559
1.60k
        if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
560
1.60k
                rsa->n, ctx))
561
158
            goto err;
562
563
1.44k
    if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
564
1.44k
            rsa->_method_mod_n))
565
0
        goto err;
566
567
1.44k
    if ((padding == RSA_X931_PADDING) && ((bn_get_words(ret)[0] & 0xf) != 12))
568
0
        if (!BN_sub(ret, rsa->n, ret))
569
0
            goto err;
570
571
1.44k
    i = BN_bn2binpad(ret, buf, num);
572
1.44k
    if (i < 0)
573
0
        goto err;
574
575
1.44k
    switch (padding) {
576
683
    case RSA_PKCS1_PADDING:
577
683
        r = RSA_padding_check_PKCS1_type_1(to, num, buf, i, num);
578
683
        break;
579
0
    case RSA_X931_PADDING:
580
0
        r = RSA_padding_check_X931(to, num, buf, i, num);
581
0
        break;
582
761
    case RSA_NO_PADDING:
583
761
        memcpy(to, buf, (r = i));
584
761
        break;
585
0
    default:
586
0
        ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
587
0
        goto err;
588
1.44k
    }
589
1.44k
    if (r < 0)
590
1.44k
        ERR_raise(ERR_LIB_RSA, RSA_R_PADDING_CHECK_FAILED);
591
592
1.64k
err:
593
1.64k
    BN_CTX_end(ctx);
594
1.64k
    BN_CTX_free(ctx);
595
1.64k
    OPENSSL_clear_free(buf, num);
596
1.64k
    return r;
597
1.44k
}
598
599
static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
600
16.3k
{
601
16.3k
    BIGNUM *r1, *m1, *vrfy;
602
16.3k
    int ret = 0, smooth = 0;
603
16.3k
#ifndef FIPS_MODULE
604
16.3k
    BIGNUM *r2, *m[RSA_MAX_PRIME_NUM - 2];
605
16.3k
    int i, ex_primes = 0;
606
16.3k
    RSA_PRIME_INFO *pinfo;
607
16.3k
#endif
608
609
16.3k
    BN_CTX_start(ctx);
610
611
16.3k
    r1 = BN_CTX_get(ctx);
612
16.3k
#ifndef FIPS_MODULE
613
16.3k
    r2 = BN_CTX_get(ctx);
614
16.3k
#endif
615
16.3k
    m1 = BN_CTX_get(ctx);
616
16.3k
    vrfy = BN_CTX_get(ctx);
617
16.3k
    if (vrfy == NULL)
618
0
        goto err;
619
620
16.3k
#ifndef FIPS_MODULE
621
16.3k
    if (rsa->version == RSA_ASN1_VERSION_MULTI
622
0
        && ((ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) <= 0
623
0
            || ex_primes > RSA_MAX_PRIME_NUM - 2))
624
0
        goto err;
625
16.3k
#endif
626
627
16.3k
    if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {
628
16.3k
        BIGNUM *factor = BN_new();
629
630
16.3k
        if (factor == NULL)
631
0
            goto err;
632
633
        /*
634
         * Make sure BN_mod_inverse in Montgomery initialization uses the
635
         * BN_FLG_CONSTTIME flag
636
         */
637
16.3k
        if (!(BN_with_flags(factor, rsa->p, BN_FLG_CONSTTIME),
638
16.3k
                BN_MONT_CTX_set_locked(&rsa->_method_mod_p, rsa->lock,
639
16.3k
                    factor, ctx))
640
16.3k
            || !(BN_with_flags(factor, rsa->q, BN_FLG_CONSTTIME),
641
16.3k
                BN_MONT_CTX_set_locked(&rsa->_method_mod_q, rsa->lock,
642
16.3k
                    factor, ctx))) {
643
0
            BN_free(factor);
644
0
            goto err;
645
0
        }
646
16.3k
#ifndef FIPS_MODULE
647
16.3k
        for (i = 0; i < ex_primes; i++) {
648
0
            pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
649
0
            BN_with_flags(factor, pinfo->r, BN_FLG_CONSTTIME);
650
0
            if (!BN_MONT_CTX_set_locked(&pinfo->m, rsa->lock, factor, ctx)) {
651
0
                BN_free(factor);
652
0
                goto err;
653
0
            }
654
0
        }
655
16.3k
#endif
656
        /*
657
         * We MUST free |factor| before any further use of the prime factors
658
         */
659
16.3k
        BN_free(factor);
660
661
16.3k
        smooth = (rsa->meth->bn_mod_exp == BN_mod_exp_mont)
662
16.3k
#ifndef FIPS_MODULE
663
16.3k
            && (ex_primes == 0)
664
16.3k
#endif
665
16.3k
            && (BN_num_bits(rsa->q) == BN_num_bits(rsa->p));
666
16.3k
    }
667
668
16.3k
    if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
669
16.3k
        if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
670
16.3k
                rsa->n, ctx))
671
0
            goto err;
672
673
16.3k
    if (smooth) {
674
        /*
675
         * Conversion from Montgomery domain, a.k.a. Montgomery reduction,
676
         * accepts values in [0-m*2^w) range. w is m's bit width rounded up
677
         * to limb width. So that at the very least if |I| is fully reduced,
678
         * i.e. less than p*q, we can count on from-to round to perform
679
         * below modulo operations on |I|. Unlike BN_mod it's constant time.
680
         */
681
16.3k
        if (/* m1 = I moq q */
682
16.3k
            !bn_from_mont_fixed_top(m1, I, rsa->_method_mod_q, ctx)
683
16.3k
            || !bn_to_mont_fixed_top(m1, m1, rsa->_method_mod_q, ctx)
684
            /* r1 = I mod p */
685
16.3k
            || !bn_from_mont_fixed_top(r1, I, rsa->_method_mod_p, ctx)
686
16.3k
            || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)
687
            /*
688
             * Use parallel exponentiations optimization if possible,
689
             * otherwise fallback to two sequential exponentiations:
690
             *    m1 = m1^dmq1 mod q
691
             *    r1 = r1^dmp1 mod p
692
             */
693
16.3k
            || !BN_mod_exp_mont_consttime_x2(m1, m1, rsa->dmq1, rsa->q,
694
16.3k
                rsa->_method_mod_q,
695
16.3k
                r1, r1, rsa->dmp1, rsa->p,
696
16.3k
                rsa->_method_mod_p,
697
16.3k
                ctx)
698
            /* r1 = (r1 - m1) mod p */
699
            /*
700
             * bn_mod_sub_fixed_top is not regular modular subtraction,
701
             * it can tolerate subtrahend to be larger than modulus, but
702
             * not bit-wise wider. This makes up for uncommon q>p case,
703
             * when |m1| can be larger than |rsa->p|.
704
             */
705
16.3k
            || !bn_mod_sub_fixed_top(r1, r1, m1, rsa->p)
706
707
            /* r1 = r1 * iqmp mod p */
708
16.3k
            || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)
709
16.3k
            || !bn_mul_mont_fixed_top(r1, r1, rsa->iqmp, rsa->_method_mod_p,
710
16.3k
                ctx)
711
            /* r0 = r1 * q + m1 */
712
16.3k
            || !bn_mul_fixed_top(r0, r1, rsa->q, ctx)
713
16.3k
            || !bn_mod_add_fixed_top(r0, r0, m1, rsa->n))
714
0
            goto err;
715
716
16.3k
        goto tail;
717
16.3k
    }
718
719
    /* compute I mod q */
720
0
    {
721
0
        BIGNUM *c = BN_new();
722
0
        if (c == NULL)
723
0
            goto err;
724
0
        BN_with_flags(c, I, BN_FLG_CONSTTIME);
725
726
0
        if (!BN_mod(r1, c, rsa->q, ctx)) {
727
0
            BN_free(c);
728
0
            goto err;
729
0
        }
730
731
0
        {
732
0
            BIGNUM *dmq1 = BN_new();
733
0
            if (dmq1 == NULL) {
734
0
                BN_free(c);
735
0
                goto err;
736
0
            }
737
0
            BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
738
739
            /* compute r1^dmq1 mod q */
740
0
            if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx,
741
0
                    rsa->_method_mod_q)) {
742
0
                BN_free(c);
743
0
                BN_free(dmq1);
744
0
                goto err;
745
0
            }
746
            /* We MUST free dmq1 before any further use of rsa->dmq1 */
747
0
            BN_free(dmq1);
748
0
        }
749
750
        /* compute I mod p */
751
0
        if (!BN_mod(r1, c, rsa->p, ctx)) {
752
0
            BN_free(c);
753
0
            goto err;
754
0
        }
755
        /* We MUST free c before any further use of I */
756
0
        BN_free(c);
757
0
    }
758
759
0
    {
760
0
        BIGNUM *dmp1 = BN_new();
761
0
        if (dmp1 == NULL)
762
0
            goto err;
763
0
        BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
764
765
        /* compute r1^dmp1 mod p */
766
0
        if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx,
767
0
                rsa->_method_mod_p)) {
768
0
            BN_free(dmp1);
769
0
            goto err;
770
0
        }
771
        /* We MUST free dmp1 before any further use of rsa->dmp1 */
772
0
        BN_free(dmp1);
773
0
    }
774
775
0
#ifndef FIPS_MODULE
776
0
    if (ex_primes > 0) {
777
0
        BIGNUM *di = BN_new(), *cc = BN_new();
778
779
0
        if (cc == NULL || di == NULL) {
780
0
            BN_free(cc);
781
0
            BN_free(di);
782
0
            goto err;
783
0
        }
784
785
0
        for (i = 0; i < ex_primes; i++) {
786
            /* prepare m_i */
787
0
            if ((m[i] = BN_CTX_get(ctx)) == NULL) {
788
0
                BN_free(cc);
789
0
                BN_free(di);
790
0
                goto err;
791
0
            }
792
793
0
            pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
794
795
            /* prepare c and d_i */
796
0
            BN_with_flags(cc, I, BN_FLG_CONSTTIME);
797
0
            BN_with_flags(di, pinfo->d, BN_FLG_CONSTTIME);
798
799
0
            if (!BN_mod(r1, cc, pinfo->r, ctx)) {
800
0
                BN_free(cc);
801
0
                BN_free(di);
802
0
                goto err;
803
0
            }
804
            /* compute r1 ^ d_i mod r_i */
805
0
            if (!rsa->meth->bn_mod_exp(m[i], r1, di, pinfo->r, ctx, pinfo->m)) {
806
0
                BN_free(cc);
807
0
                BN_free(di);
808
0
                goto err;
809
0
            }
810
0
        }
811
812
0
        BN_free(cc);
813
0
        BN_free(di);
814
0
    }
815
0
#endif
816
817
0
    if (!BN_sub(r0, r0, m1))
818
0
        goto err;
819
    /*
820
     * This will help stop the size of r0 increasing, which does affect the
821
     * multiply if it optimised for a power of 2 size
822
     */
823
0
    if (BN_is_negative(r0))
824
0
        if (!BN_add(r0, r0, rsa->p))
825
0
            goto err;
826
827
0
    if (!BN_mul(r1, r0, rsa->iqmp, ctx))
828
0
        goto err;
829
830
0
    {
831
0
        BIGNUM *pr1 = BN_new();
832
0
        if (pr1 == NULL)
833
0
            goto err;
834
0
        BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
835
836
0
        if (!BN_mod(r0, pr1, rsa->p, ctx)) {
837
0
            BN_free(pr1);
838
0
            goto err;
839
0
        }
840
        /* We MUST free pr1 before any further use of r1 */
841
0
        BN_free(pr1);
842
0
    }
843
844
    /*
845
     * If p < q it is occasionally possible for the correction of adding 'p'
846
     * if r0 is negative above to leave the result still negative. This can
847
     * break the private key operations: the following second correction
848
     * should *always* correct this rare occurrence. This will *never* happen
849
     * with OpenSSL generated keys because they ensure p > q [steve]
850
     */
851
0
    if (BN_is_negative(r0))
852
0
        if (!BN_add(r0, r0, rsa->p))
853
0
            goto err;
854
0
    if (!BN_mul(r1, r0, rsa->q, ctx))
855
0
        goto err;
856
0
    if (!BN_add(r0, r1, m1))
857
0
        goto err;
858
859
0
#ifndef FIPS_MODULE
860
    /* add m_i to m in multi-prime case */
861
0
    if (ex_primes > 0) {
862
0
        BIGNUM *pr2 = BN_new();
863
864
0
        if (pr2 == NULL)
865
0
            goto err;
866
867
0
        for (i = 0; i < ex_primes; i++) {
868
0
            pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
869
0
            if (!BN_sub(r1, m[i], r0)) {
870
0
                BN_free(pr2);
871
0
                goto err;
872
0
            }
873
874
0
            if (!BN_mul(r2, r1, pinfo->t, ctx)) {
875
0
                BN_free(pr2);
876
0
                goto err;
877
0
            }
878
879
0
            BN_with_flags(pr2, r2, BN_FLG_CONSTTIME);
880
881
0
            if (!BN_mod(r1, pr2, pinfo->r, ctx)) {
882
0
                BN_free(pr2);
883
0
                goto err;
884
0
            }
885
886
0
            if (BN_is_negative(r1))
887
0
                if (!BN_add(r1, r1, pinfo->r)) {
888
0
                    BN_free(pr2);
889
0
                    goto err;
890
0
                }
891
0
            if (!BN_mul(r1, r1, pinfo->pp, ctx)) {
892
0
                BN_free(pr2);
893
0
                goto err;
894
0
            }
895
0
            if (!BN_add(r0, r0, r1)) {
896
0
                BN_free(pr2);
897
0
                goto err;
898
0
            }
899
0
        }
900
0
        BN_free(pr2);
901
0
    }
902
0
#endif
903
904
16.3k
tail:
905
16.3k
    if (rsa->e && rsa->n) {
906
16.3k
        if (rsa->meth->bn_mod_exp == BN_mod_exp_mont) {
907
16.3k
            if (!BN_mod_exp_mont(vrfy, r0, rsa->e, rsa->n, ctx,
908
16.3k
                    rsa->_method_mod_n))
909
0
                goto err;
910
16.3k
        } else {
911
0
            bn_correct_top(r0);
912
0
            if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,
913
0
                    rsa->_method_mod_n))
914
0
                goto err;
915
0
        }
916
        /*
917
         * If 'I' was greater than (or equal to) rsa->n, the operation will
918
         * be equivalent to using 'I mod n'. However, the result of the
919
         * verify will *always* be less than 'n' so we don't check for
920
         * absolute equality, just congruency.
921
         */
922
16.3k
        if (!BN_sub(vrfy, vrfy, I))
923
0
            goto err;
924
16.3k
        if (BN_is_zero(vrfy)) {
925
16.3k
            bn_correct_top(r0);
926
16.3k
            ret = 1;
927
16.3k
            goto err; /* not actually error */
928
16.3k
        }
929
0
        if (!BN_mod(vrfy, vrfy, rsa->n, ctx))
930
0
            goto err;
931
0
        if (BN_is_negative(vrfy))
932
0
            if (!BN_add(vrfy, vrfy, rsa->n))
933
0
                goto err;
934
0
        if (!BN_is_zero(vrfy)) {
935
            /*
936
             * 'I' and 'vrfy' aren't congruent mod n. Don't leak
937
             * miscalculated CRT output, just do a raw (slower) mod_exp and
938
             * return that instead.
939
             */
940
941
0
            BIGNUM *d = BN_new();
942
0
            if (d == NULL)
943
0
                goto err;
944
0
            BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
945
946
0
            if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx,
947
0
                    rsa->_method_mod_n)) {
948
0
                BN_free(d);
949
0
                goto err;
950
0
            }
951
            /* We MUST free d before any further use of rsa->d */
952
0
            BN_free(d);
953
0
        }
954
0
    }
955
    /*
956
     * It's unfortunate that we have to bn_correct_top(r0). What hopefully
957
     * saves the day is that correction is highly unlike, and private key
958
     * operations are customarily performed on blinded message. Which means
959
     * that attacker won't observe correlation with chosen plaintext.
960
     * Secondly, remaining code would still handle it in same computational
961
     * time and even conceal memory access pattern around corrected top.
962
     */
963
0
    bn_correct_top(r0);
964
0
    ret = 1;
965
16.3k
err:
966
16.3k
    BN_CTX_end(ctx);
967
16.3k
    return ret;
968
0
}
969
970
static int rsa_ossl_init(RSA *rsa)
971
592k
{
972
592k
    rsa->flags |= RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE;
973
592k
    return 1;
974
592k
}
975
976
static int rsa_ossl_finish(RSA *rsa)
977
592k
{
978
592k
#ifndef FIPS_MODULE
979
592k
    int i;
980
592k
    RSA_PRIME_INFO *pinfo;
981
982
900k
    for (i = 0; i < sk_RSA_PRIME_INFO_num(rsa->prime_infos); i++) {
983
307k
        pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
984
307k
        BN_MONT_CTX_free(pinfo->m);
985
307k
    }
986
592k
#endif
987
988
592k
    BN_MONT_CTX_free(rsa->_method_mod_n);
989
592k
    BN_MONT_CTX_free(rsa->_method_mod_p);
990
592k
    BN_MONT_CTX_free(rsa->_method_mod_q);
991
592k
    return 1;
992
592k
}