|
Lines 40-45
Author: Ralph Mor, X Consortium
Link Here
|
| 40 |
#include <bsd/stdlib.h> /* for arc4random_buf() */ |
40 |
#include <bsd/stdlib.h> /* for arc4random_buf() */ |
| 41 |
#endif |
41 |
#endif |
| 42 |
|
42 |
|
|
|
43 |
#ifdef HAVE_GETRANDOM |
| 44 |
#include <linux/random.h> |
| 45 |
#include <sys/syscall.h> |
| 46 |
#endif |
| 47 |
|
| 48 |
#include <sys/types.h> |
| 49 |
#include <sys/stat.h> |
| 50 |
#include <fcntl.h> |
| 51 |
#include <errno.h> |
| 52 |
|
| 43 |
static int was_called_state; |
53 |
static int was_called_state; |
| 44 |
|
54 |
|
| 45 |
#ifndef HAVE_ARC4RANDOM_BUF |
55 |
#ifndef HAVE_ARC4RANDOM_BUF |
|
Lines 78-97
emulate_getrandom_buf (
Link Here
|
| 78 |
} |
88 |
} |
| 79 |
} |
89 |
} |
| 80 |
|
90 |
|
|
|
91 |
#ifdef HAVE_GETRANDOM |
| 92 |
static int |
| 93 |
emulate_getentropy_getrandom |
| 94 |
( |
| 95 |
void *buffer, |
| 96 |
size_t length |
| 97 |
) |
| 98 |
{ |
| 99 |
int res; |
| 100 |
size_t filled = 0; |
| 101 |
|
| 102 |
if( length > 256 ) { |
| 103 |
errno = EIO; |
| 104 |
return -1; |
| 105 |
} |
| 106 |
|
| 107 |
while( filled < length ) { |
| 108 |
/* |
| 109 |
* glibc does not contain a syscall wrapper for this in older |
| 110 |
* versions |
| 111 |
*/ |
| 112 |
res = syscall(SYS_getrandom, (char*)buffer + filled, length - filled, 0); |
| 113 |
|
| 114 |
if( res == -1 ) { |
| 115 |
if( errno == EINTR ) |
| 116 |
continue; |
| 117 |
|
| 118 |
return -1; |
| 119 |
} else if ( res == 0 ) { |
| 120 |
// no more bytes available? should not happen |
| 121 |
errno = EIO; |
| 122 |
return -1; |
| 123 |
} |
| 124 |
|
| 125 |
filled += res; |
| 126 |
} |
| 127 |
|
| 128 |
return 0; |
| 129 |
} |
| 130 |
#endif |
| 131 |
|
| 132 |
static int emulate_getentropy_devurandom |
| 133 |
( |
| 134 |
void *buffer, |
| 135 |
size_t length |
| 136 |
) |
| 137 |
{ |
| 138 |
int random_fd = -1; |
| 139 |
ssize_t res = -1; |
| 140 |
size_t filled = 0; |
| 141 |
|
| 142 |
if( length > 256 ) { |
| 143 |
errno = EIO; |
| 144 |
return -1; |
| 145 |
} |
| 146 |
|
| 147 |
random_fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC); |
| 148 |
|
| 149 |
if( random_fd == -1 ) { |
| 150 |
return -1; |
| 151 |
} |
| 152 |
|
| 153 |
while( filled < length ) { |
| 154 |
res = read(random_fd, (char*)buffer + filled, length - filled); |
| 155 |
|
| 156 |
if( res == -1 ) { |
| 157 |
// shouldn't actually happen acc. to man(4) random, |
| 158 |
// but you never know |
| 159 |
if( errno == EINTR ) |
| 160 |
continue; |
| 161 |
|
| 162 |
return -1; |
| 163 |
} else if( res == 0 ) { |
| 164 |
// no more bytes available? should not happen |
| 165 |
errno = EIO; |
| 166 |
return -1; |
| 167 |
} |
| 168 |
|
| 169 |
filled += res; |
| 170 |
} |
| 171 |
|
| 172 |
return 0; |
| 173 |
} |
| 174 |
|
| 81 |
static void |
175 |
static void |
| 82 |
arc4random_buf ( |
176 |
arc4random_buf ( |
| 83 |
char *auth, |
177 |
char *auth, |
| 84 |
int len |
178 |
int len |
| 85 |
) |
179 |
) |
| 86 |
{ |
180 |
{ |
| 87 |
int ret; |
181 |
int ret = -1; |
| 88 |
|
182 |
|
| 89 |
#if HAVE_GETENTROPY |
183 |
#if HAVE_GETENTROPY |
| 90 |
/* weak emulation of arc4random through the entropy libc */ |
184 |
/* weak emulation of arc4random through the entropy libc */ |
| 91 |
ret = getentropy (auth, len); |
185 |
ret = getentropy (auth, len); |
|
|
186 |
#elif HAVE_GETRANDOM |
| 187 |
/* drop-in replacement for getentropy() via getrandrom() */ |
| 188 |
ret = emulate_getentropy_getrandom (auth, len); |
| 189 |
#else |
| 190 |
/* drop-in replacement for getentropy() using /dev/urandom */ |
| 191 |
ret = emulate_getentropy_devurandom(auth, len); |
| 192 |
#endif /* HAVE_GETENTROPY */ |
| 92 |
if (ret == 0) |
193 |
if (ret == 0) |
| 93 |
return; |
194 |
return; |
| 94 |
#endif /* HAVE_GETENTROPY */ |
|
|
| 95 |
|
195 |
|
| 96 |
emulate_getrandom_buf (auth, len); |
196 |
emulate_getrandom_buf (auth, len); |
| 97 |
} |
197 |
} |