rng.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * RNG: Random Number Generator algorithms under the crypto API
  4. *
  5. * Copyright (c) 2008 Neil Horman <nhorman@tuxdriver.com>
  6. * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
  7. */
  8. #ifndef _CRYPTO_RNG_H
  9. #define _CRYPTO_RNG_H
  10. #include <linux/atomic.h>
  11. #include <linux/container_of.h>
  12. #include <linux/crypto.h>
  13. struct crypto_rng;
  14. /**
  15. * struct rng_alg - random number generator definition
  16. *
  17. * @generate: The function defined by this variable obtains a
  18. * random number. The random number generator transform
  19. * must generate the random number out of the context
  20. * provided with this call, plus any additional data
  21. * if provided to the call.
  22. * @seed: Seed or reseed the random number generator. With the
  23. * invocation of this function call, the random number
  24. * generator shall become ready for generation. If the
  25. * random number generator requires a seed for setting
  26. * up a new state, the seed must be provided by the
  27. * consumer while invoking this function. The required
  28. * size of the seed is defined with @seedsize .
  29. * @set_ent: Set entropy that would otherwise be obtained from
  30. * entropy source. Internal use only.
  31. * @seedsize: The seed size required for a random number generator
  32. * initialization defined with this variable. Some
  33. * random number generators does not require a seed
  34. * as the seeding is implemented internally without
  35. * the need of support by the consumer. In this case,
  36. * the seed size is set to zero.
  37. * @base: Common crypto API algorithm data structure.
  38. */
  39. struct rng_alg {
  40. int (*generate)(struct crypto_rng *tfm,
  41. const u8 *src, unsigned int slen,
  42. u8 *dst, unsigned int dlen);
  43. int (*seed)(struct crypto_rng *tfm, const u8 *seed, unsigned int slen);
  44. void (*set_ent)(struct crypto_rng *tfm, const u8 *data,
  45. unsigned int len);
  46. unsigned int seedsize;
  47. struct crypto_alg base;
  48. };
  49. struct crypto_rng {
  50. struct crypto_tfm base;
  51. };
  52. extern struct crypto_rng *crypto_default_rng;
  53. int crypto_get_default_rng(void);
  54. void crypto_put_default_rng(void);
  55. /**
  56. * DOC: Random number generator API
  57. *
  58. * The random number generator API is used with the ciphers of type
  59. * CRYPTO_ALG_TYPE_RNG (listed as type "rng" in /proc/crypto)
  60. */
  61. /**
  62. * crypto_alloc_rng() -- allocate RNG handle
  63. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  64. * message digest cipher
  65. * @type: specifies the type of the cipher
  66. * @mask: specifies the mask for the cipher
  67. *
  68. * Allocate a cipher handle for a random number generator. The returned struct
  69. * crypto_rng is the cipher handle that is required for any subsequent
  70. * API invocation for that random number generator.
  71. *
  72. * For all random number generators, this call creates a new private copy of
  73. * the random number generator that does not share a state with other
  74. * instances. The only exception is the "krng" random number generator which
  75. * is a kernel crypto API use case for the get_random_bytes() function of the
  76. * /dev/random driver.
  77. *
  78. * Return: allocated cipher handle in case of success; IS_ERR() is true in case
  79. * of an error, PTR_ERR() returns the error code.
  80. */
  81. struct crypto_rng *crypto_alloc_rng(const char *alg_name, u32 type, u32 mask);
  82. static inline struct crypto_tfm *crypto_rng_tfm(struct crypto_rng *tfm)
  83. {
  84. return &tfm->base;
  85. }
  86. static inline struct rng_alg *__crypto_rng_alg(struct crypto_alg *alg)
  87. {
  88. return container_of(alg, struct rng_alg, base);
  89. }
  90. /**
  91. * crypto_rng_alg() - obtain 'struct rng_alg' pointer from RNG handle
  92. * @tfm: RNG handle
  93. *
  94. * Return: Pointer to 'struct rng_alg', derived from @tfm RNG handle
  95. */
  96. static inline struct rng_alg *crypto_rng_alg(struct crypto_rng *tfm)
  97. {
  98. return __crypto_rng_alg(crypto_rng_tfm(tfm)->__crt_alg);
  99. }
  100. /**
  101. * crypto_free_rng() - zeroize and free RNG handle
  102. * @tfm: cipher handle to be freed
  103. *
  104. * If @tfm is a NULL or error pointer, this function does nothing.
  105. */
  106. static inline void crypto_free_rng(struct crypto_rng *tfm)
  107. {
  108. crypto_destroy_tfm(tfm, crypto_rng_tfm(tfm));
  109. }
  110. /**
  111. * crypto_rng_generate() - get random number
  112. * @tfm: cipher handle
  113. * @src: Input buffer holding additional data, may be NULL
  114. * @slen: Length of additional data
  115. * @dst: output buffer holding the random numbers
  116. * @dlen: length of the output buffer
  117. *
  118. * This function fills the caller-allocated buffer with random
  119. * numbers using the random number generator referenced by the
  120. * cipher handle.
  121. *
  122. * Return: 0 function was successful; < 0 if an error occurred
  123. */
  124. static inline int crypto_rng_generate(struct crypto_rng *tfm,
  125. const u8 *src, unsigned int slen,
  126. u8 *dst, unsigned int dlen)
  127. {
  128. return crypto_rng_alg(tfm)->generate(tfm, src, slen, dst, dlen);
  129. }
  130. /**
  131. * crypto_rng_get_bytes() - get random number
  132. * @tfm: cipher handle
  133. * @rdata: output buffer holding the random numbers
  134. * @dlen: length of the output buffer
  135. *
  136. * This function fills the caller-allocated buffer with random numbers using the
  137. * random number generator referenced by the cipher handle.
  138. *
  139. * Return: 0 function was successful; < 0 if an error occurred
  140. */
  141. static inline int crypto_rng_get_bytes(struct crypto_rng *tfm,
  142. u8 *rdata, unsigned int dlen)
  143. {
  144. return crypto_rng_generate(tfm, NULL, 0, rdata, dlen);
  145. }
  146. /**
  147. * crypto_rng_reset() - re-initialize the RNG
  148. * @tfm: cipher handle
  149. * @seed: seed input data
  150. * @slen: length of the seed input data
  151. *
  152. * The reset function completely re-initializes the random number generator
  153. * referenced by the cipher handle by clearing the current state. The new state
  154. * is initialized with the caller provided seed or automatically, depending on
  155. * the random number generator type. (The SP800-90A DRBGs perform an automatic
  156. * seeding.) The seed is provided as a parameter to this function call. The
  157. * provided seed should have the length of the seed size defined for the random
  158. * number generator as defined by crypto_rng_seedsize.
  159. *
  160. * Return: 0 if the setting of the key was successful; < 0 if an error occurred
  161. */
  162. int crypto_rng_reset(struct crypto_rng *tfm, const u8 *seed,
  163. unsigned int slen);
  164. /**
  165. * crypto_rng_seedsize() - obtain seed size of RNG
  166. * @tfm: cipher handle
  167. *
  168. * The function returns the seed size for the random number generator
  169. * referenced by the cipher handle. This value may be zero if the random
  170. * number generator does not implement or require a reseeding. For example,
  171. * the SP800-90A DRBGs implement an automated reseeding after reaching a
  172. * pre-defined threshold.
  173. *
  174. * Return: seed size for the random number generator
  175. */
  176. static inline int crypto_rng_seedsize(struct crypto_rng *tfm)
  177. {
  178. return crypto_rng_alg(tfm)->seedsize;
  179. }
  180. #endif