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
33 /* Function prototype copied from dlls/advapi32/crypt.c */
34 BOOL WINAPI
SystemFunction036(PVOID pbBuffer
, ULONG dwLen
);
36 BOOL
init_hash_impl(ALG_ID aiAlgid
, HASH_CONTEXT
*pHashContext
)
38 BCRYPT_ALG_HANDLE provider
;
44 status
= BCryptOpenAlgorithmProvider(&provider
, BCRYPT_MD2_ALGORITHM
, MS_PRIMITIVE_PROVIDER
, 0);
48 status
= BCryptOpenAlgorithmProvider(&provider
, BCRYPT_MD4_ALGORITHM
, MS_PRIMITIVE_PROVIDER
, 0);
52 status
= BCryptOpenAlgorithmProvider(&provider
, BCRYPT_MD5_ALGORITHM
, MS_PRIMITIVE_PROVIDER
, 0);
56 status
= BCryptOpenAlgorithmProvider(&provider
, BCRYPT_SHA1_ALGORITHM
, MS_PRIMITIVE_PROVIDER
, 0);
60 status
= BCryptOpenAlgorithmProvider(&provider
, BCRYPT_SHA256_ALGORITHM
, MS_PRIMITIVE_PROVIDER
, 0);
64 status
= BCryptOpenAlgorithmProvider(&provider
, BCRYPT_SHA384_ALGORITHM
, MS_PRIMITIVE_PROVIDER
, 0);
68 status
= BCryptOpenAlgorithmProvider(&provider
, BCRYPT_SHA512_ALGORITHM
, MS_PRIMITIVE_PROVIDER
, 0);
75 if (status
) return FALSE
;
77 status
= BCryptCreateHash(provider
, &pHashContext
->bcrypt_hash
, NULL
, 0, NULL
, 0, 0);
78 BCryptCloseAlgorithmProvider(provider
, 0);
82 BOOL
update_hash_impl(HASH_CONTEXT
*pHashContext
, const BYTE
*pbData
, DWORD dwDataLen
)
84 BCryptHashData(pHashContext
->bcrypt_hash
, (UCHAR
*)pbData
, dwDataLen
, 0);
88 BOOL
finalize_hash_impl(HASH_CONTEXT
*pHashContext
, BYTE
*pbHashValue
)
90 BCryptFinishHash(pHashContext
->bcrypt_hash
, pbHashValue
, RSAENH_MAX_HASH_SIZE
, 0);
91 BCryptDestroyHash(pHashContext
->bcrypt_hash
);
95 BOOL
duplicate_hash_impl(const HASH_CONTEXT
*pSrcHashContext
, HASH_CONTEXT
*pDestHashContext
)
97 return !BCryptDuplicateHash(pSrcHashContext
->bcrypt_hash
, &pDestHashContext
->bcrypt_hash
, NULL
, 0, 0);
100 BOOL
new_key_impl(ALG_ID aiAlgid
, KEY_CONTEXT
*pKeyContext
, DWORD dwKeyLen
)
106 if (rsa_make_key((int)dwKeyLen
, 65537, &pKeyContext
->rsa
) != CRYPT_OK
) {
107 SetLastError(NTE_FAIL
);
116 BOOL
free_key_impl(ALG_ID aiAlgid
, KEY_CONTEXT
*pKeyContext
)
122 rsa_free(&pKeyContext
->rsa
);
128 BOOL
setup_key_impl(ALG_ID aiAlgid
, KEY_CONTEXT
*pKeyContext
, DWORD dwKeyLen
,
129 DWORD dwEffectiveKeyLen
, DWORD dwSaltLen
, BYTE
*abKeyValue
)
134 rc4_start(&pKeyContext
->rc4
);
135 rc4_add_entropy(abKeyValue
, dwKeyLen
+ dwSaltLen
, &pKeyContext
->rc4
);
136 rc4_ready(&pKeyContext
->rc4
);
140 rc2_setup(abKeyValue
, dwKeyLen
+ dwSaltLen
, dwEffectiveKeyLen
?
141 dwEffectiveKeyLen
: dwKeyLen
<< 3, 0, &pKeyContext
->rc2
);
145 des3_setup(abKeyValue
, 24, 0, &pKeyContext
->des3
);
149 memcpy(abKeyValue
+16, abKeyValue
, 8);
150 des3_setup(abKeyValue
, 24, 0, &pKeyContext
->des3
);
154 des_setup(abKeyValue
, 8, 0, &pKeyContext
->des
);
159 aes_setup(abKeyValue
, 16, 0, &pKeyContext
->aes
);
163 aes_setup(abKeyValue
, 24, 0, &pKeyContext
->aes
);
167 aes_setup(abKeyValue
, 32, 0, &pKeyContext
->aes
);
174 BOOL
duplicate_key_impl(ALG_ID aiAlgid
, const KEY_CONTEXT
*pSrcKeyContext
,
175 KEY_CONTEXT
*pDestKeyContext
)
188 *pDestKeyContext
= *pSrcKeyContext
;
192 pDestKeyContext
->rsa
.type
= pSrcKeyContext
->rsa
.type
;
193 mp_init_copy(&pDestKeyContext
->rsa
.e
, &pSrcKeyContext
->rsa
.e
);
194 mp_init_copy(&pDestKeyContext
->rsa
.d
, &pSrcKeyContext
->rsa
.d
);
195 mp_init_copy(&pDestKeyContext
->rsa
.N
, &pSrcKeyContext
->rsa
.N
);
196 mp_init_copy(&pDestKeyContext
->rsa
.p
, &pSrcKeyContext
->rsa
.p
);
197 mp_init_copy(&pDestKeyContext
->rsa
.q
, &pSrcKeyContext
->rsa
.q
);
198 mp_init_copy(&pDestKeyContext
->rsa
.qP
, &pSrcKeyContext
->rsa
.qP
);
199 mp_init_copy(&pDestKeyContext
->rsa
.dP
, &pSrcKeyContext
->rsa
.dP
);
200 mp_init_copy(&pDestKeyContext
->rsa
.dQ
, &pSrcKeyContext
->rsa
.dQ
);
204 SetLastError(NTE_BAD_ALGID
);
211 static inline void reverse_bytes(BYTE
*pbData
, DWORD dwLen
) {
215 for (i
=0; i
<dwLen
/2; i
++) {
217 pbData
[i
] = pbData
[dwLen
-i
-1];
218 pbData
[dwLen
-i
-1] = swap
;
222 BOOL
encrypt_block_impl(ALG_ID aiAlgid
, DWORD dwKeySpec
, KEY_CONTEXT
*pKeyContext
, const BYTE
*in
,
223 BYTE
*out
, DWORD enc
)
225 unsigned long inlen
, outlen
;
226 BYTE
*in_reversed
= NULL
;
231 rc2_ecb_encrypt(in
, out
, &pKeyContext
->rc2
);
233 rc2_ecb_decrypt(in
, out
, &pKeyContext
->rc2
);
240 des3_ecb_encrypt(in
, out
, &pKeyContext
->des3
);
242 des3_ecb_decrypt(in
, out
, &pKeyContext
->des3
);
248 des_ecb_encrypt(in
, out
, &pKeyContext
->des
);
250 des_ecb_decrypt(in
, out
, &pKeyContext
->des
);
259 aes_ecb_encrypt(in
, out
, &pKeyContext
->aes
);
261 aes_ecb_decrypt(in
, out
, &pKeyContext
->aes
);
267 case CALG_SSL3_SHAMD5
:
268 outlen
= inlen
= (mp_count_bits(&pKeyContext
->rsa
.N
)+7)/8;
270 if (rsa_exptmod(in
, inlen
, out
, &outlen
, dwKeySpec
, &pKeyContext
->rsa
) != CRYPT_OK
) {
271 SetLastError(NTE_FAIL
);
274 reverse_bytes(out
, outlen
);
276 in_reversed
= HeapAlloc(GetProcessHeap(), 0, inlen
);
278 SetLastError(NTE_NO_MEMORY
);
281 memcpy(in_reversed
, in
, inlen
);
282 reverse_bytes(in_reversed
, inlen
);
283 if (rsa_exptmod(in_reversed
, inlen
, out
, &outlen
, dwKeySpec
, &pKeyContext
->rsa
) != CRYPT_OK
) {
284 HeapFree(GetProcessHeap(), 0, in_reversed
);
285 SetLastError(NTE_FAIL
);
288 HeapFree(GetProcessHeap(), 0, in_reversed
);
293 SetLastError(NTE_BAD_ALGID
);
300 BOOL
encrypt_stream_impl(ALG_ID aiAlgid
, KEY_CONTEXT
*pKeyContext
, BYTE
*stream
, DWORD dwLen
)
304 rc4_read(stream
, dwLen
, &pKeyContext
->rc4
);
308 SetLastError(NTE_BAD_ALGID
);
315 BOOL
gen_rand_impl(BYTE
*pbBuffer
, DWORD dwLen
)
317 return SystemFunction036(pbBuffer
, dwLen
);
320 BOOL
export_public_key_impl(BYTE
*pbDest
, const KEY_CONTEXT
*pKeyContext
, DWORD dwKeyLen
,DWORD
*pdwPubExp
)
322 mp_to_unsigned_bin(&pKeyContext
->rsa
.N
, pbDest
);
323 reverse_bytes(pbDest
, mp_unsigned_bin_size(&pKeyContext
->rsa
.N
));
324 if (mp_unsigned_bin_size(&pKeyContext
->rsa
.N
) < dwKeyLen
)
325 memset(pbDest
+ mp_unsigned_bin_size(&pKeyContext
->rsa
.N
), 0,
326 dwKeyLen
- mp_unsigned_bin_size(&pKeyContext
->rsa
.N
));
327 *pdwPubExp
= (DWORD
)mp_get_int(&pKeyContext
->rsa
.e
);
331 BOOL
import_public_key_impl(const BYTE
*pbSrc
, KEY_CONTEXT
*pKeyContext
, DWORD dwKeyLen
,
336 if (mp_init_multi(&pKeyContext
->rsa
.e
, &pKeyContext
->rsa
.d
, &pKeyContext
->rsa
.N
,
337 &pKeyContext
->rsa
.dQ
,&pKeyContext
->rsa
.dP
,&pKeyContext
->rsa
.qP
,
338 &pKeyContext
->rsa
.p
, &pKeyContext
->rsa
.q
, NULL
) != MP_OKAY
)
340 SetLastError(NTE_FAIL
);
344 pbTemp
= HeapAlloc(GetProcessHeap(), 0, dwKeyLen
);
345 if (!pbTemp
) return FALSE
;
346 memcpy(pbTemp
, pbSrc
, dwKeyLen
);
348 pKeyContext
->rsa
.type
= PK_PUBLIC
;
349 reverse_bytes(pbTemp
, dwKeyLen
);
350 mp_read_unsigned_bin(&pKeyContext
->rsa
.N
, pbTemp
, dwKeyLen
);
351 HeapFree(GetProcessHeap(), 0, pbTemp
);
352 mp_set_int(&pKeyContext
->rsa
.e
, dwPubExp
);
357 BOOL
export_private_key_impl(BYTE
*pbDest
, const KEY_CONTEXT
*pKeyContext
, DWORD dwKeyLen
,
360 mp_to_unsigned_bin(&pKeyContext
->rsa
.N
, pbDest
);
361 reverse_bytes(pbDest
, mp_unsigned_bin_size(&pKeyContext
->rsa
.N
));
362 if (mp_unsigned_bin_size(&pKeyContext
->rsa
.N
) < dwKeyLen
)
363 memset(pbDest
+ mp_unsigned_bin_size(&pKeyContext
->rsa
.N
), 0,
364 dwKeyLen
- mp_unsigned_bin_size(&pKeyContext
->rsa
.N
));
366 mp_to_unsigned_bin(&pKeyContext
->rsa
.p
, pbDest
);
367 reverse_bytes(pbDest
, mp_unsigned_bin_size(&pKeyContext
->rsa
.p
));
368 if (mp_unsigned_bin_size(&pKeyContext
->rsa
.p
) < (dwKeyLen
+1)>>1)
369 memset(pbDest
+ mp_unsigned_bin_size(&pKeyContext
->rsa
.p
), 0,
370 ((dwKeyLen
+1)>>1) - mp_unsigned_bin_size(&pKeyContext
->rsa
.p
));
371 pbDest
+= (dwKeyLen
+1)>>1;
372 mp_to_unsigned_bin(&pKeyContext
->rsa
.q
, pbDest
);
373 reverse_bytes(pbDest
, mp_unsigned_bin_size(&pKeyContext
->rsa
.q
));
374 if (mp_unsigned_bin_size(&pKeyContext
->rsa
.q
) < (dwKeyLen
+1)>>1)
375 memset(pbDest
+ mp_unsigned_bin_size(&pKeyContext
->rsa
.q
), 0,
376 ((dwKeyLen
+1)>>1) - mp_unsigned_bin_size(&pKeyContext
->rsa
.q
));
377 pbDest
+= (dwKeyLen
+1)>>1;
378 mp_to_unsigned_bin(&pKeyContext
->rsa
.dP
, pbDest
);
379 reverse_bytes(pbDest
, mp_unsigned_bin_size(&pKeyContext
->rsa
.dP
));
380 if (mp_unsigned_bin_size(&pKeyContext
->rsa
.dP
) < (dwKeyLen
+1)>>1)
381 memset(pbDest
+ mp_unsigned_bin_size(&pKeyContext
->rsa
.dP
), 0,
382 ((dwKeyLen
+1)>>1) - mp_unsigned_bin_size(&pKeyContext
->rsa
.dP
));
383 pbDest
+= (dwKeyLen
+1)>>1;
384 mp_to_unsigned_bin(&pKeyContext
->rsa
.dQ
, pbDest
);
385 reverse_bytes(pbDest
, mp_unsigned_bin_size(&pKeyContext
->rsa
.dQ
));
386 if (mp_unsigned_bin_size(&pKeyContext
->rsa
.dQ
) < (dwKeyLen
+1)>>1)
387 memset(pbDest
+ mp_unsigned_bin_size(&pKeyContext
->rsa
.dQ
), 0,
388 ((dwKeyLen
+1)>>1) - mp_unsigned_bin_size(&pKeyContext
->rsa
.dQ
));
389 pbDest
+= (dwKeyLen
+1)>>1;
390 mp_to_unsigned_bin(&pKeyContext
->rsa
.qP
, pbDest
);
391 reverse_bytes(pbDest
, mp_unsigned_bin_size(&pKeyContext
->rsa
.qP
));
392 if (mp_unsigned_bin_size(&pKeyContext
->rsa
.qP
) < (dwKeyLen
+1)>>1)
393 memset(pbDest
+ mp_unsigned_bin_size(&pKeyContext
->rsa
.qP
), 0,
394 ((dwKeyLen
+1)>>1) - mp_unsigned_bin_size(&pKeyContext
->rsa
.qP
));
395 pbDest
+= (dwKeyLen
+1)>>1;
396 mp_to_unsigned_bin(&pKeyContext
->rsa
.d
, pbDest
);
397 reverse_bytes(pbDest
, mp_unsigned_bin_size(&pKeyContext
->rsa
.d
));
398 if (mp_unsigned_bin_size(&pKeyContext
->rsa
.d
) < dwKeyLen
)
399 memset(pbDest
+ mp_unsigned_bin_size(&pKeyContext
->rsa
.d
), 0,
400 dwKeyLen
- mp_unsigned_bin_size(&pKeyContext
->rsa
.d
));
401 *pdwPubExp
= (DWORD
)mp_get_int(&pKeyContext
->rsa
.e
);
406 BOOL
import_private_key_impl(const BYTE
*pbSrc
, KEY_CONTEXT
*pKeyContext
, DWORD dwKeyLen
,
407 DWORD dwDataLen
, DWORD dwPubExp
)
409 BYTE
*pbTemp
, *pbBigNum
;
411 if (mp_init_multi(&pKeyContext
->rsa
.e
, &pKeyContext
->rsa
.d
, &pKeyContext
->rsa
.N
,
412 &pKeyContext
->rsa
.dQ
,&pKeyContext
->rsa
.dP
,&pKeyContext
->rsa
.qP
,
413 &pKeyContext
->rsa
.p
, &pKeyContext
->rsa
.q
, NULL
) != MP_OKAY
)
415 SetLastError(NTE_FAIL
);
419 pbTemp
= HeapAlloc(GetProcessHeap(), 0, 2*dwKeyLen
+5*((dwKeyLen
+1)>>1));
420 if (!pbTemp
) return FALSE
;
421 memcpy(pbTemp
, pbSrc
, min(dwDataLen
, 2*dwKeyLen
+5*((dwKeyLen
+1)>>1)));
424 pKeyContext
->rsa
.type
= PK_PRIVATE
;
425 reverse_bytes(pbBigNum
, dwKeyLen
);
426 mp_read_unsigned_bin(&pKeyContext
->rsa
.N
, pbBigNum
, dwKeyLen
);
427 pbBigNum
+= dwKeyLen
;
428 reverse_bytes(pbBigNum
, (dwKeyLen
+1)>>1);
429 mp_read_unsigned_bin(&pKeyContext
->rsa
.p
, pbBigNum
, (dwKeyLen
+1)>>1);
430 pbBigNum
+= (dwKeyLen
+1)>>1;
431 reverse_bytes(pbBigNum
, (dwKeyLen
+1)>>1);
432 mp_read_unsigned_bin(&pKeyContext
->rsa
.q
, pbBigNum
, (dwKeyLen
+1)>>1);
433 pbBigNum
+= (dwKeyLen
+1)>>1;
434 reverse_bytes(pbBigNum
, (dwKeyLen
+1)>>1);
435 mp_read_unsigned_bin(&pKeyContext
->rsa
.dP
, pbBigNum
, (dwKeyLen
+1)>>1);
436 pbBigNum
+= (dwKeyLen
+1)>>1;
437 reverse_bytes(pbBigNum
, (dwKeyLen
+1)>>1);
438 mp_read_unsigned_bin(&pKeyContext
->rsa
.dQ
, pbBigNum
, (dwKeyLen
+1)>>1);
439 pbBigNum
+= (dwKeyLen
+1)>>1;
440 reverse_bytes(pbBigNum
, (dwKeyLen
+1)>>1);
441 mp_read_unsigned_bin(&pKeyContext
->rsa
.qP
, pbBigNum
, (dwKeyLen
+1)>>1);
442 pbBigNum
+= (dwKeyLen
+1)>>1;
443 /* The size of the private exponent d is inferred from the remaining
446 dwKeyLen
= min(dwKeyLen
, dwDataLen
- (pbBigNum
- pbTemp
));
447 reverse_bytes(pbBigNum
, dwKeyLen
);
448 mp_read_unsigned_bin(&pKeyContext
->rsa
.d
, pbBigNum
, dwKeyLen
);
449 mp_set_int(&pKeyContext
->rsa
.e
, dwPubExp
);
451 HeapFree(GetProcessHeap(), 0, pbTemp
);