1 /* sha1.c - Functions to compute SHA1 message digest of files or
2 memory blocks according to the NIST specification FIPS-180-1.
4 Copyright (C) 2000-2001, 2003-2006, 2008-2011 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20 /* Written by Scott G. Miller
22 Robert Klep <robert@ilse.nl> -- Expansion function fix
24 Modified by Eric Biggers for wimlib: Conditionally compile in the use of
25 OpenSSL or Intel's assembly code for SHA1 block updates
31 #include "endianness.h"
34 #define SWAP(n) to_be32(n)
36 #define BLOCKSIZE 32768
37 #if BLOCKSIZE % 64 != 0
38 #error "invalid BLOCKSIZE"
44 static inline void sha1_init_ctx(SHA_CTX *ctx)
49 static inline void sha1_process_block(const void *buffer, size_t len,
52 SHA1_Update(ctx, buffer, len);
55 static inline void sha1_process_bytes(const void *buffer, size_t len,
58 SHA1_Update(ctx, buffer, len);
62 static inline void *sha1_finish_ctx(SHA_CTX *ctx, void *resbuf)
64 SHA1_Final(resbuf, ctx);
66 #else /* WITH_LIBCRYPTO */
68 /* Structure to save state of computation between the single steps. */
81 typedef struct sha1_ctx SHA_CTX;
83 #ifdef ENABLE_SSSE3_SHA1
84 extern void sha1_update_intel(int *hash, const char* input, size_t num_blocks);
86 static inline void sha1_process_block(const void *buffer, size_t len,
89 sha1_update_intel((int*)ctx, buffer, len / 64);
91 if (ctx->total[0] < len)
96 void ssse3_not_found()
99 "Cannot calculate SHA1 message digest: CPU does not support SSSE3\n"
100 "instructions! Recompile wimlib without the --enable-ssse3-sha1 flag\n"
101 "to use wimlib on this CPU.\n");
104 #else /* ENABLE_SSSE3_SHA1 */
106 static void sha1_process_block(const void *buffer, size_t len,
109 #endif /* ENABLE_SSSE3_SHA1 */
112 /* This array contains the bytes used to pad the buffer to the next
113 64-byte boundary. (RFC 1321, 3.1: Step 1) */
114 static const u8 fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
116 /* Initialize structure containing state of computation. */
117 static void sha1_init_ctx(SHA_CTX *ctx);
119 /* Starting with the result of former calls of this function (or the
120 initialization function update the context for the next LEN bytes
122 It is NOT required that LEN is a multiple of 64. */
123 static void sha1_process_bytes(const void *buffer, size_t len,
126 /* Process the remaining bytes in the buffer and put result from CTX
127 in first 20 bytes following RESBUF. The result is always in little
128 endian byte order, so that a byte-wise output yields to the wanted
129 ASCII representation of the message digest. */
130 static void *sha1_finish_ctx(SHA_CTX *ctx, void *resbuf);
132 /* Put result from CTX in first 20 bytes following RESBUF. The result is
133 always in little endian byte order, so that a byte-wise output yields
134 to the wanted ASCII representation of the message digest. */
135 static void *sha1_read_ctx(const SHA_CTX *ctx, void *resbuf);
137 #endif /* WITH_LIBCRYPTO */
141 /* Compute SHA1 message digest for bytes read from STREAM. The resulting
142 * message digest number will be written into the 20 bytes beginning at
144 int sha1_stream(FILE * stream, void *resblock)
150 char *buffer = MALLOC(BLOCKSIZE + 72);
152 ERROR("Out of memory!\n");
153 return WIMLIB_ERR_NOMEM;
156 /* Initialize the computation context. */
159 /* Iterate over full file contents. */
161 /* We read the file in blocks of BLOCKSIZE bytes. One call of the
162 computation function processes the whole buffer so that with the
163 next round of the loop another block can be read. */
167 /* Read block. Take care for partial reads. */
169 n = fread(buffer + sum, 1, BLOCKSIZE - sum, stream);
173 if (sum == BLOCKSIZE)
177 /* Check for the error flag IFF N == 0, so that
178 * we don't exit the loop after a partial read
179 * due to e.g., EAGAIN or EWOULDBLOCK. */
180 if (ferror(stream)) {
182 ERROR("Read error while calculating "
183 "SHA1 message digest: %m\n");
184 return WIMLIB_ERR_READ;
186 goto process_partial_block;
189 /* We've read at least one byte, so ignore errors. But always
190 check for EOF, since feof may be true even though N > 0.
191 Otherwise, we could end up calling fread after EOF. */
193 goto process_partial_block;
196 /* Process buffer with BLOCKSIZE bytes. Note that
199 sha1_process_block(buffer, BLOCKSIZE, &ctx);
202 process_partial_block:;
204 /* Process any remaining bytes. */
206 sha1_process_bytes(buffer, sum, &ctx);
208 /* Construct result in desired memory. */
209 sha1_finish_ctx(&ctx, resblock);
214 #ifndef WITH_LIBCRYPTO
215 /* Compute SHA1 message digest for LEN bytes beginning at BUFFER. The
216 result is always in little endian byte order, so that a byte-wise
217 output yields to the wanted ASCII representation of the message
219 void *sha1_buffer(const char *buffer, size_t len, void *resblock)
223 /* Initialize the computation context. */
226 /* Process whole buffer but last len % 64 bytes. */
227 sha1_process_bytes(buffer, len, &ctx);
229 /* Put result in desired memory area. */
230 return sha1_finish_ctx(&ctx, resblock);
233 /* Take a pointer to a 160 bit block of data (five 32 bit ints) and
234 initialize it to the start constants of the SHA1 algorithm. This
235 must be called before using hash in the call to sha1_hash. */
236 static void sha1_init_ctx(SHA_CTX *ctx)
244 ctx->total[0] = ctx->total[1] = 0;
248 /* Copy the 4 byte value from v into the memory location pointed to by *cp,
249 If your architecture allows unaligned access this is equivalent to
250 * (uint32_t *) cp = v */
251 static inline void set_uint32(char *cp, uint32_t v)
253 memcpy(cp, &v, sizeof v);
256 /* Put result from CTX in first 20 bytes following RESBUF. The result
257 must be in little endian byte order. */
258 static void *sha1_read_ctx(const SHA_CTX *ctx, void *resbuf)
261 set_uint32(r + 0 * sizeof ctx->A, SWAP(ctx->A));
262 set_uint32(r + 1 * sizeof ctx->B, SWAP(ctx->B));
263 set_uint32(r + 2 * sizeof ctx->C, SWAP(ctx->C));
264 set_uint32(r + 3 * sizeof ctx->D, SWAP(ctx->D));
265 set_uint32(r + 4 * sizeof ctx->E, SWAP(ctx->E));
270 /* Process the remaining bytes in the internal buffer and the usual
271 prolog according to the standard and write the result to RESBUF. */
272 static void *sha1_finish_ctx(SHA_CTX *ctx, void *resbuf)
274 /* Take yet unprocessed bytes into account. */
275 uint32_t bytes = ctx->buflen;
276 size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4;
278 /* Now count remaining bytes. */
279 ctx->total[0] += bytes;
280 if (ctx->total[0] < bytes)
283 /* Put the 64-bit file length in *bits* at the end of the buffer. */
284 ctx->buffer[size - 2] =
285 SWAP((ctx->total[1] << 3) | (ctx->total[0] >> 29));
286 ctx->buffer[size - 1] = SWAP(ctx->total[0] << 3);
288 memcpy(&((char *)ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes);
290 /* Process last bytes. */
291 sha1_process_block(ctx->buffer, size * 4, ctx);
293 return sha1_read_ctx(ctx, resbuf);
297 static void sha1_process_bytes(const void *buffer, size_t len, SHA_CTX *ctx)
299 /* When we already have some bits in our internal buffer concatenate
300 both inputs first. */
301 if (ctx->buflen != 0) {
302 size_t left_over = ctx->buflen;
303 size_t add = 128 - left_over > len ? len : 128 - left_over;
305 memcpy(&((char *)ctx->buffer)[left_over], buffer, add);
308 if (ctx->buflen > 64) {
309 sha1_process_block(ctx->buffer, ctx->buflen & ~63, ctx);
312 /* The regions in the following copy operation cannot overlap. */
314 &((char *)ctx->buffer)[(left_over + add) & ~63],
318 buffer = (const char *)buffer + add;
322 /* Process available complete blocks. */
324 #if !_STRING_ARCH_unaligned
325 #define alignof(type) offsetof (struct { char c; type x; }, x)
326 #define UNALIGNED_P(p) (((size_t) p) % alignof (uint32_t) != 0)
327 if (UNALIGNED_P(buffer))
329 sha1_process_block(memcpy
330 (ctx->buffer, buffer, 64),
332 buffer = (const char *)buffer + 64;
337 sha1_process_block(buffer, len & ~63, ctx);
338 buffer = (const char *)buffer + (len & ~63);
343 /* Move remaining bytes in internal buffer. */
345 size_t left_over = ctx->buflen;
347 memcpy(&((char *)ctx->buffer)[left_over], buffer, len);
349 if (left_over >= 64) {
350 sha1_process_block(ctx->buffer, 64, ctx);
352 memcpy(ctx->buffer, &ctx->buffer[16], left_over);
354 ctx->buflen = left_over;
358 /* --- Code below is the primary difference between md5.c and sha1.c --- */
360 /* SHA1 round constants */
361 #define K1 0x5a827999
362 #define K2 0x6ed9eba1
363 #define K3 0x8f1bbcdc
364 #define K4 0xca62c1d6
366 /* Round functions. Note that F2 is the same as F4. */
367 #define F1(B,C,D) ( D ^ ( B & ( C ^ D ) ) )
368 #define F2(B,C,D) (B ^ C ^ D)
369 #define F3(B,C,D) ( ( B & C ) | ( D & ( B | C ) ) )
370 #define F4(B,C,D) (B ^ C ^ D)
372 /* Process LEN bytes of BUFFER, accumulating context into CTX.
373 It is assumed that LEN % 64 == 0.
374 Most of this code comes from GnuPG's cipher/sha1.c. */
376 #ifndef ENABLE_SSSE3_SHA1
377 static void sha1_process_block(const void *buffer, size_t len, SHA_CTX *ctx)
379 const uint32_t *words = buffer;
380 size_t nwords = len / sizeof(uint32_t);
381 const uint32_t *endp = words + nwords;
389 /* First increment the byte count. RFC 1321 specifies the possible
390 length of the file up to 2^64 bits. Here we only compute the
391 number of bytes. Do a double word increment. */
392 ctx->total[0] += len;
393 if (ctx->total[0] < len)
396 #define rol(x, n) (((x) << (n)) | ((uint32_t) (x) >> (32 - (n))))
398 #define M(I) ( tm = x[I&0x0f] ^ x[(I-14)&0x0f] \
399 ^ x[(I-8)&0x0f] ^ x[(I-3)&0x0f] \
400 , (x[I&0x0f] = rol(tm, 1)) )
402 #define R(A,B,C,D,E,F,K,M) do { E += rol( A, 5 ) \
409 while (words < endp) {
412 for (t = 0; t < 16; t++) {
417 R(a, b, c, d, e, F1, K1, x[0]);
418 R(e, a, b, c, d, F1, K1, x[1]);
419 R(d, e, a, b, c, F1, K1, x[2]);
420 R(c, d, e, a, b, F1, K1, x[3]);
421 R(b, c, d, e, a, F1, K1, x[4]);
422 R(a, b, c, d, e, F1, K1, x[5]);
423 R(e, a, b, c, d, F1, K1, x[6]);
424 R(d, e, a, b, c, F1, K1, x[7]);
425 R(c, d, e, a, b, F1, K1, x[8]);
426 R(b, c, d, e, a, F1, K1, x[9]);
427 R(a, b, c, d, e, F1, K1, x[10]);
428 R(e, a, b, c, d, F1, K1, x[11]);
429 R(d, e, a, b, c, F1, K1, x[12]);
430 R(c, d, e, a, b, F1, K1, x[13]);
431 R(b, c, d, e, a, F1, K1, x[14]);
432 R(a, b, c, d, e, F1, K1, x[15]);
433 R(e, a, b, c, d, F1, K1, M(16));
434 R(d, e, a, b, c, F1, K1, M(17));
435 R(c, d, e, a, b, F1, K1, M(18));
436 R(b, c, d, e, a, F1, K1, M(19));
437 R(a, b, c, d, e, F2, K2, M(20));
438 R(e, a, b, c, d, F2, K2, M(21));
439 R(d, e, a, b, c, F2, K2, M(22));
440 R(c, d, e, a, b, F2, K2, M(23));
441 R(b, c, d, e, a, F2, K2, M(24));
442 R(a, b, c, d, e, F2, K2, M(25));
443 R(e, a, b, c, d, F2, K2, M(26));
444 R(d, e, a, b, c, F2, K2, M(27));
445 R(c, d, e, a, b, F2, K2, M(28));
446 R(b, c, d, e, a, F2, K2, M(29));
447 R(a, b, c, d, e, F2, K2, M(30));
448 R(e, a, b, c, d, F2, K2, M(31));
449 R(d, e, a, b, c, F2, K2, M(32));
450 R(c, d, e, a, b, F2, K2, M(33));
451 R(b, c, d, e, a, F2, K2, M(34));
452 R(a, b, c, d, e, F2, K2, M(35));
453 R(e, a, b, c, d, F2, K2, M(36));
454 R(d, e, a, b, c, F2, K2, M(37));
455 R(c, d, e, a, b, F2, K2, M(38));
456 R(b, c, d, e, a, F2, K2, M(39));
457 R(a, b, c, d, e, F3, K3, M(40));
458 R(e, a, b, c, d, F3, K3, M(41));
459 R(d, e, a, b, c, F3, K3, M(42));
460 R(c, d, e, a, b, F3, K3, M(43));
461 R(b, c, d, e, a, F3, K3, M(44));
462 R(a, b, c, d, e, F3, K3, M(45));
463 R(e, a, b, c, d, F3, K3, M(46));
464 R(d, e, a, b, c, F3, K3, M(47));
465 R(c, d, e, a, b, F3, K3, M(48));
466 R(b, c, d, e, a, F3, K3, M(49));
467 R(a, b, c, d, e, F3, K3, M(50));
468 R(e, a, b, c, d, F3, K3, M(51));
469 R(d, e, a, b, c, F3, K3, M(52));
470 R(c, d, e, a, b, F3, K3, M(53));
471 R(b, c, d, e, a, F3, K3, M(54));
472 R(a, b, c, d, e, F3, K3, M(55));
473 R(e, a, b, c, d, F3, K3, M(56));
474 R(d, e, a, b, c, F3, K3, M(57));
475 R(c, d, e, a, b, F3, K3, M(58));
476 R(b, c, d, e, a, F3, K3, M(59));
477 R(a, b, c, d, e, F4, K4, M(60));
478 R(e, a, b, c, d, F4, K4, M(61));
479 R(d, e, a, b, c, F4, K4, M(62));
480 R(c, d, e, a, b, F4, K4, M(63));
481 R(b, c, d, e, a, F4, K4, M(64));
482 R(a, b, c, d, e, F4, K4, M(65));
483 R(e, a, b, c, d, F4, K4, M(66));
484 R(d, e, a, b, c, F4, K4, M(67));
485 R(c, d, e, a, b, F4, K4, M(68));
486 R(b, c, d, e, a, F4, K4, M(69));
487 R(a, b, c, d, e, F4, K4, M(70));
488 R(e, a, b, c, d, F4, K4, M(71));
489 R(d, e, a, b, c, F4, K4, M(72));
490 R(c, d, e, a, b, F4, K4, M(73));
491 R(b, c, d, e, a, F4, K4, M(74));
492 R(a, b, c, d, e, F4, K4, M(75));
493 R(e, a, b, c, d, F4, K4, M(76));
494 R(d, e, a, b, c, F4, K4, M(77));
495 R(c, d, e, a, b, F4, K4, M(78));
496 R(b, c, d, e, a, F4, K4, M(79));
505 #endif /* ENABLE_SSSE3_SHA1 */
507 #endif /* WITH_LIBCRYPTO */