You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

463 lines
13 KiB

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