xref: /freebsd/sys/dev/random/ivy.c (revision 2cb54a800c3a42cc256adcda0be98bd0af39096c)
1ef9461baSKonstantin Belousov /*-
27c3547baSKonstantin Belousov  * Copyright (c) 2013 The FreeBSD Foundation
35711939bSDavid E. O'Brien  * Copyright (c) 2013 David E. O'Brien <obrien@NUXI.org>
4ef9461baSKonstantin Belousov  * Copyright (c) 2012 Konstantin Belousov <kib@FreeBSD.org>
5ef9461baSKonstantin Belousov  * All rights reserved.
6ef9461baSKonstantin Belousov  *
77c3547baSKonstantin Belousov  * Portions of this software were developed by Konstantin Belousov
87c3547baSKonstantin Belousov  * under sponsorship from the FreeBSD Foundation.
97c3547baSKonstantin Belousov  *
10ef9461baSKonstantin Belousov  * Redistribution and use in source and binary forms, with or without
11ef9461baSKonstantin Belousov  * modification, are permitted provided that the following conditions
12ef9461baSKonstantin Belousov  * are met:
13ef9461baSKonstantin Belousov  * 1. Redistributions of source code must retain the above copyright
14ef9461baSKonstantin Belousov  *    notice, this list of conditions and the following disclaimer
15ef9461baSKonstantin Belousov  *    in this position and unchanged.
16ef9461baSKonstantin Belousov  * 2. Redistributions in binary form must reproduce the above copyright
17ef9461baSKonstantin Belousov  *    notice, this list of conditions and the following disclaimer in the
18ef9461baSKonstantin Belousov  *    documentation and/or other materials provided with the distribution.
19ef9461baSKonstantin Belousov  *
20ef9461baSKonstantin Belousov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21ef9461baSKonstantin Belousov  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22ef9461baSKonstantin Belousov  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23ef9461baSKonstantin Belousov  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24ef9461baSKonstantin Belousov  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25ef9461baSKonstantin Belousov  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26ef9461baSKonstantin Belousov  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27ef9461baSKonstantin Belousov  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28ef9461baSKonstantin Belousov  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29ef9461baSKonstantin Belousov  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30ef9461baSKonstantin Belousov  *
31ef9461baSKonstantin Belousov  */
32ef9461baSKonstantin Belousov 
33ef9461baSKonstantin Belousov #include <sys/cdefs.h>
34ef9461baSKonstantin Belousov __FBSDID("$FreeBSD$");
35ef9461baSKonstantin Belousov 
36ef9461baSKonstantin Belousov #include <sys/param.h>
375711939bSDavid E. O'Brien #include <sys/kernel.h>
3810cb2424SMark Murray #include <sys/conf.h>
39ef9461baSKonstantin Belousov #include <sys/lock.h>
40095ed2c9SMark Murray #include <sys/malloc.h>
415711939bSDavid E. O'Brien #include <sys/module.h>
42f02e47dcSMark Murray #include <sys/random.h>
43ef9461baSKonstantin Belousov #include <sys/systm.h>
445711939bSDavid E. O'Brien 
455711939bSDavid E. O'Brien #include <machine/md_var.h>
465711939bSDavid E. O'Brien #include <machine/specialreg.h>
47*2cb54a80SConrad Meyer #include <x86/ifunc.h>
485711939bSDavid E. O'Brien 
49095ed2c9SMark Murray #include <dev/random/randomdev.h>
50ef9461baSKonstantin Belousov 
51ef9461baSKonstantin Belousov #define	RETRY_COUNT	10
52ef9461baSKonstantin Belousov 
53*2cb54a80SConrad Meyer static bool has_rdrand, has_rdseed;
5410cb2424SMark Murray static u_int random_ivy_read(void *, u_int);
55ef9461baSKonstantin Belousov 
56d1b06863SMark Murray static struct random_source random_ivy = {
57d1b06863SMark Murray 	.rs_ident = "Intel Secure Key RNG",
58d1b06863SMark Murray 	.rs_source = RANDOM_PURE_RDRAND,
59d1b06863SMark Murray 	.rs_read = random_ivy_read
60ef9461baSKonstantin Belousov };
61ef9461baSKonstantin Belousov 
62*2cb54a80SConrad Meyer static int
63*2cb54a80SConrad Meyer x86_rdrand_store(u_long *buf)
64ef9461baSKonstantin Belousov {
6576c16ab9SKonstantin Belousov 	u_long rndval;
667c3547baSKonstantin Belousov 	int retry;
67ef9461baSKonstantin Belousov 
687c3547baSKonstantin Belousov 	retry = RETRY_COUNT;
69ef9461baSKonstantin Belousov 	__asm __volatile(
707c3547baSKonstantin Belousov 	    "1:\n\t"
71843c718fSKonstantin Belousov 	    "rdrand	%1\n\t"	/* read randomness into rndval */
7276c16ab9SKonstantin Belousov 	    "jc		2f\n\t" /* CF is set on success, exit retry loop */
737c3547baSKonstantin Belousov 	    "dec	%0\n\t" /* otherwise, retry-- */
747c3547baSKonstantin Belousov 	    "jne	1b\n\t" /* and loop if retries are not exhausted */
7576c16ab9SKonstantin Belousov 	    "2:"
7676c16ab9SKonstantin Belousov 	    : "+r" (retry), "=r" (rndval) : : "cc");
7776c16ab9SKonstantin Belousov 	*buf = rndval;
787c3547baSKonstantin Belousov 	return (retry);
79*2cb54a80SConrad Meyer }
80*2cb54a80SConrad Meyer 
81*2cb54a80SConrad Meyer static int
82*2cb54a80SConrad Meyer x86_rdseed_store(u_long *buf)
83*2cb54a80SConrad Meyer {
84*2cb54a80SConrad Meyer 	u_long rndval;
85*2cb54a80SConrad Meyer 	int retry;
86*2cb54a80SConrad Meyer 
87*2cb54a80SConrad Meyer 	retry = RETRY_COUNT;
88*2cb54a80SConrad Meyer 	__asm __volatile(
89*2cb54a80SConrad Meyer 	    "1:\n\t"
90*2cb54a80SConrad Meyer 	    "rdseed	%1\n\t"	/* read randomness into rndval */
91*2cb54a80SConrad Meyer 	    "jc		2f\n\t" /* CF is set on success, exit retry loop */
92*2cb54a80SConrad Meyer 	    "dec	%0\n\t" /* otherwise, retry-- */
93*2cb54a80SConrad Meyer 	    "jne	1b\n\t" /* and loop if retries are not exhausted */
94*2cb54a80SConrad Meyer 	    "2:"
95*2cb54a80SConrad Meyer 	    : "+r" (retry), "=r" (rndval) : : "cc");
96*2cb54a80SConrad Meyer 	*buf = rndval;
97*2cb54a80SConrad Meyer 	return (retry);
98*2cb54a80SConrad Meyer }
99*2cb54a80SConrad Meyer 
100*2cb54a80SConrad Meyer DEFINE_IFUNC(static, int, x86_rng_store, (u_long *buf), static)
101*2cb54a80SConrad Meyer {
102*2cb54a80SConrad Meyer 	has_rdrand = (cpu_feature2 & CPUID2_RDRAND);
103*2cb54a80SConrad Meyer 	has_rdseed = (cpu_stdext_feature & CPUID_STDEXT_RDSEED);
104*2cb54a80SConrad Meyer 
105*2cb54a80SConrad Meyer 	if (has_rdseed)
106*2cb54a80SConrad Meyer 		return (x86_rdseed_store);
107*2cb54a80SConrad Meyer 	else if (has_rdrand)
108*2cb54a80SConrad Meyer 		return (x86_rdrand_store);
109*2cb54a80SConrad Meyer 	else
110*2cb54a80SConrad Meyer 		return (NULL);
111ef9461baSKonstantin Belousov }
112ef9461baSKonstantin Belousov 
11376c16ab9SKonstantin Belousov /* It is required that buf length is a multiple of sizeof(u_long). */
11410cb2424SMark Murray static u_int
11510cb2424SMark Murray random_ivy_read(void *buf, u_int c)
116ef9461baSKonstantin Belousov {
11776c16ab9SKonstantin Belousov 	u_long *b, rndval;
11810cb2424SMark Murray 	u_int count;
119ef9461baSKonstantin Belousov 
12010cb2424SMark Murray 	KASSERT(c % sizeof(*b) == 0, ("partial read %d", c));
12110cb2424SMark Murray 	b = buf;
12210cb2424SMark Murray 	for (count = c; count > 0; count -= sizeof(*b)) {
123*2cb54a80SConrad Meyer 		if (x86_rng_store(&rndval) == 0)
124ef9461baSKonstantin Belousov 			break;
12576c16ab9SKonstantin Belousov 		*b++ = rndval;
126ef9461baSKonstantin Belousov 	}
127ef9461baSKonstantin Belousov 	return (c - count);
128ef9461baSKonstantin Belousov }
129ef9461baSKonstantin Belousov 
1305711939bSDavid E. O'Brien static int
1315711939bSDavid E. O'Brien rdrand_modevent(module_t mod, int type, void *unused)
1325711939bSDavid E. O'Brien {
133f02e47dcSMark Murray 	int error = 0;
1345711939bSDavid E. O'Brien 
1355711939bSDavid E. O'Brien 	switch (type) {
1365711939bSDavid E. O'Brien 	case MOD_LOAD:
137*2cb54a80SConrad Meyer 		if (has_rdrand || has_rdseed) {
138d1b06863SMark Murray 			random_source_register(&random_ivy);
139d1b06863SMark Murray 			printf("random: fast provider: \"%s\"\n", random_ivy.rs_ident);
14010cb2424SMark Murray 		}
141f02e47dcSMark Murray 		break;
142f02e47dcSMark Murray 
143f02e47dcSMark Murray 	case MOD_UNLOAD:
144*2cb54a80SConrad Meyer 		if (has_rdrand || has_rdseed)
145d1b06863SMark Murray 			random_source_deregister(&random_ivy);
146f02e47dcSMark Murray 		break;
147f02e47dcSMark Murray 
148f02e47dcSMark Murray 	case MOD_SHUTDOWN:
149f02e47dcSMark Murray 		break;
150f02e47dcSMark Murray 
151f02e47dcSMark Murray 	default:
152f02e47dcSMark Murray 		error = EOPNOTSUPP;
153f02e47dcSMark Murray 		break;
154f02e47dcSMark Murray 
1555711939bSDavid E. O'Brien 	}
1565711939bSDavid E. O'Brien 
157f02e47dcSMark Murray 	return (error);
1585711939bSDavid E. O'Brien }
1595711939bSDavid E. O'Brien 
16010cb2424SMark Murray DEV_MODULE(rdrand, rdrand_modevent, NULL);
16110cb2424SMark Murray MODULE_VERSION(rdrand, 1);
16297b9c34fSKonstantin Belousov MODULE_DEPEND(rdrand, random_device, 1, 1, 1);
163