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.

}<br />

}<br />

if (hWndParent) EnableWindow(hWndParent, TRUE);<br />

}<br />

if (dEntropy >= cbOutput * 8) {<br />

if (CryptGetHashParam(hHash, HP_HASHVAL, pbHashData, &cbHashData, 0)) {<br />

bResult = TRUE;<br />

CopyMemory(pbOutput, pbHashData, cbOutput);<br />

}<br />

}<br />

done:<br />

if (pbHashData) LocalFree(pbHashData);<br />

if (hHash) CryptDestroyHash(hHash);<br />

if (hProvider) CryptReleaseContext(hProvider, 0);<br />

return bResult;<br />

}<br />

There are other ways to achieve the same result on Windows. For example, you<br />

could install a temporary hook to intercept all messages and use the modal dialog<br />

functions instead of the modeless ones that we have used here. Another possibility is<br />

to be collecting entropy throughout your entire program by installing a more permanent<br />

hook or by moving the entropy collection code out of<br />

SpcGatherKeyboardEntropy( ) and placing it into your program’s main message-processing<br />

loop. SpcGatherKeyboardEntropy( ) could then be modified to operate in global<br />

state, presenting a dialog only if there is not a sufficient amount of entropy<br />

collected already.<br />

Note that the dialog uses a progress bar control. While this control is a standard control<br />

on Windows, it is part of the common controls, so you must initialize common<br />

controls before instantiating the dialog; otherwise, CreateDialog( ) will fail inexplicably<br />

(GetLastError( ) will return 0, which obviously is not very informative). The following<br />

code demonstrates initializing common controls and calling<br />

SpcGatherKeyboardEntropy( ):<br />

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,<br />

int nShowCmd) {<br />

BYTE pbEntropy[20];<br />

INITCOMMONCONTROLSEX CommonControls;<br />

CommonControls.dwSize = sizeof(CommonControls);<br />

CommonControls.dwICC = ICC_PROGRESS_CLASS;<br />

InitCommonControlsEx(&CommonControls);<br />

SpcGatherKeyboardEntropy(hInstance, 0, pbEntropy, sizeof(pbEntropy));<br />

return 0;<br />

}<br />

See Also<br />

Recipes 4.14, 6.5, 11.19<br />

Gathering Entropy from the Keyboard | 637<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!