]>
git.saurik.com Git - apple/xnu.git/blob - bsd/dev/random/randomdev.c
96f997e24d0324d9da3231448ce3c48ec9d78580
2 * Copyright (c) 1999, 2000-2002 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 */
44 * A struct describing which functions will get invoked for certain
47 static struct cdevsw random_cdevsw
=
49 random_open
, /* open */
50 random_close
, /* close */
51 random_read
, /* read */
52 random_write
, /* write */
53 eno_ioctl
, /* ioctl */
57 eno_select
, /* select */
59 eno_strat
, /* strategy */
65 /* Used to detect whether we've already been initialized */
66 static int gRandomInstalled
= 0;
67 static PrngRef gPrngRef
;
68 static int gRandomError
= 1;
69 static mutex_t
*gYarrowMutex
= 0;
71 #define RESEED_TICKS 50 /* how long a reseed operation can take */
74 *Initialize ONLY the Yarrow generator.
76 void PreliminarySetup ()
78 prng_error_status perr
;
82 /* create a Yarrow object */
83 perr
= prngInitialize(&gPrngRef
);
85 printf ("Couldn't initialize Yarrow, /dev/random will not work.\n");
89 /* clear the error flag, reads and write should then work */
92 /* get a little non-deterministic data as an initial seed. */
96 * So how much of the system clock is entropic?
97 * It's hard to say, but assume that at least the
98 * least significant byte of a 64 bit structure
99 * is entropic. It's probably more, how can you figure
100 * the exact time the user turned the computer on, for example.
102 perr
= prngInput(gPrngRef
, (BYTE
*) &tt
, sizeof (tt
), SYSTEM_SOURCE
, 8);
104 /* an error, complain */
105 printf ("Couldn't seed Yarrow.\n");
109 /* turn the data around */
110 perr
= prngOutput(gPrngRef
, (BYTE
*) buffer
, sizeof (buffer
));
112 /* and scramble it some more */
113 perr
= prngForceReseed(gPrngRef
, RESEED_TICKS
);
115 /* make a mutex to control access */
116 gYarrowMutex
= mutex_alloc(0);
120 * Called to initialize our device,
121 * and to register ourselves with devfs
128 if (gRandomInstalled
)
131 /* install us in the file system */
132 gRandomInstalled
= 1;
134 /* setup yarrow and the mutex */
137 ret
= cdevsw_add(RANDOM_MAJOR
, &random_cdevsw
);
139 printf("random_init: failed to allocate a major number!\n");
140 gRandomInstalled
= 0;
144 devfs_make_node(makedev (ret
, 0), DEVFS_CHAR
,
145 UID_ROOT
, GID_WHEEL
, 0644, "random", 0);
149 * (which is exactly the same thing in our context)
151 devfs_make_node(makedev (ret
, 1), DEVFS_CHAR
,
152 UID_ROOT
, GID_WHEEL
, 0644, "urandom", 0);
156 * Open the device. Make sure init happened, and make sure the caller is
161 random_open(dev_t dev
, int flags
, int devtype
, struct proc
*p
)
163 if (gRandomError
!= 0) {
164 /* forget it, yarrow didn't come up */
169 * if we are being opened for write,
170 * make sure that we have privledges do so
172 if (flags
& FWRITE
) {
173 if (securelevel
>= 2)
175 if ((securelevel
>= 1) && suser(p
->p_ucred
, &p
->p_acflag
))
188 random_close(dev_t dev
, int flags
, int mode
, struct proc
*p
)
195 * Get entropic data from the Security Server, and use it to reseed the
199 random_write (dev_t dev
, struct uio
*uio
, int ioflag
)
204 if (gRandomError
!= 0) {
208 /* get control of the Yarrow instance, Yarrow is NOT thread safe */
209 mutex_lock(gYarrowMutex
);
211 /* Security server is sending us entropy */
213 while (uio
->uio_resid
> 0 && retCode
== 0) {
214 /* get the user's data */
215 int bytesToInput
= min(uio
->uio_resid
, sizeof (rdBuffer
));
216 retCode
= uiomove(rdBuffer
, bytesToInput
, uio
);
218 goto /*ugh*/ error_exit
;
220 /* put it in Yarrow */
221 if (prngInput(gPrngRef
, (BYTE
*) rdBuffer
,
222 sizeof (rdBuffer
), SYSTEM_SOURCE
,
223 sizeof (rdBuffer
) * 8) != 0) {
230 if (prngForceReseed(gPrngRef
, RESEED_TICKS
) != 0) {
235 /* retCode should be 0 at this point */
237 error_exit
: /* do this to make sure the mutex unlocks. */
238 mutex_unlock(gYarrowMutex
);
243 * return data to the caller. Results unpredictable.
246 random_read(dev_t dev
, struct uio
*uio
, int ioflag
)
251 if (gRandomError
!= 0)
254 /* lock down the mutex */
255 mutex_lock(gYarrowMutex
);
257 while (uio
->uio_resid
> 0 && retCode
== 0) {
258 /* get the user's data */
259 int bytesToRead
= min(uio
->uio_resid
, sizeof (wrBuffer
));
261 /* get the data from Yarrow */
262 if (prngOutput(gPrngRef
, (BYTE
*) wrBuffer
, sizeof (wrBuffer
)) != 0) {
263 printf ("Couldn't read data from Yarrow.\n");
265 /* something's really weird */
270 retCode
= uiomove(wrBuffer
, bytesToRead
, uio
);
279 mutex_unlock(gYarrowMutex
);
283 /* export good random numbers to the rest of the kernel */
285 read_random(void* buffer
, u_int numbytes
)
287 if (gYarrowMutex
== 0) { /* are we initialized? */
291 mutex_lock(gYarrowMutex
);
292 prngOutput(gPrngRef
, (BYTE
*) buffer
, numbytes
);
293 mutex_unlock(gYarrowMutex
);
297 * Return an unsigned long pseudo-random number.
303 read_random(&buf
, sizeof (buf
));