rng.c 5.0 KB

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