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/sha3.h> 41 #include <linux/fips.h> 42 #include <linux/kernel.h> 43 #include <linux/module.h> 44 #include <linux/slab.h> 45 #include <linux/time.h> 46 #include <crypto/internal/rng.h> 47 48 #include "jitterentropy.h" 49 50 /*************************************************************************** 51 * Helper function 52 ***************************************************************************/ 53 54 void *jent_kvzalloc(unsigned int len) 55 { 56 return kvzalloc(len, GFP_KERNEL); 57 } 58 59 void jent_kvzfree(void *ptr, unsigned int len) 60 { 61 kvfree_sensitive(ptr, len); 62 } 63 64 void *jent_zalloc(unsigned int len) 65 { 66 return kzalloc(len, GFP_KERNEL); 67 } 68 69 void jent_zfree(void *ptr) 70 { 71 kfree_sensitive(ptr); 72 } 73 74 /* 75 * Obtain a high-resolution time stamp value. The time stamp is used to measure 76 * the execution time of a given code path and its variations. Hence, the time 77 * stamp must have a sufficiently high resolution. 78 * 79 * Note, if the function returns zero because a given architecture does not 80 * implement a high-resolution time stamp, the RNG code's runtime test 81 * will detect it and will not produce output. 82 */ 83 void jent_get_nstime(__u64 *out) 84 { 85 __u64 tmp = 0; 86 87 tmp = random_get_entropy(); 88 89 /* 90 * If random_get_entropy does not return a value, i.e. it is not 91 * implemented for a given architecture, use a clock source. 92 * hoping that there are timers we can work with. 93 */ 94 if (tmp == 0) 95 tmp = ktime_get_ns(); 96 97 *out = tmp; 98 jent_raw_hires_entropy_store(tmp); 99 } 100 101 void jent_hash_time(struct sha3_ctx *hash_state, __u64 time, u8 *addtl, 102 unsigned int addtl_len, __u64 hash_loop_cnt, 103 unsigned int stuck) 104 { 105 struct sha3_ctx tmp_state; /* zeroized by sha3_final() */ 106 u8 intermediary[SHA3_256_DIGEST_SIZE]; 107 __u64 j = 0; 108 109 kmsan_unpoison_memory(intermediary, sizeof(intermediary)); 110 111 /* 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. 119 * 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 sha3_final. 123 */ 124 for (j = 0; j < hash_loop_cnt; j++) { 125 sha3_256_init(&tmp_state); 126 sha3_update(&tmp_state, intermediary, sizeof(intermediary)); 127 sha3_update(&tmp_state, addtl, addtl_len); 128 sha3_final(&tmp_state, intermediary); 129 } 130 131 /* 132 * Inject the data from the previous loop into the pool. This data is 133 * not considered to contain any entropy, but it stirs the pool a bit. 134 */ 135 sha3_update(hash_state, intermediary, sizeof(intermediary)); 136 137 /* 138 * Insert the time stamp into the hash context representing the pool. 139 * 140 * If the time stamp is stuck, do not finally insert the value into the 141 * entropy pool. Although this operation should not do any harm even 142 * when the time stamp has no entropy, SP800-90B requires that any 143 * conditioning operation to have an identical amount of input data 144 * according to section 3.1.5. 145 */ 146 if (stuck) { 147 time = 0; 148 } 149 150 sha3_update(hash_state, (u8 *)&time, sizeof(__u64)); 151 memzero_explicit(intermediary, sizeof(intermediary)); 152 } 153 154 void jent_read_random_block(struct sha3_ctx *hash_state, char *dst, 155 unsigned int dst_len) 156 { 157 u8 jent_block[SHA3_256_DIGEST_SIZE]; 158 159 /* Obtain data from entropy pool and re-initialize it */ 160 sha3_final(hash_state, jent_block); 161 sha3_256_init(hash_state); 162 sha3_update(hash_state, jent_block, sizeof(jent_block)); 163 164 if (dst_len) 165 memcpy(dst, jent_block, dst_len); 166 167 memzero_explicit(jent_block, sizeof(jent_block)); 168 } 169 170 /*************************************************************************** 171 * Kernel crypto API interface 172 ***************************************************************************/ 173 174 struct jitterentropy { 175 spinlock_t jent_lock; 176 struct rand_data *entropy_collector; 177 struct sha3_ctx hash_state; 178 }; 179 180 static void jent_kcapi_cleanup(struct crypto_tfm *tfm) 181 { 182 struct jitterentropy *rng = crypto_tfm_ctx(tfm); 183 184 spin_lock(&rng->jent_lock); 185 186 memzero_explicit(&rng->hash_state, sizeof(rng->hash_state)); 187 188 if (rng->entropy_collector) 189 jent_entropy_collector_free(rng->entropy_collector); 190 rng->entropy_collector = NULL; 191 spin_unlock(&rng->jent_lock); 192 } 193 194 static int jent_kcapi_init(struct crypto_tfm *tfm) 195 { 196 struct jitterentropy *rng = crypto_tfm_ctx(tfm); 197 int ret = 0; 198 199 spin_lock_init(&rng->jent_lock); 200 201 /* Use SHA3-256 as conditioner */ 202 sha3_256_init(&rng->hash_state); 203 204 rng->entropy_collector = jent_entropy_collector_alloc( 205 CONFIG_CRYPTO_JITTERENTROPY_OSR, 0, &rng->hash_state); 206 if (!rng->entropy_collector) { 207 ret = -ENOMEM; 208 goto err; 209 } 210 211 spin_lock_init(&rng->jent_lock); 212 return 0; 213 214 err: 215 jent_kcapi_cleanup(tfm); 216 return ret; 217 } 218 219 static int jent_kcapi_random(struct crypto_rng *tfm, 220 const u8 *src, unsigned int slen, 221 u8 *rdata, unsigned int dlen) 222 { 223 struct jitterentropy *rng = crypto_rng_ctx(tfm); 224 int ret = 0; 225 226 spin_lock(&rng->jent_lock); 227 228 ret = jent_read_entropy(rng->entropy_collector, rdata, dlen); 229 230 if (ret == -3) { 231 /* Handle permanent health test error */ 232 /* 233 * If the kernel was booted with fips=1, it implies that 234 * the entire kernel acts as a FIPS 140 module. In this case 235 * an SP800-90B permanent health test error is treated as 236 * a FIPS module error. 237 */ 238 if (fips_enabled) 239 panic("Jitter RNG permanent health test failure\n"); 240 241 pr_err("Jitter RNG permanent health test failure\n"); 242 ret = -EFAULT; 243 } else if (ret == -2) { 244 /* Handle intermittent health test error */ 245 pr_warn_ratelimited("Reset Jitter RNG due to intermittent health test failure\n"); 246 ret = -EAGAIN; 247 } else if (ret == -1) { 248 /* Handle other errors */ 249 ret = -EINVAL; 250 } 251 252 spin_unlock(&rng->jent_lock); 253 254 return ret; 255 } 256 257 static int jent_kcapi_reset(struct crypto_rng *tfm, 258 const u8 *seed, unsigned int slen) 259 { 260 return 0; 261 } 262 263 static struct rng_alg jent_alg = { 264 .generate = jent_kcapi_random, 265 .seed = jent_kcapi_reset, 266 .seedsize = 0, 267 .base = { 268 .cra_name = "jitterentropy_rng", 269 .cra_driver_name = "jitterentropy_rng", 270 .cra_priority = 100, 271 .cra_ctxsize = sizeof(struct jitterentropy), 272 .cra_module = THIS_MODULE, 273 .cra_init = jent_kcapi_init, 274 .cra_exit = jent_kcapi_cleanup, 275 } 276 }; 277 278 static int __init jent_mod_init(void) 279 { 280 struct sha3_ctx hash_state; 281 int ret = 0; 282 283 jent_testing_init(); 284 285 sha3_256_init(&hash_state); 286 287 ret = jent_entropy_init(CONFIG_CRYPTO_JITTERENTROPY_OSR, 0, &hash_state, 288 NULL); 289 memzero_explicit(&hash_state, sizeof(hash_state)); 290 if (ret) { 291 /* Handle permanent health test error */ 292 if (fips_enabled) 293 panic("jitterentropy: Initialization failed with host not compliant with requirements: %d\n", ret); 294 295 jent_testing_exit(); 296 pr_info("jitterentropy: Initialization failed with host not compliant with requirements: %d\n", ret); 297 return -EFAULT; 298 } 299 return crypto_register_rng(&jent_alg); 300 } 301 302 static void __exit jent_mod_exit(void) 303 { 304 jent_testing_exit(); 305 crypto_unregister_rng(&jent_alg); 306 } 307 308 module_init(jent_mod_init); 309 module_exit(jent_mod_exit); 310 311 MODULE_LICENSE("Dual BSD/GPL"); 312 MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>"); 313 MODULE_DESCRIPTION("Non-physical True Random Number Generator based on CPU Jitter"); 314 MODULE_ALIAS_CRYPTO("jitterentropy_rng"); 315