1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Use CommonRandom if available

This commit is contained in:
Nobuyoshi Nakada 2021-03-19 15:23:03 +09:00
parent a85ed626f1
commit 4ea96f1d4f
No known key found for this signature in database
GPG key ID: 7CD2805BFA3770C6
Notes: git 2021-03-19 16:41:31 +09:00
2 changed files with 20 additions and 4 deletions

View file

@ -3634,7 +3634,8 @@ AS_CASE(["$target_os"],
RUBY_APPEND_OPTION(CFLAGS, -pipe)
AC_COMPILE_IFELSE([
AC_LANG_BOOL_COMPILE_TRY([@%:@include <AvailabilityMacros.h>],
[MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_7])],
[MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_7 &&
MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_10])],
[dnl
RUBY_APPEND_OPTION(XLDFLAGS, [-framework Security])
RUBY_APPEND_OPTION(LIBRUBYARG_STATIC, [-framework Security])

View file

@ -495,20 +495,35 @@ fill_random_bytes_urandom(void *seed, size_t size)
#if 0
#elif defined MAC_OS_X_VERSION_10_7 && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_7
#include <Security/SecRandom.h>
# if defined MAC_OS_X_VERSION_10_10 && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_10
# include <CommonCrypto/CommonRandom.h>
# define USE_COMMON_RANDOM 1
# else
# include <Security/SecRandom.h>
# define USE_COMMON_RANDOM 0
# endif
static int
fill_random_bytes_syscall(void *seed, size_t size, int unused)
{
int status = SecRandomCopyBytes(kSecRandomDefault, size, seed);
#if USE_COMMON_RANDOM
int failed = CCRandomGenerateBytes(seed, size) != kCCSuccess;
#else
int failed = SecRandomCopyBytes(kSecRandomDefault, size, seed) != errSecSuccess;
#endif
if (status != errSecSuccess) {
if (failed) {
# if 0
# if USE_COMMON_RANDOM
/* How to get the error message? */
# else
CFStringRef s = SecCopyErrorMessageString(status, NULL);
const char *m = s ? CFStringGetCStringPtr(s, kCFStringEncodingUTF8) : NULL;
fprintf(stderr, "SecRandomCopyBytes failed: %d: %s\n", status,
m ? m : "unknown");
if (s) CFRelease(s);
# endif
# endif
return -1;
}