Problem with HMAC RRS feed

  • Question

  • According to Wikipedia and verified with online HMAC routines, HMAC_SHA1("key", "The quick brown fox jumps over the lazy dog") should produce an HMAC of:
    DE 7C 9B 85 B8 B7 8A A6 BC 8A 7A 36 F7 0A 90 70 1C 9D B4 D9
    However, when I use the Example C Program: Creating an HMAC on MSDN:
    (shortened here for brevity), I get:
    41 4E 4C 89 33 30 47 9B 9E F1 85 DF 40 6A 66 33 49 D6 3A C7

    What am I doing wrong?

    J.A. Coutts

    HCRYPTPROV  hProv       = NULL;
    HCRYPTHASH  hHash       = NULL;
    HCRYPTKEY   hKey        = NULL;
    HCRYPTHASH  hHmacHash   = NULL;
    PBYTE       pbHash      = NULL;
    DWORD       dwDataLen   = 0;
    BYTE        Data1[]     = {0x6B,0x65,0x79}; //"key"
    BYTE        Data2[]     = {0x54,0x68,0x65,0x20,0x71,0x75,0x69,0x63,0x6B,0x20,
    //"The quick brown fox jumps over the lazy dog"
    HMAC_INFO   HmacInfo;
    ZeroMemory(&HmacInfo, sizeof(HmacInfo));
    HmacInfo.HashAlgid = CALG_SHA1;
    (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
    (!CryptCreateHash(hProv, CALG_SHA1, 0, 0, &hHash))
    (!CryptHashData(hHash, Data1, sizeof(Data1), 0))
    (!CryptDeriveKey(hProv, CALG_RC4, hHash, 0, &hKey))

    (!CryptCreateHash(hProv, CALG_HMAC, hKey, 0, &hHmacHash))
    (!CryptSetHashParam(hHmacHash, HP_HMAC_INFO, (BYTE*)&HmacInfo, 0))
    (!CryptHashData(hHmacHash, Data2, sizeof(Data2), 0))
    (!CryptGetHashParam(hHmacHash, HP_HASHVAL, NULL, &dwDataLen, 0))
    pbHash = (BYTE*)malloc(dwDataLen);
    (!CryptGetHashParam(hHmacHash, HP_HASHVAL, pbHash, &dwDataLen, 0))

    Tuesday, November 26, 2013 6:59 PM


  • I got fed up with this routine and wrote my own HMAC routine. It's a pretty simple process, but it requires the actual key (not just a handle). If you already have a handle to the key, the MSDN routine works just fine. Translation; the problem is in the key generation itself.

    J.A. Coutts

    • Marked as answer by allecnarf Wednesday, November 27, 2013 4:24 PM
    Wednesday, November 27, 2013 4:24 PM