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
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.
10 * Written by Scott G. Miller
12 * Robert Klep <robert@ilse.nl> -- Expansion function fix
13 * Modifications by Michael Vrable <mvrable@cs.ucsd.edu> to integrate into
16 * Original code (in C) is taken from GNU coreutils (Debian package 5.97-5).
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.
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.
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.
39 #include <arpa/inet.h>
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)
49 #define BLOCKSIZE 4096
50 #if BLOCKSIZE % 64 != 0
51 # error "invalid BLOCKSIZE"
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, ... */ };
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.
65 sha1_init_ctx (struct sha1_ctx *ctx)
73 ctx->total[0] = ctx->total[1] = 0;
77 /* Put result from CTX in first 20 bytes following RESBUF. The result
78 must be in little endian byte order.
80 IMPORTANT: On some systems it is required that RESBUF is correctly
81 aligned for a 32 bits value. */
83 sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf)
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);
94 /* Process the remaining bytes in the internal buffer and the usual
95 prolog according to the standard and write the result to RESBUF.
97 IMPORTANT: On some systems it is required that RESBUF is correctly
98 aligned for a 32 bits value. */
100 sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf)
102 /* Take yet unprocessed bytes into account. */
103 md5_uint32 bytes = ctx->buflen;
106 /* Now count remaining bytes. */
107 ctx->total[0] += bytes;
108 if (ctx->total[0] < bytes)
111 pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
112 memcpy (&ctx->buffer[bytes], fillbuf, pad);
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));
119 /* Process last bytes. */
120 sha1_process_block (ctx->buffer, bytes + pad + 8, ctx);
122 return sha1_read_ctx (ctx, resbuf);
126 sha1_process_bytes (const void *buffer, size_t len, struct sha1_ctx *ctx)
128 /* When we already have some bits in our internal buffer concatenate
129 both inputs first. */
130 if (ctx->buflen != 0)
132 size_t left_over = ctx->buflen;
133 size_t add = 128 - left_over > len ? len : 128 - left_over;
135 memcpy (&ctx->buffer[left_over], buffer, add);
138 if (ctx->buflen > 64)
140 sha1_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
143 /* The regions in the following copy operation cannot overlap. */
144 memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
148 buffer = (const char *) buffer + add;
152 /* Process available complete blocks. */
155 #if !_STRING_ARCH_unaligned
156 # define alignof(type) offsetof (struct { char c; type x; }, x)
157 # define UNALIGNED_P(p) (((size_t) p) % alignof (md5_uint32) != 0)
158 if (UNALIGNED_P (buffer))
161 sha1_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
162 buffer = (const char *) buffer + 64;
168 sha1_process_block (buffer, len & ~63, ctx);
169 buffer = (const char *) buffer + (len & ~63);
174 /* Move remaining bytes in internal buffer. */
177 size_t left_over = ctx->buflen;
179 memcpy (&ctx->buffer[left_over], buffer, len);
183 sha1_process_block (ctx->buffer, 64, ctx);
185 memcpy (ctx->buffer, &ctx->buffer[64], left_over);
187 ctx->buflen = left_over;
191 /* --- Code below is the primary difference between md5.c and sha1.c --- */
193 /* SHA1 round constants */
194 #define K1 0x5a827999L
195 #define K2 0x6ed9eba1L
196 #define K3 0x8f1bbcdcL
197 #define K4 0xca62c1d6L
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)
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. */
210 sha1_process_block (const void *buffer, size_t len, struct sha1_ctx *ctx)
212 const md5_uint32 *words = (const md5_uint32 *)buffer;
213 size_t nwords = len / sizeof (md5_uint32);
214 const md5_uint32 *endp = words + nwords;
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;
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)
229 #define rol(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
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)) )
235 #define R(A,B,C,D,E,F,K,M) do { E += rol( A, 5 ) \
246 for (t = 0; t < 16; t++)
248 x[t] = SWAP (*words);
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) );
341 /* ---- Object-Oriented Wrapper */
342 SHA1Checksum::SHA1Checksum()
347 SHA1Checksum::~SHA1Checksum()
351 void SHA1Checksum::process(const void *data, size_t len)
353 sha1_process_bytes(data, len, &ctx);
356 bool SHA1Checksum::process_file(const char *filename)
358 FILE *f = fopen(filename, "rb");
364 size_t bytes = fread(buf, 1, sizeof(buf), f);
378 const uint8_t *SHA1Checksum::checksum()
380 sha1_finish_ctx(&ctx, resbuf);
381 return (const uint8_t *)resbuf;
384 string SHA1Checksum::checksum_str()
388 string result = "sha1=";
390 sha1_finish_ctx(&ctx, resbuf);
392 for (int i = 0; i < 20; i++) {
393 sprintf(hexbuf, "%02x", resbuf[i]);
400 class SHA1Hash : public Hash {
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"; }
409 const uint8_t *finalize();
413 char resbuf[20] __attribute__ ((__aligned__ (__alignof__ (md5_uint32))));
421 void SHA1Hash::update(const void *data, size_t len)
423 sha1_process_bytes(data, len, &ctx);
426 const uint8_t *SHA1Hash::finalize()
428 sha1_finish_ctx(&ctx, resbuf);
429 return (const uint8_t *)resbuf;
434 Hash::Register("sha1", SHA1Hash::New);