1
/* rndlinux.c - raw random number for OSes with /dev/random
2
* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4
* This file is part of GnuPG.
6
* GnuPG is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* GnuPG is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
31
#include <sys/types.h>
33
#ifdef HAVE_GETTIMEOFDAY
34
#include <sys/times.h>
40
#include <sys/ioctl.h>
41
#include <asm/types.h>
42
#include <linux/random.h>
47
#include "algorithms.h"
51
static int open_device( const char *name, int minor );
55
#ifdef HAVE_DEV_RANDOM_IOCTL
57
get_entropy_count( int fd )
61
if( ioctl( fd, RNDGETENTCNT, &count ) == -1 )
62
g10_log_fatal("ioctl(RNDGETENTCNT) failed: %s\n", strerror(errno) );
69
* Used to open the /dev/random devices (Linux, xBSD, Solaris (if it exists), ...)
72
open_device( const char *name, int minor )
77
fd = open( name, O_RDONLY );
79
g10_log_fatal("can't open %s: %s\n", name, strerror(errno) );
80
if( fstat( fd, &sb ) )
81
g10_log_fatal("stat() off %s failed: %s\n", name, strerror(errno) );
82
/* Don't check device type for better portability */
83
/* if( (!S_ISCHR(sb.st_mode)) && (!S_ISFIFO(sb.st_mode)) )
84
g10_log_fatal("invalid random device!\n" ); */
90
* Note: Using a level of 0 should never block and better add nothing
91
* to the pool. This is easy to accomplish with /dev/urandom.
94
rndlinux_gather_random( void (*add)(const void*, size_t, int), int requester,
95
size_t length, int level )
97
static int fd_urandom = -1;
98
static int fd_random = -1;
105
if( fd_random == -1 )
106
fd_random = open_device( NAME_OF_DEV_RANDOM, 8 );
110
/* this will also be used for elve 0 but by using /dev/urandom
111
* we can be sure that oit will never block. */
112
if( fd_urandom == -1 )
113
fd_urandom = open_device( NAME_OF_DEV_URANDOM, 9 );
118
#ifdef HAVE_DEV_RANDOM_IOCTL
119
g10_log_info("entropy count of %d is %lu\n", fd, get_entropy_count(fd) );
131
if( !(rc=select(fd+1, &rfds, NULL, NULL, &tv)) ) {
135
"Not enough random bytes available. Please do some other work to give\n"
136
"the OS a chance to collect more entropy! (Need %d more bytes)\n"), (int)length );
140
else if( rc == -1 ) {
142
"select() error: %s\n", strerror(errno));
147
int nbytes = length < sizeof(buffer)? length : sizeof(buffer);
148
n = read(fd, buffer, nbytes );
149
if( n >= 0 && n > nbytes ) {
150
g10_log_error("bogus read from random device (n=%d)\n", n );
153
} while( n == -1 && errno == EINTR );
155
g10_log_fatal("read error on random device: %s\n", strerror(errno));
156
(*add)( buffer, n, requester );
159
memset(buffer, 0, sizeof(buffer) );
161
return 0; /* success */
164
#endif /*USE_RNDLINUX*/