rng.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 <asm/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/string.h>
  22. static DEFINE_MUTEX(crypto_default_rng_lock);
  23. struct crypto_rng *crypto_default_rng;
  24. EXPORT_SYMBOL_GPL(crypto_default_rng);
  25. static int crypto_default_rng_refcnt;
  26. static int rngapi_reset(struct crypto_rng *tfm, u8 *seed, unsigned int slen)
  27. {
  28. u8 *buf = NULL;
  29. int err;
  30. if (!seed && slen) {
  31. buf = kmalloc(slen, GFP_KERNEL);
  32. if (!buf)
  33. return -ENOMEM;
  34. get_random_bytes(buf, slen);
  35. seed = buf;
  36. }
  37. err = crypto_rng_alg(tfm)->rng_reset(tfm, seed, slen);
  38. kfree(buf);
  39. return err;
  40. }
  41. static int crypto_init_rng_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
  42. {
  43. struct rng_alg *alg = &tfm->__crt_alg->cra_rng;
  44. struct rng_tfm *ops = &tfm->crt_rng;
  45. ops->rng_gen_random = alg->rng_make_random;
  46. ops->rng_reset = rngapi_reset;
  47. return 0;
  48. }
  49. static void crypto_rng_show(struct seq_file *m, struct crypto_alg *alg)
  50. __attribute__ ((unused));
  51. static void crypto_rng_show(struct seq_file *m, struct crypto_alg *alg)
  52. {
  53. seq_printf(m, "type : rng\n");
  54. seq_printf(m, "seedsize : %u\n", alg->cra_rng.seedsize);
  55. }
  56. static unsigned int crypto_rng_ctxsize(struct crypto_alg *alg, u32 type,
  57. u32 mask)
  58. {
  59. return alg->cra_ctxsize;
  60. }
  61. const struct crypto_type crypto_rng_type = {
  62. .ctxsize = crypto_rng_ctxsize,
  63. .init = crypto_init_rng_ops,
  64. #ifdef CONFIG_PROC_FS
  65. .show = crypto_rng_show,
  66. #endif
  67. };
  68. EXPORT_SYMBOL_GPL(crypto_rng_type);
  69. int crypto_get_default_rng(void)
  70. {
  71. struct crypto_rng *rng;
  72. int err;
  73. mutex_lock(&crypto_default_rng_lock);
  74. if (!crypto_default_rng) {
  75. rng = crypto_alloc_rng("stdrng", 0, 0);
  76. err = PTR_ERR(rng);
  77. if (IS_ERR(rng))
  78. goto unlock;
  79. err = crypto_rng_reset(rng, NULL, crypto_rng_seedsize(rng));
  80. if (err) {
  81. crypto_free_rng(rng);
  82. goto unlock;
  83. }
  84. crypto_default_rng = rng;
  85. }
  86. crypto_default_rng_refcnt++;
  87. err = 0;
  88. unlock:
  89. mutex_unlock(&crypto_default_rng_lock);
  90. return err;
  91. }
  92. EXPORT_SYMBOL_GPL(crypto_get_default_rng);
  93. void crypto_put_default_rng(void)
  94. {
  95. mutex_lock(&crypto_default_rng_lock);
  96. if (!--crypto_default_rng_refcnt) {
  97. crypto_free_rng(crypto_default_rng);
  98. crypto_default_rng = NULL;
  99. }
  100. mutex_unlock(&crypto_default_rng_lock);
  101. }
  102. EXPORT_SYMBOL_GPL(crypto_put_default_rng);
  103. MODULE_LICENSE("GPL");
  104. MODULE_DESCRIPTION("Random Number Genertor");