OLD | NEW |
1 /* This Source Code Form is subject to the terms of the Mozilla Public | 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 | 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/. */ | 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 | 4 |
5 /* | 5 /* |
6 * RSA key generation, public key op, private key op. | 6 * RSA key generation, public key op, private key op. |
7 */ | 7 */ |
8 #ifdef FREEBL_NO_DEPEND | 8 #ifdef FREEBL_NO_DEPEND |
9 #include "stubs.h" | 9 #include "stubs.h" |
10 #endif | 10 #endif |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 static struct RSABlindingParamsListStr blindingParamsList = { 0 }; | 90 static struct RSABlindingParamsListStr blindingParamsList = { 0 }; |
91 | 91 |
92 /* Number of times to reuse (f, g). Suggested by Paul Kocher */ | 92 /* Number of times to reuse (f, g). Suggested by Paul Kocher */ |
93 #define RSA_BLINDING_PARAMS_MAX_REUSE 50 | 93 #define RSA_BLINDING_PARAMS_MAX_REUSE 50 |
94 | 94 |
95 /* Global, allows optional use of blinding. On by default. */ | 95 /* Global, allows optional use of blinding. On by default. */ |
96 /* Cannot be changed at the moment, due to thread-safety issues. */ | 96 /* Cannot be changed at the moment, due to thread-safety issues. */ |
97 static PRBool nssRSAUseBlinding = PR_TRUE; | 97 static PRBool nssRSAUseBlinding = PR_TRUE; |
98 | 98 |
99 static SECStatus | 99 static SECStatus |
100 rsa_build_from_primes(mp_int *p, mp_int *q, | 100 rsa_build_from_primes(const mp_int *p, const mp_int *q, |
101 » » mp_int *e, PRBool needPublicExponent, | 101 » » mp_int *e, PRBool needPublicExponent, |
102 mp_int *d, PRBool needPrivateExponent, | 102 mp_int *d, PRBool needPrivateExponent, |
103 RSAPrivateKey *key, unsigned int keySizeInBits) | 103 RSAPrivateKey *key, unsigned int keySizeInBits) |
104 { | 104 { |
105 mp_int n, phi; | 105 mp_int n, phi; |
106 mp_int psub1, qsub1, tmp; | 106 mp_int psub1, qsub1, tmp; |
107 mp_err err = MP_OKAY; | 107 mp_err err = MP_OKAY; |
108 SECStatus rv = SECSuccess; | 108 SECStatus rv = SECSuccess; |
109 MP_DIGITS(&n) = 0; | 109 MP_DIGITS(&n) = 0; |
110 MP_DIGITS(&phi) = 0; | 110 MP_DIGITS(&phi) = 0; |
111 MP_DIGITS(&psub1) = 0; | 111 MP_DIGITS(&psub1) = 0; |
112 MP_DIGITS(&qsub1) = 0; | 112 MP_DIGITS(&qsub1) = 0; |
113 MP_DIGITS(&tmp) = 0; | 113 MP_DIGITS(&tmp) = 0; |
114 CHECK_MPI_OK( mp_init(&n) ); | 114 CHECK_MPI_OK( mp_init(&n) ); |
115 CHECK_MPI_OK( mp_init(&phi) ); | 115 CHECK_MPI_OK( mp_init(&phi) ); |
116 CHECK_MPI_OK( mp_init(&psub1) ); | 116 CHECK_MPI_OK( mp_init(&psub1) ); |
117 CHECK_MPI_OK( mp_init(&qsub1) ); | 117 CHECK_MPI_OK( mp_init(&qsub1) ); |
118 CHECK_MPI_OK( mp_init(&tmp) ); | 118 CHECK_MPI_OK( mp_init(&tmp) ); |
| 119 /* p and q must be distinct. */ |
| 120 if (mp_cmp(p, q) == 0) { |
| 121 PORT_SetError(SEC_ERROR_NEED_RANDOM); |
| 122 rv = SECFailure; |
| 123 goto cleanup; |
| 124 } |
119 /* 1. Compute n = p*q */ | 125 /* 1. Compute n = p*q */ |
120 CHECK_MPI_OK( mp_mul(p, q, &n) ); | 126 CHECK_MPI_OK( mp_mul(p, q, &n) ); |
121 /* verify that the modulus has the desired number of bits */ | 127 /* verify that the modulus has the desired number of bits */ |
122 if ((unsigned)mpl_significant_bits(&n) != keySizeInBits) { | 128 if ((unsigned)mpl_significant_bits(&n) != keySizeInBits) { |
123 PORT_SetError(SEC_ERROR_NEED_RANDOM); | 129 PORT_SetError(SEC_ERROR_NEED_RANDOM); |
124 rv = SECFailure; | 130 rv = SECFailure; |
125 goto cleanup; | 131 goto cleanup; |
126 } | 132 } |
127 | 133 |
128 /* at least one exponent must be given */ | 134 /* at least one exponent must be given */ |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 SECITEM_AllocItem(arena, &key->version, 1); | 279 SECITEM_AllocItem(arena, &key->version, 1); |
274 key->version.data[0] = 0; | 280 key->version.data[0] = 0; |
275 /* 3. Set the public exponent */ | 281 /* 3. Set the public exponent */ |
276 SECITEM_TO_MPINT(*publicExponent, &e); | 282 SECITEM_TO_MPINT(*publicExponent, &e); |
277 kiter = 0; | 283 kiter = 0; |
278 do { | 284 do { |
279 prerr = 0; | 285 prerr = 0; |
280 PORT_SetError(0); | 286 PORT_SetError(0); |
281 CHECK_SEC_OK( generate_prime(&p, primeLen) ); | 287 CHECK_SEC_OK( generate_prime(&p, primeLen) ); |
282 CHECK_SEC_OK( generate_prime(&q, primeLen) ); | 288 CHECK_SEC_OK( generate_prime(&q, primeLen) ); |
283 » /* Assure q < p */ | 289 » /* Assure p > q */ |
| 290 » /* NOTE: PKCS #1 does not require p > q, and NSS doesn't use any |
| 291 » * implementation optimization that requires p > q. We can remove |
| 292 » * this code in the future. |
| 293 » */ |
284 if (mp_cmp(&p, &q) < 0) | 294 if (mp_cmp(&p, &q) < 0) |
285 mp_exch(&p, &q); | 295 mp_exch(&p, &q); |
286 /* Attempt to use these primes to generate a key */ | 296 /* Attempt to use these primes to generate a key */ |
287 rv = rsa_build_from_primes(&p, &q, | 297 rv = rsa_build_from_primes(&p, &q, |
288 &e, PR_FALSE, /* needPublicExponent=false */ | 298 &e, PR_FALSE, /* needPublicExponent=false */ |
289 &d, PR_TRUE, /* needPrivateExponent=true */ | 299 &d, PR_TRUE, /* needPrivateExponent=true */ |
290 key, keySizeInBits); | 300 key, keySizeInBits); |
291 if (rv == SECSuccess) | 301 if (rv == SECSuccess) |
292 break; /* generated two good primes */ | 302 break; /* generated two good primes */ |
293 prerr = PORT_GetError(); | 303 prerr = PORT_GetError(); |
(...skipping 461 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
755 ((prime_count > 0) || hasModulus)) { | 765 ((prime_count > 0) || hasModulus)) { |
756 CHECK_MPI_OK(rsa_get_primes_from_exponents(&e,&d,&p,&q, | 766 CHECK_MPI_OK(rsa_get_primes_from_exponents(&e,&d,&p,&q, |
757 &n,hasModulus,keySizeInBits)); | 767 &n,hasModulus,keySizeInBits)); |
758 } else { | 768 } else { |
759 /* not enough given parameters to get both primes */ | 769 /* not enough given parameters to get both primes */ |
760 err = MP_BADARG; | 770 err = MP_BADARG; |
761 goto cleanup; | 771 goto cleanup; |
762 } | 772 } |
763 } | 773 } |
764 | 774 |
765 /* force p to the the larger prime */ | 775 /* Assure p > q */ |
| 776 /* NOTE: PKCS #1 does not require p > q, and NSS doesn't use any |
| 777 * implementation optimization that requires p > q. We can remove |
| 778 * this code in the future. |
| 779 */ |
766 if (mp_cmp(&p, &q) < 0) | 780 if (mp_cmp(&p, &q) < 0) |
767 mp_exch(&p, &q); | 781 mp_exch(&p, &q); |
768 | 782 |
769 /* we now have our 2 primes and at least one exponent, we can fill | 783 /* we now have our 2 primes and at least one exponent, we can fill |
770 * in the key */ | 784 * in the key */ |
771 rv = rsa_build_from_primes(&p, &q, | 785 rv = rsa_build_from_primes(&p, &q, |
772 &e, needPublicExponent, | 786 &e, needPublicExponent, |
773 &d, needPrivateExponent, | 787 &d, needPrivateExponent, |
774 key, keySizeInBits); | 788 key, keySizeInBits); |
775 cleanup: | 789 cleanup: |
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1086 | 1100 |
1087 return SECSuccess; | 1101 return SECSuccess; |
1088 } | 1102 } |
1089 | 1103 |
1090 static SECStatus | 1104 static SECStatus |
1091 get_blinding_params(RSAPrivateKey *key, mp_int *n, unsigned int modLen, | 1105 get_blinding_params(RSAPrivateKey *key, mp_int *n, unsigned int modLen, |
1092 mp_int *f, mp_int *g) | 1106 mp_int *f, mp_int *g) |
1093 { | 1107 { |
1094 RSABlindingParams *rsabp = NULL; | 1108 RSABlindingParams *rsabp = NULL; |
1095 blindingParams *bpUnlinked = NULL; | 1109 blindingParams *bpUnlinked = NULL; |
1096 blindingParams *bp, *prevbp = NULL; | 1110 blindingParams *bp; |
1097 PRCList *el; | 1111 PRCList *el; |
1098 SECStatus rv = SECSuccess; | 1112 SECStatus rv = SECSuccess; |
1099 mp_err err = MP_OKAY; | 1113 mp_err err = MP_OKAY; |
1100 int cmp = -1; | 1114 int cmp = -1; |
1101 PRBool holdingLock = PR_FALSE; | 1115 PRBool holdingLock = PR_FALSE; |
1102 | 1116 |
1103 do { | 1117 do { |
1104 if (blindingParamsList.lock == NULL) { | 1118 if (blindingParamsList.lock == NULL) { |
1105 PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); | 1119 PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
1106 return SECFailure; | 1120 return SECFailure; |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1176 */ | 1190 */ |
1177 if (blindingParamsList.waitCount > 0) { | 1191 if (blindingParamsList.waitCount > 0) { |
1178 PR_NotifyCondVar( blindingParamsList.cVar ); | 1192 PR_NotifyCondVar( blindingParamsList.cVar ); |
1179 blindingParamsList.waitCount--; | 1193 blindingParamsList.waitCount--; |
1180 } | 1194 } |
1181 PZ_Unlock(blindingParamsList.lock); | 1195 PZ_Unlock(blindingParamsList.lock); |
1182 return SECSuccess; | 1196 return SECSuccess; |
1183 } | 1197 } |
1184 /* We did not find a usable set of blinding params. Can we make one? */ | 1198 /* We did not find a usable set of blinding params. Can we make one? */ |
1185 /* Find a free bp struct. */ | 1199 /* Find a free bp struct. */ |
1186 prevbp = NULL; | |
1187 if ((bp = rsabp->free) != NULL) { | 1200 if ((bp = rsabp->free) != NULL) { |
1188 /* unlink this bp */ | 1201 /* unlink this bp */ |
1189 rsabp->free = bp->next; | 1202 rsabp->free = bp->next; |
1190 bp->next = NULL; | 1203 bp->next = NULL; |
1191 bpUnlinked = bp; /* In case we fail */ | 1204 bpUnlinked = bp; /* In case we fail */ |
1192 | 1205 |
1193 PZ_Unlock(blindingParamsList.lock); | 1206 PZ_Unlock(blindingParamsList.lock); |
1194 holdingLock = PR_FALSE; | 1207 holdingLock = PR_FALSE; |
1195 /* generate blinding parameter values for the current thread */ | 1208 /* generate blinding parameter values for the current thread */ |
1196 CHECK_SEC_OK( generate_blinding_params(key, f, g, n, modLen ) ); | 1209 CHECK_SEC_OK( generate_blinding_params(key, f, g, n, modLen ) ); |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1393 } | 1406 } |
1394 | 1407 |
1395 SECITEM_TO_MPINT(key->modulus, &n); | 1408 SECITEM_TO_MPINT(key->modulus, &n); |
1396 SECITEM_TO_MPINT(key->prime1, &p); | 1409 SECITEM_TO_MPINT(key->prime1, &p); |
1397 SECITEM_TO_MPINT(key->prime2, &q); | 1410 SECITEM_TO_MPINT(key->prime2, &q); |
1398 SECITEM_TO_MPINT(key->publicExponent, &e); | 1411 SECITEM_TO_MPINT(key->publicExponent, &e); |
1399 SECITEM_TO_MPINT(key->privateExponent, &d); | 1412 SECITEM_TO_MPINT(key->privateExponent, &d); |
1400 SECITEM_TO_MPINT(key->exponent1, &d_p); | 1413 SECITEM_TO_MPINT(key->exponent1, &d_p); |
1401 SECITEM_TO_MPINT(key->exponent2, &d_q); | 1414 SECITEM_TO_MPINT(key->exponent2, &d_q); |
1402 SECITEM_TO_MPINT(key->coefficient, &qInv); | 1415 SECITEM_TO_MPINT(key->coefficient, &qInv); |
1403 /* p > q */ | 1416 /* p and q must be distinct. */ |
1404 if (mp_cmp(&p, &q) <= 0) { | 1417 if (mp_cmp(&p, &q) == 0) { |
1405 rv = SECFailure; | 1418 rv = SECFailure; |
1406 goto cleanup; | 1419 goto cleanup; |
1407 } | 1420 } |
1408 #define VERIFY_MPI_EQUAL(m1, m2) \ | 1421 #define VERIFY_MPI_EQUAL(m1, m2) \ |
1409 if (mp_cmp(m1, m2) != 0) { \ | 1422 if (mp_cmp(m1, m2) != 0) { \ |
1410 rv = SECFailure; \ | 1423 rv = SECFailure; \ |
1411 goto cleanup; \ | 1424 goto cleanup; \ |
1412 } | 1425 } |
1413 #define VERIFY_MPI_EQUAL_1(m) \ | 1426 #define VERIFY_MPI_EQUAL_1(m) \ |
1414 if (mp_cmp_d(m, 1) != 0) { \ | 1427 if (mp_cmp_d(m, 1) != 0) { \ |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1532 PRBool bl_parentForkedAfterC_Initialize; | 1545 PRBool bl_parentForkedAfterC_Initialize; |
1533 | 1546 |
1534 /* | 1547 /* |
1535 * Set fork flag so it can be tested in SKIP_AFTER_FORK on relevant platforms. | 1548 * Set fork flag so it can be tested in SKIP_AFTER_FORK on relevant platforms. |
1536 */ | 1549 */ |
1537 void BL_SetForkState(PRBool forked) | 1550 void BL_SetForkState(PRBool forked) |
1538 { | 1551 { |
1539 bl_parentForkedAfterC_Initialize = forked; | 1552 bl_parentForkedAfterC_Initialize = forked; |
1540 } | 1553 } |
1541 | 1554 |
OLD | NEW |