jitterentropy-testing.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
  2. /*
  3. * Test interface for Jitter RNG.
  4. *
  5. * Copyright (C) 2023, Stephan Mueller <smueller@chronox.de>
  6. */
  7. #include <linux/debugfs.h>
  8. #include <linux/module.h>
  9. #include <linux/uaccess.h>
  10. #include "jitterentropy.h"
  11. #define JENT_TEST_RINGBUFFER_SIZE (1<<10)
  12. #define JENT_TEST_RINGBUFFER_MASK (JENT_TEST_RINGBUFFER_SIZE - 1)
  13. struct jent_testing {
  14. u32 jent_testing_rb[JENT_TEST_RINGBUFFER_SIZE];
  15. u32 rb_reader;
  16. atomic_t rb_writer;
  17. atomic_t jent_testing_enabled;
  18. spinlock_t lock;
  19. wait_queue_head_t read_wait;
  20. };
  21. static struct dentry *jent_raw_debugfs_root = NULL;
  22. /*************************** Generic Data Handling ****************************/
  23. /*
  24. * boot variable:
  25. * 0 ==> No boot test, gathering of runtime data allowed
  26. * 1 ==> Boot test enabled and ready for collecting data, gathering runtime
  27. * data is disabled
  28. * 2 ==> Boot test completed and disabled, gathering of runtime data is
  29. * disabled
  30. */
  31. static void jent_testing_reset(struct jent_testing *data)
  32. {
  33. unsigned long flags;
  34. spin_lock_irqsave(&data->lock, flags);
  35. data->rb_reader = 0;
  36. atomic_set(&data->rb_writer, 0);
  37. spin_unlock_irqrestore(&data->lock, flags);
  38. }
  39. static void jent_testing_data_init(struct jent_testing *data, u32 boot)
  40. {
  41. /*
  42. * The boot time testing implies we have a running test. If the
  43. * caller wants to clear it, he has to unset the boot_test flag
  44. * at runtime via sysfs to enable regular runtime testing
  45. */
  46. if (boot)
  47. return;
  48. jent_testing_reset(data);
  49. atomic_set(&data->jent_testing_enabled, 1);
  50. pr_warn("Enabling data collection\n");
  51. }
  52. static void jent_testing_fini(struct jent_testing *data, u32 boot)
  53. {
  54. /* If we have boot data, we do not reset yet to allow data to be read */
  55. if (boot)
  56. return;
  57. atomic_set(&data->jent_testing_enabled, 0);
  58. jent_testing_reset(data);
  59. pr_warn("Disabling data collection\n");
  60. }
  61. static bool jent_testing_store(struct jent_testing *data, u32 value,
  62. u32 *boot)
  63. {
  64. unsigned long flags;
  65. if (!atomic_read(&data->jent_testing_enabled) && (*boot != 1))
  66. return false;
  67. spin_lock_irqsave(&data->lock, flags);
  68. /*
  69. * Disable entropy testing for boot time testing after ring buffer
  70. * is filled.
  71. */
  72. if (*boot) {
  73. if (((u32)atomic_read(&data->rb_writer)) >
  74. JENT_TEST_RINGBUFFER_SIZE) {
  75. *boot = 2;
  76. pr_warn_once("One time data collection test disabled\n");
  77. spin_unlock_irqrestore(&data->lock, flags);
  78. return false;
  79. }
  80. if (atomic_read(&data->rb_writer) == 1)
  81. pr_warn("One time data collection test enabled\n");
  82. }
  83. data->jent_testing_rb[((u32)atomic_read(&data->rb_writer)) &
  84. JENT_TEST_RINGBUFFER_MASK] = value;
  85. atomic_inc(&data->rb_writer);
  86. spin_unlock_irqrestore(&data->lock, flags);
  87. if (wq_has_sleeper(&data->read_wait))
  88. wake_up_interruptible(&data->read_wait);
  89. return true;
  90. }
  91. static bool jent_testing_have_data(struct jent_testing *data)
  92. {
  93. return ((((u32)atomic_read(&data->rb_writer)) &
  94. JENT_TEST_RINGBUFFER_MASK) !=
  95. (data->rb_reader & JENT_TEST_RINGBUFFER_MASK));
  96. }
  97. static int jent_testing_reader(struct jent_testing *data, u32 *boot,
  98. u8 *outbuf, u32 outbuflen)
  99. {
  100. unsigned long flags;
  101. int collected_data = 0;
  102. jent_testing_data_init(data, *boot);
  103. while (outbuflen) {
  104. u32 writer = (u32)atomic_read(&data->rb_writer);
  105. spin_lock_irqsave(&data->lock, flags);
  106. /* We have no data or reached the writer. */
  107. if (!writer || (writer == data->rb_reader)) {
  108. spin_unlock_irqrestore(&data->lock, flags);
  109. /*
  110. * Now we gathered all boot data, enable regular data
  111. * collection.
  112. */
  113. if (*boot) {
  114. *boot = 0;
  115. goto out;
  116. }
  117. wait_event_interruptible(data->read_wait,
  118. jent_testing_have_data(data));
  119. if (signal_pending(current)) {
  120. collected_data = -ERESTARTSYS;
  121. goto out;
  122. }
  123. continue;
  124. }
  125. /* We copy out word-wise */
  126. if (outbuflen < sizeof(u32)) {
  127. spin_unlock_irqrestore(&data->lock, flags);
  128. goto out;
  129. }
  130. memcpy(outbuf, &data->jent_testing_rb[data->rb_reader],
  131. sizeof(u32));
  132. data->rb_reader++;
  133. spin_unlock_irqrestore(&data->lock, flags);
  134. outbuf += sizeof(u32);
  135. outbuflen -= sizeof(u32);
  136. collected_data += sizeof(u32);
  137. }
  138. out:
  139. jent_testing_fini(data, *boot);
  140. return collected_data;
  141. }
  142. static int jent_testing_extract_user(struct file *file, char __user *buf,
  143. size_t nbytes, loff_t *ppos,
  144. int (*reader)(u8 *outbuf, u32 outbuflen))
  145. {
  146. u8 *tmp, *tmp_aligned;
  147. int ret = 0, large_request = (nbytes > 256);
  148. if (!nbytes)
  149. return 0;
  150. /*
  151. * The intention of this interface is for collecting at least
  152. * 1000 samples due to the SP800-90B requirements. So, we make no
  153. * effort in avoiding allocating more memory that actually needed
  154. * by the user. Hence, we allocate sufficient memory to always hold
  155. * that amount of data.
  156. */
  157. tmp = kmalloc(JENT_TEST_RINGBUFFER_SIZE + sizeof(u32), GFP_KERNEL);
  158. if (!tmp)
  159. return -ENOMEM;
  160. tmp_aligned = PTR_ALIGN(tmp, sizeof(u32));
  161. while (nbytes) {
  162. int i;
  163. if (large_request && need_resched()) {
  164. if (signal_pending(current)) {
  165. if (ret == 0)
  166. ret = -ERESTARTSYS;
  167. break;
  168. }
  169. schedule();
  170. }
  171. i = min_t(int, nbytes, JENT_TEST_RINGBUFFER_SIZE);
  172. i = reader(tmp_aligned, i);
  173. if (i <= 0) {
  174. if (i < 0)
  175. ret = i;
  176. break;
  177. }
  178. if (copy_to_user(buf, tmp_aligned, i)) {
  179. ret = -EFAULT;
  180. break;
  181. }
  182. nbytes -= i;
  183. buf += i;
  184. ret += i;
  185. }
  186. kfree_sensitive(tmp);
  187. if (ret > 0)
  188. *ppos += ret;
  189. return ret;
  190. }
  191. /************** Raw High-Resolution Timer Entropy Data Handling **************/
  192. static u32 boot_raw_hires_test = 0;
  193. module_param(boot_raw_hires_test, uint, 0644);
  194. MODULE_PARM_DESC(boot_raw_hires_test,
  195. "Enable gathering boot time high resolution timer entropy of the first Jitter RNG entropy events");
  196. static struct jent_testing jent_raw_hires = {
  197. .rb_reader = 0,
  198. .rb_writer = ATOMIC_INIT(0),
  199. .lock = __SPIN_LOCK_UNLOCKED(jent_raw_hires.lock),
  200. .read_wait = __WAIT_QUEUE_HEAD_INITIALIZER(jent_raw_hires.read_wait)
  201. };
  202. int jent_raw_hires_entropy_store(__u32 value)
  203. {
  204. return jent_testing_store(&jent_raw_hires, value, &boot_raw_hires_test);
  205. }
  206. EXPORT_SYMBOL(jent_raw_hires_entropy_store);
  207. static int jent_raw_hires_entropy_reader(u8 *outbuf, u32 outbuflen)
  208. {
  209. return jent_testing_reader(&jent_raw_hires, &boot_raw_hires_test,
  210. outbuf, outbuflen);
  211. }
  212. static ssize_t jent_raw_hires_read(struct file *file, char __user *to,
  213. size_t count, loff_t *ppos)
  214. {
  215. return jent_testing_extract_user(file, to, count, ppos,
  216. jent_raw_hires_entropy_reader);
  217. }
  218. static const struct file_operations jent_raw_hires_fops = {
  219. .owner = THIS_MODULE,
  220. .read = jent_raw_hires_read,
  221. };
  222. /******************************* Initialization *******************************/
  223. void jent_testing_init(void)
  224. {
  225. jent_raw_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
  226. debugfs_create_file_unsafe("jent_raw_hires", 0400,
  227. jent_raw_debugfs_root, NULL,
  228. &jent_raw_hires_fops);
  229. }
  230. EXPORT_SYMBOL(jent_testing_init);
  231. void jent_testing_exit(void)
  232. {
  233. debugfs_remove_recursive(jent_raw_debugfs_root);
  234. }
  235. EXPORT_SYMBOL(jent_testing_exit);