21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

SpcGatherKeyboardEntropy( ) uses the CryptoAPI to hash the data collected from the<br />

keyboard. It first acquires a context object, then creates a hash object. After the arguments<br />

are validated, the dialog resource is loaded by calling CreateDialog( ), which<br />

creates a modeless dialog. The dialog is created modeless so that keyboard messages<br />

can be captured. If a modal dialog is created using DialogBox( ) or one of its siblings,<br />

message handling for the dialog prevents us from capturing the keyboard messages.<br />

Once the dialog is successfully created, the message-handling loop performs normal<br />

message dispatching, calling IsDialogMessage( ) to do dialog message processing.<br />

Keyboard messages are captured in the loop prior to calling IsDialogMessage( ), however.<br />

That’s because IsDialogMessage( ) causes the messages to be translated and dispatched,<br />

so handling them in the dialog’s message procedure isn’t possible.<br />

When a key is pressed, a WM_KEYDOWN message will be received, which contains information<br />

about which key was pressed. When a key is released, a WM_KEYUP message<br />

will be received, which contains the same information about which key was released<br />

as WM_KEYDOWN contains about a key press. The keyboard scan code is extracted from<br />

the message, combined with a timestamp, and fed into the hash object. If the current<br />

scan code is the same as the previous scan code, it is not counted as entropy but<br />

is added into the hash anyway. As other keystrokes are collected, the progress bar is<br />

updated, and when the requested amount of entropy has been obtained, the OK button<br />

is enabled.<br />

When the OK button is clicked, the dialog is destroyed, terminating the message<br />

loop. The output from the hash function is copied into the output buffer from the<br />

caller, and internal data is cleaned up before returning to the caller.<br />

#include <br />

#include <br />

#include <br />

#define SPC_ENTROPY_PER_SAMPLE 0.5<br />

#define SPC_KEYBOARD_DLGID 101<br />

#define SPC_PROGRESS_BARID 1000<br />

#define SPC_KEYBOARD_STATIC -1<br />

typedef struct {<br />

BYTE bScanCode;<br />

DWORD dwTickCount;<br />

} SPC_KEYPRESS;<br />

static BOOL CALLBACK KeyboardEntropyProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,<br />

LPARAM lParam) {<br />

HWND *pHwnd;<br />

if (uMsg != WM_COMMAND || LOWORD(wParam) != IDOK ||<br />

HIWORD(wParam) != BN_CLICKED) return FALSE;<br />

pHwnd = (HWND *)GetWindowLong(hwndDlg, DWL_USER);<br />

DestroyWindow(hwndDlg);<br />

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