]>
git.saurik.com Git - apple/xnu.git/blob - bsd/dev/random/randomdev.c
63d66a6ea79ea727bb41302a58fbb25af75f7d85
2 * Copyright (c) 1999, 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 #include <sys/param.h>
27 #include <sys/systm.h>
29 #include <sys/errno.h>
30 #include <sys/ioctl.h>
32 #include <sys/fcntl.h>
33 #include <miscfs/devfs/devfs.h>
34 #include <kern/lock.h>
36 #include <sys/malloc.h>
38 #include <dev/random/randomdev.h>
39 #include <dev/random/YarrowCoreLib/include/yarrow.h>
41 #define RANDOM_MAJOR -1 /* let the kernel pick the device number */
43 d_ioctl_t random_ioctl
;
46 * A struct describing which functions will get invoked for certain
49 static struct cdevsw random_cdevsw
=
51 random_open
, /* open */
52 random_close
, /* close */
53 random_read
, /* read */
54 random_write
, /* write */
55 random_ioctl
, /* ioctl */
59 eno_select
, /* select */
61 eno_strat
, /* strategy */
67 /* Used to detect whether we've already been initialized */
68 static int gRandomInstalled
= 0;
69 static PrngRef gPrngRef
;
70 static int gRandomError
= 1;
71 static mutex_t
*gYarrowMutex
= 0;
73 #define RESEED_TICKS 50 /* how long a reseed operation can take */
76 *Initialize ONLY the Yarrow generator.
78 void PreliminarySetup ()
80 prng_error_status perr
;
84 /* create a Yarrow object */
85 perr
= prngInitialize(&gPrngRef
);
87 printf ("Couldn't initialize Yarrow, /dev/random will not work.\n");
91 /* clear the error flag, reads and write should then work */
94 /* get a little non-deterministic data as an initial seed. */
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.
104 perr
= prngInput(gPrngRef
, (BYTE
*) &tt
, sizeof (tt
), SYSTEM_SOURCE
, 8);
106 /* an error, complain */
107 printf ("Couldn't seed Yarrow.\n");
111 /* turn the data around */
112 perr
= prngOutput(gPrngRef
, (BYTE
*) buffer
, sizeof (buffer
));
114 /* and scramble it some more */
115 perr
= prngForceReseed(gPrngRef
, RESEED_TICKS
);
117 /* make a mutex to control access */
118 gYarrowMutex
= mutex_alloc(0);
122 * Called to initialize our device,
123 * and to register ourselves with devfs
130 if (gRandomInstalled
)
133 /* install us in the file system */
134 gRandomInstalled
= 1;
136 /* setup yarrow and the mutex */
139 ret
= cdevsw_add(RANDOM_MAJOR
, &random_cdevsw
);
141 printf("random_init: failed to allocate a major number!\n");
142 gRandomInstalled
= 0;
146 devfs_make_node(makedev (ret
, 0), DEVFS_CHAR
,
147 UID_ROOT
, GID_WHEEL
, 0666, "random", 0);
151 * (which is exactly the same thing in our context)
153 devfs_make_node(makedev (ret
, 1), DEVFS_CHAR
,
154 UID_ROOT
, GID_WHEEL
, 0666, "urandom", 0);
158 random_ioctl(dev
, cmd
, data
, flag
, p
)
177 * Open the device. Make sure init happened, and make sure the caller is
182 random_open(dev_t dev
, int flags
, int devtype
, struct proc
*p
)
184 if (gRandomError
!= 0) {
185 /* forget it, yarrow didn't come up */
190 * if we are being opened for write,
191 * make sure that we have privledges do so
193 if (flags
& FWRITE
) {
194 if (securelevel
>= 2)
197 if ((securelevel
>= 1) && suser(p
->p_ucred
, &p
->p_acflag
))
199 #endif /* !__APPLE__ */
211 random_close(dev_t dev
, int flags
, int mode
, struct proc
*p
)
218 * Get entropic data from the Security Server, and use it to reseed the
222 random_write (dev_t dev
, struct uio
*uio
, int ioflag
)
227 if (gRandomError
!= 0) {
231 /* get control of the Yarrow instance, Yarrow is NOT thread safe */
232 mutex_lock(gYarrowMutex
);
234 /* Security server is sending us entropy */
236 while (uio
->uio_resid
> 0 && retCode
== 0) {
237 /* get the user's data */
238 int bytesToInput
= min(uio
->uio_resid
, sizeof (rdBuffer
));
239 retCode
= uiomove(rdBuffer
, bytesToInput
, uio
);
241 goto /*ugh*/ error_exit
;
243 /* put it in Yarrow */
244 if (prngInput(gPrngRef
, (BYTE
*) rdBuffer
,
245 sizeof (rdBuffer
), SYSTEM_SOURCE
,
246 sizeof (rdBuffer
) * 8) != 0) {
253 if (prngForceReseed(gPrngRef
, RESEED_TICKS
) != 0) {
258 /* retCode should be 0 at this point */
260 error_exit
: /* do this to make sure the mutex unlocks. */
261 mutex_unlock(gYarrowMutex
);
266 * return data to the caller. Results unpredictable.
269 random_read(dev_t dev
, struct uio
*uio
, int ioflag
)
274 if (gRandomError
!= 0)
277 /* lock down the mutex */
278 mutex_lock(gYarrowMutex
);
280 while (uio
->uio_resid
> 0 && retCode
== 0) {
281 /* get the user's data */
282 int bytesToRead
= min(uio
->uio_resid
, sizeof (wrBuffer
));
284 /* get the data from Yarrow */
285 if (prngOutput(gPrngRef
, (BYTE
*) wrBuffer
, sizeof (wrBuffer
)) != 0) {
286 printf ("Couldn't read data from Yarrow.\n");
288 /* something's really weird */
293 retCode
= uiomove(wrBuffer
, bytesToRead
, uio
);
302 mutex_unlock(gYarrowMutex
);
306 /* export good random numbers to the rest of the kernel */
308 read_random(void* buffer
, u_int numbytes
)
310 if (gYarrowMutex
== 0) { /* are we initialized? */
314 mutex_lock(gYarrowMutex
);
315 prngOutput(gPrngRef
, (BYTE
*) buffer
, numbytes
);
316 mutex_unlock(gYarrowMutex
);
320 * Return an unsigned long pseudo-random number.
326 read_random(&buf
, sizeof (buf
));