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>
101 #include <linux/string.h>
103 /***************************************************************
104 * Backend cipher definitions available to DRBG
105 ***************************************************************/
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).
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.
116 static const struct drbg_core drbg_cores[] = {
117 #ifdef CONFIG_CRYPTO_DRBG_CTR
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 = "ecb(aes)",
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 = "ecb(aes)",
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 = "ecb(aes)",
137 #endif /* CONFIG_CRYPTO_DRBG_CTR */
138 #ifdef CONFIG_CRYPTO_DRBG_HASH
140 .flags = DRBG_HASH | DRBG_STRENGTH128,
141 .statelen = 55, /* 440 bits */
142 .blocklen_bytes = 20,
144 .backend_cra_name = "sha1",
146 .flags = DRBG_HASH | DRBG_STRENGTH256,
147 .statelen = 111, /* 888 bits */
148 .blocklen_bytes = 48,
149 .cra_name = "sha384",
150 .backend_cra_name = "sha384",
152 .flags = DRBG_HASH | DRBG_STRENGTH256,
153 .statelen = 111, /* 888 bits */
154 .blocklen_bytes = 64,
155 .cra_name = "sha512",
156 .backend_cra_name = "sha512",
158 .flags = DRBG_HASH | DRBG_STRENGTH256,
159 .statelen = 55, /* 440 bits */
160 .blocklen_bytes = 32,
161 .cra_name = "sha256",
162 .backend_cra_name = "sha256",
164 #endif /* CONFIG_CRYPTO_DRBG_HASH */
165 #ifdef CONFIG_CRYPTO_DRBG_HMAC
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)",
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)",
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)",
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)",
191 #endif /* CONFIG_CRYPTO_DRBG_HMAC */
194 /******************************************************************
195 * Generic helper functions
196 ******************************************************************/
199 * Return strength of DRBG according to SP800-90A section 8.4
201 * @flags DRBG flags reference
203 * Return: normalized strength in *bytes* value or 32 as default
204 * to counter programming errors
206 static inline unsigned short drbg_sec_strength(drbg_flag_t flags)
208 switch (flags & DRBG_STRENGTH_MASK) {
209 case DRBG_STRENGTH128:
211 case DRBG_STRENGTH192:
213 case DRBG_STRENGTH256:
221 * FIPS 140-2 continuous self test
222 * The test is performed on the result of one round of the output
223 * function. Thus, the function implicitly knows the size of the
227 * @buf output buffer of random data to be checked
233 static bool drbg_fips_continuous_test(struct drbg_state *drbg,
234 const unsigned char *buf)
236 #ifdef CONFIG_CRYPTO_FIPS
238 /* skip test if we test the overall system */
241 /* only perform test in FIPS mode */
242 if (0 == fips_enabled)
244 if (!drbg->fips_primed) {
245 /* Priming of FIPS test */
246 memcpy(drbg->prev, buf, drbg_blocklen(drbg));
247 drbg->fips_primed = true;
248 /* return false due to priming, i.e. another round is needed */
251 ret = memcmp(drbg->prev, buf, drbg_blocklen(drbg));
253 panic("DRBG continuous self test failed\n");
254 memcpy(drbg->prev, buf, drbg_blocklen(drbg));
255 /* the test shall pass when the two compared values are not equal */
259 #endif /* CONFIG_CRYPTO_FIPS */
263 * Convert an integer into a byte representation of this integer.
264 * The byte representation is big-endian
266 * @val value to be converted
267 * @buf buffer holding the converted integer -- caller must ensure that
268 * buffer size is at least 32 bit
270 #if (defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_CTR))
271 static inline void drbg_cpu_to_be32(__u32 val, unsigned char *buf)
276 struct s *conversion = (struct s *) buf;
278 conversion->conv = cpu_to_be32(val);
280 #endif /* defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_CTR) */
282 /******************************************************************
283 * CTR DRBG callback functions
284 ******************************************************************/
286 #ifdef CONFIG_CRYPTO_DRBG_CTR
287 #define CRYPTO_DRBG_CTR_STRING "CTR "
288 MODULE_ALIAS_CRYPTO("drbg_pr_ctr_aes256");
289 MODULE_ALIAS_CRYPTO("drbg_nopr_ctr_aes256");
290 MODULE_ALIAS_CRYPTO("drbg_pr_ctr_aes192");
291 MODULE_ALIAS_CRYPTO("drbg_nopr_ctr_aes192");
292 MODULE_ALIAS_CRYPTO("drbg_pr_ctr_aes128");
293 MODULE_ALIAS_CRYPTO("drbg_nopr_ctr_aes128");
295 static int drbg_kcapi_sym(struct drbg_state *drbg, const unsigned char *key,
296 unsigned char *outval, const struct drbg_string *in);
297 static int drbg_init_sym_kernel(struct drbg_state *drbg);
298 static int drbg_fini_sym_kernel(struct drbg_state *drbg);
300 /* BCC function for CTR DRBG as defined in 10.4.3 */
301 static int drbg_ctr_bcc(struct drbg_state *drbg,
302 unsigned char *out, const unsigned char *key,
303 struct list_head *in)
306 struct drbg_string *curr = NULL;
307 struct drbg_string data;
310 drbg_string_fill(&data, out, drbg_blocklen(drbg));
313 memset(out, 0, drbg_blocklen(drbg));
315 /* 10.4.3 step 2 / 4 */
316 list_for_each_entry(curr, in, list) {
317 const unsigned char *pos = curr->buf;
318 size_t len = curr->len;
319 /* 10.4.3 step 4.1 */
321 /* 10.4.3 step 4.2 */
322 if (drbg_blocklen(drbg) == cnt) {
324 ret = drbg_kcapi_sym(drbg, key, out, &data);
334 /* 10.4.3 step 4.2 for last block */
336 ret = drbg_kcapi_sym(drbg, key, out, &data);
342 * scratchpad usage: drbg_ctr_update is interlinked with drbg_ctr_df
343 * (and drbg_ctr_bcc, but this function does not need any temporary buffers),
344 * the scratchpad is used as follows:
347 * start: drbg->scratchpad
348 * length: drbg_statelen(drbg) + drbg_blocklen(drbg)
349 * note: the cipher writing into this variable works
350 * blocklen-wise. Now, when the statelen is not a multiple
351 * of blocklen, the generateion loop below "spills over"
352 * by at most blocklen. Thus, we need to give sufficient
355 * start: drbg->scratchpad +
356 * drbg_statelen(drbg) + drbg_blocklen(drbg)
357 * length: drbg_statelen(drbg)
361 * start: df_data + drbg_statelen(drbg)
362 * length: drbg_blocklen(drbg)
364 * start: pad + drbg_blocklen(drbg)
365 * length: drbg_blocklen(drbg)
367 * start: iv + drbg_blocklen(drbg)
368 * length: drbg_satelen(drbg) + drbg_blocklen(drbg)
369 * note: temp is the buffer that the BCC function operates
370 * on. BCC operates blockwise. drbg_statelen(drbg)
371 * is sufficient when the DRBG state length is a multiple
372 * of the block size. For AES192 (and maybe other ciphers)
373 * this is not correct and the length for temp is
374 * insufficient (yes, that also means for such ciphers,
375 * the final output of all BCC rounds are truncated).
376 * Therefore, add drbg_blocklen(drbg) to cover all
380 /* Derivation Function for CTR DRBG as defined in 10.4.2 */
381 static int drbg_ctr_df(struct drbg_state *drbg,
382 unsigned char *df_data, size_t bytes_to_return,
383 struct list_head *seedlist)
386 unsigned char L_N[8];
388 struct drbg_string S1, S2, S4, cipherin;
390 unsigned char *pad = df_data + drbg_statelen(drbg);
391 unsigned char *iv = pad + drbg_blocklen(drbg);
392 unsigned char *temp = iv + drbg_blocklen(drbg);
394 unsigned int templen = 0;
398 const unsigned char *K = (unsigned char *)
399 "\x00\x01\x02\x03\x04\x05\x06\x07"
400 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
401 "\x10\x11\x12\x13\x14\x15\x16\x17"
402 "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f";
404 size_t generated_len = 0;
406 struct drbg_string *seed = NULL;
408 memset(pad, 0, drbg_blocklen(drbg));
409 memset(iv, 0, drbg_blocklen(drbg));
410 memset(temp, 0, drbg_statelen(drbg));
412 /* 10.4.2 step 1 is implicit as we work byte-wise */
415 if ((512/8) < bytes_to_return)
418 /* 10.4.2 step 2 -- calculate the entire length of all input data */
419 list_for_each_entry(seed, seedlist, list)
420 inputlen += seed->len;
421 drbg_cpu_to_be32(inputlen, &L_N[0]);
424 drbg_cpu_to_be32(bytes_to_return, &L_N[4]);
426 /* 10.4.2 step 5: length is L_N, input_string, one byte, padding */
427 padlen = (inputlen + sizeof(L_N) + 1) % (drbg_blocklen(drbg));
428 /* wrap the padlen appropriately */
430 padlen = drbg_blocklen(drbg) - padlen;
432 * pad / padlen contains the 0x80 byte and the following zero bytes.
433 * As the calculated padlen value only covers the number of zero
434 * bytes, this value has to be incremented by one for the 0x80 byte.
439 /* 10.4.2 step 4 -- first fill the linked list and then order it */
440 drbg_string_fill(&S1, iv, drbg_blocklen(drbg));
441 list_add_tail(&S1.list, &bcc_list);
442 drbg_string_fill(&S2, L_N, sizeof(L_N));
443 list_add_tail(&S2.list, &bcc_list);
444 list_splice_tail(seedlist, &bcc_list);
445 drbg_string_fill(&S4, pad, padlen);
446 list_add_tail(&S4.list, &bcc_list);
449 while (templen < (drbg_keylen(drbg) + (drbg_blocklen(drbg)))) {
451 * 10.4.2 step 9.1 - the padding is implicit as the buffer
452 * holds zeros after allocation -- even the increment of i
453 * is irrelevant as the increment remains within length of i
455 drbg_cpu_to_be32(i, iv);
456 /* 10.4.2 step 9.2 -- BCC and concatenation with temp */
457 ret = drbg_ctr_bcc(drbg, temp + templen, K, &bcc_list);
460 /* 10.4.2 step 9.3 */
462 templen += drbg_blocklen(drbg);
466 X = temp + (drbg_keylen(drbg));
467 drbg_string_fill(&cipherin, X, drbg_blocklen(drbg));
469 /* 10.4.2 step 12: overwriting of outval is implemented in next step */
472 while (generated_len < bytes_to_return) {
475 * 10.4.2 step 13.1: the truncation of the key length is
476 * implicit as the key is only drbg_blocklen in size based on
477 * the implementation of the cipher function callback
479 ret = drbg_kcapi_sym(drbg, temp, X, &cipherin);
482 blocklen = (drbg_blocklen(drbg) <
483 (bytes_to_return - generated_len)) ?
484 drbg_blocklen(drbg) :
485 (bytes_to_return - generated_len);
486 /* 10.4.2 step 13.2 and 14 */
487 memcpy(df_data + generated_len, X, blocklen);
488 generated_len += blocklen;
494 memzero_explicit(iv, drbg_blocklen(drbg));
495 memzero_explicit(temp, drbg_statelen(drbg));
496 memzero_explicit(pad, drbg_blocklen(drbg));
501 * update function of CTR DRBG as defined in 10.2.1.2
503 * The reseed variable has an enhanced meaning compared to the update
504 * functions of the other DRBGs as follows:
505 * 0 => initial seed from initialization
506 * 1 => reseed via drbg_seed
507 * 2 => first invocation from drbg_ctr_update when addtl is present. In
508 * this case, the df_data scratchpad is not deleted so that it is
509 * available for another calls to prevent calling the DF function
511 * 3 => second invocation from drbg_ctr_update. When the update function
512 * was called with addtl, the df_data memory already contains the
513 * DFed addtl information and we do not need to call DF again.
515 static int drbg_ctr_update(struct drbg_state *drbg, struct list_head *seed,
519 /* 10.2.1.2 step 1 */
520 unsigned char *temp = drbg->scratchpad;
521 unsigned char *df_data = drbg->scratchpad + drbg_statelen(drbg) +
523 unsigned char *temp_p, *df_data_p; /* pointer to iterate over buffers */
524 unsigned int len = 0;
525 struct drbg_string cipherin;
527 memset(temp, 0, drbg_statelen(drbg) + drbg_blocklen(drbg));
529 memset(df_data, 0, drbg_statelen(drbg));
531 /* 10.2.1.3.2 step 2 and 10.2.1.4.2 step 2 */
533 ret = drbg_ctr_df(drbg, df_data, drbg_statelen(drbg), seed);
538 drbg_string_fill(&cipherin, drbg->V, drbg_blocklen(drbg));
540 * 10.2.1.3.2 steps 2 and 3 are already covered as the allocation
541 * zeroizes all memory during initialization
543 while (len < (drbg_statelen(drbg))) {
544 /* 10.2.1.2 step 2.1 */
545 crypto_inc(drbg->V, drbg_blocklen(drbg));
547 * 10.2.1.2 step 2.2 */
548 ret = drbg_kcapi_sym(drbg, drbg->C, temp + len, &cipherin);
551 /* 10.2.1.2 step 2.3 and 3 */
552 len += drbg_blocklen(drbg);
555 /* 10.2.1.2 step 4 */
558 for (len = 0; len < drbg_statelen(drbg); len++) {
559 *temp_p ^= *df_data_p;
560 df_data_p++; temp_p++;
563 /* 10.2.1.2 step 5 */
564 memcpy(drbg->C, temp, drbg_keylen(drbg));
565 /* 10.2.1.2 step 6 */
566 memcpy(drbg->V, temp + drbg_keylen(drbg), drbg_blocklen(drbg));
570 memzero_explicit(temp, drbg_statelen(drbg) + drbg_blocklen(drbg));
572 memzero_explicit(df_data, drbg_statelen(drbg));
577 * scratchpad use: drbg_ctr_update is called independently from
578 * drbg_ctr_extract_bytes. Therefore, the scratchpad is reused
580 /* Generate function of CTR DRBG as defined in 10.2.1.5.2 */
581 static int drbg_ctr_generate(struct drbg_state *drbg,
582 unsigned char *buf, unsigned int buflen,
583 struct list_head *addtl)
587 struct drbg_string data;
589 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
591 /* 10.2.1.5.2 step 2 */
592 if (addtl && !list_empty(addtl)) {
593 ret = drbg_ctr_update(drbg, addtl, 2);
598 /* 10.2.1.5.2 step 4.1 */
599 crypto_inc(drbg->V, drbg_blocklen(drbg));
600 drbg_string_fill(&data, drbg->V, drbg_blocklen(drbg));
601 while (len < buflen) {
603 /* 10.2.1.5.2 step 4.2 */
604 ret = drbg_kcapi_sym(drbg, drbg->C, drbg->scratchpad, &data);
609 outlen = (drbg_blocklen(drbg) < (buflen - len)) ?
610 drbg_blocklen(drbg) : (buflen - len);
611 if (!drbg_fips_continuous_test(drbg, drbg->scratchpad)) {
612 /* 10.2.1.5.2 step 6 */
613 crypto_inc(drbg->V, drbg_blocklen(drbg));
616 /* 10.2.1.5.2 step 4.3 */
617 memcpy(buf + len, drbg->scratchpad, outlen);
619 /* 10.2.1.5.2 step 6 */
621 crypto_inc(drbg->V, drbg_blocklen(drbg));
624 /* 10.2.1.5.2 step 6 */
625 ret = drbg_ctr_update(drbg, NULL, 3);
630 memzero_explicit(drbg->scratchpad, drbg_blocklen(drbg));
634 static struct drbg_state_ops drbg_ctr_ops = {
635 .update = drbg_ctr_update,
636 .generate = drbg_ctr_generate,
637 .crypto_init = drbg_init_sym_kernel,
638 .crypto_fini = drbg_fini_sym_kernel,
640 #endif /* CONFIG_CRYPTO_DRBG_CTR */
642 /******************************************************************
643 * HMAC DRBG callback functions
644 ******************************************************************/
646 #if defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_HMAC)
647 static int drbg_kcapi_hash(struct drbg_state *drbg, const unsigned char *key,
648 unsigned char *outval, const struct list_head *in);
649 static int drbg_init_hash_kernel(struct drbg_state *drbg);
650 static int drbg_fini_hash_kernel(struct drbg_state *drbg);
651 #endif /* (CONFIG_CRYPTO_DRBG_HASH || CONFIG_CRYPTO_DRBG_HMAC) */
653 #ifdef CONFIG_CRYPTO_DRBG_HMAC
654 #define CRYPTO_DRBG_HMAC_STRING "HMAC "
655 MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha512");
656 MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha512");
657 MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha384");
658 MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha384");
659 MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha256");
660 MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha256");
661 MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha1");
662 MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha1");
664 /* update function of HMAC DRBG as defined in 10.1.2.2 */
665 static int drbg_hmac_update(struct drbg_state *drbg, struct list_head *seed,
670 struct drbg_string seed1, seed2, vdata;
672 LIST_HEAD(vdatalist);
675 /* 10.1.2.3 step 2 -- memset(0) of C is implicit with kzalloc */
676 memset(drbg->V, 1, drbg_statelen(drbg));
678 drbg_string_fill(&seed1, drbg->V, drbg_statelen(drbg));
679 list_add_tail(&seed1.list, &seedlist);
680 /* buffer of seed2 will be filled in for loop below with one byte */
681 drbg_string_fill(&seed2, NULL, 1);
682 list_add_tail(&seed2.list, &seedlist);
683 /* input data of seed is allowed to be NULL at this point */
685 list_splice_tail(seed, &seedlist);
687 drbg_string_fill(&vdata, drbg->V, drbg_statelen(drbg));
688 list_add_tail(&vdata.list, &vdatalist);
689 for (i = 2; 0 < i; i--) {
690 /* first round uses 0x0, second 0x1 */
691 unsigned char prefix = DRBG_PREFIX0;
693 prefix = DRBG_PREFIX1;
694 /* 10.1.2.2 step 1 and 4 -- concatenation and HMAC for key */
696 ret = drbg_kcapi_hash(drbg, drbg->C, drbg->C, &seedlist);
700 /* 10.1.2.2 step 2 and 5 -- HMAC for V */
701 ret = drbg_kcapi_hash(drbg, drbg->C, drbg->V, &vdatalist);
705 /* 10.1.2.2 step 3 */
713 /* generate function of HMAC DRBG as defined in 10.1.2.5 */
714 static int drbg_hmac_generate(struct drbg_state *drbg,
717 struct list_head *addtl)
721 struct drbg_string data;
724 /* 10.1.2.5 step 2 */
725 if (addtl && !list_empty(addtl)) {
726 ret = drbg_hmac_update(drbg, addtl, 1);
731 drbg_string_fill(&data, drbg->V, drbg_statelen(drbg));
732 list_add_tail(&data.list, &datalist);
733 while (len < buflen) {
734 unsigned int outlen = 0;
735 /* 10.1.2.5 step 4.1 */
736 ret = drbg_kcapi_hash(drbg, drbg->C, drbg->V, &datalist);
739 outlen = (drbg_blocklen(drbg) < (buflen - len)) ?
740 drbg_blocklen(drbg) : (buflen - len);
741 if (!drbg_fips_continuous_test(drbg, drbg->V))
744 /* 10.1.2.5 step 4.2 */
745 memcpy(buf + len, drbg->V, outlen);
749 /* 10.1.2.5 step 6 */
750 if (addtl && !list_empty(addtl))
751 ret = drbg_hmac_update(drbg, addtl, 1);
753 ret = drbg_hmac_update(drbg, NULL, 1);
760 static struct drbg_state_ops drbg_hmac_ops = {
761 .update = drbg_hmac_update,
762 .generate = drbg_hmac_generate,
763 .crypto_init = drbg_init_hash_kernel,
764 .crypto_fini = drbg_fini_hash_kernel,
767 #endif /* CONFIG_CRYPTO_DRBG_HMAC */
769 /******************************************************************
770 * Hash DRBG callback functions
771 ******************************************************************/
773 #ifdef CONFIG_CRYPTO_DRBG_HASH
774 #define CRYPTO_DRBG_HASH_STRING "HASH "
775 MODULE_ALIAS_CRYPTO("drbg_pr_sha512");
776 MODULE_ALIAS_CRYPTO("drbg_nopr_sha512");
777 MODULE_ALIAS_CRYPTO("drbg_pr_sha384");
778 MODULE_ALIAS_CRYPTO("drbg_nopr_sha384");
779 MODULE_ALIAS_CRYPTO("drbg_pr_sha256");
780 MODULE_ALIAS_CRYPTO("drbg_nopr_sha256");
781 MODULE_ALIAS_CRYPTO("drbg_pr_sha1");
782 MODULE_ALIAS_CRYPTO("drbg_nopr_sha1");
787 * @dst buffer to increment
790 static inline void drbg_add_buf(unsigned char *dst, size_t dstlen,
791 const unsigned char *add, size_t addlen)
793 /* implied: dstlen > addlen */
794 unsigned char *dstptr;
795 const unsigned char *addptr;
796 unsigned int remainder = 0;
799 dstptr = dst + (dstlen-1);
800 addptr = add + (addlen-1);
802 remainder += *dstptr + *addptr;
803 *dstptr = remainder & 0xff;
805 len--; dstptr--; addptr--;
807 len = dstlen - addlen;
808 while (len && remainder > 0) {
809 remainder = *dstptr + 1;
810 *dstptr = remainder & 0xff;
817 * scratchpad usage: as drbg_hash_update and drbg_hash_df are used
818 * interlinked, the scratchpad is used as follows:
820 * start: drbg->scratchpad
821 * length: drbg_statelen(drbg)
823 * start: drbg->scratchpad + drbg_statelen(drbg)
824 * length: drbg_blocklen(drbg)
826 * drbg_hash_process_addtl uses the scratchpad, but fully completes
827 * before either of the functions mentioned before are invoked. Therefore,
828 * drbg_hash_process_addtl does not need to be specifically considered.
831 /* Derivation Function for Hash DRBG as defined in 10.4.1 */
832 static int drbg_hash_df(struct drbg_state *drbg,
833 unsigned char *outval, size_t outlen,
834 struct list_head *entropylist)
838 unsigned char input[5];
839 unsigned char *tmp = drbg->scratchpad + drbg_statelen(drbg);
840 struct drbg_string data;
842 memset(tmp, 0, drbg_blocklen(drbg));
846 drbg_cpu_to_be32((outlen * 8), &input[1]);
848 /* 10.4.1 step 4.1 -- concatenation of data for input into hash */
849 drbg_string_fill(&data, input, 5);
850 list_add(&data.list, entropylist);
853 while (len < outlen) {
855 /* 10.4.1 step 4.1 */
856 ret = drbg_kcapi_hash(drbg, NULL, tmp, entropylist);
859 /* 10.4.1 step 4.2 */
861 blocklen = (drbg_blocklen(drbg) < (outlen - len)) ?
862 drbg_blocklen(drbg) : (outlen - len);
863 memcpy(outval + len, tmp, blocklen);
868 memzero_explicit(tmp, drbg_blocklen(drbg));
872 /* update function for Hash DRBG as defined in 10.1.1.2 / 10.1.1.3 */
873 static int drbg_hash_update(struct drbg_state *drbg, struct list_head *seed,
877 struct drbg_string data1, data2;
879 LIST_HEAD(datalist2);
880 unsigned char *V = drbg->scratchpad;
881 unsigned char prefix = DRBG_PREFIX1;
883 memset(drbg->scratchpad, 0, drbg_statelen(drbg));
888 /* 10.1.1.3 step 1 */
889 memcpy(V, drbg->V, drbg_statelen(drbg));
890 drbg_string_fill(&data1, &prefix, 1);
891 list_add_tail(&data1.list, &datalist);
892 drbg_string_fill(&data2, V, drbg_statelen(drbg));
893 list_add_tail(&data2.list, &datalist);
895 list_splice_tail(seed, &datalist);
897 /* 10.1.1.2 / 10.1.1.3 step 2 and 3 */
898 ret = drbg_hash_df(drbg, drbg->V, drbg_statelen(drbg), &datalist);
902 /* 10.1.1.2 / 10.1.1.3 step 4 */
903 prefix = DRBG_PREFIX0;
904 drbg_string_fill(&data1, &prefix, 1);
905 list_add_tail(&data1.list, &datalist2);
906 drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
907 list_add_tail(&data2.list, &datalist2);
908 /* 10.1.1.2 / 10.1.1.3 step 4 */
909 ret = drbg_hash_df(drbg, drbg->C, drbg_statelen(drbg), &datalist2);
912 memzero_explicit(drbg->scratchpad, drbg_statelen(drbg));
916 /* processing of additional information string for Hash DRBG */
917 static int drbg_hash_process_addtl(struct drbg_state *drbg,
918 struct list_head *addtl)
921 struct drbg_string data1, data2;
923 unsigned char prefix = DRBG_PREFIX2;
925 /* this is value w as per documentation */
926 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
928 /* 10.1.1.4 step 2 */
929 if (!addtl || list_empty(addtl))
932 /* 10.1.1.4 step 2a */
933 drbg_string_fill(&data1, &prefix, 1);
934 drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
935 list_add_tail(&data1.list, &datalist);
936 list_add_tail(&data2.list, &datalist);
937 list_splice_tail(addtl, &datalist);
938 ret = drbg_kcapi_hash(drbg, NULL, drbg->scratchpad, &datalist);
942 /* 10.1.1.4 step 2b */
943 drbg_add_buf(drbg->V, drbg_statelen(drbg),
944 drbg->scratchpad, drbg_blocklen(drbg));
947 memzero_explicit(drbg->scratchpad, drbg_blocklen(drbg));
951 /* Hashgen defined in 10.1.1.4 */
952 static int drbg_hash_hashgen(struct drbg_state *drbg,
958 unsigned char *src = drbg->scratchpad;
959 unsigned char *dst = drbg->scratchpad + drbg_statelen(drbg);
960 struct drbg_string data;
963 memset(src, 0, drbg_statelen(drbg));
964 memset(dst, 0, drbg_blocklen(drbg));
966 /* 10.1.1.4 step hashgen 2 */
967 memcpy(src, drbg->V, drbg_statelen(drbg));
969 drbg_string_fill(&data, src, drbg_statelen(drbg));
970 list_add_tail(&data.list, &datalist);
971 while (len < buflen) {
972 unsigned int outlen = 0;
973 /* 10.1.1.4 step hashgen 4.1 */
974 ret = drbg_kcapi_hash(drbg, NULL, dst, &datalist);
979 outlen = (drbg_blocklen(drbg) < (buflen - len)) ?
980 drbg_blocklen(drbg) : (buflen - len);
981 if (!drbg_fips_continuous_test(drbg, dst)) {
982 crypto_inc(src, drbg_statelen(drbg));
985 /* 10.1.1.4 step hashgen 4.2 */
986 memcpy(buf + len, dst, outlen);
988 /* 10.1.1.4 hashgen step 4.3 */
990 crypto_inc(src, drbg_statelen(drbg));
994 memzero_explicit(drbg->scratchpad,
995 (drbg_statelen(drbg) + drbg_blocklen(drbg)));
999 /* generate function for Hash DRBG as defined in 10.1.1.4 */
1000 static int drbg_hash_generate(struct drbg_state *drbg,
1001 unsigned char *buf, unsigned int buflen,
1002 struct list_head *addtl)
1007 unsigned char req[8];
1010 unsigned char prefix = DRBG_PREFIX3;
1011 struct drbg_string data1, data2;
1012 LIST_HEAD(datalist);
1014 /* 10.1.1.4 step 2 */
1015 ret = drbg_hash_process_addtl(drbg, addtl);
1018 /* 10.1.1.4 step 3 */
1019 len = drbg_hash_hashgen(drbg, buf, buflen);
1021 /* this is the value H as documented in 10.1.1.4 */
1022 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
1023 /* 10.1.1.4 step 4 */
1024 drbg_string_fill(&data1, &prefix, 1);
1025 list_add_tail(&data1.list, &datalist);
1026 drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
1027 list_add_tail(&data2.list, &datalist);
1028 ret = drbg_kcapi_hash(drbg, NULL, drbg->scratchpad, &datalist);
1034 /* 10.1.1.4 step 5 */
1035 drbg_add_buf(drbg->V, drbg_statelen(drbg),
1036 drbg->scratchpad, drbg_blocklen(drbg));
1037 drbg_add_buf(drbg->V, drbg_statelen(drbg),
1038 drbg->C, drbg_statelen(drbg));
1039 u.req_int = cpu_to_be64(drbg->reseed_ctr);
1040 drbg_add_buf(drbg->V, drbg_statelen(drbg), u.req, 8);
1043 memzero_explicit(drbg->scratchpad, drbg_blocklen(drbg));
1048 * scratchpad usage: as update and generate are used isolated, both
1049 * can use the scratchpad
1051 static struct drbg_state_ops drbg_hash_ops = {
1052 .update = drbg_hash_update,
1053 .generate = drbg_hash_generate,
1054 .crypto_init = drbg_init_hash_kernel,
1055 .crypto_fini = drbg_fini_hash_kernel,
1057 #endif /* CONFIG_CRYPTO_DRBG_HASH */
1059 /******************************************************************
1060 * Functions common for DRBG implementations
1061 ******************************************************************/
1064 * Seeding or reseeding of the DRBG
1066 * @drbg: DRBG state struct
1067 * @pers: personalization / additional information buffer
1068 * @reseed: 0 for initial seed process, 1 for reseeding
1072 * error value otherwise
1074 static int drbg_seed(struct drbg_state *drbg, struct drbg_string *pers,
1078 unsigned char *entropy = NULL;
1079 size_t entropylen = 0;
1080 struct drbg_string data1;
1081 LIST_HEAD(seedlist);
1083 /* 9.1 / 9.2 / 9.3.1 step 3 */
1084 if (pers && pers->len > (drbg_max_addtl(drbg))) {
1085 pr_devel("DRBG: personalization string too long %zu\n",
1090 if (drbg->test_data && drbg->test_data->testentropy) {
1091 drbg_string_fill(&data1, drbg->test_data->testentropy->buf,
1092 drbg->test_data->testentropy->len);
1093 pr_devel("DRBG: using test entropy\n");
1096 * Gather entropy equal to the security strength of the DRBG.
1097 * With a derivation function, a nonce is required in addition
1098 * to the entropy. A nonce must be at least 1/2 of the security
1099 * strength of the DRBG in size. Thus, entropy * nonce is 3/2
1100 * of the strength. The consideration of a nonce is only
1101 * applicable during initial seeding.
1103 entropylen = drbg_sec_strength(drbg->core->flags);
1107 entropylen = ((entropylen + 1) / 2) * 3;
1108 pr_devel("DRBG: (re)seeding with %zu bytes of entropy\n",
1110 entropy = kzalloc(entropylen, GFP_KERNEL);
1113 get_random_bytes(entropy, entropylen);
1114 drbg_string_fill(&data1, entropy, entropylen);
1116 list_add_tail(&data1.list, &seedlist);
1119 * concatenation of entropy with personalization str / addtl input)
1120 * the variable pers is directly handed in by the caller, so check its
1121 * contents whether it is appropriate
1123 if (pers && pers->buf && 0 < pers->len) {
1124 list_add_tail(&pers->list, &seedlist);
1125 pr_devel("DRBG: using personalization string\n");
1129 memset(drbg->V, 0, drbg_statelen(drbg));
1130 memset(drbg->C, 0, drbg_statelen(drbg));
1133 ret = drbg->d_ops->update(drbg, &seedlist, reseed);
1137 drbg->seeded = true;
1138 /* 10.1.1.2 / 10.1.1.3 step 5 */
1139 drbg->reseed_ctr = 1;
1146 /* Free all substructures in a DRBG state without the DRBG state structure */
1147 static inline void drbg_dealloc_state(struct drbg_state *drbg)
1155 kzfree(drbg->scratchpad);
1156 drbg->scratchpad = NULL;
1157 drbg->reseed_ctr = 0;
1158 #ifdef CONFIG_CRYPTO_FIPS
1161 drbg->fips_primed = false;
1166 * Allocate all sub-structures for a DRBG state.
1167 * The DRBG state structure must already be allocated.
1169 static inline int drbg_alloc_state(struct drbg_state *drbg)
1172 unsigned int sb_size = 0;
1174 drbg->V = kmalloc(drbg_statelen(drbg), GFP_KERNEL);
1177 drbg->C = kmalloc(drbg_statelen(drbg), GFP_KERNEL);
1180 #ifdef CONFIG_CRYPTO_FIPS
1181 drbg->prev = kmalloc(drbg_blocklen(drbg), GFP_KERNEL);
1184 drbg->fips_primed = false;
1186 /* scratchpad is only generated for CTR and Hash */
1187 if (drbg->core->flags & DRBG_HMAC)
1189 else if (drbg->core->flags & DRBG_CTR)
1190 sb_size = drbg_statelen(drbg) + drbg_blocklen(drbg) + /* temp */
1191 drbg_statelen(drbg) + /* df_data */
1192 drbg_blocklen(drbg) + /* pad */
1193 drbg_blocklen(drbg) + /* iv */
1194 drbg_statelen(drbg) + drbg_blocklen(drbg); /* temp */
1196 sb_size = drbg_statelen(drbg) + drbg_blocklen(drbg);
1199 drbg->scratchpad = kzalloc(sb_size, GFP_KERNEL);
1200 if (!drbg->scratchpad)
1203 spin_lock_init(&drbg->drbg_lock);
1207 drbg_dealloc_state(drbg);
1212 * Strategy to avoid holding long term locks: generate a shadow copy of DRBG
1213 * and perform all operations on this shadow copy. After finishing, restore
1214 * the updated state of the shadow copy into original drbg state. This way,
1215 * only the read and write operations of the original drbg state must be
1218 static inline void drbg_copy_drbg(struct drbg_state *src,
1219 struct drbg_state *dst)
1223 memcpy(dst->V, src->V, drbg_statelen(src));
1224 memcpy(dst->C, src->C, drbg_statelen(src));
1225 dst->reseed_ctr = src->reseed_ctr;
1226 dst->seeded = src->seeded;
1228 #ifdef CONFIG_CRYPTO_FIPS
1229 dst->fips_primed = src->fips_primed;
1230 memcpy(dst->prev, src->prev, drbg_blocklen(src));
1234 * scratchpad is initialized drbg_alloc_state;
1235 * priv_data is initialized with call to crypto_init;
1236 * d_ops and core are set outside, as these parameters are const;
1237 * test_data is set outside to prevent it being copied back.
1241 static int drbg_make_shadow(struct drbg_state *drbg, struct drbg_state **shadow)
1244 struct drbg_state *tmp = NULL;
1246 tmp = kzalloc(sizeof(struct drbg_state), GFP_KERNEL);
1250 /* read-only data as they are defined as const, no lock needed */
1251 tmp->core = drbg->core;
1252 tmp->d_ops = drbg->d_ops;
1254 ret = drbg_alloc_state(tmp);
1258 spin_lock_bh(&drbg->drbg_lock);
1259 drbg_copy_drbg(drbg, tmp);
1260 /* only make a link to the test buffer, as we only read that data */
1261 tmp->test_data = drbg->test_data;
1262 spin_unlock_bh(&drbg->drbg_lock);
1271 static void drbg_restore_shadow(struct drbg_state *drbg,
1272 struct drbg_state **shadow)
1274 struct drbg_state *tmp = *shadow;
1276 spin_lock_bh(&drbg->drbg_lock);
1277 drbg_copy_drbg(tmp, drbg);
1278 spin_unlock_bh(&drbg->drbg_lock);
1279 drbg_dealloc_state(tmp);
1284 /*************************************************************************
1285 * DRBG interface functions
1286 *************************************************************************/
1289 * DRBG generate function as required by SP800-90A - this function
1290 * generates random numbers
1292 * @drbg DRBG state handle
1293 * @buf Buffer where to store the random numbers -- the buffer must already
1294 * be pre-allocated by caller
1295 * @buflen Length of output buffer - this value defines the number of random
1296 * bytes pulled from DRBG
1297 * @addtl Additional input that is mixed into state, may be NULL -- note
1298 * the entropy is pulled by the DRBG internally unconditionally
1299 * as defined in SP800-90A. The additional input is mixed into
1300 * the state in addition to the pulled entropy.
1302 * return: generated number of bytes
1304 static int drbg_generate(struct drbg_state *drbg,
1305 unsigned char *buf, unsigned int buflen,
1306 struct drbg_string *addtl)
1309 struct drbg_state *shadow = NULL;
1310 LIST_HEAD(addtllist);
1311 struct drbg_string timestamp;
1314 unsigned char char_cycles[sizeof(cycles_t)];
1317 if (0 == buflen || !buf) {
1318 pr_devel("DRBG: no output buffer provided\n");
1321 if (addtl && NULL == addtl->buf && 0 < addtl->len) {
1322 pr_devel("DRBG: wrong format of additional information\n");
1326 len = drbg_make_shadow(drbg, &shadow);
1328 pr_devel("DRBG: shadow copy cannot be generated\n");
1334 if (buflen > (drbg_max_request_bytes(shadow))) {
1335 pr_devel("DRBG: requested random numbers too large %u\n",
1340 /* 9.3.1 step 3 is implicit with the chosen DRBG */
1343 if (addtl && addtl->len > (drbg_max_addtl(shadow))) {
1344 pr_devel("DRBG: additional information string too long %zu\n",
1348 /* 9.3.1 step 5 is implicit with the chosen DRBG */
1351 * 9.3.1 step 6 and 9 supplemented by 9.3.2 step c is implemented
1352 * here. The spec is a bit convoluted here, we make it simpler.
1354 if ((drbg_max_requests(shadow)) < shadow->reseed_ctr)
1355 shadow->seeded = false;
1357 /* allocate cipher handle */
1358 len = shadow->d_ops->crypto_init(shadow);
1362 if (shadow->pr || !shadow->seeded) {
1363 pr_devel("DRBG: reseeding before generation (prediction "
1364 "resistance: %s, state %s)\n",
1365 drbg->pr ? "true" : "false",
1366 drbg->seeded ? "seeded" : "unseeded");
1367 /* 9.3.1 steps 7.1 through 7.3 */
1368 len = drbg_seed(shadow, addtl, true);
1371 /* 9.3.1 step 7.4 */
1376 * Mix the time stamp into the DRBG state if the DRBG is not in
1377 * test mode. If there are two callers invoking the DRBG at the same
1378 * time, i.e. before the first caller merges its shadow state back,
1379 * both callers would obtain the same random number stream without
1380 * changing the state here.
1382 if (!drbg->test_data) {
1383 now.cycles = random_get_entropy();
1384 drbg_string_fill(×tamp, now.char_cycles, sizeof(cycles_t));
1385 list_add_tail(×tamp.list, &addtllist);
1387 if (addtl && 0 < addtl->len)
1388 list_add_tail(&addtl->list, &addtllist);
1389 /* 9.3.1 step 8 and 10 */
1390 len = shadow->d_ops->generate(shadow, buf, buflen, &addtllist);
1392 /* 10.1.1.4 step 6, 10.1.2.5 step 7, 10.2.1.5.2 step 7 */
1393 shadow->reseed_ctr++;
1398 * Section 11.3.3 requires to re-perform self tests after some
1399 * generated random numbers. The chosen value after which self
1400 * test is performed is arbitrary, but it should be reasonable.
1401 * However, we do not perform the self tests because of the following
1402 * reasons: it is mathematically impossible that the initial self tests
1403 * were successfully and the following are not. If the initial would
1404 * pass and the following would not, the kernel integrity is violated.
1405 * In this case, the entire kernel operation is questionable and it
1406 * is unlikely that the integrity violation only affects the
1407 * correct operation of the DRBG.
1409 * Albeit the following code is commented out, it is provided in
1410 * case somebody has a need to implement the test of 11.3.3.
1413 if (shadow->reseed_ctr && !(shadow->reseed_ctr % 4096)) {
1415 pr_devel("DRBG: start to perform self test\n");
1416 if (drbg->core->flags & DRBG_HMAC)
1417 err = alg_test("drbg_pr_hmac_sha256",
1418 "drbg_pr_hmac_sha256", 0, 0);
1419 else if (drbg->core->flags & DRBG_CTR)
1420 err = alg_test("drbg_pr_ctr_aes128",
1421 "drbg_pr_ctr_aes128", 0, 0);
1423 err = alg_test("drbg_pr_sha256",
1424 "drbg_pr_sha256", 0, 0);
1426 pr_err("DRBG: periodical self test failed\n");
1428 * uninstantiate implies that from now on, only errors
1429 * are returned when reusing this DRBG cipher handle
1431 drbg_uninstantiate(drbg);
1432 drbg_dealloc_state(shadow);
1436 pr_devel("DRBG: self test successful\n");
1442 shadow->d_ops->crypto_fini(shadow);
1443 drbg_restore_shadow(drbg, &shadow);
1448 * Wrapper around drbg_generate which can pull arbitrary long strings
1449 * from the DRBG without hitting the maximum request limitation.
1451 * Parameters: see drbg_generate
1452 * Return codes: see drbg_generate -- if one drbg_generate request fails,
1453 * the entire drbg_generate_long request fails
1455 static int drbg_generate_long(struct drbg_state *drbg,
1456 unsigned char *buf, unsigned int buflen,
1457 struct drbg_string *addtl)
1460 unsigned int slice = 0;
1463 unsigned int chunk = 0;
1464 slice = ((buflen - len) / drbg_max_request_bytes(drbg));
1465 chunk = slice ? drbg_max_request_bytes(drbg) : (buflen - len);
1466 tmplen = drbg_generate(drbg, buf + len, chunk, addtl);
1470 } while (slice > 0 && (len < buflen));
1475 * DRBG instantiation function as required by SP800-90A - this function
1476 * sets up the DRBG handle, performs the initial seeding and all sanity
1477 * checks required by SP800-90A
1479 * @drbg memory of state -- if NULL, new memory is allocated
1480 * @pers Personalization string that is mixed into state, may be NULL -- note
1481 * the entropy is pulled by the DRBG internally unconditionally
1482 * as defined in SP800-90A. The additional input is mixed into
1483 * the state in addition to the pulled entropy.
1484 * @coreref reference to core
1485 * @pr prediction resistance enabled
1489 * error value otherwise
1491 static int drbg_instantiate(struct drbg_state *drbg, struct drbg_string *pers,
1492 int coreref, bool pr)
1496 pr_devel("DRBG: Initializing DRBG core %d with prediction resistance "
1497 "%s\n", coreref, pr ? "enabled" : "disabled");
1498 drbg->core = &drbg_cores[coreref];
1500 drbg->seeded = false;
1501 switch (drbg->core->flags & DRBG_TYPE_MASK) {
1502 #ifdef CONFIG_CRYPTO_DRBG_HMAC
1504 drbg->d_ops = &drbg_hmac_ops;
1506 #endif /* CONFIG_CRYPTO_DRBG_HMAC */
1507 #ifdef CONFIG_CRYPTO_DRBG_HASH
1509 drbg->d_ops = &drbg_hash_ops;
1511 #endif /* CONFIG_CRYPTO_DRBG_HASH */
1512 #ifdef CONFIG_CRYPTO_DRBG_CTR
1514 drbg->d_ops = &drbg_ctr_ops;
1516 #endif /* CONFIG_CRYPTO_DRBG_CTR */
1521 /* 9.1 step 1 is implicit with the selected DRBG type */
1524 * 9.1 step 2 is implicit as caller can select prediction resistance
1525 * and the flag is copied into drbg->flags --
1526 * all DRBG types support prediction resistance
1529 /* 9.1 step 4 is implicit in drbg_sec_strength */
1531 ret = drbg_alloc_state(drbg);
1536 if (drbg->d_ops->crypto_init(drbg))
1538 ret = drbg_seed(drbg, pers, false);
1539 drbg->d_ops->crypto_fini(drbg);
1546 drbg_dealloc_state(drbg);
1551 * DRBG uninstantiate function as required by SP800-90A - this function
1552 * frees all buffers and the DRBG handle
1554 * @drbg DRBG state handle
1559 static int drbg_uninstantiate(struct drbg_state *drbg)
1561 spin_lock_bh(&drbg->drbg_lock);
1562 drbg_dealloc_state(drbg);
1563 /* no scrubbing of test_data -- this shall survive an uninstantiate */
1564 spin_unlock_bh(&drbg->drbg_lock);
1569 * Helper function for setting the test data in the DRBG
1571 * @drbg DRBG state handle
1572 * @test_data test data to sets
1574 static inline void drbg_set_testdata(struct drbg_state *drbg,
1575 struct drbg_test_data *test_data)
1577 if (!test_data || !test_data->testentropy)
1579 spin_lock_bh(&drbg->drbg_lock);
1580 drbg->test_data = test_data;
1581 spin_unlock_bh(&drbg->drbg_lock);
1584 /***************************************************************
1585 * Kernel crypto API cipher invocations requested by DRBG
1586 ***************************************************************/
1588 #if defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_HMAC)
1590 struct shash_desc shash;
1594 static int drbg_init_hash_kernel(struct drbg_state *drbg)
1596 struct sdesc *sdesc;
1597 struct crypto_shash *tfm;
1599 tfm = crypto_alloc_shash(drbg->core->backend_cra_name, 0, 0);
1601 pr_info("DRBG: could not allocate digest TFM handle\n");
1602 return PTR_ERR(tfm);
1604 BUG_ON(drbg_blocklen(drbg) != crypto_shash_digestsize(tfm));
1605 sdesc = kzalloc(sizeof(struct shash_desc) + crypto_shash_descsize(tfm),
1608 crypto_free_shash(tfm);
1612 sdesc->shash.tfm = tfm;
1613 sdesc->shash.flags = 0;
1614 drbg->priv_data = sdesc;
1618 static int drbg_fini_hash_kernel(struct drbg_state *drbg)
1620 struct sdesc *sdesc = (struct sdesc *)drbg->priv_data;
1622 crypto_free_shash(sdesc->shash.tfm);
1625 drbg->priv_data = NULL;
1629 static int drbg_kcapi_hash(struct drbg_state *drbg, const unsigned char *key,
1630 unsigned char *outval, const struct list_head *in)
1632 struct sdesc *sdesc = (struct sdesc *)drbg->priv_data;
1633 struct drbg_string *input = NULL;
1636 crypto_shash_setkey(sdesc->shash.tfm, key, drbg_statelen(drbg));
1637 crypto_shash_init(&sdesc->shash);
1638 list_for_each_entry(input, in, list)
1639 crypto_shash_update(&sdesc->shash, input->buf, input->len);
1640 return crypto_shash_final(&sdesc->shash, outval);
1642 #endif /* (CONFIG_CRYPTO_DRBG_HASH || CONFIG_CRYPTO_DRBG_HMAC) */
1644 #ifdef CONFIG_CRYPTO_DRBG_CTR
1645 static int drbg_init_sym_kernel(struct drbg_state *drbg)
1648 struct crypto_blkcipher *tfm;
1650 tfm = crypto_alloc_blkcipher(drbg->core->backend_cra_name, 0, 0);
1652 pr_info("DRBG: could not allocate cipher TFM handle\n");
1653 return PTR_ERR(tfm);
1655 BUG_ON(drbg_blocklen(drbg) != crypto_blkcipher_blocksize(tfm));
1656 drbg->priv_data = tfm;
1660 static int drbg_fini_sym_kernel(struct drbg_state *drbg)
1662 struct crypto_blkcipher *tfm =
1663 (struct crypto_blkcipher *)drbg->priv_data;
1665 crypto_free_blkcipher(tfm);
1666 drbg->priv_data = NULL;
1670 static int drbg_kcapi_sym(struct drbg_state *drbg, const unsigned char *key,
1671 unsigned char *outval, const struct drbg_string *in)
1674 struct scatterlist sg_in, sg_out;
1675 struct blkcipher_desc desc;
1676 struct crypto_blkcipher *tfm =
1677 (struct crypto_blkcipher *)drbg->priv_data;
1681 crypto_blkcipher_setkey(tfm, key, (drbg_keylen(drbg)));
1682 /* there is only component in *in */
1683 sg_init_one(&sg_in, in->buf, in->len);
1684 sg_init_one(&sg_out, outval, drbg_blocklen(drbg));
1685 ret = crypto_blkcipher_encrypt(&desc, &sg_out, &sg_in, in->len);
1689 #endif /* CONFIG_CRYPTO_DRBG_CTR */
1691 /***************************************************************
1692 * Kernel crypto API interface to register DRBG
1693 ***************************************************************/
1696 * Look up the DRBG flags by given kernel crypto API cra_name
1697 * The code uses the drbg_cores definition to do this
1699 * @cra_name kernel crypto API cra_name
1700 * @coreref reference to integer which is filled with the pointer to
1701 * the applicable core
1702 * @pr reference for setting prediction resistance
1706 static inline void drbg_convert_tfm_core(const char *cra_driver_name,
1707 int *coreref, bool *pr)
1714 /* disassemble the names */
1715 if (!memcmp(cra_driver_name, "drbg_nopr_", 10)) {
1718 } else if (!memcmp(cra_driver_name, "drbg_pr_", 8)) {
1724 /* remove the first part */
1725 len = strlen(cra_driver_name) - start;
1726 for (i = 0; ARRAY_SIZE(drbg_cores) > i; i++) {
1727 if (!memcmp(cra_driver_name + start, drbg_cores[i].cra_name,
1735 static int drbg_kcapi_init(struct crypto_tfm *tfm)
1737 struct drbg_state *drbg = crypto_tfm_ctx(tfm);
1741 drbg_convert_tfm_core(crypto_tfm_alg_driver_name(tfm), &coreref, &pr);
1743 * when personalization string is needed, the caller must call reset
1744 * and provide the personalization string as seed information
1746 return drbg_instantiate(drbg, NULL, coreref, pr);
1749 static void drbg_kcapi_cleanup(struct crypto_tfm *tfm)
1751 drbg_uninstantiate(crypto_tfm_ctx(tfm));
1755 * Generate random numbers invoked by the kernel crypto API:
1756 * The API of the kernel crypto API is extended as follows:
1758 * If dlen is larger than zero, rdata is interpreted as the output buffer
1759 * where random data is to be stored.
1761 * If dlen is zero, rdata is interpreted as a pointer to a struct drbg_gen
1762 * which holds the additional information string that is used for the
1763 * DRBG generation process. The output buffer that is to be used to store
1764 * data is also pointed to by struct drbg_gen.
1766 static int drbg_kcapi_random(struct crypto_rng *tfm, u8 *rdata,
1769 struct drbg_state *drbg = crypto_rng_ctx(tfm);
1771 return drbg_generate_long(drbg, rdata, dlen, NULL);
1773 struct drbg_gen *data = (struct drbg_gen *)rdata;
1774 struct drbg_string addtl;
1775 /* catch NULL pointer */
1778 drbg_set_testdata(drbg, data->test_data);
1779 /* linked list variable is now local to allow modification */
1780 drbg_string_fill(&addtl, data->addtl->buf, data->addtl->len);
1781 return drbg_generate_long(drbg, data->outbuf, data->outlen,
1787 * Reset the DRBG invoked by the kernel crypto API
1788 * The reset implies a full re-initialization of the DRBG. Similar to the
1789 * generate function of drbg_kcapi_random, this function extends the
1790 * kernel crypto API interface with struct drbg_gen
1792 static int drbg_kcapi_reset(struct crypto_rng *tfm, u8 *seed, unsigned int slen)
1794 struct drbg_state *drbg = crypto_rng_ctx(tfm);
1795 struct crypto_tfm *tfm_base = crypto_rng_tfm(tfm);
1797 struct drbg_string seed_string;
1800 drbg_uninstantiate(drbg);
1801 drbg_convert_tfm_core(crypto_tfm_alg_driver_name(tfm_base), &coreref,
1804 drbg_string_fill(&seed_string, seed, slen);
1805 return drbg_instantiate(drbg, &seed_string, coreref, pr);
1807 struct drbg_gen *data = (struct drbg_gen *)seed;
1808 /* allow invocation of API call with NULL, 0 */
1810 return drbg_instantiate(drbg, NULL, coreref, pr);
1811 drbg_set_testdata(drbg, data->test_data);
1812 /* linked list variable is now local to allow modification */
1813 drbg_string_fill(&seed_string, data->addtl->buf,
1815 return drbg_instantiate(drbg, &seed_string, coreref, pr);
1819 /***************************************************************
1820 * Kernel module: code to load the module
1821 ***************************************************************/
1824 * Tests as defined in 11.3.2 in addition to the cipher tests: testing
1825 * of the error handling.
1827 * Note: testing of failing seed source as defined in 11.3.2 is not applicable
1828 * as seed source of get_random_bytes does not fail.
1830 * Note 2: There is no sensible way of testing the reseed counter
1831 * enforcement, so skip it.
1833 static inline int __init drbg_healthcheck_sanity(void)
1835 #ifdef CONFIG_CRYPTO_FIPS
1837 #define OUTBUFLEN 16
1838 unsigned char buf[OUTBUFLEN];
1839 struct drbg_state *drbg = NULL;
1844 struct drbg_string addtl;
1845 size_t max_addtllen, max_request_bytes;
1847 /* only perform test in FIPS mode */
1851 #ifdef CONFIG_CRYPTO_DRBG_CTR
1852 drbg_convert_tfm_core("drbg_nopr_ctr_aes128", &coreref, &pr);
1853 #elif defined CONFIG_CRYPTO_DRBG_HASH
1854 drbg_convert_tfm_core("drbg_nopr_sha256", &coreref, &pr);
1856 drbg_convert_tfm_core("drbg_nopr_hmac_sha256", &coreref, &pr);
1859 drbg = kzalloc(sizeof(struct drbg_state), GFP_KERNEL);
1864 * if the following tests fail, it is likely that there is a buffer
1865 * overflow as buf is much smaller than the requested or provided
1866 * string lengths -- in case the error handling does not succeed
1867 * we may get an OOPS. And we want to get an OOPS as this is a
1871 /* get a valid instance of DRBG for following tests */
1872 ret = drbg_instantiate(drbg, NULL, coreref, pr);
1877 max_addtllen = drbg_max_addtl(drbg);
1878 max_request_bytes = drbg_max_request_bytes(drbg);
1879 drbg_string_fill(&addtl, buf, max_addtllen + 1);
1880 /* overflow addtllen with additonal info string */
1881 len = drbg_generate(drbg, buf, OUTBUFLEN, &addtl);
1883 /* overflow max_bits */
1884 len = drbg_generate(drbg, buf, (max_request_bytes + 1), NULL);
1886 drbg_uninstantiate(drbg);
1888 /* overflow max addtllen with personalization string */
1889 ret = drbg_instantiate(drbg, &addtl, coreref, pr);
1891 /* all tests passed */
1894 pr_devel("DRBG: Sanity tests for failure code paths successfully "
1897 drbg_uninstantiate(drbg);
1901 #else /* CONFIG_CRYPTO_FIPS */
1903 #endif /* CONFIG_CRYPTO_FIPS */
1906 static struct crypto_alg drbg_algs[22];
1909 * Fill the array drbg_algs used to register the different DRBGs
1910 * with the kernel crypto API. To fill the array, the information
1911 * from drbg_cores[] is used.
1913 static inline void __init drbg_fill_array(struct crypto_alg *alg,
1914 const struct drbg_core *core, int pr)
1917 static int priority = 100;
1919 memset(alg, 0, sizeof(struct crypto_alg));
1920 memcpy(alg->cra_name, "stdrng", 6);
1922 memcpy(alg->cra_driver_name, "drbg_pr_", 8);
1925 memcpy(alg->cra_driver_name, "drbg_nopr_", 10);
1928 memcpy(alg->cra_driver_name + pos, core->cra_name,
1929 strlen(core->cra_name));
1931 alg->cra_priority = priority;
1934 * If FIPS mode enabled, the selected DRBG shall have the
1935 * highest cra_priority over other stdrng instances to ensure
1939 alg->cra_priority += 200;
1941 alg->cra_flags = CRYPTO_ALG_TYPE_RNG;
1942 alg->cra_ctxsize = sizeof(struct drbg_state);
1943 alg->cra_type = &crypto_rng_type;
1944 alg->cra_module = THIS_MODULE;
1945 alg->cra_init = drbg_kcapi_init;
1946 alg->cra_exit = drbg_kcapi_cleanup;
1947 alg->cra_u.rng.rng_make_random = drbg_kcapi_random;
1948 alg->cra_u.rng.rng_reset = drbg_kcapi_reset;
1949 alg->cra_u.rng.seedsize = 0;
1952 static int __init drbg_init(void)
1954 unsigned int i = 0; /* pointer to drbg_algs */
1955 unsigned int j = 0; /* pointer to drbg_cores */
1958 ret = drbg_healthcheck_sanity();
1962 if (ARRAY_SIZE(drbg_cores) * 2 > ARRAY_SIZE(drbg_algs)) {
1963 pr_info("DRBG: Cannot register all DRBG types"
1964 "(slots needed: %zu, slots available: %zu)\n",
1965 ARRAY_SIZE(drbg_cores) * 2, ARRAY_SIZE(drbg_algs));
1970 * each DRBG definition can be used with PR and without PR, thus
1971 * we instantiate each DRBG in drbg_cores[] twice.
1973 * As the order of placing them into the drbg_algs array matters
1974 * (the later DRBGs receive a higher cra_priority) we register the
1975 * prediction resistance DRBGs first as the should not be too
1978 for (j = 0; ARRAY_SIZE(drbg_cores) > j; j++, i++)
1979 drbg_fill_array(&drbg_algs[i], &drbg_cores[j], 1);
1980 for (j = 0; ARRAY_SIZE(drbg_cores) > j; j++, i++)
1981 drbg_fill_array(&drbg_algs[i], &drbg_cores[j], 0);
1982 return crypto_register_algs(drbg_algs, (ARRAY_SIZE(drbg_cores) * 2));
1985 static void __exit drbg_exit(void)
1987 crypto_unregister_algs(drbg_algs, (ARRAY_SIZE(drbg_cores) * 2));
1990 module_init(drbg_init);
1991 module_exit(drbg_exit);
1992 #ifndef CRYPTO_DRBG_HASH_STRING
1993 #define CRYPTO_DRBG_HASH_STRING ""
1995 #ifndef CRYPTO_DRBG_HMAC_STRING
1996 #define CRYPTO_DRBG_HMAC_STRING ""
1998 #ifndef CRYPTO_DRBG_CTR_STRING
1999 #define CRYPTO_DRBG_CTR_STRING ""
2001 MODULE_LICENSE("GPL");
2002 MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
2003 MODULE_DESCRIPTION("NIST SP800-90A Deterministic Random Bit Generator (DRBG) "
2004 "using following cores: "
2005 CRYPTO_DRBG_HASH_STRING
2006 CRYPTO_DRBG_HMAC_STRING
2007 CRYPTO_DRBG_CTR_STRING);