From 45c5c98f9fb49ca4b76af87e0fcfcab766a06cd8 Mon Sep 17 00:00:00 2001 From: Jonathan Dieter Date: Tue, 3 Jul 2018 14:15:52 +0300 Subject: [PATCH] Add support for using OpenSSL for checksums Signed-off-by: Jonathan Dieter --- meson.build | 20 ++++++++++++++++++++ meson_options.txt | 1 + src/lib/hash/hash.c | 24 ++++++++++++++++++++---- src/lib/hash/meson.build | 6 ++++-- src/lib/meson.build | 2 +- test/meson.build | 2 +- 6 files changed, 47 insertions(+), 8 deletions(-) diff --git a/meson.build b/meson.build index 128c8a4..5065768 100644 --- a/meson.build +++ b/meson.build @@ -14,10 +14,30 @@ if use_zstd == 'auto' or use_zstd == 'yes' if zstd_dep.found() add_project_arguments('-DZCHUNK_ZSTD', language : 'c') use_zstd = 'yes' + else + use_zstd = 'no' endif endif + curl_dep = dependency('libcurl', required:true) +use_openssl = get_option('with-openssl') +openssl_dep = [] +if use_openssl == 'yes' + openssl_required = true +else + openssl_required = false +endif +if use_openssl == 'auto' or use_openssl == 'yes' + openssl_dep = dependency('openssl', required:openssl_required) + if openssl_dep.found() + add_project_arguments('-DZCHUNK_OPENSSL', language : 'c') + use_openssl = 'yes' + else + use_openssl = 'no' + endif +endif + inc = include_directories('include') subdir('include') diff --git a/meson_options.txt b/meson_options.txt index 6dc1a6d..67cdd4f 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -1 +1,2 @@ option('with-zstd', type : 'combo', choices : ['yes', 'no', 'auto'], value : 'auto') +option('with-openssl', type : 'combo', choices : ['yes', 'no', 'auto'], value : 'auto') diff --git a/src/lib/hash/hash.c b/src/lib/hash/hash.c index cf4047e..029b09e 100644 --- a/src/lib/hash/hash.c +++ b/src/lib/hash/hash.c @@ -31,8 +31,24 @@ #include #include "zck_private.h" + +/***** If we're not using OpenSSL, use bundled sha libraries *****/ +#ifndef ZCHUNK_OPENSSL #include "sha1/sha1.h" #include "sha2/sha2.h" +#define SHA256_CTX sha256_ctx +#define SHA256_Init sha256_init +#define SHA256_Update sha256_update +static void SHA256_Final(unsigned char *md, SHA256_CTX *c) { + sha256_final(c, md); +} +/***** If we are using OpenSSL, set the defines accordingly *****/ +#else +#include +#define SHA256_DIGEST_SIZE SHA256_DIGEST_LENGTH +#define SHA1_DIGEST_LENGTH SHA_DIGEST_LENGTH +#define sha1_byte void +#endif #define VALIDATE(f) if(!f) { \ zck_log(ZCK_LOG_ERROR, "zckCtx not initialized\n"); \ @@ -201,11 +217,11 @@ int hash_init(zckHash *hash, zckHashType *hash_type) { return True; }else if(hash_type->type == ZCK_HASH_SHA256) { zck_log(ZCK_LOG_DDEBUG, "Initializing SHA-256 hash\n"); - hash->ctx = zmalloc(sizeof(sha256_ctx)); + hash->ctx = zmalloc(sizeof(SHA256_CTX)); hash->type = hash_type; if(hash->ctx == NULL) return False; - sha256_init((sha256_ctx *) hash->ctx); + SHA256_Init((SHA256_CTX *) hash->ctx); return True; } zck_log(ZCK_LOG_ERROR, "Unsupported hash type: %i\n", hash_type->type); @@ -233,7 +249,7 @@ int hash_update(zckHash *hash, const char *message, const size_t size) { SHA1_Update((SHA_CTX *)hash->ctx, (const sha1_byte *)message, size); return True; } else if(hash->type->type == ZCK_HASH_SHA256) { - sha256_update((sha256_ctx *)hash->ctx, (const unsigned char *)message, size); + SHA256_Update((SHA256_CTX *)hash->ctx, (const unsigned char *)message, size); return True; } zck_log(ZCK_LOG_ERROR, "Unsupported hash type: %i\n", hash->type); @@ -252,7 +268,7 @@ char *hash_finalize(zckHash *hash) { return (char *)digest; } else if(hash->type->type == ZCK_HASH_SHA256) { unsigned char *digest = zmalloc(hash->type->digest_size); - sha256_final((sha256_ctx *)hash->ctx, digest); + SHA256_Final(digest, (SHA256_CTX *)hash->ctx); hash_close(hash); return (char *)digest; } diff --git a/src/lib/hash/meson.build b/src/lib/hash/meson.build index 661e94a..999d85d 100644 --- a/src/lib/hash/meson.build +++ b/src/lib/hash/meson.build @@ -1,3 +1,5 @@ lib_sources += files('hash.c') -subdir('sha1') -subdir('sha2') +if use_openssl == 'no' + subdir('sha1') + subdir('sha2') +endif diff --git a/src/lib/meson.build b/src/lib/meson.build index 1acaf8d..df18c4b 100644 --- a/src/lib/meson.build +++ b/src/lib/meson.build @@ -8,7 +8,7 @@ lib_sources += files('zck.c', 'header.c', 'io.c', 'log.c', 'compint.c') zcklib = shared_library('zck', lib_sources, include_directories: inc, - dependencies: [zstd_dep], + dependencies: [zstd_dep, openssl_dep], install: true, version: meson.project_version(), soversion: so_version) diff --git a/test/meson.build b/test/meson.build index 58c8107..50a1914 100644 --- a/test/meson.build +++ b/test/meson.build @@ -1,7 +1,7 @@ util_sources = [] subdir('lib') incdir = include_directories(['lib', '../src/lib', '../include']) -empty = executable('empty', ['empty.c'] + util_sources, include_directories: incdir, dependencies: [zstd_dep, curl_dep]) +empty = executable('empty', ['empty.c'] + util_sources, include_directories: incdir, dependencies: [zstd_dep, curl_dep, openssl_dep]) file_path = join_paths(meson.source_root(), 'test/files') test('create and validate empty zchunk file', empty) -- 2.30.2