1 /*-
2 * Copyright (c) 2013 The FreeBSD Foundation
3 * Copyright (c) 2013 David E. O'Brien <[email protected]>
4 * Copyright (c) 2012 Konstantin Belousov <[email protected]>
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 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/conf.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/random.h>
41 #include <sys/sysctl.h>
42 #include <sys/systm.h>
43
44 #include <machine/md_var.h>
45 #include <machine/specialreg.h>
46 #include <x86/ifunc.h>
47
48 #include <dev/random/randomdev.h>
49
50 #define RETRY_COUNT 10
51
52 static bool has_rdrand, has_rdseed;
53 static u_int random_ivy_read(void *, u_int);
54
55 static struct random_source random_ivy = {
56 .rs_ident = "Intel Secure Key RNG",
57 .rs_source = RANDOM_PURE_RDRAND,
58 .rs_read = random_ivy_read
59 };
60
61 SYSCTL_NODE(_kern_random, OID_AUTO, rdrand, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
62 "rdrand (ivy) entropy source");
63 static bool acquire_independent_seed_samples = false;
64 SYSCTL_BOOL(_kern_random_rdrand, OID_AUTO, rdrand_independent_seed,
65 CTLFLAG_RWTUN, &acquire_independent_seed_samples, 0,
66 "If non-zero, use more expensive and slow, but safer, seeded samples "
67 "where RDSEED is not present.");
68
69 static bool
x86_rdrand_store(u_long * buf)70 x86_rdrand_store(u_long *buf)
71 {
72 u_long rndval, seed_iterations, i;
73 int retry;
74
75 /* Per [1], "§ 5.2.6 Generating Seeds from RDRAND,"
76 * machines lacking RDSEED will guarantee RDRAND is reseeded every 8kB
77 * of generated output.
78 *
79 * [1]: https://software.intel.com/en-us/articles/intel-digital-random-number-generator-drng-software-implementation-guide#inpage-nav-6-8
80 */
81 if (acquire_independent_seed_samples)
82 seed_iterations = 8 * 1024 / sizeof(*buf);
83 else
84 seed_iterations = 1;
85
86 for (i = 0; i < seed_iterations; i++) {
87 retry = RETRY_COUNT;
88 __asm __volatile(
89 "1:\n\t"
90 "rdrand %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 if (retry == 0)
97 return (false);
98 }
99 *buf = rndval;
100 return (true);
101 }
102
103 static bool
x86_rdseed_store(u_long * buf)104 x86_rdseed_store(u_long *buf)
105 {
106 u_long rndval;
107 int retry;
108
109 retry = RETRY_COUNT;
110 __asm __volatile(
111 "1:\n\t"
112 "rdseed %1\n\t" /* read randomness into rndval */
113 "jc 2f\n\t" /* CF is set on success, exit retry loop */
114 "dec %0\n\t" /* otherwise, retry-- */
115 "jne 1b\n\t" /* and loop if retries are not exhausted */
116 "2:"
117 : "+r" (retry), "=r" (rndval) : : "cc");
118 *buf = rndval;
119 return (retry != 0);
120 }
121
122 static bool
x86_unimpl_store(u_long * buf __unused)123 x86_unimpl_store(u_long *buf __unused)
124 {
125
126 panic("%s called", __func__);
127 }
128
129 DEFINE_IFUNC(static, bool, x86_rng_store, (u_long *buf))
130 {
131 has_rdrand = (cpu_feature2 & CPUID2_RDRAND);
132 has_rdseed = (cpu_stdext_feature & CPUID_STDEXT_RDSEED);
133
134 if (has_rdseed)
135 return (x86_rdseed_store);
136 else if (has_rdrand)
137 return (x86_rdrand_store);
138 else
139 return (x86_unimpl_store);
140 }
141
142 /* It is required that buf length is a multiple of sizeof(u_long). */
143 static u_int
random_ivy_read(void * buf,u_int c)144 random_ivy_read(void *buf, u_int c)
145 {
146 u_long *b, rndval;
147 u_int count;
148
149 KASSERT(c % sizeof(*b) == 0, ("partial read %d", c));
150 b = buf;
151 for (count = c; count > 0; count -= sizeof(*b)) {
152 if (!x86_rng_store(&rndval))
153 break;
154 *b++ = rndval;
155 }
156 return (c - count);
157 }
158
159 static int
rdrand_modevent(module_t mod,int type,void * unused)160 rdrand_modevent(module_t mod, int type, void *unused)
161 {
162 int error = 0;
163
164 switch (type) {
165 case MOD_LOAD:
166 if (has_rdrand || has_rdseed) {
167 random_source_register(&random_ivy);
168 printf("random: fast provider: \"%s\"\n", random_ivy.rs_ident);
169 }
170 break;
171
172 case MOD_UNLOAD:
173 if (has_rdrand || has_rdseed)
174 random_source_deregister(&random_ivy);
175 break;
176
177 case MOD_SHUTDOWN:
178 break;
179
180 default:
181 error = EOPNOTSUPP;
182 break;
183
184 }
185
186 return (error);
187 }
188
189 static moduledata_t rdrand_mod = {
190 "rdrand",
191 rdrand_modevent,
192 0
193 };
194
195 DECLARE_MODULE(rdrand, rdrand_mod, SI_SUB_RANDOM, SI_ORDER_FOURTH);
196 MODULE_VERSION(rdrand, 1);
197 MODULE_DEPEND(rdrand, random_harvestq, 1, 1, 1);
198