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 /*
22 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2015, Joyent, Inc.
25 */
26
27 /*
28 * This file implements the interfaces that the /dev/random
29 * driver uses for read(2), write(2) and poll(2) on /dev/random or
30 * /dev/urandom. It also implements the kernel API - random_add_entropy(),
31 * random_add_pseudo_entropy(), random_get_pseudo_bytes()
32 * and random_get_bytes().
33 *
34 * We periodically collect random bits from providers which are registered
35 * with the Kernel Cryptographic Framework (kCF) as capable of random
36 * number generation. The random bits are maintained in a cache and
37 * it is used for high quality random numbers (/dev/random) requests.
38 * We pick a provider and call its SPI routine, if the cache does not have
39 * enough bytes to satisfy a request.
40 *
41 * /dev/urandom requests use a software-based generator algorithm that uses the
42 * random bits in the cache as a seed. We create one pseudo-random generator
43 * (for /dev/urandom) per possible CPU on the system, and use it,
44 * kmem-magazine-style, to avoid cache line contention.
905 *
906 * &rnd_pollhead is passed in *phpp in order to indicate the calling thread
907 * will block. When enough random bytes are available, later, the timeout
908 * handler routine will issue the pollwakeup() calls.
909 */
910 void
911 kcf_rnd_chpoll(short events, int anyyet, short *reventsp,
912 struct pollhead **phpp)
913 {
914 *reventsp = events & POLLOUT;
915
916 if (events & (POLLIN | POLLRDNORM)) {
917 /*
918 * Sampling of rnbyte_cnt is an atomic
919 * operation. Hence we do not need any locking.
920 */
921 if (rnbyte_cnt >= MINEXTRACTBYTES)
922 *reventsp |= (events & (POLLIN | POLLRDNORM));
923 }
924
925 if (*reventsp == 0 && !anyyet)
926 *phpp = &rnd_pollhead;
927 }
928
929 /*ARGSUSED*/
930 static void
931 rnd_handler(void *arg)
932 {
933 int len = 0;
934
935 if (!rng_prov_found && rng_ok_to_log) {
936 cmn_err(CE_WARN, "No randomness provider enabled for "
937 "/dev/random. Use cryptoadm(1M) to enable a provider.");
938 rng_ok_to_log = B_FALSE;
939 }
940
941 if (num_waiters > 0)
942 /*
943 * Note: len has no relationship with how many bytes
944 * a poll thread needs.
945 */
|
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 /*
22 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
24 * Copyright 2017 Joyent, Inc.
25 */
26
27 /*
28 * This file implements the interfaces that the /dev/random
29 * driver uses for read(2), write(2) and poll(2) on /dev/random or
30 * /dev/urandom. It also implements the kernel API - random_add_entropy(),
31 * random_add_pseudo_entropy(), random_get_pseudo_bytes()
32 * and random_get_bytes().
33 *
34 * We periodically collect random bits from providers which are registered
35 * with the Kernel Cryptographic Framework (kCF) as capable of random
36 * number generation. The random bits are maintained in a cache and
37 * it is used for high quality random numbers (/dev/random) requests.
38 * We pick a provider and call its SPI routine, if the cache does not have
39 * enough bytes to satisfy a request.
40 *
41 * /dev/urandom requests use a software-based generator algorithm that uses the
42 * random bits in the cache as a seed. We create one pseudo-random generator
43 * (for /dev/urandom) per possible CPU on the system, and use it,
44 * kmem-magazine-style, to avoid cache line contention.
905 *
906 * &rnd_pollhead is passed in *phpp in order to indicate the calling thread
907 * will block. When enough random bytes are available, later, the timeout
908 * handler routine will issue the pollwakeup() calls.
909 */
910 void
911 kcf_rnd_chpoll(short events, int anyyet, short *reventsp,
912 struct pollhead **phpp)
913 {
914 *reventsp = events & POLLOUT;
915
916 if (events & (POLLIN | POLLRDNORM)) {
917 /*
918 * Sampling of rnbyte_cnt is an atomic
919 * operation. Hence we do not need any locking.
920 */
921 if (rnbyte_cnt >= MINEXTRACTBYTES)
922 *reventsp |= (events & (POLLIN | POLLRDNORM));
923 }
924
925 if ((*reventsp == 0 && !anyyet) || (events & POLLET))
926 *phpp = &rnd_pollhead;
927 }
928
929 /*ARGSUSED*/
930 static void
931 rnd_handler(void *arg)
932 {
933 int len = 0;
934
935 if (!rng_prov_found && rng_ok_to_log) {
936 cmn_err(CE_WARN, "No randomness provider enabled for "
937 "/dev/random. Use cryptoadm(1M) to enable a provider.");
938 rng_ok_to_log = B_FALSE;
939 }
940
941 if (num_waiters > 0)
942 /*
943 * Note: len has no relationship with how many bytes
944 * a poll thread needs.
945 */
|