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