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.

See Also<br />

Recipes 11.19, 11.20<br />

11.22 Gathering Entropy from Thread Timings<br />

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

You want to collect some entropy without user intervention, hoping that there is<br />

some inherent, measurable entropy in the environment.<br />

Solution<br />

In practice, timing how long it takes to start up and stop a particular number of<br />

threads can provide a bit of entropy. For example, many Java virtual machines exclusively<br />

use such a technique to gather entropy.<br />

Because the thread timing data is only indirectly related to actual user input, it is<br />

good to be extremely conservative about the quality of this entropy source. We recommend<br />

the following methodology:<br />

1. Launch and join on some fixed number of threads (at least 100).<br />

2. Mix in a timestamp when all threads have returned successfully.<br />

3. Estimate entropy based on the considerations discussed in Recipe 11.19.<br />

4. Wait at least a second before trying again, in hopes that there is additional<br />

entropy affecting the system later.<br />

The following code spawns a particular number of threads that you can time, in the<br />

hope of getting some entropy. This code works on Unix implementations that have<br />

the pthreads library (the POSIX standard for threads). Linking is different depending<br />

on platform; check your local pthreads documentation.<br />

#include <br />

static void *thread_stub(void *arg) {<br />

return 0;<br />

}<br />

void spc_time_threads(unsigned int numiters) {<br />

pthread_t tid;<br />

}<br />

while (numiters--)<br />

if (!pthread_create(&tid, 0, thread_stub, 0))<br />

pthread_join(tid, 0);<br />

Gathering Entropy from Thread Timings | 643<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!