rng.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. get_random_bytes(buf, slen);
  41. seed = buf;
  42. }
  43. err = crypto_rng_alg(tfm)->seed(tfm, seed, slen);
  44. kzfree(buf);
  45. return err;
  46. }
  47. EXPORT_SYMBOL_GPL(crypto_rng_reset);
  48. static int crypto_rng_init_tfm(struct crypto_tfm *tfm)
  49. {
  50. return 0;
  51. }
  52. static unsigned int seedsize(struct crypto_alg *alg)
  53. {
  54. struct rng_alg *ralg = container_of(alg, struct rng_alg, base);
  55. return ralg->seedsize;
  56. }
  57. #ifdef CONFIG_NET
  58. static int crypto_rng_report(struct sk_buff *skb, struct crypto_alg *alg)
  59. {
  60. struct crypto_report_rng rrng;
  61. strncpy(rrng.type, "rng", sizeof(rrng.type));
  62. rrng.seedsize = seedsize(alg);
  63. if (nla_put(skb, CRYPTOCFGA_REPORT_RNG,
  64. sizeof(struct crypto_report_rng), &rrng))
  65. goto nla_put_failure;
  66. return 0;
  67. nla_put_failure:
  68. return -EMSGSIZE;
  69. }
  70. #else
  71. static int crypto_rng_report(struct sk_buff *skb, struct crypto_alg *alg)
  72. {
  73. return -ENOSYS;
  74. }
  75. #endif
  76. static void crypto_rng_show(struct seq_file *m, struct crypto_alg *alg)
  77. __maybe_unused;
  78. static void crypto_rng_show(struct seq_file *m, struct crypto_alg *alg)
  79. {
  80. seq_printf(m, "type : rng\n");
  81. seq_printf(m, "seedsize : %u\n", seedsize(alg));
  82. }
  83. static const struct crypto_type crypto_rng_type = {
  84. .extsize = crypto_alg_extsize,
  85. .init_tfm = crypto_rng_init_tfm,
  86. #ifdef CONFIG_PROC_FS
  87. .show = crypto_rng_show,
  88. #endif
  89. .report = crypto_rng_report,
  90. .maskclear = ~CRYPTO_ALG_TYPE_MASK,
  91. .maskset = CRYPTO_ALG_TYPE_MASK,
  92. .type = CRYPTO_ALG_TYPE_RNG,
  93. .tfmsize = offsetof(struct crypto_rng, base),
  94. };
  95. struct crypto_rng *crypto_alloc_rng(const char *alg_name, u32 type, u32 mask)
  96. {
  97. return crypto_alloc_tfm(alg_name, &crypto_rng_type, type, mask);
  98. }
  99. EXPORT_SYMBOL_GPL(crypto_alloc_rng);
  100. int crypto_get_default_rng(void)
  101. {
  102. struct crypto_rng *rng;
  103. int err;
  104. mutex_lock(&crypto_default_rng_lock);
  105. if (!crypto_default_rng) {
  106. rng = crypto_alloc_rng("stdrng", 0, 0);
  107. err = PTR_ERR(rng);
  108. if (IS_ERR(rng))
  109. goto unlock;
  110. err = crypto_rng_reset(rng, NULL, crypto_rng_seedsize(rng));
  111. if (err) {
  112. crypto_free_rng(rng);
  113. goto unlock;
  114. }
  115. crypto_default_rng = rng;
  116. }
  117. crypto_default_rng_refcnt++;
  118. err = 0;
  119. unlock:
  120. mutex_unlock(&crypto_default_rng_lock);
  121. return err;
  122. }
  123. EXPORT_SYMBOL_GPL(crypto_get_default_rng);
  124. void crypto_put_default_rng(void)
  125. {
  126. mutex_lock(&crypto_default_rng_lock);
  127. crypto_default_rng_refcnt--;
  128. mutex_unlock(&crypto_default_rng_lock);
  129. }
  130. EXPORT_SYMBOL_GPL(crypto_put_default_rng);
  131. #if defined(CONFIG_CRYPTO_RNG) || defined(CONFIG_CRYPTO_RNG_MODULE)
  132. int crypto_del_default_rng(void)
  133. {
  134. int err = -EBUSY;
  135. mutex_lock(&crypto_default_rng_lock);
  136. if (crypto_default_rng_refcnt)
  137. goto out;
  138. crypto_free_rng(crypto_default_rng);
  139. crypto_default_rng = NULL;
  140. err = 0;
  141. out:
  142. mutex_unlock(&crypto_default_rng_lock);
  143. return err;
  144. }
  145. EXPORT_SYMBOL_GPL(crypto_del_default_rng);
  146. #endif
  147. int crypto_register_rng(struct rng_alg *alg)
  148. {
  149. struct crypto_alg *base = &alg->base;
  150. if (alg->seedsize > PAGE_SIZE / 8)
  151. return -EINVAL;
  152. base->cra_type = &crypto_rng_type;
  153. base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
  154. base->cra_flags |= CRYPTO_ALG_TYPE_RNG;
  155. return crypto_register_alg(base);
  156. }
  157. EXPORT_SYMBOL_GPL(crypto_register_rng);
  158. void crypto_unregister_rng(struct rng_alg *alg)
  159. {
  160. crypto_unregister_alg(&alg->base);
  161. }
  162. EXPORT_SYMBOL_GPL(crypto_unregister_rng);
  163. int crypto_register_rngs(struct rng_alg *algs, int count)
  164. {
  165. int i, ret;
  166. for (i = 0; i < count; i++) {
  167. ret = crypto_register_rng(algs + i);
  168. if (ret)
  169. goto err;
  170. }
  171. return 0;
  172. err:
  173. for (--i; i >= 0; --i)
  174. crypto_unregister_rng(algs + i);
  175. return ret;
  176. }
  177. EXPORT_SYMBOL_GPL(crypto_register_rngs);
  178. void crypto_unregister_rngs(struct rng_alg *algs, int count)
  179. {
  180. int i;
  181. for (i = count - 1; i >= 0; --i)
  182. crypto_unregister_rng(algs + i);
  183. }
  184. EXPORT_SYMBOL_GPL(crypto_unregister_rngs);
  185. MODULE_LICENSE("GPL");
  186. MODULE_DESCRIPTION("Random Number Generator");