1 /* sha.c - Functions to compute the SHA1 hash (message-digest) of files
2 or blocks of memory. Complies to the NIST specification FIPS-180-1.
4 Copyright (C) 2000, 2001, 2003 Scott G. Miller
7 Robert Klep <robert@ilse.nl> -- Expansion function fix
16 #include <sys/types.h>
21 #include "unlocked-io.h"
24 Not-swap is a macro that does an endian swap on architectures that are
25 big-endian, as SHA needs some data in a little-endian format
28 #ifdef WORDS_BIGENDIAN
29 # define NOTSWAP(n) (n)
31 (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
34 (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
38 #define BLOCKSIZE 4096
39 /* Ensure that BLOCKSIZE is a multiple of 64. */
40 #if BLOCKSIZE % 64 != 0
41 /* FIXME-someday (soon?): use #error instead of this kludge. */
45 /* This array contains the bytes used to pad the buffer to the next
46 64-byte boundary. (RFC 1321, 3.1: Step 1) */
47 static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
51 Takes a pointer to a 160 bit block of data (five 32 bit ints) and
52 intializes it to the start constants of the SHA1 algorithm. This
53 must be called before using hash in the call to sha_hash
56 sha_init_ctx (struct sha_ctx *ctx)
64 ctx->total[0] = ctx->total[1] = 0;
68 /* Put result from CTX in first 20 bytes following RESBUF. The result
69 must be in little endian byte order.
71 IMPORTANT: On some systems it is required that RESBUF is correctly
72 aligned for a 32 bits value. */
74 sha_read_ctx (const struct sha_ctx *ctx, void *resbuf)
76 ((md5_uint32 *) resbuf)[0] = NOTSWAP (ctx->A);
77 ((md5_uint32 *) resbuf)[1] = NOTSWAP (ctx->B);
78 ((md5_uint32 *) resbuf)[2] = NOTSWAP (ctx->C);
79 ((md5_uint32 *) resbuf)[3] = NOTSWAP (ctx->D);
80 ((md5_uint32 *) resbuf)[4] = NOTSWAP (ctx->E);
85 /* Process the remaining bytes in the internal buffer and the usual
86 prolog according to the standard and write the result to RESBUF.
88 IMPORTANT: On some systems it is required that RESBUF is correctly
89 aligned for a 32 bits value. */
91 sha_finish_ctx (struct sha_ctx *ctx, void *resbuf)
93 /* Take yet unprocessed bytes into account. */
94 md5_uint32 bytes = ctx->buflen;
97 /* Now count remaining bytes. */
98 ctx->total[0] += bytes;
99 if (ctx->total[0] < bytes)
102 pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
103 memcpy (&ctx->buffer[bytes], fillbuf, pad);
105 /* Put the 64-bit file length in *bits* at the end of the buffer. */
106 *(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = NOTSWAP (ctx->total[0] << 3);
107 *(md5_uint32 *) &ctx->buffer[bytes + pad] = NOTSWAP ((ctx->total[1] << 3) |
108 (ctx->total[0] >> 29));
110 /* Process last bytes. */
111 sha_process_block (ctx->buffer, bytes + pad + 8, ctx);
113 return sha_read_ctx (ctx, resbuf);
116 /* Compute SHA1 message digest for bytes read from STREAM. The
117 resulting message digest number will be written into the 16 bytes
118 beginning at RESBLOCK. */
120 sha_stream (FILE *stream, void *resblock)
123 char buffer[BLOCKSIZE + 72];
126 /* Initialize the computation context. */
129 /* Iterate over full file contents. */
132 /* We read the file in blocks of BLOCKSIZE bytes. One call of the
133 computation function processes the whole buffer so that with the
134 next round of the loop another block can be read. */
138 /* Read block. Take care for partial reads. */
141 n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
145 if (sum == BLOCKSIZE)
150 /* Check for the error flag IFF N == 0, so that we don't
151 exit the loop after a partial read due to e.g., EAGAIN
155 goto process_partial_block;
158 /* We've read at least one byte, so ignore errors. But always
159 check for EOF, since feof may be true even though N > 0.
160 Otherwise, we could end up calling fread after EOF. */
162 goto process_partial_block;
165 /* Process buffer with BLOCKSIZE bytes. Note that
168 sha_process_block (buffer, BLOCKSIZE, &ctx);
171 process_partial_block:;
173 /* Process any remaining bytes. */
175 sha_process_bytes (buffer, sum, &ctx);
177 /* Construct result in desired memory. */
178 sha_finish_ctx (&ctx, resblock);
182 /* Compute MD5 message digest for LEN bytes beginning at BUFFER. The
183 result is always in little endian byte order, so that a byte-wise
184 output yields to the wanted ASCII representation of the message
187 sha_buffer (const char *buffer, size_t len, void *resblock)
191 /* Initialize the computation context. */
194 /* Process whole buffer but last len % 64 bytes. */
195 sha_process_bytes (buffer, len, &ctx);
197 /* Put result in desired memory area. */
198 return sha_finish_ctx (&ctx, resblock);
202 sha_process_bytes (const void *buffer, size_t len, struct sha_ctx *ctx)
204 /* When we already have some bits in our internal buffer concatenate
205 both inputs first. */
206 if (ctx->buflen != 0)
208 size_t left_over = ctx->buflen;
209 size_t add = 128 - left_over > len ? len : 128 - left_over;
211 memcpy (&ctx->buffer[left_over], buffer, add);
214 if (ctx->buflen > 64)
216 sha_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
219 /* The regions in the following copy operation cannot overlap. */
220 memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
224 buffer = (const char *) buffer + add;
228 /* Process available complete blocks. */
231 #if !_STRING_ARCH_unaligned
232 /* To check alignment gcc has an appropriate operator. Other
235 # define UNALIGNED_P(p) (((md5_uintptr) p) % __alignof__ (md5_uint32) != 0)
237 # define UNALIGNED_P(p) (((md5_uintptr) p) % sizeof (md5_uint32) != 0)
239 if (UNALIGNED_P (buffer))
242 sha_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
243 buffer = (const char *) buffer + 64;
249 sha_process_block (buffer, len & ~63, ctx);
250 buffer = (const char *) buffer + (len & ~63);
255 /* Move remaining bytes in internal buffer. */
258 size_t left_over = ctx->buflen;
260 memcpy (&ctx->buffer[left_over], buffer, len);
264 sha_process_block (ctx->buffer, 64, ctx);
266 memcpy (ctx->buffer, &ctx->buffer[64], left_over);
268 ctx->buflen = left_over;
272 /* --- Code below is the primary difference between md5.c and sha.c --- */
274 /* SHA1 round constants */
275 #define K1 0x5a827999L
276 #define K2 0x6ed9eba1L
277 #define K3 0x8f1bbcdcL
278 #define K4 0xca62c1d6L
280 /* Round functions. Note that F2 is the same as F4. */
281 #define F1(B,C,D) ( D ^ ( B & ( C ^ D ) ) )
282 #define F2(B,C,D) (B ^ C ^ D)
283 #define F3(B,C,D) ( ( B & C ) | ( D & ( B | C ) ) )
284 #define F4(B,C,D) (B ^ C ^ D)
286 /* Process LEN bytes of BUFFER, accumulating context into CTX.
287 It is assumed that LEN % 64 == 0.
288 Most of this code comes from GnuPG's cipher/sha1.c. */
291 sha_process_block (const void *buffer, size_t len, struct sha_ctx *ctx)
293 const md5_uint32 *words = buffer;
294 size_t nwords = len / sizeof (md5_uint32);
295 const md5_uint32 *endp = words + nwords;
297 md5_uint32 a = ctx->A;
298 md5_uint32 b = ctx->B;
299 md5_uint32 c = ctx->C;
300 md5_uint32 d = ctx->D;
301 md5_uint32 e = ctx->E;
303 /* First increment the byte count. RFC 1321 specifies the possible
304 length of the file up to 2^64 bits. Here we only compute the
305 number of bytes. Do a double word increment. */
306 ctx->total[0] += len;
307 if (ctx->total[0] < len)
310 #define M(I) ( tm = x[I&0x0f] ^ x[(I-14)&0x0f] \
311 ^ x[(I-8)&0x0f] ^ x[(I-3)&0x0f] \
312 , (x[I&0x0f] = rol(tm, 1)) )
314 #define R(A,B,C,D,E,F,K,M) do { E += rol( A, 5 ) \
325 /* FIXME: see sha1.c for a better implementation. */
326 for (t = 0; t < 16; t++)
328 x[t] = NOTSWAP (*words);
332 R( a, b, c, d, e, F1, K1, x[ 0] );
333 R( e, a, b, c, d, F1, K1, x[ 1] );
334 R( d, e, a, b, c, F1, K1, x[ 2] );
335 R( c, d, e, a, b, F1, K1, x[ 3] );
336 R( b, c, d, e, a, F1, K1, x[ 4] );
337 R( a, b, c, d, e, F1, K1, x[ 5] );
338 R( e, a, b, c, d, F1, K1, x[ 6] );
339 R( d, e, a, b, c, F1, K1, x[ 7] );
340 R( c, d, e, a, b, F1, K1, x[ 8] );
341 R( b, c, d, e, a, F1, K1, x[ 9] );
342 R( a, b, c, d, e, F1, K1, x[10] );
343 R( e, a, b, c, d, F1, K1, x[11] );
344 R( d, e, a, b, c, F1, K1, x[12] );
345 R( c, d, e, a, b, F1, K1, x[13] );
346 R( b, c, d, e, a, F1, K1, x[14] );
347 R( a, b, c, d, e, F1, K1, x[15] );
348 R( e, a, b, c, d, F1, K1, M(16) );
349 R( d, e, a, b, c, F1, K1, M(17) );
350 R( c, d, e, a, b, F1, K1, M(18) );
351 R( b, c, d, e, a, F1, K1, M(19) );
352 R( a, b, c, d, e, F2, K2, M(20) );
353 R( e, a, b, c, d, F2, K2, M(21) );
354 R( d, e, a, b, c, F2, K2, M(22) );
355 R( c, d, e, a, b, F2, K2, M(23) );
356 R( b, c, d, e, a, F2, K2, M(24) );
357 R( a, b, c, d, e, F2, K2, M(25) );
358 R( e, a, b, c, d, F2, K2, M(26) );
359 R( d, e, a, b, c, F2, K2, M(27) );
360 R( c, d, e, a, b, F2, K2, M(28) );
361 R( b, c, d, e, a, F2, K2, M(29) );
362 R( a, b, c, d, e, F2, K2, M(30) );
363 R( e, a, b, c, d, F2, K2, M(31) );
364 R( d, e, a, b, c, F2, K2, M(32) );
365 R( c, d, e, a, b, F2, K2, M(33) );
366 R( b, c, d, e, a, F2, K2, M(34) );
367 R( a, b, c, d, e, F2, K2, M(35) );
368 R( e, a, b, c, d, F2, K2, M(36) );
369 R( d, e, a, b, c, F2, K2, M(37) );
370 R( c, d, e, a, b, F2, K2, M(38) );
371 R( b, c, d, e, a, F2, K2, M(39) );
372 R( a, b, c, d, e, F3, K3, M(40) );
373 R( e, a, b, c, d, F3, K3, M(41) );
374 R( d, e, a, b, c, F3, K3, M(42) );
375 R( c, d, e, a, b, F3, K3, M(43) );
376 R( b, c, d, e, a, F3, K3, M(44) );
377 R( a, b, c, d, e, F3, K3, M(45) );
378 R( e, a, b, c, d, F3, K3, M(46) );
379 R( d, e, a, b, c, F3, K3, M(47) );
380 R( c, d, e, a, b, F3, K3, M(48) );
381 R( b, c, d, e, a, F3, K3, M(49) );
382 R( a, b, c, d, e, F3, K3, M(50) );
383 R( e, a, b, c, d, F3, K3, M(51) );
384 R( d, e, a, b, c, F3, K3, M(52) );
385 R( c, d, e, a, b, F3, K3, M(53) );
386 R( b, c, d, e, a, F3, K3, M(54) );
387 R( a, b, c, d, e, F3, K3, M(55) );
388 R( e, a, b, c, d, F3, K3, M(56) );
389 R( d, e, a, b, c, F3, K3, M(57) );
390 R( c, d, e, a, b, F3, K3, M(58) );
391 R( b, c, d, e, a, F3, K3, M(59) );
392 R( a, b, c, d, e, F4, K4, M(60) );
393 R( e, a, b, c, d, F4, K4, M(61) );
394 R( d, e, a, b, c, F4, K4, M(62) );
395 R( c, d, e, a, b, F4, K4, M(63) );
396 R( b, c, d, e, a, F4, K4, M(64) );
397 R( a, b, c, d, e, F4, K4, M(65) );
398 R( e, a, b, c, d, F4, K4, M(66) );
399 R( d, e, a, b, c, F4, K4, M(67) );
400 R( c, d, e, a, b, F4, K4, M(68) );
401 R( b, c, d, e, a, F4, K4, M(69) );
402 R( a, b, c, d, e, F4, K4, M(70) );
403 R( e, a, b, c, d, F4, K4, M(71) );
404 R( d, e, a, b, c, F4, K4, M(72) );
405 R( c, d, e, a, b, F4, K4, M(73) );
406 R( b, c, d, e, a, F4, K4, M(74) );
407 R( a, b, c, d, e, F4, K4, M(75) );
408 R( e, a, b, c, d, F4, K4, M(76) );
409 R( d, e, a, b, c, F4, K4, M(77) );
410 R( c, d, e, a, b, F4, K4, M(78) );
411 R( b, c, d, e, a, F4, K4, M(79) );