/home/XXXX-2/Sources/nssgit/nss/lib/freebl/rsa.c
Line
Count
Source (jump to first uncovered line)
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
3
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
/*
6
 * RSA key generation, public key op, private key op.
7
 */
8
#ifdef FREEBL_NO_DEPEND
9
#include "stubs.h"
10
#endif
11
12
#include "secerr.h"
13
14
#include "prclist.h"
15
#include "nssilock.h"
16
#include "prinit.h"
17
#include "blapi.h"
18
#include "mpi.h"
19
#include "mpprime.h"
20
#include "mplogic.h"
21
#include "secmpi.h"
22
#include "secitem.h"
23
#include "blapii.h"
24
25
/* The minimal required randomness is 64 bits */
26
/* EXP_BLINDING_RANDOMNESS_LEN is the length of the randomness in mp_digits */
27
/* for 32 bits platforts it is 2 mp_digits (= 2 * 32 bits), for 64 bits it is equal to 128 bits */
28
8.68k
#define EXP_BLINDING_RANDOMNESS_LEN ((128 + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT)
29
4.34k
#define EXP_BLINDING_RANDOMNESS_LEN_BYTES (EXP_BLINDING_RANDOMNESS_LEN * sizeof(mp_digit))
30
31
/*
32
** Number of times to attempt to generate a prime (p or q) from a random
33
** seed (the seed changes for each iteration).
34
*/
35
1.93k
#define MAX_PRIME_GEN_ATTEMPTS 10
36
/*
37
** Number of times to attempt to generate a key.  The primes p and q change
38
** for each attempt.
39
*/
40
#define MAX_KEY_GEN_ATTEMPTS 10
41
42
/* Blinding Parameters max cache size  */
43
95.1k
#define RSA_BLINDING_PARAMS_MAX_CACHE_SIZE 20
44
45
/* exponent should not be greater than modulus */
46
#define BAD_RSA_KEY_SIZE(modLen, expLen)                           \
47
7.48k
    ((expLen) > (modLen) || (modLen) > RSA_MAX_MODULUS_BITS / 8 || \
48
7.48k
     (expLen) > RSA_MAX_EXPONENT_BITS / 8)
49
50
struct blindingParamsStr;
51
typedef struct blindingParamsStr blindingParams;
52
53
struct blindingParamsStr {
54
    blindingParams *next;
55
    mp_int f, g; /* blinding parameter                 */
56
    int counter; /* number of remaining uses of (f, g) */
57
};
58
59
/*
60
** RSABlindingParamsStr
61
**
62
** For discussion of Paul Kocher's timing attack against an RSA private key
63
** operation, see http://www.cryptography.com/timingattack/paper.html.  The
64
** countermeasure to this attack, known as blinding, is also discussed in
65
** the Handbook of Applied Cryptography, 11.118-11.119.
66
*/
67
struct RSABlindingParamsStr {
68
    /* Blinding-specific parameters */
69
    PRCList link;              /* link to list of structs            */
70
    SECItem modulus;           /* list element "key"                 */
71
    blindingParams *free, *bp; /* Blinding parameters queue          */
72
    blindingParams array[RSA_BLINDING_PARAMS_MAX_CACHE_SIZE];
73
};
74
typedef struct RSABlindingParamsStr RSABlindingParams;
75
76
/*
77
** RSABlindingParamsListStr
78
**
79
** List of key-specific blinding params.  The arena holds the volatile pool
80
** of memory for each entry and the list itself.  The lock is for list
81
** operations, in this case insertions and iterations, as well as control
82
** of the counter for each set of blinding parameters.
83
*/
84
struct RSABlindingParamsListStr {
85
    PZLock *lock;    /* Lock for the list   */
86
    PRCondVar *cVar; /* Condidtion Variable */
87
    int waitCount;   /* Number of threads waiting on cVar */
88
    PRCList head;    /* Pointer to the list */
89
};
90
91
/*
92
** The master blinding params list.
93
*/
94
static struct RSABlindingParamsListStr blindingParamsList = { 0 };
95
96
/* Number of times to reuse (f, g).  Suggested by Paul Kocher */
97
2.17k
#define RSA_BLINDING_PARAMS_MAX_REUSE 50
98
99
/* Global, allows optional use of blinding.  On by default. */
100
/* Cannot be changed at the moment, due to thread-safety issues. */
101
static PRBool nssRSAUseBlinding = PR_TRUE;
102
103
static SECStatus
104
rsa_build_from_primes(const mp_int *p, const mp_int *q,
105
                      mp_int *e, PRBool needPublicExponent,
106
                      mp_int *d, PRBool needPrivateExponent,
107
                      RSAPrivateKey *key, unsigned int keySizeInBits)
108
9.57k
{
109
9.57k
    mp_int n, phi;
110
9.57k
    mp_int psub1, qsub1, tmp;
111
9.57k
    mp_err err = MP_OKAY;
112
9.57k
    SECStatus rv = SECSuccess;
113
9.57k
    MP_DIGITS(&n) = 0;
114
9.57k
    MP_DIGITS(&phi) = 0;
115
9.57k
    MP_DIGITS(&psub1) = 0;
116
9.57k
    MP_DIGITS(&qsub1) = 0;
117
9.57k
    MP_DIGITS(&tmp) = 0;
118
9.57k
    CHECK_MPI_OK(mp_init(&n));
119
9.57k
    CHECK_MPI_OK(mp_init(&phi));
120
9.57k
    CHECK_MPI_OK(mp_init(&psub1));
121
9.57k
    CHECK_MPI_OK(mp_init(&qsub1));
122
9.57k
    CHECK_MPI_OK(mp_init(&tmp));
123
    /* p and q must be distinct. */
124
9.57k
    if (mp_cmp(p, q) == 0) {
125
150
        PORT_SetError(SEC_ERROR_NEED_RANDOM);
126
150
        rv = SECFailure;
127
150
        goto cleanup;
128
150
    }
129
    /* 1.  Compute n = p*q */
130
9.42k
    CHECK_MPI_OK(mp_mul(p, q, &n));
131
    /*     verify that the modulus has the desired number of bits */
132
9.42k
    if ((unsigned)mpl_significant_bits(&n) != keySizeInBits) {
133
890
        PORT_SetError(SEC_ERROR_NEED_RANDOM);
134
890
        rv = SECFailure;
135
890
        goto cleanup;
136
890
    }
137
138
    /* at least one exponent must be given */
139
8.53k
    PORT_Assert(!(needPublicExponent && needPrivateExponent));
140
141
    /* 2.  Compute phi = (p-1)*(q-1) */
142
8.53k
    CHECK_MPI_OK(mp_sub_d(p, 1, &psub1));
143
8.53k
    CHECK_MPI_OK(mp_sub_d(q, 1, &qsub1));
144
8.53k
    if (needPublicExponent || needPrivateExponent) {
145
8.36k
        CHECK_MPI_OK(mp_lcm(&psub1, &qsub1, &phi));
146
        /* 3.  Compute d = e**-1 mod(phi) */
147
        /*     or      e = d**-1 mod(phi) as necessary */
148
8.36k
        if (needPublicExponent) {
149
7.28k
            err = mp_invmod(d, &phi, e);
150
7.28k
        } else {
151
1.07k
            err = mp_invmod(e, &phi, d);
152
1.07k
        }
153
8.36k
    } else {
154
178
        err = MP_OKAY;
155
178
    }
156
    /*     Verify that phi(n) and e have no common divisors */
157
8.53k
    if (err != MP_OKAY) {
158
2.18k
        if (err == MP_UNDEF) {
159
1.97k
            PORT_SetError(SEC_ERROR_NEED_RANDOM);
160
1.97k
            err = MP_OKAY; /* to keep PORT_SetError from being called again */
161
1.97k
            rv = SECFailure;
162
1.97k
        }
163
2.18k
        goto cleanup;
164
2.18k
    }
165
166
    /* 4.  Compute exponent1 = d mod (p-1) */
167
6.35k
    CHECK_MPI_OK(mp_mod(d, &psub1, &tmp));
168
6.35k
    MPINT_TO_SECITEM(&tmp, &key->exponent1, key->arena);
169
    /* 5.  Compute exponent2 = d mod (q-1) */
170
6.35k
    CHECK_MPI_OK(mp_mod(d, &qsub1, &tmp));
171
6.33k
    MPINT_TO_SECITEM(&tmp, &key->exponent2, key->arena);
172
    /* 6.  Compute coefficient = q**-1 mod p */
173
6.33k
    CHECK_MPI_OK(mp_invmod(q, p, &tmp));
174
5.31k
    MPINT_TO_SECITEM(&tmp, &key->coefficient, key->arena);
175
176
    /* copy our calculated results, overwrite what is there */
177
5.31k
    key->modulus.data = NULL;
178
5.31k
    MPINT_TO_SECITEM(&n, &key->modulus, key->arena);
179
5.31k
    key->privateExponent.data = NULL;
180
5.31k
    MPINT_TO_SECITEM(d, &key->privateExponent, key->arena);
181
5.31k
    key->publicExponent.data = NULL;
182
5.31k
    MPINT_TO_SECITEM(e, &key->publicExponent, key->arena);
183
5.31k
    key->prime1.data = NULL;
184
5.31k
    MPINT_TO_SECITEM(p, &key->prime1, key->arena);
185
5.31k
    key->prime2.data = NULL;
186
5.31k
    MPINT_TO_SECITEM(q, &key->prime2, key->arena);
187
9.57k
cleanup:
188
9.57k
    mp_clear(&n);
189
9.57k
    mp_clear(&phi);
190
9.57k
    mp_clear(&psub1);
191
9.57k
    mp_clear(&qsub1);
192
9.57k
    mp_clear(&tmp);
193
9.57k
    if (err) {
194
1.24k
        MP_TO_SEC_ERROR(err);
195
1.24k
        rv = SECFailure;
196
1.24k
    }
197
9.57k
    return rv;
198
9.57k
}
199
200
SECStatus
201
generate_prime(mp_int *prime, int primeLen)
202
1.93k
{
203
1.93k
    mp_err err = MP_OKAY;
204
1.93k
    SECStatus rv = SECSuccess;
205
1.93k
    int piter;
206
1.93k
    unsigned char *pb = NULL;
207
1.93k
    pb = PORT_Alloc(primeLen);
208
1.93k
    if (!pb) {
209
0
        PORT_SetError(SEC_ERROR_NO_MEMORY);
210
0
        goto cleanup;
211
0
    }
212
1.93k
    for (piter = 0; piter < MAX_PRIME_GEN_ATTEMPTS; piter++) {
213
1.93k
        CHECK_SEC_OK(RNG_GenerateGlobalRandomBytes(pb, primeLen));
214
1.93k
        pb[0] |= 0xC0;            /* set two high-order bits */
215
1.93k
        pb[primeLen - 1] |= 0x01; /* set low-order bit       */
216
1.93k
        CHECK_MPI_OK(mp_read_unsigned_octets(prime, pb, primeLen));
217
1.93k
        err = mpp_make_prime_secure(prime, primeLen * 8, PR_FALSE);
218
1.93k
        if (err != MP_NO)
219
1.93k
            goto cleanup;
220
        /* keep going while err == MP_NO */
221
1.93k
    }
222
1.93k
cleanup:
223
1.93k
    if (pb)
224
1.93k
        PORT_ZFree(pb, primeLen);
225
1.93k
    if (err) {
226
0
        MP_TO_SEC_ERROR(err);
227
0
        rv = SECFailure;
228
0
    }
229
1.93k
    return rv;
230
1.93k
}
231
232
/*
233
 *  make sure the key components meet fips186 requirements.
234
 */
235
static PRBool
236
rsa_fips186_verify(mp_int *p, mp_int *q, mp_int *d, int keySizeInBits)
237
119
{
238
119
    mp_int pq_diff;
239
119
    mp_err err = MP_OKAY;
240
119
    PRBool ret = PR_FALSE;
241
242
119
    if (keySizeInBits < 250) {
243
        /* not a valid FIPS length, no point in our other tests */
244
        /* if you are here, and in FIPS mode, you are outside the security
245
         * policy */
246
0
        return PR_TRUE;
247
0
    }
248
249
    /* p & q are already known to be greater then sqrt(2)*2^(keySize/2-1) */
250
    /* we also know that gcd(p-1,e) = 1 and gcd(q-1,e) = 1 because the
251
     * mp_invmod() function will fail. */
252
    /* now check p-q > 2^(keysize/2-100) */
253
119
    MP_DIGITS(&pq_diff) = 0;
254
119
    CHECK_MPI_OK(mp_init(&pq_diff));
255
    /* NSS always has p > q, so we know pq_diff is positive */
256
119
    CHECK_MPI_OK(mp_sub(p, q, &pq_diff));
257
119
    if ((unsigned)mpl_significant_bits(&pq_diff) < (keySizeInBits / 2 - 100)) {
258
0
        goto cleanup;
259
0
    }
260
    /* now verify d is large enough*/
261
119
    if ((unsigned)mpl_significant_bits(d) < (keySizeInBits / 2)) {
262
0
        goto cleanup;
263
0
    }
264
119
    ret = PR_TRUE;
265
266
119
cleanup:
267
119
    mp_clear(&pq_diff);
268
119
    return ret;
269
119
}
270
271
/*
272
** Generate and return a new RSA public and private key.
273
**  Both keys are encoded in a single RSAPrivateKey structure.
274
**  "cx" is the random number generator context
275
**  "keySizeInBits" is the size of the key to be generated, in bits.
276
**     512, 1024, etc.
277
**  "publicExponent" when not NULL is a pointer to some data that
278
**     represents the public exponent to use. The data is a byte
279
**     encoded integer, in "big endian" order.
280
*/
281
RSAPrivateKey *
282
RSA_NewKey(int keySizeInBits, SECItem *publicExponent)
283
125
{
284
125
    unsigned int primeLen;
285
125
    mp_int p = { 0, 0, 0, NULL };
286
125
    mp_int q = { 0, 0, 0, NULL };
287
125
    mp_int e = { 0, 0, 0, NULL };
288
125
    mp_int d = { 0, 0, 0, NULL };
289
125
    int kiter;
290
125
    int max_attempts;
291
125
    mp_err err = MP_OKAY;
292
125
    SECStatus rv = SECSuccess;
293
125
    int prerr = 0;
294
125
    RSAPrivateKey *key = NULL;
295
125
    PLArenaPool *arena = NULL;
296
    /* Require key size to be a multiple of 16 bits. */
297
125
    if (!publicExponent || keySizeInBits % 16 != 0 ||
298
125
        BAD_RSA_KEY_SIZE((unsigned int)keySizeInBits / 8, publicExponent->len)) {
299
3
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
300
3
        return NULL;
301
3
    }
302
    /* 1.  Set the public exponent and check if it's uneven and greater than 2.*/
303
122
    MP_DIGITS(&e) = 0;
304
122
    CHECK_MPI_OK(mp_init(&e));
305
122
    SECITEM_TO_MPINT(*publicExponent, &e);
306
122
    if (mp_iseven(&e) || !(mp_cmp_d(&e, 2) > 0)) {
307
3
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
308
3
        goto cleanup;
309
3
    }
310
#ifndef NSS_FIPS_DISABLED
311
    /* Check that the exponent is not smaller than 65537  */
312
    if (mp_cmp_d(&e, 0x10001) < 0) {
313
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
314
        goto cleanup;
315
    }
316
#endif
317
318
    /* 2. Allocate arena & key */
319
119
    arena = PORT_NewArena(NSS_FREEBL_DEFAULT_CHUNKSIZE);
320
119
    if (!arena) {
321
0
        PORT_SetError(SEC_ERROR_NO_MEMORY);
322
0
        goto cleanup;
323
0
    }
324
119
    key = PORT_ArenaZNew(arena, RSAPrivateKey);
325
119
    if (!key) {
326
0
        PORT_SetError(SEC_ERROR_NO_MEMORY);
327
0
        goto cleanup;
328
0
    }
329
119
    key->arena = arena;
330
    /* length of primes p and q (in bytes) */
331
119
    primeLen = keySizeInBits / (2 * PR_BITS_PER_BYTE);
332
119
    MP_DIGITS(&p) = 0;
333
119
    MP_DIGITS(&q) = 0;
334
119
    MP_DIGITS(&d) = 0;
335
119
    CHECK_MPI_OK(mp_init(&p));
336
119
    CHECK_MPI_OK(mp_init(&q));
337
119
    CHECK_MPI_OK(mp_init(&d));
338
    /* 3.  Set the version number (PKCS1 v1.5 says it should be zero) */
339
119
    SECITEM_AllocItem(arena, &key->version, 1);
340
119
    key->version.data[0] = 0;
341
342
119
    kiter = 0;
343
119
    max_attempts = 5 * (keySizeInBits / 2); /* FIPS 186-4 B.3.3 steps 4.7 and 5.8 */
344
966
    do {
345
966
        PORT_SetError(0);
346
966
        CHECK_SEC_OK(generate_prime(&p, primeLen));
347
966
        CHECK_SEC_OK(generate_prime(&q, primeLen));
348
        /* Assure p > q */
349
        /* NOTE: PKCS #1 does not require p > q, and NSS doesn't use any
350
         * implementation optimization that requires p > q. We can remove
351
         * this code in the future.
352
         */
353
966
        if (mp_cmp(&p, &q) < 0)
354
482
            mp_exch(&p, &q);
355
        /* Attempt to use these primes to generate a key */
356
966
        rv = rsa_build_from_primes(&p, &q,
357
966
                                   &e, PR_FALSE, /* needPublicExponent=false */
358
966
                                   &d, PR_TRUE,  /* needPrivateExponent=true */
359
966
                                   key, keySizeInBits);
360
966
        if (rv == SECSuccess) {
361
119
            if (rsa_fips186_verify(&p, &q, &d, keySizeInBits)) {
362
119
                break;
363
119
            }
364
0
            prerr = SEC_ERROR_NEED_RANDOM; /* retry with different values */
365
847
        } else {
366
847
            prerr = PORT_GetError();
367
847
        }
368
847
        kiter++;
369
        /* loop until have primes */
370
847
    } while (prerr == SEC_ERROR_NEED_RANDOM && kiter < max_attempts);
371
372
122
cleanup:
373
122
    mp_clear(&p);
374
122
    mp_clear(&q);
375
122
    mp_clear(&e);
376
122
    mp_clear(&d);
377
122
    if (err) {
378
0
        MP_TO_SEC_ERROR(err);
379
0
        rv = SECFailure;
380
0
    }
381
122
    if (rv && arena) {
382
0
        PORT_FreeArena(arena, PR_TRUE);
383
0
        key = NULL;
384
0
    }
385
122
    return key;
386
122
}
387
388
mp_err
389
rsa_is_prime(mp_int *p)
390
0
{
391
0
    int res;
392
393
    /* run a Fermat test */
394
0
    res = mpp_fermat(p, 2);
395
0
    if (res != MP_OKAY) {
396
0
        return res;
397
0
    }
398
399
    /* If that passed, run some Miller-Rabin tests */
400
0
    res = mpp_pprime_secure(p, 2);
401
0
    return res;
402
0
}
403
404
/*
405
 * Factorize a RSA modulus n into p and q by using the exponents e and d.
406
 *
407
 * In: e, d, n
408
 * Out: p, q
409
 *
410
 * See Handbook of Applied Cryptography, 8.2.2(i).
411
 *
412
 * The algorithm is probabilistic, it is run 64 times and each run has a 50%
413
 * chance of succeeding with a runtime of O(log(e*d)).
414
 *
415
 * The returned p might be smaller than q.
416
 */
417
static mp_err
418
rsa_factorize_n_from_exponents(mp_int *e, mp_int *d, mp_int *p, mp_int *q,
419
                               mp_int *n)
420
944
{
421
    /* lambda is the private modulus: e*d = 1 mod lambda */
422
    /* so: e*d - 1 = k*lambda = t*2^s where t is odd */
423
944
    mp_int klambda;
424
944
    mp_int t, onetwentyeight;
425
944
    unsigned long s = 0;
426
944
    unsigned long i;
427
428
    /* cand = a^(t * 2^i) mod n, next_cand = a^(t * 2^(i+1)) mod n */
429
944
    mp_int a;
430
944
    mp_int cand;
431
944
    mp_int next_cand;
432
433
944
    mp_int n_minus_one;
434
944
    mp_err err = MP_OKAY;
435
436
944
    MP_DIGITS(&klambda) = 0;
437
944
    MP_DIGITS(&t) = 0;
438
944
    MP_DIGITS(&a) = 0;
439
944
    MP_DIGITS(&cand) = 0;
440
944
    MP_DIGITS(&n_minus_one) = 0;
441
944
    MP_DIGITS(&next_cand) = 0;
442
944
    MP_DIGITS(&onetwentyeight) = 0;
443
944
    CHECK_MPI_OK(mp_init(&klambda));
444
944
    CHECK_MPI_OK(mp_init(&t));
445
944
    CHECK_MPI_OK(mp_init(&a));
446
944
    CHECK_MPI_OK(mp_init(&cand));
447
944
    CHECK_MPI_OK(mp_init(&n_minus_one));
448
944
    CHECK_MPI_OK(mp_init(&next_cand));
449
944
    CHECK_MPI_OK(mp_init(&onetwentyeight));
450
451
944
    mp_set_int(&onetwentyeight, 128);
452
453
    /* calculate k*lambda = e*d - 1 */
454
944
    CHECK_MPI_OK(mp_mul(e, d, &klambda));
455
944
    CHECK_MPI_OK(mp_sub_d(&klambda, 1, &klambda));
456
457
    /* factorize klambda into t*2^s */
458
944
    CHECK_MPI_OK(mp_copy(&klambda, &t));
459
4.98k
    while (mpp_divis_d(&t, 2) == MP_YES) {
460
4.03k
        CHECK_MPI_OK(mp_div_2(&t, &t));
461
4.03k
        s += 1;
462
4.03k
    }
463
464
    /* precompute n_minus_one = n - 1 */
465
944
    CHECK_MPI_OK(mp_copy(n, &n_minus_one));
466
944
    CHECK_MPI_OK(mp_sub_d(&n_minus_one, 1, &n_minus_one));
467
468
    /* pick random bases a, each one has a 50% leading to a factorization */
469
944
    CHECK_MPI_OK(mp_set_int(&a, 2));
470
    /* The following is equivalent to for (a=2, a <= 128, a+=2) */
471
50.5k
    while (mp_cmp(&a, &onetwentyeight) <= 0) {
472
        /* compute the base cand = a^(t * 2^0) [i = 0] */
473
49.8k
        CHECK_MPI_OK(mp_exptmod(&a, &t, n, &cand));
474
475
243k
        for (i = 0; i < s; i++) {
476
            /* condition 1: skip the base if we hit a trivial factor of n */
477
195k
            if (mp_cmp(&cand, &n_minus_one) == 0 || mp_cmp_d(&cand, 1) == 0) {
478
1.54k
                break;
479
1.54k
            }
480
481
            /* increase i in a^(t * 2^i) by squaring the number */
482
194k
            CHECK_MPI_OK(mp_exptmod_d(&cand, 2, n, &next_cand));
483
484
            /* condition 2: a^(t * 2^(i+1)) = 1 mod n */
485
194k
            if (mp_cmp_d(&next_cand, 1) == 0) {
486
                /* conditions verified, gcd(a^(t * 2^i) - 1, n) is a factor */
487
208
                CHECK_MPI_OK(mp_sub_d(&cand, 1, &cand));
488
208
                CHECK_MPI_OK(mp_gcd(&cand, n, p));
489
208
                if (mp_cmp_d(p, 1) == 0) {
490
0
                    CHECK_MPI_OK(mp_add_d(&cand, 1, &cand));
491
0
                    break;
492
0
                }
493
208
                CHECK_MPI_OK(mp_div(n, p, q, NULL));
494
208
                goto cleanup;
495
208
            }
496
193k
            CHECK_MPI_OK(mp_copy(&next_cand, &cand));
497
193k
        }
498
499
49.6k
        CHECK_MPI_OK(mp_add_d(&a, 2, &a));
500
49.6k
    }
501
502
    /* if we reach here it's likely (2^64 - 1 / 2^64) that d is wrong */
503
727
    err = MP_RANGE;
504
505
944
cleanup:
506
944
    mp_clear(&klambda);
507
944
    mp_clear(&t);
508
944
    mp_clear(&a);
509
944
    mp_clear(&cand);
510
944
    mp_clear(&n_minus_one);
511
944
    mp_clear(&next_cand);
512
944
    mp_clear(&onetwentyeight);
513
944
    return err;
514
727
}
515
516
/*
517
 * Try to find the two primes based on 2 exponents plus a prime.
518
 *
519
 * In: e, d and p.
520
 * Out: p,q.
521
 *
522
 * Step 1, Since d = e**-1 mod phi, we know that d*e == 1 mod phi, or
523
 *  d*e = 1+k*phi, or d*e-1 = k*phi. since d is less than phi and e is
524
 *  usually less than d, then k must be an integer between e-1 and 1
525
 *  (probably on the order of e).
526
 * Step 1a, We can divide k*phi by prime-1 and get k*(q-1). This will reduce
527
 *      the size of our division through the rest of the loop.
528
 * Step 2, Loop through the values k=e-1 to 1 looking for k. k should be on
529
 *  the order or e, and e is typically small. This may take a while for
530
 *  a large random e. We are looking for a k that divides kphi
531
 *  evenly. Once we find a k that divides kphi evenly, we assume it
532
 *  is the true k. It's possible this k is not the 'true' k but has
533
 *  swapped factors of p-1 and/or q-1. Because of this, we
534
 *  tentatively continue Steps 3-6 inside this loop, and may return looking
535
 *  for another k on failure.
536
 * Step 3, Calculate our tentative phi=kphi/k. Note: real phi is (p-1)*(q-1).
537
 * Step 4a, kphi is k*(q-1), so phi is our tenative q-1. q = phi+1.
538
 *      If k is correct, q should be the right length and prime.
539
 * Step 4b, It's possible q-1 and k could have swapped factors. We now have a
540
 *  possible solution that meets our criteria. It may not be the only
541
 *      solution, however, so we keep looking. If we find more than one,
542
 *      we will fail since we cannot determine which is the correct
543
 *      solution, and returning the wrong modulus will compromise both
544
 *      moduli. If no other solution is found, we return the unique solution.
545
 *
546
 * This will return p & q. q may be larger than p in the case that p was given
547
 * and it was the smaller prime.
548
 */
549
static mp_err
550
rsa_get_prime_from_exponents(mp_int *e, mp_int *d, mp_int *p, mp_int *q,
551
                             mp_int *n, unsigned int keySizeInBits)
552
90
{
553
90
    mp_int kphi; /* k*phi */
554
90
    mp_int k;    /* current guess at 'k' */
555
90
    mp_int phi;  /* (p-1)(q-1) */
556
90
    mp_int r;    /* remainder */
557
90
    mp_int tmp;  /* p-1 if p is given */
558
90
    mp_err err = MP_OKAY;
559
90
    unsigned int order_k;
560
561
90
    MP_DIGITS(&kphi) = 0;
562
90
    MP_DIGITS(&phi) = 0;
563
90
    MP_DIGITS(&k) = 0;
564
90
    MP_DIGITS(&r) = 0;
565
90
    MP_DIGITS(&tmp) = 0;
566
90
    CHECK_MPI_OK(mp_init(&kphi));
567
90
    CHECK_MPI_OK(mp_init(&phi));
568
90
    CHECK_MPI_OK(mp_init(&k));
569
90
    CHECK_MPI_OK(mp_init(&r));
570
90
    CHECK_MPI_OK(mp_init(&tmp));
571
572
    /* our algorithm looks for a factor k whose maximum size is dependent
573
     * on the size of our smallest exponent, which had better be the public
574
     * exponent (if it's the private, the key is vulnerable to a brute force
575
     * attack).
576
     *
577
     * since our factor search is linear, we need to limit the maximum
578
     * size of the public key. this should not be a problem normally, since
579
     * public keys are usually small.
580
     *
581
     * if we want to handle larger public key sizes, we should have
582
     * a version which tries to 'completely' factor k*phi (where completely
583
     * means 'factor into primes, or composites with which are products of
584
     * large primes). Once we have all the factors, we can sort them out and
585
     * try different combinations to form our phi. The risk is if (p-1)/2,
586
     * (q-1)/2, and k are all large primes. In any case if the public key
587
     * is small (order of 20 some bits), then a linear search for k is
588
     * manageable.
589
     */
590
90
    if (mpl_significant_bits(e) > 23) {
591
3
        err = MP_RANGE;
592
3
        goto cleanup;
593
3
    }
594
595
    /* calculate k*phi = e*d - 1 */
596
87
    CHECK_MPI_OK(mp_mul(e, d, &kphi));
597
87
    CHECK_MPI_OK(mp_sub_d(&kphi, 1, &kphi));
598
599
    /* kphi is (e*d)-1, which is the same as k*(p-1)(q-1)
600
     * d < (p-1)(q-1), therefor k must be less than e-1
601
     * We can narrow down k even more, though. Since p and q are odd and both
602
     * have their high bit set, then we know that phi must be on order of
603
     * keySizeBits.
604
     */
605
87
    order_k = (unsigned)mpl_significant_bits(&kphi) - keySizeInBits;
606
607
87
    if (order_k <= 1) {
608
3
        err = MP_RANGE;
609
3
        goto cleanup;
610
3
    }
611
612
    /* for (k=kinit; order(k) >= order_k; k--) { */
613
    /* k=kinit: k can't be bigger than  kphi/2^(keySizeInBits -1) */
614
84
    CHECK_MPI_OK(mp_2expt(&k, keySizeInBits - 1));
615
84
    CHECK_MPI_OK(mp_div(&kphi, &k, &k, NULL));
616
84
    if (mp_cmp(&k, e) >= 0) {
617
        /* also can't be bigger then e-1 */
618
42
        CHECK_MPI_OK(mp_sub_d(e, 1, &k));
619
42
    }
620
621
    /* calculate our temp value */
622
    /* This saves recalculating this value when the k guess is wrong, which
623
     * is reasonably frequent. */
624
    /* tmp = p-1 (used to calculate q-1= phi/tmp) */
625
84
    CHECK_MPI_OK(mp_sub_d(p, 1, &tmp));
626
84
    CHECK_MPI_OK(mp_div(&kphi, &tmp, &kphi, &r));
627
78
    if (mp_cmp_z(&r) != 0) {
628
        /* p-1 doesn't divide kphi, some parameter wasn't correct */
629
30
        err = MP_RANGE;
630
30
        goto cleanup;
631
30
    }
632
48
    mp_zero(q);
633
    /* kphi is now k*(q-1) */
634
635
    /* rest of the for loop */
636
143k
    for (; (err == MP_OKAY) && (mpl_significant_bits(&k) >= order_k);
637
143k
         err = mp_sub_d(&k, 1, &k)) {
638
143k
        CHECK_MPI_OK(err);
639
        /* looking for k as a factor of kphi */
640
143k
        CHECK_MPI_OK(mp_div(&kphi, &k, &phi, &r));
641
143k
        if (mp_cmp_z(&r) != 0) {
642
            /* not a factor, try the next one */
643
142k
            continue;
644
142k
        }
645
        /* we have a possible phi, see if it works */
646
129
        if ((unsigned)mpl_significant_bits(&phi) != keySizeInBits / 2) {
647
            /* phi is not the right size */
648
129
            continue;
649
129
        }
650
        /* phi should be divisible by 2, since
651
         * q is odd and phi=(q-1). */
652
0
        if (mpp_divis_d(&phi, 2) == MP_NO) {
653
            /* phi is not divisible by 4 */
654
0
            continue;
655
0
        }
656
        /* we now have a candidate for the second prime */
657
0
        CHECK_MPI_OK(mp_add_d(&phi, 1, &tmp));
658
659
        /* check to make sure it is prime */
660
0
        err = rsa_is_prime(&tmp);
661
0
        if (err != MP_OKAY) {
662
0
            if (err == MP_NO) {
663
                /* No, then we still have the wrong phi */
664
0
                continue;
665
0
            }
666
0
            goto cleanup;
667
0
        }
668
        /*
669
         * It is possible that we have the wrong phi if
670
         * k_guess*(q_guess-1) = k*(q-1) (k and q-1 have swapped factors).
671
         * since our q_quess is prime, however. We have found a valid
672
         * rsa key because:
673
         *   q is the correct order of magnitude.
674
         *   phi = (p-1)(q-1) where p and q are both primes.
675
         *   e*d mod phi = 1.
676
         * There is no way to know from the info given if this is the
677
         * original key. We never want to return the wrong key because if
678
         * two moduli with the same factor is known, then euclid's gcd
679
         * algorithm can be used to find that factor. Even though the
680
         * caller didn't pass the original modulus, it doesn't mean the
681
         * modulus wasn't known or isn't available somewhere. So to be safe
682
         * if we can't be sure we have the right q, we don't return any.
683
         *
684
         * So to make sure we continue looking for other valid q's. If none
685
         * are found, then we can safely return this one, otherwise we just
686
         * fail */
687
0
        if (mp_cmp_z(q) != 0) {
688
            /* this is the second valid q, don't return either,
689
             * just fail */
690
0
            err = MP_RANGE;
691
0
            break;
692
0
        }
693
        /* we only have one q so far, save it and if no others are found,
694
         * it's safe to return it */
695
0
        CHECK_MPI_OK(mp_copy(&tmp, q));
696
0
        continue;
697
0
    }
698
48
    if ((unsigned)mpl_significant_bits(&k) < order_k) {
699
48
        if (mp_cmp_z(q) == 0) {
700
            /* If we get here, something was wrong with the parameters we
701
             * were given */
702
48
            err = MP_RANGE;
703
48
        }
704
48
    }
705
90
cleanup:
706
90
    mp_clear(&kphi);
707
90
    mp_clear(&phi);
708
90
    mp_clear(&k);
709
90
    mp_clear(&r);
710
90
    mp_clear(&tmp);
711
90
    return err;
712
48
}
713
714
/*
715
 * take a private key with only a few elements and fill out the missing pieces.
716
 *
717
 * All the entries will be overwritten with data allocated out of the arena
718
 * If no arena is supplied, one will be created.
719
 *
720
 * The following fields must be supplied in order for this function
721
 * to succeed:
722
 *   one of either publicExponent or privateExponent
723
 *   two more of the following 5 parameters.
724
 *      modulus (n)
725
 *      prime1  (p)
726
 *      prime2  (q)
727
 *      publicExponent (e)
728
 *      privateExponent (d)
729
 *
730
 * NOTE: if only the publicExponent, privateExponent, and one prime is given,
731
 * then there may be more than one RSA key that matches that combination.
732
 *
733
 * All parameters will be replaced in the key structure with new parameters
734
 * Allocated out of the arena. There is no attempt to free the old structures.
735
 * Prime1 will always be greater than prime2 (even if the caller supplies the
736
 * smaller prime as prime1 or the larger prime as prime2). The parameters are
737
 * not overwritten on failure.
738
 *
739
 *  How it works:
740
 *     We can generate all the parameters from one of the exponents, plus the
741
 *        two primes. (rsa_build_key_from_primes)
742
 *     If we are given one of the exponents and both primes, we are done.
743
 *     If we are given one of the exponents, the modulus and one prime, we
744
 *        caclulate the second prime by dividing the modulus by the given
745
 *        prime, giving us an exponent and 2 primes.
746
 *     If we are given 2 exponents and one of the primes we calculate
747
 *        k*phi = d*e-1, where k is an integer less than d which
748
 *        divides d*e-1. We find factor k so we can isolate phi.
749
 *            phi = (p-1)(q-1)
750
 *        We can use phi to find the other prime as follows:
751
 *        q = (phi/(p-1)) + 1. We now have 2 primes and an exponent.
752
 *        (NOTE: if more then one prime meets this condition, the operation
753
 *        will fail. See comments elsewhere in this file about this).
754
 *        (rsa_get_prime_from_exponents)
755
 *     If we are given 2 exponents and the modulus we factor the modulus to
756
 *        get the 2 missing primes (rsa_factorize_n_from_exponents)
757
 *
758
 */
759
SECStatus
760
RSA_PopulatePrivateKey(RSAPrivateKey *key)
761
10.4k
{
762
10.4k
    PLArenaPool *arena = NULL;
763
10.4k
    PRBool needPublicExponent = PR_TRUE;
764
10.4k
    PRBool needPrivateExponent = PR_TRUE;
765
10.4k
    PRBool hasModulus = PR_FALSE;
766
10.4k
    unsigned int keySizeInBits = 0;
767
10.4k
    int prime_count = 0;
768
    /* standard RSA nominclature */
769
10.4k
    mp_int p, q, e, d, n;
770
    /* remainder */
771
10.4k
    mp_int r;
772
10.4k
    mp_err err = 0;
773
10.4k
    SECStatus rv = SECFailure;
774
775
10.4k
    MP_DIGITS(&p) = 0;
776
10.4k
    MP_DIGITS(&q) = 0;
777
10.4k
    MP_DIGITS(&e) = 0;
778
10.4k
    MP_DIGITS(&d) = 0;
779
10.4k
    MP_DIGITS(&n) = 0;
780
10.4k
    MP_DIGITS(&r) = 0;
781
10.4k
    CHECK_MPI_OK(mp_init(&p));
782
10.4k
    CHECK_MPI_OK(mp_init(&q));
783
10.4k
    CHECK_MPI_OK(mp_init(&e));
784
10.4k
    CHECK_MPI_OK(mp_init(&d));
785
10.4k
    CHECK_MPI_OK(mp_init(&n));
786
10.4k
    CHECK_MPI_OK(mp_init(&r));
787
788
    /* if the key didn't already have an arena, create one. */
789
10.4k
    if (key->arena == NULL) {
790
0
        arena = PORT_NewArena(NSS_FREEBL_DEFAULT_CHUNKSIZE);
791
0
        if (!arena) {
792
0
            goto cleanup;
793
0
        }
794
0
        key->arena = arena;
795
0
    }
796
797
    /* load up the known exponents */
798
10.4k
    if (key->publicExponent.data) {
799
1.25k
        SECITEM_TO_MPINT(key->publicExponent, &e);
800
1.25k
        needPublicExponent = PR_FALSE;
801
1.25k
    }
802
10.4k
    if (key->privateExponent.data) {
803
9.81k
        SECITEM_TO_MPINT(key->privateExponent, &d);
804
9.81k
        needPrivateExponent = PR_FALSE;
805
9.81k
    }
806
10.4k
    if (needPrivateExponent && needPublicExponent) {
807
        /* Not enough information, we need at least one exponent */
808
471
        err = MP_BADARG;
809
471
        goto cleanup;
810
471
    }
811
812
    /* load up the known primes. If only one prime is given, it will be
813
     * assigned 'p'. Once we have both primes, well make sure p is the larger.
814
     * The value prime_count tells us howe many we have acquired.
815
     */
816
9.93k
    if (key->prime1.data) {
817
8.59k
        int primeLen = key->prime1.len;
818
8.59k
        if (key->prime1.data[0] == 0) {
819
798
            primeLen--;
820
798
        }
821
8.59k
        keySizeInBits = primeLen * 2 * PR_BITS_PER_BYTE;
822
8.59k
        SECITEM_TO_MPINT(key->prime1, &p);
823
8.59k
        prime_count++;
824
8.59k
    }
825
9.93k
    if (key->prime2.data) {
826
7.99k
        int primeLen = key->prime2.len;
827
7.99k
        if (key->prime2.data[0] == 0) {
828
721
            primeLen--;
829
721
        }
830
7.99k
        keySizeInBits = primeLen * 2 * PR_BITS_PER_BYTE;
831
7.99k
        SECITEM_TO_MPINT(key->prime2, prime_count ? &q : &p);
832
7.99k
        prime_count++;
833
7.99k
    }
834
    /* load up the modulus */
835
9.93k
    if (key->modulus.data) {
836
8.45k
        int modLen = key->modulus.len;
837
8.45k
        if (key->modulus.data[0] == 0) {
838
903
            modLen--;
839
903
        }
840
8.45k
        keySizeInBits = modLen * PR_BITS_PER_BYTE;
841
8.45k
        SECITEM_TO_MPINT(key->modulus, &n);
842
8.45k
        hasModulus = PR_TRUE;
843
8.45k
    }
844
    /* if we have the modulus and one prime, calculate the second. */
845
9.93k
    if ((prime_count == 1) && (hasModulus)) {
846
1.15k
        if (mp_div(&n, &p, &q, &r) != MP_OKAY || mp_cmp_z(&r) != 0) {
847
            /* p is not a factor or n, fail */
848
401
            err = MP_BADARG;
849
401
            goto cleanup;
850
401
        }
851
749
        prime_count++;
852
749
    }
853
854
    /* If we didn't have enough primes try to calculate the primes from
855
     * the exponents */
856
9.53k
    if (prime_count < 2) {
857
        /* if we don't have at least 2 primes at this point, then we need both
858
         * exponents and one prime or a modulus*/
859
1.13k
        if (!needPublicExponent && !needPrivateExponent &&
860
1.13k
            (prime_count > 0)) {
861
90
            CHECK_MPI_OK(rsa_get_prime_from_exponents(&e, &d, &p, &q, &n,
862
90
                                                      keySizeInBits));
863
1.04k
        } else if (!needPublicExponent && !needPrivateExponent && hasModulus) {
864
944
            CHECK_MPI_OK(rsa_factorize_n_from_exponents(&e, &d, &p, &q, &n));
865
944
        } else {
866
            /* not enough given parameters to get both primes */
867
100
            err = MP_BADARG;
868
100
            goto cleanup;
869
100
        }
870
1.13k
    }
871
872
    /* Assure p > q */
873
    /* NOTE: PKCS #1 does not require p > q, and NSS doesn't use any
874
      * implementation optimization that requires p > q. We can remove
875
      * this code in the future.
876
      */
877
8.61k
    if (mp_cmp(&p, &q) < 0)
878
7.13k
        mp_exch(&p, &q);
879
880
    /* we now have our 2 primes and at least one exponent, we can fill
881
      * in the key */
882
8.61k
    rv = rsa_build_from_primes(&p, &q,
883
8.61k
                               &e, needPublicExponent,
884
8.61k
                               &d, needPrivateExponent,
885
8.61k
                               key, keySizeInBits);
886
10.4k
cleanup:
887
10.4k
    mp_clear(&p);
888
10.4k
    mp_clear(&q);
889
10.4k
    mp_clear(&e);
890
10.4k
    mp_clear(&d);
891
10.4k
    mp_clear(&n);
892
10.4k
    mp_clear(&r);
893
10.4k
    if (err) {
894
1.79k
        MP_TO_SEC_ERROR(err);
895
1.79k
        rv = SECFailure;
896
1.79k
    }
897
10.4k
    if (rv && arena) {
898
0
        PORT_FreeArena(arena, PR_TRUE);
899
0
        key->arena = NULL;
900
0
    }
901
10.4k
    return rv;
902
10.4k
}
903
904
static unsigned int
905
rsa_modulusLen(SECItem *modulus)
906
19.6k
{
907
19.6k
    if (modulus->len == 0) {
908
345
        return 0;
909
19.2k
    };
910
19.2k
    unsigned char byteZero = modulus->data[0];
911
19.2k
    unsigned int modLen = modulus->len - !byteZero;
912
19.2k
    return modLen;
913
19.6k
}
914
915
/*
916
** Perform a raw public-key operation
917
**  Length of input and output buffers are equal to key's modulus len.
918
*/
919
SECStatus
920
RSA_PublicKeyOp(RSAPublicKey *key,
921
                unsigned char *output,
922
                const unsigned char *input)
923
7.62k
{
924
7.62k
    unsigned int modLen, expLen, offset;
925
7.62k
    mp_int n, e, m, c;
926
7.62k
    mp_err err = MP_OKAY;
927
7.62k
    SECStatus rv = SECSuccess;
928
7.62k
    if (!key || !output || !input) {
929
0
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
930
0
        return SECFailure;
931
0
    }
932
7.62k
    MP_DIGITS(&n) = 0;
933
7.62k
    MP_DIGITS(&e) = 0;
934
7.62k
    MP_DIGITS(&m) = 0;
935
7.62k
    MP_DIGITS(&c) = 0;
936
7.62k
    CHECK_MPI_OK(mp_init(&n));
937
7.62k
    CHECK_MPI_OK(mp_init(&e));
938
7.62k
    CHECK_MPI_OK(mp_init(&m));
939
7.62k
    CHECK_MPI_OK(mp_init(&c));
940
7.62k
    modLen = rsa_modulusLen(&key->modulus);
941
7.62k
    expLen = rsa_modulusLen(&key->publicExponent);
942
943
7.62k
    if (modLen == 0 || expLen == 0) {
944
265
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
945
265
        rv = SECFailure;
946
265
        goto cleanup;
947
265
    }
948
949
    /* 1.  Obtain public key (n, e) */
950
7.35k
    if (BAD_RSA_KEY_SIZE(modLen, expLen)) {
951
570
        PORT_SetError(SEC_ERROR_INVALID_KEY);
952
570
        rv = SECFailure;
953
570
        goto cleanup;
954
570
    }
955
6.78k
    SECITEM_TO_MPINT(key->modulus, &n);
956
6.78k
    SECITEM_TO_MPINT(key->publicExponent, &e);
957
6.78k
    if (e.used > n.used) {
958
        /* exponent should not be greater than modulus */
959
0
        PORT_SetError(SEC_ERROR_INVALID_KEY);
960
0
        rv = SECFailure;
961
0
        goto cleanup;
962
0
    }
963
    /* 2. check input out of range (needs to be in range [0..n-1]) */
964
6.78k
    offset = (key->modulus.data[0] == 0) ? 1 : 0; /* may be leading 0 */
965
6.78k
    if (memcmp(input, key->modulus.data + offset, modLen) >= 0) {
966
100
        PORT_SetError(SEC_ERROR_INPUT_LEN);
967
100
        rv = SECFailure;
968
100
        goto cleanup;
969
100
    }
970
    /* 2 bis.  Represent message as integer in range [0..n-1] */
971
6.68k
    CHECK_MPI_OK(mp_read_unsigned_octets(&m, input, modLen));
972
/* 3.  Compute c = m**e mod n */
973
#ifdef USE_MPI_EXPT_D
974
    /* XXX see which is faster */
975
    if (MP_USED(&e) == 1) {
976
        CHECK_MPI_OK(mp_exptmod_d(&m, MP_DIGIT(&e, 0), &n, &c));
977
    } else
978
#endif
979
6.68k
        CHECK_MPI_OK(mp_exptmod(&m, &e, &n, &c));
980
    /* 4.  result c is ciphertext */
981
6.68k
    err = mp_to_fixlen_octets(&c, output, modLen);
982
6.68k
    if (err >= 0)
983
6.68k
        err = MP_OKAY;
984
7.62k
cleanup:
985
7.62k
    mp_clear(&n);
986
7.62k
    mp_clear(&e);
987
7.62k
    mp_clear(&m);
988
7.62k
    mp_clear(&c);
989
7.62k
    if (err) {
990
0
        MP_TO_SEC_ERROR(err);
991
0
        rv = SECFailure;
992
0
    }
993
7.62k
    return rv;
994
7.62k
}
995
996
/*
997
**  RSA Private key operation (no CRT).
998
*/
999
static SECStatus
1000
rsa_PrivateKeyOpNoCRT(RSAPrivateKey *key, mp_int *m, mp_int *c, mp_int *n,
1001
                      unsigned int modLen)
1002
0
{
1003
0
    mp_int d;
1004
0
    mp_err err = MP_OKAY;
1005
0
    SECStatus rv = SECSuccess;
1006
0
    MP_DIGITS(&d) = 0;
1007
0
    CHECK_MPI_OK(mp_init(&d));
1008
0
    SECITEM_TO_MPINT(key->privateExponent, &d);
1009
    /* 1. m = c**d mod n */
1010
0
    CHECK_MPI_OK(mp_exptmod(c, &d, n, m));
1011
0
cleanup:
1012
0
    mp_clear(&d);
1013
0
    if (err) {
1014
0
        MP_TO_SEC_ERROR(err);
1015
0
        rv = SECFailure;
1016
0
    }
1017
0
    return rv;
1018
0
}
1019
1020
/*
1021
**  RSA Private key operation using CRT.
1022
*/
1023
static SECStatus
1024
rsa_PrivateKeyOpCRTNoCheck(RSAPrivateKey *key, mp_int *m, mp_int *c)
1025
2.17k
{
1026
2.17k
    mp_int p, q, d_p, d_q, qInv;
1027
    /*
1028
            The length of the randomness comes from the papers: 
1029
            https://link.springer.com/chapter/10.1007/978-3-642-29912-4_7
1030
            https://link.springer.com/chapter/10.1007/978-3-642-21554-4_5. 
1031
        */
1032
2.17k
    mp_int blinding_dp, blinding_dq, r1, r2;
1033
2.17k
    unsigned char random_block[EXP_BLINDING_RANDOMNESS_LEN_BYTES];
1034
2.17k
    mp_int m1, m2, h, ctmp;
1035
2.17k
    mp_err err = MP_OKAY;
1036
2.17k
    SECStatus rv = SECSuccess;
1037
2.17k
    MP_DIGITS(&p) = 0;
1038
2.17k
    MP_DIGITS(&q) = 0;
1039
2.17k
    MP_DIGITS(&d_p) = 0;
1040
2.17k
    MP_DIGITS(&d_q) = 0;
1041
2.17k
    MP_DIGITS(&qInv) = 0;
1042
2.17k
    MP_DIGITS(&m1) = 0;
1043
2.17k
    MP_DIGITS(&m2) = 0;
1044
2.17k
    MP_DIGITS(&h) = 0;
1045
2.17k
    MP_DIGITS(&ctmp) = 0;
1046
2.17k
    MP_DIGITS(&blinding_dp) = 0;
1047
2.17k
    MP_DIGITS(&blinding_dq) = 0;
1048
2.17k
    MP_DIGITS(&r1) = 0;
1049
2.17k
    MP_DIGITS(&r2) = 0;
1050
1051
2.17k
    CHECK_MPI_OK(mp_init(&p));
1052
2.17k
    CHECK_MPI_OK(mp_init(&q));
1053
2.17k
    CHECK_MPI_OK(mp_init(&d_p));
1054
2.17k
    CHECK_MPI_OK(mp_init(&d_q));
1055
2.17k
    CHECK_MPI_OK(mp_init(&qInv));
1056
2.17k
    CHECK_MPI_OK(mp_init(&m1));
1057
2.17k
    CHECK_MPI_OK(mp_init(&m2));
1058
2.17k
    CHECK_MPI_OK(mp_init(&h));
1059
2.17k
    CHECK_MPI_OK(mp_init(&ctmp));
1060
2.17k
    CHECK_MPI_OK(mp_init(&blinding_dp));
1061
2.17k
    CHECK_MPI_OK(mp_init(&blinding_dq));
1062
2.17k
    CHECK_MPI_OK(mp_init_size(&r1, EXP_BLINDING_RANDOMNESS_LEN));
1063
2.17k
    CHECK_MPI_OK(mp_init_size(&r2, EXP_BLINDING_RANDOMNESS_LEN));
1064
1065
    /* copy private key parameters into mp integers */
1066
2.17k
    SECITEM_TO_MPINT(key->prime1, &p);         /* p */
1067
2.17k
    SECITEM_TO_MPINT(key->prime2, &q);         /* q */
1068
2.17k
    SECITEM_TO_MPINT(key->exponent1, &d_p);    /* d_p  = d mod (p-1) */
1069
2.17k
    SECITEM_TO_MPINT(key->exponent2, &d_q);    /* d_q  = d mod (q-1) */
1070
2.17k
    SECITEM_TO_MPINT(key->coefficient, &qInv); /* qInv = q**-1 mod p */
1071
1072
    // blinding_dp = 1
1073
2.17k
    CHECK_MPI_OK(mp_set_int(&blinding_dp, 1));
1074
    // blinding_dp = p - 1
1075
2.17k
    CHECK_MPI_OK(mp_sub(&p, &blinding_dp, &blinding_dp));
1076
    // generating a random value
1077
2.17k
    RNG_GenerateGlobalRandomBytes(random_block, EXP_BLINDING_RANDOMNESS_LEN_BYTES);
1078
2.17k
    MP_USED(&r1) = EXP_BLINDING_RANDOMNESS_LEN;
1079
2.17k
    memcpy(MP_DIGITS(&r1), random_block, sizeof(random_block));
1080
    // blinding_dp = random * (p - 1)
1081
2.17k
    CHECK_MPI_OK(mp_mul(&blinding_dp, &r1, &blinding_dp));
1082
    //d_p = d_p + random * (p - 1)
1083
2.17k
    CHECK_MPI_OK(mp_add(&d_p, &blinding_dp, &d_p));
1084
1085
    // blinding_dq = 1
1086
2.17k
    CHECK_MPI_OK(mp_set_int(&blinding_dq, 1));
1087
    // blinding_dq = q - 1
1088
2.17k
    CHECK_MPI_OK(mp_sub(&q, &blinding_dq, &blinding_dq));
1089
    // generating a random value
1090
2.17k
    RNG_GenerateGlobalRandomBytes(random_block, EXP_BLINDING_RANDOMNESS_LEN_BYTES);
1091
2.17k
    memcpy(MP_DIGITS(&r2), random_block, sizeof(random_block));
1092
2.17k
    MP_USED(&r2) = EXP_BLINDING_RANDOMNESS_LEN;
1093
    // blinding_dq = random * (q - 1)
1094
2.17k
    CHECK_MPI_OK(mp_mul(&blinding_dq, &r2, &blinding_dq));
1095
    //d_q = d_q + random * (q-1)
1096
2.17k
    CHECK_MPI_OK(mp_add(&d_q, &blinding_dq, &d_q));
1097
1098
    /* 1. m1 = c**d_p mod p */
1099
2.17k
    CHECK_MPI_OK(mp_mod(c, &p, &ctmp));
1100
2.17k
    CHECK_MPI_OK(mp_exptmod(&ctmp, &d_p, &p, &m1));
1101
    /* 2. m2 = c**d_q mod q */
1102
2.17k
    CHECK_MPI_OK(mp_mod(c, &q, &ctmp));
1103
2.17k
    CHECK_MPI_OK(mp_exptmod(&ctmp, &d_q, &q, &m2));
1104
    /* 3.  h = (m1 - m2) * qInv mod p */
1105
2.17k
    CHECK_MPI_OK(mp_submod(&m1, &m2, &p, &h));
1106
2.17k
    CHECK_MPI_OK(mp_mulmod(&h, &qInv, &p, &h));
1107
    /* 4.  m = m2 + h * q */
1108
2.17k
    CHECK_MPI_OK(mp_mul(&h, &q, m));
1109
2.17k
    CHECK_MPI_OK(mp_add(m, &m2, m));
1110
2.17k
cleanup:
1111
2.17k
    mp_clear(&p);
1112
2.17k
    mp_clear(&q);
1113
2.17k
    mp_clear(&d_p);
1114
2.17k
    mp_clear(&d_q);
1115
2.17k
    mp_clear(&qInv);
1116
2.17k
    mp_clear(&m1);
1117
2.17k
    mp_clear(&m2);
1118
2.17k
    mp_clear(&h);
1119
2.17k
    mp_clear(&ctmp);
1120
2.17k
    mp_clear(&blinding_dp);
1121
2.17k
    mp_clear(&blinding_dq);
1122
2.17k
    mp_clear(&r1);
1123
2.17k
    mp_clear(&r2);
1124
2.17k
    if (err) {
1125
0
        MP_TO_SEC_ERROR(err);
1126
0
        rv = SECFailure;
1127
0
    }
1128
2.17k
    return rv;
1129
2.17k
}
1130
1131
/*
1132
** An attack against RSA CRT was described by Boneh, DeMillo, and Lipton in:
1133
** "On the Importance of Eliminating Errors in Cryptographic Computations",
1134
** http://theory.stanford.edu/~dabo/papers/faults.ps.gz
1135
**
1136
** As a defense against the attack, carry out the private key operation,
1137
** followed up with a public key operation to invert the result.
1138
** Verify that result against the input.
1139
*/
1140
static SECStatus
1141
rsa_PrivateKeyOpCRTCheckedPubKey(RSAPrivateKey *key, mp_int *m, mp_int *c)
1142
2.17k
{
1143
2.17k
    mp_int n, e, v;
1144
2.17k
    mp_err err = MP_OKAY;
1145
2.17k
    SECStatus rv = SECSuccess;
1146
2.17k
    MP_DIGITS(&n) = 0;
1147
2.17k
    MP_DIGITS(&e) = 0;
1148
2.17k
    MP_DIGITS(&v) = 0;
1149
2.17k
    CHECK_MPI_OK(mp_init(&n));
1150
2.17k
    CHECK_MPI_OK(mp_init(&e));
1151
2.17k
    CHECK_MPI_OK(mp_init(&v));
1152
2.17k
    CHECK_SEC_OK(rsa_PrivateKeyOpCRTNoCheck(key, m, c));
1153
2.17k
    SECITEM_TO_MPINT(key->modulus, &n);
1154
2.17k
    SECITEM_TO_MPINT(key->publicExponent, &e);
1155
    /* Perform a public key operation v = m ** e mod n */
1156
2.17k
    CHECK_MPI_OK(mp_exptmod(m, &e, &n, &v));
1157
2.17k
    if (mp_cmp(&v, c) != 0) {
1158
2.12k
        rv = SECFailure;
1159
2.12k
    }
1160
2.17k
cleanup:
1161
2.17k
    mp_clear(&n);
1162
2.17k
    mp_clear(&e);
1163
2.17k
    mp_clear(&v);
1164
2.17k
    if (err) {
1165
0
        MP_TO_SEC_ERROR(err);
1166
0
        rv = SECFailure;
1167
0
    }
1168
2.17k
    return rv;
1169
2.17k
}
1170
1171
static PRCallOnceType coBPInit = { 0, 0, 0 };
1172
static PRStatus
1173
init_blinding_params_list(void)
1174
19.3k
{
1175
19.3k
    blindingParamsList.lock = PZ_NewLock(nssILockOther);
1176
19.3k
    if (!blindingParamsList.lock) {
1177
0
        PORT_SetError(SEC_ERROR_NO_MEMORY);
1178
0
        return PR_FAILURE;
1179
0
    }
1180
19.3k
    blindingParamsList.cVar = PR_NewCondVar(blindingParamsList.lock);
1181
19.3k
    if (!blindingParamsList.cVar) {
1182
0
        PORT_SetError(SEC_ERROR_NO_MEMORY);
1183
0
        return PR_FAILURE;
1184
0
    }
1185
19.3k
    blindingParamsList.waitCount = 0;
1186
19.3k
    PR_INIT_CLIST(&blindingParamsList.head);
1187
19.3k
    return PR_SUCCESS;
1188
19.3k
}
1189
1190
static SECStatus
1191
generate_blinding_params(RSAPrivateKey *key, mp_int *f, mp_int *g, mp_int *n,
1192
                         unsigned int modLen)
1193
4.32k
{
1194
4.32k
    SECStatus rv = SECSuccess;
1195
4.32k
    mp_int e, k;
1196
4.32k
    mp_err err = MP_OKAY;
1197
4.32k
    unsigned char *kb = NULL;
1198
1199
4.32k
    MP_DIGITS(&e) = 0;
1200
4.32k
    MP_DIGITS(&k) = 0;
1201
4.32k
    CHECK_MPI_OK(mp_init(&e));
1202
4.32k
    CHECK_MPI_OK(mp_init(&k));
1203
4.32k
    SECITEM_TO_MPINT(key->publicExponent, &e);
1204
    /* generate random k < n */
1205
4.32k
    kb = PORT_Alloc(modLen);
1206
4.32k
    if (!kb) {
1207
0
        PORT_SetError(SEC_ERROR_NO_MEMORY);
1208
0
        goto cleanup;
1209
0
    }
1210
4.32k
    CHECK_SEC_OK(RNG_GenerateGlobalRandomBytes(kb, modLen));
1211
4.32k
    CHECK_MPI_OK(mp_read_unsigned_octets(&k, kb, modLen));
1212
    /* k < n */
1213
4.32k
    CHECK_MPI_OK(mp_mod(&k, n, &k));
1214
    /* f = k**e mod n */
1215
4.32k
    CHECK_MPI_OK(mp_exptmod(&k, &e, n, f));
1216
    /* g = k**-1 mod n */
1217
4.32k
    CHECK_MPI_OK(mp_invmod(&k, n, g));
1218
4.32k
cleanup:
1219
4.32k
    if (kb)
1220
4.32k
        PORT_ZFree(kb, modLen);
1221
4.32k
    mp_clear(&k);
1222
4.32k
    mp_clear(&e);
1223
4.32k
    if (err) {
1224
2.15k
        MP_TO_SEC_ERROR(err);
1225
2.15k
        rv = SECFailure;
1226
2.15k
    }
1227
4.32k
    return rv;
1228
4.32k
}
1229
1230
static SECStatus
1231
init_blinding_params(RSABlindingParams *rsabp, RSAPrivateKey *key,
1232
                     mp_int *n, unsigned int modLen)
1233
4.32k
{
1234
4.32k
    blindingParams *bp = rsabp->array;
1235
4.32k
    int i = 0;
1236
1237
    /* Initialize the list pointer for the element */
1238
4.32k
    PR_INIT_CLIST(&rsabp->link);
1239
90.7k
    for (i = 0; i < RSA_BLINDING_PARAMS_MAX_CACHE_SIZE; ++i, ++bp) {
1240
86.4k
        bp->next = bp + 1;
1241
86.4k
        MP_DIGITS(&bp->f) = 0;
1242
86.4k
        MP_DIGITS(&bp->g) = 0;
1243
86.4k
        bp->counter = 0;
1244
86.4k
    }
1245
    /* The last bp->next value was initialized with out
1246
     * of rsabp->array pointer and must be set to NULL
1247
     */
1248
4.32k
    rsabp->array[RSA_BLINDING_PARAMS_MAX_CACHE_SIZE - 1].next = NULL;
1249
1250
4.32k
    bp = rsabp->array;
1251
4.32k
    rsabp->bp = NULL;
1252
4.32k
    rsabp->free = bp;
1253
1254
    /* List elements are keyed using the modulus */
1255
4.32k
    return SECITEM_CopyItem(NULL, &rsabp->modulus, &key->modulus);
1256
4.32k
}
1257
1258
static SECStatus
1259
get_blinding_params(RSAPrivateKey *key, mp_int *n, unsigned int modLen,
1260
                    mp_int *f, mp_int *g)
1261
4.32k
{
1262
4.32k
    RSABlindingParams *rsabp = NULL;
1263
4.32k
    blindingParams *bpUnlinked = NULL;
1264
4.32k
    blindingParams *bp;
1265
4.32k
    PRCList *el;
1266
4.32k
    SECStatus rv = SECSuccess;
1267
4.32k
    mp_err err = MP_OKAY;
1268
4.32k
    int cmp = -1;
1269
4.32k
    PRBool holdingLock = PR_FALSE;
1270
1271
4.32k
    do {
1272
4.32k
        if (blindingParamsList.lock == NULL) {
1273
0
            PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
1274
0
            return SECFailure;
1275
0
        }
1276
        /* Acquire the list lock */
1277
4.32k
        PZ_Lock(blindingParamsList.lock);
1278
4.32k
        holdingLock = PR_TRUE;
1279
1280
        /* Walk the list looking for the private key */
1281
4.32k
        for (el = PR_NEXT_LINK(&blindingParamsList.head);
1282
4.32k
             el != &blindingParamsList.head;
1283
4.32k
             el = PR_NEXT_LINK(el)) {
1284
0
            rsabp = (RSABlindingParams *)el;
1285
0
            cmp = SECITEM_CompareItem(&rsabp->modulus, &key->modulus);
1286
0
            if (cmp >= 0) {
1287
                /* The key is found or not in the list. */
1288
0
                break;
1289
0
            }
1290
0
        }
1291
1292
4.32k
        if (cmp) {
1293
            /* At this point, the key is not in the list.  el should point to
1294
            ** the list element before which this key should be inserted.
1295
            */
1296
4.32k
            rsabp = PORT_ZNew(RSABlindingParams);
1297
4.32k
            if (!rsabp) {
1298
0
                PORT_SetError(SEC_ERROR_NO_MEMORY);
1299
0
                goto cleanup;
1300
0
            }
1301
1302
4.32k
            rv = init_blinding_params(rsabp, key, n, modLen);
1303
4.32k
            if (rv != SECSuccess) {
1304
0
                PORT_ZFree(rsabp, sizeof(RSABlindingParams));
1305
0
                goto cleanup;
1306
0
            }
1307
1308
            /* Insert the new element into the list
1309
            ** If inserting in the middle of the list, el points to the link
1310
            ** to insert before.  Otherwise, the link needs to be appended to
1311
            ** the end of the list, which is the same as inserting before the
1312
            ** head (since el would have looped back to the head).
1313
            */
1314
4.32k
            PR_INSERT_BEFORE(&rsabp->link, el);
1315
4.32k
        }
1316
1317
        /* We've found (or created) the RSAblindingParams struct for this key.
1318
         * Now, search its list of ready blinding params for a usable one.
1319
         */
1320
4.32k
        while (0 != (bp = rsabp->bp)) {
1321
#ifdef UNSAFE_FUZZER_MODE
1322
            /* Found a match and there are still remaining uses left */
1323
            /* Return the parameters */
1324
            CHECK_MPI_OK(mp_copy(&bp->f, f));
1325
            CHECK_MPI_OK(mp_copy(&bp->g, g));
1326
1327
            PZ_Unlock(blindingParamsList.lock);
1328
            return SECSuccess;
1329
#else
1330
0
            if (--(bp->counter) > 0) {
1331
                /* Found a match and there are still remaining uses left */
1332
                /* Return the parameters */
1333
0
                CHECK_MPI_OK(mp_copy(&bp->f, f));
1334
0
                CHECK_MPI_OK(mp_copy(&bp->g, g));
1335
1336
0
                PZ_Unlock(blindingParamsList.lock);
1337
0
                return SECSuccess;
1338
0
            }
1339
            /* exhausted this one, give its values to caller, and
1340
             * then retire it.
1341
             */
1342
0
            mp_exch(&bp->f, f);
1343
0
            mp_exch(&bp->g, g);
1344
0
            mp_clear(&bp->f);
1345
0
            mp_clear(&bp->g);
1346
0
            bp->counter = 0;
1347
            /* Move to free list */
1348
0
            rsabp->bp = bp->next;
1349
0
            bp->next = rsabp->free;
1350
0
            rsabp->free = bp;
1351
            /* In case there're threads waiting for new blinding
1352
             * value - notify 1 thread the value is ready
1353
             */
1354
0
            if (blindingParamsList.waitCount > 0) {
1355
0
                PR_NotifyCondVar(blindingParamsList.cVar);
1356
0
                blindingParamsList.waitCount--;
1357
0
            }
1358
0
            PZ_Unlock(blindingParamsList.lock);
1359
0
            return SECSuccess;
1360
0
#endif
1361
0
        }
1362
        /* We did not find a usable set of blinding params.  Can we make one? */
1363
        /* Find a free bp struct. */
1364
4.32k
        if ((bp = rsabp->free) != NULL) {
1365
            /* unlink this bp */
1366
4.32k
            rsabp->free = bp->next;
1367
4.32k
            bp->next = NULL;
1368
4.32k
            bpUnlinked = bp; /* In case we fail */
1369
1370
4.32k
            PZ_Unlock(blindingParamsList.lock);
1371
4.32k
            holdingLock = PR_FALSE;
1372
            /* generate blinding parameter values for the current thread */
1373
4.32k
            CHECK_SEC_OK(generate_blinding_params(key, f, g, n, modLen));
1374
1375
            /* put the blinding parameter values into cache */
1376
2.17k
            CHECK_MPI_OK(mp_init(&bp->f));
1377
2.17k
            CHECK_MPI_OK(mp_init(&bp->g));
1378
2.17k
            CHECK_MPI_OK(mp_copy(f, &bp->f));
1379
2.17k
            CHECK_MPI_OK(mp_copy(g, &bp->g));
1380
1381
            /* Put this at head of queue of usable params. */
1382
2.17k
            PZ_Lock(blindingParamsList.lock);
1383
2.17k
            holdingLock = PR_TRUE;
1384
2.17k
            (void)holdingLock;
1385
            /* initialize RSABlindingParamsStr */
1386
2.17k
            bp->counter = RSA_BLINDING_PARAMS_MAX_REUSE;
1387
2.17k
            bp->next = rsabp->bp;
1388
2.17k
            rsabp->bp = bp;
1389
2.17k
            bpUnlinked = NULL;
1390
            /* In case there're threads waiting for new blinding value
1391
             * just notify them the value is ready
1392
             */
1393
2.17k
            if (blindingParamsList.waitCount > 0) {
1394
0
                PR_NotifyAllCondVar(blindingParamsList.cVar);
1395
0
                blindingParamsList.waitCount = 0;
1396
0
            }
1397
2.17k
            PZ_Unlock(blindingParamsList.lock);
1398
2.17k
            return SECSuccess;
1399
2.17k
        }
1400
        /* Here, there are no usable blinding parameters available,
1401
         * and no free bp blocks, presumably because they're all
1402
         * actively having parameters generated for them.
1403
         * So, we need to wait here and not eat up CPU until some
1404
         * change happens.
1405
         */
1406
0
        blindingParamsList.waitCount++;
1407
0
        PR_WaitCondVar(blindingParamsList.cVar, PR_INTERVAL_NO_TIMEOUT);
1408
0
        PZ_Unlock(blindingParamsList.lock);
1409
0
        holdingLock = PR_FALSE;
1410
0
        (void)holdingLock;
1411
0
    } while (1);
1412
1413
2.15k
cleanup:
1414
    /* It is possible to reach this after the lock is already released.  */
1415
2.15k
    if (bpUnlinked) {
1416
2.15k
        if (!holdingLock) {
1417
2.15k
            PZ_Lock(blindingParamsList.lock);
1418
2.15k
            holdingLock = PR_TRUE;
1419
2.15k
        }
1420
2.15k
        bp = bpUnlinked;
1421
2.15k
        mp_clear(&bp->f);
1422
2.15k
        mp_clear(&bp->g);
1423
2.15k
        bp->counter = 0;
1424
        /* Must put the unlinked bp back on the free list */
1425
2.15k
        bp->next = rsabp->free;
1426
2.15k
        rsabp->free = bp;
1427
2.15k
    }
1428
2.15k
    if (holdingLock) {
1429
2.15k
        PZ_Unlock(blindingParamsList.lock);
1430
2.15k
    }
1431
2.15k
    if (err) {
1432
0
        MP_TO_SEC_ERROR(err);
1433
0
    }
1434
2.15k
    return SECFailure;
1435
2.15k
}
1436
1437
/*
1438
** Perform a raw private-key operation
1439
**  Length of input and output buffers are equal to key's modulus len.
1440
*/
1441
static SECStatus
1442
rsa_PrivateKeyOp(RSAPrivateKey *key,
1443
                 unsigned char *output,
1444
                 const unsigned char *input,
1445
                 PRBool check)
1446
4.35k
{
1447
4.35k
    unsigned int modLen;
1448
4.35k
    unsigned int offset;
1449
4.35k
    SECStatus rv = SECSuccess;
1450
4.35k
    mp_err err;
1451
4.35k
    mp_int n, c, m;
1452
4.35k
    mp_int f, g;
1453
4.35k
    if (!key || !output || !input) {
1454
0
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
1455
0
        return SECFailure;
1456
0
    }
1457
    /* check input out of range (needs to be in range [0..n-1]) */
1458
4.35k
    modLen = rsa_modulusLen(&key->modulus);
1459
4.35k
    if (modLen == 0 || key->publicExponent.len == 0 || key->privateExponent.len == 0) {
1460
0
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
1461
0
        return SECFailure;
1462
0
    }
1463
4.35k
    offset = (key->modulus.data[0] == 0) ? 1 : 0; /* may be leading 0 */
1464
4.35k
    if (memcmp(input, key->modulus.data + offset, modLen) >= 0) {
1465
36
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
1466
36
        return SECFailure;
1467
36
    }
1468
4.32k
    MP_DIGITS(&n) = 0;
1469
4.32k
    MP_DIGITS(&c) = 0;
1470
4.32k
    MP_DIGITS(&m) = 0;
1471
4.32k
    MP_DIGITS(&f) = 0;
1472
4.32k
    MP_DIGITS(&g) = 0;
1473
4.32k
    CHECK_MPI_OK(mp_init(&n));
1474
4.32k
    CHECK_MPI_OK(mp_init(&c));
1475
4.32k
    CHECK_MPI_OK(mp_init(&m));
1476
4.32k
    CHECK_MPI_OK(mp_init(&f));
1477
4.32k
    CHECK_MPI_OK(mp_init(&g));
1478
4.32k
    SECITEM_TO_MPINT(key->modulus, &n);
1479
4.32k
    OCTETS_TO_MPINT(input, &c, modLen);
1480
    /* If blinding, compute pre-image of ciphertext by multiplying by
1481
    ** blinding factor
1482
    */
1483
4.32k
    if (nssRSAUseBlinding) {
1484
4.32k
        CHECK_SEC_OK(get_blinding_params(key, &n, modLen, &f, &g));
1485
        /* c' = c*f mod n */
1486
2.17k
        CHECK_MPI_OK(mp_mulmod(&c, &f, &n, &c));
1487
2.17k
    }
1488
    /* Do the private key operation m = c**d mod n */
1489
2.17k
    if (key->prime1.len == 0 ||
1490
2.17k
        key->prime2.len == 0 ||
1491
2.17k
        key->exponent1.len == 0 ||
1492
2.17k
        key->exponent2.len == 0 ||
1493
2.17k
        key->coefficient.len == 0) {
1494
0
        CHECK_SEC_OK(rsa_PrivateKeyOpNoCRT(key, &m, &c, &n, modLen));
1495
2.17k
    } else if (check) {
1496
2.17k
        CHECK_SEC_OK(rsa_PrivateKeyOpCRTCheckedPubKey(key, &m, &c));
1497
2.17k
    } else {
1498
0
        CHECK_SEC_OK(rsa_PrivateKeyOpCRTNoCheck(key, &m, &c));
1499
0
    }
1500
    /* If blinding, compute post-image of plaintext by multiplying by
1501
    ** blinding factor
1502
    */
1503
47
    if (nssRSAUseBlinding) {
1504
        /* m = m'*g mod n */
1505
47
        CHECK_MPI_OK(mp_mulmod(&m, &g, &n, &m));
1506
47
    }
1507
47
    err = mp_to_fixlen_octets(&m, output, modLen);
1508
47
    if (err >= 0)
1509
47
        err = MP_OKAY;
1510
4.32k
cleanup:
1511
4.32k
    mp_clear(&n);
1512
4.32k
    mp_clear(&c);
1513
4.32k
    mp_clear(&m);
1514
4.32k
    mp_clear(&f);
1515
4.32k
    mp_clear(&g);
1516
4.32k
    if (err) {
1517
0
        MP_TO_SEC_ERROR(err);
1518
0
        rv = SECFailure;
1519
0
    }
1520
4.32k
    return rv;
1521
4.32k
}
1522
1523
SECStatus
1524
RSA_PrivateKeyOp(RSAPrivateKey *key,
1525
                 unsigned char *output,
1526
                 const unsigned char *input)
1527
3
{
1528
3
    return rsa_PrivateKeyOp(key, output, input, PR_FALSE);
1529
3
}
1530
1531
SECStatus
1532
RSA_PrivateKeyOpDoubleChecked(RSAPrivateKey *key,
1533
                              unsigned char *output,
1534
                              const unsigned char *input)
1535
4.35k
{
1536
4.35k
    return rsa_PrivateKeyOp(key, output, input, PR_TRUE);
1537
4.35k
}
1538
1539
SECStatus
1540
RSA_PrivateKeyCheck(const RSAPrivateKey *key)
1541
0
{
1542
0
    mp_int p, q, n, psub1, qsub1, e, d, d_p, d_q, qInv, res;
1543
0
    mp_err err = MP_OKAY;
1544
0
    SECStatus rv = SECSuccess;
1545
0
    MP_DIGITS(&p) = 0;
1546
0
    MP_DIGITS(&q) = 0;
1547
0
    MP_DIGITS(&n) = 0;
1548
0
    MP_DIGITS(&psub1) = 0;
1549
0
    MP_DIGITS(&qsub1) = 0;
1550
0
    MP_DIGITS(&e) = 0;
1551
0
    MP_DIGITS(&d) = 0;
1552
0
    MP_DIGITS(&d_p) = 0;
1553
0
    MP_DIGITS(&d_q) = 0;
1554
0
    MP_DIGITS(&qInv) = 0;
1555
0
    MP_DIGITS(&res) = 0;
1556
0
    CHECK_MPI_OK(mp_init(&p));
1557
0
    CHECK_MPI_OK(mp_init(&q));
1558
0
    CHECK_MPI_OK(mp_init(&n));
1559
0
    CHECK_MPI_OK(mp_init(&psub1));
1560
0
    CHECK_MPI_OK(mp_init(&qsub1));
1561
0
    CHECK_MPI_OK(mp_init(&e));
1562
0
    CHECK_MPI_OK(mp_init(&d));
1563
0
    CHECK_MPI_OK(mp_init(&d_p));
1564
0
    CHECK_MPI_OK(mp_init(&d_q));
1565
0
    CHECK_MPI_OK(mp_init(&qInv));
1566
0
    CHECK_MPI_OK(mp_init(&res));
1567
1568
0
    if (!key->modulus.data || !key->prime1.data || !key->prime2.data ||
1569
0
        !key->publicExponent.data || !key->privateExponent.data ||
1570
0
        !key->exponent1.data || !key->exponent2.data ||
1571
0
        !key->coefficient.data) {
1572
        /* call RSA_PopulatePrivateKey first, if the application wishes to
1573
         * recover these parameters */
1574
0
        err = MP_BADARG;
1575
0
        goto cleanup;
1576
0
    }
1577
1578
0
    SECITEM_TO_MPINT(key->modulus, &n);
1579
0
    SECITEM_TO_MPINT(key->prime1, &p);
1580
0
    SECITEM_TO_MPINT(key->prime2, &q);
1581
0
    SECITEM_TO_MPINT(key->publicExponent, &e);
1582
0
    SECITEM_TO_MPINT(key->privateExponent, &d);
1583
0
    SECITEM_TO_MPINT(key->exponent1, &d_p);
1584
0
    SECITEM_TO_MPINT(key->exponent2, &d_q);
1585
0
    SECITEM_TO_MPINT(key->coefficient, &qInv);
1586
    /* p and q must be distinct. */
1587
0
    if (mp_cmp(&p, &q) == 0) {
1588
0
        rv = SECFailure;
1589
0
        goto cleanup;
1590
0
    }
1591
0
#define VERIFY_MPI_EQUAL(m1, m2) \
1592
0
    if (mp_cmp(m1, m2) != 0) {   \
1593
0
        rv = SECFailure;         \
1594
0
        goto cleanup;            \
1595
0
    }
1596
0
#define VERIFY_MPI_EQUAL_1(m)  \
1597
0
    if (mp_cmp_d(m, 1) != 0) { \
1598
0
        rv = SECFailure;       \
1599
0
        goto cleanup;          \
1600
0
    }
1601
    /* n == p * q */
1602
0
    CHECK_MPI_OK(mp_mul(&p, &q, &res));
1603
0
    VERIFY_MPI_EQUAL(&res, &n);
1604
    /* gcd(e, p-1) == 1 */
1605
0
    CHECK_MPI_OK(mp_sub_d(&p, 1, &psub1));
1606
0
    CHECK_MPI_OK(mp_gcd(&e, &psub1, &res));
1607
0
    VERIFY_MPI_EQUAL_1(&res);
1608
    /* gcd(e, q-1) == 1 */
1609
0
    CHECK_MPI_OK(mp_sub_d(&q, 1, &qsub1));
1610
0
    CHECK_MPI_OK(mp_gcd(&e, &qsub1, &res));
1611
0
    VERIFY_MPI_EQUAL_1(&res);
1612
    /* d*e == 1 mod p-1 */
1613
0
    CHECK_MPI_OK(mp_mulmod(&d, &e, &psub1, &res));
1614
0
    VERIFY_MPI_EQUAL_1(&res);
1615
    /* d*e == 1 mod q-1 */
1616
0
    CHECK_MPI_OK(mp_mulmod(&d, &e, &qsub1, &res));
1617
0
    VERIFY_MPI_EQUAL_1(&res);
1618
    /* d_p == d mod p-1 */
1619
0
    CHECK_MPI_OK(mp_mod(&d, &psub1, &res));
1620
0
    VERIFY_MPI_EQUAL(&res, &d_p);
1621
    /* d_q == d mod q-1 */
1622
0
    CHECK_MPI_OK(mp_mod(&d, &qsub1, &res));
1623
0
    VERIFY_MPI_EQUAL(&res, &d_q);
1624
    /* q * q**-1 == 1 mod p */
1625
0
    CHECK_MPI_OK(mp_mulmod(&q, &qInv, &p, &res));
1626
0
    VERIFY_MPI_EQUAL_1(&res);
1627
1628
0
cleanup:
1629
0
    mp_clear(&n);
1630
0
    mp_clear(&p);
1631
0
    mp_clear(&q);
1632
0
    mp_clear(&psub1);
1633
0
    mp_clear(&qsub1);
1634
0
    mp_clear(&e);
1635
0
    mp_clear(&d);
1636
0
    mp_clear(&d_p);
1637
0
    mp_clear(&d_q);
1638
0
    mp_clear(&qInv);
1639
0
    mp_clear(&res);
1640
0
    if (err) {
1641
0
        MP_TO_SEC_ERROR(err);
1642
0
        rv = SECFailure;
1643
0
    }
1644
0
    return rv;
1645
0
}
1646
1647
SECStatus
1648
RSA_Init(void)
1649
19.3k
{
1650
19.3k
    if (PR_CallOnce(&coBPInit, init_blinding_params_list) != PR_SUCCESS) {
1651
0
        PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
1652
0
        return SECFailure;
1653
0
    }
1654
19.3k
    return SECSuccess;
1655
19.3k
}
1656
1657
/* cleanup at shutdown */
1658
void
1659
RSA_Cleanup(void)
1660
19.3k
{
1661
19.3k
    blindingParams *bp = NULL;
1662
19.3k
    if (!coBPInit.initialized)
1663
0
        return;
1664
1665
23.6k
    while (!PR_CLIST_IS_EMPTY(&blindingParamsList.head)) {
1666
4.32k
        RSABlindingParams *rsabp =
1667
4.32k
            (RSABlindingParams *)PR_LIST_HEAD(&blindingParamsList.head);
1668
4.32k
        PR_REMOVE_LINK(&rsabp->link);
1669
        /* clear parameters cache */
1670
6.49k
        while (rsabp->bp != NULL) {
1671
2.17k
            bp = rsabp->bp;
1672
2.17k
            rsabp->bp = rsabp->bp->next;
1673
2.17k
            mp_clear(&bp->f);
1674
2.17k
            mp_clear(&bp->g);
1675
2.17k
        }
1676
4.32k
        SECITEM_ZfreeItem(&rsabp->modulus, PR_FALSE);
1677
4.32k
        PORT_Free(rsabp);
1678
4.32k
    }
1679
1680
19.3k
    if (blindingParamsList.cVar) {
1681
19.3k
        PR_DestroyCondVar(blindingParamsList.cVar);
1682
19.3k
        blindingParamsList.cVar = NULL;
1683
19.3k
    }
1684
1685
19.3k
    if (blindingParamsList.lock) {
1686
19.3k
        SKIP_AFTER_FORK(PZ_DestroyLock(blindingParamsList.lock));
1687
19.3k
        blindingParamsList.lock = NULL;
1688
19.3k
    }
1689
1690
19.3k
    coBPInit.initialized = 0;
1691
19.3k
    coBPInit.inProgress = 0;
1692
19.3k
    coBPInit.status = 0;
1693
19.3k
}
1694
1695
/*
1696
 * need a central place for this function to free up all the memory that
1697
 * free_bl may have allocated along the way. Currently only RSA does this,
1698
 * so I've put it here for now.
1699
 */
1700
void
1701
BL_Cleanup(void)
1702
19.3k
{
1703
19.3k
    RSA_Cleanup();
1704
19.3k
}
1705
1706
PRBool bl_parentForkedAfterC_Initialize;
1707
1708
/*
1709
 * Set fork flag so it can be tested in SKIP_AFTER_FORK on relevant platforms.
1710
 */
1711
void
1712
BL_SetForkState(PRBool forked)
1713
38.6k
{
1714
38.6k
    bl_parentForkedAfterC_Initialize = forked;
1715
38.6k
}
/home/XXXX-2/Sources/nssgit/nss/lib/freebl/rsapkcs.c
Line
Count
Source (jump to first uncovered line)
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
3
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
/*
6
 * RSA PKCS#1 v2.1 (RFC 3447) operations
7
 */
8
9
#ifdef FREEBL_NO_DEPEND
10
#include "stubs.h"
11
#endif
12
13
#include "secerr.h"
14
15
#include "blapi.h"
16
#include "secitem.h"
17
#include "blapii.h"
18
19
11.8k
#define RSA_BLOCK_MIN_PAD_LEN 8
20
6.57k
#define RSA_BLOCK_FIRST_OCTET 0x00
21
3.42k
#define RSA_BLOCK_PRIVATE_PAD_OCTET 0xff
22
3.21k
#define RSA_BLOCK_AFTER_PAD_OCTET 0x00
23
24
/*
25
 * RSA block types
26
 *
27
 * The values of RSA_BlockPrivate and RSA_BlockPublic are fixed.
28
 * The value of RSA_BlockRaw isn't fixed by definition, but we are keeping
29
 * the value that NSS has been using in the past.
30
 */
31
typedef enum {
32
    RSA_BlockPrivate = 1, /* pad for a private-key operation */
33
    RSA_BlockPublic = 2,  /* pad for a public-key operation */
34
    RSA_BlockRaw = 4      /* simply justify the block appropriately */
35
} RSA_BlockType;
36
37
/* Needed for RSA-PSS functions */
38
static const unsigned char eightZeros[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
39
40
/* Constant time comparison of a single byte.
41
 * Returns 1 iff a == b, otherwise returns 0.
42
 * Note: For ranges of bytes, use constantTimeCompare.
43
 */
44
static unsigned char
45
constantTimeEQ8(unsigned char a, unsigned char b)
46
0
{
47
0
    unsigned char c = ~((a - b) | (b - a));
48
0
    c >>= 7;
49
0
    return c;
50
0
}
51
52
/* Constant time comparison of a range of bytes.
53
 * Returns 1 iff len bytes of a are identical to len bytes of b, otherwise
54
 * returns 0.
55
 */
56
static unsigned char
57
constantTimeCompare(const unsigned char *a,
58
                    const unsigned char *b,
59
                    unsigned int len)
60
0
{
61
0
    unsigned char tmp = 0;
62
0
    unsigned int i;
63
0
    for (i = 0; i < len; ++i, ++a, ++b)
64
0
        tmp |= *a ^ *b;
65
0
    return constantTimeEQ8(0x00, tmp);
66
0
}
67
68
/* Constant time conditional.
69
 * Returns a if c is 1, or b if c is 0. The result is undefined if c is
70
 * not 0 or 1.
71
 */
72
static unsigned int
73
constantTimeCondition(unsigned int c,
74
                      unsigned int a,
75
                      unsigned int b)
76
0
{
77
0
    return (~(c - 1) & a) | ((c - 1) & b);
78
0
}
79
80
static unsigned int
81
rsa_modulusLen(SECItem *modulus)
82
13.2k
{
83
13.2k
    if (modulus->len == 0) {
84
146
        return 0;
85
146
    }
86
87
13.1k
    unsigned char byteZero = modulus->data[0];
88
13.1k
    unsigned int modLen = modulus->len - !byteZero;
89
13.1k
    return modLen;
90
13.2k
}
91
92
static unsigned int
93
rsa_modulusBits(SECItem *modulus)
94
3.46k
{
95
3.46k
    if (modulus->len == 0) {
96
59
        return 0;
97
59
    }
98
99
3.40k
    unsigned char byteZero = modulus->data[0];
100
3.40k
    unsigned int numBits = (modulus->len - 1) * 8;
101
102
3.40k
    if (byteZero == 0 && modulus->len == 1) {
103
36
        return 0;
104
36
    }
105
106
3.37k
    if (byteZero == 0) {
107
63
        numBits -= 8;
108
63
        byteZero = modulus->data[1];
109
63
    }
110
111
24.8k
    while (byteZero > 0) {
112
21.4k
        numBits++;
113
21.4k
        byteZero >>= 1;
114
21.4k
    }
115
116
3.37k
    return numBits;
117
3.40k
}
118
119
/*
120
 * Format one block of data for public/private key encryption using
121
 * the rules defined in PKCS #1.
122
 */
123
static unsigned char *
124
rsa_FormatOneBlock(unsigned modulusLen,
125
                   RSA_BlockType blockType,
126
                   SECItem *data)
127
3.21k
{
128
3.21k
    unsigned char *block;
129
3.21k
    unsigned char *bp;
130
3.21k
    unsigned int padLen;
131
3.21k
    unsigned int i, j;
132
3.21k
    SECStatus rv;
133
134
3.21k
    block = (unsigned char *)PORT_Alloc(modulusLen);
135
3.21k
    if (block == NULL)
136
0
        return NULL;
137
138
3.21k
    bp = block;
139
140
    /*
141
     * All RSA blocks start with two octets:
142
     *  0x00 || BlockType
143
     */
144
3.21k
    *bp++ = RSA_BLOCK_FIRST_OCTET;
145
3.21k
    *bp++ = (unsigned char)blockType;
146
147
3.21k
    switch (blockType) {
148
149
        /*
150
         * Blocks intended for private-key operation.
151
         */
152
3.21k
        case RSA_BlockPrivate: /* preferred method */
153
            /*
154
             * 0x00 || BT || Pad || 0x00 || ActualData
155
             *   1      1   padLen    1      data->len
156
             * padLen must be at least RSA_BLOCK_MIN_PAD_LEN (8) bytes.
157
             * Pad is either all 0x00 or all 0xff bytes, depending on blockType.
158
             */
159
3.21k
            padLen = modulusLen - data->len - 3;
160
3.21k
            PORT_Assert(padLen >= RSA_BLOCK_MIN_PAD_LEN);
161
3.21k
            if (padLen < RSA_BLOCK_MIN_PAD_LEN) {
162
0
                PORT_ZFree(block, modulusLen);
163
0
                return NULL;
164
0
            }
165
3.21k
            PORT_Memset(bp, RSA_BLOCK_PRIVATE_PAD_OCTET, padLen);
166
3.21k
            bp += padLen;
167
3.21k
            *bp++ = RSA_BLOCK_AFTER_PAD_OCTET;
168
3.21k
            PORT_Memcpy(bp, data->data, data->len);
169
3.21k
            break;
170
171
        /*
172
         * Blocks intended for public-key operation.
173
         */
174
0
        case RSA_BlockPublic:
175
            /*
176
             * 0x00 || BT || Pad || 0x00 || ActualData
177
             *   1      1   padLen    1      data->len
178
             * Pad is 8 or more non-zero random bytes.
179
             *
180
             * Build the block left to right.
181
             * Fill the entire block from Pad to the end with random bytes.
182
             * Use the bytes after Pad as a supply of extra random bytes from
183
             * which to find replacements for the zero bytes in Pad.
184
             * If we need more than that, refill the bytes after Pad with
185
             * new random bytes as necessary.
186
             */
187
188
0
            padLen = modulusLen - (data->len + 3);
189
0
            PORT_Assert(padLen >= RSA_BLOCK_MIN_PAD_LEN);
190
0
            if (padLen < RSA_BLOCK_MIN_PAD_LEN) {
191
0
                PORT_ZFree(block, modulusLen);
192
0
                return NULL;
193
0
            }
194
0
            j = modulusLen - 2;
195
0
            rv = RNG_GenerateGlobalRandomBytes(bp, j);
196
0
            if (rv == SECSuccess) {
197
0
                for (i = 0; i < padLen;) {
198
0
                    unsigned char repl;
199
                    /* Pad with non-zero random data. */
200
0
                    if (bp[i] != RSA_BLOCK_AFTER_PAD_OCTET) {
201
0
                        ++i;
202
0
                        continue;
203
0
                    }
204
0
                    if (j <= padLen) {
205
0
                        rv = RNG_GenerateGlobalRandomBytes(bp + padLen,
206
0
                                                           modulusLen - (2 + padLen));
207
0
                        if (rv != SECSuccess)
208
0
                            break;
209
0
                        j = modulusLen - 2;
210
0
                    }
211
0
                    do {
212
0
                        repl = bp[--j];
213
0
                    } while (repl == RSA_BLOCK_AFTER_PAD_OCTET && j > padLen);
214
0
                    if (repl != RSA_BLOCK_AFTER_PAD_OCTET) {
215
0
                        bp[i++] = repl;
216
0
                    }
217
0
                }
218
0
            }
219
0
            if (rv != SECSuccess) {
220
0
                PORT_ZFree(block, modulusLen);
221
0
                PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
222
0
                return NULL;
223
0
            }
224
0
            bp += padLen;
225
0
            *bp++ = RSA_BLOCK_AFTER_PAD_OCTET;
226
0
            PORT_Memcpy(bp, data->data, data->len);
227
0
            break;
228
229
0
        default:
230
0
            PORT_Assert(0);
231
0
            PORT_ZFree(block, modulusLen);
232
0
            return NULL;
233
3.21k
    }
234
235
3.21k
    return block;
236
3.21k
}
237
238
/* modulusLen has to be larger than RSA_BLOCK_MIN_PAD_LEN + 3, and data has to be smaller than modulus - (RSA_BLOCK_MIN_PAD_LEN + 3) */
239
static SECStatus
240
rsa_FormatBlock(SECItem *result,
241
                unsigned modulusLen,
242
                RSA_BlockType blockType,
243
                SECItem *data)
244
6.58k
{
245
6.58k
    switch (blockType) {
246
3.43k
        case RSA_BlockPrivate:
247
3.43k
        case RSA_BlockPublic:
248
            /*
249
             * 0x00 || BT || Pad || 0x00 || ActualData
250
             *
251
             * The "3" below is the first octet + the second octet + the 0x00
252
             * octet that always comes just before the ActualData.
253
             */
254
3.43k
            if (modulusLen < (3 + RSA_BLOCK_MIN_PAD_LEN) || data->len > (modulusLen - (3 + RSA_BLOCK_MIN_PAD_LEN))) {
255
220
                return SECFailure;
256
220
            }
257
3.21k
            result->data = rsa_FormatOneBlock(modulusLen, blockType, data);
258
3.21k
            if (result->data == NULL) {
259
0
                result->len = 0;
260
0
                return SECFailure;
261
0
            }
262
3.21k
            result->len = modulusLen;
263
264
3.21k
            break;
265
266
3.14k
        case RSA_BlockRaw:
267
            /*
268
             * Pad || ActualData
269
             * Pad is zeros. The application is responsible for recovering
270
             * the actual data.
271
             */
272
3.14k
            if (data->len > modulusLen) {
273
112
                return SECFailure;
274
112
            }
275
3.03k
            result->data = (unsigned char *)PORT_ZAlloc(modulusLen);
276
3.03k
            result->len = modulusLen;
277
3.03k
            PORT_Memcpy(result->data + (modulusLen - data->len),
278
3.03k
                        data->data, data->len);
279
3.03k
            break;
280
281
0
        default:
282
0
            PORT_Assert(0);
283
0
            result->data = NULL;
284
0
            result->len = 0;
285
0
            return SECFailure;
286
6.58k
    }
287
288
6.24k
    return SECSuccess;
289
6.58k
}
290
291
/*
292
 * Mask generation function MGF1 as defined in PKCS #1 v2.1 / RFC 3447.
293
 */
294
static SECStatus
295
MGF1(HASH_HashType hashAlg,
296
     unsigned char *mask,
297
     unsigned int maskLen,
298
     const unsigned char *mgfSeed,
299
     unsigned int mgfSeedLen)
300
1.77k
{
301
1.77k
    unsigned int digestLen;
302
1.77k
    PRUint32 counter;
303
1.77k
    PRUint32 rounds;
304
1.77k
    unsigned char *tempHash;
305
1.77k
    unsigned char *temp;
306
1.77k
    const SECHashObject *hash;
307
1.77k
    void *hashContext;
308
1.77k
    unsigned char C[4];
309
1.77k
    SECStatus rv = SECSuccess;
310
311
1.77k
    hash = HASH_GetRawHashObject(hashAlg);
312
1.77k
    if (hash == NULL) {
313
0
        return SECFailure;
314
0
    }
315
316
1.77k
    hashContext = (*hash->create)();
317
1.77k
    rounds = (maskLen + hash->length - 1) / hash->length;
318
28.5k
    for (counter = 0; counter < rounds; counter++) {
319
26.7k
        C[0] = (unsigned char)((counter >> 24) & 0xff);
320
26.7k
        C[1] = (unsigned char)((counter >> 16) & 0xff);
321
26.7k
        C[2] = (unsigned char)((counter >> 8) & 0xff);
322
26.7k
        C[3] = (unsigned char)(counter & 0xff);
323
324
        /* This could be optimized when the clone functions in
325
         * rawhash.c are implemented. */
326
26.7k
        (*hash->begin)(hashContext);
327
26.7k
        (*hash->update)(hashContext, mgfSeed, mgfSeedLen);
328
26.7k
        (*hash->update)(hashContext, C, sizeof C);
329
330
26.7k
        tempHash = mask + counter * hash->length;
331
26.7k
        if (counter != (rounds - 1)) {
332
25.0k
            (*hash->end)(hashContext, tempHash, &digestLen, hash->length);
333
25.0k
        } else { /* we're in the last round and need to cut the hash */
334
1.77k
            temp = (unsigned char *)PORT_Alloc(hash->length);
335
1.77k
            if (!temp) {
336
0
                rv = SECFailure;
337
0
                goto done;
338
0
            }
339
1.77k
            (*hash->end)(hashContext, temp, &digestLen, hash->length);
340
1.77k
            PORT_Memcpy(tempHash, temp, maskLen - counter * hash->length);
341
1.77k
            PORT_Free(temp);
342
1.77k
        }
343
26.7k
    }
344
345
1.77k
done:
346
1.77k
    (*hash->destroy)(hashContext, PR_TRUE);
347
1.77k
    return rv;
348
1.77k
}
349
350
/* XXX Doesn't set error code */
351
SECStatus
352
RSA_SignRaw(RSAPrivateKey *key,
353
            unsigned char *output,
354
            unsigned int *outputLen,
355
            unsigned int maxOutputLen,
356
            const unsigned char *data,
357
            unsigned int dataLen)
358
498
{
359
498
    SECStatus rv = SECSuccess;
360
498
    unsigned int modulusLen = rsa_modulusLen(&key->modulus);
361
498
    SECItem formatted;
362
498
    SECItem unformatted;
363
364
498
    if (maxOutputLen < modulusLen)
365
0
        return SECFailure;
366
367
498
    unformatted.len = dataLen;
368
498
    unformatted.data = (unsigned char *)data;
369
498
    formatted.data = NULL;
370
498
    rv = rsa_FormatBlock(&formatted, modulusLen, RSA_BlockRaw, &unformatted);
371
498
    if (rv != SECSuccess)
372
83
        goto done;
373
374
415
    rv = RSA_PrivateKeyOpDoubleChecked(key, output, formatted.data);
375
415
    *outputLen = modulusLen;
376
377
498
done:
378
498
    if (formatted.data != NULL)
379
415
        PORT_ZFree(formatted.data, modulusLen);
380
498
    return rv;
381
415
}
382
383
/* XXX Doesn't set error code */
384
SECStatus
385
RSA_CheckSignRaw(RSAPublicKey *key,
386
                 const unsigned char *sig,
387
                 unsigned int sigLen,
388
                 const unsigned char *hash,
389
                 unsigned int hashLen)
390
68
{
391
68
    SECStatus rv;
392
68
    unsigned int modulusLen = rsa_modulusLen(&key->modulus);
393
68
    unsigned char *buffer;
394
395
68
    if (sigLen != modulusLen)
396
14
        goto failure;
397
54
    if (hashLen > modulusLen)
398
13
        goto failure;
399
400
41
    buffer = (unsigned char *)PORT_Alloc(modulusLen + 1);
401
41
    if (!buffer)
402
0
        goto failure;
403
404
41
    rv = RSA_PublicKeyOp(key, buffer, sig);
405
41
    if (rv != SECSuccess)
406
13
        goto loser;
407
408
    /*
409
     * make sure we get the same results
410
     */
411
    /* XXX(rsleevi): Constant time */
412
    /* NOTE: should we verify the leading zeros? */
413
28
    if (PORT_Memcmp(buffer + (modulusLen - hashLen), hash, hashLen) != 0)
414
18
        goto loser;
415
416
10
    PORT_Free(buffer);
417
10
    return SECSuccess;
418
419
31
loser:
420
31
    PORT_Free(buffer);
421
58
failure:
422
58
    return SECFailure;
423
31
}
424
425
/* XXX Doesn't set error code */
426
SECStatus
427
RSA_CheckSignRecoverRaw(RSAPublicKey *key,
428
                        unsigned char *data,
429
                        unsigned int *dataLen,
430
                        unsigned int maxDataLen,
431
                        const unsigned char *sig,
432
                        unsigned int sigLen)
433
56
{
434
56
    SECStatus rv;
435
56
    unsigned int modulusLen = rsa_modulusLen(&key->modulus);
436
437
56
    if (sigLen != modulusLen)
438
11
        goto failure;
439
45
    if (maxDataLen < modulusLen)
440
0
        goto failure;
441
442
45
    rv = RSA_PublicKeyOp(key, data, sig);
443
45
    if (rv != SECSuccess)
444
19
        goto failure;
445
446
26
    *dataLen = modulusLen;
447
26
    return SECSuccess;
448
449
30
failure:
450
30
    return SECFailure;
451
45
}
452
453
/* XXX Doesn't set error code */
454
SECStatus
455
RSA_EncryptRaw(RSAPublicKey *key,
456
               unsigned char *output,
457
               unsigned int *outputLen,
458
               unsigned int maxOutputLen,
459
               const unsigned char *input,
460
               unsigned int inputLen)
461
2.64k
{
462
2.64k
    SECStatus rv;
463
2.64k
    unsigned int modulusLen = rsa_modulusLen(&key->modulus);
464
2.64k
    SECItem formatted;
465
2.64k
    SECItem unformatted;
466
467
2.64k
    formatted.data = NULL;
468
2.64k
    if (maxOutputLen < modulusLen)
469
0
        goto failure;
470
471
2.64k
    unformatted.len = inputLen;
472
2.64k
    unformatted.data = (unsigned char *)input;
473
2.64k
    formatted.data = NULL;
474
2.64k
    rv = rsa_FormatBlock(&formatted, modulusLen, RSA_BlockRaw, &unformatted);
475
2.64k
    if (rv != SECSuccess)
476
29
        goto failure;
477
478
2.61k
    rv = RSA_PublicKeyOp(key, output, formatted.data);
479
2.61k
    if (rv != SECSuccess)
480
129
        goto failure;
481
482
2.48k
    PORT_ZFree(formatted.data, modulusLen);
483
2.48k
    *outputLen = modulusLen;
484
2.48k
    return SECSuccess;
485
486
158
failure:
487
158
    if (formatted.data != NULL)
488
129
        PORT_ZFree(formatted.data, modulusLen);
489
158
    return SECFailure;
490
2.61k
}
491
492
/* XXX Doesn't set error code */
493
SECStatus
494
RSA_DecryptRaw(RSAPrivateKey *key,
495
               unsigned char *output,
496
               unsigned int *outputLen,
497
               unsigned int maxOutputLen,
498
               const unsigned char *input,
499
               unsigned int inputLen)
500
100
{
501
100
    SECStatus rv;
502
100
    unsigned int modulusLen = rsa_modulusLen(&key->modulus);
503
504
100
    if (modulusLen > maxOutputLen)
505
0
        goto failure;
506
100
    if (inputLen != modulusLen)
507
97
        goto failure;
508
509
3
    rv = RSA_PrivateKeyOp(key, output, input);
510
3
    if (rv != SECSuccess)
511
3
        goto failure;
512
513
0
    *outputLen = modulusLen;
514
0
    return SECSuccess;
515
516
100
failure:
517
100
    return SECFailure;
518
3
}
519
520
/*
521
 * Decodes an EME-OAEP encoded block, validating the encoding in constant
522
 * time.
523
 * Described in RFC 3447, section 7.1.2.
524
 * input contains the encoded block, after decryption.
525
 * label is the optional value L that was associated with the message.
526
 * On success, the original message and message length will be stored in
527
 * output and outputLen.
528
 */
529
static SECStatus
530
eme_oaep_decode(unsigned char *output,
531
                unsigned int *outputLen,
532
                unsigned int maxOutputLen,
533
                const unsigned char *input,
534
                unsigned int inputLen,
535
                HASH_HashType hashAlg,
536
                HASH_HashType maskHashAlg,
537
                const unsigned char *label,
538
                unsigned int labelLen)
539
2
{
540
2
    const SECHashObject *hash;
541
2
    void *hashContext;
542
2
    SECStatus rv = SECFailure;
543
2
    unsigned char labelHash[HASH_LENGTH_MAX];
544
2
    unsigned int i;
545
2
    unsigned int maskLen;
546
2
    unsigned int paddingOffset;
547
2
    unsigned char *mask = NULL;
548
2
    unsigned char *tmpOutput = NULL;
549
2
    unsigned char isGood;
550
2
    unsigned char foundPaddingEnd;
551
552
2
    hash = HASH_GetRawHashObject(hashAlg);
553
554
    /* 1.c */
555
2
    if (inputLen < (hash->length * 2) + 2) {
556
2
        PORT_SetError(SEC_ERROR_INPUT_LEN);
557
2
        return SECFailure;
558
2
    }
559
560
    /* Step 3.a - Generate lHash */
561
0
    hashContext = (*hash->create)();
562
0
    if (hashContext == NULL) {
563
0
        PORT_SetError(SEC_ERROR_NO_MEMORY);
564
0
        return SECFailure;
565
0
    }
566
0
    (*hash->begin)(hashContext);
567
0
    if (labelLen > 0)
568
0
        (*hash->update)(hashContext, label, labelLen);
569
0
    (*hash->end)(hashContext, labelHash, &i, sizeof(labelHash));
570
0
    (*hash->destroy)(hashContext, PR_TRUE);
571
572
0
    tmpOutput = (unsigned char *)PORT_Alloc(inputLen);
573
0
    if (tmpOutput == NULL) {
574
0
        PORT_SetError(SEC_ERROR_NO_MEMORY);
575
0
        goto done;
576
0
    }
577
578
0
    maskLen = inputLen - hash->length - 1;
579
0
    mask = (unsigned char *)PORT_Alloc(maskLen);
580
0
    if (mask == NULL) {
581
0
        PORT_SetError(SEC_ERROR_NO_MEMORY);
582
0
        goto done;
583
0
    }
584
585
0
    PORT_Memcpy(tmpOutput, input, inputLen);
586
587
    /* 3.c - Generate seedMask */
588
0
    MGF1(maskHashAlg, mask, hash->length, &tmpOutput[1 + hash->length],
589
0
         inputLen - hash->length - 1);
590
    /* 3.d - Unmask seed */
591
0
    for (i = 0; i < hash->length; ++i)
592
0
        tmpOutput[1 + i] ^= mask[i];
593
594
    /* 3.e - Generate dbMask */
595
0
    MGF1(maskHashAlg, mask, maskLen, &tmpOutput[1], hash->length);
596
    /* 3.f - Unmask DB */
597
0
    for (i = 0; i < maskLen; ++i)
598
0
        tmpOutput[1 + hash->length + i] ^= mask[i];
599
600
    /* 3.g - Compare Y, lHash, and PS in constant time
601
     * Warning: This code is timing dependent and must not disclose which of
602
     * these were invalid.
603
     */
604
0
    paddingOffset = 0;
605
0
    isGood = 1;
606
0
    foundPaddingEnd = 0;
607
608
    /* Compare Y */
609
0
    isGood &= constantTimeEQ8(0x00, tmpOutput[0]);
610
611
    /* Compare lHash and lHash' */
612
0
    isGood &= constantTimeCompare(&labelHash[0],
613
0
                                  &tmpOutput[1 + hash->length],
614
0
                                  hash->length);
615
616
    /* Compare that the padding is zero or more zero octets, followed by a
617
     * 0x01 octet */
618
0
    for (i = 1 + (hash->length * 2); i < inputLen; ++i) {
619
0
        unsigned char isZero = constantTimeEQ8(0x00, tmpOutput[i]);
620
0
        unsigned char isOne = constantTimeEQ8(0x01, tmpOutput[i]);
621
        /* non-constant time equivalent:
622
         * if (tmpOutput[i] == 0x01 && !foundPaddingEnd)
623
         *     paddingOffset = i;
624
         */
625
0
        paddingOffset = constantTimeCondition(isOne & ~foundPaddingEnd, i,
626
0
                                              paddingOffset);
627
        /* non-constant time equivalent:
628
         * if (tmpOutput[i] == 0x01)
629
         *    foundPaddingEnd = true;
630
         *
631
         * Note: This may yield false positives, as it will be set whenever
632
         * a 0x01 byte is encountered. If there was bad padding (eg:
633
         * 0x03 0x02 0x01), foundPaddingEnd will still be set to true, and
634
         * paddingOffset will still be set to 2.
635
         */
636
0
        foundPaddingEnd = constantTimeCondition(isOne, 1, foundPaddingEnd);
637
        /* non-constant time equivalent:
638
         * if (tmpOutput[i] != 0x00 && tmpOutput[i] != 0x01 &&
639
         *     !foundPaddingEnd) {
640
         *    isGood = false;
641
         * }
642
         *
643
         * Note: This may yield false positives, as a message (and padding)
644
         * that is entirely zeros will result in isGood still being true. Thus
645
         * it's necessary to check foundPaddingEnd is positive below.
646
         */
647
0
        isGood = constantTimeCondition(~foundPaddingEnd & ~isZero, 0, isGood);
648
0
    }
649
650
    /* While both isGood and foundPaddingEnd may have false positives, they
651
     * cannot BOTH have false positives. If both are not true, then an invalid
652
     * message was received. Note, this comparison must still be done in constant
653
     * time so as not to leak either condition.
654
     */
655
0
    if (!(isGood & foundPaddingEnd)) {
656
0
        PORT_SetError(SEC_ERROR_BAD_DATA);
657
0
        goto done;
658
0
    }
659
660
    /* End timing dependent code */
661
662
0
    ++paddingOffset; /* Skip the 0x01 following the end of PS */
663
664
0
    *outputLen = inputLen - paddingOffset;
665
0
    if (*outputLen > maxOutputLen) {
666
0
        PORT_SetError(SEC_ERROR_OUTPUT_LEN);
667
0
        goto done;
668
0
    }
669
670
0
    if (*outputLen)
671
0
        PORT_Memcpy(output, &tmpOutput[paddingOffset], *outputLen);
672
0
    rv = SECSuccess;
673
674
0
done:
675
0
    if (mask)
676
0
        PORT_ZFree(mask, maskLen);
677
0
    if (tmpOutput)
678
0
        PORT_ZFree(tmpOutput, inputLen);
679
0
    return rv;
680
0
}
681
682
/*
683
 * Generate an EME-OAEP encoded block for encryption
684
 * Described in RFC 3447, section 7.1.1
685
 * We use input instead of M for the message to be encrypted
686
 * label is the optional value L to be associated with the message.
687
 */
688
static SECStatus
689
eme_oaep_encode(unsigned char *em,
690
                unsigned int emLen,
691
                const unsigned char *input,
692
                unsigned int inputLen,
693
                HASH_HashType hashAlg,
694
                HASH_HashType maskHashAlg,
695
                const unsigned char *label,
696
                unsigned int labelLen,
697
                const unsigned char *seed,
698
                unsigned int seedLen)
699
603
{
700
603
    const SECHashObject *hash;
701
603
    void *hashContext;
702
603
    SECStatus rv;
703
603
    unsigned char *mask;
704
603
    unsigned int reservedLen;
705
603
    unsigned int dbMaskLen;
706
603
    unsigned int i;
707
708
603
    hash = HASH_GetRawHashObject(hashAlg);
709
603
    PORT_Assert(seed == NULL || seedLen == hash->length);
710
711
    /* Step 1.b */
712
603
    reservedLen = (2 * hash->length) + 2;
713
603
    if (emLen < reservedLen || inputLen > (emLen - reservedLen)) {
714
31
        PORT_SetError(SEC_ERROR_INPUT_LEN);
715
31
        return SECFailure;
716
31
    }
717
718
    /*
719
     * From RFC 3447, Section 7.1
720
     *                      +----------+---------+-------+
721
     *                 DB = |  lHash   |    PS   |   M   |
722
     *                      +----------+---------+-------+
723
     *                                     |
724
     *           +----------+              V
725
     *           |   seed   |--> MGF ---> xor
726
     *           +----------+              |
727
     *                 |                   |
728
     *        +--+     V                   |
729
     *        |00|    xor <----- MGF <-----|
730
     *        +--+     |                   |
731
     *          |      |                   |
732
     *          V      V                   V
733
     *        +--+----------+----------------------------+
734
     *  EM =  |00|maskedSeed|          maskedDB          |
735
     *        +--+----------+----------------------------+
736
     *
737
     * We use mask to hold the result of the MGF functions, and all other
738
     * values are generated in their final resting place.
739
     */
740
572
    *em = 0x00;
741
742
    /* Step 2.a - Generate lHash */
743
572
    hashContext = (*hash->create)();
744
572
    if (hashContext == NULL) {
745
0
        PORT_SetError(SEC_ERROR_NO_MEMORY);
746
0
        return SECFailure;
747
0
    }
748
572
    (*hash->begin)(hashContext);
749
572
    if (labelLen > 0)
750
298
        (*hash->update)(hashContext, label, labelLen);
751
572
    (*hash->end)(hashContext, &em[1 + hash->length], &i, hash->length);
752
572
    (*hash->destroy)(hashContext, PR_TRUE);
753
754
    /* Step 2.b - Generate PS */
755
572
    if (emLen - reservedLen - inputLen > 0) {
756
562
        PORT_Memset(em + 1 + (hash->length * 2), 0x00,
757
562
                    emLen - reservedLen - inputLen);
758
562
    }
759
760
    /* Step 2.c. - Generate DB
761
     * DB = lHash || PS || 0x01 || M
762
     * Note that PS and lHash have already been placed into em at their
763
     * appropriate offsets. This just copies M into place
764
     */
765
572
    em[emLen - inputLen - 1] = 0x01;
766
572
    if (inputLen)
767
161
        PORT_Memcpy(em + emLen - inputLen, input, inputLen);
768
769
572
    if (seed == NULL) {
770
        /* Step 2.d - Generate seed */
771
361
        rv = RNG_GenerateGlobalRandomBytes(em + 1, hash->length);
772
361
        if (rv != SECSuccess) {
773
0
            return rv;
774
0
        }
775
361
    } else {
776
        /* For Known Answer Tests, copy the supplied seed. */
777
211
        PORT_Memcpy(em + 1, seed, seedLen);
778
211
    }
779
780
    /* Step 2.e - Generate dbMask*/
781
572
    dbMaskLen = emLen - hash->length - 1;
782
572
    mask = (unsigned char *)PORT_Alloc(dbMaskLen);
783
572
    if (mask == NULL) {
784
0
        PORT_SetError(SEC_ERROR_NO_MEMORY);
785
0
        return SECFailure;
786
0
    }
787
572
    MGF1(maskHashAlg, mask, dbMaskLen, em + 1, hash->length);
788
    /* Step 2.f - Compute maskedDB*/
789
639k
    for (i = 0; i < dbMaskLen; ++i)
790
639k
        em[1 + hash->length + i] ^= mask[i];
791
792
    /* Step 2.g - Generate seedMask */
793
572
    MGF1(maskHashAlg, mask, hash->length, &em[1 + hash->length], dbMaskLen);
794
    /* Step 2.h - Compute maskedSeed */
795
18.1k
    for (i = 0; i < hash->length; ++i)
796
17.5k
        em[1 + i] ^= mask[i];
797
798
572
    PORT_ZFree(mask, dbMaskLen);
799
572
    return SECSuccess;
800
572
}
801
802
SECStatus
803
RSA_EncryptOAEP(RSAPublicKey *key,
804
                HASH_HashType hashAlg,
805
                HASH_HashType maskHashAlg,
806
                const unsigned char *label,
807
                unsigned int labelLen,
808
                const unsigned char *seed,
809
                unsigned int seedLen,
810
                unsigned char *output,
811
                unsigned int *outputLen,
812
                unsigned int maxOutputLen,
813
                const unsigned char *input,
814
                unsigned int inputLen)
815
604
{
816
604
    SECStatus rv = SECFailure;
817
604
    unsigned int modulusLen = rsa_modulusLen(&key->modulus);
818
604
    unsigned char *oaepEncoded = NULL;
819
820
604
    if (maxOutputLen < modulusLen) {
821
0
        PORT_SetError(SEC_ERROR_OUTPUT_LEN);
822
0
        return SECFailure;
823
0
    }
824
825
604
    if ((hashAlg == HASH_AlgNULL) || (maskHashAlg == HASH_AlgNULL)) {
826
0
        PORT_SetError(SEC_ERROR_INVALID_ALGORITHM);
827
0
        return SECFailure;
828
0
    }
829
830
604
    if ((labelLen == 0 && label != NULL) ||
831
604
        (labelLen > 0 && label == NULL)) {
832
1
        PORT_SetError(SEC_ERROR_INVALID_ALGORITHM);
833
1
        return SECFailure;
834
1
    }
835
836
603
    oaepEncoded = (unsigned char *)PORT_Alloc(modulusLen);
837
603
    if (oaepEncoded == NULL) {
838
0
        PORT_SetError(SEC_ERROR_NO_MEMORY);
839
0
        return SECFailure;
840
0
    }
841
603
    rv = eme_oaep_encode(oaepEncoded, modulusLen, input, inputLen,
842
603
                         hashAlg, maskHashAlg, label, labelLen, seed, seedLen);
843
603
    if (rv != SECSuccess)
844
31
        goto done;
845
846
572
    rv = RSA_PublicKeyOp(key, output, oaepEncoded);
847
572
    if (rv != SECSuccess)
848
408
        goto done;
849
164
    *outputLen = modulusLen;
850
851
603
done:
852
603
    PORT_Free(oaepEncoded);
853
603
    return rv;
854
164
}
855
856
SECStatus
857
RSA_DecryptOAEP(RSAPrivateKey *key,
858
                HASH_HashType hashAlg,
859
                HASH_HashType maskHashAlg,
860
                const unsigned char *label,
861
                unsigned int labelLen,
862
                unsigned char *output,
863
                unsigned int *outputLen,
864
                unsigned int maxOutputLen,
865
                const unsigned char *input,
866
                unsigned int inputLen)
867
400
{
868
400
    SECStatus rv = SECFailure;
869
400
    unsigned int modulusLen = rsa_modulusLen(&key->modulus);
870
400
    unsigned char *oaepEncoded = NULL;
871
872
400
    if ((hashAlg == HASH_AlgNULL) || (maskHashAlg == HASH_AlgNULL)) {
873
0
        PORT_SetError(SEC_ERROR_INVALID_ALGORITHM);
874
0
        return SECFailure;
875
0
    }
876
877
400
    if (inputLen != modulusLen) {
878
115
        PORT_SetError(SEC_ERROR_INPUT_LEN);
879
115
        return SECFailure;
880
115
    }
881
882
285
    if ((labelLen == 0 && label != NULL) ||
883
285
        (labelLen > 0 && label == NULL)) {
884
0
        PORT_SetError(SEC_ERROR_INVALID_ALGORITHM);
885
0
        return SECFailure;
886
0
    }
887
888
285
    oaepEncoded = (unsigned char *)PORT_Alloc(modulusLen);
889
285
    if (oaepEncoded == NULL) {
890
0
        PORT_SetError(SEC_ERROR_NO_MEMORY);
891
0
        return SECFailure;
892
0
    }
893
894
285
    rv = RSA_PrivateKeyOpDoubleChecked(key, oaepEncoded, input);
895
285
    if (rv != SECSuccess) {
896
283
        goto done;
897
283
    }
898
2
    rv = eme_oaep_decode(output, outputLen, maxOutputLen, oaepEncoded,
899
2
                         modulusLen, hashAlg, maskHashAlg, label,
900
2
                         labelLen);
901
902
285
done:
903
285
    if (oaepEncoded)
904
285
        PORT_ZFree(oaepEncoded, modulusLen);
905
285
    return rv;
906
2
}
907
908
/* XXX Doesn't set error code */
909
SECStatus
910
RSA_EncryptBlock(RSAPublicKey *key,
911
                 unsigned char *output,
912
                 unsigned int *outputLen,
913
                 unsigned int maxOutputLen,
914
                 const unsigned char *input,
915
                 unsigned int inputLen)
916
0
{
917
0
    SECStatus rv;
918
0
    unsigned int modulusLen = rsa_modulusLen(&key->modulus);
919
0
    SECItem formatted;
920
0
    SECItem unformatted;
921
922
0
    formatted.data = NULL;
923
0
    if (maxOutputLen < modulusLen)
924
0
        goto failure;
925
926
0
    unformatted.len = inputLen;
927
0
    unformatted.data = (unsigned char *)input;
928
0
    formatted.data = NULL;
929
0
    rv = rsa_FormatBlock(&formatted, modulusLen, RSA_BlockPublic,
930
0
                         &unformatted);
931
0
    if (rv != SECSuccess)
932
0
        goto failure;
933
934
0
    rv = RSA_PublicKeyOp(key, output, formatted.data);
935
0
    if (rv != SECSuccess)
936
0
        goto failure;
937
938
0
    PORT_ZFree(formatted.data, modulusLen);
939
0
    *outputLen = modulusLen;
940
0
    return SECSuccess;
941
942
0
failure:
943
0
    if (formatted.data != NULL)
944
0
        PORT_ZFree(formatted.data, modulusLen);
945
0
    return SECFailure;
946
0
}
947
948
static HMACContext *
949
rsa_GetHMACContext(const SECHashObject *hash, RSAPrivateKey *key,
950
                   const unsigned char *input, unsigned int inputLen)
951
0
{
952
0
    unsigned char keyHash[HASH_LENGTH_MAX];
953
0
    void *hashContext;
954
0
    HMACContext *hmac = NULL;
955
0
    unsigned int privKeyLen = key->privateExponent.len;
956
0
    unsigned int keyLen;
957
0
    SECStatus rv;
958
959
    /* first get the key hash (should store in the key structure) */
960
0
    PORT_Memset(keyHash, 0, sizeof(keyHash));
961
0
    hashContext = (*hash->create)();
962
0
    if (hashContext == NULL) {
963
0
        return NULL;
964
0
    }
965
0
    (*hash->begin)(hashContext);
966
0
    if (privKeyLen < inputLen) {
967
0
        int padLen = inputLen - privKeyLen;
968
0
        while (padLen > sizeof(keyHash)) {
969
0
            (*hash->update)(hashContext, keyHash, sizeof(keyHash));
970
0
            padLen -= sizeof(keyHash);
971
0
        }
972
0
        (*hash->update)(hashContext, keyHash, padLen);
973
0
    }
974
0
    (*hash->update)(hashContext, key->privateExponent.data, privKeyLen);
975
0
    (*hash->end)(hashContext, keyHash, &keyLen, sizeof(keyHash));
976
0
    (*hash->destroy)(hashContext, PR_TRUE);
977
978
    /* now create the hmac key */
979
0
    hmac = HMAC_Create(hash, keyHash, keyLen, PR_TRUE);
980
0
    if (hmac == NULL) {
981
0
        PORT_Memset(keyHash, 0, sizeof(keyHash));
982
0
        return NULL;
983
0
    }
984
0
    HMAC_Begin(hmac);
985
0
    HMAC_Update(hmac, input, inputLen);
986
0
    rv = HMAC_Finish(hmac, keyHash, &keyLen, sizeof(keyHash));
987
0
    if (rv != SECSuccess) {
988
0
        PORT_Memset(keyHash, 0, sizeof(keyHash));
989
0
        HMAC_Destroy(hmac, PR_TRUE);
990
0
        return NULL;
991
0
    }
992
    /* Finally set the new key into the hash context. We
993
     * reuse the original context allocated above so we don't
994
     * need to allocate and free another one */
995
0
    rv = HMAC_ReInit(hmac, hash, keyHash, keyLen, PR_TRUE);
996
0
    PORT_Memset(keyHash, 0, sizeof(keyHash));
997
0
    if (rv != SECSuccess) {
998
0
        HMAC_Destroy(hmac, PR_TRUE);
999
0
        return NULL;
1000
0
    }
1001
1002
0
    return hmac;
1003
0
}
1004
1005
static SECStatus
1006
rsa_HMACPrf(HMACContext *hmac, const char *label, int labelLen,
1007
            int hashLength, unsigned char *output, int length)
1008
0
{
1009
0
    unsigned char iterator[2] = { 0, 0 };
1010
0
    unsigned char encodedLen[2] = { 0, 0 };
1011
0
    unsigned char hmacLast[HASH_LENGTH_MAX];
1012
0
    unsigned int left = length;
1013
0
    unsigned int hashReturn;
1014
0
    SECStatus rv = SECSuccess;
1015
1016
    /* encodedLen is in bits, length is in bytes, thus the shifts
1017
     * do an implied multiply by 8 */
1018
0
    encodedLen[0] = (length >> 5) & 0xff;
1019
0
    encodedLen[1] = (length << 3) & 0xff;
1020
1021
0
    while (left > hashLength) {
1022
0
        HMAC_Begin(hmac);
1023
0
        HMAC_Update(hmac, iterator, 2);
1024
0
        HMAC_Update(hmac, (const unsigned char *)label, labelLen);
1025
0
        HMAC_Update(hmac, encodedLen, 2);
1026
0
        rv = HMAC_Finish(hmac, output, &hashReturn, hashLength);
1027
0
        if (rv != SECSuccess) {
1028
0
            return rv;
1029
0
        }
1030
0
        iterator[1]++;
1031
0
        if (iterator[1] == 0)
1032
0
            iterator[0]++;
1033
0
        left -= hashLength;
1034
0
        output += hashLength;
1035
0
    }
1036
0
    if (left) {
1037
0
        HMAC_Begin(hmac);
1038
0
        HMAC_Update(hmac, iterator, 2);
1039
0
        HMAC_Update(hmac, (const unsigned char *)label, labelLen);
1040
0
        HMAC_Update(hmac, encodedLen, 2);
1041
0
        rv = HMAC_Finish(hmac, hmacLast, &hashReturn, sizeof(hmacLast));
1042
0
        if (rv != SECSuccess) {
1043
0
            return rv;
1044
0
        }
1045
0
        PORT_Memcpy(output, hmacLast, left);
1046
0
        PORT_Memset(hmacLast, 0, sizeof(hmacLast));
1047
0
    }
1048
0
    return rv;
1049
0
}
1050
1051
/* This function takes a 16-bit input number and
1052
 * creates the smallest mask which covers
1053
 * the whole number. Examples:
1054
 *     0x81 -> 0xff
1055
 *     0x1af -> 0x1ff
1056
 *     0x4d1 -> 0x7ff
1057
 */
1058
static int
1059
makeMask16(int len)
1060
0
{
1061
    // or the high bit in each bit location
1062
0
    len |= (len >> 1);
1063
0
    len |= (len >> 2);
1064
0
    len |= (len >> 4);
1065
0
    len |= (len >> 8);
1066
0
    return len;
1067
0
}
1068
1069
0
#define STRING_AND_LENGTH(s) s, sizeof(s) - 1
1070
static int
1071
rsa_GetErrorLength(HMACContext *hmac, int hashLen, int maxLegalLen)
1072
0
{
1073
0
    unsigned char out[128 * 2];
1074
0
    unsigned char *outp;
1075
0
    int outLength = 0;
1076
0
    int lengthMask;
1077
0
    SECStatus rv;
1078
1079
0
    lengthMask = makeMask16(maxLegalLen);
1080
0
    rv = rsa_HMACPrf(hmac, STRING_AND_LENGTH("length"), hashLen,
1081
0
                     out, sizeof(out));
1082
0
    if (rv != SECSuccess) {
1083
0
        return -1;
1084
0
    }
1085
0
    for (outp = out; outp < out + sizeof(out); outp += 2) {
1086
0
        int candidate = outp[0] << 8 | outp[1];
1087
0
        candidate = candidate & lengthMask;
1088
0
        outLength = PORT_CT_SEL(PORT_CT_LT(candidate, maxLegalLen),
1089
0
                                candidate, outLength);
1090
0
    }
1091
0
    PORT_Memset(out, 0, sizeof(out));
1092
0
    return outLength;
1093
0
}
1094
1095
/*
1096
 * This function can only fail in environmental cases: Programming errors
1097
 * and out of memory situations. It can't fail if the keys are valid and
1098
 * the inputs are the proper size. If the actual RSA decryption fails, a
1099
 * fake value and a fake length, both of which have already been generated
1100
 * based on the key and input, are returned.
1101
 * Applications are expected to detect decryption failures based on the fact
1102
 * that the decrypted value (usually a key) doesn't validate. The prevents
1103
 * Blecheinbaucher style attacks against the key. */
1104
SECStatus
1105
RSA_DecryptBlock(RSAPrivateKey *key,
1106
                 unsigned char *output,
1107
                 unsigned int *outputLen,
1108
                 unsigned int maxOutputLen,
1109
                 const unsigned char *input,
1110
                 unsigned int inputLen)
1111
0
{
1112
0
    SECStatus rv;
1113
0
    PRUint32 fail;
1114
0
    unsigned int modulusLen = rsa_modulusLen(&key->modulus);
1115
0
    unsigned int i;
1116
0
    unsigned char *buffer = NULL;
1117
0
    unsigned char *errorBuffer = NULL;
1118
0
    unsigned char *bp = NULL;
1119
0
    unsigned char *ep = NULL;
1120
0
    unsigned int outLen = modulusLen;
1121
0
    unsigned int maxLegalLen = modulusLen - 10;
1122
0
    unsigned int errorLength;
1123
0
    const SECHashObject *hashObj;
1124
0
    HMACContext *hmac = NULL;
1125
1126
    /* failures in the top section indicate failures in the environment
1127
     * (memory) or the library. OK to return errors in these cases because
1128
     * it doesn't provide any oracle information to attackers. */
1129
0
    if (inputLen != modulusLen || modulusLen < 10) {
1130
0
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
1131
0
        return SECFailure;
1132
0
    }
1133
1134
    /* Allocate enough space to decrypt */
1135
0
    buffer = PORT_ZAlloc(modulusLen);
1136
0
    if (!buffer) {
1137
0
        goto loser;
1138
0
    }
1139
0
    errorBuffer = PORT_ZAlloc(modulusLen);
1140
0
    if (!errorBuffer) {
1141
0
        goto loser;
1142
0
    }
1143
0
    hashObj = HASH_GetRawHashObject(HASH_AlgSHA256);
1144
0
    if (hashObj == NULL) {
1145
0
        goto loser;
1146
0
    }
1147
1148
    /* calculate the values to return in the error case rather than
1149
     * the actual returned values. This data is the same for the
1150
     * same input and private key. */
1151
0
    hmac = rsa_GetHMACContext(hashObj, key, input, inputLen);
1152
0
    if (hmac == NULL) {
1153
0
        goto loser;
1154
0
    }
1155
0
    errorLength = rsa_GetErrorLength(hmac, hashObj->length, maxLegalLen);
1156
0
    if (((int)errorLength) < 0) {
1157
0
        goto loser;
1158
0
    }
1159
    /* we always have to generate a full moduluslen error string. Otherwise
1160
     * we create a timing dependency on errorLength, which could be used to
1161
     * determine the difference between errorLength and outputLen and tell
1162
     * us that there was a pkcs1 decryption failure */
1163
0
    rv = rsa_HMACPrf(hmac, STRING_AND_LENGTH("message"),
1164
0
                     hashObj->length, errorBuffer, modulusLen);
1165
0
    if (rv != SECSuccess) {
1166
0
        goto loser;
1167
0
    }
1168
1169
0
    HMAC_Destroy(hmac, PR_TRUE);
1170
0
    hmac = NULL;
1171
1172
    /* From here on out, we will always return success. If there is
1173
     * an error, we will return deterministic output based on the key
1174
     * and the input data. */
1175
0
    rv = RSA_PrivateKeyOp(key, buffer, input);
1176
1177
0
    fail = PORT_CT_NE(rv, SECSuccess);
1178
0
    fail |= PORT_CT_NE(buffer[0], RSA_BLOCK_FIRST_OCTET) | PORT_CT_NE(buffer[1], RSA_BlockPublic);
1179
1180
    /* There have to be at least 8 bytes of padding. */
1181
0
    for (i = 2; i < 10; i++) {
1182
0
        fail |= PORT_CT_EQ(buffer[i], RSA_BLOCK_AFTER_PAD_OCTET);
1183
0
    }
1184
1185
0
    for (i = 10; i < modulusLen; i++) {
1186
0
        unsigned int newLen = modulusLen - i - 1;
1187
0
        PRUint32 condition = PORT_CT_EQ(buffer[i], RSA_BLOCK_AFTER_PAD_OCTET) & PORT_CT_EQ(outLen, modulusLen);
1188
0
        outLen = PORT_CT_SEL(condition, newLen, outLen);
1189
0
    }
1190
    // this can only happen if a zero wasn't found above
1191
0
    fail |= PORT_CT_GE(outLen, modulusLen);
1192
1193
0
    outLen = PORT_CT_SEL(fail, errorLength, outLen);
1194
1195
    /* index into the correct buffer. Do it before we truncate outLen if the
1196
     * application was asking for less data than we can return */
1197
0
    bp = buffer + modulusLen - outLen;
1198
0
    ep = errorBuffer + modulusLen - outLen;
1199
1200
    /* at this point, outLen returns no information about decryption failures,
1201
     * no need to hide its value. maxOutputLen is how much data the
1202
     * application is expecting, which is also not sensitive. */
1203
0
    if (outLen > maxOutputLen) {
1204
0
        outLen = maxOutputLen;
1205
0
    }
1206
1207
    /* we can't use PORT_Memcpy because caching could create a time dependency
1208
     * on the status of fail. */
1209
0
    for (i = 0; i < outLen; i++) {
1210
0
        output[i] = PORT_CT_SEL(fail, ep[i], bp[i]);
1211
0
    }
1212
1213
0
    *outputLen = outLen;
1214
1215
0
    PORT_Free(buffer);
1216
0
    PORT_Free(errorBuffer);
1217
1218
0
    return SECSuccess;
1219
1220
0
loser:
1221
0
    if (hmac) {
1222
0
        HMAC_Destroy(hmac, PR_TRUE);
1223
0
    }
1224
0
    PORT_Free(buffer);
1225
0
    PORT_Free(errorBuffer);
1226
1227
0
    return SECFailure;
1228
0
}
1229
1230
/*
1231
 * Encode a RSA-PSS signature.
1232
 * Described in RFC 3447, section 9.1.1.
1233
 * We use mHash instead of M as input.
1234
 * emBits from the RFC is just modBits - 1, see section 8.1.1.
1235
 * We only support MGF1 as the MGF.
1236
 */
1237
static SECStatus
1238
emsa_pss_encode(unsigned char *em,
1239
                unsigned int emLen,
1240
                unsigned int emBits,
1241
                const unsigned char *mHash,
1242
                HASH_HashType hashAlg,
1243
                HASH_HashType maskHashAlg,
1244
                const unsigned char *salt,
1245
                unsigned int saltLen)
1246
762
{
1247
762
    const SECHashObject *hash;
1248
762
    void *hash_context;
1249
762
    unsigned char *dbMask;
1250
762
    unsigned int dbMaskLen;
1251
762
    unsigned int i;
1252
762
    SECStatus rv;
1253
1254
762
    hash = HASH_GetRawHashObject(hashAlg);
1255
762
    dbMaskLen = emLen - hash->length - 1;
1256
1257
    /* Step 3 */
1258
762
    if (emLen < hash->length + saltLen + 2) {
1259
317
        PORT_SetError(SEC_ERROR_OUTPUT_LEN);
1260
317
        return SECFailure;
1261
317
    }
1262
1263
    /* Step 4 */
1264
445
    if (salt == NULL) {
1265
237
        rv = RNG_GenerateGlobalRandomBytes(&em[dbMaskLen - saltLen], saltLen);
1266
237
        if (rv != SECSuccess) {
1267
6
            return rv;
1268
6
        }
1269
237
    } else {
1270
208
        PORT_Memcpy(&em[dbMaskLen - saltLen], salt, saltLen);
1271
208
    }
1272
1273
    /* Step 5 + 6 */
1274
    /* Compute H and store it at its final location &em[dbMaskLen]. */
1275
439
    hash_context = (*hash->create)();
1276
439
    if (hash_context == NULL) {
1277
0
        PORT_SetError(SEC_ERROR_NO_MEMORY);
1278
0
        return SECFailure;
1279
0
    }
1280
439
    (*hash->begin)(hash_context);
1281
439
    (*hash->update)(hash_context, eightZeros, 8);
1282
439
    (*hash->update)(hash_context, mHash, hash->length);
1283
439
    (*hash->update)(hash_context, &em[dbMaskLen - saltLen], saltLen);
1284
439
    (*hash->end)(hash_context, &em[dbMaskLen], &i, hash->length);
1285
439
    (*hash->destroy)(hash_context, PR_TRUE);
1286
1287
    /* Step 7 + 8 */
1288
439
    PORT_Memset(em, 0, dbMaskLen - saltLen - 1);
1289
439
    em[dbMaskLen - saltLen - 1] = 0x01;
1290
1291
    /* Step 9 */
1292
439
    dbMask = (unsigned char *)PORT_Alloc(dbMaskLen);
1293
439
    if (dbMask == NULL) {
1294
0
        PORT_SetError(SEC_ERROR_NO_MEMORY);
1295
0
        return SECFailure;
1296
0
    }
1297
439
    MGF1(maskHashAlg, dbMask, dbMaskLen, &em[dbMaskLen], hash->length);
1298
1299
    /* Step 10 */
1300
81.8k
    for (i = 0; i < dbMaskLen; i++)
1301
81.4k
        em[i] ^= dbMask[i];
1302
439
    PORT_Free(dbMask);
1303
1304
    /* Step 11 */
1305
439
    em[0] &= 0xff >> (8 * emLen - emBits);
1306
1307
    /* Step 12 */
1308
439
    em[emLen - 1] = 0xbc;
1309
1310
439
    return SECSuccess;
1311
439
}
1312
1313
/*
1314
 * Verify a RSA-PSS signature.
1315
 * Described in RFC 3447, section 9.1.2.
1316
 * We use mHash instead of M as input.
1317
 * emBits from the RFC is just modBits - 1, see section 8.1.2.
1318
 * We only support MGF1 as the MGF.
1319
 */
1320
static SECStatus
1321
emsa_pss_verify(const unsigned char *mHash,
1322
                const unsigned char *em,
1323
                unsigned int emLen,
1324
                unsigned int emBits,
1325
                HASH_HashType hashAlg,
1326
                HASH_HashType maskHashAlg,
1327
                unsigned int saltLen)
1328
2.30k
{
1329
2.30k
    const SECHashObject *hash;
1330
2.30k
    void *hash_context;
1331
2.30k
    unsigned char *db;
1332
2.30k
    unsigned char *H_; /* H' from the RFC */
1333
2.30k
    unsigned int i;
1334
2.30k
    unsigned int dbMaskLen;
1335
2.30k
    unsigned int zeroBits;
1336
2.30k
    SECStatus rv;
1337
1338
2.30k
    hash = HASH_GetRawHashObject(hashAlg);
1339
2.30k
    dbMaskLen = emLen - hash->length - 1;
1340
1341
    /* Step 3 + 4 */
1342
2.30k
    if ((emLen < (hash->length + saltLen + 2)) ||
1343
2.30k
        (em[emLen - 1] != 0xbc)) {
1344
2.09k
        PORT_SetError(SEC_ERROR_BAD_SIGNATURE);
1345
2.09k
        return SECFailure;
1346
2.09k
    }
1347
1348
    /* Step 6 */
1349
211
    zeroBits = 8 * emLen - emBits;
1350
211
    if (em[0] >> (8 - zeroBits)) {
1351
19
        PORT_SetError(SEC_ERROR_BAD_SIGNATURE);
1352
19
        return SECFailure;
1353
19
    }
1354
1355
    /* Step 7 */
1356
192
    db = (unsigned char *)PORT_Alloc(dbMaskLen);
1357
192
    if (db == NULL) {
1358
0
        PORT_SetError(SEC_ERROR_NO_MEMORY);
1359
0
        return SECFailure;
1360
0
    }
1361
    /* &em[dbMaskLen] points to H, used as mgfSeed */
1362
192
    MGF1(maskHashAlg, db, dbMaskLen, &em[dbMaskLen], hash->length);
1363
1364
    /* Step 8 */
1365
8.32k
    for (i = 0; i < dbMaskLen; i++) {
1366
8.12k
        db[i] ^= em[i];
1367
8.12k
    }
1368
1369
    /* Step 9 */
1370
192
    db[0] &= 0xff >> zeroBits;
1371
1372
    /* Step 10 */
1373
206
    for (i = 0; i < (dbMaskLen - saltLen - 1); i++) {
1374
189
        if (db[i] != 0) {
1375
175
            PORT_Free(db);
1376
175
            PORT_SetError(SEC_ERROR_BAD_SIGNATURE);
1377
175
            return SECFailure;
1378
175
        }
1379
189
    }
1380
17
    if (db[dbMaskLen - saltLen - 1] != 0x01) {
1381
13
        PORT_Free(db);
1382
13
        PORT_SetError(SEC_ERROR_BAD_SIGNATURE);
1383
13
        return SECFailure;
1384
13
    }
1385
1386
    /* Step 12 + 13 */
1387
4
    H_ = (unsigned char *)PORT_Alloc(hash->length);
1388
4
    if (H_ == NULL) {
1389
0
        PORT_Free(db);
1390
0
        PORT_SetError(SEC_ERROR_NO_MEMORY);
1391
0
        return SECFailure;
1392
0
    }
1393
4
    hash_context = (*hash->create)();
1394
4
    if (hash_context == NULL) {
1395
0
        PORT_Free(db);
1396
0
        PORT_Free(H_);
1397
0
        PORT_SetError(SEC_ERROR_NO_MEMORY);
1398
0
        return SECFailure;
1399
0
    }
1400
4
    (*hash->begin)(hash_context);
1401
4
    (*hash->update)(hash_context, eightZeros, 8);
1402
4
    (*hash->update)(hash_context, mHash, hash->length);
1403
4
    (*hash->update)(hash_context, &db[dbMaskLen - saltLen], saltLen);
1404
4
    (*hash->end)(hash_context, H_, &i, hash->length);
1405
4
    (*hash->destroy)(hash_context, PR_TRUE);
1406
1407
4
    PORT_Free(db);
1408
1409
    /* Step 14 */
1410
4
    if (PORT_Memcmp(H_, &em[dbMaskLen], hash->length) != 0) {
1411
4
        PORT_SetError(SEC_ERROR_BAD_SIGNATURE);
1412
4
        rv = SECFailure;
1413
4
    } else {
1414
0
        rv = SECSuccess;
1415
0
    }
1416
1417
4
    PORT_Free(H_);
1418
4
    return rv;
1419
4
}
1420
1421
SECStatus
1422
RSA_SignPSS(RSAPrivateKey *key,
1423
            HASH_HashType hashAlg,
1424
            HASH_HashType maskHashAlg,
1425
            const unsigned char *salt,
1426
            unsigned int saltLength,
1427
            unsigned char *output,
1428
            unsigned int *outputLen,
1429
            unsigned int maxOutputLen,
1430
            const unsigned char *input,
1431
            unsigned int inputLen)
1432
762
{
1433
762
    SECStatus rv = SECSuccess;
1434
762
    unsigned int modulusLen = rsa_modulusLen(&key->modulus);
1435
762
    unsigned int modulusBits = rsa_modulusBits(&key->modulus);
1436
762
    unsigned int emLen = modulusLen;
1437
762
    unsigned char *pssEncoded, *em;
1438
1439
762
    if (maxOutputLen < modulusLen) {
1440
0
        PORT_SetError(SEC_ERROR_OUTPUT_LEN);
1441
0
        return SECFailure;
1442
0
    }
1443
1444
762
    if ((hashAlg == HASH_AlgNULL) || (maskHashAlg == HASH_AlgNULL)) {
1445
0
        PORT_SetError(SEC_ERROR_INVALID_ALGORITHM);
1446
0
        return SECFailure;
1447
0
    }
1448
1449
762
    pssEncoded = em = (unsigned char *)PORT_Alloc(modulusLen);
1450
762
    if (pssEncoded == NULL) {
1451
0
        PORT_SetError(SEC_ERROR_NO_MEMORY);
1452
0
        return SECFailure;
1453
0
    }
1454
1455
    /* len(em) == ceil((modulusBits - 1) / 8). */
1456
762
    if (modulusBits % 8 == 1) {
1457
0
        em[0] = 0;
1458
0
        emLen--;
1459
0
        em++;
1460
0
    }
1461
762
    rv = emsa_pss_encode(em, emLen, modulusBits - 1, input, hashAlg,
1462
762
                         maskHashAlg, salt, saltLength);
1463
762
    if (rv != SECSuccess)
1464
323
        goto done;
1465
1466
    // This sets error codes upon failure.
1467
439
    rv = RSA_PrivateKeyOpDoubleChecked(key, output, pssEncoded);
1468
439
    *outputLen = modulusLen;
1469
1470
762
done:
1471
762
    PORT_Free(pssEncoded);
1472
762
    return rv;
1473
439
}
1474
1475
SECStatus
1476
RSA_CheckSignPSS(RSAPublicKey *key,
1477
                 HASH_HashType hashAlg,
1478
                 HASH_HashType maskHashAlg,
1479
                 unsigned int saltLength,
1480
                 const unsigned char *sig,
1481
                 unsigned int sigLen,
1482
                 const unsigned char *hash,
1483
                 unsigned int hashLen)
1484
2.71k
{
1485
2.71k
    SECStatus rv;
1486
2.71k
    unsigned int modulusLen = rsa_modulusLen(&key->modulus);
1487
2.71k
    unsigned int modulusBits = rsa_modulusBits(&key->modulus);
1488
2.71k
    unsigned int emLen = modulusLen;
1489
2.71k
    unsigned char *buffer, *em;
1490
1491
2.71k
    if (sigLen != modulusLen) {
1492
198
        PORT_SetError(SEC_ERROR_BAD_SIGNATURE);
1493
198
        return SECFailure;
1494
198
    }
1495
1496
2.51k
    if ((hashAlg == HASH_AlgNULL) || (maskHashAlg == HASH_AlgNULL)) {
1497
0
        PORT_SetError(SEC_ERROR_INVALID_ALGORITHM);
1498
0
        return SECFailure;
1499
0
    }
1500
1501
2.51k
    buffer = em = (unsigned char *)PORT_Alloc(modulusLen);
1502
2.51k
    if (!buffer) {
1503
0
        PORT_SetError(SEC_ERROR_NO_MEMORY);
1504
0
        return SECFailure;
1505
0
    }
1506
1507
2.51k
    rv = RSA_PublicKeyOp(key, buffer, sig);
1508
2.51k
    if (rv != SECSuccess) {
1509
208
        PORT_Free(buffer);
1510
208
        PORT_SetError(SEC_ERROR_BAD_SIGNATURE);
1511
208
        return SECFailure;
1512
208
    }
1513
1514
    /* len(em) == ceil((modulusBits - 1) / 8). */
1515
2.30k
    if (modulusBits % 8 == 1) {
1516
481
        emLen--;
1517
481
        em++;
1518
481
    }
1519
2.30k
    rv = emsa_pss_verify(hash, em, emLen, modulusBits - 1, hashAlg,
1520
2.30k
                         maskHashAlg, saltLength);
1521
1522
2.30k
    PORT_Free(buffer);
1523
2.30k
    return rv;
1524
2.51k
}
1525
1526
SECStatus
1527
RSA_Sign(RSAPrivateKey *key,
1528
         unsigned char *output,
1529
         unsigned int *outputLen,
1530
         unsigned int maxOutputLen,
1531
         const unsigned char *input,
1532
         unsigned int inputLen)
1533
3.43k
{
1534
3.43k
    SECStatus rv = SECFailure;
1535
3.43k
    unsigned int modulusLen = rsa_modulusLen(&key->modulus);
1536
3.43k
    SECItem formatted = { siBuffer, NULL, 0 };
1537
3.43k
    SECItem unformatted = { siBuffer, (unsigned char *)input, inputLen };
1538
1539
3.43k
    if (maxOutputLen < modulusLen) {
1540
0
        PORT_SetError(SEC_ERROR_OUTPUT_LEN);
1541
0
        goto done;
1542
0
    }
1543
1544
3.43k
    rv = rsa_FormatBlock(&formatted, modulusLen, RSA_BlockPrivate,
1545
3.43k
                         &unformatted);
1546
3.43k
    if (rv != SECSuccess) {
1547
220
        PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
1548
220
        goto done;
1549
220
    }
1550
1551
    // This sets error codes upon failure.
1552
3.21k
    rv = RSA_PrivateKeyOpDoubleChecked(key, output, formatted.data);
1553
3.21k
    *outputLen = modulusLen;
1554
1555
3.43k
done:
1556
3.43k
    if (formatted.data != NULL) {
1557
3.21k
        PORT_ZFree(formatted.data, modulusLen);
1558
3.21k
    }
1559
3.43k
    return rv;
1560
3.21k
}
1561
1562
SECStatus
1563
RSA_CheckSign(RSAPublicKey *key,
1564
              const unsigned char *sig,
1565
              unsigned int sigLen,
1566
              const unsigned char *data,
1567
              unsigned int dataLen)
1568
1.94k
{
1569
1.94k
    SECStatus rv = SECFailure;
1570
1.94k
    unsigned int modulusLen = rsa_modulusLen(&key->modulus);
1571
1.94k
    unsigned int i;
1572
1.94k
    unsigned char *buffer = NULL;
1573
1574
1.94k
    if (sigLen != modulusLen) {
1575
60
        PORT_SetError(SEC_ERROR_BAD_SIGNATURE);
1576
60
        goto done;
1577
60
    }
1578
1579
    /*
1580
     * 0x00 || BT || Pad || 0x00 || ActualData
1581
     *
1582
     * The "3" below is the first octet + the second octet + the 0x00
1583
     * octet that always comes just before the ActualData.
1584
     */
1585
1.88k
    if (dataLen > modulusLen - (3 + RSA_BLOCK_MIN_PAD_LEN)) {
1586
63
        PORT_SetError(SEC_ERROR_BAD_DATA);
1587
63
        goto done;
1588
63
    }
1589
1590
1.82k
    buffer = (unsigned char *)PORT_Alloc(modulusLen + 1);
1591
1.82k
    if (!buffer) {
1592
0
        PORT_SetError(SEC_ERROR_NO_MEMORY);
1593
0
        goto done;
1594
0
    }
1595
1596
1.82k
    if (RSA_PublicKeyOp(key, buffer, sig) != SECSuccess) {
1597
148
        PORT_SetError(SEC_ERROR_BAD_SIGNATURE);
1598
148
        goto done;
1599
148
    }
1600
1601
    /*
1602
     * check the padding that was used
1603
     */
1604
1.67k
    if (buffer[0] != RSA_BLOCK_FIRST_OCTET ||
1605
1.67k
        buffer[1] != (unsigned char)RSA_BlockPrivate) {
1606
1.63k
        PORT_SetError(SEC_ERROR_BAD_SIGNATURE);
1607
1.63k
        goto done;
1608
1.63k
    }
1609
205
    for (i = 2; i < modulusLen - dataLen - 1; i++) {
1610
203
        if (buffer[i] != RSA_BLOCK_PRIVATE_PAD_OCTET) {
1611
34
            PORT_SetError(SEC_ERROR_BAD_SIGNATURE);
1612
34
            goto done;
1613
34
        }
1614
203
    }
1615
2
    if (buffer[i] != RSA_BLOCK_AFTER_PAD_OCTET) {
1616
2
        PORT_SetError(SEC_ERROR_BAD_SIGNATURE);
1617
2
        goto done;
1618
2
    }
1619
1620
    /*
1621
     * make sure we get the same results
1622
     */
1623
0
    if (PORT_Memcmp(buffer + modulusLen - dataLen, data, dataLen) == 0) {
1624
0
        rv = SECSuccess;
1625
0
    }
1626
1627
1.94k
done:
1628
1.94k
    if (buffer) {
1629
1.82k
        PORT_Free(buffer);
1630
1.82k
    }
1631
1.94k
    return rv;
1632
0
}
1633
1634
SECStatus
1635
RSA_CheckSignRecover(RSAPublicKey *key,
1636
                     unsigned char *output,
1637
                     unsigned int *outputLen,
1638
                     unsigned int maxOutputLen,
1639
                     const unsigned char *sig,
1640
                     unsigned int sigLen)
1641
32
{
1642
32
    SECStatus rv = SECFailure;
1643
32
    unsigned int modulusLen = rsa_modulusLen(&key->modulus);
1644
32
    unsigned int i;
1645
32
    unsigned char *buffer = NULL;
1646
32
    unsigned int padLen;
1647
1648
32
    if (sigLen != modulusLen) {
1649
15
        PORT_SetError(SEC_ERROR_BAD_SIGNATURE);
1650
15
        goto done;
1651
15
    }
1652
1653
17
    buffer = (unsigned char *)PORT_Alloc(modulusLen + 1);
1654
17
    if (!buffer) {
1655
0
        PORT_SetError(SEC_ERROR_NO_MEMORY);
1656
0
        goto done;
1657
0
    }
1658
1659
17
    if (RSA_PublicKeyOp(key, buffer, sig) != SECSuccess) {
1660
12
        PORT_SetError(SEC_ERROR_BAD_SIGNATURE);
1661
12
        goto done;
1662
12
    }
1663
1664
5
    *outputLen = 0;
1665
1666
    /*
1667
     * check the padding that was used
1668
     */
1669
5
    if (buffer[0] != RSA_BLOCK_FIRST_OCTET ||
1670
5
        buffer[1] != (unsigned char)RSA_BlockPrivate) {
1671
5
        PORT_SetError(SEC_ERROR_BAD_SIGNATURE);
1672
5
        goto done;
1673
5
    }
1674
0
    for (i = 2; i < modulusLen; i++) {
1675
0
        if (buffer[i] == RSA_BLOCK_AFTER_PAD_OCTET) {
1676
0
            *outputLen = modulusLen - i - 1;
1677
0
            break;
1678
0
        }
1679
0
        if (buffer[i] != RSA_BLOCK_PRIVATE_PAD_OCTET) {
1680
0
            PORT_SetError(SEC_ERROR_BAD_SIGNATURE);
1681
0
            goto done;
1682
0
        }
1683
0
    }
1684
0
    padLen = i - 2;
1685
0
    if (padLen < RSA_BLOCK_MIN_PAD_LEN) {
1686
0
        PORT_SetError(SEC_ERROR_BAD_SIGNATURE);
1687
0
        goto done;
1688
0
    }
1689
0
    if (*outputLen == 0) {
1690
0
        PORT_SetError(SEC_ERROR_BAD_SIGNATURE);
1691
0
        goto done;
1692
0
    }
1693
0
    if (*outputLen > maxOutputLen) {
1694
0
        PORT_SetError(SEC_ERROR_OUTPUT_LEN);
1695
0
        goto done;
1696
0
    }
1697
1698
0
    PORT_Memcpy(output, buffer + modulusLen - *outputLen, *outputLen);
1699
0
    rv = SECSuccess;
1700
1701
32
done:
1702
32
    if (buffer) {
1703
17
        PORT_Free(buffer);
1704
17
    }
1705
32
    return rv;
1706
0
}