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