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.

Using a stream cipher as a generator<br />

As we mentioned, stream ciphers are themselves pseudo-random number generators,<br />

where the key (and the initialization vector, if appropriate) constitutes the seed.<br />

If you are planning to use such a cipher, we strongly recommend the SNOW 2.0<br />

cipher, discussed in Recipe 5.2.<br />

Because of the popularity of the RC4 cipher, we expect that people will prefer to use<br />

RC4, even though it does not look as good as SNOW. The RC4 stream cipher does<br />

make an acceptable pseudo-random number generator, and it is incredibly fast if you<br />

do not rekey frequently (that is particularly useful if you expect to need a heck of a<br />

lot of numbers). If you do rekey frequently to avoid backtracking attacks, a block<br />

cipher–based approach may be faster; time it to make sure.<br />

RC4 requires a little bit of work to use properly, given a standard API. First, most<br />

APIs want you to pass in data to encrypt. Because you want only the raw keystream,<br />

you must always pass in zeros. Second, be sure to use RC4 in a secure manner, as<br />

discussed in Recipe 5.23.<br />

If your RC4 implementation has the API discussed in Recipe 5.23, seeding it as a<br />

pseudo-random number generator is the same as keying the algorithm. RC4 can<br />

accept keys up to 256 bytes in length.<br />

Because of limitations in RC4, you should throw away the first 256<br />

bytes of RC4 output, as discussed in Recipe 5.23.<br />

After encrypting 256 bytes and throwing the results away, you can then, given an<br />

RC4 context, get random data by encrypting zeros. Assuming the RC4 API from Recipe<br />

5.23 and assuming you have a context statically allocated in a global variable<br />

named spc_prng, here’s a binding of RC4 to the spc_rand( ) function that we introduced<br />

in Recipe 11.2:<br />

/* NOTE: This code should be augmented to reseed after each request<br />

/* for pseudo-random data, as discussed in Recipe 11.6<br />

/*<br />

#ifndef WIN32<br />

#include <br />

static pthread_mutex_t spc_rc4rng_mutex = PTHREAD_MUTEX_INITIALIZER;<br />

#define SPC_RC4RNG_LOCK( ) pthread_mutex_lock(&spc_rc4rng_mutex)<br />

#define SPC_RC4RNG_UNLOCK( ) pthread_mutex_unlock(&spc_rc4rng_mutex)<br />

#else<br />

#include <br />

static HANDLE hSpcRC4RNGMutex;<br />

#define SPC_RC4RNG_LOCK( ) WaitForSingleObject(hSpcRC4RNGMutex, INFINITE)<br />

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