1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
#include <stdio.h>
#include <string>
#include <vector>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <asm/types.h>
#include <arpa/inet.h>
#include <linux/random.h>
#include "error.h"
#include "utils.h"
#include "kernel_prng_io.h"
int kernel_rng_read_blocking(unsigned char *buffer, int n)
{
int fd = open(DEV_RANDOM, O_RDONLY);
if (fd == -1)
error_exit("Failed to open %s", DEV_RANDOM);
int rc = -1;
if (READ(fd, buffer, n) == n)
rc = n;
close(fd);
return rc;
}
int kernel_rng_read_non_blocking(unsigned char *buffer, int n)
{
int rc;
int fd = open(DEV_URANDOM, O_RDONLY);
if (fd == -1)
error_exit("Failed to open %s", DEV_URANDOM);
for(;;)
{
rc = read(fd, buffer, n);
if (rc == -1)
{
if (errno == EINTR || errno == EAGAIN)
continue;
error_exit("error reading from %s", DEV_URANDOM);
}
break;
}
close(fd);
return rc;
}
int kernel_rng_write_non_blocking(unsigned char *buffer, int n)
{
int rc;
int fd = open(DEV_URANDOM, O_WRONLY);
if (fd == -1)
return -1;
for(;;)
{
rc = write(fd, buffer, n);
if (rc == -1)
{
if (errno == EINTR || errno == EAGAIN)
continue;
error_exit("error writing to %s", DEV_URANDOM);
}
break;
}
close(fd);
return rc;
}
|