4215bdd8d92f7f42fbcb277cc7c61dd09a2eb3b7
[openwrt.git] / package / libs / nettle / patches / 0002-Added-option-to-use-mini-gmp-in-hogweed.patch
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.
5
6 ---
7  Makefile.in              |  8 +++--
8  bignum.h                 |  6 +++-
9  configure.ac             | 76 +++++++++++++++++++++++++++++++++++-------------
10  dsa.h                    |  2 +-
11  ecc-internal.h           |  2 +-
12  ecc.h                    |  2 +-
13  eccdata.c                | 12 +++++++-
14  examples/Makefile.in     |  2 +-
15  examples/ecc-benchmark.c |  2 ++
16  gmp-glue.c               |  1 +
17  gmp-glue.h               |  2 +-
18  hogweed.pc.in            |  2 +-
19  mini-gmp.c               |  2 +-
20  mini-gmp.h               |  9 ++++++
21  pkcs1.h                  |  2 +-
22  rsa.h                    |  2 +-
23  testsuite/Makefile.in    |  4 +--
24  testsuite/testutils.h    |  5 ++--
25  18 files changed, 102 insertions(+), 39 deletions(-)
26
27 diff --git a/Makefile.in b/Makefile.in
28 index 3b0e1cb..42be3a9 100644
29 --- a/Makefile.in
30 +++ b/Makefile.in
31 @@ -16,6 +16,10 @@ MKDIR_P = @MKDIR_P@
32  
33  OPT_ASM_SOURCES = @OPT_ASM_SOURCES@
34  
35 +OPT_HOGWEED_SOURCES = @OPT_HOGWEED_SOURCES@
36 +
37 +OPT_HOGWEED_HEADERS = @OPT_HOGWEED_HEADERS@
38 +
39  SUBDIRS = tools testsuite examples
40  
41  include config.make
42 @@ -106,7 +110,7 @@ nettle_SOURCES = aes-decrypt-internal.c aes-decrypt.c \
43                  nettle-meta-armors.c \
44                  write-be32.c write-le32.c write-le64.c
45  
46 -hogweed_SOURCES = sexp.c sexp-format.c \
47 +hogweed_SOURCES = $(OPT_HOGWEED_SOURCES) sexp.c sexp-format.c \
48                   sexp-transport.c sexp-transport-format.c \
49                   bignum.c bignum-next-prime.c \
50                   bignum-random.c bignum-random-prime.c \
51 @@ -156,7 +160,7 @@ HEADERS = aes.h arcfour.h arctwo.h asn1.h bignum.h blowfish.h \
52           pgp.h pkcs1.h realloc.h ripemd160.h rsa.h rsa-compat.h \
53           salsa20.h sexp.h \
54           serpent.h sha.h sha1.h sha2.h sha3.h twofish.h \
55 -         umac.h yarrow.h
56 +         umac.h yarrow.h $(OPT_HOGWEED_HEADERS)
57  
58  INSTALL_HEADERS = $(HEADERS) nettle-stdint.h
59  
60 diff --git a/bignum.h b/bignum.h
61 index 746b21f..1f2ff07 100644
62 --- a/bignum.h
63 +++ b/bignum.h
64 @@ -28,7 +28,11 @@
65  
66  #include "nettle-meta.h"
67  
68 -#include <gmp.h>
69 +#ifdef USE_MINI_GMP
70 +# include "mini-gmp.h"
71 +#else
72 +# include <gmp.h>
73 +#endif
74  #include "nettle-types.h"
75  
76  #ifdef __cplusplus
77 diff --git a/configure.ac b/configure.ac
78 index 78a3d4e..93eb09a 100644
79 --- a/configure.ac
80 +++ b/configure.ac
81 @@ -72,6 +72,16 @@ AC_ARG_ENABLE(arm-neon,
82    AC_HELP_STRING([--enable-arm-neon], [Enable ARM Neon assembly. (default=auto)]),,
83    [enable_arm_neon=auto])
84  
85 +AC_ARG_ENABLE(mini-gmp,
86 +  AC_HELP_STRING([--enable-mini-gmp], [Disable gmp support and enable mini-gmp. (default=disabled)]),,
87 +  [enable_mini_gmp=no])
88 +
89 +if test "x$enable_mini_gmp" = xyes ; then
90 +GMP_LIBS=""
91 +else
92 +GMP_LIBS="-lgmp"
93 +fi
94 +
95  LSH_RPATH_INIT([`echo $with_lib_path | sed 's/:/ /g'` \
96      `echo $exec_prefix | sed "s@^NONE@$prefix/lib@g" | sed "s@^NONE@$ac_default_prefix/lib@g"` \
97      /usr/local/lib /sw/local/lib /sw/lib \
98 @@ -409,7 +419,7 @@ case "$host_os" in
99      LIBHOGWEED_SONAME='libhogweed.$(LIBHOGWEED_MAJOR).dylib'
100      LIBHOGWEED_FILE='libhogweed.$(LIBHOGWEED_MAJOR).$(LIBHOGWEED_MINOR).dylib'
101      LIBHOGWEED_LINK='$(CC) $(CFLAGS) -dynamiclib -L. $(LDFLAGS) -install_name ${libdir}/$(LIBHOGWEED_SONAME) -compatibility_version $(LIBHOGWEED_MAJOR) -current_version $(LIBHOGWEED_MAJOR).$(LIBHOGWEED_MINOR)'
102 -    LIBHOGWEED_LIBS='-lnettle -lgmp'
103 +    LIBHOGWEED_LIBS='-lnettle $(GMP_LIBS)'
104      ;;
105    solaris*)
106      # Sun's ld uses -h to set the soname, and this option is passed
107 @@ -425,7 +435,7 @@ case "$host_os" in
108      LIBHOGWEED_SONAME='$(LIBHOGWEED_FORLINK).$(LIBHOGWEED_MAJOR)'
109      LIBHOGWEED_FILE='$(LIBHOGWEED_SONAME).$(LIBHOGWEED_MINOR)'
110      LIBHOGWEED_LINK='$(CC) $(CFLAGS) $(LDFLAGS) -L. -G -h $(LIBHOGWEED_SONAME)'
111 -    LIBHOGWEED_LIBS='-lnettle -lgmp'
112 +    LIBHOGWEED_LIBS='-lnettle $(GMP_LIBS)'
113      ;;
114    *)
115      LIBNETTLE_FORLINK=libnettle.so
116 @@ -443,7 +453,7 @@ case "$host_os" in
117      # -lhogweed -lgmp -lnettle are still required). Also makes dlopen
118      # of libhogweed.so work, without having to use RTLD_GLOBAL.
119      # Depends on -L. above, to locate nettle.so.
120 -    LIBHOGWEED_LIBS='-lnettle -lgmp'
121 +    LIBHOGWEED_LIBS='-lnettle $(GMP_LIBS)'
122      ;;
123  esac
124  
125 @@ -644,32 +654,55 @@ if test "x$nettle_cv_fcntl_locking" = "xyes" ; then
126    AC_DEFINE(HAVE_FCNTL_LOCKING)
127  fi
128  
129 -# Checks for libraries
130 -AC_CHECK_LIB(gmp, __gmpz_getlimbn,,
131 -    [AC_MSG_WARN(
132 +TESTSUITE_LIBS=""
133 +
134 +OPT_HOGWEED_SOURCES=""
135 +OPT_HOGWEED_HEADERS=""
136 +if test "x$enable_mini_gmp" = xyes ; then
137 +  OPT_HOGWEED_SOURCES="$OPT_HOGWEED_SOURCES mini-gmp.c"
138 +  OPT_HOGWEED_HEADERS="$OPT_HOGWEED_HEADERS mini-gmp.h"
139 +  AC_DEFINE([USE_MINI_GMP], 1, [Use mini-gmp instead of gmp])
140 +  IF_NOT_MINI_GMP='#'
141 +  GMP_NUMB_BITS="0"
142 +else
143 +
144 +  # Checks for libraries
145 +  AC_CHECK_LIB(gmp, __gmpz_getlimbn,,
146 +      [AC_MSG_WARN(
147  [GNU MP not found, or not 3.1 or up, see http://gmplib.org/.
148  Support for public key algorithms will be unavailable.])]
149 -    enable_public_key=no)
150 +      enable_public_key=no)
151  
152 -# Add -R flags needed to run programs linked with gmp
153 -LSH_RPATH_FIX
154 +  # Add -R flags needed to run programs linked with gmp
155 +  LSH_RPATH_FIX
156  
157 -# Check for gmp limb size
158 -nettle_cv_gmp_numb_bits=0
159 -if test "$enable_public_key" = yes; then
160 -  AC_MSG_CHECKING([for GMP limb size])
161 -  AC_COMPUTE_INT(nettle_cv_gmp_numb_bits, [GMP_NUMB_BITS],
162 -      [#include <gmp.h>],
163 -      [AC_MSG_FAILURE([cannot find value of GMP_NUMB_BITS])])
164 +  # Check for gmp limb size
165 +  nettle_cv_gmp_numb_bits=0
166 +  if test "$enable_public_key" = yes; then
167 +    AC_MSG_CHECKING([for GMP limb size])
168 +    AC_COMPUTE_INT(nettle_cv_gmp_numb_bits, [GMP_NUMB_BITS],
169 +        [#include <gmp.h>],
170 +        [AC_MSG_FAILURE([cannot find value of GMP_NUMB_BITS])])
171  
172 -  AC_MSG_RESULT([$nettle_cv_gmp_numb_bits bits])
173 +    AC_MSG_RESULT([$nettle_cv_gmp_numb_bits bits])
174 +  fi
175 +
176 +  GMP_NUMB_BITS="$nettle_cv_gmp_numb_bits"
177 +
178 +  AH_TEMPLATE([HAVE_MPZ_POWM_SEC], [Define if mpz_powm_sec is available (appeared in GMP-5)])
179 +  AC_CHECK_FUNC(__gmpz_powm_sec, [AC_DEFINE(HAVE_MPZ_POWM_SEC)])
180 +
181 +  IF_NOT_MINI_GMP=''
182 +  TESTSUITE_LIBS="-lgmp"
183  fi
184  
185 -GMP_NUMB_BITS="$nettle_cv_gmp_numb_bits"
186 -AC_SUBST([GMP_NUMB_BITS])
187 +AC_SUBST([GMP_LIBS])
188  
189 -AH_TEMPLATE([HAVE_MPZ_POWM_SEC], [Define if mpz_powm_sec is available (appeared in GMP-5)])
190 -AC_CHECK_FUNC(__gmpz_powm_sec, [AC_DEFINE(HAVE_MPZ_POWM_SEC)])
191 +dnl The testsuite requires gmp
192 +AC_SUBST([TESTSUITE_LIBS])
193 +AC_SUBST([GMP_NUMB_BITS])
194 +AC_SUBST([OPT_HOGWEED_SOURCES])
195 +AC_SUBST([OPT_HOGWEED_HEADERS])
196  
197  AH_TEMPLATE([WITH_HOGWEED], [Defined if public key features are enabled])
198  
199 @@ -714,6 +747,7 @@ else
200    IF_DOCUMENTATION='#'
201  fi
202  
203 +AC_SUBST(IF_NOT_MINI_GMP)
204  AC_SUBST(IF_HOGWEED)
205  AC_SUBST(IF_STATIC)
206  AC_SUBST(IF_SHARED)
207 diff --git a/dsa.h b/dsa.h
208 index 7ee2624..478e4fc 100644
209 --- a/dsa.h
210 +++ b/dsa.h
211 @@ -26,7 +26,7 @@
212  #ifndef NETTLE_DSA_H_INCLUDED
213  #define NETTLE_DSA_H_INCLUDED
214  
215 -#include <gmp.h>
216 +#include "bignum.h"
217  
218  #include "nettle-types.h"
219  
220 diff --git a/ecc-internal.h b/ecc-internal.h
221 index e8974fe..5cdd0e1 100644
222 --- a/ecc-internal.h
223 +++ b/ecc-internal.h
224 @@ -25,7 +25,7 @@
225  #ifndef NETTLE_ECC_INTERNAL_H_INCLUDED
226  #define NETTLE_ECC_INTERNAL_H_INCLUDED
227  
228 -#include <gmp.h>
229 +#include "bignum.h"
230  
231  #include "nettle-types.h"
232  #include "ecc-curve.h"
233 diff --git a/ecc.h b/ecc.h
234 index 609d246..924f8ec 100644
235 --- a/ecc.h
236 +++ b/ecc.h
237 @@ -25,7 +25,7 @@
238  #ifndef NETTLE_ECC_H_INCLUDED
239  #define NETTLE_ECC_H_INCLUDED
240  
241 -#include <gmp.h>
242 +#include "bignum.h"
243  
244  #include "nettle-types.h"
245  
246 diff --git a/eccdata.c b/eccdata.c
247 index 466753c..24f64e3 100644
248 --- a/eccdata.c
249 +++ b/eccdata.c
250 @@ -24,11 +24,14 @@
251  
252  /* Development of Nettle's ECC support was funded by the .SE Internet Fund. */
253  
254 +#include "config.h"
255 +
256  #include <assert.h>
257  #include <stdio.h>
258  #include <stdlib.h>
259  #include <string.h>
260  
261 +#include "mini-gmp.h"
262  #include "mini-gmp.c"
263  
264  /* Affine coordinates, for simplicity. Infinity point represented as x
265 @@ -895,6 +898,7 @@ int
266  main (int argc, char **argv)
267  {
268    struct ecc_curve ecc;
269 +  unsigned bits;
270  
271    if (argc < 4)
272      {
273 @@ -902,6 +906,7 @@ main (int argc, char **argv)
274        return EXIT_FAILURE;
275      }
276  
277 +  bits = atoi(argv[4]);
278    ecc_curve_init (&ecc, atoi(argv[1]));
279  
280    ecc_pippenger_precompute (&ecc, atoi(argv[2]), atoi(argv[3]));
281 @@ -912,7 +917,12 @@ main (int argc, char **argv)
282    ecc_curve_check (&ecc);
283  
284    if (argc > 4)
285 -    output_curve (&ecc, atoi(argv[4]));
286 +    {
287 +      /* when using mini-gmp we cannot estimate it on configure */
288 +      if (bits == 0)
289 +        bits = GMP_NUMB_BITS;
290 +      output_curve (&ecc, bits);
291 +    }
292  
293    return EXIT_SUCCESS;
294  }
295 diff --git a/examples/Makefile.in b/examples/Makefile.in
296 index 563d0dc..2bb0436 100644
297 --- a/examples/Makefile.in
298 +++ b/examples/Makefile.in
299 @@ -11,7 +11,7 @@ PRE_CPPFLAGS = -I.. -I$(top_srcdir)
300  PRE_LDFLAGS = -L..
301  
302  OPENSSL_LIBFLAGS = @OPENSSL_LIBFLAGS@
303 -BENCH_LIBS = @BENCH_LIBS@ -lm
304 +BENCH_LIBS = @BENCH_LIBS@ $(TESTSUITE_LIBS) -lm
305  
306  HOGWEED_TARGETS = rsa-keygen$(EXEEXT) rsa-sign$(EXEEXT) \
307               rsa-verify$(EXEEXT) rsa-encrypt$(EXEEXT) rsa-decrypt$(EXEEXT) \
308 diff --git a/examples/ecc-benchmark.c b/examples/ecc-benchmark.c
309 index 1faf6dc..16a1fd6 100644
310 --- a/examples/ecc-benchmark.c
311 +++ b/examples/ecc-benchmark.c
312 @@ -37,6 +37,8 @@
313  
314  #include "timing.h"
315  
316 +#undef USE_MINI_GMP
317 +#include <gmp.h>
318  #include "../ecc.h"
319  #include "../ecc-internal.h"
320  #include "../gmp-glue.h"
321 diff --git a/gmp-glue.c b/gmp-glue.c
322 index a2633a5..d76fdea 100644
323 --- a/gmp-glue.c
324 +++ b/gmp-glue.c
325 @@ -239,3 +239,4 @@ gmp_free_limbs (mp_limb_t *p, mp_size_t n)
326  
327    free_func (p, (size_t) n * sizeof(mp_limb_t));
328  }
329 +
330 diff --git a/gmp-glue.h b/gmp-glue.h
331 index 269667f..f51b5b5 100644
332 --- a/gmp-glue.h
333 +++ b/gmp-glue.h
334 @@ -23,7 +23,7 @@
335  #ifndef NETTLE_GMP_GLUE_H_INCLUDED
336  #define NETTLE_GMP_GLUE_H_INCLUDED
337  
338 -#include <gmp.h>
339 +#include "bignum.h"
340  
341  #include "nettle-stdint.h"
342  
343 diff --git a/hogweed.pc.in b/hogweed.pc.in
344 index 457f5f2..9a9dea0 100644
345 --- a/hogweed.pc.in
346 +++ b/hogweed.pc.in
347 @@ -13,6 +13,6 @@ URL: http://www.lysator.liu.se/~nisse/nettle
348  Version: @PACKAGE_VERSION@
349  Requires.private: nettle
350  Libs: -L${libdir} -lhogweed
351 -Libs.private: -lgmp
352 +Libs.private: @GMP_LIBS@
353  Cflags: -I${includedir}
354  
355 diff --git a/mini-gmp.c b/mini-gmp.c
356 index 766df30..cf76a2a 100644
357 --- a/mini-gmp.c
358 +++ b/mini-gmp.c
359 @@ -1388,7 +1388,7 @@ mpz_clear (mpz_t r)
360    gmp_free (r->_mp_d);
361  }
362  
363 -static void *
364 +void *
365  mpz_realloc (mpz_t r, mp_size_t size)
366  {
367    size = GMP_MAX (size, 1);
368 diff --git a/mini-gmp.h b/mini-gmp.h
369 index d8f691f..dac7d1e 100644
370 --- a/mini-gmp.h
371 +++ b/mini-gmp.h
372 @@ -70,6 +70,11 @@ typedef struct
373    mp_limb_t *_mp_d;            /* Pointer to the limbs.  */
374  } __mpz_struct;
375  
376 +typedef __mpz_struct MP_INT;
377 +
378 +#define GMP_NUMB_BITS (SIZEOF_LONG*8)
379 +#define GMP_NUMB_MASK (~0)
380 +
381  typedef __mpz_struct mpz_t[1];
382  
383  typedef __mpz_struct *mpz_ptr;
384 @@ -119,6 +124,10 @@ void mpz_init (mpz_t);
385  void mpz_init2 (mpz_t, mp_bitcnt_t);
386  void mpz_clear (mpz_t);
387  
388 +void *
389 +mpz_realloc (mpz_t r, mp_size_t size);
390 +#define _mpz_realloc mpz_realloc
391 +
392  #define mpz_odd_p(z)   (((z)->_mp_size != 0) & (int) (z)->_mp_d[0])
393  #define mpz_even_p(z)  (! mpz_odd_p (z))
394  
395 diff --git a/pkcs1.h b/pkcs1.h
396 index fa27225..f402688 100644
397 --- a/pkcs1.h
398 +++ b/pkcs1.h
399 @@ -26,7 +26,7 @@
400  #ifndef NETTLE_PKCS1_H_INCLUDED
401  #define NETTLE_PKCS1_H_INCLUDED
402  
403 -#include <gmp.h>
404 +#include "bignum.h"
405  #include "nettle-types.h"
406  
407  #ifdef __cplusplus
408 diff --git a/rsa.h b/rsa.h
409 index 4226f38..c293cd1 100644
410 --- a/rsa.h
411 +++ b/rsa.h
412 @@ -26,7 +26,7 @@
413  #ifndef NETTLE_RSA_H_INCLUDED
414  #define NETTLE_RSA_H_INCLUDED
415  
416 -#include <gmp.h>
417 +#include "bignum.h"
418  #include "nettle-types.h"
419  
420  #include "md5.h"
421 diff --git a/testsuite/Makefile.in b/testsuite/Makefile.in
422 index 91f6e2a..c8ab5f6 100644
423 --- a/testsuite/Makefile.in
424 +++ b/testsuite/Makefile.in
425 @@ -47,7 +47,7 @@ TS_HOGWEED = $(TS_HOGWEED_SOURCES:.c=$(EXEEXT))
426  TS_C = $(TS_NETTLE) @IF_HOGWEED@ $(TS_HOGWEED)
427  TS_CXX = @IF_CXX@ $(CXX_SOURCES:.cxx=$(EXEEXT))
428  TARGETS = $(TS_C) $(TS_CXX)
429 -TS_SH = sexp-conv-test pkcs1-conv-test symbols-test
430 +TS_SH = sexp-conv-test pkcs1-conv-test @IF_NOT_MINI_GMP@ symbols-test
431  TS_ALL = $(TARGETS) $(TS_SH)
432  EXTRA_SOURCES = sha1-huge-test.c
433  EXTRA_TARGETS = $(EXTRA_SOURCES:.c=$(EXEEXT))
434 @@ -75,7 +75,7 @@ all: $(TARGETS) $(EXTRA_TARGETS)
435  
436  LIB_HOGWEED = @IF_HOGWEED@ -lhogweed
437  TEST_OBJS = testutils.$(OBJEXT) ../nettle-internal.$(OBJEXT) \
438 -           $(LIB_HOGWEED) -lnettle $(LIBS)
439 +           $(LIB_HOGWEED) -lnettle $(LIBS) $(TESTSUITE_LIBS)
440  
441  ../nettle-internal.$(OBJEXT):
442         ( cd .. && $(MAKE) nettle-internal.$(OBJEXT) )
443 diff --git a/testsuite/testutils.h b/testsuite/testutils.h
444 index 123bae2..a677a2a 100644
445 --- a/testsuite/testutils.h
446 +++ b/testsuite/testutils.h
447 @@ -12,11 +12,10 @@
448  #include <stdio.h>
449  #include <string.h>
450  
451 -#if HAVE_LIBGMP
452 -# include "bignum.h"
453 -#endif
454  
455  #if WITH_HOGWEED
456 +# include <gmp.h>
457 +# undef USE_MINI_GMP
458  # include "rsa.h"
459  # include "dsa.h"
460  # include "ecc-curve.h"
461 -- 
462 1.9.2
463