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.

if (cbOutput > cbHashData) goto done;<br />

if (!(pbHashData = (BYTE *)LocalAlloc(LMEM_FIXED, cbHashData))) goto done;<br />

DialogData.dEntropy = 0.0;<br />

DialogData.cbRequested = cbOutput * 8;<br />

DialogData.hHash = hHash;<br />

DialogData.dwLastTime = 0;<br />

GetCursorPos(&(DialogData.ptLastPos));<br />

bResult = DialogBoxParam(hInstance, MAKEINTRESOURCE(SPC_MOUSE_DLGID),<br />

hWndParent, MouseEntropyProc, (LPARAM)&DialogData);<br />

if (bResult) {<br />

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

bResult = FALSE;<br />

else<br />

CopyMemory(pbOutput, pbHashData, cbOutput);<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, entropy<br />

could be collected throughout your entire program by installing a message hook or<br />

by moving the entropy collection code out of MouseEntropyProc( ) and placing it into<br />

your program’s main message processing loop. SpcGatherMouseEntropy( ) could then<br />

be modified to operate in global state, presenting a dialog only if there is not a sufficient<br />

amount of entropy 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, DialogBoxParam( ) will fail inexplicably<br />

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

following code demonstrates initializing common controls and calling<br />

SpcGatherMouseEntropy( ):<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 />

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

return 0;<br />

}<br />

642 | Chapter 11: Random Numbers<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!