Commit bd48bf3fb9368541bafda7877cf159f94884c187

Patrick Steinhardt 2019-06-14T14:21:32

hash: introduce source files to break include circles The hash source files have circular include dependencies right now, which shows by our broken generic hash implementation. The "hash.h" header declares two functions and the `git_hash_ctx` typedef before actually including the hash backend header and can only declare the remaining hash functions after the include due to possibly static function declarations inside of the implementation includes. Let's break this cycle and help maintainability by creating a real implementation file for each of the hash implementations. Instead of relying on the exact include order, we now especially avoid the use of `GIT_INLINE` for function declarations.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
diff --git a/cmake/Modules/SelectHashes.cmake b/cmake/Modules/SelectHashes.cmake
index 450e2bd..78ff41f 100644
--- a/cmake/Modules/SelectHashes.cmake
+++ b/cmake/Modules/SelectHashes.cmake
@@ -40,8 +40,10 @@ ELSEIF(SHA1_BACKEND STREQUAL "OpenSSL")
 	ELSE()
 		LIST(APPEND LIBGIT2_PC_REQUIRES "openssl")
 	ENDIF()
+	FILE(GLOB SRC_SHA1 hash/hash_openssl.c)
 ELSEIF(SHA1_BACKEND STREQUAL "CommonCrypto")
 	SET(GIT_SHA1_COMMON_CRYPTO 1)
+	FILE(GLOB SRC_SHA1 hash/hash_common_crypto.c)
 ELSEIF(SHA1_BACKEND STREQUAL "mbedTLS")
 	SET(GIT_SHA1_MBEDTLS 1)
 	FILE(GLOB SRC_SHA1 hash/hash_mbedtls.c)
diff --git a/src/hash.h b/src/hash.h
index 0d2e7e4..1ccf66d 100644
--- a/src/hash.h
+++ b/src/hash.h
@@ -13,8 +13,10 @@
 
 typedef struct git_hash_ctx git_hash_ctx;
 
-int git_hash_ctx_init(git_hash_ctx *ctx);
-void git_hash_ctx_cleanup(git_hash_ctx *ctx);
+typedef struct {
+	void *data;
+	size_t len;
+} git_buf_vec;
 
 #if defined(GIT_SHA1_COLLISIONDETECT)
 # include "hash/hash_collisiondetect.h"
@@ -30,10 +32,10 @@ void git_hash_ctx_cleanup(git_hash_ctx *ctx);
 # include "hash/hash_generic.h"
 #endif
 
-typedef struct {
-	void *data;
-	size_t len;
-} git_buf_vec;
+int git_hash_global_init(void);
+
+int git_hash_ctx_init(git_hash_ctx *ctx);
+void git_hash_ctx_cleanup(git_hash_ctx *ctx);
 
 int git_hash_init(git_hash_ctx *c);
 int git_hash_update(git_hash_ctx *c, const void *data, size_t len);
diff --git a/src/hash/hash_collisiondetect.c b/src/hash/hash_collisiondetect.c
new file mode 100644
index 0000000..baf2789
--- /dev/null
+++ b/src/hash/hash_collisiondetect.c
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#include "hash_collisiondetect.h"
+
+int git_hash_global_init(void)
+{
+	return 0;
+}
+
+int git_hash_ctx_init(git_hash_ctx *ctx)
+{
+	return git_hash_init(ctx);
+}
+
+void git_hash_ctx_cleanup(git_hash_ctx *ctx)
+{
+	GIT_UNUSED(ctx);
+}
+
+int git_hash_init(git_hash_ctx *ctx)
+{
+	assert(ctx);
+	SHA1DCInit(&ctx->c);
+	return 0;
+}
+
+int git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
+{
+	assert(ctx);
+	SHA1DCUpdate(&ctx->c, data, len);
+	return 0;
+}
+
+int git_hash_final(git_oid *out, git_hash_ctx *ctx)
+{
+	assert(ctx);
+	if (SHA1DCFinal(out->id, &ctx->c)) {
+		git_error_set(GIT_ERROR_SHA1, "SHA1 collision attack detected");
+		return -1;
+	}
+
+	return 0;
+}
diff --git a/src/hash/hash_collisiondetect.h b/src/hash/hash_collisiondetect.h
index 7432098..8cba5fe 100644
--- a/src/hash/hash_collisiondetect.h
+++ b/src/hash/hash_collisiondetect.h
@@ -9,43 +9,11 @@
 #define INCLUDE_hash_hash_collisiondetect_h__
 
 #include "hash.h"
+
 #include "sha1dc/sha1.h"
 
 struct git_hash_ctx {
 	SHA1_CTX c;
 };
 
-#define git_hash_ctx_init(ctx) git_hash_init(ctx)
-#define git_hash_ctx_cleanup(ctx)
-
-GIT_INLINE(int) git_hash_global_init(void)
-{
-	return 0;
-}
-
-GIT_INLINE(int) git_hash_init(git_hash_ctx *ctx)
-{
-	assert(ctx);
-	SHA1DCInit(&ctx->c);
-	return 0;
-}
-
-GIT_INLINE(int) git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
-{
-	assert(ctx);
-	SHA1DCUpdate(&ctx->c, data, len);
-	return 0;
-}
-
-GIT_INLINE(int) git_hash_final(git_oid *out, git_hash_ctx *ctx)
-{
-	assert(ctx);
-	if (SHA1DCFinal(out->id, &ctx->c)) {
-		git_error_set(GIT_ERROR_SHA1, "SHA1 collision attack detected");
-		return -1;
-	}
-
-	return 0;
-}
-
 #endif
diff --git a/src/hash/hash_common_crypto.c b/src/hash/hash_common_crypto.c
new file mode 100644
index 0000000..1ad6f4a
--- /dev/null
+++ b/src/hash/hash_common_crypto.c
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#include "hash_common_crypto.h"
+
+#define CC_LONG_MAX ((CC_LONG)-1)
+
+int git_hash_global_init(void)
+{
+	return 0;
+}
+
+int git_hash_ctx_init(git_hash_ctx *ctx)
+{
+	return git_hash_init(ctx);
+}
+
+void git_hash_ctx_cleanup(git_hash_ctx *ctx)
+{
+	GIT_UNUSED(ctx);
+}
+
+int git_hash_init(git_hash_ctx *ctx)
+{
+	assert(ctx);
+	CC_SHA1_Init(&ctx->c);
+	return 0;
+}
+
+int git_hash_update(git_hash_ctx *ctx, const void *_data, size_t len)
+{
+	const unsigned char *data = _data;
+
+	assert(ctx);
+
+	while (len > 0) {
+		CC_LONG chunk = (len > CC_LONG_MAX) ? CC_LONG_MAX : (CC_LONG)len;
+
+		CC_SHA1_Update(&ctx->c, data, chunk);
+
+		data += chunk;
+		len -= chunk;
+	}
+
+	return 0;
+}
+
+int git_hash_final(git_oid *out, git_hash_ctx *ctx)
+{
+	assert(ctx);
+	CC_SHA1_Final(out->id, &ctx->c);
+	return 0;
+}
diff --git a/src/hash/hash_common_crypto.h b/src/hash/hash_common_crypto.h
index ce352a6..55bec2c 100644
--- a/src/hash/hash_common_crypto.h
+++ b/src/hash/hash_common_crypto.h
@@ -16,46 +16,4 @@ struct git_hash_ctx {
 	CC_SHA1_CTX c;
 };
 
-#define CC_LONG_MAX ((CC_LONG)-1)
-
-#define git_hash_ctx_init(ctx) git_hash_init(ctx)
-#define git_hash_ctx_cleanup(ctx)
-
-GIT_INLINE(int) git_hash_global_init(void)
-{
-	return 0;
-}
-
-GIT_INLINE(int) git_hash_init(git_hash_ctx *ctx)
-{
-	assert(ctx);
-	CC_SHA1_Init(&ctx->c);
-	return 0;
-}
-
-GIT_INLINE(int) git_hash_update(git_hash_ctx *ctx, const void *_data, size_t len)
-{
-	const unsigned char *data = _data;
-
-	assert(ctx);
-
-	while (len > 0) {
-		CC_LONG chunk = (len > CC_LONG_MAX) ? CC_LONG_MAX : (CC_LONG)len;
-
-		CC_SHA1_Update(&ctx->c, data, chunk);
-
-		data += chunk;
-		len -= chunk;
-	}
-
-	return 0;
-}
-
-GIT_INLINE(int) git_hash_final(git_oid *out, git_hash_ctx *ctx)
-{
-	assert(ctx);
-	CC_SHA1_Final(out->id, &ctx->c);
-	return 0;
-}
-
 #endif
diff --git a/src/hash/hash_generic.c b/src/hash/hash_generic.c
index 7b33b61..55fbf8d 100644
--- a/src/hash/hash_generic.c
+++ b/src/hash/hash_generic.c
@@ -7,8 +7,6 @@
 
 #include "hash_generic.h"
 
-#include "hash.h"
-
 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
 
 /*
@@ -221,6 +219,21 @@ static void hash__block(git_hash_ctx *ctx, const unsigned int *data)
 	ctx->H[4] += E;
 }
 
+int git_hash_global_init(void)
+{
+	return 0;
+}
+
+int git_hash_ctx_init(git_hash_ctx *ctx)
+{
+	return git_hash_init(ctx);
+}
+
+void git_hash_ctx_cleanup(git_hash_ctx *ctx)
+{
+	GIT_UNUSED(ctx);
+}
+
 int git_hash_init(git_hash_ctx *ctx)
 {
 	ctx->size = 0;
@@ -285,4 +298,3 @@ int git_hash_final(git_oid *out, git_hash_ctx *ctx)
 
 	return 0;
 }
-
diff --git a/src/hash/hash_generic.h b/src/hash/hash_generic.h
index fb0009c..b97796c 100644
--- a/src/hash/hash_generic.h
+++ b/src/hash/hash_generic.h
@@ -8,8 +8,6 @@
 #ifndef INCLUDE_hash_hash_generic_h__
 #define INCLUDE_hash_hash_generic_h__
 
-#include "common.h"
-
 #include "hash.h"
 
 struct git_hash_ctx {
@@ -18,12 +16,4 @@ struct git_hash_ctx {
 	unsigned int W[16];
 };
 
-#define git_hash_ctx_init(ctx) git_hash_init(ctx)
-#define git_hash_ctx_cleanup(ctx)
-
-GIT_INLINE(int) git_hash_global_init(void)
-{
-	return 0;
-}
-
 #endif
diff --git a/src/hash/hash_mbedtls.c b/src/hash/hash_mbedtls.c
index a19d763..b29226c 100644
--- a/src/hash/hash_mbedtls.c
+++ b/src/hash/hash_mbedtls.c
@@ -9,6 +9,16 @@
 #include "hash.h"
 #include "hash/hash_mbedtls.h"
 
+int git_hash_global_init(void)
+{
+	return 0;
+}
+
+int git_hash_ctx_init(git_hash_ctx *ctx)
+{
+	return git_hash_init(ctx);
+}
+
 void git_hash_ctx_cleanup(git_hash_ctx *ctx)
 {
     assert(ctx);
diff --git a/src/hash/hash_mbedtls.h b/src/hash/hash_mbedtls.h
index 7f3decd..834b79d 100644
--- a/src/hash/hash_mbedtls.h
+++ b/src/hash/hash_mbedtls.h
@@ -8,17 +8,12 @@
 #ifndef INCLUDE_hash_mbedtld_h__
 #define INCLUDE_hash_mbedtld_h__
 
+#include "hash.h"
+
 #include <mbedtls/sha1.h>
 
 struct git_hash_ctx {
     mbedtls_sha1_context c;
 };
 
-#define git_hash_ctx_init(ctx) git_hash_init(ctx)
-
-GIT_INLINE(int) git_hash_global_init(void)
-{
-	return 0;
-}
-
 #endif /* INCLUDE_hash_mbedtld_h__ */
diff --git a/src/hash/hash_openssl.c b/src/hash/hash_openssl.c
new file mode 100644
index 0000000..8a6dd7a
--- /dev/null
+++ b/src/hash/hash_openssl.c
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#include "hash_openssl.h"
+
+int git_hash_global_init(void)
+{
+	return 0;
+}
+
+int git_hash_ctx_init(git_hash_ctx *ctx)
+{
+	return git_hash_init(ctx);
+}
+
+void git_hash_ctx_cleanup(git_hash_ctx *ctx)
+{
+	GIT_UNUSED(ctx);
+}
+
+int git_hash_init(git_hash_ctx *ctx)
+{
+	assert(ctx);
+
+	if (SHA1_Init(&ctx->c) != 1) {
+		git_error_set(GIT_ERROR_SHA1, "hash_openssl: failed to initialize hash context");
+		return -1;
+	}
+
+	return 0;
+}
+
+int git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
+{
+	assert(ctx);
+
+	if (SHA1_Update(&ctx->c, data, len) != 1) {
+		git_error_set(GIT_ERROR_SHA1, "hash_openssl: failed to update hash");
+		return -1;
+	}
+
+	return 0;
+}
+
+int git_hash_final(git_oid *out, git_hash_ctx *ctx)
+{
+	assert(ctx);
+
+	if (SHA1_Final(out->id, &ctx->c) != 1) {
+		git_error_set(GIT_ERROR_SHA1, "hash_openssl: failed to finalize hash");
+		return -1;
+	}
+
+	return 0;
+}
diff --git a/src/hash/hash_openssl.h b/src/hash/hash_openssl.h
index 8dbfd5a..c08ae10 100644
--- a/src/hash/hash_openssl.h
+++ b/src/hash/hash_openssl.h
@@ -8,6 +8,8 @@
 #ifndef INCLUDE_hash_hash_openssl_h__
 #define INCLUDE_hash_hash_openssl_h__
 
+#include "common.h"
+
 #include "hash.h"
 
 #include <openssl/sha.h>
@@ -16,48 +18,4 @@ struct git_hash_ctx {
 	SHA_CTX c;
 };
 
-#define git_hash_ctx_init(ctx) git_hash_init(ctx)
-#define git_hash_ctx_cleanup(ctx)
-
-GIT_INLINE(int) git_hash_global_init(void)
-{
-	return 0;
-}
-
-GIT_INLINE(int) git_hash_init(git_hash_ctx *ctx)
-{
-	assert(ctx);
-
-	if (SHA1_Init(&ctx->c) != 1) {
-		git_error_set(GIT_ERROR_SHA1, "hash_openssl: failed to initialize hash context");
-		return -1;
-	}
-
-	return 0;
-}
-
-GIT_INLINE(int) git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
-{
-	assert(ctx);
-
-	if (SHA1_Update(&ctx->c, data, len) != 1) {
-		git_error_set(GIT_ERROR_SHA1, "hash_openssl: failed to update hash");
-		return -1;
-	}
-
-	return 0;
-}
-
-GIT_INLINE(int) git_hash_final(git_oid *out, git_hash_ctx *ctx)
-{
-	assert(ctx);
-
-	if (SHA1_Final(out->id, &ctx->c) != 1) {
-		git_error_set(GIT_ERROR_SHA1, "hash_openssl: failed to finalize hash");
-		return -1;
-	}
-
-	return 0;
-}
-
 #endif
diff --git a/src/hash/hash_win32.c b/src/hash/hash_win32.c
index 4b68303..4d00c54 100644
--- a/src/hash/hash_win32.c
+++ b/src/hash/hash_win32.c
@@ -8,11 +8,21 @@
 #include "hash_win32.h"
 
 #include "global.h"
-#include "hash.h"
 
 #include <wincrypt.h>
 #include <strsafe.h>
 
+#define GIT_HASH_CNG_DLL_NAME           "bcrypt.dll"
+
+/* BCRYPT_SHA1_ALGORITHM */
+#define GIT_HASH_CNG_HASH_TYPE          L"SHA1"
+
+/* BCRYPT_OBJECT_LENGTH */
+#define GIT_HASH_CNG_HASH_OBJECT_LEN    L"ObjectLength"
+
+/* BCRYPT_HASH_REUSEABLE_FLAGS */
+#define GIT_HASH_CNG_HASH_REUSABLE      0x00000020
+
 static struct git_hash_prov hash_prov = {0};
 
 /* Hash initialization */
@@ -101,7 +111,7 @@ GIT_INLINE(void) hash_cryptoapi_prov_shutdown(void)
 	hash_prov.type = INVALID;
 }
 
-static void git_hash_global_shutdown(void)
+static void sha1_shutdown(void)
 {
 	if (hash_prov.type == CNG)
 		hash_cng_prov_shutdown();
@@ -119,7 +129,7 @@ int git_hash_global_init(void)
 	if ((error = hash_cng_prov_init()) < 0)
 		error = hash_cryptoapi_prov_init();
 
-	git__on_shutdown(git_hash_global_shutdown);
+	git__on_shutdown(sha1_shutdown);
 
 	return error;
 }
diff --git a/src/hash/hash_win32.h b/src/hash/hash_win32.h
index b9a85f8..e4970ce 100644
--- a/src/hash/hash_win32.h
+++ b/src/hash/hash_win32.h
@@ -36,17 +36,6 @@ struct hash_cryptoapi_prov {
  * would not exist when building in pre-Windows 2008 environments.
  */
 
-#define GIT_HASH_CNG_DLL_NAME           "bcrypt.dll"
-
-/* BCRYPT_SHA1_ALGORITHM */
-#define GIT_HASH_CNG_HASH_TYPE          L"SHA1"
-
-/* BCRYPT_OBJECT_LENGTH */
-#define GIT_HASH_CNG_HASH_OBJECT_LEN    L"ObjectLength"
-
-/* BCRYPT_HASH_REUSEABLE_FLAGS */
-#define GIT_HASH_CNG_HASH_REUSABLE      0x00000020
-
 /* Function declarations for CNG */
 typedef NTSTATUS (WINAPI *hash_win32_cng_open_algorithm_provider_fn)(
 	HANDLE /* BCRYPT_ALG_HANDLE */ *phAlgorithm,
@@ -138,6 +127,4 @@ struct git_hash_ctx {
 	} ctx;
 };
 
-extern int git_hash_global_init(void);
-
 #endif