]>
Commit | Line | Data |
---|---|---|
0b4e3aa0 | 1 | /* |
b0d623f7 | 2 | * Copyright (c) 1999-2009 Apple, Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
39037602 | 5 | * |
2d21ac55 A |
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. | |
39037602 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
39037602 | 17 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
39037602 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
0b4e3aa0 A |
27 | */ |
28 | ||
b0d623f7 | 29 | |
0b4e3aa0 A |
30 | #include <sys/param.h> |
31 | #include <sys/systm.h> | |
32 | #include <sys/proc.h> | |
33 | #include <sys/errno.h> | |
34 | #include <sys/ioctl.h> | |
35 | #include <sys/conf.h> | |
36 | #include <sys/fcntl.h> | |
8ad349bb | 37 | #include <string.h> |
0b4e3aa0 | 38 | #include <miscfs/devfs/devfs.h> |
fe8ab488 | 39 | #include <kern/locks.h> |
2d21ac55 | 40 | #include <kern/clock.h> |
0b4e3aa0 A |
41 | #include <sys/time.h> |
42 | #include <sys/malloc.h> | |
39037602 | 43 | #include <sys/sysproto.h> |
91447636 | 44 | #include <sys/uio_internal.h> |
0b4e3aa0 A |
45 | |
46 | #include <dev/random/randomdev.h> | |
b0d623f7 A |
47 | |
48 | #include <libkern/OSByteOrder.h> | |
bd504ef0 | 49 | #include <libkern/OSAtomic.h> |
2d21ac55 A |
50 | |
51 | #include <mach/mach_time.h> | |
b0d623f7 | 52 | |
0b4e3aa0 | 53 | #define RANDOM_MAJOR -1 /* let the kernel pick the device number */ |
fe8ab488 A |
54 | #define RANDOM_MINOR 0 |
55 | #define URANDOM_MINOR 1 | |
0b4e3aa0 | 56 | |
55e303ae A |
57 | d_ioctl_t random_ioctl; |
58 | ||
f427ee49 | 59 | static const struct cdevsw random_cdevsw = |
0b4e3aa0 | 60 | { |
f427ee49 A |
61 | .d_open = random_open, |
62 | .d_close = random_close, | |
63 | .d_read = random_read, | |
64 | .d_write = random_write, | |
65 | .d_ioctl = random_ioctl, | |
66 | .d_stop = (stop_fcn_t *)nulldev, | |
67 | .d_reset = (reset_fcn_t *)nulldev, | |
68 | .d_select = eno_select, | |
69 | .d_mmap = eno_mmap, | |
70 | .d_strategy = eno_strat, | |
71 | .d_reserved_1 = eno_getc, | |
72 | .d_reserved_2 = eno_putc, | |
0b4e3aa0 A |
73 | }; |
74 | ||
d1ecb069 | 75 | |
0b4e3aa0 A |
76 | /* |
77 | * Called to initialize our device, | |
78 | * and to register ourselves with devfs | |
79 | */ | |
80 | void | |
8ad349bb | 81 | random_init(void) |
0b4e3aa0 A |
82 | { |
83 | int ret; | |
84 | ||
0b4e3aa0 A |
85 | ret = cdevsw_add(RANDOM_MAJOR, &random_cdevsw); |
86 | if (ret < 0) { | |
fe8ab488 | 87 | panic("random_init: failed to allocate a major number!"); |
0b4e3aa0 A |
88 | } |
89 | ||
0a7de745 A |
90 | devfs_make_node(makedev(ret, RANDOM_MINOR), DEVFS_CHAR, |
91 | UID_ROOT, GID_WHEEL, 0666, "random", 0); | |
0b4e3aa0 A |
92 | |
93 | /* | |
39037602 | 94 | * also make urandom |
0b4e3aa0 A |
95 | * (which is exactly the same thing in our context) |
96 | */ | |
0a7de745 A |
97 | devfs_make_node(makedev(ret, URANDOM_MINOR), DEVFS_CHAR, |
98 | UID_ROOT, GID_WHEEL, 0666, "urandom", 0); | |
55e303ae A |
99 | } |
100 | ||
101 | int | |
0a7de745 A |
102 | random_ioctl( __unused dev_t dev, u_long cmd, __unused caddr_t data, |
103 | __unused int flag, __unused struct proc *p ) | |
55e303ae A |
104 | { |
105 | switch (cmd) { | |
106 | case FIONBIO: | |
107 | case FIOASYNC: | |
108 | break; | |
109 | default: | |
110 | return ENODEV; | |
111 | } | |
112 | ||
0a7de745 | 113 | return 0; |
0b4e3aa0 A |
114 | } |
115 | ||
116 | /* | |
117 | * Open the device. Make sure init happened, and make sure the caller is | |
118 | * authorized. | |
119 | */ | |
39037602 | 120 | |
0b4e3aa0 | 121 | int |
91447636 | 122 | random_open(__unused dev_t dev, int flags, __unused int devtype, __unused struct proc *p) |
0b4e3aa0 | 123 | { |
0b4e3aa0 A |
124 | /* |
125 | * if we are being opened for write, | |
126 | * make sure that we have privledges do so | |
127 | */ | |
128 | if (flags & FWRITE) { | |
0a7de745 A |
129 | if (securelevel >= 2) { |
130 | return EPERM; | |
131 | } | |
55e303ae | 132 | #ifndef __APPLE__ |
0a7de745 A |
133 | if ((securelevel >= 1) && proc_suser(p)) { |
134 | return EPERM; | |
135 | } | |
136 | #endif /* !__APPLE__ */ | |
0b4e3aa0 A |
137 | } |
138 | ||
0a7de745 | 139 | return 0; |
0b4e3aa0 A |
140 | } |
141 | ||
142 | ||
143 | /* | |
144 | * close the device. | |
145 | */ | |
39037602 | 146 | |
0b4e3aa0 | 147 | int |
91447636 | 148 | random_close(__unused dev_t dev, __unused int flags, __unused int mode, __unused struct proc *p) |
0b4e3aa0 | 149 | { |
0a7de745 | 150 | return 0; |
0b4e3aa0 A |
151 | } |
152 | ||
153 | ||
154 | /* | |
155 | * Get entropic data from the Security Server, and use it to reseed the | |
156 | * prng. | |
157 | */ | |
158 | int | |
0a7de745 | 159 | random_write(dev_t dev, struct uio *uio, __unused int ioflag) |
0b4e3aa0 | 160 | { |
0a7de745 A |
161 | int retCode = 0; |
162 | char rdBuffer[256]; | |
163 | ||
164 | if (minor(dev) != RANDOM_MINOR) { | |
165 | return EPERM; | |
166 | } | |
167 | ||
168 | /* Security server is sending us entropy */ | |
169 | ||
f427ee49 | 170 | while ((size_t)uio_resid(uio) > 0 && retCode == 0) { |
0a7de745 | 171 | /* get the user's data */ |
f427ee49 A |
172 | size_t bytesToInput = MIN((size_t)uio_resid(uio), sizeof(rdBuffer)); |
173 | retCode = uiomove(rdBuffer, (int)bytesToInput, uio); | |
0a7de745 A |
174 | if (retCode != 0) { |
175 | break; | |
176 | } | |
f427ee49 | 177 | retCode = write_random(rdBuffer, (u_int)bytesToInput); |
0a7de745 A |
178 | if (retCode != 0) { |
179 | break; | |
180 | } | |
181 | } | |
182 | ||
183 | return retCode; | |
0b4e3aa0 A |
184 | } |
185 | ||
39236c6e A |
186 | /* |
187 | * return data to the caller. Results unpredictable. | |
39037602 | 188 | */ |
39236c6e A |
189 | int |
190 | random_read(__unused dev_t dev, struct uio *uio, __unused int ioflag) | |
191 | { | |
fe8ab488 A |
192 | int retCode = 0; |
193 | char buffer[512]; | |
39236c6e | 194 | |
f427ee49 | 195 | size_t bytes_remaining = (size_t)uio_resid(uio); |
fe8ab488 | 196 | while (bytes_remaining > 0 && retCode == 0) { |
f427ee49 A |
197 | size_t bytesToRead = MIN(bytes_remaining, sizeof(buffer)); |
198 | read_random(buffer, (u_int)bytesToRead); | |
39037602 | 199 | |
f427ee49 | 200 | retCode = uiomove(buffer, (int)bytesToRead, uio); |
0a7de745 | 201 | if (retCode != 0) { |
fe8ab488 | 202 | break; |
0a7de745 | 203 | } |
39037602 | 204 | |
f427ee49 | 205 | bytes_remaining = (size_t)uio_resid(uio); |
fe8ab488 | 206 | } |
39037602 | 207 | |
fe8ab488 | 208 | return retCode; |
39236c6e | 209 | } |
fe8ab488 | 210 | |
0b4e3aa0 | 211 | /* |
b0d623f7 | 212 | * Return an u_int32_t pseudo-random number. |
0b4e3aa0 | 213 | */ |
b0d623f7 | 214 | u_int32_t |
8ad349bb | 215 | RandomULong(void) |
0b4e3aa0 | 216 | { |
b0d623f7 | 217 | u_int32_t buf; |
0a7de745 A |
218 | read_random(&buf, sizeof(buf)); |
219 | return buf; | |
0b4e3aa0 | 220 | } |
d1ecb069 | 221 | |
39037602 A |
222 | |
223 | int | |
0a7de745 A |
224 | getentropy(__unused struct proc * p, struct getentropy_args *gap, __unused int * ret) |
225 | { | |
39037602 | 226 | user_addr_t user_addr; |
f427ee49 | 227 | user_size_t user_size; |
39037602 A |
228 | char buffer[256]; |
229 | ||
f427ee49 | 230 | user_addr = (user_addr_t)gap->buffer; |
39037602 A |
231 | user_size = gap->size; |
232 | /* Can't request more than 256 random bytes | |
233 | * at once. Complying with openbsd getentropy() | |
234 | */ | |
235 | if (user_size > sizeof(buffer)) { | |
236 | return EINVAL; | |
237 | } | |
f427ee49 | 238 | read_random(buffer, (u_int)user_size); |
39037602 A |
239 | return copyout(buffer, user_addr, user_size); |
240 | } |