Skip to content

Commit ce78c83

Browse files
committed
sha: ensure we test both cng and cryptoapi on windows
When GIT_SHA1_WIN32 or GIT_SHA256_WIN32 is used, ensure that we test both CryptoNG ("cng") and CryptoAPI.
1 parent 6a7d5d2 commit ce78c83

File tree

4 files changed

+95
-5
lines changed

4 files changed

+95
-5
lines changed

src/util/hash/win32.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,27 @@ static int hash_provider_init(void)
239239
return error;
240240
}
241241

242+
git_hash_win32_provider_t git_hash_win32_provider(void)
243+
{
244+
return hash_provider.type;
245+
}
246+
247+
int git_hash_win32_set_provider(git_hash_win32_provider_t provider)
248+
{
249+
if (provider == hash_provider.type)
250+
return 0;
251+
252+
hash_provider_shutdown();
253+
254+
if (provider == GIT_HASH_WIN32_CNG)
255+
return cng_provider_init();
256+
else if (provider == GIT_HASH_WIN32_CRYPTOAPI)
257+
return cryptoapi_provider_init();
258+
259+
git_error_set(GIT_ERROR_SHA, "unsupported win32 provider");
260+
return -1;
261+
}
262+
242263
/* CryptoAPI: available in Windows XP and newer */
243264

244265
GIT_INLINE(int) hash_cryptoapi_init(git_hash_win32_ctx *ctx)

src/util/hash/win32.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ typedef struct {
3838
} ctx;
3939
} git_hash_win32_ctx;
4040

41+
/*
42+
* Gets/sets the current hash provider (cng or cryptoapi). This is only
43+
* for testing purposes.
44+
*/
45+
git_hash_win32_provider_t git_hash_win32_provider(void);
46+
int git_hash_win32_set_provider(git_hash_win32_provider_t provider);
47+
4148
#ifdef GIT_SHA1_WIN32
4249
struct git_hash_sha1_ctx {
4350
git_hash_win32_ctx win32;

tests/util/sha1.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,25 @@
33

44
#define FIXTURE_DIR "sha1"
55

6+
#ifdef GIT_SHA1_WIN32
7+
static git_hash_win32_provider_t orig_provider;
8+
#endif
9+
610
void test_sha1__initialize(void)
711
{
12+
#ifdef GIT_SHA1_WIN32
13+
orig_provider = git_hash_win32_provider();
14+
#endif
15+
816
cl_fixture_sandbox(FIXTURE_DIR);
917
}
1018

1119
void test_sha1__cleanup(void)
1220
{
21+
#ifdef GIT_SHA1_WIN32
22+
git_hash_win32_set_provider(orig_provider);
23+
#endif
24+
1325
cl_fixture_cleanup(FIXTURE_DIR);
1426
}
1527

@@ -68,3 +80,21 @@ void test_sha1__detect_collision_attack(void)
6880
#endif
6981
}
7082

83+
void test_sha1__win32_providers(void)
84+
{
85+
#ifdef GIT_SHA1_WIN32
86+
unsigned char expected[GIT_HASH_SHA1_SIZE] = {
87+
0x38, 0x76, 0x2c, 0xf7, 0xf5, 0x59, 0x34, 0xb3, 0x4d, 0x17,
88+
0x9a, 0xe6, 0xa4, 0xc8, 0x0c, 0xad, 0xcc, 0xbb, 0x7f, 0x0a
89+
};
90+
unsigned char actual[GIT_HASH_SHA1_SIZE];
91+
92+
git_hash_win32_set_provider(GIT_HASH_WIN32_CRYPTOAPI);
93+
cl_git_pass(sha1_file(actual, FIXTURE_DIR "/shattered-1.pdf"));
94+
cl_assert_equal_i(0, memcmp(expected, actual, GIT_HASH_SHA1_SIZE));
95+
96+
git_hash_win32_set_provider(GIT_HASH_WIN32_CNG);
97+
cl_git_pass(sha1_file(actual, FIXTURE_DIR "/shattered-1.pdf"));
98+
cl_assert_equal_i(0, memcmp(expected, actual, GIT_HASH_SHA1_SIZE));
99+
#endif
100+
}

tests/util/sha256.c

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,25 @@
33

44
#define FIXTURE_DIR "sha1"
55

6-
void test_core_sha256__initialize(void)
6+
#ifdef GIT_SHA256_WIN32
7+
static git_hash_win32_provider_t orig_provider;
8+
#endif
9+
10+
void test_sha256__initialize(void)
711
{
12+
#ifdef GIT_SHA256_WIN32
13+
orig_provider = git_hash_win32_provider();
14+
#endif
15+
816
cl_fixture_sandbox(FIXTURE_DIR);
917
}
1018

11-
void test_core_sha256__cleanup(void)
19+
void test_sha256__cleanup(void)
1220
{
21+
#ifdef GIT_SHA256_WIN32
22+
git_hash_win32_set_provider(orig_provider);
23+
#endif
24+
1325
cl_fixture_cleanup(FIXTURE_DIR);
1426
}
1527

@@ -37,7 +49,7 @@ static int sha256_file(unsigned char *out, const char *filename)
3749
return ret;
3850
}
3951

40-
void test_core_sha256__empty(void)
52+
void test_sha256__empty(void)
4153
{
4254
unsigned char expected[GIT_HASH_SHA256_SIZE] = {
4355
0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14,
@@ -51,7 +63,7 @@ void test_core_sha256__empty(void)
5163
cl_assert_equal_i(0, memcmp(expected, actual, GIT_HASH_SHA256_SIZE));
5264
}
5365

54-
void test_core_sha256__hello(void)
66+
void test_sha256__hello(void)
5567
{
5668
unsigned char expected[GIT_HASH_SHA256_SIZE] = {
5769
0xaa, 0x32, 0x7f, 0xae, 0x5c, 0x91, 0x58, 0x3a,
@@ -65,7 +77,7 @@ void test_core_sha256__hello(void)
6577
cl_assert_equal_i(0, memcmp(expected, actual, GIT_HASH_SHA256_SIZE));
6678
}
6779

68-
void test_core_sha256__pdf(void)
80+
void test_sha256__pdf(void)
6981
{
7082
unsigned char expected[GIT_HASH_SHA256_SIZE] = {
7183
0x2b, 0xb7, 0x87, 0xa7, 0x3e, 0x37, 0x35, 0x2f,
@@ -79,3 +91,23 @@ void test_core_sha256__pdf(void)
7991
cl_assert_equal_i(0, memcmp(expected, actual, GIT_HASH_SHA256_SIZE));
8092
}
8193

94+
void test_sha256__win32_providers(void)
95+
{
96+
#ifdef GIT_SHA256_WIN32
97+
unsigned char expected[GIT_HASH_SHA256_SIZE] = {
98+
0x2b, 0xb7, 0x87, 0xa7, 0x3e, 0x37, 0x35, 0x2f,
99+
0x92, 0x38, 0x3a, 0xbe, 0x7e, 0x29, 0x02, 0x93,
100+
0x6d, 0x10, 0x59, 0xad, 0x9f, 0x1b, 0xa6, 0xda,
101+
0xaa, 0x9c, 0x1e, 0x58, 0xee, 0x69, 0x70, 0xd0
102+
};
103+
unsigned char actual[GIT_HASH_SHA256_SIZE];
104+
105+
git_hash_win32_set_provider(GIT_HASH_WIN32_CRYPTOAPI);
106+
cl_git_pass(sha256_file(actual, FIXTURE_DIR "/shattered-1.pdf"));
107+
cl_assert_equal_i(0, memcmp(expected, actual, GIT_HASH_SHA256_SIZE));
108+
109+
git_hash_win32_set_provider(GIT_HASH_WIN32_CNG);
110+
cl_git_pass(sha256_file(actual, FIXTURE_DIR "/shattered-1.pdf"));
111+
cl_assert_equal_i(0, memcmp(expected, actual, GIT_HASH_SHA256_SIZE));
112+
#endif
113+
}

0 commit comments

Comments
 (0)