Skip to content

Commit b5d2d8e

Browse files
committed
Merge branch 'ad/sha1-update-chunked' into maint
Apple's common crypto implementation of SHA1_Update() does not take more than 4GB at a time, and we now have a compile-time workaround for it. * ad/sha1-update-chunked: sha1: allow limiting the size of the data passed to SHA1_Update() sha1: provide another level of indirection for the SHA-1 functions
2 parents e6ed5a4 + 001fd7a commit b5d2d8e

File tree

7 files changed

+69
-13
lines changed

7 files changed

+69
-13
lines changed

Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ all::
144144
# Define PPC_SHA1 environment variable when running make to make use of
145145
# a bundled SHA1 routine optimized for PowerPC.
146146
#
147+
# Define SHA1_MAX_BLOCK_SIZE to limit the amount of data that will be hashed
148+
# in one call to the platform's SHA1_Update(). e.g. APPLE_COMMON_CRYPTO
149+
# wants 'SHA1_MAX_BLOCK_SIZE=1024L*1024L*1024L' defined.
150+
#
147151
# Define NEEDS_CRYPTO_WITH_SSL if you need -lcrypto when using -lssl (Darwin).
148152
#
149153
# Define NEEDS_SSL_WITH_CRYPTO if you need -lssl when using -lcrypto (Darwin).
@@ -1340,6 +1344,11 @@ ifdef NO_POSIX_GOODIES
13401344
BASIC_CFLAGS += -DNO_POSIX_GOODIES
13411345
endif
13421346

1347+
ifdef APPLE_COMMON_CRYPTO
1348+
# Apple CommonCrypto requires chunking
1349+
SHA1_MAX_BLOCK_SIZE = 1024L*1024L*1024L
1350+
endif
1351+
13431352
ifdef BLK_SHA1
13441353
SHA1_HEADER = "block-sha1/sha1.h"
13451354
LIB_OBJS += block-sha1/sha1.o
@@ -1358,6 +1367,10 @@ endif
13581367
endif
13591368
endif
13601369

1370+
ifdef SHA1_MAX_BLOCK_SIZE
1371+
LIB_OBJS += compat/sha1-chunked.o
1372+
BASIC_CFLAGS += -DSHA1_MAX_BLOCK_SIZE="$(SHA1_MAX_BLOCK_SIZE)"
1373+
endif
13611374
ifdef NO_PERL_MAKEMAKER
13621375
export NO_PERL_MAKEMAKER
13631376
endif

block-sha1/sha1.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ void blk_SHA1_Init(blk_SHA_CTX *ctx);
1616
void blk_SHA1_Update(blk_SHA_CTX *ctx, const void *dataIn, unsigned long len);
1717
void blk_SHA1_Final(unsigned char hashout[20], blk_SHA_CTX *ctx);
1818

19-
#define git_SHA_CTX blk_SHA_CTX
20-
#define git_SHA1_Init blk_SHA1_Init
21-
#define git_SHA1_Update blk_SHA1_Update
22-
#define git_SHA1_Final blk_SHA1_Final
19+
#define platform_SHA_CTX blk_SHA_CTX
20+
#define platform_SHA1_Init blk_SHA1_Init
21+
#define platform_SHA1_Update blk_SHA1_Update
22+
#define platform_SHA1_Final blk_SHA1_Final

cache.h

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,29 @@
1111
#include "string-list.h"
1212

1313
#include SHA1_HEADER
14-
#ifndef git_SHA_CTX
15-
#define git_SHA_CTX SHA_CTX
16-
#define git_SHA1_Init SHA1_Init
17-
#define git_SHA1_Update SHA1_Update
18-
#define git_SHA1_Final SHA1_Final
14+
#ifndef platform_SHA_CTX
15+
/*
16+
* platform's underlying implementation of SHA-1; could be OpenSSL,
17+
* blk_SHA, Apple CommonCrypto, etc... Note that including
18+
* SHA1_HEADER may have already defined platform_SHA_CTX for our
19+
* own implementations like block-sha1 and ppc-sha1, so we list
20+
* the default for OpenSSL compatible SHA-1 implementations here.
21+
*/
22+
#define platform_SHA_CTX SHA_CTX
23+
#define platform_SHA1_Init SHA1_Init
24+
#define platform_SHA1_Update SHA1_Update
25+
#define platform_SHA1_Final SHA1_Final
26+
#endif
27+
28+
#define git_SHA_CTX platform_SHA_CTX
29+
#define git_SHA1_Init platform_SHA1_Init
30+
#define git_SHA1_Update platform_SHA1_Update
31+
#define git_SHA1_Final platform_SHA1_Final
32+
33+
#ifdef SHA1_MAX_BLOCK_SIZE
34+
#include "compat/sha1-chunked.h"
35+
#undef git_SHA1_Update
36+
#define git_SHA1_Update git_SHA1_Update_Chunked
1937
#endif
2038

2139
#include <zlib.h>

compat/apple-common-crypto.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
#undef TYPE_BOOL
1717
#endif
1818

19+
#ifndef SHA1_MAX_BLOCK_SIZE
20+
#error Using Apple Common Crypto library requires setting SHA1_MAX_BLOCK_SIZE
21+
#endif
22+
1923
#ifdef APPLE_LION_OR_NEWER
2024
#define git_CC_error_check(pattern, err) \
2125
do { \

compat/sha1-chunked.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "cache.h"
2+
3+
int git_SHA1_Update_Chunked(platform_SHA_CTX *c, const void *data, size_t len)
4+
{
5+
size_t nr;
6+
size_t total = 0;
7+
const char *cdata = (const char*)data;
8+
9+
while (len) {
10+
nr = len;
11+
if (nr > SHA1_MAX_BLOCK_SIZE)
12+
nr = SHA1_MAX_BLOCK_SIZE;
13+
platform_SHA1_Update(c, cdata, nr);
14+
total += nr;
15+
cdata += nr;
16+
len -= nr;
17+
}
18+
return total;
19+
}

compat/sha1-chunked.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
int git_SHA1_Update_Chunked(platform_SHA_CTX *c, const void *data, size_t len);

ppc/sha1.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ int ppc_SHA1_Init(ppc_SHA_CTX *c);
1919
int ppc_SHA1_Update(ppc_SHA_CTX *c, const void *p, unsigned long n);
2020
int ppc_SHA1_Final(unsigned char *hash, ppc_SHA_CTX *c);
2121

22-
#define git_SHA_CTX ppc_SHA_CTX
23-
#define git_SHA1_Init ppc_SHA1_Init
24-
#define git_SHA1_Update ppc_SHA1_Update
25-
#define git_SHA1_Final ppc_SHA1_Final
22+
#define platform_SHA_CTX ppc_SHA_CTX
23+
#define platform_SHA1_Init ppc_SHA1_Init
24+
#define platform_SHA1_Update ppc_SHA1_Update
25+
#define platform_SHA1_Final ppc_SHA1_Final

0 commit comments

Comments
 (0)