nettle: import from packages, update to 3.0
[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 --- a/Makefile.in
28 +++ b/Makefile.in
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@
32  
33 +OPT_HOGWEED_SOURCES = @OPT_HOGWEED_SOURCES@
34 +
35 +OPT_HOGWEED_HEADERS = @OPT_HOGWEED_HEADERS@
36 +
37  SUBDIRS = tools testsuite examples
38  
39  include config.make
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
43  
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 \
48                   sexp2bignum.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 \
51           salsa20.h sexp.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)
55  
56  INSTALL_HEADERS = $(HEADERS) nettle-stdint.h
57  
58 --- a/bignum.h
59 +++ b/bignum.h
60 @@ -36,7 +36,11 @@
61  
62  #include "nettle-meta.h"
63  
64 -#include <gmp.h>
65 +#ifdef USE_MINI_GMP
66 +# include "mini-gmp.h"
67 +#else
68 +# include <gmp.h>
69 +#endif
70  #include "nettle-types.h"
71  
72  #ifdef __cplusplus
73 --- a/configure.ac
74 +++ b/configure.ac
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])
78  
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])
82 +
83 +if test "x$enable_mini_gmp" = xyes ; then
84 +GMP_LIBS=""
85 +else
86 +GMP_LIBS="-lgmp"
87 +fi
88 +
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)'
98      ;;
99    solaris*)
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)'
107      ;;
108    *)
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)'
116      ;;
117  esac
118  
119 @@ -692,7 +702,18 @@ if test "x$nettle_cv_fcntl_locking" = "x
120  fi
121  
122  # Checks for libraries
123 +TESTSUITE_LIBS=""
124 +
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='#'
133 +  GMP_NUMB_BITS="0"  
134 +else
135    AC_CHECK_LIB(gmp, __gmpz_getlimbn,,
136        [AC_MSG_WARN(
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" ;
139  
140    # Add -R flags needed to run programs linked with gmp
141    LSH_RPATH_FIX
142 -fi
143  
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" ;
149    
150      AC_MSG_RESULT([$nettle_cv_gmp_numb_bits bits])
151    fi
152 +
153    
154    GMP_NUMB_BITS="$nettle_cv_gmp_numb_bits"
155    AC_SUBST([GMP_NUMB_BITS])
156    
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)])
159 +  IF_NOT_MINI_GMP=''
160 +  TESTSUITE_LIBS="-lgmp"
161 +fi
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])
168 +
169  fi
170  
171  AH_TEMPLATE([WITH_HOGWEED], [Defined if public key features are enabled])
172 @@ -765,6 +795,7 @@ else
173    IF_DOCUMENTATION='#'
174  fi
175  
176 +AC_SUBST(IF_NOT_MINI_GMP)
177  AC_SUBST(IF_HOGWEED)
178  AC_SUBST(IF_STATIC)
179  AC_SUBST(IF_SHARED)
180 --- a/dsa.h
181 +++ b/dsa.h
182 @@ -34,7 +34,7 @@
183  #ifndef NETTLE_DSA_H_INCLUDED
184  #define NETTLE_DSA_H_INCLUDED
185  
186 -#include <gmp.h>
187 +#include "bignum.h"
188  
189  #include "nettle-types.h"
190  
191 --- a/ecc-internal.h
192 +++ b/ecc-internal.h
193 @@ -34,7 +34,7 @@
194  #ifndef NETTLE_ECC_INTERNAL_H_INCLUDED
195  #define NETTLE_ECC_INTERNAL_H_INCLUDED
196  
197 -#include <gmp.h>
198 +#include "bignum.h"
199  
200  #include "nettle-types.h"
201  #include "ecc-curve.h"
202 --- a/ecc.h
203 +++ b/ecc.h
204 @@ -34,7 +34,7 @@
205  #ifndef NETTLE_ECC_H_INCLUDED
206  #define NETTLE_ECC_H_INCLUDED
207  
208 -#include <gmp.h>
209 +#include "bignum.h"
210  
211  #include "nettle-types.h"
212  
213 --- a/eccdata.c
214 +++ b/eccdata.c
215 @@ -33,11 +33,14 @@
216  
217  /* Development of Nettle's ECC support was funded by the .SE Internet Fund. */
218  
219 +#include "config.h"
220 +
221  #include <assert.h>
222  #include <stdio.h>
223  #include <stdlib.h>
224  #include <string.h>
225  
226 +#include "mini-gmp.h"
227  #include "mini-gmp.c"
228  
229  /* Affine coordinates, for simplicity. Infinity point represented as x
230 @@ -904,6 +907,7 @@ int
231  main (int argc, char **argv)
232  {
233    struct ecc_curve ecc;
234 +  unsigned bits;
235  
236    if (argc < 4)
237      {
238 @@ -911,6 +915,7 @@ main (int argc, char **argv)
239        return EXIT_FAILURE;
240      }
241  
242 +  bits = atoi(argv[4]);
243    ecc_curve_init (&ecc, atoi(argv[1]));
244  
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);
248  
249    if (argc > 4)
250 -    output_curve (&ecc, atoi(argv[4]));
251 +    {
252 +      /* when using mini-gmp we cannot estimate it on configure */
253 +      if (bits == 0)
254 +        bits = GMP_NUMB_BITS;
255 +      output_curve (&ecc, bits);
256 +    }
257  
258    return EXIT_SUCCESS;
259  }
260 --- a/examples/Makefile.in
261 +++ b/examples/Makefile.in
262 @@ -11,7 +11,7 @@ PRE_CPPFLAGS = -I.. -I$(top_srcdir)
263  PRE_LDFLAGS = -L..
264  
265  OPENSSL_LIBFLAGS = @OPENSSL_LIBFLAGS@
266 -BENCH_LIBS = @BENCH_LIBS@ -lm
267 +BENCH_LIBS = @BENCH_LIBS@ $(TESTSUITE_LIBS) -lm
268  
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
273 @@ -46,6 +46,8 @@
274  
275  #include "timing.h"
276  
277 +#undef USE_MINI_GMP
278 +#include <gmp.h>
279  #include "../ecc.h"
280  #include "../ecc-internal.h"
281  #include "../gmp-glue.h"
282 --- a/gmp-glue.c
283 +++ b/gmp-glue.c
284 @@ -271,3 +271,4 @@ gmp_free(void *p, size_t n)
285  
286    free_func (p, (size_t) n);
287  }
288 +
289 --- a/gmp-glue.h
290 +++ b/gmp-glue.h
291 @@ -33,7 +33,7 @@
292  #ifndef NETTLE_GMP_GLUE_H_INCLUDED
293  #define NETTLE_GMP_GLUE_H_INCLUDED
294  
295 -#include <gmp.h>
296 +#include "bignum.h"
297  
298  #include "nettle-stdint.h"
299  
300 --- a/hogweed.pc.in
301 +++ b/hogweed.pc.in
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
306 -Libs.private: -lgmp
307 +Libs.private: @GMP_LIBS@
308  Cflags: -I${includedir}
309  
310 --- a/mini-gmp.c
311 +++ b/mini-gmp.c
312 @@ -1388,7 +1388,7 @@ mpz_clear (mpz_t r)
313    gmp_free (r->_mp_d);
314  }
315  
316 -static void *
317 +void *
318  mpz_realloc (mpz_t r, mp_size_t size)
319  {
320    size = GMP_MAX (size, 1);
321 --- a/mini-gmp.h
322 +++ b/mini-gmp.h
323 @@ -70,6 +70,11 @@ typedef struct
324    mp_limb_t *_mp_d;            /* Pointer to the limbs.  */
325  } __mpz_struct;
326  
327 +typedef __mpz_struct MP_INT;
328 +
329 +#define GMP_NUMB_BITS (SIZEOF_LONG*8)
330 +#define GMP_NUMB_MASK (~0)
331 +
332  typedef __mpz_struct mpz_t[1];
333  
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);
338  
339 +void *
340 +mpz_realloc (mpz_t r, mp_size_t size);
341 +#define _mpz_realloc mpz_realloc
342 +
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))
345  
346 --- a/pkcs1.h
347 +++ b/pkcs1.h
348 @@ -34,7 +34,7 @@
349  #ifndef NETTLE_PKCS1_H_INCLUDED
350  #define NETTLE_PKCS1_H_INCLUDED
351  
352 -#include <gmp.h>
353 +#include "bignum.h"
354  #include "nettle-types.h"
355  
356  #ifdef __cplusplus
357 --- a/rsa.h
358 +++ b/rsa.h
359 @@ -34,7 +34,7 @@
360  #ifndef NETTLE_RSA_H_INCLUDED
361  #define NETTLE_RSA_H_INCLUDED
362  
363 -#include <gmp.h>
364 +#include "bignum.h"
365  #include "nettle-types.h"
366  
367  #include "md5.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)
380  
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)
385  
386  ../nettle-internal.$(OBJEXT):
387         ( cd .. && $(MAKE) nettle-internal.$(OBJEXT) )
388 --- a/testsuite/testutils.h
389 +++ b/testsuite/testutils.h
390 @@ -12,11 +12,10 @@
391  #include <stdio.h>
392  #include <string.h>
393  
394 -#if HAVE_LIBGMP
395 -# include "bignum.h"
396 -#endif
397  
398  #if WITH_HOGWEED
399 +# include <gmp.h>
400 +# undef USE_MINI_GMP
401  # include "rsa.h"
402  # include "dsa-compat.h"
403  # include "ecc-curve.h"