]>
git.saurik.com Git - apple/xnu.git/blob - bsd/dev/random/randomdev.c
6cf09a1387bd0d3fe5387c09a15aa1ac199f6a20
2 * Copyright (c) 1999-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <sys/param.h>
25 #include <sys/systm.h>
27 #include <sys/errno.h>
28 #include <sys/ioctl.h>
30 #include <sys/fcntl.h>
31 #include <miscfs/devfs/devfs.h>
32 #include <kern/lock.h>
34 #include <sys/malloc.h>
35 #include <sys/uio_internal.h>
37 #include <dev/random/randomdev.h>
38 #include <dev/random/YarrowCoreLib/include/yarrow.h>
40 #define RANDOM_MAJOR -1 /* let the kernel pick the device number */
42 d_ioctl_t random_ioctl
;
45 * A struct describing which functions will get invoked for certain
48 static struct cdevsw random_cdevsw
=
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 */
58 eno_select
, /* select */
60 eno_strat
, /* strategy */
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;
72 #define RESEED_TICKS 50 /* how long a reseed operation can take */
76 *Initialize ONLY the Yarrow generator.
78 void PreliminarySetup( void )
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( __unused dev_t dev
, u_long cmd
, __unused caddr_t data
,
159 __unused
int flag
, __unused
struct proc
*p
)
173 * Open the device. Make sure init happened, and make sure the caller is
178 random_open(__unused dev_t dev
, int flags
, __unused
int devtype
, __unused
struct proc
*p
)
180 if (gRandomError
!= 0) {
181 /* forget it, yarrow didn't come up */
186 * if we are being opened for write,
187 * make sure that we have privledges do so
189 if (flags
& FWRITE
) {
190 if (securelevel
>= 2)
193 if ((securelevel
>= 1) && proc_suser(p
))
195 #endif /* !__APPLE__ */
207 random_close(__unused dev_t dev
, __unused
int flags
, __unused
int mode
, __unused
struct proc
*p
)
214 * Get entropic data from the Security Server, and use it to reseed the
218 random_write (__unused dev_t dev
, struct uio
*uio
, __unused
int ioflag
)
223 if (gRandomError
!= 0) {
227 /* get control of the Yarrow instance, Yarrow is NOT thread safe */
228 mutex_lock(gYarrowMutex
);
230 /* Security server is sending us entropy */
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
);
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(__unused dev_t dev
, struct uio
*uio
, __unused
int ioflag
)
271 if (gRandomError
!= 0)
274 /* lock down the mutex */
275 mutex_lock(gYarrowMutex
);
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
));
282 /* get the data from Yarrow */
283 if (prngOutput(gPrngRef
, (BYTE
*) wrBuffer
, sizeof (wrBuffer
)) != 0) {
284 printf ("Couldn't read data from Yarrow.\n");
286 /* something's really weird */
291 retCode
= uiomove(wrBuffer
, bytesToRead
, uio
);
300 mutex_unlock(gYarrowMutex
);
304 /* export good random numbers to the rest of the kernel */
306 read_random(void* buffer
, u_int numbytes
)
308 if (gYarrowMutex
== 0) { /* are we initialized? */
312 mutex_lock(gYarrowMutex
);
313 prngOutput(gPrngRef
, (BYTE
*) buffer
, numbytes
);
314 mutex_unlock(gYarrowMutex
);
318 * Return an unsigned long pseudo-random number.
324 read_random(&buf
, sizeof (buf
));