core: add random number generator

This commit is contained in:
2024-10-24 21:33:05 +01:00
parent d0dcee9c6f
commit 44fb8593a5
8 changed files with 505 additions and 3 deletions

35
core/sys/darwin/random.c Normal file
View File

@@ -0,0 +1,35 @@
#include <fcntl.h>
#include <stdint.h>
#include <unistd.h>
uint64_t z__b_platform_random_seed()
{
int fd = open("/dev/urandom", O_RDONLY);
if (fd == -1) {
return (uint64_t)-1;
}
uint64_t v = 0;
if (read(fd, &v, sizeof v) != sizeof v) {
close(fd);
return (uint64_t)-1;
}
return v;
}
uint64_t z__b_platform_random_seed_secure()
{
int fd = open("/dev/random", O_RDONLY);
if (fd == -1) {
return (uint64_t)-1;
}
uint64_t v = 0;
if (read(fd, &v, sizeof v) != sizeof v) {
close(fd);
return (uint64_t)-1;
}
return v;
}

35
core/sys/linux/random.c Normal file
View File

@@ -0,0 +1,35 @@
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
uint64_t z__c_platform_random_seed()
{
int fd = open("/dev/urandom", O_RDONLY);
if (fd == -1) {
return (uint64_t)-1;
}
uint64_t v = 0;
if (read(fd, &v, sizeof v) != sizeof v) {
close(fd);
return (uint64_t)-1;
}
return v;
}
uint64_t z__c_platform_random_seed_secure()
{
int fd = open("/dev/random", O_RDONLY);
if (fd == -1) {
return (uint64_t)-1;
}
uint64_t v = 0;
if (read(fd, &v, sizeof v) != sizeof v) {
close(fd);
return (uint64_t)-1;
}
return v;
}