File tree Expand file tree Collapse file tree 2 files changed +32
-16
lines changed Expand file tree Collapse file tree 2 files changed +32
-16
lines changed Original file line number Diff line number Diff line change 55#include " platform/globals.h"
66#if defined(DART_HOST_OS_LINUX) || defined(DART_HOST_OS_ANDROID)
77
8- #include < errno.h> // NOLINT
9- #include < fcntl.h> // NOLINT
8+ #include < errno.h>
9+ #include < fcntl.h>
10+ #include < sys/syscall.h>
1011
1112#include " bin/crypto.h"
1213#include " bin/fdutils.h"
1516namespace dart {
1617namespace bin {
1718
18- bool Crypto::GetRandomBytes (intptr_t count, uint8_t * buffer) {
19+ static bool GetRandomFromDev (intptr_t count, uint8_t * buffer) {
1920 ThreadSignalBlocker signal_blocker (SIGPROF);
2021 intptr_t fd = TEMP_FAILURE_RETRY_NO_SIGNAL_BLOCKER (
2122 open (" /dev/urandom" , O_RDONLY | O_CLOEXEC));
@@ -38,6 +39,25 @@ bool Crypto::GetRandomBytes(intptr_t count, uint8_t* buffer) {
3839 return true ;
3940}
4041
42+ bool Crypto::GetRandomBytes (intptr_t count, uint8_t * buffer) {
43+ intptr_t bytes_read = 0 ;
44+ do {
45+ ssize_t res;
46+ do {
47+ res = syscall (__NR_getrandom, buffer + bytes_read, count - bytes_read,
48+ /* flags=*/ 0 );
49+ } while (res == -1 && errno == EINTR);
50+ if (res == -1 ) {
51+ if (errno == ENOSYS) {
52+ return GetRandomFromDev (count, buffer);
53+ }
54+ return false ;
55+ }
56+ bytes_read += res;
57+ } while (bytes_read < count);
58+ return true ;
59+ }
60+
4161} // namespace bin
4262} // namespace dart
4363
Original file line number Diff line number Diff line change 55#include " platform/globals.h"
66#if defined(DART_HOST_OS_MACOS)
77
8- #include < errno.h> // NOLINT
9- #include < fcntl.h> // NOLINT
8+ #include < errno.h>
9+ #include < fcntl.h>
10+ #include < sys/random.h>
1011
1112#include " bin/crypto.h"
1213#include " bin/fdutils.h"
@@ -16,23 +17,18 @@ namespace dart {
1617namespace bin {
1718
1819bool Crypto::GetRandomBytes (intptr_t count, uint8_t * buffer) {
19- intptr_t fd = TEMP_FAILURE_RETRY (open (" /dev/urandom" , O_RDONLY | O_CLOEXEC));
20- if (fd < 0 ) {
21- return false ;
22- }
2320 intptr_t bytes_read = 0 ;
2421 do {
25- int res =
26- TEMP_FAILURE_RETRY (read (fd, buffer + bytes_read, count - bytes_read));
22+ intptr_t chunk_size = count - bytes_read;
23+ if (chunk_size > 256 ) {
24+ chunk_size = 256 ;
25+ }
26+ int res = getentropy (buffer + bytes_read, chunk_size);
2727 if (res < 0 ) {
28- int err = errno;
29- close (fd);
30- errno = err;
3128 return false ;
3229 }
33- bytes_read += res ;
30+ bytes_read += chunk_size ;
3431 } while (bytes_read < count);
35- close (fd);
3632 return true ;
3733}
3834
You can’t perform that action at this time.
0 commit comments