[PATCH 2/5] lib: random: get_random_bytes from HWRNG if present

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Instead of generating pseudo random numbers, get random bytes
from an optional HW generator, if enabled and registered.

Signed-off-by: Steffen Trumtrar <s.trumtrar@xxxxxxxxxxxxxx>
---
 lib/random.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/lib/random.c b/lib/random.c
index 210fea99466e..ff82dd1bed85 100644
--- a/lib/random.c
+++ b/lib/random.c
@@ -1,5 +1,6 @@
 #include <common.h>
 #include <stdlib.h>
+#include <linux/hw_random.h>
 
 static unsigned int random_seed;
 
@@ -22,6 +23,24 @@ void get_random_bytes(void *_buf, int len)
 {
 	char *buf = _buf;
 
+	if (IS_ENABLED(CONFIG_HWRNG)) {
+		struct hwrng *rng;
+		int bytes;
+
+		rng = hwrng_get_first();
+		if (!PTR_ERR(rng)) {
+			while (len) {
+				bytes = hwrng_get_data(rng, _buf, len, 0);
+				if (!bytes)
+					goto sw_fallback;
+				len -= bytes;
+			}
+
+			return;
+		}
+	}
+
+sw_fallback:
 	while (len--)
 		*buf++ = rand() % 256;
 }
-- 
2.7.0


_______________________________________________
barebox mailing list
barebox@xxxxxxxxxxxxxxxxxxx
http://lists.infradead.org/mailman/listinfo/barebox



[Index of Archives]     [Linux Embedded]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux