template VOID algImpTestInteropGenerateKeyEntry()

in unittest/lib/testDl_msbignum.cpp [29:153]


template<> VOID algImpTestInteropGenerateKeyEntry< ImpMsBignum >(PBYTE pKeyEntry)
{
    PTEST_DL_KEYENTRY pKE = (PTEST_DL_KEYENTRY) pKeyEntry;

    BOOL success = FALSE;
    BYTE rgbDigest[SYMCRYPT_SHA512_RESULT_SIZE] = { 0 };        // Maximum digest size
    bigctx_t bignumCtx = { 0 };
    hash_function_context HashFunctionCtx = { 0 };
    dsa_other_info_tc other = { DSA_exponentiator_default };    // This will be used for both DSA and DH keys
    dsa_fullkey_t * pDsaFullKey = NULL;
    dsa_dwkey_t * pdwDsaKey = NULL;
    dh_fullkey_t * pDhFullKey = NULL;
    dh_dwkey_t * pdwDhKey = NULL;

    digit_t * pTemporary = NULL;

    DSAMethodEnum eFipsStandard = testDlMsBignum_ScFipsVersionToDsaMethodEnum(pKE->eFipsStandard);

    // Initialize the hash function context
    if (pKE->pHashAlgorithm != NULL)
    {
        testInteropScToHashContext(
            pKE->pHashAlgorithm,
            rgbDigest,
            &HashFunctionCtx);
    }
    else
    {
        testInteropScToHashContext(
            SymCryptSha1Algorithm,
            rgbDigest,
            &HashFunctionCtx);
    }

    // Allocate Dsa key
    pDsaFullKey = (dsa_fullkey_t *)SymCryptCallbackAlloc(sizeof(dsa_fullkey_t));
    pdwDsaKey = (dsa_dwkey_t *)SymCryptCallbackAlloc(sizeof(dsa_dwkey_t));
    CHECK((pDsaFullKey!=NULL) && (pdwDsaKey!=NULL), "?");

    // Group and DSA key
    success = DSA_key_generation_ex(
                    eFipsStandard,
                    &HashFunctionCtx,
                    pKE->nBitsOfP,
                    pKE->nBitsOfQ,
                    &other,
                    pDsaFullKey,
                    pdwDsaKey,
                    &bignumCtx);
    CHECK( success, "?" );

    pKE->pGroups[IMPMSBIGNUM_INDEX] = NULL;
    pKE->pKeysDsa[IMPMSBIGNUM_INDEX] = (PBYTE) pDsaFullKey;

    // First DH key
    // Allocate a new pdwkey and copy the original one
    pdwDhKey = (dh_dwkey_t *)SymCryptCallbackAlloc(sizeof(dh_dwkey_t));
    CHECK((pdwDhKey!=NULL), "?");

    CHECK(sizeof(dh_dwkey_t)==sizeof(dsa_dwkey_t), "?");
    memcpy( pdwDhKey, pDsaFullKey->pdwkey, sizeof(dh_dwkey_t));

    // Allocate a new full key
    pDhFullKey = (dh_fullkey_t *)SymCryptCallbackAlloc(sizeof(dh_fullkey_t));
    CHECK((pDhFullKey!=NULL), "?");

    // Create the first DH key (with the same public and private parts)
    success = DH_build_fullkey(
                    FIPS_186_3,     // Only this is supported for DH
                    &HashFunctionCtx,
                    pdwDhKey,
                    &other,
                    pDhFullKey,
                    FALSE,          // No verify for this as we might have used 186_2
                    &bignumCtx);
    CHECK( success, "?" );

    pKE->pKeysDhA[IMPMSBIGNUM_INDEX] = (PBYTE) pDhFullKey;

    // Second DH key
    // Allocate a new pdwkey and copy the original one
    pdwDhKey = (dh_dwkey_t *)SymCryptCallbackAlloc(sizeof(dh_dwkey_t));
    CHECK((pdwDhKey!=NULL), "?");

    CHECK(sizeof(dh_dwkey_t)==sizeof(dsa_dwkey_t), "?");
    memcpy( pdwDhKey, pDsaFullKey->pdwkey, sizeof(dh_dwkey_t));

    // Erase private and public keys and S,C
    SymCryptWipe((PBYTE)&pdwDhKey->x[0], (2*DSA_P_MAXDWORDS+DSA_Q_MAXDWORDS+1)*sizeof(DWORD));

    // Allocate a new full key
    pDhFullKey = (dh_fullkey_t *)SymCryptCallbackAlloc(sizeof(dh_fullkey_t));
    CHECK((pDhFullKey!=NULL), "?");

    // Create the second DH key
    success = DH_build_fullkey(
                    FIPS_186_3,     // Only this is supported for DH
                    &HashFunctionCtx,
                    pdwDhKey,
                    &other,
                    pDhFullKey,
                    FALSE,          // No verify for this as we might have used 186_2
                    &bignumCtx);
    CHECK( success, "?" );

    // Allocate temp memory
    pTemporary = (digit_t *)SymCryptCallbackAlloc(3*DH_P_MAXDIGITS*sizeof(digit_t));
    CHECK( pTemporary!=NULL, "?" );

    // Create new private and public keys
    success = DH_gen_x_and_y(
                    TRUE,           // Use Q
                    &other,
                    pDhFullKey,
                    pdwDhKey,
                    pTemporary,
                    &bignumCtx);
    CHECK( success, "?" );

    pKE->pKeysDhB[IMPMSBIGNUM_INDEX] = (PBYTE) pDhFullKey;

    SymCryptWipe((PBYTE)pTemporary, 3*DH_P_MAXDIGITS*sizeof(digit_t));
    SymCryptCallbackFree((PBYTE)pTemporary);

}