util: Fix race condition on libgcrypt initialization
[mesa.git] / src / util / mesa-sha1.c
1 /* Copyright © 2007 Carl Worth
2 * Copyright © 2009 Jeremy Huddleston, Julien Cristau, and Matthieu Herrb
3 * Copyright © 2009-2010 Mikhail Gusarov
4 * Copyright © 2012 Yaakov Selkowitz and Keith Packard
5 * Copyright © 2014 Intel Corporation
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 */
26
27 #include "mesa-sha1.h"
28
29 #ifdef HAVE_SHA1
30
31 #if defined(HAVE_SHA1_IN_LIBMD) /* Use libmd for SHA1 */ \
32 || defined(HAVE_SHA1_IN_LIBC) /* Use libc for SHA1 */
33
34 #include <sha1.h>
35
36 struct mesa_sha1 *
37 _mesa_sha1_init(void)
38 {
39 SHA1_CTX *ctx = malloc(sizeof(*ctx));
40
41 if (!ctx)
42 return NULL;
43
44 SHA1Init(ctx);
45 return (struct mesa_sha1 *) ctx;
46 }
47
48 int
49 _mesa_sha1_update(struct mesa_sha1 *ctx, const void *data, int size)
50 {
51 SHA1_CTX *sha1_ctx = (SHA1_CTX *) ctx;
52
53 SHA1Update(sha1_ctx, data, size);
54 return 1;
55 }
56
57 int
58 _mesa_sha1_final(struct mesa_sha1 *ctx, unsigned char result[20])
59 {
60 SHA1_CTX *sha1_ctx = (SHA1_CTX *) ctx;
61
62 SHA1Final(result, sha1_ctx);
63 free(sha1_ctx);
64 return 1;
65 }
66
67 #elif defined(HAVE_SHA1_IN_COMMONCRYPTO) /* Use CommonCrypto for SHA1 */
68
69 #include <CommonCrypto/CommonDigest.h>
70
71 struct mesa_sha1 *
72 _mesa_sha1_init(void)
73 {
74 CC_SHA1_CTX *ctx = malloc(sizeof(*ctx));
75
76 if (!ctx)
77 return NULL;
78
79 CC_SHA1_Init(ctx);
80 return (struct mesa_sha1 *) ctx;
81 }
82
83 int
84 _mesa_sha1_update(struct mesa_sha1 *ctx, const void *data, int size)
85 {
86 CC_SHA1_CTX *sha1_ctx = (CC_SHA1_CTX *) ctx;
87
88 CC_SHA1_Update(sha1_ctx, data, size);
89 return 1;
90 }
91
92 int
93 _mesa_sha1_final(struct mesa_sha1 *ctx, unsigned char result[20])
94 {
95 CC_SHA1_CTX *sha1_ctx = (CC_SHA1_CTX *) ctx;
96
97 CC_SHA1_Final(result, sha1_ctx);
98 free(sha1_ctx);
99 return 1;
100 }
101
102 #elif defined(HAVE_SHA1_IN_CRYPTOAPI) /* Use CryptoAPI for SHA1 */
103
104 #define WIN32_LEAN_AND_MEAN
105 #include <windows.h>
106 #include <wincrypt.h>
107
108 static HCRYPTPROV hProv;
109
110 struct mesa_sha1 *
111 _mesa_sha1_init(void)
112 {
113 HCRYPTHASH *ctx = malloc(sizeof(*ctx));
114
115 if (!ctx)
116 return NULL;
117
118 CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
119 CryptCreateHash(hProv, CALG_SHA1, 0, 0, ctx);
120 return (struct mesa_sha1 *) ctx;
121 }
122
123 int
124 _mesa_sha1_update(struct mesa_sha1 *ctx, const void *data, int size)
125 {
126 HCRYPTHASH *hHash = (HCRYPTHASH *) ctx;
127
128 CryptHashData(*hHash, data, size, 0);
129 return 1;
130 }
131
132 int
133 _mesa_sha1_final(struct mesa_sha1 *ctx, unsigned char result[20])
134 {
135 HCRYPTHASH *hHash = (HCRYPTHASH *) ctx;
136 DWORD len = 20;
137
138 CryptGetHashParam(*hHash, HP_HASHVAL, result, &len, 0);
139 CryptDestroyHash(*hHash);
140 CryptReleaseContext(hProv, 0);
141 free(ctx);
142 return 1;
143 }
144
145 #elif defined(HAVE_SHA1_IN_LIBNETTLE) /* Use libnettle for SHA1 */
146
147 #include <nettle/sha.h>
148
149 struct mesa_sha1 *
150 _mesa_sha1_init(void)
151 {
152 struct sha1_ctx *ctx = malloc(sizeof(*ctx));
153
154 if (!ctx)
155 return NULL;
156 sha1_init(ctx);
157 return (struct mesa_sha1 *) ctx;
158 }
159
160 int
161 _mesa_sha1_update(struct mesa_sha1 *ctx, const void *data, int size)
162 {
163 sha1_update((struct sha1_ctx *) ctx, size, data);
164 return 1;
165 }
166
167 int
168 _mesa_sha1_final(struct mesa_sha1 *ctx, unsigned char result[20])
169 {
170 sha1_digest((struct sha1_ctx *) ctx, 20, result);
171 free(ctx);
172 return 1;
173 }
174
175 #elif defined(HAVE_SHA1_IN_LIBGCRYPT) /* Use libgcrypt for SHA1 */
176
177 #include <gcrypt.h>
178 #include "c11/threads.h"
179
180 static void _mesa_libgcrypt_init(void)
181 {
182 if (!gcry_check_version(NULL))
183 return NULL;
184 gcry_control(GCRYCTL_DISABLE_SECMEM, 0);
185 gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
186 }
187
188 struct mesa_sha1 *
189 _mesa_sha1_init(void)
190 {
191 static once_flag flag = ONCE_FLAG_INIT;
192 gcry_md_hd_t h;
193 gcry_error_t err;
194
195 call_once(&flag, _mesa_libgcrypt_init);
196
197 err = gcry_md_open(&h, GCRY_MD_SHA1, 0);
198 if (err)
199 return NULL;
200 return (struct mesa_sha1 *) h;
201 }
202
203 int
204 _mesa_sha1_update(struct mesa_sha1 *ctx, const void *data, int size)
205 {
206 gcry_md_hd_t h = (gcry_md_hd_t) ctx;
207
208 gcry_md_write(h, data, size);
209 return 1;
210 }
211
212 int
213 _mesa_sha1_final(struct mesa_sha1 *ctx, unsigned char result[20])
214 {
215 gcry_md_hd_t h = (gcry_md_hd_t) ctx;
216
217 memcpy(result, gcry_md_read(h, GCRY_MD_SHA1), 20);
218 gcry_md_close(h);
219 return 1;
220 }
221
222 #elif defined(HAVE_SHA1_IN_LIBSHA1) /* Use libsha1 */
223
224 #include <libsha1.h>
225
226 struct mesa_sha1 *
227 _mesa_sha1_init(void)
228 {
229 sha1_ctx *ctx = malloc(sizeof(*ctx));
230
231 if (!ctx)
232 return NULL;
233 sha1_begin(ctx);
234 return (struct mesa_sha1 *) ctx;
235 }
236
237 int
238 _mesa_sha1_update(struct mesa_sha1 *ctx, const void *data, int size)
239 {
240 sha1_hash(data, size, (sha1_ctx *) ctx);
241 return 1;
242 }
243
244 int
245 _mesa_sha1_final(struct mesa_sha1 *ctx, unsigned char result[20])
246 {
247 sha1_end(result, (sha1_ctx *) ctx);
248 free(ctx);
249 return 1;
250 }
251
252 #else /* Use OpenSSL's libcrypto */
253
254 #include <stddef.h> /* buggy openssl/sha.h wants size_t */
255 #include <openssl/sha.h>
256
257 struct mesa_sha1 *
258 _mesa_sha1_init(void)
259 {
260 int ret;
261 SHA_CTX *ctx = malloc(sizeof(*ctx));
262
263 if (!ctx)
264 return NULL;
265 ret = SHA1_Init(ctx);
266 if (!ret) {
267 free(ctx);
268 return NULL;
269 }
270 return (struct mesa_sha1 *) ctx;
271 }
272
273 int
274 _mesa_sha1_update(struct mesa_sha1 *ctx, const void *data, int size)
275 {
276 int ret;
277 SHA_CTX *sha_ctx = (SHA_CTX *) ctx;
278
279 ret = SHA1_Update(sha_ctx, data, size);
280 if (!ret)
281 free(sha_ctx);
282 return ret;
283 }
284
285 int
286 _mesa_sha1_final(struct mesa_sha1 *ctx, unsigned char result[20])
287 {
288 int ret;
289 SHA_CTX *sha_ctx = (SHA_CTX *) ctx;
290
291 ret = SHA1_Final(result, (SHA_CTX *) sha_ctx);
292 free(sha_ctx);
293 return ret;
294 }
295
296 #endif
297
298 void
299 _mesa_sha1_compute(const void *data, size_t size, unsigned char result[20])
300 {
301 struct mesa_sha1 *ctx;
302
303 ctx = _mesa_sha1_init();
304 _mesa_sha1_update(ctx, data, size);
305 _mesa_sha1_final(ctx, result);
306 }
307
308 char *
309 _mesa_sha1_format(char *buf, const unsigned char *sha1)
310 {
311 static const char hex_digits[] = "0123456789abcdef";
312 int i;
313
314 for (i = 0; i < 40; i += 2) {
315 buf[i] = hex_digits[sha1[i >> 1] >> 4];
316 buf[i + 1] = hex_digits[sha1[i >> 1] & 0x0f];
317 }
318 buf[i] = '\0';
319
320 return buf;
321 }
322
323 #endif