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 1335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1346 } | 1346 } |
1347 | 1347 |
1348 SECStatus | 1348 SECStatus |
1349 RSA_PrivateKeyOpDoubleChecked(RSAPrivateKey *key, | 1349 RSA_PrivateKeyOpDoubleChecked(RSAPrivateKey *key, |
1350 unsigned char *output, | 1350 unsigned char *output, |
1351 const unsigned char *input) | 1351 const unsigned char *input) |
1352 { | 1352 { |
1353 return rsa_PrivateKeyOp(key, output, input, PR_TRUE); | 1353 return rsa_PrivateKeyOp(key, output, input, PR_TRUE); |
1354 } | 1354 } |
1355 | 1355 |
1356 static SECStatus | |
1357 swap_in_key_value(PLArenaPool *arena, mp_int *mpval, SECItem *buffer) | |
1358 { | |
1359 int len; | |
1360 mp_err err = MP_OKAY; | |
1361 memset(buffer->data, 0, buffer->len); | |
1362 len = mp_unsigned_octet_size(mpval); | |
1363 if (len <= 0) return SECFailure; | |
1364 if ((unsigned int)len <= buffer->len) { | |
1365 /* The new value is no longer than the old buffer, so use it */ | |
1366 err = mp_to_unsigned_octets(mpval, buffer->data, len); | |
1367 if (err >= 0) err = MP_OKAY; | |
1368 buffer->len = len; | |
1369 } else if (arena) { | |
1370 /* The new value is longer, but working within an arena */ | |
1371 (void)SECITEM_AllocItem(arena, buffer, len); | |
1372 err = mp_to_unsigned_octets(mpval, buffer->data, len); | |
1373 if (err >= 0) err = MP_OKAY; | |
1374 } else { | |
1375 /* The new value is longer, no arena, can't handle this key */ | |
1376 return SECFailure; | |
1377 } | |
1378 return (err == MP_OKAY) ? SECSuccess : SECFailure; | |
1379 } | |
1380 | |
1381 SECStatus | 1356 SECStatus |
1382 RSA_PrivateKeyCheck(RSAPrivateKey *key) | 1357 RSA_PrivateKeyCheck(const RSAPrivateKey *key) |
1383 { | 1358 { |
1384 mp_int p, q, n, psub1, qsub1, e, d, d_p, d_q, qInv, res; | 1359 mp_int p, q, n, psub1, qsub1, e, d, d_p, d_q, qInv, res; |
1385 mp_err err = MP_OKAY; | 1360 mp_err err = MP_OKAY; |
1386 SECStatus rv = SECSuccess; | 1361 SECStatus rv = SECSuccess; |
1387 MP_DIGITS(&p) = 0; | 1362 MP_DIGITS(&p) = 0; |
1388 MP_DIGITS(&q) = 0; | 1363 MP_DIGITS(&q) = 0; |
1389 MP_DIGITS(&n) = 0; | 1364 MP_DIGITS(&n) = 0; |
1390 MP_DIGITS(&psub1)= 0; | 1365 MP_DIGITS(&psub1)= 0; |
1391 MP_DIGITS(&qsub1)= 0; | 1366 MP_DIGITS(&qsub1)= 0; |
1392 MP_DIGITS(&e) = 0; | 1367 MP_DIGITS(&e) = 0; |
(...skipping 25 matching lines...) Expand all Loading... |
1418 } | 1393 } |
1419 | 1394 |
1420 SECITEM_TO_MPINT(key->modulus, &n); | 1395 SECITEM_TO_MPINT(key->modulus, &n); |
1421 SECITEM_TO_MPINT(key->prime1, &p); | 1396 SECITEM_TO_MPINT(key->prime1, &p); |
1422 SECITEM_TO_MPINT(key->prime2, &q); | 1397 SECITEM_TO_MPINT(key->prime2, &q); |
1423 SECITEM_TO_MPINT(key->publicExponent, &e); | 1398 SECITEM_TO_MPINT(key->publicExponent, &e); |
1424 SECITEM_TO_MPINT(key->privateExponent, &d); | 1399 SECITEM_TO_MPINT(key->privateExponent, &d); |
1425 SECITEM_TO_MPINT(key->exponent1, &d_p); | 1400 SECITEM_TO_MPINT(key->exponent1, &d_p); |
1426 SECITEM_TO_MPINT(key->exponent2, &d_q); | 1401 SECITEM_TO_MPINT(key->exponent2, &d_q); |
1427 SECITEM_TO_MPINT(key->coefficient, &qInv); | 1402 SECITEM_TO_MPINT(key->coefficient, &qInv); |
1428 /* p > q */ | 1403 /* p > q */ |
1429 if (mp_cmp(&p, &q) <= 0) { | 1404 if (mp_cmp(&p, &q) <= 0) { |
1430 » /* mind the p's and q's (and d_p's and d_q's) */ | 1405 » rv = SECFailure; |
1431 » SECItem tmp; | 1406 » goto cleanup; |
1432 » mp_exch(&p, &q); | |
1433 » mp_exch(&d_p,&d_q); | |
1434 » tmp = key->prime1; | |
1435 » key->prime1 = key->prime2; | |
1436 » key->prime2 = tmp; | |
1437 » tmp = key->exponent1; | |
1438 » key->exponent1 = key->exponent2; | |
1439 » key->exponent2 = tmp; | |
1440 } | 1407 } |
1441 #define VERIFY_MPI_EQUAL(m1, m2) \ | 1408 #define VERIFY_MPI_EQUAL(m1, m2) \ |
1442 if (mp_cmp(m1, m2) != 0) { \ | 1409 if (mp_cmp(m1, m2) != 0) { \ |
1443 rv = SECFailure; \ | 1410 rv = SECFailure; \ |
1444 goto cleanup; \ | 1411 goto cleanup; \ |
1445 } | 1412 } |
1446 #define VERIFY_MPI_EQUAL_1(m) \ | 1413 #define VERIFY_MPI_EQUAL_1(m) \ |
1447 if (mp_cmp_d(m, 1) != 0) { \ | 1414 if (mp_cmp_d(m, 1) != 0) { \ |
1448 rv = SECFailure; \ | 1415 rv = SECFailure; \ |
1449 goto cleanup; \ | 1416 goto cleanup; \ |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1572 PRBool bl_parentForkedAfterC_Initialize; | 1539 PRBool bl_parentForkedAfterC_Initialize; |
1573 | 1540 |
1574 /* | 1541 /* |
1575 * Set fork flag so it can be tested in SKIP_AFTER_FORK on relevant platforms. | 1542 * Set fork flag so it can be tested in SKIP_AFTER_FORK on relevant platforms. |
1576 */ | 1543 */ |
1577 void BL_SetForkState(PRBool forked) | 1544 void BL_SetForkState(PRBool forked) |
1578 { | 1545 { |
1579 bl_parentForkedAfterC_Initialize = forked; | 1546 bl_parentForkedAfterC_Initialize = forked; |
1580 } | 1547 } |
1581 | 1548 |
OLD | NEW |