Branch data Line data Source code
1 : : /* sha1.c - Functions to compute SHA1 message digest of files or
2 : : memory blocks according to the NIST specification FIPS-180-1.
3 : :
4 : : Copyright (C) 2000-2001, 2003-2006, 2008-2012 Free Software Foundation, Inc.
5 : :
6 : : This program is free software; you can redistribute it and/or modify it
7 : : under the terms of the GNU Lesser General Public License as published by the
8 : : Free Software Foundation; either version 2.1, or (at your option) any
9 : : later version.
10 : :
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 Lesser General Public License for more details.
15 : :
16 : : You should have received a copy of the GNU Lesser General Public License
17 : : along with this program; if not, see <http://www.gnu.org/licenses/>. */
18 : :
19 : : /* Written by Scott G. Miller
20 : : Credits:
21 : : Robert Klep <robert@ilse.nl> -- Expansion function fix
22 : : */
23 : :
24 : : #include <config.h>
25 : :
26 : : #include "sha1.h"
27 : :
28 : : #include <stdalign.h>
29 : : #include <stdint.h>
30 : : #include <stdlib.h>
31 : : #include <string.h>
32 : :
33 : : #if USE_UNLOCKED_IO
34 : : # include "unlocked-io.h"
35 : : #endif
36 : :
37 : : #ifdef WORDS_BIGENDIAN
38 : : # define SWAP(n) (n)
39 : : #else
40 : : # define SWAP(n) \
41 : : (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
42 : : #endif
43 : :
44 : : #define BLOCKSIZE 32768
45 : : #if BLOCKSIZE % 64 != 0
46 : : # error "invalid BLOCKSIZE"
47 : : #endif
48 : :
49 : : /* This array contains the bytes used to pad the buffer to the next
50 : : 64-byte boundary. (RFC 1321, 3.1: Step 1) */
51 : : static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
52 : :
53 : :
54 : : /* Take a pointer to a 160 bit block of data (five 32 bit ints) and
55 : : initialize it to the start constants of the SHA1 algorithm. This
56 : : must be called before using hash in the call to sha1_hash. */
57 : : void
58 : 7 : sha1_init_ctx (struct sha1_ctx *ctx)
59 : : {
60 : 7 : ctx->A = 0x67452301;
61 : 7 : ctx->B = 0xefcdab89;
62 : 7 : ctx->C = 0x98badcfe;
63 : 7 : ctx->D = 0x10325476;
64 : 7 : ctx->E = 0xc3d2e1f0;
65 : :
66 : 7 : ctx->total[0] = ctx->total[1] = 0;
67 : 7 : ctx->buflen = 0;
68 : 7 : }
69 : :
70 : : /* Copy the 4 byte value from v into the memory location pointed to by *cp,
71 : : If your architecture allows unaligned access this is equivalent to
72 : : * (uint32_t *) cp = v */
73 : : static inline void
74 : 35 : set_uint32 (char *cp, uint32_t v)
75 : : {
76 : 35 : memcpy (cp, &v, sizeof v);
77 : 35 : }
78 : :
79 : : /* Put result from CTX in first 20 bytes following RESBUF. The result
80 : : must be in little endian byte order. */
81 : : void *
82 : 7 : sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf)
83 : : {
84 : 7 : char *r = resbuf;
85 : 7 : set_uint32 (r + 0 * sizeof ctx->A, SWAP (ctx->A));
86 : 7 : set_uint32 (r + 1 * sizeof ctx->B, SWAP (ctx->B));
87 : 7 : set_uint32 (r + 2 * sizeof ctx->C, SWAP (ctx->C));
88 : 7 : set_uint32 (r + 3 * sizeof ctx->D, SWAP (ctx->D));
89 : 7 : set_uint32 (r + 4 * sizeof ctx->E, SWAP (ctx->E));
90 : :
91 : 7 : 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 : : void *
97 : 7 : sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf)
98 : : {
99 : : /* Take yet unprocessed bytes into account. */
100 : 7 : uint32_t bytes = ctx->buflen;
101 [ + - ]: 7 : size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4;
102 : :
103 : : /* Now count remaining bytes. */
104 : 7 : ctx->total[0] += bytes;
105 [ - + ]: 7 : if (ctx->total[0] < bytes)
106 : 0 : ++ctx->total[1];
107 : :
108 : : /* Put the 64-bit file length in *bits* at the end of the buffer. */
109 : 7 : ctx->buffer[size - 2] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29));
110 : 7 : ctx->buffer[size - 1] = SWAP (ctx->total[0] << 3);
111 : :
112 : 7 : memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes);
113 : :
114 : : /* Process last bytes. */
115 : 7 : sha1_process_block (ctx->buffer, size * 4, ctx);
116 : :
117 : 7 : return sha1_read_ctx (ctx, resbuf);
118 : : }
119 : :
120 : : /* Compute SHA1 message digest for bytes read from STREAM. The
121 : : resulting message digest number will be written into the 16 bytes
122 : : beginning at RESBLOCK. */
123 : : int
124 : 0 : sha1_stream (FILE *stream, void *resblock)
125 : : {
126 : : struct sha1_ctx ctx;
127 : : size_t sum;
128 : :
129 : 0 : char *buffer = malloc (BLOCKSIZE + 72);
130 [ # # ]: 0 : if (!buffer)
131 : 0 : return 1;
132 : :
133 : : /* Initialize the computation context. */
134 : 0 : sha1_init_ctx (&ctx);
135 : :
136 : : /* Iterate over full file contents. */
137 : : while (1)
138 : : {
139 : : /* We read the file in blocks of BLOCKSIZE bytes. One call of the
140 : : computation function processes the whole buffer so that with the
141 : : next round of the loop another block can be read. */
142 : : size_t n;
143 : 0 : sum = 0;
144 : :
145 : : /* Read block. Take care for partial reads. */
146 : : while (1)
147 : : {
148 : 0 : n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
149 : :
150 : 0 : sum += n;
151 : :
152 [ # # ]: 0 : if (sum == BLOCKSIZE)
153 : 0 : break;
154 : :
155 [ # # ]: 0 : if (n == 0)
156 : : {
157 : : /* Check for the error flag IFF N == 0, so that we don't
158 : : exit the loop after a partial read due to e.g., EAGAIN
159 : : or EWOULDBLOCK. */
160 [ # # ]: 0 : if (ferror (stream))
161 : : {
162 : 0 : free (buffer);
163 : 0 : return 1;
164 : : }
165 : 0 : goto process_partial_block;
166 : : }
167 : :
168 : : /* We've read at least one byte, so ignore errors. But always
169 : : check for EOF, since feof may be true even though N > 0.
170 : : Otherwise, we could end up calling fread after EOF. */
171 [ # # ]: 0 : if (feof (stream))
172 : 0 : goto process_partial_block;
173 : 0 : }
174 : :
175 : : /* Process buffer with BLOCKSIZE bytes. Note that
176 : : BLOCKSIZE % 64 == 0
177 : : */
178 : 0 : sha1_process_block (buffer, BLOCKSIZE, &ctx);
179 : 0 : }
180 : :
181 : : process_partial_block:;
182 : :
183 : : /* Process any remaining bytes. */
184 [ # # ]: 0 : if (sum > 0)
185 : 0 : sha1_process_bytes (buffer, sum, &ctx);
186 : :
187 : : /* Construct result in desired memory. */
188 : 0 : sha1_finish_ctx (&ctx, resblock);
189 : 0 : free (buffer);
190 : 0 : return 0;
191 : : }
192 : :
193 : : /* Compute SHA1 message digest for LEN bytes beginning at BUFFER. The
194 : : result is always in little endian byte order, so that a byte-wise
195 : : output yields to the wanted ASCII representation of the message
196 : : digest. */
197 : : void *
198 : 1 : sha1_buffer (const char *buffer, size_t len, void *resblock)
199 : : {
200 : : struct sha1_ctx ctx;
201 : :
202 : : /* Initialize the computation context. */
203 : 1 : sha1_init_ctx (&ctx);
204 : :
205 : : /* Process whole buffer but last len % 64 bytes. */
206 : 1 : sha1_process_bytes (buffer, len, &ctx);
207 : :
208 : : /* Put result in desired memory area. */
209 : 1 : return sha1_finish_ctx (&ctx, resblock);
210 : : }
211 : :
212 : : void
213 : 7 : sha1_process_bytes (const void *buffer, size_t len, struct sha1_ctx *ctx)
214 : : {
215 : : /* When we already have some bits in our internal buffer concatenate
216 : : both inputs first. */
217 [ - + ]: 7 : if (ctx->buflen != 0)
218 : : {
219 : 0 : size_t left_over = ctx->buflen;
220 : 0 : size_t add = 128 - left_over > len ? len : 128 - left_over;
221 : :
222 : 0 : memcpy (&((char *) ctx->buffer)[left_over], buffer, add);
223 : 0 : ctx->buflen += add;
224 : :
225 [ # # ]: 0 : if (ctx->buflen > 64)
226 : : {
227 : 0 : sha1_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
228 : :
229 : 0 : ctx->buflen &= 63;
230 : : /* The regions in the following copy operation cannot overlap. */
231 : 0 : memcpy (ctx->buffer,
232 : 0 : &((char *) ctx->buffer)[(left_over + add) & ~63],
233 : 0 : ctx->buflen);
234 : : }
235 : :
236 : 0 : buffer = (const char *) buffer + add;
237 : 0 : len -= add;
238 : : }
239 : :
240 : : /* Process available complete blocks. */
241 [ - + ]: 7 : if (len >= 64)
242 : : {
243 : : #if !_STRING_ARCH_unaligned
244 : : # define UNALIGNED_P(p) ((uintptr_t) (p) % alignof (uint32_t) != 0)
245 [ # # ]: 0 : if (UNALIGNED_P (buffer))
246 [ # # ]: 0 : while (len > 64)
247 : : {
248 : 0 : sha1_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
249 : 0 : buffer = (const char *) buffer + 64;
250 : 0 : len -= 64;
251 : : }
252 : : else
253 : : #endif
254 : : {
255 : 0 : sha1_process_block (buffer, len & ~63, ctx);
256 : 0 : buffer = (const char *) buffer + (len & ~63);
257 : 0 : len &= 63;
258 : : }
259 : : }
260 : :
261 : : /* Move remaining bytes in internal buffer. */
262 [ + - ]: 7 : if (len > 0)
263 : : {
264 : 7 : size_t left_over = ctx->buflen;
265 : :
266 : 7 : memcpy (&((char *) ctx->buffer)[left_over], buffer, len);
267 : 7 : left_over += len;
268 [ - + ]: 7 : if (left_over >= 64)
269 : : {
270 : 0 : sha1_process_block (ctx->buffer, 64, ctx);
271 : 0 : left_over -= 64;
272 : 0 : memcpy (ctx->buffer, &ctx->buffer[16], left_over);
273 : : }
274 : 7 : ctx->buflen = left_over;
275 : : }
276 : 7 : }
277 : :
278 : : /* --- Code below is the primary difference between md5.c and sha1.c --- */
279 : :
280 : : /* SHA1 round constants */
281 : : #define K1 0x5a827999
282 : : #define K2 0x6ed9eba1
283 : : #define K3 0x8f1bbcdc
284 : : #define K4 0xca62c1d6
285 : :
286 : : /* Round functions. Note that F2 is the same as F4. */
287 : : #define F1(B,C,D) ( D ^ ( B & ( C ^ D ) ) )
288 : : #define F2(B,C,D) (B ^ C ^ D)
289 : : #define F3(B,C,D) ( ( B & C ) | ( D & ( B | C ) ) )
290 : : #define F4(B,C,D) (B ^ C ^ D)
291 : :
292 : : /* Process LEN bytes of BUFFER, accumulating context into CTX.
293 : : It is assumed that LEN % 64 == 0.
294 : : Most of this code comes from GnuPG's cipher/sha1.c. */
295 : :
296 : : void
297 : 13 : sha1_process_block (const void *buffer, size_t len, struct sha1_ctx *ctx)
298 : : {
299 : 13 : const uint32_t *words = buffer;
300 : 13 : size_t nwords = len / sizeof (uint32_t);
301 : 13 : const uint32_t *endp = words + nwords;
302 : : uint32_t x[16];
303 : 13 : uint32_t a = ctx->A;
304 : 13 : uint32_t b = ctx->B;
305 : 13 : uint32_t c = ctx->C;
306 : 13 : uint32_t d = ctx->D;
307 : 13 : uint32_t e = ctx->E;
308 : 13 : uint32_t lolen = len;
309 : :
310 : : /* First increment the byte count. RFC 1321 specifies the possible
311 : : length of the file up to 2^64 bits. Here we only compute the
312 : : number of bytes. Do a double word increment. */
313 : 13 : ctx->total[0] += lolen;
314 : 13 : ctx->total[1] += (len >> 31 >> 1) + (ctx->total[0] < lolen);
315 : :
316 : : #define rol(x, n) (((x) << (n)) | ((uint32_t) (x) >> (32 - (n))))
317 : :
318 : : #define M(I) ( tm = x[I&0x0f] ^ x[(I-14)&0x0f] \
319 : : ^ x[(I-8)&0x0f] ^ x[(I-3)&0x0f] \
320 : : , (x[I&0x0f] = rol(tm, 1)) )
321 : :
322 : : #define R(A,B,C,D,E,F,K,M) do { E += rol( A, 5 ) \
323 : : + F( B, C, D ) \
324 : : + K \
325 : : + M; \
326 : : B = rol( B, 30 ); \
327 : : } while(0)
328 : :
329 [ + + ]: 26 : while (words < endp)
330 : : {
331 : : uint32_t tm;
332 : : int t;
333 [ + + ]: 221 : for (t = 0; t < 16; t++)
334 : : {
335 : 208 : x[t] = SWAP (*words);
336 : 208 : words++;
337 : : }
338 : :
339 : 13 : R( a, b, c, d, e, F1, K1, x[ 0] );
340 : 13 : R( e, a, b, c, d, F1, K1, x[ 1] );
341 : 13 : R( d, e, a, b, c, F1, K1, x[ 2] );
342 : 13 : R( c, d, e, a, b, F1, K1, x[ 3] );
343 : 13 : R( b, c, d, e, a, F1, K1, x[ 4] );
344 : 13 : R( a, b, c, d, e, F1, K1, x[ 5] );
345 : 13 : R( e, a, b, c, d, F1, K1, x[ 6] );
346 : 13 : R( d, e, a, b, c, F1, K1, x[ 7] );
347 : 13 : R( c, d, e, a, b, F1, K1, x[ 8] );
348 : 13 : R( b, c, d, e, a, F1, K1, x[ 9] );
349 : 13 : R( a, b, c, d, e, F1, K1, x[10] );
350 : 13 : R( e, a, b, c, d, F1, K1, x[11] );
351 : 13 : R( d, e, a, b, c, F1, K1, x[12] );
352 : 13 : R( c, d, e, a, b, F1, K1, x[13] );
353 : 13 : R( b, c, d, e, a, F1, K1, x[14] );
354 : 13 : R( a, b, c, d, e, F1, K1, x[15] );
355 : 13 : R( e, a, b, c, d, F1, K1, M(16) );
356 : 13 : R( d, e, a, b, c, F1, K1, M(17) );
357 : 13 : R( c, d, e, a, b, F1, K1, M(18) );
358 : 13 : R( b, c, d, e, a, F1, K1, M(19) );
359 : 13 : R( a, b, c, d, e, F2, K2, M(20) );
360 : 13 : R( e, a, b, c, d, F2, K2, M(21) );
361 : 13 : R( d, e, a, b, c, F2, K2, M(22) );
362 : 13 : R( c, d, e, a, b, F2, K2, M(23) );
363 : 13 : R( b, c, d, e, a, F2, K2, M(24) );
364 : 13 : R( a, b, c, d, e, F2, K2, M(25) );
365 : 13 : R( e, a, b, c, d, F2, K2, M(26) );
366 : 13 : R( d, e, a, b, c, F2, K2, M(27) );
367 : 13 : R( c, d, e, a, b, F2, K2, M(28) );
368 : 13 : R( b, c, d, e, a, F2, K2, M(29) );
369 : 13 : R( a, b, c, d, e, F2, K2, M(30) );
370 : 13 : R( e, a, b, c, d, F2, K2, M(31) );
371 : 13 : R( d, e, a, b, c, F2, K2, M(32) );
372 : 13 : R( c, d, e, a, b, F2, K2, M(33) );
373 : 13 : R( b, c, d, e, a, F2, K2, M(34) );
374 : 13 : R( a, b, c, d, e, F2, K2, M(35) );
375 : 13 : R( e, a, b, c, d, F2, K2, M(36) );
376 : 13 : R( d, e, a, b, c, F2, K2, M(37) );
377 : 13 : R( c, d, e, a, b, F2, K2, M(38) );
378 : 13 : R( b, c, d, e, a, F2, K2, M(39) );
379 : 13 : R( a, b, c, d, e, F3, K3, M(40) );
380 : 13 : R( e, a, b, c, d, F3, K3, M(41) );
381 : 13 : R( d, e, a, b, c, F3, K3, M(42) );
382 : 13 : R( c, d, e, a, b, F3, K3, M(43) );
383 : 13 : R( b, c, d, e, a, F3, K3, M(44) );
384 : 13 : R( a, b, c, d, e, F3, K3, M(45) );
385 : 13 : R( e, a, b, c, d, F3, K3, M(46) );
386 : 13 : R( d, e, a, b, c, F3, K3, M(47) );
387 : 13 : R( c, d, e, a, b, F3, K3, M(48) );
388 : 13 : R( b, c, d, e, a, F3, K3, M(49) );
389 : 13 : R( a, b, c, d, e, F3, K3, M(50) );
390 : 13 : R( e, a, b, c, d, F3, K3, M(51) );
391 : 13 : R( d, e, a, b, c, F3, K3, M(52) );
392 : 13 : R( c, d, e, a, b, F3, K3, M(53) );
393 : 13 : R( b, c, d, e, a, F3, K3, M(54) );
394 : 13 : R( a, b, c, d, e, F3, K3, M(55) );
395 : 13 : R( e, a, b, c, d, F3, K3, M(56) );
396 : 13 : R( d, e, a, b, c, F3, K3, M(57) );
397 : 13 : R( c, d, e, a, b, F3, K3, M(58) );
398 : 13 : R( b, c, d, e, a, F3, K3, M(59) );
399 : 13 : R( a, b, c, d, e, F4, K4, M(60) );
400 : 13 : R( e, a, b, c, d, F4, K4, M(61) );
401 : 13 : R( d, e, a, b, c, F4, K4, M(62) );
402 : 13 : R( c, d, e, a, b, F4, K4, M(63) );
403 : 13 : R( b, c, d, e, a, F4, K4, M(64) );
404 : 13 : R( a, b, c, d, e, F4, K4, M(65) );
405 : 13 : R( e, a, b, c, d, F4, K4, M(66) );
406 : 13 : R( d, e, a, b, c, F4, K4, M(67) );
407 : 13 : R( c, d, e, a, b, F4, K4, M(68) );
408 : 13 : R( b, c, d, e, a, F4, K4, M(69) );
409 : 13 : R( a, b, c, d, e, F4, K4, M(70) );
410 : 13 : R( e, a, b, c, d, F4, K4, M(71) );
411 : 13 : R( d, e, a, b, c, F4, K4, M(72) );
412 : 13 : R( c, d, e, a, b, F4, K4, M(73) );
413 : 13 : R( b, c, d, e, a, F4, K4, M(74) );
414 : 13 : R( a, b, c, d, e, F4, K4, M(75) );
415 : 13 : R( e, a, b, c, d, F4, K4, M(76) );
416 : 13 : R( d, e, a, b, c, F4, K4, M(77) );
417 : 13 : R( c, d, e, a, b, F4, K4, M(78) );
418 : 13 : R( b, c, d, e, a, F4, K4, M(79) );
419 : :
420 : 13 : a = ctx->A += a;
421 : 13 : b = ctx->B += b;
422 : 13 : c = ctx->C += c;
423 : 13 : d = ctx->D += d;
424 : 13 : e = ctx->E += e;
425 : : }
426 : 13 : }
|