rng.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Cryptographic API.
  4. *
  5. * RNG operations.
  6. *
  7. * Copyright (c) 2008 Neil Horman <nhorman@tuxdriver.com>
  8. * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
  9. */
  10. #include <linux/atomic.h>
  11. #include <crypto/internal/rng.h>
  12. #include <linux/err.h>
  13. #include <linux/module.h>
  14. #include <linux/mutex.h>
  15. #include <linux/random.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/slab.h>
  18. #include <linux/string.h>
  19. #include <linux/cryptouser.h>
  20. #include <linux/compiler.h>
  21. #include <net/netlink.h>
  22. #include "internal.h"
  23. static DEFINE_MUTEX(crypto_default_rng_lock);
  24. struct crypto_rng *crypto_default_rng;
  25. EXPORT_SYMBOL_GPL(crypto_default_rng);
  26. static int crypto_default_rng_refcnt;
  27. int crypto_rng_reset(struct crypto_rng *tfm, const u8 *seed, unsigned int slen)
  28. {
  29. struct crypto_alg *alg = tfm->base.__crt_alg;
  30. u8 *buf = NULL;
  31. int err;
  32. if (!seed && slen) {
  33. buf = kmalloc(slen, GFP_KERNEL);
  34. if (!buf)
  35. return -ENOMEM;
  36. err = get_random_bytes_wait(buf, slen);
  37. if (err)
  38. goto out;
  39. seed = buf;
  40. }
  41. crypto_stats_get(alg);
  42. err = crypto_rng_alg(tfm)->seed(tfm, seed, slen);
  43. crypto_stats_rng_seed(alg, err);
  44. out:
  45. kfree_sensitive(buf);
  46. return err;
  47. }
  48. EXPORT_SYMBOL_GPL(crypto_rng_reset);
  49. static int crypto_rng_init_tfm(struct crypto_tfm *tfm)
  50. {
  51. return 0;
  52. }
  53. static unsigned int seedsize(struct crypto_alg *alg)
  54. {
  55. struct rng_alg *ralg = container_of(alg, struct rng_alg, base);
  56. return ralg->seedsize;
  57. }
  58. #ifdef CONFIG_NET
  59. static int crypto_rng_report(struct sk_buff *skb, struct crypto_alg *alg)
  60. {
  61. struct crypto_report_rng rrng;
  62. memset(&rrng, 0, sizeof(rrng));
  63. strscpy(rrng.type, "rng", sizeof(rrng.type));
  64. rrng.seedsize = seedsize(alg);
  65. return nla_put(skb, CRYPTOCFGA_REPORT_RNG, sizeof(rrng), &rrng);
  66. }
  67. #else
  68. static int crypto_rng_report(struct sk_buff *skb, struct crypto_alg *alg)
  69. {
  70. return -ENOSYS;
  71. }
  72. #endif
  73. static void crypto_rng_show(struct seq_file *m, struct crypto_alg *alg)
  74. __maybe_unused;
  75. static void crypto_rng_show(struct seq_file *m, struct crypto_alg *alg)
  76. {
  77. seq_printf(m, "type : rng\n");
  78. seq_printf(m, "seedsize : %u\n", seedsize(alg));
  79. }
  80. static const struct crypto_type crypto_rng_type = {
  81. .extsize = crypto_alg_extsize,
  82. .init_tfm = crypto_rng_init_tfm,
  83. #ifdef CONFIG_PROC_FS
  84. .show = crypto_rng_show,
  85. #endif
  86. .report = crypto_rng_report,
  87. .maskclear = ~CRYPTO_ALG_TYPE_MASK,
  88. .maskset = CRYPTO_ALG_TYPE_MASK,
  89. .type = CRYPTO_ALG_TYPE_RNG,
  90. .tfmsize = offsetof(struct crypto_rng, base),
  91. };
  92. struct crypto_rng *crypto_alloc_rng(const char *alg_name, u32 type, u32 mask)
  93. {
  94. return crypto_alloc_tfm(alg_name, &crypto_rng_type, type, mask);
  95. }
  96. EXPORT_SYMBOL_GPL(crypto_alloc_rng);
  97. int crypto_get_default_rng(void)
  98. {
  99. struct crypto_rng *rng;
  100. int err;
  101. mutex_lock(&crypto_default_rng_lock);
  102. if (!crypto_default_rng) {
  103. rng = crypto_alloc_rng("stdrng", 0, 0);
  104. err = PTR_ERR(rng);
  105. if (IS_ERR(rng))
  106. goto unlock;
  107. err = crypto_rng_reset(rng, NULL, crypto_rng_seedsize(rng));
  108. if (err) {
  109. crypto_free_rng(rng);
  110. goto unlock;
  111. }
  112. crypto_default_rng = rng;
  113. }
  114. crypto_default_rng_refcnt++;
  115. err = 0;
  116. unlock:
  117. mutex_unlock(&crypto_default_rng_lock);
  118. return err;
  119. }
  120. EXPORT_SYMBOL_GPL(crypto_get_default_rng);
  121. void crypto_put_default_rng(void)
  122. {
  123. mutex_lock(&crypto_default_rng_lock);
  124. crypto_default_rng_refcnt--;
  125. mutex_unlock(&crypto_default_rng_lock);
  126. }
  127. EXPORT_SYMBOL_GPL(crypto_put_default_rng);
  128. #if defined(CONFIG_CRYPTO_RNG) || defined(CONFIG_CRYPTO_RNG_MODULE)
  129. int crypto_del_default_rng(void)
  130. {
  131. int err = -EBUSY;
  132. mutex_lock(&crypto_default_rng_lock);
  133. if (crypto_default_rng_refcnt)
  134. goto out;
  135. crypto_free_rng(crypto_default_rng);
  136. crypto_default_rng = NULL;
  137. err = 0;
  138. out:
  139. mutex_unlock(&crypto_default_rng_lock);
  140. return err;
  141. }
  142. EXPORT_SYMBOL_GPL(crypto_del_default_rng);
  143. #endif
  144. int crypto_register_rng(struct rng_alg *alg)
  145. {
  146. struct crypto_alg *base = &alg->base;
  147. if (alg->seedsize > PAGE_SIZE / 8)
  148. return -EINVAL;
  149. base->cra_type = &crypto_rng_type;
  150. base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
  151. base->cra_flags |= CRYPTO_ALG_TYPE_RNG;
  152. return crypto_register_alg(base);
  153. }
  154. EXPORT_SYMBOL_GPL(crypto_register_rng);
  155. void crypto_unregister_rng(struct rng_alg *alg)
  156. {
  157. crypto_unregister_alg(&alg->base);
  158. }
  159. EXPORT_SYMBOL_GPL(crypto_unregister_rng);
  160. int crypto_register_rngs(struct rng_alg *algs, int count)
  161. {
  162. int i, ret;
  163. for (i = 0; i < count; i++) {
  164. ret = crypto_register_rng(algs + i);
  165. if (ret)
  166. goto err;
  167. }
  168. return 0;
  169. err:
  170. for (--i; i >= 0; --i)
  171. crypto_unregister_rng(algs + i);
  172. return ret;
  173. }
  174. EXPORT_SYMBOL_GPL(crypto_register_rngs);
  175. void crypto_unregister_rngs(struct rng_alg *algs, int count)
  176. {
  177. int i;
  178. for (i = count - 1; i >= 0; --i)
  179. crypto_unregister_rng(algs + i);
  180. }
  181. EXPORT_SYMBOL_GPL(crypto_unregister_rngs);
  182. MODULE_LICENSE("GPL");
  183. MODULE_DESCRIPTION("Random Number Generator");