rng.c 4.9 KB

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