Subject: [PATCH v2 0/4] hash.h: support choosing a separate SHA-1 for non-cryptographic uses

From: Taylor Blau <[email-masked]>
Date: Thu, 5 Sep 2024 11:11:36 -0400

This series adds a build-time knob to allow selecting an alternative
SHA-1 implementation for non-cryptographic hashing within Git, starting
with the `hashwrite()` family of functions.

This version is a small reroll from the original round which addresses a
handful of suggestions made during review, and also fixes compiling with
OPENSSL_SHA1_FAST with older versions of OpenSSL (having
OPENSSL_API_LEVEL < 3).

Otherwise, the series is unchanged from the first round. But as always,
a range-diff is included below for convenience.

Thanks in advance for your review!

Taylor Blau (4):
  sha1: do not redefine `platform_SHA_CTX` and friends
  hash.h: scaffolding for _fast hashing variants
  Makefile: allow specifying a SHA-1 for non-cryptographic uses
  csum-file.c: use fast SHA-1 implementation when available

 Makefile          | 25 ++++++++++++++++
 block-sha1/sha1.h |  2 ++
 csum-file.c       | 18 ++++++------
 hash.h            | 72 +++++++++++++++++++++++++++++++++++++++++++++++
 object-file.c     | 42 +++++++++++++++++++++++++++
 sha1/openssl.h    |  2 ++
 sha1dc_git.h      |  3 ++
 7 files changed, 155 insertions(+), 9 deletions(-)

Range-diff against v1:
1:  e7cd23bf4c = 1:  e7cd23bf4c sha1: do not redefine `platform_SHA_CTX` and friends
2:  6ac6f934c3 ! 2:  3b5f21e4a6 hash.h: scaffolding for _fast hashing variants
    @@ hash.h
      #endif

     +#ifndef platform_SHA_CTX_fast
    -+#define platform_SHA_CTX_fast	  platform_SHA_CTX
    -+#define platform_SHA1_Init_fast	  platform_SHA1_Init
    -+#define platform_SHA1_Update_fast platform_SHA1_Update
    -+#define platform_SHA1_Final_fast  platform_SHA1_Final
    -+#ifdef platform_SHA1_Clone
    -+#define platform_SHA1_Clone_fast  platform_SHA1_Clone
    -+#endif
    ++#  define platform_SHA_CTX_fast     platform_SHA_CTX
    ++#  define platform_SHA1_Init_fast   platform_SHA1_Init
    ++#  define platform_SHA1_Update_fast platform_SHA1_Update
    ++#  define platform_SHA1_Final_fast  platform_SHA1_Final
    ++#  ifdef platform_SHA1_Clone
    ++#    define platform_SHA1_Clone_fast platform_SHA1_Clone
    ++#  endif
     +#endif
     +
      #define git_SHA_CTX		platform_SHA_CTX
    @@ hash.h
      #define git_SHA1_Clone	platform_SHA1_Clone
      #endif
     +#ifdef platform_SHA1_Clone_fast
    -+#define git_SHA1_Clone_fast	platform_SHA1_Clone_fast
    ++#  define git_SHA1_Clone_fast platform_SHA1_Clone_fast
     +#endif

      #ifndef platform_SHA256_CTX
    @@ hash.h: struct git_hash_algo {
      	/* The hash finalization function for object IDs. */
      	git_hash_final_oid_fn final_oid_fn;

    -+	/* The fast hash initialization function. */
    ++	/* The fast / non-cryptographic hash initialization function. */
     +	git_hash_init_fn fast_init_fn;
     +
    -+	/* The fast hash context cloning function. */
    ++	/* The fast / non-cryptographic hash context cloning function. */
     +	git_hash_clone_fn fast_clone_fn;
     +
    -+	/* The fast hash update function. */
    ++	/* The fast / non-cryptographic hash update function. */
     +	git_hash_update_fn fast_update_fn;
     +
    -+	/* The fast hash finalization function. */
    ++	/* The fast / non-cryptographic hash finalization function. */
     +	git_hash_final_fn fast_final_fn;
     +
    -+	/* The fast hash finalization function for object IDs. */
    ++	/* The fast / non-cryptographic hash finalization function. */
     +	git_hash_final_oid_fn fast_final_oid_fn;
     +
      	/* The OID of the empty tree. */
3:  682e4c2cc3 ! 3:  02764de139 Makefile: allow specifying a SHA-1 for non-cryptographic uses
    @@ hash.h
      #endif

     +#if defined(SHA1_APPLE_FAST)
    -+#include <CommonCrypto/CommonDigest.h>
    -+#define platform_SHA_CTX_fast CC_SHA1_CTX
    -+#define platform_SHA1_Init_fast CC_SHA1_Init
    -+#define platform_SHA1_Update_fast CC_SHA1_Update
    -+#define platform_SHA1_Final_fast CC_SHA1_Final
    ++#  include <CommonCrypto/CommonDigest.h>
    ++#  define platform_SHA_CTX_fast CC_SHA1_CTX
    ++#  define platform_SHA1_Init_fast CC_SHA1_Init
    ++#  define platform_SHA1_Update_fast CC_SHA1_Update
    ++#  define platform_SHA1_Final_fast CC_SHA1_Final
     +#elif defined(SHA1_OPENSSL_FAST)
     +#  include <openssl/sha.h>
     +#  if defined(OPENSSL_API_LEVEL) && OPENSSL_API_LEVEL >= 3
     +#    define SHA1_NEEDS_CLONE_HELPER_FAST
     +#    include "sha1/openssl.h"
    ++#    define platform_SHA_CTX_fast openssl_SHA1_CTX
    ++#    define platform_SHA1_Init_fast openssl_SHA1_Init
    ++#    define platform_SHA1_Clone_fast openssl_SHA1_Clone
    ++#    define platform_SHA1_Update_fast openssl_SHA1_Update
    ++#    define platform_SHA1_Final_fast openssl_SHA1_Final
    ++#  else
    ++#    define platform_SHA_CTX_fast SHA_CTX
    ++#    define platform_SHA1_Init_fast SHA1_Init
    ++#    define platform_SHA1_Update_fast SHA1_Update
    ++#    define platform_SHA1_Final_fast SHA1_Final
     +#  endif
    -+#  define platform_SHA_CTX_fast openssl_SHA1_CTX
    -+#  define platform_SHA1_Init_fast openssl_SHA1_Init
    -+#  define platform_SHA1_Clone_fast openssl_SHA1_Clone
    -+#  define platform_SHA1_Update_fast openssl_SHA1_Update
    -+#  define platform_SHA1_Final_fast openssl_SHA1_Final
     +#elif defined(SHA1_BLK_FAST)
    -+#include "block-sha1/sha1.h"
    -+#define platform_SHA_CTX_fast blk_SHA_CTX
    -+#define platform_SHA1_Init_fast blk_SHA1_Init
    -+#define platform_SHA1_Update_fast blk_SHA1_Update
    -+#define platform_SHA1_Final_fast blk_SHA1_Final
    ++#  include "block-sha1/sha1.h"
    ++#  define platform_SHA_CTX_fast blk_SHA_CTX
    ++#  define platform_SHA1_Init_fast blk_SHA1_Init
    ++#  define platform_SHA1_Update_fast blk_SHA1_Update
    ++#  define platform_SHA1_Final_fast blk_SHA1_Final
     +#endif
     +
      #if defined(SHA256_NETTLE)
4:  e8f5cbd280 = 4:  311fcc9596 csum-file.c: use fast SHA-1 implementation when available
--
2.46.0.426.g82754d92509.dirty