summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'util.c')
-rw-r--r--util.c30
1 files changed, 29 insertions, 1 deletions
diff --git a/util.c b/util.c
index f0d1fda..a5eb220 100644
--- a/util.c
+++ b/util.c
@@ -68,7 +68,7 @@ void hash(const unsigned char salt[static SALT_LEN], unsigned char *out, const v
sha256_final(&ctx, out);
}
-void print_hash(const unsigned char digest[static HASH_LEN]) {
+static void print_hash(const unsigned char digest[static HASH_LEN]) {
#ifdef DEBUG
char hash[HASH_LEN*2+1];
mem2hex(hash, digest, HASH_LEN);
@@ -93,3 +93,31 @@ bool hash_match(const unsigned char digest[static HASH_LEN], const char *arg) {
#endif
return !memcmp(digest, theirdigest, HASH_LEN);
}
+
+ssize_t random_get(void *buf, size_t buflen, unsigned int flags) {
+ memset(buf, 0, buflen);
+
+ long rv = syscall(SYS_getrandom, buf, buflen, flags);
+ if (rv == -1) {
+ if (errno == ENOSYS) {
+ fputs("getrandom returned ENOSYS. random-seed requires Linux 3.17\n", stderr);
+ exit(1);
+ }
+ } else {
+ bool all_zero = true;
+ if (buflen > 32) {
+ for (size_t i = 0; i < buflen; i++) {
+ if (((unsigned char *)buf)[i] != 0) {
+ all_zero = false;
+ break;
+ }
+ }
+ }
+ if (all_zero) {
+ fputs("getrandom returned all zeros, probably broken\n", stderr);
+ exit(1);
+ }
+ }
+
+ return rv;
+}