]>
git.saurik.com Git - apple/xnu.git/blob - bsd/dev/random/randomdev.c
2 * Copyright (c) 1999, 2000-2002 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 */
41 * A struct describing which functions will get invoked for certain
44 static struct cdevsw random_cdevsw
=
46 random_open
, /* open */
47 random_close
, /* close */
48 random_read
, /* read */
49 random_write
, /* write */
50 eno_ioctl
, /* ioctl */
54 eno_select
, /* select */
56 eno_strat
, /* strategy */
62 /* Used to detect whether we've already been initialized */
63 static int gRandomInstalled
= 0;
64 static PrngRef gPrngRef
;
65 static int gRandomError
= 1;
66 static mutex_t
*gYarrowMutex
= 0;
68 #define RESEED_TICKS 50 /* how long a reseed operation can take */
71 *Initialize ONLY the Yarrow generator.
73 void PreliminarySetup ()
75 prng_error_status perr
;
79 /* create a Yarrow object */
80 perr
= prngInitialize(&gPrngRef
);
82 printf ("Couldn't initialize Yarrow, /dev/random will not work.\n");
86 /* clear the error flag, reads and write should then work */
89 /* get a little non-deterministic data as an initial seed. */
93 * So how much of the system clock is entropic?
94 * It's hard to say, but assume that at least the
95 * least significant byte of a 64 bit structure
96 * is entropic. It's probably more, how can you figure
97 * the exact time the user turned the computer on, for example.
99 perr
= prngInput(gPrngRef
, (BYTE
*) &tt
, sizeof (tt
), SYSTEM_SOURCE
, 8);
101 /* an error, complain */
102 printf ("Couldn't seed Yarrow.\n");
106 /* turn the data around */
107 perr
= prngOutput(gPrngRef
, (BYTE
*) buffer
, sizeof (buffer
));
109 /* and scramble it some more */
110 perr
= prngForceReseed(gPrngRef
, RESEED_TICKS
);
112 /* make a mutex to control access */
113 gYarrowMutex
= mutex_alloc(0);
117 * Called to initialize our device,
118 * and to register ourselves with devfs
125 if (gRandomInstalled
)
128 /* install us in the file system */
129 gRandomInstalled
= 1;
131 /* setup yarrow and the mutex */
134 ret
= cdevsw_add(RANDOM_MAJOR
, &random_cdevsw
);
136 printf("random_init: failed to allocate a major number!\n");
137 gRandomInstalled
= 0;
141 devfs_make_node(makedev (ret
, 0), DEVFS_CHAR
,
142 UID_ROOT
, GID_WHEEL
, 0644, "random", 0);
146 * (which is exactly the same thing in our context)
148 devfs_make_node(makedev (ret
, 1), DEVFS_CHAR
,
149 UID_ROOT
, GID_WHEEL
, 0644, "urandom", 0);
153 * Open the device. Make sure init happened, and make sure the caller is
158 random_open(dev_t dev
, int flags
, int devtype
, struct proc
*p
)
160 if (gRandomError
!= 0) {
161 /* forget it, yarrow didn't come up */
166 * if we are being opened for write,
167 * make sure that we have privledges do so
169 if (flags
& FWRITE
) {
170 if (securelevel
>= 2)
172 if ((securelevel
>= 1) && suser(p
->p_ucred
, &p
->p_acflag
))
185 random_close(dev_t dev
, int flags
, int mode
, struct proc
*p
)
192 * Get entropic data from the Security Server, and use it to reseed the
196 random_write (dev_t dev
, struct uio
*uio
, int ioflag
)
201 if (gRandomError
!= 0) {
205 /* get control of the Yarrow instance, Yarrow is NOT thread safe */
206 mutex_lock(gYarrowMutex
);
208 /* Security server is sending us entropy */
210 while (uio
->uio_resid
> 0 && retCode
== 0) {
211 /* get the user's data */
212 int bytesToInput
= min(uio
->uio_resid
, sizeof (rdBuffer
));
213 retCode
= uiomove(rdBuffer
, bytesToInput
, uio
);
215 goto /*ugh*/ error_exit
;
217 /* put it in Yarrow */
218 if (prngInput(gPrngRef
, (BYTE
*) rdBuffer
,
219 sizeof (rdBuffer
), SYSTEM_SOURCE
,
220 sizeof (rdBuffer
) * 8) != 0) {
227 if (prngForceReseed(gPrngRef
, RESEED_TICKS
) != 0) {
232 /* retCode should be 0 at this point */
234 error_exit
: /* do this to make sure the mutex unlocks. */
235 mutex_unlock(gYarrowMutex
);
240 * return data to the caller. Results unpredictable.
243 random_read(dev_t dev
, struct uio
*uio
, int ioflag
)
248 if (gRandomError
!= 0)
251 /* lock down the mutex */
252 mutex_lock(gYarrowMutex
);
254 while (uio
->uio_resid
> 0 && retCode
== 0) {
255 /* get the user's data */
256 int bytesToRead
= min(uio
->uio_resid
, sizeof (wrBuffer
));
258 /* get the data from Yarrow */
259 if (prngOutput(gPrngRef
, (BYTE
*) wrBuffer
, sizeof (wrBuffer
)) != 0) {
260 printf ("Couldn't read data from Yarrow.\n");
262 /* something's really weird */
267 retCode
= uiomove(wrBuffer
, bytesToRead
, uio
);
276 mutex_unlock(gYarrowMutex
);
280 /* export good random numbers to the rest of the kernel */
282 read_random(void* buffer
, u_int numbytes
)
284 if (gYarrowMutex
== 0) { /* are we initialized? */
288 mutex_lock(gYarrowMutex
);
289 prngOutput(gPrngRef
, (BYTE
*) buffer
, numbytes
);
290 mutex_unlock(gYarrowMutex
);
294 * Return an unsigned long pseudo-random number.
300 read_random(&buf
, sizeof (buf
));