Rename sha1.c and sha1.h to mesa-sha1.c and mesa-sha1.h
[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 #if defined(HAVE_SHA1_IN_LIBMD) /* Use libmd for SHA1 */ \
30 || defined(HAVE_SHA1_IN_LIBC) /* Use libc for SHA1 */
31
32 #include <sha1.h>
33
34 struct mesa_sha1 *
35 _mesa_sha1_init(void)
36 {
37 SHA1_CTX *ctx = malloc(sizeof(*ctx));
38
39 if (!ctx)
40 return NULL;
41
42 SHA1Init(ctx);
43 return (struct mesa_sha1 *) ctx;
44 }
45
46 int
47 _mesa_sha1_update(struct mesa_sha1 *ctx, const void *data, int size)
48 {
49 SHA1_CTX *sha1_ctx = (SHA1_CTX *) ctx;
50
51 SHA1Update(sha1_ctx, data, size);
52 return 1;
53 }
54
55 int
56 _mesa_sha1_final(struct mesa_sha1 *ctx, unsigned char result[20])
57 {
58 SHA1_CTX *sha1_ctx = (SHA1_CTX *) ctx;
59
60 SHA1Final(result, sha1_ctx);
61 free(sha1_ctx);
62 return 1;
63 }
64
65 #elif defined(HAVE_SHA1_IN_COMMONCRYPTO) /* Use CommonCrypto for SHA1 */
66
67 #include <CommonCrypto/CommonDigest.h>
68
69 struct mesa_sha1 *
70 _mesa_sha1_init(void)
71 {
72 CC_SHA1_CTX *ctx = malloc(sizeof(*ctx));
73
74 if (!ctx)
75 return NULL;
76
77 CC_SHA1_Init(ctx);
78 return (struct mesa_sha1 *) ctx;
79 }
80
81 int
82 _mesa_sha1_update(struct mesa_sha1 *ctx, const void *data, int size)
83 {
84 CC_SHA1_CTX *sha1_ctx = (CC_SHA1_CTX *) ctx;
85
86 CC_SHA1_Update(sha1_ctx, data, size);
87 return 1;
88 }
89
90 int
91 _mesa_sha1_final(struct mesa_sha1 *ctx, unsigned char result[20])
92 {
93 CC_SHA1_CTX *sha1_ctx = (CC_SHA1_CTX *) ctx;
94
95 CC_SHA1_Final(result, sha1_ctx);
96 free(sha1_ctx);
97 return 1;
98 }
99
100 #elif defined(HAVE_SHA1_IN_CRYPTOAPI) /* Use CryptoAPI for SHA1 */
101
102 #define WIN32_LEAN_AND_MEAN
103 #include <windows.h>
104 #include <wincrypt.h>
105
106 static HCRYPTPROV hProv;
107
108 struct mesa_sha1 *
109 _mesa_sha1_init(void)
110 {
111 HCRYPTHASH *ctx = malloc(sizeof(*ctx));
112
113 if (!ctx)
114 return NULL;
115
116 CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
117 CryptCreateHash(hProv, CALG_SHA1, 0, 0, ctx);
118 return (struct mesa_sha1 *) ctx;
119 }
120
121 int
122 _mesa_sha1_update(struct mesa_sha1 *ctx, const void *data, int size)
123 {
124 HCRYPTHASH *hHash = (HCRYPTHASH *) ctx;
125
126 CryptHashData(*hHash, data, size, 0);
127 return 1;
128 }
129
130 int
131 _mesa_sha1_final(struct mesa_sha1 *ctx, unsigned char result[20])
132 {
133 HCRYPTHASH *hHash = (HCRYPTHASH *) ctx;
134 DWORD len = 20;
135
136 CryptGetHashParam(*hHash, HP_HASHVAL, result, &len, 0);
137 CryptDestroyHash(*hHash);
138 CryptReleaseContext(hProv, 0);
139 free(ctx);
140 return 1;
141 }
142
143 #elif defined(HAVE_SHA1_IN_LIBNETTLE) /* Use libnettle for SHA1 */
144
145 #include <nettle/sha.h>
146
147 struct mesa_sha1 *
148 _mesa_sha1_init(void)
149 {
150 struct sha1_ctx *ctx = malloc(sizeof(*ctx));
151
152 if (!ctx)
153 return NULL;
154 sha1_init(ctx);
155 return (struct mesa_sha1 *) ctx;
156 }
157
158 int
159 _mesa_sha1_update(struct mesa_sha1 *ctx, const void *data, int size)
160 {
161 sha1_update((struct sha1_ctx *) ctx, size, data);
162 return 1;
163 }
164
165 int
166 _mesa_sha1_final(struct mesa_sha1 *ctx, unsigned char result[20])
167 {
168 sha1_digest((struct sha1_ctx *) ctx, 20, result);
169 free(ctx);
170 return 1;
171 }
172
173 #elif defined(HAVE_SHA1_IN_LIBGCRYPT) /* Use libgcrypt for SHA1 */
174
175 #include <gcrypt.h>
176
177 struct mesa_sha1 *
178 _mesa_sha1_init(void)
179 {
180 static int init;
181 gcry_md_hd_t h;
182 gcry_error_t err;
183
184 if (!init) {
185 if (!gcry_check_version(NULL))
186 return NULL;
187 gcry_control(GCRYCTL_DISABLE_SECMEM, 0);
188 gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
189 init = 1;
190 }
191
192 err = gcry_md_open(&h, GCRY_MD_SHA1, 0);
193 if (err)
194 return NULL;
195 return (struct mesa_sha1 *) h;
196 }
197
198 int
199 _mesa_sha1_update(struct mesa_sha1 *ctx, const void *data, int size)
200 {
201 gcry_md_hd_t h = (gcry_md_hd_t) ctx;
202
203 gcry_md_write(h, data, size);
204 return 1;
205 }
206
207 int
208 _mesa_sha1_final(struct mesa_sha1 *ctx, unsigned char result[20])
209 {
210 gcry_md_hd_t h = (gcry_md_hd_t) ctx;
211
212 memcpy(result, gcry_md_read(h, GCRY_MD_SHA1), 20);
213 gcry_md_close(h);
214 return 1;
215 }
216
217 #elif defined(HAVE_SHA1_IN_LIBSHA1) /* Use libsha1 */
218
219 #include <libsha1.h>
220
221 struct mesa_sha1 *
222 _mesa_sha1_init(void)
223 {
224 sha1_ctx *ctx = malloc(sizeof(*ctx));
225
226 if (!ctx)
227 return NULL;
228 sha1_begin(ctx);
229 return (struct mesa_sha1 *) ctx;
230 }
231
232 int
233 _mesa_sha1_update(struct mesa_sha1 *ctx, const void *data, int size)
234 {
235 sha1_hash(data, size, (sha1_ctx *) ctx);
236 return 1;
237 }
238
239 int
240 _mesa_sha1_final(struct mesa_sha1 *ctx, unsigned char result[20])
241 {
242 sha1_end(result, (sha1_ctx *) ctx);
243 free(ctx);
244 return 1;
245 }
246
247 #else /* Use OpenSSL's libcrypto */
248
249 #include <stddef.h> /* buggy openssl/sha.h wants size_t */
250 #include <openssl/sha.h>
251
252 struct mesa_sha1 *
253 _mesa_sha1_init(void)
254 {
255 int ret;
256 SHA_CTX *ctx = malloc(sizeof(*ctx));
257
258 if (!ctx)
259 return NULL;
260 ret = SHA1_Init(ctx);
261 if (!ret) {
262 free(ctx);
263 return NULL;
264 }
265 return (struct mesa_sha1 *) ctx;
266 }
267
268 int
269 _mesa_sha1_update(struct mesa_sha1 *ctx, const void *data, int size)
270 {
271 int ret;
272 SHA_CTX *sha_ctx = (SHA_CTX *) ctx;
273
274 ret = SHA1_Update(sha_ctx, data, size);
275 if (!ret)
276 free(sha_ctx);
277 return ret;
278 }
279
280 int
281 _mesa_sha1_final(struct mesa_sha1 *ctx, unsigned char result[20])
282 {
283 int ret;
284 SHA_CTX *sha_ctx = (SHA_CTX *) ctx;
285
286 ret = SHA1_Final(result, (SHA_CTX *) sha_ctx);
287 free(sha_ctx);
288 return ret;
289 }
290
291 #endif
292
293 void
294 _mesa_sha1_compute(const void *data, size_t size, unsigned char result[20])
295 {
296 struct mesa_sha1 *ctx;
297
298 ctx = _mesa_sha1_init();
299 _mesa_sha1_update(ctx, data, size);
300 _mesa_sha1_final(ctx, result);
301 }
302
303 char *
304 _mesa_sha1_format(char *buf, const unsigned char *sha1)
305 {
306 static const char hex_digits[] = "0123456789abcdef";
307 int i;
308
309 for (i = 0; i < 40; i += 2) {
310 buf[i] = hex_digits[sha1[i >> 1] >> 4];
311 buf[i + 1] = hex_digits[sha1[i >> 1] & 0x0f];
312 }
313 buf[i] = '\0';
314
315 return buf;
316 }