xref: /onnv-gate/usr/src/uts/common/crypto/io/crypto.c (revision 4219:f64d02446988)
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
51808Smcpowers  * Common Development and Distribution License (the "License").
61808Smcpowers  * 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  */
210Sstevel@tonic-gate /*
223620Skrishna  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * The ioctl interface for cryptographic commands.
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <sys/modctl.h>
340Sstevel@tonic-gate #include <sys/conf.h>
350Sstevel@tonic-gate #include <sys/stat.h>
360Sstevel@tonic-gate #include <sys/ddi.h>
370Sstevel@tonic-gate #include <sys/sunddi.h>
380Sstevel@tonic-gate #include <sys/kmem.h>
390Sstevel@tonic-gate #include <sys/errno.h>
400Sstevel@tonic-gate #include <sys/ksynch.h>
410Sstevel@tonic-gate #include <sys/file.h>
420Sstevel@tonic-gate #include <sys/open.h>
430Sstevel@tonic-gate #include <sys/cred.h>
440Sstevel@tonic-gate #include <sys/proc.h>
450Sstevel@tonic-gate #include <sys/task.h>
460Sstevel@tonic-gate #include <sys/mkdev.h>
470Sstevel@tonic-gate #include <sys/model.h>
480Sstevel@tonic-gate #include <sys/sysmacros.h>
490Sstevel@tonic-gate #include <sys/crypto/common.h>
500Sstevel@tonic-gate #include <sys/crypto/api.h>
510Sstevel@tonic-gate #include <sys/crypto/impl.h>
520Sstevel@tonic-gate #include <sys/crypto/sched_impl.h>
530Sstevel@tonic-gate #include <sys/crypto/ioctl.h>
540Sstevel@tonic-gate 
550Sstevel@tonic-gate /*
560Sstevel@tonic-gate  * Locking notes:
570Sstevel@tonic-gate  *
580Sstevel@tonic-gate  * crypto_lock protects the global array of minor structures.  It
590Sstevel@tonic-gate  * also protects the cm_refcnt member of each of these structures.
600Sstevel@tonic-gate  * The crypto_cv is used to signal decrements in the cm_refcnt,
610Sstevel@tonic-gate  * and is used with the global crypto_lock.
620Sstevel@tonic-gate  *
630Sstevel@tonic-gate  * Other fields in the minor structure are protected by the
640Sstevel@tonic-gate  * cm_lock member of the minor structure.
650Sstevel@tonic-gate  */
660Sstevel@tonic-gate 
670Sstevel@tonic-gate /*
680Sstevel@tonic-gate  * DDI entry points.
690Sstevel@tonic-gate  */
700Sstevel@tonic-gate static int crypto_attach(dev_info_t *, ddi_attach_cmd_t);
710Sstevel@tonic-gate static int crypto_detach(dev_info_t *, ddi_detach_cmd_t);
720Sstevel@tonic-gate static int crypto_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
730Sstevel@tonic-gate static int crypto_open(dev_t *, int, int, cred_t *);
740Sstevel@tonic-gate static int crypto_close(dev_t, int, int, cred_t *);
750Sstevel@tonic-gate static int crypto_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
760Sstevel@tonic-gate 
77904Smcpowers static int cipher_init(dev_t, caddr_t, int, int (*)(crypto_provider_t,
780Sstevel@tonic-gate     crypto_session_id_t, crypto_mechanism_t *, crypto_key_t *,
790Sstevel@tonic-gate     crypto_ctx_template_t, crypto_context_t *, crypto_call_req_t *));
800Sstevel@tonic-gate 
810Sstevel@tonic-gate static int common_digest(dev_t, caddr_t, int, int (*)(crypto_context_t,
820Sstevel@tonic-gate     crypto_data_t *, crypto_data_t *, crypto_call_req_t *));
830Sstevel@tonic-gate 
840Sstevel@tonic-gate static int cipher(dev_t, caddr_t, int, int (*)(crypto_context_t,
850Sstevel@tonic-gate     crypto_data_t *, crypto_data_t *, crypto_call_req_t *));
860Sstevel@tonic-gate 
870Sstevel@tonic-gate static int cipher_update(dev_t, caddr_t, int, int (*)(crypto_context_t,
880Sstevel@tonic-gate     crypto_data_t *, crypto_data_t *, crypto_call_req_t *));
890Sstevel@tonic-gate 
900Sstevel@tonic-gate static int common_final(dev_t, caddr_t, int, int (*)(crypto_context_t,
910Sstevel@tonic-gate     crypto_data_t *, crypto_call_req_t *));
920Sstevel@tonic-gate 
93904Smcpowers static int sign_verify_init(dev_t, caddr_t, int, int (*)(crypto_provider_t,
940Sstevel@tonic-gate     crypto_session_id_t, crypto_mechanism_t *, crypto_key_t *,
950Sstevel@tonic-gate     crypto_ctx_template_t, crypto_context_t *, crypto_call_req_t *));
960Sstevel@tonic-gate 
970Sstevel@tonic-gate static int sign_verify_update(dev_t dev, caddr_t arg, int mode,
980Sstevel@tonic-gate     int (*)(crypto_context_t, crypto_data_t *, crypto_call_req_t *));
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate static void crypto_initialize_rctl(void);
1010Sstevel@tonic-gate static void crypto_release_provider_session(crypto_minor_t *,
1020Sstevel@tonic-gate     crypto_provider_session_t *);
1033916Skrishna static int crypto_buffer_check(size_t);
1040Sstevel@tonic-gate static int crypto_free_find_ctx(crypto_session_data_t *);
1050Sstevel@tonic-gate static int crypto_get_provider_list(crypto_minor_t *, uint_t *,
1060Sstevel@tonic-gate     crypto_provider_entry_t **, boolean_t);
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate /* number of minor numbers to allocate at a time */
1090Sstevel@tonic-gate #define	CRYPTO_MINOR_CHUNK	16
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate /*
1120Sstevel@tonic-gate  * There are two limits associated with kernel memory. The first,
1130Sstevel@tonic-gate  * CRYPTO_MAX_BUFFER_LEN, is the maximum number of bytes that can be
1140Sstevel@tonic-gate  * allocated for a single copyin/copyout buffer. The second limit is
1150Sstevel@tonic-gate  * the total number of bytes that can be allocated by a process
1160Sstevel@tonic-gate  * for copyin/copyout buffers. The latter is enforced by the
1170Sstevel@tonic-gate  * project.max-crypto-memory resource control.
1180Sstevel@tonic-gate  */
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate #define	CRYPTO_MAX_BUFFER_LEN	(2 * 1024 * 1024)
1210Sstevel@tonic-gate #define	CRYPTO_MAX_FIND_COUNT	512
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate /*
1240Sstevel@tonic-gate  * When a mechanism parameter length is less than CRYPTO_DEFERRED_LIMIT
1250Sstevel@tonic-gate  * bytes, then the length is added to the next resource control check.
1260Sstevel@tonic-gate  */
1270Sstevel@tonic-gate #define	CRYPTO_DEFERRED_LIMIT	100
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate /* The session table grows by CRYPTO_SESSION_CHUNK increments */
1300Sstevel@tonic-gate #define	CRYPTO_SESSION_CHUNK	100
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate size_t crypto_max_buffer_len = CRYPTO_MAX_BUFFER_LEN;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate #define	INIT_RAW_CRYPTO_DATA(data, len)				\
1350Sstevel@tonic-gate 	(data).cd_format = CRYPTO_DATA_RAW;			\
1360Sstevel@tonic-gate 	(data).cd_raw.iov_base = kmem_alloc(len, KM_SLEEP);	\
1370Sstevel@tonic-gate 	(data).cd_raw.iov_len = len;				\
1380Sstevel@tonic-gate 	(data).cd_offset = 0;					\
1390Sstevel@tonic-gate 	(data).cd_length = len;
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate static struct kmem_cache *crypto_session_cache;
1420Sstevel@tonic-gate static crypto_minor_t **crypto_minors = NULL;
1430Sstevel@tonic-gate static dev_info_t *crypto_dip = NULL;
1440Sstevel@tonic-gate static minor_t crypto_minor_chunk = CRYPTO_MINOR_CHUNK;
1450Sstevel@tonic-gate static minor_t crypto_minors_table_count = 0;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate /*
1480Sstevel@tonic-gate  * Minors are started from 1 because vmem_alloc()
1490Sstevel@tonic-gate  * returns 0 in case of failure.
1500Sstevel@tonic-gate  */
1510Sstevel@tonic-gate static vmem_t *crypto_arena = NULL;	/* Arena for device minors */
1520Sstevel@tonic-gate static minor_t crypto_minors_count = 0;
1530Sstevel@tonic-gate static kmutex_t crypto_lock;
1540Sstevel@tonic-gate static kcondvar_t crypto_cv;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate #define	RETURN_LIST			B_TRUE
1570Sstevel@tonic-gate #define	DONT_RETURN_LIST		B_FALSE
1580Sstevel@tonic-gate 
159904Smcpowers #define	CRYPTO_OPS_OFFSET(f)		offsetof(crypto_ops_t, co_##f)
1600Sstevel@tonic-gate #define	CRYPTO_RANDOM_OFFSET(f)		offsetof(crypto_random_number_ops_t, f)
1610Sstevel@tonic-gate #define	CRYPTO_SESSION_OFFSET(f)	offsetof(crypto_session_ops_t, f)
1620Sstevel@tonic-gate #define	CRYPTO_OBJECT_OFFSET(f)		offsetof(crypto_object_ops_t, f)
1630Sstevel@tonic-gate #define	CRYPTO_PROVIDER_OFFSET(f)	\
1640Sstevel@tonic-gate 	offsetof(crypto_provider_management_ops_t, f)
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate #define	CRYPTO_CANCEL_CTX(spp) {	\
1670Sstevel@tonic-gate 	crypto_cancel_ctx(*(spp));	\
1680Sstevel@tonic-gate 	*(spp) = NULL;			\
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate #define	CRYPTO_CANCEL_ALL_CTX(sp) {				\
1720Sstevel@tonic-gate 	if ((sp)->sd_digest_ctx != NULL) {			\
1730Sstevel@tonic-gate 		crypto_cancel_ctx((sp)->sd_digest_ctx);		\
1740Sstevel@tonic-gate 		(sp)->sd_digest_ctx = NULL;			\
1750Sstevel@tonic-gate 	}							\
1760Sstevel@tonic-gate 	if ((sp)->sd_encr_ctx != NULL) {			\
1770Sstevel@tonic-gate 		crypto_cancel_ctx((sp)->sd_encr_ctx);		\
1780Sstevel@tonic-gate 		(sp)->sd_encr_ctx = NULL;			\
1790Sstevel@tonic-gate 	}							\
1800Sstevel@tonic-gate 	if ((sp)->sd_decr_ctx != NULL) {			\
1810Sstevel@tonic-gate 		crypto_cancel_ctx((sp)->sd_decr_ctx);		\
1820Sstevel@tonic-gate 		(sp)->sd_decr_ctx = NULL;			\
1830Sstevel@tonic-gate 	}							\
1840Sstevel@tonic-gate 	if ((sp)->sd_sign_ctx != NULL) {			\
1850Sstevel@tonic-gate 		crypto_cancel_ctx((sp)->sd_sign_ctx);		\
1860Sstevel@tonic-gate 		(sp)->sd_sign_ctx = NULL;			\
1870Sstevel@tonic-gate 	}							\
1880Sstevel@tonic-gate 	if ((sp)->sd_verify_ctx != NULL) {			\
1890Sstevel@tonic-gate 		crypto_cancel_ctx((sp)->sd_verify_ctx);		\
1900Sstevel@tonic-gate 		(sp)->sd_verify_ctx = NULL;			\
1910Sstevel@tonic-gate 	}							\
1920Sstevel@tonic-gate 	if ((sp)->sd_sign_recover_ctx != NULL) {		\
1930Sstevel@tonic-gate 		crypto_cancel_ctx((sp)->sd_sign_recover_ctx);	\
1940Sstevel@tonic-gate 		(sp)->sd_sign_recover_ctx = NULL;		\
1950Sstevel@tonic-gate 	}							\
1960Sstevel@tonic-gate 	if ((sp)->sd_verify_recover_ctx != NULL) {		\
1970Sstevel@tonic-gate 		crypto_cancel_ctx((sp)->sd_verify_recover_ctx);	\
1980Sstevel@tonic-gate 		(sp)->sd_verify_recover_ctx = NULL;		\
1990Sstevel@tonic-gate 	}							\
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate 
2023916Skrishna #define	CRYPTO_DECREMENT_RCTL(val) {				\
2033916Skrishna 	kproject_t *projp;					\
2043916Skrishna 	mutex_enter(&curproc->p_lock);				\
2053916Skrishna 	projp = curproc->p_task->tk_proj;			\
2063916Skrishna 	ASSERT(projp != NULL);					\
2073916Skrishna 	mutex_enter(&(projp->kpj_data.kpd_crypto_lock));	\
2083916Skrishna 	projp->kpj_data.kpd_crypto_mem -= (val);		\
2093916Skrishna 	mutex_exit(&(projp->kpj_data.kpd_crypto_lock));		\
2103916Skrishna 	curproc->p_crypto_mem -= (val);				\
2113916Skrishna 	mutex_exit(&curproc->p_lock);				\
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate /*
2150Sstevel@tonic-gate  * Module linkage.
2160Sstevel@tonic-gate  */
2170Sstevel@tonic-gate static struct cb_ops cbops = {
2180Sstevel@tonic-gate 	crypto_open,		/* cb_open */
2190Sstevel@tonic-gate 	crypto_close,		/* cb_close */
2200Sstevel@tonic-gate 	nodev,			/* cb_strategy */
2210Sstevel@tonic-gate 	nodev,			/* cb_print */
2220Sstevel@tonic-gate 	nodev,			/* cb_dump */
2230Sstevel@tonic-gate 	nodev,			/* cb_read */
2240Sstevel@tonic-gate 	nodev,			/* cb_write */
2250Sstevel@tonic-gate 	crypto_ioctl,		/* cb_ioctl */
2260Sstevel@tonic-gate 	nodev,			/* cb_devmap */
2270Sstevel@tonic-gate 	nodev,			/* cb_mmap */
2280Sstevel@tonic-gate 	nodev,			/* cb_segmap */
2290Sstevel@tonic-gate 	nochpoll,		/* cb_chpoll */
2300Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
2310Sstevel@tonic-gate 	NULL,			/* cb_streamtab */
2320Sstevel@tonic-gate 	D_MP,			/* cb_flag */
2330Sstevel@tonic-gate 	CB_REV,			/* cb_rev */
2340Sstevel@tonic-gate 	nodev,			/* cb_aread */
2350Sstevel@tonic-gate 	nodev,			/* cb_awrite */
2360Sstevel@tonic-gate };
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate static struct dev_ops devops = {
2390Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
2400Sstevel@tonic-gate 	0,			/* devo_refcnt */
2410Sstevel@tonic-gate 	crypto_getinfo,		/* devo_getinfo */
2420Sstevel@tonic-gate 	nulldev,		/* devo_identify */
2430Sstevel@tonic-gate 	nulldev,		/* devo_probe */
2440Sstevel@tonic-gate 	crypto_attach,		/* devo_attach */
2450Sstevel@tonic-gate 	crypto_detach,		/* devo_detach */
2460Sstevel@tonic-gate 	nodev,			/* devo_reset */
2470Sstevel@tonic-gate 	&cbops,			/* devo_cb_ops */
2480Sstevel@tonic-gate 	NULL,			/* devo_bus_ops */
2490Sstevel@tonic-gate 	NULL,			/* devo_power */
2500Sstevel@tonic-gate };
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate static struct modldrv modldrv = {
2530Sstevel@tonic-gate 	&mod_driverops,					/* drv_modops */
2540Sstevel@tonic-gate 	"Cryptographic Library Interface v%I%",	/* drv_linkinfo */
2550Sstevel@tonic-gate 	&devops,
2560Sstevel@tonic-gate };
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate static struct modlinkage modlinkage = {
2590Sstevel@tonic-gate 	MODREV_1,		/* ml_rev */
2600Sstevel@tonic-gate 	&modldrv,		/* ml_linkage */
2610Sstevel@tonic-gate 	NULL
2620Sstevel@tonic-gate };
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate /*
2650Sstevel@tonic-gate  * DDI entry points.
2660Sstevel@tonic-gate  */
2670Sstevel@tonic-gate int
2680Sstevel@tonic-gate _init(void)
2690Sstevel@tonic-gate {
2700Sstevel@tonic-gate 	return (mod_install(&modlinkage));
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate int
2740Sstevel@tonic-gate _fini(void)
2750Sstevel@tonic-gate {
2760Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate int
2800Sstevel@tonic-gate _info(struct modinfo *modinfop)
2810Sstevel@tonic-gate {
2820Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate /* ARGSUSED */
2860Sstevel@tonic-gate static int
2870Sstevel@tonic-gate crypto_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
2880Sstevel@tonic-gate {
2890Sstevel@tonic-gate 	switch (cmd) {
2900Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
2910Sstevel@tonic-gate 		*result = crypto_dip;
2920Sstevel@tonic-gate 		return (DDI_SUCCESS);
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
2950Sstevel@tonic-gate 		*result = (void *)0;
2960Sstevel@tonic-gate 		return (DDI_SUCCESS);
2970Sstevel@tonic-gate 	}
2980Sstevel@tonic-gate 	return (DDI_FAILURE);
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate static int
3020Sstevel@tonic-gate crypto_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3030Sstevel@tonic-gate {
3040Sstevel@tonic-gate 	if (cmd != DDI_ATTACH) {
3050Sstevel@tonic-gate 		return (DDI_FAILURE);
3060Sstevel@tonic-gate 	}
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	if (ddi_get_instance(dip) != 0) {
3090Sstevel@tonic-gate 		/* we only allow instance 0 to attach */
3100Sstevel@tonic-gate 		return (DDI_FAILURE);
3110Sstevel@tonic-gate 	}
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	crypto_session_cache = kmem_cache_create("crypto_session_cache",
3140Sstevel@tonic-gate 	    sizeof (crypto_session_data_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	if (crypto_session_cache == NULL)
3170Sstevel@tonic-gate 		return (DDI_FAILURE);
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 	/* create the minor node */
3200Sstevel@tonic-gate 	if (ddi_create_minor_node(dip, "crypto", S_IFCHR, 0,
3210Sstevel@tonic-gate 	    DDI_PSEUDO, 0) != DDI_SUCCESS) {
3220Sstevel@tonic-gate 		kmem_cache_destroy(crypto_session_cache);
3230Sstevel@tonic-gate 		crypto_session_cache = NULL;
3240Sstevel@tonic-gate 		cmn_err(CE_WARN, "crypto_attach: failed creating minor node");
3250Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
3260Sstevel@tonic-gate 		return (DDI_FAILURE);
3270Sstevel@tonic-gate 	}
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	mutex_init(&crypto_lock, NULL, MUTEX_DRIVER, NULL);
3300Sstevel@tonic-gate 	cv_init(&crypto_cv, NULL, CV_DRIVER, NULL);
3310Sstevel@tonic-gate 	crypto_dip = dip;
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	/* allocate integer space for minor numbers */
3340Sstevel@tonic-gate 	crypto_arena = vmem_create("crypto", (void *)1,
3350Sstevel@tonic-gate 	    CRYPTO_MINOR_CHUNK, 1, NULL, NULL, NULL, 0,
3360Sstevel@tonic-gate 	    VM_SLEEP | VMC_IDENTIFIER);
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	return (DDI_SUCCESS);
3390Sstevel@tonic-gate }
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate static int
3420Sstevel@tonic-gate crypto_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3430Sstevel@tonic-gate {
3440Sstevel@tonic-gate 	minor_t i;
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
3470Sstevel@tonic-gate 		return (DDI_FAILURE);
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	/* check if device is open */
3500Sstevel@tonic-gate 	mutex_enter(&crypto_lock);
3510Sstevel@tonic-gate 	for (i = 0; i < crypto_minors_table_count; i++) {
3520Sstevel@tonic-gate 		if (crypto_minors[i] != NULL) {
3530Sstevel@tonic-gate 			mutex_exit(&crypto_lock);
3540Sstevel@tonic-gate 			return (DDI_FAILURE);
3550Sstevel@tonic-gate 		}
3560Sstevel@tonic-gate 	}
3570Sstevel@tonic-gate 	mutex_exit(&crypto_lock);
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	crypto_dip = NULL;
3600Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	kmem_cache_destroy(crypto_session_cache);
3630Sstevel@tonic-gate 	crypto_session_cache = NULL;
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	kmem_free(crypto_minors,
3660Sstevel@tonic-gate 	    sizeof (crypto_minor_t *) * crypto_minors_table_count);
3670Sstevel@tonic-gate 	crypto_minors = NULL;
3680Sstevel@tonic-gate 	crypto_minors_table_count = 0;
3690Sstevel@tonic-gate 	mutex_destroy(&crypto_lock);
3700Sstevel@tonic-gate 	cv_destroy(&crypto_cv);
3710Sstevel@tonic-gate 	vmem_destroy(crypto_arena);
3720Sstevel@tonic-gate 	crypto_arena = NULL;
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	return (DDI_SUCCESS);
3750Sstevel@tonic-gate }
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate /* ARGSUSED */
3780Sstevel@tonic-gate static int
3790Sstevel@tonic-gate crypto_open(dev_t *devp, int flag, int otyp, cred_t *credp)
3800Sstevel@tonic-gate {
3810Sstevel@tonic-gate 	crypto_minor_t *cm = NULL;
3820Sstevel@tonic-gate 	minor_t mn;
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
3850Sstevel@tonic-gate 		return (ENXIO);
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	if (crypto_dip == NULL)
3880Sstevel@tonic-gate 		return (ENXIO);
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	/* exclusive opens are not supported */
3910Sstevel@tonic-gate 	if (flag & FEXCL)
3920Sstevel@tonic-gate 		return (ENOTSUP);
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	mutex_enter(&crypto_lock);
3950Sstevel@tonic-gate again:
3960Sstevel@tonic-gate 	/* grow the minors table if needed */
3970Sstevel@tonic-gate 	if (crypto_minors_count >= crypto_minors_table_count) {
3980Sstevel@tonic-gate 		crypto_minor_t **newtable;
3990Sstevel@tonic-gate 		minor_t chunk = crypto_minor_chunk;
4000Sstevel@tonic-gate 		minor_t saved_count;
4010Sstevel@tonic-gate 		size_t new_size;
4020Sstevel@tonic-gate 		ulong_t big_count;
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 		big_count = crypto_minors_count + chunk;
4050Sstevel@tonic-gate 		if (big_count > MAXMIN) {
4060Sstevel@tonic-gate 			mutex_exit(&crypto_lock);
4070Sstevel@tonic-gate 			return (ENOMEM);
4080Sstevel@tonic-gate 		}
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 		saved_count = crypto_minors_table_count;
4110Sstevel@tonic-gate 		new_size = sizeof (crypto_minor_t *) *
4120Sstevel@tonic-gate 		    (crypto_minors_table_count + chunk);
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 		mutex_exit(&crypto_lock);
4150Sstevel@tonic-gate 		newtable = kmem_zalloc(new_size, KM_SLEEP);
4160Sstevel@tonic-gate 		mutex_enter(&crypto_lock);
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 		/*
4190Sstevel@tonic-gate 		 * Check if table grew while we were sleeping.
4200Sstevel@tonic-gate 		 * The minors table never shrinks.
4210Sstevel@tonic-gate 		 */
4220Sstevel@tonic-gate 		if (crypto_minors_table_count > saved_count) {
4230Sstevel@tonic-gate 			kmem_free(newtable, new_size);
4240Sstevel@tonic-gate 			goto again;
4250Sstevel@tonic-gate 		}
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 		/* we assume that bcopy() will return if count is 0 */
4280Sstevel@tonic-gate 		bcopy(crypto_minors, newtable,
4290Sstevel@tonic-gate 		    sizeof (crypto_minor_t *) * crypto_minors_table_count);
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 		kmem_free(crypto_minors,
4320Sstevel@tonic-gate 		    sizeof (crypto_minor_t *) * crypto_minors_table_count);
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 		/* grow the minors number space */
4350Sstevel@tonic-gate 		if (crypto_minors_table_count != 0) {
4360Sstevel@tonic-gate 			(void) vmem_add(crypto_arena,
4370Sstevel@tonic-gate 			    (void *)(uintptr_t)(crypto_minors_table_count + 1),
4380Sstevel@tonic-gate 			    crypto_minor_chunk, VM_SLEEP);
4390Sstevel@tonic-gate 		}
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 		crypto_minors = newtable;
4420Sstevel@tonic-gate 		crypto_minors_table_count += chunk;
4430Sstevel@tonic-gate 	}
4440Sstevel@tonic-gate 	mutex_exit(&crypto_lock);
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	/* allocate a new minor number starting with 1 */
4470Sstevel@tonic-gate 	mn = (minor_t)(uintptr_t)vmem_alloc(crypto_arena, 1, VM_SLEEP);
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	cm = kmem_zalloc(sizeof (crypto_minor_t), KM_SLEEP);
4500Sstevel@tonic-gate 	mutex_init(&cm->cm_lock, NULL, MUTEX_DRIVER, NULL);
4510Sstevel@tonic-gate 	cv_init(&cm->cm_cv, NULL, CV_DRIVER, NULL);
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	mutex_enter(&crypto_lock);
4540Sstevel@tonic-gate 	crypto_minors[mn - 1] = cm;
4550Sstevel@tonic-gate 	crypto_minors_count++;
4560Sstevel@tonic-gate 	mutex_exit(&crypto_lock);
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	*devp = makedevice(getmajor(*devp), mn);
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	return (0);
4610Sstevel@tonic-gate }
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate /* ARGSUSED */
4640Sstevel@tonic-gate static int
4650Sstevel@tonic-gate crypto_close(dev_t dev, int flag, int otyp, cred_t *credp)
4660Sstevel@tonic-gate {
4670Sstevel@tonic-gate 	crypto_minor_t *cm = NULL;
4680Sstevel@tonic-gate 	crypto_session_data_t *sp;
4690Sstevel@tonic-gate 	minor_t mn = getminor(dev);
4700Sstevel@tonic-gate 	uint_t i;
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	mutex_enter(&crypto_lock);
4730Sstevel@tonic-gate 	if (mn > crypto_minors_table_count) {
4740Sstevel@tonic-gate 		mutex_exit(&crypto_lock);
4750Sstevel@tonic-gate 		cmn_err(CE_WARN, "crypto_close: bad minor (too big) %d", mn);
4760Sstevel@tonic-gate 		return (ENODEV);
4770Sstevel@tonic-gate 	}
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	while (((cm = crypto_minors[mn - 1]) != NULL) && (cm->cm_refcnt > 0)) {
4800Sstevel@tonic-gate 		cv_wait(&crypto_cv, &crypto_lock);
4810Sstevel@tonic-gate 	}
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	if (cm == NULL) {
4840Sstevel@tonic-gate 		mutex_exit(&crypto_lock);
4850Sstevel@tonic-gate 		cmn_err(CE_WARN, "crypto_close: duplicate close of minor %d",
4860Sstevel@tonic-gate 		    getminor(dev));
4870Sstevel@tonic-gate 		return (ENODEV);
4880Sstevel@tonic-gate 	}
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	/* take it out of the global table */
4910Sstevel@tonic-gate 	crypto_minors[mn - 1] = NULL;
4920Sstevel@tonic-gate 	crypto_minors_count--;
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	vmem_free(crypto_arena, (void *)(uintptr_t)mn, 1);
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
4970Sstevel@tonic-gate 	mutex_exit(&crypto_lock);
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	/* free all session table entries starting with 1 */
5000Sstevel@tonic-gate 	for (i = 1; i < cm->cm_session_table_count; i++) {
5010Sstevel@tonic-gate 		if (cm->cm_session_table[i] == NULL)
5020Sstevel@tonic-gate 			continue;
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 		sp = cm->cm_session_table[i];
5050Sstevel@tonic-gate 		ASSERT((sp->sd_flags & CRYPTO_SESSION_IS_BUSY) == 0);
5060Sstevel@tonic-gate 		if (sp->sd_find_init_cookie != NULL) {
5070Sstevel@tonic-gate 			(void) crypto_free_find_ctx(sp);
5080Sstevel@tonic-gate 		}
5090Sstevel@tonic-gate 		crypto_release_provider_session(cm, sp->sd_provider_session);
5100Sstevel@tonic-gate 		KCF_PROV_REFRELE(sp->sd_provider);
5110Sstevel@tonic-gate 		CRYPTO_CANCEL_ALL_CTX(sp);
5120Sstevel@tonic-gate 		mutex_destroy(&sp->sd_lock);
5130Sstevel@tonic-gate 		cv_destroy(&sp->sd_cv);
5140Sstevel@tonic-gate 		kmem_cache_free(crypto_session_cache, sp);
5150Sstevel@tonic-gate 		cm->cm_session_table[i] = NULL;
5160Sstevel@tonic-gate 	}
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 	/* free the session table */
5190Sstevel@tonic-gate 	if (cm->cm_session_table != NULL && cm->cm_session_table_count > 0)
5200Sstevel@tonic-gate 		kmem_free(cm->cm_session_table, cm->cm_session_table_count *
5210Sstevel@tonic-gate 		    sizeof (void *));
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	if (cm->cm_session_table_count != 0) {
5240Sstevel@tonic-gate 		CRYPTO_DECREMENT_RCTL(cm->cm_session_table_count *
5253916Skrishna 		    sizeof (void *));
5260Sstevel@tonic-gate 	}
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	kcf_free_provider_tab(cm->cm_provider_count,
5290Sstevel@tonic-gate 	    cm->cm_provider_array);
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	mutex_destroy(&cm->cm_lock);
5320Sstevel@tonic-gate 	cv_destroy(&cm->cm_cv);
5330Sstevel@tonic-gate 	kmem_free(cm, sizeof (crypto_minor_t));
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	return (0);
5360Sstevel@tonic-gate }
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate static crypto_minor_t *
5390Sstevel@tonic-gate crypto_hold_minor(minor_t minor)
5400Sstevel@tonic-gate {
5410Sstevel@tonic-gate 	crypto_minor_t *cm = NULL;
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 	mutex_enter(&crypto_lock);
5440Sstevel@tonic-gate 	if ((minor <= crypto_minors_table_count) &&
5450Sstevel@tonic-gate 	    ((cm = crypto_minors[minor - 1]) != NULL)) {
5460Sstevel@tonic-gate 		cm->cm_refcnt++;
5470Sstevel@tonic-gate 	}
5480Sstevel@tonic-gate 	mutex_exit(&crypto_lock);
5490Sstevel@tonic-gate 	return (cm);
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate static void
5530Sstevel@tonic-gate crypto_release_minor(crypto_minor_t *cm)
5540Sstevel@tonic-gate {
5550Sstevel@tonic-gate 	mutex_enter(&crypto_lock);
5560Sstevel@tonic-gate 	cm->cm_refcnt--;
5570Sstevel@tonic-gate 	if (cm->cm_refcnt == 0) {
5580Sstevel@tonic-gate 		cv_broadcast(&crypto_cv);
5590Sstevel@tonic-gate 	}
5600Sstevel@tonic-gate 	mutex_exit(&crypto_lock);
5610Sstevel@tonic-gate }
5620Sstevel@tonic-gate 
5634072Skrishna extern int kcf_md5_threshold;
5644072Skrishna 
5650Sstevel@tonic-gate /*
5664072Skrishna  * Build a list of functions and other information for the provider, pd.
5670Sstevel@tonic-gate  */
5680Sstevel@tonic-gate static void
5690Sstevel@tonic-gate crypto_build_function_list(crypto_function_list_t *fl, kcf_provider_desc_t *pd)
5700Sstevel@tonic-gate {
5710Sstevel@tonic-gate 	crypto_ops_t *ops;
5720Sstevel@tonic-gate 	crypto_digest_ops_t *digest_ops;
5730Sstevel@tonic-gate 	crypto_cipher_ops_t *cipher_ops;
5740Sstevel@tonic-gate 	crypto_mac_ops_t *mac_ops;
5750Sstevel@tonic-gate 	crypto_sign_ops_t *sign_ops;
5760Sstevel@tonic-gate 	crypto_verify_ops_t *verify_ops;
5770Sstevel@tonic-gate 	crypto_dual_ops_t *dual_ops;
5780Sstevel@tonic-gate 	crypto_random_number_ops_t *random_number_ops;
5790Sstevel@tonic-gate 	crypto_session_ops_t *session_ops;
5800Sstevel@tonic-gate 	crypto_object_ops_t *object_ops;
5810Sstevel@tonic-gate 	crypto_key_ops_t *key_ops;
5820Sstevel@tonic-gate 	crypto_provider_management_ops_t *provider_ops;
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 	if ((ops = pd->pd_ops_vector) == NULL)
5850Sstevel@tonic-gate 		return;
5860Sstevel@tonic-gate 
587904Smcpowers 	if ((digest_ops = ops->co_digest_ops) != NULL) {
5880Sstevel@tonic-gate 		if (digest_ops->digest_init != NULL)
5890Sstevel@tonic-gate 			fl->fl_digest_init = B_TRUE;
5900Sstevel@tonic-gate 		if (digest_ops->digest != NULL)
5910Sstevel@tonic-gate 			fl->fl_digest = B_TRUE;
5920Sstevel@tonic-gate 		if (digest_ops->digest_update != NULL)
5930Sstevel@tonic-gate 			fl->fl_digest_update = B_TRUE;
5940Sstevel@tonic-gate 		if (digest_ops->digest_key != NULL)
5950Sstevel@tonic-gate 			fl->fl_digest_key = B_TRUE;
5960Sstevel@tonic-gate 		if (digest_ops->digest_final != NULL)
5970Sstevel@tonic-gate 			fl->fl_digest_final = B_TRUE;
5980Sstevel@tonic-gate 	}
599904Smcpowers 	if ((cipher_ops = ops->co_cipher_ops) != NULL) {
6000Sstevel@tonic-gate 		if (cipher_ops->encrypt_init != NULL)
6010Sstevel@tonic-gate 			fl->fl_encrypt_init = B_TRUE;
6020Sstevel@tonic-gate 		if (cipher_ops->encrypt != NULL)
6030Sstevel@tonic-gate 			fl->fl_encrypt = B_TRUE;
6040Sstevel@tonic-gate 		if (cipher_ops->encrypt_update != NULL)
6050Sstevel@tonic-gate 			fl->fl_encrypt_update = B_TRUE;
6060Sstevel@tonic-gate 		if (cipher_ops->encrypt_final != NULL)
6070Sstevel@tonic-gate 			fl->fl_encrypt_final = B_TRUE;
6080Sstevel@tonic-gate 		if (cipher_ops->decrypt_init != NULL)
6090Sstevel@tonic-gate 			fl->fl_decrypt_init = B_TRUE;
6100Sstevel@tonic-gate 		if (cipher_ops->decrypt != NULL)
6110Sstevel@tonic-gate 			fl->fl_decrypt = B_TRUE;
6120Sstevel@tonic-gate 		if (cipher_ops->decrypt_update != NULL)
6130Sstevel@tonic-gate 			fl->fl_decrypt_update = B_TRUE;
6140Sstevel@tonic-gate 		if (cipher_ops->decrypt_final != NULL)
6150Sstevel@tonic-gate 			fl->fl_decrypt_final = B_TRUE;
6160Sstevel@tonic-gate 	}
617904Smcpowers 	if ((mac_ops = ops->co_mac_ops) != NULL) {
6180Sstevel@tonic-gate 		if (mac_ops->mac_init != NULL)
6190Sstevel@tonic-gate 			fl->fl_mac_init = B_TRUE;
6200Sstevel@tonic-gate 		if (mac_ops->mac != NULL)
6210Sstevel@tonic-gate 			fl->fl_mac = B_TRUE;
6220Sstevel@tonic-gate 		if (mac_ops->mac_update != NULL)
6230Sstevel@tonic-gate 			fl->fl_mac_update = B_TRUE;
6240Sstevel@tonic-gate 		if (mac_ops->mac_final != NULL)
6250Sstevel@tonic-gate 			fl->fl_mac_final = B_TRUE;
6260Sstevel@tonic-gate 	}
627904Smcpowers 	if ((sign_ops = ops->co_sign_ops) != NULL) {
6280Sstevel@tonic-gate 		if (sign_ops->sign_init != NULL)
6290Sstevel@tonic-gate 			fl->fl_sign_init = B_TRUE;
6300Sstevel@tonic-gate 		if (sign_ops->sign != NULL)
6310Sstevel@tonic-gate 			fl->fl_sign = B_TRUE;
6320Sstevel@tonic-gate 		if (sign_ops->sign_update != NULL)
6330Sstevel@tonic-gate 			fl->fl_sign_update = B_TRUE;
6340Sstevel@tonic-gate 		if (sign_ops->sign_final != NULL)
6350Sstevel@tonic-gate 			fl->fl_sign_final = B_TRUE;
6360Sstevel@tonic-gate 		if (sign_ops->sign_recover_init != NULL)
6370Sstevel@tonic-gate 			fl->fl_sign_recover_init = B_TRUE;
6380Sstevel@tonic-gate 		if (sign_ops->sign_recover != NULL)
6390Sstevel@tonic-gate 			fl->fl_sign_recover = B_TRUE;
6400Sstevel@tonic-gate 	}
641904Smcpowers 	if ((verify_ops = ops->co_verify_ops) != NULL) {
6420Sstevel@tonic-gate 		if (verify_ops->verify_init != NULL)
6430Sstevel@tonic-gate 			fl->fl_verify_init = B_TRUE;
6440Sstevel@tonic-gate 		if (verify_ops->verify != NULL)
6450Sstevel@tonic-gate 			fl->fl_verify = B_TRUE;
6460Sstevel@tonic-gate 		if (verify_ops->verify_update != NULL)
6470Sstevel@tonic-gate 			fl->fl_verify_update = B_TRUE;
6480Sstevel@tonic-gate 		if (verify_ops->verify_final != NULL)
6490Sstevel@tonic-gate 			fl->fl_verify_final = B_TRUE;
6500Sstevel@tonic-gate 		if (verify_ops->verify_recover_init != NULL)
6510Sstevel@tonic-gate 			fl->fl_verify_recover_init = B_TRUE;
6520Sstevel@tonic-gate 		if (verify_ops->verify_recover != NULL)
6530Sstevel@tonic-gate 			fl->fl_verify_recover = B_TRUE;
6540Sstevel@tonic-gate 	}
655904Smcpowers 	if ((dual_ops = ops->co_dual_ops) != NULL) {
6560Sstevel@tonic-gate 		if (dual_ops->digest_encrypt_update != NULL)
6570Sstevel@tonic-gate 			fl->fl_digest_encrypt_update = B_TRUE;
6580Sstevel@tonic-gate 		if (dual_ops->decrypt_digest_update != NULL)
6590Sstevel@tonic-gate 			fl->fl_decrypt_digest_update = B_TRUE;
6600Sstevel@tonic-gate 		if (dual_ops->sign_encrypt_update != NULL)
6610Sstevel@tonic-gate 			fl->fl_sign_encrypt_update = B_TRUE;
6620Sstevel@tonic-gate 		if (dual_ops->decrypt_verify_update != NULL)
6630Sstevel@tonic-gate 			fl->fl_decrypt_verify_update = B_TRUE;
6640Sstevel@tonic-gate 	}
665904Smcpowers 	if ((random_number_ops = ops->co_random_ops) != NULL) {
6660Sstevel@tonic-gate 		if (random_number_ops->seed_random != NULL)
6670Sstevel@tonic-gate 			fl->fl_seed_random = B_TRUE;
6680Sstevel@tonic-gate 		if (random_number_ops->generate_random != NULL)
6690Sstevel@tonic-gate 			fl->fl_generate_random = B_TRUE;
6700Sstevel@tonic-gate 	}
671904Smcpowers 	if ((session_ops = ops->co_session_ops) != NULL) {
6720Sstevel@tonic-gate 		if (session_ops->session_open != NULL)
6730Sstevel@tonic-gate 			fl->fl_session_open = B_TRUE;
6740Sstevel@tonic-gate 		if (session_ops->session_close != NULL)
6750Sstevel@tonic-gate 			fl->fl_session_close = B_TRUE;
6760Sstevel@tonic-gate 		if (session_ops->session_login != NULL)
6770Sstevel@tonic-gate 			fl->fl_session_login = B_TRUE;
6780Sstevel@tonic-gate 		if (session_ops->session_logout != NULL)
6790Sstevel@tonic-gate 			fl->fl_session_logout = B_TRUE;
6800Sstevel@tonic-gate 	}
681904Smcpowers 	if ((object_ops = ops->co_object_ops) != NULL) {
6820Sstevel@tonic-gate 		if (object_ops->object_create != NULL)
6830Sstevel@tonic-gate 			fl->fl_object_create = B_TRUE;
6840Sstevel@tonic-gate 		if (object_ops->object_copy != NULL)
6850Sstevel@tonic-gate 			fl->fl_object_copy = B_TRUE;
6860Sstevel@tonic-gate 		if (object_ops->object_destroy != NULL)
6870Sstevel@tonic-gate 			fl->fl_object_destroy = B_TRUE;
6880Sstevel@tonic-gate 		if (object_ops->object_get_size != NULL)
6890Sstevel@tonic-gate 			fl->fl_object_get_size = B_TRUE;
6900Sstevel@tonic-gate 		if (object_ops->object_get_attribute_value != NULL)
6910Sstevel@tonic-gate 			fl->fl_object_get_attribute_value = B_TRUE;
6920Sstevel@tonic-gate 		if (object_ops->object_set_attribute_value != NULL)
6930Sstevel@tonic-gate 			fl->fl_object_set_attribute_value = B_TRUE;
6940Sstevel@tonic-gate 		if (object_ops->object_find_init != NULL)
6950Sstevel@tonic-gate 			fl->fl_object_find_init = B_TRUE;
6960Sstevel@tonic-gate 		if (object_ops->object_find != NULL)
6970Sstevel@tonic-gate 			fl->fl_object_find = B_TRUE;
6980Sstevel@tonic-gate 		if (object_ops->object_find_final != NULL)
6990Sstevel@tonic-gate 			fl->fl_object_find_final = B_TRUE;
7000Sstevel@tonic-gate 	}
701904Smcpowers 	if ((key_ops = ops->co_key_ops) != NULL) {
7020Sstevel@tonic-gate 		if (key_ops->key_generate != NULL)
7030Sstevel@tonic-gate 			fl->fl_key_generate = B_TRUE;
7040Sstevel@tonic-gate 		if (key_ops->key_generate_pair != NULL)
7050Sstevel@tonic-gate 			fl->fl_key_generate_pair = B_TRUE;
7060Sstevel@tonic-gate 		if (key_ops->key_wrap != NULL)
7070Sstevel@tonic-gate 			fl->fl_key_wrap = B_TRUE;
7080Sstevel@tonic-gate 		if (key_ops->key_unwrap != NULL)
7090Sstevel@tonic-gate 			fl->fl_key_unwrap = B_TRUE;
7100Sstevel@tonic-gate 		if (key_ops->key_derive != NULL)
7110Sstevel@tonic-gate 			fl->fl_key_derive = B_TRUE;
7120Sstevel@tonic-gate 	}
713904Smcpowers 	if ((provider_ops = ops->co_provider_ops) != NULL) {
7140Sstevel@tonic-gate 		if (provider_ops->init_token != NULL)
7150Sstevel@tonic-gate 			fl->fl_init_token = B_TRUE;
7160Sstevel@tonic-gate 		if (provider_ops->init_pin != NULL)
7170Sstevel@tonic-gate 			fl->fl_init_pin = B_TRUE;
7180Sstevel@tonic-gate 		if (provider_ops->set_pin != NULL)
7190Sstevel@tonic-gate 			fl->fl_set_pin = B_TRUE;
7200Sstevel@tonic-gate 	}
7214072Skrishna 
7224072Skrishna 	fl->prov_is_limited = pd->pd_flags & CRYPTO_HASH_NO_UPDATE;
7234072Skrishna 	if (fl->prov_is_limited) {
7244072Skrishna 		/*
7254072Skrishna 		 * XXX - The threshold should ideally be per hash
7264072Skrishna 		 * mechanism. For now, we use the same value for all
7274072Skrishna 		 * hash mechanisms. Empirical evidence suggests this
7284072Skrishna 		 * is fine.
7294072Skrishna 		 */
7304072Skrishna 		fl->prov_hash_threshold = kcf_md5_threshold;
7314072Skrishna 		fl->prov_hash_limit = min(pd->pd_hash_limit,
7324072Skrishna 		    min(CRYPTO_MAX_BUFFER_LEN,
7334072Skrishna 		    curproc->p_task->tk_proj->kpj_data.kpd_crypto_mem_ctl));
7344072Skrishna 	}
7350Sstevel@tonic-gate }
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate /* ARGSUSED */
7380Sstevel@tonic-gate static int
7390Sstevel@tonic-gate get_function_list(dev_t dev, caddr_t arg, int mode, int *rval)
7400Sstevel@tonic-gate {
7410Sstevel@tonic-gate 	crypto_get_function_list_t get_function_list;
7420Sstevel@tonic-gate 	crypto_minor_t *cm;
7430Sstevel@tonic-gate 	crypto_provider_id_t provider_id;
7440Sstevel@tonic-gate 	crypto_function_list_t *fl;
7450Sstevel@tonic-gate 	kcf_provider_desc_t *provider;
7460Sstevel@tonic-gate 	int rv;
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
7490Sstevel@tonic-gate 		cmn_err(CE_WARN, "get_function_list: failed holding minor");
7500Sstevel@tonic-gate 		return (ENXIO);
7510Sstevel@tonic-gate 	}
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	if (copyin(arg, &get_function_list, sizeof (get_function_list)) != 0) {
7540Sstevel@tonic-gate 		crypto_release_minor(cm);
7550Sstevel@tonic-gate 		return (EFAULT);
7560Sstevel@tonic-gate 	}
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 	/* initialize provider_array */
7590Sstevel@tonic-gate 	if (cm->cm_provider_array == NULL) {
7600Sstevel@tonic-gate 		rv = crypto_get_provider_list(cm, NULL, NULL, DONT_RETURN_LIST);
7610Sstevel@tonic-gate 		if (rv != CRYPTO_SUCCESS) {
7620Sstevel@tonic-gate 			goto release_minor;
7630Sstevel@tonic-gate 		}
7640Sstevel@tonic-gate 	}
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 	provider_id = get_function_list.fl_provider_id;
7670Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
7680Sstevel@tonic-gate 	/* index must be less than count of providers */
7690Sstevel@tonic-gate 	if (provider_id >= cm->cm_provider_count) {
7700Sstevel@tonic-gate 		mutex_exit(&cm->cm_lock);
7710Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
7720Sstevel@tonic-gate 		goto release_minor;
7730Sstevel@tonic-gate 	}
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	ASSERT(cm->cm_provider_array != NULL);
7760Sstevel@tonic-gate 	provider = cm->cm_provider_array[provider_id];
7770Sstevel@tonic-gate 	mutex_exit(&cm->cm_lock);
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	fl = &get_function_list.fl_list;
7800Sstevel@tonic-gate 	bzero(fl, sizeof (crypto_function_list_t));
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 	if (provider->pd_prov_type != CRYPTO_LOGICAL_PROVIDER) {
7830Sstevel@tonic-gate 		crypto_build_function_list(fl, provider);
7840Sstevel@tonic-gate 	} else {
7850Sstevel@tonic-gate 		kcf_provider_desc_t *prev = NULL, *pd;
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 		mutex_enter(&provider->pd_lock);
7880Sstevel@tonic-gate 		while (kcf_get_next_logical_provider_member(provider,
7890Sstevel@tonic-gate 		    prev, &pd)) {
7900Sstevel@tonic-gate 			prev = pd;
7910Sstevel@tonic-gate 			crypto_build_function_list(fl, pd);
7920Sstevel@tonic-gate 			KCF_PROV_REFRELE(pd);
7930Sstevel@tonic-gate 		}
7940Sstevel@tonic-gate 		mutex_exit(&provider->pd_lock);
7950Sstevel@tonic-gate 	}
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 	rv = CRYPTO_SUCCESS;
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate release_minor:
8000Sstevel@tonic-gate 	crypto_release_minor(cm);
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 	get_function_list.fl_return_value = rv;
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 	if (copyout(&get_function_list, arg, sizeof (get_function_list)) != 0) {
8050Sstevel@tonic-gate 		return (EFAULT);
8060Sstevel@tonic-gate 	}
8070Sstevel@tonic-gate 	return (0);
8080Sstevel@tonic-gate }
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate /*
8110Sstevel@tonic-gate  * This ioctl maps a PKCS#11 mechanism string into an internal number
8120Sstevel@tonic-gate  * that is used by the kernel.  pn_internal_number is set to the
8130Sstevel@tonic-gate  * internal number.
8140Sstevel@tonic-gate  */
8150Sstevel@tonic-gate /* ARGSUSED */
8160Sstevel@tonic-gate static int
8170Sstevel@tonic-gate get_mechanism_number(dev_t dev, caddr_t arg, int mode, int *rval)
8180Sstevel@tonic-gate {
8190Sstevel@tonic-gate 	STRUCT_DECL(crypto_get_mechanism_number, get_number);
8200Sstevel@tonic-gate 	crypto_mech_type_t number;
8210Sstevel@tonic-gate 	size_t len;
8220Sstevel@tonic-gate 	char *mechanism_name;
8230Sstevel@tonic-gate 	int rv;
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	STRUCT_INIT(get_number, mode);
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(get_number), STRUCT_SIZE(get_number)) != 0)
8280Sstevel@tonic-gate 		return (EFAULT);
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 	len = STRUCT_FGET(get_number, pn_mechanism_len);
8310Sstevel@tonic-gate 	if (len == 0 || len > CRYPTO_MAX_MECH_NAME) {
8320Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
8330Sstevel@tonic-gate 		goto out;
8340Sstevel@tonic-gate 	}
8350Sstevel@tonic-gate 	mechanism_name = kmem_alloc(len, KM_SLEEP);
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	if (copyin(STRUCT_FGETP(get_number, pn_mechanism_string),
8380Sstevel@tonic-gate 	    mechanism_name, len) != 0) {
8390Sstevel@tonic-gate 		kmem_free(mechanism_name, len);
8400Sstevel@tonic-gate 		return (EFAULT);
8410Sstevel@tonic-gate 	}
8420Sstevel@tonic-gate 
8432935Skrishna 	/*
8442935Skrishna 	 * Get mechanism number from kcf. We set the load_module
8452935Skrishna 	 * flag to false since we use only hardware providers.
8462935Skrishna 	 */
8472935Skrishna 	number = crypto_mech2id_common(mechanism_name, B_FALSE);
8480Sstevel@tonic-gate 	kmem_free(mechanism_name, len);
8490Sstevel@tonic-gate 	if (number == CRYPTO_MECH_INVALID) {
8500Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
8510Sstevel@tonic-gate 		goto out;
8520Sstevel@tonic-gate 	}
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 	bcopy((char *)&number, (char *)STRUCT_FADDR(get_number,
8550Sstevel@tonic-gate 	    pn_internal_number), sizeof (number));
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	rv = CRYPTO_SUCCESS;
8580Sstevel@tonic-gate out:
8590Sstevel@tonic-gate 	STRUCT_FSET(get_number, pn_return_value, rv);
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(get_number), arg,
8620Sstevel@tonic-gate 	    STRUCT_SIZE(get_number)) != 0) {
8630Sstevel@tonic-gate 		return (EFAULT);
8640Sstevel@tonic-gate 	}
8650Sstevel@tonic-gate 	return (0);
8660Sstevel@tonic-gate }
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate /*
8690Sstevel@tonic-gate  * Side-effects:
8700Sstevel@tonic-gate  *  1. This routine stores provider descriptor pointers in an array
8710Sstevel@tonic-gate  *     and increments each descriptor's reference count.  The array
8720Sstevel@tonic-gate  *     is stored in per-minor number storage.
8730Sstevel@tonic-gate  *  2. Destroys the old array and creates a new one every time
8740Sstevel@tonic-gate  *     this routine is called.
8750Sstevel@tonic-gate  */
8760Sstevel@tonic-gate int
8770Sstevel@tonic-gate crypto_get_provider_list(crypto_minor_t *cm, uint_t *count,
8780Sstevel@tonic-gate     crypto_provider_entry_t **array, boolean_t return_slot_list)
8790Sstevel@tonic-gate {
8800Sstevel@tonic-gate 	kcf_provider_desc_t **provider_array;
8810Sstevel@tonic-gate 	crypto_provider_entry_t *p = NULL;
8820Sstevel@tonic-gate 	uint_t provider_count;
8830Sstevel@tonic-gate 	int rval;
8840Sstevel@tonic-gate 	int i;
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 	/*
8870Sstevel@tonic-gate 	 * Take snapshot of provider table returning only HW entries
8880Sstevel@tonic-gate 	 * that are in a usable state. Also returns logical provider entries.
8890Sstevel@tonic-gate 	 */
8900Sstevel@tonic-gate 	rval =  kcf_get_slot_list(&provider_count, &provider_array, B_FALSE);
8910Sstevel@tonic-gate 	if (rval != CRYPTO_SUCCESS)
8920Sstevel@tonic-gate 		return (rval);
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 	/* allocate memory before taking cm->cm_lock */
8950Sstevel@tonic-gate 	if (return_slot_list) {
8960Sstevel@tonic-gate 		if (provider_count != 0) {
8970Sstevel@tonic-gate 			p = kmem_alloc(provider_count *
8980Sstevel@tonic-gate 			    sizeof (crypto_provider_entry_t), KM_SLEEP);
8990Sstevel@tonic-gate 			for (i = 0; i < provider_count; i++) {
9000Sstevel@tonic-gate 				p[i].pe_provider_id = i;
9010Sstevel@tonic-gate 				p[i].pe_mechanism_count =
9020Sstevel@tonic-gate 				    provider_array[i]->pd_mech_list_count;
9030Sstevel@tonic-gate 			}
9040Sstevel@tonic-gate 		}
9050Sstevel@tonic-gate 		*array = p;
9060Sstevel@tonic-gate 		*count = provider_count;
9070Sstevel@tonic-gate 	}
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 	/*
9100Sstevel@tonic-gate 	 * Free existing array of providers and replace with new list.
9110Sstevel@tonic-gate 	 */
9120Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
9130Sstevel@tonic-gate 	if (cm->cm_provider_array != NULL) {
9140Sstevel@tonic-gate 		ASSERT(cm->cm_provider_count > 0);
9150Sstevel@tonic-gate 		kcf_free_provider_tab(cm->cm_provider_count,
9160Sstevel@tonic-gate 		    cm->cm_provider_array);
9170Sstevel@tonic-gate 	}
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 	cm->cm_provider_array = provider_array;
9200Sstevel@tonic-gate 	cm->cm_provider_count = provider_count;
9210Sstevel@tonic-gate 	mutex_exit(&cm->cm_lock);
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
9240Sstevel@tonic-gate }
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate /*
9270Sstevel@tonic-gate  * This ioctl returns an array of crypto_provider_entry_t entries.
9280Sstevel@tonic-gate  * This is how consumers learn which hardware providers are available.
9290Sstevel@tonic-gate  */
9300Sstevel@tonic-gate /* ARGSUSED */
9310Sstevel@tonic-gate static int
9320Sstevel@tonic-gate get_provider_list(dev_t dev, caddr_t arg, int mode, int *rval)
9330Sstevel@tonic-gate {
9340Sstevel@tonic-gate 	STRUCT_DECL(crypto_get_provider_list, get_list);
9350Sstevel@tonic-gate 	crypto_provider_entry_t *entries;
9360Sstevel@tonic-gate 	crypto_minor_t *cm;
9370Sstevel@tonic-gate 	size_t copyout_size;
9380Sstevel@tonic-gate 	uint_t req_count;
9390Sstevel@tonic-gate 	uint_t count;
9400Sstevel@tonic-gate 	ulong_t offset;
9410Sstevel@tonic-gate 	int rv;
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate 	STRUCT_INIT(get_list, mode);
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
9460Sstevel@tonic-gate 		cmn_err(CE_WARN, "get_provider_list: failed holding minor");
9470Sstevel@tonic-gate 		return (ENXIO);
9480Sstevel@tonic-gate 	}
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(get_list), STRUCT_SIZE(get_list)) != 0) {
9510Sstevel@tonic-gate 		crypto_release_minor(cm);
9520Sstevel@tonic-gate 		return (EFAULT);
9530Sstevel@tonic-gate 	}
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 	rv = crypto_get_provider_list(cm, &count, &entries, RETURN_LIST);
9560Sstevel@tonic-gate 	if (rv != CRYPTO_SUCCESS) {
9570Sstevel@tonic-gate 		crypto_release_minor(cm);
9580Sstevel@tonic-gate 		STRUCT_FSET(get_list, pl_return_value, rv);
9590Sstevel@tonic-gate 		if (copyout(STRUCT_BUF(get_list), arg,
9600Sstevel@tonic-gate 		    STRUCT_SIZE(get_list)) != 0) {
9610Sstevel@tonic-gate 			return (EFAULT);
9620Sstevel@tonic-gate 		}
9630Sstevel@tonic-gate 		return (0);
9640Sstevel@tonic-gate 	}
9650Sstevel@tonic-gate 	crypto_release_minor(cm);
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 	/* Number of slots caller thinks we have */
9680Sstevel@tonic-gate 	req_count = STRUCT_FGET(get_list, pl_count);
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 	/* Check if only requesting number of slots */
9710Sstevel@tonic-gate 	if (req_count == 0) {
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate 		STRUCT_FSET(get_list, pl_count, count);
9740Sstevel@tonic-gate 		STRUCT_FSET(get_list, pl_return_value, CRYPTO_SUCCESS);
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate 		crypto_free_provider_list(entries, count);
9770Sstevel@tonic-gate 		if (copyout(STRUCT_BUF(get_list), arg,
9780Sstevel@tonic-gate 			STRUCT_SIZE(get_list)) != 0) {
9790Sstevel@tonic-gate 			return (EFAULT);
9800Sstevel@tonic-gate 		}
9810Sstevel@tonic-gate 		return (0);
9820Sstevel@tonic-gate 	}
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 	/* check if buffer is too small */
9850Sstevel@tonic-gate 	req_count = STRUCT_FGET(get_list, pl_count);
9860Sstevel@tonic-gate 	if (count > req_count) {
9870Sstevel@tonic-gate 		STRUCT_FSET(get_list, pl_count, count);
9880Sstevel@tonic-gate 		STRUCT_FSET(get_list, pl_return_value, CRYPTO_BUFFER_TOO_SMALL);
9890Sstevel@tonic-gate 		crypto_free_provider_list(entries, count);
9900Sstevel@tonic-gate 		if (copyout(STRUCT_BUF(get_list), arg,
9910Sstevel@tonic-gate 		    STRUCT_SIZE(get_list)) != 0) {
9920Sstevel@tonic-gate 			return (EFAULT);
9930Sstevel@tonic-gate 		}
9940Sstevel@tonic-gate 		return (0);
9950Sstevel@tonic-gate 	}
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 	STRUCT_FSET(get_list, pl_count, count);
9980Sstevel@tonic-gate 	STRUCT_FSET(get_list, pl_return_value, CRYPTO_SUCCESS);
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate 	copyout_size = count * sizeof (crypto_provider_entry_t);
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate 	/* copyout the first stuff */
10030Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(get_list), arg, STRUCT_SIZE(get_list)) != 0) {
10040Sstevel@tonic-gate 		crypto_free_provider_list(entries, count);
10050Sstevel@tonic-gate 		return (EFAULT);
10060Sstevel@tonic-gate 	}
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 	if (count == 0) {
10090Sstevel@tonic-gate 		crypto_free_provider_list(entries, count);
10100Sstevel@tonic-gate 		return (0);
10110Sstevel@tonic-gate 	}
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 	/* copyout entries */
10140Sstevel@tonic-gate 	offset = (ulong_t)STRUCT_FADDR(get_list, pl_list);
10150Sstevel@tonic-gate 	offset -= (ulong_t)STRUCT_BUF(get_list);
10160Sstevel@tonic-gate 	if (copyout(entries, arg + offset, copyout_size) != 0) {
10170Sstevel@tonic-gate 		crypto_free_provider_list(entries, count);
10180Sstevel@tonic-gate 		return (EFAULT);
10190Sstevel@tonic-gate 	}
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 	crypto_free_provider_list(entries, count);
10220Sstevel@tonic-gate 	return (0);
10230Sstevel@tonic-gate }
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate static void
10260Sstevel@tonic-gate ext_to_provider_data(int mode, kcf_provider_desc_t *provider,
10270Sstevel@tonic-gate     crypto_provider_ext_info_t *ei, void *out)
10280Sstevel@tonic-gate {
10290Sstevel@tonic-gate 	STRUCT_DECL(crypto_provider_data, pd);
10300Sstevel@tonic-gate 	STRUCT_DECL(crypto_version, version);
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 	STRUCT_INIT(pd, mode);
10330Sstevel@tonic-gate 	STRUCT_INIT(version, mode);
10340Sstevel@tonic-gate 
10350Sstevel@tonic-gate 	bcopy(provider->pd_description, STRUCT_FGET(pd, pd_prov_desc),
10360Sstevel@tonic-gate 	    CRYPTO_PROVIDER_DESCR_MAX_LEN);
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate 	bcopy(ei->ei_label, STRUCT_FGET(pd, pd_label), CRYPTO_EXT_SIZE_LABEL);
10390Sstevel@tonic-gate 	bcopy(ei->ei_manufacturerID, STRUCT_FGET(pd, pd_manufacturerID),
10400Sstevel@tonic-gate 	    CRYPTO_EXT_SIZE_MANUF);
10410Sstevel@tonic-gate 	bcopy(ei->ei_model, STRUCT_FGET(pd, pd_model), CRYPTO_EXT_SIZE_MODEL);
10420Sstevel@tonic-gate 	bcopy(ei->ei_serial_number, STRUCT_FGET(pd, pd_serial_number),
10430Sstevel@tonic-gate 	    CRYPTO_EXT_SIZE_SERIAL);
10440Sstevel@tonic-gate 	/*
10450Sstevel@tonic-gate 	 * We do not support ioctls for dual-function crypto operations yet.
10460Sstevel@tonic-gate 	 * So, we clear this flag as it might have been set by a provider.
10470Sstevel@tonic-gate 	 */
10480Sstevel@tonic-gate 	ei->ei_flags &= ~CRYPTO_EXTF_DUAL_CRYPTO_OPERATIONS;
10490Sstevel@tonic-gate 
10500Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_flags, ei->ei_flags);
10510Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_max_session_count, ei->ei_max_session_count);
10520Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_session_count, (int)CRYPTO_UNAVAILABLE_INFO);
10530Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_max_rw_session_count, ei->ei_max_session_count);
10540Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_rw_session_count, (int)CRYPTO_UNAVAILABLE_INFO);
10550Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_max_pin_len, ei->ei_max_pin_len);
10560Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_min_pin_len, ei->ei_min_pin_len);
10570Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_total_public_memory, ei->ei_total_public_memory);
10580Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_free_public_memory, ei->ei_free_public_memory);
10590Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_total_private_memory, ei->ei_total_private_memory);
10600Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_free_private_memory, ei->ei_free_private_memory);
10610Sstevel@tonic-gate 	STRUCT_FSET(version, cv_major, ei->ei_hardware_version.cv_major);
10620Sstevel@tonic-gate 	STRUCT_FSET(version, cv_minor, ei->ei_hardware_version.cv_minor);
10630Sstevel@tonic-gate 	bcopy(STRUCT_BUF(version), STRUCT_FADDR(pd, pd_hardware_version),
10640Sstevel@tonic-gate 	    STRUCT_SIZE(version));
10650Sstevel@tonic-gate 	bcopy(STRUCT_BUF(version), STRUCT_FADDR(pd, pd_firmware_version),
10660Sstevel@tonic-gate 	    STRUCT_SIZE(version));
10670Sstevel@tonic-gate 	bcopy(ei->ei_time, STRUCT_FGET(pd, pd_time), CRYPTO_EXT_SIZE_TIME);
10680Sstevel@tonic-gate 	bcopy(STRUCT_BUF(pd), out, STRUCT_SIZE(pd));
10690Sstevel@tonic-gate }
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate /*
10720Sstevel@tonic-gate  * Utility routine to construct a crypto_provider_ext_info structure. Some
10730Sstevel@tonic-gate  * of the fields are constructed from information in the provider structure.
10740Sstevel@tonic-gate  * The rest of the fields have default values. We need to do this for
10750Sstevel@tonic-gate  * providers which do not support crypto_provider_management_ops routines.
10760Sstevel@tonic-gate  */
10770Sstevel@tonic-gate static void
10780Sstevel@tonic-gate fabricate_ext_info(kcf_provider_desc_t *provider,
10790Sstevel@tonic-gate     crypto_provider_ext_info_t *ei)
10800Sstevel@tonic-gate {
10810Sstevel@tonic-gate 	/* empty label */
10820Sstevel@tonic-gate 	(void) memset(ei->ei_label, ' ', CRYPTO_EXT_SIZE_LABEL);
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate 	(void) memset(ei->ei_manufacturerID, ' ', CRYPTO_EXT_SIZE_MANUF);
10850Sstevel@tonic-gate 	(void) strncpy((char *)ei->ei_manufacturerID, "Unknown", 7);
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate 	(void) memset(ei->ei_model, ' ', CRYPTO_EXT_SIZE_MODEL);
10880Sstevel@tonic-gate 	(void) strncpy((char *)ei->ei_model, "Unknown", 7);
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 	(void) memset(ei->ei_serial_number, ' ', CRYPTO_EXT_SIZE_SERIAL);
10910Sstevel@tonic-gate 	(void) strncpy((char *)ei->ei_serial_number, "Unknown", 7);
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 	if (KCF_PROV_RANDOM_OPS(provider) != NULL)
10940Sstevel@tonic-gate 		ei->ei_flags |= CRYPTO_EXTF_RNG;
10950Sstevel@tonic-gate 	if (KCF_PROV_DUAL_OPS(provider) != NULL)
10960Sstevel@tonic-gate 		ei->ei_flags |= CRYPTO_EXTF_DUAL_CRYPTO_OPERATIONS;
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 	ei->ei_max_session_count = CRYPTO_UNAVAILABLE_INFO;
10990Sstevel@tonic-gate 	ei->ei_max_pin_len = 0;
11000Sstevel@tonic-gate 	ei->ei_min_pin_len = 0;
11010Sstevel@tonic-gate 	ei->ei_total_public_memory = CRYPTO_UNAVAILABLE_INFO;
11020Sstevel@tonic-gate 	ei->ei_free_public_memory = CRYPTO_UNAVAILABLE_INFO;
11030Sstevel@tonic-gate 	ei->ei_total_private_memory = CRYPTO_UNAVAILABLE_INFO;
11040Sstevel@tonic-gate 	ei->ei_free_private_memory = CRYPTO_UNAVAILABLE_INFO;
11050Sstevel@tonic-gate 	ei->ei_hardware_version.cv_major = 1;
11060Sstevel@tonic-gate 	ei->ei_hardware_version.cv_minor = 0;
11070Sstevel@tonic-gate 	ei->ei_firmware_version.cv_major = 1;
11080Sstevel@tonic-gate 	ei->ei_firmware_version.cv_minor = 0;
11090Sstevel@tonic-gate }
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate /* ARGSUSED */
11120Sstevel@tonic-gate static int
11130Sstevel@tonic-gate get_provider_info(dev_t dev, caddr_t arg, int mode, int *rval)
11140Sstevel@tonic-gate {
11150Sstevel@tonic-gate 	STRUCT_DECL(crypto_get_provider_info, get_info);
11160Sstevel@tonic-gate 	crypto_minor_t *cm;
11170Sstevel@tonic-gate 	crypto_provider_id_t provider_id;
11180Sstevel@tonic-gate 	kcf_provider_desc_t *provider, *real_provider;
11190Sstevel@tonic-gate 	crypto_provider_ext_info_t *ext_info = NULL;
11200Sstevel@tonic-gate 	size_t need;
11210Sstevel@tonic-gate 	int error = 0;
11220Sstevel@tonic-gate 	int rv;
11230Sstevel@tonic-gate 	kcf_req_params_t params;
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate 	STRUCT_INIT(get_info, mode);
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
11280Sstevel@tonic-gate 		cmn_err(CE_WARN, "get_provider_info: failed holding minor");
11290Sstevel@tonic-gate 		return (ENXIO);
11300Sstevel@tonic-gate 	}
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(get_info), STRUCT_SIZE(get_info)) != 0) {
11330Sstevel@tonic-gate 		crypto_release_minor(cm);
11340Sstevel@tonic-gate 		return (EFAULT);
11350Sstevel@tonic-gate 	}
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate 	need = sizeof (crypto_provider_ext_info_t);
11383916Skrishna 	if ((rv = crypto_buffer_check(need)) != CRYPTO_SUCCESS) {
11390Sstevel@tonic-gate 		need = 0;
11400Sstevel@tonic-gate 		goto release_minor;
11410Sstevel@tonic-gate 	}
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate 	/* initialize provider_array */
11440Sstevel@tonic-gate 	if (cm->cm_provider_array == NULL) {
11450Sstevel@tonic-gate 		rv = crypto_get_provider_list(cm, NULL, NULL, DONT_RETURN_LIST);
11460Sstevel@tonic-gate 		if (rv != CRYPTO_SUCCESS) {
11470Sstevel@tonic-gate 			goto release_minor;
11480Sstevel@tonic-gate 		}
11490Sstevel@tonic-gate 	}
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate 	ext_info = kmem_zalloc(need, KM_SLEEP);
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 	provider_id = STRUCT_FGET(get_info, gi_provider_id);
11540Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
11550Sstevel@tonic-gate 	/* index must be less than count of providers */
11560Sstevel@tonic-gate 	if (provider_id >= cm->cm_provider_count) {
11570Sstevel@tonic-gate 		mutex_exit(&cm->cm_lock);
11580Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
11590Sstevel@tonic-gate 		goto release_minor;
11600Sstevel@tonic-gate 	}
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate 	ASSERT(cm->cm_provider_array != NULL);
11630Sstevel@tonic-gate 	provider = cm->cm_provider_array[provider_id];
11640Sstevel@tonic-gate 	KCF_PROV_REFHOLD(provider);
11650Sstevel@tonic-gate 	mutex_exit(&cm->cm_lock);
11660Sstevel@tonic-gate 
11670Sstevel@tonic-gate 	(void) kcf_get_hardware_provider_nomech(
11680Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(provider_ops), CRYPTO_PROVIDER_OFFSET(ext_info),
1169904Smcpowers 	    CHECK_RESTRICT_FALSE, provider, &real_provider);
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 	if (real_provider != NULL) {
11720Sstevel@tonic-gate 		ASSERT(real_provider == provider ||
11730Sstevel@tonic-gate 		    provider->pd_prov_type == CRYPTO_LOGICAL_PROVIDER);
11740Sstevel@tonic-gate 		KCF_WRAP_PROVMGMT_OPS_PARAMS(&params, KCF_OP_MGMT_EXTINFO,
11750Sstevel@tonic-gate 		    0, NULL, 0, NULL, 0, NULL, ext_info, provider);
11760Sstevel@tonic-gate 		rv = kcf_submit_request(real_provider, NULL, NULL, &params,
11770Sstevel@tonic-gate 		    B_FALSE);
11780Sstevel@tonic-gate 		ASSERT(rv != CRYPTO_NOT_SUPPORTED);
1179904Smcpowers 		KCF_PROV_REFRELE(real_provider);
11800Sstevel@tonic-gate 	} else {
11810Sstevel@tonic-gate 		/* do the best we can */
11820Sstevel@tonic-gate 		fabricate_ext_info(provider, ext_info);
11830Sstevel@tonic-gate 		rv = CRYPTO_SUCCESS;
11840Sstevel@tonic-gate 	}
11850Sstevel@tonic-gate 	KCF_PROV_REFRELE(provider);
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
11880Sstevel@tonic-gate 		ext_to_provider_data(mode, provider, ext_info,
11890Sstevel@tonic-gate 		    STRUCT_FADDR(get_info, gi_provider_data));
11900Sstevel@tonic-gate 	}
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate release_minor:
11930Sstevel@tonic-gate 	if (need != 0) {
11943916Skrishna 		CRYPTO_DECREMENT_RCTL(need);
11950Sstevel@tonic-gate 	}
11960Sstevel@tonic-gate 	crypto_release_minor(cm);
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate 	if (ext_info != NULL)
11990Sstevel@tonic-gate 		kmem_free(ext_info, sizeof (crypto_provider_ext_info_t));
12000Sstevel@tonic-gate 
12010Sstevel@tonic-gate 	if (error != 0)
12020Sstevel@tonic-gate 		return (error);
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 	STRUCT_FSET(get_info, gi_return_value, rv);
12050Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(get_info), arg, STRUCT_SIZE(get_info)) != 0) {
12060Sstevel@tonic-gate 		return (EFAULT);
12070Sstevel@tonic-gate 	}
12080Sstevel@tonic-gate 	return (0);
12090Sstevel@tonic-gate }
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate /*
12120Sstevel@tonic-gate  * This ioctl returns an array of crypto_mech_name_t entries.
12130Sstevel@tonic-gate  * This is how consumers learn which mechanisms are permitted
12140Sstevel@tonic-gate  * by a provider.
12150Sstevel@tonic-gate  */
12160Sstevel@tonic-gate /* ARGSUSED */
12170Sstevel@tonic-gate static int
12180Sstevel@tonic-gate get_provider_mechanisms(dev_t dev, caddr_t arg, int mode, int *rval)
12190Sstevel@tonic-gate {
12200Sstevel@tonic-gate 	STRUCT_DECL(crypto_get_provider_mechanisms, get_mechanisms);
12210Sstevel@tonic-gate 	crypto_mech_name_t *entries;
12220Sstevel@tonic-gate 	crypto_minor_t *cm;
12230Sstevel@tonic-gate 	size_t copyout_size;
12240Sstevel@tonic-gate 	uint_t req_count;
12250Sstevel@tonic-gate 	uint_t count;
12260Sstevel@tonic-gate 	ulong_t offset;
12270Sstevel@tonic-gate 	int err;
12280Sstevel@tonic-gate 
12290Sstevel@tonic-gate 	STRUCT_INIT(get_mechanisms, mode);
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
12320Sstevel@tonic-gate 		cmn_err(CE_WARN,
12330Sstevel@tonic-gate 		    "get_provider_mechanisms: failed holding minor");
12340Sstevel@tonic-gate 		return (ENXIO);
12350Sstevel@tonic-gate 	}
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(get_mechanisms),
12380Sstevel@tonic-gate 	    STRUCT_SIZE(get_mechanisms)) != 0) {
12390Sstevel@tonic-gate 		crypto_release_minor(cm);
12400Sstevel@tonic-gate 		return (EFAULT);
12410Sstevel@tonic-gate 	}
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 	/* get array of mechanisms from the core module */
12440Sstevel@tonic-gate 	if ((err = crypto_get_provider_mechanisms(cm,
12450Sstevel@tonic-gate 	    STRUCT_FGET(get_mechanisms, pm_provider_id),
12460Sstevel@tonic-gate 	    &count, &entries)) != 0) {
12470Sstevel@tonic-gate 		crypto_release_minor(cm);
12480Sstevel@tonic-gate 		STRUCT_FSET(get_mechanisms, pm_return_value, err);
12490Sstevel@tonic-gate 		if (copyout(STRUCT_BUF(get_mechanisms), arg,
12500Sstevel@tonic-gate 		    STRUCT_SIZE(get_mechanisms)) != 0) {
12510Sstevel@tonic-gate 			return (EFAULT);
12520Sstevel@tonic-gate 		}
12530Sstevel@tonic-gate 		return (0);
12540Sstevel@tonic-gate 	}
12550Sstevel@tonic-gate 	crypto_release_minor(cm);
12560Sstevel@tonic-gate 	/* Number of mechs caller thinks we have */
12570Sstevel@tonic-gate 	req_count = STRUCT_FGET(get_mechanisms, pm_count);
12580Sstevel@tonic-gate 
12590Sstevel@tonic-gate 	/* Check if caller is just requesting a count of mechanisms */
12600Sstevel@tonic-gate 	if (req_count == 0) {
12610Sstevel@tonic-gate 		STRUCT_FSET(get_mechanisms, pm_count, count);
12620Sstevel@tonic-gate 		STRUCT_FSET(get_mechanisms, pm_return_value, CRYPTO_SUCCESS);
12630Sstevel@tonic-gate 
12640Sstevel@tonic-gate 		crypto_free_mech_list(entries, count);
12650Sstevel@tonic-gate 		if (copyout(STRUCT_BUF(get_mechanisms), arg,
12660Sstevel@tonic-gate 		    STRUCT_SIZE(get_mechanisms)) != 0) {
12670Sstevel@tonic-gate 			return (EFAULT);
12680Sstevel@tonic-gate 		}
12690Sstevel@tonic-gate 		return (0);
12700Sstevel@tonic-gate 	}
12710Sstevel@tonic-gate 
12720Sstevel@tonic-gate 	/* check if buffer is too small */
12730Sstevel@tonic-gate 	if (count > req_count) {
12740Sstevel@tonic-gate 		STRUCT_FSET(get_mechanisms, pm_count, count);
12750Sstevel@tonic-gate 		STRUCT_FSET(get_mechanisms, pm_return_value,
12760Sstevel@tonic-gate 		    CRYPTO_BUFFER_TOO_SMALL);
12770Sstevel@tonic-gate 		crypto_free_mech_list(entries, count);
12780Sstevel@tonic-gate 		if (copyout(STRUCT_BUF(get_mechanisms), arg,
12790Sstevel@tonic-gate 		    STRUCT_SIZE(get_mechanisms)) != 0) {
12800Sstevel@tonic-gate 			return (EFAULT);
12810Sstevel@tonic-gate 		}
12820Sstevel@tonic-gate 		return (0);
12830Sstevel@tonic-gate 	}
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 	STRUCT_FSET(get_mechanisms, pm_count, count);
12860Sstevel@tonic-gate 	STRUCT_FSET(get_mechanisms, pm_return_value, CRYPTO_SUCCESS);
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 	copyout_size = count * sizeof (crypto_mech_name_t);
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate 	/* copyout the first stuff */
12910Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(get_mechanisms), arg,
12920Sstevel@tonic-gate 	    STRUCT_SIZE(get_mechanisms)) != 0) {
12930Sstevel@tonic-gate 		crypto_free_mech_list(entries, count);
12940Sstevel@tonic-gate 		return (EFAULT);
12950Sstevel@tonic-gate 	}
12960Sstevel@tonic-gate 
12970Sstevel@tonic-gate 	if (count == 0) {
12980Sstevel@tonic-gate 		return (0);
12990Sstevel@tonic-gate 	}
13000Sstevel@tonic-gate 
13010Sstevel@tonic-gate 	/* copyout entries */
13020Sstevel@tonic-gate 	offset = (ulong_t)STRUCT_FADDR(get_mechanisms, pm_list);
13030Sstevel@tonic-gate 	offset -= (ulong_t)STRUCT_BUF(get_mechanisms);
13040Sstevel@tonic-gate 	if (copyout(entries, arg + offset, copyout_size) != 0) {
13050Sstevel@tonic-gate 		crypto_free_mech_list(entries, count);
13060Sstevel@tonic-gate 		return (EFAULT);
13070Sstevel@tonic-gate 	}
13080Sstevel@tonic-gate 
13090Sstevel@tonic-gate 	crypto_free_mech_list(entries, count);
13100Sstevel@tonic-gate 	return (0);
13110Sstevel@tonic-gate }
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate /*
13140Sstevel@tonic-gate  * This ioctl returns information about a provider's mechanism.
13150Sstevel@tonic-gate  */
13160Sstevel@tonic-gate /* ARGSUSED */
13170Sstevel@tonic-gate static int
13180Sstevel@tonic-gate get_provider_mechanism_info(dev_t dev, caddr_t arg, int mode, int *rval)
13190Sstevel@tonic-gate {
13200Sstevel@tonic-gate 	crypto_get_provider_mechanism_info_t mechanism_info;
13210Sstevel@tonic-gate 	crypto_minor_t *cm;
13220Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
13230Sstevel@tonic-gate 	crypto_mech_info_t *mi = NULL;
13240Sstevel@tonic-gate 	int rv = CRYPTO_SUCCESS;
13250Sstevel@tonic-gate 	int i;
13260Sstevel@tonic-gate 
13270Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
13280Sstevel@tonic-gate 		cmn_err(CE_WARN,
13290Sstevel@tonic-gate 		    "get_provider_mechanism_info: failed holding minor");
13300Sstevel@tonic-gate 		return (ENXIO);
13310Sstevel@tonic-gate 	}
13320Sstevel@tonic-gate 
13330Sstevel@tonic-gate 	if (copyin(arg, &mechanism_info, sizeof (mechanism_info)) != 0) {
13340Sstevel@tonic-gate 		crypto_release_minor(cm);
13350Sstevel@tonic-gate 		return (EFAULT);
13360Sstevel@tonic-gate 	}
13370Sstevel@tonic-gate 
13380Sstevel@tonic-gate 	/* initialize provider table */
13390Sstevel@tonic-gate 	if (cm->cm_provider_array == NULL) {
13400Sstevel@tonic-gate 		rv = crypto_get_provider_list(cm, NULL, NULL, DONT_RETURN_LIST);
13410Sstevel@tonic-gate 		if (rv != CRYPTO_SUCCESS) {
13420Sstevel@tonic-gate 			mutex_enter(&cm->cm_lock);
13430Sstevel@tonic-gate 			goto fail;
13440Sstevel@tonic-gate 		}
13450Sstevel@tonic-gate 	}
13460Sstevel@tonic-gate 
13470Sstevel@tonic-gate 	/*
13480Sstevel@tonic-gate 	 * Provider ID must be less than the count of providers
13490Sstevel@tonic-gate 	 * obtained by calling get_provider_list().
13500Sstevel@tonic-gate 	 */
13510Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
13520Sstevel@tonic-gate 	if (mechanism_info.mi_provider_id >= cm->cm_provider_count) {
13530Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
13540Sstevel@tonic-gate 		goto fail;
13550Sstevel@tonic-gate 	}
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate 	pd = cm->cm_provider_array[mechanism_info.mi_provider_id];
13580Sstevel@tonic-gate 
13590Sstevel@tonic-gate 	for (i = 0; i < pd->pd_mech_list_count; i++) {
13600Sstevel@tonic-gate 		if (strncmp(pd->pd_mechanisms[i].cm_mech_name,
13610Sstevel@tonic-gate 		    mechanism_info.mi_mechanism_name,
13620Sstevel@tonic-gate 		    CRYPTO_MAX_MECH_NAME) == 0) {
13630Sstevel@tonic-gate 			mi = &pd->pd_mechanisms[i];
13640Sstevel@tonic-gate 		}
13650Sstevel@tonic-gate 	}
13660Sstevel@tonic-gate 
13670Sstevel@tonic-gate 	if (mi == NULL) {
13680Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
13690Sstevel@tonic-gate 		goto fail;
13700Sstevel@tonic-gate 	}
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate 	mechanism_info.mi_min_key_size = mi->cm_min_key_length;
13730Sstevel@tonic-gate 	mechanism_info.mi_max_key_size = mi->cm_max_key_length;
13740Sstevel@tonic-gate 	mechanism_info.mi_flags = mi->cm_func_group_mask;
13750Sstevel@tonic-gate 
13760Sstevel@tonic-gate fail:
13770Sstevel@tonic-gate 	mutex_exit(&cm->cm_lock);
13780Sstevel@tonic-gate 	crypto_release_minor(cm);
13790Sstevel@tonic-gate 	mechanism_info.mi_return_value = rv;
13800Sstevel@tonic-gate 	if (copyout(&mechanism_info, arg, sizeof (mechanism_info)) != 0) {
13810Sstevel@tonic-gate 		return (EFAULT);
13820Sstevel@tonic-gate 	}
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate 	return (0);
13850Sstevel@tonic-gate }
13860Sstevel@tonic-gate 
13870Sstevel@tonic-gate /*
13880Sstevel@tonic-gate  * Every open of /dev/crypto multiplexes all PKCS#11 sessions across
13890Sstevel@tonic-gate  * a single session to each provider. Calls to open and close session
13900Sstevel@tonic-gate  * are not made to providers that do not support sessions. For these
13910Sstevel@tonic-gate  * providers, a session number of 0 is passed during subsequent operations,
13920Sstevel@tonic-gate  * and it is ignored by the provider.
13930Sstevel@tonic-gate  */
13940Sstevel@tonic-gate static int
13950Sstevel@tonic-gate crypto_get_provider_session(crypto_minor_t *cm,
13960Sstevel@tonic-gate     crypto_provider_id_t provider_index, crypto_provider_session_t **output_ps)
13970Sstevel@tonic-gate {
13980Sstevel@tonic-gate 	kcf_provider_desc_t *pd, *real_provider;
13990Sstevel@tonic-gate 	kcf_req_params_t params;
14000Sstevel@tonic-gate 	crypto_provider_session_t *ps, *new_ps;
14010Sstevel@tonic-gate 	crypto_session_id_t provider_session_id = 0;
14020Sstevel@tonic-gate 	int rv;
14030Sstevel@tonic-gate 
14040Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cm->cm_lock));
14050Sstevel@tonic-gate 
14060Sstevel@tonic-gate 	/* pd may be a logical provider */
14070Sstevel@tonic-gate 	pd = cm->cm_provider_array[provider_index];
14080Sstevel@tonic-gate 
14090Sstevel@tonic-gate again:
14100Sstevel@tonic-gate 	/*
14110Sstevel@tonic-gate 	 * Check if there is already a session to the provider.
14120Sstevel@tonic-gate 	 * Sessions may be to a logical provider or a real provider.
14130Sstevel@tonic-gate 	 */
14140Sstevel@tonic-gate 	for (ps = cm->cm_provider_session; ps != NULL; ps = ps->ps_next) {
14150Sstevel@tonic-gate 		if (ps->ps_provider == pd)
14160Sstevel@tonic-gate 			break;
14170Sstevel@tonic-gate 	}
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate 	/* found existing session */
14200Sstevel@tonic-gate 	if (ps != NULL) {
14210Sstevel@tonic-gate 		ps->ps_refcnt++;
14220Sstevel@tonic-gate 		*output_ps = ps;
14230Sstevel@tonic-gate 		return (CRYPTO_SUCCESS);
14240Sstevel@tonic-gate 	}
14250Sstevel@tonic-gate 	mutex_exit(&cm->cm_lock);
14260Sstevel@tonic-gate 
14270Sstevel@tonic-gate 	/* find a hardware provider that supports session ops */
14280Sstevel@tonic-gate 	(void) kcf_get_hardware_provider_nomech(CRYPTO_OPS_OFFSET(session_ops),
1429904Smcpowers 	    CRYPTO_SESSION_OFFSET(session_open), CHECK_RESTRICT_FALSE,
1430904Smcpowers 	    pd, &real_provider);
14310Sstevel@tonic-gate 
14320Sstevel@tonic-gate 	if (real_provider != NULL) {
14330Sstevel@tonic-gate 		ASSERT(real_provider == pd ||
14340Sstevel@tonic-gate 		    pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER);
14350Sstevel@tonic-gate 		/* open session to provider */
14360Sstevel@tonic-gate 		KCF_WRAP_SESSION_OPS_PARAMS(&params, KCF_OP_SESSION_OPEN,
14370Sstevel@tonic-gate 		    &provider_session_id, 0, CRYPTO_USER, NULL, 0, pd);
14380Sstevel@tonic-gate 		rv = kcf_submit_request(real_provider, NULL, NULL, &params,
14390Sstevel@tonic-gate 		    B_FALSE);
14400Sstevel@tonic-gate 		if (rv != CRYPTO_SUCCESS) {
14410Sstevel@tonic-gate 			mutex_enter(&cm->cm_lock);
1442904Smcpowers 			KCF_PROV_REFRELE(real_provider);
14430Sstevel@tonic-gate 			return (rv);
14440Sstevel@tonic-gate 		}
14450Sstevel@tonic-gate 	}
14460Sstevel@tonic-gate 
14470Sstevel@tonic-gate 	/* allocate crypto_provider_session structure */
14480Sstevel@tonic-gate 	new_ps = kmem_zalloc(sizeof (crypto_provider_session_t), KM_SLEEP);
14490Sstevel@tonic-gate 
14500Sstevel@tonic-gate 	/*
14510Sstevel@tonic-gate 	 * Check if someone opened a session to the provider
14520Sstevel@tonic-gate 	 * while we dropped the lock.
14530Sstevel@tonic-gate 	 */
14540Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
14550Sstevel@tonic-gate 	for (ps = cm->cm_provider_session; ps != NULL; ps = ps->ps_next) {
14560Sstevel@tonic-gate 		if (ps->ps_provider == pd) {
14570Sstevel@tonic-gate 			mutex_exit(&cm->cm_lock);
14580Sstevel@tonic-gate 			kmem_free(new_ps, sizeof (crypto_provider_session_t));
14590Sstevel@tonic-gate 			if (real_provider != NULL) {
14600Sstevel@tonic-gate 				KCF_WRAP_SESSION_OPS_PARAMS(&params,
14610Sstevel@tonic-gate 				    KCF_OP_SESSION_CLOSE, NULL,
14620Sstevel@tonic-gate 				    provider_session_id, CRYPTO_USER, NULL, 0,
14630Sstevel@tonic-gate 				    pd);
14640Sstevel@tonic-gate 				(void) kcf_submit_request(real_provider, NULL,
14650Sstevel@tonic-gate 				    NULL, &params, B_FALSE);
1466904Smcpowers 				KCF_PROV_REFRELE(real_provider);
14670Sstevel@tonic-gate 			}
14680Sstevel@tonic-gate 			mutex_enter(&cm->cm_lock);
14690Sstevel@tonic-gate 			goto again;
14700Sstevel@tonic-gate 
14710Sstevel@tonic-gate 		}
14720Sstevel@tonic-gate 	}
14730Sstevel@tonic-gate 
14740Sstevel@tonic-gate 	/* increment refcnt and attach to crypto_minor structure */
14750Sstevel@tonic-gate 	new_ps->ps_session = provider_session_id;
14760Sstevel@tonic-gate 	new_ps->ps_refcnt = 1;
14770Sstevel@tonic-gate 	KCF_PROV_REFHOLD(pd);
14780Sstevel@tonic-gate 	new_ps->ps_provider = pd;
14790Sstevel@tonic-gate 	if (real_provider != NULL) {
14800Sstevel@tonic-gate 		new_ps->ps_real_provider = real_provider;
14810Sstevel@tonic-gate 	}
14820Sstevel@tonic-gate 	new_ps->ps_next = cm->cm_provider_session;
14830Sstevel@tonic-gate 	cm->cm_provider_session = new_ps;
14840Sstevel@tonic-gate 
14850Sstevel@tonic-gate 	*output_ps = new_ps;
14860Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
14870Sstevel@tonic-gate }
14880Sstevel@tonic-gate 
14890Sstevel@tonic-gate /*
14900Sstevel@tonic-gate  * Release a provider session.
14910Sstevel@tonic-gate  * If the reference count goes to zero, then close the session
14920Sstevel@tonic-gate  * to the provider.
14930Sstevel@tonic-gate  */
14940Sstevel@tonic-gate static void
14950Sstevel@tonic-gate crypto_release_provider_session(crypto_minor_t *cm,
14960Sstevel@tonic-gate     crypto_provider_session_t *provider_session)
14970Sstevel@tonic-gate {
14980Sstevel@tonic-gate 	kcf_req_params_t params;
14990Sstevel@tonic-gate 	crypto_provider_session_t *ps = NULL, **prev;
15000Sstevel@tonic-gate 
15010Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cm->cm_lock));
15020Sstevel@tonic-gate 
15030Sstevel@tonic-gate 	/* verify that provider_session is valid */
15040Sstevel@tonic-gate 	for (ps = cm->cm_provider_session, prev = &cm->cm_provider_session;
15050Sstevel@tonic-gate 	    ps != NULL; prev = &ps->ps_next, ps = ps->ps_next) {
15060Sstevel@tonic-gate 		if (ps == provider_session) {
15070Sstevel@tonic-gate 			break;
15080Sstevel@tonic-gate 		}
15090Sstevel@tonic-gate 	}
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate 	if (ps == NULL)
15120Sstevel@tonic-gate 		return;
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate 	ps->ps_refcnt--;
15150Sstevel@tonic-gate 
15160Sstevel@tonic-gate 	if (ps->ps_refcnt > 0)
15170Sstevel@tonic-gate 		return;
15180Sstevel@tonic-gate 
15190Sstevel@tonic-gate 	if (ps->ps_real_provider != NULL) {
15200Sstevel@tonic-gate 		/* close session with provider */
15210Sstevel@tonic-gate 		KCF_WRAP_SESSION_OPS_PARAMS(&params, KCF_OP_SESSION_CLOSE, NULL,
15220Sstevel@tonic-gate 		    ps->ps_session, CRYPTO_USER, NULL, 0, ps->ps_provider);
15230Sstevel@tonic-gate 		(void) kcf_submit_request(ps->ps_real_provider,
15240Sstevel@tonic-gate 		    NULL, NULL, &params, B_FALSE);
15250Sstevel@tonic-gate 		KCF_PROV_REFRELE(ps->ps_real_provider);
15260Sstevel@tonic-gate 	}
15270Sstevel@tonic-gate 	KCF_PROV_REFRELE(ps->ps_provider);
15280Sstevel@tonic-gate 	*prev = ps->ps_next;
15290Sstevel@tonic-gate 	kmem_free(ps, sizeof (*ps));
15300Sstevel@tonic-gate }
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate static int
15330Sstevel@tonic-gate grow_session_table(crypto_minor_t *cm)
15340Sstevel@tonic-gate {
15350Sstevel@tonic-gate 	crypto_session_data_t **session_table;
15360Sstevel@tonic-gate 	crypto_session_data_t **new;
15370Sstevel@tonic-gate 	uint_t session_table_count;
15380Sstevel@tonic-gate 	uint_t need;
15390Sstevel@tonic-gate 	size_t current_allocation;
15400Sstevel@tonic-gate 	size_t new_allocation;
15413916Skrishna 	int rv;
15420Sstevel@tonic-gate 
15430Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cm->cm_lock));
15440Sstevel@tonic-gate 
15450Sstevel@tonic-gate 	session_table_count = cm->cm_session_table_count;
15460Sstevel@tonic-gate 	session_table = cm->cm_session_table;
15470Sstevel@tonic-gate 	need = session_table_count + CRYPTO_SESSION_CHUNK;
15480Sstevel@tonic-gate 
15490Sstevel@tonic-gate 	current_allocation = session_table_count * sizeof (void *);
15500Sstevel@tonic-gate 	new_allocation = need * sizeof (void *);
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate 	/*
15530Sstevel@tonic-gate 	 * Memory needed to grow the session table is checked
15540Sstevel@tonic-gate 	 * against the project.max-crypto-memory resource control.
15550Sstevel@tonic-gate 	 */
15563916Skrishna 	if ((rv = crypto_buffer_check(new_allocation - current_allocation)) !=
15573916Skrishna 	    CRYPTO_SUCCESS) {
15583916Skrishna 		return (rv);
15593916Skrishna 	}
15600Sstevel@tonic-gate 
15610Sstevel@tonic-gate 	/* drop lock while we allocate memory */
15620Sstevel@tonic-gate 	mutex_exit(&cm->cm_lock);
15630Sstevel@tonic-gate 	new = kmem_zalloc(new_allocation, KM_SLEEP);
15640Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
15650Sstevel@tonic-gate 
15660Sstevel@tonic-gate 	/* check if another thread increased the table size */
15670Sstevel@tonic-gate 	if (session_table_count != cm->cm_session_table_count) {
15680Sstevel@tonic-gate 		kmem_free(new, new_allocation);
15690Sstevel@tonic-gate 		return (CRYPTO_SUCCESS);
15700Sstevel@tonic-gate 	}
15710Sstevel@tonic-gate 
15720Sstevel@tonic-gate 	bcopy(session_table, new, current_allocation);
15730Sstevel@tonic-gate 	kmem_free(session_table, current_allocation);
15740Sstevel@tonic-gate 	cm->cm_session_table = new;
15750Sstevel@tonic-gate 	cm->cm_session_table_count += CRYPTO_SESSION_CHUNK;
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
15780Sstevel@tonic-gate }
15790Sstevel@tonic-gate 
15800Sstevel@tonic-gate /*
15810Sstevel@tonic-gate  * Find unused entry in session table and return it's index.
15820Sstevel@tonic-gate  * Initialize session table entry.
15830Sstevel@tonic-gate  */
15840Sstevel@tonic-gate /* ARGSUSED */
15850Sstevel@tonic-gate static int
15860Sstevel@tonic-gate crypto_open_session(dev_t dev, uint_t flags, crypto_session_id_t *session_index,
15870Sstevel@tonic-gate     crypto_provider_id_t provider_id)
15880Sstevel@tonic-gate {
15890Sstevel@tonic-gate 	crypto_session_data_t **session_table;
15900Sstevel@tonic-gate 	crypto_session_data_t *sp;
15910Sstevel@tonic-gate 	crypto_minor_t *cm;
15920Sstevel@tonic-gate 	uint_t session_table_count;
15930Sstevel@tonic-gate 	uint_t i;
15940Sstevel@tonic-gate 	int rv;
15950Sstevel@tonic-gate 	crypto_provider_session_t *ps;
15960Sstevel@tonic-gate 	kcf_provider_desc_t *provider;
15970Sstevel@tonic-gate 
15980Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
15990Sstevel@tonic-gate 		cmn_err(CE_WARN, "crypto_open_session: failed holding minor");
16000Sstevel@tonic-gate 		return (CRYPTO_FAILED);
16010Sstevel@tonic-gate 	}
16020Sstevel@tonic-gate 
16030Sstevel@tonic-gate 	/* initialize provider_array */
16040Sstevel@tonic-gate 	if (cm->cm_provider_array == NULL) {
16050Sstevel@tonic-gate 		rv = crypto_get_provider_list(cm, NULL, NULL, DONT_RETURN_LIST);
16060Sstevel@tonic-gate 		if (rv != 0) {
16070Sstevel@tonic-gate 			crypto_release_minor(cm);
16080Sstevel@tonic-gate 			return (rv);
16090Sstevel@tonic-gate 		}
16100Sstevel@tonic-gate 	}
16110Sstevel@tonic-gate 
16120Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
16130Sstevel@tonic-gate 	/* index must be less than count of providers */
16140Sstevel@tonic-gate 	if (provider_id >= cm->cm_provider_count) {
16150Sstevel@tonic-gate 		mutex_exit(&cm->cm_lock);
16160Sstevel@tonic-gate 		crypto_release_minor(cm);
16170Sstevel@tonic-gate 		return (CRYPTO_INVALID_PROVIDER_ID);
16180Sstevel@tonic-gate 	}
16190Sstevel@tonic-gate 	ASSERT(cm->cm_provider_array != NULL);
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate 	rv = crypto_get_provider_session(cm, provider_id, &ps);
16220Sstevel@tonic-gate 	if (rv != CRYPTO_SUCCESS) {
16230Sstevel@tonic-gate 		mutex_exit(&cm->cm_lock);
16240Sstevel@tonic-gate 		crypto_release_minor(cm);
16250Sstevel@tonic-gate 		return (rv);
16260Sstevel@tonic-gate 	}
16270Sstevel@tonic-gate 	provider = cm->cm_provider_array[provider_id];
16280Sstevel@tonic-gate 
16290Sstevel@tonic-gate again:
16300Sstevel@tonic-gate 	session_table_count = cm->cm_session_table_count;
16310Sstevel@tonic-gate 	session_table = cm->cm_session_table;
16320Sstevel@tonic-gate 
16330Sstevel@tonic-gate 	/* session handles start with 1 */
16340Sstevel@tonic-gate 	for (i = 1; i < session_table_count; i++) {
16350Sstevel@tonic-gate 		if (session_table[i] == NULL)
16360Sstevel@tonic-gate 			break;
16370Sstevel@tonic-gate 	}
16380Sstevel@tonic-gate 
16390Sstevel@tonic-gate 	if (i == session_table_count || session_table_count == 0) {
16400Sstevel@tonic-gate 		if ((rv = grow_session_table(cm)) != CRYPTO_SUCCESS) {
16410Sstevel@tonic-gate 			crypto_release_provider_session(cm, ps);
16420Sstevel@tonic-gate 			mutex_exit(&cm->cm_lock);
16430Sstevel@tonic-gate 			crypto_release_minor(cm);
16440Sstevel@tonic-gate 			return (rv);
16450Sstevel@tonic-gate 		}
16460Sstevel@tonic-gate 		goto again;
16470Sstevel@tonic-gate 	}
16480Sstevel@tonic-gate 
16490Sstevel@tonic-gate 	sp = kmem_cache_alloc(crypto_session_cache, KM_SLEEP);
16500Sstevel@tonic-gate 	sp->sd_flags = 0;
16510Sstevel@tonic-gate 	sp->sd_find_init_cookie = NULL;
16520Sstevel@tonic-gate 	sp->sd_digest_ctx = NULL;
16530Sstevel@tonic-gate 	sp->sd_encr_ctx = NULL;
16540Sstevel@tonic-gate 	sp->sd_decr_ctx = NULL;
16550Sstevel@tonic-gate 	sp->sd_sign_ctx = NULL;
16560Sstevel@tonic-gate 	sp->sd_verify_ctx = NULL;
16570Sstevel@tonic-gate 	sp->sd_sign_recover_ctx = NULL;
16580Sstevel@tonic-gate 	sp->sd_verify_recover_ctx = NULL;
16590Sstevel@tonic-gate 	mutex_init(&sp->sd_lock, NULL, MUTEX_DRIVER, NULL);
16600Sstevel@tonic-gate 	cv_init(&sp->sd_cv, NULL, CV_DRIVER, NULL);
16610Sstevel@tonic-gate 	KCF_PROV_REFHOLD(provider);
16620Sstevel@tonic-gate 	sp->sd_provider = provider;
16630Sstevel@tonic-gate 	sp->sd_provider_session = ps;
16640Sstevel@tonic-gate 	cm->cm_session_table[i] = sp;
16650Sstevel@tonic-gate 	mutex_exit(&cm->cm_lock);
16660Sstevel@tonic-gate 	crypto_release_minor(cm);
16670Sstevel@tonic-gate 	*session_index = i;
16680Sstevel@tonic-gate 
16690Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
16700Sstevel@tonic-gate }
16710Sstevel@tonic-gate 
16720Sstevel@tonic-gate /*
16730Sstevel@tonic-gate  * Close a session.
16740Sstevel@tonic-gate  */
16750Sstevel@tonic-gate static int
16760Sstevel@tonic-gate crypto_close_session(dev_t dev, crypto_session_id_t session_index)
16770Sstevel@tonic-gate {
16780Sstevel@tonic-gate 	crypto_session_data_t **session_table;
16790Sstevel@tonic-gate 	crypto_session_data_t *sp;
16800Sstevel@tonic-gate 	crypto_minor_t *cm;
16810Sstevel@tonic-gate 
16820Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
16830Sstevel@tonic-gate 		cmn_err(CE_WARN, "crypto_close_session: failed holding minor");
16840Sstevel@tonic-gate 		return (CRYPTO_FAILED);
16850Sstevel@tonic-gate 	}
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
16880Sstevel@tonic-gate 	session_table = cm->cm_session_table;
16890Sstevel@tonic-gate 
16900Sstevel@tonic-gate 	if ((session_index) == 0 ||
16910Sstevel@tonic-gate 	    (session_index >= cm->cm_session_table_count)) {
16920Sstevel@tonic-gate 		mutex_exit(&cm->cm_lock);
16930Sstevel@tonic-gate 		crypto_release_minor(cm);
16940Sstevel@tonic-gate 		return (CRYPTO_SESSION_HANDLE_INVALID);
16950Sstevel@tonic-gate 	}
16960Sstevel@tonic-gate 
16970Sstevel@tonic-gate 	sp = session_table[session_index];
16980Sstevel@tonic-gate 	if (sp == NULL) {
16990Sstevel@tonic-gate 		mutex_exit(&cm->cm_lock);
17000Sstevel@tonic-gate 		crypto_release_minor(cm);
17010Sstevel@tonic-gate 		return (CRYPTO_SESSION_HANDLE_INVALID);
17020Sstevel@tonic-gate 	}
17030Sstevel@tonic-gate 	/*
17040Sstevel@tonic-gate 	 * If session is in use, free it when the thread
17050Sstevel@tonic-gate 	 * finishes with the session.
17060Sstevel@tonic-gate 	 */
17070Sstevel@tonic-gate 	mutex_enter(&sp->sd_lock);
17080Sstevel@tonic-gate 	if (sp->sd_flags & CRYPTO_SESSION_IS_BUSY) {
17090Sstevel@tonic-gate 		sp->sd_flags |= CRYPTO_SESSION_IS_CLOSED;
17100Sstevel@tonic-gate 		mutex_exit(&sp->sd_lock);
17110Sstevel@tonic-gate 	} else {
17120Sstevel@tonic-gate 		if (sp->sd_find_init_cookie != NULL) {
17130Sstevel@tonic-gate 			(void) crypto_free_find_ctx(sp);
17140Sstevel@tonic-gate 		}
17150Sstevel@tonic-gate 
17160Sstevel@tonic-gate 		crypto_release_provider_session(cm, sp->sd_provider_session);
17170Sstevel@tonic-gate 		KCF_PROV_REFRELE(sp->sd_provider);
17180Sstevel@tonic-gate 		CRYPTO_CANCEL_ALL_CTX(sp);
17190Sstevel@tonic-gate 		mutex_destroy(&sp->sd_lock);
17200Sstevel@tonic-gate 		cv_destroy(&sp->sd_cv);
17210Sstevel@tonic-gate 		kmem_cache_free(crypto_session_cache, sp);
17220Sstevel@tonic-gate 		session_table[session_index] = NULL;
17230Sstevel@tonic-gate 	}
17240Sstevel@tonic-gate 
17250Sstevel@tonic-gate 	mutex_exit(&cm->cm_lock);
17260Sstevel@tonic-gate 	crypto_release_minor(cm);
17270Sstevel@tonic-gate 
17280Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
17290Sstevel@tonic-gate }
17300Sstevel@tonic-gate 
17310Sstevel@tonic-gate /*
17320Sstevel@tonic-gate  * This ioctl opens a session and returns the session ID in os_session.
17330Sstevel@tonic-gate  */
17340Sstevel@tonic-gate /* ARGSUSED */
17350Sstevel@tonic-gate static int
17360Sstevel@tonic-gate open_session(dev_t dev, caddr_t arg, int mode, int *rval)
17370Sstevel@tonic-gate {
17380Sstevel@tonic-gate 	crypto_open_session_t open_session;
17390Sstevel@tonic-gate 	crypto_session_id_t session;
17400Sstevel@tonic-gate 	int rv;
17410Sstevel@tonic-gate 
17420Sstevel@tonic-gate 	if (copyin(arg, &open_session, sizeof (open_session)) != 0)
17430Sstevel@tonic-gate 		return (EFAULT);
17440Sstevel@tonic-gate 
17450Sstevel@tonic-gate 	rv = crypto_open_session(dev, open_session.os_flags,
17460Sstevel@tonic-gate 	    &session, open_session.os_provider_id);
17470Sstevel@tonic-gate 	if (rv != CRYPTO_SUCCESS) {
17480Sstevel@tonic-gate 		open_session.os_return_value = rv;
17490Sstevel@tonic-gate 		if (copyout(&open_session, arg, sizeof (open_session)) != 0) {
17500Sstevel@tonic-gate 			return (EFAULT);
17510Sstevel@tonic-gate 		}
17520Sstevel@tonic-gate 		return (0);
17530Sstevel@tonic-gate 	}
17540Sstevel@tonic-gate 
17550Sstevel@tonic-gate 	open_session.os_session = session;
17560Sstevel@tonic-gate 	open_session.os_return_value = CRYPTO_SUCCESS;
17570Sstevel@tonic-gate 
17580Sstevel@tonic-gate 	if (copyout(&open_session, arg, sizeof (open_session)) != 0) {
17590Sstevel@tonic-gate 		return (EFAULT);
17600Sstevel@tonic-gate 	}
17610Sstevel@tonic-gate 	return (0);
17620Sstevel@tonic-gate }
17630Sstevel@tonic-gate 
17640Sstevel@tonic-gate /*
17650Sstevel@tonic-gate  * This ioctl closes a session.
17660Sstevel@tonic-gate  */
17670Sstevel@tonic-gate /* ARGSUSED */
17680Sstevel@tonic-gate static int
17690Sstevel@tonic-gate close_session(dev_t dev, caddr_t arg, int mode, int *rval)
17700Sstevel@tonic-gate {
17710Sstevel@tonic-gate 	crypto_close_session_t close_session;
17720Sstevel@tonic-gate 	int rv;
17730Sstevel@tonic-gate 
17740Sstevel@tonic-gate 	if (copyin(arg, &close_session, sizeof (close_session)) != 0)
17750Sstevel@tonic-gate 		return (EFAULT);
17760Sstevel@tonic-gate 
17770Sstevel@tonic-gate 	rv = crypto_close_session(dev, close_session.cs_session);
1778904Smcpowers 	close_session.cs_return_value = rv;
17790Sstevel@tonic-gate 	if (copyout(&close_session, arg, sizeof (close_session)) != 0) {
17800Sstevel@tonic-gate 		return (EFAULT);
17810Sstevel@tonic-gate 	}
17820Sstevel@tonic-gate 	return (0);
17830Sstevel@tonic-gate }
17840Sstevel@tonic-gate 
17850Sstevel@tonic-gate /*
17860Sstevel@tonic-gate  * Copy data model dependent mechanism structure into a kernel mechanism
17870Sstevel@tonic-gate  * structure.  Allocate param storage if necessary.
17880Sstevel@tonic-gate  */
17890Sstevel@tonic-gate static boolean_t
17900Sstevel@tonic-gate copyin_mech(int mode, crypto_mechanism_t *in_mech,
17910Sstevel@tonic-gate     crypto_mechanism_t *out_mech, size_t *out_rctl_bytes, size_t *out_carry,
17923916Skrishna     int *out_rv, int *out_error)
17930Sstevel@tonic-gate {
17940Sstevel@tonic-gate 	STRUCT_DECL(crypto_mechanism, mech);
17950Sstevel@tonic-gate 	caddr_t param;
17960Sstevel@tonic-gate 	size_t param_len;
17970Sstevel@tonic-gate 	size_t rctl_bytes = 0, carry = 0;
17980Sstevel@tonic-gate 	int error = 0;
17990Sstevel@tonic-gate 	int rv = 0;
18000Sstevel@tonic-gate 
18010Sstevel@tonic-gate 	STRUCT_INIT(mech, mode);
18020Sstevel@tonic-gate 	bcopy(in_mech, STRUCT_BUF(mech), STRUCT_SIZE(mech));
18030Sstevel@tonic-gate 	param = STRUCT_FGETP(mech, cm_param);
18040Sstevel@tonic-gate 	param_len = STRUCT_FGET(mech, cm_param_len);
18050Sstevel@tonic-gate 	out_mech->cm_type = STRUCT_FGET(mech, cm_type);
18060Sstevel@tonic-gate 	out_mech->cm_param = NULL;
18070Sstevel@tonic-gate 	out_mech->cm_param_len = 0;
18080Sstevel@tonic-gate 	if (param != NULL && param_len != 0) {
18090Sstevel@tonic-gate 		if (param_len > crypto_max_buffer_len) {
18100Sstevel@tonic-gate 			cmn_err(CE_NOTE, "copyin_mech: buffer greater than "
18110Sstevel@tonic-gate 			    "%ld bytes, pid = %d", crypto_max_buffer_len,
18120Sstevel@tonic-gate 			    curproc->p_pid);
18130Sstevel@tonic-gate 			rv = CRYPTO_ARGUMENTS_BAD;
18140Sstevel@tonic-gate 			goto out;
18150Sstevel@tonic-gate 		}
18160Sstevel@tonic-gate 
18170Sstevel@tonic-gate 		/*
18180Sstevel@tonic-gate 		 * Most calls to copyin_mech() are followed by a call to
18190Sstevel@tonic-gate 		 * copyin_key(), resulting in two resource control checks.
18200Sstevel@tonic-gate 		 * As an optimization, the resource control check is not
18210Sstevel@tonic-gate 		 * made in this function if the check is for less than
18220Sstevel@tonic-gate 		 * CRYPTO_DEFERRED_LIMIT bytes. The number of bytes that
18230Sstevel@tonic-gate 		 * would be checked is passed as an argument to copyin_key()
18240Sstevel@tonic-gate 		 * where the check is made, and the bytes are charged against
18250Sstevel@tonic-gate 		 * the project.max-crypto-memory resource control.
18260Sstevel@tonic-gate 		 */
18270Sstevel@tonic-gate 		if ((param_len > CRYPTO_DEFERRED_LIMIT) || out_carry == NULL) {
18283916Skrishna 			rv = crypto_buffer_check(param_len);
18290Sstevel@tonic-gate 			if (rv != CRYPTO_SUCCESS) {
18300Sstevel@tonic-gate 				goto out;
18310Sstevel@tonic-gate 			}
18320Sstevel@tonic-gate 			rctl_bytes = param_len;
18330Sstevel@tonic-gate 		} else {
18340Sstevel@tonic-gate 			carry = param_len;
18350Sstevel@tonic-gate 		}
18360Sstevel@tonic-gate 		out_mech->cm_param = kmem_alloc(param_len, KM_SLEEP);
18370Sstevel@tonic-gate 		if (copyin((char *)param, out_mech->cm_param, param_len) != 0) {
18380Sstevel@tonic-gate 			kmem_free(out_mech->cm_param, param_len);
18390Sstevel@tonic-gate 			out_mech->cm_param = NULL;
18400Sstevel@tonic-gate 			error = EFAULT;
18410Sstevel@tonic-gate 			goto out;
18420Sstevel@tonic-gate 		}
18430Sstevel@tonic-gate 		out_mech->cm_param_len = param_len;
18440Sstevel@tonic-gate 	}
18450Sstevel@tonic-gate out:
18460Sstevel@tonic-gate 	*out_rctl_bytes = rctl_bytes;
18470Sstevel@tonic-gate 	*out_rv = rv;
18480Sstevel@tonic-gate 	*out_error = error;
18490Sstevel@tonic-gate 	if (out_carry != NULL)
18500Sstevel@tonic-gate 		*out_carry = carry;
18510Sstevel@tonic-gate 	return ((rv | error) ? B_FALSE : B_TRUE);
18520Sstevel@tonic-gate }
18530Sstevel@tonic-gate 
18540Sstevel@tonic-gate /*
18550Sstevel@tonic-gate  * Free key attributes when key type is CRYPTO_KEY_ATTR_LIST.
18560Sstevel@tonic-gate  * The crypto_key structure is not freed.
18570Sstevel@tonic-gate  */
18580Sstevel@tonic-gate static void
18590Sstevel@tonic-gate crypto_free_key_attributes(crypto_key_t *key)
18600Sstevel@tonic-gate {
18610Sstevel@tonic-gate 	crypto_object_attribute_t *attrs;
18620Sstevel@tonic-gate 	size_t len = 0;
18630Sstevel@tonic-gate 	int i;
18640Sstevel@tonic-gate 
18650Sstevel@tonic-gate 	ASSERT(key->ck_format == CRYPTO_KEY_ATTR_LIST);
18660Sstevel@tonic-gate 	if (key->ck_count == 0 || key->ck_attrs == NULL)
18670Sstevel@tonic-gate 		return;
18680Sstevel@tonic-gate 
18690Sstevel@tonic-gate 	/* compute the size of the container */
18700Sstevel@tonic-gate 	len = key->ck_count * sizeof (crypto_object_attribute_t);
18710Sstevel@tonic-gate 
18720Sstevel@tonic-gate 	/* total up the size of all attributes in the container */
18730Sstevel@tonic-gate 	for (i = 0; i < key->ck_count; i++) {
18740Sstevel@tonic-gate 		attrs = &key->ck_attrs[i];
18750Sstevel@tonic-gate 		if (attrs->oa_value_len != 0 &&
18760Sstevel@tonic-gate 		    attrs->oa_value != NULL) {
18770Sstevel@tonic-gate 			len += roundup(attrs->oa_value_len, sizeof (caddr_t));
18780Sstevel@tonic-gate 		}
18790Sstevel@tonic-gate 	}
18800Sstevel@tonic-gate 
18810Sstevel@tonic-gate 	bzero(key->ck_attrs, len);
18820Sstevel@tonic-gate 	kmem_free(key->ck_attrs, len);
18830Sstevel@tonic-gate }
18840Sstevel@tonic-gate 
18850Sstevel@tonic-gate /*
18860Sstevel@tonic-gate  * Frees allocated storage in the key structure, but doesn't free
18870Sstevel@tonic-gate  * the key structure.
18880Sstevel@tonic-gate  */
18890Sstevel@tonic-gate static void
18900Sstevel@tonic-gate free_crypto_key(crypto_key_t *key)
18910Sstevel@tonic-gate {
18920Sstevel@tonic-gate 	switch (key->ck_format) {
18930Sstevel@tonic-gate 	case CRYPTO_KEY_RAW: {
18940Sstevel@tonic-gate 		size_t len;
18950Sstevel@tonic-gate 
18960Sstevel@tonic-gate 		if (key->ck_length == 0 || key->ck_data == NULL)
18970Sstevel@tonic-gate 			break;
18980Sstevel@tonic-gate 
18990Sstevel@tonic-gate 		len = CRYPTO_BITS2BYTES(key->ck_length);
19000Sstevel@tonic-gate 		bzero(key->ck_data, len);
19010Sstevel@tonic-gate 		kmem_free(key->ck_data, len);
19020Sstevel@tonic-gate 		break;
19030Sstevel@tonic-gate 	}
19040Sstevel@tonic-gate 
19050Sstevel@tonic-gate 	case CRYPTO_KEY_ATTR_LIST:
19060Sstevel@tonic-gate 		crypto_free_key_attributes(key);
19070Sstevel@tonic-gate 		break;
19080Sstevel@tonic-gate 
19090Sstevel@tonic-gate 	default:
19100Sstevel@tonic-gate 		break;
19110Sstevel@tonic-gate 	}
19120Sstevel@tonic-gate }
19130Sstevel@tonic-gate 
19140Sstevel@tonic-gate /*
19150Sstevel@tonic-gate  * Copy in an array of crypto_object_attribute structures from user-space.
19160Sstevel@tonic-gate  * Kernel memory is allocated for the array and the value of each attribute
19170Sstevel@tonic-gate  * in the array.  Since unprivileged users can specify the size of attributes,
19180Sstevel@tonic-gate  * the amount of memory needed is charged against the
19190Sstevel@tonic-gate  * project.max-crypto-memory resource control.
19200Sstevel@tonic-gate  *
19210Sstevel@tonic-gate  * Attribute values are copied in from user-space if copyin_value is set to
19220Sstevel@tonic-gate  * B_TRUE.  This routine returns B_TRUE if the copyin was successful.
19230Sstevel@tonic-gate  */
19240Sstevel@tonic-gate static boolean_t
19250Sstevel@tonic-gate copyin_attributes(int mode, uint_t count, caddr_t oc_attributes,
19260Sstevel@tonic-gate     crypto_object_attribute_t **k_attrs_out, size_t *k_attrs_size_out,
19270Sstevel@tonic-gate     caddr_t *u_attrs_out, int *out_rv, int *out_error, size_t *out_rctl_bytes,
19283916Skrishna     size_t carry, boolean_t copyin_value)
19290Sstevel@tonic-gate {
19300Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_attribute, oa);
19310Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
19320Sstevel@tonic-gate 	caddr_t attrs = NULL, ap, p, value;
19330Sstevel@tonic-gate 	caddr_t k_attrs_buf;
19340Sstevel@tonic-gate 	size_t k_attrs_len;
19350Sstevel@tonic-gate 	size_t k_attrs_buf_len = 0;
19360Sstevel@tonic-gate 	size_t k_attrs_total_len = 0;
19370Sstevel@tonic-gate 	size_t tmp_len;
19380Sstevel@tonic-gate 	size_t rctl_bytes = 0;
19390Sstevel@tonic-gate 	size_t len = 0;
19400Sstevel@tonic-gate 	size_t value_len;
19410Sstevel@tonic-gate 	int error = 0;
19420Sstevel@tonic-gate 	int rv = 0;
19430Sstevel@tonic-gate 	int i;
19440Sstevel@tonic-gate 
19450Sstevel@tonic-gate 	STRUCT_INIT(oa, mode);
19460Sstevel@tonic-gate 
19470Sstevel@tonic-gate 	if (count == 0) {
19480Sstevel@tonic-gate 		rv = CRYPTO_SUCCESS;
19490Sstevel@tonic-gate 		goto out;
19500Sstevel@tonic-gate 	}
19510Sstevel@tonic-gate 
19520Sstevel@tonic-gate 	if (count > CRYPTO_MAX_ATTRIBUTE_COUNT) {
19530Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
19540Sstevel@tonic-gate 		goto out;
19550Sstevel@tonic-gate 	}
19560Sstevel@tonic-gate 
19570Sstevel@tonic-gate 	/* compute size of crypto_object_attribute array */
19580Sstevel@tonic-gate 	len = count * STRUCT_SIZE(oa);
19590Sstevel@tonic-gate 
19600Sstevel@tonic-gate 	/* this allocation is not charged against the user's resource limit */
19610Sstevel@tonic-gate 	attrs = kmem_alloc(len, KM_SLEEP);
19620Sstevel@tonic-gate 	if (copyin(oc_attributes, attrs, len) != 0) {
19630Sstevel@tonic-gate 		error = EFAULT;
19640Sstevel@tonic-gate 		goto out;
19650Sstevel@tonic-gate 	}
19660Sstevel@tonic-gate 
19670Sstevel@tonic-gate 	/* figure out how much memory to allocate for all of the attributes */
19680Sstevel@tonic-gate 	ap = attrs;
19690Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
19700Sstevel@tonic-gate 		bcopy(ap, STRUCT_BUF(oa), STRUCT_SIZE(oa));
19710Sstevel@tonic-gate 		tmp_len = roundup(STRUCT_FGET(oa, oa_value_len),
19720Sstevel@tonic-gate 		    sizeof (caddr_t));
19730Sstevel@tonic-gate 		if (tmp_len > crypto_max_buffer_len) {
19740Sstevel@tonic-gate 			cmn_err(CE_NOTE, "copyin_attributes: buffer greater "
19750Sstevel@tonic-gate 			    "than %ld bytes, pid = %d", crypto_max_buffer_len,
19760Sstevel@tonic-gate 			    curproc->p_pid);
19770Sstevel@tonic-gate 			rv = CRYPTO_ARGUMENTS_BAD;
19780Sstevel@tonic-gate 			goto out;
19790Sstevel@tonic-gate 		}
19800Sstevel@tonic-gate 		if (STRUCT_FGETP(oa, oa_value) != NULL)
19810Sstevel@tonic-gate 			k_attrs_buf_len += tmp_len;
19820Sstevel@tonic-gate 		ap += STRUCT_SIZE(oa);
19830Sstevel@tonic-gate 	}
19840Sstevel@tonic-gate 
19850Sstevel@tonic-gate 	k_attrs_len = count * sizeof (crypto_object_attribute_t);
19860Sstevel@tonic-gate 	k_attrs_total_len = k_attrs_buf_len + k_attrs_len;
19870Sstevel@tonic-gate 	if ((k_attrs_total_len + carry) != 0) {
19883916Skrishna 		rv = crypto_buffer_check(k_attrs_total_len + carry);
19890Sstevel@tonic-gate 		if (rv != CRYPTO_SUCCESS) {
19900Sstevel@tonic-gate 			goto out;
19910Sstevel@tonic-gate 		}
19920Sstevel@tonic-gate 	}
19930Sstevel@tonic-gate 	rctl_bytes = k_attrs_total_len + carry;
19940Sstevel@tonic-gate 
19950Sstevel@tonic-gate 	/* one big allocation for everything */
19960Sstevel@tonic-gate 	k_attrs = kmem_alloc(k_attrs_total_len, KM_SLEEP);
19970Sstevel@tonic-gate 	k_attrs_buf = (char *)k_attrs + k_attrs_len;
19980Sstevel@tonic-gate 
19990Sstevel@tonic-gate 	ap = attrs;
20000Sstevel@tonic-gate 	p = k_attrs_buf;
20010Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
20020Sstevel@tonic-gate 		bcopy(ap, STRUCT_BUF(oa), STRUCT_SIZE(oa));
20030Sstevel@tonic-gate 		k_attrs[i].oa_type = STRUCT_FGET(oa, oa_type);
20040Sstevel@tonic-gate 		value = STRUCT_FGETP(oa, oa_value);
20050Sstevel@tonic-gate 		value_len = STRUCT_FGET(oa, oa_value_len);
20060Sstevel@tonic-gate 		if (value != NULL && value_len != 0 && copyin_value) {
20070Sstevel@tonic-gate 			if (copyin(value, p, value_len) != 0) {
20080Sstevel@tonic-gate 				kmem_free(k_attrs, k_attrs_total_len);
20090Sstevel@tonic-gate 				k_attrs = NULL;
20100Sstevel@tonic-gate 				error = EFAULT;
20110Sstevel@tonic-gate 				goto out;
20120Sstevel@tonic-gate 			}
20130Sstevel@tonic-gate 		}
20140Sstevel@tonic-gate 
2015292Smcpowers 		if (value != NULL) {
2016292Smcpowers 			k_attrs[i].oa_value = p;
2017292Smcpowers 			p += roundup(value_len, sizeof (caddr_t));
2018292Smcpowers 		} else {
2019292Smcpowers 			k_attrs[i].oa_value = NULL;
2020292Smcpowers 		}
20210Sstevel@tonic-gate 		k_attrs[i].oa_value_len = value_len;
20220Sstevel@tonic-gate 		ap += STRUCT_SIZE(oa);
20230Sstevel@tonic-gate 	}
20240Sstevel@tonic-gate out:
20250Sstevel@tonic-gate 	if (attrs != NULL) {
20260Sstevel@tonic-gate 		/*
20270Sstevel@tonic-gate 		 * Free the array if there is a failure or the caller
20280Sstevel@tonic-gate 		 * doesn't want the array to be returned.
20290Sstevel@tonic-gate 		 */
20300Sstevel@tonic-gate 		if (error != 0 || rv != CRYPTO_SUCCESS || u_attrs_out == NULL) {
20310Sstevel@tonic-gate 			kmem_free(attrs, len);
20320Sstevel@tonic-gate 			attrs = NULL;
20330Sstevel@tonic-gate 		}
20340Sstevel@tonic-gate 	}
20350Sstevel@tonic-gate 
20360Sstevel@tonic-gate 	if (u_attrs_out != NULL)
20370Sstevel@tonic-gate 		*u_attrs_out = attrs;
20380Sstevel@tonic-gate 	if (k_attrs_size_out != NULL)
20390Sstevel@tonic-gate 		*k_attrs_size_out = k_attrs_total_len;
20400Sstevel@tonic-gate 	*k_attrs_out = k_attrs;
20410Sstevel@tonic-gate 	*out_rctl_bytes = rctl_bytes;
20420Sstevel@tonic-gate 	*out_rv = rv;
20430Sstevel@tonic-gate 	*out_error = error;
20440Sstevel@tonic-gate 	return ((rv | error) ? B_FALSE : B_TRUE);
20450Sstevel@tonic-gate }
20460Sstevel@tonic-gate 
20470Sstevel@tonic-gate /*
20480Sstevel@tonic-gate  * Copy data model dependent raw key into a kernel key
20490Sstevel@tonic-gate  * structure.  Checks key length or attribute lengths against
20500Sstevel@tonic-gate  * resource controls before allocating memory.  Returns B_TRUE
20510Sstevel@tonic-gate  * if both error and rv are set to 0.
20520Sstevel@tonic-gate  */
20530Sstevel@tonic-gate static boolean_t
20540Sstevel@tonic-gate copyin_key(int mode, crypto_key_t *in_key, crypto_key_t *out_key,
20553916Skrishna     size_t *out_rctl_bytes, int *out_rv, int *out_error, size_t carry)
20560Sstevel@tonic-gate {
20570Sstevel@tonic-gate 	STRUCT_DECL(crypto_key, key);
20580Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
20590Sstevel@tonic-gate 	size_t key_bits;
20600Sstevel@tonic-gate 	size_t key_bytes = 0;
20610Sstevel@tonic-gate 	size_t rctl_bytes = 0;
20620Sstevel@tonic-gate 	int count;
20630Sstevel@tonic-gate 	int error = 0;
20640Sstevel@tonic-gate 	int rv = CRYPTO_SUCCESS;
20650Sstevel@tonic-gate 
20660Sstevel@tonic-gate 	STRUCT_INIT(key, mode);
20670Sstevel@tonic-gate 	bcopy(in_key, STRUCT_BUF(key), STRUCT_SIZE(key));
20680Sstevel@tonic-gate 	out_key->ck_format = STRUCT_FGET(key, ck_format);
20690Sstevel@tonic-gate 	switch (out_key->ck_format) {
20700Sstevel@tonic-gate 	case CRYPTO_KEY_RAW:
20710Sstevel@tonic-gate 		key_bits = STRUCT_FGET(key, ck_length);
20720Sstevel@tonic-gate 		if (key_bits != 0) {
20730Sstevel@tonic-gate 			key_bytes = CRYPTO_BITS2BYTES(key_bits);
20740Sstevel@tonic-gate 			if (key_bytes > crypto_max_buffer_len) {
20750Sstevel@tonic-gate 				cmn_err(CE_NOTE, "copyin_key: buffer greater "
20760Sstevel@tonic-gate 				    "than %ld bytes, pid = %d",
20770Sstevel@tonic-gate 				    crypto_max_buffer_len, curproc->p_pid);
20780Sstevel@tonic-gate 				rv = CRYPTO_ARGUMENTS_BAD;
20790Sstevel@tonic-gate 				goto out;
20800Sstevel@tonic-gate 			}
20810Sstevel@tonic-gate 
20823916Skrishna 			rv = crypto_buffer_check(key_bytes + carry);
20830Sstevel@tonic-gate 			if (rv != CRYPTO_SUCCESS) {
20840Sstevel@tonic-gate 				goto out;
20850Sstevel@tonic-gate 			}
20860Sstevel@tonic-gate 			rctl_bytes = key_bytes + carry;
20870Sstevel@tonic-gate 
20880Sstevel@tonic-gate 			out_key->ck_data = kmem_alloc(key_bytes, KM_SLEEP);
20890Sstevel@tonic-gate 
20900Sstevel@tonic-gate 			if (copyin((char *)STRUCT_FGETP(key, ck_data),
20910Sstevel@tonic-gate 			    out_key->ck_data, key_bytes) != 0) {
20920Sstevel@tonic-gate 				kmem_free(out_key->ck_data, key_bytes);
20930Sstevel@tonic-gate 				out_key->ck_data = NULL;
20940Sstevel@tonic-gate 				out_key->ck_length = 0;
20950Sstevel@tonic-gate 				error = EFAULT;
20960Sstevel@tonic-gate 				goto out;
20970Sstevel@tonic-gate 			}
20980Sstevel@tonic-gate 		}
20990Sstevel@tonic-gate 		out_key->ck_length = key_bits;
21000Sstevel@tonic-gate 		break;
21010Sstevel@tonic-gate 
21020Sstevel@tonic-gate 	case CRYPTO_KEY_ATTR_LIST:
21030Sstevel@tonic-gate 		count = STRUCT_FGET(key, ck_count);
21040Sstevel@tonic-gate 
21050Sstevel@tonic-gate 		if (copyin_attributes(mode, count,
21060Sstevel@tonic-gate 		    (caddr_t)STRUCT_FGETP(key, ck_attrs), &k_attrs, NULL, NULL,
21073916Skrishna 		    &rv, &error, &rctl_bytes, carry, B_TRUE)) {
21080Sstevel@tonic-gate 			out_key->ck_count = count;
21090Sstevel@tonic-gate 			out_key->ck_attrs = k_attrs;
21100Sstevel@tonic-gate 			k_attrs = NULL;
21110Sstevel@tonic-gate 		} else {
21120Sstevel@tonic-gate 			out_key->ck_count = 0;
21130Sstevel@tonic-gate 			out_key->ck_attrs = NULL;
21140Sstevel@tonic-gate 		}
21150Sstevel@tonic-gate 		break;
21160Sstevel@tonic-gate 
21170Sstevel@tonic-gate 	case CRYPTO_KEY_REFERENCE:
21180Sstevel@tonic-gate 		out_key->ck_obj_id = STRUCT_FGET(key, ck_obj_id);
21190Sstevel@tonic-gate 		break;
21200Sstevel@tonic-gate 
21210Sstevel@tonic-gate 	default:
21220Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
21230Sstevel@tonic-gate 	}
21240Sstevel@tonic-gate 
21250Sstevel@tonic-gate out:
21260Sstevel@tonic-gate 	*out_rctl_bytes = rctl_bytes;
21270Sstevel@tonic-gate 	*out_rv = rv;
21280Sstevel@tonic-gate 	*out_error = error;
21290Sstevel@tonic-gate 	return ((rv | error) ? B_FALSE : B_TRUE);
21300Sstevel@tonic-gate }
21310Sstevel@tonic-gate 
21320Sstevel@tonic-gate /*
21330Sstevel@tonic-gate  * This routine does two things:
21340Sstevel@tonic-gate  * 1. Given a crypto_minor structure and a session ID, it returns
21350Sstevel@tonic-gate  *    a valid session pointer.
21360Sstevel@tonic-gate  * 2. It checks that the provider, to which the session has been opened,
21370Sstevel@tonic-gate  *    has not been removed.
21380Sstevel@tonic-gate  */
21390Sstevel@tonic-gate static boolean_t
21400Sstevel@tonic-gate get_session_ptr(crypto_session_id_t i, crypto_minor_t *cm,
21410Sstevel@tonic-gate     crypto_session_data_t **session_ptr, int *out_error, int *out_rv)
21420Sstevel@tonic-gate {
21430Sstevel@tonic-gate 	crypto_session_data_t *sp = NULL;
21440Sstevel@tonic-gate 	int rv = CRYPTO_SESSION_HANDLE_INVALID;
21450Sstevel@tonic-gate 	int error = 0;
21460Sstevel@tonic-gate 
21470Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
21480Sstevel@tonic-gate 	if ((i < cm->cm_session_table_count) &&
21490Sstevel@tonic-gate 	    (cm->cm_session_table[i] != NULL)) {
21500Sstevel@tonic-gate 		sp = cm->cm_session_table[i];
21510Sstevel@tonic-gate 		mutex_enter(&sp->sd_lock);
21520Sstevel@tonic-gate 		mutex_exit(&cm->cm_lock);
21530Sstevel@tonic-gate 		while (sp->sd_flags & CRYPTO_SESSION_IS_BUSY) {
21540Sstevel@tonic-gate 			if (cv_wait_sig(&sp->sd_cv, &sp->sd_lock) == 0) {
21550Sstevel@tonic-gate 				mutex_exit(&sp->sd_lock);
21560Sstevel@tonic-gate 				sp = NULL;
21570Sstevel@tonic-gate 				error = EINTR;
21580Sstevel@tonic-gate 				goto out;
21590Sstevel@tonic-gate 			}
21600Sstevel@tonic-gate 		}
21610Sstevel@tonic-gate 
21620Sstevel@tonic-gate 		if (sp->sd_flags & CRYPTO_SESSION_IS_CLOSED) {
21630Sstevel@tonic-gate 			mutex_exit(&sp->sd_lock);
21640Sstevel@tonic-gate 			sp = NULL;
21650Sstevel@tonic-gate 			goto out;
21660Sstevel@tonic-gate 		}
21670Sstevel@tonic-gate 
21680Sstevel@tonic-gate 		if (KCF_IS_PROV_REMOVED(sp->sd_provider)) {
21690Sstevel@tonic-gate 			mutex_exit(&sp->sd_lock);
21700Sstevel@tonic-gate 			sp = NULL;
21710Sstevel@tonic-gate 			rv = CRYPTO_DEVICE_ERROR;
21720Sstevel@tonic-gate 			goto out;
21730Sstevel@tonic-gate 		}
21740Sstevel@tonic-gate 
21750Sstevel@tonic-gate 		rv = CRYPTO_SUCCESS;
21760Sstevel@tonic-gate 		sp->sd_flags |= CRYPTO_SESSION_IS_BUSY;
21770Sstevel@tonic-gate 		mutex_exit(&sp->sd_lock);
21780Sstevel@tonic-gate 	} else {
21790Sstevel@tonic-gate 		mutex_exit(&cm->cm_lock);
21800Sstevel@tonic-gate 	}
21810Sstevel@tonic-gate out:
21820Sstevel@tonic-gate 	*session_ptr = sp;
21830Sstevel@tonic-gate 	*out_error = error;
21840Sstevel@tonic-gate 	*out_rv = rv;
21850Sstevel@tonic-gate 	return ((rv == CRYPTO_SUCCESS && error == 0) ? B_TRUE : B_FALSE);
21860Sstevel@tonic-gate }
21870Sstevel@tonic-gate 
21880Sstevel@tonic-gate #define	CRYPTO_SESSION_RELE(s) {			\
21890Sstevel@tonic-gate 	mutex_enter(&((s)->sd_lock));			\
21900Sstevel@tonic-gate 	(s)->sd_flags &= ~CRYPTO_SESSION_IS_BUSY;	\
21910Sstevel@tonic-gate 	cv_broadcast(&(s)->sd_cv);			\
21920Sstevel@tonic-gate 	mutex_exit(&((s)->sd_lock));			\
21930Sstevel@tonic-gate }
21940Sstevel@tonic-gate 
21950Sstevel@tonic-gate /* ARGSUSED */
21960Sstevel@tonic-gate static int
21970Sstevel@tonic-gate encrypt_init(dev_t dev, caddr_t arg, int mode, int *rval)
21980Sstevel@tonic-gate {
21990Sstevel@tonic-gate 	return (cipher_init(dev, arg, mode, crypto_encrypt_init_prov));
22000Sstevel@tonic-gate }
22010Sstevel@tonic-gate 
22020Sstevel@tonic-gate /* ARGSUSED */
22030Sstevel@tonic-gate static int
22040Sstevel@tonic-gate decrypt_init(dev_t dev, caddr_t arg, int mode, int *rval)
22050Sstevel@tonic-gate {
22060Sstevel@tonic-gate 	return (cipher_init(dev, arg, mode, crypto_decrypt_init_prov));
22070Sstevel@tonic-gate }
22080Sstevel@tonic-gate 
22090Sstevel@tonic-gate /*
2210904Smcpowers  * umech is a mechanism structure that has been copied from user address
2211904Smcpowers  * space into kernel address space. Only one copyin has been done.
2212904Smcpowers  * The mechanism parameter, if non-null, still points to user address space.
2213904Smcpowers  * If the mechanism parameter contains pointers, they are pointers into
2214904Smcpowers  * user address space.
2215904Smcpowers  *
2216904Smcpowers  * kmech is a umech with all pointers and structures in kernel address space.
2217904Smcpowers  *
2218904Smcpowers  * This routine calls the provider's entry point to copy a umech parameter
2219904Smcpowers  * into kernel address space. Kernel memory is allocated by the provider.
2220904Smcpowers  */
2221904Smcpowers static int
2222904Smcpowers crypto_provider_copyin_mech_param(kcf_provider_desc_t *pd,
2223904Smcpowers     crypto_mechanism_t *umech, crypto_mechanism_t *kmech, int mode, int *error)
2224904Smcpowers {
2225904Smcpowers 	crypto_mech_type_t provider_mech_type;
2226904Smcpowers 	int rv;
2227904Smcpowers 
2228904Smcpowers 	/* get the provider's mech number */
22293708Skrishna 	provider_mech_type = KCF_TO_PROV_MECHNUM(pd, umech->cm_type);
2230904Smcpowers 
2231904Smcpowers 	kmech->cm_param = NULL;
2232904Smcpowers 	kmech->cm_param_len = 0;
2233904Smcpowers 	kmech->cm_type = provider_mech_type;
2234904Smcpowers 	rv = KCF_PROV_COPYIN_MECH(pd, umech, kmech, error, mode);
2235904Smcpowers 	kmech->cm_type = umech->cm_type;
2236904Smcpowers 
2237904Smcpowers 	return (rv);
2238904Smcpowers }
2239904Smcpowers 
2240904Smcpowers /*
2241904Smcpowers  * umech is a mechanism structure that has been copied from user address
2242904Smcpowers  * space into kernel address space. Only one copyin has been done.
2243904Smcpowers  * The mechanism parameter, if non-null, still points to user address space.
2244904Smcpowers  * If the mechanism parameter contains pointers, they are pointers into
2245904Smcpowers  * user address space.
2246904Smcpowers  *
2247904Smcpowers  * kmech is a umech with all pointers and structures in kernel address space.
2248904Smcpowers  *
2249904Smcpowers  * This routine calls the provider's entry point to copy a kmech parameter
2250904Smcpowers  * into user address space using umech as a template containing
2251904Smcpowers  * user address pointers.
2252904Smcpowers  */
2253904Smcpowers static int
2254904Smcpowers crypto_provider_copyout_mech_param(kcf_provider_desc_t *pd,
2255904Smcpowers     crypto_mechanism_t *kmech, crypto_mechanism_t *umech, int mode, int *error)
2256904Smcpowers {
2257904Smcpowers 	crypto_mech_type_t provider_mech_type;
2258904Smcpowers 	int rv;
2259904Smcpowers 
2260904Smcpowers 	/* get the provider's mech number */
22613708Skrishna 	provider_mech_type = KCF_TO_PROV_MECHNUM(pd, umech->cm_type);
2262904Smcpowers 
2263904Smcpowers 	kmech->cm_type = provider_mech_type;
2264904Smcpowers 	rv = KCF_PROV_COPYOUT_MECH(pd, kmech, umech, error, mode);
2265904Smcpowers 	kmech->cm_type = umech->cm_type;
2266904Smcpowers 
2267904Smcpowers 	return (rv);
2268904Smcpowers }
2269904Smcpowers 
2270904Smcpowers /*
2271904Smcpowers  * Call the provider's entry point to free kernel memory that has been
2272904Smcpowers  * allocated for the mechanism's parameter.
2273904Smcpowers  */
2274904Smcpowers static void
2275904Smcpowers crypto_free_mech(kcf_provider_desc_t *pd, boolean_t allocated_by_crypto_module,
2276904Smcpowers     crypto_mechanism_t *mech)
2277904Smcpowers {
2278904Smcpowers 	crypto_mech_type_t provider_mech_type;
2279904Smcpowers 
2280904Smcpowers 	if (allocated_by_crypto_module) {
2281904Smcpowers 		if (mech->cm_param != NULL)
2282904Smcpowers 			kmem_free(mech->cm_param, mech->cm_param_len);
2283904Smcpowers 	} else {
2284904Smcpowers 		/* get the provider's mech number */
22853708Skrishna 		provider_mech_type = KCF_TO_PROV_MECHNUM(pd, mech->cm_type);
2286904Smcpowers 
2287904Smcpowers 		if (mech->cm_param != NULL && mech->cm_param_len != 0) {
2288904Smcpowers 			mech->cm_type = provider_mech_type;
2289904Smcpowers 			(void) KCF_PROV_FREE_MECH(pd, mech);
2290904Smcpowers 		}
2291904Smcpowers 	}
2292904Smcpowers }
2293904Smcpowers 
2294904Smcpowers /*
22950Sstevel@tonic-gate  * ASSUMPTION: crypto_encrypt_init and crypto_decrypt_init
22960Sstevel@tonic-gate  * structures are identical except for field names.
22970Sstevel@tonic-gate  */
22980Sstevel@tonic-gate static int
2299904Smcpowers cipher_init(dev_t dev, caddr_t arg, int mode, int (*init)(crypto_provider_t,
23000Sstevel@tonic-gate     crypto_session_id_t, crypto_mechanism_t *, crypto_key_t *,
23010Sstevel@tonic-gate     crypto_ctx_template_t, crypto_context_t *, crypto_call_req_t *))
23020Sstevel@tonic-gate {
23030Sstevel@tonic-gate 	STRUCT_DECL(crypto_encrypt_init, encrypt_init);
2304904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
23050Sstevel@tonic-gate 	crypto_session_id_t session_id;
23060Sstevel@tonic-gate 	crypto_mechanism_t mech;
23070Sstevel@tonic-gate 	crypto_key_t key;
23080Sstevel@tonic-gate 	crypto_minor_t *cm;
23090Sstevel@tonic-gate 	crypto_session_data_t *sp;
23100Sstevel@tonic-gate 	crypto_context_t cc;
23110Sstevel@tonic-gate 	crypto_ctx_t **ctxpp;
23120Sstevel@tonic-gate 	size_t mech_rctl_bytes = 0;
23130Sstevel@tonic-gate 	size_t key_rctl_bytes = 0;
23140Sstevel@tonic-gate 	size_t carry;
23150Sstevel@tonic-gate 	int error = 0;
23160Sstevel@tonic-gate 	int rv;
2317904Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
23181808Smcpowers 	crypto_func_group_t fg;
23190Sstevel@tonic-gate 
23200Sstevel@tonic-gate 	STRUCT_INIT(encrypt_init, mode);
23210Sstevel@tonic-gate 
23220Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
23230Sstevel@tonic-gate 		cmn_err(CE_WARN, "cipher_init: failed holding minor");
23240Sstevel@tonic-gate 		return (ENXIO);
23250Sstevel@tonic-gate 	}
23260Sstevel@tonic-gate 
23270Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(encrypt_init),
23280Sstevel@tonic-gate 	    STRUCT_SIZE(encrypt_init)) != 0) {
23290Sstevel@tonic-gate 		crypto_release_minor(cm);
23300Sstevel@tonic-gate 		return (EFAULT);
23310Sstevel@tonic-gate 	}
23320Sstevel@tonic-gate 
23330Sstevel@tonic-gate 	mech.cm_param = NULL;
23340Sstevel@tonic-gate 	bzero(&key, sizeof (crypto_key_t));
23350Sstevel@tonic-gate 
23360Sstevel@tonic-gate 	session_id = STRUCT_FGET(encrypt_init, ei_session);
23370Sstevel@tonic-gate 
23380Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
23390Sstevel@tonic-gate 		goto release_minor;
23400Sstevel@tonic-gate 	}
23410Sstevel@tonic-gate 
2342904Smcpowers 	bcopy(STRUCT_FADDR(encrypt_init, ei_mech), &mech.cm_type,
2343904Smcpowers 	    sizeof (crypto_mech_type_t));
23440Sstevel@tonic-gate 
23451808Smcpowers 	if (init == crypto_encrypt_init_prov) {
23461808Smcpowers 		fg = CRYPTO_FG_ENCRYPT;
23471808Smcpowers 	} else {
23481808Smcpowers 		fg = CRYPTO_FG_DECRYPT;
23491808Smcpowers 	}
23500Sstevel@tonic-gate 
2351904Smcpowers 	if ((rv = kcf_get_hardware_provider(mech.cm_type, CRYPTO_MECH_INVALID,
23521808Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider, fg))
23531808Smcpowers 	    != CRYPTO_SUCCESS) {
23540Sstevel@tonic-gate 		goto out;
23550Sstevel@tonic-gate 	}
23560Sstevel@tonic-gate 
2357904Smcpowers 	carry = 0;
2358904Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
2359904Smcpowers 	    STRUCT_FADDR(encrypt_init, ei_mech), &mech, mode, &error);
2360904Smcpowers 
2361904Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
2362904Smcpowers 		allocated_by_crypto_module = B_TRUE;
2363904Smcpowers 		if (!copyin_mech(mode, STRUCT_FADDR(encrypt_init, ei_mech),
23643916Skrishna 		    &mech, &mech_rctl_bytes, &carry, &rv, &error)) {
2365904Smcpowers 			goto out;
2366904Smcpowers 		}
2367904Smcpowers 	} else {
2368904Smcpowers 		if (rv != CRYPTO_SUCCESS)
2369904Smcpowers 			goto out;
2370904Smcpowers 	}
2371904Smcpowers 
23720Sstevel@tonic-gate 	if (!copyin_key(mode, STRUCT_FADDR(encrypt_init, ei_key), &key,
23733916Skrishna 	    &key_rctl_bytes, &rv, &error, carry)) {
23740Sstevel@tonic-gate 		goto out;
23750Sstevel@tonic-gate 	}
23760Sstevel@tonic-gate 
23770Sstevel@tonic-gate 	rv = (init)(real_provider, sp->sd_provider_session->ps_session,
23780Sstevel@tonic-gate 	    &mech, &key, NULL, &cc, NULL);
23790Sstevel@tonic-gate 
23800Sstevel@tonic-gate 	/*
23810Sstevel@tonic-gate 	 * Check if a context already exists. If so, it means it is being
23820Sstevel@tonic-gate 	 * abandoned. So, cancel it to avoid leaking it.
23830Sstevel@tonic-gate 	 */
23840Sstevel@tonic-gate 	ctxpp = (init == crypto_encrypt_init_prov) ?
23850Sstevel@tonic-gate 	    &sp->sd_encr_ctx : &sp->sd_decr_ctx;
23860Sstevel@tonic-gate 
23870Sstevel@tonic-gate 	if (*ctxpp != NULL)
23880Sstevel@tonic-gate 		CRYPTO_CANCEL_CTX(ctxpp);
23890Sstevel@tonic-gate 	*ctxpp = (rv == CRYPTO_SUCCESS) ? cc : NULL;
2390904Smcpowers 
23910Sstevel@tonic-gate out:
23920Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
23930Sstevel@tonic-gate 
23940Sstevel@tonic-gate release_minor:
23953916Skrishna 	if (mech_rctl_bytes + key_rctl_bytes != 0)
23963916Skrishna 		CRYPTO_DECREMENT_RCTL(mech_rctl_bytes + key_rctl_bytes);
23970Sstevel@tonic-gate 	crypto_release_minor(cm);
23980Sstevel@tonic-gate 
2399904Smcpowers 	if (real_provider != NULL) {
2400904Smcpowers 		crypto_free_mech(real_provider,
2401904Smcpowers 		    allocated_by_crypto_module, &mech);
2402904Smcpowers 		KCF_PROV_REFRELE(real_provider);
2403904Smcpowers 	}
24040Sstevel@tonic-gate 
24050Sstevel@tonic-gate 	free_crypto_key(&key);
24060Sstevel@tonic-gate 
24070Sstevel@tonic-gate 	if (error != 0)
2408904Smcpowers 		/* XXX free context */
24090Sstevel@tonic-gate 		return (error);
24100Sstevel@tonic-gate 
24110Sstevel@tonic-gate 	STRUCT_FSET(encrypt_init, ei_return_value, rv);
24120Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(encrypt_init), arg,
24130Sstevel@tonic-gate 	    STRUCT_SIZE(encrypt_init)) != 0) {
2414904Smcpowers 		/* XXX free context */
24150Sstevel@tonic-gate 		return (EFAULT);
24160Sstevel@tonic-gate 	}
24170Sstevel@tonic-gate 	return (0);
24180Sstevel@tonic-gate }
24190Sstevel@tonic-gate 
24200Sstevel@tonic-gate /* ARGSUSED */
24210Sstevel@tonic-gate static int
24220Sstevel@tonic-gate encrypt(dev_t dev, caddr_t arg, int mode, int *rval)
24230Sstevel@tonic-gate {
24240Sstevel@tonic-gate 	return (cipher(dev, arg, mode, crypto_encrypt_single));
24250Sstevel@tonic-gate }
24260Sstevel@tonic-gate 
24270Sstevel@tonic-gate /* ARGSUSED */
24280Sstevel@tonic-gate static int
24290Sstevel@tonic-gate decrypt(dev_t dev, caddr_t arg, int mode, int *rval)
24300Sstevel@tonic-gate {
24310Sstevel@tonic-gate 	return (cipher(dev, arg, mode, crypto_decrypt_single));
24320Sstevel@tonic-gate }
24330Sstevel@tonic-gate 
24340Sstevel@tonic-gate /*
24350Sstevel@tonic-gate  * ASSUMPTION: crypto_encrypt and crypto_decrypt structures
24360Sstevel@tonic-gate  * are identical except for field names.
24370Sstevel@tonic-gate  */
24380Sstevel@tonic-gate static int
24390Sstevel@tonic-gate cipher(dev_t dev, caddr_t arg, int mode,
24400Sstevel@tonic-gate     int (*single)(crypto_context_t, crypto_data_t *, crypto_data_t *,
24410Sstevel@tonic-gate     crypto_call_req_t *))
24420Sstevel@tonic-gate {
24430Sstevel@tonic-gate 	STRUCT_DECL(crypto_encrypt, encrypt);
24440Sstevel@tonic-gate 	crypto_session_id_t session_id;
24450Sstevel@tonic-gate 	crypto_minor_t *cm;
24460Sstevel@tonic-gate 	crypto_session_data_t *sp;
24470Sstevel@tonic-gate 	crypto_ctx_t **ctxpp;
24480Sstevel@tonic-gate 	crypto_data_t data, encr;
24490Sstevel@tonic-gate 	size_t datalen, encrlen, need = 0;
24500Sstevel@tonic-gate 	char *encrbuf;
24510Sstevel@tonic-gate 	int error = 0;
24520Sstevel@tonic-gate 	int rv;
24530Sstevel@tonic-gate 
24540Sstevel@tonic-gate 	STRUCT_INIT(encrypt, mode);
24550Sstevel@tonic-gate 
24560Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
24570Sstevel@tonic-gate 		cmn_err(CE_WARN, "cipher: failed holding minor");
24580Sstevel@tonic-gate 		return (ENXIO);
24590Sstevel@tonic-gate 	}
24600Sstevel@tonic-gate 
24610Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(encrypt), STRUCT_SIZE(encrypt)) != 0) {
24620Sstevel@tonic-gate 		crypto_release_minor(cm);
24630Sstevel@tonic-gate 		return (EFAULT);
24640Sstevel@tonic-gate 	}
24650Sstevel@tonic-gate 
24660Sstevel@tonic-gate 	data.cd_raw.iov_base = NULL;
24670Sstevel@tonic-gate 	encr.cd_raw.iov_base = NULL;
24680Sstevel@tonic-gate 
24690Sstevel@tonic-gate 	datalen = STRUCT_FGET(encrypt, ce_datalen);
24700Sstevel@tonic-gate 	encrlen = STRUCT_FGET(encrypt, ce_encrlen);
24710Sstevel@tonic-gate 
24720Sstevel@tonic-gate 	/*
24730Sstevel@tonic-gate 	 * Don't allocate output buffer unless both buffer pointer and
24740Sstevel@tonic-gate 	 * buffer length are not NULL or 0 (length).
24750Sstevel@tonic-gate 	 */
24760Sstevel@tonic-gate 	encrbuf = STRUCT_FGETP(encrypt, ce_encrbuf);
24770Sstevel@tonic-gate 	if (encrbuf == NULL || encrlen == 0) {
24780Sstevel@tonic-gate 		encrlen = 0;
24790Sstevel@tonic-gate 	}
24800Sstevel@tonic-gate 
24810Sstevel@tonic-gate 	if (datalen > crypto_max_buffer_len ||
24820Sstevel@tonic-gate 	    encrlen > crypto_max_buffer_len) {
24830Sstevel@tonic-gate 		cmn_err(CE_NOTE, "cipher: buffer greater than %ld bytes, "
24840Sstevel@tonic-gate 		    "pid = %d", crypto_max_buffer_len, curproc->p_pid);
24850Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
24860Sstevel@tonic-gate 		goto release_minor;
24870Sstevel@tonic-gate 	}
24880Sstevel@tonic-gate 
24890Sstevel@tonic-gate 	need = datalen + encrlen;
24903916Skrishna 	if ((rv = crypto_buffer_check(need)) != CRYPTO_SUCCESS) {
24910Sstevel@tonic-gate 		need = 0;
24920Sstevel@tonic-gate 		goto release_minor;
24930Sstevel@tonic-gate 	}
24940Sstevel@tonic-gate 
24950Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(data, datalen);
24960Sstevel@tonic-gate 	data.cd_miscdata = NULL;
24970Sstevel@tonic-gate 
24980Sstevel@tonic-gate 	if (datalen != 0 && copyin(STRUCT_FGETP(encrypt, ce_databuf),
24990Sstevel@tonic-gate 	    data.cd_raw.iov_base, datalen) != 0) {
25000Sstevel@tonic-gate 		error = EFAULT;
25010Sstevel@tonic-gate 		goto release_minor;
25020Sstevel@tonic-gate 	}
25030Sstevel@tonic-gate 
25040Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(encr, encrlen);
25050Sstevel@tonic-gate 
25060Sstevel@tonic-gate 	session_id = STRUCT_FGET(encrypt, ce_session);
25070Sstevel@tonic-gate 
25080Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv))  {
25090Sstevel@tonic-gate 		goto release_minor;
25100Sstevel@tonic-gate 	}
25110Sstevel@tonic-gate 
25120Sstevel@tonic-gate 	ctxpp = (single == crypto_encrypt_single) ?
25130Sstevel@tonic-gate 	    &sp->sd_encr_ctx : &sp->sd_decr_ctx;
25140Sstevel@tonic-gate 
25150Sstevel@tonic-gate 	rv = (single)(*ctxpp, &data, &encr, NULL);
25160Sstevel@tonic-gate 	if (KCF_CONTEXT_DONE(rv))
25170Sstevel@tonic-gate 		*ctxpp = NULL;
25180Sstevel@tonic-gate 
25190Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
25200Sstevel@tonic-gate 
25210Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
25220Sstevel@tonic-gate 		ASSERT(encr.cd_length <= encrlen);
25230Sstevel@tonic-gate 		if (encr.cd_length != 0 && copyout(encr.cd_raw.iov_base,
25240Sstevel@tonic-gate 		    encrbuf, encr.cd_length) != 0) {
25250Sstevel@tonic-gate 			error = EFAULT;
25260Sstevel@tonic-gate 			goto release_minor;
25270Sstevel@tonic-gate 		}
25280Sstevel@tonic-gate 		STRUCT_FSET(encrypt, ce_encrlen, encr.cd_length);
25290Sstevel@tonic-gate 	}
25300Sstevel@tonic-gate 
25310Sstevel@tonic-gate 	if (rv == CRYPTO_BUFFER_TOO_SMALL) {
25320Sstevel@tonic-gate 		/*
25330Sstevel@tonic-gate 		 * The providers return CRYPTO_BUFFER_TOO_SMALL even for case 1
25340Sstevel@tonic-gate 		 * of section 11.2 of the pkcs11 spec. We catch it here and
25350Sstevel@tonic-gate 		 * provide the correct pkcs11 return value.
25360Sstevel@tonic-gate 		 */
25370Sstevel@tonic-gate 		if (STRUCT_FGETP(encrypt, ce_encrbuf) == NULL)
25380Sstevel@tonic-gate 			rv = CRYPTO_SUCCESS;
25390Sstevel@tonic-gate 		STRUCT_FSET(encrypt, ce_encrlen, encr.cd_length);
25400Sstevel@tonic-gate 	}
25410Sstevel@tonic-gate 
25420Sstevel@tonic-gate release_minor:
25430Sstevel@tonic-gate 	if (need != 0) {
25443916Skrishna 		CRYPTO_DECREMENT_RCTL(need);
25450Sstevel@tonic-gate 	}
25460Sstevel@tonic-gate 	crypto_release_minor(cm);
25470Sstevel@tonic-gate 
25480Sstevel@tonic-gate 	if (data.cd_raw.iov_base != NULL)
25490Sstevel@tonic-gate 		kmem_free(data.cd_raw.iov_base, datalen);
25500Sstevel@tonic-gate 
25510Sstevel@tonic-gate 	if (encr.cd_raw.iov_base != NULL)
25520Sstevel@tonic-gate 		kmem_free(encr.cd_raw.iov_base, encrlen);
25530Sstevel@tonic-gate 
25540Sstevel@tonic-gate 	if (error != 0)
25550Sstevel@tonic-gate 		return (error);
25560Sstevel@tonic-gate 
25570Sstevel@tonic-gate 	STRUCT_FSET(encrypt, ce_return_value, rv);
25580Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(encrypt), arg, STRUCT_SIZE(encrypt)) != 0) {
25590Sstevel@tonic-gate 		return (EFAULT);
25600Sstevel@tonic-gate 	}
25610Sstevel@tonic-gate 	return (0);
25620Sstevel@tonic-gate }
25630Sstevel@tonic-gate 
25640Sstevel@tonic-gate /* ARGSUSED */
25650Sstevel@tonic-gate static int
25660Sstevel@tonic-gate encrypt_update(dev_t dev, caddr_t arg, int mode, int *rval)
25670Sstevel@tonic-gate {
25680Sstevel@tonic-gate 	return (cipher_update(dev, arg, mode, crypto_encrypt_update));
25690Sstevel@tonic-gate }
25700Sstevel@tonic-gate 
25710Sstevel@tonic-gate /* ARGSUSED */
25720Sstevel@tonic-gate static int
25730Sstevel@tonic-gate decrypt_update(dev_t dev, caddr_t arg, int mode, int *rval)
25740Sstevel@tonic-gate {
25750Sstevel@tonic-gate 	return (cipher_update(dev, arg, mode, crypto_decrypt_update));
25760Sstevel@tonic-gate }
25770Sstevel@tonic-gate 
25780Sstevel@tonic-gate /*
25790Sstevel@tonic-gate  * ASSUMPTION: crypto_encrypt_update and crypto_decrypt_update
25800Sstevel@tonic-gate  * structures are identical except for field names.
25810Sstevel@tonic-gate  */
25820Sstevel@tonic-gate static int
25830Sstevel@tonic-gate cipher_update(dev_t dev, caddr_t arg, int mode,
25840Sstevel@tonic-gate     int (*update)(crypto_context_t, crypto_data_t *, crypto_data_t *,
25850Sstevel@tonic-gate     crypto_call_req_t *))
25860Sstevel@tonic-gate {
25870Sstevel@tonic-gate 	STRUCT_DECL(crypto_encrypt_update, encrypt_update);
25880Sstevel@tonic-gate 	crypto_session_id_t session_id;
25890Sstevel@tonic-gate 	crypto_minor_t *cm;
25900Sstevel@tonic-gate 	crypto_session_data_t *sp;
25910Sstevel@tonic-gate 	crypto_ctx_t **ctxpp;
25920Sstevel@tonic-gate 	crypto_data_t data, encr;
25930Sstevel@tonic-gate 	size_t datalen, encrlen, need = 0;
25940Sstevel@tonic-gate 	char *encrbuf;
25950Sstevel@tonic-gate 	int error = 0;
25960Sstevel@tonic-gate 	int rv;
25970Sstevel@tonic-gate 
25980Sstevel@tonic-gate 	STRUCT_INIT(encrypt_update, mode);
25990Sstevel@tonic-gate 
26000Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
26010Sstevel@tonic-gate 		cmn_err(CE_WARN, "cipher_update: failed holding minor");
26020Sstevel@tonic-gate 		return (ENXIO);
26030Sstevel@tonic-gate 	}
26040Sstevel@tonic-gate 
26050Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(encrypt_update),
26060Sstevel@tonic-gate 	    STRUCT_SIZE(encrypt_update)) != 0) {
26070Sstevel@tonic-gate 		crypto_release_minor(cm);
26080Sstevel@tonic-gate 		return (EFAULT);
26090Sstevel@tonic-gate 	}
26100Sstevel@tonic-gate 
26110Sstevel@tonic-gate 	data.cd_raw.iov_base = NULL;
26120Sstevel@tonic-gate 	encr.cd_raw.iov_base = NULL;
26130Sstevel@tonic-gate 
26140Sstevel@tonic-gate 	datalen = STRUCT_FGET(encrypt_update, eu_datalen);
26150Sstevel@tonic-gate 	encrlen = STRUCT_FGET(encrypt_update, eu_encrlen);
26160Sstevel@tonic-gate 
26170Sstevel@tonic-gate 	/*
26180Sstevel@tonic-gate 	 * Don't allocate output buffer unless both buffer pointer and
26190Sstevel@tonic-gate 	 * buffer length are not NULL or 0 (length).
26200Sstevel@tonic-gate 	 */
26210Sstevel@tonic-gate 	encrbuf = STRUCT_FGETP(encrypt_update, eu_encrbuf);
26220Sstevel@tonic-gate 	if (encrbuf == NULL || encrlen == 0) {
26230Sstevel@tonic-gate 		encrlen = 0;
26240Sstevel@tonic-gate 	}
26250Sstevel@tonic-gate 
26260Sstevel@tonic-gate 	if (datalen > crypto_max_buffer_len ||
26270Sstevel@tonic-gate 	    encrlen > crypto_max_buffer_len) {
26280Sstevel@tonic-gate 		cmn_err(CE_NOTE, "cipher_update: buffer greater than %ld "
26290Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
26300Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
26310Sstevel@tonic-gate 		goto release_minor;
26320Sstevel@tonic-gate 	}
26330Sstevel@tonic-gate 
26340Sstevel@tonic-gate 	need = datalen + encrlen;
26353916Skrishna 	if ((rv = crypto_buffer_check(need)) != CRYPTO_SUCCESS) {
26360Sstevel@tonic-gate 		need = 0;
26370Sstevel@tonic-gate 		goto release_minor;
26380Sstevel@tonic-gate 	}
26390Sstevel@tonic-gate 
26400Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(data, datalen);
26410Sstevel@tonic-gate 	data.cd_miscdata = NULL;
26420Sstevel@tonic-gate 
26430Sstevel@tonic-gate 	if (datalen != 0 && copyin(STRUCT_FGETP(encrypt_update, eu_databuf),
26440Sstevel@tonic-gate 	    data.cd_raw.iov_base, datalen) != 0) {
26450Sstevel@tonic-gate 		error = EFAULT;
26460Sstevel@tonic-gate 		goto release_minor;
26470Sstevel@tonic-gate 	}
26480Sstevel@tonic-gate 
26490Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(encr, encrlen);
26500Sstevel@tonic-gate 
26510Sstevel@tonic-gate 	session_id = STRUCT_FGET(encrypt_update, eu_session);
26520Sstevel@tonic-gate 
26530Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv))  {
26540Sstevel@tonic-gate 		goto release_minor;
26550Sstevel@tonic-gate 	}
26560Sstevel@tonic-gate 
26570Sstevel@tonic-gate 	ctxpp = (update == crypto_encrypt_update) ?
26580Sstevel@tonic-gate 	    &sp->sd_encr_ctx : &sp->sd_decr_ctx;
26590Sstevel@tonic-gate 
26600Sstevel@tonic-gate 	rv = (update)(*ctxpp, &data, &encr, NULL);
26610Sstevel@tonic-gate 
26620Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS || rv == CRYPTO_BUFFER_TOO_SMALL) {
26630Sstevel@tonic-gate 		if (rv == CRYPTO_SUCCESS) {
26640Sstevel@tonic-gate 			ASSERT(encr.cd_length <= encrlen);
26650Sstevel@tonic-gate 			if (encr.cd_length != 0 && copyout(encr.cd_raw.iov_base,
26660Sstevel@tonic-gate 			    encrbuf, encr.cd_length) != 0) {
26670Sstevel@tonic-gate 				error = EFAULT;
26680Sstevel@tonic-gate 				goto out;
26690Sstevel@tonic-gate 			}
26700Sstevel@tonic-gate 		} else {
26710Sstevel@tonic-gate 			/*
26720Sstevel@tonic-gate 			 * The providers return CRYPTO_BUFFER_TOO_SMALL even
26730Sstevel@tonic-gate 			 * for case 1 of section 11.2 of the pkcs11 spec.
26740Sstevel@tonic-gate 			 * We catch it here and provide the correct pkcs11
26750Sstevel@tonic-gate 			 * return value.
26760Sstevel@tonic-gate 			 */
26770Sstevel@tonic-gate 			if (STRUCT_FGETP(encrypt_update, eu_encrbuf) == NULL)
26780Sstevel@tonic-gate 				rv = CRYPTO_SUCCESS;
26790Sstevel@tonic-gate 		}
26800Sstevel@tonic-gate 		STRUCT_FSET(encrypt_update, eu_encrlen, encr.cd_length);
26810Sstevel@tonic-gate 	} else {
26820Sstevel@tonic-gate 		CRYPTO_CANCEL_CTX(ctxpp);
26830Sstevel@tonic-gate 	}
26840Sstevel@tonic-gate out:
26850Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
26860Sstevel@tonic-gate 
26870Sstevel@tonic-gate release_minor:
26880Sstevel@tonic-gate 	if (need != 0) {
26893916Skrishna 		CRYPTO_DECREMENT_RCTL(need);
26900Sstevel@tonic-gate 	}
26910Sstevel@tonic-gate 	crypto_release_minor(cm);
26920Sstevel@tonic-gate 
26930Sstevel@tonic-gate 	if (data.cd_raw.iov_base != NULL)
26940Sstevel@tonic-gate 		kmem_free(data.cd_raw.iov_base, datalen);
26950Sstevel@tonic-gate 
26960Sstevel@tonic-gate 	if (encr.cd_raw.iov_base != NULL)
26970Sstevel@tonic-gate 		kmem_free(encr.cd_raw.iov_base, encrlen);
26980Sstevel@tonic-gate 
26990Sstevel@tonic-gate 	if (error != 0)
27000Sstevel@tonic-gate 		return (error);
27010Sstevel@tonic-gate 
27020Sstevel@tonic-gate 	STRUCT_FSET(encrypt_update, eu_return_value, rv);
27030Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(encrypt_update), arg,
27040Sstevel@tonic-gate 	    STRUCT_SIZE(encrypt_update)) != 0) {
27050Sstevel@tonic-gate 		return (EFAULT);
27060Sstevel@tonic-gate 	}
27070Sstevel@tonic-gate 	return (0);
27080Sstevel@tonic-gate }
27090Sstevel@tonic-gate 
27100Sstevel@tonic-gate /* ARGSUSED */
27110Sstevel@tonic-gate static int
27120Sstevel@tonic-gate encrypt_final(dev_t dev, caddr_t arg, int mode, int *rval)
27130Sstevel@tonic-gate {
27140Sstevel@tonic-gate 	return (common_final(dev, arg, mode, crypto_encrypt_final));
27150Sstevel@tonic-gate }
27160Sstevel@tonic-gate 
27170Sstevel@tonic-gate /* ARGSUSED */
27180Sstevel@tonic-gate static int
27190Sstevel@tonic-gate decrypt_final(dev_t dev, caddr_t arg, int mode, int *rval)
27200Sstevel@tonic-gate {
27210Sstevel@tonic-gate 	return (common_final(dev, arg, mode, crypto_decrypt_final));
27220Sstevel@tonic-gate }
27230Sstevel@tonic-gate 
27240Sstevel@tonic-gate /*
27250Sstevel@tonic-gate  * ASSUMPTION: crypto_encrypt_final, crypto_decrypt_final, crypto_sign_final,
27260Sstevel@tonic-gate  * and crypto_digest_final structures are identical except for field names.
27270Sstevel@tonic-gate  */
27280Sstevel@tonic-gate static int
27290Sstevel@tonic-gate common_final(dev_t dev, caddr_t arg, int mode,
27300Sstevel@tonic-gate     int (*final)(crypto_context_t, crypto_data_t *, crypto_call_req_t *))
27310Sstevel@tonic-gate {
27320Sstevel@tonic-gate 	STRUCT_DECL(crypto_encrypt_final, encrypt_final);
27330Sstevel@tonic-gate 	crypto_session_id_t session_id;
27340Sstevel@tonic-gate 	crypto_minor_t *cm;
27350Sstevel@tonic-gate 	crypto_session_data_t *sp;
27360Sstevel@tonic-gate 	crypto_ctx_t **ctxpp;
27370Sstevel@tonic-gate 	crypto_data_t encr;
27380Sstevel@tonic-gate 	size_t encrlen, need = 0;
27390Sstevel@tonic-gate 	char *encrbuf;
27400Sstevel@tonic-gate 	int error = 0;
27410Sstevel@tonic-gate 	int rv;
27420Sstevel@tonic-gate 
27430Sstevel@tonic-gate 	STRUCT_INIT(encrypt_final, mode);
27440Sstevel@tonic-gate 
27450Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
27460Sstevel@tonic-gate 		cmn_err(CE_WARN, "common_final: failed holding minor");
27470Sstevel@tonic-gate 		return (ENXIO);
27480Sstevel@tonic-gate 	}
27490Sstevel@tonic-gate 
27500Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(encrypt_final),
27510Sstevel@tonic-gate 	    STRUCT_SIZE(encrypt_final)) != 0) {
27520Sstevel@tonic-gate 		crypto_release_minor(cm);
27530Sstevel@tonic-gate 		return (EFAULT);
27540Sstevel@tonic-gate 	}
27550Sstevel@tonic-gate 
27560Sstevel@tonic-gate 	encr.cd_format = CRYPTO_DATA_RAW;
27570Sstevel@tonic-gate 	encr.cd_raw.iov_base = NULL;
27580Sstevel@tonic-gate 
27590Sstevel@tonic-gate 	encrlen = STRUCT_FGET(encrypt_final, ef_encrlen);
27600Sstevel@tonic-gate 
27610Sstevel@tonic-gate 	/*
27620Sstevel@tonic-gate 	 * Don't allocate output buffer unless both buffer pointer and
27630Sstevel@tonic-gate 	 * buffer length are not NULL or 0 (length).
27640Sstevel@tonic-gate 	 */
27650Sstevel@tonic-gate 	encrbuf = STRUCT_FGETP(encrypt_final, ef_encrbuf);
27660Sstevel@tonic-gate 	if (encrbuf == NULL || encrlen == 0) {
27670Sstevel@tonic-gate 		encrlen = 0;
27680Sstevel@tonic-gate 	}
27690Sstevel@tonic-gate 
27700Sstevel@tonic-gate 	if (encrlen > crypto_max_buffer_len) {
27710Sstevel@tonic-gate 		cmn_err(CE_NOTE, "common_final: buffer greater than %ld "
27720Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
27730Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
27740Sstevel@tonic-gate 		goto release_minor;
27750Sstevel@tonic-gate 	}
27760Sstevel@tonic-gate 
27773916Skrishna 	if ((rv = crypto_buffer_check(encrlen)) != CRYPTO_SUCCESS) {
27780Sstevel@tonic-gate 		goto release_minor;
27790Sstevel@tonic-gate 	}
27800Sstevel@tonic-gate 	need = encrlen;
27810Sstevel@tonic-gate 	encr.cd_raw.iov_base = kmem_alloc(encrlen, KM_SLEEP);
27820Sstevel@tonic-gate 	encr.cd_raw.iov_len = encrlen;
27830Sstevel@tonic-gate 
27840Sstevel@tonic-gate 	encr.cd_offset = 0;
27850Sstevel@tonic-gate 	encr.cd_length = encrlen;
27860Sstevel@tonic-gate 
27870Sstevel@tonic-gate 	session_id = STRUCT_FGET(encrypt_final, ef_session);
27880Sstevel@tonic-gate 
27890Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
27900Sstevel@tonic-gate 		goto release_minor;
27910Sstevel@tonic-gate 	}
27920Sstevel@tonic-gate 
27930Sstevel@tonic-gate 	ASSERT(final == crypto_encrypt_final ||
27940Sstevel@tonic-gate 	    final == crypto_decrypt_final || final == crypto_sign_final ||
27950Sstevel@tonic-gate 	    final == crypto_digest_final);
27960Sstevel@tonic-gate 
27970Sstevel@tonic-gate 	if (final == crypto_encrypt_final) {
27980Sstevel@tonic-gate 		ctxpp = &sp->sd_encr_ctx;
27990Sstevel@tonic-gate 	} else if (final == crypto_decrypt_final) {
28000Sstevel@tonic-gate 		ctxpp = &sp->sd_decr_ctx;
28010Sstevel@tonic-gate 	} else if (final == crypto_sign_final) {
28020Sstevel@tonic-gate 		ctxpp = &sp->sd_sign_ctx;
28030Sstevel@tonic-gate 	} else {
28040Sstevel@tonic-gate 		ctxpp = &sp->sd_digest_ctx;
28050Sstevel@tonic-gate 	}
28060Sstevel@tonic-gate 
28070Sstevel@tonic-gate 	rv = (final)(*ctxpp, &encr, NULL);
28080Sstevel@tonic-gate 	if (KCF_CONTEXT_DONE(rv))
28090Sstevel@tonic-gate 		*ctxpp = NULL;
28100Sstevel@tonic-gate 
28110Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
28120Sstevel@tonic-gate 
28130Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
28140Sstevel@tonic-gate 		ASSERT(encr.cd_length <= encrlen);
28150Sstevel@tonic-gate 		if (encr.cd_length != 0 && copyout(encr.cd_raw.iov_base,
28160Sstevel@tonic-gate 		    encrbuf, encr.cd_length) != 0) {
28170Sstevel@tonic-gate 			error = EFAULT;
28180Sstevel@tonic-gate 			goto release_minor;
28190Sstevel@tonic-gate 		}
28200Sstevel@tonic-gate 		STRUCT_FSET(encrypt_final, ef_encrlen, encr.cd_length);
28210Sstevel@tonic-gate 	}
28220Sstevel@tonic-gate 
28230Sstevel@tonic-gate 	if (rv == CRYPTO_BUFFER_TOO_SMALL) {
28240Sstevel@tonic-gate 		/*
28250Sstevel@tonic-gate 		 * The providers return CRYPTO_BUFFER_TOO_SMALL even for case 1
28260Sstevel@tonic-gate 		 * of section 11.2 of the pkcs11 spec. We catch it here and
28270Sstevel@tonic-gate 		 * provide the correct pkcs11 return value.
28280Sstevel@tonic-gate 		 */
28290Sstevel@tonic-gate 		if (STRUCT_FGETP(encrypt_final, ef_encrbuf) == NULL)
28300Sstevel@tonic-gate 			rv = CRYPTO_SUCCESS;
28310Sstevel@tonic-gate 		STRUCT_FSET(encrypt_final, ef_encrlen, encr.cd_length);
28320Sstevel@tonic-gate 	}
28330Sstevel@tonic-gate 
28340Sstevel@tonic-gate release_minor:
28350Sstevel@tonic-gate 	if (need != 0) {
28363916Skrishna 		CRYPTO_DECREMENT_RCTL(need);
28370Sstevel@tonic-gate 	}
28380Sstevel@tonic-gate 	crypto_release_minor(cm);
28390Sstevel@tonic-gate 
28400Sstevel@tonic-gate 	if (encr.cd_raw.iov_base != NULL)
28410Sstevel@tonic-gate 		kmem_free(encr.cd_raw.iov_base, encrlen);
28420Sstevel@tonic-gate 
28430Sstevel@tonic-gate 	if (error != 0)
28440Sstevel@tonic-gate 		return (error);
28450Sstevel@tonic-gate 
28460Sstevel@tonic-gate 	STRUCT_FSET(encrypt_final, ef_return_value, rv);
28470Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(encrypt_final), arg,
28480Sstevel@tonic-gate 	    STRUCT_SIZE(encrypt_final)) != 0) {
28490Sstevel@tonic-gate 		return (EFAULT);
28500Sstevel@tonic-gate 	}
28510Sstevel@tonic-gate 	return (0);
28520Sstevel@tonic-gate }
28530Sstevel@tonic-gate 
28540Sstevel@tonic-gate /* ARGSUSED */
28550Sstevel@tonic-gate static int
28560Sstevel@tonic-gate digest_init(dev_t dev, caddr_t arg, int mode, int *rval)
28570Sstevel@tonic-gate {
28580Sstevel@tonic-gate 	STRUCT_DECL(crypto_digest_init, digest_init);
2859904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
28600Sstevel@tonic-gate 	crypto_session_id_t session_id;
28610Sstevel@tonic-gate 	crypto_mechanism_t mech;
28620Sstevel@tonic-gate 	crypto_minor_t *cm;
28630Sstevel@tonic-gate 	crypto_session_data_t *sp;
28640Sstevel@tonic-gate 	crypto_context_t cc;
28650Sstevel@tonic-gate 	size_t rctl_bytes = 0;
28660Sstevel@tonic-gate 	int error = 0;
28670Sstevel@tonic-gate 	int rv;
28680Sstevel@tonic-gate 
28690Sstevel@tonic-gate 	STRUCT_INIT(digest_init, mode);
28700Sstevel@tonic-gate 
28710Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
28720Sstevel@tonic-gate 		cmn_err(CE_WARN, "digest_init: failed holding minor");
28730Sstevel@tonic-gate 		return (ENXIO);
28740Sstevel@tonic-gate 	}
28750Sstevel@tonic-gate 
28760Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(digest_init),
28770Sstevel@tonic-gate 	    STRUCT_SIZE(digest_init)) != 0) {
28780Sstevel@tonic-gate 		crypto_release_minor(cm);
28790Sstevel@tonic-gate 		return (EFAULT);
28800Sstevel@tonic-gate 	}
28810Sstevel@tonic-gate 
28820Sstevel@tonic-gate 	mech.cm_param = NULL;
28830Sstevel@tonic-gate 
28840Sstevel@tonic-gate 	session_id = STRUCT_FGET(digest_init, di_session);
28850Sstevel@tonic-gate 
28860Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv))  {
28870Sstevel@tonic-gate 		goto release_minor;
28880Sstevel@tonic-gate 	}
28890Sstevel@tonic-gate 
28900Sstevel@tonic-gate 	if (!copyin_mech(mode, STRUCT_FADDR(digest_init, di_mech), &mech,
28913916Skrishna 	    &rctl_bytes, NULL, &rv, &error)) {
28920Sstevel@tonic-gate 		goto out;
28930Sstevel@tonic-gate 	}
28940Sstevel@tonic-gate 
2895904Smcpowers 	if ((rv = kcf_get_hardware_provider(mech.cm_type, CRYPTO_MECH_INVALID,
28961808Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider,
28971808Smcpowers 	    CRYPTO_FG_DIGEST)) != CRYPTO_SUCCESS) {
28980Sstevel@tonic-gate 		goto out;
28990Sstevel@tonic-gate 	}
29000Sstevel@tonic-gate 
29010Sstevel@tonic-gate 	rv = crypto_digest_init_prov(real_provider,
29020Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, &mech, &cc, NULL);
29030Sstevel@tonic-gate 
29040Sstevel@tonic-gate 	/*
29050Sstevel@tonic-gate 	 * Check if a context already exists. If so, it means it is being
29060Sstevel@tonic-gate 	 * abandoned. So, cancel it to avoid leaking it.
29070Sstevel@tonic-gate 	 */
29080Sstevel@tonic-gate 	if (sp->sd_digest_ctx != NULL)
29090Sstevel@tonic-gate 		CRYPTO_CANCEL_CTX(&sp->sd_digest_ctx);
29100Sstevel@tonic-gate 	sp->sd_digest_ctx = (rv == CRYPTO_SUCCESS) ? cc : NULL;
29110Sstevel@tonic-gate out:
29120Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
29130Sstevel@tonic-gate 
29140Sstevel@tonic-gate release_minor:
29150Sstevel@tonic-gate 	if (rctl_bytes != 0) {
29163916Skrishna 		CRYPTO_DECREMENT_RCTL(rctl_bytes);
29170Sstevel@tonic-gate 	}
29180Sstevel@tonic-gate 	crypto_release_minor(cm);
29190Sstevel@tonic-gate 
2920904Smcpowers 	if (real_provider != NULL)
2921904Smcpowers 		KCF_PROV_REFRELE(real_provider);
2922904Smcpowers 
29230Sstevel@tonic-gate 	if (mech.cm_param != NULL)
29240Sstevel@tonic-gate 		kmem_free(mech.cm_param, mech.cm_param_len);
29250Sstevel@tonic-gate 
29260Sstevel@tonic-gate 	if (error != 0)
29270Sstevel@tonic-gate 		return (error);
29280Sstevel@tonic-gate 
29290Sstevel@tonic-gate 	STRUCT_FSET(digest_init, di_return_value, rv);
29300Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(digest_init), arg,
29310Sstevel@tonic-gate 	    STRUCT_SIZE(digest_init)) != 0) {
29320Sstevel@tonic-gate 		return (EFAULT);
29330Sstevel@tonic-gate 	}
29340Sstevel@tonic-gate 	return (0);
29350Sstevel@tonic-gate }
29360Sstevel@tonic-gate 
29370Sstevel@tonic-gate /* ARGSUSED */
29380Sstevel@tonic-gate static int
29390Sstevel@tonic-gate digest_update(dev_t dev, caddr_t arg, int mode, int *rval)
29400Sstevel@tonic-gate {
29410Sstevel@tonic-gate 	STRUCT_DECL(crypto_digest_update, digest_update);
29420Sstevel@tonic-gate 	crypto_session_id_t session_id;
29430Sstevel@tonic-gate 	crypto_minor_t *cm;
29440Sstevel@tonic-gate 	crypto_session_data_t *sp;
29450Sstevel@tonic-gate 	crypto_data_t data;
29460Sstevel@tonic-gate 	size_t datalen, need = 0;
29470Sstevel@tonic-gate 	int error = 0;
29480Sstevel@tonic-gate 	int rv;
29490Sstevel@tonic-gate 
29500Sstevel@tonic-gate 	STRUCT_INIT(digest_update, mode);
29510Sstevel@tonic-gate 
29520Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
29530Sstevel@tonic-gate 		cmn_err(CE_WARN, "digest_update: failed holding minor");
29540Sstevel@tonic-gate 		return (ENXIO);
29550Sstevel@tonic-gate 	}
29560Sstevel@tonic-gate 
29570Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(digest_update),
29580Sstevel@tonic-gate 	    STRUCT_SIZE(digest_update)) != 0) {
29590Sstevel@tonic-gate 		crypto_release_minor(cm);
29600Sstevel@tonic-gate 		return (EFAULT);
29610Sstevel@tonic-gate 	}
29620Sstevel@tonic-gate 
29630Sstevel@tonic-gate 	data.cd_format = CRYPTO_DATA_RAW;
29640Sstevel@tonic-gate 	data.cd_raw.iov_base = NULL;
29650Sstevel@tonic-gate 
29660Sstevel@tonic-gate 	datalen = STRUCT_FGET(digest_update, du_datalen);
29670Sstevel@tonic-gate 	if (datalen > crypto_max_buffer_len) {
29680Sstevel@tonic-gate 		cmn_err(CE_NOTE, "digest_update: buffer greater than %ld "
29690Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
29700Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
29710Sstevel@tonic-gate 		goto release_minor;
29720Sstevel@tonic-gate 	}
29730Sstevel@tonic-gate 
29743916Skrishna 	if ((rv = crypto_buffer_check(datalen)) != CRYPTO_SUCCESS) {
29750Sstevel@tonic-gate 		goto release_minor;
29760Sstevel@tonic-gate 	}
29770Sstevel@tonic-gate 	need = datalen;
29780Sstevel@tonic-gate 	data.cd_raw.iov_base = kmem_alloc(datalen, KM_SLEEP);
29790Sstevel@tonic-gate 	data.cd_raw.iov_len = datalen;
29800Sstevel@tonic-gate 
29810Sstevel@tonic-gate 	if (datalen != 0 && copyin(STRUCT_FGETP(digest_update, du_databuf),
29820Sstevel@tonic-gate 	    data.cd_raw.iov_base, datalen) != 0) {
29830Sstevel@tonic-gate 		error = EFAULT;
29840Sstevel@tonic-gate 		goto release_minor;
29850Sstevel@tonic-gate 	}
29860Sstevel@tonic-gate 
29870Sstevel@tonic-gate 	data.cd_offset = 0;
29880Sstevel@tonic-gate 	data.cd_length = datalen;
29890Sstevel@tonic-gate 
29900Sstevel@tonic-gate 	session_id = STRUCT_FGET(digest_update, du_session);
29910Sstevel@tonic-gate 
29920Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv))  {
29930Sstevel@tonic-gate 		goto release_minor;
29940Sstevel@tonic-gate 	}
29950Sstevel@tonic-gate 
29960Sstevel@tonic-gate 	rv = crypto_digest_update(sp->sd_digest_ctx, &data, NULL);
29970Sstevel@tonic-gate 	if (rv != CRYPTO_SUCCESS)
29980Sstevel@tonic-gate 		CRYPTO_CANCEL_CTX(&sp->sd_digest_ctx);
29990Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
30000Sstevel@tonic-gate 
30010Sstevel@tonic-gate release_minor:
30020Sstevel@tonic-gate 	if (need != 0) {
30033916Skrishna 		CRYPTO_DECREMENT_RCTL(need);
30040Sstevel@tonic-gate 	}
30050Sstevel@tonic-gate 	crypto_release_minor(cm);
30060Sstevel@tonic-gate 
30070Sstevel@tonic-gate 	if (data.cd_raw.iov_base != NULL)
30080Sstevel@tonic-gate 		kmem_free(data.cd_raw.iov_base, datalen);
30090Sstevel@tonic-gate 
30100Sstevel@tonic-gate 	if (error != 0)
30110Sstevel@tonic-gate 		return (error);
30120Sstevel@tonic-gate 
30130Sstevel@tonic-gate 	STRUCT_FSET(digest_update, du_return_value, rv);
30140Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(digest_update), arg,
30150Sstevel@tonic-gate 	    STRUCT_SIZE(digest_update)) != 0) {
30160Sstevel@tonic-gate 		return (EFAULT);
30170Sstevel@tonic-gate 	}
30180Sstevel@tonic-gate 	return (0);
30190Sstevel@tonic-gate }
30200Sstevel@tonic-gate 
30210Sstevel@tonic-gate /* ARGSUSED */
30220Sstevel@tonic-gate static int
30230Sstevel@tonic-gate digest_key(dev_t dev, caddr_t arg, int mode, int *rval)
30240Sstevel@tonic-gate {
30250Sstevel@tonic-gate 	STRUCT_DECL(crypto_digest_key, digest_key);
30260Sstevel@tonic-gate 	crypto_session_id_t session_id;
30270Sstevel@tonic-gate 	crypto_key_t key;
30280Sstevel@tonic-gate 	crypto_minor_t *cm;
30290Sstevel@tonic-gate 	crypto_session_data_t *sp;
30300Sstevel@tonic-gate 	size_t rctl_bytes = 0;
30310Sstevel@tonic-gate 	int error = 0;
30320Sstevel@tonic-gate 	int rv;
30330Sstevel@tonic-gate 
30340Sstevel@tonic-gate 	STRUCT_INIT(digest_key, mode);
30350Sstevel@tonic-gate 
30360Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
30370Sstevel@tonic-gate 		cmn_err(CE_WARN, "digest_key: failed holding minor");
30380Sstevel@tonic-gate 		return (ENXIO);
30390Sstevel@tonic-gate 	}
30400Sstevel@tonic-gate 
30410Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(digest_key), STRUCT_SIZE(digest_key)) != 0) {
30420Sstevel@tonic-gate 		crypto_release_minor(cm);
30430Sstevel@tonic-gate 		return (EFAULT);
30440Sstevel@tonic-gate 	}
30450Sstevel@tonic-gate 
30460Sstevel@tonic-gate 	bzero(&key, sizeof (crypto_key_t));
30470Sstevel@tonic-gate 
30480Sstevel@tonic-gate 	session_id = STRUCT_FGET(digest_key, dk_session);
30490Sstevel@tonic-gate 
30500Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv))  {
30510Sstevel@tonic-gate 		goto release_minor;
30520Sstevel@tonic-gate 	}
30530Sstevel@tonic-gate 
30540Sstevel@tonic-gate 	if (!copyin_key(mode, STRUCT_FADDR(digest_key, dk_key), &key,
30553916Skrishna 	    &rctl_bytes, &rv, &error, 0)) {
30560Sstevel@tonic-gate 		goto out;
30570Sstevel@tonic-gate 	}
30580Sstevel@tonic-gate 
30590Sstevel@tonic-gate 	rv = crypto_digest_key_prov(sp->sd_digest_ctx, &key, NULL);
30600Sstevel@tonic-gate 	if (rv != CRYPTO_SUCCESS)
30610Sstevel@tonic-gate 		CRYPTO_CANCEL_CTX(&sp->sd_digest_ctx);
30620Sstevel@tonic-gate out:
30630Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
30640Sstevel@tonic-gate 
30650Sstevel@tonic-gate release_minor:
30660Sstevel@tonic-gate 	if (rctl_bytes != 0) {
30673916Skrishna 		CRYPTO_DECREMENT_RCTL(rctl_bytes);
30680Sstevel@tonic-gate 	}
30690Sstevel@tonic-gate 	crypto_release_minor(cm);
30700Sstevel@tonic-gate 
30710Sstevel@tonic-gate 	free_crypto_key(&key);
30720Sstevel@tonic-gate 
30730Sstevel@tonic-gate 	if (error != 0)
30740Sstevel@tonic-gate 		return (error);
30750Sstevel@tonic-gate 
30760Sstevel@tonic-gate 	STRUCT_FSET(digest_key, dk_return_value, rv);
30770Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(digest_key), arg,
30780Sstevel@tonic-gate 	    STRUCT_SIZE(digest_key)) != 0) {
30790Sstevel@tonic-gate 		return (EFAULT);
30800Sstevel@tonic-gate 	}
30810Sstevel@tonic-gate 	return (0);
30820Sstevel@tonic-gate }
30830Sstevel@tonic-gate 
30840Sstevel@tonic-gate /* ARGSUSED */
30850Sstevel@tonic-gate static int
30860Sstevel@tonic-gate digest_final(dev_t dev, caddr_t arg, int mode, int *rval)
30870Sstevel@tonic-gate {
30880Sstevel@tonic-gate 	return (common_final(dev, arg, mode, crypto_digest_final));
30890Sstevel@tonic-gate }
30900Sstevel@tonic-gate 
30910Sstevel@tonic-gate /* ARGSUSED */
30920Sstevel@tonic-gate static int
30930Sstevel@tonic-gate digest(dev_t dev, caddr_t arg, int mode, int *rval)
30940Sstevel@tonic-gate {
30950Sstevel@tonic-gate 	return (common_digest(dev, arg, mode, crypto_digest_single));
30960Sstevel@tonic-gate }
30970Sstevel@tonic-gate 
30980Sstevel@tonic-gate /*
30990Sstevel@tonic-gate  * ASSUMPTION: crypto_digest, crypto_sign, crypto_sign_recover,
31000Sstevel@tonic-gate  * and crypto_verify_recover are identical except for field names.
31010Sstevel@tonic-gate  */
31020Sstevel@tonic-gate static int
31030Sstevel@tonic-gate common_digest(dev_t dev, caddr_t arg, int mode,
31040Sstevel@tonic-gate     int (*single)(crypto_context_t, crypto_data_t *, crypto_data_t *,
31050Sstevel@tonic-gate     crypto_call_req_t *))
31060Sstevel@tonic-gate {
31070Sstevel@tonic-gate 	STRUCT_DECL(crypto_digest, crypto_digest);
31080Sstevel@tonic-gate 	crypto_session_id_t session_id;
31090Sstevel@tonic-gate 	crypto_minor_t *cm;
31100Sstevel@tonic-gate 	crypto_session_data_t *sp;
31110Sstevel@tonic-gate 	crypto_data_t data, digest;
31120Sstevel@tonic-gate 	crypto_ctx_t **ctxpp;
31130Sstevel@tonic-gate 	size_t datalen, digestlen, need = 0;
31140Sstevel@tonic-gate 	char *digestbuf;
31150Sstevel@tonic-gate 	int error = 0;
31160Sstevel@tonic-gate 	int rv;
31170Sstevel@tonic-gate 
31180Sstevel@tonic-gate 	STRUCT_INIT(crypto_digest, mode);
31190Sstevel@tonic-gate 
31200Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
31210Sstevel@tonic-gate 		cmn_err(CE_WARN, "common_digest: failed holding minor");
31220Sstevel@tonic-gate 		return (ENXIO);
31230Sstevel@tonic-gate 	}
31240Sstevel@tonic-gate 
31250Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(crypto_digest),
31260Sstevel@tonic-gate 	    STRUCT_SIZE(crypto_digest)) != 0) {
31270Sstevel@tonic-gate 		crypto_release_minor(cm);
31280Sstevel@tonic-gate 		return (EFAULT);
31290Sstevel@tonic-gate 	}
31300Sstevel@tonic-gate 
31310Sstevel@tonic-gate 	data.cd_raw.iov_base = NULL;
31320Sstevel@tonic-gate 	digest.cd_raw.iov_base = NULL;
31330Sstevel@tonic-gate 
31340Sstevel@tonic-gate 	datalen = STRUCT_FGET(crypto_digest, cd_datalen);
31350Sstevel@tonic-gate 	digestlen = STRUCT_FGET(crypto_digest, cd_digestlen);
31360Sstevel@tonic-gate 
31370Sstevel@tonic-gate 	/*
31380Sstevel@tonic-gate 	 * Don't allocate output buffer unless both buffer pointer and
31390Sstevel@tonic-gate 	 * buffer length are not NULL or 0 (length).
31400Sstevel@tonic-gate 	 */
31410Sstevel@tonic-gate 	digestbuf = STRUCT_FGETP(crypto_digest, cd_digestbuf);
31420Sstevel@tonic-gate 	if (digestbuf == NULL || digestlen == 0) {
31430Sstevel@tonic-gate 		digestlen = 0;
31440Sstevel@tonic-gate 	}
31450Sstevel@tonic-gate 
31460Sstevel@tonic-gate 	if (datalen > crypto_max_buffer_len ||
31470Sstevel@tonic-gate 	    digestlen > crypto_max_buffer_len) {
31480Sstevel@tonic-gate 		cmn_err(CE_NOTE, "common_digest: buffer greater than %ld "
31490Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
31500Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
31510Sstevel@tonic-gate 		goto release_minor;
31520Sstevel@tonic-gate 	}
31530Sstevel@tonic-gate 
31540Sstevel@tonic-gate 	need = datalen + digestlen;
31553916Skrishna 	if ((rv = crypto_buffer_check(need)) != CRYPTO_SUCCESS) {
31560Sstevel@tonic-gate 		need = 0;
31570Sstevel@tonic-gate 		goto release_minor;
31580Sstevel@tonic-gate 	}
31590Sstevel@tonic-gate 
31600Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(data, datalen);
31610Sstevel@tonic-gate 
31620Sstevel@tonic-gate 	if (datalen != 0 && copyin(STRUCT_FGETP(crypto_digest, cd_databuf),
31630Sstevel@tonic-gate 	    data.cd_raw.iov_base, datalen) != 0) {
31640Sstevel@tonic-gate 		error = EFAULT;
31650Sstevel@tonic-gate 		goto release_minor;
31660Sstevel@tonic-gate 	}
31670Sstevel@tonic-gate 
31680Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(digest, digestlen);
31690Sstevel@tonic-gate 
31700Sstevel@tonic-gate 	session_id = STRUCT_FGET(crypto_digest, cd_session);
31710Sstevel@tonic-gate 
31720Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv))  {
31730Sstevel@tonic-gate 		goto release_minor;
31740Sstevel@tonic-gate 	}
31750Sstevel@tonic-gate 
31760Sstevel@tonic-gate 	ASSERT(single == crypto_digest_single ||
31770Sstevel@tonic-gate 	    single == crypto_sign_single ||
31780Sstevel@tonic-gate 	    single == crypto_verify_recover_single ||
31790Sstevel@tonic-gate 	    single == crypto_sign_recover_single);
31800Sstevel@tonic-gate 
31810Sstevel@tonic-gate 	if (single == crypto_digest_single) {
31820Sstevel@tonic-gate 		ctxpp = &sp->sd_digest_ctx;
31830Sstevel@tonic-gate 	} else if (single == crypto_sign_single) {
31840Sstevel@tonic-gate 		ctxpp = &sp->sd_sign_ctx;
31850Sstevel@tonic-gate 	} else if (single == crypto_verify_recover_single) {
31860Sstevel@tonic-gate 		ctxpp = &sp->sd_verify_recover_ctx;
31870Sstevel@tonic-gate 	} else {
31880Sstevel@tonic-gate 		ctxpp = &sp->sd_sign_recover_ctx;
31890Sstevel@tonic-gate 	}
31900Sstevel@tonic-gate 	rv = (single)(*ctxpp, &data, &digest, NULL);
31910Sstevel@tonic-gate 	if (KCF_CONTEXT_DONE(rv))
31920Sstevel@tonic-gate 		*ctxpp = NULL;
31930Sstevel@tonic-gate 
31940Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
31950Sstevel@tonic-gate 
31960Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
31970Sstevel@tonic-gate 		ASSERT(digest.cd_length <= digestlen);
31980Sstevel@tonic-gate 		if (digest.cd_length != 0 && copyout(digest.cd_raw.iov_base,
31990Sstevel@tonic-gate 		    digestbuf, digest.cd_length) != 0) {
32000Sstevel@tonic-gate 			error = EFAULT;
32010Sstevel@tonic-gate 			goto release_minor;
32020Sstevel@tonic-gate 		}
32030Sstevel@tonic-gate 		STRUCT_FSET(crypto_digest, cd_digestlen, digest.cd_length);
32040Sstevel@tonic-gate 	}
32050Sstevel@tonic-gate 
32060Sstevel@tonic-gate 	if (rv == CRYPTO_BUFFER_TOO_SMALL) {
32070Sstevel@tonic-gate 		/*
32080Sstevel@tonic-gate 		 * The providers return CRYPTO_BUFFER_TOO_SMALL even for case 1
32090Sstevel@tonic-gate 		 * of section 11.2 of the pkcs11 spec. We catch it here and
32100Sstevel@tonic-gate 		 * provide the correct pkcs11 return value.
32110Sstevel@tonic-gate 		 */
32120Sstevel@tonic-gate 		if (STRUCT_FGETP(crypto_digest, cd_digestbuf) == NULL)
32130Sstevel@tonic-gate 			rv = CRYPTO_SUCCESS;
32140Sstevel@tonic-gate 		STRUCT_FSET(crypto_digest, cd_digestlen, digest.cd_length);
32150Sstevel@tonic-gate 	}
32160Sstevel@tonic-gate 
32170Sstevel@tonic-gate release_minor:
32180Sstevel@tonic-gate 	if (need != 0) {
32193916Skrishna 		CRYPTO_DECREMENT_RCTL(need);
32200Sstevel@tonic-gate 	}
32210Sstevel@tonic-gate 	crypto_release_minor(cm);
32220Sstevel@tonic-gate 
32230Sstevel@tonic-gate 	if (data.cd_raw.iov_base != NULL)
32240Sstevel@tonic-gate 		kmem_free(data.cd_raw.iov_base, datalen);
32250Sstevel@tonic-gate 
32260Sstevel@tonic-gate 	if (digest.cd_raw.iov_base != NULL)
32270Sstevel@tonic-gate 		kmem_free(digest.cd_raw.iov_base, digestlen);
32280Sstevel@tonic-gate 
32290Sstevel@tonic-gate 	if (error != 0)
32300Sstevel@tonic-gate 		return (error);
32310Sstevel@tonic-gate 
32320Sstevel@tonic-gate 	STRUCT_FSET(crypto_digest, cd_return_value, rv);
32330Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(crypto_digest), arg,
32340Sstevel@tonic-gate 	    STRUCT_SIZE(crypto_digest)) != 0) {
32350Sstevel@tonic-gate 		return (EFAULT);
32360Sstevel@tonic-gate 	}
32370Sstevel@tonic-gate 	return (0);
32380Sstevel@tonic-gate }
32390Sstevel@tonic-gate 
32400Sstevel@tonic-gate /*
32410Sstevel@tonic-gate  * A helper function that does what the name suggests.
32420Sstevel@tonic-gate  * Returns 0 on success and non-zero otherwise.
32430Sstevel@tonic-gate  * On failure, out_pin is set to 0.
32440Sstevel@tonic-gate  */
32450Sstevel@tonic-gate int
32460Sstevel@tonic-gate get_pin_and_session_ptr(char *in_pin, char **out_pin, size_t pin_len,
32470Sstevel@tonic-gate     crypto_minor_t *cm, crypto_session_id_t sid, crypto_session_data_t **sp,
32480Sstevel@tonic-gate     int *rv, int *error)
32490Sstevel@tonic-gate {
32500Sstevel@tonic-gate 	char *tmp_pin = NULL;
32510Sstevel@tonic-gate 	int tmp_error = 0, tmp_rv = 0;
32520Sstevel@tonic-gate 
32530Sstevel@tonic-gate 	if (pin_len > KCF_MAX_PIN_LEN) {
32540Sstevel@tonic-gate 		tmp_rv = CRYPTO_PIN_LEN_RANGE;
32550Sstevel@tonic-gate 		goto out;
32560Sstevel@tonic-gate 	}
32570Sstevel@tonic-gate 	tmp_pin = kmem_alloc(pin_len, KM_SLEEP);
32580Sstevel@tonic-gate 
32590Sstevel@tonic-gate 	if (pin_len != 0 && copyin(in_pin, tmp_pin, pin_len) != 0) {
32600Sstevel@tonic-gate 		tmp_error = EFAULT;
32610Sstevel@tonic-gate 		goto out;
32620Sstevel@tonic-gate 	}
32630Sstevel@tonic-gate 
32640Sstevel@tonic-gate 	(void) get_session_ptr(sid, cm, sp, &tmp_error, &tmp_rv);
32650Sstevel@tonic-gate out:
32660Sstevel@tonic-gate 	*out_pin = tmp_pin;
32670Sstevel@tonic-gate 	*rv = tmp_rv;
32680Sstevel@tonic-gate 	*error = tmp_error;
32690Sstevel@tonic-gate 	return (tmp_rv | tmp_error);
32700Sstevel@tonic-gate }
32710Sstevel@tonic-gate 
32720Sstevel@tonic-gate /* ARGSUSED */
32730Sstevel@tonic-gate static int
32740Sstevel@tonic-gate set_pin(dev_t dev, caddr_t arg, int mode, int *rval)
32750Sstevel@tonic-gate {
32760Sstevel@tonic-gate 	STRUCT_DECL(crypto_set_pin, set_pin);
32770Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
32780Sstevel@tonic-gate 	kcf_req_params_t params;
32790Sstevel@tonic-gate 	crypto_minor_t *cm;
32800Sstevel@tonic-gate 	crypto_session_data_t *sp;
32810Sstevel@tonic-gate 	char *old_pin = NULL;
32820Sstevel@tonic-gate 	char *new_pin = NULL;
32830Sstevel@tonic-gate 	size_t old_pin_len;
32840Sstevel@tonic-gate 	size_t new_pin_len;
32850Sstevel@tonic-gate 	int error = 0;
32860Sstevel@tonic-gate 	int rv;
32870Sstevel@tonic-gate 
32880Sstevel@tonic-gate 	STRUCT_INIT(set_pin, mode);
32890Sstevel@tonic-gate 
32900Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
32910Sstevel@tonic-gate 		cmn_err(CE_WARN, "set_pin: failed holding minor");
32920Sstevel@tonic-gate 		return (ENXIO);
32930Sstevel@tonic-gate 	}
32940Sstevel@tonic-gate 
32950Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(set_pin),
32960Sstevel@tonic-gate 	    STRUCT_SIZE(set_pin)) != 0) {
32970Sstevel@tonic-gate 		crypto_release_minor(cm);
32980Sstevel@tonic-gate 		return (EFAULT);
32990Sstevel@tonic-gate 	}
33000Sstevel@tonic-gate 
33010Sstevel@tonic-gate 	old_pin_len = STRUCT_FGET(set_pin, sp_old_len);
33020Sstevel@tonic-gate 
33030Sstevel@tonic-gate 	if (get_pin_and_session_ptr(STRUCT_FGETP(set_pin, sp_old_pin),
33040Sstevel@tonic-gate 	    &old_pin, old_pin_len, cm, STRUCT_FGET(set_pin, sp_session),
33050Sstevel@tonic-gate 	    &sp, &rv, &error) != 0)
33060Sstevel@tonic-gate 		goto release_minor;
33070Sstevel@tonic-gate 
33080Sstevel@tonic-gate 	new_pin_len = STRUCT_FGET(set_pin, sp_new_len);
33090Sstevel@tonic-gate 	if (new_pin_len > KCF_MAX_PIN_LEN) {
33100Sstevel@tonic-gate 		rv = CRYPTO_PIN_LEN_RANGE;
33110Sstevel@tonic-gate 		goto out;
33120Sstevel@tonic-gate 	}
33130Sstevel@tonic-gate 	new_pin = kmem_alloc(new_pin_len, KM_SLEEP);
33140Sstevel@tonic-gate 
33150Sstevel@tonic-gate 	if (new_pin_len != 0 && copyin(STRUCT_FGETP(set_pin, sp_new_pin),
33160Sstevel@tonic-gate 	    new_pin, new_pin_len) != 0) {
33170Sstevel@tonic-gate 		error = EFAULT;
33180Sstevel@tonic-gate 		goto out;
33190Sstevel@tonic-gate 	}
33200Sstevel@tonic-gate 
33210Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
33220Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(provider_ops), CRYPTO_PROVIDER_OFFSET(set_pin),
3323904Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider))
3324904Smcpowers 	    != CRYPTO_SUCCESS) {
33250Sstevel@tonic-gate 		goto out;
33260Sstevel@tonic-gate 	}
33270Sstevel@tonic-gate 
33280Sstevel@tonic-gate 	KCF_WRAP_PROVMGMT_OPS_PARAMS(&params, KCF_OP_MGMT_SETPIN,
33290Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, old_pin, old_pin_len,
33300Sstevel@tonic-gate 	    new_pin, new_pin_len, NULL, NULL, real_provider);
33310Sstevel@tonic-gate 
33320Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
3333904Smcpowers 	KCF_PROV_REFRELE(real_provider);
33340Sstevel@tonic-gate 
33350Sstevel@tonic-gate out:
33360Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
33370Sstevel@tonic-gate 
33380Sstevel@tonic-gate release_minor:
33390Sstevel@tonic-gate 	crypto_release_minor(cm);
33400Sstevel@tonic-gate 
33410Sstevel@tonic-gate 	if (old_pin != NULL) {
33420Sstevel@tonic-gate 		bzero(old_pin, old_pin_len);
33430Sstevel@tonic-gate 		kmem_free(old_pin, old_pin_len);
33440Sstevel@tonic-gate 	}
33450Sstevel@tonic-gate 
33460Sstevel@tonic-gate 	if (new_pin != NULL) {
33470Sstevel@tonic-gate 		bzero(new_pin, new_pin_len);
33480Sstevel@tonic-gate 		kmem_free(new_pin, new_pin_len);
33490Sstevel@tonic-gate 	}
33500Sstevel@tonic-gate 
33510Sstevel@tonic-gate 	if (error != 0)
33520Sstevel@tonic-gate 		return (error);
33530Sstevel@tonic-gate 
33540Sstevel@tonic-gate 	STRUCT_FSET(set_pin, sp_return_value, rv);
33550Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(set_pin), arg, STRUCT_SIZE(set_pin)) != 0) {
33560Sstevel@tonic-gate 		return (EFAULT);
33570Sstevel@tonic-gate 	}
33580Sstevel@tonic-gate 	return (0);
33590Sstevel@tonic-gate }
33600Sstevel@tonic-gate 
33610Sstevel@tonic-gate /* ARGSUSED */
33620Sstevel@tonic-gate static int
33630Sstevel@tonic-gate login(dev_t dev, caddr_t arg, int mode, int *rval)
33640Sstevel@tonic-gate {
33650Sstevel@tonic-gate 	STRUCT_DECL(crypto_login, login);
33660Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
33670Sstevel@tonic-gate 	kcf_req_params_t params;
33680Sstevel@tonic-gate 	crypto_minor_t *cm;
33690Sstevel@tonic-gate 	crypto_session_data_t *sp;
33700Sstevel@tonic-gate 	size_t pin_len;
33710Sstevel@tonic-gate 	char *pin;
33720Sstevel@tonic-gate 	uint_t user_type;
33730Sstevel@tonic-gate 	int error = 0;
33740Sstevel@tonic-gate 	int rv;
33750Sstevel@tonic-gate 
33760Sstevel@tonic-gate 	STRUCT_INIT(login, mode);
33770Sstevel@tonic-gate 
33780Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
33790Sstevel@tonic-gate 		cmn_err(CE_WARN, "login: failed holding minor");
33800Sstevel@tonic-gate 		return (ENXIO);
33810Sstevel@tonic-gate 	}
33820Sstevel@tonic-gate 
33830Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(login), STRUCT_SIZE(login)) != 0) {
33840Sstevel@tonic-gate 		crypto_release_minor(cm);
33850Sstevel@tonic-gate 		return (EFAULT);
33860Sstevel@tonic-gate 	}
33870Sstevel@tonic-gate 
33880Sstevel@tonic-gate 	user_type = STRUCT_FGET(login, co_user_type);
33890Sstevel@tonic-gate 
33900Sstevel@tonic-gate 	pin_len = STRUCT_FGET(login, co_pin_len);
33910Sstevel@tonic-gate 
33920Sstevel@tonic-gate 	if (get_pin_and_session_ptr(STRUCT_FGETP(login, co_pin),
33930Sstevel@tonic-gate 	    &pin, pin_len, cm, STRUCT_FGET(login, co_session),
33940Sstevel@tonic-gate 	    &sp, &rv, &error) != 0) {
33950Sstevel@tonic-gate 		if (rv == CRYPTO_PIN_LEN_RANGE)
33960Sstevel@tonic-gate 			rv = CRYPTO_PIN_INCORRECT;
33970Sstevel@tonic-gate 		goto release_minor;
33980Sstevel@tonic-gate 	}
33990Sstevel@tonic-gate 
34000Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
34010Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(session_ops),
34020Sstevel@tonic-gate 	    CRYPTO_SESSION_OFFSET(session_login),
3403904Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider))
3404904Smcpowers 	    != CRYPTO_SUCCESS) {
34050Sstevel@tonic-gate 		goto out;
34060Sstevel@tonic-gate 	}
34070Sstevel@tonic-gate 
34080Sstevel@tonic-gate 	KCF_WRAP_SESSION_OPS_PARAMS(&params, KCF_OP_SESSION_LOGIN, NULL,
34090Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, user_type, pin, pin_len,
34100Sstevel@tonic-gate 	    real_provider);
34110Sstevel@tonic-gate 
34120Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
3413904Smcpowers 	KCF_PROV_REFRELE(real_provider);
34140Sstevel@tonic-gate 
34150Sstevel@tonic-gate out:
34160Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
34170Sstevel@tonic-gate 
34180Sstevel@tonic-gate release_minor:
34190Sstevel@tonic-gate 	crypto_release_minor(cm);
34200Sstevel@tonic-gate 
34210Sstevel@tonic-gate 	if (pin != NULL) {
34220Sstevel@tonic-gate 		bzero(pin, pin_len);
34230Sstevel@tonic-gate 		kmem_free(pin, pin_len);
34240Sstevel@tonic-gate 	}
34250Sstevel@tonic-gate 
34260Sstevel@tonic-gate 	if (error != 0)
34270Sstevel@tonic-gate 		return (error);
34280Sstevel@tonic-gate 
34290Sstevel@tonic-gate 	STRUCT_FSET(login, co_return_value, rv);
34300Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(login), arg, STRUCT_SIZE(login)) != 0) {
34310Sstevel@tonic-gate 		return (EFAULT);
34320Sstevel@tonic-gate 	}
34330Sstevel@tonic-gate 	return (0);
34340Sstevel@tonic-gate }
34350Sstevel@tonic-gate 
34360Sstevel@tonic-gate /* ARGSUSED */
34370Sstevel@tonic-gate static int
34380Sstevel@tonic-gate logout(dev_t dev, caddr_t arg, int mode, int *rval)
34390Sstevel@tonic-gate {
34400Sstevel@tonic-gate 	crypto_logout_t logout;
34410Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
34420Sstevel@tonic-gate 	kcf_req_params_t params;
34430Sstevel@tonic-gate 	crypto_minor_t *cm;
34440Sstevel@tonic-gate 	crypto_session_data_t *sp;
34450Sstevel@tonic-gate 	int error = 0;
34460Sstevel@tonic-gate 	int rv;
34470Sstevel@tonic-gate 
34480Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
34490Sstevel@tonic-gate 		cmn_err(CE_WARN, "logout: failed holding minor");
34500Sstevel@tonic-gate 		return (ENXIO);
34510Sstevel@tonic-gate 	}
34520Sstevel@tonic-gate 
34530Sstevel@tonic-gate 	if (copyin(arg, &logout, sizeof (logout)) != 0) {
34540Sstevel@tonic-gate 		crypto_release_minor(cm);
34550Sstevel@tonic-gate 		return (EFAULT);
34560Sstevel@tonic-gate 	}
34570Sstevel@tonic-gate 
34580Sstevel@tonic-gate 	if (!get_session_ptr(logout.cl_session, cm, &sp, &error, &rv))  {
34590Sstevel@tonic-gate 		goto release_minor;
34600Sstevel@tonic-gate 	}
34610Sstevel@tonic-gate 
34620Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
34630Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(session_ops),
3464904Smcpowers 	    CRYPTO_SESSION_OFFSET(session_logout), CHECK_RESTRICT_FALSE,
34650Sstevel@tonic-gate 	    sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
34660Sstevel@tonic-gate 		goto out;
34670Sstevel@tonic-gate 	}
34680Sstevel@tonic-gate 
34690Sstevel@tonic-gate 	KCF_WRAP_SESSION_OPS_PARAMS(&params, KCF_OP_SESSION_LOGOUT, NULL,
34700Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, 0, NULL, 0, real_provider);
34710Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
3472904Smcpowers 	KCF_PROV_REFRELE(real_provider);
34730Sstevel@tonic-gate 
34740Sstevel@tonic-gate out:
34750Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
34760Sstevel@tonic-gate 
34770Sstevel@tonic-gate release_minor:
34780Sstevel@tonic-gate 	crypto_release_minor(cm);
34790Sstevel@tonic-gate 
34800Sstevel@tonic-gate 	if (error != 0)
34810Sstevel@tonic-gate 		return (error);
34820Sstevel@tonic-gate 
34830Sstevel@tonic-gate 	logout.cl_return_value = rv;
34840Sstevel@tonic-gate 	if (copyout(&logout, arg, sizeof (logout)) != 0) {
34850Sstevel@tonic-gate 		return (EFAULT);
34860Sstevel@tonic-gate 	}
34870Sstevel@tonic-gate 	return (0);
34880Sstevel@tonic-gate }
34890Sstevel@tonic-gate 
34900Sstevel@tonic-gate /* ARGSUSED */
34910Sstevel@tonic-gate static int
34920Sstevel@tonic-gate sign_init(dev_t dev, caddr_t arg, int mode, int *rval)
34930Sstevel@tonic-gate {
34940Sstevel@tonic-gate 	return (sign_verify_init(dev, arg, mode, crypto_sign_init_prov));
34950Sstevel@tonic-gate }
34960Sstevel@tonic-gate 
34970Sstevel@tonic-gate /* ARGSUSED */
34980Sstevel@tonic-gate static int
34990Sstevel@tonic-gate sign_recover_init(dev_t dev, caddr_t arg, int mode, int *rval)
35000Sstevel@tonic-gate {
35010Sstevel@tonic-gate 	return (sign_verify_init(dev, arg, mode,
35020Sstevel@tonic-gate 	    crypto_sign_recover_init_prov));
35030Sstevel@tonic-gate }
35040Sstevel@tonic-gate 
35050Sstevel@tonic-gate /* ARGSUSED */
35060Sstevel@tonic-gate static int
35070Sstevel@tonic-gate verify_init(dev_t dev, caddr_t arg, int mode, int *rval)
35080Sstevel@tonic-gate {
35090Sstevel@tonic-gate 	return (sign_verify_init(dev, arg, mode, crypto_verify_init_prov));
35100Sstevel@tonic-gate }
35110Sstevel@tonic-gate 
35120Sstevel@tonic-gate /* ARGSUSED */
35130Sstevel@tonic-gate static int
35140Sstevel@tonic-gate verify_recover_init(dev_t dev, caddr_t arg, int mode, int *rval)
35150Sstevel@tonic-gate {
35160Sstevel@tonic-gate 	return (sign_verify_init(dev, arg, mode,
35170Sstevel@tonic-gate 	    crypto_verify_recover_init_prov));
35180Sstevel@tonic-gate }
35190Sstevel@tonic-gate 
35200Sstevel@tonic-gate /*
35210Sstevel@tonic-gate  * ASSUMPTION: crypto_sign_init, crypto_verify_init, crypto_sign_recover_init,
35220Sstevel@tonic-gate  * and crypto_verify_recover_init structures are identical
35230Sstevel@tonic-gate  * except for field names.
35240Sstevel@tonic-gate  */
35250Sstevel@tonic-gate static int
35260Sstevel@tonic-gate sign_verify_init(dev_t dev, caddr_t arg, int mode,
3527904Smcpowers     int (*init)(crypto_provider_t, crypto_session_id_t,
35280Sstevel@tonic-gate     crypto_mechanism_t *, crypto_key_t *, crypto_ctx_template_t,
35290Sstevel@tonic-gate     crypto_context_t *, crypto_call_req_t *))
35300Sstevel@tonic-gate {
35310Sstevel@tonic-gate 	STRUCT_DECL(crypto_sign_init, sign_init);
3532904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
35330Sstevel@tonic-gate 	crypto_session_id_t session_id;
35340Sstevel@tonic-gate 	crypto_mechanism_t mech;
35350Sstevel@tonic-gate 	crypto_key_t key;
35360Sstevel@tonic-gate 	crypto_minor_t *cm;
35370Sstevel@tonic-gate 	crypto_session_data_t *sp;
35380Sstevel@tonic-gate 	crypto_context_t cc;
35390Sstevel@tonic-gate 	crypto_ctx_t **ctxpp;
35400Sstevel@tonic-gate 	size_t mech_rctl_bytes = 0;
35410Sstevel@tonic-gate 	size_t key_rctl_bytes = 0;
35420Sstevel@tonic-gate 	size_t carry;
35430Sstevel@tonic-gate 	int error = 0;
35440Sstevel@tonic-gate 	int rv;
3545904Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
35461808Smcpowers 	crypto_func_group_t fg;
35470Sstevel@tonic-gate 
35480Sstevel@tonic-gate 	STRUCT_INIT(sign_init, mode);
35490Sstevel@tonic-gate 
35500Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
35510Sstevel@tonic-gate 		cmn_err(CE_WARN, "sign_verify_init: failed holding minor");
35520Sstevel@tonic-gate 		return (ENXIO);
35530Sstevel@tonic-gate 	}
35540Sstevel@tonic-gate 
35550Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(sign_init), STRUCT_SIZE(sign_init)) != 0) {
35560Sstevel@tonic-gate 		crypto_release_minor(cm);
35570Sstevel@tonic-gate 		return (EFAULT);
35580Sstevel@tonic-gate 	}
35590Sstevel@tonic-gate 
35600Sstevel@tonic-gate 	mech.cm_param = NULL;
35610Sstevel@tonic-gate 	bzero(&key, sizeof (key));
35620Sstevel@tonic-gate 
35630Sstevel@tonic-gate 	session_id = STRUCT_FGET(sign_init, si_session);
35640Sstevel@tonic-gate 
35650Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
35660Sstevel@tonic-gate 		goto release_minor;
35670Sstevel@tonic-gate 	}
35680Sstevel@tonic-gate 
3569904Smcpowers 	bcopy(STRUCT_FADDR(sign_init, si_mech), &mech.cm_type,
3570904Smcpowers 	    sizeof (crypto_mech_type_t));
35710Sstevel@tonic-gate 
35720Sstevel@tonic-gate 	ASSERT(init == crypto_sign_init_prov ||
35730Sstevel@tonic-gate 	    init == crypto_verify_init_prov ||
35740Sstevel@tonic-gate 	    init == crypto_sign_recover_init_prov ||
35750Sstevel@tonic-gate 	    init == crypto_verify_recover_init_prov);
35760Sstevel@tonic-gate 
35770Sstevel@tonic-gate 	if (init == crypto_sign_init_prov) {
35781808Smcpowers 		fg =  CRYPTO_FG_SIGN;
35790Sstevel@tonic-gate 		ctxpp = &sp->sd_sign_ctx;
35800Sstevel@tonic-gate 	} else if (init == crypto_verify_init_prov) {
35811808Smcpowers 		fg =  CRYPTO_FG_VERIFY;
35820Sstevel@tonic-gate 		ctxpp = &sp->sd_verify_ctx;
35830Sstevel@tonic-gate 	} else if (init == crypto_sign_recover_init_prov) {
35841808Smcpowers 		fg =  CRYPTO_FG_SIGN_RECOVER;
35850Sstevel@tonic-gate 		ctxpp = &sp->sd_sign_recover_ctx;
35860Sstevel@tonic-gate 	} else {
35871808Smcpowers 		fg =  CRYPTO_FG_VERIFY_RECOVER;
35880Sstevel@tonic-gate 		ctxpp = &sp->sd_verify_recover_ctx;
35890Sstevel@tonic-gate 	}
35900Sstevel@tonic-gate 
3591904Smcpowers 	if ((rv = kcf_get_hardware_provider(mech.cm_type, CRYPTO_MECH_INVALID,
35921808Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider, fg))
35931808Smcpowers 	    != CRYPTO_SUCCESS) {
35940Sstevel@tonic-gate 		goto out;
35950Sstevel@tonic-gate 	}
35960Sstevel@tonic-gate 
3597904Smcpowers 	carry = 0;
3598904Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
3599904Smcpowers 	    STRUCT_FADDR(sign_init, si_mech), &mech, mode, &error);
3600904Smcpowers 
3601904Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
3602904Smcpowers 		allocated_by_crypto_module = B_TRUE;
3603904Smcpowers 		if (!copyin_mech(mode, STRUCT_FADDR(sign_init, si_mech),
36043916Skrishna 		    &mech, &mech_rctl_bytes, &carry, &rv, &error)) {
3605904Smcpowers 			goto out;
3606904Smcpowers 		}
3607904Smcpowers 	} else {
3608904Smcpowers 		if (rv != CRYPTO_SUCCESS)
3609904Smcpowers 			goto out;
3610904Smcpowers 	}
3611904Smcpowers 
36120Sstevel@tonic-gate 	if (!copyin_key(mode, STRUCT_FADDR(sign_init, si_key), &key,
36133916Skrishna 	    &key_rctl_bytes, &rv, &error, carry)) {
36140Sstevel@tonic-gate 		goto out;
36150Sstevel@tonic-gate 	}
36160Sstevel@tonic-gate 
36170Sstevel@tonic-gate 	rv = (init)(real_provider, sp->sd_provider_session->ps_session,
36180Sstevel@tonic-gate 	    &mech, &key, NULL, &cc, NULL);
36190Sstevel@tonic-gate 
36200Sstevel@tonic-gate 	/*
36210Sstevel@tonic-gate 	 * Check if a context already exists. If so, it means it is being
36220Sstevel@tonic-gate 	 * abandoned. So, cancel it to avoid leaking it.
36230Sstevel@tonic-gate 	 */
36240Sstevel@tonic-gate 	if (*ctxpp != NULL)
36250Sstevel@tonic-gate 		CRYPTO_CANCEL_CTX(ctxpp);
36260Sstevel@tonic-gate 	*ctxpp = (rv == CRYPTO_SUCCESS) ? cc : NULL;
36270Sstevel@tonic-gate 
36280Sstevel@tonic-gate out:
36290Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
36300Sstevel@tonic-gate 
36310Sstevel@tonic-gate release_minor:
36323916Skrishna 	if (mech_rctl_bytes + key_rctl_bytes != 0)
36333916Skrishna 		CRYPTO_DECREMENT_RCTL(mech_rctl_bytes + key_rctl_bytes);
36340Sstevel@tonic-gate 	crypto_release_minor(cm);
36350Sstevel@tonic-gate 
3636904Smcpowers 	if (real_provider != NULL) {
3637904Smcpowers 		crypto_free_mech(real_provider,
3638904Smcpowers 		    allocated_by_crypto_module, &mech);
3639904Smcpowers 		KCF_PROV_REFRELE(real_provider);
3640904Smcpowers 	}
36410Sstevel@tonic-gate 
36420Sstevel@tonic-gate 	free_crypto_key(&key);
36430Sstevel@tonic-gate 
36440Sstevel@tonic-gate 	if (error != 0)
36450Sstevel@tonic-gate 		return (error);
36460Sstevel@tonic-gate 
36470Sstevel@tonic-gate 	STRUCT_FSET(sign_init, si_return_value, rv);
36480Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(sign_init), arg, STRUCT_SIZE(sign_init)) != 0) {
36490Sstevel@tonic-gate 		return (EFAULT);
36500Sstevel@tonic-gate 	}
36510Sstevel@tonic-gate 	return (0);
36520Sstevel@tonic-gate }
36530Sstevel@tonic-gate 
36540Sstevel@tonic-gate /* ARGSUSED */
36550Sstevel@tonic-gate static int
36560Sstevel@tonic-gate sign(dev_t dev, caddr_t arg, int mode, int *rval)
36570Sstevel@tonic-gate {
36580Sstevel@tonic-gate 	return (common_digest(dev, arg, mode, crypto_sign_single));
36590Sstevel@tonic-gate }
36600Sstevel@tonic-gate 
36610Sstevel@tonic-gate /* ARGSUSED */
36620Sstevel@tonic-gate static int
36630Sstevel@tonic-gate sign_recover(dev_t dev, caddr_t arg, int mode, int *rval)
36640Sstevel@tonic-gate {
36650Sstevel@tonic-gate 	return (common_digest(dev, arg, mode, crypto_sign_recover_single));
36660Sstevel@tonic-gate }
36670Sstevel@tonic-gate 
36680Sstevel@tonic-gate /* ARGSUSED */
36690Sstevel@tonic-gate static int
36700Sstevel@tonic-gate verify(dev_t dev, caddr_t arg, int mode, int *rval)
36710Sstevel@tonic-gate {
36720Sstevel@tonic-gate 	STRUCT_DECL(crypto_verify, verify);
36730Sstevel@tonic-gate 	crypto_session_id_t session_id;
36740Sstevel@tonic-gate 	crypto_minor_t *cm;
36750Sstevel@tonic-gate 	crypto_session_data_t *sp;
36760Sstevel@tonic-gate 	crypto_data_t data, sign;
36770Sstevel@tonic-gate 	size_t datalen, signlen, need = 0;
36780Sstevel@tonic-gate 	int error = 0;
36790Sstevel@tonic-gate 	int rv;
36800Sstevel@tonic-gate 
36810Sstevel@tonic-gate 	STRUCT_INIT(verify, mode);
36820Sstevel@tonic-gate 
36830Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
36840Sstevel@tonic-gate 		cmn_err(CE_WARN, "verify: failed holding minor");
36850Sstevel@tonic-gate 		return (ENXIO);
36860Sstevel@tonic-gate 	}
36870Sstevel@tonic-gate 
36880Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(verify), STRUCT_SIZE(verify)) != 0) {
36890Sstevel@tonic-gate 		crypto_release_minor(cm);
36900Sstevel@tonic-gate 		return (EFAULT);
36910Sstevel@tonic-gate 	}
36920Sstevel@tonic-gate 
36930Sstevel@tonic-gate 	data.cd_raw.iov_base = NULL;
36940Sstevel@tonic-gate 	sign.cd_raw.iov_base = NULL;
36950Sstevel@tonic-gate 
36960Sstevel@tonic-gate 	datalen = STRUCT_FGET(verify, cv_datalen);
36970Sstevel@tonic-gate 	signlen = STRUCT_FGET(verify, cv_signlen);
36980Sstevel@tonic-gate 	if (datalen > crypto_max_buffer_len ||
36990Sstevel@tonic-gate 	    signlen > crypto_max_buffer_len) {
37000Sstevel@tonic-gate 		cmn_err(CE_NOTE, "verify: buffer greater than %ld bytes, "
37010Sstevel@tonic-gate 		"pid = %d", crypto_max_buffer_len, curproc->p_pid);
37020Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
37030Sstevel@tonic-gate 		goto release_minor;
37040Sstevel@tonic-gate 	}
37050Sstevel@tonic-gate 
37060Sstevel@tonic-gate 	need = datalen + signlen;
37073916Skrishna 	if ((rv = crypto_buffer_check(need)) != CRYPTO_SUCCESS) {
37080Sstevel@tonic-gate 		need = 0;
37090Sstevel@tonic-gate 		goto release_minor;
37100Sstevel@tonic-gate 	}
37110Sstevel@tonic-gate 
37120Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(data, datalen);
37130Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(sign, signlen);
37140Sstevel@tonic-gate 
37150Sstevel@tonic-gate 	if (datalen != 0 && copyin(STRUCT_FGETP(verify, cv_databuf),
37160Sstevel@tonic-gate 	    data.cd_raw.iov_base, datalen) != 0) {
37170Sstevel@tonic-gate 		error = EFAULT;
37180Sstevel@tonic-gate 		goto release_minor;
37190Sstevel@tonic-gate 	}
37200Sstevel@tonic-gate 
37210Sstevel@tonic-gate 	if (signlen != 0 && copyin(STRUCT_FGETP(verify, cv_signbuf),
37220Sstevel@tonic-gate 	    sign.cd_raw.iov_base, signlen) != 0) {
37230Sstevel@tonic-gate 		error = EFAULT;
37240Sstevel@tonic-gate 		goto release_minor;
37250Sstevel@tonic-gate 	}
37260Sstevel@tonic-gate 	session_id = STRUCT_FGET(verify, cv_session);
37270Sstevel@tonic-gate 
37280Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
37290Sstevel@tonic-gate 		goto release_minor;
37300Sstevel@tonic-gate 	}
37310Sstevel@tonic-gate 
37320Sstevel@tonic-gate 	rv = crypto_verify_single(sp->sd_verify_ctx, &data, &sign, NULL);
37330Sstevel@tonic-gate 	if (KCF_CONTEXT_DONE(rv))
37340Sstevel@tonic-gate 		sp->sd_verify_ctx = NULL;
37350Sstevel@tonic-gate 
37360Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
37370Sstevel@tonic-gate 
37380Sstevel@tonic-gate release_minor:
37390Sstevel@tonic-gate 	if (need != 0) {
37403916Skrishna 		CRYPTO_DECREMENT_RCTL(need);
37410Sstevel@tonic-gate 	}
37420Sstevel@tonic-gate 	crypto_release_minor(cm);
37430Sstevel@tonic-gate 
37440Sstevel@tonic-gate 	if (data.cd_raw.iov_base != NULL)
37450Sstevel@tonic-gate 		kmem_free(data.cd_raw.iov_base, datalen);
37460Sstevel@tonic-gate 
37470Sstevel@tonic-gate 	if (sign.cd_raw.iov_base != NULL)
37480Sstevel@tonic-gate 		kmem_free(sign.cd_raw.iov_base, signlen);
37490Sstevel@tonic-gate 
37500Sstevel@tonic-gate 	if (error != 0)
37510Sstevel@tonic-gate 		return (error);
37520Sstevel@tonic-gate 
37530Sstevel@tonic-gate 	STRUCT_FSET(verify, cv_return_value, rv);
37540Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(verify), arg, STRUCT_SIZE(verify)) != 0) {
37550Sstevel@tonic-gate 		return (EFAULT);
37560Sstevel@tonic-gate 	}
37570Sstevel@tonic-gate 	return (0);
37580Sstevel@tonic-gate }
37590Sstevel@tonic-gate 
37600Sstevel@tonic-gate /* ARGSUSED */
37610Sstevel@tonic-gate static int
37620Sstevel@tonic-gate verify_recover(dev_t dev, caddr_t arg, int mode, int *rval)
37630Sstevel@tonic-gate {
37640Sstevel@tonic-gate 	return (common_digest(dev, arg, mode, crypto_verify_recover_single));
37650Sstevel@tonic-gate }
37660Sstevel@tonic-gate 
37670Sstevel@tonic-gate /* ARGSUSED */
37680Sstevel@tonic-gate static int
37690Sstevel@tonic-gate sign_update(dev_t dev, caddr_t arg, int mode, int *rval)
37700Sstevel@tonic-gate {
37710Sstevel@tonic-gate 	return (sign_verify_update(dev, arg, mode, crypto_sign_update));
37720Sstevel@tonic-gate }
37730Sstevel@tonic-gate 
37740Sstevel@tonic-gate /* ARGSUSED */
37750Sstevel@tonic-gate static int
37760Sstevel@tonic-gate verify_update(dev_t dev, caddr_t arg, int mode, int *rval)
37770Sstevel@tonic-gate {
37780Sstevel@tonic-gate 	return (sign_verify_update(dev, arg, mode, crypto_verify_update));
37790Sstevel@tonic-gate }
37800Sstevel@tonic-gate 
37810Sstevel@tonic-gate /*
37820Sstevel@tonic-gate  * ASSUMPTION: crypto_sign_update and crypto_verify_update structures
37830Sstevel@tonic-gate  * are identical except for field names.
37840Sstevel@tonic-gate  */
37850Sstevel@tonic-gate static int
37860Sstevel@tonic-gate sign_verify_update(dev_t dev, caddr_t arg, int mode,
37870Sstevel@tonic-gate     int (*update)(crypto_context_t, crypto_data_t *, crypto_call_req_t *))
37880Sstevel@tonic-gate {
37890Sstevel@tonic-gate 	STRUCT_DECL(crypto_sign_update, sign_update);
37900Sstevel@tonic-gate 	crypto_session_id_t session_id;
37910Sstevel@tonic-gate 	crypto_minor_t *cm;
37920Sstevel@tonic-gate 	crypto_session_data_t *sp;
37930Sstevel@tonic-gate 	crypto_ctx_t **ctxpp;
37940Sstevel@tonic-gate 	crypto_data_t data;
37950Sstevel@tonic-gate 	size_t datalen, need = 0;
37960Sstevel@tonic-gate 	int error = 0;
37970Sstevel@tonic-gate 	int rv;
37980Sstevel@tonic-gate 
37990Sstevel@tonic-gate 	STRUCT_INIT(sign_update, mode);
38000Sstevel@tonic-gate 
38010Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
38020Sstevel@tonic-gate 		cmn_err(CE_WARN, "sign_verify_update: failed holding minor");
38030Sstevel@tonic-gate 		return (ENXIO);
38040Sstevel@tonic-gate 	}
38050Sstevel@tonic-gate 
38060Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(sign_update),
38070Sstevel@tonic-gate 	    STRUCT_SIZE(sign_update)) != 0) {
38080Sstevel@tonic-gate 		crypto_release_minor(cm);
38090Sstevel@tonic-gate 		return (EFAULT);
38100Sstevel@tonic-gate 	}
38110Sstevel@tonic-gate 
38120Sstevel@tonic-gate 	data.cd_raw.iov_base = NULL;
38130Sstevel@tonic-gate 
38140Sstevel@tonic-gate 	datalen = STRUCT_FGET(sign_update, su_datalen);
38150Sstevel@tonic-gate 	if (datalen > crypto_max_buffer_len) {
38160Sstevel@tonic-gate 		cmn_err(CE_NOTE, "sign_verify_update: buffer greater than %ld "
38170Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
38180Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
38190Sstevel@tonic-gate 		goto release_minor;
38200Sstevel@tonic-gate 	}
38210Sstevel@tonic-gate 
38223916Skrishna 	if ((rv = crypto_buffer_check(datalen)) != CRYPTO_SUCCESS) {
38230Sstevel@tonic-gate 		goto release_minor;
38240Sstevel@tonic-gate 	}
38250Sstevel@tonic-gate 	need = datalen;
38260Sstevel@tonic-gate 
38270Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(data, datalen);
38280Sstevel@tonic-gate 
38290Sstevel@tonic-gate 	if (datalen != 0 && copyin(STRUCT_FGETP(sign_update, su_databuf),
38300Sstevel@tonic-gate 	    data.cd_raw.iov_base, datalen) != 0) {
38310Sstevel@tonic-gate 		error = EFAULT;
38320Sstevel@tonic-gate 		goto release_minor;
38330Sstevel@tonic-gate 	}
38340Sstevel@tonic-gate 
38350Sstevel@tonic-gate 	session_id = STRUCT_FGET(sign_update, su_session);
38360Sstevel@tonic-gate 
38370Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
38380Sstevel@tonic-gate 		goto release_minor;
38390Sstevel@tonic-gate 	}
38400Sstevel@tonic-gate 
38410Sstevel@tonic-gate 	ctxpp = (update == crypto_sign_update) ?
38420Sstevel@tonic-gate 	    &sp->sd_sign_ctx : &sp->sd_verify_ctx;
38430Sstevel@tonic-gate 
38440Sstevel@tonic-gate 	rv = (update)(*ctxpp, &data, NULL);
38450Sstevel@tonic-gate 	if (rv != CRYPTO_SUCCESS)
38460Sstevel@tonic-gate 		CRYPTO_CANCEL_CTX(ctxpp);
38470Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
38480Sstevel@tonic-gate 
38490Sstevel@tonic-gate release_minor:
38500Sstevel@tonic-gate 	if (need != 0) {
38513916Skrishna 		CRYPTO_DECREMENT_RCTL(need);
38520Sstevel@tonic-gate 	}
38530Sstevel@tonic-gate 	crypto_release_minor(cm);
38540Sstevel@tonic-gate 
38550Sstevel@tonic-gate 	if (data.cd_raw.iov_base != NULL)
38560Sstevel@tonic-gate 		kmem_free(data.cd_raw.iov_base, datalen);
38570Sstevel@tonic-gate 
38580Sstevel@tonic-gate 	if (error != 0)
38590Sstevel@tonic-gate 		return (error);
38600Sstevel@tonic-gate 
38610Sstevel@tonic-gate 	STRUCT_FSET(sign_update, su_return_value, rv);
38620Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(sign_update), arg,
38630Sstevel@tonic-gate 	    STRUCT_SIZE(sign_update)) != 0) {
38640Sstevel@tonic-gate 		return (EFAULT);
38650Sstevel@tonic-gate 	}
38660Sstevel@tonic-gate 	return (0);
38670Sstevel@tonic-gate }
38680Sstevel@tonic-gate 
38690Sstevel@tonic-gate /* ARGSUSED */
38700Sstevel@tonic-gate static int
38710Sstevel@tonic-gate sign_final(dev_t dev, caddr_t arg, int mode, int *rval)
38720Sstevel@tonic-gate {
38730Sstevel@tonic-gate 	return (common_final(dev, arg, mode, crypto_sign_final));
38740Sstevel@tonic-gate }
38750Sstevel@tonic-gate 
38760Sstevel@tonic-gate /*
38770Sstevel@tonic-gate  * Can't use the common final because it does a copyout of
38780Sstevel@tonic-gate  * the final part.
38790Sstevel@tonic-gate  */
38800Sstevel@tonic-gate /* ARGSUSED */
38810Sstevel@tonic-gate static int
38820Sstevel@tonic-gate verify_final(dev_t dev, caddr_t arg, int mode, int *rval)
38830Sstevel@tonic-gate {
38840Sstevel@tonic-gate 	STRUCT_DECL(crypto_verify_final, verify_final);
38850Sstevel@tonic-gate 	crypto_session_id_t session_id;
38860Sstevel@tonic-gate 	crypto_minor_t *cm;
38870Sstevel@tonic-gate 	crypto_session_data_t *sp;
38880Sstevel@tonic-gate 	crypto_data_t sign;
38890Sstevel@tonic-gate 	size_t signlen, need = 0;
38900Sstevel@tonic-gate 	int error = 0;
38910Sstevel@tonic-gate 	int rv;
38920Sstevel@tonic-gate 
38930Sstevel@tonic-gate 	STRUCT_INIT(verify_final, mode);
38940Sstevel@tonic-gate 
38950Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
38960Sstevel@tonic-gate 		cmn_err(CE_WARN, "verify_final: failed holding minor");
38970Sstevel@tonic-gate 		return (ENXIO);
38980Sstevel@tonic-gate 	}
38990Sstevel@tonic-gate 
39000Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(verify_final),
39010Sstevel@tonic-gate 	    STRUCT_SIZE(verify_final)) != 0) {
39020Sstevel@tonic-gate 		crypto_release_minor(cm);
39030Sstevel@tonic-gate 		return (EFAULT);
39040Sstevel@tonic-gate 	}
39050Sstevel@tonic-gate 
39060Sstevel@tonic-gate 	sign.cd_raw.iov_base = NULL;
39070Sstevel@tonic-gate 
39080Sstevel@tonic-gate 	signlen = STRUCT_FGET(verify_final, vf_signlen);
39090Sstevel@tonic-gate 	if (signlen > crypto_max_buffer_len) {
39100Sstevel@tonic-gate 		cmn_err(CE_NOTE, "verify_final: buffer greater than %ld "
39110Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
39120Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
39130Sstevel@tonic-gate 		goto release_minor;
39140Sstevel@tonic-gate 	}
39150Sstevel@tonic-gate 
39163916Skrishna 	if ((rv = crypto_buffer_check(signlen)) != CRYPTO_SUCCESS) {
39170Sstevel@tonic-gate 		goto release_minor;
39180Sstevel@tonic-gate 	}
39190Sstevel@tonic-gate 	need = signlen;
39200Sstevel@tonic-gate 
39210Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(sign, signlen);
39220Sstevel@tonic-gate 
39230Sstevel@tonic-gate 	if (signlen != 0 && copyin(STRUCT_FGETP(verify_final, vf_signbuf),
39240Sstevel@tonic-gate 	    sign.cd_raw.iov_base, signlen) != 0) {
39250Sstevel@tonic-gate 		error = EFAULT;
39260Sstevel@tonic-gate 		goto release_minor;
39270Sstevel@tonic-gate 	}
39280Sstevel@tonic-gate 
39290Sstevel@tonic-gate 	session_id = STRUCT_FGET(verify_final, vf_session);
39300Sstevel@tonic-gate 
39310Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
39320Sstevel@tonic-gate 		goto release_minor;
39330Sstevel@tonic-gate 	}
39340Sstevel@tonic-gate 
39350Sstevel@tonic-gate 	rv = crypto_verify_final(sp->sd_verify_ctx, &sign, NULL);
39360Sstevel@tonic-gate 	if (KCF_CONTEXT_DONE(rv))
39370Sstevel@tonic-gate 		sp->sd_verify_ctx = NULL;
39380Sstevel@tonic-gate 
39390Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
39400Sstevel@tonic-gate 
39410Sstevel@tonic-gate release_minor:
39420Sstevel@tonic-gate 	if (need != 0) {
39433916Skrishna 		CRYPTO_DECREMENT_RCTL(need);
39440Sstevel@tonic-gate 	}
39450Sstevel@tonic-gate 	crypto_release_minor(cm);
39460Sstevel@tonic-gate 
39470Sstevel@tonic-gate 	if (sign.cd_raw.iov_base != NULL)
39480Sstevel@tonic-gate 		kmem_free(sign.cd_raw.iov_base, signlen);
39490Sstevel@tonic-gate 
39500Sstevel@tonic-gate 	if (error != 0)
39510Sstevel@tonic-gate 		return (error);
39520Sstevel@tonic-gate 
39530Sstevel@tonic-gate 	STRUCT_FSET(verify_final, vf_return_value, rv);
39540Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(verify_final), arg,
39550Sstevel@tonic-gate 	    STRUCT_SIZE(verify_final)) != 0) {
39560Sstevel@tonic-gate 		return (EFAULT);
39570Sstevel@tonic-gate 	}
39580Sstevel@tonic-gate 	return (0);
39590Sstevel@tonic-gate }
39600Sstevel@tonic-gate 
39610Sstevel@tonic-gate /* ARGSUSED */
39620Sstevel@tonic-gate static int
39630Sstevel@tonic-gate seed_random(dev_t dev, caddr_t arg, int mode, int *rval)
39640Sstevel@tonic-gate {
39650Sstevel@tonic-gate 	STRUCT_DECL(crypto_seed_random, seed_random);
3966904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
39670Sstevel@tonic-gate 	kcf_req_params_t params;
39680Sstevel@tonic-gate 	crypto_session_id_t session_id;
39690Sstevel@tonic-gate 	crypto_minor_t *cm;
39700Sstevel@tonic-gate 	crypto_session_data_t *sp;
39710Sstevel@tonic-gate 	uchar_t *seed_buffer = NULL;
39720Sstevel@tonic-gate 	size_t seed_len;
39730Sstevel@tonic-gate 	size_t need = 0;
39740Sstevel@tonic-gate 	int error = 0;
39750Sstevel@tonic-gate 	int rv;
39760Sstevel@tonic-gate 
39770Sstevel@tonic-gate 	STRUCT_INIT(seed_random, mode);
39780Sstevel@tonic-gate 
39790Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
39800Sstevel@tonic-gate 		cmn_err(CE_WARN, "seed_random: failed holding minor");
39810Sstevel@tonic-gate 		return (ENXIO);
39820Sstevel@tonic-gate 	}
39830Sstevel@tonic-gate 
39840Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(seed_random),
39850Sstevel@tonic-gate 	    STRUCT_SIZE(seed_random)) != 0) {
39860Sstevel@tonic-gate 		crypto_release_minor(cm);
39870Sstevel@tonic-gate 		return (EFAULT);
39880Sstevel@tonic-gate 	}
39890Sstevel@tonic-gate 
39900Sstevel@tonic-gate 	seed_len = STRUCT_FGET(seed_random, sr_seedlen);
39910Sstevel@tonic-gate 	if (seed_len > crypto_max_buffer_len) {
39920Sstevel@tonic-gate 		cmn_err(CE_NOTE, "seed_random: buffer greater than %ld "
39930Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
39940Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
39950Sstevel@tonic-gate 		goto release_minor;
39960Sstevel@tonic-gate 	}
39970Sstevel@tonic-gate 
39983916Skrishna 	if ((rv = crypto_buffer_check(seed_len)) != CRYPTO_SUCCESS) {
39990Sstevel@tonic-gate 		goto release_minor;
40000Sstevel@tonic-gate 	}
40010Sstevel@tonic-gate 	need = seed_len;
40020Sstevel@tonic-gate 	seed_buffer = kmem_alloc(seed_len, KM_SLEEP);
40030Sstevel@tonic-gate 
40040Sstevel@tonic-gate 	if (seed_len != 0 && copyin(STRUCT_FGETP(seed_random, sr_seedbuf),
40050Sstevel@tonic-gate 	    seed_buffer, seed_len) != 0) {
40060Sstevel@tonic-gate 		error = EFAULT;
40070Sstevel@tonic-gate 		goto release_minor;
40080Sstevel@tonic-gate 	}
40090Sstevel@tonic-gate 
40100Sstevel@tonic-gate 	session_id = STRUCT_FGET(seed_random, sr_session);
40110Sstevel@tonic-gate 
40120Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
40130Sstevel@tonic-gate 		goto release_minor;
40140Sstevel@tonic-gate 	}
40150Sstevel@tonic-gate 
40161808Smcpowers 	if ((rv = kcf_get_hardware_provider_nomech(
4017904Smcpowers 	    CRYPTO_OPS_OFFSET(random_ops), CRYPTO_RANDOM_OFFSET(seed_random),
4018904Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider))
4019904Smcpowers 	    != CRYPTO_SUCCESS) {
40200Sstevel@tonic-gate 		goto out;
40210Sstevel@tonic-gate 	}
40220Sstevel@tonic-gate 
40230Sstevel@tonic-gate 	KCF_WRAP_RANDOM_OPS_PARAMS(&params, KCF_OP_RANDOM_SEED,
40241920Smcpowers 	    sp->sd_provider_session->ps_session, seed_buffer, seed_len, 0,
40251920Smcpowers 	    CRYPTO_SEED_NOW);
40260Sstevel@tonic-gate 
40270Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
40280Sstevel@tonic-gate 
40290Sstevel@tonic-gate out:
40300Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
40310Sstevel@tonic-gate 
40320Sstevel@tonic-gate release_minor:
40330Sstevel@tonic-gate 	if (need != 0) {
40343916Skrishna 		CRYPTO_DECREMENT_RCTL(need);
40350Sstevel@tonic-gate 	}
40360Sstevel@tonic-gate 	crypto_release_minor(cm);
40370Sstevel@tonic-gate 
4038904Smcpowers 	if (real_provider != NULL)
4039904Smcpowers 		KCF_PROV_REFRELE(real_provider);
4040904Smcpowers 
40410Sstevel@tonic-gate 	if (seed_buffer != NULL)
40420Sstevel@tonic-gate 		kmem_free(seed_buffer, seed_len);
40430Sstevel@tonic-gate 
40440Sstevel@tonic-gate 	if (error != 0)
40450Sstevel@tonic-gate 		return (error);
40460Sstevel@tonic-gate 
40470Sstevel@tonic-gate 	STRUCT_FSET(seed_random, sr_return_value, rv);
40480Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(seed_random), arg,
40490Sstevel@tonic-gate 	    STRUCT_SIZE(seed_random)) != 0) {
40500Sstevel@tonic-gate 		return (EFAULT);
40510Sstevel@tonic-gate 	}
40520Sstevel@tonic-gate 	return (0);
40530Sstevel@tonic-gate }
40540Sstevel@tonic-gate 
40550Sstevel@tonic-gate /* ARGSUSED */
40560Sstevel@tonic-gate static int
40570Sstevel@tonic-gate generate_random(dev_t dev, caddr_t arg, int mode, int *rval)
40580Sstevel@tonic-gate {
40590Sstevel@tonic-gate 	STRUCT_DECL(crypto_generate_random, generate_random);
4060904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
40610Sstevel@tonic-gate 	kcf_req_params_t params;
40620Sstevel@tonic-gate 	crypto_session_id_t session_id;
40630Sstevel@tonic-gate 	crypto_minor_t *cm;
40640Sstevel@tonic-gate 	crypto_session_data_t *sp;
40650Sstevel@tonic-gate 	uchar_t *buffer = NULL;
40660Sstevel@tonic-gate 	size_t len;
40670Sstevel@tonic-gate 	size_t need = 0;
40680Sstevel@tonic-gate 	int error = 0;
40690Sstevel@tonic-gate 	int rv;
40700Sstevel@tonic-gate 
40710Sstevel@tonic-gate 	STRUCT_INIT(generate_random, mode);
40720Sstevel@tonic-gate 
40730Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
40740Sstevel@tonic-gate 		cmn_err(CE_WARN, "generate_random: failed holding minor");
40750Sstevel@tonic-gate 		return (ENXIO);
40760Sstevel@tonic-gate 	}
40770Sstevel@tonic-gate 
40780Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(generate_random),
40790Sstevel@tonic-gate 	    STRUCT_SIZE(generate_random)) != 0) {
40800Sstevel@tonic-gate 		crypto_release_minor(cm);
40810Sstevel@tonic-gate 		return (EFAULT);
40820Sstevel@tonic-gate 	}
40830Sstevel@tonic-gate 
40840Sstevel@tonic-gate 	len = STRUCT_FGET(generate_random, gr_buflen);
40850Sstevel@tonic-gate 	if (len > crypto_max_buffer_len) {
40860Sstevel@tonic-gate 		cmn_err(CE_NOTE, "generate_random: buffer greater than %ld "
40870Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
40880Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
40890Sstevel@tonic-gate 		goto release_minor;
40900Sstevel@tonic-gate 	}
40910Sstevel@tonic-gate 
40923916Skrishna 	if ((rv = crypto_buffer_check(len)) != CRYPTO_SUCCESS) {
40930Sstevel@tonic-gate 		goto release_minor;
40940Sstevel@tonic-gate 	}
40950Sstevel@tonic-gate 	need = len;
40960Sstevel@tonic-gate 	buffer = kmem_alloc(len, KM_SLEEP);
40970Sstevel@tonic-gate 
40980Sstevel@tonic-gate 	session_id = STRUCT_FGET(generate_random, gr_session);
40990Sstevel@tonic-gate 
41000Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
41010Sstevel@tonic-gate 		goto release_minor;
41020Sstevel@tonic-gate 	}
41030Sstevel@tonic-gate 
41041808Smcpowers 	if ((rv = kcf_get_hardware_provider_nomech(
41050Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(random_ops),
4106904Smcpowers 	    CRYPTO_RANDOM_OFFSET(generate_random), CHECK_RESTRICT_FALSE,
41070Sstevel@tonic-gate 	    sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
41080Sstevel@tonic-gate 		goto out;
41090Sstevel@tonic-gate 	}
41100Sstevel@tonic-gate 
41110Sstevel@tonic-gate 	KCF_WRAP_RANDOM_OPS_PARAMS(&params, KCF_OP_RANDOM_GENERATE,
41121920Smcpowers 	    sp->sd_provider_session->ps_session, buffer, len, 0, 0);
41130Sstevel@tonic-gate 
41140Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
41150Sstevel@tonic-gate 
41160Sstevel@tonic-gate out:
41170Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
41180Sstevel@tonic-gate 
41190Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
41200Sstevel@tonic-gate 		if (len != 0 && copyout(buffer,
41210Sstevel@tonic-gate 		    STRUCT_FGETP(generate_random, gr_buf), len) != 0) {
41220Sstevel@tonic-gate 			error = EFAULT;
41230Sstevel@tonic-gate 		}
41240Sstevel@tonic-gate 	}
41250Sstevel@tonic-gate 
41260Sstevel@tonic-gate release_minor:
41270Sstevel@tonic-gate 	if (need != 0) {
41283916Skrishna 		CRYPTO_DECREMENT_RCTL(need);
41290Sstevel@tonic-gate 	}
41300Sstevel@tonic-gate 	crypto_release_minor(cm);
41310Sstevel@tonic-gate 
4132904Smcpowers 	if (real_provider != NULL)
4133904Smcpowers 		KCF_PROV_REFRELE(real_provider);
4134904Smcpowers 
41350Sstevel@tonic-gate 	if (buffer != NULL) {
41360Sstevel@tonic-gate 		/* random numbers are often used to create keys */
41370Sstevel@tonic-gate 		bzero(buffer, len);
41380Sstevel@tonic-gate 		kmem_free(buffer, len);
41390Sstevel@tonic-gate 	}
41400Sstevel@tonic-gate 
41410Sstevel@tonic-gate 	if (error != 0)
41420Sstevel@tonic-gate 		return (error);
41430Sstevel@tonic-gate 
41440Sstevel@tonic-gate 	STRUCT_FSET(generate_random, gr_return_value, rv);
41450Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(generate_random), arg,
41460Sstevel@tonic-gate 	    STRUCT_SIZE(generate_random)) != 0) {
41470Sstevel@tonic-gate 		return (EFAULT);
41480Sstevel@tonic-gate 	}
41490Sstevel@tonic-gate 	return (0);
41500Sstevel@tonic-gate }
41510Sstevel@tonic-gate 
41520Sstevel@tonic-gate /*
41530Sstevel@tonic-gate  * Copyout a kernel array of attributes to user space.
41540Sstevel@tonic-gate  * u_attrs is the corresponding user space array containing
41550Sstevel@tonic-gate  * user space pointers necessary for the copyout.
41560Sstevel@tonic-gate  */
41570Sstevel@tonic-gate /* ARGSUSED */
41580Sstevel@tonic-gate static int
41590Sstevel@tonic-gate copyout_attributes(int mode, caddr_t out, uint_t count,
41600Sstevel@tonic-gate     crypto_object_attribute_t *k_attrs, caddr_t u_attrs)
41610Sstevel@tonic-gate {
41620Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_attribute, oa);
41630Sstevel@tonic-gate 	caddr_t p, valuep;
41640Sstevel@tonic-gate 	size_t value_len;
41650Sstevel@tonic-gate 	size_t len;
41660Sstevel@tonic-gate 	int i;
41670Sstevel@tonic-gate 	int error = 0;
41680Sstevel@tonic-gate 
41690Sstevel@tonic-gate 	if (count == 0)
41700Sstevel@tonic-gate 		return (0);
41710Sstevel@tonic-gate 
41720Sstevel@tonic-gate 	STRUCT_INIT(oa, mode);
41730Sstevel@tonic-gate 
41740Sstevel@tonic-gate 	len = count * STRUCT_SIZE(oa);
41750Sstevel@tonic-gate 
41760Sstevel@tonic-gate 	ASSERT(u_attrs != NULL);
41770Sstevel@tonic-gate 	p = u_attrs;
41780Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
41790Sstevel@tonic-gate 		/* can this bcopy be eliminated? */
41800Sstevel@tonic-gate 		bcopy(p, STRUCT_BUF(oa), STRUCT_SIZE(oa));
41810Sstevel@tonic-gate 		value_len = k_attrs[i].oa_value_len;
41820Sstevel@tonic-gate 		STRUCT_FSET(oa, oa_type, k_attrs[i].oa_type);
41830Sstevel@tonic-gate 		STRUCT_FSET(oa, oa_value_len, value_len);
41840Sstevel@tonic-gate 		valuep = STRUCT_FGETP(oa, oa_value);
41850Sstevel@tonic-gate 		if (valuep != NULL && value_len != -1) {
41860Sstevel@tonic-gate 			if (copyout(k_attrs[i].oa_value,
41870Sstevel@tonic-gate 			    valuep, value_len) != 0) {
41880Sstevel@tonic-gate 				error = EFAULT;
41890Sstevel@tonic-gate 				goto out;
41900Sstevel@tonic-gate 			}
41910Sstevel@tonic-gate 		}
41920Sstevel@tonic-gate 		bcopy(STRUCT_BUF(oa), p, STRUCT_SIZE(oa));
41930Sstevel@tonic-gate 		p += STRUCT_SIZE(oa);
41940Sstevel@tonic-gate 	}
41950Sstevel@tonic-gate 	if (copyout(u_attrs, out, len)) {
41960Sstevel@tonic-gate 		error = EFAULT;
41970Sstevel@tonic-gate 	}
41980Sstevel@tonic-gate out:
41990Sstevel@tonic-gate 	return (error);
42000Sstevel@tonic-gate }
42010Sstevel@tonic-gate 
42020Sstevel@tonic-gate 
42030Sstevel@tonic-gate /* ARGSUSED */
42040Sstevel@tonic-gate static int
42050Sstevel@tonic-gate object_create(dev_t dev, caddr_t arg, int mode, int *rval)
42060Sstevel@tonic-gate {
42070Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_create, object_create);
4208904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
42090Sstevel@tonic-gate 	kcf_req_params_t params;
42100Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
42110Sstevel@tonic-gate 	crypto_session_id_t session_id;
42120Sstevel@tonic-gate 	crypto_minor_t *cm;
42130Sstevel@tonic-gate 	crypto_session_data_t *sp = NULL;
42140Sstevel@tonic-gate 	crypto_object_id_t object_handle;
42150Sstevel@tonic-gate 	caddr_t oc_attributes;
42160Sstevel@tonic-gate 	size_t k_attrs_size;
42170Sstevel@tonic-gate 	size_t rctl_bytes = 0;
42180Sstevel@tonic-gate 	int error = 0;
42190Sstevel@tonic-gate 	int rv;
42200Sstevel@tonic-gate 	uint_t count;
42210Sstevel@tonic-gate 
42220Sstevel@tonic-gate 	STRUCT_INIT(object_create, mode);
42230Sstevel@tonic-gate 
42240Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
42250Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_create: failed holding minor");
42260Sstevel@tonic-gate 		return (ENXIO);
42270Sstevel@tonic-gate 	}
42280Sstevel@tonic-gate 
42290Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(object_create),
42300Sstevel@tonic-gate 	    STRUCT_SIZE(object_create)) != 0) {
42310Sstevel@tonic-gate 		crypto_release_minor(cm);
42320Sstevel@tonic-gate 		return (EFAULT);
42330Sstevel@tonic-gate 	}
42340Sstevel@tonic-gate 
42350Sstevel@tonic-gate 	count = STRUCT_FGET(object_create, oc_count);
42360Sstevel@tonic-gate 	oc_attributes = STRUCT_FGETP(object_create, oc_attributes);
42370Sstevel@tonic-gate 	if (!copyin_attributes(mode, count, oc_attributes, &k_attrs,
42383916Skrishna 	    &k_attrs_size, NULL, &rv, &error, &rctl_bytes, 0, B_TRUE)) {
42390Sstevel@tonic-gate 		goto release_minor;
42400Sstevel@tonic-gate 	}
42410Sstevel@tonic-gate 
42420Sstevel@tonic-gate 	session_id = STRUCT_FGET(object_create, oc_session);
42430Sstevel@tonic-gate 
42440Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
42450Sstevel@tonic-gate 		goto release_minor;
42460Sstevel@tonic-gate 	}
42470Sstevel@tonic-gate 
42480Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
42490Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
42500Sstevel@tonic-gate 	    CRYPTO_OBJECT_OFFSET(object_create),
4251904Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider))
4252904Smcpowers 	    != CRYPTO_SUCCESS) {
42530Sstevel@tonic-gate 		goto release_minor;
42540Sstevel@tonic-gate 	}
42550Sstevel@tonic-gate 
42560Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_CREATE,
42570Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, 0, k_attrs, count,
42580Sstevel@tonic-gate 	    &object_handle, 0, NULL, NULL, 0, NULL);
42590Sstevel@tonic-gate 
42600Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
42610Sstevel@tonic-gate 
42620Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS)
42630Sstevel@tonic-gate 		STRUCT_FSET(object_create, oc_handle, object_handle);
42640Sstevel@tonic-gate 
42650Sstevel@tonic-gate release_minor:
42660Sstevel@tonic-gate 	if (rctl_bytes != 0) {
42673916Skrishna 		CRYPTO_DECREMENT_RCTL(rctl_bytes);
42680Sstevel@tonic-gate 	}
42690Sstevel@tonic-gate 
42700Sstevel@tonic-gate 	if (k_attrs != NULL)
42710Sstevel@tonic-gate 		kmem_free(k_attrs, k_attrs_size);
42720Sstevel@tonic-gate 
42730Sstevel@tonic-gate 	if (error != 0)
42740Sstevel@tonic-gate 		goto out;
42750Sstevel@tonic-gate 
42760Sstevel@tonic-gate 	STRUCT_FSET(object_create, oc_return_value, rv);
42770Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(object_create), arg,
42780Sstevel@tonic-gate 	    STRUCT_SIZE(object_create)) != 0) {
42790Sstevel@tonic-gate 		if (rv == CRYPTO_SUCCESS) {
42800Sstevel@tonic-gate 			KCF_WRAP_OBJECT_OPS_PARAMS(&params,
42810Sstevel@tonic-gate 			    KCF_OP_OBJECT_DESTROY,
42820Sstevel@tonic-gate 			    sp->sd_provider_session->ps_session, object_handle,
42830Sstevel@tonic-gate 			    NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
42840Sstevel@tonic-gate 
42850Sstevel@tonic-gate 			(void) kcf_submit_request(real_provider, NULL,
42860Sstevel@tonic-gate 			    NULL, &params, B_FALSE);
42870Sstevel@tonic-gate 
42880Sstevel@tonic-gate 			error = EFAULT;
42890Sstevel@tonic-gate 		}
42900Sstevel@tonic-gate 	}
42910Sstevel@tonic-gate out:
42920Sstevel@tonic-gate 	if (sp != NULL)
42930Sstevel@tonic-gate 		CRYPTO_SESSION_RELE(sp);
42940Sstevel@tonic-gate 	crypto_release_minor(cm);
4295904Smcpowers 	if (real_provider != NULL)
4296904Smcpowers 		KCF_PROV_REFRELE(real_provider);
42970Sstevel@tonic-gate 	return (error);
42980Sstevel@tonic-gate }
42990Sstevel@tonic-gate 
43000Sstevel@tonic-gate /* ARGSUSED */
43010Sstevel@tonic-gate static int
43020Sstevel@tonic-gate object_copy(dev_t dev, caddr_t arg, int mode, int *rval)
43030Sstevel@tonic-gate {
43040Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_copy, object_copy);
4305904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
43060Sstevel@tonic-gate 	kcf_req_params_t params;
43070Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
43080Sstevel@tonic-gate 	crypto_session_id_t session_id;
43090Sstevel@tonic-gate 	crypto_minor_t *cm;
43100Sstevel@tonic-gate 	crypto_session_data_t *sp = NULL;
43110Sstevel@tonic-gate 	crypto_object_id_t handle, new_handle;
43120Sstevel@tonic-gate 	caddr_t oc_new_attributes;
43130Sstevel@tonic-gate 	size_t k_attrs_size;
43140Sstevel@tonic-gate 	size_t rctl_bytes = 0;
43150Sstevel@tonic-gate 	int error = 0;
43160Sstevel@tonic-gate 	int rv;
43170Sstevel@tonic-gate 	uint_t count;
43180Sstevel@tonic-gate 
43190Sstevel@tonic-gate 	STRUCT_INIT(object_copy, mode);
43200Sstevel@tonic-gate 
43210Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
43220Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_copy: failed holding minor");
43230Sstevel@tonic-gate 		return (ENXIO);
43240Sstevel@tonic-gate 	}
43250Sstevel@tonic-gate 
43260Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(object_copy),
43270Sstevel@tonic-gate 	    STRUCT_SIZE(object_copy)) != 0) {
43280Sstevel@tonic-gate 		crypto_release_minor(cm);
43290Sstevel@tonic-gate 		return (EFAULT);
43300Sstevel@tonic-gate 	}
43310Sstevel@tonic-gate 
43320Sstevel@tonic-gate 	count = STRUCT_FGET(object_copy, oc_count);
43330Sstevel@tonic-gate 	oc_new_attributes = STRUCT_FGETP(object_copy, oc_new_attributes);
43340Sstevel@tonic-gate 	if (!copyin_attributes(mode, count, oc_new_attributes, &k_attrs,
43353916Skrishna 	    &k_attrs_size, NULL, &rv, &error, &rctl_bytes, 0, B_TRUE)) {
43360Sstevel@tonic-gate 		goto release_minor;
43370Sstevel@tonic-gate 	}
43380Sstevel@tonic-gate 
43390Sstevel@tonic-gate 	session_id = STRUCT_FGET(object_copy, oc_session);
43400Sstevel@tonic-gate 
43410Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
43420Sstevel@tonic-gate 		goto release_minor;
43430Sstevel@tonic-gate 	}
43440Sstevel@tonic-gate 
43450Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
43460Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
4347904Smcpowers 	    CRYPTO_OBJECT_OFFSET(object_copy), CHECK_RESTRICT_FALSE,
43480Sstevel@tonic-gate 	    sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
43490Sstevel@tonic-gate 		goto release_minor;
43500Sstevel@tonic-gate 	}
43510Sstevel@tonic-gate 
43520Sstevel@tonic-gate 	handle = STRUCT_FGET(object_copy, oc_handle);
43530Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_COPY,
43540Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, handle, k_attrs, count,
43550Sstevel@tonic-gate 	    &new_handle, 0, NULL, NULL, 0, NULL);
43560Sstevel@tonic-gate 
43570Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
43580Sstevel@tonic-gate 
43590Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS)
43600Sstevel@tonic-gate 		STRUCT_FSET(object_copy, oc_new_handle, new_handle);
43610Sstevel@tonic-gate 
43620Sstevel@tonic-gate release_minor:
43630Sstevel@tonic-gate 	if (rctl_bytes != 0) {
43643916Skrishna 		CRYPTO_DECREMENT_RCTL(rctl_bytes);
43650Sstevel@tonic-gate 	}
43660Sstevel@tonic-gate 
43670Sstevel@tonic-gate 	if (k_attrs != NULL)
43680Sstevel@tonic-gate 		kmem_free(k_attrs, k_attrs_size);
43690Sstevel@tonic-gate 
43700Sstevel@tonic-gate 	if (error != 0)
43710Sstevel@tonic-gate 		goto out;
43720Sstevel@tonic-gate 
43730Sstevel@tonic-gate 	STRUCT_FSET(object_copy, oc_return_value, rv);
43740Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(object_copy), arg,
43750Sstevel@tonic-gate 	    STRUCT_SIZE(object_copy)) != 0) {
43760Sstevel@tonic-gate 		if (rv == CRYPTO_SUCCESS) {
43770Sstevel@tonic-gate 			KCF_WRAP_OBJECT_OPS_PARAMS(&params,
43780Sstevel@tonic-gate 			    KCF_OP_OBJECT_DESTROY,
43790Sstevel@tonic-gate 			    sp->sd_provider_session->ps_session, new_handle,
43800Sstevel@tonic-gate 			    NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
43810Sstevel@tonic-gate 
43820Sstevel@tonic-gate 			(void) kcf_submit_request(real_provider, NULL,
43830Sstevel@tonic-gate 			    NULL, &params, B_FALSE);
43840Sstevel@tonic-gate 
43850Sstevel@tonic-gate 			error = EFAULT;
43860Sstevel@tonic-gate 		}
43870Sstevel@tonic-gate 	}
43880Sstevel@tonic-gate out:
43890Sstevel@tonic-gate 	if (sp != NULL)
43900Sstevel@tonic-gate 		CRYPTO_SESSION_RELE(sp);
43910Sstevel@tonic-gate 	crypto_release_minor(cm);
4392904Smcpowers 	if (real_provider != NULL)
4393904Smcpowers 		KCF_PROV_REFRELE(real_provider);
43940Sstevel@tonic-gate 	return (error);
43950Sstevel@tonic-gate }
43960Sstevel@tonic-gate 
43970Sstevel@tonic-gate /* ARGSUSED */
43980Sstevel@tonic-gate static int
43990Sstevel@tonic-gate object_destroy(dev_t dev, caddr_t arg, int mode, int *rval)
44000Sstevel@tonic-gate {
44010Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_destroy, object_destroy);
44020Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
44030Sstevel@tonic-gate 	kcf_req_params_t params;
44040Sstevel@tonic-gate 	crypto_session_id_t session_id;
44050Sstevel@tonic-gate 	crypto_minor_t *cm;
44060Sstevel@tonic-gate 	crypto_session_data_t *sp;
44070Sstevel@tonic-gate 	crypto_object_id_t handle;
44080Sstevel@tonic-gate 	int error = 0;
44090Sstevel@tonic-gate 	int rv;
44100Sstevel@tonic-gate 
44110Sstevel@tonic-gate 	STRUCT_INIT(object_destroy, mode);
44120Sstevel@tonic-gate 
44130Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
44140Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_destroy: failed holding minor");
44150Sstevel@tonic-gate 		return (ENXIO);
44160Sstevel@tonic-gate 	}
44170Sstevel@tonic-gate 
44180Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(object_destroy),
44190Sstevel@tonic-gate 	    STRUCT_SIZE(object_destroy)) != 0) {
44200Sstevel@tonic-gate 		crypto_release_minor(cm);
44210Sstevel@tonic-gate 		return (EFAULT);
44220Sstevel@tonic-gate 	}
44230Sstevel@tonic-gate 
44240Sstevel@tonic-gate 	session_id = STRUCT_FGET(object_destroy, od_session);
44250Sstevel@tonic-gate 
44260Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
44270Sstevel@tonic-gate 		goto release_minor;
44280Sstevel@tonic-gate 	}
44290Sstevel@tonic-gate 
44300Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
44310Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
4432904Smcpowers 	    CRYPTO_OBJECT_OFFSET(object_destroy), CHECK_RESTRICT_FALSE,
44330Sstevel@tonic-gate 	    sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
44340Sstevel@tonic-gate 		goto out;
44350Sstevel@tonic-gate 	}
44360Sstevel@tonic-gate 
44370Sstevel@tonic-gate 	handle = STRUCT_FGET(object_destroy, od_handle);
44380Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_DESTROY,
44390Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, handle, NULL, 0, NULL, 0,
44400Sstevel@tonic-gate 	    NULL, NULL, 0, NULL);
44410Sstevel@tonic-gate 
44420Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
4443904Smcpowers 	KCF_PROV_REFRELE(real_provider);
44440Sstevel@tonic-gate 
44450Sstevel@tonic-gate out:
44460Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
44470Sstevel@tonic-gate 
44480Sstevel@tonic-gate release_minor:
44490Sstevel@tonic-gate 	crypto_release_minor(cm);
44500Sstevel@tonic-gate 
44510Sstevel@tonic-gate 	if (error != 0)
44520Sstevel@tonic-gate 		return (error);
44530Sstevel@tonic-gate 
44540Sstevel@tonic-gate 	STRUCT_FSET(object_destroy, od_return_value, rv);
44550Sstevel@tonic-gate 
44560Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(object_destroy), arg,
44570Sstevel@tonic-gate 	    STRUCT_SIZE(object_destroy)) != 0) {
44580Sstevel@tonic-gate 		return (EFAULT);
44590Sstevel@tonic-gate 	}
44600Sstevel@tonic-gate 	return (0);
44610Sstevel@tonic-gate }
44620Sstevel@tonic-gate 
44630Sstevel@tonic-gate /* ARGSUSED */
44640Sstevel@tonic-gate static int
44650Sstevel@tonic-gate object_get_attribute_value(dev_t dev, caddr_t arg, int mode, int *rval)
44660Sstevel@tonic-gate {
44670Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_get_attribute_value, get_attribute_value);
44680Sstevel@tonic-gate 	/* LINTED E_FUNC_SET_NOT_USED */
44690Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_attribute, oa);
44700Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
44710Sstevel@tonic-gate 	kcf_req_params_t params;
44720Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
44730Sstevel@tonic-gate 	crypto_session_id_t session_id;
44740Sstevel@tonic-gate 	crypto_minor_t *cm;
44750Sstevel@tonic-gate 	crypto_session_data_t *sp;
44760Sstevel@tonic-gate 	crypto_object_id_t handle;
44770Sstevel@tonic-gate 	caddr_t og_attributes;
44780Sstevel@tonic-gate 	caddr_t u_attrs;
44790Sstevel@tonic-gate 	size_t k_attrs_size;
44800Sstevel@tonic-gate 	size_t rctl_bytes = 0;
44810Sstevel@tonic-gate 	int error = 0;
44820Sstevel@tonic-gate 	int rv;
44830Sstevel@tonic-gate 	uint_t count;
44840Sstevel@tonic-gate 
44850Sstevel@tonic-gate 	STRUCT_INIT(get_attribute_value, mode);
44860Sstevel@tonic-gate 	STRUCT_INIT(oa, mode);
44870Sstevel@tonic-gate 
44880Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
44890Sstevel@tonic-gate 		cmn_err(CE_WARN,
44900Sstevel@tonic-gate 		    "object_get_attribute_value: failed holding minor");
44910Sstevel@tonic-gate 		return (ENXIO);
44920Sstevel@tonic-gate 	}
44930Sstevel@tonic-gate 
44940Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(get_attribute_value),
44950Sstevel@tonic-gate 	    STRUCT_SIZE(get_attribute_value)) != 0) {
44960Sstevel@tonic-gate 		crypto_release_minor(cm);
44970Sstevel@tonic-gate 		return (EFAULT);
44980Sstevel@tonic-gate 	}
44990Sstevel@tonic-gate 
45000Sstevel@tonic-gate 	count = STRUCT_FGET(get_attribute_value, og_count);
45010Sstevel@tonic-gate 	og_attributes = STRUCT_FGETP(get_attribute_value, og_attributes);
45020Sstevel@tonic-gate 	if (!copyin_attributes(mode, count, og_attributes, &k_attrs,
45033916Skrishna 	    &k_attrs_size, &u_attrs, &rv, &error, &rctl_bytes, 0, B_FALSE)) {
45040Sstevel@tonic-gate 		goto release_minor;
45050Sstevel@tonic-gate 	}
45060Sstevel@tonic-gate 
45070Sstevel@tonic-gate 	session_id = STRUCT_FGET(get_attribute_value, og_session);
45080Sstevel@tonic-gate 
45090Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
45100Sstevel@tonic-gate 		goto release_minor;
45110Sstevel@tonic-gate 	}
45120Sstevel@tonic-gate 
45130Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
45140Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
45150Sstevel@tonic-gate 	    CRYPTO_OBJECT_OFFSET(object_get_attribute_value),
4516904Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider))
4517904Smcpowers 	    != CRYPTO_SUCCESS) {
45180Sstevel@tonic-gate 		goto out;
45190Sstevel@tonic-gate 	}
45200Sstevel@tonic-gate 
45210Sstevel@tonic-gate 	handle = STRUCT_FGET(get_attribute_value, og_handle);
45220Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_GET_ATTRIBUTE_VALUE,
45230Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, handle, k_attrs, count, NULL,
45240Sstevel@tonic-gate 	    0, NULL, NULL, 0, NULL);
45250Sstevel@tonic-gate 
45260Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
4527904Smcpowers 	KCF_PROV_REFRELE(real_provider);
45280Sstevel@tonic-gate 
45290Sstevel@tonic-gate out:
45300Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
45310Sstevel@tonic-gate 
45320Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS || rv == CRYPTO_ATTRIBUTE_SENSITIVE ||
45330Sstevel@tonic-gate 	    rv == CRYPTO_ATTRIBUTE_TYPE_INVALID ||
45340Sstevel@tonic-gate 	    rv == CRYPTO_BUFFER_TOO_SMALL) {
45350Sstevel@tonic-gate 		error = copyout_attributes(mode,
45360Sstevel@tonic-gate 		    STRUCT_FGETP(get_attribute_value, og_attributes),
45370Sstevel@tonic-gate 		    count, k_attrs, u_attrs);
45380Sstevel@tonic-gate 	}
45390Sstevel@tonic-gate 
45400Sstevel@tonic-gate release_minor:
45410Sstevel@tonic-gate 	if (rctl_bytes != 0) {
45423916Skrishna 		CRYPTO_DECREMENT_RCTL(rctl_bytes);
45430Sstevel@tonic-gate 	}
45440Sstevel@tonic-gate 	crypto_release_minor(cm);
45450Sstevel@tonic-gate 
45460Sstevel@tonic-gate 	if (k_attrs != NULL)
45470Sstevel@tonic-gate 		kmem_free(k_attrs, k_attrs_size);
45480Sstevel@tonic-gate 
45490Sstevel@tonic-gate 	if (u_attrs != NULL)
45500Sstevel@tonic-gate 		kmem_free(u_attrs, count * STRUCT_SIZE(oa));
45510Sstevel@tonic-gate 
45520Sstevel@tonic-gate 	if (error != 0)
45530Sstevel@tonic-gate 		return (error);
45540Sstevel@tonic-gate 
45550Sstevel@tonic-gate 	STRUCT_FSET(get_attribute_value, og_return_value, rv);
45560Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(get_attribute_value), arg,
45570Sstevel@tonic-gate 	    STRUCT_SIZE(get_attribute_value)) != 0) {
45580Sstevel@tonic-gate 		return (EFAULT);
45590Sstevel@tonic-gate 	}
45600Sstevel@tonic-gate 	return (0);
45610Sstevel@tonic-gate }
45620Sstevel@tonic-gate 
45630Sstevel@tonic-gate /* ARGSUSED */
45640Sstevel@tonic-gate static int
45650Sstevel@tonic-gate object_get_size(dev_t dev, caddr_t arg, int mode, int *rval)
45660Sstevel@tonic-gate {
45670Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_get_size, object_get_size);
45680Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
45690Sstevel@tonic-gate 	kcf_req_params_t params;
45700Sstevel@tonic-gate 	crypto_session_id_t session_id;
45710Sstevel@tonic-gate 	crypto_minor_t *cm;
45720Sstevel@tonic-gate 	crypto_session_data_t *sp;
45730Sstevel@tonic-gate 	crypto_object_id_t handle;
45740Sstevel@tonic-gate 	size_t size;
45750Sstevel@tonic-gate 	int error = 0;
45760Sstevel@tonic-gate 	int rv;
45770Sstevel@tonic-gate 
45780Sstevel@tonic-gate 	STRUCT_INIT(object_get_size, mode);
45790Sstevel@tonic-gate 
45800Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
45810Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_get_size: failed holding minor");
45820Sstevel@tonic-gate 		return (ENXIO);
45830Sstevel@tonic-gate 	}
45840Sstevel@tonic-gate 
45850Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(object_get_size),
45860Sstevel@tonic-gate 	    STRUCT_SIZE(object_get_size)) != 0) {
45870Sstevel@tonic-gate 		crypto_release_minor(cm);
45880Sstevel@tonic-gate 		return (EFAULT);
45890Sstevel@tonic-gate 	}
45900Sstevel@tonic-gate 
45910Sstevel@tonic-gate 	session_id = STRUCT_FGET(object_get_size, gs_session);
45920Sstevel@tonic-gate 
45930Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
45940Sstevel@tonic-gate 		goto release_minor;
45950Sstevel@tonic-gate 	}
45960Sstevel@tonic-gate 
45970Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
45980Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
4599904Smcpowers 	    CRYPTO_OBJECT_OFFSET(object_get_size), CHECK_RESTRICT_FALSE,
46000Sstevel@tonic-gate 	    sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
46010Sstevel@tonic-gate 		goto out;
46020Sstevel@tonic-gate 	}
46030Sstevel@tonic-gate 
46040Sstevel@tonic-gate 	handle = STRUCT_FGET(object_get_size, gs_handle);
46050Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_GET_SIZE,
46060Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, handle, NULL, 0, NULL, &size,
46070Sstevel@tonic-gate 	    NULL, NULL, 0, NULL);
46080Sstevel@tonic-gate 
46090Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
4610904Smcpowers 	KCF_PROV_REFRELE(real_provider);
46110Sstevel@tonic-gate 
46120Sstevel@tonic-gate out:
46130Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
46140Sstevel@tonic-gate 
46150Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
46160Sstevel@tonic-gate 		STRUCT_FSET(object_get_size, gs_size, size);
46170Sstevel@tonic-gate 	}
46180Sstevel@tonic-gate release_minor:
46190Sstevel@tonic-gate 	crypto_release_minor(cm);
46200Sstevel@tonic-gate 
46210Sstevel@tonic-gate 	if (error != 0)
46220Sstevel@tonic-gate 		return (error);
46230Sstevel@tonic-gate 
46240Sstevel@tonic-gate 	STRUCT_FSET(object_get_size, gs_return_value, rv);
46250Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(object_get_size), arg,
46260Sstevel@tonic-gate 	    STRUCT_SIZE(object_get_size)) != 0) {
46270Sstevel@tonic-gate 		return (EFAULT);
46280Sstevel@tonic-gate 	}
46290Sstevel@tonic-gate 	return (0);
46300Sstevel@tonic-gate }
46310Sstevel@tonic-gate 
46320Sstevel@tonic-gate /* ARGSUSED */
46330Sstevel@tonic-gate static int
46340Sstevel@tonic-gate object_set_attribute_value(dev_t dev, caddr_t arg, int mode, int *rval)
46350Sstevel@tonic-gate {
46360Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_set_attribute_value, set_attribute_value);
46370Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
46380Sstevel@tonic-gate 	kcf_req_params_t params;
46390Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
46400Sstevel@tonic-gate 	crypto_session_id_t session_id;
46410Sstevel@tonic-gate 	crypto_minor_t *cm;
46420Sstevel@tonic-gate 	crypto_session_data_t *sp;
46430Sstevel@tonic-gate 	crypto_object_id_t object_handle;
46440Sstevel@tonic-gate 	caddr_t sa_attributes;
46450Sstevel@tonic-gate 	size_t k_attrs_size;
46460Sstevel@tonic-gate 	size_t rctl_bytes = 0;
46470Sstevel@tonic-gate 	int error = 0;
46480Sstevel@tonic-gate 	int rv;
46490Sstevel@tonic-gate 	uint_t count;
46500Sstevel@tonic-gate 
46510Sstevel@tonic-gate 	STRUCT_INIT(set_attribute_value, mode);
46520Sstevel@tonic-gate 
46530Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
46540Sstevel@tonic-gate 		cmn_err(CE_WARN,
46550Sstevel@tonic-gate 		    "object_set_attribute_value: failed holding minor");
46560Sstevel@tonic-gate 		return (ENXIO);
46570Sstevel@tonic-gate 	}
46580Sstevel@tonic-gate 
46590Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(set_attribute_value),
46600Sstevel@tonic-gate 	    STRUCT_SIZE(set_attribute_value)) != 0) {
46610Sstevel@tonic-gate 		crypto_release_minor(cm);
46620Sstevel@tonic-gate 		return (EFAULT);
46630Sstevel@tonic-gate 	}
46640Sstevel@tonic-gate 
46650Sstevel@tonic-gate 	count = STRUCT_FGET(set_attribute_value, sa_count);
46660Sstevel@tonic-gate 	sa_attributes = STRUCT_FGETP(set_attribute_value, sa_attributes);
46670Sstevel@tonic-gate 	if (!copyin_attributes(mode, count, sa_attributes, &k_attrs,
46683916Skrishna 	    &k_attrs_size, NULL, &rv, &error, &rctl_bytes, 0, B_TRUE)) {
46690Sstevel@tonic-gate 		goto release_minor;
46700Sstevel@tonic-gate 	}
46710Sstevel@tonic-gate 
46720Sstevel@tonic-gate 	session_id = STRUCT_FGET(set_attribute_value, sa_session);
46730Sstevel@tonic-gate 
46740Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
46750Sstevel@tonic-gate 		goto release_minor;
46760Sstevel@tonic-gate 	}
46770Sstevel@tonic-gate 
46780Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
46790Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
46800Sstevel@tonic-gate 	    CRYPTO_OBJECT_OFFSET(object_set_attribute_value),
4681904Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider))
4682904Smcpowers 	    != CRYPTO_SUCCESS) {
46830Sstevel@tonic-gate 		goto out;
46840Sstevel@tonic-gate 	}
46850Sstevel@tonic-gate 
46860Sstevel@tonic-gate 	object_handle = STRUCT_FGET(set_attribute_value, sa_handle);
46870Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_SET_ATTRIBUTE_VALUE,
46880Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, object_handle, k_attrs, count,
46890Sstevel@tonic-gate 	    NULL, 0, NULL, NULL, 0, NULL);
46900Sstevel@tonic-gate 
46910Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
4692904Smcpowers 	KCF_PROV_REFRELE(real_provider);
46930Sstevel@tonic-gate 
46940Sstevel@tonic-gate out:
46950Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
46960Sstevel@tonic-gate 
46970Sstevel@tonic-gate release_minor:
46980Sstevel@tonic-gate 	if (rctl_bytes != 0) {
46993916Skrishna 		CRYPTO_DECREMENT_RCTL(rctl_bytes);
47000Sstevel@tonic-gate 	}
47010Sstevel@tonic-gate 	crypto_release_minor(cm);
47020Sstevel@tonic-gate 
47030Sstevel@tonic-gate 	if (k_attrs != NULL)
47040Sstevel@tonic-gate 		kmem_free(k_attrs, k_attrs_size);
47050Sstevel@tonic-gate 
47060Sstevel@tonic-gate 	if (error != 0)
47070Sstevel@tonic-gate 		return (error);
47080Sstevel@tonic-gate 
47090Sstevel@tonic-gate 	STRUCT_FSET(set_attribute_value, sa_return_value, rv);
47100Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(set_attribute_value), arg,
47110Sstevel@tonic-gate 	    STRUCT_SIZE(set_attribute_value)) != 0) {
47120Sstevel@tonic-gate 		return (EFAULT);
47130Sstevel@tonic-gate 	}
47140Sstevel@tonic-gate 	return (0);
47150Sstevel@tonic-gate }
47160Sstevel@tonic-gate 
47170Sstevel@tonic-gate /* ARGSUSED */
47180Sstevel@tonic-gate static int
47190Sstevel@tonic-gate object_find_init(dev_t dev, caddr_t arg, int mode, int *rval)
47200Sstevel@tonic-gate {
47210Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_find_init, find_init);
4722904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
47230Sstevel@tonic-gate 	kcf_req_params_t params;
47240Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
47250Sstevel@tonic-gate 	crypto_session_id_t session_id;
47260Sstevel@tonic-gate 	crypto_minor_t *cm;
47270Sstevel@tonic-gate 	crypto_session_data_t *sp;
47280Sstevel@tonic-gate 	caddr_t attributes;
47290Sstevel@tonic-gate 	size_t k_attrs_size;
47300Sstevel@tonic-gate 	size_t rctl_bytes = 0;
47310Sstevel@tonic-gate 	int error = 0;
47320Sstevel@tonic-gate 	int rv;
47330Sstevel@tonic-gate 	uint_t count;
47340Sstevel@tonic-gate 	void *cookie;
47350Sstevel@tonic-gate 
47360Sstevel@tonic-gate 	STRUCT_INIT(find_init, mode);
47370Sstevel@tonic-gate 
47380Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
47390Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_find_init: failed holding minor");
47400Sstevel@tonic-gate 		return (ENXIO);
47410Sstevel@tonic-gate 	}
47420Sstevel@tonic-gate 
47430Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(find_init), STRUCT_SIZE(find_init)) != 0) {
47440Sstevel@tonic-gate 		crypto_release_minor(cm);
47450Sstevel@tonic-gate 		return (EFAULT);
47460Sstevel@tonic-gate 	}
47470Sstevel@tonic-gate 
47480Sstevel@tonic-gate 	count = STRUCT_FGET(find_init, fi_count);
47490Sstevel@tonic-gate 	attributes = STRUCT_FGETP(find_init, fi_attributes);
47500Sstevel@tonic-gate 	if (!copyin_attributes(mode, count, attributes, &k_attrs,
47513916Skrishna 	    &k_attrs_size, NULL, &rv, &error, &rctl_bytes, 0, B_TRUE)) {
47520Sstevel@tonic-gate 		goto release_minor;
47530Sstevel@tonic-gate 	}
47540Sstevel@tonic-gate 
47550Sstevel@tonic-gate 	session_id = STRUCT_FGET(find_init, fi_session);
47560Sstevel@tonic-gate 
47570Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
47580Sstevel@tonic-gate 		goto release_minor;
47590Sstevel@tonic-gate 	}
47600Sstevel@tonic-gate 
47610Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
47620Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
4763904Smcpowers 	    CRYPTO_OBJECT_OFFSET(object_find_init), CHECK_RESTRICT_FALSE,
47640Sstevel@tonic-gate 	    sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
47650Sstevel@tonic-gate 		goto out;
47660Sstevel@tonic-gate 	}
47670Sstevel@tonic-gate 
47680Sstevel@tonic-gate 	/* check for an active find */
47690Sstevel@tonic-gate 	if (sp->sd_find_init_cookie != NULL) {
47700Sstevel@tonic-gate 		rv = CRYPTO_OPERATION_IS_ACTIVE;
47710Sstevel@tonic-gate 		CRYPTO_SESSION_RELE(sp);
47720Sstevel@tonic-gate 		goto release_minor;
47730Sstevel@tonic-gate 	}
47740Sstevel@tonic-gate 
47750Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_FIND_INIT,
47760Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, 0, k_attrs, count, NULL, 0,
47770Sstevel@tonic-gate 	    &cookie, NULL, 0, NULL);
47780Sstevel@tonic-gate 
47790Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
47800Sstevel@tonic-gate 
47810Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
47820Sstevel@tonic-gate 		/*
47830Sstevel@tonic-gate 		 * The cookie is allocated by a provider at the start of an
47840Sstevel@tonic-gate 		 * object search.  It is freed when the search is terminated
47850Sstevel@tonic-gate 		 * by a final operation, or when the session is closed.
47860Sstevel@tonic-gate 		 * It contains state information about which object handles
47870Sstevel@tonic-gate 		 * have been returned to the caller.
47880Sstevel@tonic-gate 		 */
47890Sstevel@tonic-gate 		sp->sd_find_init_cookie = cookie;
47900Sstevel@tonic-gate 	}
47910Sstevel@tonic-gate 
47920Sstevel@tonic-gate out:
47930Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
47940Sstevel@tonic-gate 
47950Sstevel@tonic-gate release_minor:
47960Sstevel@tonic-gate 	if (rctl_bytes != 0) {
47973916Skrishna 		CRYPTO_DECREMENT_RCTL(rctl_bytes);
47980Sstevel@tonic-gate 	}
47990Sstevel@tonic-gate 	crypto_release_minor(cm);
48000Sstevel@tonic-gate 
4801904Smcpowers 	if (real_provider != NULL)
4802904Smcpowers 		KCF_PROV_REFRELE(real_provider);
4803904Smcpowers 
48040Sstevel@tonic-gate 	if (k_attrs != NULL)
48050Sstevel@tonic-gate 		kmem_free(k_attrs, k_attrs_size);
48060Sstevel@tonic-gate 
48070Sstevel@tonic-gate 	if (error != 0)
48080Sstevel@tonic-gate 		return (error);
48090Sstevel@tonic-gate 
48100Sstevel@tonic-gate 	STRUCT_FSET(find_init, fi_return_value, rv);
48110Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(find_init), arg, STRUCT_SIZE(find_init)) != 0) {
48120Sstevel@tonic-gate 		return (EFAULT);
48130Sstevel@tonic-gate 	}
48140Sstevel@tonic-gate 	return (0);
48150Sstevel@tonic-gate }
48160Sstevel@tonic-gate 
48170Sstevel@tonic-gate /* ARGSUSED */
48180Sstevel@tonic-gate static int
48190Sstevel@tonic-gate object_find_update(dev_t dev, caddr_t arg, int mode, int *rval)
48200Sstevel@tonic-gate {
48210Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_find_update, find_update);
48220Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
48230Sstevel@tonic-gate 	kcf_req_params_t params;
48240Sstevel@tonic-gate 	crypto_minor_t *cm;
48250Sstevel@tonic-gate 	crypto_session_data_t *sp;
48260Sstevel@tonic-gate 	crypto_object_id_t *buffer = NULL;
48270Sstevel@tonic-gate 	crypto_session_id_t session_id;
48280Sstevel@tonic-gate 	size_t len, rctl_bytes = 0;
48290Sstevel@tonic-gate 	uint_t count, max_count;
48300Sstevel@tonic-gate 	int rv, error = 0;
48310Sstevel@tonic-gate 
48320Sstevel@tonic-gate 	STRUCT_INIT(find_update, mode);
48330Sstevel@tonic-gate 
48340Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
48350Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_find_update: failed holding minor");
48360Sstevel@tonic-gate 		return (ENXIO);
48370Sstevel@tonic-gate 	}
48380Sstevel@tonic-gate 
48390Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(find_update),
48400Sstevel@tonic-gate 	    STRUCT_SIZE(find_update)) != 0) {
48410Sstevel@tonic-gate 		crypto_release_minor(cm);
48420Sstevel@tonic-gate 		return (EFAULT);
48430Sstevel@tonic-gate 	}
48440Sstevel@tonic-gate 
48450Sstevel@tonic-gate 	max_count = STRUCT_FGET(find_update, fu_max_count);
48460Sstevel@tonic-gate 	if (max_count > CRYPTO_MAX_FIND_COUNT) {
48470Sstevel@tonic-gate 		cmn_err(CE_NOTE, "object_find_update: count greater than %d, "
48480Sstevel@tonic-gate 		    "pid = %d", CRYPTO_MAX_FIND_COUNT, curproc->p_pid);
48490Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
48500Sstevel@tonic-gate 		goto release_minor;
48510Sstevel@tonic-gate 	}
48520Sstevel@tonic-gate 	len = max_count * sizeof (crypto_object_id_t);
48533916Skrishna 	if ((rv = crypto_buffer_check(len)) != CRYPTO_SUCCESS) {
48540Sstevel@tonic-gate 		goto release_minor;
48550Sstevel@tonic-gate 	}
48560Sstevel@tonic-gate 	rctl_bytes = len;
48570Sstevel@tonic-gate 	buffer = kmem_alloc(len, KM_SLEEP);
48580Sstevel@tonic-gate 
48590Sstevel@tonic-gate 	session_id = STRUCT_FGET(find_update, fu_session);
48600Sstevel@tonic-gate 
48610Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
48620Sstevel@tonic-gate 		goto release_minor;
48630Sstevel@tonic-gate 	}
48640Sstevel@tonic-gate 
48650Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
48660Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
4867904Smcpowers 	    CRYPTO_OBJECT_OFFSET(object_find), CHECK_RESTRICT_FALSE,
48680Sstevel@tonic-gate 	    sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
48690Sstevel@tonic-gate 		goto out;
48700Sstevel@tonic-gate 	}
48710Sstevel@tonic-gate 
48720Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_FIND,
48730Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, 0, NULL, 0, buffer, 0,
48740Sstevel@tonic-gate 	    NULL, sp->sd_find_init_cookie, max_count, &count);
48750Sstevel@tonic-gate 
48760Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
4877904Smcpowers 	KCF_PROV_REFRELE(real_provider);
48780Sstevel@tonic-gate 
48790Sstevel@tonic-gate out:
48800Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
48810Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
48820Sstevel@tonic-gate 		if (count > max_count) {
48830Sstevel@tonic-gate 			/* bad bad provider */
48840Sstevel@tonic-gate 			rv = CRYPTO_FAILED;
48850Sstevel@tonic-gate 			goto release_minor;
48860Sstevel@tonic-gate 		}
48870Sstevel@tonic-gate 		if (count != 0) {
48880Sstevel@tonic-gate 			/* copyout handles */
48890Sstevel@tonic-gate 			if (copyout(buffer,
48900Sstevel@tonic-gate 			    STRUCT_FGETP(find_update, fu_handles),
48910Sstevel@tonic-gate 			    count * sizeof (crypto_object_id_t)) != 0) {
48920Sstevel@tonic-gate 				error = EFAULT;
48930Sstevel@tonic-gate 			}
48940Sstevel@tonic-gate 		}
48950Sstevel@tonic-gate 		STRUCT_FSET(find_update, fu_count, count);
48960Sstevel@tonic-gate 	}
48970Sstevel@tonic-gate 
48980Sstevel@tonic-gate release_minor:
48990Sstevel@tonic-gate 	if (rctl_bytes != 0) {
49003916Skrishna 		CRYPTO_DECREMENT_RCTL(rctl_bytes);
49010Sstevel@tonic-gate 	}
49020Sstevel@tonic-gate 	crypto_release_minor(cm);
49030Sstevel@tonic-gate 
49040Sstevel@tonic-gate 	if (buffer != NULL)
49050Sstevel@tonic-gate 		kmem_free(buffer, len);
49060Sstevel@tonic-gate 
49070Sstevel@tonic-gate 	if (error != 0)
49080Sstevel@tonic-gate 		return (error);
49090Sstevel@tonic-gate 
49100Sstevel@tonic-gate 	STRUCT_FSET(find_update, fu_return_value, rv);
49110Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(find_update), arg,
49120Sstevel@tonic-gate 	    STRUCT_SIZE(find_update)) != 0) {
49130Sstevel@tonic-gate 		return (EFAULT);
49140Sstevel@tonic-gate 	}
49150Sstevel@tonic-gate 
49160Sstevel@tonic-gate 	return (0);
49170Sstevel@tonic-gate }
49180Sstevel@tonic-gate 
49190Sstevel@tonic-gate /*
49200Sstevel@tonic-gate  * Free provider-allocated storage used for find object searches.
49210Sstevel@tonic-gate  */
49220Sstevel@tonic-gate static int
49230Sstevel@tonic-gate crypto_free_find_ctx(crypto_session_data_t *sp)
49240Sstevel@tonic-gate {
49250Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
49260Sstevel@tonic-gate 	kcf_req_params_t params;
49270Sstevel@tonic-gate 	int rv;
49280Sstevel@tonic-gate 
49290Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
49300Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
4931904Smcpowers 	    CRYPTO_OBJECT_OFFSET(object_find_final), CHECK_RESTRICT_FALSE,
49320Sstevel@tonic-gate 	    sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
49330Sstevel@tonic-gate 		return (rv);
49340Sstevel@tonic-gate 	}
49350Sstevel@tonic-gate 
49360Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_FIND_FINAL,
49370Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, 0, NULL, 0, NULL, 0,
49380Sstevel@tonic-gate 	    NULL, sp->sd_find_init_cookie, 0, NULL);
49390Sstevel@tonic-gate 
4940904Smcpowers 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
4941904Smcpowers 	KCF_PROV_REFRELE(real_provider);
4942904Smcpowers 	return (rv);
49430Sstevel@tonic-gate }
49440Sstevel@tonic-gate 
49450Sstevel@tonic-gate /* ARGSUSED */
49460Sstevel@tonic-gate static int
49470Sstevel@tonic-gate object_find_final(dev_t dev, caddr_t arg, int mode, int *rval)
49480Sstevel@tonic-gate {
49490Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_find_final, object_find_final);
49500Sstevel@tonic-gate 	crypto_session_id_t session_id;
49510Sstevel@tonic-gate 	crypto_minor_t *cm;
49520Sstevel@tonic-gate 	crypto_session_data_t *sp;
49530Sstevel@tonic-gate 	int error = 0;
49540Sstevel@tonic-gate 	int rv;
49550Sstevel@tonic-gate 
49560Sstevel@tonic-gate 	STRUCT_INIT(object_find_final, mode);
49570Sstevel@tonic-gate 
49580Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
49590Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_find_final: failed holding minor");
49600Sstevel@tonic-gate 		return (ENXIO);
49610Sstevel@tonic-gate 	}
49620Sstevel@tonic-gate 
49630Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(object_find_final),
49640Sstevel@tonic-gate 	    STRUCT_SIZE(object_find_final)) != 0) {
49650Sstevel@tonic-gate 		crypto_release_minor(cm);
49660Sstevel@tonic-gate 		return (EFAULT);
49670Sstevel@tonic-gate 	}
49680Sstevel@tonic-gate 
49690Sstevel@tonic-gate 	session_id = STRUCT_FGET(object_find_final, ff_session);
49700Sstevel@tonic-gate 
49710Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
49720Sstevel@tonic-gate 		goto release_minor;
49730Sstevel@tonic-gate 	}
49740Sstevel@tonic-gate 
49750Sstevel@tonic-gate 	if ((rv = crypto_free_find_ctx(sp)) == CRYPTO_SUCCESS) {
49760Sstevel@tonic-gate 		sp->sd_find_init_cookie = NULL;
49770Sstevel@tonic-gate 	}
49780Sstevel@tonic-gate 
49790Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
49800Sstevel@tonic-gate 
49810Sstevel@tonic-gate release_minor:
49820Sstevel@tonic-gate 	crypto_release_minor(cm);
49830Sstevel@tonic-gate 
49840Sstevel@tonic-gate 	if (error != 0)
49850Sstevel@tonic-gate 		return (error);
49860Sstevel@tonic-gate 
49870Sstevel@tonic-gate 	STRUCT_FSET(object_find_final, ff_return_value, rv);
49880Sstevel@tonic-gate 
49890Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(object_find_final), arg,
49900Sstevel@tonic-gate 	    STRUCT_SIZE(object_find_final)) != 0) {
49910Sstevel@tonic-gate 		return (EFAULT);
49920Sstevel@tonic-gate 	}
49930Sstevel@tonic-gate 	return (0);
49940Sstevel@tonic-gate }
49950Sstevel@tonic-gate 
49960Sstevel@tonic-gate /* ARGSUSED */
49970Sstevel@tonic-gate static int
49980Sstevel@tonic-gate object_generate_key(dev_t dev, caddr_t arg, int mode, int *rval)
49990Sstevel@tonic-gate {
50000Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_generate_key, generate_key);
5001904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
50020Sstevel@tonic-gate 	kcf_req_params_t params;
50030Sstevel@tonic-gate 	crypto_mechanism_t mech;
50040Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
50050Sstevel@tonic-gate 	crypto_session_id_t session_id;
50060Sstevel@tonic-gate 	crypto_minor_t *cm;
50070Sstevel@tonic-gate 	crypto_session_data_t *sp = NULL;
50080Sstevel@tonic-gate 	crypto_object_id_t key_handle;
50090Sstevel@tonic-gate 	caddr_t attributes;
50100Sstevel@tonic-gate 	size_t k_attrs_size;
50110Sstevel@tonic-gate 	size_t mech_rctl_bytes = 0, key_rctl_bytes = 0;
50120Sstevel@tonic-gate 	size_t carry;
50130Sstevel@tonic-gate 	uint_t count;
50140Sstevel@tonic-gate 	int error = 0;
50150Sstevel@tonic-gate 	int rv;
5016904Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
50170Sstevel@tonic-gate 
50180Sstevel@tonic-gate 	STRUCT_INIT(generate_key, mode);
50190Sstevel@tonic-gate 
50200Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
50210Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_generate_key: failed holding minor");
50220Sstevel@tonic-gate 		return (ENXIO);
50230Sstevel@tonic-gate 	}
50240Sstevel@tonic-gate 
50250Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(generate_key),
50260Sstevel@tonic-gate 	    STRUCT_SIZE(generate_key)) != 0) {
50270Sstevel@tonic-gate 		crypto_release_minor(cm);
50280Sstevel@tonic-gate 		return (EFAULT);
50290Sstevel@tonic-gate 	}
50300Sstevel@tonic-gate 
50310Sstevel@tonic-gate 	session_id = STRUCT_FGET(generate_key, gk_session);
50320Sstevel@tonic-gate 
50330Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
50340Sstevel@tonic-gate 		goto release_minor;
50350Sstevel@tonic-gate 	}
50360Sstevel@tonic-gate 
5037904Smcpowers 	bcopy(STRUCT_FADDR(generate_key, gk_mechanism), &mech.cm_type,
5038904Smcpowers 	    sizeof (crypto_mech_type_t));
5039904Smcpowers 
5040904Smcpowers 	if ((rv = kcf_get_hardware_provider(mech.cm_type, CRYPTO_MECH_INVALID,
50411808Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider,
50421808Smcpowers 	    CRYPTO_FG_GENERATE)) != CRYPTO_SUCCESS) {
50430Sstevel@tonic-gate 		goto release_minor;
50440Sstevel@tonic-gate 	}
50450Sstevel@tonic-gate 
5046904Smcpowers 	carry = 0;
5047904Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
5048904Smcpowers 	    STRUCT_FADDR(generate_key, gk_mechanism), &mech, mode, &error);
5049904Smcpowers 
5050904Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
5051904Smcpowers 		allocated_by_crypto_module = B_TRUE;
5052904Smcpowers 		if (!copyin_mech(mode, STRUCT_FADDR(generate_key, gk_mechanism),
50533916Skrishna 		    &mech, &mech_rctl_bytes, &carry, &rv, &error)) {
5054904Smcpowers 			goto release_minor;
5055904Smcpowers 		}
5056904Smcpowers 	} else {
5057904Smcpowers 		if (rv != CRYPTO_SUCCESS)
5058904Smcpowers 			goto release_minor;
50590Sstevel@tonic-gate 	}
50600Sstevel@tonic-gate 
50610Sstevel@tonic-gate 	count = STRUCT_FGET(generate_key, gk_count);
50620Sstevel@tonic-gate 	attributes = STRUCT_FGETP(generate_key, gk_attributes);
50630Sstevel@tonic-gate 	if (!copyin_attributes(mode, count, attributes, &k_attrs,
50643916Skrishna 	    &k_attrs_size, NULL, &rv, &error, &key_rctl_bytes, carry, B_TRUE)) {
50650Sstevel@tonic-gate 		goto release_minor;
50660Sstevel@tonic-gate 	}
50670Sstevel@tonic-gate 
50680Sstevel@tonic-gate 	KCF_WRAP_KEY_OPS_PARAMS(&params, KCF_OP_KEY_GENERATE,
50690Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, &mech, k_attrs, count,
50700Sstevel@tonic-gate 	    &key_handle, NULL, 0, NULL, NULL, NULL, 0);
50710Sstevel@tonic-gate 
50720Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
50730Sstevel@tonic-gate 
50740Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS)
50750Sstevel@tonic-gate 		STRUCT_FSET(generate_key, gk_handle, key_handle);
50760Sstevel@tonic-gate 
50770Sstevel@tonic-gate release_minor:
50783916Skrishna 	if (mech_rctl_bytes + key_rctl_bytes != 0)
50793916Skrishna 		CRYPTO_DECREMENT_RCTL(mech_rctl_bytes + key_rctl_bytes);
50800Sstevel@tonic-gate 
50810Sstevel@tonic-gate 	if (k_attrs != NULL)
50820Sstevel@tonic-gate 		kmem_free(k_attrs, k_attrs_size);
50830Sstevel@tonic-gate 
50840Sstevel@tonic-gate 	if (error != 0)
50850Sstevel@tonic-gate 		goto out;
50860Sstevel@tonic-gate 
50870Sstevel@tonic-gate 	STRUCT_FSET(generate_key, gk_return_value, rv);
50880Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(generate_key), arg,
50890Sstevel@tonic-gate 	    STRUCT_SIZE(generate_key)) != 0) {
50900Sstevel@tonic-gate 		if (rv == CRYPTO_SUCCESS) {
50910Sstevel@tonic-gate 			KCF_WRAP_OBJECT_OPS_PARAMS(&params,
50920Sstevel@tonic-gate 			    KCF_OP_OBJECT_DESTROY,
50930Sstevel@tonic-gate 			    sp->sd_provider_session->ps_session, key_handle,
50940Sstevel@tonic-gate 			    NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
50950Sstevel@tonic-gate 
50960Sstevel@tonic-gate 			(void) kcf_submit_request(real_provider, NULL,
50970Sstevel@tonic-gate 			    NULL, &params, B_FALSE);
50980Sstevel@tonic-gate 
50990Sstevel@tonic-gate 			error = EFAULT;
51000Sstevel@tonic-gate 		}
51010Sstevel@tonic-gate 	}
51020Sstevel@tonic-gate out:
51030Sstevel@tonic-gate 	if (sp != NULL)
51040Sstevel@tonic-gate 		CRYPTO_SESSION_RELE(sp);
51050Sstevel@tonic-gate 	crypto_release_minor(cm);
5106904Smcpowers 
5107904Smcpowers 	if (real_provider != NULL) {
5108904Smcpowers 		crypto_free_mech(real_provider,
5109904Smcpowers 		    allocated_by_crypto_module, &mech);
5110904Smcpowers 		KCF_PROV_REFRELE(real_provider);
5111904Smcpowers 	}
51120Sstevel@tonic-gate 	return (error);
51130Sstevel@tonic-gate }
51140Sstevel@tonic-gate 
51150Sstevel@tonic-gate /* ARGSUSED */
51160Sstevel@tonic-gate static int
5117*4219Smcpowers nostore_generate_key(dev_t dev, caddr_t arg, int mode, int *rval)
5118*4219Smcpowers {
5119*4219Smcpowers 	STRUCT_DECL(crypto_nostore_generate_key, generate_key);
5120*4219Smcpowers 	/* LINTED E_FUNC_SET_NOT_USED */
5121*4219Smcpowers 	STRUCT_DECL(crypto_object_attribute, oa);
5122*4219Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
5123*4219Smcpowers 	kcf_req_params_t params;
5124*4219Smcpowers 	crypto_mechanism_t mech;
5125*4219Smcpowers 	crypto_object_attribute_t *k_in_attrs = NULL;
5126*4219Smcpowers 	crypto_object_attribute_t *k_out_attrs = NULL;
5127*4219Smcpowers 	crypto_session_id_t session_id;
5128*4219Smcpowers 	crypto_minor_t *cm;
5129*4219Smcpowers 	crypto_session_data_t *sp = NULL;
5130*4219Smcpowers 	caddr_t in_attributes;
5131*4219Smcpowers 	caddr_t out_attributes;
5132*4219Smcpowers 	size_t k_in_attrs_size;
5133*4219Smcpowers 	size_t k_out_attrs_size;
5134*4219Smcpowers 	size_t mech_rctl_bytes = 0;
5135*4219Smcpowers 	size_t in_key_rctl_bytes = 0, out_key_rctl_bytes = 0;
5136*4219Smcpowers 	size_t carry;
5137*4219Smcpowers 	uint_t in_count;
5138*4219Smcpowers 	uint_t out_count;
5139*4219Smcpowers 	int error = 0;
5140*4219Smcpowers 	int rv;
5141*4219Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
5142*4219Smcpowers 	caddr_t u_attrs = NULL;
5143*4219Smcpowers 
5144*4219Smcpowers 	STRUCT_INIT(generate_key, mode);
5145*4219Smcpowers 	STRUCT_INIT(oa, mode);
5146*4219Smcpowers 
5147*4219Smcpowers 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
5148*4219Smcpowers 		cmn_err(CE_WARN, "nostore_generate_key: failed holding minor");
5149*4219Smcpowers 		return (ENXIO);
5150*4219Smcpowers 	}
5151*4219Smcpowers 
5152*4219Smcpowers 	if (copyin(arg, STRUCT_BUF(generate_key),
5153*4219Smcpowers 	    STRUCT_SIZE(generate_key)) != 0) {
5154*4219Smcpowers 		crypto_release_minor(cm);
5155*4219Smcpowers 		return (EFAULT);
5156*4219Smcpowers 	}
5157*4219Smcpowers 
5158*4219Smcpowers 	session_id = STRUCT_FGET(generate_key, ngk_session);
5159*4219Smcpowers 
5160*4219Smcpowers 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
5161*4219Smcpowers 		goto release_minor;
5162*4219Smcpowers 	}
5163*4219Smcpowers 
5164*4219Smcpowers 	bcopy(STRUCT_FADDR(generate_key, ngk_mechanism), &mech.cm_type,
5165*4219Smcpowers 	    sizeof (crypto_mech_type_t));
5166*4219Smcpowers 
5167*4219Smcpowers 	if ((rv = kcf_get_hardware_provider(mech.cm_type, CRYPTO_MECH_INVALID,
5168*4219Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider,
5169*4219Smcpowers 	    CRYPTO_FG_GENERATE)) != CRYPTO_SUCCESS) {
5170*4219Smcpowers 		goto release_minor;
5171*4219Smcpowers 	}
5172*4219Smcpowers 
5173*4219Smcpowers 	carry = 0;
5174*4219Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
5175*4219Smcpowers 	    STRUCT_FADDR(generate_key, ngk_mechanism), &mech, mode, &error);
5176*4219Smcpowers 
5177*4219Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
5178*4219Smcpowers 		allocated_by_crypto_module = B_TRUE;
5179*4219Smcpowers 		if (!copyin_mech(mode, STRUCT_FADDR(generate_key,
5180*4219Smcpowers 		    ngk_mechanism), &mech, &mech_rctl_bytes, &carry, &rv,
5181*4219Smcpowers 		    &error)) {
5182*4219Smcpowers 			goto release_minor;
5183*4219Smcpowers 		}
5184*4219Smcpowers 	} else {
5185*4219Smcpowers 		if (rv != CRYPTO_SUCCESS)
5186*4219Smcpowers 			goto release_minor;
5187*4219Smcpowers 	}
5188*4219Smcpowers 
5189*4219Smcpowers 	in_count = STRUCT_FGET(generate_key, ngk_in_count);
5190*4219Smcpowers 	in_attributes = STRUCT_FGETP(generate_key, ngk_in_attributes);
5191*4219Smcpowers 	if (!copyin_attributes(mode, in_count, in_attributes, &k_in_attrs,
5192*4219Smcpowers 	    &k_in_attrs_size, NULL, &rv, &error, &in_key_rctl_bytes,
5193*4219Smcpowers 	    carry, B_TRUE)) {
5194*4219Smcpowers 		goto release_minor;
5195*4219Smcpowers 	}
5196*4219Smcpowers 
5197*4219Smcpowers 	out_count = STRUCT_FGET(generate_key, ngk_out_count);
5198*4219Smcpowers 	out_attributes = STRUCT_FGETP(generate_key, ngk_out_attributes);
5199*4219Smcpowers 	if (!copyin_attributes(mode, out_count, out_attributes, &k_out_attrs,
5200*4219Smcpowers 	    &k_out_attrs_size, &u_attrs, &rv, &error, &out_key_rctl_bytes,
5201*4219Smcpowers 	    0, B_FALSE)) {
5202*4219Smcpowers 		goto release_minor;
5203*4219Smcpowers 	}
5204*4219Smcpowers 
5205*4219Smcpowers 	KCF_WRAP_NOSTORE_KEY_OPS_PARAMS(&params, KCF_OP_KEY_GENERATE,
5206*4219Smcpowers 	    sp->sd_provider_session->ps_session, &mech, k_in_attrs, in_count,
5207*4219Smcpowers 	    NULL, 0, NULL, k_out_attrs, out_count, NULL, 0);
5208*4219Smcpowers 
5209*4219Smcpowers 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
5210*4219Smcpowers 
5211*4219Smcpowers 	if (rv == CRYPTO_SUCCESS) {
5212*4219Smcpowers 		error = copyout_attributes(mode, out_attributes,
5213*4219Smcpowers 		    out_count, k_out_attrs, u_attrs);
5214*4219Smcpowers 	}
5215*4219Smcpowers release_minor:
5216*4219Smcpowers 	if (mech_rctl_bytes + in_key_rctl_bytes + out_key_rctl_bytes != 0)
5217*4219Smcpowers 		CRYPTO_DECREMENT_RCTL(mech_rctl_bytes + in_key_rctl_bytes +
5218*4219Smcpowers 		    out_key_rctl_bytes);
5219*4219Smcpowers 
5220*4219Smcpowers 	if (k_in_attrs != NULL)
5221*4219Smcpowers 		kmem_free(k_in_attrs, k_in_attrs_size);
5222*4219Smcpowers 	if (k_out_attrs != NULL) {
5223*4219Smcpowers 		bzero(k_out_attrs, k_out_attrs_size);
5224*4219Smcpowers 		kmem_free(k_out_attrs, k_out_attrs_size);
5225*4219Smcpowers 	}
5226*4219Smcpowers 
5227*4219Smcpowers 	if (u_attrs != NULL)
5228*4219Smcpowers 		kmem_free(u_attrs, out_count * STRUCT_SIZE(oa));
5229*4219Smcpowers 
5230*4219Smcpowers 	if (error != 0)
5231*4219Smcpowers 		goto out;
5232*4219Smcpowers 
5233*4219Smcpowers 	STRUCT_FSET(generate_key, ngk_return_value, rv);
5234*4219Smcpowers 	if (copyout(STRUCT_BUF(generate_key), arg,
5235*4219Smcpowers 	    STRUCT_SIZE(generate_key)) != 0) {
5236*4219Smcpowers 		error = EFAULT;
5237*4219Smcpowers 	}
5238*4219Smcpowers out:
5239*4219Smcpowers 	if (sp != NULL)
5240*4219Smcpowers 		CRYPTO_SESSION_RELE(sp);
5241*4219Smcpowers 	crypto_release_minor(cm);
5242*4219Smcpowers 
5243*4219Smcpowers 	if (real_provider != NULL) {
5244*4219Smcpowers 		crypto_free_mech(real_provider,
5245*4219Smcpowers 		    allocated_by_crypto_module, &mech);
5246*4219Smcpowers 		KCF_PROV_REFRELE(real_provider);
5247*4219Smcpowers 	}
5248*4219Smcpowers 	return (error);
5249*4219Smcpowers }
5250*4219Smcpowers 
5251*4219Smcpowers /* ARGSUSED */
5252*4219Smcpowers static int
52530Sstevel@tonic-gate object_generate_key_pair(dev_t dev, caddr_t arg, int mode, int *rval)
52540Sstevel@tonic-gate {
52550Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_generate_key_pair, generate_key_pair);
5256904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
52570Sstevel@tonic-gate 	kcf_req_params_t params;
52580Sstevel@tonic-gate 	crypto_mechanism_t mech;
52590Sstevel@tonic-gate 	crypto_object_attribute_t *k_pub_attrs = NULL;
52600Sstevel@tonic-gate 	crypto_object_attribute_t *k_pri_attrs = NULL;
52610Sstevel@tonic-gate 	crypto_session_id_t session_id;
52620Sstevel@tonic-gate 	crypto_minor_t *cm;
52630Sstevel@tonic-gate 	crypto_session_data_t *sp = NULL;
52640Sstevel@tonic-gate 	crypto_object_id_t pub_handle;
52650Sstevel@tonic-gate 	crypto_object_id_t pri_handle;
52660Sstevel@tonic-gate 	caddr_t pri_attributes;
52670Sstevel@tonic-gate 	caddr_t pub_attributes;
52680Sstevel@tonic-gate 	size_t k_pub_attrs_size, k_pri_attrs_size;
52690Sstevel@tonic-gate 	size_t mech_rctl_bytes = 0;
52700Sstevel@tonic-gate 	size_t pub_rctl_bytes = 0;
52710Sstevel@tonic-gate 	size_t pri_rctl_bytes = 0;
52720Sstevel@tonic-gate 	size_t carry;
52730Sstevel@tonic-gate 	uint_t pub_count;
52740Sstevel@tonic-gate 	uint_t pri_count;
52750Sstevel@tonic-gate 	int error = 0;
52760Sstevel@tonic-gate 	int rv;
5277904Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
52780Sstevel@tonic-gate 
52790Sstevel@tonic-gate 	STRUCT_INIT(generate_key_pair, mode);
52800Sstevel@tonic-gate 
52810Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
52820Sstevel@tonic-gate 		cmn_err(CE_WARN,
52830Sstevel@tonic-gate 		    "object_generate_key_pair: failed holding minor");
52840Sstevel@tonic-gate 		return (ENXIO);
52850Sstevel@tonic-gate 	}
52860Sstevel@tonic-gate 
52870Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(generate_key_pair),
52880Sstevel@tonic-gate 	    STRUCT_SIZE(generate_key_pair)) != 0) {
52890Sstevel@tonic-gate 		crypto_release_minor(cm);
52900Sstevel@tonic-gate 		return (EFAULT);
52910Sstevel@tonic-gate 	}
52920Sstevel@tonic-gate 
52930Sstevel@tonic-gate 	session_id = STRUCT_FGET(generate_key_pair, kp_session);
52940Sstevel@tonic-gate 
52950Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
52960Sstevel@tonic-gate 		goto release_minor;
52970Sstevel@tonic-gate 	}
52980Sstevel@tonic-gate 
5299904Smcpowers 	bcopy(STRUCT_FADDR(generate_key_pair, kp_mechanism), &mech.cm_type,
5300904Smcpowers 	    sizeof (crypto_mech_type_t));
5301904Smcpowers 
5302904Smcpowers 	if ((rv = kcf_get_hardware_provider(mech.cm_type, CRYPTO_MECH_INVALID,
53031808Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider,
53041808Smcpowers 	    CRYPTO_FG_GENERATE_KEY_PAIR)) != CRYPTO_SUCCESS) {
53050Sstevel@tonic-gate 		goto release_minor;
53060Sstevel@tonic-gate 	}
53070Sstevel@tonic-gate 
5308904Smcpowers 	carry = 0;
5309904Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
5310904Smcpowers 	    STRUCT_FADDR(generate_key_pair, kp_mechanism), &mech, mode, &error);
5311904Smcpowers 
5312904Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
5313904Smcpowers 		allocated_by_crypto_module = B_TRUE;
5314904Smcpowers 		if (!copyin_mech(mode, STRUCT_FADDR(generate_key_pair,
5315904Smcpowers 		    kp_mechanism), &mech, &mech_rctl_bytes, &carry, &rv,
53163916Skrishna 		    &error)) {
5317904Smcpowers 			goto release_minor;
5318904Smcpowers 		}
5319904Smcpowers 	} else {
5320904Smcpowers 		if (rv != CRYPTO_SUCCESS)
5321904Smcpowers 			goto release_minor;
53220Sstevel@tonic-gate 	}
53230Sstevel@tonic-gate 
53240Sstevel@tonic-gate 	pub_count = STRUCT_FGET(generate_key_pair, kp_public_count);
53250Sstevel@tonic-gate 	pri_count = STRUCT_FGET(generate_key_pair, kp_private_count);
53260Sstevel@tonic-gate 
53270Sstevel@tonic-gate 	pub_attributes = STRUCT_FGETP(generate_key_pair, kp_public_attributes);
53280Sstevel@tonic-gate 	if (!copyin_attributes(mode, pub_count, pub_attributes, &k_pub_attrs,
53290Sstevel@tonic-gate 	    &k_pub_attrs_size, NULL, &rv, &error, &pub_rctl_bytes, carry,
53303916Skrishna 	    B_TRUE)) {
53310Sstevel@tonic-gate 		goto release_minor;
53320Sstevel@tonic-gate 	}
53330Sstevel@tonic-gate 
53340Sstevel@tonic-gate 	pri_attributes = STRUCT_FGETP(generate_key_pair, kp_private_attributes);
53350Sstevel@tonic-gate 	if (!copyin_attributes(mode, pri_count, pri_attributes, &k_pri_attrs,
53360Sstevel@tonic-gate 	    &k_pri_attrs_size, NULL, &rv, &error, &pri_rctl_bytes, 0,
53373916Skrishna 	    B_TRUE)) {
53380Sstevel@tonic-gate 		goto release_minor;
53390Sstevel@tonic-gate 	}
53400Sstevel@tonic-gate 
53410Sstevel@tonic-gate 	KCF_WRAP_KEY_OPS_PARAMS(&params, KCF_OP_KEY_GENERATE_PAIR,
53420Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, &mech, k_pub_attrs,
53430Sstevel@tonic-gate 	    pub_count, &pub_handle, k_pri_attrs, pri_count, &pri_handle,
53440Sstevel@tonic-gate 	    NULL, NULL, 0);
53450Sstevel@tonic-gate 
53460Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
53470Sstevel@tonic-gate 
53480Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
53490Sstevel@tonic-gate 		STRUCT_FSET(generate_key_pair, kp_public_handle, pub_handle);
53500Sstevel@tonic-gate 		STRUCT_FSET(generate_key_pair, kp_private_handle, pri_handle);
53510Sstevel@tonic-gate 	}
53520Sstevel@tonic-gate 
53530Sstevel@tonic-gate release_minor:
53543916Skrishna 	if (mech_rctl_bytes + pub_rctl_bytes + pri_rctl_bytes != 0)
53553916Skrishna 		CRYPTO_DECREMENT_RCTL(mech_rctl_bytes + pub_rctl_bytes +
53563916Skrishna 		    pri_rctl_bytes);
53570Sstevel@tonic-gate 
53580Sstevel@tonic-gate 	if (k_pub_attrs != NULL)
53590Sstevel@tonic-gate 		kmem_free(k_pub_attrs, k_pub_attrs_size);
53600Sstevel@tonic-gate 
53610Sstevel@tonic-gate 	if (k_pri_attrs != NULL)
53620Sstevel@tonic-gate 		kmem_free(k_pri_attrs, k_pri_attrs_size);
53630Sstevel@tonic-gate 
53640Sstevel@tonic-gate 	if (error != 0)
53650Sstevel@tonic-gate 		goto out;
53660Sstevel@tonic-gate 
53670Sstevel@tonic-gate 	STRUCT_FSET(generate_key_pair, kp_return_value, rv);
53680Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(generate_key_pair), arg,
53690Sstevel@tonic-gate 	    STRUCT_SIZE(generate_key_pair)) != 0) {
53700Sstevel@tonic-gate 		if (rv == CRYPTO_SUCCESS) {
53710Sstevel@tonic-gate 			KCF_WRAP_OBJECT_OPS_PARAMS(&params,
53720Sstevel@tonic-gate 			    KCF_OP_OBJECT_DESTROY,
53730Sstevel@tonic-gate 			    sp->sd_provider_session->ps_session, pub_handle,
53740Sstevel@tonic-gate 			    NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
53750Sstevel@tonic-gate 
53760Sstevel@tonic-gate 			(void) kcf_submit_request(real_provider, NULL,
53770Sstevel@tonic-gate 			    NULL, &params, B_FALSE);
53780Sstevel@tonic-gate 
53790Sstevel@tonic-gate 			KCF_WRAP_OBJECT_OPS_PARAMS(&params,
53800Sstevel@tonic-gate 			    KCF_OP_OBJECT_DESTROY,
53810Sstevel@tonic-gate 			    sp->sd_provider_session->ps_session, pri_handle,
53820Sstevel@tonic-gate 			    NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
53830Sstevel@tonic-gate 
53840Sstevel@tonic-gate 			(void) kcf_submit_request(real_provider, NULL,
53850Sstevel@tonic-gate 			    NULL, &params, B_FALSE);
53860Sstevel@tonic-gate 
53870Sstevel@tonic-gate 			error = EFAULT;
53880Sstevel@tonic-gate 		}
53890Sstevel@tonic-gate 	}
53900Sstevel@tonic-gate out:
53910Sstevel@tonic-gate 	if (sp != NULL)
53920Sstevel@tonic-gate 		CRYPTO_SESSION_RELE(sp);
53930Sstevel@tonic-gate 	crypto_release_minor(cm);
5394904Smcpowers 
5395904Smcpowers 	if (real_provider != NULL) {
5396904Smcpowers 		crypto_free_mech(real_provider,
5397904Smcpowers 		    allocated_by_crypto_module, &mech);
5398904Smcpowers 		KCF_PROV_REFRELE(real_provider);
5399904Smcpowers 	}
54000Sstevel@tonic-gate 	return (error);
54010Sstevel@tonic-gate }
54020Sstevel@tonic-gate 
54030Sstevel@tonic-gate /* ARGSUSED */
54040Sstevel@tonic-gate static int
5405*4219Smcpowers nostore_generate_key_pair(dev_t dev, caddr_t arg, int mode, int *rval)
5406*4219Smcpowers {
5407*4219Smcpowers 	STRUCT_DECL(crypto_nostore_generate_key_pair, generate_key_pair);
5408*4219Smcpowers 	/* LINTED E_FUNC_SET_NOT_USED */
5409*4219Smcpowers 	STRUCT_DECL(crypto_object_attribute, oa);
5410*4219Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
5411*4219Smcpowers 	kcf_req_params_t params;
5412*4219Smcpowers 	crypto_mechanism_t mech;
5413*4219Smcpowers 	crypto_object_attribute_t *k_in_pub_attrs = NULL;
5414*4219Smcpowers 	crypto_object_attribute_t *k_in_pri_attrs = NULL;
5415*4219Smcpowers 	crypto_object_attribute_t *k_out_pub_attrs = NULL;
5416*4219Smcpowers 	crypto_object_attribute_t *k_out_pri_attrs = NULL;
5417*4219Smcpowers 	crypto_session_id_t session_id;
5418*4219Smcpowers 	crypto_minor_t *cm;
5419*4219Smcpowers 	crypto_session_data_t *sp = NULL;
5420*4219Smcpowers 	caddr_t in_pri_attributes;
5421*4219Smcpowers 	caddr_t in_pub_attributes;
5422*4219Smcpowers 	caddr_t out_pri_attributes;
5423*4219Smcpowers 	caddr_t out_pub_attributes;
5424*4219Smcpowers 	size_t k_in_pub_attrs_size, k_in_pri_attrs_size;
5425*4219Smcpowers 	size_t k_out_pub_attrs_size, k_out_pri_attrs_size;
5426*4219Smcpowers 	size_t mech_rctl_bytes = 0;
5427*4219Smcpowers 	size_t in_pub_rctl_bytes = 0;
5428*4219Smcpowers 	size_t in_pri_rctl_bytes = 0;
5429*4219Smcpowers 	size_t out_pub_rctl_bytes = 0;
5430*4219Smcpowers 	size_t out_pri_rctl_bytes = 0;
5431*4219Smcpowers 	size_t carry;
5432*4219Smcpowers 	uint_t in_pub_count;
5433*4219Smcpowers 	uint_t in_pri_count;
5434*4219Smcpowers 	uint_t out_pub_count;
5435*4219Smcpowers 	uint_t out_pri_count;
5436*4219Smcpowers 	int error = 0;
5437*4219Smcpowers 	int rv;
5438*4219Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
5439*4219Smcpowers 	caddr_t u_pub_attrs = NULL;
5440*4219Smcpowers 	caddr_t u_pri_attrs = NULL;
5441*4219Smcpowers 
5442*4219Smcpowers 	STRUCT_INIT(generate_key_pair, mode);
5443*4219Smcpowers 	STRUCT_INIT(oa, mode);
5444*4219Smcpowers 
5445*4219Smcpowers 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
5446*4219Smcpowers 		cmn_err(CE_WARN,
5447*4219Smcpowers 		    "nostore_generate_key_pair: failed holding minor");
5448*4219Smcpowers 		return (ENXIO);
5449*4219Smcpowers 	}
5450*4219Smcpowers 
5451*4219Smcpowers 	if (copyin(arg, STRUCT_BUF(generate_key_pair),
5452*4219Smcpowers 	    STRUCT_SIZE(generate_key_pair)) != 0) {
5453*4219Smcpowers 		crypto_release_minor(cm);
5454*4219Smcpowers 		return (EFAULT);
5455*4219Smcpowers 	}
5456*4219Smcpowers 
5457*4219Smcpowers 	session_id = STRUCT_FGET(generate_key_pair, nkp_session);
5458*4219Smcpowers 
5459*4219Smcpowers 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
5460*4219Smcpowers 		goto release_minor;
5461*4219Smcpowers 	}
5462*4219Smcpowers 
5463*4219Smcpowers 	bcopy(STRUCT_FADDR(generate_key_pair, nkp_mechanism), &mech.cm_type,
5464*4219Smcpowers 	    sizeof (crypto_mech_type_t));
5465*4219Smcpowers 
5466*4219Smcpowers 	if ((rv = kcf_get_hardware_provider(mech.cm_type, CRYPTO_MECH_INVALID,
5467*4219Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider,
5468*4219Smcpowers 	    CRYPTO_FG_GENERATE_KEY_PAIR)) != CRYPTO_SUCCESS) {
5469*4219Smcpowers 		goto release_minor;
5470*4219Smcpowers 	}
5471*4219Smcpowers 
5472*4219Smcpowers 	carry = 0;
5473*4219Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
5474*4219Smcpowers 	    STRUCT_FADDR(generate_key_pair, nkp_mechanism), &mech, mode,
5475*4219Smcpowers 	    &error);
5476*4219Smcpowers 
5477*4219Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
5478*4219Smcpowers 		allocated_by_crypto_module = B_TRUE;
5479*4219Smcpowers 		if (!copyin_mech(mode, STRUCT_FADDR(generate_key_pair,
5480*4219Smcpowers 		    nkp_mechanism), &mech, &mech_rctl_bytes, &carry, &rv,
5481*4219Smcpowers 		    &error)) {
5482*4219Smcpowers 			goto release_minor;
5483*4219Smcpowers 		}
5484*4219Smcpowers 	} else {
5485*4219Smcpowers 		if (rv != CRYPTO_SUCCESS)
5486*4219Smcpowers 			goto release_minor;
5487*4219Smcpowers 	}
5488*4219Smcpowers 
5489*4219Smcpowers 	in_pub_count = STRUCT_FGET(generate_key_pair, nkp_in_public_count);
5490*4219Smcpowers 	in_pri_count = STRUCT_FGET(generate_key_pair, nkp_in_private_count);
5491*4219Smcpowers 
5492*4219Smcpowers 	in_pub_attributes = STRUCT_FGETP(generate_key_pair,
5493*4219Smcpowers 	    nkp_in_public_attributes);
5494*4219Smcpowers 	if (!copyin_attributes(mode, in_pub_count, in_pub_attributes,
5495*4219Smcpowers 	    &k_in_pub_attrs, &k_in_pub_attrs_size, NULL, &rv, &error,
5496*4219Smcpowers 	    &in_pub_rctl_bytes, carry, B_TRUE)) {
5497*4219Smcpowers 		goto release_minor;
5498*4219Smcpowers 	}
5499*4219Smcpowers 
5500*4219Smcpowers 	in_pri_attributes = STRUCT_FGETP(generate_key_pair,
5501*4219Smcpowers 	    nkp_in_private_attributes);
5502*4219Smcpowers 	if (!copyin_attributes(mode, in_pri_count, in_pri_attributes,
5503*4219Smcpowers 	    &k_in_pri_attrs, &k_in_pri_attrs_size, NULL, &rv, &error,
5504*4219Smcpowers 	    &in_pri_rctl_bytes, 0, B_TRUE)) {
5505*4219Smcpowers 		goto release_minor;
5506*4219Smcpowers 	}
5507*4219Smcpowers 
5508*4219Smcpowers 	out_pub_count = STRUCT_FGET(generate_key_pair, nkp_out_public_count);
5509*4219Smcpowers 	out_pri_count = STRUCT_FGET(generate_key_pair, nkp_out_private_count);
5510*4219Smcpowers 
5511*4219Smcpowers 	out_pub_attributes = STRUCT_FGETP(generate_key_pair,
5512*4219Smcpowers 	    nkp_out_public_attributes);
5513*4219Smcpowers 	if (!copyin_attributes(mode, out_pub_count, out_pub_attributes,
5514*4219Smcpowers 	    &k_out_pub_attrs, &k_out_pub_attrs_size, &u_pub_attrs, &rv, &error,
5515*4219Smcpowers 	    &out_pub_rctl_bytes, 0, B_FALSE)) {
5516*4219Smcpowers 		goto release_minor;
5517*4219Smcpowers 	}
5518*4219Smcpowers 
5519*4219Smcpowers 	out_pri_attributes = STRUCT_FGETP(generate_key_pair,
5520*4219Smcpowers 	    nkp_out_private_attributes);
5521*4219Smcpowers 	if (!copyin_attributes(mode, out_pri_count, out_pri_attributes,
5522*4219Smcpowers 	    &k_out_pri_attrs, &k_out_pri_attrs_size, &u_pri_attrs, &rv, &error,
5523*4219Smcpowers 	    &out_pri_rctl_bytes, 0, B_FALSE)) {
5524*4219Smcpowers 		goto release_minor;
5525*4219Smcpowers 	}
5526*4219Smcpowers 
5527*4219Smcpowers 	KCF_WRAP_NOSTORE_KEY_OPS_PARAMS(&params, KCF_OP_KEY_GENERATE_PAIR,
5528*4219Smcpowers 	    sp->sd_provider_session->ps_session, &mech, k_in_pub_attrs,
5529*4219Smcpowers 	    in_pub_count, k_in_pri_attrs, in_pri_count, NULL, k_out_pub_attrs,
5530*4219Smcpowers 	    out_pub_count, k_out_pri_attrs, out_pri_count);
5531*4219Smcpowers 
5532*4219Smcpowers 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
5533*4219Smcpowers 
5534*4219Smcpowers 	if (rv == CRYPTO_SUCCESS) {
5535*4219Smcpowers 		error = copyout_attributes(mode, out_pub_attributes,
5536*4219Smcpowers 		    out_pub_count, k_out_pub_attrs, u_pub_attrs);
5537*4219Smcpowers 		if (error != CRYPTO_SUCCESS)
5538*4219Smcpowers 			goto release_minor;
5539*4219Smcpowers 		error = copyout_attributes(mode, out_pri_attributes,
5540*4219Smcpowers 		    out_pri_count, k_out_pri_attrs, u_pri_attrs);
5541*4219Smcpowers 	}
5542*4219Smcpowers 
5543*4219Smcpowers release_minor:
5544*4219Smcpowers 	if (mech_rctl_bytes + in_pub_rctl_bytes + in_pri_rctl_bytes +
5545*4219Smcpowers 	    out_pub_rctl_bytes + out_pri_rctl_bytes != 0)
5546*4219Smcpowers 		CRYPTO_DECREMENT_RCTL(mech_rctl_bytes + in_pub_rctl_bytes +
5547*4219Smcpowers 		    in_pri_rctl_bytes + out_pub_rctl_bytes +
5548*4219Smcpowers 		    out_pri_rctl_bytes);
5549*4219Smcpowers 
5550*4219Smcpowers 	if (k_in_pub_attrs != NULL)
5551*4219Smcpowers 		kmem_free(k_in_pub_attrs, k_in_pub_attrs_size);
5552*4219Smcpowers 
5553*4219Smcpowers 	if (k_in_pri_attrs != NULL)
5554*4219Smcpowers 		kmem_free(k_in_pri_attrs, k_in_pri_attrs_size);
5555*4219Smcpowers 
5556*4219Smcpowers 	if (k_out_pub_attrs != NULL)
5557*4219Smcpowers 		kmem_free(k_out_pub_attrs, k_out_pub_attrs_size);
5558*4219Smcpowers 
5559*4219Smcpowers 	if (k_out_pri_attrs != NULL) {
5560*4219Smcpowers 		bzero(k_out_pri_attrs, k_out_pri_attrs_size);
5561*4219Smcpowers 		kmem_free(k_out_pri_attrs, k_out_pri_attrs_size);
5562*4219Smcpowers 	}
5563*4219Smcpowers 
5564*4219Smcpowers 	if (u_pub_attrs != NULL)
5565*4219Smcpowers 		kmem_free(u_pub_attrs, out_pub_count * STRUCT_SIZE(oa));
5566*4219Smcpowers 
5567*4219Smcpowers 	if (u_pri_attrs != NULL)
5568*4219Smcpowers 		kmem_free(u_pri_attrs, out_pri_count * STRUCT_SIZE(oa));
5569*4219Smcpowers 
5570*4219Smcpowers 	if (error != 0)
5571*4219Smcpowers 		goto out;
5572*4219Smcpowers 
5573*4219Smcpowers 	STRUCT_FSET(generate_key_pair, nkp_return_value, rv);
5574*4219Smcpowers 	if (copyout(STRUCT_BUF(generate_key_pair), arg,
5575*4219Smcpowers 	    STRUCT_SIZE(generate_key_pair)) != 0) {
5576*4219Smcpowers 		error = EFAULT;
5577*4219Smcpowers 	}
5578*4219Smcpowers out:
5579*4219Smcpowers 	if (sp != NULL)
5580*4219Smcpowers 		CRYPTO_SESSION_RELE(sp);
5581*4219Smcpowers 	crypto_release_minor(cm);
5582*4219Smcpowers 
5583*4219Smcpowers 	if (real_provider != NULL) {
5584*4219Smcpowers 		crypto_free_mech(real_provider,
5585*4219Smcpowers 		    allocated_by_crypto_module, &mech);
5586*4219Smcpowers 		KCF_PROV_REFRELE(real_provider);
5587*4219Smcpowers 	}
5588*4219Smcpowers 	return (error);
5589*4219Smcpowers }
5590*4219Smcpowers 
5591*4219Smcpowers /* ARGSUSED */
5592*4219Smcpowers static int
55930Sstevel@tonic-gate object_wrap_key(dev_t dev, caddr_t arg, int mode, int *rval)
55940Sstevel@tonic-gate {
55950Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_wrap_key, wrap_key);
5596904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
55970Sstevel@tonic-gate 	kcf_req_params_t params;
55980Sstevel@tonic-gate 	crypto_mechanism_t mech;
55990Sstevel@tonic-gate 	crypto_key_t key;
56000Sstevel@tonic-gate 	crypto_session_id_t session_id;
56010Sstevel@tonic-gate 	crypto_minor_t *cm;
56020Sstevel@tonic-gate 	crypto_session_data_t *sp;
56030Sstevel@tonic-gate 	crypto_object_id_t handle;
56040Sstevel@tonic-gate 	size_t mech_rctl_bytes = 0, key_rctl_bytes = 0;
56050Sstevel@tonic-gate 	size_t wrapped_key_rctl_bytes = 0;
56060Sstevel@tonic-gate 	size_t carry;
56070Sstevel@tonic-gate 	size_t wrapped_key_len, new_wrapped_key_len;
56080Sstevel@tonic-gate 	uchar_t *wrapped_key = NULL;
56090Sstevel@tonic-gate 	char *wrapped_key_buffer;
56100Sstevel@tonic-gate 	int error = 0;
56110Sstevel@tonic-gate 	int rv;
5612904Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
56130Sstevel@tonic-gate 
56140Sstevel@tonic-gate 	STRUCT_INIT(wrap_key, mode);
56150Sstevel@tonic-gate 
56160Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
56170Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_wrap_key: failed holding minor");
56180Sstevel@tonic-gate 		return (ENXIO);
56190Sstevel@tonic-gate 	}
56200Sstevel@tonic-gate 
56210Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(wrap_key), STRUCT_SIZE(wrap_key)) != 0) {
56220Sstevel@tonic-gate 		crypto_release_minor(cm);
56230Sstevel@tonic-gate 		return (EFAULT);
56240Sstevel@tonic-gate 	}
56250Sstevel@tonic-gate 
56260Sstevel@tonic-gate 	bzero(&key, sizeof (crypto_key_t));
56270Sstevel@tonic-gate 
56280Sstevel@tonic-gate 	session_id = STRUCT_FGET(wrap_key, wk_session);
56290Sstevel@tonic-gate 
56300Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
56310Sstevel@tonic-gate 		goto release_minor;
56320Sstevel@tonic-gate 	}
56330Sstevel@tonic-gate 
5634904Smcpowers 	bcopy(STRUCT_FADDR(wrap_key, wk_mechanism), &mech.cm_type,
5635904Smcpowers 	    sizeof (crypto_mech_type_t));
5636904Smcpowers 
5637904Smcpowers 	if ((rv = kcf_get_hardware_provider(mech.cm_type, CRYPTO_MECH_INVALID,
56381808Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider,
56391808Smcpowers 	    CRYPTO_FG_WRAP)) != CRYPTO_SUCCESS) {
56400Sstevel@tonic-gate 		goto out;
56410Sstevel@tonic-gate 	}
56420Sstevel@tonic-gate 
5643904Smcpowers 	carry = 0;
5644904Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
5645904Smcpowers 	    STRUCT_FADDR(wrap_key, wk_mechanism), &mech, mode, &error);
5646904Smcpowers 
5647904Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
5648904Smcpowers 		allocated_by_crypto_module = B_TRUE;
5649904Smcpowers 		if (!copyin_mech(mode, STRUCT_FADDR(wrap_key, wk_mechanism),
56503916Skrishna 		    &mech, &mech_rctl_bytes, &carry, &rv, &error)) {
5651904Smcpowers 			goto out;
5652904Smcpowers 		}
5653904Smcpowers 	} else {
5654904Smcpowers 		if (rv != CRYPTO_SUCCESS)
5655904Smcpowers 			goto out;
56560Sstevel@tonic-gate 	}
56570Sstevel@tonic-gate 
56580Sstevel@tonic-gate 	if (!copyin_key(mode, STRUCT_FADDR(wrap_key, wk_wrapping_key), &key,
56593916Skrishna 	    &key_rctl_bytes, &rv, &error, carry)) {
56600Sstevel@tonic-gate 		goto out;
56610Sstevel@tonic-gate 	}
56620Sstevel@tonic-gate 
56630Sstevel@tonic-gate 	wrapped_key_len = STRUCT_FGET(wrap_key, wk_wrapped_key_len);
56640Sstevel@tonic-gate 
56650Sstevel@tonic-gate 	/*
56660Sstevel@tonic-gate 	 * Don't allocate output buffer unless both buffer pointer and
56670Sstevel@tonic-gate 	 * buffer length are not NULL or 0 (length).
56680Sstevel@tonic-gate 	 */
56690Sstevel@tonic-gate 	wrapped_key_buffer = STRUCT_FGETP(wrap_key, wk_wrapped_key);
56700Sstevel@tonic-gate 	if (wrapped_key_buffer == NULL || wrapped_key_len == 0) {
56710Sstevel@tonic-gate 		wrapped_key_len = 0;
56720Sstevel@tonic-gate 	}
56730Sstevel@tonic-gate 
56740Sstevel@tonic-gate 	if (wrapped_key_len > crypto_max_buffer_len) {
56750Sstevel@tonic-gate 		cmn_err(CE_NOTE, "object_wrap_key: buffer greater than %ld "
56760Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
56770Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
56780Sstevel@tonic-gate 		goto out;
56790Sstevel@tonic-gate 	}
56800Sstevel@tonic-gate 
56813916Skrishna 	if ((rv = crypto_buffer_check(wrapped_key_len)) != CRYPTO_SUCCESS) {
56820Sstevel@tonic-gate 		goto out;
56830Sstevel@tonic-gate 	}
56840Sstevel@tonic-gate 
56850Sstevel@tonic-gate 	/* new_wrapped_key_len can be modified by the provider */
56860Sstevel@tonic-gate 	wrapped_key_rctl_bytes = new_wrapped_key_len = wrapped_key_len;
56870Sstevel@tonic-gate 	wrapped_key = kmem_alloc(wrapped_key_len, KM_SLEEP);
56880Sstevel@tonic-gate 
56890Sstevel@tonic-gate 	handle = STRUCT_FGET(wrap_key, wk_object_handle);
56900Sstevel@tonic-gate 	KCF_WRAP_KEY_OPS_PARAMS(&params, KCF_OP_KEY_WRAP,
56910Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, &mech, NULL, 0, &handle,
56920Sstevel@tonic-gate 	    NULL, 0, NULL, &key, wrapped_key, &new_wrapped_key_len);
56930Sstevel@tonic-gate 
56940Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
56950Sstevel@tonic-gate 
56960Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
56970Sstevel@tonic-gate 		if (wrapped_key_len != 0 && copyout(wrapped_key,
56980Sstevel@tonic-gate 		    wrapped_key_buffer, new_wrapped_key_len) != 0) {
56990Sstevel@tonic-gate 			error = EFAULT;
57000Sstevel@tonic-gate 		}
57010Sstevel@tonic-gate 		STRUCT_FSET(wrap_key, wk_wrapped_key_len, new_wrapped_key_len);
57020Sstevel@tonic-gate 	}
57030Sstevel@tonic-gate 
57040Sstevel@tonic-gate 	if (rv == CRYPTO_BUFFER_TOO_SMALL) {
57050Sstevel@tonic-gate 		/*
57060Sstevel@tonic-gate 		 * The providers return CRYPTO_BUFFER_TOO_SMALL even for case 1
57070Sstevel@tonic-gate 		 * of section 11.2 of the pkcs11 spec. We catch it here and
57080Sstevel@tonic-gate 		 * provide the correct pkcs11 return value.
57090Sstevel@tonic-gate 		 */
57100Sstevel@tonic-gate 		if (STRUCT_FGETP(wrap_key, wk_wrapped_key) == NULL)
57110Sstevel@tonic-gate 			rv = CRYPTO_SUCCESS;
57120Sstevel@tonic-gate 		STRUCT_FSET(wrap_key, wk_wrapped_key_len, new_wrapped_key_len);
57130Sstevel@tonic-gate 	}
57140Sstevel@tonic-gate out:
57150Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
57160Sstevel@tonic-gate 
57170Sstevel@tonic-gate release_minor:
57183916Skrishna 	if (mech_rctl_bytes + key_rctl_bytes + wrapped_key_rctl_bytes != 0)
57193916Skrishna 		CRYPTO_DECREMENT_RCTL(mech_rctl_bytes + key_rctl_bytes +
57203916Skrishna 		    wrapped_key_rctl_bytes);
57210Sstevel@tonic-gate 	crypto_release_minor(cm);
57220Sstevel@tonic-gate 
5723904Smcpowers 	if (real_provider != NULL) {
5724904Smcpowers 		crypto_free_mech(real_provider,
5725904Smcpowers 		    allocated_by_crypto_module, &mech);
5726904Smcpowers 		KCF_PROV_REFRELE(real_provider);
5727904Smcpowers 	}
5728904Smcpowers 
57290Sstevel@tonic-gate 	if (wrapped_key != NULL)
57300Sstevel@tonic-gate 		kmem_free(wrapped_key, wrapped_key_len);
57310Sstevel@tonic-gate 
57320Sstevel@tonic-gate 	free_crypto_key(&key);
57330Sstevel@tonic-gate 
57340Sstevel@tonic-gate 	if (error != 0)
57350Sstevel@tonic-gate 		return (error);
57360Sstevel@tonic-gate 
57370Sstevel@tonic-gate 	STRUCT_FSET(wrap_key, wk_return_value, rv);
57380Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(wrap_key), arg, STRUCT_SIZE(wrap_key)) != 0) {
57390Sstevel@tonic-gate 		return (EFAULT);
57400Sstevel@tonic-gate 	}
57410Sstevel@tonic-gate 	return (0);
57420Sstevel@tonic-gate }
57430Sstevel@tonic-gate 
57440Sstevel@tonic-gate /* ARGSUSED */
57450Sstevel@tonic-gate static int
57460Sstevel@tonic-gate object_unwrap_key(dev_t dev, caddr_t arg, int mode, int *rval)
57470Sstevel@tonic-gate {
57480Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_unwrap_key, unwrap_key);
5749904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
57500Sstevel@tonic-gate 	kcf_req_params_t params;
57510Sstevel@tonic-gate 	crypto_mechanism_t mech;
57520Sstevel@tonic-gate 	crypto_key_t unwrapping_key;
57530Sstevel@tonic-gate 	crypto_session_id_t session_id;
57540Sstevel@tonic-gate 	crypto_minor_t *cm;
57550Sstevel@tonic-gate 	crypto_session_data_t *sp = NULL;
57560Sstevel@tonic-gate 	crypto_object_id_t handle;
57570Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
57580Sstevel@tonic-gate 	size_t k_attrs_size;
57590Sstevel@tonic-gate 	size_t mech_rctl_bytes = 0, unwrapping_key_rctl_bytes = 0;
57600Sstevel@tonic-gate 	size_t wrapped_key_rctl_bytes = 0, k_attrs_rctl_bytes = 0;
57610Sstevel@tonic-gate 	size_t carry;
57620Sstevel@tonic-gate 	size_t wrapped_key_len;
57630Sstevel@tonic-gate 	uchar_t *wrapped_key = NULL;
57640Sstevel@tonic-gate 	int error = 0;
57650Sstevel@tonic-gate 	int rv;
57660Sstevel@tonic-gate 	uint_t count;
57670Sstevel@tonic-gate 	caddr_t uk_attributes;
5768904Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
57690Sstevel@tonic-gate 
57700Sstevel@tonic-gate 	STRUCT_INIT(unwrap_key, mode);
57710Sstevel@tonic-gate 
57720Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
57730Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_unwrap_key: failed holding minor");
57740Sstevel@tonic-gate 		return (ENXIO);
57750Sstevel@tonic-gate 	}
57760Sstevel@tonic-gate 
57770Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(unwrap_key), STRUCT_SIZE(unwrap_key)) != 0) {
57780Sstevel@tonic-gate 		crypto_release_minor(cm);
57790Sstevel@tonic-gate 		return (EFAULT);
57800Sstevel@tonic-gate 	}
57810Sstevel@tonic-gate 
57820Sstevel@tonic-gate 	bzero(&unwrapping_key, sizeof (unwrapping_key));
57830Sstevel@tonic-gate 
57840Sstevel@tonic-gate 	session_id = STRUCT_FGET(unwrap_key, uk_session);
57850Sstevel@tonic-gate 
57860Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
57870Sstevel@tonic-gate 		goto release_minor;
57880Sstevel@tonic-gate 	}
57890Sstevel@tonic-gate 
5790904Smcpowers 	bcopy(STRUCT_FADDR(unwrap_key, uk_mechanism), &mech.cm_type,
5791904Smcpowers 	    sizeof (crypto_mech_type_t));
5792904Smcpowers 
5793904Smcpowers 	if ((rv = kcf_get_hardware_provider(mech.cm_type, CRYPTO_MECH_INVALID,
57941808Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider,
57951808Smcpowers 	    CRYPTO_FG_UNWRAP)) != CRYPTO_SUCCESS) {
57960Sstevel@tonic-gate 		goto release_minor;
57970Sstevel@tonic-gate 	}
57980Sstevel@tonic-gate 
5799904Smcpowers 	carry = 0;
5800904Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
5801904Smcpowers 	    STRUCT_FADDR(unwrap_key, uk_mechanism), &mech, mode, &error);
5802904Smcpowers 
5803904Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
5804904Smcpowers 		allocated_by_crypto_module = B_TRUE;
5805904Smcpowers 		if (!copyin_mech(mode, STRUCT_FADDR(unwrap_key, uk_mechanism),
58063916Skrishna 		    &mech, &mech_rctl_bytes, &carry, &rv, &error)) {
5807904Smcpowers 			goto release_minor;
5808904Smcpowers 		}
5809904Smcpowers 	} else {
5810904Smcpowers 		if (rv != CRYPTO_SUCCESS)
5811904Smcpowers 			goto release_minor;
58120Sstevel@tonic-gate 	}
58130Sstevel@tonic-gate 
58140Sstevel@tonic-gate 	if (!copyin_key(mode, STRUCT_FADDR(unwrap_key, uk_unwrapping_key),
58153916Skrishna 	    &unwrapping_key, &unwrapping_key_rctl_bytes, &rv, &error, carry)) {
58160Sstevel@tonic-gate 		goto release_minor;
58170Sstevel@tonic-gate 	}
58180Sstevel@tonic-gate 
58190Sstevel@tonic-gate 	count = STRUCT_FGET(unwrap_key, uk_count);
58200Sstevel@tonic-gate 	uk_attributes = STRUCT_FGETP(unwrap_key, uk_attributes);
58210Sstevel@tonic-gate 	if (!copyin_attributes(mode, count, uk_attributes, &k_attrs,
58223916Skrishna 	    &k_attrs_size, NULL, &rv, &error, &k_attrs_rctl_bytes, 0, B_TRUE)) {
58230Sstevel@tonic-gate 		goto release_minor;
58240Sstevel@tonic-gate 	}
58250Sstevel@tonic-gate 
58260Sstevel@tonic-gate 	wrapped_key_len = STRUCT_FGET(unwrap_key, uk_wrapped_key_len);
58270Sstevel@tonic-gate 	if (wrapped_key_len > crypto_max_buffer_len) {
58280Sstevel@tonic-gate 		cmn_err(CE_NOTE, "object_unwrap_key: buffer greater than %ld "
58290Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
58300Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
58310Sstevel@tonic-gate 		goto release_minor;
58320Sstevel@tonic-gate 	}
58330Sstevel@tonic-gate 
58343916Skrishna 	if ((rv = crypto_buffer_check(wrapped_key_len))
58350Sstevel@tonic-gate 	    != CRYPTO_SUCCESS) {
58360Sstevel@tonic-gate 		goto release_minor;
58370Sstevel@tonic-gate 	}
58380Sstevel@tonic-gate 	wrapped_key_rctl_bytes = wrapped_key_len;
58390Sstevel@tonic-gate 	wrapped_key = kmem_alloc(wrapped_key_len, KM_SLEEP);
58400Sstevel@tonic-gate 
58410Sstevel@tonic-gate 	if (wrapped_key_len != 0 && copyin(STRUCT_FGETP(unwrap_key,
58420Sstevel@tonic-gate 	    uk_wrapped_key), wrapped_key, wrapped_key_len) != 0) {
58430Sstevel@tonic-gate 		error = EFAULT;
58440Sstevel@tonic-gate 		goto release_minor;
58450Sstevel@tonic-gate 	}
58460Sstevel@tonic-gate 
58470Sstevel@tonic-gate 	/* wrapped_key_len is not modified by the unwrap operation */
58480Sstevel@tonic-gate 	KCF_WRAP_KEY_OPS_PARAMS(&params, KCF_OP_KEY_UNWRAP,
58490Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, &mech, k_attrs, count, &handle,
58500Sstevel@tonic-gate 	    NULL, 0, NULL, &unwrapping_key, wrapped_key, &wrapped_key_len);
58510Sstevel@tonic-gate 
58520Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
58530Sstevel@tonic-gate 
58540Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS)
58550Sstevel@tonic-gate 		STRUCT_FSET(unwrap_key, uk_object_handle, handle);
58560Sstevel@tonic-gate 
58570Sstevel@tonic-gate release_minor:
58583916Skrishna 	if (mech_rctl_bytes + unwrapping_key_rctl_bytes +
58593916Skrishna 	    wrapped_key_rctl_bytes + k_attrs_rctl_bytes != 0)
58603916Skrishna 		CRYPTO_DECREMENT_RCTL(mech_rctl_bytes +
58613916Skrishna 		    unwrapping_key_rctl_bytes +
58623916Skrishna 		    wrapped_key_rctl_bytes + k_attrs_rctl_bytes);
58630Sstevel@tonic-gate 
58640Sstevel@tonic-gate 	if (k_attrs != NULL)
58650Sstevel@tonic-gate 		kmem_free(k_attrs, k_attrs_size);
58660Sstevel@tonic-gate 
58670Sstevel@tonic-gate 	if (wrapped_key != NULL)
58680Sstevel@tonic-gate 		kmem_free(wrapped_key, wrapped_key_len);
58690Sstevel@tonic-gate 
58700Sstevel@tonic-gate 	free_crypto_key(&unwrapping_key);
58710Sstevel@tonic-gate 
58720Sstevel@tonic-gate 	if (error != 0)
58730Sstevel@tonic-gate 		goto out;
58740Sstevel@tonic-gate 
58750Sstevel@tonic-gate 	STRUCT_FSET(unwrap_key, uk_return_value, rv);
58760Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(unwrap_key), arg,
58770Sstevel@tonic-gate 	    STRUCT_SIZE(unwrap_key)) != 0) {
58780Sstevel@tonic-gate 		if (rv == CRYPTO_SUCCESS) {
58790Sstevel@tonic-gate 			KCF_WRAP_OBJECT_OPS_PARAMS(&params,
58800Sstevel@tonic-gate 			    KCF_OP_OBJECT_DESTROY,
58810Sstevel@tonic-gate 			    sp->sd_provider_session->ps_session, handle,
58820Sstevel@tonic-gate 			    NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
58830Sstevel@tonic-gate 
58840Sstevel@tonic-gate 			(void) kcf_submit_request(real_provider, NULL,
58850Sstevel@tonic-gate 			    NULL, &params, B_FALSE);
58860Sstevel@tonic-gate 
58870Sstevel@tonic-gate 			error = EFAULT;
58880Sstevel@tonic-gate 		}
58890Sstevel@tonic-gate 	}
58900Sstevel@tonic-gate out:
58910Sstevel@tonic-gate 	if (sp != NULL)
58920Sstevel@tonic-gate 		CRYPTO_SESSION_RELE(sp);
58930Sstevel@tonic-gate 	crypto_release_minor(cm);
5894904Smcpowers 
5895904Smcpowers 	if (real_provider != NULL) {
5896904Smcpowers 		crypto_free_mech(real_provider,
5897904Smcpowers 		    allocated_by_crypto_module, &mech);
5898904Smcpowers 		KCF_PROV_REFRELE(real_provider);
5899904Smcpowers 	}
5900904Smcpowers 
59010Sstevel@tonic-gate 	return (error);
59020Sstevel@tonic-gate }
59030Sstevel@tonic-gate 
59040Sstevel@tonic-gate /* ARGSUSED */
59050Sstevel@tonic-gate static int
59060Sstevel@tonic-gate object_derive_key(dev_t dev, caddr_t arg, int mode, int *rval)
59070Sstevel@tonic-gate {
59080Sstevel@tonic-gate 	STRUCT_DECL(crypto_derive_key, derive_key);
5909904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
59100Sstevel@tonic-gate 	kcf_req_params_t params;
59110Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
59120Sstevel@tonic-gate 	crypto_mechanism_t mech;
59130Sstevel@tonic-gate 	crypto_key_t base_key;
59140Sstevel@tonic-gate 	crypto_session_id_t session_id;
59150Sstevel@tonic-gate 	crypto_minor_t *cm;
59160Sstevel@tonic-gate 	crypto_session_data_t *sp = NULL;
59170Sstevel@tonic-gate 	crypto_object_id_t handle;
59180Sstevel@tonic-gate 	size_t k_attrs_size;
59190Sstevel@tonic-gate 	size_t key_rctl_bytes = 0, mech_rctl_bytes = 0;
59200Sstevel@tonic-gate 	size_t attributes_rctl_bytes = 0;
59210Sstevel@tonic-gate 	size_t carry;
59220Sstevel@tonic-gate 	caddr_t attributes;
59230Sstevel@tonic-gate 	uint_t count;
59240Sstevel@tonic-gate 	int error = 0;
59250Sstevel@tonic-gate 	int rv;
5926904Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
5927904Smcpowers 	boolean_t please_destroy_object = B_FALSE;
59280Sstevel@tonic-gate 
59290Sstevel@tonic-gate 	STRUCT_INIT(derive_key, mode);
59300Sstevel@tonic-gate 
59310Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
59320Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_derive_key: failed holding minor");
59330Sstevel@tonic-gate 		return (ENXIO);
59340Sstevel@tonic-gate 	}
59350Sstevel@tonic-gate 
59360Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(derive_key), STRUCT_SIZE(derive_key)) != 0) {
59370Sstevel@tonic-gate 		crypto_release_minor(cm);
59380Sstevel@tonic-gate 		return (EFAULT);
59390Sstevel@tonic-gate 	}
59400Sstevel@tonic-gate 
59410Sstevel@tonic-gate 	bzero(&base_key, sizeof (base_key));
59420Sstevel@tonic-gate 
59430Sstevel@tonic-gate 	session_id = STRUCT_FGET(derive_key, dk_session);
59440Sstevel@tonic-gate 
59450Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
59460Sstevel@tonic-gate 		goto release_minor;
59470Sstevel@tonic-gate 	}
59480Sstevel@tonic-gate 
5949904Smcpowers 	bcopy(STRUCT_FADDR(derive_key, dk_mechanism), &mech.cm_type,
5950904Smcpowers 	    sizeof (crypto_mech_type_t));
5951904Smcpowers 
5952904Smcpowers 	if ((rv = kcf_get_hardware_provider(mech.cm_type, CRYPTO_MECH_INVALID,
59531808Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider,
59541808Smcpowers 	    CRYPTO_FG_DERIVE)) != CRYPTO_SUCCESS) {
59550Sstevel@tonic-gate 		goto release_minor;
59560Sstevel@tonic-gate 	}
59570Sstevel@tonic-gate 
5958904Smcpowers 	carry = 0;
5959904Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
5960904Smcpowers 	    STRUCT_FADDR(derive_key, dk_mechanism), &mech, mode, &error);
5961904Smcpowers 
5962904Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
5963904Smcpowers 		allocated_by_crypto_module = B_TRUE;
5964904Smcpowers 		if (!copyin_mech(mode, STRUCT_FADDR(derive_key, dk_mechanism),
59653916Skrishna 		    &mech, &mech_rctl_bytes, &carry, &rv, &error)) {
5966904Smcpowers 			goto release_minor;
5967904Smcpowers 		}
5968904Smcpowers 	} else {
5969904Smcpowers 		if (rv != CRYPTO_SUCCESS)
5970904Smcpowers 			goto release_minor;
59710Sstevel@tonic-gate 	}
59720Sstevel@tonic-gate 
59730Sstevel@tonic-gate 	if (!copyin_key(mode, STRUCT_FADDR(derive_key, dk_base_key),
59743916Skrishna 	    &base_key, &key_rctl_bytes, &rv, &error, carry)) {
59750Sstevel@tonic-gate 		goto release_minor;
59760Sstevel@tonic-gate 	}
59770Sstevel@tonic-gate 
59780Sstevel@tonic-gate 	count = STRUCT_FGET(derive_key, dk_count);
59790Sstevel@tonic-gate 
59800Sstevel@tonic-gate 	attributes = STRUCT_FGETP(derive_key, dk_attributes);
59810Sstevel@tonic-gate 	if (!copyin_attributes(mode, count, attributes, &k_attrs,
59823916Skrishna 	    &k_attrs_size, NULL, &rv, &error,
59833916Skrishna 	    &attributes_rctl_bytes, 0, B_TRUE)) {
59840Sstevel@tonic-gate 		goto release_minor;
59850Sstevel@tonic-gate 	}
59860Sstevel@tonic-gate 
59870Sstevel@tonic-gate 	KCF_WRAP_KEY_OPS_PARAMS(&params, KCF_OP_KEY_DERIVE,
59880Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, &mech, k_attrs, count,
59890Sstevel@tonic-gate 	    &handle, NULL, 0, NULL, &base_key, NULL, NULL);
59900Sstevel@tonic-gate 
59910Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
59920Sstevel@tonic-gate 
5993904Smcpowers 	if (rv == CRYPTO_SUCCESS) {
59940Sstevel@tonic-gate 		STRUCT_FSET(derive_key, dk_object_handle, handle);
59950Sstevel@tonic-gate 
5996904Smcpowers 		rv = crypto_provider_copyout_mech_param(real_provider,
5997904Smcpowers 		    &mech, STRUCT_FADDR(derive_key, dk_mechanism),
5998904Smcpowers 		    mode, &error);
5999904Smcpowers 
6000904Smcpowers 		if (rv == CRYPTO_NOT_SUPPORTED) {
6001904Smcpowers 			rv = CRYPTO_SUCCESS;
6002904Smcpowers 			goto release_minor;
6003904Smcpowers 		}
6004904Smcpowers 
6005904Smcpowers 		if (rv != CRYPTO_SUCCESS)
6006904Smcpowers 			please_destroy_object = B_TRUE;
6007904Smcpowers 	}
6008904Smcpowers 
60090Sstevel@tonic-gate release_minor:
60103916Skrishna 	if (mech_rctl_bytes + key_rctl_bytes + attributes_rctl_bytes != 0)
60113916Skrishna 		CRYPTO_DECREMENT_RCTL(mech_rctl_bytes + key_rctl_bytes +
60123916Skrishna 		    attributes_rctl_bytes);
60130Sstevel@tonic-gate 
60140Sstevel@tonic-gate 	if (k_attrs != NULL)
60150Sstevel@tonic-gate 		kmem_free(k_attrs, k_attrs_size);
60160Sstevel@tonic-gate 
60170Sstevel@tonic-gate 	free_crypto_key(&base_key);
60180Sstevel@tonic-gate 
60190Sstevel@tonic-gate 	if (error != 0)
60200Sstevel@tonic-gate 		goto out;
60210Sstevel@tonic-gate 
60220Sstevel@tonic-gate 	STRUCT_FSET(derive_key, dk_return_value, rv);
60230Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(derive_key), arg,
60240Sstevel@tonic-gate 	    STRUCT_SIZE(derive_key)) != 0) {
60250Sstevel@tonic-gate 		if (rv == CRYPTO_SUCCESS) {
6026904Smcpowers 			please_destroy_object = B_TRUE;
60270Sstevel@tonic-gate 			error = EFAULT;
60280Sstevel@tonic-gate 		}
60290Sstevel@tonic-gate 	}
60300Sstevel@tonic-gate out:
6031904Smcpowers 	if (please_destroy_object) {
6032904Smcpowers 		KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_DESTROY,
6033904Smcpowers 		    sp->sd_provider_session->ps_session, handle,
6034904Smcpowers 		    NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
6035904Smcpowers 
6036904Smcpowers 		(void) kcf_submit_request(real_provider, NULL,
6037904Smcpowers 		    NULL, &params, B_FALSE);
6038904Smcpowers 	}
6039904Smcpowers 
60400Sstevel@tonic-gate 	if (sp != NULL)
60410Sstevel@tonic-gate 		CRYPTO_SESSION_RELE(sp);
60420Sstevel@tonic-gate 	crypto_release_minor(cm);
6043904Smcpowers 
6044904Smcpowers 	if (real_provider != NULL) {
6045904Smcpowers 		crypto_free_mech(real_provider,
6046904Smcpowers 		    allocated_by_crypto_module, &mech);
6047904Smcpowers 		KCF_PROV_REFRELE(real_provider);
6048904Smcpowers 	}
60490Sstevel@tonic-gate 	return (error);
60500Sstevel@tonic-gate }
60510Sstevel@tonic-gate 
60520Sstevel@tonic-gate /* ARGSUSED */
60530Sstevel@tonic-gate static int
6054*4219Smcpowers nostore_derive_key(dev_t dev, caddr_t arg, int mode, int *rval)
6055*4219Smcpowers {
6056*4219Smcpowers 	STRUCT_DECL(crypto_nostore_derive_key, derive_key);
6057*4219Smcpowers 	/* LINTED E_FUNC_SET_NOT_USED */
6058*4219Smcpowers 	STRUCT_DECL(crypto_object_attribute, oa);
6059*4219Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
6060*4219Smcpowers 	kcf_req_params_t params;
6061*4219Smcpowers 	crypto_object_attribute_t *k_in_attrs = NULL;
6062*4219Smcpowers 	crypto_object_attribute_t *k_out_attrs = NULL;
6063*4219Smcpowers 	crypto_mechanism_t mech;
6064*4219Smcpowers 	crypto_key_t base_key;
6065*4219Smcpowers 	crypto_session_id_t session_id;
6066*4219Smcpowers 	crypto_minor_t *cm;
6067*4219Smcpowers 	crypto_session_data_t *sp = NULL;
6068*4219Smcpowers 	size_t k_in_attrs_size, k_out_attrs_size;
6069*4219Smcpowers 	size_t key_rctl_bytes = 0, mech_rctl_bytes = 0;
6070*4219Smcpowers 	size_t in_attributes_rctl_bytes = 0;
6071*4219Smcpowers 	size_t out_attributes_rctl_bytes = 0;
6072*4219Smcpowers 	size_t carry;
6073*4219Smcpowers 	caddr_t in_attributes, out_attributes;
6074*4219Smcpowers 	uint_t in_count, out_count;
6075*4219Smcpowers 	int error = 0;
6076*4219Smcpowers 	int rv;
6077*4219Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
6078*4219Smcpowers 	caddr_t u_attrs = NULL;
6079*4219Smcpowers 
6080*4219Smcpowers 	STRUCT_INIT(derive_key, mode);
6081*4219Smcpowers 	STRUCT_INIT(oa, mode);
6082*4219Smcpowers 
6083*4219Smcpowers 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
6084*4219Smcpowers 		cmn_err(CE_WARN, "nostore_derive_key: failed holding minor");
6085*4219Smcpowers 		return (ENXIO);
6086*4219Smcpowers 	}
6087*4219Smcpowers 
6088*4219Smcpowers 	if (copyin(arg, STRUCT_BUF(derive_key), STRUCT_SIZE(derive_key)) != 0) {
6089*4219Smcpowers 		crypto_release_minor(cm);
6090*4219Smcpowers 		return (EFAULT);
6091*4219Smcpowers 	}
6092*4219Smcpowers 
6093*4219Smcpowers 	bzero(&base_key, sizeof (base_key));
6094*4219Smcpowers 
6095*4219Smcpowers 	session_id = STRUCT_FGET(derive_key, ndk_session);
6096*4219Smcpowers 
6097*4219Smcpowers 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
6098*4219Smcpowers 		goto release_minor;
6099*4219Smcpowers 	}
6100*4219Smcpowers 
6101*4219Smcpowers 	bcopy(STRUCT_FADDR(derive_key, ndk_mechanism), &mech.cm_type,
6102*4219Smcpowers 	    sizeof (crypto_mech_type_t));
6103*4219Smcpowers 
6104*4219Smcpowers 	if ((rv = kcf_get_hardware_provider(mech.cm_type, CRYPTO_MECH_INVALID,
6105*4219Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider,
6106*4219Smcpowers 	    CRYPTO_FG_DERIVE)) != CRYPTO_SUCCESS) {
6107*4219Smcpowers 		goto release_minor;
6108*4219Smcpowers 	}
6109*4219Smcpowers 
6110*4219Smcpowers 	carry = 0;
6111*4219Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
6112*4219Smcpowers 	    STRUCT_FADDR(derive_key, ndk_mechanism), &mech, mode, &error);
6113*4219Smcpowers 
6114*4219Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
6115*4219Smcpowers 		allocated_by_crypto_module = B_TRUE;
6116*4219Smcpowers 		if (!copyin_mech(mode, STRUCT_FADDR(derive_key, ndk_mechanism),
6117*4219Smcpowers 		    &mech, &mech_rctl_bytes, &carry, &rv, &error)) {
6118*4219Smcpowers 			goto release_minor;
6119*4219Smcpowers 		}
6120*4219Smcpowers 	} else {
6121*4219Smcpowers 		if (rv != CRYPTO_SUCCESS)
6122*4219Smcpowers 			goto release_minor;
6123*4219Smcpowers 	}
6124*4219Smcpowers 
6125*4219Smcpowers 	if (!copyin_key(mode, STRUCT_FADDR(derive_key, ndk_base_key),
6126*4219Smcpowers 	    &base_key, &key_rctl_bytes, &rv, &error, carry)) {
6127*4219Smcpowers 		goto release_minor;
6128*4219Smcpowers 	}
6129*4219Smcpowers 
6130*4219Smcpowers 	in_count = STRUCT_FGET(derive_key, ndk_in_count);
6131*4219Smcpowers 	out_count = STRUCT_FGET(derive_key, ndk_out_count);
6132*4219Smcpowers 
6133*4219Smcpowers 	in_attributes = STRUCT_FGETP(derive_key, ndk_in_attributes);
6134*4219Smcpowers 	if (!copyin_attributes(mode, in_count, in_attributes, &k_in_attrs,
6135*4219Smcpowers 	    &k_in_attrs_size, NULL, &rv, &error, &in_attributes_rctl_bytes,
6136*4219Smcpowers 	    0, B_TRUE)) {
6137*4219Smcpowers 		goto release_minor;
6138*4219Smcpowers 	}
6139*4219Smcpowers 
6140*4219Smcpowers 	out_attributes = STRUCT_FGETP(derive_key, ndk_out_attributes);
6141*4219Smcpowers 	if (!copyin_attributes(mode, out_count, out_attributes, &k_out_attrs,
6142*4219Smcpowers 	    &k_out_attrs_size, &u_attrs, &rv, &error,
6143*4219Smcpowers 	    &out_attributes_rctl_bytes, 0, B_FALSE)) {
6144*4219Smcpowers 		goto release_minor;
6145*4219Smcpowers 	}
6146*4219Smcpowers 
6147*4219Smcpowers 	KCF_WRAP_NOSTORE_KEY_OPS_PARAMS(&params, KCF_OP_KEY_DERIVE,
6148*4219Smcpowers 	    sp->sd_provider_session->ps_session, &mech, k_in_attrs, in_count,
6149*4219Smcpowers 	    NULL, 0, &base_key, k_out_attrs, out_count, NULL, 0);
6150*4219Smcpowers 
6151*4219Smcpowers 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
6152*4219Smcpowers 
6153*4219Smcpowers 	if (rv == CRYPTO_SUCCESS) {
6154*4219Smcpowers 		rv = crypto_provider_copyout_mech_param(real_provider,
6155*4219Smcpowers 		    &mech, STRUCT_FADDR(derive_key, ndk_mechanism),
6156*4219Smcpowers 		    mode, &error);
6157*4219Smcpowers 
6158*4219Smcpowers 		if (rv == CRYPTO_NOT_SUPPORTED) {
6159*4219Smcpowers 			rv = CRYPTO_SUCCESS;
6160*4219Smcpowers 		}
6161*4219Smcpowers 		/* copyout the derived secret */
6162*4219Smcpowers 		if (copyout_attributes(mode, out_attributes, out_count,
6163*4219Smcpowers 		    k_out_attrs, u_attrs) != 0)
6164*4219Smcpowers 			error = EFAULT;
6165*4219Smcpowers 	}
6166*4219Smcpowers 
6167*4219Smcpowers release_minor:
6168*4219Smcpowers 	if (mech_rctl_bytes + key_rctl_bytes + in_attributes_rctl_bytes +
6169*4219Smcpowers 	    out_attributes_rctl_bytes != 0)
6170*4219Smcpowers 		CRYPTO_DECREMENT_RCTL(mech_rctl_bytes + key_rctl_bytes +
6171*4219Smcpowers 		    in_attributes_rctl_bytes + out_attributes_rctl_bytes);
6172*4219Smcpowers 
6173*4219Smcpowers 	if (k_in_attrs != NULL)
6174*4219Smcpowers 		kmem_free(k_in_attrs, k_in_attrs_size);
6175*4219Smcpowers 	if (k_out_attrs != NULL) {
6176*4219Smcpowers 		bzero(k_out_attrs, k_out_attrs_size);
6177*4219Smcpowers 		kmem_free(k_out_attrs, k_out_attrs_size);
6178*4219Smcpowers 	}
6179*4219Smcpowers 
6180*4219Smcpowers 	if (u_attrs != NULL)
6181*4219Smcpowers 		kmem_free(u_attrs, out_count * STRUCT_SIZE(oa));
6182*4219Smcpowers 
6183*4219Smcpowers 	free_crypto_key(&base_key);
6184*4219Smcpowers 
6185*4219Smcpowers 	if (error != 0)
6186*4219Smcpowers 		goto out;
6187*4219Smcpowers 
6188*4219Smcpowers 	STRUCT_FSET(derive_key, ndk_return_value, rv);
6189*4219Smcpowers 	if (copyout(STRUCT_BUF(derive_key), arg,
6190*4219Smcpowers 	    STRUCT_SIZE(derive_key)) != 0) {
6191*4219Smcpowers 		error = EFAULT;
6192*4219Smcpowers 	}
6193*4219Smcpowers out:
6194*4219Smcpowers 	if (sp != NULL)
6195*4219Smcpowers 		CRYPTO_SESSION_RELE(sp);
6196*4219Smcpowers 	crypto_release_minor(cm);
6197*4219Smcpowers 
6198*4219Smcpowers 	if (real_provider != NULL) {
6199*4219Smcpowers 		crypto_free_mech(real_provider,
6200*4219Smcpowers 		    allocated_by_crypto_module, &mech);
6201*4219Smcpowers 		KCF_PROV_REFRELE(real_provider);
6202*4219Smcpowers 	}
6203*4219Smcpowers 	return (error);
6204*4219Smcpowers }
6205*4219Smcpowers 
6206*4219Smcpowers /* ARGSUSED */
6207*4219Smcpowers static int
62080Sstevel@tonic-gate crypto_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *c,
62090Sstevel@tonic-gate     int *rval)
62100Sstevel@tonic-gate {
62110Sstevel@tonic-gate #define	ARG	((caddr_t)arg)
62120Sstevel@tonic-gate 
62130Sstevel@tonic-gate 	switch (cmd) {
62140Sstevel@tonic-gate 	case CRYPTO_GET_FUNCTION_LIST:
62150Sstevel@tonic-gate 		return (get_function_list(dev, ARG, mode, rval));
62160Sstevel@tonic-gate 
62170Sstevel@tonic-gate 	case CRYPTO_GET_MECHANISM_NUMBER:
62180Sstevel@tonic-gate 		return (get_mechanism_number(dev, ARG, mode, rval));
62190Sstevel@tonic-gate 
62200Sstevel@tonic-gate 	case CRYPTO_GET_PROVIDER_LIST:
62210Sstevel@tonic-gate 		return (get_provider_list(dev, ARG, mode, rval));
62220Sstevel@tonic-gate 
62230Sstevel@tonic-gate 	case CRYPTO_GET_PROVIDER_INFO:
62240Sstevel@tonic-gate 		return (get_provider_info(dev, ARG, mode, rval));
62250Sstevel@tonic-gate 
62260Sstevel@tonic-gate 	case CRYPTO_GET_PROVIDER_MECHANISMS:
62270Sstevel@tonic-gate 		return (get_provider_mechanisms(dev, ARG, mode, rval));
62280Sstevel@tonic-gate 
62290Sstevel@tonic-gate 	case CRYPTO_GET_PROVIDER_MECHANISM_INFO:
62300Sstevel@tonic-gate 		return (get_provider_mechanism_info(dev, ARG, mode, rval));
62310Sstevel@tonic-gate 
62320Sstevel@tonic-gate 	case CRYPTO_OPEN_SESSION:
62330Sstevel@tonic-gate 		return (open_session(dev, ARG, mode, rval));
62340Sstevel@tonic-gate 
62350Sstevel@tonic-gate 	case CRYPTO_CLOSE_SESSION:
62360Sstevel@tonic-gate 		return (close_session(dev, ARG, mode, rval));
62370Sstevel@tonic-gate 
62380Sstevel@tonic-gate 	case CRYPTO_ENCRYPT_INIT:
62390Sstevel@tonic-gate 		return (encrypt_init(dev, ARG, mode, rval));
62400Sstevel@tonic-gate 
62410Sstevel@tonic-gate 	case CRYPTO_DECRYPT_INIT:
62420Sstevel@tonic-gate 		return (decrypt_init(dev, ARG, mode, rval));
62430Sstevel@tonic-gate 
62440Sstevel@tonic-gate 	case CRYPTO_ENCRYPT:
62450Sstevel@tonic-gate 		return (encrypt(dev, ARG, mode, rval));
62460Sstevel@tonic-gate 
62470Sstevel@tonic-gate 	case CRYPTO_DECRYPT:
62480Sstevel@tonic-gate 		return (decrypt(dev, ARG, mode, rval));
62490Sstevel@tonic-gate 
62500Sstevel@tonic-gate 	case CRYPTO_ENCRYPT_UPDATE:
62510Sstevel@tonic-gate 		return (encrypt_update(dev, ARG, mode, rval));
62520Sstevel@tonic-gate 
62530Sstevel@tonic-gate 	case CRYPTO_DECRYPT_UPDATE:
62540Sstevel@tonic-gate 		return (decrypt_update(dev, ARG, mode, rval));
62550Sstevel@tonic-gate 
62560Sstevel@tonic-gate 	case CRYPTO_ENCRYPT_FINAL:
62570Sstevel@tonic-gate 		return (encrypt_final(dev, ARG, mode, rval));
62580Sstevel@tonic-gate 
62590Sstevel@tonic-gate 	case CRYPTO_DECRYPT_FINAL:
62600Sstevel@tonic-gate 		return (decrypt_final(dev, ARG, mode, rval));
62610Sstevel@tonic-gate 
62620Sstevel@tonic-gate 	case CRYPTO_DIGEST_INIT:
62630Sstevel@tonic-gate 		return (digest_init(dev, ARG, mode, rval));
62640Sstevel@tonic-gate 
62650Sstevel@tonic-gate 	case CRYPTO_DIGEST:
62660Sstevel@tonic-gate 		return (digest(dev, ARG, mode, rval));
62670Sstevel@tonic-gate 
62680Sstevel@tonic-gate 	case CRYPTO_DIGEST_UPDATE:
62690Sstevel@tonic-gate 		return (digest_update(dev, ARG, mode, rval));
62700Sstevel@tonic-gate 
62710Sstevel@tonic-gate 	case CRYPTO_DIGEST_KEY:
62720Sstevel@tonic-gate 		return (digest_key(dev, ARG, mode, rval));
62730Sstevel@tonic-gate 
62740Sstevel@tonic-gate 	case CRYPTO_DIGEST_FINAL:
62750Sstevel@tonic-gate 		return (digest_final(dev, ARG, mode, rval));
62760Sstevel@tonic-gate 
62770Sstevel@tonic-gate 	case CRYPTO_SIGN_INIT:
62780Sstevel@tonic-gate 		return (sign_init(dev, ARG, mode, rval));
62790Sstevel@tonic-gate 
62800Sstevel@tonic-gate 	case CRYPTO_SIGN:
62810Sstevel@tonic-gate 		return (sign(dev, ARG, mode, rval));
62820Sstevel@tonic-gate 
62830Sstevel@tonic-gate 	case CRYPTO_SIGN_UPDATE:
62840Sstevel@tonic-gate 		return (sign_update(dev, ARG, mode, rval));
62850Sstevel@tonic-gate 
62860Sstevel@tonic-gate 	case CRYPTO_SIGN_FINAL:
62870Sstevel@tonic-gate 		return (sign_final(dev, ARG, mode, rval));
62880Sstevel@tonic-gate 
62890Sstevel@tonic-gate 	case CRYPTO_SIGN_RECOVER_INIT:
62900Sstevel@tonic-gate 		return (sign_recover_init(dev, ARG, mode, rval));
62910Sstevel@tonic-gate 
62920Sstevel@tonic-gate 	case CRYPTO_SIGN_RECOVER:
62930Sstevel@tonic-gate 		return (sign_recover(dev, ARG, mode, rval));
62940Sstevel@tonic-gate 
62950Sstevel@tonic-gate 	case CRYPTO_VERIFY_INIT:
62960Sstevel@tonic-gate 		return (verify_init(dev, ARG, mode, rval));
62970Sstevel@tonic-gate 
62980Sstevel@tonic-gate 	case CRYPTO_VERIFY:
62990Sstevel@tonic-gate 		return (verify(dev, ARG, mode, rval));
63000Sstevel@tonic-gate 
63010Sstevel@tonic-gate 	case CRYPTO_VERIFY_UPDATE:
63020Sstevel@tonic-gate 		return (verify_update(dev, ARG, mode, rval));
63030Sstevel@tonic-gate 
63040Sstevel@tonic-gate 	case CRYPTO_VERIFY_FINAL:
63050Sstevel@tonic-gate 		return (verify_final(dev, ARG, mode, rval));
63060Sstevel@tonic-gate 
63070Sstevel@tonic-gate 	case CRYPTO_VERIFY_RECOVER_INIT:
63080Sstevel@tonic-gate 		return (verify_recover_init(dev, ARG, mode, rval));
63090Sstevel@tonic-gate 
63100Sstevel@tonic-gate 	case CRYPTO_VERIFY_RECOVER:
63110Sstevel@tonic-gate 		return (verify_recover(dev, ARG, mode, rval));
63120Sstevel@tonic-gate 
63130Sstevel@tonic-gate 	case CRYPTO_SET_PIN:
63140Sstevel@tonic-gate 		return (set_pin(dev, ARG, mode, rval));
63150Sstevel@tonic-gate 
63160Sstevel@tonic-gate 	case CRYPTO_LOGIN:
63170Sstevel@tonic-gate 		return (login(dev, ARG, mode, rval));
63180Sstevel@tonic-gate 
63190Sstevel@tonic-gate 	case CRYPTO_LOGOUT:
63200Sstevel@tonic-gate 		return (logout(dev, ARG, mode, rval));
63210Sstevel@tonic-gate 
63220Sstevel@tonic-gate 	case CRYPTO_SEED_RANDOM:
63230Sstevel@tonic-gate 		return (seed_random(dev, ARG, mode, rval));
63240Sstevel@tonic-gate 
63250Sstevel@tonic-gate 	case CRYPTO_GENERATE_RANDOM:
63260Sstevel@tonic-gate 		return (generate_random(dev, ARG, mode, rval));
63270Sstevel@tonic-gate 
63280Sstevel@tonic-gate 	case CRYPTO_OBJECT_CREATE:
63290Sstevel@tonic-gate 		return (object_create(dev, ARG, mode, rval));
63300Sstevel@tonic-gate 
63310Sstevel@tonic-gate 	case CRYPTO_OBJECT_COPY:
63320Sstevel@tonic-gate 		return (object_copy(dev, ARG, mode, rval));
63330Sstevel@tonic-gate 
63340Sstevel@tonic-gate 	case CRYPTO_OBJECT_DESTROY:
63350Sstevel@tonic-gate 		return (object_destroy(dev, ARG, mode, rval));
63360Sstevel@tonic-gate 
63370Sstevel@tonic-gate 	case CRYPTO_OBJECT_GET_ATTRIBUTE_VALUE:
63380Sstevel@tonic-gate 		return (object_get_attribute_value(dev, ARG, mode, rval));
63390Sstevel@tonic-gate 
63400Sstevel@tonic-gate 	case CRYPTO_OBJECT_GET_SIZE:
63410Sstevel@tonic-gate 		return (object_get_size(dev, ARG, mode, rval));
63420Sstevel@tonic-gate 
63430Sstevel@tonic-gate 	case CRYPTO_OBJECT_SET_ATTRIBUTE_VALUE:
63440Sstevel@tonic-gate 		return (object_set_attribute_value(dev, ARG, mode, rval));
63450Sstevel@tonic-gate 
63460Sstevel@tonic-gate 	case CRYPTO_OBJECT_FIND_INIT:
63470Sstevel@tonic-gate 		return (object_find_init(dev, ARG, mode, rval));
63480Sstevel@tonic-gate 
63490Sstevel@tonic-gate 	case CRYPTO_OBJECT_FIND_UPDATE:
63500Sstevel@tonic-gate 		return (object_find_update(dev, ARG, mode, rval));
63510Sstevel@tonic-gate 
63520Sstevel@tonic-gate 	case CRYPTO_OBJECT_FIND_FINAL:
63530Sstevel@tonic-gate 		return (object_find_final(dev, ARG, mode, rval));
63540Sstevel@tonic-gate 
63550Sstevel@tonic-gate 	case CRYPTO_GENERATE_KEY:
63560Sstevel@tonic-gate 		return (object_generate_key(dev, ARG, mode, rval));
63570Sstevel@tonic-gate 
63580Sstevel@tonic-gate 	case CRYPTO_GENERATE_KEY_PAIR:
63590Sstevel@tonic-gate 		return (object_generate_key_pair(dev, ARG, mode, rval));
63600Sstevel@tonic-gate 
63610Sstevel@tonic-gate 	case CRYPTO_WRAP_KEY:
63620Sstevel@tonic-gate 		return (object_wrap_key(dev, ARG, mode, rval));
63630Sstevel@tonic-gate 
63640Sstevel@tonic-gate 	case CRYPTO_UNWRAP_KEY:
63650Sstevel@tonic-gate 		return (object_unwrap_key(dev, ARG, mode, rval));
63660Sstevel@tonic-gate 
63670Sstevel@tonic-gate 	case CRYPTO_DERIVE_KEY:
63680Sstevel@tonic-gate 		return (object_derive_key(dev, ARG, mode, rval));
6369*4219Smcpowers 
6370*4219Smcpowers 	case CRYPTO_NOSTORE_GENERATE_KEY:
6371*4219Smcpowers 		return (nostore_generate_key(dev, ARG, mode, rval));
6372*4219Smcpowers 
6373*4219Smcpowers 	case CRYPTO_NOSTORE_GENERATE_KEY_PAIR:
6374*4219Smcpowers 		return (nostore_generate_key_pair(dev, ARG, mode, rval));
6375*4219Smcpowers 
6376*4219Smcpowers 	case CRYPTO_NOSTORE_DERIVE_KEY:
6377*4219Smcpowers 		return (nostore_derive_key(dev, ARG, mode, rval));
63780Sstevel@tonic-gate 	}
63790Sstevel@tonic-gate 	return (EINVAL);
63800Sstevel@tonic-gate }
63810Sstevel@tonic-gate 
63820Sstevel@tonic-gate /*
63830Sstevel@tonic-gate  * Check for the project.max-crypto-memory resource control.
63840Sstevel@tonic-gate  */
63850Sstevel@tonic-gate static int
63863916Skrishna crypto_buffer_check(size_t need)
63870Sstevel@tonic-gate {
63883620Skrishna 	kproject_t *kpj;
63890Sstevel@tonic-gate 
63900Sstevel@tonic-gate 	if (need == 0)
63910Sstevel@tonic-gate 		return (CRYPTO_SUCCESS);
63920Sstevel@tonic-gate 
63930Sstevel@tonic-gate 	mutex_enter(&curproc->p_lock);
63943620Skrishna 	kpj = curproc->p_task->tk_proj;
63953916Skrishna 	mutex_enter(&(kpj->kpj_data.kpd_crypto_lock));
63963620Skrishna 
63973620Skrishna 	if (kpj->kpj_data.kpd_crypto_mem + need >
63983620Skrishna 	    kpj->kpj_data.kpd_crypto_mem_ctl) {
63993620Skrishna 		if (rctl_test(rc_project_crypto_mem,
64003620Skrishna 		    kpj->kpj_rctls, curproc, need, 0) & RCT_DENY) {
64013916Skrishna 			mutex_exit(&(kpj->kpj_data.kpd_crypto_lock));
64023620Skrishna 			mutex_exit(&curproc->p_lock);
64033620Skrishna 			return (CRYPTO_HOST_MEMORY);
64043620Skrishna 		}
64053620Skrishna 	}
64063620Skrishna 
64073620Skrishna 	kpj->kpj_data.kpd_crypto_mem += need;
64083916Skrishna 	mutex_exit(&(kpj->kpj_data.kpd_crypto_lock));
64093916Skrishna 
64103916Skrishna 	curproc->p_crypto_mem += need;
64110Sstevel@tonic-gate 	mutex_exit(&curproc->p_lock);
64123620Skrishna 
64130Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
64140Sstevel@tonic-gate }
6415