blob: 8c0a742b60f6596b9a367338ff533c9d3b6fc755 [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 */
Stephan Mueller541af942014-05-31 15:44:17 +0200120 .blocklen_bytes = 16,
121 .cra_name = "ctr_aes128",
122 .backend_cra_name = "ecb(aes)",
123 }, {
124 .flags = DRBG_CTR | DRBG_STRENGTH192,
125 .statelen = 40, /* 320 bits as defined in 10.2.1 */
Stephan Mueller541af942014-05-31 15:44:17 +0200126 .blocklen_bytes = 16,
127 .cra_name = "ctr_aes192",
128 .backend_cra_name = "ecb(aes)",
129 }, {
130 .flags = DRBG_CTR | DRBG_STRENGTH256,
131 .statelen = 48, /* 384 bits as defined in 10.2.1 */
Stephan Mueller541af942014-05-31 15:44:17 +0200132 .blocklen_bytes = 16,
133 .cra_name = "ctr_aes256",
134 .backend_cra_name = "ecb(aes)",
135 },
136#endif /* CONFIG_CRYPTO_DRBG_CTR */
137#ifdef CONFIG_CRYPTO_DRBG_HASH
138 {
139 .flags = DRBG_HASH | DRBG_STRENGTH128,
140 .statelen = 55, /* 440 bits */
Stephan Mueller541af942014-05-31 15:44:17 +0200141 .blocklen_bytes = 20,
142 .cra_name = "sha1",
143 .backend_cra_name = "sha1",
144 }, {
145 .flags = DRBG_HASH | DRBG_STRENGTH256,
146 .statelen = 111, /* 888 bits */
Stephan Mueller541af942014-05-31 15:44:17 +0200147 .blocklen_bytes = 48,
148 .cra_name = "sha384",
149 .backend_cra_name = "sha384",
150 }, {
151 .flags = DRBG_HASH | DRBG_STRENGTH256,
152 .statelen = 111, /* 888 bits */
Stephan Mueller541af942014-05-31 15:44:17 +0200153 .blocklen_bytes = 64,
154 .cra_name = "sha512",
155 .backend_cra_name = "sha512",
156 }, {
157 .flags = DRBG_HASH | DRBG_STRENGTH256,
158 .statelen = 55, /* 440 bits */
Stephan Mueller541af942014-05-31 15:44:17 +0200159 .blocklen_bytes = 32,
160 .cra_name = "sha256",
161 .backend_cra_name = "sha256",
162 },
163#endif /* CONFIG_CRYPTO_DRBG_HASH */
164#ifdef CONFIG_CRYPTO_DRBG_HMAC
165 {
Stephan Mueller5b635e22014-07-06 02:26:37 +0200166 .flags = DRBG_HMAC | DRBG_STRENGTH128,
Stephan Mueller541af942014-05-31 15:44:17 +0200167 .statelen = 20, /* block length of cipher */
Stephan Mueller541af942014-05-31 15:44:17 +0200168 .blocklen_bytes = 20,
169 .cra_name = "hmac_sha1",
170 .backend_cra_name = "hmac(sha1)",
171 }, {
172 .flags = DRBG_HMAC | DRBG_STRENGTH256,
173 .statelen = 48, /* block length of cipher */
Stephan Mueller541af942014-05-31 15:44:17 +0200174 .blocklen_bytes = 48,
175 .cra_name = "hmac_sha384",
176 .backend_cra_name = "hmac(sha384)",
177 }, {
178 .flags = DRBG_HMAC | DRBG_STRENGTH256,
179 .statelen = 64, /* block length of cipher */
Stephan Mueller541af942014-05-31 15:44:17 +0200180 .blocklen_bytes = 64,
181 .cra_name = "hmac_sha512",
182 .backend_cra_name = "hmac(sha512)",
183 }, {
184 .flags = DRBG_HMAC | DRBG_STRENGTH256,
185 .statelen = 32, /* block length of cipher */
Stephan Mueller541af942014-05-31 15:44:17 +0200186 .blocklen_bytes = 32,
187 .cra_name = "hmac_sha256",
188 .backend_cra_name = "hmac(sha256)",
189 },
190#endif /* CONFIG_CRYPTO_DRBG_HMAC */
191};
192
193/******************************************************************
194 * Generic helper functions
195 ******************************************************************/
196
197/*
198 * Return strength of DRBG according to SP800-90A section 8.4
199 *
200 * @flags DRBG flags reference
201 *
202 * Return: normalized strength in *bytes* value or 32 as default
203 * to counter programming errors
204 */
205static inline unsigned short drbg_sec_strength(drbg_flag_t flags)
206{
207 switch (flags & DRBG_STRENGTH_MASK) {
208 case DRBG_STRENGTH128:
209 return 16;
210 case DRBG_STRENGTH192:
211 return 24;
212 case DRBG_STRENGTH256:
213 return 32;
214 default:
215 return 32;
216 }
217}
218
219/*
220 * FIPS 140-2 continuous self test
221 * The test is performed on the result of one round of the output
222 * function. Thus, the function implicitly knows the size of the
223 * buffer.
224 *
225 * The FIPS test can be called in an endless loop until it returns
226 * true. Although the code looks like a potential for a deadlock, it
227 * is not the case, because returning a false cannot mathematically
228 * occur (except once when a reseed took place and the updated state
229 * would is now set up such that the generation of new value returns
230 * an identical one -- this is most unlikely and would happen only once).
231 * Thus, if this function repeatedly returns false and thus would cause
232 * a deadlock, the integrity of the entire kernel is lost.
233 *
234 * @drbg DRBG handle
235 * @buf output buffer of random data to be checked
236 *
237 * return:
238 * true on success
239 * false on error
240 */
241static bool drbg_fips_continuous_test(struct drbg_state *drbg,
242 const unsigned char *buf)
243{
244#ifdef CONFIG_CRYPTO_FIPS
245 int ret = 0;
246 /* skip test if we test the overall system */
247 if (drbg->test_data)
248 return true;
249 /* only perform test in FIPS mode */
250 if (0 == fips_enabled)
251 return true;
252 if (!drbg->fips_primed) {
253 /* Priming of FIPS test */
254 memcpy(drbg->prev, buf, drbg_blocklen(drbg));
255 drbg->fips_primed = true;
256 /* return false due to priming, i.e. another round is needed */
257 return false;
258 }
259 ret = memcmp(drbg->prev, buf, drbg_blocklen(drbg));
260 memcpy(drbg->prev, buf, drbg_blocklen(drbg));
261 /* the test shall pass when the two compared values are not equal */
262 return ret != 0;
263#else
264 return true;
265#endif /* CONFIG_CRYPTO_FIPS */
266}
267
268/*
269 * Convert an integer into a byte representation of this integer.
270 * The byte representation is big-endian
271 *
Stephan Mueller541af942014-05-31 15:44:17 +0200272 * @val value to be converted
Stephan Mueller72f3e002014-08-17 17:37:34 +0200273 * @buf buffer holding the converted integer -- caller must ensure that
274 * buffer size is at least 32 bit
Stephan Mueller541af942014-05-31 15:44:17 +0200275 */
276#if (defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_CTR))
Stephan Mueller72f3e002014-08-17 17:37:34 +0200277static inline void drbg_cpu_to_be32(__u32 val, unsigned char *buf)
Stephan Mueller541af942014-05-31 15:44:17 +0200278{
Stephan Mueller72f3e002014-08-17 17:37:34 +0200279 struct s {
Stephan Mueller7c8ae032014-08-26 09:32:24 +0200280 __be32 conv;
Stephan Mueller72f3e002014-08-17 17:37:34 +0200281 };
282 struct s *conversion = (struct s *) buf;
Stephan Mueller541af942014-05-31 15:44:17 +0200283
Stephan Mueller72f3e002014-08-17 17:37:34 +0200284 conversion->conv = cpu_to_be32(val);
Stephan Mueller541af942014-05-31 15:44:17 +0200285}
Stephan Mueller541af942014-05-31 15:44:17 +0200286#endif /* defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_CTR) */
287
288/******************************************************************
289 * CTR DRBG callback functions
290 ******************************************************************/
291
292#ifdef CONFIG_CRYPTO_DRBG_CTR
Stephan Muellere25e47e2014-07-06 02:23:03 +0200293#define CRYPTO_DRBG_CTR_STRING "CTR "
Stephan Mueller541af942014-05-31 15:44:17 +0200294static int drbg_kcapi_sym(struct drbg_state *drbg, const unsigned char *key,
295 unsigned char *outval, const struct drbg_string *in);
296static int drbg_init_sym_kernel(struct drbg_state *drbg);
297static int drbg_fini_sym_kernel(struct drbg_state *drbg);
298
299/* BCC function for CTR DRBG as defined in 10.4.3 */
300static int drbg_ctr_bcc(struct drbg_state *drbg,
301 unsigned char *out, const unsigned char *key,
Stephan Mueller8c987162014-06-28 21:58:24 +0200302 struct list_head *in)
Stephan Mueller541af942014-05-31 15:44:17 +0200303{
Stephan Mueller8c987162014-06-28 21:58:24 +0200304 int ret = 0;
305 struct drbg_string *curr = NULL;
Stephan Mueller541af942014-05-31 15:44:17 +0200306 struct drbg_string data;
Stephan Mueller8c987162014-06-28 21:58:24 +0200307 short cnt = 0;
Stephan Mueller541af942014-05-31 15:44:17 +0200308
309 drbg_string_fill(&data, out, drbg_blocklen(drbg));
310
311 /* 10.4.3 step 1 */
312 memset(out, 0, drbg_blocklen(drbg));
313
314 /* 10.4.3 step 2 / 4 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200315 list_for_each_entry(curr, in, list) {
316 const unsigned char *pos = curr->buf;
317 size_t len = curr->len;
Stephan Mueller541af942014-05-31 15:44:17 +0200318 /* 10.4.3 step 4.1 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200319 while (len) {
320 /* 10.4.3 step 4.2 */
321 if (drbg_blocklen(drbg) == cnt) {
322 cnt = 0;
323 ret = drbg_kcapi_sym(drbg, key, out, &data);
324 if (ret)
325 return ret;
Stephan Mueller541af942014-05-31 15:44:17 +0200326 }
Stephan Mueller8c987162014-06-28 21:58:24 +0200327 out[cnt] ^= *pos;
328 pos++;
329 cnt++;
330 len--;
Stephan Mueller541af942014-05-31 15:44:17 +0200331 }
Stephan Mueller541af942014-05-31 15:44:17 +0200332 }
Stephan Mueller8c987162014-06-28 21:58:24 +0200333 /* 10.4.3 step 4.2 for last block */
334 if (cnt)
335 ret = drbg_kcapi_sym(drbg, key, out, &data);
336
337 return ret;
Stephan Mueller541af942014-05-31 15:44:17 +0200338}
339
340/*
341 * scratchpad usage: drbg_ctr_update is interlinked with drbg_ctr_df
342 * (and drbg_ctr_bcc, but this function does not need any temporary buffers),
343 * the scratchpad is used as follows:
344 * drbg_ctr_update:
345 * temp
346 * start: drbg->scratchpad
347 * length: drbg_statelen(drbg) + drbg_blocklen(drbg)
348 * note: the cipher writing into this variable works
349 * blocklen-wise. Now, when the statelen is not a multiple
350 * of blocklen, the generateion loop below "spills over"
351 * by at most blocklen. Thus, we need to give sufficient
352 * memory.
353 * df_data
354 * start: drbg->scratchpad +
355 * drbg_statelen(drbg) + drbg_blocklen(drbg)
356 * length: drbg_statelen(drbg)
357 *
358 * drbg_ctr_df:
359 * pad
360 * start: df_data + drbg_statelen(drbg)
361 * length: drbg_blocklen(drbg)
362 * iv
363 * start: pad + drbg_blocklen(drbg)
364 * length: drbg_blocklen(drbg)
365 * temp
366 * start: iv + drbg_blocklen(drbg)
Stephan Mueller8fecaad2014-07-01 17:08:48 +0200367 * length: drbg_satelen(drbg) + drbg_blocklen(drbg)
368 * note: temp is the buffer that the BCC function operates
369 * on. BCC operates blockwise. drbg_statelen(drbg)
370 * is sufficient when the DRBG state length is a multiple
371 * of the block size. For AES192 (and maybe other ciphers)
372 * this is not correct and the length for temp is
373 * insufficient (yes, that also means for such ciphers,
374 * the final output of all BCC rounds are truncated).
375 * Therefore, add drbg_blocklen(drbg) to cover all
376 * possibilities.
Stephan Mueller541af942014-05-31 15:44:17 +0200377 */
378
379/* Derivation Function for CTR DRBG as defined in 10.4.2 */
380static int drbg_ctr_df(struct drbg_state *drbg,
381 unsigned char *df_data, size_t bytes_to_return,
Stephan Mueller8c987162014-06-28 21:58:24 +0200382 struct list_head *seedlist)
Stephan Mueller541af942014-05-31 15:44:17 +0200383{
384 int ret = -EFAULT;
385 unsigned char L_N[8];
386 /* S3 is input */
387 struct drbg_string S1, S2, S4, cipherin;
Stephan Mueller8c987162014-06-28 21:58:24 +0200388 LIST_HEAD(bcc_list);
Stephan Mueller541af942014-05-31 15:44:17 +0200389 unsigned char *pad = df_data + drbg_statelen(drbg);
390 unsigned char *iv = pad + drbg_blocklen(drbg);
391 unsigned char *temp = iv + drbg_blocklen(drbg);
392 size_t padlen = 0;
393 unsigned int templen = 0;
394 /* 10.4.2 step 7 */
395 unsigned int i = 0;
396 /* 10.4.2 step 8 */
397 const unsigned char *K = (unsigned char *)
398 "\x00\x01\x02\x03\x04\x05\x06\x07"
399 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
400 "\x10\x11\x12\x13\x14\x15\x16\x17"
401 "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f";
402 unsigned char *X;
403 size_t generated_len = 0;
404 size_t inputlen = 0;
Stephan Mueller8c987162014-06-28 21:58:24 +0200405 struct drbg_string *seed = NULL;
Stephan Mueller541af942014-05-31 15:44:17 +0200406
407 memset(pad, 0, drbg_blocklen(drbg));
408 memset(iv, 0, drbg_blocklen(drbg));
409 memset(temp, 0, drbg_statelen(drbg));
410
411 /* 10.4.2 step 1 is implicit as we work byte-wise */
412
413 /* 10.4.2 step 2 */
414 if ((512/8) < bytes_to_return)
415 return -EINVAL;
416
417 /* 10.4.2 step 2 -- calculate the entire length of all input data */
Stephan Mueller8c987162014-06-28 21:58:24 +0200418 list_for_each_entry(seed, seedlist, list)
419 inputlen += seed->len;
Stephan Mueller72f3e002014-08-17 17:37:34 +0200420 drbg_cpu_to_be32(inputlen, &L_N[0]);
Stephan Mueller541af942014-05-31 15:44:17 +0200421
422 /* 10.4.2 step 3 */
Stephan Mueller72f3e002014-08-17 17:37:34 +0200423 drbg_cpu_to_be32(bytes_to_return, &L_N[4]);
Stephan Mueller541af942014-05-31 15:44:17 +0200424
425 /* 10.4.2 step 5: length is L_N, input_string, one byte, padding */
426 padlen = (inputlen + sizeof(L_N) + 1) % (drbg_blocklen(drbg));
427 /* wrap the padlen appropriately */
428 if (padlen)
429 padlen = drbg_blocklen(drbg) - padlen;
430 /*
431 * pad / padlen contains the 0x80 byte and the following zero bytes.
432 * As the calculated padlen value only covers the number of zero
433 * bytes, this value has to be incremented by one for the 0x80 byte.
434 */
435 padlen++;
436 pad[0] = 0x80;
437
438 /* 10.4.2 step 4 -- first fill the linked list and then order it */
439 drbg_string_fill(&S1, iv, drbg_blocklen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +0200440 list_add_tail(&S1.list, &bcc_list);
Stephan Mueller541af942014-05-31 15:44:17 +0200441 drbg_string_fill(&S2, L_N, sizeof(L_N));
Stephan Mueller8c987162014-06-28 21:58:24 +0200442 list_add_tail(&S2.list, &bcc_list);
443 list_splice_tail(seedlist, &bcc_list);
Stephan Mueller541af942014-05-31 15:44:17 +0200444 drbg_string_fill(&S4, pad, padlen);
Stephan Mueller8c987162014-06-28 21:58:24 +0200445 list_add_tail(&S4.list, &bcc_list);
Stephan Mueller541af942014-05-31 15:44:17 +0200446
447 /* 10.4.2 step 9 */
448 while (templen < (drbg_keylen(drbg) + (drbg_blocklen(drbg)))) {
449 /*
450 * 10.4.2 step 9.1 - the padding is implicit as the buffer
451 * holds zeros after allocation -- even the increment of i
452 * is irrelevant as the increment remains within length of i
453 */
Stephan Mueller72f3e002014-08-17 17:37:34 +0200454 drbg_cpu_to_be32(i, iv);
Stephan Mueller541af942014-05-31 15:44:17 +0200455 /* 10.4.2 step 9.2 -- BCC and concatenation with temp */
Stephan Mueller8c987162014-06-28 21:58:24 +0200456 ret = drbg_ctr_bcc(drbg, temp + templen, K, &bcc_list);
Stephan Mueller541af942014-05-31 15:44:17 +0200457 if (ret)
458 goto out;
459 /* 10.4.2 step 9.3 */
460 i++;
461 templen += drbg_blocklen(drbg);
462 }
463
464 /* 10.4.2 step 11 */
465 X = temp + (drbg_keylen(drbg));
466 drbg_string_fill(&cipherin, X, drbg_blocklen(drbg));
467
468 /* 10.4.2 step 12: overwriting of outval is implemented in next step */
469
470 /* 10.4.2 step 13 */
471 while (generated_len < bytes_to_return) {
472 short blocklen = 0;
473 /*
474 * 10.4.2 step 13.1: the truncation of the key length is
475 * implicit as the key is only drbg_blocklen in size based on
476 * the implementation of the cipher function callback
477 */
478 ret = drbg_kcapi_sym(drbg, temp, X, &cipherin);
479 if (ret)
480 goto out;
481 blocklen = (drbg_blocklen(drbg) <
482 (bytes_to_return - generated_len)) ?
483 drbg_blocklen(drbg) :
484 (bytes_to_return - generated_len);
485 /* 10.4.2 step 13.2 and 14 */
486 memcpy(df_data + generated_len, X, blocklen);
487 generated_len += blocklen;
488 }
489
490 ret = 0;
491
492out:
493 memset(iv, 0, drbg_blocklen(drbg));
494 memset(temp, 0, drbg_statelen(drbg));
495 memset(pad, 0, drbg_blocklen(drbg));
496 return ret;
497}
498
Stephan Mueller72e7c252014-07-06 02:24:35 +0200499/*
500 * update function of CTR DRBG as defined in 10.2.1.2
501 *
502 * The reseed variable has an enhanced meaning compared to the update
503 * functions of the other DRBGs as follows:
504 * 0 => initial seed from initialization
505 * 1 => reseed via drbg_seed
506 * 2 => first invocation from drbg_ctr_update when addtl is present. In
507 * this case, the df_data scratchpad is not deleted so that it is
508 * available for another calls to prevent calling the DF function
509 * again.
510 * 3 => second invocation from drbg_ctr_update. When the update function
511 * was called with addtl, the df_data memory already contains the
512 * DFed addtl information and we do not need to call DF again.
513 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200514static int drbg_ctr_update(struct drbg_state *drbg, struct list_head *seed,
515 int reseed)
Stephan Mueller541af942014-05-31 15:44:17 +0200516{
517 int ret = -EFAULT;
518 /* 10.2.1.2 step 1 */
519 unsigned char *temp = drbg->scratchpad;
520 unsigned char *df_data = drbg->scratchpad + drbg_statelen(drbg) +
521 drbg_blocklen(drbg);
522 unsigned char *temp_p, *df_data_p; /* pointer to iterate over buffers */
523 unsigned int len = 0;
524 struct drbg_string cipherin;
Stephan Mueller541af942014-05-31 15:44:17 +0200525
526 memset(temp, 0, drbg_statelen(drbg) + drbg_blocklen(drbg));
Stephan Mueller72e7c252014-07-06 02:24:35 +0200527 if (3 > reseed)
528 memset(df_data, 0, drbg_statelen(drbg));
Stephan Mueller541af942014-05-31 15:44:17 +0200529
530 /* 10.2.1.3.2 step 2 and 10.2.1.4.2 step 2 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200531 if (seed) {
532 ret = drbg_ctr_df(drbg, df_data, drbg_statelen(drbg), seed);
Stephan Mueller541af942014-05-31 15:44:17 +0200533 if (ret)
534 goto out;
535 }
536
537 drbg_string_fill(&cipherin, drbg->V, drbg_blocklen(drbg));
538 /*
539 * 10.2.1.3.2 steps 2 and 3 are already covered as the allocation
540 * zeroizes all memory during initialization
541 */
542 while (len < (drbg_statelen(drbg))) {
543 /* 10.2.1.2 step 2.1 */
Stephan Mueller41a84982014-10-14 21:50:13 +0200544 crypto_inc(drbg->V, drbg_blocklen(drbg));
Stephan Mueller541af942014-05-31 15:44:17 +0200545 /*
546 * 10.2.1.2 step 2.2 */
547 ret = drbg_kcapi_sym(drbg, drbg->C, temp + len, &cipherin);
548 if (ret)
549 goto out;
550 /* 10.2.1.2 step 2.3 and 3 */
551 len += drbg_blocklen(drbg);
552 }
553
554 /* 10.2.1.2 step 4 */
555 temp_p = temp;
556 df_data_p = df_data;
557 for (len = 0; len < drbg_statelen(drbg); len++) {
558 *temp_p ^= *df_data_p;
559 df_data_p++; temp_p++;
560 }
561
562 /* 10.2.1.2 step 5 */
563 memcpy(drbg->C, temp, drbg_keylen(drbg));
564 /* 10.2.1.2 step 6 */
565 memcpy(drbg->V, temp + drbg_keylen(drbg), drbg_blocklen(drbg));
566 ret = 0;
567
568out:
569 memset(temp, 0, drbg_statelen(drbg) + drbg_blocklen(drbg));
Stephan Mueller72e7c252014-07-06 02:24:35 +0200570 if (2 != reseed)
571 memset(df_data, 0, drbg_statelen(drbg));
Stephan Mueller541af942014-05-31 15:44:17 +0200572 return ret;
573}
574
575/*
576 * scratchpad use: drbg_ctr_update is called independently from
577 * drbg_ctr_extract_bytes. Therefore, the scratchpad is reused
578 */
579/* Generate function of CTR DRBG as defined in 10.2.1.5.2 */
580static int drbg_ctr_generate(struct drbg_state *drbg,
581 unsigned char *buf, unsigned int buflen,
Stephan Mueller27e4de22014-07-06 02:25:36 +0200582 struct list_head *addtl)
Stephan Mueller541af942014-05-31 15:44:17 +0200583{
584 int len = 0;
585 int ret = 0;
586 struct drbg_string data;
Stephan Mueller541af942014-05-31 15:44:17 +0200587
588 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
589
590 /* 10.2.1.5.2 step 2 */
Stephan Mueller27e4de22014-07-06 02:25:36 +0200591 if (addtl && !list_empty(addtl)) {
592 ret = drbg_ctr_update(drbg, addtl, 2);
Stephan Mueller541af942014-05-31 15:44:17 +0200593 if (ret)
594 return 0;
595 }
596
597 /* 10.2.1.5.2 step 4.1 */
Stephan Mueller41a84982014-10-14 21:50:13 +0200598 crypto_inc(drbg->V, drbg_blocklen(drbg));
Stephan Mueller541af942014-05-31 15:44:17 +0200599 drbg_string_fill(&data, drbg->V, drbg_blocklen(drbg));
600 while (len < buflen) {
601 int outlen = 0;
602 /* 10.2.1.5.2 step 4.2 */
603 ret = drbg_kcapi_sym(drbg, drbg->C, drbg->scratchpad, &data);
604 if (ret) {
605 len = ret;
606 goto out;
607 }
608 outlen = (drbg_blocklen(drbg) < (buflen - len)) ?
609 drbg_blocklen(drbg) : (buflen - len);
610 if (!drbg_fips_continuous_test(drbg, drbg->scratchpad)) {
611 /* 10.2.1.5.2 step 6 */
Stephan Mueller41a84982014-10-14 21:50:13 +0200612 crypto_inc(drbg->V, drbg_blocklen(drbg));
Stephan Mueller541af942014-05-31 15:44:17 +0200613 continue;
614 }
615 /* 10.2.1.5.2 step 4.3 */
616 memcpy(buf + len, drbg->scratchpad, outlen);
617 len += outlen;
618 /* 10.2.1.5.2 step 6 */
619 if (len < buflen)
Stephan Mueller41a84982014-10-14 21:50:13 +0200620 crypto_inc(drbg->V, drbg_blocklen(drbg));
Stephan Mueller541af942014-05-31 15:44:17 +0200621 }
622
Stephan Mueller72e7c252014-07-06 02:24:35 +0200623 /* 10.2.1.5.2 step 6 */
624 ret = drbg_ctr_update(drbg, NULL, 3);
Stephan Mueller541af942014-05-31 15:44:17 +0200625 if (ret)
626 len = ret;
627
628out:
629 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
630 return len;
631}
632
633static struct drbg_state_ops drbg_ctr_ops = {
634 .update = drbg_ctr_update,
635 .generate = drbg_ctr_generate,
636 .crypto_init = drbg_init_sym_kernel,
637 .crypto_fini = drbg_fini_sym_kernel,
638};
639#endif /* CONFIG_CRYPTO_DRBG_CTR */
640
641/******************************************************************
642 * HMAC DRBG callback functions
643 ******************************************************************/
644
645#if defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_HMAC)
646static int drbg_kcapi_hash(struct drbg_state *drbg, const unsigned char *key,
Stephan Mueller8c987162014-06-28 21:58:24 +0200647 unsigned char *outval, const struct list_head *in);
Stephan Mueller541af942014-05-31 15:44:17 +0200648static int drbg_init_hash_kernel(struct drbg_state *drbg);
649static int drbg_fini_hash_kernel(struct drbg_state *drbg);
650#endif /* (CONFIG_CRYPTO_DRBG_HASH || CONFIG_CRYPTO_DRBG_HMAC) */
651
652#ifdef CONFIG_CRYPTO_DRBG_HMAC
Stephan Muellere25e47e2014-07-06 02:23:03 +0200653#define CRYPTO_DRBG_HMAC_STRING "HMAC "
Stephan Mueller541af942014-05-31 15:44:17 +0200654/* update function of HMAC DRBG as defined in 10.1.2.2 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200655static int drbg_hmac_update(struct drbg_state *drbg, struct list_head *seed,
656 int reseed)
Stephan Mueller541af942014-05-31 15:44:17 +0200657{
658 int ret = -EFAULT;
659 int i = 0;
Stephan Mueller8c987162014-06-28 21:58:24 +0200660 struct drbg_string seed1, seed2, vdata;
661 LIST_HEAD(seedlist);
662 LIST_HEAD(vdatalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200663
Stephan Muellerf072f0e2014-08-17 17:38:58 +0200664 if (!reseed)
665 /* 10.1.2.3 step 2 -- memset(0) of C is implicit with kzalloc */
Stephan Mueller541af942014-05-31 15:44:17 +0200666 memset(drbg->V, 1, drbg_statelen(drbg));
Stephan Mueller541af942014-05-31 15:44:17 +0200667
668 drbg_string_fill(&seed1, drbg->V, drbg_statelen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +0200669 list_add_tail(&seed1.list, &seedlist);
Stephan Mueller541af942014-05-31 15:44:17 +0200670 /* buffer of seed2 will be filled in for loop below with one byte */
671 drbg_string_fill(&seed2, NULL, 1);
Stephan Mueller8c987162014-06-28 21:58:24 +0200672 list_add_tail(&seed2.list, &seedlist);
Stephan Mueller541af942014-05-31 15:44:17 +0200673 /* input data of seed is allowed to be NULL at this point */
Stephan Mueller8c987162014-06-28 21:58:24 +0200674 if (seed)
675 list_splice_tail(seed, &seedlist);
Stephan Mueller541af942014-05-31 15:44:17 +0200676
Stephan Mueller8c987162014-06-28 21:58:24 +0200677 drbg_string_fill(&vdata, drbg->V, drbg_statelen(drbg));
678 list_add_tail(&vdata.list, &vdatalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200679 for (i = 2; 0 < i; i--) {
680 /* first round uses 0x0, second 0x1 */
681 unsigned char prefix = DRBG_PREFIX0;
682 if (1 == i)
683 prefix = DRBG_PREFIX1;
684 /* 10.1.2.2 step 1 and 4 -- concatenation and HMAC for key */
685 seed2.buf = &prefix;
Stephan Mueller8c987162014-06-28 21:58:24 +0200686 ret = drbg_kcapi_hash(drbg, drbg->C, drbg->C, &seedlist);
Stephan Mueller541af942014-05-31 15:44:17 +0200687 if (ret)
688 return ret;
689
690 /* 10.1.2.2 step 2 and 5 -- HMAC for V */
Stephan Mueller8c987162014-06-28 21:58:24 +0200691 ret = drbg_kcapi_hash(drbg, drbg->C, drbg->V, &vdatalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200692 if (ret)
693 return ret;
694
695 /* 10.1.2.2 step 3 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200696 if (!seed)
Stephan Mueller541af942014-05-31 15:44:17 +0200697 return ret;
698 }
699
700 return 0;
701}
702
703/* generate function of HMAC DRBG as defined in 10.1.2.5 */
704static int drbg_hmac_generate(struct drbg_state *drbg,
705 unsigned char *buf,
706 unsigned int buflen,
Stephan Mueller27e4de22014-07-06 02:25:36 +0200707 struct list_head *addtl)
Stephan Mueller541af942014-05-31 15:44:17 +0200708{
709 int len = 0;
710 int ret = 0;
711 struct drbg_string data;
Stephan Mueller8c987162014-06-28 21:58:24 +0200712 LIST_HEAD(datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200713
714 /* 10.1.2.5 step 2 */
Stephan Mueller27e4de22014-07-06 02:25:36 +0200715 if (addtl && !list_empty(addtl)) {
716 ret = drbg_hmac_update(drbg, addtl, 1);
Stephan Mueller541af942014-05-31 15:44:17 +0200717 if (ret)
718 return ret;
719 }
720
721 drbg_string_fill(&data, drbg->V, drbg_statelen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +0200722 list_add_tail(&data.list, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200723 while (len < buflen) {
724 unsigned int outlen = 0;
725 /* 10.1.2.5 step 4.1 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200726 ret = drbg_kcapi_hash(drbg, drbg->C, drbg->V, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200727 if (ret)
728 return ret;
729 outlen = (drbg_blocklen(drbg) < (buflen - len)) ?
730 drbg_blocklen(drbg) : (buflen - len);
731 if (!drbg_fips_continuous_test(drbg, drbg->V))
732 continue;
733
734 /* 10.1.2.5 step 4.2 */
735 memcpy(buf + len, drbg->V, outlen);
736 len += outlen;
737 }
738
739 /* 10.1.2.5 step 6 */
Stephan Mueller27e4de22014-07-06 02:25:36 +0200740 if (addtl && !list_empty(addtl))
741 ret = drbg_hmac_update(drbg, addtl, 1);
742 else
Stephan Mueller8c987162014-06-28 21:58:24 +0200743 ret = drbg_hmac_update(drbg, NULL, 1);
Stephan Mueller541af942014-05-31 15:44:17 +0200744 if (ret)
745 return ret;
746
747 return len;
748}
749
750static struct drbg_state_ops drbg_hmac_ops = {
751 .update = drbg_hmac_update,
752 .generate = drbg_hmac_generate,
753 .crypto_init = drbg_init_hash_kernel,
754 .crypto_fini = drbg_fini_hash_kernel,
755
756};
757#endif /* CONFIG_CRYPTO_DRBG_HMAC */
758
759/******************************************************************
760 * Hash DRBG callback functions
761 ******************************************************************/
762
763#ifdef CONFIG_CRYPTO_DRBG_HASH
Stephan Muellere25e47e2014-07-06 02:23:03 +0200764#define CRYPTO_DRBG_HASH_STRING "HASH "
Stephan Mueller541af942014-05-31 15:44:17 +0200765/*
Stephan Mueller41a84982014-10-14 21:50:13 +0200766 * Increment buffer
767 *
768 * @dst buffer to increment
769 * @add value to add
770 */
771static inline void drbg_add_buf(unsigned char *dst, size_t dstlen,
772 const unsigned char *add, size_t addlen)
773{
774 /* implied: dstlen > addlen */
775 unsigned char *dstptr;
776 const unsigned char *addptr;
777 unsigned int remainder = 0;
778 size_t len = addlen;
779
780 dstptr = dst + (dstlen-1);
781 addptr = add + (addlen-1);
782 while (len) {
783 remainder += *dstptr + *addptr;
784 *dstptr = remainder & 0xff;
785 remainder >>= 8;
786 len--; dstptr--; addptr--;
787 }
788 len = dstlen - addlen;
789 while (len && remainder > 0) {
790 remainder = *dstptr + 1;
791 *dstptr = remainder & 0xff;
792 remainder >>= 8;
793 len--; dstptr--;
794 }
795}
796
797/*
Stephan Mueller541af942014-05-31 15:44:17 +0200798 * scratchpad usage: as drbg_hash_update and drbg_hash_df are used
799 * interlinked, the scratchpad is used as follows:
800 * drbg_hash_update
801 * start: drbg->scratchpad
802 * length: drbg_statelen(drbg)
803 * drbg_hash_df:
804 * start: drbg->scratchpad + drbg_statelen(drbg)
805 * length: drbg_blocklen(drbg)
806 *
807 * drbg_hash_process_addtl uses the scratchpad, but fully completes
808 * before either of the functions mentioned before are invoked. Therefore,
809 * drbg_hash_process_addtl does not need to be specifically considered.
810 */
811
812/* Derivation Function for Hash DRBG as defined in 10.4.1 */
813static int drbg_hash_df(struct drbg_state *drbg,
814 unsigned char *outval, size_t outlen,
Stephan Mueller8c987162014-06-28 21:58:24 +0200815 struct list_head *entropylist)
Stephan Mueller541af942014-05-31 15:44:17 +0200816{
817 int ret = 0;
818 size_t len = 0;
819 unsigned char input[5];
820 unsigned char *tmp = drbg->scratchpad + drbg_statelen(drbg);
Stephan Mueller8c987162014-06-28 21:58:24 +0200821 struct drbg_string data;
Stephan Mueller541af942014-05-31 15:44:17 +0200822
823 memset(tmp, 0, drbg_blocklen(drbg));
824
825 /* 10.4.1 step 3 */
826 input[0] = 1;
Stephan Mueller72f3e002014-08-17 17:37:34 +0200827 drbg_cpu_to_be32((outlen * 8), &input[1]);
Stephan Mueller541af942014-05-31 15:44:17 +0200828
829 /* 10.4.1 step 4.1 -- concatenation of data for input into hash */
Stephan Mueller8c987162014-06-28 21:58:24 +0200830 drbg_string_fill(&data, input, 5);
831 list_add(&data.list, entropylist);
Stephan Mueller541af942014-05-31 15:44:17 +0200832
833 /* 10.4.1 step 4 */
834 while (len < outlen) {
835 short blocklen = 0;
836 /* 10.4.1 step 4.1 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200837 ret = drbg_kcapi_hash(drbg, NULL, tmp, entropylist);
Stephan Mueller541af942014-05-31 15:44:17 +0200838 if (ret)
839 goto out;
840 /* 10.4.1 step 4.2 */
841 input[0]++;
842 blocklen = (drbg_blocklen(drbg) < (outlen - len)) ?
843 drbg_blocklen(drbg) : (outlen - len);
844 memcpy(outval + len, tmp, blocklen);
845 len += blocklen;
846 }
847
848out:
849 memset(tmp, 0, drbg_blocklen(drbg));
850 return ret;
851}
852
853/* update function for Hash DRBG as defined in 10.1.1.2 / 10.1.1.3 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200854static int drbg_hash_update(struct drbg_state *drbg, struct list_head *seed,
Stephan Mueller541af942014-05-31 15:44:17 +0200855 int reseed)
856{
857 int ret = 0;
858 struct drbg_string data1, data2;
Stephan Mueller8c987162014-06-28 21:58:24 +0200859 LIST_HEAD(datalist);
860 LIST_HEAD(datalist2);
Stephan Mueller541af942014-05-31 15:44:17 +0200861 unsigned char *V = drbg->scratchpad;
862 unsigned char prefix = DRBG_PREFIX1;
863
864 memset(drbg->scratchpad, 0, drbg_statelen(drbg));
865 if (!seed)
866 return -EINVAL;
867
868 if (reseed) {
869 /* 10.1.1.3 step 1 */
870 memcpy(V, drbg->V, drbg_statelen(drbg));
871 drbg_string_fill(&data1, &prefix, 1);
Stephan Mueller8c987162014-06-28 21:58:24 +0200872 list_add_tail(&data1.list, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200873 drbg_string_fill(&data2, V, drbg_statelen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +0200874 list_add_tail(&data2.list, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200875 }
Stephan Mueller8c987162014-06-28 21:58:24 +0200876 list_splice_tail(seed, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200877
878 /* 10.1.1.2 / 10.1.1.3 step 2 and 3 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200879 ret = drbg_hash_df(drbg, drbg->V, drbg_statelen(drbg), &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200880 if (ret)
881 goto out;
882
883 /* 10.1.1.2 / 10.1.1.3 step 4 */
884 prefix = DRBG_PREFIX0;
885 drbg_string_fill(&data1, &prefix, 1);
Stephan Mueller8c987162014-06-28 21:58:24 +0200886 list_add_tail(&data1.list, &datalist2);
Stephan Mueller541af942014-05-31 15:44:17 +0200887 drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +0200888 list_add_tail(&data2.list, &datalist2);
Stephan Mueller541af942014-05-31 15:44:17 +0200889 /* 10.1.1.2 / 10.1.1.3 step 4 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200890 ret = drbg_hash_df(drbg, drbg->C, drbg_statelen(drbg), &datalist2);
Stephan Mueller541af942014-05-31 15:44:17 +0200891
892out:
893 memset(drbg->scratchpad, 0, drbg_statelen(drbg));
894 return ret;
895}
896
897/* processing of additional information string for Hash DRBG */
898static int drbg_hash_process_addtl(struct drbg_state *drbg,
Stephan Mueller27e4de22014-07-06 02:25:36 +0200899 struct list_head *addtl)
Stephan Mueller541af942014-05-31 15:44:17 +0200900{
901 int ret = 0;
902 struct drbg_string data1, data2;
Stephan Mueller8c987162014-06-28 21:58:24 +0200903 LIST_HEAD(datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200904 unsigned char prefix = DRBG_PREFIX2;
905
906 /* this is value w as per documentation */
907 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
908
909 /* 10.1.1.4 step 2 */
Stephan Mueller27e4de22014-07-06 02:25:36 +0200910 if (!addtl || list_empty(addtl))
Stephan Mueller541af942014-05-31 15:44:17 +0200911 return 0;
912
913 /* 10.1.1.4 step 2a */
914 drbg_string_fill(&data1, &prefix, 1);
915 drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +0200916 list_add_tail(&data1.list, &datalist);
917 list_add_tail(&data2.list, &datalist);
Stephan Mueller27e4de22014-07-06 02:25:36 +0200918 list_splice_tail(addtl, &datalist);
Stephan Mueller8c987162014-06-28 21:58:24 +0200919 ret = drbg_kcapi_hash(drbg, NULL, drbg->scratchpad, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200920 if (ret)
921 goto out;
922
923 /* 10.1.1.4 step 2b */
924 drbg_add_buf(drbg->V, drbg_statelen(drbg),
925 drbg->scratchpad, drbg_blocklen(drbg));
926
927out:
928 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
929 return ret;
930}
931
932/* Hashgen defined in 10.1.1.4 */
933static int drbg_hash_hashgen(struct drbg_state *drbg,
934 unsigned char *buf,
935 unsigned int buflen)
936{
937 int len = 0;
938 int ret = 0;
939 unsigned char *src = drbg->scratchpad;
940 unsigned char *dst = drbg->scratchpad + drbg_statelen(drbg);
941 struct drbg_string data;
Stephan Mueller8c987162014-06-28 21:58:24 +0200942 LIST_HEAD(datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200943
944 memset(src, 0, drbg_statelen(drbg));
945 memset(dst, 0, drbg_blocklen(drbg));
946
947 /* 10.1.1.4 step hashgen 2 */
948 memcpy(src, drbg->V, drbg_statelen(drbg));
949
950 drbg_string_fill(&data, src, drbg_statelen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +0200951 list_add_tail(&data.list, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200952 while (len < buflen) {
953 unsigned int outlen = 0;
954 /* 10.1.1.4 step hashgen 4.1 */
Stephan Mueller8c987162014-06-28 21:58:24 +0200955 ret = drbg_kcapi_hash(drbg, NULL, dst, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200956 if (ret) {
957 len = ret;
958 goto out;
959 }
960 outlen = (drbg_blocklen(drbg) < (buflen - len)) ?
961 drbg_blocklen(drbg) : (buflen - len);
962 if (!drbg_fips_continuous_test(drbg, dst)) {
Stephan Mueller41a84982014-10-14 21:50:13 +0200963 crypto_inc(src, drbg_statelen(drbg));
Stephan Mueller541af942014-05-31 15:44:17 +0200964 continue;
965 }
966 /* 10.1.1.4 step hashgen 4.2 */
967 memcpy(buf + len, dst, outlen);
968 len += outlen;
969 /* 10.1.1.4 hashgen step 4.3 */
970 if (len < buflen)
Stephan Mueller41a84982014-10-14 21:50:13 +0200971 crypto_inc(src, drbg_statelen(drbg));
Stephan Mueller541af942014-05-31 15:44:17 +0200972 }
973
974out:
975 memset(drbg->scratchpad, 0,
976 (drbg_statelen(drbg) + drbg_blocklen(drbg)));
977 return len;
978}
979
980/* generate function for Hash DRBG as defined in 10.1.1.4 */
981static int drbg_hash_generate(struct drbg_state *drbg,
982 unsigned char *buf, unsigned int buflen,
Stephan Mueller27e4de22014-07-06 02:25:36 +0200983 struct list_head *addtl)
Stephan Mueller541af942014-05-31 15:44:17 +0200984{
985 int len = 0;
986 int ret = 0;
Stephan Mueller72f3e002014-08-17 17:37:34 +0200987 union {
988 unsigned char req[8];
Stephan Mueller7c8ae032014-08-26 09:32:24 +0200989 __be64 req_int;
Stephan Mueller72f3e002014-08-17 17:37:34 +0200990 } u;
Stephan Mueller541af942014-05-31 15:44:17 +0200991 unsigned char prefix = DRBG_PREFIX3;
992 struct drbg_string data1, data2;
Stephan Mueller8c987162014-06-28 21:58:24 +0200993 LIST_HEAD(datalist);
Stephan Mueller541af942014-05-31 15:44:17 +0200994
995 /* 10.1.1.4 step 2 */
996 ret = drbg_hash_process_addtl(drbg, addtl);
997 if (ret)
998 return ret;
999 /* 10.1.1.4 step 3 */
1000 len = drbg_hash_hashgen(drbg, buf, buflen);
1001
1002 /* this is the value H as documented in 10.1.1.4 */
1003 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
1004 /* 10.1.1.4 step 4 */
1005 drbg_string_fill(&data1, &prefix, 1);
Stephan Mueller8c987162014-06-28 21:58:24 +02001006 list_add_tail(&data1.list, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +02001007 drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
Stephan Mueller8c987162014-06-28 21:58:24 +02001008 list_add_tail(&data2.list, &datalist);
1009 ret = drbg_kcapi_hash(drbg, NULL, drbg->scratchpad, &datalist);
Stephan Mueller541af942014-05-31 15:44:17 +02001010 if (ret) {
1011 len = ret;
1012 goto out;
1013 }
1014
1015 /* 10.1.1.4 step 5 */
1016 drbg_add_buf(drbg->V, drbg_statelen(drbg),
1017 drbg->scratchpad, drbg_blocklen(drbg));
1018 drbg_add_buf(drbg->V, drbg_statelen(drbg),
1019 drbg->C, drbg_statelen(drbg));
Stephan Mueller72f3e002014-08-17 17:37:34 +02001020 u.req_int = cpu_to_be64(drbg->reseed_ctr);
1021 drbg_add_buf(drbg->V, drbg_statelen(drbg), u.req, 8);
Stephan Mueller541af942014-05-31 15:44:17 +02001022
1023out:
1024 memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
1025 return len;
1026}
1027
1028/*
1029 * scratchpad usage: as update and generate are used isolated, both
1030 * can use the scratchpad
1031 */
1032static struct drbg_state_ops drbg_hash_ops = {
1033 .update = drbg_hash_update,
1034 .generate = drbg_hash_generate,
1035 .crypto_init = drbg_init_hash_kernel,
1036 .crypto_fini = drbg_fini_hash_kernel,
1037};
1038#endif /* CONFIG_CRYPTO_DRBG_HASH */
1039
1040/******************************************************************
1041 * Functions common for DRBG implementations
1042 ******************************************************************/
1043
1044/*
1045 * Seeding or reseeding of the DRBG
1046 *
1047 * @drbg: DRBG state struct
1048 * @pers: personalization / additional information buffer
1049 * @reseed: 0 for initial seed process, 1 for reseeding
1050 *
1051 * return:
1052 * 0 on success
1053 * error value otherwise
1054 */
1055static int drbg_seed(struct drbg_state *drbg, struct drbg_string *pers,
1056 bool reseed)
1057{
1058 int ret = 0;
1059 unsigned char *entropy = NULL;
1060 size_t entropylen = 0;
1061 struct drbg_string data1;
Stephan Mueller8c987162014-06-28 21:58:24 +02001062 LIST_HEAD(seedlist);
Stephan Mueller541af942014-05-31 15:44:17 +02001063
1064 /* 9.1 / 9.2 / 9.3.1 step 3 */
1065 if (pers && pers->len > (drbg_max_addtl(drbg))) {
Stephan Muellera9089572014-07-06 02:24:03 +02001066 pr_devel("DRBG: personalization string too long %zu\n",
Stephan Mueller541af942014-05-31 15:44:17 +02001067 pers->len);
1068 return -EINVAL;
1069 }
1070
1071 if (drbg->test_data && drbg->test_data->testentropy) {
1072 drbg_string_fill(&data1, drbg->test_data->testentropy->buf,
1073 drbg->test_data->testentropy->len);
1074 pr_devel("DRBG: using test entropy\n");
1075 } else {
1076 /*
1077 * Gather entropy equal to the security strength of the DRBG.
1078 * With a derivation function, a nonce is required in addition
1079 * to the entropy. A nonce must be at least 1/2 of the security
1080 * strength of the DRBG in size. Thus, entropy * nonce is 3/2
1081 * of the strength. The consideration of a nonce is only
1082 * applicable during initial seeding.
1083 */
1084 entropylen = drbg_sec_strength(drbg->core->flags);
1085 if (!entropylen)
1086 return -EFAULT;
1087 if (!reseed)
1088 entropylen = ((entropylen + 1) / 2) * 3;
1089 pr_devel("DRBG: (re)seeding with %zu bytes of entropy\n",
1090 entropylen);
1091 entropy = kzalloc(entropylen, GFP_KERNEL);
1092 if (!entropy)
1093 return -ENOMEM;
1094 get_random_bytes(entropy, entropylen);
1095 drbg_string_fill(&data1, entropy, entropylen);
1096 }
Stephan Mueller8c987162014-06-28 21:58:24 +02001097 list_add_tail(&data1.list, &seedlist);
Stephan Mueller541af942014-05-31 15:44:17 +02001098
1099 /*
1100 * concatenation of entropy with personalization str / addtl input)
1101 * the variable pers is directly handed in by the caller, so check its
1102 * contents whether it is appropriate
1103 */
Stephan Mueller8c987162014-06-28 21:58:24 +02001104 if (pers && pers->buf && 0 < pers->len) {
1105 list_add_tail(&pers->list, &seedlist);
Stephan Mueller541af942014-05-31 15:44:17 +02001106 pr_devel("DRBG: using personalization string\n");
1107 }
1108
Stephan Muellere6c02442014-08-17 17:39:31 +02001109 if (!reseed) {
1110 memset(drbg->V, 0, drbg_statelen(drbg));
1111 memset(drbg->C, 0, drbg_statelen(drbg));
1112 }
1113
Stephan Mueller8c987162014-06-28 21:58:24 +02001114 ret = drbg->d_ops->update(drbg, &seedlist, reseed);
Stephan Mueller541af942014-05-31 15:44:17 +02001115 if (ret)
1116 goto out;
1117
1118 drbg->seeded = true;
1119 /* 10.1.1.2 / 10.1.1.3 step 5 */
1120 drbg->reseed_ctr = 1;
1121
1122out:
Stephan Mueller46f64f62014-08-17 17:37:59 +02001123 kzfree(entropy);
Stephan Mueller541af942014-05-31 15:44:17 +02001124 return ret;
1125}
1126
1127/* Free all substructures in a DRBG state without the DRBG state structure */
1128static inline void drbg_dealloc_state(struct drbg_state *drbg)
1129{
1130 if (!drbg)
1131 return;
Stephan Mueller46f64f62014-08-17 17:37:59 +02001132 kzfree(drbg->V);
Stephan Mueller541af942014-05-31 15:44:17 +02001133 drbg->V = NULL;
Stephan Mueller46f64f62014-08-17 17:37:59 +02001134 kzfree(drbg->C);
Stephan Mueller541af942014-05-31 15:44:17 +02001135 drbg->C = NULL;
Stephan Mueller46f64f62014-08-17 17:37:59 +02001136 kzfree(drbg->scratchpad);
Stephan Mueller541af942014-05-31 15:44:17 +02001137 drbg->scratchpad = NULL;
1138 drbg->reseed_ctr = 0;
1139#ifdef CONFIG_CRYPTO_FIPS
Stephan Mueller46f64f62014-08-17 17:37:59 +02001140 kzfree(drbg->prev);
Stephan Mueller541af942014-05-31 15:44:17 +02001141 drbg->prev = NULL;
1142 drbg->fips_primed = false;
1143#endif
1144}
1145
1146/*
1147 * Allocate all sub-structures for a DRBG state.
1148 * The DRBG state structure must already be allocated.
1149 */
1150static inline int drbg_alloc_state(struct drbg_state *drbg)
1151{
1152 int ret = -ENOMEM;
1153 unsigned int sb_size = 0;
1154
Stephan Muellere6c02442014-08-17 17:39:31 +02001155 drbg->V = kmalloc(drbg_statelen(drbg), GFP_KERNEL);
Stephan Mueller541af942014-05-31 15:44:17 +02001156 if (!drbg->V)
1157 goto err;
Stephan Muellere6c02442014-08-17 17:39:31 +02001158 drbg->C = kmalloc(drbg_statelen(drbg), GFP_KERNEL);
Stephan Mueller541af942014-05-31 15:44:17 +02001159 if (!drbg->C)
1160 goto err;
1161#ifdef CONFIG_CRYPTO_FIPS
Stephan Muellere6c02442014-08-17 17:39:31 +02001162 drbg->prev = kmalloc(drbg_blocklen(drbg), GFP_KERNEL);
Stephan Mueller541af942014-05-31 15:44:17 +02001163 if (!drbg->prev)
1164 goto err;
1165 drbg->fips_primed = false;
1166#endif
1167 /* scratchpad is only generated for CTR and Hash */
1168 if (drbg->core->flags & DRBG_HMAC)
1169 sb_size = 0;
1170 else if (drbg->core->flags & DRBG_CTR)
1171 sb_size = drbg_statelen(drbg) + drbg_blocklen(drbg) + /* temp */
1172 drbg_statelen(drbg) + /* df_data */
1173 drbg_blocklen(drbg) + /* pad */
1174 drbg_blocklen(drbg) + /* iv */
Stephan Mueller8fecaad2014-07-01 17:08:48 +02001175 drbg_statelen(drbg) + drbg_blocklen(drbg); /* temp */
Stephan Mueller541af942014-05-31 15:44:17 +02001176 else
1177 sb_size = drbg_statelen(drbg) + drbg_blocklen(drbg);
1178
1179 if (0 < sb_size) {
1180 drbg->scratchpad = kzalloc(sb_size, GFP_KERNEL);
1181 if (!drbg->scratchpad)
1182 goto err;
1183 }
1184 spin_lock_init(&drbg->drbg_lock);
1185 return 0;
1186
1187err:
1188 drbg_dealloc_state(drbg);
1189 return ret;
1190}
1191
1192/*
1193 * Strategy to avoid holding long term locks: generate a shadow copy of DRBG
1194 * and perform all operations on this shadow copy. After finishing, restore
1195 * the updated state of the shadow copy into original drbg state. This way,
1196 * only the read and write operations of the original drbg state must be
1197 * locked
1198 */
1199static inline void drbg_copy_drbg(struct drbg_state *src,
1200 struct drbg_state *dst)
1201{
1202 if (!src || !dst)
1203 return;
1204 memcpy(dst->V, src->V, drbg_statelen(src));
1205 memcpy(dst->C, src->C, drbg_statelen(src));
1206 dst->reseed_ctr = src->reseed_ctr;
1207 dst->seeded = src->seeded;
1208 dst->pr = src->pr;
1209#ifdef CONFIG_CRYPTO_FIPS
1210 dst->fips_primed = src->fips_primed;
1211 memcpy(dst->prev, src->prev, drbg_blocklen(src));
1212#endif
1213 /*
1214 * Not copied:
1215 * scratchpad is initialized drbg_alloc_state;
1216 * priv_data is initialized with call to crypto_init;
1217 * d_ops and core are set outside, as these parameters are const;
1218 * test_data is set outside to prevent it being copied back.
1219 */
1220}
1221
1222static int drbg_make_shadow(struct drbg_state *drbg, struct drbg_state **shadow)
1223{
1224 int ret = -ENOMEM;
1225 struct drbg_state *tmp = NULL;
1226
Stephan Mueller541af942014-05-31 15:44:17 +02001227 tmp = kzalloc(sizeof(struct drbg_state), GFP_KERNEL);
1228 if (!tmp)
1229 return -ENOMEM;
1230
1231 /* read-only data as they are defined as const, no lock needed */
1232 tmp->core = drbg->core;
1233 tmp->d_ops = drbg->d_ops;
1234
1235 ret = drbg_alloc_state(tmp);
1236 if (ret)
1237 goto err;
1238
1239 spin_lock_bh(&drbg->drbg_lock);
1240 drbg_copy_drbg(drbg, tmp);
1241 /* only make a link to the test buffer, as we only read that data */
1242 tmp->test_data = drbg->test_data;
1243 spin_unlock_bh(&drbg->drbg_lock);
1244 *shadow = tmp;
1245 return 0;
1246
1247err:
Stephan Mueller46f64f62014-08-17 17:37:59 +02001248 kzfree(tmp);
Stephan Mueller541af942014-05-31 15:44:17 +02001249 return ret;
1250}
1251
1252static void drbg_restore_shadow(struct drbg_state *drbg,
1253 struct drbg_state **shadow)
1254{
1255 struct drbg_state *tmp = *shadow;
1256
1257 spin_lock_bh(&drbg->drbg_lock);
1258 drbg_copy_drbg(tmp, drbg);
1259 spin_unlock_bh(&drbg->drbg_lock);
1260 drbg_dealloc_state(tmp);
1261 kzfree(tmp);
1262 *shadow = NULL;
1263}
1264
1265/*************************************************************************
1266 * DRBG interface functions
1267 *************************************************************************/
1268
1269/*
1270 * DRBG generate function as required by SP800-90A - this function
1271 * generates random numbers
1272 *
1273 * @drbg DRBG state handle
1274 * @buf Buffer where to store the random numbers -- the buffer must already
1275 * be pre-allocated by caller
1276 * @buflen Length of output buffer - this value defines the number of random
1277 * bytes pulled from DRBG
1278 * @addtl Additional input that is mixed into state, may be NULL -- note
1279 * the entropy is pulled by the DRBG internally unconditionally
1280 * as defined in SP800-90A. The additional input is mixed into
1281 * the state in addition to the pulled entropy.
1282 *
1283 * return: generated number of bytes
1284 */
1285static int drbg_generate(struct drbg_state *drbg,
1286 unsigned char *buf, unsigned int buflen,
1287 struct drbg_string *addtl)
1288{
1289 int len = 0;
1290 struct drbg_state *shadow = NULL;
Stephan Mueller27e4de22014-07-06 02:25:36 +02001291 LIST_HEAD(addtllist);
1292 struct drbg_string timestamp;
1293 union {
1294 cycles_t cycles;
1295 unsigned char char_cycles[sizeof(cycles_t)];
1296 } now;
Stephan Mueller541af942014-05-31 15:44:17 +02001297
1298 if (0 == buflen || !buf) {
1299 pr_devel("DRBG: no output buffer provided\n");
1300 return -EINVAL;
1301 }
1302 if (addtl && NULL == addtl->buf && 0 < addtl->len) {
1303 pr_devel("DRBG: wrong format of additional information\n");
1304 return -EINVAL;
1305 }
1306
1307 len = drbg_make_shadow(drbg, &shadow);
1308 if (len) {
1309 pr_devel("DRBG: shadow copy cannot be generated\n");
1310 return len;
1311 }
1312
1313 /* 9.3.1 step 2 */
1314 len = -EINVAL;
1315 if (buflen > (drbg_max_request_bytes(shadow))) {
1316 pr_devel("DRBG: requested random numbers too large %u\n",
1317 buflen);
1318 goto err;
1319 }
1320
1321 /* 9.3.1 step 3 is implicit with the chosen DRBG */
1322
1323 /* 9.3.1 step 4 */
1324 if (addtl && addtl->len > (drbg_max_addtl(shadow))) {
1325 pr_devel("DRBG: additional information string too long %zu\n",
1326 addtl->len);
1327 goto err;
1328 }
1329 /* 9.3.1 step 5 is implicit with the chosen DRBG */
1330
1331 /*
1332 * 9.3.1 step 6 and 9 supplemented by 9.3.2 step c is implemented
1333 * here. The spec is a bit convoluted here, we make it simpler.
1334 */
1335 if ((drbg_max_requests(shadow)) < shadow->reseed_ctr)
1336 shadow->seeded = false;
1337
1338 /* allocate cipher handle */
Stephan Mueller45943a52014-08-17 17:38:29 +02001339 len = shadow->d_ops->crypto_init(shadow);
1340 if (len)
1341 goto err;
Stephan Mueller541af942014-05-31 15:44:17 +02001342
1343 if (shadow->pr || !shadow->seeded) {
1344 pr_devel("DRBG: reseeding before generation (prediction "
1345 "resistance: %s, state %s)\n",
1346 drbg->pr ? "true" : "false",
1347 drbg->seeded ? "seeded" : "unseeded");
1348 /* 9.3.1 steps 7.1 through 7.3 */
1349 len = drbg_seed(shadow, addtl, true);
1350 if (len)
1351 goto err;
1352 /* 9.3.1 step 7.4 */
1353 addtl = NULL;
1354 }
Stephan Mueller27e4de22014-07-06 02:25:36 +02001355
1356 /*
1357 * Mix the time stamp into the DRBG state if the DRBG is not in
1358 * test mode. If there are two callers invoking the DRBG at the same
1359 * time, i.e. before the first caller merges its shadow state back,
1360 * both callers would obtain the same random number stream without
1361 * changing the state here.
1362 */
1363 if (!drbg->test_data) {
1364 now.cycles = random_get_entropy();
1365 drbg_string_fill(&timestamp, now.char_cycles, sizeof(cycles_t));
1366 list_add_tail(&timestamp.list, &addtllist);
1367 }
1368 if (addtl && 0 < addtl->len)
1369 list_add_tail(&addtl->list, &addtllist);
Stephan Mueller541af942014-05-31 15:44:17 +02001370 /* 9.3.1 step 8 and 10 */
Stephan Mueller27e4de22014-07-06 02:25:36 +02001371 len = shadow->d_ops->generate(shadow, buf, buflen, &addtllist);
Stephan Mueller541af942014-05-31 15:44:17 +02001372
1373 /* 10.1.1.4 step 6, 10.1.2.5 step 7, 10.2.1.5.2 step 7 */
1374 shadow->reseed_ctr++;
1375 if (0 >= len)
1376 goto err;
1377
1378 /*
1379 * Section 11.3.3 requires to re-perform self tests after some
1380 * generated random numbers. The chosen value after which self
1381 * test is performed is arbitrary, but it should be reasonable.
1382 * However, we do not perform the self tests because of the following
1383 * reasons: it is mathematically impossible that the initial self tests
1384 * were successfully and the following are not. If the initial would
1385 * pass and the following would not, the kernel integrity is violated.
1386 * In this case, the entire kernel operation is questionable and it
1387 * is unlikely that the integrity violation only affects the
1388 * correct operation of the DRBG.
1389 *
1390 * Albeit the following code is commented out, it is provided in
1391 * case somebody has a need to implement the test of 11.3.3.
1392 */
1393#if 0
1394 if (shadow->reseed_ctr && !(shadow->reseed_ctr % 4096)) {
1395 int err = 0;
1396 pr_devel("DRBG: start to perform self test\n");
1397 if (drbg->core->flags & DRBG_HMAC)
1398 err = alg_test("drbg_pr_hmac_sha256",
1399 "drbg_pr_hmac_sha256", 0, 0);
1400 else if (drbg->core->flags & DRBG_CTR)
1401 err = alg_test("drbg_pr_ctr_aes128",
1402 "drbg_pr_ctr_aes128", 0, 0);
1403 else
1404 err = alg_test("drbg_pr_sha256",
1405 "drbg_pr_sha256", 0, 0);
1406 if (err) {
1407 pr_err("DRBG: periodical self test failed\n");
1408 /*
1409 * uninstantiate implies that from now on, only errors
1410 * are returned when reusing this DRBG cipher handle
1411 */
1412 drbg_uninstantiate(drbg);
1413 drbg_dealloc_state(shadow);
1414 kzfree(shadow);
1415 return 0;
1416 } else {
1417 pr_devel("DRBG: self test successful\n");
1418 }
1419 }
1420#endif
1421
1422err:
Stephan Mueller45943a52014-08-17 17:38:29 +02001423 shadow->d_ops->crypto_fini(shadow);
Stephan Mueller541af942014-05-31 15:44:17 +02001424 drbg_restore_shadow(drbg, &shadow);
1425 return len;
1426}
1427
1428/*
1429 * Wrapper around drbg_generate which can pull arbitrary long strings
1430 * from the DRBG without hitting the maximum request limitation.
1431 *
1432 * Parameters: see drbg_generate
1433 * Return codes: see drbg_generate -- if one drbg_generate request fails,
1434 * the entire drbg_generate_long request fails
1435 */
1436static int drbg_generate_long(struct drbg_state *drbg,
1437 unsigned char *buf, unsigned int buflen,
1438 struct drbg_string *addtl)
1439{
1440 int len = 0;
1441 unsigned int slice = 0;
1442 do {
1443 int tmplen = 0;
1444 unsigned int chunk = 0;
1445 slice = ((buflen - len) / drbg_max_request_bytes(drbg));
1446 chunk = slice ? drbg_max_request_bytes(drbg) : (buflen - len);
1447 tmplen = drbg_generate(drbg, buf + len, chunk, addtl);
1448 if (0 >= tmplen)
1449 return tmplen;
1450 len += tmplen;
Stephan Muellerce5481d2014-07-31 21:47:33 +02001451 } while (slice > 0 && (len < buflen));
Stephan Mueller541af942014-05-31 15:44:17 +02001452 return len;
1453}
1454
1455/*
1456 * DRBG instantiation function as required by SP800-90A - this function
1457 * sets up the DRBG handle, performs the initial seeding and all sanity
1458 * checks required by SP800-90A
1459 *
1460 * @drbg memory of state -- if NULL, new memory is allocated
1461 * @pers Personalization string that is mixed into state, may be NULL -- note
1462 * the entropy is pulled by the DRBG internally unconditionally
1463 * as defined in SP800-90A. The additional input is mixed into
1464 * the state in addition to the pulled entropy.
1465 * @coreref reference to core
1466 * @pr prediction resistance enabled
1467 *
1468 * return
1469 * 0 on success
1470 * error value otherwise
1471 */
1472static int drbg_instantiate(struct drbg_state *drbg, struct drbg_string *pers,
1473 int coreref, bool pr)
1474{
1475 int ret = -ENOMEM;
1476
1477 pr_devel("DRBG: Initializing DRBG core %d with prediction resistance "
1478 "%s\n", coreref, pr ? "enabled" : "disabled");
1479 drbg->core = &drbg_cores[coreref];
1480 drbg->pr = pr;
1481 drbg->seeded = false;
1482 switch (drbg->core->flags & DRBG_TYPE_MASK) {
1483#ifdef CONFIG_CRYPTO_DRBG_HMAC
1484 case DRBG_HMAC:
1485 drbg->d_ops = &drbg_hmac_ops;
1486 break;
1487#endif /* CONFIG_CRYPTO_DRBG_HMAC */
1488#ifdef CONFIG_CRYPTO_DRBG_HASH
1489 case DRBG_HASH:
1490 drbg->d_ops = &drbg_hash_ops;
1491 break;
1492#endif /* CONFIG_CRYPTO_DRBG_HASH */
1493#ifdef CONFIG_CRYPTO_DRBG_CTR
1494 case DRBG_CTR:
1495 drbg->d_ops = &drbg_ctr_ops;
1496 break;
1497#endif /* CONFIG_CRYPTO_DRBG_CTR */
1498 default:
1499 return -EOPNOTSUPP;
1500 }
1501
1502 /* 9.1 step 1 is implicit with the selected DRBG type */
1503
1504 /*
1505 * 9.1 step 2 is implicit as caller can select prediction resistance
1506 * and the flag is copied into drbg->flags --
1507 * all DRBG types support prediction resistance
1508 */
1509
1510 /* 9.1 step 4 is implicit in drbg_sec_strength */
1511
1512 ret = drbg_alloc_state(drbg);
1513 if (ret)
1514 return ret;
1515
1516 ret = -EFAULT;
Stephan Mueller45943a52014-08-17 17:38:29 +02001517 if (drbg->d_ops->crypto_init(drbg))
Stephan Mueller541af942014-05-31 15:44:17 +02001518 goto err;
1519 ret = drbg_seed(drbg, pers, false);
Stephan Mueller45943a52014-08-17 17:38:29 +02001520 drbg->d_ops->crypto_fini(drbg);
Stephan Mueller541af942014-05-31 15:44:17 +02001521 if (ret)
1522 goto err;
1523
1524 return 0;
1525
1526err:
1527 drbg_dealloc_state(drbg);
1528 return ret;
1529}
1530
1531/*
1532 * DRBG uninstantiate function as required by SP800-90A - this function
1533 * frees all buffers and the DRBG handle
1534 *
1535 * @drbg DRBG state handle
1536 *
1537 * return
1538 * 0 on success
1539 */
1540static int drbg_uninstantiate(struct drbg_state *drbg)
1541{
1542 spin_lock_bh(&drbg->drbg_lock);
1543 drbg_dealloc_state(drbg);
1544 /* no scrubbing of test_data -- this shall survive an uninstantiate */
1545 spin_unlock_bh(&drbg->drbg_lock);
1546 return 0;
1547}
1548
1549/*
1550 * Helper function for setting the test data in the DRBG
1551 *
1552 * @drbg DRBG state handle
1553 * @test_data test data to sets
1554 */
1555static inline void drbg_set_testdata(struct drbg_state *drbg,
1556 struct drbg_test_data *test_data)
1557{
1558 if (!test_data || !test_data->testentropy)
1559 return;
1560 spin_lock_bh(&drbg->drbg_lock);
1561 drbg->test_data = test_data;
1562 spin_unlock_bh(&drbg->drbg_lock);
1563}
1564
1565/***************************************************************
1566 * Kernel crypto API cipher invocations requested by DRBG
1567 ***************************************************************/
1568
1569#if defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_HMAC)
1570struct sdesc {
1571 struct shash_desc shash;
1572 char ctx[];
1573};
1574
1575static int drbg_init_hash_kernel(struct drbg_state *drbg)
1576{
1577 struct sdesc *sdesc;
1578 struct crypto_shash *tfm;
1579
1580 tfm = crypto_alloc_shash(drbg->core->backend_cra_name, 0, 0);
1581 if (IS_ERR(tfm)) {
1582 pr_info("DRBG: could not allocate digest TFM handle\n");
1583 return PTR_ERR(tfm);
1584 }
1585 BUG_ON(drbg_blocklen(drbg) != crypto_shash_digestsize(tfm));
1586 sdesc = kzalloc(sizeof(struct shash_desc) + crypto_shash_descsize(tfm),
1587 GFP_KERNEL);
1588 if (!sdesc) {
1589 crypto_free_shash(tfm);
1590 return -ENOMEM;
1591 }
1592
1593 sdesc->shash.tfm = tfm;
1594 sdesc->shash.flags = 0;
1595 drbg->priv_data = sdesc;
1596 return 0;
1597}
1598
1599static int drbg_fini_hash_kernel(struct drbg_state *drbg)
1600{
1601 struct sdesc *sdesc = (struct sdesc *)drbg->priv_data;
1602 if (sdesc) {
1603 crypto_free_shash(sdesc->shash.tfm);
1604 kzfree(sdesc);
1605 }
1606 drbg->priv_data = NULL;
1607 return 0;
1608}
1609
1610static int drbg_kcapi_hash(struct drbg_state *drbg, const unsigned char *key,
Stephan Mueller8c987162014-06-28 21:58:24 +02001611 unsigned char *outval, const struct list_head *in)
Stephan Mueller541af942014-05-31 15:44:17 +02001612{
1613 struct sdesc *sdesc = (struct sdesc *)drbg->priv_data;
Stephan Mueller8c987162014-06-28 21:58:24 +02001614 struct drbg_string *input = NULL;
Stephan Mueller541af942014-05-31 15:44:17 +02001615
1616 if (key)
1617 crypto_shash_setkey(sdesc->shash.tfm, key, drbg_statelen(drbg));
1618 crypto_shash_init(&sdesc->shash);
Stephan Mueller8c987162014-06-28 21:58:24 +02001619 list_for_each_entry(input, in, list)
1620 crypto_shash_update(&sdesc->shash, input->buf, input->len);
Stephan Mueller541af942014-05-31 15:44:17 +02001621 return crypto_shash_final(&sdesc->shash, outval);
1622}
1623#endif /* (CONFIG_CRYPTO_DRBG_HASH || CONFIG_CRYPTO_DRBG_HMAC) */
1624
1625#ifdef CONFIG_CRYPTO_DRBG_CTR
1626static int drbg_init_sym_kernel(struct drbg_state *drbg)
1627{
1628 int ret = 0;
1629 struct crypto_blkcipher *tfm;
1630
1631 tfm = crypto_alloc_blkcipher(drbg->core->backend_cra_name, 0, 0);
1632 if (IS_ERR(tfm)) {
1633 pr_info("DRBG: could not allocate cipher TFM handle\n");
1634 return PTR_ERR(tfm);
1635 }
1636 BUG_ON(drbg_blocklen(drbg) != crypto_blkcipher_blocksize(tfm));
1637 drbg->priv_data = tfm;
1638 return ret;
1639}
1640
1641static int drbg_fini_sym_kernel(struct drbg_state *drbg)
1642{
1643 struct crypto_blkcipher *tfm =
1644 (struct crypto_blkcipher *)drbg->priv_data;
1645 if (tfm)
1646 crypto_free_blkcipher(tfm);
1647 drbg->priv_data = NULL;
1648 return 0;
1649}
1650
1651static int drbg_kcapi_sym(struct drbg_state *drbg, const unsigned char *key,
1652 unsigned char *outval, const struct drbg_string *in)
1653{
1654 int ret = 0;
1655 struct scatterlist sg_in, sg_out;
1656 struct blkcipher_desc desc;
1657 struct crypto_blkcipher *tfm =
1658 (struct crypto_blkcipher *)drbg->priv_data;
1659
1660 desc.tfm = tfm;
1661 desc.flags = 0;
1662 crypto_blkcipher_setkey(tfm, key, (drbg_keylen(drbg)));
1663 /* there is only component in *in */
1664 sg_init_one(&sg_in, in->buf, in->len);
1665 sg_init_one(&sg_out, outval, drbg_blocklen(drbg));
1666 ret = crypto_blkcipher_encrypt(&desc, &sg_out, &sg_in, in->len);
1667
1668 return ret;
1669}
1670#endif /* CONFIG_CRYPTO_DRBG_CTR */
1671
1672/***************************************************************
1673 * Kernel crypto API interface to register DRBG
1674 ***************************************************************/
1675
1676/*
1677 * Look up the DRBG flags by given kernel crypto API cra_name
1678 * The code uses the drbg_cores definition to do this
1679 *
1680 * @cra_name kernel crypto API cra_name
1681 * @coreref reference to integer which is filled with the pointer to
1682 * the applicable core
1683 * @pr reference for setting prediction resistance
1684 *
1685 * return: flags
1686 */
1687static inline void drbg_convert_tfm_core(const char *cra_driver_name,
1688 int *coreref, bool *pr)
1689{
1690 int i = 0;
1691 size_t start = 0;
1692 int len = 0;
1693
1694 *pr = true;
1695 /* disassemble the names */
1696 if (!memcmp(cra_driver_name, "drbg_nopr_", 10)) {
1697 start = 10;
1698 *pr = false;
1699 } else if (!memcmp(cra_driver_name, "drbg_pr_", 8)) {
1700 start = 8;
1701 } else {
1702 return;
1703 }
1704
1705 /* remove the first part */
1706 len = strlen(cra_driver_name) - start;
1707 for (i = 0; ARRAY_SIZE(drbg_cores) > i; i++) {
1708 if (!memcmp(cra_driver_name + start, drbg_cores[i].cra_name,
1709 len)) {
1710 *coreref = i;
1711 return;
1712 }
1713 }
1714}
1715
1716static int drbg_kcapi_init(struct crypto_tfm *tfm)
1717{
1718 struct drbg_state *drbg = crypto_tfm_ctx(tfm);
1719 bool pr = false;
1720 int coreref = 0;
1721
Stephan Mueller4f150712014-07-06 02:25:04 +02001722 drbg_convert_tfm_core(crypto_tfm_alg_driver_name(tfm), &coreref, &pr);
Stephan Mueller541af942014-05-31 15:44:17 +02001723 /*
1724 * when personalization string is needed, the caller must call reset
1725 * and provide the personalization string as seed information
1726 */
1727 return drbg_instantiate(drbg, NULL, coreref, pr);
1728}
1729
1730static void drbg_kcapi_cleanup(struct crypto_tfm *tfm)
1731{
1732 drbg_uninstantiate(crypto_tfm_ctx(tfm));
1733}
1734
1735/*
1736 * Generate random numbers invoked by the kernel crypto API:
1737 * The API of the kernel crypto API is extended as follows:
1738 *
1739 * If dlen is larger than zero, rdata is interpreted as the output buffer
1740 * where random data is to be stored.
1741 *
1742 * If dlen is zero, rdata is interpreted as a pointer to a struct drbg_gen
1743 * which holds the additional information string that is used for the
1744 * DRBG generation process. The output buffer that is to be used to store
1745 * data is also pointed to by struct drbg_gen.
1746 */
1747static int drbg_kcapi_random(struct crypto_rng *tfm, u8 *rdata,
1748 unsigned int dlen)
1749{
1750 struct drbg_state *drbg = crypto_rng_ctx(tfm);
1751 if (0 < dlen) {
1752 return drbg_generate_long(drbg, rdata, dlen, NULL);
1753 } else {
1754 struct drbg_gen *data = (struct drbg_gen *)rdata;
Stephan Mueller8c987162014-06-28 21:58:24 +02001755 struct drbg_string addtl;
Stephan Mueller541af942014-05-31 15:44:17 +02001756 /* catch NULL pointer */
1757 if (!data)
1758 return 0;
1759 drbg_set_testdata(drbg, data->test_data);
Stephan Mueller8c987162014-06-28 21:58:24 +02001760 /* linked list variable is now local to allow modification */
1761 drbg_string_fill(&addtl, data->addtl->buf, data->addtl->len);
Stephan Mueller541af942014-05-31 15:44:17 +02001762 return drbg_generate_long(drbg, data->outbuf, data->outlen,
Stephan Mueller8c987162014-06-28 21:58:24 +02001763 &addtl);
Stephan Mueller541af942014-05-31 15:44:17 +02001764 }
1765}
1766
1767/*
1768 * Reset the DRBG invoked by the kernel crypto API
1769 * The reset implies a full re-initialization of the DRBG. Similar to the
1770 * generate function of drbg_kcapi_random, this function extends the
1771 * kernel crypto API interface with struct drbg_gen
1772 */
1773static int drbg_kcapi_reset(struct crypto_rng *tfm, u8 *seed, unsigned int slen)
1774{
1775 struct drbg_state *drbg = crypto_rng_ctx(tfm);
1776 struct crypto_tfm *tfm_base = crypto_rng_tfm(tfm);
1777 bool pr = false;
1778 struct drbg_string seed_string;
1779 int coreref = 0;
1780
1781 drbg_uninstantiate(drbg);
1782 drbg_convert_tfm_core(crypto_tfm_alg_driver_name(tfm_base), &coreref,
1783 &pr);
1784 if (0 < slen) {
1785 drbg_string_fill(&seed_string, seed, slen);
1786 return drbg_instantiate(drbg, &seed_string, coreref, pr);
1787 } else {
1788 struct drbg_gen *data = (struct drbg_gen *)seed;
1789 /* allow invocation of API call with NULL, 0 */
1790 if (!data)
1791 return drbg_instantiate(drbg, NULL, coreref, pr);
1792 drbg_set_testdata(drbg, data->test_data);
Stephan Mueller8c987162014-06-28 21:58:24 +02001793 /* linked list variable is now local to allow modification */
1794 drbg_string_fill(&seed_string, data->addtl->buf,
1795 data->addtl->len);
1796 return drbg_instantiate(drbg, &seed_string, coreref, pr);
Stephan Mueller541af942014-05-31 15:44:17 +02001797 }
1798}
1799
1800/***************************************************************
1801 * Kernel module: code to load the module
1802 ***************************************************************/
1803
1804/*
1805 * Tests as defined in 11.3.2 in addition to the cipher tests: testing
1806 * of the error handling.
1807 *
1808 * Note: testing of failing seed source as defined in 11.3.2 is not applicable
1809 * as seed source of get_random_bytes does not fail.
1810 *
1811 * Note 2: There is no sensible way of testing the reseed counter
1812 * enforcement, so skip it.
1813 */
1814static inline int __init drbg_healthcheck_sanity(void)
1815{
1816#ifdef CONFIG_CRYPTO_FIPS
1817 int len = 0;
1818#define OUTBUFLEN 16
1819 unsigned char buf[OUTBUFLEN];
1820 struct drbg_state *drbg = NULL;
1821 int ret = -EFAULT;
1822 int rc = -EFAULT;
1823 bool pr = false;
1824 int coreref = 0;
1825 struct drbg_string addtl;
1826 size_t max_addtllen, max_request_bytes;
1827
1828 /* only perform test in FIPS mode */
1829 if (!fips_enabled)
1830 return 0;
1831
1832#ifdef CONFIG_CRYPTO_DRBG_CTR
1833 drbg_convert_tfm_core("drbg_nopr_ctr_aes128", &coreref, &pr);
Stephan Muellere25e47e2014-07-06 02:23:03 +02001834#elif defined CONFIG_CRYPTO_DRBG_HASH
Stephan Mueller541af942014-05-31 15:44:17 +02001835 drbg_convert_tfm_core("drbg_nopr_sha256", &coreref, &pr);
1836#else
1837 drbg_convert_tfm_core("drbg_nopr_hmac_sha256", &coreref, &pr);
1838#endif
1839
1840 drbg = kzalloc(sizeof(struct drbg_state), GFP_KERNEL);
1841 if (!drbg)
1842 return -ENOMEM;
1843
1844 /*
1845 * if the following tests fail, it is likely that there is a buffer
1846 * overflow as buf is much smaller than the requested or provided
1847 * string lengths -- in case the error handling does not succeed
1848 * we may get an OOPS. And we want to get an OOPS as this is a
1849 * grave bug.
1850 */
1851
1852 /* get a valid instance of DRBG for following tests */
1853 ret = drbg_instantiate(drbg, NULL, coreref, pr);
1854 if (ret) {
1855 rc = ret;
1856 goto outbuf;
1857 }
1858 max_addtllen = drbg_max_addtl(drbg);
1859 max_request_bytes = drbg_max_request_bytes(drbg);
1860 drbg_string_fill(&addtl, buf, max_addtllen + 1);
1861 /* overflow addtllen with additonal info string */
1862 len = drbg_generate(drbg, buf, OUTBUFLEN, &addtl);
1863 BUG_ON(0 < len);
1864 /* overflow max_bits */
1865 len = drbg_generate(drbg, buf, (max_request_bytes + 1), NULL);
1866 BUG_ON(0 < len);
1867 drbg_uninstantiate(drbg);
1868
1869 /* overflow max addtllen with personalization string */
1870 ret = drbg_instantiate(drbg, &addtl, coreref, pr);
1871 BUG_ON(0 == ret);
Stephan Mueller541af942014-05-31 15:44:17 +02001872 /* all tests passed */
1873 rc = 0;
1874
1875 pr_devel("DRBG: Sanity tests for failure code paths successfully "
1876 "completed\n");
1877
1878 drbg_uninstantiate(drbg);
1879outbuf:
1880 kzfree(drbg);
1881 return rc;
1882#else /* CONFIG_CRYPTO_FIPS */
1883 return 0;
1884#endif /* CONFIG_CRYPTO_FIPS */
1885}
1886
1887static struct crypto_alg drbg_algs[22];
1888
1889/*
1890 * Fill the array drbg_algs used to register the different DRBGs
1891 * with the kernel crypto API. To fill the array, the information
1892 * from drbg_cores[] is used.
1893 */
1894static inline void __init drbg_fill_array(struct crypto_alg *alg,
1895 const struct drbg_core *core, int pr)
1896{
1897 int pos = 0;
1898 static int priority = 100;
1899
1900 memset(alg, 0, sizeof(struct crypto_alg));
1901 memcpy(alg->cra_name, "stdrng", 6);
1902 if (pr) {
1903 memcpy(alg->cra_driver_name, "drbg_pr_", 8);
1904 pos = 8;
1905 } else {
1906 memcpy(alg->cra_driver_name, "drbg_nopr_", 10);
1907 pos = 10;
1908 }
1909 memcpy(alg->cra_driver_name + pos, core->cra_name,
1910 strlen(core->cra_name));
1911
1912 alg->cra_priority = priority;
1913 priority++;
1914 /*
1915 * If FIPS mode enabled, the selected DRBG shall have the
1916 * highest cra_priority over other stdrng instances to ensure
1917 * it is selected.
1918 */
1919 if (fips_enabled)
1920 alg->cra_priority += 200;
1921
1922 alg->cra_flags = CRYPTO_ALG_TYPE_RNG;
1923 alg->cra_ctxsize = sizeof(struct drbg_state);
1924 alg->cra_type = &crypto_rng_type;
1925 alg->cra_module = THIS_MODULE;
1926 alg->cra_init = drbg_kcapi_init;
1927 alg->cra_exit = drbg_kcapi_cleanup;
1928 alg->cra_u.rng.rng_make_random = drbg_kcapi_random;
1929 alg->cra_u.rng.rng_reset = drbg_kcapi_reset;
1930 alg->cra_u.rng.seedsize = 0;
1931}
1932
1933static int __init drbg_init(void)
1934{
1935 unsigned int i = 0; /* pointer to drbg_algs */
1936 unsigned int j = 0; /* pointer to drbg_cores */
1937 int ret = -EFAULT;
1938
1939 ret = drbg_healthcheck_sanity();
1940 if (ret)
1941 return ret;
1942
1943 if (ARRAY_SIZE(drbg_cores) * 2 > ARRAY_SIZE(drbg_algs)) {
1944 pr_info("DRBG: Cannot register all DRBG types"
Stephan Muellera9089572014-07-06 02:24:03 +02001945 "(slots needed: %zu, slots available: %zu)\n",
Stephan Mueller541af942014-05-31 15:44:17 +02001946 ARRAY_SIZE(drbg_cores) * 2, ARRAY_SIZE(drbg_algs));
1947 return ret;
1948 }
1949
1950 /*
1951 * each DRBG definition can be used with PR and without PR, thus
1952 * we instantiate each DRBG in drbg_cores[] twice.
1953 *
1954 * As the order of placing them into the drbg_algs array matters
1955 * (the later DRBGs receive a higher cra_priority) we register the
1956 * prediction resistance DRBGs first as the should not be too
1957 * interesting.
1958 */
1959 for (j = 0; ARRAY_SIZE(drbg_cores) > j; j++, i++)
1960 drbg_fill_array(&drbg_algs[i], &drbg_cores[j], 1);
1961 for (j = 0; ARRAY_SIZE(drbg_cores) > j; j++, i++)
1962 drbg_fill_array(&drbg_algs[i], &drbg_cores[j], 0);
1963 return crypto_register_algs(drbg_algs, (ARRAY_SIZE(drbg_cores) * 2));
1964}
1965
Fengguang Wu96956ae2014-07-10 16:52:04 +08001966static void __exit drbg_exit(void)
Stephan Mueller541af942014-05-31 15:44:17 +02001967{
1968 crypto_unregister_algs(drbg_algs, (ARRAY_SIZE(drbg_cores) * 2));
1969}
1970
1971module_init(drbg_init);
1972module_exit(drbg_exit);
Stephan Muellere25e47e2014-07-06 02:23:03 +02001973#ifndef CRYPTO_DRBG_HASH_STRING
1974#define CRYPTO_DRBG_HASH_STRING ""
1975#endif
1976#ifndef CRYPTO_DRBG_HMAC_STRING
1977#define CRYPTO_DRBG_HMAC_STRING ""
1978#endif
1979#ifndef CRYPTO_DRBG_CTR_STRING
1980#define CRYPTO_DRBG_CTR_STRING ""
1981#endif
Stephan Mueller541af942014-05-31 15:44:17 +02001982MODULE_LICENSE("GPL");
1983MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
Stephan Muellere25e47e2014-07-06 02:23:03 +02001984MODULE_DESCRIPTION("NIST SP800-90A Deterministic Random Bit Generator (DRBG) "
1985 "using following cores: "
1986 CRYPTO_DRBG_HASH_STRING
1987 CRYPTO_DRBG_HMAC_STRING
1988 CRYPTO_DRBG_CTR_STRING);