Skip to contentSkip to conversation

ath9k: feeding entropy in kernel from ADC capture

PATCH Superseded [email protected] 2 participants 3 messages Sep 30, 2015 – Oct 9, 2015 Qualcomm Atheros ATH9K wireless driver

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 ath9k: feeding entropy in kernel from ADC capture

From: Miaoqing Pan <[email protected]>

This patch is derived from
commit 6301566e0b2d ("ath9k: export HW random number generator"),

We evaluated the entropy of the ADC data on QCA9531, QCA9561, QCA955x, and AR9340, and it has sufficient quality random data (at least 10 bits and up to 22 bits of min-entropy for a 32-bit value). We conservatively assume the min-entropy is 10 bits out of 32 bits. Thus, ATH9K_RNG_BUF_SIZE is set to 320 (u32) i.e., 1.25 kilobytes of data is inserted to fill up the pool as soon as the entropy counter becomes 896/4096 (set by random.c). Since ADC was not designed to be a dedicated HW RNG, we do not want to bind it to /dev/hwrng framework directly. This patch feeds the entropy directly from the WiFi driver to the input pool. The ADC register output is only used as a seed for the Linux entropy pool. No conditioning is needed, since all the conditioning is performed by the pool itself.

---
 drivers/net/wireless/ath/ath9k/Kconfig  |  11 ++++
 drivers/net/wireless/ath/ath9k/Makefile |   1 +
 drivers/net/wireless/ath/ath9k/ath9k.h  |  22 +++++++
 drivers/net/wireless/ath/ath9k/main.c   |   4 ++
 drivers/net/wireless/ath/ath9k/rng.c    | 106 ++++++++++++++++++++++++++++++++
 5 files changed, 144 insertions(+)
 create mode 100644 drivers/net/wireless/ath/ath9k/rng.c
drivers/net/wireless/ath/ath9k/Kconfig +11 -0
index fee0cad..40fa915 100644
@@ -176,3 +176,14 @@ config ATH9K_HTC_DEBUGFS
 	depends on ATH9K_HTC && DEBUG_FS
 	---help---
 	  Say Y, if you need access to ath9k_htc's statistics.
+
+config ATH9K_HWRNG
+	bool "Random number generator support"
+	depends on ATH9K && (HW_RANDOM = y || HW_RANDOM = ATH9K)
+	default y
+	---help---
+	  This option incorporates the ADC register output as a source of
+	  randomness into Linux entropy pool (/dev/urandom and /dev/random)
+
+	  Say Y, feeds the entropy directly from the WiFi driver to the input
+	  pool.
drivers/net/wireless/ath/ath9k/Makefile +1 -0
index ecda613..76f9dc3 100644
@@ -15,6 +15,7 @@ ath9k-$(CONFIG_ATH9K_DFS_DEBUGFS) += dfs_debug.o
 ath9k-$(CONFIG_ATH9K_DFS_CERTIFIED) += dfs.o
 ath9k-$(CONFIG_ATH9K_TX99) += tx99.o
 ath9k-$(CONFIG_ATH9K_WOW) += wow.o
+ath9k-$(CONFIG_ATH9K_HWRNG) += rng.o
 
 ath9k-$(CONFIG_ATH9K_DEBUGFS) += debug.o
 
drivers/net/wireless/ath/ath9k/ath9k.h +22 -0
index c85c479..cd74f68 100644
@@ -23,6 +23,7 @@
 #include <linux/leds.h>
 #include <linux/completion.h>
 #include <linux/time.h>
+#include <linux/hw_random.h>
 
 #include "common.h"
 #include "debug.h"
@@ -1039,6 +1040,11 @@ struct ath_softc {
 	u32 wow_intr_before_sleep;
 	bool force_wow;
 #endif
+
+#ifdef CONFIG_ATH9K_HWRNG
+	u32 rng_last;
+	struct task_struct *rng_task;
+#endif
 };
 
 /********/
@@ -1061,6 +1067,22 @@ static inline int ath9k_tx99_send(struct ath_softc *sc,
 }
 #endif /* CONFIG_ATH9K_TX99 */
 
+/***************************/
+/* Random Number Generator */
+/***************************/
+#ifdef CONFIG_ATH9K_HWRNG
+void ath9k_rng_start(struct ath_softc *sc);
+void ath9k_rng_stop(struct ath_softc *sc);
+#else
+static inline void ath9k_rng_start(struct ath_softc *sc)
+{
+}
+
+static inline void ath9k_rng_stop(struct ath_softc *sc)
+{
+}
+#endif
+
 static inline void ath_read_cachesize(struct ath_common *common, int *csz)
 {
 	common->bus_ops->read_cachesize(common, csz);
drivers/net/wireless/ath/ath9k/main.c +4 -0
index 323eb33..39ba17f 100644
@@ -739,6 +739,8 @@ static int ath9k_start(struct ieee80211_hw *hw)
 
 	ath9k_ps_restore(sc);
 
+	ath9k_rng_start(sc);
+
 	return 0;
 }
 
@@ -828,6 +830,8 @@ static void ath9k_stop(struct ieee80211_hw *hw)
 
 	ath9k_deinit_channel_context(sc);
 
+	ath9k_rng_stop(sc);
+
 	mutex_lock(&sc->mutex);
 
 	ath_cancel_work(sc);
drivers/net/wireless/ath/ath9k/rng.c new file +106 -0

106 lines not on this page · raw message

Signature · 1 line
1.9.1
#1 lore raw
Quoted 44 lines · On 2015-09-30 05:09, [email protected] wrote:

From: Miaoqing Pan <[email protected]>

This patch is derived from
commit 6301566e0b2d ("ath9k: export HW random number generator"),

We evaluated the entropy of the ADC data on QCA9531, QCA9561, QCA955x, and AR9340, and it has sufficient quality random data (at least 10 bits and up to 22 bits of min-entropy for a 32-bit value). We conservatively assume the min-entropy is 10 bits out of 32 bits. Thus, ATH9K_RNG_BUF_SIZE is set to 320 (u32) i.e., 1.25 kilobytes of data is inserted to fill up the pool as soon as the entropy counter becomes 896/4096 (set by random.c). Since ADC was not designed to be a dedicated HW RNG, we do not want to bind it to /dev/hwrng framework directly. This patch feeds the entropy directly from the WiFi driver to the input pool. The ADC register output is only used as a seed for the Linux entropy pool. No conditioning is needed, since all the conditioning is performed by the pool itself.

---
 drivers/net/wireless/ath/ath9k/Kconfig  |  11 ++++
 drivers/net/wireless/ath/ath9k/Makefile |   1 +
 drivers/net/wireless/ath/ath9k/ath9k.h  |  22 +++++++
 drivers/net/wireless/ath/ath9k/main.c   |   4 ++
 drivers/net/wireless/ath/ath9k/rng.c    | 106 ++++++++++++++++++++++++++++++++
 5 files changed, 144 insertions(+)
 create mode 100644 drivers/net/wireless/ath/ath9k/rng.c
drivers/net/wireless/ath/ath9k/rng.c new file +10 -0
new file mode 100644
index 0000000..93a7485
[...]
+static int ath9k_rng_kthread(void *data)
+{
+	int bytes_read;
+	struct ath_softc *sc = data;
+	u32 *rng_buf;
+
+	rng_buf = kmalloc_array(ATH9K_RNG_BUF_SIZE, sizeof(u32), GFP_KERNEL);
+	if (!rng_buf)
+		goto out;
+

drivers/net/wireless/ath/ath9k/rng.c line 77

Quoted 10 lines
drivers/net/wireless/ath/ath9k/rng.c new file +10 -0
+	while (!kthread_should_stop()) {
+		bytes_read = ath9k_rng_data_read(sc, rng_buf,
+						 ATH9K_RNG_BUF_SIZE);
+		if (!bytes_read) {
+			msleep_interruptible(10);
+			continue;
+		}
+
+		add_hwgenerator_randomness((void *)rng_buf, bytes_read,
+					   ATH9K_RNG_ENTROPY(bytes_read));

Polling every 10 milliseconds seems a bit excessive to me. Think about all those useless wakeups per second that this will produce on battery powered devices.
How about polling more data without sleeping when the thread starts, and then afterwards only poll once every few seconds?

- Felix

#2 lore raw

Thanks Felix. You mentioned 'Polling every 10 milliseconds' is not right, the kthread will always sleep until when the condition 'ENTROPY_BITS(&input_pool) <= random_write_wakeup_bits'(add_hwgenerator_randomness()) is true. And ath9k_rng_data_read() almost w/o failure, I will add unlikely in patch v3.

- Miaoqing

Quoted 69 lines · On 2015-09-30 05:09, [email protected] wrote:
Quoted 50 lines

From: Miaoqing Pan <[email protected]>

This patch is derived from
commit 6301566e0b2d ("ath9k: export HW random number generator"),

We evaluated the entropy of the ADC data on QCA9531, QCA9561, QCA955x, and AR9340, and it has sufficient quality random data (at least 10 bits and up to 22 bits of min-entropy for a 32-bit value). We conservatively assume the min-entropy is 10 bits out of 32 bits. Thus, ATH9K_RNG_BUF_SIZE
is set to 320 (u32) i.e., 1.25 kilobytes of data is inserted to fill up the pool as soon as the entropy counter becomes 896/4096 (set by random.c).
Since ADC was not designed to be a dedicated HW RNG, we do not want to bind
it to /dev/hwrng framework directly. This patch feeds the entropy directly
from the WiFi driver to the input pool. The ADC register output is only used as a seed for the Linux entropy pool. No conditioning is needed, since all the conditioning is performed by the pool itself.

---
 drivers/net/wireless/ath/ath9k/Kconfig  |  11 ++++
 drivers/net/wireless/ath/ath9k/Makefile |   1 +
 drivers/net/wireless/ath/ath9k/ath9k.h  |  22 +++++++
 drivers/net/wireless/ath/ath9k/main.c   |   4 ++
 drivers/net/wireless/ath/ath9k/rng.c    | 106
++++++++++++++++++++++++++++++++
 5 files changed, 144 insertions(+)
 create mode 100644 drivers/net/wireless/ath/ath9k/rng.c
drivers/net/wireless/ath/ath9k/rng.c new file +10 -0
new file mode 100644
index 0000000..93a7485
[...]
+static int ath9k_rng_kthread(void *data)
+{
+	int bytes_read;
+	struct ath_softc *sc = data;
+	u32 *rng_buf;
+
+	rng_buf = kmalloc_array(ATH9K_RNG_BUF_SIZE, sizeof(u32), GFP_KERNEL);
+	if (!rng_buf)
+		goto out;
+

drivers/net/wireless/ath/ath9k/rng.c line 77

Quoted 10 lines
drivers/net/wireless/ath/ath9k/rng.c new file +10 -0
+	while (!kthread_should_stop()) {
+		bytes_read = ath9k_rng_data_read(sc, rng_buf,
+						 ATH9K_RNG_BUF_SIZE);
+		if (!bytes_read) {
+			msleep_interruptible(10);
+			continue;
+		}
+
+		add_hwgenerator_randomness((void *)rng_buf, bytes_read,
+					   ATH9K_RNG_ENTROPY(bytes_read));

Polling every 10 milliseconds seems a bit excessive to me. Think about all those useless wakeups per second that this will produce on battery powered devices.
How about polling more data without sleeping when the thread starts, and then afterwards only poll once every few seconds?

- Felix