template VOID algImpTestInteropImportKeyEntryBuffers()

in unittest/lib/testDl_msbignum.cpp [195:328]


template<> VOID algImpTestInteropImportKeyEntryBuffers< 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;

    DSAMethodEnum eFipsStandard = testDlMsBignum_ScFipsVersionToDsaMethodEnum(pKE->eFipsStandard);

    CHECK(pKE->pGroups[IMPMSBIGNUM_INDEX]==NULL, "?");
    CHECK(pKE->pKeysDsa[IMPMSBIGNUM_INDEX]==NULL, "?");
    CHECK(pKE->pKeysDhA[IMPMSBIGNUM_INDEX]==NULL, "?");
    CHECK(pKE->pKeysDhB[IMPMSBIGNUM_INDEX]==NULL, "?");

    // 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), "?");

    // Wipe the new pdwkey
    SymCryptWipe((PBYTE)pdwDsaKey, sizeof(dsa_dwkey_t));

    // Set what we know
    pdwDsaKey->nbitp = pKE->nBitsOfP;
    pdwDsaKey->nbitq = pKE->nBitsOfQ;
    pdwDsaKey->nbitx = pdwDsaKey->nbitq;      // We have Q sized private keys for the first keys

    // Build the pdwkey by copying buffers
    testInteropReverseMemCopy( (PBYTE)pdwDsaKey->p, pKE->rbPrimeP, pKE->cbPrimeP );
    testInteropReverseMemCopy( (PBYTE)pdwDsaKey->q, pKE->rbPrimeQ, pKE->cbPrimeQ );
    testInteropReverseMemCopy( (PBYTE)pdwDsaKey->g, pKE->rbGenG, pKE->cbPrimeP );

    // Seed and gen counter
    testInteropReverseMemCopy( (PBYTE)pdwDsaKey->S, pKE->rbSeed, pKE->cbPrimeQ );
    pdwDsaKey->C = pKE->dwGenCounter;

    testInteropReverseMemCopy( (PBYTE)pdwDsaKey->x, pKE->rbPrivateKeyA, pKE->cbPrimeQ );
    testInteropReverseMemCopy( (PBYTE)pdwDsaKey->y, pKE->rbPublicKeyA, pKE->cbPrimeP );

    // Create the DSA key
    success = DSA_build_fullkey_ex(
                    eFipsStandard,
                    &HashFunctionCtx,
                    pdwDsaKey,
                    &other,
                    pDsaFullKey,
                    TRUE,          // Verify!
                    &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));

    // Set the new parameters
    pdwDsaKey->nbitx = 8*pKE->cbPrivateKeyB;      // We might have bigger keys
    testInteropReverseMemCopy( (PBYTE)pdwDhKey->x, pKE->rbPrivateKeyB, pKE->cbPrivateKeyB );
    testInteropReverseMemCopy( (PBYTE)pdwDhKey->y, pKE->rbPublicKeyB, pKE->cbPrimeP );

    // 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, "?" );

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