summaryrefslogtreecommitdiff
path: root/src/krng-add-entropy.c
blob: fc81d29e3f4d855300953c4e8084daffdde12a69 (plain)
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
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <asm/types.h>
#include <linux/random.h>
#include <errno.h>
#include <error.h>
#include <string.h>

char *program_name;
void usage() 
{
  error(1, 0, "usage: %s [--bytes] <bits>", program_name);
}

int main(int argc, char **argv)
{
  argc--, program_name = *argv++;
  int multiplier = 1;
  int bits;
  while (argc > 1) {
    if (!strcmp(*argv, "--bytes")) {
      multiplier = 8;
    } else {
      usage();
    }
    argc--, argv++;
  }
  if ((argc != 1) || (bits = multiplier * atoi(*argv)) <= 0)
    usage();

  struct rand_pool_info *output;
  int fd = open("/dev/random", O_WRONLY);
  if (fd < 0)
    error(1, errno, "opening /dev/random");

  output = (struct rand_pool_info *) malloc(sizeof(struct rand_pool_info));
  output -> entropy_count = bits;
  output -> buf_size = 0;

  int r = ioctl(fd, RNDADDENTROPY, output);
  if (r < 0)
    error(1, errno, "ioctl(RNDADDENTROPY)");

  return 0;
}