drbg.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. /*
  79. * maximum length of personalization string or additional input
  80. * string -- exponent for base 2
  81. */
  82. __u8 max_addtllen;
  83. /* maximum bits per RNG request -- exponent for base 2*/
  84. __u8 max_bits;
  85. /* maximum number of requests -- exponent for base 2 */
  86. __u8 max_req;
  87. __u8 blocklen_bytes; /* block size of output in bytes */
  88. char cra_name[CRYPTO_MAX_ALG_NAME]; /* mapping to kernel crypto API */
  89. /* kernel crypto API backend cipher name */
  90. char backend_cra_name[CRYPTO_MAX_ALG_NAME];
  91. };
  92. struct drbg_state_ops {
  93. int (*update)(struct drbg_state *drbg, struct list_head *seed,
  94. int reseed);
  95. int (*generate)(struct drbg_state *drbg,
  96. unsigned char *buf, unsigned int buflen,
  97. struct list_head *addtl);
  98. int (*crypto_init)(struct drbg_state *drbg);
  99. int (*crypto_fini)(struct drbg_state *drbg);
  100. };
  101. struct drbg_test_data {
  102. struct drbg_string *testentropy; /* TEST PARAMETER: test entropy */
  103. };
  104. struct drbg_state {
  105. spinlock_t drbg_lock; /* lock around DRBG */
  106. unsigned char *V; /* internal state 10.1.1.1 1a) */
  107. /* hash: static value 10.1.1.1 1b) hmac / ctr: key */
  108. unsigned char *C;
  109. /* Number of RNG requests since last reseed -- 10.1.1.1 1c) */
  110. size_t reseed_ctr;
  111. /* some memory the DRBG can use for its operation */
  112. unsigned char *scratchpad;
  113. void *priv_data; /* Cipher handle */
  114. bool seeded; /* DRBG fully seeded? */
  115. bool pr; /* Prediction resistance enabled? */
  116. #ifdef CONFIG_CRYPTO_FIPS
  117. bool fips_primed; /* Continuous test primed? */
  118. unsigned char *prev; /* FIPS 140-2 continuous test value */
  119. #endif
  120. const struct drbg_state_ops *d_ops;
  121. const struct drbg_core *core;
  122. struct drbg_test_data *test_data;
  123. };
  124. static inline __u8 drbg_statelen(struct drbg_state *drbg)
  125. {
  126. if (drbg && drbg->core)
  127. return drbg->core->statelen;
  128. return 0;
  129. }
  130. static inline __u8 drbg_blocklen(struct drbg_state *drbg)
  131. {
  132. if (drbg && drbg->core)
  133. return drbg->core->blocklen_bytes;
  134. return 0;
  135. }
  136. static inline __u8 drbg_keylen(struct drbg_state *drbg)
  137. {
  138. if (drbg && drbg->core)
  139. return (drbg->core->statelen - drbg->core->blocklen_bytes);
  140. return 0;
  141. }
  142. static inline size_t drbg_max_request_bytes(struct drbg_state *drbg)
  143. {
  144. /* max_bits is in bits, but buflen is in bytes */
  145. return (1 << (drbg->core->max_bits - 3));
  146. }
  147. static inline size_t drbg_max_addtl(struct drbg_state *drbg)
  148. {
  149. return (1UL<<(drbg->core->max_addtllen));
  150. }
  151. static inline size_t drbg_max_requests(struct drbg_state *drbg)
  152. {
  153. return (1UL<<(drbg->core->max_req));
  154. }
  155. /*
  156. * kernel crypto API input data structure for DRBG generate in case dlen
  157. * is set to 0
  158. */
  159. struct drbg_gen {
  160. unsigned char *outbuf; /* output buffer for random numbers */
  161. unsigned int outlen; /* size of output buffer */
  162. struct drbg_string *addtl; /* additional information string */
  163. struct drbg_test_data *test_data; /* test data */
  164. };
  165. /*
  166. * This is a wrapper to the kernel crypto API function of
  167. * crypto_rng_get_bytes() to allow the caller to provide additional data.
  168. *
  169. * @drng DRBG handle -- see crypto_rng_get_bytes
  170. * @outbuf output buffer -- see crypto_rng_get_bytes
  171. * @outlen length of output buffer -- see crypto_rng_get_bytes
  172. * @addtl_input additional information string input buffer
  173. * @addtllen length of additional information string buffer
  174. *
  175. * return
  176. * see crypto_rng_get_bytes
  177. */
  178. static inline int crypto_drbg_get_bytes_addtl(struct crypto_rng *drng,
  179. unsigned char *outbuf, unsigned int outlen,
  180. struct drbg_string *addtl)
  181. {
  182. int ret;
  183. struct drbg_gen genbuf;
  184. genbuf.outbuf = outbuf;
  185. genbuf.outlen = outlen;
  186. genbuf.addtl = addtl;
  187. genbuf.test_data = NULL;
  188. ret = crypto_rng_get_bytes(drng, (u8 *)&genbuf, 0);
  189. return ret;
  190. }
  191. /*
  192. * TEST code
  193. *
  194. * This is a wrapper to the kernel crypto API function of
  195. * crypto_rng_get_bytes() to allow the caller to provide additional data and
  196. * allow furnishing of test_data
  197. *
  198. * @drng DRBG handle -- see crypto_rng_get_bytes
  199. * @outbuf output buffer -- see crypto_rng_get_bytes
  200. * @outlen length of output buffer -- see crypto_rng_get_bytes
  201. * @addtl_input additional information string input buffer
  202. * @addtllen length of additional information string buffer
  203. * @test_data filled test data
  204. *
  205. * return
  206. * see crypto_rng_get_bytes
  207. */
  208. static inline int crypto_drbg_get_bytes_addtl_test(struct crypto_rng *drng,
  209. unsigned char *outbuf, unsigned int outlen,
  210. struct drbg_string *addtl,
  211. struct drbg_test_data *test_data)
  212. {
  213. int ret;
  214. struct drbg_gen genbuf;
  215. genbuf.outbuf = outbuf;
  216. genbuf.outlen = outlen;
  217. genbuf.addtl = addtl;
  218. genbuf.test_data = test_data;
  219. ret = crypto_rng_get_bytes(drng, (u8 *)&genbuf, 0);
  220. return ret;
  221. }
  222. /*
  223. * TEST code
  224. *
  225. * This is a wrapper to the kernel crypto API function of
  226. * crypto_rng_reset() to allow the caller to provide test_data
  227. *
  228. * @drng DRBG handle -- see crypto_rng_reset
  229. * @pers personalization string input buffer
  230. * @perslen length of additional information string buffer
  231. * @test_data filled test data
  232. *
  233. * return
  234. * see crypto_rng_reset
  235. */
  236. static inline int crypto_drbg_reset_test(struct crypto_rng *drng,
  237. struct drbg_string *pers,
  238. struct drbg_test_data *test_data)
  239. {
  240. int ret;
  241. struct drbg_gen genbuf;
  242. genbuf.outbuf = NULL;
  243. genbuf.outlen = 0;
  244. genbuf.addtl = pers;
  245. genbuf.test_data = test_data;
  246. ret = crypto_rng_reset(drng, (u8 *)&genbuf, 0);
  247. return ret;
  248. }
  249. /* DRBG type flags */
  250. #define DRBG_CTR ((drbg_flag_t)1<<0)
  251. #define DRBG_HMAC ((drbg_flag_t)1<<1)
  252. #define DRBG_HASH ((drbg_flag_t)1<<2)
  253. #define DRBG_TYPE_MASK (DRBG_CTR | DRBG_HMAC | DRBG_HASH)
  254. /* DRBG strength flags */
  255. #define DRBG_STRENGTH128 ((drbg_flag_t)1<<3)
  256. #define DRBG_STRENGTH192 ((drbg_flag_t)1<<4)
  257. #define DRBG_STRENGTH256 ((drbg_flag_t)1<<5)
  258. #define DRBG_STRENGTH_MASK (DRBG_STRENGTH128 | DRBG_STRENGTH192 | \
  259. DRBG_STRENGTH256)
  260. enum drbg_prefixes {
  261. DRBG_PREFIX0 = 0x00,
  262. DRBG_PREFIX1,
  263. DRBG_PREFIX2,
  264. DRBG_PREFIX3
  265. };
  266. #endif /* _DRBG_H */