Subject: [PATCH] use gcrypt instead of libssl for hash

From: Junichi Uekawa <[email-masked]>
Date: Sun, 17 Apr 2005 19:52:53 +0900

Hi,

This is the first  time for me to send you a patch; be gentle.
the following patch allows for use of gcrypt.

libssl seems to have a restrictive licensing wrt GPL applications.

This patch adds a requirement for libgcrypt11, and 
removes the requirement for libssl.

I hope I have not overlooked anything.

Signed-off-by: Junichi Uekawa <[email-masked]>

Makefile: 770595e02be4a9a55ff98910c53617c3b0f1e145
--- Makefile
+++ Makefile	2005-04-17 19:38:00.000000000 +0900
@@ -7,7 +7,7 @@
 # BREAK YOUR LOCAL DIFFS! show-diff and anything using it will likely randomly
 # break unless your underlying filesystem supports those sub-second times
 # (my ext3 doesn't).
-CFLAGS=-g -O3 -Wall
+CFLAGS=-g -O3 -Wall $(shell libgcrypt-config --cflags)
 
 CC=gcc
 
@@ -21,7 +21,7 @@
 install: $(PROG)
 	install $(PROG) $(HOME)/bin/
 
-LIBS= -lssl -lz
+LIBS= $(shell libgcrypt-config --libs) -lz
 
 init-db: init-db.o
 
cache.h: 5948db759b3f6fb5ade3b027f202330f71a8cb6a
--- cache.h
+++ cache.h	2005-04-17 19:25:40.000000000 +0900
@@ -12,7 +12,7 @@
 #include <sys/mman.h>
 #include <netinet/in.h>
 
-#include <openssl/sha.h>
+#include <gcrypt.h>
 #include <zlib.h>
 
 /*
read-cache.c: 740ffcce7026b268bd4dfe1d0a773ad7e3a24f96
--- read-cache.c
+++ read-cache.c	2005-04-17 19:34:37.000000000 +0900
@@ -116,11 +116,14 @@
 int check_sha1_signature(unsigned char *sha1, void *map, unsigned long size)
 {
 	unsigned char real_sha1[20];
-	SHA_CTX c;
+	gcry_md_hd_t c;
 
-	SHA1_Init(&c);
-	SHA1_Update(&c, map, size);
-	SHA1_Final(real_sha1, &c);
+	gcry_md_open(&c, GCRY_MD_SHA1, 0);
+	gcry_md_write(c, map, size);
+	gcry_md_final(c);
+	memcpy(real_sha1, gcry_md_read(c, 0), 20);
+	gcry_md_close(c);
+	
 	return memcmp(sha1, real_sha1, 20) ? -1 : 0;
 }
 
@@ -203,7 +206,7 @@
 	char *compressed;
 	z_stream stream;
 	unsigned char sha1[20];
-	SHA_CTX c;
+	gcry_md_hd_t c;
 
 	/* Set it up */
 	memset(&stream, 0, sizeof(stream));
@@ -222,9 +225,11 @@
 	size = stream.total_out;
 
 	/* Sha1.. */
-	SHA1_Init(&c);
-	SHA1_Update(&c, compressed, size);
-	SHA1_Final(sha1, &c);
+	gcry_md_open(&c, GCRY_MD_SHA1, 0);
+	gcry_md_write(c, compressed, size);
+	gcry_md_final(c);
+	memcpy(sha1, gcry_md_read(c, 0), 20);
+	gcry_md_close(c);
 
 	if (write_sha1_buffer(sha1, compressed, size) < 0)
 		return -1;
@@ -425,17 +430,20 @@
 
 static int verify_hdr(struct cache_header *hdr, unsigned long size)
 {
-	SHA_CTX c;
+	gcry_md_hd_t c;
 	unsigned char sha1[20];
 
 	if (hdr->hdr_signature != htonl(CACHE_SIGNATURE))
 		return error("bad signature");
 	if (hdr->hdr_version != htonl(1))
 		return error("bad version");
-	SHA1_Init(&c);
-	SHA1_Update(&c, hdr, offsetof(struct cache_header, sha1));
-	SHA1_Update(&c, hdr+1, size - sizeof(*hdr));
-	SHA1_Final(sha1, &c);
+	gcry_md_open(&c, GCRY_MD_SHA1, 0);
+	gcry_md_write(c, hdr, offsetof(struct cache_header, sha1));
+	gcry_md_write(c, hdr+1, size - sizeof(*hdr));
+	gcry_md_final(c);
+	memcpy(sha1, gcry_md_read(c, 0), 20);
+	gcry_md_close(c);
+
 	if (memcmp(sha1, hdr->sha1, 20))
 		return error("bad header sha1");
 	return 0;
@@ -498,7 +506,8 @@
 
 int write_cache(int newfd, struct cache_entry **cache, int entries)
 {
-	SHA_CTX c;
+	gcry_md_hd_t c;
+
 	struct cache_header hdr;
 	int i;
 
@@ -506,14 +515,16 @@
 	hdr.hdr_version = htonl(1);
 	hdr.hdr_entries = htonl(entries);
 
-	SHA1_Init(&c);
-	SHA1_Update(&c, &hdr, offsetof(struct cache_header, sha1));
+	gcry_md_open(&c, GCRY_MD_SHA1, 0);
+	gcry_md_write(c, &hdr, offsetof(struct cache_header, sha1));
 	for (i = 0; i < entries; i++) {
 		struct cache_entry *ce = cache[i];
 		int size = ce_size(ce);
-		SHA1_Update(&c, ce, size);
+		gcry_md_write(c, ce, size);
 	}
-	SHA1_Final(hdr.sha1, &c);
+	gcry_md_final(c);
+	memcpy(hdr.sha1, gcry_md_read(c, 0), 20);
+	gcry_md_close(c);
 
 	if (write(newfd, &hdr, sizeof(hdr)) != sizeof(hdr))
 		return -1;
update-cache.c: 5afecd1a4fd90d2505753ce2d5044d780fe69a7f
--- update-cache.c
+++ update-cache.c	2005-04-17 19:36:40.000000000 +0900
@@ -22,7 +22,7 @@
 	void *out = malloc(max_out_bytes);
 	void *metadata = malloc(namelen + 200);
 	void *in;
-	SHA_CTX c;
+	gcry_md_hd_t c;
 
 	in = "";
 	if (size)
@@ -54,9 +54,11 @@
 
 	deflateEnd(&stream);
 	
-	SHA1_Init(&c);
-	SHA1_Update(&c, out, stream.total_out);
-	SHA1_Final(ce->sha1, &c);
+	gcry_md_open(&c, GCRY_MD_SHA1, 0);
+	gcry_md_write(c, out, stream.total_out);
+	gcry_md_final(c);
+	memcpy(ce->sha1, gcry_md_read(c, 0), 20);
+	gcry_md_close(c);
 
 	return write_sha1_buffer(ce->sha1, out, stream.total_out);
 }
write-tree.c: 34a351b20fb38ea588f34bd9634f101b9dc533cb

regards,
	junichi

-- 
Junichi Uekawa, Debian Developer
17D6 120E 4455 1832 9423  7447 3059 BF92 CD37 56F4
http://www.netfort.gr.jp/~dancer/