Linux-mtd Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] mtd: otp: Put factory OTP/NVRAM into the entropy pool
@ 2023-06-02 21:23 Linus Walleij
  2023-06-02 23:24 ` Jason A. Donenfeld
  2023-06-03  0:14 ` Michael Walle
  0 siblings, 2 replies; 3+ messages in thread
From: Linus Walleij @ 2023-06-02 21:23 UTC (permalink / raw)
  To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra
  Cc: linux-mtd, Linus Walleij, Jason A . Donenfeld, Michael Walle

The factory OTP, if supported, contains factory-programmed
information such as typically the serial number or production
week for the chip.

As this is device-unique information, submit it into the
system entropy pool.

This does not count as improvement of the entropy as such
but in practice it makes it a bit more random to mix in these
numbers.

Cc: Jason A. Donenfeld <Jason@zx2c4.com>
Cc: Michael Walle <michael@walle.cc>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
---
This is similar to the patch I made to add MMC/SD-card serial
numbers to randomness, just with raw NOR flash.
---
 drivers/mtd/mtdcore.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
index 60670b2f70b9..efc8cf76dc60 100644
--- a/drivers/mtd/mtdcore.c
+++ b/drivers/mtd/mtdcore.c
@@ -23,6 +23,7 @@
 #include <linux/idr.h>
 #include <linux/backing-dev.h>
 #include <linux/gfp.h>
+#include <linux/random.h>
 #include <linux/slab.h>
 #include <linux/reboot.h>
 #include <linux/leds.h>
@@ -966,6 +967,25 @@ static int mtd_otp_nvmem_add(struct mtd_info *mtd)
 		}
 
 		if (size > 0) {
+			/*
+			 * The factory OTP contains thing such as a unique serial
+			 * number and is small, so let's read it out and put it
+			 * into the entropy pool.
+			 */
+			void *otp;
+
+			otp = kmalloc(size, GFP_KERNEL);
+			if (!otp)
+				return -ENOMEM;
+			err = mtd_nvmem_fact_otp_reg_read(mtd, 0, otp, size);
+			if (err < 0) {
+				kfree(otp);
+				return err;
+			}
+			if (err == size)
+				add_device_randomness(otp, size);
+			kfree(otp);
+
 			nvmem = mtd_otp_nvmem_register(mtd, "factory-otp", size,
 						       mtd_nvmem_fact_otp_reg_read);
 			if (IS_ERR(nvmem)) {
-- 
2.40.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

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

* Re: [PATCH] mtd: otp: Put factory OTP/NVRAM into the entropy pool
  2023-06-02 21:23 [PATCH] mtd: otp: Put factory OTP/NVRAM into the entropy pool Linus Walleij
@ 2023-06-02 23:24 ` Jason A. Donenfeld
  2023-06-03  0:14 ` Michael Walle
  1 sibling, 0 replies; 3+ messages in thread
From: Jason A. Donenfeld @ 2023-06-02 23:24 UTC (permalink / raw)
  To: Linus Walleij
  Cc: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra, linux-mtd,
	Michael Walle

On Fri, Jun 02, 2023 at 11:23:18PM +0200, Linus Walleij wrote:
> The factory OTP, if supported, contains factory-programmed
> information such as typically the serial number or production
> week for the chip.
> 
> As this is device-unique information, submit it into the
> system entropy pool.
> 
> This does not count as improvement of the entropy as such
> but in practice it makes it a bit more random to mix in these
> numbers.
> 
> Cc: Jason A. Donenfeld <Jason@zx2c4.com>
> Cc: Michael Walle <michael@walle.cc>
> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>

Acked-by: Jason A. Donenfeld <Jason@zx2c4.com>

Great idea!

Just one question below. Feel free to disregard if it's silly.

> ---
> This is similar to the patch I made to add MMC/SD-card serial
> numbers to randomness, just with raw NOR flash.
> ---
>  drivers/mtd/mtdcore.c | 20 ++++++++++++++++++++
>  1 file changed, 20 insertions(+)
> 
> diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
> index 60670b2f70b9..efc8cf76dc60 100644
> --- a/drivers/mtd/mtdcore.c
> +++ b/drivers/mtd/mtdcore.c
> @@ -23,6 +23,7 @@
>  #include <linux/idr.h>
>  #include <linux/backing-dev.h>
>  #include <linux/gfp.h>
> +#include <linux/random.h>
>  #include <linux/slab.h>
>  #include <linux/reboot.h>
>  #include <linux/leds.h>
> @@ -966,6 +967,25 @@ static int mtd_otp_nvmem_add(struct mtd_info *mtd)
>  		}
>  
>  		if (size > 0) {
> +			/*
> +			 * The factory OTP contains thing such as a unique serial
> +			 * number and is small, so let's read it out and put it
> +			 * into the entropy pool.
> +			 */
> +			void *otp;
> +
> +			otp = kmalloc(size, GFP_KERNEL);
> +			if (!otp)
> +				return -ENOMEM;
> +			err = mtd_nvmem_fact_otp_reg_read(mtd, 0, otp, size);
> +			if (err < 0) {
> +				kfree(otp);
> +				return err;
> +			}
> +			if (err == size)
> +				add_device_randomness(otp, size);

What if instead you just did `add_device_randomness(otp, err)`, without
the conditional checking that `err == size`? You check for the actual
error case above (`err < 0`), and so in the case that
mtd_nvmem_fact_otp_reg_read() returns less than you thought it should,
it can't hurt to still add it to the rng.

Jason

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

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

* Re: [PATCH] mtd: otp: Put factory OTP/NVRAM into the entropy pool
  2023-06-02 21:23 [PATCH] mtd: otp: Put factory OTP/NVRAM into the entropy pool Linus Walleij
  2023-06-02 23:24 ` Jason A. Donenfeld
@ 2023-06-03  0:14 ` Michael Walle
  1 sibling, 0 replies; 3+ messages in thread
From: Michael Walle @ 2023-06-03  0:14 UTC (permalink / raw)
  To: Linus Walleij, Miquel Raynal, Richard Weinberger,
	Vignesh Raghavendra
  Cc: linux-mtd, Jason A . Donenfeld

Am 3. Juni 2023 05:23:18 GMT+08:00 schrieb Linus Walleij <linus.walleij@linaro.org>:
>The factory OTP, if supported, contains factory-programmed
>information such as typically the serial number or production
>week for the chip.
>
>As this is device-unique information, submit it into the
>system entropy pool.
>
>This does not count as improvement of the entropy as such
>but in practice it makes it a bit more random to mix in these
>numbers.

Please note, that not every flash will have some kind of unique identifier, some won't have that section populated at all or some OEM stuff in it. But I guess that's alright.

-michael 
[I'm still on vacation and just have access to a mobile phone, so sorry for any incorrect formatting] 

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

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

end of thread, other threads:[~2023-06-03  0:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-06-02 21:23 [PATCH] mtd: otp: Put factory OTP/NVRAM into the entropy pool Linus Walleij
2023-06-02 23:24 ` Jason A. Donenfeld
2023-06-03  0:14 ` Michael Walle

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