Skip to contentSkip to conversation

use gcrypt instead of libssl for hash

PATCH In review Junichi Uekawa 6 participants 13 messages Jun 15, 2016 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 use gcrypt instead of libssl for hash

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.

Makefile: 770595e02be4a9a55ff98910c53617c3b0f1e145

Makefile +2 -2
@@ -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 +1 -1
@@ -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 +29 -18

94 lines not on this page · raw message

update-cache.c: 5afecd1a4fd90d2505753ce2d5044d780fe69a7f

update-cache.c +6 -4
@@ -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

Signature · 3 lines
Junichi Uekawa, Debian Developer
17D6 120E 4455 1832 9423  7447 3059 BF92 CD37 56F4
http://www.netfort.gr.jp/~dancer/
#1 lore raw
Quoted 3 lines · Junichi Uekawa

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

Well, libgcrypt seems to be pretty rare out there - I certainly don't have it installed on my machine.

Quoted 1 line

libssl seems to have a restrictive licensing wrt GPL applications.

It does? I really don't read it that way.

The openssl license is BSD+mention, but it only kicks in if you _redistribute_ it (and you're not allowed to market things as being openssl based without giving them credit, but that's another thing).

So yes, the openssl license is incompatible with the GPL in the sense that you cannot actually _mix_ the openssl source-code with the GPL source-code. But that's true of a lot of libraries, the normal system C library being just one common example.

The GPL makes explicit mention of the system libraries (which openssl definitely is by now), so it's ok by the GPL . And I don't see how you'd claim that the openssl license doesn't allow it. So it all looks ok by me.

That said, if somebody wants to abstract this out, and have a simple "sign with sha1" interface that can be used with both openssl and libgcrypt (or any other crypt license), then hey, go wild. Or merge the SHA1 code from the kernel, even, and make the project entirely self-sufficient.

But requiring libgcrypt seems silly. Especially as the libgcrypt interfaces are horribly ugly, much more so than the openssl ones - so even if you use libgcrypt, you don't actually want to use it directly, you want to have much nicer wrappers around it.

Linus

#2 lore raw
Quoted 3 lines · Linus Torvalds

Well, libgcrypt seems to be pretty rare out there - I certainly don't have it installed on my machine.

Well, I don't even have openssl ...

Quoted 2 lines

Or merge the SHA1 code from the kernel, even, and make the project entirely self-sufficient.

... so I took the sha1 code from Firefox (it's MPL or GPL - you choose).

Here's the patch.

Makefile +37 -22
diff -ruN git-0.04-orig/Makefile git-0.04/Makefile
@@ -9,42 +9,57 @@
 install: $(PROG)
 	install $(PROG) $(HOME)/bin/
 
-LIBS= -lssl -lz
+LIBS= -lz
 
 init-db: init-db.o
 
-update-cache: update-cache.o read-cache.o
-	$(CC) $(CFLAGS) -o update-cache update-cache.o read-cache.o $(LIBS)
+update-cache: update-cache.o read-cache.o sha1.o
+	$(CC) $(CFLAGS) -o update-cache update-cache.o read-cache.o sha1.o $(LIBS)
 
-show-diff: show-diff.o read-cache.o
-	$(CC) $(CFLAGS) -o show-diff show-diff.o read-cache.o $(LIBS)
+show-diff: show-diff.o read-cache.o sha1.o
+	$(CC) $(CFLAGS) -o show-diff show-diff.o read-cache.o sha1.o $(LIBS)
 
-write-tree: write-tree.o read-cache.o
-	$(CC) $(CFLAGS) -o write-tree write-tree.o read-cache.o $(LIBS)
+write-tree: write-tree.o read-cache.o sha1.o
+	$(CC) $(CFLAGS) -o write-tree write-tree.o read-cache.o sha1.o $(LIBS)
 
-read-tree: read-tree.o read-cache.o
-	$(CC) $(CFLAGS) -o read-tree read-tree.o read-cache.o $(LIBS)
+read-tree: read-tree.o read-cache.o sha1.o
+	$(CC) $(CFLAGS) -o read-tree read-tree.o read-cache.o sha1.o $(LIBS)
 
-commit-tree: commit-tree.o read-cache.o
-	$(CC) $(CFLAGS) -o commit-tree commit-tree.o read-cache.o $(LIBS)
+commit-tree: commit-tree.o read-cache.o sha1.o
+	$(CC) $(CFLAGS) -o commit-tree commit-tree.o read-cache.o sha1.o $(LIBS)
 
-cat-file: cat-file.o read-cache.o
-	$(CC) $(CFLAGS) -o cat-file cat-file.o read-cache.o $(LIBS)
+cat-file: cat-file.o read-cache.o sha1.o
+	$(CC) $(CFLAGS) -o cat-file cat-file.o read-cache.o sha1.o $(LIBS)
 
-fsck-cache: fsck-cache.o read-cache.o
-	$(CC) $(CFLAGS) -o fsck-cache fsck-cache.o read-cache.o $(LIBS)
+fsck-cache: fsck-cache.o read-cache.o sha1.o
+	$(CC) $(CFLAGS) -o fsck-cache fsck-cache.o read-cache.o sha1.o $(LIBS)
 
-checkout-cache: checkout-cache.o read-cache.o
-	$(CC) $(CFLAGS) -o checkout-cache checkout-cache.o read-cache.o $(LIBS)
+checkout-cache: checkout-cache.o read-cache.o sha1.o
+	$(CC) $(CFLAGS) -o checkout-cache checkout-cache.o read-cache.o sha1.o $(LIBS)
 
-diff-tree: diff-tree.o read-cache.o
-	$(CC) $(CFLAGS) -o diff-tree diff-tree.o read-cache.o $(LIBS)
-
-read-cache.o: cache.h
-show-diff.o: cache.h
+diff-tree: diff-tree.o read-cache.o sha1.o
+	$(CC) $(CFLAGS) -o diff-tree diff-tree.o read-cache.o sha1.o $(LIBS)
 
 clean:
 	rm -f *.o $(PROG) temp_git_file_*
 
 backup: clean
 	cd .. ; tar czvf dircache.tar.gz dir-cache
+
+depend:
+	makedepend -Y -- -- *.c 2>/dev/null
+
+# DO NOT DELETE
+
+cat-file.o: cache.h sha1.h
+checkout-cache.o: cache.h sha1.h
+commit-tree.o: cache.h sha1.h
+diff-tree.o: cache.h sha1.h
+fsck-cache.o: cache.h sha1.h
+init-db.o: cache.h sha1.h
+read-cache.o: cache.h sha1.h
+read-tree.o: cache.h sha1.h
+sha1.o: sha1.h
+show-diff.o: cache.h sha1.h
+update-cache.o: cache.h sha1.h
+write-tree.o: cache.h sha1.h
cache.h +1 -1
diff -ruN git-0.04-orig/cache.h git-0.04/cache.h
@@ -11,7 +11,7 @@
 #include <errno.h>
 #include <sys/mman.h>
 
-#include <openssl/sha.h>
+#include "sha1.h"
 #include <zlib.h>
 
 /*
sha1.c +152 -0

152 lines not on this page · raw message

sha1.h +45 -0

45 lines not on this page · raw message

Ciao, ET.

#3 lore raw

Mozilla SHA1 implementation

I've just integrated the Mozilla SHA1 library implementation that Adgar Toernig sent me into the standard git archive (but I did the integration differently).

The Mozilla SHA1 code is copyright Paul Kocher and Cryptography Research, and is released under a dual MPL/GPL license. Git obviously uses it in the GPL version, but I left the MPL choice there too, so that those two files (that are in a subdirectory of its own) will continue to be dual-licensed.

NOTE! I left git using the openssl libraries by default, and this is a built-time choice in the makefile. You can choose the Mozilla SHA1 implementation by doing

make clean
MOZILLA_SHA1=1 make
make install

but I suspect that anybody that has openssl installed and is working on an x86 is much better off with the i586-optimized openssl version. But if you don't have openssl by default, or if you don't like openssl for some other reason, you now have a nice easy choice.

Interestingly, the Mozilla SHA1 code is about twice as fast as the openssl code on my G5, and judging by the disassembly, it's because it's much simpler. I think the openssl people have unrolled all the loops totally, which tends to be a disaster on any half-way modern CPU. But hey, it could be something as simple as optimization flags too.

Linus

#12 lore raw
In reply to Linus Torvalds

Hi,

Thanks for your comments.

Quoted 5 lines
Quoted 2 lines

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

Well, libgcrypt seems to be pretty rare out there - I certainly don't have it installed on my machine.

Hmm... okay. Might be the case if you don't use much of GNOME and other apps.

Quoted 7 lines
Quoted 1 line

libssl seems to have a restrictive licensing wrt GPL applications.

The GPL makes explicit mention of the system libraries (which openssl definitely is by now), so it's ok by the GPL . And I don't see how you'd claim that the openssl license doesn't allow it. So it all looks ok by me.

From a standpoint of Debian Developer; it feels not-so-clear;

since Debian will be distributing openssl and git.

openssl guys seem to recommend adding a quote, which might be sufficient.
"This program is released under the GPL with the additional exemption that compiling, linking, and/or using OpenSSL is allowed."
http://www.openssl.org/support/faq.html#LEGAL2

Quoted 4 lines

But requiring libgcrypt seems silly. Especially as the libgcrypt interfaces are horribly ugly, much more so than the openssl ones - so even if you use libgcrypt, you don't actually want to use it directly, you want to have much nicer wrappers around it.

I'll consider wrappers approach.

regards,
junichi

Signature · 3 lines
Junichi Uekawa, Debian Developer
17D6 120E 4455 1832 9423  7447 3059 BF92 CD37 56F4
http://www.netfort.gr.jp/~dancer/
#4 lore raw

Re: Mozilla SHA1 implementation

In reply to Linus Torvalds
Quoted 3 lines · Linus Torvalds

I've just integrated the Mozilla SHA1 library implementation that Adgar Toernig sent me into the standard git archive (but I did the integration differently).

Here is a new PPC SHA1 patch that integrates better with this...

Quoted 5 lines

Interestingly, the Mozilla SHA1 code is about twice as fast as the openssl code on my G5, and judging by the disassembly, it's because it's much simpler. I think the openssl people have unrolled all the loops totally, which tends to be a disaster on any half-way modern CPU. But hey, it could be something as simple as optimization flags too.

Very interesting. On my G4 powerbook (since I am at LCA), for a fsck-cache on a linux-2.6 tree, it takes 6.6 seconds with the openssl SHA1, 10.7 seconds with the Mozilla SHA1, and ~5.8 seconds with my SHA1. I'll test it on a G5 tonight, hopefully.

Paul.

Makefile +6 -1
diff -urN git.orig/Makefile git/Makefile
@@ -34,9 +34,14 @@
   SHA1_HEADER="mozilla-sha1/sha1.h"
   LIB_OBJS += mozilla-sha1/sha1.o
 else
+ifdef PPC_SHA1
+  SHA1_HEADER="ppc/sha1.h"
+  LIB_OBJS += ppc/sha1.o ppc/sha1ppc.o
+else
   SHA1_HEADER=<openssl/sha.h>
   LIBS += -lssl
 endif
+endif
 
 CFLAGS += '-DSHA1_HEADER=$(SHA1_HEADER)'
 
@@ -77,7 +82,7 @@
 write-tree.o: $(LIB_H)
 
 clean:
-	rm -f *.o mozilla-sha1/*.o $(PROG) $(LIB_FILE)
+	rm -f *.o mozilla-sha1/*.o ppc/*.o $(PROG) $(LIB_FILE)
 
 backup: clean
 	cd .. ; tar czvf dircache.tar.gz dir-cache
ppc/sha1.c new file +72 -0

72 lines not on this page · raw message

ppc/sha1.h new file +20 -0
diff -urN git.orig/ppc/sha1.h git/ppc/sha1.h
@@ -0,0 +1,20 @@
+/*
+ * SHA-1 implementation.
+ *
+ * Copyright (C) 2005 Paul Mackerras <[email protected]>
+ */
+#include <stdint.h>
+
+typedef struct sha_context {
+	uint32_t hash[5];
+	uint32_t cnt;
+	uint64_t len;
+	union {
+		unsigned char b[64];
+		uint64_t l[8];
+	} buf;
+} SHA_CTX;
+
+int SHA1_Init(SHA_CTX *c);
+int SHA1_Update(SHA_CTX *c, const void *p, unsigned long n);
+int SHA1_Final(unsigned char *hash, SHA_CTX *c);
ppc/sha1ppc.S new file +185 -0

185 lines not on this page · raw message

#5 lore raw

Re: Mozilla SHA1 implementation

In reply to Linus Torvalds
Quoted 5 lines · Linus Torvalds

Interestingly, the Mozilla SHA1 code is about twice as fast as the openssl code on my G5, and judging by the disassembly, it's because it's much simpler. I think the openssl people have unrolled all the loops totally, which tends to be a disaster on any half-way modern CPU. But hey, it could be something as simple as optimization flags too.

Which gcc version are you using?

I get the opposite result on my 2GHz G5: the Mozilla version does 45MB/s, the openssl version does 135MB/s, and my version does 218MB/s. The time for a fsck-cache on a linux-2.6 tree (cache hot) is 8.0 seconds for the Mozilla version, 5.2 seconds for the openssl version, and 4.4 seconds for my version.

Paul.

#6 lore raw

Re: Mozilla SHA1 implementation

Quoted 9 lines · Paul Mackerras
Quoted 5 lines · Linus Torvalds

Interestingly, the Mozilla SHA1 code is about twice as fast as the openssl code on my G5, and judging by the disassembly, it's because it's much simpler. I think the openssl people have unrolled all the loops totally, which tends to be a disaster on any half-way modern CPU. But hey, it could be something as simple as optimization flags too.

Which gcc version are you using?

gcc-3.3.3.

But it's more likely the precompiled libssl. I'm not compiling the openssl thing myself, but just using the standard 0.9.7a version that comes with YDL. Which, btw, causes all of

	/lib/libcrypto.so.4
	/usr/lib/libgssapi_krb5.so.2
	/usr/lib/libkrb5.so.3
	/lib/libcom_err.so.2
	/usr/lib/libk5crypto.so.3
	/lib/libresolv.so.2
	/lib/libdl.so.2

to also be included. Oh, well.

Quoted 5 lines

I get the opposite result on my 2GHz G5: the Mozilla version does 45MB/s, the openssl version does 135MB/s, and my version does 218MB/s. The time for a fsck-cache on a linux-2.6 tree (cache hot) is 8.0 seconds for the Mozilla version, 5.2 seconds for the openssl version, and 4.4 seconds for my version.

I get 16 seconds for the openssl one, and 8 for the Mozilla one. I'll try your version.

Linus

#7 lore raw

Re: Mozilla SHA1 implementation

Quoted 5 lines · Linus Torvalds

But it's more likely the precompiled libssl. I'm not compiling the openssl thing myself, but just using the standard 0.9.7a version that comes with YDL. Which, btw, causes all of

	/lib/libcrypto.so.4

This is the one that actually has the SHA1 stuff, not libssl at all. You can skip at least some of this by just using -lcrypto.

Quoted 6 lines
	/usr/lib/libgssapi_krb5.so.2
	/usr/lib/libkrb5.so.3
	/lib/libcom_err.so.2
	/usr/lib/libk5crypto.so.3
	/lib/libresolv.so.2
	/lib/libdl.so.2

-Daniel
*This .sig left intentionally blank*

#8 lore raw

Re: Mozilla SHA1 implementation

In reply to Linus Torvalds
Quoted 9 lines · Linus Torvalds
Quoted 6 lines

I get the opposite result on my 2GHz G5: the Mozilla version does 45MB/s, the openssl version does 135MB/s, and my version does 218MB/s. The time for a fsck-cache on a linux-2.6 tree (cache hot) is 8.0 seconds for the Mozilla version, 5.2 seconds for the openssl version, and 4.4 seconds for my version.

I get 16 seconds for the openssl one, and 8 for the Mozilla one. I'll try your version.

Ok, I get 4.9s on my kernel archive, so this is definitely a big win.

Can you sign off on the thing, since this is real new code? Let's do it right.

Linus

#9 lore raw

(anal) Q: Are there any coding styles or development guidelines?

I'm currently doing a source audit of the git core components. Mainly I want to check if I can spot some left over memory leaks.

Unfortunately ;) I didn't find any so far (after reading five files).

Still I did find quite a lot of stuff that lint would most likely complain about. Like not checking return values. Should this be fixed now or isn't it time to do the cleanup, yet?

I also found several literal copies of the same function including function name, parameter list, etc. Wouldn't it be better do clean those up and put them in a utility.{c,h} file? A similar problem is the continous reimplementation of linked lists, dynamic memory, smart strings / vectors, etc. And then there are some stale files (i.e. revision.*) that the Changelog already mentions as removed, but which are still active in HEAD.

I am a little reluctant to do the work, as the code still changes so fast I do not really know if code I fix will still be there tomorrow.

Also I do not know if there is any notion of coding style published somewhere. I only noticed, that the code does not look like anything I'd have written and seems to follow some general principle.

Kind regards,

--Robert Suetterlin ([email protected])
phone: (+49)89 / 30000-3546 fax: (+49)89 / 30000-3950

#10 lore raw

Re: (anal) Q: Are there any coding styles or development guidelines?

Quoted 9 lines · Klaus Robert Suetterlin

I'm currently doing a source audit of the git core components. Mainly I want to check if I can spot some left over memory leaks.

Unfortunately ;) I didn't find any so far (after reading five files).

Still I did find quite a lot of stuff that lint would most likely complain about. Like not checking return values. Should this be fixed now or isn't it time to do the cleanup, yet?

I personally don't think we're quite there yet. You'll note that a lot of routines just call "die()" when they don't find the data they want etc. All things that we probably will need to clean up eventually, but is fine for now.

Quoted 3 lines

I also found several literal copies of the same function including function name, parameter list, etc. Wouldn't it be better do clean those up and put them in a utility.{c,h} file?

Yes, we can start doing things like that, especially if they really _do_ end up making sense in a bigger picture.

Some of that is because I didn't clean up the Makefile until fairly recently, so it used to be harder than it is now to have a shared object file. In contrast, now you just add it to the list of library objects, and off you go.

Quoted 3 lines

A similar problem
is the continous reimplementation of linked lists, dynamic memory, smart strings / vectors, etc.

Sometimes specific duplication is just _easier_ than trying to be clever and having some generic shared version. For example, there's a few data-specific versions of "insert into sorted array" there, and the fact is, they are so simple that it's likely less work to keep them that way than it would be to create some "generic" thing that did it and was passed in some descriptor of the data.

Generic routines aren't always good. Sometimes the abstraction ends up adding more of a mental burden than just doing the thing in a simple way in the first place.

Quoted 2 lines

And then there are some stale files (i.e. revision.*) that the Changelog already mentions as removed, but which are still active in HEAD.

They definitely are _not_ active in HEAD, at least not in my version.

You may have stale copies in your directory, but they should show up as being stale when you do "show-files --others".

Linus

#11 lore raw

Re: Mozilla SHA1 implementation

In reply to Linus Torvalds
Quoted 3 lines · Linus Torvalds

I've just integrated the Mozilla SHA1 library implementation into the standard git archive

Thanks.

In the mood for another compatibility hack? My zlib doesn't have deflateBound and browsing through the git-ml archive it seems I'm not the only one. How about putting this snippet into some header file?

#if ZLIB_VERNUM < 0x1200
#define deflateBound(c,s) ((s) + (((s) + 7) >> 3) + (((s) + 63) >> 6) + 11) #endif

The formula is the conservative upper bound from zlib-1.2.2.

Ciao, ET.