call extended euclidean algorithm directly instead of using mp_invmod

This commit is contained in:
mcgreer%netscape.com 2000-09-07 03:14:16 +00:00
Родитель 5a560e9d15
Коммит 47641a2f5f
1 изменённых файлов: 8 добавлений и 13 удалений

Просмотреть файл

@ -30,7 +30,7 @@
* may use your version of this file under either the MPL or the
* GPL.
*
* $Id: rsa.c,v 1.3 2000-09-06 23:58:41 mcgreer%netscape.com Exp $
* $Id: rsa.c,v 1.4 2000-09-07 03:14:16 mcgreer%netscape.com Exp $
*/
#include "prerr.h"
@ -126,16 +126,6 @@ retry:
CHECK_MPI_OK( mp_read_unsigned_octets(&q, qb, primeLen) );
CHECK_MPI_OK( mpp_make_prime(&p, primeLen * 8, PR_FALSE, &counter) );
CHECK_MPI_OK( mpp_make_prime(&q, primeLen * 8, PR_FALSE, &counter) );
/* Verify that (p-1) and e have no common divisors (PKCS1 v1.5) */
CHECK_MPI_OK( mp_sub_d(&p, 1, &psub1) );
CHECK_MPI_OK( mp_gcd(&psub1, &e, &tmp) );
if (mp_cmp_d(&tmp, 1) > 0)
goto retry;
/* Verify that (q-1) and e have no common divisors (PKCS1 v1.5) */
CHECK_MPI_OK( mp_sub_d(&q, 1, &qsub1) );
CHECK_MPI_OK( mp_gcd(&qsub1, &e, &tmp) );
if (mp_cmp_d(&tmp, 1) > 0)
goto retry;
MPINT_TO_SECITEM(&p, &key->prime1, arena);
MPINT_TO_SECITEM(&q, &key->prime2, arena);
/* 5. Compute n = p*q */
@ -143,13 +133,18 @@ retry:
MPINT_TO_SECITEM(&n, &key->modulus, arena);
/* 6. Compute phi = (p-1)*(q-1) */
CHECK_MPI_OK( mp_mul(&psub1, &qsub1, &phi) );
/* 7. Compute d = e**-1 mod(phi) */
CHECK_MPI_OK( mp_invmod(&e, &phi, &d) );
/* 7. Compute d = e**-1 mod(phi) using extended Euclidean algorithm */
CHECK_MPI_OK( mp_xgcd(&e, &phi, &tmp, &d, NULL) );
/* Verify that phi(n) and e have no common divisors */
if (mp_cmp_d(&tmp, 1) != 0)
goto retry;
MPINT_TO_SECITEM(&d, &key->privateExponent, arena);
/* 8. Compute exponent1 = d mod (p-1) */
CHECK_MPI_OK( mp_sub_d(&p, 1, &psub1) );
CHECK_MPI_OK( mp_mod(&d, &psub1, &tmp) );
MPINT_TO_SECITEM(&tmp, &key->exponent1, arena);
/* 9. Compute exponent2 = d mod (q-1) */
CHECK_MPI_OK( mp_sub_d(&q, 1, &qsub1) );
CHECK_MPI_OK( mp_mod(&d, &qsub1, &tmp) );
MPINT_TO_SECITEM(&tmp, &key->exponent2, arena);
/*10. Compute coefficient = q**-1 mod p */