3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 *
21 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
22 * Use is subject to license terms.
23 */
24
25
26 /*
27 * Random number generator pseudo-driver
28 *
29 * This is a lightweight driver which calls in to the Kernel Cryptographic
30 * Framework to do the real work. Kernel modules should NOT depend on this
31 * driver for /dev/random kernel API.
32 *
33 * Applications may ask for 2 types of random bits:
34 * . High quality random by reading from /dev/random. The output is extracted
35 * only when a minimum amount of entropy is available.
36 * . Pseudo-random, by reading from /dev/urandom, that can be generated any
37 * time.
38 */
39
40 #include <sys/types.h>
41 #include <sys/errno.h>
42 #include <sys/stat.h>
272
273 /*ARGSUSED*/
274 static int
275 rnd_write(dev_t dev, struct uio *uiop, cred_t *credp)
276 {
277 int error;
278 uint8_t buf[WRITEBUFSIZE];
279 size_t bytes;
280 minor_t devno;
281
282 devno = getminor(dev);
283
284 while (uiop->uio_resid > 0) {
285 bytes = min(sizeof (buf), uiop->uio_resid);
286
287 /* See comments in rnd_read() */
288 uiop->uio_loffset = 0;
289 if ((error = uiomove(buf, bytes, UIO_WRITE, uiop)) != 0)
290 return (error);
291
292 switch (devno) {
293 case DEVRANDOM:
294 if ((error = random_add_entropy(buf, bytes, 0)) != 0)
295 return (error);
296 break;
297 case DEVURANDOM:
298 if ((error = random_add_pseudo_entropy(buf, bytes,
299 0)) != 0)
300 return (error);
301 break;
302 default:
303 return (ENXIO);
304 }
305 }
306
307 return (0);
308 }
309
310 static struct pollhead urnd_pollhd;
311
|
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 *
21 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
22 * Use is subject to license terms.
23 *
24 * Copyright 2016 Joyent, Inc.
25 */
26
27
28 /*
29 * Random number generator pseudo-driver
30 *
31 * This is a lightweight driver which calls in to the Kernel Cryptographic
32 * Framework to do the real work. Kernel modules should NOT depend on this
33 * driver for /dev/random kernel API.
34 *
35 * Applications may ask for 2 types of random bits:
36 * . High quality random by reading from /dev/random. The output is extracted
37 * only when a minimum amount of entropy is available.
38 * . Pseudo-random, by reading from /dev/urandom, that can be generated any
39 * time.
40 */
41
42 #include <sys/types.h>
43 #include <sys/errno.h>
44 #include <sys/stat.h>
274
275 /*ARGSUSED*/
276 static int
277 rnd_write(dev_t dev, struct uio *uiop, cred_t *credp)
278 {
279 int error;
280 uint8_t buf[WRITEBUFSIZE];
281 size_t bytes;
282 minor_t devno;
283
284 devno = getminor(dev);
285
286 while (uiop->uio_resid > 0) {
287 bytes = min(sizeof (buf), uiop->uio_resid);
288
289 /* See comments in rnd_read() */
290 uiop->uio_loffset = 0;
291 if ((error = uiomove(buf, bytes, UIO_WRITE, uiop)) != 0)
292 return (error);
293
294 if (crgetzone(credp) != global_zone)
295 continue;
296
297 switch (devno) {
298 case DEVRANDOM:
299 if ((error = random_add_entropy(buf, bytes, 0)) != 0)
300 return (error);
301 break;
302 case DEVURANDOM:
303 if ((error = random_add_pseudo_entropy(buf, bytes,
304 0)) != 0)
305 return (error);
306 break;
307 default:
308 return (ENXIO);
309 }
310 }
311
312 return (0);
313 }
314
315 static struct pollhead urnd_pollhd;
316
|