diff mbox series

[v2] mtd: otp: Put factory OTP/NVRAM into the entropy pool

Message ID 20230603183159.3566505-1-linus.walleij@linaro.org
State Changes Requested
Headers show
Series [v2] mtd: otp: Put factory OTP/NVRAM into the entropy pool | expand

Commit Message

Linus Walleij June 3, 2023, 6:31 p.m. UTC
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: Michael Walle <michael@walle.cc>
Acked-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
---
ChangeLog v1->v2:
- Drop the conditional to return as many bytes as requested
  to add the OTP to the entropy pool: it doesn't matter
  whatsoever in this case.
- Collected Jason's ACK.

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 | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

Comments

Jason A. Donenfeld June 4, 2023, 11:32 a.m. UTC | #1
On Sat, Jun 3, 2023 at 8:32 PM Linus Walleij <linus.walleij@linaro.org> 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: Michael Walle <michael@walle.cc>
> Acked-by: Jason A. Donenfeld <Jason@zx2c4.com>
> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
> ---
> ChangeLog v1->v2:
> - Drop the conditional to return as many bytes as requested
>   to add the OTP to the entropy pool: it doesn't matter
>   whatsoever in this case.
> - Collected Jason's ACK.
>
> 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 | 19 +++++++++++++++++++
>  1 file changed, 19 insertions(+)
>
> diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
> index 60670b2f70b9..d03412a82549 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,24 @@ 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;
> +                       }
> +                       add_device_randomness(otp, size);

My suggestion was to use `err` here, not `size`. But it's not like it
really matters much either way.
diff mbox series

Patch

diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
index 60670b2f70b9..d03412a82549 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,24 @@  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;
+			}
+			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)) {