|
49 | 49 |
|
50 | 50 | #if HAVE_SYS_PARAM_H
|
51 | 51 | # include <sys/param.h>
|
52 |
| -# if (__FreeBSD__ && __FreeBSD_version > 1200000) || (__DragonFly__ && __DragonFly_version >= 500700) || defined(__sun) |
| 52 | +# if (__FreeBSD__ && __FreeBSD_version > 1200000) || (__DragonFly__ && __DragonFly_version >= 500700) || \ |
| 53 | + defined(__sun) || (defined(__NetBSD__) && __NetBSD_Version__ >= 1000000000) |
53 | 54 | # include <sys/random.h>
|
54 | 55 | # endif
|
55 | 56 | #endif
|
@@ -502,14 +503,27 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw)
|
502 | 503 | }
|
503 | 504 | return FAILURE;
|
504 | 505 | }
|
505 |
| -#elif HAVE_DECL_ARC4RANDOM_BUF && ((defined(__OpenBSD__) && OpenBSD >= 201405) || (defined(__NetBSD__) && __NetBSD_Version__ >= 700000001) || defined(__APPLE__)) |
| 506 | +#elif HAVE_DECL_ARC4RANDOM_BUF && ((defined(__OpenBSD__) && OpenBSD >= 201405) || (defined(__NetBSD__) && __NetBSD_Version__ >= 700000001 && __NetBSD_Version__ < 1000000000) || \ |
| 507 | + defined(__APPLE__) || defined(__GLIBC__)) |
| 508 | + /* |
| 509 | + * OpenBSD until there is a valid equivalent |
| 510 | + * or NetBSD before the 10.x release |
| 511 | + * falls back to arc4random_buf |
| 512 | + * giving a decent output, the main benefit |
| 513 | + * is being (relatively) failsafe. |
| 514 | + * Older macOs releases fall also into this |
| 515 | + * category for reasons explained above. |
| 516 | + */ |
506 | 517 | arc4random_buf(bytes, size);
|
507 | 518 | #else
|
508 | 519 | size_t read_bytes = 0;
|
509 | 520 | ssize_t n;
|
510 |
| -# if (defined(__linux__) && defined(SYS_getrandom)) || (defined(__FreeBSD__) && __FreeBSD_version >= 1200000) || (defined(__DragonFly__) && __DragonFly_version >= 500700) || defined(__sun) |
511 |
| - /* Linux getrandom(2) syscall or FreeBSD/DragonFlyBSD getrandom(2) function*/ |
512 |
| - /* Keep reading until we get enough entropy */ |
| 521 | +# if (defined(__linux__) && defined(SYS_getrandom)) || (defined(__FreeBSD__) && __FreeBSD_version >= 1200000) || (defined(__DragonFly__) && __DragonFly_version >= 500700) || \ |
| 522 | + defined(__sun) || (defined(__NetBSD__) && __NetBSD_Version__ >= 1000000000) |
| 523 | + /* Linux getrandom(2) syscall or FreeBSD/DragonFlyBSD/NetBSD getrandom(2) function |
| 524 | + * Being a syscall, implemented in the kernel, getrandom offers higher quality output |
| 525 | + * compared to the arc4random api albeit a fallback to /dev/urandom is considered. |
| 526 | + */ |
513 | 527 | while (read_bytes < size) {
|
514 | 528 | errno = 0;
|
515 | 529 |
|
|
0 commit comments