xref: /freebsd/sys/dev/random/rdseed.c (revision 5180fce1cf7ec7430ccf2adb88358602e5ccee1e)
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_rdseed_read(void *, u_int);
51 
52 static struct random_source random_rdseed = {
53 	.rs_ident = "Intel Secure Key Seed",
54 	.rs_source = RANDOM_PURE_RDSEED,
55 	.rs_read = random_rdseed_read
56 };
57 
58 SYSCTL_NODE(_kern_random, OID_AUTO, rdseed, CTLFLAG_RW, 0,
59     "rdseed (x86) entropy source");
60 /* XXX: kern.random.rdseed.enabled=0 also disables RDRAND */
61 static bool enabled = true;
62 SYSCTL_BOOL(_kern_random_rdseed, OID_AUTO, enabled, CTLFLAG_RDTUN, &enabled, 0,
63     "If zero, disable the use of RDSEED.");
64 
65 static bool
66 x86_rdseed_store(u_long *buf)
67 {
68 	u_long rndval;
69 	int retry;
70 
71 	retry = RETRY_COUNT;
72 	__asm __volatile(
73 	    "1:\n\t"
74 	    "rdseed	%1\n\t"	/* read randomness into rndval */
75 	    "jc		2f\n\t" /* CF is set on success, exit retry loop */
76 	    "dec	%0\n\t" /* otherwise, retry-- */
77 	    "jne	1b\n\t" /* and loop if retries are not exhausted */
78 	    "2:"
79 	    : "+r" (retry), "=r" (rndval) : : "cc");
80 	*buf = rndval;
81 	return (retry != 0);
82 }
83 
84 /* It is required that buf length is a multiple of sizeof(u_long). */
85 static u_int
86 random_rdseed_read(void *buf, u_int c)
87 {
88 	u_long *b, rndval;
89 	u_int count;
90 
91 	KASSERT(c % sizeof(*b) == 0, ("partial read %d", c));
92 	b = buf;
93 	for (count = c; count > 0; count -= sizeof(*b)) {
94 		if (!x86_rdseed_store(&rndval))
95 			break;
96 		*b++ = rndval;
97 	}
98 	return (c - count);
99 }
100 
101 static int
102 rdseed_modevent(module_t mod, int type, void *unused)
103 {
104 	bool has_rdseed;
105 	int error = 0;
106 
107 	has_rdseed = (cpu_stdext_feature & CPUID_STDEXT_RDSEED);
108 
109 	switch (type) {
110 	case MOD_LOAD:
111 		if (has_rdseed && enabled) {
112 			random_source_register(&random_rdseed);
113 			printf("random: fast provider: \"%s\"\n", random_rdseed.rs_ident);
114 		}
115 		break;
116 
117 	case MOD_UNLOAD:
118 		if (has_rdseed)
119 			random_source_deregister(&random_rdseed);
120 		break;
121 
122 	case MOD_SHUTDOWN:
123 		break;
124 
125 	default:
126 		error = EOPNOTSUPP;
127 		break;
128 
129 	}
130 
131 	return (error);
132 }
133 
134 static moduledata_t rdseed_mod = {
135 	"rdseed",
136 	rdseed_modevent,
137 	0
138 };
139 
140 DECLARE_MODULE(rdseed, rdseed_mod, SI_SUB_RANDOM, SI_ORDER_FOURTH);
141 MODULE_VERSION(rdseed, 1);
142 MODULE_DEPEND(rdseed, random_harvestq, 1, 1, 1);
143 
144 /*
145  * Intel's RDSEED Entropy Assessment Report min-entropy claim is 0.6 Shannons
146  * per bit of data output.  Rrefer to the following Entropy Source Validation
147  * (ESV) certificates:
148  *
149  *	E#87:	Junos OS Physical Entropy Source - Broadwell EP 10-Core Die
150  *		Broadwell-EP-10 FCLGA2011 Intel(R) Xeon(R) E5-2620 V4 Processor
151  *		https://csrc.nist.gov/projects/cryptographic-module-validation-program/entropy-validations/certificate/87
152  *		(URLs below omitted for brevity but follow same format.)
153  *
154  *	E#121:	Junos OS Physical Entropy Source - Intel Atom C3000 Series
155  *		(Denverton) 16 Core Die with FCBGA1310 Package
156  *
157  *	E#122:	Junos OS Physical Entropy Source - Intel Xeon D-1500 Family
158  *		(Broadwell) 8 Core Die with FCBGA1667 Package
159  *
160  *	E#123:	Junos OS Physical Entropy Source - Intel Xeon D-2100 Series
161  *		(Skylake) 18 Core Die with FCBGA2518 Package
162  *
163  *	E#141:	Junos OS Physical Entropy Source - Intel Xeon D-10 Series
164  *		(Ice Lake-D-10) Die with FCBGA2227 Package
165  *
166  *	E#169:	Junos OS Physical Entropy Source - Intel Xeon AWS-1000 v4 and
167  *		E5 v4 (Broadwell EP) 15 Core Die with FCLGA2011 Package
168  */
169