Coverage Report

Created: 2018-08-29 13:53

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