From 60f0955d87a511503ce48de0e99f697e112ff3d0 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Mon, 9 Nov 2015 12:45:04 +0100 Subject: [PATCH] Allow use of Nettle as an alternative to gcrypt --- configure.ac | 34 +++++++++++++++++++++++++++++++++- src/Makefile.am | 2 +- src/wrbuf_sha1.c | 22 +++++++++++++++++++++- src/zoom-memcached.c | 4 ++-- test/Makefile.am | 2 +- test/test_wrbuf.c | 6 +++--- yaz-config.in | 3 ++- 7 files changed, 63 insertions(+), 10 deletions(-) diff --git a/configure.ac b/configure.ac index 365422c7..7bcbdfd 100644 --- a/configure.ac +++ b/configure.ac @@ -61,11 +61,40 @@ if test "$checkBoth" = "1"; then AC_CHECK_FUNC([accept], , [LIBS=$oldLibs]) fi AC_CHECK_FUNC([gethostbyname], ,[AC_CHECK_LIB(nsl, main, [LIBS="$LIBS -lnsl"])]) +dnl +dnl ------ nettle +nettle=default +AC_SUBST([NETTLE_LIBS]) +AC_ARG_WITH([nettle], [ --with-nettle Nettle library], [nettle=$withval]) +if test "$nettle" != "no"; then + AC_MSG_CHECKING([for nettle]) + if $pkgconfigpath --cflags nettle >/dev/null 2>&1 ; then + if $pkgconfigpath --atleast-version 2.0 nettle; then + AC_MSG_RESULT([yes]) + CFLAGS="$CFLAGS `$pkgconfigpath --cflags nettle`" + NETTLE_LIBS="`$pkgconfigpath --libs nettle`" + libgcryptversion=2enable + AC_DEFINE([HAVE_NETTLE],[1],[Define to 1 if nettle is enabled]) + else + AC_MSG_RESULT([no. Version 2.0 required]) + if test "$nettle" != "default"; then + AC_MSG_ERROR([nettle libraries missing]) + fi + fi + else + AC_MSG_RESULT([no]) + if test "$nettle" != "default"; then + AC_MSG_ERROR([nettle libraries missing]) + fi + fi +fi +dnl +dnl dnl ------ libgcrypt AC_SUBST([GCRYPT_LIBS]) libgcryptpath=NONE AC_ARG_WITH(libgcrypt, [ --with-libgcrypt=DIR use libgcrypt-config in DIR],[libgcryptpath=$withval]) -if test "$libgcryptpath" != "no"; then +if test "$libgcryptpath" != "no" -a -z "$libgcryptversion"; then if test "$libgcryptpath" = "NONE"; then AC_PATH_PROG([libgcrypt],[libgcrypt-config],[NONE]) elif test -x $libgcryptpath/libgcrypt-config; then @@ -150,6 +179,9 @@ if test "$memcached" != "no" -a -n "$libgcryptversion" -a "$pkgconfigpath" != "N fi fi fi +dnl +dnl +dnl dnl ------ GNU TLS AC_SUBST([SSL_CFLAGS]) AC_SUBST([SSL_LIBS]) diff --git a/src/Makefile.am b/src/Makefile.am index 2382f57..d44c5c2 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -26,7 +26,7 @@ YAZCOMP_Z = $(YAZCOMP) -d $(srcdir)/z.tcl -i yaz -I$(top_srcdir)/include YAZCOMP_I = $(YAZCOMP) -d $(srcdir)/ill.tcl -i yaz -I$(top_srcdir)/include AM_CPPFLAGS=-I$(top_srcdir)/include $(XML2_CFLAGS) $(SSL_CFLAGS) -libyaz_la_LIBADD = $(SSL_LIBS) $(TCPD_LIBS) $(GCRYPT_LIBS) \ +libyaz_la_LIBADD = $(SSL_LIBS) $(TCPD_LIBS) $(NETTLE_LIBS) $(GCRYPT_LIBS) \ $(MEMCACHED_LIBS) $(HIREDIS_LIBS) libyaz_server_la_LIBADD = libyaz.la libyaz_icu_la_CPPFLAGS = $(AM_CPPFLAGS) $(ICU_CPPFLAGS) -I$(top_srcdir)/libstemmer_c/include diff --git a/src/wrbuf_sha1.c b/src/wrbuf_sha1.c index 5627f6e..d801a28 100644 --- a/src/wrbuf_sha1.c +++ b/src/wrbuf_sha1.c @@ -20,10 +20,30 @@ #if HAVE_GCRYPT_H #include #endif +#if HAVE_NETTLE +#include +#endif int wrbuf_sha1_write(WRBUF b, const char *cp, size_t sz, int hexit) { -#if HAVE_GCRYPT_H +#if HAVE_NETTLE + struct sha1_ctx ctx; + uint8_t digest[SHA1_DIGEST_SIZE]; + + sha1_init(&ctx); + sha1_update(&ctx, sz, (uint8_t *) cp); + sha1_digest(&ctx, SHA1_DIGEST_SIZE, digest); + + if (hexit) + { + int i; + for (i = 0; i < SHA1_DIGEST_SIZE; i++) + wrbuf_printf(b, "%02x", digest[i]); + } + else + wrbuf_write(b, (const char *) digest, SHA1_DIGEST_SIZE); + return 0; +#elif HAVE_GCRYPT_H gcry_error_t e; gcry_md_hd_t hd; const unsigned char *digest_buf; diff --git a/src/zoom-memcached.c b/src/zoom-memcached.c index 332f5ab..4ee605b 100644 --- a/src/zoom-memcached.c +++ b/src/zoom-memcached.c @@ -185,7 +185,7 @@ int ZOOM_memcached_configure(ZOOM_connection c) return 0; } -#if HAVE_GCRYPT_H +#if HAVE_GCRYPT_H || HAVE_NETTLE static void wrbuf_vary_puts(WRBUF w, const char *v) { if (v) @@ -204,7 +204,7 @@ static void wrbuf_vary_puts(WRBUF w, const char *v) void ZOOM_memcached_resultset(ZOOM_resultset r, ZOOM_query q) { -#if HAVE_GCRYPT_H +#if HAVE_GCRYPT_H || HAVE_NETTLE ZOOM_connection c = r->connection; r->mc_key = wrbuf_alloc(); diff --git a/test/Makefile.am b/test/Makefile.am index 905526d..98e4b7a 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -57,7 +57,7 @@ dist-hook: test_odrcodec.c test_odrcodec.h: tstodr.asn $(YAZCOMP) cd $(srcdir); $(YAZCOMP) tstodr.asn -LDADD = ../src/libyaz.la +LDADD = ../src/libyaz.la test_icu_LDADD = ../src/libyaz_icu.la ../src/libyaz.la $(ICU_LIBS) test_libstemmer_LDADD = ../src/libyaz_icu.la ../src/libyaz.la $(ICU_LIBS) diff --git a/test/test_wrbuf.c b/test/test_wrbuf.c index a0046ef..278122b 100644 --- a/test/test_wrbuf.c +++ b/test/test_wrbuf.c @@ -17,7 +17,7 @@ static int sha1_test(WRBUF wr, const char *msg, const char *expect) { wrbuf_rewind(wr); -#if HAVE_GCRYPT_H +#if HAVE_GCRYPT_H || HAVE_NETTLE wrbuf_sha1_write(wr, msg, strlen(msg), 1); if (!strcmp(wrbuf_cstr(wr), expect)) return 1; @@ -36,7 +36,7 @@ static void *my_handler(void *arg) { char buf[100]; sprintf(buf, "Hello world %d", i); -#if HAVE_GCRYPT_H +#if HAVE_GCRYPT_H || HAVE_NETTLE wrbuf_sha1_write(wr, buf, strlen(buf), 1); #endif wrbuf_rewind(wr); @@ -126,7 +126,7 @@ static void tstwrbuf(void) wrbuf_insert(wr, 5, "abc", 3); YAZ_CHECK(!strcmp(wrbuf_cstr(wr), "1234")); - YAZ_CHECK(sha1_test(wr, + YAZ_CHECK(sha1_test(wr, "Hello world\n", "33ab5639bfd8e7b95eb1d8d0b87781d4ffea4d5d")); diff --git a/yaz-config.in b/yaz-config.in index 979985d..a5d7384 100644 --- a/yaz-config.in +++ b/yaz-config.in @@ -20,6 +20,7 @@ ICU_LIBS="@ICU_LIBS@" ICU_CPPFLAGS="@ICU_CPPFLAGS@" SSL_LIBS="@SSL_LIBS@" GCRYPT_LIBS="@GCRYPT_LIBS@" +NETTLE_LIBS="@NETTLE_LIBS@" MEMCACHED_LIBS="@MEMCACHED_LIBS@" HIREDIS_LIBS="@HIREDIS_LIBS@" LIBS="@LIBS@" @@ -133,7 +134,7 @@ if test "$echo_source" = "yes"; then else YAZLIB="-L${build_root}/src/.libs $YAZLIB" fi - LIBS="${SSL_LIBS} ${GCRYPT_LIBS} ${MEMCACHED_LIBS} ${HIREDIS_LIBS} $LIBS" + LIBS="${SSL_LIBS} ${GCRYPT_LIBS} ${NETTLE_LIBS} ${MEMCACHED_LIBS} ${HIREDIS_LIBS} $LIBS" YAZLIB="$YAZLIB $LIBS" YAZLALIB="${build_root}/src/libyaz.la" -- 1.7.10.4