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