From 2578676b1b0a28a85d05f6e808ce953084c6f2a0 Mon Sep 17 00:00:00 2001 From: Moinak Ghosh Date: Wed, 3 Oct 2012 22:43:02 +0530 Subject: [PATCH] Add support for SHA256 and SHA512 digests from OpenSSL library. --- Makefile.in | 4 +-- README.md | 4 +-- config | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++- main.c | 4 +-- utils/utils.c | 19 +++++++++++++- utils/utils.h | 4 ++- 6 files changed, 94 insertions(+), 9 deletions(-) diff --git a/Makefile.in b/Makefile.in index 2de01c9..a4f4da4 100644 --- a/Makefile.in +++ b/Makefile.in @@ -85,10 +85,10 @@ BAKFILES = *~ lzma/*~ lzfx/*~ lz4/*~ rabin/*~ bsdiff/*~ lzp/*~ utils/*~ RM = rm -f COMMON_CPPFLAGS = -I. -I./lzma -I./lzfx -I./lz4 -I./rabin -I./bsdiff -DNODEFAULT_PROPS \ -DFILE_OFFSET_BITS=64 -D_REENTRANT -D__USE_SSE_INTRIN__ -D_LZMA_PROB32 \ - -I./lzp @LIBBSCCPPFLAGS@ -I./crypto/skein -I./utils + -I./lzp @LIBBSCCPPFLAGS@ -I./crypto/skein -I./utils -I@OPENSSL_INCDIR@ COMMON_VEC_FLAGS = -ftree-vectorize COMMON_LOOP_OPTFLAGS = $(VEC_FLAGS) -floop-interchange -floop-block -LDLIBS = -ldl -lbz2 $(ZLIB_DIR) -lz -lm @LIBBSCLFLAGS@ +LDLIBS = -ldl -lbz2 $(ZLIB_DIR) -lz -lm @LIBBSCLFLAGS@ -L@OPENSSL_LIBDIR@ -lcrypto OBJS = $(MAINOBJS) $(LZMAOBJS) $(PPMDOBJS) $(LZFXOBJS) $(LZ4OBJS) $(CRCOBJS) \ $(RABINOBJS) $(BSDIFFOBJS) $(LZPOBJS) @LIBBSCWRAPOBJ@ $(SKEINOBJS) $(SKEIN_BLOCK_OBJ) diff --git a/README.md b/README.md index 1ab79a6..a7b6ed4 100644 --- a/README.md +++ b/README.md @@ -98,8 +98,8 @@ NOTE: The option "libbsc" uses Ilya Grebnov's block sorting compression library algorithms with some extra CPU and very low RAM overhead. Using delta encoding in conjunction with this may not always be beneficial. '-S' - - Specify chunk checksum to use: CRC64, SKEIN256, SKEIN512 - Default one is SKEIN256. The implementation actually uses SKEIN + - Specify chunk checksum to use: CRC64, SKEIN256, SKEIN512, SHA256 and + SHA512. Default one is SKEIN256. The implementation actually uses SKEIN 512-256. This is 25% slower than simple CRC64 but is many times more robust than CRC64 in detecting data integrity errors. SKEIN is a finalist in the NIST SHA-3 standard selection process and is one of diff --git a/config b/config index d109949..95f0e7a 100755 --- a/config +++ b/config @@ -11,9 +11,12 @@ ${prog} [] --enable-debug Enable debug mode compilation (default: disabled). --disable-allocator Disable use of internal memory allocator mechanism (default: enabled). --enable-debug-stats Enable printing of some verbose debug info (default: disabled). ---with-libbsc= +--with-libbsc= Enable support for libbsc (See: libbsc.com). Full path to the libbsc source tree must be provided. It links the library statically. +--with-openssl= (Default: System) + This defaults to the system's OpenSSL library. You can use this option + if you want to use an alternate OpenSSL installation. --help Display this help message. _EOF @@ -30,6 +33,9 @@ libbsclflags= libbscwrapobj= libbscgenopt= libbsccppflags= +openssl_prefix= +openssl_libdir= +openssl_incdir= while [ "${arg1}" != "" ] do @@ -55,6 +61,9 @@ do echo "Libbsc not found in ${path}, not enabling libbsc support.\n" fi ;; + --with-openssl=*) + openssl_prefix=`echo ${arg1} | cut -f2 -d"="` + ;; --help) usage $0;; *) echo "Unrecognized option: ${arg1}" @@ -91,6 +100,58 @@ then skeinblock='\$\(SKEIN_BLOCK_ASM\)' fi +# Detect OpenSSL library +for lib in "${openssl_prefix}/lib64" "${openssl_prefix}/usr/lib64" \ + "${openssl_prefix}/lib" "${openssl_prefix}/usr/lib" \ + "${openssl_prefix}/ssl/lib64" "${openssl_prefix}/ssl/lib" +do + if [ -d ${lib} ] + then + if [ -f "${lib}/libcrypto.so" ] + then + openssl_libdir=${lib} + break + else + if [ -f "${lib}/libcrypto.a" ] + then + openssl_libdir=${lib} + break + fi + fi + fi +done + +if [ "x${openssl_libdir}" = "x" ] +then + echo "ERROR: OpenSSL libraries not detected." + exit 1 +fi + +# Detect OpenSSL headers +for inc in "${openssl_prefix}/include" \ + "${openssl_prefix}/usr/include" \ + "${openssl_prefix}/ssl/include" +do + if [ -d ${inc} ] + then + if [ -f "${inc}/openssl/sha.h" ] + then + openssl_incdir=${inc} + break + fi + fi +done + +if [ "x${openssl_incdir}" = "x" ] +then + echo "ERROR: OpenSSL header files not detected." + if [ "x${openssl_prefix}" = "x" ] + then + echo "Depending on your system you may need to install the openssl-devel package." + fi + exit 1 +fi + linkvar="LINK" compilevar="COMPILE" compilecppvar="COMPILE_cpp" @@ -111,6 +172,9 @@ libbscwrapobjvar="LIBBSCWRAPOBJ" libbscgenoptvar="LIBBSCGEN_OPT" libbsccppflagsvar="LIBBSCCPPFLAGS" +openssllibdirvar="OPENSSL_LIBDIR" +opensslincdirvar="OPENSSL_INCDIR" + noslabcppflagsval= debugstatscppflagsval= @@ -136,5 +200,7 @@ s#@${libbscwrapobjvar}@#${libbscwrapobj}#g s#@${libbscgenoptvar}@#${libbscgenopt}#g s#@${libbsccppflagsvar}@#${libbsccppflags}#g s#@${skeinblockvar}@#${skeinblock}#g +s#@${openssllibdirvar}@#${openssl_libdir}#g +s#@${opensslincdirvar}@#${openssl_incdir}#g " > Makefile diff --git a/main.c b/main.c index 2749c50..fcd1af4 100644 --- a/main.c +++ b/main.c @@ -144,8 +144,8 @@ usage(void) " '-L' - Enable LZP pre-compression. This improves compression ratio of all\n" " algorithms with some extra CPU and very low RAM overhead.\n" " '-S' \n" - " - Specify chunk checksum to use: CRC64, SKEIN256, SKEIN512\n" - " Default one is SKEIN256.\n" + " - Specify chunk checksum to use: CRC64, SKEIN256, SKEIN512, SHA256 and\n" + " SHA512. Default one is SKEIN256.\n" " '-F' - Perform Fixed-Block Deduplication. Faster than '-D' in some cases\n" " but with lower deduplication ratio.\n" " '-B' <1..5>\n" diff --git a/utils/utils.c b/utils/utils.c index 057c0f8..5e2f653 100644 --- a/utils/utils.c +++ b/utils/utils.c @@ -33,6 +33,7 @@ #include #include #include +#include #include "utils.h" @@ -46,7 +47,9 @@ static struct { } cksum_props[] = { {"CRC64", CKSUM_CRC64, 8}, {"SKEIN256", CKSUM_SKEIN256, 32}, - {"SKEIN512", CKSUM_SKEIN512, 64} + {"SKEIN512", CKSUM_SKEIN512, 64}, + {"SHA256", CKSUM_SHA256, 32}, + {"SHA512", CKSUM_SHA512, 64} }; extern uint64_t lzma_crc64(const uint8_t *buf, size_t size, uint64_t crc); @@ -334,6 +337,20 @@ compute_checksum(uchar_t *cksum_buf, int cksum, uchar_t *buf, ssize_t bytes) Skein_512_Init(&ctx, 512); Skein_512_Update(&ctx, buf, bytes); Skein_512_Final(&ctx, cksum_buf); + + } else if (cksum == CKSUM_SHA256) { + SHA256_CTX ctx; + + SHA256_Init(&ctx); + SHA256_Update(&ctx, buf, bytes); + SHA256_Final(cksum_buf, &ctx); + + } else if (cksum == CKSUM_SKEIN512) { + SHA512_CTX ctx; + + SHA512_Init(&ctx); + SHA512_Update(&ctx, buf, bytes); + SHA512_Final(cksum_buf, &ctx); } else { return (-1); } diff --git a/utils/utils.h b/utils/utils.h index 8fec47e..f04ee17 100644 --- a/utils/utils.h +++ b/utils/utils.h @@ -108,7 +108,9 @@ typedef ssize_t bsize_t; typedef enum { CKSUM_CRC64 = 0x100, CKSUM_SKEIN256 = 0x200, - CKSUM_SKEIN512 = 0x300 + CKSUM_SKEIN512 = 0x300, + CKSUM_SHA256 = 0x400, + CKSUM_SHA512 = 0x500 } cksum_t; #define CKSUM_MASK 0x700