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
26 #include "wine/port.h"
27 #include "wine/library.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)
56 md2_init(&pHashContext->md2);
60 MD4Init(&pHashContext->md4);
64 MD5Init(&pHashContext->md5);
68 A_SHAInit(&pHashContext->sha);
75 BOOL update_hash_impl(ALG_ID aiAlgid, HASH_CONTEXT *pHashContext, CONST BYTE *pbData,
81 md2_process(&pHashContext->md2, pbData, dwDataLen);
85 MD4Update(&pHashContext->md4, pbData, dwDataLen);
89 MD5Update(&pHashContext->md5, pbData, dwDataLen);
93 A_SHAUpdate(&pHashContext->sha, pbData, dwDataLen);
97 SetLastError(NTE_BAD_ALGID);
104 BOOL finalize_hash_impl(ALG_ID aiAlgid, HASH_CONTEXT *pHashContext, BYTE *pbHashValue)
109 md2_done(&pHashContext->md2, pbHashValue);
113 MD4Final(&pHashContext->md4);
114 memcpy(pbHashValue, pHashContext->md4.digest, 16);
118 MD5Final(&pHashContext->md5);
119 memcpy(pbHashValue, pHashContext->md5.digest, 16);
123 A_SHAFinal(&pHashContext->sha, (PULONG)pbHashValue);
127 SetLastError(NTE_BAD_ALGID);
134 BOOL duplicate_hash_impl(ALG_ID aiAlgid, CONST HASH_CONTEXT *pSrcHashContext,
135 HASH_CONTEXT *pDestHashContext)
137 memcpy(pDestHashContext, pSrcHashContext, sizeof(HASH_CONTEXT));
142 BOOL new_key_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen)
148 if (rsa_make_key((int)dwKeyLen, 65537, &pKeyContext->rsa) != CRYPT_OK) {
149 SetLastError(NTE_FAIL);
158 BOOL free_key_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext)
164 rsa_free(&pKeyContext->rsa);
170 BOOL setup_key_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
171 DWORD dwEffectiveKeyLen, DWORD dwSaltLen, BYTE *abKeyValue)
176 rc4_start(&pKeyContext->rc4);
177 rc4_add_entropy(abKeyValue, dwKeyLen + dwSaltLen, &pKeyContext->rc4);
178 rc4_ready(&pKeyContext->rc4);
182 rc2_setup(abKeyValue, dwKeyLen + dwSaltLen, dwEffectiveKeyLen ?
183 dwEffectiveKeyLen : dwKeyLen << 3, 0, &pKeyContext->rc2);
187 des3_setup(abKeyValue, 24, 0, &pKeyContext->des3);
191 memcpy(abKeyValue+16, abKeyValue, 8);
192 des3_setup(abKeyValue, 24, 0, &pKeyContext->des3);
196 des_setup(abKeyValue, 8, 0, &pKeyContext->des);
201 aes_setup(abKeyValue, 16, 0, &pKeyContext->aes);
205 aes_setup(abKeyValue, 24, 0, &pKeyContext->aes);
209 aes_setup(abKeyValue, 32, 0, &pKeyContext->aes);
216 BOOL duplicate_key_impl(ALG_ID aiAlgid, CONST KEY_CONTEXT *pSrcKeyContext,
217 KEY_CONTEXT *pDestKeyContext)
230 memcpy(pDestKeyContext, pSrcKeyContext, sizeof(KEY_CONTEXT));
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);
246 SetLastError(NTE_BAD_ALGID);
253 static inline void reverse_bytes(BYTE *pbData, DWORD dwLen) {
257 for (i=0; i<dwLen/2; 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,
267 unsigned long inlen, outlen;
268 BYTE *in_reversed = NULL;
273 rc2_ecb_encrypt(in, out, &pKeyContext->rc2);
275 rc2_ecb_decrypt(in, out, &pKeyContext->rc2);
282 des3_ecb_encrypt(in, out, &pKeyContext->des3);
284 des3_ecb_decrypt(in, out, &pKeyContext->des3);
290 des_ecb_encrypt(in, out, &pKeyContext->des);
292 des_ecb_decrypt(in, out, &pKeyContext->des);
301 aes_ecb_encrypt(in, out, &pKeyContext->aes);
303 aes_ecb_decrypt(in, out, &pKeyContext->aes);
309 outlen = inlen = (mp_count_bits(&pKeyContext->rsa.N)+7)/8;
311 if (rsa_exptmod(in, inlen, out, &outlen, dwKeySpec, &pKeyContext->rsa) != CRYPT_OK) {
312 SetLastError(NTE_FAIL);
315 reverse_bytes(out, outlen);
317 in_reversed = HeapAlloc(GetProcessHeap(), 0, inlen);
319 SetLastError(NTE_NO_MEMORY);
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);
329 HeapFree(GetProcessHeap(), 0, in_reversed);
334 SetLastError(NTE_BAD_ALGID);
341 BOOL encrypt_stream_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, BYTE *stream, DWORD dwLen)
345 rc4_read(stream, dwLen, &pKeyContext->rc4);
349 SetLastError(NTE_BAD_ALGID);
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);
369 BOOL import_public_key_impl(CONST BYTE *pbSrc, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
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);
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);
395 BOOL export_private_key_impl(BYTE *pbDest, const KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
398 mp_to_unsigned_bin(&pKeyContext->rsa.N, pbDest);
399 reverse_bytes(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);
423 BOOL import_private_key_impl(CONST BYTE *pbSrc, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
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);
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));
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);