drbg.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 <crypto/skcipher.h>
  44. #include <linux/module.h>
  45. #include <linux/crypto.h>
  46. #include <linux/slab.h>
  47. #include <crypto/internal/drbg.h>
  48. #include <crypto/internal/rng.h>
  49. #include <crypto/rng.h>
  50. #include <linux/fips.h>
  51. #include <linux/mutex.h>
  52. #include <linux/list.h>
  53. #include <linux/workqueue.h>
  54. struct drbg_state;
  55. typedef uint32_t drbg_flag_t;
  56. struct drbg_core {
  57. drbg_flag_t flags; /* flags for the cipher */
  58. __u8 statelen; /* maximum state length */
  59. __u8 blocklen_bytes; /* block size of output in bytes */
  60. char cra_name[CRYPTO_MAX_ALG_NAME]; /* mapping to kernel crypto API */
  61. /* kernel crypto API backend cipher name */
  62. char backend_cra_name[CRYPTO_MAX_ALG_NAME];
  63. };
  64. struct drbg_state_ops {
  65. int (*update)(struct drbg_state *drbg, struct list_head *seed,
  66. int reseed);
  67. int (*generate)(struct drbg_state *drbg,
  68. unsigned char *buf, unsigned int buflen,
  69. struct list_head *addtl);
  70. int (*crypto_init)(struct drbg_state *drbg);
  71. int (*crypto_fini)(struct drbg_state *drbg);
  72. };
  73. struct drbg_test_data {
  74. struct drbg_string *testentropy; /* TEST PARAMETER: test entropy */
  75. };
  76. enum drbg_seed_state {
  77. DRBG_SEED_STATE_UNSEEDED,
  78. DRBG_SEED_STATE_PARTIAL, /* Seeded with !rng_is_initialized() */
  79. DRBG_SEED_STATE_FULL,
  80. };
  81. struct drbg_state {
  82. struct mutex drbg_mutex; /* lock around DRBG */
  83. unsigned char *V; /* internal state 10.1.1.1 1a) */
  84. unsigned char *Vbuf;
  85. /* hash: static value 10.1.1.1 1b) hmac / ctr: key */
  86. unsigned char *C;
  87. unsigned char *Cbuf;
  88. /* Number of RNG requests since last reseed -- 10.1.1.1 1c) */
  89. size_t reseed_ctr;
  90. size_t reseed_threshold;
  91. /* some memory the DRBG can use for its operation */
  92. unsigned char *scratchpad;
  93. unsigned char *scratchpadbuf;
  94. void *priv_data; /* Cipher handle */
  95. struct crypto_skcipher *ctr_handle; /* CTR mode cipher handle */
  96. struct skcipher_request *ctr_req; /* CTR mode request handle */
  97. __u8 *outscratchpadbuf; /* CTR mode output scratchpad */
  98. __u8 *outscratchpad; /* CTR mode aligned outbuf */
  99. struct crypto_wait ctr_wait; /* CTR mode async wait obj */
  100. struct scatterlist sg_in, sg_out; /* CTR mode SGLs */
  101. enum drbg_seed_state seeded; /* DRBG fully seeded? */
  102. unsigned long last_seed_time;
  103. bool pr; /* Prediction resistance enabled? */
  104. bool fips_primed; /* Continuous test primed? */
  105. unsigned char *prev; /* FIPS 140-2 continuous test value */
  106. struct crypto_rng *jent;
  107. const struct drbg_state_ops *d_ops;
  108. const struct drbg_core *core;
  109. struct drbg_string test_data;
  110. };
  111. static inline __u8 drbg_statelen(struct drbg_state *drbg)
  112. {
  113. if (drbg && drbg->core)
  114. return drbg->core->statelen;
  115. return 0;
  116. }
  117. static inline __u8 drbg_blocklen(struct drbg_state *drbg)
  118. {
  119. if (drbg && drbg->core)
  120. return drbg->core->blocklen_bytes;
  121. return 0;
  122. }
  123. static inline __u8 drbg_keylen(struct drbg_state *drbg)
  124. {
  125. if (drbg && drbg->core)
  126. return (drbg->core->statelen - drbg->core->blocklen_bytes);
  127. return 0;
  128. }
  129. static inline size_t drbg_max_request_bytes(struct drbg_state *drbg)
  130. {
  131. /* SP800-90A requires the limit 2**19 bits, but we return bytes */
  132. return (1 << 16);
  133. }
  134. static inline size_t drbg_max_addtl(struct drbg_state *drbg)
  135. {
  136. /* SP800-90A requires 2**35 bytes additional info str / pers str */
  137. #if (__BITS_PER_LONG == 32)
  138. /*
  139. * SP800-90A allows smaller maximum numbers to be returned -- we
  140. * return SIZE_MAX - 1 to allow the verification of the enforcement
  141. * of this value in drbg_healthcheck_sanity.
  142. */
  143. return (SIZE_MAX - 1);
  144. #else
  145. return (1UL<<35);
  146. #endif
  147. }
  148. static inline size_t drbg_max_requests(struct drbg_state *drbg)
  149. {
  150. /* SP800-90A requires 2**48 maximum requests before reseeding */
  151. return (1<<20);
  152. }
  153. /*
  154. * This is a wrapper to the kernel crypto API function of
  155. * crypto_rng_generate() to allow the caller to provide additional data.
  156. *
  157. * @drng DRBG handle -- see crypto_rng_get_bytes
  158. * @outbuf output buffer -- see crypto_rng_get_bytes
  159. * @outlen length of output buffer -- see crypto_rng_get_bytes
  160. * @addtl_input additional information string input buffer
  161. * @addtllen length of additional information string buffer
  162. *
  163. * return
  164. * see crypto_rng_get_bytes
  165. */
  166. static inline int crypto_drbg_get_bytes_addtl(struct crypto_rng *drng,
  167. unsigned char *outbuf, unsigned int outlen,
  168. struct drbg_string *addtl)
  169. {
  170. return crypto_rng_generate(drng, addtl->buf, addtl->len,
  171. outbuf, outlen);
  172. }
  173. /*
  174. * TEST code
  175. *
  176. * This is a wrapper to the kernel crypto API function of
  177. * crypto_rng_generate() to allow the caller to provide additional data and
  178. * allow furnishing of test_data
  179. *
  180. * @drng DRBG handle -- see crypto_rng_get_bytes
  181. * @outbuf output buffer -- see crypto_rng_get_bytes
  182. * @outlen length of output buffer -- see crypto_rng_get_bytes
  183. * @addtl_input additional information string input buffer
  184. * @addtllen length of additional information string buffer
  185. * @test_data filled test data
  186. *
  187. * return
  188. * see crypto_rng_get_bytes
  189. */
  190. static inline int crypto_drbg_get_bytes_addtl_test(struct crypto_rng *drng,
  191. unsigned char *outbuf, unsigned int outlen,
  192. struct drbg_string *addtl,
  193. struct drbg_test_data *test_data)
  194. {
  195. crypto_rng_set_entropy(drng, test_data->testentropy->buf,
  196. test_data->testentropy->len);
  197. return crypto_rng_generate(drng, addtl->buf, addtl->len,
  198. outbuf, outlen);
  199. }
  200. /*
  201. * TEST code
  202. *
  203. * This is a wrapper to the kernel crypto API function of
  204. * crypto_rng_reset() to allow the caller to provide test_data
  205. *
  206. * @drng DRBG handle -- see crypto_rng_reset
  207. * @pers personalization string input buffer
  208. * @perslen length of additional information string buffer
  209. * @test_data filled test data
  210. *
  211. * return
  212. * see crypto_rng_reset
  213. */
  214. static inline int crypto_drbg_reset_test(struct crypto_rng *drng,
  215. struct drbg_string *pers,
  216. struct drbg_test_data *test_data)
  217. {
  218. crypto_rng_set_entropy(drng, test_data->testentropy->buf,
  219. test_data->testentropy->len);
  220. return crypto_rng_reset(drng, pers->buf, pers->len);
  221. }
  222. /* DRBG type flags */
  223. #define DRBG_CTR ((drbg_flag_t)1<<0)
  224. #define DRBG_HMAC ((drbg_flag_t)1<<1)
  225. #define DRBG_HASH ((drbg_flag_t)1<<2)
  226. #define DRBG_TYPE_MASK (DRBG_CTR | DRBG_HMAC | DRBG_HASH)
  227. /* DRBG strength flags */
  228. #define DRBG_STRENGTH128 ((drbg_flag_t)1<<3)
  229. #define DRBG_STRENGTH192 ((drbg_flag_t)1<<4)
  230. #define DRBG_STRENGTH256 ((drbg_flag_t)1<<5)
  231. #define DRBG_STRENGTH_MASK (DRBG_STRENGTH128 | DRBG_STRENGTH192 | \
  232. DRBG_STRENGTH256)
  233. enum drbg_prefixes {
  234. DRBG_PREFIX0 = 0x00,
  235. DRBG_PREFIX1,
  236. DRBG_PREFIX2,
  237. DRBG_PREFIX3
  238. };
  239. #endif /* _DRBG_H */