jitterentropy-testing.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. u64 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, u64 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(u64)) {
  127. spin_unlock_irqrestore(&data->lock, flags);
  128. goto out;
  129. }
  130. memcpy(outbuf, &data->jent_testing_rb[data->rb_reader],
  131. sizeof(u64));
  132. data->rb_reader++;
  133. spin_unlock_irqrestore(&data->lock, flags);
  134. outbuf += sizeof(u64);
  135. outbuflen -= sizeof(u64);
  136. collected_data += sizeof(u64);
  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. However, due to
  153. * memory and performance constraints, it is not desirable to allocate
  154. * 8000 bytes of memory. Instead, we allocate space for only 125
  155. * samples, which will allow the user to collect all 1000 samples using
  156. * 8 calls to this interface.
  157. */
  158. tmp = kmalloc(125 * sizeof(u64) + sizeof(u64), GFP_KERNEL);
  159. if (!tmp)
  160. return -ENOMEM;
  161. tmp_aligned = PTR_ALIGN(tmp, sizeof(u64));
  162. while (nbytes) {
  163. int i;
  164. if (large_request && need_resched()) {
  165. if (signal_pending(current)) {
  166. if (ret == 0)
  167. ret = -ERESTARTSYS;
  168. break;
  169. }
  170. schedule();
  171. }
  172. i = min_t(int, nbytes, 125 * sizeof(u64));
  173. i = reader(tmp_aligned, i);
  174. if (i <= 0) {
  175. if (i < 0)
  176. ret = i;
  177. break;
  178. }
  179. if (copy_to_user(buf, tmp_aligned, i)) {
  180. ret = -EFAULT;
  181. break;
  182. }
  183. nbytes -= i;
  184. buf += i;
  185. ret += i;
  186. }
  187. kfree_sensitive(tmp);
  188. if (ret > 0)
  189. *ppos += ret;
  190. return ret;
  191. }
  192. /************** Raw High-Resolution Timer Entropy Data Handling **************/
  193. static u32 boot_raw_hires_test = 0;
  194. module_param(boot_raw_hires_test, uint, 0644);
  195. MODULE_PARM_DESC(boot_raw_hires_test,
  196. "Enable gathering boot time high resolution timer entropy of the first Jitter RNG entropy events");
  197. static struct jent_testing jent_raw_hires = {
  198. .rb_reader = 0,
  199. .rb_writer = ATOMIC_INIT(0),
  200. .lock = __SPIN_LOCK_UNLOCKED(jent_raw_hires.lock),
  201. .read_wait = __WAIT_QUEUE_HEAD_INITIALIZER(jent_raw_hires.read_wait)
  202. };
  203. int jent_raw_hires_entropy_store(__u64 value)
  204. {
  205. return jent_testing_store(&jent_raw_hires, value, &boot_raw_hires_test);
  206. }
  207. EXPORT_SYMBOL(jent_raw_hires_entropy_store);
  208. static int jent_raw_hires_entropy_reader(u8 *outbuf, u32 outbuflen)
  209. {
  210. return jent_testing_reader(&jent_raw_hires, &boot_raw_hires_test,
  211. outbuf, outbuflen);
  212. }
  213. static ssize_t jent_raw_hires_read(struct file *file, char __user *to,
  214. size_t count, loff_t *ppos)
  215. {
  216. return jent_testing_extract_user(file, to, count, ppos,
  217. jent_raw_hires_entropy_reader);
  218. }
  219. static const struct file_operations jent_raw_hires_fops = {
  220. .owner = THIS_MODULE,
  221. .read = jent_raw_hires_read,
  222. };
  223. /******************************* Initialization *******************************/
  224. void jent_testing_init(void)
  225. {
  226. jent_raw_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
  227. debugfs_create_file_unsafe("jent_raw_hires", 0400,
  228. jent_raw_debugfs_root, NULL,
  229. &jent_raw_hires_fops);
  230. }
  231. EXPORT_SYMBOL(jent_testing_init);
  232. void jent_testing_exit(void)
  233. {
  234. debugfs_remove_recursive(jent_raw_debugfs_root);
  235. }
  236. EXPORT_SYMBOL(jent_testing_exit);