]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/random/nehemiah.c
Decouple yarrow from random(4) device.
[FreeBSD/FreeBSD.git] / sys / dev / random / nehemiah.c
1 /*-
2  * Copyright (c) 2004 Mark R V Murray
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/time.h>
33 #include <sys/lock.h>
34 #include <sys/mutex.h>
35 #include <sys/module.h>
36 #include <sys/selinfo.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39
40 #include <machine/pcb.h>
41 #include <machine/md_var.h>
42 #include <machine/specialreg.h>
43
44 #include <dev/random/random_adaptors.h>
45 #include <dev/random/randomdev.h>
46
47 #define RANDOM_BLOCK_SIZE       256
48 #define CIPHER_BLOCK_SIZE       16
49
50 static void random_nehemiah_init(void);
51 static void random_nehemiah_deinit(void);
52 static int random_nehemiah_read(void *, int);
53
54 struct random_adaptor random_nehemiah = {
55         .ident = "Hardware, VIA Nehemiah",
56         .init = random_nehemiah_init,
57         .deinit = random_nehemiah_deinit,
58         .read = random_nehemiah_read,
59         .write = (random_write_func_t *)random_null_func,
60         .reseed = (random_reseed_func_t *)random_null_func,
61         .seeded = 1,
62 };
63
64 union VIA_ACE_CW {
65         uint64_t raw;
66         struct {
67                 u_int round_count : 4;
68                 u_int algorithm_type : 3;
69                 u_int key_generation_type : 1;
70                 u_int intermediate : 1;
71                 u_int decrypt : 1;
72                 u_int key_size : 2;
73                 u_int filler0 : 20;
74                 u_int filler1 : 32;
75                 u_int filler2 : 32;
76                 u_int filler3 : 32;
77         } field;
78 };
79
80 /* The extra 7 is to allow an 8-byte write on the last byte of the
81  * arrays.  The ACE wants the AES data 16-byte/128-bit aligned, and
82  * it _always_ writes n*64 bits. The RNG does not care about alignment,
83  * and it always writes n*32 bits or n*64 bits.
84  */
85 static uint8_t key[CIPHER_BLOCK_SIZE+7] __aligned(16);
86 static uint8_t iv[CIPHER_BLOCK_SIZE+7]  __aligned(16);
87 static uint8_t in[RANDOM_BLOCK_SIZE+7]  __aligned(16);
88 static uint8_t out[RANDOM_BLOCK_SIZE+7] __aligned(16);
89
90 static union VIA_ACE_CW acw             __aligned(16);
91
92 static struct fpu_kern_ctx *fpu_ctx_save;
93
94 static struct mtx random_nehemiah_mtx;
95
96 /* ARGSUSED */
97 static __inline size_t
98 VIA_RNG_store(void *buf)
99 {
100 #ifdef __GNUCLIKE_ASM
101         uint32_t retval = 0;
102         uint32_t rate = 0;
103
104         /* The .byte line is really VIA C3 "xstore" instruction */
105         __asm __volatile(
106                 "movl   $0,%%edx                \n\t"
107                 ".byte  0x0f, 0xa7, 0xc0"
108                         : "=a" (retval), "+d" (rate), "+D" (buf)
109                         :
110                         : "memory"
111         );
112         if (rate == 0)
113                 return (retval&0x1f);
114 #endif
115         return (0);
116 }
117
118 /* ARGSUSED */
119 static __inline void
120 VIA_ACE_cbc(void *in, void *out, size_t count, void *key, union VIA_ACE_CW *cw, void *iv)
121 {
122 #ifdef __GNUCLIKE_ASM
123         /* The .byte line is really VIA C3 "xcrypt-cbc" instruction */
124         __asm __volatile(
125                 "pushf                          \n\t"
126                 "popf                           \n\t"
127                 "rep                            \n\t"
128                 ".byte  0x0f, 0xa7, 0xc8"
129                         : "+a" (iv), "+c" (count), "+D" (out), "+S" (in)
130                         : "b" (key), "d" (cw)
131                         : "cc", "memory"
132                 );
133 #endif
134 }
135
136 static void
137 random_nehemiah_init(void)
138 {
139         acw.raw = 0ULL;
140         acw.field.round_count = 12;
141
142         mtx_init(&random_nehemiah_mtx, "random nehemiah", NULL, MTX_DEF);
143         fpu_ctx_save = fpu_kern_alloc_ctx(FPU_KERN_NORMAL);
144 }
145
146 void
147 random_nehemiah_deinit(void)
148 {
149
150         fpu_kern_free_ctx(fpu_ctx_save);
151         mtx_destroy(&random_nehemiah_mtx);
152 }
153
154 static int
155 random_nehemiah_read(void *buf, int c)
156 {
157         int i, error;
158         size_t count, ret;
159         uint8_t *p;
160
161         mtx_lock(&random_nehemiah_mtx);
162         error = fpu_kern_enter(curthread, fpu_ctx_save, FPU_KERN_NORMAL);
163         if (error != 0) {
164                 mtx_unlock(&random_nehemiah_mtx);
165                 return (0);
166         }
167
168         /* Get a random AES key */
169         count = 0;
170         p = key;
171         do {
172                 ret = VIA_RNG_store(p);
173                 p += ret;
174                 count += ret;
175         } while (count < CIPHER_BLOCK_SIZE);
176
177         /* Get a random AES IV */
178         count = 0;
179         p = iv;
180         do {
181                 ret = VIA_RNG_store(p);
182                 p += ret;
183                 count += ret;
184         } while (count < CIPHER_BLOCK_SIZE);
185
186         /* Get a block of random bytes */
187         count = 0;
188         p = in;
189         do {
190                 ret = VIA_RNG_store(p);
191                 p += ret;
192                 count += ret;
193         } while (count < RANDOM_BLOCK_SIZE);
194
195         /* This is a Davies-Meyer hash of the most paranoid variety; the
196          * key, IV and the data are all read directly from the hardware RNG.
197          * All of these are used precisely once.
198          */
199         VIA_ACE_cbc(in, out, RANDOM_BLOCK_SIZE/CIPHER_BLOCK_SIZE,
200             key, &acw, iv);
201         for (i = 0; i < RANDOM_BLOCK_SIZE; i++)
202                 out[i] ^= in[i];
203
204         c = MIN(RANDOM_BLOCK_SIZE, c);
205         memcpy(buf, out, (size_t)c);
206
207         fpu_kern_leave(curthread, fpu_ctx_save);
208         mtx_unlock(&random_nehemiah_mtx);
209         return (c);
210 }
211
212 static int
213 nehemiah_modevent(module_t mod, int type, void *unused)
214 {
215
216         switch (type) {
217         case MOD_LOAD:
218                 if (via_feature_rng & VIA_HAS_RNG) {
219                         random_adaptor_register("nehemiah", &random_nehemiah);
220                         EVENTHANDLER_INVOKE(random_adaptor_attach,
221                             &random_nehemiah);
222                         return (0);
223                 } else {
224 #ifndef KLD_MODULE
225                         if (bootverbose)
226 #endif
227                                 printf(
228                             "%s: VIA RNG feature is not present on this CPU\n",
229                                     random_nehemiah.ident);
230 #ifdef KLD_MODULE
231                         return (ENXIO);
232 #else
233                         return (0);
234 #endif
235                 }
236         }
237
238         return (EINVAL);
239 }
240
241 RANDOM_ADAPTOR_MODULE(nehemiah, nehemiah_modevent, 1);