1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * DRBG based on NIST SP800-90A
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright Stephan Mueller <smueller@chronox.de>, 2014
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Redistribution and use in source and binary forms, with or without
7*4882a593Smuzhiyun * modification, are permitted provided that the following conditions
8*4882a593Smuzhiyun * are met:
9*4882a593Smuzhiyun * 1. Redistributions of source code must retain the above copyright
10*4882a593Smuzhiyun * notice, and the entire permission notice in its entirety,
11*4882a593Smuzhiyun * including the disclaimer of warranties.
12*4882a593Smuzhiyun * 2. Redistributions in binary form must reproduce the above copyright
13*4882a593Smuzhiyun * notice, this list of conditions and the following disclaimer in the
14*4882a593Smuzhiyun * documentation and/or other materials provided with the distribution.
15*4882a593Smuzhiyun * 3. The name of the author may not be used to endorse or promote
16*4882a593Smuzhiyun * products derived from this software without specific prior
17*4882a593Smuzhiyun * written permission.
18*4882a593Smuzhiyun *
19*4882a593Smuzhiyun * ALTERNATIVELY, this product may be distributed under the terms of
20*4882a593Smuzhiyun * the GNU General Public License, in which case the provisions of the GPL are
21*4882a593Smuzhiyun * required INSTEAD OF the above restrictions. (This clause is
22*4882a593Smuzhiyun * necessary due to a potential bad interaction between the GPL and
23*4882a593Smuzhiyun * the restrictions contained in a BSD-style copyright.)
24*4882a593Smuzhiyun *
25*4882a593Smuzhiyun * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26*4882a593Smuzhiyun * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27*4882a593Smuzhiyun * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
28*4882a593Smuzhiyun * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
29*4882a593Smuzhiyun * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30*4882a593Smuzhiyun * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
31*4882a593Smuzhiyun * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32*4882a593Smuzhiyun * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33*4882a593Smuzhiyun * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34*4882a593Smuzhiyun * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
35*4882a593Smuzhiyun * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
36*4882a593Smuzhiyun * DAMAGE.
37*4882a593Smuzhiyun */
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun #ifndef _DRBG_H
40*4882a593Smuzhiyun #define _DRBG_H
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun #include <linux/random.h>
44*4882a593Smuzhiyun #include <linux/scatterlist.h>
45*4882a593Smuzhiyun #include <crypto/hash.h>
46*4882a593Smuzhiyun #include <crypto/skcipher.h>
47*4882a593Smuzhiyun #include <linux/module.h>
48*4882a593Smuzhiyun #include <linux/crypto.h>
49*4882a593Smuzhiyun #include <linux/slab.h>
50*4882a593Smuzhiyun #include <crypto/internal/rng.h>
51*4882a593Smuzhiyun #include <crypto/rng.h>
52*4882a593Smuzhiyun #include <linux/fips.h>
53*4882a593Smuzhiyun #include <linux/mutex.h>
54*4882a593Smuzhiyun #include <linux/list.h>
55*4882a593Smuzhiyun #include <linux/workqueue.h>
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun /*
58*4882a593Smuzhiyun * Concatenation Helper and string operation helper
59*4882a593Smuzhiyun *
60*4882a593Smuzhiyun * SP800-90A requires the concatenation of different data. To avoid copying
61*4882a593Smuzhiyun * buffers around or allocate additional memory, the following data structure
62*4882a593Smuzhiyun * is used to point to the original memory with its size. In addition, it
63*4882a593Smuzhiyun * is used to build a linked list. The linked list defines the concatenation
64*4882a593Smuzhiyun * of individual buffers. The order of memory block referenced in that
65*4882a593Smuzhiyun * linked list determines the order of concatenation.
66*4882a593Smuzhiyun */
67*4882a593Smuzhiyun struct drbg_string {
68*4882a593Smuzhiyun const unsigned char *buf;
69*4882a593Smuzhiyun size_t len;
70*4882a593Smuzhiyun struct list_head list;
71*4882a593Smuzhiyun };
72*4882a593Smuzhiyun
drbg_string_fill(struct drbg_string * string,const unsigned char * buf,size_t len)73*4882a593Smuzhiyun static inline void drbg_string_fill(struct drbg_string *string,
74*4882a593Smuzhiyun const unsigned char *buf, size_t len)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun string->buf = buf;
77*4882a593Smuzhiyun string->len = len;
78*4882a593Smuzhiyun INIT_LIST_HEAD(&string->list);
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun struct drbg_state;
82*4882a593Smuzhiyun typedef uint32_t drbg_flag_t;
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun struct drbg_core {
85*4882a593Smuzhiyun drbg_flag_t flags; /* flags for the cipher */
86*4882a593Smuzhiyun __u8 statelen; /* maximum state length */
87*4882a593Smuzhiyun __u8 blocklen_bytes; /* block size of output in bytes */
88*4882a593Smuzhiyun char cra_name[CRYPTO_MAX_ALG_NAME]; /* mapping to kernel crypto API */
89*4882a593Smuzhiyun /* kernel crypto API backend cipher name */
90*4882a593Smuzhiyun char backend_cra_name[CRYPTO_MAX_ALG_NAME];
91*4882a593Smuzhiyun };
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun struct drbg_state_ops {
94*4882a593Smuzhiyun int (*update)(struct drbg_state *drbg, struct list_head *seed,
95*4882a593Smuzhiyun int reseed);
96*4882a593Smuzhiyun int (*generate)(struct drbg_state *drbg,
97*4882a593Smuzhiyun unsigned char *buf, unsigned int buflen,
98*4882a593Smuzhiyun struct list_head *addtl);
99*4882a593Smuzhiyun int (*crypto_init)(struct drbg_state *drbg);
100*4882a593Smuzhiyun int (*crypto_fini)(struct drbg_state *drbg);
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun };
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun struct drbg_test_data {
105*4882a593Smuzhiyun struct drbg_string *testentropy; /* TEST PARAMETER: test entropy */
106*4882a593Smuzhiyun };
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun enum drbg_seed_state {
109*4882a593Smuzhiyun DRBG_SEED_STATE_UNSEEDED,
110*4882a593Smuzhiyun DRBG_SEED_STATE_PARTIAL, /* Seeded with !rng_is_initialized() */
111*4882a593Smuzhiyun DRBG_SEED_STATE_FULL,
112*4882a593Smuzhiyun };
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun struct drbg_state {
115*4882a593Smuzhiyun struct mutex drbg_mutex; /* lock around DRBG */
116*4882a593Smuzhiyun unsigned char *V; /* internal state 10.1.1.1 1a) */
117*4882a593Smuzhiyun unsigned char *Vbuf;
118*4882a593Smuzhiyun /* hash: static value 10.1.1.1 1b) hmac / ctr: key */
119*4882a593Smuzhiyun unsigned char *C;
120*4882a593Smuzhiyun unsigned char *Cbuf;
121*4882a593Smuzhiyun /* Number of RNG requests since last reseed -- 10.1.1.1 1c) */
122*4882a593Smuzhiyun size_t reseed_ctr;
123*4882a593Smuzhiyun size_t reseed_threshold;
124*4882a593Smuzhiyun /* some memory the DRBG can use for its operation */
125*4882a593Smuzhiyun unsigned char *scratchpad;
126*4882a593Smuzhiyun unsigned char *scratchpadbuf;
127*4882a593Smuzhiyun void *priv_data; /* Cipher handle */
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun struct crypto_skcipher *ctr_handle; /* CTR mode cipher handle */
130*4882a593Smuzhiyun struct skcipher_request *ctr_req; /* CTR mode request handle */
131*4882a593Smuzhiyun __u8 *outscratchpadbuf; /* CTR mode output scratchpad */
132*4882a593Smuzhiyun __u8 *outscratchpad; /* CTR mode aligned outbuf */
133*4882a593Smuzhiyun struct crypto_wait ctr_wait; /* CTR mode async wait obj */
134*4882a593Smuzhiyun struct scatterlist sg_in, sg_out; /* CTR mode SGLs */
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun enum drbg_seed_state seeded; /* DRBG fully seeded? */
137*4882a593Smuzhiyun bool pr; /* Prediction resistance enabled? */
138*4882a593Smuzhiyun bool fips_primed; /* Continuous test primed? */
139*4882a593Smuzhiyun unsigned char *prev; /* FIPS 140-2 continuous test value */
140*4882a593Smuzhiyun struct crypto_rng *jent;
141*4882a593Smuzhiyun const struct drbg_state_ops *d_ops;
142*4882a593Smuzhiyun const struct drbg_core *core;
143*4882a593Smuzhiyun struct drbg_string test_data;
144*4882a593Smuzhiyun };
145*4882a593Smuzhiyun
drbg_statelen(struct drbg_state * drbg)146*4882a593Smuzhiyun static inline __u8 drbg_statelen(struct drbg_state *drbg)
147*4882a593Smuzhiyun {
148*4882a593Smuzhiyun if (drbg && drbg->core)
149*4882a593Smuzhiyun return drbg->core->statelen;
150*4882a593Smuzhiyun return 0;
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun
drbg_blocklen(struct drbg_state * drbg)153*4882a593Smuzhiyun static inline __u8 drbg_blocklen(struct drbg_state *drbg)
154*4882a593Smuzhiyun {
155*4882a593Smuzhiyun if (drbg && drbg->core)
156*4882a593Smuzhiyun return drbg->core->blocklen_bytes;
157*4882a593Smuzhiyun return 0;
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun
drbg_keylen(struct drbg_state * drbg)160*4882a593Smuzhiyun static inline __u8 drbg_keylen(struct drbg_state *drbg)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun if (drbg && drbg->core)
163*4882a593Smuzhiyun return (drbg->core->statelen - drbg->core->blocklen_bytes);
164*4882a593Smuzhiyun return 0;
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun
drbg_max_request_bytes(struct drbg_state * drbg)167*4882a593Smuzhiyun static inline size_t drbg_max_request_bytes(struct drbg_state *drbg)
168*4882a593Smuzhiyun {
169*4882a593Smuzhiyun /* SP800-90A requires the limit 2**19 bits, but we return bytes */
170*4882a593Smuzhiyun return (1 << 16);
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun
drbg_max_addtl(struct drbg_state * drbg)173*4882a593Smuzhiyun static inline size_t drbg_max_addtl(struct drbg_state *drbg)
174*4882a593Smuzhiyun {
175*4882a593Smuzhiyun /* SP800-90A requires 2**35 bytes additional info str / pers str */
176*4882a593Smuzhiyun #if (__BITS_PER_LONG == 32)
177*4882a593Smuzhiyun /*
178*4882a593Smuzhiyun * SP800-90A allows smaller maximum numbers to be returned -- we
179*4882a593Smuzhiyun * return SIZE_MAX - 1 to allow the verification of the enforcement
180*4882a593Smuzhiyun * of this value in drbg_healthcheck_sanity.
181*4882a593Smuzhiyun */
182*4882a593Smuzhiyun return (SIZE_MAX - 1);
183*4882a593Smuzhiyun #else
184*4882a593Smuzhiyun return (1UL<<35);
185*4882a593Smuzhiyun #endif
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun
drbg_max_requests(struct drbg_state * drbg)188*4882a593Smuzhiyun static inline size_t drbg_max_requests(struct drbg_state *drbg)
189*4882a593Smuzhiyun {
190*4882a593Smuzhiyun /* SP800-90A requires 2**48 maximum requests before reseeding */
191*4882a593Smuzhiyun return (1<<20);
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun /*
195*4882a593Smuzhiyun * This is a wrapper to the kernel crypto API function of
196*4882a593Smuzhiyun * crypto_rng_generate() to allow the caller to provide additional data.
197*4882a593Smuzhiyun *
198*4882a593Smuzhiyun * @drng DRBG handle -- see crypto_rng_get_bytes
199*4882a593Smuzhiyun * @outbuf output buffer -- see crypto_rng_get_bytes
200*4882a593Smuzhiyun * @outlen length of output buffer -- see crypto_rng_get_bytes
201*4882a593Smuzhiyun * @addtl_input additional information string input buffer
202*4882a593Smuzhiyun * @addtllen length of additional information string buffer
203*4882a593Smuzhiyun *
204*4882a593Smuzhiyun * return
205*4882a593Smuzhiyun * see crypto_rng_get_bytes
206*4882a593Smuzhiyun */
crypto_drbg_get_bytes_addtl(struct crypto_rng * drng,unsigned char * outbuf,unsigned int outlen,struct drbg_string * addtl)207*4882a593Smuzhiyun static inline int crypto_drbg_get_bytes_addtl(struct crypto_rng *drng,
208*4882a593Smuzhiyun unsigned char *outbuf, unsigned int outlen,
209*4882a593Smuzhiyun struct drbg_string *addtl)
210*4882a593Smuzhiyun {
211*4882a593Smuzhiyun return crypto_rng_generate(drng, addtl->buf, addtl->len,
212*4882a593Smuzhiyun outbuf, outlen);
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun /*
216*4882a593Smuzhiyun * TEST code
217*4882a593Smuzhiyun *
218*4882a593Smuzhiyun * This is a wrapper to the kernel crypto API function of
219*4882a593Smuzhiyun * crypto_rng_generate() to allow the caller to provide additional data and
220*4882a593Smuzhiyun * allow furnishing of test_data
221*4882a593Smuzhiyun *
222*4882a593Smuzhiyun * @drng DRBG handle -- see crypto_rng_get_bytes
223*4882a593Smuzhiyun * @outbuf output buffer -- see crypto_rng_get_bytes
224*4882a593Smuzhiyun * @outlen length of output buffer -- see crypto_rng_get_bytes
225*4882a593Smuzhiyun * @addtl_input additional information string input buffer
226*4882a593Smuzhiyun * @addtllen length of additional information string buffer
227*4882a593Smuzhiyun * @test_data filled test data
228*4882a593Smuzhiyun *
229*4882a593Smuzhiyun * return
230*4882a593Smuzhiyun * see crypto_rng_get_bytes
231*4882a593Smuzhiyun */
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)232*4882a593Smuzhiyun static inline int crypto_drbg_get_bytes_addtl_test(struct crypto_rng *drng,
233*4882a593Smuzhiyun unsigned char *outbuf, unsigned int outlen,
234*4882a593Smuzhiyun struct drbg_string *addtl,
235*4882a593Smuzhiyun struct drbg_test_data *test_data)
236*4882a593Smuzhiyun {
237*4882a593Smuzhiyun crypto_rng_set_entropy(drng, test_data->testentropy->buf,
238*4882a593Smuzhiyun test_data->testentropy->len);
239*4882a593Smuzhiyun return crypto_rng_generate(drng, addtl->buf, addtl->len,
240*4882a593Smuzhiyun outbuf, outlen);
241*4882a593Smuzhiyun }
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun /*
244*4882a593Smuzhiyun * TEST code
245*4882a593Smuzhiyun *
246*4882a593Smuzhiyun * This is a wrapper to the kernel crypto API function of
247*4882a593Smuzhiyun * crypto_rng_reset() to allow the caller to provide test_data
248*4882a593Smuzhiyun *
249*4882a593Smuzhiyun * @drng DRBG handle -- see crypto_rng_reset
250*4882a593Smuzhiyun * @pers personalization string input buffer
251*4882a593Smuzhiyun * @perslen length of additional information string buffer
252*4882a593Smuzhiyun * @test_data filled test data
253*4882a593Smuzhiyun *
254*4882a593Smuzhiyun * return
255*4882a593Smuzhiyun * see crypto_rng_reset
256*4882a593Smuzhiyun */
crypto_drbg_reset_test(struct crypto_rng * drng,struct drbg_string * pers,struct drbg_test_data * test_data)257*4882a593Smuzhiyun static inline int crypto_drbg_reset_test(struct crypto_rng *drng,
258*4882a593Smuzhiyun struct drbg_string *pers,
259*4882a593Smuzhiyun struct drbg_test_data *test_data)
260*4882a593Smuzhiyun {
261*4882a593Smuzhiyun crypto_rng_set_entropy(drng, test_data->testentropy->buf,
262*4882a593Smuzhiyun test_data->testentropy->len);
263*4882a593Smuzhiyun return crypto_rng_reset(drng, pers->buf, pers->len);
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun /* DRBG type flags */
267*4882a593Smuzhiyun #define DRBG_CTR ((drbg_flag_t)1<<0)
268*4882a593Smuzhiyun #define DRBG_HMAC ((drbg_flag_t)1<<1)
269*4882a593Smuzhiyun #define DRBG_HASH ((drbg_flag_t)1<<2)
270*4882a593Smuzhiyun #define DRBG_TYPE_MASK (DRBG_CTR | DRBG_HMAC | DRBG_HASH)
271*4882a593Smuzhiyun /* DRBG strength flags */
272*4882a593Smuzhiyun #define DRBG_STRENGTH128 ((drbg_flag_t)1<<3)
273*4882a593Smuzhiyun #define DRBG_STRENGTH192 ((drbg_flag_t)1<<4)
274*4882a593Smuzhiyun #define DRBG_STRENGTH256 ((drbg_flag_t)1<<5)
275*4882a593Smuzhiyun #define DRBG_STRENGTH_MASK (DRBG_STRENGTH128 | DRBG_STRENGTH192 | \
276*4882a593Smuzhiyun DRBG_STRENGTH256)
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun enum drbg_prefixes {
279*4882a593Smuzhiyun DRBG_PREFIX0 = 0x00,
280*4882a593Smuzhiyun DRBG_PREFIX1,
281*4882a593Smuzhiyun DRBG_PREFIX2,
282*4882a593Smuzhiyun DRBG_PREFIX3
283*4882a593Smuzhiyun };
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun #endif /* _DRBG_H */
286