Coverage Report

Created: 2025-12-31 06:58

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
596k
{
61
596k
    return default_RSA_meth;
62
596k
}
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
5.52k
{
77
5.52k
    BIGNUM *f, *ret;
78
5.52k
    int i, num = 0, r = -1;
79
5.52k
    unsigned char *buf = NULL;
80
5.52k
    BN_CTX *ctx = NULL;
81
82
5.52k
    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
5.52k
    if (BN_ucmp(rsa->n, rsa->e) <= 0) {
88
8
        ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
89
8
        return -1;
90
8
    }
91
92
    /* for large moduli, enforce exponent limit */
93
5.51k
    if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
94
21
        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
21
    }
99
100
5.51k
    if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
101
0
        goto err;
102
5.51k
    BN_CTX_start(ctx);
103
5.51k
    f = BN_CTX_get(ctx);
104
5.51k
    ret = BN_CTX_get(ctx);
105
5.51k
    num = BN_num_bytes(rsa->n);
106
5.51k
    buf = OPENSSL_malloc(num);
107
5.51k
    if (ret == NULL || buf == NULL) {
108
0
        ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
109
0
        goto err;
110
0
    }
111
112
5.51k
    switch (padding) {
113
5.51k
    case RSA_PKCS1_PADDING:
114
5.51k
        i = ossl_rsa_padding_add_PKCS1_type_2_ex(rsa->libctx, buf, num,
115
5.51k
            from, flen);
116
5.51k
        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
5.51k
    }
129
5.51k
    if (i <= 0)
130
46
        goto err;
131
132
5.46k
    if (BN_bin2bn(buf, num, f) == NULL)
133
0
        goto err;
134
135
5.46k
    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
5.46k
    if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
142
5.46k
        if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
143
5.46k
                rsa->n, ctx))
144
666
            goto err;
145
146
4.80k
    if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
147
4.80k
            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
4.80k
    r = BN_bn2binpad(ret, to, num);
155
5.51k
err:
156
5.51k
    BN_CTX_end(ctx);
157
5.51k
    BN_CTX_free(ctx);
158
5.51k
    OPENSSL_clear_free(buf, num);
159
5.51k
    return r;
160
4.80k
}
161
162
static BN_BLINDING *rsa_get_blinding(RSA *rsa, int *local, BN_CTX *ctx)
163
1.28k
{
164
1.28k
    BN_BLINDING *ret;
165
166
1.28k
    if (!CRYPTO_THREAD_write_lock(rsa->lock))
167
0
        return NULL;
168
169
1.28k
    if (rsa->blinding == NULL) {
170
1.28k
        rsa->blinding = RSA_setup_blinding(rsa, ctx);
171
1.28k
    }
172
173
1.28k
    ret = rsa->blinding;
174
1.28k
    if (ret == NULL)
175
0
        goto err;
176
177
1.28k
    if (BN_BLINDING_is_current_thread(ret)) {
178
        /* rsa->blinding is ours! */
179
180
1.28k
        *local = 1;
181
1.28k
    } 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.28k
err:
198
1.28k
    CRYPTO_THREAD_unlock(rsa->lock);
199
1.28k
    return ret;
200
1.28k
}
201
202
static int rsa_blinding_convert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind,
203
    BN_CTX *ctx)
204
10.7k
{
205
10.7k
    if (unblind == NULL) {
206
        /*
207
         * Local blinding: store the unblinding factor in BN_BLINDING.
208
         */
209
10.7k
        return BN_BLINDING_convert_ex(f, NULL, b, ctx);
210
10.7k
    } 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.7k
}
225
226
static int rsa_blinding_invert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind,
227
    BN_CTX *ctx)
228
17.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
17.3k
    BN_set_flags(f, BN_FLG_CONSTTIME);
238
17.3k
    return BN_BLINDING_invert_ex(f, unblind, b, ctx);
239
17.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.58k
{
245
3.58k
    BIGNUM *f, *ret, *res;
246
3.58k
    int i, num = 0, r = -1;
247
3.58k
    unsigned char *buf = NULL;
248
3.58k
    BN_CTX *ctx = NULL;
249
3.58k
    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.58k
    BIGNUM *unblind = NULL;
256
3.58k
    BN_BLINDING *blinding = NULL;
257
258
3.58k
    if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
259
0
        goto err;
260
3.58k
    BN_CTX_start(ctx);
261
3.58k
    f = BN_CTX_get(ctx);
262
3.58k
    ret = BN_CTX_get(ctx);
263
3.58k
    num = BN_num_bytes(rsa->n);
264
3.58k
    buf = OPENSSL_malloc(num);
265
3.58k
    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.58k
    switch (padding) {
271
2.91k
    case RSA_PKCS1_PADDING:
272
2.91k
        i = RSA_padding_add_PKCS1_type_1(buf, num, from, flen);
273
2.91k
        break;
274
0
    case RSA_X931_PADDING:
275
0
        i = RSA_padding_add_X931(buf, num, from, flen);
276
0
        break;
277
665
    case RSA_NO_PADDING:
278
665
        i = RSA_padding_add_none(buf, num, from, flen);
279
665
        break;
280
0
    default:
281
0
        ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
282
0
        goto err;
283
3.58k
    }
284
3.58k
    if (i <= 0)
285
0
        goto err;
286
287
3.58k
    if (BN_bin2bn(buf, num, f) == NULL)
288
0
        goto err;
289
290
3.58k
    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.58k
    if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
297
3.58k
        if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
298
3.58k
                rsa->n, ctx))
299
0
            goto err;
300
301
3.58k
    if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
302
3.58k
        blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
303
3.58k
        if (blinding == NULL) {
304
0
            ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
305
0
            goto err;
306
0
        }
307
3.58k
    }
308
309
3.58k
    if (blinding != NULL) {
310
3.58k
        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.58k
        if (!rsa_blinding_convert(blinding, f, unblind, ctx))
315
0
            goto err;
316
3.58k
    }
317
318
3.58k
    if ((rsa->flags & RSA_FLAG_EXT_PKEY) || (rsa->version == RSA_ASN1_VERSION_MULTI) || ((rsa->p != NULL) && (rsa->q != NULL) && (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
319
3.58k
        if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
320
0
            goto err;
321
3.58k
    } 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.58k
    if (blinding)
344
3.58k
        if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
345
0
            goto err;
346
347
3.58k
    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.58k
    } else {
355
3.58k
        res = ret;
356
3.58k
    }
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.58k
    r = BN_bn2binpad(res, to, num);
363
3.58k
err:
364
3.58k
    BN_CTX_end(ctx);
365
3.58k
    BN_CTX_free(ctx);
366
3.58k
    OPENSSL_clear_free(buf, num);
367
3.58k
    return r;
368
3.58k
}
369
370
static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
371
    unsigned char *to, RSA *rsa, int padding)
372
603
{
373
603
    BIGNUM *f, *ret;
374
603
    int j, num = 0, r = -1;
375
603
    unsigned char *buf = NULL;
376
603
    BN_CTX *ctx = NULL;
377
603
    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
603
    BIGNUM *unblind = NULL;
384
603
    BN_BLINDING *blinding = NULL;
385
386
603
    if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
387
0
        goto err;
388
603
    BN_CTX_start(ctx);
389
603
    f = BN_CTX_get(ctx);
390
603
    ret = BN_CTX_get(ctx);
391
603
    num = BN_num_bytes(rsa->n);
392
603
    buf = OPENSSL_malloc(num);
393
603
    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
603
    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
603
    if (BN_bin2bn(from, (int)flen, f) == NULL)
409
0
        goto err;
410
411
603
    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
602
    if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
417
602
        if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
418
602
                rsa->n, ctx))
419
0
            goto err;
420
421
602
    if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
422
602
        blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
423
602
        if (blinding == NULL) {
424
0
            ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
425
0
            goto err;
426
0
        }
427
602
    }
428
429
602
    if (blinding != NULL) {
430
602
        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
602
        if (!rsa_blinding_convert(blinding, f, unblind, ctx))
435
0
            goto err;
436
602
    }
437
438
    /* do the decrypt */
439
602
    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
602
        if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
441
0
            goto err;
442
602
    } 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
602
    if (blinding)
464
602
        if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
465
0
            goto err;
466
467
602
    j = BN_bn2binpad(ret, buf, num);
468
602
    if (j < 0)
469
0
        goto err;
470
471
602
    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
602
    case RSA_NO_PADDING:
479
602
        memcpy(to, buf, (r = j));
480
602
        break;
481
0
    default:
482
0
        ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
483
0
        goto err;
484
602
    }
485
602
#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
602
    ERR_raise(ERR_LIB_RSA, RSA_R_PADDING_CHECK_FAILED);
492
602
    err_clear_last_constant_time(1 & ~constant_time_msb(r));
493
602
#endif
494
495
603
err:
496
603
    BN_CTX_end(ctx);
497
603
    BN_CTX_free(ctx);
498
603
    OPENSSL_clear_free(buf, num);
499
603
    return r;
500
602
}
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.78k
{
506
1.78k
    BIGNUM *f, *ret;
507
1.78k
    int i, num = 0, r = -1;
508
1.78k
    unsigned char *buf = NULL;
509
1.78k
    BN_CTX *ctx = NULL;
510
511
1.78k
    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.78k
    if (BN_ucmp(rsa->n, rsa->e) <= 0) {
517
4
        ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
518
4
        return -1;
519
4
    }
520
521
    /* for large moduli, enforce exponent limit */
522
1.78k
    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.78k
    if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
530
0
        goto err;
531
1.78k
    BN_CTX_start(ctx);
532
1.78k
    f = BN_CTX_get(ctx);
533
1.78k
    ret = BN_CTX_get(ctx);
534
1.78k
    num = BN_num_bytes(rsa->n);
535
1.78k
    buf = OPENSSL_malloc(num);
536
1.78k
    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.78k
    if (flen > num) {
546
24
        ERR_raise(ERR_LIB_RSA, RSA_R_DATA_GREATER_THAN_MOD_LEN);
547
24
        goto err;
548
24
    }
549
550
1.75k
    if (BN_bin2bn(from, flen, f) == NULL)
551
0
        goto err;
552
553
1.75k
    if (BN_ucmp(f, rsa->n) >= 0) {
554
25
        ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
555
25
        goto err;
556
25
    }
557
558
1.73k
    if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
559
1.73k
        if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
560
1.73k
                rsa->n, ctx))
561
176
            goto err;
562
563
1.55k
    if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
564
1.55k
            rsa->_method_mod_n))
565
0
        goto err;
566
567
1.55k
    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.55k
    i = BN_bn2binpad(ret, buf, num);
572
1.55k
    if (i < 0)
573
0
        goto err;
574
575
1.55k
    switch (padding) {
576
715
    case RSA_PKCS1_PADDING:
577
715
        r = RSA_padding_check_PKCS1_type_1(to, num, buf, i, num);
578
715
        break;
579
0
    case RSA_X931_PADDING:
580
0
        r = RSA_padding_check_X931(to, num, buf, i, num);
581
0
        break;
582
842
    case RSA_NO_PADDING:
583
842
        memcpy(to, buf, (r = i));
584
842
        break;
585
0
    default:
586
0
        ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
587
0
        goto err;
588
1.55k
    }
589
1.55k
    if (r < 0)
590
1.55k
        ERR_raise(ERR_LIB_RSA, RSA_R_PADDING_CHECK_FAILED);
591
592
1.78k
err:
593
1.78k
    BN_CTX_end(ctx);
594
1.78k
    BN_CTX_free(ctx);
595
1.78k
    OPENSSL_clear_free(buf, num);
596
1.78k
    return r;
597
1.55k
}
598
599
static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
600
17.3k
{
601
17.3k
    BIGNUM *r1, *m1, *vrfy;
602
17.3k
    int ret = 0, smooth = 0;
603
17.3k
#ifndef FIPS_MODULE
604
17.3k
    BIGNUM *r2, *m[RSA_MAX_PRIME_NUM - 2];
605
17.3k
    int i, ex_primes = 0;
606
17.3k
    RSA_PRIME_INFO *pinfo;
607
17.3k
#endif
608
609
17.3k
    BN_CTX_start(ctx);
610
611
17.3k
    r1 = BN_CTX_get(ctx);
612
17.3k
#ifndef FIPS_MODULE
613
17.3k
    r2 = BN_CTX_get(ctx);
614
17.3k
#endif
615
17.3k
    m1 = BN_CTX_get(ctx);
616
17.3k
    vrfy = BN_CTX_get(ctx);
617
17.3k
    if (vrfy == NULL)
618
0
        goto err;
619
620
17.3k
#ifndef FIPS_MODULE
621
17.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
17.3k
#endif
626
627
17.3k
    if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {
628
17.3k
        BIGNUM *factor = BN_new();
629
630
17.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
17.3k
        if (!(BN_with_flags(factor, rsa->p, BN_FLG_CONSTTIME),
638
17.3k
                BN_MONT_CTX_set_locked(&rsa->_method_mod_p, rsa->lock,
639
17.3k
                    factor, ctx))
640
17.3k
            || !(BN_with_flags(factor, rsa->q, BN_FLG_CONSTTIME),
641
17.3k
                BN_MONT_CTX_set_locked(&rsa->_method_mod_q, rsa->lock,
642
17.3k
                    factor, ctx))) {
643
0
            BN_free(factor);
644
0
            goto err;
645
0
        }
646
17.3k
#ifndef FIPS_MODULE
647
17.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
17.3k
#endif
656
        /*
657
         * We MUST free |factor| before any further use of the prime factors
658
         */
659
17.3k
        BN_free(factor);
660
661
17.3k
        smooth = (rsa->meth->bn_mod_exp == BN_mod_exp_mont)
662
17.3k
#ifndef FIPS_MODULE
663
17.3k
            && (ex_primes == 0)
664
17.3k
#endif
665
17.3k
            && (BN_num_bits(rsa->q) == BN_num_bits(rsa->p));
666
17.3k
    }
667
668
17.3k
    if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
669
17.3k
        if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
670
17.3k
                rsa->n, ctx))
671
0
            goto err;
672
673
17.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
17.3k
        if (/* m1 = I moq q */
682
17.3k
            !bn_from_mont_fixed_top(m1, I, rsa->_method_mod_q, ctx)
683
17.3k
            || !bn_to_mont_fixed_top(m1, m1, rsa->_method_mod_q, ctx)
684
            /* r1 = I mod p */
685
17.3k
            || !bn_from_mont_fixed_top(r1, I, rsa->_method_mod_p, ctx)
686
17.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
17.3k
            || !BN_mod_exp_mont_consttime_x2(m1, m1, rsa->dmq1, rsa->q,
694
17.3k
                rsa->_method_mod_q,
695
17.3k
                r1, r1, rsa->dmp1, rsa->p,
696
17.3k
                rsa->_method_mod_p,
697
17.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
17.3k
            || !bn_mod_sub_fixed_top(r1, r1, m1, rsa->p)
706
707
            /* r1 = r1 * iqmp mod p */
708
17.3k
            || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)
709
17.3k
            || !bn_mul_mont_fixed_top(r1, r1, rsa->iqmp, rsa->_method_mod_p,
710
17.3k
                ctx)
711
            /* r0 = r1 * q + m1 */
712
17.3k
            || !bn_mul_fixed_top(r0, r1, rsa->q, ctx)
713
17.3k
            || !bn_mod_add_fixed_top(r0, r0, m1, rsa->n))
714
0
            goto err;
715
716
17.3k
        goto tail;
717
17.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
17.3k
tail:
905
17.3k
    if (rsa->e && rsa->n) {
906
17.3k
        if (rsa->meth->bn_mod_exp == BN_mod_exp_mont) {
907
17.3k
            if (!BN_mod_exp_mont(vrfy, r0, rsa->e, rsa->n, ctx,
908
17.3k
                    rsa->_method_mod_n))
909
0
                goto err;
910
17.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
17.3k
        if (!BN_sub(vrfy, vrfy, I))
923
0
            goto err;
924
17.3k
        if (BN_is_zero(vrfy)) {
925
17.3k
            bn_correct_top(r0);
926
17.3k
            ret = 1;
927
17.3k
            goto err; /* not actually error */
928
17.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
17.3k
err:
966
17.3k
    BN_CTX_end(ctx);
967
17.3k
    return ret;
968
0
}
969
970
static int rsa_ossl_init(RSA *rsa)
971
596k
{
972
596k
    rsa->flags |= RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE;
973
596k
    return 1;
974
596k
}
975
976
static int rsa_ossl_finish(RSA *rsa)
977
596k
{
978
596k
#ifndef FIPS_MODULE
979
596k
    int i;
980
596k
    RSA_PRIME_INFO *pinfo;
981
982
816k
    for (i = 0; i < sk_RSA_PRIME_INFO_num(rsa->prime_infos); i++) {
983
219k
        pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
984
219k
        BN_MONT_CTX_free(pinfo->m);
985
219k
    }
986
596k
#endif
987
988
596k
    BN_MONT_CTX_free(rsa->_method_mod_n);
989
596k
    BN_MONT_CTX_free(rsa->_method_mod_p);
990
596k
    BN_MONT_CTX_free(rsa->_method_mod_q);
991
596k
    return 1;
992
596k
}