random.c revision 8047c9fb10f4d3f14385d535d6b23a5eb80c0c0f
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Random number generator pseudo-driver
*
* This is a lightweight driver which calls in to the Kernel Cryptographic
* Framework to do the real work. Kernel modules should NOT depend on this
*
* Applications may ask for 2 types of random bits:
* only when a minimum amount of entropy is available.
* time.
*/
#define DEVRANDOM 0
#define DEVURANDOM 1
/* Must be a multiple of HASHSIZE */
static dev_info_t *rnd_dip;
/* DDI declarations */
static struct cb_ops rnd_cb_ops = {
rnd_open, /* open */
rnd_close, /* close */
nodev, /* strategy */
nodev, /* print */
nodev, /* dump */
rnd_read, /* read */
rnd_write, /* write */
nodev, /* ioctl */
nodev, /* devmap */
nodev, /* mmap */
nodev, /* segmap */
rnd_chpoll, /* chpoll */
ddi_prop_op, /* prop_op */
NULL, /* streamtab */
CB_REV, /* cb_rev */
nodev, /* aread */
nodev /* awrite */
};
DEVO_REV, /* devo_rev, */
0, /* refcnt */
rnd_getinfo, /* get_dev_info */
nulldev, /* identify */
nulldev, /* probe */
rnd_attach, /* attach */
rnd_detach, /* detach */
nodev, /* reset */
&rnd_cb_ops, /* driver operations */
NULL, /* bus operations */
NULL /* power */
};
/* Modlinkage */
"random number device v%I%",
};
/* DDI glue */
int
_init(void)
{
return (mod_install(&modlinkage));
}
int
_fini(void)
{
return (mod_remove(&modlinkage));
}
int
{
}
static int
{
if (cmd != DDI_ATTACH)
return (DDI_FAILURE);
DDI_PSEUDO, 0) == DDI_FAILURE) {
return (DDI_FAILURE);
}
DDI_PSEUDO, 0) == DDI_FAILURE) {
return (DDI_FAILURE);
}
return (DDI_SUCCESS);
}
static int
{
if (cmd != DDI_DETACH)
return (DDI_FAILURE);
return (DDI_SUCCESS);
}
/*ARGSUSED*/
static int
{
int error;
switch (infocmd) {
case DDI_INFO_DEVT2DEVINFO:
error = DDI_SUCCESS;
break;
case DDI_INFO_DEVT2INSTANCE:
*result = (void *)0;
error = DDI_SUCCESS;
break;
default:
error = DDI_FAILURE;
}
return (error);
}
/*ARGSUSED3*/
static int
{
case DEVRANDOM:
if (!kcf_rngprov_check())
return (ENXIO);
break;
case DEVURANDOM:
break;
default:
return (ENXIO);
}
return (EINVAL);
return (EINVAL);
return (0);
}
/*ARGSUSED*/
static int
{
return (0);
}
/*ARGSUSED2*/
static int
{
int error = 0;
int nbytes = 0;
switch (devno) {
case DEVRANDOM:
break;
case DEVURANDOM:
break;
default:
return (ENXIO);
}
if (error == 0) {
/*
* /dev/[u]random is not a seekable device. To prevent
* uio offset from growing and eventually exceeding
* the maximum, reset the offset here for every call.
*/
uiop->uio_loffset = 0;
if (nbytes >= MAXRETBYTES)
break;
error = 0;
break;
}
}
return (error);
}
/*ARGSUSED*/
static int
{
int error;
/* See comments in rnd_read() */
uiop->uio_loffset = 0;
return (error);
switch (devno) {
case DEVRANDOM:
return (error);
break;
case DEVURANDOM:
0)) != 0)
return (error);
break;
default:
return (ENXIO);
}
}
return (0);
}
/*
* poll(2) is supported as follows:
* . Only POLLIN, POLLOUT, and POLLRDNORM events are valid.
* . POLLOUT always succeeds.
* minimum amount of entropy is available.
*/
static int
{
case DEVURANDOM:
/* We're being polled for non-supported events */
return (EINVAL);
break;
case DEVRANDOM:
/* Either POLLOUT only or unsupported event */
return (EINVAL);
break;
}
break;
default:
return (ENXIO);
}
return (0);
}