Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fcntl.h>
- #include <linux/types.h>
- #include <linux/random.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/ioctl.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #define MAX_BYTES 512
- #define MAX_BITS (8 * MAX_BYTES)
- struct my_rand_pool_info {
- int entropy_count;
- int buf_size;
- unsigned char buf[MAX_BYTES];
- } rpi;
- static ssize_t
- add_entropy(size_t nbits)
- {
- int fd = open( "/dev/random", O_RDWR);
- if (fd < 0) {
- perror( "Cannot open /dev/random");
- return -1;
- }
- rpi.entropy_count = nbits;
- rpi.buf_size = 1;
- rpi.buf[0] = 0;
- if (ioctl(fd, RNDADDENTROPY, &rpi) == -1) {
- perror("ioctl");
- return -1;
- }
- return rpi.entropy_count;
- }
- int main( int argc, char *argv[]) {
- int nbits = 0;
- if (argc < 2) {
- fprintf( stderr, "Usage: rand_ioctl <num bits>\n");
- exit(1);
- }
- nbits = atoi( argv[1]);
- if (nbits <= 0)
- exit(0);
- else if (nbits > MAX_BITS)
- nbits = MAX_BITS;
- printf( "Adding %d bits of fake entropy to the pool.\n", nbits);
- ssize_t nbits_added = add_entropy( nbits);
- if (nbits_added >= 0) {
- printf( "%d bits added.\n", nbits_added);
- exit(0);
- }
- else
- exit(1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement