drbg.h 8.9 KB

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