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