xref: /openbmc/linux/crypto/jitterentropy-kcapi.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1dfc9fa91SStephan Mueller /*
2dfc9fa91SStephan Mueller  * Non-physical true random number generator based on timing jitter --
3dfc9fa91SStephan Mueller  * Linux Kernel Crypto API specific code
4dfc9fa91SStephan Mueller  *
5dfc9fa91SStephan Mueller  * Copyright Stephan Mueller <smueller@chronox.de>, 2015 - 2023
6dfc9fa91SStephan Mueller  *
7dfc9fa91SStephan Mueller  * Redistribution and use in source and binary forms, with or without
8dfc9fa91SStephan Mueller  * modification, are permitted provided that the following conditions
9dfc9fa91SStephan Mueller  * are met:
10dfc9fa91SStephan Mueller  * 1. Redistributions of source code must retain the above copyright
11dfc9fa91SStephan Mueller  *    notice, and the entire permission notice in its entirety,
12dfc9fa91SStephan Mueller  *    including the disclaimer of warranties.
13dfc9fa91SStephan Mueller  * 2. Redistributions in binary form must reproduce the above copyright
14dfc9fa91SStephan Mueller  *    notice, this list of conditions and the following disclaimer in the
15dfc9fa91SStephan Mueller  *    documentation and/or other materials provided with the distribution.
16dfc9fa91SStephan Mueller  * 3. The name of the author may not be used to endorse or promote
17dfc9fa91SStephan Mueller  *    products derived from this software without specific prior
18dfc9fa91SStephan Mueller  *    written permission.
19dfc9fa91SStephan Mueller  *
20dfc9fa91SStephan Mueller  * ALTERNATIVELY, this product may be distributed under the terms of
21dfc9fa91SStephan Mueller  * the GNU General Public License, in which case the provisions of the GPL2 are
22dfc9fa91SStephan Mueller  * required INSTEAD OF the above restrictions.  (This clause is
23dfc9fa91SStephan Mueller  * necessary due to a potential bad interaction between the GPL and
24dfc9fa91SStephan Mueller  * the restrictions contained in a BSD-style copyright.)
25dfc9fa91SStephan Mueller  *
26dfc9fa91SStephan Mueller  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
27dfc9fa91SStephan Mueller  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28dfc9fa91SStephan Mueller  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
29dfc9fa91SStephan Mueller  * WHICH ARE HEREBY DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE
30dfc9fa91SStephan Mueller  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31dfc9fa91SStephan Mueller  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
32dfc9fa91SStephan Mueller  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
33dfc9fa91SStephan Mueller  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34dfc9fa91SStephan Mueller  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35dfc9fa91SStephan Mueller  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
36dfc9fa91SStephan Mueller  * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
37dfc9fa91SStephan Mueller  * DAMAGE.
38dfc9fa91SStephan Mueller  */
39dfc9fa91SStephan Mueller 
40*3fde2fe9SStephan Müller #include <crypto/hash.h>
410c3dc787SHerbert Xu #include <crypto/sha3.h>
42dfc9fa91SStephan Mueller #include <linux/fips.h>
43dfc9fa91SStephan Mueller #include <linux/kernel.h>
44dfc9fa91SStephan Mueller #include <linux/module.h>
45dfc9fa91SStephan Mueller #include <linux/slab.h>
46dfc9fa91SStephan Mueller #include <linux/time.h>
47965d7286SBen Dooks #include <crypto/internal/rng.h>
48dfc9fa91SStephan Mueller 
49dfc9fa91SStephan Mueller #include "jitterentropy.h"
50dfc9fa91SStephan Mueller 
51dfc9fa91SStephan Mueller #define JENT_CONDITIONING_HASH	"sha3-256-generic"
52dfc9fa91SStephan Mueller 
53dfc9fa91SStephan Mueller /***************************************************************************
54dfc9fa91SStephan Mueller  * Helper function
55dfc9fa91SStephan Mueller  ***************************************************************************/
56dfc9fa91SStephan Mueller 
jent_zalloc(unsigned int len)57dfc9fa91SStephan Mueller void *jent_zalloc(unsigned int len)
58dfc9fa91SStephan Mueller {
59dfc9fa91SStephan Mueller 	return kzalloc(len, GFP_KERNEL);
60453431a5SWaiman Long }
61dfc9fa91SStephan Mueller 
jent_zfree(void * ptr)62dfc9fa91SStephan Mueller void jent_zfree(void *ptr)
63dfc9fa91SStephan Mueller {
64dfc9fa91SStephan Mueller 	kfree_sensitive(ptr);
65dfc9fa91SStephan Mueller }
66dfc9fa91SStephan Mueller 
67dfc9fa91SStephan Mueller /*
68b578456cSStephan Mueller  * Obtain a high-resolution time stamp value. The time stamp is used to measure
69b578456cSStephan Mueller  * the execution time of a given code path and its variations. Hence, the time
70b578456cSStephan Mueller  * stamp must have a sufficiently high resolution.
71b578456cSStephan Mueller  *
72b578456cSStephan Mueller  * Note, if the function returns zero because a given architecture does not
73b578456cSStephan Mueller  * implement a high-resolution time stamp, the RNG code's runtime test
74b578456cSStephan Mueller  * will detect it and will not produce output.
75b578456cSStephan Mueller  */
jent_get_nstime(__u64 * out)76b578456cSStephan Mueller void jent_get_nstime(__u64 *out)
77dfc9fa91SStephan Mueller {
78dfc9fa91SStephan Mueller 	__u64 tmp = 0;
79dfc9fa91SStephan Mueller 
80dfc9fa91SStephan Mueller 	tmp = random_get_entropy();
81dfc9fa91SStephan Mueller 
82dfc9fa91SStephan Mueller 	/*
83dfc9fa91SStephan Mueller 	 * If random_get_entropy does not return a value, i.e. it is not
84b578456cSStephan Mueller 	 * implemented for a given architecture, use a clock source.
85b578456cSStephan Mueller 	 * hoping that there are timers we can work with.
86dfc9fa91SStephan Mueller 	 */
87dfc9fa91SStephan Mueller 	if (tmp == 0)
88b578456cSStephan Mueller 		tmp = ktime_get_ns();
89b578456cSStephan Mueller 
90dfc9fa91SStephan Mueller 	*out = tmp;
91dfc9fa91SStephan Mueller 	jent_raw_hires_entropy_store(tmp);
92dfc9fa91SStephan Mueller }
93dfc9fa91SStephan Mueller 
jent_hash_time(void * hash_state,__u64 time,u8 * addtl,unsigned int addtl_len,__u64 hash_loop_cnt,unsigned int stuck)94dfc9fa91SStephan Mueller int jent_hash_time(void *hash_state, __u64 time, u8 *addtl,
95dfc9fa91SStephan Mueller 		   unsigned int addtl_len, __u64 hash_loop_cnt,
96dfc9fa91SStephan Mueller 		   unsigned int stuck)
97dfc9fa91SStephan Mueller {
98dfc9fa91SStephan Mueller 	struct shash_desc *hash_state_desc = (struct shash_desc *)hash_state;
99dfc9fa91SStephan Mueller 	SHASH_DESC_ON_STACK(desc, hash_state_desc->tfm);
100dfc9fa91SStephan Mueller 	u8 intermediary[SHA3_256_DIGEST_SIZE];
101dfc9fa91SStephan Mueller 	__u64 j = 0;
102dfc9fa91SStephan Mueller 	int ret;
103dfc9fa91SStephan Mueller 
104dfc9fa91SStephan Mueller 	desc->tfm = hash_state_desc->tfm;
105dfc9fa91SStephan Mueller 
106dfc9fa91SStephan Mueller 	if (sizeof(intermediary) != crypto_shash_digestsize(desc->tfm)) {
107dfc9fa91SStephan Mueller 		pr_warn_ratelimited("Unexpected digest size\n");
108dfc9fa91SStephan Mueller 		return -EINVAL;
109dfc9fa91SStephan Mueller 	}
110dfc9fa91SStephan Mueller 
111dfc9fa91SStephan Mueller 	/*
112dfc9fa91SStephan Mueller 	 * This loop fills a buffer which is injected into the entropy pool.
113dfc9fa91SStephan Mueller 	 * The main reason for this loop is to execute something over which we
114dfc9fa91SStephan Mueller 	 * can perform a timing measurement. The injection of the resulting
115dfc9fa91SStephan Mueller 	 * data into the pool is performed to ensure the result is used and
116dfc9fa91SStephan Mueller 	 * the compiler cannot optimize the loop away in case the result is not
117dfc9fa91SStephan Mueller 	 * used at all. Yet that data is considered "additional information"
118dfc9fa91SStephan Mueller 	 * considering the terminology from SP800-90A without any entropy.
119dfc9fa91SStephan Mueller 	 *
120dfc9fa91SStephan Mueller 	 * Note, it does not matter which or how much data you inject, we are
121dfc9fa91SStephan Mueller 	 * interested in one Keccack1600 compression operation performed with
122dfc9fa91SStephan Mueller 	 * the crypto_shash_final.
123dfc9fa91SStephan Mueller 	 */
124dfc9fa91SStephan Mueller 	for (j = 0; j < hash_loop_cnt; j++) {
125dfc9fa91SStephan Mueller 		ret = crypto_shash_init(desc) ?:
126dfc9fa91SStephan Mueller 		      crypto_shash_update(desc, intermediary,
127dfc9fa91SStephan Mueller 					  sizeof(intermediary)) ?:
128dfc9fa91SStephan Mueller 		      crypto_shash_finup(desc, addtl, addtl_len, intermediary);
129dfc9fa91SStephan Mueller 		if (ret)
130dfc9fa91SStephan Mueller 			goto err;
131dfc9fa91SStephan Mueller 	}
132dfc9fa91SStephan Mueller 
133dfc9fa91SStephan Mueller 	/*
134dfc9fa91SStephan Mueller 	 * Inject the data from the previous loop into the pool. This data is
135764428feSStephan Müller 	 * not considered to contain any entropy, but it stirs the pool a bit.
136dfc9fa91SStephan Mueller 	 */
137764428feSStephan Müller 	ret = crypto_shash_update(desc, intermediary, sizeof(intermediary));
138*3fde2fe9SStephan Müller 	if (ret)
139*3fde2fe9SStephan Müller 		goto err;
140*3fde2fe9SStephan Müller 
141*3fde2fe9SStephan Müller 	/*
142*3fde2fe9SStephan Müller 	 * Insert the time stamp into the hash context representing the pool.
143*3fde2fe9SStephan Müller 	 *
144*3fde2fe9SStephan Müller 	 * If the time stamp is stuck, do not finally insert the value into the
145*3fde2fe9SStephan Müller 	 * entropy pool. Although this operation should not do any harm even
146*3fde2fe9SStephan Müller 	 * when the time stamp has no entropy, SP800-90B requires that any
147*3fde2fe9SStephan Müller 	 * conditioning operation to have an identical amount of input data
148764428feSStephan Müller 	 * according to section 3.1.5.
149*3fde2fe9SStephan Müller 	 */
150*3fde2fe9SStephan Müller 	if (!stuck) {
151*3fde2fe9SStephan Müller 		ret = crypto_shash_update(hash_state_desc, (u8 *)&time,
152*3fde2fe9SStephan Müller 					  sizeof(__u64));
153*3fde2fe9SStephan Müller 	}
154764428feSStephan Müller 
155*3fde2fe9SStephan Müller err:
156*3fde2fe9SStephan Müller 	shash_desc_zero(desc);
157764428feSStephan Müller 	memzero_explicit(intermediary, sizeof(intermediary));
158764428feSStephan Müller 
159764428feSStephan Müller 	return ret;
160dfc9fa91SStephan Mueller }
161dfc9fa91SStephan Mueller 
jent_read_random_block(void * hash_state,char * dst,unsigned int dst_len)162dfc9fa91SStephan Mueller int jent_read_random_block(void *hash_state, char *dst, unsigned int dst_len)
163dfc9fa91SStephan Mueller {
164dfc9fa91SStephan Mueller 	struct shash_desc *hash_state_desc = (struct shash_desc *)hash_state;
165dfc9fa91SStephan Mueller 	u8 jent_block[SHA3_256_DIGEST_SIZE];
166dfc9fa91SStephan Mueller 	/* Obtain data from entropy pool and re-initialize it */
167dfc9fa91SStephan Mueller 	int ret = crypto_shash_final(hash_state_desc, jent_block) ?:
168dfc9fa91SStephan Mueller 		  crypto_shash_init(hash_state_desc) ?:
169dfc9fa91SStephan Mueller 		  crypto_shash_update(hash_state_desc, jent_block,
170dfc9fa91SStephan Mueller 				      sizeof(jent_block));
171dfc9fa91SStephan Mueller 
172dfc9fa91SStephan Mueller 	if (!ret && dst_len)
173dfc9fa91SStephan Mueller 		memcpy(dst, jent_block, dst_len);
174dfc9fa91SStephan Mueller 
175dfc9fa91SStephan Mueller 	memzero_explicit(jent_block, sizeof(jent_block));
176dfc9fa91SStephan Mueller 	return ret;
177dfc9fa91SStephan Mueller }
178dfc9fa91SStephan Mueller 
179dfc9fa91SStephan Mueller /***************************************************************************
180dfc9fa91SStephan Mueller  * Kernel crypto API interface
181dfc9fa91SStephan Mueller  ***************************************************************************/
182dfc9fa91SStephan Mueller 
183dfc9fa91SStephan Mueller struct jitterentropy {
184dfc9fa91SStephan Mueller 	spinlock_t jent_lock;
185dfc9fa91SStephan Mueller 	struct rand_data *entropy_collector;
186dfc9fa91SStephan Mueller 	struct crypto_shash *tfm;
187dfc9fa91SStephan Mueller 	struct shash_desc *sdesc;
188dfc9fa91SStephan Mueller };
189dfc9fa91SStephan Mueller 
jent_kcapi_cleanup(struct crypto_tfm * tfm)190dfc9fa91SStephan Mueller static void jent_kcapi_cleanup(struct crypto_tfm *tfm)
191dfc9fa91SStephan Mueller {
192dfc9fa91SStephan Mueller 	struct jitterentropy *rng = crypto_tfm_ctx(tfm);
193*3fde2fe9SStephan Müller 
194*3fde2fe9SStephan Müller 	spin_lock(&rng->jent_lock);
195*3fde2fe9SStephan Müller 
196*3fde2fe9SStephan Müller 	if (rng->sdesc) {
197dfc9fa91SStephan Mueller 		shash_desc_zero(rng->sdesc);
198dfc9fa91SStephan Mueller 		kfree(rng->sdesc);
199dfc9fa91SStephan Mueller 	}
200dfc9fa91SStephan Mueller 	rng->sdesc = NULL;
201dfc9fa91SStephan Mueller 
202dfc9fa91SStephan Mueller 	if (rng->tfm)
203dfc9fa91SStephan Mueller 		crypto_free_shash(rng->tfm);
204dfc9fa91SStephan Mueller 	rng->tfm = NULL;
205dfc9fa91SStephan Mueller 
206dfc9fa91SStephan Mueller 	if (rng->entropy_collector)
207dfc9fa91SStephan Mueller 		jent_entropy_collector_free(rng->entropy_collector);
2089c5b34c2SEric Biggers 	rng->entropy_collector = NULL;
209dfc9fa91SStephan Mueller 	spin_unlock(&rng->jent_lock);
210dfc9fa91SStephan Mueller }
211dfc9fa91SStephan Mueller 
jent_kcapi_init(struct crypto_tfm * tfm)212dfc9fa91SStephan Mueller static int jent_kcapi_init(struct crypto_tfm *tfm)
213dfc9fa91SStephan Mueller {
214dfc9fa91SStephan Mueller 	struct jitterentropy *rng = crypto_tfm_ctx(tfm);
215 	struct crypto_shash *hash;
216 	struct shash_desc *sdesc;
217 	int size, ret = 0;
218 
219 	spin_lock_init(&rng->jent_lock);
220 
221 	/*
222 	 * Use SHA3-256 as conditioner. We allocate only the generic
223 	 * implementation as we are not interested in high-performance. The
224 	 * execution time of the SHA3 operation is measured and adds to the
225 	 * Jitter RNG's unpredictable behavior. If we have a slower hash
226 	 * implementation, the execution timing variations are larger. When
227 	 * using a fast implementation, we would need to call it more often
228 	 * as its variations are lower.
229 	 */
230 	hash = crypto_alloc_shash(JENT_CONDITIONING_HASH, 0, 0);
231 	if (IS_ERR(hash)) {
232 		pr_err("Cannot allocate conditioning digest\n");
233 		return PTR_ERR(hash);
234 	}
235 	rng->tfm = hash;
236 
237 	size = sizeof(struct shash_desc) + crypto_shash_descsize(hash);
238 	sdesc = kmalloc(size, GFP_KERNEL);
239 	if (!sdesc) {
240 		ret = -ENOMEM;
241 		goto err;
242 	}
243 
244 	sdesc->tfm = hash;
245 	crypto_shash_init(sdesc);
246 	rng->sdesc = sdesc;
247 
248 	rng->entropy_collector = jent_entropy_collector_alloc(1, 0, sdesc);
249 	if (!rng->entropy_collector) {
250 		ret = -ENOMEM;
251 		goto err;
252 	}
253 
254 	spin_lock_init(&rng->jent_lock);
255 	return 0;
256 
257 err:
258 	jent_kcapi_cleanup(tfm);
259 	return ret;
260 }
261 
jent_kcapi_random(struct crypto_rng * tfm,const u8 * src,unsigned int slen,u8 * rdata,unsigned int dlen)262 static int jent_kcapi_random(struct crypto_rng *tfm,
263 			     const u8 *src, unsigned int slen,
264 			     u8 *rdata, unsigned int dlen)
265 {
266 	struct jitterentropy *rng = crypto_rng_ctx(tfm);
267 	int ret = 0;
268 
269 	spin_lock(&rng->jent_lock);
270 
271 	ret = jent_read_entropy(rng->entropy_collector, rdata, dlen);
272 
273 	if (ret == -3) {
274 		/* Handle permanent health test error */
275 		/*
276 		 * If the kernel was booted with fips=1, it implies that
277 		 * the entire kernel acts as a FIPS 140 module. In this case
278 		 * an SP800-90B permanent health test error is treated as
279 		 * a FIPS module error.
280 		 */
281 		if (fips_enabled)
282 			panic("Jitter RNG permanent health test failure\n");
283 
284 		pr_err("Jitter RNG permanent health test failure\n");
285 		ret = -EFAULT;
286 	} else if (ret == -2) {
287 		/* Handle intermittent health test error */
288 		pr_warn_ratelimited("Reset Jitter RNG due to intermittent health test failure\n");
289 		ret = -EAGAIN;
290 	} else if (ret == -1) {
291 		/* Handle other errors */
292 		ret = -EINVAL;
293 	}
294 
295 	spin_unlock(&rng->jent_lock);
296 
297 	return ret;
298 }
299 
jent_kcapi_reset(struct crypto_rng * tfm,const u8 * seed,unsigned int slen)300 static int jent_kcapi_reset(struct crypto_rng *tfm,
301 			    const u8 *seed, unsigned int slen)
302 {
303 	return 0;
304 }
305 
306 static struct rng_alg jent_alg = {
307 	.generate		= jent_kcapi_random,
308 	.seed			= jent_kcapi_reset,
309 	.seedsize		= 0,
310 	.base			= {
311 		.cra_name               = "jitterentropy_rng",
312 		.cra_driver_name        = "jitterentropy_rng",
313 		.cra_priority           = 100,
314 		.cra_ctxsize            = sizeof(struct jitterentropy),
315 		.cra_module             = THIS_MODULE,
316 		.cra_init               = jent_kcapi_init,
317 		.cra_exit               = jent_kcapi_cleanup,
318 	}
319 };
320 
jent_mod_init(void)321 static int __init jent_mod_init(void)
322 {
323 	SHASH_DESC_ON_STACK(desc, tfm);
324 	struct crypto_shash *tfm;
325 	int ret = 0;
326 
327 	jent_testing_init();
328 
329 	tfm = crypto_alloc_shash(JENT_CONDITIONING_HASH, 0, 0);
330 	if (IS_ERR(tfm)) {
331 		jent_testing_exit();
332 		return PTR_ERR(tfm);
333 	}
334 
335 	desc->tfm = tfm;
336 	crypto_shash_init(desc);
337 	ret = jent_entropy_init(desc);
338 	shash_desc_zero(desc);
339 	crypto_free_shash(tfm);
340 	if (ret) {
341 		/* Handle permanent health test error */
342 		if (fips_enabled)
343 			panic("jitterentropy: Initialization failed with host not compliant with requirements: %d\n", ret);
344 
345 		jent_testing_exit();
346 		pr_info("jitterentropy: Initialization failed with host not compliant with requirements: %d\n", ret);
347 		return -EFAULT;
348 	}
349 	return crypto_register_rng(&jent_alg);
350 }
351 
jent_mod_exit(void)352 static void __exit jent_mod_exit(void)
353 {
354 	jent_testing_exit();
355 	crypto_unregister_rng(&jent_alg);
356 }
357 
358 module_init(jent_mod_init);
359 module_exit(jent_mod_exit);
360 
361 MODULE_LICENSE("Dual BSD/GPL");
362 MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
363 MODULE_DESCRIPTION("Non-physical True Random Number Generator based on CPU Jitter");
364 MODULE_ALIAS_CRYPTO("jitterentropy_rng");
365