1 /* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
3 * Test interface for Jitter RNG.
5 * Copyright (C) 2023, Stephan Mueller <smueller@chronox.de>
8 #include <linux/debugfs.h>
9 #include <linux/module.h>
10 #include <linux/uaccess.h>
12 #include "jitterentropy.h"
14 #define JENT_TEST_RINGBUFFER_SIZE (1<<10)
15 #define JENT_TEST_RINGBUFFER_MASK (JENT_TEST_RINGBUFFER_SIZE - 1)
18 u32 jent_testing_rb[JENT_TEST_RINGBUFFER_SIZE];
21 atomic_t jent_testing_enabled;
23 wait_queue_head_t read_wait;
26 static struct dentry *jent_raw_debugfs_root = NULL;
28 /*************************** Generic Data Handling ****************************/
32 * 0 ==> No boot test, gathering of runtime data allowed
33 * 1 ==> Boot test enabled and ready for collecting data, gathering runtime
35 * 2 ==> Boot test completed and disabled, gathering of runtime data is
39 static void jent_testing_reset(struct jent_testing *data)
43 spin_lock_irqsave(&data->lock, flags);
45 atomic_set(&data->rb_writer, 0);
46 spin_unlock_irqrestore(&data->lock, flags);
49 static void jent_testing_data_init(struct jent_testing *data, u32 boot)
52 * The boot time testing implies we have a running test. If the
53 * caller wants to clear it, he has to unset the boot_test flag
54 * at runtime via sysfs to enable regular runtime testing
59 jent_testing_reset(data);
60 atomic_set(&data->jent_testing_enabled, 1);
61 pr_warn("Enabling data collection\n");
64 static void jent_testing_fini(struct jent_testing *data, u32 boot)
66 /* If we have boot data, we do not reset yet to allow data to be read */
70 atomic_set(&data->jent_testing_enabled, 0);
71 jent_testing_reset(data);
72 pr_warn("Disabling data collection\n");
75 static bool jent_testing_store(struct jent_testing *data, u32 value,
80 if (!atomic_read(&data->jent_testing_enabled) && (*boot != 1))
83 spin_lock_irqsave(&data->lock, flags);
86 * Disable entropy testing for boot time testing after ring buffer
90 if (((u32)atomic_read(&data->rb_writer)) >
91 JENT_TEST_RINGBUFFER_SIZE) {
93 pr_warn_once("One time data collection test disabled\n");
94 spin_unlock_irqrestore(&data->lock, flags);
98 if (atomic_read(&data->rb_writer) == 1)
99 pr_warn("One time data collection test enabled\n");
102 data->jent_testing_rb[((u32)atomic_read(&data->rb_writer)) &
103 JENT_TEST_RINGBUFFER_MASK] = value;
104 atomic_inc(&data->rb_writer);
106 spin_unlock_irqrestore(&data->lock, flags);
108 if (wq_has_sleeper(&data->read_wait))
109 wake_up_interruptible(&data->read_wait);
114 static bool jent_testing_have_data(struct jent_testing *data)
116 return ((((u32)atomic_read(&data->rb_writer)) &
117 JENT_TEST_RINGBUFFER_MASK) !=
118 (data->rb_reader & JENT_TEST_RINGBUFFER_MASK));
121 static int jent_testing_reader(struct jent_testing *data, u32 *boot,
122 u8 *outbuf, u32 outbuflen)
125 int collected_data = 0;
127 jent_testing_data_init(data, *boot);
130 u32 writer = (u32)atomic_read(&data->rb_writer);
132 spin_lock_irqsave(&data->lock, flags);
134 /* We have no data or reached the writer. */
135 if (!writer || (writer == data->rb_reader)) {
137 spin_unlock_irqrestore(&data->lock, flags);
140 * Now we gathered all boot data, enable regular data
148 wait_event_interruptible(data->read_wait,
149 jent_testing_have_data(data));
150 if (signal_pending(current)) {
151 collected_data = -ERESTARTSYS;
158 /* We copy out word-wise */
159 if (outbuflen < sizeof(u32)) {
160 spin_unlock_irqrestore(&data->lock, flags);
164 memcpy(outbuf, &data->jent_testing_rb[data->rb_reader],
168 spin_unlock_irqrestore(&data->lock, flags);
170 outbuf += sizeof(u32);
171 outbuflen -= sizeof(u32);
172 collected_data += sizeof(u32);
176 jent_testing_fini(data, *boot);
177 return collected_data;
180 static int jent_testing_extract_user(struct file *file, char __user *buf,
181 size_t nbytes, loff_t *ppos,
182 int (*reader)(u8 *outbuf, u32 outbuflen))
184 u8 *tmp, *tmp_aligned;
185 int ret = 0, large_request = (nbytes > 256);
191 * The intention of this interface is for collecting at least
192 * 1000 samples due to the SP800-90B requirements. So, we make no
193 * effort in avoiding allocating more memory that actually needed
194 * by the user. Hence, we allocate sufficient memory to always hold
195 * that amount of data.
197 tmp = kmalloc(JENT_TEST_RINGBUFFER_SIZE + sizeof(u32), GFP_KERNEL);
201 tmp_aligned = PTR_ALIGN(tmp, sizeof(u32));
206 if (large_request && need_resched()) {
207 if (signal_pending(current)) {
215 i = min_t(int, nbytes, JENT_TEST_RINGBUFFER_SIZE);
216 i = reader(tmp_aligned, i);
222 if (copy_to_user(buf, tmp_aligned, i)) {
232 kfree_sensitive(tmp);
240 /************** Raw High-Resolution Timer Entropy Data Handling **************/
242 static u32 boot_raw_hires_test = 0;
243 module_param(boot_raw_hires_test, uint, 0644);
244 MODULE_PARM_DESC(boot_raw_hires_test,
245 "Enable gathering boot time high resolution timer entropy of the first Jitter RNG entropy events");
247 static struct jent_testing jent_raw_hires = {
249 .rb_writer = ATOMIC_INIT(0),
250 .lock = __SPIN_LOCK_UNLOCKED(jent_raw_hires.lock),
251 .read_wait = __WAIT_QUEUE_HEAD_INITIALIZER(jent_raw_hires.read_wait)
254 int jent_raw_hires_entropy_store(__u32 value)
256 return jent_testing_store(&jent_raw_hires, value, &boot_raw_hires_test);
258 EXPORT_SYMBOL(jent_raw_hires_entropy_store);
260 static int jent_raw_hires_entropy_reader(u8 *outbuf, u32 outbuflen)
262 return jent_testing_reader(&jent_raw_hires, &boot_raw_hires_test,
266 static ssize_t jent_raw_hires_read(struct file *file, char __user *to,
267 size_t count, loff_t *ppos)
269 return jent_testing_extract_user(file, to, count, ppos,
270 jent_raw_hires_entropy_reader);
273 static const struct file_operations jent_raw_hires_fops = {
274 .owner = THIS_MODULE,
275 .read = jent_raw_hires_read,
278 /******************************* Initialization *******************************/
280 void jent_testing_init(void)
282 jent_raw_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
284 debugfs_create_file_unsafe("jent_raw_hires", 0400,
285 jent_raw_debugfs_root, NULL,
286 &jent_raw_hires_fops);
288 EXPORT_SYMBOL(jent_testing_init);
290 void jent_testing_exit(void)
292 debugfs_remove_recursive(jent_raw_debugfs_root);
294 EXPORT_SYMBOL(jent_testing_exit);