17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 58047c9fbSmcpowers * Common Development and Distribution License (the "License"). 68047c9fbSmcpowers * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate * 219d31afc5SKrishna Yenduri * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 227c478bd9Sstevel@tonic-gate * Use is subject to license terms. 237c478bd9Sstevel@tonic-gate */ 247c478bd9Sstevel@tonic-gate 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate /* 277c478bd9Sstevel@tonic-gate * Random number generator pseudo-driver 287c478bd9Sstevel@tonic-gate * 297c478bd9Sstevel@tonic-gate * This is a lightweight driver which calls in to the Kernel Cryptographic 307c478bd9Sstevel@tonic-gate * Framework to do the real work. Kernel modules should NOT depend on this 317c478bd9Sstevel@tonic-gate * driver for /dev/random kernel API. 327c478bd9Sstevel@tonic-gate * 337c478bd9Sstevel@tonic-gate * Applications may ask for 2 types of random bits: 347c478bd9Sstevel@tonic-gate * . High quality random by reading from /dev/random. The output is extracted 357c478bd9Sstevel@tonic-gate * only when a minimum amount of entropy is available. 367c478bd9Sstevel@tonic-gate * . Pseudo-random, by reading from /dev/urandom, that can be generated any 377c478bd9Sstevel@tonic-gate * time. 387c478bd9Sstevel@tonic-gate */ 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate #include <sys/types.h> 417c478bd9Sstevel@tonic-gate #include <sys/errno.h> 427c478bd9Sstevel@tonic-gate #include <sys/stat.h> 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate #include <sys/file.h> 457c478bd9Sstevel@tonic-gate #include <sys/open.h> 467c478bd9Sstevel@tonic-gate #include <sys/poll.h> 477c478bd9Sstevel@tonic-gate #include <sys/uio.h> 487c478bd9Sstevel@tonic-gate #include <sys/cred.h> 497c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 507c478bd9Sstevel@tonic-gate #include <sys/conf.h> 517c478bd9Sstevel@tonic-gate #include <sys/ddi.h> 527c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 537c478bd9Sstevel@tonic-gate #include <sys/random.h> 547c478bd9Sstevel@tonic-gate #include <sys/crypto/impl.h> 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate #define DEVRANDOM 0 577c478bd9Sstevel@tonic-gate #define DEVURANDOM 1 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate #define HASHSIZE 20 /* Assuming a SHA1 hash algorithm */ 607c478bd9Sstevel@tonic-gate #define WRITEBUFSIZE 512 /* Size of buffer for write request */ 617c478bd9Sstevel@tonic-gate #define MAXRETBYTES 1040 /* Max bytes returned per read. */ 627c478bd9Sstevel@tonic-gate /* Must be a multiple of HASHSIZE */ 637c478bd9Sstevel@tonic-gate static dev_info_t *rnd_dip; 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate static int rnd_open(dev_t *, int, int, cred_t *); 667c478bd9Sstevel@tonic-gate static int rnd_close(dev_t, int, int, cred_t *); 677c478bd9Sstevel@tonic-gate static int rnd_read(dev_t, struct uio *, cred_t *); 687c478bd9Sstevel@tonic-gate static int rnd_write(dev_t, struct uio *, cred_t *); 697c478bd9Sstevel@tonic-gate static int rnd_chpoll(dev_t, short, int, short *, struct pollhead **); 707c478bd9Sstevel@tonic-gate static int rnd_attach(dev_info_t *, ddi_attach_cmd_t); 717c478bd9Sstevel@tonic-gate static int rnd_detach(dev_info_t *, ddi_detach_cmd_t); 727c478bd9Sstevel@tonic-gate static int rnd_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **); 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate /* DDI declarations */ 757c478bd9Sstevel@tonic-gate static struct cb_ops rnd_cb_ops = { 767c478bd9Sstevel@tonic-gate rnd_open, /* open */ 777c478bd9Sstevel@tonic-gate rnd_close, /* close */ 787c478bd9Sstevel@tonic-gate nodev, /* strategy */ 797c478bd9Sstevel@tonic-gate nodev, /* print */ 807c478bd9Sstevel@tonic-gate nodev, /* dump */ 817c478bd9Sstevel@tonic-gate rnd_read, /* read */ 827c478bd9Sstevel@tonic-gate rnd_write, /* write */ 837c478bd9Sstevel@tonic-gate nodev, /* ioctl */ 847c478bd9Sstevel@tonic-gate nodev, /* devmap */ 857c478bd9Sstevel@tonic-gate nodev, /* mmap */ 867c478bd9Sstevel@tonic-gate nodev, /* segmap */ 877c478bd9Sstevel@tonic-gate rnd_chpoll, /* chpoll */ 887c478bd9Sstevel@tonic-gate ddi_prop_op, /* prop_op */ 897c478bd9Sstevel@tonic-gate NULL, /* streamtab */ 907c478bd9Sstevel@tonic-gate (D_NEW | D_MP), /* cb_flag */ 917c478bd9Sstevel@tonic-gate CB_REV, /* cb_rev */ 927c478bd9Sstevel@tonic-gate nodev, /* aread */ 937c478bd9Sstevel@tonic-gate nodev /* awrite */ 947c478bd9Sstevel@tonic-gate }; 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate static struct dev_ops rnd_ops = { 977c478bd9Sstevel@tonic-gate DEVO_REV, /* devo_rev, */ 987c478bd9Sstevel@tonic-gate 0, /* refcnt */ 997c478bd9Sstevel@tonic-gate rnd_getinfo, /* get_dev_info */ 1007c478bd9Sstevel@tonic-gate nulldev, /* identify */ 1017c478bd9Sstevel@tonic-gate nulldev, /* probe */ 1027c478bd9Sstevel@tonic-gate rnd_attach, /* attach */ 1037c478bd9Sstevel@tonic-gate rnd_detach, /* detach */ 1047c478bd9Sstevel@tonic-gate nodev, /* reset */ 1057c478bd9Sstevel@tonic-gate &rnd_cb_ops, /* driver operations */ 1067c478bd9Sstevel@tonic-gate NULL, /* bus operations */ 10719397407SSherry Moore NULL, /* power */ 10819397407SSherry Moore ddi_quiesce_not_needed, /* quiesce */ 1097c478bd9Sstevel@tonic-gate }; 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate /* Modlinkage */ 1127c478bd9Sstevel@tonic-gate static struct modldrv modldrv = { 1137c478bd9Sstevel@tonic-gate &mod_driverops, 11419397407SSherry Moore "random number device", 1157c478bd9Sstevel@tonic-gate &rnd_ops 1167c478bd9Sstevel@tonic-gate }; 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = { MODREV_1, { &modldrv, NULL } }; 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate /* DDI glue */ 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate int 1247c478bd9Sstevel@tonic-gate _init(void) 1257c478bd9Sstevel@tonic-gate { 1267c478bd9Sstevel@tonic-gate return (mod_install(&modlinkage)); 1277c478bd9Sstevel@tonic-gate } 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate int 1307c478bd9Sstevel@tonic-gate _fini(void) 1317c478bd9Sstevel@tonic-gate { 1327c478bd9Sstevel@tonic-gate return (mod_remove(&modlinkage)); 1337c478bd9Sstevel@tonic-gate } 1347c478bd9Sstevel@tonic-gate 1357c478bd9Sstevel@tonic-gate int 1367c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop) 1377c478bd9Sstevel@tonic-gate { 1387c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 1397c478bd9Sstevel@tonic-gate } 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate static int 1427c478bd9Sstevel@tonic-gate rnd_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 1437c478bd9Sstevel@tonic-gate { 1447c478bd9Sstevel@tonic-gate if (cmd != DDI_ATTACH) 1457c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate if (ddi_create_minor_node(dip, "random", S_IFCHR, DEVRANDOM, 1487c478bd9Sstevel@tonic-gate DDI_PSEUDO, 0) == DDI_FAILURE) { 1497c478bd9Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 1507c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 1517c478bd9Sstevel@tonic-gate } 1527c478bd9Sstevel@tonic-gate if (ddi_create_minor_node(dip, "urandom", S_IFCHR, DEVURANDOM, 1537c478bd9Sstevel@tonic-gate DDI_PSEUDO, 0) == DDI_FAILURE) { 1547c478bd9Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 1557c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 1567c478bd9Sstevel@tonic-gate } 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate rnd_dip = dip; 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 1617c478bd9Sstevel@tonic-gate } 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate static int 1647c478bd9Sstevel@tonic-gate rnd_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 1657c478bd9Sstevel@tonic-gate { 1667c478bd9Sstevel@tonic-gate if (cmd != DDI_DETACH) 1677c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate rnd_dip = NULL; 1707c478bd9Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 1737c478bd9Sstevel@tonic-gate } 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 1767c478bd9Sstevel@tonic-gate static int 1777c478bd9Sstevel@tonic-gate rnd_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 1787c478bd9Sstevel@tonic-gate { 1797c478bd9Sstevel@tonic-gate int error; 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate switch (infocmd) { 1827c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 1837c478bd9Sstevel@tonic-gate *result = rnd_dip; 1847c478bd9Sstevel@tonic-gate error = DDI_SUCCESS; 1857c478bd9Sstevel@tonic-gate break; 1867c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 1877c478bd9Sstevel@tonic-gate *result = (void *)0; 1887c478bd9Sstevel@tonic-gate error = DDI_SUCCESS; 1897c478bd9Sstevel@tonic-gate break; 1907c478bd9Sstevel@tonic-gate default: 1917c478bd9Sstevel@tonic-gate error = DDI_FAILURE; 1927c478bd9Sstevel@tonic-gate } 1937c478bd9Sstevel@tonic-gate return (error); 1947c478bd9Sstevel@tonic-gate } 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate /*ARGSUSED3*/ 1977c478bd9Sstevel@tonic-gate static int 1987c478bd9Sstevel@tonic-gate rnd_open(dev_t *devp, int flag, int otyp, cred_t *credp) 1997c478bd9Sstevel@tonic-gate { 2007c478bd9Sstevel@tonic-gate switch (getminor(*devp)) { 2017c478bd9Sstevel@tonic-gate case DEVRANDOM: 2027c478bd9Sstevel@tonic-gate if (!kcf_rngprov_check()) 2037c478bd9Sstevel@tonic-gate return (ENXIO); 2047c478bd9Sstevel@tonic-gate break; 2057c478bd9Sstevel@tonic-gate case DEVURANDOM: 2067c478bd9Sstevel@tonic-gate break; 2077c478bd9Sstevel@tonic-gate default: 2087c478bd9Sstevel@tonic-gate return (ENXIO); 2097c478bd9Sstevel@tonic-gate } 2107c478bd9Sstevel@tonic-gate if (otyp != OTYP_CHR) 2117c478bd9Sstevel@tonic-gate return (EINVAL); 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate if (flag & FEXCL) 2147c478bd9Sstevel@tonic-gate return (EINVAL); 2157c478bd9Sstevel@tonic-gate return (0); 2167c478bd9Sstevel@tonic-gate } 2177c478bd9Sstevel@tonic-gate 2187c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 2197c478bd9Sstevel@tonic-gate static int 2207c478bd9Sstevel@tonic-gate rnd_close(dev_t dev, int flag, int otyp, cred_t *credp) 2217c478bd9Sstevel@tonic-gate { 2227c478bd9Sstevel@tonic-gate return (0); 2237c478bd9Sstevel@tonic-gate } 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate /*ARGSUSED2*/ 2267c478bd9Sstevel@tonic-gate static int 2277c478bd9Sstevel@tonic-gate rnd_read(dev_t dev, struct uio *uiop, cred_t *credp) 2287c478bd9Sstevel@tonic-gate { 2297c478bd9Sstevel@tonic-gate size_t len; 2307c478bd9Sstevel@tonic-gate minor_t devno; 2317c478bd9Sstevel@tonic-gate int error = 0; 2327c478bd9Sstevel@tonic-gate int nbytes = 0; 2337c478bd9Sstevel@tonic-gate uint8_t random_bytes[2 * HASHSIZE]; 2347c478bd9Sstevel@tonic-gate 2357c478bd9Sstevel@tonic-gate devno = getminor(dev); 2367c478bd9Sstevel@tonic-gate 2377c478bd9Sstevel@tonic-gate while (error == 0 && uiop->uio_resid > 0) { 2387c478bd9Sstevel@tonic-gate len = min(sizeof (random_bytes), uiop->uio_resid); 2397c478bd9Sstevel@tonic-gate switch (devno) { 2407c478bd9Sstevel@tonic-gate case DEVRANDOM: 2417c478bd9Sstevel@tonic-gate error = kcf_rnd_get_bytes(random_bytes, len, 242*8b502715SKrishna Yenduri uiop->uio_fmode & (FNDELAY|FNONBLOCK)); 2437c478bd9Sstevel@tonic-gate break; 2447c478bd9Sstevel@tonic-gate case DEVURANDOM: 2457c478bd9Sstevel@tonic-gate error = kcf_rnd_get_pseudo_bytes(random_bytes, len); 2467c478bd9Sstevel@tonic-gate break; 2477c478bd9Sstevel@tonic-gate default: 2487c478bd9Sstevel@tonic-gate return (ENXIO); 2497c478bd9Sstevel@tonic-gate } 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate if (error == 0) { 2527c478bd9Sstevel@tonic-gate /* 2537c478bd9Sstevel@tonic-gate * /dev/[u]random is not a seekable device. To prevent 2547c478bd9Sstevel@tonic-gate * uio offset from growing and eventually exceeding 2557c478bd9Sstevel@tonic-gate * the maximum, reset the offset here for every call. 2567c478bd9Sstevel@tonic-gate */ 2577c478bd9Sstevel@tonic-gate uiop->uio_loffset = 0; 2587c478bd9Sstevel@tonic-gate error = uiomove(random_bytes, len, UIO_READ, uiop); 2597c478bd9Sstevel@tonic-gate 2607c478bd9Sstevel@tonic-gate nbytes += len; 2617c478bd9Sstevel@tonic-gate 262f317a3a3Skrishna if (devno == DEVRANDOM && nbytes >= MAXRETBYTES) 2637c478bd9Sstevel@tonic-gate break; 2647c478bd9Sstevel@tonic-gate 2657c478bd9Sstevel@tonic-gate } else if ((error == EAGAIN) && (nbytes > 0)) { 2667c478bd9Sstevel@tonic-gate error = 0; 2677c478bd9Sstevel@tonic-gate break; 2687c478bd9Sstevel@tonic-gate } 2697c478bd9Sstevel@tonic-gate } 2707c478bd9Sstevel@tonic-gate return (error); 2717c478bd9Sstevel@tonic-gate } 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 2747c478bd9Sstevel@tonic-gate static int 2757c478bd9Sstevel@tonic-gate rnd_write(dev_t dev, struct uio *uiop, cred_t *credp) 2767c478bd9Sstevel@tonic-gate { 2777c478bd9Sstevel@tonic-gate int error; 2787c478bd9Sstevel@tonic-gate uint8_t buf[WRITEBUFSIZE]; 2797c478bd9Sstevel@tonic-gate size_t bytes; 2808047c9fbSmcpowers minor_t devno; 2818047c9fbSmcpowers 2828047c9fbSmcpowers devno = getminor(dev); 2837c478bd9Sstevel@tonic-gate 2847c478bd9Sstevel@tonic-gate while (uiop->uio_resid > 0) { 2857c478bd9Sstevel@tonic-gate bytes = min(sizeof (buf), uiop->uio_resid); 2867c478bd9Sstevel@tonic-gate 2877c478bd9Sstevel@tonic-gate /* See comments in rnd_read() */ 2887c478bd9Sstevel@tonic-gate uiop->uio_loffset = 0; 2897c478bd9Sstevel@tonic-gate if ((error = uiomove(buf, bytes, UIO_WRITE, uiop)) != 0) 2907c478bd9Sstevel@tonic-gate return (error); 2917c478bd9Sstevel@tonic-gate 2928047c9fbSmcpowers switch (devno) { 2938047c9fbSmcpowers case DEVRANDOM: 2947c478bd9Sstevel@tonic-gate if ((error = random_add_entropy(buf, bytes, 0)) != 0) 2957c478bd9Sstevel@tonic-gate return (error); 2968047c9fbSmcpowers break; 2978047c9fbSmcpowers case DEVURANDOM: 2988047c9fbSmcpowers if ((error = random_add_pseudo_entropy(buf, bytes, 2998047c9fbSmcpowers 0)) != 0) 3008047c9fbSmcpowers return (error); 3018047c9fbSmcpowers break; 3028047c9fbSmcpowers default: 3038047c9fbSmcpowers return (ENXIO); 3048047c9fbSmcpowers } 3057c478bd9Sstevel@tonic-gate } 3067c478bd9Sstevel@tonic-gate 3077c478bd9Sstevel@tonic-gate return (0); 3087c478bd9Sstevel@tonic-gate } 3097c478bd9Sstevel@tonic-gate 3109d31afc5SKrishna Yenduri static struct pollhead urnd_pollhd; 3119d31afc5SKrishna Yenduri 3127c478bd9Sstevel@tonic-gate /* 3137c478bd9Sstevel@tonic-gate * poll(2) is supported as follows: 3149d31afc5SKrishna Yenduri * . Only POLLIN, POLLOUT, and POLLRDNORM events are supported. 3157c478bd9Sstevel@tonic-gate * . POLLOUT always succeeds. 3167c478bd9Sstevel@tonic-gate * . POLLIN and POLLRDNORM from /dev/urandom always succeeds. 3177c478bd9Sstevel@tonic-gate * . POLLIN and POLLRDNORM from /dev/random will block until a 3187c478bd9Sstevel@tonic-gate * minimum amount of entropy is available. 3197c478bd9Sstevel@tonic-gate */ 3207c478bd9Sstevel@tonic-gate static int 3217c478bd9Sstevel@tonic-gate rnd_chpoll(dev_t dev, short events, int anyyet, short *reventsp, 3227c478bd9Sstevel@tonic-gate struct pollhead **phpp) 3237c478bd9Sstevel@tonic-gate { 3247c478bd9Sstevel@tonic-gate switch (getminor(dev)) { 3257c478bd9Sstevel@tonic-gate case DEVURANDOM: 3267c478bd9Sstevel@tonic-gate *reventsp = events & (POLLOUT | POLLIN | POLLRDNORM); 3277c478bd9Sstevel@tonic-gate 3289d31afc5SKrishna Yenduri /* 3299d31afc5SKrishna Yenduri * A non NULL pollhead pointer should be returned in case 3309d31afc5SKrishna Yenduri * user polls for 0 events. 3319d31afc5SKrishna Yenduri */ 3327c478bd9Sstevel@tonic-gate if (*reventsp == 0 && !anyyet) 3339d31afc5SKrishna Yenduri *phpp = &urnd_pollhd; 3347c478bd9Sstevel@tonic-gate 3357c478bd9Sstevel@tonic-gate break; 3367c478bd9Sstevel@tonic-gate case DEVRANDOM: 3379d31afc5SKrishna Yenduri kcf_rnd_chpoll(events, anyyet, reventsp, phpp); 3387c478bd9Sstevel@tonic-gate break; 3397c478bd9Sstevel@tonic-gate default: 3407c478bd9Sstevel@tonic-gate return (ENXIO); 3417c478bd9Sstevel@tonic-gate } 3427c478bd9Sstevel@tonic-gate 3437c478bd9Sstevel@tonic-gate return (0); 3447c478bd9Sstevel@tonic-gate } 345