2004-11-30 18:43:59 +01:00
|
|
|
/*
|
|
|
|
* dlls/rsaenh/implglue.c
|
|
|
|
* Glueing the RSAENH specific code to the crypto library
|
|
|
|
*
|
2005-01-24 12:30:05 +01:00
|
|
|
* Copyright (c) 2004, 2005 Michael Jung
|
2007-11-20 23:58:23 +01:00
|
|
|
* Copyright (c) 2007 Vijay Kiran Kamuju
|
2004-11-30 18:43:59 +01:00
|
|
|
*
|
|
|
|
* based on code by Mike McCormack and David Hammerton
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2004-11-30 18:43:59 +01:00
|
|
|
*/
|
|
|
|
|
2019-06-19 10:32:27 +02:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
2004-11-30 18:43:59 +01:00
|
|
|
#include "windef.h"
|
2019-06-19 10:32:27 +02:00
|
|
|
#include "winbase.h"
|
2004-11-30 18:43:59 +01:00
|
|
|
#include "wincrypt.h"
|
|
|
|
|
|
|
|
#include "implglue.h"
|
|
|
|
|
2005-05-27 21:22:22 +02:00
|
|
|
/* Function prototype copied from dlls/advapi32/crypt.c */
|
|
|
|
BOOL WINAPI SystemFunction036(PVOID pbBuffer, ULONG dwLen);
|
2004-11-30 18:43:59 +01:00
|
|
|
|
|
|
|
BOOL init_hash_impl(ALG_ID aiAlgid, HASH_CONTEXT *pHashContext)
|
|
|
|
{
|
2017-07-21 15:36:31 +02:00
|
|
|
BCRYPT_ALG_HANDLE provider;
|
|
|
|
NTSTATUS status;
|
2017-06-28 18:22:12 +02:00
|
|
|
|
2004-11-30 18:43:59 +01:00
|
|
|
switch (aiAlgid)
|
|
|
|
{
|
|
|
|
case CALG_MD2:
|
2017-12-07 19:46:33 +01:00
|
|
|
status = BCryptOpenAlgorithmProvider(&provider, BCRYPT_MD2_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
|
|
|
|
break;
|
2004-11-30 18:43:59 +01:00
|
|
|
|
|
|
|
case CALG_MD4:
|
2017-12-07 19:47:06 +01:00
|
|
|
status = BCryptOpenAlgorithmProvider(&provider, BCRYPT_MD4_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
|
|
|
|
break;
|
2004-11-30 18:43:59 +01:00
|
|
|
|
|
|
|
case CALG_MD5:
|
2017-12-07 19:47:13 +01:00
|
|
|
status = BCryptOpenAlgorithmProvider(&provider, BCRYPT_MD5_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
|
|
|
|
break;
|
2004-11-30 18:43:59 +01:00
|
|
|
|
|
|
|
case CALG_SHA:
|
2017-12-07 19:46:41 +01:00
|
|
|
status = BCryptOpenAlgorithmProvider(&provider, BCRYPT_SHA1_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
|
|
|
|
break;
|
2010-03-15 20:04:29 +01:00
|
|
|
|
|
|
|
case CALG_SHA_256:
|
2017-07-21 15:36:31 +02:00
|
|
|
status = BCryptOpenAlgorithmProvider(&provider, BCRYPT_SHA256_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
|
2010-03-15 20:04:29 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CALG_SHA_384:
|
2017-07-21 15:36:31 +02:00
|
|
|
status = BCryptOpenAlgorithmProvider(&provider, BCRYPT_SHA384_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
|
2010-03-15 20:04:29 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CALG_SHA_512:
|
2017-07-21 15:36:31 +02:00
|
|
|
status = BCryptOpenAlgorithmProvider(&provider, BCRYPT_SHA512_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
|
2010-03-15 20:04:29 +01:00
|
|
|
break;
|
2004-11-30 18:43:59 +01:00
|
|
|
|
2017-07-21 15:36:31 +02:00
|
|
|
default:
|
|
|
|
return TRUE;
|
|
|
|
}
|
2017-06-28 18:22:12 +02:00
|
|
|
|
2017-07-21 15:36:31 +02:00
|
|
|
if (status) return FALSE;
|
2017-06-28 18:22:12 +02:00
|
|
|
|
2017-07-21 15:36:31 +02:00
|
|
|
status = BCryptCreateHash(provider, &pHashContext->bcrypt_hash, NULL, 0, NULL, 0, 0);
|
|
|
|
BCryptCloseAlgorithmProvider(provider, 0);
|
|
|
|
return !status;
|
2004-11-30 18:43:59 +01:00
|
|
|
}
|
|
|
|
|
2017-12-07 19:47:21 +01:00
|
|
|
BOOL update_hash_impl(HASH_CONTEXT *pHashContext, const BYTE *pbData, DWORD dwDataLen)
|
2004-11-30 18:43:59 +01:00
|
|
|
{
|
2017-12-07 19:47:13 +01:00
|
|
|
BCryptHashData(pHashContext->bcrypt_hash, (UCHAR*)pbData, dwDataLen, 0);
|
2004-11-30 18:43:59 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2017-12-07 19:47:21 +01:00
|
|
|
BOOL finalize_hash_impl(HASH_CONTEXT *pHashContext, BYTE *pbHashValue)
|
2004-11-30 18:43:59 +01:00
|
|
|
{
|
2017-12-07 19:47:13 +01:00
|
|
|
BCryptFinishHash(pHashContext->bcrypt_hash, pbHashValue, RSAENH_MAX_HASH_SIZE, 0);
|
|
|
|
BCryptDestroyHash(pHashContext->bcrypt_hash);
|
2004-11-30 18:43:59 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2017-12-07 19:47:21 +01:00
|
|
|
BOOL duplicate_hash_impl(const HASH_CONTEXT *pSrcHashContext, HASH_CONTEXT *pDestHashContext)
|
2004-11-30 18:43:59 +01:00
|
|
|
{
|
2017-12-07 19:47:13 +01:00
|
|
|
return !BCryptDuplicateHash(pSrcHashContext->bcrypt_hash, &pDestHashContext->bcrypt_hash, NULL, 0, 0);
|
2004-11-30 18:43:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL new_key_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen)
|
|
|
|
{
|
|
|
|
switch (aiAlgid)
|
|
|
|
{
|
|
|
|
case CALG_RSA_KEYX:
|
|
|
|
case CALG_RSA_SIGN:
|
2004-12-01 16:34:56 +01:00
|
|
|
if (rsa_make_key((int)dwKeyLen, 65537, &pKeyContext->rsa) != CRYPT_OK) {
|
|
|
|
SetLastError(NTE_FAIL);
|
2004-11-30 18:43:59 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2004-12-01 16:34:56 +01:00
|
|
|
return TRUE;
|
2004-11-30 18:43:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL free_key_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext)
|
|
|
|
{
|
|
|
|
switch (aiAlgid)
|
|
|
|
{
|
|
|
|
case CALG_RSA_KEYX:
|
|
|
|
case CALG_RSA_SIGN:
|
|
|
|
rsa_free(&pKeyContext->rsa);
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2007-09-11 00:05:24 +02:00
|
|
|
BOOL setup_key_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
|
|
|
|
DWORD dwEffectiveKeyLen, DWORD dwSaltLen, BYTE *abKeyValue)
|
2004-11-30 18:43:59 +01:00
|
|
|
{
|
|
|
|
switch (aiAlgid)
|
|
|
|
{
|
|
|
|
case CALG_RC4:
|
|
|
|
rc4_start(&pKeyContext->rc4);
|
|
|
|
rc4_add_entropy(abKeyValue, dwKeyLen + dwSaltLen, &pKeyContext->rc4);
|
|
|
|
rc4_ready(&pKeyContext->rc4);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CALG_RC2:
|
2007-09-11 00:05:24 +02:00
|
|
|
rc2_setup(abKeyValue, dwKeyLen + dwSaltLen, dwEffectiveKeyLen ?
|
|
|
|
dwEffectiveKeyLen : dwKeyLen << 3, 0, &pKeyContext->rc2);
|
2004-11-30 18:43:59 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CALG_3DES:
|
|
|
|
des3_setup(abKeyValue, 24, 0, &pKeyContext->des3);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CALG_3DES_112:
|
|
|
|
memcpy(abKeyValue+16, abKeyValue, 8);
|
|
|
|
des3_setup(abKeyValue, 24, 0, &pKeyContext->des3);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CALG_DES:
|
|
|
|
des_setup(abKeyValue, 8, 0, &pKeyContext->des);
|
|
|
|
break;
|
2007-11-20 23:58:23 +01:00
|
|
|
|
|
|
|
case CALG_AES:
|
|
|
|
case CALG_AES_128:
|
|
|
|
aes_setup(abKeyValue, 16, 0, &pKeyContext->aes);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CALG_AES_192:
|
|
|
|
aes_setup(abKeyValue, 24, 0, &pKeyContext->aes);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CALG_AES_256:
|
|
|
|
aes_setup(abKeyValue, 32, 0, &pKeyContext->aes);
|
|
|
|
break;
|
2004-11-30 18:43:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-09-23 00:29:19 +02:00
|
|
|
BOOL duplicate_key_impl(ALG_ID aiAlgid, const KEY_CONTEXT *pSrcKeyContext,
|
2004-11-30 18:43:59 +01:00
|
|
|
KEY_CONTEXT *pDestKeyContext)
|
|
|
|
{
|
|
|
|
switch (aiAlgid)
|
|
|
|
{
|
|
|
|
case CALG_RC4:
|
|
|
|
case CALG_RC2:
|
|
|
|
case CALG_3DES:
|
|
|
|
case CALG_3DES_112:
|
|
|
|
case CALG_DES:
|
2007-11-20 23:58:23 +01:00
|
|
|
case CALG_AES:
|
|
|
|
case CALG_AES_128:
|
|
|
|
case CALG_AES_192:
|
|
|
|
case CALG_AES_256:
|
2008-03-16 00:25:38 +01:00
|
|
|
*pDestKeyContext = *pSrcKeyContext;
|
2004-11-30 18:43:59 +01:00
|
|
|
break;
|
|
|
|
case CALG_RSA_KEYX:
|
|
|
|
case CALG_RSA_SIGN:
|
|
|
|
pDestKeyContext->rsa.type = pSrcKeyContext->rsa.type;
|
|
|
|
mp_init_copy(&pDestKeyContext->rsa.e, &pSrcKeyContext->rsa.e);
|
|
|
|
mp_init_copy(&pDestKeyContext->rsa.d, &pSrcKeyContext->rsa.d);
|
|
|
|
mp_init_copy(&pDestKeyContext->rsa.N, &pSrcKeyContext->rsa.N);
|
|
|
|
mp_init_copy(&pDestKeyContext->rsa.p, &pSrcKeyContext->rsa.p);
|
|
|
|
mp_init_copy(&pDestKeyContext->rsa.q, &pSrcKeyContext->rsa.q);
|
|
|
|
mp_init_copy(&pDestKeyContext->rsa.qP, &pSrcKeyContext->rsa.qP);
|
|
|
|
mp_init_copy(&pDestKeyContext->rsa.dP, &pSrcKeyContext->rsa.dP);
|
|
|
|
mp_init_copy(&pDestKeyContext->rsa.dQ, &pSrcKeyContext->rsa.dQ);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
SetLastError(NTE_BAD_ALGID);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void reverse_bytes(BYTE *pbData, DWORD dwLen) {
|
|
|
|
BYTE swap;
|
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
for (i=0; i<dwLen/2; i++) {
|
|
|
|
swap = pbData[i];
|
|
|
|
pbData[i] = pbData[dwLen-i-1];
|
|
|
|
pbData[dwLen-i-1] = swap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-23 00:29:19 +02:00
|
|
|
BOOL encrypt_block_impl(ALG_ID aiAlgid, DWORD dwKeySpec, KEY_CONTEXT *pKeyContext, const BYTE *in,
|
|
|
|
BYTE *out, DWORD enc)
|
2004-11-30 18:43:59 +01:00
|
|
|
{
|
|
|
|
unsigned long inlen, outlen;
|
2005-01-03 21:14:03 +01:00
|
|
|
BYTE *in_reversed = NULL;
|
2004-11-30 18:43:59 +01:00
|
|
|
|
|
|
|
switch (aiAlgid) {
|
|
|
|
case CALG_RC2:
|
|
|
|
if (enc) {
|
|
|
|
rc2_ecb_encrypt(in, out, &pKeyContext->rc2);
|
|
|
|
} else {
|
|
|
|
rc2_ecb_decrypt(in, out, &pKeyContext->rc2);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CALG_3DES:
|
|
|
|
case CALG_3DES_112:
|
|
|
|
if (enc) {
|
|
|
|
des3_ecb_encrypt(in, out, &pKeyContext->des3);
|
|
|
|
} else {
|
|
|
|
des3_ecb_decrypt(in, out, &pKeyContext->des3);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CALG_DES:
|
|
|
|
if (enc) {
|
|
|
|
des_ecb_encrypt(in, out, &pKeyContext->des);
|
|
|
|
} else {
|
|
|
|
des_ecb_decrypt(in, out, &pKeyContext->des);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2007-11-20 23:58:23 +01:00
|
|
|
case CALG_AES:
|
|
|
|
case CALG_AES_128:
|
|
|
|
case CALG_AES_192:
|
|
|
|
case CALG_AES_256:
|
|
|
|
if (enc) {
|
|
|
|
aes_ecb_encrypt(in, out, &pKeyContext->aes);
|
|
|
|
} else {
|
|
|
|
aes_ecb_decrypt(in, out, &pKeyContext->aes);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2004-11-30 18:43:59 +01:00
|
|
|
case CALG_RSA_KEYX:
|
2005-01-03 21:14:03 +01:00
|
|
|
case CALG_RSA_SIGN:
|
2010-05-21 02:23:25 +02:00
|
|
|
case CALG_SSL3_SHAMD5:
|
2004-11-30 18:43:59 +01:00
|
|
|
outlen = inlen = (mp_count_bits(&pKeyContext->rsa.N)+7)/8;
|
|
|
|
if (enc) {
|
2006-09-28 19:50:50 +02:00
|
|
|
if (rsa_exptmod(in, inlen, out, &outlen, dwKeySpec, &pKeyContext->rsa) != CRYPT_OK) {
|
2004-12-01 16:34:56 +01:00
|
|
|
SetLastError(NTE_FAIL);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2005-01-03 21:14:03 +01:00
|
|
|
reverse_bytes(out, outlen);
|
2004-11-30 18:43:59 +01:00
|
|
|
} else {
|
2005-01-03 21:14:03 +01:00
|
|
|
in_reversed = HeapAlloc(GetProcessHeap(), 0, inlen);
|
|
|
|
if (!in_reversed) {
|
|
|
|
SetLastError(NTE_NO_MEMORY);
|
2004-12-13 14:24:48 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2005-01-03 21:14:03 +01:00
|
|
|
memcpy(in_reversed, in, inlen);
|
|
|
|
reverse_bytes(in_reversed, inlen);
|
2006-09-28 19:50:50 +02:00
|
|
|
if (rsa_exptmod(in_reversed, inlen, out, &outlen, dwKeySpec, &pKeyContext->rsa) != CRYPT_OK) {
|
2005-01-03 21:14:03 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, in_reversed);
|
2004-12-13 14:24:48 +01:00
|
|
|
SetLastError(NTE_FAIL);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2005-01-03 21:14:03 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, in_reversed);
|
2004-11-30 18:43:59 +01:00
|
|
|
}
|
2004-12-06 12:37:43 +01:00
|
|
|
break;
|
|
|
|
|
2004-11-30 18:43:59 +01:00
|
|
|
default:
|
|
|
|
SetLastError(NTE_BAD_ALGID);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL encrypt_stream_impl(ALG_ID aiAlgid, KEY_CONTEXT *pKeyContext, BYTE *stream, DWORD dwLen)
|
|
|
|
{
|
|
|
|
switch (aiAlgid) {
|
|
|
|
case CALG_RC4:
|
|
|
|
rc4_read(stream, dwLen, &pKeyContext->rc4);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
SetLastError(NTE_BAD_ALGID);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL gen_rand_impl(BYTE *pbBuffer, DWORD dwLen)
|
|
|
|
{
|
2005-05-27 21:22:22 +02:00
|
|
|
return SystemFunction036(pbBuffer, dwLen);
|
2004-11-30 18:43:59 +01:00
|
|
|
}
|
|
|
|
|
2007-08-23 22:32:19 +02:00
|
|
|
BOOL export_public_key_impl(BYTE *pbDest, const KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,DWORD *pdwPubExp)
|
2004-11-30 18:43:59 +01:00
|
|
|
{
|
|
|
|
mp_to_unsigned_bin(&pKeyContext->rsa.N, pbDest);
|
2009-11-05 19:02:57 +01:00
|
|
|
reverse_bytes(pbDest, mp_unsigned_bin_size(&pKeyContext->rsa.N));
|
2009-11-05 18:47:49 +01:00
|
|
|
if (mp_unsigned_bin_size(&pKeyContext->rsa.N) < dwKeyLen)
|
|
|
|
memset(pbDest + mp_unsigned_bin_size(&pKeyContext->rsa.N), 0,
|
|
|
|
dwKeyLen - mp_unsigned_bin_size(&pKeyContext->rsa.N));
|
2004-11-30 18:43:59 +01:00
|
|
|
*pdwPubExp = (DWORD)mp_get_int(&pKeyContext->rsa.e);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-09-23 00:29:19 +02:00
|
|
|
BOOL import_public_key_impl(const BYTE *pbSrc, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
|
2004-11-30 18:43:59 +01:00
|
|
|
DWORD dwPubExp)
|
|
|
|
{
|
|
|
|
BYTE *pbTemp;
|
|
|
|
|
2004-12-01 16:34:56 +01:00
|
|
|
if (mp_init_multi(&pKeyContext->rsa.e, &pKeyContext->rsa.d, &pKeyContext->rsa.N,
|
|
|
|
&pKeyContext->rsa.dQ,&pKeyContext->rsa.dP,&pKeyContext->rsa.qP,
|
|
|
|
&pKeyContext->rsa.p, &pKeyContext->rsa.q, NULL) != MP_OKAY)
|
|
|
|
{
|
|
|
|
SetLastError(NTE_FAIL);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2005-03-24 22:01:35 +01:00
|
|
|
pbTemp = HeapAlloc(GetProcessHeap(), 0, dwKeyLen);
|
2004-11-30 18:43:59 +01:00
|
|
|
if (!pbTemp) return FALSE;
|
|
|
|
memcpy(pbTemp, pbSrc, dwKeyLen);
|
|
|
|
|
|
|
|
pKeyContext->rsa.type = PK_PUBLIC;
|
|
|
|
reverse_bytes(pbTemp, dwKeyLen);
|
|
|
|
mp_read_unsigned_bin(&pKeyContext->rsa.N, pbTemp, dwKeyLen);
|
|
|
|
HeapFree(GetProcessHeap(), 0, pbTemp);
|
|
|
|
mp_set_int(&pKeyContext->rsa.e, dwPubExp);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2007-08-23 22:32:19 +02:00
|
|
|
BOOL export_private_key_impl(BYTE *pbDest, const KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
|
2004-11-30 18:43:59 +01:00
|
|
|
DWORD *pdwPubExp)
|
|
|
|
{
|
|
|
|
mp_to_unsigned_bin(&pKeyContext->rsa.N, pbDest);
|
2009-11-05 19:02:03 +01:00
|
|
|
reverse_bytes(pbDest, mp_unsigned_bin_size(&pKeyContext->rsa.N));
|
2009-11-05 18:49:00 +01:00
|
|
|
if (mp_unsigned_bin_size(&pKeyContext->rsa.N) < dwKeyLen)
|
|
|
|
memset(pbDest + mp_unsigned_bin_size(&pKeyContext->rsa.N), 0,
|
|
|
|
dwKeyLen - mp_unsigned_bin_size(&pKeyContext->rsa.N));
|
2004-11-30 18:43:59 +01:00
|
|
|
pbDest += dwKeyLen;
|
|
|
|
mp_to_unsigned_bin(&pKeyContext->rsa.p, pbDest);
|
2009-11-05 19:02:03 +01:00
|
|
|
reverse_bytes(pbDest, mp_unsigned_bin_size(&pKeyContext->rsa.p));
|
2009-11-05 18:49:00 +01:00
|
|
|
if (mp_unsigned_bin_size(&pKeyContext->rsa.p) < (dwKeyLen+1)>>1)
|
|
|
|
memset(pbDest + mp_unsigned_bin_size(&pKeyContext->rsa.p), 0,
|
|
|
|
((dwKeyLen+1)>>1) - mp_unsigned_bin_size(&pKeyContext->rsa.p));
|
2004-11-30 18:43:59 +01:00
|
|
|
pbDest += (dwKeyLen+1)>>1;
|
|
|
|
mp_to_unsigned_bin(&pKeyContext->rsa.q, pbDest);
|
2009-11-05 19:02:03 +01:00
|
|
|
reverse_bytes(pbDest, mp_unsigned_bin_size(&pKeyContext->rsa.q));
|
2009-11-05 18:49:00 +01:00
|
|
|
if (mp_unsigned_bin_size(&pKeyContext->rsa.q) < (dwKeyLen+1)>>1)
|
|
|
|
memset(pbDest + mp_unsigned_bin_size(&pKeyContext->rsa.q), 0,
|
|
|
|
((dwKeyLen+1)>>1) - mp_unsigned_bin_size(&pKeyContext->rsa.q));
|
2004-11-30 18:43:59 +01:00
|
|
|
pbDest += (dwKeyLen+1)>>1;
|
|
|
|
mp_to_unsigned_bin(&pKeyContext->rsa.dP, pbDest);
|
2009-11-05 19:02:03 +01:00
|
|
|
reverse_bytes(pbDest, mp_unsigned_bin_size(&pKeyContext->rsa.dP));
|
2009-11-05 18:49:00 +01:00
|
|
|
if (mp_unsigned_bin_size(&pKeyContext->rsa.dP) < (dwKeyLen+1)>>1)
|
|
|
|
memset(pbDest + mp_unsigned_bin_size(&pKeyContext->rsa.dP), 0,
|
|
|
|
((dwKeyLen+1)>>1) - mp_unsigned_bin_size(&pKeyContext->rsa.dP));
|
2004-11-30 18:43:59 +01:00
|
|
|
pbDest += (dwKeyLen+1)>>1;
|
|
|
|
mp_to_unsigned_bin(&pKeyContext->rsa.dQ, pbDest);
|
2009-11-05 19:02:03 +01:00
|
|
|
reverse_bytes(pbDest, mp_unsigned_bin_size(&pKeyContext->rsa.dQ));
|
2009-11-05 18:49:00 +01:00
|
|
|
if (mp_unsigned_bin_size(&pKeyContext->rsa.dQ) < (dwKeyLen+1)>>1)
|
|
|
|
memset(pbDest + mp_unsigned_bin_size(&pKeyContext->rsa.dQ), 0,
|
|
|
|
((dwKeyLen+1)>>1) - mp_unsigned_bin_size(&pKeyContext->rsa.dQ));
|
2004-11-30 18:43:59 +01:00
|
|
|
pbDest += (dwKeyLen+1)>>1;
|
|
|
|
mp_to_unsigned_bin(&pKeyContext->rsa.qP, pbDest);
|
2009-11-05 19:02:03 +01:00
|
|
|
reverse_bytes(pbDest, mp_unsigned_bin_size(&pKeyContext->rsa.qP));
|
2009-11-05 18:49:00 +01:00
|
|
|
if (mp_unsigned_bin_size(&pKeyContext->rsa.qP) < (dwKeyLen+1)>>1)
|
|
|
|
memset(pbDest + mp_unsigned_bin_size(&pKeyContext->rsa.qP), 0,
|
|
|
|
((dwKeyLen+1)>>1) - mp_unsigned_bin_size(&pKeyContext->rsa.qP));
|
2004-11-30 18:43:59 +01:00
|
|
|
pbDest += (dwKeyLen+1)>>1;
|
|
|
|
mp_to_unsigned_bin(&pKeyContext->rsa.d, pbDest);
|
2009-11-05 19:02:03 +01:00
|
|
|
reverse_bytes(pbDest, mp_unsigned_bin_size(&pKeyContext->rsa.d));
|
2009-11-05 18:49:00 +01:00
|
|
|
if (mp_unsigned_bin_size(&pKeyContext->rsa.d) < dwKeyLen)
|
|
|
|
memset(pbDest + mp_unsigned_bin_size(&pKeyContext->rsa.d), 0,
|
|
|
|
dwKeyLen - mp_unsigned_bin_size(&pKeyContext->rsa.d));
|
2004-11-30 18:43:59 +01:00
|
|
|
*pdwPubExp = (DWORD)mp_get_int(&pKeyContext->rsa.e);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-09-23 00:29:19 +02:00
|
|
|
BOOL import_private_key_impl(const BYTE *pbSrc, KEY_CONTEXT *pKeyContext, DWORD dwKeyLen,
|
2011-10-31 18:44:23 +01:00
|
|
|
DWORD dwDataLen, DWORD dwPubExp)
|
2004-11-30 18:43:59 +01:00
|
|
|
{
|
|
|
|
BYTE *pbTemp, *pbBigNum;
|
|
|
|
|
2004-12-01 16:34:56 +01:00
|
|
|
if (mp_init_multi(&pKeyContext->rsa.e, &pKeyContext->rsa.d, &pKeyContext->rsa.N,
|
|
|
|
&pKeyContext->rsa.dQ,&pKeyContext->rsa.dP,&pKeyContext->rsa.qP,
|
|
|
|
&pKeyContext->rsa.p, &pKeyContext->rsa.q, NULL) != MP_OKAY)
|
|
|
|
{
|
|
|
|
SetLastError(NTE_FAIL);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2004-11-30 18:43:59 +01:00
|
|
|
pbTemp = HeapAlloc(GetProcessHeap(), 0, 2*dwKeyLen+5*((dwKeyLen+1)>>1));
|
|
|
|
if (!pbTemp) return FALSE;
|
2011-10-31 18:44:23 +01:00
|
|
|
memcpy(pbTemp, pbSrc, min(dwDataLen, 2*dwKeyLen+5*((dwKeyLen+1)>>1)));
|
2004-11-30 18:43:59 +01:00
|
|
|
pbBigNum = pbTemp;
|
|
|
|
|
|
|
|
pKeyContext->rsa.type = PK_PRIVATE;
|
|
|
|
reverse_bytes(pbBigNum, dwKeyLen);
|
|
|
|
mp_read_unsigned_bin(&pKeyContext->rsa.N, pbBigNum, dwKeyLen);
|
|
|
|
pbBigNum += dwKeyLen;
|
|
|
|
reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
|
|
|
|
mp_read_unsigned_bin(&pKeyContext->rsa.p, pbBigNum, (dwKeyLen+1)>>1);
|
|
|
|
pbBigNum += (dwKeyLen+1)>>1;
|
|
|
|
reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
|
|
|
|
mp_read_unsigned_bin(&pKeyContext->rsa.q, pbBigNum, (dwKeyLen+1)>>1);
|
|
|
|
pbBigNum += (dwKeyLen+1)>>1;
|
|
|
|
reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
|
|
|
|
mp_read_unsigned_bin(&pKeyContext->rsa.dP, pbBigNum, (dwKeyLen+1)>>1);
|
|
|
|
pbBigNum += (dwKeyLen+1)>>1;
|
|
|
|
reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
|
|
|
|
mp_read_unsigned_bin(&pKeyContext->rsa.dQ, pbBigNum, (dwKeyLen+1)>>1);
|
|
|
|
pbBigNum += (dwKeyLen+1)>>1;
|
|
|
|
reverse_bytes(pbBigNum, (dwKeyLen+1)>>1);
|
|
|
|
mp_read_unsigned_bin(&pKeyContext->rsa.qP, pbBigNum, (dwKeyLen+1)>>1);
|
|
|
|
pbBigNum += (dwKeyLen+1)>>1;
|
2011-10-31 18:44:23 +01:00
|
|
|
/* The size of the private exponent d is inferred from the remaining
|
|
|
|
* data length.
|
|
|
|
*/
|
|
|
|
dwKeyLen = min(dwKeyLen, dwDataLen - (pbBigNum - pbTemp));
|
2004-11-30 18:43:59 +01:00
|
|
|
reverse_bytes(pbBigNum, dwKeyLen);
|
|
|
|
mp_read_unsigned_bin(&pKeyContext->rsa.d, pbBigNum, dwKeyLen);
|
|
|
|
mp_set_int(&pKeyContext->rsa.e, dwPubExp);
|
|
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, pbTemp);
|
|
|
|
return TRUE;
|
|
|
|
}
|