jitterentropy-kcapi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * Non-physical true random number generator based on timing jitter --
  3. * Linux Kernel Crypto API specific code
  4. *
  5. * Copyright Stephan Mueller <smueller@chronox.de>, 2015 - 2023
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, and the entire permission notice in its entirety,
  12. * including the disclaimer of warranties.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. The name of the author may not be used to endorse or promote
  17. * products derived from this software without specific prior
  18. * written permission.
  19. *
  20. * ALTERNATIVELY, this product may be distributed under the terms of
  21. * the GNU General Public License, in which case the provisions of the GPL2 are
  22. * required INSTEAD OF the above restrictions. (This clause is
  23. * necessary due to a potential bad interaction between the GPL and
  24. * the restrictions contained in a BSD-style copyright.)
  25. *
  26. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  27. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  28. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
  29. * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  32. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  33. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  34. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  35. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  36. * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
  37. * DAMAGE.
  38. */
  39. #include <crypto/hash.h>
  40. #include <crypto/sha3.h>
  41. #include <linux/fips.h>
  42. #include <linux/kernel.h>
  43. #include <linux/module.h>
  44. #include <linux/slab.h>
  45. #include <linux/time.h>
  46. #include <crypto/internal/rng.h>
  47. #include "jitterentropy.h"
  48. #define JENT_CONDITIONING_HASH "sha3-256-generic"
  49. /***************************************************************************
  50. * Helper function
  51. ***************************************************************************/
  52. void *jent_kvzalloc(unsigned int len)
  53. {
  54. return kvzalloc(len, GFP_KERNEL);
  55. }
  56. void jent_kvzfree(void *ptr, unsigned int len)
  57. {
  58. kvfree_sensitive(ptr, len);
  59. }
  60. void *jent_zalloc(unsigned int len)
  61. {
  62. return kzalloc(len, GFP_KERNEL);
  63. }
  64. void jent_zfree(void *ptr)
  65. {
  66. kfree_sensitive(ptr);
  67. }
  68. /*
  69. * Obtain a high-resolution time stamp value. The time stamp is used to measure
  70. * the execution time of a given code path and its variations. Hence, the time
  71. * stamp must have a sufficiently high resolution.
  72. *
  73. * Note, if the function returns zero because a given architecture does not
  74. * implement a high-resolution time stamp, the RNG code's runtime test
  75. * will detect it and will not produce output.
  76. */
  77. void jent_get_nstime(__u64 *out)
  78. {
  79. __u64 tmp = 0;
  80. tmp = random_get_entropy();
  81. /*
  82. * If random_get_entropy does not return a value, i.e. it is not
  83. * implemented for a given architecture, use a clock source.
  84. * hoping that there are timers we can work with.
  85. */
  86. if (tmp == 0)
  87. tmp = ktime_get_ns();
  88. *out = tmp;
  89. jent_raw_hires_entropy_store(tmp);
  90. }
  91. int jent_hash_time(void *hash_state, __u64 time, u8 *addtl,
  92. unsigned int addtl_len, __u64 hash_loop_cnt,
  93. unsigned int stuck)
  94. {
  95. struct shash_desc *hash_state_desc = (struct shash_desc *)hash_state;
  96. SHASH_DESC_ON_STACK(desc, hash_state_desc->tfm);
  97. u8 intermediary[SHA3_256_DIGEST_SIZE];
  98. __u64 j = 0;
  99. int ret;
  100. desc->tfm = hash_state_desc->tfm;
  101. if (sizeof(intermediary) != crypto_shash_digestsize(desc->tfm)) {
  102. pr_warn_ratelimited("Unexpected digest size\n");
  103. return -EINVAL;
  104. }
  105. /*
  106. * This loop fills a buffer which is injected into the entropy pool.
  107. * The main reason for this loop is to execute something over which we
  108. * can perform a timing measurement. The injection of the resulting
  109. * data into the pool is performed to ensure the result is used and
  110. * the compiler cannot optimize the loop away in case the result is not
  111. * used at all. Yet that data is considered "additional information"
  112. * considering the terminology from SP800-90A without any entropy.
  113. *
  114. * Note, it does not matter which or how much data you inject, we are
  115. * interested in one Keccack1600 compression operation performed with
  116. * the crypto_shash_final.
  117. */
  118. for (j = 0; j < hash_loop_cnt; j++) {
  119. ret = crypto_shash_init(desc) ?:
  120. crypto_shash_update(desc, intermediary,
  121. sizeof(intermediary)) ?:
  122. crypto_shash_finup(desc, addtl, addtl_len, intermediary);
  123. if (ret)
  124. goto err;
  125. }
  126. /*
  127. * Inject the data from the previous loop into the pool. This data is
  128. * not considered to contain any entropy, but it stirs the pool a bit.
  129. */
  130. ret = crypto_shash_update(desc, intermediary, sizeof(intermediary));
  131. if (ret)
  132. goto err;
  133. /*
  134. * Insert the time stamp into the hash context representing the pool.
  135. *
  136. * If the time stamp is stuck, do not finally insert the value into the
  137. * entropy pool. Although this operation should not do any harm even
  138. * when the time stamp has no entropy, SP800-90B requires that any
  139. * conditioning operation to have an identical amount of input data
  140. * according to section 3.1.5.
  141. */
  142. if (!stuck) {
  143. ret = crypto_shash_update(hash_state_desc, (u8 *)&time,
  144. sizeof(__u64));
  145. }
  146. err:
  147. shash_desc_zero(desc);
  148. memzero_explicit(intermediary, sizeof(intermediary));
  149. return ret;
  150. }
  151. int jent_read_random_block(void *hash_state, char *dst, unsigned int dst_len)
  152. {
  153. struct shash_desc *hash_state_desc = (struct shash_desc *)hash_state;
  154. u8 jent_block[SHA3_256_DIGEST_SIZE];
  155. /* Obtain data from entropy pool and re-initialize it */
  156. int ret = crypto_shash_final(hash_state_desc, jent_block) ?:
  157. crypto_shash_init(hash_state_desc) ?:
  158. crypto_shash_update(hash_state_desc, jent_block,
  159. sizeof(jent_block));
  160. if (!ret && dst_len)
  161. memcpy(dst, jent_block, dst_len);
  162. memzero_explicit(jent_block, sizeof(jent_block));
  163. return ret;
  164. }
  165. /***************************************************************************
  166. * Kernel crypto API interface
  167. ***************************************************************************/
  168. struct jitterentropy {
  169. spinlock_t jent_lock;
  170. struct rand_data *entropy_collector;
  171. struct crypto_shash *tfm;
  172. struct shash_desc *sdesc;
  173. };
  174. static void jent_kcapi_cleanup(struct crypto_tfm *tfm)
  175. {
  176. struct jitterentropy *rng = crypto_tfm_ctx(tfm);
  177. spin_lock(&rng->jent_lock);
  178. if (rng->sdesc) {
  179. shash_desc_zero(rng->sdesc);
  180. kfree(rng->sdesc);
  181. }
  182. rng->sdesc = NULL;
  183. if (rng->tfm)
  184. crypto_free_shash(rng->tfm);
  185. rng->tfm = NULL;
  186. if (rng->entropy_collector)
  187. jent_entropy_collector_free(rng->entropy_collector);
  188. rng->entropy_collector = NULL;
  189. spin_unlock(&rng->jent_lock);
  190. }
  191. static int jent_kcapi_init(struct crypto_tfm *tfm)
  192. {
  193. struct jitterentropy *rng = crypto_tfm_ctx(tfm);
  194. struct crypto_shash *hash;
  195. struct shash_desc *sdesc;
  196. int size, ret = 0;
  197. spin_lock_init(&rng->jent_lock);
  198. /*
  199. * Use SHA3-256 as conditioner. We allocate only the generic
  200. * implementation as we are not interested in high-performance. The
  201. * execution time of the SHA3 operation is measured and adds to the
  202. * Jitter RNG's unpredictable behavior. If we have a slower hash
  203. * implementation, the execution timing variations are larger. When
  204. * using a fast implementation, we would need to call it more often
  205. * as its variations are lower.
  206. */
  207. hash = crypto_alloc_shash(JENT_CONDITIONING_HASH, 0, 0);
  208. if (IS_ERR(hash)) {
  209. pr_err("Cannot allocate conditioning digest\n");
  210. return PTR_ERR(hash);
  211. }
  212. rng->tfm = hash;
  213. size = sizeof(struct shash_desc) + crypto_shash_descsize(hash);
  214. sdesc = kmalloc(size, GFP_KERNEL);
  215. if (!sdesc) {
  216. ret = -ENOMEM;
  217. goto err;
  218. }
  219. sdesc->tfm = hash;
  220. crypto_shash_init(sdesc);
  221. rng->sdesc = sdesc;
  222. rng->entropy_collector =
  223. jent_entropy_collector_alloc(CONFIG_CRYPTO_JITTERENTROPY_OSR, 0,
  224. sdesc);
  225. if (!rng->entropy_collector) {
  226. ret = -ENOMEM;
  227. goto err;
  228. }
  229. spin_lock_init(&rng->jent_lock);
  230. return 0;
  231. err:
  232. jent_kcapi_cleanup(tfm);
  233. return ret;
  234. }
  235. static int jent_kcapi_random(struct crypto_rng *tfm,
  236. const u8 *src, unsigned int slen,
  237. u8 *rdata, unsigned int dlen)
  238. {
  239. struct jitterentropy *rng = crypto_rng_ctx(tfm);
  240. int ret = 0;
  241. spin_lock(&rng->jent_lock);
  242. ret = jent_read_entropy(rng->entropy_collector, rdata, dlen);
  243. if (ret == -3) {
  244. /* Handle permanent health test error */
  245. /*
  246. * If the kernel was booted with fips=1, it implies that
  247. * the entire kernel acts as a FIPS 140 module. In this case
  248. * an SP800-90B permanent health test error is treated as
  249. * a FIPS module error.
  250. */
  251. if (fips_enabled)
  252. panic("Jitter RNG permanent health test failure\n");
  253. pr_err("Jitter RNG permanent health test failure\n");
  254. ret = -EFAULT;
  255. } else if (ret == -2) {
  256. /* Handle intermittent health test error */
  257. pr_warn_ratelimited("Reset Jitter RNG due to intermittent health test failure\n");
  258. ret = -EAGAIN;
  259. } else if (ret == -1) {
  260. /* Handle other errors */
  261. ret = -EINVAL;
  262. }
  263. spin_unlock(&rng->jent_lock);
  264. return ret;
  265. }
  266. static int jent_kcapi_reset(struct crypto_rng *tfm,
  267. const u8 *seed, unsigned int slen)
  268. {
  269. return 0;
  270. }
  271. static struct rng_alg jent_alg = {
  272. .generate = jent_kcapi_random,
  273. .seed = jent_kcapi_reset,
  274. .seedsize = 0,
  275. .base = {
  276. .cra_name = "jitterentropy_rng",
  277. .cra_driver_name = "jitterentropy_rng",
  278. .cra_priority = 100,
  279. .cra_ctxsize = sizeof(struct jitterentropy),
  280. .cra_module = THIS_MODULE,
  281. .cra_init = jent_kcapi_init,
  282. .cra_exit = jent_kcapi_cleanup,
  283. }
  284. };
  285. static int __init jent_mod_init(void)
  286. {
  287. SHASH_DESC_ON_STACK(desc, tfm);
  288. struct crypto_shash *tfm;
  289. int ret = 0;
  290. jent_testing_init();
  291. tfm = crypto_alloc_shash(JENT_CONDITIONING_HASH, 0, 0);
  292. if (IS_ERR(tfm)) {
  293. jent_testing_exit();
  294. return PTR_ERR(tfm);
  295. }
  296. desc->tfm = tfm;
  297. crypto_shash_init(desc);
  298. ret = jent_entropy_init(CONFIG_CRYPTO_JITTERENTROPY_OSR, 0, desc, NULL);
  299. shash_desc_zero(desc);
  300. crypto_free_shash(tfm);
  301. if (ret) {
  302. /* Handle permanent health test error */
  303. if (fips_enabled)
  304. panic("jitterentropy: Initialization failed with host not compliant with requirements: %d\n", ret);
  305. jent_testing_exit();
  306. pr_info("jitterentropy: Initialization failed with host not compliant with requirements: %d\n", ret);
  307. return -EFAULT;
  308. }
  309. return crypto_register_rng(&jent_alg);
  310. }
  311. static void __exit jent_mod_exit(void)
  312. {
  313. jent_testing_exit();
  314. crypto_unregister_rng(&jent_alg);
  315. }
  316. module_init(jent_mod_init);
  317. module_exit(jent_mod_exit);
  318. MODULE_LICENSE("Dual BSD/GPL");
  319. MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
  320. MODULE_DESCRIPTION("Non-physical True Random Number Generator based on CPU Jitter");
  321. MODULE_ALIAS_CRYPTO("jitterentropy_rng");