From: Wayne Davison Date: Sat, 23 May 2020 16:23:01 +0000 (-0700) Subject: Add optional use of the openssl crypto lib for MD5. X-Git-Tag: v3.2.0pre1~128 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=15c1162b24f51b29cbd534b2e8f732e06995ef89;p=thirdparty%2Frsync.git Add optional use of the openssl crypto lib for MD5. --- diff --git a/NEWS b/NEWS index edf59452..59999509 100644 --- a/NEWS +++ b/NEWS @@ -38,10 +38,11 @@ Changes since 3.1.3: ENHANCEMENTS: - - Various checksum enhancements, including x86_64 optimizations for the - rolling checksum, optimizations for the MD5 checksums, the addition of - xxhash checksums, and a simple checksum negotation heuristic that will - ensure that it is easier to add improved checksum algorithms in the + - Various checksum enhancements, including the optional use of openssl's + MD5 checksum algorithms, x86_64 optimizations for the rolling checksum, + x86_64 optimizations for the (non-openssl) MD5 checksum, the addition of + xxhash checksum support, and a simple checksum negotation heuristic that + will ensure that it is easier to add new checksum algorithms in the future. Currently the x86_64 optimizations require the use of the --enable-simd flag to configure, but they will probably be enabled by default in the near future. diff --git a/checksum.c b/checksum.c index a21222d4..7c4c855c 100644 --- a/checksum.c +++ b/checksum.c @@ -10,6 +10,13 @@ * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * + * In addition, as a special exception, the copyright holders give + * permission to dynamically link rsync with the OpenSSL and xxhash + * libraries when those libraries are being distributed in compliance + * with their license terms, and to distribute a dynamically linked + * combination of rsync and these libraries. This is also considered + * to be covered under the GPL's System Libraries exception. + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the @@ -23,6 +30,9 @@ #ifdef SUPPORT_XXHASH #include "xxhash.h" #endif +#ifdef USE_OPENSSL +#include "openssl/md5.h" +#endif extern int am_server; extern int local_server; @@ -58,6 +68,13 @@ struct csum_struct { #define MAX_CHECKSUM_LIST 1024 +#ifndef USE_OPENSSL +#define MD5_CTX md_context +#define MD5_Init md5_begin +#define MD5_Update md5_update +#define MD5_Final(digest, cptr) md5_result(cptr, digest) +#endif + int xfersum_type = 0; /* used for the file transfer checksums */ int checksum_type = 0; /* used for the pre-transfer (--checksum) checksums */ const char *negotiated_csum_name = NULL; @@ -298,25 +315,26 @@ uint32 get_checksum1(char *buf1, int32 len) void get_checksum2(char *buf, int32 len, char *sum) { md_context m; + MD5_CTX m5; switch (xfersum_type) { case CSUM_MD5: { uchar seedbuf[4]; - md5_begin(&m); + MD5_Init(&m5); if (proper_seed_order) { if (checksum_seed) { SIVALu(seedbuf, 0, checksum_seed); - md5_update(&m, seedbuf, 4); + MD5_Update(&m5, seedbuf, 4); } - md5_update(&m, (uchar *)buf, len); + MD5_Update(&m5, (uchar *)buf, len); } else { - md5_update(&m, (uchar *)buf, len); + MD5_Update(&m5, (uchar *)buf, len); if (checksum_seed) { SIVALu(seedbuf, 0, checksum_seed); - md5_update(&m, seedbuf, 4); + MD5_Update(&m5, seedbuf, 4); } } - md5_result(&m, (uchar *)sum); + MD5_Final((uchar *)sum, &m5); break; } case CSUM_MD4: @@ -374,6 +392,7 @@ void file_checksum(const char *fname, const STRUCT_STAT *st_p, char *sum) struct map_struct *buf; OFF_T i, len = st_p->st_size; md_context m; + MD5_CTX m5; int32 remainder; int fd; @@ -387,18 +406,18 @@ void file_checksum(const char *fname, const STRUCT_STAT *st_p, char *sum) switch (checksum_type) { case CSUM_MD5: - md5_begin(&m); + MD5_Init(&m5); for (i = 0; i + CSUM_CHUNK <= len; i += CSUM_CHUNK) { - md5_update(&m, (uchar *)map_ptr(buf, i, CSUM_CHUNK), + MD5_Update(&m5, (uchar *)map_ptr(buf, i, CSUM_CHUNK), CSUM_CHUNK); } remainder = (int32)(len - i); if (remainder > 0) - md5_update(&m, (uchar *)map_ptr(buf, i, remainder), remainder); + MD5_Update(&m5, (uchar *)map_ptr(buf, i, remainder), remainder); - md5_result(&m, (uchar *)sum); + MD5_Final((uchar *)sum, &m5); break; case CSUM_MD4: case CSUM_MD4_OLD: @@ -459,6 +478,7 @@ void file_checksum(const char *fname, const STRUCT_STAT *st_p, char *sum) static int32 sumresidue; static md_context md; +static MD5_CTX m5; static int cursum_type; #ifdef SUPPORT_XXHASH XXH64_state_t* xxh64_state = NULL; @@ -474,7 +494,7 @@ void sum_init(int csum_type, int seed) switch (csum_type) { case CSUM_MD5: - md5_begin(&md); + MD5_Init(&m5); break; case CSUM_MD4: mdfour_begin(&md); @@ -520,7 +540,7 @@ void sum_update(const char *p, int32 len) { switch (cursum_type) { case CSUM_MD5: - md5_update(&md, (uchar *)p, len); + MD5_Update(&m5, (uchar *)p, len); break; case CSUM_MD4: case CSUM_MD4_OLD: @@ -573,7 +593,7 @@ int sum_end(char *sum) { switch (cursum_type) { case CSUM_MD5: - md5_result(&md, (uchar *)sum); + MD5_Final((uchar *)sum, &m5); break; case CSUM_MD4: case CSUM_MD4_OLD: diff --git a/configure.ac b/configure.ac index 394f5b52..77b18278 100644 --- a/configure.ac +++ b/configure.ac @@ -381,19 +381,33 @@ AC_CHECK_HEADERS(sys/fcntl.h sys/select.h fcntl.h sys/time.h sys/unistd.h \ netdb.h malloc.h float.h limits.h iconv.h libcharset.h langinfo.h \ sys/acl.h acl/libacl.h attr/xattr.h sys/xattr.h sys/extattr.h \ popt.h popt/popt.h linux/falloc.h netinet/in_systm.h netinet/ip.h \ - zlib.h xxhash.h) + zlib.h xxhash.h openssl/md5.h) AC_HEADER_MAJOR_FIXED -dnl Do you want to disable use of xxhash checksums +AC_MSG_CHECKING([whether to enable use of openssl crypto library]) +AC_ARG_ENABLE([openssl], + AS_HELP_STRING([--disable-openssl],[disable openssl crypto library])) +AH_TEMPLATE([USE_OPENSSL], +[Undefine if you do not want to use openssl crypto library. By default this is defined.]) +if test x"$enable_openssl" != x"no" && test x"$ac_cv_header_openssl_md5_h" = x"yes"; then + AC_MSG_RESULT(yes) + AC_SEARCH_LIBS(MD5_Init, crypto) + AC_DEFINE(USE_OPENSSL) +else + AC_MSG_RESULT(no) +fi + +AC_MSG_CHECKING([whether to enable the xxhash support]) AC_ARG_ENABLE([xxhash], AS_HELP_STRING([--disable-xxhash],[disable xxhash checksums])) AH_TEMPLATE([SUPPORT_XXHASH], [Undefine if you do not want xxhash checksums. By default this is defined.]) -if test x"$enable_xxhash" != x"no"; then - if test x"$ac_cv_header_xxhash_h" = x"yes"; then - AC_SEARCH_LIBS(XXH64_createState, xxhash) - AC_DEFINE(SUPPORT_XXHASH) - fi +if test x"$enable_xxhash" != x"no" && test x"$ac_cv_header_xxhash_h" = x"yes"; then + AC_MSG_RESULT(yes) + AC_SEARCH_LIBS(XXH64_createState, xxhash) + AC_DEFINE(SUPPORT_XXHASH) +else + AC_MSG_RESULT(no) fi AC_CACHE_CHECK([if makedev takes 3 args],rsync_cv_MAKEDEV_TAKES_3_ARGS,[ diff --git a/lib/md5.c b/lib/md5.c index d697b9b3..a2eebdb0 100644 --- a/lib/md5.c +++ b/lib/md5.c @@ -20,6 +20,7 @@ #include "rsync.h" +#ifndef USE_OPENSSL void md5_begin(md_context *ctx) { ctx->A = 0x67452301; @@ -220,6 +221,9 @@ void md5_result(md_context *ctx, uchar digest[MD5_DIGEST_LEN]) SIVALu(digest, 8, ctx->C); SIVALu(digest, 12, ctx->D); } +#endif + +#ifdef TEST_MD5 void get_md5(uchar *out, const uchar *input, int n) { @@ -229,8 +233,6 @@ void get_md5(uchar *out, const uchar *input, int n) md5_result(&ctx, out); } -#ifdef TEST_MD5 - #include #include diff --git a/lib/mdfour.c b/lib/mdfour.c index 399da83e..70a8ba34 100644 --- a/lib/mdfour.c +++ b/lib/mdfour.c @@ -193,6 +193,8 @@ void mdfour_result(md_context *md, uchar digest[MD4_DIGEST_LEN]) copy4(digest+12, m->D); } +#ifdef TEST_MDFOUR + void mdfour(uchar digest[MD4_DIGEST_LEN], uchar *in, int length) { md_context md; @@ -201,7 +203,6 @@ void mdfour(uchar digest[MD4_DIGEST_LEN], uchar *in, int length) mdfour_result(&md, digest); } -#ifdef TEST_MDFOUR int protocol_version = 28; static void file_checksum1(char *fname) diff --git a/lib/mdigest.h b/lib/mdigest.h index e0e33ed3..86c1140f 100644 --- a/lib/mdigest.h +++ b/lib/mdigest.h @@ -17,10 +17,8 @@ void mdfour_begin(md_context *md); void mdfour_update(md_context *md, const uchar *in, uint32 length); void mdfour_result(md_context *md, uchar digest[MD4_DIGEST_LEN]); -void get_mdfour(uchar digest[MD4_DIGEST_LEN], const uchar *in, int length); - +#ifndef USE_OPENSSL void md5_begin(md_context *ctx); void md5_update(md_context *ctx, const uchar *input, uint32 length); void md5_result(md_context *ctx, uchar digest[MD5_DIGEST_LEN]); - -void get_md5(uchar digest[MD5_DIGEST_LEN], const uchar *input, int n); +#endif