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
|
#include <string>
#include <vector>
#include <stdio.h>
#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_get_entropy_count()
{
int n_bits;
int fd = open(DEV_RANDOM, O_RDONLY);
if (fd == -1)
error_exit("Failed to open %s", DEV_RANDOM);
if (ioctl(fd, RNDGETENTCNT, &n_bits) == -1)
error_exit("ioctl(RNDGETENTCNT) failed");
close(fd);
return n_bits;
}
int kernel_rng_add_entropy(unsigned char *data, int n, int n_bits)
{
int total_size;
struct rand_pool_info *output;
int fd = open(DEV_RANDOM, O_WRONLY);
if (fd == -1)
error_exit("Failed to open %s", DEV_RANDOM);
total_size = sizeof(struct rand_pool_info) + n;
output = (struct rand_pool_info *)malloc(total_size);
if (!output)
error_exit("malloc failure in kernel_rng_add_entropy_no_bitcount_increase(%d)", n);
output -> entropy_count = n_bits;
output -> buf_size = n;
memcpy(&(output -> buf[0]), data, n);
if (ioctl(fd, RNDADDENTROPY, output) == -1)
error_exit("ioctl(RNDADDENTROPY) failed!");
free(output);
close(fd);
return 0;
}
int kernel_rng_get_max_entropy_count(void)
{
int bit_count;
FILE *fh = fopen(PROC_POOLSIZE, "r");
if (!fh)
error_exit("Failed to open %s", PROC_POOLSIZE);
if (fscanf(fh, "%d", &bit_count) != 1)
error_exit("Failed to read from %s", PROC_POOLSIZE);
fclose(fh);
return bit_count;
}
|