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