ole32: Rename PROPSET_BLOCK_SIZE to RAW_DIRENTRY_SIZE.
[wine/testsucceed.git] / dlls / rsaenh / implglue.c
blobb73eba93ec65676e5ea9b3679a74d7bfae83eaf3
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
24 #include "config.h"
26 #include "wine/port.h"
27 #include "wine/library.h"
29 #include "windef.h"
30 #include "wincrypt.h"
32 #include "implglue.h"
34 #include <stdio.h>
36 /* Function prototypes copied from dlls/advapi32/crypt_md4.c */
37 VOID WINAPI MD4Init( MD4_CTX *ctx );
38 VOID WINAPI MD4Update( MD4_CTX *ctx, const unsigned char *buf, unsigned int len );
39 VOID WINAPI MD4Final( MD4_CTX *ctx );
40 /* Function prototypes copied from dlls/advapi32/crypt_md5.c */
41 VOID WINAPI MD5Init( MD5_CTX *ctx );
42 VOID WINAPI MD5Update( MD5_CTX *ctx, const unsigned char *buf, unsigned int len );
43 VOID WINAPI MD5Final( MD5_CTX *ctx );
44 /* Function prototypes copied from dlls/advapi32/crypt_sha.c */
45 VOID WINAPI A_SHAInit(PSHA_CTX Context);
46 VOID WINAPI A_SHAUpdate(PSHA_CTX Context, const unsigned char *Buffer, UINT BufferSize);
47 VOID WINAPI A_SHAFinal(PSHA_CTX Context, PULONG Result);
48 /* Function prototype copied from dlls/advapi32/crypt.c */
49 BOOL WINAPI SystemFunction036(PVOID pbBuffer, ULONG dwLen);
51 BOOL init_hash_impl(ALG_ID aiAlgid, HASH_CONTEXT *pHashContext)
53 switch (aiAlgid)
55 case CALG_MD2:
56 md2_init(&pHashContext->md2);
57 break;
59 case CALG_MD4:
60 MD4Init(&pHashContext->md4);
61 break;
63 case CALG_MD5:
64 MD5Init(&pHashContext->md5);
65 break;
67 case CALG_SHA:
68 A_SHAInit(&pHashContext->sha);
69 break;
72 return TRUE;
75 BOOL update_hash_impl(ALG_ID aiAlgid, HASH_CONTEXT *pHashContext, CONST BYTE *pbData,
76 DWORD dwDataLen)
78 switch (aiAlgid)
80 case CALG_MD2:
81 md2_process(&pHashContext->md2, pbData, dwDataLen);
82 break;
84 case CALG_MD4:
85 MD4Update(&pHashContext->md4, pbData, dwDataLen);
86 break;
88 case CALG_MD5:
89 MD5Update(&pHashContext->md5, pbData, dwDataLen);
90 break;
92 case CALG_SHA:
93 A_SHAUpdate(&pHashContext->sha, pbData, dwDataLen);
94 break;
96 default:
97 SetLastError(NTE_BAD_ALGID);
98 return FALSE;
101 return TRUE;
104 BOOL finalize_hash_impl(ALG_ID aiAlgid, HASH_CONTEXT *pHashContext, BYTE *pbHashValue)
106 switch (aiAlgid)
108 case CALG_MD2:
109 md2_done(&pHashContext->md2, pbHashValue);
110 break;
112 case CALG_MD4:
113 MD4Final(&pHashContext->md4);
114 memcpy(pbHashValue, pHashContext->md4.digest, 16);
115 break;
117 case CALG_MD5:
118 MD5Final(&pHashContext->md5);
119 memcpy(pbHashValue, pHashContext->md5.digest, 16);
120 break;
122 case CALG_SHA:
123 A_SHAFinal(&pHashContext->sha, (PULONG)pbHashValue);
124 break;
126 default:
127 SetLastError(NTE_BAD_ALGID);
128 return FALSE;
131 return TRUE;
134 BOOL duplicate_hash_impl(ALG_ID aiAlgid, CONST HASH_CONTEXT *pSrcHashContext,
135 HASH_CONTEXT *pDestHashContext)
137 *pDestHashContext = *pSrcHashContext;
139 return TRUE;
142 BOOL new_key_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen)
144 switch (aiAlgid)
146 case CALG_RSA_KEYX:
147 case CALG_RSA_SIGN:
148 if (rsa_make_key((int)dwKeyLen, 65537, &pKeyContext->rsa) != CRYPT_OK) {
149 SetLastError(NTE_FAIL);
150 return FALSE;
152 return TRUE;
155 return TRUE;
158 BOOL free_key_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext)
160 switch (aiAlgid)
162 case CALG_RSA_KEYX:
163 case CALG_RSA_SIGN:
164 rsa_free(&pKeyContext->rsa);
167 return TRUE;
170 BOOL setup_key_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
171 DWORD dwEffectiveKeyLen, DWORD dwSaltLen, BYTE *abKeyValue)
173 switch (aiAlgid)
175 case CALG_RC4:
176 rc4_start(&pKeyContext->rc4);
177 rc4_add_entropy(abKeyValue, dwKeyLen + dwSaltLen, &pKeyContext->rc4);
178 rc4_ready(&pKeyContext->rc4);
179 break;
181 case CALG_RC2:
182 rc2_setup(abKeyValue, dwKeyLen + dwSaltLen, dwEffectiveKeyLen ?
183 dwEffectiveKeyLen : dwKeyLen << 3, 0, &pKeyContext->rc2);
184 break;
186 case CALG_3DES:
187 des3_setup(abKeyValue, 24, 0, &pKeyContext->des3);
188 break;
190 case CALG_3DES_112:
191 memcpy(abKeyValue+16, abKeyValue, 8);
192 des3_setup(abKeyValue, 24, 0, &pKeyContext->des3);
193 break;
195 case CALG_DES:
196 des_setup(abKeyValue, 8, 0, &pKeyContext->des);
197 break;
199 case CALG_AES:
200 case CALG_AES_128:
201 aes_setup(abKeyValue, 16, 0, &pKeyContext->aes);
202 break;
204 case CALG_AES_192:
205 aes_setup(abKeyValue, 24, 0, &pKeyContext->aes);
206 break;
208 case CALG_AES_256:
209 aes_setup(abKeyValue, 32, 0, &pKeyContext->aes);
210 break;
213 return TRUE;
216 BOOL duplicate_key_impl(ALG_ID aiAlgid, CONST KEY_CONTEXT *pSrcKeyContext,
217 KEY_CONTEXT *pDestKeyContext)
219 switch (aiAlgid)
221 case CALG_RC4:
222 case CALG_RC2:
223 case CALG_3DES:
224 case CALG_3DES_112:
225 case CALG_DES:
226 case CALG_AES:
227 case CALG_AES_128:
228 case CALG_AES_192:
229 case CALG_AES_256:
230 *pDestKeyContext = *pSrcKeyContext;
231 break;
232 case CALG_RSA_KEYX:
233 case CALG_RSA_SIGN:
234 pDestKeyContext->rsa.type = pSrcKeyContext->rsa.type;
235 mp_init_copy(&pDestKeyContext->rsa.e, &pSrcKeyContext->rsa.e);
236 mp_init_copy(&pDestKeyContext->rsa.d, &pSrcKeyContext->rsa.d);
237 mp_init_copy(&pDestKeyContext->rsa.N, &pSrcKeyContext->rsa.N);
238 mp_init_copy(&pDestKeyContext->rsa.p, &pSrcKeyContext->rsa.p);
239 mp_init_copy(&pDestKeyContext->rsa.q, &pSrcKeyContext->rsa.q);
240 mp_init_copy(&pDestKeyContext->rsa.qP, &pSrcKeyContext->rsa.qP);
241 mp_init_copy(&pDestKeyContext->rsa.dP, &pSrcKeyContext->rsa.dP);
242 mp_init_copy(&pDestKeyContext->rsa.dQ, &pSrcKeyContext->rsa.dQ);
243 break;
245 default:
246 SetLastError(NTE_BAD_ALGID);
247 return FALSE;
250 return TRUE;
253 static inline void reverse_bytes(BYTE *pbData, DWORD dwLen) {
254 BYTE swap;
255 DWORD i;
257 for (i=0; i<dwLen/2; i++) {
258 swap = pbData[i];
259 pbData[i] = pbData[dwLen-i-1];
260 pbData[dwLen-i-1] = swap;
264 BOOL encrypt_block_impl(ALG_ID aiAlgid, DWORD dwKeySpec, KEY_CONTEXT *pKeyContext, CONST BYTE *in, BYTE *out,
265 DWORD enc)
267 unsigned long inlen, outlen;
268 BYTE *in_reversed = NULL;
270 switch (aiAlgid) {
271 case CALG_RC2:
272 if (enc) {
273 rc2_ecb_encrypt(in, out, &pKeyContext->rc2);
274 } else {
275 rc2_ecb_decrypt(in, out, &pKeyContext->rc2);
277 break;
279 case CALG_3DES:
280 case CALG_3DES_112:
281 if (enc) {
282 des3_ecb_encrypt(in, out, &pKeyContext->des3);
283 } else {
284 des3_ecb_decrypt(in, out, &pKeyContext->des3);
286 break;
288 case CALG_DES:
289 if (enc) {
290 des_ecb_encrypt(in, out, &pKeyContext->des);
291 } else {
292 des_ecb_decrypt(in, out, &pKeyContext->des);
294 break;
296 case CALG_AES:
297 case CALG_AES_128:
298 case CALG_AES_192:
299 case CALG_AES_256:
300 if (enc) {
301 aes_ecb_encrypt(in, out, &pKeyContext->aes);
302 } else {
303 aes_ecb_decrypt(in, out, &pKeyContext->aes);
305 break;
307 case CALG_RSA_KEYX:
308 case CALG_RSA_SIGN:
309 outlen = inlen = (mp_count_bits(&pKeyContext->rsa.N)+7)/8;
310 if (enc) {
311 if (rsa_exptmod(in, inlen, out, &outlen, dwKeySpec, &pKeyContext->rsa) != CRYPT_OK) {
312 SetLastError(NTE_FAIL);
313 return FALSE;
315 reverse_bytes(out, outlen);
316 } else {
317 in_reversed = HeapAlloc(GetProcessHeap(), 0, inlen);
318 if (!in_reversed) {
319 SetLastError(NTE_NO_MEMORY);
320 return FALSE;
322 memcpy(in_reversed, in, inlen);
323 reverse_bytes(in_reversed, inlen);
324 if (rsa_exptmod(in_reversed, inlen, out, &outlen, dwKeySpec, &pKeyContext->rsa) != CRYPT_OK) {
325 HeapFree(GetProcessHeap(), 0, in_reversed);
326 SetLastError(NTE_FAIL);
327 return FALSE;
329 HeapFree(GetProcessHeap(), 0, in_reversed);
331 break;
333 default:
334 SetLastError(NTE_BAD_ALGID);
335 return FALSE;
338 return TRUE;
341 BOOL encrypt_stream_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, BYTE *stream, DWORD dwLen)
343 switch (aiAlgid) {
344 case CALG_RC4:
345 rc4_read(stream, dwLen, &pKeyContext->rc4);
346 break;
348 default:
349 SetLastError(NTE_BAD_ALGID);
350 return FALSE;
353 return TRUE;
356 BOOL gen_rand_impl(BYTE *pbBuffer, DWORD dwLen)
358 return SystemFunction036(pbBuffer, dwLen);
361 BOOL export_public_key_impl(BYTE *pbDest, const KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,DWORD *pdwPubExp)
363 mp_to_unsigned_bin(&pKeyContext->rsa.N, pbDest);
364 reverse_bytes(pbDest, mp_unsigned_bin_size(&pKeyContext->rsa.N));
365 if (mp_unsigned_bin_size(&pKeyContext->rsa.N) < dwKeyLen)
366 memset(pbDest + mp_unsigned_bin_size(&pKeyContext->rsa.N), 0,
367 dwKeyLen - mp_unsigned_bin_size(&pKeyContext->rsa.N));
368 *pdwPubExp = (DWORD)mp_get_int(&pKeyContext->rsa.e);
369 return TRUE;
372 BOOL import_public_key_impl(CONST BYTE *pbSrc, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
373 DWORD dwPubExp)
375 BYTE *pbTemp;
377 if (mp_init_multi(&pKeyContext->rsa.e, &pKeyContext->rsa.d, &pKeyContext->rsa.N,
378 &pKeyContext->rsa.dQ,&pKeyContext->rsa.dP,&pKeyContext->rsa.qP,
379 &pKeyContext->rsa.p, &pKeyContext->rsa.q, NULL) != MP_OKAY)
381 SetLastError(NTE_FAIL);
382 return FALSE;
385 pbTemp = HeapAlloc(GetProcessHeap(), 0, dwKeyLen);
386 if (!pbTemp) return FALSE;
387 memcpy(pbTemp, pbSrc, dwKeyLen);
389 pKeyContext->rsa.type = PK_PUBLIC;
390 reverse_bytes(pbTemp, dwKeyLen);
391 mp_read_unsigned_bin(&pKeyContext->rsa.N, pbTemp, dwKeyLen);
392 HeapFree(GetProcessHeap(), 0, pbTemp);
393 mp_set_int(&pKeyContext->rsa.e, dwPubExp);
395 return TRUE;
398 BOOL export_private_key_impl(BYTE *pbDest, const KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
399 DWORD *pdwPubExp)
401 mp_to_unsigned_bin(&pKeyContext->rsa.N, pbDest);
402 reverse_bytes(pbDest, mp_unsigned_bin_size(&pKeyContext->rsa.N));
403 if (mp_unsigned_bin_size(&pKeyContext->rsa.N) < dwKeyLen)
404 memset(pbDest + mp_unsigned_bin_size(&pKeyContext->rsa.N), 0,
405 dwKeyLen - mp_unsigned_bin_size(&pKeyContext->rsa.N));
406 pbDest += dwKeyLen;
407 mp_to_unsigned_bin(&pKeyContext->rsa.p, pbDest);
408 reverse_bytes(pbDest, mp_unsigned_bin_size(&pKeyContext->rsa.p));
409 if (mp_unsigned_bin_size(&pKeyContext->rsa.p) < (dwKeyLen+1)>>1)
410 memset(pbDest + mp_unsigned_bin_size(&pKeyContext->rsa.p), 0,
411 ((dwKeyLen+1)>>1) - mp_unsigned_bin_size(&pKeyContext->rsa.p));
412 pbDest += (dwKeyLen+1)>>1;
413 mp_to_unsigned_bin(&pKeyContext->rsa.q, pbDest);
414 reverse_bytes(pbDest, mp_unsigned_bin_size(&pKeyContext->rsa.q));
415 if (mp_unsigned_bin_size(&pKeyContext->rsa.q) < (dwKeyLen+1)>>1)
416 memset(pbDest + mp_unsigned_bin_size(&pKeyContext->rsa.q), 0,
417 ((dwKeyLen+1)>>1) - mp_unsigned_bin_size(&pKeyContext->rsa.q));
418 pbDest += (dwKeyLen+1)>>1;
419 mp_to_unsigned_bin(&pKeyContext->rsa.dP, pbDest);
420 reverse_bytes(pbDest, mp_unsigned_bin_size(&pKeyContext->rsa.dP));
421 if (mp_unsigned_bin_size(&pKeyContext->rsa.dP) < (dwKeyLen+1)>>1)
422 memset(pbDest + mp_unsigned_bin_size(&pKeyContext->rsa.dP), 0,
423 ((dwKeyLen+1)>>1) - mp_unsigned_bin_size(&pKeyContext->rsa.dP));
424 pbDest += (dwKeyLen+1)>>1;
425 mp_to_unsigned_bin(&pKeyContext->rsa.dQ, pbDest);
426 reverse_bytes(pbDest, mp_unsigned_bin_size(&pKeyContext->rsa.dQ));
427 if (mp_unsigned_bin_size(&pKeyContext->rsa.dQ) < (dwKeyLen+1)>>1)
428 memset(pbDest + mp_unsigned_bin_size(&pKeyContext->rsa.dQ), 0,
429 ((dwKeyLen+1)>>1) - mp_unsigned_bin_size(&pKeyContext->rsa.dQ));
430 pbDest += (dwKeyLen+1)>>1;
431 mp_to_unsigned_bin(&pKeyContext->rsa.qP, pbDest);
432 reverse_bytes(pbDest, mp_unsigned_bin_size(&pKeyContext->rsa.qP));
433 if (mp_unsigned_bin_size(&pKeyContext->rsa.qP) < (dwKeyLen+1)>>1)
434 memset(pbDest + mp_unsigned_bin_size(&pKeyContext->rsa.qP), 0,
435 ((dwKeyLen+1)>>1) - mp_unsigned_bin_size(&pKeyContext->rsa.qP));
436 pbDest += (dwKeyLen+1)>>1;
437 mp_to_unsigned_bin(&pKeyContext->rsa.d, pbDest);
438 reverse_bytes(pbDest, mp_unsigned_bin_size(&pKeyContext->rsa.d));
439 if (mp_unsigned_bin_size(&pKeyContext->rsa.d) < dwKeyLen)
440 memset(pbDest + mp_unsigned_bin_size(&pKeyContext->rsa.d), 0,
441 dwKeyLen - mp_unsigned_bin_size(&pKeyContext->rsa.d));
442 *pdwPubExp = (DWORD)mp_get_int(&pKeyContext->rsa.e);
444 return TRUE;
447 BOOL import_private_key_impl(CONST BYTE *pbSrc, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
448 DWORD dwPubExp)
450 BYTE *pbTemp, *pbBigNum;
452 if (mp_init_multi(&pKeyContext->rsa.e, &pKeyContext->rsa.d, &pKeyContext->rsa.N,
453 &pKeyContext->rsa.dQ,&pKeyContext->rsa.dP,&pKeyContext->rsa.qP,
454 &pKeyContext->rsa.p, &pKeyContext->rsa.q, NULL) != MP_OKAY)
456 SetLastError(NTE_FAIL);
457 return FALSE;
460 pbTemp = HeapAlloc(GetProcessHeap(), 0, 2*dwKeyLen+5*((dwKeyLen+1)>>1));
461 if (!pbTemp) return FALSE;
462 memcpy(pbTemp, pbSrc, 2*dwKeyLen+5*((dwKeyLen+1)>>1));
463 pbBigNum = pbTemp;
465 pKeyContext->rsa.type = PK_PRIVATE;
466 reverse_bytes(pbBigNum, dwKeyLen);
467 mp_read_unsigned_bin(&pKeyContext->rsa.N, pbBigNum, dwKeyLen);
468 pbBigNum += dwKeyLen;
469 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
470 mp_read_unsigned_bin(&pKeyContext->rsa.p, pbBigNum, (dwKeyLen+1)>>1);
471 pbBigNum += (dwKeyLen+1)>>1;
472 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
473 mp_read_unsigned_bin(&pKeyContext->rsa.q, pbBigNum, (dwKeyLen+1)>>1);
474 pbBigNum += (dwKeyLen+1)>>1;
475 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
476 mp_read_unsigned_bin(&pKeyContext->rsa.dP, pbBigNum, (dwKeyLen+1)>>1);
477 pbBigNum += (dwKeyLen+1)>>1;
478 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
479 mp_read_unsigned_bin(&pKeyContext->rsa.dQ, pbBigNum, (dwKeyLen+1)>>1);
480 pbBigNum += (dwKeyLen+1)>>1;
481 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
482 mp_read_unsigned_bin(&pKeyContext->rsa.qP, pbBigNum, (dwKeyLen+1)>>1);
483 pbBigNum += (dwKeyLen+1)>>1;
484 reverse_bytes(pbBigNum, dwKeyLen);
485 mp_read_unsigned_bin(&pKeyContext->rsa.d, pbBigNum, dwKeyLen);
486 mp_set_int(&pKeyContext->rsa.e, dwPubExp);
488 HeapFree(GetProcessHeap(), 0, pbTemp);
489 return TRUE;