Skip to content

Commit 36cfaa4

Browse files
committed
Improve retrieving entropy on Apple platforms
- replace call to SecRandomCopyBytes() by call to CCRandomGenerateBytes() (see #207) - remove dependency on the Security framework
1 parent 05088d5 commit 36cfaa4

File tree

2 files changed

+9
-38
lines changed

2 files changed

+9
-38
lines changed

Makefile.am

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ else
4040
AM_CFLAGS += -DSQLIT3MC_USE_MINIZ=1
4141
endif
4242

43-
if HOST_OSX
44-
AM_LDFLAGS += -framework Security
45-
endif
43+
#if HOST_OSX
44+
#AM_LDFLAGS += -framework Security
45+
#endif
4646

4747
LDADD = lib@[email protected]
4848

@@ -159,8 +159,8 @@ else
159159
# use it only in the "else" branch.
160160
sqlite3shell_LDFLAGS += -no-install
161161

162-
if HOST_OSX
163-
sqlite3shell_LDFLAGS += -framework Security
164-
endif
162+
#if HOST_OSX
163+
#sqlite3shell_LDFLAGS += -framework Security
164+
#endif
165165

166166
endif

src/chacha20poly1305.c

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -383,42 +383,13 @@ static size_t read_urandom(void* buf, size_t n)
383383
}
384384

385385
#if defined(__APPLE__)
386-
#include <AvailabilityMacros.h>
387-
#include <TargetConditionals.h>
388-
389-
/* Define version macros if needed */
390-
#ifndef MAC_OS_X_VERSION_MIN_REQUIRED
391-
#define MAC_OS_X_VERSION_MIN_REQUIRED 0
392-
#endif
393-
#ifndef __IPHONE_OS_VERSION_MIN_REQUIRED
394-
#define __IPHONE_OS_VERSION_MIN_REQUIRED 0
395-
#endif
396-
#ifndef __TV_OS_VERSION_MIN_REQUIRED
397-
#define __TV_OS_VERSION_MIN_REQUIRED 0
398-
#endif
399-
#ifndef __WATCH_OS_VERSION_MIN_REQUIRED
400-
#define __WATCH_OS_VERSION_MIN_REQUIRED 0
401-
#endif
402-
403-
#define CAN_USE_SECRANDOM() ( \
404-
(TARGET_OS_OSX && MAC_OS_X_VERSION_MIN_REQUIRED >= 1070) || \
405-
(TARGET_OS_IOS && __IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || \
406-
(TARGET_OS_TV && __TV_OS_VERSION_MIN_REQUIRED >= 90000) || \
407-
(TARGET_OS_WATCH && __WATCH_OS_VERSION_MIN_REQUIRED >= 20000) \
408-
)
409-
410-
#if CAN_USE_SECRANDOM()
411-
#include <Security/SecRandom.h>
412-
#endif
413-
414-
#else /* !__APPLE__ */
415-
#define CAN_USE_SECRANDOM() 0
386+
#include <CommonCrypto/CommonRandom.h>
416387
#endif
417388

418389
static size_t entropy(void* buf, size_t n)
419390
{
420-
#if defined(__APPLE__) && CAN_USE_SECRANDOM()
421-
if (SecRandomCopyBytes(kSecRandomDefault, n, (uint8_t*) buf) == 0)
391+
#if defined(__APPLE__)
392+
if (CCRandomGenerateBytes(buf, n) == kCCSuccess)
422393
return n;
423394
#elif defined(__linux__) && defined(SYS_getrandom)
424395
if (syscall(SYS_getrandom, buf, n, 0) == n)

0 commit comments

Comments
 (0)