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.

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

if (!CryptCreateHash(hProvider, CALG_HMAC, hKey, 0, &hHash)) {<br />

CryptDestroyKey(hKey);<br />

CryptReleaseContext(hProvider, 0);<br />

return FALSE;<br />

}<br />

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

CryptHashData(hHash, pbDist, cbDist, 0);<br />

dwBigCounter = SwapInt32(dwCounter++);<br />

CryptHashData(hHash, (BYTE *)&dwBigCounter, sizeof(dwBigCounter), 0);<br />

cbData = HMAC_OUT_LEN;<br />

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

CryptDestroyHash(hHash);<br />

pbOut += HMAC_OUT_LEN;<br />

cbOut -= HMAC_OUT_LEN;<br />

}<br />

if (cbOut) {<br />

if (!CryptCreateHash(hProvider, CALG_HMAC, hKey, 0, &hHash)) {<br />

CryptDestroyKey(hKey);<br />

CryptReleaseContext(hProvider, 0);<br />

return FALSE;<br />

}<br />

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

CryptHashData(hHash, pbDist, cbDist, 0);<br />

dwBigCounter = SwapInt32(dwCounter);<br />

CryptHashData(hHash, (BYTE *)&dwBigCounter, sizeof(dwBigCounter), 0);<br />

cbData = HMAC_OUT_LEN;<br />

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

CryptDestroyHash(hHash);<br />

CopyMemory(pbOut, pbLast, cbOut);<br />

}<br />

CryptDestroyKey(hKey);<br />

CryptReleaseContext(hProvider, 0);<br />

return TRUE;<br />

}<br />

Ultimately, if you have a well-specified constant set of distinguishers and a constant<br />

base secret length, it is sufficient to replace HMAC by SHA1-hashing the concatenation<br />

of the key, distinguisher, and counter.<br />

See Also<br />

Recipes 4.10, 5.26, 6.10, 6.15, 6.16<br />

4.12 Encrypting in a Single Reduced Character Set<br />

<strong>Problem</strong><br />

You’re storing data in a format in which particular characters are invalid. For example,<br />

you might be using a database, and you’d like to encrypt all the fields, but the<br />

146 | Chapter 4: Symmetric Cryptography Fundamentals<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!