|
Lines 78-83
emulate_getrandom_buf (
Link Here
|
| 78 |
} |
78 |
} |
| 79 |
} |
79 |
} |
| 80 |
|
80 |
|
|
|
81 |
#ifndef _GNU_SOURCE |
| 82 |
#define _GNU_SOURCE // needed on SLE11 for O_CLOEXEC |
| 83 |
#endif |
| 84 |
#include <sys/types.h> |
| 85 |
#include <sys/stat.h> |
| 86 |
#include <fcntl.h> |
| 87 |
#include <errno.h> |
| 88 |
#include <linux/random.h> |
| 89 |
#include <sys/syscall.h> |
| 90 |
|
| 91 |
int getentropy_urandom(void *buffer, size_t length) |
| 92 |
{ |
| 93 |
int random_fd = -1; |
| 94 |
ssize_t res = -1; |
| 95 |
size_t filled = 0; |
| 96 |
|
| 97 |
if( length > 256 ) |
| 98 |
{ |
| 99 |
errno = EIO; |
| 100 |
return -1; |
| 101 |
} |
| 102 |
|
| 103 |
random_fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC); |
| 104 |
|
| 105 |
if( random_fd == -1 ) |
| 106 |
{ |
| 107 |
return -1; |
| 108 |
} |
| 109 |
|
| 110 |
while( filled < length ) |
| 111 |
{ |
| 112 |
res = read(random_fd, (char*)buffer + filled, length - filled); |
| 113 |
|
| 114 |
if( res == -1 ) |
| 115 |
{ |
| 116 |
// shouldn't actually happen acc. to man(4) random, |
| 117 |
// but you never know |
| 118 |
if( errno == EINTR ) |
| 119 |
continue; |
| 120 |
|
| 121 |
return -1; |
| 122 |
} |
| 123 |
else if( res == 0 ) |
| 124 |
{ |
| 125 |
// no more bytes available? should not happen |
| 126 |
errno = EIO; |
| 127 |
return -1; |
| 128 |
} |
| 129 |
|
| 130 |
filled += res; |
| 131 |
} |
| 132 |
|
| 133 |
return 0; |
| 134 |
} |
| 135 |
|
| 136 |
int getentropy_getrandom(void *buffer, size_t length) |
| 137 |
{ |
| 138 |
int res; |
| 139 |
size_t filled = 0; |
| 140 |
|
| 141 |
if( length > 256 ) |
| 142 |
{ |
| 143 |
errno = EIO; |
| 144 |
return -1; |
| 145 |
} |
| 146 |
|
| 147 |
while( filled < length ) |
| 148 |
{ |
| 149 |
#ifdef SYS_getrandom |
| 150 |
/* |
| 151 |
* glibc does not contain a syscall wrapper for this in older |
| 152 |
* versions |
| 153 |
*/ |
| 154 |
res = syscall(SYS_getrandom, (char*)buffer + filled, length - filled, 0); |
| 155 |
#else |
| 156 |
# warning no getrandom |
| 157 |
errno = ENOSYS; |
| 158 |
return -1; |
| 159 |
#endif // SYS_getrandom |
| 160 |
|
| 161 |
if( res == -1 ) |
| 162 |
{ |
| 163 |
if( errno == EINTR ) |
| 164 |
continue; |
| 165 |
|
| 166 |
return -1; |
| 167 |
} |
| 168 |
else if( res == 0 ) |
| 169 |
{ |
| 170 |
// no more bytes available? should not happen |
| 171 |
errno = EIO; |
| 172 |
return -1; |
| 173 |
} |
| 174 |
|
| 175 |
filled += res; |
| 176 |
} |
| 177 |
|
| 178 |
return 0; |
| 179 |
} |
| 180 |
|
| 181 |
int getentropy_emulate(void *buffer, size_t length) |
| 182 |
{ |
| 183 |
/* |
| 184 |
* check at runtime whether we have a getrandom system call available, |
| 185 |
* otherwise fall back to urandom approach. autoconf check for |
| 186 |
* getrandom() does not work, because there's been no declaration for |
| 187 |
* it for years. |
| 188 |
*/ |
| 189 |
int res = getentropy_getrandom(buffer, length); |
| 190 |
|
| 191 |
if( res == -1 && errno == ENOSYS ) |
| 192 |
{ |
| 193 |
return getentropy_urandom(buffer, length); |
| 194 |
} |
| 195 |
|
| 196 |
return res; |
| 197 |
} |
| 198 |
|
| 81 |
static void |
199 |
static void |
| 82 |
arc4random_buf ( |
200 |
arc4random_buf ( |
| 83 |
char *auth, |
201 |
char *auth, |
|
Lines 89-97
arc4random_buf (
Link Here
|
| 89 |
#if HAVE_GETENTROPY |
207 |
#if HAVE_GETENTROPY |
| 90 |
/* weak emulation of arc4random through the entropy libc */ |
208 |
/* weak emulation of arc4random through the entropy libc */ |
| 91 |
ret = getentropy (auth, len); |
209 |
ret = getentropy (auth, len); |
|
|
210 |
#else |
| 211 |
ret = getentropy_emulate (auth, len); |
| 212 |
#endif /* HAVE_GETENTROPY */ |
| 92 |
if (ret == 0) |
213 |
if (ret == 0) |
| 93 |
return; |
214 |
return; |
| 94 |
#endif /* HAVE_GETENTROPY */ |
|
|
| 95 |
|
215 |
|
| 96 |
emulate_getrandom_buf (auth, len); |
216 |
emulate_getrandom_buf (auth, len); |
| 97 |
} |
217 |
} |