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
25 #include "wine/port.h"
26 #include "wine/library.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)
55 md2_init(&pHashContext->md2);
59 MD4Init(&pHashContext->md4);
63 MD5Init(&pHashContext->md5);
67 A_SHAInit(&pHashContext->sha);
74 BOOL update_hash_impl(ALG_ID aiAlgid, HASH_CONTEXT *pHashContext, CONST BYTE *pbData,
80 md2_process(&pHashContext->md2, pbData, dwDataLen);
84 MD4Update(&pHashContext->md4, pbData, dwDataLen);
88 MD5Update(&pHashContext->md5, pbData, dwDataLen);
92 A_SHAUpdate(&pHashContext->sha, (PCHAR)pbData, dwDataLen);
96 SetLastError(NTE_BAD_ALGID);
103 BOOL finalize_hash_impl(ALG_ID aiAlgid, HASH_CONTEXT *pHashContext, BYTE *pbHashValue)
108 md2_done(&pHashContext->md2, pbHashValue);
112 MD4Final(&pHashContext->md4);
113 memcpy(pbHashValue, pHashContext->md4.digest, 16);
117 MD5Final(&pHashContext->md5);
118 memcpy(pbHashValue, pHashContext->md5.digest, 16);
122 A_SHAFinal(&pHashContext->sha, (PULONG)pbHashValue);
126 SetLastError(NTE_BAD_ALGID);
133 BOOL duplicate_hash_impl(ALG_ID aiAlgid, CONST HASH_CONTEXT *pSrcHashContext,
134 HASH_CONTEXT *pDestHashContext)
136 memcpy(pDestHashContext, pSrcHashContext, sizeof(HASH_CONTEXT));
141 BOOL new_key_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen)
147 if (rsa_make_key((int)dwKeyLen, 65537, &pKeyContext->rsa) != CRYPT_OK) {
148 SetLastError(NTE_FAIL);
157 BOOL free_key_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext)
163 rsa_free(&pKeyContext->rsa);
169 BOOL setup_key_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen, DWORD dwSaltLen,
175 rc4_start(&pKeyContext->rc4);
176 rc4_add_entropy(abKeyValue, dwKeyLen + dwSaltLen, &pKeyContext->rc4);
177 rc4_ready(&pKeyContext->rc4);
181 rc2_setup(abKeyValue, dwKeyLen + dwSaltLen, dwKeyLen << 3, 0, &pKeyContext->rc2);
185 des3_setup(abKeyValue, 24, 0, &pKeyContext->des3);
189 memcpy(abKeyValue+16, abKeyValue, 8);
190 des3_setup(abKeyValue, 24, 0, &pKeyContext->des3);
194 des_setup(abKeyValue, 8, 0, &pKeyContext->des);
201 BOOL duplicate_key_impl(ALG_ID aiAlgid, CONST KEY_CONTEXT *pSrcKeyContext,
202 KEY_CONTEXT *pDestKeyContext)
211 memcpy(pDestKeyContext, pSrcKeyContext, sizeof(KEY_CONTEXT));
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);
227 SetLastError(NTE_BAD_ALGID);
234 static inline void reverse_bytes(BYTE *pbData, DWORD dwLen) {
238 for (i=0; i<dwLen/2; i++) {
240 pbData[i] = pbData[dwLen-i-1];
241 pbData[dwLen-i-1] = swap;
245 BOOL encrypt_block_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, CONST BYTE *in, BYTE *out,
248 unsigned long inlen, outlen;
249 BYTE *in_reversed = NULL;
255 rc2_ecb_encrypt(in, out, &pKeyContext->rc2);
257 rc2_ecb_decrypt(in, out, &pKeyContext->rc2);
264 des3_ecb_encrypt(in, out, &pKeyContext->des3);
266 des3_ecb_decrypt(in, out, &pKeyContext->des3);
272 des_ecb_encrypt(in, out, &pKeyContext->des);
274 des_ecb_decrypt(in, out, &pKeyContext->des);
280 outlen = inlen = (mp_count_bits(&pKeyContext->rsa.N)+7)/8;
282 if (aiAlgid == CALG_RSA_SIGN) {
287 if (rsa_exptmod(in, inlen, out, &outlen, key, &pKeyContext->rsa) != CRYPT_OK) {
288 SetLastError(NTE_FAIL);
291 reverse_bytes(out, outlen);
293 if (aiAlgid == CALG_RSA_SIGN) {
298 in_reversed = HeapAlloc(GetProcessHeap(), 0, inlen);
300 SetLastError(NTE_NO_MEMORY);
303 memcpy(in_reversed, in, inlen);
304 reverse_bytes(in_reversed, inlen);
305 if (rsa_exptmod(in_reversed, inlen, out, &outlen, key, &pKeyContext->rsa) != CRYPT_OK) {
306 HeapFree(GetProcessHeap(), 0, in_reversed);
307 SetLastError(NTE_FAIL);
310 HeapFree(GetProcessHeap(), 0, in_reversed);
315 SetLastError(NTE_BAD_ALGID);
322 BOOL encrypt_stream_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, BYTE *stream, DWORD dwLen)
326 rc4_read(stream, dwLen, &pKeyContext->rc4);
330 SetLastError(NTE_BAD_ALGID);
337 BOOL gen_rand_impl(BYTE *pbBuffer, DWORD dwLen)
339 return SystemFunction036(pbBuffer, dwLen);
342 BOOL export_public_key_impl(BYTE *pbDest, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,DWORD *pdwPubExp)
344 mp_to_unsigned_bin(&pKeyContext->rsa.N, pbDest);
345 reverse_bytes(pbDest, dwKeyLen);
346 *pdwPubExp = (DWORD)mp_get_int(&pKeyContext->rsa.e);
350 BOOL import_public_key_impl(CONST BYTE *pbSrc, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
355 if (mp_init_multi(&pKeyContext->rsa.e, &pKeyContext->rsa.d, &pKeyContext->rsa.N,
356 &pKeyContext->rsa.dQ,&pKeyContext->rsa.dP,&pKeyContext->rsa.qP,
357 &pKeyContext->rsa.p, &pKeyContext->rsa.q, NULL) != MP_OKAY)
359 SetLastError(NTE_FAIL);
363 pbTemp = HeapAlloc(GetProcessHeap(), 0, dwKeyLen);
364 if (!pbTemp) return FALSE;
365 memcpy(pbTemp, pbSrc, dwKeyLen);
367 pKeyContext->rsa.type = PK_PUBLIC;
368 reverse_bytes(pbTemp, dwKeyLen);
369 mp_read_unsigned_bin(&pKeyContext->rsa.N, pbTemp, dwKeyLen);
370 HeapFree(GetProcessHeap(), 0, pbTemp);
371 mp_set_int(&pKeyContext->rsa.e, dwPubExp);
376 BOOL export_private_key_impl(BYTE *pbDest, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
379 mp_to_unsigned_bin(&pKeyContext->rsa.N, pbDest);
380 reverse_bytes(pbDest, dwKeyLen);
382 mp_to_unsigned_bin(&pKeyContext->rsa.p, pbDest);
383 reverse_bytes(pbDest, (dwKeyLen+1)>>1);
384 pbDest += (dwKeyLen+1)>>1;
385 mp_to_unsigned_bin(&pKeyContext->rsa.q, pbDest);
386 reverse_bytes(pbDest, (dwKeyLen+1)>>1);
387 pbDest += (dwKeyLen+1)>>1;
388 mp_to_unsigned_bin(&pKeyContext->rsa.dP, pbDest);
389 reverse_bytes(pbDest, (dwKeyLen+1)>>1);
390 pbDest += (dwKeyLen+1)>>1;
391 mp_to_unsigned_bin(&pKeyContext->rsa.dQ, pbDest);
392 reverse_bytes(pbDest, (dwKeyLen+1)>>1);
393 pbDest += (dwKeyLen+1)>>1;
394 mp_to_unsigned_bin(&pKeyContext->rsa.qP, pbDest);
395 reverse_bytes(pbDest, (dwKeyLen+1)>>1);
396 pbDest += (dwKeyLen+1)>>1;
397 mp_to_unsigned_bin(&pKeyContext->rsa.d, pbDest);
398 reverse_bytes(pbDest, dwKeyLen);
399 *pdwPubExp = (DWORD)mp_get_int(&pKeyContext->rsa.e);
404 BOOL import_private_key_impl(CONST BYTE *pbSrc, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
407 BYTE *pbTemp, *pbBigNum;
409 if (mp_init_multi(&pKeyContext->rsa.e, &pKeyContext->rsa.d, &pKeyContext->rsa.N,
410 &pKeyContext->rsa.dQ,&pKeyContext->rsa.dP,&pKeyContext->rsa.qP,
411 &pKeyContext->rsa.p, &pKeyContext->rsa.q, NULL) != MP_OKAY)
413 SetLastError(NTE_FAIL);
417 pbTemp = HeapAlloc(GetProcessHeap(), 0, 2*dwKeyLen+5*((dwKeyLen+1)>>1));
418 if (!pbTemp) return FALSE;
419 memcpy(pbTemp, pbSrc, 2*dwKeyLen+5*((dwKeyLen+1)>>1));
422 pKeyContext->rsa.type = PK_PRIVATE;
423 reverse_bytes(pbBigNum, dwKeyLen);
424 mp_read_unsigned_bin(&pKeyContext->rsa.N, pbBigNum, dwKeyLen);
425 pbBigNum += dwKeyLen;
426 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
427 mp_read_unsigned_bin(&pKeyContext->rsa.p, pbBigNum, (dwKeyLen+1)>>1);
428 pbBigNum += (dwKeyLen+1)>>1;
429 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
430 mp_read_unsigned_bin(&pKeyContext->rsa.q, pbBigNum, (dwKeyLen+1)>>1);
431 pbBigNum += (dwKeyLen+1)>>1;
432 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
433 mp_read_unsigned_bin(&pKeyContext->rsa.dP, pbBigNum, (dwKeyLen+1)>>1);
434 pbBigNum += (dwKeyLen+1)>>1;
435 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
436 mp_read_unsigned_bin(&pKeyContext->rsa.dQ, pbBigNum, (dwKeyLen+1)>>1);
437 pbBigNum += (dwKeyLen+1)>>1;
438 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
439 mp_read_unsigned_bin(&pKeyContext->rsa.qP, pbBigNum, (dwKeyLen+1)>>1);
440 pbBigNum += (dwKeyLen+1)>>1;
441 reverse_bytes(pbBigNum, dwKeyLen);
442 mp_read_unsigned_bin(&pKeyContext->rsa.d, pbBigNum, dwKeyLen);
443 mp_set_int(&pKeyContext->rsa.e, dwPubExp);
445 HeapFree(GetProcessHeap(), 0, pbTemp);