summaryrefslogtreecommitdiff
path: root/lib/crypto
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2016-12-01 17:38:43 -0800
committerOri Bernstein <ori@eigenstate.org>2016-12-01 17:39:23 -0800
commit9d6574afc35fe96fbd8f2172fc62f9037df0efb1 (patch)
treef32d1b27715323ae104613d0abccf2f53d1dc273 /lib/crypto
parent1ebc8978591d1a852130e8d2246bfb559597c471 (diff)
downloadmc-9d6574afc35fe96fbd8f2172fc62f9037df0efb1.tar.gz
Add 'getentropy()' call.
It gets entropy. Should work on all systems with /dev/random, and will get overridden on systems with getentropy() syscalls.
Diffstat (limited to 'lib/crypto')
-rw-r--r--lib/crypto/bld.sub3
-rw-r--r--lib/crypto/entropy.myr24
2 files changed, 27 insertions, 0 deletions
diff --git a/lib/crypto/bld.sub b/lib/crypto/bld.sub
index 2ca1f13..b378d09 100644
--- a/lib/crypto/bld.sub
+++ b/lib/crypto/bld.sub
@@ -11,6 +11,9 @@ lib crypto =
chacha20.myr
aes.myr
+ # randomness
+ entropy.myr # currently assumes a /dev/random
+
lib ../std:std
lib ../sys:sys
;;
diff --git a/lib/crypto/entropy.myr b/lib/crypto/entropy.myr
new file mode 100644
index 0000000..b5c5120
--- /dev/null
+++ b/lib/crypto/entropy.myr
@@ -0,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
+ ;;
+ ;;
+}