in unittest/lib/testDl_cng.cpp [176:282]
template<> VOID algImpTestInteropGenerateKeyEntry< ImpCng >(PBYTE pKeyEntry)
{
PTEST_DL_KEYENTRY pKE = (PTEST_DL_KEYENTRY) pKeyEntry;
NTSTATUS ntStatus = STATUS_SUCCESS;
BCRYPT_ALG_HANDLE hAlg = NULL;
BCRYPT_KEY_HANDLE hKey = NULL;
BYTE rbDsaKeyBlob[ TEST_DL_MAX_SIZEOF_DSA_BLOB ] = { 0 };
SIZE_T cbDsaKeyBlob = 0;
BYTE rbDhKeyBlob[ TEST_DL_MAX_SIZEOF_DH_BLOB ] = { 0 };
SIZE_T cbDhKeyBlob = 0;
BYTE rbDhParams[ TEST_DL_MAX_SIZEOF_DH_PARAMS ] = { 0 };
SIZE_T cbDhParams = 0;
// DSA key
ntStatus = BCryptOpenAlgorithmProvider(
&hAlg,
BCRYPT_DSA_ALGORITHM,
MS_PRIMITIVE_PROVIDER,
0 );
CHECK( ntStatus == STATUS_SUCCESS, "?" );
ntStatus = BCryptGenerateKeyPair(
hAlg,
&hKey,
pKE->nBitsOfP,
0 );
CHECK( ntStatus == STATUS_SUCCESS, "?" );
ntStatus = BCryptFinalizeKeyPair(
hKey,
0 );
CHECK( ntStatus == STATUS_SUCCESS, "?" );
ntStatus = BCryptCloseAlgorithmProvider( hAlg, 0 );
CHECK( ntStatus == STATUS_SUCCESS, "?" );
pKE->pGroups[IMPCNG_INDEX] = NULL;
pKE->pKeysDsa[IMPCNG_INDEX] = (PBYTE) hKey;
// Export the key we just created
ntStatus = BCryptExportKey(
hKey,
NULL, // Export key
BCRYPT_DSA_PRIVATE_BLOB,
(PUCHAR) rbDsaKeyBlob,
sizeof( rbDsaKeyBlob ),
(ULONG*) &cbDsaKeyBlob,
0 );
CHECK3( ntStatus == STATUS_SUCCESS, "BCryptExportKey failed with 0x%x", ntStatus );
// Convert it to DH blob
testDlCng_DsaToDhBlob( pKE->nBitsOfP, rbDsaKeyBlob, rbDhKeyBlob, &cbDhKeyBlob );
// DH key A
ntStatus = BCryptOpenAlgorithmProvider(
&hAlg,
BCRYPT_DH_ALGORITHM,
MS_PRIMITIVE_PROVIDER,
0 );
CHECK( ntStatus == STATUS_SUCCESS, "?" );
ntStatus = BCryptImportKeyPair(
hAlg,
NULL,
BCRYPT_DH_PRIVATE_BLOB,
&hKey,
rbDhKeyBlob,
(ULONG) cbDhKeyBlob,
BCRYPT_NO_KEY_VALIDATION );
CHECK3( ntStatus == STATUS_SUCCESS, "BCryptImportKeyPair failed with 0x%x", ntStatus );
pKE->pKeysDhA[IMPCNG_INDEX] = (PBYTE) hKey;
// DH key B
ntStatus = BCryptGenerateKeyPair(
hAlg,
&hKey,
pKE->nBitsOfP,
0 );
CHECK( ntStatus == STATUS_SUCCESS, "?" );
// Get the parameters
testDlCng_DsaToDhParams( pKE->nBitsOfP, rbDsaKeyBlob, rbDhParams, &cbDhParams );
// Set the property
ntStatus = BCryptSetProperty(
hKey,
BCRYPT_DH_PARAMETERS,
rbDhParams,
(ULONG) cbDhParams,
0);
CHECK( ntStatus == STATUS_SUCCESS, "?" );
ntStatus = BCryptFinalizeKeyPair(
hKey,
0 );
CHECK( ntStatus == STATUS_SUCCESS, "?" );
pKE->pKeysDhB[IMPCNG_INDEX] = (PBYTE) hKey;
ntStatus = BCryptCloseAlgorithmProvider( hAlg, 0 );
CHECK( ntStatus == STATUS_SUCCESS, "?" );
}