1
/* -*- buffer-read-only: t -*- vi: set ro: */
2
/* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3
/* sha1.c - Functions to compute SHA1 message digest of files or
4
memory blocks according to the NIST specification FIPS-180-1.
6
Copyright (C) 2000, 2001, 2003, 2004, 2005, 2006, 2008, 2009, 2010 Free
7
Software Foundation, Inc.
9
This program is free software; you can redistribute it and/or modify it
10
under the terms of the GNU General Public License as published by the
11
Free Software Foundation; either version 3, or (at your option) any
14
This program is distributed in the hope that it will be useful,
15
but WITHOUT ANY WARRANTY; without even the implied warranty of
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
GNU General Public License for more details.
19
You should have received a copy of the GNU General Public License
20
along with this program; if not, write to the Free Software Foundation,
21
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23
/* Written by Scott G. Miller
25
Robert Klep <robert@ilse.nl> -- Expansion function fix
37
# include "unlocked-io.h"
40
#ifdef WORDS_BIGENDIAN
44
(((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
47
#define BLOCKSIZE 32768
48
#if BLOCKSIZE % 64 != 0
49
# error "invalid BLOCKSIZE"
52
/* This array contains the bytes used to pad the buffer to the next
53
64-byte boundary. (RFC 1321, 3.1: Step 1) */
54
static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
57
/* Take a pointer to a 160 bit block of data (five 32 bit ints) and
58
initialize it to the start constants of the SHA1 algorithm. This
59
must be called before using hash in the call to sha1_hash. */
61
sha1_init_ctx (struct sha1_ctx *ctx)
69
ctx->total[0] = ctx->total[1] = 0;
73
/* Copy the 4 byte value from v into the memory location pointed to by *cp,
74
If your architecture allows unaligned access this is equivalent to
75
* (uint32_t *) cp = v */
77
set_uint32 (char *cp, uint32_t v)
79
memcpy (cp, &v, sizeof v);
82
/* Put result from CTX in first 20 bytes following RESBUF. The result
83
must be in little endian byte order. */
85
sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf)
88
set_uint32 (r + 0 * sizeof ctx->A, SWAP (ctx->A));
89
set_uint32 (r + 1 * sizeof ctx->B, SWAP (ctx->B));
90
set_uint32 (r + 2 * sizeof ctx->C, SWAP (ctx->C));
91
set_uint32 (r + 3 * sizeof ctx->D, SWAP (ctx->D));
92
set_uint32 (r + 4 * sizeof ctx->E, SWAP (ctx->E));
97
/* Process the remaining bytes in the internal buffer and the usual
98
prolog according to the standard and write the result to RESBUF. */
100
sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf)
102
/* Take yet unprocessed bytes into account. */
103
uint32_t bytes = ctx->buflen;
104
size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4;
106
/* Now count remaining bytes. */
107
ctx->total[0] += bytes;
108
if (ctx->total[0] < bytes)
111
/* Put the 64-bit file length in *bits* at the end of the buffer. */
112
ctx->buffer[size - 2] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29));
113
ctx->buffer[size - 1] = SWAP (ctx->total[0] << 3);
115
memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes);
117
/* Process last bytes. */
118
sha1_process_block (ctx->buffer, size * 4, ctx);
120
return sha1_read_ctx (ctx, resbuf);
123
/* Compute SHA1 message digest for bytes read from STREAM. The
124
resulting message digest number will be written into the 16 bytes
125
beginning at RESBLOCK. */
127
sha1_stream (FILE *stream, void *resblock)
132
char *buffer = malloc (BLOCKSIZE + 72);
136
/* Initialize the computation context. */
137
sha1_init_ctx (&ctx);
139
/* Iterate over full file contents. */
142
/* We read the file in blocks of BLOCKSIZE bytes. One call of the
143
computation function processes the whole buffer so that with the
144
next round of the loop another block can be read. */
148
/* Read block. Take care for partial reads. */
151
n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
155
if (sum == BLOCKSIZE)
160
/* Check for the error flag IFF N == 0, so that we don't
161
exit the loop after a partial read due to e.g., EAGAIN
168
goto process_partial_block;
171
/* We've read at least one byte, so ignore errors. But always
172
check for EOF, since feof may be true even though N > 0.
173
Otherwise, we could end up calling fread after EOF. */
175
goto process_partial_block;
178
/* Process buffer with BLOCKSIZE bytes. Note that
181
sha1_process_block (buffer, BLOCKSIZE, &ctx);
184
process_partial_block:;
186
/* Process any remaining bytes. */
188
sha1_process_bytes (buffer, sum, &ctx);
190
/* Construct result in desired memory. */
191
sha1_finish_ctx (&ctx, resblock);
196
/* Compute SHA1 message digest for LEN bytes beginning at BUFFER. The
197
result is always in little endian byte order, so that a byte-wise
198
output yields to the wanted ASCII representation of the message
201
sha1_buffer (const char *buffer, size_t len, void *resblock)
205
/* Initialize the computation context. */
206
sha1_init_ctx (&ctx);
208
/* Process whole buffer but last len % 64 bytes. */
209
sha1_process_bytes (buffer, len, &ctx);
211
/* Put result in desired memory area. */
212
return sha1_finish_ctx (&ctx, resblock);
216
sha1_process_bytes (const void *buffer, size_t len, struct sha1_ctx *ctx)
218
/* When we already have some bits in our internal buffer concatenate
219
both inputs first. */
220
if (ctx->buflen != 0)
222
size_t left_over = ctx->buflen;
223
size_t add = 128 - left_over > len ? len : 128 - left_over;
225
memcpy (&((char *) ctx->buffer)[left_over], buffer, add);
228
if (ctx->buflen > 64)
230
sha1_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
233
/* The regions in the following copy operation cannot overlap. */
235
&((char *) ctx->buffer)[(left_over + add) & ~63],
239
buffer = (const char *) buffer + add;
243
/* Process available complete blocks. */
246
#if !_STRING_ARCH_unaligned
247
# define alignof(type) offsetof (struct { char c; type x; }, x)
248
# define UNALIGNED_P(p) (((size_t) p) % alignof (uint32_t) != 0)
249
if (UNALIGNED_P (buffer))
252
sha1_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
253
buffer = (const char *) buffer + 64;
259
sha1_process_block (buffer, len & ~63, ctx);
260
buffer = (const char *) buffer + (len & ~63);
265
/* Move remaining bytes in internal buffer. */
268
size_t left_over = ctx->buflen;
270
memcpy (&((char *) ctx->buffer)[left_over], buffer, len);
274
sha1_process_block (ctx->buffer, 64, ctx);
276
memcpy (ctx->buffer, &ctx->buffer[16], left_over);
278
ctx->buflen = left_over;
282
/* --- Code below is the primary difference between md5.c and sha1.c --- */
284
/* SHA1 round constants */
285
#define K1 0x5a827999
286
#define K2 0x6ed9eba1
287
#define K3 0x8f1bbcdc
288
#define K4 0xca62c1d6
290
/* Round functions. Note that F2 is the same as F4. */
291
#define F1(B,C,D) ( D ^ ( B & ( C ^ D ) ) )
292
#define F2(B,C,D) (B ^ C ^ D)
293
#define F3(B,C,D) ( ( B & C ) | ( D & ( B | C ) ) )
294
#define F4(B,C,D) (B ^ C ^ D)
296
/* Process LEN bytes of BUFFER, accumulating context into CTX.
297
It is assumed that LEN % 64 == 0.
298
Most of this code comes from GnuPG's cipher/sha1.c. */
301
sha1_process_block (const void *buffer, size_t len, struct sha1_ctx *ctx)
303
const uint32_t *words = buffer;
304
size_t nwords = len / sizeof (uint32_t);
305
const uint32_t *endp = words + nwords;
313
/* First increment the byte count. RFC 1321 specifies the possible
314
length of the file up to 2^64 bits. Here we only compute the
315
number of bytes. Do a double word increment. */
316
ctx->total[0] += len;
317
if (ctx->total[0] < len)
320
#define rol(x, n) (((x) << (n)) | ((uint32_t) (x) >> (32 - (n))))
322
#define M(I) ( tm = x[I&0x0f] ^ x[(I-14)&0x0f] \
323
^ x[(I-8)&0x0f] ^ x[(I-3)&0x0f] \
324
, (x[I&0x0f] = rol(tm, 1)) )
326
#define R(A,B,C,D,E,F,K,M) do { E += rol( A, 5 ) \
337
for (t = 0; t < 16; t++)
339
x[t] = SWAP (*words);
343
R( a, b, c, d, e, F1, K1, x[ 0] );
344
R( e, a, b, c, d, F1, K1, x[ 1] );
345
R( d, e, a, b, c, F1, K1, x[ 2] );
346
R( c, d, e, a, b, F1, K1, x[ 3] );
347
R( b, c, d, e, a, F1, K1, x[ 4] );
348
R( a, b, c, d, e, F1, K1, x[ 5] );
349
R( e, a, b, c, d, F1, K1, x[ 6] );
350
R( d, e, a, b, c, F1, K1, x[ 7] );
351
R( c, d, e, a, b, F1, K1, x[ 8] );
352
R( b, c, d, e, a, F1, K1, x[ 9] );
353
R( a, b, c, d, e, F1, K1, x[10] );
354
R( e, a, b, c, d, F1, K1, x[11] );
355
R( d, e, a, b, c, F1, K1, x[12] );
356
R( c, d, e, a, b, F1, K1, x[13] );
357
R( b, c, d, e, a, F1, K1, x[14] );
358
R( a, b, c, d, e, F1, K1, x[15] );
359
R( e, a, b, c, d, F1, K1, M(16) );
360
R( d, e, a, b, c, F1, K1, M(17) );
361
R( c, d, e, a, b, F1, K1, M(18) );
362
R( b, c, d, e, a, F1, K1, M(19) );
363
R( a, b, c, d, e, F2, K2, M(20) );
364
R( e, a, b, c, d, F2, K2, M(21) );
365
R( d, e, a, b, c, F2, K2, M(22) );
366
R( c, d, e, a, b, F2, K2, M(23) );
367
R( b, c, d, e, a, F2, K2, M(24) );
368
R( a, b, c, d, e, F2, K2, M(25) );
369
R( e, a, b, c, d, F2, K2, M(26) );
370
R( d, e, a, b, c, F2, K2, M(27) );
371
R( c, d, e, a, b, F2, K2, M(28) );
372
R( b, c, d, e, a, F2, K2, M(29) );
373
R( a, b, c, d, e, F2, K2, M(30) );
374
R( e, a, b, c, d, F2, K2, M(31) );
375
R( d, e, a, b, c, F2, K2, M(32) );
376
R( c, d, e, a, b, F2, K2, M(33) );
377
R( b, c, d, e, a, F2, K2, M(34) );
378
R( a, b, c, d, e, F2, K2, M(35) );
379
R( e, a, b, c, d, F2, K2, M(36) );
380
R( d, e, a, b, c, F2, K2, M(37) );
381
R( c, d, e, a, b, F2, K2, M(38) );
382
R( b, c, d, e, a, F2, K2, M(39) );
383
R( a, b, c, d, e, F3, K3, M(40) );
384
R( e, a, b, c, d, F3, K3, M(41) );
385
R( d, e, a, b, c, F3, K3, M(42) );
386
R( c, d, e, a, b, F3, K3, M(43) );
387
R( b, c, d, e, a, F3, K3, M(44) );
388
R( a, b, c, d, e, F3, K3, M(45) );
389
R( e, a, b, c, d, F3, K3, M(46) );
390
R( d, e, a, b, c, F3, K3, M(47) );
391
R( c, d, e, a, b, F3, K3, M(48) );
392
R( b, c, d, e, a, F3, K3, M(49) );
393
R( a, b, c, d, e, F3, K3, M(50) );
394
R( e, a, b, c, d, F3, K3, M(51) );
395
R( d, e, a, b, c, F3, K3, M(52) );
396
R( c, d, e, a, b, F3, K3, M(53) );
397
R( b, c, d, e, a, F3, K3, M(54) );
398
R( a, b, c, d, e, F3, K3, M(55) );
399
R( e, a, b, c, d, F3, K3, M(56) );
400
R( d, e, a, b, c, F3, K3, M(57) );
401
R( c, d, e, a, b, F3, K3, M(58) );
402
R( b, c, d, e, a, F3, K3, M(59) );
403
R( a, b, c, d, e, F4, K4, M(60) );
404
R( e, a, b, c, d, F4, K4, M(61) );
405
R( d, e, a, b, c, F4, K4, M(62) );
406
R( c, d, e, a, b, F4, K4, M(63) );
407
R( b, c, d, e, a, F4, K4, M(64) );
408
R( a, b, c, d, e, F4, K4, M(65) );
409
R( e, a, b, c, d, F4, K4, M(66) );
410
R( d, e, a, b, c, F4, K4, M(67) );
411
R( c, d, e, a, b, F4, K4, M(68) );
412
R( b, c, d, e, a, F4, K4, M(69) );
413
R( a, b, c, d, e, F4, K4, M(70) );
414
R( e, a, b, c, d, F4, K4, M(71) );
415
R( d, e, a, b, c, F4, K4, M(72) );
416
R( c, d, e, a, b, F4, K4, M(73) );
417
R( b, c, d, e, a, F4, K4, M(74) );
418
R( a, b, c, d, e, F4, K4, M(75) );
419
R( e, a, b, c, d, F4, K4, M(76) );
420
R( d, e, a, b, c, F4, K4, M(77) );
421
R( c, d, e, a, b, F4, K4, M(78) );
422
R( b, c, d, e, a, F4, K4, M(79) );