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