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
, DWORD dwSaltLen
,
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
, dwKeyLen
<< 3, 0, &pKeyContext
->rc2
);
185 des3_setup(abKeyValue
, 24, 0, &pKeyContext
->des3
);
189 memcpy(abKeyValue
+16, abKeyValue
, 8);
190 des3_setup(abKeyValue
, 24, 0, &pKeyContext
->des3
);
194 des_setup(abKeyValue
, 8, 0, &pKeyContext
->des
);
201 BOOL
duplicate_key_impl(ALG_ID aiAlgid
, CONST KEY_CONTEXT
*pSrcKeyContext
,
202 KEY_CONTEXT
*pDestKeyContext
)
211 memcpy(pDestKeyContext
, pSrcKeyContext
, sizeof(KEY_CONTEXT
));
215 pDestKeyContext
->rsa
.type
= pSrcKeyContext
->rsa
.type
;
216 mp_init_copy(&pDestKeyContext
->rsa
.e
, &pSrcKeyContext
->rsa
.e
);
217 mp_init_copy(&pDestKeyContext
->rsa
.d
, &pSrcKeyContext
->rsa
.d
);
218 mp_init_copy(&pDestKeyContext
->rsa
.N
, &pSrcKeyContext
->rsa
.N
);
219 mp_init_copy(&pDestKeyContext
->rsa
.p
, &pSrcKeyContext
->rsa
.p
);
220 mp_init_copy(&pDestKeyContext
->rsa
.q
, &pSrcKeyContext
->rsa
.q
);
221 mp_init_copy(&pDestKeyContext
->rsa
.qP
, &pSrcKeyContext
->rsa
.qP
);
222 mp_init_copy(&pDestKeyContext
->rsa
.dP
, &pSrcKeyContext
->rsa
.dP
);
223 mp_init_copy(&pDestKeyContext
->rsa
.dQ
, &pSrcKeyContext
->rsa
.dQ
);
227 SetLastError(NTE_BAD_ALGID
);
234 static inline void reverse_bytes(BYTE
*pbData
, DWORD dwLen
) {
238 for (i
=0; i
<dwLen
/2; i
++) {
240 pbData
[i
] = pbData
[dwLen
-i
-1];
241 pbData
[dwLen
-i
-1] = swap
;
245 BOOL
encrypt_block_impl(ALG_ID aiAlgid
, DWORD dwKeySpec
, KEY_CONTEXT
*pKeyContext
, CONST BYTE
*in
, BYTE
*out
,
248 unsigned long inlen
, outlen
;
249 BYTE
*in_reversed
= NULL
;
254 rc2_ecb_encrypt(in
, out
, &pKeyContext
->rc2
);
256 rc2_ecb_decrypt(in
, out
, &pKeyContext
->rc2
);
263 des3_ecb_encrypt(in
, out
, &pKeyContext
->des3
);
265 des3_ecb_decrypt(in
, out
, &pKeyContext
->des3
);
271 des_ecb_encrypt(in
, out
, &pKeyContext
->des
);
273 des_ecb_decrypt(in
, out
, &pKeyContext
->des
);
279 outlen
= inlen
= (mp_count_bits(&pKeyContext
->rsa
.N
)+7)/8;
281 if (rsa_exptmod(in
, inlen
, out
, &outlen
, dwKeySpec
, &pKeyContext
->rsa
) != CRYPT_OK
) {
282 SetLastError(NTE_FAIL
);
285 reverse_bytes(out
, outlen
);
287 in_reversed
= HeapAlloc(GetProcessHeap(), 0, inlen
);
289 SetLastError(NTE_NO_MEMORY
);
292 memcpy(in_reversed
, in
, inlen
);
293 reverse_bytes(in_reversed
, inlen
);
294 if (rsa_exptmod(in_reversed
, inlen
, out
, &outlen
, dwKeySpec
, &pKeyContext
->rsa
) != CRYPT_OK
) {
295 HeapFree(GetProcessHeap(), 0, in_reversed
);
296 SetLastError(NTE_FAIL
);
299 HeapFree(GetProcessHeap(), 0, in_reversed
);
304 SetLastError(NTE_BAD_ALGID
);
311 BOOL
encrypt_stream_impl(ALG_ID aiAlgid
, KEY_CONTEXT
*pKeyContext
, BYTE
*stream
, DWORD dwLen
)
315 rc4_read(stream
, dwLen
, &pKeyContext
->rc4
);
319 SetLastError(NTE_BAD_ALGID
);
326 BOOL
gen_rand_impl(BYTE
*pbBuffer
, DWORD dwLen
)
328 return SystemFunction036(pbBuffer
, dwLen
);
331 BOOL
export_public_key_impl(BYTE
*pbDest
, const KEY_CONTEXT
*pKeyContext
, DWORD dwKeyLen
,DWORD
*pdwPubExp
)
333 mp_to_unsigned_bin(&pKeyContext
->rsa
.N
, pbDest
);
334 reverse_bytes(pbDest
, dwKeyLen
);
335 *pdwPubExp
= (DWORD
)mp_get_int(&pKeyContext
->rsa
.e
);
339 BOOL
import_public_key_impl(CONST BYTE
*pbSrc
, KEY_CONTEXT
*pKeyContext
, DWORD dwKeyLen
,
344 if (mp_init_multi(&pKeyContext
->rsa
.e
, &pKeyContext
->rsa
.d
, &pKeyContext
->rsa
.N
,
345 &pKeyContext
->rsa
.dQ
,&pKeyContext
->rsa
.dP
,&pKeyContext
->rsa
.qP
,
346 &pKeyContext
->rsa
.p
, &pKeyContext
->rsa
.q
, NULL
) != MP_OKAY
)
348 SetLastError(NTE_FAIL
);
352 pbTemp
= HeapAlloc(GetProcessHeap(), 0, dwKeyLen
);
353 if (!pbTemp
) return FALSE
;
354 memcpy(pbTemp
, pbSrc
, dwKeyLen
);
356 pKeyContext
->rsa
.type
= PK_PUBLIC
;
357 reverse_bytes(pbTemp
, dwKeyLen
);
358 mp_read_unsigned_bin(&pKeyContext
->rsa
.N
, pbTemp
, dwKeyLen
);
359 HeapFree(GetProcessHeap(), 0, pbTemp
);
360 mp_set_int(&pKeyContext
->rsa
.e
, dwPubExp
);
365 BOOL
export_private_key_impl(BYTE
*pbDest
, const KEY_CONTEXT
*pKeyContext
, DWORD dwKeyLen
,
368 mp_to_unsigned_bin(&pKeyContext
->rsa
.N
, pbDest
);
369 reverse_bytes(pbDest
, dwKeyLen
);
371 mp_to_unsigned_bin(&pKeyContext
->rsa
.p
, pbDest
);
372 reverse_bytes(pbDest
, (dwKeyLen
+1)>>1);
373 pbDest
+= (dwKeyLen
+1)>>1;
374 mp_to_unsigned_bin(&pKeyContext
->rsa
.q
, pbDest
);
375 reverse_bytes(pbDest
, (dwKeyLen
+1)>>1);
376 pbDest
+= (dwKeyLen
+1)>>1;
377 mp_to_unsigned_bin(&pKeyContext
->rsa
.dP
, pbDest
);
378 reverse_bytes(pbDest
, (dwKeyLen
+1)>>1);
379 pbDest
+= (dwKeyLen
+1)>>1;
380 mp_to_unsigned_bin(&pKeyContext
->rsa
.dQ
, pbDest
);
381 reverse_bytes(pbDest
, (dwKeyLen
+1)>>1);
382 pbDest
+= (dwKeyLen
+1)>>1;
383 mp_to_unsigned_bin(&pKeyContext
->rsa
.qP
, pbDest
);
384 reverse_bytes(pbDest
, (dwKeyLen
+1)>>1);
385 pbDest
+= (dwKeyLen
+1)>>1;
386 mp_to_unsigned_bin(&pKeyContext
->rsa
.d
, pbDest
);
387 reverse_bytes(pbDest
, dwKeyLen
);
388 *pdwPubExp
= (DWORD
)mp_get_int(&pKeyContext
->rsa
.e
);
393 BOOL
import_private_key_impl(CONST BYTE
*pbSrc
, KEY_CONTEXT
*pKeyContext
, DWORD dwKeyLen
,
396 BYTE
*pbTemp
, *pbBigNum
;
398 if (mp_init_multi(&pKeyContext
->rsa
.e
, &pKeyContext
->rsa
.d
, &pKeyContext
->rsa
.N
,
399 &pKeyContext
->rsa
.dQ
,&pKeyContext
->rsa
.dP
,&pKeyContext
->rsa
.qP
,
400 &pKeyContext
->rsa
.p
, &pKeyContext
->rsa
.q
, NULL
) != MP_OKAY
)
402 SetLastError(NTE_FAIL
);
406 pbTemp
= HeapAlloc(GetProcessHeap(), 0, 2*dwKeyLen
+5*((dwKeyLen
+1)>>1));
407 if (!pbTemp
) return FALSE
;
408 memcpy(pbTemp
, pbSrc
, 2*dwKeyLen
+5*((dwKeyLen
+1)>>1));
411 pKeyContext
->rsa
.type
= PK_PRIVATE
;
412 reverse_bytes(pbBigNum
, dwKeyLen
);
413 mp_read_unsigned_bin(&pKeyContext
->rsa
.N
, pbBigNum
, dwKeyLen
);
414 pbBigNum
+= dwKeyLen
;
415 reverse_bytes(pbBigNum
, (dwKeyLen
+1)>>1);
416 mp_read_unsigned_bin(&pKeyContext
->rsa
.p
, pbBigNum
, (dwKeyLen
+1)>>1);
417 pbBigNum
+= (dwKeyLen
+1)>>1;
418 reverse_bytes(pbBigNum
, (dwKeyLen
+1)>>1);
419 mp_read_unsigned_bin(&pKeyContext
->rsa
.q
, pbBigNum
, (dwKeyLen
+1)>>1);
420 pbBigNum
+= (dwKeyLen
+1)>>1;
421 reverse_bytes(pbBigNum
, (dwKeyLen
+1)>>1);
422 mp_read_unsigned_bin(&pKeyContext
->rsa
.dP
, pbBigNum
, (dwKeyLen
+1)>>1);
423 pbBigNum
+= (dwKeyLen
+1)>>1;
424 reverse_bytes(pbBigNum
, (dwKeyLen
+1)>>1);
425 mp_read_unsigned_bin(&pKeyContext
->rsa
.dQ
, pbBigNum
, (dwKeyLen
+1)>>1);
426 pbBigNum
+= (dwKeyLen
+1)>>1;
427 reverse_bytes(pbBigNum
, (dwKeyLen
+1)>>1);
428 mp_read_unsigned_bin(&pKeyContext
->rsa
.qP
, pbBigNum
, (dwKeyLen
+1)>>1);
429 pbBigNum
+= (dwKeyLen
+1)>>1;
430 reverse_bytes(pbBigNum
, dwKeyLen
);
431 mp_read_unsigned_bin(&pKeyContext
->rsa
.d
, pbBigNum
, dwKeyLen
);
432 mp_set_int(&pKeyContext
->rsa
.e
, dwPubExp
);
434 HeapFree(GetProcessHeap(), 0, pbTemp
);