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
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:
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
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.
27 #include "mesa-sha1.h"
31 #if defined(HAVE_SHA1_IN_LIBMD) /* Use libmd for SHA1 */ \
32 || defined(HAVE_SHA1_IN_LIBC) /* Use libc for SHA1 */
39 SHA1_CTX
*ctx
= malloc(sizeof(*ctx
));
45 return (struct mesa_sha1
*) ctx
;
49 _mesa_sha1_update(struct mesa_sha1
*ctx
, const void *data
, int size
)
51 SHA1_CTX
*sha1_ctx
= (SHA1_CTX
*) ctx
;
53 SHA1Update(sha1_ctx
, data
, size
);
58 _mesa_sha1_final(struct mesa_sha1
*ctx
, unsigned char result
[20])
60 SHA1_CTX
*sha1_ctx
= (SHA1_CTX
*) ctx
;
62 SHA1Final(result
, sha1_ctx
);
67 #elif defined(HAVE_SHA1_IN_COMMONCRYPTO) /* Use CommonCrypto for SHA1 */
69 #include <CommonCrypto/CommonDigest.h>
74 CC_SHA1_CTX
*ctx
= malloc(sizeof(*ctx
));
80 return (struct mesa_sha1
*) ctx
;
84 _mesa_sha1_update(struct mesa_sha1
*ctx
, const void *data
, int size
)
86 CC_SHA1_CTX
*sha1_ctx
= (CC_SHA1_CTX
*) ctx
;
88 CC_SHA1_Update(sha1_ctx
, data
, size
);
93 _mesa_sha1_final(struct mesa_sha1
*ctx
, unsigned char result
[20])
95 CC_SHA1_CTX
*sha1_ctx
= (CC_SHA1_CTX
*) ctx
;
97 CC_SHA1_Final(result
, sha1_ctx
);
102 #elif defined(HAVE_SHA1_IN_CRYPTOAPI) /* Use CryptoAPI for SHA1 */
104 #define WIN32_LEAN_AND_MEAN
106 #include <wincrypt.h>
108 static HCRYPTPROV hProv
;
111 _mesa_sha1_init(void)
113 HCRYPTHASH
*ctx
= malloc(sizeof(*ctx
));
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
;
124 _mesa_sha1_update(struct mesa_sha1
*ctx
, const void *data
, int size
)
126 HCRYPTHASH
*hHash
= (HCRYPTHASH
*) ctx
;
128 CryptHashData(*hHash
, data
, size
, 0);
133 _mesa_sha1_final(struct mesa_sha1
*ctx
, unsigned char result
[20])
135 HCRYPTHASH
*hHash
= (HCRYPTHASH
*) ctx
;
138 CryptGetHashParam(*hHash
, HP_HASHVAL
, result
, &len
, 0);
139 CryptDestroyHash(*hHash
);
140 CryptReleaseContext(hProv
, 0);
145 #elif defined(HAVE_SHA1_IN_LIBNETTLE) /* Use libnettle for SHA1 */
147 #include <nettle/sha.h>
150 _mesa_sha1_init(void)
152 struct sha1_ctx
*ctx
= malloc(sizeof(*ctx
));
157 return (struct mesa_sha1
*) ctx
;
161 _mesa_sha1_update(struct mesa_sha1
*ctx
, const void *data
, int size
)
163 sha1_update((struct sha1_ctx
*) ctx
, size
, data
);
168 _mesa_sha1_final(struct mesa_sha1
*ctx
, unsigned char result
[20])
170 sha1_digest((struct sha1_ctx
*) ctx
, 20, result
);
175 #elif defined(HAVE_SHA1_IN_LIBGCRYPT) /* Use libgcrypt for SHA1 */
180 _mesa_sha1_init(void)
187 if (!gcry_check_version(NULL
))
189 gcry_control(GCRYCTL_DISABLE_SECMEM
, 0);
190 gcry_control(GCRYCTL_INITIALIZATION_FINISHED
, 0);
194 err
= gcry_md_open(&h
, GCRY_MD_SHA1
, 0);
197 return (struct mesa_sha1
*) h
;
201 _mesa_sha1_update(struct mesa_sha1
*ctx
, const void *data
, int size
)
203 gcry_md_hd_t h
= (gcry_md_hd_t
) ctx
;
205 gcry_md_write(h
, data
, size
);
210 _mesa_sha1_final(struct mesa_sha1
*ctx
, unsigned char result
[20])
212 gcry_md_hd_t h
= (gcry_md_hd_t
) ctx
;
214 memcpy(result
, gcry_md_read(h
, GCRY_MD_SHA1
), 20);
219 #elif defined(HAVE_SHA1_IN_LIBSHA1) /* Use libsha1 */
224 _mesa_sha1_init(void)
226 sha1_ctx
*ctx
= malloc(sizeof(*ctx
));
231 return (struct mesa_sha1
*) ctx
;
235 _mesa_sha1_update(struct mesa_sha1
*ctx
, const void *data
, int size
)
237 sha1_hash(data
, size
, (sha1_ctx
*) ctx
);
242 _mesa_sha1_final(struct mesa_sha1
*ctx
, unsigned char result
[20])
244 sha1_end(result
, (sha1_ctx
*) ctx
);
249 #else /* Use OpenSSL's libcrypto */
251 #include <stddef.h> /* buggy openssl/sha.h wants size_t */
252 #include <openssl/sha.h>
255 _mesa_sha1_init(void)
258 SHA_CTX
*ctx
= malloc(sizeof(*ctx
));
262 ret
= SHA1_Init(ctx
);
267 return (struct mesa_sha1
*) ctx
;
271 _mesa_sha1_update(struct mesa_sha1
*ctx
, const void *data
, int size
)
274 SHA_CTX
*sha_ctx
= (SHA_CTX
*) ctx
;
276 ret
= SHA1_Update(sha_ctx
, data
, size
);
283 _mesa_sha1_final(struct mesa_sha1
*ctx
, unsigned char result
[20])
286 SHA_CTX
*sha_ctx
= (SHA_CTX
*) ctx
;
288 ret
= SHA1_Final(result
, (SHA_CTX
*) sha_ctx
);
296 _mesa_sha1_compute(const void *data
, size_t size
, unsigned char result
[20])
298 struct mesa_sha1
*ctx
;
300 ctx
= _mesa_sha1_init();
301 _mesa_sha1_update(ctx
, data
, size
);
302 _mesa_sha1_final(ctx
, result
);
306 _mesa_sha1_format(char *buf
, const unsigned char *sha1
)
308 static const char hex_digits
[] = "0123456789abcdef";
311 for (i
= 0; i
< 40; i
+= 2) {
312 buf
[i
] = hex_digits
[sha1
[i
>> 1] >> 4];
313 buf
[i
+ 1] = hex_digits
[sha1
[i
>> 1] & 0x0f];