Add optional use of the openssl crypto lib for MD5.
authorWayne Davison <wayne@opencoder.net>
Sat, 23 May 2020 16:23:01 +0000 (09:23 -0700)
committerWayne Davison <wayne@opencoder.net>
Sat, 23 May 2020 17:06:59 +0000 (10:06 -0700)
NEWS
checksum.c
configure.ac
lib/md5.c
lib/mdfour.c
lib/mdigest.h

diff --git a/NEWS b/NEWS
index edf594526e42964af9e782a52376c051a97a03ac..599995096b38559706925e517daa42a8e4b1c58b 100644 (file)
--- 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.
index a21222d49577bca440f7511a17d46a9220cd0fb1..7c4c855cdf3262a6711205131d836db2914763d0 100644 (file)
  * 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:
index 394f5b52f73aa1e1d95c5b5da184fea9667db3fc..77b1827822349778c4305af789b90329d8ace785 100644 (file)
@@ -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,[
index d697b9b3fa438a1f47e96ec960580668cdc8ea0c..a2eebdb0e0db96096c3446bb039d2a415a83c816 100644 (file)
--- 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 <stdlib.h>
 #include <stdio.h>
 
index 399da83ef11de0caea64723ede4b2b977a3e2f7f..70a8ba34d86bb34660f675a3f861e00c7385f89f 100644 (file)
@@ -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)
index e0e33ed364c831bde796fcf05f811d0cfde6f814..86c1140f090674691da4eb14e2a840e24b49b197 100644 (file)
@@ -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