1
/* rndlinux.c - raw random number for OSes with /dev/random
2
* Copyright (C) 1998, 2001, 2002, 2003, 2007 Free Software Foundation, Inc.
4
* This file is part of Libgcrypt.
6
* Libgcrypt is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU Lesser General Public License as
8
* published by the Free Software Foundation; either version 2.1 of
9
* the License, or (at your option) any later version.
11
* Libgcrypt 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 Lesser General Public License for more details.
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
26
#include <sys/types.h>
28
#ifdef HAVE_GETTIMEOFDAY
29
# include <sys/times.h>
36
#include "rand-internal.h"
38
static int open_device ( const char *name );
42
set_cloexec_flag (int fd)
46
oldflags= fcntl (fd, F_GETFD, 0);
49
oldflags |= FD_CLOEXEC;
50
return fcntl (fd, F_SETFD, oldflags);
56
* Used to open the /dev/random devices (Linux, xBSD, Solaris (if it exists)).
59
open_device ( const char *name )
63
fd = open ( name, O_RDONLY );
65
log_fatal ("can't open %s: %s\n", name, strerror(errno) );
67
if (set_cloexec_flag (fd))
68
log_error ("error setting FD_CLOEXEC on fd %d: %s\n",
69
fd, strerror (errno));
71
/* We used to do the following check, however it turned out that this
72
is not portable since more OSes provide a random device which is
73
sometimes implemented as another device type.
77
if( fstat( fd, &sb ) )
78
log_fatal("stat() off %s failed: %s\n", name, strerror(errno) );
79
if( (!S_ISCHR(sb.st_mode)) && (!S_ISFIFO(sb.st_mode)) )
80
log_fatal("invalid random device!\n" );
87
_gcry_rndlinux_gather_random (void (*add)(const void*, size_t,
89
enum random_origins origin,
90
size_t length, int level )
92
static int fd_urandom = -1;
93
static int fd_random = -1;
100
/* First read from a hardware source. However let it account only
101
for up to 50% of the requested bytes. */
102
n_hw = _gcry_rndhw_poll_slow (add, origin);
108
/* Open the requested device. */
111
if( fd_random == -1 )
112
fd_random = open_device ( NAME_OF_DEV_RANDOM );
117
if( fd_urandom == -1 )
118
fd_urandom = open_device ( NAME_OF_DEV_URANDOM );
122
/* And enter the read loop. */
133
if( !(rc=select(fd+1, &rfds, NULL, NULL, &tv)) )
137
_gcry_random_progress ("need_entropy", 'X', 0, (int)length);
144
log_error ("select() error: %s\n", strerror(errno));
150
int nbytes = length < sizeof(buffer)? length : sizeof(buffer);
151
n = read(fd, buffer, nbytes );
152
if( n >= 0 && n > nbytes )
154
log_error("bogus read from random device (n=%d)\n", n );
158
while( n == -1 && errno == EINTR );
160
log_fatal("read error on random device: %s\n", strerror(errno));
161
(*add)( buffer, n, origin );
164
memset(buffer, 0, sizeof(buffer) );
166
return 0; /* success */