Since OpenSSL supports hardware or SIMD acceleration on some platforms. Signed-off-by: Gao Xiang <hsiang...@linux.alibaba.com> --- configure.ac | 1 + dump/Makefile.am | 2 +- fsck/Makefile.am | 4 ++-- lib/sha256.c | 27 +++++++++++++++++++++++++++ lib/sha256.h | 8 ++++++++ 5 files changed, 39 insertions(+), 3 deletions(-)
diff --git a/configure.ac b/configure.ac index 7769ac9..afd01a9 100644 --- a/configure.ac +++ b/configure.ac @@ -641,6 +641,7 @@ AS_IF([test "x$with_openssl" != "xno"], [ #include <openssl/hmac.h> ]]) ]) + AC_CHECK_HEADERS([openssl/sha.h],[],[]) LIBS="${saved_LIBS}" CPPFLAGS="${saved_CPPFLAGS}"], [ AS_IF([test "x$with_openssl" = "xyes"], [ diff --git a/dump/Makefile.am b/dump/Makefile.am index 2cf7fe8..c56e19f 100644 --- a/dump/Makefile.am +++ b/dump/Makefile.am @@ -8,4 +8,4 @@ dump_erofs_SOURCES = main.c dump_erofs_CFLAGS = -Wall -I$(top_srcdir)/include dump_erofs_LDADD = $(top_builddir)/lib/liberofs.la ${libselinux_LIBS} \ ${liblz4_LIBS} ${liblzma_LIBS} ${zlib_LIBS} ${libdeflate_LIBS} \ - ${libzstd_LIBS} ${libqpl_LIBS} ${libxxhash_LIBS} + ${libzstd_LIBS} ${libqpl_LIBS} ${libxxhash_LIBS} ${openssl_LIBS} diff --git a/fsck/Makefile.am b/fsck/Makefile.am index 3b7b591..a1b4623 100644 --- a/fsck/Makefile.am +++ b/fsck/Makefile.am @@ -8,7 +8,7 @@ fsck_erofs_SOURCES = main.c fsck_erofs_CFLAGS = -Wall -I$(top_srcdir)/include fsck_erofs_LDADD = $(top_builddir)/lib/liberofs.la ${libselinux_LIBS} \ ${liblz4_LIBS} ${liblzma_LIBS} ${zlib_LIBS} ${libdeflate_LIBS} \ - ${libzstd_LIBS} ${libqpl_LIBS} ${libxxhash_LIBS} + ${libzstd_LIBS} ${libqpl_LIBS} ${libxxhash_LIBS} ${openssl_LIBS} if ENABLE_FUZZING noinst_PROGRAMS = fuzz_erofsfsck @@ -17,5 +17,5 @@ fuzz_erofsfsck_CFLAGS = -Wall -I$(top_srcdir)/include -DFUZZING fuzz_erofsfsck_LDFLAGS = -fsanitize=address,fuzzer fuzz_erofsfsck_LDADD = $(top_builddir)/lib/liberofs.la ${libselinux_LIBS} \ ${liblz4_LIBS} ${liblzma_LIBS} ${zlib_LIBS} ${libdeflate_LIBS} \ - ${libzstd_LIBS} ${libqpl_LIBS} ${libxxhash_LIBS} + ${libzstd_LIBS} ${libqpl_LIBS} ${libxxhash_LIBS} ${openssl_LIBS} endif diff --git a/lib/sha256.c b/lib/sha256.c index 9bb7fbb..a7ba067 100644 --- a/lib/sha256.c +++ b/lib/sha256.c @@ -7,6 +7,32 @@ #include "sha256.h" #include <string.h> +#ifdef __USE_OPENSSL_SHA256 +void erofs_sha256_init(struct sha256_state *md) +{ + int ret; + + ret = SHA256_Init(&md->ctx); + DBG_BUGON(ret != 1); +} + +int erofs_sha256_process(struct sha256_state *md, + const unsigned char *in, unsigned long inlen) +{ + int ret; + + ret = SHA256_Update(&md->ctx, in, inlen); + return (ret == 1) - 1; +} + +int erofs_sha256_done(struct sha256_state *md, unsigned char *out) +{ + int ret; + + ret = SHA256_Final(out, &md->ctx); + return (ret == 1) - 1; +} +#else /* This is based on SHA256 implementation in LibTomCrypt that was released into * public domain by Tom St Denis. */ /* the K array */ @@ -186,6 +212,7 @@ int erofs_sha256_done(struct sha256_state *md, unsigned char *out) STORE32H(md->state[i], out + (4 * i)); return 0; } +#endif void erofs_sha256(const unsigned char *in, unsigned long in_size, unsigned char out[32]) diff --git a/lib/sha256.h b/lib/sha256.h index dd39970..45e49cd 100644 --- a/lib/sha256.h +++ b/lib/sha256.h @@ -4,11 +4,19 @@ #include "erofs/defs.h" +#if defined(HAVE_OPENSSL) && defined(HAVE_OPENSSL_SHA_H) +#include <openssl/sha.h> +struct sha256_state { + SHA256_CTX ctx; +}; +#define __USE_OPENSSL_SHA256 +#else struct sha256_state { u64 length; u32 state[8], curlen; u8 buf[64]; }; +#endif void erofs_sha256_init(struct sha256_state *md); int erofs_sha256_process(struct sha256_state *md, -- 2.43.5