Skip to content

Commit 21307d5

Browse files
committed
Use SecRandomCopyBytes() on Apple platforms
Function SecRandomCopyBytes() seems to be available on all relevant Apple platforms, while getentropy() is not.
1 parent dd508dc commit 21307d5

File tree

1 file changed

+3
-10
lines changed

1 file changed

+3
-10
lines changed

src/chacha20poly1305.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -371,20 +371,13 @@ static size_t read_urandom(void* buf, size_t n)
371371
}
372372

373373
#if defined(__APPLE__)
374-
#include "TargetConditionals.h"
375-
#endif
376-
377-
#if defined(__APPLE__) && defined(__MAC_10_12) && !defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
378-
#include <sys/random.h>
374+
#include <Security/SecRandom.h>
379375
#endif
380376

381377
static size_t entropy(void* buf, size_t n)
382378
{
383-
#if defined(__APPLE__) && ((defined(__MAC_10_12) && __MAC_OS_X_VERSION_MAX_ALLOWED >= __MAC_10_12) || TARGET_OS_TV || TARGET_OS_WATCH)
384-
#if TARGET_OS_TV || TARGET_OS_WATCH
385-
extern int getentropy(void* buffer, size_t buflen);
386-
#endif
387-
if (getentropy(buf, n) == 0)
379+
#if defined(__APPLE__)
380+
if (SecRandomCopyBytes(kSecRandomDefault, n, (uint8_t*) buf) == 0)
388381
return n;
389382
#elif defined(__linux__) && defined(SYS_getrandom)
390383
if (syscall(SYS_getrandom, buf, n, 0) == n)

0 commit comments

Comments
 (0)