1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-25 09:47:17 +00:00

Switch to using /dev/urandom for OS X prior to 10.7

This commit is contained in:
Jason Pepas 2020-06-30 04:17:01 -05:00
parent f5d208d5d6
commit f06e9ae30c
2 changed files with 9 additions and 24 deletions

View File

@ -58,7 +58,6 @@
/* #define JANET_NO_REALPATH */ /* #define JANET_NO_REALPATH */
/* #define JANET_NO_SYMLINKS */ /* #define JANET_NO_SYMLINKS */
/* #define JANET_NO_UMASK */ /* #define JANET_NO_UMASK */
/* #define JANET_NO_ARC4RANDOM_BUF */
/* #define JANET_OUT_OF_MEMORY do { printf("janet out of memory\n"); exit(1); } while (0) */ /* #define JANET_OUT_OF_MEMORY do { printf("janet out of memory\n"); exit(1); } while (0) */
/* #define JANET_EXIT(msg) do { printf("C assert failed executing janet: %s\n", msg); exit(1); } while (0) */ /* #define JANET_EXIT(msg) do { printf("C assert failed executing janet: %s\n", msg); exit(1); } while (0) */
/* #define JANET_TOP_LEVEL_SIGNAL(msg) call_my_function((msg), stderr) */ /* #define JANET_TOP_LEVEL_SIGNAL(msg) call_my_function((msg), stderr) */

View File

@ -39,6 +39,10 @@
#define RETRY_EINTR(RC, CALL) do { (RC) = CALL; } while((RC) < 0 && errno == EINTR) #define RETRY_EINTR(RC, CALL) do { (RC) = CALL; } while((RC) < 0 && errno == EINTR)
#ifdef JANET_APPLE
#include <AvailabilityMacros.h>
#endif
#ifdef JANET_WINDOWS #ifdef JANET_WINDOWS
#include <windows.h> #include <windows.h>
#include <direct.h> #include <direct.h>
@ -66,29 +70,10 @@ extern char **environ;
/* Setting C99 standard makes this not available, but it should /* Setting C99 standard makes this not available, but it should
* work/link properly if we detect a BSD */ * work/link properly if we detect a BSD */
#if defined(JANET_BSD) || defined(JANET_APPLE) #if defined(JANET_BSD) || defined(MAC_OS_X_VERSION_10_7)
void arc4random_buf(void *buf, size_t nbytes); void arc4random_buf(void *buf, size_t nbytes);
#endif #endif
/* arc4random_buf wasn't available in OS X until 10.7. */
#ifdef JANET_NO_ARC4RANDOM_BUF
uint32_t arc4random(void);
void arc4random_buf(void *buf, size_t nbytes) {
uint32_t *buf_as_words = (uint32_t*)buf;
size_t nwords = nbytes / 4;
for (size_t i=0; i < nwords; i++) {
buf_as_words[i] = arc4random();
}
size_t tail_len = nbytes % 4;
if (tail_len) {
uint8_t *tail = buf + nbytes - tail_len;
uint32_t rand = arc4random();
memcpy(tail, &rand, tail_len);
}
}
#endif
/* Not POSIX, but all Unixes but Solaris have this function. */ /* Not POSIX, but all Unixes but Solaris have this function. */
#if defined(JANET_POSIX) && !defined(__sun) #if defined(JANET_POSIX) && !defined(__sun)
time_t timegm(struct tm *tm); time_t timegm(struct tm *tm);
@ -625,10 +610,11 @@ static Janet os_cryptorand(int32_t argc, Janet *argv) {
v = v >> 8; v = v >> 8;
} }
} }
#elif defined(JANET_LINUX) #elif defined(JANET_LINUX) || ( defined(JANET_APPLE) && !defined(MAC_OS_X_VERSION_10_7) )
/* We should be able to call getrandom on linux, but it doesn't seem /* We should be able to call getrandom on linux, but it doesn't seem
to be uniformly supported on linux distros. to be uniformly supported on linux distros.
In both cases, use this fallback path for now... */ On Mac, arc4random_buf wasn't available on until 10.7.
In these cases, use this fallback path for now... */
int rc; int rc;
int randfd; int randfd;
RETRY_EINTR(randfd, open("/dev/urandom", O_RDONLY | O_CLOEXEC)); RETRY_EINTR(randfd, open("/dev/urandom", O_RDONLY | O_CLOEXEC));
@ -645,7 +631,7 @@ static Janet os_cryptorand(int32_t argc, Janet *argv) {
n -= nread; n -= nread;
} }
RETRY_EINTR(rc, close(randfd)); RETRY_EINTR(rc, close(randfd));
#elif defined(JANET_BSD) || defined(JANET_APPLE) #elif defined(JANET_BSD) || defined(MAC_OS_X_VERSION_10_7)
(void) genericerr; (void) genericerr;
arc4random_buf(buffer->data + offset, n); arc4random_buf(buffer->data + offset, n);
#else #else