rng.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Cryptographic API.
  3. *
  4. * RNG operations.
  5. *
  6. * Copyright (c) 2008 Neil Horman <nhorman@tuxdriver.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the Free
  10. * Software Foundation; either version 2 of the License, or (at your option)
  11. * any later version.
  12. *
  13. */
  14. #include <linux/atomic.h>
  15. #include <crypto/internal/rng.h>
  16. #include <linux/err.h>
  17. #include <linux/module.h>
  18. #include <linux/mutex.h>
  19. #include <linux/random.h>
  20. #include <linux/seq_file.h>
  21. #include <linux/slab.h>
  22. #include <linux/string.h>
  23. #include <linux/cryptouser.h>
  24. #include <net/netlink.h>
  25. static DEFINE_MUTEX(crypto_default_rng_lock);
  26. struct crypto_rng *crypto_default_rng;
  27. EXPORT_SYMBOL_GPL(crypto_default_rng);
  28. static int crypto_default_rng_refcnt;
  29. static int rngapi_reset(struct crypto_rng *tfm, u8 *seed, unsigned int slen)
  30. {
  31. u8 *buf = NULL;
  32. int err;
  33. if (!seed && slen) {
  34. buf = kmalloc(slen, GFP_KERNEL);
  35. if (!buf)
  36. return -ENOMEM;
  37. get_random_bytes(buf, slen);
  38. seed = buf;
  39. }
  40. err = crypto_rng_alg(tfm)->rng_reset(tfm, seed, slen);
  41. kfree(buf);
  42. return err;
  43. }
  44. static int crypto_init_rng_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
  45. {
  46. struct rng_alg *alg = &tfm->__crt_alg->cra_rng;
  47. struct rng_tfm *ops = &tfm->crt_rng;
  48. ops->rng_gen_random = alg->rng_make_random;
  49. ops->rng_reset = rngapi_reset;
  50. return 0;
  51. }
  52. #ifdef CONFIG_NET
  53. static int crypto_rng_report(struct sk_buff *skb, struct crypto_alg *alg)
  54. {
  55. struct crypto_report_rng rrng;
  56. snprintf(rrng.type, CRYPTO_MAX_ALG_NAME, "%s", "rng");
  57. rrng.seedsize = alg->cra_rng.seedsize;
  58. if (nla_put(skb, CRYPTOCFGA_REPORT_RNG,
  59. sizeof(struct crypto_report_rng), &rrng))
  60. goto nla_put_failure;
  61. return 0;
  62. nla_put_failure:
  63. return -EMSGSIZE;
  64. }
  65. #else
  66. static int crypto_rng_report(struct sk_buff *skb, struct crypto_alg *alg)
  67. {
  68. return -ENOSYS;
  69. }
  70. #endif
  71. static void crypto_rng_show(struct seq_file *m, struct crypto_alg *alg)
  72. __attribute__ ((unused));
  73. static void crypto_rng_show(struct seq_file *m, struct crypto_alg *alg)
  74. {
  75. seq_printf(m, "type : rng\n");
  76. seq_printf(m, "seedsize : %u\n", alg->cra_rng.seedsize);
  77. }
  78. static unsigned int crypto_rng_ctxsize(struct crypto_alg *alg, u32 type,
  79. u32 mask)
  80. {
  81. return alg->cra_ctxsize;
  82. }
  83. const struct crypto_type crypto_rng_type = {
  84. .ctxsize = crypto_rng_ctxsize,
  85. .init = crypto_init_rng_ops,
  86. #ifdef CONFIG_PROC_FS
  87. .show = crypto_rng_show,
  88. #endif
  89. .report = crypto_rng_report,
  90. };
  91. EXPORT_SYMBOL_GPL(crypto_rng_type);
  92. int crypto_get_default_rng(void)
  93. {
  94. struct crypto_rng *rng;
  95. int err;
  96. mutex_lock(&crypto_default_rng_lock);
  97. if (!crypto_default_rng) {
  98. rng = crypto_alloc_rng("stdrng", 0, 0);
  99. err = PTR_ERR(rng);
  100. if (IS_ERR(rng))
  101. goto unlock;
  102. err = crypto_rng_reset(rng, NULL, crypto_rng_seedsize(rng));
  103. if (err) {
  104. crypto_free_rng(rng);
  105. goto unlock;
  106. }
  107. crypto_default_rng = rng;
  108. }
  109. crypto_default_rng_refcnt++;
  110. err = 0;
  111. unlock:
  112. mutex_unlock(&crypto_default_rng_lock);
  113. return err;
  114. }
  115. EXPORT_SYMBOL_GPL(crypto_get_default_rng);
  116. void crypto_put_default_rng(void)
  117. {
  118. mutex_lock(&crypto_default_rng_lock);
  119. if (!--crypto_default_rng_refcnt) {
  120. crypto_free_rng(crypto_default_rng);
  121. crypto_default_rng = NULL;
  122. }
  123. mutex_unlock(&crypto_default_rng_lock);
  124. }
  125. EXPORT_SYMBOL_GPL(crypto_put_default_rng);
  126. MODULE_LICENSE("GPL");
  127. MODULE_DESCRIPTION("Random Number Generator");