blob: af5ad51d3eef81212a55fb7e41a668b62435e142 [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>
Stephan Mueller35591282016-06-14 07:34:13 +020046#include <crypto/skcipher.h>
Stephan Mueller3e16f952014-05-31 17:21:48 +020047#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>
Stephan Mueller76899a42015-04-18 19:36:17 +020053#include <linux/mutex.h>
Stephan Mueller8c987162014-06-28 21:58:24 +020054#include <linux/list.h>
Stephan Mueller4c787992015-05-25 15:09:36 +020055#include <linux/workqueue.h>
Stephan Mueller3e16f952014-05-31 17:21:48 +020056
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 */
67struct drbg_string {
68 const unsigned char *buf;
69 size_t len;
Stephan Mueller8c987162014-06-28 21:58:24 +020070 struct list_head list;
Stephan Mueller3e16f952014-05-31 17:21:48 +020071};
72
73static 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;
Stephan Mueller8c987162014-06-28 21:58:24 +020078 INIT_LIST_HEAD(&string->list);
Stephan Mueller3e16f952014-05-31 17:21:48 +020079}
80
81struct drbg_state;
82typedef uint32_t drbg_flag_t;
83
84struct drbg_core {
85 drbg_flag_t flags; /* flags for the cipher */
86 __u8 statelen; /* maximum state length */
Stephan Mueller3e16f952014-05-31 17:21:48 +020087 __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
93struct drbg_state_ops {
Stephan Mueller8c987162014-06-28 21:58:24 +020094 int (*update)(struct drbg_state *drbg, struct list_head *seed,
Stephan Mueller3e16f952014-05-31 17:21:48 +020095 int reseed);
96 int (*generate)(struct drbg_state *drbg,
97 unsigned char *buf, unsigned int buflen,
Stephan Mueller27e4de22014-07-06 02:25:36 +020098 struct list_head *addtl);
Stephan Mueller3e16f952014-05-31 17:21:48 +020099 int (*crypto_init)(struct drbg_state *drbg);
100 int (*crypto_fini)(struct drbg_state *drbg);
101
102};
103
104struct drbg_test_data {
105 struct drbg_string *testentropy; /* TEST PARAMETER: test entropy */
106};
107
Nicolai Stangece8ce312021-11-15 15:18:04 +0100108enum drbg_seed_state {
109 DRBG_SEED_STATE_UNSEEDED,
Nicolai Stange2bcd2542021-11-15 15:18:05 +0100110 DRBG_SEED_STATE_PARTIAL, /* Seeded with !rng_is_initialized() */
Nicolai Stangece8ce312021-11-15 15:18:04 +0100111 DRBG_SEED_STATE_FULL,
112};
113
Stephan Mueller3e16f952014-05-31 17:21:48 +0200114struct drbg_state {
Stephan Mueller76899a42015-04-18 19:36:17 +0200115 struct mutex drbg_mutex; /* lock around DRBG */
Stephan Mueller3e16f952014-05-31 17:21:48 +0200116 unsigned char *V; /* internal state 10.1.1.1 1a) */
Stephan Mueller3cfc3b92016-06-14 07:35:13 +0200117 unsigned char *Vbuf;
Stephan Mueller3e16f952014-05-31 17:21:48 +0200118 /* hash: static value 10.1.1.1 1b) hmac / ctr: key */
119 unsigned char *C;
Stephan Mueller3cfc3b92016-06-14 07:35:13 +0200120 unsigned char *Cbuf;
Stephan Mueller3e16f952014-05-31 17:21:48 +0200121 /* Number of RNG requests since last reseed -- 10.1.1.1 1c) */
122 size_t reseed_ctr;
Stephan Mueller42ea5072015-06-10 03:33:37 +0200123 size_t reseed_threshold;
Stephan Mueller3e16f952014-05-31 17:21:48 +0200124 /* some memory the DRBG can use for its operation */
125 unsigned char *scratchpad;
Stephan Mueller3cfc3b92016-06-14 07:35:13 +0200126 unsigned char *scratchpadbuf;
Stephan Mueller3e16f952014-05-31 17:21:48 +0200127 void *priv_data; /* Cipher handle */
Stephan Mueller35591282016-06-14 07:34:13 +0200128
129 struct crypto_skcipher *ctr_handle; /* CTR mode cipher handle */
130 struct skcipher_request *ctr_req; /* CTR mode request handle */
Stephan Mueller51029812016-11-29 09:45:04 +0100131 __u8 *outscratchpadbuf; /* CTR mode output scratchpad */
132 __u8 *outscratchpad; /* CTR mode aligned outbuf */
Gilad Ben-Yossef85a2dea2017-10-18 08:00:41 +0100133 struct crypto_wait ctr_wait; /* CTR mode async wait obj */
Stephan Muellercf862cb2018-07-10 17:56:33 +0200134 struct scatterlist sg_in, sg_out; /* CTR mode SGLs */
Stephan Mueller35591282016-06-14 07:34:13 +0200135
Nicolai Stangece8ce312021-11-15 15:18:04 +0100136 enum drbg_seed_state seeded; /* DRBG fully seeded? */
Nicolai Stange8ea5ee02021-11-15 15:18:09 +0100137 unsigned long last_seed_time;
Stephan Mueller3e16f952014-05-31 17:21:48 +0200138 bool pr; /* Prediction resistance enabled? */
Stephan Muellerdb07cd22019-05-08 16:19:24 +0200139 bool fips_primed; /* Continuous test primed? */
140 unsigned char *prev; /* FIPS 140-2 continuous test value */
Stephan Muellerb8ec5ba2015-05-25 15:09:59 +0200141 struct crypto_rng *jent;
Stephan Mueller3e16f952014-05-31 17:21:48 +0200142 const struct drbg_state_ops *d_ops;
143 const struct drbg_core *core;
Herbert Xu8fded592015-04-21 10:46:41 +0800144 struct drbg_string test_data;
Stephan Mueller3e16f952014-05-31 17:21:48 +0200145};
146
147static inline __u8 drbg_statelen(struct drbg_state *drbg)
148{
149 if (drbg && drbg->core)
150 return drbg->core->statelen;
151 return 0;
152}
153
154static inline __u8 drbg_blocklen(struct drbg_state *drbg)
155{
156 if (drbg && drbg->core)
157 return drbg->core->blocklen_bytes;
158 return 0;
159}
160
161static inline __u8 drbg_keylen(struct drbg_state *drbg)
162{
163 if (drbg && drbg->core)
164 return (drbg->core->statelen - drbg->core->blocklen_bytes);
165 return 0;
166}
167
168static inline size_t drbg_max_request_bytes(struct drbg_state *drbg)
169{
Stephan Mueller05c81cc2014-08-17 17:41:10 +0200170 /* SP800-90A requires the limit 2**19 bits, but we return bytes */
171 return (1 << 16);
Stephan Mueller3e16f952014-05-31 17:21:48 +0200172}
173
174static inline size_t drbg_max_addtl(struct drbg_state *drbg)
175{
Stephan Mueller05c81cc2014-08-17 17:41:10 +0200176 /* SP800-90A requires 2**35 bytes additional info str / pers str */
Stephan Muellerb9347af2014-08-26 10:29:45 +0200177#if (__BITS_PER_LONG == 32)
178 /*
179 * SP800-90A allows smaller maximum numbers to be returned -- we
180 * return SIZE_MAX - 1 to allow the verification of the enforcement
181 * of this value in drbg_healthcheck_sanity.
182 */
183 return (SIZE_MAX - 1);
184#else
Stephan Mueller05c81cc2014-08-17 17:41:10 +0200185 return (1UL<<35);
Stephan Muellerb9347af2014-08-26 10:29:45 +0200186#endif
Stephan Mueller3e16f952014-05-31 17:21:48 +0200187}
188
189static inline size_t drbg_max_requests(struct drbg_state *drbg)
190{
Stephan Mueller05c81cc2014-08-17 17:41:10 +0200191 /* SP800-90A requires 2**48 maximum requests before reseeding */
Stephan Müller97f26502020-04-17 21:34:03 +0200192 return (1<<20);
Stephan Mueller3e16f952014-05-31 17:21:48 +0200193}
194
195/*
Stephan Mueller3e16f952014-05-31 17:21:48 +0200196 * This is a wrapper to the kernel crypto API function of
Herbert Xu8fded592015-04-21 10:46:41 +0800197 * crypto_rng_generate() to allow the caller to provide additional data.
Stephan Mueller3e16f952014-05-31 17:21:48 +0200198 *
199 * @drng DRBG handle -- see crypto_rng_get_bytes
200 * @outbuf output buffer -- see crypto_rng_get_bytes
201 * @outlen length of output buffer -- see crypto_rng_get_bytes
202 * @addtl_input additional information string input buffer
203 * @addtllen length of additional information string buffer
204 *
205 * return
206 * see crypto_rng_get_bytes
207 */
208static inline int crypto_drbg_get_bytes_addtl(struct crypto_rng *drng,
209 unsigned char *outbuf, unsigned int outlen,
210 struct drbg_string *addtl)
211{
Herbert Xu8fded592015-04-21 10:46:41 +0800212 return crypto_rng_generate(drng, addtl->buf, addtl->len,
213 outbuf, outlen);
Stephan Mueller3e16f952014-05-31 17:21:48 +0200214}
215
216/*
217 * TEST code
218 *
219 * This is a wrapper to the kernel crypto API function of
Herbert Xu8fded592015-04-21 10:46:41 +0800220 * crypto_rng_generate() to allow the caller to provide additional data and
Stephan Mueller3e16f952014-05-31 17:21:48 +0200221 * allow furnishing of test_data
222 *
223 * @drng DRBG handle -- see crypto_rng_get_bytes
224 * @outbuf output buffer -- see crypto_rng_get_bytes
225 * @outlen length of output buffer -- see crypto_rng_get_bytes
226 * @addtl_input additional information string input buffer
227 * @addtllen length of additional information string buffer
228 * @test_data filled test data
229 *
230 * return
231 * see crypto_rng_get_bytes
232 */
233static inline int crypto_drbg_get_bytes_addtl_test(struct crypto_rng *drng,
234 unsigned char *outbuf, unsigned int outlen,
235 struct drbg_string *addtl,
236 struct drbg_test_data *test_data)
237{
Herbert Xu8fded592015-04-21 10:46:41 +0800238 crypto_rng_set_entropy(drng, test_data->testentropy->buf,
239 test_data->testentropy->len);
240 return crypto_rng_generate(drng, addtl->buf, addtl->len,
241 outbuf, outlen);
Stephan Mueller3e16f952014-05-31 17:21:48 +0200242}
243
244/*
245 * TEST code
246 *
247 * This is a wrapper to the kernel crypto API function of
248 * crypto_rng_reset() to allow the caller to provide test_data
249 *
250 * @drng DRBG handle -- see crypto_rng_reset
251 * @pers personalization string input buffer
252 * @perslen length of additional information string buffer
253 * @test_data filled test data
254 *
255 * return
256 * see crypto_rng_reset
257 */
258static inline int crypto_drbg_reset_test(struct crypto_rng *drng,
259 struct drbg_string *pers,
260 struct drbg_test_data *test_data)
261{
Herbert Xu8fded592015-04-21 10:46:41 +0800262 crypto_rng_set_entropy(drng, test_data->testentropy->buf,
263 test_data->testentropy->len);
264 return crypto_rng_reset(drng, pers->buf, pers->len);
Stephan Mueller3e16f952014-05-31 17:21:48 +0200265}
266
267/* DRBG type flags */
268#define DRBG_CTR ((drbg_flag_t)1<<0)
269#define DRBG_HMAC ((drbg_flag_t)1<<1)
270#define DRBG_HASH ((drbg_flag_t)1<<2)
271#define DRBG_TYPE_MASK (DRBG_CTR | DRBG_HMAC | DRBG_HASH)
272/* DRBG strength flags */
273#define DRBG_STRENGTH128 ((drbg_flag_t)1<<3)
274#define DRBG_STRENGTH192 ((drbg_flag_t)1<<4)
275#define DRBG_STRENGTH256 ((drbg_flag_t)1<<5)
276#define DRBG_STRENGTH_MASK (DRBG_STRENGTH128 | DRBG_STRENGTH192 | \
277 DRBG_STRENGTH256)
278
279enum drbg_prefixes {
280 DRBG_PREFIX0 = 0x00,
281 DRBG_PREFIX1,
282 DRBG_PREFIX2,
283 DRBG_PREFIX3
284};
285
286#endif /* _DRBG_H */