2 * Copyright (c) 1999-2009 Apple, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/errno.h>
34 #include <sys/ioctl.h>
36 #include <sys/fcntl.h>
38 #include <miscfs/devfs/devfs.h>
39 #include <kern/locks.h>
40 #include <kern/clock.h>
42 #include <sys/malloc.h>
43 #include <sys/uio_internal.h>
45 #include <dev/random/randomdev.h>
47 #include <libkern/OSByteOrder.h>
48 #include <libkern/OSAtomic.h>
50 #include <mach/mach_time.h>
52 #define RANDOM_MAJOR -1 /* let the kernel pick the device number */
53 #define RANDOM_MINOR 0
54 #define URANDOM_MINOR 1
56 d_ioctl_t random_ioctl
;
59 * A struct describing which functions will get invoked for certain
62 static struct cdevsw random_cdevsw
=
64 random_open
, /* open */
65 random_close
, /* close */
66 random_read
, /* read */
67 random_write
, /* write */
68 random_ioctl
, /* ioctl */
69 (stop_fcn_t
*)nulldev
, /* stop */
70 (reset_fcn_t
*)nulldev
, /* reset */
72 eno_select
, /* select */
74 eno_strat
, /* strategy */
82 * Called to initialize our device,
83 * and to register ourselves with devfs
90 ret
= cdevsw_add(RANDOM_MAJOR
, &random_cdevsw
);
92 panic("random_init: failed to allocate a major number!");
95 devfs_make_node(makedev (ret
, RANDOM_MINOR
), DEVFS_CHAR
,
96 UID_ROOT
, GID_WHEEL
, 0666, "random", 0);
100 * (which is exactly the same thing in our context)
102 devfs_make_node(makedev (ret
, URANDOM_MINOR
), DEVFS_CHAR
,
103 UID_ROOT
, GID_WHEEL
, 0666, "urandom", 0);
108 random_ioctl( __unused dev_t dev
, u_long cmd
, __unused caddr_t data
,
109 __unused
int flag
, __unused
struct proc
*p
)
123 * Open the device. Make sure init happened, and make sure the caller is
128 random_open(__unused dev_t dev
, int flags
, __unused
int devtype
, __unused
struct proc
*p
)
131 * if we are being opened for write,
132 * make sure that we have privledges do so
134 if (flags
& FWRITE
) {
135 if (securelevel
>= 2)
138 if ((securelevel
>= 1) && proc_suser(p
))
140 #endif /* !__APPLE__ */
152 random_close(__unused dev_t dev
, __unused
int flags
, __unused
int mode
, __unused
struct proc
*p
)
159 * Get entropic data from the Security Server, and use it to reseed the
163 random_write (dev_t dev
, struct uio
*uio
, __unused
int ioflag
)
168 if (minor(dev
) != RANDOM_MINOR
)
171 /* Security server is sending us entropy */
173 while (uio_resid(uio
) > 0 && retCode
== 0) {
174 /* get the user's data */
175 int bytesToInput
= MIN(uio_resid(uio
),
176 (user_ssize_t
) sizeof(rdBuffer
));
177 retCode
= uiomove(rdBuffer
, bytesToInput
, uio
);
180 retCode
= write_random(rdBuffer
, bytesToInput
);
189 * return data to the caller. Results unpredictable.
192 random_read(__unused dev_t dev
, struct uio
*uio
, __unused
int ioflag
)
197 user_ssize_t bytes_remaining
= uio_resid(uio
);
198 while (bytes_remaining
> 0 && retCode
== 0) {
199 int bytesToRead
= MIN(bytes_remaining
,
200 (user_ssize_t
) sizeof(buffer
));
201 read_random(buffer
, bytesToRead
);
203 retCode
= uiomove(buffer
, bytesToRead
, uio
);
207 bytes_remaining
= uio_resid(uio
);
214 * Return an u_int32_t pseudo-random number.
220 read_random(&buf
, sizeof (buf
));