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
34 /* Function prototype copied from dlls/advapi32/crypt.c */
35 BOOL WINAPI
SystemFunction036(PVOID pbBuffer
, ULONG dwLen
);
37 BOOL
init_hash_impl(ALG_ID aiAlgid
, BCRYPT_HASH_HANDLE
*hash_handle
)
42 return !BCryptCreateHash(BCRYPT_MD2_ALG_HANDLE
, hash_handle
, NULL
, 0, NULL
, 0, 0);
45 return !BCryptCreateHash(BCRYPT_MD4_ALG_HANDLE
, hash_handle
, NULL
, 0, NULL
, 0, 0);
48 return !BCryptCreateHash(BCRYPT_MD5_ALG_HANDLE
, hash_handle
, NULL
, 0, NULL
, 0, 0);
51 return !BCryptCreateHash(BCRYPT_SHA1_ALG_HANDLE
, hash_handle
, NULL
, 0, NULL
, 0, 0);
54 return !BCryptCreateHash(BCRYPT_SHA256_ALG_HANDLE
, hash_handle
, NULL
, 0, NULL
, 0, 0);
57 return !BCryptCreateHash(BCRYPT_SHA384_ALG_HANDLE
, hash_handle
, NULL
, 0, NULL
, 0, 0);
60 return !BCryptCreateHash(BCRYPT_SHA512_ALG_HANDLE
, hash_handle
, NULL
, 0, NULL
, 0, 0);
67 BOOL
update_hash_impl(BCRYPT_HASH_HANDLE hash_handle
, const BYTE
*pbData
, DWORD dwDataLen
)
69 BCryptHashData(hash_handle
, (UCHAR
*)pbData
, dwDataLen
, 0);
73 BOOL
finalize_hash_impl(BCRYPT_HASH_HANDLE hash_handle
, BYTE
*hash_value
, DWORD hash_size
)
75 BCryptFinishHash(hash_handle
, hash_value
, hash_size
, 0);
76 BCryptDestroyHash(hash_handle
);
80 BOOL
duplicate_hash_impl(BCRYPT_HASH_HANDLE src_hash_handle
, BCRYPT_HASH_HANDLE
*dest_hash_handle
)
82 return !BCryptDuplicateHash(src_hash_handle
, dest_hash_handle
, NULL
, 0, 0);
85 BOOL
new_key_impl(ALG_ID aiAlgid
, KEY_CONTEXT
*pKeyContext
, DWORD dwKeyLen
)
91 if (rsa_make_key((int)dwKeyLen
, 65537, &pKeyContext
->rsa
) != CRYPT_OK
) {
92 SetLastError(NTE_FAIL
);
101 BOOL
free_key_impl(ALG_ID aiAlgid
, KEY_CONTEXT
*pKeyContext
)
107 rsa_free(&pKeyContext
->rsa
);
113 BOOL
setup_key_impl(ALG_ID aiAlgid
, KEY_CONTEXT
*pKeyContext
, DWORD dwKeyLen
,
114 DWORD dwEffectiveKeyLen
, DWORD dwSaltLen
, BYTE
*abKeyValue
)
119 rc4_start(&pKeyContext
->rc4
);
120 rc4_add_entropy(abKeyValue
, dwKeyLen
+ dwSaltLen
, &pKeyContext
->rc4
);
121 rc4_ready(&pKeyContext
->rc4
);
125 rc2_setup(abKeyValue
, dwKeyLen
+ dwSaltLen
, dwEffectiveKeyLen
?
126 dwEffectiveKeyLen
: dwKeyLen
<< 3, 0, &pKeyContext
->rc2
);
130 des3_setup(abKeyValue
, 24, 0, &pKeyContext
->des3
);
134 memcpy(abKeyValue
+16, abKeyValue
, 8);
135 des3_setup(abKeyValue
, 24, 0, &pKeyContext
->des3
);
139 des_setup(abKeyValue
, 8, 0, &pKeyContext
->des
);
144 aes_setup(abKeyValue
, 16, 0, &pKeyContext
->aes
);
148 aes_setup(abKeyValue
, 24, 0, &pKeyContext
->aes
);
152 aes_setup(abKeyValue
, 32, 0, &pKeyContext
->aes
);
159 BOOL
duplicate_key_impl(ALG_ID aiAlgid
, const KEY_CONTEXT
*pSrcKeyContext
,
160 KEY_CONTEXT
*pDestKeyContext
)
173 *pDestKeyContext
= *pSrcKeyContext
;
177 pDestKeyContext
->rsa
.type
= pSrcKeyContext
->rsa
.type
;
178 mp_init_copy(&pDestKeyContext
->rsa
.e
, &pSrcKeyContext
->rsa
.e
);
179 mp_init_copy(&pDestKeyContext
->rsa
.d
, &pSrcKeyContext
->rsa
.d
);
180 mp_init_copy(&pDestKeyContext
->rsa
.N
, &pSrcKeyContext
->rsa
.N
);
181 mp_init_copy(&pDestKeyContext
->rsa
.p
, &pSrcKeyContext
->rsa
.p
);
182 mp_init_copy(&pDestKeyContext
->rsa
.q
, &pSrcKeyContext
->rsa
.q
);
183 mp_init_copy(&pDestKeyContext
->rsa
.qP
, &pSrcKeyContext
->rsa
.qP
);
184 mp_init_copy(&pDestKeyContext
->rsa
.dP
, &pSrcKeyContext
->rsa
.dP
);
185 mp_init_copy(&pDestKeyContext
->rsa
.dQ
, &pSrcKeyContext
->rsa
.dQ
);
189 SetLastError(NTE_BAD_ALGID
);
196 static inline void reverse_bytes(BYTE
*pbData
, DWORD dwLen
) {
200 for (i
=0; i
<dwLen
/2; i
++) {
202 pbData
[i
] = pbData
[dwLen
-i
-1];
203 pbData
[dwLen
-i
-1] = swap
;
207 BOOL
encrypt_block_impl(ALG_ID aiAlgid
, DWORD dwKeySpec
, KEY_CONTEXT
*pKeyContext
, const BYTE
*in
,
208 BYTE
*out
, DWORD enc
)
210 unsigned long inlen
, outlen
;
211 BYTE
*in_reversed
= NULL
;
216 rc2_ecb_encrypt(in
, out
, &pKeyContext
->rc2
);
218 rc2_ecb_decrypt(in
, out
, &pKeyContext
->rc2
);
225 des3_ecb_encrypt(in
, out
, &pKeyContext
->des3
);
227 des3_ecb_decrypt(in
, out
, &pKeyContext
->des3
);
233 des_ecb_encrypt(in
, out
, &pKeyContext
->des
);
235 des_ecb_decrypt(in
, out
, &pKeyContext
->des
);
244 aes_ecb_encrypt(in
, out
, &pKeyContext
->aes
);
246 aes_ecb_decrypt(in
, out
, &pKeyContext
->aes
);
252 case CALG_SSL3_SHAMD5
:
253 outlen
= inlen
= (mp_count_bits(&pKeyContext
->rsa
.N
)+7)/8;
255 if (rsa_exptmod(in
, inlen
, out
, &outlen
, dwKeySpec
, &pKeyContext
->rsa
) != CRYPT_OK
) {
256 SetLastError(NTE_FAIL
);
259 reverse_bytes(out
, outlen
);
261 in_reversed
= malloc(inlen
);
263 SetLastError(NTE_NO_MEMORY
);
266 memcpy(in_reversed
, in
, inlen
);
267 reverse_bytes(in_reversed
, inlen
);
268 if (rsa_exptmod(in_reversed
, inlen
, out
, &outlen
, dwKeySpec
, &pKeyContext
->rsa
) != CRYPT_OK
) {
270 SetLastError(NTE_FAIL
);
278 SetLastError(NTE_BAD_ALGID
);
285 BOOL
encrypt_stream_impl(ALG_ID aiAlgid
, KEY_CONTEXT
*pKeyContext
, BYTE
*stream
, DWORD dwLen
)
289 rc4_read(stream
, dwLen
, &pKeyContext
->rc4
);
293 SetLastError(NTE_BAD_ALGID
);
300 BOOL
gen_rand_impl(BYTE
*pbBuffer
, DWORD dwLen
)
302 return SystemFunction036(pbBuffer
, dwLen
);
305 BOOL
export_public_key_impl(BYTE
*pbDest
, const KEY_CONTEXT
*pKeyContext
, DWORD dwKeyLen
,DWORD
*pdwPubExp
)
307 mp_to_unsigned_bin(&pKeyContext
->rsa
.N
, pbDest
);
308 reverse_bytes(pbDest
, mp_unsigned_bin_size(&pKeyContext
->rsa
.N
));
309 if (mp_unsigned_bin_size(&pKeyContext
->rsa
.N
) < dwKeyLen
)
310 memset(pbDest
+ mp_unsigned_bin_size(&pKeyContext
->rsa
.N
), 0,
311 dwKeyLen
- mp_unsigned_bin_size(&pKeyContext
->rsa
.N
));
312 *pdwPubExp
= (DWORD
)mp_get_int(&pKeyContext
->rsa
.e
);
316 BOOL
import_public_key_impl(const BYTE
*pbSrc
, KEY_CONTEXT
*pKeyContext
, DWORD dwKeyLen
,
321 if (mp_init_multi(&pKeyContext
->rsa
.e
, &pKeyContext
->rsa
.d
, &pKeyContext
->rsa
.N
,
322 &pKeyContext
->rsa
.dQ
,&pKeyContext
->rsa
.dP
,&pKeyContext
->rsa
.qP
,
323 &pKeyContext
->rsa
.p
, &pKeyContext
->rsa
.q
, NULL
) != MP_OKAY
)
325 SetLastError(NTE_FAIL
);
329 pbTemp
= malloc(dwKeyLen
);
330 if (!pbTemp
) return FALSE
;
331 memcpy(pbTemp
, pbSrc
, dwKeyLen
);
333 pKeyContext
->rsa
.type
= PK_PUBLIC
;
334 reverse_bytes(pbTemp
, dwKeyLen
);
335 mp_read_unsigned_bin(&pKeyContext
->rsa
.N
, pbTemp
, dwKeyLen
);
337 mp_set_int(&pKeyContext
->rsa
.e
, dwPubExp
);
342 BOOL
export_private_key_impl(BYTE
*pbDest
, const KEY_CONTEXT
*pKeyContext
, DWORD dwKeyLen
,
345 mp_to_unsigned_bin(&pKeyContext
->rsa
.N
, pbDest
);
346 reverse_bytes(pbDest
, mp_unsigned_bin_size(&pKeyContext
->rsa
.N
));
347 if (mp_unsigned_bin_size(&pKeyContext
->rsa
.N
) < dwKeyLen
)
348 memset(pbDest
+ mp_unsigned_bin_size(&pKeyContext
->rsa
.N
), 0,
349 dwKeyLen
- mp_unsigned_bin_size(&pKeyContext
->rsa
.N
));
351 mp_to_unsigned_bin(&pKeyContext
->rsa
.p
, pbDest
);
352 reverse_bytes(pbDest
, mp_unsigned_bin_size(&pKeyContext
->rsa
.p
));
353 if (mp_unsigned_bin_size(&pKeyContext
->rsa
.p
) < (dwKeyLen
+1)>>1)
354 memset(pbDest
+ mp_unsigned_bin_size(&pKeyContext
->rsa
.p
), 0,
355 ((dwKeyLen
+1)>>1) - mp_unsigned_bin_size(&pKeyContext
->rsa
.p
));
356 pbDest
+= (dwKeyLen
+1)>>1;
357 mp_to_unsigned_bin(&pKeyContext
->rsa
.q
, pbDest
);
358 reverse_bytes(pbDest
, mp_unsigned_bin_size(&pKeyContext
->rsa
.q
));
359 if (mp_unsigned_bin_size(&pKeyContext
->rsa
.q
) < (dwKeyLen
+1)>>1)
360 memset(pbDest
+ mp_unsigned_bin_size(&pKeyContext
->rsa
.q
), 0,
361 ((dwKeyLen
+1)>>1) - mp_unsigned_bin_size(&pKeyContext
->rsa
.q
));
362 pbDest
+= (dwKeyLen
+1)>>1;
363 mp_to_unsigned_bin(&pKeyContext
->rsa
.dP
, pbDest
);
364 reverse_bytes(pbDest
, mp_unsigned_bin_size(&pKeyContext
->rsa
.dP
));
365 if (mp_unsigned_bin_size(&pKeyContext
->rsa
.dP
) < (dwKeyLen
+1)>>1)
366 memset(pbDest
+ mp_unsigned_bin_size(&pKeyContext
->rsa
.dP
), 0,
367 ((dwKeyLen
+1)>>1) - mp_unsigned_bin_size(&pKeyContext
->rsa
.dP
));
368 pbDest
+= (dwKeyLen
+1)>>1;
369 mp_to_unsigned_bin(&pKeyContext
->rsa
.dQ
, pbDest
);
370 reverse_bytes(pbDest
, mp_unsigned_bin_size(&pKeyContext
->rsa
.dQ
));
371 if (mp_unsigned_bin_size(&pKeyContext
->rsa
.dQ
) < (dwKeyLen
+1)>>1)
372 memset(pbDest
+ mp_unsigned_bin_size(&pKeyContext
->rsa
.dQ
), 0,
373 ((dwKeyLen
+1)>>1) - mp_unsigned_bin_size(&pKeyContext
->rsa
.dQ
));
374 pbDest
+= (dwKeyLen
+1)>>1;
375 mp_to_unsigned_bin(&pKeyContext
->rsa
.qP
, pbDest
);
376 reverse_bytes(pbDest
, mp_unsigned_bin_size(&pKeyContext
->rsa
.qP
));
377 if (mp_unsigned_bin_size(&pKeyContext
->rsa
.qP
) < (dwKeyLen
+1)>>1)
378 memset(pbDest
+ mp_unsigned_bin_size(&pKeyContext
->rsa
.qP
), 0,
379 ((dwKeyLen
+1)>>1) - mp_unsigned_bin_size(&pKeyContext
->rsa
.qP
));
380 pbDest
+= (dwKeyLen
+1)>>1;
381 mp_to_unsigned_bin(&pKeyContext
->rsa
.d
, pbDest
);
382 reverse_bytes(pbDest
, mp_unsigned_bin_size(&pKeyContext
->rsa
.d
));
383 if (mp_unsigned_bin_size(&pKeyContext
->rsa
.d
) < dwKeyLen
)
384 memset(pbDest
+ mp_unsigned_bin_size(&pKeyContext
->rsa
.d
), 0,
385 dwKeyLen
- mp_unsigned_bin_size(&pKeyContext
->rsa
.d
));
386 *pdwPubExp
= (DWORD
)mp_get_int(&pKeyContext
->rsa
.e
);
391 BOOL
import_private_key_impl(const BYTE
*pbSrc
, KEY_CONTEXT
*pKeyContext
, DWORD dwKeyLen
,
392 DWORD dwDataLen
, DWORD dwPubExp
)
394 BYTE
*pbTemp
, *pbBigNum
;
396 if (mp_init_multi(&pKeyContext
->rsa
.e
, &pKeyContext
->rsa
.d
, &pKeyContext
->rsa
.N
,
397 &pKeyContext
->rsa
.dQ
,&pKeyContext
->rsa
.dP
,&pKeyContext
->rsa
.qP
,
398 &pKeyContext
->rsa
.p
, &pKeyContext
->rsa
.q
, NULL
) != MP_OKAY
)
400 SetLastError(NTE_FAIL
);
404 pbTemp
= malloc(2*dwKeyLen
+5*((dwKeyLen
+1)>>1));
405 if (!pbTemp
) return FALSE
;
406 memcpy(pbTemp
, pbSrc
, min(dwDataLen
, 2*dwKeyLen
+5*((dwKeyLen
+1)>>1)));
409 pKeyContext
->rsa
.type
= PK_PRIVATE
;
410 reverse_bytes(pbBigNum
, dwKeyLen
);
411 mp_read_unsigned_bin(&pKeyContext
->rsa
.N
, pbBigNum
, dwKeyLen
);
412 pbBigNum
+= dwKeyLen
;
413 reverse_bytes(pbBigNum
, (dwKeyLen
+1)>>1);
414 mp_read_unsigned_bin(&pKeyContext
->rsa
.p
, pbBigNum
, (dwKeyLen
+1)>>1);
415 pbBigNum
+= (dwKeyLen
+1)>>1;
416 reverse_bytes(pbBigNum
, (dwKeyLen
+1)>>1);
417 mp_read_unsigned_bin(&pKeyContext
->rsa
.q
, pbBigNum
, (dwKeyLen
+1)>>1);
418 pbBigNum
+= (dwKeyLen
+1)>>1;
419 reverse_bytes(pbBigNum
, (dwKeyLen
+1)>>1);
420 mp_read_unsigned_bin(&pKeyContext
->rsa
.dP
, pbBigNum
, (dwKeyLen
+1)>>1);
421 pbBigNum
+= (dwKeyLen
+1)>>1;
422 reverse_bytes(pbBigNum
, (dwKeyLen
+1)>>1);
423 mp_read_unsigned_bin(&pKeyContext
->rsa
.dQ
, pbBigNum
, (dwKeyLen
+1)>>1);
424 pbBigNum
+= (dwKeyLen
+1)>>1;
425 reverse_bytes(pbBigNum
, (dwKeyLen
+1)>>1);
426 mp_read_unsigned_bin(&pKeyContext
->rsa
.qP
, pbBigNum
, (dwKeyLen
+1)>>1);
427 pbBigNum
+= (dwKeyLen
+1)>>1;
428 /* The size of the private exponent d is inferred from the remaining
431 dwKeyLen
= min(dwKeyLen
, dwDataLen
- (pbBigNum
- pbTemp
));
432 reverse_bytes(pbBigNum
, dwKeyLen
);
433 mp_read_unsigned_bin(&pKeyContext
->rsa
.d
, pbBigNum
, dwKeyLen
);
434 mp_set_int(&pKeyContext
->rsa
.e
, dwPubExp
);