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 909 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
920 CHECK_MPI_OK( mp_init(&d_q) ); | 920 CHECK_MPI_OK( mp_init(&d_q) ); |
921 CHECK_MPI_OK( mp_init(&qInv) ); | 921 CHECK_MPI_OK( mp_init(&qInv) ); |
922 CHECK_MPI_OK( mp_init(&m1) ); | 922 CHECK_MPI_OK( mp_init(&m1) ); |
923 CHECK_MPI_OK( mp_init(&m2) ); | 923 CHECK_MPI_OK( mp_init(&m2) ); |
924 CHECK_MPI_OK( mp_init(&h) ); | 924 CHECK_MPI_OK( mp_init(&h) ); |
925 CHECK_MPI_OK( mp_init(&ctmp) ); | 925 CHECK_MPI_OK( mp_init(&ctmp) ); |
926 /* copy private key parameters into mp integers */ | 926 /* copy private key parameters into mp integers */ |
927 SECITEM_TO_MPINT(key->prime1, &p); /* p */ | 927 SECITEM_TO_MPINT(key->prime1, &p); /* p */ |
928 SECITEM_TO_MPINT(key->prime2, &q); /* q */ | 928 SECITEM_TO_MPINT(key->prime2, &q); /* q */ |
929 SECITEM_TO_MPINT(key->exponent1, &d_p); /* d_p = d mod (p-1) */ | 929 SECITEM_TO_MPINT(key->exponent1, &d_p); /* d_p = d mod (p-1) */ |
930 SECITEM_TO_MPINT(key->exponent2, &d_q); /* d_q = d mod (q-1) */ | 930 SECITEM_TO_MPINT(key->exponent2, &d_q); /* d_q = d mod (q-1) */ |
wtc
2014/06/11 19:35:24
Another option is to do the p > q check here and s
| |
931 SECITEM_TO_MPINT(key->coefficient, &qInv); /* qInv = q**-1 mod p */ | 931 SECITEM_TO_MPINT(key->coefficient, &qInv); /* qInv = q**-1 mod p */ |
932 /* 1. m1 = c**d_p mod p */ | 932 /* 1. m1 = c**d_p mod p */ |
933 CHECK_MPI_OK( mp_mod(c, &p, &ctmp) ); | 933 CHECK_MPI_OK( mp_mod(c, &p, &ctmp) ); |
934 CHECK_MPI_OK( mp_exptmod(&ctmp, &d_p, &p, &m1) ); | 934 CHECK_MPI_OK( mp_exptmod(&ctmp, &d_p, &p, &m1) ); |
935 /* 2. m2 = c**d_q mod q */ | 935 /* 2. m2 = c**d_q mod q */ |
936 CHECK_MPI_OK( mp_mod(c, &q, &ctmp) ); | 936 CHECK_MPI_OK( mp_mod(c, &q, &ctmp) ); |
937 CHECK_MPI_OK( mp_exptmod(&ctmp, &d_q, &q, &m2) ); | 937 CHECK_MPI_OK( mp_exptmod(&ctmp, &d_q, &q, &m2) ); |
938 /* 3. h = (m1 - m2) * qInv mod p */ | 938 /* 3. h = (m1 - m2) * qInv mod p */ |
939 CHECK_MPI_OK( mp_submod(&m1, &m2, &p, &h) ); | 939 CHECK_MPI_OK( mp_submod(&m1, &m2, &p, &h) ); |
940 CHECK_MPI_OK( mp_mulmod(&h, &qInv, &p, &h) ); | 940 CHECK_MPI_OK( mp_mulmod(&h, &qInv, &p, &h) ); |
(...skipping 405 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 /* The qInv check depends on 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 /* mind the p's and q's (and d_p's and d_q's) */ |
1431 SECItem tmp; | |
1432 mp_exch(&p, &q); | 1406 mp_exch(&p, &q); |
1433 mp_exch(&d_p,&d_q); | 1407 mp_exch(&d_p,&d_q); |
Ryan Sleevi
2014/06/11 19:11:16
This now means we're allowing invalid qInvs. I sus
wtc
2014/06/11 19:35:24
I see. We can also just delete this p > q check (l
Ryan Sleevi
2014/06/11 19:41:13
We could, but it seems like that'd be more computa
| |
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 } | 1408 } |
1441 #define VERIFY_MPI_EQUAL(m1, m2) \ | 1409 #define VERIFY_MPI_EQUAL(m1, m2) \ |
1442 if (mp_cmp(m1, m2) != 0) { \ | 1410 if (mp_cmp(m1, m2) != 0) { \ |
1443 rv = SECFailure; \ | 1411 rv = SECFailure; \ |
1444 goto cleanup; \ | 1412 goto cleanup; \ |
1445 } | 1413 } |
1446 #define VERIFY_MPI_EQUAL_1(m) \ | 1414 #define VERIFY_MPI_EQUAL_1(m) \ |
1447 if (mp_cmp_d(m, 1) != 0) { \ | 1415 if (mp_cmp_d(m, 1) != 0) { \ |
1448 rv = SECFailure; \ | 1416 rv = SECFailure; \ |
1449 goto cleanup; \ | 1417 goto cleanup; \ |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1572 PRBool bl_parentForkedAfterC_Initialize; | 1540 PRBool bl_parentForkedAfterC_Initialize; |
1573 | 1541 |
1574 /* | 1542 /* |
1575 * Set fork flag so it can be tested in SKIP_AFTER_FORK on relevant platforms. | 1543 * Set fork flag so it can be tested in SKIP_AFTER_FORK on relevant platforms. |
1576 */ | 1544 */ |
1577 void BL_SetForkState(PRBool forked) | 1545 void BL_SetForkState(PRBool forked) |
1578 { | 1546 { |
1579 bl_parentForkedAfterC_Initialize = forked; | 1547 bl_parentForkedAfterC_Initialize = forked; |
1580 } | 1548 } |
1581 | 1549 |
OLD | NEW |