Coverage Report

Created: 2026-01-09 06:53

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