Permalink
Browse files

Link against openssl rather than including Madore's SHA-1.

  • Loading branch information...
1 parent 7384e17 commit 54fb39461f82a2648b1485107c6912366a084129 @jech jech committed Dec 14, 2009
Showing with 16 additions and 206 deletions.
  1. +2 −2 Makefile
  2. +1 −3 hekate.cpc
  3. +4 −4 parse.c
  4. +2 −2 parse.h
  5. +0 −145 sha1.c
  6. +0 −34 sha1.h
  7. +7 −16 tracker.cpc
View
@@ -4,13 +4,13 @@ CLIBS=-lcurl
CDEBUGFLAGS=-O3 -Wall -g
CFLAGS=$(CDEBUGFLAGS) $(CLIBS) $(EXTRA_DEFINES)
-LDLIBS=-lcurl -lm -pthread -lcpcfull
+LDLIBS=-lcurl -lssl -lm -pthread -lcpcfull
.SUFFIXES: .cpc .cpi
.PHONY: all clean
-hekate: sha1.o util.o io.o list.o hashtable.o parse.o torrent.o tracker.o server.o hekate.o dht/dht.o
+hekate: util.o io.o list.o hashtable.o parse.o torrent.o tracker.o server.o hekate.o dht/dht.o
all: hekate
View
@@ -36,14 +36,12 @@ THE SOFTWARE.
#include <sys/types.h>
#include <curl/curl.h>
-
-#include "sha1.h"
+#include <openssl/sha.h>
#include "util.h"
#include "hashtable.h"
#include "parse.h"
#include "server.h"
-#include "sha1.h"
#include "torrent.h"
#include "tracker.h"
View
@@ -33,8 +33,8 @@ THE SOFTWARE.
#include <ctype.h> /* isdigit */
#include <inttypes.h> /* int64_t */
+#include <openssl/sha.h>
-#include "sha1.h"
#include "parse.h"
#include "hashtable.h"
@@ -103,14 +103,14 @@ close_buffer(buffer *b)
inline void
start_hashing(buffer *b)
{
- SHA1_start(&b->sha1);
+ SHA1_Init(&b->sha1);
b->hashing = 1;
}
inline void
stop_hashing(buffer *b, unsigned char *result)
{
- SHA1_stop(&b->sha1, result);
+ SHA1_Final(result, &b->sha1);
b->hashing = 0;
}
@@ -144,7 +144,7 @@ get_byte(buffer *b)
c = b->buf[b->cur];
b->cur = (b->cur + 1) % BUF_SIZE;
if(b->hashing)
- SHA1_feed(&b->sha1, c);
+ SHA1_Update(&b->sha1, &c, 1);
return c;
}
View
@@ -25,7 +25,7 @@ THE SOFTWARE.
#define PARSE_H
#include <inttypes.h>
-#include "sha1.h"
+#include <openssl/sha.h>
#define BUF_SIZE 512
#define SET_BASE_SIZE 8
@@ -60,7 +60,7 @@ typedef struct buffer {
int fd;
int cur;
int eof;
- struct sha1_ctx sha1;
+ SHA_CTX sha1;
char hashing;
} buffer;
View
145 sha1.c
@@ -1,145 +0,0 @@
-/* SHA1 routines */
-/* Written by David Madore <david.madore@ens.fr> */
-/* Public domain (2001/05/12) */
-/* This version last modified 2001/05/12 */
-
-/* Note: these routines do not depend on endianness. */
-
-/* === The header === */
-
-/* Put this in sha1.h if you don't like having everything in one big
- * file. */
-
-#ifndef _DMADORE_SHA1_H
-#define _DMADORE_SHA1_H
-
-struct sha1_ctx {
- /* The five chaining variables */
- unsigned long buf[5];
- /* Count number of message bits */
- unsigned long bits[2];
- /* Data being fed in */
- unsigned long in[16];
- /* Our position within the 512 bits (always between 0 and 63) */
- int b;
-};
-
-void SHA1_transform (unsigned long buf[5], const unsigned long in[16]);
-void SHA1_start (struct sha1_ctx *context);
-void SHA1_feed (struct sha1_ctx *context, unsigned char inb);
-void SHA1_stop (struct sha1_ctx *context, unsigned char digest[20]);
-
-#endif /* not defined _DMADORE_SHA1_H */
-
-/* === The implementation === */
-
-#define F1(x, y, z) ((x & y) | ((~x) & z))
-#define F2(x, y, z) (x ^ y ^ z)
-#define F3(x, y, z) ((x & y) | (x & z) | (y & z))
-#define F4(x, y, z) (x ^ y ^ z)
-
-#define SHA1STEP(f, data) \
- { unsigned long temp; \
- temp = e + f (b, c, d) + data + ((a<<5) | (a>>27)); \
- e=d; d=c; c=((b<<30)&0xffffffffUL)|(b>>2); \
- b=a; a=temp&0xffffffffUL; }
-
-void
-SHA1_transform (unsigned long buf[5], const unsigned long in[16])
-{
- register unsigned long a, b, c, d, e;
- unsigned long w[80];
- int i;
-
- a = buf[0]; b = buf[1]; c = buf[2]; d = buf[3]; e = buf[4];
- for ( i=0 ; i<16 ; i++ )
- w[i] = in[i];
- for ( ; i<80 ; i++ )
- {
- w[i] = w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16];
- w[i] = ((w[i]<<1)&0xffffffffUL) | (w[i]>>31);
- }
- for ( i=0 ; i<20 ; i++ )
- SHA1STEP (F1, w[i] + 0x5a827999UL);
- for ( ; i<40 ; i++ )
- SHA1STEP (F2, w[i] + 0x6ed9eba1UL);
- for ( ; i<60 ; i++ )
- SHA1STEP (F3, w[i] + 0x8f1bbcdcUL);
- for ( ; i<80 ; i++ )
- SHA1STEP (F4, w[i] + 0xca62c1d6UL);
- buf[0] += a; buf[1] += b; buf[2] += c; buf[3] += d; buf[4] += e;
- buf[0] &= 0xffffffffUL; buf[1] &= 0xffffffffUL;
- buf[2] &= 0xffffffffUL; buf[3] &= 0xffffffffUL; buf[4] &= 0xffffffffUL;
-}
-
-#undef F1
-#undef F2
-#undef F3
-#undef F4
-#undef SHA1STEP
-
-void
-SHA1_start (struct sha1_ctx *ctx)
-{
- int i;
-
- ctx->buf[0] = 0x67452301UL;
- ctx->buf[1] = 0xefcdab89UL;
- ctx->buf[2] = 0x98badcfeUL;
- ctx->buf[3] = 0x10325476UL;
- ctx->buf[4] = 0xc3d2e1f0UL;
- ctx->bits[0] = 0;
- ctx->bits[1] = 0;
- for ( i=0 ; i<16 ; i++ )
- ctx->in[i] = 0;
- ctx->b = 0;
-}
-
-void
-SHA1_feed (struct sha1_ctx *ctx, unsigned char inb)
-{
- int i;
- unsigned long temp;
-
- ctx->in[ctx->b/4] <<= 8;
- ctx->in[ctx->b/4] |= inb;
- if ( ++ctx->b >= 64 )
- {
- SHA1_transform (ctx->buf, ctx->in);
- ctx->b = 0;
- for ( i=0 ; i<16 ; i++ )
- ctx->in[i] = 0;
- }
- temp = ctx->bits[0];
- ctx->bits[0] += 8;
- ctx->bits[0] &= 0xffffffffUL;
- if ( temp > ctx->bits[0] )
- ctx->bits[1]++;
-}
-
-void
-SHA1_stop (struct sha1_ctx *ctx, unsigned char digest[20])
-{
- int i;
- unsigned long bits[2];
-
- for ( i=0 ; i<2 ; i++ )
- bits[i] = ctx->bits[i];
- SHA1_feed (ctx, 0x80);
- for ( ; ctx->b!=56 ; )
- SHA1_feed (ctx, 0);
- for ( i=1 ; i>=0 ; i-- )
- {
- SHA1_feed (ctx, (bits[i]>>24)&0xff);
- SHA1_feed (ctx, (bits[i]>>16)&0xff);
- SHA1_feed (ctx, (bits[i]>>8)&0xff);
- SHA1_feed (ctx, bits[i]&0xff);
- }
- for ( i=0 ; i<5 ; i++ )
- {
- digest[4*i] = (ctx->buf[i]>>24)&0xff;
- digest[4*i+1] = (ctx->buf[i]>>16)&0xff;
- digest[4*i+2] = (ctx->buf[i]>>8)&0xff;
- digest[4*i+3] = ctx->buf[i]&0xff;
- }
-}
View
34 sha1.h
@@ -1,34 +0,0 @@
-/* SHA1 routines */
-/* Written by David Madore <david.madore@ens.fr> */
-/* Public domain (2001/05/12) */
-/* This version last modified 2001/05/12 */
-
-/* Note: these routines do not depend on endianness. */
-
-/* === The header === */
-
-/* Put this in sha1.h if you don't like having everything in one big
- * file. */
-
-#ifndef _DMADORE_SHA1_H
-#define _DMADORE_SHA1_H
-
-struct sha1_ctx {
- /* The five chaining variables */
- unsigned long buf[5];
- /* Count number of message bits */
- unsigned long bits[2];
- /* Data being fed in */
- unsigned long in[16];
- /* Our position within the 512 bits (always between 0 and 63) */
- int b;
-};
-
-void SHA1_transform (unsigned long buf[5], const unsigned long in[16]);
-void SHA1_start (struct sha1_ctx *context);
-void SHA1_feed (struct sha1_ctx *context, unsigned char inb);
-void SHA1_stop (struct sha1_ctx *context, unsigned char digest[20]);
-
-#endif /* not defined _DMADORE_SHA1_H */
-
-/* EOF */
View
@@ -32,9 +32,9 @@ THE SOFTWARE.
#include <fcntl.h>
#include <unistd.h>
#include <arpa/inet.h>
+#include <openssl/sha.h>
#include "dht/dht.h"
-#include "sha1.h"
#include "io.h"
#include "list.h"
@@ -323,28 +323,19 @@ init_trackers()
/* DHT callbacks */
-/* Stupid, stupid, stupid. */
-static void
-SHA1_more(struct sha1_ctx *ctx, unsigned char *buf, int len)
-{
- int i;
- for(i = 0; i < len; i++)
- SHA1_feed(ctx, buf[i]);
-}
-
void
dht_hash(void *hash_return, int hash_size,
const void *v1, int len1,
const void *v2, int len2,
const void *v3, int len3)
{
- static struct sha1_ctx ctx;
+ static SHA_CTX ctx;
unsigned char sha1[20];
- SHA1_start(&ctx);
- SHA1_more(&ctx, v1, len1);
- SHA1_more(&ctx, v2, len2);
- SHA1_more(&ctx, v3, len3);
- SHA1_stop(&ctx, sha1);
+ SHA1_Init(&ctx);
+ SHA1_Update(&ctx, v1, len1);
+ SHA1_Update(&ctx, v2, len2);
+ SHA1_Update(&ctx, v3, len3);
+ SHA1_Final(sha1, &ctx);
if(hash_size > 20)
memset((char*)hash_return + 20, 0, hash_size - 20);
memcpy(hash_return, sha1, hash_size > 20 ? 20 : hash_size);

0 comments on commit 54fb394

Please sign in to comment.