blktap: Fall back to libcrypto if libgcrypt is not installed.
authorKeir Fraser <keir.fraser@citrix.com>
Mon, 30 Jun 2008 10:39:10 +0000 (11:39 +0100)
committerKeir Fraser <keir.fraser@citrix.com>
Mon, 30 Jun 2008 10:39:10 +0000 (11:39 +0100)
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
tools/blktap/drivers/Makefile
tools/blktap/drivers/block-qcow.c
tools/blktap/drivers/check_gcrypt [new file with mode: 0644]

index 548f080e7c7e5f36cc395a3ae64ab7d8ebbf592b..b1f4410cc962bffbc8b658b9cbf6c9f41f7c20d0 100644 (file)
@@ -17,8 +17,16 @@ CFLAGS   += -D_GNU_SOURCE
 CFLAGS   += -Wp,-MD,.$(@F).d
 DEPS      = .*.d
 
+ifeq ($(shell . ./check_gcrypt),"yes")
+CFLAGS += -DUSE_GCRYPT
+CRYPT_LIB := -lgcrypt
+else
+CRYPT_LIB := -lcrypto
+$(warning *** libgcrypt not installed: falling back to libcrypto ***)
+endif
+
 LDFLAGS_blktapctrl := $(LDFLAGS_libxenctrl) $(LDFLAGS_libxenstore) -L../lib -lblktap
-LDFLAGS_img := $(LIBAIO_DIR)/libaio.a -lgcrypt -lpthread -lz
+LDFLAGS_img := $(LIBAIO_DIR)/libaio.a $(CRYPT_LIB) -lpthread -lz
 
 BLK-OBJS-y  := block-aio.o
 BLK-OBJS-y  += block-sync.o
index dd6c6c7c076ac391f9a17e5aad0fa8134ddc6b64..1f3b46936ca06659759bda2c606381cedb0ef7fe 100644 (file)
@@ -33,7 +33,6 @@
 #include <zlib.h>
 #include <inttypes.h>
 #include <libaio.h>
-#include <gcrypt.h>
 #include "bswap.h"
 #include "aes.h"
 #include "tapdisk.h"
@@ -146,6 +145,10 @@ struct tdqcow_state {
 
 static int decompress_cluster(struct tdqcow_state *s, uint64_t cluster_offset);
 
+#ifdef USE_GCRYPT
+
+#include <gcrypt.h>
+
 static uint32_t gen_cksum(char *ptr, int len)
 {
        int i;
@@ -167,6 +170,41 @@ static uint32_t gen_cksum(char *ptr, int len)
        return md[0];
 }
 
+#else /* use libcrypto */
+
+#include <openssl/md5.h>
+
+static uint32_t gen_cksum(char *ptr, int len)
+{
+       int i;
+       unsigned char *md;
+       uint32_t ret;
+
+       md = malloc(MD5_DIGEST_LENGTH);
+       if(!md) return 0;
+
+       /* Convert L1 table to big endian */
+       for(i = 0; i < len / sizeof(uint64_t); i++) {
+               cpu_to_be64s(&((uint64_t*) ptr)[i]);
+       }
+
+       /* Generate checksum */
+       if (MD5((unsigned char *)ptr, len, md) != md)
+               ret = 0;
+       else
+               memcpy(&ret, md, sizeof(uint32_t));
+
+       /* Convert L1 table back to native endianess */
+       for(i = 0; i < len / sizeof(uint64_t); i++) {
+               be64_to_cpus(&((uint64_t*) ptr)[i]);
+       }
+
+       free(md);
+       return ret;
+}
+
+#endif
+
 static int get_filesize(char *filename, uint64_t *size, struct stat *st)
 {
        int fd;
diff --git a/tools/blktap/drivers/check_gcrypt b/tools/blktap/drivers/check_gcrypt
new file mode 100644 (file)
index 0000000..154ba24
--- /dev/null
@@ -0,0 +1,14 @@
+#!/bin/sh
+
+cat > .gcrypt.c << EOF
+#include <gcrypt.h>
+int main(void) { return 0; }
+EOF
+
+if $1 -o .gcrypt .gcrypt.c -lgcrypt 2>/dev/null ; then
+  echo "yes"
+else
+  echo "no"
+fi
+
+rm -f .gcrypt*