drbg.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * DRBG based on NIST SP800-90A
  3. *
  4. * Copyright Stephan Mueller <smueller@chronox.de>, 2014
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, and the entire permission notice in its entirety,
  11. * including the disclaimer of warranties.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. The name of the author may not be used to endorse or promote
  16. * products derived from this software without specific prior
  17. * written permission.
  18. *
  19. * ALTERNATIVELY, this product may be distributed under the terms of
  20. * the GNU General Public License, in which case the provisions of the GPL are
  21. * required INSTEAD OF the above restrictions. (This clause is
  22. * necessary due to a potential bad interaction between the GPL and
  23. * the restrictions contained in a BSD-style copyright.)
  24. *
  25. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  26. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  27. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
  28. * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
  29. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  31. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  32. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  35. * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
  36. * DAMAGE.
  37. */
  38. #ifndef _DRBG_H
  39. #define _DRBG_H
  40. #include <linux/random.h>
  41. #include <linux/scatterlist.h>
  42. #include <crypto/hash.h>
  43. #include <linux/module.h>
  44. #include <linux/crypto.h>
  45. #include <linux/slab.h>
  46. #include <crypto/internal/rng.h>
  47. #include <crypto/rng.h>
  48. #include <linux/fips.h>
  49. #include <linux/mutex.h>
  50. #include <linux/list.h>
  51. #include <linux/workqueue.h>
  52. /*
  53. * Concatenation Helper and string operation helper
  54. *
  55. * SP800-90A requires the concatenation of different data. To avoid copying
  56. * buffers around or allocate additional memory, the following data structure
  57. * is used to point to the original memory with its size. In addition, it
  58. * is used to build a linked list. The linked list defines the concatenation
  59. * of individual buffers. The order of memory block referenced in that
  60. * linked list determines the order of concatenation.
  61. */
  62. struct drbg_string {
  63. const unsigned char *buf;
  64. size_t len;
  65. struct list_head list;
  66. };
  67. static inline void drbg_string_fill(struct drbg_string *string,
  68. const unsigned char *buf, size_t len)
  69. {
  70. string->buf = buf;
  71. string->len = len;
  72. INIT_LIST_HEAD(&string->list);
  73. }
  74. struct drbg_state;
  75. typedef uint32_t drbg_flag_t;
  76. struct drbg_core {
  77. drbg_flag_t flags; /* flags for the cipher */
  78. __u8 statelen; /* maximum state length */
  79. __u8 blocklen_bytes; /* block size of output in bytes */
  80. char cra_name[CRYPTO_MAX_ALG_NAME]; /* mapping to kernel crypto API */
  81. /* kernel crypto API backend cipher name */
  82. char backend_cra_name[CRYPTO_MAX_ALG_NAME];
  83. };
  84. struct drbg_state_ops {
  85. int (*update)(struct drbg_state *drbg, struct list_head *seed,
  86. int reseed);
  87. int (*generate)(struct drbg_state *drbg,
  88. unsigned char *buf, unsigned int buflen,
  89. struct list_head *addtl);
  90. int (*crypto_init)(struct drbg_state *drbg);
  91. int (*crypto_fini)(struct drbg_state *drbg);
  92. };
  93. struct drbg_test_data {
  94. struct drbg_string *testentropy; /* TEST PARAMETER: test entropy */
  95. };
  96. struct drbg_state {
  97. struct mutex drbg_mutex; /* lock around DRBG */
  98. unsigned char *V; /* internal state 10.1.1.1 1a) */
  99. /* hash: static value 10.1.1.1 1b) hmac / ctr: key */
  100. unsigned char *C;
  101. /* Number of RNG requests since last reseed -- 10.1.1.1 1c) */
  102. size_t reseed_ctr;
  103. size_t reseed_threshold;
  104. /* some memory the DRBG can use for its operation */
  105. unsigned char *scratchpad;
  106. void *priv_data; /* Cipher handle */
  107. bool seeded; /* DRBG fully seeded? */
  108. bool pr; /* Prediction resistance enabled? */
  109. #ifdef CONFIG_CRYPTO_FIPS
  110. bool fips_primed; /* Continuous test primed? */
  111. unsigned char *prev; /* FIPS 140-2 continuous test value */
  112. #endif
  113. struct work_struct seed_work; /* asynchronous seeding support */
  114. struct crypto_rng *jent;
  115. const struct drbg_state_ops *d_ops;
  116. const struct drbg_core *core;
  117. struct drbg_string test_data;
  118. struct random_ready_callback random_ready;
  119. };
  120. static inline __u8 drbg_statelen(struct drbg_state *drbg)
  121. {
  122. if (drbg && drbg->core)
  123. return drbg->core->statelen;
  124. return 0;
  125. }
  126. static inline __u8 drbg_blocklen(struct drbg_state *drbg)
  127. {
  128. if (drbg && drbg->core)
  129. return drbg->core->blocklen_bytes;
  130. return 0;
  131. }
  132. static inline __u8 drbg_keylen(struct drbg_state *drbg)
  133. {
  134. if (drbg && drbg->core)
  135. return (drbg->core->statelen - drbg->core->blocklen_bytes);
  136. return 0;
  137. }
  138. static inline size_t drbg_max_request_bytes(struct drbg_state *drbg)
  139. {
  140. /* SP800-90A requires the limit 2**19 bits, but we return bytes */
  141. return (1 << 16);
  142. }
  143. static inline size_t drbg_max_addtl(struct drbg_state *drbg)
  144. {
  145. /* SP800-90A requires 2**35 bytes additional info str / pers str */
  146. #if (__BITS_PER_LONG == 32)
  147. /*
  148. * SP800-90A allows smaller maximum numbers to be returned -- we
  149. * return SIZE_MAX - 1 to allow the verification of the enforcement
  150. * of this value in drbg_healthcheck_sanity.
  151. */
  152. return (SIZE_MAX - 1);
  153. #else
  154. return (1UL<<35);
  155. #endif
  156. }
  157. static inline size_t drbg_max_requests(struct drbg_state *drbg)
  158. {
  159. /* SP800-90A requires 2**48 maximum requests before reseeding */
  160. #if (__BITS_PER_LONG == 32)
  161. return SIZE_MAX;
  162. #else
  163. return (1UL<<48);
  164. #endif
  165. }
  166. /*
  167. * This is a wrapper to the kernel crypto API function of
  168. * crypto_rng_generate() to allow the caller to provide additional data.
  169. *
  170. * @drng DRBG handle -- see crypto_rng_get_bytes
  171. * @outbuf output buffer -- see crypto_rng_get_bytes
  172. * @outlen length of output buffer -- see crypto_rng_get_bytes
  173. * @addtl_input additional information string input buffer
  174. * @addtllen length of additional information string buffer
  175. *
  176. * return
  177. * see crypto_rng_get_bytes
  178. */
  179. static inline int crypto_drbg_get_bytes_addtl(struct crypto_rng *drng,
  180. unsigned char *outbuf, unsigned int outlen,
  181. struct drbg_string *addtl)
  182. {
  183. return crypto_rng_generate(drng, addtl->buf, addtl->len,
  184. outbuf, outlen);
  185. }
  186. /*
  187. * TEST code
  188. *
  189. * This is a wrapper to the kernel crypto API function of
  190. * crypto_rng_generate() to allow the caller to provide additional data and
  191. * allow furnishing of test_data
  192. *
  193. * @drng DRBG handle -- see crypto_rng_get_bytes
  194. * @outbuf output buffer -- see crypto_rng_get_bytes
  195. * @outlen length of output buffer -- see crypto_rng_get_bytes
  196. * @addtl_input additional information string input buffer
  197. * @addtllen length of additional information string buffer
  198. * @test_data filled test data
  199. *
  200. * return
  201. * see crypto_rng_get_bytes
  202. */
  203. static inline int crypto_drbg_get_bytes_addtl_test(struct crypto_rng *drng,
  204. unsigned char *outbuf, unsigned int outlen,
  205. struct drbg_string *addtl,
  206. struct drbg_test_data *test_data)
  207. {
  208. crypto_rng_set_entropy(drng, test_data->testentropy->buf,
  209. test_data->testentropy->len);
  210. return crypto_rng_generate(drng, addtl->buf, addtl->len,
  211. outbuf, outlen);
  212. }
  213. /*
  214. * TEST code
  215. *
  216. * This is a wrapper to the kernel crypto API function of
  217. * crypto_rng_reset() to allow the caller to provide test_data
  218. *
  219. * @drng DRBG handle -- see crypto_rng_reset
  220. * @pers personalization string input buffer
  221. * @perslen length of additional information string buffer
  222. * @test_data filled test data
  223. *
  224. * return
  225. * see crypto_rng_reset
  226. */
  227. static inline int crypto_drbg_reset_test(struct crypto_rng *drng,
  228. struct drbg_string *pers,
  229. struct drbg_test_data *test_data)
  230. {
  231. crypto_rng_set_entropy(drng, test_data->testentropy->buf,
  232. test_data->testentropy->len);
  233. return crypto_rng_reset(drng, pers->buf, pers->len);
  234. }
  235. /* DRBG type flags */
  236. #define DRBG_CTR ((drbg_flag_t)1<<0)
  237. #define DRBG_HMAC ((drbg_flag_t)1<<1)
  238. #define DRBG_HASH ((drbg_flag_t)1<<2)
  239. #define DRBG_TYPE_MASK (DRBG_CTR | DRBG_HMAC | DRBG_HASH)
  240. /* DRBG strength flags */
  241. #define DRBG_STRENGTH128 ((drbg_flag_t)1<<3)
  242. #define DRBG_STRENGTH192 ((drbg_flag_t)1<<4)
  243. #define DRBG_STRENGTH256 ((drbg_flag_t)1<<5)
  244. #define DRBG_STRENGTH_MASK (DRBG_STRENGTH128 | DRBG_STRENGTH192 | \
  245. DRBG_STRENGTH256)
  246. enum drbg_prefixes {
  247. DRBG_PREFIX0 = 0x00,
  248. DRBG_PREFIX1,
  249. DRBG_PREFIX2,
  250. DRBG_PREFIX3
  251. };
  252. #endif /* _DRBG_H */