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