xref: /xnu-10002.81.5/bsd/dev/random/randomdev.c (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1*5e3eaea3SApple OSS Distributions /*
2*5e3eaea3SApple OSS Distributions  * Copyright (c) 1999-2009 Apple, Inc. All rights reserved.
3*5e3eaea3SApple OSS Distributions  *
4*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*5e3eaea3SApple OSS Distributions  *
6*5e3eaea3SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*5e3eaea3SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*5e3eaea3SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*5e3eaea3SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*5e3eaea3SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*5e3eaea3SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*5e3eaea3SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*5e3eaea3SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*5e3eaea3SApple OSS Distributions  *
15*5e3eaea3SApple OSS Distributions  * Please obtain a copy of the License at
16*5e3eaea3SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*5e3eaea3SApple OSS Distributions  *
18*5e3eaea3SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*5e3eaea3SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*5e3eaea3SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*5e3eaea3SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*5e3eaea3SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*5e3eaea3SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*5e3eaea3SApple OSS Distributions  * limitations under the License.
25*5e3eaea3SApple OSS Distributions  *
26*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*5e3eaea3SApple OSS Distributions  */
28*5e3eaea3SApple OSS Distributions 
29*5e3eaea3SApple OSS Distributions 
30*5e3eaea3SApple OSS Distributions #include <sys/param.h>
31*5e3eaea3SApple OSS Distributions #include <sys/systm.h>
32*5e3eaea3SApple OSS Distributions #include <sys/proc.h>
33*5e3eaea3SApple OSS Distributions #include <sys/errno.h>
34*5e3eaea3SApple OSS Distributions #include <sys/ioctl.h>
35*5e3eaea3SApple OSS Distributions #include <sys/conf.h>
36*5e3eaea3SApple OSS Distributions #include <sys/fcntl.h>
37*5e3eaea3SApple OSS Distributions #include <string.h>
38*5e3eaea3SApple OSS Distributions #include <miscfs/devfs/devfs.h>
39*5e3eaea3SApple OSS Distributions #include <kern/locks.h>
40*5e3eaea3SApple OSS Distributions #include <kern/clock.h>
41*5e3eaea3SApple OSS Distributions #include <sys/time.h>
42*5e3eaea3SApple OSS Distributions #include <sys/malloc.h>
43*5e3eaea3SApple OSS Distributions #include <sys/sysproto.h>
44*5e3eaea3SApple OSS Distributions #include <sys/uio_internal.h>
45*5e3eaea3SApple OSS Distributions 
46*5e3eaea3SApple OSS Distributions #include <dev/random/randomdev.h>
47*5e3eaea3SApple OSS Distributions 
48*5e3eaea3SApple OSS Distributions #include <libkern/OSByteOrder.h>
49*5e3eaea3SApple OSS Distributions #include <libkern/OSAtomic.h>
50*5e3eaea3SApple OSS Distributions 
51*5e3eaea3SApple OSS Distributions #include <mach/mach_time.h>
52*5e3eaea3SApple OSS Distributions 
53*5e3eaea3SApple OSS Distributions #define RANDOM_MAJOR  -1 /* let the kernel pick the device number */
54*5e3eaea3SApple OSS Distributions #define RANDOM_MINOR   0
55*5e3eaea3SApple OSS Distributions #define URANDOM_MINOR  1
56*5e3eaea3SApple OSS Distributions 
57*5e3eaea3SApple OSS Distributions d_ioctl_t       random_ioctl;
58*5e3eaea3SApple OSS Distributions 
59*5e3eaea3SApple OSS Distributions static int
random_stop(__unused struct tty * tp,__unused int rw)60*5e3eaea3SApple OSS Distributions random_stop(__unused struct tty *tp, __unused int rw)
61*5e3eaea3SApple OSS Distributions {
62*5e3eaea3SApple OSS Distributions 	return 0;
63*5e3eaea3SApple OSS Distributions }
64*5e3eaea3SApple OSS Distributions 
65*5e3eaea3SApple OSS Distributions static int
random_reset(__unused int uban)66*5e3eaea3SApple OSS Distributions random_reset(__unused int uban)
67*5e3eaea3SApple OSS Distributions {
68*5e3eaea3SApple OSS Distributions 	return 0;
69*5e3eaea3SApple OSS Distributions }
70*5e3eaea3SApple OSS Distributions 
71*5e3eaea3SApple OSS Distributions static int
random_select(__unused dev_t dev,__unused int which,__unused void * wql,__unused struct proc * p)72*5e3eaea3SApple OSS Distributions random_select(__unused dev_t dev, __unused int which, __unused void *wql,
73*5e3eaea3SApple OSS Distributions     __unused struct proc *p)
74*5e3eaea3SApple OSS Distributions {
75*5e3eaea3SApple OSS Distributions 	return ENODEV;
76*5e3eaea3SApple OSS Distributions }
77*5e3eaea3SApple OSS Distributions 
78*5e3eaea3SApple OSS Distributions static const struct cdevsw random_cdevsw =
79*5e3eaea3SApple OSS Distributions {
80*5e3eaea3SApple OSS Distributions 	.d_open = random_open,
81*5e3eaea3SApple OSS Distributions 	.d_close = random_close,
82*5e3eaea3SApple OSS Distributions 	.d_read = random_read,
83*5e3eaea3SApple OSS Distributions 	.d_write = random_write,
84*5e3eaea3SApple OSS Distributions 	.d_ioctl = random_ioctl,
85*5e3eaea3SApple OSS Distributions 	.d_stop = random_stop,
86*5e3eaea3SApple OSS Distributions 	.d_reset = random_reset,
87*5e3eaea3SApple OSS Distributions 	.d_select = random_select,
88*5e3eaea3SApple OSS Distributions 	.d_mmap = eno_mmap,
89*5e3eaea3SApple OSS Distributions 	.d_strategy = eno_strat,
90*5e3eaea3SApple OSS Distributions 	.d_reserved_1 = eno_getc,
91*5e3eaea3SApple OSS Distributions 	.d_reserved_2 = eno_putc,
92*5e3eaea3SApple OSS Distributions };
93*5e3eaea3SApple OSS Distributions 
94*5e3eaea3SApple OSS Distributions 
95*5e3eaea3SApple OSS Distributions /*
96*5e3eaea3SApple OSS Distributions  * Called to initialize our device,
97*5e3eaea3SApple OSS Distributions  * and to register ourselves with devfs
98*5e3eaea3SApple OSS Distributions  */
99*5e3eaea3SApple OSS Distributions void
random_init(void)100*5e3eaea3SApple OSS Distributions random_init(void)
101*5e3eaea3SApple OSS Distributions {
102*5e3eaea3SApple OSS Distributions 	int ret;
103*5e3eaea3SApple OSS Distributions 
104*5e3eaea3SApple OSS Distributions 	ret = cdevsw_add(RANDOM_MAJOR, &random_cdevsw);
105*5e3eaea3SApple OSS Distributions 	if (ret < 0) {
106*5e3eaea3SApple OSS Distributions 		panic("random_init: failed to allocate a major number!");
107*5e3eaea3SApple OSS Distributions 	}
108*5e3eaea3SApple OSS Distributions 
109*5e3eaea3SApple OSS Distributions 	devfs_make_node(makedev(ret, RANDOM_MINOR), DEVFS_CHAR,
110*5e3eaea3SApple OSS Distributions 	    UID_ROOT, GID_WHEEL, 0666, "random");
111*5e3eaea3SApple OSS Distributions 
112*5e3eaea3SApple OSS Distributions 	/*
113*5e3eaea3SApple OSS Distributions 	 * also make urandom
114*5e3eaea3SApple OSS Distributions 	 * (which is exactly the same thing in our context)
115*5e3eaea3SApple OSS Distributions 	 */
116*5e3eaea3SApple OSS Distributions 	devfs_make_node(makedev(ret, URANDOM_MINOR), DEVFS_CHAR,
117*5e3eaea3SApple OSS Distributions 	    UID_ROOT, GID_WHEEL, 0666, "urandom");
118*5e3eaea3SApple OSS Distributions }
119*5e3eaea3SApple OSS Distributions 
120*5e3eaea3SApple OSS Distributions int
random_ioctl(__unused dev_t dev,u_long cmd,__unused caddr_t data,__unused int flag,__unused struct proc * p)121*5e3eaea3SApple OSS Distributions random_ioctl(   __unused dev_t dev, u_long cmd, __unused caddr_t data,
122*5e3eaea3SApple OSS Distributions     __unused int flag, __unused struct proc *p  )
123*5e3eaea3SApple OSS Distributions {
124*5e3eaea3SApple OSS Distributions 	switch (cmd) {
125*5e3eaea3SApple OSS Distributions 	case FIONBIO:
126*5e3eaea3SApple OSS Distributions 	case FIOASYNC:
127*5e3eaea3SApple OSS Distributions 		break;
128*5e3eaea3SApple OSS Distributions 	default:
129*5e3eaea3SApple OSS Distributions 		return ENODEV;
130*5e3eaea3SApple OSS Distributions 	}
131*5e3eaea3SApple OSS Distributions 
132*5e3eaea3SApple OSS Distributions 	return 0;
133*5e3eaea3SApple OSS Distributions }
134*5e3eaea3SApple OSS Distributions 
135*5e3eaea3SApple OSS Distributions /*
136*5e3eaea3SApple OSS Distributions  * Open the device.  Make sure init happened, and make sure the caller is
137*5e3eaea3SApple OSS Distributions  * authorized.
138*5e3eaea3SApple OSS Distributions  */
139*5e3eaea3SApple OSS Distributions 
140*5e3eaea3SApple OSS Distributions int
random_open(__unused dev_t dev,int flags,__unused int devtype,__unused struct proc * p)141*5e3eaea3SApple OSS Distributions random_open(__unused dev_t dev, int flags, __unused int devtype, __unused struct proc *p)
142*5e3eaea3SApple OSS Distributions {
143*5e3eaea3SApple OSS Distributions 	/*
144*5e3eaea3SApple OSS Distributions 	 * if we are being opened for write,
145*5e3eaea3SApple OSS Distributions 	 * make sure that we have privledges do so
146*5e3eaea3SApple OSS Distributions 	 */
147*5e3eaea3SApple OSS Distributions 	if (flags & FWRITE) {
148*5e3eaea3SApple OSS Distributions 		if (securelevel >= 2) {
149*5e3eaea3SApple OSS Distributions 			return EPERM;
150*5e3eaea3SApple OSS Distributions 		}
151*5e3eaea3SApple OSS Distributions #ifndef __APPLE__
152*5e3eaea3SApple OSS Distributions 		if ((securelevel >= 1) && proc_suser(p)) {
153*5e3eaea3SApple OSS Distributions 			return EPERM;
154*5e3eaea3SApple OSS Distributions 		}
155*5e3eaea3SApple OSS Distributions #endif  /* !__APPLE__ */
156*5e3eaea3SApple OSS Distributions 	}
157*5e3eaea3SApple OSS Distributions 
158*5e3eaea3SApple OSS Distributions 	return 0;
159*5e3eaea3SApple OSS Distributions }
160*5e3eaea3SApple OSS Distributions 
161*5e3eaea3SApple OSS Distributions 
162*5e3eaea3SApple OSS Distributions /*
163*5e3eaea3SApple OSS Distributions  * close the device.
164*5e3eaea3SApple OSS Distributions  */
165*5e3eaea3SApple OSS Distributions 
166*5e3eaea3SApple OSS Distributions int
random_close(__unused dev_t dev,__unused int flags,__unused int mode,__unused struct proc * p)167*5e3eaea3SApple OSS Distributions random_close(__unused dev_t dev, __unused int flags, __unused int mode, __unused struct proc *p)
168*5e3eaea3SApple OSS Distributions {
169*5e3eaea3SApple OSS Distributions 	return 0;
170*5e3eaea3SApple OSS Distributions }
171*5e3eaea3SApple OSS Distributions 
172*5e3eaea3SApple OSS Distributions 
173*5e3eaea3SApple OSS Distributions /*
174*5e3eaea3SApple OSS Distributions  * Get entropic data from the Security Server, and use it to reseed the
175*5e3eaea3SApple OSS Distributions  * prng.
176*5e3eaea3SApple OSS Distributions  */
177*5e3eaea3SApple OSS Distributions int
random_write(dev_t dev,struct uio * uio,__unused int ioflag)178*5e3eaea3SApple OSS Distributions random_write(dev_t dev, struct uio *uio, __unused int ioflag)
179*5e3eaea3SApple OSS Distributions {
180*5e3eaea3SApple OSS Distributions 	int retCode = 0;
181*5e3eaea3SApple OSS Distributions 	char rdBuffer[256];
182*5e3eaea3SApple OSS Distributions 
183*5e3eaea3SApple OSS Distributions 	if (minor(dev) != RANDOM_MINOR) {
184*5e3eaea3SApple OSS Distributions 		return EPERM;
185*5e3eaea3SApple OSS Distributions 	}
186*5e3eaea3SApple OSS Distributions 
187*5e3eaea3SApple OSS Distributions 	/* Security server is sending us entropy */
188*5e3eaea3SApple OSS Distributions 
189*5e3eaea3SApple OSS Distributions 	while ((size_t)uio_resid(uio) > 0 && retCode == 0) {
190*5e3eaea3SApple OSS Distributions 		/* get the user's data */
191*5e3eaea3SApple OSS Distributions 		size_t bytesToInput = MIN((size_t)uio_resid(uio), sizeof(rdBuffer));
192*5e3eaea3SApple OSS Distributions 		retCode = uiomove(rdBuffer, (int)bytesToInput, uio);
193*5e3eaea3SApple OSS Distributions 		if (retCode != 0) {
194*5e3eaea3SApple OSS Distributions 			break;
195*5e3eaea3SApple OSS Distributions 		}
196*5e3eaea3SApple OSS Distributions 		retCode = write_random(rdBuffer, (u_int)bytesToInput);
197*5e3eaea3SApple OSS Distributions 		if (retCode != 0) {
198*5e3eaea3SApple OSS Distributions 			break;
199*5e3eaea3SApple OSS Distributions 		}
200*5e3eaea3SApple OSS Distributions 	}
201*5e3eaea3SApple OSS Distributions 
202*5e3eaea3SApple OSS Distributions 	return retCode;
203*5e3eaea3SApple OSS Distributions }
204*5e3eaea3SApple OSS Distributions 
205*5e3eaea3SApple OSS Distributions /*
206*5e3eaea3SApple OSS Distributions  * return data to the caller.  Results unpredictable.
207*5e3eaea3SApple OSS Distributions  */
208*5e3eaea3SApple OSS Distributions int
random_read(__unused dev_t dev,struct uio * uio,__unused int ioflag)209*5e3eaea3SApple OSS Distributions random_read(__unused dev_t dev, struct uio *uio, __unused int ioflag)
210*5e3eaea3SApple OSS Distributions {
211*5e3eaea3SApple OSS Distributions 	int retCode = 0;
212*5e3eaea3SApple OSS Distributions 	char buffer[512];
213*5e3eaea3SApple OSS Distributions 
214*5e3eaea3SApple OSS Distributions 	size_t bytes_remaining = (size_t)uio_resid(uio);
215*5e3eaea3SApple OSS Distributions 	while (bytes_remaining > 0 && retCode == 0) {
216*5e3eaea3SApple OSS Distributions 		size_t bytesToRead = MIN(bytes_remaining, sizeof(buffer));
217*5e3eaea3SApple OSS Distributions 		read_random(buffer, (u_int)bytesToRead);
218*5e3eaea3SApple OSS Distributions 
219*5e3eaea3SApple OSS Distributions 		retCode = uiomove(buffer, (int)bytesToRead, uio);
220*5e3eaea3SApple OSS Distributions 		if (retCode != 0) {
221*5e3eaea3SApple OSS Distributions 			break;
222*5e3eaea3SApple OSS Distributions 		}
223*5e3eaea3SApple OSS Distributions 
224*5e3eaea3SApple OSS Distributions 		bytes_remaining = (size_t)uio_resid(uio);
225*5e3eaea3SApple OSS Distributions 	}
226*5e3eaea3SApple OSS Distributions 
227*5e3eaea3SApple OSS Distributions 	return retCode;
228*5e3eaea3SApple OSS Distributions }
229*5e3eaea3SApple OSS Distributions 
230*5e3eaea3SApple OSS Distributions /*
231*5e3eaea3SApple OSS Distributions  * Return an u_int32_t pseudo-random number.
232*5e3eaea3SApple OSS Distributions  */
233*5e3eaea3SApple OSS Distributions u_int32_t
RandomULong(void)234*5e3eaea3SApple OSS Distributions RandomULong(void)
235*5e3eaea3SApple OSS Distributions {
236*5e3eaea3SApple OSS Distributions 	u_int32_t buf;
237*5e3eaea3SApple OSS Distributions 	read_random(&buf, sizeof(buf));
238*5e3eaea3SApple OSS Distributions 	return buf;
239*5e3eaea3SApple OSS Distributions }
240*5e3eaea3SApple OSS Distributions 
241*5e3eaea3SApple OSS Distributions 
242*5e3eaea3SApple OSS Distributions int
getentropy(__unused struct proc * p,struct getentropy_args * gap,__unused int * ret)243*5e3eaea3SApple OSS Distributions getentropy(__unused struct proc * p, struct getentropy_args *gap, __unused int * ret)
244*5e3eaea3SApple OSS Distributions {
245*5e3eaea3SApple OSS Distributions 	user_addr_t user_addr;
246*5e3eaea3SApple OSS Distributions 	user_size_t user_size;
247*5e3eaea3SApple OSS Distributions 	char buffer[256];
248*5e3eaea3SApple OSS Distributions 
249*5e3eaea3SApple OSS Distributions 	user_addr = (user_addr_t)gap->buffer;
250*5e3eaea3SApple OSS Distributions 	user_size = gap->size;
251*5e3eaea3SApple OSS Distributions 	/* Can't request more than 256 random bytes
252*5e3eaea3SApple OSS Distributions 	 * at once. Complying with openbsd getentropy()
253*5e3eaea3SApple OSS Distributions 	 */
254*5e3eaea3SApple OSS Distributions 	if (user_size > sizeof(buffer)) {
255*5e3eaea3SApple OSS Distributions 		return EINVAL;
256*5e3eaea3SApple OSS Distributions 	}
257*5e3eaea3SApple OSS Distributions 	read_random(buffer, (u_int)user_size);
258*5e3eaea3SApple OSS Distributions 	return copyout(buffer, user_addr, user_size);
259*5e3eaea3SApple OSS Distributions }
260