winex11.drv: Map coordinates before calling send_mouse_input.
[wine/zf.git] / dlls / rsaenh / implglue.c
blob9d90ad70f530273fe12d64863b917fa5cf9089de
1 /*
2 * dlls/rsaenh/implglue.c
3 * Glueing the RSAENH specific code to the crypto library
5 * Copyright (c) 2004, 2005 Michael Jung
6 * Copyright (c) 2007 Vijay Kiran Kamuju
8 * based on code by Mike McCormack and David Hammerton
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wincrypt.h"
31 #include "implglue.h"
33 /* Function prototype copied from dlls/advapi32/crypt.c */
34 BOOL WINAPI SystemFunction036(PVOID pbBuffer, ULONG dwLen);
36 BOOL init_hash_impl(ALG_ID aiAlgid, HASH_CONTEXT *pHashContext)
38 BCRYPT_ALG_HANDLE provider;
39 NTSTATUS status;
41 switch (aiAlgid)
43 case CALG_MD2:
44 status = BCryptOpenAlgorithmProvider(&provider, BCRYPT_MD2_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
45 break;
47 case CALG_MD4:
48 status = BCryptOpenAlgorithmProvider(&provider, BCRYPT_MD4_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
49 break;
51 case CALG_MD5:
52 status = BCryptOpenAlgorithmProvider(&provider, BCRYPT_MD5_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
53 break;
55 case CALG_SHA:
56 status = BCryptOpenAlgorithmProvider(&provider, BCRYPT_SHA1_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
57 break;
59 case CALG_SHA_256:
60 status = BCryptOpenAlgorithmProvider(&provider, BCRYPT_SHA256_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
61 break;
63 case CALG_SHA_384:
64 status = BCryptOpenAlgorithmProvider(&provider, BCRYPT_SHA384_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
65 break;
67 case CALG_SHA_512:
68 status = BCryptOpenAlgorithmProvider(&provider, BCRYPT_SHA512_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
69 break;
71 default:
72 return TRUE;
75 if (status) return FALSE;
77 status = BCryptCreateHash(provider, &pHashContext->bcrypt_hash, NULL, 0, NULL, 0, 0);
78 BCryptCloseAlgorithmProvider(provider, 0);
79 return !status;
82 BOOL update_hash_impl(HASH_CONTEXT *pHashContext, const BYTE *pbData, DWORD dwDataLen)
84 BCryptHashData(pHashContext->bcrypt_hash, (UCHAR*)pbData, dwDataLen, 0);
85 return TRUE;
88 BOOL finalize_hash_impl(HASH_CONTEXT *pHashContext, BYTE *pbHashValue)
90 BCryptFinishHash(pHashContext->bcrypt_hash, pbHashValue, RSAENH_MAX_HASH_SIZE, 0);
91 BCryptDestroyHash(pHashContext->bcrypt_hash);
92 return TRUE;
95 BOOL duplicate_hash_impl(const HASH_CONTEXT *pSrcHashContext, HASH_CONTEXT *pDestHashContext)
97 return !BCryptDuplicateHash(pSrcHashContext->bcrypt_hash, &pDestHashContext->bcrypt_hash, NULL, 0, 0);
100 BOOL new_key_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen)
102 switch (aiAlgid)
104 case CALG_RSA_KEYX:
105 case CALG_RSA_SIGN:
106 if (rsa_make_key((int)dwKeyLen, 65537, &pKeyContext->rsa) != CRYPT_OK) {
107 SetLastError(NTE_FAIL);
108 return FALSE;
110 return TRUE;
113 return TRUE;
116 BOOL free_key_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext)
118 switch (aiAlgid)
120 case CALG_RSA_KEYX:
121 case CALG_RSA_SIGN:
122 rsa_free(&pKeyContext->rsa);
125 return TRUE;
128 BOOL setup_key_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
129 DWORD dwEffectiveKeyLen, DWORD dwSaltLen, BYTE *abKeyValue)
131 switch (aiAlgid)
133 case CALG_RC4:
134 rc4_start(&pKeyContext->rc4);
135 rc4_add_entropy(abKeyValue, dwKeyLen + dwSaltLen, &pKeyContext->rc4);
136 rc4_ready(&pKeyContext->rc4);
137 break;
139 case CALG_RC2:
140 rc2_setup(abKeyValue, dwKeyLen + dwSaltLen, dwEffectiveKeyLen ?
141 dwEffectiveKeyLen : dwKeyLen << 3, 0, &pKeyContext->rc2);
142 break;
144 case CALG_3DES:
145 des3_setup(abKeyValue, 24, 0, &pKeyContext->des3);
146 break;
148 case CALG_3DES_112:
149 memcpy(abKeyValue+16, abKeyValue, 8);
150 des3_setup(abKeyValue, 24, 0, &pKeyContext->des3);
151 break;
153 case CALG_DES:
154 des_setup(abKeyValue, 8, 0, &pKeyContext->des);
155 break;
157 case CALG_AES:
158 case CALG_AES_128:
159 aes_setup(abKeyValue, 16, 0, &pKeyContext->aes);
160 break;
162 case CALG_AES_192:
163 aes_setup(abKeyValue, 24, 0, &pKeyContext->aes);
164 break;
166 case CALG_AES_256:
167 aes_setup(abKeyValue, 32, 0, &pKeyContext->aes);
168 break;
171 return TRUE;
174 BOOL duplicate_key_impl(ALG_ID aiAlgid, const KEY_CONTEXT *pSrcKeyContext,
175 KEY_CONTEXT *pDestKeyContext)
177 switch (aiAlgid)
179 case CALG_RC4:
180 case CALG_RC2:
181 case CALG_3DES:
182 case CALG_3DES_112:
183 case CALG_DES:
184 case CALG_AES:
185 case CALG_AES_128:
186 case CALG_AES_192:
187 case CALG_AES_256:
188 *pDestKeyContext = *pSrcKeyContext;
189 break;
190 case CALG_RSA_KEYX:
191 case CALG_RSA_SIGN:
192 pDestKeyContext->rsa.type = pSrcKeyContext->rsa.type;
193 mp_init_copy(&pDestKeyContext->rsa.e, &pSrcKeyContext->rsa.e);
194 mp_init_copy(&pDestKeyContext->rsa.d, &pSrcKeyContext->rsa.d);
195 mp_init_copy(&pDestKeyContext->rsa.N, &pSrcKeyContext->rsa.N);
196 mp_init_copy(&pDestKeyContext->rsa.p, &pSrcKeyContext->rsa.p);
197 mp_init_copy(&pDestKeyContext->rsa.q, &pSrcKeyContext->rsa.q);
198 mp_init_copy(&pDestKeyContext->rsa.qP, &pSrcKeyContext->rsa.qP);
199 mp_init_copy(&pDestKeyContext->rsa.dP, &pSrcKeyContext->rsa.dP);
200 mp_init_copy(&pDestKeyContext->rsa.dQ, &pSrcKeyContext->rsa.dQ);
201 break;
203 default:
204 SetLastError(NTE_BAD_ALGID);
205 return FALSE;
208 return TRUE;
211 static inline void reverse_bytes(BYTE *pbData, DWORD dwLen) {
212 BYTE swap;
213 DWORD i;
215 for (i=0; i<dwLen/2; i++) {
216 swap = pbData[i];
217 pbData[i] = pbData[dwLen-i-1];
218 pbData[dwLen-i-1] = swap;
222 BOOL encrypt_block_impl(ALG_ID aiAlgid, DWORD dwKeySpec, KEY_CONTEXT *pKeyContext, const BYTE *in,
223 BYTE *out, DWORD enc)
225 unsigned long inlen, outlen;
226 BYTE *in_reversed = NULL;
228 switch (aiAlgid) {
229 case CALG_RC2:
230 if (enc) {
231 rc2_ecb_encrypt(in, out, &pKeyContext->rc2);
232 } else {
233 rc2_ecb_decrypt(in, out, &pKeyContext->rc2);
235 break;
237 case CALG_3DES:
238 case CALG_3DES_112:
239 if (enc) {
240 des3_ecb_encrypt(in, out, &pKeyContext->des3);
241 } else {
242 des3_ecb_decrypt(in, out, &pKeyContext->des3);
244 break;
246 case CALG_DES:
247 if (enc) {
248 des_ecb_encrypt(in, out, &pKeyContext->des);
249 } else {
250 des_ecb_decrypt(in, out, &pKeyContext->des);
252 break;
254 case CALG_AES:
255 case CALG_AES_128:
256 case CALG_AES_192:
257 case CALG_AES_256:
258 if (enc) {
259 aes_ecb_encrypt(in, out, &pKeyContext->aes);
260 } else {
261 aes_ecb_decrypt(in, out, &pKeyContext->aes);
263 break;
265 case CALG_RSA_KEYX:
266 case CALG_RSA_SIGN:
267 case CALG_SSL3_SHAMD5:
268 outlen = inlen = (mp_count_bits(&pKeyContext->rsa.N)+7)/8;
269 if (enc) {
270 if (rsa_exptmod(in, inlen, out, &outlen, dwKeySpec, &pKeyContext->rsa) != CRYPT_OK) {
271 SetLastError(NTE_FAIL);
272 return FALSE;
274 reverse_bytes(out, outlen);
275 } else {
276 in_reversed = HeapAlloc(GetProcessHeap(), 0, inlen);
277 if (!in_reversed) {
278 SetLastError(NTE_NO_MEMORY);
279 return FALSE;
281 memcpy(in_reversed, in, inlen);
282 reverse_bytes(in_reversed, inlen);
283 if (rsa_exptmod(in_reversed, inlen, out, &outlen, dwKeySpec, &pKeyContext->rsa) != CRYPT_OK) {
284 HeapFree(GetProcessHeap(), 0, in_reversed);
285 SetLastError(NTE_FAIL);
286 return FALSE;
288 HeapFree(GetProcessHeap(), 0, in_reversed);
290 break;
292 default:
293 SetLastError(NTE_BAD_ALGID);
294 return FALSE;
297 return TRUE;
300 BOOL encrypt_stream_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, BYTE *stream, DWORD dwLen)
302 switch (aiAlgid) {
303 case CALG_RC4:
304 rc4_read(stream, dwLen, &pKeyContext->rc4);
305 break;
307 default:
308 SetLastError(NTE_BAD_ALGID);
309 return FALSE;
312 return TRUE;
315 BOOL gen_rand_impl(BYTE *pbBuffer, DWORD dwLen)
317 return SystemFunction036(pbBuffer, dwLen);
320 BOOL export_public_key_impl(BYTE *pbDest, const KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,DWORD *pdwPubExp)
322 mp_to_unsigned_bin(&pKeyContext->rsa.N, pbDest);
323 reverse_bytes(pbDest, mp_unsigned_bin_size(&pKeyContext->rsa.N));
324 if (mp_unsigned_bin_size(&pKeyContext->rsa.N) < dwKeyLen)
325 memset(pbDest + mp_unsigned_bin_size(&pKeyContext->rsa.N), 0,
326 dwKeyLen - mp_unsigned_bin_size(&pKeyContext->rsa.N));
327 *pdwPubExp = (DWORD)mp_get_int(&pKeyContext->rsa.e);
328 return TRUE;
331 BOOL import_public_key_impl(const BYTE *pbSrc, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
332 DWORD dwPubExp)
334 BYTE *pbTemp;
336 if (mp_init_multi(&pKeyContext->rsa.e, &pKeyContext->rsa.d, &pKeyContext->rsa.N,
337 &pKeyContext->rsa.dQ,&pKeyContext->rsa.dP,&pKeyContext->rsa.qP,
338 &pKeyContext->rsa.p, &pKeyContext->rsa.q, NULL) != MP_OKAY)
340 SetLastError(NTE_FAIL);
341 return FALSE;
344 pbTemp = HeapAlloc(GetProcessHeap(), 0, dwKeyLen);
345 if (!pbTemp) return FALSE;
346 memcpy(pbTemp, pbSrc, dwKeyLen);
348 pKeyContext->rsa.type = PK_PUBLIC;
349 reverse_bytes(pbTemp, dwKeyLen);
350 mp_read_unsigned_bin(&pKeyContext->rsa.N, pbTemp, dwKeyLen);
351 HeapFree(GetProcessHeap(), 0, pbTemp);
352 mp_set_int(&pKeyContext->rsa.e, dwPubExp);
354 return TRUE;
357 BOOL export_private_key_impl(BYTE *pbDest, const KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
358 DWORD *pdwPubExp)
360 mp_to_unsigned_bin(&pKeyContext->rsa.N, pbDest);
361 reverse_bytes(pbDest, mp_unsigned_bin_size(&pKeyContext->rsa.N));
362 if (mp_unsigned_bin_size(&pKeyContext->rsa.N) < dwKeyLen)
363 memset(pbDest + mp_unsigned_bin_size(&pKeyContext->rsa.N), 0,
364 dwKeyLen - mp_unsigned_bin_size(&pKeyContext->rsa.N));
365 pbDest += dwKeyLen;
366 mp_to_unsigned_bin(&pKeyContext->rsa.p, pbDest);
367 reverse_bytes(pbDest, mp_unsigned_bin_size(&pKeyContext->rsa.p));
368 if (mp_unsigned_bin_size(&pKeyContext->rsa.p) < (dwKeyLen+1)>>1)
369 memset(pbDest + mp_unsigned_bin_size(&pKeyContext->rsa.p), 0,
370 ((dwKeyLen+1)>>1) - mp_unsigned_bin_size(&pKeyContext->rsa.p));
371 pbDest += (dwKeyLen+1)>>1;
372 mp_to_unsigned_bin(&pKeyContext->rsa.q, pbDest);
373 reverse_bytes(pbDest, mp_unsigned_bin_size(&pKeyContext->rsa.q));
374 if (mp_unsigned_bin_size(&pKeyContext->rsa.q) < (dwKeyLen+1)>>1)
375 memset(pbDest + mp_unsigned_bin_size(&pKeyContext->rsa.q), 0,
376 ((dwKeyLen+1)>>1) - mp_unsigned_bin_size(&pKeyContext->rsa.q));
377 pbDest += (dwKeyLen+1)>>1;
378 mp_to_unsigned_bin(&pKeyContext->rsa.dP, pbDest);
379 reverse_bytes(pbDest, mp_unsigned_bin_size(&pKeyContext->rsa.dP));
380 if (mp_unsigned_bin_size(&pKeyContext->rsa.dP) < (dwKeyLen+1)>>1)
381 memset(pbDest + mp_unsigned_bin_size(&pKeyContext->rsa.dP), 0,
382 ((dwKeyLen+1)>>1) - mp_unsigned_bin_size(&pKeyContext->rsa.dP));
383 pbDest += (dwKeyLen+1)>>1;
384 mp_to_unsigned_bin(&pKeyContext->rsa.dQ, pbDest);
385 reverse_bytes(pbDest, mp_unsigned_bin_size(&pKeyContext->rsa.dQ));
386 if (mp_unsigned_bin_size(&pKeyContext->rsa.dQ) < (dwKeyLen+1)>>1)
387 memset(pbDest + mp_unsigned_bin_size(&pKeyContext->rsa.dQ), 0,
388 ((dwKeyLen+1)>>1) - mp_unsigned_bin_size(&pKeyContext->rsa.dQ));
389 pbDest += (dwKeyLen+1)>>1;
390 mp_to_unsigned_bin(&pKeyContext->rsa.qP, pbDest);
391 reverse_bytes(pbDest, mp_unsigned_bin_size(&pKeyContext->rsa.qP));
392 if (mp_unsigned_bin_size(&pKeyContext->rsa.qP) < (dwKeyLen+1)>>1)
393 memset(pbDest + mp_unsigned_bin_size(&pKeyContext->rsa.qP), 0,
394 ((dwKeyLen+1)>>1) - mp_unsigned_bin_size(&pKeyContext->rsa.qP));
395 pbDest += (dwKeyLen+1)>>1;
396 mp_to_unsigned_bin(&pKeyContext->rsa.d, pbDest);
397 reverse_bytes(pbDest, mp_unsigned_bin_size(&pKeyContext->rsa.d));
398 if (mp_unsigned_bin_size(&pKeyContext->rsa.d) < dwKeyLen)
399 memset(pbDest + mp_unsigned_bin_size(&pKeyContext->rsa.d), 0,
400 dwKeyLen - mp_unsigned_bin_size(&pKeyContext->rsa.d));
401 *pdwPubExp = (DWORD)mp_get_int(&pKeyContext->rsa.e);
403 return TRUE;
406 BOOL import_private_key_impl(const BYTE *pbSrc, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
407 DWORD dwDataLen, DWORD dwPubExp)
409 BYTE *pbTemp, *pbBigNum;
411 if (mp_init_multi(&pKeyContext->rsa.e, &pKeyContext->rsa.d, &pKeyContext->rsa.N,
412 &pKeyContext->rsa.dQ,&pKeyContext->rsa.dP,&pKeyContext->rsa.qP,
413 &pKeyContext->rsa.p, &pKeyContext->rsa.q, NULL) != MP_OKAY)
415 SetLastError(NTE_FAIL);
416 return FALSE;
419 pbTemp = HeapAlloc(GetProcessHeap(), 0, 2*dwKeyLen+5*((dwKeyLen+1)>>1));
420 if (!pbTemp) return FALSE;
421 memcpy(pbTemp, pbSrc, min(dwDataLen, 2*dwKeyLen+5*((dwKeyLen+1)>>1)));
422 pbBigNum = pbTemp;
424 pKeyContext->rsa.type = PK_PRIVATE;
425 reverse_bytes(pbBigNum, dwKeyLen);
426 mp_read_unsigned_bin(&pKeyContext->rsa.N, pbBigNum, dwKeyLen);
427 pbBigNum += dwKeyLen;
428 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
429 mp_read_unsigned_bin(&pKeyContext->rsa.p, pbBigNum, (dwKeyLen+1)>>1);
430 pbBigNum += (dwKeyLen+1)>>1;
431 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
432 mp_read_unsigned_bin(&pKeyContext->rsa.q, pbBigNum, (dwKeyLen+1)>>1);
433 pbBigNum += (dwKeyLen+1)>>1;
434 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
435 mp_read_unsigned_bin(&pKeyContext->rsa.dP, pbBigNum, (dwKeyLen+1)>>1);
436 pbBigNum += (dwKeyLen+1)>>1;
437 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
438 mp_read_unsigned_bin(&pKeyContext->rsa.dQ, pbBigNum, (dwKeyLen+1)>>1);
439 pbBigNum += (dwKeyLen+1)>>1;
440 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
441 mp_read_unsigned_bin(&pKeyContext->rsa.qP, pbBigNum, (dwKeyLen+1)>>1);
442 pbBigNum += (dwKeyLen+1)>>1;
443 /* The size of the private exponent d is inferred from the remaining
444 * data length.
446 dwKeyLen = min(dwKeyLen, dwDataLen - (pbBigNum - pbTemp));
447 reverse_bytes(pbBigNum, dwKeyLen);
448 mp_read_unsigned_bin(&pKeyContext->rsa.d, pbBigNum, dwKeyLen);
449 mp_set_int(&pKeyContext->rsa.e, dwPubExp);
451 HeapFree(GetProcessHeap(), 0, pbTemp);
452 return TRUE;