libcrypto: Remove unused old version and date in Makefile.inc.
[dragonfly.git] / contrib / cryptsetup / luks / random.c
blob711a7b5af31867ec18acc91e54a0314679bf5b7a
1 /*
2 * Random supply helper
3 * Copyright 2004, Clemens Fruhwirth <clemens@endorphin.org>
5 */
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <errno.h>
11 #include <stdio.h>
12 #include <unistd.h>
14 static int randomfd = -1;
16 int openRandom() {
17 if(randomfd == -1)
18 randomfd = open("/dev/urandom", O_RDONLY);
19 return randomfd;
22 /* This method leaks a file descriptor that can be obtained by calling
23 closeRandom */
24 int getRandom(char *buf, size_t len)
26 if(openRandom() == -1) {
27 perror("getRandom:");
28 return -EINVAL;
30 while(len) {
31 int r;
32 r = read(randomfd,buf,len);
33 if (-1 == r && errno != -EINTR) {
34 perror("read: "); return -EINVAL;
36 len-= r; buf += r;
38 return 0;
41 void closeRandom() {
42 if(randomfd != -1) {
43 close(randomfd);
44 randomfd = -1;