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.

HMAC_CTX c;<br />

unsigned long ctr = 0, nbo_ctr;<br />

size_t tl, i;<br />

unsigned char last[HMAC_OUT_LEN];<br />

while (ol >= HMAC_OUT_LEN) {<br />

HMAC_Init(&c, base, bl, EVP_sha1( ));<br />

HMAC_Update(&c, dist, dl);<br />

nbo_ctr = htonl(ctr++);<br />

HMAC_Update(&c, (unsigned char *)&nbo_ctr, sizeof(nbo_ctr));<br />

HMAC_Final(&c, out, &tl);<br />

out += HMAC_OUT_LEN;<br />

ol -= HMAC_OUT_LEN;<br />

}<br />

if (!ol) return;<br />

HMAC_Init(&c, base, bl, EVP_sha1( ));<br />

HMAC_Update(&c, dist, dl);<br />

nbo_ctr = htonl(ctr);<br />

HMAC_Update(&c, (unsigned char *)&nbo_ctr, sizeof(nbo_ctr));<br />

HMAC_Final(&c, last, &tl);<br />

for (i = 0; i < ol; i++)<br />

out[i] = last[i];<br />

}<br />

Here is an example implementation of a PRF based on HMAC-SHA1, using the Windows<br />

CryptoAPI for HMAC (discussed in Recipe 6.10). The code presented here also<br />

requires SpcGetExportableContext( ) and SpcImportKeyData( ) from Recipe 5.26.<br />

#include <br />

#include <br />

#define HMAC_OUT_LEN 20 /* SHA1 specific */<br />

static DWORD SwapInt32(DWORD dwInt32) {<br />

__asm mov eax, dwInt32<br />

__asm bswap eax<br />

}<br />

BOOL SpcMakeDerivedKey(BYTE *pbBase, DWORD cbBase, BYTE *pbDist, DWORD cbDist,<br />

BYTE *pbOut, DWORD cbOut) {<br />

BYTE pbLast[HMAC_OUT_LEN];<br />

DWORD cbData, dwCounter = 0, dwBigCounter;<br />

HCRYPTKEY hKey;<br />

HMAC_INFO HMACInfo;<br />

HCRYPTHASH hHash;<br />

HCRYPTPROV hProvider;<br />

if (!(hProvider = SpcGetExportableContext( ))) return FALSE;<br />

if (!(hKey = SpcImportKeyData(hProvider, CALG_RC4, pbBase, cbBase))) {<br />

CryptReleaseContext(hProvider, 0);<br />

return FALSE;<br />

}<br />

HMACInfo.HashAlgid = CALG_SHA1;<br />

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

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

Algorithmically Generating Symmetric Keys from One Base Secret | 145<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!