2 * dlls/rsaenh/implglue.c
3 * Glueing the RSAENH specific code to the crypto library
5 * Copyright (c) 2004 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "wine/port.h"
26 #include "wine/library.h"
27 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
38 /* Function prototypes copied from dlls/advapi32/crypt_md4.c */
39 VOID WINAPI MD4Init( MD4_CTX *ctx );
40 VOID WINAPI MD4Update( MD4_CTX *ctx, const unsigned char *buf, unsigned int len );
41 VOID WINAPI MD4Final( MD4_CTX *ctx );
42 /* Function prototypes copied from dlls/advapi32/crypt_md5.c */
43 VOID WINAPI MD5Init( MD5_CTX *ctx );
44 VOID WINAPI MD5Update( MD5_CTX *ctx, const unsigned char *buf, unsigned int len );
45 VOID WINAPI MD5Final( MD5_CTX *ctx );
46 /* Function prototypes copied from dlls/advapi32/crypt_sha.c */
47 VOID WINAPI A_SHAInit(PSHA_CTX Context);
48 VOID WINAPI A_SHAUpdate(PSHA_CTX Context, PCHAR Buffer, UINT BufferSize);
49 VOID WINAPI A_SHAFinal(PSHA_CTX Context, PULONG Result);
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);
72 SetLastError(NTE_BAD_ALGID);
79 BOOL update_hash_impl(ALG_ID aiAlgid, HASH_CONTEXT *pHashContext, CONST BYTE *pbData,
85 md2_process(&pHashContext->md2, pbData, dwDataLen);
89 MD4Update(&pHashContext->md4, pbData, dwDataLen);
93 MD5Update(&pHashContext->md5, pbData, dwDataLen);
97 A_SHAUpdate(&pHashContext->sha, (PCHAR)pbData, dwDataLen);
101 SetLastError(NTE_BAD_ALGID);
108 BOOL finalize_hash_impl(ALG_ID aiAlgid, HASH_CONTEXT *pHashContext, BYTE *pbHashValue)
113 md2_done(&pHashContext->md2, pbHashValue);
117 MD4Final(&pHashContext->md4);
118 memcpy(pbHashValue, pHashContext->md4.digest, 16);
122 MD5Final(&pHashContext->md5);
123 memcpy(pbHashValue, pHashContext->md5.digest, 16);
127 A_SHAFinal(&pHashContext->sha, (PULONG)pbHashValue);
131 SetLastError(NTE_BAD_ALGID);
138 BOOL duplicate_hash_impl(ALG_ID aiAlgid, CONST HASH_CONTEXT *pSrcHashContext,
139 HASH_CONTEXT *pDestHashContext)
141 memcpy(pDestHashContext, pSrcHashContext, sizeof(HASH_CONTEXT));
146 BOOL new_key_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen)
152 if (rsa_make_key((int)dwKeyLen, 65537, &pKeyContext->rsa) != CRYPT_OK) {
153 SetLastError(NTE_FAIL);
162 BOOL free_key_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext)
168 rsa_free(&pKeyContext->rsa);
174 BOOL setup_key_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen, DWORD dwSaltLen,
180 rc4_start(&pKeyContext->rc4);
181 rc4_add_entropy(abKeyValue, dwKeyLen + dwSaltLen, &pKeyContext->rc4);
182 rc4_ready(&pKeyContext->rc4);
186 rc2_setup(abKeyValue, dwKeyLen + dwSaltLen, dwKeyLen << 3, 0, &pKeyContext->rc2);
190 des3_setup(abKeyValue, 24, 0, &pKeyContext->des3);
194 memcpy(abKeyValue+16, abKeyValue, 8);
195 des3_setup(abKeyValue, 24, 0, &pKeyContext->des3);
199 des_setup(abKeyValue, 8, 0, &pKeyContext->des);
207 SetLastError(NTE_BAD_ALGID);
214 BOOL duplicate_key_impl(ALG_ID aiAlgid, CONST KEY_CONTEXT *pSrcKeyContext,
215 KEY_CONTEXT *pDestKeyContext)
224 memcpy(pDestKeyContext, pSrcKeyContext, sizeof(KEY_CONTEXT));
228 pDestKeyContext->rsa.type = pSrcKeyContext->rsa.type;
229 mp_init_copy(&pDestKeyContext->rsa.e, &pSrcKeyContext->rsa.e);
230 mp_init_copy(&pDestKeyContext->rsa.d, &pSrcKeyContext->rsa.d);
231 mp_init_copy(&pDestKeyContext->rsa.N, &pSrcKeyContext->rsa.N);
232 mp_init_copy(&pDestKeyContext->rsa.p, &pSrcKeyContext->rsa.p);
233 mp_init_copy(&pDestKeyContext->rsa.q, &pSrcKeyContext->rsa.q);
234 mp_init_copy(&pDestKeyContext->rsa.qP, &pSrcKeyContext->rsa.qP);
235 mp_init_copy(&pDestKeyContext->rsa.dP, &pSrcKeyContext->rsa.dP);
236 mp_init_copy(&pDestKeyContext->rsa.dQ, &pSrcKeyContext->rsa.dQ);
240 SetLastError(NTE_BAD_ALGID);
247 static inline void reverse_bytes(BYTE *pbData, DWORD dwLen) {
251 for (i=0; i<dwLen/2; i++) {
253 pbData[i] = pbData[dwLen-i-1];
254 pbData[dwLen-i-1] = swap;
258 BOOL encrypt_block_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, CONST BYTE *in, BYTE *out,
261 unsigned long inlen, outlen;
262 BYTE *in_reversed = NULL;
268 rc2_ecb_encrypt(in, out, &pKeyContext->rc2);
270 rc2_ecb_decrypt(in, out, &pKeyContext->rc2);
277 des3_ecb_encrypt(in, out, &pKeyContext->des3);
279 des3_ecb_decrypt(in, out, &pKeyContext->des3);
285 des_ecb_encrypt(in, out, &pKeyContext->des);
287 des_ecb_decrypt(in, out, &pKeyContext->des);
293 outlen = inlen = (mp_count_bits(&pKeyContext->rsa.N)+7)/8;
295 if (aiAlgid == CALG_RSA_SIGN) {
300 if (rsa_exptmod(in, inlen, out, &outlen, key, &pKeyContext->rsa) != CRYPT_OK) {
301 SetLastError(NTE_FAIL);
304 reverse_bytes(out, outlen);
306 if (aiAlgid == CALG_RSA_SIGN) {
311 in_reversed = HeapAlloc(GetProcessHeap(), 0, inlen);
313 SetLastError(NTE_NO_MEMORY);
316 memcpy(in_reversed, in, inlen);
317 reverse_bytes(in_reversed, inlen);
318 if (rsa_exptmod(in_reversed, inlen, out, &outlen, key, &pKeyContext->rsa) != CRYPT_OK) {
319 HeapFree(GetProcessHeap(), 0, in_reversed);
320 SetLastError(NTE_FAIL);
323 HeapFree(GetProcessHeap(), 0, in_reversed);
328 SetLastError(NTE_BAD_ALGID);
335 BOOL encrypt_stream_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, BYTE *stream, DWORD dwLen)
339 rc4_read(stream, dwLen, &pKeyContext->rc4);
343 SetLastError(NTE_BAD_ALGID);
350 BOOL gen_rand_impl(BYTE *pbBuffer, DWORD dwLen)
354 /* FIXME: /dev/urandom does not provide random numbers of a sufficient
355 * quality for cryptographic applications. /dev/random is much better,
356 * but it blocks if the kernel has not yet collected enough entropy for
357 * the request, which will suspend the calling thread for an indefinite
359 dev_random = open("/dev/urandom", O_RDONLY);
360 if (dev_random != -1)
362 if (read(dev_random, pbBuffer, dwLen) == (ssize_t)dwLen)
369 SetLastError(NTE_FAIL);
373 BOOL export_public_key_impl(BYTE *pbDest, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,DWORD *pdwPubExp)
375 mp_to_unsigned_bin(&pKeyContext->rsa.N, pbDest);
376 reverse_bytes(pbDest, dwKeyLen);
377 *pdwPubExp = (DWORD)mp_get_int(&pKeyContext->rsa.e);
381 BOOL import_public_key_impl(CONST BYTE *pbSrc, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
386 if (mp_init_multi(&pKeyContext->rsa.e, &pKeyContext->rsa.d, &pKeyContext->rsa.N,
387 &pKeyContext->rsa.dQ,&pKeyContext->rsa.dP,&pKeyContext->rsa.qP,
388 &pKeyContext->rsa.p, &pKeyContext->rsa.q, NULL) != MP_OKAY)
390 SetLastError(NTE_FAIL);
394 pbTemp = (BYTE*)HeapAlloc(GetProcessHeap(), 0, dwKeyLen);
395 if (!pbTemp) return FALSE;
396 memcpy(pbTemp, pbSrc, dwKeyLen);
398 pKeyContext->rsa.type = PK_PUBLIC;
399 reverse_bytes(pbTemp, dwKeyLen);
400 mp_read_unsigned_bin(&pKeyContext->rsa.N, pbTemp, dwKeyLen);
401 HeapFree(GetProcessHeap(), 0, pbTemp);
402 mp_set_int(&pKeyContext->rsa.e, dwPubExp);
407 BOOL export_private_key_impl(BYTE *pbDest, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
410 mp_to_unsigned_bin(&pKeyContext->rsa.N, pbDest);
411 reverse_bytes(pbDest, dwKeyLen);
413 mp_to_unsigned_bin(&pKeyContext->rsa.p, pbDest);
414 reverse_bytes(pbDest, (dwKeyLen+1)>>1);
415 pbDest += (dwKeyLen+1)>>1;
416 mp_to_unsigned_bin(&pKeyContext->rsa.q, pbDest);
417 reverse_bytes(pbDest, (dwKeyLen+1)>>1);
418 pbDest += (dwKeyLen+1)>>1;
419 mp_to_unsigned_bin(&pKeyContext->rsa.dP, pbDest);
420 reverse_bytes(pbDest, (dwKeyLen+1)>>1);
421 pbDest += (dwKeyLen+1)>>1;
422 mp_to_unsigned_bin(&pKeyContext->rsa.dQ, pbDest);
423 reverse_bytes(pbDest, (dwKeyLen+1)>>1);
424 pbDest += (dwKeyLen+1)>>1;
425 mp_to_unsigned_bin(&pKeyContext->rsa.qP, pbDest);
426 reverse_bytes(pbDest, (dwKeyLen+1)>>1);
427 pbDest += (dwKeyLen+1)>>1;
428 mp_to_unsigned_bin(&pKeyContext->rsa.d, pbDest);
429 reverse_bytes(pbDest, dwKeyLen);
430 *pdwPubExp = (DWORD)mp_get_int(&pKeyContext->rsa.e);
435 BOOL import_private_key_impl(CONST BYTE *pbSrc, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
438 BYTE *pbTemp, *pbBigNum;
440 if (mp_init_multi(&pKeyContext->rsa.e, &pKeyContext->rsa.d, &pKeyContext->rsa.N,
441 &pKeyContext->rsa.dQ,&pKeyContext->rsa.dP,&pKeyContext->rsa.qP,
442 &pKeyContext->rsa.p, &pKeyContext->rsa.q, NULL) != MP_OKAY)
444 SetLastError(NTE_FAIL);
448 pbTemp = HeapAlloc(GetProcessHeap(), 0, 2*dwKeyLen+5*((dwKeyLen+1)>>1));
449 if (!pbTemp) return FALSE;
450 memcpy(pbTemp, pbSrc, 2*dwKeyLen+5*((dwKeyLen+1)>>1));
453 pKeyContext->rsa.type = PK_PRIVATE;
454 reverse_bytes(pbBigNum, dwKeyLen);
455 mp_read_unsigned_bin(&pKeyContext->rsa.N, pbBigNum, dwKeyLen);
456 pbBigNum += dwKeyLen;
457 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
458 mp_read_unsigned_bin(&pKeyContext->rsa.p, pbBigNum, (dwKeyLen+1)>>1);
459 pbBigNum += (dwKeyLen+1)>>1;
460 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
461 mp_read_unsigned_bin(&pKeyContext->rsa.q, pbBigNum, (dwKeyLen+1)>>1);
462 pbBigNum += (dwKeyLen+1)>>1;
463 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
464 mp_read_unsigned_bin(&pKeyContext->rsa.dP, pbBigNum, (dwKeyLen+1)>>1);
465 pbBigNum += (dwKeyLen+1)>>1;
466 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
467 mp_read_unsigned_bin(&pKeyContext->rsa.dQ, pbBigNum, (dwKeyLen+1)>>1);
468 pbBigNum += (dwKeyLen+1)>>1;
469 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
470 mp_read_unsigned_bin(&pKeyContext->rsa.qP, pbBigNum, (dwKeyLen+1)>>1);
471 pbBigNum += (dwKeyLen+1)>>1;
472 reverse_bytes(pbBigNum, dwKeyLen);
473 mp_read_unsigned_bin(&pKeyContext->rsa.d, pbBigNum, dwKeyLen);
474 mp_set_int(&pKeyContext->rsa.e, dwPubExp);
476 HeapFree(GetProcessHeap(), 0, pbTemp);