1 /*- 2 * Copyright (c) 2013 The FreeBSD Foundation 3 * Copyright (c) 2013 David E. O'Brien <obrien@NUXI.org> 4 * Copyright (c) 2012 Konstantin Belousov <kib@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Portions of this software were developed by Konstantin Belousov 8 * under sponsorship from the FreeBSD Foundation. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer 15 * in this position and unchanged. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 * 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include <sys/param.h> 37 #include <sys/kernel.h> 38 #include <sys/conf.h> 39 #include <sys/lock.h> 40 #include <sys/malloc.h> 41 #include <sys/module.h> 42 #include <sys/random.h> 43 #include <sys/systm.h> 44 45 #include <machine/md_var.h> 46 #include <machine/specialreg.h> 47 #include <x86/ifunc.h> 48 49 #include <dev/random/randomdev.h> 50 51 #define RETRY_COUNT 10 52 53 static bool has_rdrand, has_rdseed; 54 static u_int random_ivy_read(void *, u_int); 55 56 static struct random_source random_ivy = { 57 .rs_ident = "Intel Secure Key RNG", 58 .rs_source = RANDOM_PURE_RDRAND, 59 .rs_read = random_ivy_read 60 }; 61 62 static int 63 x86_rdrand_store(u_long *buf) 64 { 65 u_long rndval; 66 int retry; 67 68 retry = RETRY_COUNT; 69 __asm __volatile( 70 "1:\n\t" 71 "rdrand %1\n\t" /* read randomness into rndval */ 72 "jc 2f\n\t" /* CF is set on success, exit retry loop */ 73 "dec %0\n\t" /* otherwise, retry-- */ 74 "jne 1b\n\t" /* and loop if retries are not exhausted */ 75 "2:" 76 : "+r" (retry), "=r" (rndval) : : "cc"); 77 *buf = rndval; 78 return (retry); 79 } 80 81 static int 82 x86_rdseed_store(u_long *buf) 83 { 84 u_long rndval; 85 int retry; 86 87 retry = RETRY_COUNT; 88 __asm __volatile( 89 "1:\n\t" 90 "rdseed %1\n\t" /* read randomness into rndval */ 91 "jc 2f\n\t" /* CF is set on success, exit retry loop */ 92 "dec %0\n\t" /* otherwise, retry-- */ 93 "jne 1b\n\t" /* and loop if retries are not exhausted */ 94 "2:" 95 : "+r" (retry), "=r" (rndval) : : "cc"); 96 *buf = rndval; 97 return (retry); 98 } 99 100 static int 101 x86_unimpl_store(u_long *buf __unused) 102 { 103 104 panic("%s called", __func__); 105 } 106 107 DEFINE_IFUNC(static, int, x86_rng_store, (u_long *buf)) 108 { 109 has_rdrand = (cpu_feature2 & CPUID2_RDRAND); 110 has_rdseed = (cpu_stdext_feature & CPUID_STDEXT_RDSEED); 111 112 if (has_rdseed) 113 return (x86_rdseed_store); 114 else if (has_rdrand) 115 return (x86_rdrand_store); 116 else 117 return (x86_unimpl_store); 118 } 119 120 /* It is required that buf length is a multiple of sizeof(u_long). */ 121 static u_int 122 random_ivy_read(void *buf, u_int c) 123 { 124 u_long *b, rndval; 125 u_int count; 126 127 KASSERT(c % sizeof(*b) == 0, ("partial read %d", c)); 128 b = buf; 129 for (count = c; count > 0; count -= sizeof(*b)) { 130 if (x86_rng_store(&rndval) == 0) 131 break; 132 *b++ = rndval; 133 } 134 return (c - count); 135 } 136 137 static int 138 rdrand_modevent(module_t mod, int type, void *unused) 139 { 140 int error = 0; 141 142 switch (type) { 143 case MOD_LOAD: 144 if (has_rdrand || has_rdseed) { 145 random_source_register(&random_ivy); 146 printf("random: fast provider: \"%s\"\n", random_ivy.rs_ident); 147 } 148 break; 149 150 case MOD_UNLOAD: 151 if (has_rdrand || has_rdseed) 152 random_source_deregister(&random_ivy); 153 break; 154 155 case MOD_SHUTDOWN: 156 break; 157 158 default: 159 error = EOPNOTSUPP; 160 break; 161 162 } 163 164 return (error); 165 } 166 167 static moduledata_t rdrand_mod = { 168 "rdrand", 169 rdrand_modevent, 170 0 171 }; 172 173 DECLARE_MODULE(rdrand, rdrand_mod, SI_SUB_RANDOM, SI_ORDER_FOURTH); 174 MODULE_VERSION(rdrand, 1); 175 MODULE_DEPEND(rdrand, random_harvestq, 1, 1, 1); 176