Coverage Report

Created: 2025-08-26 06:04

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