Add x64 SHA-NI implementation to sha1 and sha256 in git ?
PATCH 1/1 Add x64 SHA-NI implementation to sha1 and sha256 in git ?
Looking for comments on if this is a good idea to add SHA-NI option to git, And if so, what'd be a good implementation.
The attached patch is more of a proof of concept, using a sha-ni example found on internet and have it tapped into git's "hash-ll.h" when it sees make flags "SHA1_SHANI=1" and/or "SHA256_SHANI=1" for sha1/sha256 respectively.
"git hash-object" is about 3-ish times faster for me
There are few topics that i'm uncertain about.
1. Is it good idea to have machine-specific asm codes in git. I read there was commit fd1ec82547 which removed assembly implementation for PPC_SHA1. Does that imply maintainers doesn't want machine-specific assembly in source?
2. If it is cool to have machine-specific codes in git, what's a good logic for their on/off? Build time flag? Run time flag? Run time automatic detection to corresponding instructions?
3. If i can include machine-specific codes, what'd be a good way/idea to cleanup the asm? I suppose git maintainers do not want someone else's license header?
Regards,
Feiyang
--- Makefile | 28 ++ hash-ll.h | 8 +- .../intel_sha_extensions_sha1_assembly.asm | 308 +++++++++++++++ sha1-x64shani/sha1_x64.c | 71 ++++ sha1-x64shani/sha1_x64.h | 21 + .../intel_sha_extensions_sha256_assembly.asm | 371 ++++++++++++++++++ sha256-x64shani/sha256_x64.c | 82 ++++ sha256-x64shani/sha256_x64.h | 23 ++ 8 files changed, 910 insertions(+), 2 deletions(-) create mode 100644 sha1-x64shani/intel_sha_extensions_sha1_assembly.asm create mode 100644 sha1-x64shani/sha1_x64.c create mode 100644 sha1-x64shani/sha1_x64.h create mode 100644 sha256-x64shani/intel_sha_extensions_sha256_assembly.asm create mode 100644 sha256-x64shani/sha256_x64.c create mode 100644 sha256-x64shani/sha256_x64.h
Makefile +28 -0
@@ -1917,6 +1917,11 @@ ifdef PPC_SHA1 $(error the PPC_SHA1 flag has been removed along with the PowerPC-specific SHA-1 implementation.) endif +ifdef SHA1_SHANI + LIB_OBJS += sha1-x64shani/sha1_x64.o + LIB_OBJS += sha1-x64shani/intel_sha_extensions_sha1_assembly.o + BASIC_CFLAGS += -DSHA1_SHANI +else ifdef OPENSSL_SHA1 EXTLIBS += $(LIB_4_CRYPTO) BASIC_CFLAGS += -DSHA1_OPENSSL
@@ -1957,7 +1962,13 @@ endif endif endif endif +endif +ifdef SHA256_SHANI + LIB_OBJS += sha256-x64shani/sha256_x64.o + LIB_OBJS += sha256-x64shani/intel_sha_extensions_sha256_assembly.o + BASIC_CFLAGS += -DSHA256_SHANI +else ifdef OPENSSL_SHA256 EXTLIBS += $(LIB_4_CRYPTO) BASIC_CFLAGS += -DSHA256_OPENSSL
@@ -1975,6 +1986,9 @@ else endif endif endif +endif + +$(info $$LIB_OBJS is [${LIB_OBJS}]) ifdef SHA1_MAX_BLOCK_SIZE LIB_OBJS += compat/sha1-chunked.o
@@ -2711,6 +2725,20 @@ missing_compdb_dir = compdb_args = endif +$(info $$OBJECTS is [${OBJECTS}]) + +### for SHA-NI +YASM_SRC := $(wildcard $(OBJECTS:o=asm)) +YASM_OBJ := $(YASM_SRC:asm=o) +OBJECTS := $(filter-out $(YASM_OBJ),$(OBJECTS)) + +$(info $$YASM_SRC is [${YASM_SRC}]) +$(info $$YASM_OBJ is [${YASM_OBJ}]) +$(info $$OBJECTS is [${OBJECTS}]) + +$(YASM_OBJ): %.o: %.asm $(missing_dep_dirs) + $(QUIET_CC)yasm $< -f elf64 -X gnu -g dwarf2 -o $@ + $(OBJECTS): %.o: %.c GIT-CFLAGS $(missing_dep_dirs) $(missing_compdb_dir) $(QUIET_CC)$(CC) -o $*.o -c $(dep_args) $(compdb_args) $(ALL_CFLAGS) $(EXTRA_CPPFLAGS) $<
hash-ll.h +6 -2
@@ -1,7 +1,9 @@#ifndef HASH_LL_H #define HASH_LL_H -#if defined(SHA1_APPLE ) +#if defined(SHA1_SHANI ) +#include "sha1-x64shani/sha1_x64.h" +#elif defined(SHA1_APPLE )#include <CommonCrypto/CommonDigest.h> #elif defined(SHA1_OPENSSL )#include <openssl/sha.h>
@@ -11,7 +13,9 @@#include "block-sha1/sha1.h" #endif -#if defined(SHA256_NETTLE ) +#if defined(SHA256_SHANI ) +#include "sha256-x64shani/sha256_x64.h" +#elif defined(SHA256_NETTLE )#include "sha256/nettle.h" #elif defined(SHA256_GCRYPT )#define SHA256_NEEDS_CLONE_HELPER
sha1-x64shani/intel_sha_extensions_sha1_assembly.asm new file +308 -0
308 lines not on this page · raw message
sha1-x64shani/sha1_x64.c new file +71 -0
71 lines not on this page · raw message
sha1-x64shani/sha1_x64.h new file +21 -0
@@ -0,0 +1,21 @@ +/* +* SHA1 routine using x64 SHA-NI instructions +*/ + +typedef struct shani_SHA_CTX +{ +uint32_t h[5 ]; +int len; +uint64_t total_len; +char msgbuf[64 ]; +}shani_SHA_CTX ; + + +void shani_SHA1_Init (shani_SHA_CTX *ctx); +void shani_SHA1_Update (shani_SHA_CTX *ctx,const void *dataIn,size_t len); +void shani_SHA1_Final (unsigned char hashout[20 ],shani_SHA_CTX *ctx); + +#define platform_SHA_CTX shani_SHA_CTX +#define platform_SHA1_Init shani_SHA1_Init +#define platform_SHA1_Update shani_SHA1_Update +#define platform_SHA1_Final shani_SHA1_Final
sha256-x64shani/intel_sha_extensions_sha256_assembly.asm new file +371 -0
371 lines not on this page · raw message
sha256-x64shani/sha256_x64.c new file +82 -0
83 lines not on this page · raw message
sha256-x64shani/sha256_x64.h new file +23 -0
@@ -0,0 +1,23 @@ +/* +* SHA256 routine using x64 SHA-NI instructions +*/ + +#define SHANI_SHA256_BLKSIZE 64 + +struct shani_SHA256_CTX { +uint32_t h[8 ]; +int len; +uint64_t total_len; +char msgbuf[SHANI_SHA256_BLKSIZE ]; +}; + +typedef struct shani_SHA256_CTX shani_SHA256_CTX ; + +void shani_SHA256_Init (shani_SHA256_CTX *ctx); +void shani_SHA256_Update (shani_SHA256_CTX *c,const void *data,size_t len); +void shani_SHA256_Final (unsigned char *digest,shani_SHA256_CTX *c); + +#define platform_SHA256_CTX shani_SHA256_CTX +#define platform_SHA256_Init shani_SHA256_Init +#define platform_SHA256_Update shani_SHA256_Update +#define platform_SHA256_Final shani_SHA256_Final
Signature · 1 line
2.41.0
Quoted 8 lines · Feiyang Xue
Looking for comments on if this is a good idea to add SHA-NI option to git, And if so, what'd be a good implementation.
The attached patch is more of a proof of concept, using a sha-ni example found on internet and have it tapped into git's "
hash-ll.h" when it sees make flags "SHA1_SHANI=1" and/or "SHA256_SHANI=1" for sha1/sha256 respectively."git hash-object" is about 3-ish times faster for me
You almost certainly don't want to use SHA-1 using native instructions because it doesn't detect collisions, unlike the default implementation (SHA-1-DC). Since SHA-1 collisions are relatively cheap (less than USD 45,000) to make, not detecting collisions would be a security issue worthy of a CVE.
It's fine for SHA-256, but see below.
Quoted 5 lines
There are few topics that i'm uncertain about.
1. Is it good idea to have machine-specific asm codes in git. I read there was commit
fd1ec82547which removed assembly implementation forPPC_SHA1. Does that imply maintainers doesn't want machine-specific assembly in source?
I'd prefer we didn't.
Nettle has SHA-NI extensions on systems that support it, and so does OpenSSL. OpenSSL can't be used by distros for licensing reasons, but Nettle can, and both of those libraries automatically detect the best code to use based on the chip. One of those libraries is almost always going to be linked into Git at some point because of libcurl anyway.
If we ship the code, then someone has to maintain it, and I don't know if we have any assembly experts. We might also have a bug that produced incorrect results, which would be pretty disastrous for the affected users. It's much better for maintainability if we push that off onto the cryptographic library maintainers who are much more competent in that regard than I am (I will not speak for others) and let distro maintainers simply link the appropriate library, which, as I said above, they're already effectively doing.
Signature · 2 lines
brian m. carlson (he/him or they/them) Toronto, Ontario, CA
Quoted 8 lines · brian m. carlson
If we ship the code, then someone has to maintain it, and I don't know if we have any assembly experts. We might also have a bug that produced incorrect results, which would be pretty disastrous for the affected users. It's much better for maintainability if we push that off onto the cryptographic library maintainers who are much more competent in that regard than I am (I will not speak for others) and let distro maintainers simply link the appropriate library, which, as I said above, they're already effectively doing.
Thanks for a well reasoned response. Very much appreciated.
Hi
Quoted 32 lines · brian m. carlson
Quoted 8 lines · Feiyang Xue
Looking for comments on if this is a good idea to add SHA-NI option to git, And if so, what'd be a good implementation.
The attached patch is more of a proof of concept, using a sha-ni example found on internet and have it tapped into git's "
hash-ll.h" when it sees make flags "SHA1_SHANI=1" and/or "SHA256_SHANI=1" for sha1/sha256 respectively."git hash-object" is about 3-ish times faster for me
You almost certainly don't want to use SHA-1 using native instructions because it doesn't detect collisions, unlike the default implementation (SHA-1-DC). Since SHA-1 collisions are relatively cheap (less than USD 45,000) to make, not detecting collisions would be a security issue worthy of a CVE.
It's fine for SHA-256, but see below.
Quoted 5 lines
There are few topics that i'm uncertain about.
1. Is it good idea to have machine-specific asm codes in git. I read there was commit
fd1ec82547which removed assembly implementation forPPC_SHA1. Does that imply maintainers doesn't want machine-specific assembly in source?I'd prefer we didn't.
Nettle has SHA-NI extensions on systems that support it, and so does OpenSSL. OpenSSL can't be used by distros for licensing reasons, but Nettle can, and both of those libraries automatically detect the best code to use based on the chip. One of those libraries is almost always going to be linked into Git at some point because of libcurl anyway.
Quoted 8 lines
If we ship the code, then someone has to maintain it, and I don't know if we have any assembly experts. We might also have a bug that produced incorrect results, which would be pretty disastrous for the affected users. It's much better for maintainability if we push that off onto the cryptographic library maintainers who are much more competent in that regard than I am (I will not speak for others) and let distro maintainers simply link the appropriate library, which, as I said above, they're already effectively doing.
In addition to all the valid points brian makes, the patch uses a non-standard assembler (yasm, see http://yasm.tortall.net/) that's not part of the default GNU toolchain.
Cheers, Beat
Quoted 45 lines · Beat Bolli
Quoted 7 lines · brian m. carlson
Quoted 6 lines · Feiyang Xue
Looking for comments on if this is a good idea to add SHA-NI option to git, And if so, what'd be a good implementation.
The attached patch is more of a proof of concept, using a sha-ni example found on internet and have it tapped into git's "
hash-ll.h" when it sees make flags "SHA1_SHANI=1" and/or "SHA256_SHANI=1" forsha1/sha256 respectively.
Quoted 17 lines
Quoted 2 lines
"git hash-object" is about 3-ish times faster for me
You almost certainly don't want to use SHA-1 using native instructions because it doesn't detect collisions, unlike the default implementation (SHA-1-DC). Since SHA-1 collisions are relatively cheap (less than USD
45,000) to make, not detecting collisions would be a security issue worthy of a CVE.It's fine for SHA-256, but see below.
Quoted 5 lines
There are few topics that i'm uncertain about.
1. Is it good idea to have machine-specific asm codes in git. I read there was commit
fd1ec82547which removed assembly implementation forPPC_SHA1. Does that imply maintainers doesn't want machine-specific assemblyin source?
Quoted 9 lines
I'd prefer we didn't.
Nettle has SHA-NI extensions on systems that support it, and so does OpenSSL. OpenSSL can't be used by distros for licensing reasons, but Nettle can, and both of those libraries automatically detect the best code to use based on the chip. One of those libraries is almost always going to be linked into Git at some point because of libcurl anyway.
Quoted 8 lines
If we ship the code, then someone has to maintain it, and I don't know if we have any assembly experts. We might also have a bug that produced incorrect results, which would be pretty disastrous for the affected users. It's much better for maintainability if we push that off onto the cryptographic library maintainers who are much more competent in that regard than I am (I will not speak for others) and let distro maintainers simply link the appropriate library, which, as I said above, they're already effectively doing.
Quoted 3 lines
In addition to all the valid points brian makes, the patch uses a non-standard assembler (yasm, see http://yasm.tortall.net/) that's not part of the default GNU toolchain.
Aside from that, not everyone uses the GNU toolchain. I would suggest that if someone needs ASM support for SHA-1, they could reimplement SHA-1-DC using rotate ASM primitives instead of directly using SHA1 assembly, but I question whether that is significantly faster than having the compiler optimizer generate the equivalent rotates. Still, maintaining any ASM code requires out of box skillsets that are unlikely sustainable. --Randall