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.

}<br />

return buf;<br />

}<br />

unsigned char *spc_entropy(unsigned char *buf, size_t l) {<br />

ssize_t nb;<br />

unsigned char *p;<br />

static unsigned char cmd = 0x02;<br />

if (spc_egd_fd = = -1) spc_rand_init( );<br />

/* Send the request command to the EGD server */<br />

do {<br />

if ((nb = write(spc_egd_fd, &cmd, sizeof(cmd))) = = -1 && errno != EINTR) {<br />

perror("Communcation with entropy server failed");<br />

exit(-1);<br />

}<br />

} while (nb = = -1);<br />

for (p = buf; l; p += nb, l -= nb) {<br />

do {<br />

if ((nb = read(spc_egd_fd, p, l)) = = -1) {<br />

if (errno = = -1) continue;<br />

perror("Communication with entropy server failed");<br />

exit(-1);<br />

}<br />

} while (nb = = -1);<br />

}<br />

return buf;<br />

}<br />

void spc_egd_write_entropy(unsigned char *data, size_t l) {<br />

ssize_t nb;<br />

unsigned char *buf, nbytes, *p;<br />

static unsigned char cmd[4] = { 0x03, 0, 0, 0 };<br />

for (buf = data; l; l -= cmd[3]) {<br />

cmd[3] = (l > 255 ? 255 : l);<br />

for (nbytes = 0, p = cmd; nbytes < sizeof(cmd); nbytes += nb) {<br />

do {<br />

if ((nb = write(spc_egd_fd, cmd, sizeof(cmd) - nbytes)) = = -1) {<br />

if (errno != EINTR) continue;<br />

perror("Communication with entropy server failed");<br />

exit(-1);<br />

}<br />

} while (nb = = -1);<br />

}<br />

for (nbytes = 0; nbytes < cmd[3]; nbytes += nb, buf += nb) {<br />

do {<br />

if ((nb = write(spc_egd_fd, data, cmd[3] - nbytes)) = = -1) {<br />

if (errno != EINTR) continue;<br />

perror("Communication with entropy server failed");<br />

exit(-1);<br />

598 | Chapter 11: Random Numbers<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!