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.

CryptAcquireContext(&hProvider, 0, MS_DEF_PROV, PROV_RSA_FULL, 0);<br />

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

CryptHashData(hHash, pbIn, cbIn, 0);<br />

CryptGetHashParam(hHash, HP_HASHSIZE, (BYTE *)cbOut, &cbData, 0);<br />

pbOut = (BYTE *)LocalAlloc(LMEM_FIXED, *cbOut);<br />

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

CryptDestroyHash(hHash);<br />

CryptReleaseContext(hProvider, 0);<br />

return pbOut;<br />

}<br />

See Also<br />

Recipe 6.7<br />

6.7 Using a Cryptographic Hash<br />

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

You need to use a cryptographic hash function outside the context of a MAC, and<br />

you want to avoid length-extension attacks, which are quite often possible.<br />

Solution<br />

A good way to thwart length-extension attacks is to run the hash function twice, once<br />

over the message, and once over the output of the first hash. This does not protect<br />

against birthday attacks, which probably aren’t a major problem in most situations. If<br />

you need to protect against those attacks as well, use the advice in Recipe 6.8 on the<br />

first hash operation.<br />

Discussion<br />

Hash functions are not secure by themselves—not for a password system,<br />

not for message authentication, not for anything!<br />

Because all of the commonly used cryptographic hash functions break a message into<br />

blocks that get processed in an iterative fashion, it’s often possible to extend the message<br />

and at the same time extend the associated hash, even if some sort of “secret”<br />

data was processed at the start of a message.<br />

It’s easy to get rid of this kind of problem at the application level. When you need a<br />

cryptographic hash, don’t use SHA1 or something similar directly. Instead, write a<br />

wrapper that hashes the message with your cryptographic hash function, then takes<br />

that output and hashes it as well, returning the result.<br />

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

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.<br />

Using a Cryptographic Hash | 269

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

Saved successfully!

Ooh no, something went wrong!