]>
git.saurik.com Git - apple/xnu.git/blob - bsd/dev/random/randomdev.c
2 * Copyright (c) 1999, 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 #include <sys/param.h>
24 #include <sys/systm.h>
26 #include <sys/errno.h>
27 #include <sys/ioctl.h>
29 #include <sys/fcntl.h>
30 #include <miscfs/devfs/devfs.h>
31 #include <kern/lock.h>
33 #include <sys/malloc.h>
35 #include <dev/random/randomdev.h>
36 #include <dev/random/YarrowCoreLib/include/yarrow.h>
38 #define RANDOM_MAJOR -1 /* let the kernel pick the device number */
40 d_ioctl_t random_ioctl
;
43 * A struct describing which functions will get invoked for certain
46 static struct cdevsw random_cdevsw
=
48 random_open
, /* open */
49 random_close
, /* close */
50 random_read
, /* read */
51 random_write
, /* write */
52 random_ioctl
, /* ioctl */
56 eno_select
, /* select */
58 eno_strat
, /* strategy */
64 /* Used to detect whether we've already been initialized */
65 static int gRandomInstalled
= 0;
66 static PrngRef gPrngRef
;
67 static int gRandomError
= 1;
68 static mutex_t
*gYarrowMutex
= 0;
70 #define RESEED_TICKS 50 /* how long a reseed operation can take */
73 *Initialize ONLY the Yarrow generator.
75 void PreliminarySetup ()
77 prng_error_status perr
;
81 /* create a Yarrow object */
82 perr
= prngInitialize(&gPrngRef
);
84 printf ("Couldn't initialize Yarrow, /dev/random will not work.\n");
88 /* clear the error flag, reads and write should then work */
91 /* get a little non-deterministic data as an initial seed. */
95 * So how much of the system clock is entropic?
96 * It's hard to say, but assume that at least the
97 * least significant byte of a 64 bit structure
98 * is entropic. It's probably more, how can you figure
99 * the exact time the user turned the computer on, for example.
101 perr
= prngInput(gPrngRef
, (BYTE
*) &tt
, sizeof (tt
), SYSTEM_SOURCE
, 8);
103 /* an error, complain */
104 printf ("Couldn't seed Yarrow.\n");
108 /* turn the data around */
109 perr
= prngOutput(gPrngRef
, (BYTE
*) buffer
, sizeof (buffer
));
111 /* and scramble it some more */
112 perr
= prngForceReseed(gPrngRef
, RESEED_TICKS
);
114 /* make a mutex to control access */
115 gYarrowMutex
= mutex_alloc(0);
119 * Called to initialize our device,
120 * and to register ourselves with devfs
127 if (gRandomInstalled
)
130 /* install us in the file system */
131 gRandomInstalled
= 1;
133 /* setup yarrow and the mutex */
136 ret
= cdevsw_add(RANDOM_MAJOR
, &random_cdevsw
);
138 printf("random_init: failed to allocate a major number!\n");
139 gRandomInstalled
= 0;
143 devfs_make_node(makedev (ret
, 0), DEVFS_CHAR
,
144 UID_ROOT
, GID_WHEEL
, 0666, "random", 0);
148 * (which is exactly the same thing in our context)
150 devfs_make_node(makedev (ret
, 1), DEVFS_CHAR
,
151 UID_ROOT
, GID_WHEEL
, 0666, "urandom", 0);
155 random_ioctl(dev
, cmd
, data
, flag
, p
)
174 * Open the device. Make sure init happened, and make sure the caller is
179 random_open(dev_t dev
, int flags
, int devtype
, struct proc
*p
)
181 if (gRandomError
!= 0) {
182 /* forget it, yarrow didn't come up */
187 * if we are being opened for write,
188 * make sure that we have privledges do so
190 if (flags
& FWRITE
) {
191 if (securelevel
>= 2)
194 if ((securelevel
>= 1) && suser(p
->p_ucred
, &p
->p_acflag
))
196 #endif /* !__APPLE__ */
208 random_close(dev_t dev
, int flags
, int mode
, struct proc
*p
)
215 * Get entropic data from the Security Server, and use it to reseed the
219 random_write (dev_t dev
, struct uio
*uio
, int ioflag
)
224 if (gRandomError
!= 0) {
228 /* get control of the Yarrow instance, Yarrow is NOT thread safe */
229 mutex_lock(gYarrowMutex
);
231 /* Security server is sending us entropy */
233 while (uio
->uio_resid
> 0 && retCode
== 0) {
234 /* get the user's data */
235 int bytesToInput
= min(uio
->uio_resid
, sizeof (rdBuffer
));
236 retCode
= uiomove(rdBuffer
, bytesToInput
, uio
);
238 goto /*ugh*/ error_exit
;
240 /* put it in Yarrow */
241 if (prngInput(gPrngRef
, (BYTE
*) rdBuffer
,
242 sizeof (rdBuffer
), SYSTEM_SOURCE
,
243 sizeof (rdBuffer
) * 8) != 0) {
250 if (prngForceReseed(gPrngRef
, RESEED_TICKS
) != 0) {
255 /* retCode should be 0 at this point */
257 error_exit
: /* do this to make sure the mutex unlocks. */
258 mutex_unlock(gYarrowMutex
);
263 * return data to the caller. Results unpredictable.
266 random_read(dev_t dev
, struct uio
*uio
, int ioflag
)
271 if (gRandomError
!= 0)
274 /* lock down the mutex */
275 mutex_lock(gYarrowMutex
);
277 while (uio
->uio_resid
> 0 && retCode
== 0) {
278 /* get the user's data */
279 int bytesToRead
= min(uio
->uio_resid
, sizeof (wrBuffer
));
281 /* get the data from Yarrow */
282 if (prngOutput(gPrngRef
, (BYTE
*) wrBuffer
, sizeof (wrBuffer
)) != 0) {
283 printf ("Couldn't read data from Yarrow.\n");
285 /* something's really weird */
290 retCode
= uiomove(wrBuffer
, bytesToRead
, uio
);
299 mutex_unlock(gYarrowMutex
);
303 /* export good random numbers to the rest of the kernel */
305 read_random(void* buffer
, u_int numbytes
)
307 if (gYarrowMutex
== 0) { /* are we initialized? */
311 mutex_lock(gYarrowMutex
);
312 prngOutput(gPrngRef
, (BYTE
*) buffer
, numbytes
);
313 mutex_unlock(gYarrowMutex
);
317 * Return an unsigned long pseudo-random number.
323 read_random(&buf
, sizeof (buf
));