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