rng.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. strncpy(rrng.type, "rng", sizeof(rrng.type));
  57. rrng.seedsize = alg->cra_rng.seedsize;
  58. NLA_PUT(skb, CRYPTOCFGA_REPORT_RNG,
  59. sizeof(struct crypto_report_rng), &rrng);
  60. return 0;
  61. nla_put_failure:
  62. return -EMSGSIZE;
  63. }
  64. #else
  65. static int crypto_rng_report(struct sk_buff *skb, struct crypto_alg *alg)
  66. {
  67. return -ENOSYS;
  68. }
  69. #endif
  70. static void crypto_rng_show(struct seq_file *m, struct crypto_alg *alg)
  71. __attribute__ ((unused));
  72. static void crypto_rng_show(struct seq_file *m, struct crypto_alg *alg)
  73. {
  74. seq_printf(m, "type : rng\n");
  75. seq_printf(m, "seedsize : %u\n", alg->cra_rng.seedsize);
  76. }
  77. static unsigned int crypto_rng_ctxsize(struct crypto_alg *alg, u32 type,
  78. u32 mask)
  79. {
  80. return alg->cra_ctxsize;
  81. }
  82. const struct crypto_type crypto_rng_type = {
  83. .ctxsize = crypto_rng_ctxsize,
  84. .init = crypto_init_rng_ops,
  85. #ifdef CONFIG_PROC_FS
  86. .show = crypto_rng_show,
  87. #endif
  88. .report = crypto_rng_report,
  89. };
  90. EXPORT_SYMBOL_GPL(crypto_rng_type);
  91. int crypto_get_default_rng(void)
  92. {
  93. struct crypto_rng *rng;
  94. int err;
  95. mutex_lock(&crypto_default_rng_lock);
  96. if (!crypto_default_rng) {
  97. rng = crypto_alloc_rng("stdrng", 0, 0);
  98. err = PTR_ERR(rng);
  99. if (IS_ERR(rng))
  100. goto unlock;
  101. err = crypto_rng_reset(rng, NULL, crypto_rng_seedsize(rng));
  102. if (err) {
  103. crypto_free_rng(rng);
  104. goto unlock;
  105. }
  106. crypto_default_rng = rng;
  107. }
  108. crypto_default_rng_refcnt++;
  109. err = 0;
  110. unlock:
  111. mutex_unlock(&crypto_default_rng_lock);
  112. return err;
  113. }
  114. EXPORT_SYMBOL_GPL(crypto_get_default_rng);
  115. void crypto_put_default_rng(void)
  116. {
  117. mutex_lock(&crypto_default_rng_lock);
  118. if (!--crypto_default_rng_refcnt) {
  119. crypto_free_rng(crypto_default_rng);
  120. crypto_default_rng = NULL;
  121. }
  122. mutex_unlock(&crypto_default_rng_lock);
  123. }
  124. EXPORT_SYMBOL_GPL(crypto_put_default_rng);
  125. MODULE_LICENSE("GPL");
  126. MODULE_DESCRIPTION("Random Number Generator");