1 /* rndlinux.c - raw random number for OSes with /dev/random
2 * Copyright (C) 1998, 2001, 2002 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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
28 #include <sys/types.h>
30 #ifdef HAVE_GETTIMEOFDAY
31 #include <sys/times.h>
37 #ifdef HAVE_LINUX_RANDOM_H
38 #include <sys/ioctl.h>
39 #include <asm/types.h>
40 #include <linux/random.h>
47 static int open_device( const char *name, int minor );
48 static int gather_random( void (*add)(const void*, size_t, int), int requester,
49 size_t length, int level );
52 #ifdef HAVE_DEV_RANDOM_IOCTL
54 get_entropy_count( int fd )
58 if( ioctl( fd, RNDGETENTCNT, &count ) == -1 )
59 log_fatal("ioctl(RNDGETENTCNT) failed: %s\n", strerror(errno) );
66 * Used to open the /dev/random devices (Linux, xBSD, Solaris (if it exists), ...)
69 open_device( const char *name, int minor )
74 fd = open( name, O_RDONLY );
76 log_fatal("can't open %s: %s\n", name, strerror(errno) );
77 if( fstat( fd, &sb ) )
78 log_fatal("stat() off %s failed: %s\n", name, strerror(errno) );
79 /* Don't check device type for better portability */
80 /* if( (!S_ISCHR(sb.st_mode)) && (!S_ISFIFO(sb.st_mode)) )
81 log_fatal("invalid random device!\n" ); */
87 gather_random( void (*add)(const void*, size_t, int), int requester,
88 size_t length, int level )
90 static int fd_urandom = -1;
91 static int fd_random = -1;
99 fd_random = open_device( NAME_OF_DEV_RANDOM, 8 );
103 if( fd_urandom == -1 )
104 fd_urandom = open_device( NAME_OF_DEV_URANDOM, 9 );
109 #ifdef HAVE_DEV_RANDOM_IOCTL
110 log_info("entropy count of %d is %lu\n", fd, get_entropy_count(fd) );
122 if( !(rc=select(fd+1, &rfds, NULL, NULL, &tv)) ) {
125 log_info (_("not enough random bytes available (need %d bytes)\n"),
127 log_info (_("please do some other work to give the OS a chance to collect more entropy\n"));
132 else if( rc == -1 ) {
133 log_error ("select() error: %s\n", strerror(errno));
138 int nbytes = length < sizeof(buffer)? length : sizeof(buffer);
139 n = read(fd, buffer, nbytes );
140 if( n >= 0 && n > nbytes ) {
141 log_error("bogus read from random device (n=%d)\n", n );
144 } while( n == -1 && errno == EINTR );
146 log_fatal("read error on random device: %s\n", strerror(errno));
147 (*add)( buffer, n, requester );
150 memset(buffer, 0, sizeof(buffer) );
152 return 0; /* success */
160 const char * const gnupgext_version = "RNDLINUX ($Revision$)";
167 { 40, 1, gather_random },
173 * Enumerate the names of the functions together with informations about
174 * this function. Set sequence to an integer with a initial value of 0 and
176 * If what is 0 all kind of functions are returned.
177 * Return values: class := class of function:
178 * 10 = message digest algorithm info function
179 * 11 = integer with available md algorithms
180 * 20 = cipher algorithm info function
181 * 21 = integer with available cipher algorithms
182 * 30 = public key algorithm info function
183 * 31 = integer with available pubkey algorithms
184 * 40 = get gather_random function
185 * 41 = get fast_random_poll function
186 * version = interface version of the function/pointer
187 * (currently this is 1 for all functions)
194 gnupgext_enum_func( int what, int *sequence, int *class, int *vers )
200 if ( i >= DIM(func_table) || i < 0 ) {
203 *class = func_table[i].class;
204 *vers = func_table[i].version;
205 ret = func_table[i].func;
207 } while ( what && what != *class );
215 _gcry_rndlinux_constructor(void)
217 _gcry_register_internal_cipher_extension( gnupgext_version,
218 gnupgext_enum_func );