diff options
author | Nikos Mavrogiannopoulos <nmav@gnutls.org> | 2011-11-05 09:11:06 +0100 |
---|---|---|
committer | Nikos Mavrogiannopoulos <nmav@gnutls.org> | 2011-11-05 09:11:06 +0100 |
commit | 2dd78b367c3c01780615f2a06329611092430f63 (patch) | |
tree | d68eecea01aa5a808f6ba3f1931d52050d3d5906 /lib/nettle | |
parent | 28602012b231b2836214b2239936d6b06f9e7769 (diff) | |
download | gnutls-2dd78b367c3c01780615f2a06329611092430f63.tar.gz |
converted more things to native gmp. This solves issue noticed in mips64 by Joseph Graham.
Diffstat (limited to 'lib/nettle')
-rw-r--r-- | lib/nettle/ecc.h | 3 | ||||
-rw-r--r-- | lib/nettle/ecc_mulmod.c | 24 | ||||
-rw-r--r-- | lib/nettle/ecc_projective_add_point.c | 2 | ||||
-rw-r--r-- | lib/nettle/ecc_projective_dbl_point.c | 2 | ||||
-rw-r--r-- | lib/nettle/ecc_projective_dbl_point_3.c | 2 | ||||
-rw-r--r-- | lib/nettle/ecc_test.c | 142 |
6 files changed, 151 insertions, 24 deletions
diff --git a/lib/nettle/ecc.h b/lib/nettle/ecc.h index caa465b75b..ea2b837488 100644 --- a/lib/nettle/ecc.h +++ b/lib/nettle/ecc.h @@ -118,6 +118,3 @@ int ecc_map(ecc_point *P, mpz_t modulus); /* helper functions */ int mp_init_multi(mpz_t *a, ...); void mp_clear_multi(mpz_t *a, ...); -#define mp_isodd(a) (mpz_size(a) > 0 ? (mpz_getlimbn(a, 0) & 1 ? 1 : 0) : 0) - -#define MP_DIGIT_BIT (sizeof(mp_limb_t) * 8 - GMP_NAIL_BITS) diff --git a/lib/nettle/ecc_mulmod.c b/lib/nettle/ecc_mulmod.c index e9eebe3f5b..05762ea755 100644 --- a/lib/nettle/ecc_mulmod.c +++ b/lib/nettle/ecc_mulmod.c @@ -45,6 +45,7 @@ ecc_mulmod (mpz_t k, ecc_point * G, ecc_point * R, mpz_t a, mpz_t modulus, { ecc_point *tG, *M[3]; int i, j, err; + int bit_to_read; unsigned long buf; int bitcnt, mode, digidx; @@ -91,29 +92,16 @@ ecc_mulmod (mpz_t k, ecc_point * G, ecc_point * R, mpz_t a, mpz_t modulus, /* setup sliding window */ mode = 0; - bitcnt = 1; - buf = 0; - digidx = mpz_size (k) - 1; + bit_to_read = mpz_size (k) * GMP_NUMB_BITS - 1; /* perform ops */ for (;;) { /* grab next digit as required */ - if (--bitcnt == 0) - { - if (digidx == -1) - { - break; - } - buf = mpz_getlimbn (k, digidx); - bitcnt = (int) MP_DIGIT_BIT; - --digidx; - } - - /* grab the next msb from the ltiplicand */ - i = (buf >> (MP_DIGIT_BIT - 1)) & 1; - buf <<= 1; - + if (bit_to_read == -1) + break; + i = mpz_tstbit (k, bit_to_read--); + if (mode == 0 && i == 0) { /* dummy operations */ diff --git a/lib/nettle/ecc_projective_add_point.c b/lib/nettle/ecc_projective_add_point.c index 292a0a3486..6e8d599d01 100644 --- a/lib/nettle/ecc_projective_add_point.c +++ b/lib/nettle/ecc_projective_add_point.c @@ -207,7 +207,7 @@ ecc_projective_add_point (ecc_point * P, ecc_point * Q, ecc_point * R, mpz_add (y, y, modulus); } /* Y = Y/2 */ - if (mp_isodd (y)) + if (mpz_odd_p (y)) { mpz_add (y, y, modulus); } diff --git a/lib/nettle/ecc_projective_dbl_point.c b/lib/nettle/ecc_projective_dbl_point.c index 4128062e17..2df4e52769 100644 --- a/lib/nettle/ecc_projective_dbl_point.c +++ b/lib/nettle/ecc_projective_dbl_point.c @@ -156,7 +156,7 @@ ecc_projective_dbl_point (ecc_point * P, ecc_point * R, mpz_t a, mpz_mod (R->y, R->y, modulus); /* Y = 8y^4 */ - if (mp_isodd (R->y)) + if (mpz_odd_p (R->y)) { mpz_add (R->y, R->y, modulus); } diff --git a/lib/nettle/ecc_projective_dbl_point_3.c b/lib/nettle/ecc_projective_dbl_point_3.c index e25c612afa..64e1cf9e24 100644 --- a/lib/nettle/ecc_projective_dbl_point_3.c +++ b/lib/nettle/ecc_projective_dbl_point_3.c @@ -107,7 +107,7 @@ ecc_projective_dbl_point (ecc_point * P, ecc_point * R, mpz_t a /* a is -3 */, mpz_mul(t2, R->y, R->y); mpz_mod(t2, t2, modulus); /* T2 = T2/2 */ - if (mp_isodd(t2)) { + if (mpz_odd_p(t2)) { mpz_add(t2, t2, modulus); } mpz_divexact_ui(t2, t2, 2); diff --git a/lib/nettle/ecc_test.c b/lib/nettle/ecc_test.c new file mode 100644 index 0000000000..30250faf3a --- /dev/null +++ b/lib/nettle/ecc_test.c @@ -0,0 +1,142 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org + */ + +/* Implements ECC over Z/pZ for curve y^2 = x^3 + ax + b + * + * All curves taken from NIST recommendation paper of July 1999 + * Available at http://csrc.nist.gov/cryptval/dss.htm + */ +#include "ecc.h" +#include "gnettle.h" +#include <gnutls_int.h> +#include <algorithms.h> + +/** + @file ecc_test.c + ECC Crypto, Tom St Denis +*/ + +/** + Perform on the ECC system + @return 0 if successful +*/ +int +ecc_test (void) +{ + mpz_t modulus, order, A; + ecc_point *G, *GG; + int i, err; + + if ((err = mp_init_multi (&modulus, &A, &order, NULL)) != 0) + { + return err; + } + + G = ecc_new_point (); + GG = ecc_new_point (); + if (G == NULL || GG == NULL) + { + mp_clear_multi (&modulus, &order, NULL); + ecc_del_point (G); + ecc_del_point (GG); + return -1; + } + + for (i = 1; i <= 3; i++) + { + const gnutls_ecc_curve_entry_st *st = _gnutls_ecc_curve_get_params (i); + + printf ("Testing %s (%d)\n", gnutls_ecc_curve_get_name (i), i); + + if (mpz_set_str (A, (char *) st->A, 16) != 0) + { + fprintf (stderr, "XXX %d\n", __LINE__); + err = -1; + goto done; + } + + if (mpz_set_str (modulus, (char *) st->prime, 16) != 0) + { + fprintf (stderr, "XXX %d\n", __LINE__); + err = -1; + goto done; + } + + if (mpz_set_str (order, (char *) st->order, 16) != 0) + { + fprintf (stderr, "XXX %d\n", __LINE__); + err = -1; + goto done; + } + + /* is prime actually prime? */ + if ((err = mpz_probab_prime_p (modulus, PRIME_CHECK_PARAM)) <= 0) + { + fprintf (stderr, "XXX %d\n", __LINE__); + err = -1; + goto done; + } + + if ((err = mpz_probab_prime_p (order, PRIME_CHECK_PARAM)) <= 0) + { + fprintf (stderr, "XXX %d\n", __LINE__); + err = -1; + goto done; + } + + if (mpz_set_str (G->x, (char *) st->Gx, 16) != 0) + { + fprintf (stderr, "XXX %d\n", __LINE__); + err = -1; + goto done; + } + + if (mpz_set_str (G->y, (char *) st->Gy, 16) != 0) + { + fprintf (stderr, "XXX %d\n", __LINE__); + err = -1; + goto done; + } + mpz_set_ui (G->z, 1); + + /* then we should have G == (order + 1)G */ + mpz_add_ui (order, order, 1); + if ((err = ecc_mulmod (order, G, GG, A, modulus, 1)) != 0) + { + goto done; + } + + if (mpz_cmp (G->y, GG->y) != 0) + { + fprintf (stderr, "XXX %d\n", __LINE__); + err = -1; + goto done; + } + + if (mpz_cmp (G->x, GG->x) != 0) + { + fprintf (stderr, "XXX %d\n", __LINE__); + err = -1; + goto done; + } + + } + err = 0; +done: + ecc_del_point (GG); + ecc_del_point (G); + mp_clear_multi (&order, &modulus, &A, NULL); + return err; +} + +/* $Source: /cvs/libtom/libtomcrypt/src/pk/ecc/ecc_test.c,v $ */ +/* $Revision: 1.12 $ */ +/* $Date: 2007/05/12 14:32:35 $ */ |