rng.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * RNG: Random Number Generator algorithms under the crypto API
  3. *
  4. * Copyright (c) 2008 Neil Horman <nhorman@tuxdriver.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. *
  11. */
  12. #ifndef _CRYPTO_RNG_H
  13. #define _CRYPTO_RNG_H
  14. #include <linux/crypto.h>
  15. extern struct crypto_rng *crypto_default_rng;
  16. int crypto_get_default_rng(void);
  17. void crypto_put_default_rng(void);
  18. /**
  19. * DOC: Random number generator API
  20. *
  21. * The random number generator API is used with the ciphers of type
  22. * CRYPTO_ALG_TYPE_RNG (listed as type "rng" in /proc/crypto)
  23. */
  24. static inline struct crypto_rng *__crypto_rng_cast(struct crypto_tfm *tfm)
  25. {
  26. return (struct crypto_rng *)tfm;
  27. }
  28. /**
  29. * crypto_alloc_rng() -- allocate RNG handle
  30. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  31. * message digest cipher
  32. * @type: specifies the type of the cipher
  33. * @mask: specifies the mask for the cipher
  34. *
  35. * Allocate a cipher handle for a random number generator. The returned struct
  36. * crypto_rng is the cipher handle that is required for any subsequent
  37. * API invocation for that random number generator.
  38. *
  39. * For all random number generators, this call creates a new private copy of
  40. * the random number generator that does not share a state with other
  41. * instances. The only exception is the "krng" random number generator which
  42. * is a kernel crypto API use case for the get_random_bytes() function of the
  43. * /dev/random driver.
  44. *
  45. * Return: allocated cipher handle in case of success; IS_ERR() is true in case
  46. * of an error, PTR_ERR() returns the error code.
  47. */
  48. static inline struct crypto_rng *crypto_alloc_rng(const char *alg_name,
  49. u32 type, u32 mask)
  50. {
  51. type &= ~CRYPTO_ALG_TYPE_MASK;
  52. type |= CRYPTO_ALG_TYPE_RNG;
  53. mask |= CRYPTO_ALG_TYPE_MASK;
  54. return __crypto_rng_cast(crypto_alloc_base(alg_name, type, mask));
  55. }
  56. static inline struct crypto_tfm *crypto_rng_tfm(struct crypto_rng *tfm)
  57. {
  58. return &tfm->base;
  59. }
  60. /**
  61. * crypto_rng_alg - obtain name of RNG
  62. * @tfm: cipher handle
  63. *
  64. * Return the generic name (cra_name) of the initialized random number generator
  65. *
  66. * Return: generic name string
  67. */
  68. static inline struct rng_alg *crypto_rng_alg(struct crypto_rng *tfm)
  69. {
  70. return &crypto_rng_tfm(tfm)->__crt_alg->cra_rng;
  71. }
  72. static inline struct rng_tfm *crypto_rng_crt(struct crypto_rng *tfm)
  73. {
  74. return &crypto_rng_tfm(tfm)->crt_rng;
  75. }
  76. /**
  77. * crypto_free_rng() - zeroize and free RNG handle
  78. * @tfm: cipher handle to be freed
  79. */
  80. static inline void crypto_free_rng(struct crypto_rng *tfm)
  81. {
  82. crypto_free_tfm(crypto_rng_tfm(tfm));
  83. }
  84. /**
  85. * crypto_rng_get_bytes() - get random number
  86. * @tfm: cipher handle
  87. * @rdata: output buffer holding the random numbers
  88. * @dlen: length of the output buffer
  89. *
  90. * This function fills the caller-allocated buffer with random numbers using the
  91. * random number generator referenced by the cipher handle.
  92. *
  93. * Return: > 0 function was successful and returns the number of generated
  94. * bytes; < 0 if an error occurred
  95. */
  96. static inline int crypto_rng_get_bytes(struct crypto_rng *tfm,
  97. u8 *rdata, unsigned int dlen)
  98. {
  99. return crypto_rng_crt(tfm)->rng_gen_random(tfm, rdata, dlen);
  100. }
  101. /**
  102. * crypto_rng_reset() - re-initialize the RNG
  103. * @tfm: cipher handle
  104. * @seed: seed input data
  105. * @slen: length of the seed input data
  106. *
  107. * The reset function completely re-initializes the random number generator
  108. * referenced by the cipher handle by clearing the current state. The new state
  109. * is initialized with the caller provided seed or automatically, depending
  110. * on the random number generator type (the ANSI X9.31 RNG requires
  111. * caller-provided seed, the SP800-90A DRBGs perform an automatic seeding).
  112. * The seed is provided as a parameter to this function call. The provided seed
  113. * should have the length of the seed size defined for the random number
  114. * generator as defined by crypto_rng_seedsize.
  115. *
  116. * Return: 0 if the setting of the key was successful; < 0 if an error occurred
  117. */
  118. static inline int crypto_rng_reset(struct crypto_rng *tfm,
  119. u8 *seed, unsigned int slen)
  120. {
  121. return crypto_rng_crt(tfm)->rng_reset(tfm, seed, slen);
  122. }
  123. /**
  124. * crypto_rng_seedsize() - obtain seed size of RNG
  125. * @tfm: cipher handle
  126. *
  127. * The function returns the seed size for the random number generator
  128. * referenced by the cipher handle. This value may be zero if the random
  129. * number generator does not implement or require a reseeding. For example,
  130. * the SP800-90A DRBGs implement an automated reseeding after reaching a
  131. * pre-defined threshold.
  132. *
  133. * Return: seed size for the random number generator
  134. */
  135. static inline int crypto_rng_seedsize(struct crypto_rng *tfm)
  136. {
  137. return crypto_rng_alg(tfm)->seedsize;
  138. }
  139. #endif