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.

strncpy(a.sun_path, EGD_SOCKET_PATH, sizeof(a.sun_path));<br />

a.sun_path[sizeof(a.sun_path) - 1] = 0;<br />

if (connect(spc_egd_fd, (struct sockaddr *)&a, sizeof(a))) {<br />

perror("Entropy server connection failed");<br />

exit(-1);<br />

}<br />

}<br />

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

ssize_t nb;<br />

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

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

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

for (p = buf; l; l -= tbytes) {<br />

/* Build and send the request command to the EGD server */<br />

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

do {<br />

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

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

exit(-1);<br />

}<br />

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

/* Get the number of bytes in the result */<br />

do {<br />

if ((nb = read(spc_egd_fd, &nbytes, 1)) = = -1 && errno != EINTR) {<br />

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

exit(-1);<br />

}<br />

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

tbytes = nbytes;<br />

/* Get all of the data from the result */<br />

while (nbytes) {<br />

do {<br />

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

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

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

exit(-1);<br />

}<br />

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

p += nb;<br />

nbytes -= nb;<br />

}<br />

/* If we didn't get as much entropy as we asked for, the server has no more<br />

* left, so we must fall back on the application-level generator to avoid<br />

* blocking.<br />

*/<br />

if (tbytes != cmd[l]) {<br />

spc_rand(p, l);<br />

break;<br />

}<br />

Using an Entropy Gathering Daemon–Compatible Solution | 597<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!