xref: /freebsd/sys/dev/random/ivy.c (revision fcb560670601b2a4d87bb31d7531c8dcc37ee71b)
1 /*-
2  * Copyright (c) 2013 The FreeBSD Foundation
3  * Copyright (c) 2013 David E. O'Brien <obrien@NUXI.org>
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/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/conf.h>
39 #include <sys/lock.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/random.h>
43 #include <sys/systm.h>
44 
45 #include <machine/md_var.h>
46 #include <machine/specialreg.h>
47 
48 #include <dev/random/randomdev.h>
49 #include <dev/random/randomdev_soft.h>
50 #include <dev/random/random_adaptors.h>
51 #include <dev/random/live_entropy_sources.h>
52 
53 #define	RETRY_COUNT	10
54 
55 static u_int random_ivy_read(void *, u_int);
56 
57 static struct live_entropy_source random_ivy = {
58 	.les_ident = "Intel Secure Key RNG",
59 	.les_source = RANDOM_PURE_RDRAND,
60 	.les_read = random_ivy_read
61 };
62 
63 static inline int
64 ivy_rng_store(u_long *buf)
65 {
66 #ifdef __GNUCLIKE_ASM
67 	u_long rndval;
68 	int retry;
69 
70 	retry = RETRY_COUNT;
71 	__asm __volatile(
72 	    "1:\n\t"
73 	    "rdrand	%1\n\t"	/* read randomness into rndval */
74 	    "jc		2f\n\t" /* CF is set on success, exit retry loop */
75 	    "dec	%0\n\t" /* otherwise, retry-- */
76 	    "jne	1b\n\t" /* and loop if retries are not exhausted */
77 	    "2:"
78 	    : "+r" (retry), "=r" (rndval) : : "cc");
79 	*buf = rndval;
80 	return (retry);
81 #else /* __GNUCLIKE_ASM */
82 	return (0);
83 #endif
84 }
85 
86 /* It is required that buf length is a multiple of sizeof(u_long). */
87 static u_int
88 random_ivy_read(void *buf, u_int c)
89 {
90 	u_long *b, rndval;
91 	u_int count;
92 
93 	KASSERT(c % sizeof(*b) == 0, ("partial read %d", c));
94 	b = buf;
95 	for (count = c; count > 0; count -= sizeof(*b)) {
96 		if (ivy_rng_store(&rndval) == 0)
97 			break;
98 		*b++ = rndval;
99 	}
100 	return (c - count);
101 }
102 
103 static int
104 rdrand_modevent(module_t mod, int type, void *unused)
105 {
106 	int error = 0;
107 
108 	switch (type) {
109 	case MOD_LOAD:
110 		if (cpu_feature2 & CPUID2_RDRAND) {
111 			live_entropy_source_register(&random_ivy);
112 			printf("random: live provider: \"%s\"\n", random_ivy.les_ident);
113 		}
114 		break;
115 
116 	case MOD_UNLOAD:
117 		if (cpu_feature2 & CPUID2_RDRAND)
118 			live_entropy_source_deregister(&random_ivy);
119 		break;
120 
121 	case MOD_SHUTDOWN:
122 		break;
123 
124 	default:
125 		error = EOPNOTSUPP;
126 		break;
127 
128 	}
129 
130 	return (error);
131 }
132 
133 DEV_MODULE(rdrand, rdrand_modevent, NULL);
134 MODULE_VERSION(rdrand, 1);
135 MODULE_DEPEND(rdrand, randomdev, 1, 1, 1);
136