Switch to EVP & add SHA256 & SHA512.
[rsync-patches.git] / sha1.diff
1 This patch adds sha1 to the checksum code when the openssl library is available.
2 It also enables SHA1 checksum hashing in the daemon auth code.
3
4 To use this patch, run these commands for a successful build:
5
6     patch -p1 <patches/fix-checksums.diff
7     patch -p1 <patches/sha1.diff
8     ./configure                               (optional if already run)
9     make
10
11 based-on: patch/master/fix-checksums
12 diff --git a/checksum.c b/checksum.c
13 --- a/checksum.c
14 +++ b/checksum.c
15 @@ -54,6 +54,15 @@ struct name_num_obj valid_checksums = {
16  #endif
17                 { CSUM_MD5, "md5", NULL },
18                 { CSUM_MD4, "md4", NULL },
19 +#ifdef SHA_DIGEST_LENGTH
20 +               { CSUM_SHA1, "sha1", NULL },
21 +#endif
22 +#ifdef SHA256_DIGEST_LENGTH
23 +               { CSUM_SHA256, "sha256", NULL },
24 +#endif
25 +#ifdef SHA512_DIGEST_LENGTH
26 +               { CSUM_SHA512, "sha512", NULL },
27 +#endif
28                 { CSUM_NONE, "none", NULL },
29                 { 0, NULL, NULL }
30         }
31 @@ -61,6 +70,15 @@ struct name_num_obj valid_checksums = {
32  
33  struct name_num_obj valid_auth_checksums = {
34         "daemon auth checksum", NULL, NULL, 0, 0, {
35 +#ifdef SHA512_DIGEST_LENGTH
36 +               { CSUM_SHA512, "sha512", NULL },
37 +#endif
38 +#ifdef SHA256_DIGEST_LENGTH
39 +               { CSUM_SHA256, "sha256", NULL },
40 +#endif
41 +#ifdef SHA_DIGEST_LENGTH
42 +               { CSUM_SHA1, "sha1", NULL },
43 +#endif
44                 { CSUM_MD5, "md5", NULL },
45                 { CSUM_MD4, "md4", NULL },
46                 { 0, NULL, NULL }
47 @@ -155,6 +173,18 @@ int csum_len_for_type(int cst, BOOL flist_csum)
48                 return MD4_DIGEST_LEN;
49           case CSUM_MD5:
50                 return MD5_DIGEST_LEN;
51 +#ifdef SHA_DIGEST_LENGTH
52 +         case CSUM_SHA1:
53 +               return SHA_DIGEST_LENGTH;
54 +#endif
55 +#ifdef SHA256_DIGEST_LENGTH
56 +         case CSUM_SHA256:
57 +               return SHA256_DIGEST_LENGTH;
58 +#endif
59 +#ifdef SHA512_DIGEST_LENGTH
60 +         case CSUM_SHA512:
61 +               return SHA512_DIGEST_LENGTH;
62 +#endif
63           case CSUM_XXH64:
64           case CSUM_XXH3_64:
65                 return 64/8;
66 @@ -180,6 +210,9 @@ int canonical_checksum(int csum_type)
67                 break;
68           case CSUM_MD4:
69           case CSUM_MD5:
70 +         case CSUM_SHA1:
71 +         case CSUM_SHA256:
72 +         case CSUM_SHA512:
73                 return -1;
74           case CSUM_XXH64:
75           case CSUM_XXH3_64:
76 @@ -232,6 +265,26 @@ void get_checksum2(char *buf, int32 len, char *sum)
77                 SIVAL64(sum, 8, digest.high64);
78                 break;
79           }
80 +#endif
81 +#ifdef SHA_DIGEST_LENGTH
82 +         case CSUM_SHA1:
83 +         case CSUM_SHA256:
84 +         case CSUM_SHA512: {
85 +               static EVP_MD_CTX *evp = NULL;
86 +               static const EVP_MD *emd = NULL;
87 +               uchar seedbuf[4];
88 +               if (!evp) {
89 +                       if (!(evp = EVP_MD_CTX_create()))
90 +                               out_of_memory("get_checksum2");
91 +                       emd = EVP_get_digestbyname(checksum_name(xfer_sum_type));
92 +               }
93 +               EVP_DigestInit_ex(evp, emd, NULL);
94 +               SIVALu(seedbuf, 0, checksum_seed);
95 +               EVP_DigestUpdate(evp, seedbuf, 4);
96 +               EVP_DigestUpdate(evp, (uchar *)buf, len);
97 +               EVP_DigestFinal_ex(evp, (uchar *)sum, NULL);
98 +               break;
99 +         }
100  #endif
101           case CSUM_MD5: {
102                 md5_context m5;
103 @@ -384,6 +437,31 @@ void file_checksum(const char *fname, const STRUCT_STAT *st_p, char *sum)
104                 SIVAL64(sum, 8, digest.high64);
105                 break;
106           }
107 +#endif
108 +#ifdef SHA_DIGEST_LENGTH
109 +         case CSUM_SHA1:
110 +         case CSUM_SHA256:
111 +         case CSUM_SHA512: {
112 +               static EVP_MD_CTX *evp = NULL;
113 +               static const EVP_MD *emd = NULL;
114 +               if (!evp) {
115 +                       if (!(evp = EVP_MD_CTX_create()))
116 +                               out_of_memory("file_checksum");
117 +                       emd = EVP_get_digestbyname(checksum_name(xfer_sum_type));
118 +               }
119 +
120 +               EVP_DigestInit_ex(evp, emd, NULL);
121 +
122 +               for (i = 0; i + CHUNK_SIZE <= len; i += CHUNK_SIZE)
123 +                       EVP_DigestUpdate(evp, (uchar *)map_ptr(buf, i, CHUNK_SIZE), CHUNK_SIZE);
124 +
125 +               remainder = (int32)(len - i);
126 +               if (remainder > 0)
127 +                       EVP_DigestUpdate(evp, (uchar *)map_ptr(buf, i, remainder), remainder);
128 +
129 +               EVP_DigestFinal_ex(evp, (uchar *)sum, NULL);
130 +               break;
131 +         }
132  #endif
133           case CSUM_MD5: {
134                 md5_context m5;
135 @@ -457,6 +535,9 @@ static union {
136  #endif
137         md5_context m5;
138  } ctx;
139 +#ifdef SHA_DIGEST_LENGTH
140 +EVP_MD_CTX *ctx_evp = NULL;
141 +#endif
142  #ifdef SUPPORT_XXHASH
143  static XXH64_state_t* xxh64_state;
144  #endif
145 @@ -494,6 +575,17 @@ int sum_init(int csum_type, int seed)
146                         out_of_memory("sum_init");
147                 XXH3_128bits_reset(xxh3_state);
148                 break;
149 +#endif
150 +#ifdef SHA_DIGEST_LENGTH
151 +         case CSUM_SHA1:
152 +         case CSUM_SHA256:
153 +         case CSUM_SHA512: {
154 +               const EVP_MD *emd = EVP_get_digestbyname(checksum_name(csum_type));
155 +               if (!ctx_evp && !(ctx_evp = EVP_MD_CTX_create()))
156 +                       out_of_memory("file_checksum");
157 +               EVP_DigestInit_ex(ctx_evp, emd, NULL);
158 +               break;
159 +         }
160  #endif
161           case CSUM_MD5:
162                 md5_begin(&ctx.m5);
163 @@ -546,6 +638,13 @@ void sum_update(const char *p, int32 len)
164           case CSUM_XXH3_128:
165                 XXH3_128bits_update(xxh3_state, p, len);
166                 break;
167 +#endif
168 +#ifdef SHA_DIGEST_LENGTH
169 +         case CSUM_SHA1:
170 +         case CSUM_SHA256:
171 +         case CSUM_SHA512:
172 +               EVP_DigestUpdate(ctx_evp, (uchar *)p, len);
173 +               break;
174  #endif
175           case CSUM_MD5:
176                 md5_update(&ctx.m5, (uchar *)p, len);
177 @@ -611,6 +710,13 @@ void sum_end(char *sum)
178                 SIVAL64(sum, 8, digest.high64);
179                 break;
180           }
181 +#endif
182 +#ifdef SHA_DIGEST_LENGTH
183 +         case CSUM_SHA1:
184 +         case CSUM_SHA256:
185 +         case CSUM_SHA512:
186 +               EVP_DigestFinal_ex(ctx_evp, (uchar *)sum, NULL);
187 +               break;
188  #endif
189           case CSUM_MD5:
190                 md5_result(&ctx.m5, (uchar *)sum);
191 diff --git a/lib/md-defines.h b/lib/md-defines.h
192 --- a/lib/md-defines.h
193 +++ b/lib/md-defines.h
194 @@ -1,8 +1,19 @@
195  /* Keep this simple so both C and ASM can use it */
196  
197 +/*#undef SHA512_DIGEST_LENGTH*/
198 +/*#undef SHA256_DIGEST_LENGTH*/
199 +
200  #define MD4_DIGEST_LEN 16
201  #define MD5_DIGEST_LEN 16
202 +#if defined SHA512_DIGEST_LENGTH
203 +#define MAX_DIGEST_LEN SHA512_DIGEST_LENGTH
204 +#elif defined SHA256_DIGEST_LENGTH
205 +#define MAX_DIGEST_LEN SHA256_DIGEST_LENGTH
206 +#elif defined SHA_DIGEST_LENGTH
207 +#define MAX_DIGEST_LEN SHA_DIGEST_LENGTH
208 +#else
209  #define MAX_DIGEST_LEN MD5_DIGEST_LEN
210 +#endif
211  
212  #define CSUM_CHUNK 64
213  
214 @@ -15,3 +26,6 @@
215  #define CSUM_XXH64 6
216  #define CSUM_XXH3_64 7
217  #define CSUM_XXH3_128 8
218 +#define CSUM_SHA1 9
219 +#define CSUM_SHA256 10
220 +#define CSUM_SHA512 11
221 diff --git a/lib/mdigest.h b/lib/mdigest.h
222 --- a/lib/mdigest.h
223 +++ b/lib/mdigest.h
224 @@ -3,6 +3,8 @@
225  #ifdef USE_OPENSSL
226  #include "openssl/md4.h"
227  #include "openssl/md5.h"
228 +#include <openssl/sha.h>
229 +#include <openssl/evp.h>
230  #endif
231  #include "md-defines.h"
232