source: packages/net/btpd/patches/000-sha1.diff @ 24776

Last change on this file since 24776 was 24776, checked in by swalker, 5 years ago

[packages] btpd: update to 0.16

  • drop the openssl changes that were re-added in r16657
  • update to current gnulib sha1 code
  • re-add gnulib config header guard
  • mark conffile
  • use PKG_INSTALL
File size: 22.5 KB
  • btpd/btpd.c

    a b  
    11#include "btpd.h" 
    22 
    3 #include <openssl/sha.h> 
     3#include "sha1.h" 
    44#include <signal.h> 
    55 
    66static uint8_t m_peer_id[20]; 
    btpd_init(void) 
    114114    idcon[sizeof(idcon) - 1] = '\0'; 
    115115    n = strlen(idcon); 
    116116 
    117     SHA1(idcon, n, m_peer_id); 
     117    sha1_buffer(idcon, n, m_peer_id); 
    118118    bcopy(m_peer_id, &seed, sizeof(seed)); 
    119119    bcopy(BTPD_VERSION, m_peer_id, sizeof(BTPD_VERSION) - 1); 
    120120    m_peer_id[sizeof(BTPD_VERSION) - 1] = '|'; 
  • btpd/content.c

    a b  
    11#include "btpd.h" 
    22 
    3 #include <openssl/sha.h> 
     3#include "sha1.h" 
    44#include <stream.h> 
    55 
    66struct content { 
    static struct timeout m_workev; 
    6060static int 
    6161test_hash(struct torrent *tp, uint8_t *hash, uint32_t piece) 
    6262{ 
    63     char piece_hash[SHA_DIGEST_LENGTH]; 
     63    char piece_hash[SHA1_DIGEST_SIZE]; 
    6464    tlib_read_hash(tp->tl, tp->pieces_off, piece, piece_hash); 
    65     return bcmp(hash, piece_hash, SHA_DIGEST_LENGTH); 
     65    return bcmp(hash, piece_hash, SHA1_DIGEST_SIZE); 
    6666} 
    6767 
    6868static int 
    6969test_piece(struct torrent *tp, uint32_t piece, int *ok) 
    7070{ 
    7171    int err; 
    72     uint8_t hash[SHA_DIGEST_LENGTH]; 
     72    uint8_t hash[SHA1_DIGEST_SIZE]; 
    7373    if ((err = bts_sha(tp->cm->rds, piece * tp->piece_length, 
    7474             torrent_piece_size(tp, piece), hash)) != 0) { 
    7575        btpd_log(BTPD_L_ERROR, "io error on '%s' (%s).\n", 
  • btpd/download_subr.c

    a b  
    2121 
    2222#include "btpd.h" 
    2323 
    24 #include <openssl/sha.h> 
     24#include "sha1.h" 
    2525#include <stream.h> 
    2626 
    2727static void 
    piece_log_hashes(struct piece *pc) 
    4242    for (unsigned i = 0; i < pc->nblocks; i++) { 
    4343        uint32_t bsize = torrent_block_size(tp, pc->index, pc->nblocks, i); 
    4444        cm_get_bytes(tp, pc->index, i * PIECE_BLOCKLEN, bsize, &buf); 
    45         SHA1(buf, bsize, &log->hashes[i * 20]); 
     45        sha1_buffer(buf, bsize, &log->hashes[i * 20]); 
    4646        free(buf); 
    4747    } 
    4848} 
  • btpd/torrent.c

    a b  
    11#include "btpd.h" 
    22 
    3 #include <openssl/sha.h> 
    4  
    53#define SAVE_INTERVAL 300 
    64 
    75static unsigned m_nghosts; 
  • configure.ac

    a b rm -f conftest.c conftest.$OBJEXT 
    2929rm -f conftest.c conftest.$OBJEXT 
    3030[$3])) 
    3131 
    32 AC_ARG_WITH(openssl, 
    33 [  --with-openssl=dir      use openssl installed in dir], 
    34 [ 
    35     AC_SUBST(openssl_LDFLAGS,["-L${withval}/lib -Wl,-R,${withval}/lib"]) 
    36     AC_SUBST(openssl_CPPFLAGS,"-I${withval}/include") 
    37 ]) 
    38  
    3932AC_ARG_WITH(evloop-method, 
    4033[  --with-evloop-method    select evloop method (epoll,poll,kqueue)], 
    4134    evloop_methods=$withval, 
    CC_ARGS_OK_IFELSE(-Wno-pointer-sign, 
    7669, 
    7770    AC_MSG_RESULT(no)) 
    7871 
    79 old_LDFLAGS="$LDFLAGS" 
    80 LDFLAGS="$LDFLAGS $openssl_LDFLAGS" 
    81 AC_CHECK_LIB(crypto, SHA1_Final,:,AC_MSG_FAILURE(btpd needs openssl's libraries and headers)) 
    82 LDFLAGS=$old_LDFLAGS 
    83  
    8472for m in $evloop_methods; do 
    8573    case $m in 
    8674    epoll) 
  • misc/metainfo.c

    a b  
    66#include <string.h> 
    77#include <strings.h> 
    88 
    9 #include <openssl/sha.h> 
    10  
     9#include "sha1.h" 
    1110#include "benc.h" 
    1211#include "metainfo.h" 
    1312#include "subr.h" 
    mi_info_hash(const char *p, uint8_t *has 
    159158    if (hash == NULL) 
    160159        if ((hash = malloc(20)) == NULL) 
    161160            return NULL; 
    162     return SHA1(info, benc_length(info), hash); 
     161    return (uint8_t *)sha1_buffer(info, benc_length(info), hash); 
    163162} 
    164163 
    165164char * 
  • new file misc/sha1.c

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

    - +  
     1/* Declarations of functions and data types used for SHA1 sum 
     2   library functions. 
     3   Copyright (C) 2000, 2001, 2003, 2005, 2006, 2008, 2009, 2010 Free Software 
     4   Foundation, Inc. 
     5 
     6   This program is free software; you can redistribute it and/or modify it 
     7   under the terms of the GNU General Public License as published by the 
     8   Free Software Foundation; either version 2, or (at your option) any 
     9   later version. 
     10 
     11   This program is distributed in the hope that it will be useful, 
     12   but WITHOUT ANY WARRANTY; without even the implied warranty of 
     13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
     14   GNU General Public License for more details. 
     15 
     16   You should have received a copy of the GNU General Public License 
     17   along with this program; if not, write to the Free Software Foundation, 
     18   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */ 
     19 
     20#ifndef SHA1_H 
     21# define SHA1_H 1 
     22 
     23# include <stdio.h> 
     24# include <stdint.h> 
     25 
     26# ifdef __cplusplus 
     27extern "C" { 
     28# endif 
     29 
     30#define SHA1_DIGEST_SIZE 20 
     31 
     32/* Structure to save state of computation between the single steps.  */ 
     33struct sha1_ctx 
     34{ 
     35  uint32_t A; 
     36  uint32_t B; 
     37  uint32_t C; 
     38  uint32_t D; 
     39  uint32_t E; 
     40 
     41  uint32_t total[2]; 
     42  uint32_t buflen; 
     43  uint32_t buffer[32]; 
     44}; 
     45 
     46 
     47/* Initialize structure containing state of computation. */ 
     48extern void sha1_init_ctx (struct sha1_ctx *ctx); 
     49 
     50/* Starting with the result of former calls of this function (or the 
     51   initialization function update the context for the next LEN bytes 
     52   starting at BUFFER. 
     53   It is necessary that LEN is a multiple of 64!!! */ 
     54extern void sha1_process_block (const void *buffer, size_t len, 
     55                                struct sha1_ctx *ctx); 
     56 
     57/* Starting with the result of former calls of this function (or the 
     58   initialization function update the context for the next LEN bytes 
     59   starting at BUFFER. 
     60   It is NOT required that LEN is a multiple of 64.  */ 
     61extern void sha1_process_bytes (const void *buffer, size_t len, 
     62                                struct sha1_ctx *ctx); 
     63 
     64/* Process the remaining bytes in the buffer and put result from CTX 
     65   in first 20 bytes following RESBUF.  The result is always in little 
     66   endian byte order, so that a byte-wise output yields to the wanted 
     67   ASCII representation of the message digest.  */ 
     68extern void *sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf); 
     69 
     70 
     71/* Put result from CTX in first 20 bytes following RESBUF.  The result is 
     72   always in little endian byte order, so that a byte-wise output yields 
     73   to the wanted ASCII representation of the message digest.  */ 
     74extern void *sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf); 
     75 
     76 
     77/* Compute SHA1 message digest for bytes read from STREAM.  The 
     78   resulting message digest number will be written into the 20 bytes 
     79   beginning at RESBLOCK.  */ 
     80extern int sha1_stream (FILE *stream, void *resblock); 
     81 
     82/* Compute SHA1 message digest for LEN bytes beginning at BUFFER.  The 
     83   result is always in little endian byte order, so that a byte-wise 
     84   output yields to the wanted ASCII representation of the message 
     85   digest.  */ 
     86extern void *sha1_buffer (const char *buffer, size_t len, void *resblock); 
     87 
     88# ifdef __cplusplus 
     89} 
     90# endif 
     91 
     92#endif 
  • misc/stream.c

    a b  
    55#include <stdlib.h> 
    66#include <unistd.h> 
    77 
    8 #include <openssl/sha.h> 
     8#include "sha1.h" 
    99 
    1010#include "metainfo.h" 
    1111#include "subr.h" 
    bts_put(struct bt_stream *bts, off_t off 
    161161int 
    162162bts_sha(struct bt_stream *bts, off_t start, off_t length, uint8_t *hash) 
    163163{ 
    164     SHA_CTX ctx; 
     164    struct sha1_ctx ctx; 
    165165    char buf[SHAFILEBUF]; 
    166166    size_t wantread; 
    167167    int err = 0; 
    168168 
    169     SHA1_Init(&ctx); 
     169    sha1_init_ctx(&ctx); 
    170170    while (length > 0) { 
    171171        wantread = min(length, SHAFILEBUF); 
    172172        if ((err = bts_get(bts, start, buf, wantread)) != 0) 
    173173            break; 
    174174        length -= wantread; 
    175175        start += wantread; 
    176         SHA1_Update(&ctx, buf, wantread); 
     176        sha1_process_bytes(buf, wantread, &ctx); 
    177177    } 
    178     SHA1_Final(hash, &ctx); 
     178    sha1_finish_ctx(&ctx, hash); 
    179179    return err; 
    180180} 
    181181 
  • Makefile.am

    a b  
    11AM_CFLAGS=-std=c99 -Wall @WARNNPS@ 
    2 AM_CPPFLAGS=-D_FILE_OFFSET_BITS=64 -D@EVLOOP_METHOD@ -I$(top_srcdir)/misc -I$(top_srcdir)/evloop @openssl_CPPFLAGS@ 
    3 AM_LDFLAGS=@openssl_LDFLAGS@ 
     2AM_CPPFLAGS=-D_FILE_OFFSET_BITS=64 -D@EVLOOP_METHOD@ -I$(top_srcdir)/misc -I$(top_srcdir)/evloop 
     3AM_LDFLAGS= 
    44 
    55bin_PROGRAMS=btpd/btpd cli/btcli cli/btinfo 
    66noinst_LIBRARIES=misc/libmisc.a evloop/libevloop.a 
    btpd_btpd_SOURCES=\ 
    2525        btpd/upload.c btpd/upload.h\ 
    2626        btpd/util.c 
    2727btpd_btpd_CFLAGS=@TD_CFLAGS@ $(AM_CFLAGS) 
    28 btpd_btpd_LDADD=@TD_LIBS@ misc/libmisc.a evloop/libevloop.a -lcrypto -lm @CLOCKLIB@ @INETLIBS@ 
     28btpd_btpd_LDADD=@TD_LIBS@ misc/libmisc.a evloop/libevloop.a -lm @CLOCKLIB@ @INETLIBS@ 
    2929 
    3030# btinfo 
    3131cli_btinfo_SOURCES=cli/btinfo.c 
    32 cli_btinfo_LDADD=misc/libmisc.a -lcrypto -lm 
     32cli_btinfo_LDADD=misc/libmisc.a -lm 
    3333 
    3434# btcli 
    3535cli_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 
    36 cli_btcli_LDADD=misc/libmisc.a -lcrypto -lm @INETLIBS@ 
     36cli_btcli_LDADD=misc/libmisc.a -lm @INETLIBS@ 
    3737 
    3838# libmisc 
    3939misc_libmisc_a_SOURCES=\ 
    misc_libmisc_a_SOURCES=\ 
    4545        misc/iobuf.c misc/iobuf.h\ 
    4646        misc/queue.h\ 
    4747        misc/stream.c misc/stream.h\ 
    48         misc/subr.c misc/subr.h 
     48        misc/subr.c misc/subr.h\ 
     49        misc/sha1.c misc/sha1.h 
    4950 
    5051# evloop 
    5152EXTRA_evloop_libevloop_a_SOURCES=evloop/epoll.c evloop/kqueue.c evloop/poll.c 
Note: See TracBrowser for help on using the repository browser.