msi: Create macro functions to set feature and component states.
[wine.git] / dlls / rsaenh / implglue.c
blob30f3cb6d5a9833bedd594346c88f0b5608832d5f
1 /*
2 * dlls/rsaenh/implglue.c
3 * Glueing the RSAENH specific code to the crypto library
5 * Copyright (c) 2004, 2005 Michael Jung
7 * based on code by Mike McCormack and David Hammerton
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
25 #include "wine/port.h"
26 #include "wine/library.h"
28 #include "windef.h"
29 #include "wincrypt.h"
31 #include "implglue.h"
33 #include <stdio.h>
35 /* Function prototypes copied from dlls/advapi32/crypt_md4.c */
36 VOID WINAPI MD4Init( MD4_CTX *ctx );
37 VOID WINAPI MD4Update( MD4_CTX *ctx, const unsigned char *buf, unsigned int len );
38 VOID WINAPI MD4Final( MD4_CTX *ctx );
39 /* Function prototypes copied from dlls/advapi32/crypt_md5.c */
40 VOID WINAPI MD5Init( MD5_CTX *ctx );
41 VOID WINAPI MD5Update( MD5_CTX *ctx, const unsigned char *buf, unsigned int len );
42 VOID WINAPI MD5Final( MD5_CTX *ctx );
43 /* Function prototypes copied from dlls/advapi32/crypt_sha.c */
44 VOID WINAPI A_SHAInit(PSHA_CTX Context);
45 VOID WINAPI A_SHAUpdate(PSHA_CTX Context, PCHAR Buffer, UINT BufferSize);
46 VOID WINAPI A_SHAFinal(PSHA_CTX Context, PULONG Result);
47 /* Function prototype copied from dlls/advapi32/crypt.c */
48 BOOL WINAPI SystemFunction036(PVOID pbBuffer, ULONG dwLen);
50 BOOL init_hash_impl(ALG_ID aiAlgid, HASH_CONTEXT *pHashContext)
52 switch (aiAlgid)
54 case CALG_MD2:
55 md2_init(&pHashContext->md2);
56 break;
58 case CALG_MD4:
59 MD4Init(&pHashContext->md4);
60 break;
62 case CALG_MD5:
63 MD5Init(&pHashContext->md5);
64 break;
66 case CALG_SHA:
67 A_SHAInit(&pHashContext->sha);
68 break;
71 return TRUE;
74 BOOL update_hash_impl(ALG_ID aiAlgid, HASH_CONTEXT *pHashContext, CONST BYTE *pbData,
75 DWORD dwDataLen)
77 switch (aiAlgid)
79 case CALG_MD2:
80 md2_process(&pHashContext->md2, pbData, dwDataLen);
81 break;
83 case CALG_MD4:
84 MD4Update(&pHashContext->md4, pbData, dwDataLen);
85 break;
87 case CALG_MD5:
88 MD5Update(&pHashContext->md5, pbData, dwDataLen);
89 break;
91 case CALG_SHA:
92 A_SHAUpdate(&pHashContext->sha, (PCHAR)pbData, dwDataLen);
93 break;
95 default:
96 SetLastError(NTE_BAD_ALGID);
97 return FALSE;
100 return TRUE;
103 BOOL finalize_hash_impl(ALG_ID aiAlgid, HASH_CONTEXT *pHashContext, BYTE *pbHashValue)
105 switch (aiAlgid)
107 case CALG_MD2:
108 md2_done(&pHashContext->md2, pbHashValue);
109 break;
111 case CALG_MD4:
112 MD4Final(&pHashContext->md4);
113 memcpy(pbHashValue, pHashContext->md4.digest, 16);
114 break;
116 case CALG_MD5:
117 MD5Final(&pHashContext->md5);
118 memcpy(pbHashValue, pHashContext->md5.digest, 16);
119 break;
121 case CALG_SHA:
122 A_SHAFinal(&pHashContext->sha, (PULONG)pbHashValue);
123 break;
125 default:
126 SetLastError(NTE_BAD_ALGID);
127 return FALSE;
130 return TRUE;
133 BOOL duplicate_hash_impl(ALG_ID aiAlgid, CONST HASH_CONTEXT *pSrcHashContext,
134 HASH_CONTEXT *pDestHashContext)
136 memcpy(pDestHashContext, pSrcHashContext, sizeof(HASH_CONTEXT));
138 return TRUE;
141 BOOL new_key_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen)
143 switch (aiAlgid)
145 case CALG_RSA_KEYX:
146 case CALG_RSA_SIGN:
147 if (rsa_make_key((int)dwKeyLen, 65537, &pKeyContext->rsa) != CRYPT_OK) {
148 SetLastError(NTE_FAIL);
149 return FALSE;
151 return TRUE;
154 return TRUE;
157 BOOL free_key_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext)
159 switch (aiAlgid)
161 case CALG_RSA_KEYX:
162 case CALG_RSA_SIGN:
163 rsa_free(&pKeyContext->rsa);
166 return TRUE;
169 BOOL setup_key_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen, DWORD dwSaltLen,
170 BYTE *abKeyValue)
172 switch (aiAlgid)
174 case CALG_RC4:
175 rc4_start(&pKeyContext->rc4);
176 rc4_add_entropy(abKeyValue, dwKeyLen + dwSaltLen, &pKeyContext->rc4);
177 rc4_ready(&pKeyContext->rc4);
178 break;
180 case CALG_RC2:
181 rc2_setup(abKeyValue, dwKeyLen + dwSaltLen, dwKeyLen << 3, 0, &pKeyContext->rc2);
182 break;
184 case CALG_3DES:
185 des3_setup(abKeyValue, 24, 0, &pKeyContext->des3);
186 break;
188 case CALG_3DES_112:
189 memcpy(abKeyValue+16, abKeyValue, 8);
190 des3_setup(abKeyValue, 24, 0, &pKeyContext->des3);
191 break;
193 case CALG_DES:
194 des_setup(abKeyValue, 8, 0, &pKeyContext->des);
195 break;
198 return TRUE;
201 BOOL duplicate_key_impl(ALG_ID aiAlgid, CONST KEY_CONTEXT *pSrcKeyContext,
202 KEY_CONTEXT *pDestKeyContext)
204 switch (aiAlgid)
206 case CALG_RC4:
207 case CALG_RC2:
208 case CALG_3DES:
209 case CALG_3DES_112:
210 case CALG_DES:
211 memcpy(pDestKeyContext, pSrcKeyContext, sizeof(KEY_CONTEXT));
212 break;
213 case CALG_RSA_KEYX:
214 case CALG_RSA_SIGN:
215 pDestKeyContext->rsa.type = pSrcKeyContext->rsa.type;
216 mp_init_copy(&pDestKeyContext->rsa.e, &pSrcKeyContext->rsa.e);
217 mp_init_copy(&pDestKeyContext->rsa.d, &pSrcKeyContext->rsa.d);
218 mp_init_copy(&pDestKeyContext->rsa.N, &pSrcKeyContext->rsa.N);
219 mp_init_copy(&pDestKeyContext->rsa.p, &pSrcKeyContext->rsa.p);
220 mp_init_copy(&pDestKeyContext->rsa.q, &pSrcKeyContext->rsa.q);
221 mp_init_copy(&pDestKeyContext->rsa.qP, &pSrcKeyContext->rsa.qP);
222 mp_init_copy(&pDestKeyContext->rsa.dP, &pSrcKeyContext->rsa.dP);
223 mp_init_copy(&pDestKeyContext->rsa.dQ, &pSrcKeyContext->rsa.dQ);
224 break;
226 default:
227 SetLastError(NTE_BAD_ALGID);
228 return FALSE;
231 return TRUE;
234 static inline void reverse_bytes(BYTE *pbData, DWORD dwLen) {
235 BYTE swap;
236 DWORD i;
238 for (i=0; i<dwLen/2; i++) {
239 swap = pbData[i];
240 pbData[i] = pbData[dwLen-i-1];
241 pbData[dwLen-i-1] = swap;
245 BOOL encrypt_block_impl(ALG_ID aiAlgid, DWORD dwKeySpec, KEY_CONTEXT *pKeyContext, CONST BYTE *in, BYTE *out,
246 DWORD enc)
248 unsigned long inlen, outlen;
249 BYTE *in_reversed = NULL;
251 switch (aiAlgid) {
252 case CALG_RC2:
253 if (enc) {
254 rc2_ecb_encrypt(in, out, &pKeyContext->rc2);
255 } else {
256 rc2_ecb_decrypt(in, out, &pKeyContext->rc2);
258 break;
260 case CALG_3DES:
261 case CALG_3DES_112:
262 if (enc) {
263 des3_ecb_encrypt(in, out, &pKeyContext->des3);
264 } else {
265 des3_ecb_decrypt(in, out, &pKeyContext->des3);
267 break;
269 case CALG_DES:
270 if (enc) {
271 des_ecb_encrypt(in, out, &pKeyContext->des);
272 } else {
273 des_ecb_decrypt(in, out, &pKeyContext->des);
275 break;
277 case CALG_RSA_KEYX:
278 case CALG_RSA_SIGN:
279 outlen = inlen = (mp_count_bits(&pKeyContext->rsa.N)+7)/8;
280 if (enc) {
281 if (rsa_exptmod(in, inlen, out, &outlen, dwKeySpec, &pKeyContext->rsa) != CRYPT_OK) {
282 SetLastError(NTE_FAIL);
283 return FALSE;
285 reverse_bytes(out, outlen);
286 } else {
287 in_reversed = HeapAlloc(GetProcessHeap(), 0, inlen);
288 if (!in_reversed) {
289 SetLastError(NTE_NO_MEMORY);
290 return FALSE;
292 memcpy(in_reversed, in, inlen);
293 reverse_bytes(in_reversed, inlen);
294 if (rsa_exptmod(in_reversed, inlen, out, &outlen, dwKeySpec, &pKeyContext->rsa) != CRYPT_OK) {
295 HeapFree(GetProcessHeap(), 0, in_reversed);
296 SetLastError(NTE_FAIL);
297 return FALSE;
299 HeapFree(GetProcessHeap(), 0, in_reversed);
301 break;
303 default:
304 SetLastError(NTE_BAD_ALGID);
305 return FALSE;
308 return TRUE;
311 BOOL encrypt_stream_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, BYTE *stream, DWORD dwLen)
313 switch (aiAlgid) {
314 case CALG_RC4:
315 rc4_read(stream, dwLen, &pKeyContext->rc4);
316 break;
318 default:
319 SetLastError(NTE_BAD_ALGID);
320 return FALSE;
323 return TRUE;
326 BOOL gen_rand_impl(BYTE *pbBuffer, DWORD dwLen)
328 return SystemFunction036(pbBuffer, dwLen);
331 BOOL export_public_key_impl(BYTE *pbDest, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,DWORD *pdwPubExp)
333 mp_to_unsigned_bin(&pKeyContext->rsa.N, pbDest);
334 reverse_bytes(pbDest, dwKeyLen);
335 *pdwPubExp = (DWORD)mp_get_int(&pKeyContext->rsa.e);
336 return TRUE;
339 BOOL import_public_key_impl(CONST BYTE *pbSrc, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
340 DWORD dwPubExp)
342 BYTE *pbTemp;
344 if (mp_init_multi(&pKeyContext->rsa.e, &pKeyContext->rsa.d, &pKeyContext->rsa.N,
345 &pKeyContext->rsa.dQ,&pKeyContext->rsa.dP,&pKeyContext->rsa.qP,
346 &pKeyContext->rsa.p, &pKeyContext->rsa.q, NULL) != MP_OKAY)
348 SetLastError(NTE_FAIL);
349 return FALSE;
352 pbTemp = HeapAlloc(GetProcessHeap(), 0, dwKeyLen);
353 if (!pbTemp) return FALSE;
354 memcpy(pbTemp, pbSrc, dwKeyLen);
356 pKeyContext->rsa.type = PK_PUBLIC;
357 reverse_bytes(pbTemp, dwKeyLen);
358 mp_read_unsigned_bin(&pKeyContext->rsa.N, pbTemp, dwKeyLen);
359 HeapFree(GetProcessHeap(), 0, pbTemp);
360 mp_set_int(&pKeyContext->rsa.e, dwPubExp);
362 return TRUE;
365 BOOL export_private_key_impl(BYTE *pbDest, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
366 DWORD *pdwPubExp)
368 mp_to_unsigned_bin(&pKeyContext->rsa.N, pbDest);
369 reverse_bytes(pbDest, dwKeyLen);
370 pbDest += dwKeyLen;
371 mp_to_unsigned_bin(&pKeyContext->rsa.p, pbDest);
372 reverse_bytes(pbDest, (dwKeyLen+1)>>1);
373 pbDest += (dwKeyLen+1)>>1;
374 mp_to_unsigned_bin(&pKeyContext->rsa.q, pbDest);
375 reverse_bytes(pbDest, (dwKeyLen+1)>>1);
376 pbDest += (dwKeyLen+1)>>1;
377 mp_to_unsigned_bin(&pKeyContext->rsa.dP, pbDest);
378 reverse_bytes(pbDest, (dwKeyLen+1)>>1);
379 pbDest += (dwKeyLen+1)>>1;
380 mp_to_unsigned_bin(&pKeyContext->rsa.dQ, pbDest);
381 reverse_bytes(pbDest, (dwKeyLen+1)>>1);
382 pbDest += (dwKeyLen+1)>>1;
383 mp_to_unsigned_bin(&pKeyContext->rsa.qP, pbDest);
384 reverse_bytes(pbDest, (dwKeyLen+1)>>1);
385 pbDest += (dwKeyLen+1)>>1;
386 mp_to_unsigned_bin(&pKeyContext->rsa.d, pbDest);
387 reverse_bytes(pbDest, dwKeyLen);
388 *pdwPubExp = (DWORD)mp_get_int(&pKeyContext->rsa.e);
390 return TRUE;
393 BOOL import_private_key_impl(CONST BYTE *pbSrc, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
394 DWORD dwPubExp)
396 BYTE *pbTemp, *pbBigNum;
398 if (mp_init_multi(&pKeyContext->rsa.e, &pKeyContext->rsa.d, &pKeyContext->rsa.N,
399 &pKeyContext->rsa.dQ,&pKeyContext->rsa.dP,&pKeyContext->rsa.qP,
400 &pKeyContext->rsa.p, &pKeyContext->rsa.q, NULL) != MP_OKAY)
402 SetLastError(NTE_FAIL);
403 return FALSE;
406 pbTemp = HeapAlloc(GetProcessHeap(), 0, 2*dwKeyLen+5*((dwKeyLen+1)>>1));
407 if (!pbTemp) return FALSE;
408 memcpy(pbTemp, pbSrc, 2*dwKeyLen+5*((dwKeyLen+1)>>1));
409 pbBigNum = pbTemp;
411 pKeyContext->rsa.type = PK_PRIVATE;
412 reverse_bytes(pbBigNum, dwKeyLen);
413 mp_read_unsigned_bin(&pKeyContext->rsa.N, pbBigNum, dwKeyLen);
414 pbBigNum += dwKeyLen;
415 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
416 mp_read_unsigned_bin(&pKeyContext->rsa.p, pbBigNum, (dwKeyLen+1)>>1);
417 pbBigNum += (dwKeyLen+1)>>1;
418 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
419 mp_read_unsigned_bin(&pKeyContext->rsa.q, pbBigNum, (dwKeyLen+1)>>1);
420 pbBigNum += (dwKeyLen+1)>>1;
421 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
422 mp_read_unsigned_bin(&pKeyContext->rsa.dP, pbBigNum, (dwKeyLen+1)>>1);
423 pbBigNum += (dwKeyLen+1)>>1;
424 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
425 mp_read_unsigned_bin(&pKeyContext->rsa.dQ, pbBigNum, (dwKeyLen+1)>>1);
426 pbBigNum += (dwKeyLen+1)>>1;
427 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
428 mp_read_unsigned_bin(&pKeyContext->rsa.qP, pbBigNum, (dwKeyLen+1)>>1);
429 pbBigNum += (dwKeyLen+1)>>1;
430 reverse_bytes(pbBigNum, dwKeyLen);
431 mp_read_unsigned_bin(&pKeyContext->rsa.d, pbBigNum, dwKeyLen);
432 mp_set_int(&pKeyContext->rsa.e, dwPubExp);
434 HeapFree(GetProcessHeap(), 0, pbTemp);
435 return TRUE;