blob: d86c67792e617e3ab89af03c082078daf55079f8 [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 {
Stephan Mueller5b635e22014-07-06 02:26:37 +0200187 .flags = DRBG_HMAC | DRBG_STRENGTH128,
Stephan Mueller541af942014-05-31 15:44:17 +0200188 .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 *
Stephan Mueller541af942014-05-31 15:44:17 +0200305 * @val value to be converted
Stephan Mueller72f3e002014-08-17 17:37:34 +0200306 * @buf buffer holding the converted integer -- caller must ensure that
307 * buffer size is at least 32 bit
Stephan Mueller541af942014-05-31 15:44:17 +0200308 */
309#if (defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_CTR))
Stephan Mueller72f3e002014-08-17 17:37:34 +0200310static inline void drbg_cpu_to_be32(__u32 val, unsigned char *buf)
Stephan Mueller541af942014-05-31 15:44:17 +0200311{
Stephan Mueller72f3e002014-08-17 17:37:34 +0200312 struct s {
313 __u32 conv;
314 };
315 struct s *conversion = (struct s *) buf;
Stephan Mueller541af942014-05-31 15:44:17 +0200316
Stephan Mueller72f3e002014-08-17 17:37:34 +0200317 conversion->conv = cpu_to_be32(val);
Stephan Mueller541af942014-05-31 15:44:17 +0200318}
319
320/*
321 * Increment buffer
322 *
323 * @dst buffer to increment
324 * @add value to add
325 */
326static inline void drbg_add_buf(unsigned char *dst, size_t dstlen,
327 const unsigned char *add, size_t addlen)
328{
329 /* implied: dstlen > addlen */
330 unsigned char *dstptr;
331 const unsigned char *addptr;
332 unsigned int remainder = 0;
333 size_t len = addlen;
334
335 dstptr = dst + (dstlen-1);
336 addptr = add + (addlen-1);
337 while (len) {
338 remainder += *dstptr + *addptr;
339 *dstptr = remainder & 0xff;
340 remainder >>= 8;
341 len--; dstptr--; addptr--;
342 }
343 len = dstlen - addlen;
344 while (len && remainder > 0) {
345 remainder = *dstptr + 1;
346 *dstptr = remainder & 0xff;
347 remainder >>= 8;
348 len--; dstptr--;
349 }
350}
351#endif /* defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_CTR) */
352
353/******************************************************************
354 * CTR DRBG callback functions
355 ******************************************************************/
356
357#ifdef CONFIG_CRYPTO_DRBG_CTR
Stephan Muellere25e47e2014-07-06 02:23:03 +0200358#define CRYPTO_DRBG_CTR_STRING "CTR "
Stephan Mueller541af942014-05-31 15:44:17 +0200359static int drbg_kcapi_sym(struct drbg_state *drbg, const unsigned char *key,
360 unsigned char *outval, const struct drbg_string *in);
361static int drbg_init_sym_kernel(struct drbg_state *drbg);
362static int drbg_fini_sym_kernel(struct drbg_state *drbg);
363
364/* BCC function for CTR DRBG as defined in 10.4.3 */
365static int drbg_ctr_bcc(struct drbg_state *drbg,
366 unsigned char *out, const unsigned char *key,
Stephan Mueller8c987162014-06-28 21:58:24 +0200367 struct list_head *in)
Stephan Mueller541af942014-05-31 15:44:17 +0200368{
Stephan Mueller8c987162014-06-28 21:58:24 +0200369 int ret = 0;
370 struct drbg_string *curr = NULL;
Stephan Mueller541af942014-05-31 15:44:17 +0200371 struct drbg_string data;
Stephan Mueller8c987162014-06-28 21:58:24 +0200372 short cnt = 0;
Stephan Mueller541af942014-05-31 15:44:17 +0200373
374 drbg_string_fill(&data, out, drbg_blocklen(drbg));
375
376 /* 10.4.3 step 1 */
377 memset(out, 0, drbg_blocklen(drbg));
378
379 /* 10.4.3 step 2 / 4 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200380 list_for_each_entry(curr, in, list) {
381 const unsigned char *pos = curr->buf;
382 size_t len = curr->len;
Stephan Mueller541af942014-05-31 15:44:17 +0200383 /* 10.4.3 step 4.1 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200384 while (len) {
385 /* 10.4.3 step 4.2 */
386 if (drbg_blocklen(drbg) == cnt) {
387 cnt = 0;
388 ret = drbg_kcapi_sym(drbg, key, out, &data);
389 if (ret)
390 return ret;
Stephan Mueller541af942014-05-31 15:44:17 +0200391 }
Stephan Mueller8c987162014-06-28 21:58:24 +0200392 out[cnt] ^= *pos;
393 pos++;
394 cnt++;
395 len--;
Stephan Mueller541af942014-05-31 15:44:17 +0200396 }
Stephan Mueller541af942014-05-31 15:44:17 +0200397 }
Stephan Mueller8c987162014-06-28 21:58:24 +0200398 /* 10.4.3 step 4.2 for last block */
399 if (cnt)
400 ret = drbg_kcapi_sym(drbg, key, out, &data);
401
402 return ret;
Stephan Mueller541af942014-05-31 15:44:17 +0200403}
404
405/*
406 * scratchpad usage: drbg_ctr_update is interlinked with drbg_ctr_df
407 * (and drbg_ctr_bcc, but this function does not need any temporary buffers),
408 * the scratchpad is used as follows:
409 * drbg_ctr_update:
410 * temp
411 * start: drbg->scratchpad
412 * length: drbg_statelen(drbg) + drbg_blocklen(drbg)
413 * note: the cipher writing into this variable works
414 * blocklen-wise. Now, when the statelen is not a multiple
415 * of blocklen, the generateion loop below "spills over"
416 * by at most blocklen. Thus, we need to give sufficient
417 * memory.
418 * df_data
419 * start: drbg->scratchpad +
420 * drbg_statelen(drbg) + drbg_blocklen(drbg)
421 * length: drbg_statelen(drbg)
422 *
423 * drbg_ctr_df:
424 * pad
425 * start: df_data + drbg_statelen(drbg)
426 * length: drbg_blocklen(drbg)
427 * iv
428 * start: pad + drbg_blocklen(drbg)
429 * length: drbg_blocklen(drbg)
430 * temp
431 * start: iv + drbg_blocklen(drbg)
Stephan Mueller8fecaad2014-07-01 17:08:48 +0200432 * length: drbg_satelen(drbg) + drbg_blocklen(drbg)
433 * note: temp is the buffer that the BCC function operates
434 * on. BCC operates blockwise. drbg_statelen(drbg)
435 * is sufficient when the DRBG state length is a multiple
436 * of the block size. For AES192 (and maybe other ciphers)
437 * this is not correct and the length for temp is
438 * insufficient (yes, that also means for such ciphers,
439 * the final output of all BCC rounds are truncated).
440 * Therefore, add drbg_blocklen(drbg) to cover all
441 * possibilities.
Stephan Mueller541af942014-05-31 15:44:17 +0200442 */
443
444/* Derivation Function for CTR DRBG as defined in 10.4.2 */
445static int drbg_ctr_df(struct drbg_state *drbg,
446 unsigned char *df_data, size_t bytes_to_return,
Stephan Mueller8c987162014-06-28 21:58:24 +0200447 struct list_head *seedlist)
Stephan Mueller541af942014-05-31 15:44:17 +0200448{
449 int ret = -EFAULT;
450 unsigned char L_N[8];
451 /* S3 is input */
452 struct drbg_string S1, S2, S4, cipherin;
Stephan Mueller8c987162014-06-28 21:58:24 +0200453 LIST_HEAD(bcc_list);
Stephan Mueller541af942014-05-31 15:44:17 +0200454 unsigned char *pad = df_data + drbg_statelen(drbg);
455 unsigned char *iv = pad + drbg_blocklen(drbg);
456 unsigned char *temp = iv + drbg_blocklen(drbg);
457 size_t padlen = 0;
458 unsigned int templen = 0;
459 /* 10.4.2 step 7 */
460 unsigned int i = 0;
461 /* 10.4.2 step 8 */
462 const unsigned char *K = (unsigned char *)
463 "\x00\x01\x02\x03\x04\x05\x06\x07"
464 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
465 "\x10\x11\x12\x13\x14\x15\x16\x17"
466 "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f";
467 unsigned char *X;
468 size_t generated_len = 0;
469 size_t inputlen = 0;
Stephan Mueller8c987162014-06-28 21:58:24 +0200470 struct drbg_string *seed = NULL;
Stephan Mueller541af942014-05-31 15:44:17 +0200471
472 memset(pad, 0, drbg_blocklen(drbg));
473 memset(iv, 0, drbg_blocklen(drbg));
474 memset(temp, 0, drbg_statelen(drbg));
475
476 /* 10.4.2 step 1 is implicit as we work byte-wise */
477
478 /* 10.4.2 step 2 */
479 if ((512/8) < bytes_to_return)
480 return -EINVAL;
481
482 /* 10.4.2 step 2 -- calculate the entire length of all input data */
Stephan Mueller8c987162014-06-28 21:58:24 +0200483 list_for_each_entry(seed, seedlist, list)
484 inputlen += seed->len;
Stephan Mueller72f3e002014-08-17 17:37:34 +0200485 drbg_cpu_to_be32(inputlen, &L_N[0]);
Stephan Mueller541af942014-05-31 15:44:17 +0200486
487 /* 10.4.2 step 3 */
Stephan Mueller72f3e002014-08-17 17:37:34 +0200488 drbg_cpu_to_be32(bytes_to_return, &L_N[4]);
Stephan Mueller541af942014-05-31 15:44:17 +0200489
490 /* 10.4.2 step 5: length is L_N, input_string, one byte, padding */
491 padlen = (inputlen + sizeof(L_N) + 1) % (drbg_blocklen(drbg));
492 /* wrap the padlen appropriately */
493 if (padlen)
494 padlen = drbg_blocklen(drbg) - padlen;
495 /*
496 * pad / padlen contains the 0x80 byte and the following zero bytes.
497 * As the calculated padlen value only covers the number of zero
498 * bytes, this value has to be incremented by one for the 0x80 byte.
499 */
500 padlen++;
501 pad[0] = 0x80;
502
503 /* 10.4.2 step 4 -- first fill the linked list and then order it */
504 drbg_string_fill(&S1, iv, drbg_blocklen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +0200505 list_add_tail(&S1.list, &bcc_list);
Stephan Mueller541af942014-05-31 15:44:17 +0200506 drbg_string_fill(&S2, L_N, sizeof(L_N));
Stephan Mueller8c987162014-06-28 21:58:24 +0200507 list_add_tail(&S2.list, &bcc_list);
508 list_splice_tail(seedlist, &bcc_list);
Stephan Mueller541af942014-05-31 15:44:17 +0200509 drbg_string_fill(&S4, pad, padlen);
Stephan Mueller8c987162014-06-28 21:58:24 +0200510 list_add_tail(&S4.list, &bcc_list);
Stephan Mueller541af942014-05-31 15:44:17 +0200511
512 /* 10.4.2 step 9 */
513 while (templen < (drbg_keylen(drbg) + (drbg_blocklen(drbg)))) {
514 /*
515 * 10.4.2 step 9.1 - the padding is implicit as the buffer
516 * holds zeros after allocation -- even the increment of i
517 * is irrelevant as the increment remains within length of i
518 */
Stephan Mueller72f3e002014-08-17 17:37:34 +0200519 drbg_cpu_to_be32(i, iv);
Stephan Mueller541af942014-05-31 15:44:17 +0200520 /* 10.4.2 step 9.2 -- BCC and concatenation with temp */
Stephan Mueller8c987162014-06-28 21:58:24 +0200521 ret = drbg_ctr_bcc(drbg, temp + templen, K, &bcc_list);
Stephan Mueller541af942014-05-31 15:44:17 +0200522 if (ret)
523 goto out;
524 /* 10.4.2 step 9.3 */
525 i++;
526 templen += drbg_blocklen(drbg);
527 }
528
529 /* 10.4.2 step 11 */
530 X = temp + (drbg_keylen(drbg));
531 drbg_string_fill(&cipherin, X, drbg_blocklen(drbg));
532
533 /* 10.4.2 step 12: overwriting of outval is implemented in next step */
534
535 /* 10.4.2 step 13 */
536 while (generated_len < bytes_to_return) {
537 short blocklen = 0;
538 /*
539 * 10.4.2 step 13.1: the truncation of the key length is
540 * implicit as the key is only drbg_blocklen in size based on
541 * the implementation of the cipher function callback
542 */
543 ret = drbg_kcapi_sym(drbg, temp, X, &cipherin);
544 if (ret)
545 goto out;
546 blocklen = (drbg_blocklen(drbg) <
547 (bytes_to_return - generated_len)) ?
548 drbg_blocklen(drbg) :
549 (bytes_to_return - generated_len);
550 /* 10.4.2 step 13.2 and 14 */
551 memcpy(df_data + generated_len, X, blocklen);
552 generated_len += blocklen;
553 }
554
555 ret = 0;
556
557out:
558 memset(iv, 0, drbg_blocklen(drbg));
559 memset(temp, 0, drbg_statelen(drbg));
560 memset(pad, 0, drbg_blocklen(drbg));
561 return ret;
562}
563
Stephan Mueller72e7c252014-07-06 02:24:35 +0200564/*
565 * update function of CTR DRBG as defined in 10.2.1.2
566 *
567 * The reseed variable has an enhanced meaning compared to the update
568 * functions of the other DRBGs as follows:
569 * 0 => initial seed from initialization
570 * 1 => reseed via drbg_seed
571 * 2 => first invocation from drbg_ctr_update when addtl is present. In
572 * this case, the df_data scratchpad is not deleted so that it is
573 * available for another calls to prevent calling the DF function
574 * again.
575 * 3 => second invocation from drbg_ctr_update. When the update function
576 * was called with addtl, the df_data memory already contains the
577 * DFed addtl information and we do not need to call DF again.
578 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200579static int drbg_ctr_update(struct drbg_state *drbg, struct list_head *seed,
580 int reseed)
Stephan Mueller541af942014-05-31 15:44:17 +0200581{
582 int ret = -EFAULT;
583 /* 10.2.1.2 step 1 */
584 unsigned char *temp = drbg->scratchpad;
585 unsigned char *df_data = drbg->scratchpad + drbg_statelen(drbg) +
586 drbg_blocklen(drbg);
587 unsigned char *temp_p, *df_data_p; /* pointer to iterate over buffers */
588 unsigned int len = 0;
589 struct drbg_string cipherin;
590 unsigned char prefix = DRBG_PREFIX1;
591
592 memset(temp, 0, drbg_statelen(drbg) + drbg_blocklen(drbg));
Stephan Mueller72e7c252014-07-06 02:24:35 +0200593 if (3 > reseed)
594 memset(df_data, 0, drbg_statelen(drbg));
Stephan Mueller541af942014-05-31 15:44:17 +0200595
596 /* 10.2.1.3.2 step 2 and 10.2.1.4.2 step 2 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200597 if (seed) {
598 ret = drbg_ctr_df(drbg, df_data, drbg_statelen(drbg), seed);
Stephan Mueller541af942014-05-31 15:44:17 +0200599 if (ret)
600 goto out;
601 }
602
603 drbg_string_fill(&cipherin, drbg->V, drbg_blocklen(drbg));
604 /*
605 * 10.2.1.3.2 steps 2 and 3 are already covered as the allocation
606 * zeroizes all memory during initialization
607 */
608 while (len < (drbg_statelen(drbg))) {
609 /* 10.2.1.2 step 2.1 */
610 drbg_add_buf(drbg->V, drbg_blocklen(drbg), &prefix, 1);
611 /*
612 * 10.2.1.2 step 2.2 */
613 ret = drbg_kcapi_sym(drbg, drbg->C, temp + len, &cipherin);
614 if (ret)
615 goto out;
616 /* 10.2.1.2 step 2.3 and 3 */
617 len += drbg_blocklen(drbg);
618 }
619
620 /* 10.2.1.2 step 4 */
621 temp_p = temp;
622 df_data_p = df_data;
623 for (len = 0; len < drbg_statelen(drbg); len++) {
624 *temp_p ^= *df_data_p;
625 df_data_p++; temp_p++;
626 }
627
628 /* 10.2.1.2 step 5 */
629 memcpy(drbg->C, temp, drbg_keylen(drbg));
630 /* 10.2.1.2 step 6 */
631 memcpy(drbg->V, temp + drbg_keylen(drbg), drbg_blocklen(drbg));
632 ret = 0;
633
634out:
635 memset(temp, 0, drbg_statelen(drbg) + drbg_blocklen(drbg));
Stephan Mueller72e7c252014-07-06 02:24:35 +0200636 if (2 != reseed)
637 memset(df_data, 0, drbg_statelen(drbg));
Stephan Mueller541af942014-05-31 15:44:17 +0200638 return ret;
639}
640
641/*
642 * scratchpad use: drbg_ctr_update is called independently from
643 * drbg_ctr_extract_bytes. Therefore, the scratchpad is reused
644 */
645/* Generate function of CTR DRBG as defined in 10.2.1.5.2 */
646static int drbg_ctr_generate(struct drbg_state *drbg,
647 unsigned char *buf, unsigned int buflen,
Stephan Mueller27e4de22014-07-06 02:25:36 +0200648 struct list_head *addtl)
Stephan Mueller541af942014-05-31 15:44:17 +0200649{
650 int len = 0;
651 int ret = 0;
652 struct drbg_string data;
653 unsigned char prefix = DRBG_PREFIX1;
654
655 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
656
657 /* 10.2.1.5.2 step 2 */
Stephan Mueller27e4de22014-07-06 02:25:36 +0200658 if (addtl && !list_empty(addtl)) {
659 ret = drbg_ctr_update(drbg, addtl, 2);
Stephan Mueller541af942014-05-31 15:44:17 +0200660 if (ret)
661 return 0;
662 }
663
664 /* 10.2.1.5.2 step 4.1 */
665 drbg_add_buf(drbg->V, drbg_blocklen(drbg), &prefix, 1);
666 drbg_string_fill(&data, drbg->V, drbg_blocklen(drbg));
667 while (len < buflen) {
668 int outlen = 0;
669 /* 10.2.1.5.2 step 4.2 */
670 ret = drbg_kcapi_sym(drbg, drbg->C, drbg->scratchpad, &data);
671 if (ret) {
672 len = ret;
673 goto out;
674 }
675 outlen = (drbg_blocklen(drbg) < (buflen - len)) ?
676 drbg_blocklen(drbg) : (buflen - len);
677 if (!drbg_fips_continuous_test(drbg, drbg->scratchpad)) {
678 /* 10.2.1.5.2 step 6 */
679 drbg_add_buf(drbg->V, drbg_blocklen(drbg), &prefix, 1);
680 continue;
681 }
682 /* 10.2.1.5.2 step 4.3 */
683 memcpy(buf + len, drbg->scratchpad, outlen);
684 len += outlen;
685 /* 10.2.1.5.2 step 6 */
686 if (len < buflen)
687 drbg_add_buf(drbg->V, drbg_blocklen(drbg), &prefix, 1);
688 }
689
Stephan Mueller72e7c252014-07-06 02:24:35 +0200690 /* 10.2.1.5.2 step 6 */
691 ret = drbg_ctr_update(drbg, NULL, 3);
Stephan Mueller541af942014-05-31 15:44:17 +0200692 if (ret)
693 len = ret;
694
695out:
696 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
697 return len;
698}
699
700static struct drbg_state_ops drbg_ctr_ops = {
701 .update = drbg_ctr_update,
702 .generate = drbg_ctr_generate,
703 .crypto_init = drbg_init_sym_kernel,
704 .crypto_fini = drbg_fini_sym_kernel,
705};
706#endif /* CONFIG_CRYPTO_DRBG_CTR */
707
708/******************************************************************
709 * HMAC DRBG callback functions
710 ******************************************************************/
711
712#if defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_HMAC)
713static int drbg_kcapi_hash(struct drbg_state *drbg, const unsigned char *key,
Stephan Mueller8c987162014-06-28 21:58:24 +0200714 unsigned char *outval, const struct list_head *in);
Stephan Mueller541af942014-05-31 15:44:17 +0200715static int drbg_init_hash_kernel(struct drbg_state *drbg);
716static int drbg_fini_hash_kernel(struct drbg_state *drbg);
717#endif /* (CONFIG_CRYPTO_DRBG_HASH || CONFIG_CRYPTO_DRBG_HMAC) */
718
719#ifdef CONFIG_CRYPTO_DRBG_HMAC
Stephan Muellere25e47e2014-07-06 02:23:03 +0200720#define CRYPTO_DRBG_HMAC_STRING "HMAC "
Stephan Mueller541af942014-05-31 15:44:17 +0200721/* update function of HMAC DRBG as defined in 10.1.2.2 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200722static int drbg_hmac_update(struct drbg_state *drbg, struct list_head *seed,
723 int reseed)
Stephan Mueller541af942014-05-31 15:44:17 +0200724{
725 int ret = -EFAULT;
726 int i = 0;
Stephan Mueller8c987162014-06-28 21:58:24 +0200727 struct drbg_string seed1, seed2, vdata;
728 LIST_HEAD(seedlist);
729 LIST_HEAD(vdatalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200730
731 if (!reseed) {
732 /* 10.1.2.3 step 2 */
733 memset(drbg->C, 0, drbg_statelen(drbg));
734 memset(drbg->V, 1, drbg_statelen(drbg));
735 }
736
737 drbg_string_fill(&seed1, drbg->V, drbg_statelen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +0200738 list_add_tail(&seed1.list, &seedlist);
Stephan Mueller541af942014-05-31 15:44:17 +0200739 /* buffer of seed2 will be filled in for loop below with one byte */
740 drbg_string_fill(&seed2, NULL, 1);
Stephan Mueller8c987162014-06-28 21:58:24 +0200741 list_add_tail(&seed2.list, &seedlist);
Stephan Mueller541af942014-05-31 15:44:17 +0200742 /* input data of seed is allowed to be NULL at this point */
Stephan Mueller8c987162014-06-28 21:58:24 +0200743 if (seed)
744 list_splice_tail(seed, &seedlist);
Stephan Mueller541af942014-05-31 15:44:17 +0200745
Stephan Mueller8c987162014-06-28 21:58:24 +0200746 drbg_string_fill(&vdata, drbg->V, drbg_statelen(drbg));
747 list_add_tail(&vdata.list, &vdatalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200748 for (i = 2; 0 < i; i--) {
749 /* first round uses 0x0, second 0x1 */
750 unsigned char prefix = DRBG_PREFIX0;
751 if (1 == i)
752 prefix = DRBG_PREFIX1;
753 /* 10.1.2.2 step 1 and 4 -- concatenation and HMAC for key */
754 seed2.buf = &prefix;
Stephan Mueller8c987162014-06-28 21:58:24 +0200755 ret = drbg_kcapi_hash(drbg, drbg->C, drbg->C, &seedlist);
Stephan Mueller541af942014-05-31 15:44:17 +0200756 if (ret)
757 return ret;
758
759 /* 10.1.2.2 step 2 and 5 -- HMAC for V */
Stephan Mueller8c987162014-06-28 21:58:24 +0200760 ret = drbg_kcapi_hash(drbg, drbg->C, drbg->V, &vdatalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200761 if (ret)
762 return ret;
763
764 /* 10.1.2.2 step 3 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200765 if (!seed)
Stephan Mueller541af942014-05-31 15:44:17 +0200766 return ret;
767 }
768
769 return 0;
770}
771
772/* generate function of HMAC DRBG as defined in 10.1.2.5 */
773static int drbg_hmac_generate(struct drbg_state *drbg,
774 unsigned char *buf,
775 unsigned int buflen,
Stephan Mueller27e4de22014-07-06 02:25:36 +0200776 struct list_head *addtl)
Stephan Mueller541af942014-05-31 15:44:17 +0200777{
778 int len = 0;
779 int ret = 0;
780 struct drbg_string data;
Stephan Mueller8c987162014-06-28 21:58:24 +0200781 LIST_HEAD(datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200782
783 /* 10.1.2.5 step 2 */
Stephan Mueller27e4de22014-07-06 02:25:36 +0200784 if (addtl && !list_empty(addtl)) {
785 ret = drbg_hmac_update(drbg, addtl, 1);
Stephan Mueller541af942014-05-31 15:44:17 +0200786 if (ret)
787 return ret;
788 }
789
790 drbg_string_fill(&data, drbg->V, drbg_statelen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +0200791 list_add_tail(&data.list, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200792 while (len < buflen) {
793 unsigned int outlen = 0;
794 /* 10.1.2.5 step 4.1 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200795 ret = drbg_kcapi_hash(drbg, drbg->C, drbg->V, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200796 if (ret)
797 return ret;
798 outlen = (drbg_blocklen(drbg) < (buflen - len)) ?
799 drbg_blocklen(drbg) : (buflen - len);
800 if (!drbg_fips_continuous_test(drbg, drbg->V))
801 continue;
802
803 /* 10.1.2.5 step 4.2 */
804 memcpy(buf + len, drbg->V, outlen);
805 len += outlen;
806 }
807
808 /* 10.1.2.5 step 6 */
Stephan Mueller27e4de22014-07-06 02:25:36 +0200809 if (addtl && !list_empty(addtl))
810 ret = drbg_hmac_update(drbg, addtl, 1);
811 else
Stephan Mueller8c987162014-06-28 21:58:24 +0200812 ret = drbg_hmac_update(drbg, NULL, 1);
Stephan Mueller541af942014-05-31 15:44:17 +0200813 if (ret)
814 return ret;
815
816 return len;
817}
818
819static struct drbg_state_ops drbg_hmac_ops = {
820 .update = drbg_hmac_update,
821 .generate = drbg_hmac_generate,
822 .crypto_init = drbg_init_hash_kernel,
823 .crypto_fini = drbg_fini_hash_kernel,
824
825};
826#endif /* CONFIG_CRYPTO_DRBG_HMAC */
827
828/******************************************************************
829 * Hash DRBG callback functions
830 ******************************************************************/
831
832#ifdef CONFIG_CRYPTO_DRBG_HASH
Stephan Muellere25e47e2014-07-06 02:23:03 +0200833#define CRYPTO_DRBG_HASH_STRING "HASH "
Stephan Mueller541af942014-05-31 15:44:17 +0200834/*
835 * scratchpad usage: as drbg_hash_update and drbg_hash_df are used
836 * interlinked, the scratchpad is used as follows:
837 * drbg_hash_update
838 * start: drbg->scratchpad
839 * length: drbg_statelen(drbg)
840 * drbg_hash_df:
841 * start: drbg->scratchpad + drbg_statelen(drbg)
842 * length: drbg_blocklen(drbg)
843 *
844 * drbg_hash_process_addtl uses the scratchpad, but fully completes
845 * before either of the functions mentioned before are invoked. Therefore,
846 * drbg_hash_process_addtl does not need to be specifically considered.
847 */
848
849/* Derivation Function for Hash DRBG as defined in 10.4.1 */
850static int drbg_hash_df(struct drbg_state *drbg,
851 unsigned char *outval, size_t outlen,
Stephan Mueller8c987162014-06-28 21:58:24 +0200852 struct list_head *entropylist)
Stephan Mueller541af942014-05-31 15:44:17 +0200853{
854 int ret = 0;
855 size_t len = 0;
856 unsigned char input[5];
857 unsigned char *tmp = drbg->scratchpad + drbg_statelen(drbg);
Stephan Mueller8c987162014-06-28 21:58:24 +0200858 struct drbg_string data;
Stephan Mueller541af942014-05-31 15:44:17 +0200859
860 memset(tmp, 0, drbg_blocklen(drbg));
861
862 /* 10.4.1 step 3 */
863 input[0] = 1;
Stephan Mueller72f3e002014-08-17 17:37:34 +0200864 drbg_cpu_to_be32((outlen * 8), &input[1]);
Stephan Mueller541af942014-05-31 15:44:17 +0200865
866 /* 10.4.1 step 4.1 -- concatenation of data for input into hash */
Stephan Mueller8c987162014-06-28 21:58:24 +0200867 drbg_string_fill(&data, input, 5);
868 list_add(&data.list, entropylist);
Stephan Mueller541af942014-05-31 15:44:17 +0200869
870 /* 10.4.1 step 4 */
871 while (len < outlen) {
872 short blocklen = 0;
873 /* 10.4.1 step 4.1 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200874 ret = drbg_kcapi_hash(drbg, NULL, tmp, entropylist);
Stephan Mueller541af942014-05-31 15:44:17 +0200875 if (ret)
876 goto out;
877 /* 10.4.1 step 4.2 */
878 input[0]++;
879 blocklen = (drbg_blocklen(drbg) < (outlen - len)) ?
880 drbg_blocklen(drbg) : (outlen - len);
881 memcpy(outval + len, tmp, blocklen);
882 len += blocklen;
883 }
884
885out:
886 memset(tmp, 0, drbg_blocklen(drbg));
887 return ret;
888}
889
890/* update function for Hash DRBG as defined in 10.1.1.2 / 10.1.1.3 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200891static int drbg_hash_update(struct drbg_state *drbg, struct list_head *seed,
Stephan Mueller541af942014-05-31 15:44:17 +0200892 int reseed)
893{
894 int ret = 0;
895 struct drbg_string data1, data2;
Stephan Mueller8c987162014-06-28 21:58:24 +0200896 LIST_HEAD(datalist);
897 LIST_HEAD(datalist2);
Stephan Mueller541af942014-05-31 15:44:17 +0200898 unsigned char *V = drbg->scratchpad;
899 unsigned char prefix = DRBG_PREFIX1;
900
901 memset(drbg->scratchpad, 0, drbg_statelen(drbg));
902 if (!seed)
903 return -EINVAL;
904
905 if (reseed) {
906 /* 10.1.1.3 step 1 */
907 memcpy(V, drbg->V, drbg_statelen(drbg));
908 drbg_string_fill(&data1, &prefix, 1);
Stephan Mueller8c987162014-06-28 21:58:24 +0200909 list_add_tail(&data1.list, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200910 drbg_string_fill(&data2, V, drbg_statelen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +0200911 list_add_tail(&data2.list, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200912 }
Stephan Mueller8c987162014-06-28 21:58:24 +0200913 list_splice_tail(seed, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200914
915 /* 10.1.1.2 / 10.1.1.3 step 2 and 3 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200916 ret = drbg_hash_df(drbg, drbg->V, drbg_statelen(drbg), &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200917 if (ret)
918 goto out;
919
920 /* 10.1.1.2 / 10.1.1.3 step 4 */
921 prefix = DRBG_PREFIX0;
922 drbg_string_fill(&data1, &prefix, 1);
Stephan Mueller8c987162014-06-28 21:58:24 +0200923 list_add_tail(&data1.list, &datalist2);
Stephan Mueller541af942014-05-31 15:44:17 +0200924 drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +0200925 list_add_tail(&data2.list, &datalist2);
Stephan Mueller541af942014-05-31 15:44:17 +0200926 /* 10.1.1.2 / 10.1.1.3 step 4 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200927 ret = drbg_hash_df(drbg, drbg->C, drbg_statelen(drbg), &datalist2);
Stephan Mueller541af942014-05-31 15:44:17 +0200928
929out:
930 memset(drbg->scratchpad, 0, drbg_statelen(drbg));
931 return ret;
932}
933
934/* processing of additional information string for Hash DRBG */
935static int drbg_hash_process_addtl(struct drbg_state *drbg,
Stephan Mueller27e4de22014-07-06 02:25:36 +0200936 struct list_head *addtl)
Stephan Mueller541af942014-05-31 15:44:17 +0200937{
938 int ret = 0;
939 struct drbg_string data1, data2;
Stephan Mueller8c987162014-06-28 21:58:24 +0200940 LIST_HEAD(datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200941 unsigned char prefix = DRBG_PREFIX2;
942
943 /* this is value w as per documentation */
944 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
945
946 /* 10.1.1.4 step 2 */
Stephan Mueller27e4de22014-07-06 02:25:36 +0200947 if (!addtl || list_empty(addtl))
Stephan Mueller541af942014-05-31 15:44:17 +0200948 return 0;
949
950 /* 10.1.1.4 step 2a */
951 drbg_string_fill(&data1, &prefix, 1);
952 drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +0200953 list_add_tail(&data1.list, &datalist);
954 list_add_tail(&data2.list, &datalist);
Stephan Mueller27e4de22014-07-06 02:25:36 +0200955 list_splice_tail(addtl, &datalist);
Stephan Mueller8c987162014-06-28 21:58:24 +0200956 ret = drbg_kcapi_hash(drbg, NULL, drbg->scratchpad, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200957 if (ret)
958 goto out;
959
960 /* 10.1.1.4 step 2b */
961 drbg_add_buf(drbg->V, drbg_statelen(drbg),
962 drbg->scratchpad, drbg_blocklen(drbg));
963
964out:
965 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
966 return ret;
967}
968
969/* Hashgen defined in 10.1.1.4 */
970static int drbg_hash_hashgen(struct drbg_state *drbg,
971 unsigned char *buf,
972 unsigned int buflen)
973{
974 int len = 0;
975 int ret = 0;
976 unsigned char *src = drbg->scratchpad;
977 unsigned char *dst = drbg->scratchpad + drbg_statelen(drbg);
978 struct drbg_string data;
Stephan Mueller8c987162014-06-28 21:58:24 +0200979 LIST_HEAD(datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200980 unsigned char prefix = DRBG_PREFIX1;
981
982 memset(src, 0, drbg_statelen(drbg));
983 memset(dst, 0, drbg_blocklen(drbg));
984
985 /* 10.1.1.4 step hashgen 2 */
986 memcpy(src, drbg->V, drbg_statelen(drbg));
987
988 drbg_string_fill(&data, src, drbg_statelen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +0200989 list_add_tail(&data.list, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200990 while (len < buflen) {
991 unsigned int outlen = 0;
992 /* 10.1.1.4 step hashgen 4.1 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200993 ret = drbg_kcapi_hash(drbg, NULL, dst, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200994 if (ret) {
995 len = ret;
996 goto out;
997 }
998 outlen = (drbg_blocklen(drbg) < (buflen - len)) ?
999 drbg_blocklen(drbg) : (buflen - len);
1000 if (!drbg_fips_continuous_test(drbg, dst)) {
1001 drbg_add_buf(src, drbg_statelen(drbg), &prefix, 1);
1002 continue;
1003 }
1004 /* 10.1.1.4 step hashgen 4.2 */
1005 memcpy(buf + len, dst, outlen);
1006 len += outlen;
1007 /* 10.1.1.4 hashgen step 4.3 */
1008 if (len < buflen)
1009 drbg_add_buf(src, drbg_statelen(drbg), &prefix, 1);
1010 }
1011
1012out:
1013 memset(drbg->scratchpad, 0,
1014 (drbg_statelen(drbg) + drbg_blocklen(drbg)));
1015 return len;
1016}
1017
1018/* generate function for Hash DRBG as defined in 10.1.1.4 */
1019static int drbg_hash_generate(struct drbg_state *drbg,
1020 unsigned char *buf, unsigned int buflen,
Stephan Mueller27e4de22014-07-06 02:25:36 +02001021 struct list_head *addtl)
Stephan Mueller541af942014-05-31 15:44:17 +02001022{
1023 int len = 0;
1024 int ret = 0;
Stephan Mueller72f3e002014-08-17 17:37:34 +02001025 union {
1026 unsigned char req[8];
1027 __u64 req_int;
1028 } u;
Stephan Mueller541af942014-05-31 15:44:17 +02001029 unsigned char prefix = DRBG_PREFIX3;
1030 struct drbg_string data1, data2;
Stephan Mueller8c987162014-06-28 21:58:24 +02001031 LIST_HEAD(datalist);
Stephan Mueller541af942014-05-31 15:44:17 +02001032
1033 /* 10.1.1.4 step 2 */
1034 ret = drbg_hash_process_addtl(drbg, addtl);
1035 if (ret)
1036 return ret;
1037 /* 10.1.1.4 step 3 */
1038 len = drbg_hash_hashgen(drbg, buf, buflen);
1039
1040 /* this is the value H as documented in 10.1.1.4 */
1041 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
1042 /* 10.1.1.4 step 4 */
1043 drbg_string_fill(&data1, &prefix, 1);
Stephan Mueller8c987162014-06-28 21:58:24 +02001044 list_add_tail(&data1.list, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +02001045 drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +02001046 list_add_tail(&data2.list, &datalist);
1047 ret = drbg_kcapi_hash(drbg, NULL, drbg->scratchpad, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +02001048 if (ret) {
1049 len = ret;
1050 goto out;
1051 }
1052
1053 /* 10.1.1.4 step 5 */
1054 drbg_add_buf(drbg->V, drbg_statelen(drbg),
1055 drbg->scratchpad, drbg_blocklen(drbg));
1056 drbg_add_buf(drbg->V, drbg_statelen(drbg),
1057 drbg->C, drbg_statelen(drbg));
Stephan Mueller72f3e002014-08-17 17:37:34 +02001058 u.req_int = cpu_to_be64(drbg->reseed_ctr);
1059 drbg_add_buf(drbg->V, drbg_statelen(drbg), u.req, 8);
Stephan Mueller541af942014-05-31 15:44:17 +02001060
1061out:
1062 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
1063 return len;
1064}
1065
1066/*
1067 * scratchpad usage: as update and generate are used isolated, both
1068 * can use the scratchpad
1069 */
1070static struct drbg_state_ops drbg_hash_ops = {
1071 .update = drbg_hash_update,
1072 .generate = drbg_hash_generate,
1073 .crypto_init = drbg_init_hash_kernel,
1074 .crypto_fini = drbg_fini_hash_kernel,
1075};
1076#endif /* CONFIG_CRYPTO_DRBG_HASH */
1077
1078/******************************************************************
1079 * Functions common for DRBG implementations
1080 ******************************************************************/
1081
1082/*
1083 * Seeding or reseeding of the DRBG
1084 *
1085 * @drbg: DRBG state struct
1086 * @pers: personalization / additional information buffer
1087 * @reseed: 0 for initial seed process, 1 for reseeding
1088 *
1089 * return:
1090 * 0 on success
1091 * error value otherwise
1092 */
1093static int drbg_seed(struct drbg_state *drbg, struct drbg_string *pers,
1094 bool reseed)
1095{
1096 int ret = 0;
1097 unsigned char *entropy = NULL;
1098 size_t entropylen = 0;
1099 struct drbg_string data1;
Stephan Mueller8c987162014-06-28 21:58:24 +02001100 LIST_HEAD(seedlist);
Stephan Mueller541af942014-05-31 15:44:17 +02001101
1102 /* 9.1 / 9.2 / 9.3.1 step 3 */
1103 if (pers && pers->len > (drbg_max_addtl(drbg))) {
Stephan Muellera9089572014-07-06 02:24:03 +02001104 pr_devel("DRBG: personalization string too long %zu\n",
Stephan Mueller541af942014-05-31 15:44:17 +02001105 pers->len);
1106 return -EINVAL;
1107 }
1108
1109 if (drbg->test_data && drbg->test_data->testentropy) {
1110 drbg_string_fill(&data1, drbg->test_data->testentropy->buf,
1111 drbg->test_data->testentropy->len);
1112 pr_devel("DRBG: using test entropy\n");
1113 } else {
1114 /*
1115 * Gather entropy equal to the security strength of the DRBG.
1116 * With a derivation function, a nonce is required in addition
1117 * to the entropy. A nonce must be at least 1/2 of the security
1118 * strength of the DRBG in size. Thus, entropy * nonce is 3/2
1119 * of the strength. The consideration of a nonce is only
1120 * applicable during initial seeding.
1121 */
1122 entropylen = drbg_sec_strength(drbg->core->flags);
1123 if (!entropylen)
1124 return -EFAULT;
1125 if (!reseed)
1126 entropylen = ((entropylen + 1) / 2) * 3;
1127 pr_devel("DRBG: (re)seeding with %zu bytes of entropy\n",
1128 entropylen);
1129 entropy = kzalloc(entropylen, GFP_KERNEL);
1130 if (!entropy)
1131 return -ENOMEM;
1132 get_random_bytes(entropy, entropylen);
1133 drbg_string_fill(&data1, entropy, entropylen);
1134 }
Stephan Mueller8c987162014-06-28 21:58:24 +02001135 list_add_tail(&data1.list, &seedlist);
Stephan Mueller541af942014-05-31 15:44:17 +02001136
1137 /*
1138 * concatenation of entropy with personalization str / addtl input)
1139 * the variable pers is directly handed in by the caller, so check its
1140 * contents whether it is appropriate
1141 */
Stephan Mueller8c987162014-06-28 21:58:24 +02001142 if (pers && pers->buf && 0 < pers->len) {
1143 list_add_tail(&pers->list, &seedlist);
Stephan Mueller541af942014-05-31 15:44:17 +02001144 pr_devel("DRBG: using personalization string\n");
1145 }
1146
Stephan Mueller8c987162014-06-28 21:58:24 +02001147 ret = drbg->d_ops->update(drbg, &seedlist, reseed);
Stephan Mueller541af942014-05-31 15:44:17 +02001148 if (ret)
1149 goto out;
1150
1151 drbg->seeded = true;
1152 /* 10.1.1.2 / 10.1.1.3 step 5 */
1153 drbg->reseed_ctr = 1;
1154
1155out:
1156 if (entropy)
1157 kzfree(entropy);
1158 return ret;
1159}
1160
1161/* Free all substructures in a DRBG state without the DRBG state structure */
1162static inline void drbg_dealloc_state(struct drbg_state *drbg)
1163{
1164 if (!drbg)
1165 return;
1166 if (drbg->V)
1167 kzfree(drbg->V);
1168 drbg->V = NULL;
1169 if (drbg->C)
1170 kzfree(drbg->C);
1171 drbg->C = NULL;
1172 if (drbg->scratchpad)
1173 kzfree(drbg->scratchpad);
1174 drbg->scratchpad = NULL;
1175 drbg->reseed_ctr = 0;
1176#ifdef CONFIG_CRYPTO_FIPS
1177 if (drbg->prev)
1178 kzfree(drbg->prev);
1179 drbg->prev = NULL;
1180 drbg->fips_primed = false;
1181#endif
1182}
1183
1184/*
1185 * Allocate all sub-structures for a DRBG state.
1186 * The DRBG state structure must already be allocated.
1187 */
1188static inline int drbg_alloc_state(struct drbg_state *drbg)
1189{
1190 int ret = -ENOMEM;
1191 unsigned int sb_size = 0;
1192
1193 if (!drbg)
1194 return -EINVAL;
1195
1196 drbg->V = kzalloc(drbg_statelen(drbg), GFP_KERNEL);
1197 if (!drbg->V)
1198 goto err;
1199 drbg->C = kzalloc(drbg_statelen(drbg), GFP_KERNEL);
1200 if (!drbg->C)
1201 goto err;
1202#ifdef CONFIG_CRYPTO_FIPS
1203 drbg->prev = kzalloc(drbg_blocklen(drbg), GFP_KERNEL);
1204 if (!drbg->prev)
1205 goto err;
1206 drbg->fips_primed = false;
1207#endif
1208 /* scratchpad is only generated for CTR and Hash */
1209 if (drbg->core->flags & DRBG_HMAC)
1210 sb_size = 0;
1211 else if (drbg->core->flags & DRBG_CTR)
1212 sb_size = drbg_statelen(drbg) + drbg_blocklen(drbg) + /* temp */
1213 drbg_statelen(drbg) + /* df_data */
1214 drbg_blocklen(drbg) + /* pad */
1215 drbg_blocklen(drbg) + /* iv */
Stephan Mueller8fecaad2014-07-01 17:08:48 +02001216 drbg_statelen(drbg) + drbg_blocklen(drbg); /* temp */
Stephan Mueller541af942014-05-31 15:44:17 +02001217 else
1218 sb_size = drbg_statelen(drbg) + drbg_blocklen(drbg);
1219
1220 if (0 < sb_size) {
1221 drbg->scratchpad = kzalloc(sb_size, GFP_KERNEL);
1222 if (!drbg->scratchpad)
1223 goto err;
1224 }
1225 spin_lock_init(&drbg->drbg_lock);
1226 return 0;
1227
1228err:
1229 drbg_dealloc_state(drbg);
1230 return ret;
1231}
1232
1233/*
1234 * Strategy to avoid holding long term locks: generate a shadow copy of DRBG
1235 * and perform all operations on this shadow copy. After finishing, restore
1236 * the updated state of the shadow copy into original drbg state. This way,
1237 * only the read and write operations of the original drbg state must be
1238 * locked
1239 */
1240static inline void drbg_copy_drbg(struct drbg_state *src,
1241 struct drbg_state *dst)
1242{
1243 if (!src || !dst)
1244 return;
1245 memcpy(dst->V, src->V, drbg_statelen(src));
1246 memcpy(dst->C, src->C, drbg_statelen(src));
1247 dst->reseed_ctr = src->reseed_ctr;
1248 dst->seeded = src->seeded;
1249 dst->pr = src->pr;
1250#ifdef CONFIG_CRYPTO_FIPS
1251 dst->fips_primed = src->fips_primed;
1252 memcpy(dst->prev, src->prev, drbg_blocklen(src));
1253#endif
1254 /*
1255 * Not copied:
1256 * scratchpad is initialized drbg_alloc_state;
1257 * priv_data is initialized with call to crypto_init;
1258 * d_ops and core are set outside, as these parameters are const;
1259 * test_data is set outside to prevent it being copied back.
1260 */
1261}
1262
1263static int drbg_make_shadow(struct drbg_state *drbg, struct drbg_state **shadow)
1264{
1265 int ret = -ENOMEM;
1266 struct drbg_state *tmp = NULL;
1267
1268 if (!drbg || !drbg->core || !drbg->V || !drbg->C) {
1269 pr_devel("DRBG: attempt to generate shadow copy for "
1270 "uninitialized DRBG state rejected\n");
1271 return -EINVAL;
1272 }
1273 /* HMAC does not have a scratchpad */
1274 if (!(drbg->core->flags & DRBG_HMAC) && NULL == drbg->scratchpad)
1275 return -EINVAL;
1276
1277 tmp = kzalloc(sizeof(struct drbg_state), GFP_KERNEL);
1278 if (!tmp)
1279 return -ENOMEM;
1280
1281 /* read-only data as they are defined as const, no lock needed */
1282 tmp->core = drbg->core;
1283 tmp->d_ops = drbg->d_ops;
1284
1285 ret = drbg_alloc_state(tmp);
1286 if (ret)
1287 goto err;
1288
1289 spin_lock_bh(&drbg->drbg_lock);
1290 drbg_copy_drbg(drbg, tmp);
1291 /* only make a link to the test buffer, as we only read that data */
1292 tmp->test_data = drbg->test_data;
1293 spin_unlock_bh(&drbg->drbg_lock);
1294 *shadow = tmp;
1295 return 0;
1296
1297err:
1298 if (tmp)
1299 kzfree(tmp);
1300 return ret;
1301}
1302
1303static void drbg_restore_shadow(struct drbg_state *drbg,
1304 struct drbg_state **shadow)
1305{
1306 struct drbg_state *tmp = *shadow;
1307
1308 spin_lock_bh(&drbg->drbg_lock);
1309 drbg_copy_drbg(tmp, drbg);
1310 spin_unlock_bh(&drbg->drbg_lock);
1311 drbg_dealloc_state(tmp);
1312 kzfree(tmp);
1313 *shadow = NULL;
1314}
1315
1316/*************************************************************************
1317 * DRBG interface functions
1318 *************************************************************************/
1319
1320/*
1321 * DRBG generate function as required by SP800-90A - this function
1322 * generates random numbers
1323 *
1324 * @drbg DRBG state handle
1325 * @buf Buffer where to store the random numbers -- the buffer must already
1326 * be pre-allocated by caller
1327 * @buflen Length of output buffer - this value defines the number of random
1328 * bytes pulled from DRBG
1329 * @addtl Additional input that is mixed into state, may be NULL -- note
1330 * the entropy is pulled by the DRBG internally unconditionally
1331 * as defined in SP800-90A. The additional input is mixed into
1332 * the state in addition to the pulled entropy.
1333 *
1334 * return: generated number of bytes
1335 */
1336static int drbg_generate(struct drbg_state *drbg,
1337 unsigned char *buf, unsigned int buflen,
1338 struct drbg_string *addtl)
1339{
1340 int len = 0;
1341 struct drbg_state *shadow = NULL;
Stephan Mueller27e4de22014-07-06 02:25:36 +02001342 LIST_HEAD(addtllist);
1343 struct drbg_string timestamp;
1344 union {
1345 cycles_t cycles;
1346 unsigned char char_cycles[sizeof(cycles_t)];
1347 } now;
Stephan Mueller541af942014-05-31 15:44:17 +02001348
1349 if (0 == buflen || !buf) {
1350 pr_devel("DRBG: no output buffer provided\n");
1351 return -EINVAL;
1352 }
1353 if (addtl && NULL == addtl->buf && 0 < addtl->len) {
1354 pr_devel("DRBG: wrong format of additional information\n");
1355 return -EINVAL;
1356 }
1357
1358 len = drbg_make_shadow(drbg, &shadow);
1359 if (len) {
1360 pr_devel("DRBG: shadow copy cannot be generated\n");
1361 return len;
1362 }
1363
1364 /* 9.3.1 step 2 */
1365 len = -EINVAL;
1366 if (buflen > (drbg_max_request_bytes(shadow))) {
1367 pr_devel("DRBG: requested random numbers too large %u\n",
1368 buflen);
1369 goto err;
1370 }
1371
1372 /* 9.3.1 step 3 is implicit with the chosen DRBG */
1373
1374 /* 9.3.1 step 4 */
1375 if (addtl && addtl->len > (drbg_max_addtl(shadow))) {
1376 pr_devel("DRBG: additional information string too long %zu\n",
1377 addtl->len);
1378 goto err;
1379 }
1380 /* 9.3.1 step 5 is implicit with the chosen DRBG */
1381
1382 /*
1383 * 9.3.1 step 6 and 9 supplemented by 9.3.2 step c is implemented
1384 * here. The spec is a bit convoluted here, we make it simpler.
1385 */
1386 if ((drbg_max_requests(shadow)) < shadow->reseed_ctr)
1387 shadow->seeded = false;
1388
1389 /* allocate cipher handle */
1390 if (shadow->d_ops->crypto_init) {
1391 len = shadow->d_ops->crypto_init(shadow);
1392 if (len)
1393 goto err;
1394 }
1395
1396 if (shadow->pr || !shadow->seeded) {
1397 pr_devel("DRBG: reseeding before generation (prediction "
1398 "resistance: %s, state %s)\n",
1399 drbg->pr ? "true" : "false",
1400 drbg->seeded ? "seeded" : "unseeded");
1401 /* 9.3.1 steps 7.1 through 7.3 */
1402 len = drbg_seed(shadow, addtl, true);
1403 if (len)
1404 goto err;
1405 /* 9.3.1 step 7.4 */
1406 addtl = NULL;
1407 }
Stephan Mueller27e4de22014-07-06 02:25:36 +02001408
1409 /*
1410 * Mix the time stamp into the DRBG state if the DRBG is not in
1411 * test mode. If there are two callers invoking the DRBG at the same
1412 * time, i.e. before the first caller merges its shadow state back,
1413 * both callers would obtain the same random number stream without
1414 * changing the state here.
1415 */
1416 if (!drbg->test_data) {
1417 now.cycles = random_get_entropy();
1418 drbg_string_fill(&timestamp, now.char_cycles, sizeof(cycles_t));
1419 list_add_tail(&timestamp.list, &addtllist);
1420 }
1421 if (addtl && 0 < addtl->len)
1422 list_add_tail(&addtl->list, &addtllist);
Stephan Mueller541af942014-05-31 15:44:17 +02001423 /* 9.3.1 step 8 and 10 */
Stephan Mueller27e4de22014-07-06 02:25:36 +02001424 len = shadow->d_ops->generate(shadow, buf, buflen, &addtllist);
Stephan Mueller541af942014-05-31 15:44:17 +02001425
1426 /* 10.1.1.4 step 6, 10.1.2.5 step 7, 10.2.1.5.2 step 7 */
1427 shadow->reseed_ctr++;
1428 if (0 >= len)
1429 goto err;
1430
1431 /*
1432 * Section 11.3.3 requires to re-perform self tests after some
1433 * generated random numbers. The chosen value after which self
1434 * test is performed is arbitrary, but it should be reasonable.
1435 * However, we do not perform the self tests because of the following
1436 * reasons: it is mathematically impossible that the initial self tests
1437 * were successfully and the following are not. If the initial would
1438 * pass and the following would not, the kernel integrity is violated.
1439 * In this case, the entire kernel operation is questionable and it
1440 * is unlikely that the integrity violation only affects the
1441 * correct operation of the DRBG.
1442 *
1443 * Albeit the following code is commented out, it is provided in
1444 * case somebody has a need to implement the test of 11.3.3.
1445 */
1446#if 0
1447 if (shadow->reseed_ctr && !(shadow->reseed_ctr % 4096)) {
1448 int err = 0;
1449 pr_devel("DRBG: start to perform self test\n");
1450 if (drbg->core->flags & DRBG_HMAC)
1451 err = alg_test("drbg_pr_hmac_sha256",
1452 "drbg_pr_hmac_sha256", 0, 0);
1453 else if (drbg->core->flags & DRBG_CTR)
1454 err = alg_test("drbg_pr_ctr_aes128",
1455 "drbg_pr_ctr_aes128", 0, 0);
1456 else
1457 err = alg_test("drbg_pr_sha256",
1458 "drbg_pr_sha256", 0, 0);
1459 if (err) {
1460 pr_err("DRBG: periodical self test failed\n");
1461 /*
1462 * uninstantiate implies that from now on, only errors
1463 * are returned when reusing this DRBG cipher handle
1464 */
1465 drbg_uninstantiate(drbg);
1466 drbg_dealloc_state(shadow);
1467 kzfree(shadow);
1468 return 0;
1469 } else {
1470 pr_devel("DRBG: self test successful\n");
1471 }
1472 }
1473#endif
1474
1475err:
1476 if (shadow->d_ops->crypto_fini)
1477 shadow->d_ops->crypto_fini(shadow);
1478 drbg_restore_shadow(drbg, &shadow);
1479 return len;
1480}
1481
1482/*
1483 * Wrapper around drbg_generate which can pull arbitrary long strings
1484 * from the DRBG without hitting the maximum request limitation.
1485 *
1486 * Parameters: see drbg_generate
1487 * Return codes: see drbg_generate -- if one drbg_generate request fails,
1488 * the entire drbg_generate_long request fails
1489 */
1490static int drbg_generate_long(struct drbg_state *drbg,
1491 unsigned char *buf, unsigned int buflen,
1492 struct drbg_string *addtl)
1493{
1494 int len = 0;
1495 unsigned int slice = 0;
1496 do {
1497 int tmplen = 0;
1498 unsigned int chunk = 0;
1499 slice = ((buflen - len) / drbg_max_request_bytes(drbg));
1500 chunk = slice ? drbg_max_request_bytes(drbg) : (buflen - len);
1501 tmplen = drbg_generate(drbg, buf + len, chunk, addtl);
1502 if (0 >= tmplen)
1503 return tmplen;
1504 len += tmplen;
Stephan Muellerce5481d2014-07-31 21:47:33 +02001505 } while (slice > 0 && (len < buflen));
Stephan Mueller541af942014-05-31 15:44:17 +02001506 return len;
1507}
1508
1509/*
1510 * DRBG instantiation function as required by SP800-90A - this function
1511 * sets up the DRBG handle, performs the initial seeding and all sanity
1512 * checks required by SP800-90A
1513 *
1514 * @drbg memory of state -- if NULL, new memory is allocated
1515 * @pers Personalization string that is mixed into state, may be NULL -- note
1516 * the entropy is pulled by the DRBG internally unconditionally
1517 * as defined in SP800-90A. The additional input is mixed into
1518 * the state in addition to the pulled entropy.
1519 * @coreref reference to core
1520 * @pr prediction resistance enabled
1521 *
1522 * return
1523 * 0 on success
1524 * error value otherwise
1525 */
1526static int drbg_instantiate(struct drbg_state *drbg, struct drbg_string *pers,
1527 int coreref, bool pr)
1528{
1529 int ret = -ENOMEM;
1530
1531 pr_devel("DRBG: Initializing DRBG core %d with prediction resistance "
1532 "%s\n", coreref, pr ? "enabled" : "disabled");
1533 drbg->core = &drbg_cores[coreref];
1534 drbg->pr = pr;
1535 drbg->seeded = false;
1536 switch (drbg->core->flags & DRBG_TYPE_MASK) {
1537#ifdef CONFIG_CRYPTO_DRBG_HMAC
1538 case DRBG_HMAC:
1539 drbg->d_ops = &drbg_hmac_ops;
1540 break;
1541#endif /* CONFIG_CRYPTO_DRBG_HMAC */
1542#ifdef CONFIG_CRYPTO_DRBG_HASH
1543 case DRBG_HASH:
1544 drbg->d_ops = &drbg_hash_ops;
1545 break;
1546#endif /* CONFIG_CRYPTO_DRBG_HASH */
1547#ifdef CONFIG_CRYPTO_DRBG_CTR
1548 case DRBG_CTR:
1549 drbg->d_ops = &drbg_ctr_ops;
1550 break;
1551#endif /* CONFIG_CRYPTO_DRBG_CTR */
1552 default:
1553 return -EOPNOTSUPP;
1554 }
1555
1556 /* 9.1 step 1 is implicit with the selected DRBG type */
1557
1558 /*
1559 * 9.1 step 2 is implicit as caller can select prediction resistance
1560 * and the flag is copied into drbg->flags --
1561 * all DRBG types support prediction resistance
1562 */
1563
1564 /* 9.1 step 4 is implicit in drbg_sec_strength */
1565
1566 ret = drbg_alloc_state(drbg);
1567 if (ret)
1568 return ret;
1569
1570 ret = -EFAULT;
1571 if (drbg->d_ops->crypto_init && drbg->d_ops->crypto_init(drbg))
1572 goto err;
1573 ret = drbg_seed(drbg, pers, false);
1574 if (drbg->d_ops->crypto_fini)
1575 drbg->d_ops->crypto_fini(drbg);
1576 if (ret)
1577 goto err;
1578
1579 return 0;
1580
1581err:
1582 drbg_dealloc_state(drbg);
1583 return ret;
1584}
1585
1586/*
1587 * DRBG uninstantiate function as required by SP800-90A - this function
1588 * frees all buffers and the DRBG handle
1589 *
1590 * @drbg DRBG state handle
1591 *
1592 * return
1593 * 0 on success
1594 */
1595static int drbg_uninstantiate(struct drbg_state *drbg)
1596{
1597 spin_lock_bh(&drbg->drbg_lock);
1598 drbg_dealloc_state(drbg);
1599 /* no scrubbing of test_data -- this shall survive an uninstantiate */
1600 spin_unlock_bh(&drbg->drbg_lock);
1601 return 0;
1602}
1603
1604/*
1605 * Helper function for setting the test data in the DRBG
1606 *
1607 * @drbg DRBG state handle
1608 * @test_data test data to sets
1609 */
1610static inline void drbg_set_testdata(struct drbg_state *drbg,
1611 struct drbg_test_data *test_data)
1612{
1613 if (!test_data || !test_data->testentropy)
1614 return;
1615 spin_lock_bh(&drbg->drbg_lock);
1616 drbg->test_data = test_data;
1617 spin_unlock_bh(&drbg->drbg_lock);
1618}
1619
1620/***************************************************************
1621 * Kernel crypto API cipher invocations requested by DRBG
1622 ***************************************************************/
1623
1624#if defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_HMAC)
1625struct sdesc {
1626 struct shash_desc shash;
1627 char ctx[];
1628};
1629
1630static int drbg_init_hash_kernel(struct drbg_state *drbg)
1631{
1632 struct sdesc *sdesc;
1633 struct crypto_shash *tfm;
1634
1635 tfm = crypto_alloc_shash(drbg->core->backend_cra_name, 0, 0);
1636 if (IS_ERR(tfm)) {
1637 pr_info("DRBG: could not allocate digest TFM handle\n");
1638 return PTR_ERR(tfm);
1639 }
1640 BUG_ON(drbg_blocklen(drbg) != crypto_shash_digestsize(tfm));
1641 sdesc = kzalloc(sizeof(struct shash_desc) + crypto_shash_descsize(tfm),
1642 GFP_KERNEL);
1643 if (!sdesc) {
1644 crypto_free_shash(tfm);
1645 return -ENOMEM;
1646 }
1647
1648 sdesc->shash.tfm = tfm;
1649 sdesc->shash.flags = 0;
1650 drbg->priv_data = sdesc;
1651 return 0;
1652}
1653
1654static int drbg_fini_hash_kernel(struct drbg_state *drbg)
1655{
1656 struct sdesc *sdesc = (struct sdesc *)drbg->priv_data;
1657 if (sdesc) {
1658 crypto_free_shash(sdesc->shash.tfm);
1659 kzfree(sdesc);
1660 }
1661 drbg->priv_data = NULL;
1662 return 0;
1663}
1664
1665static int drbg_kcapi_hash(struct drbg_state *drbg, const unsigned char *key,
Stephan Mueller8c987162014-06-28 21:58:24 +02001666 unsigned char *outval, const struct list_head *in)
Stephan Mueller541af942014-05-31 15:44:17 +02001667{
1668 struct sdesc *sdesc = (struct sdesc *)drbg->priv_data;
Stephan Mueller8c987162014-06-28 21:58:24 +02001669 struct drbg_string *input = NULL;
Stephan Mueller541af942014-05-31 15:44:17 +02001670
1671 if (key)
1672 crypto_shash_setkey(sdesc->shash.tfm, key, drbg_statelen(drbg));
1673 crypto_shash_init(&sdesc->shash);
Stephan Mueller8c987162014-06-28 21:58:24 +02001674 list_for_each_entry(input, in, list)
1675 crypto_shash_update(&sdesc->shash, input->buf, input->len);
Stephan Mueller541af942014-05-31 15:44:17 +02001676 return crypto_shash_final(&sdesc->shash, outval);
1677}
1678#endif /* (CONFIG_CRYPTO_DRBG_HASH || CONFIG_CRYPTO_DRBG_HMAC) */
1679
1680#ifdef CONFIG_CRYPTO_DRBG_CTR
1681static int drbg_init_sym_kernel(struct drbg_state *drbg)
1682{
1683 int ret = 0;
1684 struct crypto_blkcipher *tfm;
1685
1686 tfm = crypto_alloc_blkcipher(drbg->core->backend_cra_name, 0, 0);
1687 if (IS_ERR(tfm)) {
1688 pr_info("DRBG: could not allocate cipher TFM handle\n");
1689 return PTR_ERR(tfm);
1690 }
1691 BUG_ON(drbg_blocklen(drbg) != crypto_blkcipher_blocksize(tfm));
1692 drbg->priv_data = tfm;
1693 return ret;
1694}
1695
1696static int drbg_fini_sym_kernel(struct drbg_state *drbg)
1697{
1698 struct crypto_blkcipher *tfm =
1699 (struct crypto_blkcipher *)drbg->priv_data;
1700 if (tfm)
1701 crypto_free_blkcipher(tfm);
1702 drbg->priv_data = NULL;
1703 return 0;
1704}
1705
1706static int drbg_kcapi_sym(struct drbg_state *drbg, const unsigned char *key,
1707 unsigned char *outval, const struct drbg_string *in)
1708{
1709 int ret = 0;
1710 struct scatterlist sg_in, sg_out;
1711 struct blkcipher_desc desc;
1712 struct crypto_blkcipher *tfm =
1713 (struct crypto_blkcipher *)drbg->priv_data;
1714
1715 desc.tfm = tfm;
1716 desc.flags = 0;
1717 crypto_blkcipher_setkey(tfm, key, (drbg_keylen(drbg)));
1718 /* there is only component in *in */
1719 sg_init_one(&sg_in, in->buf, in->len);
1720 sg_init_one(&sg_out, outval, drbg_blocklen(drbg));
1721 ret = crypto_blkcipher_encrypt(&desc, &sg_out, &sg_in, in->len);
1722
1723 return ret;
1724}
1725#endif /* CONFIG_CRYPTO_DRBG_CTR */
1726
1727/***************************************************************
1728 * Kernel crypto API interface to register DRBG
1729 ***************************************************************/
1730
1731/*
1732 * Look up the DRBG flags by given kernel crypto API cra_name
1733 * The code uses the drbg_cores definition to do this
1734 *
1735 * @cra_name kernel crypto API cra_name
1736 * @coreref reference to integer which is filled with the pointer to
1737 * the applicable core
1738 * @pr reference for setting prediction resistance
1739 *
1740 * return: flags
1741 */
1742static inline void drbg_convert_tfm_core(const char *cra_driver_name,
1743 int *coreref, bool *pr)
1744{
1745 int i = 0;
1746 size_t start = 0;
1747 int len = 0;
1748
1749 *pr = true;
1750 /* disassemble the names */
1751 if (!memcmp(cra_driver_name, "drbg_nopr_", 10)) {
1752 start = 10;
1753 *pr = false;
1754 } else if (!memcmp(cra_driver_name, "drbg_pr_", 8)) {
1755 start = 8;
1756 } else {
1757 return;
1758 }
1759
1760 /* remove the first part */
1761 len = strlen(cra_driver_name) - start;
1762 for (i = 0; ARRAY_SIZE(drbg_cores) > i; i++) {
1763 if (!memcmp(cra_driver_name + start, drbg_cores[i].cra_name,
1764 len)) {
1765 *coreref = i;
1766 return;
1767 }
1768 }
1769}
1770
1771static int drbg_kcapi_init(struct crypto_tfm *tfm)
1772{
1773 struct drbg_state *drbg = crypto_tfm_ctx(tfm);
1774 bool pr = false;
1775 int coreref = 0;
1776
Stephan Mueller4f150712014-07-06 02:25:04 +02001777 drbg_convert_tfm_core(crypto_tfm_alg_driver_name(tfm), &coreref, &pr);
Stephan Mueller541af942014-05-31 15:44:17 +02001778 /*
1779 * when personalization string is needed, the caller must call reset
1780 * and provide the personalization string as seed information
1781 */
1782 return drbg_instantiate(drbg, NULL, coreref, pr);
1783}
1784
1785static void drbg_kcapi_cleanup(struct crypto_tfm *tfm)
1786{
1787 drbg_uninstantiate(crypto_tfm_ctx(tfm));
1788}
1789
1790/*
1791 * Generate random numbers invoked by the kernel crypto API:
1792 * The API of the kernel crypto API is extended as follows:
1793 *
1794 * If dlen is larger than zero, rdata is interpreted as the output buffer
1795 * where random data is to be stored.
1796 *
1797 * If dlen is zero, rdata is interpreted as a pointer to a struct drbg_gen
1798 * which holds the additional information string that is used for the
1799 * DRBG generation process. The output buffer that is to be used to store
1800 * data is also pointed to by struct drbg_gen.
1801 */
1802static int drbg_kcapi_random(struct crypto_rng *tfm, u8 *rdata,
1803 unsigned int dlen)
1804{
1805 struct drbg_state *drbg = crypto_rng_ctx(tfm);
1806 if (0 < dlen) {
1807 return drbg_generate_long(drbg, rdata, dlen, NULL);
1808 } else {
1809 struct drbg_gen *data = (struct drbg_gen *)rdata;
Stephan Mueller8c987162014-06-28 21:58:24 +02001810 struct drbg_string addtl;
Stephan Mueller541af942014-05-31 15:44:17 +02001811 /* catch NULL pointer */
1812 if (!data)
1813 return 0;
1814 drbg_set_testdata(drbg, data->test_data);
Stephan Mueller8c987162014-06-28 21:58:24 +02001815 /* linked list variable is now local to allow modification */
1816 drbg_string_fill(&addtl, data->addtl->buf, data->addtl->len);
Stephan Mueller541af942014-05-31 15:44:17 +02001817 return drbg_generate_long(drbg, data->outbuf, data->outlen,
Stephan Mueller8c987162014-06-28 21:58:24 +02001818 &addtl);
Stephan Mueller541af942014-05-31 15:44:17 +02001819 }
1820}
1821
1822/*
1823 * Reset the DRBG invoked by the kernel crypto API
1824 * The reset implies a full re-initialization of the DRBG. Similar to the
1825 * generate function of drbg_kcapi_random, this function extends the
1826 * kernel crypto API interface with struct drbg_gen
1827 */
1828static int drbg_kcapi_reset(struct crypto_rng *tfm, u8 *seed, unsigned int slen)
1829{
1830 struct drbg_state *drbg = crypto_rng_ctx(tfm);
1831 struct crypto_tfm *tfm_base = crypto_rng_tfm(tfm);
1832 bool pr = false;
1833 struct drbg_string seed_string;
1834 int coreref = 0;
1835
1836 drbg_uninstantiate(drbg);
1837 drbg_convert_tfm_core(crypto_tfm_alg_driver_name(tfm_base), &coreref,
1838 &pr);
1839 if (0 < slen) {
1840 drbg_string_fill(&seed_string, seed, slen);
1841 return drbg_instantiate(drbg, &seed_string, coreref, pr);
1842 } else {
1843 struct drbg_gen *data = (struct drbg_gen *)seed;
1844 /* allow invocation of API call with NULL, 0 */
1845 if (!data)
1846 return drbg_instantiate(drbg, NULL, coreref, pr);
1847 drbg_set_testdata(drbg, data->test_data);
Stephan Mueller8c987162014-06-28 21:58:24 +02001848 /* linked list variable is now local to allow modification */
1849 drbg_string_fill(&seed_string, data->addtl->buf,
1850 data->addtl->len);
1851 return drbg_instantiate(drbg, &seed_string, coreref, pr);
Stephan Mueller541af942014-05-31 15:44:17 +02001852 }
1853}
1854
1855/***************************************************************
1856 * Kernel module: code to load the module
1857 ***************************************************************/
1858
1859/*
1860 * Tests as defined in 11.3.2 in addition to the cipher tests: testing
1861 * of the error handling.
1862 *
1863 * Note: testing of failing seed source as defined in 11.3.2 is not applicable
1864 * as seed source of get_random_bytes does not fail.
1865 *
1866 * Note 2: There is no sensible way of testing the reseed counter
1867 * enforcement, so skip it.
1868 */
1869static inline int __init drbg_healthcheck_sanity(void)
1870{
1871#ifdef CONFIG_CRYPTO_FIPS
1872 int len = 0;
1873#define OUTBUFLEN 16
1874 unsigned char buf[OUTBUFLEN];
1875 struct drbg_state *drbg = NULL;
1876 int ret = -EFAULT;
1877 int rc = -EFAULT;
1878 bool pr = false;
1879 int coreref = 0;
1880 struct drbg_string addtl;
1881 size_t max_addtllen, max_request_bytes;
1882
1883 /* only perform test in FIPS mode */
1884 if (!fips_enabled)
1885 return 0;
1886
1887#ifdef CONFIG_CRYPTO_DRBG_CTR
1888 drbg_convert_tfm_core("drbg_nopr_ctr_aes128", &coreref, &pr);
Stephan Muellere25e47e2014-07-06 02:23:03 +02001889#elif defined CONFIG_CRYPTO_DRBG_HASH
Stephan Mueller541af942014-05-31 15:44:17 +02001890 drbg_convert_tfm_core("drbg_nopr_sha256", &coreref, &pr);
1891#else
1892 drbg_convert_tfm_core("drbg_nopr_hmac_sha256", &coreref, &pr);
1893#endif
1894
1895 drbg = kzalloc(sizeof(struct drbg_state), GFP_KERNEL);
1896 if (!drbg)
1897 return -ENOMEM;
1898
1899 /*
1900 * if the following tests fail, it is likely that there is a buffer
1901 * overflow as buf is much smaller than the requested or provided
1902 * string lengths -- in case the error handling does not succeed
1903 * we may get an OOPS. And we want to get an OOPS as this is a
1904 * grave bug.
1905 */
1906
1907 /* get a valid instance of DRBG for following tests */
1908 ret = drbg_instantiate(drbg, NULL, coreref, pr);
1909 if (ret) {
1910 rc = ret;
1911 goto outbuf;
1912 }
1913 max_addtllen = drbg_max_addtl(drbg);
1914 max_request_bytes = drbg_max_request_bytes(drbg);
1915 drbg_string_fill(&addtl, buf, max_addtllen + 1);
1916 /* overflow addtllen with additonal info string */
1917 len = drbg_generate(drbg, buf, OUTBUFLEN, &addtl);
1918 BUG_ON(0 < len);
1919 /* overflow max_bits */
1920 len = drbg_generate(drbg, buf, (max_request_bytes + 1), NULL);
1921 BUG_ON(0 < len);
1922 drbg_uninstantiate(drbg);
1923
1924 /* overflow max addtllen with personalization string */
1925 ret = drbg_instantiate(drbg, &addtl, coreref, pr);
1926 BUG_ON(0 == ret);
1927 /* test uninstantated DRBG */
1928 len = drbg_generate(drbg, buf, (max_request_bytes + 1), NULL);
1929 BUG_ON(0 < len);
1930 /* all tests passed */
1931 rc = 0;
1932
1933 pr_devel("DRBG: Sanity tests for failure code paths successfully "
1934 "completed\n");
1935
1936 drbg_uninstantiate(drbg);
1937outbuf:
1938 kzfree(drbg);
1939 return rc;
1940#else /* CONFIG_CRYPTO_FIPS */
1941 return 0;
1942#endif /* CONFIG_CRYPTO_FIPS */
1943}
1944
1945static struct crypto_alg drbg_algs[22];
1946
1947/*
1948 * Fill the array drbg_algs used to register the different DRBGs
1949 * with the kernel crypto API. To fill the array, the information
1950 * from drbg_cores[] is used.
1951 */
1952static inline void __init drbg_fill_array(struct crypto_alg *alg,
1953 const struct drbg_core *core, int pr)
1954{
1955 int pos = 0;
1956 static int priority = 100;
1957
1958 memset(alg, 0, sizeof(struct crypto_alg));
1959 memcpy(alg->cra_name, "stdrng", 6);
1960 if (pr) {
1961 memcpy(alg->cra_driver_name, "drbg_pr_", 8);
1962 pos = 8;
1963 } else {
1964 memcpy(alg->cra_driver_name, "drbg_nopr_", 10);
1965 pos = 10;
1966 }
1967 memcpy(alg->cra_driver_name + pos, core->cra_name,
1968 strlen(core->cra_name));
1969
1970 alg->cra_priority = priority;
1971 priority++;
1972 /*
1973 * If FIPS mode enabled, the selected DRBG shall have the
1974 * highest cra_priority over other stdrng instances to ensure
1975 * it is selected.
1976 */
1977 if (fips_enabled)
1978 alg->cra_priority += 200;
1979
1980 alg->cra_flags = CRYPTO_ALG_TYPE_RNG;
1981 alg->cra_ctxsize = sizeof(struct drbg_state);
1982 alg->cra_type = &crypto_rng_type;
1983 alg->cra_module = THIS_MODULE;
1984 alg->cra_init = drbg_kcapi_init;
1985 alg->cra_exit = drbg_kcapi_cleanup;
1986 alg->cra_u.rng.rng_make_random = drbg_kcapi_random;
1987 alg->cra_u.rng.rng_reset = drbg_kcapi_reset;
1988 alg->cra_u.rng.seedsize = 0;
1989}
1990
1991static int __init drbg_init(void)
1992{
1993 unsigned int i = 0; /* pointer to drbg_algs */
1994 unsigned int j = 0; /* pointer to drbg_cores */
1995 int ret = -EFAULT;
1996
1997 ret = drbg_healthcheck_sanity();
1998 if (ret)
1999 return ret;
2000
2001 if (ARRAY_SIZE(drbg_cores) * 2 > ARRAY_SIZE(drbg_algs)) {
2002 pr_info("DRBG: Cannot register all DRBG types"
Stephan Muellera9089572014-07-06 02:24:03 +02002003 "(slots needed: %zu, slots available: %zu)\n",
Stephan Mueller541af942014-05-31 15:44:17 +02002004 ARRAY_SIZE(drbg_cores) * 2, ARRAY_SIZE(drbg_algs));
2005 return ret;
2006 }
2007
2008 /*
2009 * each DRBG definition can be used with PR and without PR, thus
2010 * we instantiate each DRBG in drbg_cores[] twice.
2011 *
2012 * As the order of placing them into the drbg_algs array matters
2013 * (the later DRBGs receive a higher cra_priority) we register the
2014 * prediction resistance DRBGs first as the should not be too
2015 * interesting.
2016 */
2017 for (j = 0; ARRAY_SIZE(drbg_cores) > j; j++, i++)
2018 drbg_fill_array(&drbg_algs[i], &drbg_cores[j], 1);
2019 for (j = 0; ARRAY_SIZE(drbg_cores) > j; j++, i++)
2020 drbg_fill_array(&drbg_algs[i], &drbg_cores[j], 0);
2021 return crypto_register_algs(drbg_algs, (ARRAY_SIZE(drbg_cores) * 2));
2022}
2023
Fengguang Wu96956ae2014-07-10 16:52:04 +08002024static void __exit drbg_exit(void)
Stephan Mueller541af942014-05-31 15:44:17 +02002025{
2026 crypto_unregister_algs(drbg_algs, (ARRAY_SIZE(drbg_cores) * 2));
2027}
2028
2029module_init(drbg_init);
2030module_exit(drbg_exit);
Stephan Muellere25e47e2014-07-06 02:23:03 +02002031#ifndef CRYPTO_DRBG_HASH_STRING
2032#define CRYPTO_DRBG_HASH_STRING ""
2033#endif
2034#ifndef CRYPTO_DRBG_HMAC_STRING
2035#define CRYPTO_DRBG_HMAC_STRING ""
2036#endif
2037#ifndef CRYPTO_DRBG_CTR_STRING
2038#define CRYPTO_DRBG_CTR_STRING ""
2039#endif
Stephan Mueller541af942014-05-31 15:44:17 +02002040MODULE_LICENSE("GPL");
2041MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
Stephan Muellere25e47e2014-07-06 02:23:03 +02002042MODULE_DESCRIPTION("NIST SP800-90A Deterministic Random Bit Generator (DRBG) "
2043 "using following cores: "
2044 CRYPTO_DRBG_HASH_STRING
2045 CRYPTO_DRBG_HMAC_STRING
2046 CRYPTO_DRBG_CTR_STRING);