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 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 RSAPrivateKey * | 232 RSAPrivateKey * |
233 RSA_NewKey(int keySizeInBits, SECItem *publicExponent) | 233 RSA_NewKey(int keySizeInBits, SECItem *publicExponent) |
234 { | 234 { |
235 unsigned int primeLen; | 235 unsigned int primeLen; |
236 mp_int p, q, e, d; | 236 mp_int p, q, e, d; |
237 int kiter; | 237 int kiter; |
238 mp_err err = MP_OKAY; | 238 mp_err err = MP_OKAY; |
239 SECStatus rv = SECSuccess; | 239 SECStatus rv = SECSuccess; |
240 int prerr = 0; | 240 int prerr = 0; |
241 RSAPrivateKey *key = NULL; | 241 RSAPrivateKey *key = NULL; |
242 PRArenaPool *arena = NULL; | 242 PLArenaPool *arena = NULL; |
243 /* Require key size to be a multiple of 16 bits. */ | 243 /* Require key size to be a multiple of 16 bits. */ |
244 if (!publicExponent || keySizeInBits % 16 != 0 || | 244 if (!publicExponent || keySizeInBits % 16 != 0 || |
245 BAD_RSA_KEY_SIZE(keySizeInBits/8, publicExponent->len)) { | 245 BAD_RSA_KEY_SIZE(keySizeInBits/8, publicExponent->len)) { |
246 PORT_SetError(SEC_ERROR_INVALID_ARGS); | 246 PORT_SetError(SEC_ERROR_INVALID_ARGS); |
247 return NULL; | 247 return NULL; |
248 } | 248 } |
249 /* 1. Allocate arena & key */ | 249 /* 1. Allocate arena & key */ |
250 arena = PORT_NewArena(NSS_FREEBL_DEFAULT_CHUNKSIZE); | 250 arena = PORT_NewArena(NSS_FREEBL_DEFAULT_CHUNKSIZE); |
251 if (!arena) { | 251 if (!arena) { |
252 PORT_SetError(SEC_ERROR_NO_MEMORY); | 252 PORT_SetError(SEC_ERROR_NO_MEMORY); |
253 return NULL; | 253 return NULL; |
254 } | 254 } |
255 key = PORT_ArenaZNew(arena, RSAPrivateKey); | 255 key = PORT_ArenaZNew(arena, RSAPrivateKey); |
256 if (!key) { | 256 if (!key) { |
257 PORT_SetError(SEC_ERROR_NO_MEMORY); | 257 PORT_SetError(SEC_ERROR_NO_MEMORY); |
258 PORT_FreeArena(arena, PR_TRUE); | 258 PORT_FreeArena(arena, PR_TRUE); |
259 return NULL; | 259 return NULL; |
260 } | 260 } |
261 key->arena = arena; | 261 key->arena = arena; |
262 /* length of primes p and q (in bytes) */ | 262 /* length of primes p and q (in bytes) */ |
263 primeLen = keySizeInBits / (2 * BITS_PER_BYTE); | 263 primeLen = keySizeInBits / (2 * PR_BITS_PER_BYTE); |
264 MP_DIGITS(&p) = 0; | 264 MP_DIGITS(&p) = 0; |
265 MP_DIGITS(&q) = 0; | 265 MP_DIGITS(&q) = 0; |
266 MP_DIGITS(&e) = 0; | 266 MP_DIGITS(&e) = 0; |
267 MP_DIGITS(&d) = 0; | 267 MP_DIGITS(&d) = 0; |
268 CHECK_MPI_OK( mp_init(&p) ); | 268 CHECK_MPI_OK( mp_init(&p) ); |
269 CHECK_MPI_OK( mp_init(&q) ); | 269 CHECK_MPI_OK( mp_init(&q) ); |
270 CHECK_MPI_OK( mp_init(&e) ); | 270 CHECK_MPI_OK( mp_init(&e) ); |
271 CHECK_MPI_OK( mp_init(&d) ); | 271 CHECK_MPI_OK( mp_init(&d) ); |
272 /* 2. Set the version number (PKCS1 v1.5 says it should be zero) */ | 272 /* 2. Set the version number (PKCS1 v1.5 says it should be zero) */ |
273 SECITEM_AllocItem(arena, &key->version, 1); | 273 SECITEM_AllocItem(arena, &key->version, 1); |
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
646 * phi = n - s + 1, s = n - phi +1. Now that we have s = p+q and n=pq, | 646 * phi = n - s + 1, s = n - phi +1. Now that we have s = p+q and n=pq, |
647 * we can solve our 2 equations and 2 unknowns as follows: q=s-p -> | 647 * we can solve our 2 equations and 2 unknowns as follows: q=s-p -> |
648 * n=p*(s-p)= sp -p^2 -> p^2-sp+n = 0. Using the quadratic to solve for | 648 * n=p*(s-p)= sp -p^2 -> p^2-sp+n = 0. Using the quadratic to solve for |
649 * p, p=1/2*(s+ sqrt(s*s-4*n)) [q=1/2*(s-sqrt(s*s-4*n)]. We again have | 649 * p, p=1/2*(s+ sqrt(s*s-4*n)) [q=1/2*(s-sqrt(s*s-4*n)]. We again have |
650 * 2 primes and an exponent. | 650 * 2 primes and an exponent. |
651 * | 651 * |
652 */ | 652 */ |
653 SECStatus | 653 SECStatus |
654 RSA_PopulatePrivateKey(RSAPrivateKey *key) | 654 RSA_PopulatePrivateKey(RSAPrivateKey *key) |
655 { | 655 { |
656 PRArenaPool *arena = NULL; | 656 PLArenaPool *arena = NULL; |
657 PRBool needPublicExponent = PR_TRUE; | 657 PRBool needPublicExponent = PR_TRUE; |
658 PRBool needPrivateExponent = PR_TRUE; | 658 PRBool needPrivateExponent = PR_TRUE; |
659 PRBool hasModulus = PR_FALSE; | 659 PRBool hasModulus = PR_FALSE; |
660 unsigned int keySizeInBits = 0; | 660 unsigned int keySizeInBits = 0; |
661 int prime_count = 0; | 661 int prime_count = 0; |
662 /* standard RSA nominclature */ | 662 /* standard RSA nominclature */ |
663 mp_int p, q, e, d, n; | 663 mp_int p, q, e, d, n; |
664 /* remainder */ | 664 /* remainder */ |
665 mp_int r; | 665 mp_int r; |
666 mp_err err = 0; | 666 mp_err err = 0; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
705 | 705 |
706 /* load up the known primes. If only one prime is given, it will be | 706 /* load up the known primes. If only one prime is given, it will be |
707 * assigned 'p'. Once we have both primes, well make sure p is the larger. | 707 * assigned 'p'. Once we have both primes, well make sure p is the larger. |
708 * The value prime_count tells us howe many we have acquired. | 708 * The value prime_count tells us howe many we have acquired. |
709 */ | 709 */ |
710 if (key->prime1.data) { | 710 if (key->prime1.data) { |
711 int primeLen = key->prime1.len; | 711 int primeLen = key->prime1.len; |
712 if (key->prime1.data[0] == 0) { | 712 if (key->prime1.data[0] == 0) { |
713 primeLen--; | 713 primeLen--; |
714 } | 714 } |
715 » keySizeInBits = primeLen * 2 * BITS_PER_BYTE; | 715 » keySizeInBits = primeLen * 2 * PR_BITS_PER_BYTE; |
716 SECITEM_TO_MPINT(key->prime1, &p); | 716 SECITEM_TO_MPINT(key->prime1, &p); |
717 prime_count++; | 717 prime_count++; |
718 } | 718 } |
719 if (key->prime2.data) { | 719 if (key->prime2.data) { |
720 int primeLen = key->prime2.len; | 720 int primeLen = key->prime2.len; |
721 if (key->prime2.data[0] == 0) { | 721 if (key->prime2.data[0] == 0) { |
722 primeLen--; | 722 primeLen--; |
723 } | 723 } |
724 » keySizeInBits = primeLen * 2 * BITS_PER_BYTE; | 724 » keySizeInBits = primeLen * 2 * PR_BITS_PER_BYTE; |
725 SECITEM_TO_MPINT(key->prime2, prime_count ? &q : &p); | 725 SECITEM_TO_MPINT(key->prime2, prime_count ? &q : &p); |
726 prime_count++; | 726 prime_count++; |
727 } | 727 } |
728 /* load up the modulus */ | 728 /* load up the modulus */ |
729 if (key->modulus.data) { | 729 if (key->modulus.data) { |
730 int modLen = key->modulus.len; | 730 int modLen = key->modulus.len; |
731 if (key->modulus.data[0] == 0) { | 731 if (key->modulus.data[0] == 0) { |
732 modLen--; | 732 modLen--; |
733 } | 733 } |
734 » keySizeInBits = modLen * BITS_PER_BYTE; | 734 » keySizeInBits = modLen * PR_BITS_PER_BYTE; |
735 SECITEM_TO_MPINT(key->modulus, &n); | 735 SECITEM_TO_MPINT(key->modulus, &n); |
736 hasModulus = PR_TRUE; | 736 hasModulus = PR_TRUE; |
737 } | 737 } |
738 /* if we have the modulus and one prime, calculate the second. */ | 738 /* if we have the modulus and one prime, calculate the second. */ |
739 if ((prime_count == 1) && (hasModulus)) { | 739 if ((prime_count == 1) && (hasModulus)) { |
740 mp_div(&n,&p,&q,&r); | 740 mp_div(&n,&p,&q,&r); |
741 if (mp_cmp_z(&r) != 0) { | 741 if (mp_cmp_z(&r) != 0) { |
742 /* p is not a factor or n, fail */ | 742 /* p is not a factor or n, fail */ |
743 err = MP_BADARG; | 743 err = MP_BADARG; |
744 goto cleanup; | 744 goto cleanup; |
(...skipping 602 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 | 1356 static SECStatus |
1357 swap_in_key_value(PRArenaPool *arena, mp_int *mpval, SECItem *buffer) | 1357 swap_in_key_value(PLArenaPool *arena, mp_int *mpval, SECItem *buffer) |
1358 { | 1358 { |
1359 int len; | 1359 int len; |
1360 mp_err err = MP_OKAY; | 1360 mp_err err = MP_OKAY; |
1361 memset(buffer->data, 0, buffer->len); | 1361 memset(buffer->data, 0, buffer->len); |
1362 len = mp_unsigned_octet_size(mpval); | 1362 len = mp_unsigned_octet_size(mpval); |
1363 if (len <= 0) return SECFailure; | 1363 if (len <= 0) return SECFailure; |
1364 if ((unsigned int)len <= buffer->len) { | 1364 if ((unsigned int)len <= buffer->len) { |
1365 /* The new value is no longer than the old buffer, so use it */ | 1365 /* The new value is no longer than the old buffer, so use it */ |
1366 err = mp_to_unsigned_octets(mpval, buffer->data, len); | 1366 err = mp_to_unsigned_octets(mpval, buffer->data, len); |
1367 if (err >= 0) err = MP_OKAY; | 1367 if (err >= 0) err = MP_OKAY; |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1569 PRBool bl_parentForkedAfterC_Initialize; | 1569 PRBool bl_parentForkedAfterC_Initialize; |
1570 | 1570 |
1571 /* | 1571 /* |
1572 * Set fork flag so it can be tested in SKIP_AFTER_FORK on relevant platforms. | 1572 * Set fork flag so it can be tested in SKIP_AFTER_FORK on relevant platforms. |
1573 */ | 1573 */ |
1574 void BL_SetForkState(PRBool forked) | 1574 void BL_SetForkState(PRBool forked) |
1575 { | 1575 { |
1576 bl_parentForkedAfterC_Initialize = forked; | 1576 bl_parentForkedAfterC_Initialize = forked; |
1577 } | 1577 } |
1578 | 1578 |
OLD | NEW |