blob: aca86847ea49b49d15cb7790c9feab8bb48d9a2d [file] [log] [blame]
Stephan Mueller541af942014-05-31 15:44:17 +02001/*
2 * DRBG: Deterministic Random Bits Generator
3 * Based on NIST Recommended DRBG from NIST SP800-90A with the following
4 * properties:
5 * * CTR DRBG with DF with AES-128, AES-192, AES-256 cores
6 * * Hash DRBG with DF with SHA-1, SHA-256, SHA-384, SHA-512 cores
7 * * HMAC DRBG with DF with SHA-1, SHA-256, SHA-384, SHA-512 cores
8 * * with and without prediction resistance
9 *
10 * Copyright Stephan Mueller <smueller@chronox.de>, 2014
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, and the entire permission notice in its entirety,
17 * including the disclaimer of warranties.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. The name of the author may not be used to endorse or promote
22 * products derived from this software without specific prior
23 * written permission.
24 *
25 * ALTERNATIVELY, this product may be distributed under the terms of
26 * the GNU General Public License, in which case the provisions of the GPL are
27 * required INSTEAD OF the above restrictions. (This clause is
28 * necessary due to a potential bad interaction between the GPL and
29 * the restrictions contained in a BSD-style copyright.)
30 *
31 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
32 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
34 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
35 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
37 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
38 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
39 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
41 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
42 * DAMAGE.
43 *
44 * DRBG Usage
45 * ==========
46 * The SP 800-90A DRBG allows the user to specify a personalization string
47 * for initialization as well as an additional information string for each
48 * random number request. The following code fragments show how a caller
49 * uses the kernel crypto API to use the full functionality of the DRBG.
50 *
51 * Usage without any additional data
52 * ---------------------------------
53 * struct crypto_rng *drng;
54 * int err;
55 * char data[DATALEN];
56 *
57 * drng = crypto_alloc_rng(drng_name, 0, 0);
58 * err = crypto_rng_get_bytes(drng, &data, DATALEN);
59 * crypto_free_rng(drng);
60 *
61 *
62 * Usage with personalization string during initialization
63 * -------------------------------------------------------
64 * struct crypto_rng *drng;
65 * int err;
66 * char data[DATALEN];
67 * struct drbg_string pers;
68 * char personalization[11] = "some-string";
69 *
70 * drbg_string_fill(&pers, personalization, strlen(personalization));
71 * drng = crypto_alloc_rng(drng_name, 0, 0);
72 * // The reset completely re-initializes the DRBG with the provided
73 * // personalization string
74 * err = crypto_rng_reset(drng, &personalization, strlen(personalization));
75 * err = crypto_rng_get_bytes(drng, &data, DATALEN);
76 * crypto_free_rng(drng);
77 *
78 *
79 * Usage with additional information string during random number request
80 * ---------------------------------------------------------------------
81 * struct crypto_rng *drng;
82 * int err;
83 * char data[DATALEN];
84 * char addtl_string[11] = "some-string";
85 * string drbg_string addtl;
86 *
87 * drbg_string_fill(&addtl, addtl_string, strlen(addtl_string));
88 * drng = crypto_alloc_rng(drng_name, 0, 0);
89 * // The following call is a wrapper to crypto_rng_get_bytes() and returns
90 * // the same error codes.
91 * err = crypto_drbg_get_bytes_addtl(drng, &data, DATALEN, &addtl);
92 * crypto_free_rng(drng);
93 *
94 *
95 * Usage with personalization and additional information strings
96 * -------------------------------------------------------------
97 * Just mix both scenarios above.
98 */
99
100#include <crypto/drbg.h>
101
Stephan Mueller541af942014-05-31 15:44:17 +0200102/***************************************************************
103 * Backend cipher definitions available to DRBG
104 ***************************************************************/
105
106/*
107 * The order of the DRBG definitions here matter: every DRBG is registered
108 * as stdrng. Each DRBG receives an increasing cra_priority values the later
109 * they are defined in this array (see drbg_fill_array).
110 *
111 * HMAC DRBGs are favored over Hash DRBGs over CTR DRBGs, and
112 * the SHA256 / AES 256 over other ciphers. Thus, the favored
113 * DRBGs are the latest entries in this array.
114 */
115static const struct drbg_core drbg_cores[] = {
116#ifdef CONFIG_CRYPTO_DRBG_CTR
117 {
118 .flags = DRBG_CTR | DRBG_STRENGTH128,
119 .statelen = 32, /* 256 bits as defined in 10.2.1 */
Stephan Mueller541af942014-05-31 15:44:17 +0200120 .blocklen_bytes = 16,
121 .cra_name = "ctr_aes128",
Stephan Mueller04bcbfc2015-03-01 20:39:17 +0100122 .backend_cra_name = "aes",
Stephan Mueller541af942014-05-31 15:44:17 +0200123 }, {
124 .flags = DRBG_CTR | DRBG_STRENGTH192,
125 .statelen = 40, /* 320 bits as defined in 10.2.1 */
Stephan Mueller541af942014-05-31 15:44:17 +0200126 .blocklen_bytes = 16,
127 .cra_name = "ctr_aes192",
Stephan Mueller04bcbfc2015-03-01 20:39:17 +0100128 .backend_cra_name = "aes",
Stephan Mueller541af942014-05-31 15:44:17 +0200129 }, {
130 .flags = DRBG_CTR | DRBG_STRENGTH256,
131 .statelen = 48, /* 384 bits as defined in 10.2.1 */
Stephan Mueller541af942014-05-31 15:44:17 +0200132 .blocklen_bytes = 16,
133 .cra_name = "ctr_aes256",
Stephan Mueller04bcbfc2015-03-01 20:39:17 +0100134 .backend_cra_name = "aes",
Stephan Mueller541af942014-05-31 15:44:17 +0200135 },
136#endif /* CONFIG_CRYPTO_DRBG_CTR */
137#ifdef CONFIG_CRYPTO_DRBG_HASH
138 {
139 .flags = DRBG_HASH | DRBG_STRENGTH128,
140 .statelen = 55, /* 440 bits */
Stephan Mueller541af942014-05-31 15:44:17 +0200141 .blocklen_bytes = 20,
142 .cra_name = "sha1",
143 .backend_cra_name = "sha1",
144 }, {
145 .flags = DRBG_HASH | DRBG_STRENGTH256,
146 .statelen = 111, /* 888 bits */
Stephan Mueller541af942014-05-31 15:44:17 +0200147 .blocklen_bytes = 48,
148 .cra_name = "sha384",
149 .backend_cra_name = "sha384",
150 }, {
151 .flags = DRBG_HASH | DRBG_STRENGTH256,
152 .statelen = 111, /* 888 bits */
Stephan Mueller541af942014-05-31 15:44:17 +0200153 .blocklen_bytes = 64,
154 .cra_name = "sha512",
155 .backend_cra_name = "sha512",
156 }, {
157 .flags = DRBG_HASH | DRBG_STRENGTH256,
158 .statelen = 55, /* 440 bits */
Stephan Mueller541af942014-05-31 15:44:17 +0200159 .blocklen_bytes = 32,
160 .cra_name = "sha256",
161 .backend_cra_name = "sha256",
162 },
163#endif /* CONFIG_CRYPTO_DRBG_HASH */
164#ifdef CONFIG_CRYPTO_DRBG_HMAC
165 {
Stephan Mueller5b635e22014-07-06 02:26:37 +0200166 .flags = DRBG_HMAC | DRBG_STRENGTH128,
Stephan Mueller541af942014-05-31 15:44:17 +0200167 .statelen = 20, /* block length of cipher */
Stephan Mueller541af942014-05-31 15:44:17 +0200168 .blocklen_bytes = 20,
169 .cra_name = "hmac_sha1",
170 .backend_cra_name = "hmac(sha1)",
171 }, {
172 .flags = DRBG_HMAC | DRBG_STRENGTH256,
173 .statelen = 48, /* block length of cipher */
Stephan Mueller541af942014-05-31 15:44:17 +0200174 .blocklen_bytes = 48,
175 .cra_name = "hmac_sha384",
176 .backend_cra_name = "hmac(sha384)",
177 }, {
178 .flags = DRBG_HMAC | DRBG_STRENGTH256,
179 .statelen = 64, /* block length of cipher */
Stephan Mueller541af942014-05-31 15:44:17 +0200180 .blocklen_bytes = 64,
181 .cra_name = "hmac_sha512",
182 .backend_cra_name = "hmac(sha512)",
183 }, {
184 .flags = DRBG_HMAC | DRBG_STRENGTH256,
185 .statelen = 32, /* block length of cipher */
Stephan Mueller541af942014-05-31 15:44:17 +0200186 .blocklen_bytes = 32,
187 .cra_name = "hmac_sha256",
188 .backend_cra_name = "hmac(sha256)",
189 },
190#endif /* CONFIG_CRYPTO_DRBG_HMAC */
191};
192
193/******************************************************************
194 * Generic helper functions
195 ******************************************************************/
196
197/*
198 * Return strength of DRBG according to SP800-90A section 8.4
199 *
200 * @flags DRBG flags reference
201 *
202 * Return: normalized strength in *bytes* value or 32 as default
203 * to counter programming errors
204 */
205static inline unsigned short drbg_sec_strength(drbg_flag_t flags)
206{
207 switch (flags & DRBG_STRENGTH_MASK) {
208 case DRBG_STRENGTH128:
209 return 16;
210 case DRBG_STRENGTH192:
211 return 24;
212 case DRBG_STRENGTH256:
213 return 32;
214 default:
215 return 32;
216 }
217}
218
219/*
220 * FIPS 140-2 continuous self test
221 * The test is performed on the result of one round of the output
222 * function. Thus, the function implicitly knows the size of the
223 * buffer.
224 *
Stephan Mueller541af942014-05-31 15:44:17 +0200225 * @drbg DRBG handle
226 * @buf output buffer of random data to be checked
227 *
228 * return:
229 * true on success
230 * false on error
231 */
232static bool drbg_fips_continuous_test(struct drbg_state *drbg,
233 const unsigned char *buf)
234{
235#ifdef CONFIG_CRYPTO_FIPS
236 int ret = 0;
237 /* skip test if we test the overall system */
Herbert Xu8fded592015-04-21 10:46:41 +0800238 if (list_empty(&drbg->test_data.list))
Stephan Mueller541af942014-05-31 15:44:17 +0200239 return true;
240 /* only perform test in FIPS mode */
241 if (0 == fips_enabled)
242 return true;
243 if (!drbg->fips_primed) {
244 /* Priming of FIPS test */
245 memcpy(drbg->prev, buf, drbg_blocklen(drbg));
246 drbg->fips_primed = true;
247 /* return false due to priming, i.e. another round is needed */
248 return false;
249 }
250 ret = memcmp(drbg->prev, buf, drbg_blocklen(drbg));
Stephan Mueller905b42e2014-12-05 22:40:21 +0100251 if (!ret)
252 panic("DRBG continuous self test failed\n");
Stephan Mueller541af942014-05-31 15:44:17 +0200253 memcpy(drbg->prev, buf, drbg_blocklen(drbg));
254 /* the test shall pass when the two compared values are not equal */
255 return ret != 0;
256#else
257 return true;
258#endif /* CONFIG_CRYPTO_FIPS */
259}
260
261/*
262 * Convert an integer into a byte representation of this integer.
263 * The byte representation is big-endian
264 *
Stephan Mueller541af942014-05-31 15:44:17 +0200265 * @val value to be converted
Stephan Mueller72f3e002014-08-17 17:37:34 +0200266 * @buf buffer holding the converted integer -- caller must ensure that
267 * buffer size is at least 32 bit
Stephan Mueller541af942014-05-31 15:44:17 +0200268 */
269#if (defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_CTR))
Stephan Mueller72f3e002014-08-17 17:37:34 +0200270static inline void drbg_cpu_to_be32(__u32 val, unsigned char *buf)
Stephan Mueller541af942014-05-31 15:44:17 +0200271{
Stephan Mueller72f3e002014-08-17 17:37:34 +0200272 struct s {
Stephan Mueller7c8ae032014-08-26 09:32:24 +0200273 __be32 conv;
Stephan Mueller72f3e002014-08-17 17:37:34 +0200274 };
275 struct s *conversion = (struct s *) buf;
Stephan Mueller541af942014-05-31 15:44:17 +0200276
Stephan Mueller72f3e002014-08-17 17:37:34 +0200277 conversion->conv = cpu_to_be32(val);
Stephan Mueller541af942014-05-31 15:44:17 +0200278}
Stephan Mueller541af942014-05-31 15:44:17 +0200279#endif /* defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_CTR) */
280
281/******************************************************************
282 * CTR DRBG callback functions
283 ******************************************************************/
284
285#ifdef CONFIG_CRYPTO_DRBG_CTR
Stephan Muellere25e47e2014-07-06 02:23:03 +0200286#define CRYPTO_DRBG_CTR_STRING "CTR "
Stephan Mueller0653a7c2014-11-25 09:28:43 +0100287MODULE_ALIAS_CRYPTO("drbg_pr_ctr_aes256");
288MODULE_ALIAS_CRYPTO("drbg_nopr_ctr_aes256");
289MODULE_ALIAS_CRYPTO("drbg_pr_ctr_aes192");
290MODULE_ALIAS_CRYPTO("drbg_nopr_ctr_aes192");
291MODULE_ALIAS_CRYPTO("drbg_pr_ctr_aes128");
292MODULE_ALIAS_CRYPTO("drbg_nopr_ctr_aes128");
Stephan Mueller62b62b62014-11-04 03:08:09 +0100293
Stephan Mueller541af942014-05-31 15:44:17 +0200294static int drbg_kcapi_sym(struct drbg_state *drbg, const unsigned char *key,
295 unsigned char *outval, const struct drbg_string *in);
296static int drbg_init_sym_kernel(struct drbg_state *drbg);
297static int drbg_fini_sym_kernel(struct drbg_state *drbg);
298
299/* BCC function for CTR DRBG as defined in 10.4.3 */
300static int drbg_ctr_bcc(struct drbg_state *drbg,
301 unsigned char *out, const unsigned char *key,
Stephan Mueller8c987162014-06-28 21:58:24 +0200302 struct list_head *in)
Stephan Mueller541af942014-05-31 15:44:17 +0200303{
Stephan Mueller8c987162014-06-28 21:58:24 +0200304 int ret = 0;
305 struct drbg_string *curr = NULL;
Stephan Mueller541af942014-05-31 15:44:17 +0200306 struct drbg_string data;
Stephan Mueller8c987162014-06-28 21:58:24 +0200307 short cnt = 0;
Stephan Mueller541af942014-05-31 15:44:17 +0200308
309 drbg_string_fill(&data, out, drbg_blocklen(drbg));
310
Stephan Mueller541af942014-05-31 15:44:17 +0200311 /* 10.4.3 step 2 / 4 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200312 list_for_each_entry(curr, in, list) {
313 const unsigned char *pos = curr->buf;
314 size_t len = curr->len;
Stephan Mueller541af942014-05-31 15:44:17 +0200315 /* 10.4.3 step 4.1 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200316 while (len) {
317 /* 10.4.3 step 4.2 */
318 if (drbg_blocklen(drbg) == cnt) {
319 cnt = 0;
320 ret = drbg_kcapi_sym(drbg, key, out, &data);
321 if (ret)
322 return ret;
Stephan Mueller541af942014-05-31 15:44:17 +0200323 }
Stephan Mueller8c987162014-06-28 21:58:24 +0200324 out[cnt] ^= *pos;
325 pos++;
326 cnt++;
327 len--;
Stephan Mueller541af942014-05-31 15:44:17 +0200328 }
Stephan Mueller541af942014-05-31 15:44:17 +0200329 }
Stephan Mueller8c987162014-06-28 21:58:24 +0200330 /* 10.4.3 step 4.2 for last block */
331 if (cnt)
332 ret = drbg_kcapi_sym(drbg, key, out, &data);
333
334 return ret;
Stephan Mueller541af942014-05-31 15:44:17 +0200335}
336
337/*
338 * scratchpad usage: drbg_ctr_update is interlinked with drbg_ctr_df
339 * (and drbg_ctr_bcc, but this function does not need any temporary buffers),
340 * the scratchpad is used as follows:
341 * drbg_ctr_update:
342 * temp
343 * start: drbg->scratchpad
344 * length: drbg_statelen(drbg) + drbg_blocklen(drbg)
345 * note: the cipher writing into this variable works
346 * blocklen-wise. Now, when the statelen is not a multiple
347 * of blocklen, the generateion loop below "spills over"
348 * by at most blocklen. Thus, we need to give sufficient
349 * memory.
350 * df_data
351 * start: drbg->scratchpad +
352 * drbg_statelen(drbg) + drbg_blocklen(drbg)
353 * length: drbg_statelen(drbg)
354 *
355 * drbg_ctr_df:
356 * pad
357 * start: df_data + drbg_statelen(drbg)
358 * length: drbg_blocklen(drbg)
359 * iv
360 * start: pad + drbg_blocklen(drbg)
361 * length: drbg_blocklen(drbg)
362 * temp
363 * start: iv + drbg_blocklen(drbg)
Stephan Mueller8fecaad2014-07-01 17:08:48 +0200364 * length: drbg_satelen(drbg) + drbg_blocklen(drbg)
365 * note: temp is the buffer that the BCC function operates
366 * on. BCC operates blockwise. drbg_statelen(drbg)
367 * is sufficient when the DRBG state length is a multiple
368 * of the block size. For AES192 (and maybe other ciphers)
369 * this is not correct and the length for temp is
370 * insufficient (yes, that also means for such ciphers,
371 * the final output of all BCC rounds are truncated).
372 * Therefore, add drbg_blocklen(drbg) to cover all
373 * possibilities.
Stephan Mueller541af942014-05-31 15:44:17 +0200374 */
375
376/* Derivation Function for CTR DRBG as defined in 10.4.2 */
377static int drbg_ctr_df(struct drbg_state *drbg,
378 unsigned char *df_data, size_t bytes_to_return,
Stephan Mueller8c987162014-06-28 21:58:24 +0200379 struct list_head *seedlist)
Stephan Mueller541af942014-05-31 15:44:17 +0200380{
381 int ret = -EFAULT;
382 unsigned char L_N[8];
383 /* S3 is input */
384 struct drbg_string S1, S2, S4, cipherin;
Stephan Mueller8c987162014-06-28 21:58:24 +0200385 LIST_HEAD(bcc_list);
Stephan Mueller541af942014-05-31 15:44:17 +0200386 unsigned char *pad = df_data + drbg_statelen(drbg);
387 unsigned char *iv = pad + drbg_blocklen(drbg);
388 unsigned char *temp = iv + drbg_blocklen(drbg);
389 size_t padlen = 0;
390 unsigned int templen = 0;
391 /* 10.4.2 step 7 */
392 unsigned int i = 0;
393 /* 10.4.2 step 8 */
394 const unsigned char *K = (unsigned char *)
395 "\x00\x01\x02\x03\x04\x05\x06\x07"
396 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
397 "\x10\x11\x12\x13\x14\x15\x16\x17"
398 "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f";
399 unsigned char *X;
400 size_t generated_len = 0;
401 size_t inputlen = 0;
Stephan Mueller8c987162014-06-28 21:58:24 +0200402 struct drbg_string *seed = NULL;
Stephan Mueller541af942014-05-31 15:44:17 +0200403
404 memset(pad, 0, drbg_blocklen(drbg));
405 memset(iv, 0, drbg_blocklen(drbg));
Stephan Mueller541af942014-05-31 15:44:17 +0200406
407 /* 10.4.2 step 1 is implicit as we work byte-wise */
408
409 /* 10.4.2 step 2 */
410 if ((512/8) < bytes_to_return)
411 return -EINVAL;
412
413 /* 10.4.2 step 2 -- calculate the entire length of all input data */
Stephan Mueller8c987162014-06-28 21:58:24 +0200414 list_for_each_entry(seed, seedlist, list)
415 inputlen += seed->len;
Stephan Mueller72f3e002014-08-17 17:37:34 +0200416 drbg_cpu_to_be32(inputlen, &L_N[0]);
Stephan Mueller541af942014-05-31 15:44:17 +0200417
418 /* 10.4.2 step 3 */
Stephan Mueller72f3e002014-08-17 17:37:34 +0200419 drbg_cpu_to_be32(bytes_to_return, &L_N[4]);
Stephan Mueller541af942014-05-31 15:44:17 +0200420
421 /* 10.4.2 step 5: length is L_N, input_string, one byte, padding */
422 padlen = (inputlen + sizeof(L_N) + 1) % (drbg_blocklen(drbg));
423 /* wrap the padlen appropriately */
424 if (padlen)
425 padlen = drbg_blocklen(drbg) - padlen;
426 /*
427 * pad / padlen contains the 0x80 byte and the following zero bytes.
428 * As the calculated padlen value only covers the number of zero
429 * bytes, this value has to be incremented by one for the 0x80 byte.
430 */
431 padlen++;
432 pad[0] = 0x80;
433
434 /* 10.4.2 step 4 -- first fill the linked list and then order it */
435 drbg_string_fill(&S1, iv, drbg_blocklen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +0200436 list_add_tail(&S1.list, &bcc_list);
Stephan Mueller541af942014-05-31 15:44:17 +0200437 drbg_string_fill(&S2, L_N, sizeof(L_N));
Stephan Mueller8c987162014-06-28 21:58:24 +0200438 list_add_tail(&S2.list, &bcc_list);
439 list_splice_tail(seedlist, &bcc_list);
Stephan Mueller541af942014-05-31 15:44:17 +0200440 drbg_string_fill(&S4, pad, padlen);
Stephan Mueller8c987162014-06-28 21:58:24 +0200441 list_add_tail(&S4.list, &bcc_list);
Stephan Mueller541af942014-05-31 15:44:17 +0200442
443 /* 10.4.2 step 9 */
444 while (templen < (drbg_keylen(drbg) + (drbg_blocklen(drbg)))) {
445 /*
446 * 10.4.2 step 9.1 - the padding is implicit as the buffer
447 * holds zeros after allocation -- even the increment of i
448 * is irrelevant as the increment remains within length of i
449 */
Stephan Mueller72f3e002014-08-17 17:37:34 +0200450 drbg_cpu_to_be32(i, iv);
Stephan Mueller541af942014-05-31 15:44:17 +0200451 /* 10.4.2 step 9.2 -- BCC and concatenation with temp */
Stephan Mueller8c987162014-06-28 21:58:24 +0200452 ret = drbg_ctr_bcc(drbg, temp + templen, K, &bcc_list);
Stephan Mueller541af942014-05-31 15:44:17 +0200453 if (ret)
454 goto out;
455 /* 10.4.2 step 9.3 */
456 i++;
457 templen += drbg_blocklen(drbg);
458 }
459
460 /* 10.4.2 step 11 */
461 X = temp + (drbg_keylen(drbg));
462 drbg_string_fill(&cipherin, X, drbg_blocklen(drbg));
463
464 /* 10.4.2 step 12: overwriting of outval is implemented in next step */
465
466 /* 10.4.2 step 13 */
467 while (generated_len < bytes_to_return) {
468 short blocklen = 0;
469 /*
470 * 10.4.2 step 13.1: the truncation of the key length is
471 * implicit as the key is only drbg_blocklen in size based on
472 * the implementation of the cipher function callback
473 */
474 ret = drbg_kcapi_sym(drbg, temp, X, &cipherin);
475 if (ret)
476 goto out;
477 blocklen = (drbg_blocklen(drbg) <
478 (bytes_to_return - generated_len)) ?
479 drbg_blocklen(drbg) :
480 (bytes_to_return - generated_len);
481 /* 10.4.2 step 13.2 and 14 */
482 memcpy(df_data + generated_len, X, blocklen);
483 generated_len += blocklen;
484 }
485
486 ret = 0;
487
488out:
Herbert Xu1471f092015-01-05 10:44:09 +1100489 memset(iv, 0, drbg_blocklen(drbg));
Stephan Mueller8e0498d2015-04-17 14:54:08 +0200490 memset(temp, 0, drbg_statelen(drbg) + drbg_blocklen(drbg));
Herbert Xu1471f092015-01-05 10:44:09 +1100491 memset(pad, 0, drbg_blocklen(drbg));
Stephan Mueller541af942014-05-31 15:44:17 +0200492 return ret;
493}
494
Stephan Mueller72e7c252014-07-06 02:24:35 +0200495/*
496 * update function of CTR DRBG as defined in 10.2.1.2
497 *
498 * The reseed variable has an enhanced meaning compared to the update
499 * functions of the other DRBGs as follows:
500 * 0 => initial seed from initialization
501 * 1 => reseed via drbg_seed
502 * 2 => first invocation from drbg_ctr_update when addtl is present. In
503 * this case, the df_data scratchpad is not deleted so that it is
504 * available for another calls to prevent calling the DF function
505 * again.
506 * 3 => second invocation from drbg_ctr_update. When the update function
507 * was called with addtl, the df_data memory already contains the
508 * DFed addtl information and we do not need to call DF again.
509 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200510static int drbg_ctr_update(struct drbg_state *drbg, struct list_head *seed,
511 int reseed)
Stephan Mueller541af942014-05-31 15:44:17 +0200512{
513 int ret = -EFAULT;
514 /* 10.2.1.2 step 1 */
515 unsigned char *temp = drbg->scratchpad;
516 unsigned char *df_data = drbg->scratchpad + drbg_statelen(drbg) +
517 drbg_blocklen(drbg);
518 unsigned char *temp_p, *df_data_p; /* pointer to iterate over buffers */
519 unsigned int len = 0;
520 struct drbg_string cipherin;
Stephan Mueller541af942014-05-31 15:44:17 +0200521
Stephan Mueller72e7c252014-07-06 02:24:35 +0200522 if (3 > reseed)
523 memset(df_data, 0, drbg_statelen(drbg));
Stephan Mueller541af942014-05-31 15:44:17 +0200524
525 /* 10.2.1.3.2 step 2 and 10.2.1.4.2 step 2 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200526 if (seed) {
527 ret = drbg_ctr_df(drbg, df_data, drbg_statelen(drbg), seed);
Stephan Mueller541af942014-05-31 15:44:17 +0200528 if (ret)
529 goto out;
530 }
531
532 drbg_string_fill(&cipherin, drbg->V, drbg_blocklen(drbg));
533 /*
534 * 10.2.1.3.2 steps 2 and 3 are already covered as the allocation
535 * zeroizes all memory during initialization
536 */
537 while (len < (drbg_statelen(drbg))) {
538 /* 10.2.1.2 step 2.1 */
Stephan Mueller41a84982014-10-14 21:50:13 +0200539 crypto_inc(drbg->V, drbg_blocklen(drbg));
Stephan Mueller541af942014-05-31 15:44:17 +0200540 /*
541 * 10.2.1.2 step 2.2 */
542 ret = drbg_kcapi_sym(drbg, drbg->C, temp + len, &cipherin);
543 if (ret)
544 goto out;
545 /* 10.2.1.2 step 2.3 and 3 */
546 len += drbg_blocklen(drbg);
547 }
548
549 /* 10.2.1.2 step 4 */
550 temp_p = temp;
551 df_data_p = df_data;
552 for (len = 0; len < drbg_statelen(drbg); len++) {
553 *temp_p ^= *df_data_p;
554 df_data_p++; temp_p++;
555 }
556
557 /* 10.2.1.2 step 5 */
558 memcpy(drbg->C, temp, drbg_keylen(drbg));
559 /* 10.2.1.2 step 6 */
560 memcpy(drbg->V, temp + drbg_keylen(drbg), drbg_blocklen(drbg));
561 ret = 0;
562
563out:
Herbert Xu1471f092015-01-05 10:44:09 +1100564 memset(temp, 0, drbg_statelen(drbg) + drbg_blocklen(drbg));
Stephan Mueller72e7c252014-07-06 02:24:35 +0200565 if (2 != reseed)
Herbert Xu1471f092015-01-05 10:44:09 +1100566 memset(df_data, 0, drbg_statelen(drbg));
Stephan Mueller541af942014-05-31 15:44:17 +0200567 return ret;
568}
569
570/*
571 * scratchpad use: drbg_ctr_update is called independently from
572 * drbg_ctr_extract_bytes. Therefore, the scratchpad is reused
573 */
574/* Generate function of CTR DRBG as defined in 10.2.1.5.2 */
575static int drbg_ctr_generate(struct drbg_state *drbg,
576 unsigned char *buf, unsigned int buflen,
Stephan Mueller27e4de22014-07-06 02:25:36 +0200577 struct list_head *addtl)
Stephan Mueller541af942014-05-31 15:44:17 +0200578{
579 int len = 0;
580 int ret = 0;
581 struct drbg_string data;
Stephan Mueller541af942014-05-31 15:44:17 +0200582
Stephan Mueller541af942014-05-31 15:44:17 +0200583 /* 10.2.1.5.2 step 2 */
Stephan Mueller27e4de22014-07-06 02:25:36 +0200584 if (addtl && !list_empty(addtl)) {
585 ret = drbg_ctr_update(drbg, addtl, 2);
Stephan Mueller541af942014-05-31 15:44:17 +0200586 if (ret)
587 return 0;
588 }
589
590 /* 10.2.1.5.2 step 4.1 */
Stephan Mueller41a84982014-10-14 21:50:13 +0200591 crypto_inc(drbg->V, drbg_blocklen(drbg));
Stephan Mueller541af942014-05-31 15:44:17 +0200592 drbg_string_fill(&data, drbg->V, drbg_blocklen(drbg));
593 while (len < buflen) {
594 int outlen = 0;
595 /* 10.2.1.5.2 step 4.2 */
596 ret = drbg_kcapi_sym(drbg, drbg->C, drbg->scratchpad, &data);
597 if (ret) {
598 len = ret;
599 goto out;
600 }
601 outlen = (drbg_blocklen(drbg) < (buflen - len)) ?
602 drbg_blocklen(drbg) : (buflen - len);
603 if (!drbg_fips_continuous_test(drbg, drbg->scratchpad)) {
604 /* 10.2.1.5.2 step 6 */
Stephan Mueller41a84982014-10-14 21:50:13 +0200605 crypto_inc(drbg->V, drbg_blocklen(drbg));
Stephan Mueller541af942014-05-31 15:44:17 +0200606 continue;
607 }
608 /* 10.2.1.5.2 step 4.3 */
609 memcpy(buf + len, drbg->scratchpad, outlen);
610 len += outlen;
611 /* 10.2.1.5.2 step 6 */
612 if (len < buflen)
Stephan Mueller41a84982014-10-14 21:50:13 +0200613 crypto_inc(drbg->V, drbg_blocklen(drbg));
Stephan Mueller541af942014-05-31 15:44:17 +0200614 }
615
Stephan Mueller72e7c252014-07-06 02:24:35 +0200616 /* 10.2.1.5.2 step 6 */
617 ret = drbg_ctr_update(drbg, NULL, 3);
Stephan Mueller541af942014-05-31 15:44:17 +0200618 if (ret)
619 len = ret;
620
621out:
Herbert Xu1471f092015-01-05 10:44:09 +1100622 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
Stephan Mueller541af942014-05-31 15:44:17 +0200623 return len;
624}
625
626static struct drbg_state_ops drbg_ctr_ops = {
627 .update = drbg_ctr_update,
628 .generate = drbg_ctr_generate,
629 .crypto_init = drbg_init_sym_kernel,
630 .crypto_fini = drbg_fini_sym_kernel,
631};
632#endif /* CONFIG_CRYPTO_DRBG_CTR */
633
634/******************************************************************
635 * HMAC DRBG callback functions
636 ******************************************************************/
637
638#if defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_HMAC)
639static int drbg_kcapi_hash(struct drbg_state *drbg, const unsigned char *key,
Stephan Mueller8c987162014-06-28 21:58:24 +0200640 unsigned char *outval, const struct list_head *in);
Stephan Mueller541af942014-05-31 15:44:17 +0200641static int drbg_init_hash_kernel(struct drbg_state *drbg);
642static int drbg_fini_hash_kernel(struct drbg_state *drbg);
643#endif /* (CONFIG_CRYPTO_DRBG_HASH || CONFIG_CRYPTO_DRBG_HMAC) */
644
645#ifdef CONFIG_CRYPTO_DRBG_HMAC
Stephan Muellere25e47e2014-07-06 02:23:03 +0200646#define CRYPTO_DRBG_HMAC_STRING "HMAC "
Stephan Mueller0653a7c2014-11-25 09:28:43 +0100647MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha512");
648MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha512");
649MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha384");
650MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha384");
651MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha256");
652MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha256");
653MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha1");
654MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha1");
Stephan Mueller62b62b62014-11-04 03:08:09 +0100655
Stephan Mueller541af942014-05-31 15:44:17 +0200656/* update function of HMAC DRBG as defined in 10.1.2.2 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200657static int drbg_hmac_update(struct drbg_state *drbg, struct list_head *seed,
658 int reseed)
Stephan Mueller541af942014-05-31 15:44:17 +0200659{
660 int ret = -EFAULT;
661 int i = 0;
Stephan Mueller8c987162014-06-28 21:58:24 +0200662 struct drbg_string seed1, seed2, vdata;
663 LIST_HEAD(seedlist);
664 LIST_HEAD(vdatalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200665
Stephan Muellerf072f0e2014-08-17 17:38:58 +0200666 if (!reseed)
667 /* 10.1.2.3 step 2 -- memset(0) of C is implicit with kzalloc */
Stephan Mueller541af942014-05-31 15:44:17 +0200668 memset(drbg->V, 1, drbg_statelen(drbg));
Stephan Mueller541af942014-05-31 15:44:17 +0200669
670 drbg_string_fill(&seed1, drbg->V, drbg_statelen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +0200671 list_add_tail(&seed1.list, &seedlist);
Stephan Mueller541af942014-05-31 15:44:17 +0200672 /* buffer of seed2 will be filled in for loop below with one byte */
673 drbg_string_fill(&seed2, NULL, 1);
Stephan Mueller8c987162014-06-28 21:58:24 +0200674 list_add_tail(&seed2.list, &seedlist);
Stephan Mueller541af942014-05-31 15:44:17 +0200675 /* input data of seed is allowed to be NULL at this point */
Stephan Mueller8c987162014-06-28 21:58:24 +0200676 if (seed)
677 list_splice_tail(seed, &seedlist);
Stephan Mueller541af942014-05-31 15:44:17 +0200678
Stephan Mueller8c987162014-06-28 21:58:24 +0200679 drbg_string_fill(&vdata, drbg->V, drbg_statelen(drbg));
680 list_add_tail(&vdata.list, &vdatalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200681 for (i = 2; 0 < i; i--) {
682 /* first round uses 0x0, second 0x1 */
683 unsigned char prefix = DRBG_PREFIX0;
684 if (1 == i)
685 prefix = DRBG_PREFIX1;
686 /* 10.1.2.2 step 1 and 4 -- concatenation and HMAC for key */
687 seed2.buf = &prefix;
Stephan Mueller8c987162014-06-28 21:58:24 +0200688 ret = drbg_kcapi_hash(drbg, drbg->C, drbg->C, &seedlist);
Stephan Mueller541af942014-05-31 15:44:17 +0200689 if (ret)
690 return ret;
691
692 /* 10.1.2.2 step 2 and 5 -- HMAC for V */
Stephan Mueller8c987162014-06-28 21:58:24 +0200693 ret = drbg_kcapi_hash(drbg, drbg->C, drbg->V, &vdatalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200694 if (ret)
695 return ret;
696
697 /* 10.1.2.2 step 3 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200698 if (!seed)
Stephan Mueller541af942014-05-31 15:44:17 +0200699 return ret;
700 }
701
702 return 0;
703}
704
705/* generate function of HMAC DRBG as defined in 10.1.2.5 */
706static int drbg_hmac_generate(struct drbg_state *drbg,
707 unsigned char *buf,
708 unsigned int buflen,
Stephan Mueller27e4de22014-07-06 02:25:36 +0200709 struct list_head *addtl)
Stephan Mueller541af942014-05-31 15:44:17 +0200710{
711 int len = 0;
712 int ret = 0;
713 struct drbg_string data;
Stephan Mueller8c987162014-06-28 21:58:24 +0200714 LIST_HEAD(datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200715
716 /* 10.1.2.5 step 2 */
Stephan Mueller27e4de22014-07-06 02:25:36 +0200717 if (addtl && !list_empty(addtl)) {
718 ret = drbg_hmac_update(drbg, addtl, 1);
Stephan Mueller541af942014-05-31 15:44:17 +0200719 if (ret)
720 return ret;
721 }
722
723 drbg_string_fill(&data, drbg->V, drbg_statelen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +0200724 list_add_tail(&data.list, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200725 while (len < buflen) {
726 unsigned int outlen = 0;
727 /* 10.1.2.5 step 4.1 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200728 ret = drbg_kcapi_hash(drbg, drbg->C, drbg->V, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200729 if (ret)
730 return ret;
731 outlen = (drbg_blocklen(drbg) < (buflen - len)) ?
732 drbg_blocklen(drbg) : (buflen - len);
733 if (!drbg_fips_continuous_test(drbg, drbg->V))
734 continue;
735
736 /* 10.1.2.5 step 4.2 */
737 memcpy(buf + len, drbg->V, outlen);
738 len += outlen;
739 }
740
741 /* 10.1.2.5 step 6 */
Stephan Mueller27e4de22014-07-06 02:25:36 +0200742 if (addtl && !list_empty(addtl))
743 ret = drbg_hmac_update(drbg, addtl, 1);
744 else
Stephan Mueller8c987162014-06-28 21:58:24 +0200745 ret = drbg_hmac_update(drbg, NULL, 1);
Stephan Mueller541af942014-05-31 15:44:17 +0200746 if (ret)
747 return ret;
748
749 return len;
750}
751
752static struct drbg_state_ops drbg_hmac_ops = {
753 .update = drbg_hmac_update,
754 .generate = drbg_hmac_generate,
755 .crypto_init = drbg_init_hash_kernel,
756 .crypto_fini = drbg_fini_hash_kernel,
Stephan Mueller541af942014-05-31 15:44:17 +0200757};
758#endif /* CONFIG_CRYPTO_DRBG_HMAC */
759
760/******************************************************************
761 * Hash DRBG callback functions
762 ******************************************************************/
763
764#ifdef CONFIG_CRYPTO_DRBG_HASH
Stephan Muellere25e47e2014-07-06 02:23:03 +0200765#define CRYPTO_DRBG_HASH_STRING "HASH "
Stephan Mueller0653a7c2014-11-25 09:28:43 +0100766MODULE_ALIAS_CRYPTO("drbg_pr_sha512");
767MODULE_ALIAS_CRYPTO("drbg_nopr_sha512");
768MODULE_ALIAS_CRYPTO("drbg_pr_sha384");
769MODULE_ALIAS_CRYPTO("drbg_nopr_sha384");
770MODULE_ALIAS_CRYPTO("drbg_pr_sha256");
771MODULE_ALIAS_CRYPTO("drbg_nopr_sha256");
772MODULE_ALIAS_CRYPTO("drbg_pr_sha1");
773MODULE_ALIAS_CRYPTO("drbg_nopr_sha1");
Stephan Mueller62b62b62014-11-04 03:08:09 +0100774
Stephan Mueller541af942014-05-31 15:44:17 +0200775/*
Stephan Mueller41a84982014-10-14 21:50:13 +0200776 * Increment buffer
777 *
778 * @dst buffer to increment
779 * @add value to add
780 */
781static inline void drbg_add_buf(unsigned char *dst, size_t dstlen,
782 const unsigned char *add, size_t addlen)
783{
784 /* implied: dstlen > addlen */
785 unsigned char *dstptr;
786 const unsigned char *addptr;
787 unsigned int remainder = 0;
788 size_t len = addlen;
789
790 dstptr = dst + (dstlen-1);
791 addptr = add + (addlen-1);
792 while (len) {
793 remainder += *dstptr + *addptr;
794 *dstptr = remainder & 0xff;
795 remainder >>= 8;
796 len--; dstptr--; addptr--;
797 }
798 len = dstlen - addlen;
799 while (len && remainder > 0) {
800 remainder = *dstptr + 1;
801 *dstptr = remainder & 0xff;
802 remainder >>= 8;
803 len--; dstptr--;
804 }
805}
806
807/*
Stephan Mueller541af942014-05-31 15:44:17 +0200808 * scratchpad usage: as drbg_hash_update and drbg_hash_df are used
809 * interlinked, the scratchpad is used as follows:
810 * drbg_hash_update
811 * start: drbg->scratchpad
812 * length: drbg_statelen(drbg)
813 * drbg_hash_df:
814 * start: drbg->scratchpad + drbg_statelen(drbg)
815 * length: drbg_blocklen(drbg)
816 *
817 * drbg_hash_process_addtl uses the scratchpad, but fully completes
818 * before either of the functions mentioned before are invoked. Therefore,
819 * drbg_hash_process_addtl does not need to be specifically considered.
820 */
821
822/* Derivation Function for Hash DRBG as defined in 10.4.1 */
823static int drbg_hash_df(struct drbg_state *drbg,
824 unsigned char *outval, size_t outlen,
Stephan Mueller8c987162014-06-28 21:58:24 +0200825 struct list_head *entropylist)
Stephan Mueller541af942014-05-31 15:44:17 +0200826{
827 int ret = 0;
828 size_t len = 0;
829 unsigned char input[5];
830 unsigned char *tmp = drbg->scratchpad + drbg_statelen(drbg);
Stephan Mueller8c987162014-06-28 21:58:24 +0200831 struct drbg_string data;
Stephan Mueller541af942014-05-31 15:44:17 +0200832
Stephan Mueller541af942014-05-31 15:44:17 +0200833 /* 10.4.1 step 3 */
834 input[0] = 1;
Stephan Mueller72f3e002014-08-17 17:37:34 +0200835 drbg_cpu_to_be32((outlen * 8), &input[1]);
Stephan Mueller541af942014-05-31 15:44:17 +0200836
837 /* 10.4.1 step 4.1 -- concatenation of data for input into hash */
Stephan Mueller8c987162014-06-28 21:58:24 +0200838 drbg_string_fill(&data, input, 5);
839 list_add(&data.list, entropylist);
Stephan Mueller541af942014-05-31 15:44:17 +0200840
841 /* 10.4.1 step 4 */
842 while (len < outlen) {
843 short blocklen = 0;
844 /* 10.4.1 step 4.1 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200845 ret = drbg_kcapi_hash(drbg, NULL, tmp, entropylist);
Stephan Mueller541af942014-05-31 15:44:17 +0200846 if (ret)
847 goto out;
848 /* 10.4.1 step 4.2 */
849 input[0]++;
850 blocklen = (drbg_blocklen(drbg) < (outlen - len)) ?
851 drbg_blocklen(drbg) : (outlen - len);
852 memcpy(outval + len, tmp, blocklen);
853 len += blocklen;
854 }
855
856out:
Herbert Xu1471f092015-01-05 10:44:09 +1100857 memset(tmp, 0, drbg_blocklen(drbg));
Stephan Mueller541af942014-05-31 15:44:17 +0200858 return ret;
859}
860
861/* update function for Hash DRBG as defined in 10.1.1.2 / 10.1.1.3 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200862static int drbg_hash_update(struct drbg_state *drbg, struct list_head *seed,
Stephan Mueller541af942014-05-31 15:44:17 +0200863 int reseed)
864{
865 int ret = 0;
866 struct drbg_string data1, data2;
Stephan Mueller8c987162014-06-28 21:58:24 +0200867 LIST_HEAD(datalist);
868 LIST_HEAD(datalist2);
Stephan Mueller541af942014-05-31 15:44:17 +0200869 unsigned char *V = drbg->scratchpad;
870 unsigned char prefix = DRBG_PREFIX1;
871
Stephan Mueller541af942014-05-31 15:44:17 +0200872 if (!seed)
873 return -EINVAL;
874
875 if (reseed) {
876 /* 10.1.1.3 step 1 */
877 memcpy(V, drbg->V, drbg_statelen(drbg));
878 drbg_string_fill(&data1, &prefix, 1);
Stephan Mueller8c987162014-06-28 21:58:24 +0200879 list_add_tail(&data1.list, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200880 drbg_string_fill(&data2, V, drbg_statelen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +0200881 list_add_tail(&data2.list, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200882 }
Stephan Mueller8c987162014-06-28 21:58:24 +0200883 list_splice_tail(seed, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200884
885 /* 10.1.1.2 / 10.1.1.3 step 2 and 3 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200886 ret = drbg_hash_df(drbg, drbg->V, drbg_statelen(drbg), &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200887 if (ret)
888 goto out;
889
890 /* 10.1.1.2 / 10.1.1.3 step 4 */
891 prefix = DRBG_PREFIX0;
892 drbg_string_fill(&data1, &prefix, 1);
Stephan Mueller8c987162014-06-28 21:58:24 +0200893 list_add_tail(&data1.list, &datalist2);
Stephan Mueller541af942014-05-31 15:44:17 +0200894 drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +0200895 list_add_tail(&data2.list, &datalist2);
Stephan Mueller541af942014-05-31 15:44:17 +0200896 /* 10.1.1.2 / 10.1.1.3 step 4 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200897 ret = drbg_hash_df(drbg, drbg->C, drbg_statelen(drbg), &datalist2);
Stephan Mueller541af942014-05-31 15:44:17 +0200898
899out:
Herbert Xu1471f092015-01-05 10:44:09 +1100900 memset(drbg->scratchpad, 0, drbg_statelen(drbg));
Stephan Mueller541af942014-05-31 15:44:17 +0200901 return ret;
902}
903
904/* processing of additional information string for Hash DRBG */
905static int drbg_hash_process_addtl(struct drbg_state *drbg,
Stephan Mueller27e4de22014-07-06 02:25:36 +0200906 struct list_head *addtl)
Stephan Mueller541af942014-05-31 15:44:17 +0200907{
908 int ret = 0;
909 struct drbg_string data1, data2;
Stephan Mueller8c987162014-06-28 21:58:24 +0200910 LIST_HEAD(datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200911 unsigned char prefix = DRBG_PREFIX2;
912
Stephan Mueller541af942014-05-31 15:44:17 +0200913 /* 10.1.1.4 step 2 */
Stephan Mueller27e4de22014-07-06 02:25:36 +0200914 if (!addtl || list_empty(addtl))
Stephan Mueller541af942014-05-31 15:44:17 +0200915 return 0;
916
917 /* 10.1.1.4 step 2a */
918 drbg_string_fill(&data1, &prefix, 1);
919 drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +0200920 list_add_tail(&data1.list, &datalist);
921 list_add_tail(&data2.list, &datalist);
Stephan Mueller27e4de22014-07-06 02:25:36 +0200922 list_splice_tail(addtl, &datalist);
Stephan Mueller8c987162014-06-28 21:58:24 +0200923 ret = drbg_kcapi_hash(drbg, NULL, drbg->scratchpad, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200924 if (ret)
925 goto out;
926
927 /* 10.1.1.4 step 2b */
928 drbg_add_buf(drbg->V, drbg_statelen(drbg),
929 drbg->scratchpad, drbg_blocklen(drbg));
930
931out:
Herbert Xu1471f092015-01-05 10:44:09 +1100932 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
Stephan Mueller541af942014-05-31 15:44:17 +0200933 return ret;
934}
935
936/* Hashgen defined in 10.1.1.4 */
937static int drbg_hash_hashgen(struct drbg_state *drbg,
938 unsigned char *buf,
939 unsigned int buflen)
940{
941 int len = 0;
942 int ret = 0;
943 unsigned char *src = drbg->scratchpad;
944 unsigned char *dst = drbg->scratchpad + drbg_statelen(drbg);
945 struct drbg_string data;
Stephan Mueller8c987162014-06-28 21:58:24 +0200946 LIST_HEAD(datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200947
Stephan Mueller541af942014-05-31 15:44:17 +0200948 /* 10.1.1.4 step hashgen 2 */
949 memcpy(src, drbg->V, drbg_statelen(drbg));
950
951 drbg_string_fill(&data, src, drbg_statelen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +0200952 list_add_tail(&data.list, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200953 while (len < buflen) {
954 unsigned int outlen = 0;
955 /* 10.1.1.4 step hashgen 4.1 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200956 ret = drbg_kcapi_hash(drbg, NULL, dst, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200957 if (ret) {
958 len = ret;
959 goto out;
960 }
961 outlen = (drbg_blocklen(drbg) < (buflen - len)) ?
962 drbg_blocklen(drbg) : (buflen - len);
963 if (!drbg_fips_continuous_test(drbg, dst)) {
Stephan Mueller41a84982014-10-14 21:50:13 +0200964 crypto_inc(src, drbg_statelen(drbg));
Stephan Mueller541af942014-05-31 15:44:17 +0200965 continue;
966 }
967 /* 10.1.1.4 step hashgen 4.2 */
968 memcpy(buf + len, dst, outlen);
969 len += outlen;
970 /* 10.1.1.4 hashgen step 4.3 */
971 if (len < buflen)
Stephan Mueller41a84982014-10-14 21:50:13 +0200972 crypto_inc(src, drbg_statelen(drbg));
Stephan Mueller541af942014-05-31 15:44:17 +0200973 }
974
975out:
Herbert Xu1471f092015-01-05 10:44:09 +1100976 memset(drbg->scratchpad, 0,
Stephan Mueller541af942014-05-31 15:44:17 +0200977 (drbg_statelen(drbg) + drbg_blocklen(drbg)));
978 return len;
979}
980
981/* generate function for Hash DRBG as defined in 10.1.1.4 */
982static int drbg_hash_generate(struct drbg_state *drbg,
983 unsigned char *buf, unsigned int buflen,
Stephan Mueller27e4de22014-07-06 02:25:36 +0200984 struct list_head *addtl)
Stephan Mueller541af942014-05-31 15:44:17 +0200985{
986 int len = 0;
987 int ret = 0;
Stephan Mueller72f3e002014-08-17 17:37:34 +0200988 union {
989 unsigned char req[8];
Stephan Mueller7c8ae032014-08-26 09:32:24 +0200990 __be64 req_int;
Stephan Mueller72f3e002014-08-17 17:37:34 +0200991 } u;
Stephan Mueller541af942014-05-31 15:44:17 +0200992 unsigned char prefix = DRBG_PREFIX3;
993 struct drbg_string data1, data2;
Stephan Mueller8c987162014-06-28 21:58:24 +0200994 LIST_HEAD(datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200995
996 /* 10.1.1.4 step 2 */
997 ret = drbg_hash_process_addtl(drbg, addtl);
998 if (ret)
999 return ret;
1000 /* 10.1.1.4 step 3 */
1001 len = drbg_hash_hashgen(drbg, buf, buflen);
1002
1003 /* this is the value H as documented in 10.1.1.4 */
Stephan Mueller541af942014-05-31 15:44:17 +02001004 /* 10.1.1.4 step 4 */
1005 drbg_string_fill(&data1, &prefix, 1);
Stephan Mueller8c987162014-06-28 21:58:24 +02001006 list_add_tail(&data1.list, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +02001007 drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +02001008 list_add_tail(&data2.list, &datalist);
1009 ret = drbg_kcapi_hash(drbg, NULL, drbg->scratchpad, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +02001010 if (ret) {
1011 len = ret;
1012 goto out;
1013 }
1014
1015 /* 10.1.1.4 step 5 */
1016 drbg_add_buf(drbg->V, drbg_statelen(drbg),
1017 drbg->scratchpad, drbg_blocklen(drbg));
1018 drbg_add_buf(drbg->V, drbg_statelen(drbg),
1019 drbg->C, drbg_statelen(drbg));
Stephan Mueller72f3e002014-08-17 17:37:34 +02001020 u.req_int = cpu_to_be64(drbg->reseed_ctr);
1021 drbg_add_buf(drbg->V, drbg_statelen(drbg), u.req, 8);
Stephan Mueller541af942014-05-31 15:44:17 +02001022
1023out:
Herbert Xu1471f092015-01-05 10:44:09 +11001024 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
Stephan Mueller541af942014-05-31 15:44:17 +02001025 return len;
1026}
1027
1028/*
1029 * scratchpad usage: as update and generate are used isolated, both
1030 * can use the scratchpad
1031 */
1032static struct drbg_state_ops drbg_hash_ops = {
1033 .update = drbg_hash_update,
1034 .generate = drbg_hash_generate,
1035 .crypto_init = drbg_init_hash_kernel,
1036 .crypto_fini = drbg_fini_hash_kernel,
1037};
1038#endif /* CONFIG_CRYPTO_DRBG_HASH */
1039
1040/******************************************************************
1041 * Functions common for DRBG implementations
1042 ******************************************************************/
1043
Stephan Mueller3d6a5f72015-05-25 15:09:14 +02001044static inline int __drbg_seed(struct drbg_state *drbg, struct list_head *seed,
1045 int reseed)
1046{
1047 int ret = drbg->d_ops->update(drbg, seed, reseed);
1048
1049 if (ret)
1050 return ret;
1051
1052 drbg->seeded = true;
1053 /* 10.1.1.2 / 10.1.1.3 step 5 */
1054 drbg->reseed_ctr = 1;
1055
1056 return ret;
1057}
1058
Stephan Mueller4c787992015-05-25 15:09:36 +02001059static void drbg_async_seed(struct work_struct *work)
1060{
1061 struct drbg_string data;
1062 LIST_HEAD(seedlist);
1063 struct drbg_state *drbg = container_of(work, struct drbg_state,
1064 seed_work);
1065
1066 get_blocking_random_bytes(drbg->seed_buf, drbg->seed_buf_len);
1067
1068 drbg_string_fill(&data, drbg->seed_buf, drbg->seed_buf_len);
1069 list_add_tail(&data.list, &seedlist);
1070 mutex_lock(&drbg->drbg_mutex);
1071 __drbg_seed(drbg, &seedlist, true);
1072 memzero_explicit(drbg->seed_buf, drbg->seed_buf_len);
1073 mutex_unlock(&drbg->drbg_mutex);
1074}
1075
Stephan Mueller541af942014-05-31 15:44:17 +02001076/*
1077 * Seeding or reseeding of the DRBG
1078 *
1079 * @drbg: DRBG state struct
1080 * @pers: personalization / additional information buffer
1081 * @reseed: 0 for initial seed process, 1 for reseeding
1082 *
1083 * return:
1084 * 0 on success
1085 * error value otherwise
1086 */
1087static int drbg_seed(struct drbg_state *drbg, struct drbg_string *pers,
1088 bool reseed)
1089{
1090 int ret = 0;
Stephan Mueller541af942014-05-31 15:44:17 +02001091 struct drbg_string data1;
Stephan Mueller8c987162014-06-28 21:58:24 +02001092 LIST_HEAD(seedlist);
Stephan Mueller541af942014-05-31 15:44:17 +02001093
1094 /* 9.1 / 9.2 / 9.3.1 step 3 */
1095 if (pers && pers->len > (drbg_max_addtl(drbg))) {
Stephan Muellera9089572014-07-06 02:24:03 +02001096 pr_devel("DRBG: personalization string too long %zu\n",
Stephan Mueller541af942014-05-31 15:44:17 +02001097 pers->len);
1098 return -EINVAL;
1099 }
1100
Herbert Xu8fded592015-04-21 10:46:41 +08001101 if (list_empty(&drbg->test_data.list)) {
1102 drbg_string_fill(&data1, drbg->test_data.buf,
1103 drbg->test_data.len);
Stephan Mueller541af942014-05-31 15:44:17 +02001104 pr_devel("DRBG: using test entropy\n");
1105 } else {
Stephan Mueller541af942014-05-31 15:44:17 +02001106 pr_devel("DRBG: (re)seeding with %zu bytes of entropy\n",
Stephan Mueller3d6a5f72015-05-25 15:09:14 +02001107 drbg->seed_buf_len);
1108 get_random_bytes(drbg->seed_buf, drbg->seed_buf_len);
1109 drbg_string_fill(&data1, drbg->seed_buf, drbg->seed_buf_len);
Stephan Mueller541af942014-05-31 15:44:17 +02001110 }
Stephan Mueller8c987162014-06-28 21:58:24 +02001111 list_add_tail(&data1.list, &seedlist);
Stephan Mueller541af942014-05-31 15:44:17 +02001112
1113 /*
1114 * concatenation of entropy with personalization str / addtl input)
1115 * the variable pers is directly handed in by the caller, so check its
1116 * contents whether it is appropriate
1117 */
Stephan Mueller8c987162014-06-28 21:58:24 +02001118 if (pers && pers->buf && 0 < pers->len) {
1119 list_add_tail(&pers->list, &seedlist);
Stephan Mueller541af942014-05-31 15:44:17 +02001120 pr_devel("DRBG: using personalization string\n");
1121 }
1122
Stephan Muellere6c02442014-08-17 17:39:31 +02001123 if (!reseed) {
1124 memset(drbg->V, 0, drbg_statelen(drbg));
1125 memset(drbg->C, 0, drbg_statelen(drbg));
1126 }
1127
Stephan Mueller3d6a5f72015-05-25 15:09:14 +02001128 ret = __drbg_seed(drbg, &seedlist, reseed);
1129
1130 /*
1131 * Clear the initial entropy buffer as the async call may not overwrite
1132 * that buffer for quite some time.
1133 */
1134 memzero_explicit(drbg->seed_buf, drbg->seed_buf_len);
Stephan Mueller541af942014-05-31 15:44:17 +02001135 if (ret)
1136 goto out;
Stephan Mueller3d6a5f72015-05-25 15:09:14 +02001137 /*
1138 * For all subsequent seeding calls, we only need the seed buffer
1139 * equal to the security strength of the DRBG. We undo the calculation
1140 * in drbg_alloc_state.
1141 */
1142 if (!reseed)
1143 drbg->seed_buf_len = drbg->seed_buf_len / 3 * 2;
Stephan Mueller541af942014-05-31 15:44:17 +02001144
Stephan Mueller4c787992015-05-25 15:09:36 +02001145 /* Invoke asynchronous seeding unless DRBG is in test mode. */
1146 if (!list_empty(&drbg->test_data.list) && !reseed)
1147 schedule_work(&drbg->seed_work);
1148
Stephan Mueller541af942014-05-31 15:44:17 +02001149out:
Stephan Mueller541af942014-05-31 15:44:17 +02001150 return ret;
1151}
1152
1153/* Free all substructures in a DRBG state without the DRBG state structure */
1154static inline void drbg_dealloc_state(struct drbg_state *drbg)
1155{
1156 if (!drbg)
1157 return;
Stephan Mueller46f64f62014-08-17 17:37:59 +02001158 kzfree(drbg->V);
Stephan Mueller541af942014-05-31 15:44:17 +02001159 drbg->V = NULL;
Stephan Mueller46f64f62014-08-17 17:37:59 +02001160 kzfree(drbg->C);
Stephan Mueller541af942014-05-31 15:44:17 +02001161 drbg->C = NULL;
Stephan Mueller46f64f62014-08-17 17:37:59 +02001162 kzfree(drbg->scratchpad);
Stephan Mueller541af942014-05-31 15:44:17 +02001163 drbg->scratchpad = NULL;
1164 drbg->reseed_ctr = 0;
Herbert Xu2a57e422015-04-20 11:29:15 +08001165 drbg->d_ops = NULL;
1166 drbg->core = NULL;
Stephan Mueller541af942014-05-31 15:44:17 +02001167#ifdef CONFIG_CRYPTO_FIPS
Stephan Mueller46f64f62014-08-17 17:37:59 +02001168 kzfree(drbg->prev);
Stephan Mueller541af942014-05-31 15:44:17 +02001169 drbg->prev = NULL;
1170 drbg->fips_primed = false;
1171#endif
Stephan Mueller3d6a5f72015-05-25 15:09:14 +02001172 kzfree(drbg->seed_buf);
1173 drbg->seed_buf = NULL;
Stephan Mueller541af942014-05-31 15:44:17 +02001174}
1175
1176/*
1177 * Allocate all sub-structures for a DRBG state.
1178 * The DRBG state structure must already be allocated.
1179 */
1180static inline int drbg_alloc_state(struct drbg_state *drbg)
1181{
1182 int ret = -ENOMEM;
1183 unsigned int sb_size = 0;
1184
Herbert Xu2a57e422015-04-20 11:29:15 +08001185 switch (drbg->core->flags & DRBG_TYPE_MASK) {
1186#ifdef CONFIG_CRYPTO_DRBG_HMAC
1187 case DRBG_HMAC:
1188 drbg->d_ops = &drbg_hmac_ops;
1189 break;
1190#endif /* CONFIG_CRYPTO_DRBG_HMAC */
1191#ifdef CONFIG_CRYPTO_DRBG_HASH
1192 case DRBG_HASH:
1193 drbg->d_ops = &drbg_hash_ops;
1194 break;
1195#endif /* CONFIG_CRYPTO_DRBG_HASH */
1196#ifdef CONFIG_CRYPTO_DRBG_CTR
1197 case DRBG_CTR:
1198 drbg->d_ops = &drbg_ctr_ops;
1199 break;
1200#endif /* CONFIG_CRYPTO_DRBG_CTR */
1201 default:
1202 ret = -EOPNOTSUPP;
1203 goto err;
1204 }
1205
Stephan Muellere6c02442014-08-17 17:39:31 +02001206 drbg->V = kmalloc(drbg_statelen(drbg), GFP_KERNEL);
Stephan Mueller541af942014-05-31 15:44:17 +02001207 if (!drbg->V)
1208 goto err;
Stephan Muellere6c02442014-08-17 17:39:31 +02001209 drbg->C = kmalloc(drbg_statelen(drbg), GFP_KERNEL);
Stephan Mueller541af942014-05-31 15:44:17 +02001210 if (!drbg->C)
1211 goto err;
1212#ifdef CONFIG_CRYPTO_FIPS
Stephan Muellere6c02442014-08-17 17:39:31 +02001213 drbg->prev = kmalloc(drbg_blocklen(drbg), GFP_KERNEL);
Stephan Mueller541af942014-05-31 15:44:17 +02001214 if (!drbg->prev)
1215 goto err;
1216 drbg->fips_primed = false;
1217#endif
1218 /* scratchpad is only generated for CTR and Hash */
1219 if (drbg->core->flags & DRBG_HMAC)
1220 sb_size = 0;
1221 else if (drbg->core->flags & DRBG_CTR)
1222 sb_size = drbg_statelen(drbg) + drbg_blocklen(drbg) + /* temp */
1223 drbg_statelen(drbg) + /* df_data */
1224 drbg_blocklen(drbg) + /* pad */
1225 drbg_blocklen(drbg) + /* iv */
Stephan Mueller8fecaad2014-07-01 17:08:48 +02001226 drbg_statelen(drbg) + drbg_blocklen(drbg); /* temp */
Stephan Mueller541af942014-05-31 15:44:17 +02001227 else
1228 sb_size = drbg_statelen(drbg) + drbg_blocklen(drbg);
1229
1230 if (0 < sb_size) {
1231 drbg->scratchpad = kzalloc(sb_size, GFP_KERNEL);
1232 if (!drbg->scratchpad)
1233 goto err;
1234 }
Stephan Mueller3d6a5f72015-05-25 15:09:14 +02001235
1236 /*
1237 * Gather entropy equal to the security strength of the DRBG.
1238 * With a derivation function, a nonce is required in addition
1239 * to the entropy. A nonce must be at least 1/2 of the security
1240 * strength of the DRBG in size. Thus, entropy * nonce is 3/2
1241 * of the strength. The consideration of a nonce is only
1242 * applicable during initial seeding.
1243 */
1244 drbg->seed_buf_len = drbg_sec_strength(drbg->core->flags);
1245 if (!drbg->seed_buf_len) {
1246 ret = -EFAULT;
1247 goto err;
1248 }
1249 /* ensure we have sufficient buffer space for initial seed */
1250 drbg->seed_buf_len = ((drbg->seed_buf_len + 1) / 2) * 3;
1251 drbg->seed_buf = kzalloc(drbg->seed_buf_len, GFP_KERNEL);
1252 if (!drbg->seed_buf)
1253 goto err;
1254
Stephan Mueller4c787992015-05-25 15:09:36 +02001255 INIT_WORK(&drbg->seed_work, drbg_async_seed);
1256
Stephan Mueller541af942014-05-31 15:44:17 +02001257 return 0;
1258
1259err:
1260 drbg_dealloc_state(drbg);
1261 return ret;
1262}
1263
Stephan Mueller541af942014-05-31 15:44:17 +02001264/*************************************************************************
1265 * DRBG interface functions
1266 *************************************************************************/
1267
1268/*
1269 * DRBG generate function as required by SP800-90A - this function
1270 * generates random numbers
1271 *
1272 * @drbg DRBG state handle
1273 * @buf Buffer where to store the random numbers -- the buffer must already
1274 * be pre-allocated by caller
1275 * @buflen Length of output buffer - this value defines the number of random
1276 * bytes pulled from DRBG
1277 * @addtl Additional input that is mixed into state, may be NULL -- note
1278 * the entropy is pulled by the DRBG internally unconditionally
1279 * as defined in SP800-90A. The additional input is mixed into
1280 * the state in addition to the pulled entropy.
1281 *
Stephan Muellercde001e2015-03-06 08:26:31 +01001282 * return: 0 when all bytes are generated; < 0 in case of an error
Stephan Mueller541af942014-05-31 15:44:17 +02001283 */
1284static int drbg_generate(struct drbg_state *drbg,
1285 unsigned char *buf, unsigned int buflen,
1286 struct drbg_string *addtl)
1287{
1288 int len = 0;
Stephan Mueller27e4de22014-07-06 02:25:36 +02001289 LIST_HEAD(addtllist);
Stephan Mueller541af942014-05-31 15:44:17 +02001290
Herbert Xu2a57e422015-04-20 11:29:15 +08001291 if (!drbg->core) {
1292 pr_devel("DRBG: not yet seeded\n");
1293 return -EINVAL;
1294 }
Stephan Mueller541af942014-05-31 15:44:17 +02001295 if (0 == buflen || !buf) {
1296 pr_devel("DRBG: no output buffer provided\n");
1297 return -EINVAL;
1298 }
1299 if (addtl && NULL == addtl->buf && 0 < addtl->len) {
1300 pr_devel("DRBG: wrong format of additional information\n");
1301 return -EINVAL;
1302 }
1303
Stephan Mueller541af942014-05-31 15:44:17 +02001304 /* 9.3.1 step 2 */
1305 len = -EINVAL;
Stephan Mueller76899a42015-04-18 19:36:17 +02001306 if (buflen > (drbg_max_request_bytes(drbg))) {
Stephan Mueller541af942014-05-31 15:44:17 +02001307 pr_devel("DRBG: requested random numbers too large %u\n",
1308 buflen);
1309 goto err;
1310 }
1311
1312 /* 9.3.1 step 3 is implicit with the chosen DRBG */
1313
1314 /* 9.3.1 step 4 */
Stephan Mueller76899a42015-04-18 19:36:17 +02001315 if (addtl && addtl->len > (drbg_max_addtl(drbg))) {
Stephan Mueller541af942014-05-31 15:44:17 +02001316 pr_devel("DRBG: additional information string too long %zu\n",
1317 addtl->len);
1318 goto err;
1319 }
1320 /* 9.3.1 step 5 is implicit with the chosen DRBG */
1321
1322 /*
1323 * 9.3.1 step 6 and 9 supplemented by 9.3.2 step c is implemented
1324 * here. The spec is a bit convoluted here, we make it simpler.
1325 */
Stephan Mueller76899a42015-04-18 19:36:17 +02001326 if ((drbg_max_requests(drbg)) < drbg->reseed_ctr)
1327 drbg->seeded = false;
Stephan Mueller541af942014-05-31 15:44:17 +02001328
Stephan Mueller76899a42015-04-18 19:36:17 +02001329 if (drbg->pr || !drbg->seeded) {
Stephan Mueller541af942014-05-31 15:44:17 +02001330 pr_devel("DRBG: reseeding before generation (prediction "
1331 "resistance: %s, state %s)\n",
1332 drbg->pr ? "true" : "false",
1333 drbg->seeded ? "seeded" : "unseeded");
1334 /* 9.3.1 steps 7.1 through 7.3 */
Stephan Mueller76899a42015-04-18 19:36:17 +02001335 len = drbg_seed(drbg, addtl, true);
Stephan Mueller541af942014-05-31 15:44:17 +02001336 if (len)
1337 goto err;
1338 /* 9.3.1 step 7.4 */
1339 addtl = NULL;
1340 }
Stephan Mueller27e4de22014-07-06 02:25:36 +02001341
Stephan Mueller27e4de22014-07-06 02:25:36 +02001342 if (addtl && 0 < addtl->len)
1343 list_add_tail(&addtl->list, &addtllist);
Stephan Mueller541af942014-05-31 15:44:17 +02001344 /* 9.3.1 step 8 and 10 */
Stephan Mueller76899a42015-04-18 19:36:17 +02001345 len = drbg->d_ops->generate(drbg, buf, buflen, &addtllist);
Stephan Mueller541af942014-05-31 15:44:17 +02001346
1347 /* 10.1.1.4 step 6, 10.1.2.5 step 7, 10.2.1.5.2 step 7 */
Stephan Mueller76899a42015-04-18 19:36:17 +02001348 drbg->reseed_ctr++;
Stephan Mueller541af942014-05-31 15:44:17 +02001349 if (0 >= len)
1350 goto err;
1351
1352 /*
1353 * Section 11.3.3 requires to re-perform self tests after some
1354 * generated random numbers. The chosen value after which self
1355 * test is performed is arbitrary, but it should be reasonable.
1356 * However, we do not perform the self tests because of the following
1357 * reasons: it is mathematically impossible that the initial self tests
1358 * were successfully and the following are not. If the initial would
1359 * pass and the following would not, the kernel integrity is violated.
1360 * In this case, the entire kernel operation is questionable and it
1361 * is unlikely that the integrity violation only affects the
1362 * correct operation of the DRBG.
1363 *
1364 * Albeit the following code is commented out, it is provided in
1365 * case somebody has a need to implement the test of 11.3.3.
1366 */
1367#if 0
Stephan Mueller76899a42015-04-18 19:36:17 +02001368 if (drbg->reseed_ctr && !(drbg->reseed_ctr % 4096)) {
Stephan Mueller541af942014-05-31 15:44:17 +02001369 int err = 0;
1370 pr_devel("DRBG: start to perform self test\n");
1371 if (drbg->core->flags & DRBG_HMAC)
1372 err = alg_test("drbg_pr_hmac_sha256",
1373 "drbg_pr_hmac_sha256", 0, 0);
1374 else if (drbg->core->flags & DRBG_CTR)
1375 err = alg_test("drbg_pr_ctr_aes128",
1376 "drbg_pr_ctr_aes128", 0, 0);
1377 else
1378 err = alg_test("drbg_pr_sha256",
1379 "drbg_pr_sha256", 0, 0);
1380 if (err) {
1381 pr_err("DRBG: periodical self test failed\n");
1382 /*
1383 * uninstantiate implies that from now on, only errors
1384 * are returned when reusing this DRBG cipher handle
1385 */
1386 drbg_uninstantiate(drbg);
Stephan Mueller541af942014-05-31 15:44:17 +02001387 return 0;
1388 } else {
1389 pr_devel("DRBG: self test successful\n");
1390 }
1391 }
1392#endif
1393
Stephan Muellercde001e2015-03-06 08:26:31 +01001394 /*
1395 * All operations were successful, return 0 as mandated by
1396 * the kernel crypto API interface.
1397 */
1398 len = 0;
Stephan Mueller541af942014-05-31 15:44:17 +02001399err:
Stephan Mueller541af942014-05-31 15:44:17 +02001400 return len;
1401}
1402
1403/*
1404 * Wrapper around drbg_generate which can pull arbitrary long strings
1405 * from the DRBG without hitting the maximum request limitation.
1406 *
1407 * Parameters: see drbg_generate
1408 * Return codes: see drbg_generate -- if one drbg_generate request fails,
1409 * the entire drbg_generate_long request fails
1410 */
1411static int drbg_generate_long(struct drbg_state *drbg,
1412 unsigned char *buf, unsigned int buflen,
1413 struct drbg_string *addtl)
1414{
Stephan Mueller082eb102015-04-18 19:35:45 +02001415 unsigned int len = 0;
Stephan Mueller541af942014-05-31 15:44:17 +02001416 unsigned int slice = 0;
1417 do {
Stephan Mueller082eb102015-04-18 19:35:45 +02001418 int err = 0;
Stephan Mueller541af942014-05-31 15:44:17 +02001419 unsigned int chunk = 0;
1420 slice = ((buflen - len) / drbg_max_request_bytes(drbg));
1421 chunk = slice ? drbg_max_request_bytes(drbg) : (buflen - len);
Stephan Mueller76899a42015-04-18 19:36:17 +02001422 mutex_lock(&drbg->drbg_mutex);
Stephan Mueller082eb102015-04-18 19:35:45 +02001423 err = drbg_generate(drbg, buf + len, chunk, addtl);
Stephan Mueller76899a42015-04-18 19:36:17 +02001424 mutex_unlock(&drbg->drbg_mutex);
Stephan Mueller082eb102015-04-18 19:35:45 +02001425 if (0 > err)
1426 return err;
1427 len += chunk;
Stephan Muellerce5481d2014-07-31 21:47:33 +02001428 } while (slice > 0 && (len < buflen));
Stephan Mueller082eb102015-04-18 19:35:45 +02001429 return 0;
Stephan Mueller541af942014-05-31 15:44:17 +02001430}
1431
1432/*
1433 * DRBG instantiation function as required by SP800-90A - this function
1434 * sets up the DRBG handle, performs the initial seeding and all sanity
1435 * checks required by SP800-90A
1436 *
1437 * @drbg memory of state -- if NULL, new memory is allocated
1438 * @pers Personalization string that is mixed into state, may be NULL -- note
1439 * the entropy is pulled by the DRBG internally unconditionally
1440 * as defined in SP800-90A. The additional input is mixed into
1441 * the state in addition to the pulled entropy.
1442 * @coreref reference to core
1443 * @pr prediction resistance enabled
1444 *
1445 * return
1446 * 0 on success
1447 * error value otherwise
1448 */
1449static int drbg_instantiate(struct drbg_state *drbg, struct drbg_string *pers,
1450 int coreref, bool pr)
1451{
Herbert Xu2a57e422015-04-20 11:29:15 +08001452 int ret;
1453 bool reseed = true;
Stephan Mueller541af942014-05-31 15:44:17 +02001454
1455 pr_devel("DRBG: Initializing DRBG core %d with prediction resistance "
1456 "%s\n", coreref, pr ? "enabled" : "disabled");
Stephan Mueller76899a42015-04-18 19:36:17 +02001457 mutex_lock(&drbg->drbg_mutex);
Stephan Mueller541af942014-05-31 15:44:17 +02001458
1459 /* 9.1 step 1 is implicit with the selected DRBG type */
1460
1461 /*
1462 * 9.1 step 2 is implicit as caller can select prediction resistance
1463 * and the flag is copied into drbg->flags --
1464 * all DRBG types support prediction resistance
1465 */
1466
1467 /* 9.1 step 4 is implicit in drbg_sec_strength */
1468
Herbert Xu2a57e422015-04-20 11:29:15 +08001469 if (!drbg->core) {
1470 drbg->core = &drbg_cores[coreref];
1471 drbg->pr = pr;
1472 drbg->seeded = false;
Stephan Mueller541af942014-05-31 15:44:17 +02001473
Herbert Xu2a57e422015-04-20 11:29:15 +08001474 ret = drbg_alloc_state(drbg);
1475 if (ret)
1476 goto unlock;
1477
1478 ret = -EFAULT;
1479 if (drbg->d_ops->crypto_init(drbg))
1480 goto err;
1481
1482 reseed = false;
1483 }
1484
1485 ret = drbg_seed(drbg, pers, reseed);
1486
1487 if (ret && !reseed) {
Stephan Muellerfa3ae622015-04-18 19:37:00 +02001488 drbg->d_ops->crypto_fini(drbg);
Stephan Mueller541af942014-05-31 15:44:17 +02001489 goto err;
Stephan Muellerfa3ae622015-04-18 19:37:00 +02001490 }
Stephan Mueller541af942014-05-31 15:44:17 +02001491
Stephan Mueller76899a42015-04-18 19:36:17 +02001492 mutex_unlock(&drbg->drbg_mutex);
Herbert Xu2a57e422015-04-20 11:29:15 +08001493 return ret;
Stephan Mueller541af942014-05-31 15:44:17 +02001494
1495err:
1496 drbg_dealloc_state(drbg);
Stephan Mueller76899a42015-04-18 19:36:17 +02001497unlock:
1498 mutex_unlock(&drbg->drbg_mutex);
Stephan Mueller541af942014-05-31 15:44:17 +02001499 return ret;
1500}
1501
1502/*
1503 * DRBG uninstantiate function as required by SP800-90A - this function
1504 * frees all buffers and the DRBG handle
1505 *
1506 * @drbg DRBG state handle
1507 *
1508 * return
1509 * 0 on success
1510 */
1511static int drbg_uninstantiate(struct drbg_state *drbg)
1512{
Stephan Mueller4c787992015-05-25 15:09:36 +02001513 cancel_work_sync(&drbg->seed_work);
Herbert Xu2a57e422015-04-20 11:29:15 +08001514 if (drbg->d_ops)
1515 drbg->d_ops->crypto_fini(drbg);
Stephan Mueller541af942014-05-31 15:44:17 +02001516 drbg_dealloc_state(drbg);
1517 /* no scrubbing of test_data -- this shall survive an uninstantiate */
Stephan Mueller541af942014-05-31 15:44:17 +02001518 return 0;
1519}
1520
1521/*
1522 * Helper function for setting the test data in the DRBG
1523 *
1524 * @drbg DRBG state handle
Herbert Xu8fded592015-04-21 10:46:41 +08001525 * @data test data
1526 * @len test data length
Stephan Mueller541af942014-05-31 15:44:17 +02001527 */
Herbert Xu8fded592015-04-21 10:46:41 +08001528static void drbg_kcapi_set_entropy(struct crypto_rng *tfm,
1529 const u8 *data, unsigned int len)
Stephan Mueller541af942014-05-31 15:44:17 +02001530{
Herbert Xu8fded592015-04-21 10:46:41 +08001531 struct drbg_state *drbg = crypto_rng_ctx(tfm);
1532
1533 mutex_lock(&drbg->drbg_mutex);
1534 drbg_string_fill(&drbg->test_data, data, len);
Stephan Mueller76899a42015-04-18 19:36:17 +02001535 mutex_unlock(&drbg->drbg_mutex);
Stephan Mueller541af942014-05-31 15:44:17 +02001536}
1537
1538/***************************************************************
1539 * Kernel crypto API cipher invocations requested by DRBG
1540 ***************************************************************/
1541
1542#if defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_HMAC)
1543struct sdesc {
1544 struct shash_desc shash;
1545 char ctx[];
1546};
1547
1548static int drbg_init_hash_kernel(struct drbg_state *drbg)
1549{
1550 struct sdesc *sdesc;
1551 struct crypto_shash *tfm;
1552
1553 tfm = crypto_alloc_shash(drbg->core->backend_cra_name, 0, 0);
1554 if (IS_ERR(tfm)) {
1555 pr_info("DRBG: could not allocate digest TFM handle\n");
1556 return PTR_ERR(tfm);
1557 }
1558 BUG_ON(drbg_blocklen(drbg) != crypto_shash_digestsize(tfm));
1559 sdesc = kzalloc(sizeof(struct shash_desc) + crypto_shash_descsize(tfm),
1560 GFP_KERNEL);
1561 if (!sdesc) {
1562 crypto_free_shash(tfm);
1563 return -ENOMEM;
1564 }
1565
1566 sdesc->shash.tfm = tfm;
1567 sdesc->shash.flags = 0;
1568 drbg->priv_data = sdesc;
1569 return 0;
1570}
1571
1572static int drbg_fini_hash_kernel(struct drbg_state *drbg)
1573{
1574 struct sdesc *sdesc = (struct sdesc *)drbg->priv_data;
1575 if (sdesc) {
1576 crypto_free_shash(sdesc->shash.tfm);
1577 kzfree(sdesc);
1578 }
1579 drbg->priv_data = NULL;
1580 return 0;
1581}
1582
1583static int drbg_kcapi_hash(struct drbg_state *drbg, const unsigned char *key,
Stephan Mueller8c987162014-06-28 21:58:24 +02001584 unsigned char *outval, const struct list_head *in)
Stephan Mueller541af942014-05-31 15:44:17 +02001585{
1586 struct sdesc *sdesc = (struct sdesc *)drbg->priv_data;
Stephan Mueller8c987162014-06-28 21:58:24 +02001587 struct drbg_string *input = NULL;
Stephan Mueller541af942014-05-31 15:44:17 +02001588
1589 if (key)
1590 crypto_shash_setkey(sdesc->shash.tfm, key, drbg_statelen(drbg));
1591 crypto_shash_init(&sdesc->shash);
Stephan Mueller8c987162014-06-28 21:58:24 +02001592 list_for_each_entry(input, in, list)
1593 crypto_shash_update(&sdesc->shash, input->buf, input->len);
Stephan Mueller541af942014-05-31 15:44:17 +02001594 return crypto_shash_final(&sdesc->shash, outval);
1595}
1596#endif /* (CONFIG_CRYPTO_DRBG_HASH || CONFIG_CRYPTO_DRBG_HMAC) */
1597
1598#ifdef CONFIG_CRYPTO_DRBG_CTR
1599static int drbg_init_sym_kernel(struct drbg_state *drbg)
1600{
1601 int ret = 0;
Stephan Mueller04bcbfc2015-03-01 20:39:17 +01001602 struct crypto_cipher *tfm;
Stephan Mueller541af942014-05-31 15:44:17 +02001603
Stephan Mueller04bcbfc2015-03-01 20:39:17 +01001604 tfm = crypto_alloc_cipher(drbg->core->backend_cra_name, 0, 0);
Stephan Mueller541af942014-05-31 15:44:17 +02001605 if (IS_ERR(tfm)) {
1606 pr_info("DRBG: could not allocate cipher TFM handle\n");
1607 return PTR_ERR(tfm);
1608 }
Stephan Mueller04bcbfc2015-03-01 20:39:17 +01001609 BUG_ON(drbg_blocklen(drbg) != crypto_cipher_blocksize(tfm));
Stephan Mueller541af942014-05-31 15:44:17 +02001610 drbg->priv_data = tfm;
1611 return ret;
1612}
1613
1614static int drbg_fini_sym_kernel(struct drbg_state *drbg)
1615{
Stephan Mueller04bcbfc2015-03-01 20:39:17 +01001616 struct crypto_cipher *tfm =
1617 (struct crypto_cipher *)drbg->priv_data;
Stephan Mueller541af942014-05-31 15:44:17 +02001618 if (tfm)
Stephan Mueller04bcbfc2015-03-01 20:39:17 +01001619 crypto_free_cipher(tfm);
Stephan Mueller541af942014-05-31 15:44:17 +02001620 drbg->priv_data = NULL;
1621 return 0;
1622}
1623
1624static int drbg_kcapi_sym(struct drbg_state *drbg, const unsigned char *key,
1625 unsigned char *outval, const struct drbg_string *in)
1626{
Stephan Mueller04bcbfc2015-03-01 20:39:17 +01001627 struct crypto_cipher *tfm =
1628 (struct crypto_cipher *)drbg->priv_data;
Stephan Mueller541af942014-05-31 15:44:17 +02001629
Stephan Mueller04bcbfc2015-03-01 20:39:17 +01001630 crypto_cipher_setkey(tfm, key, (drbg_keylen(drbg)));
Stephan Mueller541af942014-05-31 15:44:17 +02001631 /* there is only component in *in */
Stephan Mueller04bcbfc2015-03-01 20:39:17 +01001632 BUG_ON(in->len < drbg_blocklen(drbg));
1633 crypto_cipher_encrypt_one(tfm, outval, in->buf);
1634 return 0;
Stephan Mueller541af942014-05-31 15:44:17 +02001635}
1636#endif /* CONFIG_CRYPTO_DRBG_CTR */
1637
1638/***************************************************************
1639 * Kernel crypto API interface to register DRBG
1640 ***************************************************************/
1641
1642/*
1643 * Look up the DRBG flags by given kernel crypto API cra_name
1644 * The code uses the drbg_cores definition to do this
1645 *
1646 * @cra_name kernel crypto API cra_name
1647 * @coreref reference to integer which is filled with the pointer to
1648 * the applicable core
1649 * @pr reference for setting prediction resistance
1650 *
1651 * return: flags
1652 */
1653static inline void drbg_convert_tfm_core(const char *cra_driver_name,
1654 int *coreref, bool *pr)
1655{
1656 int i = 0;
1657 size_t start = 0;
1658 int len = 0;
1659
1660 *pr = true;
1661 /* disassemble the names */
1662 if (!memcmp(cra_driver_name, "drbg_nopr_", 10)) {
1663 start = 10;
1664 *pr = false;
1665 } else if (!memcmp(cra_driver_name, "drbg_pr_", 8)) {
1666 start = 8;
1667 } else {
1668 return;
1669 }
1670
1671 /* remove the first part */
1672 len = strlen(cra_driver_name) - start;
1673 for (i = 0; ARRAY_SIZE(drbg_cores) > i; i++) {
1674 if (!memcmp(cra_driver_name + start, drbg_cores[i].cra_name,
1675 len)) {
1676 *coreref = i;
1677 return;
1678 }
1679 }
1680}
1681
1682static int drbg_kcapi_init(struct crypto_tfm *tfm)
1683{
1684 struct drbg_state *drbg = crypto_tfm_ctx(tfm);
Stephan Mueller541af942014-05-31 15:44:17 +02001685
Stephan Mueller76899a42015-04-18 19:36:17 +02001686 mutex_init(&drbg->drbg_mutex);
Herbert Xu2a57e422015-04-20 11:29:15 +08001687
1688 return 0;
Stephan Mueller541af942014-05-31 15:44:17 +02001689}
1690
1691static void drbg_kcapi_cleanup(struct crypto_tfm *tfm)
1692{
1693 drbg_uninstantiate(crypto_tfm_ctx(tfm));
1694}
1695
1696/*
1697 * Generate random numbers invoked by the kernel crypto API:
1698 * The API of the kernel crypto API is extended as follows:
1699 *
Herbert Xu8fded592015-04-21 10:46:41 +08001700 * src is additional input supplied to the RNG.
1701 * slen is the length of src.
1702 * dst is the output buffer where random data is to be stored.
1703 * dlen is the length of dst.
Stephan Mueller541af942014-05-31 15:44:17 +02001704 */
Herbert Xu8fded592015-04-21 10:46:41 +08001705static int drbg_kcapi_random(struct crypto_rng *tfm,
1706 const u8 *src, unsigned int slen,
1707 u8 *dst, unsigned int dlen)
Stephan Mueller541af942014-05-31 15:44:17 +02001708{
1709 struct drbg_state *drbg = crypto_rng_ctx(tfm);
Herbert Xu8fded592015-04-21 10:46:41 +08001710 struct drbg_string *addtl = NULL;
1711 struct drbg_string string;
1712
1713 if (slen) {
Stephan Mueller8c987162014-06-28 21:58:24 +02001714 /* linked list variable is now local to allow modification */
Herbert Xu8fded592015-04-21 10:46:41 +08001715 drbg_string_fill(&string, src, slen);
1716 addtl = &string;
Stephan Mueller541af942014-05-31 15:44:17 +02001717 }
Herbert Xu8fded592015-04-21 10:46:41 +08001718
1719 return drbg_generate_long(drbg, dst, dlen, addtl);
Stephan Mueller541af942014-05-31 15:44:17 +02001720}
1721
1722/*
Herbert Xu2a57e422015-04-20 11:29:15 +08001723 * Seed the DRBG invoked by the kernel crypto API
Stephan Mueller541af942014-05-31 15:44:17 +02001724 */
Herbert Xu8fded592015-04-21 10:46:41 +08001725static int drbg_kcapi_seed(struct crypto_rng *tfm,
1726 const u8 *seed, unsigned int slen)
Stephan Mueller541af942014-05-31 15:44:17 +02001727{
1728 struct drbg_state *drbg = crypto_rng_ctx(tfm);
1729 struct crypto_tfm *tfm_base = crypto_rng_tfm(tfm);
1730 bool pr = false;
Herbert Xu8fded592015-04-21 10:46:41 +08001731 struct drbg_string string;
1732 struct drbg_string *seed_string = NULL;
Stephan Mueller541af942014-05-31 15:44:17 +02001733 int coreref = 0;
1734
Stephan Mueller541af942014-05-31 15:44:17 +02001735 drbg_convert_tfm_core(crypto_tfm_alg_driver_name(tfm_base), &coreref,
1736 &pr);
1737 if (0 < slen) {
Herbert Xu8fded592015-04-21 10:46:41 +08001738 drbg_string_fill(&string, seed, slen);
1739 seed_string = &string;
Stephan Mueller541af942014-05-31 15:44:17 +02001740 }
Herbert Xu8fded592015-04-21 10:46:41 +08001741
1742 return drbg_instantiate(drbg, seed_string, coreref, pr);
Stephan Mueller541af942014-05-31 15:44:17 +02001743}
1744
1745/***************************************************************
1746 * Kernel module: code to load the module
1747 ***************************************************************/
1748
1749/*
1750 * Tests as defined in 11.3.2 in addition to the cipher tests: testing
1751 * of the error handling.
1752 *
1753 * Note: testing of failing seed source as defined in 11.3.2 is not applicable
1754 * as seed source of get_random_bytes does not fail.
1755 *
1756 * Note 2: There is no sensible way of testing the reseed counter
1757 * enforcement, so skip it.
1758 */
1759static inline int __init drbg_healthcheck_sanity(void)
1760{
Stephan Mueller541af942014-05-31 15:44:17 +02001761 int len = 0;
1762#define OUTBUFLEN 16
1763 unsigned char buf[OUTBUFLEN];
1764 struct drbg_state *drbg = NULL;
1765 int ret = -EFAULT;
1766 int rc = -EFAULT;
1767 bool pr = false;
1768 int coreref = 0;
1769 struct drbg_string addtl;
1770 size_t max_addtllen, max_request_bytes;
1771
1772 /* only perform test in FIPS mode */
1773 if (!fips_enabled)
1774 return 0;
1775
1776#ifdef CONFIG_CRYPTO_DRBG_CTR
1777 drbg_convert_tfm_core("drbg_nopr_ctr_aes128", &coreref, &pr);
Stephan Muellere25e47e2014-07-06 02:23:03 +02001778#elif defined CONFIG_CRYPTO_DRBG_HASH
Stephan Mueller541af942014-05-31 15:44:17 +02001779 drbg_convert_tfm_core("drbg_nopr_sha256", &coreref, &pr);
1780#else
1781 drbg_convert_tfm_core("drbg_nopr_hmac_sha256", &coreref, &pr);
1782#endif
1783
1784 drbg = kzalloc(sizeof(struct drbg_state), GFP_KERNEL);
1785 if (!drbg)
1786 return -ENOMEM;
1787
Herbert Xue11a7542015-04-20 11:26:48 +08001788 mutex_init(&drbg->drbg_mutex);
1789
Stephan Mueller541af942014-05-31 15:44:17 +02001790 /*
1791 * if the following tests fail, it is likely that there is a buffer
1792 * overflow as buf is much smaller than the requested or provided
1793 * string lengths -- in case the error handling does not succeed
1794 * we may get an OOPS. And we want to get an OOPS as this is a
1795 * grave bug.
1796 */
1797
1798 /* get a valid instance of DRBG for following tests */
1799 ret = drbg_instantiate(drbg, NULL, coreref, pr);
1800 if (ret) {
1801 rc = ret;
1802 goto outbuf;
1803 }
1804 max_addtllen = drbg_max_addtl(drbg);
1805 max_request_bytes = drbg_max_request_bytes(drbg);
1806 drbg_string_fill(&addtl, buf, max_addtllen + 1);
1807 /* overflow addtllen with additonal info string */
1808 len = drbg_generate(drbg, buf, OUTBUFLEN, &addtl);
1809 BUG_ON(0 < len);
1810 /* overflow max_bits */
1811 len = drbg_generate(drbg, buf, (max_request_bytes + 1), NULL);
1812 BUG_ON(0 < len);
1813 drbg_uninstantiate(drbg);
1814
1815 /* overflow max addtllen with personalization string */
1816 ret = drbg_instantiate(drbg, &addtl, coreref, pr);
1817 BUG_ON(0 == ret);
Stephan Mueller541af942014-05-31 15:44:17 +02001818 /* all tests passed */
1819 rc = 0;
1820
1821 pr_devel("DRBG: Sanity tests for failure code paths successfully "
1822 "completed\n");
1823
1824 drbg_uninstantiate(drbg);
1825outbuf:
1826 kzfree(drbg);
1827 return rc;
Stephan Mueller541af942014-05-31 15:44:17 +02001828}
1829
Herbert Xu8fded592015-04-21 10:46:41 +08001830static struct rng_alg drbg_algs[22];
Stephan Mueller541af942014-05-31 15:44:17 +02001831
1832/*
1833 * Fill the array drbg_algs used to register the different DRBGs
1834 * with the kernel crypto API. To fill the array, the information
1835 * from drbg_cores[] is used.
1836 */
Herbert Xu8fded592015-04-21 10:46:41 +08001837static inline void __init drbg_fill_array(struct rng_alg *alg,
Stephan Mueller541af942014-05-31 15:44:17 +02001838 const struct drbg_core *core, int pr)
1839{
1840 int pos = 0;
1841 static int priority = 100;
1842
Herbert Xu8fded592015-04-21 10:46:41 +08001843 memcpy(alg->base.cra_name, "stdrng", 6);
Stephan Mueller541af942014-05-31 15:44:17 +02001844 if (pr) {
Herbert Xu8fded592015-04-21 10:46:41 +08001845 memcpy(alg->base.cra_driver_name, "drbg_pr_", 8);
Stephan Mueller541af942014-05-31 15:44:17 +02001846 pos = 8;
1847 } else {
Herbert Xu8fded592015-04-21 10:46:41 +08001848 memcpy(alg->base.cra_driver_name, "drbg_nopr_", 10);
Stephan Mueller541af942014-05-31 15:44:17 +02001849 pos = 10;
1850 }
Herbert Xu8fded592015-04-21 10:46:41 +08001851 memcpy(alg->base.cra_driver_name + pos, core->cra_name,
Stephan Mueller541af942014-05-31 15:44:17 +02001852 strlen(core->cra_name));
1853
Herbert Xu8fded592015-04-21 10:46:41 +08001854 alg->base.cra_priority = priority;
Stephan Mueller541af942014-05-31 15:44:17 +02001855 priority++;
1856 /*
1857 * If FIPS mode enabled, the selected DRBG shall have the
1858 * highest cra_priority over other stdrng instances to ensure
1859 * it is selected.
1860 */
1861 if (fips_enabled)
Herbert Xu8fded592015-04-21 10:46:41 +08001862 alg->base.cra_priority += 200;
Stephan Mueller541af942014-05-31 15:44:17 +02001863
Herbert Xu8fded592015-04-21 10:46:41 +08001864 alg->base.cra_ctxsize = sizeof(struct drbg_state);
1865 alg->base.cra_module = THIS_MODULE;
1866 alg->base.cra_init = drbg_kcapi_init;
1867 alg->base.cra_exit = drbg_kcapi_cleanup;
1868 alg->generate = drbg_kcapi_random;
1869 alg->seed = drbg_kcapi_seed;
1870 alg->set_ent = drbg_kcapi_set_entropy;
1871 alg->seedsize = 0;
Stephan Mueller541af942014-05-31 15:44:17 +02001872}
1873
1874static int __init drbg_init(void)
1875{
1876 unsigned int i = 0; /* pointer to drbg_algs */
1877 unsigned int j = 0; /* pointer to drbg_cores */
1878 int ret = -EFAULT;
1879
1880 ret = drbg_healthcheck_sanity();
1881 if (ret)
1882 return ret;
1883
1884 if (ARRAY_SIZE(drbg_cores) * 2 > ARRAY_SIZE(drbg_algs)) {
1885 pr_info("DRBG: Cannot register all DRBG types"
Stephan Muellera9089572014-07-06 02:24:03 +02001886 "(slots needed: %zu, slots available: %zu)\n",
Stephan Mueller541af942014-05-31 15:44:17 +02001887 ARRAY_SIZE(drbg_cores) * 2, ARRAY_SIZE(drbg_algs));
1888 return ret;
1889 }
1890
1891 /*
1892 * each DRBG definition can be used with PR and without PR, thus
1893 * we instantiate each DRBG in drbg_cores[] twice.
1894 *
1895 * As the order of placing them into the drbg_algs array matters
1896 * (the later DRBGs receive a higher cra_priority) we register the
1897 * prediction resistance DRBGs first as the should not be too
1898 * interesting.
1899 */
1900 for (j = 0; ARRAY_SIZE(drbg_cores) > j; j++, i++)
1901 drbg_fill_array(&drbg_algs[i], &drbg_cores[j], 1);
1902 for (j = 0; ARRAY_SIZE(drbg_cores) > j; j++, i++)
1903 drbg_fill_array(&drbg_algs[i], &drbg_cores[j], 0);
Herbert Xu8fded592015-04-21 10:46:41 +08001904 return crypto_register_rngs(drbg_algs, (ARRAY_SIZE(drbg_cores) * 2));
Stephan Mueller541af942014-05-31 15:44:17 +02001905}
1906
Fengguang Wu96956ae2014-07-10 16:52:04 +08001907static void __exit drbg_exit(void)
Stephan Mueller541af942014-05-31 15:44:17 +02001908{
Herbert Xu8fded592015-04-21 10:46:41 +08001909 crypto_unregister_rngs(drbg_algs, (ARRAY_SIZE(drbg_cores) * 2));
Stephan Mueller541af942014-05-31 15:44:17 +02001910}
1911
1912module_init(drbg_init);
1913module_exit(drbg_exit);
Stephan Muellere25e47e2014-07-06 02:23:03 +02001914#ifndef CRYPTO_DRBG_HASH_STRING
1915#define CRYPTO_DRBG_HASH_STRING ""
1916#endif
1917#ifndef CRYPTO_DRBG_HMAC_STRING
1918#define CRYPTO_DRBG_HMAC_STRING ""
1919#endif
1920#ifndef CRYPTO_DRBG_CTR_STRING
1921#define CRYPTO_DRBG_CTR_STRING ""
1922#endif
Stephan Mueller541af942014-05-31 15:44:17 +02001923MODULE_LICENSE("GPL");
1924MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
Stephan Muellere25e47e2014-07-06 02:23:03 +02001925MODULE_DESCRIPTION("NIST SP800-90A Deterministic Random Bit Generator (DRBG) "
1926 "using following cores: "
1927 CRYPTO_DRBG_HASH_STRING
1928 CRYPTO_DRBG_HMAC_STRING
1929 CRYPTO_DRBG_CTR_STRING);