Skip to content

Instantly share code, notes, and snippets.

@szaydel
Created May 4, 2021 13:09
Show Gist options
  • Save szaydel/06c7a8c1c0cd1fc228ad88fea3f6eaca to your computer and use it in GitHub Desktop.
Save szaydel/06c7a8c1c0cd1fc228ad88fea3f6eaca to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/random.h>
#define BUFSIZE 256
typedef struct {
int entropy_count;
int buf_size;
unsigned int buf[0];
} entropy_t;
int main(void) {
entropy_t *en = malloc(sizeof(*en) + sizeof(unsigned int[BUFSIZE]));
// for (int i = 0 ; i < BUFSIZE; i++) {
// en->buf[i] = i;
// }
// for (int i = 0 ; i < BUFSIZE; i++) {
// printf("(%d) => %d\n", i, en->buf[i]);
// }
// printf("sizeof(*en): %zu\n", sizeof(*en));
// printf("sizeof(en): %zu\n", sizeof(en));
// printf("sizeof(unsigned int[%d]): %zu\n", BUFSIZE, sizeof(unsigned int[BUFSIZE]));
int fd = open("/dev/random", O_WRONLY);
int rc = ioctl(fd, RNDADDENTROPY, en);
if (rc != 0) {
perror("ioctl()");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment