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