diff options
| author | beck <> | 2016-09-03 11:33:43 +0000 |
|---|---|---|
| committer | beck <> | 2016-09-03 11:33:43 +0000 |
| commit | bfb40cbd29e3a70915d1708f9660744ebfb3c110 (patch) | |
| tree | bda3f96ee18135da341fa9b72de3608609a4f2b7 /src/lib/libcrypto/crypto/getentropy_aix.c | |
| parent | 792b1b7757ae0eebdddba7a0b8601e5c0ae6c579 (diff) | |
| download | openbsd-bfb40cbd29e3a70915d1708f9660744ebfb3c110.tar.gz openbsd-bfb40cbd29e3a70915d1708f9660744ebfb3c110.tar.bz2 openbsd-bfb40cbd29e3a70915d1708f9660744ebfb3c110.zip | |
Remove the libcrypto/crypto directory
Diffstat (limited to 'src/lib/libcrypto/crypto/getentropy_aix.c')
| -rw-r--r-- | src/lib/libcrypto/crypto/getentropy_aix.c | 425 |
1 files changed, 0 insertions, 425 deletions
diff --git a/src/lib/libcrypto/crypto/getentropy_aix.c b/src/lib/libcrypto/crypto/getentropy_aix.c deleted file mode 100644 index ff48ae7071..0000000000 --- a/src/lib/libcrypto/crypto/getentropy_aix.c +++ /dev/null | |||
| @@ -1,425 +0,0 @@ | |||
| 1 | /* $OpenBSD: getentropy_aix.c,v 1.5 2016/08/07 03:27:21 tb Exp $ */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * Copyright (c) 2015 Michael Felt <aixtools@gmail.com> | ||
| 5 | * Copyright (c) 2014 Theo de Raadt <deraadt@openbsd.org> | ||
| 6 | * Copyright (c) 2014 Bob Beck <beck@obtuse.com> | ||
| 7 | * | ||
| 8 | * Permission to use, copy, modify, and distribute this software for any | ||
| 9 | * purpose with or without fee is hereby granted, provided that the above | ||
| 10 | * copyright notice and this permission notice appear in all copies. | ||
| 11 | * | ||
| 12 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
| 13 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
| 14 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
| 15 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
| 16 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
| 17 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
| 18 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
| 19 | * | ||
| 20 | * Emulation of getentropy(2) as documented at: | ||
| 21 | * http://man.openbsd.org/getentropy.2 | ||
| 22 | */ | ||
| 23 | /* | ||
| 24 | * -lperfstat is needed for the psuedo entropy data | ||
| 25 | */ | ||
| 26 | |||
| 27 | #include <sys/mman.h> | ||
| 28 | #include <sys/procfs.h> | ||
| 29 | #include <sys/protosw.h> | ||
| 30 | #include <sys/resource.h> | ||
| 31 | #include <sys/socket.h> | ||
| 32 | #include <sys/stat.h> | ||
| 33 | #include <sys/statvfs.h> | ||
| 34 | #include <sys/timers.h> | ||
| 35 | #include <errno.h> | ||
| 36 | #include <fcntl.h> | ||
| 37 | #include <signal.h> | ||
| 38 | #include <stdio.h> | ||
| 39 | #include <string.h> | ||
| 40 | #include <termios.h> | ||
| 41 | |||
| 42 | #include <openssl/sha.h> | ||
| 43 | |||
| 44 | #include <libperfstat.h> | ||
| 45 | |||
| 46 | #define REPEAT 5 | ||
| 47 | #define min(a, b) (((a) < (b)) ? (a) : (b)) | ||
| 48 | |||
| 49 | #define HX(a, b) \ | ||
| 50 | do { \ | ||
| 51 | if ((a)) \ | ||
| 52 | HD(errno); \ | ||
| 53 | else \ | ||
| 54 | HD(b); \ | ||
| 55 | } while (0) | ||
| 56 | |||
| 57 | #define HR(x, l) (SHA512_Update(&ctx, (char *)(x), (l))) | ||
| 58 | #define HD(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (x))) | ||
| 59 | #define HF(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (void*))) | ||
| 60 | |||
| 61 | int getentropy(void *buf, size_t len); | ||
| 62 | |||
| 63 | static int gotdata(char *buf, size_t len); | ||
| 64 | static int getentropy_urandom(void *buf, size_t len, const char *path, | ||
| 65 | int devfscheck); | ||
| 66 | static int getentropy_fallback(void *buf, size_t len); | ||
| 67 | |||
| 68 | int | ||
| 69 | getentropy(void *buf, size_t len) | ||
| 70 | { | ||
| 71 | int ret = -1; | ||
| 72 | |||
| 73 | if (len > 256) { | ||
| 74 | errno = EIO; | ||
| 75 | return (-1); | ||
| 76 | } | ||
| 77 | |||
| 78 | /* | ||
| 79 | * Try to get entropy with /dev/urandom | ||
| 80 | */ | ||
| 81 | ret = getentropy_urandom(buf, len, "/dev/urandom", 0); | ||
| 82 | if (ret != -1) | ||
| 83 | return (ret); | ||
| 84 | |||
| 85 | /* | ||
| 86 | * Entropy collection via /dev/urandom has failed. | ||
| 87 | * | ||
| 88 | * No other API exists for collecting entropy, and we have | ||
| 89 | * no failsafe way to get it on AIX that is not sensitive | ||
| 90 | * to resource exhaustion. | ||
| 91 | * | ||
| 92 | * We have very few options: | ||
| 93 | * - Even syslog_r is unsafe to call at this low level, so | ||
| 94 | * there is no way to alert the user or program. | ||
| 95 | * - Cannot call abort() because some systems have unsafe | ||
| 96 | * corefiles. | ||
| 97 | * - Could raise(SIGKILL) resulting in silent program termination. | ||
| 98 | * - Return EIO, to hint that arc4random's stir function | ||
| 99 | * should raise(SIGKILL) | ||
| 100 | * - Do the best under the circumstances.... | ||
| 101 | * | ||
| 102 | * This code path exists to bring light to the issue that AIX | ||
| 103 | * does not provide a failsafe API for entropy collection. | ||
| 104 | * | ||
| 105 | * We hope this demonstrates that AIX should consider | ||
| 106 | * providing a new failsafe API which works in a chroot or | ||
| 107 | * when file descriptors are exhausted. | ||
| 108 | */ | ||
| 109 | #undef FAIL_INSTEAD_OF_TRYING_FALLBACK | ||
| 110 | #ifdef FAIL_INSTEAD_OF_TRYING_FALLBACK | ||
| 111 | raise(SIGKILL); | ||
| 112 | #endif | ||
| 113 | ret = getentropy_fallback(buf, len); | ||
| 114 | if (ret != -1) | ||
| 115 | return (ret); | ||
| 116 | |||
| 117 | errno = EIO; | ||
| 118 | return (ret); | ||
| 119 | } | ||
| 120 | |||
| 121 | /* | ||
| 122 | * Basic sanity checking; wish we could do better. | ||
| 123 | */ | ||
| 124 | static int | ||
| 125 | gotdata(char *buf, size_t len) | ||
| 126 | { | ||
| 127 | char any_set = 0; | ||
| 128 | size_t i; | ||
| 129 | |||
| 130 | for (i = 0; i < len; ++i) | ||
| 131 | any_set |= buf[i]; | ||
| 132 | if (any_set == 0) | ||
| 133 | return (-1); | ||
| 134 | return (0); | ||
| 135 | } | ||
| 136 | |||
| 137 | static int | ||
| 138 | getentropy_urandom(void *buf, size_t len, const char *path, int devfscheck) | ||
| 139 | { | ||
| 140 | struct stat st; | ||
| 141 | size_t i; | ||
| 142 | int fd, flags; | ||
| 143 | int save_errno = errno; | ||
| 144 | |||
| 145 | start: | ||
| 146 | |||
| 147 | flags = O_RDONLY; | ||
| 148 | #ifdef O_NOFOLLOW | ||
| 149 | flags |= O_NOFOLLOW; | ||
| 150 | #endif | ||
| 151 | #ifdef O_CLOEXEC | ||
| 152 | flags |= O_CLOEXEC; | ||
| 153 | #endif | ||
| 154 | fd = open(path, flags, 0); | ||
| 155 | if (fd == -1) { | ||
| 156 | if (errno == EINTR) | ||
| 157 | goto start; | ||
| 158 | goto nodevrandom; | ||
| 159 | } | ||
| 160 | #ifndef O_CLOEXEC | ||
| 161 | fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC); | ||
| 162 | #endif | ||
| 163 | |||
| 164 | /* Lightly verify that the device node looks sane */ | ||
| 165 | if (fstat(fd, &st) == -1 || !S_ISCHR(st.st_mode)) { | ||
| 166 | close(fd); | ||
| 167 | goto nodevrandom; | ||
| 168 | } | ||
| 169 | for (i = 0; i < len; ) { | ||
| 170 | size_t wanted = len - i; | ||
| 171 | ssize_t ret = read(fd, (char *)buf + i, wanted); | ||
| 172 | |||
| 173 | if (ret == -1) { | ||
| 174 | if (errno == EAGAIN || errno == EINTR) | ||
| 175 | continue; | ||
| 176 | close(fd); | ||
| 177 | goto nodevrandom; | ||
| 178 | } | ||
| 179 | i += ret; | ||
| 180 | } | ||
| 181 | close(fd); | ||
| 182 | if (gotdata(buf, len) == 0) { | ||
| 183 | errno = save_errno; | ||
| 184 | return (0); /* satisfied */ | ||
| 185 | } | ||
| 186 | nodevrandom: | ||
| 187 | errno = EIO; | ||
| 188 | return (-1); | ||
| 189 | } | ||
| 190 | |||
| 191 | static const int cl[] = { | ||
| 192 | CLOCK_REALTIME, | ||
| 193 | #ifdef CLOCK_MONOTONIC | ||
| 194 | CLOCK_MONOTONIC, | ||
| 195 | #endif | ||
| 196 | #ifdef CLOCK_MONOTONIC_RAW | ||
| 197 | CLOCK_MONOTONIC_RAW, | ||
| 198 | #endif | ||
| 199 | #ifdef CLOCK_TAI | ||
| 200 | CLOCK_TAI, | ||
| 201 | #endif | ||
| 202 | #ifdef CLOCK_VIRTUAL | ||
| 203 | CLOCK_VIRTUAL, | ||
| 204 | #endif | ||
| 205 | #ifdef CLOCK_UPTIME | ||
| 206 | CLOCK_UPTIME, | ||
| 207 | #endif | ||
| 208 | #ifdef CLOCK_PROCESS_CPUTIME_ID | ||
| 209 | CLOCK_PROCESS_CPUTIME_ID, | ||
| 210 | #endif | ||
| 211 | #ifdef CLOCK_THREAD_CPUTIME_ID | ||
| 212 | CLOCK_THREAD_CPUTIME_ID, | ||
| 213 | #endif | ||
| 214 | }; | ||
| 215 | |||
| 216 | static int | ||
| 217 | getentropy_fallback(void *buf, size_t len) | ||
| 218 | { | ||
| 219 | uint8_t results[SHA512_DIGEST_LENGTH]; | ||
| 220 | int save_errno = errno, e, pgs = sysconf(_SC_PAGESIZE), faster = 0, repeat; | ||
| 221 | static int cnt; | ||
| 222 | struct timespec ts; | ||
| 223 | struct timeval tv; | ||
| 224 | perfstat_cpu_total_t cpustats; | ||
| 225 | #ifdef _AIX61 | ||
| 226 | perfstat_cpu_total_wpar_t cpustats_wpar; | ||
| 227 | #endif | ||
| 228 | perfstat_partition_total_t lparstats; | ||
| 229 | perfstat_disk_total_t diskinfo; | ||
| 230 | perfstat_netinterface_total_t netinfo; | ||
| 231 | struct rusage ru; | ||
| 232 | sigset_t sigset; | ||
| 233 | struct stat st; | ||
| 234 | SHA512_CTX ctx; | ||
| 235 | static pid_t lastpid; | ||
| 236 | pid_t pid; | ||
| 237 | size_t i, ii, m; | ||
| 238 | char *p; | ||
| 239 | |||
| 240 | pid = getpid(); | ||
| 241 | if (lastpid == pid) { | ||
| 242 | faster = 1; | ||
| 243 | repeat = 2; | ||
| 244 | } else { | ||
| 245 | faster = 0; | ||
| 246 | lastpid = pid; | ||
| 247 | repeat = REPEAT; | ||
| 248 | } | ||
| 249 | for (i = 0; i < len; ) { | ||
| 250 | int j; | ||
| 251 | SHA512_Init(&ctx); | ||
| 252 | for (j = 0; j < repeat; j++) { | ||
| 253 | HX((e = gettimeofday(&tv, NULL)) == -1, tv); | ||
| 254 | if (e != -1) { | ||
| 255 | cnt += (int)tv.tv_sec; | ||
| 256 | cnt += (int)tv.tv_usec; | ||
| 257 | } | ||
| 258 | |||
| 259 | HX(perfstat_cpu_total(NULL, &cpustats, | ||
| 260 | sizeof(cpustats), 1) == -1, cpustats); | ||
| 261 | |||
| 262 | #ifdef _AIX61 | ||
| 263 | HX(perfstat_cpu_total_wpar(NULL, &cpustats_wpar, | ||
| 264 | sizeof(cpustats_wpar), 1) == -1, cpustats_wpar); | ||
| 265 | #endif | ||
| 266 | |||
| 267 | HX(perfstat_partition_total(NULL, &lparstats, | ||
| 268 | sizeof(lparstats), 1) == -1, lparstats); | ||
| 269 | |||
| 270 | HX(perfstat_disk_total(NULL, &diskinfo, | ||
| 271 | sizeof(diskinfo), 1) == -1, diskinfo); | ||
| 272 | |||
| 273 | HX(perfstat_netinterface_total(NULL, &netinfo, | ||
| 274 | sizeof(netinfo), 1) == -1, netinfo); | ||
| 275 | |||
| 276 | for (ii = 0; ii < sizeof(cl)/sizeof(cl[0]); ii++) | ||
| 277 | HX(clock_gettime(cl[ii], &ts) == -1, ts); | ||
| 278 | |||
| 279 | HX((pid = getpid()) == -1, pid); | ||
| 280 | HX((pid = getsid(pid)) == -1, pid); | ||
| 281 | HX((pid = getppid()) == -1, pid); | ||
| 282 | HX((pid = getpgid(0)) == -1, pid); | ||
| 283 | HX((e = getpriority(0, 0)) == -1, e); | ||
| 284 | |||
| 285 | if (!faster) { | ||
| 286 | ts.tv_sec = 0; | ||
| 287 | ts.tv_nsec = 1; | ||
| 288 | (void) nanosleep(&ts, NULL); | ||
| 289 | } | ||
| 290 | |||
| 291 | HX(sigpending(&sigset) == -1, sigset); | ||
| 292 | HX(sigprocmask(SIG_BLOCK, NULL, &sigset) == -1, | ||
| 293 | sigset); | ||
| 294 | |||
| 295 | HF(getentropy); /* an addr in this library */ | ||
| 296 | HF(printf); /* an addr in libc */ | ||
| 297 | p = (char *)&p; | ||
| 298 | HD(p); /* an addr on stack */ | ||
| 299 | p = (char *)&errno; | ||
| 300 | HD(p); /* the addr of errno */ | ||
| 301 | |||
| 302 | if (i == 0) { | ||
| 303 | struct sockaddr_storage ss; | ||
| 304 | struct statvfs stvfs; | ||
| 305 | struct termios tios; | ||
| 306 | socklen_t ssl; | ||
| 307 | off_t off; | ||
| 308 | |||
| 309 | /* | ||
| 310 | * Prime-sized mappings encourage fragmentation; | ||
| 311 | * thus exposing some address entropy. | ||
| 312 | */ | ||
| 313 | struct mm { | ||
| 314 | size_t npg; | ||
| 315 | void *p; | ||
| 316 | } mm[] = { | ||
| 317 | { 17, MAP_FAILED }, { 3, MAP_FAILED }, | ||
| 318 | { 11, MAP_FAILED }, { 2, MAP_FAILED }, | ||
| 319 | { 5, MAP_FAILED }, { 3, MAP_FAILED }, | ||
| 320 | { 7, MAP_FAILED }, { 1, MAP_FAILED }, | ||
| 321 | { 57, MAP_FAILED }, { 3, MAP_FAILED }, | ||
| 322 | { 131, MAP_FAILED }, { 1, MAP_FAILED }, | ||
| 323 | }; | ||
| 324 | |||
| 325 | for (m = 0; m < sizeof mm/sizeof(mm[0]); m++) { | ||
| 326 | HX(mm[m].p = mmap(NULL, | ||
| 327 | mm[m].npg * pgs, | ||
| 328 | PROT_READ|PROT_WRITE, | ||
| 329 | MAP_PRIVATE|MAP_ANON, -1, | ||
| 330 | (off_t)0), mm[m].p); | ||
| 331 | if (mm[m].p != MAP_FAILED) { | ||
| 332 | size_t mo; | ||
| 333 | |||
| 334 | /* Touch some memory... */ | ||
| 335 | p = mm[m].p; | ||
| 336 | mo = cnt % | ||
| 337 | (mm[m].npg * pgs - 1); | ||
| 338 | p[mo] = 1; | ||
| 339 | cnt += (int)((long)(mm[m].p) | ||
| 340 | / pgs); | ||
| 341 | } | ||
| 342 | |||
| 343 | /* Check cnts and times... */ | ||
| 344 | for (ii = 0; ii < sizeof(cl)/sizeof(cl[0]); | ||
| 345 | ii++) { | ||
| 346 | HX((e = clock_gettime(cl[ii], | ||
| 347 | &ts)) == -1, ts); | ||
| 348 | if (e != -1) | ||
| 349 | cnt += (int)ts.tv_nsec; | ||
| 350 | } | ||
| 351 | |||
| 352 | HX((e = getrusage(RUSAGE_SELF, | ||
| 353 | &ru)) == -1, ru); | ||
| 354 | if (e != -1) { | ||
| 355 | cnt += (int)ru.ru_utime.tv_sec; | ||
| 356 | cnt += (int)ru.ru_utime.tv_usec; | ||
| 357 | } | ||
| 358 | } | ||
| 359 | |||
| 360 | for (m = 0; m < sizeof mm/sizeof(mm[0]); m++) { | ||
| 361 | if (mm[m].p != MAP_FAILED) | ||
| 362 | munmap(mm[m].p, mm[m].npg * pgs); | ||
| 363 | mm[m].p = MAP_FAILED; | ||
| 364 | } | ||
| 365 | |||
| 366 | HX(stat(".", &st) == -1, st); | ||
| 367 | HX(statvfs(".", &stvfs) == -1, stvfs); | ||
| 368 | |||
| 369 | HX(stat("/", &st) == -1, st); | ||
| 370 | HX(statvfs("/", &stvfs) == -1, stvfs); | ||
| 371 | |||
| 372 | HX((e = fstat(0, &st)) == -1, st); | ||
| 373 | if (e == -1) { | ||
| 374 | if (S_ISREG(st.st_mode) || | ||
| 375 | S_ISFIFO(st.st_mode) || | ||
| 376 | S_ISSOCK(st.st_mode)) { | ||
| 377 | HX(fstatvfs(0, &stvfs) == -1, | ||
| 378 | stvfs); | ||
| 379 | HX((off = lseek(0, (off_t)0, | ||
| 380 | SEEK_CUR)) < 0, off); | ||
| 381 | } | ||
| 382 | if (S_ISCHR(st.st_mode)) { | ||
| 383 | HX(tcgetattr(0, &tios) == -1, | ||
| 384 | tios); | ||
| 385 | } else if (S_ISSOCK(st.st_mode)) { | ||
| 386 | memset(&ss, 0, sizeof ss); | ||
| 387 | ssl = sizeof(ss); | ||
| 388 | HX(getpeername(0, | ||
| 389 | (void *)&ss, &ssl) == -1, | ||
| 390 | ss); | ||
| 391 | } | ||
| 392 | } | ||
| 393 | |||
| 394 | HX((e = getrusage(RUSAGE_CHILDREN, | ||
| 395 | &ru)) == -1, ru); | ||
| 396 | if (e != -1) { | ||
| 397 | cnt += (int)ru.ru_utime.tv_sec; | ||
| 398 | cnt += (int)ru.ru_utime.tv_usec; | ||
| 399 | } | ||
| 400 | } else { | ||
| 401 | /* Subsequent hashes absorb previous result */ | ||
| 402 | HD(results); | ||
| 403 | } | ||
| 404 | |||
| 405 | HX((e = gettimeofday(&tv, NULL)) == -1, tv); | ||
| 406 | if (e != -1) { | ||
| 407 | cnt += (int)tv.tv_sec; | ||
| 408 | cnt += (int)tv.tv_usec; | ||
| 409 | } | ||
| 410 | |||
| 411 | HD(cnt); | ||
| 412 | } | ||
| 413 | SHA512_Final(results, &ctx); | ||
| 414 | memcpy((char *)buf + i, results, min(sizeof(results), len - i)); | ||
| 415 | i += min(sizeof(results), len - i); | ||
| 416 | } | ||
| 417 | explicit_bzero(&ctx, sizeof ctx); | ||
| 418 | explicit_bzero(results, sizeof results); | ||
| 419 | if (gotdata(buf, len) == 0) { | ||
| 420 | errno = save_errno; | ||
| 421 | return (0); /* satisfied */ | ||
| 422 | } | ||
| 423 | errno = EIO; | ||
| 424 | return (-1); | ||
| 425 | } | ||
