File: random.c

package info (click to toggle)
cryptsetup 2%3A1.0.4%2Bsvn26-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,572 kB
  • ctags: 418
  • sloc: sh: 10,706; ansic: 3,187; makefile: 329; python: 90; perl: 40; sed: 16
file content (37 lines) | stat: -rw-r--r-- 681 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
 *	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>

int getRandom(char *buf, size_t len)
{
	int randomfd, r = 0, index = 0;

	//FIXME Run a FIPS test for the random device or include
	// PRNG if urandom not avail.
	
	randomfd = open("/dev/urandom", O_RDONLY);
	if(-1 == randomfd) {
		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;
	}
	close(randomfd);

	return r;
}