Fix C++ compilation warnings/errors under g++ 8.2.
[cumulus.git] / third_party / sha1.cc
1 /* sha1.cc - Functions to compute SHA1 message digest of data streams
2  * according to the NIST specification FIPS-180-1.
3  * part of Cumulus: Efficient Filesystem Backup to the Cloud
4  *
5  * Copyright (C) 2000, 2001, 2003, 2004, 2005 Free Software Foundation, Inc.
6  * Copyright (C) 2006-2007  The Regents of the University of California
7  * Copyright (C) 2012 The Cumulus Developers
8  * See the AUTHORS file for a list of Cumulus contributors.
9  *
10  * Written by Scott G. Miller
11  * Additional Credits:
12  *    Robert Klep <robert@ilse.nl>  -- Expansion function fix
13  * Modifications by Michael Vrable <mvrable@cs.ucsd.edu> to integrate into
14  * Cumulus.
15  *
16  * Original code (in C) is taken from GNU coreutils (Debian package 5.97-5).
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License along
29  * with this program; if not, write to the Free Software Foundation, Inc.,
30  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31  */
32
33 #include "../hash.h"
34 #include "sha1.h"
35
36 #include <stddef.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <arpa/inet.h>
40
41 #include <string.h>
42
43 using std::string;
44
45 /* SWAP does an endian swap on architectures that are little-endian,
46    as SHA1 needs some data in a big-endian form.  */
47 #define SWAP(n) htonl(n)
48
49 #define BLOCKSIZE 4096
50 #if BLOCKSIZE % 64 != 0
51 # error "invalid BLOCKSIZE"
52 #endif
53
54 /* This array contains the bytes used to pad the buffer to the next
55    64-byte boundary.  (RFC 1321, 3.1: Step 1)  */
56 static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ...  */ };
57
58
59 /*
60   Takes a pointer to a 160 bit block of data (five 32 bit ints) and
61   intializes it to the start constants of the SHA1 algorithm.  This
62   must be called before using hash in the call to sha1_hash.
63 */
64 void
65 sha1_init_ctx (struct sha1_ctx *ctx)
66 {
67   ctx->A = 0x67452301;
68   ctx->B = 0xefcdab89;
69   ctx->C = 0x98badcfe;
70   ctx->D = 0x10325476;
71   ctx->E = 0xc3d2e1f0;
72
73   ctx->total[0] = ctx->total[1] = 0;
74   ctx->buflen = 0;
75 }
76
77 /* Put result from CTX in first 20 bytes following RESBUF.  The result
78    must be in little endian byte order.
79
80    IMPORTANT: On some systems it is required that RESBUF is correctly
81    aligned for a 32 bits value.  */
82 void *
83 sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf)
84 {
85   ((md5_uint32 *) resbuf)[0] = SWAP (ctx->A);
86   ((md5_uint32 *) resbuf)[1] = SWAP (ctx->B);
87   ((md5_uint32 *) resbuf)[2] = SWAP (ctx->C);
88   ((md5_uint32 *) resbuf)[3] = SWAP (ctx->D);
89   ((md5_uint32 *) resbuf)[4] = SWAP (ctx->E);
90
91   return resbuf;
92 }
93
94 /* Process the remaining bytes in the internal buffer and the usual
95    prolog according to the standard and write the result to RESBUF.
96
97    IMPORTANT: On some systems it is required that RESBUF is correctly
98    aligned for a 32 bits value.  */
99 void *
100 sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf)
101 {
102   /* Take yet unprocessed bytes into account.  */
103   md5_uint32 bytes = ctx->buflen;
104   size_t pad;
105
106   /* Now count remaining bytes.  */
107   ctx->total[0] += bytes;
108   if (ctx->total[0] < bytes)
109     ++ctx->total[1];
110
111   pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
112   memcpy (&ctx->buffer[bytes], fillbuf, pad);
113
114   /* Put the 64-bit file length in *bits* at the end of the buffer.  */
115   *(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = SWAP (ctx->total[0] << 3);
116   *(md5_uint32 *) &ctx->buffer[bytes + pad] = SWAP ((ctx->total[1] << 3) |
117                                                     (ctx->total[0] >> 29));
118
119   /* Process last bytes.  */
120   sha1_process_block (ctx->buffer, bytes + pad + 8, ctx);
121
122   return sha1_read_ctx (ctx, resbuf);
123 }
124
125 void
126 sha1_process_bytes (const void *buffer, size_t len, struct sha1_ctx *ctx)
127 {
128   /* When we already have some bits in our internal buffer concatenate
129      both inputs first.  */
130   if (ctx->buflen != 0)
131     {
132       size_t left_over = ctx->buflen;
133       size_t add = 128 - left_over > len ? len : 128 - left_over;
134
135       memcpy (&ctx->buffer[left_over], buffer, add);
136       ctx->buflen += add;
137
138       if (ctx->buflen > 64)
139         {
140           sha1_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
141
142           ctx->buflen &= 63;
143           /* The regions in the following copy operation cannot overlap.  */
144           memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
145                   ctx->buflen);
146         }
147
148       buffer = (const char *) buffer + add;
149       len -= add;
150     }
151
152   /* Process available complete blocks.  */
153   if (len >= 64)
154     {
155 #if !_STRING_ARCH_unaligned
156 # define alignof(type) offsetof (struct { char c; type x; }, x)
157 # define UNALIGNED_P(p) (1)
158       if (UNALIGNED_P (buffer))
159         while (len > 64)
160           {
161             sha1_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
162             buffer = (const char *) buffer + 64;
163             len -= 64;
164           }
165       else
166 #endif
167         {
168           sha1_process_block (buffer, len & ~63, ctx);
169           buffer = (const char *) buffer + (len & ~63);
170           len &= 63;
171         }
172     }
173
174   /* Move remaining bytes in internal buffer.  */
175   if (len > 0)
176     {
177       size_t left_over = ctx->buflen;
178
179       memcpy (&ctx->buffer[left_over], buffer, len);
180       left_over += len;
181       if (left_over >= 64)
182         {
183           sha1_process_block (ctx->buffer, 64, ctx);
184           left_over -= 64;
185           memcpy (ctx->buffer, &ctx->buffer[64], left_over);
186         }
187       ctx->buflen = left_over;
188     }
189 }
190
191 /* --- Code below is the primary difference between md5.c and sha1.c --- */
192
193 /* SHA1 round constants */
194 #define K1 0x5a827999L
195 #define K2 0x6ed9eba1L
196 #define K3 0x8f1bbcdcL
197 #define K4 0xca62c1d6L
198
199 /* Round functions.  Note that F2 is the same as F4.  */
200 #define F1(B,C,D) ( D ^ ( B & ( C ^ D ) ) )
201 #define F2(B,C,D) (B ^ C ^ D)
202 #define F3(B,C,D) ( ( B & C ) | ( D & ( B | C ) ) )
203 #define F4(B,C,D) (B ^ C ^ D)
204
205 /* Process LEN bytes of BUFFER, accumulating context into CTX.
206    It is assumed that LEN % 64 == 0.
207    Most of this code comes from GnuPG's cipher/sha1.c.  */
208
209 void
210 sha1_process_block (const void *buffer, size_t len, struct sha1_ctx *ctx)
211 {
212   const md5_uint32 *words = (const md5_uint32 *)buffer;
213   size_t nwords = len / sizeof (md5_uint32);
214   const md5_uint32 *endp = words + nwords;
215   md5_uint32 x[16];
216   md5_uint32 a = ctx->A;
217   md5_uint32 b = ctx->B;
218   md5_uint32 c = ctx->C;
219   md5_uint32 d = ctx->D;
220   md5_uint32 e = ctx->E;
221
222   /* First increment the byte count.  RFC 1321 specifies the possible
223      length of the file up to 2^64 bits.  Here we only compute the
224      number of bytes.  Do a double word increment.  */
225   ctx->total[0] += len;
226   if (ctx->total[0] < len)
227     ++ctx->total[1];
228
229 #define rol(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
230
231 #define M(I) ( tm =   x[I&0x0f] ^ x[(I-14)&0x0f] \
232                     ^ x[(I-8)&0x0f] ^ x[(I-3)&0x0f] \
233                , (x[I&0x0f] = rol(tm, 1)) )
234
235 #define R(A,B,C,D,E,F,K,M)  do { E += rol( A, 5 )     \
236                                       + F( B, C, D )  \
237                                       + K             \
238                                       + M;            \
239                                  B = rol( B, 30 );    \
240                                } while(0)
241
242   while (words < endp)
243     {
244       md5_uint32 tm;
245       int t;
246       for (t = 0; t < 16; t++)
247         {
248           x[t] = SWAP (*words);
249           words++;
250         }
251
252       R( a, b, c, d, e, F1, K1, x[ 0] );
253       R( e, a, b, c, d, F1, K1, x[ 1] );
254       R( d, e, a, b, c, F1, K1, x[ 2] );
255       R( c, d, e, a, b, F1, K1, x[ 3] );
256       R( b, c, d, e, a, F1, K1, x[ 4] );
257       R( a, b, c, d, e, F1, K1, x[ 5] );
258       R( e, a, b, c, d, F1, K1, x[ 6] );
259       R( d, e, a, b, c, F1, K1, x[ 7] );
260       R( c, d, e, a, b, F1, K1, x[ 8] );
261       R( b, c, d, e, a, F1, K1, x[ 9] );
262       R( a, b, c, d, e, F1, K1, x[10] );
263       R( e, a, b, c, d, F1, K1, x[11] );
264       R( d, e, a, b, c, F1, K1, x[12] );
265       R( c, d, e, a, b, F1, K1, x[13] );
266       R( b, c, d, e, a, F1, K1, x[14] );
267       R( a, b, c, d, e, F1, K1, x[15] );
268       R( e, a, b, c, d, F1, K1, M(16) );
269       R( d, e, a, b, c, F1, K1, M(17) );
270       R( c, d, e, a, b, F1, K1, M(18) );
271       R( b, c, d, e, a, F1, K1, M(19) );
272       R( a, b, c, d, e, F2, K2, M(20) );
273       R( e, a, b, c, d, F2, K2, M(21) );
274       R( d, e, a, b, c, F2, K2, M(22) );
275       R( c, d, e, a, b, F2, K2, M(23) );
276       R( b, c, d, e, a, F2, K2, M(24) );
277       R( a, b, c, d, e, F2, K2, M(25) );
278       R( e, a, b, c, d, F2, K2, M(26) );
279       R( d, e, a, b, c, F2, K2, M(27) );
280       R( c, d, e, a, b, F2, K2, M(28) );
281       R( b, c, d, e, a, F2, K2, M(29) );
282       R( a, b, c, d, e, F2, K2, M(30) );
283       R( e, a, b, c, d, F2, K2, M(31) );
284       R( d, e, a, b, c, F2, K2, M(32) );
285       R( c, d, e, a, b, F2, K2, M(33) );
286       R( b, c, d, e, a, F2, K2, M(34) );
287       R( a, b, c, d, e, F2, K2, M(35) );
288       R( e, a, b, c, d, F2, K2, M(36) );
289       R( d, e, a, b, c, F2, K2, M(37) );
290       R( c, d, e, a, b, F2, K2, M(38) );
291       R( b, c, d, e, a, F2, K2, M(39) );
292       R( a, b, c, d, e, F3, K3, M(40) );
293       R( e, a, b, c, d, F3, K3, M(41) );
294       R( d, e, a, b, c, F3, K3, M(42) );
295       R( c, d, e, a, b, F3, K3, M(43) );
296       R( b, c, d, e, a, F3, K3, M(44) );
297       R( a, b, c, d, e, F3, K3, M(45) );
298       R( e, a, b, c, d, F3, K3, M(46) );
299       R( d, e, a, b, c, F3, K3, M(47) );
300       R( c, d, e, a, b, F3, K3, M(48) );
301       R( b, c, d, e, a, F3, K3, M(49) );
302       R( a, b, c, d, e, F3, K3, M(50) );
303       R( e, a, b, c, d, F3, K3, M(51) );
304       R( d, e, a, b, c, F3, K3, M(52) );
305       R( c, d, e, a, b, F3, K3, M(53) );
306       R( b, c, d, e, a, F3, K3, M(54) );
307       R( a, b, c, d, e, F3, K3, M(55) );
308       R( e, a, b, c, d, F3, K3, M(56) );
309       R( d, e, a, b, c, F3, K3, M(57) );
310       R( c, d, e, a, b, F3, K3, M(58) );
311       R( b, c, d, e, a, F3, K3, M(59) );
312       R( a, b, c, d, e, F4, K4, M(60) );
313       R( e, a, b, c, d, F4, K4, M(61) );
314       R( d, e, a, b, c, F4, K4, M(62) );
315       R( c, d, e, a, b, F4, K4, M(63) );
316       R( b, c, d, e, a, F4, K4, M(64) );
317       R( a, b, c, d, e, F4, K4, M(65) );
318       R( e, a, b, c, d, F4, K4, M(66) );
319       R( d, e, a, b, c, F4, K4, M(67) );
320       R( c, d, e, a, b, F4, K4, M(68) );
321       R( b, c, d, e, a, F4, K4, M(69) );
322       R( a, b, c, d, e, F4, K4, M(70) );
323       R( e, a, b, c, d, F4, K4, M(71) );
324       R( d, e, a, b, c, F4, K4, M(72) );
325       R( c, d, e, a, b, F4, K4, M(73) );
326       R( b, c, d, e, a, F4, K4, M(74) );
327       R( a, b, c, d, e, F4, K4, M(75) );
328       R( e, a, b, c, d, F4, K4, M(76) );
329       R( d, e, a, b, c, F4, K4, M(77) );
330       R( c, d, e, a, b, F4, K4, M(78) );
331       R( b, c, d, e, a, F4, K4, M(79) );
332
333       a = ctx->A += a;
334       b = ctx->B += b;
335       c = ctx->C += c;
336       d = ctx->D += d;
337       e = ctx->E += e;
338     }
339 }
340
341 /* ---- Object-Oriented Wrapper */
342 SHA1Checksum::SHA1Checksum()
343 {
344     sha1_init_ctx(&ctx);
345 }
346
347 SHA1Checksum::~SHA1Checksum()
348 {
349 }
350
351 void SHA1Checksum::process(const void *data, size_t len)
352 {
353     sha1_process_bytes(data, len, &ctx);
354 }
355
356 bool SHA1Checksum::process_file(const char *filename)
357 {
358     FILE *f = fopen(filename, "rb");
359     if (f == NULL)
360         return false;
361
362     while (!feof(f)) {
363         char buf[4096];
364         size_t bytes = fread(buf, 1, sizeof(buf), f);
365
366         if (ferror(f)) {
367             fclose(f);
368             return false;
369         }
370
371         process(buf, bytes);
372     }
373
374     fclose(f);
375     return true;
376 }
377
378 const uint8_t *SHA1Checksum::checksum()
379 {
380     sha1_finish_ctx(&ctx, resbuf);
381     return (const uint8_t *)resbuf;
382 }
383
384 string SHA1Checksum::checksum_str()
385 {
386     uint8_t resbuf[20];
387     char hexbuf[4];
388     string result = "sha1=";
389
390     sha1_finish_ctx(&ctx, resbuf);
391
392     for (int i = 0; i < 20; i++) {
393         sprintf(hexbuf, "%02x", resbuf[i]);
394         result += hexbuf;
395     }
396
397     return result;
398 }
399
400 class SHA1Hash : public Hash {
401 public:
402     SHA1Hash();
403     static Hash *New() { return new SHA1Hash; }
404     virtual void update(const void *data, size_t len);
405     virtual size_t digest_size() const { return 20; }
406     virtual std::string name() const { return "sha1"; }
407
408 protected:
409     const uint8_t *finalize();
410
411 private:
412     struct sha1_ctx ctx;
413     char resbuf[20] __attribute__ ((__aligned__ (__alignof__ (md5_uint32))));
414 };
415
416 SHA1Hash::SHA1Hash()
417 {
418     sha1_init_ctx(&ctx);
419 }
420
421 void SHA1Hash::update(const void *data, size_t len)
422 {
423     sha1_process_bytes(data, len, &ctx);
424 }
425
426 const uint8_t *SHA1Hash::finalize()
427 {
428     sha1_finish_ctx(&ctx, resbuf);
429     return (const uint8_t *)resbuf;
430 }
431
432 void sha1_register()
433 {
434     Hash::Register("sha1", SHA1Hash::New);
435 }