Coverage Report

Created: 2023-06-08 06:40

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