ish/kernel/random.c
Theodore Dubois 5cb73a326f Remove dependency on Security.framework
The replacement, CCRandomGenerateBytes, is available in the C library. My computer was giving me an annoying and somewhat strange linker warning:

ld: warning: text-based stub file /System/Library/Frameworks//Security.framework/Security.tbd and library file /System/Library/Frameworks//Security.framework/Security are out of sync. Falling back to library file for linking.
2018-12-10 16:16:20 -08:00

31 lines
675 B
C

#include <fcntl.h>
#include "kernel/calls.h"
#ifdef __APPLE__
#include <CommonCrypto/CommonCrypto.h>
#include <CommonCrypto/CommonRandom.h>
#else
#include <unistd.h>
#include <sys/syscall.h>
#include <linux/random.h>
#endif
int get_random(char *buf, size_t len) {
#ifdef __APPLE__
return CCRandomGenerateBytes(buf, len) != kCCSuccess;
#else
return syscall(SYS_getrandom, buf, len, 0) < 0;
#endif
}
dword_t sys_getrandom(addr_t buf_addr, dword_t len, dword_t flags) {
if (len > 256)
return _EIO;
char buf[256];
if (get_random(buf, len) != 0)
return _EIO;
if (user_write(buf_addr, buf, len))
return _EFAULT;
return len;
}