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.

f[c + 2] = '>';<br />

} else<br />

snprintf(bf, sizeof(bf), "\r%d%%", (int)(entropy * 100.0 / target));<br />

while (write(ttyfd, bf, strlen(bf)) = = -1)<br />

if (errno != EAGAIN) abort( );<br />

}<br />

static void spc_end_progress_bar(int target, int ttyfd) {<br />

int bsz, i;<br />

if (!(bsz = spc_get_barsize(ttyfd))) {<br />

printf("100%%\r\n");<br />

return;<br />

}<br />

printf("\r[");<br />

for (i = 0; i < bsz; i++) putchar('=');<br />

printf("] 100%%\r\n");<br />

}<br />

void spc_gather_keyboard_entropy(int l, char *output) {<br />

int fd, n;<br />

char lastc = 0;<br />

double entropy = 0.0;<br />

SHA_CTX pool;<br />

volatile char dgst[HASH_OUT_SZ];<br />

struct termios opts;<br />

struct {<br />

char c;<br />

long long timestamp;<br />

} data;<br />

if (l > HASH_OUT_SZ) abort( );<br />

if ((fd = open("/dev/tty", O_RDWR)) = = -1) abort( );<br />

spc_raw(fd, &opts);<br />

SHA1_Init(&pool);<br />

do {<br />

spc_show_progress_bar(entropy, l * 8, fd);<br />

if ((n = read(fd, &(data.c), 1)) < 1) {<br />

if (errno = = EAGAIN) continue;<br />

abort( );<br />

}<br />

current_stamp(&(data.timestamp));<br />

SHA1_Update(&pool, &data, sizeof(data));<br />

if (lastc != data.c) entropy += ENTROPY_PER_SAMPLE;<br />

lastc = data.c;<br />

} while (entropy < (l * 8));<br />

spc_end_progress_bar(l * 8, fd);<br />

/* Try to reset the terminal. */<br />

tcsetattr(fd, TCSAFLUSH, &opts);<br />

close(fd);<br />

SHA1_Final((unsigned char *)dgst, &pool);<br />

spc_memcpy(output, (char *)dgst, l);<br />

spc_memset(dgst, 0, sizeof(dgst));<br />

}<br />

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