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;
266 rc2_ecb_encrypt(in, out, &pKeyContext->rc2);
268 rc2_ecb_decrypt(in, out, &pKeyContext->rc2);
275 des3_ecb_encrypt(in, out, &pKeyContext->des3);
277 des3_ecb_decrypt(in, out, &pKeyContext->des3);
283 des_ecb_encrypt(in, out, &pKeyContext->des);
285 des_ecb_decrypt(in, out, &pKeyContext->des);
290 outlen = inlen = (mp_count_bits(&pKeyContext->rsa.N)+7)/8;
292 if (rsa_exptmod(in, inlen, out, &outlen, PK_PUBLIC, &pKeyContext->rsa) != CRYPT_OK) {
293 SetLastError(NTE_FAIL);
296 reverse_bytes((BYTE*)out, outlen);
298 reverse_bytes((BYTE*)in, inlen);
299 if (rsa_exptmod(in, inlen, out, &outlen, PK_PRIVATE, &pKeyContext->rsa) != CRYPT_OK) {
300 SetLastError(NTE_FAIL);
307 outlen = inlen = (mp_count_bits(&pKeyContext->rsa.N)+7)/8;
309 if (rsa_exptmod(in, inlen, out, &outlen, PK_PRIVATE, &pKeyContext->rsa) != CRYPT_OK) {
310 SetLastError(NTE_FAIL);
313 reverse_bytes((BYTE*)out, outlen);
315 reverse_bytes((BYTE*)in, inlen);
316 if (rsa_exptmod(in, inlen, out, &outlen, PK_PUBLIC, &pKeyContext->rsa) != CRYPT_OK) {
317 SetLastError(NTE_FAIL);
324 SetLastError(NTE_BAD_ALGID);
331 BOOL encrypt_stream_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, BYTE *stream, DWORD dwLen)
335 rc4_read(stream, dwLen, &pKeyContext->rc4);
339 SetLastError(NTE_BAD_ALGID);
346 BOOL gen_rand_impl(BYTE *pbBuffer, DWORD dwLen)
350 /* FIXME: /dev/urandom does not provide random numbers of a sufficient
351 * quality for cryptographic applications. /dev/random is much better,
352 * but it blocks if the kernel has not yet collected enough entropy for
353 * the request, which will suspend the calling thread for an indefinite
355 dev_random = open("/dev/urandom", O_RDONLY);
356 if (dev_random != -1)
358 if (read(dev_random, pbBuffer, dwLen) == (ssize_t)dwLen)
365 SetLastError(NTE_FAIL);
369 BOOL export_public_key_impl(BYTE *pbDest, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,DWORD *pdwPubExp)
371 mp_to_unsigned_bin(&pKeyContext->rsa.N, pbDest);
372 reverse_bytes(pbDest, dwKeyLen);
373 *pdwPubExp = (DWORD)mp_get_int(&pKeyContext->rsa.e);
377 BOOL import_public_key_impl(CONST BYTE *pbSrc, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
382 if (mp_init_multi(&pKeyContext->rsa.e, &pKeyContext->rsa.d, &pKeyContext->rsa.N,
383 &pKeyContext->rsa.dQ,&pKeyContext->rsa.dP,&pKeyContext->rsa.qP,
384 &pKeyContext->rsa.p, &pKeyContext->rsa.q, NULL) != MP_OKAY)
386 SetLastError(NTE_FAIL);
390 pbTemp = (BYTE*)HeapAlloc(GetProcessHeap(), 0, dwKeyLen);
391 if (!pbTemp) return FALSE;
392 memcpy(pbTemp, pbSrc, dwKeyLen);
394 pKeyContext->rsa.type = PK_PUBLIC;
395 reverse_bytes(pbTemp, dwKeyLen);
396 mp_read_unsigned_bin(&pKeyContext->rsa.N, pbTemp, dwKeyLen);
397 HeapFree(GetProcessHeap(), 0, pbTemp);
398 mp_set_int(&pKeyContext->rsa.e, dwPubExp);
403 BOOL export_private_key_impl(BYTE *pbDest, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
406 mp_to_unsigned_bin(&pKeyContext->rsa.N, pbDest);
407 reverse_bytes(pbDest, dwKeyLen);
409 mp_to_unsigned_bin(&pKeyContext->rsa.p, pbDest);
410 reverse_bytes(pbDest, (dwKeyLen+1)>>1);
411 pbDest += (dwKeyLen+1)>>1;
412 mp_to_unsigned_bin(&pKeyContext->rsa.q, pbDest);
413 reverse_bytes(pbDest, (dwKeyLen+1)>>1);
414 pbDest += (dwKeyLen+1)>>1;
415 mp_to_unsigned_bin(&pKeyContext->rsa.dP, pbDest);
416 reverse_bytes(pbDest, (dwKeyLen+1)>>1);
417 pbDest += (dwKeyLen+1)>>1;
418 mp_to_unsigned_bin(&pKeyContext->rsa.dQ, pbDest);
419 reverse_bytes(pbDest, (dwKeyLen+1)>>1);
420 pbDest += (dwKeyLen+1)>>1;
421 mp_to_unsigned_bin(&pKeyContext->rsa.qP, pbDest);
422 reverse_bytes(pbDest, (dwKeyLen+1)>>1);
423 pbDest += (dwKeyLen+1)>>1;
424 mp_to_unsigned_bin(&pKeyContext->rsa.d, pbDest);
425 reverse_bytes(pbDest, dwKeyLen);
426 *pdwPubExp = (DWORD)mp_get_int(&pKeyContext->rsa.e);
431 BOOL import_private_key_impl(CONST BYTE *pbSrc, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
434 BYTE *pbTemp, *pbBigNum;
436 if (mp_init_multi(&pKeyContext->rsa.e, &pKeyContext->rsa.d, &pKeyContext->rsa.N,
437 &pKeyContext->rsa.dQ,&pKeyContext->rsa.dP,&pKeyContext->rsa.qP,
438 &pKeyContext->rsa.p, &pKeyContext->rsa.q, NULL) != MP_OKAY)
440 SetLastError(NTE_FAIL);
444 pbTemp = HeapAlloc(GetProcessHeap(), 0, 2*dwKeyLen+5*((dwKeyLen+1)>>1));
445 if (!pbTemp) return FALSE;
446 memcpy(pbTemp, pbSrc, 2*dwKeyLen+5*((dwKeyLen+1)>>1));
449 pKeyContext->rsa.type = PK_PRIVATE;
450 reverse_bytes(pbBigNum, dwKeyLen);
451 mp_read_unsigned_bin(&pKeyContext->rsa.N, pbBigNum, dwKeyLen);
452 pbBigNum += dwKeyLen;
453 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
454 mp_read_unsigned_bin(&pKeyContext->rsa.p, pbBigNum, (dwKeyLen+1)>>1);
455 pbBigNum += (dwKeyLen+1)>>1;
456 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
457 mp_read_unsigned_bin(&pKeyContext->rsa.q, pbBigNum, (dwKeyLen+1)>>1);
458 pbBigNum += (dwKeyLen+1)>>1;
459 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
460 mp_read_unsigned_bin(&pKeyContext->rsa.dP, pbBigNum, (dwKeyLen+1)>>1);
461 pbBigNum += (dwKeyLen+1)>>1;
462 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
463 mp_read_unsigned_bin(&pKeyContext->rsa.dQ, pbBigNum, (dwKeyLen+1)>>1);
464 pbBigNum += (dwKeyLen+1)>>1;
465 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
466 mp_read_unsigned_bin(&pKeyContext->rsa.qP, pbBigNum, (dwKeyLen+1)>>1);
467 pbBigNum += (dwKeyLen+1)>>1;
468 reverse_bytes(pbBigNum, dwKeyLen);
469 mp_read_unsigned_bin(&pKeyContext->rsa.d, pbBigNum, dwKeyLen);
470 mp_set_int(&pKeyContext->rsa.e, dwPubExp);
472 HeapFree(GetProcessHeap(), 0, pbTemp);