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 - 2023
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 <crypto/hash.h>
41#include <crypto/sha3.h>
42#include <linux/fips.h>
43#include <linux/kernel.h>
44#include <linux/module.h>
45#include <linux/slab.h>
46#include <linux/time.h>
47#include <crypto/internal/rng.h>
48
49#include "jitterentropy.h"
50
51#define JENT_CONDITIONING_HASH "sha3-256-generic"
52
53/***************************************************************************
54 * Helper function
55 ***************************************************************************/
56
57void *jent_kvzalloc(unsigned int len)
58{
59 return kvzalloc(len, GFP_KERNEL);
60}
61
62void jent_kvzfree(void *ptr, unsigned int len)
63{
64 kvfree_sensitive(addr: ptr, len);
65}
66
67void *jent_zalloc(unsigned int len)
68{
69 return kzalloc(len, GFP_KERNEL);
70}
71
72void jent_zfree(void *ptr)
73{
74 kfree_sensitive(objp: ptr);
75}
76
77/*
78 * Obtain a high-resolution time stamp value. The time stamp is used to measure
79 * the execution time of a given code path and its variations. Hence, the time
80 * stamp must have a sufficiently high resolution.
81 *
82 * Note, if the function returns zero because a given architecture does not
83 * implement a high-resolution time stamp, the RNG code's runtime test
84 * will detect it and will not produce output.
85 */
86void jent_get_nstime(__u64 *out)
87{
88 __u64 tmp = 0;
89
90 tmp = random_get_entropy();
91
92 /*
93 * If random_get_entropy does not return a value, i.e. it is not
94 * implemented for a given architecture, use a clock source.
95 * hoping that there are timers we can work with.
96 */
97 if (tmp == 0)
98 tmp = ktime_get_ns();
99
100 *out = tmp;
101 jent_raw_hires_entropy_store(value: tmp);
102}
103
104int jent_hash_time(void *hash_state, __u64 time, u8 *addtl,
105 unsigned int addtl_len, __u64 hash_loop_cnt,
106 unsigned int stuck)
107{
108 struct shash_desc *hash_state_desc = (struct shash_desc *)hash_state;
109 SHASH_DESC_ON_STACK(desc, hash_state_desc->tfm);
110 u8 intermediary[SHA3_256_DIGEST_SIZE];
111 __u64 j = 0;
112 int ret;
113
114 desc->tfm = hash_state_desc->tfm;
115
116 if (sizeof(intermediary) != crypto_shash_digestsize(tfm: desc->tfm)) {
117 pr_warn_ratelimited("Unexpected digest size\n");
118 return -EINVAL;
119 }
120 kmsan_unpoison_memory(address: intermediary, size: sizeof(intermediary));
121
122 /*
123 * This loop fills a buffer which is injected into the entropy pool.
124 * The main reason for this loop is to execute something over which we
125 * can perform a timing measurement. The injection of the resulting
126 * data into the pool is performed to ensure the result is used and
127 * the compiler cannot optimize the loop away in case the result is not
128 * used at all. Yet that data is considered "additional information"
129 * considering the terminology from SP800-90A without any entropy.
130 *
131 * Note, it does not matter which or how much data you inject, we are
132 * interested in one Keccack1600 compression operation performed with
133 * the crypto_shash_final.
134 */
135 for (j = 0; j < hash_loop_cnt; j++) {
136 ret = crypto_shash_init(desc) ?:
137 crypto_shash_update(desc, data: intermediary,
138 len: sizeof(intermediary)) ?:
139 crypto_shash_finup(desc, data: addtl, len: addtl_len, out: intermediary);
140 if (ret)
141 goto err;
142 }
143
144 /*
145 * Inject the data from the previous loop into the pool. This data is
146 * not considered to contain any entropy, but it stirs the pool a bit.
147 */
148 ret = crypto_shash_update(desc: hash_state_desc, data: intermediary, len: sizeof(intermediary));
149 if (ret)
150 goto err;
151
152 /*
153 * Insert the time stamp into the hash context representing the pool.
154 *
155 * If the time stamp is stuck, do not finally insert the value into the
156 * entropy pool. Although this operation should not do any harm even
157 * when the time stamp has no entropy, SP800-90B requires that any
158 * conditioning operation to have an identical amount of input data
159 * according to section 3.1.5.
160 */
161 if (stuck) {
162 time = 0;
163 }
164
165 ret = crypto_shash_update(desc: hash_state_desc, data: (u8 *)&time, len: sizeof(__u64));
166
167err:
168 shash_desc_zero(desc);
169 memzero_explicit(s: intermediary, count: sizeof(intermediary));
170
171 return ret;
172}
173
174int jent_read_random_block(void *hash_state, char *dst, unsigned int dst_len)
175{
176 struct shash_desc *hash_state_desc = (struct shash_desc *)hash_state;
177 u8 jent_block[SHA3_256_DIGEST_SIZE];
178 /* Obtain data from entropy pool and re-initialize it */
179 int ret = crypto_shash_final(desc: hash_state_desc, out: jent_block) ?:
180 crypto_shash_init(desc: hash_state_desc) ?:
181 crypto_shash_update(desc: hash_state_desc, data: jent_block,
182 len: sizeof(jent_block));
183
184 if (!ret && dst_len)
185 memcpy(to: dst, from: jent_block, len: dst_len);
186
187 memzero_explicit(s: jent_block, count: sizeof(jent_block));
188 return ret;
189}
190
191/***************************************************************************
192 * Kernel crypto API interface
193 ***************************************************************************/
194
195struct jitterentropy {
196 spinlock_t jent_lock;
197 struct rand_data *entropy_collector;
198 struct crypto_shash *tfm;
199 struct shash_desc *sdesc;
200};
201
202static void jent_kcapi_cleanup(struct crypto_tfm *tfm)
203{
204 struct jitterentropy *rng = crypto_tfm_ctx(tfm);
205
206 spin_lock(lock: &rng->jent_lock);
207
208 if (rng->sdesc) {
209 shash_desc_zero(desc: rng->sdesc);
210 kfree(objp: rng->sdesc);
211 }
212 rng->sdesc = NULL;
213
214 if (rng->tfm)
215 crypto_free_shash(tfm: rng->tfm);
216 rng->tfm = NULL;
217
218 if (rng->entropy_collector)
219 jent_entropy_collector_free(entropy_collector: rng->entropy_collector);
220 rng->entropy_collector = NULL;
221 spin_unlock(lock: &rng->jent_lock);
222}
223
224static int jent_kcapi_init(struct crypto_tfm *tfm)
225{
226 struct jitterentropy *rng = crypto_tfm_ctx(tfm);
227 struct crypto_shash *hash;
228 struct shash_desc *sdesc;
229 int size, ret = 0;
230
231 spin_lock_init(&rng->jent_lock);
232
233 /*
234 * Use SHA3-256 as conditioner. We allocate only the generic
235 * implementation as we are not interested in high-performance. The
236 * execution time of the SHA3 operation is measured and adds to the
237 * Jitter RNG's unpredictable behavior. If we have a slower hash
238 * implementation, the execution timing variations are larger. When
239 * using a fast implementation, we would need to call it more often
240 * as its variations are lower.
241 */
242 hash = crypto_alloc_shash(JENT_CONDITIONING_HASH, type: 0, mask: 0);
243 if (IS_ERR(ptr: hash)) {
244 pr_err("Cannot allocate conditioning digest\n");
245 return PTR_ERR(ptr: hash);
246 }
247 rng->tfm = hash;
248
249 size = sizeof(struct shash_desc) + crypto_shash_descsize(tfm: hash);
250 sdesc = kmalloc(size, GFP_KERNEL);
251 if (!sdesc) {
252 ret = -ENOMEM;
253 goto err;
254 }
255
256 sdesc->tfm = hash;
257 crypto_shash_init(desc: sdesc);
258 rng->sdesc = sdesc;
259
260 rng->entropy_collector =
261 jent_entropy_collector_alloc(CONFIG_CRYPTO_JITTERENTROPY_OSR, flags: 0,
262 hash_state: sdesc);
263 if (!rng->entropy_collector) {
264 ret = -ENOMEM;
265 goto err;
266 }
267
268 spin_lock_init(&rng->jent_lock);
269 return 0;
270
271err:
272 jent_kcapi_cleanup(tfm);
273 return ret;
274}
275
276static int jent_kcapi_random(struct crypto_rng *tfm,
277 const u8 *src, unsigned int slen,
278 u8 *rdata, unsigned int dlen)
279{
280 struct jitterentropy *rng = crypto_rng_ctx(tfm);
281 int ret = 0;
282
283 spin_lock(lock: &rng->jent_lock);
284
285 ret = jent_read_entropy(ec: rng->entropy_collector, data: rdata, len: dlen);
286
287 if (ret == -3) {
288 /* Handle permanent health test error */
289 /*
290 * If the kernel was booted with fips=1, it implies that
291 * the entire kernel acts as a FIPS 140 module. In this case
292 * an SP800-90B permanent health test error is treated as
293 * a FIPS module error.
294 */
295 if (fips_enabled)
296 panic(fmt: "Jitter RNG permanent health test failure\n");
297
298 pr_err("Jitter RNG permanent health test failure\n");
299 ret = -EFAULT;
300 } else if (ret == -2) {
301 /* Handle intermittent health test error */
302 pr_warn_ratelimited("Reset Jitter RNG due to intermittent health test failure\n");
303 ret = -EAGAIN;
304 } else if (ret == -1) {
305 /* Handle other errors */
306 ret = -EINVAL;
307 }
308
309 spin_unlock(lock: &rng->jent_lock);
310
311 return ret;
312}
313
314static int jent_kcapi_reset(struct crypto_rng *tfm,
315 const u8 *seed, unsigned int slen)
316{
317 return 0;
318}
319
320static struct rng_alg jent_alg = {
321 .generate = jent_kcapi_random,
322 .seed = jent_kcapi_reset,
323 .seedsize = 0,
324 .base = {
325 .cra_name = "jitterentropy_rng",
326 .cra_driver_name = "jitterentropy_rng",
327 .cra_priority = 100,
328 .cra_ctxsize = sizeof(struct jitterentropy),
329 .cra_module = THIS_MODULE,
330 .cra_init = jent_kcapi_init,
331 .cra_exit = jent_kcapi_cleanup,
332 }
333};
334
335static int __init jent_mod_init(void)
336{
337 SHASH_DESC_ON_STACK(desc, tfm);
338 struct crypto_shash *tfm;
339 int ret = 0;
340
341 jent_testing_init();
342
343 tfm = crypto_alloc_shash(JENT_CONDITIONING_HASH, type: 0, mask: 0);
344 if (IS_ERR(ptr: tfm)) {
345 jent_testing_exit();
346 return PTR_ERR(ptr: tfm);
347 }
348
349 desc->tfm = tfm;
350 crypto_shash_init(desc);
351 ret = jent_entropy_init(CONFIG_CRYPTO_JITTERENTROPY_OSR, flags: 0, hash_state: desc, NULL);
352 shash_desc_zero(desc);
353 crypto_free_shash(tfm);
354 if (ret) {
355 /* Handle permanent health test error */
356 if (fips_enabled)
357 panic(fmt: "jitterentropy: Initialization failed with host not compliant with requirements: %d\n", ret);
358
359 jent_testing_exit();
360 pr_info("jitterentropy: Initialization failed with host not compliant with requirements: %d\n", ret);
361 return -EFAULT;
362 }
363 return crypto_register_rng(alg: &jent_alg);
364}
365
366static void __exit jent_mod_exit(void)
367{
368 jent_testing_exit();
369 crypto_unregister_rng(alg: &jent_alg);
370}
371
372module_init(jent_mod_init);
373module_exit(jent_mod_exit);
374
375MODULE_LICENSE("Dual BSD/GPL");
376MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
377MODULE_DESCRIPTION("Non-physical True Random Number Generator based on CPU Jitter");
378MODULE_ALIAS_CRYPTO("jitterentropy_rng");
379