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/kernel.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
= "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
= "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
= "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 static int drbg_uninstantiate(struct drbg_state
*drbg
);
196 /******************************************************************
197 * Generic helper functions
198 ******************************************************************/
201 * Return strength of DRBG according to SP800-90A section 8.4
203 * @flags DRBG flags reference
205 * Return: normalized strength in *bytes* value or 32 as default
206 * to counter programming errors
208 static inline unsigned short drbg_sec_strength(drbg_flag_t flags
)
210 switch (flags
& DRBG_STRENGTH_MASK
) {
211 case DRBG_STRENGTH128
:
213 case DRBG_STRENGTH192
:
215 case DRBG_STRENGTH256
:
223 * Convert an integer into a byte representation of this integer.
224 * The byte representation is big-endian
226 * @val value to be converted
227 * @buf buffer holding the converted integer -- caller must ensure that
228 * buffer size is at least 32 bit
230 #if (defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_CTR))
231 static inline void drbg_cpu_to_be32(__u32 val
, unsigned char *buf
)
236 struct s
*conversion
= (struct s
*) buf
;
238 conversion
->conv
= cpu_to_be32(val
);
240 #endif /* defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_CTR) */
242 /******************************************************************
243 * CTR DRBG callback functions
244 ******************************************************************/
246 #ifdef CONFIG_CRYPTO_DRBG_CTR
247 #define CRYPTO_DRBG_CTR_STRING "CTR "
248 MODULE_ALIAS_CRYPTO("drbg_pr_ctr_aes256");
249 MODULE_ALIAS_CRYPTO("drbg_nopr_ctr_aes256");
250 MODULE_ALIAS_CRYPTO("drbg_pr_ctr_aes192");
251 MODULE_ALIAS_CRYPTO("drbg_nopr_ctr_aes192");
252 MODULE_ALIAS_CRYPTO("drbg_pr_ctr_aes128");
253 MODULE_ALIAS_CRYPTO("drbg_nopr_ctr_aes128");
255 static void drbg_kcapi_symsetkey(struct drbg_state
*drbg
,
256 const unsigned char *key
);
257 static int drbg_kcapi_sym(struct drbg_state
*drbg
, unsigned char *outval
,
258 const struct drbg_string
*in
);
259 static int drbg_init_sym_kernel(struct drbg_state
*drbg
);
260 static int drbg_fini_sym_kernel(struct drbg_state
*drbg
);
261 static int drbg_kcapi_sym_ctr(struct drbg_state
*drbg
, u8
*outbuf
, u32 outlen
);
263 /* BCC function for CTR DRBG as defined in 10.4.3 */
264 static int drbg_ctr_bcc(struct drbg_state
*drbg
,
265 unsigned char *out
, const unsigned char *key
,
266 struct list_head
*in
)
269 struct drbg_string
*curr
= NULL
;
270 struct drbg_string data
;
273 drbg_string_fill(&data
, out
, drbg_blocklen(drbg
));
275 /* 10.4.3 step 2 / 4 */
276 drbg_kcapi_symsetkey(drbg
, key
);
277 list_for_each_entry(curr
, in
, list
) {
278 const unsigned char *pos
= curr
->buf
;
279 size_t len
= curr
->len
;
280 /* 10.4.3 step 4.1 */
282 /* 10.4.3 step 4.2 */
283 if (drbg_blocklen(drbg
) == cnt
) {
285 ret
= drbg_kcapi_sym(drbg
, out
, &data
);
295 /* 10.4.3 step 4.2 for last block */
297 ret
= drbg_kcapi_sym(drbg
, out
, &data
);
303 * scratchpad usage: drbg_ctr_update is interlinked with drbg_ctr_df
304 * (and drbg_ctr_bcc, but this function does not need any temporary buffers),
305 * the scratchpad is used as follows:
308 * start: drbg->scratchpad
309 * length: drbg_statelen(drbg) + drbg_blocklen(drbg)
310 * note: the cipher writing into this variable works
311 * blocklen-wise. Now, when the statelen is not a multiple
312 * of blocklen, the generateion loop below "spills over"
313 * by at most blocklen. Thus, we need to give sufficient
316 * start: drbg->scratchpad +
317 * drbg_statelen(drbg) + drbg_blocklen(drbg)
318 * length: drbg_statelen(drbg)
322 * start: df_data + drbg_statelen(drbg)
323 * length: drbg_blocklen(drbg)
325 * start: pad + drbg_blocklen(drbg)
326 * length: drbg_blocklen(drbg)
328 * start: iv + drbg_blocklen(drbg)
329 * length: drbg_satelen(drbg) + drbg_blocklen(drbg)
330 * note: temp is the buffer that the BCC function operates
331 * on. BCC operates blockwise. drbg_statelen(drbg)
332 * is sufficient when the DRBG state length is a multiple
333 * of the block size. For AES192 (and maybe other ciphers)
334 * this is not correct and the length for temp is
335 * insufficient (yes, that also means for such ciphers,
336 * the final output of all BCC rounds are truncated).
337 * Therefore, add drbg_blocklen(drbg) to cover all
341 /* Derivation Function for CTR DRBG as defined in 10.4.2 */
342 static int drbg_ctr_df(struct drbg_state
*drbg
,
343 unsigned char *df_data
, size_t bytes_to_return
,
344 struct list_head
*seedlist
)
347 unsigned char L_N
[8];
349 struct drbg_string S1
, S2
, S4
, cipherin
;
351 unsigned char *pad
= df_data
+ drbg_statelen(drbg
);
352 unsigned char *iv
= pad
+ drbg_blocklen(drbg
);
353 unsigned char *temp
= iv
+ drbg_blocklen(drbg
);
355 unsigned int templen
= 0;
359 const unsigned char *K
= (unsigned char *)
360 "\x00\x01\x02\x03\x04\x05\x06\x07"
361 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
362 "\x10\x11\x12\x13\x14\x15\x16\x17"
363 "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f";
365 size_t generated_len
= 0;
367 struct drbg_string
*seed
= NULL
;
369 memset(pad
, 0, drbg_blocklen(drbg
));
370 memset(iv
, 0, drbg_blocklen(drbg
));
372 /* 10.4.2 step 1 is implicit as we work byte-wise */
375 if ((512/8) < bytes_to_return
)
378 /* 10.4.2 step 2 -- calculate the entire length of all input data */
379 list_for_each_entry(seed
, seedlist
, list
)
380 inputlen
+= seed
->len
;
381 drbg_cpu_to_be32(inputlen
, &L_N
[0]);
384 drbg_cpu_to_be32(bytes_to_return
, &L_N
[4]);
386 /* 10.4.2 step 5: length is L_N, input_string, one byte, padding */
387 padlen
= (inputlen
+ sizeof(L_N
) + 1) % (drbg_blocklen(drbg
));
388 /* wrap the padlen appropriately */
390 padlen
= drbg_blocklen(drbg
) - padlen
;
392 * pad / padlen contains the 0x80 byte and the following zero bytes.
393 * As the calculated padlen value only covers the number of zero
394 * bytes, this value has to be incremented by one for the 0x80 byte.
399 /* 10.4.2 step 4 -- first fill the linked list and then order it */
400 drbg_string_fill(&S1
, iv
, drbg_blocklen(drbg
));
401 list_add_tail(&S1
.list
, &bcc_list
);
402 drbg_string_fill(&S2
, L_N
, sizeof(L_N
));
403 list_add_tail(&S2
.list
, &bcc_list
);
404 list_splice_tail(seedlist
, &bcc_list
);
405 drbg_string_fill(&S4
, pad
, padlen
);
406 list_add_tail(&S4
.list
, &bcc_list
);
409 while (templen
< (drbg_keylen(drbg
) + (drbg_blocklen(drbg
)))) {
411 * 10.4.2 step 9.1 - the padding is implicit as the buffer
412 * holds zeros after allocation -- even the increment of i
413 * is irrelevant as the increment remains within length of i
415 drbg_cpu_to_be32(i
, iv
);
416 /* 10.4.2 step 9.2 -- BCC and concatenation with temp */
417 ret
= drbg_ctr_bcc(drbg
, temp
+ templen
, K
, &bcc_list
);
420 /* 10.4.2 step 9.3 */
422 templen
+= drbg_blocklen(drbg
);
426 X
= temp
+ (drbg_keylen(drbg
));
427 drbg_string_fill(&cipherin
, X
, drbg_blocklen(drbg
));
429 /* 10.4.2 step 12: overwriting of outval is implemented in next step */
432 drbg_kcapi_symsetkey(drbg
, temp
);
433 while (generated_len
< bytes_to_return
) {
436 * 10.4.2 step 13.1: the truncation of the key length is
437 * implicit as the key is only drbg_blocklen in size based on
438 * the implementation of the cipher function callback
440 ret
= drbg_kcapi_sym(drbg
, X
, &cipherin
);
443 blocklen
= (drbg_blocklen(drbg
) <
444 (bytes_to_return
- generated_len
)) ?
445 drbg_blocklen(drbg
) :
446 (bytes_to_return
- generated_len
);
447 /* 10.4.2 step 13.2 and 14 */
448 memcpy(df_data
+ generated_len
, X
, blocklen
);
449 generated_len
+= blocklen
;
455 memset(iv
, 0, drbg_blocklen(drbg
));
456 memset(temp
, 0, drbg_statelen(drbg
) + drbg_blocklen(drbg
));
457 memset(pad
, 0, drbg_blocklen(drbg
));
462 * update function of CTR DRBG as defined in 10.2.1.2
464 * The reseed variable has an enhanced meaning compared to the update
465 * functions of the other DRBGs as follows:
466 * 0 => initial seed from initialization
467 * 1 => reseed via drbg_seed
468 * 2 => first invocation from drbg_ctr_update when addtl is present. In
469 * this case, the df_data scratchpad is not deleted so that it is
470 * available for another calls to prevent calling the DF function
472 * 3 => second invocation from drbg_ctr_update. When the update function
473 * was called with addtl, the df_data memory already contains the
474 * DFed addtl information and we do not need to call DF again.
476 static int drbg_ctr_update(struct drbg_state
*drbg
, struct list_head
*seed
,
480 /* 10.2.1.2 step 1 */
481 unsigned char *temp
= drbg
->scratchpad
;
482 unsigned char *df_data
= drbg
->scratchpad
+ drbg_statelen(drbg
) +
484 unsigned char *temp_p
, *df_data_p
; /* pointer to iterate over buffers */
485 unsigned int len
= 0;
488 memset(df_data
, 0, drbg_statelen(drbg
));
492 * The DRBG uses the CTR mode of the underlying AES cipher. The
493 * CTR mode increments the counter value after the AES operation
494 * but SP800-90A requires that the counter is incremented before
495 * the AES operation. Hence, we increment it at the time we set
498 crypto_inc(drbg
->V
, drbg_blocklen(drbg
));
500 ret
= crypto_skcipher_setkey(drbg
->ctr_handle
, drbg
->C
,
506 /* 10.2.1.3.2 step 2 and 10.2.1.4.2 step 2 */
508 ret
= drbg_ctr_df(drbg
, df_data
, drbg_statelen(drbg
), seed
);
513 ret
= drbg_kcapi_sym_ctr(drbg
, temp
, drbg_statelen(drbg
));
517 /* 10.2.1.2 step 4 */
520 for (len
= 0; len
< drbg_statelen(drbg
); len
++) {
521 *temp_p
^= *df_data_p
;
522 df_data_p
++; temp_p
++;
525 /* 10.2.1.2 step 5 */
526 memcpy(drbg
->C
, temp
, drbg_keylen(drbg
));
527 ret
= crypto_skcipher_setkey(drbg
->ctr_handle
, drbg
->C
,
531 /* 10.2.1.2 step 6 */
532 memcpy(drbg
->V
, temp
+ drbg_keylen(drbg
), drbg_blocklen(drbg
));
533 /* See above: increment counter by one to compensate timing of CTR op */
534 crypto_inc(drbg
->V
, drbg_blocklen(drbg
));
538 memset(temp
, 0, drbg_statelen(drbg
) + drbg_blocklen(drbg
));
540 memset(df_data
, 0, drbg_statelen(drbg
));
545 * scratchpad use: drbg_ctr_update is called independently from
546 * drbg_ctr_extract_bytes. Therefore, the scratchpad is reused
548 /* Generate function of CTR DRBG as defined in 10.2.1.5.2 */
549 static int drbg_ctr_generate(struct drbg_state
*drbg
,
550 unsigned char *buf
, unsigned int buflen
,
551 struct list_head
*addtl
)
554 int len
= min_t(int, buflen
, INT_MAX
);
556 /* 10.2.1.5.2 step 2 */
557 if (addtl
&& !list_empty(addtl
)) {
558 ret
= drbg_ctr_update(drbg
, addtl
, 2);
563 /* 10.2.1.5.2 step 4.1 */
564 ret
= drbg_kcapi_sym_ctr(drbg
, buf
, len
);
568 /* 10.2.1.5.2 step 6 */
569 ret
= drbg_ctr_update(drbg
, NULL
, 3);
576 static const struct drbg_state_ops drbg_ctr_ops
= {
577 .update
= drbg_ctr_update
,
578 .generate
= drbg_ctr_generate
,
579 .crypto_init
= drbg_init_sym_kernel
,
580 .crypto_fini
= drbg_fini_sym_kernel
,
582 #endif /* CONFIG_CRYPTO_DRBG_CTR */
584 /******************************************************************
585 * HMAC DRBG callback functions
586 ******************************************************************/
588 #if defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_HMAC)
589 static int drbg_kcapi_hash(struct drbg_state
*drbg
, unsigned char *outval
,
590 const struct list_head
*in
);
591 static void drbg_kcapi_hmacsetkey(struct drbg_state
*drbg
,
592 const unsigned char *key
);
593 static int drbg_init_hash_kernel(struct drbg_state
*drbg
);
594 static int drbg_fini_hash_kernel(struct drbg_state
*drbg
);
595 #endif /* (CONFIG_CRYPTO_DRBG_HASH || CONFIG_CRYPTO_DRBG_HMAC) */
597 #ifdef CONFIG_CRYPTO_DRBG_HMAC
598 #define CRYPTO_DRBG_HMAC_STRING "HMAC "
599 MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha512");
600 MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha512");
601 MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha384");
602 MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha384");
603 MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha256");
604 MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha256");
605 MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha1");
606 MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha1");
608 /* update function of HMAC DRBG as defined in 10.1.2.2 */
609 static int drbg_hmac_update(struct drbg_state
*drbg
, struct list_head
*seed
,
614 struct drbg_string seed1
, seed2
, vdata
;
616 LIST_HEAD(vdatalist
);
619 /* 10.1.2.3 step 2 -- memset(0) of C is implicit with kzalloc */
620 memset(drbg
->V
, 1, drbg_statelen(drbg
));
621 drbg_kcapi_hmacsetkey(drbg
, drbg
->C
);
624 drbg_string_fill(&seed1
, drbg
->V
, drbg_statelen(drbg
));
625 list_add_tail(&seed1
.list
, &seedlist
);
626 /* buffer of seed2 will be filled in for loop below with one byte */
627 drbg_string_fill(&seed2
, NULL
, 1);
628 list_add_tail(&seed2
.list
, &seedlist
);
629 /* input data of seed is allowed to be NULL at this point */
631 list_splice_tail(seed
, &seedlist
);
633 drbg_string_fill(&vdata
, drbg
->V
, drbg_statelen(drbg
));
634 list_add_tail(&vdata
.list
, &vdatalist
);
635 for (i
= 2; 0 < i
; i
--) {
636 /* first round uses 0x0, second 0x1 */
637 unsigned char prefix
= DRBG_PREFIX0
;
639 prefix
= DRBG_PREFIX1
;
640 /* 10.1.2.2 step 1 and 4 -- concatenation and HMAC for key */
642 ret
= drbg_kcapi_hash(drbg
, drbg
->C
, &seedlist
);
645 drbg_kcapi_hmacsetkey(drbg
, drbg
->C
);
647 /* 10.1.2.2 step 2 and 5 -- HMAC for V */
648 ret
= drbg_kcapi_hash(drbg
, drbg
->V
, &vdatalist
);
652 /* 10.1.2.2 step 3 */
660 /* generate function of HMAC DRBG as defined in 10.1.2.5 */
661 static int drbg_hmac_generate(struct drbg_state
*drbg
,
664 struct list_head
*addtl
)
668 struct drbg_string data
;
671 /* 10.1.2.5 step 2 */
672 if (addtl
&& !list_empty(addtl
)) {
673 ret
= drbg_hmac_update(drbg
, addtl
, 1);
678 drbg_string_fill(&data
, drbg
->V
, drbg_statelen(drbg
));
679 list_add_tail(&data
.list
, &datalist
);
680 while (len
< buflen
) {
681 unsigned int outlen
= 0;
682 /* 10.1.2.5 step 4.1 */
683 ret
= drbg_kcapi_hash(drbg
, drbg
->V
, &datalist
);
686 outlen
= (drbg_blocklen(drbg
) < (buflen
- len
)) ?
687 drbg_blocklen(drbg
) : (buflen
- len
);
689 /* 10.1.2.5 step 4.2 */
690 memcpy(buf
+ len
, drbg
->V
, outlen
);
694 /* 10.1.2.5 step 6 */
695 if (addtl
&& !list_empty(addtl
))
696 ret
= drbg_hmac_update(drbg
, addtl
, 1);
698 ret
= drbg_hmac_update(drbg
, NULL
, 1);
705 static const struct drbg_state_ops drbg_hmac_ops
= {
706 .update
= drbg_hmac_update
,
707 .generate
= drbg_hmac_generate
,
708 .crypto_init
= drbg_init_hash_kernel
,
709 .crypto_fini
= drbg_fini_hash_kernel
,
711 #endif /* CONFIG_CRYPTO_DRBG_HMAC */
713 /******************************************************************
714 * Hash DRBG callback functions
715 ******************************************************************/
717 #ifdef CONFIG_CRYPTO_DRBG_HASH
718 #define CRYPTO_DRBG_HASH_STRING "HASH "
719 MODULE_ALIAS_CRYPTO("drbg_pr_sha512");
720 MODULE_ALIAS_CRYPTO("drbg_nopr_sha512");
721 MODULE_ALIAS_CRYPTO("drbg_pr_sha384");
722 MODULE_ALIAS_CRYPTO("drbg_nopr_sha384");
723 MODULE_ALIAS_CRYPTO("drbg_pr_sha256");
724 MODULE_ALIAS_CRYPTO("drbg_nopr_sha256");
725 MODULE_ALIAS_CRYPTO("drbg_pr_sha1");
726 MODULE_ALIAS_CRYPTO("drbg_nopr_sha1");
731 * @dst buffer to increment
734 static inline void drbg_add_buf(unsigned char *dst
, size_t dstlen
,
735 const unsigned char *add
, size_t addlen
)
737 /* implied: dstlen > addlen */
738 unsigned char *dstptr
;
739 const unsigned char *addptr
;
740 unsigned int remainder
= 0;
743 dstptr
= dst
+ (dstlen
-1);
744 addptr
= add
+ (addlen
-1);
746 remainder
+= *dstptr
+ *addptr
;
747 *dstptr
= remainder
& 0xff;
749 len
--; dstptr
--; addptr
--;
751 len
= dstlen
- addlen
;
752 while (len
&& remainder
> 0) {
753 remainder
= *dstptr
+ 1;
754 *dstptr
= remainder
& 0xff;
761 * scratchpad usage: as drbg_hash_update and drbg_hash_df are used
762 * interlinked, the scratchpad is used as follows:
764 * start: drbg->scratchpad
765 * length: drbg_statelen(drbg)
767 * start: drbg->scratchpad + drbg_statelen(drbg)
768 * length: drbg_blocklen(drbg)
770 * drbg_hash_process_addtl uses the scratchpad, but fully completes
771 * before either of the functions mentioned before are invoked. Therefore,
772 * drbg_hash_process_addtl does not need to be specifically considered.
775 /* Derivation Function for Hash DRBG as defined in 10.4.1 */
776 static int drbg_hash_df(struct drbg_state
*drbg
,
777 unsigned char *outval
, size_t outlen
,
778 struct list_head
*entropylist
)
782 unsigned char input
[5];
783 unsigned char *tmp
= drbg
->scratchpad
+ drbg_statelen(drbg
);
784 struct drbg_string data
;
788 drbg_cpu_to_be32((outlen
* 8), &input
[1]);
790 /* 10.4.1 step 4.1 -- concatenation of data for input into hash */
791 drbg_string_fill(&data
, input
, 5);
792 list_add(&data
.list
, entropylist
);
795 while (len
< outlen
) {
797 /* 10.4.1 step 4.1 */
798 ret
= drbg_kcapi_hash(drbg
, tmp
, entropylist
);
801 /* 10.4.1 step 4.2 */
803 blocklen
= (drbg_blocklen(drbg
) < (outlen
- len
)) ?
804 drbg_blocklen(drbg
) : (outlen
- len
);
805 memcpy(outval
+ len
, tmp
, blocklen
);
810 memset(tmp
, 0, drbg_blocklen(drbg
));
814 /* update function for Hash DRBG as defined in 10.1.1.2 / 10.1.1.3 */
815 static int drbg_hash_update(struct drbg_state
*drbg
, struct list_head
*seed
,
819 struct drbg_string data1
, data2
;
821 LIST_HEAD(datalist2
);
822 unsigned char *V
= drbg
->scratchpad
;
823 unsigned char prefix
= DRBG_PREFIX1
;
829 /* 10.1.1.3 step 1 */
830 memcpy(V
, drbg
->V
, drbg_statelen(drbg
));
831 drbg_string_fill(&data1
, &prefix
, 1);
832 list_add_tail(&data1
.list
, &datalist
);
833 drbg_string_fill(&data2
, V
, drbg_statelen(drbg
));
834 list_add_tail(&data2
.list
, &datalist
);
836 list_splice_tail(seed
, &datalist
);
838 /* 10.1.1.2 / 10.1.1.3 step 2 and 3 */
839 ret
= drbg_hash_df(drbg
, drbg
->V
, drbg_statelen(drbg
), &datalist
);
843 /* 10.1.1.2 / 10.1.1.3 step 4 */
844 prefix
= DRBG_PREFIX0
;
845 drbg_string_fill(&data1
, &prefix
, 1);
846 list_add_tail(&data1
.list
, &datalist2
);
847 drbg_string_fill(&data2
, drbg
->V
, drbg_statelen(drbg
));
848 list_add_tail(&data2
.list
, &datalist2
);
849 /* 10.1.1.2 / 10.1.1.3 step 4 */
850 ret
= drbg_hash_df(drbg
, drbg
->C
, drbg_statelen(drbg
), &datalist2
);
853 memset(drbg
->scratchpad
, 0, drbg_statelen(drbg
));
857 /* processing of additional information string for Hash DRBG */
858 static int drbg_hash_process_addtl(struct drbg_state
*drbg
,
859 struct list_head
*addtl
)
862 struct drbg_string data1
, data2
;
864 unsigned char prefix
= DRBG_PREFIX2
;
866 /* 10.1.1.4 step 2 */
867 if (!addtl
|| list_empty(addtl
))
870 /* 10.1.1.4 step 2a */
871 drbg_string_fill(&data1
, &prefix
, 1);
872 drbg_string_fill(&data2
, drbg
->V
, drbg_statelen(drbg
));
873 list_add_tail(&data1
.list
, &datalist
);
874 list_add_tail(&data2
.list
, &datalist
);
875 list_splice_tail(addtl
, &datalist
);
876 ret
= drbg_kcapi_hash(drbg
, drbg
->scratchpad
, &datalist
);
880 /* 10.1.1.4 step 2b */
881 drbg_add_buf(drbg
->V
, drbg_statelen(drbg
),
882 drbg
->scratchpad
, drbg_blocklen(drbg
));
885 memset(drbg
->scratchpad
, 0, drbg_blocklen(drbg
));
889 /* Hashgen defined in 10.1.1.4 */
890 static int drbg_hash_hashgen(struct drbg_state
*drbg
,
896 unsigned char *src
= drbg
->scratchpad
;
897 unsigned char *dst
= drbg
->scratchpad
+ drbg_statelen(drbg
);
898 struct drbg_string data
;
901 /* 10.1.1.4 step hashgen 2 */
902 memcpy(src
, drbg
->V
, drbg_statelen(drbg
));
904 drbg_string_fill(&data
, src
, drbg_statelen(drbg
));
905 list_add_tail(&data
.list
, &datalist
);
906 while (len
< buflen
) {
907 unsigned int outlen
= 0;
908 /* 10.1.1.4 step hashgen 4.1 */
909 ret
= drbg_kcapi_hash(drbg
, dst
, &datalist
);
914 outlen
= (drbg_blocklen(drbg
) < (buflen
- len
)) ?
915 drbg_blocklen(drbg
) : (buflen
- len
);
916 /* 10.1.1.4 step hashgen 4.2 */
917 memcpy(buf
+ len
, dst
, outlen
);
919 /* 10.1.1.4 hashgen step 4.3 */
921 crypto_inc(src
, drbg_statelen(drbg
));
925 memset(drbg
->scratchpad
, 0,
926 (drbg_statelen(drbg
) + drbg_blocklen(drbg
)));
930 /* generate function for Hash DRBG as defined in 10.1.1.4 */
931 static int drbg_hash_generate(struct drbg_state
*drbg
,
932 unsigned char *buf
, unsigned int buflen
,
933 struct list_head
*addtl
)
938 unsigned char req
[8];
941 unsigned char prefix
= DRBG_PREFIX3
;
942 struct drbg_string data1
, data2
;
945 /* 10.1.1.4 step 2 */
946 ret
= drbg_hash_process_addtl(drbg
, addtl
);
949 /* 10.1.1.4 step 3 */
950 len
= drbg_hash_hashgen(drbg
, buf
, buflen
);
952 /* this is the value H as documented in 10.1.1.4 */
953 /* 10.1.1.4 step 4 */
954 drbg_string_fill(&data1
, &prefix
, 1);
955 list_add_tail(&data1
.list
, &datalist
);
956 drbg_string_fill(&data2
, drbg
->V
, drbg_statelen(drbg
));
957 list_add_tail(&data2
.list
, &datalist
);
958 ret
= drbg_kcapi_hash(drbg
, drbg
->scratchpad
, &datalist
);
964 /* 10.1.1.4 step 5 */
965 drbg_add_buf(drbg
->V
, drbg_statelen(drbg
),
966 drbg
->scratchpad
, drbg_blocklen(drbg
));
967 drbg_add_buf(drbg
->V
, drbg_statelen(drbg
),
968 drbg
->C
, drbg_statelen(drbg
));
969 u
.req_int
= cpu_to_be64(drbg
->reseed_ctr
);
970 drbg_add_buf(drbg
->V
, drbg_statelen(drbg
), u
.req
, 8);
973 memset(drbg
->scratchpad
, 0, drbg_blocklen(drbg
));
978 * scratchpad usage: as update and generate are used isolated, both
979 * can use the scratchpad
981 static const struct drbg_state_ops drbg_hash_ops
= {
982 .update
= drbg_hash_update
,
983 .generate
= drbg_hash_generate
,
984 .crypto_init
= drbg_init_hash_kernel
,
985 .crypto_fini
= drbg_fini_hash_kernel
,
987 #endif /* CONFIG_CRYPTO_DRBG_HASH */
989 /******************************************************************
990 * Functions common for DRBG implementations
991 ******************************************************************/
993 static inline int __drbg_seed(struct drbg_state
*drbg
, struct list_head
*seed
,
996 int ret
= drbg
->d_ops
->update(drbg
, seed
, reseed
);
1001 drbg
->seeded
= true;
1002 /* 10.1.1.2 / 10.1.1.3 step 5 */
1003 drbg
->reseed_ctr
= 1;
1008 static void drbg_async_seed(struct work_struct
*work
)
1010 struct drbg_string data
;
1011 LIST_HEAD(seedlist
);
1012 struct drbg_state
*drbg
= container_of(work
, struct drbg_state
,
1014 unsigned int entropylen
= drbg_sec_strength(drbg
->core
->flags
);
1015 unsigned char entropy
[32];
1017 BUG_ON(!entropylen
);
1018 BUG_ON(entropylen
> sizeof(entropy
));
1019 get_random_bytes(entropy
, entropylen
);
1021 drbg_string_fill(&data
, entropy
, entropylen
);
1022 list_add_tail(&data
.list
, &seedlist
);
1024 mutex_lock(&drbg
->drbg_mutex
);
1026 /* If nonblocking pool is initialized, deactivate Jitter RNG */
1027 crypto_free_rng(drbg
->jent
);
1030 /* Set seeded to false so that if __drbg_seed fails the
1031 * next generate call will trigger a reseed.
1033 drbg
->seeded
= false;
1035 __drbg_seed(drbg
, &seedlist
, true);
1038 drbg
->reseed_threshold
= drbg_max_requests(drbg
);
1040 mutex_unlock(&drbg
->drbg_mutex
);
1042 memzero_explicit(entropy
, entropylen
);
1046 * Seeding or reseeding of the DRBG
1048 * @drbg: DRBG state struct
1049 * @pers: personalization / additional information buffer
1050 * @reseed: 0 for initial seed process, 1 for reseeding
1054 * error value otherwise
1056 static int drbg_seed(struct drbg_state
*drbg
, struct drbg_string
*pers
,
1060 unsigned char entropy
[((32 + 16) * 2)];
1061 unsigned int entropylen
= drbg_sec_strength(drbg
->core
->flags
);
1062 struct drbg_string data1
;
1063 LIST_HEAD(seedlist
);
1065 /* 9.1 / 9.2 / 9.3.1 step 3 */
1066 if (pers
&& pers
->len
> (drbg_max_addtl(drbg
))) {
1067 pr_devel("DRBG: personalization string too long %zu\n",
1072 if (list_empty(&drbg
->test_data
.list
)) {
1073 drbg_string_fill(&data1
, drbg
->test_data
.buf
,
1074 drbg
->test_data
.len
);
1075 pr_devel("DRBG: using test entropy\n");
1078 * Gather entropy equal to the security strength of the DRBG.
1079 * With a derivation function, a nonce is required in addition
1080 * to the entropy. A nonce must be at least 1/2 of the security
1081 * strength of the DRBG in size. Thus, entropy + nonce is 3/2
1082 * of the strength. The consideration of a nonce is only
1083 * applicable during initial seeding.
1085 BUG_ON(!entropylen
);
1087 entropylen
= ((entropylen
+ 1) / 2) * 3;
1088 BUG_ON((entropylen
* 2) > sizeof(entropy
));
1090 /* Get seed from in-kernel /dev/urandom */
1091 get_random_bytes(entropy
, entropylen
);
1094 drbg_string_fill(&data1
, entropy
, entropylen
);
1095 pr_devel("DRBG: (re)seeding with %u bytes of entropy\n",
1098 /* Get seed from Jitter RNG */
1099 ret
= crypto_rng_get_bytes(drbg
->jent
,
1100 entropy
+ entropylen
,
1103 pr_devel("DRBG: jent failed with %d\n", ret
);
1107 drbg_string_fill(&data1
, entropy
, entropylen
* 2);
1108 pr_devel("DRBG: (re)seeding with %u bytes of entropy\n",
1112 list_add_tail(&data1
.list
, &seedlist
);
1115 * concatenation of entropy with personalization str / addtl input)
1116 * the variable pers is directly handed in by the caller, so check its
1117 * contents whether it is appropriate
1119 if (pers
&& pers
->buf
&& 0 < pers
->len
) {
1120 list_add_tail(&pers
->list
, &seedlist
);
1121 pr_devel("DRBG: using personalization string\n");
1125 memset(drbg
->V
, 0, drbg_statelen(drbg
));
1126 memset(drbg
->C
, 0, drbg_statelen(drbg
));
1129 ret
= __drbg_seed(drbg
, &seedlist
, reseed
);
1131 memzero_explicit(entropy
, entropylen
* 2);
1136 /* Free all substructures in a DRBG state without the DRBG state structure */
1137 static inline void drbg_dealloc_state(struct drbg_state
*drbg
)
1145 kzfree(drbg
->scratchpad
);
1146 drbg
->scratchpad
= NULL
;
1147 drbg
->reseed_ctr
= 0;
1153 * Allocate all sub-structures for a DRBG state.
1154 * The DRBG state structure must already be allocated.
1156 static inline int drbg_alloc_state(struct drbg_state
*drbg
)
1159 unsigned int sb_size
= 0;
1161 switch (drbg
->core
->flags
& DRBG_TYPE_MASK
) {
1162 #ifdef CONFIG_CRYPTO_DRBG_HMAC
1164 drbg
->d_ops
= &drbg_hmac_ops
;
1166 #endif /* CONFIG_CRYPTO_DRBG_HMAC */
1167 #ifdef CONFIG_CRYPTO_DRBG_HASH
1169 drbg
->d_ops
= &drbg_hash_ops
;
1171 #endif /* CONFIG_CRYPTO_DRBG_HASH */
1172 #ifdef CONFIG_CRYPTO_DRBG_CTR
1174 drbg
->d_ops
= &drbg_ctr_ops
;
1176 #endif /* CONFIG_CRYPTO_DRBG_CTR */
1182 drbg
->V
= kmalloc(drbg_statelen(drbg
), GFP_KERNEL
);
1185 drbg
->C
= kmalloc(drbg_statelen(drbg
), GFP_KERNEL
);
1188 /* scratchpad is only generated for CTR and Hash */
1189 if (drbg
->core
->flags
& DRBG_HMAC
)
1191 else if (drbg
->core
->flags
& DRBG_CTR
)
1192 sb_size
= drbg_statelen(drbg
) + drbg_blocklen(drbg
) + /* temp */
1193 drbg_statelen(drbg
) + /* df_data */
1194 drbg_blocklen(drbg
) + /* pad */
1195 drbg_blocklen(drbg
) + /* iv */
1196 drbg_statelen(drbg
) + drbg_blocklen(drbg
); /* temp */
1198 sb_size
= drbg_statelen(drbg
) + drbg_blocklen(drbg
);
1201 drbg
->scratchpad
= kzalloc(sb_size
, GFP_KERNEL
);
1202 if (!drbg
->scratchpad
)
1209 drbg_dealloc_state(drbg
);
1213 /*************************************************************************
1214 * DRBG interface functions
1215 *************************************************************************/
1218 * DRBG generate function as required by SP800-90A - this function
1219 * generates random numbers
1221 * @drbg DRBG state handle
1222 * @buf Buffer where to store the random numbers -- the buffer must already
1223 * be pre-allocated by caller
1224 * @buflen Length of output buffer - this value defines the number of random
1225 * bytes pulled from DRBG
1226 * @addtl Additional input that is mixed into state, may be NULL -- note
1227 * the entropy is pulled by the DRBG internally unconditionally
1228 * as defined in SP800-90A. The additional input is mixed into
1229 * the state in addition to the pulled entropy.
1231 * return: 0 when all bytes are generated; < 0 in case of an error
1233 static int drbg_generate(struct drbg_state
*drbg
,
1234 unsigned char *buf
, unsigned int buflen
,
1235 struct drbg_string
*addtl
)
1238 LIST_HEAD(addtllist
);
1241 pr_devel("DRBG: not yet seeded\n");
1244 if (0 == buflen
|| !buf
) {
1245 pr_devel("DRBG: no output buffer provided\n");
1248 if (addtl
&& NULL
== addtl
->buf
&& 0 < addtl
->len
) {
1249 pr_devel("DRBG: wrong format of additional information\n");
1255 if (buflen
> (drbg_max_request_bytes(drbg
))) {
1256 pr_devel("DRBG: requested random numbers too large %u\n",
1261 /* 9.3.1 step 3 is implicit with the chosen DRBG */
1264 if (addtl
&& addtl
->len
> (drbg_max_addtl(drbg
))) {
1265 pr_devel("DRBG: additional information string too long %zu\n",
1269 /* 9.3.1 step 5 is implicit with the chosen DRBG */
1272 * 9.3.1 step 6 and 9 supplemented by 9.3.2 step c is implemented
1273 * here. The spec is a bit convoluted here, we make it simpler.
1275 if (drbg
->reseed_threshold
< drbg
->reseed_ctr
)
1276 drbg
->seeded
= false;
1278 if (drbg
->pr
|| !drbg
->seeded
) {
1279 pr_devel("DRBG: reseeding before generation (prediction "
1280 "resistance: %s, state %s)\n",
1281 drbg
->pr
? "true" : "false",
1282 drbg
->seeded
? "seeded" : "unseeded");
1283 /* 9.3.1 steps 7.1 through 7.3 */
1284 len
= drbg_seed(drbg
, addtl
, true);
1287 /* 9.3.1 step 7.4 */
1291 if (addtl
&& 0 < addtl
->len
)
1292 list_add_tail(&addtl
->list
, &addtllist
);
1293 /* 9.3.1 step 8 and 10 */
1294 len
= drbg
->d_ops
->generate(drbg
, buf
, buflen
, &addtllist
);
1296 /* 10.1.1.4 step 6, 10.1.2.5 step 7, 10.2.1.5.2 step 7 */
1302 * Section 11.3.3 requires to re-perform self tests after some
1303 * generated random numbers. The chosen value after which self
1304 * test is performed is arbitrary, but it should be reasonable.
1305 * However, we do not perform the self tests because of the following
1306 * reasons: it is mathematically impossible that the initial self tests
1307 * were successfully and the following are not. If the initial would
1308 * pass and the following would not, the kernel integrity is violated.
1309 * In this case, the entire kernel operation is questionable and it
1310 * is unlikely that the integrity violation only affects the
1311 * correct operation of the DRBG.
1313 * Albeit the following code is commented out, it is provided in
1314 * case somebody has a need to implement the test of 11.3.3.
1317 if (drbg
->reseed_ctr
&& !(drbg
->reseed_ctr
% 4096)) {
1319 pr_devel("DRBG: start to perform self test\n");
1320 if (drbg
->core
->flags
& DRBG_HMAC
)
1321 err
= alg_test("drbg_pr_hmac_sha256",
1322 "drbg_pr_hmac_sha256", 0, 0);
1323 else if (drbg
->core
->flags
& DRBG_CTR
)
1324 err
= alg_test("drbg_pr_ctr_aes128",
1325 "drbg_pr_ctr_aes128", 0, 0);
1327 err
= alg_test("drbg_pr_sha256",
1328 "drbg_pr_sha256", 0, 0);
1330 pr_err("DRBG: periodical self test failed\n");
1332 * uninstantiate implies that from now on, only errors
1333 * are returned when reusing this DRBG cipher handle
1335 drbg_uninstantiate(drbg
);
1338 pr_devel("DRBG: self test successful\n");
1344 * All operations were successful, return 0 as mandated by
1345 * the kernel crypto API interface.
1353 * Wrapper around drbg_generate which can pull arbitrary long strings
1354 * from the DRBG without hitting the maximum request limitation.
1356 * Parameters: see drbg_generate
1357 * Return codes: see drbg_generate -- if one drbg_generate request fails,
1358 * the entire drbg_generate_long request fails
1360 static int drbg_generate_long(struct drbg_state
*drbg
,
1361 unsigned char *buf
, unsigned int buflen
,
1362 struct drbg_string
*addtl
)
1364 unsigned int len
= 0;
1365 unsigned int slice
= 0;
1368 unsigned int chunk
= 0;
1369 slice
= ((buflen
- len
) / drbg_max_request_bytes(drbg
));
1370 chunk
= slice
? drbg_max_request_bytes(drbg
) : (buflen
- len
);
1371 mutex_lock(&drbg
->drbg_mutex
);
1372 err
= drbg_generate(drbg
, buf
+ len
, chunk
, addtl
);
1373 mutex_unlock(&drbg
->drbg_mutex
);
1377 } while (slice
> 0 && (len
< buflen
));
1381 static void drbg_schedule_async_seed(struct random_ready_callback
*rdy
)
1383 struct drbg_state
*drbg
= container_of(rdy
, struct drbg_state
,
1386 schedule_work(&drbg
->seed_work
);
1389 static int drbg_prepare_hrng(struct drbg_state
*drbg
)
1393 /* We do not need an HRNG in test mode. */
1394 if (list_empty(&drbg
->test_data
.list
))
1397 INIT_WORK(&drbg
->seed_work
, drbg_async_seed
);
1399 drbg
->random_ready
.owner
= THIS_MODULE
;
1400 drbg
->random_ready
.func
= drbg_schedule_async_seed
;
1402 err
= add_random_ready_callback(&drbg
->random_ready
);
1413 drbg
->random_ready
.func
= NULL
;
1417 drbg
->jent
= crypto_alloc_rng("jitterentropy_rng", 0, 0);
1420 * Require frequent reseeds until the seed source is fully
1423 drbg
->reseed_threshold
= 50;
1429 * DRBG instantiation function as required by SP800-90A - this function
1430 * sets up the DRBG handle, performs the initial seeding and all sanity
1431 * checks required by SP800-90A
1433 * @drbg memory of state -- if NULL, new memory is allocated
1434 * @pers Personalization string that is mixed into state, may be NULL -- note
1435 * the entropy is pulled by the DRBG internally unconditionally
1436 * as defined in SP800-90A. The additional input is mixed into
1437 * the state in addition to the pulled entropy.
1438 * @coreref reference to core
1439 * @pr prediction resistance enabled
1443 * error value otherwise
1445 static int drbg_instantiate(struct drbg_state
*drbg
, struct drbg_string
*pers
,
1446 int coreref
, bool pr
)
1451 pr_devel("DRBG: Initializing DRBG core %d with prediction resistance "
1452 "%s\n", coreref
, pr
? "enabled" : "disabled");
1453 mutex_lock(&drbg
->drbg_mutex
);
1455 /* 9.1 step 1 is implicit with the selected DRBG type */
1458 * 9.1 step 2 is implicit as caller can select prediction resistance
1459 * and the flag is copied into drbg->flags --
1460 * all DRBG types support prediction resistance
1463 /* 9.1 step 4 is implicit in drbg_sec_strength */
1466 drbg
->core
= &drbg_cores
[coreref
];
1468 drbg
->seeded
= false;
1469 drbg
->reseed_threshold
= drbg_max_requests(drbg
);
1471 ret
= drbg_alloc_state(drbg
);
1476 if (drbg
->d_ops
->crypto_init(drbg
))
1479 ret
= drbg_prepare_hrng(drbg
);
1481 goto free_everything
;
1483 if (IS_ERR(drbg
->jent
)) {
1484 ret
= PTR_ERR(drbg
->jent
);
1486 if (fips_enabled
|| ret
!= -ENOENT
)
1487 goto free_everything
;
1488 pr_info("DRBG: Continuing without Jitter RNG\n");
1494 ret
= drbg_seed(drbg
, pers
, reseed
);
1497 goto free_everything
;
1499 mutex_unlock(&drbg
->drbg_mutex
);
1503 drbg_dealloc_state(drbg
);
1505 mutex_unlock(&drbg
->drbg_mutex
);
1509 mutex_unlock(&drbg
->drbg_mutex
);
1510 drbg_uninstantiate(drbg
);
1515 * DRBG uninstantiate function as required by SP800-90A - this function
1516 * frees all buffers and the DRBG handle
1518 * @drbg DRBG state handle
1523 static int drbg_uninstantiate(struct drbg_state
*drbg
)
1525 if (drbg
->random_ready
.func
) {
1526 del_random_ready_callback(&drbg
->random_ready
);
1527 cancel_work_sync(&drbg
->seed_work
);
1528 crypto_free_rng(drbg
->jent
);
1533 drbg
->d_ops
->crypto_fini(drbg
);
1534 drbg_dealloc_state(drbg
);
1535 /* no scrubbing of test_data -- this shall survive an uninstantiate */
1540 * Helper function for setting the test data in the DRBG
1542 * @drbg DRBG state handle
1544 * @len test data length
1546 static void drbg_kcapi_set_entropy(struct crypto_rng
*tfm
,
1547 const u8
*data
, unsigned int len
)
1549 struct drbg_state
*drbg
= crypto_rng_ctx(tfm
);
1551 mutex_lock(&drbg
->drbg_mutex
);
1552 drbg_string_fill(&drbg
->test_data
, data
, len
);
1553 mutex_unlock(&drbg
->drbg_mutex
);
1556 /***************************************************************
1557 * Kernel crypto API cipher invocations requested by DRBG
1558 ***************************************************************/
1560 #if defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_HMAC)
1562 struct shash_desc shash
;
1566 static int drbg_init_hash_kernel(struct drbg_state
*drbg
)
1568 struct sdesc
*sdesc
;
1569 struct crypto_shash
*tfm
;
1571 tfm
= crypto_alloc_shash(drbg
->core
->backend_cra_name
, 0, 0);
1573 pr_info("DRBG: could not allocate digest TFM handle: %s\n",
1574 drbg
->core
->backend_cra_name
);
1575 return PTR_ERR(tfm
);
1577 BUG_ON(drbg_blocklen(drbg
) != crypto_shash_digestsize(tfm
));
1578 sdesc
= kzalloc(sizeof(struct shash_desc
) + crypto_shash_descsize(tfm
),
1581 crypto_free_shash(tfm
);
1585 sdesc
->shash
.tfm
= tfm
;
1586 sdesc
->shash
.flags
= 0;
1587 drbg
->priv_data
= sdesc
;
1591 static int drbg_fini_hash_kernel(struct drbg_state
*drbg
)
1593 struct sdesc
*sdesc
= (struct sdesc
*)drbg
->priv_data
;
1595 crypto_free_shash(sdesc
->shash
.tfm
);
1598 drbg
->priv_data
= NULL
;
1602 static void drbg_kcapi_hmacsetkey(struct drbg_state
*drbg
,
1603 const unsigned char *key
)
1605 struct sdesc
*sdesc
= (struct sdesc
*)drbg
->priv_data
;
1607 crypto_shash_setkey(sdesc
->shash
.tfm
, key
, drbg_statelen(drbg
));
1610 static int drbg_kcapi_hash(struct drbg_state
*drbg
, unsigned char *outval
,
1611 const struct list_head
*in
)
1613 struct sdesc
*sdesc
= (struct sdesc
*)drbg
->priv_data
;
1614 struct drbg_string
*input
= NULL
;
1616 crypto_shash_init(&sdesc
->shash
);
1617 list_for_each_entry(input
, in
, list
)
1618 crypto_shash_update(&sdesc
->shash
, input
->buf
, input
->len
);
1619 return crypto_shash_final(&sdesc
->shash
, outval
);
1621 #endif /* (CONFIG_CRYPTO_DRBG_HASH || CONFIG_CRYPTO_DRBG_HMAC) */
1623 #ifdef CONFIG_CRYPTO_DRBG_CTR
1624 static int drbg_fini_sym_kernel(struct drbg_state
*drbg
)
1626 struct crypto_cipher
*tfm
=
1627 (struct crypto_cipher
*)drbg
->priv_data
;
1629 crypto_free_cipher(tfm
);
1630 drbg
->priv_data
= NULL
;
1632 if (drbg
->ctr_handle
)
1633 crypto_free_skcipher(drbg
->ctr_handle
);
1634 drbg
->ctr_handle
= NULL
;
1637 skcipher_request_free(drbg
->ctr_req
);;
1638 drbg
->ctr_req
= NULL
;
1640 kfree(drbg
->ctr_null_value_buf
);
1641 drbg
->ctr_null_value
= NULL
;
1646 static void drbg_skcipher_cb(struct crypto_async_request
*req
, int error
)
1648 struct drbg_state
*drbg
= req
->data
;
1650 if (error
== -EINPROGRESS
)
1652 drbg
->ctr_async_err
= error
;
1653 complete(&drbg
->ctr_completion
);
1656 #define DRBG_CTR_NULL_LEN 128
1657 static int drbg_init_sym_kernel(struct drbg_state
*drbg
)
1659 struct crypto_cipher
*tfm
;
1660 struct crypto_skcipher
*sk_tfm
;
1661 struct skcipher_request
*req
;
1662 unsigned int alignmask
;
1663 char ctr_name
[CRYPTO_MAX_ALG_NAME
];
1665 tfm
= crypto_alloc_cipher(drbg
->core
->backend_cra_name
, 0, 0);
1667 pr_info("DRBG: could not allocate cipher TFM handle: %s\n",
1668 drbg
->core
->backend_cra_name
);
1669 return PTR_ERR(tfm
);
1671 BUG_ON(drbg_blocklen(drbg
) != crypto_cipher_blocksize(tfm
));
1672 drbg
->priv_data
= tfm
;
1674 if (snprintf(ctr_name
, CRYPTO_MAX_ALG_NAME
, "ctr(%s)",
1675 drbg
->core
->backend_cra_name
) >= CRYPTO_MAX_ALG_NAME
) {
1676 drbg_fini_sym_kernel(drbg
);
1679 sk_tfm
= crypto_alloc_skcipher(ctr_name
, 0, 0);
1680 if (IS_ERR(sk_tfm
)) {
1681 pr_info("DRBG: could not allocate CTR cipher TFM handle: %s\n",
1683 drbg_fini_sym_kernel(drbg
);
1684 return PTR_ERR(sk_tfm
);
1686 drbg
->ctr_handle
= sk_tfm
;
1688 req
= skcipher_request_alloc(sk_tfm
, GFP_KERNEL
);
1690 pr_info("DRBG: could not allocate request queue\n");
1691 drbg_fini_sym_kernel(drbg
);
1692 return PTR_ERR(req
);
1694 drbg
->ctr_req
= req
;
1695 skcipher_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_BACKLOG
,
1696 drbg_skcipher_cb
, drbg
);
1698 alignmask
= crypto_skcipher_alignmask(sk_tfm
);
1699 drbg
->ctr_null_value_buf
= kzalloc(DRBG_CTR_NULL_LEN
+ alignmask
,
1701 if (!drbg
->ctr_null_value_buf
) {
1702 drbg_fini_sym_kernel(drbg
);
1705 drbg
->ctr_null_value
= (u8
*)PTR_ALIGN(drbg
->ctr_null_value_buf
,
1711 static void drbg_kcapi_symsetkey(struct drbg_state
*drbg
,
1712 const unsigned char *key
)
1714 struct crypto_cipher
*tfm
=
1715 (struct crypto_cipher
*)drbg
->priv_data
;
1717 crypto_cipher_setkey(tfm
, key
, (drbg_keylen(drbg
)));
1720 static int drbg_kcapi_sym(struct drbg_state
*drbg
, unsigned char *outval
,
1721 const struct drbg_string
*in
)
1723 struct crypto_cipher
*tfm
=
1724 (struct crypto_cipher
*)drbg
->priv_data
;
1726 /* there is only component in *in */
1727 BUG_ON(in
->len
< drbg_blocklen(drbg
));
1728 crypto_cipher_encrypt_one(tfm
, outval
, in
->buf
);
1732 static int drbg_kcapi_sym_ctr(struct drbg_state
*drbg
, u8
*outbuf
, u32 outlen
)
1734 struct scatterlist sg_in
;
1736 sg_init_one(&sg_in
, drbg
->ctr_null_value
, DRBG_CTR_NULL_LEN
);
1739 u32 cryptlen
= min_t(u32
, outlen
, DRBG_CTR_NULL_LEN
);
1740 struct scatterlist sg_out
;
1743 sg_init_one(&sg_out
, outbuf
, cryptlen
);
1744 skcipher_request_set_crypt(drbg
->ctr_req
, &sg_in
, &sg_out
,
1746 ret
= crypto_skcipher_encrypt(drbg
->ctr_req
);
1752 ret
= wait_for_completion_interruptible(
1753 &drbg
->ctr_completion
);
1754 if (!ret
&& !drbg
->ctr_async_err
) {
1755 reinit_completion(&drbg
->ctr_completion
);
1761 init_completion(&drbg
->ctr_completion
);
1768 #endif /* CONFIG_CRYPTO_DRBG_CTR */
1770 /***************************************************************
1771 * Kernel crypto API interface to register DRBG
1772 ***************************************************************/
1775 * Look up the DRBG flags by given kernel crypto API cra_name
1776 * The code uses the drbg_cores definition to do this
1778 * @cra_name kernel crypto API cra_name
1779 * @coreref reference to integer which is filled with the pointer to
1780 * the applicable core
1781 * @pr reference for setting prediction resistance
1785 static inline void drbg_convert_tfm_core(const char *cra_driver_name
,
1786 int *coreref
, bool *pr
)
1793 /* disassemble the names */
1794 if (!memcmp(cra_driver_name
, "drbg_nopr_", 10)) {
1797 } else if (!memcmp(cra_driver_name
, "drbg_pr_", 8)) {
1803 /* remove the first part */
1804 len
= strlen(cra_driver_name
) - start
;
1805 for (i
= 0; ARRAY_SIZE(drbg_cores
) > i
; i
++) {
1806 if (!memcmp(cra_driver_name
+ start
, drbg_cores
[i
].cra_name
,
1814 static int drbg_kcapi_init(struct crypto_tfm
*tfm
)
1816 struct drbg_state
*drbg
= crypto_tfm_ctx(tfm
);
1818 mutex_init(&drbg
->drbg_mutex
);
1823 static void drbg_kcapi_cleanup(struct crypto_tfm
*tfm
)
1825 drbg_uninstantiate(crypto_tfm_ctx(tfm
));
1829 * Generate random numbers invoked by the kernel crypto API:
1830 * The API of the kernel crypto API is extended as follows:
1832 * src is additional input supplied to the RNG.
1833 * slen is the length of src.
1834 * dst is the output buffer where random data is to be stored.
1835 * dlen is the length of dst.
1837 static int drbg_kcapi_random(struct crypto_rng
*tfm
,
1838 const u8
*src
, unsigned int slen
,
1839 u8
*dst
, unsigned int dlen
)
1841 struct drbg_state
*drbg
= crypto_rng_ctx(tfm
);
1842 struct drbg_string
*addtl
= NULL
;
1843 struct drbg_string string
;
1846 /* linked list variable is now local to allow modification */
1847 drbg_string_fill(&string
, src
, slen
);
1851 return drbg_generate_long(drbg
, dst
, dlen
, addtl
);
1855 * Seed the DRBG invoked by the kernel crypto API
1857 static int drbg_kcapi_seed(struct crypto_rng
*tfm
,
1858 const u8
*seed
, unsigned int slen
)
1860 struct drbg_state
*drbg
= crypto_rng_ctx(tfm
);
1861 struct crypto_tfm
*tfm_base
= crypto_rng_tfm(tfm
);
1863 struct drbg_string string
;
1864 struct drbg_string
*seed_string
= NULL
;
1867 drbg_convert_tfm_core(crypto_tfm_alg_driver_name(tfm_base
), &coreref
,
1870 drbg_string_fill(&string
, seed
, slen
);
1871 seed_string
= &string
;
1874 return drbg_instantiate(drbg
, seed_string
, coreref
, pr
);
1877 /***************************************************************
1878 * Kernel module: code to load the module
1879 ***************************************************************/
1882 * Tests as defined in 11.3.2 in addition to the cipher tests: testing
1883 * of the error handling.
1885 * Note: testing of failing seed source as defined in 11.3.2 is not applicable
1886 * as seed source of get_random_bytes does not fail.
1888 * Note 2: There is no sensible way of testing the reseed counter
1889 * enforcement, so skip it.
1891 static inline int __init
drbg_healthcheck_sanity(void)
1894 #define OUTBUFLEN 16
1895 unsigned char buf
[OUTBUFLEN
];
1896 struct drbg_state
*drbg
= NULL
;
1901 struct drbg_string addtl
;
1902 size_t max_addtllen
, max_request_bytes
;
1904 /* only perform test in FIPS mode */
1908 #ifdef CONFIG_CRYPTO_DRBG_CTR
1909 drbg_convert_tfm_core("drbg_nopr_ctr_aes128", &coreref
, &pr
);
1910 #elif defined CONFIG_CRYPTO_DRBG_HASH
1911 drbg_convert_tfm_core("drbg_nopr_sha256", &coreref
, &pr
);
1913 drbg_convert_tfm_core("drbg_nopr_hmac_sha256", &coreref
, &pr
);
1916 drbg
= kzalloc(sizeof(struct drbg_state
), GFP_KERNEL
);
1920 mutex_init(&drbg
->drbg_mutex
);
1923 * if the following tests fail, it is likely that there is a buffer
1924 * overflow as buf is much smaller than the requested or provided
1925 * string lengths -- in case the error handling does not succeed
1926 * we may get an OOPS. And we want to get an OOPS as this is a
1930 /* get a valid instance of DRBG for following tests */
1931 ret
= drbg_instantiate(drbg
, NULL
, coreref
, pr
);
1936 max_addtllen
= drbg_max_addtl(drbg
);
1937 max_request_bytes
= drbg_max_request_bytes(drbg
);
1938 drbg_string_fill(&addtl
, buf
, max_addtllen
+ 1);
1939 /* overflow addtllen with additonal info string */
1940 len
= drbg_generate(drbg
, buf
, OUTBUFLEN
, &addtl
);
1942 /* overflow max_bits */
1943 len
= drbg_generate(drbg
, buf
, (max_request_bytes
+ 1), NULL
);
1945 drbg_uninstantiate(drbg
);
1947 /* overflow max addtllen with personalization string */
1948 ret
= drbg_instantiate(drbg
, &addtl
, coreref
, pr
);
1950 /* all tests passed */
1953 pr_devel("DRBG: Sanity tests for failure code paths successfully "
1956 drbg_uninstantiate(drbg
);
1962 static struct rng_alg drbg_algs
[22];
1965 * Fill the array drbg_algs used to register the different DRBGs
1966 * with the kernel crypto API. To fill the array, the information
1967 * from drbg_cores[] is used.
1969 static inline void __init
drbg_fill_array(struct rng_alg
*alg
,
1970 const struct drbg_core
*core
, int pr
)
1973 static int priority
= 200;
1975 memcpy(alg
->base
.cra_name
, "stdrng", 6);
1977 memcpy(alg
->base
.cra_driver_name
, "drbg_pr_", 8);
1980 memcpy(alg
->base
.cra_driver_name
, "drbg_nopr_", 10);
1983 memcpy(alg
->base
.cra_driver_name
+ pos
, core
->cra_name
,
1984 strlen(core
->cra_name
));
1986 alg
->base
.cra_priority
= priority
;
1989 * If FIPS mode enabled, the selected DRBG shall have the
1990 * highest cra_priority over other stdrng instances to ensure
1994 alg
->base
.cra_priority
+= 200;
1996 alg
->base
.cra_ctxsize
= sizeof(struct drbg_state
);
1997 alg
->base
.cra_module
= THIS_MODULE
;
1998 alg
->base
.cra_init
= drbg_kcapi_init
;
1999 alg
->base
.cra_exit
= drbg_kcapi_cleanup
;
2000 alg
->generate
= drbg_kcapi_random
;
2001 alg
->seed
= drbg_kcapi_seed
;
2002 alg
->set_ent
= drbg_kcapi_set_entropy
;
2006 static int __init
drbg_init(void)
2008 unsigned int i
= 0; /* pointer to drbg_algs */
2009 unsigned int j
= 0; /* pointer to drbg_cores */
2012 ret
= drbg_healthcheck_sanity();
2016 if (ARRAY_SIZE(drbg_cores
) * 2 > ARRAY_SIZE(drbg_algs
)) {
2017 pr_info("DRBG: Cannot register all DRBG types"
2018 "(slots needed: %zu, slots available: %zu)\n",
2019 ARRAY_SIZE(drbg_cores
) * 2, ARRAY_SIZE(drbg_algs
));
2024 * each DRBG definition can be used with PR and without PR, thus
2025 * we instantiate each DRBG in drbg_cores[] twice.
2027 * As the order of placing them into the drbg_algs array matters
2028 * (the later DRBGs receive a higher cra_priority) we register the
2029 * prediction resistance DRBGs first as the should not be too
2032 for (j
= 0; ARRAY_SIZE(drbg_cores
) > j
; j
++, i
++)
2033 drbg_fill_array(&drbg_algs
[i
], &drbg_cores
[j
], 1);
2034 for (j
= 0; ARRAY_SIZE(drbg_cores
) > j
; j
++, i
++)
2035 drbg_fill_array(&drbg_algs
[i
], &drbg_cores
[j
], 0);
2036 return crypto_register_rngs(drbg_algs
, (ARRAY_SIZE(drbg_cores
) * 2));
2039 static void __exit
drbg_exit(void)
2041 crypto_unregister_rngs(drbg_algs
, (ARRAY_SIZE(drbg_cores
) * 2));
2044 module_init(drbg_init
);
2045 module_exit(drbg_exit
);
2046 #ifndef CRYPTO_DRBG_HASH_STRING
2047 #define CRYPTO_DRBG_HASH_STRING ""
2049 #ifndef CRYPTO_DRBG_HMAC_STRING
2050 #define CRYPTO_DRBG_HMAC_STRING ""
2052 #ifndef CRYPTO_DRBG_CTR_STRING
2053 #define CRYPTO_DRBG_CTR_STRING ""
2055 MODULE_LICENSE("GPL");
2056 MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
2057 MODULE_DESCRIPTION("NIST SP800-90A Deterministic Random Bit Generator (DRBG) "
2058 "using following cores: "
2059 CRYPTO_DRBG_HASH_STRING
2060 CRYPTO_DRBG_HMAC_STRING
2061 CRYPTO_DRBG_CTR_STRING
);
2062 MODULE_ALIAS_CRYPTO("stdrng");