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, const unsigned char *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, 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,
170 DWORD dwEffectiveKeyLen, DWORD dwSaltLen, BYTE *abKeyValue)
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, dwEffectiveKeyLen ?
182 dwEffectiveKeyLen : dwKeyLen << 3, 0, &pKeyContext->rc2);
186 des3_setup(abKeyValue, 24, 0, &pKeyContext->des3);
190 memcpy(abKeyValue+16, abKeyValue, 8);
191 des3_setup(abKeyValue, 24, 0, &pKeyContext->des3);
195 des_setup(abKeyValue, 8, 0, &pKeyContext->des);
202 BOOL duplicate_key_impl(ALG_ID aiAlgid, CONST KEY_CONTEXT *pSrcKeyContext,
203 KEY_CONTEXT *pDestKeyContext)
212 memcpy(pDestKeyContext, pSrcKeyContext, sizeof(KEY_CONTEXT));
216 pDestKeyContext->rsa.type = pSrcKeyContext->rsa.type;
217 mp_init_copy(&pDestKeyContext->rsa.e, &pSrcKeyContext->rsa.e);
218 mp_init_copy(&pDestKeyContext->rsa.d, &pSrcKeyContext->rsa.d);
219 mp_init_copy(&pDestKeyContext->rsa.N, &pSrcKeyContext->rsa.N);
220 mp_init_copy(&pDestKeyContext->rsa.p, &pSrcKeyContext->rsa.p);
221 mp_init_copy(&pDestKeyContext->rsa.q, &pSrcKeyContext->rsa.q);
222 mp_init_copy(&pDestKeyContext->rsa.qP, &pSrcKeyContext->rsa.qP);
223 mp_init_copy(&pDestKeyContext->rsa.dP, &pSrcKeyContext->rsa.dP);
224 mp_init_copy(&pDestKeyContext->rsa.dQ, &pSrcKeyContext->rsa.dQ);
228 SetLastError(NTE_BAD_ALGID);
235 static inline void reverse_bytes(BYTE *pbData, DWORD dwLen) {
239 for (i=0; i<dwLen/2; i++) {
241 pbData[i] = pbData[dwLen-i-1];
242 pbData[dwLen-i-1] = swap;
246 BOOL encrypt_block_impl(ALG_ID aiAlgid, DWORD dwKeySpec, KEY_CONTEXT *pKeyContext, CONST BYTE *in, BYTE *out,
249 unsigned long inlen, outlen;
250 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 (rsa_exptmod(in, inlen, out, &outlen, dwKeySpec, &pKeyContext->rsa) != CRYPT_OK) {
283 SetLastError(NTE_FAIL);
286 reverse_bytes(out, outlen);
288 in_reversed = HeapAlloc(GetProcessHeap(), 0, inlen);
290 SetLastError(NTE_NO_MEMORY);
293 memcpy(in_reversed, in, inlen);
294 reverse_bytes(in_reversed, inlen);
295 if (rsa_exptmod(in_reversed, inlen, out, &outlen, dwKeySpec, &pKeyContext->rsa) != CRYPT_OK) {
296 HeapFree(GetProcessHeap(), 0, in_reversed);
297 SetLastError(NTE_FAIL);
300 HeapFree(GetProcessHeap(), 0, in_reversed);
305 SetLastError(NTE_BAD_ALGID);
312 BOOL encrypt_stream_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, BYTE *stream, DWORD dwLen)
316 rc4_read(stream, dwLen, &pKeyContext->rc4);
320 SetLastError(NTE_BAD_ALGID);
327 BOOL gen_rand_impl(BYTE *pbBuffer, DWORD dwLen)
329 return SystemFunction036(pbBuffer, dwLen);
332 BOOL export_public_key_impl(BYTE *pbDest, const KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,DWORD *pdwPubExp)
334 mp_to_unsigned_bin(&pKeyContext->rsa.N, pbDest);
335 reverse_bytes(pbDest, dwKeyLen);
336 *pdwPubExp = (DWORD)mp_get_int(&pKeyContext->rsa.e);
340 BOOL import_public_key_impl(CONST BYTE *pbSrc, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
345 if (mp_init_multi(&pKeyContext->rsa.e, &pKeyContext->rsa.d, &pKeyContext->rsa.N,
346 &pKeyContext->rsa.dQ,&pKeyContext->rsa.dP,&pKeyContext->rsa.qP,
347 &pKeyContext->rsa.p, &pKeyContext->rsa.q, NULL) != MP_OKAY)
349 SetLastError(NTE_FAIL);
353 pbTemp = HeapAlloc(GetProcessHeap(), 0, dwKeyLen);
354 if (!pbTemp) return FALSE;
355 memcpy(pbTemp, pbSrc, dwKeyLen);
357 pKeyContext->rsa.type = PK_PUBLIC;
358 reverse_bytes(pbTemp, dwKeyLen);
359 mp_read_unsigned_bin(&pKeyContext->rsa.N, pbTemp, dwKeyLen);
360 HeapFree(GetProcessHeap(), 0, pbTemp);
361 mp_set_int(&pKeyContext->rsa.e, dwPubExp);
366 BOOL export_private_key_impl(BYTE *pbDest, const KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
369 mp_to_unsigned_bin(&pKeyContext->rsa.N, pbDest);
370 reverse_bytes(pbDest, dwKeyLen);
372 mp_to_unsigned_bin(&pKeyContext->rsa.p, pbDest);
373 reverse_bytes(pbDest, (dwKeyLen+1)>>1);
374 pbDest += (dwKeyLen+1)>>1;
375 mp_to_unsigned_bin(&pKeyContext->rsa.q, pbDest);
376 reverse_bytes(pbDest, (dwKeyLen+1)>>1);
377 pbDest += (dwKeyLen+1)>>1;
378 mp_to_unsigned_bin(&pKeyContext->rsa.dP, pbDest);
379 reverse_bytes(pbDest, (dwKeyLen+1)>>1);
380 pbDest += (dwKeyLen+1)>>1;
381 mp_to_unsigned_bin(&pKeyContext->rsa.dQ, pbDest);
382 reverse_bytes(pbDest, (dwKeyLen+1)>>1);
383 pbDest += (dwKeyLen+1)>>1;
384 mp_to_unsigned_bin(&pKeyContext->rsa.qP, pbDest);
385 reverse_bytes(pbDest, (dwKeyLen+1)>>1);
386 pbDest += (dwKeyLen+1)>>1;
387 mp_to_unsigned_bin(&pKeyContext->rsa.d, pbDest);
388 reverse_bytes(pbDest, dwKeyLen);
389 *pdwPubExp = (DWORD)mp_get_int(&pKeyContext->rsa.e);
394 BOOL import_private_key_impl(CONST BYTE *pbSrc, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
397 BYTE *pbTemp, *pbBigNum;
399 if (mp_init_multi(&pKeyContext->rsa.e, &pKeyContext->rsa.d, &pKeyContext->rsa.N,
400 &pKeyContext->rsa.dQ,&pKeyContext->rsa.dP,&pKeyContext->rsa.qP,
401 &pKeyContext->rsa.p, &pKeyContext->rsa.q, NULL) != MP_OKAY)
403 SetLastError(NTE_FAIL);
407 pbTemp = HeapAlloc(GetProcessHeap(), 0, 2*dwKeyLen+5*((dwKeyLen+1)>>1));
408 if (!pbTemp) return FALSE;
409 memcpy(pbTemp, pbSrc, 2*dwKeyLen+5*((dwKeyLen+1)>>1));
412 pKeyContext->rsa.type = PK_PRIVATE;
413 reverse_bytes(pbBigNum, dwKeyLen);
414 mp_read_unsigned_bin(&pKeyContext->rsa.N, pbBigNum, dwKeyLen);
415 pbBigNum += dwKeyLen;
416 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
417 mp_read_unsigned_bin(&pKeyContext->rsa.p, pbBigNum, (dwKeyLen+1)>>1);
418 pbBigNum += (dwKeyLen+1)>>1;
419 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
420 mp_read_unsigned_bin(&pKeyContext->rsa.q, pbBigNum, (dwKeyLen+1)>>1);
421 pbBigNum += (dwKeyLen+1)>>1;
422 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
423 mp_read_unsigned_bin(&pKeyContext->rsa.dP, pbBigNum, (dwKeyLen+1)>>1);
424 pbBigNum += (dwKeyLen+1)>>1;
425 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
426 mp_read_unsigned_bin(&pKeyContext->rsa.dQ, pbBigNum, (dwKeyLen+1)>>1);
427 pbBigNum += (dwKeyLen+1)>>1;
428 reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
429 mp_read_unsigned_bin(&pKeyContext->rsa.qP, pbBigNum, (dwKeyLen+1)>>1);
430 pbBigNum += (dwKeyLen+1)>>1;
431 reverse_bytes(pbBigNum, dwKeyLen);
432 mp_read_unsigned_bin(&pKeyContext->rsa.d, pbBigNum, dwKeyLen);
433 mp_set_int(&pKeyContext->rsa.e, dwPubExp);
435 HeapFree(GetProcessHeap(), 0, pbTemp);