10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51920Smcpowers * Common Development and Distribution License (the "License").
61920Smcpowers * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate *
218928SBhargava.Yenduri@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
220Sstevel@tonic-gate * Use is subject to license terms.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate
250Sstevel@tonic-gate
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate * Random number generator pseudo-driver
280Sstevel@tonic-gate *
290Sstevel@tonic-gate * This is a lightweight driver which calls in to the Kernel Cryptographic
300Sstevel@tonic-gate * Framework to do the real work. Kernel modules should NOT depend on this
310Sstevel@tonic-gate * driver for /dev/random kernel API.
320Sstevel@tonic-gate *
330Sstevel@tonic-gate * Applications may ask for 2 types of random bits:
340Sstevel@tonic-gate * . High quality random by reading from /dev/random. The output is extracted
350Sstevel@tonic-gate * only when a minimum amount of entropy is available.
360Sstevel@tonic-gate * . Pseudo-random, by reading from /dev/urandom, that can be generated any
370Sstevel@tonic-gate * time.
380Sstevel@tonic-gate */
390Sstevel@tonic-gate
400Sstevel@tonic-gate #include <sys/types.h>
410Sstevel@tonic-gate #include <sys/errno.h>
420Sstevel@tonic-gate #include <sys/stat.h>
430Sstevel@tonic-gate
440Sstevel@tonic-gate #include <sys/file.h>
450Sstevel@tonic-gate #include <sys/open.h>
460Sstevel@tonic-gate #include <sys/poll.h>
470Sstevel@tonic-gate #include <sys/uio.h>
480Sstevel@tonic-gate #include <sys/cred.h>
490Sstevel@tonic-gate #include <sys/modctl.h>
500Sstevel@tonic-gate #include <sys/conf.h>
510Sstevel@tonic-gate #include <sys/ddi.h>
520Sstevel@tonic-gate #include <sys/sunddi.h>
530Sstevel@tonic-gate #include <sys/random.h>
540Sstevel@tonic-gate #include <sys/crypto/impl.h>
550Sstevel@tonic-gate
560Sstevel@tonic-gate #define DEVRANDOM 0
570Sstevel@tonic-gate #define DEVURANDOM 1
580Sstevel@tonic-gate
590Sstevel@tonic-gate #define HASHSIZE 20 /* Assuming a SHA1 hash algorithm */
600Sstevel@tonic-gate #define WRITEBUFSIZE 512 /* Size of buffer for write request */
610Sstevel@tonic-gate #define MAXRETBYTES 1040 /* Max bytes returned per read. */
620Sstevel@tonic-gate /* Must be a multiple of HASHSIZE */
630Sstevel@tonic-gate static dev_info_t *rnd_dip;
640Sstevel@tonic-gate
650Sstevel@tonic-gate static int rnd_open(dev_t *, int, int, cred_t *);
660Sstevel@tonic-gate static int rnd_close(dev_t, int, int, cred_t *);
670Sstevel@tonic-gate static int rnd_read(dev_t, struct uio *, cred_t *);
680Sstevel@tonic-gate static int rnd_write(dev_t, struct uio *, cred_t *);
690Sstevel@tonic-gate static int rnd_chpoll(dev_t, short, int, short *, struct pollhead **);
700Sstevel@tonic-gate static int rnd_attach(dev_info_t *, ddi_attach_cmd_t);
710Sstevel@tonic-gate static int rnd_detach(dev_info_t *, ddi_detach_cmd_t);
720Sstevel@tonic-gate static int rnd_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
730Sstevel@tonic-gate
740Sstevel@tonic-gate /* DDI declarations */
750Sstevel@tonic-gate static struct cb_ops rnd_cb_ops = {
760Sstevel@tonic-gate rnd_open, /* open */
770Sstevel@tonic-gate rnd_close, /* close */
780Sstevel@tonic-gate nodev, /* strategy */
790Sstevel@tonic-gate nodev, /* print */
800Sstevel@tonic-gate nodev, /* dump */
810Sstevel@tonic-gate rnd_read, /* read */
820Sstevel@tonic-gate rnd_write, /* write */
830Sstevel@tonic-gate nodev, /* ioctl */
840Sstevel@tonic-gate nodev, /* devmap */
850Sstevel@tonic-gate nodev, /* mmap */
860Sstevel@tonic-gate nodev, /* segmap */
870Sstevel@tonic-gate rnd_chpoll, /* chpoll */
880Sstevel@tonic-gate ddi_prop_op, /* prop_op */
890Sstevel@tonic-gate NULL, /* streamtab */
900Sstevel@tonic-gate (D_NEW | D_MP), /* cb_flag */
910Sstevel@tonic-gate CB_REV, /* cb_rev */
920Sstevel@tonic-gate nodev, /* aread */
930Sstevel@tonic-gate nodev /* awrite */
940Sstevel@tonic-gate };
950Sstevel@tonic-gate
960Sstevel@tonic-gate static struct dev_ops rnd_ops = {
970Sstevel@tonic-gate DEVO_REV, /* devo_rev, */
980Sstevel@tonic-gate 0, /* refcnt */
990Sstevel@tonic-gate rnd_getinfo, /* get_dev_info */
1000Sstevel@tonic-gate nulldev, /* identify */
1010Sstevel@tonic-gate nulldev, /* probe */
1020Sstevel@tonic-gate rnd_attach, /* attach */
1030Sstevel@tonic-gate rnd_detach, /* detach */
1040Sstevel@tonic-gate nodev, /* reset */
1050Sstevel@tonic-gate &rnd_cb_ops, /* driver operations */
1060Sstevel@tonic-gate NULL, /* bus operations */
1077656SSherry.Moore@Sun.COM NULL, /* power */
1087656SSherry.Moore@Sun.COM ddi_quiesce_not_needed, /* quiesce */
1090Sstevel@tonic-gate };
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate /* Modlinkage */
1120Sstevel@tonic-gate static struct modldrv modldrv = {
1130Sstevel@tonic-gate &mod_driverops,
1147656SSherry.Moore@Sun.COM "random number device",
1150Sstevel@tonic-gate &rnd_ops
1160Sstevel@tonic-gate };
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate static struct modlinkage modlinkage = { MODREV_1, { &modldrv, NULL } };
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate /* DDI glue */
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate int
_init(void)1240Sstevel@tonic-gate _init(void)
1250Sstevel@tonic-gate {
1260Sstevel@tonic-gate return (mod_install(&modlinkage));
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate int
_fini(void)1300Sstevel@tonic-gate _fini(void)
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate return (mod_remove(&modlinkage));
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1360Sstevel@tonic-gate _info(struct modinfo *modinfop)
1370Sstevel@tonic-gate {
1380Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate static int
rnd_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)1420Sstevel@tonic-gate rnd_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
1430Sstevel@tonic-gate {
1440Sstevel@tonic-gate if (cmd != DDI_ATTACH)
1450Sstevel@tonic-gate return (DDI_FAILURE);
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate if (ddi_create_minor_node(dip, "random", S_IFCHR, DEVRANDOM,
1480Sstevel@tonic-gate DDI_PSEUDO, 0) == DDI_FAILURE) {
1490Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL);
1500Sstevel@tonic-gate return (DDI_FAILURE);
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate if (ddi_create_minor_node(dip, "urandom", S_IFCHR, DEVURANDOM,
1530Sstevel@tonic-gate DDI_PSEUDO, 0) == DDI_FAILURE) {
1540Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL);
1550Sstevel@tonic-gate return (DDI_FAILURE);
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate rnd_dip = dip;
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate return (DDI_SUCCESS);
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate static int
rnd_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)1640Sstevel@tonic-gate rnd_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
1650Sstevel@tonic-gate {
1660Sstevel@tonic-gate if (cmd != DDI_DETACH)
1670Sstevel@tonic-gate return (DDI_FAILURE);
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate rnd_dip = NULL;
1700Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL);
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate return (DDI_SUCCESS);
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate /*ARGSUSED*/
1760Sstevel@tonic-gate static int
rnd_getinfo(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)1770Sstevel@tonic-gate rnd_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
1780Sstevel@tonic-gate {
1790Sstevel@tonic-gate int error;
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate switch (infocmd) {
1820Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO:
1830Sstevel@tonic-gate *result = rnd_dip;
1840Sstevel@tonic-gate error = DDI_SUCCESS;
1850Sstevel@tonic-gate break;
1860Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE:
1870Sstevel@tonic-gate *result = (void *)0;
1880Sstevel@tonic-gate error = DDI_SUCCESS;
1890Sstevel@tonic-gate break;
1900Sstevel@tonic-gate default:
1910Sstevel@tonic-gate error = DDI_FAILURE;
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate return (error);
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate
1960Sstevel@tonic-gate /*ARGSUSED3*/
1970Sstevel@tonic-gate static int
rnd_open(dev_t * devp,int flag,int otyp,cred_t * credp)1980Sstevel@tonic-gate rnd_open(dev_t *devp, int flag, int otyp, cred_t *credp)
1990Sstevel@tonic-gate {
2000Sstevel@tonic-gate switch (getminor(*devp)) {
2010Sstevel@tonic-gate case DEVRANDOM:
2020Sstevel@tonic-gate if (!kcf_rngprov_check())
2030Sstevel@tonic-gate return (ENXIO);
2040Sstevel@tonic-gate break;
2050Sstevel@tonic-gate case DEVURANDOM:
2060Sstevel@tonic-gate break;
2070Sstevel@tonic-gate default:
2080Sstevel@tonic-gate return (ENXIO);
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate if (otyp != OTYP_CHR)
2110Sstevel@tonic-gate return (EINVAL);
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate if (flag & FEXCL)
2140Sstevel@tonic-gate return (EINVAL);
2150Sstevel@tonic-gate return (0);
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate /*ARGSUSED*/
2190Sstevel@tonic-gate static int
rnd_close(dev_t dev,int flag,int otyp,cred_t * credp)2200Sstevel@tonic-gate rnd_close(dev_t dev, int flag, int otyp, cred_t *credp)
2210Sstevel@tonic-gate {
2220Sstevel@tonic-gate return (0);
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate /*ARGSUSED2*/
2260Sstevel@tonic-gate static int
rnd_read(dev_t dev,struct uio * uiop,cred_t * credp)2270Sstevel@tonic-gate rnd_read(dev_t dev, struct uio *uiop, cred_t *credp)
2280Sstevel@tonic-gate {
2290Sstevel@tonic-gate size_t len;
2300Sstevel@tonic-gate minor_t devno;
2310Sstevel@tonic-gate int error = 0;
2320Sstevel@tonic-gate int nbytes = 0;
2330Sstevel@tonic-gate uint8_t random_bytes[2 * HASHSIZE];
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate devno = getminor(dev);
2360Sstevel@tonic-gate
2370Sstevel@tonic-gate while (error == 0 && uiop->uio_resid > 0) {
2380Sstevel@tonic-gate len = min(sizeof (random_bytes), uiop->uio_resid);
2390Sstevel@tonic-gate switch (devno) {
2400Sstevel@tonic-gate case DEVRANDOM:
2410Sstevel@tonic-gate error = kcf_rnd_get_bytes(random_bytes, len,
242*9619SBhargava.Yenduri@Sun.COM uiop->uio_fmode & (FNDELAY|FNONBLOCK));
2430Sstevel@tonic-gate break;
2440Sstevel@tonic-gate case DEVURANDOM:
2450Sstevel@tonic-gate error = kcf_rnd_get_pseudo_bytes(random_bytes, len);
2460Sstevel@tonic-gate break;
2470Sstevel@tonic-gate default:
2480Sstevel@tonic-gate return (ENXIO);
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate
2510Sstevel@tonic-gate if (error == 0) {
2520Sstevel@tonic-gate /*
2530Sstevel@tonic-gate * /dev/[u]random is not a seekable device. To prevent
2540Sstevel@tonic-gate * uio offset from growing and eventually exceeding
2550Sstevel@tonic-gate * the maximum, reset the offset here for every call.
2560Sstevel@tonic-gate */
2570Sstevel@tonic-gate uiop->uio_loffset = 0;
2580Sstevel@tonic-gate error = uiomove(random_bytes, len, UIO_READ, uiop);
2590Sstevel@tonic-gate
2600Sstevel@tonic-gate nbytes += len;
2610Sstevel@tonic-gate
2623096Skrishna if (devno == DEVRANDOM && nbytes >= MAXRETBYTES)
2630Sstevel@tonic-gate break;
2640Sstevel@tonic-gate
2650Sstevel@tonic-gate } else if ((error == EAGAIN) && (nbytes > 0)) {
2660Sstevel@tonic-gate error = 0;
2670Sstevel@tonic-gate break;
2680Sstevel@tonic-gate }
2690Sstevel@tonic-gate }
2700Sstevel@tonic-gate return (error);
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate
2730Sstevel@tonic-gate /*ARGSUSED*/
2740Sstevel@tonic-gate static int
rnd_write(dev_t dev,struct uio * uiop,cred_t * credp)2750Sstevel@tonic-gate rnd_write(dev_t dev, struct uio *uiop, cred_t *credp)
2760Sstevel@tonic-gate {
2770Sstevel@tonic-gate int error;
2780Sstevel@tonic-gate uint8_t buf[WRITEBUFSIZE];
2790Sstevel@tonic-gate size_t bytes;
2801920Smcpowers minor_t devno;
2811920Smcpowers
2821920Smcpowers devno = getminor(dev);
2830Sstevel@tonic-gate
2840Sstevel@tonic-gate while (uiop->uio_resid > 0) {
2850Sstevel@tonic-gate bytes = min(sizeof (buf), uiop->uio_resid);
2860Sstevel@tonic-gate
2870Sstevel@tonic-gate /* See comments in rnd_read() */
2880Sstevel@tonic-gate uiop->uio_loffset = 0;
2890Sstevel@tonic-gate if ((error = uiomove(buf, bytes, UIO_WRITE, uiop)) != 0)
2900Sstevel@tonic-gate return (error);
2910Sstevel@tonic-gate
2921920Smcpowers switch (devno) {
2931920Smcpowers case DEVRANDOM:
2941920Smcpowers if ((error = random_add_entropy(buf, bytes, 0)) != 0)
2951920Smcpowers return (error);
2961920Smcpowers break;
2971920Smcpowers case DEVURANDOM:
2981920Smcpowers if ((error = random_add_pseudo_entropy(buf, bytes,
2991920Smcpowers 0)) != 0)
3001920Smcpowers return (error);
3011920Smcpowers break;
3021920Smcpowers default:
3031920Smcpowers return (ENXIO);
3041920Smcpowers }
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate
3070Sstevel@tonic-gate return (0);
3080Sstevel@tonic-gate }
3090Sstevel@tonic-gate
3108928SBhargava.Yenduri@Sun.COM static struct pollhead urnd_pollhd;
3118928SBhargava.Yenduri@Sun.COM
3120Sstevel@tonic-gate /*
3130Sstevel@tonic-gate * poll(2) is supported as follows:
3148928SBhargava.Yenduri@Sun.COM * . Only POLLIN, POLLOUT, and POLLRDNORM events are supported.
3150Sstevel@tonic-gate * . POLLOUT always succeeds.
3160Sstevel@tonic-gate * . POLLIN and POLLRDNORM from /dev/urandom always succeeds.
3170Sstevel@tonic-gate * . POLLIN and POLLRDNORM from /dev/random will block until a
3180Sstevel@tonic-gate * minimum amount of entropy is available.
3190Sstevel@tonic-gate */
3200Sstevel@tonic-gate static int
rnd_chpoll(dev_t dev,short events,int anyyet,short * reventsp,struct pollhead ** phpp)3210Sstevel@tonic-gate rnd_chpoll(dev_t dev, short events, int anyyet, short *reventsp,
3228928SBhargava.Yenduri@Sun.COM struct pollhead **phpp)
3230Sstevel@tonic-gate {
3240Sstevel@tonic-gate switch (getminor(dev)) {
3250Sstevel@tonic-gate case DEVURANDOM:
3260Sstevel@tonic-gate *reventsp = events & (POLLOUT | POLLIN | POLLRDNORM);
3270Sstevel@tonic-gate
3288928SBhargava.Yenduri@Sun.COM /*
3298928SBhargava.Yenduri@Sun.COM * A non NULL pollhead pointer should be returned in case
3308928SBhargava.Yenduri@Sun.COM * user polls for 0 events.
3318928SBhargava.Yenduri@Sun.COM */
3320Sstevel@tonic-gate if (*reventsp == 0 && !anyyet)
3338928SBhargava.Yenduri@Sun.COM *phpp = &urnd_pollhd;
3340Sstevel@tonic-gate
3350Sstevel@tonic-gate break;
3360Sstevel@tonic-gate case DEVRANDOM:
3378928SBhargava.Yenduri@Sun.COM kcf_rnd_chpoll(events, anyyet, reventsp, phpp);
3380Sstevel@tonic-gate break;
3390Sstevel@tonic-gate default:
3400Sstevel@tonic-gate return (ENXIO);
3410Sstevel@tonic-gate }
3420Sstevel@tonic-gate
3430Sstevel@tonic-gate return (0);
3440Sstevel@tonic-gate }
345