]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
Use nettle's functions for integer import/export.
authorNikos Mavrogiannopoulos <nmav@gnutls.org>
Thu, 26 May 2011 12:10:01 +0000 (14:10 +0200)
committerNikos Mavrogiannopoulos <nmav@gnutls.org>
Thu, 26 May 2011 16:36:27 +0000 (18:36 +0200)
lib/nettle/Makefile.am
lib/nettle/ecc.h
lib/nettle/ecc_make_key.c
lib/nettle/ecc_shared_secret.c
lib/nettle/ecc_sign_hash.c
lib/nettle/ecc_verify_hash.c
lib/nettle/mp_unsigned_bin.c [deleted file]

index a4bd44cfc0f9145b459ceacf708ea7dcf048aecd..233ab306a3eb039aa4588efd5c583d1422b0bc70 100644 (file)
@@ -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 
index 2a7ce3d359dc8f14d1bd262c677f46c954fe331b..07a882c3fd7dc0043489f5890f194fd7ef713744 100644 (file)
@@ -1,6 +1,7 @@
 #include <gmp.h>
 #include <nettle/nettle-types.h>
 #include <nettle/dsa.h>
+#include <nettle/bignum.h>
 #include <stdlib.h>
 #include <stdint.h>
 #include <string.h>
@@ -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)
index 08af1bcef704ddaf7bfee94fd702b8f28921a881..3667a5bbd654c1adcdaf4d1a339c3df8e4abb5cb 100644 (file)
@@ -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)
index 62074190b7fc11cd7961edae0c816e5c5f34d0dc..c229870b51c43f474193d71541905ac7867f791d 100644 (file)
@@ -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;
index 4111610912f179a05c458ee421bcc60cab995dbe..12be36dc725eb2a7e6db634f41f050eb34a32bfc 100644 (file)
@@ -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 (;;)
index df66927c3323dc71e6cf95576ead278943f3b4ae..62efae02d2ce60c17387b51c53761590295c3b6f 100644 (file)
@@ -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 (file)
index 0da8bba..0000000
+++ /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;
-}