Linux Audio

Check our new training course

Loading...
v5.4
  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 */
 67struct drbg_string {
 68	const unsigned char *buf;
 69	size_t len;
 70	struct list_head list;
 71};
 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;
 78	INIT_LIST_HEAD(&string->list);
 79}
 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 */
 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
 93struct 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
104struct drbg_test_data {
105	struct drbg_string *testentropy; /* TEST PARAMETER: test entropy */
106};
107
108struct drbg_state {
109	struct mutex drbg_mutex;	/* lock around DRBG */
110	unsigned char *V;	/* internal state 10.1.1.1 1a) */
111	unsigned char *Vbuf;
112	/* hash: static value 10.1.1.1 1b) hmac / ctr: key */
113	unsigned char *C;
114	unsigned char *Cbuf;
115	/* Number of RNG requests since last reseed -- 10.1.1.1 1c) */
116	size_t reseed_ctr;
117	size_t reseed_threshold;
118	 /* some memory the DRBG can use for its operation */
119	unsigned char *scratchpad;
120	unsigned char *scratchpadbuf;
121	void *priv_data;	/* Cipher handle */
122
123	struct crypto_skcipher *ctr_handle;	/* CTR mode cipher handle */
124	struct skcipher_request *ctr_req;	/* CTR mode request handle */
125	__u8 *outscratchpadbuf;			/* CTR mode output scratchpad */
126        __u8 *outscratchpad;			/* CTR mode aligned outbuf */
127	struct crypto_wait ctr_wait;		/* CTR mode async wait obj */
128	struct scatterlist sg_in, sg_out;	/* CTR mode SGLs */
129
130	bool seeded;		/* DRBG fully seeded? */
131	bool pr;		/* Prediction resistance enabled? */
132	bool fips_primed;	/* Continuous test primed? */
133	unsigned char *prev;	/* FIPS 140-2 continuous test value */
134	struct work_struct seed_work;	/* asynchronous seeding support */
135	struct crypto_rng *jent;
136	const struct drbg_state_ops *d_ops;
137	const struct drbg_core *core;
138	struct drbg_string test_data;
139	struct random_ready_callback random_ready;
140};
141
142static inline __u8 drbg_statelen(struct drbg_state *drbg)
143{
144	if (drbg && drbg->core)
145		return drbg->core->statelen;
146	return 0;
147}
148
149static inline __u8 drbg_blocklen(struct drbg_state *drbg)
150{
151	if (drbg && drbg->core)
152		return drbg->core->blocklen_bytes;
153	return 0;
154}
155
156static inline __u8 drbg_keylen(struct drbg_state *drbg)
157{
158	if (drbg && drbg->core)
159		return (drbg->core->statelen - drbg->core->blocklen_bytes);
160	return 0;
161}
162
163static inline size_t drbg_max_request_bytes(struct drbg_state *drbg)
164{
165	/* SP800-90A requires the limit 2**19 bits, but we return bytes */
166	return (1 << 16);
167}
168
169static inline size_t drbg_max_addtl(struct drbg_state *drbg)
170{
171	/* SP800-90A requires 2**35 bytes additional info str / pers str */
172#if (__BITS_PER_LONG == 32)
173	/*
174	 * SP800-90A allows smaller maximum numbers to be returned -- we
175	 * return SIZE_MAX - 1 to allow the verification of the enforcement
176	 * of this value in drbg_healthcheck_sanity.
177	 */
178	return (SIZE_MAX - 1);
179#else
180	return (1UL<<35);
181#endif
182}
183
184static inline size_t drbg_max_requests(struct drbg_state *drbg)
185{
186	/* SP800-90A requires 2**48 maximum requests before reseeding */
187#if (__BITS_PER_LONG == 32)
188	return SIZE_MAX;
189#else
190	return (1UL<<48);
191#endif
192}
193
194/*
195 * This is a wrapper to the kernel crypto API function of
196 * crypto_rng_generate() to allow the caller to provide additional data.
197 *
198 * @drng DRBG handle -- see crypto_rng_get_bytes
199 * @outbuf output buffer -- see crypto_rng_get_bytes
200 * @outlen length of output buffer -- see crypto_rng_get_bytes
201 * @addtl_input additional information string input buffer
202 * @addtllen length of additional information string buffer
203 *
204 * return
205 *	see crypto_rng_get_bytes
206 */
207static inline int crypto_drbg_get_bytes_addtl(struct crypto_rng *drng,
208			unsigned char *outbuf, unsigned int outlen,
209			struct drbg_string *addtl)
210{
211	return crypto_rng_generate(drng, addtl->buf, addtl->len,
212				   outbuf, outlen);
213}
214
215/*
216 * TEST code
217 *
218 * This is a wrapper to the kernel crypto API function of
219 * crypto_rng_generate() to allow the caller to provide additional data and
220 * allow furnishing of test_data
221 *
222 * @drng DRBG handle -- see crypto_rng_get_bytes
223 * @outbuf output buffer -- see crypto_rng_get_bytes
224 * @outlen length of output buffer -- see crypto_rng_get_bytes
225 * @addtl_input additional information string input buffer
226 * @addtllen length of additional information string buffer
227 * @test_data filled test data
228 *
229 * return
230 *	see crypto_rng_get_bytes
231 */
232static inline int crypto_drbg_get_bytes_addtl_test(struct crypto_rng *drng,
233			unsigned char *outbuf, unsigned int outlen,
234			struct drbg_string *addtl,
235			struct drbg_test_data *test_data)
236{
237	crypto_rng_set_entropy(drng, test_data->testentropy->buf,
238			       test_data->testentropy->len);
239	return crypto_rng_generate(drng, addtl->buf, addtl->len,
240				   outbuf, outlen);
241}
242
243/*
244 * TEST code
245 *
246 * This is a wrapper to the kernel crypto API function of
247 * crypto_rng_reset() to allow the caller to provide test_data
248 *
249 * @drng DRBG handle -- see crypto_rng_reset
250 * @pers personalization string input buffer
251 * @perslen length of additional information string buffer
252 * @test_data filled test data
253 *
254 * return
255 *	see crypto_rng_reset
256 */
257static inline int crypto_drbg_reset_test(struct crypto_rng *drng,
258					 struct drbg_string *pers,
259					 struct drbg_test_data *test_data)
260{
261	crypto_rng_set_entropy(drng, test_data->testentropy->buf,
262			       test_data->testentropy->len);
263	return crypto_rng_reset(drng, pers->buf, pers->len);
264}
265
266/* DRBG type flags */
267#define DRBG_CTR	((drbg_flag_t)1<<0)
268#define DRBG_HMAC	((drbg_flag_t)1<<1)
269#define DRBG_HASH	((drbg_flag_t)1<<2)
270#define DRBG_TYPE_MASK	(DRBG_CTR | DRBG_HMAC | DRBG_HASH)
271/* DRBG strength flags */
272#define DRBG_STRENGTH128	((drbg_flag_t)1<<3)
273#define DRBG_STRENGTH192	((drbg_flag_t)1<<4)
274#define DRBG_STRENGTH256	((drbg_flag_t)1<<5)
275#define DRBG_STRENGTH_MASK	(DRBG_STRENGTH128 | DRBG_STRENGTH192 | \
276				 DRBG_STRENGTH256)
277
278enum drbg_prefixes {
279	DRBG_PREFIX0 = 0x00,
280	DRBG_PREFIX1,
281	DRBG_PREFIX2,
282	DRBG_PREFIX3
283};
284
285#endif /* _DRBG_H */
v4.6
  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>
 52#include <linux/mutex.h>
 53#include <linux/list.h>
 54#include <linux/workqueue.h>
 55
 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;
 69	struct list_head list;
 70};
 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;
 77	INIT_LIST_HEAD(&string->list);
 78}
 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 */
 86	__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 {
 93	int (*update)(struct drbg_state *drbg, struct list_head *seed,
 94		      int reseed);
 95	int (*generate)(struct drbg_state *drbg,
 96			unsigned char *buf, unsigned int buflen,
 97			struct list_head *addtl);
 98	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 {
108	struct mutex drbg_mutex;	/* lock around DRBG */
109	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;
114	size_t reseed_threshold;
115	 /* 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	struct work_struct seed_work;	/* asynchronous seeding support */
121	struct crypto_rng *jent;
122	const struct drbg_state_ops *d_ops;
123	const struct drbg_core *core;
124	struct drbg_string test_data;
125	struct random_ready_callback random_ready;
126};
127
128static inline __u8 drbg_statelen(struct drbg_state *drbg)
129{
130	if (drbg && drbg->core)
131		return drbg->core->statelen;
132	return 0;
133}
134
135static inline __u8 drbg_blocklen(struct drbg_state *drbg)
136{
137	if (drbg && drbg->core)
138		return drbg->core->blocklen_bytes;
139	return 0;
140}
141
142static inline __u8 drbg_keylen(struct drbg_state *drbg)
143{
144	if (drbg && drbg->core)
145		return (drbg->core->statelen - drbg->core->blocklen_bytes);
146	return 0;
147}
148
149static inline size_t drbg_max_request_bytes(struct drbg_state *drbg)
150{
151	/* SP800-90A requires the limit 2**19 bits, but we return bytes */
152	return (1 << 16);
153}
154
155static inline size_t drbg_max_addtl(struct drbg_state *drbg)
156{
157	/* SP800-90A requires 2**35 bytes additional info str / pers str */
158#if (__BITS_PER_LONG == 32)
159	/*
160	 * SP800-90A allows smaller maximum numbers to be returned -- we
161	 * return SIZE_MAX - 1 to allow the verification of the enforcement
162	 * of this value in drbg_healthcheck_sanity.
163	 */
164	return (SIZE_MAX - 1);
165#else
166	return (1UL<<35);
167#endif
168}
169
170static inline size_t drbg_max_requests(struct drbg_state *drbg)
171{
172	/* SP800-90A requires 2**48 maximum requests before reseeding */
173#if (__BITS_PER_LONG == 32)
174	return SIZE_MAX;
175#else
176	return (1UL<<48);
177#endif
178}
179
180/*
181 * This is a wrapper to the kernel crypto API function of
182 * crypto_rng_generate() to allow the caller to provide additional data.
183 *
184 * @drng DRBG handle -- see crypto_rng_get_bytes
185 * @outbuf output buffer -- see crypto_rng_get_bytes
186 * @outlen length of output buffer -- see crypto_rng_get_bytes
187 * @addtl_input additional information string input buffer
188 * @addtllen length of additional information string buffer
189 *
190 * return
191 *	see crypto_rng_get_bytes
192 */
193static inline int crypto_drbg_get_bytes_addtl(struct crypto_rng *drng,
194			unsigned char *outbuf, unsigned int outlen,
195			struct drbg_string *addtl)
196{
197	return crypto_rng_generate(drng, addtl->buf, addtl->len,
198				   outbuf, outlen);
199}
200
201/*
202 * TEST code
203 *
204 * This is a wrapper to the kernel crypto API function of
205 * crypto_rng_generate() to allow the caller to provide additional data and
206 * allow furnishing of test_data
207 *
208 * @drng DRBG handle -- see crypto_rng_get_bytes
209 * @outbuf output buffer -- see crypto_rng_get_bytes
210 * @outlen length of output buffer -- see crypto_rng_get_bytes
211 * @addtl_input additional information string input buffer
212 * @addtllen length of additional information string buffer
213 * @test_data filled test data
214 *
215 * return
216 *	see crypto_rng_get_bytes
217 */
218static inline int crypto_drbg_get_bytes_addtl_test(struct crypto_rng *drng,
219			unsigned char *outbuf, unsigned int outlen,
220			struct drbg_string *addtl,
221			struct drbg_test_data *test_data)
222{
223	crypto_rng_set_entropy(drng, test_data->testentropy->buf,
224			       test_data->testentropy->len);
225	return crypto_rng_generate(drng, addtl->buf, addtl->len,
226				   outbuf, outlen);
227}
228
229/*
230 * TEST code
231 *
232 * This is a wrapper to the kernel crypto API function of
233 * crypto_rng_reset() to allow the caller to provide test_data
234 *
235 * @drng DRBG handle -- see crypto_rng_reset
236 * @pers personalization string input buffer
237 * @perslen length of additional information string buffer
238 * @test_data filled test data
239 *
240 * return
241 *	see crypto_rng_reset
242 */
243static inline int crypto_drbg_reset_test(struct crypto_rng *drng,
244					 struct drbg_string *pers,
245					 struct drbg_test_data *test_data)
246{
247	crypto_rng_set_entropy(drng, test_data->testentropy->buf,
248			       test_data->testentropy->len);
249	return crypto_rng_reset(drng, pers->buf, pers->len);
250}
251
252/* DRBG type flags */
253#define DRBG_CTR	((drbg_flag_t)1<<0)
254#define DRBG_HMAC	((drbg_flag_t)1<<1)
255#define DRBG_HASH	((drbg_flag_t)1<<2)
256#define DRBG_TYPE_MASK	(DRBG_CTR | DRBG_HMAC | DRBG_HASH)
257/* DRBG strength flags */
258#define DRBG_STRENGTH128	((drbg_flag_t)1<<3)
259#define DRBG_STRENGTH192	((drbg_flag_t)1<<4)
260#define DRBG_STRENGTH256	((drbg_flag_t)1<<5)
261#define DRBG_STRENGTH_MASK	(DRBG_STRENGTH128 | DRBG_STRENGTH192 | \
262				 DRBG_STRENGTH256)
263
264enum drbg_prefixes {
265	DRBG_PREFIX0 = 0x00,
266	DRBG_PREFIX1,
267	DRBG_PREFIX2,
268	DRBG_PREFIX3
269};
270
271#endif /* _DRBG_H */