mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH v2 0/3] upstream hwrng framework
@ 2017-03-16  5:31 Oleksij Rempel
  2017-03-16  5:31 ` [PATCH v2 1/3] drivers: add simple hw_random implementation Oleksij Rempel
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Oleksij Rempel @ 2017-03-16  5:31 UTC (permalink / raw)
  To: barebox; +Cc: Oleksij Rempel

changes v1:
 - initial version

changes v2:
 - drop Freescale RNGC for now. It need more testing
 - add caamrng port
 - fix hwrng_init()
 - fix hwrng_get_first check in get_random_bytes

Oleksij Rempel (1):
  camrng: port to hwrng framework

Steffen Trumtrar (2):
  drivers: add simple hw_random implementation
  lib: random: get_random_bytes from HWRNG if present

 drivers/Kconfig               |   1 +
 drivers/Makefile              |   1 +
 drivers/crypto/caam/Kconfig   |   1 +
 drivers/crypto/caam/caamrng.c |  40 +++++-----------
 drivers/hw_random/Kconfig     |   6 +++
 drivers/hw_random/Makefile    |   1 +
 drivers/hw_random/core.c      | 108 ++++++++++++++++++++++++++++++++++++++++++
 include/linux/hw_random.h     |  43 +++++++++++++++++
 lib/random.c                  |  19 ++++++++
 9 files changed, 192 insertions(+), 28 deletions(-)
 create mode 100644 drivers/hw_random/Kconfig
 create mode 100644 drivers/hw_random/Makefile
 create mode 100644 drivers/hw_random/core.c
 create mode 100644 include/linux/hw_random.h

-- 
2.11.0


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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 1/3] drivers: add simple hw_random implementation
  2017-03-16  5:31 [PATCH v2 0/3] upstream hwrng framework Oleksij Rempel
@ 2017-03-16  5:31 ` Oleksij Rempel
  2017-03-16  5:31 ` [PATCH v2 2/3] lib: random: get_random_bytes from HWRNG if present Oleksij Rempel
  2017-03-16  5:31 ` [PATCH v2 3/3] caamrng: port to hwrng framework Oleksij Rempel
  2 siblings, 0 replies; 6+ messages in thread
From: Oleksij Rempel @ 2017-03-16  5:31 UTC (permalink / raw)
  To: barebox; +Cc: Oleksij Rempel, Steffen Trumtrar

From: Steffen Trumtrar <s.trumtrar@pengutronix.de>

Add a simple hw_random implementation based on code from
Linux v4.5-rc5.

All the entropypool initialization stuff is left out and
the obsolete data_read/data_present calls are omitted.

Signed-off-by: Steffen Trumtrar <s.trumtrar@pengutronix.de>
Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
 drivers/Kconfig            |   1 +
 drivers/Makefile           |   1 +
 drivers/hw_random/Kconfig  |   6 +++
 drivers/hw_random/Makefile |   1 +
 drivers/hw_random/core.c   | 108 +++++++++++++++++++++++++++++++++++++++++++++
 include/linux/hw_random.h  |  43 ++++++++++++++++++
 6 files changed, 160 insertions(+)
 create mode 100644 drivers/hw_random/Kconfig
 create mode 100644 drivers/hw_random/Makefile
 create mode 100644 drivers/hw_random/core.c
 create mode 100644 include/linux/hw_random.h

diff --git a/drivers/Kconfig b/drivers/Kconfig
index cc086ac2d..2f5784a4d 100644
--- a/drivers/Kconfig
+++ b/drivers/Kconfig
@@ -21,6 +21,7 @@ source "drivers/eeprom/Kconfig"
 source "drivers/input/Kconfig"
 source "drivers/watchdog/Kconfig"
 source "drivers/pwm/Kconfig"
+source "drivers/hw_random/Kconfig"
 source "drivers/dma/Kconfig"
 source "drivers/gpio/Kconfig"
 source "drivers/w1/Kconfig"
diff --git a/drivers/Makefile b/drivers/Makefile
index 6a70f6ee1..7e9b80e59 100644
--- a/drivers/Makefile
+++ b/drivers/Makefile
@@ -20,6 +20,7 @@ obj-y	+= misc/
 obj-y	+= dma/
 obj-y  += watchdog/
 obj-y	+= gpio/
+obj-$(CONFIG_HWRNG) += hw_random/
 obj-$(CONFIG_OFTREE) += of/
 obj-$(CONFIG_W1) += w1/
 obj-y += pinctrl/
diff --git a/drivers/hw_random/Kconfig b/drivers/hw_random/Kconfig
new file mode 100644
index 000000000..807fcadd3
--- /dev/null
+++ b/drivers/hw_random/Kconfig
@@ -0,0 +1,6 @@
+menuconfig HWRNG
+	bool "HWRNG Support"
+	help
+	  Support for HWRNG(Hardware Random Number Generator) devices.
+
+	  If unsure, say no.
diff --git a/drivers/hw_random/Makefile b/drivers/hw_random/Makefile
new file mode 100644
index 000000000..15307b100
--- /dev/null
+++ b/drivers/hw_random/Makefile
@@ -0,0 +1 @@
+obj-$(CONFIG_HWRNG)		+= core.o
diff --git a/drivers/hw_random/core.c b/drivers/hw_random/core.c
new file mode 100644
index 000000000..2991f58c3
--- /dev/null
+++ b/drivers/hw_random/core.c
@@ -0,0 +1,108 @@
+/*
+ * Copyright (c) 2016 Pengutronix, Steffen Trumtrar <kernel@pengutronix.de>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * derived from Linux kernel drivers/char/hw_random/core.c
+ */
+
+#include <common.h>
+#include <linux/hw_random.h>
+#include <malloc.h>
+
+static LIST_HEAD(hwrngs);
+
+#define RNG_BUFFER_SIZE		32
+
+int hwrng_get_data(struct hwrng *rng, void *buffer, size_t size, int wait)
+{
+	return rng->read(rng, buffer, size, wait);
+}
+
+static int hwrng_init(struct hwrng *rng)
+{
+	int ret = 0;
+
+	if (rng->init)
+		ret = rng->init(rng);
+
+	if (!ret)
+		list_add_tail(&rng->list, &hwrngs);
+
+	return ret;
+}
+
+static ssize_t rng_dev_read(struct cdev *cdev, void *buf, size_t size,
+			    loff_t offset, unsigned long flags)
+{
+	struct hwrng *rng = container_of(cdev, struct hwrng, cdev);
+	size_t count = size;
+	ssize_t cur = 0;
+	int len;
+
+	memset(buf, 0, size);
+
+	while (count) {
+		int max = min(count, (size_t)RNG_BUFFER_SIZE);
+		len = hwrng_get_data(rng, rng->buf, max, true);
+		if (len < 0) {
+			cur = len;
+			break;
+		}
+
+		memcpy(buf + cur, rng->buf, len);
+
+		count -= len;
+		cur += len;
+	}
+
+	return cur;
+}
+
+static struct file_operations rng_chrdev_ops = {
+	.read  = rng_dev_read,
+	.lseek = dev_lseek_default,
+};
+
+static int hwrng_register_cdev(struct hwrng *rng)
+{
+	rng->cdev.name = "hwrng";
+	rng->cdev.flags = DEVFS_IS_CHARACTER_DEV;
+	rng->cdev.ops = &rng_chrdev_ops;
+	rng->cdev.dev = rng->dev;
+
+	return devfs_create(&rng->cdev);
+}
+
+struct hwrng *hwrng_get_first(void)
+{
+	if (list_empty(&hwrngs))
+		return ERR_PTR(-ENODEV);
+	else
+		return list_first_entry(&hwrngs, struct hwrng, list);
+}
+
+int hwrng_register(struct device_d *dev, struct hwrng *rng)
+{
+	int err;
+
+	if (rng->name == NULL || rng->read == NULL)
+		return -EINVAL;
+
+	rng->buf = xzalloc(RNG_BUFFER_SIZE);
+	rng->dev = dev;
+
+	err = hwrng_init(rng);
+	if (err) {
+		free(rng->buf);
+		return err;
+	}
+
+	err = hwrng_register_cdev(rng);
+	if (err)
+		free(rng->buf);
+
+	return err;
+}
diff --git a/include/linux/hw_random.h b/include/linux/hw_random.h
new file mode 100644
index 000000000..299a717dd
--- /dev/null
+++ b/include/linux/hw_random.h
@@ -0,0 +1,43 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef LINUX_HWRANDOM_H_
+#define LINUX_HWRANDOM_H_
+
+#include <linux/list.h>
+
+/**
+ * struct hwrng - Hardware Random Number Generator driver
+ * @name:		Unique RNG name.
+ * @init:		Initialization callback (can be NULL).
+ * @read:		New API. drivers can fill up to max bytes of data
+ *			into the buffer. The buffer is aligned for any type.
+ * @priv:		Private data, for use by the RNG driver.
+ */
+struct hwrng {
+	const char *name;
+	int (*init)(struct hwrng *rng);
+	int (*read)(struct hwrng *rng, void *data, size_t max, bool wait);
+
+	struct list_head list;
+
+	struct cdev cdev;
+	struct device_d *dev;
+	void *buf;
+};
+
+/* Register a new Hardware Random Number Generator driver. */
+int hwrng_register(struct device_d *dev, struct hwrng *rng);
+struct hwrng *hwrng_get_first(void);
+int hwrng_get_data(struct hwrng *rng, void *buffer, size_t size, int wait);
+
+#endif /* LINUX_HWRANDOM_H_ */
-- 
2.11.0


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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 2/3] lib: random: get_random_bytes from HWRNG if present
  2017-03-16  5:31 [PATCH v2 0/3] upstream hwrng framework Oleksij Rempel
  2017-03-16  5:31 ` [PATCH v2 1/3] drivers: add simple hw_random implementation Oleksij Rempel
@ 2017-03-16  5:31 ` Oleksij Rempel
  2017-03-17 12:30   ` Sascha Hauer
  2017-03-16  5:31 ` [PATCH v2 3/3] caamrng: port to hwrng framework Oleksij Rempel
  2 siblings, 1 reply; 6+ messages in thread
From: Oleksij Rempel @ 2017-03-16  5:31 UTC (permalink / raw)
  To: barebox; +Cc: Oleksij Rempel, Steffen Trumtrar

From: Steffen Trumtrar <s.trumtrar@pengutronix.de>

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@pengutronix.de>
Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
 lib/random.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/lib/random.c b/lib/random.c
index 210fea994..42c0f83b7 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 (!IS_ERR(rng)) {
+			while (len) {
+				bytes = hwrng_get_data(rng, _buf, len, 1);
+				if (!bytes)
+					goto sw_fallback;
+				len -= bytes;
+			}
+
+			return;
+		}
+	}
+
+sw_fallback:
 	while (len--)
 		*buf++ = rand() % 256;
 }
-- 
2.11.0


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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 3/3] caamrng: port to hwrng framework
  2017-03-16  5:31 [PATCH v2 0/3] upstream hwrng framework Oleksij Rempel
  2017-03-16  5:31 ` [PATCH v2 1/3] drivers: add simple hw_random implementation Oleksij Rempel
  2017-03-16  5:31 ` [PATCH v2 2/3] lib: random: get_random_bytes from HWRNG if present Oleksij Rempel
@ 2017-03-16  5:31 ` Oleksij Rempel
  2 siblings, 0 replies; 6+ messages in thread
From: Oleksij Rempel @ 2017-03-16  5:31 UTC (permalink / raw)
  To: barebox; +Cc: Oleksij Rempel

Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
 drivers/crypto/caam/Kconfig   |  1 +
 drivers/crypto/caam/caamrng.c | 40 ++++++++++++----------------------------
 2 files changed, 13 insertions(+), 28 deletions(-)

diff --git a/drivers/crypto/caam/Kconfig b/drivers/crypto/caam/Kconfig
index cf05d1c07..2ab509d11 100644
--- a/drivers/crypto/caam/Kconfig
+++ b/drivers/crypto/caam/Kconfig
@@ -29,6 +29,7 @@ config CRYPTO_DEV_FSL_CAAM_RINGSIZE
 config CRYPTO_DEV_FSL_CAAM_RNG
 	bool "Register caam RNG device"
 	depends on CRYPTO_DEV_FSL_CAAM
+	depends on HWRNG
 	default y
 	help
 	  Selecting this will register the SEC4 hardware rng.
diff --git a/drivers/crypto/caam/caamrng.c b/drivers/crypto/caam/caamrng.c
index 0fef171a2..aabad0416 100644
--- a/drivers/crypto/caam/caamrng.c
+++ b/drivers/crypto/caam/caamrng.c
@@ -35,6 +35,7 @@
 #include <driver.h>
 #include <init.h>
 #include <linux/spinlock.h>
+#include <linux/hw_random.h>
 
 #include "regs.h"
 #include "intern.h"
@@ -71,7 +72,7 @@ struct caam_rng_ctx {
 	unsigned int cur_buf_idx;
 	int current_buf;
 	struct buf_data bufs[2];
-	struct cdev cdev;
+	struct hwrng rng;
 };
 
 static struct caam_rng_ctx *rng_ctx;
@@ -116,8 +117,9 @@ static inline int submit_job(struct caam_rng_ctx *ctx, int to_current)
 	return err;
 }
 
-static int caam_read(struct caam_rng_ctx *ctx, void *data, size_t max, bool wait)
+static int caam_read(struct hwrng *rng, void *data, size_t max, bool wait)
 {
+	struct caam_rng_ctx *ctx = container_of(rng, struct caam_rng_ctx, rng);
 	struct buf_data *bd = &ctx->bufs[ctx->current_buf];
 	int next_buf_idx, copied_idx;
 	int err;
@@ -162,7 +164,7 @@ static int caam_read(struct caam_rng_ctx *ctx, void *data, size_t max, bool wait
 	dev_dbg(ctx->jrdev, "switched to buffer %d\n", ctx->current_buf);
 
 	/* since there already is some data read, don't wait */
-	return copied_idx + caam_read(ctx, data + copied_idx,
+	return copied_idx + caam_read(rng, data + copied_idx,
 				      max - copied_idx, false);
 }
 
@@ -248,29 +250,6 @@ static int caam_init_rng(struct caam_rng_ctx *ctx, struct device_d *jrdev)
 	return 0;
 }
 
-static ssize_t random_read(struct cdev *cdev, void *buf, size_t count,
-			   loff_t offset, ulong flags)
-{
-	struct caam_rng_ctx *ctx = container_of(cdev, struct caam_rng_ctx, cdev);
-
-	return caam_read(ctx, buf, count, true);
-}
-
-static struct file_operations randomops = {
-	.read  = random_read,
-	.lseek = dev_lseek_default,
-};
-
-static int caam_init_devrandom(struct caam_rng_ctx *ctx, struct device_d *dev)
-{
-	ctx->cdev.name = "hwrng";
-	ctx->cdev.flags = DEVFS_IS_CHARACTER_DEV;
-	ctx->cdev.ops = &randomops;
-	ctx->cdev.dev = dev;
-
-	return devfs_create(&ctx->cdev);
-}
-
 int caam_rng_probe(struct device_d *dev, struct device_d *jrdev)
 {
 	int err;
@@ -281,9 +260,14 @@ int caam_rng_probe(struct device_d *dev, struct device_d *jrdev)
 	if (err)
 		return err;
 
-	err = caam_init_devrandom(rng_ctx, dev);
-	if (err)
+	rng_ctx->rng.name = dev->name;
+	rng_ctx->rng.read = caam_read;
+
+	err = hwrng_register(dev, &rng_ctx->rng);
+	if (err) {
+		dev_err(dev, "rng-caam registering failed (%d)\n", err);
 		return err;
+	}
 
 	dev_info(dev, "registering rng-caam\n");
 
-- 
2.11.0


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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 2/3] lib: random: get_random_bytes from HWRNG if present
  2017-03-16  5:31 ` [PATCH v2 2/3] lib: random: get_random_bytes from HWRNG if present Oleksij Rempel
@ 2017-03-17 12:30   ` Sascha Hauer
  2017-03-17 14:30     ` Oleksij Rempel
  0 siblings, 1 reply; 6+ messages in thread
From: Sascha Hauer @ 2017-03-17 12:30 UTC (permalink / raw)
  To: Oleksij Rempel; +Cc: barebox, Steffen Trumtrar

On Thu, Mar 16, 2017 at 06:31:45AM +0100, Oleksij Rempel wrote:
> From: Steffen Trumtrar <s.trumtrar@pengutronix.de>
> 
> 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@pengutronix.de>
> Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
> ---
>  lib/random.c | 19 +++++++++++++++++++
>  1 file changed, 19 insertions(+)
> 
> diff --git a/lib/random.c b/lib/random.c
> index 210fea994..42c0f83b7 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 (!IS_ERR(rng)) {
> +			while (len) {
> +				bytes = hwrng_get_data(rng, _buf, len, 1);
> +				if (!bytes)
> +					goto sw_fallback;

hwrng_get_data() looks like it could return a negative error code.

Sascha


-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 2/3] lib: random: get_random_bytes from HWRNG if present
  2017-03-17 12:30   ` Sascha Hauer
@ 2017-03-17 14:30     ` Oleksij Rempel
  0 siblings, 0 replies; 6+ messages in thread
From: Oleksij Rempel @ 2017-03-17 14:30 UTC (permalink / raw)
  To: barebox


[-- Attachment #1.1.1: Type: text/plain, Size: 1301 bytes --]

Am 17.03.2017 um 13:30 schrieb Sascha Hauer:
> On Thu, Mar 16, 2017 at 06:31:45AM +0100, Oleksij Rempel wrote:
>> From: Steffen Trumtrar <s.trumtrar@pengutronix.de>
>>
>> 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@pengutronix.de>
>> Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
>> ---
>>  lib/random.c | 19 +++++++++++++++++++
>>  1 file changed, 19 insertions(+)
>>
>> diff --git a/lib/random.c b/lib/random.c
>> index 210fea994..42c0f83b7 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 (!IS_ERR(rng)) {
>> +			while (len) {
>> +				bytes = hwrng_get_data(rng, _buf, len, 1);
>> +				if (!bytes)
>> +					goto sw_fallback;
> 
> hwrng_get_data() looks like it could return a negative error code.

hm.. good point.


-- 
Regards,
Oleksij


[-- Attachment #1.2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 213 bytes --]

[-- Attachment #2: Type: text/plain, Size: 149 bytes --]

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2017-03-17 14:31 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-03-16  5:31 [PATCH v2 0/3] upstream hwrng framework Oleksij Rempel
2017-03-16  5:31 ` [PATCH v2 1/3] drivers: add simple hw_random implementation Oleksij Rempel
2017-03-16  5:31 ` [PATCH v2 2/3] lib: random: get_random_bytes from HWRNG if present Oleksij Rempel
2017-03-17 12:30   ` Sascha Hauer
2017-03-17 14:30     ` Oleksij Rempel
2017-03-16  5:31 ` [PATCH v2 3/3] caamrng: port to hwrng framework Oleksij Rempel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox