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