mirror of
https://gitlab.com/cryptsetup/cryptsetup.git
synced 2025-12-12 11:20:10 +01:00
Initial checking of what was revision 56 on http://luks.endorphin.org/svn/cryptsetup
git-svn-id: https://cryptsetup.googlecode.com/svn/trunk@3 36d66b0a-2a48-0410-832c-cd162a569da5
This commit is contained in:
48
luks/random.c
Normal file
48
luks/random.c
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Random supply helper
|
||||
* Copyright 2004, Clemens Fruhwirth <clemens@endorphin.org>
|
||||
*
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static int randomfd = -1;
|
||||
|
||||
int openRandom() {
|
||||
if(randomfd == -1)
|
||||
randomfd = open("/dev/urandom", O_RDONLY);
|
||||
return randomfd;
|
||||
}
|
||||
|
||||
/* This method leaks a file descriptor that can be obtained by calling
|
||||
closeRandom */
|
||||
int getRandom(char *buf, size_t len)
|
||||
{
|
||||
int r = 0;
|
||||
|
||||
if(openRandom() == -1) {
|
||||
perror("getRandom:");
|
||||
return -EINVAL;
|
||||
}
|
||||
while(len) {
|
||||
int r;
|
||||
r = read(randomfd,buf,len);
|
||||
if (-1 == r && errno != -EINTR) {
|
||||
perror("read: "); return -EINVAL;
|
||||
}
|
||||
len-= r; buf += r;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
void closeRandom() {
|
||||
if(randomfd != -1) {
|
||||
close(randomfd);
|
||||
randomfd = -1;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user