blob: 9756c70899d8195eb54f7349efb911e8604d6222 [file] [log] [blame]
Stephan Mueller3e16f952014-05-31 17:21:48 +02001/*
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 <linux/module.h>
47#include <linux/crypto.h>
48#include <linux/slab.h>
49#include <crypto/internal/rng.h>
50#include <crypto/rng.h>
51#include <linux/fips.h>
Stephan Mueller76899a42015-04-18 19:36:17 +020052#include <linux/mutex.h>
Stephan Mueller8c987162014-06-28 21:58:24 +020053#include <linux/list.h>
Stephan Mueller4c787992015-05-25 15:09:36 +020054#include <linux/workqueue.h>
Stephan Mueller3e16f952014-05-31 17:21:48 +020055
56/*
57 * Concatenation Helper and string operation helper
58 *
59 * SP800-90A requires the concatenation of different data. To avoid copying
60 * buffers around or allocate additional memory, the following data structure
61 * is used to point to the original memory with its size. In addition, it
62 * is used to build a linked list. The linked list defines the concatenation
63 * of individual buffers. The order of memory block referenced in that
64 * linked list determines the order of concatenation.
65 */
66struct drbg_string {
67 const unsigned char *buf;
68 size_t len;
Stephan Mueller8c987162014-06-28 21:58:24 +020069 struct list_head list;
Stephan Mueller3e16f952014-05-31 17:21:48 +020070};
71
72static inline void drbg_string_fill(struct drbg_string *string,
73 const unsigned char *buf, size_t len)
74{
75 string->buf = buf;
76 string->len = len;
Stephan Mueller8c987162014-06-28 21:58:24 +020077 INIT_LIST_HEAD(&string->list);
Stephan Mueller3e16f952014-05-31 17:21:48 +020078}
79
80struct drbg_state;
81typedef uint32_t drbg_flag_t;
82
83struct drbg_core {
84 drbg_flag_t flags; /* flags for the cipher */
85 __u8 statelen; /* maximum state length */
Stephan Mueller3e16f952014-05-31 17:21:48 +020086 __u8 blocklen_bytes; /* block size of output in bytes */
87 char cra_name[CRYPTO_MAX_ALG_NAME]; /* mapping to kernel crypto API */
88 /* kernel crypto API backend cipher name */
89 char backend_cra_name[CRYPTO_MAX_ALG_NAME];
90};
91
92struct drbg_state_ops {
Stephan Mueller8c987162014-06-28 21:58:24 +020093 int (*update)(struct drbg_state *drbg, struct list_head *seed,
Stephan Mueller3e16f952014-05-31 17:21:48 +020094 int reseed);
95 int (*generate)(struct drbg_state *drbg,
96 unsigned char *buf, unsigned int buflen,
Stephan Mueller27e4de22014-07-06 02:25:36 +020097 struct list_head *addtl);
Stephan Mueller3e16f952014-05-31 17:21:48 +020098 int (*crypto_init)(struct drbg_state *drbg);
99 int (*crypto_fini)(struct drbg_state *drbg);
100
101};
102
103struct drbg_test_data {
104 struct drbg_string *testentropy; /* TEST PARAMETER: test entropy */
105};
106
107struct drbg_state {
Stephan Mueller76899a42015-04-18 19:36:17 +0200108 struct mutex drbg_mutex; /* lock around DRBG */
Stephan Mueller3e16f952014-05-31 17:21:48 +0200109 unsigned char *V; /* internal state 10.1.1.1 1a) */
110 /* hash: static value 10.1.1.1 1b) hmac / ctr: key */
111 unsigned char *C;
112 /* Number of RNG requests since last reseed -- 10.1.1.1 1c) */
113 size_t reseed_ctr;
Stephan Mueller42ea5072015-06-10 03:33:37 +0200114 size_t reseed_threshold;
Stephan Mueller3e16f952014-05-31 17:21:48 +0200115 /* some memory the DRBG can use for its operation */
116 unsigned char *scratchpad;
117 void *priv_data; /* Cipher handle */
118 bool seeded; /* DRBG fully seeded? */
119 bool pr; /* Prediction resistance enabled? */
120#ifdef CONFIG_CRYPTO_FIPS
121 bool fips_primed; /* Continuous test primed? */
122 unsigned char *prev; /* FIPS 140-2 continuous test value */
123#endif
Stephan Mueller4c787992015-05-25 15:09:36 +0200124 struct work_struct seed_work; /* asynchronous seeding support */
Stephan Muellerb8ec5ba2015-05-25 15:09:59 +0200125 struct crypto_rng *jent;
Stephan Mueller3e16f952014-05-31 17:21:48 +0200126 const struct drbg_state_ops *d_ops;
127 const struct drbg_core *core;
Herbert Xu8fded592015-04-21 10:46:41 +0800128 struct drbg_string test_data;
Stephan Mueller57225e62015-06-09 21:55:38 +0800129 struct random_ready_callback random_ready;
Stephan Mueller3e16f952014-05-31 17:21:48 +0200130};
131
132static inline __u8 drbg_statelen(struct drbg_state *drbg)
133{
134 if (drbg && drbg->core)
135 return drbg->core->statelen;
136 return 0;
137}
138
139static inline __u8 drbg_blocklen(struct drbg_state *drbg)
140{
141 if (drbg && drbg->core)
142 return drbg->core->blocklen_bytes;
143 return 0;
144}
145
146static inline __u8 drbg_keylen(struct drbg_state *drbg)
147{
148 if (drbg && drbg->core)
149 return (drbg->core->statelen - drbg->core->blocklen_bytes);
150 return 0;
151}
152
153static inline size_t drbg_max_request_bytes(struct drbg_state *drbg)
154{
Stephan Mueller05c81cc2014-08-17 17:41:10 +0200155 /* SP800-90A requires the limit 2**19 bits, but we return bytes */
156 return (1 << 16);
Stephan Mueller3e16f952014-05-31 17:21:48 +0200157}
158
159static inline size_t drbg_max_addtl(struct drbg_state *drbg)
160{
Stephan Mueller05c81cc2014-08-17 17:41:10 +0200161 /* SP800-90A requires 2**35 bytes additional info str / pers str */
Stephan Muellerb9347af2014-08-26 10:29:45 +0200162#if (__BITS_PER_LONG == 32)
163 /*
164 * SP800-90A allows smaller maximum numbers to be returned -- we
165 * return SIZE_MAX - 1 to allow the verification of the enforcement
166 * of this value in drbg_healthcheck_sanity.
167 */
168 return (SIZE_MAX - 1);
169#else
Stephan Mueller05c81cc2014-08-17 17:41:10 +0200170 return (1UL<<35);
Stephan Muellerb9347af2014-08-26 10:29:45 +0200171#endif
Stephan Mueller3e16f952014-05-31 17:21:48 +0200172}
173
174static inline size_t drbg_max_requests(struct drbg_state *drbg)
175{
Stephan Mueller05c81cc2014-08-17 17:41:10 +0200176 /* SP800-90A requires 2**48 maximum requests before reseeding */
Stephan Muellerb9347af2014-08-26 10:29:45 +0200177#if (__BITS_PER_LONG == 32)
178 return SIZE_MAX;
179#else
Stephan Mueller05c81cc2014-08-17 17:41:10 +0200180 return (1UL<<48);
Stephan Muellerb9347af2014-08-26 10:29:45 +0200181#endif
Stephan Mueller3e16f952014-05-31 17:21:48 +0200182}
183
184/*
Stephan Mueller3e16f952014-05-31 17:21:48 +0200185 * This is a wrapper to the kernel crypto API function of
Herbert Xu8fded592015-04-21 10:46:41 +0800186 * crypto_rng_generate() to allow the caller to provide additional data.
Stephan Mueller3e16f952014-05-31 17:21:48 +0200187 *
188 * @drng DRBG handle -- see crypto_rng_get_bytes
189 * @outbuf output buffer -- see crypto_rng_get_bytes
190 * @outlen length of output buffer -- see crypto_rng_get_bytes
191 * @addtl_input additional information string input buffer
192 * @addtllen length of additional information string buffer
193 *
194 * return
195 * see crypto_rng_get_bytes
196 */
197static inline int crypto_drbg_get_bytes_addtl(struct crypto_rng *drng,
198 unsigned char *outbuf, unsigned int outlen,
199 struct drbg_string *addtl)
200{
Herbert Xu8fded592015-04-21 10:46:41 +0800201 return crypto_rng_generate(drng, addtl->buf, addtl->len,
202 outbuf, outlen);
Stephan Mueller3e16f952014-05-31 17:21:48 +0200203}
204
205/*
206 * TEST code
207 *
208 * This is a wrapper to the kernel crypto API function of
Herbert Xu8fded592015-04-21 10:46:41 +0800209 * crypto_rng_generate() to allow the caller to provide additional data and
Stephan Mueller3e16f952014-05-31 17:21:48 +0200210 * allow furnishing of test_data
211 *
212 * @drng DRBG handle -- see crypto_rng_get_bytes
213 * @outbuf output buffer -- see crypto_rng_get_bytes
214 * @outlen length of output buffer -- see crypto_rng_get_bytes
215 * @addtl_input additional information string input buffer
216 * @addtllen length of additional information string buffer
217 * @test_data filled test data
218 *
219 * return
220 * see crypto_rng_get_bytes
221 */
222static inline int crypto_drbg_get_bytes_addtl_test(struct crypto_rng *drng,
223 unsigned char *outbuf, unsigned int outlen,
224 struct drbg_string *addtl,
225 struct drbg_test_data *test_data)
226{
Herbert Xu8fded592015-04-21 10:46:41 +0800227 crypto_rng_set_entropy(drng, test_data->testentropy->buf,
228 test_data->testentropy->len);
229 return crypto_rng_generate(drng, addtl->buf, addtl->len,
230 outbuf, outlen);
Stephan Mueller3e16f952014-05-31 17:21:48 +0200231}
232
233/*
234 * TEST code
235 *
236 * This is a wrapper to the kernel crypto API function of
237 * crypto_rng_reset() to allow the caller to provide test_data
238 *
239 * @drng DRBG handle -- see crypto_rng_reset
240 * @pers personalization string input buffer
241 * @perslen length of additional information string buffer
242 * @test_data filled test data
243 *
244 * return
245 * see crypto_rng_reset
246 */
247static inline int crypto_drbg_reset_test(struct crypto_rng *drng,
248 struct drbg_string *pers,
249 struct drbg_test_data *test_data)
250{
Herbert Xu8fded592015-04-21 10:46:41 +0800251 crypto_rng_set_entropy(drng, test_data->testentropy->buf,
252 test_data->testentropy->len);
253 return crypto_rng_reset(drng, pers->buf, pers->len);
Stephan Mueller3e16f952014-05-31 17:21:48 +0200254}
255
256/* DRBG type flags */
257#define DRBG_CTR ((drbg_flag_t)1<<0)
258#define DRBG_HMAC ((drbg_flag_t)1<<1)
259#define DRBG_HASH ((drbg_flag_t)1<<2)
260#define DRBG_TYPE_MASK (DRBG_CTR | DRBG_HMAC | DRBG_HASH)
261/* DRBG strength flags */
262#define DRBG_STRENGTH128 ((drbg_flag_t)1<<3)
263#define DRBG_STRENGTH192 ((drbg_flag_t)1<<4)
264#define DRBG_STRENGTH256 ((drbg_flag_t)1<<5)
265#define DRBG_STRENGTH_MASK (DRBG_STRENGTH128 | DRBG_STRENGTH192 | \
266 DRBG_STRENGTH256)
267
268enum drbg_prefixes {
269 DRBG_PREFIX0 = 0x00,
270 DRBG_PREFIX1,
271 DRBG_PREFIX2,
272 DRBG_PREFIX3
273};
274
275#endif /* _DRBG_H */