2 * Non-physical true random number generator based on timing jitter --
3 * Linux Kernel Crypto API specific code
5 * Copyright Stephan Mueller <smueller@chronox.de>, 2015 - 2023
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.)
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
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>
49 #include "jitterentropy.h"
51 #define JENT_CONDITIONING_HASH "sha3-256-generic"
53 /***************************************************************************
55 ***************************************************************************/
57 void *jent_zalloc(unsigned int len)
59 return kzalloc(len, GFP_KERNEL);
62 void jent_zfree(void *ptr)
68 * Obtain a high-resolution time stamp value. The time stamp is used to measure
69 * the execution time of a given code path and its variations. Hence, the time
70 * stamp must have a sufficiently high resolution.
72 * Note, if the function returns zero because a given architecture does not
73 * implement a high-resolution time stamp, the RNG code's runtime test
74 * will detect it and will not produce output.
76 void jent_get_nstime(__u64 *out)
80 tmp = random_get_entropy();
83 * If random_get_entropy does not return a value, i.e. it is not
84 * implemented for a given architecture, use a clock source.
85 * hoping that there are timers we can work with.
91 jent_raw_hires_entropy_store(tmp);
94 int jent_hash_time(void *hash_state, __u64 time, u8 *addtl,
95 unsigned int addtl_len, __u64 hash_loop_cnt,
98 struct shash_desc *hash_state_desc = (struct shash_desc *)hash_state;
99 SHASH_DESC_ON_STACK(desc, hash_state_desc->tfm);
100 u8 intermediary[SHA3_256_DIGEST_SIZE];
104 desc->tfm = hash_state_desc->tfm;
106 if (sizeof(intermediary) != crypto_shash_digestsize(desc->tfm)) {
107 pr_warn_ratelimited("Unexpected digest size\n");
112 * This loop fills a buffer which is injected into the entropy pool.
113 * The main reason for this loop is to execute something over which we
114 * can perform a timing measurement. The injection of the resulting
115 * data into the pool is performed to ensure the result is used and
116 * the compiler cannot optimize the loop away in case the result is not
117 * used at all. Yet that data is considered "additional information"
118 * considering the terminology from SP800-90A without any entropy.
120 * Note, it does not matter which or how much data you inject, we are
121 * interested in one Keccack1600 compression operation performed with
122 * the crypto_shash_final.
124 for (j = 0; j < hash_loop_cnt; j++) {
125 ret = crypto_shash_init(desc) ?:
126 crypto_shash_update(desc, intermediary,
127 sizeof(intermediary)) ?:
128 crypto_shash_finup(desc, addtl, addtl_len, intermediary);
134 * Inject the data from the previous loop into the pool. This data is
135 * not considered to contain any entropy, but it stirs the pool a bit.
137 ret = crypto_shash_update(desc, intermediary, sizeof(intermediary));
142 * Insert the time stamp into the hash context representing the pool.
144 * If the time stamp is stuck, do not finally insert the value into the
145 * entropy pool. Although this operation should not do any harm even
146 * when the time stamp has no entropy, SP800-90B requires that any
147 * conditioning operation to have an identical amount of input data
148 * according to section 3.1.5.
151 ret = crypto_shash_update(hash_state_desc, (u8 *)&time,
156 shash_desc_zero(desc);
157 memzero_explicit(intermediary, sizeof(intermediary));
162 int jent_read_random_block(void *hash_state, char *dst, unsigned int dst_len)
164 struct shash_desc *hash_state_desc = (struct shash_desc *)hash_state;
165 u8 jent_block[SHA3_256_DIGEST_SIZE];
166 /* Obtain data from entropy pool and re-initialize it */
167 int ret = crypto_shash_final(hash_state_desc, jent_block) ?:
168 crypto_shash_init(hash_state_desc) ?:
169 crypto_shash_update(hash_state_desc, jent_block,
173 memcpy(dst, jent_block, dst_len);
175 memzero_explicit(jent_block, sizeof(jent_block));
179 /***************************************************************************
180 * Kernel crypto API interface
181 ***************************************************************************/
183 struct jitterentropy {
184 spinlock_t jent_lock;
185 struct rand_data *entropy_collector;
186 struct crypto_shash *tfm;
187 struct shash_desc *sdesc;
190 static void jent_kcapi_cleanup(struct crypto_tfm *tfm)
192 struct jitterentropy *rng = crypto_tfm_ctx(tfm);
194 spin_lock(&rng->jent_lock);
197 shash_desc_zero(rng->sdesc);
203 crypto_free_shash(rng->tfm);
206 if (rng->entropy_collector)
207 jent_entropy_collector_free(rng->entropy_collector);
208 rng->entropy_collector = NULL;
209 spin_unlock(&rng->jent_lock);
212 static int jent_kcapi_init(struct crypto_tfm *tfm)
214 struct jitterentropy *rng = crypto_tfm_ctx(tfm);
215 struct crypto_shash *hash;
216 struct shash_desc *sdesc;
219 spin_lock_init(&rng->jent_lock);
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.
230 hash = crypto_alloc_shash(JENT_CONDITIONING_HASH, 0, 0);
232 pr_err("Cannot allocate conditioning digest\n");
233 return PTR_ERR(hash);
237 size = sizeof(struct shash_desc) + crypto_shash_descsize(hash);
238 sdesc = kmalloc(size, GFP_KERNEL);
245 crypto_shash_init(sdesc);
248 rng->entropy_collector = jent_entropy_collector_alloc(1, 0, sdesc);
249 if (!rng->entropy_collector) {
254 spin_lock_init(&rng->jent_lock);
258 jent_kcapi_cleanup(tfm);
262 static int jent_kcapi_random(struct crypto_rng *tfm,
263 const u8 *src, unsigned int slen,
264 u8 *rdata, unsigned int dlen)
266 struct jitterentropy *rng = crypto_rng_ctx(tfm);
269 spin_lock(&rng->jent_lock);
271 ret = jent_read_entropy(rng->entropy_collector, rdata, dlen);
274 /* Handle permanent health test error */
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.
282 panic("Jitter RNG permanent health test failure\n");
284 pr_err("Jitter RNG permanent health test failure\n");
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");
290 } else if (ret == -1) {
291 /* Handle other errors */
295 spin_unlock(&rng->jent_lock);
300 static int jent_kcapi_reset(struct crypto_rng *tfm,
301 const u8 *seed, unsigned int slen)
306 static struct rng_alg jent_alg = {
307 .generate = jent_kcapi_random,
308 .seed = jent_kcapi_reset,
311 .cra_name = "jitterentropy_rng",
312 .cra_driver_name = "jitterentropy_rng",
314 .cra_ctxsize = sizeof(struct jitterentropy),
315 .cra_module = THIS_MODULE,
316 .cra_init = jent_kcapi_init,
317 .cra_exit = jent_kcapi_cleanup,
321 static int __init jent_mod_init(void)
323 SHASH_DESC_ON_STACK(desc, tfm);
324 struct crypto_shash *tfm;
329 tfm = crypto_alloc_shash(JENT_CONDITIONING_HASH, 0, 0);
336 crypto_shash_init(desc);
337 ret = jent_entropy_init(desc);
338 shash_desc_zero(desc);
339 crypto_free_shash(tfm);
341 /* Handle permanent health test error */
343 panic("jitterentropy: Initialization failed with host not compliant with requirements: %d\n", ret);
346 pr_info("jitterentropy: Initialization failed with host not compliant with requirements: %d\n", ret);
349 return crypto_register_rng(&jent_alg);
352 static void __exit jent_mod_exit(void)
355 crypto_unregister_rng(&jent_alg);
358 module_init(jent_mod_init);
359 module_exit(jent_mod_exit);
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");