Allow use of Nettle as an alternative to gcrypt
authorAdam Dickmeiss <adam@indexdata.dk>
Mon, 9 Nov 2015 11:45:04 +0000 (12:45 +0100)
committerAdam Dickmeiss <adam@indexdata.dk>
Mon, 9 Nov 2015 11:45:04 +0000 (12:45 +0100)
configure.ac
src/Makefile.am
src/wrbuf_sha1.c
src/zoom-memcached.c
test/Makefile.am
test/test_wrbuf.c
yaz-config.in

index 365422c..7bcbdfd 100644 (file)
@@ -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])
index 2382f57..d44c5c2 100644 (file)
@@ -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
index 5627f6e..d801a28 100644 (file)
 #if HAVE_GCRYPT_H
 #include <gcrypt.h>
 #endif
+#if HAVE_NETTLE
+#include <nettle/sha.h>
+#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;
index 332f5ab..4ee605b 100644 (file)
@@ -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();
index 905526d..98e4b7a 100644 (file)
@@ -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)
 
index a0046ef..278122b 100644 (file)
@@ -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"));
 
index 979985d..a5d7384 100644 (file)
@@ -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"