Skip to content

Commit 3b270fa

Browse files
Linus Walleijmiquelraynal
authored andcommitted
mtd: otp: Put factory OTP/NVRAM into the entropy pool
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> Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com> Link: https://lore.kernel.org/linux-mtd/20230606142931.3721374-1-linus.walleij@linaro.org
1 parent 6db02fd commit 3b270fa

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

drivers/mtd/mtdcore.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <linux/idr.h>
2424
#include <linux/backing-dev.h>
2525
#include <linux/gfp.h>
26+
#include <linux/random.h>
2627
#include <linux/slab.h>
2728
#include <linux/reboot.h>
2829
#include <linux/leds.h>
@@ -966,6 +967,24 @@ static int mtd_otp_nvmem_add(struct mtd_info *mtd)
966967
}
967968

968969
if (size > 0) {
970+
/*
971+
* The factory OTP contains thing such as a unique serial
972+
* number and is small, so let's read it out and put it
973+
* into the entropy pool.
974+
*/
975+
void *otp;
976+
977+
otp = kmalloc(size, GFP_KERNEL);
978+
if (!otp)
979+
return -ENOMEM;
980+
err = mtd_nvmem_fact_otp_reg_read(mtd, 0, otp, size);
981+
if (err < 0) {
982+
kfree(otp);
983+
return err;
984+
}
985+
add_device_randomness(otp, err);
986+
kfree(otp);
987+
969988
nvmem = mtd_otp_nvmem_register(mtd, "factory-otp", size,
970989
mtd_nvmem_fact_otp_reg_read);
971990
if (IS_ERR(nvmem)) {

0 commit comments

Comments
 (0)