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/mutex.h> 45 #include <linux/slab.h> 46 #include <linux/time.h> 47 #include <crypto/internal/rng.h> 48 49 #include "jitterentropy.h" 50 51 /*************************************************************************** 52 * Helper function 53 ***************************************************************************/ 54 55 void *jent_kvzalloc(unsigned int len) 56 { 57 return kvzalloc(len, GFP_KERNEL); 58 } 59 60 void jent_kvzfree(void *ptr, unsigned int len) 61 { 62 kvfree_sensitive(ptr, len); 63 } 64 65 void *jent_zalloc(unsigned int len) 66 { 67 return kzalloc(len, GFP_KERNEL); 68 } 69 70 void jent_zfree(void *ptr) 71 { 72 kfree_sensitive(ptr); 73 } 74 75 /* 76 * Obtain a high-resolution time stamp value. The time stamp is used to measure 77 * the execution time of a given code path and its variations. Hence, the time 78 * stamp must have a sufficiently high resolution. 79 * 80 * Note, if the function returns zero because a given architecture does not 81 * implement a high-resolution time stamp, the RNG code's runtime test 82 * will detect it and will not produce output. 83 */ 84 void jent_get_nstime(__u64 *out) 85 { 86 __u64 tmp = 0; 87 88 tmp = random_get_entropy(); 89 90 /* 91 * If random_get_entropy does not return a value, i.e. it is not 92 * implemented for a given architecture, use a clock source. 93 * hoping that there are timers we can work with. 94 */ 95 if (tmp == 0) 96 tmp = ktime_get_ns(); 97 98 *out = tmp; 99 jent_raw_hires_entropy_store(tmp); 100 } 101 102 void jent_hash_time(struct sha3_ctx *hash_state, __u64 time, u8 *addtl, 103 unsigned int addtl_len, __u64 hash_loop_cnt, 104 unsigned int stuck) 105 { 106 struct sha3_ctx tmp_state; /* zeroized by sha3_final() */ 107 u8 intermediary[SHA3_256_DIGEST_SIZE]; 108 __u64 j = 0; 109 110 kmsan_unpoison_memory(intermediary, sizeof(intermediary)); 111 112 /* 113 * This loop fills a buffer which is injected into the entropy pool. 114 * The main reason for this loop is to execute something over which we 115 * can perform a timing measurement. The injection of the resulting 116 * data into the pool is performed to ensure the result is used and 117 * the compiler cannot optimize the loop away in case the result is not 118 * used at all. Yet that data is considered "additional information" 119 * considering the terminology from SP800-90A without any entropy. 120 * 121 * Note, it does not matter which or how much data you inject, we are 122 * interested in one Keccack1600 compression operation performed with 123 * the sha3_final. 124 */ 125 for (j = 0; j < hash_loop_cnt; j++) { 126 sha3_256_init(&tmp_state); 127 sha3_update(&tmp_state, intermediary, sizeof(intermediary)); 128 sha3_update(&tmp_state, addtl, addtl_len); 129 sha3_final(&tmp_state, intermediary); 130 } 131 132 /* 133 * Inject the data from the previous loop into the pool. This data is 134 * not considered to contain any entropy, but it stirs the pool a bit. 135 */ 136 sha3_update(hash_state, intermediary, sizeof(intermediary)); 137 138 /* 139 * Insert the time stamp into the hash context representing the pool. 140 * 141 * If the time stamp is stuck, do not finally insert the value into the 142 * entropy pool. Although this operation should not do any harm even 143 * when the time stamp has no entropy, SP800-90B requires that any 144 * conditioning operation to have an identical amount of input data 145 * according to section 3.1.5. 146 */ 147 if (stuck) { 148 time = 0; 149 } 150 151 sha3_update(hash_state, (u8 *)&time, sizeof(__u64)); 152 memzero_explicit(intermediary, sizeof(intermediary)); 153 } 154 155 void jent_read_random_block(struct sha3_ctx *hash_state, char *dst, 156 unsigned int dst_len) 157 { 158 u8 jent_block[SHA3_256_DIGEST_SIZE]; 159 160 /* Obtain data from entropy pool and re-initialize it */ 161 sha3_final(hash_state, jent_block); 162 sha3_256_init(hash_state); 163 sha3_update(hash_state, jent_block, sizeof(jent_block)); 164 165 if (dst_len) 166 memcpy(dst, jent_block, dst_len); 167 168 memzero_explicit(jent_block, sizeof(jent_block)); 169 } 170 171 /*************************************************************************** 172 * Kernel crypto API interface 173 ***************************************************************************/ 174 175 struct jitterentropy { 176 struct mutex jent_lock; 177 struct rand_data *entropy_collector; 178 struct sha3_ctx hash_state; 179 }; 180 181 static void jent_kcapi_cleanup(struct crypto_tfm *tfm) 182 { 183 struct jitterentropy *rng = crypto_tfm_ctx(tfm); 184 185 mutex_lock(&rng->jent_lock); 186 187 memzero_explicit(&rng->hash_state, sizeof(rng->hash_state)); 188 189 if (rng->entropy_collector) 190 jent_entropy_collector_free(rng->entropy_collector); 191 rng->entropy_collector = NULL; 192 mutex_unlock(&rng->jent_lock); 193 } 194 195 static int jent_kcapi_init(struct crypto_tfm *tfm) 196 { 197 struct jitterentropy *rng = crypto_tfm_ctx(tfm); 198 int ret = 0; 199 200 mutex_init(&rng->jent_lock); 201 202 /* Use SHA3-256 as conditioner */ 203 sha3_256_init(&rng->hash_state); 204 205 rng->entropy_collector = jent_entropy_collector_alloc( 206 CONFIG_CRYPTO_JITTERENTROPY_OSR, 0, &rng->hash_state); 207 if (!rng->entropy_collector) { 208 ret = -ENOMEM; 209 goto err; 210 } 211 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 mutex_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 mutex_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