drbg.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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/spinlock.h>
  50. #include <linux/list.h>
  51. #define SIZE_MAX (~(size_t)0)
  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. spinlock_t drbg_lock; /* 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. /* some memory the DRBG can use for its operation */
  104. unsigned char *scratchpad;
  105. void *priv_data; /* Cipher handle */
  106. bool seeded; /* DRBG fully seeded? */
  107. bool pr; /* Prediction resistance enabled? */
  108. #ifdef CONFIG_CRYPTO_FIPS
  109. bool fips_primed; /* Continuous test primed? */
  110. unsigned char *prev; /* FIPS 140-2 continuous test value */
  111. #endif
  112. const struct drbg_state_ops *d_ops;
  113. const struct drbg_core *core;
  114. struct drbg_test_data *test_data;
  115. };
  116. static inline __u8 drbg_statelen(struct drbg_state *drbg)
  117. {
  118. if (drbg && drbg->core)
  119. return drbg->core->statelen;
  120. return 0;
  121. }
  122. static inline __u8 drbg_blocklen(struct drbg_state *drbg)
  123. {
  124. if (drbg && drbg->core)
  125. return drbg->core->blocklen_bytes;
  126. return 0;
  127. }
  128. static inline __u8 drbg_keylen(struct drbg_state *drbg)
  129. {
  130. if (drbg && drbg->core)
  131. return (drbg->core->statelen - drbg->core->blocklen_bytes);
  132. return 0;
  133. }
  134. static inline size_t drbg_max_request_bytes(struct drbg_state *drbg)
  135. {
  136. /* SP800-90A requires the limit 2**19 bits, but we return bytes */
  137. return (1 << 16);
  138. }
  139. static inline size_t drbg_max_addtl(struct drbg_state *drbg)
  140. {
  141. /* SP800-90A requires 2**35 bytes additional info str / pers str */
  142. #if (__BITS_PER_LONG == 32)
  143. /*
  144. * SP800-90A allows smaller maximum numbers to be returned -- we
  145. * return SIZE_MAX - 1 to allow the verification of the enforcement
  146. * of this value in drbg_healthcheck_sanity.
  147. */
  148. return (SIZE_MAX - 1);
  149. #else
  150. return (1UL<<35);
  151. #endif
  152. }
  153. static inline size_t drbg_max_requests(struct drbg_state *drbg)
  154. {
  155. /* SP800-90A requires 2**48 maximum requests before reseeding */
  156. #if (__BITS_PER_LONG == 32)
  157. return SIZE_MAX;
  158. #else
  159. return (1UL<<48);
  160. #endif
  161. }
  162. /*
  163. * kernel crypto API input data structure for DRBG generate in case dlen
  164. * is set to 0
  165. */
  166. struct drbg_gen {
  167. unsigned char *outbuf; /* output buffer for random numbers */
  168. unsigned int outlen; /* size of output buffer */
  169. struct drbg_string *addtl; /* additional information string */
  170. struct drbg_test_data *test_data; /* test data */
  171. };
  172. /*
  173. * This is a wrapper to the kernel crypto API function of
  174. * crypto_rng_get_bytes() to allow the caller to provide additional data.
  175. *
  176. * @drng DRBG handle -- see crypto_rng_get_bytes
  177. * @outbuf output buffer -- see crypto_rng_get_bytes
  178. * @outlen length of output buffer -- see crypto_rng_get_bytes
  179. * @addtl_input additional information string input buffer
  180. * @addtllen length of additional information string buffer
  181. *
  182. * return
  183. * see crypto_rng_get_bytes
  184. */
  185. static inline int crypto_drbg_get_bytes_addtl(struct crypto_rng *drng,
  186. unsigned char *outbuf, unsigned int outlen,
  187. struct drbg_string *addtl)
  188. {
  189. int ret;
  190. struct drbg_gen genbuf;
  191. genbuf.outbuf = outbuf;
  192. genbuf.outlen = outlen;
  193. genbuf.addtl = addtl;
  194. genbuf.test_data = NULL;
  195. ret = crypto_rng_get_bytes(drng, (u8 *)&genbuf, 0);
  196. return ret;
  197. }
  198. /*
  199. * TEST code
  200. *
  201. * This is a wrapper to the kernel crypto API function of
  202. * crypto_rng_get_bytes() to allow the caller to provide additional data and
  203. * allow furnishing of test_data
  204. *
  205. * @drng DRBG handle -- see crypto_rng_get_bytes
  206. * @outbuf output buffer -- see crypto_rng_get_bytes
  207. * @outlen length of output buffer -- see crypto_rng_get_bytes
  208. * @addtl_input additional information string input buffer
  209. * @addtllen length of additional information string buffer
  210. * @test_data filled test data
  211. *
  212. * return
  213. * see crypto_rng_get_bytes
  214. */
  215. static inline int crypto_drbg_get_bytes_addtl_test(struct crypto_rng *drng,
  216. unsigned char *outbuf, unsigned int outlen,
  217. struct drbg_string *addtl,
  218. struct drbg_test_data *test_data)
  219. {
  220. int ret;
  221. struct drbg_gen genbuf;
  222. genbuf.outbuf = outbuf;
  223. genbuf.outlen = outlen;
  224. genbuf.addtl = addtl;
  225. genbuf.test_data = test_data;
  226. ret = crypto_rng_get_bytes(drng, (u8 *)&genbuf, 0);
  227. return ret;
  228. }
  229. /*
  230. * TEST code
  231. *
  232. * This is a wrapper to the kernel crypto API function of
  233. * crypto_rng_reset() to allow the caller to provide test_data
  234. *
  235. * @drng DRBG handle -- see crypto_rng_reset
  236. * @pers personalization string input buffer
  237. * @perslen length of additional information string buffer
  238. * @test_data filled test data
  239. *
  240. * return
  241. * see crypto_rng_reset
  242. */
  243. static inline int crypto_drbg_reset_test(struct crypto_rng *drng,
  244. struct drbg_string *pers,
  245. struct drbg_test_data *test_data)
  246. {
  247. int ret;
  248. struct drbg_gen genbuf;
  249. genbuf.outbuf = NULL;
  250. genbuf.outlen = 0;
  251. genbuf.addtl = pers;
  252. genbuf.test_data = test_data;
  253. ret = crypto_rng_reset(drng, (u8 *)&genbuf, 0);
  254. return ret;
  255. }
  256. /* DRBG type flags */
  257. #define DRBG_CTR ((drbg_flag_t)1<<0)
  258. #define DRBG_HMAC ((drbg_flag_t)1<<1)
  259. #define DRBG_HASH ((drbg_flag_t)1<<2)
  260. #define DRBG_TYPE_MASK (DRBG_CTR | DRBG_HMAC | DRBG_HASH)
  261. /* DRBG strength flags */
  262. #define DRBG_STRENGTH128 ((drbg_flag_t)1<<3)
  263. #define DRBG_STRENGTH192 ((drbg_flag_t)1<<4)
  264. #define DRBG_STRENGTH256 ((drbg_flag_t)1<<5)
  265. #define DRBG_STRENGTH_MASK (DRBG_STRENGTH128 | DRBG_STRENGTH192 | \
  266. DRBG_STRENGTH256)
  267. enum drbg_prefixes {
  268. DRBG_PREFIX0 = 0x00,
  269. DRBG_PREFIX1,
  270. DRBG_PREFIX2,
  271. DRBG_PREFIX3
  272. };
  273. #endif /* _DRBG_H */