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