1dfc9fa91SStephan Mueller /*
2dfc9fa91SStephan Mueller  * Non-physical true random number generator based on timing jitter --
3dfc9fa91SStephan Mueller  * Linux Kernel Crypto API specific code
4dfc9fa91SStephan Mueller  *
5bb897c55SStephan Müller  * 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 
40bb897c55SStephan Müller #include <crypto/hash.h>
41bb897c55SStephan Müller #include <crypto/sha3.h>
423fde2fe9SStephan Müller #include <linux/fips.h>
430c3dc787SHerbert Xu #include <linux/kernel.h>
44dfc9fa91SStephan Mueller #include <linux/module.h>
45dfc9fa91SStephan Mueller #include <linux/slab.h>
46dfc9fa91SStephan Mueller #include <linux/time.h>
47dfc9fa91SStephan Mueller #include <crypto/internal/rng.h>
48dfc9fa91SStephan Mueller 
49965d7286SBen Dooks #include "jitterentropy.h"
50dfc9fa91SStephan Mueller 
51bb897c55SStephan Müller #define JENT_CONDITIONING_HASH	"sha3-256-generic"
52bb897c55SStephan Müller 
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);
60dfc9fa91SStephan Mueller }
61dfc9fa91SStephan Mueller 
jent_zfree(void * ptr)62dfc9fa91SStephan Mueller void jent_zfree(void *ptr)
63dfc9fa91SStephan Mueller {
64453431a5SWaiman Long 	kfree_sensitive(ptr);
65dfc9fa91SStephan Mueller }
66dfc9fa91SStephan Mueller 
67b578456cSStephan 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)76dfc9fa91SStephan 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 	/*
83b578456cSStephan 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.
85dfc9fa91SStephan Mueller 	 * hoping that there are timers we can work with.
86dfc9fa91SStephan Mueller 	 */
87b578456cSStephan Mueller 	if (tmp == 0)
88b578456cSStephan Mueller 		tmp = ktime_get_ns();
89dfc9fa91SStephan Mueller 
90dfc9fa91SStephan Mueller 	*out = tmp;
91*69f1c387SStephan Müller 	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)94bb897c55SStephan Müller int jent_hash_time(void *hash_state, __u64 time, u8 *addtl,
95bb897c55SStephan Müller 		   unsigned int addtl_len, __u64 hash_loop_cnt,
96bb897c55SStephan Müller 		   unsigned int stuck)
97bb897c55SStephan Müller {
98bb897c55SStephan Müller 	struct shash_desc *hash_state_desc = (struct shash_desc *)hash_state;
99bb897c55SStephan Müller 	SHASH_DESC_ON_STACK(desc, hash_state_desc->tfm);
100bb897c55SStephan Müller 	u8 intermediary[SHA3_256_DIGEST_SIZE];
101bb897c55SStephan Müller 	__u64 j = 0;
102bb897c55SStephan Müller 	int ret;
103bb897c55SStephan Müller 
104bb897c55SStephan Müller 	desc->tfm = hash_state_desc->tfm;
105bb897c55SStephan Müller 
106bb897c55SStephan Müller 	if (sizeof(intermediary) != crypto_shash_digestsize(desc->tfm)) {
107bb897c55SStephan Müller 		pr_warn_ratelimited("Unexpected digest size\n");
108bb897c55SStephan Müller 		return -EINVAL;
109bb897c55SStephan Müller 	}
110bb897c55SStephan Müller 
111bb897c55SStephan Müller 	/*
112bb897c55SStephan Müller 	 * This loop fills a buffer which is injected into the entropy pool.
113bb897c55SStephan Müller 	 * The main reason for this loop is to execute something over which we
114bb897c55SStephan Müller 	 * can perform a timing measurement. The injection of the resulting
115bb897c55SStephan Müller 	 * data into the pool is performed to ensure the result is used and
116bb897c55SStephan Müller 	 * the compiler cannot optimize the loop away in case the result is not
117bb897c55SStephan Müller 	 * used at all. Yet that data is considered "additional information"
118bb897c55SStephan Müller 	 * considering the terminology from SP800-90A without any entropy.
119bb897c55SStephan Müller 	 *
120bb897c55SStephan Müller 	 * Note, it does not matter which or how much data you inject, we are
121bb897c55SStephan Müller 	 * interested in one Keccack1600 compression operation performed with
122bb897c55SStephan Müller 	 * the crypto_shash_final.
123bb897c55SStephan Müller 	 */
124bb897c55SStephan Müller 	for (j = 0; j < hash_loop_cnt; j++) {
125bb897c55SStephan Müller 		ret = crypto_shash_init(desc) ?:
126bb897c55SStephan Müller 		      crypto_shash_update(desc, intermediary,
127bb897c55SStephan Müller 					  sizeof(intermediary)) ?:
128bb897c55SStephan Müller 		      crypto_shash_finup(desc, addtl, addtl_len, intermediary);
129bb897c55SStephan Müller 		if (ret)
130bb897c55SStephan Müller 			goto err;
131bb897c55SStephan Müller 	}
132bb897c55SStephan Müller 
133bb897c55SStephan Müller 	/*
134bb897c55SStephan Müller 	 * Inject the data from the previous loop into the pool. This data is
135bb897c55SStephan Müller 	 * not considered to contain any entropy, but it stirs the pool a bit.
136bb897c55SStephan Müller 	 */
137bb897c55SStephan Müller 	ret = crypto_shash_update(desc, intermediary, sizeof(intermediary));
138bb897c55SStephan Müller 	if (ret)
139bb897c55SStephan Müller 		goto err;
140bb897c55SStephan Müller 
141bb897c55SStephan Müller 	/*
142bb897c55SStephan Müller 	 * Insert the time stamp into the hash context representing the pool.
143bb897c55SStephan Müller 	 *
144bb897c55SStephan Müller 	 * If the time stamp is stuck, do not finally insert the value into the
145bb897c55SStephan Müller 	 * entropy pool. Although this operation should not do any harm even
146bb897c55SStephan Müller 	 * when the time stamp has no entropy, SP800-90B requires that any
147bb897c55SStephan Müller 	 * conditioning operation to have an identical amount of input data
148bb897c55SStephan Müller 	 * according to section 3.1.5.
149bb897c55SStephan Müller 	 */
150bb897c55SStephan Müller 	if (!stuck) {
151bb897c55SStephan Müller 		ret = crypto_shash_update(hash_state_desc, (u8 *)&time,
152bb897c55SStephan Müller 					  sizeof(__u64));
153bb897c55SStephan Müller 	}
154bb897c55SStephan Müller 
155bb897c55SStephan Müller err:
156bb897c55SStephan Müller 	shash_desc_zero(desc);
157bb897c55SStephan Müller 	memzero_explicit(intermediary, sizeof(intermediary));
158bb897c55SStephan Müller 
159bb897c55SStephan Müller 	return ret;
160bb897c55SStephan Müller }
161bb897c55SStephan Müller 
jent_read_random_block(void * hash_state,char * dst,unsigned int dst_len)162bb897c55SStephan Müller int jent_read_random_block(void *hash_state, char *dst, unsigned int dst_len)
163bb897c55SStephan Müller {
164bb897c55SStephan Müller 	struct shash_desc *hash_state_desc = (struct shash_desc *)hash_state;
165bb897c55SStephan Müller 	u8 jent_block[SHA3_256_DIGEST_SIZE];
166bb897c55SStephan Müller 	/* Obtain data from entropy pool and re-initialize it */
167bb897c55SStephan Müller 	int ret = crypto_shash_final(hash_state_desc, jent_block) ?:
168bb897c55SStephan Müller 		  crypto_shash_init(hash_state_desc) ?:
169bb897c55SStephan Müller 		  crypto_shash_update(hash_state_desc, jent_block,
170bb897c55SStephan Müller 				      sizeof(jent_block));
171bb897c55SStephan Müller 
172bb897c55SStephan Müller 	if (!ret && dst_len)
173bb897c55SStephan Müller 		memcpy(dst, jent_block, dst_len);
174bb897c55SStephan Müller 
175bb897c55SStephan Müller 	memzero_explicit(jent_block, sizeof(jent_block));
176bb897c55SStephan Müller 	return ret;
177bb897c55SStephan Müller }
178bb897c55SStephan Müller 
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;
186bb897c55SStephan Müller 	struct crypto_shash *tfm;
187bb897c55SStephan Müller 	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);
193dfc9fa91SStephan Mueller 
194dfc9fa91SStephan Mueller 	spin_lock(&rng->jent_lock);
195bb897c55SStephan Müller 
196bb897c55SStephan Müller 	if (rng->sdesc) {
197bb897c55SStephan Müller 		shash_desc_zero(rng->sdesc);
198bb897c55SStephan Müller 		kfree(rng->sdesc);
199bb897c55SStephan Müller 	}
200bb897c55SStephan Müller 	rng->sdesc = NULL;
201bb897c55SStephan Müller 
202bb897c55SStephan Müller 	if (rng->tfm)
203bb897c55SStephan Müller 		crypto_free_shash(rng->tfm);
204bb897c55SStephan Müller 	rng->tfm = NULL;
205bb897c55SStephan Müller 
206dfc9fa91SStephan Mueller 	if (rng->entropy_collector)
207dfc9fa91SStephan Mueller 		jent_entropy_collector_free(rng->entropy_collector);
208dfc9fa91SStephan Mueller 	rng->entropy_collector = NULL;
209dfc9fa91SStephan Mueller 	spin_unlock(&rng->jent_lock);
210dfc9fa91SStephan Mueller }
211dfc9fa91SStephan Mueller 
jent_kcapi_init(struct crypto_tfm * tfm)212bb897c55SStephan Müller static int jent_kcapi_init(struct crypto_tfm *tfm)
213bb897c55SStephan Müller {
214bb897c55SStephan Müller 	struct jitterentropy *rng = crypto_tfm_ctx(tfm);
215bb897c55SStephan Müller 	struct crypto_shash *hash;
216bb897c55SStephan Müller 	struct shash_desc *sdesc;
217bb897c55SStephan Müller 	int size, ret = 0;
218bb897c55SStephan Müller 
219bb897c55SStephan Müller 	spin_lock_init(&rng->jent_lock);
220bb897c55SStephan Müller 
221bb897c55SStephan Müller 	/*
222bb897c55SStephan Müller 	 * Use SHA3-256 as conditioner. We allocate only the generic
223bb897c55SStephan Müller 	 * implementation as we are not interested in high-performance. The
224bb897c55SStephan Müller 	 * execution time of the SHA3 operation is measured and adds to the
225bb897c55SStephan Müller 	 * Jitter RNG's unpredictable behavior. If we have a slower hash
226bb897c55SStephan Müller 	 * implementation, the execution timing variations are larger. When
227bb897c55SStephan Müller 	 * using a fast implementation, we would need to call it more often
228bb897c55SStephan Müller 	 * as its variations are lower.
229bb897c55SStephan Müller 	 */
230bb897c55SStephan Müller 	hash = crypto_alloc_shash(JENT_CONDITIONING_HASH, 0, 0);
231bb897c55SStephan Müller 	if (IS_ERR(hash)) {
232bb897c55SStephan Müller 		pr_err("Cannot allocate conditioning digest\n");
233bb897c55SStephan Müller 		return PTR_ERR(hash);
234bb897c55SStephan Müller 	}
235bb897c55SStephan Müller 	rng->tfm = hash;
236bb897c55SStephan Müller 
237bb897c55SStephan Müller 	size = sizeof(struct shash_desc) + crypto_shash_descsize(hash);
238bb897c55SStephan Müller 	sdesc = kmalloc(size, GFP_KERNEL);
239bb897c55SStephan Müller 	if (!sdesc) {
240bb897c55SStephan Müller 		ret = -ENOMEM;
241bb897c55SStephan Müller 		goto err;
242bb897c55SStephan Müller 	}
243bb897c55SStephan Müller 
244bb897c55SStephan Müller 	sdesc->tfm = hash;
245bb897c55SStephan Müller 	crypto_shash_init(sdesc);
246bb897c55SStephan Müller 	rng->sdesc = sdesc;
247bb897c55SStephan Müller 
248bb897c55SStephan Müller 	rng->entropy_collector = jent_entropy_collector_alloc(1, 0, sdesc);
249bb897c55SStephan Müller 	if (!rng->entropy_collector) {
250bb897c55SStephan Müller 		ret = -ENOMEM;
251bb897c55SStephan Müller 		goto err;
252bb897c55SStephan Müller 	}
253bb897c55SStephan Müller 
254bb897c55SStephan Müller 	spin_lock_init(&rng->jent_lock);
255bb897c55SStephan Müller 	return 0;
256bb897c55SStephan Müller 
257bb897c55SStephan Müller err:
258bb897c55SStephan Müller 	jent_kcapi_cleanup(tfm);
259bb897c55SStephan Müller 	return ret;
260bb897c55SStephan Müller }
261bb897c55SStephan Müller 
jent_kcapi_random(struct crypto_rng * tfm,const u8 * src,unsigned int slen,u8 * rdata,unsigned int dlen)262dfc9fa91SStephan Mueller static int jent_kcapi_random(struct crypto_rng *tfm,
263dfc9fa91SStephan Mueller 			     const u8 *src, unsigned int slen,
264dfc9fa91SStephan Mueller 			     u8 *rdata, unsigned int dlen)
265dfc9fa91SStephan Mueller {
266dfc9fa91SStephan Mueller 	struct jitterentropy *rng = crypto_rng_ctx(tfm);
267dfc9fa91SStephan Mueller 	int ret = 0;
268dfc9fa91SStephan Mueller 
269dfc9fa91SStephan Mueller 	spin_lock(&rng->jent_lock);
270764428feSStephan Müller 
271dfc9fa91SStephan Mueller 	ret = jent_read_entropy(rng->entropy_collector, rdata, dlen);
272764428feSStephan Müller 
2733fde2fe9SStephan Müller 	if (ret == -3) {
2743fde2fe9SStephan Müller 		/* Handle permanent health test error */
2753fde2fe9SStephan Müller 		/*
2763fde2fe9SStephan Müller 		 * If the kernel was booted with fips=1, it implies that
2773fde2fe9SStephan Müller 		 * the entire kernel acts as a FIPS 140 module. In this case
2783fde2fe9SStephan Müller 		 * an SP800-90B permanent health test error is treated as
2793fde2fe9SStephan Müller 		 * a FIPS module error.
2803fde2fe9SStephan Müller 		 */
2813fde2fe9SStephan Müller 		if (fips_enabled)
2823fde2fe9SStephan Müller 			panic("Jitter RNG permanent health test failure\n");
283764428feSStephan Müller 
2843fde2fe9SStephan Müller 		pr_err("Jitter RNG permanent health test failure\n");
2853fde2fe9SStephan Müller 		ret = -EFAULT;
2863fde2fe9SStephan Müller 	} else if (ret == -2) {
2873fde2fe9SStephan Müller 		/* Handle intermittent health test error */
2883fde2fe9SStephan Müller 		pr_warn_ratelimited("Reset Jitter RNG due to intermittent health test failure\n");
289764428feSStephan Müller 		ret = -EAGAIN;
2903fde2fe9SStephan Müller 	} else if (ret == -1) {
2913fde2fe9SStephan Müller 		/* Handle other errors */
292764428feSStephan Müller 		ret = -EINVAL;
293764428feSStephan Müller 	}
294764428feSStephan Müller 
295dfc9fa91SStephan Mueller 	spin_unlock(&rng->jent_lock);
296dfc9fa91SStephan Mueller 
297dfc9fa91SStephan Mueller 	return ret;
298dfc9fa91SStephan Mueller }
299dfc9fa91SStephan Mueller 
jent_kcapi_reset(struct crypto_rng * tfm,const u8 * seed,unsigned int slen)300dfc9fa91SStephan Mueller static int jent_kcapi_reset(struct crypto_rng *tfm,
301dfc9fa91SStephan Mueller 			    const u8 *seed, unsigned int slen)
302dfc9fa91SStephan Mueller {
303dfc9fa91SStephan Mueller 	return 0;
304dfc9fa91SStephan Mueller }
305dfc9fa91SStephan Mueller 
306dfc9fa91SStephan Mueller static struct rng_alg jent_alg = {
307dfc9fa91SStephan Mueller 	.generate		= jent_kcapi_random,
308dfc9fa91SStephan Mueller 	.seed			= jent_kcapi_reset,
309dfc9fa91SStephan Mueller 	.seedsize		= 0,
310dfc9fa91SStephan Mueller 	.base			= {
311dfc9fa91SStephan Mueller 		.cra_name               = "jitterentropy_rng",
312dfc9fa91SStephan Mueller 		.cra_driver_name        = "jitterentropy_rng",
313dfc9fa91SStephan Mueller 		.cra_priority           = 100,
314dfc9fa91SStephan Mueller 		.cra_ctxsize            = sizeof(struct jitterentropy),
315dfc9fa91SStephan Mueller 		.cra_module             = THIS_MODULE,
316dfc9fa91SStephan Mueller 		.cra_init               = jent_kcapi_init,
317dfc9fa91SStephan Mueller 		.cra_exit               = jent_kcapi_cleanup,
318dfc9fa91SStephan Mueller 	}
319dfc9fa91SStephan Mueller };
320dfc9fa91SStephan Mueller 
jent_mod_init(void)321dfc9fa91SStephan Mueller static int __init jent_mod_init(void)
322dfc9fa91SStephan Mueller {
323bb897c55SStephan Müller 	SHASH_DESC_ON_STACK(desc, tfm);
324bb897c55SStephan Müller 	struct crypto_shash *tfm;
325dfc9fa91SStephan Mueller 	int ret = 0;
326dfc9fa91SStephan Mueller 
327*69f1c387SStephan Müller 	jent_testing_init();
328*69f1c387SStephan Müller 
329bb897c55SStephan Müller 	tfm = crypto_alloc_shash(JENT_CONDITIONING_HASH, 0, 0);
330*69f1c387SStephan Müller 	if (IS_ERR(tfm)) {
331*69f1c387SStephan Müller 		jent_testing_exit();
332bb897c55SStephan Müller 		return PTR_ERR(tfm);
333*69f1c387SStephan Müller 	}
334bb897c55SStephan Müller 
335bb897c55SStephan Müller 	desc->tfm = tfm;
336bb897c55SStephan Müller 	crypto_shash_init(desc);
337bb897c55SStephan Müller 	ret = jent_entropy_init(desc);
338bb897c55SStephan Müller 	shash_desc_zero(desc);
339bb897c55SStephan Müller 	crypto_free_shash(tfm);
340dfc9fa91SStephan Mueller 	if (ret) {
3413fde2fe9SStephan Müller 		/* Handle permanent health test error */
3423fde2fe9SStephan Müller 		if (fips_enabled)
3433fde2fe9SStephan Müller 			panic("jitterentropy: Initialization failed with host not compliant with requirements: %d\n", ret);
3443fde2fe9SStephan Müller 
345*69f1c387SStephan Müller 		jent_testing_exit();
346dfc9fa91SStephan Mueller 		pr_info("jitterentropy: Initialization failed with host not compliant with requirements: %d\n", ret);
347dfc9fa91SStephan Mueller 		return -EFAULT;
348dfc9fa91SStephan Mueller 	}
349dfc9fa91SStephan Mueller 	return crypto_register_rng(&jent_alg);
350dfc9fa91SStephan Mueller }
351dfc9fa91SStephan Mueller 
jent_mod_exit(void)352dfc9fa91SStephan Mueller static void __exit jent_mod_exit(void)
353dfc9fa91SStephan Mueller {
354*69f1c387SStephan Müller 	jent_testing_exit();
355dfc9fa91SStephan Mueller 	crypto_unregister_rng(&jent_alg);
356dfc9fa91SStephan Mueller }
357dfc9fa91SStephan Mueller 
3589c5b34c2SEric Biggers module_init(jent_mod_init);
359dfc9fa91SStephan Mueller module_exit(jent_mod_exit);
360dfc9fa91SStephan Mueller 
361dfc9fa91SStephan Mueller MODULE_LICENSE("Dual BSD/GPL");
362dfc9fa91SStephan Mueller MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
363dfc9fa91SStephan Mueller MODULE_DESCRIPTION("Non-physical True Random Number Generator based on CPU Jitter");
364dfc9fa91SStephan Mueller MODULE_ALIAS_CRYPTO("jitterentropy_rng");
365