public inbox for b.a.t.m.a.n@lists.open-mesh.org
 help / color / mirror / Atom feed
From: Sven Eckelmann <sven@narfation.org>
To: b.a.t.m.a.n@lists.open-mesh.org
Subject: [B.A.T.M.A.N.] [PATCH v3 8/9] batctl: Add helper to generate instant random bytes
Date: Tue, 18 Oct 2016 16:17:30 +0200	[thread overview]
Message-ID: <20161018141731.7970-8-sven@narfation.org> (raw)
In-Reply-To: <2403515.P9aVkiGJpp@bentobox>

Linux provides different ways to get instant random bytes. These are not
all supported on all systems and thus a fallback may have to be used.
Abstract all this in a single function which can be used from different
parts of the code.

The current implementations are

 * get random data from urandom pool via SYS_getrandom syscall
 * get random data from reading /dev/urandom
 * fallback to per-program prng initialized via the current time (seconds +
   nanoseconds)

All are tried in this order in hope to get a high quality random number
source before falling back to some really low quality one.

Signed-off-by: Sven Eckelmann <sven@narfation.org>
---
v3:
 - rebased on current master
 - changed __unused to __maybe_unused
 - integrated in rtnl patchset due to dependencies
---
 functions.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 functions.h |  2 ++
 2 files changed, 68 insertions(+)

diff --git a/functions.c b/functions.c
index 66b2d16..abd5882 100644
--- a/functions.c
+++ b/functions.c
@@ -41,6 +41,7 @@
 #include <linux/if_link.h>
 #include <linux/rtnetlink.h>
 #include <linux/neighbour.h>
+#include <sys/syscall.h>
 #include <errno.h>
 #include <net/if.h>
 #include <netlink/socket.h>
@@ -1072,3 +1073,68 @@ int check_mesh_iface_ownership(char *mesh_iface, char *hard_iface)
 
 	return EXIT_SUCCESS;
 }
+
+static int get_random_bytes_syscall(void *buf __maybe_unused,
+				    size_t buflen __maybe_unused)
+{
+#ifdef SYS_getrandom
+	return syscall(SYS_getrandom, buf, buflen, 0);
+#else
+	return -EOPNOTSUPP;
+#endif
+}
+
+static int get_random_bytes_urandom(void *buf, size_t buflen)
+{
+	int fd;
+	ssize_t r;
+
+	fd = open("/dev/urandom", O_RDONLY);
+	if (fd < 0)
+		return -EOPNOTSUPP;
+
+	r = read(fd, buf, buflen);
+	close(fd);
+	if (r < 0)
+		return -EOPNOTSUPP;
+
+	if ((size_t)r != buflen)
+		return -EOPNOTSUPP;
+
+	return 0;
+}
+
+static int get_random_bytes_fallback(void *buf, size_t buflen)
+{
+	struct timespec now;
+	static int initialized = 0;
+	size_t i;
+	uint8_t *bufc = buf;
+
+	/* this is not a good source for randomness */
+	if (!initialized) {
+		clock_gettime(CLOCK_MONOTONIC, &now);
+		srand(now.tv_sec ^ now.tv_nsec);
+		initialized = 1;
+	}
+
+	for (i = 0; i < buflen; i++)
+		bufc[i] = rand() & 0xff;
+
+	return 0;
+}
+
+void get_random_bytes(void *buf, size_t buflen)
+{
+	int ret;
+
+	ret = get_random_bytes_syscall(buf, buflen);
+	if (ret != -EOPNOTSUPP)
+		return;
+
+	ret = get_random_bytes_urandom(buf, buflen);
+	if (ret != -EOPNOTSUPP)
+		return;
+
+	get_random_bytes_fallback(buf, buflen);
+}
diff --git a/functions.h b/functions.h
index e413d6b..95cd6cf 100644
--- a/functions.h
+++ b/functions.h
@@ -53,6 +53,8 @@ int netlink_simple_request(struct nl_msg *msg);
 int check_mesh_iface(char *mesh_iface);
 int check_mesh_iface_ownership(char *mesh_iface, char *hard_iface);
 
+void get_random_bytes(void *buf, size_t buflen);
+
 int print_routing_algos(void);
 extern char *line_ptr;
 
-- 
2.9.3


  parent reply	other threads:[~2016-10-18 14:17 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-10-18 14:16 [B.A.T.M.A.N.] [PATCH v3 0/9] batctl: rtnetlink interface manipulation + userspace icmp Sven Eckelmann
2016-10-18 14:17 ` [B.A.T.M.A.N.] [PATCH v3 1/9] batctl: Use rtnl to query list of softif devices Sven Eckelmann
2016-10-18 14:17 ` [B.A.T.M.A.N.] [PATCH v3 2/9] batctl: Add command to create/destroy batman-adv interface Sven Eckelmann
2016-10-18 14:17 ` [B.A.T.M.A.N.] [PATCH v3 3/9] batctl: Use rtnl to add/remove interfaces Sven Eckelmann
2016-10-18 14:17 ` [B.A.T.M.A.N.] [PATCH v3 4/9] batctl: Parse interface arguments relative to last parsed option Sven Eckelmann
2016-10-18 14:17 ` [B.A.T.M.A.N.] [PATCH v3 5/9] batctl: Allow to disable automatic interface create/destroy Sven Eckelmann
2016-10-18 14:17 ` [B.A.T.M.A.N.] [PATCH v3 6/9] batctl: Move interface command to extra file Sven Eckelmann
2016-10-18 14:17 ` [B.A.T.M.A.N.] [PATCH v3 7/9] batctl: Move check_mesh_iface* to functions.c Sven Eckelmann
2016-10-18 14:17 ` Sven Eckelmann [this message]
2016-10-18 14:17 ` [B.A.T.M.A.N.] [PATCH v3 9/9] batctl: Implement non-routing batadv_icmp in userspace Sven Eckelmann
2016-10-24 11:18 ` [B.A.T.M.A.N.] [PATCH v3 0/9] batctl: rtnetlink interface manipulation + userspace icmp Simon Wunderlich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20161018141731.7970-8-sven@narfation.org \
    --to=sven@narfation.org \
    --cc=b.a.t.m.a.n@lists.open-mesh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox