Additional SHA implementations
cover Additional SHA implementations
Hi Folks,
I have been investigating performance and security of alternative hash algorithms in git, and while doing so, have added algorithm alternatives to git to allow testing them out to see how they affect performance.
I have been thinking that it might not be ideal to add lots of algorithm variants to git so as to reduce potential future incompatibility, however, that is another discussion. The algorithms need to be added to test out performance, and this patch series is for anyone interested in testing out alternative algorithms. While it is necessary to have mandatory hashes for compatibility, there are potential use-cases where alternative hashes might be beneficial. There is also the possibility of N-hash support when implementing compatObjectFormat as outlined in Git hash function transition [1].
This patch series adds to the generic hash algorithm interface in git with two new hash algorithms: SHA512 (including SHA512/224, SHA512/256) and SHA3. The patch also adds SHA224 (truncated SHA256) and add internal implementations for the two new hashes as well as library wrappers. THe SHA512 block digest function is derived from the existing SHA256 implementation, modified for 64-bit, as well as the truncated forms.
- moves existing SHA code into sha/ directory
- adds new SHA512 implementation (based on existing SHA256 impl)
- adds new SHA3 implementation (based on NIST submission)
- adds gcrypt library interfaces (make GCRYPT_SHA512=1 GCRYPT_SHA3=1)
- adds OpenSSL EVP interfaces (make OPENSSL_EVP=1)
- adds test cipher commands to test-tool
- adds test vectors to t/t0015-hash.sh
- adds empty tree and blob hashes using pre-existing scheme
It seems that muliple hash function support would have more utility if the additional computed hashes were the pure hash of the unpacked object, instead of compressed and prefixed object data. In any case, I can see several potential uses for the alternative hash algorithms.
Noticed that the current empty tree and empty blob have the length prefixes ("blob 0\0" and "tree 0\0") which may or may not be required with new hashes given that they have length extension resistance?
This series adds almost all known SHA variants so that folk who are working on abstracting away support for new algos can easily try alternatives. The algos added by this patch series are not exposed to the user in any way, and require changes to .hash_algo in cache.h to enable them, so they should not cause any compatibility issues, rather just extend the in-tree API.
We could perhaps add options so they are not compiled by default.
This table shows the algorithm type codes after the series is applied:
| algorithm | type-code | integer-code | | -------------- | --------- | ------------ | | "sha1" | "sha1" | 0x73686131 | | "sha224" | "s224" | 0x73323234 | | "sha256" | "s256" | 0x73323536 | | "sha512" | "s512" | 0x73353132 | | "sha512/224" | "s226" | 0x73323236 | | "sha512/256" | "s228" | 0x73323238 | | "sha3-224" | "s388" | 0x73323234 | | "sha3-256" | "s398" | 0x73323536 | | "sha3-384" | "s3a8" | 0x73336834 | | "sha3-512" | "s3b8" | 0x73356132 |
I hope you find these patches useful.
Regards,
Michael
[1] https://github.com/git/git/blob/master/Documentation/technical/hash-function-transition.txt
Michael Clark (6):
Move all SHA algorithm variants into sha/ directory Add an implementation of the SHA-512 hash algorithm Add an implementation of the SHA-3 hash algorithm Add an implementation of the SHA224 truncated hash algorithm Add OpenSSL EVP interface for SHA-3 and SHA-512 algorithms Add sha/README.md with table of SHA algorithm details
Makefile | 46 +++-
hash.h | 125 ++++++++++-
sha/README.md | 23 ++
{block-sha1 => sha/sha1}/sha1.c | 2 +-
{block-sha1 => sha/sha1}/sha1.h | 0
{sha1dc => sha/sha1dc}/.gitattributes | 0
{sha1dc => sha/sha1dc}/LICENSE.txt | 0
{sha1dc => sha/sha1dc}/sha1.c | 0
{sha1dc => sha/sha1dc}/sha1.h | 0
{sha1dc => sha/sha1dc}/ubc_check.c | 0
{sha1dc => sha/sha1dc}/ubc_check.h | 0
sha1dc_git.c => sha/sha1dc_git.c | 0
sha1dc_git.h => sha/sha1dc_git.h | 2 +-
{ppc => sha/sha1ppc}/sha1.c | 0
{ppc => sha/sha1ppc}/sha1.h | 0
{ppc => sha/sha1ppc}/sha1ppc.S | 0
{sha256 => sha/sha256}/gcrypt.h | 15 +-
{sha256/block => sha/sha256}/sha256.c | 18 +-
{sha256/block => sha/sha256}/sha256.h | 9 +
sha/sha3/gcrypt.h | 53 +++++
sha/sha3/sha3.c | 271 ++++++++++++++++++++++
sha/sha3/sha3.h | 34 +++
sha/sha512/gcrypt.h | 43 ++++
sha/sha512/sha512.c | 206 +++++++++++++++++
sha/sha512/sha512.h | 31 +++
sha/sha_evp/sha_evp.c | 99 +++++++++
sha/sha_evp/sha_evp.h | 51 +++++
sha1-file.c | 309 ++++++++++++++++++++++++++
t/helper/test-sha256.c | 5 +
t/helper/test-sha3.c | 22 ++
t/helper/test-sha512.c | 17 ++
t/helper/test-tool.c | 8 +
t/helper/test-tool.h | 8 +
t/t0015-hash.sh | 214 ++++++++++++++++++
34 files changed, 1592 insertions(+), 19 deletions(-)
create mode 100644 sha/README.md
rename {block-sha1 => sha/sha1}/sha1.c (99%)
rename {block-sha1 => sha/sha1}/sha1.h (100%)
rename {sha1dc => sha/sha1dc}/.gitattributes (100%)
rename {sha1dc => sha/sha1dc}/LICENSE.txt (100%)
rename {sha1dc => sha/sha1dc}/sha1.c (100%)
rename {sha1dc => sha/sha1dc}/sha1.h (100%)
rename {sha1dc => sha/sha1dc}/ubc_check.c (100%)
rename {sha1dc => sha/sha1dc}/ubc_check.h (100%)
rename sha1dc_git.c => sha/sha1dc_git.c (100%)
rename sha1dc_git.h => sha/sha1dc_git.h (95%)
rename {ppc => sha/sha1ppc}/sha1.c (100%)
rename {ppc => sha/sha1ppc}/sha1.h (100%)
rename {ppc => sha/sha1ppc}/sha1ppc.S (100%)
rename {sha256 => sha/sha256}/gcrypt.h (58%)
rename {sha256/block => sha/sha256}/sha256.c (93%)
rename {sha256/block => sha/sha256}/sha256.h (67%)
create mode 100644 sha/sha3/gcrypt.h
create mode 100644 sha/sha3/sha3.c
create mode 100644 sha/sha3/sha3.h
create mode 100644 sha/sha512/gcrypt.h
create mode 100644 sha/sha512/sha512.c
create mode 100644 sha/sha512/sha512.h
create mode 100644 sha/sha_evp/sha_evp.c
create mode 100644 sha/sha_evp/sha_evp.h
create mode 100644 t/helper/test-sha3.c
create mode 100644 t/helper/test-sha512.cSignature · 1 line
2.20.1
PATCH 1/6 Move all SHA algorithm variants into sha/ directory
This is a tidy up before we add additional hash agorithms. This patch does not change code. It only contains renames.
After this patch, all hash algorithm implementations are in sha/.
---
Makefile | 16 ++++++++--------
hash.h | 6 +++---
{block-sha1 => sha/sha1}/sha1.c | 2 +-
{block-sha1 => sha/sha1}/sha1.h | 0
{sha1dc => sha/sha1dc}/.gitattributes | 0
{sha1dc => sha/sha1dc}/LICENSE.txt | 0
{sha1dc => sha/sha1dc}/sha1.c | 0
{sha1dc => sha/sha1dc}/sha1.h | 0
{sha1dc => sha/sha1dc}/ubc_check.c | 0
{sha1dc => sha/sha1dc}/ubc_check.h | 0
sha1dc_git.c => sha/sha1dc_git.c | 0
sha1dc_git.h => sha/sha1dc_git.h | 2 +-
{ppc => sha/sha1ppc}/sha1.c | 0
{ppc => sha/sha1ppc}/sha1.h | 0
{ppc => sha/sha1ppc}/sha1ppc.S | 0
{sha256 => sha/sha256}/gcrypt.h | 0
{sha256/block => sha/sha256}/sha256.c | 0
{sha256/block => sha/sha256}/sha256.h | 0
18 files changed, 13 insertions(+), 13 deletions(-)
rename {block-sha1 => sha/sha1}/sha1.c (99%)
rename {block-sha1 => sha/sha1}/sha1.h (100%)
rename {sha1dc => sha/sha1dc}/.gitattributes (100%)
rename {sha1dc => sha/sha1dc}/LICENSE.txt (100%)
rename {sha1dc => sha/sha1dc}/sha1.c (100%)
rename {sha1dc => sha/sha1dc}/sha1.h (100%)
rename {sha1dc => sha/sha1dc}/ubc_check.c (100%)
rename {sha1dc => sha/sha1dc}/ubc_check.h (100%)
rename sha1dc_git.c => sha/sha1dc_git.c (100%)
rename sha1dc_git.h => sha/sha1dc_git.h (95%)
rename {ppc => sha/sha1ppc}/sha1.c (100%)
rename {ppc => sha/sha1ppc}/sha1.h (100%)
rename {ppc => sha/sha1ppc}/sha1ppc.S (100%)
rename {sha256 => sha/sha256}/gcrypt.h (100%)
rename {sha256/block => sha/sha256}/sha256.c (100%)
rename {sha256/block => sha/sha256}/sha256.h (100%)Makefile +8 -8
@@ -1158,7 +1158,7 @@ THIRD_PARTY_SOURCES += compat/obstack.% THIRD_PARTY_SOURCES += compat/poll/% THIRD_PARTY_SOURCES += compat/regex/% THIRD_PARTY_SOURCES += sha1collisiondetection/% -THIRD_PARTY_SOURCES += sha1dc/% +THIRD_PARTY_SOURCES += sha/sha1dc/% GITLIBS = common-main.o $(LIB_FILE) $(XDIFF_LIB) EXTLIBS =
@@ -1657,11 +1657,11 @@ ifdef OPENSSL_SHA1 BASIC_CFLAGS += -DSHA1_OPENSSL else ifdef BLK_SHA1 - LIB_OBJS += block-sha1/sha1.o + LIB_OBJS += sha/sha1/sha1.o BASIC_CFLAGS += -DSHA1_BLK else ifdef PPC_SHA1 - LIB_OBJS += ppc/sha1.o ppc/sha1ppc.o + LIB_OBJS += sha/sha1ppc/sha1.o sha/sha1ppc/sha1ppc.o BASIC_CFLAGS += -DSHA1_PPC else ifdef APPLE_COMMON_CRYPTO
@@ -1670,7 +1670,7 @@ ifdef APPLE_COMMON_CRYPTO else DC_SHA1 := YesPlease BASIC_CFLAGS += -DSHA1_DC - LIB_OBJS += sha1dc_git.o + LIB_OBJS += sha/sha1dc_git.o ifdef DC_SHA1_EXTERNAL ifdef DC_SHA1_SUBMODULE ifneq ($(DC_SHA1_SUBMODULE),auto)
@@ -1685,8 +1685,8 @@ ifdef DC_SHA1_SUBMODULE LIB_OBJS += sha1collisiondetection/lib/ubc_check.o BASIC_CFLAGS += -DDC_SHA1_SUBMODULE else - LIB_OBJS += sha1dc/sha1.o - LIB_OBJS += sha1dc/ubc_check.o + LIB_OBJS += sha/sha1dc/sha1.o + LIB_OBJS += sha/sha1dc/ubc_check.o endif BASIC_CFLAGS += \ -DSHA1DC_NO_STANDARD_INCLUDES \
@@ -1707,7 +1707,7 @@ ifdef GCRYPT_SHA256 BASIC_CFLAGS += -DSHA256_GCRYPT EXTLIBS += -lgcrypt else - LIB_OBJS += sha256/block/sha256.o + LIB_OBJS += sha/sha256/sha256.o BASIC_CFLAGS += -DSHA256_BLK endif endif
@@ -2782,7 +2782,7 @@ sparse: $(SP_OBJ) GEN_HDRS := command-list.h unicode-width.h EXCEPT_HDRS := $(GEN_HDRS) compat/% xdiff/% ifndef GCRYPT_SHA256 - EXCEPT_HDRS += sha256/gcrypt.h + EXCEPT_HDRS += sha/sha256/gcrypt.h endif CHK_HDRS = $(filter-out $(EXCEPT_HDRS),$(LIB_H)) HCO = $(patsubst %.h,%.hco,$(CHK_HDRS))
hash.h +3 -3
@@ -10,9 +10,9 @@#elif defined(SHA1_OPENSSL )#include <openssl/sha.h> #elif defined(SHA1_DC ) -#include "sha1dc_git.h" +#include "sha/sha1dc_git.h" #else /* SHA1_BLK */ -#include "block-sha1/sha1.h" +#include "sha/sha1/sha1.h" #endif #if defined(SHA256_GCRYPT )
@@ -20,7 +20,7 @@#elif defined(SHA256_OPENSSL )#include <openssl/sha.h> #else -#include "sha256/block/sha256.h" +#include "sha/sha256/sha256.h" #endif #ifndef platform_SHA_CTX
sha/sha1/sha1.c renamed from block-sha1/sha1.c +1 -1
@@ -7,7 +7,7 @@*/ /* this is only to get definitions for memcpy(), ntohl() and htonl() */ -#include "../git-compat-util.h" +#include "git-compat-util.h" #include "sha1.h"
sha/sha1/sha1.h renamed from block-sha1/sha1.h +0 -0
sha/sha1dc/.gitattributes renamed from sha1dc/.gitattributes +0 -0
sha/sha1dc/LICENSE.txt renamed from sha1dc/LICENSE.txt +0 -0
sha/sha1dc/sha1.c renamed from sha1dc/sha1.c +0 -0
sha/sha1dc/sha1.h renamed from sha1dc/sha1.h +0 -0
sha/sha1dc/ubc_check.c renamed from sha1dc/ubc_check.c +0 -0
sha/sha1dc/ubc_check.h renamed from sha1dc/ubc_check.h +0 -0
sha/sha1dc_git.c renamed from sha1dc_git.c +0 -0
sha/sha1dc_git.h renamed from sha1dc_git.h +1 -1
@@ -5,7 +5,7 @@#elif defined(DC_SHA1_SUBMODULE )#include "sha1collisiondetection/lib/sha1.h" #else -#include "sha1dc/sha1.h" +#include "sha/sha1dc/sha1.h" #endif #ifdef DC_SHA1_EXTERNAL
sha/sha1ppc/sha1.c renamed from ppc/sha1.c +0 -0
sha/sha1ppc/sha1.h renamed from ppc/sha1.h +0 -0
sha/sha1ppc/sha1ppc.S renamed from ppc/sha1ppc.S +0 -0
sha/sha256/gcrypt.h renamed from sha256/gcrypt.h +0 -0
sha/sha256/sha256.c renamed from sha256/block/sha256.c +0 -0
sha/sha256/sha256.h renamed from sha256/block/sha256.h +0 -0
Signature · 1 line
2.20.1
PATCH 2/6 Add an implementation of the SHA-512 hash algorithm
In reply to Michael Clark- Add SHA-512 hash algorithm implementation derived from the SHA-256 implementation translated to 64-bit and 80 rounds.
- Add configuration machinery to select builtin impl or gcrypt.
- Add sha512-224, sha512-256 and sha512 commands to test-tool.
- Add sha512 hash tests to t/t0015-hash.sh
--- Makefile | 14 +++ hash.h | 46 ++++++- sha/sha512/gcrypt.h | 43 +++++++ sha/sha512/sha512.c | 206 +++++++++++++++++++++++++++++++ sha/sha512/sha512.h | 31 +++++ sha1-file.c | 268 +++++++++++++++++++++++++++++++++++++++++ t/helper/test-sha512.c | 17 +++ t/helper/test-tool.c | 3 + t/helper/test-tool.h | 3 + t/t0015-hash.sh | 80 ++++++++++++ 10 files changed, 707 insertions(+), 4 deletions(-) create mode 100644 sha/sha512/gcrypt.h create mode 100644 sha/sha512/sha512.c create mode 100644 sha/sha512/sha512.h create mode 100644 t/helper/test-sha512.c
Makefile +14 -0
@@ -183,6 +183,8 @@ all:: # # Define BLK_SHA256 to use the built-in SHA-256 routines. # +# Define BLK_SHA512 to use the built-in SHA-512 routines. +# # Define GCRYPT_SHA256 to use the SHA-256 routines in libgcrypt. # # Define OPENSSL_SHA256 to use the SHA-256 routines in OpenSSL.
@@ -739,6 +741,7 @@ TEST_BUILTINS_OBJS += test-serve-v2.o TEST_BUILTINS_OBJS += test-sha1.o TEST_BUILTINS_OBJS += test-sha1-array.o TEST_BUILTINS_OBJS += test-sha256.o +TEST_BUILTINS_OBJS += test-sha512.o TEST_BUILTINS_OBJS += test-sigchain.o TEST_BUILTINS_OBJS += test-strcmp-offset.o TEST_BUILTINS_OBJS += test-string-list.o
@@ -1712,6 +1715,14 @@ else endif endif +ifdef GCRYPT_SHA512 + BASIC_CFLAGS += -DSHA512_GCRYPT + EXTLIBS += -lgcrypt +else + LIB_OBJS += sha/sha512/sha512.o + BASIC_CFLAGS += -DSHA512_BLK +endif + ifdef SHA1_MAX_BLOCK_SIZE LIB_OBJS += compat/sha1-chunked.o BASIC_CFLAGS += -DSHA1_MAX_BLOCK_SIZE="$(SHA1_MAX_BLOCK_SIZE)"
@@ -2784,6 +2795,9 @@ EXCEPT_HDRS := $(GEN_HDRS) compat/% xdiff/% ifndef GCRYPT_SHA256 EXCEPT_HDRS += sha/sha256/gcrypt.h endif +ifndef GCRYPT_SHA512 + EXCEPT_HDRS += sha/sha512/gcrypt.h +endif CHK_HDRS = $(filter-out $(EXCEPT_HDRS),$(LIB_H)) HCO = $(patsubst %.h,%.hco,$(CHK_HDRS)) HCC = $(HCO:hco=hcc)
hash.h +42 -4
78 lines not on this page · raw message
sha/sha512/gcrypt.h new file +43 -0
43 lines not on this page · raw message
sha/sha512/sha512.c new file +206 -0
206 lines not on this page · raw message
sha/sha512/sha512.h new file +31 -0
@@ -0,0 +1,31 @@ +#ifndef SHA512_BLOCK_SHA512_H +#define SHA512_BLOCK_SHA512_H + +#define blk_SHA512_BLKSIZE 128 +#define blk_SHA512_224_HASHSIZE 28 +#define blk_SHA512_256_HASHSIZE 32 +#define blk_SHA512_HASHSIZE 64 + +struct blk_SHA512_CTX { +uint64_t state[8 ]; +uint64_t size; +uint64_t digestlen; +uint8_t buf[blk_SHA512_BLKSIZE]; +}; + +typedef struct blk_SHA512_CTX blk_SHA512_CTX ; + +void blk_SHA512_Init (blk_SHA512_CTX *ctx); +void blk_SHA512_224_Init (blk_SHA512_CTX *ctx); +void blk_SHA512_256_Init (blk_SHA512_CTX *ctx); +void blk_SHA512_Update (blk_SHA512_CTX *ctx,const void *data,size_t len); +void blk_SHA512_Final (unsigned char *digest,blk_SHA512_CTX *ctx); + +#define platform_SHA512_CTX blk_SHA512_CTX +#define platform_SHA512_Init blk_SHA512_Init +#define platform_SHA512_224_Init blk_SHA512_224_Init +#define platform_SHA512_256_Init blk_SHA512_256_Init +#define platform_SHA512_Update blk_SHA512_Update +#define platform_SHA512_Final blk_SHA512_Final + +#endif
sha1-file.c +268 -0
298 lines not on this page · raw message
t/helper/test-sha512.c new file +17 -0
@@ -0,0 +1,17 @@ +#include "test-tool.h" +#include "cache.h" + +int cmd__sha512 (int ac,const char **av) +{ +return cmd_hash_impl (ac, av,GIT_HASH_SHA512 ); +} + +int cmd__sha512_224 (int ac,const char **av) +{ +return cmd_hash_impl (ac, av,GIT_HASH_SHA512_224 ); +} + +int cmd__sha512_256 (int ac,const char **av) +{ +return cmd_hash_impl (ac, av,GIT_HASH_SHA512_256 ); +}
t/helper/test-tool.c +3 -0
@@ -57,6 +57,9 @@ static struct test_cmd cmds[] = { { "sha1", cmd__sha1 }, { "sha1-array", cmd__sha1_array }, { "sha256", cmd__sha256 }, + { "sha512", cmd__sha512 }, + { "sha512-224", cmd__sha512_224 }, + { "sha512-256", cmd__sha512_256 }, { "sigchain", cmd__sigchain }, { "strcmp-offset", cmd__strcmp_offset }, { "string-list", cmd__string_list },
t/helper/test-tool.h +3 -0
@@ -47,6 +47,9 @@ int cmd__serve_v2(int argc, const char **argv);int cmd__sha1 (int argc,const char **argv);int cmd__sha1_array (int argc,const char **argv);int cmd__sha256 (int argc,const char **argv); +int cmd__sha512 (int argc,const char **argv); +int cmd__sha512_224 (int argc,const char **argv); +int cmd__sha512_256 (int argc,const char **argv);int cmd__sigchain (int argc,const char **argv);int cmd__strcmp_offset (int argc,const char **argv);int cmd__string_list (int argc,const char **argv);
t/t0015-hash.sh +80 -0
84 lines not on this page · raw message
Signature · 1 line
2.20.1
PATCH 3/6 Add an implementation of the SHA-3 hash algorithm
In reply to Michael Clark- Add SHA3-224, SHA3-256, SHA3-384 and SHA3-512 hash algorithms derived from the Keccak SHA-3 submission to NIST (Round 3), 2011 by Guido Bertoni, Joan Daemen, Michaël Peeters and Gilles Van Assche
- Add configuration machinery to select builtin impl or gcrypt.
- Add sha3-224, sha3-256, sha3-384 and sha3-512 commands to test-tool.
- Add sha3 hash tests to t/t0015-hash.sh
--- Makefile | 9 ++ hash.h | 49 +++++++- sha/sha3/gcrypt.h | 53 +++++++++ sha/sha3/sha3.c | 271 +++++++++++++++++++++++++++++++++++++++++++ sha/sha3/sha3.h | 34 ++++++ t/helper/test-sha3.c | 22 ++++ t/helper/test-tool.c | 4 + t/helper/test-tool.h | 4 + t/t0015-hash.sh | 108 +++++++++++++++++ 9 files changed, 553 insertions(+), 1 deletion(-) create mode 100644 sha/sha3/gcrypt.h create mode 100644 sha/sha3/sha3.c create mode 100644 sha/sha3/sha3.h create mode 100644 t/helper/test-sha3.c
Makefile +9 -0
@@ -742,6 +742,7 @@ TEST_BUILTINS_OBJS += test-sha1.o TEST_BUILTINS_OBJS += test-sha1-array.o TEST_BUILTINS_OBJS += test-sha256.o TEST_BUILTINS_OBJS += test-sha512.o +TEST_BUILTINS_OBJS += test-sha3.o TEST_BUILTINS_OBJS += test-sigchain.o TEST_BUILTINS_OBJS += test-strcmp-offset.o TEST_BUILTINS_OBJS += test-string-list.o
@@ -1723,6 +1724,14 @@ else BASIC_CFLAGS += -DSHA512_BLK endif +ifdef GCRYPT_SHA3 + BASIC_CFLAGS += -DSHA3_GCRYPT + EXTLIBS += -lgcrypt +else + LIB_OBJS += sha/sha3/sha3.o + BASIC_CFLAGS += -DSHA3_BLK +endif + ifdef SHA1_MAX_BLOCK_SIZE LIB_OBJS += compat/sha1-chunked.o BASIC_CFLAGS += -DSHA1_MAX_BLOCK_SIZE="$(SHA1_MAX_BLOCK_SIZE)"
hash.h +48 -1
80 lines not on this page · raw message
sha/sha3/gcrypt.h new file +53 -0
53 lines not on this page · raw message
sha/sha3/sha3.c new file +271 -0
271 lines not on this page · raw message
sha/sha3/sha3.h new file +34 -0
@@ -0,0 +1,34 @@ +#ifndef SHA3_BLOCK_H +#define SHA3_BLOCK_H + +#define blk_SHA3_224_hash_size 28 +#define blk_SHA3_256_hash_size 32 +#define blk_SHA3_384_hash_size 48 +#define blk_SHA3_512_hash_size 64 +#define blk_SHA3_max_permutation_size 200 + +typedef struct blk_SHA3_CTX +{ +uint64_t state[blk_SHA3_max_permutation_size]; +uint64_t size; +uint64_t block_size; +uint8_t buf[blk_SHA3_max_permutation_size]; +}blk_SHA3_CTX ; + +void blk_SHA3_224_Init (blk_SHA3_CTX * ctx); +void blk_SHA3_256_Init (blk_SHA3_CTX * ctx); +void blk_SHA3_384_Init (blk_SHA3_CTX * ctx); +void blk_SHA3_512_Init (blk_SHA3_CTX * ctx); +void blk_SHA3_Update (blk_SHA3_CTX * ctx,const void *data,size_t len); +void blk_SHA3_Final (unsigned char * digest,blk_SHA3_CTX * ctx); + +#define platform_SHA3_CTX blk_SHA3_CTX +#define platform_SHA3_Init blk_SHA3_Init +#define platform_SHA3_224_Init blk_SHA3_224_Init +#define platform_SHA3_256_Init blk_SHA3_256_Init +#define platform_SHA3_384_Init blk_SHA3_384_Init +#define platform_SHA3_512_Init blk_SHA3_512_Init +#define platform_SHA3_Update blk_SHA3_Update +#define platform_SHA3_Final blk_SHA3_Final + +#endif
t/helper/test-sha3.c new file +22 -0
@@ -0,0 +1,22 @@ +#include "test-tool.h" +#include "cache.h" + +int cmd__sha3_224 (int ac,const char **av) +{ +return cmd_hash_impl (ac, av,GIT_HASH_SHA3_224 ); +} + +int cmd__sha3_256 (int ac,const char **av) +{ +return cmd_hash_impl (ac, av,GIT_HASH_SHA3_256 ); +} + +int cmd__sha3_384 (int ac,const char **av) +{ +return cmd_hash_impl (ac, av,GIT_HASH_SHA3_384 ); +} + +int cmd__sha3_512 (int ac,const char **av) +{ +return cmd_hash_impl (ac, av,GIT_HASH_SHA3_512 ); +}
t/helper/test-tool.c +4 -0
@@ -60,6 +60,10 @@ static struct test_cmd cmds[] = { { "sha512", cmd__sha512 }, { "sha512-224", cmd__sha512_224 }, { "sha512-256", cmd__sha512_256 }, + { "sha3-224", cmd__sha3_224 }, + { "sha3-256", cmd__sha3_256 }, + { "sha3-384", cmd__sha3_384 }, + { "sha3-512", cmd__sha3_512 }, { "sigchain", cmd__sigchain }, { "strcmp-offset", cmd__strcmp_offset }, { "string-list", cmd__string_list },
t/helper/test-tool.h +4 -0
@@ -50,6 +50,10 @@ int cmd__sha256(int argc, const char **argv);int cmd__sha512 (int argc,const char **argv);int cmd__sha512_224 (int argc,const char **argv);int cmd__sha512_256 (int argc,const char **argv); +int cmd__sha3_224 (int argc,const char **argv); +int cmd__sha3_256 (int argc,const char **argv); +int cmd__sha3_384 (int argc,const char **argv); +int cmd__sha3_512 (int argc,const char **argv);int cmd__sigchain (int argc,const char **argv);int cmd__strcmp_offset (int argc,const char **argv);int cmd__string_list (int argc,const char **argv);
t/t0015-hash.sh +108 -0
112 lines not on this page · raw message
Signature · 1 line
2.20.1
PATCH 4/6 Add an implementation of the SHA224 truncated hash algorithm
In reply to Michael Clark- Update existing SHA256 implementation adding initialization vector for SHA224 and add algorithm to list of hash algos.
- Add sha224 commands to test-tool.
- Add sha224 hash tests to t/t0015-hash.sh
--- hash.h | 34 +++++++++++++++++++++++++++------- sha/sha256/gcrypt.h | 15 ++++++++++++++- sha/sha256/sha256.c | 18 +++++++++++++++++- sha/sha256/sha256.h | 9 +++++++++ sha1-file.c | 41 +++++++++++++++++++++++++++++++++++++++++ t/helper/test-sha256.c | 5 +++++ t/helper/test-tool.c | 1 + t/helper/test-tool.h | 1 + t/t0015-hash.sh | 26 ++++++++++++++++++++++++++ 9 files changed, 141 insertions(+), 9 deletions(-)
hash.h +27 -7
@@ -54,6 +54,18 @@#define git_SHA1_Update platform_SHA1_Update#define git_SHA1_Final platform_SHA1_Final +#ifndef platform_SHA224_CTX +#define platform_SHA224_CTX SHA224_CTX +#define platform_SHA224_Init SHA224_Init +#define platform_SHA224_Update SHA224_Update +#define platform_SHA224_Final SHA224_Final +#endif + +#define git_SHA224_CTX platform_SHA224_CTX +#define git_SHA224_Init platform_SHA224_Init +#define git_SHA224_Update platform_SHA224_Update +#define git_SHA224_Final platform_SHA224_Final +#ifndef platform_SHA256_CTX#define platform_SHA256_CTX SHA256_CTX#define platform_SHA256_Init SHA256_Init
@@ -101,20 +113,22 @@#define GIT_HASH_SHA1 1/* SHA-256 */ #define GIT_HASH_SHA256 2 +/* SHA-224 */ +#define GIT_HASH_SHA224 3/* SHA-512 */ -#define GIT_HASH_SHA512 3 +#define GIT_HASH_SHA512 4/* SHA-512-224 */ -#define GIT_HASH_SHA512_224 4 +#define GIT_HASH_SHA512_224 5/* SHA-512-256 */ -#define GIT_HASH_SHA512_256 5 +#define GIT_HASH_SHA512_256 6/* SHA-3-224 */ -#define GIT_HASH_SHA3_224 6 +#define GIT_HASH_SHA3_224 7/* SHA-3-256 */ -#define GIT_HASH_SHA3_256 7 +#define GIT_HASH_SHA3_256 8/* SHA-3-384 */ -#define GIT_HASH_SHA3_384 8 +#define GIT_HASH_SHA3_384 9/* SHA-3-512 */ -#define GIT_HASH_SHA3_512 9 +#define GIT_HASH_SHA3_512 10/* Number of algorithms supported (including unknown). */ #define GIT_HASH_NALGOS (GIT_HASH_SHA3_512 + 1)
@@ -188,6 +202,12 @@ static inline int hash_algo_by_ptr(const struct git_hash_algo *p)/* The block size of SHA-1. */ #define GIT_SHA1_BLKSZ 64 +/* The length in bytes and in hex digits of an object name (SHA-224 value). */ +#define GIT_SHA224_RAWSZ 28 +#define GIT_SHA224_HEXSZ (2 * GIT_SHA224_RAWSZ) +/* The block size of SHA-224. */ +#define GIT_SHA224_BLKSZ 64 +/* The length in bytes and in hex digits of an object name (SHA-256 value). */ #define GIT_SHA256_RAWSZ 32#define GIT_SHA256_HEXSZ (2 * GIT_SHA256_RAWSZ)
sha/sha256/gcrypt.h +14 -1
@@ -3,10 +3,16 @@#include <gcrypt.h> +#define SHA224_DIGEST_SIZE 28#define SHA256_DIGEST_SIZE 32typedef gcry_md_hd_t gcrypt_SHA256_CTX ; +inline void gcrypt_SHA224_Init (gcrypt_SHA256_CTX *ctx) +{ +gcry_md_open (ctx,GCRY_MD_SHA224 ,0 ); +} +inline void gcrypt_SHA256_Init (gcrypt_SHA256_CTX *ctx) {gcry_md_open (ctx,GCRY_MD_SHA256 ,0 );
@@ -19,7 +25,9 @@ inline void gcrypt_SHA256_Update(gcrypt_SHA256_CTX *ctx, const void *data, size_inline void gcrypt_SHA256_Final (unsigned char *digest,gcrypt_SHA256_CTX *ctx) { -memcpy (digest,gcry_md_read (*ctx,GCRY_MD_SHA256 ),SHA256_DIGEST_SIZE ); +int algo =gcry_md_get_algo (ctx); +unsigned int dlen =gcry_md_get_algo_dlen (algo); +memcpy (digest,gcry_md_read (*ctx, algo), dlen); }#define platform_SHA256_CTX gcrypt_SHA256_CTX
@@ -27,4 +35,9 @@ inline void gcrypt_SHA256_Final(unsigned char *digest, gcrypt_SHA256_CTX *ctx)#define platform_SHA256_Update gcrypt_SHA256_Update#define platform_SHA256_Final gcrypt_SHA256_Final +#define platform_SHA224_CTX gcrypt_SHA256_CTX +#define platform_SHA224_Init gcrypt_SHA224_Init +#define platform_SHA224_Update gcrypt_SHA256_Update +#define platform_SHA224_Final gcrypt_SHA256_Final +#endif
sha/sha256/sha256.c +17 -1
@@ -6,8 +6,24 @@#define BLKSIZE blk_SHA256_BLKSIZE +void blk_SHA224_Init (blk_SHA256_CTX *ctx) +{ + ctx->digestlen = blk_SHA224_HASHSIZE; + ctx->offset =0 ; + ctx->size =0 ; + ctx->state[0 ] =0xc1059ed8ul ; + ctx->state[1 ] =0x367cd507ul ; + ctx->state[2 ] =0x3070dd17ul ; + ctx->state[3 ] =0xf70e5939ul ; + ctx->state[4 ] =0xffc00b31ul ; + ctx->state[5 ] =0x68581511ul ; + ctx->state[6 ] =0x64f98fa7ul ; + ctx->state[7 ] =0xbefa4fa4ul ; +} +void blk_SHA256_Init (blk_SHA256_CTX *ctx) { + ctx->digestlen = blk_SHA256_HASHSIZE; ctx->offset =0 ; ctx->size =0 ; ctx->state[0 ] =0x6a09e667ul ;
@@ -191,6 +207,6 @@ void blk_SHA256_Final(unsigned char *digest, blk_SHA256_CTX *ctx)blk_SHA256_Update (ctx, padlen,8 );/* copy output */ -for (i =0 ; i <8 ; i++, digest +=sizeof (uint32_t )) +for (i =0 ; i < (ctx->digestlen >>2 ); i++, digest +=sizeof (uint32_t ))put_be32 (digest, ctx->state[i]); }
sha/sha256/sha256.h +9 -0
@@ -2,16 +2,20 @@#define SHA256_BLOCK_SHA256_H #define blk_SHA256_BLKSIZE 64 +#define blk_SHA224_HASHSIZE 28 +#define blk_SHA256_HASHSIZE 32struct blk_SHA256_CTX {uint32_t state[8 ];uint64_t size;uint32_t offset; +uint32_t digestlen;uint8_t buf[blk_SHA256_BLKSIZE]; };typedef struct blk_SHA256_CTX blk_SHA256_CTX ; +void blk_SHA224_Init (blk_SHA256_CTX *ctx);void blk_SHA256_Init (blk_SHA256_CTX *ctx);void blk_SHA256_Update (blk_SHA256_CTX *ctx,const void *data,size_t len);void blk_SHA256_Final (unsigned char *digest,blk_SHA256_CTX *ctx);
@@ -21,4 +25,9 @@ void blk_SHA256_Final(unsigned char *digest, blk_SHA256_CTX *ctx);#define platform_SHA256_Update blk_SHA256_Update#define platform_SHA256_Final blk_SHA256_Final +#define platform_SHA224_CTX blk_SHA256_CTX +#define platform_SHA224_Init blk_SHA224_Init +#define platform_SHA224_Update blk_SHA256_Update +#define platform_SHA224_Final blk_SHA256_Final +#endif
sha1-file.c +41 -0
71 lines not on this page · raw message
t/helper/test-sha256.c +5 -0
@@ -1,6 +1,11 @@#include "test-tool.h" #include "cache.h" +int cmd__sha224 (int ac,const char **av) +{ +return cmd_hash_impl (ac, av,GIT_HASH_SHA224 ); +} +int cmd__sha256 (int ac,const char **av) {return cmd_hash_impl (ac, av,GIT_HASH_SHA256 );
t/helper/test-tool.c +1 -0
@@ -56,6 +56,7 @@ static struct test_cmd cmds[] = { { "serve-v2", cmd__serve_v2 }, { "sha1", cmd__sha1 }, { "sha1-array", cmd__sha1_array }, + { "sha224", cmd__sha224 }, { "sha256", cmd__sha256 }, { "sha512", cmd__sha512 }, { "sha512-224", cmd__sha512_224 },
t/helper/test-tool.h +1 -0
@@ -46,6 +46,7 @@ int cmd__scrap_cache_tree(int argc, const char **argv);int cmd__serve_v2 (int argc,const char **argv);int cmd__sha1 (int argc,const char **argv);int cmd__sha1_array (int argc,const char **argv); +int cmd__sha224 (int argc,const char **argv);int cmd__sha256 (int argc,const char **argv);int cmd__sha512 (int argc,const char **argv);int cmd__sha512_224 (int argc,const char **argv);
t/t0015-hash.sh +26 -0
@@ -52,6 +52,32 @@ test_expect_success 'test basic SHA-256 hash values' ' grep 6ef19b41225c5369f1c104d45d8d85efa9b057b53b14b4b9b939dd74decc5321 actual ' +test_expect_success 'test basic SHA-224 hash values' ' + test-tool sha224 </dev/null >actual && + grep d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f actual && + printf "a" | test-tool sha224 >actual && + grep abd37534c7d9a2efb9465de931cd7055ffdb8879563ae98078d6d6d5 actual && + printf "abc" | test-tool sha224 >actual && + grep 23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7 actual && + printf "message digest" | test-tool sha224 >actual && + grep 2cb21c83ae2f004de7e81c3c7019cbcb65b71ab656b22d6d0c39b8eb actual && + printf "abcdefghijklmnopqrstuvwxyz" | test-tool sha224 >actual && + grep 45a5f72c39c5cff2522eb3429799e49e5f44b356ef926bcf390dccc2 actual && + # Try to exercise the chunking code by turning autoflush on. + perl -e "$| = 1; print q{aaaaaaaaaa} for 1..100000;" | \ + test-tool sha224 >actual && + grep 20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67 actual && + perl -e "$| = 1; print q{abcdefghijklmnopqrstuvwxyz} for 1..100000;" | \ + test-tool sha224 >actual && + grep 22c732697633e465ba836e11c7829ee185a72b53fbf35fde511bfbcd actual && + printf "blob 0\0" | test-tool sha224 >actual && + grep fcdd1b166dde803306a44852e0b7dcdf73bad0dec56f001feb39ea89 actual && + printf "blob 3\0abc" | test-tool sha224 >actual && + grep 22d78642a42d232e5b21911de160bed5b033badbecb8fc8b693e6ffe actual && + printf "tree 0\0" | test-tool sha224 >actual && + grep c82429250d411b4991a3de9b42f500908a4a242a8d3b3485e5e3bf71 actual +' + test_expect_success 'test basic SHA-512/224 hash values' ' test-tool sha512-224 </dev/null >actual && grep 6ed0dd02806fa89e25de060c19d3ac86cabb87d6a0ddd05c333b84f4 actual &&
Signature · 1 line
2.20.1
PATCH 5/6 Add OpenSSL EVP interface for SHA-3 and SHA-512 algorithms
In reply to Michael Clark- Add OpenSSL EVP context wrapper and interface.
- Add configuration machinery to select SHA-3 and SHA-512 impls from OpenSSL using the EVP interface.
- Use make OPENSSL_EVP=1 to build using the OpenSSL EVP impls for all SHA-3 and SHA-512 algos.
--- Makefile | 9 +++- hash.h | 6 +++ sha/sha_evp/sha_evp.c | 99 +++++++++++++++++++++++++++++++++++++++++++ sha/sha_evp/sha_evp.h | 51 ++++++++++++++++++++++ 4 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 sha/sha_evp/sha_evp.c create mode 100644 sha/sha_evp/sha_evp.h
Makefile +8 -1
@@ -189,6 +189,8 @@ all:: # # Define OPENSSL_SHA256 to use the SHA-256 routines in OpenSSL. # +# Define OPENSSL_EVP to use the SHA-3 and SHA-512 routines in OpenSSL. +# # Define NEEDS_CRYPTO_WITH_SSL if you need -lcrypto when using -lssl (Darwin). # # Define NEEDS_SSL_WITH_CRYPTO if you need -lssl when using -lcrypto (Darwin).
@@ -1716,6 +1718,11 @@ else endif endif +ifdef OPENSSL_EVP + EXTLIBS += $(LIB_4_CRYPTO) + BASIC_CFLAGS += -DSHA_EVP_OPENSSL + LIB_OBJS += sha/sha_evp/sha_evp.o +else ifdef GCRYPT_SHA512 BASIC_CFLAGS += -DSHA512_GCRYPT EXTLIBS += -lgcrypt
@@ -1723,7 +1730,6 @@ else LIB_OBJS += sha/sha512/sha512.o BASIC_CFLAGS += -DSHA512_BLK endif - ifdef GCRYPT_SHA3 BASIC_CFLAGS += -DSHA3_GCRYPT EXTLIBS += -lgcrypt
@@ -1731,6 +1737,7 @@ else LIB_OBJS += sha/sha3/sha3.o BASIC_CFLAGS += -DSHA3_BLK endif +endif ifdef SHA1_MAX_BLOCK_SIZE LIB_OBJS += compat/sha1-chunked.o
hash.h +6 -0
@@ -25,12 +25,18 @@#if defined(SHA512_GCRYPT )#include "sha/sha512/gcrypt.h" +#elif defined(SHA_EVP_OPENSSL ) +#include <openssl/evp.h> +#include "sha/sha_evp/sha_evp.h" #else #include "sha/sha512/sha512.h" #endif #if defined(SHA3_GCRYPT )#include "sha/sha3/gcrypt.h" +#elif defined(SHA_EVP_OPENSSL ) +#include <openssl/evp.h> +#include "sha/sha_evp/sha_evp.h" #else #include "sha/sha3/sha3.h" #endif
sha/sha_evp/sha_evp.c new file +99 -0
99 lines not on this page · raw message
sha/sha_evp/sha_evp.h new file +51 -0
52 lines not on this page · raw message
Signature · 1 line
2.20.1
PATCH 6/6 Add sha/README.md with table of SHA algorithm details
In reply to Michael Clark- Add table showing algorith name, mnemonic, type-code, wdith, and library implementation (builtin, gcrypt, OpenSSL, EVP).
--- sha/README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 sha/README.md
sha/README.md new file +23 -0
@@ -0,0 +1,23 @@ +# SHA algorithms + +### Algorithms + +Table showing details of the SHA algorithms: + +| algorithm | mnemonic | type-code | width | extension | builtin | gcrypt | OpenSSL| EVP | +|:---------------|:---------|:-------------|------:|----------:|:-------------|:-------|:-------|:-------| +| `sha1` | `sha1` | `0x73686131` | 160 | yes | `sha/sha1` | yes | yes | - | +| `sha256` | `s256` | `0x73323536` | 256 | yes | `sha/sha256` | yes | yes | - | +| `sha224` | `s224` | `0x73323234` | 224 | no | `sha/sha256` | yes | yes | - | +| `sha512` | `s512` | `0x73353132` | 512 | yes | `sha/sha512` | yes | yes | yes | +| `sha512/224` | `s226` | `0x73323236` | 224 | no | `sha/sha512` | yes | yes | yes | +| `sha512/256` | `s228` | `0x73323238` | 256 | no | `sha/sha512` | yes | yes | yes | +| `sha3-224` | `s388` | `0x73333838` | 224 | no | `sha/sha3` | yes | - | yes | +| `sha3-256` | `s398` | `0x73333938` | 256 | no | `sha/sha3` | yes | - | yes | +| `sha3-384` | `s3a8` | `0x73336138` | 384 | no | `sha/sha3` | yes | - | yes | +| `sha3-512` | `s3b8` | `0x73336238` | 512 | no | `sha/sha3` | yes | - | yes | + +#### Notes + +- The _'extension'_ column refers to whether the algorithm is vulnerable to the + [length extension attack](https://en.wikipedia.org/wiki/Length_extension_attack).
Signature · 1 line
2.20.1