#include #include #include #include #include #include "logerr.h" #include "random.h" static FILE * FRANDOM = NULL; int random_init(void) { int rc = -1; FRANDOM = fopen("/dev/urandom", "r"); if (FRANDOM == NULL) { logerr("fopen(): %s", strerror(errno)); goto out; } rc = 0; out: return rc; } int random_end(void) { int rc = -1; if (fclose(FRANDOM)) { logerr("fclose(): %s", strerror(errno)); goto out; } rc = 0; out: FRANDOM = NULL; return rc; } int random_bytes(const size_t length, u8 (*const out)[]) { int rc = -1; assert(FRANDOM != NULL); const size_t read_count = fread(out, 1U, length, FRANDOM); if (ferror(FRANDOM)) { logerr("fread(): %s", strerror(errno)); goto out; } assert(read_count == length); rc = 0; out: return rc; }