xref: /OK3568_Linux_fs/kernel/crypto/jitterentropy-kcapi.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Non-physical true random number generator based on timing jitter --
3*4882a593Smuzhiyun  * Linux Kernel Crypto API specific code
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright Stephan Mueller <smueller@chronox.de>, 2015
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Redistribution and use in source and binary forms, with or without
8*4882a593Smuzhiyun  * modification, are permitted provided that the following conditions
9*4882a593Smuzhiyun  * are met:
10*4882a593Smuzhiyun  * 1. Redistributions of source code must retain the above copyright
11*4882a593Smuzhiyun  *    notice, and the entire permission notice in its entirety,
12*4882a593Smuzhiyun  *    including the disclaimer of warranties.
13*4882a593Smuzhiyun  * 2. Redistributions in binary form must reproduce the above copyright
14*4882a593Smuzhiyun  *    notice, this list of conditions and the following disclaimer in the
15*4882a593Smuzhiyun  *    documentation and/or other materials provided with the distribution.
16*4882a593Smuzhiyun  * 3. The name of the author may not be used to endorse or promote
17*4882a593Smuzhiyun  *    products derived from this software without specific prior
18*4882a593Smuzhiyun  *    written permission.
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  * ALTERNATIVELY, this product may be distributed under the terms of
21*4882a593Smuzhiyun  * the GNU General Public License, in which case the provisions of the GPL2 are
22*4882a593Smuzhiyun  * required INSTEAD OF the above restrictions.  (This clause is
23*4882a593Smuzhiyun  * necessary due to a potential bad interaction between the GPL and
24*4882a593Smuzhiyun  * the restrictions contained in a BSD-style copyright.)
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
27*4882a593Smuzhiyun  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28*4882a593Smuzhiyun  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
29*4882a593Smuzhiyun  * WHICH ARE HEREBY DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE
30*4882a593Smuzhiyun  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31*4882a593Smuzhiyun  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
32*4882a593Smuzhiyun  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
33*4882a593Smuzhiyun  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34*4882a593Smuzhiyun  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35*4882a593Smuzhiyun  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
36*4882a593Smuzhiyun  * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
37*4882a593Smuzhiyun  * DAMAGE.
38*4882a593Smuzhiyun  */
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun #include <linux/kernel.h>
41*4882a593Smuzhiyun #include <linux/module.h>
42*4882a593Smuzhiyun #include <linux/slab.h>
43*4882a593Smuzhiyun #include <linux/fips.h>
44*4882a593Smuzhiyun #include <linux/time.h>
45*4882a593Smuzhiyun #include <crypto/internal/rng.h>
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun #include "jitterentropy.h"
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun /***************************************************************************
50*4882a593Smuzhiyun  * Helper function
51*4882a593Smuzhiyun  ***************************************************************************/
52*4882a593Smuzhiyun 
jent_zalloc(unsigned int len)53*4882a593Smuzhiyun void *jent_zalloc(unsigned int len)
54*4882a593Smuzhiyun {
55*4882a593Smuzhiyun 	return kzalloc(len, GFP_KERNEL);
56*4882a593Smuzhiyun }
57*4882a593Smuzhiyun 
jent_zfree(void * ptr)58*4882a593Smuzhiyun void jent_zfree(void *ptr)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun 	kfree_sensitive(ptr);
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun 
jent_fips_enabled(void)63*4882a593Smuzhiyun int jent_fips_enabled(void)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun 	return fips_enabled;
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun 
jent_panic(char * s)68*4882a593Smuzhiyun void jent_panic(char *s)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun 	panic("%s", s);
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun 
jent_memcpy(void * dest,const void * src,unsigned int n)73*4882a593Smuzhiyun void jent_memcpy(void *dest, const void *src, unsigned int n)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun 	memcpy(dest, src, n);
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun /*
79*4882a593Smuzhiyun  * Obtain a high-resolution time stamp value. The time stamp is used to measure
80*4882a593Smuzhiyun  * the execution time of a given code path and its variations. Hence, the time
81*4882a593Smuzhiyun  * stamp must have a sufficiently high resolution.
82*4882a593Smuzhiyun  *
83*4882a593Smuzhiyun  * Note, if the function returns zero because a given architecture does not
84*4882a593Smuzhiyun  * implement a high-resolution time stamp, the RNG code's runtime test
85*4882a593Smuzhiyun  * will detect it and will not produce output.
86*4882a593Smuzhiyun  */
jent_get_nstime(__u64 * out)87*4882a593Smuzhiyun void jent_get_nstime(__u64 *out)
88*4882a593Smuzhiyun {
89*4882a593Smuzhiyun 	__u64 tmp = 0;
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	tmp = random_get_entropy();
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	/*
94*4882a593Smuzhiyun 	 * If random_get_entropy does not return a value, i.e. it is not
95*4882a593Smuzhiyun 	 * implemented for a given architecture, use a clock source.
96*4882a593Smuzhiyun 	 * hoping that there are timers we can work with.
97*4882a593Smuzhiyun 	 */
98*4882a593Smuzhiyun 	if (tmp == 0)
99*4882a593Smuzhiyun 		tmp = ktime_get_ns();
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	*out = tmp;
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun /***************************************************************************
105*4882a593Smuzhiyun  * Kernel crypto API interface
106*4882a593Smuzhiyun  ***************************************************************************/
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun struct jitterentropy {
109*4882a593Smuzhiyun 	spinlock_t jent_lock;
110*4882a593Smuzhiyun 	struct rand_data *entropy_collector;
111*4882a593Smuzhiyun 	unsigned int reset_cnt;
112*4882a593Smuzhiyun };
113*4882a593Smuzhiyun 
jent_kcapi_init(struct crypto_tfm * tfm)114*4882a593Smuzhiyun static int jent_kcapi_init(struct crypto_tfm *tfm)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun 	struct jitterentropy *rng = crypto_tfm_ctx(tfm);
117*4882a593Smuzhiyun 	int ret = 0;
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	rng->entropy_collector = jent_entropy_collector_alloc(1, 0);
120*4882a593Smuzhiyun 	if (!rng->entropy_collector)
121*4882a593Smuzhiyun 		ret = -ENOMEM;
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	spin_lock_init(&rng->jent_lock);
124*4882a593Smuzhiyun 	return ret;
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun 
jent_kcapi_cleanup(struct crypto_tfm * tfm)127*4882a593Smuzhiyun static void jent_kcapi_cleanup(struct crypto_tfm *tfm)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun 	struct jitterentropy *rng = crypto_tfm_ctx(tfm);
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	spin_lock(&rng->jent_lock);
132*4882a593Smuzhiyun 	if (rng->entropy_collector)
133*4882a593Smuzhiyun 		jent_entropy_collector_free(rng->entropy_collector);
134*4882a593Smuzhiyun 	rng->entropy_collector = NULL;
135*4882a593Smuzhiyun 	spin_unlock(&rng->jent_lock);
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun 
jent_kcapi_random(struct crypto_rng * tfm,const u8 * src,unsigned int slen,u8 * rdata,unsigned int dlen)138*4882a593Smuzhiyun static int jent_kcapi_random(struct crypto_rng *tfm,
139*4882a593Smuzhiyun 			     const u8 *src, unsigned int slen,
140*4882a593Smuzhiyun 			     u8 *rdata, unsigned int dlen)
141*4882a593Smuzhiyun {
142*4882a593Smuzhiyun 	struct jitterentropy *rng = crypto_rng_ctx(tfm);
143*4882a593Smuzhiyun 	int ret = 0;
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	spin_lock(&rng->jent_lock);
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	/* Return a permanent error in case we had too many resets in a row. */
148*4882a593Smuzhiyun 	if (rng->reset_cnt > (1<<10)) {
149*4882a593Smuzhiyun 		ret = -EFAULT;
150*4882a593Smuzhiyun 		goto out;
151*4882a593Smuzhiyun 	}
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	ret = jent_read_entropy(rng->entropy_collector, rdata, dlen);
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	/* Reset RNG in case of health failures */
156*4882a593Smuzhiyun 	if (ret < -1) {
157*4882a593Smuzhiyun 		pr_warn_ratelimited("Reset Jitter RNG due to health test failure: %s failure\n",
158*4882a593Smuzhiyun 				    (ret == -2) ? "Repetition Count Test" :
159*4882a593Smuzhiyun 						  "Adaptive Proportion Test");
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 		rng->reset_cnt++;
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 		ret = -EAGAIN;
164*4882a593Smuzhiyun 	} else {
165*4882a593Smuzhiyun 		rng->reset_cnt = 0;
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 		/* Convert the Jitter RNG error into a usable error code */
168*4882a593Smuzhiyun 		if (ret == -1)
169*4882a593Smuzhiyun 			ret = -EINVAL;
170*4882a593Smuzhiyun 	}
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun out:
173*4882a593Smuzhiyun 	spin_unlock(&rng->jent_lock);
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	return ret;
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun 
jent_kcapi_reset(struct crypto_rng * tfm,const u8 * seed,unsigned int slen)178*4882a593Smuzhiyun static int jent_kcapi_reset(struct crypto_rng *tfm,
179*4882a593Smuzhiyun 			    const u8 *seed, unsigned int slen)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun 	return 0;
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun static struct rng_alg jent_alg = {
185*4882a593Smuzhiyun 	.generate		= jent_kcapi_random,
186*4882a593Smuzhiyun 	.seed			= jent_kcapi_reset,
187*4882a593Smuzhiyun 	.seedsize		= 0,
188*4882a593Smuzhiyun 	.base			= {
189*4882a593Smuzhiyun 		.cra_name               = "jitterentropy_rng",
190*4882a593Smuzhiyun 		.cra_driver_name        = "jitterentropy_rng",
191*4882a593Smuzhiyun 		.cra_priority           = 100,
192*4882a593Smuzhiyun 		.cra_ctxsize            = sizeof(struct jitterentropy),
193*4882a593Smuzhiyun 		.cra_module             = THIS_MODULE,
194*4882a593Smuzhiyun 		.cra_init               = jent_kcapi_init,
195*4882a593Smuzhiyun 		.cra_exit               = jent_kcapi_cleanup,
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	}
198*4882a593Smuzhiyun };
199*4882a593Smuzhiyun 
jent_mod_init(void)200*4882a593Smuzhiyun static int __init jent_mod_init(void)
201*4882a593Smuzhiyun {
202*4882a593Smuzhiyun 	int ret = 0;
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	ret = jent_entropy_init();
205*4882a593Smuzhiyun 	if (ret) {
206*4882a593Smuzhiyun 		pr_info("jitterentropy: Initialization failed with host not compliant with requirements: %d\n", ret);
207*4882a593Smuzhiyun 		return -EFAULT;
208*4882a593Smuzhiyun 	}
209*4882a593Smuzhiyun 	return crypto_register_rng(&jent_alg);
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun 
jent_mod_exit(void)212*4882a593Smuzhiyun static void __exit jent_mod_exit(void)
213*4882a593Smuzhiyun {
214*4882a593Smuzhiyun 	crypto_unregister_rng(&jent_alg);
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun module_init(jent_mod_init);
218*4882a593Smuzhiyun module_exit(jent_mod_exit);
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun MODULE_LICENSE("Dual BSD/GPL");
221*4882a593Smuzhiyun MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
222*4882a593Smuzhiyun MODULE_DESCRIPTION("Non-physical True Random Number Generator based on CPU Jitter");
223*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("jitterentropy_rng");
224