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.

Here we show how to use this function by binding it to the API from Recipe 11.2:<br />

#include <br />

#include <br />

static HCRYPTPROV hProvider;<br />

void spc_rand_init(void) {<br />

if (!CryptAcquireContext(&hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))<br />

ExitProcess((UINT)-1); /* Feel free to properly signal an error instead. */<br />

}<br />

unsigned char *spc_rand(unsigned char *pbBuffer, size_t cbBuffer) {<br />

if (!hProvider) spc_rand_init( );<br />

if (!CryptGenRandom(hProvider, cbBuffer, pbBuffer))<br />

ExitProcess((UINT)-1); /* Feel free to properly signal an error instead. */<br />

return pbBuffer;<br />

}<br />

unsigned char *spc_keygen(unsigned char *pbBuffer, size_t cbBuffer) {<br />

if (!hProvider) spc_rand_init( );<br />

if (!CryptGenRandom(hProvider, cbBuffer, pbBuffer))<br />

ExitProcess((UINT)-1);<br />

return pbBuffer;<br />

}<br />

See Also<br />

Recipes 5.25, 11.2, 11.8, 11.20, 11.21, 11.22, 11.23<br />

11.5 Using an Application-Level Generator<br />

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

You are in an environment where you do not have access to a built-in, cryptographically<br />

strong pseudo-random number generator. You have obtained enough entropy<br />

to seed a pseudo-random generator, but you lack a generator.<br />

Solution<br />

For general-purpose use, we recommend a pseudo-random number generator based<br />

on the AES encryption algorithm run in counter (CTR) mode (see Recipe 5.9). This<br />

generator has the best theoretical security assurance, assuming that the underlying<br />

cryptographic primitive is secure. If you would prefer a generator based on a hash<br />

function, you can run HMAC-SHA1 (see Recipe 6.10) in counter mode.<br />

In addition, the keystream of a secure stream cipher can be used as a pseudo-random<br />

number generator.<br />

Using an Application-Level Generator | 581<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!