From: Nikos Mavrogiannopoulos Date: Thu, 26 May 2011 12:10:01 +0000 (+0200) Subject: Use nettle's functions for integer import/export. X-Git-Tag: gnutls_2_99_2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=19288b5d88ae012bf0db145a28f2eaccea72a6cf;p=thirdparty%2Fgnutls.git Use nettle's functions for integer import/export. --- diff --git a/lib/nettle/Makefile.am b/lib/nettle/Makefile.am index a4bd44cfc0..233ab306a3 100644 --- a/lib/nettle/Makefile.am +++ b/lib/nettle/Makefile.am @@ -39,4 +39,4 @@ libcrypto_la_SOURCES = pk.c mpi.c mac.c cipher.c rnd.c init.c egd.c egd.h \ ecc_test.c ecc_map.c \ ecc_mulmod.c ecc_points.c ecc_projective_dbl_point_3.c \ ecc_projective_add_point.c ecc_projective_dbl_point.c \ - mp_unsigned_bin.c ecc_sign_hash.c ecc_verify_hash.c + ecc_sign_hash.c ecc_verify_hash.c diff --git a/lib/nettle/ecc.h b/lib/nettle/ecc.h index 2a7ce3d359..07a882c3fd 100644 --- a/lib/nettle/ecc.h +++ b/lib/nettle/ecc.h @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -120,9 +121,6 @@ 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, ...); -unsigned long mp_unsigned_bin_size(mpz_t a); -int mp_to_unsigned_bin(mpz_t a, unsigned char *b); -int mp_read_unsigned_bin(mpz_t a, unsigned char *b, unsigned long len); #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_make_key.c b/lib/nettle/ecc_make_key.c index 08af1bcef7..3667a5bbd6 100644 --- a/lib/nettle/ecc_make_key.c +++ b/lib/nettle/ecc_make_key.c @@ -45,7 +45,7 @@ ecc_make_key_ex (void *random_ctx, nettle_random_func random, ecc_key * key, assert (key != NULL); assert (random != NULL); - keysize = mp_unsigned_bin_size (order); + keysize = nettle_mpz_sizeinbase_256_u (order); /* allocate ram */ base = NULL; @@ -83,11 +83,8 @@ ecc_make_key_ex (void *random_ctx, nettle_random_func random, ecc_key * key, mpz_set (base->x, key->Gx); mpz_set (base->y, key->Gy); mpz_set_ui (base->z, 1); - if ((err = - mp_read_unsigned_bin (key->k, (unsigned char *) buf, keysize)) != 0) - { - goto errkey; - } + + nettle_mpz_set_str_256_u (key->k, keysize, buf); /* the key should be smaller than the order of base point */ if (mpz_cmp (key->k, key->order) >= 0) diff --git a/lib/nettle/ecc_shared_secret.c b/lib/nettle/ecc_shared_secret.c index 62074190b7..c229870b51 100644 --- a/lib/nettle/ecc_shared_secret.c +++ b/lib/nettle/ecc_shared_secret.c @@ -63,7 +63,7 @@ ecc_shared_secret (ecc_key * private_key, ecc_key * public_key, goto done; } - x = (unsigned long) mp_unsigned_bin_size (private_key->prime); + x = nettle_mpz_sizeinbase_256_u (private_key->prime); if (*outlen < x) { *outlen = x; @@ -71,13 +71,7 @@ ecc_shared_secret (ecc_key * private_key, ecc_key * public_key, goto done; } memset (out, 0, x); - if ((err = - mp_to_unsigned_bin (result->x, - out + (x - mp_unsigned_bin_size (result->x)))) != - 0) - { - goto done; - } + nettle_mpz_get_str_256(x, out + (x - nettle_mpz_sizeinbase_256_u (result->x)), result->x); err = 0; *outlen = x; diff --git a/lib/nettle/ecc_sign_hash.c b/lib/nettle/ecc_sign_hash.c index 4111610912..12be36dc72 100644 --- a/lib/nettle/ecc_sign_hash.c +++ b/lib/nettle/ecc_sign_hash.c @@ -57,11 +57,8 @@ ecc_sign_hash (const unsigned char *in, unsigned long inlen, { return err; } - if ((err = - mp_read_unsigned_bin (e, (unsigned char *) in, (int) inlen)) != 0) - { - goto errnokey; - } + + nettle_mpz_set_str_256_u (e, inlen, in); /* make up a key and export the public copy */ for (;;) diff --git a/lib/nettle/ecc_verify_hash.c b/lib/nettle/ecc_verify_hash.c index df66927c33..62efae02d2 100644 --- a/lib/nettle/ecc_verify_hash.c +++ b/lib/nettle/ecc_verify_hash.c @@ -82,11 +82,7 @@ ecc_verify_hash (struct dsa_signature *signature, } /* read hash */ - if ((err = - mp_read_unsigned_bin (e, (unsigned char *) hash, (int) hashlen)) != 0) - { - goto error; - } + nettle_mpz_set_str_256_u (e, hashlen, hash); /* w = s^-1 mod n */ mpz_invert (w, signature->s, key->order); diff --git a/lib/nettle/mp_unsigned_bin.c b/lib/nettle/mp_unsigned_bin.c deleted file mode 100644 index 0da8bba32e..0000000000 --- a/lib/nettle/mp_unsigned_bin.c +++ /dev/null @@ -1,28 +0,0 @@ -#include "ecc.h" - -unsigned long mp_unsigned_bin_size(mpz_t a) -{ - unsigned long t; - assert(a != NULL); - - t = mpz_sizeinbase(a, 2); - if (mpz_cmp_ui((a), 0) == 0) return 0; - return (t>>3) + ((t&7)?1:0); -} - -int mp_to_unsigned_bin(mpz_t a, unsigned char *b) -{ - assert(a != NULL); - assert(b != NULL); - mpz_export(b, NULL, 1, 1, 1, 0, a); - - return 0; -} - -int mp_read_unsigned_bin(mpz_t a, unsigned char *b, unsigned long len) -{ - assert(a != NULL); - assert(b != NULL); - mpz_import(a, len, 1, 1, 1, 0, b); - return 0; -}