]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - crypto/drbg.c
crypto: drbg - use CTR AES instead of ECB AES
[mirror_ubuntu-bionic-kernel.git] / crypto / drbg.c
1 /*
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 #include <linux/kernel.h>
102
103 /***************************************************************
104 * Backend cipher definitions available to DRBG
105 ***************************************************************/
106
107 /*
108 * The order of the DRBG definitions here matter: every DRBG is registered
109 * as stdrng. Each DRBG receives an increasing cra_priority values the later
110 * they are defined in this array (see drbg_fill_array).
111 *
112 * HMAC DRBGs are favored over Hash DRBGs over CTR DRBGs, and
113 * the SHA256 / AES 256 over other ciphers. Thus, the favored
114 * DRBGs are the latest entries in this array.
115 */
116 static const struct drbg_core drbg_cores[] = {
117 #ifdef CONFIG_CRYPTO_DRBG_CTR
118 {
119 .flags = DRBG_CTR | DRBG_STRENGTH128,
120 .statelen = 32, /* 256 bits as defined in 10.2.1 */
121 .blocklen_bytes = 16,
122 .cra_name = "ctr_aes128",
123 .backend_cra_name = "aes",
124 }, {
125 .flags = DRBG_CTR | DRBG_STRENGTH192,
126 .statelen = 40, /* 320 bits as defined in 10.2.1 */
127 .blocklen_bytes = 16,
128 .cra_name = "ctr_aes192",
129 .backend_cra_name = "aes",
130 }, {
131 .flags = DRBG_CTR | DRBG_STRENGTH256,
132 .statelen = 48, /* 384 bits as defined in 10.2.1 */
133 .blocklen_bytes = 16,
134 .cra_name = "ctr_aes256",
135 .backend_cra_name = "aes",
136 },
137 #endif /* CONFIG_CRYPTO_DRBG_CTR */
138 #ifdef CONFIG_CRYPTO_DRBG_HASH
139 {
140 .flags = DRBG_HASH | DRBG_STRENGTH128,
141 .statelen = 55, /* 440 bits */
142 .blocklen_bytes = 20,
143 .cra_name = "sha1",
144 .backend_cra_name = "sha1",
145 }, {
146 .flags = DRBG_HASH | DRBG_STRENGTH256,
147 .statelen = 111, /* 888 bits */
148 .blocklen_bytes = 48,
149 .cra_name = "sha384",
150 .backend_cra_name = "sha384",
151 }, {
152 .flags = DRBG_HASH | DRBG_STRENGTH256,
153 .statelen = 111, /* 888 bits */
154 .blocklen_bytes = 64,
155 .cra_name = "sha512",
156 .backend_cra_name = "sha512",
157 }, {
158 .flags = DRBG_HASH | DRBG_STRENGTH256,
159 .statelen = 55, /* 440 bits */
160 .blocklen_bytes = 32,
161 .cra_name = "sha256",
162 .backend_cra_name = "sha256",
163 },
164 #endif /* CONFIG_CRYPTO_DRBG_HASH */
165 #ifdef CONFIG_CRYPTO_DRBG_HMAC
166 {
167 .flags = DRBG_HMAC | DRBG_STRENGTH128,
168 .statelen = 20, /* block length of cipher */
169 .blocklen_bytes = 20,
170 .cra_name = "hmac_sha1",
171 .backend_cra_name = "hmac(sha1)",
172 }, {
173 .flags = DRBG_HMAC | DRBG_STRENGTH256,
174 .statelen = 48, /* block length of cipher */
175 .blocklen_bytes = 48,
176 .cra_name = "hmac_sha384",
177 .backend_cra_name = "hmac(sha384)",
178 }, {
179 .flags = DRBG_HMAC | DRBG_STRENGTH256,
180 .statelen = 64, /* block length of cipher */
181 .blocklen_bytes = 64,
182 .cra_name = "hmac_sha512",
183 .backend_cra_name = "hmac(sha512)",
184 }, {
185 .flags = DRBG_HMAC | DRBG_STRENGTH256,
186 .statelen = 32, /* block length of cipher */
187 .blocklen_bytes = 32,
188 .cra_name = "hmac_sha256",
189 .backend_cra_name = "hmac(sha256)",
190 },
191 #endif /* CONFIG_CRYPTO_DRBG_HMAC */
192 };
193
194 static int drbg_uninstantiate(struct drbg_state *drbg);
195
196 /******************************************************************
197 * Generic helper functions
198 ******************************************************************/
199
200 /*
201 * Return strength of DRBG according to SP800-90A section 8.4
202 *
203 * @flags DRBG flags reference
204 *
205 * Return: normalized strength in *bytes* value or 32 as default
206 * to counter programming errors
207 */
208 static inline unsigned short drbg_sec_strength(drbg_flag_t flags)
209 {
210 switch (flags & DRBG_STRENGTH_MASK) {
211 case DRBG_STRENGTH128:
212 return 16;
213 case DRBG_STRENGTH192:
214 return 24;
215 case DRBG_STRENGTH256:
216 return 32;
217 default:
218 return 32;
219 }
220 }
221
222 /*
223 * Convert an integer into a byte representation of this integer.
224 * The byte representation is big-endian
225 *
226 * @val value to be converted
227 * @buf buffer holding the converted integer -- caller must ensure that
228 * buffer size is at least 32 bit
229 */
230 #if (defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_CTR))
231 static inline void drbg_cpu_to_be32(__u32 val, unsigned char *buf)
232 {
233 struct s {
234 __be32 conv;
235 };
236 struct s *conversion = (struct s *) buf;
237
238 conversion->conv = cpu_to_be32(val);
239 }
240 #endif /* defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_CTR) */
241
242 /******************************************************************
243 * CTR DRBG callback functions
244 ******************************************************************/
245
246 #ifdef CONFIG_CRYPTO_DRBG_CTR
247 #define CRYPTO_DRBG_CTR_STRING "CTR "
248 MODULE_ALIAS_CRYPTO("drbg_pr_ctr_aes256");
249 MODULE_ALIAS_CRYPTO("drbg_nopr_ctr_aes256");
250 MODULE_ALIAS_CRYPTO("drbg_pr_ctr_aes192");
251 MODULE_ALIAS_CRYPTO("drbg_nopr_ctr_aes192");
252 MODULE_ALIAS_CRYPTO("drbg_pr_ctr_aes128");
253 MODULE_ALIAS_CRYPTO("drbg_nopr_ctr_aes128");
254
255 static void drbg_kcapi_symsetkey(struct drbg_state *drbg,
256 const unsigned char *key);
257 static int drbg_kcapi_sym(struct drbg_state *drbg, unsigned char *outval,
258 const struct drbg_string *in);
259 static int drbg_init_sym_kernel(struct drbg_state *drbg);
260 static int drbg_fini_sym_kernel(struct drbg_state *drbg);
261 static int drbg_kcapi_sym_ctr(struct drbg_state *drbg, u8 *outbuf, u32 outlen);
262
263 /* BCC function for CTR DRBG as defined in 10.4.3 */
264 static int drbg_ctr_bcc(struct drbg_state *drbg,
265 unsigned char *out, const unsigned char *key,
266 struct list_head *in)
267 {
268 int ret = 0;
269 struct drbg_string *curr = NULL;
270 struct drbg_string data;
271 short cnt = 0;
272
273 drbg_string_fill(&data, out, drbg_blocklen(drbg));
274
275 /* 10.4.3 step 2 / 4 */
276 drbg_kcapi_symsetkey(drbg, key);
277 list_for_each_entry(curr, in, list) {
278 const unsigned char *pos = curr->buf;
279 size_t len = curr->len;
280 /* 10.4.3 step 4.1 */
281 while (len) {
282 /* 10.4.3 step 4.2 */
283 if (drbg_blocklen(drbg) == cnt) {
284 cnt = 0;
285 ret = drbg_kcapi_sym(drbg, out, &data);
286 if (ret)
287 return ret;
288 }
289 out[cnt] ^= *pos;
290 pos++;
291 cnt++;
292 len--;
293 }
294 }
295 /* 10.4.3 step 4.2 for last block */
296 if (cnt)
297 ret = drbg_kcapi_sym(drbg, out, &data);
298
299 return ret;
300 }
301
302 /*
303 * scratchpad usage: drbg_ctr_update is interlinked with drbg_ctr_df
304 * (and drbg_ctr_bcc, but this function does not need any temporary buffers),
305 * the scratchpad is used as follows:
306 * drbg_ctr_update:
307 * temp
308 * start: drbg->scratchpad
309 * length: drbg_statelen(drbg) + drbg_blocklen(drbg)
310 * note: the cipher writing into this variable works
311 * blocklen-wise. Now, when the statelen is not a multiple
312 * of blocklen, the generateion loop below "spills over"
313 * by at most blocklen. Thus, we need to give sufficient
314 * memory.
315 * df_data
316 * start: drbg->scratchpad +
317 * drbg_statelen(drbg) + drbg_blocklen(drbg)
318 * length: drbg_statelen(drbg)
319 *
320 * drbg_ctr_df:
321 * pad
322 * start: df_data + drbg_statelen(drbg)
323 * length: drbg_blocklen(drbg)
324 * iv
325 * start: pad + drbg_blocklen(drbg)
326 * length: drbg_blocklen(drbg)
327 * temp
328 * start: iv + drbg_blocklen(drbg)
329 * length: drbg_satelen(drbg) + drbg_blocklen(drbg)
330 * note: temp is the buffer that the BCC function operates
331 * on. BCC operates blockwise. drbg_statelen(drbg)
332 * is sufficient when the DRBG state length is a multiple
333 * of the block size. For AES192 (and maybe other ciphers)
334 * this is not correct and the length for temp is
335 * insufficient (yes, that also means for such ciphers,
336 * the final output of all BCC rounds are truncated).
337 * Therefore, add drbg_blocklen(drbg) to cover all
338 * possibilities.
339 */
340
341 /* Derivation Function for CTR DRBG as defined in 10.4.2 */
342 static int drbg_ctr_df(struct drbg_state *drbg,
343 unsigned char *df_data, size_t bytes_to_return,
344 struct list_head *seedlist)
345 {
346 int ret = -EFAULT;
347 unsigned char L_N[8];
348 /* S3 is input */
349 struct drbg_string S1, S2, S4, cipherin;
350 LIST_HEAD(bcc_list);
351 unsigned char *pad = df_data + drbg_statelen(drbg);
352 unsigned char *iv = pad + drbg_blocklen(drbg);
353 unsigned char *temp = iv + drbg_blocklen(drbg);
354 size_t padlen = 0;
355 unsigned int templen = 0;
356 /* 10.4.2 step 7 */
357 unsigned int i = 0;
358 /* 10.4.2 step 8 */
359 const unsigned char *K = (unsigned char *)
360 "\x00\x01\x02\x03\x04\x05\x06\x07"
361 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
362 "\x10\x11\x12\x13\x14\x15\x16\x17"
363 "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f";
364 unsigned char *X;
365 size_t generated_len = 0;
366 size_t inputlen = 0;
367 struct drbg_string *seed = NULL;
368
369 memset(pad, 0, drbg_blocklen(drbg));
370 memset(iv, 0, drbg_blocklen(drbg));
371
372 /* 10.4.2 step 1 is implicit as we work byte-wise */
373
374 /* 10.4.2 step 2 */
375 if ((512/8) < bytes_to_return)
376 return -EINVAL;
377
378 /* 10.4.2 step 2 -- calculate the entire length of all input data */
379 list_for_each_entry(seed, seedlist, list)
380 inputlen += seed->len;
381 drbg_cpu_to_be32(inputlen, &L_N[0]);
382
383 /* 10.4.2 step 3 */
384 drbg_cpu_to_be32(bytes_to_return, &L_N[4]);
385
386 /* 10.4.2 step 5: length is L_N, input_string, one byte, padding */
387 padlen = (inputlen + sizeof(L_N) + 1) % (drbg_blocklen(drbg));
388 /* wrap the padlen appropriately */
389 if (padlen)
390 padlen = drbg_blocklen(drbg) - padlen;
391 /*
392 * pad / padlen contains the 0x80 byte and the following zero bytes.
393 * As the calculated padlen value only covers the number of zero
394 * bytes, this value has to be incremented by one for the 0x80 byte.
395 */
396 padlen++;
397 pad[0] = 0x80;
398
399 /* 10.4.2 step 4 -- first fill the linked list and then order it */
400 drbg_string_fill(&S1, iv, drbg_blocklen(drbg));
401 list_add_tail(&S1.list, &bcc_list);
402 drbg_string_fill(&S2, L_N, sizeof(L_N));
403 list_add_tail(&S2.list, &bcc_list);
404 list_splice_tail(seedlist, &bcc_list);
405 drbg_string_fill(&S4, pad, padlen);
406 list_add_tail(&S4.list, &bcc_list);
407
408 /* 10.4.2 step 9 */
409 while (templen < (drbg_keylen(drbg) + (drbg_blocklen(drbg)))) {
410 /*
411 * 10.4.2 step 9.1 - the padding is implicit as the buffer
412 * holds zeros after allocation -- even the increment of i
413 * is irrelevant as the increment remains within length of i
414 */
415 drbg_cpu_to_be32(i, iv);
416 /* 10.4.2 step 9.2 -- BCC and concatenation with temp */
417 ret = drbg_ctr_bcc(drbg, temp + templen, K, &bcc_list);
418 if (ret)
419 goto out;
420 /* 10.4.2 step 9.3 */
421 i++;
422 templen += drbg_blocklen(drbg);
423 }
424
425 /* 10.4.2 step 11 */
426 X = temp + (drbg_keylen(drbg));
427 drbg_string_fill(&cipherin, X, drbg_blocklen(drbg));
428
429 /* 10.4.2 step 12: overwriting of outval is implemented in next step */
430
431 /* 10.4.2 step 13 */
432 drbg_kcapi_symsetkey(drbg, temp);
433 while (generated_len < bytes_to_return) {
434 short blocklen = 0;
435 /*
436 * 10.4.2 step 13.1: the truncation of the key length is
437 * implicit as the key is only drbg_blocklen in size based on
438 * the implementation of the cipher function callback
439 */
440 ret = drbg_kcapi_sym(drbg, X, &cipherin);
441 if (ret)
442 goto out;
443 blocklen = (drbg_blocklen(drbg) <
444 (bytes_to_return - generated_len)) ?
445 drbg_blocklen(drbg) :
446 (bytes_to_return - generated_len);
447 /* 10.4.2 step 13.2 and 14 */
448 memcpy(df_data + generated_len, X, blocklen);
449 generated_len += blocklen;
450 }
451
452 ret = 0;
453
454 out:
455 memset(iv, 0, drbg_blocklen(drbg));
456 memset(temp, 0, drbg_statelen(drbg) + drbg_blocklen(drbg));
457 memset(pad, 0, drbg_blocklen(drbg));
458 return ret;
459 }
460
461 /*
462 * update function of CTR DRBG as defined in 10.2.1.2
463 *
464 * The reseed variable has an enhanced meaning compared to the update
465 * functions of the other DRBGs as follows:
466 * 0 => initial seed from initialization
467 * 1 => reseed via drbg_seed
468 * 2 => first invocation from drbg_ctr_update when addtl is present. In
469 * this case, the df_data scratchpad is not deleted so that it is
470 * available for another calls to prevent calling the DF function
471 * again.
472 * 3 => second invocation from drbg_ctr_update. When the update function
473 * was called with addtl, the df_data memory already contains the
474 * DFed addtl information and we do not need to call DF again.
475 */
476 static int drbg_ctr_update(struct drbg_state *drbg, struct list_head *seed,
477 int reseed)
478 {
479 int ret = -EFAULT;
480 /* 10.2.1.2 step 1 */
481 unsigned char *temp = drbg->scratchpad;
482 unsigned char *df_data = drbg->scratchpad + drbg_statelen(drbg) +
483 drbg_blocklen(drbg);
484 unsigned char *temp_p, *df_data_p; /* pointer to iterate over buffers */
485 unsigned int len = 0;
486
487 if (3 > reseed)
488 memset(df_data, 0, drbg_statelen(drbg));
489
490 if (!reseed) {
491 /*
492 * The DRBG uses the CTR mode of the underlying AES cipher. The
493 * CTR mode increments the counter value after the AES operation
494 * but SP800-90A requires that the counter is incremented before
495 * the AES operation. Hence, we increment it at the time we set
496 * it by one.
497 */
498 crypto_inc(drbg->V, drbg_blocklen(drbg));
499
500 ret = crypto_skcipher_setkey(drbg->ctr_handle, drbg->C,
501 drbg_keylen(drbg));
502 if (ret)
503 goto out;
504 }
505
506 /* 10.2.1.3.2 step 2 and 10.2.1.4.2 step 2 */
507 if (seed) {
508 ret = drbg_ctr_df(drbg, df_data, drbg_statelen(drbg), seed);
509 if (ret)
510 goto out;
511 }
512
513 ret = drbg_kcapi_sym_ctr(drbg, temp, drbg_statelen(drbg));
514 if (ret)
515 return ret;
516
517 /* 10.2.1.2 step 4 */
518 temp_p = temp;
519 df_data_p = df_data;
520 for (len = 0; len < drbg_statelen(drbg); len++) {
521 *temp_p ^= *df_data_p;
522 df_data_p++; temp_p++;
523 }
524
525 /* 10.2.1.2 step 5 */
526 memcpy(drbg->C, temp, drbg_keylen(drbg));
527 ret = crypto_skcipher_setkey(drbg->ctr_handle, drbg->C,
528 drbg_keylen(drbg));
529 if (ret)
530 goto out;
531 /* 10.2.1.2 step 6 */
532 memcpy(drbg->V, temp + drbg_keylen(drbg), drbg_blocklen(drbg));
533 /* See above: increment counter by one to compensate timing of CTR op */
534 crypto_inc(drbg->V, drbg_blocklen(drbg));
535 ret = 0;
536
537 out:
538 memset(temp, 0, drbg_statelen(drbg) + drbg_blocklen(drbg));
539 if (2 != reseed)
540 memset(df_data, 0, drbg_statelen(drbg));
541 return ret;
542 }
543
544 /*
545 * scratchpad use: drbg_ctr_update is called independently from
546 * drbg_ctr_extract_bytes. Therefore, the scratchpad is reused
547 */
548 /* Generate function of CTR DRBG as defined in 10.2.1.5.2 */
549 static int drbg_ctr_generate(struct drbg_state *drbg,
550 unsigned char *buf, unsigned int buflen,
551 struct list_head *addtl)
552 {
553 int ret;
554 int len = min_t(int, buflen, INT_MAX);
555
556 /* 10.2.1.5.2 step 2 */
557 if (addtl && !list_empty(addtl)) {
558 ret = drbg_ctr_update(drbg, addtl, 2);
559 if (ret)
560 return 0;
561 }
562
563 /* 10.2.1.5.2 step 4.1 */
564 ret = drbg_kcapi_sym_ctr(drbg, buf, len);
565 if (ret)
566 return ret;
567
568 /* 10.2.1.5.2 step 6 */
569 ret = drbg_ctr_update(drbg, NULL, 3);
570 if (ret)
571 len = ret;
572
573 return len;
574 }
575
576 static const struct drbg_state_ops drbg_ctr_ops = {
577 .update = drbg_ctr_update,
578 .generate = drbg_ctr_generate,
579 .crypto_init = drbg_init_sym_kernel,
580 .crypto_fini = drbg_fini_sym_kernel,
581 };
582 #endif /* CONFIG_CRYPTO_DRBG_CTR */
583
584 /******************************************************************
585 * HMAC DRBG callback functions
586 ******************************************************************/
587
588 #if defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_HMAC)
589 static int drbg_kcapi_hash(struct drbg_state *drbg, unsigned char *outval,
590 const struct list_head *in);
591 static void drbg_kcapi_hmacsetkey(struct drbg_state *drbg,
592 const unsigned char *key);
593 static int drbg_init_hash_kernel(struct drbg_state *drbg);
594 static int drbg_fini_hash_kernel(struct drbg_state *drbg);
595 #endif /* (CONFIG_CRYPTO_DRBG_HASH || CONFIG_CRYPTO_DRBG_HMAC) */
596
597 #ifdef CONFIG_CRYPTO_DRBG_HMAC
598 #define CRYPTO_DRBG_HMAC_STRING "HMAC "
599 MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha512");
600 MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha512");
601 MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha384");
602 MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha384");
603 MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha256");
604 MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha256");
605 MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha1");
606 MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha1");
607
608 /* update function of HMAC DRBG as defined in 10.1.2.2 */
609 static int drbg_hmac_update(struct drbg_state *drbg, struct list_head *seed,
610 int reseed)
611 {
612 int ret = -EFAULT;
613 int i = 0;
614 struct drbg_string seed1, seed2, vdata;
615 LIST_HEAD(seedlist);
616 LIST_HEAD(vdatalist);
617
618 if (!reseed) {
619 /* 10.1.2.3 step 2 -- memset(0) of C is implicit with kzalloc */
620 memset(drbg->V, 1, drbg_statelen(drbg));
621 drbg_kcapi_hmacsetkey(drbg, drbg->C);
622 }
623
624 drbg_string_fill(&seed1, drbg->V, drbg_statelen(drbg));
625 list_add_tail(&seed1.list, &seedlist);
626 /* buffer of seed2 will be filled in for loop below with one byte */
627 drbg_string_fill(&seed2, NULL, 1);
628 list_add_tail(&seed2.list, &seedlist);
629 /* input data of seed is allowed to be NULL at this point */
630 if (seed)
631 list_splice_tail(seed, &seedlist);
632
633 drbg_string_fill(&vdata, drbg->V, drbg_statelen(drbg));
634 list_add_tail(&vdata.list, &vdatalist);
635 for (i = 2; 0 < i; i--) {
636 /* first round uses 0x0, second 0x1 */
637 unsigned char prefix = DRBG_PREFIX0;
638 if (1 == i)
639 prefix = DRBG_PREFIX1;
640 /* 10.1.2.2 step 1 and 4 -- concatenation and HMAC for key */
641 seed2.buf = &prefix;
642 ret = drbg_kcapi_hash(drbg, drbg->C, &seedlist);
643 if (ret)
644 return ret;
645 drbg_kcapi_hmacsetkey(drbg, drbg->C);
646
647 /* 10.1.2.2 step 2 and 5 -- HMAC for V */
648 ret = drbg_kcapi_hash(drbg, drbg->V, &vdatalist);
649 if (ret)
650 return ret;
651
652 /* 10.1.2.2 step 3 */
653 if (!seed)
654 return ret;
655 }
656
657 return 0;
658 }
659
660 /* generate function of HMAC DRBG as defined in 10.1.2.5 */
661 static int drbg_hmac_generate(struct drbg_state *drbg,
662 unsigned char *buf,
663 unsigned int buflen,
664 struct list_head *addtl)
665 {
666 int len = 0;
667 int ret = 0;
668 struct drbg_string data;
669 LIST_HEAD(datalist);
670
671 /* 10.1.2.5 step 2 */
672 if (addtl && !list_empty(addtl)) {
673 ret = drbg_hmac_update(drbg, addtl, 1);
674 if (ret)
675 return ret;
676 }
677
678 drbg_string_fill(&data, drbg->V, drbg_statelen(drbg));
679 list_add_tail(&data.list, &datalist);
680 while (len < buflen) {
681 unsigned int outlen = 0;
682 /* 10.1.2.5 step 4.1 */
683 ret = drbg_kcapi_hash(drbg, drbg->V, &datalist);
684 if (ret)
685 return ret;
686 outlen = (drbg_blocklen(drbg) < (buflen - len)) ?
687 drbg_blocklen(drbg) : (buflen - len);
688
689 /* 10.1.2.5 step 4.2 */
690 memcpy(buf + len, drbg->V, outlen);
691 len += outlen;
692 }
693
694 /* 10.1.2.5 step 6 */
695 if (addtl && !list_empty(addtl))
696 ret = drbg_hmac_update(drbg, addtl, 1);
697 else
698 ret = drbg_hmac_update(drbg, NULL, 1);
699 if (ret)
700 return ret;
701
702 return len;
703 }
704
705 static const struct drbg_state_ops drbg_hmac_ops = {
706 .update = drbg_hmac_update,
707 .generate = drbg_hmac_generate,
708 .crypto_init = drbg_init_hash_kernel,
709 .crypto_fini = drbg_fini_hash_kernel,
710 };
711 #endif /* CONFIG_CRYPTO_DRBG_HMAC */
712
713 /******************************************************************
714 * Hash DRBG callback functions
715 ******************************************************************/
716
717 #ifdef CONFIG_CRYPTO_DRBG_HASH
718 #define CRYPTO_DRBG_HASH_STRING "HASH "
719 MODULE_ALIAS_CRYPTO("drbg_pr_sha512");
720 MODULE_ALIAS_CRYPTO("drbg_nopr_sha512");
721 MODULE_ALIAS_CRYPTO("drbg_pr_sha384");
722 MODULE_ALIAS_CRYPTO("drbg_nopr_sha384");
723 MODULE_ALIAS_CRYPTO("drbg_pr_sha256");
724 MODULE_ALIAS_CRYPTO("drbg_nopr_sha256");
725 MODULE_ALIAS_CRYPTO("drbg_pr_sha1");
726 MODULE_ALIAS_CRYPTO("drbg_nopr_sha1");
727
728 /*
729 * Increment buffer
730 *
731 * @dst buffer to increment
732 * @add value to add
733 */
734 static inline void drbg_add_buf(unsigned char *dst, size_t dstlen,
735 const unsigned char *add, size_t addlen)
736 {
737 /* implied: dstlen > addlen */
738 unsigned char *dstptr;
739 const unsigned char *addptr;
740 unsigned int remainder = 0;
741 size_t len = addlen;
742
743 dstptr = dst + (dstlen-1);
744 addptr = add + (addlen-1);
745 while (len) {
746 remainder += *dstptr + *addptr;
747 *dstptr = remainder & 0xff;
748 remainder >>= 8;
749 len--; dstptr--; addptr--;
750 }
751 len = dstlen - addlen;
752 while (len && remainder > 0) {
753 remainder = *dstptr + 1;
754 *dstptr = remainder & 0xff;
755 remainder >>= 8;
756 len--; dstptr--;
757 }
758 }
759
760 /*
761 * scratchpad usage: as drbg_hash_update and drbg_hash_df are used
762 * interlinked, the scratchpad is used as follows:
763 * drbg_hash_update
764 * start: drbg->scratchpad
765 * length: drbg_statelen(drbg)
766 * drbg_hash_df:
767 * start: drbg->scratchpad + drbg_statelen(drbg)
768 * length: drbg_blocklen(drbg)
769 *
770 * drbg_hash_process_addtl uses the scratchpad, but fully completes
771 * before either of the functions mentioned before are invoked. Therefore,
772 * drbg_hash_process_addtl does not need to be specifically considered.
773 */
774
775 /* Derivation Function for Hash DRBG as defined in 10.4.1 */
776 static int drbg_hash_df(struct drbg_state *drbg,
777 unsigned char *outval, size_t outlen,
778 struct list_head *entropylist)
779 {
780 int ret = 0;
781 size_t len = 0;
782 unsigned char input[5];
783 unsigned char *tmp = drbg->scratchpad + drbg_statelen(drbg);
784 struct drbg_string data;
785
786 /* 10.4.1 step 3 */
787 input[0] = 1;
788 drbg_cpu_to_be32((outlen * 8), &input[1]);
789
790 /* 10.4.1 step 4.1 -- concatenation of data for input into hash */
791 drbg_string_fill(&data, input, 5);
792 list_add(&data.list, entropylist);
793
794 /* 10.4.1 step 4 */
795 while (len < outlen) {
796 short blocklen = 0;
797 /* 10.4.1 step 4.1 */
798 ret = drbg_kcapi_hash(drbg, tmp, entropylist);
799 if (ret)
800 goto out;
801 /* 10.4.1 step 4.2 */
802 input[0]++;
803 blocklen = (drbg_blocklen(drbg) < (outlen - len)) ?
804 drbg_blocklen(drbg) : (outlen - len);
805 memcpy(outval + len, tmp, blocklen);
806 len += blocklen;
807 }
808
809 out:
810 memset(tmp, 0, drbg_blocklen(drbg));
811 return ret;
812 }
813
814 /* update function for Hash DRBG as defined in 10.1.1.2 / 10.1.1.3 */
815 static int drbg_hash_update(struct drbg_state *drbg, struct list_head *seed,
816 int reseed)
817 {
818 int ret = 0;
819 struct drbg_string data1, data2;
820 LIST_HEAD(datalist);
821 LIST_HEAD(datalist2);
822 unsigned char *V = drbg->scratchpad;
823 unsigned char prefix = DRBG_PREFIX1;
824
825 if (!seed)
826 return -EINVAL;
827
828 if (reseed) {
829 /* 10.1.1.3 step 1 */
830 memcpy(V, drbg->V, drbg_statelen(drbg));
831 drbg_string_fill(&data1, &prefix, 1);
832 list_add_tail(&data1.list, &datalist);
833 drbg_string_fill(&data2, V, drbg_statelen(drbg));
834 list_add_tail(&data2.list, &datalist);
835 }
836 list_splice_tail(seed, &datalist);
837
838 /* 10.1.1.2 / 10.1.1.3 step 2 and 3 */
839 ret = drbg_hash_df(drbg, drbg->V, drbg_statelen(drbg), &datalist);
840 if (ret)
841 goto out;
842
843 /* 10.1.1.2 / 10.1.1.3 step 4 */
844 prefix = DRBG_PREFIX0;
845 drbg_string_fill(&data1, &prefix, 1);
846 list_add_tail(&data1.list, &datalist2);
847 drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
848 list_add_tail(&data2.list, &datalist2);
849 /* 10.1.1.2 / 10.1.1.3 step 4 */
850 ret = drbg_hash_df(drbg, drbg->C, drbg_statelen(drbg), &datalist2);
851
852 out:
853 memset(drbg->scratchpad, 0, drbg_statelen(drbg));
854 return ret;
855 }
856
857 /* processing of additional information string for Hash DRBG */
858 static int drbg_hash_process_addtl(struct drbg_state *drbg,
859 struct list_head *addtl)
860 {
861 int ret = 0;
862 struct drbg_string data1, data2;
863 LIST_HEAD(datalist);
864 unsigned char prefix = DRBG_PREFIX2;
865
866 /* 10.1.1.4 step 2 */
867 if (!addtl || list_empty(addtl))
868 return 0;
869
870 /* 10.1.1.4 step 2a */
871 drbg_string_fill(&data1, &prefix, 1);
872 drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
873 list_add_tail(&data1.list, &datalist);
874 list_add_tail(&data2.list, &datalist);
875 list_splice_tail(addtl, &datalist);
876 ret = drbg_kcapi_hash(drbg, drbg->scratchpad, &datalist);
877 if (ret)
878 goto out;
879
880 /* 10.1.1.4 step 2b */
881 drbg_add_buf(drbg->V, drbg_statelen(drbg),
882 drbg->scratchpad, drbg_blocklen(drbg));
883
884 out:
885 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
886 return ret;
887 }
888
889 /* Hashgen defined in 10.1.1.4 */
890 static int drbg_hash_hashgen(struct drbg_state *drbg,
891 unsigned char *buf,
892 unsigned int buflen)
893 {
894 int len = 0;
895 int ret = 0;
896 unsigned char *src = drbg->scratchpad;
897 unsigned char *dst = drbg->scratchpad + drbg_statelen(drbg);
898 struct drbg_string data;
899 LIST_HEAD(datalist);
900
901 /* 10.1.1.4 step hashgen 2 */
902 memcpy(src, drbg->V, drbg_statelen(drbg));
903
904 drbg_string_fill(&data, src, drbg_statelen(drbg));
905 list_add_tail(&data.list, &datalist);
906 while (len < buflen) {
907 unsigned int outlen = 0;
908 /* 10.1.1.4 step hashgen 4.1 */
909 ret = drbg_kcapi_hash(drbg, dst, &datalist);
910 if (ret) {
911 len = ret;
912 goto out;
913 }
914 outlen = (drbg_blocklen(drbg) < (buflen - len)) ?
915 drbg_blocklen(drbg) : (buflen - len);
916 /* 10.1.1.4 step hashgen 4.2 */
917 memcpy(buf + len, dst, outlen);
918 len += outlen;
919 /* 10.1.1.4 hashgen step 4.3 */
920 if (len < buflen)
921 crypto_inc(src, drbg_statelen(drbg));
922 }
923
924 out:
925 memset(drbg->scratchpad, 0,
926 (drbg_statelen(drbg) + drbg_blocklen(drbg)));
927 return len;
928 }
929
930 /* generate function for Hash DRBG as defined in 10.1.1.4 */
931 static int drbg_hash_generate(struct drbg_state *drbg,
932 unsigned char *buf, unsigned int buflen,
933 struct list_head *addtl)
934 {
935 int len = 0;
936 int ret = 0;
937 union {
938 unsigned char req[8];
939 __be64 req_int;
940 } u;
941 unsigned char prefix = DRBG_PREFIX3;
942 struct drbg_string data1, data2;
943 LIST_HEAD(datalist);
944
945 /* 10.1.1.4 step 2 */
946 ret = drbg_hash_process_addtl(drbg, addtl);
947 if (ret)
948 return ret;
949 /* 10.1.1.4 step 3 */
950 len = drbg_hash_hashgen(drbg, buf, buflen);
951
952 /* this is the value H as documented in 10.1.1.4 */
953 /* 10.1.1.4 step 4 */
954 drbg_string_fill(&data1, &prefix, 1);
955 list_add_tail(&data1.list, &datalist);
956 drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
957 list_add_tail(&data2.list, &datalist);
958 ret = drbg_kcapi_hash(drbg, drbg->scratchpad, &datalist);
959 if (ret) {
960 len = ret;
961 goto out;
962 }
963
964 /* 10.1.1.4 step 5 */
965 drbg_add_buf(drbg->V, drbg_statelen(drbg),
966 drbg->scratchpad, drbg_blocklen(drbg));
967 drbg_add_buf(drbg->V, drbg_statelen(drbg),
968 drbg->C, drbg_statelen(drbg));
969 u.req_int = cpu_to_be64(drbg->reseed_ctr);
970 drbg_add_buf(drbg->V, drbg_statelen(drbg), u.req, 8);
971
972 out:
973 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
974 return len;
975 }
976
977 /*
978 * scratchpad usage: as update and generate are used isolated, both
979 * can use the scratchpad
980 */
981 static const struct drbg_state_ops drbg_hash_ops = {
982 .update = drbg_hash_update,
983 .generate = drbg_hash_generate,
984 .crypto_init = drbg_init_hash_kernel,
985 .crypto_fini = drbg_fini_hash_kernel,
986 };
987 #endif /* CONFIG_CRYPTO_DRBG_HASH */
988
989 /******************************************************************
990 * Functions common for DRBG implementations
991 ******************************************************************/
992
993 static inline int __drbg_seed(struct drbg_state *drbg, struct list_head *seed,
994 int reseed)
995 {
996 int ret = drbg->d_ops->update(drbg, seed, reseed);
997
998 if (ret)
999 return ret;
1000
1001 drbg->seeded = true;
1002 /* 10.1.1.2 / 10.1.1.3 step 5 */
1003 drbg->reseed_ctr = 1;
1004
1005 return ret;
1006 }
1007
1008 static void drbg_async_seed(struct work_struct *work)
1009 {
1010 struct drbg_string data;
1011 LIST_HEAD(seedlist);
1012 struct drbg_state *drbg = container_of(work, struct drbg_state,
1013 seed_work);
1014 unsigned int entropylen = drbg_sec_strength(drbg->core->flags);
1015 unsigned char entropy[32];
1016
1017 BUG_ON(!entropylen);
1018 BUG_ON(entropylen > sizeof(entropy));
1019 get_random_bytes(entropy, entropylen);
1020
1021 drbg_string_fill(&data, entropy, entropylen);
1022 list_add_tail(&data.list, &seedlist);
1023
1024 mutex_lock(&drbg->drbg_mutex);
1025
1026 /* If nonblocking pool is initialized, deactivate Jitter RNG */
1027 crypto_free_rng(drbg->jent);
1028 drbg->jent = NULL;
1029
1030 /* Set seeded to false so that if __drbg_seed fails the
1031 * next generate call will trigger a reseed.
1032 */
1033 drbg->seeded = false;
1034
1035 __drbg_seed(drbg, &seedlist, true);
1036
1037 if (drbg->seeded)
1038 drbg->reseed_threshold = drbg_max_requests(drbg);
1039
1040 mutex_unlock(&drbg->drbg_mutex);
1041
1042 memzero_explicit(entropy, entropylen);
1043 }
1044
1045 /*
1046 * Seeding or reseeding of the DRBG
1047 *
1048 * @drbg: DRBG state struct
1049 * @pers: personalization / additional information buffer
1050 * @reseed: 0 for initial seed process, 1 for reseeding
1051 *
1052 * return:
1053 * 0 on success
1054 * error value otherwise
1055 */
1056 static int drbg_seed(struct drbg_state *drbg, struct drbg_string *pers,
1057 bool reseed)
1058 {
1059 int ret;
1060 unsigned char entropy[((32 + 16) * 2)];
1061 unsigned int entropylen = drbg_sec_strength(drbg->core->flags);
1062 struct drbg_string data1;
1063 LIST_HEAD(seedlist);
1064
1065 /* 9.1 / 9.2 / 9.3.1 step 3 */
1066 if (pers && pers->len > (drbg_max_addtl(drbg))) {
1067 pr_devel("DRBG: personalization string too long %zu\n",
1068 pers->len);
1069 return -EINVAL;
1070 }
1071
1072 if (list_empty(&drbg->test_data.list)) {
1073 drbg_string_fill(&data1, drbg->test_data.buf,
1074 drbg->test_data.len);
1075 pr_devel("DRBG: using test entropy\n");
1076 } else {
1077 /*
1078 * Gather entropy equal to the security strength of the DRBG.
1079 * With a derivation function, a nonce is required in addition
1080 * to the entropy. A nonce must be at least 1/2 of the security
1081 * strength of the DRBG in size. Thus, entropy + nonce is 3/2
1082 * of the strength. The consideration of a nonce is only
1083 * applicable during initial seeding.
1084 */
1085 BUG_ON(!entropylen);
1086 if (!reseed)
1087 entropylen = ((entropylen + 1) / 2) * 3;
1088 BUG_ON((entropylen * 2) > sizeof(entropy));
1089
1090 /* Get seed from in-kernel /dev/urandom */
1091 get_random_bytes(entropy, entropylen);
1092
1093 if (!drbg->jent) {
1094 drbg_string_fill(&data1, entropy, entropylen);
1095 pr_devel("DRBG: (re)seeding with %u bytes of entropy\n",
1096 entropylen);
1097 } else {
1098 /* Get seed from Jitter RNG */
1099 ret = crypto_rng_get_bytes(drbg->jent,
1100 entropy + entropylen,
1101 entropylen);
1102 if (ret) {
1103 pr_devel("DRBG: jent failed with %d\n", ret);
1104 return ret;
1105 }
1106
1107 drbg_string_fill(&data1, entropy, entropylen * 2);
1108 pr_devel("DRBG: (re)seeding with %u bytes of entropy\n",
1109 entropylen * 2);
1110 }
1111 }
1112 list_add_tail(&data1.list, &seedlist);
1113
1114 /*
1115 * concatenation of entropy with personalization str / addtl input)
1116 * the variable pers is directly handed in by the caller, so check its
1117 * contents whether it is appropriate
1118 */
1119 if (pers && pers->buf && 0 < pers->len) {
1120 list_add_tail(&pers->list, &seedlist);
1121 pr_devel("DRBG: using personalization string\n");
1122 }
1123
1124 if (!reseed) {
1125 memset(drbg->V, 0, drbg_statelen(drbg));
1126 memset(drbg->C, 0, drbg_statelen(drbg));
1127 }
1128
1129 ret = __drbg_seed(drbg, &seedlist, reseed);
1130
1131 memzero_explicit(entropy, entropylen * 2);
1132
1133 return ret;
1134 }
1135
1136 /* Free all substructures in a DRBG state without the DRBG state structure */
1137 static inline void drbg_dealloc_state(struct drbg_state *drbg)
1138 {
1139 if (!drbg)
1140 return;
1141 kzfree(drbg->V);
1142 drbg->V = NULL;
1143 kzfree(drbg->C);
1144 drbg->C = NULL;
1145 kzfree(drbg->scratchpad);
1146 drbg->scratchpad = NULL;
1147 drbg->reseed_ctr = 0;
1148 drbg->d_ops = NULL;
1149 drbg->core = NULL;
1150 }
1151
1152 /*
1153 * Allocate all sub-structures for a DRBG state.
1154 * The DRBG state structure must already be allocated.
1155 */
1156 static inline int drbg_alloc_state(struct drbg_state *drbg)
1157 {
1158 int ret = -ENOMEM;
1159 unsigned int sb_size = 0;
1160
1161 switch (drbg->core->flags & DRBG_TYPE_MASK) {
1162 #ifdef CONFIG_CRYPTO_DRBG_HMAC
1163 case DRBG_HMAC:
1164 drbg->d_ops = &drbg_hmac_ops;
1165 break;
1166 #endif /* CONFIG_CRYPTO_DRBG_HMAC */
1167 #ifdef CONFIG_CRYPTO_DRBG_HASH
1168 case DRBG_HASH:
1169 drbg->d_ops = &drbg_hash_ops;
1170 break;
1171 #endif /* CONFIG_CRYPTO_DRBG_HASH */
1172 #ifdef CONFIG_CRYPTO_DRBG_CTR
1173 case DRBG_CTR:
1174 drbg->d_ops = &drbg_ctr_ops;
1175 break;
1176 #endif /* CONFIG_CRYPTO_DRBG_CTR */
1177 default:
1178 ret = -EOPNOTSUPP;
1179 goto err;
1180 }
1181
1182 drbg->V = kmalloc(drbg_statelen(drbg), GFP_KERNEL);
1183 if (!drbg->V)
1184 goto err;
1185 drbg->C = kmalloc(drbg_statelen(drbg), GFP_KERNEL);
1186 if (!drbg->C)
1187 goto err;
1188 /* scratchpad is only generated for CTR and Hash */
1189 if (drbg->core->flags & DRBG_HMAC)
1190 sb_size = 0;
1191 else if (drbg->core->flags & DRBG_CTR)
1192 sb_size = drbg_statelen(drbg) + drbg_blocklen(drbg) + /* temp */
1193 drbg_statelen(drbg) + /* df_data */
1194 drbg_blocklen(drbg) + /* pad */
1195 drbg_blocklen(drbg) + /* iv */
1196 drbg_statelen(drbg) + drbg_blocklen(drbg); /* temp */
1197 else
1198 sb_size = drbg_statelen(drbg) + drbg_blocklen(drbg);
1199
1200 if (0 < sb_size) {
1201 drbg->scratchpad = kzalloc(sb_size, GFP_KERNEL);
1202 if (!drbg->scratchpad)
1203 goto err;
1204 }
1205
1206 return 0;
1207
1208 err:
1209 drbg_dealloc_state(drbg);
1210 return ret;
1211 }
1212
1213 /*************************************************************************
1214 * DRBG interface functions
1215 *************************************************************************/
1216
1217 /*
1218 * DRBG generate function as required by SP800-90A - this function
1219 * generates random numbers
1220 *
1221 * @drbg DRBG state handle
1222 * @buf Buffer where to store the random numbers -- the buffer must already
1223 * be pre-allocated by caller
1224 * @buflen Length of output buffer - this value defines the number of random
1225 * bytes pulled from DRBG
1226 * @addtl Additional input that is mixed into state, may be NULL -- note
1227 * the entropy is pulled by the DRBG internally unconditionally
1228 * as defined in SP800-90A. The additional input is mixed into
1229 * the state in addition to the pulled entropy.
1230 *
1231 * return: 0 when all bytes are generated; < 0 in case of an error
1232 */
1233 static int drbg_generate(struct drbg_state *drbg,
1234 unsigned char *buf, unsigned int buflen,
1235 struct drbg_string *addtl)
1236 {
1237 int len = 0;
1238 LIST_HEAD(addtllist);
1239
1240 if (!drbg->core) {
1241 pr_devel("DRBG: not yet seeded\n");
1242 return -EINVAL;
1243 }
1244 if (0 == buflen || !buf) {
1245 pr_devel("DRBG: no output buffer provided\n");
1246 return -EINVAL;
1247 }
1248 if (addtl && NULL == addtl->buf && 0 < addtl->len) {
1249 pr_devel("DRBG: wrong format of additional information\n");
1250 return -EINVAL;
1251 }
1252
1253 /* 9.3.1 step 2 */
1254 len = -EINVAL;
1255 if (buflen > (drbg_max_request_bytes(drbg))) {
1256 pr_devel("DRBG: requested random numbers too large %u\n",
1257 buflen);
1258 goto err;
1259 }
1260
1261 /* 9.3.1 step 3 is implicit with the chosen DRBG */
1262
1263 /* 9.3.1 step 4 */
1264 if (addtl && addtl->len > (drbg_max_addtl(drbg))) {
1265 pr_devel("DRBG: additional information string too long %zu\n",
1266 addtl->len);
1267 goto err;
1268 }
1269 /* 9.3.1 step 5 is implicit with the chosen DRBG */
1270
1271 /*
1272 * 9.3.1 step 6 and 9 supplemented by 9.3.2 step c is implemented
1273 * here. The spec is a bit convoluted here, we make it simpler.
1274 */
1275 if (drbg->reseed_threshold < drbg->reseed_ctr)
1276 drbg->seeded = false;
1277
1278 if (drbg->pr || !drbg->seeded) {
1279 pr_devel("DRBG: reseeding before generation (prediction "
1280 "resistance: %s, state %s)\n",
1281 drbg->pr ? "true" : "false",
1282 drbg->seeded ? "seeded" : "unseeded");
1283 /* 9.3.1 steps 7.1 through 7.3 */
1284 len = drbg_seed(drbg, addtl, true);
1285 if (len)
1286 goto err;
1287 /* 9.3.1 step 7.4 */
1288 addtl = NULL;
1289 }
1290
1291 if (addtl && 0 < addtl->len)
1292 list_add_tail(&addtl->list, &addtllist);
1293 /* 9.3.1 step 8 and 10 */
1294 len = drbg->d_ops->generate(drbg, buf, buflen, &addtllist);
1295
1296 /* 10.1.1.4 step 6, 10.1.2.5 step 7, 10.2.1.5.2 step 7 */
1297 drbg->reseed_ctr++;
1298 if (0 >= len)
1299 goto err;
1300
1301 /*
1302 * Section 11.3.3 requires to re-perform self tests after some
1303 * generated random numbers. The chosen value after which self
1304 * test is performed is arbitrary, but it should be reasonable.
1305 * However, we do not perform the self tests because of the following
1306 * reasons: it is mathematically impossible that the initial self tests
1307 * were successfully and the following are not. If the initial would
1308 * pass and the following would not, the kernel integrity is violated.
1309 * In this case, the entire kernel operation is questionable and it
1310 * is unlikely that the integrity violation only affects the
1311 * correct operation of the DRBG.
1312 *
1313 * Albeit the following code is commented out, it is provided in
1314 * case somebody has a need to implement the test of 11.3.3.
1315 */
1316 #if 0
1317 if (drbg->reseed_ctr && !(drbg->reseed_ctr % 4096)) {
1318 int err = 0;
1319 pr_devel("DRBG: start to perform self test\n");
1320 if (drbg->core->flags & DRBG_HMAC)
1321 err = alg_test("drbg_pr_hmac_sha256",
1322 "drbg_pr_hmac_sha256", 0, 0);
1323 else if (drbg->core->flags & DRBG_CTR)
1324 err = alg_test("drbg_pr_ctr_aes128",
1325 "drbg_pr_ctr_aes128", 0, 0);
1326 else
1327 err = alg_test("drbg_pr_sha256",
1328 "drbg_pr_sha256", 0, 0);
1329 if (err) {
1330 pr_err("DRBG: periodical self test failed\n");
1331 /*
1332 * uninstantiate implies that from now on, only errors
1333 * are returned when reusing this DRBG cipher handle
1334 */
1335 drbg_uninstantiate(drbg);
1336 return 0;
1337 } else {
1338 pr_devel("DRBG: self test successful\n");
1339 }
1340 }
1341 #endif
1342
1343 /*
1344 * All operations were successful, return 0 as mandated by
1345 * the kernel crypto API interface.
1346 */
1347 len = 0;
1348 err:
1349 return len;
1350 }
1351
1352 /*
1353 * Wrapper around drbg_generate which can pull arbitrary long strings
1354 * from the DRBG without hitting the maximum request limitation.
1355 *
1356 * Parameters: see drbg_generate
1357 * Return codes: see drbg_generate -- if one drbg_generate request fails,
1358 * the entire drbg_generate_long request fails
1359 */
1360 static int drbg_generate_long(struct drbg_state *drbg,
1361 unsigned char *buf, unsigned int buflen,
1362 struct drbg_string *addtl)
1363 {
1364 unsigned int len = 0;
1365 unsigned int slice = 0;
1366 do {
1367 int err = 0;
1368 unsigned int chunk = 0;
1369 slice = ((buflen - len) / drbg_max_request_bytes(drbg));
1370 chunk = slice ? drbg_max_request_bytes(drbg) : (buflen - len);
1371 mutex_lock(&drbg->drbg_mutex);
1372 err = drbg_generate(drbg, buf + len, chunk, addtl);
1373 mutex_unlock(&drbg->drbg_mutex);
1374 if (0 > err)
1375 return err;
1376 len += chunk;
1377 } while (slice > 0 && (len < buflen));
1378 return 0;
1379 }
1380
1381 static void drbg_schedule_async_seed(struct random_ready_callback *rdy)
1382 {
1383 struct drbg_state *drbg = container_of(rdy, struct drbg_state,
1384 random_ready);
1385
1386 schedule_work(&drbg->seed_work);
1387 }
1388
1389 static int drbg_prepare_hrng(struct drbg_state *drbg)
1390 {
1391 int err;
1392
1393 /* We do not need an HRNG in test mode. */
1394 if (list_empty(&drbg->test_data.list))
1395 return 0;
1396
1397 INIT_WORK(&drbg->seed_work, drbg_async_seed);
1398
1399 drbg->random_ready.owner = THIS_MODULE;
1400 drbg->random_ready.func = drbg_schedule_async_seed;
1401
1402 err = add_random_ready_callback(&drbg->random_ready);
1403
1404 switch (err) {
1405 case 0:
1406 break;
1407
1408 case -EALREADY:
1409 err = 0;
1410 /* fall through */
1411
1412 default:
1413 drbg->random_ready.func = NULL;
1414 return err;
1415 }
1416
1417 drbg->jent = crypto_alloc_rng("jitterentropy_rng", 0, 0);
1418
1419 /*
1420 * Require frequent reseeds until the seed source is fully
1421 * initialized.
1422 */
1423 drbg->reseed_threshold = 50;
1424
1425 return err;
1426 }
1427
1428 /*
1429 * DRBG instantiation function as required by SP800-90A - this function
1430 * sets up the DRBG handle, performs the initial seeding and all sanity
1431 * checks required by SP800-90A
1432 *
1433 * @drbg memory of state -- if NULL, new memory is allocated
1434 * @pers Personalization string that is mixed into state, may be NULL -- note
1435 * the entropy is pulled by the DRBG internally unconditionally
1436 * as defined in SP800-90A. The additional input is mixed into
1437 * the state in addition to the pulled entropy.
1438 * @coreref reference to core
1439 * @pr prediction resistance enabled
1440 *
1441 * return
1442 * 0 on success
1443 * error value otherwise
1444 */
1445 static int drbg_instantiate(struct drbg_state *drbg, struct drbg_string *pers,
1446 int coreref, bool pr)
1447 {
1448 int ret;
1449 bool reseed = true;
1450
1451 pr_devel("DRBG: Initializing DRBG core %d with prediction resistance "
1452 "%s\n", coreref, pr ? "enabled" : "disabled");
1453 mutex_lock(&drbg->drbg_mutex);
1454
1455 /* 9.1 step 1 is implicit with the selected DRBG type */
1456
1457 /*
1458 * 9.1 step 2 is implicit as caller can select prediction resistance
1459 * and the flag is copied into drbg->flags --
1460 * all DRBG types support prediction resistance
1461 */
1462
1463 /* 9.1 step 4 is implicit in drbg_sec_strength */
1464
1465 if (!drbg->core) {
1466 drbg->core = &drbg_cores[coreref];
1467 drbg->pr = pr;
1468 drbg->seeded = false;
1469 drbg->reseed_threshold = drbg_max_requests(drbg);
1470
1471 ret = drbg_alloc_state(drbg);
1472 if (ret)
1473 goto unlock;
1474
1475 ret = -EFAULT;
1476 if (drbg->d_ops->crypto_init(drbg))
1477 goto err;
1478
1479 ret = drbg_prepare_hrng(drbg);
1480 if (ret)
1481 goto free_everything;
1482
1483 if (IS_ERR(drbg->jent)) {
1484 ret = PTR_ERR(drbg->jent);
1485 drbg->jent = NULL;
1486 if (fips_enabled || ret != -ENOENT)
1487 goto free_everything;
1488 pr_info("DRBG: Continuing without Jitter RNG\n");
1489 }
1490
1491 reseed = false;
1492 }
1493
1494 ret = drbg_seed(drbg, pers, reseed);
1495
1496 if (ret && !reseed)
1497 goto free_everything;
1498
1499 mutex_unlock(&drbg->drbg_mutex);
1500 return ret;
1501
1502 err:
1503 drbg_dealloc_state(drbg);
1504 unlock:
1505 mutex_unlock(&drbg->drbg_mutex);
1506 return ret;
1507
1508 free_everything:
1509 mutex_unlock(&drbg->drbg_mutex);
1510 drbg_uninstantiate(drbg);
1511 return ret;
1512 }
1513
1514 /*
1515 * DRBG uninstantiate function as required by SP800-90A - this function
1516 * frees all buffers and the DRBG handle
1517 *
1518 * @drbg DRBG state handle
1519 *
1520 * return
1521 * 0 on success
1522 */
1523 static int drbg_uninstantiate(struct drbg_state *drbg)
1524 {
1525 if (drbg->random_ready.func) {
1526 del_random_ready_callback(&drbg->random_ready);
1527 cancel_work_sync(&drbg->seed_work);
1528 crypto_free_rng(drbg->jent);
1529 drbg->jent = NULL;
1530 }
1531
1532 if (drbg->d_ops)
1533 drbg->d_ops->crypto_fini(drbg);
1534 drbg_dealloc_state(drbg);
1535 /* no scrubbing of test_data -- this shall survive an uninstantiate */
1536 return 0;
1537 }
1538
1539 /*
1540 * Helper function for setting the test data in the DRBG
1541 *
1542 * @drbg DRBG state handle
1543 * @data test data
1544 * @len test data length
1545 */
1546 static void drbg_kcapi_set_entropy(struct crypto_rng *tfm,
1547 const u8 *data, unsigned int len)
1548 {
1549 struct drbg_state *drbg = crypto_rng_ctx(tfm);
1550
1551 mutex_lock(&drbg->drbg_mutex);
1552 drbg_string_fill(&drbg->test_data, data, len);
1553 mutex_unlock(&drbg->drbg_mutex);
1554 }
1555
1556 /***************************************************************
1557 * Kernel crypto API cipher invocations requested by DRBG
1558 ***************************************************************/
1559
1560 #if defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_HMAC)
1561 struct sdesc {
1562 struct shash_desc shash;
1563 char ctx[];
1564 };
1565
1566 static int drbg_init_hash_kernel(struct drbg_state *drbg)
1567 {
1568 struct sdesc *sdesc;
1569 struct crypto_shash *tfm;
1570
1571 tfm = crypto_alloc_shash(drbg->core->backend_cra_name, 0, 0);
1572 if (IS_ERR(tfm)) {
1573 pr_info("DRBG: could not allocate digest TFM handle: %s\n",
1574 drbg->core->backend_cra_name);
1575 return PTR_ERR(tfm);
1576 }
1577 BUG_ON(drbg_blocklen(drbg) != crypto_shash_digestsize(tfm));
1578 sdesc = kzalloc(sizeof(struct shash_desc) + crypto_shash_descsize(tfm),
1579 GFP_KERNEL);
1580 if (!sdesc) {
1581 crypto_free_shash(tfm);
1582 return -ENOMEM;
1583 }
1584
1585 sdesc->shash.tfm = tfm;
1586 sdesc->shash.flags = 0;
1587 drbg->priv_data = sdesc;
1588 return 0;
1589 }
1590
1591 static int drbg_fini_hash_kernel(struct drbg_state *drbg)
1592 {
1593 struct sdesc *sdesc = (struct sdesc *)drbg->priv_data;
1594 if (sdesc) {
1595 crypto_free_shash(sdesc->shash.tfm);
1596 kzfree(sdesc);
1597 }
1598 drbg->priv_data = NULL;
1599 return 0;
1600 }
1601
1602 static void drbg_kcapi_hmacsetkey(struct drbg_state *drbg,
1603 const unsigned char *key)
1604 {
1605 struct sdesc *sdesc = (struct sdesc *)drbg->priv_data;
1606
1607 crypto_shash_setkey(sdesc->shash.tfm, key, drbg_statelen(drbg));
1608 }
1609
1610 static int drbg_kcapi_hash(struct drbg_state *drbg, unsigned char *outval,
1611 const struct list_head *in)
1612 {
1613 struct sdesc *sdesc = (struct sdesc *)drbg->priv_data;
1614 struct drbg_string *input = NULL;
1615
1616 crypto_shash_init(&sdesc->shash);
1617 list_for_each_entry(input, in, list)
1618 crypto_shash_update(&sdesc->shash, input->buf, input->len);
1619 return crypto_shash_final(&sdesc->shash, outval);
1620 }
1621 #endif /* (CONFIG_CRYPTO_DRBG_HASH || CONFIG_CRYPTO_DRBG_HMAC) */
1622
1623 #ifdef CONFIG_CRYPTO_DRBG_CTR
1624 static int drbg_fini_sym_kernel(struct drbg_state *drbg)
1625 {
1626 struct crypto_cipher *tfm =
1627 (struct crypto_cipher *)drbg->priv_data;
1628 if (tfm)
1629 crypto_free_cipher(tfm);
1630 drbg->priv_data = NULL;
1631
1632 if (drbg->ctr_handle)
1633 crypto_free_skcipher(drbg->ctr_handle);
1634 drbg->ctr_handle = NULL;
1635
1636 if (drbg->ctr_req)
1637 skcipher_request_free(drbg->ctr_req);;
1638 drbg->ctr_req = NULL;
1639
1640 kfree(drbg->ctr_null_value_buf);
1641 drbg->ctr_null_value = NULL;
1642
1643 return 0;
1644 }
1645
1646 static void drbg_skcipher_cb(struct crypto_async_request *req, int error)
1647 {
1648 struct drbg_state *drbg = req->data;
1649
1650 if (error == -EINPROGRESS)
1651 return;
1652 drbg->ctr_async_err = error;
1653 complete(&drbg->ctr_completion);
1654 }
1655
1656 #define DRBG_CTR_NULL_LEN 128
1657 static int drbg_init_sym_kernel(struct drbg_state *drbg)
1658 {
1659 struct crypto_cipher *tfm;
1660 struct crypto_skcipher *sk_tfm;
1661 struct skcipher_request *req;
1662 unsigned int alignmask;
1663 char ctr_name[CRYPTO_MAX_ALG_NAME];
1664
1665 tfm = crypto_alloc_cipher(drbg->core->backend_cra_name, 0, 0);
1666 if (IS_ERR(tfm)) {
1667 pr_info("DRBG: could not allocate cipher TFM handle: %s\n",
1668 drbg->core->backend_cra_name);
1669 return PTR_ERR(tfm);
1670 }
1671 BUG_ON(drbg_blocklen(drbg) != crypto_cipher_blocksize(tfm));
1672 drbg->priv_data = tfm;
1673
1674 if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)",
1675 drbg->core->backend_cra_name) >= CRYPTO_MAX_ALG_NAME) {
1676 drbg_fini_sym_kernel(drbg);
1677 return -EINVAL;
1678 }
1679 sk_tfm = crypto_alloc_skcipher(ctr_name, 0, 0);
1680 if (IS_ERR(sk_tfm)) {
1681 pr_info("DRBG: could not allocate CTR cipher TFM handle: %s\n",
1682 ctr_name);
1683 drbg_fini_sym_kernel(drbg);
1684 return PTR_ERR(sk_tfm);
1685 }
1686 drbg->ctr_handle = sk_tfm;
1687
1688 req = skcipher_request_alloc(sk_tfm, GFP_KERNEL);
1689 if (!req) {
1690 pr_info("DRBG: could not allocate request queue\n");
1691 drbg_fini_sym_kernel(drbg);
1692 return PTR_ERR(req);
1693 }
1694 drbg->ctr_req = req;
1695 skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1696 drbg_skcipher_cb, drbg);
1697
1698 alignmask = crypto_skcipher_alignmask(sk_tfm);
1699 drbg->ctr_null_value_buf = kzalloc(DRBG_CTR_NULL_LEN + alignmask,
1700 GFP_KERNEL);
1701 if (!drbg->ctr_null_value_buf) {
1702 drbg_fini_sym_kernel(drbg);
1703 return -ENOMEM;
1704 }
1705 drbg->ctr_null_value = (u8 *)PTR_ALIGN(drbg->ctr_null_value_buf,
1706 alignmask + 1);
1707
1708 return 0;
1709 }
1710
1711 static void drbg_kcapi_symsetkey(struct drbg_state *drbg,
1712 const unsigned char *key)
1713 {
1714 struct crypto_cipher *tfm =
1715 (struct crypto_cipher *)drbg->priv_data;
1716
1717 crypto_cipher_setkey(tfm, key, (drbg_keylen(drbg)));
1718 }
1719
1720 static int drbg_kcapi_sym(struct drbg_state *drbg, unsigned char *outval,
1721 const struct drbg_string *in)
1722 {
1723 struct crypto_cipher *tfm =
1724 (struct crypto_cipher *)drbg->priv_data;
1725
1726 /* there is only component in *in */
1727 BUG_ON(in->len < drbg_blocklen(drbg));
1728 crypto_cipher_encrypt_one(tfm, outval, in->buf);
1729 return 0;
1730 }
1731
1732 static int drbg_kcapi_sym_ctr(struct drbg_state *drbg, u8 *outbuf, u32 outlen)
1733 {
1734 struct scatterlist sg_in;
1735
1736 sg_init_one(&sg_in, drbg->ctr_null_value, DRBG_CTR_NULL_LEN);
1737
1738 while (outlen) {
1739 u32 cryptlen = min_t(u32, outlen, DRBG_CTR_NULL_LEN);
1740 struct scatterlist sg_out;
1741 int ret;
1742
1743 sg_init_one(&sg_out, outbuf, cryptlen);
1744 skcipher_request_set_crypt(drbg->ctr_req, &sg_in, &sg_out,
1745 cryptlen, drbg->V);
1746 ret = crypto_skcipher_encrypt(drbg->ctr_req);
1747 switch (ret) {
1748 case 0:
1749 break;
1750 case -EINPROGRESS:
1751 case -EBUSY:
1752 ret = wait_for_completion_interruptible(
1753 &drbg->ctr_completion);
1754 if (!ret && !drbg->ctr_async_err) {
1755 reinit_completion(&drbg->ctr_completion);
1756 break;
1757 }
1758 default:
1759 return ret;
1760 }
1761 init_completion(&drbg->ctr_completion);
1762
1763 outlen -= cryptlen;
1764 }
1765
1766 return 0;
1767 }
1768 #endif /* CONFIG_CRYPTO_DRBG_CTR */
1769
1770 /***************************************************************
1771 * Kernel crypto API interface to register DRBG
1772 ***************************************************************/
1773
1774 /*
1775 * Look up the DRBG flags by given kernel crypto API cra_name
1776 * The code uses the drbg_cores definition to do this
1777 *
1778 * @cra_name kernel crypto API cra_name
1779 * @coreref reference to integer which is filled with the pointer to
1780 * the applicable core
1781 * @pr reference for setting prediction resistance
1782 *
1783 * return: flags
1784 */
1785 static inline void drbg_convert_tfm_core(const char *cra_driver_name,
1786 int *coreref, bool *pr)
1787 {
1788 int i = 0;
1789 size_t start = 0;
1790 int len = 0;
1791
1792 *pr = true;
1793 /* disassemble the names */
1794 if (!memcmp(cra_driver_name, "drbg_nopr_", 10)) {
1795 start = 10;
1796 *pr = false;
1797 } else if (!memcmp(cra_driver_name, "drbg_pr_", 8)) {
1798 start = 8;
1799 } else {
1800 return;
1801 }
1802
1803 /* remove the first part */
1804 len = strlen(cra_driver_name) - start;
1805 for (i = 0; ARRAY_SIZE(drbg_cores) > i; i++) {
1806 if (!memcmp(cra_driver_name + start, drbg_cores[i].cra_name,
1807 len)) {
1808 *coreref = i;
1809 return;
1810 }
1811 }
1812 }
1813
1814 static int drbg_kcapi_init(struct crypto_tfm *tfm)
1815 {
1816 struct drbg_state *drbg = crypto_tfm_ctx(tfm);
1817
1818 mutex_init(&drbg->drbg_mutex);
1819
1820 return 0;
1821 }
1822
1823 static void drbg_kcapi_cleanup(struct crypto_tfm *tfm)
1824 {
1825 drbg_uninstantiate(crypto_tfm_ctx(tfm));
1826 }
1827
1828 /*
1829 * Generate random numbers invoked by the kernel crypto API:
1830 * The API of the kernel crypto API is extended as follows:
1831 *
1832 * src is additional input supplied to the RNG.
1833 * slen is the length of src.
1834 * dst is the output buffer where random data is to be stored.
1835 * dlen is the length of dst.
1836 */
1837 static int drbg_kcapi_random(struct crypto_rng *tfm,
1838 const u8 *src, unsigned int slen,
1839 u8 *dst, unsigned int dlen)
1840 {
1841 struct drbg_state *drbg = crypto_rng_ctx(tfm);
1842 struct drbg_string *addtl = NULL;
1843 struct drbg_string string;
1844
1845 if (slen) {
1846 /* linked list variable is now local to allow modification */
1847 drbg_string_fill(&string, src, slen);
1848 addtl = &string;
1849 }
1850
1851 return drbg_generate_long(drbg, dst, dlen, addtl);
1852 }
1853
1854 /*
1855 * Seed the DRBG invoked by the kernel crypto API
1856 */
1857 static int drbg_kcapi_seed(struct crypto_rng *tfm,
1858 const u8 *seed, unsigned int slen)
1859 {
1860 struct drbg_state *drbg = crypto_rng_ctx(tfm);
1861 struct crypto_tfm *tfm_base = crypto_rng_tfm(tfm);
1862 bool pr = false;
1863 struct drbg_string string;
1864 struct drbg_string *seed_string = NULL;
1865 int coreref = 0;
1866
1867 drbg_convert_tfm_core(crypto_tfm_alg_driver_name(tfm_base), &coreref,
1868 &pr);
1869 if (0 < slen) {
1870 drbg_string_fill(&string, seed, slen);
1871 seed_string = &string;
1872 }
1873
1874 return drbg_instantiate(drbg, seed_string, coreref, pr);
1875 }
1876
1877 /***************************************************************
1878 * Kernel module: code to load the module
1879 ***************************************************************/
1880
1881 /*
1882 * Tests as defined in 11.3.2 in addition to the cipher tests: testing
1883 * of the error handling.
1884 *
1885 * Note: testing of failing seed source as defined in 11.3.2 is not applicable
1886 * as seed source of get_random_bytes does not fail.
1887 *
1888 * Note 2: There is no sensible way of testing the reseed counter
1889 * enforcement, so skip it.
1890 */
1891 static inline int __init drbg_healthcheck_sanity(void)
1892 {
1893 int len = 0;
1894 #define OUTBUFLEN 16
1895 unsigned char buf[OUTBUFLEN];
1896 struct drbg_state *drbg = NULL;
1897 int ret = -EFAULT;
1898 int rc = -EFAULT;
1899 bool pr = false;
1900 int coreref = 0;
1901 struct drbg_string addtl;
1902 size_t max_addtllen, max_request_bytes;
1903
1904 /* only perform test in FIPS mode */
1905 if (!fips_enabled)
1906 return 0;
1907
1908 #ifdef CONFIG_CRYPTO_DRBG_CTR
1909 drbg_convert_tfm_core("drbg_nopr_ctr_aes128", &coreref, &pr);
1910 #elif defined CONFIG_CRYPTO_DRBG_HASH
1911 drbg_convert_tfm_core("drbg_nopr_sha256", &coreref, &pr);
1912 #else
1913 drbg_convert_tfm_core("drbg_nopr_hmac_sha256", &coreref, &pr);
1914 #endif
1915
1916 drbg = kzalloc(sizeof(struct drbg_state), GFP_KERNEL);
1917 if (!drbg)
1918 return -ENOMEM;
1919
1920 mutex_init(&drbg->drbg_mutex);
1921
1922 /*
1923 * if the following tests fail, it is likely that there is a buffer
1924 * overflow as buf is much smaller than the requested or provided
1925 * string lengths -- in case the error handling does not succeed
1926 * we may get an OOPS. And we want to get an OOPS as this is a
1927 * grave bug.
1928 */
1929
1930 /* get a valid instance of DRBG for following tests */
1931 ret = drbg_instantiate(drbg, NULL, coreref, pr);
1932 if (ret) {
1933 rc = ret;
1934 goto outbuf;
1935 }
1936 max_addtllen = drbg_max_addtl(drbg);
1937 max_request_bytes = drbg_max_request_bytes(drbg);
1938 drbg_string_fill(&addtl, buf, max_addtllen + 1);
1939 /* overflow addtllen with additonal info string */
1940 len = drbg_generate(drbg, buf, OUTBUFLEN, &addtl);
1941 BUG_ON(0 < len);
1942 /* overflow max_bits */
1943 len = drbg_generate(drbg, buf, (max_request_bytes + 1), NULL);
1944 BUG_ON(0 < len);
1945 drbg_uninstantiate(drbg);
1946
1947 /* overflow max addtllen with personalization string */
1948 ret = drbg_instantiate(drbg, &addtl, coreref, pr);
1949 BUG_ON(0 == ret);
1950 /* all tests passed */
1951 rc = 0;
1952
1953 pr_devel("DRBG: Sanity tests for failure code paths successfully "
1954 "completed\n");
1955
1956 drbg_uninstantiate(drbg);
1957 outbuf:
1958 kzfree(drbg);
1959 return rc;
1960 }
1961
1962 static struct rng_alg drbg_algs[22];
1963
1964 /*
1965 * Fill the array drbg_algs used to register the different DRBGs
1966 * with the kernel crypto API. To fill the array, the information
1967 * from drbg_cores[] is used.
1968 */
1969 static inline void __init drbg_fill_array(struct rng_alg *alg,
1970 const struct drbg_core *core, int pr)
1971 {
1972 int pos = 0;
1973 static int priority = 200;
1974
1975 memcpy(alg->base.cra_name, "stdrng", 6);
1976 if (pr) {
1977 memcpy(alg->base.cra_driver_name, "drbg_pr_", 8);
1978 pos = 8;
1979 } else {
1980 memcpy(alg->base.cra_driver_name, "drbg_nopr_", 10);
1981 pos = 10;
1982 }
1983 memcpy(alg->base.cra_driver_name + pos, core->cra_name,
1984 strlen(core->cra_name));
1985
1986 alg->base.cra_priority = priority;
1987 priority++;
1988 /*
1989 * If FIPS mode enabled, the selected DRBG shall have the
1990 * highest cra_priority over other stdrng instances to ensure
1991 * it is selected.
1992 */
1993 if (fips_enabled)
1994 alg->base.cra_priority += 200;
1995
1996 alg->base.cra_ctxsize = sizeof(struct drbg_state);
1997 alg->base.cra_module = THIS_MODULE;
1998 alg->base.cra_init = drbg_kcapi_init;
1999 alg->base.cra_exit = drbg_kcapi_cleanup;
2000 alg->generate = drbg_kcapi_random;
2001 alg->seed = drbg_kcapi_seed;
2002 alg->set_ent = drbg_kcapi_set_entropy;
2003 alg->seedsize = 0;
2004 }
2005
2006 static int __init drbg_init(void)
2007 {
2008 unsigned int i = 0; /* pointer to drbg_algs */
2009 unsigned int j = 0; /* pointer to drbg_cores */
2010 int ret = -EFAULT;
2011
2012 ret = drbg_healthcheck_sanity();
2013 if (ret)
2014 return ret;
2015
2016 if (ARRAY_SIZE(drbg_cores) * 2 > ARRAY_SIZE(drbg_algs)) {
2017 pr_info("DRBG: Cannot register all DRBG types"
2018 "(slots needed: %zu, slots available: %zu)\n",
2019 ARRAY_SIZE(drbg_cores) * 2, ARRAY_SIZE(drbg_algs));
2020 return ret;
2021 }
2022
2023 /*
2024 * each DRBG definition can be used with PR and without PR, thus
2025 * we instantiate each DRBG in drbg_cores[] twice.
2026 *
2027 * As the order of placing them into the drbg_algs array matters
2028 * (the later DRBGs receive a higher cra_priority) we register the
2029 * prediction resistance DRBGs first as the should not be too
2030 * interesting.
2031 */
2032 for (j = 0; ARRAY_SIZE(drbg_cores) > j; j++, i++)
2033 drbg_fill_array(&drbg_algs[i], &drbg_cores[j], 1);
2034 for (j = 0; ARRAY_SIZE(drbg_cores) > j; j++, i++)
2035 drbg_fill_array(&drbg_algs[i], &drbg_cores[j], 0);
2036 return crypto_register_rngs(drbg_algs, (ARRAY_SIZE(drbg_cores) * 2));
2037 }
2038
2039 static void __exit drbg_exit(void)
2040 {
2041 crypto_unregister_rngs(drbg_algs, (ARRAY_SIZE(drbg_cores) * 2));
2042 }
2043
2044 module_init(drbg_init);
2045 module_exit(drbg_exit);
2046 #ifndef CRYPTO_DRBG_HASH_STRING
2047 #define CRYPTO_DRBG_HASH_STRING ""
2048 #endif
2049 #ifndef CRYPTO_DRBG_HMAC_STRING
2050 #define CRYPTO_DRBG_HMAC_STRING ""
2051 #endif
2052 #ifndef CRYPTO_DRBG_CTR_STRING
2053 #define CRYPTO_DRBG_CTR_STRING ""
2054 #endif
2055 MODULE_LICENSE("GPL");
2056 MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
2057 MODULE_DESCRIPTION("NIST SP800-90A Deterministic Random Bit Generator (DRBG) "
2058 "using following cores: "
2059 CRYPTO_DRBG_HASH_STRING
2060 CRYPTO_DRBG_HMAC_STRING
2061 CRYPTO_DRBG_CTR_STRING);
2062 MODULE_ALIAS_CRYPTO("stdrng");