commit 65ab15d2e352648da6e03aeb31b99a4f013d03ff
parent 4a6f19ad8893cd3334b3ba95af35166790458021
Author: William Casarin <jb55@jb55.com>
Date: Fri, 9 Feb 2024 18:59:30 -0800
random: add getrandom fallback for android
Signed-off-by: William Casarin <jb55@jb55.com>
Diffstat:
1 file changed, 20 insertions(+), 0 deletions(-)
diff --git a/src/random.h b/src/random.h
@@ -42,6 +42,26 @@ static int fill_random(unsigned char* data, size_t size) {
} else {
return 1;
}
+#elif defined(__ANDROID__)
+ int fd = open("/dev/urandom", O_RDONLY);
+ if (fd < 0) {
+ return 0; // Failed to open /dev/urandom
+ }
+ ssize_t read_bytes = 0;
+ while (size > 0) {
+ read_bytes = read(fd, data, size);
+ if (read_bytes <= 0) {
+ if (errno == EINTR) {
+ continue; // If interrupted by signal, try again
+ }
+ close(fd);
+ return 0; // Failed to read
+ }
+ data += read_bytes;
+ size -= read_bytes;
+ }
+ close(fd);
+ return 1;
#elif defined(__linux__) || defined(__FreeBSD__)
/* If `getrandom(2)` is not available you should fallback to /dev/urandom */
ssize_t res = getrandom(data, size, 0);