1 /* rndlinux.c - raw random number for OSes with /dev/random
2 * Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3 *
4 * This file is part of GnuPG.
5 *
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 3 of the License, or
9 * (at your option) any later version.
10 *
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.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <assert.h>
25 #include <errno.h>
26 #include <sys/time.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #ifdef HAVE_GETTIMEOFDAY
30 #include <sys/times.h>
31 #endif
32 #include <string.h>
33 #include <unistd.h>
34 #include <fcntl.h>
35 #if 0
36 #include <sys/ioctl.h>
37 #include <asm/types.h>
38 #include <linux/random.h>
39 #endif
40 #include "types.h"
41 #include "util.h"
42 #include "ttyio.h"
43 #include "algorithms.h"
44
45 #include "i18n.h"
46
47 static int open_device( const char *name, int minor );
48
49
50 #if 0
51 #ifdef HAVE_DEV_RANDOM_IOCTL
52 static ulong
53 get_entropy_count( int fd )
54 {
55 ulong count;
56
57 if( ioctl( fd, RNDGETENTCNT, &count ) == -1 )
58 g10_log_fatal("ioctl(RNDGETENTCNT) failed: %s\n", strerror(errno) );
59 return count;
60 }
61 #endif
62 #endif
63
64 /*
65 * Used to open the /dev/random devices (Linux, xBSD, Solaris (if it
66 * exists), ...)
67 */
68 static int
open_device(const char * name,int minor)69 open_device( const char *name, int minor )
70 {
71 int fd;
72 struct stat sb;
73
74 fd = open( name, O_RDONLY );
75 if( fd == -1 )
76 g10_log_fatal("can't open %s: %s\n", name, strerror(errno) );
77 if( fstat( fd, &sb ) )
78 g10_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 g10_log_fatal("invalid random device!\n" ); */
82 return fd;
83 }
84
85
86 /*
87 * Note: Using a level of 0 should never block and better add nothing
88 * to the pool. This is easy to accomplish with /dev/urandom.
89 */
90 int
rndlinux_gather_random(void (* add)(const void *,size_t,int),int requester,size_t length,int level)91 rndlinux_gather_random( void (*add)(const void*, size_t, int), int requester,
92 size_t length, int level )
93 {
94 static int fd_urandom = -1;
95 static int fd_random = -1;
96 int fd;
97 int n;
98 int warn=0;
99 byte buffer[768];
100
101 if( level >= 2 ) {
102 if( fd_random == -1 )
103 fd_random = open_device( NAME_OF_DEV_RANDOM, 8 );
104 fd = fd_random;
105 }
106 else {
107 /* This will also be used for level 0. By using /dev/urandom
108 * we can be sure that it will never block. */
109 if( fd_urandom == -1 )
110 fd_urandom = open_device( NAME_OF_DEV_URANDOM, 9 );
111 fd = fd_urandom;
112 }
113
114 #if 0
115 #ifdef HAVE_DEV_RANDOM_IOCTL
116 g10_log_info("entropy count of %d is %lu\n", fd, get_entropy_count(fd) );
117 #endif
118 #endif
119 while( length ) {
120 #ifdef FD_SETSIZE
121 fd_set rfds;
122 struct timeval tv;
123 int rc;
124
125 FD_ZERO(&rfds);
126 tv.tv_sec = 3;
127 tv.tv_usec = 0;
128 if (fd < FD_SETSIZE)
129 {
130 FD_SET(fd, &rfds);
131 if( !(rc=select(fd+1, &rfds, NULL, NULL, &tv)) ) {
132 if( !warn )
133 tty_printf(
134 _("\n"
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 );
137 warn = 1;
138 continue;
139 }
140 else if( rc == -1 ) {
141 tty_printf(
142 "select() error: %s\n", strerror(errno));
143 continue;
144 }
145 }
146 #endif /*FD_SETSIZE*/
147
148 do {
149 int nbytes = length < sizeof(buffer)? length : sizeof(buffer);
150 n = read(fd, buffer, nbytes );
151 if( n >= 0 && n > nbytes ) {
152 g10_log_error("bogus read from random device (n=%d)\n", n );
153 n = nbytes;
154 }
155 } while( n == -1 && errno == EINTR );
156 if( n == -1 )
157 g10_log_fatal("read error on random device: %s\n", strerror(errno));
158 (*add)( buffer, n, requester );
159 length -= n;
160 }
161 wipememory(buffer, sizeof(buffer) );
162
163 return 0; /* success */
164 }
165