]>
git.saurik.com Git - apple/xnu.git/blob - bsd/dev/random/randomdev.c
2 * Copyright (c) 1999-2004 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>
34 #include <sys/uio_internal.h>
36 #include <dev/random/randomdev.h>
37 #include <dev/random/YarrowCoreLib/include/yarrow.h>
39 #define RANDOM_MAJOR -1 /* let the kernel pick the device number */
41 d_ioctl_t random_ioctl
;
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 random_ioctl
, /* ioctl */
54 (stop_fcn_t
*)nulldev
, /* stop */
55 (reset_fcn_t
*)nulldev
, /* reset */
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 */
75 *Initialize ONLY the Yarrow generator.
77 void PreliminarySetup( void )
79 prng_error_status perr
;
83 /* create a Yarrow object */
84 perr
= prngInitialize(&gPrngRef
);
86 printf ("Couldn't initialize Yarrow, /dev/random will not work.\n");
90 /* clear the error flag, reads and write should then work */
93 /* get a little non-deterministic data as an initial seed. */
97 * So how much of the system clock is entropic?
98 * It's hard to say, but assume that at least the
99 * least significant byte of a 64 bit structure
100 * is entropic. It's probably more, how can you figure
101 * the exact time the user turned the computer on, for example.
103 perr
= prngInput(gPrngRef
, (BYTE
*) &tt
, sizeof (tt
), SYSTEM_SOURCE
, 8);
105 /* an error, complain */
106 printf ("Couldn't seed Yarrow.\n");
110 /* turn the data around */
111 perr
= prngOutput(gPrngRef
, (BYTE
*) buffer
, sizeof (buffer
));
113 /* and scramble it some more */
114 perr
= prngForceReseed(gPrngRef
, RESEED_TICKS
);
116 /* make a mutex to control access */
117 gYarrowMutex
= mutex_alloc(0);
121 * Called to initialize our device,
122 * and to register ourselves with devfs
129 if (gRandomInstalled
)
132 /* install us in the file system */
133 gRandomInstalled
= 1;
135 /* setup yarrow and the mutex */
138 ret
= cdevsw_add(RANDOM_MAJOR
, &random_cdevsw
);
140 printf("random_init: failed to allocate a major number!\n");
141 gRandomInstalled
= 0;
145 devfs_make_node(makedev (ret
, 0), DEVFS_CHAR
,
146 UID_ROOT
, GID_WHEEL
, 0666, "random", 0);
150 * (which is exactly the same thing in our context)
152 devfs_make_node(makedev (ret
, 1), DEVFS_CHAR
,
153 UID_ROOT
, GID_WHEEL
, 0666, "urandom", 0);
157 random_ioctl( __unused dev_t dev
, u_long cmd
, __unused caddr_t data
,
158 __unused
int flag
, __unused
struct proc
*p
)
172 * Open the device. Make sure init happened, and make sure the caller is
177 random_open(__unused dev_t dev
, int flags
, __unused
int devtype
, __unused
struct proc
*p
)
179 if (gRandomError
!= 0) {
180 /* forget it, yarrow didn't come up */
185 * if we are being opened for write,
186 * make sure that we have privledges do so
188 if (flags
& FWRITE
) {
189 if (securelevel
>= 2)
192 if ((securelevel
>= 1) && proc_suser(p
))
194 #endif /* !__APPLE__ */
206 random_close(__unused dev_t dev
, __unused
int flags
, __unused
int mode
, __unused
struct proc
*p
)
213 * Get entropic data from the Security Server, and use it to reseed the
217 random_write (__unused dev_t dev
, struct uio
*uio
, __unused
int ioflag
)
222 if (gRandomError
!= 0) {
226 /* get control of the Yarrow instance, Yarrow is NOT thread safe */
227 mutex_lock(gYarrowMutex
);
229 /* Security server is sending us entropy */
231 while (uio_resid(uio
) > 0 && retCode
== 0) {
232 /* get the user's data */
233 // LP64todo - fix this! uio_resid may be 64-bit value
234 int bytesToInput
= min(uio_resid(uio
), sizeof (rdBuffer
));
235 retCode
= uiomove(rdBuffer
, bytesToInput
, uio
);
237 goto /*ugh*/ error_exit
;
239 /* put it in Yarrow */
240 if (prngInput(gPrngRef
, (BYTE
*) rdBuffer
,
241 sizeof (rdBuffer
), SYSTEM_SOURCE
,
242 sizeof (rdBuffer
) * 8) != 0) {
249 if (prngForceReseed(gPrngRef
, RESEED_TICKS
) != 0) {
254 /* retCode should be 0 at this point */
256 error_exit
: /* do this to make sure the mutex unlocks. */
257 mutex_unlock(gYarrowMutex
);
262 * return data to the caller. Results unpredictable.
265 random_read(__unused dev_t dev
, struct uio
*uio
, __unused
int ioflag
)
270 if (gRandomError
!= 0)
273 /* lock down the mutex */
274 mutex_lock(gYarrowMutex
);
276 while (uio_resid(uio
) > 0 && retCode
== 0) {
277 /* get the user's data */
278 // LP64todo - fix this! uio_resid may be 64-bit value
279 int bytesToRead
= min(uio_resid(uio
), 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
));