2 * DRBG: Deterministic Random Bits Generator
3 * Based on NIST Recommended DRBG from NIST SP800-90A with the following
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
10 * Copyright Stephan Mueller <smueller@chronox.de>, 2014
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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
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.)
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
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.
51 * Usage without any additional data
52 * ---------------------------------
53 * struct crypto_rng *drng;
57 * drng = crypto_alloc_rng(drng_name, 0, 0);
58 * err = crypto_rng_get_bytes(drng, &data, DATALEN);
59 * crypto_free_rng(drng);
62 * Usage with personalization string during initialization
63 * -------------------------------------------------------
64 * struct crypto_rng *drng;
67 * struct drbg_string pers;
68 * char personalization[11] = "some-string";
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);
79 * Usage with additional information string during random number request
80 * ---------------------------------------------------------------------
81 * struct crypto_rng *drng;
84 * char addtl_string[11] = "some-string";
85 * string drbg_string addtl;
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);
95 * Usage with personalization and additional information strings
96 * -------------------------------------------------------------
97 * Just mix both scenarios above.
100 #include <crypto/drbg.h>
102 #if !defined(CONFIG_CRYPTO_DRBG_HASH) && \
103 !defined(CONFIG_CRYPTO_DRBG_HMAC) && \
104 !defined(CONFIG_CRYPTO_DRBG_CTR)
105 #warning "The DRBG code is useless without compiling at least one DRBG type"
108 /***************************************************************
109 * Backend cipher definitions available to DRBG
110 ***************************************************************/
113 * The order of the DRBG definitions here matter: every DRBG is registered
114 * as stdrng. Each DRBG receives an increasing cra_priority values the later
115 * they are defined in this array (see drbg_fill_array).
117 * HMAC DRBGs are favored over Hash DRBGs over CTR DRBGs, and
118 * the SHA256 / AES 256 over other ciphers. Thus, the favored
119 * DRBGs are the latest entries in this array.
121 static const struct drbg_core drbg_cores[] = {
122 #ifdef CONFIG_CRYPTO_DRBG_CTR
124 .flags = DRBG_CTR | DRBG_STRENGTH128,
125 .statelen = 32, /* 256 bits as defined in 10.2.1 */
129 .blocklen_bytes = 16,
130 .cra_name = "ctr_aes128",
131 .backend_cra_name = "ecb(aes)",
133 .flags = DRBG_CTR | DRBG_STRENGTH192,
134 .statelen = 40, /* 320 bits as defined in 10.2.1 */
138 .blocklen_bytes = 16,
139 .cra_name = "ctr_aes192",
140 .backend_cra_name = "ecb(aes)",
142 .flags = DRBG_CTR | DRBG_STRENGTH256,
143 .statelen = 48, /* 384 bits as defined in 10.2.1 */
147 .blocklen_bytes = 16,
148 .cra_name = "ctr_aes256",
149 .backend_cra_name = "ecb(aes)",
151 #endif /* CONFIG_CRYPTO_DRBG_CTR */
152 #ifdef CONFIG_CRYPTO_DRBG_HASH
154 .flags = DRBG_HASH | DRBG_STRENGTH128,
155 .statelen = 55, /* 440 bits */
159 .blocklen_bytes = 20,
161 .backend_cra_name = "sha1",
163 .flags = DRBG_HASH | DRBG_STRENGTH256,
164 .statelen = 111, /* 888 bits */
168 .blocklen_bytes = 48,
169 .cra_name = "sha384",
170 .backend_cra_name = "sha384",
172 .flags = DRBG_HASH | DRBG_STRENGTH256,
173 .statelen = 111, /* 888 bits */
177 .blocklen_bytes = 64,
178 .cra_name = "sha512",
179 .backend_cra_name = "sha512",
181 .flags = DRBG_HASH | DRBG_STRENGTH256,
182 .statelen = 55, /* 440 bits */
186 .blocklen_bytes = 32,
187 .cra_name = "sha256",
188 .backend_cra_name = "sha256",
190 #endif /* CONFIG_CRYPTO_DRBG_HASH */
191 #ifdef CONFIG_CRYPTO_DRBG_HMAC
193 .flags = DRBG_HMAC | DRBG_STRENGTH256,
194 .statelen = 20, /* block length of cipher */
198 .blocklen_bytes = 20,
199 .cra_name = "hmac_sha1",
200 .backend_cra_name = "hmac(sha1)",
202 .flags = DRBG_HMAC | DRBG_STRENGTH256,
203 .statelen = 48, /* block length of cipher */
207 .blocklen_bytes = 48,
208 .cra_name = "hmac_sha384",
209 .backend_cra_name = "hmac(sha384)",
211 .flags = DRBG_HMAC | DRBG_STRENGTH256,
212 .statelen = 64, /* block length of cipher */
216 .blocklen_bytes = 64,
217 .cra_name = "hmac_sha512",
218 .backend_cra_name = "hmac(sha512)",
220 .flags = DRBG_HMAC | DRBG_STRENGTH256,
221 .statelen = 32, /* block length of cipher */
225 .blocklen_bytes = 32,
226 .cra_name = "hmac_sha256",
227 .backend_cra_name = "hmac(sha256)",
229 #endif /* CONFIG_CRYPTO_DRBG_HMAC */
232 /******************************************************************
233 * Generic helper functions
234 ******************************************************************/
237 * Return strength of DRBG according to SP800-90A section 8.4
239 * @flags DRBG flags reference
241 * Return: normalized strength in *bytes* value or 32 as default
242 * to counter programming errors
244 static inline unsigned short drbg_sec_strength(drbg_flag_t flags)
246 switch (flags & DRBG_STRENGTH_MASK) {
247 case DRBG_STRENGTH128:
249 case DRBG_STRENGTH192:
251 case DRBG_STRENGTH256:
259 * FIPS 140-2 continuous self test
260 * The test is performed on the result of one round of the output
261 * function. Thus, the function implicitly knows the size of the
264 * The FIPS test can be called in an endless loop until it returns
265 * true. Although the code looks like a potential for a deadlock, it
266 * is not the case, because returning a false cannot mathematically
267 * occur (except once when a reseed took place and the updated state
268 * would is now set up such that the generation of new value returns
269 * an identical one -- this is most unlikely and would happen only once).
270 * Thus, if this function repeatedly returns false and thus would cause
271 * a deadlock, the integrity of the entire kernel is lost.
274 * @buf output buffer of random data to be checked
280 static bool drbg_fips_continuous_test(struct drbg_state *drbg,
281 const unsigned char *buf)
283 #ifdef CONFIG_CRYPTO_FIPS
285 /* skip test if we test the overall system */
288 /* only perform test in FIPS mode */
289 if (0 == fips_enabled)
291 if (!drbg->fips_primed) {
292 /* Priming of FIPS test */
293 memcpy(drbg->prev, buf, drbg_blocklen(drbg));
294 drbg->fips_primed = true;
295 /* return false due to priming, i.e. another round is needed */
298 ret = memcmp(drbg->prev, buf, drbg_blocklen(drbg));
299 memcpy(drbg->prev, buf, drbg_blocklen(drbg));
300 /* the test shall pass when the two compared values are not equal */
304 #endif /* CONFIG_CRYPTO_FIPS */
308 * Convert an integer into a byte representation of this integer.
309 * The byte representation is big-endian
311 * @buf buffer holding the converted integer
312 * @val value to be converted
313 * @buflen length of buffer
315 #if (defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_CTR))
316 static inline void drbg_int2byte(unsigned char *buf, uint64_t val,
322 byte = buf + (buflen - 1);
323 for (i = 0; i < buflen; i++)
324 *(byte--) = val >> (i * 8) & 0xff;
330 * @dst buffer to increment
333 static inline void drbg_add_buf(unsigned char *dst, size_t dstlen,
334 const unsigned char *add, size_t addlen)
336 /* implied: dstlen > addlen */
337 unsigned char *dstptr;
338 const unsigned char *addptr;
339 unsigned int remainder = 0;
342 dstptr = dst + (dstlen-1);
343 addptr = add + (addlen-1);
345 remainder += *dstptr + *addptr;
346 *dstptr = remainder & 0xff;
348 len--; dstptr--; addptr--;
350 len = dstlen - addlen;
351 while (len && remainder > 0) {
352 remainder = *dstptr + 1;
353 *dstptr = remainder & 0xff;
358 #endif /* defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_CTR) */
360 /******************************************************************
361 * CTR DRBG callback functions
362 ******************************************************************/
364 #ifdef CONFIG_CRYPTO_DRBG_CTR
365 static int drbg_kcapi_sym(struct drbg_state *drbg, const unsigned char *key,
366 unsigned char *outval, const struct drbg_string *in);
367 static int drbg_init_sym_kernel(struct drbg_state *drbg);
368 static int drbg_fini_sym_kernel(struct drbg_state *drbg);
370 /* BCC function for CTR DRBG as defined in 10.4.3 */
371 static int drbg_ctr_bcc(struct drbg_state *drbg,
372 unsigned char *out, const unsigned char *key,
373 struct list_head *in)
376 struct drbg_string *curr = NULL;
377 struct drbg_string data;
380 drbg_string_fill(&data, out, drbg_blocklen(drbg));
383 memset(out, 0, drbg_blocklen(drbg));
385 /* 10.4.3 step 2 / 4 */
386 list_for_each_entry(curr, in, list) {
387 const unsigned char *pos = curr->buf;
388 size_t len = curr->len;
389 /* 10.4.3 step 4.1 */
391 /* 10.4.3 step 4.2 */
392 if (drbg_blocklen(drbg) == cnt) {
394 ret = drbg_kcapi_sym(drbg, key, out, &data);
404 /* 10.4.3 step 4.2 for last block */
406 ret = drbg_kcapi_sym(drbg, key, out, &data);
412 * scratchpad usage: drbg_ctr_update is interlinked with drbg_ctr_df
413 * (and drbg_ctr_bcc, but this function does not need any temporary buffers),
414 * the scratchpad is used as follows:
417 * start: drbg->scratchpad
418 * length: drbg_statelen(drbg) + drbg_blocklen(drbg)
419 * note: the cipher writing into this variable works
420 * blocklen-wise. Now, when the statelen is not a multiple
421 * of blocklen, the generateion loop below "spills over"
422 * by at most blocklen. Thus, we need to give sufficient
425 * start: drbg->scratchpad +
426 * drbg_statelen(drbg) + drbg_blocklen(drbg)
427 * length: drbg_statelen(drbg)
431 * start: df_data + drbg_statelen(drbg)
432 * length: drbg_blocklen(drbg)
434 * start: pad + drbg_blocklen(drbg)
435 * length: drbg_blocklen(drbg)
437 * start: iv + drbg_blocklen(drbg)
438 * length: drbg_satelen(drbg) + drbg_blocklen(drbg)
439 * note: temp is the buffer that the BCC function operates
440 * on. BCC operates blockwise. drbg_statelen(drbg)
441 * is sufficient when the DRBG state length is a multiple
442 * of the block size. For AES192 (and maybe other ciphers)
443 * this is not correct and the length for temp is
444 * insufficient (yes, that also means for such ciphers,
445 * the final output of all BCC rounds are truncated).
446 * Therefore, add drbg_blocklen(drbg) to cover all
450 /* Derivation Function for CTR DRBG as defined in 10.4.2 */
451 static int drbg_ctr_df(struct drbg_state *drbg,
452 unsigned char *df_data, size_t bytes_to_return,
453 struct list_head *seedlist)
456 unsigned char L_N[8];
458 struct drbg_string S1, S2, S4, cipherin;
460 unsigned char *pad = df_data + drbg_statelen(drbg);
461 unsigned char *iv = pad + drbg_blocklen(drbg);
462 unsigned char *temp = iv + drbg_blocklen(drbg);
464 unsigned int templen = 0;
468 const unsigned char *K = (unsigned char *)
469 "\x00\x01\x02\x03\x04\x05\x06\x07"
470 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
471 "\x10\x11\x12\x13\x14\x15\x16\x17"
472 "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f";
474 size_t generated_len = 0;
476 struct drbg_string *seed = NULL;
478 memset(pad, 0, drbg_blocklen(drbg));
479 memset(iv, 0, drbg_blocklen(drbg));
480 memset(temp, 0, drbg_statelen(drbg));
482 /* 10.4.2 step 1 is implicit as we work byte-wise */
485 if ((512/8) < bytes_to_return)
488 /* 10.4.2 step 2 -- calculate the entire length of all input data */
489 list_for_each_entry(seed, seedlist, list)
490 inputlen += seed->len;
491 drbg_int2byte(&L_N[0], inputlen, 4);
494 drbg_int2byte(&L_N[4], bytes_to_return, 4);
496 /* 10.4.2 step 5: length is L_N, input_string, one byte, padding */
497 padlen = (inputlen + sizeof(L_N) + 1) % (drbg_blocklen(drbg));
498 /* wrap the padlen appropriately */
500 padlen = drbg_blocklen(drbg) - padlen;
502 * pad / padlen contains the 0x80 byte and the following zero bytes.
503 * As the calculated padlen value only covers the number of zero
504 * bytes, this value has to be incremented by one for the 0x80 byte.
509 /* 10.4.2 step 4 -- first fill the linked list and then order it */
510 drbg_string_fill(&S1, iv, drbg_blocklen(drbg));
511 list_add_tail(&S1.list, &bcc_list);
512 drbg_string_fill(&S2, L_N, sizeof(L_N));
513 list_add_tail(&S2.list, &bcc_list);
514 list_splice_tail(seedlist, &bcc_list);
515 drbg_string_fill(&S4, pad, padlen);
516 list_add_tail(&S4.list, &bcc_list);
519 while (templen < (drbg_keylen(drbg) + (drbg_blocklen(drbg)))) {
521 * 10.4.2 step 9.1 - the padding is implicit as the buffer
522 * holds zeros after allocation -- even the increment of i
523 * is irrelevant as the increment remains within length of i
525 drbg_int2byte(iv, i, 4);
526 /* 10.4.2 step 9.2 -- BCC and concatenation with temp */
527 ret = drbg_ctr_bcc(drbg, temp + templen, K, &bcc_list);
530 /* 10.4.2 step 9.3 */
532 templen += drbg_blocklen(drbg);
536 X = temp + (drbg_keylen(drbg));
537 drbg_string_fill(&cipherin, X, drbg_blocklen(drbg));
539 /* 10.4.2 step 12: overwriting of outval is implemented in next step */
542 while (generated_len < bytes_to_return) {
545 * 10.4.2 step 13.1: the truncation of the key length is
546 * implicit as the key is only drbg_blocklen in size based on
547 * the implementation of the cipher function callback
549 ret = drbg_kcapi_sym(drbg, temp, X, &cipherin);
552 blocklen = (drbg_blocklen(drbg) <
553 (bytes_to_return - generated_len)) ?
554 drbg_blocklen(drbg) :
555 (bytes_to_return - generated_len);
556 /* 10.4.2 step 13.2 and 14 */
557 memcpy(df_data + generated_len, X, blocklen);
558 generated_len += blocklen;
564 memset(iv, 0, drbg_blocklen(drbg));
565 memset(temp, 0, drbg_statelen(drbg));
566 memset(pad, 0, drbg_blocklen(drbg));
570 /* update function of CTR DRBG as defined in 10.2.1.2 */
571 static int drbg_ctr_update(struct drbg_state *drbg, struct list_head *seed,
575 /* 10.2.1.2 step 1 */
576 unsigned char *temp = drbg->scratchpad;
577 unsigned char *df_data = drbg->scratchpad + drbg_statelen(drbg) +
579 unsigned char *temp_p, *df_data_p; /* pointer to iterate over buffers */
580 unsigned int len = 0;
581 struct drbg_string cipherin;
582 unsigned char prefix = DRBG_PREFIX1;
584 memset(temp, 0, drbg_statelen(drbg) + drbg_blocklen(drbg));
585 memset(df_data, 0, drbg_statelen(drbg));
587 /* 10.2.1.3.2 step 2 and 10.2.1.4.2 step 2 */
589 ret = drbg_ctr_df(drbg, df_data, drbg_statelen(drbg), seed);
594 drbg_string_fill(&cipherin, drbg->V, drbg_blocklen(drbg));
596 * 10.2.1.3.2 steps 2 and 3 are already covered as the allocation
597 * zeroizes all memory during initialization
599 while (len < (drbg_statelen(drbg))) {
600 /* 10.2.1.2 step 2.1 */
601 drbg_add_buf(drbg->V, drbg_blocklen(drbg), &prefix, 1);
603 * 10.2.1.2 step 2.2 */
604 ret = drbg_kcapi_sym(drbg, drbg->C, temp + len, &cipherin);
607 /* 10.2.1.2 step 2.3 and 3 */
608 len += drbg_blocklen(drbg);
611 /* 10.2.1.2 step 4 */
614 for (len = 0; len < drbg_statelen(drbg); len++) {
615 *temp_p ^= *df_data_p;
616 df_data_p++; temp_p++;
619 /* 10.2.1.2 step 5 */
620 memcpy(drbg->C, temp, drbg_keylen(drbg));
621 /* 10.2.1.2 step 6 */
622 memcpy(drbg->V, temp + drbg_keylen(drbg), drbg_blocklen(drbg));
626 memset(temp, 0, drbg_statelen(drbg) + drbg_blocklen(drbg));
627 memset(df_data, 0, drbg_statelen(drbg));
632 * scratchpad use: drbg_ctr_update is called independently from
633 * drbg_ctr_extract_bytes. Therefore, the scratchpad is reused
635 /* Generate function of CTR DRBG as defined in 10.2.1.5.2 */
636 static int drbg_ctr_generate(struct drbg_state *drbg,
637 unsigned char *buf, unsigned int buflen,
638 struct drbg_string *addtl)
642 struct drbg_string data;
643 unsigned char prefix = DRBG_PREFIX1;
645 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
647 /* 10.2.1.5.2 step 2 */
648 if (addtl && 0 < addtl->len) {
649 LIST_HEAD(addtllist);
651 list_add_tail(&addtl->list, &addtllist);
652 ret = drbg_ctr_update(drbg, &addtllist, 1);
657 /* 10.2.1.5.2 step 4.1 */
658 drbg_add_buf(drbg->V, drbg_blocklen(drbg), &prefix, 1);
659 drbg_string_fill(&data, drbg->V, drbg_blocklen(drbg));
660 while (len < buflen) {
662 /* 10.2.1.5.2 step 4.2 */
663 ret = drbg_kcapi_sym(drbg, drbg->C, drbg->scratchpad, &data);
668 outlen = (drbg_blocklen(drbg) < (buflen - len)) ?
669 drbg_blocklen(drbg) : (buflen - len);
670 if (!drbg_fips_continuous_test(drbg, drbg->scratchpad)) {
671 /* 10.2.1.5.2 step 6 */
672 drbg_add_buf(drbg->V, drbg_blocklen(drbg), &prefix, 1);
675 /* 10.2.1.5.2 step 4.3 */
676 memcpy(buf + len, drbg->scratchpad, outlen);
678 /* 10.2.1.5.2 step 6 */
680 drbg_add_buf(drbg->V, drbg_blocklen(drbg), &prefix, 1);
685 * The following call invokes the DF function again which could be
686 * optimized. In step 2, the "additional_input" after step 2 is the
687 * output of the DF function. If this result would be saved, the DF
688 * function would not need to be invoked again at this point.
690 if (addtl && 0 < addtl->len) {
691 LIST_HEAD(addtllist);
693 list_add_tail(&addtl->list, &addtllist);
694 ret = drbg_ctr_update(drbg, &addtllist, 1);
696 ret = drbg_ctr_update(drbg, NULL, 1);
702 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
706 static struct drbg_state_ops drbg_ctr_ops = {
707 .update = drbg_ctr_update,
708 .generate = drbg_ctr_generate,
709 .crypto_init = drbg_init_sym_kernel,
710 .crypto_fini = drbg_fini_sym_kernel,
712 #endif /* CONFIG_CRYPTO_DRBG_CTR */
714 /******************************************************************
715 * HMAC DRBG callback functions
716 ******************************************************************/
718 #if defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_HMAC)
719 static int drbg_kcapi_hash(struct drbg_state *drbg, const unsigned char *key,
720 unsigned char *outval, const struct list_head *in);
721 static int drbg_init_hash_kernel(struct drbg_state *drbg);
722 static int drbg_fini_hash_kernel(struct drbg_state *drbg);
723 #endif /* (CONFIG_CRYPTO_DRBG_HASH || CONFIG_CRYPTO_DRBG_HMAC) */
725 #ifdef CONFIG_CRYPTO_DRBG_HMAC
726 /* update function of HMAC DRBG as defined in 10.1.2.2 */
727 static int drbg_hmac_update(struct drbg_state *drbg, struct list_head *seed,
732 struct drbg_string seed1, seed2, vdata;
734 LIST_HEAD(vdatalist);
737 /* 10.1.2.3 step 2 */
738 memset(drbg->C, 0, drbg_statelen(drbg));
739 memset(drbg->V, 1, drbg_statelen(drbg));
742 drbg_string_fill(&seed1, drbg->V, drbg_statelen(drbg));
743 list_add_tail(&seed1.list, &seedlist);
744 /* buffer of seed2 will be filled in for loop below with one byte */
745 drbg_string_fill(&seed2, NULL, 1);
746 list_add_tail(&seed2.list, &seedlist);
747 /* input data of seed is allowed to be NULL at this point */
749 list_splice_tail(seed, &seedlist);
751 drbg_string_fill(&vdata, drbg->V, drbg_statelen(drbg));
752 list_add_tail(&vdata.list, &vdatalist);
753 for (i = 2; 0 < i; i--) {
754 /* first round uses 0x0, second 0x1 */
755 unsigned char prefix = DRBG_PREFIX0;
757 prefix = DRBG_PREFIX1;
758 /* 10.1.2.2 step 1 and 4 -- concatenation and HMAC for key */
760 ret = drbg_kcapi_hash(drbg, drbg->C, drbg->C, &seedlist);
764 /* 10.1.2.2 step 2 and 5 -- HMAC for V */
765 ret = drbg_kcapi_hash(drbg, drbg->C, drbg->V, &vdatalist);
769 /* 10.1.2.2 step 3 */
777 /* generate function of HMAC DRBG as defined in 10.1.2.5 */
778 static int drbg_hmac_generate(struct drbg_state *drbg,
781 struct drbg_string *addtl)
785 struct drbg_string data;
788 /* 10.1.2.5 step 2 */
789 if (addtl && 0 < addtl->len) {
790 LIST_HEAD(addtllist);
792 list_add_tail(&addtl->list, &addtllist);
793 ret = drbg_hmac_update(drbg, &addtllist, 1);
798 drbg_string_fill(&data, drbg->V, drbg_statelen(drbg));
799 list_add_tail(&data.list, &datalist);
800 while (len < buflen) {
801 unsigned int outlen = 0;
802 /* 10.1.2.5 step 4.1 */
803 ret = drbg_kcapi_hash(drbg, drbg->C, drbg->V, &datalist);
806 outlen = (drbg_blocklen(drbg) < (buflen - len)) ?
807 drbg_blocklen(drbg) : (buflen - len);
808 if (!drbg_fips_continuous_test(drbg, drbg->V))
811 /* 10.1.2.5 step 4.2 */
812 memcpy(buf + len, drbg->V, outlen);
816 /* 10.1.2.5 step 6 */
817 if (addtl && 0 < addtl->len) {
818 LIST_HEAD(addtllist);
820 list_add_tail(&addtl->list, &addtllist);
821 ret = drbg_hmac_update(drbg, &addtllist, 1);
823 ret = drbg_hmac_update(drbg, NULL, 1);
831 static struct drbg_state_ops drbg_hmac_ops = {
832 .update = drbg_hmac_update,
833 .generate = drbg_hmac_generate,
834 .crypto_init = drbg_init_hash_kernel,
835 .crypto_fini = drbg_fini_hash_kernel,
838 #endif /* CONFIG_CRYPTO_DRBG_HMAC */
840 /******************************************************************
841 * Hash DRBG callback functions
842 ******************************************************************/
844 #ifdef CONFIG_CRYPTO_DRBG_HASH
846 * scratchpad usage: as drbg_hash_update and drbg_hash_df are used
847 * interlinked, the scratchpad is used as follows:
849 * start: drbg->scratchpad
850 * length: drbg_statelen(drbg)
852 * start: drbg->scratchpad + drbg_statelen(drbg)
853 * length: drbg_blocklen(drbg)
855 * drbg_hash_process_addtl uses the scratchpad, but fully completes
856 * before either of the functions mentioned before are invoked. Therefore,
857 * drbg_hash_process_addtl does not need to be specifically considered.
860 /* Derivation Function for Hash DRBG as defined in 10.4.1 */
861 static int drbg_hash_df(struct drbg_state *drbg,
862 unsigned char *outval, size_t outlen,
863 struct list_head *entropylist)
867 unsigned char input[5];
868 unsigned char *tmp = drbg->scratchpad + drbg_statelen(drbg);
869 struct drbg_string data;
871 memset(tmp, 0, drbg_blocklen(drbg));
875 drbg_int2byte(&input[1], (outlen * 8), 4);
877 /* 10.4.1 step 4.1 -- concatenation of data for input into hash */
878 drbg_string_fill(&data, input, 5);
879 list_add(&data.list, entropylist);
882 while (len < outlen) {
884 /* 10.4.1 step 4.1 */
885 ret = drbg_kcapi_hash(drbg, NULL, tmp, entropylist);
888 /* 10.4.1 step 4.2 */
890 blocklen = (drbg_blocklen(drbg) < (outlen - len)) ?
891 drbg_blocklen(drbg) : (outlen - len);
892 memcpy(outval + len, tmp, blocklen);
897 memset(tmp, 0, drbg_blocklen(drbg));
901 /* update function for Hash DRBG as defined in 10.1.1.2 / 10.1.1.3 */
902 static int drbg_hash_update(struct drbg_state *drbg, struct list_head *seed,
906 struct drbg_string data1, data2;
908 LIST_HEAD(datalist2);
909 unsigned char *V = drbg->scratchpad;
910 unsigned char prefix = DRBG_PREFIX1;
912 memset(drbg->scratchpad, 0, drbg_statelen(drbg));
917 /* 10.1.1.3 step 1 */
918 memcpy(V, drbg->V, drbg_statelen(drbg));
919 drbg_string_fill(&data1, &prefix, 1);
920 list_add_tail(&data1.list, &datalist);
921 drbg_string_fill(&data2, V, drbg_statelen(drbg));
922 list_add_tail(&data2.list, &datalist);
924 list_splice_tail(seed, &datalist);
926 /* 10.1.1.2 / 10.1.1.3 step 2 and 3 */
927 ret = drbg_hash_df(drbg, drbg->V, drbg_statelen(drbg), &datalist);
931 /* 10.1.1.2 / 10.1.1.3 step 4 */
932 prefix = DRBG_PREFIX0;
933 drbg_string_fill(&data1, &prefix, 1);
934 list_add_tail(&data1.list, &datalist2);
935 drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
936 list_add_tail(&data2.list, &datalist2);
937 /* 10.1.1.2 / 10.1.1.3 step 4 */
938 ret = drbg_hash_df(drbg, drbg->C, drbg_statelen(drbg), &datalist2);
941 memset(drbg->scratchpad, 0, drbg_statelen(drbg));
945 /* processing of additional information string for Hash DRBG */
946 static int drbg_hash_process_addtl(struct drbg_state *drbg,
947 struct drbg_string *addtl)
950 struct drbg_string data1, data2;
952 unsigned char prefix = DRBG_PREFIX2;
954 /* this is value w as per documentation */
955 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
957 /* 10.1.1.4 step 2 */
958 if (!addtl || 0 == addtl->len)
961 /* 10.1.1.4 step 2a */
962 drbg_string_fill(&data1, &prefix, 1);
963 drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
964 list_add_tail(&data1.list, &datalist);
965 list_add_tail(&data2.list, &datalist);
966 list_add_tail(&addtl->list, &datalist);
967 ret = drbg_kcapi_hash(drbg, NULL, drbg->scratchpad, &datalist);
971 /* 10.1.1.4 step 2b */
972 drbg_add_buf(drbg->V, drbg_statelen(drbg),
973 drbg->scratchpad, drbg_blocklen(drbg));
976 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
980 /* Hashgen defined in 10.1.1.4 */
981 static int drbg_hash_hashgen(struct drbg_state *drbg,
987 unsigned char *src = drbg->scratchpad;
988 unsigned char *dst = drbg->scratchpad + drbg_statelen(drbg);
989 struct drbg_string data;
991 unsigned char prefix = DRBG_PREFIX1;
993 memset(src, 0, drbg_statelen(drbg));
994 memset(dst, 0, drbg_blocklen(drbg));
996 /* 10.1.1.4 step hashgen 2 */
997 memcpy(src, drbg->V, drbg_statelen(drbg));
999 drbg_string_fill(&data, src, drbg_statelen(drbg));
1000 list_add_tail(&data.list, &datalist);
1001 while (len < buflen) {
1002 unsigned int outlen = 0;
1003 /* 10.1.1.4 step hashgen 4.1 */
1004 ret = drbg_kcapi_hash(drbg, NULL, dst, &datalist);
1009 outlen = (drbg_blocklen(drbg) < (buflen - len)) ?
1010 drbg_blocklen(drbg) : (buflen - len);
1011 if (!drbg_fips_continuous_test(drbg, dst)) {
1012 drbg_add_buf(src, drbg_statelen(drbg), &prefix, 1);
1015 /* 10.1.1.4 step hashgen 4.2 */
1016 memcpy(buf + len, dst, outlen);
1018 /* 10.1.1.4 hashgen step 4.3 */
1020 drbg_add_buf(src, drbg_statelen(drbg), &prefix, 1);
1024 memset(drbg->scratchpad, 0,
1025 (drbg_statelen(drbg) + drbg_blocklen(drbg)));
1029 /* generate function for Hash DRBG as defined in 10.1.1.4 */
1030 static int drbg_hash_generate(struct drbg_state *drbg,
1031 unsigned char *buf, unsigned int buflen,
1032 struct drbg_string *addtl)
1036 unsigned char req[8];
1037 unsigned char prefix = DRBG_PREFIX3;
1038 struct drbg_string data1, data2;
1039 LIST_HEAD(datalist);
1041 /* 10.1.1.4 step 2 */
1042 ret = drbg_hash_process_addtl(drbg, addtl);
1045 /* 10.1.1.4 step 3 */
1046 len = drbg_hash_hashgen(drbg, buf, buflen);
1048 /* this is the value H as documented in 10.1.1.4 */
1049 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
1050 /* 10.1.1.4 step 4 */
1051 drbg_string_fill(&data1, &prefix, 1);
1052 list_add_tail(&data1.list, &datalist);
1053 drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
1054 list_add_tail(&data2.list, &datalist);
1055 ret = drbg_kcapi_hash(drbg, NULL, drbg->scratchpad, &datalist);
1061 /* 10.1.1.4 step 5 */
1062 drbg_add_buf(drbg->V, drbg_statelen(drbg),
1063 drbg->scratchpad, drbg_blocklen(drbg));
1064 drbg_add_buf(drbg->V, drbg_statelen(drbg),
1065 drbg->C, drbg_statelen(drbg));
1066 drbg_int2byte(req, drbg->reseed_ctr, sizeof(req));
1067 drbg_add_buf(drbg->V, drbg_statelen(drbg), req, 8);
1070 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
1075 * scratchpad usage: as update and generate are used isolated, both
1076 * can use the scratchpad
1078 static struct drbg_state_ops drbg_hash_ops = {
1079 .update = drbg_hash_update,
1080 .generate = drbg_hash_generate,
1081 .crypto_init = drbg_init_hash_kernel,
1082 .crypto_fini = drbg_fini_hash_kernel,
1084 #endif /* CONFIG_CRYPTO_DRBG_HASH */
1086 /******************************************************************
1087 * Functions common for DRBG implementations
1088 ******************************************************************/
1091 * Seeding or reseeding of the DRBG
1093 * @drbg: DRBG state struct
1094 * @pers: personalization / additional information buffer
1095 * @reseed: 0 for initial seed process, 1 for reseeding
1099 * error value otherwise
1101 static int drbg_seed(struct drbg_state *drbg, struct drbg_string *pers,
1105 unsigned char *entropy = NULL;
1106 size_t entropylen = 0;
1107 struct drbg_string data1;
1108 LIST_HEAD(seedlist);
1110 /* 9.1 / 9.2 / 9.3.1 step 3 */
1111 if (pers && pers->len > (drbg_max_addtl(drbg))) {
1112 pr_devel("DRBG: personalization string too long %lu\n",
1117 if (drbg->test_data && drbg->test_data->testentropy) {
1118 drbg_string_fill(&data1, drbg->test_data->testentropy->buf,
1119 drbg->test_data->testentropy->len);
1120 pr_devel("DRBG: using test entropy\n");
1123 * Gather entropy equal to the security strength of the DRBG.
1124 * With a derivation function, a nonce is required in addition
1125 * to the entropy. A nonce must be at least 1/2 of the security
1126 * strength of the DRBG in size. Thus, entropy * nonce is 3/2
1127 * of the strength. The consideration of a nonce is only
1128 * applicable during initial seeding.
1130 entropylen = drbg_sec_strength(drbg->core->flags);
1134 entropylen = ((entropylen + 1) / 2) * 3;
1135 pr_devel("DRBG: (re)seeding with %zu bytes of entropy\n",
1137 entropy = kzalloc(entropylen, GFP_KERNEL);
1140 get_random_bytes(entropy, entropylen);
1141 drbg_string_fill(&data1, entropy, entropylen);
1143 list_add_tail(&data1.list, &seedlist);
1146 * concatenation of entropy with personalization str / addtl input)
1147 * the variable pers is directly handed in by the caller, so check its
1148 * contents whether it is appropriate
1150 if (pers && pers->buf && 0 < pers->len) {
1151 list_add_tail(&pers->list, &seedlist);
1152 pr_devel("DRBG: using personalization string\n");
1155 ret = drbg->d_ops->update(drbg, &seedlist, reseed);
1159 drbg->seeded = true;
1160 /* 10.1.1.2 / 10.1.1.3 step 5 */
1161 drbg->reseed_ctr = 1;
1169 /* Free all substructures in a DRBG state without the DRBG state structure */
1170 static inline void drbg_dealloc_state(struct drbg_state *drbg)
1180 if (drbg->scratchpad)
1181 kzfree(drbg->scratchpad);
1182 drbg->scratchpad = NULL;
1183 drbg->reseed_ctr = 0;
1184 #ifdef CONFIG_CRYPTO_FIPS
1188 drbg->fips_primed = false;
1193 * Allocate all sub-structures for a DRBG state.
1194 * The DRBG state structure must already be allocated.
1196 static inline int drbg_alloc_state(struct drbg_state *drbg)
1199 unsigned int sb_size = 0;
1204 drbg->V = kzalloc(drbg_statelen(drbg), GFP_KERNEL);
1207 drbg->C = kzalloc(drbg_statelen(drbg), GFP_KERNEL);
1210 #ifdef CONFIG_CRYPTO_FIPS
1211 drbg->prev = kzalloc(drbg_blocklen(drbg), GFP_KERNEL);
1214 drbg->fips_primed = false;
1216 /* scratchpad is only generated for CTR and Hash */
1217 if (drbg->core->flags & DRBG_HMAC)
1219 else if (drbg->core->flags & DRBG_CTR)
1220 sb_size = drbg_statelen(drbg) + drbg_blocklen(drbg) + /* temp */
1221 drbg_statelen(drbg) + /* df_data */
1222 drbg_blocklen(drbg) + /* pad */
1223 drbg_blocklen(drbg) + /* iv */
1224 drbg_statelen(drbg) + drbg_blocklen(drbg); /* temp */
1226 sb_size = drbg_statelen(drbg) + drbg_blocklen(drbg);
1229 drbg->scratchpad = kzalloc(sb_size, GFP_KERNEL);
1230 if (!drbg->scratchpad)
1233 spin_lock_init(&drbg->drbg_lock);
1237 drbg_dealloc_state(drbg);
1242 * Strategy to avoid holding long term locks: generate a shadow copy of DRBG
1243 * and perform all operations on this shadow copy. After finishing, restore
1244 * the updated state of the shadow copy into original drbg state. This way,
1245 * only the read and write operations of the original drbg state must be
1248 static inline void drbg_copy_drbg(struct drbg_state *src,
1249 struct drbg_state *dst)
1253 memcpy(dst->V, src->V, drbg_statelen(src));
1254 memcpy(dst->C, src->C, drbg_statelen(src));
1255 dst->reseed_ctr = src->reseed_ctr;
1256 dst->seeded = src->seeded;
1258 #ifdef CONFIG_CRYPTO_FIPS
1259 dst->fips_primed = src->fips_primed;
1260 memcpy(dst->prev, src->prev, drbg_blocklen(src));
1264 * scratchpad is initialized drbg_alloc_state;
1265 * priv_data is initialized with call to crypto_init;
1266 * d_ops and core are set outside, as these parameters are const;
1267 * test_data is set outside to prevent it being copied back.
1271 static int drbg_make_shadow(struct drbg_state *drbg, struct drbg_state **shadow)
1274 struct drbg_state *tmp = NULL;
1276 if (!drbg || !drbg->core || !drbg->V || !drbg->C) {
1277 pr_devel("DRBG: attempt to generate shadow copy for "
1278 "uninitialized DRBG state rejected\n");
1281 /* HMAC does not have a scratchpad */
1282 if (!(drbg->core->flags & DRBG_HMAC) && NULL == drbg->scratchpad)
1285 tmp = kzalloc(sizeof(struct drbg_state), GFP_KERNEL);
1289 /* read-only data as they are defined as const, no lock needed */
1290 tmp->core = drbg->core;
1291 tmp->d_ops = drbg->d_ops;
1293 ret = drbg_alloc_state(tmp);
1297 spin_lock_bh(&drbg->drbg_lock);
1298 drbg_copy_drbg(drbg, tmp);
1299 /* only make a link to the test buffer, as we only read that data */
1300 tmp->test_data = drbg->test_data;
1301 spin_unlock_bh(&drbg->drbg_lock);
1311 static void drbg_restore_shadow(struct drbg_state *drbg,
1312 struct drbg_state **shadow)
1314 struct drbg_state *tmp = *shadow;
1316 spin_lock_bh(&drbg->drbg_lock);
1317 drbg_copy_drbg(tmp, drbg);
1318 spin_unlock_bh(&drbg->drbg_lock);
1319 drbg_dealloc_state(tmp);
1324 /*************************************************************************
1325 * DRBG interface functions
1326 *************************************************************************/
1329 * DRBG generate function as required by SP800-90A - this function
1330 * generates random numbers
1332 * @drbg DRBG state handle
1333 * @buf Buffer where to store the random numbers -- the buffer must already
1334 * be pre-allocated by caller
1335 * @buflen Length of output buffer - this value defines the number of random
1336 * bytes pulled from DRBG
1337 * @addtl Additional input that is mixed into state, may be NULL -- note
1338 * the entropy is pulled by the DRBG internally unconditionally
1339 * as defined in SP800-90A. The additional input is mixed into
1340 * the state in addition to the pulled entropy.
1342 * return: generated number of bytes
1344 static int drbg_generate(struct drbg_state *drbg,
1345 unsigned char *buf, unsigned int buflen,
1346 struct drbg_string *addtl)
1349 struct drbg_state *shadow = NULL;
1351 if (0 == buflen || !buf) {
1352 pr_devel("DRBG: no output buffer provided\n");
1355 if (addtl && NULL == addtl->buf && 0 < addtl->len) {
1356 pr_devel("DRBG: wrong format of additional information\n");
1360 len = drbg_make_shadow(drbg, &shadow);
1362 pr_devel("DRBG: shadow copy cannot be generated\n");
1368 if (buflen > (drbg_max_request_bytes(shadow))) {
1369 pr_devel("DRBG: requested random numbers too large %u\n",
1374 /* 9.3.1 step 3 is implicit with the chosen DRBG */
1377 if (addtl && addtl->len > (drbg_max_addtl(shadow))) {
1378 pr_devel("DRBG: additional information string too long %zu\n",
1382 /* 9.3.1 step 5 is implicit with the chosen DRBG */
1385 * 9.3.1 step 6 and 9 supplemented by 9.3.2 step c is implemented
1386 * here. The spec is a bit convoluted here, we make it simpler.
1388 if ((drbg_max_requests(shadow)) < shadow->reseed_ctr)
1389 shadow->seeded = false;
1391 /* allocate cipher handle */
1392 if (shadow->d_ops->crypto_init) {
1393 len = shadow->d_ops->crypto_init(shadow);
1398 if (shadow->pr || !shadow->seeded) {
1399 pr_devel("DRBG: reseeding before generation (prediction "
1400 "resistance: %s, state %s)\n",
1401 drbg->pr ? "true" : "false",
1402 drbg->seeded ? "seeded" : "unseeded");
1403 /* 9.3.1 steps 7.1 through 7.3 */
1404 len = drbg_seed(shadow, addtl, true);
1407 /* 9.3.1 step 7.4 */
1410 /* 9.3.1 step 8 and 10 */
1411 len = shadow->d_ops->generate(shadow, buf, buflen, addtl);
1413 /* 10.1.1.4 step 6, 10.1.2.5 step 7, 10.2.1.5.2 step 7 */
1414 shadow->reseed_ctr++;
1419 * Section 11.3.3 requires to re-perform self tests after some
1420 * generated random numbers. The chosen value after which self
1421 * test is performed is arbitrary, but it should be reasonable.
1422 * However, we do not perform the self tests because of the following
1423 * reasons: it is mathematically impossible that the initial self tests
1424 * were successfully and the following are not. If the initial would
1425 * pass and the following would not, the kernel integrity is violated.
1426 * In this case, the entire kernel operation is questionable and it
1427 * is unlikely that the integrity violation only affects the
1428 * correct operation of the DRBG.
1430 * Albeit the following code is commented out, it is provided in
1431 * case somebody has a need to implement the test of 11.3.3.
1434 if (shadow->reseed_ctr && !(shadow->reseed_ctr % 4096)) {
1436 pr_devel("DRBG: start to perform self test\n");
1437 if (drbg->core->flags & DRBG_HMAC)
1438 err = alg_test("drbg_pr_hmac_sha256",
1439 "drbg_pr_hmac_sha256", 0, 0);
1440 else if (drbg->core->flags & DRBG_CTR)
1441 err = alg_test("drbg_pr_ctr_aes128",
1442 "drbg_pr_ctr_aes128", 0, 0);
1444 err = alg_test("drbg_pr_sha256",
1445 "drbg_pr_sha256", 0, 0);
1447 pr_err("DRBG: periodical self test failed\n");
1449 * uninstantiate implies that from now on, only errors
1450 * are returned when reusing this DRBG cipher handle
1452 drbg_uninstantiate(drbg);
1453 drbg_dealloc_state(shadow);
1457 pr_devel("DRBG: self test successful\n");
1463 if (shadow->d_ops->crypto_fini)
1464 shadow->d_ops->crypto_fini(shadow);
1465 drbg_restore_shadow(drbg, &shadow);
1470 * Wrapper around drbg_generate which can pull arbitrary long strings
1471 * from the DRBG without hitting the maximum request limitation.
1473 * Parameters: see drbg_generate
1474 * Return codes: see drbg_generate -- if one drbg_generate request fails,
1475 * the entire drbg_generate_long request fails
1477 static int drbg_generate_long(struct drbg_state *drbg,
1478 unsigned char *buf, unsigned int buflen,
1479 struct drbg_string *addtl)
1482 unsigned int slice = 0;
1485 unsigned int chunk = 0;
1486 slice = ((buflen - len) / drbg_max_request_bytes(drbg));
1487 chunk = slice ? drbg_max_request_bytes(drbg) : (buflen - len);
1488 tmplen = drbg_generate(drbg, buf + len, chunk, addtl);
1492 } while (slice > 0);
1497 * DRBG instantiation function as required by SP800-90A - this function
1498 * sets up the DRBG handle, performs the initial seeding and all sanity
1499 * checks required by SP800-90A
1501 * @drbg memory of state -- if NULL, new memory is allocated
1502 * @pers Personalization string that is mixed into state, may be NULL -- note
1503 * the entropy is pulled by the DRBG internally unconditionally
1504 * as defined in SP800-90A. The additional input is mixed into
1505 * the state in addition to the pulled entropy.
1506 * @coreref reference to core
1507 * @pr prediction resistance enabled
1511 * error value otherwise
1513 static int drbg_instantiate(struct drbg_state *drbg, struct drbg_string *pers,
1514 int coreref, bool pr)
1518 pr_devel("DRBG: Initializing DRBG core %d with prediction resistance "
1519 "%s\n", coreref, pr ? "enabled" : "disabled");
1520 drbg->core = &drbg_cores[coreref];
1522 drbg->seeded = false;
1523 switch (drbg->core->flags & DRBG_TYPE_MASK) {
1524 #ifdef CONFIG_CRYPTO_DRBG_HMAC
1526 drbg->d_ops = &drbg_hmac_ops;
1528 #endif /* CONFIG_CRYPTO_DRBG_HMAC */
1529 #ifdef CONFIG_CRYPTO_DRBG_HASH
1531 drbg->d_ops = &drbg_hash_ops;
1533 #endif /* CONFIG_CRYPTO_DRBG_HASH */
1534 #ifdef CONFIG_CRYPTO_DRBG_CTR
1536 drbg->d_ops = &drbg_ctr_ops;
1538 #endif /* CONFIG_CRYPTO_DRBG_CTR */
1543 /* 9.1 step 1 is implicit with the selected DRBG type */
1546 * 9.1 step 2 is implicit as caller can select prediction resistance
1547 * and the flag is copied into drbg->flags --
1548 * all DRBG types support prediction resistance
1551 /* 9.1 step 4 is implicit in drbg_sec_strength */
1553 ret = drbg_alloc_state(drbg);
1558 if (drbg->d_ops->crypto_init && drbg->d_ops->crypto_init(drbg))
1560 ret = drbg_seed(drbg, pers, false);
1561 if (drbg->d_ops->crypto_fini)
1562 drbg->d_ops->crypto_fini(drbg);
1569 drbg_dealloc_state(drbg);
1574 * DRBG uninstantiate function as required by SP800-90A - this function
1575 * frees all buffers and the DRBG handle
1577 * @drbg DRBG state handle
1582 static int drbg_uninstantiate(struct drbg_state *drbg)
1584 spin_lock_bh(&drbg->drbg_lock);
1585 drbg_dealloc_state(drbg);
1586 /* no scrubbing of test_data -- this shall survive an uninstantiate */
1587 spin_unlock_bh(&drbg->drbg_lock);
1592 * Helper function for setting the test data in the DRBG
1594 * @drbg DRBG state handle
1595 * @test_data test data to sets
1597 static inline void drbg_set_testdata(struct drbg_state *drbg,
1598 struct drbg_test_data *test_data)
1600 if (!test_data || !test_data->testentropy)
1602 spin_lock_bh(&drbg->drbg_lock);
1603 drbg->test_data = test_data;
1604 spin_unlock_bh(&drbg->drbg_lock);
1607 /***************************************************************
1608 * Kernel crypto API cipher invocations requested by DRBG
1609 ***************************************************************/
1611 #if defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_HMAC)
1613 struct shash_desc shash;
1617 static int drbg_init_hash_kernel(struct drbg_state *drbg)
1619 struct sdesc *sdesc;
1620 struct crypto_shash *tfm;
1622 tfm = crypto_alloc_shash(drbg->core->backend_cra_name, 0, 0);
1624 pr_info("DRBG: could not allocate digest TFM handle\n");
1625 return PTR_ERR(tfm);
1627 BUG_ON(drbg_blocklen(drbg) != crypto_shash_digestsize(tfm));
1628 sdesc = kzalloc(sizeof(struct shash_desc) + crypto_shash_descsize(tfm),
1631 crypto_free_shash(tfm);
1635 sdesc->shash.tfm = tfm;
1636 sdesc->shash.flags = 0;
1637 drbg->priv_data = sdesc;
1641 static int drbg_fini_hash_kernel(struct drbg_state *drbg)
1643 struct sdesc *sdesc = (struct sdesc *)drbg->priv_data;
1645 crypto_free_shash(sdesc->shash.tfm);
1648 drbg->priv_data = NULL;
1652 static int drbg_kcapi_hash(struct drbg_state *drbg, const unsigned char *key,
1653 unsigned char *outval, const struct list_head *in)
1655 struct sdesc *sdesc = (struct sdesc *)drbg->priv_data;
1656 struct drbg_string *input = NULL;
1659 crypto_shash_setkey(sdesc->shash.tfm, key, drbg_statelen(drbg));
1660 crypto_shash_init(&sdesc->shash);
1661 list_for_each_entry(input, in, list)
1662 crypto_shash_update(&sdesc->shash, input->buf, input->len);
1663 return crypto_shash_final(&sdesc->shash, outval);
1665 #endif /* (CONFIG_CRYPTO_DRBG_HASH || CONFIG_CRYPTO_DRBG_HMAC) */
1667 #ifdef CONFIG_CRYPTO_DRBG_CTR
1668 static int drbg_init_sym_kernel(struct drbg_state *drbg)
1671 struct crypto_blkcipher *tfm;
1673 tfm = crypto_alloc_blkcipher(drbg->core->backend_cra_name, 0, 0);
1675 pr_info("DRBG: could not allocate cipher TFM handle\n");
1676 return PTR_ERR(tfm);
1678 BUG_ON(drbg_blocklen(drbg) != crypto_blkcipher_blocksize(tfm));
1679 drbg->priv_data = tfm;
1683 static int drbg_fini_sym_kernel(struct drbg_state *drbg)
1685 struct crypto_blkcipher *tfm =
1686 (struct crypto_blkcipher *)drbg->priv_data;
1688 crypto_free_blkcipher(tfm);
1689 drbg->priv_data = NULL;
1693 static int drbg_kcapi_sym(struct drbg_state *drbg, const unsigned char *key,
1694 unsigned char *outval, const struct drbg_string *in)
1697 struct scatterlist sg_in, sg_out;
1698 struct blkcipher_desc desc;
1699 struct crypto_blkcipher *tfm =
1700 (struct crypto_blkcipher *)drbg->priv_data;
1704 crypto_blkcipher_setkey(tfm, key, (drbg_keylen(drbg)));
1705 /* there is only component in *in */
1706 sg_init_one(&sg_in, in->buf, in->len);
1707 sg_init_one(&sg_out, outval, drbg_blocklen(drbg));
1708 ret = crypto_blkcipher_encrypt(&desc, &sg_out, &sg_in, in->len);
1712 #endif /* CONFIG_CRYPTO_DRBG_CTR */
1714 /***************************************************************
1715 * Kernel crypto API interface to register DRBG
1716 ***************************************************************/
1719 * Look up the DRBG flags by given kernel crypto API cra_name
1720 * The code uses the drbg_cores definition to do this
1722 * @cra_name kernel crypto API cra_name
1723 * @coreref reference to integer which is filled with the pointer to
1724 * the applicable core
1725 * @pr reference for setting prediction resistance
1729 static inline void drbg_convert_tfm_core(const char *cra_driver_name,
1730 int *coreref, bool *pr)
1737 /* disassemble the names */
1738 if (!memcmp(cra_driver_name, "drbg_nopr_", 10)) {
1741 } else if (!memcmp(cra_driver_name, "drbg_pr_", 8)) {
1747 /* remove the first part */
1748 len = strlen(cra_driver_name) - start;
1749 for (i = 0; ARRAY_SIZE(drbg_cores) > i; i++) {
1750 if (!memcmp(cra_driver_name + start, drbg_cores[i].cra_name,
1758 static int drbg_kcapi_init(struct crypto_tfm *tfm)
1760 struct drbg_state *drbg = crypto_tfm_ctx(tfm);
1764 drbg_convert_tfm_core(crypto_tfm_alg_name(tfm), &coreref, &pr);
1766 * when personalization string is needed, the caller must call reset
1767 * and provide the personalization string as seed information
1769 return drbg_instantiate(drbg, NULL, coreref, pr);
1772 static void drbg_kcapi_cleanup(struct crypto_tfm *tfm)
1774 drbg_uninstantiate(crypto_tfm_ctx(tfm));
1778 * Generate random numbers invoked by the kernel crypto API:
1779 * The API of the kernel crypto API is extended as follows:
1781 * If dlen is larger than zero, rdata is interpreted as the output buffer
1782 * where random data is to be stored.
1784 * If dlen is zero, rdata is interpreted as a pointer to a struct drbg_gen
1785 * which holds the additional information string that is used for the
1786 * DRBG generation process. The output buffer that is to be used to store
1787 * data is also pointed to by struct drbg_gen.
1789 static int drbg_kcapi_random(struct crypto_rng *tfm, u8 *rdata,
1792 struct drbg_state *drbg = crypto_rng_ctx(tfm);
1794 return drbg_generate_long(drbg, rdata, dlen, NULL);
1796 struct drbg_gen *data = (struct drbg_gen *)rdata;
1797 struct drbg_string addtl;
1798 /* catch NULL pointer */
1801 drbg_set_testdata(drbg, data->test_data);
1802 /* linked list variable is now local to allow modification */
1803 drbg_string_fill(&addtl, data->addtl->buf, data->addtl->len);
1804 return drbg_generate_long(drbg, data->outbuf, data->outlen,
1810 * Reset the DRBG invoked by the kernel crypto API
1811 * The reset implies a full re-initialization of the DRBG. Similar to the
1812 * generate function of drbg_kcapi_random, this function extends the
1813 * kernel crypto API interface with struct drbg_gen
1815 static int drbg_kcapi_reset(struct crypto_rng *tfm, u8 *seed, unsigned int slen)
1817 struct drbg_state *drbg = crypto_rng_ctx(tfm);
1818 struct crypto_tfm *tfm_base = crypto_rng_tfm(tfm);
1820 struct drbg_string seed_string;
1823 drbg_uninstantiate(drbg);
1824 drbg_convert_tfm_core(crypto_tfm_alg_driver_name(tfm_base), &coreref,
1827 drbg_string_fill(&seed_string, seed, slen);
1828 return drbg_instantiate(drbg, &seed_string, coreref, pr);
1830 struct drbg_gen *data = (struct drbg_gen *)seed;
1831 /* allow invocation of API call with NULL, 0 */
1833 return drbg_instantiate(drbg, NULL, coreref, pr);
1834 drbg_set_testdata(drbg, data->test_data);
1835 /* linked list variable is now local to allow modification */
1836 drbg_string_fill(&seed_string, data->addtl->buf,
1838 return drbg_instantiate(drbg, &seed_string, coreref, pr);
1842 /***************************************************************
1843 * Kernel module: code to load the module
1844 ***************************************************************/
1847 * Tests as defined in 11.3.2 in addition to the cipher tests: testing
1848 * of the error handling.
1850 * Note: testing of failing seed source as defined in 11.3.2 is not applicable
1851 * as seed source of get_random_bytes does not fail.
1853 * Note 2: There is no sensible way of testing the reseed counter
1854 * enforcement, so skip it.
1856 static inline int __init drbg_healthcheck_sanity(void)
1858 #ifdef CONFIG_CRYPTO_FIPS
1860 #define OUTBUFLEN 16
1861 unsigned char buf[OUTBUFLEN];
1862 struct drbg_state *drbg = NULL;
1867 struct drbg_string addtl;
1868 size_t max_addtllen, max_request_bytes;
1870 /* only perform test in FIPS mode */
1874 #ifdef CONFIG_CRYPTO_DRBG_CTR
1875 drbg_convert_tfm_core("drbg_nopr_ctr_aes128", &coreref, &pr);
1876 #elif CONFIG_CRYPTO_DRBG_HASH
1877 drbg_convert_tfm_core("drbg_nopr_sha256", &coreref, &pr);
1879 drbg_convert_tfm_core("drbg_nopr_hmac_sha256", &coreref, &pr);
1882 drbg = kzalloc(sizeof(struct drbg_state), GFP_KERNEL);
1887 * if the following tests fail, it is likely that there is a buffer
1888 * overflow as buf is much smaller than the requested or provided
1889 * string lengths -- in case the error handling does not succeed
1890 * we may get an OOPS. And we want to get an OOPS as this is a
1894 /* get a valid instance of DRBG for following tests */
1895 ret = drbg_instantiate(drbg, NULL, coreref, pr);
1900 max_addtllen = drbg_max_addtl(drbg);
1901 max_request_bytes = drbg_max_request_bytes(drbg);
1902 drbg_string_fill(&addtl, buf, max_addtllen + 1);
1903 /* overflow addtllen with additonal info string */
1904 len = drbg_generate(drbg, buf, OUTBUFLEN, &addtl);
1906 /* overflow max_bits */
1907 len = drbg_generate(drbg, buf, (max_request_bytes + 1), NULL);
1909 drbg_uninstantiate(drbg);
1911 /* overflow max addtllen with personalization string */
1912 ret = drbg_instantiate(drbg, &addtl, coreref, pr);
1914 /* test uninstantated DRBG */
1915 len = drbg_generate(drbg, buf, (max_request_bytes + 1), NULL);
1917 /* all tests passed */
1920 pr_devel("DRBG: Sanity tests for failure code paths successfully "
1923 drbg_uninstantiate(drbg);
1927 #else /* CONFIG_CRYPTO_FIPS */
1929 #endif /* CONFIG_CRYPTO_FIPS */
1932 static struct crypto_alg drbg_algs[22];
1935 * Fill the array drbg_algs used to register the different DRBGs
1936 * with the kernel crypto API. To fill the array, the information
1937 * from drbg_cores[] is used.
1939 static inline void __init drbg_fill_array(struct crypto_alg *alg,
1940 const struct drbg_core *core, int pr)
1943 static int priority = 100;
1945 memset(alg, 0, sizeof(struct crypto_alg));
1946 memcpy(alg->cra_name, "stdrng", 6);
1948 memcpy(alg->cra_driver_name, "drbg_pr_", 8);
1951 memcpy(alg->cra_driver_name, "drbg_nopr_", 10);
1954 memcpy(alg->cra_driver_name + pos, core->cra_name,
1955 strlen(core->cra_name));
1957 alg->cra_priority = priority;
1960 * If FIPS mode enabled, the selected DRBG shall have the
1961 * highest cra_priority over other stdrng instances to ensure
1965 alg->cra_priority += 200;
1967 alg->cra_flags = CRYPTO_ALG_TYPE_RNG;
1968 alg->cra_ctxsize = sizeof(struct drbg_state);
1969 alg->cra_type = &crypto_rng_type;
1970 alg->cra_module = THIS_MODULE;
1971 alg->cra_init = drbg_kcapi_init;
1972 alg->cra_exit = drbg_kcapi_cleanup;
1973 alg->cra_u.rng.rng_make_random = drbg_kcapi_random;
1974 alg->cra_u.rng.rng_reset = drbg_kcapi_reset;
1975 alg->cra_u.rng.seedsize = 0;
1978 static int __init drbg_init(void)
1980 unsigned int i = 0; /* pointer to drbg_algs */
1981 unsigned int j = 0; /* pointer to drbg_cores */
1984 ret = drbg_healthcheck_sanity();
1988 if (ARRAY_SIZE(drbg_cores) * 2 > ARRAY_SIZE(drbg_algs)) {
1989 pr_info("DRBG: Cannot register all DRBG types"
1990 "(slots needed: %lu, slots available: %lu)\n",
1991 ARRAY_SIZE(drbg_cores) * 2, ARRAY_SIZE(drbg_algs));
1996 * each DRBG definition can be used with PR and without PR, thus
1997 * we instantiate each DRBG in drbg_cores[] twice.
1999 * As the order of placing them into the drbg_algs array matters
2000 * (the later DRBGs receive a higher cra_priority) we register the
2001 * prediction resistance DRBGs first as the should not be too
2004 for (j = 0; ARRAY_SIZE(drbg_cores) > j; j++, i++)
2005 drbg_fill_array(&drbg_algs[i], &drbg_cores[j], 1);
2006 for (j = 0; ARRAY_SIZE(drbg_cores) > j; j++, i++)
2007 drbg_fill_array(&drbg_algs[i], &drbg_cores[j], 0);
2008 return crypto_register_algs(drbg_algs, (ARRAY_SIZE(drbg_cores) * 2));
2011 void __exit drbg_exit(void)
2013 crypto_unregister_algs(drbg_algs, (ARRAY_SIZE(drbg_cores) * 2));
2016 module_init(drbg_init);
2017 module_exit(drbg_exit);
2018 MODULE_LICENSE("GPL");
2019 MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
2020 MODULE_DESCRIPTION("NIST SP800-90A Deterministic Random Bit Generator (DRBG) using following cores:"
2021 #ifdef CONFIG_CRYPTO_DRBG_HMAC
2024 #ifdef CONFIG_CRYPTO_DRBG_HASH
2027 #ifdef CONFIG_CRYPTO_DRBG_CTR