Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

rtc: wm831x: Feed the write counter into device_add_randomness()

The tamper evident features of the RTC include the "write counter" which
is a pseudo-random number regenerated whenever we set the RTC. Since this
value is unpredictable it should provide some useful seeding to the random
number generator.

Only do this on boot since the goal is to seed the pool rather than add
useful entropy.

Signed-off-by: Mark Brown <broonie@opensource.wolfsonmicro.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
Cc: stable@vger.kernel.org

authored by

Mark Brown and committed by
Theodore Ts'o
9dccf55f 330e0a01

+23 -1
+23 -1
drivers/rtc/rtc-wm831x.c
··· 24 24 #include <linux/mfd/wm831x/core.h> 25 25 #include <linux/delay.h> 26 26 #include <linux/platform_device.h> 27 - 27 + #include <linux/random.h> 28 28 29 29 /* 30 30 * R16416 (0x4020) - RTC Write Counter ··· 95 95 struct rtc_device *rtc; 96 96 unsigned int alarm_enabled:1; 97 97 }; 98 + 99 + static void wm831x_rtc_add_randomness(struct wm831x *wm831x) 100 + { 101 + int ret; 102 + u16 reg; 103 + 104 + /* 105 + * The write counter contains a pseudo-random number which is 106 + * regenerated every time we set the RTC so it should be a 107 + * useful per-system source of entropy. 108 + */ 109 + ret = wm831x_reg_read(wm831x, WM831X_RTC_WRITE_COUNTER); 110 + if (ret >= 0) { 111 + reg = ret; 112 + add_device_randomness(&reg, sizeof(reg)); 113 + } else { 114 + dev_warn(wm831x->dev, "Failed to read RTC write counter: %d\n", 115 + ret); 116 + } 117 + } 98 118 99 119 /* 100 120 * Read current time and date in RTC ··· 450 430 dev_err(&pdev->dev, "Failed to request alarm IRQ %d: %d\n", 451 431 alm_irq, ret); 452 432 } 433 + 434 + wm831x_rtc_add_randomness(wm831x); 453 435 454 436 return 0; 455 437