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