shithub: mc

Download patch

ref: 9d6574afc35fe96fbd8f2172fc62f9037df0efb1
parent: 1ebc8978591d1a852130e8d2246bfb559597c471
author: Ori Bernstein <ori@eigenstate.org>
date: Thu Dec 1 12:38:43 EST 2016

Add 'getentropy()' call.

	It gets entropy. Should work on all systems with
	/dev/random, and will get overridden on systems
	with getentropy() syscalls.

--- a/lib/crypto/bld.sub
+++ b/lib/crypto/bld.sub
@@ -11,6 +11,9 @@
 	chacha20.myr
 	aes.myr
 
+	# randomness
+	entropy.myr	# currently assumes a /dev/random
+
 	lib ../std:std
 	lib ../sys:sys
 ;;
--- /dev/null
+++ b/lib/crypto/entropy.myr
@@ -1,0 +1,24 @@
+use std
+
+pkg crypto =
+	const getentropy : (buf : byte[:] -> void)
+;;
+
+var randfd
+
+const __init__ = {
+	randfd = std.try(std.open("/dev/random", std.Ordonly))
+}
+
+const getentropy = {buf
+	var nread
+
+	nread = 0
+	while nread < buf.len
+		match std.read(randfd, buf)
+		| `std.Err e:	std.die(std.fmt("unable to read from randfd: {}\n", e))
+		| `std.Ok 0:	std.die("could not get entropy from randfd: EOF\n")
+		| `std.Ok n:	nread += n
+		;;
+	;;
+}