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