2 * dlls/rsaenh/implglue.c
3 * Glueing the RSAENH specific code to the crypto library
5 * Copyright (c) 2004, 2005 Michael Jung
6 * Copyright (c) 2007 Vijay Kiran Kamuju
8 * based on code by Mike McCormack and David Hammerton
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "wine/port.h"
27 #include "wine/library.h"
36 /* Function prototypes copied from dlls/advapi32/crypt_md4.c */
37 VOID WINAPI
MD4Init( MD4_CTX
*ctx
);
38 VOID WINAPI
MD4Update( MD4_CTX
*ctx
, const unsigned char *buf
, unsigned int len
);
39 VOID WINAPI
MD4Final( MD4_CTX
*ctx
);
40 /* Function prototypes copied from dlls/advapi32/crypt_md5.c */
41 VOID WINAPI
MD5Init( MD5_CTX
*ctx
);
42 VOID WINAPI
MD5Update( MD5_CTX
*ctx
, const unsigned char *buf
, unsigned int len
);
43 VOID WINAPI
MD5Final( MD5_CTX
*ctx
);
44 /* Function prototypes copied from dlls/advapi32/crypt_sha.c */
45 VOID WINAPI
A_SHAInit(PSHA_CTX Context
);
46 VOID WINAPI
A_SHAUpdate(PSHA_CTX Context
, const unsigned char *Buffer
, UINT BufferSize
);
47 VOID WINAPI
A_SHAFinal(PSHA_CTX Context
, PULONG Result
);
48 /* Function prototype copied from dlls/advapi32/crypt.c */
49 BOOL WINAPI
SystemFunction036(PVOID pbBuffer
, ULONG dwLen
);
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
);
75 BOOL
update_hash_impl(ALG_ID aiAlgid
, HASH_CONTEXT
*pHashContext
, CONST BYTE
*pbData
,
81 md2_process(&pHashContext
->md2
, pbData
, dwDataLen
);
85 MD4Update(&pHashContext
->md4
, pbData
, dwDataLen
);
89 MD5Update(&pHashContext
->md5
, pbData
, dwDataLen
);
93 A_SHAUpdate(&pHashContext
->sha
, pbData
, dwDataLen
);
97 SetLastError(NTE_BAD_ALGID
);
104 BOOL
finalize_hash_impl(ALG_ID aiAlgid
, HASH_CONTEXT
*pHashContext
, BYTE
*pbHashValue
)
109 md2_done(&pHashContext
->md2
, pbHashValue
);
113 MD4Final(&pHashContext
->md4
);
114 memcpy(pbHashValue
, pHashContext
->md4
.digest
, 16);
118 MD5Final(&pHashContext
->md5
);
119 memcpy(pbHashValue
, pHashContext
->md5
.digest
, 16);
123 A_SHAFinal(&pHashContext
->sha
, (PULONG
)pbHashValue
);
127 SetLastError(NTE_BAD_ALGID
);
134 BOOL
duplicate_hash_impl(ALG_ID aiAlgid
, CONST HASH_CONTEXT
*pSrcHashContext
,
135 HASH_CONTEXT
*pDestHashContext
)
137 *pDestHashContext
= *pSrcHashContext
;
142 BOOL
new_key_impl(ALG_ID aiAlgid
, KEY_CONTEXT
*pKeyContext
, DWORD dwKeyLen
)
148 if (rsa_make_key((int)dwKeyLen
, 65537, &pKeyContext
->rsa
) != CRYPT_OK
) {
149 SetLastError(NTE_FAIL
);
158 BOOL
free_key_impl(ALG_ID aiAlgid
, KEY_CONTEXT
*pKeyContext
)
164 rsa_free(&pKeyContext
->rsa
);
170 BOOL
setup_key_impl(ALG_ID aiAlgid
, KEY_CONTEXT
*pKeyContext
, DWORD dwKeyLen
,
171 DWORD dwEffectiveKeyLen
, DWORD dwSaltLen
, BYTE
*abKeyValue
)
176 rc4_start(&pKeyContext
->rc4
);
177 rc4_add_entropy(abKeyValue
, dwKeyLen
+ dwSaltLen
, &pKeyContext
->rc4
);
178 rc4_ready(&pKeyContext
->rc4
);
182 rc2_setup(abKeyValue
, dwKeyLen
+ dwSaltLen
, dwEffectiveKeyLen
?
183 dwEffectiveKeyLen
: dwKeyLen
<< 3, 0, &pKeyContext
->rc2
);
187 des3_setup(abKeyValue
, 24, 0, &pKeyContext
->des3
);
191 memcpy(abKeyValue
+16, abKeyValue
, 8);
192 des3_setup(abKeyValue
, 24, 0, &pKeyContext
->des3
);
196 des_setup(abKeyValue
, 8, 0, &pKeyContext
->des
);
201 aes_setup(abKeyValue
, 16, 0, &pKeyContext
->aes
);
205 aes_setup(abKeyValue
, 24, 0, &pKeyContext
->aes
);
209 aes_setup(abKeyValue
, 32, 0, &pKeyContext
->aes
);
216 BOOL
duplicate_key_impl(ALG_ID aiAlgid
, CONST KEY_CONTEXT
*pSrcKeyContext
,
217 KEY_CONTEXT
*pDestKeyContext
)
230 *pDestKeyContext
= *pSrcKeyContext
;
234 pDestKeyContext
->rsa
.type
= pSrcKeyContext
->rsa
.type
;
235 mp_init_copy(&pDestKeyContext
->rsa
.e
, &pSrcKeyContext
->rsa
.e
);
236 mp_init_copy(&pDestKeyContext
->rsa
.d
, &pSrcKeyContext
->rsa
.d
);
237 mp_init_copy(&pDestKeyContext
->rsa
.N
, &pSrcKeyContext
->rsa
.N
);
238 mp_init_copy(&pDestKeyContext
->rsa
.p
, &pSrcKeyContext
->rsa
.p
);
239 mp_init_copy(&pDestKeyContext
->rsa
.q
, &pSrcKeyContext
->rsa
.q
);
240 mp_init_copy(&pDestKeyContext
->rsa
.qP
, &pSrcKeyContext
->rsa
.qP
);
241 mp_init_copy(&pDestKeyContext
->rsa
.dP
, &pSrcKeyContext
->rsa
.dP
);
242 mp_init_copy(&pDestKeyContext
->rsa
.dQ
, &pSrcKeyContext
->rsa
.dQ
);
246 SetLastError(NTE_BAD_ALGID
);
253 static inline void reverse_bytes(BYTE
*pbData
, DWORD dwLen
) {
257 for (i
=0; i
<dwLen
/2; i
++) {
259 pbData
[i
] = pbData
[dwLen
-i
-1];
260 pbData
[dwLen
-i
-1] = swap
;
264 BOOL
encrypt_block_impl(ALG_ID aiAlgid
, DWORD dwKeySpec
, KEY_CONTEXT
*pKeyContext
, CONST BYTE
*in
, BYTE
*out
,
267 unsigned long inlen
, outlen
;
268 BYTE
*in_reversed
= NULL
;
273 rc2_ecb_encrypt(in
, out
, &pKeyContext
->rc2
);
275 rc2_ecb_decrypt(in
, out
, &pKeyContext
->rc2
);
282 des3_ecb_encrypt(in
, out
, &pKeyContext
->des3
);
284 des3_ecb_decrypt(in
, out
, &pKeyContext
->des3
);
290 des_ecb_encrypt(in
, out
, &pKeyContext
->des
);
292 des_ecb_decrypt(in
, out
, &pKeyContext
->des
);
301 aes_ecb_encrypt(in
, out
, &pKeyContext
->aes
);
303 aes_ecb_decrypt(in
, out
, &pKeyContext
->aes
);
309 outlen
= inlen
= (mp_count_bits(&pKeyContext
->rsa
.N
)+7)/8;
311 if (rsa_exptmod(in
, inlen
, out
, &outlen
, dwKeySpec
, &pKeyContext
->rsa
) != CRYPT_OK
) {
312 SetLastError(NTE_FAIL
);
315 reverse_bytes(out
, outlen
);
317 in_reversed
= HeapAlloc(GetProcessHeap(), 0, inlen
);
319 SetLastError(NTE_NO_MEMORY
);
322 memcpy(in_reversed
, in
, inlen
);
323 reverse_bytes(in_reversed
, inlen
);
324 if (rsa_exptmod(in_reversed
, inlen
, out
, &outlen
, dwKeySpec
, &pKeyContext
->rsa
) != CRYPT_OK
) {
325 HeapFree(GetProcessHeap(), 0, in_reversed
);
326 SetLastError(NTE_FAIL
);
329 HeapFree(GetProcessHeap(), 0, in_reversed
);
334 SetLastError(NTE_BAD_ALGID
);
341 BOOL
encrypt_stream_impl(ALG_ID aiAlgid
, KEY_CONTEXT
*pKeyContext
, BYTE
*stream
, DWORD dwLen
)
345 rc4_read(stream
, dwLen
, &pKeyContext
->rc4
);
349 SetLastError(NTE_BAD_ALGID
);
356 BOOL
gen_rand_impl(BYTE
*pbBuffer
, DWORD dwLen
)
358 return SystemFunction036(pbBuffer
, dwLen
);
361 BOOL
export_public_key_impl(BYTE
*pbDest
, const KEY_CONTEXT
*pKeyContext
, DWORD dwKeyLen
,DWORD
*pdwPubExp
)
363 mp_to_unsigned_bin(&pKeyContext
->rsa
.N
, pbDest
);
364 reverse_bytes(pbDest
, mp_unsigned_bin_size(&pKeyContext
->rsa
.N
));
365 if (mp_unsigned_bin_size(&pKeyContext
->rsa
.N
) < dwKeyLen
)
366 memset(pbDest
+ mp_unsigned_bin_size(&pKeyContext
->rsa
.N
), 0,
367 dwKeyLen
- mp_unsigned_bin_size(&pKeyContext
->rsa
.N
));
368 *pdwPubExp
= (DWORD
)mp_get_int(&pKeyContext
->rsa
.e
);
372 BOOL
import_public_key_impl(CONST BYTE
*pbSrc
, KEY_CONTEXT
*pKeyContext
, DWORD dwKeyLen
,
377 if (mp_init_multi(&pKeyContext
->rsa
.e
, &pKeyContext
->rsa
.d
, &pKeyContext
->rsa
.N
,
378 &pKeyContext
->rsa
.dQ
,&pKeyContext
->rsa
.dP
,&pKeyContext
->rsa
.qP
,
379 &pKeyContext
->rsa
.p
, &pKeyContext
->rsa
.q
, NULL
) != MP_OKAY
)
381 SetLastError(NTE_FAIL
);
385 pbTemp
= HeapAlloc(GetProcessHeap(), 0, dwKeyLen
);
386 if (!pbTemp
) return FALSE
;
387 memcpy(pbTemp
, pbSrc
, dwKeyLen
);
389 pKeyContext
->rsa
.type
= PK_PUBLIC
;
390 reverse_bytes(pbTemp
, dwKeyLen
);
391 mp_read_unsigned_bin(&pKeyContext
->rsa
.N
, pbTemp
, dwKeyLen
);
392 HeapFree(GetProcessHeap(), 0, pbTemp
);
393 mp_set_int(&pKeyContext
->rsa
.e
, dwPubExp
);
398 BOOL
export_private_key_impl(BYTE
*pbDest
, const KEY_CONTEXT
*pKeyContext
, DWORD dwKeyLen
,
401 mp_to_unsigned_bin(&pKeyContext
->rsa
.N
, pbDest
);
402 reverse_bytes(pbDest
, mp_unsigned_bin_size(&pKeyContext
->rsa
.N
));
403 if (mp_unsigned_bin_size(&pKeyContext
->rsa
.N
) < dwKeyLen
)
404 memset(pbDest
+ mp_unsigned_bin_size(&pKeyContext
->rsa
.N
), 0,
405 dwKeyLen
- mp_unsigned_bin_size(&pKeyContext
->rsa
.N
));
407 mp_to_unsigned_bin(&pKeyContext
->rsa
.p
, pbDest
);
408 reverse_bytes(pbDest
, mp_unsigned_bin_size(&pKeyContext
->rsa
.p
));
409 if (mp_unsigned_bin_size(&pKeyContext
->rsa
.p
) < (dwKeyLen
+1)>>1)
410 memset(pbDest
+ mp_unsigned_bin_size(&pKeyContext
->rsa
.p
), 0,
411 ((dwKeyLen
+1)>>1) - mp_unsigned_bin_size(&pKeyContext
->rsa
.p
));
412 pbDest
+= (dwKeyLen
+1)>>1;
413 mp_to_unsigned_bin(&pKeyContext
->rsa
.q
, pbDest
);
414 reverse_bytes(pbDest
, mp_unsigned_bin_size(&pKeyContext
->rsa
.q
));
415 if (mp_unsigned_bin_size(&pKeyContext
->rsa
.q
) < (dwKeyLen
+1)>>1)
416 memset(pbDest
+ mp_unsigned_bin_size(&pKeyContext
->rsa
.q
), 0,
417 ((dwKeyLen
+1)>>1) - mp_unsigned_bin_size(&pKeyContext
->rsa
.q
));
418 pbDest
+= (dwKeyLen
+1)>>1;
419 mp_to_unsigned_bin(&pKeyContext
->rsa
.dP
, pbDest
);
420 reverse_bytes(pbDest
, mp_unsigned_bin_size(&pKeyContext
->rsa
.dP
));
421 if (mp_unsigned_bin_size(&pKeyContext
->rsa
.dP
) < (dwKeyLen
+1)>>1)
422 memset(pbDest
+ mp_unsigned_bin_size(&pKeyContext
->rsa
.dP
), 0,
423 ((dwKeyLen
+1)>>1) - mp_unsigned_bin_size(&pKeyContext
->rsa
.dP
));
424 pbDest
+= (dwKeyLen
+1)>>1;
425 mp_to_unsigned_bin(&pKeyContext
->rsa
.dQ
, pbDest
);
426 reverse_bytes(pbDest
, mp_unsigned_bin_size(&pKeyContext
->rsa
.dQ
));
427 if (mp_unsigned_bin_size(&pKeyContext
->rsa
.dQ
) < (dwKeyLen
+1)>>1)
428 memset(pbDest
+ mp_unsigned_bin_size(&pKeyContext
->rsa
.dQ
), 0,
429 ((dwKeyLen
+1)>>1) - mp_unsigned_bin_size(&pKeyContext
->rsa
.dQ
));
430 pbDest
+= (dwKeyLen
+1)>>1;
431 mp_to_unsigned_bin(&pKeyContext
->rsa
.qP
, pbDest
);
432 reverse_bytes(pbDest
, mp_unsigned_bin_size(&pKeyContext
->rsa
.qP
));
433 if (mp_unsigned_bin_size(&pKeyContext
->rsa
.qP
) < (dwKeyLen
+1)>>1)
434 memset(pbDest
+ mp_unsigned_bin_size(&pKeyContext
->rsa
.qP
), 0,
435 ((dwKeyLen
+1)>>1) - mp_unsigned_bin_size(&pKeyContext
->rsa
.qP
));
436 pbDest
+= (dwKeyLen
+1)>>1;
437 mp_to_unsigned_bin(&pKeyContext
->rsa
.d
, pbDest
);
438 reverse_bytes(pbDest
, mp_unsigned_bin_size(&pKeyContext
->rsa
.d
));
439 if (mp_unsigned_bin_size(&pKeyContext
->rsa
.d
) < dwKeyLen
)
440 memset(pbDest
+ mp_unsigned_bin_size(&pKeyContext
->rsa
.d
), 0,
441 dwKeyLen
- mp_unsigned_bin_size(&pKeyContext
->rsa
.d
));
442 *pdwPubExp
= (DWORD
)mp_get_int(&pKeyContext
->rsa
.e
);
447 BOOL
import_private_key_impl(CONST BYTE
*pbSrc
, KEY_CONTEXT
*pKeyContext
, DWORD dwKeyLen
,
450 BYTE
*pbTemp
, *pbBigNum
;
452 if (mp_init_multi(&pKeyContext
->rsa
.e
, &pKeyContext
->rsa
.d
, &pKeyContext
->rsa
.N
,
453 &pKeyContext
->rsa
.dQ
,&pKeyContext
->rsa
.dP
,&pKeyContext
->rsa
.qP
,
454 &pKeyContext
->rsa
.p
, &pKeyContext
->rsa
.q
, NULL
) != MP_OKAY
)
456 SetLastError(NTE_FAIL
);
460 pbTemp
= HeapAlloc(GetProcessHeap(), 0, 2*dwKeyLen
+5*((dwKeyLen
+1)>>1));
461 if (!pbTemp
) return FALSE
;
462 memcpy(pbTemp
, pbSrc
, 2*dwKeyLen
+5*((dwKeyLen
+1)>>1));
465 pKeyContext
->rsa
.type
= PK_PRIVATE
;
466 reverse_bytes(pbBigNum
, dwKeyLen
);
467 mp_read_unsigned_bin(&pKeyContext
->rsa
.N
, pbBigNum
, dwKeyLen
);
468 pbBigNum
+= dwKeyLen
;
469 reverse_bytes(pbBigNum
, (dwKeyLen
+1)>>1);
470 mp_read_unsigned_bin(&pKeyContext
->rsa
.p
, pbBigNum
, (dwKeyLen
+1)>>1);
471 pbBigNum
+= (dwKeyLen
+1)>>1;
472 reverse_bytes(pbBigNum
, (dwKeyLen
+1)>>1);
473 mp_read_unsigned_bin(&pKeyContext
->rsa
.q
, pbBigNum
, (dwKeyLen
+1)>>1);
474 pbBigNum
+= (dwKeyLen
+1)>>1;
475 reverse_bytes(pbBigNum
, (dwKeyLen
+1)>>1);
476 mp_read_unsigned_bin(&pKeyContext
->rsa
.dP
, pbBigNum
, (dwKeyLen
+1)>>1);
477 pbBigNum
+= (dwKeyLen
+1)>>1;
478 reverse_bytes(pbBigNum
, (dwKeyLen
+1)>>1);
479 mp_read_unsigned_bin(&pKeyContext
->rsa
.dQ
, pbBigNum
, (dwKeyLen
+1)>>1);
480 pbBigNum
+= (dwKeyLen
+1)>>1;
481 reverse_bytes(pbBigNum
, (dwKeyLen
+1)>>1);
482 mp_read_unsigned_bin(&pKeyContext
->rsa
.qP
, pbBigNum
, (dwKeyLen
+1)>>1);
483 pbBigNum
+= (dwKeyLen
+1)>>1;
484 reverse_bytes(pbBigNum
, dwKeyLen
);
485 mp_read_unsigned_bin(&pKeyContext
->rsa
.d
, pbBigNum
, dwKeyLen
);
486 mp_set_int(&pKeyContext
->rsa
.e
, dwPubExp
);
488 HeapFree(GetProcessHeap(), 0, pbTemp
);