Created
October 4, 2012 08:37
-
-
Save dspezia/3832276 to your computer and use it in GitHub Desktop.
Dead simple (unsafe) entropy generator for Linux /dev/random
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <fcntl.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <sys/ioctl.h> | |
#include <linux/types.h> | |
#include <linux/random.h> | |
#define N 1024 | |
int main() | |
{ | |
struct { | |
int ent_count; | |
int size; | |
unsigned char data[N]; | |
} entropy; | |
int src, dst; | |
FILE *source; | |
if ( (dst = open("/dev/random", O_WRONLY)) < 0) { | |
perror("random" ); | |
exit(1); | |
} | |
if ( (src=open("/dev/urandom", O_RDONLY )) < 0 ) { | |
perror("urandom"); | |
exit(1); | |
} | |
for ( ; ; ) { | |
char buf[N]; | |
if ( N != read( src, entropy.data, N ) ) { | |
perror("read"); | |
exit(1); | |
} | |
entropy.ent_count = entropy.size = N; | |
if ( ioctl(dst, RNDADDENTROPY, &entropy) < 0 ) { | |
perror("ioctl"); | |
exit(1); | |
} | |
sleep( 1 ); | |
} | |
exit( 0 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment