]>
Commit | Line | Data |
---|---|---|
d6fb0924 | 1 | /* |
359fc2d2 | 2 | * Copyright (C) the libgit2 contributors. All rights reserved. |
d6fb0924 ET |
3 | * |
4 | * This file is part of libgit2, distributed under the GNU GPL v2 with | |
5 | * a Linking Exception. For full terms see the included COPYING file. | |
6 | */ | |
7 | ||
eae0bfdc PP |
8 | #ifndef INCLUDE_hash_hash_win32_h__ |
9 | #define INCLUDE_hash_hash_win32_h__ | |
d6fb0924 ET |
10 | |
11 | #include "common.h" | |
eae0bfdc | 12 | |
d6fb0924 ET |
13 | #include "hash.h" |
14 | ||
15 | #include <wincrypt.h> | |
16 | #include <strsafe.h> | |
17 | ||
18 | enum hash_win32_prov_type { | |
19 | INVALID = 0, | |
20 | CRYPTOAPI, | |
21 | CNG | |
22 | }; | |
23 | ||
24 | /* | |
25 | * CryptoAPI is available for hashing on Windows XP and newer. | |
26 | */ | |
27 | ||
28 | struct hash_cryptoapi_prov { | |
29 | HCRYPTPROV handle; | |
30 | }; | |
31 | ||
32 | /* | |
33 | * CNG (bcrypt.dll) is significantly more performant than CryptoAPI and is | |
34 | * preferred, however it is only available on Windows 2008 and newer and | |
35 | * must therefore be dynamically loaded, and we must inline constants that | |
36 | * would not exist when building in pre-Windows 2008 environments. | |
37 | */ | |
38 | ||
39 | #define GIT_HASH_CNG_DLL_NAME "bcrypt.dll" | |
40 | ||
41 | /* BCRYPT_SHA1_ALGORITHM */ | |
42 | #define GIT_HASH_CNG_HASH_TYPE L"SHA1" | |
43 | ||
44 | /* BCRYPT_OBJECT_LENGTH */ | |
45 | #define GIT_HASH_CNG_HASH_OBJECT_LEN L"ObjectLength" | |
46 | ||
47 | /* BCRYPT_HASH_REUSEABLE_FLAGS */ | |
48 | #define GIT_HASH_CNG_HASH_REUSABLE 0x00000020 | |
49 | ||
50 | /* Function declarations for CNG */ | |
51 | typedef NTSTATUS (WINAPI *hash_win32_cng_open_algorithm_provider_fn)( | |
0cb16fe9 L |
52 | HANDLE /* BCRYPT_ALG_HANDLE */ *phAlgorithm, |
53 | LPCWSTR pszAlgId, | |
54 | LPCWSTR pszImplementation, | |
55 | DWORD dwFlags); | |
d6fb0924 ET |
56 | |
57 | typedef NTSTATUS (WINAPI *hash_win32_cng_get_property_fn)( | |
58 | HANDLE /* BCRYPT_HANDLE */ hObject, | |
59 | LPCWSTR pszProperty, | |
60 | PUCHAR pbOutput, | |
61 | ULONG cbOutput, | |
62 | ULONG *pcbResult, | |
63 | ULONG dwFlags); | |
64 | ||
65 | typedef NTSTATUS (WINAPI *hash_win32_cng_create_hash_fn)( | |
66 | HANDLE /* BCRYPT_ALG_HANDLE */ hAlgorithm, | |
67 | HANDLE /* BCRYPT_HASH_HANDLE */ *phHash, | |
68 | PUCHAR pbHashObject, ULONG cbHashObject, | |
69 | PUCHAR pbSecret, | |
70 | ULONG cbSecret, | |
71 | ULONG dwFlags); | |
72 | ||
73 | typedef NTSTATUS (WINAPI *hash_win32_cng_finish_hash_fn)( | |
74 | HANDLE /* BCRYPT_HASH_HANDLE */ hHash, | |
75 | PUCHAR pbOutput, | |
76 | ULONG cbOutput, | |
77 | ULONG dwFlags); | |
78 | ||
79 | typedef NTSTATUS (WINAPI *hash_win32_cng_hash_data_fn)( | |
80 | HANDLE /* BCRYPT_HASH_HANDLE */ hHash, | |
81 | PUCHAR pbInput, | |
82 | ULONG cbInput, | |
83 | ULONG dwFlags); | |
84 | ||
85 | typedef NTSTATUS (WINAPI *hash_win32_cng_destroy_hash_fn)( | |
86 | HANDLE /* BCRYPT_HASH_HANDLE */ hHash); | |
87 | ||
88 | typedef NTSTATUS (WINAPI *hash_win32_cng_close_algorithm_provider_fn)( | |
89 | HANDLE /* BCRYPT_ALG_HANDLE */ hAlgorithm, | |
90 | ULONG dwFlags); | |
91 | ||
92 | struct hash_cng_prov { | |
93 | /* DLL for CNG */ | |
94 | HINSTANCE dll; | |
95 | ||
96 | /* Function pointers for CNG */ | |
97 | hash_win32_cng_open_algorithm_provider_fn open_algorithm_provider; | |
98 | hash_win32_cng_get_property_fn get_property; | |
99 | hash_win32_cng_create_hash_fn create_hash; | |
100 | hash_win32_cng_finish_hash_fn finish_hash; | |
101 | hash_win32_cng_hash_data_fn hash_data; | |
102 | hash_win32_cng_destroy_hash_fn destroy_hash; | |
103 | hash_win32_cng_close_algorithm_provider_fn close_algorithm_provider; | |
104 | ||
105 | HANDLE /* BCRYPT_ALG_HANDLE */ handle; | |
106 | DWORD hash_object_size; | |
107 | }; | |
108 | ||
109 | struct git_hash_prov { | |
110 | enum hash_win32_prov_type type; | |
111 | ||
112 | union { | |
113 | struct hash_cryptoapi_prov cryptoapi; | |
114 | struct hash_cng_prov cng; | |
115 | } prov; | |
116 | }; | |
117 | ||
118 | /* Hash contexts */ | |
119 | ||
120 | struct hash_cryptoapi_ctx { | |
121 | bool valid; | |
122 | HCRYPTHASH hash_handle; | |
123 | }; | |
124 | ||
125 | struct hash_cng_ctx { | |
126 | bool updated; | |
127 | HANDLE /* BCRYPT_HASH_HANDLE */ hash_handle; | |
128 | PBYTE hash_object; | |
129 | }; | |
130 | ||
131 | struct git_hash_ctx { | |
132 | enum hash_win32_prov_type type; | |
133 | git_hash_prov *prov; | |
134 | ||
135 | union { | |
136 | struct hash_cryptoapi_ctx cryptoapi; | |
137 | struct hash_cng_ctx cng; | |
138 | } ctx; | |
139 | }; | |
140 | ||
eae0bfdc | 141 | #endif |