ruby-changes:65520
From: Nobuyoshi <ko1@a...>
Date: Fri, 19 Mar 2021 16:41:30 +0900 (JST)
Subject: [ruby-changes:65520] 4ea96f1d4f (master): Use CommonRandom if available
https://git.ruby-lang.org/ruby.git/commit/?id=4ea96f1d4f From 4ea96f1d4f8a50c204c4367c994cdbf12cd97b64 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Fri, 19 Mar 2021 15:23:03 +0900 Subject: Use CommonRandom if available --- configure.ac | 3 ++- random.c | 21 ++++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/configure.ac b/configure.ac index 0110896..83eee92 100644 --- a/configure.ac +++ b/configure.ac @@ -3634,7 +3634,8 @@ AS_CASE(["$target_os"], https://github.com/ruby/ruby/blob/trunk/configure.ac#L3634 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]) diff --git a/random.c b/random.c index d68eade..0fc8789 100644 --- a/random.c +++ b/random.c @@ -495,21 +495,36 @@ fill_random_bytes_urandom(void *seed, size_t size) https://github.com/ruby/ruby/blob/trunk/random.c#L495 #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; } return 0; -- cgit v1.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/