Skip to content

Commit

Permalink
Adding support for arc4random_buf for OSX
Browse files Browse the repository at this point in the history
  • Loading branch information
david-dick committed Jan 19, 2025
1 parent cfedce5 commit b488dc1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
17 changes: 13 additions & 4 deletions URandom.xs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@
#ifdef HAVE_CRYPT_URANDOM_SYSCALL_GETRANDOM
#include <sys/syscall.h>
#else
#ifdef HAVE_CRYPT_URANDOM_NATIVE_GETENTROPY
#ifdef HAVE_CRYPT_URANDOM_NATIVE_ARC4RANDOM_BUF
#include <sys/random.h>
#else
#ifdef HAVE_CRYPT_URANDOM_UNISTD_GETENTROPY
#ifdef HAVE_CRYPT_URANDOM_UNISTD_ARC4RANDOM_BUF
#include <unistd.h>
#else
#ifdef HAVE_CRYPT_URANDOM_STDLIB_ARC4RANDOM_BUF
#include <stdlib.h>
#endif
#endif
#endif
#endif
Expand Down Expand Up @@ -42,18 +46,23 @@ crypt_urandom_getrandom(length)
#ifdef HAVE_CRYPT_URANDOM_SYSCALL_GETRANDOM
result = syscall(SYS_getrandom, data, length, GRND_NONBLOCK);
#else
#ifdef HAVE_CRYPT_URANDOM_NATIVE_GETENTROPY
#ifdef HAVE_CRYPT_URANDOM_NATIVE_ARC4RANDOM_BUF
arc4random_buf(data, length);
result = length;
#else
#ifdef HAVE_CRYPT_URANDOM_UNISTD_ARC4RANDOM_BUF
arc4random_buf(data, length);
result = length;
#else
#ifdef HAVE_CRYPT_URANDOM_UNISTD_GETENTROPY
#ifdef HAVE_CRYPT_URANDOM_STDLIB_ARC4RANDOM_BUF
arc4random_buf(data, length);
result = length;
#else
croak("Unable to find getrandom or an alternative");
#endif
#endif
#endif
#endif
#endif
if (result != length) {
if (errno == EINTR) {
Expand Down
43 changes: 32 additions & 11 deletions check_random.inc
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ _OUT_
if ($? == 0) {
warn_or_diag "getentropy from sys/random is AVAILABLE\n";
unlink $binary_name or die "Failed to unlink $binary_name:$!";
$optional{DEFINE} = '-DHAVE_CRYPT_URANDOM_NATIVE_GETENTROPY';
$optional{DEFINE} = '-DHAVE_CRYPT_URANDOM_NATIVE_ARC4RANDOM_BUF';
} else {
warn_or_diag "getentropy from sys/random is unavailable\n";
open FOO, ">$test_file_name" or die "Failed to open $test_file_name for writing:$!";
Expand All @@ -92,28 +92,49 @@ _OUT_
if ($? == 0) {
warn_or_diag "getentropy from unistd is AVAILABLE\n";
unlink $binary_name or die "Failed to unlink $binary_name:$!";
$optional{DEFINE} = '-DHAVE_CRYPT_URANDOM_UNISTD_GETENTROPY';
$optional{DEFINE} = '-DHAVE_CRYPT_URANDOM_UNISTD_ARC4RANDOM_BUF';
} else {
warn_or_diag "getentropy from unistd is unavailable\n";
warn_or_diag "getentropy from sys/random is unavailable\n";
open FOO, ">$test_file_name" or die "Failed to open $test_file_name for writing:$!";
print FOO <<'_OUT_';
#include <stdlib.h>

int main(void)
{
return 0;
char buf[5];
int l = 5;
arc4random_buf(buf, l);
return l;
}
_OUT_
close FOO or die "Failed to close $test_file_name:$!";
$output .= `$Config{cc} -o $binary_name $test_file_name 2>&1`;
if ($? == 0) {
warn_or_diag "C compiler is AVAILABLE\n";
if ($ENV{CRYPT_URANDOM_BUILD_DEBUG}) {
warn_or_diag $output;
}
warn_or_diag "getentropy from stdlib is AVAILABLE\n";
unlink $binary_name or die "Failed to unlink $binary_name:$!";
$optional{DEFINE} = '-DUNKNOWN_ENVIRONMENT';
$optional{DEFINE} = '-DHAVE_CRYPT_URANDOM_STDLIB_ARC4RANDOM_BUF';
} else {
warn_or_diag "C compiler is unavailable\n";
$optional{DEFINE} = '-DNO_COMPILER_FOUND';
warn_or_diag "getentropy from unistd is unavailable\n";
open FOO, ">$test_file_name" or die "Failed to open $test_file_name for writing:$!";
print FOO <<'_OUT_';
int main(void)
{
return 0;
}
_OUT_
close FOO or die "Failed to close $test_file_name:$!";
$output .= `$Config{cc} -o $binary_name $test_file_name 2>&1`;
if ($? == 0) {
warn_or_diag "C compiler is AVAILABLE\n";
if ($ENV{CRYPT_URANDOM_BUILD_DEBUG}) {
warn_or_diag $output;
}
unlink $binary_name or die "Failed to unlink $binary_name:$!";
$optional{DEFINE} = '-DUNKNOWN_ENVIRONMENT';
} else {
warn_or_diag "C compiler is unavailable\n";
$optional{DEFINE} = '-DNO_COMPILER_FOUND';
}
}
}
}
Expand Down

0 comments on commit b488dc1

Please sign in to comment.