jitterentropy-kcapi.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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
  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/module.h>
  40. #include <linux/slab.h>
  41. #include <linux/module.h>
  42. #include <linux/fips.h>
  43. #include <linux/time.h>
  44. #include <linux/crypto.h>
  45. #include <crypto/internal/rng.h>
  46. struct rand_data;
  47. int jent_read_entropy(struct rand_data *ec, unsigned char *data,
  48. unsigned int len);
  49. int jent_entropy_init(void);
  50. struct rand_data *jent_entropy_collector_alloc(unsigned int osr,
  51. unsigned int flags);
  52. void jent_entropy_collector_free(struct rand_data *entropy_collector);
  53. /***************************************************************************
  54. * Helper function
  55. ***************************************************************************/
  56. __u64 jent_rol64(__u64 word, unsigned int shift)
  57. {
  58. return rol64(word, shift);
  59. }
  60. void *jent_zalloc(unsigned int len)
  61. {
  62. return kzalloc(len, GFP_KERNEL);
  63. }
  64. void jent_zfree(void *ptr)
  65. {
  66. kzfree(ptr);
  67. }
  68. int jent_fips_enabled(void)
  69. {
  70. return fips_enabled;
  71. }
  72. void jent_panic(char *s)
  73. {
  74. panic(s);
  75. }
  76. void jent_memcpy(void *dest, const void *src, unsigned int n)
  77. {
  78. memcpy(dest, src, n);
  79. }
  80. void jent_get_nstime(__u64 *out)
  81. {
  82. struct timespec ts;
  83. __u64 tmp = 0;
  84. tmp = random_get_entropy();
  85. /*
  86. * If random_get_entropy does not return a value (which is possible on,
  87. * for example, MIPS), invoke __getnstimeofday
  88. * hoping that there are timers we can work with.
  89. *
  90. * The list of available timers can be obtained from
  91. * /sys/devices/system/clocksource/clocksource0/available_clocksource
  92. * and are registered with clocksource_register()
  93. */
  94. if ((0 == tmp) &&
  95. (0 == __getnstimeofday(&ts))) {
  96. tmp = ts.tv_sec;
  97. tmp = tmp << 32;
  98. tmp = tmp | ts.tv_nsec;
  99. }
  100. *out = tmp;
  101. }
  102. /***************************************************************************
  103. * Kernel crypto API interface
  104. ***************************************************************************/
  105. struct jitterentropy {
  106. spinlock_t jent_lock;
  107. struct rand_data *entropy_collector;
  108. };
  109. static int jent_kcapi_init(struct crypto_tfm *tfm)
  110. {
  111. struct jitterentropy *rng = crypto_tfm_ctx(tfm);
  112. int ret = 0;
  113. rng->entropy_collector = jent_entropy_collector_alloc(1, 0);
  114. if (!rng->entropy_collector)
  115. ret = -ENOMEM;
  116. spin_lock_init(&rng->jent_lock);
  117. return ret;
  118. }
  119. static void jent_kcapi_cleanup(struct crypto_tfm *tfm)
  120. {
  121. struct jitterentropy *rng = crypto_tfm_ctx(tfm);
  122. spin_lock(&rng->jent_lock);
  123. if (rng->entropy_collector)
  124. jent_entropy_collector_free(rng->entropy_collector);
  125. rng->entropy_collector = NULL;
  126. spin_unlock(&rng->jent_lock);
  127. }
  128. static int jent_kcapi_random(struct crypto_rng *tfm,
  129. const u8 *src, unsigned int slen,
  130. u8 *rdata, unsigned int dlen)
  131. {
  132. struct jitterentropy *rng = crypto_rng_ctx(tfm);
  133. int ret = 0;
  134. spin_lock(&rng->jent_lock);
  135. ret = jent_read_entropy(rng->entropy_collector, rdata, dlen);
  136. spin_unlock(&rng->jent_lock);
  137. return ret;
  138. }
  139. static int jent_kcapi_reset(struct crypto_rng *tfm,
  140. const u8 *seed, unsigned int slen)
  141. {
  142. return 0;
  143. }
  144. static struct rng_alg jent_alg = {
  145. .generate = jent_kcapi_random,
  146. .seed = jent_kcapi_reset,
  147. .seedsize = 0,
  148. .base = {
  149. .cra_name = "jitterentropy_rng",
  150. .cra_driver_name = "jitterentropy_rng",
  151. .cra_priority = 100,
  152. .cra_ctxsize = sizeof(struct jitterentropy),
  153. .cra_module = THIS_MODULE,
  154. .cra_init = jent_kcapi_init,
  155. .cra_exit = jent_kcapi_cleanup,
  156. }
  157. };
  158. static int __init jent_mod_init(void)
  159. {
  160. int ret = 0;
  161. ret = jent_entropy_init();
  162. if (ret) {
  163. pr_info("jitterentropy: Initialization failed with host not compliant with requirements: %d\n", ret);
  164. return -EFAULT;
  165. }
  166. return crypto_register_rng(&jent_alg);
  167. }
  168. static void __exit jent_mod_exit(void)
  169. {
  170. crypto_unregister_rng(&jent_alg);
  171. }
  172. module_init(jent_mod_init);
  173. module_exit(jent_mod_exit);
  174. MODULE_LICENSE("Dual BSD/GPL");
  175. MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
  176. MODULE_DESCRIPTION("Non-physical True Random Number Generator based on CPU Jitter");
  177. MODULE_ALIAS_CRYPTO("jitterentropy_rng");