#include <sys/random.h> | |
#include <assert.h> | |
#include <errno.h> | |
#include <zircon/syscalls.h> | |
#define MAX_LENGTH 256 | |
static_assert(MAX_LENGTH <= ZX_CPRNG_DRAW_MAX_LEN, ""); | |
int getentropy(void* buffer, size_t length) { | |
if (length > MAX_LENGTH) { | |
errno = EIO; | |
return -1; | |
} | |
size_t actual_length; | |
zx_status_t status = _zx_cprng_draw(buffer, length, &actual_length); | |
if (status != ZX_OK || actual_length != length) { | |
errno = EIO; | |
return -1; | |
} | |
return 0; | |
} |