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