13e16f959SStephan Mueller /*
23e16f959SStephan Mueller * DRBG based on NIST SP800-90A
33e16f959SStephan Mueller *
43e16f959SStephan Mueller * Copyright Stephan Mueller <smueller@chronox.de>, 2014
53e16f959SStephan Mueller *
63e16f959SStephan Mueller * Redistribution and use in source and binary forms, with or without
73e16f959SStephan Mueller * modification, are permitted provided that the following conditions
83e16f959SStephan Mueller * are met:
93e16f959SStephan Mueller * 1. Redistributions of source code must retain the above copyright
103e16f959SStephan Mueller * notice, and the entire permission notice in its entirety,
113e16f959SStephan Mueller * including the disclaimer of warranties.
123e16f959SStephan Mueller * 2. Redistributions in binary form must reproduce the above copyright
133e16f959SStephan Mueller * notice, this list of conditions and the following disclaimer in the
143e16f959SStephan Mueller * documentation and/or other materials provided with the distribution.
153e16f959SStephan Mueller * 3. The name of the author may not be used to endorse or promote
163e16f959SStephan Mueller * products derived from this software without specific prior
173e16f959SStephan Mueller * written permission.
183e16f959SStephan Mueller *
193e16f959SStephan Mueller * ALTERNATIVELY, this product may be distributed under the terms of
203e16f959SStephan Mueller * the GNU General Public License, in which case the provisions of the GPL are
213e16f959SStephan Mueller * required INSTEAD OF the above restrictions. (This clause is
223e16f959SStephan Mueller * necessary due to a potential bad interaction between the GPL and
233e16f959SStephan Mueller * the restrictions contained in a BSD-style copyright.)
243e16f959SStephan Mueller *
253e16f959SStephan Mueller * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
263e16f959SStephan Mueller * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
273e16f959SStephan Mueller * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
283e16f959SStephan Mueller * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
293e16f959SStephan Mueller * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
303e16f959SStephan Mueller * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
313e16f959SStephan Mueller * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
323e16f959SStephan Mueller * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
333e16f959SStephan Mueller * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
343e16f959SStephan Mueller * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
353e16f959SStephan Mueller * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
363e16f959SStephan Mueller * DAMAGE.
373e16f959SStephan Mueller */
383e16f959SStephan Mueller
393e16f959SStephan Mueller #ifndef _DRBG_H
403e16f959SStephan Mueller #define _DRBG_H
413e16f959SStephan Mueller
423e16f959SStephan Mueller
433e16f959SStephan Mueller #include <linux/random.h>
443e16f959SStephan Mueller #include <linux/scatterlist.h>
453e16f959SStephan Mueller #include <crypto/hash.h>
4635591285SStephan Mueller #include <crypto/skcipher.h>
473e16f959SStephan Mueller #include <linux/module.h>
483e16f959SStephan Mueller #include <linux/crypto.h>
493e16f959SStephan Mueller #include <linux/slab.h>
503e16f959SStephan Mueller #include <crypto/internal/rng.h>
513e16f959SStephan Mueller #include <crypto/rng.h>
523e16f959SStephan Mueller #include <linux/fips.h>
5376899a41SStephan Mueller #include <linux/mutex.h>
548c987166SStephan Mueller #include <linux/list.h>
554c787990SStephan Mueller #include <linux/workqueue.h>
563e16f959SStephan Mueller
573e16f959SStephan Mueller /*
583e16f959SStephan Mueller * Concatenation Helper and string operation helper
593e16f959SStephan Mueller *
603e16f959SStephan Mueller * SP800-90A requires the concatenation of different data. To avoid copying
613e16f959SStephan Mueller * buffers around or allocate additional memory, the following data structure
623e16f959SStephan Mueller * is used to point to the original memory with its size. In addition, it
633e16f959SStephan Mueller * is used to build a linked list. The linked list defines the concatenation
643e16f959SStephan Mueller * of individual buffers. The order of memory block referenced in that
653e16f959SStephan Mueller * linked list determines the order of concatenation.
663e16f959SStephan Mueller */
673e16f959SStephan Mueller struct drbg_string {
683e16f959SStephan Mueller const unsigned char *buf;
693e16f959SStephan Mueller size_t len;
708c987166SStephan Mueller struct list_head list;
713e16f959SStephan Mueller };
723e16f959SStephan Mueller
drbg_string_fill(struct drbg_string * string,const unsigned char * buf,size_t len)733e16f959SStephan Mueller static inline void drbg_string_fill(struct drbg_string *string,
743e16f959SStephan Mueller const unsigned char *buf, size_t len)
753e16f959SStephan Mueller {
763e16f959SStephan Mueller string->buf = buf;
773e16f959SStephan Mueller string->len = len;
788c987166SStephan Mueller INIT_LIST_HEAD(&string->list);
793e16f959SStephan Mueller }
803e16f959SStephan Mueller
813e16f959SStephan Mueller struct drbg_state;
823e16f959SStephan Mueller typedef uint32_t drbg_flag_t;
833e16f959SStephan Mueller
843e16f959SStephan Mueller struct drbg_core {
853e16f959SStephan Mueller drbg_flag_t flags; /* flags for the cipher */
863e16f959SStephan Mueller __u8 statelen; /* maximum state length */
873e16f959SStephan Mueller __u8 blocklen_bytes; /* block size of output in bytes */
883e16f959SStephan Mueller char cra_name[CRYPTO_MAX_ALG_NAME]; /* mapping to kernel crypto API */
893e16f959SStephan Mueller /* kernel crypto API backend cipher name */
903e16f959SStephan Mueller char backend_cra_name[CRYPTO_MAX_ALG_NAME];
913e16f959SStephan Mueller };
923e16f959SStephan Mueller
933e16f959SStephan Mueller struct drbg_state_ops {
948c987166SStephan Mueller int (*update)(struct drbg_state *drbg, struct list_head *seed,
953e16f959SStephan Mueller int reseed);
963e16f959SStephan Mueller int (*generate)(struct drbg_state *drbg,
973e16f959SStephan Mueller unsigned char *buf, unsigned int buflen,
9827e4de2bSStephan Mueller struct list_head *addtl);
993e16f959SStephan Mueller int (*crypto_init)(struct drbg_state *drbg);
1003e16f959SStephan Mueller int (*crypto_fini)(struct drbg_state *drbg);
1013e16f959SStephan Mueller
1023e16f959SStephan Mueller };
1033e16f959SStephan Mueller
1043e16f959SStephan Mueller struct drbg_test_data {
1053e16f959SStephan Mueller struct drbg_string *testentropy; /* TEST PARAMETER: test entropy */
1063e16f959SStephan Mueller };
1073e16f959SStephan Mueller
108ce8ce31bSNicolai Stange enum drbg_seed_state {
109ce8ce31bSNicolai Stange DRBG_SEED_STATE_UNSEEDED,
1102bcd2544SNicolai Stange DRBG_SEED_STATE_PARTIAL, /* Seeded with !rng_is_initialized() */
111ce8ce31bSNicolai Stange DRBG_SEED_STATE_FULL,
112ce8ce31bSNicolai Stange };
113ce8ce31bSNicolai Stange
1143e16f959SStephan Mueller struct drbg_state {
11576899a41SStephan Mueller struct mutex drbg_mutex; /* lock around DRBG */
1163e16f959SStephan Mueller unsigned char *V; /* internal state 10.1.1.1 1a) */
1173cfc3b97SStephan Mueller unsigned char *Vbuf;
1183e16f959SStephan Mueller /* hash: static value 10.1.1.1 1b) hmac / ctr: key */
1193e16f959SStephan Mueller unsigned char *C;
1203cfc3b97SStephan Mueller unsigned char *Cbuf;
1213e16f959SStephan Mueller /* Number of RNG requests since last reseed -- 10.1.1.1 1c) */
1223e16f959SStephan Mueller size_t reseed_ctr;
12342ea507fSStephan Mueller size_t reseed_threshold;
1243e16f959SStephan Mueller /* some memory the DRBG can use for its operation */
1253e16f959SStephan Mueller unsigned char *scratchpad;
1263cfc3b97SStephan Mueller unsigned char *scratchpadbuf;
1273e16f959SStephan Mueller void *priv_data; /* Cipher handle */
12835591285SStephan Mueller
12935591285SStephan Mueller struct crypto_skcipher *ctr_handle; /* CTR mode cipher handle */
13035591285SStephan Mueller struct skcipher_request *ctr_req; /* CTR mode request handle */
13151029812SStephan Mueller __u8 *outscratchpadbuf; /* CTR mode output scratchpad */
13251029812SStephan Mueller __u8 *outscratchpad; /* CTR mode aligned outbuf */
13385a2dea4SGilad Ben-Yossef struct crypto_wait ctr_wait; /* CTR mode async wait obj */
134cf862cbcSStephan Mueller struct scatterlist sg_in, sg_out; /* CTR mode SGLs */
13535591285SStephan Mueller
136ce8ce31bSNicolai Stange enum drbg_seed_state seeded; /* DRBG fully seeded? */
137*8ea5ee00SNicolai Stange unsigned long last_seed_time;
1383e16f959SStephan Mueller bool pr; /* Prediction resistance enabled? */
139db07cd26SStephan Mueller bool fips_primed; /* Continuous test primed? */
140db07cd26SStephan Mueller unsigned char *prev; /* FIPS 140-2 continuous test value */
141b8ec5ba4SStephan Mueller struct crypto_rng *jent;
1423e16f959SStephan Mueller const struct drbg_state_ops *d_ops;
1433e16f959SStephan Mueller const struct drbg_core *core;
1448fded592SHerbert Xu struct drbg_string test_data;
1453e16f959SStephan Mueller };
1463e16f959SStephan Mueller
drbg_statelen(struct drbg_state * drbg)1473e16f959SStephan Mueller static inline __u8 drbg_statelen(struct drbg_state *drbg)
1483e16f959SStephan Mueller {
1493e16f959SStephan Mueller if (drbg && drbg->core)
1503e16f959SStephan Mueller return drbg->core->statelen;
1513e16f959SStephan Mueller return 0;
1523e16f959SStephan Mueller }
1533e16f959SStephan Mueller
drbg_blocklen(struct drbg_state * drbg)1543e16f959SStephan Mueller static inline __u8 drbg_blocklen(struct drbg_state *drbg)
1553e16f959SStephan Mueller {
1563e16f959SStephan Mueller if (drbg && drbg->core)
1573e16f959SStephan Mueller return drbg->core->blocklen_bytes;
1583e16f959SStephan Mueller return 0;
1593e16f959SStephan Mueller }
1603e16f959SStephan Mueller
drbg_keylen(struct drbg_state * drbg)1613e16f959SStephan Mueller static inline __u8 drbg_keylen(struct drbg_state *drbg)
1623e16f959SStephan Mueller {
1633e16f959SStephan Mueller if (drbg && drbg->core)
1643e16f959SStephan Mueller return (drbg->core->statelen - drbg->core->blocklen_bytes);
1653e16f959SStephan Mueller return 0;
1663e16f959SStephan Mueller }
1673e16f959SStephan Mueller
drbg_max_request_bytes(struct drbg_state * drbg)1683e16f959SStephan Mueller static inline size_t drbg_max_request_bytes(struct drbg_state *drbg)
1693e16f959SStephan Mueller {
17005c81ccdSStephan Mueller /* SP800-90A requires the limit 2**19 bits, but we return bytes */
17105c81ccdSStephan Mueller return (1 << 16);
1723e16f959SStephan Mueller }
1733e16f959SStephan Mueller
drbg_max_addtl(struct drbg_state * drbg)1743e16f959SStephan Mueller static inline size_t drbg_max_addtl(struct drbg_state *drbg)
1753e16f959SStephan Mueller {
17605c81ccdSStephan Mueller /* SP800-90A requires 2**35 bytes additional info str / pers str */
177b9347affSStephan Mueller #if (__BITS_PER_LONG == 32)
178b9347affSStephan Mueller /*
179b9347affSStephan Mueller * SP800-90A allows smaller maximum numbers to be returned -- we
180b9347affSStephan Mueller * return SIZE_MAX - 1 to allow the verification of the enforcement
181b9347affSStephan Mueller * of this value in drbg_healthcheck_sanity.
182b9347affSStephan Mueller */
183b9347affSStephan Mueller return (SIZE_MAX - 1);
184b9347affSStephan Mueller #else
18505c81ccdSStephan Mueller return (1UL<<35);
186b9347affSStephan Mueller #endif
1873e16f959SStephan Mueller }
1883e16f959SStephan Mueller
drbg_max_requests(struct drbg_state * drbg)1893e16f959SStephan Mueller static inline size_t drbg_max_requests(struct drbg_state *drbg)
1903e16f959SStephan Mueller {
19105c81ccdSStephan Mueller /* SP800-90A requires 2**48 maximum requests before reseeding */
19297f2650eSStephan Müller return (1<<20);
1933e16f959SStephan Mueller }
1943e16f959SStephan Mueller
1953e16f959SStephan Mueller /*
1963e16f959SStephan Mueller * This is a wrapper to the kernel crypto API function of
1978fded592SHerbert Xu * crypto_rng_generate() to allow the caller to provide additional data.
1983e16f959SStephan Mueller *
1993e16f959SStephan Mueller * @drng DRBG handle -- see crypto_rng_get_bytes
2003e16f959SStephan Mueller * @outbuf output buffer -- see crypto_rng_get_bytes
2013e16f959SStephan Mueller * @outlen length of output buffer -- see crypto_rng_get_bytes
2023e16f959SStephan Mueller * @addtl_input additional information string input buffer
2033e16f959SStephan Mueller * @addtllen length of additional information string buffer
2043e16f959SStephan Mueller *
2053e16f959SStephan Mueller * return
2063e16f959SStephan Mueller * see crypto_rng_get_bytes
2073e16f959SStephan Mueller */
crypto_drbg_get_bytes_addtl(struct crypto_rng * drng,unsigned char * outbuf,unsigned int outlen,struct drbg_string * addtl)2083e16f959SStephan Mueller static inline int crypto_drbg_get_bytes_addtl(struct crypto_rng *drng,
2093e16f959SStephan Mueller unsigned char *outbuf, unsigned int outlen,
2103e16f959SStephan Mueller struct drbg_string *addtl)
2113e16f959SStephan Mueller {
2128fded592SHerbert Xu return crypto_rng_generate(drng, addtl->buf, addtl->len,
2138fded592SHerbert Xu outbuf, outlen);
2143e16f959SStephan Mueller }
2153e16f959SStephan Mueller
2163e16f959SStephan Mueller /*
2173e16f959SStephan Mueller * TEST code
2183e16f959SStephan Mueller *
2193e16f959SStephan Mueller * This is a wrapper to the kernel crypto API function of
2208fded592SHerbert Xu * crypto_rng_generate() to allow the caller to provide additional data and
2213e16f959SStephan Mueller * allow furnishing of test_data
2223e16f959SStephan Mueller *
2233e16f959SStephan Mueller * @drng DRBG handle -- see crypto_rng_get_bytes
2243e16f959SStephan Mueller * @outbuf output buffer -- see crypto_rng_get_bytes
2253e16f959SStephan Mueller * @outlen length of output buffer -- see crypto_rng_get_bytes
2263e16f959SStephan Mueller * @addtl_input additional information string input buffer
2273e16f959SStephan Mueller * @addtllen length of additional information string buffer
2283e16f959SStephan Mueller * @test_data filled test data
2293e16f959SStephan Mueller *
2303e16f959SStephan Mueller * return
2313e16f959SStephan Mueller * see crypto_rng_get_bytes
2323e16f959SStephan Mueller */
crypto_drbg_get_bytes_addtl_test(struct crypto_rng * drng,unsigned char * outbuf,unsigned int outlen,struct drbg_string * addtl,struct drbg_test_data * test_data)2333e16f959SStephan Mueller static inline int crypto_drbg_get_bytes_addtl_test(struct crypto_rng *drng,
2343e16f959SStephan Mueller unsigned char *outbuf, unsigned int outlen,
2353e16f959SStephan Mueller struct drbg_string *addtl,
2363e16f959SStephan Mueller struct drbg_test_data *test_data)
2373e16f959SStephan Mueller {
2388fded592SHerbert Xu crypto_rng_set_entropy(drng, test_data->testentropy->buf,
2398fded592SHerbert Xu test_data->testentropy->len);
2408fded592SHerbert Xu return crypto_rng_generate(drng, addtl->buf, addtl->len,
2418fded592SHerbert Xu outbuf, outlen);
2423e16f959SStephan Mueller }
2433e16f959SStephan Mueller
2443e16f959SStephan Mueller /*
2453e16f959SStephan Mueller * TEST code
2463e16f959SStephan Mueller *
2473e16f959SStephan Mueller * This is a wrapper to the kernel crypto API function of
2483e16f959SStephan Mueller * crypto_rng_reset() to allow the caller to provide test_data
2493e16f959SStephan Mueller *
2503e16f959SStephan Mueller * @drng DRBG handle -- see crypto_rng_reset
2513e16f959SStephan Mueller * @pers personalization string input buffer
2523e16f959SStephan Mueller * @perslen length of additional information string buffer
2533e16f959SStephan Mueller * @test_data filled test data
2543e16f959SStephan Mueller *
2553e16f959SStephan Mueller * return
2563e16f959SStephan Mueller * see crypto_rng_reset
2573e16f959SStephan Mueller */
crypto_drbg_reset_test(struct crypto_rng * drng,struct drbg_string * pers,struct drbg_test_data * test_data)2583e16f959SStephan Mueller static inline int crypto_drbg_reset_test(struct crypto_rng *drng,
2593e16f959SStephan Mueller struct drbg_string *pers,
2603e16f959SStephan Mueller struct drbg_test_data *test_data)
2613e16f959SStephan Mueller {
2628fded592SHerbert Xu crypto_rng_set_entropy(drng, test_data->testentropy->buf,
2638fded592SHerbert Xu test_data->testentropy->len);
2648fded592SHerbert Xu return crypto_rng_reset(drng, pers->buf, pers->len);
2653e16f959SStephan Mueller }
2663e16f959SStephan Mueller
2673e16f959SStephan Mueller /* DRBG type flags */
2683e16f959SStephan Mueller #define DRBG_CTR ((drbg_flag_t)1<<0)
2693e16f959SStephan Mueller #define DRBG_HMAC ((drbg_flag_t)1<<1)
2703e16f959SStephan Mueller #define DRBG_HASH ((drbg_flag_t)1<<2)
2713e16f959SStephan Mueller #define DRBG_TYPE_MASK (DRBG_CTR | DRBG_HMAC | DRBG_HASH)
2723e16f959SStephan Mueller /* DRBG strength flags */
2733e16f959SStephan Mueller #define DRBG_STRENGTH128 ((drbg_flag_t)1<<3)
2743e16f959SStephan Mueller #define DRBG_STRENGTH192 ((drbg_flag_t)1<<4)
2753e16f959SStephan Mueller #define DRBG_STRENGTH256 ((drbg_flag_t)1<<5)
2763e16f959SStephan Mueller #define DRBG_STRENGTH_MASK (DRBG_STRENGTH128 | DRBG_STRENGTH192 | \
2773e16f959SStephan Mueller DRBG_STRENGTH256)
2783e16f959SStephan Mueller
2793e16f959SStephan Mueller enum drbg_prefixes {
2803e16f959SStephan Mueller DRBG_PREFIX0 = 0x00,
2813e16f959SStephan Mueller DRBG_PREFIX1,
2823e16f959SStephan Mueller DRBG_PREFIX2,
2833e16f959SStephan Mueller DRBG_PREFIX3
2843e16f959SStephan Mueller };
2853e16f959SStephan Mueller
2863e16f959SStephan Mueller #endif /* _DRBG_H */
287