jitterentropy-kcapi.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Non-physical true random number generator based on timing jitter --
  3. * Linux Kernel Crypto API specific code
  4. *
  5. * Copyright Stephan Mueller <[email protected]>, 2015
  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 <linux/fips.h>
  40. #include <linux/kernel.h>
  41. #include <linux/module.h>
  42. #include <linux/slab.h>
  43. #include <linux/time.h>
  44. #include <crypto/internal/rng.h>
  45. #include "jitterentropy.h"
  46. /***************************************************************************
  47. * Helper function
  48. ***************************************************************************/
  49. void *jent_zalloc(unsigned int len)
  50. {
  51. return kzalloc(len, GFP_KERNEL);
  52. }
  53. void jent_zfree(void *ptr)
  54. {
  55. kfree_sensitive(ptr);
  56. }
  57. void jent_memcpy(void *dest, const void *src, unsigned int n)
  58. {
  59. memcpy(dest, src, n);
  60. }
  61. /*
  62. * Obtain a high-resolution time stamp value. The time stamp is used to measure
  63. * the execution time of a given code path and its variations. Hence, the time
  64. * stamp must have a sufficiently high resolution.
  65. *
  66. * Note, if the function returns zero because a given architecture does not
  67. * implement a high-resolution time stamp, the RNG code's runtime test
  68. * will detect it and will not produce output.
  69. */
  70. void jent_get_nstime(__u64 *out)
  71. {
  72. __u64 tmp = 0;
  73. tmp = random_get_entropy();
  74. /*
  75. * If random_get_entropy does not return a value, i.e. it is not
  76. * implemented for a given architecture, use a clock source.
  77. * hoping that there are timers we can work with.
  78. */
  79. if (tmp == 0)
  80. tmp = ktime_get_ns();
  81. *out = tmp;
  82. }
  83. /***************************************************************************
  84. * Kernel crypto API interface
  85. ***************************************************************************/
  86. struct jitterentropy {
  87. spinlock_t jent_lock;
  88. struct rand_data *entropy_collector;
  89. };
  90. static int jent_kcapi_init(struct crypto_tfm *tfm)
  91. {
  92. struct jitterentropy *rng = crypto_tfm_ctx(tfm);
  93. int ret = 0;
  94. rng->entropy_collector = jent_entropy_collector_alloc(1, 0);
  95. if (!rng->entropy_collector)
  96. ret = -ENOMEM;
  97. spin_lock_init(&rng->jent_lock);
  98. return ret;
  99. }
  100. static void jent_kcapi_cleanup(struct crypto_tfm *tfm)
  101. {
  102. struct jitterentropy *rng = crypto_tfm_ctx(tfm);
  103. spin_lock(&rng->jent_lock);
  104. if (rng->entropy_collector)
  105. jent_entropy_collector_free(rng->entropy_collector);
  106. rng->entropy_collector = NULL;
  107. spin_unlock(&rng->jent_lock);
  108. }
  109. static int jent_kcapi_random(struct crypto_rng *tfm,
  110. const u8 *src, unsigned int slen,
  111. u8 *rdata, unsigned int dlen)
  112. {
  113. struct jitterentropy *rng = crypto_rng_ctx(tfm);
  114. int ret = 0;
  115. spin_lock(&rng->jent_lock);
  116. ret = jent_read_entropy(rng->entropy_collector, rdata, dlen);
  117. if (ret == -3) {
  118. /* Handle permanent health test error */
  119. /*
  120. * If the kernel was booted with fips=1, it implies that
  121. * the entire kernel acts as a FIPS 140 module. In this case
  122. * an SP800-90B permanent health test error is treated as
  123. * a FIPS module error.
  124. */
  125. if (fips_enabled)
  126. panic("Jitter RNG permanent health test failure\n");
  127. pr_err("Jitter RNG permanent health test failure\n");
  128. ret = -EFAULT;
  129. } else if (ret == -2) {
  130. /* Handle intermittent health test error */
  131. pr_warn_ratelimited("Reset Jitter RNG due to intermittent health test failure\n");
  132. ret = -EAGAIN;
  133. } else if (ret == -1) {
  134. /* Handle other errors */
  135. ret = -EINVAL;
  136. }
  137. spin_unlock(&rng->jent_lock);
  138. return ret;
  139. }
  140. static int jent_kcapi_reset(struct crypto_rng *tfm,
  141. const u8 *seed, unsigned int slen)
  142. {
  143. return 0;
  144. }
  145. static struct rng_alg jent_alg = {
  146. .generate = jent_kcapi_random,
  147. .seed = jent_kcapi_reset,
  148. .seedsize = 0,
  149. .base = {
  150. .cra_name = "jitterentropy_rng",
  151. .cra_driver_name = "jitterentropy_rng",
  152. .cra_priority = 100,
  153. .cra_ctxsize = sizeof(struct jitterentropy),
  154. .cra_module = THIS_MODULE,
  155. .cra_init = jent_kcapi_init,
  156. .cra_exit = jent_kcapi_cleanup,
  157. }
  158. };
  159. static int __init jent_mod_init(void)
  160. {
  161. int ret = 0;
  162. ret = jent_entropy_init();
  163. if (ret) {
  164. /* Handle permanent health test error */
  165. if (fips_enabled)
  166. panic("jitterentropy: Initialization failed with host not compliant with requirements: %d\n", ret);
  167. pr_info("jitterentropy: Initialization failed with host not compliant with requirements: %d\n", ret);
  168. return -EFAULT;
  169. }
  170. return crypto_register_rng(&jent_alg);
  171. }
  172. static void __exit jent_mod_exit(void)
  173. {
  174. crypto_unregister_rng(&jent_alg);
  175. }
  176. module_init(jent_mod_init);
  177. module_exit(jent_mod_exit);
  178. MODULE_LICENSE("Dual BSD/GPL");
  179. MODULE_AUTHOR("Stephan Mueller <[email protected]>");
  180. MODULE_DESCRIPTION("Non-physical True Random Number Generator based on CPU Jitter");
  181. MODULE_ALIAS_CRYPTO("jitterentropy_rng");