1 From 8d5360e3e5eea3c9dbfd44f9b981f18a41605e45 Mon Sep 17 00:00:00 2001
2 From: Nikos Mavrogiannopoulos <nmav@gnutls.org>
3 Date: Sun, 9 Mar 2014 11:30:25 +0100
4 Subject: [PATCH 2/5] Added option to use mini-gmp in hogweed.
9 configure.ac | 76 +++++++++++++++++++++++++++++++++++-------------
13 eccdata.c | 12 +++++++-
14 examples/Makefile.in | 2 +-
15 examples/ecc-benchmark.c | 2 ++
23 testsuite/Makefile.in | 4 +--
24 testsuite/testutils.h | 5 ++--
25 18 files changed, 102 insertions(+), 39 deletions(-)
29 @@ -15,6 +15,10 @@ MKDIR_P = @MKDIR_P@
30 OPT_ASM_NETTLE_SOURCES = @OPT_ASM_NETTLE_SOURCES@
31 OPT_ASM_HOGWEED_SOURCES = @OPT_ASM_HOGWEED_SOURCES@
33 +OPT_HOGWEED_SOURCES = @OPT_HOGWEED_SOURCES@
35 +OPT_HOGWEED_HEADERS = @OPT_HOGWEED_HEADERS@
37 SUBDIRS = tools testsuite examples
40 @@ -132,7 +136,7 @@ nettle_SOURCES = aes-decrypt-internal.c
41 write-be32.c write-le32.c write-le64.c \
42 yarrow256.c yarrow_key_event.c
44 -hogweed_SOURCES = sexp.c sexp-format.c \
45 +hogweed_SOURCES = $(OPT_HOGWEED_SOURCES) sexp.c sexp-format.c \
46 sexp-transport.c sexp-transport-format.c \
47 bignum.c bignum-random.c bignum-random-prime.c \
49 @@ -183,7 +187,7 @@ HEADERS = aes.h arcfour.h arctwo.h asn1.
50 pgp.h pkcs1.h realloc.h ripemd160.h rsa.h \
52 serpent.h sha.h sha1.h sha2.h sha3.h twofish.h \
53 - umac.h yarrow.h poly1305.h
54 + umac.h yarrow.h poly1305.h $(OPT_HOGWEED_HEADERS)
56 INSTALL_HEADERS = $(HEADERS) nettle-stdint.h
62 #include "nettle-meta.h"
66 +# include "mini-gmp.h"
70 #include "nettle-types.h"
75 @@ -72,6 +72,16 @@ AC_ARG_ENABLE(arm-neon,
76 AC_HELP_STRING([--enable-arm-neon], [Enable ARM Neon assembly. (default=auto)]),,
77 [enable_arm_neon=auto])
79 +AC_ARG_ENABLE(mini-gmp,
80 + AC_HELP_STRING([--enable-mini-gmp], [Disable gmp support and enable mini-gmp. (default=disabled)]),,
81 + [enable_mini_gmp=no])
83 +if test "x$enable_mini_gmp" = xyes ; then
89 LSH_RPATH_INIT([`echo $with_lib_path | sed 's/:/ /g'` \
90 `echo $exec_prefix | sed "s@^NONE@$prefix/lib@g" | sed "s@^NONE@$ac_default_prefix/lib@g"` \
91 /usr/local/lib /sw/local/lib /sw/lib \
92 @@ -441,7 +451,7 @@ case "$host_os" in
93 LIBHOGWEED_SONAME='libhogweed.$(LIBHOGWEED_MAJOR).dylib'
94 LIBHOGWEED_FILE='libhogweed.$(LIBHOGWEED_MAJOR).$(LIBHOGWEED_MINOR).dylib'
95 LIBHOGWEED_LINK='$(CC) $(CFLAGS) -dynamiclib -L. $(LDFLAGS) -install_name ${libdir}/$(LIBHOGWEED_SONAME) -compatibility_version $(LIBHOGWEED_MAJOR) -current_version $(LIBHOGWEED_MAJOR).$(LIBHOGWEED_MINOR)'
96 - LIBHOGWEED_LIBS='-lnettle -lgmp'
97 + LIBHOGWEED_LIBS='-lnettle $(GMP_LIBS)'
100 # Sun's ld uses -h to set the soname, and this option is passed
101 @@ -457,7 +467,7 @@ case "$host_os" in
102 LIBHOGWEED_SONAME='$(LIBHOGWEED_FORLINK).$(LIBHOGWEED_MAJOR)'
103 LIBHOGWEED_FILE='$(LIBHOGWEED_SONAME).$(LIBHOGWEED_MINOR)'
104 LIBHOGWEED_LINK='$(CC) $(CFLAGS) $(LDFLAGS) -G -h $(LIBHOGWEED_SONAME)'
105 - LIBHOGWEED_LIBS='libnettle.so -lgmp'
106 + LIBHOGWEED_LIBS='libnettle.so $(GMP_LIBS)'
109 LIBNETTLE_FORLINK=libnettle.so
110 @@ -474,7 +484,7 @@ case "$host_os" in
111 # (does not work in general, e.g., with static linking all of
112 # -lhogweed -lgmp -lnettle are still required). Also makes dlopen
113 # of libhogweed.so work, without having to use RTLD_GLOBAL.
114 - LIBHOGWEED_LIBS='libnettle.so -lgmp'
115 + LIBHOGWEED_LIBS='libnettle.so $(GMP_LIBS)'
119 @@ -692,7 +702,18 @@ if test "x$nettle_cv_fcntl_locking" = "x
122 # Checks for libraries
125 +OPT_HOGWEED_SOURCES=""
126 +OPT_HOGWEED_HEADERS=""
127 if test "x$enable_public_key" = "xyes" ; then
128 +if test "x$enable_mini_gmp" = xyes ; then
129 + OPT_HOGWEED_SOURCES="$OPT_HOGWEED_SOURCES mini-gmp.c"
130 + OPT_HOGWEED_HEADERS="$OPT_HOGWEED_HEADERS mini-gmp.h"
131 + AC_DEFINE([USE_MINI_GMP], 1, [Use mini-gmp instead of gmp])
132 + IF_NOT_MINI_GMP='#'
135 AC_CHECK_LIB(gmp, __gmpz_getlimbn,,
137 [GNU MP not found, or not 3.1 or up, see http://gmplib.org/.
138 @@ -701,9 +722,7 @@ if test "x$enable_public_key" = "xyes" ;
140 # Add -R flags needed to run programs linked with gmp
144 -if test "x$enable_public_key" = "xyes" ; then
145 # Check for gmp limb size
146 nettle_cv_gmp_numb_bits=0
147 if test "$enable_public_key" = yes; then
148 @@ -714,12 +733,23 @@ if test "x$enable_public_key" = "xyes" ;
150 AC_MSG_RESULT([$nettle_cv_gmp_numb_bits bits])
154 GMP_NUMB_BITS="$nettle_cv_gmp_numb_bits"
155 AC_SUBST([GMP_NUMB_BITS])
157 AH_TEMPLATE([HAVE_MPZ_POWM_SEC], [Define if mpz_powm_sec is available (appeared in GMP-5)])
158 AC_CHECK_FUNC(__gmpz_powm_sec, [AC_DEFINE(HAVE_MPZ_POWM_SEC)])
160 + TESTSUITE_LIBS="-lgmp"
162 + AC_SUBST([GMP_LIBS])
163 +dnl The testsuite requires gmp
164 +AC_SUBST([TESTSUITE_LIBS])
165 +AC_SUBST([GMP_NUMB_BITS])
166 +AC_SUBST([OPT_HOGWEED_SOURCES])
167 +AC_SUBST([OPT_HOGWEED_HEADERS])
171 AH_TEMPLATE([WITH_HOGWEED], [Defined if public key features are enabled])
172 @@ -765,6 +795,7 @@ else
176 +AC_SUBST(IF_NOT_MINI_GMP)
183 #ifndef NETTLE_DSA_H_INCLUDED
184 #define NETTLE_DSA_H_INCLUDED
189 #include "nettle-types.h"
194 #ifndef NETTLE_ECC_INTERNAL_H_INCLUDED
195 #define NETTLE_ECC_INTERNAL_H_INCLUDED
200 #include "nettle-types.h"
201 #include "ecc-curve.h"
205 #ifndef NETTLE_ECC_H_INCLUDED
206 #define NETTLE_ECC_H_INCLUDED
211 #include "nettle-types.h"
217 /* Development of Nettle's ECC support was funded by the .SE Internet Fund. */
226 +#include "mini-gmp.h"
227 #include "mini-gmp.c"
229 /* Affine coordinates, for simplicity. Infinity point represented as x
230 @@ -904,6 +907,7 @@ int
231 main (int argc, char **argv)
233 struct ecc_curve ecc;
238 @@ -911,6 +915,7 @@ main (int argc, char **argv)
242 + bits = atoi(argv[4]);
243 ecc_curve_init (&ecc, atoi(argv[1]));
245 ecc_pippenger_precompute (&ecc, atoi(argv[2]), atoi(argv[3]));
246 @@ -921,7 +926,12 @@ main (int argc, char **argv)
247 ecc_curve_check (&ecc);
250 - output_curve (&ecc, atoi(argv[4]));
252 + /* when using mini-gmp we cannot estimate it on configure */
254 + bits = GMP_NUMB_BITS;
255 + output_curve (&ecc, bits);
260 --- a/examples/Makefile.in
261 +++ b/examples/Makefile.in
262 @@ -11,7 +11,7 @@ PRE_CPPFLAGS = -I.. -I$(top_srcdir)
265 OPENSSL_LIBFLAGS = @OPENSSL_LIBFLAGS@
266 -BENCH_LIBS = @BENCH_LIBS@ -lm
267 +BENCH_LIBS = @BENCH_LIBS@ $(TESTSUITE_LIBS) -lm
269 HOGWEED_TARGETS = rsa-keygen$(EXEEXT) rsa-sign$(EXEEXT) \
270 rsa-verify$(EXEEXT) rsa-encrypt$(EXEEXT) rsa-decrypt$(EXEEXT) \
271 --- a/examples/ecc-benchmark.c
272 +++ b/examples/ecc-benchmark.c
280 #include "../ecc-internal.h"
281 #include "../gmp-glue.h"
284 @@ -271,3 +271,4 @@ gmp_free(void *p, size_t n)
286 free_func (p, (size_t) n);
292 #ifndef NETTLE_GMP_GLUE_H_INCLUDED
293 #define NETTLE_GMP_GLUE_H_INCLUDED
298 #include "nettle-stdint.h"
302 @@ -13,6 +13,6 @@ URL: http://www.lysator.liu.se/~nisse/ne
303 Version: @PACKAGE_VERSION@
304 Requires.private: nettle
305 Libs: -L${libdir} -lhogweed
307 +Libs.private: @GMP_LIBS@
308 Cflags: -I${includedir}
312 @@ -1388,7 +1388,7 @@ mpz_clear (mpz_t r)
318 mpz_realloc (mpz_t r, mp_size_t size)
320 size = GMP_MAX (size, 1);
323 @@ -70,6 +70,11 @@ typedef struct
324 mp_limb_t *_mp_d; /* Pointer to the limbs. */
327 +typedef __mpz_struct MP_INT;
329 +#define GMP_NUMB_BITS (SIZEOF_LONG*8)
330 +#define GMP_NUMB_MASK (~0)
332 typedef __mpz_struct mpz_t[1];
334 typedef __mpz_struct *mpz_ptr;
335 @@ -119,6 +124,10 @@ void mpz_init (mpz_t);
336 void mpz_init2 (mpz_t, mp_bitcnt_t);
337 void mpz_clear (mpz_t);
340 +mpz_realloc (mpz_t r, mp_size_t size);
341 +#define _mpz_realloc mpz_realloc
343 #define mpz_odd_p(z) (((z)->_mp_size != 0) & (int) (z)->_mp_d[0])
344 #define mpz_even_p(z) (! mpz_odd_p (z))
349 #ifndef NETTLE_PKCS1_H_INCLUDED
350 #define NETTLE_PKCS1_H_INCLUDED
354 #include "nettle-types.h"
360 #ifndef NETTLE_RSA_H_INCLUDED
361 #define NETTLE_RSA_H_INCLUDED
365 #include "nettle-types.h"
368 --- a/testsuite/Makefile.in
369 +++ b/testsuite/Makefile.in
370 @@ -50,7 +50,7 @@ TS_HOGWEED = $(TS_HOGWEED_SOURCES:.c=$(E
371 TS_C = $(TS_NETTLE) @IF_HOGWEED@ $(TS_HOGWEED)
372 TS_CXX = @IF_CXX@ $(CXX_SOURCES:.cxx=$(EXEEXT))
373 TARGETS = $(TS_C) $(TS_CXX)
374 -TS_SH = sexp-conv-test pkcs1-conv-test nettle-pbkdf2-test symbols-test
375 +TS_SH = sexp-conv-test pkcs1-conv-test nettle-pbkdf2-test symbols-test @IF_NOT_MINI_GMP@
376 TS_ALL = $(TARGETS) $(TS_SH)
377 EXTRA_SOURCES = sha1-huge-test.c
378 EXTRA_TARGETS = $(EXTRA_SOURCES:.c=$(EXEEXT))
379 @@ -78,7 +78,7 @@ all: $(EXTRA_TARGETS)
381 LIB_HOGWEED = @IF_HOGWEED@ -lhogweed
382 TEST_OBJS = testutils.$(OBJEXT) ../nettle-internal.$(OBJEXT) \
383 - $(LIB_HOGWEED) -lnettle $(LIBS)
384 + $(LIB_HOGWEED) -lnettle $(LIBS) $(TESTSUITE_LIBS)
386 ../nettle-internal.$(OBJEXT):
387 ( cd .. && $(MAKE) nettle-internal.$(OBJEXT) )
388 --- a/testsuite/testutils.h
389 +++ b/testsuite/testutils.h
395 -# include "bignum.h"
400 +# undef USE_MINI_GMP
402 # include "dsa-compat.h"
403 # include "ecc-curve.h"