blob: cce915b9d6bc67a1beabeefb3524904ff02f62e1 [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
565/* update function of CTR DRBG as defined in 10.2.1.2 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200566static int drbg_ctr_update(struct drbg_state *drbg, struct list_head *seed,
567 int reseed)
Stephan Mueller541af942014-05-31 15:44:17 +0200568{
569 int ret = -EFAULT;
570 /* 10.2.1.2 step 1 */
571 unsigned char *temp = drbg->scratchpad;
572 unsigned char *df_data = drbg->scratchpad + drbg_statelen(drbg) +
573 drbg_blocklen(drbg);
574 unsigned char *temp_p, *df_data_p; /* pointer to iterate over buffers */
575 unsigned int len = 0;
576 struct drbg_string cipherin;
577 unsigned char prefix = DRBG_PREFIX1;
578
579 memset(temp, 0, drbg_statelen(drbg) + drbg_blocklen(drbg));
580 memset(df_data, 0, drbg_statelen(drbg));
581
582 /* 10.2.1.3.2 step 2 and 10.2.1.4.2 step 2 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200583 if (seed) {
584 ret = drbg_ctr_df(drbg, df_data, drbg_statelen(drbg), seed);
Stephan Mueller541af942014-05-31 15:44:17 +0200585 if (ret)
586 goto out;
587 }
588
589 drbg_string_fill(&cipherin, drbg->V, drbg_blocklen(drbg));
590 /*
591 * 10.2.1.3.2 steps 2 and 3 are already covered as the allocation
592 * zeroizes all memory during initialization
593 */
594 while (len < (drbg_statelen(drbg))) {
595 /* 10.2.1.2 step 2.1 */
596 drbg_add_buf(drbg->V, drbg_blocklen(drbg), &prefix, 1);
597 /*
598 * 10.2.1.2 step 2.2 */
599 ret = drbg_kcapi_sym(drbg, drbg->C, temp + len, &cipherin);
600 if (ret)
601 goto out;
602 /* 10.2.1.2 step 2.3 and 3 */
603 len += drbg_blocklen(drbg);
604 }
605
606 /* 10.2.1.2 step 4 */
607 temp_p = temp;
608 df_data_p = df_data;
609 for (len = 0; len < drbg_statelen(drbg); len++) {
610 *temp_p ^= *df_data_p;
611 df_data_p++; temp_p++;
612 }
613
614 /* 10.2.1.2 step 5 */
615 memcpy(drbg->C, temp, drbg_keylen(drbg));
616 /* 10.2.1.2 step 6 */
617 memcpy(drbg->V, temp + drbg_keylen(drbg), drbg_blocklen(drbg));
618 ret = 0;
619
620out:
621 memset(temp, 0, drbg_statelen(drbg) + drbg_blocklen(drbg));
622 memset(df_data, 0, drbg_statelen(drbg));
623 return ret;
624}
625
626/*
627 * scratchpad use: drbg_ctr_update is called independently from
628 * drbg_ctr_extract_bytes. Therefore, the scratchpad is reused
629 */
630/* Generate function of CTR DRBG as defined in 10.2.1.5.2 */
631static int drbg_ctr_generate(struct drbg_state *drbg,
632 unsigned char *buf, unsigned int buflen,
633 struct drbg_string *addtl)
634{
635 int len = 0;
636 int ret = 0;
637 struct drbg_string data;
638 unsigned char prefix = DRBG_PREFIX1;
639
640 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
641
642 /* 10.2.1.5.2 step 2 */
643 if (addtl && 0 < addtl->len) {
Stephan Mueller8c987162014-06-28 21:58:24 +0200644 LIST_HEAD(addtllist);
645
646 list_add_tail(&addtl->list, &addtllist);
647 ret = drbg_ctr_update(drbg, &addtllist, 1);
Stephan Mueller541af942014-05-31 15:44:17 +0200648 if (ret)
649 return 0;
650 }
651
652 /* 10.2.1.5.2 step 4.1 */
653 drbg_add_buf(drbg->V, drbg_blocklen(drbg), &prefix, 1);
654 drbg_string_fill(&data, drbg->V, drbg_blocklen(drbg));
655 while (len < buflen) {
656 int outlen = 0;
657 /* 10.2.1.5.2 step 4.2 */
658 ret = drbg_kcapi_sym(drbg, drbg->C, drbg->scratchpad, &data);
659 if (ret) {
660 len = ret;
661 goto out;
662 }
663 outlen = (drbg_blocklen(drbg) < (buflen - len)) ?
664 drbg_blocklen(drbg) : (buflen - len);
665 if (!drbg_fips_continuous_test(drbg, drbg->scratchpad)) {
666 /* 10.2.1.5.2 step 6 */
667 drbg_add_buf(drbg->V, drbg_blocklen(drbg), &prefix, 1);
668 continue;
669 }
670 /* 10.2.1.5.2 step 4.3 */
671 memcpy(buf + len, drbg->scratchpad, outlen);
672 len += outlen;
673 /* 10.2.1.5.2 step 6 */
674 if (len < buflen)
675 drbg_add_buf(drbg->V, drbg_blocklen(drbg), &prefix, 1);
676 }
677
Stephan Mueller541af942014-05-31 15:44:17 +0200678 /*
Stephan Mueller8c987162014-06-28 21:58:24 +0200679 * 10.2.1.5.2 step 6
Stephan Mueller541af942014-05-31 15:44:17 +0200680 * The following call invokes the DF function again which could be
681 * optimized. In step 2, the "additional_input" after step 2 is the
682 * output of the DF function. If this result would be saved, the DF
683 * function would not need to be invoked again at this point.
684 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200685 if (addtl && 0 < addtl->len) {
686 LIST_HEAD(addtllist);
687
688 list_add_tail(&addtl->list, &addtllist);
689 ret = drbg_ctr_update(drbg, &addtllist, 1);
690 } else {
691 ret = drbg_ctr_update(drbg, NULL, 1);
692 }
Stephan Mueller541af942014-05-31 15:44:17 +0200693 if (ret)
694 len = ret;
695
696out:
697 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
698 return len;
699}
700
701static struct drbg_state_ops drbg_ctr_ops = {
702 .update = drbg_ctr_update,
703 .generate = drbg_ctr_generate,
704 .crypto_init = drbg_init_sym_kernel,
705 .crypto_fini = drbg_fini_sym_kernel,
706};
707#endif /* CONFIG_CRYPTO_DRBG_CTR */
708
709/******************************************************************
710 * HMAC DRBG callback functions
711 ******************************************************************/
712
713#if defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_HMAC)
714static int drbg_kcapi_hash(struct drbg_state *drbg, const unsigned char *key,
Stephan Mueller8c987162014-06-28 21:58:24 +0200715 unsigned char *outval, const struct list_head *in);
Stephan Mueller541af942014-05-31 15:44:17 +0200716static int drbg_init_hash_kernel(struct drbg_state *drbg);
717static int drbg_fini_hash_kernel(struct drbg_state *drbg);
718#endif /* (CONFIG_CRYPTO_DRBG_HASH || CONFIG_CRYPTO_DRBG_HMAC) */
719
720#ifdef CONFIG_CRYPTO_DRBG_HMAC
Stephan Muellere25e47e2014-07-06 02:23:03 +0200721#define CRYPTO_DRBG_HMAC_STRING "HMAC "
Stephan Mueller541af942014-05-31 15:44:17 +0200722/* update function of HMAC DRBG as defined in 10.1.2.2 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200723static int drbg_hmac_update(struct drbg_state *drbg, struct list_head *seed,
724 int reseed)
Stephan Mueller541af942014-05-31 15:44:17 +0200725{
726 int ret = -EFAULT;
727 int i = 0;
Stephan Mueller8c987162014-06-28 21:58:24 +0200728 struct drbg_string seed1, seed2, vdata;
729 LIST_HEAD(seedlist);
730 LIST_HEAD(vdatalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200731
732 if (!reseed) {
733 /* 10.1.2.3 step 2 */
734 memset(drbg->C, 0, drbg_statelen(drbg));
735 memset(drbg->V, 1, drbg_statelen(drbg));
736 }
737
738 drbg_string_fill(&seed1, drbg->V, drbg_statelen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +0200739 list_add_tail(&seed1.list, &seedlist);
Stephan Mueller541af942014-05-31 15:44:17 +0200740 /* buffer of seed2 will be filled in for loop below with one byte */
741 drbg_string_fill(&seed2, NULL, 1);
Stephan Mueller8c987162014-06-28 21:58:24 +0200742 list_add_tail(&seed2.list, &seedlist);
Stephan Mueller541af942014-05-31 15:44:17 +0200743 /* input data of seed is allowed to be NULL at this point */
Stephan Mueller8c987162014-06-28 21:58:24 +0200744 if (seed)
745 list_splice_tail(seed, &seedlist);
Stephan Mueller541af942014-05-31 15:44:17 +0200746
Stephan Mueller8c987162014-06-28 21:58:24 +0200747 drbg_string_fill(&vdata, drbg->V, drbg_statelen(drbg));
748 list_add_tail(&vdata.list, &vdatalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200749 for (i = 2; 0 < i; i--) {
750 /* first round uses 0x0, second 0x1 */
751 unsigned char prefix = DRBG_PREFIX0;
752 if (1 == i)
753 prefix = DRBG_PREFIX1;
754 /* 10.1.2.2 step 1 and 4 -- concatenation and HMAC for key */
755 seed2.buf = &prefix;
Stephan Mueller8c987162014-06-28 21:58:24 +0200756 ret = drbg_kcapi_hash(drbg, drbg->C, drbg->C, &seedlist);
Stephan Mueller541af942014-05-31 15:44:17 +0200757 if (ret)
758 return ret;
759
760 /* 10.1.2.2 step 2 and 5 -- HMAC for V */
Stephan Mueller8c987162014-06-28 21:58:24 +0200761 ret = drbg_kcapi_hash(drbg, drbg->C, drbg->V, &vdatalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200762 if (ret)
763 return ret;
764
765 /* 10.1.2.2 step 3 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200766 if (!seed)
Stephan Mueller541af942014-05-31 15:44:17 +0200767 return ret;
768 }
769
770 return 0;
771}
772
773/* generate function of HMAC DRBG as defined in 10.1.2.5 */
774static int drbg_hmac_generate(struct drbg_state *drbg,
775 unsigned char *buf,
776 unsigned int buflen,
777 struct drbg_string *addtl)
778{
779 int len = 0;
780 int ret = 0;
781 struct drbg_string data;
Stephan Mueller8c987162014-06-28 21:58:24 +0200782 LIST_HEAD(datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200783
784 /* 10.1.2.5 step 2 */
785 if (addtl && 0 < addtl->len) {
Stephan Mueller8c987162014-06-28 21:58:24 +0200786 LIST_HEAD(addtllist);
787
788 list_add_tail(&addtl->list, &addtllist);
789 ret = drbg_hmac_update(drbg, &addtllist, 1);
Stephan Mueller541af942014-05-31 15:44:17 +0200790 if (ret)
791 return ret;
792 }
793
794 drbg_string_fill(&data, drbg->V, drbg_statelen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +0200795 list_add_tail(&data.list, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200796 while (len < buflen) {
797 unsigned int outlen = 0;
798 /* 10.1.2.5 step 4.1 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200799 ret = drbg_kcapi_hash(drbg, drbg->C, drbg->V, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200800 if (ret)
801 return ret;
802 outlen = (drbg_blocklen(drbg) < (buflen - len)) ?
803 drbg_blocklen(drbg) : (buflen - len);
804 if (!drbg_fips_continuous_test(drbg, drbg->V))
805 continue;
806
807 /* 10.1.2.5 step 4.2 */
808 memcpy(buf + len, drbg->V, outlen);
809 len += outlen;
810 }
811
812 /* 10.1.2.5 step 6 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200813 if (addtl && 0 < addtl->len) {
814 LIST_HEAD(addtllist);
815
816 list_add_tail(&addtl->list, &addtllist);
817 ret = drbg_hmac_update(drbg, &addtllist, 1);
818 } else {
819 ret = drbg_hmac_update(drbg, NULL, 1);
820 }
Stephan Mueller541af942014-05-31 15:44:17 +0200821 if (ret)
822 return ret;
823
824 return len;
825}
826
827static struct drbg_state_ops drbg_hmac_ops = {
828 .update = drbg_hmac_update,
829 .generate = drbg_hmac_generate,
830 .crypto_init = drbg_init_hash_kernel,
831 .crypto_fini = drbg_fini_hash_kernel,
832
833};
834#endif /* CONFIG_CRYPTO_DRBG_HMAC */
835
836/******************************************************************
837 * Hash DRBG callback functions
838 ******************************************************************/
839
840#ifdef CONFIG_CRYPTO_DRBG_HASH
Stephan Muellere25e47e2014-07-06 02:23:03 +0200841#define CRYPTO_DRBG_HASH_STRING "HASH "
Stephan Mueller541af942014-05-31 15:44:17 +0200842/*
843 * scratchpad usage: as drbg_hash_update and drbg_hash_df are used
844 * interlinked, the scratchpad is used as follows:
845 * drbg_hash_update
846 * start: drbg->scratchpad
847 * length: drbg_statelen(drbg)
848 * drbg_hash_df:
849 * start: drbg->scratchpad + drbg_statelen(drbg)
850 * length: drbg_blocklen(drbg)
851 *
852 * drbg_hash_process_addtl uses the scratchpad, but fully completes
853 * before either of the functions mentioned before are invoked. Therefore,
854 * drbg_hash_process_addtl does not need to be specifically considered.
855 */
856
857/* Derivation Function for Hash DRBG as defined in 10.4.1 */
858static int drbg_hash_df(struct drbg_state *drbg,
859 unsigned char *outval, size_t outlen,
Stephan Mueller8c987162014-06-28 21:58:24 +0200860 struct list_head *entropylist)
Stephan Mueller541af942014-05-31 15:44:17 +0200861{
862 int ret = 0;
863 size_t len = 0;
864 unsigned char input[5];
865 unsigned char *tmp = drbg->scratchpad + drbg_statelen(drbg);
Stephan Mueller8c987162014-06-28 21:58:24 +0200866 struct drbg_string data;
Stephan Mueller541af942014-05-31 15:44:17 +0200867
868 memset(tmp, 0, drbg_blocklen(drbg));
869
870 /* 10.4.1 step 3 */
871 input[0] = 1;
872 drbg_int2byte(&input[1], (outlen * 8), 4);
873
874 /* 10.4.1 step 4.1 -- concatenation of data for input into hash */
Stephan Mueller8c987162014-06-28 21:58:24 +0200875 drbg_string_fill(&data, input, 5);
876 list_add(&data.list, entropylist);
Stephan Mueller541af942014-05-31 15:44:17 +0200877
878 /* 10.4.1 step 4 */
879 while (len < outlen) {
880 short blocklen = 0;
881 /* 10.4.1 step 4.1 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200882 ret = drbg_kcapi_hash(drbg, NULL, tmp, entropylist);
Stephan Mueller541af942014-05-31 15:44:17 +0200883 if (ret)
884 goto out;
885 /* 10.4.1 step 4.2 */
886 input[0]++;
887 blocklen = (drbg_blocklen(drbg) < (outlen - len)) ?
888 drbg_blocklen(drbg) : (outlen - len);
889 memcpy(outval + len, tmp, blocklen);
890 len += blocklen;
891 }
892
893out:
894 memset(tmp, 0, drbg_blocklen(drbg));
895 return ret;
896}
897
898/* update function for Hash DRBG as defined in 10.1.1.2 / 10.1.1.3 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200899static int drbg_hash_update(struct drbg_state *drbg, struct list_head *seed,
Stephan Mueller541af942014-05-31 15:44:17 +0200900 int reseed)
901{
902 int ret = 0;
903 struct drbg_string data1, data2;
Stephan Mueller8c987162014-06-28 21:58:24 +0200904 LIST_HEAD(datalist);
905 LIST_HEAD(datalist2);
Stephan Mueller541af942014-05-31 15:44:17 +0200906 unsigned char *V = drbg->scratchpad;
907 unsigned char prefix = DRBG_PREFIX1;
908
909 memset(drbg->scratchpad, 0, drbg_statelen(drbg));
910 if (!seed)
911 return -EINVAL;
912
913 if (reseed) {
914 /* 10.1.1.3 step 1 */
915 memcpy(V, drbg->V, drbg_statelen(drbg));
916 drbg_string_fill(&data1, &prefix, 1);
Stephan Mueller8c987162014-06-28 21:58:24 +0200917 list_add_tail(&data1.list, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200918 drbg_string_fill(&data2, V, drbg_statelen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +0200919 list_add_tail(&data2.list, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200920 }
Stephan Mueller8c987162014-06-28 21:58:24 +0200921 list_splice_tail(seed, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200922
923 /* 10.1.1.2 / 10.1.1.3 step 2 and 3 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200924 ret = drbg_hash_df(drbg, drbg->V, drbg_statelen(drbg), &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200925 if (ret)
926 goto out;
927
928 /* 10.1.1.2 / 10.1.1.3 step 4 */
929 prefix = DRBG_PREFIX0;
930 drbg_string_fill(&data1, &prefix, 1);
Stephan Mueller8c987162014-06-28 21:58:24 +0200931 list_add_tail(&data1.list, &datalist2);
Stephan Mueller541af942014-05-31 15:44:17 +0200932 drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +0200933 list_add_tail(&data2.list, &datalist2);
Stephan Mueller541af942014-05-31 15:44:17 +0200934 /* 10.1.1.2 / 10.1.1.3 step 4 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200935 ret = drbg_hash_df(drbg, drbg->C, drbg_statelen(drbg), &datalist2);
Stephan Mueller541af942014-05-31 15:44:17 +0200936
937out:
938 memset(drbg->scratchpad, 0, drbg_statelen(drbg));
939 return ret;
940}
941
942/* processing of additional information string for Hash DRBG */
943static int drbg_hash_process_addtl(struct drbg_state *drbg,
944 struct drbg_string *addtl)
945{
946 int ret = 0;
947 struct drbg_string data1, data2;
Stephan Mueller8c987162014-06-28 21:58:24 +0200948 LIST_HEAD(datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200949 unsigned char prefix = DRBG_PREFIX2;
950
951 /* this is value w as per documentation */
952 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
953
954 /* 10.1.1.4 step 2 */
955 if (!addtl || 0 == addtl->len)
956 return 0;
957
958 /* 10.1.1.4 step 2a */
959 drbg_string_fill(&data1, &prefix, 1);
960 drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +0200961 list_add_tail(&data1.list, &datalist);
962 list_add_tail(&data2.list, &datalist);
963 list_add_tail(&addtl->list, &datalist);
964 ret = drbg_kcapi_hash(drbg, NULL, drbg->scratchpad, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200965 if (ret)
966 goto out;
967
968 /* 10.1.1.4 step 2b */
969 drbg_add_buf(drbg->V, drbg_statelen(drbg),
970 drbg->scratchpad, drbg_blocklen(drbg));
971
972out:
973 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
974 return ret;
975}
976
977/* Hashgen defined in 10.1.1.4 */
978static int drbg_hash_hashgen(struct drbg_state *drbg,
979 unsigned char *buf,
980 unsigned int buflen)
981{
982 int len = 0;
983 int ret = 0;
984 unsigned char *src = drbg->scratchpad;
985 unsigned char *dst = drbg->scratchpad + drbg_statelen(drbg);
986 struct drbg_string data;
Stephan Mueller8c987162014-06-28 21:58:24 +0200987 LIST_HEAD(datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200988 unsigned char prefix = DRBG_PREFIX1;
989
990 memset(src, 0, drbg_statelen(drbg));
991 memset(dst, 0, drbg_blocklen(drbg));
992
993 /* 10.1.1.4 step hashgen 2 */
994 memcpy(src, drbg->V, drbg_statelen(drbg));
995
996 drbg_string_fill(&data, src, drbg_statelen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +0200997 list_add_tail(&data.list, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200998 while (len < buflen) {
999 unsigned int outlen = 0;
1000 /* 10.1.1.4 step hashgen 4.1 */
Stephan Mueller8c987162014-06-28 21:58:24 +02001001 ret = drbg_kcapi_hash(drbg, NULL, dst, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +02001002 if (ret) {
1003 len = ret;
1004 goto out;
1005 }
1006 outlen = (drbg_blocklen(drbg) < (buflen - len)) ?
1007 drbg_blocklen(drbg) : (buflen - len);
1008 if (!drbg_fips_continuous_test(drbg, dst)) {
1009 drbg_add_buf(src, drbg_statelen(drbg), &prefix, 1);
1010 continue;
1011 }
1012 /* 10.1.1.4 step hashgen 4.2 */
1013 memcpy(buf + len, dst, outlen);
1014 len += outlen;
1015 /* 10.1.1.4 hashgen step 4.3 */
1016 if (len < buflen)
1017 drbg_add_buf(src, drbg_statelen(drbg), &prefix, 1);
1018 }
1019
1020out:
1021 memset(drbg->scratchpad, 0,
1022 (drbg_statelen(drbg) + drbg_blocklen(drbg)));
1023 return len;
1024}
1025
1026/* generate function for Hash DRBG as defined in 10.1.1.4 */
1027static int drbg_hash_generate(struct drbg_state *drbg,
1028 unsigned char *buf, unsigned int buflen,
1029 struct drbg_string *addtl)
1030{
1031 int len = 0;
1032 int ret = 0;
1033 unsigned char req[8];
1034 unsigned char prefix = DRBG_PREFIX3;
1035 struct drbg_string data1, data2;
Stephan Mueller8c987162014-06-28 21:58:24 +02001036 LIST_HEAD(datalist);
Stephan Mueller541af942014-05-31 15:44:17 +02001037
1038 /* 10.1.1.4 step 2 */
1039 ret = drbg_hash_process_addtl(drbg, addtl);
1040 if (ret)
1041 return ret;
1042 /* 10.1.1.4 step 3 */
1043 len = drbg_hash_hashgen(drbg, buf, buflen);
1044
1045 /* this is the value H as documented in 10.1.1.4 */
1046 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
1047 /* 10.1.1.4 step 4 */
1048 drbg_string_fill(&data1, &prefix, 1);
Stephan Mueller8c987162014-06-28 21:58:24 +02001049 list_add_tail(&data1.list, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +02001050 drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +02001051 list_add_tail(&data2.list, &datalist);
1052 ret = drbg_kcapi_hash(drbg, NULL, drbg->scratchpad, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +02001053 if (ret) {
1054 len = ret;
1055 goto out;
1056 }
1057
1058 /* 10.1.1.4 step 5 */
1059 drbg_add_buf(drbg->V, drbg_statelen(drbg),
1060 drbg->scratchpad, drbg_blocklen(drbg));
1061 drbg_add_buf(drbg->V, drbg_statelen(drbg),
1062 drbg->C, drbg_statelen(drbg));
1063 drbg_int2byte(req, drbg->reseed_ctr, sizeof(req));
1064 drbg_add_buf(drbg->V, drbg_statelen(drbg), req, 8);
1065
1066out:
1067 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
1068 return len;
1069}
1070
1071/*
1072 * scratchpad usage: as update and generate are used isolated, both
1073 * can use the scratchpad
1074 */
1075static struct drbg_state_ops drbg_hash_ops = {
1076 .update = drbg_hash_update,
1077 .generate = drbg_hash_generate,
1078 .crypto_init = drbg_init_hash_kernel,
1079 .crypto_fini = drbg_fini_hash_kernel,
1080};
1081#endif /* CONFIG_CRYPTO_DRBG_HASH */
1082
1083/******************************************************************
1084 * Functions common for DRBG implementations
1085 ******************************************************************/
1086
1087/*
1088 * Seeding or reseeding of the DRBG
1089 *
1090 * @drbg: DRBG state struct
1091 * @pers: personalization / additional information buffer
1092 * @reseed: 0 for initial seed process, 1 for reseeding
1093 *
1094 * return:
1095 * 0 on success
1096 * error value otherwise
1097 */
1098static int drbg_seed(struct drbg_state *drbg, struct drbg_string *pers,
1099 bool reseed)
1100{
1101 int ret = 0;
1102 unsigned char *entropy = NULL;
1103 size_t entropylen = 0;
1104 struct drbg_string data1;
Stephan Mueller8c987162014-06-28 21:58:24 +02001105 LIST_HEAD(seedlist);
Stephan Mueller541af942014-05-31 15:44:17 +02001106
1107 /* 9.1 / 9.2 / 9.3.1 step 3 */
1108 if (pers && pers->len > (drbg_max_addtl(drbg))) {
1109 pr_devel("DRBG: personalization string too long %lu\n",
1110 pers->len);
1111 return -EINVAL;
1112 }
1113
1114 if (drbg->test_data && drbg->test_data->testentropy) {
1115 drbg_string_fill(&data1, drbg->test_data->testentropy->buf,
1116 drbg->test_data->testentropy->len);
1117 pr_devel("DRBG: using test entropy\n");
1118 } else {
1119 /*
1120 * Gather entropy equal to the security strength of the DRBG.
1121 * With a derivation function, a nonce is required in addition
1122 * to the entropy. A nonce must be at least 1/2 of the security
1123 * strength of the DRBG in size. Thus, entropy * nonce is 3/2
1124 * of the strength. The consideration of a nonce is only
1125 * applicable during initial seeding.
1126 */
1127 entropylen = drbg_sec_strength(drbg->core->flags);
1128 if (!entropylen)
1129 return -EFAULT;
1130 if (!reseed)
1131 entropylen = ((entropylen + 1) / 2) * 3;
1132 pr_devel("DRBG: (re)seeding with %zu bytes of entropy\n",
1133 entropylen);
1134 entropy = kzalloc(entropylen, GFP_KERNEL);
1135 if (!entropy)
1136 return -ENOMEM;
1137 get_random_bytes(entropy, entropylen);
1138 drbg_string_fill(&data1, entropy, entropylen);
1139 }
Stephan Mueller8c987162014-06-28 21:58:24 +02001140 list_add_tail(&data1.list, &seedlist);
Stephan Mueller541af942014-05-31 15:44:17 +02001141
1142 /*
1143 * concatenation of entropy with personalization str / addtl input)
1144 * the variable pers is directly handed in by the caller, so check its
1145 * contents whether it is appropriate
1146 */
Stephan Mueller8c987162014-06-28 21:58:24 +02001147 if (pers && pers->buf && 0 < pers->len) {
1148 list_add_tail(&pers->list, &seedlist);
Stephan Mueller541af942014-05-31 15:44:17 +02001149 pr_devel("DRBG: using personalization string\n");
1150 }
1151
Stephan Mueller8c987162014-06-28 21:58:24 +02001152 ret = drbg->d_ops->update(drbg, &seedlist, reseed);
Stephan Mueller541af942014-05-31 15:44:17 +02001153 if (ret)
1154 goto out;
1155
1156 drbg->seeded = true;
1157 /* 10.1.1.2 / 10.1.1.3 step 5 */
1158 drbg->reseed_ctr = 1;
1159
1160out:
1161 if (entropy)
1162 kzfree(entropy);
1163 return ret;
1164}
1165
1166/* Free all substructures in a DRBG state without the DRBG state structure */
1167static inline void drbg_dealloc_state(struct drbg_state *drbg)
1168{
1169 if (!drbg)
1170 return;
1171 if (drbg->V)
1172 kzfree(drbg->V);
1173 drbg->V = NULL;
1174 if (drbg->C)
1175 kzfree(drbg->C);
1176 drbg->C = NULL;
1177 if (drbg->scratchpad)
1178 kzfree(drbg->scratchpad);
1179 drbg->scratchpad = NULL;
1180 drbg->reseed_ctr = 0;
1181#ifdef CONFIG_CRYPTO_FIPS
1182 if (drbg->prev)
1183 kzfree(drbg->prev);
1184 drbg->prev = NULL;
1185 drbg->fips_primed = false;
1186#endif
1187}
1188
1189/*
1190 * Allocate all sub-structures for a DRBG state.
1191 * The DRBG state structure must already be allocated.
1192 */
1193static inline int drbg_alloc_state(struct drbg_state *drbg)
1194{
1195 int ret = -ENOMEM;
1196 unsigned int sb_size = 0;
1197
1198 if (!drbg)
1199 return -EINVAL;
1200
1201 drbg->V = kzalloc(drbg_statelen(drbg), GFP_KERNEL);
1202 if (!drbg->V)
1203 goto err;
1204 drbg->C = kzalloc(drbg_statelen(drbg), GFP_KERNEL);
1205 if (!drbg->C)
1206 goto err;
1207#ifdef CONFIG_CRYPTO_FIPS
1208 drbg->prev = kzalloc(drbg_blocklen(drbg), GFP_KERNEL);
1209 if (!drbg->prev)
1210 goto err;
1211 drbg->fips_primed = false;
1212#endif
1213 /* scratchpad is only generated for CTR and Hash */
1214 if (drbg->core->flags & DRBG_HMAC)
1215 sb_size = 0;
1216 else if (drbg->core->flags & DRBG_CTR)
1217 sb_size = drbg_statelen(drbg) + drbg_blocklen(drbg) + /* temp */
1218 drbg_statelen(drbg) + /* df_data */
1219 drbg_blocklen(drbg) + /* pad */
1220 drbg_blocklen(drbg) + /* iv */
Stephan Mueller8fecaad2014-07-01 17:08:48 +02001221 drbg_statelen(drbg) + drbg_blocklen(drbg); /* temp */
Stephan Mueller541af942014-05-31 15:44:17 +02001222 else
1223 sb_size = drbg_statelen(drbg) + drbg_blocklen(drbg);
1224
1225 if (0 < sb_size) {
1226 drbg->scratchpad = kzalloc(sb_size, GFP_KERNEL);
1227 if (!drbg->scratchpad)
1228 goto err;
1229 }
1230 spin_lock_init(&drbg->drbg_lock);
1231 return 0;
1232
1233err:
1234 drbg_dealloc_state(drbg);
1235 return ret;
1236}
1237
1238/*
1239 * Strategy to avoid holding long term locks: generate a shadow copy of DRBG
1240 * and perform all operations on this shadow copy. After finishing, restore
1241 * the updated state of the shadow copy into original drbg state. This way,
1242 * only the read and write operations of the original drbg state must be
1243 * locked
1244 */
1245static inline void drbg_copy_drbg(struct drbg_state *src,
1246 struct drbg_state *dst)
1247{
1248 if (!src || !dst)
1249 return;
1250 memcpy(dst->V, src->V, drbg_statelen(src));
1251 memcpy(dst->C, src->C, drbg_statelen(src));
1252 dst->reseed_ctr = src->reseed_ctr;
1253 dst->seeded = src->seeded;
1254 dst->pr = src->pr;
1255#ifdef CONFIG_CRYPTO_FIPS
1256 dst->fips_primed = src->fips_primed;
1257 memcpy(dst->prev, src->prev, drbg_blocklen(src));
1258#endif
1259 /*
1260 * Not copied:
1261 * scratchpad is initialized drbg_alloc_state;
1262 * priv_data is initialized with call to crypto_init;
1263 * d_ops and core are set outside, as these parameters are const;
1264 * test_data is set outside to prevent it being copied back.
1265 */
1266}
1267
1268static int drbg_make_shadow(struct drbg_state *drbg, struct drbg_state **shadow)
1269{
1270 int ret = -ENOMEM;
1271 struct drbg_state *tmp = NULL;
1272
1273 if (!drbg || !drbg->core || !drbg->V || !drbg->C) {
1274 pr_devel("DRBG: attempt to generate shadow copy for "
1275 "uninitialized DRBG state rejected\n");
1276 return -EINVAL;
1277 }
1278 /* HMAC does not have a scratchpad */
1279 if (!(drbg->core->flags & DRBG_HMAC) && NULL == drbg->scratchpad)
1280 return -EINVAL;
1281
1282 tmp = kzalloc(sizeof(struct drbg_state), GFP_KERNEL);
1283 if (!tmp)
1284 return -ENOMEM;
1285
1286 /* read-only data as they are defined as const, no lock needed */
1287 tmp->core = drbg->core;
1288 tmp->d_ops = drbg->d_ops;
1289
1290 ret = drbg_alloc_state(tmp);
1291 if (ret)
1292 goto err;
1293
1294 spin_lock_bh(&drbg->drbg_lock);
1295 drbg_copy_drbg(drbg, tmp);
1296 /* only make a link to the test buffer, as we only read that data */
1297 tmp->test_data = drbg->test_data;
1298 spin_unlock_bh(&drbg->drbg_lock);
1299 *shadow = tmp;
1300 return 0;
1301
1302err:
1303 if (tmp)
1304 kzfree(tmp);
1305 return ret;
1306}
1307
1308static void drbg_restore_shadow(struct drbg_state *drbg,
1309 struct drbg_state **shadow)
1310{
1311 struct drbg_state *tmp = *shadow;
1312
1313 spin_lock_bh(&drbg->drbg_lock);
1314 drbg_copy_drbg(tmp, drbg);
1315 spin_unlock_bh(&drbg->drbg_lock);
1316 drbg_dealloc_state(tmp);
1317 kzfree(tmp);
1318 *shadow = NULL;
1319}
1320
1321/*************************************************************************
1322 * DRBG interface functions
1323 *************************************************************************/
1324
1325/*
1326 * DRBG generate function as required by SP800-90A - this function
1327 * generates random numbers
1328 *
1329 * @drbg DRBG state handle
1330 * @buf Buffer where to store the random numbers -- the buffer must already
1331 * be pre-allocated by caller
1332 * @buflen Length of output buffer - this value defines the number of random
1333 * bytes pulled from DRBG
1334 * @addtl Additional input that is mixed into state, may be NULL -- note
1335 * the entropy is pulled by the DRBG internally unconditionally
1336 * as defined in SP800-90A. The additional input is mixed into
1337 * the state in addition to the pulled entropy.
1338 *
1339 * return: generated number of bytes
1340 */
1341static int drbg_generate(struct drbg_state *drbg,
1342 unsigned char *buf, unsigned int buflen,
1343 struct drbg_string *addtl)
1344{
1345 int len = 0;
1346 struct drbg_state *shadow = NULL;
1347
1348 if (0 == buflen || !buf) {
1349 pr_devel("DRBG: no output buffer provided\n");
1350 return -EINVAL;
1351 }
1352 if (addtl && NULL == addtl->buf && 0 < addtl->len) {
1353 pr_devel("DRBG: wrong format of additional information\n");
1354 return -EINVAL;
1355 }
1356
1357 len = drbg_make_shadow(drbg, &shadow);
1358 if (len) {
1359 pr_devel("DRBG: shadow copy cannot be generated\n");
1360 return len;
1361 }
1362
1363 /* 9.3.1 step 2 */
1364 len = -EINVAL;
1365 if (buflen > (drbg_max_request_bytes(shadow))) {
1366 pr_devel("DRBG: requested random numbers too large %u\n",
1367 buflen);
1368 goto err;
1369 }
1370
1371 /* 9.3.1 step 3 is implicit with the chosen DRBG */
1372
1373 /* 9.3.1 step 4 */
1374 if (addtl && addtl->len > (drbg_max_addtl(shadow))) {
1375 pr_devel("DRBG: additional information string too long %zu\n",
1376 addtl->len);
1377 goto err;
1378 }
1379 /* 9.3.1 step 5 is implicit with the chosen DRBG */
1380
1381 /*
1382 * 9.3.1 step 6 and 9 supplemented by 9.3.2 step c is implemented
1383 * here. The spec is a bit convoluted here, we make it simpler.
1384 */
1385 if ((drbg_max_requests(shadow)) < shadow->reseed_ctr)
1386 shadow->seeded = false;
1387
1388 /* allocate cipher handle */
1389 if (shadow->d_ops->crypto_init) {
1390 len = shadow->d_ops->crypto_init(shadow);
1391 if (len)
1392 goto err;
1393 }
1394
1395 if (shadow->pr || !shadow->seeded) {
1396 pr_devel("DRBG: reseeding before generation (prediction "
1397 "resistance: %s, state %s)\n",
1398 drbg->pr ? "true" : "false",
1399 drbg->seeded ? "seeded" : "unseeded");
1400 /* 9.3.1 steps 7.1 through 7.3 */
1401 len = drbg_seed(shadow, addtl, true);
1402 if (len)
1403 goto err;
1404 /* 9.3.1 step 7.4 */
1405 addtl = NULL;
1406 }
1407 /* 9.3.1 step 8 and 10 */
1408 len = shadow->d_ops->generate(shadow, buf, buflen, addtl);
1409
1410 /* 10.1.1.4 step 6, 10.1.2.5 step 7, 10.2.1.5.2 step 7 */
1411 shadow->reseed_ctr++;
1412 if (0 >= len)
1413 goto err;
1414
1415 /*
1416 * Section 11.3.3 requires to re-perform self tests after some
1417 * generated random numbers. The chosen value after which self
1418 * test is performed is arbitrary, but it should be reasonable.
1419 * However, we do not perform the self tests because of the following
1420 * reasons: it is mathematically impossible that the initial self tests
1421 * were successfully and the following are not. If the initial would
1422 * pass and the following would not, the kernel integrity is violated.
1423 * In this case, the entire kernel operation is questionable and it
1424 * is unlikely that the integrity violation only affects the
1425 * correct operation of the DRBG.
1426 *
1427 * Albeit the following code is commented out, it is provided in
1428 * case somebody has a need to implement the test of 11.3.3.
1429 */
1430#if 0
1431 if (shadow->reseed_ctr && !(shadow->reseed_ctr % 4096)) {
1432 int err = 0;
1433 pr_devel("DRBG: start to perform self test\n");
1434 if (drbg->core->flags & DRBG_HMAC)
1435 err = alg_test("drbg_pr_hmac_sha256",
1436 "drbg_pr_hmac_sha256", 0, 0);
1437 else if (drbg->core->flags & DRBG_CTR)
1438 err = alg_test("drbg_pr_ctr_aes128",
1439 "drbg_pr_ctr_aes128", 0, 0);
1440 else
1441 err = alg_test("drbg_pr_sha256",
1442 "drbg_pr_sha256", 0, 0);
1443 if (err) {
1444 pr_err("DRBG: periodical self test failed\n");
1445 /*
1446 * uninstantiate implies that from now on, only errors
1447 * are returned when reusing this DRBG cipher handle
1448 */
1449 drbg_uninstantiate(drbg);
1450 drbg_dealloc_state(shadow);
1451 kzfree(shadow);
1452 return 0;
1453 } else {
1454 pr_devel("DRBG: self test successful\n");
1455 }
1456 }
1457#endif
1458
1459err:
1460 if (shadow->d_ops->crypto_fini)
1461 shadow->d_ops->crypto_fini(shadow);
1462 drbg_restore_shadow(drbg, &shadow);
1463 return len;
1464}
1465
1466/*
1467 * Wrapper around drbg_generate which can pull arbitrary long strings
1468 * from the DRBG without hitting the maximum request limitation.
1469 *
1470 * Parameters: see drbg_generate
1471 * Return codes: see drbg_generate -- if one drbg_generate request fails,
1472 * the entire drbg_generate_long request fails
1473 */
1474static int drbg_generate_long(struct drbg_state *drbg,
1475 unsigned char *buf, unsigned int buflen,
1476 struct drbg_string *addtl)
1477{
1478 int len = 0;
1479 unsigned int slice = 0;
1480 do {
1481 int tmplen = 0;
1482 unsigned int chunk = 0;
1483 slice = ((buflen - len) / drbg_max_request_bytes(drbg));
1484 chunk = slice ? drbg_max_request_bytes(drbg) : (buflen - len);
1485 tmplen = drbg_generate(drbg, buf + len, chunk, addtl);
1486 if (0 >= tmplen)
1487 return tmplen;
1488 len += tmplen;
1489 } while (slice > 0);
1490 return len;
1491}
1492
1493/*
1494 * DRBG instantiation function as required by SP800-90A - this function
1495 * sets up the DRBG handle, performs the initial seeding and all sanity
1496 * checks required by SP800-90A
1497 *
1498 * @drbg memory of state -- if NULL, new memory is allocated
1499 * @pers Personalization string that is mixed into state, may be NULL -- note
1500 * the entropy is pulled by the DRBG internally unconditionally
1501 * as defined in SP800-90A. The additional input is mixed into
1502 * the state in addition to the pulled entropy.
1503 * @coreref reference to core
1504 * @pr prediction resistance enabled
1505 *
1506 * return
1507 * 0 on success
1508 * error value otherwise
1509 */
1510static int drbg_instantiate(struct drbg_state *drbg, struct drbg_string *pers,
1511 int coreref, bool pr)
1512{
1513 int ret = -ENOMEM;
1514
1515 pr_devel("DRBG: Initializing DRBG core %d with prediction resistance "
1516 "%s\n", coreref, pr ? "enabled" : "disabled");
1517 drbg->core = &drbg_cores[coreref];
1518 drbg->pr = pr;
1519 drbg->seeded = false;
1520 switch (drbg->core->flags & DRBG_TYPE_MASK) {
1521#ifdef CONFIG_CRYPTO_DRBG_HMAC
1522 case DRBG_HMAC:
1523 drbg->d_ops = &drbg_hmac_ops;
1524 break;
1525#endif /* CONFIG_CRYPTO_DRBG_HMAC */
1526#ifdef CONFIG_CRYPTO_DRBG_HASH
1527 case DRBG_HASH:
1528 drbg->d_ops = &drbg_hash_ops;
1529 break;
1530#endif /* CONFIG_CRYPTO_DRBG_HASH */
1531#ifdef CONFIG_CRYPTO_DRBG_CTR
1532 case DRBG_CTR:
1533 drbg->d_ops = &drbg_ctr_ops;
1534 break;
1535#endif /* CONFIG_CRYPTO_DRBG_CTR */
1536 default:
1537 return -EOPNOTSUPP;
1538 }
1539
1540 /* 9.1 step 1 is implicit with the selected DRBG type */
1541
1542 /*
1543 * 9.1 step 2 is implicit as caller can select prediction resistance
1544 * and the flag is copied into drbg->flags --
1545 * all DRBG types support prediction resistance
1546 */
1547
1548 /* 9.1 step 4 is implicit in drbg_sec_strength */
1549
1550 ret = drbg_alloc_state(drbg);
1551 if (ret)
1552 return ret;
1553
1554 ret = -EFAULT;
1555 if (drbg->d_ops->crypto_init && drbg->d_ops->crypto_init(drbg))
1556 goto err;
1557 ret = drbg_seed(drbg, pers, false);
1558 if (drbg->d_ops->crypto_fini)
1559 drbg->d_ops->crypto_fini(drbg);
1560 if (ret)
1561 goto err;
1562
1563 return 0;
1564
1565err:
1566 drbg_dealloc_state(drbg);
1567 return ret;
1568}
1569
1570/*
1571 * DRBG uninstantiate function as required by SP800-90A - this function
1572 * frees all buffers and the DRBG handle
1573 *
1574 * @drbg DRBG state handle
1575 *
1576 * return
1577 * 0 on success
1578 */
1579static int drbg_uninstantiate(struct drbg_state *drbg)
1580{
1581 spin_lock_bh(&drbg->drbg_lock);
1582 drbg_dealloc_state(drbg);
1583 /* no scrubbing of test_data -- this shall survive an uninstantiate */
1584 spin_unlock_bh(&drbg->drbg_lock);
1585 return 0;
1586}
1587
1588/*
1589 * Helper function for setting the test data in the DRBG
1590 *
1591 * @drbg DRBG state handle
1592 * @test_data test data to sets
1593 */
1594static inline void drbg_set_testdata(struct drbg_state *drbg,
1595 struct drbg_test_data *test_data)
1596{
1597 if (!test_data || !test_data->testentropy)
1598 return;
1599 spin_lock_bh(&drbg->drbg_lock);
1600 drbg->test_data = test_data;
1601 spin_unlock_bh(&drbg->drbg_lock);
1602}
1603
1604/***************************************************************
1605 * Kernel crypto API cipher invocations requested by DRBG
1606 ***************************************************************/
1607
1608#if defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_HMAC)
1609struct sdesc {
1610 struct shash_desc shash;
1611 char ctx[];
1612};
1613
1614static int drbg_init_hash_kernel(struct drbg_state *drbg)
1615{
1616 struct sdesc *sdesc;
1617 struct crypto_shash *tfm;
1618
1619 tfm = crypto_alloc_shash(drbg->core->backend_cra_name, 0, 0);
1620 if (IS_ERR(tfm)) {
1621 pr_info("DRBG: could not allocate digest TFM handle\n");
1622 return PTR_ERR(tfm);
1623 }
1624 BUG_ON(drbg_blocklen(drbg) != crypto_shash_digestsize(tfm));
1625 sdesc = kzalloc(sizeof(struct shash_desc) + crypto_shash_descsize(tfm),
1626 GFP_KERNEL);
1627 if (!sdesc) {
1628 crypto_free_shash(tfm);
1629 return -ENOMEM;
1630 }
1631
1632 sdesc->shash.tfm = tfm;
1633 sdesc->shash.flags = 0;
1634 drbg->priv_data = sdesc;
1635 return 0;
1636}
1637
1638static int drbg_fini_hash_kernel(struct drbg_state *drbg)
1639{
1640 struct sdesc *sdesc = (struct sdesc *)drbg->priv_data;
1641 if (sdesc) {
1642 crypto_free_shash(sdesc->shash.tfm);
1643 kzfree(sdesc);
1644 }
1645 drbg->priv_data = NULL;
1646 return 0;
1647}
1648
1649static int drbg_kcapi_hash(struct drbg_state *drbg, const unsigned char *key,
Stephan Mueller8c987162014-06-28 21:58:24 +02001650 unsigned char *outval, const struct list_head *in)
Stephan Mueller541af942014-05-31 15:44:17 +02001651{
1652 struct sdesc *sdesc = (struct sdesc *)drbg->priv_data;
Stephan Mueller8c987162014-06-28 21:58:24 +02001653 struct drbg_string *input = NULL;
Stephan Mueller541af942014-05-31 15:44:17 +02001654
1655 if (key)
1656 crypto_shash_setkey(sdesc->shash.tfm, key, drbg_statelen(drbg));
1657 crypto_shash_init(&sdesc->shash);
Stephan Mueller8c987162014-06-28 21:58:24 +02001658 list_for_each_entry(input, in, list)
1659 crypto_shash_update(&sdesc->shash, input->buf, input->len);
Stephan Mueller541af942014-05-31 15:44:17 +02001660 return crypto_shash_final(&sdesc->shash, outval);
1661}
1662#endif /* (CONFIG_CRYPTO_DRBG_HASH || CONFIG_CRYPTO_DRBG_HMAC) */
1663
1664#ifdef CONFIG_CRYPTO_DRBG_CTR
1665static int drbg_init_sym_kernel(struct drbg_state *drbg)
1666{
1667 int ret = 0;
1668 struct crypto_blkcipher *tfm;
1669
1670 tfm = crypto_alloc_blkcipher(drbg->core->backend_cra_name, 0, 0);
1671 if (IS_ERR(tfm)) {
1672 pr_info("DRBG: could not allocate cipher TFM handle\n");
1673 return PTR_ERR(tfm);
1674 }
1675 BUG_ON(drbg_blocklen(drbg) != crypto_blkcipher_blocksize(tfm));
1676 drbg->priv_data = tfm;
1677 return ret;
1678}
1679
1680static int drbg_fini_sym_kernel(struct drbg_state *drbg)
1681{
1682 struct crypto_blkcipher *tfm =
1683 (struct crypto_blkcipher *)drbg->priv_data;
1684 if (tfm)
1685 crypto_free_blkcipher(tfm);
1686 drbg->priv_data = NULL;
1687 return 0;
1688}
1689
1690static int drbg_kcapi_sym(struct drbg_state *drbg, const unsigned char *key,
1691 unsigned char *outval, const struct drbg_string *in)
1692{
1693 int ret = 0;
1694 struct scatterlist sg_in, sg_out;
1695 struct blkcipher_desc desc;
1696 struct crypto_blkcipher *tfm =
1697 (struct crypto_blkcipher *)drbg->priv_data;
1698
1699 desc.tfm = tfm;
1700 desc.flags = 0;
1701 crypto_blkcipher_setkey(tfm, key, (drbg_keylen(drbg)));
1702 /* there is only component in *in */
1703 sg_init_one(&sg_in, in->buf, in->len);
1704 sg_init_one(&sg_out, outval, drbg_blocklen(drbg));
1705 ret = crypto_blkcipher_encrypt(&desc, &sg_out, &sg_in, in->len);
1706
1707 return ret;
1708}
1709#endif /* CONFIG_CRYPTO_DRBG_CTR */
1710
1711/***************************************************************
1712 * Kernel crypto API interface to register DRBG
1713 ***************************************************************/
1714
1715/*
1716 * Look up the DRBG flags by given kernel crypto API cra_name
1717 * The code uses the drbg_cores definition to do this
1718 *
1719 * @cra_name kernel crypto API cra_name
1720 * @coreref reference to integer which is filled with the pointer to
1721 * the applicable core
1722 * @pr reference for setting prediction resistance
1723 *
1724 * return: flags
1725 */
1726static inline void drbg_convert_tfm_core(const char *cra_driver_name,
1727 int *coreref, bool *pr)
1728{
1729 int i = 0;
1730 size_t start = 0;
1731 int len = 0;
1732
1733 *pr = true;
1734 /* disassemble the names */
1735 if (!memcmp(cra_driver_name, "drbg_nopr_", 10)) {
1736 start = 10;
1737 *pr = false;
1738 } else if (!memcmp(cra_driver_name, "drbg_pr_", 8)) {
1739 start = 8;
1740 } else {
1741 return;
1742 }
1743
1744 /* remove the first part */
1745 len = strlen(cra_driver_name) - start;
1746 for (i = 0; ARRAY_SIZE(drbg_cores) > i; i++) {
1747 if (!memcmp(cra_driver_name + start, drbg_cores[i].cra_name,
1748 len)) {
1749 *coreref = i;
1750 return;
1751 }
1752 }
1753}
1754
1755static int drbg_kcapi_init(struct crypto_tfm *tfm)
1756{
1757 struct drbg_state *drbg = crypto_tfm_ctx(tfm);
1758 bool pr = false;
1759 int coreref = 0;
1760
1761 drbg_convert_tfm_core(crypto_tfm_alg_name(tfm), &coreref, &pr);
1762 /*
1763 * when personalization string is needed, the caller must call reset
1764 * and provide the personalization string as seed information
1765 */
1766 return drbg_instantiate(drbg, NULL, coreref, pr);
1767}
1768
1769static void drbg_kcapi_cleanup(struct crypto_tfm *tfm)
1770{
1771 drbg_uninstantiate(crypto_tfm_ctx(tfm));
1772}
1773
1774/*
1775 * Generate random numbers invoked by the kernel crypto API:
1776 * The API of the kernel crypto API is extended as follows:
1777 *
1778 * If dlen is larger than zero, rdata is interpreted as the output buffer
1779 * where random data is to be stored.
1780 *
1781 * If dlen is zero, rdata is interpreted as a pointer to a struct drbg_gen
1782 * which holds the additional information string that is used for the
1783 * DRBG generation process. The output buffer that is to be used to store
1784 * data is also pointed to by struct drbg_gen.
1785 */
1786static int drbg_kcapi_random(struct crypto_rng *tfm, u8 *rdata,
1787 unsigned int dlen)
1788{
1789 struct drbg_state *drbg = crypto_rng_ctx(tfm);
1790 if (0 < dlen) {
1791 return drbg_generate_long(drbg, rdata, dlen, NULL);
1792 } else {
1793 struct drbg_gen *data = (struct drbg_gen *)rdata;
Stephan Mueller8c987162014-06-28 21:58:24 +02001794 struct drbg_string addtl;
Stephan Mueller541af942014-05-31 15:44:17 +02001795 /* catch NULL pointer */
1796 if (!data)
1797 return 0;
1798 drbg_set_testdata(drbg, data->test_data);
Stephan Mueller8c987162014-06-28 21:58:24 +02001799 /* linked list variable is now local to allow modification */
1800 drbg_string_fill(&addtl, data->addtl->buf, data->addtl->len);
Stephan Mueller541af942014-05-31 15:44:17 +02001801 return drbg_generate_long(drbg, data->outbuf, data->outlen,
Stephan Mueller8c987162014-06-28 21:58:24 +02001802 &addtl);
Stephan Mueller541af942014-05-31 15:44:17 +02001803 }
1804}
1805
1806/*
1807 * Reset the DRBG invoked by the kernel crypto API
1808 * The reset implies a full re-initialization of the DRBG. Similar to the
1809 * generate function of drbg_kcapi_random, this function extends the
1810 * kernel crypto API interface with struct drbg_gen
1811 */
1812static int drbg_kcapi_reset(struct crypto_rng *tfm, u8 *seed, unsigned int slen)
1813{
1814 struct drbg_state *drbg = crypto_rng_ctx(tfm);
1815 struct crypto_tfm *tfm_base = crypto_rng_tfm(tfm);
1816 bool pr = false;
1817 struct drbg_string seed_string;
1818 int coreref = 0;
1819
1820 drbg_uninstantiate(drbg);
1821 drbg_convert_tfm_core(crypto_tfm_alg_driver_name(tfm_base), &coreref,
1822 &pr);
1823 if (0 < slen) {
1824 drbg_string_fill(&seed_string, seed, slen);
1825 return drbg_instantiate(drbg, &seed_string, coreref, pr);
1826 } else {
1827 struct drbg_gen *data = (struct drbg_gen *)seed;
1828 /* allow invocation of API call with NULL, 0 */
1829 if (!data)
1830 return drbg_instantiate(drbg, NULL, coreref, pr);
1831 drbg_set_testdata(drbg, data->test_data);
Stephan Mueller8c987162014-06-28 21:58:24 +02001832 /* linked list variable is now local to allow modification */
1833 drbg_string_fill(&seed_string, data->addtl->buf,
1834 data->addtl->len);
1835 return drbg_instantiate(drbg, &seed_string, coreref, pr);
Stephan Mueller541af942014-05-31 15:44:17 +02001836 }
1837}
1838
1839/***************************************************************
1840 * Kernel module: code to load the module
1841 ***************************************************************/
1842
1843/*
1844 * Tests as defined in 11.3.2 in addition to the cipher tests: testing
1845 * of the error handling.
1846 *
1847 * Note: testing of failing seed source as defined in 11.3.2 is not applicable
1848 * as seed source of get_random_bytes does not fail.
1849 *
1850 * Note 2: There is no sensible way of testing the reseed counter
1851 * enforcement, so skip it.
1852 */
1853static inline int __init drbg_healthcheck_sanity(void)
1854{
1855#ifdef CONFIG_CRYPTO_FIPS
1856 int len = 0;
1857#define OUTBUFLEN 16
1858 unsigned char buf[OUTBUFLEN];
1859 struct drbg_state *drbg = NULL;
1860 int ret = -EFAULT;
1861 int rc = -EFAULT;
1862 bool pr = false;
1863 int coreref = 0;
1864 struct drbg_string addtl;
1865 size_t max_addtllen, max_request_bytes;
1866
1867 /* only perform test in FIPS mode */
1868 if (!fips_enabled)
1869 return 0;
1870
1871#ifdef CONFIG_CRYPTO_DRBG_CTR
1872 drbg_convert_tfm_core("drbg_nopr_ctr_aes128", &coreref, &pr);
Stephan Muellere25e47e2014-07-06 02:23:03 +02001873#elif defined CONFIG_CRYPTO_DRBG_HASH
Stephan Mueller541af942014-05-31 15:44:17 +02001874 drbg_convert_tfm_core("drbg_nopr_sha256", &coreref, &pr);
1875#else
1876 drbg_convert_tfm_core("drbg_nopr_hmac_sha256", &coreref, &pr);
1877#endif
1878
1879 drbg = kzalloc(sizeof(struct drbg_state), GFP_KERNEL);
1880 if (!drbg)
1881 return -ENOMEM;
1882
1883 /*
1884 * if the following tests fail, it is likely that there is a buffer
1885 * overflow as buf is much smaller than the requested or provided
1886 * string lengths -- in case the error handling does not succeed
1887 * we may get an OOPS. And we want to get an OOPS as this is a
1888 * grave bug.
1889 */
1890
1891 /* get a valid instance of DRBG for following tests */
1892 ret = drbg_instantiate(drbg, NULL, coreref, pr);
1893 if (ret) {
1894 rc = ret;
1895 goto outbuf;
1896 }
1897 max_addtllen = drbg_max_addtl(drbg);
1898 max_request_bytes = drbg_max_request_bytes(drbg);
1899 drbg_string_fill(&addtl, buf, max_addtllen + 1);
1900 /* overflow addtllen with additonal info string */
1901 len = drbg_generate(drbg, buf, OUTBUFLEN, &addtl);
1902 BUG_ON(0 < len);
1903 /* overflow max_bits */
1904 len = drbg_generate(drbg, buf, (max_request_bytes + 1), NULL);
1905 BUG_ON(0 < len);
1906 drbg_uninstantiate(drbg);
1907
1908 /* overflow max addtllen with personalization string */
1909 ret = drbg_instantiate(drbg, &addtl, coreref, pr);
1910 BUG_ON(0 == ret);
1911 /* test uninstantated DRBG */
1912 len = drbg_generate(drbg, buf, (max_request_bytes + 1), NULL);
1913 BUG_ON(0 < len);
1914 /* all tests passed */
1915 rc = 0;
1916
1917 pr_devel("DRBG: Sanity tests for failure code paths successfully "
1918 "completed\n");
1919
1920 drbg_uninstantiate(drbg);
1921outbuf:
1922 kzfree(drbg);
1923 return rc;
1924#else /* CONFIG_CRYPTO_FIPS */
1925 return 0;
1926#endif /* CONFIG_CRYPTO_FIPS */
1927}
1928
1929static struct crypto_alg drbg_algs[22];
1930
1931/*
1932 * Fill the array drbg_algs used to register the different DRBGs
1933 * with the kernel crypto API. To fill the array, the information
1934 * from drbg_cores[] is used.
1935 */
1936static inline void __init drbg_fill_array(struct crypto_alg *alg,
1937 const struct drbg_core *core, int pr)
1938{
1939 int pos = 0;
1940 static int priority = 100;
1941
1942 memset(alg, 0, sizeof(struct crypto_alg));
1943 memcpy(alg->cra_name, "stdrng", 6);
1944 if (pr) {
1945 memcpy(alg->cra_driver_name, "drbg_pr_", 8);
1946 pos = 8;
1947 } else {
1948 memcpy(alg->cra_driver_name, "drbg_nopr_", 10);
1949 pos = 10;
1950 }
1951 memcpy(alg->cra_driver_name + pos, core->cra_name,
1952 strlen(core->cra_name));
1953
1954 alg->cra_priority = priority;
1955 priority++;
1956 /*
1957 * If FIPS mode enabled, the selected DRBG shall have the
1958 * highest cra_priority over other stdrng instances to ensure
1959 * it is selected.
1960 */
1961 if (fips_enabled)
1962 alg->cra_priority += 200;
1963
1964 alg->cra_flags = CRYPTO_ALG_TYPE_RNG;
1965 alg->cra_ctxsize = sizeof(struct drbg_state);
1966 alg->cra_type = &crypto_rng_type;
1967 alg->cra_module = THIS_MODULE;
1968 alg->cra_init = drbg_kcapi_init;
1969 alg->cra_exit = drbg_kcapi_cleanup;
1970 alg->cra_u.rng.rng_make_random = drbg_kcapi_random;
1971 alg->cra_u.rng.rng_reset = drbg_kcapi_reset;
1972 alg->cra_u.rng.seedsize = 0;
1973}
1974
1975static int __init drbg_init(void)
1976{
1977 unsigned int i = 0; /* pointer to drbg_algs */
1978 unsigned int j = 0; /* pointer to drbg_cores */
1979 int ret = -EFAULT;
1980
1981 ret = drbg_healthcheck_sanity();
1982 if (ret)
1983 return ret;
1984
1985 if (ARRAY_SIZE(drbg_cores) * 2 > ARRAY_SIZE(drbg_algs)) {
1986 pr_info("DRBG: Cannot register all DRBG types"
1987 "(slots needed: %lu, slots available: %lu)\n",
1988 ARRAY_SIZE(drbg_cores) * 2, ARRAY_SIZE(drbg_algs));
1989 return ret;
1990 }
1991
1992 /*
1993 * each DRBG definition can be used with PR and without PR, thus
1994 * we instantiate each DRBG in drbg_cores[] twice.
1995 *
1996 * As the order of placing them into the drbg_algs array matters
1997 * (the later DRBGs receive a higher cra_priority) we register the
1998 * prediction resistance DRBGs first as the should not be too
1999 * interesting.
2000 */
2001 for (j = 0; ARRAY_SIZE(drbg_cores) > j; j++, i++)
2002 drbg_fill_array(&drbg_algs[i], &drbg_cores[j], 1);
2003 for (j = 0; ARRAY_SIZE(drbg_cores) > j; j++, i++)
2004 drbg_fill_array(&drbg_algs[i], &drbg_cores[j], 0);
2005 return crypto_register_algs(drbg_algs, (ARRAY_SIZE(drbg_cores) * 2));
2006}
2007
2008void __exit drbg_exit(void)
2009{
2010 crypto_unregister_algs(drbg_algs, (ARRAY_SIZE(drbg_cores) * 2));
2011}
2012
2013module_init(drbg_init);
2014module_exit(drbg_exit);
Stephan Muellere25e47e2014-07-06 02:23:03 +02002015#ifndef CRYPTO_DRBG_HASH_STRING
2016#define CRYPTO_DRBG_HASH_STRING ""
2017#endif
2018#ifndef CRYPTO_DRBG_HMAC_STRING
2019#define CRYPTO_DRBG_HMAC_STRING ""
2020#endif
2021#ifndef CRYPTO_DRBG_CTR_STRING
2022#define CRYPTO_DRBG_CTR_STRING ""
2023#endif
Stephan Mueller541af942014-05-31 15:44:17 +02002024MODULE_LICENSE("GPL");
2025MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
Stephan Muellere25e47e2014-07-06 02:23:03 +02002026MODULE_DESCRIPTION("NIST SP800-90A Deterministic Random Bit Generator (DRBG) "
2027 "using following cores: "
2028 CRYPTO_DRBG_HASH_STRING
2029 CRYPTO_DRBG_HMAC_STRING
2030 CRYPTO_DRBG_CTR_STRING);