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., 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
);
50 /* Function prototype copied from dlls/advapi32/crypt.c */
51 BOOL WINAPI
SystemFunction036(PVOID pbBuffer
, ULONG dwLen
);
53 BOOL
init_hash_impl(ALG_ID aiAlgid
, HASH_CONTEXT
*pHashContext
)
58 md2_init(&pHashContext
->md2
);
62 MD4Init(&pHashContext
->md4
);
66 MD5Init(&pHashContext
->md5
);
70 A_SHAInit(&pHashContext
->sha
);
77 BOOL
update_hash_impl(ALG_ID aiAlgid
, HASH_CONTEXT
*pHashContext
, CONST BYTE
*pbData
,
83 md2_process(&pHashContext
->md2
, pbData
, dwDataLen
);
87 MD4Update(&pHashContext
->md4
, pbData
, dwDataLen
);
91 MD5Update(&pHashContext
->md5
, pbData
, dwDataLen
);
95 A_SHAUpdate(&pHashContext
->sha
, (PCHAR
)pbData
, dwDataLen
);
99 SetLastError(NTE_BAD_ALGID
);
106 BOOL
finalize_hash_impl(ALG_ID aiAlgid
, HASH_CONTEXT
*pHashContext
, BYTE
*pbHashValue
)
111 md2_done(&pHashContext
->md2
, pbHashValue
);
115 MD4Final(&pHashContext
->md4
);
116 memcpy(pbHashValue
, pHashContext
->md4
.digest
, 16);
120 MD5Final(&pHashContext
->md5
);
121 memcpy(pbHashValue
, pHashContext
->md5
.digest
, 16);
125 A_SHAFinal(&pHashContext
->sha
, (PULONG
)pbHashValue
);
129 SetLastError(NTE_BAD_ALGID
);
136 BOOL
duplicate_hash_impl(ALG_ID aiAlgid
, CONST HASH_CONTEXT
*pSrcHashContext
,
137 HASH_CONTEXT
*pDestHashContext
)
139 memcpy(pDestHashContext
, pSrcHashContext
, sizeof(HASH_CONTEXT
));
144 BOOL
new_key_impl(ALG_ID aiAlgid
, KEY_CONTEXT
*pKeyContext
, DWORD dwKeyLen
)
150 if (rsa_make_key((int)dwKeyLen
, 65537, &pKeyContext
->rsa
) != CRYPT_OK
) {
151 SetLastError(NTE_FAIL
);
160 BOOL
free_key_impl(ALG_ID aiAlgid
, KEY_CONTEXT
*pKeyContext
)
166 rsa_free(&pKeyContext
->rsa
);
172 BOOL
setup_key_impl(ALG_ID aiAlgid
, KEY_CONTEXT
*pKeyContext
, DWORD dwKeyLen
, DWORD dwSaltLen
,
178 rc4_start(&pKeyContext
->rc4
);
179 rc4_add_entropy(abKeyValue
, dwKeyLen
+ dwSaltLen
, &pKeyContext
->rc4
);
180 rc4_ready(&pKeyContext
->rc4
);
184 rc2_setup(abKeyValue
, dwKeyLen
+ dwSaltLen
, dwKeyLen
<< 3, 0, &pKeyContext
->rc2
);
188 des3_setup(abKeyValue
, 24, 0, &pKeyContext
->des3
);
192 memcpy(abKeyValue
+16, abKeyValue
, 8);
193 des3_setup(abKeyValue
, 24, 0, &pKeyContext
->des3
);
197 des_setup(abKeyValue
, 8, 0, &pKeyContext
->des
);
204 BOOL
duplicate_key_impl(ALG_ID aiAlgid
, CONST KEY_CONTEXT
*pSrcKeyContext
,
205 KEY_CONTEXT
*pDestKeyContext
)
214 memcpy(pDestKeyContext
, pSrcKeyContext
, sizeof(KEY_CONTEXT
));
218 pDestKeyContext
->rsa
.type
= pSrcKeyContext
->rsa
.type
;
219 mp_init_copy(&pDestKeyContext
->rsa
.e
, &pSrcKeyContext
->rsa
.e
);
220 mp_init_copy(&pDestKeyContext
->rsa
.d
, &pSrcKeyContext
->rsa
.d
);
221 mp_init_copy(&pDestKeyContext
->rsa
.N
, &pSrcKeyContext
->rsa
.N
);
222 mp_init_copy(&pDestKeyContext
->rsa
.p
, &pSrcKeyContext
->rsa
.p
);
223 mp_init_copy(&pDestKeyContext
->rsa
.q
, &pSrcKeyContext
->rsa
.q
);
224 mp_init_copy(&pDestKeyContext
->rsa
.qP
, &pSrcKeyContext
->rsa
.qP
);
225 mp_init_copy(&pDestKeyContext
->rsa
.dP
, &pSrcKeyContext
->rsa
.dP
);
226 mp_init_copy(&pDestKeyContext
->rsa
.dQ
, &pSrcKeyContext
->rsa
.dQ
);
230 SetLastError(NTE_BAD_ALGID
);
237 static inline void reverse_bytes(BYTE
*pbData
, DWORD dwLen
) {
241 for (i
=0; i
<dwLen
/2; i
++) {
243 pbData
[i
] = pbData
[dwLen
-i
-1];
244 pbData
[dwLen
-i
-1] = swap
;
248 BOOL
encrypt_block_impl(ALG_ID aiAlgid
, KEY_CONTEXT
*pKeyContext
, CONST BYTE
*in
, BYTE
*out
,
251 unsigned long inlen
, outlen
;
252 BYTE
*in_reversed
= NULL
;
258 rc2_ecb_encrypt(in
, out
, &pKeyContext
->rc2
);
260 rc2_ecb_decrypt(in
, out
, &pKeyContext
->rc2
);
267 des3_ecb_encrypt(in
, out
, &pKeyContext
->des3
);
269 des3_ecb_decrypt(in
, out
, &pKeyContext
->des3
);
275 des_ecb_encrypt(in
, out
, &pKeyContext
->des
);
277 des_ecb_decrypt(in
, out
, &pKeyContext
->des
);
283 outlen
= inlen
= (mp_count_bits(&pKeyContext
->rsa
.N
)+7)/8;
285 if (aiAlgid
== CALG_RSA_SIGN
) {
290 if (rsa_exptmod(in
, inlen
, out
, &outlen
, key
, &pKeyContext
->rsa
) != CRYPT_OK
) {
291 SetLastError(NTE_FAIL
);
294 reverse_bytes(out
, outlen
);
296 if (aiAlgid
== CALG_RSA_SIGN
) {
301 in_reversed
= HeapAlloc(GetProcessHeap(), 0, inlen
);
303 SetLastError(NTE_NO_MEMORY
);
306 memcpy(in_reversed
, in
, inlen
);
307 reverse_bytes(in_reversed
, inlen
);
308 if (rsa_exptmod(in_reversed
, inlen
, out
, &outlen
, key
, &pKeyContext
->rsa
) != CRYPT_OK
) {
309 HeapFree(GetProcessHeap(), 0, in_reversed
);
310 SetLastError(NTE_FAIL
);
313 HeapFree(GetProcessHeap(), 0, in_reversed
);
318 SetLastError(NTE_BAD_ALGID
);
325 BOOL
encrypt_stream_impl(ALG_ID aiAlgid
, KEY_CONTEXT
*pKeyContext
, BYTE
*stream
, DWORD dwLen
)
329 rc4_read(stream
, dwLen
, &pKeyContext
->rc4
);
333 SetLastError(NTE_BAD_ALGID
);
340 BOOL
gen_rand_impl(BYTE
*pbBuffer
, DWORD dwLen
)
342 return SystemFunction036(pbBuffer
, dwLen
);
345 BOOL
export_public_key_impl(BYTE
*pbDest
, KEY_CONTEXT
*pKeyContext
, DWORD dwKeyLen
,DWORD
*pdwPubExp
)
347 mp_to_unsigned_bin(&pKeyContext
->rsa
.N
, pbDest
);
348 reverse_bytes(pbDest
, dwKeyLen
);
349 *pdwPubExp
= (DWORD
)mp_get_int(&pKeyContext
->rsa
.e
);
353 BOOL
import_public_key_impl(CONST BYTE
*pbSrc
, KEY_CONTEXT
*pKeyContext
, DWORD dwKeyLen
,
358 if (mp_init_multi(&pKeyContext
->rsa
.e
, &pKeyContext
->rsa
.d
, &pKeyContext
->rsa
.N
,
359 &pKeyContext
->rsa
.dQ
,&pKeyContext
->rsa
.dP
,&pKeyContext
->rsa
.qP
,
360 &pKeyContext
->rsa
.p
, &pKeyContext
->rsa
.q
, NULL
) != MP_OKAY
)
362 SetLastError(NTE_FAIL
);
366 pbTemp
= HeapAlloc(GetProcessHeap(), 0, dwKeyLen
);
367 if (!pbTemp
) return FALSE
;
368 memcpy(pbTemp
, pbSrc
, dwKeyLen
);
370 pKeyContext
->rsa
.type
= PK_PUBLIC
;
371 reverse_bytes(pbTemp
, dwKeyLen
);
372 mp_read_unsigned_bin(&pKeyContext
->rsa
.N
, pbTemp
, dwKeyLen
);
373 HeapFree(GetProcessHeap(), 0, pbTemp
);
374 mp_set_int(&pKeyContext
->rsa
.e
, dwPubExp
);
379 BOOL
export_private_key_impl(BYTE
*pbDest
, KEY_CONTEXT
*pKeyContext
, DWORD dwKeyLen
,
382 mp_to_unsigned_bin(&pKeyContext
->rsa
.N
, pbDest
);
383 reverse_bytes(pbDest
, dwKeyLen
);
385 mp_to_unsigned_bin(&pKeyContext
->rsa
.p
, pbDest
);
386 reverse_bytes(pbDest
, (dwKeyLen
+1)>>1);
387 pbDest
+= (dwKeyLen
+1)>>1;
388 mp_to_unsigned_bin(&pKeyContext
->rsa
.q
, pbDest
);
389 reverse_bytes(pbDest
, (dwKeyLen
+1)>>1);
390 pbDest
+= (dwKeyLen
+1)>>1;
391 mp_to_unsigned_bin(&pKeyContext
->rsa
.dP
, pbDest
);
392 reverse_bytes(pbDest
, (dwKeyLen
+1)>>1);
393 pbDest
+= (dwKeyLen
+1)>>1;
394 mp_to_unsigned_bin(&pKeyContext
->rsa
.dQ
, pbDest
);
395 reverse_bytes(pbDest
, (dwKeyLen
+1)>>1);
396 pbDest
+= (dwKeyLen
+1)>>1;
397 mp_to_unsigned_bin(&pKeyContext
->rsa
.qP
, pbDest
);
398 reverse_bytes(pbDest
, (dwKeyLen
+1)>>1);
399 pbDest
+= (dwKeyLen
+1)>>1;
400 mp_to_unsigned_bin(&pKeyContext
->rsa
.d
, pbDest
);
401 reverse_bytes(pbDest
, dwKeyLen
);
402 *pdwPubExp
= (DWORD
)mp_get_int(&pKeyContext
->rsa
.e
);
407 BOOL
import_private_key_impl(CONST BYTE
*pbSrc
, KEY_CONTEXT
*pKeyContext
, DWORD dwKeyLen
,
410 BYTE
*pbTemp
, *pbBigNum
;
412 if (mp_init_multi(&pKeyContext
->rsa
.e
, &pKeyContext
->rsa
.d
, &pKeyContext
->rsa
.N
,
413 &pKeyContext
->rsa
.dQ
,&pKeyContext
->rsa
.dP
,&pKeyContext
->rsa
.qP
,
414 &pKeyContext
->rsa
.p
, &pKeyContext
->rsa
.q
, NULL
) != MP_OKAY
)
416 SetLastError(NTE_FAIL
);
420 pbTemp
= HeapAlloc(GetProcessHeap(), 0, 2*dwKeyLen
+5*((dwKeyLen
+1)>>1));
421 if (!pbTemp
) return FALSE
;
422 memcpy(pbTemp
, pbSrc
, 2*dwKeyLen
+5*((dwKeyLen
+1)>>1));
425 pKeyContext
->rsa
.type
= PK_PRIVATE
;
426 reverse_bytes(pbBigNum
, dwKeyLen
);
427 mp_read_unsigned_bin(&pKeyContext
->rsa
.N
, pbBigNum
, dwKeyLen
);
428 pbBigNum
+= dwKeyLen
;
429 reverse_bytes(pbBigNum
, (dwKeyLen
+1)>>1);
430 mp_read_unsigned_bin(&pKeyContext
->rsa
.p
, pbBigNum
, (dwKeyLen
+1)>>1);
431 pbBigNum
+= (dwKeyLen
+1)>>1;
432 reverse_bytes(pbBigNum
, (dwKeyLen
+1)>>1);
433 mp_read_unsigned_bin(&pKeyContext
->rsa
.q
, pbBigNum
, (dwKeyLen
+1)>>1);
434 pbBigNum
+= (dwKeyLen
+1)>>1;
435 reverse_bytes(pbBigNum
, (dwKeyLen
+1)>>1);
436 mp_read_unsigned_bin(&pKeyContext
->rsa
.dP
, pbBigNum
, (dwKeyLen
+1)>>1);
437 pbBigNum
+= (dwKeyLen
+1)>>1;
438 reverse_bytes(pbBigNum
, (dwKeyLen
+1)>>1);
439 mp_read_unsigned_bin(&pKeyContext
->rsa
.dQ
, pbBigNum
, (dwKeyLen
+1)>>1);
440 pbBigNum
+= (dwKeyLen
+1)>>1;
441 reverse_bytes(pbBigNum
, (dwKeyLen
+1)>>1);
442 mp_read_unsigned_bin(&pKeyContext
->rsa
.qP
, pbBigNum
, (dwKeyLen
+1)>>1);
443 pbBigNum
+= (dwKeyLen
+1)>>1;
444 reverse_bytes(pbBigNum
, dwKeyLen
);
445 mp_read_unsigned_bin(&pKeyContext
->rsa
.d
, pbBigNum
, dwKeyLen
);
446 mp_set_int(&pKeyContext
->rsa
.e
, dwPubExp
);
448 HeapFree(GetProcessHeap(), 0, pbTemp
);