2 * DRBG based on NIST SP800-90A
4 * Copyright Stephan Mueller <smueller@chronox.de>, 2014
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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.)
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
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/spinlock.h>
53 #include <linux/list.h>
56 * Concatenation Helper and string operation helper
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.
66 const unsigned char *buf
;
68 struct list_head list
;
71 static inline void drbg_string_fill(struct drbg_string
*string
,
72 const unsigned char *buf
, size_t len
)
76 INIT_LIST_HEAD(&string
->list
);
80 typedef uint32_t drbg_flag_t
;
83 drbg_flag_t flags
; /* flags for the cipher */
84 __u8 statelen
; /* maximum state length */
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
];
91 struct drbg_state_ops
{
92 int (*update
)(struct drbg_state
*drbg
, struct list_head
*seed
,
94 int (*generate
)(struct drbg_state
*drbg
,
95 unsigned char *buf
, unsigned int buflen
,
96 struct list_head
*addtl
);
97 int (*crypto_init
)(struct drbg_state
*drbg
);
98 int (*crypto_fini
)(struct drbg_state
*drbg
);
102 struct drbg_test_data
{
103 struct drbg_string
*testentropy
; /* TEST PARAMETER: test entropy */
107 spinlock_t drbg_lock
; /* lock around DRBG */
108 unsigned char *V
; /* internal state 10.1.1.1 1a) */
109 /* hash: static value 10.1.1.1 1b) hmac / ctr: key */
111 /* Number of RNG requests since last reseed -- 10.1.1.1 1c) */
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 */
122 const struct drbg_state_ops
*d_ops
;
123 const struct drbg_core
*core
;
124 struct drbg_test_data
*test_data
;
127 static inline __u8
drbg_statelen(struct drbg_state
*drbg
)
129 if (drbg
&& drbg
->core
)
130 return drbg
->core
->statelen
;
134 static inline __u8
drbg_blocklen(struct drbg_state
*drbg
)
136 if (drbg
&& drbg
->core
)
137 return drbg
->core
->blocklen_bytes
;
141 static inline __u8
drbg_keylen(struct drbg_state
*drbg
)
143 if (drbg
&& drbg
->core
)
144 return (drbg
->core
->statelen
- drbg
->core
->blocklen_bytes
);
148 static inline size_t drbg_max_request_bytes(struct drbg_state
*drbg
)
150 /* SP800-90A requires the limit 2**19 bits, but we return bytes */
154 static inline size_t drbg_max_addtl(struct drbg_state
*drbg
)
156 /* SP800-90A requires 2**35 bytes additional info str / pers str */
157 #if (__BITS_PER_LONG == 32)
159 * SP800-90A allows smaller maximum numbers to be returned -- we
160 * return SIZE_MAX - 1 to allow the verification of the enforcement
161 * of this value in drbg_healthcheck_sanity.
163 return (SIZE_MAX
- 1);
169 static inline size_t drbg_max_requests(struct drbg_state
*drbg
)
171 /* SP800-90A requires 2**48 maximum requests before reseeding */
172 #if (__BITS_PER_LONG == 32)
180 * kernel crypto API input data structure for DRBG generate in case dlen
184 unsigned char *outbuf
; /* output buffer for random numbers */
185 unsigned int outlen
; /* size of output buffer */
186 struct drbg_string
*addtl
; /* additional information string */
187 struct drbg_test_data
*test_data
; /* test data */
191 * This is a wrapper to the kernel crypto API function of
192 * crypto_rng_get_bytes() to allow the caller to provide additional data.
194 * @drng DRBG handle -- see crypto_rng_get_bytes
195 * @outbuf output buffer -- see crypto_rng_get_bytes
196 * @outlen length of output buffer -- see crypto_rng_get_bytes
197 * @addtl_input additional information string input buffer
198 * @addtllen length of additional information string buffer
201 * see crypto_rng_get_bytes
203 static inline int crypto_drbg_get_bytes_addtl(struct crypto_rng
*drng
,
204 unsigned char *outbuf
, unsigned int outlen
,
205 struct drbg_string
*addtl
)
208 struct drbg_gen genbuf
;
209 genbuf
.outbuf
= outbuf
;
210 genbuf
.outlen
= outlen
;
211 genbuf
.addtl
= addtl
;
212 genbuf
.test_data
= NULL
;
213 ret
= crypto_rng_get_bytes(drng
, (u8
*)&genbuf
, 0);
220 * This is a wrapper to the kernel crypto API function of
221 * crypto_rng_get_bytes() to allow the caller to provide additional data and
222 * allow furnishing of test_data
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
232 * see crypto_rng_get_bytes
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
)
240 struct drbg_gen genbuf
;
241 genbuf
.outbuf
= outbuf
;
242 genbuf
.outlen
= outlen
;
243 genbuf
.addtl
= addtl
;
244 genbuf
.test_data
= test_data
;
245 ret
= crypto_rng_get_bytes(drng
, (u8
*)&genbuf
, 0);
252 * This is a wrapper to the kernel crypto API function of
253 * crypto_rng_reset() to allow the caller to provide test_data
255 * @drng DRBG handle -- see crypto_rng_reset
256 * @pers personalization string input buffer
257 * @perslen length of additional information string buffer
258 * @test_data filled test data
261 * see crypto_rng_reset
263 static inline int crypto_drbg_reset_test(struct crypto_rng
*drng
,
264 struct drbg_string
*pers
,
265 struct drbg_test_data
*test_data
)
268 struct drbg_gen genbuf
;
269 genbuf
.outbuf
= NULL
;
272 genbuf
.test_data
= test_data
;
273 ret
= crypto_rng_reset(drng
, (u8
*)&genbuf
, 0);
277 /* DRBG type flags */
278 #define DRBG_CTR ((drbg_flag_t)1<<0)
279 #define DRBG_HMAC ((drbg_flag_t)1<<1)
280 #define DRBG_HASH ((drbg_flag_t)1<<2)
281 #define DRBG_TYPE_MASK (DRBG_CTR | DRBG_HMAC | DRBG_HASH)
282 /* DRBG strength flags */
283 #define DRBG_STRENGTH128 ((drbg_flag_t)1<<3)
284 #define DRBG_STRENGTH192 ((drbg_flag_t)1<<4)
285 #define DRBG_STRENGTH256 ((drbg_flag_t)1<<5)
286 #define DRBG_STRENGTH_MASK (DRBG_STRENGTH128 | DRBG_STRENGTH192 | \