Apply a patch for fixing TDIS-5990 (CVE-2013-1940 allow physically proximate attacker...
[framework/uifw/xorg/server/xorg-server.git] / os / xsha1.c
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
13 void *
14 x_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
24 int
25 x_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
33 int
34 x_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
47 void *
48 x_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
58 int
59 x_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
67 int
68 x_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
83 static HCRYPTPROV hProv;
84
85 void *
86 x_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
97 int
98 x_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
106 int
107 x_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_LIBGCRYPT)   /* Use libgcrypt for SHA1 */
120
121 #include <gcrypt.h>
122
123 void *
124 x_sha1_init(void)
125 {
126     static int init;
127     gcry_md_hd_t h;
128     gcry_error_t err;
129
130     if (!init) {
131         if (!gcry_check_version(NULL))
132             return NULL;
133         gcry_control(GCRYCTL_DISABLE_SECMEM, 0);
134         gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
135         init = 1;
136     }
137
138     err = gcry_md_open(&h, GCRY_MD_SHA1, 0);
139     if (err)
140         return NULL;
141     return h;
142 }
143
144 int
145 x_sha1_update(void *ctx, void *data, int size)
146 {
147     gcry_md_hd_t h = ctx;
148
149     gcry_md_write(h, data, size);
150     return 1;
151 }
152
153 int
154 x_sha1_final(void *ctx, unsigned char result[20])
155 {
156     gcry_md_hd_t h = ctx;
157
158     memcpy(result, gcry_md_read(h, GCRY_MD_SHA1), 20);
159     gcry_md_close(h);
160     return 1;
161 }
162
163 #elif defined(HAVE_SHA1_IN_LIBSHA1)     /* Use libsha1 */
164
165 #include <libsha1.h>
166
167 void *
168 x_sha1_init(void)
169 {
170     sha1_ctx *ctx = malloc(sizeof(*ctx));
171
172     if (!ctx)
173         return NULL;
174     sha1_begin(ctx);
175     return ctx;
176 }
177
178 int
179 x_sha1_update(void *ctx, void *data, int size)
180 {
181     sha1_hash(data, size, ctx);
182     return 1;
183 }
184
185 int
186 x_sha1_final(void *ctx, unsigned char result[20])
187 {
188     sha1_end(result, ctx);
189     free(ctx);
190     return 1;
191 }
192
193 #else                           /* Use OpenSSL's libcrypto */
194
195 #include <stddef.h>             /* buggy openssl/sha.h wants size_t */
196 #include <openssl/sha.h>
197
198 void *
199 x_sha1_init(void)
200 {
201     int ret;
202     SHA_CTX *ctx = malloc(sizeof(*ctx));
203
204     if (!ctx)
205         return NULL;
206     ret = SHA1_Init(ctx);
207     if (!ret) {
208         free(ctx);
209         return NULL;
210     }
211     return ctx;
212 }
213
214 int
215 x_sha1_update(void *ctx, void *data, int size)
216 {
217     int ret;
218     SHA_CTX *sha_ctx = ctx;
219
220     ret = SHA1_Update(sha_ctx, data, size);
221     if (!ret)
222         free(sha_ctx);
223     return ret;
224 }
225
226 int
227 x_sha1_final(void *ctx, unsigned char result[20])
228 {
229     int ret;
230     SHA_CTX *sha_ctx = ctx;
231
232     ret = SHA1_Final(result, sha_ctx);
233     free(sha_ctx);
234     return ret;
235 }
236
237 #endif