sync rsaenh with wine 1.1.33
[reactos.git] / reactos / dll / win32 / rsaenh / implglue.c
1 /*
2 * dlls/rsaenh/implglue.c
3 * Glueing the RSAENH specific code to the crypto library
4 *
5 * Copyright (c) 2004, 2005 Michael Jung
6 * Copyright (c) 2007 Vijay Kiran Kamuju
7 *
8 * based on code by Mike McCormack and David Hammerton
9 *
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.
14 *
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.
19 *
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
23 */
24 #include "config.h"
25
26 #include "wine/port.h"
27 #include "wine/library.h"
28
29 #include "windef.h"
30 #include "wincrypt.h"
31
32 #include "implglue.h"
33
34 #include <stdio.h>
35
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);
50
51 BOOL init_hash_impl(ALG_ID aiAlgid, HASH_CONTEXT *pHashContext)
52 {
53 switch (aiAlgid)
54 {
55 case CALG_MD2:
56 md2_init(&pHashContext->md2);
57 break;
58
59 case CALG_MD4:
60 MD4Init(&pHashContext->md4);
61 break;
62
63 case CALG_MD5:
64 MD5Init(&pHashContext->md5);
65 break;
66
67 case CALG_SHA:
68 A_SHAInit(&pHashContext->sha);
69 break;
70 }
71
72 return TRUE;
73 }
74
75 BOOL update_hash_impl(ALG_ID aiAlgid, HASH_CONTEXT *pHashContext, CONST BYTE *pbData,
76 DWORD dwDataLen)
77 {
78 switch (aiAlgid)
79 {
80 case CALG_MD2:
81 md2_process(&pHashContext->md2, pbData, dwDataLen);
82 break;
83
84 case CALG_MD4:
85 MD4Update(&pHashContext->md4, pbData, dwDataLen);
86 break;
87
88 case CALG_MD5:
89 MD5Update(&pHashContext->md5, pbData, dwDataLen);
90 break;
91
92 case CALG_SHA:
93 A_SHAUpdate(&pHashContext->sha, pbData, dwDataLen);
94 break;
95
96 default:
97 SetLastError(NTE_BAD_ALGID);
98 return FALSE;
99 }
100
101 return TRUE;
102 }
103
104 BOOL finalize_hash_impl(ALG_ID aiAlgid, HASH_CONTEXT *pHashContext, BYTE *pbHashValue)
105 {
106 switch (aiAlgid)
107 {
108 case CALG_MD2:
109 md2_done(&pHashContext->md2, pbHashValue);
110 break;
111
112 case CALG_MD4:
113 MD4Final(&pHashContext->md4);
114 memcpy(pbHashValue, pHashContext->md4.digest, 16);
115 break;
116
117 case CALG_MD5:
118 MD5Final(&pHashContext->md5);
119 memcpy(pbHashValue, pHashContext->md5.digest, 16);
120 break;
121
122 case CALG_SHA:
123 A_SHAFinal(&pHashContext->sha, (PULONG)pbHashValue);
124 break;
125
126 default:
127 SetLastError(NTE_BAD_ALGID);
128 return FALSE;
129 }
130
131 return TRUE;
132 }
133
134 BOOL duplicate_hash_impl(ALG_ID aiAlgid, CONST HASH_CONTEXT *pSrcHashContext,
135 HASH_CONTEXT *pDestHashContext)
136 {
137 *pDestHashContext = *pSrcHashContext;
138
139 return TRUE;
140 }
141
142 BOOL new_key_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen)
143 {
144 switch (aiAlgid)
145 {
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;
151 }
152 return TRUE;
153 }
154
155 return TRUE;
156 }
157
158 BOOL free_key_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext)
159 {
160 switch (aiAlgid)
161 {
162 case CALG_RSA_KEYX:
163 case CALG_RSA_SIGN:
164 rsa_free(&pKeyContext->rsa);
165 }
166
167 return TRUE;
168 }
169
170 BOOL setup_key_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
171 DWORD dwEffectiveKeyLen, DWORD dwSaltLen, BYTE *abKeyValue)
172 {
173 switch (aiAlgid)
174 {
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;
180
181 case CALG_RC2:
182 rc2_setup(abKeyValue, dwKeyLen + dwSaltLen, dwEffectiveKeyLen ?
183 dwEffectiveKeyLen : dwKeyLen << 3, 0, &pKeyContext->rc2);
184 break;
185
186 case CALG_3DES:
187 des3_setup(abKeyValue, 24, 0, &pKeyContext->des3);
188 break;
189
190 case CALG_3DES_112:
191 memcpy(abKeyValue+16, abKeyValue, 8);
192 des3_setup(abKeyValue, 24, 0, &pKeyContext->des3);
193 break;
194
195 case CALG_DES:
196 des_setup(abKeyValue, 8, 0, &pKeyContext->des);
197 break;
198
199 case CALG_AES:
200 case CALG_AES_128:
201 aes_setup(abKeyValue, 16, 0, &pKeyContext->aes);
202 break;
203
204 case CALG_AES_192:
205 aes_setup(abKeyValue, 24, 0, &pKeyContext->aes);
206 break;
207
208 case CALG_AES_256:
209 aes_setup(abKeyValue, 32, 0, &pKeyContext->aes);
210 break;
211 }
212
213 return TRUE;
214 }
215
216 BOOL duplicate_key_impl(ALG_ID aiAlgid, CONST KEY_CONTEXT *pSrcKeyContext,
217 KEY_CONTEXT *pDestKeyContext)
218 {
219 switch (aiAlgid)
220 {
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;
244
245 default:
246 SetLastError(NTE_BAD_ALGID);
247 return FALSE;
248 }
249
250 return TRUE;
251 }
252
253 static inline void reverse_bytes(BYTE *pbData, DWORD dwLen) {
254 BYTE swap;
255 DWORD i;
256
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;
261 }
262 }
263
264 BOOL encrypt_block_impl(ALG_ID aiAlgid, DWORD dwKeySpec, KEY_CONTEXT *pKeyContext, CONST BYTE *in, BYTE *out,
265 DWORD enc)
266 {
267 unsigned long inlen, outlen;
268 BYTE *in_reversed = NULL;
269
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);
276 }
277 break;
278
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);
285 }
286 break;
287
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);
293 }
294 break;
295
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);
304 }
305 break;
306
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;
314 }
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;
321 }
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;
328 }
329 HeapFree(GetProcessHeap(), 0, in_reversed);
330 }
331 break;
332
333 default:
334 SetLastError(NTE_BAD_ALGID);
335 return FALSE;
336 }
337
338 return TRUE;
339 }
340
341 BOOL encrypt_stream_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, BYTE *stream, DWORD dwLen)
342 {
343 switch (aiAlgid) {
344 case CALG_RC4:
345 rc4_read(stream, dwLen, &pKeyContext->rc4);
346 break;
347
348 default:
349 SetLastError(NTE_BAD_ALGID);
350 return FALSE;
351 }
352
353 return TRUE;
354 }
355
356 BOOL gen_rand_impl(BYTE *pbBuffer, DWORD dwLen)
357 {
358 return SystemFunction036(pbBuffer, dwLen);
359 }
360
361 BOOL export_public_key_impl(BYTE *pbDest, const KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,DWORD *pdwPubExp)
362 {
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;
370 }
371
372 BOOL import_public_key_impl(CONST BYTE *pbSrc, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
373 DWORD dwPubExp)
374 {
375 BYTE *pbTemp;
376
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)
380 {
381 SetLastError(NTE_FAIL);
382 return FALSE;
383 }
384
385 pbTemp = HeapAlloc(GetProcessHeap(), 0, dwKeyLen);
386 if (!pbTemp) return FALSE;
387 memcpy(pbTemp, pbSrc, dwKeyLen);
388
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);
394
395 return TRUE;
396 }
397
398 BOOL export_private_key_impl(BYTE *pbDest, const KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
399 DWORD *pdwPubExp)
400 {
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);
443
444 return TRUE;
445 }
446
447 BOOL import_private_key_impl(CONST BYTE *pbSrc, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
448 DWORD dwPubExp)
449 {
450 BYTE *pbTemp, *pbBigNum;
451
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)
455 {
456 SetLastError(NTE_FAIL);
457 return FALSE;
458 }
459
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;
464
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);
487
488 HeapFree(GetProcessHeap(), 0, pbTemp);
489 return TRUE;
490 }