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