Imported Upstream version 1.15.1
[deb_xorg-server.git] / os / xsha1.c
CommitLineData
a09e091a
JB
1#ifdef HAVE_DIX_CONFIG_H
2#include <dix-config.h>
3#endif
4
5#include "os.h"
6#include "xsha1.h"
7
8#if defined(HAVE_SHA1_IN_LIBMD) /* Use libmd for SHA1 */ \
9 || defined(HAVE_SHA1_IN_LIBC) /* Use libc for SHA1 */
10
11#include <sha1.h>
12
13void *
14x_sha1_init(void)
15{
16 SHA1_CTX *ctx = malloc(sizeof(*ctx));
17
18 if (!ctx)
19 return NULL;
20 SHA1Init(ctx);
21 return ctx;
22}
23
24int
25x_sha1_update(void *ctx, void *data, int size)
26{
27 SHA1_CTX *sha1_ctx = ctx;
28
29 SHA1Update(sha1_ctx, data, size);
30 return 1;
31}
32
33int
34x_sha1_final(void *ctx, unsigned char result[20])
35{
36 SHA1_CTX *sha1_ctx = ctx;
37
38 SHA1Final(result, sha1_ctx);
39 free(sha1_ctx);
40 return 1;
41}
42
43#elif defined(HAVE_SHA1_IN_COMMONCRYPTO) /* Use CommonCrypto for SHA1 */
44
45#include <CommonCrypto/CommonDigest.h>
46
47void *
48x_sha1_init(void)
49{
50 CC_SHA1_CTX *ctx = malloc(sizeof(*ctx));
51
52 if (!ctx)
53 return NULL;
54 CC_SHA1_Init(ctx);
55 return ctx;
56}
57
58int
59x_sha1_update(void *ctx, void *data, int size)
60{
61 CC_SHA1_CTX *sha1_ctx = ctx;
62
63 CC_SHA1_Update(sha1_ctx, data, size);
64 return 1;
65}
66
67int
68x_sha1_final(void *ctx, unsigned char result[20])
69{
70 CC_SHA1_CTX *sha1_ctx = ctx;
71
72 CC_SHA1_Final(result, sha1_ctx);
73 free(sha1_ctx);
74 return 1;
75}
76
77#elif defined(HAVE_SHA1_IN_CRYPTOAPI) /* Use CryptoAPI for SHA1 */
78
79#define WIN32_LEAN_AND_MEAN
80#include <X11/Xwindows.h>
81#include <wincrypt.h>
82
83static HCRYPTPROV hProv;
84
85void *
86x_sha1_init(void)
87{
88 HCRYPTHASH *ctx = malloc(sizeof(*ctx));
89
90 if (!ctx)
91 return NULL;
92 CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
93 CryptCreateHash(hProv, CALG_SHA1, 0, 0, ctx);
94 return ctx;
95}
96
97int
98x_sha1_update(void *ctx, void *data, int size)
99{
100 HCRYPTHASH *hHash = ctx;
101
102 CryptHashData(*hHash, data, size, 0);
103 return 1;
104}
105
106int
107x_sha1_final(void *ctx, unsigned char result[20])
108{
109 HCRYPTHASH *hHash = ctx;
110 DWORD len = 20;
111
112 CryptGetHashParam(*hHash, HP_HASHVAL, result, &len, 0);
113 CryptDestroyHash(*hHash);
114 CryptReleaseContext(hProv, 0);
115 free(ctx);
116 return 1;
117}
118
119#elif defined(HAVE_SHA1_IN_LIBNETTLE) /* Use libnettle for SHA1 */
120
121#include <nettle/sha.h>
122
123void *
124x_sha1_init(void)
125{
126 struct sha1_ctx *ctx = malloc(sizeof(*ctx));
127
128 if (!ctx)
129 return NULL;
130 sha1_init(ctx);
131 return ctx;
132}
133
134int
135x_sha1_update(void *ctx, void *data, int size)
136{
137 sha1_update(ctx, size, data);
138 return 1;
139}
140
141int
142x_sha1_final(void *ctx, unsigned char result[20])
143{
144 sha1_digest(ctx, 20, result);
145 free(ctx);
146 return 1;
147}
148
149#elif defined(HAVE_SHA1_IN_LIBGCRYPT) /* Use libgcrypt for SHA1 */
150
151#include <gcrypt.h>
152
153void *
154x_sha1_init(void)
155{
156 static int init;
157 gcry_md_hd_t h;
158 gcry_error_t err;
159
160 if (!init) {
161 if (!gcry_check_version(NULL))
162 return NULL;
163 gcry_control(GCRYCTL_DISABLE_SECMEM, 0);
164 gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
165 init = 1;
166 }
167
168 err = gcry_md_open(&h, GCRY_MD_SHA1, 0);
169 if (err)
170 return NULL;
171 return h;
172}
173
174int
175x_sha1_update(void *ctx, void *data, int size)
176{
177 gcry_md_hd_t h = ctx;
178
179 gcry_md_write(h, data, size);
180 return 1;
181}
182
183int
184x_sha1_final(void *ctx, unsigned char result[20])
185{
186 gcry_md_hd_t h = ctx;
187
188 memcpy(result, gcry_md_read(h, GCRY_MD_SHA1), 20);
189 gcry_md_close(h);
190 return 1;
191}
192
193#elif defined(HAVE_SHA1_IN_LIBSHA1) /* Use libsha1 */
194
195#include <libsha1.h>
196
197void *
198x_sha1_init(void)
199{
200 sha1_ctx *ctx = malloc(sizeof(*ctx));
201
202 if (!ctx)
203 return NULL;
204 sha1_begin(ctx);
205 return ctx;
206}
207
208int
209x_sha1_update(void *ctx, void *data, int size)
210{
211 sha1_hash(data, size, ctx);
212 return 1;
213}
214
215int
216x_sha1_final(void *ctx, unsigned char result[20])
217{
218 sha1_end(result, ctx);
219 free(ctx);
220 return 1;
221}
222
223#else /* Use OpenSSL's libcrypto */
224
225#include <stddef.h> /* buggy openssl/sha.h wants size_t */
226#include <openssl/sha.h>
227
228void *
229x_sha1_init(void)
230{
231 int ret;
232 SHA_CTX *ctx = malloc(sizeof(*ctx));
233
234 if (!ctx)
235 return NULL;
236 ret = SHA1_Init(ctx);
237 if (!ret) {
238 free(ctx);
239 return NULL;
240 }
241 return ctx;
242}
243
244int
245x_sha1_update(void *ctx, void *data, int size)
246{
247 int ret;
248 SHA_CTX *sha_ctx = ctx;
249
250 ret = SHA1_Update(sha_ctx, data, size);
251 if (!ret)
252 free(sha_ctx);
253 return ret;
254}
255
256int
257x_sha1_final(void *ctx, unsigned char result[20])
258{
259 int ret;
260 SHA_CTX *sha_ctx = ctx;
261
262 ret = SHA1_Final(result, sha_ctx);
263 free(sha_ctx);
264 return ret;
265}
266
267#endif