blob: 2a7860f73288b4fbf2ad343a45ee146da505a666 [file] [log] [blame]
Stephan Mueller541af942014-05-31 15:44:17 +02001/*
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
Stephan Mueller541af942014-05-31 15:44:17 +0200102/***************************************************************
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
Stephan Muellere25e47e2014-07-06 02:23:03 +0200359#define CRYPTO_DRBG_CTR_STRING "CTR "
Stephan Mueller541af942014-05-31 15:44:17 +0200360static int drbg_kcapi_sym(struct drbg_state *drbg, const unsigned char *key,
361 unsigned char *outval, const struct drbg_string *in);
362static int drbg_init_sym_kernel(struct drbg_state *drbg);
363static int drbg_fini_sym_kernel(struct drbg_state *drbg);
364
365/* BCC function for CTR DRBG as defined in 10.4.3 */
366static int drbg_ctr_bcc(struct drbg_state *drbg,
367 unsigned char *out, const unsigned char *key,
Stephan Mueller8c987162014-06-28 21:58:24 +0200368 struct list_head *in)
Stephan Mueller541af942014-05-31 15:44:17 +0200369{
Stephan Mueller8c987162014-06-28 21:58:24 +0200370 int ret = 0;
371 struct drbg_string *curr = NULL;
Stephan Mueller541af942014-05-31 15:44:17 +0200372 struct drbg_string data;
Stephan Mueller8c987162014-06-28 21:58:24 +0200373 short cnt = 0;
Stephan Mueller541af942014-05-31 15:44:17 +0200374
375 drbg_string_fill(&data, out, drbg_blocklen(drbg));
376
377 /* 10.4.3 step 1 */
378 memset(out, 0, drbg_blocklen(drbg));
379
380 /* 10.4.3 step 2 / 4 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200381 list_for_each_entry(curr, in, list) {
382 const unsigned char *pos = curr->buf;
383 size_t len = curr->len;
Stephan Mueller541af942014-05-31 15:44:17 +0200384 /* 10.4.3 step 4.1 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200385 while (len) {
386 /* 10.4.3 step 4.2 */
387 if (drbg_blocklen(drbg) == cnt) {
388 cnt = 0;
389 ret = drbg_kcapi_sym(drbg, key, out, &data);
390 if (ret)
391 return ret;
Stephan Mueller541af942014-05-31 15:44:17 +0200392 }
Stephan Mueller8c987162014-06-28 21:58:24 +0200393 out[cnt] ^= *pos;
394 pos++;
395 cnt++;
396 len--;
Stephan Mueller541af942014-05-31 15:44:17 +0200397 }
Stephan Mueller541af942014-05-31 15:44:17 +0200398 }
Stephan Mueller8c987162014-06-28 21:58:24 +0200399 /* 10.4.3 step 4.2 for last block */
400 if (cnt)
401 ret = drbg_kcapi_sym(drbg, key, out, &data);
402
403 return ret;
Stephan Mueller541af942014-05-31 15:44:17 +0200404}
405
406/*
407 * scratchpad usage: drbg_ctr_update is interlinked with drbg_ctr_df
408 * (and drbg_ctr_bcc, but this function does not need any temporary buffers),
409 * the scratchpad is used as follows:
410 * drbg_ctr_update:
411 * temp
412 * start: drbg->scratchpad
413 * length: drbg_statelen(drbg) + drbg_blocklen(drbg)
414 * note: the cipher writing into this variable works
415 * blocklen-wise. Now, when the statelen is not a multiple
416 * of blocklen, the generateion loop below "spills over"
417 * by at most blocklen. Thus, we need to give sufficient
418 * memory.
419 * df_data
420 * start: drbg->scratchpad +
421 * drbg_statelen(drbg) + drbg_blocklen(drbg)
422 * length: drbg_statelen(drbg)
423 *
424 * drbg_ctr_df:
425 * pad
426 * start: df_data + drbg_statelen(drbg)
427 * length: drbg_blocklen(drbg)
428 * iv
429 * start: pad + drbg_blocklen(drbg)
430 * length: drbg_blocklen(drbg)
431 * temp
432 * start: iv + drbg_blocklen(drbg)
Stephan Mueller8fecaad2014-07-01 17:08:48 +0200433 * length: drbg_satelen(drbg) + drbg_blocklen(drbg)
434 * note: temp is the buffer that the BCC function operates
435 * on. BCC operates blockwise. drbg_statelen(drbg)
436 * is sufficient when the DRBG state length is a multiple
437 * of the block size. For AES192 (and maybe other ciphers)
438 * this is not correct and the length for temp is
439 * insufficient (yes, that also means for such ciphers,
440 * the final output of all BCC rounds are truncated).
441 * Therefore, add drbg_blocklen(drbg) to cover all
442 * possibilities.
Stephan Mueller541af942014-05-31 15:44:17 +0200443 */
444
445/* Derivation Function for CTR DRBG as defined in 10.4.2 */
446static int drbg_ctr_df(struct drbg_state *drbg,
447 unsigned char *df_data, size_t bytes_to_return,
Stephan Mueller8c987162014-06-28 21:58:24 +0200448 struct list_head *seedlist)
Stephan Mueller541af942014-05-31 15:44:17 +0200449{
450 int ret = -EFAULT;
451 unsigned char L_N[8];
452 /* S3 is input */
453 struct drbg_string S1, S2, S4, cipherin;
Stephan Mueller8c987162014-06-28 21:58:24 +0200454 LIST_HEAD(bcc_list);
Stephan Mueller541af942014-05-31 15:44:17 +0200455 unsigned char *pad = df_data + drbg_statelen(drbg);
456 unsigned char *iv = pad + drbg_blocklen(drbg);
457 unsigned char *temp = iv + drbg_blocklen(drbg);
458 size_t padlen = 0;
459 unsigned int templen = 0;
460 /* 10.4.2 step 7 */
461 unsigned int i = 0;
462 /* 10.4.2 step 8 */
463 const unsigned char *K = (unsigned char *)
464 "\x00\x01\x02\x03\x04\x05\x06\x07"
465 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
466 "\x10\x11\x12\x13\x14\x15\x16\x17"
467 "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f";
468 unsigned char *X;
469 size_t generated_len = 0;
470 size_t inputlen = 0;
Stephan Mueller8c987162014-06-28 21:58:24 +0200471 struct drbg_string *seed = NULL;
Stephan Mueller541af942014-05-31 15:44:17 +0200472
473 memset(pad, 0, drbg_blocklen(drbg));
474 memset(iv, 0, drbg_blocklen(drbg));
475 memset(temp, 0, drbg_statelen(drbg));
476
477 /* 10.4.2 step 1 is implicit as we work byte-wise */
478
479 /* 10.4.2 step 2 */
480 if ((512/8) < bytes_to_return)
481 return -EINVAL;
482
483 /* 10.4.2 step 2 -- calculate the entire length of all input data */
Stephan Mueller8c987162014-06-28 21:58:24 +0200484 list_for_each_entry(seed, seedlist, list)
485 inputlen += seed->len;
Stephan Mueller541af942014-05-31 15:44:17 +0200486 drbg_int2byte(&L_N[0], inputlen, 4);
487
488 /* 10.4.2 step 3 */
489 drbg_int2byte(&L_N[4], bytes_to_return, 4);
490
491 /* 10.4.2 step 5: length is L_N, input_string, one byte, padding */
492 padlen = (inputlen + sizeof(L_N) + 1) % (drbg_blocklen(drbg));
493 /* wrap the padlen appropriately */
494 if (padlen)
495 padlen = drbg_blocklen(drbg) - padlen;
496 /*
497 * pad / padlen contains the 0x80 byte and the following zero bytes.
498 * As the calculated padlen value only covers the number of zero
499 * bytes, this value has to be incremented by one for the 0x80 byte.
500 */
501 padlen++;
502 pad[0] = 0x80;
503
504 /* 10.4.2 step 4 -- first fill the linked list and then order it */
505 drbg_string_fill(&S1, iv, drbg_blocklen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +0200506 list_add_tail(&S1.list, &bcc_list);
Stephan Mueller541af942014-05-31 15:44:17 +0200507 drbg_string_fill(&S2, L_N, sizeof(L_N));
Stephan Mueller8c987162014-06-28 21:58:24 +0200508 list_add_tail(&S2.list, &bcc_list);
509 list_splice_tail(seedlist, &bcc_list);
Stephan Mueller541af942014-05-31 15:44:17 +0200510 drbg_string_fill(&S4, pad, padlen);
Stephan Mueller8c987162014-06-28 21:58:24 +0200511 list_add_tail(&S4.list, &bcc_list);
Stephan Mueller541af942014-05-31 15:44:17 +0200512
513 /* 10.4.2 step 9 */
514 while (templen < (drbg_keylen(drbg) + (drbg_blocklen(drbg)))) {
515 /*
516 * 10.4.2 step 9.1 - the padding is implicit as the buffer
517 * holds zeros after allocation -- even the increment of i
518 * is irrelevant as the increment remains within length of i
519 */
520 drbg_int2byte(iv, i, 4);
521 /* 10.4.2 step 9.2 -- BCC and concatenation with temp */
Stephan Mueller8c987162014-06-28 21:58:24 +0200522 ret = drbg_ctr_bcc(drbg, temp + templen, K, &bcc_list);
Stephan Mueller541af942014-05-31 15:44:17 +0200523 if (ret)
524 goto out;
525 /* 10.4.2 step 9.3 */
526 i++;
527 templen += drbg_blocklen(drbg);
528 }
529
530 /* 10.4.2 step 11 */
531 X = temp + (drbg_keylen(drbg));
532 drbg_string_fill(&cipherin, X, drbg_blocklen(drbg));
533
534 /* 10.4.2 step 12: overwriting of outval is implemented in next step */
535
536 /* 10.4.2 step 13 */
537 while (generated_len < bytes_to_return) {
538 short blocklen = 0;
539 /*
540 * 10.4.2 step 13.1: the truncation of the key length is
541 * implicit as the key is only drbg_blocklen in size based on
542 * the implementation of the cipher function callback
543 */
544 ret = drbg_kcapi_sym(drbg, temp, X, &cipherin);
545 if (ret)
546 goto out;
547 blocklen = (drbg_blocklen(drbg) <
548 (bytes_to_return - generated_len)) ?
549 drbg_blocklen(drbg) :
550 (bytes_to_return - generated_len);
551 /* 10.4.2 step 13.2 and 14 */
552 memcpy(df_data + generated_len, X, blocklen);
553 generated_len += blocklen;
554 }
555
556 ret = 0;
557
558out:
559 memset(iv, 0, drbg_blocklen(drbg));
560 memset(temp, 0, drbg_statelen(drbg));
561 memset(pad, 0, drbg_blocklen(drbg));
562 return ret;
563}
564
Stephan Mueller72e7c252014-07-06 02:24:35 +0200565/*
566 * update function of CTR DRBG as defined in 10.2.1.2
567 *
568 * The reseed variable has an enhanced meaning compared to the update
569 * functions of the other DRBGs as follows:
570 * 0 => initial seed from initialization
571 * 1 => reseed via drbg_seed
572 * 2 => first invocation from drbg_ctr_update when addtl is present. In
573 * this case, the df_data scratchpad is not deleted so that it is
574 * available for another calls to prevent calling the DF function
575 * again.
576 * 3 => second invocation from drbg_ctr_update. When the update function
577 * was called with addtl, the df_data memory already contains the
578 * DFed addtl information and we do not need to call DF again.
579 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200580static int drbg_ctr_update(struct drbg_state *drbg, struct list_head *seed,
581 int reseed)
Stephan Mueller541af942014-05-31 15:44:17 +0200582{
583 int ret = -EFAULT;
584 /* 10.2.1.2 step 1 */
585 unsigned char *temp = drbg->scratchpad;
586 unsigned char *df_data = drbg->scratchpad + drbg_statelen(drbg) +
587 drbg_blocklen(drbg);
588 unsigned char *temp_p, *df_data_p; /* pointer to iterate over buffers */
589 unsigned int len = 0;
590 struct drbg_string cipherin;
591 unsigned char prefix = DRBG_PREFIX1;
592
593 memset(temp, 0, drbg_statelen(drbg) + drbg_blocklen(drbg));
Stephan Mueller72e7c252014-07-06 02:24:35 +0200594 if (3 > reseed)
595 memset(df_data, 0, drbg_statelen(drbg));
Stephan Mueller541af942014-05-31 15:44:17 +0200596
597 /* 10.2.1.3.2 step 2 and 10.2.1.4.2 step 2 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200598 if (seed) {
599 ret = drbg_ctr_df(drbg, df_data, drbg_statelen(drbg), seed);
Stephan Mueller541af942014-05-31 15:44:17 +0200600 if (ret)
601 goto out;
602 }
603
604 drbg_string_fill(&cipherin, drbg->V, drbg_blocklen(drbg));
605 /*
606 * 10.2.1.3.2 steps 2 and 3 are already covered as the allocation
607 * zeroizes all memory during initialization
608 */
609 while (len < (drbg_statelen(drbg))) {
610 /* 10.2.1.2 step 2.1 */
611 drbg_add_buf(drbg->V, drbg_blocklen(drbg), &prefix, 1);
612 /*
613 * 10.2.1.2 step 2.2 */
614 ret = drbg_kcapi_sym(drbg, drbg->C, temp + len, &cipherin);
615 if (ret)
616 goto out;
617 /* 10.2.1.2 step 2.3 and 3 */
618 len += drbg_blocklen(drbg);
619 }
620
621 /* 10.2.1.2 step 4 */
622 temp_p = temp;
623 df_data_p = df_data;
624 for (len = 0; len < drbg_statelen(drbg); len++) {
625 *temp_p ^= *df_data_p;
626 df_data_p++; temp_p++;
627 }
628
629 /* 10.2.1.2 step 5 */
630 memcpy(drbg->C, temp, drbg_keylen(drbg));
631 /* 10.2.1.2 step 6 */
632 memcpy(drbg->V, temp + drbg_keylen(drbg), drbg_blocklen(drbg));
633 ret = 0;
634
635out:
636 memset(temp, 0, drbg_statelen(drbg) + drbg_blocklen(drbg));
Stephan Mueller72e7c252014-07-06 02:24:35 +0200637 if (2 != reseed)
638 memset(df_data, 0, drbg_statelen(drbg));
Stephan Mueller541af942014-05-31 15:44:17 +0200639 return ret;
640}
641
642/*
643 * scratchpad use: drbg_ctr_update is called independently from
644 * drbg_ctr_extract_bytes. Therefore, the scratchpad is reused
645 */
646/* Generate function of CTR DRBG as defined in 10.2.1.5.2 */
647static int drbg_ctr_generate(struct drbg_state *drbg,
648 unsigned char *buf, unsigned int buflen,
649 struct drbg_string *addtl)
650{
651 int len = 0;
652 int ret = 0;
653 struct drbg_string data;
654 unsigned char prefix = DRBG_PREFIX1;
655
656 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
657
658 /* 10.2.1.5.2 step 2 */
659 if (addtl && 0 < addtl->len) {
Stephan Mueller8c987162014-06-28 21:58:24 +0200660 LIST_HEAD(addtllist);
661
662 list_add_tail(&addtl->list, &addtllist);
Stephan Mueller72e7c252014-07-06 02:24:35 +0200663 ret = drbg_ctr_update(drbg, &addtllist, 2);
Stephan Mueller541af942014-05-31 15:44:17 +0200664 if (ret)
665 return 0;
666 }
667
668 /* 10.2.1.5.2 step 4.1 */
669 drbg_add_buf(drbg->V, drbg_blocklen(drbg), &prefix, 1);
670 drbg_string_fill(&data, drbg->V, drbg_blocklen(drbg));
671 while (len < buflen) {
672 int outlen = 0;
673 /* 10.2.1.5.2 step 4.2 */
674 ret = drbg_kcapi_sym(drbg, drbg->C, drbg->scratchpad, &data);
675 if (ret) {
676 len = ret;
677 goto out;
678 }
679 outlen = (drbg_blocklen(drbg) < (buflen - len)) ?
680 drbg_blocklen(drbg) : (buflen - len);
681 if (!drbg_fips_continuous_test(drbg, drbg->scratchpad)) {
682 /* 10.2.1.5.2 step 6 */
683 drbg_add_buf(drbg->V, drbg_blocklen(drbg), &prefix, 1);
684 continue;
685 }
686 /* 10.2.1.5.2 step 4.3 */
687 memcpy(buf + len, drbg->scratchpad, outlen);
688 len += outlen;
689 /* 10.2.1.5.2 step 6 */
690 if (len < buflen)
691 drbg_add_buf(drbg->V, drbg_blocklen(drbg), &prefix, 1);
692 }
693
Stephan Mueller72e7c252014-07-06 02:24:35 +0200694 /* 10.2.1.5.2 step 6 */
695 ret = drbg_ctr_update(drbg, NULL, 3);
Stephan Mueller541af942014-05-31 15:44:17 +0200696 if (ret)
697 len = ret;
698
699out:
700 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
701 return len;
702}
703
704static struct drbg_state_ops drbg_ctr_ops = {
705 .update = drbg_ctr_update,
706 .generate = drbg_ctr_generate,
707 .crypto_init = drbg_init_sym_kernel,
708 .crypto_fini = drbg_fini_sym_kernel,
709};
710#endif /* CONFIG_CRYPTO_DRBG_CTR */
711
712/******************************************************************
713 * HMAC DRBG callback functions
714 ******************************************************************/
715
716#if defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_HMAC)
717static int drbg_kcapi_hash(struct drbg_state *drbg, const unsigned char *key,
Stephan Mueller8c987162014-06-28 21:58:24 +0200718 unsigned char *outval, const struct list_head *in);
Stephan Mueller541af942014-05-31 15:44:17 +0200719static int drbg_init_hash_kernel(struct drbg_state *drbg);
720static int drbg_fini_hash_kernel(struct drbg_state *drbg);
721#endif /* (CONFIG_CRYPTO_DRBG_HASH || CONFIG_CRYPTO_DRBG_HMAC) */
722
723#ifdef CONFIG_CRYPTO_DRBG_HMAC
Stephan Muellere25e47e2014-07-06 02:23:03 +0200724#define CRYPTO_DRBG_HMAC_STRING "HMAC "
Stephan Mueller541af942014-05-31 15:44:17 +0200725/* update function of HMAC DRBG as defined in 10.1.2.2 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200726static int drbg_hmac_update(struct drbg_state *drbg, struct list_head *seed,
727 int reseed)
Stephan Mueller541af942014-05-31 15:44:17 +0200728{
729 int ret = -EFAULT;
730 int i = 0;
Stephan Mueller8c987162014-06-28 21:58:24 +0200731 struct drbg_string seed1, seed2, vdata;
732 LIST_HEAD(seedlist);
733 LIST_HEAD(vdatalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200734
735 if (!reseed) {
736 /* 10.1.2.3 step 2 */
737 memset(drbg->C, 0, drbg_statelen(drbg));
738 memset(drbg->V, 1, drbg_statelen(drbg));
739 }
740
741 drbg_string_fill(&seed1, drbg->V, drbg_statelen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +0200742 list_add_tail(&seed1.list, &seedlist);
Stephan Mueller541af942014-05-31 15:44:17 +0200743 /* buffer of seed2 will be filled in for loop below with one byte */
744 drbg_string_fill(&seed2, NULL, 1);
Stephan Mueller8c987162014-06-28 21:58:24 +0200745 list_add_tail(&seed2.list, &seedlist);
Stephan Mueller541af942014-05-31 15:44:17 +0200746 /* input data of seed is allowed to be NULL at this point */
Stephan Mueller8c987162014-06-28 21:58:24 +0200747 if (seed)
748 list_splice_tail(seed, &seedlist);
Stephan Mueller541af942014-05-31 15:44:17 +0200749
Stephan Mueller8c987162014-06-28 21:58:24 +0200750 drbg_string_fill(&vdata, drbg->V, drbg_statelen(drbg));
751 list_add_tail(&vdata.list, &vdatalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200752 for (i = 2; 0 < i; i--) {
753 /* first round uses 0x0, second 0x1 */
754 unsigned char prefix = DRBG_PREFIX0;
755 if (1 == i)
756 prefix = DRBG_PREFIX1;
757 /* 10.1.2.2 step 1 and 4 -- concatenation and HMAC for key */
758 seed2.buf = &prefix;
Stephan Mueller8c987162014-06-28 21:58:24 +0200759 ret = drbg_kcapi_hash(drbg, drbg->C, drbg->C, &seedlist);
Stephan Mueller541af942014-05-31 15:44:17 +0200760 if (ret)
761 return ret;
762
763 /* 10.1.2.2 step 2 and 5 -- HMAC for V */
Stephan Mueller8c987162014-06-28 21:58:24 +0200764 ret = drbg_kcapi_hash(drbg, drbg->C, drbg->V, &vdatalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200765 if (ret)
766 return ret;
767
768 /* 10.1.2.2 step 3 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200769 if (!seed)
Stephan Mueller541af942014-05-31 15:44:17 +0200770 return ret;
771 }
772
773 return 0;
774}
775
776/* generate function of HMAC DRBG as defined in 10.1.2.5 */
777static int drbg_hmac_generate(struct drbg_state *drbg,
778 unsigned char *buf,
779 unsigned int buflen,
780 struct drbg_string *addtl)
781{
782 int len = 0;
783 int ret = 0;
784 struct drbg_string data;
Stephan Mueller8c987162014-06-28 21:58:24 +0200785 LIST_HEAD(datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200786
787 /* 10.1.2.5 step 2 */
788 if (addtl && 0 < addtl->len) {
Stephan Mueller8c987162014-06-28 21:58:24 +0200789 LIST_HEAD(addtllist);
790
791 list_add_tail(&addtl->list, &addtllist);
792 ret = drbg_hmac_update(drbg, &addtllist, 1);
Stephan Mueller541af942014-05-31 15:44:17 +0200793 if (ret)
794 return ret;
795 }
796
797 drbg_string_fill(&data, drbg->V, drbg_statelen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +0200798 list_add_tail(&data.list, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200799 while (len < buflen) {
800 unsigned int outlen = 0;
801 /* 10.1.2.5 step 4.1 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200802 ret = drbg_kcapi_hash(drbg, drbg->C, drbg->V, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200803 if (ret)
804 return ret;
805 outlen = (drbg_blocklen(drbg) < (buflen - len)) ?
806 drbg_blocklen(drbg) : (buflen - len);
807 if (!drbg_fips_continuous_test(drbg, drbg->V))
808 continue;
809
810 /* 10.1.2.5 step 4.2 */
811 memcpy(buf + len, drbg->V, outlen);
812 len += outlen;
813 }
814
815 /* 10.1.2.5 step 6 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200816 if (addtl && 0 < addtl->len) {
817 LIST_HEAD(addtllist);
818
819 list_add_tail(&addtl->list, &addtllist);
820 ret = drbg_hmac_update(drbg, &addtllist, 1);
821 } else {
822 ret = drbg_hmac_update(drbg, NULL, 1);
823 }
Stephan Mueller541af942014-05-31 15:44:17 +0200824 if (ret)
825 return ret;
826
827 return len;
828}
829
830static struct drbg_state_ops drbg_hmac_ops = {
831 .update = drbg_hmac_update,
832 .generate = drbg_hmac_generate,
833 .crypto_init = drbg_init_hash_kernel,
834 .crypto_fini = drbg_fini_hash_kernel,
835
836};
837#endif /* CONFIG_CRYPTO_DRBG_HMAC */
838
839/******************************************************************
840 * Hash DRBG callback functions
841 ******************************************************************/
842
843#ifdef CONFIG_CRYPTO_DRBG_HASH
Stephan Muellere25e47e2014-07-06 02:23:03 +0200844#define CRYPTO_DRBG_HASH_STRING "HASH "
Stephan Mueller541af942014-05-31 15:44:17 +0200845/*
846 * scratchpad usage: as drbg_hash_update and drbg_hash_df are used
847 * interlinked, the scratchpad is used as follows:
848 * drbg_hash_update
849 * start: drbg->scratchpad
850 * length: drbg_statelen(drbg)
851 * drbg_hash_df:
852 * start: drbg->scratchpad + drbg_statelen(drbg)
853 * length: drbg_blocklen(drbg)
854 *
855 * drbg_hash_process_addtl uses the scratchpad, but fully completes
856 * before either of the functions mentioned before are invoked. Therefore,
857 * drbg_hash_process_addtl does not need to be specifically considered.
858 */
859
860/* Derivation Function for Hash DRBG as defined in 10.4.1 */
861static int drbg_hash_df(struct drbg_state *drbg,
862 unsigned char *outval, size_t outlen,
Stephan Mueller8c987162014-06-28 21:58:24 +0200863 struct list_head *entropylist)
Stephan Mueller541af942014-05-31 15:44:17 +0200864{
865 int ret = 0;
866 size_t len = 0;
867 unsigned char input[5];
868 unsigned char *tmp = drbg->scratchpad + drbg_statelen(drbg);
Stephan Mueller8c987162014-06-28 21:58:24 +0200869 struct drbg_string data;
Stephan Mueller541af942014-05-31 15:44:17 +0200870
871 memset(tmp, 0, drbg_blocklen(drbg));
872
873 /* 10.4.1 step 3 */
874 input[0] = 1;
875 drbg_int2byte(&input[1], (outlen * 8), 4);
876
877 /* 10.4.1 step 4.1 -- concatenation of data for input into hash */
Stephan Mueller8c987162014-06-28 21:58:24 +0200878 drbg_string_fill(&data, input, 5);
879 list_add(&data.list, entropylist);
Stephan Mueller541af942014-05-31 15:44:17 +0200880
881 /* 10.4.1 step 4 */
882 while (len < outlen) {
883 short blocklen = 0;
884 /* 10.4.1 step 4.1 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200885 ret = drbg_kcapi_hash(drbg, NULL, tmp, entropylist);
Stephan Mueller541af942014-05-31 15:44:17 +0200886 if (ret)
887 goto out;
888 /* 10.4.1 step 4.2 */
889 input[0]++;
890 blocklen = (drbg_blocklen(drbg) < (outlen - len)) ?
891 drbg_blocklen(drbg) : (outlen - len);
892 memcpy(outval + len, tmp, blocklen);
893 len += blocklen;
894 }
895
896out:
897 memset(tmp, 0, drbg_blocklen(drbg));
898 return ret;
899}
900
901/* update function for Hash DRBG as defined in 10.1.1.2 / 10.1.1.3 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200902static int drbg_hash_update(struct drbg_state *drbg, struct list_head *seed,
Stephan Mueller541af942014-05-31 15:44:17 +0200903 int reseed)
904{
905 int ret = 0;
906 struct drbg_string data1, data2;
Stephan Mueller8c987162014-06-28 21:58:24 +0200907 LIST_HEAD(datalist);
908 LIST_HEAD(datalist2);
Stephan Mueller541af942014-05-31 15:44:17 +0200909 unsigned char *V = drbg->scratchpad;
910 unsigned char prefix = DRBG_PREFIX1;
911
912 memset(drbg->scratchpad, 0, drbg_statelen(drbg));
913 if (!seed)
914 return -EINVAL;
915
916 if (reseed) {
917 /* 10.1.1.3 step 1 */
918 memcpy(V, drbg->V, drbg_statelen(drbg));
919 drbg_string_fill(&data1, &prefix, 1);
Stephan Mueller8c987162014-06-28 21:58:24 +0200920 list_add_tail(&data1.list, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200921 drbg_string_fill(&data2, V, drbg_statelen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +0200922 list_add_tail(&data2.list, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200923 }
Stephan Mueller8c987162014-06-28 21:58:24 +0200924 list_splice_tail(seed, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200925
926 /* 10.1.1.2 / 10.1.1.3 step 2 and 3 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200927 ret = drbg_hash_df(drbg, drbg->V, drbg_statelen(drbg), &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200928 if (ret)
929 goto out;
930
931 /* 10.1.1.2 / 10.1.1.3 step 4 */
932 prefix = DRBG_PREFIX0;
933 drbg_string_fill(&data1, &prefix, 1);
Stephan Mueller8c987162014-06-28 21:58:24 +0200934 list_add_tail(&data1.list, &datalist2);
Stephan Mueller541af942014-05-31 15:44:17 +0200935 drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +0200936 list_add_tail(&data2.list, &datalist2);
Stephan Mueller541af942014-05-31 15:44:17 +0200937 /* 10.1.1.2 / 10.1.1.3 step 4 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200938 ret = drbg_hash_df(drbg, drbg->C, drbg_statelen(drbg), &datalist2);
Stephan Mueller541af942014-05-31 15:44:17 +0200939
940out:
941 memset(drbg->scratchpad, 0, drbg_statelen(drbg));
942 return ret;
943}
944
945/* processing of additional information string for Hash DRBG */
946static int drbg_hash_process_addtl(struct drbg_state *drbg,
947 struct drbg_string *addtl)
948{
949 int ret = 0;
950 struct drbg_string data1, data2;
Stephan Mueller8c987162014-06-28 21:58:24 +0200951 LIST_HEAD(datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200952 unsigned char prefix = DRBG_PREFIX2;
953
954 /* this is value w as per documentation */
955 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
956
957 /* 10.1.1.4 step 2 */
958 if (!addtl || 0 == addtl->len)
959 return 0;
960
961 /* 10.1.1.4 step 2a */
962 drbg_string_fill(&data1, &prefix, 1);
963 drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +0200964 list_add_tail(&data1.list, &datalist);
965 list_add_tail(&data2.list, &datalist);
966 list_add_tail(&addtl->list, &datalist);
967 ret = drbg_kcapi_hash(drbg, NULL, drbg->scratchpad, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200968 if (ret)
969 goto out;
970
971 /* 10.1.1.4 step 2b */
972 drbg_add_buf(drbg->V, drbg_statelen(drbg),
973 drbg->scratchpad, drbg_blocklen(drbg));
974
975out:
976 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
977 return ret;
978}
979
980/* Hashgen defined in 10.1.1.4 */
981static int drbg_hash_hashgen(struct drbg_state *drbg,
982 unsigned char *buf,
983 unsigned int buflen)
984{
985 int len = 0;
986 int ret = 0;
987 unsigned char *src = drbg->scratchpad;
988 unsigned char *dst = drbg->scratchpad + drbg_statelen(drbg);
989 struct drbg_string data;
Stephan Mueller8c987162014-06-28 21:58:24 +0200990 LIST_HEAD(datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200991 unsigned char prefix = DRBG_PREFIX1;
992
993 memset(src, 0, drbg_statelen(drbg));
994 memset(dst, 0, drbg_blocklen(drbg));
995
996 /* 10.1.1.4 step hashgen 2 */
997 memcpy(src, drbg->V, drbg_statelen(drbg));
998
999 drbg_string_fill(&data, src, drbg_statelen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +02001000 list_add_tail(&data.list, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +02001001 while (len < buflen) {
1002 unsigned int outlen = 0;
1003 /* 10.1.1.4 step hashgen 4.1 */
Stephan Mueller8c987162014-06-28 21:58:24 +02001004 ret = drbg_kcapi_hash(drbg, NULL, dst, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +02001005 if (ret) {
1006 len = ret;
1007 goto out;
1008 }
1009 outlen = (drbg_blocklen(drbg) < (buflen - len)) ?
1010 drbg_blocklen(drbg) : (buflen - len);
1011 if (!drbg_fips_continuous_test(drbg, dst)) {
1012 drbg_add_buf(src, drbg_statelen(drbg), &prefix, 1);
1013 continue;
1014 }
1015 /* 10.1.1.4 step hashgen 4.2 */
1016 memcpy(buf + len, dst, outlen);
1017 len += outlen;
1018 /* 10.1.1.4 hashgen step 4.3 */
1019 if (len < buflen)
1020 drbg_add_buf(src, drbg_statelen(drbg), &prefix, 1);
1021 }
1022
1023out:
1024 memset(drbg->scratchpad, 0,
1025 (drbg_statelen(drbg) + drbg_blocklen(drbg)));
1026 return len;
1027}
1028
1029/* generate function for Hash DRBG as defined in 10.1.1.4 */
1030static int drbg_hash_generate(struct drbg_state *drbg,
1031 unsigned char *buf, unsigned int buflen,
1032 struct drbg_string *addtl)
1033{
1034 int len = 0;
1035 int ret = 0;
1036 unsigned char req[8];
1037 unsigned char prefix = DRBG_PREFIX3;
1038 struct drbg_string data1, data2;
Stephan Mueller8c987162014-06-28 21:58:24 +02001039 LIST_HEAD(datalist);
Stephan Mueller541af942014-05-31 15:44:17 +02001040
1041 /* 10.1.1.4 step 2 */
1042 ret = drbg_hash_process_addtl(drbg, addtl);
1043 if (ret)
1044 return ret;
1045 /* 10.1.1.4 step 3 */
1046 len = drbg_hash_hashgen(drbg, buf, buflen);
1047
1048 /* this is the value H as documented in 10.1.1.4 */
1049 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
1050 /* 10.1.1.4 step 4 */
1051 drbg_string_fill(&data1, &prefix, 1);
Stephan Mueller8c987162014-06-28 21:58:24 +02001052 list_add_tail(&data1.list, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +02001053 drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +02001054 list_add_tail(&data2.list, &datalist);
1055 ret = drbg_kcapi_hash(drbg, NULL, drbg->scratchpad, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +02001056 if (ret) {
1057 len = ret;
1058 goto out;
1059 }
1060
1061 /* 10.1.1.4 step 5 */
1062 drbg_add_buf(drbg->V, drbg_statelen(drbg),
1063 drbg->scratchpad, drbg_blocklen(drbg));
1064 drbg_add_buf(drbg->V, drbg_statelen(drbg),
1065 drbg->C, drbg_statelen(drbg));
1066 drbg_int2byte(req, drbg->reseed_ctr, sizeof(req));
1067 drbg_add_buf(drbg->V, drbg_statelen(drbg), req, 8);
1068
1069out:
1070 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
1071 return len;
1072}
1073
1074/*
1075 * scratchpad usage: as update and generate are used isolated, both
1076 * can use the scratchpad
1077 */
1078static struct drbg_state_ops drbg_hash_ops = {
1079 .update = drbg_hash_update,
1080 .generate = drbg_hash_generate,
1081 .crypto_init = drbg_init_hash_kernel,
1082 .crypto_fini = drbg_fini_hash_kernel,
1083};
1084#endif /* CONFIG_CRYPTO_DRBG_HASH */
1085
1086/******************************************************************
1087 * Functions common for DRBG implementations
1088 ******************************************************************/
1089
1090/*
1091 * Seeding or reseeding of the DRBG
1092 *
1093 * @drbg: DRBG state struct
1094 * @pers: personalization / additional information buffer
1095 * @reseed: 0 for initial seed process, 1 for reseeding
1096 *
1097 * return:
1098 * 0 on success
1099 * error value otherwise
1100 */
1101static int drbg_seed(struct drbg_state *drbg, struct drbg_string *pers,
1102 bool reseed)
1103{
1104 int ret = 0;
1105 unsigned char *entropy = NULL;
1106 size_t entropylen = 0;
1107 struct drbg_string data1;
Stephan Mueller8c987162014-06-28 21:58:24 +02001108 LIST_HEAD(seedlist);
Stephan Mueller541af942014-05-31 15:44:17 +02001109
1110 /* 9.1 / 9.2 / 9.3.1 step 3 */
1111 if (pers && pers->len > (drbg_max_addtl(drbg))) {
Stephan Muellera9089572014-07-06 02:24:03 +02001112 pr_devel("DRBG: personalization string too long %zu\n",
Stephan Mueller541af942014-05-31 15:44:17 +02001113 pers->len);
1114 return -EINVAL;
1115 }
1116
1117 if (drbg->test_data && drbg->test_data->testentropy) {
1118 drbg_string_fill(&data1, drbg->test_data->testentropy->buf,
1119 drbg->test_data->testentropy->len);
1120 pr_devel("DRBG: using test entropy\n");
1121 } else {
1122 /*
1123 * Gather entropy equal to the security strength of the DRBG.
1124 * With a derivation function, a nonce is required in addition
1125 * to the entropy. A nonce must be at least 1/2 of the security
1126 * strength of the DRBG in size. Thus, entropy * nonce is 3/2
1127 * of the strength. The consideration of a nonce is only
1128 * applicable during initial seeding.
1129 */
1130 entropylen = drbg_sec_strength(drbg->core->flags);
1131 if (!entropylen)
1132 return -EFAULT;
1133 if (!reseed)
1134 entropylen = ((entropylen + 1) / 2) * 3;
1135 pr_devel("DRBG: (re)seeding with %zu bytes of entropy\n",
1136 entropylen);
1137 entropy = kzalloc(entropylen, GFP_KERNEL);
1138 if (!entropy)
1139 return -ENOMEM;
1140 get_random_bytes(entropy, entropylen);
1141 drbg_string_fill(&data1, entropy, entropylen);
1142 }
Stephan Mueller8c987162014-06-28 21:58:24 +02001143 list_add_tail(&data1.list, &seedlist);
Stephan Mueller541af942014-05-31 15:44:17 +02001144
1145 /*
1146 * concatenation of entropy with personalization str / addtl input)
1147 * the variable pers is directly handed in by the caller, so check its
1148 * contents whether it is appropriate
1149 */
Stephan Mueller8c987162014-06-28 21:58:24 +02001150 if (pers && pers->buf && 0 < pers->len) {
1151 list_add_tail(&pers->list, &seedlist);
Stephan Mueller541af942014-05-31 15:44:17 +02001152 pr_devel("DRBG: using personalization string\n");
1153 }
1154
Stephan Mueller8c987162014-06-28 21:58:24 +02001155 ret = drbg->d_ops->update(drbg, &seedlist, reseed);
Stephan Mueller541af942014-05-31 15:44:17 +02001156 if (ret)
1157 goto out;
1158
1159 drbg->seeded = true;
1160 /* 10.1.1.2 / 10.1.1.3 step 5 */
1161 drbg->reseed_ctr = 1;
1162
1163out:
1164 if (entropy)
1165 kzfree(entropy);
1166 return ret;
1167}
1168
1169/* Free all substructures in a DRBG state without the DRBG state structure */
1170static inline void drbg_dealloc_state(struct drbg_state *drbg)
1171{
1172 if (!drbg)
1173 return;
1174 if (drbg->V)
1175 kzfree(drbg->V);
1176 drbg->V = NULL;
1177 if (drbg->C)
1178 kzfree(drbg->C);
1179 drbg->C = NULL;
1180 if (drbg->scratchpad)
1181 kzfree(drbg->scratchpad);
1182 drbg->scratchpad = NULL;
1183 drbg->reseed_ctr = 0;
1184#ifdef CONFIG_CRYPTO_FIPS
1185 if (drbg->prev)
1186 kzfree(drbg->prev);
1187 drbg->prev = NULL;
1188 drbg->fips_primed = false;
1189#endif
1190}
1191
1192/*
1193 * Allocate all sub-structures for a DRBG state.
1194 * The DRBG state structure must already be allocated.
1195 */
1196static inline int drbg_alloc_state(struct drbg_state *drbg)
1197{
1198 int ret = -ENOMEM;
1199 unsigned int sb_size = 0;
1200
1201 if (!drbg)
1202 return -EINVAL;
1203
1204 drbg->V = kzalloc(drbg_statelen(drbg), GFP_KERNEL);
1205 if (!drbg->V)
1206 goto err;
1207 drbg->C = kzalloc(drbg_statelen(drbg), GFP_KERNEL);
1208 if (!drbg->C)
1209 goto err;
1210#ifdef CONFIG_CRYPTO_FIPS
1211 drbg->prev = kzalloc(drbg_blocklen(drbg), GFP_KERNEL);
1212 if (!drbg->prev)
1213 goto err;
1214 drbg->fips_primed = false;
1215#endif
1216 /* scratchpad is only generated for CTR and Hash */
1217 if (drbg->core->flags & DRBG_HMAC)
1218 sb_size = 0;
1219 else if (drbg->core->flags & DRBG_CTR)
1220 sb_size = drbg_statelen(drbg) + drbg_blocklen(drbg) + /* temp */
1221 drbg_statelen(drbg) + /* df_data */
1222 drbg_blocklen(drbg) + /* pad */
1223 drbg_blocklen(drbg) + /* iv */
Stephan Mueller8fecaad2014-07-01 17:08:48 +02001224 drbg_statelen(drbg) + drbg_blocklen(drbg); /* temp */
Stephan Mueller541af942014-05-31 15:44:17 +02001225 else
1226 sb_size = drbg_statelen(drbg) + drbg_blocklen(drbg);
1227
1228 if (0 < sb_size) {
1229 drbg->scratchpad = kzalloc(sb_size, GFP_KERNEL);
1230 if (!drbg->scratchpad)
1231 goto err;
1232 }
1233 spin_lock_init(&drbg->drbg_lock);
1234 return 0;
1235
1236err:
1237 drbg_dealloc_state(drbg);
1238 return ret;
1239}
1240
1241/*
1242 * Strategy to avoid holding long term locks: generate a shadow copy of DRBG
1243 * and perform all operations on this shadow copy. After finishing, restore
1244 * the updated state of the shadow copy into original drbg state. This way,
1245 * only the read and write operations of the original drbg state must be
1246 * locked
1247 */
1248static inline void drbg_copy_drbg(struct drbg_state *src,
1249 struct drbg_state *dst)
1250{
1251 if (!src || !dst)
1252 return;
1253 memcpy(dst->V, src->V, drbg_statelen(src));
1254 memcpy(dst->C, src->C, drbg_statelen(src));
1255 dst->reseed_ctr = src->reseed_ctr;
1256 dst->seeded = src->seeded;
1257 dst->pr = src->pr;
1258#ifdef CONFIG_CRYPTO_FIPS
1259 dst->fips_primed = src->fips_primed;
1260 memcpy(dst->prev, src->prev, drbg_blocklen(src));
1261#endif
1262 /*
1263 * Not copied:
1264 * scratchpad is initialized drbg_alloc_state;
1265 * priv_data is initialized with call to crypto_init;
1266 * d_ops and core are set outside, as these parameters are const;
1267 * test_data is set outside to prevent it being copied back.
1268 */
1269}
1270
1271static int drbg_make_shadow(struct drbg_state *drbg, struct drbg_state **shadow)
1272{
1273 int ret = -ENOMEM;
1274 struct drbg_state *tmp = NULL;
1275
1276 if (!drbg || !drbg->core || !drbg->V || !drbg->C) {
1277 pr_devel("DRBG: attempt to generate shadow copy for "
1278 "uninitialized DRBG state rejected\n");
1279 return -EINVAL;
1280 }
1281 /* HMAC does not have a scratchpad */
1282 if (!(drbg->core->flags & DRBG_HMAC) && NULL == drbg->scratchpad)
1283 return -EINVAL;
1284
1285 tmp = kzalloc(sizeof(struct drbg_state), GFP_KERNEL);
1286 if (!tmp)
1287 return -ENOMEM;
1288
1289 /* read-only data as they are defined as const, no lock needed */
1290 tmp->core = drbg->core;
1291 tmp->d_ops = drbg->d_ops;
1292
1293 ret = drbg_alloc_state(tmp);
1294 if (ret)
1295 goto err;
1296
1297 spin_lock_bh(&drbg->drbg_lock);
1298 drbg_copy_drbg(drbg, tmp);
1299 /* only make a link to the test buffer, as we only read that data */
1300 tmp->test_data = drbg->test_data;
1301 spin_unlock_bh(&drbg->drbg_lock);
1302 *shadow = tmp;
1303 return 0;
1304
1305err:
1306 if (tmp)
1307 kzfree(tmp);
1308 return ret;
1309}
1310
1311static void drbg_restore_shadow(struct drbg_state *drbg,
1312 struct drbg_state **shadow)
1313{
1314 struct drbg_state *tmp = *shadow;
1315
1316 spin_lock_bh(&drbg->drbg_lock);
1317 drbg_copy_drbg(tmp, drbg);
1318 spin_unlock_bh(&drbg->drbg_lock);
1319 drbg_dealloc_state(tmp);
1320 kzfree(tmp);
1321 *shadow = NULL;
1322}
1323
1324/*************************************************************************
1325 * DRBG interface functions
1326 *************************************************************************/
1327
1328/*
1329 * DRBG generate function as required by SP800-90A - this function
1330 * generates random numbers
1331 *
1332 * @drbg DRBG state handle
1333 * @buf Buffer where to store the random numbers -- the buffer must already
1334 * be pre-allocated by caller
1335 * @buflen Length of output buffer - this value defines the number of random
1336 * bytes pulled from DRBG
1337 * @addtl Additional input that is mixed into state, may be NULL -- note
1338 * the entropy is pulled by the DRBG internally unconditionally
1339 * as defined in SP800-90A. The additional input is mixed into
1340 * the state in addition to the pulled entropy.
1341 *
1342 * return: generated number of bytes
1343 */
1344static int drbg_generate(struct drbg_state *drbg,
1345 unsigned char *buf, unsigned int buflen,
1346 struct drbg_string *addtl)
1347{
1348 int len = 0;
1349 struct drbg_state *shadow = NULL;
1350
1351 if (0 == buflen || !buf) {
1352 pr_devel("DRBG: no output buffer provided\n");
1353 return -EINVAL;
1354 }
1355 if (addtl && NULL == addtl->buf && 0 < addtl->len) {
1356 pr_devel("DRBG: wrong format of additional information\n");
1357 return -EINVAL;
1358 }
1359
1360 len = drbg_make_shadow(drbg, &shadow);
1361 if (len) {
1362 pr_devel("DRBG: shadow copy cannot be generated\n");
1363 return len;
1364 }
1365
1366 /* 9.3.1 step 2 */
1367 len = -EINVAL;
1368 if (buflen > (drbg_max_request_bytes(shadow))) {
1369 pr_devel("DRBG: requested random numbers too large %u\n",
1370 buflen);
1371 goto err;
1372 }
1373
1374 /* 9.3.1 step 3 is implicit with the chosen DRBG */
1375
1376 /* 9.3.1 step 4 */
1377 if (addtl && addtl->len > (drbg_max_addtl(shadow))) {
1378 pr_devel("DRBG: additional information string too long %zu\n",
1379 addtl->len);
1380 goto err;
1381 }
1382 /* 9.3.1 step 5 is implicit with the chosen DRBG */
1383
1384 /*
1385 * 9.3.1 step 6 and 9 supplemented by 9.3.2 step c is implemented
1386 * here. The spec is a bit convoluted here, we make it simpler.
1387 */
1388 if ((drbg_max_requests(shadow)) < shadow->reseed_ctr)
1389 shadow->seeded = false;
1390
1391 /* allocate cipher handle */
1392 if (shadow->d_ops->crypto_init) {
1393 len = shadow->d_ops->crypto_init(shadow);
1394 if (len)
1395 goto err;
1396 }
1397
1398 if (shadow->pr || !shadow->seeded) {
1399 pr_devel("DRBG: reseeding before generation (prediction "
1400 "resistance: %s, state %s)\n",
1401 drbg->pr ? "true" : "false",
1402 drbg->seeded ? "seeded" : "unseeded");
1403 /* 9.3.1 steps 7.1 through 7.3 */
1404 len = drbg_seed(shadow, addtl, true);
1405 if (len)
1406 goto err;
1407 /* 9.3.1 step 7.4 */
1408 addtl = NULL;
1409 }
1410 /* 9.3.1 step 8 and 10 */
1411 len = shadow->d_ops->generate(shadow, buf, buflen, addtl);
1412
1413 /* 10.1.1.4 step 6, 10.1.2.5 step 7, 10.2.1.5.2 step 7 */
1414 shadow->reseed_ctr++;
1415 if (0 >= len)
1416 goto err;
1417
1418 /*
1419 * Section 11.3.3 requires to re-perform self tests after some
1420 * generated random numbers. The chosen value after which self
1421 * test is performed is arbitrary, but it should be reasonable.
1422 * However, we do not perform the self tests because of the following
1423 * reasons: it is mathematically impossible that the initial self tests
1424 * were successfully and the following are not. If the initial would
1425 * pass and the following would not, the kernel integrity is violated.
1426 * In this case, the entire kernel operation is questionable and it
1427 * is unlikely that the integrity violation only affects the
1428 * correct operation of the DRBG.
1429 *
1430 * Albeit the following code is commented out, it is provided in
1431 * case somebody has a need to implement the test of 11.3.3.
1432 */
1433#if 0
1434 if (shadow->reseed_ctr && !(shadow->reseed_ctr % 4096)) {
1435 int err = 0;
1436 pr_devel("DRBG: start to perform self test\n");
1437 if (drbg->core->flags & DRBG_HMAC)
1438 err = alg_test("drbg_pr_hmac_sha256",
1439 "drbg_pr_hmac_sha256", 0, 0);
1440 else if (drbg->core->flags & DRBG_CTR)
1441 err = alg_test("drbg_pr_ctr_aes128",
1442 "drbg_pr_ctr_aes128", 0, 0);
1443 else
1444 err = alg_test("drbg_pr_sha256",
1445 "drbg_pr_sha256", 0, 0);
1446 if (err) {
1447 pr_err("DRBG: periodical self test failed\n");
1448 /*
1449 * uninstantiate implies that from now on, only errors
1450 * are returned when reusing this DRBG cipher handle
1451 */
1452 drbg_uninstantiate(drbg);
1453 drbg_dealloc_state(shadow);
1454 kzfree(shadow);
1455 return 0;
1456 } else {
1457 pr_devel("DRBG: self test successful\n");
1458 }
1459 }
1460#endif
1461
1462err:
1463 if (shadow->d_ops->crypto_fini)
1464 shadow->d_ops->crypto_fini(shadow);
1465 drbg_restore_shadow(drbg, &shadow);
1466 return len;
1467}
1468
1469/*
1470 * Wrapper around drbg_generate which can pull arbitrary long strings
1471 * from the DRBG without hitting the maximum request limitation.
1472 *
1473 * Parameters: see drbg_generate
1474 * Return codes: see drbg_generate -- if one drbg_generate request fails,
1475 * the entire drbg_generate_long request fails
1476 */
1477static int drbg_generate_long(struct drbg_state *drbg,
1478 unsigned char *buf, unsigned int buflen,
1479 struct drbg_string *addtl)
1480{
1481 int len = 0;
1482 unsigned int slice = 0;
1483 do {
1484 int tmplen = 0;
1485 unsigned int chunk = 0;
1486 slice = ((buflen - len) / drbg_max_request_bytes(drbg));
1487 chunk = slice ? drbg_max_request_bytes(drbg) : (buflen - len);
1488 tmplen = drbg_generate(drbg, buf + len, chunk, addtl);
1489 if (0 >= tmplen)
1490 return tmplen;
1491 len += tmplen;
1492 } while (slice > 0);
1493 return len;
1494}
1495
1496/*
1497 * DRBG instantiation function as required by SP800-90A - this function
1498 * sets up the DRBG handle, performs the initial seeding and all sanity
1499 * checks required by SP800-90A
1500 *
1501 * @drbg memory of state -- if NULL, new memory is allocated
1502 * @pers Personalization string that is mixed into state, may be NULL -- note
1503 * the entropy is pulled by the DRBG internally unconditionally
1504 * as defined in SP800-90A. The additional input is mixed into
1505 * the state in addition to the pulled entropy.
1506 * @coreref reference to core
1507 * @pr prediction resistance enabled
1508 *
1509 * return
1510 * 0 on success
1511 * error value otherwise
1512 */
1513static int drbg_instantiate(struct drbg_state *drbg, struct drbg_string *pers,
1514 int coreref, bool pr)
1515{
1516 int ret = -ENOMEM;
1517
1518 pr_devel("DRBG: Initializing DRBG core %d with prediction resistance "
1519 "%s\n", coreref, pr ? "enabled" : "disabled");
1520 drbg->core = &drbg_cores[coreref];
1521 drbg->pr = pr;
1522 drbg->seeded = false;
1523 switch (drbg->core->flags & DRBG_TYPE_MASK) {
1524#ifdef CONFIG_CRYPTO_DRBG_HMAC
1525 case DRBG_HMAC:
1526 drbg->d_ops = &drbg_hmac_ops;
1527 break;
1528#endif /* CONFIG_CRYPTO_DRBG_HMAC */
1529#ifdef CONFIG_CRYPTO_DRBG_HASH
1530 case DRBG_HASH:
1531 drbg->d_ops = &drbg_hash_ops;
1532 break;
1533#endif /* CONFIG_CRYPTO_DRBG_HASH */
1534#ifdef CONFIG_CRYPTO_DRBG_CTR
1535 case DRBG_CTR:
1536 drbg->d_ops = &drbg_ctr_ops;
1537 break;
1538#endif /* CONFIG_CRYPTO_DRBG_CTR */
1539 default:
1540 return -EOPNOTSUPP;
1541 }
1542
1543 /* 9.1 step 1 is implicit with the selected DRBG type */
1544
1545 /*
1546 * 9.1 step 2 is implicit as caller can select prediction resistance
1547 * and the flag is copied into drbg->flags --
1548 * all DRBG types support prediction resistance
1549 */
1550
1551 /* 9.1 step 4 is implicit in drbg_sec_strength */
1552
1553 ret = drbg_alloc_state(drbg);
1554 if (ret)
1555 return ret;
1556
1557 ret = -EFAULT;
1558 if (drbg->d_ops->crypto_init && drbg->d_ops->crypto_init(drbg))
1559 goto err;
1560 ret = drbg_seed(drbg, pers, false);
1561 if (drbg->d_ops->crypto_fini)
1562 drbg->d_ops->crypto_fini(drbg);
1563 if (ret)
1564 goto err;
1565
1566 return 0;
1567
1568err:
1569 drbg_dealloc_state(drbg);
1570 return ret;
1571}
1572
1573/*
1574 * DRBG uninstantiate function as required by SP800-90A - this function
1575 * frees all buffers and the DRBG handle
1576 *
1577 * @drbg DRBG state handle
1578 *
1579 * return
1580 * 0 on success
1581 */
1582static int drbg_uninstantiate(struct drbg_state *drbg)
1583{
1584 spin_lock_bh(&drbg->drbg_lock);
1585 drbg_dealloc_state(drbg);
1586 /* no scrubbing of test_data -- this shall survive an uninstantiate */
1587 spin_unlock_bh(&drbg->drbg_lock);
1588 return 0;
1589}
1590
1591/*
1592 * Helper function for setting the test data in the DRBG
1593 *
1594 * @drbg DRBG state handle
1595 * @test_data test data to sets
1596 */
1597static inline void drbg_set_testdata(struct drbg_state *drbg,
1598 struct drbg_test_data *test_data)
1599{
1600 if (!test_data || !test_data->testentropy)
1601 return;
1602 spin_lock_bh(&drbg->drbg_lock);
1603 drbg->test_data = test_data;
1604 spin_unlock_bh(&drbg->drbg_lock);
1605}
1606
1607/***************************************************************
1608 * Kernel crypto API cipher invocations requested by DRBG
1609 ***************************************************************/
1610
1611#if defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_HMAC)
1612struct sdesc {
1613 struct shash_desc shash;
1614 char ctx[];
1615};
1616
1617static int drbg_init_hash_kernel(struct drbg_state *drbg)
1618{
1619 struct sdesc *sdesc;
1620 struct crypto_shash *tfm;
1621
1622 tfm = crypto_alloc_shash(drbg->core->backend_cra_name, 0, 0);
1623 if (IS_ERR(tfm)) {
1624 pr_info("DRBG: could not allocate digest TFM handle\n");
1625 return PTR_ERR(tfm);
1626 }
1627 BUG_ON(drbg_blocklen(drbg) != crypto_shash_digestsize(tfm));
1628 sdesc = kzalloc(sizeof(struct shash_desc) + crypto_shash_descsize(tfm),
1629 GFP_KERNEL);
1630 if (!sdesc) {
1631 crypto_free_shash(tfm);
1632 return -ENOMEM;
1633 }
1634
1635 sdesc->shash.tfm = tfm;
1636 sdesc->shash.flags = 0;
1637 drbg->priv_data = sdesc;
1638 return 0;
1639}
1640
1641static int drbg_fini_hash_kernel(struct drbg_state *drbg)
1642{
1643 struct sdesc *sdesc = (struct sdesc *)drbg->priv_data;
1644 if (sdesc) {
1645 crypto_free_shash(sdesc->shash.tfm);
1646 kzfree(sdesc);
1647 }
1648 drbg->priv_data = NULL;
1649 return 0;
1650}
1651
1652static int drbg_kcapi_hash(struct drbg_state *drbg, const unsigned char *key,
Stephan Mueller8c987162014-06-28 21:58:24 +02001653 unsigned char *outval, const struct list_head *in)
Stephan Mueller541af942014-05-31 15:44:17 +02001654{
1655 struct sdesc *sdesc = (struct sdesc *)drbg->priv_data;
Stephan Mueller8c987162014-06-28 21:58:24 +02001656 struct drbg_string *input = NULL;
Stephan Mueller541af942014-05-31 15:44:17 +02001657
1658 if (key)
1659 crypto_shash_setkey(sdesc->shash.tfm, key, drbg_statelen(drbg));
1660 crypto_shash_init(&sdesc->shash);
Stephan Mueller8c987162014-06-28 21:58:24 +02001661 list_for_each_entry(input, in, list)
1662 crypto_shash_update(&sdesc->shash, input->buf, input->len);
Stephan Mueller541af942014-05-31 15:44:17 +02001663 return crypto_shash_final(&sdesc->shash, outval);
1664}
1665#endif /* (CONFIG_CRYPTO_DRBG_HASH || CONFIG_CRYPTO_DRBG_HMAC) */
1666
1667#ifdef CONFIG_CRYPTO_DRBG_CTR
1668static int drbg_init_sym_kernel(struct drbg_state *drbg)
1669{
1670 int ret = 0;
1671 struct crypto_blkcipher *tfm;
1672
1673 tfm = crypto_alloc_blkcipher(drbg->core->backend_cra_name, 0, 0);
1674 if (IS_ERR(tfm)) {
1675 pr_info("DRBG: could not allocate cipher TFM handle\n");
1676 return PTR_ERR(tfm);
1677 }
1678 BUG_ON(drbg_blocklen(drbg) != crypto_blkcipher_blocksize(tfm));
1679 drbg->priv_data = tfm;
1680 return ret;
1681}
1682
1683static int drbg_fini_sym_kernel(struct drbg_state *drbg)
1684{
1685 struct crypto_blkcipher *tfm =
1686 (struct crypto_blkcipher *)drbg->priv_data;
1687 if (tfm)
1688 crypto_free_blkcipher(tfm);
1689 drbg->priv_data = NULL;
1690 return 0;
1691}
1692
1693static int drbg_kcapi_sym(struct drbg_state *drbg, const unsigned char *key,
1694 unsigned char *outval, const struct drbg_string *in)
1695{
1696 int ret = 0;
1697 struct scatterlist sg_in, sg_out;
1698 struct blkcipher_desc desc;
1699 struct crypto_blkcipher *tfm =
1700 (struct crypto_blkcipher *)drbg->priv_data;
1701
1702 desc.tfm = tfm;
1703 desc.flags = 0;
1704 crypto_blkcipher_setkey(tfm, key, (drbg_keylen(drbg)));
1705 /* there is only component in *in */
1706 sg_init_one(&sg_in, in->buf, in->len);
1707 sg_init_one(&sg_out, outval, drbg_blocklen(drbg));
1708 ret = crypto_blkcipher_encrypt(&desc, &sg_out, &sg_in, in->len);
1709
1710 return ret;
1711}
1712#endif /* CONFIG_CRYPTO_DRBG_CTR */
1713
1714/***************************************************************
1715 * Kernel crypto API interface to register DRBG
1716 ***************************************************************/
1717
1718/*
1719 * Look up the DRBG flags by given kernel crypto API cra_name
1720 * The code uses the drbg_cores definition to do this
1721 *
1722 * @cra_name kernel crypto API cra_name
1723 * @coreref reference to integer which is filled with the pointer to
1724 * the applicable core
1725 * @pr reference for setting prediction resistance
1726 *
1727 * return: flags
1728 */
1729static inline void drbg_convert_tfm_core(const char *cra_driver_name,
1730 int *coreref, bool *pr)
1731{
1732 int i = 0;
1733 size_t start = 0;
1734 int len = 0;
1735
1736 *pr = true;
1737 /* disassemble the names */
1738 if (!memcmp(cra_driver_name, "drbg_nopr_", 10)) {
1739 start = 10;
1740 *pr = false;
1741 } else if (!memcmp(cra_driver_name, "drbg_pr_", 8)) {
1742 start = 8;
1743 } else {
1744 return;
1745 }
1746
1747 /* remove the first part */
1748 len = strlen(cra_driver_name) - start;
1749 for (i = 0; ARRAY_SIZE(drbg_cores) > i; i++) {
1750 if (!memcmp(cra_driver_name + start, drbg_cores[i].cra_name,
1751 len)) {
1752 *coreref = i;
1753 return;
1754 }
1755 }
1756}
1757
1758static int drbg_kcapi_init(struct crypto_tfm *tfm)
1759{
1760 struct drbg_state *drbg = crypto_tfm_ctx(tfm);
1761 bool pr = false;
1762 int coreref = 0;
1763
Stephan Mueller4f150712014-07-06 02:25:04 +02001764 drbg_convert_tfm_core(crypto_tfm_alg_driver_name(tfm), &coreref, &pr);
Stephan Mueller541af942014-05-31 15:44:17 +02001765 /*
1766 * when personalization string is needed, the caller must call reset
1767 * and provide the personalization string as seed information
1768 */
1769 return drbg_instantiate(drbg, NULL, coreref, pr);
1770}
1771
1772static void drbg_kcapi_cleanup(struct crypto_tfm *tfm)
1773{
1774 drbg_uninstantiate(crypto_tfm_ctx(tfm));
1775}
1776
1777/*
1778 * Generate random numbers invoked by the kernel crypto API:
1779 * The API of the kernel crypto API is extended as follows:
1780 *
1781 * If dlen is larger than zero, rdata is interpreted as the output buffer
1782 * where random data is to be stored.
1783 *
1784 * If dlen is zero, rdata is interpreted as a pointer to a struct drbg_gen
1785 * which holds the additional information string that is used for the
1786 * DRBG generation process. The output buffer that is to be used to store
1787 * data is also pointed to by struct drbg_gen.
1788 */
1789static int drbg_kcapi_random(struct crypto_rng *tfm, u8 *rdata,
1790 unsigned int dlen)
1791{
1792 struct drbg_state *drbg = crypto_rng_ctx(tfm);
1793 if (0 < dlen) {
1794 return drbg_generate_long(drbg, rdata, dlen, NULL);
1795 } else {
1796 struct drbg_gen *data = (struct drbg_gen *)rdata;
Stephan Mueller8c987162014-06-28 21:58:24 +02001797 struct drbg_string addtl;
Stephan Mueller541af942014-05-31 15:44:17 +02001798 /* catch NULL pointer */
1799 if (!data)
1800 return 0;
1801 drbg_set_testdata(drbg, data->test_data);
Stephan Mueller8c987162014-06-28 21:58:24 +02001802 /* linked list variable is now local to allow modification */
1803 drbg_string_fill(&addtl, data->addtl->buf, data->addtl->len);
Stephan Mueller541af942014-05-31 15:44:17 +02001804 return drbg_generate_long(drbg, data->outbuf, data->outlen,
Stephan Mueller8c987162014-06-28 21:58:24 +02001805 &addtl);
Stephan Mueller541af942014-05-31 15:44:17 +02001806 }
1807}
1808
1809/*
1810 * Reset the DRBG invoked by the kernel crypto API
1811 * The reset implies a full re-initialization of the DRBG. Similar to the
1812 * generate function of drbg_kcapi_random, this function extends the
1813 * kernel crypto API interface with struct drbg_gen
1814 */
1815static int drbg_kcapi_reset(struct crypto_rng *tfm, u8 *seed, unsigned int slen)
1816{
1817 struct drbg_state *drbg = crypto_rng_ctx(tfm);
1818 struct crypto_tfm *tfm_base = crypto_rng_tfm(tfm);
1819 bool pr = false;
1820 struct drbg_string seed_string;
1821 int coreref = 0;
1822
1823 drbg_uninstantiate(drbg);
1824 drbg_convert_tfm_core(crypto_tfm_alg_driver_name(tfm_base), &coreref,
1825 &pr);
1826 if (0 < slen) {
1827 drbg_string_fill(&seed_string, seed, slen);
1828 return drbg_instantiate(drbg, &seed_string, coreref, pr);
1829 } else {
1830 struct drbg_gen *data = (struct drbg_gen *)seed;
1831 /* allow invocation of API call with NULL, 0 */
1832 if (!data)
1833 return drbg_instantiate(drbg, NULL, coreref, pr);
1834 drbg_set_testdata(drbg, data->test_data);
Stephan Mueller8c987162014-06-28 21:58:24 +02001835 /* linked list variable is now local to allow modification */
1836 drbg_string_fill(&seed_string, data->addtl->buf,
1837 data->addtl->len);
1838 return drbg_instantiate(drbg, &seed_string, coreref, pr);
Stephan Mueller541af942014-05-31 15:44:17 +02001839 }
1840}
1841
1842/***************************************************************
1843 * Kernel module: code to load the module
1844 ***************************************************************/
1845
1846/*
1847 * Tests as defined in 11.3.2 in addition to the cipher tests: testing
1848 * of the error handling.
1849 *
1850 * Note: testing of failing seed source as defined in 11.3.2 is not applicable
1851 * as seed source of get_random_bytes does not fail.
1852 *
1853 * Note 2: There is no sensible way of testing the reseed counter
1854 * enforcement, so skip it.
1855 */
1856static inline int __init drbg_healthcheck_sanity(void)
1857{
1858#ifdef CONFIG_CRYPTO_FIPS
1859 int len = 0;
1860#define OUTBUFLEN 16
1861 unsigned char buf[OUTBUFLEN];
1862 struct drbg_state *drbg = NULL;
1863 int ret = -EFAULT;
1864 int rc = -EFAULT;
1865 bool pr = false;
1866 int coreref = 0;
1867 struct drbg_string addtl;
1868 size_t max_addtllen, max_request_bytes;
1869
1870 /* only perform test in FIPS mode */
1871 if (!fips_enabled)
1872 return 0;
1873
1874#ifdef CONFIG_CRYPTO_DRBG_CTR
1875 drbg_convert_tfm_core("drbg_nopr_ctr_aes128", &coreref, &pr);
Stephan Muellere25e47e2014-07-06 02:23:03 +02001876#elif defined CONFIG_CRYPTO_DRBG_HASH
Stephan Mueller541af942014-05-31 15:44:17 +02001877 drbg_convert_tfm_core("drbg_nopr_sha256", &coreref, &pr);
1878#else
1879 drbg_convert_tfm_core("drbg_nopr_hmac_sha256", &coreref, &pr);
1880#endif
1881
1882 drbg = kzalloc(sizeof(struct drbg_state), GFP_KERNEL);
1883 if (!drbg)
1884 return -ENOMEM;
1885
1886 /*
1887 * if the following tests fail, it is likely that there is a buffer
1888 * overflow as buf is much smaller than the requested or provided
1889 * string lengths -- in case the error handling does not succeed
1890 * we may get an OOPS. And we want to get an OOPS as this is a
1891 * grave bug.
1892 */
1893
1894 /* get a valid instance of DRBG for following tests */
1895 ret = drbg_instantiate(drbg, NULL, coreref, pr);
1896 if (ret) {
1897 rc = ret;
1898 goto outbuf;
1899 }
1900 max_addtllen = drbg_max_addtl(drbg);
1901 max_request_bytes = drbg_max_request_bytes(drbg);
1902 drbg_string_fill(&addtl, buf, max_addtllen + 1);
1903 /* overflow addtllen with additonal info string */
1904 len = drbg_generate(drbg, buf, OUTBUFLEN, &addtl);
1905 BUG_ON(0 < len);
1906 /* overflow max_bits */
1907 len = drbg_generate(drbg, buf, (max_request_bytes + 1), NULL);
1908 BUG_ON(0 < len);
1909 drbg_uninstantiate(drbg);
1910
1911 /* overflow max addtllen with personalization string */
1912 ret = drbg_instantiate(drbg, &addtl, coreref, pr);
1913 BUG_ON(0 == ret);
1914 /* test uninstantated DRBG */
1915 len = drbg_generate(drbg, buf, (max_request_bytes + 1), NULL);
1916 BUG_ON(0 < len);
1917 /* all tests passed */
1918 rc = 0;
1919
1920 pr_devel("DRBG: Sanity tests for failure code paths successfully "
1921 "completed\n");
1922
1923 drbg_uninstantiate(drbg);
1924outbuf:
1925 kzfree(drbg);
1926 return rc;
1927#else /* CONFIG_CRYPTO_FIPS */
1928 return 0;
1929#endif /* CONFIG_CRYPTO_FIPS */
1930}
1931
1932static struct crypto_alg drbg_algs[22];
1933
1934/*
1935 * Fill the array drbg_algs used to register the different DRBGs
1936 * with the kernel crypto API. To fill the array, the information
1937 * from drbg_cores[] is used.
1938 */
1939static inline void __init drbg_fill_array(struct crypto_alg *alg,
1940 const struct drbg_core *core, int pr)
1941{
1942 int pos = 0;
1943 static int priority = 100;
1944
1945 memset(alg, 0, sizeof(struct crypto_alg));
1946 memcpy(alg->cra_name, "stdrng", 6);
1947 if (pr) {
1948 memcpy(alg->cra_driver_name, "drbg_pr_", 8);
1949 pos = 8;
1950 } else {
1951 memcpy(alg->cra_driver_name, "drbg_nopr_", 10);
1952 pos = 10;
1953 }
1954 memcpy(alg->cra_driver_name + pos, core->cra_name,
1955 strlen(core->cra_name));
1956
1957 alg->cra_priority = priority;
1958 priority++;
1959 /*
1960 * If FIPS mode enabled, the selected DRBG shall have the
1961 * highest cra_priority over other stdrng instances to ensure
1962 * it is selected.
1963 */
1964 if (fips_enabled)
1965 alg->cra_priority += 200;
1966
1967 alg->cra_flags = CRYPTO_ALG_TYPE_RNG;
1968 alg->cra_ctxsize = sizeof(struct drbg_state);
1969 alg->cra_type = &crypto_rng_type;
1970 alg->cra_module = THIS_MODULE;
1971 alg->cra_init = drbg_kcapi_init;
1972 alg->cra_exit = drbg_kcapi_cleanup;
1973 alg->cra_u.rng.rng_make_random = drbg_kcapi_random;
1974 alg->cra_u.rng.rng_reset = drbg_kcapi_reset;
1975 alg->cra_u.rng.seedsize = 0;
1976}
1977
1978static int __init drbg_init(void)
1979{
1980 unsigned int i = 0; /* pointer to drbg_algs */
1981 unsigned int j = 0; /* pointer to drbg_cores */
1982 int ret = -EFAULT;
1983
1984 ret = drbg_healthcheck_sanity();
1985 if (ret)
1986 return ret;
1987
1988 if (ARRAY_SIZE(drbg_cores) * 2 > ARRAY_SIZE(drbg_algs)) {
1989 pr_info("DRBG: Cannot register all DRBG types"
Stephan Muellera9089572014-07-06 02:24:03 +02001990 "(slots needed: %zu, slots available: %zu)\n",
Stephan Mueller541af942014-05-31 15:44:17 +02001991 ARRAY_SIZE(drbg_cores) * 2, ARRAY_SIZE(drbg_algs));
1992 return ret;
1993 }
1994
1995 /*
1996 * each DRBG definition can be used with PR and without PR, thus
1997 * we instantiate each DRBG in drbg_cores[] twice.
1998 *
1999 * As the order of placing them into the drbg_algs array matters
2000 * (the later DRBGs receive a higher cra_priority) we register the
2001 * prediction resistance DRBGs first as the should not be too
2002 * interesting.
2003 */
2004 for (j = 0; ARRAY_SIZE(drbg_cores) > j; j++, i++)
2005 drbg_fill_array(&drbg_algs[i], &drbg_cores[j], 1);
2006 for (j = 0; ARRAY_SIZE(drbg_cores) > j; j++, i++)
2007 drbg_fill_array(&drbg_algs[i], &drbg_cores[j], 0);
2008 return crypto_register_algs(drbg_algs, (ARRAY_SIZE(drbg_cores) * 2));
2009}
2010
2011void __exit drbg_exit(void)
2012{
2013 crypto_unregister_algs(drbg_algs, (ARRAY_SIZE(drbg_cores) * 2));
2014}
2015
2016module_init(drbg_init);
2017module_exit(drbg_exit);
Stephan Muellere25e47e2014-07-06 02:23:03 +02002018#ifndef CRYPTO_DRBG_HASH_STRING
2019#define CRYPTO_DRBG_HASH_STRING ""
2020#endif
2021#ifndef CRYPTO_DRBG_HMAC_STRING
2022#define CRYPTO_DRBG_HMAC_STRING ""
2023#endif
2024#ifndef CRYPTO_DRBG_CTR_STRING
2025#define CRYPTO_DRBG_CTR_STRING ""
2026#endif
Stephan Mueller541af942014-05-31 15:44:17 +02002027MODULE_LICENSE("GPL");
2028MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
Stephan Muellere25e47e2014-07-06 02:23:03 +02002029MODULE_DESCRIPTION("NIST SP800-90A Deterministic Random Bit Generator (DRBG) "
2030 "using following cores: "
2031 CRYPTO_DRBG_HASH_STRING
2032 CRYPTO_DRBG_HMAC_STRING
2033 CRYPTO_DRBG_CTR_STRING);