Skip to content

Commit 0b30429

Browse files
ttaylorrgitster
authored andcommitted
hash.h: scaffolding for _fast hashing variants
Git's default SHA-1 implementation is collision-detecting, which hardens us against known SHA-1 attacks against Git objects. This makes Git object writes safer at the expense of some speed when hashing through the collision-detecting implementation, which is slower than non-collision detecting alternatives. Prepare for loading a separate "fast" SHA-1 implementation that can be used for non-cryptographic purposes, like computing the checksum of files that use the hashwrite() API. This commit does not actually introduce any new compile-time knobs to control which implementation is used as the fast SHA-1 variant, but does add scaffolding so that the "git_hash_algo" structure has five new function pointers which are "fast" variants of the five existing hashing-related function pointers: - git_hash_init_fn fast_init_fn - git_hash_clone_fn fast_clone_fn - git_hash_update_fn fast_update_fn - git_hash_final_fn fast_final_fn - git_hash_final_oid_fn fast_final_oid_fn The following commit will introduce compile-time knobs to specify which SHA-1 implementation is used for non-cryptographic uses. Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6acf70f commit 0b30429

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

hash.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,32 @@
4444
#define platform_SHA1_Final SHA1_Final
4545
#endif
4646

47+
#ifndef platform_SHA_CTX_fast
48+
# define platform_SHA_CTX_fast platform_SHA_CTX
49+
# define platform_SHA1_Init_fast platform_SHA1_Init
50+
# define platform_SHA1_Update_fast platform_SHA1_Update
51+
# define platform_SHA1_Final_fast platform_SHA1_Final
52+
# ifdef platform_SHA1_Clone
53+
# define platform_SHA1_Clone_fast platform_SHA1_Clone
54+
# endif
55+
#endif
56+
4757
#define git_SHA_CTX platform_SHA_CTX
4858
#define git_SHA1_Init platform_SHA1_Init
4959
#define git_SHA1_Update platform_SHA1_Update
5060
#define git_SHA1_Final platform_SHA1_Final
5161

62+
#define git_SHA_CTX_fast platform_SHA_CTX_fast
63+
#define git_SHA1_Init_fast platform_SHA1_Init_fast
64+
#define git_SHA1_Update_fast platform_SHA1_Update_fast
65+
#define git_SHA1_Final_fast platform_SHA1_Final_fast
66+
5267
#ifdef platform_SHA1_Clone
5368
#define git_SHA1_Clone platform_SHA1_Clone
5469
#endif
70+
#ifdef platform_SHA1_Clone_fast
71+
# define git_SHA1_Clone_fast platform_SHA1_Clone_fast
72+
#endif
5573

5674
#ifndef platform_SHA256_CTX
5775
#define platform_SHA256_CTX SHA256_CTX
@@ -81,6 +99,13 @@ static inline void git_SHA1_Clone(git_SHA_CTX *dst, const git_SHA_CTX *src)
8199
memcpy(dst, src, sizeof(*dst));
82100
}
83101
#endif
102+
#ifndef SHA1_NEEDS_CLONE_HELPER_FAST
103+
static inline void git_SHA1_Clone_fast(git_SHA_CTX_fast *dst,
104+
const git_SHA_CTX_fast *src)
105+
{
106+
memcpy(dst, src, sizeof(*dst));
107+
}
108+
#endif
84109

85110
#ifndef SHA256_NEEDS_CLONE_HELPER
86111
static inline void git_SHA256_Clone(git_SHA256_CTX *dst, const git_SHA256_CTX *src)
@@ -178,6 +203,8 @@ enum get_oid_result {
178203
/* A suitably aligned type for stack allocations of hash contexts. */
179204
union git_hash_ctx {
180205
git_SHA_CTX sha1;
206+
git_SHA_CTX_fast sha1_fast;
207+
181208
git_SHA256_CTX sha256;
182209
};
183210
typedef union git_hash_ctx git_hash_ctx;
@@ -222,6 +249,21 @@ struct git_hash_algo {
222249
/* The hash finalization function for object IDs. */
223250
git_hash_final_oid_fn final_oid_fn;
224251

252+
/* The fast / non-cryptographic hash initialization function. */
253+
git_hash_init_fn fast_init_fn;
254+
255+
/* The fast / non-cryptographic hash context cloning function. */
256+
git_hash_clone_fn fast_clone_fn;
257+
258+
/* The fast / non-cryptographic hash update function. */
259+
git_hash_update_fn fast_update_fn;
260+
261+
/* The fast / non-cryptographic hash finalization function. */
262+
git_hash_final_fn fast_final_fn;
263+
264+
/* The fast / non-cryptographic hash finalization function. */
265+
git_hash_final_oid_fn fast_final_oid_fn;
266+
225267
/* The OID of the empty tree. */
226268
const struct object_id *empty_tree;
227269

object-file.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,33 @@ static void git_hash_sha1_final_oid(struct object_id *oid, git_hash_ctx *ctx)
115115
oid->algo = GIT_HASH_SHA1;
116116
}
117117

118+
static void git_hash_sha1_init_fast(git_hash_ctx *ctx)
119+
{
120+
git_SHA1_Init_fast(&ctx->sha1_fast);
121+
}
122+
123+
static void git_hash_sha1_clone_fast(git_hash_ctx *dst, const git_hash_ctx *src)
124+
{
125+
git_SHA1_Clone_fast(&dst->sha1_fast, &src->sha1_fast);
126+
}
127+
128+
static void git_hash_sha1_update_fast(git_hash_ctx *ctx, const void *data,
129+
size_t len)
130+
{
131+
git_SHA1_Update_fast(&ctx->sha1_fast, data, len);
132+
}
133+
134+
static void git_hash_sha1_final_fast(unsigned char *hash, git_hash_ctx *ctx)
135+
{
136+
git_SHA1_Final_fast(hash, &ctx->sha1_fast);
137+
}
138+
139+
static void git_hash_sha1_final_oid_fast(struct object_id *oid, git_hash_ctx *ctx)
140+
{
141+
git_SHA1_Final_fast(oid->hash, &ctx->sha1_fast);
142+
memset(oid->hash + GIT_SHA1_RAWSZ, 0, GIT_MAX_RAWSZ - GIT_SHA1_RAWSZ);
143+
oid->algo = GIT_HASH_SHA1;
144+
}
118145

119146
static void git_hash_sha256_init(git_hash_ctx *ctx)
120147
{
@@ -189,6 +216,11 @@ const struct git_hash_algo hash_algos[GIT_HASH_NALGOS] = {
189216
.update_fn = git_hash_unknown_update,
190217
.final_fn = git_hash_unknown_final,
191218
.final_oid_fn = git_hash_unknown_final_oid,
219+
.fast_init_fn = git_hash_unknown_init,
220+
.fast_clone_fn = git_hash_unknown_clone,
221+
.fast_update_fn = git_hash_unknown_update,
222+
.fast_final_fn = git_hash_unknown_final,
223+
.fast_final_oid_fn = git_hash_unknown_final_oid,
192224
.empty_tree = NULL,
193225
.empty_blob = NULL,
194226
.null_oid = NULL,
@@ -204,6 +236,11 @@ const struct git_hash_algo hash_algos[GIT_HASH_NALGOS] = {
204236
.update_fn = git_hash_sha1_update,
205237
.final_fn = git_hash_sha1_final,
206238
.final_oid_fn = git_hash_sha1_final_oid,
239+
.fast_init_fn = git_hash_sha1_init_fast,
240+
.fast_clone_fn = git_hash_sha1_clone_fast,
241+
.fast_update_fn = git_hash_sha1_update_fast,
242+
.fast_final_fn = git_hash_sha1_final_fast,
243+
.fast_final_oid_fn = git_hash_sha1_final_oid_fast,
207244
.empty_tree = &empty_tree_oid,
208245
.empty_blob = &empty_blob_oid,
209246
.null_oid = &null_oid_sha1,
@@ -219,6 +256,11 @@ const struct git_hash_algo hash_algos[GIT_HASH_NALGOS] = {
219256
.update_fn = git_hash_sha256_update,
220257
.final_fn = git_hash_sha256_final,
221258
.final_oid_fn = git_hash_sha256_final_oid,
259+
.fast_init_fn = git_hash_sha256_init,
260+
.fast_clone_fn = git_hash_sha256_clone,
261+
.fast_update_fn = git_hash_sha256_update,
262+
.fast_final_fn = git_hash_sha256_final,
263+
.fast_final_oid_fn = git_hash_sha256_final_oid,
222264
.empty_tree = &empty_tree_oid_sha256,
223265
.empty_blob = &empty_blob_oid_sha256,
224266
.null_oid = &null_oid_sha256,

0 commit comments

Comments
 (0)