]> git.saurik.com Git - apple/xnu.git/blob - bsd/dev/random/randomdev.c
6cf09a1387bd0d3fe5387c09a15aa1ac199f6a20
[apple/xnu.git] / bsd / dev / random / randomdev.c
1 /*
2 * Copyright (c) 1999-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #include <sys/param.h>
25 #include <sys/systm.h>
26 #include <sys/proc.h>
27 #include <sys/errno.h>
28 #include <sys/ioctl.h>
29 #include <sys/conf.h>
30 #include <sys/fcntl.h>
31 #include <miscfs/devfs/devfs.h>
32 #include <kern/lock.h>
33 #include <sys/time.h>
34 #include <sys/malloc.h>
35 #include <sys/uio_internal.h>
36
37 #include <dev/random/randomdev.h>
38 #include <dev/random/YarrowCoreLib/include/yarrow.h>
39
40 #define RANDOM_MAJOR -1 /* let the kernel pick the device number */
41
42 d_ioctl_t random_ioctl;
43
44 /*
45 * A struct describing which functions will get invoked for certain
46 * actions.
47 */
48 static struct cdevsw random_cdevsw =
49 {
50 random_open, /* open */
51 random_close, /* close */
52 random_read, /* read */
53 random_write, /* write */
54 random_ioctl, /* ioctl */
55 (stop_fcn_t *)nulldev, /* stop */
56 (reset_fcn_t *)nulldev, /* reset */
57 NULL, /* tty's */
58 eno_select, /* select */
59 eno_mmap, /* mmap */
60 eno_strat, /* strategy */
61 eno_getc, /* getc */
62 eno_putc, /* putc */
63 0 /* type */
64 };
65
66 /* Used to detect whether we've already been initialized */
67 static int gRandomInstalled = 0;
68 static PrngRef gPrngRef;
69 static int gRandomError = 1;
70 static mutex_t *gYarrowMutex = 0;
71
72 #define RESEED_TICKS 50 /* how long a reseed operation can take */
73
74
75 /*
76 *Initialize ONLY the Yarrow generator.
77 */
78 void PreliminarySetup( void )
79 {
80 prng_error_status perr;
81 struct timeval tt;
82 char buffer [16];
83
84 /* create a Yarrow object */
85 perr = prngInitialize(&gPrngRef);
86 if (perr != 0) {
87 printf ("Couldn't initialize Yarrow, /dev/random will not work.\n");
88 return;
89 }
90
91 /* clear the error flag, reads and write should then work */
92 gRandomError = 0;
93
94 /* get a little non-deterministic data as an initial seed. */
95 microtime(&tt);
96
97 /*
98 * So how much of the system clock is entropic?
99 * It's hard to say, but assume that at least the
100 * least significant byte of a 64 bit structure
101 * is entropic. It's probably more, how can you figure
102 * the exact time the user turned the computer on, for example.
103 */
104 perr = prngInput(gPrngRef, (BYTE*) &tt, sizeof (tt), SYSTEM_SOURCE, 8);
105 if (perr != 0) {
106 /* an error, complain */
107 printf ("Couldn't seed Yarrow.\n");
108 return;
109 }
110
111 /* turn the data around */
112 perr = prngOutput(gPrngRef, (BYTE*) buffer, sizeof (buffer));
113
114 /* and scramble it some more */
115 perr = prngForceReseed(gPrngRef, RESEED_TICKS);
116
117 /* make a mutex to control access */
118 gYarrowMutex = mutex_alloc(0);
119 }
120
121 /*
122 * Called to initialize our device,
123 * and to register ourselves with devfs
124 */
125 void
126 random_init( void )
127 {
128 int ret;
129
130 if (gRandomInstalled)
131 return;
132
133 /* install us in the file system */
134 gRandomInstalled = 1;
135
136 /* setup yarrow and the mutex */
137 PreliminarySetup();
138
139 ret = cdevsw_add(RANDOM_MAJOR, &random_cdevsw);
140 if (ret < 0) {
141 printf("random_init: failed to allocate a major number!\n");
142 gRandomInstalled = 0;
143 return;
144 }
145
146 devfs_make_node(makedev (ret, 0), DEVFS_CHAR,
147 UID_ROOT, GID_WHEEL, 0666, "random", 0);
148
149 /*
150 * also make urandom
151 * (which is exactly the same thing in our context)
152 */
153 devfs_make_node(makedev (ret, 1), DEVFS_CHAR,
154 UID_ROOT, GID_WHEEL, 0666, "urandom", 0);
155 }
156
157 int
158 random_ioctl( __unused dev_t dev, u_long cmd, __unused caddr_t data,
159 __unused int flag, __unused struct proc *p )
160 {
161 switch (cmd) {
162 case FIONBIO:
163 case FIOASYNC:
164 break;
165 default:
166 return ENODEV;
167 }
168
169 return (0);
170 }
171
172 /*
173 * Open the device. Make sure init happened, and make sure the caller is
174 * authorized.
175 */
176
177 int
178 random_open(__unused dev_t dev, int flags, __unused int devtype, __unused struct proc *p)
179 {
180 if (gRandomError != 0) {
181 /* forget it, yarrow didn't come up */
182 return (ENOTSUP);
183 }
184
185 /*
186 * if we are being opened for write,
187 * make sure that we have privledges do so
188 */
189 if (flags & FWRITE) {
190 if (securelevel >= 2)
191 return (EPERM);
192 #ifndef __APPLE__
193 if ((securelevel >= 1) && proc_suser(p))
194 return (EPERM);
195 #endif /* !__APPLE__ */
196 }
197
198 return (0);
199 }
200
201
202 /*
203 * close the device.
204 */
205
206 int
207 random_close(__unused dev_t dev, __unused int flags, __unused int mode, __unused struct proc *p)
208 {
209 return (0);
210 }
211
212
213 /*
214 * Get entropic data from the Security Server, and use it to reseed the
215 * prng.
216 */
217 int
218 random_write (__unused dev_t dev, struct uio *uio, __unused int ioflag)
219 {
220 int retCode = 0;
221 char rdBuffer[256];
222
223 if (gRandomError != 0) {
224 return (ENOTSUP);
225 }
226
227 /* get control of the Yarrow instance, Yarrow is NOT thread safe */
228 mutex_lock(gYarrowMutex);
229
230 /* Security server is sending us entropy */
231
232 while (uio_resid(uio) > 0 && retCode == 0) {
233 /* get the user's data */
234 // LP64todo - fix this! uio_resid may be 64-bit value
235 int bytesToInput = min(uio_resid(uio), sizeof (rdBuffer));
236 retCode = uiomove(rdBuffer, bytesToInput, uio);
237 if (retCode != 0)
238 goto /*ugh*/ error_exit;
239
240 /* put it in Yarrow */
241 if (prngInput(gPrngRef, (BYTE*) rdBuffer,
242 sizeof (rdBuffer), SYSTEM_SOURCE,
243 sizeof (rdBuffer) * 8) != 0) {
244 retCode = EIO;
245 goto error_exit;
246 }
247 }
248
249 /* force a reseed */
250 if (prngForceReseed(gPrngRef, RESEED_TICKS) != 0) {
251 retCode = EIO;
252 goto error_exit;
253 }
254
255 /* retCode should be 0 at this point */
256
257 error_exit: /* do this to make sure the mutex unlocks. */
258 mutex_unlock(gYarrowMutex);
259 return (retCode);
260 }
261
262 /*
263 * return data to the caller. Results unpredictable.
264 */
265 int
266 random_read(__unused dev_t dev, struct uio *uio, __unused int ioflag)
267 {
268 int retCode = 0;
269 char wrBuffer[512];
270
271 if (gRandomError != 0)
272 return (ENOTSUP);
273
274 /* lock down the mutex */
275 mutex_lock(gYarrowMutex);
276
277 while (uio_resid(uio) > 0 && retCode == 0) {
278 /* get the user's data */
279 // LP64todo - fix this! uio_resid may be 64-bit value
280 int bytesToRead = min(uio_resid(uio), sizeof (wrBuffer));
281
282 /* get the data from Yarrow */
283 if (prngOutput(gPrngRef, (BYTE *) wrBuffer, sizeof (wrBuffer)) != 0) {
284 printf ("Couldn't read data from Yarrow.\n");
285
286 /* something's really weird */
287 retCode = EIO;
288 goto error_exit;
289 }
290
291 retCode = uiomove(wrBuffer, bytesToRead, uio);
292
293 if (retCode != 0)
294 goto error_exit;
295 }
296
297 retCode = 0;
298
299 error_exit:
300 mutex_unlock(gYarrowMutex);
301 return retCode;
302 }
303
304 /* export good random numbers to the rest of the kernel */
305 void
306 read_random(void* buffer, u_int numbytes)
307 {
308 if (gYarrowMutex == 0) { /* are we initialized? */
309 PreliminarySetup ();
310 }
311
312 mutex_lock(gYarrowMutex);
313 prngOutput(gPrngRef, (BYTE *) buffer, numbytes);
314 mutex_unlock(gYarrowMutex);
315 }
316
317 /*
318 * Return an unsigned long pseudo-random number.
319 */
320 u_long
321 RandomULong( void )
322 {
323 u_long buf;
324 read_random(&buf, sizeof (buf));
325 return (buf);
326 }
327