error with CryptImportKey and large or small amounts of data

From: Andy Lowe (andyl_at_dmetrix.com)
Date: 03/11/04


Date: 10 Mar 2004 16:57:24 -0800

Hello-

I'm working on writing some code to do PBKDF2 encryption, using the
examples in Viega and Messier's "Secure Programming Cookbook" as a
guide. The code works fine with strings of 5-16 characters. With
less than 5 or more than 16 characters, a call to CryptImportKey
fails. GetLastError() returns NTE_BAD_FLAGS, although the flags are
the same for all string lengths. I'm using the enhanced provider with
PROV_RSA_FULL. The line that fails is at the bottom of the following
snippet.

if (!::CryptGetUserKey(hProvider, AT_KEYEXCHANGE, &hImportKey))
{
        if (::GetLastError() == NTE_NO_KEY)
        {
                //the key doesn't exist, so create it
                if (!::CryptGenKey(hProvider, AT_KEYEXCHANGE, (1024 << 16),
                               &hImportKey))
                {
                        hImportKey = NULL;
                }
        }
        else
        {
                hImportKey = NULL;
        }
}

//if the user's key was acquired successfully, figure out how much
space is needed and allocate it
if (hImportKey != NULL &&
        ::CryptEncrypt(hImportKey, 0, TRUE, 0, 0, &dwDataSize, dwDataSize) &&
        (pbData = static_cast<BYTE *>(::LocalAlloc(LMEM_FIXED, dwDataSize +
                                            dwHeaderLen))) != NULL)
{
        //copy the key's data into the allocated block
        ::CopyMemory(pbData + dwHeaderLen, pbKeyData, dwKeyDataSize);
        dwKeyLen = dwKeyDataSize;

        //encrypt the data
        if (::CryptEncrypt(hImportKey, 0, TRUE, 0, pbData + dwHeaderLen,
                         &dwKeyLen, dwDataSize))
        {
                pBlob = reinterpret_cast<BLOBHEADER *>(pbData);
                pAlgID = reinterpret_cast<ALG_ID *>(pbData + sizeof(BLOBHEADER));
                pBlob->bType = SIMPLEBLOB;
                pBlob->bVersion = 2;
                pBlob->reserved = 0;
                pBlob->aiKeyAlg = AlgID;
                dwDataLen = sizeof(ALG_ID);

                //transfer the key over to the provider
                if (!(bResult = ::CryptGetKeyParam(hImportKey, KP_ALGID,
                                     reinterpret_cast<BYTE *>(pAlgID),
                                    &dwDataLen, 0)))
                {
                        DWORD dwError = ::GetLastError();
                        std::cerr << "CryptGetKeyParam failed. Error: " << dwError;
                }
//-----------this fails------------------------
                else if (!(bResult = ::CryptImportKey(hProvider,
                                                                pbData,
                                                                dwDataSize + dwHeaderLen,
                                                                hImportKey,
                                                                0,
                                                                &hKey)))
                {
                        DWORD dwError = ::GetLastError();
                        std::cerr << "CryptImportKey failed. Error: " << dwError;
                }
        }
}

Any ideas? Thanks.
Andy



Relevant Pages

  • MS Sql server - make test failures
    ... In the code it is inserting data into the ... It always returns 255 characters, ... It fails the test, not fails ... Dan Strohschein ...
    (perl.dbi.users)
  • Re: CreateFile and MAX_PATH and UNICODE
    ... Yes this is a known issue with *relative* ANSI paths. ... Is the path in your code absolute or relative? ... is limited to MAX_PATH characters. ... 208 characters using CreateFile fails fails. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: How do I get a derrived point to have 2 decimals and rounded?
    ... The CCurconverts the data type to Currency. ... That fails if the value is Null, ... I want all but the last 2 characters of each name. ...
    (microsoft.public.access.queries)
  • Re: Search on Numeric property on Sharepoint 2003
    ... It appears that since it does not have enough characters in the string, ... it fails. ... "Sarah" wrote in message ... > Settings - Manage Properties of Crawled Content, ...
    (microsoft.public.sharepoint.portalserver)
  • Re: delete comments in .c file
    ... handling comments fails to reconsider characters in a few cases. ... L2_NORMAL for characters that are not in ). ... Reading email is like searching for food in the garbage, ...
    (comp.lang.c)

Loading