[packages] btpd: update to 0.16
[openwrt/svn-archive/archive.git] / net / btpd / patches / 000-sha1.diff
1 --- a/btpd/btpd.c
2 +++ b/btpd/btpd.c
3 @@ -1,6 +1,6 @@
4 #include "btpd.h"
5
6 -#include <openssl/sha.h>
7 +#include "sha1.h"
8 #include <signal.h>
9
10 static uint8_t m_peer_id[20];
11 @@ -114,7 +114,7 @@ btpd_init(void)
12 idcon[sizeof(idcon) - 1] = '\0';
13 n = strlen(idcon);
14
15 - SHA1(idcon, n, m_peer_id);
16 + sha1_buffer(idcon, n, m_peer_id);
17 bcopy(m_peer_id, &seed, sizeof(seed));
18 bcopy(BTPD_VERSION, m_peer_id, sizeof(BTPD_VERSION) - 1);
19 m_peer_id[sizeof(BTPD_VERSION) - 1] = '|';
20 --- a/btpd/content.c
21 +++ b/btpd/content.c
22 @@ -1,6 +1,6 @@
23 #include "btpd.h"
24
25 -#include <openssl/sha.h>
26 +#include "sha1.h"
27 #include <stream.h>
28
29 struct content {
30 @@ -60,16 +60,16 @@ static struct timeout m_workev;
31 static int
32 test_hash(struct torrent *tp, uint8_t *hash, uint32_t piece)
33 {
34 - char piece_hash[SHA_DIGEST_LENGTH];
35 + char piece_hash[SHA1_DIGEST_SIZE];
36 tlib_read_hash(tp->tl, tp->pieces_off, piece, piece_hash);
37 - return bcmp(hash, piece_hash, SHA_DIGEST_LENGTH);
38 + return bcmp(hash, piece_hash, SHA1_DIGEST_SIZE);
39 }
40
41 static int
42 test_piece(struct torrent *tp, uint32_t piece, int *ok)
43 {
44 int err;
45 - uint8_t hash[SHA_DIGEST_LENGTH];
46 + uint8_t hash[SHA1_DIGEST_SIZE];
47 if ((err = bts_sha(tp->cm->rds, piece * tp->piece_length,
48 torrent_piece_size(tp, piece), hash)) != 0) {
49 btpd_log(BTPD_L_ERROR, "io error on '%s' (%s).\n",
50 --- a/btpd/download_subr.c
51 +++ b/btpd/download_subr.c
52 @@ -21,7 +21,7 @@
53
54 #include "btpd.h"
55
56 -#include <openssl/sha.h>
57 +#include "sha1.h"
58 #include <stream.h>
59
60 static void
61 @@ -42,7 +42,7 @@ piece_log_hashes(struct piece *pc)
62 for (unsigned i = 0; i < pc->nblocks; i++) {
63 uint32_t bsize = torrent_block_size(tp, pc->index, pc->nblocks, i);
64 cm_get_bytes(tp, pc->index, i * PIECE_BLOCKLEN, bsize, &buf);
65 - SHA1(buf, bsize, &log->hashes[i * 20]);
66 + sha1_buffer(buf, bsize, &log->hashes[i * 20]);
67 free(buf);
68 }
69 }
70 --- a/btpd/torrent.c
71 +++ b/btpd/torrent.c
72 @@ -1,7 +1,5 @@
73 #include "btpd.h"
74
75 -#include <openssl/sha.h>
76 -
77 #define SAVE_INTERVAL 300
78
79 static unsigned m_nghosts;
80 --- a/configure.ac
81 +++ b/configure.ac
82 @@ -29,13 +29,6 @@ rm -f conftest.c conftest.$OBJEXT
83 rm -f conftest.c conftest.$OBJEXT
84 [$3]))
85
86 -AC_ARG_WITH(openssl,
87 -[ --with-openssl=dir use openssl installed in dir],
88 -[
89 - AC_SUBST(openssl_LDFLAGS,["-L${withval}/lib -Wl,-R,${withval}/lib"])
90 - AC_SUBST(openssl_CPPFLAGS,"-I${withval}/include")
91 -])
92 -
93 AC_ARG_WITH(evloop-method,
94 [ --with-evloop-method select evloop method (epoll,poll,kqueue)],
95 evloop_methods=$withval,
96 @@ -76,11 +69,6 @@ CC_ARGS_OK_IFELSE(-Wno-pointer-sign,
97 ,
98 AC_MSG_RESULT(no))
99
100 -old_LDFLAGS="$LDFLAGS"
101 -LDFLAGS="$LDFLAGS $openssl_LDFLAGS"
102 -AC_CHECK_LIB(crypto, SHA1_Final,:,AC_MSG_FAILURE(btpd needs openssl's libraries and headers))
103 -LDFLAGS=$old_LDFLAGS
104 -
105 for m in $evloop_methods; do
106 case $m in
107 epoll)
108 --- a/misc/metainfo.c
109 +++ b/misc/metainfo.c
110 @@ -6,8 +6,7 @@
111 #include <string.h>
112 #include <strings.h>
113
114 -#include <openssl/sha.h>
115 -
116 +#include "sha1.h"
117 #include "benc.h"
118 #include "metainfo.h"
119 #include "subr.h"
120 @@ -159,7 +158,7 @@ mi_info_hash(const char *p, uint8_t *has
121 if (hash == NULL)
122 if ((hash = malloc(20)) == NULL)
123 return NULL;
124 - return SHA1(info, benc_length(info), hash);
125 + return (uint8_t *)sha1_buffer(info, benc_length(info), hash);
126 }
127
128 char *
129 --- /dev/null
130 +++ b/misc/sha1.c
131 @@ -0,0 +1,428 @@
132 +/* sha1.c - Functions to compute SHA1 message digest of files or
133 + memory blocks according to the NIST specification FIPS-180-1.
134 +
135 + Copyright (C) 2000, 2001, 2003, 2004, 2005, 2006, 2008, 2009, 2010 Free
136 + Software Foundation, Inc.
137 +
138 + This program is free software; you can redistribute it and/or modify it
139 + under the terms of the GNU General Public License as published by the
140 + Free Software Foundation; either version 2, or (at your option) any
141 + later version.
142 +
143 + This program is distributed in the hope that it will be useful,
144 + but WITHOUT ANY WARRANTY; without even the implied warranty of
145 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
146 + GNU General Public License for more details.
147 +
148 + You should have received a copy of the GNU General Public License
149 + along with this program; if not, write to the Free Software Foundation,
150 + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
151 +
152 +/* Written by Scott G. Miller
153 + Credits:
154 + Robert Klep <robert@ilse.nl> -- Expansion function fix
155 +*/
156 +
157 +#include <config.h>
158 +
159 +#include "sha1.h"
160 +
161 +#include <stddef.h>
162 +#include <stdlib.h>
163 +#include <string.h>
164 +
165 +#if USE_UNLOCKED_IO
166 +# include "unlocked-io.h"
167 +#endif
168 +
169 +#ifdef WORDS_BIGENDIAN
170 +# define SWAP(n) (n)
171 +#else
172 +# define SWAP(n) \
173 + (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
174 +#endif
175 +
176 +#define BLOCKSIZE 32768
177 +#if BLOCKSIZE % 64 != 0
178 +# error "invalid BLOCKSIZE"
179 +#endif
180 +
181 +/* This array contains the bytes used to pad the buffer to the next
182 + 64-byte boundary. (RFC 1321, 3.1: Step 1) */
183 +static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
184 +
185 +
186 +/* Take a pointer to a 160 bit block of data (five 32 bit ints) and
187 + initialize it to the start constants of the SHA1 algorithm. This
188 + must be called before using hash in the call to sha1_hash. */
189 +void
190 +sha1_init_ctx (struct sha1_ctx *ctx)
191 +{
192 + ctx->A = 0x67452301;
193 + ctx->B = 0xefcdab89;
194 + ctx->C = 0x98badcfe;
195 + ctx->D = 0x10325476;
196 + ctx->E = 0xc3d2e1f0;
197 +
198 + ctx->total[0] = ctx->total[1] = 0;
199 + ctx->buflen = 0;
200 +}
201 +
202 +/* Copy the 4 byte value from v into the memory location pointed to by *cp,
203 + If your architecture allows unaligned access this is equivalent to
204 + * (uint32_t *) cp = v */
205 +static inline void
206 +set_uint32 (char *cp, uint32_t v)
207 +{
208 + memcpy (cp, &v, sizeof v);
209 +}
210 +
211 +/* Put result from CTX in first 20 bytes following RESBUF. The result
212 + must be in little endian byte order. */
213 +void *
214 +sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf)
215 +{
216 + char *r = resbuf;
217 + set_uint32 (r + 0 * sizeof ctx->A, SWAP (ctx->A));
218 + set_uint32 (r + 1 * sizeof ctx->B, SWAP (ctx->B));
219 + set_uint32 (r + 2 * sizeof ctx->C, SWAP (ctx->C));
220 + set_uint32 (r + 3 * sizeof ctx->D, SWAP (ctx->D));
221 + set_uint32 (r + 4 * sizeof ctx->E, SWAP (ctx->E));
222 +
223 + return resbuf;
224 +}
225 +
226 +/* Process the remaining bytes in the internal buffer and the usual
227 + prolog according to the standard and write the result to RESBUF. */
228 +void *
229 +sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf)
230 +{
231 + /* Take yet unprocessed bytes into account. */
232 + uint32_t bytes = ctx->buflen;
233 + size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4;
234 +
235 + /* Now count remaining bytes. */
236 + ctx->total[0] += bytes;
237 + if (ctx->total[0] < bytes)
238 + ++ctx->total[1];
239 +
240 + /* Put the 64-bit file length in *bits* at the end of the buffer. */
241 + ctx->buffer[size - 2] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29));
242 + ctx->buffer[size - 1] = SWAP (ctx->total[0] << 3);
243 +
244 + memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes);
245 +
246 + /* Process last bytes. */
247 + sha1_process_block (ctx->buffer, size * 4, ctx);
248 +
249 + return sha1_read_ctx (ctx, resbuf);
250 +}
251 +
252 +/* Compute SHA1 message digest for bytes read from STREAM. The
253 + resulting message digest number will be written into the 16 bytes
254 + beginning at RESBLOCK. */
255 +int
256 +sha1_stream (FILE *stream, void *resblock)
257 +{
258 + struct sha1_ctx ctx;
259 + size_t sum;
260 +
261 + char *buffer = malloc (BLOCKSIZE + 72);
262 + if (!buffer)
263 + return 1;
264 +
265 + /* Initialize the computation context. */
266 + sha1_init_ctx (&ctx);
267 +
268 + /* Iterate over full file contents. */
269 + while (1)
270 + {
271 + /* We read the file in blocks of BLOCKSIZE bytes. One call of the
272 + computation function processes the whole buffer so that with the
273 + next round of the loop another block can be read. */
274 + size_t n;
275 + sum = 0;
276 +
277 + /* Read block. Take care for partial reads. */
278 + while (1)
279 + {
280 + n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
281 +
282 + sum += n;
283 +
284 + if (sum == BLOCKSIZE)
285 + break;
286 +
287 + if (n == 0)
288 + {
289 + /* Check for the error flag IFF N == 0, so that we don't
290 + exit the loop after a partial read due to e.g., EAGAIN
291 + or EWOULDBLOCK. */
292 + if (ferror (stream))
293 + {
294 + free (buffer);
295 + return 1;
296 + }
297 + goto process_partial_block;
298 + }
299 +
300 + /* We've read at least one byte, so ignore errors. But always
301 + check for EOF, since feof may be true even though N > 0.
302 + Otherwise, we could end up calling fread after EOF. */
303 + if (feof (stream))
304 + goto process_partial_block;
305 + }
306 +
307 + /* Process buffer with BLOCKSIZE bytes. Note that
308 + BLOCKSIZE % 64 == 0
309 + */
310 + sha1_process_block (buffer, BLOCKSIZE, &ctx);
311 + }
312 +
313 + process_partial_block:;
314 +
315 + /* Process any remaining bytes. */
316 + if (sum > 0)
317 + sha1_process_bytes (buffer, sum, &ctx);
318 +
319 + /* Construct result in desired memory. */
320 + sha1_finish_ctx (&ctx, resblock);
321 + free (buffer);
322 + return 0;
323 +}
324 +
325 +/* Compute SHA1 message digest for LEN bytes beginning at BUFFER. The
326 + result is always in little endian byte order, so that a byte-wise
327 + output yields to the wanted ASCII representation of the message
328 + digest. */
329 +void *
330 +sha1_buffer (const char *buffer, size_t len, void *resblock)
331 +{
332 + struct sha1_ctx ctx;
333 +
334 + /* Initialize the computation context. */
335 + sha1_init_ctx (&ctx);
336 +
337 + /* Process whole buffer but last len % 64 bytes. */
338 + sha1_process_bytes (buffer, len, &ctx);
339 +
340 + /* Put result in desired memory area. */
341 + return sha1_finish_ctx (&ctx, resblock);
342 +}
343 +
344 +void
345 +sha1_process_bytes (const void *buffer, size_t len, struct sha1_ctx *ctx)
346 +{
347 + /* When we already have some bits in our internal buffer concatenate
348 + both inputs first. */
349 + if (ctx->buflen != 0)
350 + {
351 + size_t left_over = ctx->buflen;
352 + size_t add = 128 - left_over > len ? len : 128 - left_over;
353 +
354 + memcpy (&((char *) ctx->buffer)[left_over], buffer, add);
355 + ctx->buflen += add;
356 +
357 + if (ctx->buflen > 64)
358 + {
359 + sha1_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
360 +
361 + ctx->buflen &= 63;
362 + /* The regions in the following copy operation cannot overlap. */
363 + memcpy (ctx->buffer,
364 + &((char *) ctx->buffer)[(left_over + add) & ~63],
365 + ctx->buflen);
366 + }
367 +
368 + buffer = (const char *) buffer + add;
369 + len -= add;
370 + }
371 +
372 + /* Process available complete blocks. */
373 + if (len >= 64)
374 + {
375 +#if !_STRING_ARCH_unaligned
376 +# define alignof(type) offsetof (struct { char c; type x; }, x)
377 +# define UNALIGNED_P(p) (((size_t) p) % alignof (uint32_t) != 0)
378 + if (UNALIGNED_P (buffer))
379 + while (len > 64)
380 + {
381 + sha1_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
382 + buffer = (const char *) buffer + 64;
383 + len -= 64;
384 + }
385 + else
386 +#endif
387 + {
388 + sha1_process_block (buffer, len & ~63, ctx);
389 + buffer = (const char *) buffer + (len & ~63);
390 + len &= 63;
391 + }
392 + }
393 +
394 + /* Move remaining bytes in internal buffer. */
395 + if (len > 0)
396 + {
397 + size_t left_over = ctx->buflen;
398 +
399 + memcpy (&((char *) ctx->buffer)[left_over], buffer, len);
400 + left_over += len;
401 + if (left_over >= 64)
402 + {
403 + sha1_process_block (ctx->buffer, 64, ctx);
404 + left_over -= 64;
405 + memcpy (ctx->buffer, &ctx->buffer[16], left_over);
406 + }
407 + ctx->buflen = left_over;
408 + }
409 +}
410 +
411 +/* --- Code below is the primary difference between md5.c and sha1.c --- */
412 +
413 +/* SHA1 round constants */
414 +#define K1 0x5a827999
415 +#define K2 0x6ed9eba1
416 +#define K3 0x8f1bbcdc
417 +#define K4 0xca62c1d6
418 +
419 +/* Round functions. Note that F2 is the same as F4. */
420 +#define F1(B,C,D) ( D ^ ( B & ( C ^ D ) ) )
421 +#define F2(B,C,D) (B ^ C ^ D)
422 +#define F3(B,C,D) ( ( B & C ) | ( D & ( B | C ) ) )
423 +#define F4(B,C,D) (B ^ C ^ D)
424 +
425 +/* Process LEN bytes of BUFFER, accumulating context into CTX.
426 + It is assumed that LEN % 64 == 0.
427 + Most of this code comes from GnuPG's cipher/sha1.c. */
428 +
429 +void
430 +sha1_process_block (const void *buffer, size_t len, struct sha1_ctx *ctx)
431 +{
432 + const uint32_t *words = buffer;
433 + size_t nwords = len / sizeof (uint32_t);
434 + const uint32_t *endp = words + nwords;
435 + uint32_t x[16];
436 + uint32_t a = ctx->A;
437 + uint32_t b = ctx->B;
438 + uint32_t c = ctx->C;
439 + uint32_t d = ctx->D;
440 + uint32_t e = ctx->E;
441 +
442 + /* First increment the byte count. RFC 1321 specifies the possible
443 + length of the file up to 2^64 bits. Here we only compute the
444 + number of bytes. Do a double word increment. */
445 + ctx->total[0] += len;
446 + if (ctx->total[0] < len)
447 + ++ctx->total[1];
448 +
449 +#define rol(x, n) (((x) << (n)) | ((uint32_t) (x) >> (32 - (n))))
450 +
451 +#define M(I) ( tm = x[I&0x0f] ^ x[(I-14)&0x0f] \
452 + ^ x[(I-8)&0x0f] ^ x[(I-3)&0x0f] \
453 + , (x[I&0x0f] = rol(tm, 1)) )
454 +
455 +#define R(A,B,C,D,E,F,K,M) do { E += rol( A, 5 ) \
456 + + F( B, C, D ) \
457 + + K \
458 + + M; \
459 + B = rol( B, 30 ); \
460 + } while(0)
461 +
462 + while (words < endp)
463 + {
464 + uint32_t tm;
465 + int t;
466 + for (t = 0; t < 16; t++)
467 + {
468 + x[t] = SWAP (*words);
469 + words++;
470 + }
471 +
472 + R( a, b, c, d, e, F1, K1, x[ 0] );
473 + R( e, a, b, c, d, F1, K1, x[ 1] );
474 + R( d, e, a, b, c, F1, K1, x[ 2] );
475 + R( c, d, e, a, b, F1, K1, x[ 3] );
476 + R( b, c, d, e, a, F1, K1, x[ 4] );
477 + R( a, b, c, d, e, F1, K1, x[ 5] );
478 + R( e, a, b, c, d, F1, K1, x[ 6] );
479 + R( d, e, a, b, c, F1, K1, x[ 7] );
480 + R( c, d, e, a, b, F1, K1, x[ 8] );
481 + R( b, c, d, e, a, F1, K1, x[ 9] );
482 + R( a, b, c, d, e, F1, K1, x[10] );
483 + R( e, a, b, c, d, F1, K1, x[11] );
484 + R( d, e, a, b, c, F1, K1, x[12] );
485 + R( c, d, e, a, b, F1, K1, x[13] );
486 + R( b, c, d, e, a, F1, K1, x[14] );
487 + R( a, b, c, d, e, F1, K1, x[15] );
488 + R( e, a, b, c, d, F1, K1, M(16) );
489 + R( d, e, a, b, c, F1, K1, M(17) );
490 + R( c, d, e, a, b, F1, K1, M(18) );
491 + R( b, c, d, e, a, F1, K1, M(19) );
492 + R( a, b, c, d, e, F2, K2, M(20) );
493 + R( e, a, b, c, d, F2, K2, M(21) );
494 + R( d, e, a, b, c, F2, K2, M(22) );
495 + R( c, d, e, a, b, F2, K2, M(23) );
496 + R( b, c, d, e, a, F2, K2, M(24) );
497 + R( a, b, c, d, e, F2, K2, M(25) );
498 + R( e, a, b, c, d, F2, K2, M(26) );
499 + R( d, e, a, b, c, F2, K2, M(27) );
500 + R( c, d, e, a, b, F2, K2, M(28) );
501 + R( b, c, d, e, a, F2, K2, M(29) );
502 + R( a, b, c, d, e, F2, K2, M(30) );
503 + R( e, a, b, c, d, F2, K2, M(31) );
504 + R( d, e, a, b, c, F2, K2, M(32) );
505 + R( c, d, e, a, b, F2, K2, M(33) );
506 + R( b, c, d, e, a, F2, K2, M(34) );
507 + R( a, b, c, d, e, F2, K2, M(35) );
508 + R( e, a, b, c, d, F2, K2, M(36) );
509 + R( d, e, a, b, c, F2, K2, M(37) );
510 + R( c, d, e, a, b, F2, K2, M(38) );
511 + R( b, c, d, e, a, F2, K2, M(39) );
512 + R( a, b, c, d, e, F3, K3, M(40) );
513 + R( e, a, b, c, d, F3, K3, M(41) );
514 + R( d, e, a, b, c, F3, K3, M(42) );
515 + R( c, d, e, a, b, F3, K3, M(43) );
516 + R( b, c, d, e, a, F3, K3, M(44) );
517 + R( a, b, c, d, e, F3, K3, M(45) );
518 + R( e, a, b, c, d, F3, K3, M(46) );
519 + R( d, e, a, b, c, F3, K3, M(47) );
520 + R( c, d, e, a, b, F3, K3, M(48) );
521 + R( b, c, d, e, a, F3, K3, M(49) );
522 + R( a, b, c, d, e, F3, K3, M(50) );
523 + R( e, a, b, c, d, F3, K3, M(51) );
524 + R( d, e, a, b, c, F3, K3, M(52) );
525 + R( c, d, e, a, b, F3, K3, M(53) );
526 + R( b, c, d, e, a, F3, K3, M(54) );
527 + R( a, b, c, d, e, F3, K3, M(55) );
528 + R( e, a, b, c, d, F3, K3, M(56) );
529 + R( d, e, a, b, c, F3, K3, M(57) );
530 + R( c, d, e, a, b, F3, K3, M(58) );
531 + R( b, c, d, e, a, F3, K3, M(59) );
532 + R( a, b, c, d, e, F4, K4, M(60) );
533 + R( e, a, b, c, d, F4, K4, M(61) );
534 + R( d, e, a, b, c, F4, K4, M(62) );
535 + R( c, d, e, a, b, F4, K4, M(63) );
536 + R( b, c, d, e, a, F4, K4, M(64) );
537 + R( a, b, c, d, e, F4, K4, M(65) );
538 + R( e, a, b, c, d, F4, K4, M(66) );
539 + R( d, e, a, b, c, F4, K4, M(67) );
540 + R( c, d, e, a, b, F4, K4, M(68) );
541 + R( b, c, d, e, a, F4, K4, M(69) );
542 + R( a, b, c, d, e, F4, K4, M(70) );
543 + R( e, a, b, c, d, F4, K4, M(71) );
544 + R( d, e, a, b, c, F4, K4, M(72) );
545 + R( c, d, e, a, b, F4, K4, M(73) );
546 + R( b, c, d, e, a, F4, K4, M(74) );
547 + R( a, b, c, d, e, F4, K4, M(75) );
548 + R( e, a, b, c, d, F4, K4, M(76) );
549 + R( d, e, a, b, c, F4, K4, M(77) );
550 + R( c, d, e, a, b, F4, K4, M(78) );
551 + R( b, c, d, e, a, F4, K4, M(79) );
552 +
553 + a = ctx->A += a;
554 + b = ctx->B += b;
555 + c = ctx->C += c;
556 + d = ctx->D += d;
557 + e = ctx->E += e;
558 + }
559 +}
560 --- /dev/null
561 +++ b/misc/sha1.h
562 @@ -0,0 +1,92 @@
563 +/* Declarations of functions and data types used for SHA1 sum
564 + library functions.
565 + Copyright (C) 2000, 2001, 2003, 2005, 2006, 2008, 2009, 2010 Free Software
566 + Foundation, Inc.
567 +
568 + This program is free software; you can redistribute it and/or modify it
569 + under the terms of the GNU General Public License as published by the
570 + Free Software Foundation; either version 2, or (at your option) any
571 + later version.
572 +
573 + This program is distributed in the hope that it will be useful,
574 + but WITHOUT ANY WARRANTY; without even the implied warranty of
575 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
576 + GNU General Public License for more details.
577 +
578 + You should have received a copy of the GNU General Public License
579 + along with this program; if not, write to the Free Software Foundation,
580 + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
581 +
582 +#ifndef SHA1_H
583 +# define SHA1_H 1
584 +
585 +# include <stdio.h>
586 +# include <stdint.h>
587 +
588 +# ifdef __cplusplus
589 +extern "C" {
590 +# endif
591 +
592 +#define SHA1_DIGEST_SIZE 20
593 +
594 +/* Structure to save state of computation between the single steps. */
595 +struct sha1_ctx
596 +{
597 + uint32_t A;
598 + uint32_t B;
599 + uint32_t C;
600 + uint32_t D;
601 + uint32_t E;
602 +
603 + uint32_t total[2];
604 + uint32_t buflen;
605 + uint32_t buffer[32];
606 +};
607 +
608 +
609 +/* Initialize structure containing state of computation. */
610 +extern void sha1_init_ctx (struct sha1_ctx *ctx);
611 +
612 +/* Starting with the result of former calls of this function (or the
613 + initialization function update the context for the next LEN bytes
614 + starting at BUFFER.
615 + It is necessary that LEN is a multiple of 64!!! */
616 +extern void sha1_process_block (const void *buffer, size_t len,
617 + struct sha1_ctx *ctx);
618 +
619 +/* Starting with the result of former calls of this function (or the
620 + initialization function update the context for the next LEN bytes
621 + starting at BUFFER.
622 + It is NOT required that LEN is a multiple of 64. */
623 +extern void sha1_process_bytes (const void *buffer, size_t len,
624 + struct sha1_ctx *ctx);
625 +
626 +/* Process the remaining bytes in the buffer and put result from CTX
627 + in first 20 bytes following RESBUF. The result is always in little
628 + endian byte order, so that a byte-wise output yields to the wanted
629 + ASCII representation of the message digest. */
630 +extern void *sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf);
631 +
632 +
633 +/* Put result from CTX in first 20 bytes following RESBUF. The result is
634 + always in little endian byte order, so that a byte-wise output yields
635 + to the wanted ASCII representation of the message digest. */
636 +extern void *sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf);
637 +
638 +
639 +/* Compute SHA1 message digest for bytes read from STREAM. The
640 + resulting message digest number will be written into the 20 bytes
641 + beginning at RESBLOCK. */
642 +extern int sha1_stream (FILE *stream, void *resblock);
643 +
644 +/* Compute SHA1 message digest for LEN bytes beginning at BUFFER. The
645 + result is always in little endian byte order, so that a byte-wise
646 + output yields to the wanted ASCII representation of the message
647 + digest. */
648 +extern void *sha1_buffer (const char *buffer, size_t len, void *resblock);
649 +
650 +# ifdef __cplusplus
651 +}
652 +# endif
653 +
654 +#endif
655 --- a/misc/stream.c
656 +++ b/misc/stream.c
657 @@ -5,7 +5,7 @@
658 #include <stdlib.h>
659 #include <unistd.h>
660
661 -#include <openssl/sha.h>
662 +#include "sha1.h"
663
664 #include "metainfo.h"
665 #include "subr.h"
666 @@ -161,21 +161,21 @@ bts_put(struct bt_stream *bts, off_t off
667 int
668 bts_sha(struct bt_stream *bts, off_t start, off_t length, uint8_t *hash)
669 {
670 - SHA_CTX ctx;
671 + struct sha1_ctx ctx;
672 char buf[SHAFILEBUF];
673 size_t wantread;
674 int err = 0;
675
676 - SHA1_Init(&ctx);
677 + sha1_init_ctx(&ctx);
678 while (length > 0) {
679 wantread = min(length, SHAFILEBUF);
680 if ((err = bts_get(bts, start, buf, wantread)) != 0)
681 break;
682 length -= wantread;
683 start += wantread;
684 - SHA1_Update(&ctx, buf, wantread);
685 + sha1_process_bytes(buf, wantread, &ctx);
686 }
687 - SHA1_Final(hash, &ctx);
688 + sha1_finish_ctx(&ctx, hash);
689 return err;
690 }
691
692 --- a/Makefile.am
693 +++ b/Makefile.am
694 @@ -1,6 +1,6 @@
695 AM_CFLAGS=-std=c99 -Wall @WARNNPS@
696 -AM_CPPFLAGS=-D_FILE_OFFSET_BITS=64 -D@EVLOOP_METHOD@ -I$(top_srcdir)/misc -I$(top_srcdir)/evloop @openssl_CPPFLAGS@
697 -AM_LDFLAGS=@openssl_LDFLAGS@
698 +AM_CPPFLAGS=-D_FILE_OFFSET_BITS=64 -D@EVLOOP_METHOD@ -I$(top_srcdir)/misc -I$(top_srcdir)/evloop
699 +AM_LDFLAGS=
700
701 bin_PROGRAMS=btpd/btpd cli/btcli cli/btinfo
702 noinst_LIBRARIES=misc/libmisc.a evloop/libevloop.a
703 @@ -25,15 +25,15 @@ btpd_btpd_SOURCES=\
704 btpd/upload.c btpd/upload.h\
705 btpd/util.c
706 btpd_btpd_CFLAGS=@TD_CFLAGS@ $(AM_CFLAGS)
707 -btpd_btpd_LDADD=@TD_LIBS@ misc/libmisc.a evloop/libevloop.a -lcrypto -lm @CLOCKLIB@ @INETLIBS@
708 +btpd_btpd_LDADD=@TD_LIBS@ misc/libmisc.a evloop/libevloop.a -lm @CLOCKLIB@ @INETLIBS@
709
710 # btinfo
711 cli_btinfo_SOURCES=cli/btinfo.c
712 -cli_btinfo_LDADD=misc/libmisc.a -lcrypto -lm
713 +cli_btinfo_LDADD=misc/libmisc.a -lm
714
715 # btcli
716 cli_btcli_SOURCES=cli/btcli.c cli/btcli.h cli/add.c cli/del.c cli/list.c cli/kill.c cli/start.c cli/stop.c cli/stat.c
717 -cli_btcli_LDADD=misc/libmisc.a -lcrypto -lm @INETLIBS@
718 +cli_btcli_LDADD=misc/libmisc.a -lm @INETLIBS@
719
720 # libmisc
721 misc_libmisc_a_SOURCES=\
722 @@ -45,7 +45,8 @@ misc_libmisc_a_SOURCES=\
723 misc/iobuf.c misc/iobuf.h\
724 misc/queue.h\
725 misc/stream.c misc/stream.h\
726 - misc/subr.c misc/subr.h
727 + misc/subr.c misc/subr.h\
728 + misc/sha1.c misc/sha1.h
729
730 # evloop
731 EXTRA_evloop_libevloop_a_SOURCES=evloop/epoll.c evloop/kqueue.c evloop/poll.c