wine.inf: Create default spool dir.
[wine/multimedia.git] / dlls / rsaenh / implglue.c
blob68a9380f6a7260c431cdc0f9c7cbb9705a4d9759
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, dwKeyLen);
365 *pdwPubExp = (DWORD)mp_get_int(&pKeyContext->rsa.e);
366 return TRUE;
369 BOOL import_public_key_impl(CONST BYTE *pbSrc, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
370 DWORD dwPubExp)
372 BYTE *pbTemp;
374 if (mp_init_multi(&pKeyContext->rsa.e, &pKeyContext->rsa.d, &pKeyContext->rsa.N,
375 &pKeyContext->rsa.dQ,&pKeyContext->rsa.dP,&pKeyContext->rsa.qP,
376 &pKeyContext->rsa.p, &pKeyContext->rsa.q, NULL) != MP_OKAY)
378 SetLastError(NTE_FAIL);
379 return FALSE;
382 pbTemp = HeapAlloc(GetProcessHeap(), 0, dwKeyLen);
383 if (!pbTemp) return FALSE;
384 memcpy(pbTemp, pbSrc, dwKeyLen);
386 pKeyContext->rsa.type = PK_PUBLIC;
387 reverse_bytes(pbTemp, dwKeyLen);
388 mp_read_unsigned_bin(&pKeyContext->rsa.N, pbTemp, dwKeyLen);
389 HeapFree(GetProcessHeap(), 0, pbTemp);
390 mp_set_int(&pKeyContext->rsa.e, dwPubExp);
392 return TRUE;
395 BOOL export_private_key_impl(BYTE *pbDest, const KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
396 DWORD *pdwPubExp)
398 mp_to_unsigned_bin(&pKeyContext->rsa.N, pbDest);
399 reverse_bytes(pbDest, dwKeyLen);
400 pbDest += dwKeyLen;
401 mp_to_unsigned_bin(&pKeyContext->rsa.p, pbDest);
402 reverse_bytes(pbDest, (dwKeyLen+1)>>1);
403 pbDest += (dwKeyLen+1)>>1;
404 mp_to_unsigned_bin(&pKeyContext->rsa.q, pbDest);
405 reverse_bytes(pbDest, (dwKeyLen+1)>>1);
406 pbDest += (dwKeyLen+1)>>1;
407 mp_to_unsigned_bin(&pKeyContext->rsa.dP, pbDest);
408 reverse_bytes(pbDest, (dwKeyLen+1)>>1);
409 pbDest += (dwKeyLen+1)>>1;
410 mp_to_unsigned_bin(&pKeyContext->rsa.dQ, pbDest);
411 reverse_bytes(pbDest, (dwKeyLen+1)>>1);
412 pbDest += (dwKeyLen+1)>>1;
413 mp_to_unsigned_bin(&pKeyContext->rsa.qP, pbDest);
414 reverse_bytes(pbDest, (dwKeyLen+1)>>1);
415 pbDest += (dwKeyLen+1)>>1;
416 mp_to_unsigned_bin(&pKeyContext->rsa.d, pbDest);
417 reverse_bytes(pbDest, dwKeyLen);
418 *pdwPubExp = (DWORD)mp_get_int(&pKeyContext->rsa.e);
420 return TRUE;
423 BOOL import_private_key_impl(CONST BYTE *pbSrc, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
424 DWORD dwPubExp)
426 BYTE *pbTemp, *pbBigNum;
428 if (mp_init_multi(&pKeyContext->rsa.e, &pKeyContext->rsa.d, &pKeyContext->rsa.N,
429 &pKeyContext->rsa.dQ,&pKeyContext->rsa.dP,&pKeyContext->rsa.qP,
430 &pKeyContext->rsa.p, &pKeyContext->rsa.q, NULL) != MP_OKAY)
432 SetLastError(NTE_FAIL);
433 return FALSE;
436 pbTemp = HeapAlloc(GetProcessHeap(), 0, 2*dwKeyLen+5*((dwKeyLen+1)>>1));
437 if (!pbTemp) return FALSE;
438 memcpy(pbTemp, pbSrc, 2*dwKeyLen+5*((dwKeyLen+1)>>1));
439 pbBigNum = pbTemp;
441 pKeyContext->rsa.type = PK_PRIVATE;
442 reverse_bytes(pbBigNum, dwKeyLen);
443 mp_read_unsigned_bin(&pKeyContext->rsa.N, pbBigNum, dwKeyLen);
444 pbBigNum += dwKeyLen;
445 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
446 mp_read_unsigned_bin(&pKeyContext->rsa.p, pbBigNum, (dwKeyLen+1)>>1);
447 pbBigNum += (dwKeyLen+1)>>1;
448 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
449 mp_read_unsigned_bin(&pKeyContext->rsa.q, pbBigNum, (dwKeyLen+1)>>1);
450 pbBigNum += (dwKeyLen+1)>>1;
451 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
452 mp_read_unsigned_bin(&pKeyContext->rsa.dP, pbBigNum, (dwKeyLen+1)>>1);
453 pbBigNum += (dwKeyLen+1)>>1;
454 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
455 mp_read_unsigned_bin(&pKeyContext->rsa.dQ, pbBigNum, (dwKeyLen+1)>>1);
456 pbBigNum += (dwKeyLen+1)>>1;
457 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
458 mp_read_unsigned_bin(&pKeyContext->rsa.qP, pbBigNum, (dwKeyLen+1)>>1);
459 pbBigNum += (dwKeyLen+1)>>1;
460 reverse_bytes(pbBigNum, dwKeyLen);
461 mp_read_unsigned_bin(&pKeyContext->rsa.d, pbBigNum, dwKeyLen);
462 mp_set_int(&pKeyContext->rsa.e, dwPubExp);
464 HeapFree(GetProcessHeap(), 0, pbTemp);
465 return TRUE;