rng.h 7.3 KB

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