/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 | } |