21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

to key data. In general, CALG_RC4 should work fine for arbitrary key data (the value<br />

will effectively be ignored).<br />

#include <br />

#include <br />

#include <br />

void SpcIncrementalHMAC(BYTE *pbKey, DWORD cbKey, ALG_ID Algid) {<br />

BYTE out[20];<br />

DWORD cbData = sizeof(out), i;<br />

HCRYPTKEY hKey;<br />

HMAC_INFO HMACInfo;<br />

HCRYPTHASH hHash;<br />

HCRYPTPROV hProvider;<br />

hProvider = SpcGetExportableContext( );<br />

hKey = SpcImportKeyData(hProvider, Algid, pbKey, cbKey);<br />

CryptCreateHash(hProvider, CALG_HMAC, hKey, 0, &hHash);<br />

HMACInfo.HashAlgid = CALG_SHA1;<br />

HMACInfo.pbInnerString = HMACInfo.pbOuterString = 0;<br />

HMACInfo.cbInnerString = HMACInfo.cbOuterString = 0;<br />

CryptSetHashParam(hHash, HP_HMAC_INFO, (BYTE *)&HMACInfo, 0);<br />

CryptHashData(hHash, (BYTE *)"fred", 4, 0);<br />

CryptGetHashParam(hHash, HP_HASHVAL, out, &cbData, 0);<br />

for (i = 0; i < cbData; i++) printf("%02x", out[i]);<br />

printf("\n");<br />

CryptDestroyHash(hHash);<br />

CryptDestroyKey(hKey);<br />

CryptReleaseContext(hProvider, 0);<br />

}<br />

If you aren’t using OpenSSLor CryptoAPI, but you have a hash function that you’d<br />

like to use with HMAC, you can use the following HMAC implementation:<br />

#include <br />

#include <br />

typedef struct {<br />

DGST_CTX mdctx;<br />

unsigned char inner[DGST_BLK_SZ];<br />

unsigned char outer[DGST_BLK_SZ];<br />

} SPC_HMAC_CTX;<br />

void SPC_HMAC_Init(SPC_HMAC_CTX *ctx, unsigned char *key, size_t klen) {<br />

int i;<br />

unsigned char dk[DGST_OUT_SZ];<br />

DGST_Init(&(ctx->mdctx));<br />

memset(ctx->inner, 0x36, DGST_BLK_SZ);<br />

memset(ctx->outer, 0x5c, DGST_BLK_SZ);<br />

278 | Chapter 6: Hashes and Message Authentication<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!