aboutsummaryrefslogtreecommitdiff
path: root/src/daemon/https/lgl/sha1.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/daemon/https/lgl/sha1.c')
-rw-r--r--src/daemon/https/lgl/sha1.c419
1 files changed, 0 insertions, 419 deletions
diff --git a/src/daemon/https/lgl/sha1.c b/src/daemon/https/lgl/sha1.c
deleted file mode 100644
index 573e7c69..00000000
--- a/src/daemon/https/lgl/sha1.c
+++ /dev/null
@@ -1,419 +0,0 @@
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, 2004, 2005, 2006 Free Software
5 Foundation, Inc.
6
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by the
9 Free Software Foundation; either version 2.1, or (at your option) any
10 later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20
21/* Written by Scott G. Miller
22 Credits:
23 Robert Klep <robert@ilse.nl> -- Expansion function fix
24*/
25
26#include "MHD_config.h"
27
28#include "sha1.h"
29
30#include <stddef.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 4096
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) */
51static 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 MHD_sha1_hash. */
57void
58MHD_sha1_init_ctx (struct MHD_sha1_ctx *ctx)
59{
60 ctx->A = 0x67452301;
61 ctx->B = 0xefcdab89;
62 ctx->C = 0x98badcfe;
63 ctx->D = 0x10325476;
64 ctx->E = 0xc3d2e1f0;
65
66 ctx->total[0] = ctx->total[1] = 0;
67 ctx->buflen = 0;
68}
69
70/* Put result from CTX in first 20 bytes following RESBUF. The result
71 must be in little endian byte order.
72
73 IMPORTANT: On some systems it is required that RESBUF is correctly
74 aligned for a 32-bit value. */
75void *
76MHD_sha1_read_ctx (const struct MHD_sha1_ctx *ctx, void *resbuf)
77{
78 ((uint32_t *) resbuf)[0] = SWAP (ctx->A);
79 ((uint32_t *) resbuf)[1] = SWAP (ctx->B);
80 ((uint32_t *) resbuf)[2] = SWAP (ctx->C);
81 ((uint32_t *) resbuf)[3] = SWAP (ctx->D);
82 ((uint32_t *) resbuf)[4] = SWAP (ctx->E);
83
84 return resbuf;
85}
86
87/* Process the remaining bytes in the internal buffer and the usual
88 prolog according to the standard and write the result to RESBUF.
89
90 IMPORTANT: On some systems it is required that RESBUF is correctly
91 aligned for a 32-bit value. */
92void *
93MHD_sha1_finish_ctx (struct MHD_sha1_ctx *ctx, void *resbuf)
94{
95 /* Take yet unprocessed bytes into account. */
96 uint32_t bytes = ctx->buflen;
97 size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4;
98
99 /* Now count remaining bytes. */
100 ctx->total[0] += bytes;
101 if (ctx->total[0] < bytes)
102 ++ctx->total[1];
103
104 /* Put the 64-bit file length in *bits* at the end of the buffer. */
105 ctx->buffer[size - 2] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29));
106 ctx->buffer[size - 1] = SWAP (ctx->total[0] << 3);
107
108 memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes);
109
110 /* Process last bytes. */
111 MHD_sha1_process_block (ctx->buffer, size * 4, ctx);
112
113 return MHD_sha1_read_ctx (ctx, resbuf);
114}
115
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. */
119int
120MHD_sha1_stream (FILE * stream, void *resblock)
121{
122 struct MHD_sha1_ctx ctx;
123 char buffer[BLOCKSIZE + 72];
124 size_t sum;
125
126 /* Initialize the computation context. */
127 MHD_sha1_init_ctx (&ctx);
128
129 /* Iterate over full file contents. */
130 while (1)
131 {
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. */
135 size_t n;
136 sum = 0;
137
138 /* Read block. Take care for partial reads. */
139 while (1)
140 {
141 n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
142
143 sum += n;
144
145 if (sum == BLOCKSIZE)
146 break;
147
148 if (n == 0)
149 {
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
152 or EWOULDBLOCK. */
153 if (ferror (stream))
154 return 1;
155 goto process_partial_block;
156 }
157
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. */
161 if (feof (stream))
162 goto process_partial_block;
163 }
164
165 /* Process buffer with BLOCKSIZE bytes. Note that
166 BLOCKSIZE % 64 == 0
167 */
168 MHD_sha1_process_block (buffer, BLOCKSIZE, &ctx);
169 }
170
171process_partial_block:;
172
173 /* Process any remaining bytes. */
174 if (sum > 0)
175 MHD_sha1_process_bytes (buffer, sum, &ctx);
176
177 /* Construct result in desired memory. */
178 MHD_sha1_finish_ctx (&ctx, resblock);
179 return 0;
180}
181
182/* Compute SHA1 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
185 digest. */
186void *
187MHD_sha1_buffer (const char *buffer, size_t len, void *resblock)
188{
189 struct MHD_sha1_ctx ctx;
190
191 /* Initialize the computation context. */
192 MHD_sha1_init_ctx (&ctx);
193
194 /* Process whole buffer but last len % 64 bytes. */
195 MHD_sha1_process_bytes (buffer, len, &ctx);
196
197 /* Put result in desired memory area. */
198 return MHD_sha1_finish_ctx (&ctx, resblock);
199}
200
201void
202MHD_sha1_process_bytes (const void *buffer, size_t len,
203 struct MHD_sha1_ctx *ctx)
204{
205 /* When we already have some bits in our internal buffer concatenate
206 both inputs first. */
207 if (ctx->buflen != 0)
208 {
209 size_t left_over = ctx->buflen;
210 size_t add = 128 - left_over > len ? len : 128 - left_over;
211
212 memcpy (&((char *) ctx->buffer)[left_over], buffer, add);
213 ctx->buflen += add;
214
215 if (ctx->buflen > 64)
216 {
217 MHD_sha1_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
218
219 ctx->buflen &= 63;
220 /* The regions in the following copy operation cannot overlap. */
221 memcpy (ctx->buffer,
222 &((char *) ctx->buffer)[(left_over + add) & ~63],
223 ctx->buflen);
224 }
225
226 buffer = (const char *) buffer + add;
227 len -= add;
228 }
229
230 /* Process available complete blocks. */
231 if (len >= 64)
232 {
233#if !_STRING_ARCH_unaligned
234# define alignof(type) offsetof (struct { char c; type x; }, x)
235# define UNALIGNED_P(p) (((size_t) p) % alignof (uint32_t) != 0)
236 if (UNALIGNED_P (buffer))
237 while (len > 64)
238 {
239 MHD_sha1_process_block (memcpy (ctx->buffer, buffer, 64), 64,
240 ctx);
241 buffer = (const char *) buffer + 64;
242 len -= 64;
243 }
244 else
245#endif
246 {
247 MHD_sha1_process_block (buffer, len & ~63, ctx);
248 buffer = (const char *) buffer + (len & ~63);
249 len &= 63;
250 }
251 }
252
253 /* Move remaining bytes in internal buffer. */
254 if (len > 0)
255 {
256 size_t left_over = ctx->buflen;
257
258 memcpy (&((char *) ctx->buffer)[left_over], buffer, len);
259 left_over += len;
260 if (left_over >= 64)
261 {
262 MHD_sha1_process_block (ctx->buffer, 64, ctx);
263 left_over -= 64;
264 memcpy (ctx->buffer, &ctx->buffer[16], left_over);
265 }
266 ctx->buflen = left_over;
267 }
268}
269
270/* --- Code below is the primary difference between md5.c and sha1.c --- */
271
272/* SHA1 round constants */
273#define K1 0x5a827999
274#define K2 0x6ed9eba1
275#define K3 0x8f1bbcdc
276#define K4 0xca62c1d6
277
278/* Round functions. Note that F2 is the same as F4. */
279#define F1(B,C,D) ( D ^ ( B & ( C ^ D ) ) )
280#define F2(B,C,D) (B ^ C ^ D)
281#define F3(B,C,D) ( ( B & C ) | ( D & ( B | C ) ) )
282#define F4(B,C,D) (B ^ C ^ D)
283
284/* Process LEN bytes of BUFFER, accumulating context into CTX.
285 It is assumed that LEN % 64 == 0.
286 Most of this code comes from GnuPG's cipher/sha1.c. */
287
288void
289MHD_sha1_process_block (const void *buffer, size_t len,
290 struct MHD_sha1_ctx *ctx)
291{
292 const uint32_t *words = buffer;
293 size_t nwords = len / sizeof (uint32_t);
294 const uint32_t *endp = words + nwords;
295 uint32_t x[16];
296 uint32_t a = ctx->A;
297 uint32_t b = ctx->B;
298 uint32_t c = ctx->C;
299 uint32_t d = ctx->D;
300 uint32_t e = ctx->E;
301
302 /* First increment the byte count. RFC 1321 specifies the possible
303 length of the file up to 2^64 bits. Here we only compute the
304 number of bytes. Do a double word increment. */
305 ctx->total[0] += len;
306 if (ctx->total[0] < len)
307 ++ctx->total[1];
308
309#define rol(x, n) (((x) << (n)) | ((uint32_t) (x) >> (32 - (n))))
310
311#define M(I) ( tm = x[I&0x0f] ^ x[(I-14)&0x0f] \
312 ^ x[(I-8)&0x0f] ^ x[(I-3)&0x0f] \
313 , (x[I&0x0f] = rol(tm, 1)) )
314
315#define R(A,B,C,D,E,F,K,M) do { E += rol( A, 5 ) \
316 + F( B, C, D ) \
317 + K \
318 + M; \
319 B = rol( B, 30 ); \
320 } while(0)
321
322 while (words < endp)
323 {
324 uint32_t tm;
325 int t;
326 for (t = 0; t < 16; t++)
327 {
328 x[t] = SWAP (*words);
329 words++;
330 }
331
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));
412
413 a = ctx->A += a;
414 b = ctx->B += b;
415 c = ctx->C += c;
416 d = ctx->D += d;
417 e = ctx->E += e;
418 }
419}