drbg.h 9.0 KB

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