Blame view

crypto/drbg.c 59.7 KB
541af946f   Stephan Mueller   crypto: drbg - SP...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  /*
   * DRBG: Deterministic Random Bits Generator
   *       Based on NIST Recommended DRBG from NIST SP800-90A with the following
   *       properties:
   *		* CTR DRBG with DF with AES-128, AES-192, AES-256 cores
   *		* Hash DRBG with DF with SHA-1, SHA-256, SHA-384, SHA-512 cores
   *		* HMAC DRBG with DF with SHA-1, SHA-256, SHA-384, SHA-512 cores
   *		* with and without prediction resistance
   *
   * Copyright Stephan Mueller <smueller@chronox.de>, 2014
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   * 1. Redistributions of source code must retain the above copyright
   *    notice, and the entire permission notice in its entirety,
   *    including the disclaimer of warranties.
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in the
   *    documentation and/or other materials provided with the distribution.
   * 3. The name of the author may not be used to endorse or promote
   *    products derived from this software without specific prior
   *    written permission.
   *
   * ALTERNATIVELY, this product may be distributed under the terms of
   * the GNU General Public License, in which case the provisions of the GPL are
   * required INSTEAD OF the above restrictions.  (This clause is
   * necessary due to a potential bad interaction between the GPL and
   * the restrictions contained in a BSD-style copyright.)
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
   * WHICH ARE HEREBY DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE
   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
   * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
   * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
   * DAMAGE.
   *
   * DRBG Usage
   * ==========
   * The SP 800-90A DRBG allows the user to specify a personalization string
   * for initialization as well as an additional information string for each
   * random number request. The following code fragments show how a caller
   * uses the kernel crypto API to use the full functionality of the DRBG.
   *
   * Usage without any additional data
   * ---------------------------------
   * struct crypto_rng *drng;
   * int err;
   * char data[DATALEN];
   *
   * drng = crypto_alloc_rng(drng_name, 0, 0);
   * err = crypto_rng_get_bytes(drng, &data, DATALEN);
   * crypto_free_rng(drng);
   *
   *
   * Usage with personalization string during initialization
   * -------------------------------------------------------
   * struct crypto_rng *drng;
   * int err;
   * char data[DATALEN];
   * struct drbg_string pers;
   * char personalization[11] = "some-string";
   *
   * drbg_string_fill(&pers, personalization, strlen(personalization));
   * drng = crypto_alloc_rng(drng_name, 0, 0);
   * // The reset completely re-initializes the DRBG with the provided
   * // personalization string
   * err = crypto_rng_reset(drng, &personalization, strlen(personalization));
   * err = crypto_rng_get_bytes(drng, &data, DATALEN);
   * crypto_free_rng(drng);
   *
   *
   * Usage with additional information string during random number request
   * ---------------------------------------------------------------------
   * struct crypto_rng *drng;
   * int err;
   * char data[DATALEN];
   * char addtl_string[11] = "some-string";
   * string drbg_string addtl;
   *
   * drbg_string_fill(&addtl, addtl_string, strlen(addtl_string));
   * drng = crypto_alloc_rng(drng_name, 0, 0);
   * // The following call is a wrapper to crypto_rng_get_bytes() and returns
   * // the same error codes.
   * err = crypto_drbg_get_bytes_addtl(drng, &data, DATALEN, &addtl);
   * crypto_free_rng(drng);
   *
   *
   * Usage with personalization and additional information strings
   * -------------------------------------------------------------
   * Just mix both scenarios above.
   */
  
  #include <crypto/drbg.h>
57225e679   Stephan Mueller   crypto: drbg - Us...
101
  #include <linux/kernel.h>
541af946f   Stephan Mueller   crypto: drbg - SP...
102

541af946f   Stephan Mueller   crypto: drbg - SP...
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
  /***************************************************************
   * Backend cipher definitions available to DRBG
   ***************************************************************/
  
  /*
   * The order of the DRBG definitions here matter: every DRBG is registered
   * as stdrng. Each DRBG receives an increasing cra_priority values the later
   * they are defined in this array (see drbg_fill_array).
   *
   * HMAC DRBGs are favored over Hash DRBGs over CTR DRBGs, and
   * the SHA256 / AES 256 over other ciphers. Thus, the favored
   * DRBGs are the latest entries in this array.
   */
  static const struct drbg_core drbg_cores[] = {
  #ifdef CONFIG_CRYPTO_DRBG_CTR
  	{
  		.flags = DRBG_CTR | DRBG_STRENGTH128,
  		.statelen = 32, /* 256 bits as defined in 10.2.1 */
541af946f   Stephan Mueller   crypto: drbg - SP...
121
122
  		.blocklen_bytes = 16,
  		.cra_name = "ctr_aes128",
04bcbfcf7   Stephan Mueller   crypto: drbg - us...
123
  		.backend_cra_name = "aes",
541af946f   Stephan Mueller   crypto: drbg - SP...
124
125
126
  	}, {
  		.flags = DRBG_CTR | DRBG_STRENGTH192,
  		.statelen = 40, /* 320 bits as defined in 10.2.1 */
541af946f   Stephan Mueller   crypto: drbg - SP...
127
128
  		.blocklen_bytes = 16,
  		.cra_name = "ctr_aes192",
04bcbfcf7   Stephan Mueller   crypto: drbg - us...
129
  		.backend_cra_name = "aes",
541af946f   Stephan Mueller   crypto: drbg - SP...
130
131
132
  	}, {
  		.flags = DRBG_CTR | DRBG_STRENGTH256,
  		.statelen = 48, /* 384 bits as defined in 10.2.1 */
541af946f   Stephan Mueller   crypto: drbg - SP...
133
134
  		.blocklen_bytes = 16,
  		.cra_name = "ctr_aes256",
04bcbfcf7   Stephan Mueller   crypto: drbg - us...
135
  		.backend_cra_name = "aes",
541af946f   Stephan Mueller   crypto: drbg - SP...
136
137
138
139
140
141
  	},
  #endif /* CONFIG_CRYPTO_DRBG_CTR */
  #ifdef CONFIG_CRYPTO_DRBG_HASH
  	{
  		.flags = DRBG_HASH | DRBG_STRENGTH128,
  		.statelen = 55, /* 440 bits */
541af946f   Stephan Mueller   crypto: drbg - SP...
142
143
144
145
146
147
  		.blocklen_bytes = 20,
  		.cra_name = "sha1",
  		.backend_cra_name = "sha1",
  	}, {
  		.flags = DRBG_HASH | DRBG_STRENGTH256,
  		.statelen = 111, /* 888 bits */
541af946f   Stephan Mueller   crypto: drbg - SP...
148
149
150
151
152
153
  		.blocklen_bytes = 48,
  		.cra_name = "sha384",
  		.backend_cra_name = "sha384",
  	}, {
  		.flags = DRBG_HASH | DRBG_STRENGTH256,
  		.statelen = 111, /* 888 bits */
541af946f   Stephan Mueller   crypto: drbg - SP...
154
155
156
157
158
159
  		.blocklen_bytes = 64,
  		.cra_name = "sha512",
  		.backend_cra_name = "sha512",
  	}, {
  		.flags = DRBG_HASH | DRBG_STRENGTH256,
  		.statelen = 55, /* 440 bits */
541af946f   Stephan Mueller   crypto: drbg - SP...
160
161
162
163
164
165
166
  		.blocklen_bytes = 32,
  		.cra_name = "sha256",
  		.backend_cra_name = "sha256",
  	},
  #endif /* CONFIG_CRYPTO_DRBG_HASH */
  #ifdef CONFIG_CRYPTO_DRBG_HMAC
  	{
5b635e280   Stephan Mueller   crypto: drbg - HM...
167
  		.flags = DRBG_HMAC | DRBG_STRENGTH128,
541af946f   Stephan Mueller   crypto: drbg - SP...
168
  		.statelen = 20, /* block length of cipher */
541af946f   Stephan Mueller   crypto: drbg - SP...
169
170
171
172
173
174
  		.blocklen_bytes = 20,
  		.cra_name = "hmac_sha1",
  		.backend_cra_name = "hmac(sha1)",
  	}, {
  		.flags = DRBG_HMAC | DRBG_STRENGTH256,
  		.statelen = 48, /* block length of cipher */
541af946f   Stephan Mueller   crypto: drbg - SP...
175
176
177
178
179
180
  		.blocklen_bytes = 48,
  		.cra_name = "hmac_sha384",
  		.backend_cra_name = "hmac(sha384)",
  	}, {
  		.flags = DRBG_HMAC | DRBG_STRENGTH256,
  		.statelen = 64, /* block length of cipher */
541af946f   Stephan Mueller   crypto: drbg - SP...
181
182
183
184
185
186
  		.blocklen_bytes = 64,
  		.cra_name = "hmac_sha512",
  		.backend_cra_name = "hmac(sha512)",
  	}, {
  		.flags = DRBG_HMAC | DRBG_STRENGTH256,
  		.statelen = 32, /* block length of cipher */
541af946f   Stephan Mueller   crypto: drbg - SP...
187
188
189
190
191
192
  		.blocklen_bytes = 32,
  		.cra_name = "hmac_sha256",
  		.backend_cra_name = "hmac(sha256)",
  	},
  #endif /* CONFIG_CRYPTO_DRBG_HMAC */
  };
57225e679   Stephan Mueller   crypto: drbg - Us...
193
  static int drbg_uninstantiate(struct drbg_state *drbg);
541af946f   Stephan Mueller   crypto: drbg - SP...
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
  /******************************************************************
   * Generic helper functions
   ******************************************************************/
  
  /*
   * Return strength of DRBG according to SP800-90A section 8.4
   *
   * @flags DRBG flags reference
   *
   * Return: normalized strength in *bytes* value or 32 as default
   *	   to counter programming errors
   */
  static inline unsigned short drbg_sec_strength(drbg_flag_t flags)
  {
  	switch (flags & DRBG_STRENGTH_MASK) {
  	case DRBG_STRENGTH128:
  		return 16;
  	case DRBG_STRENGTH192:
  		return 24;
  	case DRBG_STRENGTH256:
  		return 32;
  	default:
  		return 32;
  	}
  }
  
  /*
db07cd26a   Stephan Mueller   crypto: drbg - ad...
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
   * FIPS 140-2 continuous self test for the noise source
   * The test is performed on the noise source input data. Thus, the function
   * implicitly knows the size of the buffer to be equal to the security
   * strength.
   *
   * Note, this function disregards the nonce trailing the entropy data during
   * initial seeding.
   *
   * drbg->drbg_mutex must have been taken.
   *
   * @drbg DRBG handle
   * @entropy buffer of seed data to be checked
   *
   * return:
   *	0 on success
   *	-EAGAIN on when the CTRNG is not yet primed
   *	< 0 on error
   */
  static int drbg_fips_continuous_test(struct drbg_state *drbg,
  				     const unsigned char *entropy)
  {
  	unsigned short entropylen = drbg_sec_strength(drbg->core->flags);
  	int ret = 0;
  
  	if (!IS_ENABLED(CONFIG_CRYPTO_FIPS))
  		return 0;
  
  	/* skip test if we test the overall system */
  	if (list_empty(&drbg->test_data.list))
  		return 0;
  	/* only perform test in FIPS mode */
  	if (!fips_enabled)
  		return 0;
  
  	if (!drbg->fips_primed) {
  		/* Priming of FIPS test */
  		memcpy(drbg->prev, entropy, entropylen);
  		drbg->fips_primed = true;
  		/* priming: another round is needed */
  		return -EAGAIN;
  	}
  	ret = memcmp(drbg->prev, entropy, entropylen);
  	if (!ret)
  		panic("DRBG continuous self test failed
  ");
  	memcpy(drbg->prev, entropy, entropylen);
  
  	/* the test shall pass when the two values are not equal */
  	return 0;
  }
  
  /*
541af946f   Stephan Mueller   crypto: drbg - SP...
273
274
275
   * Convert an integer into a byte representation of this integer.
   * The byte representation is big-endian
   *
541af946f   Stephan Mueller   crypto: drbg - SP...
276
   * @val value to be converted
72f3e00dd   Stephan Mueller   crypto: drbg - re...
277
278
   * @buf buffer holding the converted integer -- caller must ensure that
   *      buffer size is at least 32 bit
541af946f   Stephan Mueller   crypto: drbg - SP...
279
280
   */
  #if (defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_CTR))
72f3e00dd   Stephan Mueller   crypto: drbg - re...
281
  static inline void drbg_cpu_to_be32(__u32 val, unsigned char *buf)
541af946f   Stephan Mueller   crypto: drbg - SP...
282
  {
72f3e00dd   Stephan Mueller   crypto: drbg - re...
283
  	struct s {
7c8ae03f4   Stephan Mueller   crypto: drbg - fi...
284
  		__be32 conv;
72f3e00dd   Stephan Mueller   crypto: drbg - re...
285
286
  	};
  	struct s *conversion = (struct s *) buf;
541af946f   Stephan Mueller   crypto: drbg - SP...
287

72f3e00dd   Stephan Mueller   crypto: drbg - re...
288
  	conversion->conv = cpu_to_be32(val);
541af946f   Stephan Mueller   crypto: drbg - SP...
289
  }
541af946f   Stephan Mueller   crypto: drbg - SP...
290
291
292
293
294
295
296
  #endif /* defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_CTR) */
  
  /******************************************************************
   * CTR DRBG callback functions
   ******************************************************************/
  
  #ifdef CONFIG_CRYPTO_DRBG_CTR
e25e47ec3   Stephan Mueller   crypto: drbg - cl...
297
  #define CRYPTO_DRBG_CTR_STRING "CTR "
0653a7cf6   Stephan Mueller   crypto: drbg - us...
298
299
300
301
302
303
  MODULE_ALIAS_CRYPTO("drbg_pr_ctr_aes256");
  MODULE_ALIAS_CRYPTO("drbg_nopr_ctr_aes256");
  MODULE_ALIAS_CRYPTO("drbg_pr_ctr_aes192");
  MODULE_ALIAS_CRYPTO("drbg_nopr_ctr_aes192");
  MODULE_ALIAS_CRYPTO("drbg_pr_ctr_aes128");
  MODULE_ALIAS_CRYPTO("drbg_nopr_ctr_aes128");
62b62b6e5   Stephan Mueller   crypto: drbg - ad...
304

ed494d4fa   Stephan Mueller   crypto: drbg - re...
305
306
307
308
  static void drbg_kcapi_symsetkey(struct drbg_state *drbg,
  				 const unsigned char *key);
  static int drbg_kcapi_sym(struct drbg_state *drbg, unsigned char *outval,
  			  const struct drbg_string *in);
541af946f   Stephan Mueller   crypto: drbg - SP...
309
310
  static int drbg_init_sym_kernel(struct drbg_state *drbg);
  static int drbg_fini_sym_kernel(struct drbg_state *drbg);
a07203fbf   Stephan Mueller   crypto: drbg - us...
311
312
313
  static int drbg_kcapi_sym_ctr(struct drbg_state *drbg,
  			      u8 *inbuf, u32 inbuflen,
  			      u8 *outbuf, u32 outlen);
43490e804   Stephan Müller   crypto: drbg - in...
314
  #define DRBG_OUTSCRATCHLEN 256
541af946f   Stephan Mueller   crypto: drbg - SP...
315
316
317
318
  
  /* BCC function for CTR DRBG as defined in 10.4.3 */
  static int drbg_ctr_bcc(struct drbg_state *drbg,
  			unsigned char *out, const unsigned char *key,
8c9871660   Stephan Mueller   crypto: drbg - us...
319
  			struct list_head *in)
541af946f   Stephan Mueller   crypto: drbg - SP...
320
  {
8c9871660   Stephan Mueller   crypto: drbg - us...
321
322
  	int ret = 0;
  	struct drbg_string *curr = NULL;
541af946f   Stephan Mueller   crypto: drbg - SP...
323
  	struct drbg_string data;
8c9871660   Stephan Mueller   crypto: drbg - us...
324
  	short cnt = 0;
541af946f   Stephan Mueller   crypto: drbg - SP...
325
326
  
  	drbg_string_fill(&data, out, drbg_blocklen(drbg));
541af946f   Stephan Mueller   crypto: drbg - SP...
327
  	/* 10.4.3 step 2 / 4 */
ed494d4fa   Stephan Mueller   crypto: drbg - re...
328
  	drbg_kcapi_symsetkey(drbg, key);
8c9871660   Stephan Mueller   crypto: drbg - us...
329
330
331
  	list_for_each_entry(curr, in, list) {
  		const unsigned char *pos = curr->buf;
  		size_t len = curr->len;
541af946f   Stephan Mueller   crypto: drbg - SP...
332
  		/* 10.4.3 step 4.1 */
8c9871660   Stephan Mueller   crypto: drbg - us...
333
334
335
336
  		while (len) {
  			/* 10.4.3 step 4.2 */
  			if (drbg_blocklen(drbg) == cnt) {
  				cnt = 0;
ed494d4fa   Stephan Mueller   crypto: drbg - re...
337
  				ret = drbg_kcapi_sym(drbg, out, &data);
8c9871660   Stephan Mueller   crypto: drbg - us...
338
339
  				if (ret)
  					return ret;
541af946f   Stephan Mueller   crypto: drbg - SP...
340
  			}
8c9871660   Stephan Mueller   crypto: drbg - us...
341
342
343
344
  			out[cnt] ^= *pos;
  			pos++;
  			cnt++;
  			len--;
541af946f   Stephan Mueller   crypto: drbg - SP...
345
  		}
541af946f   Stephan Mueller   crypto: drbg - SP...
346
  	}
8c9871660   Stephan Mueller   crypto: drbg - us...
347
348
  	/* 10.4.3 step 4.2 for last block */
  	if (cnt)
ed494d4fa   Stephan Mueller   crypto: drbg - re...
349
  		ret = drbg_kcapi_sym(drbg, out, &data);
8c9871660   Stephan Mueller   crypto: drbg - us...
350
351
  
  	return ret;
541af946f   Stephan Mueller   crypto: drbg - SP...
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
  }
  
  /*
   * scratchpad usage: drbg_ctr_update is interlinked with drbg_ctr_df
   * (and drbg_ctr_bcc, but this function does not need any temporary buffers),
   * the scratchpad is used as follows:
   * drbg_ctr_update:
   *	temp
   *		start: drbg->scratchpad
   *		length: drbg_statelen(drbg) + drbg_blocklen(drbg)
   *			note: the cipher writing into this variable works
   *			blocklen-wise. Now, when the statelen is not a multiple
   *			of blocklen, the generateion loop below "spills over"
   *			by at most blocklen. Thus, we need to give sufficient
   *			memory.
   *	df_data
   *		start: drbg->scratchpad +
   *				drbg_statelen(drbg) + drbg_blocklen(drbg)
   *		length: drbg_statelen(drbg)
   *
   * drbg_ctr_df:
   *	pad
   *		start: df_data + drbg_statelen(drbg)
   *		length: drbg_blocklen(drbg)
   *	iv
   *		start: pad + drbg_blocklen(drbg)
   *		length: drbg_blocklen(drbg)
   *	temp
   *		start: iv + drbg_blocklen(drbg)
8fecaad77   Stephan Mueller   crypto: drbg - fi...
381
382
383
384
385
386
387
388
389
390
   *		length: drbg_satelen(drbg) + drbg_blocklen(drbg)
   *			note: temp is the buffer that the BCC function operates
   *			on. BCC operates blockwise. drbg_statelen(drbg)
   *			is sufficient when the DRBG state length is a multiple
   *			of the block size. For AES192 (and maybe other ciphers)
   *			this is not correct and the length for temp is
   *			insufficient (yes, that also means for such ciphers,
   *			the final output of all BCC rounds are truncated).
   *			Therefore, add drbg_blocklen(drbg) to cover all
   *			possibilities.
541af946f   Stephan Mueller   crypto: drbg - SP...
391
392
393
394
395
   */
  
  /* Derivation Function for CTR DRBG as defined in 10.4.2 */
  static int drbg_ctr_df(struct drbg_state *drbg,
  		       unsigned char *df_data, size_t bytes_to_return,
8c9871660   Stephan Mueller   crypto: drbg - us...
396
  		       struct list_head *seedlist)
541af946f   Stephan Mueller   crypto: drbg - SP...
397
398
399
400
401
  {
  	int ret = -EFAULT;
  	unsigned char L_N[8];
  	/* S3 is input */
  	struct drbg_string S1, S2, S4, cipherin;
8c9871660   Stephan Mueller   crypto: drbg - us...
402
  	LIST_HEAD(bcc_list);
541af946f   Stephan Mueller   crypto: drbg - SP...
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
  	unsigned char *pad = df_data + drbg_statelen(drbg);
  	unsigned char *iv = pad + drbg_blocklen(drbg);
  	unsigned char *temp = iv + drbg_blocklen(drbg);
  	size_t padlen = 0;
  	unsigned int templen = 0;
  	/* 10.4.2 step 7 */
  	unsigned int i = 0;
  	/* 10.4.2 step 8 */
  	const unsigned char *K = (unsigned char *)
  			   "\x00\x01\x02\x03\x04\x05\x06\x07"
  			   "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
  			   "\x10\x11\x12\x13\x14\x15\x16\x17"
  			   "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f";
  	unsigned char *X;
  	size_t generated_len = 0;
  	size_t inputlen = 0;
8c9871660   Stephan Mueller   crypto: drbg - us...
419
  	struct drbg_string *seed = NULL;
541af946f   Stephan Mueller   crypto: drbg - SP...
420
421
422
  
  	memset(pad, 0, drbg_blocklen(drbg));
  	memset(iv, 0, drbg_blocklen(drbg));
541af946f   Stephan Mueller   crypto: drbg - SP...
423
424
425
426
427
428
429
430
  
  	/* 10.4.2 step 1 is implicit as we work byte-wise */
  
  	/* 10.4.2 step 2 */
  	if ((512/8) < bytes_to_return)
  		return -EINVAL;
  
  	/* 10.4.2 step 2 -- calculate the entire length of all input data */
8c9871660   Stephan Mueller   crypto: drbg - us...
431
432
  	list_for_each_entry(seed, seedlist, list)
  		inputlen += seed->len;
72f3e00dd   Stephan Mueller   crypto: drbg - re...
433
  	drbg_cpu_to_be32(inputlen, &L_N[0]);
541af946f   Stephan Mueller   crypto: drbg - SP...
434
435
  
  	/* 10.4.2 step 3 */
72f3e00dd   Stephan Mueller   crypto: drbg - re...
436
  	drbg_cpu_to_be32(bytes_to_return, &L_N[4]);
541af946f   Stephan Mueller   crypto: drbg - SP...
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
  
  	/* 10.4.2 step 5: length is L_N, input_string, one byte, padding */
  	padlen = (inputlen + sizeof(L_N) + 1) % (drbg_blocklen(drbg));
  	/* wrap the padlen appropriately */
  	if (padlen)
  		padlen = drbg_blocklen(drbg) - padlen;
  	/*
  	 * pad / padlen contains the 0x80 byte and the following zero bytes.
  	 * As the calculated padlen value only covers the number of zero
  	 * bytes, this value has to be incremented by one for the 0x80 byte.
  	 */
  	padlen++;
  	pad[0] = 0x80;
  
  	/* 10.4.2 step 4 -- first fill the linked list and then order it */
  	drbg_string_fill(&S1, iv, drbg_blocklen(drbg));
8c9871660   Stephan Mueller   crypto: drbg - us...
453
  	list_add_tail(&S1.list, &bcc_list);
541af946f   Stephan Mueller   crypto: drbg - SP...
454
  	drbg_string_fill(&S2, L_N, sizeof(L_N));
8c9871660   Stephan Mueller   crypto: drbg - us...
455
456
  	list_add_tail(&S2.list, &bcc_list);
  	list_splice_tail(seedlist, &bcc_list);
541af946f   Stephan Mueller   crypto: drbg - SP...
457
  	drbg_string_fill(&S4, pad, padlen);
8c9871660   Stephan Mueller   crypto: drbg - us...
458
  	list_add_tail(&S4.list, &bcc_list);
541af946f   Stephan Mueller   crypto: drbg - SP...
459
460
461
462
463
464
465
466
  
  	/* 10.4.2 step 9 */
  	while (templen < (drbg_keylen(drbg) + (drbg_blocklen(drbg)))) {
  		/*
  		 * 10.4.2 step 9.1 - the padding is implicit as the buffer
  		 * holds zeros after allocation -- even the increment of i
  		 * is irrelevant as the increment remains within length of i
  		 */
72f3e00dd   Stephan Mueller   crypto: drbg - re...
467
  		drbg_cpu_to_be32(i, iv);
541af946f   Stephan Mueller   crypto: drbg - SP...
468
  		/* 10.4.2 step 9.2 -- BCC and concatenation with temp */
8c9871660   Stephan Mueller   crypto: drbg - us...
469
  		ret = drbg_ctr_bcc(drbg, temp + templen, K, &bcc_list);
541af946f   Stephan Mueller   crypto: drbg - SP...
470
471
472
473
474
475
476
477
478
479
480
481
482
483
  		if (ret)
  			goto out;
  		/* 10.4.2 step 9.3 */
  		i++;
  		templen += drbg_blocklen(drbg);
  	}
  
  	/* 10.4.2 step 11 */
  	X = temp + (drbg_keylen(drbg));
  	drbg_string_fill(&cipherin, X, drbg_blocklen(drbg));
  
  	/* 10.4.2 step 12: overwriting of outval is implemented in next step */
  
  	/* 10.4.2 step 13 */
ed494d4fa   Stephan Mueller   crypto: drbg - re...
484
  	drbg_kcapi_symsetkey(drbg, temp);
541af946f   Stephan Mueller   crypto: drbg - SP...
485
486
487
488
489
490
491
  	while (generated_len < bytes_to_return) {
  		short blocklen = 0;
  		/*
  		 * 10.4.2 step 13.1: the truncation of the key length is
  		 * implicit as the key is only drbg_blocklen in size based on
  		 * the implementation of the cipher function callback
  		 */
ed494d4fa   Stephan Mueller   crypto: drbg - re...
492
  		ret = drbg_kcapi_sym(drbg, X, &cipherin);
541af946f   Stephan Mueller   crypto: drbg - SP...
493
494
495
496
497
498
499
500
501
502
503
504
505
506
  		if (ret)
  			goto out;
  		blocklen = (drbg_blocklen(drbg) <
  				(bytes_to_return - generated_len)) ?
  			    drbg_blocklen(drbg) :
  				(bytes_to_return - generated_len);
  		/* 10.4.2 step 13.2 and 14 */
  		memcpy(df_data + generated_len, X, blocklen);
  		generated_len += blocklen;
  	}
  
  	ret = 0;
  
  out:
1471f09f9   Herbert Xu   Revert "crypto: d...
507
  	memset(iv, 0, drbg_blocklen(drbg));
8e0498d99   Stephan Mueller   cryoto: drbg - cl...
508
  	memset(temp, 0, drbg_statelen(drbg) + drbg_blocklen(drbg));
1471f09f9   Herbert Xu   Revert "crypto: d...
509
  	memset(pad, 0, drbg_blocklen(drbg));
541af946f   Stephan Mueller   crypto: drbg - SP...
510
511
  	return ret;
  }
72e7c25aa   Stephan Mueller   crypto: drbg - Ca...
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
  /*
   * update function of CTR DRBG as defined in 10.2.1.2
   *
   * The reseed variable has an enhanced meaning compared to the update
   * functions of the other DRBGs as follows:
   * 0 => initial seed from initialization
   * 1 => reseed via drbg_seed
   * 2 => first invocation from drbg_ctr_update when addtl is present. In
   *      this case, the df_data scratchpad is not deleted so that it is
   *      available for another calls to prevent calling the DF function
   *      again.
   * 3 => second invocation from drbg_ctr_update. When the update function
   *      was called with addtl, the df_data memory already contains the
   *      DFed addtl information and we do not need to call DF again.
   */
8c9871660   Stephan Mueller   crypto: drbg - us...
527
528
  static int drbg_ctr_update(struct drbg_state *drbg, struct list_head *seed,
  			   int reseed)
541af946f   Stephan Mueller   crypto: drbg - SP...
529
530
531
532
533
534
  {
  	int ret = -EFAULT;
  	/* 10.2.1.2 step 1 */
  	unsigned char *temp = drbg->scratchpad;
  	unsigned char *df_data = drbg->scratchpad + drbg_statelen(drbg) +
  				 drbg_blocklen(drbg);
541af946f   Stephan Mueller   crypto: drbg - SP...
535

72e7c25aa   Stephan Mueller   crypto: drbg - Ca...
536
537
  	if (3 > reseed)
  		memset(df_data, 0, drbg_statelen(drbg));
541af946f   Stephan Mueller   crypto: drbg - SP...
538

355912852   Stephan Mueller   crypto: drbg - us...
539
540
541
542
543
544
545
546
547
548
549
550
  	if (!reseed) {
  		/*
  		 * The DRBG uses the CTR mode of the underlying AES cipher. The
  		 * CTR mode increments the counter value after the AES operation
  		 * but SP800-90A requires that the counter is incremented before
  		 * the AES operation. Hence, we increment it at the time we set
  		 * it by one.
  		 */
  		crypto_inc(drbg->V, drbg_blocklen(drbg));
  
  		ret = crypto_skcipher_setkey(drbg->ctr_handle, drbg->C,
  					     drbg_keylen(drbg));
541af946f   Stephan Mueller   crypto: drbg - SP...
551
552
553
  		if (ret)
  			goto out;
  	}
355912852   Stephan Mueller   crypto: drbg - us...
554
555
556
  	/* 10.2.1.3.2 step 2 and 10.2.1.4.2 step 2 */
  	if (seed) {
  		ret = drbg_ctr_df(drbg, df_data, drbg_statelen(drbg), seed);
541af946f   Stephan Mueller   crypto: drbg - SP...
557
558
  		if (ret)
  			goto out;
541af946f   Stephan Mueller   crypto: drbg - SP...
559
  	}
a07203fbf   Stephan Mueller   crypto: drbg - us...
560
561
  	ret = drbg_kcapi_sym_ctr(drbg, df_data, drbg_statelen(drbg),
  				 temp, drbg_statelen(drbg));
355912852   Stephan Mueller   crypto: drbg - us...
562
563
  	if (ret)
  		return ret;
541af946f   Stephan Mueller   crypto: drbg - SP...
564
  	/* 10.2.1.2 step 5 */
103eb3f7b   Stephan Mueller   crypto: drbg - av...
565
  	ret = crypto_skcipher_setkey(drbg->ctr_handle, temp,
355912852   Stephan Mueller   crypto: drbg - us...
566
567
568
  				     drbg_keylen(drbg));
  	if (ret)
  		goto out;
541af946f   Stephan Mueller   crypto: drbg - SP...
569
570
  	/* 10.2.1.2 step 6 */
  	memcpy(drbg->V, temp + drbg_keylen(drbg), drbg_blocklen(drbg));
355912852   Stephan Mueller   crypto: drbg - us...
571
572
  	/* See above: increment counter by one to compensate timing of CTR op */
  	crypto_inc(drbg->V, drbg_blocklen(drbg));
541af946f   Stephan Mueller   crypto: drbg - SP...
573
574
575
  	ret = 0;
  
  out:
1471f09f9   Herbert Xu   Revert "crypto: d...
576
  	memset(temp, 0, drbg_statelen(drbg) + drbg_blocklen(drbg));
72e7c25aa   Stephan Mueller   crypto: drbg - Ca...
577
  	if (2 != reseed)
1471f09f9   Herbert Xu   Revert "crypto: d...
578
  		memset(df_data, 0, drbg_statelen(drbg));
541af946f   Stephan Mueller   crypto: drbg - SP...
579
580
581
582
583
584
585
586
587
588
  	return ret;
  }
  
  /*
   * scratchpad use: drbg_ctr_update is called independently from
   * drbg_ctr_extract_bytes. Therefore, the scratchpad is reused
   */
  /* Generate function of CTR DRBG as defined in 10.2.1.5.2 */
  static int drbg_ctr_generate(struct drbg_state *drbg,
  			     unsigned char *buf, unsigned int buflen,
27e4de2bd   Stephan Mueller   crypto: drbg - Mi...
589
  			     struct list_head *addtl)
541af946f   Stephan Mueller   crypto: drbg - SP...
590
  {
355912852   Stephan Mueller   crypto: drbg - us...
591
592
  	int ret;
  	int len = min_t(int, buflen, INT_MAX);
541af946f   Stephan Mueller   crypto: drbg - SP...
593

541af946f   Stephan Mueller   crypto: drbg - SP...
594
  	/* 10.2.1.5.2 step 2 */
27e4de2bd   Stephan Mueller   crypto: drbg - Mi...
595
596
  	if (addtl && !list_empty(addtl)) {
  		ret = drbg_ctr_update(drbg, addtl, 2);
541af946f   Stephan Mueller   crypto: drbg - SP...
597
598
599
600
601
  		if (ret)
  			return 0;
  	}
  
  	/* 10.2.1.5.2 step 4.1 */
43490e804   Stephan Müller   crypto: drbg - in...
602
  	ret = drbg_kcapi_sym_ctr(drbg, NULL, 0, buf, len);
355912852   Stephan Mueller   crypto: drbg - us...
603
604
  	if (ret)
  		return ret;
541af946f   Stephan Mueller   crypto: drbg - SP...
605

72e7c25aa   Stephan Mueller   crypto: drbg - Ca...
606
607
  	/* 10.2.1.5.2 step 6 */
  	ret = drbg_ctr_update(drbg, NULL, 3);
541af946f   Stephan Mueller   crypto: drbg - SP...
608
609
  	if (ret)
  		len = ret;
541af946f   Stephan Mueller   crypto: drbg - SP...
610
611
  	return len;
  }
e4bc02ace   Julia Lawall   crypto: drbg - co...
612
  static const struct drbg_state_ops drbg_ctr_ops = {
541af946f   Stephan Mueller   crypto: drbg - SP...
613
614
615
616
617
618
619
620
621
622
623
624
  	.update		= drbg_ctr_update,
  	.generate	= drbg_ctr_generate,
  	.crypto_init	= drbg_init_sym_kernel,
  	.crypto_fini	= drbg_fini_sym_kernel,
  };
  #endif /* CONFIG_CRYPTO_DRBG_CTR */
  
  /******************************************************************
   * HMAC DRBG callback functions
   ******************************************************************/
  
  #if defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_HMAC)
4218ebe8c   Stephan Mueller   crypto: drbg - se...
625
626
627
628
  static int drbg_kcapi_hash(struct drbg_state *drbg, unsigned char *outval,
  			   const struct list_head *in);
  static void drbg_kcapi_hmacsetkey(struct drbg_state *drbg,
  				  const unsigned char *key);
541af946f   Stephan Mueller   crypto: drbg - SP...
629
630
631
632
633
  static int drbg_init_hash_kernel(struct drbg_state *drbg);
  static int drbg_fini_hash_kernel(struct drbg_state *drbg);
  #endif /* (CONFIG_CRYPTO_DRBG_HASH || CONFIG_CRYPTO_DRBG_HMAC) */
  
  #ifdef CONFIG_CRYPTO_DRBG_HMAC
e25e47ec3   Stephan Mueller   crypto: drbg - cl...
634
  #define CRYPTO_DRBG_HMAC_STRING "HMAC "
0653a7cf6   Stephan Mueller   crypto: drbg - us...
635
636
637
638
639
640
641
642
  MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha512");
  MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha512");
  MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha384");
  MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha384");
  MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha256");
  MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha256");
  MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha1");
  MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha1");
62b62b6e5   Stephan Mueller   crypto: drbg - ad...
643

541af946f   Stephan Mueller   crypto: drbg - SP...
644
  /* update function of HMAC DRBG as defined in 10.1.2.2 */
8c9871660   Stephan Mueller   crypto: drbg - us...
645
646
  static int drbg_hmac_update(struct drbg_state *drbg, struct list_head *seed,
  			    int reseed)
541af946f   Stephan Mueller   crypto: drbg - SP...
647
648
649
  {
  	int ret = -EFAULT;
  	int i = 0;
8c9871660   Stephan Mueller   crypto: drbg - us...
650
651
652
  	struct drbg_string seed1, seed2, vdata;
  	LIST_HEAD(seedlist);
  	LIST_HEAD(vdatalist);
541af946f   Stephan Mueller   crypto: drbg - SP...
653

4218ebe8c   Stephan Mueller   crypto: drbg - se...
654
  	if (!reseed) {
f072f0e0f   Stephan Mueller   crypto: drbg - re...
655
  		/* 10.1.2.3 step 2 -- memset(0) of C is implicit with kzalloc */
541af946f   Stephan Mueller   crypto: drbg - SP...
656
  		memset(drbg->V, 1, drbg_statelen(drbg));
4218ebe8c   Stephan Mueller   crypto: drbg - se...
657
658
  		drbg_kcapi_hmacsetkey(drbg, drbg->C);
  	}
541af946f   Stephan Mueller   crypto: drbg - SP...
659
660
  
  	drbg_string_fill(&seed1, drbg->V, drbg_statelen(drbg));
8c9871660   Stephan Mueller   crypto: drbg - us...
661
  	list_add_tail(&seed1.list, &seedlist);
541af946f   Stephan Mueller   crypto: drbg - SP...
662
663
  	/* buffer of seed2 will be filled in for loop below with one byte */
  	drbg_string_fill(&seed2, NULL, 1);
8c9871660   Stephan Mueller   crypto: drbg - us...
664
  	list_add_tail(&seed2.list, &seedlist);
541af946f   Stephan Mueller   crypto: drbg - SP...
665
  	/* input data of seed is allowed to be NULL at this point */
8c9871660   Stephan Mueller   crypto: drbg - us...
666
667
  	if (seed)
  		list_splice_tail(seed, &seedlist);
541af946f   Stephan Mueller   crypto: drbg - SP...
668

8c9871660   Stephan Mueller   crypto: drbg - us...
669
670
  	drbg_string_fill(&vdata, drbg->V, drbg_statelen(drbg));
  	list_add_tail(&vdata.list, &vdatalist);
541af946f   Stephan Mueller   crypto: drbg - SP...
671
672
673
674
675
676
677
  	for (i = 2; 0 < i; i--) {
  		/* first round uses 0x0, second 0x1 */
  		unsigned char prefix = DRBG_PREFIX0;
  		if (1 == i)
  			prefix = DRBG_PREFIX1;
  		/* 10.1.2.2 step 1 and 4 -- concatenation and HMAC for key */
  		seed2.buf = &prefix;
4218ebe8c   Stephan Mueller   crypto: drbg - se...
678
  		ret = drbg_kcapi_hash(drbg, drbg->C, &seedlist);
541af946f   Stephan Mueller   crypto: drbg - SP...
679
680
  		if (ret)
  			return ret;
4218ebe8c   Stephan Mueller   crypto: drbg - se...
681
  		drbg_kcapi_hmacsetkey(drbg, drbg->C);
541af946f   Stephan Mueller   crypto: drbg - SP...
682
683
  
  		/* 10.1.2.2 step 2 and 5 -- HMAC for V */
4218ebe8c   Stephan Mueller   crypto: drbg - se...
684
  		ret = drbg_kcapi_hash(drbg, drbg->V, &vdatalist);
541af946f   Stephan Mueller   crypto: drbg - SP...
685
686
687
688
  		if (ret)
  			return ret;
  
  		/* 10.1.2.2 step 3 */
8c9871660   Stephan Mueller   crypto: drbg - us...
689
  		if (!seed)
541af946f   Stephan Mueller   crypto: drbg - SP...
690
691
692
693
694
695
696
697
698
699
  			return ret;
  	}
  
  	return 0;
  }
  
  /* generate function of HMAC DRBG as defined in 10.1.2.5 */
  static int drbg_hmac_generate(struct drbg_state *drbg,
  			      unsigned char *buf,
  			      unsigned int buflen,
27e4de2bd   Stephan Mueller   crypto: drbg - Mi...
700
  			      struct list_head *addtl)
541af946f   Stephan Mueller   crypto: drbg - SP...
701
702
703
704
  {
  	int len = 0;
  	int ret = 0;
  	struct drbg_string data;
8c9871660   Stephan Mueller   crypto: drbg - us...
705
  	LIST_HEAD(datalist);
541af946f   Stephan Mueller   crypto: drbg - SP...
706
707
  
  	/* 10.1.2.5 step 2 */
27e4de2bd   Stephan Mueller   crypto: drbg - Mi...
708
709
  	if (addtl && !list_empty(addtl)) {
  		ret = drbg_hmac_update(drbg, addtl, 1);
541af946f   Stephan Mueller   crypto: drbg - SP...
710
711
712
713
714
  		if (ret)
  			return ret;
  	}
  
  	drbg_string_fill(&data, drbg->V, drbg_statelen(drbg));
8c9871660   Stephan Mueller   crypto: drbg - us...
715
  	list_add_tail(&data.list, &datalist);
541af946f   Stephan Mueller   crypto: drbg - SP...
716
717
718
  	while (len < buflen) {
  		unsigned int outlen = 0;
  		/* 10.1.2.5 step 4.1 */
4218ebe8c   Stephan Mueller   crypto: drbg - se...
719
  		ret = drbg_kcapi_hash(drbg, drbg->V, &datalist);
541af946f   Stephan Mueller   crypto: drbg - SP...
720
721
722
723
  		if (ret)
  			return ret;
  		outlen = (drbg_blocklen(drbg) < (buflen - len)) ?
  			  drbg_blocklen(drbg) : (buflen - len);
541af946f   Stephan Mueller   crypto: drbg - SP...
724
725
726
727
728
729
730
  
  		/* 10.1.2.5 step 4.2 */
  		memcpy(buf + len, drbg->V, outlen);
  		len += outlen;
  	}
  
  	/* 10.1.2.5 step 6 */
27e4de2bd   Stephan Mueller   crypto: drbg - Mi...
731
732
733
  	if (addtl && !list_empty(addtl))
  		ret = drbg_hmac_update(drbg, addtl, 1);
  	else
8c9871660   Stephan Mueller   crypto: drbg - us...
734
  		ret = drbg_hmac_update(drbg, NULL, 1);
541af946f   Stephan Mueller   crypto: drbg - SP...
735
736
737
738
739
  	if (ret)
  		return ret;
  
  	return len;
  }
e4bc02ace   Julia Lawall   crypto: drbg - co...
740
  static const struct drbg_state_ops drbg_hmac_ops = {
541af946f   Stephan Mueller   crypto: drbg - SP...
741
742
743
744
  	.update		= drbg_hmac_update,
  	.generate	= drbg_hmac_generate,
  	.crypto_init	= drbg_init_hash_kernel,
  	.crypto_fini	= drbg_fini_hash_kernel,
541af946f   Stephan Mueller   crypto: drbg - SP...
745
746
747
748
749
750
751
752
  };
  #endif /* CONFIG_CRYPTO_DRBG_HMAC */
  
  /******************************************************************
   * Hash DRBG callback functions
   ******************************************************************/
  
  #ifdef CONFIG_CRYPTO_DRBG_HASH
e25e47ec3   Stephan Mueller   crypto: drbg - cl...
753
  #define CRYPTO_DRBG_HASH_STRING "HASH "
0653a7cf6   Stephan Mueller   crypto: drbg - us...
754
755
756
757
758
759
760
761
  MODULE_ALIAS_CRYPTO("drbg_pr_sha512");
  MODULE_ALIAS_CRYPTO("drbg_nopr_sha512");
  MODULE_ALIAS_CRYPTO("drbg_pr_sha384");
  MODULE_ALIAS_CRYPTO("drbg_nopr_sha384");
  MODULE_ALIAS_CRYPTO("drbg_pr_sha256");
  MODULE_ALIAS_CRYPTO("drbg_nopr_sha256");
  MODULE_ALIAS_CRYPTO("drbg_pr_sha1");
  MODULE_ALIAS_CRYPTO("drbg_nopr_sha1");
62b62b6e5   Stephan Mueller   crypto: drbg - ad...
762

541af946f   Stephan Mueller   crypto: drbg - SP...
763
  /*
41a84982a   Stephan Mueller   crypto: drbg - us...
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
   * Increment buffer
   *
   * @dst buffer to increment
   * @add value to add
   */
  static inline void drbg_add_buf(unsigned char *dst, size_t dstlen,
  				const unsigned char *add, size_t addlen)
  {
  	/* implied: dstlen > addlen */
  	unsigned char *dstptr;
  	const unsigned char *addptr;
  	unsigned int remainder = 0;
  	size_t len = addlen;
  
  	dstptr = dst + (dstlen-1);
  	addptr = add + (addlen-1);
  	while (len) {
  		remainder += *dstptr + *addptr;
  		*dstptr = remainder & 0xff;
  		remainder >>= 8;
  		len--; dstptr--; addptr--;
  	}
  	len = dstlen - addlen;
  	while (len && remainder > 0) {
  		remainder = *dstptr + 1;
  		*dstptr = remainder & 0xff;
  		remainder >>= 8;
  		len--; dstptr--;
  	}
  }
  
  /*
541af946f   Stephan Mueller   crypto: drbg - SP...
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
   * scratchpad usage: as drbg_hash_update and drbg_hash_df are used
   * interlinked, the scratchpad is used as follows:
   * drbg_hash_update
   *	start: drbg->scratchpad
   *	length: drbg_statelen(drbg)
   * drbg_hash_df:
   *	start: drbg->scratchpad + drbg_statelen(drbg)
   *	length: drbg_blocklen(drbg)
   *
   * drbg_hash_process_addtl uses the scratchpad, but fully completes
   * before either of the functions mentioned before are invoked. Therefore,
   * drbg_hash_process_addtl does not need to be specifically considered.
   */
  
  /* Derivation Function for Hash DRBG as defined in 10.4.1 */
  static int drbg_hash_df(struct drbg_state *drbg,
  			unsigned char *outval, size_t outlen,
8c9871660   Stephan Mueller   crypto: drbg - us...
813
  			struct list_head *entropylist)
541af946f   Stephan Mueller   crypto: drbg - SP...
814
815
816
817
818
  {
  	int ret = 0;
  	size_t len = 0;
  	unsigned char input[5];
  	unsigned char *tmp = drbg->scratchpad + drbg_statelen(drbg);
8c9871660   Stephan Mueller   crypto: drbg - us...
819
  	struct drbg_string data;
541af946f   Stephan Mueller   crypto: drbg - SP...
820

541af946f   Stephan Mueller   crypto: drbg - SP...
821
822
  	/* 10.4.1 step 3 */
  	input[0] = 1;
72f3e00dd   Stephan Mueller   crypto: drbg - re...
823
  	drbg_cpu_to_be32((outlen * 8), &input[1]);
541af946f   Stephan Mueller   crypto: drbg - SP...
824
825
  
  	/* 10.4.1 step 4.1 -- concatenation of data for input into hash */
8c9871660   Stephan Mueller   crypto: drbg - us...
826
827
  	drbg_string_fill(&data, input, 5);
  	list_add(&data.list, entropylist);
541af946f   Stephan Mueller   crypto: drbg - SP...
828
829
830
831
832
  
  	/* 10.4.1 step 4 */
  	while (len < outlen) {
  		short blocklen = 0;
  		/* 10.4.1 step 4.1 */
4218ebe8c   Stephan Mueller   crypto: drbg - se...
833
  		ret = drbg_kcapi_hash(drbg, tmp, entropylist);
541af946f   Stephan Mueller   crypto: drbg - SP...
834
835
836
837
838
839
840
841
842
843
844
  		if (ret)
  			goto out;
  		/* 10.4.1 step 4.2 */
  		input[0]++;
  		blocklen = (drbg_blocklen(drbg) < (outlen - len)) ?
  			    drbg_blocklen(drbg) : (outlen - len);
  		memcpy(outval + len, tmp, blocklen);
  		len += blocklen;
  	}
  
  out:
1471f09f9   Herbert Xu   Revert "crypto: d...
845
  	memset(tmp, 0, drbg_blocklen(drbg));
541af946f   Stephan Mueller   crypto: drbg - SP...
846
847
848
849
  	return ret;
  }
  
  /* update function for Hash DRBG as defined in 10.1.1.2 / 10.1.1.3 */
8c9871660   Stephan Mueller   crypto: drbg - us...
850
  static int drbg_hash_update(struct drbg_state *drbg, struct list_head *seed,
541af946f   Stephan Mueller   crypto: drbg - SP...
851
852
853
854
  			    int reseed)
  {
  	int ret = 0;
  	struct drbg_string data1, data2;
8c9871660   Stephan Mueller   crypto: drbg - us...
855
856
  	LIST_HEAD(datalist);
  	LIST_HEAD(datalist2);
541af946f   Stephan Mueller   crypto: drbg - SP...
857
858
  	unsigned char *V = drbg->scratchpad;
  	unsigned char prefix = DRBG_PREFIX1;
541af946f   Stephan Mueller   crypto: drbg - SP...
859
860
861
862
863
864
865
  	if (!seed)
  		return -EINVAL;
  
  	if (reseed) {
  		/* 10.1.1.3 step 1 */
  		memcpy(V, drbg->V, drbg_statelen(drbg));
  		drbg_string_fill(&data1, &prefix, 1);
8c9871660   Stephan Mueller   crypto: drbg - us...
866
  		list_add_tail(&data1.list, &datalist);
541af946f   Stephan Mueller   crypto: drbg - SP...
867
  		drbg_string_fill(&data2, V, drbg_statelen(drbg));
8c9871660   Stephan Mueller   crypto: drbg - us...
868
  		list_add_tail(&data2.list, &datalist);
541af946f   Stephan Mueller   crypto: drbg - SP...
869
  	}
8c9871660   Stephan Mueller   crypto: drbg - us...
870
  	list_splice_tail(seed, &datalist);
541af946f   Stephan Mueller   crypto: drbg - SP...
871
872
  
  	/* 10.1.1.2 / 10.1.1.3 step 2 and 3 */
8c9871660   Stephan Mueller   crypto: drbg - us...
873
  	ret = drbg_hash_df(drbg, drbg->V, drbg_statelen(drbg), &datalist);
541af946f   Stephan Mueller   crypto: drbg - SP...
874
875
876
877
878
879
  	if (ret)
  		goto out;
  
  	/* 10.1.1.2 / 10.1.1.3 step 4  */
  	prefix = DRBG_PREFIX0;
  	drbg_string_fill(&data1, &prefix, 1);
8c9871660   Stephan Mueller   crypto: drbg - us...
880
  	list_add_tail(&data1.list, &datalist2);
541af946f   Stephan Mueller   crypto: drbg - SP...
881
  	drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
8c9871660   Stephan Mueller   crypto: drbg - us...
882
  	list_add_tail(&data2.list, &datalist2);
541af946f   Stephan Mueller   crypto: drbg - SP...
883
  	/* 10.1.1.2 / 10.1.1.3 step 4 */
8c9871660   Stephan Mueller   crypto: drbg - us...
884
  	ret = drbg_hash_df(drbg, drbg->C, drbg_statelen(drbg), &datalist2);
541af946f   Stephan Mueller   crypto: drbg - SP...
885
886
  
  out:
1471f09f9   Herbert Xu   Revert "crypto: d...
887
  	memset(drbg->scratchpad, 0, drbg_statelen(drbg));
541af946f   Stephan Mueller   crypto: drbg - SP...
888
889
890
891
892
  	return ret;
  }
  
  /* processing of additional information string for Hash DRBG */
  static int drbg_hash_process_addtl(struct drbg_state *drbg,
27e4de2bd   Stephan Mueller   crypto: drbg - Mi...
893
  				   struct list_head *addtl)
541af946f   Stephan Mueller   crypto: drbg - SP...
894
895
896
  {
  	int ret = 0;
  	struct drbg_string data1, data2;
8c9871660   Stephan Mueller   crypto: drbg - us...
897
  	LIST_HEAD(datalist);
541af946f   Stephan Mueller   crypto: drbg - SP...
898
  	unsigned char prefix = DRBG_PREFIX2;
541af946f   Stephan Mueller   crypto: drbg - SP...
899
  	/* 10.1.1.4 step 2 */
27e4de2bd   Stephan Mueller   crypto: drbg - Mi...
900
  	if (!addtl || list_empty(addtl))
541af946f   Stephan Mueller   crypto: drbg - SP...
901
902
903
904
905
  		return 0;
  
  	/* 10.1.1.4 step 2a */
  	drbg_string_fill(&data1, &prefix, 1);
  	drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
8c9871660   Stephan Mueller   crypto: drbg - us...
906
907
  	list_add_tail(&data1.list, &datalist);
  	list_add_tail(&data2.list, &datalist);
27e4de2bd   Stephan Mueller   crypto: drbg - Mi...
908
  	list_splice_tail(addtl, &datalist);
4218ebe8c   Stephan Mueller   crypto: drbg - se...
909
  	ret = drbg_kcapi_hash(drbg, drbg->scratchpad, &datalist);
541af946f   Stephan Mueller   crypto: drbg - SP...
910
911
912
913
914
915
916
917
  	if (ret)
  		goto out;
  
  	/* 10.1.1.4 step 2b */
  	drbg_add_buf(drbg->V, drbg_statelen(drbg),
  		     drbg->scratchpad, drbg_blocklen(drbg));
  
  out:
1471f09f9   Herbert Xu   Revert "crypto: d...
918
  	memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
541af946f   Stephan Mueller   crypto: drbg - SP...
919
920
921
922
923
924
925
926
927
928
929
930
931
  	return ret;
  }
  
  /* Hashgen defined in 10.1.1.4 */
  static int drbg_hash_hashgen(struct drbg_state *drbg,
  			     unsigned char *buf,
  			     unsigned int buflen)
  {
  	int len = 0;
  	int ret = 0;
  	unsigned char *src = drbg->scratchpad;
  	unsigned char *dst = drbg->scratchpad + drbg_statelen(drbg);
  	struct drbg_string data;
8c9871660   Stephan Mueller   crypto: drbg - us...
932
  	LIST_HEAD(datalist);
541af946f   Stephan Mueller   crypto: drbg - SP...
933

541af946f   Stephan Mueller   crypto: drbg - SP...
934
935
936
937
  	/* 10.1.1.4 step hashgen 2 */
  	memcpy(src, drbg->V, drbg_statelen(drbg));
  
  	drbg_string_fill(&data, src, drbg_statelen(drbg));
8c9871660   Stephan Mueller   crypto: drbg - us...
938
  	list_add_tail(&data.list, &datalist);
541af946f   Stephan Mueller   crypto: drbg - SP...
939
940
941
  	while (len < buflen) {
  		unsigned int outlen = 0;
  		/* 10.1.1.4 step hashgen 4.1 */
4218ebe8c   Stephan Mueller   crypto: drbg - se...
942
  		ret = drbg_kcapi_hash(drbg, dst, &datalist);
541af946f   Stephan Mueller   crypto: drbg - SP...
943
944
945
946
947
948
  		if (ret) {
  			len = ret;
  			goto out;
  		}
  		outlen = (drbg_blocklen(drbg) < (buflen - len)) ?
  			  drbg_blocklen(drbg) : (buflen - len);
541af946f   Stephan Mueller   crypto: drbg - SP...
949
950
951
952
953
  		/* 10.1.1.4 step hashgen 4.2 */
  		memcpy(buf + len, dst, outlen);
  		len += outlen;
  		/* 10.1.1.4 hashgen step 4.3 */
  		if (len < buflen)
41a84982a   Stephan Mueller   crypto: drbg - us...
954
  			crypto_inc(src, drbg_statelen(drbg));
541af946f   Stephan Mueller   crypto: drbg - SP...
955
956
957
  	}
  
  out:
1471f09f9   Herbert Xu   Revert "crypto: d...
958
  	memset(drbg->scratchpad, 0,
541af946f   Stephan Mueller   crypto: drbg - SP...
959
960
961
962
963
964
965
  	       (drbg_statelen(drbg) + drbg_blocklen(drbg)));
  	return len;
  }
  
  /* generate function for Hash DRBG as defined in  10.1.1.4 */
  static int drbg_hash_generate(struct drbg_state *drbg,
  			      unsigned char *buf, unsigned int buflen,
27e4de2bd   Stephan Mueller   crypto: drbg - Mi...
966
  			      struct list_head *addtl)
541af946f   Stephan Mueller   crypto: drbg - SP...
967
968
969
  {
  	int len = 0;
  	int ret = 0;
72f3e00dd   Stephan Mueller   crypto: drbg - re...
970
971
  	union {
  		unsigned char req[8];
7c8ae03f4   Stephan Mueller   crypto: drbg - fi...
972
  		__be64 req_int;
72f3e00dd   Stephan Mueller   crypto: drbg - re...
973
  	} u;
541af946f   Stephan Mueller   crypto: drbg - SP...
974
975
  	unsigned char prefix = DRBG_PREFIX3;
  	struct drbg_string data1, data2;
8c9871660   Stephan Mueller   crypto: drbg - us...
976
  	LIST_HEAD(datalist);
541af946f   Stephan Mueller   crypto: drbg - SP...
977
978
979
980
981
982
983
984
985
  
  	/* 10.1.1.4 step 2 */
  	ret = drbg_hash_process_addtl(drbg, addtl);
  	if (ret)
  		return ret;
  	/* 10.1.1.4 step 3 */
  	len = drbg_hash_hashgen(drbg, buf, buflen);
  
  	/* this is the value H as documented in 10.1.1.4 */
541af946f   Stephan Mueller   crypto: drbg - SP...
986
987
  	/* 10.1.1.4 step 4 */
  	drbg_string_fill(&data1, &prefix, 1);
8c9871660   Stephan Mueller   crypto: drbg - us...
988
  	list_add_tail(&data1.list, &datalist);
541af946f   Stephan Mueller   crypto: drbg - SP...
989
  	drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
8c9871660   Stephan Mueller   crypto: drbg - us...
990
  	list_add_tail(&data2.list, &datalist);
4218ebe8c   Stephan Mueller   crypto: drbg - se...
991
  	ret = drbg_kcapi_hash(drbg, drbg->scratchpad, &datalist);
541af946f   Stephan Mueller   crypto: drbg - SP...
992
993
994
995
996
997
998
999
1000
1001
  	if (ret) {
  		len = ret;
  		goto out;
  	}
  
  	/* 10.1.1.4 step 5 */
  	drbg_add_buf(drbg->V, drbg_statelen(drbg),
  		     drbg->scratchpad, drbg_blocklen(drbg));
  	drbg_add_buf(drbg->V, drbg_statelen(drbg),
  		     drbg->C, drbg_statelen(drbg));
72f3e00dd   Stephan Mueller   crypto: drbg - re...
1002
1003
  	u.req_int = cpu_to_be64(drbg->reseed_ctr);
  	drbg_add_buf(drbg->V, drbg_statelen(drbg), u.req, 8);
541af946f   Stephan Mueller   crypto: drbg - SP...
1004
1005
  
  out:
1471f09f9   Herbert Xu   Revert "crypto: d...
1006
  	memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
541af946f   Stephan Mueller   crypto: drbg - SP...
1007
1008
1009
1010
1011
1012
1013
  	return len;
  }
  
  /*
   * scratchpad usage: as update and generate are used isolated, both
   * can use the scratchpad
   */
e4bc02ace   Julia Lawall   crypto: drbg - co...
1014
  static const struct drbg_state_ops drbg_hash_ops = {
541af946f   Stephan Mueller   crypto: drbg - SP...
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
  	.update		= drbg_hash_update,
  	.generate	= drbg_hash_generate,
  	.crypto_init	= drbg_init_hash_kernel,
  	.crypto_fini	= drbg_fini_hash_kernel,
  };
  #endif /* CONFIG_CRYPTO_DRBG_HASH */
  
  /******************************************************************
   * Functions common for DRBG implementations
   ******************************************************************/
3d6a5f75d   Stephan Mueller   crypto: drbg - pr...
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
  static inline int __drbg_seed(struct drbg_state *drbg, struct list_head *seed,
  			      int reseed)
  {
  	int ret = drbg->d_ops->update(drbg, seed, reseed);
  
  	if (ret)
  		return ret;
  
  	drbg->seeded = true;
  	/* 10.1.1.2 / 10.1.1.3 step 5 */
  	drbg->reseed_ctr = 1;
  
  	return ret;
  }
db07cd26a   Stephan Mueller   crypto: drbg - ad...
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
  static inline int drbg_get_random_bytes(struct drbg_state *drbg,
  					unsigned char *entropy,
  					unsigned int entropylen)
  {
  	int ret;
  
  	do {
  		get_random_bytes(entropy, entropylen);
  		ret = drbg_fips_continuous_test(drbg, entropy);
  		if (ret && ret != -EAGAIN)
  			return ret;
  	} while (ret);
  
  	return 0;
  }
4c7879907   Stephan Mueller   crypto: drbg - ad...
1054
1055
1056
1057
1058
1059
  static void drbg_async_seed(struct work_struct *work)
  {
  	struct drbg_string data;
  	LIST_HEAD(seedlist);
  	struct drbg_state *drbg = container_of(work, struct drbg_state,
  					       seed_work);
57225e679   Stephan Mueller   crypto: drbg - Us...
1060
1061
  	unsigned int entropylen = drbg_sec_strength(drbg->core->flags);
  	unsigned char entropy[32];
db07cd26a   Stephan Mueller   crypto: drbg - ad...
1062
  	int ret;
4c7879907   Stephan Mueller   crypto: drbg - ad...
1063

57225e679   Stephan Mueller   crypto: drbg - Us...
1064
1065
  	BUG_ON(!entropylen);
  	BUG_ON(entropylen > sizeof(entropy));
4c7879907   Stephan Mueller   crypto: drbg - ad...
1066

57225e679   Stephan Mueller   crypto: drbg - Us...
1067
  	drbg_string_fill(&data, entropy, entropylen);
4c7879907   Stephan Mueller   crypto: drbg - ad...
1068
  	list_add_tail(&data.list, &seedlist);
57225e679   Stephan Mueller   crypto: drbg - Us...
1069

4c7879907   Stephan Mueller   crypto: drbg - ad...
1070
  	mutex_lock(&drbg->drbg_mutex);
57225e679   Stephan Mueller   crypto: drbg - Us...
1071

db07cd26a   Stephan Mueller   crypto: drbg - ad...
1072
1073
1074
  	ret = drbg_get_random_bytes(drbg, entropy, entropylen);
  	if (ret)
  		goto unlock;
57225e679   Stephan Mueller   crypto: drbg - Us...
1075
1076
1077
1078
1079
1080
  	/* Set seeded to false so that if __drbg_seed fails the
  	 * next generate call will trigger a reseed.
  	 */
  	drbg->seeded = false;
  
  	__drbg_seed(drbg, &seedlist, true);
42ea507fa   Stephan Mueller   crypto: drbg - re...
1081
1082
  	if (drbg->seeded)
  		drbg->reseed_threshold = drbg_max_requests(drbg);
db07cd26a   Stephan Mueller   crypto: drbg - ad...
1083
  unlock:
4c7879907   Stephan Mueller   crypto: drbg - ad...
1084
  	mutex_unlock(&drbg->drbg_mutex);
57225e679   Stephan Mueller   crypto: drbg - Us...
1085
1086
  
  	memzero_explicit(entropy, entropylen);
4c7879907   Stephan Mueller   crypto: drbg - ad...
1087
  }
541af946f   Stephan Mueller   crypto: drbg - SP...
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
  /*
   * Seeding or reseeding of the DRBG
   *
   * @drbg: DRBG state struct
   * @pers: personalization / additional information buffer
   * @reseed: 0 for initial seed process, 1 for reseeding
   *
   * return:
   *	0 on success
   *	error value otherwise
   */
  static int drbg_seed(struct drbg_state *drbg, struct drbg_string *pers,
  		     bool reseed)
  {
57225e679   Stephan Mueller   crypto: drbg - Us...
1102
1103
1104
  	int ret;
  	unsigned char entropy[((32 + 16) * 2)];
  	unsigned int entropylen = drbg_sec_strength(drbg->core->flags);
541af946f   Stephan Mueller   crypto: drbg - SP...
1105
  	struct drbg_string data1;
8c9871660   Stephan Mueller   crypto: drbg - us...
1106
  	LIST_HEAD(seedlist);
541af946f   Stephan Mueller   crypto: drbg - SP...
1107
1108
1109
  
  	/* 9.1 / 9.2 / 9.3.1 step 3 */
  	if (pers && pers->len > (drbg_max_addtl(drbg))) {
a9089571f   Stephan Mueller   crypto: drbg - Fi...
1110
1111
  		pr_devel("DRBG: personalization string too long %zu
  ",
541af946f   Stephan Mueller   crypto: drbg - SP...
1112
1113
1114
  			 pers->len);
  		return -EINVAL;
  	}
8fded5925   Herbert Xu   crypto: drbg - Co...
1115
1116
1117
  	if (list_empty(&drbg->test_data.list)) {
  		drbg_string_fill(&data1, drbg->test_data.buf,
  				 drbg->test_data.len);
541af946f   Stephan Mueller   crypto: drbg - SP...
1118
1119
1120
  		pr_devel("DRBG: using test entropy
  ");
  	} else {
57225e679   Stephan Mueller   crypto: drbg - Us...
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
  		/*
  		 * Gather entropy equal to the security strength of the DRBG.
  		 * With a derivation function, a nonce is required in addition
  		 * to the entropy. A nonce must be at least 1/2 of the security
  		 * strength of the DRBG in size. Thus, entropy + nonce is 3/2
  		 * of the strength. The consideration of a nonce is only
  		 * applicable during initial seeding.
  		 */
  		BUG_ON(!entropylen);
  		if (!reseed)
  			entropylen = ((entropylen + 1) / 2) * 3;
  		BUG_ON((entropylen * 2) > sizeof(entropy));
b8ec5ba42   Stephan Mueller   crypto: drbg - us...
1133
  		/* Get seed from in-kernel /dev/urandom */
db07cd26a   Stephan Mueller   crypto: drbg - ad...
1134
1135
1136
  		ret = drbg_get_random_bytes(drbg, entropy, entropylen);
  		if (ret)
  			goto out;
57225e679   Stephan Mueller   crypto: drbg - Us...
1137
1138
1139
1140
1141
1142
  
  		if (!drbg->jent) {
  			drbg_string_fill(&data1, entropy, entropylen);
  			pr_devel("DRBG: (re)seeding with %u bytes of entropy
  ",
  				 entropylen);
b8ec5ba42   Stephan Mueller   crypto: drbg - us...
1143
  		} else {
57225e679   Stephan Mueller   crypto: drbg - Us...
1144
1145
1146
1147
1148
1149
1150
  			/* Get seed from Jitter RNG */
  			ret = crypto_rng_get_bytes(drbg->jent,
  						   entropy + entropylen,
  						   entropylen);
  			if (ret) {
  				pr_devel("DRBG: jent failed with %d
  ", ret);
97f2650e5   Stephan Müller   crypto: drbg - al...
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
  
  				/*
  				 * Do not treat the transient failure of the
  				 * Jitter RNG as an error that needs to be
  				 * reported. The combined number of the
  				 * maximum reseed threshold times the maximum
  				 * number of Jitter RNG transient errors is
  				 * less than the reseed threshold required by
  				 * SP800-90A allowing us to treat the
  				 * transient errors as such.
  				 *
  				 * However, we mandate that at least the first
  				 * seeding operation must succeed with the
  				 * Jitter RNG.
  				 */
  				if (!reseed || ret != -EAGAIN)
  					goto out;
57225e679   Stephan Mueller   crypto: drbg - Us...
1168
1169
1170
1171
1172
1173
  			}
  
  			drbg_string_fill(&data1, entropy, entropylen * 2);
  			pr_devel("DRBG: (re)seeding with %u bytes of entropy
  ",
  				 entropylen * 2);
b8ec5ba42   Stephan Mueller   crypto: drbg - us...
1174
  		}
541af946f   Stephan Mueller   crypto: drbg - SP...
1175
  	}
8c9871660   Stephan Mueller   crypto: drbg - us...
1176
  	list_add_tail(&data1.list, &seedlist);
541af946f   Stephan Mueller   crypto: drbg - SP...
1177
1178
1179
1180
1181
1182
  
  	/*
  	 * concatenation of entropy with personalization str / addtl input)
  	 * the variable pers is directly handed in by the caller, so check its
  	 * contents whether it is appropriate
  	 */
8c9871660   Stephan Mueller   crypto: drbg - us...
1183
1184
  	if (pers && pers->buf && 0 < pers->len) {
  		list_add_tail(&pers->list, &seedlist);
541af946f   Stephan Mueller   crypto: drbg - SP...
1185
1186
1187
  		pr_devel("DRBG: using personalization string
  ");
  	}
e6c0244ad   Stephan Mueller   crypto: drbg - us...
1188
1189
1190
1191
  	if (!reseed) {
  		memset(drbg->V, 0, drbg_statelen(drbg));
  		memset(drbg->C, 0, drbg_statelen(drbg));
  	}
3d6a5f75d   Stephan Mueller   crypto: drbg - pr...
1192
  	ret = __drbg_seed(drbg, &seedlist, reseed);
db07cd26a   Stephan Mueller   crypto: drbg - ad...
1193
  out:
57225e679   Stephan Mueller   crypto: drbg - Us...
1194
  	memzero_explicit(entropy, entropylen * 2);
4c7879907   Stephan Mueller   crypto: drbg - ad...
1195

541af946f   Stephan Mueller   crypto: drbg - SP...
1196
1197
1198
1199
1200
1201
1202
1203
  	return ret;
  }
  
  /* Free all substructures in a DRBG state without the DRBG state structure */
  static inline void drbg_dealloc_state(struct drbg_state *drbg)
  {
  	if (!drbg)
  		return;
453431a54   Waiman Long   mm, treewide: ren...
1204
  	kfree_sensitive(drbg->Vbuf);
eea0d3ea7   Stephan Mueller   crypto: drbg - se...
1205
  	drbg->Vbuf = NULL;
bd6227a15   Stephan Mueller   crypto: drbg - fi...
1206
  	drbg->V = NULL;
453431a54   Waiman Long   mm, treewide: ren...
1207
  	kfree_sensitive(drbg->Cbuf);
eea0d3ea7   Stephan Mueller   crypto: drbg - se...
1208
  	drbg->Cbuf = NULL;
bd6227a15   Stephan Mueller   crypto: drbg - fi...
1209
  	drbg->C = NULL;
453431a54   Waiman Long   mm, treewide: ren...
1210
  	kfree_sensitive(drbg->scratchpadbuf);
3cfc3b972   Stephan Mueller   crypto: drbg - us...
1211
  	drbg->scratchpadbuf = NULL;
541af946f   Stephan Mueller   crypto: drbg - SP...
1212
  	drbg->reseed_ctr = 0;
2a57e4241   Herbert Xu   crypto: drbg - Do...
1213
1214
  	drbg->d_ops = NULL;
  	drbg->core = NULL;
db07cd26a   Stephan Mueller   crypto: drbg - ad...
1215
  	if (IS_ENABLED(CONFIG_CRYPTO_FIPS)) {
453431a54   Waiman Long   mm, treewide: ren...
1216
  		kfree_sensitive(drbg->prev);
db07cd26a   Stephan Mueller   crypto: drbg - ad...
1217
1218
1219
  		drbg->prev = NULL;
  		drbg->fips_primed = false;
  	}
541af946f   Stephan Mueller   crypto: drbg - SP...
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
  }
  
  /*
   * Allocate all sub-structures for a DRBG state.
   * The DRBG state structure must already be allocated.
   */
  static inline int drbg_alloc_state(struct drbg_state *drbg)
  {
  	int ret = -ENOMEM;
  	unsigned int sb_size = 0;
2a57e4241   Herbert Xu   crypto: drbg - Do...
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
  	switch (drbg->core->flags & DRBG_TYPE_MASK) {
  #ifdef CONFIG_CRYPTO_DRBG_HMAC
  	case DRBG_HMAC:
  		drbg->d_ops = &drbg_hmac_ops;
  		break;
  #endif /* CONFIG_CRYPTO_DRBG_HMAC */
  #ifdef CONFIG_CRYPTO_DRBG_HASH
  	case DRBG_HASH:
  		drbg->d_ops = &drbg_hash_ops;
  		break;
  #endif /* CONFIG_CRYPTO_DRBG_HASH */
  #ifdef CONFIG_CRYPTO_DRBG_CTR
  	case DRBG_CTR:
  		drbg->d_ops = &drbg_ctr_ops;
  		break;
  #endif /* CONFIG_CRYPTO_DRBG_CTR */
  	default:
  		ret = -EOPNOTSUPP;
  		goto err;
  	}
3cfc3b972   Stephan Mueller   crypto: drbg - us...
1250
1251
  	ret = drbg->d_ops->crypto_init(drbg);
  	if (ret < 0)
541af946f   Stephan Mueller   crypto: drbg - SP...
1252
  		goto err;
3cfc3b972   Stephan Mueller   crypto: drbg - us...
1253
1254
  
  	drbg->Vbuf = kmalloc(drbg_statelen(drbg) + ret, GFP_KERNEL);
1a45d7e34   Wei Yongjun   crypto: drbg - fi...
1255
1256
  	if (!drbg->Vbuf) {
  		ret = -ENOMEM;
3cfc3b972   Stephan Mueller   crypto: drbg - us...
1257
  		goto fini;
1a45d7e34   Wei Yongjun   crypto: drbg - fi...
1258
  	}
3cfc3b972   Stephan Mueller   crypto: drbg - us...
1259
1260
  	drbg->V = PTR_ALIGN(drbg->Vbuf, ret + 1);
  	drbg->Cbuf = kmalloc(drbg_statelen(drbg) + ret, GFP_KERNEL);
1a45d7e34   Wei Yongjun   crypto: drbg - fi...
1261
1262
  	if (!drbg->Cbuf) {
  		ret = -ENOMEM;
3cfc3b972   Stephan Mueller   crypto: drbg - us...
1263
  		goto fini;
1a45d7e34   Wei Yongjun   crypto: drbg - fi...
1264
  	}
3cfc3b972   Stephan Mueller   crypto: drbg - us...
1265
  	drbg->C = PTR_ALIGN(drbg->Cbuf, ret + 1);
541af946f   Stephan Mueller   crypto: drbg - SP...
1266
1267
1268
1269
1270
1271
1272
1273
  	/* scratchpad is only generated for CTR and Hash */
  	if (drbg->core->flags & DRBG_HMAC)
  		sb_size = 0;
  	else if (drbg->core->flags & DRBG_CTR)
  		sb_size = drbg_statelen(drbg) + drbg_blocklen(drbg) + /* temp */
  			  drbg_statelen(drbg) +	/* df_data */
  			  drbg_blocklen(drbg) +	/* pad */
  			  drbg_blocklen(drbg) +	/* iv */
8fecaad77   Stephan Mueller   crypto: drbg - fi...
1274
  			  drbg_statelen(drbg) + drbg_blocklen(drbg); /* temp */
541af946f   Stephan Mueller   crypto: drbg - SP...
1275
1276
1277
1278
  	else
  		sb_size = drbg_statelen(drbg) + drbg_blocklen(drbg);
  
  	if (0 < sb_size) {
3cfc3b972   Stephan Mueller   crypto: drbg - us...
1279
  		drbg->scratchpadbuf = kzalloc(sb_size + ret, GFP_KERNEL);
1a45d7e34   Wei Yongjun   crypto: drbg - fi...
1280
1281
  		if (!drbg->scratchpadbuf) {
  			ret = -ENOMEM;
3cfc3b972   Stephan Mueller   crypto: drbg - us...
1282
  			goto fini;
1a45d7e34   Wei Yongjun   crypto: drbg - fi...
1283
  		}
3cfc3b972   Stephan Mueller   crypto: drbg - us...
1284
  		drbg->scratchpad = PTR_ALIGN(drbg->scratchpadbuf, ret + 1);
541af946f   Stephan Mueller   crypto: drbg - SP...
1285
  	}
3d6a5f75d   Stephan Mueller   crypto: drbg - pr...
1286

db07cd26a   Stephan Mueller   crypto: drbg - ad...
1287
1288
1289
  	if (IS_ENABLED(CONFIG_CRYPTO_FIPS)) {
  		drbg->prev = kzalloc(drbg_sec_strength(drbg->core->flags),
  				     GFP_KERNEL);
e0664ebce   Wei Yongjun   crypto: drbg - fi...
1290
1291
  		if (!drbg->prev) {
  			ret = -ENOMEM;
db07cd26a   Stephan Mueller   crypto: drbg - ad...
1292
  			goto fini;
e0664ebce   Wei Yongjun   crypto: drbg - fi...
1293
  		}
db07cd26a   Stephan Mueller   crypto: drbg - ad...
1294
1295
  		drbg->fips_primed = false;
  	}
541af946f   Stephan Mueller   crypto: drbg - SP...
1296
  	return 0;
3cfc3b972   Stephan Mueller   crypto: drbg - us...
1297
1298
  fini:
  	drbg->d_ops->crypto_fini(drbg);
541af946f   Stephan Mueller   crypto: drbg - SP...
1299
1300
1301
1302
  err:
  	drbg_dealloc_state(drbg);
  	return ret;
  }
541af946f   Stephan Mueller   crypto: drbg - SP...
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
  /*************************************************************************
   * DRBG interface functions
   *************************************************************************/
  
  /*
   * DRBG generate function as required by SP800-90A - this function
   * generates random numbers
   *
   * @drbg DRBG state handle
   * @buf Buffer where to store the random numbers -- the buffer must already
   *      be pre-allocated by caller
   * @buflen Length of output buffer - this value defines the number of random
   *	   bytes pulled from DRBG
   * @addtl Additional input that is mixed into state, may be NULL -- note
   *	  the entropy is pulled by the DRBG internally unconditionally
   *	  as defined in SP800-90A. The additional input is mixed into
   *	  the state in addition to the pulled entropy.
   *
cde001e4c   Stephan Mueller   crypto: rng - RNG...
1321
   * return: 0 when all bytes are generated; < 0 in case of an error
541af946f   Stephan Mueller   crypto: drbg - SP...
1322
1323
1324
1325
1326
1327
   */
  static int drbg_generate(struct drbg_state *drbg,
  			 unsigned char *buf, unsigned int buflen,
  			 struct drbg_string *addtl)
  {
  	int len = 0;
27e4de2bd   Stephan Mueller   crypto: drbg - Mi...
1328
  	LIST_HEAD(addtllist);
541af946f   Stephan Mueller   crypto: drbg - SP...
1329

2a57e4241   Herbert Xu   crypto: drbg - Do...
1330
1331
1332
1333
1334
  	if (!drbg->core) {
  		pr_devel("DRBG: not yet seeded
  ");
  		return -EINVAL;
  	}
541af946f   Stephan Mueller   crypto: drbg - SP...
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
  	if (0 == buflen || !buf) {
  		pr_devel("DRBG: no output buffer provided
  ");
  		return -EINVAL;
  	}
  	if (addtl && NULL == addtl->buf && 0 < addtl->len) {
  		pr_devel("DRBG: wrong format of additional information
  ");
  		return -EINVAL;
  	}
541af946f   Stephan Mueller   crypto: drbg - SP...
1345
1346
  	/* 9.3.1 step 2 */
  	len = -EINVAL;
76899a41f   Stephan Mueller   crypto: drbg - re...
1347
  	if (buflen > (drbg_max_request_bytes(drbg))) {
541af946f   Stephan Mueller   crypto: drbg - SP...
1348
1349
1350
1351
1352
1353
1354
1355
1356
  		pr_devel("DRBG: requested random numbers too large %u
  ",
  			 buflen);
  		goto err;
  	}
  
  	/* 9.3.1 step 3 is implicit with the chosen DRBG */
  
  	/* 9.3.1 step 4 */
76899a41f   Stephan Mueller   crypto: drbg - re...
1357
  	if (addtl && addtl->len > (drbg_max_addtl(drbg))) {
541af946f   Stephan Mueller   crypto: drbg - SP...
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
  		pr_devel("DRBG: additional information string too long %zu
  ",
  			 addtl->len);
  		goto err;
  	}
  	/* 9.3.1 step 5 is implicit with the chosen DRBG */
  
  	/*
  	 * 9.3.1 step 6 and 9 supplemented by 9.3.2 step c is implemented
  	 * here. The spec is a bit convoluted here, we make it simpler.
  	 */
42ea507fa   Stephan Mueller   crypto: drbg - re...
1369
  	if (drbg->reseed_threshold < drbg->reseed_ctr)
76899a41f   Stephan Mueller   crypto: drbg - re...
1370
  		drbg->seeded = false;
541af946f   Stephan Mueller   crypto: drbg - SP...
1371

76899a41f   Stephan Mueller   crypto: drbg - re...
1372
  	if (drbg->pr || !drbg->seeded) {
541af946f   Stephan Mueller   crypto: drbg - SP...
1373
1374
1375
1376
1377
1378
  		pr_devel("DRBG: reseeding before generation (prediction "
  			 "resistance: %s, state %s)
  ",
  			 drbg->pr ? "true" : "false",
  			 drbg->seeded ? "seeded" : "unseeded");
  		/* 9.3.1 steps 7.1 through 7.3 */
76899a41f   Stephan Mueller   crypto: drbg - re...
1379
  		len = drbg_seed(drbg, addtl, true);
541af946f   Stephan Mueller   crypto: drbg - SP...
1380
1381
1382
1383
1384
  		if (len)
  			goto err;
  		/* 9.3.1 step 7.4 */
  		addtl = NULL;
  	}
27e4de2bd   Stephan Mueller   crypto: drbg - Mi...
1385

27e4de2bd   Stephan Mueller   crypto: drbg - Mi...
1386
1387
  	if (addtl && 0 < addtl->len)
  		list_add_tail(&addtl->list, &addtllist);
541af946f   Stephan Mueller   crypto: drbg - SP...
1388
  	/* 9.3.1 step 8 and 10 */
76899a41f   Stephan Mueller   crypto: drbg - re...
1389
  	len = drbg->d_ops->generate(drbg, buf, buflen, &addtllist);
541af946f   Stephan Mueller   crypto: drbg - SP...
1390
1391
  
  	/* 10.1.1.4 step 6, 10.1.2.5 step 7, 10.2.1.5.2 step 7 */
76899a41f   Stephan Mueller   crypto: drbg - re...
1392
  	drbg->reseed_ctr++;
541af946f   Stephan Mueller   crypto: drbg - SP...
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
  	if (0 >= len)
  		goto err;
  
  	/*
  	 * Section 11.3.3 requires to re-perform self tests after some
  	 * generated random numbers. The chosen value after which self
  	 * test is performed is arbitrary, but it should be reasonable.
  	 * However, we do not perform the self tests because of the following
  	 * reasons: it is mathematically impossible that the initial self tests
  	 * were successfully and the following are not. If the initial would
  	 * pass and the following would not, the kernel integrity is violated.
  	 * In this case, the entire kernel operation is questionable and it
  	 * is unlikely that the integrity violation only affects the
  	 * correct operation of the DRBG.
  	 *
  	 * Albeit the following code is commented out, it is provided in
  	 * case somebody has a need to implement the test of 11.3.3.
  	 */
  #if 0
76899a41f   Stephan Mueller   crypto: drbg - re...
1412
  	if (drbg->reseed_ctr && !(drbg->reseed_ctr % 4096)) {
541af946f   Stephan Mueller   crypto: drbg - SP...
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
  		int err = 0;
  		pr_devel("DRBG: start to perform self test
  ");
  		if (drbg->core->flags & DRBG_HMAC)
  			err = alg_test("drbg_pr_hmac_sha256",
  				       "drbg_pr_hmac_sha256", 0, 0);
  		else if (drbg->core->flags & DRBG_CTR)
  			err = alg_test("drbg_pr_ctr_aes128",
  				       "drbg_pr_ctr_aes128", 0, 0);
  		else
  			err = alg_test("drbg_pr_sha256",
  				       "drbg_pr_sha256", 0, 0);
  		if (err) {
  			pr_err("DRBG: periodical self test failed
  ");
  			/*
  			 * uninstantiate implies that from now on, only errors
  			 * are returned when reusing this DRBG cipher handle
  			 */
  			drbg_uninstantiate(drbg);
541af946f   Stephan Mueller   crypto: drbg - SP...
1433
1434
1435
1436
1437
1438
1439
  			return 0;
  		} else {
  			pr_devel("DRBG: self test successful
  ");
  		}
  	}
  #endif
cde001e4c   Stephan Mueller   crypto: rng - RNG...
1440
1441
1442
1443
1444
  	/*
  	 * All operations were successful, return 0 as mandated by
  	 * the kernel crypto API interface.
  	 */
  	len = 0;
541af946f   Stephan Mueller   crypto: drbg - SP...
1445
  err:
541af946f   Stephan Mueller   crypto: drbg - SP...
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
  	return len;
  }
  
  /*
   * Wrapper around drbg_generate which can pull arbitrary long strings
   * from the DRBG without hitting the maximum request limitation.
   *
   * Parameters: see drbg_generate
   * Return codes: see drbg_generate -- if one drbg_generate request fails,
   *		 the entire drbg_generate_long request fails
   */
  static int drbg_generate_long(struct drbg_state *drbg,
  			      unsigned char *buf, unsigned int buflen,
  			      struct drbg_string *addtl)
  {
082eb10ba   Stephan Mueller   crypto: drbg - fi...
1461
  	unsigned int len = 0;
541af946f   Stephan Mueller   crypto: drbg - SP...
1462
1463
  	unsigned int slice = 0;
  	do {
082eb10ba   Stephan Mueller   crypto: drbg - fi...
1464
  		int err = 0;
541af946f   Stephan Mueller   crypto: drbg - SP...
1465
1466
1467
  		unsigned int chunk = 0;
  		slice = ((buflen - len) / drbg_max_request_bytes(drbg));
  		chunk = slice ? drbg_max_request_bytes(drbg) : (buflen - len);
76899a41f   Stephan Mueller   crypto: drbg - re...
1468
  		mutex_lock(&drbg->drbg_mutex);
082eb10ba   Stephan Mueller   crypto: drbg - fi...
1469
  		err = drbg_generate(drbg, buf + len, chunk, addtl);
76899a41f   Stephan Mueller   crypto: drbg - re...
1470
  		mutex_unlock(&drbg->drbg_mutex);
082eb10ba   Stephan Mueller   crypto: drbg - fi...
1471
1472
1473
  		if (0 > err)
  			return err;
  		len += chunk;
ce5481d01   Stephan Mueller   crypto: drbg - fi...
1474
  	} while (slice > 0 && (len < buflen));
082eb10ba   Stephan Mueller   crypto: drbg - fi...
1475
  	return 0;
541af946f   Stephan Mueller   crypto: drbg - SP...
1476
  }
57225e679   Stephan Mueller   crypto: drbg - Us...
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
  static void drbg_schedule_async_seed(struct random_ready_callback *rdy)
  {
  	struct drbg_state *drbg = container_of(rdy, struct drbg_state,
  					       random_ready);
  
  	schedule_work(&drbg->seed_work);
  }
  
  static int drbg_prepare_hrng(struct drbg_state *drbg)
  {
  	int err;
  
  	/* We do not need an HRNG in test mode. */
  	if (list_empty(&drbg->test_data.list))
  		return 0;
97f2650e5   Stephan Müller   crypto: drbg - al...
1492
  	drbg->jent = crypto_alloc_rng("jitterentropy_rng", 0, 0);
57225e679   Stephan Mueller   crypto: drbg - Us...
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
  	INIT_WORK(&drbg->seed_work, drbg_async_seed);
  
  	drbg->random_ready.owner = THIS_MODULE;
  	drbg->random_ready.func = drbg_schedule_async_seed;
  
  	err = add_random_ready_callback(&drbg->random_ready);
  
  	switch (err) {
  	case 0:
  		break;
  
  	case -EALREADY:
  		err = 0;
df561f668   Gustavo A. R. Silva   treewide: Use fal...
1506
  		fallthrough;
57225e679   Stephan Mueller   crypto: drbg - Us...
1507
1508
1509
1510
1511
  
  	default:
  		drbg->random_ready.func = NULL;
  		return err;
  	}
42ea507fa   Stephan Mueller   crypto: drbg - re...
1512
1513
1514
1515
1516
  	/*
  	 * Require frequent reseeds until the seed source is fully
  	 * initialized.
  	 */
  	drbg->reseed_threshold = 50;
57225e679   Stephan Mueller   crypto: drbg - Us...
1517
1518
  	return err;
  }
541af946f   Stephan Mueller   crypto: drbg - SP...
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
  /*
   * DRBG instantiation function as required by SP800-90A - this function
   * sets up the DRBG handle, performs the initial seeding and all sanity
   * checks required by SP800-90A
   *
   * @drbg memory of state -- if NULL, new memory is allocated
   * @pers Personalization string that is mixed into state, may be NULL -- note
   *	 the entropy is pulled by the DRBG internally unconditionally
   *	 as defined in SP800-90A. The additional input is mixed into
   *	 the state in addition to the pulled entropy.
   * @coreref reference to core
   * @pr prediction resistance enabled
   *
   * return
   *	0 on success
   *	error value otherwise
   */
  static int drbg_instantiate(struct drbg_state *drbg, struct drbg_string *pers,
  			    int coreref, bool pr)
  {
2a57e4241   Herbert Xu   crypto: drbg - Do...
1539
1540
  	int ret;
  	bool reseed = true;
541af946f   Stephan Mueller   crypto: drbg - SP...
1541
1542
1543
1544
  
  	pr_devel("DRBG: Initializing DRBG core %d with prediction resistance "
  		 "%s
  ", coreref, pr ? "enabled" : "disabled");
76899a41f   Stephan Mueller   crypto: drbg - re...
1545
  	mutex_lock(&drbg->drbg_mutex);
541af946f   Stephan Mueller   crypto: drbg - SP...
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
  
  	/* 9.1 step 1 is implicit with the selected DRBG type */
  
  	/*
  	 * 9.1 step 2 is implicit as caller can select prediction resistance
  	 * and the flag is copied into drbg->flags --
  	 * all DRBG types support prediction resistance
  	 */
  
  	/* 9.1 step 4 is implicit in  drbg_sec_strength */
2a57e4241   Herbert Xu   crypto: drbg - Do...
1556
1557
1558
1559
  	if (!drbg->core) {
  		drbg->core = &drbg_cores[coreref];
  		drbg->pr = pr;
  		drbg->seeded = false;
42ea507fa   Stephan Mueller   crypto: drbg - re...
1560
  		drbg->reseed_threshold = drbg_max_requests(drbg);
541af946f   Stephan Mueller   crypto: drbg - SP...
1561

2a57e4241   Herbert Xu   crypto: drbg - Do...
1562
1563
1564
  		ret = drbg_alloc_state(drbg);
  		if (ret)
  			goto unlock;
57225e679   Stephan Mueller   crypto: drbg - Us...
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
  		ret = drbg_prepare_hrng(drbg);
  		if (ret)
  			goto free_everything;
  
  		if (IS_ERR(drbg->jent)) {
  			ret = PTR_ERR(drbg->jent);
  			drbg->jent = NULL;
  			if (fips_enabled || ret != -ENOENT)
  				goto free_everything;
  			pr_info("DRBG: Continuing without Jitter RNG
  ");
  		}
2a57e4241   Herbert Xu   crypto: drbg - Do...
1577
1578
1579
1580
  		reseed = false;
  	}
  
  	ret = drbg_seed(drbg, pers, reseed);
57225e679   Stephan Mueller   crypto: drbg - Us...
1581
1582
  	if (ret && !reseed)
  		goto free_everything;
541af946f   Stephan Mueller   crypto: drbg - SP...
1583

76899a41f   Stephan Mueller   crypto: drbg - re...
1584
  	mutex_unlock(&drbg->drbg_mutex);
2a57e4241   Herbert Xu   crypto: drbg - Do...
1585
  	return ret;
541af946f   Stephan Mueller   crypto: drbg - SP...
1586

76899a41f   Stephan Mueller   crypto: drbg - re...
1587
1588
  unlock:
  	mutex_unlock(&drbg->drbg_mutex);
541af946f   Stephan Mueller   crypto: drbg - SP...
1589
  	return ret;
57225e679   Stephan Mueller   crypto: drbg - Us...
1590
1591
1592
1593
1594
  
  free_everything:
  	mutex_unlock(&drbg->drbg_mutex);
  	drbg_uninstantiate(drbg);
  	return ret;
541af946f   Stephan Mueller   crypto: drbg - SP...
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
  }
  
  /*
   * DRBG uninstantiate function as required by SP800-90A - this function
   * frees all buffers and the DRBG handle
   *
   * @drbg DRBG state handle
   *
   * return
   *	0 on success
   */
  static int drbg_uninstantiate(struct drbg_state *drbg)
  {
57225e679   Stephan Mueller   crypto: drbg - Us...
1608
1609
1610
  	if (drbg->random_ready.func) {
  		del_random_ready_callback(&drbg->random_ready);
  		cancel_work_sync(&drbg->seed_work);
57225e679   Stephan Mueller   crypto: drbg - Us...
1611
  	}
819966c06   Stephan Müller   crypto: drbg - al...
1612
1613
1614
  	if (!IS_ERR_OR_NULL(drbg->jent))
  		crypto_free_rng(drbg->jent);
  	drbg->jent = NULL;
2a57e4241   Herbert Xu   crypto: drbg - Do...
1615
1616
  	if (drbg->d_ops)
  		drbg->d_ops->crypto_fini(drbg);
541af946f   Stephan Mueller   crypto: drbg - SP...
1617
1618
  	drbg_dealloc_state(drbg);
  	/* no scrubbing of test_data -- this shall survive an uninstantiate */
541af946f   Stephan Mueller   crypto: drbg - SP...
1619
1620
1621
1622
1623
1624
1625
  	return 0;
  }
  
  /*
   * Helper function for setting the test data in the DRBG
   *
   * @drbg DRBG state handle
8fded5925   Herbert Xu   crypto: drbg - Co...
1626
1627
   * @data test data
   * @len test data length
541af946f   Stephan Mueller   crypto: drbg - SP...
1628
   */
8fded5925   Herbert Xu   crypto: drbg - Co...
1629
1630
  static void drbg_kcapi_set_entropy(struct crypto_rng *tfm,
  				   const u8 *data, unsigned int len)
541af946f   Stephan Mueller   crypto: drbg - SP...
1631
  {
8fded5925   Herbert Xu   crypto: drbg - Co...
1632
1633
1634
1635
  	struct drbg_state *drbg = crypto_rng_ctx(tfm);
  
  	mutex_lock(&drbg->drbg_mutex);
  	drbg_string_fill(&drbg->test_data, data, len);
76899a41f   Stephan Mueller   crypto: drbg - re...
1636
  	mutex_unlock(&drbg->drbg_mutex);
541af946f   Stephan Mueller   crypto: drbg - SP...
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
  }
  
  /***************************************************************
   * Kernel crypto API cipher invocations requested by DRBG
   ***************************************************************/
  
  #if defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_HMAC)
  struct sdesc {
  	struct shash_desc shash;
  	char ctx[];
  };
  
  static int drbg_init_hash_kernel(struct drbg_state *drbg)
  {
  	struct sdesc *sdesc;
  	struct crypto_shash *tfm;
  
  	tfm = crypto_alloc_shash(drbg->core->backend_cra_name, 0, 0);
  	if (IS_ERR(tfm)) {
593dfbd9c   Sergey Senozhatsky   crypto: drbg - re...
1656
1657
1658
  		pr_info("DRBG: could not allocate digest TFM handle: %s
  ",
  				drbg->core->backend_cra_name);
541af946f   Stephan Mueller   crypto: drbg - SP...
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
  		return PTR_ERR(tfm);
  	}
  	BUG_ON(drbg_blocklen(drbg) != crypto_shash_digestsize(tfm));
  	sdesc = kzalloc(sizeof(struct shash_desc) + crypto_shash_descsize(tfm),
  			GFP_KERNEL);
  	if (!sdesc) {
  		crypto_free_shash(tfm);
  		return -ENOMEM;
  	}
  
  	sdesc->shash.tfm = tfm;
541af946f   Stephan Mueller   crypto: drbg - SP...
1670
  	drbg->priv_data = sdesc;
3cfc3b972   Stephan Mueller   crypto: drbg - us...
1671
1672
  
  	return crypto_shash_alignmask(tfm);
541af946f   Stephan Mueller   crypto: drbg - SP...
1673
1674
1675
1676
1677
1678
1679
  }
  
  static int drbg_fini_hash_kernel(struct drbg_state *drbg)
  {
  	struct sdesc *sdesc = (struct sdesc *)drbg->priv_data;
  	if (sdesc) {
  		crypto_free_shash(sdesc->shash.tfm);
453431a54   Waiman Long   mm, treewide: ren...
1680
  		kfree_sensitive(sdesc);
541af946f   Stephan Mueller   crypto: drbg - SP...
1681
1682
1683
1684
  	}
  	drbg->priv_data = NULL;
  	return 0;
  }
4218ebe8c   Stephan Mueller   crypto: drbg - se...
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
  static void drbg_kcapi_hmacsetkey(struct drbg_state *drbg,
  				  const unsigned char *key)
  {
  	struct sdesc *sdesc = (struct sdesc *)drbg->priv_data;
  
  	crypto_shash_setkey(sdesc->shash.tfm, key, drbg_statelen(drbg));
  }
  
  static int drbg_kcapi_hash(struct drbg_state *drbg, unsigned char *outval,
  			   const struct list_head *in)
541af946f   Stephan Mueller   crypto: drbg - SP...
1695
1696
  {
  	struct sdesc *sdesc = (struct sdesc *)drbg->priv_data;
8c9871660   Stephan Mueller   crypto: drbg - us...
1697
  	struct drbg_string *input = NULL;
541af946f   Stephan Mueller   crypto: drbg - SP...
1698

541af946f   Stephan Mueller   crypto: drbg - SP...
1699
  	crypto_shash_init(&sdesc->shash);
8c9871660   Stephan Mueller   crypto: drbg - us...
1700
1701
  	list_for_each_entry(input, in, list)
  		crypto_shash_update(&sdesc->shash, input->buf, input->len);
541af946f   Stephan Mueller   crypto: drbg - SP...
1702
1703
1704
1705
1706
  	return crypto_shash_final(&sdesc->shash, outval);
  }
  #endif /* (CONFIG_CRYPTO_DRBG_HASH || CONFIG_CRYPTO_DRBG_HMAC) */
  
  #ifdef CONFIG_CRYPTO_DRBG_CTR
355912852   Stephan Mueller   crypto: drbg - us...
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
  static int drbg_fini_sym_kernel(struct drbg_state *drbg)
  {
  	struct crypto_cipher *tfm =
  		(struct crypto_cipher *)drbg->priv_data;
  	if (tfm)
  		crypto_free_cipher(tfm);
  	drbg->priv_data = NULL;
  
  	if (drbg->ctr_handle)
  		crypto_free_skcipher(drbg->ctr_handle);
  	drbg->ctr_handle = NULL;
  
  	if (drbg->ctr_req)
88f1d316b   Wu Fengguang   crypto: drbg - fi...
1720
  		skcipher_request_free(drbg->ctr_req);
355912852   Stephan Mueller   crypto: drbg - us...
1721
  	drbg->ctr_req = NULL;
510298121   Stephan Mueller   crypto: drbg - pr...
1722
1723
  	kfree(drbg->outscratchpadbuf);
  	drbg->outscratchpadbuf = NULL;
355912852   Stephan Mueller   crypto: drbg - us...
1724
1725
  	return 0;
  }
541af946f   Stephan Mueller   crypto: drbg - SP...
1726
1727
  static int drbg_init_sym_kernel(struct drbg_state *drbg)
  {
04bcbfcf7   Stephan Mueller   crypto: drbg - us...
1728
  	struct crypto_cipher *tfm;
355912852   Stephan Mueller   crypto: drbg - us...
1729
1730
1731
1732
  	struct crypto_skcipher *sk_tfm;
  	struct skcipher_request *req;
  	unsigned int alignmask;
  	char ctr_name[CRYPTO_MAX_ALG_NAME];
541af946f   Stephan Mueller   crypto: drbg - SP...
1733

04bcbfcf7   Stephan Mueller   crypto: drbg - us...
1734
  	tfm = crypto_alloc_cipher(drbg->core->backend_cra_name, 0, 0);
541af946f   Stephan Mueller   crypto: drbg - SP...
1735
  	if (IS_ERR(tfm)) {
593dfbd9c   Sergey Senozhatsky   crypto: drbg - re...
1736
1737
1738
  		pr_info("DRBG: could not allocate cipher TFM handle: %s
  ",
  				drbg->core->backend_cra_name);
541af946f   Stephan Mueller   crypto: drbg - SP...
1739
1740
  		return PTR_ERR(tfm);
  	}
04bcbfcf7   Stephan Mueller   crypto: drbg - us...
1741
  	BUG_ON(drbg_blocklen(drbg) != crypto_cipher_blocksize(tfm));
541af946f   Stephan Mueller   crypto: drbg - SP...
1742
  	drbg->priv_data = tfm;
541af946f   Stephan Mueller   crypto: drbg - SP...
1743

355912852   Stephan Mueller   crypto: drbg - us...
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
  	if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)",
  	    drbg->core->backend_cra_name) >= CRYPTO_MAX_ALG_NAME) {
  		drbg_fini_sym_kernel(drbg);
  		return -EINVAL;
  	}
  	sk_tfm = crypto_alloc_skcipher(ctr_name, 0, 0);
  	if (IS_ERR(sk_tfm)) {
  		pr_info("DRBG: could not allocate CTR cipher TFM handle: %s
  ",
  				ctr_name);
  		drbg_fini_sym_kernel(drbg);
  		return PTR_ERR(sk_tfm);
  	}
  	drbg->ctr_handle = sk_tfm;
85a2dea4b   Gilad Ben-Yossef   crypto: drbg - mo...
1758
  	crypto_init_wait(&drbg->ctr_wait);
355912852   Stephan Mueller   crypto: drbg - us...
1759
1760
1761
1762
1763
1764
  
  	req = skcipher_request_alloc(sk_tfm, GFP_KERNEL);
  	if (!req) {
  		pr_info("DRBG: could not allocate request queue
  ");
  		drbg_fini_sym_kernel(drbg);
01ac94580   Dan Carpenter   crypto: drbg - fi...
1765
  		return -ENOMEM;
355912852   Stephan Mueller   crypto: drbg - us...
1766
1767
  	}
  	drbg->ctr_req = req;
85a2dea4b   Gilad Ben-Yossef   crypto: drbg - mo...
1768
1769
1770
  	skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
  						CRYPTO_TFM_REQ_MAY_SLEEP,
  					crypto_req_done, &drbg->ctr_wait);
355912852   Stephan Mueller   crypto: drbg - us...
1771
1772
  
  	alignmask = crypto_skcipher_alignmask(sk_tfm);
510298121   Stephan Mueller   crypto: drbg - pr...
1773
1774
1775
1776
1777
1778
1779
1780
  	drbg->outscratchpadbuf = kmalloc(DRBG_OUTSCRATCHLEN + alignmask,
  					 GFP_KERNEL);
  	if (!drbg->outscratchpadbuf) {
  		drbg_fini_sym_kernel(drbg);
  		return -ENOMEM;
  	}
  	drbg->outscratchpad = (u8 *)PTR_ALIGN(drbg->outscratchpadbuf,
  					      alignmask + 1);
cf862cbc8   Stephan Mueller   crypto: drbg - el...
1781
  	sg_init_table(&drbg->sg_in, 1);
43490e804   Stephan Müller   crypto: drbg - in...
1782
  	sg_init_one(&drbg->sg_out, drbg->outscratchpad, DRBG_OUTSCRATCHLEN);
cf862cbc8   Stephan Mueller   crypto: drbg - el...
1783

3cfc3b972   Stephan Mueller   crypto: drbg - us...
1784
  	return alignmask;
541af946f   Stephan Mueller   crypto: drbg - SP...
1785
  }
ed494d4fa   Stephan Mueller   crypto: drbg - re...
1786
1787
  static void drbg_kcapi_symsetkey(struct drbg_state *drbg,
  				 const unsigned char *key)
541af946f   Stephan Mueller   crypto: drbg - SP...
1788
  {
04bcbfcf7   Stephan Mueller   crypto: drbg - us...
1789
1790
  	struct crypto_cipher *tfm =
  		(struct crypto_cipher *)drbg->priv_data;
541af946f   Stephan Mueller   crypto: drbg - SP...
1791

04bcbfcf7   Stephan Mueller   crypto: drbg - us...
1792
  	crypto_cipher_setkey(tfm, key, (drbg_keylen(drbg)));
ed494d4fa   Stephan Mueller   crypto: drbg - re...
1793
1794
1795
1796
1797
1798
1799
  }
  
  static int drbg_kcapi_sym(struct drbg_state *drbg, unsigned char *outval,
  			  const struct drbg_string *in)
  {
  	struct crypto_cipher *tfm =
  		(struct crypto_cipher *)drbg->priv_data;
04bcbfcf7   Stephan Mueller   crypto: drbg - us...
1800
1801
1802
1803
  	/* there is only component in *in */
  	BUG_ON(in->len < drbg_blocklen(drbg));
  	crypto_cipher_encrypt_one(tfm, outval, in->buf);
  	return 0;
541af946f   Stephan Mueller   crypto: drbg - SP...
1804
  }
355912852   Stephan Mueller   crypto: drbg - us...
1805

a07203fbf   Stephan Mueller   crypto: drbg - us...
1806
1807
1808
  static int drbg_kcapi_sym_ctr(struct drbg_state *drbg,
  			      u8 *inbuf, u32 inlen,
  			      u8 *outbuf, u32 outlen)
355912852   Stephan Mueller   crypto: drbg - us...
1809
  {
cf862cbc8   Stephan Mueller   crypto: drbg - el...
1810
  	struct scatterlist *sg_in = &drbg->sg_in, *sg_out = &drbg->sg_out;
43490e804   Stephan Müller   crypto: drbg - in...
1811
  	u32 scratchpad_use = min_t(u32, outlen, DRBG_OUTSCRATCHLEN);
510298121   Stephan Mueller   crypto: drbg - pr...
1812
  	int ret;
355912852   Stephan Mueller   crypto: drbg - us...
1813

43490e804   Stephan Müller   crypto: drbg - in...
1814
1815
1816
1817
1818
1819
1820
1821
1822
  	if (inbuf) {
  		/* Use caller-provided input buffer */
  		sg_set_buf(sg_in, inbuf, inlen);
  	} else {
  		/* Use scratchpad for in-place operation */
  		inlen = scratchpad_use;
  		memset(drbg->outscratchpad, 0, scratchpad_use);
  		sg_set_buf(sg_in, drbg->outscratchpad, scratchpad_use);
  	}
355912852   Stephan Mueller   crypto: drbg - us...
1823
1824
  
  	while (outlen) {
510298121   Stephan Mueller   crypto: drbg - pr...
1825
  		u32 cryptlen = min3(inlen, outlen, (u32)DRBG_OUTSCRATCHLEN);
355912852   Stephan Mueller   crypto: drbg - us...
1826

510298121   Stephan Mueller   crypto: drbg - pr...
1827
  		/* Output buffer may not be valid for SGL, use scratchpad */
cf862cbc8   Stephan Mueller   crypto: drbg - el...
1828
  		skcipher_request_set_crypt(drbg->ctr_req, sg_in, sg_out,
355912852   Stephan Mueller   crypto: drbg - us...
1829
  					   cryptlen, drbg->V);
85a2dea4b   Gilad Ben-Yossef   crypto: drbg - mo...
1830
1831
1832
  		ret = crypto_wait_req(crypto_skcipher_encrypt(drbg->ctr_req),
  					&drbg->ctr_wait);
  		if (ret)
510298121   Stephan Mueller   crypto: drbg - pr...
1833
  			goto out;
85a2dea4b   Gilad Ben-Yossef   crypto: drbg - mo...
1834
1835
  
  		crypto_init_wait(&drbg->ctr_wait);
355912852   Stephan Mueller   crypto: drbg - us...
1836

510298121   Stephan Mueller   crypto: drbg - pr...
1837
  		memcpy(outbuf, drbg->outscratchpad, cryptlen);
43490e804   Stephan Müller   crypto: drbg - in...
1838
  		memzero_explicit(drbg->outscratchpad, cryptlen);
510298121   Stephan Mueller   crypto: drbg - pr...
1839

355912852   Stephan Mueller   crypto: drbg - us...
1840
  		outlen -= cryptlen;
8ff4c191d   Stephan Mueller   crypto: drbg - ad...
1841
  		outbuf += cryptlen;
355912852   Stephan Mueller   crypto: drbg - us...
1842
  	}
510298121   Stephan Mueller   crypto: drbg - pr...
1843
  	ret = 0;
355912852   Stephan Mueller   crypto: drbg - us...
1844

510298121   Stephan Mueller   crypto: drbg - pr...
1845
  out:
510298121   Stephan Mueller   crypto: drbg - pr...
1846
  	return ret;
355912852   Stephan Mueller   crypto: drbg - us...
1847
  }
541af946f   Stephan Mueller   crypto: drbg - SP...
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
  #endif /* CONFIG_CRYPTO_DRBG_CTR */
  
  /***************************************************************
   * Kernel crypto API interface to register DRBG
   ***************************************************************/
  
  /*
   * Look up the DRBG flags by given kernel crypto API cra_name
   * The code uses the drbg_cores definition to do this
   *
   * @cra_name kernel crypto API cra_name
   * @coreref reference to integer which is filled with the pointer to
   *  the applicable core
   * @pr reference for setting prediction resistance
   *
   * return: flags
   */
  static inline void drbg_convert_tfm_core(const char *cra_driver_name,
  					 int *coreref, bool *pr)
  {
  	int i = 0;
  	size_t start = 0;
  	int len = 0;
  
  	*pr = true;
  	/* disassemble the names */
  	if (!memcmp(cra_driver_name, "drbg_nopr_", 10)) {
  		start = 10;
  		*pr = false;
  	} else if (!memcmp(cra_driver_name, "drbg_pr_", 8)) {
  		start = 8;
  	} else {
  		return;
  	}
  
  	/* remove the first part */
  	len = strlen(cra_driver_name) - start;
  	for (i = 0; ARRAY_SIZE(drbg_cores) > i; i++) {
  		if (!memcmp(cra_driver_name + start, drbg_cores[i].cra_name,
  			    len)) {
  			*coreref = i;
  			return;
  		}
  	}
  }
  
  static int drbg_kcapi_init(struct crypto_tfm *tfm)
  {
  	struct drbg_state *drbg = crypto_tfm_ctx(tfm);
541af946f   Stephan Mueller   crypto: drbg - SP...
1897

76899a41f   Stephan Mueller   crypto: drbg - re...
1898
  	mutex_init(&drbg->drbg_mutex);
2a57e4241   Herbert Xu   crypto: drbg - Do...
1899
1900
  
  	return 0;
541af946f   Stephan Mueller   crypto: drbg - SP...
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
  }
  
  static void drbg_kcapi_cleanup(struct crypto_tfm *tfm)
  {
  	drbg_uninstantiate(crypto_tfm_ctx(tfm));
  }
  
  /*
   * Generate random numbers invoked by the kernel crypto API:
   * The API of the kernel crypto API is extended as follows:
   *
8fded5925   Herbert Xu   crypto: drbg - Co...
1912
1913
1914
1915
   * src is additional input supplied to the RNG.
   * slen is the length of src.
   * dst is the output buffer where random data is to be stored.
   * dlen is the length of dst.
541af946f   Stephan Mueller   crypto: drbg - SP...
1916
   */
8fded5925   Herbert Xu   crypto: drbg - Co...
1917
1918
1919
  static int drbg_kcapi_random(struct crypto_rng *tfm,
  			     const u8 *src, unsigned int slen,
  			     u8 *dst, unsigned int dlen)
541af946f   Stephan Mueller   crypto: drbg - SP...
1920
1921
  {
  	struct drbg_state *drbg = crypto_rng_ctx(tfm);
8fded5925   Herbert Xu   crypto: drbg - Co...
1922
1923
1924
1925
  	struct drbg_string *addtl = NULL;
  	struct drbg_string string;
  
  	if (slen) {
8c9871660   Stephan Mueller   crypto: drbg - us...
1926
  		/* linked list variable is now local to allow modification */
8fded5925   Herbert Xu   crypto: drbg - Co...
1927
1928
  		drbg_string_fill(&string, src, slen);
  		addtl = &string;
541af946f   Stephan Mueller   crypto: drbg - SP...
1929
  	}
8fded5925   Herbert Xu   crypto: drbg - Co...
1930
1931
  
  	return drbg_generate_long(drbg, dst, dlen, addtl);
541af946f   Stephan Mueller   crypto: drbg - SP...
1932
1933
1934
  }
  
  /*
2a57e4241   Herbert Xu   crypto: drbg - Do...
1935
   * Seed the DRBG invoked by the kernel crypto API
541af946f   Stephan Mueller   crypto: drbg - SP...
1936
   */
8fded5925   Herbert Xu   crypto: drbg - Co...
1937
1938
  static int drbg_kcapi_seed(struct crypto_rng *tfm,
  			   const u8 *seed, unsigned int slen)
541af946f   Stephan Mueller   crypto: drbg - SP...
1939
1940
1941
1942
  {
  	struct drbg_state *drbg = crypto_rng_ctx(tfm);
  	struct crypto_tfm *tfm_base = crypto_rng_tfm(tfm);
  	bool pr = false;
8fded5925   Herbert Xu   crypto: drbg - Co...
1943
1944
  	struct drbg_string string;
  	struct drbg_string *seed_string = NULL;
541af946f   Stephan Mueller   crypto: drbg - SP...
1945
  	int coreref = 0;
541af946f   Stephan Mueller   crypto: drbg - SP...
1946
1947
1948
  	drbg_convert_tfm_core(crypto_tfm_alg_driver_name(tfm_base), &coreref,
  			      &pr);
  	if (0 < slen) {
8fded5925   Herbert Xu   crypto: drbg - Co...
1949
1950
  		drbg_string_fill(&string, seed, slen);
  		seed_string = &string;
541af946f   Stephan Mueller   crypto: drbg - SP...
1951
  	}
8fded5925   Herbert Xu   crypto: drbg - Co...
1952
1953
  
  	return drbg_instantiate(drbg, seed_string, coreref, pr);
541af946f   Stephan Mueller   crypto: drbg - SP...
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
  }
  
  /***************************************************************
   * Kernel module: code to load the module
   ***************************************************************/
  
  /*
   * Tests as defined in 11.3.2 in addition to the cipher tests: testing
   * of the error handling.
   *
   * Note: testing of failing seed source as defined in 11.3.2 is not applicable
   * as seed source of get_random_bytes does not fail.
   *
   * Note 2: There is no sensible way of testing the reseed counter
   * enforcement, so skip it.
   */
  static inline int __init drbg_healthcheck_sanity(void)
  {
541af946f   Stephan Mueller   crypto: drbg - SP...
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
  	int len = 0;
  #define OUTBUFLEN 16
  	unsigned char buf[OUTBUFLEN];
  	struct drbg_state *drbg = NULL;
  	int ret = -EFAULT;
  	int rc = -EFAULT;
  	bool pr = false;
  	int coreref = 0;
  	struct drbg_string addtl;
  	size_t max_addtllen, max_request_bytes;
  
  	/* only perform test in FIPS mode */
  	if (!fips_enabled)
  		return 0;
  
  #ifdef CONFIG_CRYPTO_DRBG_CTR
  	drbg_convert_tfm_core("drbg_nopr_ctr_aes128", &coreref, &pr);
e25e47ec3   Stephan Mueller   crypto: drbg - cl...
1989
  #elif defined CONFIG_CRYPTO_DRBG_HASH
541af946f   Stephan Mueller   crypto: drbg - SP...
1990
1991
1992
1993
1994
1995
1996
1997
  	drbg_convert_tfm_core("drbg_nopr_sha256", &coreref, &pr);
  #else
  	drbg_convert_tfm_core("drbg_nopr_hmac_sha256", &coreref, &pr);
  #endif
  
  	drbg = kzalloc(sizeof(struct drbg_state), GFP_KERNEL);
  	if (!drbg)
  		return -ENOMEM;
e11a75481   Herbert Xu   crypto: drbg - In...
1998
  	mutex_init(&drbg->drbg_mutex);
d89a67134   Stephan Mueller   crypto: drbg - do...
1999
2000
  	drbg->core = &drbg_cores[coreref];
  	drbg->reseed_threshold = drbg_max_requests(drbg);
e11a75481   Herbert Xu   crypto: drbg - In...
2001

541af946f   Stephan Mueller   crypto: drbg - SP...
2002
2003
2004
2005
2006
2007
2008
  	/*
  	 * if the following tests fail, it is likely that there is a buffer
  	 * overflow as buf is much smaller than the requested or provided
  	 * string lengths -- in case the error handling does not succeed
  	 * we may get an OOPS. And we want to get an OOPS as this is a
  	 * grave bug.
  	 */
541af946f   Stephan Mueller   crypto: drbg - SP...
2009
2010
2011
2012
2013
2014
2015
2016
2017
  	max_addtllen = drbg_max_addtl(drbg);
  	max_request_bytes = drbg_max_request_bytes(drbg);
  	drbg_string_fill(&addtl, buf, max_addtllen + 1);
  	/* overflow addtllen with additonal info string */
  	len = drbg_generate(drbg, buf, OUTBUFLEN, &addtl);
  	BUG_ON(0 < len);
  	/* overflow max_bits */
  	len = drbg_generate(drbg, buf, (max_request_bytes + 1), NULL);
  	BUG_ON(0 < len);
541af946f   Stephan Mueller   crypto: drbg - SP...
2018
2019
  
  	/* overflow max addtllen with personalization string */
d89a67134   Stephan Mueller   crypto: drbg - do...
2020
  	ret = drbg_seed(drbg, &addtl, false);
541af946f   Stephan Mueller   crypto: drbg - SP...
2021
  	BUG_ON(0 == ret);
541af946f   Stephan Mueller   crypto: drbg - SP...
2022
2023
2024
2025
2026
2027
  	/* all tests passed */
  	rc = 0;
  
  	pr_devel("DRBG: Sanity tests for failure code paths successfully "
  		 "completed
  ");
d89a67134   Stephan Mueller   crypto: drbg - do...
2028
  	kfree(drbg);
541af946f   Stephan Mueller   crypto: drbg - SP...
2029
  	return rc;
541af946f   Stephan Mueller   crypto: drbg - SP...
2030
  }
8fded5925   Herbert Xu   crypto: drbg - Co...
2031
  static struct rng_alg drbg_algs[22];
541af946f   Stephan Mueller   crypto: drbg - SP...
2032
2033
2034
2035
2036
2037
  
  /*
   * Fill the array drbg_algs used to register the different DRBGs
   * with the kernel crypto API. To fill the array, the information
   * from drbg_cores[] is used.
   */
8fded5925   Herbert Xu   crypto: drbg - Co...
2038
  static inline void __init drbg_fill_array(struct rng_alg *alg,
541af946f   Stephan Mueller   crypto: drbg - SP...
2039
2040
2041
  					  const struct drbg_core *core, int pr)
  {
  	int pos = 0;
51ee14227   Herbert Xu   crypto: drbg - Ad...
2042
  	static int priority = 200;
541af946f   Stephan Mueller   crypto: drbg - SP...
2043

8fded5925   Herbert Xu   crypto: drbg - Co...
2044
  	memcpy(alg->base.cra_name, "stdrng", 6);
541af946f   Stephan Mueller   crypto: drbg - SP...
2045
  	if (pr) {
8fded5925   Herbert Xu   crypto: drbg - Co...
2046
  		memcpy(alg->base.cra_driver_name, "drbg_pr_", 8);
541af946f   Stephan Mueller   crypto: drbg - SP...
2047
2048
  		pos = 8;
  	} else {
8fded5925   Herbert Xu   crypto: drbg - Co...
2049
  		memcpy(alg->base.cra_driver_name, "drbg_nopr_", 10);
541af946f   Stephan Mueller   crypto: drbg - SP...
2050
2051
  		pos = 10;
  	}
8fded5925   Herbert Xu   crypto: drbg - Co...
2052
  	memcpy(alg->base.cra_driver_name + pos, core->cra_name,
541af946f   Stephan Mueller   crypto: drbg - SP...
2053
  	       strlen(core->cra_name));
8fded5925   Herbert Xu   crypto: drbg - Co...
2054
  	alg->base.cra_priority = priority;
541af946f   Stephan Mueller   crypto: drbg - SP...
2055
2056
2057
2058
2059
2060
2061
  	priority++;
  	/*
  	 * If FIPS mode enabled, the selected DRBG shall have the
  	 * highest cra_priority over other stdrng instances to ensure
  	 * it is selected.
  	 */
  	if (fips_enabled)
8fded5925   Herbert Xu   crypto: drbg - Co...
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
  		alg->base.cra_priority += 200;
  
  	alg->base.cra_ctxsize 	= sizeof(struct drbg_state);
  	alg->base.cra_module	= THIS_MODULE;
  	alg->base.cra_init	= drbg_kcapi_init;
  	alg->base.cra_exit	= drbg_kcapi_cleanup;
  	alg->generate		= drbg_kcapi_random;
  	alg->seed		= drbg_kcapi_seed;
  	alg->set_ent		= drbg_kcapi_set_entropy;
  	alg->seedsize		= 0;
541af946f   Stephan Mueller   crypto: drbg - SP...
2072
2073
2074
2075
2076
2077
  }
  
  static int __init drbg_init(void)
  {
  	unsigned int i = 0; /* pointer to drbg_algs */
  	unsigned int j = 0; /* pointer to drbg_cores */
1a45d7e34   Wei Yongjun   crypto: drbg - fi...
2078
  	int ret;
541af946f   Stephan Mueller   crypto: drbg - SP...
2079
2080
2081
2082
2083
2084
2085
  
  	ret = drbg_healthcheck_sanity();
  	if (ret)
  		return ret;
  
  	if (ARRAY_SIZE(drbg_cores) * 2 > ARRAY_SIZE(drbg_algs)) {
  		pr_info("DRBG: Cannot register all DRBG types"
a9089571f   Stephan Mueller   crypto: drbg - Fi...
2086
2087
  			"(slots needed: %zu, slots available: %zu)
  ",
541af946f   Stephan Mueller   crypto: drbg - SP...
2088
  			ARRAY_SIZE(drbg_cores) * 2, ARRAY_SIZE(drbg_algs));
1a45d7e34   Wei Yongjun   crypto: drbg - fi...
2089
  		return -EFAULT;
541af946f   Stephan Mueller   crypto: drbg - SP...
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
  	}
  
  	/*
  	 * each DRBG definition can be used with PR and without PR, thus
  	 * we instantiate each DRBG in drbg_cores[] twice.
  	 *
  	 * As the order of placing them into the drbg_algs array matters
  	 * (the later DRBGs receive a higher cra_priority) we register the
  	 * prediction resistance DRBGs first as the should not be too
  	 * interesting.
  	 */
  	for (j = 0; ARRAY_SIZE(drbg_cores) > j; j++, i++)
  		drbg_fill_array(&drbg_algs[i], &drbg_cores[j], 1);
  	for (j = 0; ARRAY_SIZE(drbg_cores) > j; j++, i++)
  		drbg_fill_array(&drbg_algs[i], &drbg_cores[j], 0);
8fded5925   Herbert Xu   crypto: drbg - Co...
2105
  	return crypto_register_rngs(drbg_algs, (ARRAY_SIZE(drbg_cores) * 2));
541af946f   Stephan Mueller   crypto: drbg - SP...
2106
  }
96956aef2   Fengguang Wu   crypto: drbg - dr...
2107
  static void __exit drbg_exit(void)
541af946f   Stephan Mueller   crypto: drbg - SP...
2108
  {
8fded5925   Herbert Xu   crypto: drbg - Co...
2109
  	crypto_unregister_rngs(drbg_algs, (ARRAY_SIZE(drbg_cores) * 2));
541af946f   Stephan Mueller   crypto: drbg - SP...
2110
  }
c4741b230   Eric Biggers   crypto: run initc...
2111
  subsys_initcall(drbg_init);
541af946f   Stephan Mueller   crypto: drbg - SP...
2112
  module_exit(drbg_exit);
e25e47ec3   Stephan Mueller   crypto: drbg - cl...
2113
2114
  #ifndef CRYPTO_DRBG_HASH_STRING
  #define CRYPTO_DRBG_HASH_STRING ""
541af946f   Stephan Mueller   crypto: drbg - SP...
2115
  #endif
e25e47ec3   Stephan Mueller   crypto: drbg - cl...
2116
2117
  #ifndef CRYPTO_DRBG_HMAC_STRING
  #define CRYPTO_DRBG_HMAC_STRING ""
541af946f   Stephan Mueller   crypto: drbg - SP...
2118
  #endif
e25e47ec3   Stephan Mueller   crypto: drbg - cl...
2119
2120
  #ifndef CRYPTO_DRBG_CTR_STRING
  #define CRYPTO_DRBG_CTR_STRING ""
541af946f   Stephan Mueller   crypto: drbg - SP...
2121
  #endif
e25e47ec3   Stephan Mueller   crypto: drbg - cl...
2122
2123
2124
2125
2126
2127
2128
  MODULE_LICENSE("GPL");
  MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
  MODULE_DESCRIPTION("NIST SP800-90A Deterministic Random Bit Generator (DRBG) "
  		   "using following cores: "
  		   CRYPTO_DRBG_HASH_STRING
  		   CRYPTO_DRBG_HMAC_STRING
  		   CRYPTO_DRBG_CTR_STRING);
51ee14227   Herbert Xu   crypto: drbg - Ad...
2129
  MODULE_ALIAS_CRYPTO("stdrng");