1 #include <assert.h> 2 #include <stdint.h> 3 #include <sys/ioctl.h> 4 #include <stdio.h> 5 #include <unistd.h> 6 #include <fcntl.h> 7 #include <linux/random.h> 8 9 #define N 2048 10 11 struct entropy { 12 int ent_count; 13 int size; 14 unsigned char data[N]; 15 }; 16 main()17int main() { 18 struct entropy buf; 19 ssize_t n; 20 21 int random_fd = open("/dev/random", O_RDWR); 22 assert(random_fd >= 0); 23 24 while ((n = read(0, &buf.data, N)) > 0) { 25 buf.ent_count = n * 8; 26 buf.size = n; 27 if (ioctl(random_fd, RNDADDENTROPY, &buf) != 0) { 28 perror("failed to add entropy"); 29 } 30 } 31 32 return 0; 33 } 34