xref: /onnv-gate/usr/src/uts/common/crypto/io/crypto.c (revision 6867:93cbc6d3a932)
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 /*
226424Skrishna  * Copyright 2008 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 
554841Shaimay extern int kcf_des3_threshold;
564841Shaimay extern int kcf_aes_threshold;
574841Shaimay extern int kcf_rc4_threshold;
584841Shaimay extern int kcf_md5_threshold;
594841Shaimay extern int kcf_sha1_threshold;
604841Shaimay 
610Sstevel@tonic-gate /*
620Sstevel@tonic-gate  * Locking notes:
630Sstevel@tonic-gate  *
640Sstevel@tonic-gate  * crypto_lock protects the global array of minor structures.  It
650Sstevel@tonic-gate  * also protects the cm_refcnt member of each of these structures.
660Sstevel@tonic-gate  * The crypto_cv is used to signal decrements in the cm_refcnt,
670Sstevel@tonic-gate  * and is used with the global crypto_lock.
680Sstevel@tonic-gate  *
690Sstevel@tonic-gate  * Other fields in the minor structure are protected by the
700Sstevel@tonic-gate  * cm_lock member of the minor structure.
710Sstevel@tonic-gate  */
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate  * DDI entry points.
750Sstevel@tonic-gate  */
760Sstevel@tonic-gate static int crypto_attach(dev_info_t *, ddi_attach_cmd_t);
770Sstevel@tonic-gate static int crypto_detach(dev_info_t *, ddi_detach_cmd_t);
780Sstevel@tonic-gate static int crypto_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
790Sstevel@tonic-gate static int crypto_open(dev_t *, int, int, cred_t *);
800Sstevel@tonic-gate static int crypto_close(dev_t, int, int, cred_t *);
810Sstevel@tonic-gate static int crypto_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
820Sstevel@tonic-gate 
83904Smcpowers static int cipher_init(dev_t, caddr_t, int, int (*)(crypto_provider_t,
840Sstevel@tonic-gate     crypto_session_id_t, crypto_mechanism_t *, crypto_key_t *,
850Sstevel@tonic-gate     crypto_ctx_template_t, crypto_context_t *, crypto_call_req_t *));
860Sstevel@tonic-gate 
870Sstevel@tonic-gate static int common_digest(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 cipher(dev_t, caddr_t, int, int (*)(crypto_context_t,
910Sstevel@tonic-gate     crypto_data_t *, crypto_data_t *, crypto_call_req_t *));
920Sstevel@tonic-gate 
930Sstevel@tonic-gate static int cipher_update(dev_t, caddr_t, int, int (*)(crypto_context_t,
940Sstevel@tonic-gate     crypto_data_t *, crypto_data_t *, crypto_call_req_t *));
950Sstevel@tonic-gate 
960Sstevel@tonic-gate static int common_final(dev_t, caddr_t, int, int (*)(crypto_context_t,
970Sstevel@tonic-gate     crypto_data_t *, crypto_call_req_t *));
980Sstevel@tonic-gate 
99904Smcpowers static int sign_verify_init(dev_t, caddr_t, int, int (*)(crypto_provider_t,
1000Sstevel@tonic-gate     crypto_session_id_t, crypto_mechanism_t *, crypto_key_t *,
1010Sstevel@tonic-gate     crypto_ctx_template_t, crypto_context_t *, crypto_call_req_t *));
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate static int sign_verify_update(dev_t dev, caddr_t arg, int mode,
1040Sstevel@tonic-gate     int (*)(crypto_context_t, crypto_data_t *, crypto_call_req_t *));
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate static void crypto_initialize_rctl(void);
1070Sstevel@tonic-gate static void crypto_release_provider_session(crypto_minor_t *,
1080Sstevel@tonic-gate     crypto_provider_session_t *);
1093916Skrishna static int crypto_buffer_check(size_t);
1100Sstevel@tonic-gate static int crypto_free_find_ctx(crypto_session_data_t *);
1110Sstevel@tonic-gate static int crypto_get_provider_list(crypto_minor_t *, uint_t *,
1120Sstevel@tonic-gate     crypto_provider_entry_t **, boolean_t);
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate /* number of minor numbers to allocate at a time */
1150Sstevel@tonic-gate #define	CRYPTO_MINOR_CHUNK	16
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate /*
1180Sstevel@tonic-gate  * There are two limits associated with kernel memory. The first,
1190Sstevel@tonic-gate  * CRYPTO_MAX_BUFFER_LEN, is the maximum number of bytes that can be
1200Sstevel@tonic-gate  * allocated for a single copyin/copyout buffer. The second limit is
1210Sstevel@tonic-gate  * the total number of bytes that can be allocated by a process
1220Sstevel@tonic-gate  * for copyin/copyout buffers. The latter is enforced by the
1230Sstevel@tonic-gate  * project.max-crypto-memory resource control.
1240Sstevel@tonic-gate  */
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate #define	CRYPTO_MAX_BUFFER_LEN	(2 * 1024 * 1024)
1270Sstevel@tonic-gate #define	CRYPTO_MAX_FIND_COUNT	512
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate /*
1306424Skrishna  * We preapprove some bytes for each session to avoid making the costly
1316424Skrishna  * crypto_buffer_check() calls. The preapproval is done when a new session
1326424Skrishna  * is created and that cost is amortized over later crypto calls.
1336424Skrishna  * Most applications create a session and then do a bunch of crypto calls
1346424Skrishna  * in that session. So, they benefit from this optimization.
1356424Skrishna  *
1366424Skrishna  * Note that we may hit the project.max-crypto-memory limit a bit sooner
1376424Skrishna  * because of this preapproval. But it is acceptable since the preapproved
1386424Skrishna  * amount is insignificant compared to the default max-crypto-memory limit
1396424Skrishna  * which is quarter of the machine's memory. The preapproved amount is
1406424Skrishna  * roughly 2 * 16K(maximum SSL record size).
1410Sstevel@tonic-gate  */
1426424Skrishna #define	CRYPTO_PRE_APPROVED_LIMIT	(32 * 1024)
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate /* The session table grows by CRYPTO_SESSION_CHUNK increments */
1450Sstevel@tonic-gate #define	CRYPTO_SESSION_CHUNK	100
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate size_t crypto_max_buffer_len = CRYPTO_MAX_BUFFER_LEN;
1486424Skrishna size_t crypto_pre_approved_limit = CRYPTO_PRE_APPROVED_LIMIT;
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate #define	INIT_RAW_CRYPTO_DATA(data, len)				\
1510Sstevel@tonic-gate 	(data).cd_format = CRYPTO_DATA_RAW;			\
1520Sstevel@tonic-gate 	(data).cd_raw.iov_base = kmem_alloc(len, KM_SLEEP);	\
1530Sstevel@tonic-gate 	(data).cd_raw.iov_len = len;				\
1540Sstevel@tonic-gate 	(data).cd_offset = 0;					\
1550Sstevel@tonic-gate 	(data).cd_length = len;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate static struct kmem_cache *crypto_session_cache;
1580Sstevel@tonic-gate static crypto_minor_t **crypto_minors = NULL;
1590Sstevel@tonic-gate static dev_info_t *crypto_dip = NULL;
1600Sstevel@tonic-gate static minor_t crypto_minor_chunk = CRYPTO_MINOR_CHUNK;
1610Sstevel@tonic-gate static minor_t crypto_minors_table_count = 0;
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate /*
1640Sstevel@tonic-gate  * Minors are started from 1 because vmem_alloc()
1650Sstevel@tonic-gate  * returns 0 in case of failure.
1660Sstevel@tonic-gate  */
1670Sstevel@tonic-gate static vmem_t *crypto_arena = NULL;	/* Arena for device minors */
1680Sstevel@tonic-gate static minor_t crypto_minors_count = 0;
1690Sstevel@tonic-gate static kmutex_t crypto_lock;
1700Sstevel@tonic-gate static kcondvar_t crypto_cv;
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate #define	RETURN_LIST			B_TRUE
1730Sstevel@tonic-gate #define	DONT_RETURN_LIST		B_FALSE
1740Sstevel@tonic-gate 
175904Smcpowers #define	CRYPTO_OPS_OFFSET(f)		offsetof(crypto_ops_t, co_##f)
1760Sstevel@tonic-gate #define	CRYPTO_RANDOM_OFFSET(f)		offsetof(crypto_random_number_ops_t, f)
1770Sstevel@tonic-gate #define	CRYPTO_SESSION_OFFSET(f)	offsetof(crypto_session_ops_t, f)
1780Sstevel@tonic-gate #define	CRYPTO_OBJECT_OFFSET(f)		offsetof(crypto_object_ops_t, f)
1790Sstevel@tonic-gate #define	CRYPTO_PROVIDER_OFFSET(f)	\
1800Sstevel@tonic-gate 	offsetof(crypto_provider_management_ops_t, f)
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate #define	CRYPTO_CANCEL_CTX(spp) {	\
1830Sstevel@tonic-gate 	crypto_cancel_ctx(*(spp));	\
1840Sstevel@tonic-gate 	*(spp) = NULL;			\
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate #define	CRYPTO_CANCEL_ALL_CTX(sp) {				\
1880Sstevel@tonic-gate 	if ((sp)->sd_digest_ctx != NULL) {			\
1890Sstevel@tonic-gate 		crypto_cancel_ctx((sp)->sd_digest_ctx);		\
1900Sstevel@tonic-gate 		(sp)->sd_digest_ctx = NULL;			\
1910Sstevel@tonic-gate 	}							\
1920Sstevel@tonic-gate 	if ((sp)->sd_encr_ctx != NULL) {			\
1930Sstevel@tonic-gate 		crypto_cancel_ctx((sp)->sd_encr_ctx);		\
1940Sstevel@tonic-gate 		(sp)->sd_encr_ctx = NULL;			\
1950Sstevel@tonic-gate 	}							\
1960Sstevel@tonic-gate 	if ((sp)->sd_decr_ctx != NULL) {			\
1970Sstevel@tonic-gate 		crypto_cancel_ctx((sp)->sd_decr_ctx);		\
1980Sstevel@tonic-gate 		(sp)->sd_decr_ctx = NULL;			\
1990Sstevel@tonic-gate 	}							\
2000Sstevel@tonic-gate 	if ((sp)->sd_sign_ctx != NULL) {			\
2010Sstevel@tonic-gate 		crypto_cancel_ctx((sp)->sd_sign_ctx);		\
2020Sstevel@tonic-gate 		(sp)->sd_sign_ctx = NULL;			\
2030Sstevel@tonic-gate 	}							\
2040Sstevel@tonic-gate 	if ((sp)->sd_verify_ctx != NULL) {			\
2050Sstevel@tonic-gate 		crypto_cancel_ctx((sp)->sd_verify_ctx);		\
2060Sstevel@tonic-gate 		(sp)->sd_verify_ctx = NULL;			\
2070Sstevel@tonic-gate 	}							\
2080Sstevel@tonic-gate 	if ((sp)->sd_sign_recover_ctx != NULL) {		\
2090Sstevel@tonic-gate 		crypto_cancel_ctx((sp)->sd_sign_recover_ctx);	\
2100Sstevel@tonic-gate 		(sp)->sd_sign_recover_ctx = NULL;		\
2110Sstevel@tonic-gate 	}							\
2120Sstevel@tonic-gate 	if ((sp)->sd_verify_recover_ctx != NULL) {		\
2130Sstevel@tonic-gate 		crypto_cancel_ctx((sp)->sd_verify_recover_ctx);	\
2140Sstevel@tonic-gate 		(sp)->sd_verify_recover_ctx = NULL;		\
2150Sstevel@tonic-gate 	}							\
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate 
2186424Skrishna #define	CRYPTO_DECREMENT_RCTL(val)	if ((val) != 0) {	\
2193916Skrishna 	kproject_t *projp;					\
2203916Skrishna 	mutex_enter(&curproc->p_lock);				\
2213916Skrishna 	projp = curproc->p_task->tk_proj;			\
2223916Skrishna 	ASSERT(projp != NULL);					\
2233916Skrishna 	mutex_enter(&(projp->kpj_data.kpd_crypto_lock));	\
2243916Skrishna 	projp->kpj_data.kpd_crypto_mem -= (val);		\
2253916Skrishna 	mutex_exit(&(projp->kpj_data.kpd_crypto_lock));		\
2263916Skrishna 	curproc->p_crypto_mem -= (val);				\
2273916Skrishna 	mutex_exit(&curproc->p_lock);				\
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate /*
2316424Skrishna  * We do not need to hold sd_lock in the macros below
2326424Skrishna  * as they are called after doing a get_session_ptr() which
2336424Skrishna  * sets the CRYPTO_SESSION_IS_BUSY flag.
2346424Skrishna  */
2356424Skrishna #define	CRYPTO_DECREMENT_RCTL_SESSION(sp, val, rctl_chk) 	\
236*6867Skrishna 	if (((val) != 0) && ((sp) != NULL)) {			\
2376424Skrishna 		ASSERT(((sp)->sd_flags & CRYPTO_SESSION_IS_BUSY) != 0);	\
2386424Skrishna 		if (rctl_chk) {				\
2396424Skrishna 			CRYPTO_DECREMENT_RCTL(val);		\
2406424Skrishna 		} else {					\
2416424Skrishna 			(sp)->sd_pre_approved_amount += (val);	\
2426424Skrishna 		}						\
2436424Skrishna 	}
2446424Skrishna 
2456424Skrishna #define	CRYPTO_BUFFER_CHECK(sp, need, rctl_chk)		\
2466424Skrishna 	((sp->sd_pre_approved_amount >= need) ?			\
2476424Skrishna 	(sp->sd_pre_approved_amount -= need,			\
2486424Skrishna 	    rctl_chk = B_FALSE, CRYPTO_SUCCESS) :		\
2496424Skrishna 	    (rctl_chk = B_TRUE, crypto_buffer_check(need)))
2506424Skrishna 
2516424Skrishna /*
2520Sstevel@tonic-gate  * Module linkage.
2530Sstevel@tonic-gate  */
2540Sstevel@tonic-gate static struct cb_ops cbops = {
2550Sstevel@tonic-gate 	crypto_open,		/* cb_open */
2560Sstevel@tonic-gate 	crypto_close,		/* cb_close */
2570Sstevel@tonic-gate 	nodev,			/* cb_strategy */
2580Sstevel@tonic-gate 	nodev,			/* cb_print */
2590Sstevel@tonic-gate 	nodev,			/* cb_dump */
2600Sstevel@tonic-gate 	nodev,			/* cb_read */
2610Sstevel@tonic-gate 	nodev,			/* cb_write */
2620Sstevel@tonic-gate 	crypto_ioctl,		/* cb_ioctl */
2630Sstevel@tonic-gate 	nodev,			/* cb_devmap */
2640Sstevel@tonic-gate 	nodev,			/* cb_mmap */
2650Sstevel@tonic-gate 	nodev,			/* cb_segmap */
2660Sstevel@tonic-gate 	nochpoll,		/* cb_chpoll */
2670Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
2680Sstevel@tonic-gate 	NULL,			/* cb_streamtab */
2690Sstevel@tonic-gate 	D_MP,			/* cb_flag */
2700Sstevel@tonic-gate 	CB_REV,			/* cb_rev */
2710Sstevel@tonic-gate 	nodev,			/* cb_aread */
2720Sstevel@tonic-gate 	nodev,			/* cb_awrite */
2730Sstevel@tonic-gate };
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate static struct dev_ops devops = {
2760Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
2770Sstevel@tonic-gate 	0,			/* devo_refcnt */
2780Sstevel@tonic-gate 	crypto_getinfo,		/* devo_getinfo */
2790Sstevel@tonic-gate 	nulldev,		/* devo_identify */
2800Sstevel@tonic-gate 	nulldev,		/* devo_probe */
2810Sstevel@tonic-gate 	crypto_attach,		/* devo_attach */
2820Sstevel@tonic-gate 	crypto_detach,		/* devo_detach */
2830Sstevel@tonic-gate 	nodev,			/* devo_reset */
2840Sstevel@tonic-gate 	&cbops,			/* devo_cb_ops */
2850Sstevel@tonic-gate 	NULL,			/* devo_bus_ops */
2860Sstevel@tonic-gate 	NULL,			/* devo_power */
2870Sstevel@tonic-gate };
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate static struct modldrv modldrv = {
2900Sstevel@tonic-gate 	&mod_driverops,					/* drv_modops */
2915072Smcpowers 	"Cryptographic Library Interface",	/* drv_linkinfo */
2920Sstevel@tonic-gate 	&devops,
2930Sstevel@tonic-gate };
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate static struct modlinkage modlinkage = {
2960Sstevel@tonic-gate 	MODREV_1,		/* ml_rev */
2970Sstevel@tonic-gate 	&modldrv,		/* ml_linkage */
2980Sstevel@tonic-gate 	NULL
2990Sstevel@tonic-gate };
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate /*
3020Sstevel@tonic-gate  * DDI entry points.
3030Sstevel@tonic-gate  */
3040Sstevel@tonic-gate int
3050Sstevel@tonic-gate _init(void)
3060Sstevel@tonic-gate {
3070Sstevel@tonic-gate 	return (mod_install(&modlinkage));
3080Sstevel@tonic-gate }
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate int
3110Sstevel@tonic-gate _fini(void)
3120Sstevel@tonic-gate {
3130Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
3140Sstevel@tonic-gate }
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate int
3170Sstevel@tonic-gate _info(struct modinfo *modinfop)
3180Sstevel@tonic-gate {
3190Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate /* ARGSUSED */
3230Sstevel@tonic-gate static int
3240Sstevel@tonic-gate crypto_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
3250Sstevel@tonic-gate {
3260Sstevel@tonic-gate 	switch (cmd) {
3270Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
3280Sstevel@tonic-gate 		*result = crypto_dip;
3290Sstevel@tonic-gate 		return (DDI_SUCCESS);
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
3320Sstevel@tonic-gate 		*result = (void *)0;
3330Sstevel@tonic-gate 		return (DDI_SUCCESS);
3340Sstevel@tonic-gate 	}
3350Sstevel@tonic-gate 	return (DDI_FAILURE);
3360Sstevel@tonic-gate }
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate static int
3390Sstevel@tonic-gate crypto_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3400Sstevel@tonic-gate {
3410Sstevel@tonic-gate 	if (cmd != DDI_ATTACH) {
3420Sstevel@tonic-gate 		return (DDI_FAILURE);
3430Sstevel@tonic-gate 	}
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	if (ddi_get_instance(dip) != 0) {
3460Sstevel@tonic-gate 		/* we only allow instance 0 to attach */
3470Sstevel@tonic-gate 		return (DDI_FAILURE);
3480Sstevel@tonic-gate 	}
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	crypto_session_cache = kmem_cache_create("crypto_session_cache",
3510Sstevel@tonic-gate 	    sizeof (crypto_session_data_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	if (crypto_session_cache == NULL)
3540Sstevel@tonic-gate 		return (DDI_FAILURE);
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	/* create the minor node */
3570Sstevel@tonic-gate 	if (ddi_create_minor_node(dip, "crypto", S_IFCHR, 0,
3580Sstevel@tonic-gate 	    DDI_PSEUDO, 0) != DDI_SUCCESS) {
3590Sstevel@tonic-gate 		kmem_cache_destroy(crypto_session_cache);
3600Sstevel@tonic-gate 		crypto_session_cache = NULL;
3610Sstevel@tonic-gate 		cmn_err(CE_WARN, "crypto_attach: failed creating minor node");
3620Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
3630Sstevel@tonic-gate 		return (DDI_FAILURE);
3640Sstevel@tonic-gate 	}
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	mutex_init(&crypto_lock, NULL, MUTEX_DRIVER, NULL);
3670Sstevel@tonic-gate 	cv_init(&crypto_cv, NULL, CV_DRIVER, NULL);
3680Sstevel@tonic-gate 	crypto_dip = dip;
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	/* allocate integer space for minor numbers */
3710Sstevel@tonic-gate 	crypto_arena = vmem_create("crypto", (void *)1,
3720Sstevel@tonic-gate 	    CRYPTO_MINOR_CHUNK, 1, NULL, NULL, NULL, 0,
3730Sstevel@tonic-gate 	    VM_SLEEP | VMC_IDENTIFIER);
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	return (DDI_SUCCESS);
3760Sstevel@tonic-gate }
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate static int
3790Sstevel@tonic-gate crypto_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3800Sstevel@tonic-gate {
3810Sstevel@tonic-gate 	minor_t i;
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
3840Sstevel@tonic-gate 		return (DDI_FAILURE);
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 	/* check if device is open */
3870Sstevel@tonic-gate 	mutex_enter(&crypto_lock);
3880Sstevel@tonic-gate 	for (i = 0; i < crypto_minors_table_count; i++) {
3890Sstevel@tonic-gate 		if (crypto_minors[i] != NULL) {
3900Sstevel@tonic-gate 			mutex_exit(&crypto_lock);
3910Sstevel@tonic-gate 			return (DDI_FAILURE);
3920Sstevel@tonic-gate 		}
3930Sstevel@tonic-gate 	}
3940Sstevel@tonic-gate 	mutex_exit(&crypto_lock);
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 	crypto_dip = NULL;
3970Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	kmem_cache_destroy(crypto_session_cache);
4000Sstevel@tonic-gate 	crypto_session_cache = NULL;
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	kmem_free(crypto_minors,
4030Sstevel@tonic-gate 	    sizeof (crypto_minor_t *) * crypto_minors_table_count);
4040Sstevel@tonic-gate 	crypto_minors = NULL;
4050Sstevel@tonic-gate 	crypto_minors_table_count = 0;
4060Sstevel@tonic-gate 	mutex_destroy(&crypto_lock);
4070Sstevel@tonic-gate 	cv_destroy(&crypto_cv);
4080Sstevel@tonic-gate 	vmem_destroy(crypto_arena);
4090Sstevel@tonic-gate 	crypto_arena = NULL;
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	return (DDI_SUCCESS);
4120Sstevel@tonic-gate }
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate /* ARGSUSED */
4150Sstevel@tonic-gate static int
4160Sstevel@tonic-gate crypto_open(dev_t *devp, int flag, int otyp, cred_t *credp)
4170Sstevel@tonic-gate {
4180Sstevel@tonic-gate 	crypto_minor_t *cm = NULL;
4190Sstevel@tonic-gate 	minor_t mn;
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
4220Sstevel@tonic-gate 		return (ENXIO);
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	if (crypto_dip == NULL)
4250Sstevel@tonic-gate 		return (ENXIO);
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	/* exclusive opens are not supported */
4280Sstevel@tonic-gate 	if (flag & FEXCL)
4290Sstevel@tonic-gate 		return (ENOTSUP);
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	mutex_enter(&crypto_lock);
4320Sstevel@tonic-gate again:
4330Sstevel@tonic-gate 	/* grow the minors table if needed */
4340Sstevel@tonic-gate 	if (crypto_minors_count >= crypto_minors_table_count) {
4350Sstevel@tonic-gate 		crypto_minor_t **newtable;
4360Sstevel@tonic-gate 		minor_t chunk = crypto_minor_chunk;
4370Sstevel@tonic-gate 		minor_t saved_count;
4380Sstevel@tonic-gate 		size_t new_size;
4390Sstevel@tonic-gate 		ulong_t big_count;
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 		big_count = crypto_minors_count + chunk;
4420Sstevel@tonic-gate 		if (big_count > MAXMIN) {
4430Sstevel@tonic-gate 			mutex_exit(&crypto_lock);
4440Sstevel@tonic-gate 			return (ENOMEM);
4450Sstevel@tonic-gate 		}
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 		saved_count = crypto_minors_table_count;
4480Sstevel@tonic-gate 		new_size = sizeof (crypto_minor_t *) *
4490Sstevel@tonic-gate 		    (crypto_minors_table_count + chunk);
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 		mutex_exit(&crypto_lock);
4520Sstevel@tonic-gate 		newtable = kmem_zalloc(new_size, KM_SLEEP);
4530Sstevel@tonic-gate 		mutex_enter(&crypto_lock);
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 		/*
4560Sstevel@tonic-gate 		 * Check if table grew while we were sleeping.
4570Sstevel@tonic-gate 		 * The minors table never shrinks.
4580Sstevel@tonic-gate 		 */
4590Sstevel@tonic-gate 		if (crypto_minors_table_count > saved_count) {
4600Sstevel@tonic-gate 			kmem_free(newtable, new_size);
4610Sstevel@tonic-gate 			goto again;
4620Sstevel@tonic-gate 		}
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 		/* we assume that bcopy() will return if count is 0 */
4650Sstevel@tonic-gate 		bcopy(crypto_minors, newtable,
4660Sstevel@tonic-gate 		    sizeof (crypto_minor_t *) * crypto_minors_table_count);
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 		kmem_free(crypto_minors,
4690Sstevel@tonic-gate 		    sizeof (crypto_minor_t *) * crypto_minors_table_count);
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 		/* grow the minors number space */
4720Sstevel@tonic-gate 		if (crypto_minors_table_count != 0) {
4730Sstevel@tonic-gate 			(void) vmem_add(crypto_arena,
4740Sstevel@tonic-gate 			    (void *)(uintptr_t)(crypto_minors_table_count + 1),
4750Sstevel@tonic-gate 			    crypto_minor_chunk, VM_SLEEP);
4760Sstevel@tonic-gate 		}
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 		crypto_minors = newtable;
4790Sstevel@tonic-gate 		crypto_minors_table_count += chunk;
4800Sstevel@tonic-gate 	}
4810Sstevel@tonic-gate 	mutex_exit(&crypto_lock);
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	/* allocate a new minor number starting with 1 */
4840Sstevel@tonic-gate 	mn = (minor_t)(uintptr_t)vmem_alloc(crypto_arena, 1, VM_SLEEP);
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	cm = kmem_zalloc(sizeof (crypto_minor_t), KM_SLEEP);
4870Sstevel@tonic-gate 	mutex_init(&cm->cm_lock, NULL, MUTEX_DRIVER, NULL);
4880Sstevel@tonic-gate 	cv_init(&cm->cm_cv, NULL, CV_DRIVER, NULL);
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	mutex_enter(&crypto_lock);
4910Sstevel@tonic-gate 	crypto_minors[mn - 1] = cm;
4920Sstevel@tonic-gate 	crypto_minors_count++;
4930Sstevel@tonic-gate 	mutex_exit(&crypto_lock);
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	*devp = makedevice(getmajor(*devp), mn);
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	return (0);
4980Sstevel@tonic-gate }
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate /* ARGSUSED */
5010Sstevel@tonic-gate static int
5020Sstevel@tonic-gate crypto_close(dev_t dev, int flag, int otyp, cred_t *credp)
5030Sstevel@tonic-gate {
5040Sstevel@tonic-gate 	crypto_minor_t *cm = NULL;
5050Sstevel@tonic-gate 	crypto_session_data_t *sp;
5060Sstevel@tonic-gate 	minor_t mn = getminor(dev);
5070Sstevel@tonic-gate 	uint_t i;
5086424Skrishna 	size_t total = 0;
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 	mutex_enter(&crypto_lock);
5110Sstevel@tonic-gate 	if (mn > crypto_minors_table_count) {
5120Sstevel@tonic-gate 		mutex_exit(&crypto_lock);
5130Sstevel@tonic-gate 		cmn_err(CE_WARN, "crypto_close: bad minor (too big) %d", mn);
5140Sstevel@tonic-gate 		return (ENODEV);
5150Sstevel@tonic-gate 	}
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	while (((cm = crypto_minors[mn - 1]) != NULL) && (cm->cm_refcnt > 0)) {
5180Sstevel@tonic-gate 		cv_wait(&crypto_cv, &crypto_lock);
5190Sstevel@tonic-gate 	}
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	if (cm == NULL) {
5220Sstevel@tonic-gate 		mutex_exit(&crypto_lock);
5230Sstevel@tonic-gate 		cmn_err(CE_WARN, "crypto_close: duplicate close of minor %d",
5240Sstevel@tonic-gate 		    getminor(dev));
5250Sstevel@tonic-gate 		return (ENODEV);
5260Sstevel@tonic-gate 	}
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	/* take it out of the global table */
5290Sstevel@tonic-gate 	crypto_minors[mn - 1] = NULL;
5300Sstevel@tonic-gate 	crypto_minors_count--;
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	vmem_free(crypto_arena, (void *)(uintptr_t)mn, 1);
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
5350Sstevel@tonic-gate 	mutex_exit(&crypto_lock);
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	/* free all session table entries starting with 1 */
5380Sstevel@tonic-gate 	for (i = 1; i < cm->cm_session_table_count; i++) {
5390Sstevel@tonic-gate 		if (cm->cm_session_table[i] == NULL)
5400Sstevel@tonic-gate 			continue;
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 		sp = cm->cm_session_table[i];
5430Sstevel@tonic-gate 		ASSERT((sp->sd_flags & CRYPTO_SESSION_IS_BUSY) == 0);
5446424Skrishna 		ASSERT(sp->sd_pre_approved_amount == 0 ||
5456424Skrishna 		    sp->sd_pre_approved_amount == crypto_pre_approved_limit);
5466424Skrishna 		total += sp->sd_pre_approved_amount;
5470Sstevel@tonic-gate 		if (sp->sd_find_init_cookie != NULL) {
5480Sstevel@tonic-gate 			(void) crypto_free_find_ctx(sp);
5490Sstevel@tonic-gate 		}
5500Sstevel@tonic-gate 		crypto_release_provider_session(cm, sp->sd_provider_session);
5510Sstevel@tonic-gate 		KCF_PROV_REFRELE(sp->sd_provider);
5520Sstevel@tonic-gate 		CRYPTO_CANCEL_ALL_CTX(sp);
5530Sstevel@tonic-gate 		mutex_destroy(&sp->sd_lock);
5540Sstevel@tonic-gate 		cv_destroy(&sp->sd_cv);
5550Sstevel@tonic-gate 		kmem_cache_free(crypto_session_cache, sp);
5560Sstevel@tonic-gate 		cm->cm_session_table[i] = NULL;
5570Sstevel@tonic-gate 	}
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 	/* free the session table */
5600Sstevel@tonic-gate 	if (cm->cm_session_table != NULL && cm->cm_session_table_count > 0)
5610Sstevel@tonic-gate 		kmem_free(cm->cm_session_table, cm->cm_session_table_count *
5620Sstevel@tonic-gate 		    sizeof (void *));
5630Sstevel@tonic-gate 
5646424Skrishna 	total += (cm->cm_session_table_count * sizeof (void *));
5656424Skrishna 	CRYPTO_DECREMENT_RCTL(total);
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	kcf_free_provider_tab(cm->cm_provider_count,
5680Sstevel@tonic-gate 	    cm->cm_provider_array);
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 	mutex_destroy(&cm->cm_lock);
5710Sstevel@tonic-gate 	cv_destroy(&cm->cm_cv);
5720Sstevel@tonic-gate 	kmem_free(cm, sizeof (crypto_minor_t));
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 	return (0);
5750Sstevel@tonic-gate }
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate static crypto_minor_t *
5780Sstevel@tonic-gate crypto_hold_minor(minor_t minor)
5790Sstevel@tonic-gate {
5800Sstevel@tonic-gate 	crypto_minor_t *cm = NULL;
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 	mutex_enter(&crypto_lock);
5830Sstevel@tonic-gate 	if ((minor <= crypto_minors_table_count) &&
5840Sstevel@tonic-gate 	    ((cm = crypto_minors[minor - 1]) != NULL)) {
5850Sstevel@tonic-gate 		cm->cm_refcnt++;
5860Sstevel@tonic-gate 	}
5870Sstevel@tonic-gate 	mutex_exit(&crypto_lock);
5880Sstevel@tonic-gate 	return (cm);
5890Sstevel@tonic-gate }
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate static void
5920Sstevel@tonic-gate crypto_release_minor(crypto_minor_t *cm)
5930Sstevel@tonic-gate {
5940Sstevel@tonic-gate 	mutex_enter(&crypto_lock);
5950Sstevel@tonic-gate 	cm->cm_refcnt--;
5960Sstevel@tonic-gate 	if (cm->cm_refcnt == 0) {
5970Sstevel@tonic-gate 		cv_broadcast(&crypto_cv);
5980Sstevel@tonic-gate 	}
5990Sstevel@tonic-gate 	mutex_exit(&crypto_lock);
6000Sstevel@tonic-gate }
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate /*
6034072Skrishna  * Build a list of functions and other information for the provider, pd.
6040Sstevel@tonic-gate  */
6050Sstevel@tonic-gate static void
6060Sstevel@tonic-gate crypto_build_function_list(crypto_function_list_t *fl, kcf_provider_desc_t *pd)
6070Sstevel@tonic-gate {
6080Sstevel@tonic-gate 	crypto_ops_t *ops;
6090Sstevel@tonic-gate 	crypto_digest_ops_t *digest_ops;
6100Sstevel@tonic-gate 	crypto_cipher_ops_t *cipher_ops;
6110Sstevel@tonic-gate 	crypto_mac_ops_t *mac_ops;
6120Sstevel@tonic-gate 	crypto_sign_ops_t *sign_ops;
6130Sstevel@tonic-gate 	crypto_verify_ops_t *verify_ops;
6140Sstevel@tonic-gate 	crypto_dual_ops_t *dual_ops;
6150Sstevel@tonic-gate 	crypto_random_number_ops_t *random_number_ops;
6160Sstevel@tonic-gate 	crypto_session_ops_t *session_ops;
6170Sstevel@tonic-gate 	crypto_object_ops_t *object_ops;
6180Sstevel@tonic-gate 	crypto_key_ops_t *key_ops;
6190Sstevel@tonic-gate 	crypto_provider_management_ops_t *provider_ops;
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 	if ((ops = pd->pd_ops_vector) == NULL)
6220Sstevel@tonic-gate 		return;
6230Sstevel@tonic-gate 
624904Smcpowers 	if ((digest_ops = ops->co_digest_ops) != NULL) {
6250Sstevel@tonic-gate 		if (digest_ops->digest_init != NULL)
6260Sstevel@tonic-gate 			fl->fl_digest_init = B_TRUE;
6270Sstevel@tonic-gate 		if (digest_ops->digest != NULL)
6280Sstevel@tonic-gate 			fl->fl_digest = B_TRUE;
6290Sstevel@tonic-gate 		if (digest_ops->digest_update != NULL)
6300Sstevel@tonic-gate 			fl->fl_digest_update = B_TRUE;
6310Sstevel@tonic-gate 		if (digest_ops->digest_key != NULL)
6320Sstevel@tonic-gate 			fl->fl_digest_key = B_TRUE;
6330Sstevel@tonic-gate 		if (digest_ops->digest_final != NULL)
6340Sstevel@tonic-gate 			fl->fl_digest_final = B_TRUE;
6350Sstevel@tonic-gate 	}
636904Smcpowers 	if ((cipher_ops = ops->co_cipher_ops) != NULL) {
6370Sstevel@tonic-gate 		if (cipher_ops->encrypt_init != NULL)
6380Sstevel@tonic-gate 			fl->fl_encrypt_init = B_TRUE;
6390Sstevel@tonic-gate 		if (cipher_ops->encrypt != NULL)
6400Sstevel@tonic-gate 			fl->fl_encrypt = B_TRUE;
6410Sstevel@tonic-gate 		if (cipher_ops->encrypt_update != NULL)
6420Sstevel@tonic-gate 			fl->fl_encrypt_update = B_TRUE;
6430Sstevel@tonic-gate 		if (cipher_ops->encrypt_final != NULL)
6440Sstevel@tonic-gate 			fl->fl_encrypt_final = B_TRUE;
6450Sstevel@tonic-gate 		if (cipher_ops->decrypt_init != NULL)
6460Sstevel@tonic-gate 			fl->fl_decrypt_init = B_TRUE;
6470Sstevel@tonic-gate 		if (cipher_ops->decrypt != NULL)
6480Sstevel@tonic-gate 			fl->fl_decrypt = B_TRUE;
6490Sstevel@tonic-gate 		if (cipher_ops->decrypt_update != NULL)
6500Sstevel@tonic-gate 			fl->fl_decrypt_update = B_TRUE;
6510Sstevel@tonic-gate 		if (cipher_ops->decrypt_final != NULL)
6520Sstevel@tonic-gate 			fl->fl_decrypt_final = B_TRUE;
6530Sstevel@tonic-gate 	}
654904Smcpowers 	if ((mac_ops = ops->co_mac_ops) != NULL) {
6550Sstevel@tonic-gate 		if (mac_ops->mac_init != NULL)
6560Sstevel@tonic-gate 			fl->fl_mac_init = B_TRUE;
6570Sstevel@tonic-gate 		if (mac_ops->mac != NULL)
6580Sstevel@tonic-gate 			fl->fl_mac = B_TRUE;
6590Sstevel@tonic-gate 		if (mac_ops->mac_update != NULL)
6600Sstevel@tonic-gate 			fl->fl_mac_update = B_TRUE;
6610Sstevel@tonic-gate 		if (mac_ops->mac_final != NULL)
6620Sstevel@tonic-gate 			fl->fl_mac_final = B_TRUE;
6630Sstevel@tonic-gate 	}
664904Smcpowers 	if ((sign_ops = ops->co_sign_ops) != NULL) {
6650Sstevel@tonic-gate 		if (sign_ops->sign_init != NULL)
6660Sstevel@tonic-gate 			fl->fl_sign_init = B_TRUE;
6670Sstevel@tonic-gate 		if (sign_ops->sign != NULL)
6680Sstevel@tonic-gate 			fl->fl_sign = B_TRUE;
6690Sstevel@tonic-gate 		if (sign_ops->sign_update != NULL)
6700Sstevel@tonic-gate 			fl->fl_sign_update = B_TRUE;
6710Sstevel@tonic-gate 		if (sign_ops->sign_final != NULL)
6720Sstevel@tonic-gate 			fl->fl_sign_final = B_TRUE;
6730Sstevel@tonic-gate 		if (sign_ops->sign_recover_init != NULL)
6740Sstevel@tonic-gate 			fl->fl_sign_recover_init = B_TRUE;
6750Sstevel@tonic-gate 		if (sign_ops->sign_recover != NULL)
6760Sstevel@tonic-gate 			fl->fl_sign_recover = B_TRUE;
6770Sstevel@tonic-gate 	}
678904Smcpowers 	if ((verify_ops = ops->co_verify_ops) != NULL) {
6790Sstevel@tonic-gate 		if (verify_ops->verify_init != NULL)
6800Sstevel@tonic-gate 			fl->fl_verify_init = B_TRUE;
6810Sstevel@tonic-gate 		if (verify_ops->verify != NULL)
6820Sstevel@tonic-gate 			fl->fl_verify = B_TRUE;
6830Sstevel@tonic-gate 		if (verify_ops->verify_update != NULL)
6840Sstevel@tonic-gate 			fl->fl_verify_update = B_TRUE;
6850Sstevel@tonic-gate 		if (verify_ops->verify_final != NULL)
6860Sstevel@tonic-gate 			fl->fl_verify_final = B_TRUE;
6870Sstevel@tonic-gate 		if (verify_ops->verify_recover_init != NULL)
6880Sstevel@tonic-gate 			fl->fl_verify_recover_init = B_TRUE;
6890Sstevel@tonic-gate 		if (verify_ops->verify_recover != NULL)
6900Sstevel@tonic-gate 			fl->fl_verify_recover = B_TRUE;
6910Sstevel@tonic-gate 	}
692904Smcpowers 	if ((dual_ops = ops->co_dual_ops) != NULL) {
6930Sstevel@tonic-gate 		if (dual_ops->digest_encrypt_update != NULL)
6940Sstevel@tonic-gate 			fl->fl_digest_encrypt_update = B_TRUE;
6950Sstevel@tonic-gate 		if (dual_ops->decrypt_digest_update != NULL)
6960Sstevel@tonic-gate 			fl->fl_decrypt_digest_update = B_TRUE;
6970Sstevel@tonic-gate 		if (dual_ops->sign_encrypt_update != NULL)
6980Sstevel@tonic-gate 			fl->fl_sign_encrypt_update = B_TRUE;
6990Sstevel@tonic-gate 		if (dual_ops->decrypt_verify_update != NULL)
7000Sstevel@tonic-gate 			fl->fl_decrypt_verify_update = B_TRUE;
7010Sstevel@tonic-gate 	}
702904Smcpowers 	if ((random_number_ops = ops->co_random_ops) != NULL) {
7030Sstevel@tonic-gate 		if (random_number_ops->seed_random != NULL)
7040Sstevel@tonic-gate 			fl->fl_seed_random = B_TRUE;
7050Sstevel@tonic-gate 		if (random_number_ops->generate_random != NULL)
7060Sstevel@tonic-gate 			fl->fl_generate_random = B_TRUE;
7070Sstevel@tonic-gate 	}
708904Smcpowers 	if ((session_ops = ops->co_session_ops) != NULL) {
7090Sstevel@tonic-gate 		if (session_ops->session_open != NULL)
7100Sstevel@tonic-gate 			fl->fl_session_open = B_TRUE;
7110Sstevel@tonic-gate 		if (session_ops->session_close != NULL)
7120Sstevel@tonic-gate 			fl->fl_session_close = B_TRUE;
7130Sstevel@tonic-gate 		if (session_ops->session_login != NULL)
7140Sstevel@tonic-gate 			fl->fl_session_login = B_TRUE;
7150Sstevel@tonic-gate 		if (session_ops->session_logout != NULL)
7160Sstevel@tonic-gate 			fl->fl_session_logout = B_TRUE;
7170Sstevel@tonic-gate 	}
718904Smcpowers 	if ((object_ops = ops->co_object_ops) != NULL) {
7190Sstevel@tonic-gate 		if (object_ops->object_create != NULL)
7200Sstevel@tonic-gate 			fl->fl_object_create = B_TRUE;
7210Sstevel@tonic-gate 		if (object_ops->object_copy != NULL)
7220Sstevel@tonic-gate 			fl->fl_object_copy = B_TRUE;
7230Sstevel@tonic-gate 		if (object_ops->object_destroy != NULL)
7240Sstevel@tonic-gate 			fl->fl_object_destroy = B_TRUE;
7250Sstevel@tonic-gate 		if (object_ops->object_get_size != NULL)
7260Sstevel@tonic-gate 			fl->fl_object_get_size = B_TRUE;
7270Sstevel@tonic-gate 		if (object_ops->object_get_attribute_value != NULL)
7280Sstevel@tonic-gate 			fl->fl_object_get_attribute_value = B_TRUE;
7290Sstevel@tonic-gate 		if (object_ops->object_set_attribute_value != NULL)
7300Sstevel@tonic-gate 			fl->fl_object_set_attribute_value = B_TRUE;
7310Sstevel@tonic-gate 		if (object_ops->object_find_init != NULL)
7320Sstevel@tonic-gate 			fl->fl_object_find_init = B_TRUE;
7330Sstevel@tonic-gate 		if (object_ops->object_find != NULL)
7340Sstevel@tonic-gate 			fl->fl_object_find = B_TRUE;
7350Sstevel@tonic-gate 		if (object_ops->object_find_final != NULL)
7360Sstevel@tonic-gate 			fl->fl_object_find_final = B_TRUE;
7370Sstevel@tonic-gate 	}
738904Smcpowers 	if ((key_ops = ops->co_key_ops) != NULL) {
7390Sstevel@tonic-gate 		if (key_ops->key_generate != NULL)
7400Sstevel@tonic-gate 			fl->fl_key_generate = B_TRUE;
7410Sstevel@tonic-gate 		if (key_ops->key_generate_pair != NULL)
7420Sstevel@tonic-gate 			fl->fl_key_generate_pair = B_TRUE;
7430Sstevel@tonic-gate 		if (key_ops->key_wrap != NULL)
7440Sstevel@tonic-gate 			fl->fl_key_wrap = B_TRUE;
7450Sstevel@tonic-gate 		if (key_ops->key_unwrap != NULL)
7460Sstevel@tonic-gate 			fl->fl_key_unwrap = B_TRUE;
7470Sstevel@tonic-gate 		if (key_ops->key_derive != NULL)
7480Sstevel@tonic-gate 			fl->fl_key_derive = B_TRUE;
7490Sstevel@tonic-gate 	}
750904Smcpowers 	if ((provider_ops = ops->co_provider_ops) != NULL) {
7510Sstevel@tonic-gate 		if (provider_ops->init_token != NULL)
7520Sstevel@tonic-gate 			fl->fl_init_token = B_TRUE;
7530Sstevel@tonic-gate 		if (provider_ops->init_pin != NULL)
7540Sstevel@tonic-gate 			fl->fl_init_pin = B_TRUE;
7550Sstevel@tonic-gate 		if (provider_ops->set_pin != NULL)
7560Sstevel@tonic-gate 			fl->fl_set_pin = B_TRUE;
7570Sstevel@tonic-gate 	}
7584072Skrishna 
7594072Skrishna 	fl->prov_is_limited = pd->pd_flags & CRYPTO_HASH_NO_UPDATE;
7604072Skrishna 	if (fl->prov_is_limited) {
7614072Skrishna 		/*
7624072Skrishna 		 * XXX - The threshold should ideally be per hash
7634072Skrishna 		 * mechanism. For now, we use the same value for all
7644072Skrishna 		 * hash mechanisms. Empirical evidence suggests this
7654072Skrishna 		 * is fine.
7664072Skrishna 		 */
7674072Skrishna 		fl->prov_hash_threshold = kcf_md5_threshold;
7684072Skrishna 		fl->prov_hash_limit = min(pd->pd_hash_limit,
7694072Skrishna 		    min(CRYPTO_MAX_BUFFER_LEN,
7704072Skrishna 		    curproc->p_task->tk_proj->kpj_data.kpd_crypto_mem_ctl));
7714072Skrishna 	}
7724841Shaimay 
7734841Shaimay 	fl->total_threshold_count = MAX_NUM_THRESHOLD;
7744841Shaimay 	fl->fl_threshold[0].mech_type = CKM_DES3_CBC;
7754841Shaimay 	fl->fl_threshold[0].mech_threshold = kcf_des3_threshold;
7764841Shaimay 	fl->fl_threshold[1].mech_type = CKM_DES3_ECB;
7774841Shaimay 	fl->fl_threshold[1].mech_threshold = kcf_des3_threshold;
7784841Shaimay 	fl->fl_threshold[2].mech_type = CKM_AES_CBC;
7794841Shaimay 	fl->fl_threshold[2].mech_threshold = kcf_aes_threshold;
7804841Shaimay 	fl->fl_threshold[3].mech_type = CKM_AES_ECB;
7814841Shaimay 	fl->fl_threshold[3].mech_threshold = kcf_aes_threshold;
7824841Shaimay 	fl->fl_threshold[4].mech_type = CKM_RC4;
7834841Shaimay 	fl->fl_threshold[4].mech_threshold = kcf_rc4_threshold;
7844841Shaimay 	fl->fl_threshold[5].mech_type = CKM_MD5;
7854841Shaimay 	fl->fl_threshold[5].mech_threshold = kcf_md5_threshold;
7864841Shaimay 	fl->fl_threshold[6].mech_type = CKM_SHA_1;
7874841Shaimay 	fl->fl_threshold[6].mech_threshold = kcf_sha1_threshold;
7880Sstevel@tonic-gate }
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate /* ARGSUSED */
7910Sstevel@tonic-gate static int
7920Sstevel@tonic-gate get_function_list(dev_t dev, caddr_t arg, int mode, int *rval)
7930Sstevel@tonic-gate {
7940Sstevel@tonic-gate 	crypto_get_function_list_t get_function_list;
7950Sstevel@tonic-gate 	crypto_minor_t *cm;
7960Sstevel@tonic-gate 	crypto_provider_id_t provider_id;
7970Sstevel@tonic-gate 	crypto_function_list_t *fl;
7980Sstevel@tonic-gate 	kcf_provider_desc_t *provider;
7990Sstevel@tonic-gate 	int rv;
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
8020Sstevel@tonic-gate 		cmn_err(CE_WARN, "get_function_list: failed holding minor");
8030Sstevel@tonic-gate 		return (ENXIO);
8040Sstevel@tonic-gate 	}
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	if (copyin(arg, &get_function_list, sizeof (get_function_list)) != 0) {
8070Sstevel@tonic-gate 		crypto_release_minor(cm);
8080Sstevel@tonic-gate 		return (EFAULT);
8090Sstevel@tonic-gate 	}
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 	/* initialize provider_array */
8120Sstevel@tonic-gate 	if (cm->cm_provider_array == NULL) {
8130Sstevel@tonic-gate 		rv = crypto_get_provider_list(cm, NULL, NULL, DONT_RETURN_LIST);
8140Sstevel@tonic-gate 		if (rv != CRYPTO_SUCCESS) {
8150Sstevel@tonic-gate 			goto release_minor;
8160Sstevel@tonic-gate 		}
8170Sstevel@tonic-gate 	}
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 	provider_id = get_function_list.fl_provider_id;
8200Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
8210Sstevel@tonic-gate 	/* index must be less than count of providers */
8220Sstevel@tonic-gate 	if (provider_id >= cm->cm_provider_count) {
8230Sstevel@tonic-gate 		mutex_exit(&cm->cm_lock);
8240Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
8250Sstevel@tonic-gate 		goto release_minor;
8260Sstevel@tonic-gate 	}
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 	ASSERT(cm->cm_provider_array != NULL);
8290Sstevel@tonic-gate 	provider = cm->cm_provider_array[provider_id];
8300Sstevel@tonic-gate 	mutex_exit(&cm->cm_lock);
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 	fl = &get_function_list.fl_list;
8330Sstevel@tonic-gate 	bzero(fl, sizeof (crypto_function_list_t));
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	if (provider->pd_prov_type != CRYPTO_LOGICAL_PROVIDER) {
8360Sstevel@tonic-gate 		crypto_build_function_list(fl, provider);
8370Sstevel@tonic-gate 	} else {
8380Sstevel@tonic-gate 		kcf_provider_desc_t *prev = NULL, *pd;
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate 		mutex_enter(&provider->pd_lock);
8410Sstevel@tonic-gate 		while (kcf_get_next_logical_provider_member(provider,
8420Sstevel@tonic-gate 		    prev, &pd)) {
8430Sstevel@tonic-gate 			prev = pd;
8440Sstevel@tonic-gate 			crypto_build_function_list(fl, pd);
8450Sstevel@tonic-gate 			KCF_PROV_REFRELE(pd);
8460Sstevel@tonic-gate 		}
8470Sstevel@tonic-gate 		mutex_exit(&provider->pd_lock);
8480Sstevel@tonic-gate 	}
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 	rv = CRYPTO_SUCCESS;
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate release_minor:
8530Sstevel@tonic-gate 	crypto_release_minor(cm);
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 	get_function_list.fl_return_value = rv;
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	if (copyout(&get_function_list, arg, sizeof (get_function_list)) != 0) {
8580Sstevel@tonic-gate 		return (EFAULT);
8590Sstevel@tonic-gate 	}
8600Sstevel@tonic-gate 	return (0);
8610Sstevel@tonic-gate }
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate /*
8640Sstevel@tonic-gate  * This ioctl maps a PKCS#11 mechanism string into an internal number
8650Sstevel@tonic-gate  * that is used by the kernel.  pn_internal_number is set to the
8660Sstevel@tonic-gate  * internal number.
8670Sstevel@tonic-gate  */
8680Sstevel@tonic-gate /* ARGSUSED */
8690Sstevel@tonic-gate static int
8700Sstevel@tonic-gate get_mechanism_number(dev_t dev, caddr_t arg, int mode, int *rval)
8710Sstevel@tonic-gate {
8720Sstevel@tonic-gate 	STRUCT_DECL(crypto_get_mechanism_number, get_number);
8730Sstevel@tonic-gate 	crypto_mech_type_t number;
8740Sstevel@tonic-gate 	size_t len;
8750Sstevel@tonic-gate 	char *mechanism_name;
8760Sstevel@tonic-gate 	int rv;
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 	STRUCT_INIT(get_number, mode);
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(get_number), STRUCT_SIZE(get_number)) != 0)
8810Sstevel@tonic-gate 		return (EFAULT);
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	len = STRUCT_FGET(get_number, pn_mechanism_len);
8840Sstevel@tonic-gate 	if (len == 0 || len > CRYPTO_MAX_MECH_NAME) {
8850Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
8860Sstevel@tonic-gate 		goto out;
8870Sstevel@tonic-gate 	}
8880Sstevel@tonic-gate 	mechanism_name = kmem_alloc(len, KM_SLEEP);
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 	if (copyin(STRUCT_FGETP(get_number, pn_mechanism_string),
8910Sstevel@tonic-gate 	    mechanism_name, len) != 0) {
8920Sstevel@tonic-gate 		kmem_free(mechanism_name, len);
8930Sstevel@tonic-gate 		return (EFAULT);
8940Sstevel@tonic-gate 	}
8950Sstevel@tonic-gate 
8962935Skrishna 	/*
8972935Skrishna 	 * Get mechanism number from kcf. We set the load_module
8982935Skrishna 	 * flag to false since we use only hardware providers.
8992935Skrishna 	 */
9002935Skrishna 	number = crypto_mech2id_common(mechanism_name, B_FALSE);
9010Sstevel@tonic-gate 	kmem_free(mechanism_name, len);
9020Sstevel@tonic-gate 	if (number == CRYPTO_MECH_INVALID) {
9030Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
9040Sstevel@tonic-gate 		goto out;
9050Sstevel@tonic-gate 	}
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 	bcopy((char *)&number, (char *)STRUCT_FADDR(get_number,
9080Sstevel@tonic-gate 	    pn_internal_number), sizeof (number));
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	rv = CRYPTO_SUCCESS;
9110Sstevel@tonic-gate out:
9120Sstevel@tonic-gate 	STRUCT_FSET(get_number, pn_return_value, rv);
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(get_number), arg,
9150Sstevel@tonic-gate 	    STRUCT_SIZE(get_number)) != 0) {
9160Sstevel@tonic-gate 		return (EFAULT);
9170Sstevel@tonic-gate 	}
9180Sstevel@tonic-gate 	return (0);
9190Sstevel@tonic-gate }
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate /*
9220Sstevel@tonic-gate  * Side-effects:
9230Sstevel@tonic-gate  *  1. This routine stores provider descriptor pointers in an array
9240Sstevel@tonic-gate  *     and increments each descriptor's reference count.  The array
9250Sstevel@tonic-gate  *     is stored in per-minor number storage.
9260Sstevel@tonic-gate  *  2. Destroys the old array and creates a new one every time
9270Sstevel@tonic-gate  *     this routine is called.
9280Sstevel@tonic-gate  */
9290Sstevel@tonic-gate int
9300Sstevel@tonic-gate crypto_get_provider_list(crypto_minor_t *cm, uint_t *count,
9310Sstevel@tonic-gate     crypto_provider_entry_t **array, boolean_t return_slot_list)
9320Sstevel@tonic-gate {
9330Sstevel@tonic-gate 	kcf_provider_desc_t **provider_array;
9340Sstevel@tonic-gate 	crypto_provider_entry_t *p = NULL;
9350Sstevel@tonic-gate 	uint_t provider_count;
9360Sstevel@tonic-gate 	int rval;
9370Sstevel@tonic-gate 	int i;
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate 	/*
9400Sstevel@tonic-gate 	 * Take snapshot of provider table returning only HW entries
9410Sstevel@tonic-gate 	 * that are in a usable state. Also returns logical provider entries.
9420Sstevel@tonic-gate 	 */
9430Sstevel@tonic-gate 	rval =  kcf_get_slot_list(&provider_count, &provider_array, B_FALSE);
9440Sstevel@tonic-gate 	if (rval != CRYPTO_SUCCESS)
9450Sstevel@tonic-gate 		return (rval);
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 	/* allocate memory before taking cm->cm_lock */
9480Sstevel@tonic-gate 	if (return_slot_list) {
9490Sstevel@tonic-gate 		if (provider_count != 0) {
9500Sstevel@tonic-gate 			p = kmem_alloc(provider_count *
9510Sstevel@tonic-gate 			    sizeof (crypto_provider_entry_t), KM_SLEEP);
9520Sstevel@tonic-gate 			for (i = 0; i < provider_count; i++) {
9530Sstevel@tonic-gate 				p[i].pe_provider_id = i;
9540Sstevel@tonic-gate 				p[i].pe_mechanism_count =
9550Sstevel@tonic-gate 				    provider_array[i]->pd_mech_list_count;
9560Sstevel@tonic-gate 			}
9570Sstevel@tonic-gate 		}
9580Sstevel@tonic-gate 		*array = p;
9590Sstevel@tonic-gate 		*count = provider_count;
9600Sstevel@tonic-gate 	}
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 	/*
9630Sstevel@tonic-gate 	 * Free existing array of providers and replace with new list.
9640Sstevel@tonic-gate 	 */
9650Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
9660Sstevel@tonic-gate 	if (cm->cm_provider_array != NULL) {
9670Sstevel@tonic-gate 		ASSERT(cm->cm_provider_count > 0);
9680Sstevel@tonic-gate 		kcf_free_provider_tab(cm->cm_provider_count,
9690Sstevel@tonic-gate 		    cm->cm_provider_array);
9700Sstevel@tonic-gate 	}
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 	cm->cm_provider_array = provider_array;
9730Sstevel@tonic-gate 	cm->cm_provider_count = provider_count;
9740Sstevel@tonic-gate 	mutex_exit(&cm->cm_lock);
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
9770Sstevel@tonic-gate }
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate /*
9800Sstevel@tonic-gate  * This ioctl returns an array of crypto_provider_entry_t entries.
9810Sstevel@tonic-gate  * This is how consumers learn which hardware providers are available.
9820Sstevel@tonic-gate  */
9830Sstevel@tonic-gate /* ARGSUSED */
9840Sstevel@tonic-gate static int
9850Sstevel@tonic-gate get_provider_list(dev_t dev, caddr_t arg, int mode, int *rval)
9860Sstevel@tonic-gate {
9870Sstevel@tonic-gate 	STRUCT_DECL(crypto_get_provider_list, get_list);
9880Sstevel@tonic-gate 	crypto_provider_entry_t *entries;
9890Sstevel@tonic-gate 	crypto_minor_t *cm;
9900Sstevel@tonic-gate 	size_t copyout_size;
9910Sstevel@tonic-gate 	uint_t req_count;
9920Sstevel@tonic-gate 	uint_t count;
9930Sstevel@tonic-gate 	ulong_t offset;
9940Sstevel@tonic-gate 	int rv;
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 	STRUCT_INIT(get_list, mode);
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
9990Sstevel@tonic-gate 		cmn_err(CE_WARN, "get_provider_list: failed holding minor");
10000Sstevel@tonic-gate 		return (ENXIO);
10010Sstevel@tonic-gate 	}
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(get_list), STRUCT_SIZE(get_list)) != 0) {
10040Sstevel@tonic-gate 		crypto_release_minor(cm);
10050Sstevel@tonic-gate 		return (EFAULT);
10060Sstevel@tonic-gate 	}
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 	rv = crypto_get_provider_list(cm, &count, &entries, RETURN_LIST);
10090Sstevel@tonic-gate 	if (rv != CRYPTO_SUCCESS) {
10100Sstevel@tonic-gate 		crypto_release_minor(cm);
10110Sstevel@tonic-gate 		STRUCT_FSET(get_list, pl_return_value, rv);
10120Sstevel@tonic-gate 		if (copyout(STRUCT_BUF(get_list), arg,
10130Sstevel@tonic-gate 		    STRUCT_SIZE(get_list)) != 0) {
10140Sstevel@tonic-gate 			return (EFAULT);
10150Sstevel@tonic-gate 		}
10160Sstevel@tonic-gate 		return (0);
10170Sstevel@tonic-gate 	}
10180Sstevel@tonic-gate 	crypto_release_minor(cm);
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 	/* Number of slots caller thinks we have */
10210Sstevel@tonic-gate 	req_count = STRUCT_FGET(get_list, pl_count);
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate 	/* Check if only requesting number of slots */
10240Sstevel@tonic-gate 	if (req_count == 0) {
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 		STRUCT_FSET(get_list, pl_count, count);
10270Sstevel@tonic-gate 		STRUCT_FSET(get_list, pl_return_value, CRYPTO_SUCCESS);
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 		crypto_free_provider_list(entries, count);
10300Sstevel@tonic-gate 		if (copyout(STRUCT_BUF(get_list), arg,
10314632Smcpowers 		    STRUCT_SIZE(get_list)) != 0) {
10320Sstevel@tonic-gate 			return (EFAULT);
10330Sstevel@tonic-gate 		}
10340Sstevel@tonic-gate 		return (0);
10350Sstevel@tonic-gate 	}
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 	/* check if buffer is too small */
10380Sstevel@tonic-gate 	req_count = STRUCT_FGET(get_list, pl_count);
10390Sstevel@tonic-gate 	if (count > req_count) {
10400Sstevel@tonic-gate 		STRUCT_FSET(get_list, pl_count, count);
10410Sstevel@tonic-gate 		STRUCT_FSET(get_list, pl_return_value, CRYPTO_BUFFER_TOO_SMALL);
10420Sstevel@tonic-gate 		crypto_free_provider_list(entries, count);
10430Sstevel@tonic-gate 		if (copyout(STRUCT_BUF(get_list), arg,
10440Sstevel@tonic-gate 		    STRUCT_SIZE(get_list)) != 0) {
10450Sstevel@tonic-gate 			return (EFAULT);
10460Sstevel@tonic-gate 		}
10470Sstevel@tonic-gate 		return (0);
10480Sstevel@tonic-gate 	}
10490Sstevel@tonic-gate 
10500Sstevel@tonic-gate 	STRUCT_FSET(get_list, pl_count, count);
10510Sstevel@tonic-gate 	STRUCT_FSET(get_list, pl_return_value, CRYPTO_SUCCESS);
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 	copyout_size = count * sizeof (crypto_provider_entry_t);
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 	/* copyout the first stuff */
10560Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(get_list), arg, STRUCT_SIZE(get_list)) != 0) {
10570Sstevel@tonic-gate 		crypto_free_provider_list(entries, count);
10580Sstevel@tonic-gate 		return (EFAULT);
10590Sstevel@tonic-gate 	}
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate 	if (count == 0) {
10620Sstevel@tonic-gate 		crypto_free_provider_list(entries, count);
10630Sstevel@tonic-gate 		return (0);
10640Sstevel@tonic-gate 	}
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate 	/* copyout entries */
10670Sstevel@tonic-gate 	offset = (ulong_t)STRUCT_FADDR(get_list, pl_list);
10680Sstevel@tonic-gate 	offset -= (ulong_t)STRUCT_BUF(get_list);
10690Sstevel@tonic-gate 	if (copyout(entries, arg + offset, copyout_size) != 0) {
10700Sstevel@tonic-gate 		crypto_free_provider_list(entries, count);
10710Sstevel@tonic-gate 		return (EFAULT);
10720Sstevel@tonic-gate 	}
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 	crypto_free_provider_list(entries, count);
10750Sstevel@tonic-gate 	return (0);
10760Sstevel@tonic-gate }
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate static void
10790Sstevel@tonic-gate ext_to_provider_data(int mode, kcf_provider_desc_t *provider,
10800Sstevel@tonic-gate     crypto_provider_ext_info_t *ei, void *out)
10810Sstevel@tonic-gate {
10820Sstevel@tonic-gate 	STRUCT_DECL(crypto_provider_data, pd);
10830Sstevel@tonic-gate 	STRUCT_DECL(crypto_version, version);
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 	STRUCT_INIT(pd, mode);
10860Sstevel@tonic-gate 	STRUCT_INIT(version, mode);
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 	bcopy(provider->pd_description, STRUCT_FGET(pd, pd_prov_desc),
10890Sstevel@tonic-gate 	    CRYPTO_PROVIDER_DESCR_MAX_LEN);
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 	bcopy(ei->ei_label, STRUCT_FGET(pd, pd_label), CRYPTO_EXT_SIZE_LABEL);
10920Sstevel@tonic-gate 	bcopy(ei->ei_manufacturerID, STRUCT_FGET(pd, pd_manufacturerID),
10930Sstevel@tonic-gate 	    CRYPTO_EXT_SIZE_MANUF);
10940Sstevel@tonic-gate 	bcopy(ei->ei_model, STRUCT_FGET(pd, pd_model), CRYPTO_EXT_SIZE_MODEL);
10950Sstevel@tonic-gate 	bcopy(ei->ei_serial_number, STRUCT_FGET(pd, pd_serial_number),
10960Sstevel@tonic-gate 	    CRYPTO_EXT_SIZE_SERIAL);
10970Sstevel@tonic-gate 	/*
10980Sstevel@tonic-gate 	 * We do not support ioctls for dual-function crypto operations yet.
10990Sstevel@tonic-gate 	 * So, we clear this flag as it might have been set by a provider.
11000Sstevel@tonic-gate 	 */
11010Sstevel@tonic-gate 	ei->ei_flags &= ~CRYPTO_EXTF_DUAL_CRYPTO_OPERATIONS;
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_flags, ei->ei_flags);
11040Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_max_session_count, ei->ei_max_session_count);
11050Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_session_count, (int)CRYPTO_UNAVAILABLE_INFO);
11060Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_max_rw_session_count, ei->ei_max_session_count);
11070Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_rw_session_count, (int)CRYPTO_UNAVAILABLE_INFO);
11080Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_max_pin_len, ei->ei_max_pin_len);
11090Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_min_pin_len, ei->ei_min_pin_len);
11100Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_total_public_memory, ei->ei_total_public_memory);
11110Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_free_public_memory, ei->ei_free_public_memory);
11120Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_total_private_memory, ei->ei_total_private_memory);
11130Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_free_private_memory, ei->ei_free_private_memory);
11140Sstevel@tonic-gate 	STRUCT_FSET(version, cv_major, ei->ei_hardware_version.cv_major);
11150Sstevel@tonic-gate 	STRUCT_FSET(version, cv_minor, ei->ei_hardware_version.cv_minor);
11160Sstevel@tonic-gate 	bcopy(STRUCT_BUF(version), STRUCT_FADDR(pd, pd_hardware_version),
11170Sstevel@tonic-gate 	    STRUCT_SIZE(version));
11180Sstevel@tonic-gate 	bcopy(STRUCT_BUF(version), STRUCT_FADDR(pd, pd_firmware_version),
11190Sstevel@tonic-gate 	    STRUCT_SIZE(version));
11200Sstevel@tonic-gate 	bcopy(ei->ei_time, STRUCT_FGET(pd, pd_time), CRYPTO_EXT_SIZE_TIME);
11210Sstevel@tonic-gate 	bcopy(STRUCT_BUF(pd), out, STRUCT_SIZE(pd));
11220Sstevel@tonic-gate }
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate /*
11250Sstevel@tonic-gate  * Utility routine to construct a crypto_provider_ext_info structure. Some
11260Sstevel@tonic-gate  * of the fields are constructed from information in the provider structure.
11270Sstevel@tonic-gate  * The rest of the fields have default values. We need to do this for
11280Sstevel@tonic-gate  * providers which do not support crypto_provider_management_ops routines.
11290Sstevel@tonic-gate  */
11300Sstevel@tonic-gate static void
11310Sstevel@tonic-gate fabricate_ext_info(kcf_provider_desc_t *provider,
11320Sstevel@tonic-gate     crypto_provider_ext_info_t *ei)
11330Sstevel@tonic-gate {
11340Sstevel@tonic-gate 	/* empty label */
11350Sstevel@tonic-gate 	(void) memset(ei->ei_label, ' ', CRYPTO_EXT_SIZE_LABEL);
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate 	(void) memset(ei->ei_manufacturerID, ' ', CRYPTO_EXT_SIZE_MANUF);
11380Sstevel@tonic-gate 	(void) strncpy((char *)ei->ei_manufacturerID, "Unknown", 7);
11390Sstevel@tonic-gate 
11400Sstevel@tonic-gate 	(void) memset(ei->ei_model, ' ', CRYPTO_EXT_SIZE_MODEL);
11410Sstevel@tonic-gate 	(void) strncpy((char *)ei->ei_model, "Unknown", 7);
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate 	(void) memset(ei->ei_serial_number, ' ', CRYPTO_EXT_SIZE_SERIAL);
11440Sstevel@tonic-gate 	(void) strncpy((char *)ei->ei_serial_number, "Unknown", 7);
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate 	if (KCF_PROV_RANDOM_OPS(provider) != NULL)
11470Sstevel@tonic-gate 		ei->ei_flags |= CRYPTO_EXTF_RNG;
11480Sstevel@tonic-gate 	if (KCF_PROV_DUAL_OPS(provider) != NULL)
11490Sstevel@tonic-gate 		ei->ei_flags |= CRYPTO_EXTF_DUAL_CRYPTO_OPERATIONS;
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate 	ei->ei_max_session_count = CRYPTO_UNAVAILABLE_INFO;
11520Sstevel@tonic-gate 	ei->ei_max_pin_len = 0;
11530Sstevel@tonic-gate 	ei->ei_min_pin_len = 0;
11540Sstevel@tonic-gate 	ei->ei_total_public_memory = CRYPTO_UNAVAILABLE_INFO;
11550Sstevel@tonic-gate 	ei->ei_free_public_memory = CRYPTO_UNAVAILABLE_INFO;
11560Sstevel@tonic-gate 	ei->ei_total_private_memory = CRYPTO_UNAVAILABLE_INFO;
11570Sstevel@tonic-gate 	ei->ei_free_private_memory = CRYPTO_UNAVAILABLE_INFO;
11580Sstevel@tonic-gate 	ei->ei_hardware_version.cv_major = 1;
11590Sstevel@tonic-gate 	ei->ei_hardware_version.cv_minor = 0;
11600Sstevel@tonic-gate 	ei->ei_firmware_version.cv_major = 1;
11610Sstevel@tonic-gate 	ei->ei_firmware_version.cv_minor = 0;
11620Sstevel@tonic-gate }
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate /* ARGSUSED */
11650Sstevel@tonic-gate static int
11660Sstevel@tonic-gate get_provider_info(dev_t dev, caddr_t arg, int mode, int *rval)
11670Sstevel@tonic-gate {
11680Sstevel@tonic-gate 	STRUCT_DECL(crypto_get_provider_info, get_info);
11690Sstevel@tonic-gate 	crypto_minor_t *cm;
11700Sstevel@tonic-gate 	crypto_provider_id_t provider_id;
11710Sstevel@tonic-gate 	kcf_provider_desc_t *provider, *real_provider;
11720Sstevel@tonic-gate 	crypto_provider_ext_info_t *ext_info = NULL;
11730Sstevel@tonic-gate 	size_t need;
11740Sstevel@tonic-gate 	int error = 0;
11750Sstevel@tonic-gate 	int rv;
11760Sstevel@tonic-gate 	kcf_req_params_t params;
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 	STRUCT_INIT(get_info, mode);
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
11810Sstevel@tonic-gate 		cmn_err(CE_WARN, "get_provider_info: failed holding minor");
11820Sstevel@tonic-gate 		return (ENXIO);
11830Sstevel@tonic-gate 	}
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(get_info), STRUCT_SIZE(get_info)) != 0) {
11860Sstevel@tonic-gate 		crypto_release_minor(cm);
11870Sstevel@tonic-gate 		return (EFAULT);
11880Sstevel@tonic-gate 	}
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 	need = sizeof (crypto_provider_ext_info_t);
11913916Skrishna 	if ((rv = crypto_buffer_check(need)) != CRYPTO_SUCCESS) {
11920Sstevel@tonic-gate 		need = 0;
11930Sstevel@tonic-gate 		goto release_minor;
11940Sstevel@tonic-gate 	}
11950Sstevel@tonic-gate 
11960Sstevel@tonic-gate 	/* initialize provider_array */
11970Sstevel@tonic-gate 	if (cm->cm_provider_array == NULL) {
11980Sstevel@tonic-gate 		rv = crypto_get_provider_list(cm, NULL, NULL, DONT_RETURN_LIST);
11990Sstevel@tonic-gate 		if (rv != CRYPTO_SUCCESS) {
12000Sstevel@tonic-gate 			goto release_minor;
12010Sstevel@tonic-gate 		}
12020Sstevel@tonic-gate 	}
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 	ext_info = kmem_zalloc(need, KM_SLEEP);
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate 	provider_id = STRUCT_FGET(get_info, gi_provider_id);
12070Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
12080Sstevel@tonic-gate 	/* index must be less than count of providers */
12090Sstevel@tonic-gate 	if (provider_id >= cm->cm_provider_count) {
12100Sstevel@tonic-gate 		mutex_exit(&cm->cm_lock);
12110Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
12120Sstevel@tonic-gate 		goto release_minor;
12130Sstevel@tonic-gate 	}
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate 	ASSERT(cm->cm_provider_array != NULL);
12160Sstevel@tonic-gate 	provider = cm->cm_provider_array[provider_id];
12170Sstevel@tonic-gate 	KCF_PROV_REFHOLD(provider);
12180Sstevel@tonic-gate 	mutex_exit(&cm->cm_lock);
12190Sstevel@tonic-gate 
12200Sstevel@tonic-gate 	(void) kcf_get_hardware_provider_nomech(
12210Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(provider_ops), CRYPTO_PROVIDER_OFFSET(ext_info),
1222904Smcpowers 	    CHECK_RESTRICT_FALSE, provider, &real_provider);
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate 	if (real_provider != NULL) {
12250Sstevel@tonic-gate 		ASSERT(real_provider == provider ||
12260Sstevel@tonic-gate 		    provider->pd_prov_type == CRYPTO_LOGICAL_PROVIDER);
12270Sstevel@tonic-gate 		KCF_WRAP_PROVMGMT_OPS_PARAMS(&params, KCF_OP_MGMT_EXTINFO,
12280Sstevel@tonic-gate 		    0, NULL, 0, NULL, 0, NULL, ext_info, provider);
12290Sstevel@tonic-gate 		rv = kcf_submit_request(real_provider, NULL, NULL, &params,
12300Sstevel@tonic-gate 		    B_FALSE);
12310Sstevel@tonic-gate 		ASSERT(rv != CRYPTO_NOT_SUPPORTED);
1232904Smcpowers 		KCF_PROV_REFRELE(real_provider);
12330Sstevel@tonic-gate 	} else {
12340Sstevel@tonic-gate 		/* do the best we can */
12350Sstevel@tonic-gate 		fabricate_ext_info(provider, ext_info);
12360Sstevel@tonic-gate 		rv = CRYPTO_SUCCESS;
12370Sstevel@tonic-gate 	}
12380Sstevel@tonic-gate 	KCF_PROV_REFRELE(provider);
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
12410Sstevel@tonic-gate 		ext_to_provider_data(mode, provider, ext_info,
12420Sstevel@tonic-gate 		    STRUCT_FADDR(get_info, gi_provider_data));
12430Sstevel@tonic-gate 	}
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate release_minor:
12466424Skrishna 	CRYPTO_DECREMENT_RCTL(need);
12470Sstevel@tonic-gate 	crypto_release_minor(cm);
12480Sstevel@tonic-gate 
12490Sstevel@tonic-gate 	if (ext_info != NULL)
12500Sstevel@tonic-gate 		kmem_free(ext_info, sizeof (crypto_provider_ext_info_t));
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 	if (error != 0)
12530Sstevel@tonic-gate 		return (error);
12540Sstevel@tonic-gate 
12550Sstevel@tonic-gate 	STRUCT_FSET(get_info, gi_return_value, rv);
12560Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(get_info), arg, STRUCT_SIZE(get_info)) != 0) {
12570Sstevel@tonic-gate 		return (EFAULT);
12580Sstevel@tonic-gate 	}
12590Sstevel@tonic-gate 	return (0);
12600Sstevel@tonic-gate }
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate /*
12630Sstevel@tonic-gate  * This ioctl returns an array of crypto_mech_name_t entries.
12640Sstevel@tonic-gate  * This is how consumers learn which mechanisms are permitted
12650Sstevel@tonic-gate  * by a provider.
12660Sstevel@tonic-gate  */
12670Sstevel@tonic-gate /* ARGSUSED */
12680Sstevel@tonic-gate static int
12690Sstevel@tonic-gate get_provider_mechanisms(dev_t dev, caddr_t arg, int mode, int *rval)
12700Sstevel@tonic-gate {
12710Sstevel@tonic-gate 	STRUCT_DECL(crypto_get_provider_mechanisms, get_mechanisms);
12720Sstevel@tonic-gate 	crypto_mech_name_t *entries;
12730Sstevel@tonic-gate 	crypto_minor_t *cm;
12740Sstevel@tonic-gate 	size_t copyout_size;
12750Sstevel@tonic-gate 	uint_t req_count;
12760Sstevel@tonic-gate 	uint_t count;
12770Sstevel@tonic-gate 	ulong_t offset;
12780Sstevel@tonic-gate 	int err;
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate 	STRUCT_INIT(get_mechanisms, mode);
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
12830Sstevel@tonic-gate 		cmn_err(CE_WARN,
12840Sstevel@tonic-gate 		    "get_provider_mechanisms: failed holding minor");
12850Sstevel@tonic-gate 		return (ENXIO);
12860Sstevel@tonic-gate 	}
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(get_mechanisms),
12890Sstevel@tonic-gate 	    STRUCT_SIZE(get_mechanisms)) != 0) {
12900Sstevel@tonic-gate 		crypto_release_minor(cm);
12910Sstevel@tonic-gate 		return (EFAULT);
12920Sstevel@tonic-gate 	}
12930Sstevel@tonic-gate 
12940Sstevel@tonic-gate 	/* get array of mechanisms from the core module */
12950Sstevel@tonic-gate 	if ((err = crypto_get_provider_mechanisms(cm,
12960Sstevel@tonic-gate 	    STRUCT_FGET(get_mechanisms, pm_provider_id),
12970Sstevel@tonic-gate 	    &count, &entries)) != 0) {
12980Sstevel@tonic-gate 		crypto_release_minor(cm);
12990Sstevel@tonic-gate 		STRUCT_FSET(get_mechanisms, pm_return_value, err);
13000Sstevel@tonic-gate 		if (copyout(STRUCT_BUF(get_mechanisms), arg,
13010Sstevel@tonic-gate 		    STRUCT_SIZE(get_mechanisms)) != 0) {
13020Sstevel@tonic-gate 			return (EFAULT);
13030Sstevel@tonic-gate 		}
13040Sstevel@tonic-gate 		return (0);
13050Sstevel@tonic-gate 	}
13060Sstevel@tonic-gate 	crypto_release_minor(cm);
13070Sstevel@tonic-gate 	/* Number of mechs caller thinks we have */
13080Sstevel@tonic-gate 	req_count = STRUCT_FGET(get_mechanisms, pm_count);
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate 	/* Check if caller is just requesting a count of mechanisms */
13110Sstevel@tonic-gate 	if (req_count == 0) {
13120Sstevel@tonic-gate 		STRUCT_FSET(get_mechanisms, pm_count, count);
13130Sstevel@tonic-gate 		STRUCT_FSET(get_mechanisms, pm_return_value, CRYPTO_SUCCESS);
13140Sstevel@tonic-gate 
13150Sstevel@tonic-gate 		crypto_free_mech_list(entries, count);
13160Sstevel@tonic-gate 		if (copyout(STRUCT_BUF(get_mechanisms), arg,
13170Sstevel@tonic-gate 		    STRUCT_SIZE(get_mechanisms)) != 0) {
13180Sstevel@tonic-gate 			return (EFAULT);
13190Sstevel@tonic-gate 		}
13200Sstevel@tonic-gate 		return (0);
13210Sstevel@tonic-gate 	}
13220Sstevel@tonic-gate 
13230Sstevel@tonic-gate 	/* check if buffer is too small */
13240Sstevel@tonic-gate 	if (count > req_count) {
13250Sstevel@tonic-gate 		STRUCT_FSET(get_mechanisms, pm_count, count);
13260Sstevel@tonic-gate 		STRUCT_FSET(get_mechanisms, pm_return_value,
13270Sstevel@tonic-gate 		    CRYPTO_BUFFER_TOO_SMALL);
13280Sstevel@tonic-gate 		crypto_free_mech_list(entries, count);
13290Sstevel@tonic-gate 		if (copyout(STRUCT_BUF(get_mechanisms), arg,
13300Sstevel@tonic-gate 		    STRUCT_SIZE(get_mechanisms)) != 0) {
13310Sstevel@tonic-gate 			return (EFAULT);
13320Sstevel@tonic-gate 		}
13330Sstevel@tonic-gate 		return (0);
13340Sstevel@tonic-gate 	}
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 	STRUCT_FSET(get_mechanisms, pm_count, count);
13370Sstevel@tonic-gate 	STRUCT_FSET(get_mechanisms, pm_return_value, CRYPTO_SUCCESS);
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 	copyout_size = count * sizeof (crypto_mech_name_t);
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate 	/* copyout the first stuff */
13420Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(get_mechanisms), arg,
13430Sstevel@tonic-gate 	    STRUCT_SIZE(get_mechanisms)) != 0) {
13440Sstevel@tonic-gate 		crypto_free_mech_list(entries, count);
13450Sstevel@tonic-gate 		return (EFAULT);
13460Sstevel@tonic-gate 	}
13470Sstevel@tonic-gate 
13480Sstevel@tonic-gate 	if (count == 0) {
13490Sstevel@tonic-gate 		return (0);
13500Sstevel@tonic-gate 	}
13510Sstevel@tonic-gate 
13520Sstevel@tonic-gate 	/* copyout entries */
13530Sstevel@tonic-gate 	offset = (ulong_t)STRUCT_FADDR(get_mechanisms, pm_list);
13540Sstevel@tonic-gate 	offset -= (ulong_t)STRUCT_BUF(get_mechanisms);
13550Sstevel@tonic-gate 	if (copyout(entries, arg + offset, copyout_size) != 0) {
13560Sstevel@tonic-gate 		crypto_free_mech_list(entries, count);
13570Sstevel@tonic-gate 		return (EFAULT);
13580Sstevel@tonic-gate 	}
13590Sstevel@tonic-gate 
13600Sstevel@tonic-gate 	crypto_free_mech_list(entries, count);
13610Sstevel@tonic-gate 	return (0);
13620Sstevel@tonic-gate }
13630Sstevel@tonic-gate 
13640Sstevel@tonic-gate /*
13650Sstevel@tonic-gate  * This ioctl returns information about a provider's mechanism.
13660Sstevel@tonic-gate  */
13670Sstevel@tonic-gate /* ARGSUSED */
13680Sstevel@tonic-gate static int
13690Sstevel@tonic-gate get_provider_mechanism_info(dev_t dev, caddr_t arg, int mode, int *rval)
13700Sstevel@tonic-gate {
13710Sstevel@tonic-gate 	crypto_get_provider_mechanism_info_t mechanism_info;
13720Sstevel@tonic-gate 	crypto_minor_t *cm;
13730Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
13740Sstevel@tonic-gate 	crypto_mech_info_t *mi = NULL;
13750Sstevel@tonic-gate 	int rv = CRYPTO_SUCCESS;
13760Sstevel@tonic-gate 	int i;
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
13790Sstevel@tonic-gate 		cmn_err(CE_WARN,
13800Sstevel@tonic-gate 		    "get_provider_mechanism_info: failed holding minor");
13810Sstevel@tonic-gate 		return (ENXIO);
13820Sstevel@tonic-gate 	}
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate 	if (copyin(arg, &mechanism_info, sizeof (mechanism_info)) != 0) {
13850Sstevel@tonic-gate 		crypto_release_minor(cm);
13860Sstevel@tonic-gate 		return (EFAULT);
13870Sstevel@tonic-gate 	}
13880Sstevel@tonic-gate 
13890Sstevel@tonic-gate 	/* initialize provider table */
13900Sstevel@tonic-gate 	if (cm->cm_provider_array == NULL) {
13910Sstevel@tonic-gate 		rv = crypto_get_provider_list(cm, NULL, NULL, DONT_RETURN_LIST);
13920Sstevel@tonic-gate 		if (rv != CRYPTO_SUCCESS) {
13930Sstevel@tonic-gate 			mutex_enter(&cm->cm_lock);
13940Sstevel@tonic-gate 			goto fail;
13950Sstevel@tonic-gate 		}
13960Sstevel@tonic-gate 	}
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate 	/*
13990Sstevel@tonic-gate 	 * Provider ID must be less than the count of providers
14000Sstevel@tonic-gate 	 * obtained by calling get_provider_list().
14010Sstevel@tonic-gate 	 */
14020Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
14030Sstevel@tonic-gate 	if (mechanism_info.mi_provider_id >= cm->cm_provider_count) {
14040Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
14050Sstevel@tonic-gate 		goto fail;
14060Sstevel@tonic-gate 	}
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate 	pd = cm->cm_provider_array[mechanism_info.mi_provider_id];
14090Sstevel@tonic-gate 
14100Sstevel@tonic-gate 	for (i = 0; i < pd->pd_mech_list_count; i++) {
14110Sstevel@tonic-gate 		if (strncmp(pd->pd_mechanisms[i].cm_mech_name,
14120Sstevel@tonic-gate 		    mechanism_info.mi_mechanism_name,
14130Sstevel@tonic-gate 		    CRYPTO_MAX_MECH_NAME) == 0) {
14140Sstevel@tonic-gate 			mi = &pd->pd_mechanisms[i];
14150Sstevel@tonic-gate 		}
14160Sstevel@tonic-gate 	}
14170Sstevel@tonic-gate 
14180Sstevel@tonic-gate 	if (mi == NULL) {
14190Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
14200Sstevel@tonic-gate 		goto fail;
14210Sstevel@tonic-gate 	}
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate 	mechanism_info.mi_min_key_size = mi->cm_min_key_length;
14240Sstevel@tonic-gate 	mechanism_info.mi_max_key_size = mi->cm_max_key_length;
14250Sstevel@tonic-gate 	mechanism_info.mi_flags = mi->cm_func_group_mask;
14260Sstevel@tonic-gate 
14270Sstevel@tonic-gate fail:
14280Sstevel@tonic-gate 	mutex_exit(&cm->cm_lock);
14290Sstevel@tonic-gate 	crypto_release_minor(cm);
14300Sstevel@tonic-gate 	mechanism_info.mi_return_value = rv;
14310Sstevel@tonic-gate 	if (copyout(&mechanism_info, arg, sizeof (mechanism_info)) != 0) {
14320Sstevel@tonic-gate 		return (EFAULT);
14330Sstevel@tonic-gate 	}
14340Sstevel@tonic-gate 
14350Sstevel@tonic-gate 	return (0);
14360Sstevel@tonic-gate }
14370Sstevel@tonic-gate 
14380Sstevel@tonic-gate /*
14390Sstevel@tonic-gate  * Every open of /dev/crypto multiplexes all PKCS#11 sessions across
14400Sstevel@tonic-gate  * a single session to each provider. Calls to open and close session
14410Sstevel@tonic-gate  * are not made to providers that do not support sessions. For these
14420Sstevel@tonic-gate  * providers, a session number of 0 is passed during subsequent operations,
14430Sstevel@tonic-gate  * and it is ignored by the provider.
14440Sstevel@tonic-gate  */
14450Sstevel@tonic-gate static int
14460Sstevel@tonic-gate crypto_get_provider_session(crypto_minor_t *cm,
14470Sstevel@tonic-gate     crypto_provider_id_t provider_index, crypto_provider_session_t **output_ps)
14480Sstevel@tonic-gate {
14490Sstevel@tonic-gate 	kcf_provider_desc_t *pd, *real_provider;
14500Sstevel@tonic-gate 	kcf_req_params_t params;
14510Sstevel@tonic-gate 	crypto_provider_session_t *ps, *new_ps;
14520Sstevel@tonic-gate 	crypto_session_id_t provider_session_id = 0;
14530Sstevel@tonic-gate 	int rv;
14540Sstevel@tonic-gate 
14550Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cm->cm_lock));
14560Sstevel@tonic-gate 
14570Sstevel@tonic-gate 	/* pd may be a logical provider */
14580Sstevel@tonic-gate 	pd = cm->cm_provider_array[provider_index];
14590Sstevel@tonic-gate 
14600Sstevel@tonic-gate again:
14610Sstevel@tonic-gate 	/*
14620Sstevel@tonic-gate 	 * Check if there is already a session to the provider.
14630Sstevel@tonic-gate 	 * Sessions may be to a logical provider or a real provider.
14640Sstevel@tonic-gate 	 */
14650Sstevel@tonic-gate 	for (ps = cm->cm_provider_session; ps != NULL; ps = ps->ps_next) {
14660Sstevel@tonic-gate 		if (ps->ps_provider == pd)
14670Sstevel@tonic-gate 			break;
14680Sstevel@tonic-gate 	}
14690Sstevel@tonic-gate 
14700Sstevel@tonic-gate 	/* found existing session */
14710Sstevel@tonic-gate 	if (ps != NULL) {
14720Sstevel@tonic-gate 		ps->ps_refcnt++;
14730Sstevel@tonic-gate 		*output_ps = ps;
14740Sstevel@tonic-gate 		return (CRYPTO_SUCCESS);
14750Sstevel@tonic-gate 	}
14760Sstevel@tonic-gate 	mutex_exit(&cm->cm_lock);
14770Sstevel@tonic-gate 
14780Sstevel@tonic-gate 	/* find a hardware provider that supports session ops */
14790Sstevel@tonic-gate 	(void) kcf_get_hardware_provider_nomech(CRYPTO_OPS_OFFSET(session_ops),
1480904Smcpowers 	    CRYPTO_SESSION_OFFSET(session_open), CHECK_RESTRICT_FALSE,
1481904Smcpowers 	    pd, &real_provider);
14820Sstevel@tonic-gate 
14830Sstevel@tonic-gate 	if (real_provider != NULL) {
14840Sstevel@tonic-gate 		ASSERT(real_provider == pd ||
14850Sstevel@tonic-gate 		    pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER);
14860Sstevel@tonic-gate 		/* open session to provider */
14870Sstevel@tonic-gate 		KCF_WRAP_SESSION_OPS_PARAMS(&params, KCF_OP_SESSION_OPEN,
14880Sstevel@tonic-gate 		    &provider_session_id, 0, CRYPTO_USER, NULL, 0, pd);
14890Sstevel@tonic-gate 		rv = kcf_submit_request(real_provider, NULL, NULL, &params,
14900Sstevel@tonic-gate 		    B_FALSE);
14910Sstevel@tonic-gate 		if (rv != CRYPTO_SUCCESS) {
14920Sstevel@tonic-gate 			mutex_enter(&cm->cm_lock);
1493904Smcpowers 			KCF_PROV_REFRELE(real_provider);
14940Sstevel@tonic-gate 			return (rv);
14950Sstevel@tonic-gate 		}
14960Sstevel@tonic-gate 	}
14970Sstevel@tonic-gate 
14980Sstevel@tonic-gate 	/* allocate crypto_provider_session structure */
14990Sstevel@tonic-gate 	new_ps = kmem_zalloc(sizeof (crypto_provider_session_t), KM_SLEEP);
15000Sstevel@tonic-gate 
15010Sstevel@tonic-gate 	/*
15020Sstevel@tonic-gate 	 * Check if someone opened a session to the provider
15030Sstevel@tonic-gate 	 * while we dropped the lock.
15040Sstevel@tonic-gate 	 */
15050Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
15060Sstevel@tonic-gate 	for (ps = cm->cm_provider_session; ps != NULL; ps = ps->ps_next) {
15070Sstevel@tonic-gate 		if (ps->ps_provider == pd) {
15080Sstevel@tonic-gate 			mutex_exit(&cm->cm_lock);
15090Sstevel@tonic-gate 			kmem_free(new_ps, sizeof (crypto_provider_session_t));
15100Sstevel@tonic-gate 			if (real_provider != NULL) {
15110Sstevel@tonic-gate 				KCF_WRAP_SESSION_OPS_PARAMS(&params,
15120Sstevel@tonic-gate 				    KCF_OP_SESSION_CLOSE, NULL,
15130Sstevel@tonic-gate 				    provider_session_id, CRYPTO_USER, NULL, 0,
15140Sstevel@tonic-gate 				    pd);
15150Sstevel@tonic-gate 				(void) kcf_submit_request(real_provider, NULL,
15160Sstevel@tonic-gate 				    NULL, &params, B_FALSE);
1517904Smcpowers 				KCF_PROV_REFRELE(real_provider);
15180Sstevel@tonic-gate 			}
15190Sstevel@tonic-gate 			mutex_enter(&cm->cm_lock);
15200Sstevel@tonic-gate 			goto again;
15210Sstevel@tonic-gate 
15220Sstevel@tonic-gate 		}
15230Sstevel@tonic-gate 	}
15240Sstevel@tonic-gate 
15250Sstevel@tonic-gate 	/* increment refcnt and attach to crypto_minor structure */
15260Sstevel@tonic-gate 	new_ps->ps_session = provider_session_id;
15270Sstevel@tonic-gate 	new_ps->ps_refcnt = 1;
15280Sstevel@tonic-gate 	KCF_PROV_REFHOLD(pd);
15290Sstevel@tonic-gate 	new_ps->ps_provider = pd;
15300Sstevel@tonic-gate 	if (real_provider != NULL) {
15310Sstevel@tonic-gate 		new_ps->ps_real_provider = real_provider;
15320Sstevel@tonic-gate 	}
15330Sstevel@tonic-gate 	new_ps->ps_next = cm->cm_provider_session;
15340Sstevel@tonic-gate 	cm->cm_provider_session = new_ps;
15350Sstevel@tonic-gate 
15360Sstevel@tonic-gate 	*output_ps = new_ps;
15370Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
15380Sstevel@tonic-gate }
15390Sstevel@tonic-gate 
15400Sstevel@tonic-gate /*
15410Sstevel@tonic-gate  * Release a provider session.
15420Sstevel@tonic-gate  * If the reference count goes to zero, then close the session
15430Sstevel@tonic-gate  * to the provider.
15440Sstevel@tonic-gate  */
15450Sstevel@tonic-gate static void
15460Sstevel@tonic-gate crypto_release_provider_session(crypto_minor_t *cm,
15470Sstevel@tonic-gate     crypto_provider_session_t *provider_session)
15480Sstevel@tonic-gate {
15490Sstevel@tonic-gate 	kcf_req_params_t params;
15500Sstevel@tonic-gate 	crypto_provider_session_t *ps = NULL, **prev;
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cm->cm_lock));
15530Sstevel@tonic-gate 
15540Sstevel@tonic-gate 	/* verify that provider_session is valid */
15550Sstevel@tonic-gate 	for (ps = cm->cm_provider_session, prev = &cm->cm_provider_session;
15560Sstevel@tonic-gate 	    ps != NULL; prev = &ps->ps_next, ps = ps->ps_next) {
15570Sstevel@tonic-gate 		if (ps == provider_session) {
15580Sstevel@tonic-gate 			break;
15590Sstevel@tonic-gate 		}
15600Sstevel@tonic-gate 	}
15610Sstevel@tonic-gate 
15620Sstevel@tonic-gate 	if (ps == NULL)
15630Sstevel@tonic-gate 		return;
15640Sstevel@tonic-gate 
15650Sstevel@tonic-gate 	ps->ps_refcnt--;
15660Sstevel@tonic-gate 
15670Sstevel@tonic-gate 	if (ps->ps_refcnt > 0)
15680Sstevel@tonic-gate 		return;
15690Sstevel@tonic-gate 
15700Sstevel@tonic-gate 	if (ps->ps_real_provider != NULL) {
15710Sstevel@tonic-gate 		/* close session with provider */
15720Sstevel@tonic-gate 		KCF_WRAP_SESSION_OPS_PARAMS(&params, KCF_OP_SESSION_CLOSE, NULL,
15730Sstevel@tonic-gate 		    ps->ps_session, CRYPTO_USER, NULL, 0, ps->ps_provider);
15740Sstevel@tonic-gate 		(void) kcf_submit_request(ps->ps_real_provider,
15750Sstevel@tonic-gate 		    NULL, NULL, &params, B_FALSE);
15760Sstevel@tonic-gate 		KCF_PROV_REFRELE(ps->ps_real_provider);
15770Sstevel@tonic-gate 	}
15780Sstevel@tonic-gate 	KCF_PROV_REFRELE(ps->ps_provider);
15790Sstevel@tonic-gate 	*prev = ps->ps_next;
15800Sstevel@tonic-gate 	kmem_free(ps, sizeof (*ps));
15810Sstevel@tonic-gate }
15820Sstevel@tonic-gate 
15830Sstevel@tonic-gate static int
15840Sstevel@tonic-gate grow_session_table(crypto_minor_t *cm)
15850Sstevel@tonic-gate {
15860Sstevel@tonic-gate 	crypto_session_data_t **session_table;
15870Sstevel@tonic-gate 	crypto_session_data_t **new;
15880Sstevel@tonic-gate 	uint_t session_table_count;
15890Sstevel@tonic-gate 	uint_t need;
15900Sstevel@tonic-gate 	size_t current_allocation;
15910Sstevel@tonic-gate 	size_t new_allocation;
15923916Skrishna 	int rv;
15930Sstevel@tonic-gate 
15940Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cm->cm_lock));
15950Sstevel@tonic-gate 
15960Sstevel@tonic-gate 	session_table_count = cm->cm_session_table_count;
15970Sstevel@tonic-gate 	session_table = cm->cm_session_table;
15980Sstevel@tonic-gate 	need = session_table_count + CRYPTO_SESSION_CHUNK;
15990Sstevel@tonic-gate 
16000Sstevel@tonic-gate 	current_allocation = session_table_count * sizeof (void *);
16010Sstevel@tonic-gate 	new_allocation = need * sizeof (void *);
16020Sstevel@tonic-gate 
16030Sstevel@tonic-gate 	/*
16040Sstevel@tonic-gate 	 * Memory needed to grow the session table is checked
16050Sstevel@tonic-gate 	 * against the project.max-crypto-memory resource control.
16060Sstevel@tonic-gate 	 */
16073916Skrishna 	if ((rv = crypto_buffer_check(new_allocation - current_allocation)) !=
16083916Skrishna 	    CRYPTO_SUCCESS) {
16093916Skrishna 		return (rv);
16103916Skrishna 	}
16110Sstevel@tonic-gate 
16120Sstevel@tonic-gate 	/* drop lock while we allocate memory */
16130Sstevel@tonic-gate 	mutex_exit(&cm->cm_lock);
16140Sstevel@tonic-gate 	new = kmem_zalloc(new_allocation, KM_SLEEP);
16150Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
16160Sstevel@tonic-gate 
16170Sstevel@tonic-gate 	/* check if another thread increased the table size */
16180Sstevel@tonic-gate 	if (session_table_count != cm->cm_session_table_count) {
16190Sstevel@tonic-gate 		kmem_free(new, new_allocation);
16200Sstevel@tonic-gate 		return (CRYPTO_SUCCESS);
16210Sstevel@tonic-gate 	}
16220Sstevel@tonic-gate 
16230Sstevel@tonic-gate 	bcopy(session_table, new, current_allocation);
16240Sstevel@tonic-gate 	kmem_free(session_table, current_allocation);
16250Sstevel@tonic-gate 	cm->cm_session_table = new;
16260Sstevel@tonic-gate 	cm->cm_session_table_count += CRYPTO_SESSION_CHUNK;
16270Sstevel@tonic-gate 
16280Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
16290Sstevel@tonic-gate }
16300Sstevel@tonic-gate 
16310Sstevel@tonic-gate /*
16320Sstevel@tonic-gate  * Find unused entry in session table and return it's index.
16330Sstevel@tonic-gate  * Initialize session table entry.
16340Sstevel@tonic-gate  */
16350Sstevel@tonic-gate /* ARGSUSED */
16360Sstevel@tonic-gate static int
16370Sstevel@tonic-gate crypto_open_session(dev_t dev, uint_t flags, crypto_session_id_t *session_index,
16380Sstevel@tonic-gate     crypto_provider_id_t provider_id)
16390Sstevel@tonic-gate {
16400Sstevel@tonic-gate 	crypto_session_data_t **session_table;
16410Sstevel@tonic-gate 	crypto_session_data_t *sp;
16420Sstevel@tonic-gate 	crypto_minor_t *cm;
16430Sstevel@tonic-gate 	uint_t session_table_count;
16440Sstevel@tonic-gate 	uint_t i;
16450Sstevel@tonic-gate 	int rv;
16460Sstevel@tonic-gate 	crypto_provider_session_t *ps;
16470Sstevel@tonic-gate 	kcf_provider_desc_t *provider;
16480Sstevel@tonic-gate 
16490Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
16500Sstevel@tonic-gate 		cmn_err(CE_WARN, "crypto_open_session: failed holding minor");
16510Sstevel@tonic-gate 		return (CRYPTO_FAILED);
16520Sstevel@tonic-gate 	}
16530Sstevel@tonic-gate 
16540Sstevel@tonic-gate 	/* initialize provider_array */
16550Sstevel@tonic-gate 	if (cm->cm_provider_array == NULL) {
16560Sstevel@tonic-gate 		rv = crypto_get_provider_list(cm, NULL, NULL, DONT_RETURN_LIST);
16570Sstevel@tonic-gate 		if (rv != 0) {
16580Sstevel@tonic-gate 			crypto_release_minor(cm);
16590Sstevel@tonic-gate 			return (rv);
16600Sstevel@tonic-gate 		}
16610Sstevel@tonic-gate 	}
16620Sstevel@tonic-gate 
16630Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
16640Sstevel@tonic-gate 	/* index must be less than count of providers */
16650Sstevel@tonic-gate 	if (provider_id >= cm->cm_provider_count) {
16660Sstevel@tonic-gate 		mutex_exit(&cm->cm_lock);
16670Sstevel@tonic-gate 		crypto_release_minor(cm);
16680Sstevel@tonic-gate 		return (CRYPTO_INVALID_PROVIDER_ID);
16690Sstevel@tonic-gate 	}
16700Sstevel@tonic-gate 	ASSERT(cm->cm_provider_array != NULL);
16710Sstevel@tonic-gate 
16720Sstevel@tonic-gate 	rv = crypto_get_provider_session(cm, provider_id, &ps);
16730Sstevel@tonic-gate 	if (rv != CRYPTO_SUCCESS) {
16740Sstevel@tonic-gate 		mutex_exit(&cm->cm_lock);
16750Sstevel@tonic-gate 		crypto_release_minor(cm);
16760Sstevel@tonic-gate 		return (rv);
16770Sstevel@tonic-gate 	}
16780Sstevel@tonic-gate 	provider = cm->cm_provider_array[provider_id];
16790Sstevel@tonic-gate 
16800Sstevel@tonic-gate again:
16810Sstevel@tonic-gate 	session_table_count = cm->cm_session_table_count;
16820Sstevel@tonic-gate 	session_table = cm->cm_session_table;
16830Sstevel@tonic-gate 
16840Sstevel@tonic-gate 	/* session handles start with 1 */
16850Sstevel@tonic-gate 	for (i = 1; i < session_table_count; i++) {
16860Sstevel@tonic-gate 		if (session_table[i] == NULL)
16870Sstevel@tonic-gate 			break;
16880Sstevel@tonic-gate 	}
16890Sstevel@tonic-gate 
16900Sstevel@tonic-gate 	if (i == session_table_count || session_table_count == 0) {
16910Sstevel@tonic-gate 		if ((rv = grow_session_table(cm)) != CRYPTO_SUCCESS) {
16920Sstevel@tonic-gate 			crypto_release_provider_session(cm, ps);
16930Sstevel@tonic-gate 			mutex_exit(&cm->cm_lock);
16940Sstevel@tonic-gate 			crypto_release_minor(cm);
16950Sstevel@tonic-gate 			return (rv);
16960Sstevel@tonic-gate 		}
16970Sstevel@tonic-gate 		goto again;
16980Sstevel@tonic-gate 	}
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate 	sp = kmem_cache_alloc(crypto_session_cache, KM_SLEEP);
17010Sstevel@tonic-gate 	sp->sd_flags = 0;
17020Sstevel@tonic-gate 	sp->sd_find_init_cookie = NULL;
17030Sstevel@tonic-gate 	sp->sd_digest_ctx = NULL;
17040Sstevel@tonic-gate 	sp->sd_encr_ctx = NULL;
17050Sstevel@tonic-gate 	sp->sd_decr_ctx = NULL;
17060Sstevel@tonic-gate 	sp->sd_sign_ctx = NULL;
17070Sstevel@tonic-gate 	sp->sd_verify_ctx = NULL;
17080Sstevel@tonic-gate 	sp->sd_sign_recover_ctx = NULL;
17090Sstevel@tonic-gate 	sp->sd_verify_recover_ctx = NULL;
17100Sstevel@tonic-gate 	mutex_init(&sp->sd_lock, NULL, MUTEX_DRIVER, NULL);
17110Sstevel@tonic-gate 	cv_init(&sp->sd_cv, NULL, CV_DRIVER, NULL);
17120Sstevel@tonic-gate 	KCF_PROV_REFHOLD(provider);
17130Sstevel@tonic-gate 	sp->sd_provider = provider;
17140Sstevel@tonic-gate 	sp->sd_provider_session = ps;
17156424Skrishna 
17166424Skrishna 	/* See the comment for CRYPTO_PRE_APPROVED_LIMIT. */
17176424Skrishna 	if ((rv = crypto_buffer_check(crypto_pre_approved_limit)) !=
17186424Skrishna 	    CRYPTO_SUCCESS) {
17196424Skrishna 		sp->sd_pre_approved_amount = 0;
17206424Skrishna 	} else {
17216424Skrishna 		sp->sd_pre_approved_amount = crypto_pre_approved_limit;
17226424Skrishna 	}
17236424Skrishna 
17240Sstevel@tonic-gate 	cm->cm_session_table[i] = sp;
17250Sstevel@tonic-gate 	mutex_exit(&cm->cm_lock);
17260Sstevel@tonic-gate 	crypto_release_minor(cm);
17270Sstevel@tonic-gate 	*session_index = i;
17280Sstevel@tonic-gate 
17290Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
17300Sstevel@tonic-gate }
17310Sstevel@tonic-gate 
17320Sstevel@tonic-gate /*
17330Sstevel@tonic-gate  * Close a session.
17340Sstevel@tonic-gate  */
17350Sstevel@tonic-gate static int
17360Sstevel@tonic-gate crypto_close_session(dev_t dev, crypto_session_id_t session_index)
17370Sstevel@tonic-gate {
17380Sstevel@tonic-gate 	crypto_session_data_t **session_table;
17390Sstevel@tonic-gate 	crypto_session_data_t *sp;
17400Sstevel@tonic-gate 	crypto_minor_t *cm;
17410Sstevel@tonic-gate 
17420Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
17430Sstevel@tonic-gate 		cmn_err(CE_WARN, "crypto_close_session: failed holding minor");
17440Sstevel@tonic-gate 		return (CRYPTO_FAILED);
17450Sstevel@tonic-gate 	}
17460Sstevel@tonic-gate 
17470Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
17480Sstevel@tonic-gate 	session_table = cm->cm_session_table;
17490Sstevel@tonic-gate 
17500Sstevel@tonic-gate 	if ((session_index) == 0 ||
17510Sstevel@tonic-gate 	    (session_index >= cm->cm_session_table_count)) {
17520Sstevel@tonic-gate 		mutex_exit(&cm->cm_lock);
17530Sstevel@tonic-gate 		crypto_release_minor(cm);
17540Sstevel@tonic-gate 		return (CRYPTO_SESSION_HANDLE_INVALID);
17550Sstevel@tonic-gate 	}
17560Sstevel@tonic-gate 
17570Sstevel@tonic-gate 	sp = session_table[session_index];
17580Sstevel@tonic-gate 	if (sp == NULL) {
17590Sstevel@tonic-gate 		mutex_exit(&cm->cm_lock);
17600Sstevel@tonic-gate 		crypto_release_minor(cm);
17610Sstevel@tonic-gate 		return (CRYPTO_SESSION_HANDLE_INVALID);
17620Sstevel@tonic-gate 	}
17630Sstevel@tonic-gate 	/*
17640Sstevel@tonic-gate 	 * If session is in use, free it when the thread
17650Sstevel@tonic-gate 	 * finishes with the session.
17660Sstevel@tonic-gate 	 */
17670Sstevel@tonic-gate 	mutex_enter(&sp->sd_lock);
17680Sstevel@tonic-gate 	if (sp->sd_flags & CRYPTO_SESSION_IS_BUSY) {
17690Sstevel@tonic-gate 		sp->sd_flags |= CRYPTO_SESSION_IS_CLOSED;
17700Sstevel@tonic-gate 		mutex_exit(&sp->sd_lock);
17710Sstevel@tonic-gate 	} else {
17726424Skrishna 		ASSERT(sp->sd_pre_approved_amount == 0 ||
17736424Skrishna 		    sp->sd_pre_approved_amount == crypto_pre_approved_limit);
17746424Skrishna 		CRYPTO_DECREMENT_RCTL(sp->sd_pre_approved_amount);
17756424Skrishna 
17760Sstevel@tonic-gate 		if (sp->sd_find_init_cookie != NULL) {
17770Sstevel@tonic-gate 			(void) crypto_free_find_ctx(sp);
17780Sstevel@tonic-gate 		}
17790Sstevel@tonic-gate 
17800Sstevel@tonic-gate 		crypto_release_provider_session(cm, sp->sd_provider_session);
17810Sstevel@tonic-gate 		KCF_PROV_REFRELE(sp->sd_provider);
17820Sstevel@tonic-gate 		CRYPTO_CANCEL_ALL_CTX(sp);
17830Sstevel@tonic-gate 		mutex_destroy(&sp->sd_lock);
17840Sstevel@tonic-gate 		cv_destroy(&sp->sd_cv);
17850Sstevel@tonic-gate 		kmem_cache_free(crypto_session_cache, sp);
17860Sstevel@tonic-gate 		session_table[session_index] = NULL;
17870Sstevel@tonic-gate 	}
17880Sstevel@tonic-gate 
17890Sstevel@tonic-gate 	mutex_exit(&cm->cm_lock);
17900Sstevel@tonic-gate 	crypto_release_minor(cm);
17910Sstevel@tonic-gate 
17920Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
17930Sstevel@tonic-gate }
17940Sstevel@tonic-gate 
17950Sstevel@tonic-gate /*
17960Sstevel@tonic-gate  * This ioctl opens a session and returns the session ID in os_session.
17970Sstevel@tonic-gate  */
17980Sstevel@tonic-gate /* ARGSUSED */
17990Sstevel@tonic-gate static int
18000Sstevel@tonic-gate open_session(dev_t dev, caddr_t arg, int mode, int *rval)
18010Sstevel@tonic-gate {
18020Sstevel@tonic-gate 	crypto_open_session_t open_session;
18030Sstevel@tonic-gate 	crypto_session_id_t session;
18040Sstevel@tonic-gate 	int rv;
18050Sstevel@tonic-gate 
18060Sstevel@tonic-gate 	if (copyin(arg, &open_session, sizeof (open_session)) != 0)
18070Sstevel@tonic-gate 		return (EFAULT);
18080Sstevel@tonic-gate 
18090Sstevel@tonic-gate 	rv = crypto_open_session(dev, open_session.os_flags,
18100Sstevel@tonic-gate 	    &session, open_session.os_provider_id);
18110Sstevel@tonic-gate 	if (rv != CRYPTO_SUCCESS) {
18120Sstevel@tonic-gate 		open_session.os_return_value = rv;
18130Sstevel@tonic-gate 		if (copyout(&open_session, arg, sizeof (open_session)) != 0) {
18140Sstevel@tonic-gate 			return (EFAULT);
18150Sstevel@tonic-gate 		}
18160Sstevel@tonic-gate 		return (0);
18170Sstevel@tonic-gate 	}
18180Sstevel@tonic-gate 
18190Sstevel@tonic-gate 	open_session.os_session = session;
18200Sstevel@tonic-gate 	open_session.os_return_value = CRYPTO_SUCCESS;
18210Sstevel@tonic-gate 
18220Sstevel@tonic-gate 	if (copyout(&open_session, arg, sizeof (open_session)) != 0) {
18230Sstevel@tonic-gate 		return (EFAULT);
18240Sstevel@tonic-gate 	}
18250Sstevel@tonic-gate 	return (0);
18260Sstevel@tonic-gate }
18270Sstevel@tonic-gate 
18280Sstevel@tonic-gate /*
18290Sstevel@tonic-gate  * This ioctl closes a session.
18300Sstevel@tonic-gate  */
18310Sstevel@tonic-gate /* ARGSUSED */
18320Sstevel@tonic-gate static int
18330Sstevel@tonic-gate close_session(dev_t dev, caddr_t arg, int mode, int *rval)
18340Sstevel@tonic-gate {
18350Sstevel@tonic-gate 	crypto_close_session_t close_session;
18360Sstevel@tonic-gate 	int rv;
18370Sstevel@tonic-gate 
18380Sstevel@tonic-gate 	if (copyin(arg, &close_session, sizeof (close_session)) != 0)
18390Sstevel@tonic-gate 		return (EFAULT);
18400Sstevel@tonic-gate 
18410Sstevel@tonic-gate 	rv = crypto_close_session(dev, close_session.cs_session);
1842904Smcpowers 	close_session.cs_return_value = rv;
18430Sstevel@tonic-gate 	if (copyout(&close_session, arg, sizeof (close_session)) != 0) {
18440Sstevel@tonic-gate 		return (EFAULT);
18450Sstevel@tonic-gate 	}
18460Sstevel@tonic-gate 	return (0);
18470Sstevel@tonic-gate }
18480Sstevel@tonic-gate 
18490Sstevel@tonic-gate /*
18500Sstevel@tonic-gate  * Copy data model dependent mechanism structure into a kernel mechanism
18510Sstevel@tonic-gate  * structure.  Allocate param storage if necessary.
18520Sstevel@tonic-gate  */
18530Sstevel@tonic-gate static boolean_t
18546424Skrishna copyin_mech(int mode, crypto_session_data_t *sp, crypto_mechanism_t *in_mech,
18556424Skrishna     crypto_mechanism_t *out_mech, size_t *out_rctl_bytes,
18566424Skrishna     boolean_t *out_rctl_chk, int *out_rv, int *out_error)
18570Sstevel@tonic-gate {
18580Sstevel@tonic-gate 	STRUCT_DECL(crypto_mechanism, mech);
18590Sstevel@tonic-gate 	caddr_t param;
18600Sstevel@tonic-gate 	size_t param_len;
18616424Skrishna 	size_t rctl_bytes = 0;
18620Sstevel@tonic-gate 	int error = 0;
18630Sstevel@tonic-gate 	int rv = 0;
18640Sstevel@tonic-gate 
18650Sstevel@tonic-gate 	STRUCT_INIT(mech, mode);
18660Sstevel@tonic-gate 	bcopy(in_mech, STRUCT_BUF(mech), STRUCT_SIZE(mech));
18670Sstevel@tonic-gate 	param = STRUCT_FGETP(mech, cm_param);
18680Sstevel@tonic-gate 	param_len = STRUCT_FGET(mech, cm_param_len);
18690Sstevel@tonic-gate 	out_mech->cm_type = STRUCT_FGET(mech, cm_type);
18700Sstevel@tonic-gate 	out_mech->cm_param = NULL;
18710Sstevel@tonic-gate 	out_mech->cm_param_len = 0;
18720Sstevel@tonic-gate 	if (param != NULL && param_len != 0) {
18730Sstevel@tonic-gate 		if (param_len > crypto_max_buffer_len) {
18740Sstevel@tonic-gate 			cmn_err(CE_NOTE, "copyin_mech: buffer greater than "
18750Sstevel@tonic-gate 			    "%ld bytes, pid = %d", crypto_max_buffer_len,
18760Sstevel@tonic-gate 			    curproc->p_pid);
18770Sstevel@tonic-gate 			rv = CRYPTO_ARGUMENTS_BAD;
18780Sstevel@tonic-gate 			goto out;
18790Sstevel@tonic-gate 		}
18800Sstevel@tonic-gate 
18816424Skrishna 		rv = CRYPTO_BUFFER_CHECK(sp, param_len, *out_rctl_chk);
18826424Skrishna 		if (rv != CRYPTO_SUCCESS) {
18836424Skrishna 			goto out;
18840Sstevel@tonic-gate 		}
18856424Skrishna 		rctl_bytes = param_len;
18866424Skrishna 
18870Sstevel@tonic-gate 		out_mech->cm_param = kmem_alloc(param_len, KM_SLEEP);
18880Sstevel@tonic-gate 		if (copyin((char *)param, out_mech->cm_param, param_len) != 0) {
18890Sstevel@tonic-gate 			kmem_free(out_mech->cm_param, param_len);
18900Sstevel@tonic-gate 			out_mech->cm_param = NULL;
18910Sstevel@tonic-gate 			error = EFAULT;
18920Sstevel@tonic-gate 			goto out;
18930Sstevel@tonic-gate 		}
18940Sstevel@tonic-gate 		out_mech->cm_param_len = param_len;
18950Sstevel@tonic-gate 	}
18960Sstevel@tonic-gate out:
18970Sstevel@tonic-gate 	*out_rctl_bytes = rctl_bytes;
18980Sstevel@tonic-gate 	*out_rv = rv;
18990Sstevel@tonic-gate 	*out_error = error;
19000Sstevel@tonic-gate 	return ((rv | error) ? B_FALSE : B_TRUE);
19010Sstevel@tonic-gate }
19020Sstevel@tonic-gate 
19030Sstevel@tonic-gate /*
19040Sstevel@tonic-gate  * Free key attributes when key type is CRYPTO_KEY_ATTR_LIST.
19050Sstevel@tonic-gate  * The crypto_key structure is not freed.
19060Sstevel@tonic-gate  */
19070Sstevel@tonic-gate static void
19080Sstevel@tonic-gate crypto_free_key_attributes(crypto_key_t *key)
19090Sstevel@tonic-gate {
19100Sstevel@tonic-gate 	crypto_object_attribute_t *attrs;
19110Sstevel@tonic-gate 	size_t len = 0;
19120Sstevel@tonic-gate 	int i;
19130Sstevel@tonic-gate 
19140Sstevel@tonic-gate 	ASSERT(key->ck_format == CRYPTO_KEY_ATTR_LIST);
19150Sstevel@tonic-gate 	if (key->ck_count == 0 || key->ck_attrs == NULL)
19160Sstevel@tonic-gate 		return;
19170Sstevel@tonic-gate 
19180Sstevel@tonic-gate 	/* compute the size of the container */
19190Sstevel@tonic-gate 	len = key->ck_count * sizeof (crypto_object_attribute_t);
19200Sstevel@tonic-gate 
19210Sstevel@tonic-gate 	/* total up the size of all attributes in the container */
19220Sstevel@tonic-gate 	for (i = 0; i < key->ck_count; i++) {
19230Sstevel@tonic-gate 		attrs = &key->ck_attrs[i];
19240Sstevel@tonic-gate 		if (attrs->oa_value_len != 0 &&
19250Sstevel@tonic-gate 		    attrs->oa_value != NULL) {
19260Sstevel@tonic-gate 			len += roundup(attrs->oa_value_len, sizeof (caddr_t));
19270Sstevel@tonic-gate 		}
19280Sstevel@tonic-gate 	}
19290Sstevel@tonic-gate 
19300Sstevel@tonic-gate 	bzero(key->ck_attrs, len);
19310Sstevel@tonic-gate 	kmem_free(key->ck_attrs, len);
19320Sstevel@tonic-gate }
19330Sstevel@tonic-gate 
19340Sstevel@tonic-gate /*
19350Sstevel@tonic-gate  * Frees allocated storage in the key structure, but doesn't free
19360Sstevel@tonic-gate  * the key structure.
19370Sstevel@tonic-gate  */
19380Sstevel@tonic-gate static void
19390Sstevel@tonic-gate free_crypto_key(crypto_key_t *key)
19400Sstevel@tonic-gate {
19410Sstevel@tonic-gate 	switch (key->ck_format) {
19420Sstevel@tonic-gate 	case CRYPTO_KEY_RAW: {
19430Sstevel@tonic-gate 		size_t len;
19440Sstevel@tonic-gate 
19450Sstevel@tonic-gate 		if (key->ck_length == 0 || key->ck_data == NULL)
19460Sstevel@tonic-gate 			break;
19470Sstevel@tonic-gate 
19480Sstevel@tonic-gate 		len = CRYPTO_BITS2BYTES(key->ck_length);
19490Sstevel@tonic-gate 		bzero(key->ck_data, len);
19500Sstevel@tonic-gate 		kmem_free(key->ck_data, len);
19510Sstevel@tonic-gate 		break;
19520Sstevel@tonic-gate 	}
19530Sstevel@tonic-gate 
19540Sstevel@tonic-gate 	case CRYPTO_KEY_ATTR_LIST:
19550Sstevel@tonic-gate 		crypto_free_key_attributes(key);
19560Sstevel@tonic-gate 		break;
19570Sstevel@tonic-gate 
19580Sstevel@tonic-gate 	default:
19590Sstevel@tonic-gate 		break;
19600Sstevel@tonic-gate 	}
19610Sstevel@tonic-gate }
19620Sstevel@tonic-gate 
19630Sstevel@tonic-gate /*
19640Sstevel@tonic-gate  * Copy in an array of crypto_object_attribute structures from user-space.
19650Sstevel@tonic-gate  * Kernel memory is allocated for the array and the value of each attribute
19660Sstevel@tonic-gate  * in the array.  Since unprivileged users can specify the size of attributes,
19670Sstevel@tonic-gate  * the amount of memory needed is charged against the
19680Sstevel@tonic-gate  * project.max-crypto-memory resource control.
19690Sstevel@tonic-gate  *
19700Sstevel@tonic-gate  * Attribute values are copied in from user-space if copyin_value is set to
19710Sstevel@tonic-gate  * B_TRUE.  This routine returns B_TRUE if the copyin was successful.
19720Sstevel@tonic-gate  */
19730Sstevel@tonic-gate static boolean_t
19746424Skrishna copyin_attributes(int mode, crypto_session_data_t *sp,
19756424Skrishna     uint_t count, caddr_t oc_attributes,
19760Sstevel@tonic-gate     crypto_object_attribute_t **k_attrs_out, size_t *k_attrs_size_out,
19770Sstevel@tonic-gate     caddr_t *u_attrs_out, int *out_rv, int *out_error, size_t *out_rctl_bytes,
19786424Skrishna     boolean_t *out_rctl_chk, boolean_t copyin_value)
19790Sstevel@tonic-gate {
19800Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_attribute, oa);
19810Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
19820Sstevel@tonic-gate 	caddr_t attrs = NULL, ap, p, value;
19830Sstevel@tonic-gate 	caddr_t k_attrs_buf;
19840Sstevel@tonic-gate 	size_t k_attrs_len;
19850Sstevel@tonic-gate 	size_t k_attrs_buf_len = 0;
19860Sstevel@tonic-gate 	size_t k_attrs_total_len = 0;
19870Sstevel@tonic-gate 	size_t tmp_len;
19880Sstevel@tonic-gate 	size_t rctl_bytes = 0;
19890Sstevel@tonic-gate 	size_t len = 0;
19900Sstevel@tonic-gate 	size_t value_len;
19910Sstevel@tonic-gate 	int error = 0;
19920Sstevel@tonic-gate 	int rv = 0;
19930Sstevel@tonic-gate 	int i;
19940Sstevel@tonic-gate 
19950Sstevel@tonic-gate 	STRUCT_INIT(oa, mode);
19960Sstevel@tonic-gate 
19970Sstevel@tonic-gate 	if (count == 0) {
19980Sstevel@tonic-gate 		rv = CRYPTO_SUCCESS;
19990Sstevel@tonic-gate 		goto out;
20000Sstevel@tonic-gate 	}
20010Sstevel@tonic-gate 
20020Sstevel@tonic-gate 	if (count > CRYPTO_MAX_ATTRIBUTE_COUNT) {
20030Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
20040Sstevel@tonic-gate 		goto out;
20050Sstevel@tonic-gate 	}
20060Sstevel@tonic-gate 
20070Sstevel@tonic-gate 	/* compute size of crypto_object_attribute array */
20080Sstevel@tonic-gate 	len = count * STRUCT_SIZE(oa);
20090Sstevel@tonic-gate 
20100Sstevel@tonic-gate 	/* this allocation is not charged against the user's resource limit */
20110Sstevel@tonic-gate 	attrs = kmem_alloc(len, KM_SLEEP);
20120Sstevel@tonic-gate 	if (copyin(oc_attributes, attrs, len) != 0) {
20130Sstevel@tonic-gate 		error = EFAULT;
20140Sstevel@tonic-gate 		goto out;
20150Sstevel@tonic-gate 	}
20160Sstevel@tonic-gate 
20170Sstevel@tonic-gate 	/* figure out how much memory to allocate for all of the attributes */
20180Sstevel@tonic-gate 	ap = attrs;
20190Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
20200Sstevel@tonic-gate 		bcopy(ap, STRUCT_BUF(oa), STRUCT_SIZE(oa));
20210Sstevel@tonic-gate 		tmp_len = roundup(STRUCT_FGET(oa, oa_value_len),
20220Sstevel@tonic-gate 		    sizeof (caddr_t));
20230Sstevel@tonic-gate 		if (tmp_len > crypto_max_buffer_len) {
20240Sstevel@tonic-gate 			cmn_err(CE_NOTE, "copyin_attributes: buffer greater "
20250Sstevel@tonic-gate 			    "than %ld bytes, pid = %d", crypto_max_buffer_len,
20260Sstevel@tonic-gate 			    curproc->p_pid);
20270Sstevel@tonic-gate 			rv = CRYPTO_ARGUMENTS_BAD;
20280Sstevel@tonic-gate 			goto out;
20290Sstevel@tonic-gate 		}
20300Sstevel@tonic-gate 		if (STRUCT_FGETP(oa, oa_value) != NULL)
20310Sstevel@tonic-gate 			k_attrs_buf_len += tmp_len;
20320Sstevel@tonic-gate 		ap += STRUCT_SIZE(oa);
20330Sstevel@tonic-gate 	}
20340Sstevel@tonic-gate 
20350Sstevel@tonic-gate 	k_attrs_len = count * sizeof (crypto_object_attribute_t);
20360Sstevel@tonic-gate 	k_attrs_total_len = k_attrs_buf_len + k_attrs_len;
20376424Skrishna 
20386424Skrishna 	rv = CRYPTO_BUFFER_CHECK(sp, k_attrs_total_len, *out_rctl_chk);
20396424Skrishna 	if (rv != CRYPTO_SUCCESS) {
20406424Skrishna 		goto out;
20416424Skrishna 	}
20426424Skrishna 	rctl_bytes = k_attrs_total_len;
20430Sstevel@tonic-gate 
20440Sstevel@tonic-gate 	/* one big allocation for everything */
20450Sstevel@tonic-gate 	k_attrs = kmem_alloc(k_attrs_total_len, KM_SLEEP);
20460Sstevel@tonic-gate 	k_attrs_buf = (char *)k_attrs + k_attrs_len;
20470Sstevel@tonic-gate 
20480Sstevel@tonic-gate 	ap = attrs;
20490Sstevel@tonic-gate 	p = k_attrs_buf;
20500Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
20510Sstevel@tonic-gate 		bcopy(ap, STRUCT_BUF(oa), STRUCT_SIZE(oa));
20520Sstevel@tonic-gate 		k_attrs[i].oa_type = STRUCT_FGET(oa, oa_type);
20530Sstevel@tonic-gate 		value = STRUCT_FGETP(oa, oa_value);
20540Sstevel@tonic-gate 		value_len = STRUCT_FGET(oa, oa_value_len);
20550Sstevel@tonic-gate 		if (value != NULL && value_len != 0 && copyin_value) {
20560Sstevel@tonic-gate 			if (copyin(value, p, value_len) != 0) {
20570Sstevel@tonic-gate 				kmem_free(k_attrs, k_attrs_total_len);
20580Sstevel@tonic-gate 				k_attrs = NULL;
20590Sstevel@tonic-gate 				error = EFAULT;
20600Sstevel@tonic-gate 				goto out;
20610Sstevel@tonic-gate 			}
20620Sstevel@tonic-gate 		}
20630Sstevel@tonic-gate 
2064292Smcpowers 		if (value != NULL) {
2065292Smcpowers 			k_attrs[i].oa_value = p;
2066292Smcpowers 			p += roundup(value_len, sizeof (caddr_t));
2067292Smcpowers 		} else {
2068292Smcpowers 			k_attrs[i].oa_value = NULL;
2069292Smcpowers 		}
20700Sstevel@tonic-gate 		k_attrs[i].oa_value_len = value_len;
20710Sstevel@tonic-gate 		ap += STRUCT_SIZE(oa);
20720Sstevel@tonic-gate 	}
20730Sstevel@tonic-gate out:
20740Sstevel@tonic-gate 	if (attrs != NULL) {
20750Sstevel@tonic-gate 		/*
20760Sstevel@tonic-gate 		 * Free the array if there is a failure or the caller
20770Sstevel@tonic-gate 		 * doesn't want the array to be returned.
20780Sstevel@tonic-gate 		 */
20790Sstevel@tonic-gate 		if (error != 0 || rv != CRYPTO_SUCCESS || u_attrs_out == NULL) {
20800Sstevel@tonic-gate 			kmem_free(attrs, len);
20810Sstevel@tonic-gate 			attrs = NULL;
20820Sstevel@tonic-gate 		}
20830Sstevel@tonic-gate 	}
20840Sstevel@tonic-gate 
20850Sstevel@tonic-gate 	if (u_attrs_out != NULL)
20860Sstevel@tonic-gate 		*u_attrs_out = attrs;
20870Sstevel@tonic-gate 	if (k_attrs_size_out != NULL)
20880Sstevel@tonic-gate 		*k_attrs_size_out = k_attrs_total_len;
20890Sstevel@tonic-gate 	*k_attrs_out = k_attrs;
20900Sstevel@tonic-gate 	*out_rctl_bytes = rctl_bytes;
20910Sstevel@tonic-gate 	*out_rv = rv;
20920Sstevel@tonic-gate 	*out_error = error;
20930Sstevel@tonic-gate 	return ((rv | error) ? B_FALSE : B_TRUE);
20940Sstevel@tonic-gate }
20950Sstevel@tonic-gate 
20960Sstevel@tonic-gate /*
20970Sstevel@tonic-gate  * Copy data model dependent raw key into a kernel key
20980Sstevel@tonic-gate  * structure.  Checks key length or attribute lengths against
20990Sstevel@tonic-gate  * resource controls before allocating memory.  Returns B_TRUE
21000Sstevel@tonic-gate  * if both error and rv are set to 0.
21010Sstevel@tonic-gate  */
21020Sstevel@tonic-gate static boolean_t
21036424Skrishna copyin_key(int mode, crypto_session_data_t *sp, crypto_key_t *in_key,
21046424Skrishna     crypto_key_t *out_key, size_t *out_rctl_bytes,
21056424Skrishna     boolean_t *out_rctl_chk, int *out_rv, int *out_error)
21060Sstevel@tonic-gate {
21070Sstevel@tonic-gate 	STRUCT_DECL(crypto_key, key);
21080Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
21090Sstevel@tonic-gate 	size_t key_bits;
21100Sstevel@tonic-gate 	size_t key_bytes = 0;
21110Sstevel@tonic-gate 	size_t rctl_bytes = 0;
21120Sstevel@tonic-gate 	int count;
21130Sstevel@tonic-gate 	int error = 0;
21140Sstevel@tonic-gate 	int rv = CRYPTO_SUCCESS;
21150Sstevel@tonic-gate 
21160Sstevel@tonic-gate 	STRUCT_INIT(key, mode);
21170Sstevel@tonic-gate 	bcopy(in_key, STRUCT_BUF(key), STRUCT_SIZE(key));
21180Sstevel@tonic-gate 	out_key->ck_format = STRUCT_FGET(key, ck_format);
21190Sstevel@tonic-gate 	switch (out_key->ck_format) {
21200Sstevel@tonic-gate 	case CRYPTO_KEY_RAW:
21210Sstevel@tonic-gate 		key_bits = STRUCT_FGET(key, ck_length);
21220Sstevel@tonic-gate 		if (key_bits != 0) {
21230Sstevel@tonic-gate 			key_bytes = CRYPTO_BITS2BYTES(key_bits);
21240Sstevel@tonic-gate 			if (key_bytes > crypto_max_buffer_len) {
21250Sstevel@tonic-gate 				cmn_err(CE_NOTE, "copyin_key: buffer greater "
21260Sstevel@tonic-gate 				    "than %ld bytes, pid = %d",
21270Sstevel@tonic-gate 				    crypto_max_buffer_len, curproc->p_pid);
21280Sstevel@tonic-gate 				rv = CRYPTO_ARGUMENTS_BAD;
21290Sstevel@tonic-gate 				goto out;
21300Sstevel@tonic-gate 			}
21310Sstevel@tonic-gate 
21326424Skrishna 			rv = CRYPTO_BUFFER_CHECK(sp, key_bytes,
21336424Skrishna 			    *out_rctl_chk);
21340Sstevel@tonic-gate 			if (rv != CRYPTO_SUCCESS) {
21350Sstevel@tonic-gate 				goto out;
21360Sstevel@tonic-gate 			}
21376424Skrishna 			rctl_bytes = key_bytes;
21380Sstevel@tonic-gate 
21390Sstevel@tonic-gate 			out_key->ck_data = kmem_alloc(key_bytes, KM_SLEEP);
21400Sstevel@tonic-gate 
21410Sstevel@tonic-gate 			if (copyin((char *)STRUCT_FGETP(key, ck_data),
21420Sstevel@tonic-gate 			    out_key->ck_data, key_bytes) != 0) {
21430Sstevel@tonic-gate 				kmem_free(out_key->ck_data, key_bytes);
21440Sstevel@tonic-gate 				out_key->ck_data = NULL;
21450Sstevel@tonic-gate 				out_key->ck_length = 0;
21460Sstevel@tonic-gate 				error = EFAULT;
21470Sstevel@tonic-gate 				goto out;
21480Sstevel@tonic-gate 			}
21490Sstevel@tonic-gate 		}
21500Sstevel@tonic-gate 		out_key->ck_length = key_bits;
21510Sstevel@tonic-gate 		break;
21520Sstevel@tonic-gate 
21530Sstevel@tonic-gate 	case CRYPTO_KEY_ATTR_LIST:
21540Sstevel@tonic-gate 		count = STRUCT_FGET(key, ck_count);
21550Sstevel@tonic-gate 
21566424Skrishna 		if (copyin_attributes(mode, sp, count,
21570Sstevel@tonic-gate 		    (caddr_t)STRUCT_FGETP(key, ck_attrs), &k_attrs, NULL, NULL,
21586424Skrishna 		    &rv, &error, &rctl_bytes, out_rctl_chk, B_TRUE)) {
21590Sstevel@tonic-gate 			out_key->ck_count = count;
21600Sstevel@tonic-gate 			out_key->ck_attrs = k_attrs;
21610Sstevel@tonic-gate 			k_attrs = NULL;
21620Sstevel@tonic-gate 		} else {
21630Sstevel@tonic-gate 			out_key->ck_count = 0;
21640Sstevel@tonic-gate 			out_key->ck_attrs = NULL;
21650Sstevel@tonic-gate 		}
21660Sstevel@tonic-gate 		break;
21670Sstevel@tonic-gate 
21680Sstevel@tonic-gate 	case CRYPTO_KEY_REFERENCE:
21690Sstevel@tonic-gate 		out_key->ck_obj_id = STRUCT_FGET(key, ck_obj_id);
21700Sstevel@tonic-gate 		break;
21710Sstevel@tonic-gate 
21720Sstevel@tonic-gate 	default:
21730Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
21740Sstevel@tonic-gate 	}
21750Sstevel@tonic-gate 
21760Sstevel@tonic-gate out:
21770Sstevel@tonic-gate 	*out_rctl_bytes = rctl_bytes;
21780Sstevel@tonic-gate 	*out_rv = rv;
21790Sstevel@tonic-gate 	*out_error = error;
21800Sstevel@tonic-gate 	return ((rv | error) ? B_FALSE : B_TRUE);
21810Sstevel@tonic-gate }
21820Sstevel@tonic-gate 
21830Sstevel@tonic-gate /*
21840Sstevel@tonic-gate  * This routine does two things:
21850Sstevel@tonic-gate  * 1. Given a crypto_minor structure and a session ID, it returns
21860Sstevel@tonic-gate  *    a valid session pointer.
21870Sstevel@tonic-gate  * 2. It checks that the provider, to which the session has been opened,
21880Sstevel@tonic-gate  *    has not been removed.
21890Sstevel@tonic-gate  */
21900Sstevel@tonic-gate static boolean_t
21910Sstevel@tonic-gate get_session_ptr(crypto_session_id_t i, crypto_minor_t *cm,
21920Sstevel@tonic-gate     crypto_session_data_t **session_ptr, int *out_error, int *out_rv)
21930Sstevel@tonic-gate {
21940Sstevel@tonic-gate 	crypto_session_data_t *sp = NULL;
21950Sstevel@tonic-gate 	int rv = CRYPTO_SESSION_HANDLE_INVALID;
21960Sstevel@tonic-gate 	int error = 0;
21970Sstevel@tonic-gate 
21980Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
21990Sstevel@tonic-gate 	if ((i < cm->cm_session_table_count) &&
22000Sstevel@tonic-gate 	    (cm->cm_session_table[i] != NULL)) {
22010Sstevel@tonic-gate 		sp = cm->cm_session_table[i];
22020Sstevel@tonic-gate 		mutex_enter(&sp->sd_lock);
22030Sstevel@tonic-gate 		mutex_exit(&cm->cm_lock);
22040Sstevel@tonic-gate 		while (sp->sd_flags & CRYPTO_SESSION_IS_BUSY) {
22050Sstevel@tonic-gate 			if (cv_wait_sig(&sp->sd_cv, &sp->sd_lock) == 0) {
22060Sstevel@tonic-gate 				mutex_exit(&sp->sd_lock);
22070Sstevel@tonic-gate 				sp = NULL;
22080Sstevel@tonic-gate 				error = EINTR;
22090Sstevel@tonic-gate 				goto out;
22100Sstevel@tonic-gate 			}
22110Sstevel@tonic-gate 		}
22120Sstevel@tonic-gate 
22130Sstevel@tonic-gate 		if (sp->sd_flags & CRYPTO_SESSION_IS_CLOSED) {
22140Sstevel@tonic-gate 			mutex_exit(&sp->sd_lock);
22150Sstevel@tonic-gate 			sp = NULL;
22160Sstevel@tonic-gate 			goto out;
22170Sstevel@tonic-gate 		}
22180Sstevel@tonic-gate 
22190Sstevel@tonic-gate 		if (KCF_IS_PROV_REMOVED(sp->sd_provider)) {
22200Sstevel@tonic-gate 			mutex_exit(&sp->sd_lock);
22210Sstevel@tonic-gate 			sp = NULL;
22220Sstevel@tonic-gate 			rv = CRYPTO_DEVICE_ERROR;
22230Sstevel@tonic-gate 			goto out;
22240Sstevel@tonic-gate 		}
22250Sstevel@tonic-gate 
22260Sstevel@tonic-gate 		rv = CRYPTO_SUCCESS;
22270Sstevel@tonic-gate 		sp->sd_flags |= CRYPTO_SESSION_IS_BUSY;
22280Sstevel@tonic-gate 		mutex_exit(&sp->sd_lock);
22290Sstevel@tonic-gate 	} else {
22300Sstevel@tonic-gate 		mutex_exit(&cm->cm_lock);
22310Sstevel@tonic-gate 	}
22320Sstevel@tonic-gate out:
22330Sstevel@tonic-gate 	*session_ptr = sp;
22340Sstevel@tonic-gate 	*out_error = error;
22350Sstevel@tonic-gate 	*out_rv = rv;
22360Sstevel@tonic-gate 	return ((rv == CRYPTO_SUCCESS && error == 0) ? B_TRUE : B_FALSE);
22370Sstevel@tonic-gate }
22380Sstevel@tonic-gate 
22396424Skrishna #define	CRYPTO_SESSION_RELE(s)	if ((s) != NULL) {	\
22400Sstevel@tonic-gate 	mutex_enter(&((s)->sd_lock));			\
22410Sstevel@tonic-gate 	(s)->sd_flags &= ~CRYPTO_SESSION_IS_BUSY;	\
22420Sstevel@tonic-gate 	cv_broadcast(&(s)->sd_cv);			\
22430Sstevel@tonic-gate 	mutex_exit(&((s)->sd_lock));			\
22440Sstevel@tonic-gate }
22450Sstevel@tonic-gate 
22460Sstevel@tonic-gate /* ARGSUSED */
22470Sstevel@tonic-gate static int
22480Sstevel@tonic-gate encrypt_init(dev_t dev, caddr_t arg, int mode, int *rval)
22490Sstevel@tonic-gate {
22500Sstevel@tonic-gate 	return (cipher_init(dev, arg, mode, crypto_encrypt_init_prov));
22510Sstevel@tonic-gate }
22520Sstevel@tonic-gate 
22530Sstevel@tonic-gate /* ARGSUSED */
22540Sstevel@tonic-gate static int
22550Sstevel@tonic-gate decrypt_init(dev_t dev, caddr_t arg, int mode, int *rval)
22560Sstevel@tonic-gate {
22570Sstevel@tonic-gate 	return (cipher_init(dev, arg, mode, crypto_decrypt_init_prov));
22580Sstevel@tonic-gate }
22590Sstevel@tonic-gate 
22600Sstevel@tonic-gate /*
2261904Smcpowers  * umech is a mechanism structure that has been copied from user address
2262904Smcpowers  * space into kernel address space. Only one copyin has been done.
2263904Smcpowers  * The mechanism parameter, if non-null, still points to user address space.
2264904Smcpowers  * If the mechanism parameter contains pointers, they are pointers into
2265904Smcpowers  * user address space.
2266904Smcpowers  *
2267904Smcpowers  * kmech is a umech with all pointers and structures in kernel address space.
2268904Smcpowers  *
2269904Smcpowers  * This routine calls the provider's entry point to copy a umech parameter
2270904Smcpowers  * into kernel address space. Kernel memory is allocated by the provider.
2271904Smcpowers  */
2272904Smcpowers static int
2273904Smcpowers crypto_provider_copyin_mech_param(kcf_provider_desc_t *pd,
2274904Smcpowers     crypto_mechanism_t *umech, crypto_mechanism_t *kmech, int mode, int *error)
2275904Smcpowers {
2276904Smcpowers 	crypto_mech_type_t provider_mech_type;
2277904Smcpowers 	int rv;
2278904Smcpowers 
2279904Smcpowers 	/* get the provider's mech number */
22803708Skrishna 	provider_mech_type = KCF_TO_PROV_MECHNUM(pd, umech->cm_type);
2281904Smcpowers 
2282904Smcpowers 	kmech->cm_param = NULL;
2283904Smcpowers 	kmech->cm_param_len = 0;
2284904Smcpowers 	kmech->cm_type = provider_mech_type;
2285904Smcpowers 	rv = KCF_PROV_COPYIN_MECH(pd, umech, kmech, error, mode);
2286904Smcpowers 	kmech->cm_type = umech->cm_type;
2287904Smcpowers 
2288904Smcpowers 	return (rv);
2289904Smcpowers }
2290904Smcpowers 
2291904Smcpowers /*
2292904Smcpowers  * umech is a mechanism structure that has been copied from user address
2293904Smcpowers  * space into kernel address space. Only one copyin has been done.
2294904Smcpowers  * The mechanism parameter, if non-null, still points to user address space.
2295904Smcpowers  * If the mechanism parameter contains pointers, they are pointers into
2296904Smcpowers  * user address space.
2297904Smcpowers  *
2298904Smcpowers  * kmech is a umech with all pointers and structures in kernel address space.
2299904Smcpowers  *
2300904Smcpowers  * This routine calls the provider's entry point to copy a kmech parameter
2301904Smcpowers  * into user address space using umech as a template containing
2302904Smcpowers  * user address pointers.
2303904Smcpowers  */
2304904Smcpowers static int
2305904Smcpowers crypto_provider_copyout_mech_param(kcf_provider_desc_t *pd,
2306904Smcpowers     crypto_mechanism_t *kmech, crypto_mechanism_t *umech, int mode, int *error)
2307904Smcpowers {
2308904Smcpowers 	crypto_mech_type_t provider_mech_type;
2309904Smcpowers 	int rv;
2310904Smcpowers 
2311904Smcpowers 	/* get the provider's mech number */
23123708Skrishna 	provider_mech_type = KCF_TO_PROV_MECHNUM(pd, umech->cm_type);
2313904Smcpowers 
2314904Smcpowers 	kmech->cm_type = provider_mech_type;
2315904Smcpowers 	rv = KCF_PROV_COPYOUT_MECH(pd, kmech, umech, error, mode);
2316904Smcpowers 	kmech->cm_type = umech->cm_type;
2317904Smcpowers 
2318904Smcpowers 	return (rv);
2319904Smcpowers }
2320904Smcpowers 
2321904Smcpowers /*
2322904Smcpowers  * Call the provider's entry point to free kernel memory that has been
2323904Smcpowers  * allocated for the mechanism's parameter.
2324904Smcpowers  */
2325904Smcpowers static void
2326904Smcpowers crypto_free_mech(kcf_provider_desc_t *pd, boolean_t allocated_by_crypto_module,
2327904Smcpowers     crypto_mechanism_t *mech)
2328904Smcpowers {
2329904Smcpowers 	crypto_mech_type_t provider_mech_type;
2330904Smcpowers 
2331904Smcpowers 	if (allocated_by_crypto_module) {
2332904Smcpowers 		if (mech->cm_param != NULL)
2333904Smcpowers 			kmem_free(mech->cm_param, mech->cm_param_len);
2334904Smcpowers 	} else {
2335904Smcpowers 		/* get the provider's mech number */
23363708Skrishna 		provider_mech_type = KCF_TO_PROV_MECHNUM(pd, mech->cm_type);
2337904Smcpowers 
2338904Smcpowers 		if (mech->cm_param != NULL && mech->cm_param_len != 0) {
2339904Smcpowers 			mech->cm_type = provider_mech_type;
2340904Smcpowers 			(void) KCF_PROV_FREE_MECH(pd, mech);
2341904Smcpowers 		}
2342904Smcpowers 	}
2343904Smcpowers }
2344904Smcpowers 
2345904Smcpowers /*
23460Sstevel@tonic-gate  * ASSUMPTION: crypto_encrypt_init and crypto_decrypt_init
23470Sstevel@tonic-gate  * structures are identical except for field names.
23480Sstevel@tonic-gate  */
23490Sstevel@tonic-gate static int
2350904Smcpowers cipher_init(dev_t dev, caddr_t arg, int mode, int (*init)(crypto_provider_t,
23510Sstevel@tonic-gate     crypto_session_id_t, crypto_mechanism_t *, crypto_key_t *,
23520Sstevel@tonic-gate     crypto_ctx_template_t, crypto_context_t *, crypto_call_req_t *))
23530Sstevel@tonic-gate {
23540Sstevel@tonic-gate 	STRUCT_DECL(crypto_encrypt_init, encrypt_init);
2355904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
23560Sstevel@tonic-gate 	crypto_session_id_t session_id;
23570Sstevel@tonic-gate 	crypto_mechanism_t mech;
23580Sstevel@tonic-gate 	crypto_key_t key;
23590Sstevel@tonic-gate 	crypto_minor_t *cm;
23606424Skrishna 	crypto_session_data_t *sp = NULL;
23610Sstevel@tonic-gate 	crypto_context_t cc;
23620Sstevel@tonic-gate 	crypto_ctx_t **ctxpp;
23630Sstevel@tonic-gate 	size_t mech_rctl_bytes = 0;
23646424Skrishna 	boolean_t mech_rctl_chk = B_FALSE;
23650Sstevel@tonic-gate 	size_t key_rctl_bytes = 0;
23666424Skrishna 	boolean_t key_rctl_chk = B_FALSE;
23670Sstevel@tonic-gate 	int error = 0;
23680Sstevel@tonic-gate 	int rv;
2369904Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
23701808Smcpowers 	crypto_func_group_t fg;
23710Sstevel@tonic-gate 
23720Sstevel@tonic-gate 	STRUCT_INIT(encrypt_init, mode);
23730Sstevel@tonic-gate 
23740Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
23750Sstevel@tonic-gate 		cmn_err(CE_WARN, "cipher_init: failed holding minor");
23760Sstevel@tonic-gate 		return (ENXIO);
23770Sstevel@tonic-gate 	}
23780Sstevel@tonic-gate 
23790Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(encrypt_init),
23800Sstevel@tonic-gate 	    STRUCT_SIZE(encrypt_init)) != 0) {
23810Sstevel@tonic-gate 		crypto_release_minor(cm);
23820Sstevel@tonic-gate 		return (EFAULT);
23830Sstevel@tonic-gate 	}
23840Sstevel@tonic-gate 
23850Sstevel@tonic-gate 	mech.cm_param = NULL;
23860Sstevel@tonic-gate 	bzero(&key, sizeof (crypto_key_t));
23870Sstevel@tonic-gate 
23880Sstevel@tonic-gate 	session_id = STRUCT_FGET(encrypt_init, ei_session);
23890Sstevel@tonic-gate 
23900Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
23916424Skrishna 		goto out;
23920Sstevel@tonic-gate 	}
23930Sstevel@tonic-gate 
2394904Smcpowers 	bcopy(STRUCT_FADDR(encrypt_init, ei_mech), &mech.cm_type,
2395904Smcpowers 	    sizeof (crypto_mech_type_t));
23960Sstevel@tonic-gate 
23971808Smcpowers 	if (init == crypto_encrypt_init_prov) {
23981808Smcpowers 		fg = CRYPTO_FG_ENCRYPT;
23991808Smcpowers 	} else {
24001808Smcpowers 		fg = CRYPTO_FG_DECRYPT;
24011808Smcpowers 	}
24020Sstevel@tonic-gate 
2403904Smcpowers 	if ((rv = kcf_get_hardware_provider(mech.cm_type, CRYPTO_MECH_INVALID,
24041808Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider, fg))
24051808Smcpowers 	    != CRYPTO_SUCCESS) {
24060Sstevel@tonic-gate 		goto out;
24070Sstevel@tonic-gate 	}
24080Sstevel@tonic-gate 
2409904Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
2410904Smcpowers 	    STRUCT_FADDR(encrypt_init, ei_mech), &mech, mode, &error);
2411904Smcpowers 
2412904Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
2413904Smcpowers 		allocated_by_crypto_module = B_TRUE;
24146424Skrishna 		if (!copyin_mech(mode, sp, STRUCT_FADDR(encrypt_init, ei_mech),
24156424Skrishna 		    &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) {
2416904Smcpowers 			goto out;
2417904Smcpowers 		}
2418904Smcpowers 	} else {
2419904Smcpowers 		if (rv != CRYPTO_SUCCESS)
2420904Smcpowers 			goto out;
2421904Smcpowers 	}
2422904Smcpowers 
24236424Skrishna 	if (!copyin_key(mode, sp, STRUCT_FADDR(encrypt_init, ei_key), &key,
24246424Skrishna 	    &key_rctl_bytes, &key_rctl_chk, &rv, &error)) {
24250Sstevel@tonic-gate 		goto out;
24260Sstevel@tonic-gate 	}
24270Sstevel@tonic-gate 
24280Sstevel@tonic-gate 	rv = (init)(real_provider, sp->sd_provider_session->ps_session,
24290Sstevel@tonic-gate 	    &mech, &key, NULL, &cc, NULL);
24300Sstevel@tonic-gate 
24310Sstevel@tonic-gate 	/*
24320Sstevel@tonic-gate 	 * Check if a context already exists. If so, it means it is being
24330Sstevel@tonic-gate 	 * abandoned. So, cancel it to avoid leaking it.
24340Sstevel@tonic-gate 	 */
24350Sstevel@tonic-gate 	ctxpp = (init == crypto_encrypt_init_prov) ?
24360Sstevel@tonic-gate 	    &sp->sd_encr_ctx : &sp->sd_decr_ctx;
24370Sstevel@tonic-gate 
24380Sstevel@tonic-gate 	if (*ctxpp != NULL)
24390Sstevel@tonic-gate 		CRYPTO_CANCEL_CTX(ctxpp);
24400Sstevel@tonic-gate 	*ctxpp = (rv == CRYPTO_SUCCESS) ? cc : NULL;
2441904Smcpowers 
24420Sstevel@tonic-gate out:
24436424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
24446424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk);
24450Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
24460Sstevel@tonic-gate 	crypto_release_minor(cm);
24470Sstevel@tonic-gate 
2448904Smcpowers 	if (real_provider != NULL) {
2449904Smcpowers 		crypto_free_mech(real_provider,
2450904Smcpowers 		    allocated_by_crypto_module, &mech);
2451904Smcpowers 		KCF_PROV_REFRELE(real_provider);
2452904Smcpowers 	}
24530Sstevel@tonic-gate 
24540Sstevel@tonic-gate 	free_crypto_key(&key);
24550Sstevel@tonic-gate 
24560Sstevel@tonic-gate 	if (error != 0)
2457904Smcpowers 		/* XXX free context */
24580Sstevel@tonic-gate 		return (error);
24590Sstevel@tonic-gate 
24600Sstevel@tonic-gate 	STRUCT_FSET(encrypt_init, ei_return_value, rv);
24610Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(encrypt_init), arg,
24620Sstevel@tonic-gate 	    STRUCT_SIZE(encrypt_init)) != 0) {
2463904Smcpowers 		/* XXX free context */
24640Sstevel@tonic-gate 		return (EFAULT);
24650Sstevel@tonic-gate 	}
24660Sstevel@tonic-gate 	return (0);
24670Sstevel@tonic-gate }
24680Sstevel@tonic-gate 
24690Sstevel@tonic-gate /* ARGSUSED */
24700Sstevel@tonic-gate static int
24710Sstevel@tonic-gate encrypt(dev_t dev, caddr_t arg, int mode, int *rval)
24720Sstevel@tonic-gate {
24730Sstevel@tonic-gate 	return (cipher(dev, arg, mode, crypto_encrypt_single));
24740Sstevel@tonic-gate }
24750Sstevel@tonic-gate 
24760Sstevel@tonic-gate /* ARGSUSED */
24770Sstevel@tonic-gate static int
24780Sstevel@tonic-gate decrypt(dev_t dev, caddr_t arg, int mode, int *rval)
24790Sstevel@tonic-gate {
24800Sstevel@tonic-gate 	return (cipher(dev, arg, mode, crypto_decrypt_single));
24810Sstevel@tonic-gate }
24820Sstevel@tonic-gate 
24830Sstevel@tonic-gate /*
24840Sstevel@tonic-gate  * ASSUMPTION: crypto_encrypt and crypto_decrypt structures
24850Sstevel@tonic-gate  * are identical except for field names.
24860Sstevel@tonic-gate  */
24870Sstevel@tonic-gate static int
24880Sstevel@tonic-gate cipher(dev_t dev, caddr_t arg, int mode,
24890Sstevel@tonic-gate     int (*single)(crypto_context_t, crypto_data_t *, crypto_data_t *,
24900Sstevel@tonic-gate     crypto_call_req_t *))
24910Sstevel@tonic-gate {
24920Sstevel@tonic-gate 	STRUCT_DECL(crypto_encrypt, encrypt);
24930Sstevel@tonic-gate 	crypto_session_id_t session_id;
24940Sstevel@tonic-gate 	crypto_minor_t *cm;
24956424Skrishna 	crypto_session_data_t *sp = NULL;
24960Sstevel@tonic-gate 	crypto_ctx_t **ctxpp;
24970Sstevel@tonic-gate 	crypto_data_t data, encr;
24980Sstevel@tonic-gate 	size_t datalen, encrlen, need = 0;
24994632Smcpowers 	boolean_t do_inplace;
25000Sstevel@tonic-gate 	char *encrbuf;
25010Sstevel@tonic-gate 	int error = 0;
25020Sstevel@tonic-gate 	int rv;
25036424Skrishna 	boolean_t rctl_chk = B_FALSE;
25040Sstevel@tonic-gate 
25050Sstevel@tonic-gate 	STRUCT_INIT(encrypt, mode);
25060Sstevel@tonic-gate 
25070Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
25080Sstevel@tonic-gate 		cmn_err(CE_WARN, "cipher: failed holding minor");
25090Sstevel@tonic-gate 		return (ENXIO);
25100Sstevel@tonic-gate 	}
25110Sstevel@tonic-gate 
25120Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(encrypt), STRUCT_SIZE(encrypt)) != 0) {
25130Sstevel@tonic-gate 		crypto_release_minor(cm);
25140Sstevel@tonic-gate 		return (EFAULT);
25150Sstevel@tonic-gate 	}
25160Sstevel@tonic-gate 
25170Sstevel@tonic-gate 	data.cd_raw.iov_base = NULL;
25180Sstevel@tonic-gate 	encr.cd_raw.iov_base = NULL;
25190Sstevel@tonic-gate 
25200Sstevel@tonic-gate 	datalen = STRUCT_FGET(encrypt, ce_datalen);
25210Sstevel@tonic-gate 	encrlen = STRUCT_FGET(encrypt, ce_encrlen);
25220Sstevel@tonic-gate 
25230Sstevel@tonic-gate 	/*
25240Sstevel@tonic-gate 	 * Don't allocate output buffer unless both buffer pointer and
25250Sstevel@tonic-gate 	 * buffer length are not NULL or 0 (length).
25260Sstevel@tonic-gate 	 */
25270Sstevel@tonic-gate 	encrbuf = STRUCT_FGETP(encrypt, ce_encrbuf);
25280Sstevel@tonic-gate 	if (encrbuf == NULL || encrlen == 0) {
25290Sstevel@tonic-gate 		encrlen = 0;
25300Sstevel@tonic-gate 	}
25310Sstevel@tonic-gate 
25320Sstevel@tonic-gate 	if (datalen > crypto_max_buffer_len ||
25330Sstevel@tonic-gate 	    encrlen > crypto_max_buffer_len) {
25340Sstevel@tonic-gate 		cmn_err(CE_NOTE, "cipher: buffer greater than %ld bytes, "
25350Sstevel@tonic-gate 		    "pid = %d", crypto_max_buffer_len, curproc->p_pid);
25360Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
25370Sstevel@tonic-gate 		goto release_minor;
25380Sstevel@tonic-gate 	}
25390Sstevel@tonic-gate 
25406424Skrishna 	session_id = STRUCT_FGET(encrypt, ce_session);
25416424Skrishna 
25426424Skrishna 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv))  {
25436424Skrishna 		goto release_minor;
25446424Skrishna 	}
2545*6867Skrishna 
2546*6867Skrishna 	do_inplace = (STRUCT_FGET(encrypt, ce_flags) &
2547*6867Skrishna 	    CRYPTO_INPLACE_OPERATION) != 0;
2548*6867Skrishna 	need = do_inplace ? datalen : datalen + encrlen;
2549*6867Skrishna 
25506424Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, need, rctl_chk)) !=
25516424Skrishna 	    CRYPTO_SUCCESS) {
25520Sstevel@tonic-gate 		need = 0;
25530Sstevel@tonic-gate 		goto release_minor;
25540Sstevel@tonic-gate 	}
25550Sstevel@tonic-gate 
25560Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(data, datalen);
25570Sstevel@tonic-gate 	data.cd_miscdata = NULL;
25580Sstevel@tonic-gate 
25590Sstevel@tonic-gate 	if (datalen != 0 && copyin(STRUCT_FGETP(encrypt, ce_databuf),
25600Sstevel@tonic-gate 	    data.cd_raw.iov_base, datalen) != 0) {
25610Sstevel@tonic-gate 		error = EFAULT;
25620Sstevel@tonic-gate 		goto release_minor;
25630Sstevel@tonic-gate 	}
25640Sstevel@tonic-gate 
25654632Smcpowers 	if (do_inplace) {
25664632Smcpowers 		/* set out = in for in-place */
25674632Smcpowers 		encr = data;
25684632Smcpowers 	} else {
25694632Smcpowers 		INIT_RAW_CRYPTO_DATA(encr, encrlen);
25704632Smcpowers 	}
25710Sstevel@tonic-gate 
25720Sstevel@tonic-gate 	ctxpp = (single == crypto_encrypt_single) ?
25730Sstevel@tonic-gate 	    &sp->sd_encr_ctx : &sp->sd_decr_ctx;
25740Sstevel@tonic-gate 
25754632Smcpowers 	/* in-place is specified by setting output NULL */
25764632Smcpowers 	if (do_inplace)
25774632Smcpowers 		rv = (single)(*ctxpp, &encr, NULL, NULL);
25784632Smcpowers 	else
25794632Smcpowers 		rv = (single)(*ctxpp, &data, &encr, NULL);
25800Sstevel@tonic-gate 	if (KCF_CONTEXT_DONE(rv))
25810Sstevel@tonic-gate 		*ctxpp = NULL;
25820Sstevel@tonic-gate 
25830Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
25840Sstevel@tonic-gate 		ASSERT(encr.cd_length <= encrlen);
25850Sstevel@tonic-gate 		if (encr.cd_length != 0 && copyout(encr.cd_raw.iov_base,
25860Sstevel@tonic-gate 		    encrbuf, encr.cd_length) != 0) {
25870Sstevel@tonic-gate 			error = EFAULT;
25880Sstevel@tonic-gate 			goto release_minor;
25890Sstevel@tonic-gate 		}
25900Sstevel@tonic-gate 		STRUCT_FSET(encrypt, ce_encrlen, encr.cd_length);
25910Sstevel@tonic-gate 	}
25920Sstevel@tonic-gate 
25930Sstevel@tonic-gate 	if (rv == CRYPTO_BUFFER_TOO_SMALL) {
25940Sstevel@tonic-gate 		/*
25950Sstevel@tonic-gate 		 * The providers return CRYPTO_BUFFER_TOO_SMALL even for case 1
25960Sstevel@tonic-gate 		 * of section 11.2 of the pkcs11 spec. We catch it here and
25970Sstevel@tonic-gate 		 * provide the correct pkcs11 return value.
25980Sstevel@tonic-gate 		 */
25990Sstevel@tonic-gate 		if (STRUCT_FGETP(encrypt, ce_encrbuf) == NULL)
26000Sstevel@tonic-gate 			rv = CRYPTO_SUCCESS;
26010Sstevel@tonic-gate 		STRUCT_FSET(encrypt, ce_encrlen, encr.cd_length);
26020Sstevel@tonic-gate 	}
26030Sstevel@tonic-gate 
26040Sstevel@tonic-gate release_minor:
26056424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
26066424Skrishna 	CRYPTO_SESSION_RELE(sp);
26070Sstevel@tonic-gate 	crypto_release_minor(cm);
26080Sstevel@tonic-gate 
26090Sstevel@tonic-gate 	if (data.cd_raw.iov_base != NULL)
26100Sstevel@tonic-gate 		kmem_free(data.cd_raw.iov_base, datalen);
26110Sstevel@tonic-gate 
26124632Smcpowers 	if (!do_inplace && encr.cd_raw.iov_base != NULL)
26130Sstevel@tonic-gate 		kmem_free(encr.cd_raw.iov_base, encrlen);
26140Sstevel@tonic-gate 
26150Sstevel@tonic-gate 	if (error != 0)
26160Sstevel@tonic-gate 		return (error);
26170Sstevel@tonic-gate 
26180Sstevel@tonic-gate 	STRUCT_FSET(encrypt, ce_return_value, rv);
26190Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(encrypt), arg, STRUCT_SIZE(encrypt)) != 0) {
26200Sstevel@tonic-gate 		return (EFAULT);
26210Sstevel@tonic-gate 	}
26220Sstevel@tonic-gate 	return (0);
26230Sstevel@tonic-gate }
26240Sstevel@tonic-gate 
26250Sstevel@tonic-gate /* ARGSUSED */
26260Sstevel@tonic-gate static int
26270Sstevel@tonic-gate encrypt_update(dev_t dev, caddr_t arg, int mode, int *rval)
26280Sstevel@tonic-gate {
26290Sstevel@tonic-gate 	return (cipher_update(dev, arg, mode, crypto_encrypt_update));
26300Sstevel@tonic-gate }
26310Sstevel@tonic-gate 
26320Sstevel@tonic-gate /* ARGSUSED */
26330Sstevel@tonic-gate static int
26340Sstevel@tonic-gate decrypt_update(dev_t dev, caddr_t arg, int mode, int *rval)
26350Sstevel@tonic-gate {
26360Sstevel@tonic-gate 	return (cipher_update(dev, arg, mode, crypto_decrypt_update));
26370Sstevel@tonic-gate }
26380Sstevel@tonic-gate 
26390Sstevel@tonic-gate /*
26400Sstevel@tonic-gate  * ASSUMPTION: crypto_encrypt_update and crypto_decrypt_update
26410Sstevel@tonic-gate  * structures are identical except for field names.
26420Sstevel@tonic-gate  */
26430Sstevel@tonic-gate static int
26440Sstevel@tonic-gate cipher_update(dev_t dev, caddr_t arg, int mode,
26450Sstevel@tonic-gate     int (*update)(crypto_context_t, crypto_data_t *, crypto_data_t *,
26460Sstevel@tonic-gate     crypto_call_req_t *))
26470Sstevel@tonic-gate {
26480Sstevel@tonic-gate 	STRUCT_DECL(crypto_encrypt_update, encrypt_update);
26490Sstevel@tonic-gate 	crypto_session_id_t session_id;
26500Sstevel@tonic-gate 	crypto_minor_t *cm;
26516424Skrishna 	crypto_session_data_t *sp = NULL;
26520Sstevel@tonic-gate 	crypto_ctx_t **ctxpp;
26530Sstevel@tonic-gate 	crypto_data_t data, encr;
26540Sstevel@tonic-gate 	size_t datalen, encrlen, need = 0;
26550Sstevel@tonic-gate 	char *encrbuf;
26560Sstevel@tonic-gate 	int error = 0;
26570Sstevel@tonic-gate 	int rv;
26586424Skrishna 	boolean_t rctl_chk = B_FALSE;
26590Sstevel@tonic-gate 
26600Sstevel@tonic-gate 	STRUCT_INIT(encrypt_update, mode);
26610Sstevel@tonic-gate 
26620Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
26630Sstevel@tonic-gate 		cmn_err(CE_WARN, "cipher_update: failed holding minor");
26640Sstevel@tonic-gate 		return (ENXIO);
26650Sstevel@tonic-gate 	}
26660Sstevel@tonic-gate 
26670Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(encrypt_update),
26680Sstevel@tonic-gate 	    STRUCT_SIZE(encrypt_update)) != 0) {
26690Sstevel@tonic-gate 		crypto_release_minor(cm);
26700Sstevel@tonic-gate 		return (EFAULT);
26710Sstevel@tonic-gate 	}
26720Sstevel@tonic-gate 
26730Sstevel@tonic-gate 	data.cd_raw.iov_base = NULL;
26740Sstevel@tonic-gate 	encr.cd_raw.iov_base = NULL;
26750Sstevel@tonic-gate 
26760Sstevel@tonic-gate 	datalen = STRUCT_FGET(encrypt_update, eu_datalen);
26770Sstevel@tonic-gate 	encrlen = STRUCT_FGET(encrypt_update, eu_encrlen);
26780Sstevel@tonic-gate 
26790Sstevel@tonic-gate 	/*
26800Sstevel@tonic-gate 	 * Don't allocate output buffer unless both buffer pointer and
26810Sstevel@tonic-gate 	 * buffer length are not NULL or 0 (length).
26820Sstevel@tonic-gate 	 */
26830Sstevel@tonic-gate 	encrbuf = STRUCT_FGETP(encrypt_update, eu_encrbuf);
26840Sstevel@tonic-gate 	if (encrbuf == NULL || encrlen == 0) {
26850Sstevel@tonic-gate 		encrlen = 0;
26860Sstevel@tonic-gate 	}
26870Sstevel@tonic-gate 
26880Sstevel@tonic-gate 	if (datalen > crypto_max_buffer_len ||
26890Sstevel@tonic-gate 	    encrlen > crypto_max_buffer_len) {
26900Sstevel@tonic-gate 		cmn_err(CE_NOTE, "cipher_update: buffer greater than %ld "
26910Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
26920Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
26936424Skrishna 		goto out;
26946424Skrishna 	}
26956424Skrishna 
26966424Skrishna 	session_id = STRUCT_FGET(encrypt_update, eu_session);
26976424Skrishna 
26986424Skrishna 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv))  {
26996424Skrishna 		goto out;
27000Sstevel@tonic-gate 	}
27010Sstevel@tonic-gate 
27020Sstevel@tonic-gate 	need = datalen + encrlen;
27036424Skrishna 
27046424Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, need, rctl_chk)) !=
27056424Skrishna 	    CRYPTO_SUCCESS) {
27060Sstevel@tonic-gate 		need = 0;
27076424Skrishna 		goto out;
27080Sstevel@tonic-gate 	}
27090Sstevel@tonic-gate 
27100Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(data, datalen);
27110Sstevel@tonic-gate 	data.cd_miscdata = NULL;
27120Sstevel@tonic-gate 
27130Sstevel@tonic-gate 	if (datalen != 0 && copyin(STRUCT_FGETP(encrypt_update, eu_databuf),
27140Sstevel@tonic-gate 	    data.cd_raw.iov_base, datalen) != 0) {
27150Sstevel@tonic-gate 		error = EFAULT;
27166424Skrishna 		goto out;
27170Sstevel@tonic-gate 	}
27180Sstevel@tonic-gate 
27190Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(encr, encrlen);
27200Sstevel@tonic-gate 
27210Sstevel@tonic-gate 	ctxpp = (update == crypto_encrypt_update) ?
27220Sstevel@tonic-gate 	    &sp->sd_encr_ctx : &sp->sd_decr_ctx;
27230Sstevel@tonic-gate 
27240Sstevel@tonic-gate 	rv = (update)(*ctxpp, &data, &encr, NULL);
27250Sstevel@tonic-gate 
27260Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS || rv == CRYPTO_BUFFER_TOO_SMALL) {
27270Sstevel@tonic-gate 		if (rv == CRYPTO_SUCCESS) {
27280Sstevel@tonic-gate 			ASSERT(encr.cd_length <= encrlen);
27290Sstevel@tonic-gate 			if (encr.cd_length != 0 && copyout(encr.cd_raw.iov_base,
27300Sstevel@tonic-gate 			    encrbuf, encr.cd_length) != 0) {
27310Sstevel@tonic-gate 				error = EFAULT;
27320Sstevel@tonic-gate 				goto out;
27330Sstevel@tonic-gate 			}
27340Sstevel@tonic-gate 		} else {
27350Sstevel@tonic-gate 			/*
27360Sstevel@tonic-gate 			 * The providers return CRYPTO_BUFFER_TOO_SMALL even
27370Sstevel@tonic-gate 			 * for case 1 of section 11.2 of the pkcs11 spec.
27380Sstevel@tonic-gate 			 * We catch it here and provide the correct pkcs11
27390Sstevel@tonic-gate 			 * return value.
27400Sstevel@tonic-gate 			 */
27410Sstevel@tonic-gate 			if (STRUCT_FGETP(encrypt_update, eu_encrbuf) == NULL)
27420Sstevel@tonic-gate 				rv = CRYPTO_SUCCESS;
27430Sstevel@tonic-gate 		}
27440Sstevel@tonic-gate 		STRUCT_FSET(encrypt_update, eu_encrlen, encr.cd_length);
27450Sstevel@tonic-gate 	} else {
27460Sstevel@tonic-gate 		CRYPTO_CANCEL_CTX(ctxpp);
27470Sstevel@tonic-gate 	}
27480Sstevel@tonic-gate out:
27496424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
27500Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
27510Sstevel@tonic-gate 	crypto_release_minor(cm);
27520Sstevel@tonic-gate 
27530Sstevel@tonic-gate 	if (data.cd_raw.iov_base != NULL)
27540Sstevel@tonic-gate 		kmem_free(data.cd_raw.iov_base, datalen);
27550Sstevel@tonic-gate 
27560Sstevel@tonic-gate 	if (encr.cd_raw.iov_base != NULL)
27570Sstevel@tonic-gate 		kmem_free(encr.cd_raw.iov_base, encrlen);
27580Sstevel@tonic-gate 
27590Sstevel@tonic-gate 	if (error != 0)
27600Sstevel@tonic-gate 		return (error);
27610Sstevel@tonic-gate 
27620Sstevel@tonic-gate 	STRUCT_FSET(encrypt_update, eu_return_value, rv);
27630Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(encrypt_update), arg,
27640Sstevel@tonic-gate 	    STRUCT_SIZE(encrypt_update)) != 0) {
27650Sstevel@tonic-gate 		return (EFAULT);
27660Sstevel@tonic-gate 	}
27670Sstevel@tonic-gate 	return (0);
27680Sstevel@tonic-gate }
27690Sstevel@tonic-gate 
27700Sstevel@tonic-gate /* ARGSUSED */
27710Sstevel@tonic-gate static int
27720Sstevel@tonic-gate encrypt_final(dev_t dev, caddr_t arg, int mode, int *rval)
27730Sstevel@tonic-gate {
27740Sstevel@tonic-gate 	return (common_final(dev, arg, mode, crypto_encrypt_final));
27750Sstevel@tonic-gate }
27760Sstevel@tonic-gate 
27770Sstevel@tonic-gate /* ARGSUSED */
27780Sstevel@tonic-gate static int
27790Sstevel@tonic-gate decrypt_final(dev_t dev, caddr_t arg, int mode, int *rval)
27800Sstevel@tonic-gate {
27810Sstevel@tonic-gate 	return (common_final(dev, arg, mode, crypto_decrypt_final));
27820Sstevel@tonic-gate }
27830Sstevel@tonic-gate 
27840Sstevel@tonic-gate /*
27850Sstevel@tonic-gate  * ASSUMPTION: crypto_encrypt_final, crypto_decrypt_final, crypto_sign_final,
27860Sstevel@tonic-gate  * and crypto_digest_final structures are identical except for field names.
27870Sstevel@tonic-gate  */
27880Sstevel@tonic-gate static int
27890Sstevel@tonic-gate common_final(dev_t dev, caddr_t arg, int mode,
27900Sstevel@tonic-gate     int (*final)(crypto_context_t, crypto_data_t *, crypto_call_req_t *))
27910Sstevel@tonic-gate {
27920Sstevel@tonic-gate 	STRUCT_DECL(crypto_encrypt_final, encrypt_final);
27930Sstevel@tonic-gate 	crypto_session_id_t session_id;
27940Sstevel@tonic-gate 	crypto_minor_t *cm;
27956424Skrishna 	crypto_session_data_t *sp = NULL;
27960Sstevel@tonic-gate 	crypto_ctx_t **ctxpp;
27970Sstevel@tonic-gate 	crypto_data_t encr;
27980Sstevel@tonic-gate 	size_t encrlen, need = 0;
27990Sstevel@tonic-gate 	char *encrbuf;
28000Sstevel@tonic-gate 	int error = 0;
28010Sstevel@tonic-gate 	int rv;
28026424Skrishna 	boolean_t rctl_chk = B_FALSE;
28030Sstevel@tonic-gate 
28040Sstevel@tonic-gate 	STRUCT_INIT(encrypt_final, mode);
28050Sstevel@tonic-gate 
28060Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
28070Sstevel@tonic-gate 		cmn_err(CE_WARN, "common_final: failed holding minor");
28080Sstevel@tonic-gate 		return (ENXIO);
28090Sstevel@tonic-gate 	}
28100Sstevel@tonic-gate 
28110Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(encrypt_final),
28120Sstevel@tonic-gate 	    STRUCT_SIZE(encrypt_final)) != 0) {
28130Sstevel@tonic-gate 		crypto_release_minor(cm);
28140Sstevel@tonic-gate 		return (EFAULT);
28150Sstevel@tonic-gate 	}
28160Sstevel@tonic-gate 
28170Sstevel@tonic-gate 	encr.cd_format = CRYPTO_DATA_RAW;
28180Sstevel@tonic-gate 	encr.cd_raw.iov_base = NULL;
28190Sstevel@tonic-gate 
28200Sstevel@tonic-gate 	encrlen = STRUCT_FGET(encrypt_final, ef_encrlen);
28210Sstevel@tonic-gate 
28220Sstevel@tonic-gate 	/*
28230Sstevel@tonic-gate 	 * Don't allocate output buffer unless both buffer pointer and
28240Sstevel@tonic-gate 	 * buffer length are not NULL or 0 (length).
28250Sstevel@tonic-gate 	 */
28260Sstevel@tonic-gate 	encrbuf = STRUCT_FGETP(encrypt_final, ef_encrbuf);
28270Sstevel@tonic-gate 	if (encrbuf == NULL || encrlen == 0) {
28280Sstevel@tonic-gate 		encrlen = 0;
28290Sstevel@tonic-gate 	}
28300Sstevel@tonic-gate 
28310Sstevel@tonic-gate 	if (encrlen > crypto_max_buffer_len) {
28320Sstevel@tonic-gate 		cmn_err(CE_NOTE, "common_final: buffer greater than %ld "
28330Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
28340Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
28350Sstevel@tonic-gate 		goto release_minor;
28360Sstevel@tonic-gate 	}
28370Sstevel@tonic-gate 
28386424Skrishna 	session_id = STRUCT_FGET(encrypt_final, ef_session);
28396424Skrishna 
28406424Skrishna 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
28416424Skrishna 		goto release_minor;
28426424Skrishna 	}
28436424Skrishna 
28446424Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, encrlen, rctl_chk)) !=
28456424Skrishna 	    CRYPTO_SUCCESS) {
28460Sstevel@tonic-gate 		goto release_minor;
28470Sstevel@tonic-gate 	}
28480Sstevel@tonic-gate 	need = encrlen;
28490Sstevel@tonic-gate 	encr.cd_raw.iov_base = kmem_alloc(encrlen, KM_SLEEP);
28500Sstevel@tonic-gate 	encr.cd_raw.iov_len = encrlen;
28510Sstevel@tonic-gate 
28520Sstevel@tonic-gate 	encr.cd_offset = 0;
28530Sstevel@tonic-gate 	encr.cd_length = encrlen;
28540Sstevel@tonic-gate 
28550Sstevel@tonic-gate 	ASSERT(final == crypto_encrypt_final ||
28560Sstevel@tonic-gate 	    final == crypto_decrypt_final || final == crypto_sign_final ||
28570Sstevel@tonic-gate 	    final == crypto_digest_final);
28580Sstevel@tonic-gate 
28590Sstevel@tonic-gate 	if (final == crypto_encrypt_final) {
28600Sstevel@tonic-gate 		ctxpp = &sp->sd_encr_ctx;
28610Sstevel@tonic-gate 	} else if (final == crypto_decrypt_final) {
28620Sstevel@tonic-gate 		ctxpp = &sp->sd_decr_ctx;
28630Sstevel@tonic-gate 	} else if (final == crypto_sign_final) {
28640Sstevel@tonic-gate 		ctxpp = &sp->sd_sign_ctx;
28650Sstevel@tonic-gate 	} else {
28660Sstevel@tonic-gate 		ctxpp = &sp->sd_digest_ctx;
28670Sstevel@tonic-gate 	}
28680Sstevel@tonic-gate 
28690Sstevel@tonic-gate 	rv = (final)(*ctxpp, &encr, NULL);
28700Sstevel@tonic-gate 	if (KCF_CONTEXT_DONE(rv))
28710Sstevel@tonic-gate 		*ctxpp = NULL;
28720Sstevel@tonic-gate 
28730Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
28740Sstevel@tonic-gate 		ASSERT(encr.cd_length <= encrlen);
28750Sstevel@tonic-gate 		if (encr.cd_length != 0 && copyout(encr.cd_raw.iov_base,
28760Sstevel@tonic-gate 		    encrbuf, encr.cd_length) != 0) {
28770Sstevel@tonic-gate 			error = EFAULT;
28780Sstevel@tonic-gate 			goto release_minor;
28790Sstevel@tonic-gate 		}
28800Sstevel@tonic-gate 		STRUCT_FSET(encrypt_final, ef_encrlen, encr.cd_length);
28810Sstevel@tonic-gate 	}
28820Sstevel@tonic-gate 
28830Sstevel@tonic-gate 	if (rv == CRYPTO_BUFFER_TOO_SMALL) {
28840Sstevel@tonic-gate 		/*
28850Sstevel@tonic-gate 		 * The providers return CRYPTO_BUFFER_TOO_SMALL even for case 1
28860Sstevel@tonic-gate 		 * of section 11.2 of the pkcs11 spec. We catch it here and
28870Sstevel@tonic-gate 		 * provide the correct pkcs11 return value.
28880Sstevel@tonic-gate 		 */
28890Sstevel@tonic-gate 		if (STRUCT_FGETP(encrypt_final, ef_encrbuf) == NULL)
28900Sstevel@tonic-gate 			rv = CRYPTO_SUCCESS;
28910Sstevel@tonic-gate 		STRUCT_FSET(encrypt_final, ef_encrlen, encr.cd_length);
28920Sstevel@tonic-gate 	}
28930Sstevel@tonic-gate 
28940Sstevel@tonic-gate release_minor:
28956424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
28966424Skrishna 	CRYPTO_SESSION_RELE(sp);
28970Sstevel@tonic-gate 	crypto_release_minor(cm);
28980Sstevel@tonic-gate 
28990Sstevel@tonic-gate 	if (encr.cd_raw.iov_base != NULL)
29000Sstevel@tonic-gate 		kmem_free(encr.cd_raw.iov_base, encrlen);
29010Sstevel@tonic-gate 
29020Sstevel@tonic-gate 	if (error != 0)
29030Sstevel@tonic-gate 		return (error);
29040Sstevel@tonic-gate 
29050Sstevel@tonic-gate 	STRUCT_FSET(encrypt_final, ef_return_value, rv);
29060Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(encrypt_final), arg,
29070Sstevel@tonic-gate 	    STRUCT_SIZE(encrypt_final)) != 0) {
29080Sstevel@tonic-gate 		return (EFAULT);
29090Sstevel@tonic-gate 	}
29100Sstevel@tonic-gate 	return (0);
29110Sstevel@tonic-gate }
29120Sstevel@tonic-gate 
29130Sstevel@tonic-gate /* ARGSUSED */
29140Sstevel@tonic-gate static int
29150Sstevel@tonic-gate digest_init(dev_t dev, caddr_t arg, int mode, int *rval)
29160Sstevel@tonic-gate {
29170Sstevel@tonic-gate 	STRUCT_DECL(crypto_digest_init, digest_init);
2918904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
29190Sstevel@tonic-gate 	crypto_session_id_t session_id;
29200Sstevel@tonic-gate 	crypto_mechanism_t mech;
29210Sstevel@tonic-gate 	crypto_minor_t *cm;
29226424Skrishna 	crypto_session_data_t *sp = NULL;
29230Sstevel@tonic-gate 	crypto_context_t cc;
29240Sstevel@tonic-gate 	size_t rctl_bytes = 0;
29256424Skrishna 	boolean_t rctl_chk = B_FALSE;
29260Sstevel@tonic-gate 	int error = 0;
29270Sstevel@tonic-gate 	int rv;
29280Sstevel@tonic-gate 
29290Sstevel@tonic-gate 	STRUCT_INIT(digest_init, mode);
29300Sstevel@tonic-gate 
29310Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
29320Sstevel@tonic-gate 		cmn_err(CE_WARN, "digest_init: failed holding minor");
29330Sstevel@tonic-gate 		return (ENXIO);
29340Sstevel@tonic-gate 	}
29350Sstevel@tonic-gate 
29360Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(digest_init),
29370Sstevel@tonic-gate 	    STRUCT_SIZE(digest_init)) != 0) {
29380Sstevel@tonic-gate 		crypto_release_minor(cm);
29390Sstevel@tonic-gate 		return (EFAULT);
29400Sstevel@tonic-gate 	}
29410Sstevel@tonic-gate 
29420Sstevel@tonic-gate 	mech.cm_param = NULL;
29430Sstevel@tonic-gate 
29440Sstevel@tonic-gate 	session_id = STRUCT_FGET(digest_init, di_session);
29450Sstevel@tonic-gate 
29460Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv))  {
29476424Skrishna 		goto out;
29486424Skrishna 	}
29496424Skrishna 
29506424Skrishna 	if (!copyin_mech(mode, sp, STRUCT_FADDR(digest_init, di_mech), &mech,
29516424Skrishna 	    &rctl_bytes, &rctl_chk, &rv, &error)) {
29520Sstevel@tonic-gate 		goto out;
29530Sstevel@tonic-gate 	}
29540Sstevel@tonic-gate 
2955904Smcpowers 	if ((rv = kcf_get_hardware_provider(mech.cm_type, CRYPTO_MECH_INVALID,
29561808Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider,
29571808Smcpowers 	    CRYPTO_FG_DIGEST)) != CRYPTO_SUCCESS) {
29580Sstevel@tonic-gate 		goto out;
29590Sstevel@tonic-gate 	}
29600Sstevel@tonic-gate 
29610Sstevel@tonic-gate 	rv = crypto_digest_init_prov(real_provider,
29620Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, &mech, &cc, NULL);
29630Sstevel@tonic-gate 
29640Sstevel@tonic-gate 	/*
29650Sstevel@tonic-gate 	 * Check if a context already exists. If so, it means it is being
29660Sstevel@tonic-gate 	 * abandoned. So, cancel it to avoid leaking it.
29670Sstevel@tonic-gate 	 */
29680Sstevel@tonic-gate 	if (sp->sd_digest_ctx != NULL)
29690Sstevel@tonic-gate 		CRYPTO_CANCEL_CTX(&sp->sd_digest_ctx);
29700Sstevel@tonic-gate 	sp->sd_digest_ctx = (rv == CRYPTO_SUCCESS) ? cc : NULL;
29710Sstevel@tonic-gate out:
29726424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk);
29730Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
29740Sstevel@tonic-gate 	crypto_release_minor(cm);
29750Sstevel@tonic-gate 
2976904Smcpowers 	if (real_provider != NULL)
2977904Smcpowers 		KCF_PROV_REFRELE(real_provider);
2978904Smcpowers 
29790Sstevel@tonic-gate 	if (mech.cm_param != NULL)
29800Sstevel@tonic-gate 		kmem_free(mech.cm_param, mech.cm_param_len);
29810Sstevel@tonic-gate 
29820Sstevel@tonic-gate 	if (error != 0)
29830Sstevel@tonic-gate 		return (error);
29840Sstevel@tonic-gate 
29850Sstevel@tonic-gate 	STRUCT_FSET(digest_init, di_return_value, rv);
29860Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(digest_init), arg,
29870Sstevel@tonic-gate 	    STRUCT_SIZE(digest_init)) != 0) {
29880Sstevel@tonic-gate 		return (EFAULT);
29890Sstevel@tonic-gate 	}
29900Sstevel@tonic-gate 	return (0);
29910Sstevel@tonic-gate }
29920Sstevel@tonic-gate 
29930Sstevel@tonic-gate /* ARGSUSED */
29940Sstevel@tonic-gate static int
29950Sstevel@tonic-gate digest_update(dev_t dev, caddr_t arg, int mode, int *rval)
29960Sstevel@tonic-gate {
29970Sstevel@tonic-gate 	STRUCT_DECL(crypto_digest_update, digest_update);
29980Sstevel@tonic-gate 	crypto_session_id_t session_id;
29990Sstevel@tonic-gate 	crypto_minor_t *cm;
30006424Skrishna 	crypto_session_data_t *sp = NULL;
30010Sstevel@tonic-gate 	crypto_data_t data;
30020Sstevel@tonic-gate 	size_t datalen, need = 0;
30030Sstevel@tonic-gate 	int error = 0;
30040Sstevel@tonic-gate 	int rv;
30056424Skrishna 	boolean_t rctl_chk = B_FALSE;
30060Sstevel@tonic-gate 
30070Sstevel@tonic-gate 	STRUCT_INIT(digest_update, mode);
30080Sstevel@tonic-gate 
30090Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
30100Sstevel@tonic-gate 		cmn_err(CE_WARN, "digest_update: failed holding minor");
30110Sstevel@tonic-gate 		return (ENXIO);
30120Sstevel@tonic-gate 	}
30130Sstevel@tonic-gate 
30140Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(digest_update),
30150Sstevel@tonic-gate 	    STRUCT_SIZE(digest_update)) != 0) {
30160Sstevel@tonic-gate 		crypto_release_minor(cm);
30170Sstevel@tonic-gate 		return (EFAULT);
30180Sstevel@tonic-gate 	}
30190Sstevel@tonic-gate 
30200Sstevel@tonic-gate 	data.cd_format = CRYPTO_DATA_RAW;
30210Sstevel@tonic-gate 	data.cd_raw.iov_base = NULL;
30220Sstevel@tonic-gate 
30230Sstevel@tonic-gate 	datalen = STRUCT_FGET(digest_update, du_datalen);
30240Sstevel@tonic-gate 	if (datalen > crypto_max_buffer_len) {
30250Sstevel@tonic-gate 		cmn_err(CE_NOTE, "digest_update: buffer greater than %ld "
30260Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
30270Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
30280Sstevel@tonic-gate 		goto release_minor;
30290Sstevel@tonic-gate 	}
30300Sstevel@tonic-gate 
30316424Skrishna 	session_id = STRUCT_FGET(digest_update, du_session);
30326424Skrishna 
30336424Skrishna 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv))  {
30340Sstevel@tonic-gate 		goto release_minor;
30350Sstevel@tonic-gate 	}
30366424Skrishna 
30376424Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, datalen, rctl_chk)) !=
30386424Skrishna 	    CRYPTO_SUCCESS) {
30396424Skrishna 		goto release_minor;
30406424Skrishna 	}
30416424Skrishna 
30420Sstevel@tonic-gate 	need = datalen;
30430Sstevel@tonic-gate 	data.cd_raw.iov_base = kmem_alloc(datalen, KM_SLEEP);
30440Sstevel@tonic-gate 	data.cd_raw.iov_len = datalen;
30450Sstevel@tonic-gate 
30460Sstevel@tonic-gate 	if (datalen != 0 && copyin(STRUCT_FGETP(digest_update, du_databuf),
30470Sstevel@tonic-gate 	    data.cd_raw.iov_base, datalen) != 0) {
30480Sstevel@tonic-gate 		error = EFAULT;
30490Sstevel@tonic-gate 		goto release_minor;
30500Sstevel@tonic-gate 	}
30510Sstevel@tonic-gate 
30520Sstevel@tonic-gate 	data.cd_offset = 0;
30530Sstevel@tonic-gate 	data.cd_length = datalen;
30540Sstevel@tonic-gate 
30550Sstevel@tonic-gate 	rv = crypto_digest_update(sp->sd_digest_ctx, &data, NULL);
30560Sstevel@tonic-gate 	if (rv != CRYPTO_SUCCESS)
30570Sstevel@tonic-gate 		CRYPTO_CANCEL_CTX(&sp->sd_digest_ctx);
30580Sstevel@tonic-gate 
30590Sstevel@tonic-gate release_minor:
30606424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
30616424Skrishna 	CRYPTO_SESSION_RELE(sp);
30620Sstevel@tonic-gate 	crypto_release_minor(cm);
30630Sstevel@tonic-gate 
30640Sstevel@tonic-gate 	if (data.cd_raw.iov_base != NULL)
30650Sstevel@tonic-gate 		kmem_free(data.cd_raw.iov_base, datalen);
30660Sstevel@tonic-gate 
30670Sstevel@tonic-gate 	if (error != 0)
30680Sstevel@tonic-gate 		return (error);
30690Sstevel@tonic-gate 
30700Sstevel@tonic-gate 	STRUCT_FSET(digest_update, du_return_value, rv);
30710Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(digest_update), arg,
30720Sstevel@tonic-gate 	    STRUCT_SIZE(digest_update)) != 0) {
30730Sstevel@tonic-gate 		return (EFAULT);
30740Sstevel@tonic-gate 	}
30750Sstevel@tonic-gate 	return (0);
30760Sstevel@tonic-gate }
30770Sstevel@tonic-gate 
30780Sstevel@tonic-gate /* ARGSUSED */
30790Sstevel@tonic-gate static int
30800Sstevel@tonic-gate digest_key(dev_t dev, caddr_t arg, int mode, int *rval)
30810Sstevel@tonic-gate {
30820Sstevel@tonic-gate 	STRUCT_DECL(crypto_digest_key, digest_key);
30830Sstevel@tonic-gate 	crypto_session_id_t session_id;
30840Sstevel@tonic-gate 	crypto_key_t key;
30850Sstevel@tonic-gate 	crypto_minor_t *cm;
30866424Skrishna 	crypto_session_data_t *sp = NULL;
30870Sstevel@tonic-gate 	size_t rctl_bytes = 0;
30886424Skrishna 	boolean_t key_rctl_chk = B_FALSE;
30890Sstevel@tonic-gate 	int error = 0;
30900Sstevel@tonic-gate 	int rv;
30910Sstevel@tonic-gate 
30920Sstevel@tonic-gate 	STRUCT_INIT(digest_key, mode);
30930Sstevel@tonic-gate 
30940Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
30950Sstevel@tonic-gate 		cmn_err(CE_WARN, "digest_key: failed holding minor");
30960Sstevel@tonic-gate 		return (ENXIO);
30970Sstevel@tonic-gate 	}
30980Sstevel@tonic-gate 
30990Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(digest_key), STRUCT_SIZE(digest_key)) != 0) {
31000Sstevel@tonic-gate 		crypto_release_minor(cm);
31010Sstevel@tonic-gate 		return (EFAULT);
31020Sstevel@tonic-gate 	}
31030Sstevel@tonic-gate 
31040Sstevel@tonic-gate 	bzero(&key, sizeof (crypto_key_t));
31050Sstevel@tonic-gate 
31060Sstevel@tonic-gate 	session_id = STRUCT_FGET(digest_key, dk_session);
31070Sstevel@tonic-gate 
31080Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv))  {
31096424Skrishna 		goto out;
31106424Skrishna 	}
31116424Skrishna 
31126424Skrishna 	if (!copyin_key(mode, sp, STRUCT_FADDR(digest_key, dk_key), &key,
31136424Skrishna 	    &rctl_bytes, &key_rctl_chk, &rv, &error)) {
31140Sstevel@tonic-gate 		goto out;
31150Sstevel@tonic-gate 	}
31160Sstevel@tonic-gate 
31170Sstevel@tonic-gate 	rv = crypto_digest_key_prov(sp->sd_digest_ctx, &key, NULL);
31180Sstevel@tonic-gate 	if (rv != CRYPTO_SUCCESS)
31190Sstevel@tonic-gate 		CRYPTO_CANCEL_CTX(&sp->sd_digest_ctx);
31200Sstevel@tonic-gate out:
31216424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, key_rctl_chk);
31220Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
31230Sstevel@tonic-gate 	crypto_release_minor(cm);
31240Sstevel@tonic-gate 
31250Sstevel@tonic-gate 	free_crypto_key(&key);
31260Sstevel@tonic-gate 
31270Sstevel@tonic-gate 	if (error != 0)
31280Sstevel@tonic-gate 		return (error);
31290Sstevel@tonic-gate 
31300Sstevel@tonic-gate 	STRUCT_FSET(digest_key, dk_return_value, rv);
31310Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(digest_key), arg,
31320Sstevel@tonic-gate 	    STRUCT_SIZE(digest_key)) != 0) {
31330Sstevel@tonic-gate 		return (EFAULT);
31340Sstevel@tonic-gate 	}
31350Sstevel@tonic-gate 	return (0);
31360Sstevel@tonic-gate }
31370Sstevel@tonic-gate 
31380Sstevel@tonic-gate /* ARGSUSED */
31390Sstevel@tonic-gate static int
31400Sstevel@tonic-gate digest_final(dev_t dev, caddr_t arg, int mode, int *rval)
31410Sstevel@tonic-gate {
31420Sstevel@tonic-gate 	return (common_final(dev, arg, mode, crypto_digest_final));
31430Sstevel@tonic-gate }
31440Sstevel@tonic-gate 
31450Sstevel@tonic-gate /* ARGSUSED */
31460Sstevel@tonic-gate static int
31470Sstevel@tonic-gate digest(dev_t dev, caddr_t arg, int mode, int *rval)
31480Sstevel@tonic-gate {
31490Sstevel@tonic-gate 	return (common_digest(dev, arg, mode, crypto_digest_single));
31500Sstevel@tonic-gate }
31510Sstevel@tonic-gate 
31520Sstevel@tonic-gate /*
31530Sstevel@tonic-gate  * ASSUMPTION: crypto_digest, crypto_sign, crypto_sign_recover,
31540Sstevel@tonic-gate  * and crypto_verify_recover are identical except for field names.
31550Sstevel@tonic-gate  */
31560Sstevel@tonic-gate static int
31570Sstevel@tonic-gate common_digest(dev_t dev, caddr_t arg, int mode,
31580Sstevel@tonic-gate     int (*single)(crypto_context_t, crypto_data_t *, crypto_data_t *,
31590Sstevel@tonic-gate     crypto_call_req_t *))
31600Sstevel@tonic-gate {
31610Sstevel@tonic-gate 	STRUCT_DECL(crypto_digest, crypto_digest);
31620Sstevel@tonic-gate 	crypto_session_id_t session_id;
31630Sstevel@tonic-gate 	crypto_minor_t *cm;
31646424Skrishna 	crypto_session_data_t *sp = NULL;
31650Sstevel@tonic-gate 	crypto_data_t data, digest;
31660Sstevel@tonic-gate 	crypto_ctx_t **ctxpp;
31670Sstevel@tonic-gate 	size_t datalen, digestlen, need = 0;
31680Sstevel@tonic-gate 	char *digestbuf;
31690Sstevel@tonic-gate 	int error = 0;
31700Sstevel@tonic-gate 	int rv;
31716424Skrishna 	boolean_t rctl_chk = B_FALSE;
31720Sstevel@tonic-gate 
31730Sstevel@tonic-gate 	STRUCT_INIT(crypto_digest, mode);
31740Sstevel@tonic-gate 
31750Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
31760Sstevel@tonic-gate 		cmn_err(CE_WARN, "common_digest: failed holding minor");
31770Sstevel@tonic-gate 		return (ENXIO);
31780Sstevel@tonic-gate 	}
31790Sstevel@tonic-gate 
31800Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(crypto_digest),
31810Sstevel@tonic-gate 	    STRUCT_SIZE(crypto_digest)) != 0) {
31820Sstevel@tonic-gate 		crypto_release_minor(cm);
31830Sstevel@tonic-gate 		return (EFAULT);
31840Sstevel@tonic-gate 	}
31850Sstevel@tonic-gate 
31860Sstevel@tonic-gate 	data.cd_raw.iov_base = NULL;
31870Sstevel@tonic-gate 	digest.cd_raw.iov_base = NULL;
31880Sstevel@tonic-gate 
31890Sstevel@tonic-gate 	datalen = STRUCT_FGET(crypto_digest, cd_datalen);
31900Sstevel@tonic-gate 	digestlen = STRUCT_FGET(crypto_digest, cd_digestlen);
31910Sstevel@tonic-gate 
31920Sstevel@tonic-gate 	/*
31930Sstevel@tonic-gate 	 * Don't allocate output buffer unless both buffer pointer and
31940Sstevel@tonic-gate 	 * buffer length are not NULL or 0 (length).
31950Sstevel@tonic-gate 	 */
31960Sstevel@tonic-gate 	digestbuf = STRUCT_FGETP(crypto_digest, cd_digestbuf);
31970Sstevel@tonic-gate 	if (digestbuf == NULL || digestlen == 0) {
31980Sstevel@tonic-gate 		digestlen = 0;
31990Sstevel@tonic-gate 	}
32000Sstevel@tonic-gate 
32010Sstevel@tonic-gate 	if (datalen > crypto_max_buffer_len ||
32020Sstevel@tonic-gate 	    digestlen > crypto_max_buffer_len) {
32030Sstevel@tonic-gate 		cmn_err(CE_NOTE, "common_digest: buffer greater than %ld "
32040Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
32050Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
32060Sstevel@tonic-gate 		goto release_minor;
32070Sstevel@tonic-gate 	}
32080Sstevel@tonic-gate 
32096424Skrishna 	session_id = STRUCT_FGET(crypto_digest, cd_session);
32106424Skrishna 
32116424Skrishna 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv))  {
32126424Skrishna 		goto release_minor;
32136424Skrishna 	}
32146424Skrishna 
32150Sstevel@tonic-gate 	need = datalen + digestlen;
32166424Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, need, rctl_chk)) !=
32176424Skrishna 	    CRYPTO_SUCCESS) {
32180Sstevel@tonic-gate 		need = 0;
32190Sstevel@tonic-gate 		goto release_minor;
32200Sstevel@tonic-gate 	}
32210Sstevel@tonic-gate 
32220Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(data, datalen);
32230Sstevel@tonic-gate 
32240Sstevel@tonic-gate 	if (datalen != 0 && copyin(STRUCT_FGETP(crypto_digest, cd_databuf),
32250Sstevel@tonic-gate 	    data.cd_raw.iov_base, datalen) != 0) {
32260Sstevel@tonic-gate 		error = EFAULT;
32270Sstevel@tonic-gate 		goto release_minor;
32280Sstevel@tonic-gate 	}
32290Sstevel@tonic-gate 
32300Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(digest, digestlen);
32310Sstevel@tonic-gate 
32320Sstevel@tonic-gate 	ASSERT(single == crypto_digest_single ||
32330Sstevel@tonic-gate 	    single == crypto_sign_single ||
32340Sstevel@tonic-gate 	    single == crypto_verify_recover_single ||
32350Sstevel@tonic-gate 	    single == crypto_sign_recover_single);
32360Sstevel@tonic-gate 
32370Sstevel@tonic-gate 	if (single == crypto_digest_single) {
32380Sstevel@tonic-gate 		ctxpp = &sp->sd_digest_ctx;
32390Sstevel@tonic-gate 	} else if (single == crypto_sign_single) {
32400Sstevel@tonic-gate 		ctxpp = &sp->sd_sign_ctx;
32410Sstevel@tonic-gate 	} else if (single == crypto_verify_recover_single) {
32420Sstevel@tonic-gate 		ctxpp = &sp->sd_verify_recover_ctx;
32430Sstevel@tonic-gate 	} else {
32440Sstevel@tonic-gate 		ctxpp = &sp->sd_sign_recover_ctx;
32450Sstevel@tonic-gate 	}
32460Sstevel@tonic-gate 	rv = (single)(*ctxpp, &data, &digest, NULL);
32470Sstevel@tonic-gate 	if (KCF_CONTEXT_DONE(rv))
32480Sstevel@tonic-gate 		*ctxpp = NULL;
32490Sstevel@tonic-gate 
32500Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
32510Sstevel@tonic-gate 		ASSERT(digest.cd_length <= digestlen);
32520Sstevel@tonic-gate 		if (digest.cd_length != 0 && copyout(digest.cd_raw.iov_base,
32530Sstevel@tonic-gate 		    digestbuf, digest.cd_length) != 0) {
32540Sstevel@tonic-gate 			error = EFAULT;
32550Sstevel@tonic-gate 			goto release_minor;
32560Sstevel@tonic-gate 		}
32570Sstevel@tonic-gate 		STRUCT_FSET(crypto_digest, cd_digestlen, digest.cd_length);
32580Sstevel@tonic-gate 	}
32590Sstevel@tonic-gate 
32600Sstevel@tonic-gate 	if (rv == CRYPTO_BUFFER_TOO_SMALL) {
32610Sstevel@tonic-gate 		/*
32620Sstevel@tonic-gate 		 * The providers return CRYPTO_BUFFER_TOO_SMALL even for case 1
32630Sstevel@tonic-gate 		 * of section 11.2 of the pkcs11 spec. We catch it here and
32640Sstevel@tonic-gate 		 * provide the correct pkcs11 return value.
32650Sstevel@tonic-gate 		 */
32660Sstevel@tonic-gate 		if (STRUCT_FGETP(crypto_digest, cd_digestbuf) == NULL)
32670Sstevel@tonic-gate 			rv = CRYPTO_SUCCESS;
32680Sstevel@tonic-gate 		STRUCT_FSET(crypto_digest, cd_digestlen, digest.cd_length);
32690Sstevel@tonic-gate 	}
32700Sstevel@tonic-gate 
32710Sstevel@tonic-gate release_minor:
32726424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
32736424Skrishna 	CRYPTO_SESSION_RELE(sp);
32740Sstevel@tonic-gate 	crypto_release_minor(cm);
32750Sstevel@tonic-gate 
32760Sstevel@tonic-gate 	if (data.cd_raw.iov_base != NULL)
32770Sstevel@tonic-gate 		kmem_free(data.cd_raw.iov_base, datalen);
32780Sstevel@tonic-gate 
32790Sstevel@tonic-gate 	if (digest.cd_raw.iov_base != NULL)
32800Sstevel@tonic-gate 		kmem_free(digest.cd_raw.iov_base, digestlen);
32810Sstevel@tonic-gate 
32820Sstevel@tonic-gate 	if (error != 0)
32830Sstevel@tonic-gate 		return (error);
32840Sstevel@tonic-gate 
32850Sstevel@tonic-gate 	STRUCT_FSET(crypto_digest, cd_return_value, rv);
32860Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(crypto_digest), arg,
32870Sstevel@tonic-gate 	    STRUCT_SIZE(crypto_digest)) != 0) {
32880Sstevel@tonic-gate 		return (EFAULT);
32890Sstevel@tonic-gate 	}
32900Sstevel@tonic-gate 	return (0);
32910Sstevel@tonic-gate }
32920Sstevel@tonic-gate 
32930Sstevel@tonic-gate /*
32940Sstevel@tonic-gate  * A helper function that does what the name suggests.
32950Sstevel@tonic-gate  * Returns 0 on success and non-zero otherwise.
32960Sstevel@tonic-gate  * On failure, out_pin is set to 0.
32970Sstevel@tonic-gate  */
32980Sstevel@tonic-gate int
32990Sstevel@tonic-gate get_pin_and_session_ptr(char *in_pin, char **out_pin, size_t pin_len,
33000Sstevel@tonic-gate     crypto_minor_t *cm, crypto_session_id_t sid, crypto_session_data_t **sp,
33010Sstevel@tonic-gate     int *rv, int *error)
33020Sstevel@tonic-gate {
33030Sstevel@tonic-gate 	char *tmp_pin = NULL;
33040Sstevel@tonic-gate 	int tmp_error = 0, tmp_rv = 0;
33050Sstevel@tonic-gate 
33060Sstevel@tonic-gate 	if (pin_len > KCF_MAX_PIN_LEN) {
33070Sstevel@tonic-gate 		tmp_rv = CRYPTO_PIN_LEN_RANGE;
33080Sstevel@tonic-gate 		goto out;
33090Sstevel@tonic-gate 	}
33100Sstevel@tonic-gate 	tmp_pin = kmem_alloc(pin_len, KM_SLEEP);
33110Sstevel@tonic-gate 
33120Sstevel@tonic-gate 	if (pin_len != 0 && copyin(in_pin, tmp_pin, pin_len) != 0) {
33130Sstevel@tonic-gate 		tmp_error = EFAULT;
33140Sstevel@tonic-gate 		goto out;
33150Sstevel@tonic-gate 	}
33160Sstevel@tonic-gate 
33170Sstevel@tonic-gate 	(void) get_session_ptr(sid, cm, sp, &tmp_error, &tmp_rv);
33180Sstevel@tonic-gate out:
33190Sstevel@tonic-gate 	*out_pin = tmp_pin;
33200Sstevel@tonic-gate 	*rv = tmp_rv;
33210Sstevel@tonic-gate 	*error = tmp_error;
33220Sstevel@tonic-gate 	return (tmp_rv | tmp_error);
33230Sstevel@tonic-gate }
33240Sstevel@tonic-gate 
33250Sstevel@tonic-gate /* ARGSUSED */
33260Sstevel@tonic-gate static int
33270Sstevel@tonic-gate set_pin(dev_t dev, caddr_t arg, int mode, int *rval)
33280Sstevel@tonic-gate {
33290Sstevel@tonic-gate 	STRUCT_DECL(crypto_set_pin, set_pin);
33300Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
33310Sstevel@tonic-gate 	kcf_req_params_t params;
33320Sstevel@tonic-gate 	crypto_minor_t *cm;
33330Sstevel@tonic-gate 	crypto_session_data_t *sp;
33340Sstevel@tonic-gate 	char *old_pin = NULL;
33350Sstevel@tonic-gate 	char *new_pin = NULL;
33360Sstevel@tonic-gate 	size_t old_pin_len;
33370Sstevel@tonic-gate 	size_t new_pin_len;
33380Sstevel@tonic-gate 	int error = 0;
33390Sstevel@tonic-gate 	int rv;
33400Sstevel@tonic-gate 
33410Sstevel@tonic-gate 	STRUCT_INIT(set_pin, mode);
33420Sstevel@tonic-gate 
33430Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
33440Sstevel@tonic-gate 		cmn_err(CE_WARN, "set_pin: failed holding minor");
33450Sstevel@tonic-gate 		return (ENXIO);
33460Sstevel@tonic-gate 	}
33470Sstevel@tonic-gate 
33480Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(set_pin),
33490Sstevel@tonic-gate 	    STRUCT_SIZE(set_pin)) != 0) {
33500Sstevel@tonic-gate 		crypto_release_minor(cm);
33510Sstevel@tonic-gate 		return (EFAULT);
33520Sstevel@tonic-gate 	}
33530Sstevel@tonic-gate 
33540Sstevel@tonic-gate 	old_pin_len = STRUCT_FGET(set_pin, sp_old_len);
33550Sstevel@tonic-gate 
33560Sstevel@tonic-gate 	if (get_pin_and_session_ptr(STRUCT_FGETP(set_pin, sp_old_pin),
33570Sstevel@tonic-gate 	    &old_pin, old_pin_len, cm, STRUCT_FGET(set_pin, sp_session),
33580Sstevel@tonic-gate 	    &sp, &rv, &error) != 0)
33590Sstevel@tonic-gate 		goto release_minor;
33600Sstevel@tonic-gate 
33610Sstevel@tonic-gate 	new_pin_len = STRUCT_FGET(set_pin, sp_new_len);
33620Sstevel@tonic-gate 	if (new_pin_len > KCF_MAX_PIN_LEN) {
33630Sstevel@tonic-gate 		rv = CRYPTO_PIN_LEN_RANGE;
33640Sstevel@tonic-gate 		goto out;
33650Sstevel@tonic-gate 	}
33660Sstevel@tonic-gate 	new_pin = kmem_alloc(new_pin_len, KM_SLEEP);
33670Sstevel@tonic-gate 
33680Sstevel@tonic-gate 	if (new_pin_len != 0 && copyin(STRUCT_FGETP(set_pin, sp_new_pin),
33690Sstevel@tonic-gate 	    new_pin, new_pin_len) != 0) {
33700Sstevel@tonic-gate 		error = EFAULT;
33710Sstevel@tonic-gate 		goto out;
33720Sstevel@tonic-gate 	}
33730Sstevel@tonic-gate 
33740Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
33750Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(provider_ops), CRYPTO_PROVIDER_OFFSET(set_pin),
3376904Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider))
3377904Smcpowers 	    != CRYPTO_SUCCESS) {
33780Sstevel@tonic-gate 		goto out;
33790Sstevel@tonic-gate 	}
33800Sstevel@tonic-gate 
33810Sstevel@tonic-gate 	KCF_WRAP_PROVMGMT_OPS_PARAMS(&params, KCF_OP_MGMT_SETPIN,
33820Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, old_pin, old_pin_len,
33830Sstevel@tonic-gate 	    new_pin, new_pin_len, NULL, NULL, real_provider);
33840Sstevel@tonic-gate 
33850Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
3386904Smcpowers 	KCF_PROV_REFRELE(real_provider);
33870Sstevel@tonic-gate 
33880Sstevel@tonic-gate out:
33890Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
33900Sstevel@tonic-gate 
33910Sstevel@tonic-gate release_minor:
33920Sstevel@tonic-gate 	crypto_release_minor(cm);
33930Sstevel@tonic-gate 
33940Sstevel@tonic-gate 	if (old_pin != NULL) {
33950Sstevel@tonic-gate 		bzero(old_pin, old_pin_len);
33960Sstevel@tonic-gate 		kmem_free(old_pin, old_pin_len);
33970Sstevel@tonic-gate 	}
33980Sstevel@tonic-gate 
33990Sstevel@tonic-gate 	if (new_pin != NULL) {
34000Sstevel@tonic-gate 		bzero(new_pin, new_pin_len);
34010Sstevel@tonic-gate 		kmem_free(new_pin, new_pin_len);
34020Sstevel@tonic-gate 	}
34030Sstevel@tonic-gate 
34040Sstevel@tonic-gate 	if (error != 0)
34050Sstevel@tonic-gate 		return (error);
34060Sstevel@tonic-gate 
34070Sstevel@tonic-gate 	STRUCT_FSET(set_pin, sp_return_value, rv);
34080Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(set_pin), arg, STRUCT_SIZE(set_pin)) != 0) {
34090Sstevel@tonic-gate 		return (EFAULT);
34100Sstevel@tonic-gate 	}
34110Sstevel@tonic-gate 	return (0);
34120Sstevel@tonic-gate }
34130Sstevel@tonic-gate 
34140Sstevel@tonic-gate /* ARGSUSED */
34150Sstevel@tonic-gate static int
34160Sstevel@tonic-gate login(dev_t dev, caddr_t arg, int mode, int *rval)
34170Sstevel@tonic-gate {
34180Sstevel@tonic-gate 	STRUCT_DECL(crypto_login, login);
34190Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
34200Sstevel@tonic-gate 	kcf_req_params_t params;
34210Sstevel@tonic-gate 	crypto_minor_t *cm;
34220Sstevel@tonic-gate 	crypto_session_data_t *sp;
34230Sstevel@tonic-gate 	size_t pin_len;
34240Sstevel@tonic-gate 	char *pin;
34250Sstevel@tonic-gate 	uint_t user_type;
34260Sstevel@tonic-gate 	int error = 0;
34270Sstevel@tonic-gate 	int rv;
34280Sstevel@tonic-gate 
34290Sstevel@tonic-gate 	STRUCT_INIT(login, mode);
34300Sstevel@tonic-gate 
34310Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
34320Sstevel@tonic-gate 		cmn_err(CE_WARN, "login: failed holding minor");
34330Sstevel@tonic-gate 		return (ENXIO);
34340Sstevel@tonic-gate 	}
34350Sstevel@tonic-gate 
34360Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(login), STRUCT_SIZE(login)) != 0) {
34370Sstevel@tonic-gate 		crypto_release_minor(cm);
34380Sstevel@tonic-gate 		return (EFAULT);
34390Sstevel@tonic-gate 	}
34400Sstevel@tonic-gate 
34410Sstevel@tonic-gate 	user_type = STRUCT_FGET(login, co_user_type);
34420Sstevel@tonic-gate 
34430Sstevel@tonic-gate 	pin_len = STRUCT_FGET(login, co_pin_len);
34440Sstevel@tonic-gate 
34450Sstevel@tonic-gate 	if (get_pin_and_session_ptr(STRUCT_FGETP(login, co_pin),
34460Sstevel@tonic-gate 	    &pin, pin_len, cm, STRUCT_FGET(login, co_session),
34470Sstevel@tonic-gate 	    &sp, &rv, &error) != 0) {
34480Sstevel@tonic-gate 		if (rv == CRYPTO_PIN_LEN_RANGE)
34490Sstevel@tonic-gate 			rv = CRYPTO_PIN_INCORRECT;
34500Sstevel@tonic-gate 		goto release_minor;
34510Sstevel@tonic-gate 	}
34520Sstevel@tonic-gate 
34530Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
34540Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(session_ops),
34550Sstevel@tonic-gate 	    CRYPTO_SESSION_OFFSET(session_login),
3456904Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider))
3457904Smcpowers 	    != CRYPTO_SUCCESS) {
34580Sstevel@tonic-gate 		goto out;
34590Sstevel@tonic-gate 	}
34600Sstevel@tonic-gate 
34610Sstevel@tonic-gate 	KCF_WRAP_SESSION_OPS_PARAMS(&params, KCF_OP_SESSION_LOGIN, NULL,
34620Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, user_type, pin, pin_len,
34630Sstevel@tonic-gate 	    real_provider);
34640Sstevel@tonic-gate 
34650Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
3466904Smcpowers 	KCF_PROV_REFRELE(real_provider);
34670Sstevel@tonic-gate 
34680Sstevel@tonic-gate out:
34690Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
34700Sstevel@tonic-gate 
34710Sstevel@tonic-gate release_minor:
34720Sstevel@tonic-gate 	crypto_release_minor(cm);
34730Sstevel@tonic-gate 
34740Sstevel@tonic-gate 	if (pin != NULL) {
34750Sstevel@tonic-gate 		bzero(pin, pin_len);
34760Sstevel@tonic-gate 		kmem_free(pin, pin_len);
34770Sstevel@tonic-gate 	}
34780Sstevel@tonic-gate 
34790Sstevel@tonic-gate 	if (error != 0)
34800Sstevel@tonic-gate 		return (error);
34810Sstevel@tonic-gate 
34820Sstevel@tonic-gate 	STRUCT_FSET(login, co_return_value, rv);
34830Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(login), arg, STRUCT_SIZE(login)) != 0) {
34840Sstevel@tonic-gate 		return (EFAULT);
34850Sstevel@tonic-gate 	}
34860Sstevel@tonic-gate 	return (0);
34870Sstevel@tonic-gate }
34880Sstevel@tonic-gate 
34890Sstevel@tonic-gate /* ARGSUSED */
34900Sstevel@tonic-gate static int
34910Sstevel@tonic-gate logout(dev_t dev, caddr_t arg, int mode, int *rval)
34920Sstevel@tonic-gate {
34930Sstevel@tonic-gate 	crypto_logout_t logout;
34940Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
34950Sstevel@tonic-gate 	kcf_req_params_t params;
34960Sstevel@tonic-gate 	crypto_minor_t *cm;
34970Sstevel@tonic-gate 	crypto_session_data_t *sp;
34980Sstevel@tonic-gate 	int error = 0;
34990Sstevel@tonic-gate 	int rv;
35000Sstevel@tonic-gate 
35010Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
35020Sstevel@tonic-gate 		cmn_err(CE_WARN, "logout: failed holding minor");
35030Sstevel@tonic-gate 		return (ENXIO);
35040Sstevel@tonic-gate 	}
35050Sstevel@tonic-gate 
35060Sstevel@tonic-gate 	if (copyin(arg, &logout, sizeof (logout)) != 0) {
35070Sstevel@tonic-gate 		crypto_release_minor(cm);
35080Sstevel@tonic-gate 		return (EFAULT);
35090Sstevel@tonic-gate 	}
35100Sstevel@tonic-gate 
35110Sstevel@tonic-gate 	if (!get_session_ptr(logout.cl_session, cm, &sp, &error, &rv))  {
35120Sstevel@tonic-gate 		goto release_minor;
35130Sstevel@tonic-gate 	}
35140Sstevel@tonic-gate 
35150Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
35160Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(session_ops),
3517904Smcpowers 	    CRYPTO_SESSION_OFFSET(session_logout), CHECK_RESTRICT_FALSE,
35180Sstevel@tonic-gate 	    sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
35190Sstevel@tonic-gate 		goto out;
35200Sstevel@tonic-gate 	}
35210Sstevel@tonic-gate 
35220Sstevel@tonic-gate 	KCF_WRAP_SESSION_OPS_PARAMS(&params, KCF_OP_SESSION_LOGOUT, NULL,
35230Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, 0, NULL, 0, real_provider);
35240Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
3525904Smcpowers 	KCF_PROV_REFRELE(real_provider);
35260Sstevel@tonic-gate 
35270Sstevel@tonic-gate out:
35280Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
35290Sstevel@tonic-gate 
35300Sstevel@tonic-gate release_minor:
35310Sstevel@tonic-gate 	crypto_release_minor(cm);
35320Sstevel@tonic-gate 
35330Sstevel@tonic-gate 	if (error != 0)
35340Sstevel@tonic-gate 		return (error);
35350Sstevel@tonic-gate 
35360Sstevel@tonic-gate 	logout.cl_return_value = rv;
35370Sstevel@tonic-gate 	if (copyout(&logout, arg, sizeof (logout)) != 0) {
35380Sstevel@tonic-gate 		return (EFAULT);
35390Sstevel@tonic-gate 	}
35400Sstevel@tonic-gate 	return (0);
35410Sstevel@tonic-gate }
35420Sstevel@tonic-gate 
35430Sstevel@tonic-gate /* ARGSUSED */
35440Sstevel@tonic-gate static int
35450Sstevel@tonic-gate sign_init(dev_t dev, caddr_t arg, int mode, int *rval)
35460Sstevel@tonic-gate {
35470Sstevel@tonic-gate 	return (sign_verify_init(dev, arg, mode, crypto_sign_init_prov));
35480Sstevel@tonic-gate }
35490Sstevel@tonic-gate 
35500Sstevel@tonic-gate /* ARGSUSED */
35510Sstevel@tonic-gate static int
35520Sstevel@tonic-gate sign_recover_init(dev_t dev, caddr_t arg, int mode, int *rval)
35530Sstevel@tonic-gate {
35540Sstevel@tonic-gate 	return (sign_verify_init(dev, arg, mode,
35550Sstevel@tonic-gate 	    crypto_sign_recover_init_prov));
35560Sstevel@tonic-gate }
35570Sstevel@tonic-gate 
35580Sstevel@tonic-gate /* ARGSUSED */
35590Sstevel@tonic-gate static int
35600Sstevel@tonic-gate verify_init(dev_t dev, caddr_t arg, int mode, int *rval)
35610Sstevel@tonic-gate {
35620Sstevel@tonic-gate 	return (sign_verify_init(dev, arg, mode, crypto_verify_init_prov));
35630Sstevel@tonic-gate }
35640Sstevel@tonic-gate 
35650Sstevel@tonic-gate /* ARGSUSED */
35660Sstevel@tonic-gate static int
35670Sstevel@tonic-gate verify_recover_init(dev_t dev, caddr_t arg, int mode, int *rval)
35680Sstevel@tonic-gate {
35690Sstevel@tonic-gate 	return (sign_verify_init(dev, arg, mode,
35700Sstevel@tonic-gate 	    crypto_verify_recover_init_prov));
35710Sstevel@tonic-gate }
35720Sstevel@tonic-gate 
35730Sstevel@tonic-gate /*
35740Sstevel@tonic-gate  * ASSUMPTION: crypto_sign_init, crypto_verify_init, crypto_sign_recover_init,
35750Sstevel@tonic-gate  * and crypto_verify_recover_init structures are identical
35760Sstevel@tonic-gate  * except for field names.
35770Sstevel@tonic-gate  */
35780Sstevel@tonic-gate static int
35790Sstevel@tonic-gate sign_verify_init(dev_t dev, caddr_t arg, int mode,
3580904Smcpowers     int (*init)(crypto_provider_t, crypto_session_id_t,
35810Sstevel@tonic-gate     crypto_mechanism_t *, crypto_key_t *, crypto_ctx_template_t,
35820Sstevel@tonic-gate     crypto_context_t *, crypto_call_req_t *))
35830Sstevel@tonic-gate {
35840Sstevel@tonic-gate 	STRUCT_DECL(crypto_sign_init, sign_init);
3585904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
35860Sstevel@tonic-gate 	crypto_session_id_t session_id;
35870Sstevel@tonic-gate 	crypto_mechanism_t mech;
35880Sstevel@tonic-gate 	crypto_key_t key;
35890Sstevel@tonic-gate 	crypto_minor_t *cm;
35906424Skrishna 	crypto_session_data_t *sp = NULL;
35910Sstevel@tonic-gate 	crypto_context_t cc;
35920Sstevel@tonic-gate 	crypto_ctx_t **ctxpp;
35930Sstevel@tonic-gate 	size_t mech_rctl_bytes = 0;
35946424Skrishna 	boolean_t mech_rctl_chk = B_FALSE;
35950Sstevel@tonic-gate 	size_t key_rctl_bytes = 0;
35966424Skrishna 	boolean_t key_rctl_chk = B_FALSE;
35970Sstevel@tonic-gate 	int error = 0;
35980Sstevel@tonic-gate 	int rv;
3599904Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
36001808Smcpowers 	crypto_func_group_t fg;
36010Sstevel@tonic-gate 
36020Sstevel@tonic-gate 	STRUCT_INIT(sign_init, mode);
36030Sstevel@tonic-gate 
36040Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
36050Sstevel@tonic-gate 		cmn_err(CE_WARN, "sign_verify_init: failed holding minor");
36060Sstevel@tonic-gate 		return (ENXIO);
36070Sstevel@tonic-gate 	}
36080Sstevel@tonic-gate 
36090Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(sign_init), STRUCT_SIZE(sign_init)) != 0) {
36100Sstevel@tonic-gate 		crypto_release_minor(cm);
36110Sstevel@tonic-gate 		return (EFAULT);
36120Sstevel@tonic-gate 	}
36130Sstevel@tonic-gate 
36140Sstevel@tonic-gate 	mech.cm_param = NULL;
36150Sstevel@tonic-gate 	bzero(&key, sizeof (key));
36160Sstevel@tonic-gate 
36170Sstevel@tonic-gate 	session_id = STRUCT_FGET(sign_init, si_session);
36180Sstevel@tonic-gate 
36190Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
36206424Skrishna 		goto out;
36210Sstevel@tonic-gate 	}
36220Sstevel@tonic-gate 
3623904Smcpowers 	bcopy(STRUCT_FADDR(sign_init, si_mech), &mech.cm_type,
3624904Smcpowers 	    sizeof (crypto_mech_type_t));
36250Sstevel@tonic-gate 
36260Sstevel@tonic-gate 	ASSERT(init == crypto_sign_init_prov ||
36270Sstevel@tonic-gate 	    init == crypto_verify_init_prov ||
36280Sstevel@tonic-gate 	    init == crypto_sign_recover_init_prov ||
36290Sstevel@tonic-gate 	    init == crypto_verify_recover_init_prov);
36300Sstevel@tonic-gate 
36310Sstevel@tonic-gate 	if (init == crypto_sign_init_prov) {
36321808Smcpowers 		fg =  CRYPTO_FG_SIGN;
36330Sstevel@tonic-gate 		ctxpp = &sp->sd_sign_ctx;
36340Sstevel@tonic-gate 	} else if (init == crypto_verify_init_prov) {
36351808Smcpowers 		fg =  CRYPTO_FG_VERIFY;
36360Sstevel@tonic-gate 		ctxpp = &sp->sd_verify_ctx;
36370Sstevel@tonic-gate 	} else if (init == crypto_sign_recover_init_prov) {
36381808Smcpowers 		fg =  CRYPTO_FG_SIGN_RECOVER;
36390Sstevel@tonic-gate 		ctxpp = &sp->sd_sign_recover_ctx;
36400Sstevel@tonic-gate 	} else {
36411808Smcpowers 		fg =  CRYPTO_FG_VERIFY_RECOVER;
36420Sstevel@tonic-gate 		ctxpp = &sp->sd_verify_recover_ctx;
36430Sstevel@tonic-gate 	}
36440Sstevel@tonic-gate 
3645904Smcpowers 	if ((rv = kcf_get_hardware_provider(mech.cm_type, CRYPTO_MECH_INVALID,
36461808Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider, fg))
36471808Smcpowers 	    != CRYPTO_SUCCESS) {
36480Sstevel@tonic-gate 		goto out;
36490Sstevel@tonic-gate 	}
36500Sstevel@tonic-gate 
3651904Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
3652904Smcpowers 	    STRUCT_FADDR(sign_init, si_mech), &mech, mode, &error);
3653904Smcpowers 
3654904Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
3655904Smcpowers 		allocated_by_crypto_module = B_TRUE;
36566424Skrishna 		if (!copyin_mech(mode, sp, STRUCT_FADDR(sign_init, si_mech),
36576424Skrishna 		    &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) {
3658904Smcpowers 			goto out;
3659904Smcpowers 		}
3660904Smcpowers 	} else {
3661904Smcpowers 		if (rv != CRYPTO_SUCCESS)
3662904Smcpowers 			goto out;
3663904Smcpowers 	}
3664904Smcpowers 
36656424Skrishna 	if (!copyin_key(mode, sp, STRUCT_FADDR(sign_init, si_key), &key,
36666424Skrishna 	    &key_rctl_bytes, &key_rctl_chk, &rv, &error)) {
36670Sstevel@tonic-gate 		goto out;
36680Sstevel@tonic-gate 	}
36690Sstevel@tonic-gate 
36700Sstevel@tonic-gate 	rv = (init)(real_provider, sp->sd_provider_session->ps_session,
36710Sstevel@tonic-gate 	    &mech, &key, NULL, &cc, NULL);
36720Sstevel@tonic-gate 
36730Sstevel@tonic-gate 	/*
36740Sstevel@tonic-gate 	 * Check if a context already exists. If so, it means it is being
36750Sstevel@tonic-gate 	 * abandoned. So, cancel it to avoid leaking it.
36760Sstevel@tonic-gate 	 */
36770Sstevel@tonic-gate 	if (*ctxpp != NULL)
36780Sstevel@tonic-gate 		CRYPTO_CANCEL_CTX(ctxpp);
36790Sstevel@tonic-gate 	*ctxpp = (rv == CRYPTO_SUCCESS) ? cc : NULL;
36800Sstevel@tonic-gate 
36810Sstevel@tonic-gate out:
36826424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
36836424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk);
36840Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
36850Sstevel@tonic-gate 	crypto_release_minor(cm);
36860Sstevel@tonic-gate 
3687904Smcpowers 	if (real_provider != NULL) {
3688904Smcpowers 		crypto_free_mech(real_provider,
3689904Smcpowers 		    allocated_by_crypto_module, &mech);
3690904Smcpowers 		KCF_PROV_REFRELE(real_provider);
3691904Smcpowers 	}
36920Sstevel@tonic-gate 
36930Sstevel@tonic-gate 	free_crypto_key(&key);
36940Sstevel@tonic-gate 
36950Sstevel@tonic-gate 	if (error != 0)
36960Sstevel@tonic-gate 		return (error);
36970Sstevel@tonic-gate 
36980Sstevel@tonic-gate 	STRUCT_FSET(sign_init, si_return_value, rv);
36990Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(sign_init), arg, STRUCT_SIZE(sign_init)) != 0) {
37000Sstevel@tonic-gate 		return (EFAULT);
37010Sstevel@tonic-gate 	}
37020Sstevel@tonic-gate 	return (0);
37030Sstevel@tonic-gate }
37040Sstevel@tonic-gate 
37050Sstevel@tonic-gate /* ARGSUSED */
37060Sstevel@tonic-gate static int
37070Sstevel@tonic-gate sign(dev_t dev, caddr_t arg, int mode, int *rval)
37080Sstevel@tonic-gate {
37090Sstevel@tonic-gate 	return (common_digest(dev, arg, mode, crypto_sign_single));
37100Sstevel@tonic-gate }
37110Sstevel@tonic-gate 
37120Sstevel@tonic-gate /* ARGSUSED */
37130Sstevel@tonic-gate static int
37140Sstevel@tonic-gate sign_recover(dev_t dev, caddr_t arg, int mode, int *rval)
37150Sstevel@tonic-gate {
37160Sstevel@tonic-gate 	return (common_digest(dev, arg, mode, crypto_sign_recover_single));
37170Sstevel@tonic-gate }
37180Sstevel@tonic-gate 
37190Sstevel@tonic-gate /* ARGSUSED */
37200Sstevel@tonic-gate static int
37210Sstevel@tonic-gate verify(dev_t dev, caddr_t arg, int mode, int *rval)
37220Sstevel@tonic-gate {
37230Sstevel@tonic-gate 	STRUCT_DECL(crypto_verify, verify);
37240Sstevel@tonic-gate 	crypto_session_id_t session_id;
37250Sstevel@tonic-gate 	crypto_minor_t *cm;
37266424Skrishna 	crypto_session_data_t *sp = NULL;
37270Sstevel@tonic-gate 	crypto_data_t data, sign;
37280Sstevel@tonic-gate 	size_t datalen, signlen, need = 0;
37290Sstevel@tonic-gate 	int error = 0;
37300Sstevel@tonic-gate 	int rv;
37316424Skrishna 	boolean_t rctl_chk = B_FALSE;
37320Sstevel@tonic-gate 
37330Sstevel@tonic-gate 	STRUCT_INIT(verify, mode);
37340Sstevel@tonic-gate 
37350Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
37360Sstevel@tonic-gate 		cmn_err(CE_WARN, "verify: failed holding minor");
37370Sstevel@tonic-gate 		return (ENXIO);
37380Sstevel@tonic-gate 	}
37390Sstevel@tonic-gate 
37400Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(verify), STRUCT_SIZE(verify)) != 0) {
37410Sstevel@tonic-gate 		crypto_release_minor(cm);
37420Sstevel@tonic-gate 		return (EFAULT);
37430Sstevel@tonic-gate 	}
37440Sstevel@tonic-gate 
37450Sstevel@tonic-gate 	data.cd_raw.iov_base = NULL;
37460Sstevel@tonic-gate 	sign.cd_raw.iov_base = NULL;
37470Sstevel@tonic-gate 
37480Sstevel@tonic-gate 	datalen = STRUCT_FGET(verify, cv_datalen);
37490Sstevel@tonic-gate 	signlen = STRUCT_FGET(verify, cv_signlen);
37500Sstevel@tonic-gate 	if (datalen > crypto_max_buffer_len ||
37510Sstevel@tonic-gate 	    signlen > crypto_max_buffer_len) {
37520Sstevel@tonic-gate 		cmn_err(CE_NOTE, "verify: buffer greater than %ld bytes, "
37530Sstevel@tonic-gate 		"pid = %d", crypto_max_buffer_len, curproc->p_pid);
37540Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
37550Sstevel@tonic-gate 		goto release_minor;
37560Sstevel@tonic-gate 	}
37570Sstevel@tonic-gate 
37586424Skrishna 	session_id = STRUCT_FGET(verify, cv_session);
37596424Skrishna 
37606424Skrishna 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
37616424Skrishna 		goto release_minor;
37626424Skrishna 	}
37636424Skrishna 
37640Sstevel@tonic-gate 	need = datalen + signlen;
37656424Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, need, rctl_chk)) !=
37666424Skrishna 	    CRYPTO_SUCCESS) {
37670Sstevel@tonic-gate 		need = 0;
37680Sstevel@tonic-gate 		goto release_minor;
37690Sstevel@tonic-gate 	}
37700Sstevel@tonic-gate 
37710Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(data, datalen);
37720Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(sign, signlen);
37730Sstevel@tonic-gate 
37740Sstevel@tonic-gate 	if (datalen != 0 && copyin(STRUCT_FGETP(verify, cv_databuf),
37750Sstevel@tonic-gate 	    data.cd_raw.iov_base, datalen) != 0) {
37760Sstevel@tonic-gate 		error = EFAULT;
37770Sstevel@tonic-gate 		goto release_minor;
37780Sstevel@tonic-gate 	}
37790Sstevel@tonic-gate 
37800Sstevel@tonic-gate 	if (signlen != 0 && copyin(STRUCT_FGETP(verify, cv_signbuf),
37810Sstevel@tonic-gate 	    sign.cd_raw.iov_base, signlen) != 0) {
37820Sstevel@tonic-gate 		error = EFAULT;
37830Sstevel@tonic-gate 		goto release_minor;
37840Sstevel@tonic-gate 	}
37850Sstevel@tonic-gate 
37860Sstevel@tonic-gate 	rv = crypto_verify_single(sp->sd_verify_ctx, &data, &sign, NULL);
37870Sstevel@tonic-gate 	if (KCF_CONTEXT_DONE(rv))
37880Sstevel@tonic-gate 		sp->sd_verify_ctx = NULL;
37890Sstevel@tonic-gate 
37906424Skrishna release_minor:
37916424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
37920Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
37930Sstevel@tonic-gate 	crypto_release_minor(cm);
37940Sstevel@tonic-gate 
37950Sstevel@tonic-gate 	if (data.cd_raw.iov_base != NULL)
37960Sstevel@tonic-gate 		kmem_free(data.cd_raw.iov_base, datalen);
37970Sstevel@tonic-gate 
37980Sstevel@tonic-gate 	if (sign.cd_raw.iov_base != NULL)
37990Sstevel@tonic-gate 		kmem_free(sign.cd_raw.iov_base, signlen);
38000Sstevel@tonic-gate 
38010Sstevel@tonic-gate 	if (error != 0)
38020Sstevel@tonic-gate 		return (error);
38030Sstevel@tonic-gate 
38040Sstevel@tonic-gate 	STRUCT_FSET(verify, cv_return_value, rv);
38050Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(verify), arg, STRUCT_SIZE(verify)) != 0) {
38060Sstevel@tonic-gate 		return (EFAULT);
38070Sstevel@tonic-gate 	}
38080Sstevel@tonic-gate 	return (0);
38090Sstevel@tonic-gate }
38100Sstevel@tonic-gate 
38110Sstevel@tonic-gate /* ARGSUSED */
38120Sstevel@tonic-gate static int
38130Sstevel@tonic-gate verify_recover(dev_t dev, caddr_t arg, int mode, int *rval)
38140Sstevel@tonic-gate {
38150Sstevel@tonic-gate 	return (common_digest(dev, arg, mode, crypto_verify_recover_single));
38160Sstevel@tonic-gate }
38170Sstevel@tonic-gate 
38180Sstevel@tonic-gate /* ARGSUSED */
38190Sstevel@tonic-gate static int
38200Sstevel@tonic-gate sign_update(dev_t dev, caddr_t arg, int mode, int *rval)
38210Sstevel@tonic-gate {
38220Sstevel@tonic-gate 	return (sign_verify_update(dev, arg, mode, crypto_sign_update));
38230Sstevel@tonic-gate }
38240Sstevel@tonic-gate 
38250Sstevel@tonic-gate /* ARGSUSED */
38260Sstevel@tonic-gate static int
38270Sstevel@tonic-gate verify_update(dev_t dev, caddr_t arg, int mode, int *rval)
38280Sstevel@tonic-gate {
38290Sstevel@tonic-gate 	return (sign_verify_update(dev, arg, mode, crypto_verify_update));
38300Sstevel@tonic-gate }
38310Sstevel@tonic-gate 
38320Sstevel@tonic-gate /*
38330Sstevel@tonic-gate  * ASSUMPTION: crypto_sign_update and crypto_verify_update structures
38340Sstevel@tonic-gate  * are identical except for field names.
38350Sstevel@tonic-gate  */
38360Sstevel@tonic-gate static int
38370Sstevel@tonic-gate sign_verify_update(dev_t dev, caddr_t arg, int mode,
38380Sstevel@tonic-gate     int (*update)(crypto_context_t, crypto_data_t *, crypto_call_req_t *))
38390Sstevel@tonic-gate {
38400Sstevel@tonic-gate 	STRUCT_DECL(crypto_sign_update, sign_update);
38410Sstevel@tonic-gate 	crypto_session_id_t session_id;
38420Sstevel@tonic-gate 	crypto_minor_t *cm;
38436424Skrishna 	crypto_session_data_t *sp = NULL;
38440Sstevel@tonic-gate 	crypto_ctx_t **ctxpp;
38450Sstevel@tonic-gate 	crypto_data_t data;
38460Sstevel@tonic-gate 	size_t datalen, need = 0;
38470Sstevel@tonic-gate 	int error = 0;
38480Sstevel@tonic-gate 	int rv;
38496424Skrishna 	boolean_t rctl_chk = B_FALSE;
38500Sstevel@tonic-gate 
38510Sstevel@tonic-gate 	STRUCT_INIT(sign_update, mode);
38520Sstevel@tonic-gate 
38530Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
38540Sstevel@tonic-gate 		cmn_err(CE_WARN, "sign_verify_update: failed holding minor");
38550Sstevel@tonic-gate 		return (ENXIO);
38560Sstevel@tonic-gate 	}
38570Sstevel@tonic-gate 
38580Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(sign_update),
38590Sstevel@tonic-gate 	    STRUCT_SIZE(sign_update)) != 0) {
38600Sstevel@tonic-gate 		crypto_release_minor(cm);
38610Sstevel@tonic-gate 		return (EFAULT);
38620Sstevel@tonic-gate 	}
38630Sstevel@tonic-gate 
38640Sstevel@tonic-gate 	data.cd_raw.iov_base = NULL;
38650Sstevel@tonic-gate 
38660Sstevel@tonic-gate 	datalen = STRUCT_FGET(sign_update, su_datalen);
38670Sstevel@tonic-gate 	if (datalen > crypto_max_buffer_len) {
38680Sstevel@tonic-gate 		cmn_err(CE_NOTE, "sign_verify_update: buffer greater than %ld "
38690Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
38700Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
38710Sstevel@tonic-gate 		goto release_minor;
38720Sstevel@tonic-gate 	}
38730Sstevel@tonic-gate 
38746424Skrishna 	session_id = STRUCT_FGET(sign_update, su_session);
38756424Skrishna 
38766424Skrishna 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
38776424Skrishna 		goto release_minor;
38786424Skrishna 	}
38796424Skrishna 
38806424Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, datalen, rctl_chk)) !=
38816424Skrishna 	    CRYPTO_SUCCESS) {
38820Sstevel@tonic-gate 		goto release_minor;
38830Sstevel@tonic-gate 	}
38840Sstevel@tonic-gate 	need = datalen;
38850Sstevel@tonic-gate 
38860Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(data, datalen);
38870Sstevel@tonic-gate 
38880Sstevel@tonic-gate 	if (datalen != 0 && copyin(STRUCT_FGETP(sign_update, su_databuf),
38890Sstevel@tonic-gate 	    data.cd_raw.iov_base, datalen) != 0) {
38900Sstevel@tonic-gate 		error = EFAULT;
38910Sstevel@tonic-gate 		goto release_minor;
38920Sstevel@tonic-gate 	}
38930Sstevel@tonic-gate 
38940Sstevel@tonic-gate 	ctxpp = (update == crypto_sign_update) ?
38950Sstevel@tonic-gate 	    &sp->sd_sign_ctx : &sp->sd_verify_ctx;
38960Sstevel@tonic-gate 
38970Sstevel@tonic-gate 	rv = (update)(*ctxpp, &data, NULL);
38980Sstevel@tonic-gate 	if (rv != CRYPTO_SUCCESS)
38990Sstevel@tonic-gate 		CRYPTO_CANCEL_CTX(ctxpp);
39000Sstevel@tonic-gate 
39010Sstevel@tonic-gate release_minor:
39026424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
39036424Skrishna 	CRYPTO_SESSION_RELE(sp);
39040Sstevel@tonic-gate 	crypto_release_minor(cm);
39050Sstevel@tonic-gate 
39060Sstevel@tonic-gate 	if (data.cd_raw.iov_base != NULL)
39070Sstevel@tonic-gate 		kmem_free(data.cd_raw.iov_base, datalen);
39080Sstevel@tonic-gate 
39090Sstevel@tonic-gate 	if (error != 0)
39100Sstevel@tonic-gate 		return (error);
39110Sstevel@tonic-gate 
39120Sstevel@tonic-gate 	STRUCT_FSET(sign_update, su_return_value, rv);
39130Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(sign_update), arg,
39140Sstevel@tonic-gate 	    STRUCT_SIZE(sign_update)) != 0) {
39150Sstevel@tonic-gate 		return (EFAULT);
39160Sstevel@tonic-gate 	}
39170Sstevel@tonic-gate 	return (0);
39180Sstevel@tonic-gate }
39190Sstevel@tonic-gate 
39200Sstevel@tonic-gate /* ARGSUSED */
39210Sstevel@tonic-gate static int
39220Sstevel@tonic-gate sign_final(dev_t dev, caddr_t arg, int mode, int *rval)
39230Sstevel@tonic-gate {
39240Sstevel@tonic-gate 	return (common_final(dev, arg, mode, crypto_sign_final));
39250Sstevel@tonic-gate }
39260Sstevel@tonic-gate 
39270Sstevel@tonic-gate /*
39280Sstevel@tonic-gate  * Can't use the common final because it does a copyout of
39290Sstevel@tonic-gate  * the final part.
39300Sstevel@tonic-gate  */
39310Sstevel@tonic-gate /* ARGSUSED */
39320Sstevel@tonic-gate static int
39330Sstevel@tonic-gate verify_final(dev_t dev, caddr_t arg, int mode, int *rval)
39340Sstevel@tonic-gate {
39350Sstevel@tonic-gate 	STRUCT_DECL(crypto_verify_final, verify_final);
39360Sstevel@tonic-gate 	crypto_session_id_t session_id;
39370Sstevel@tonic-gate 	crypto_minor_t *cm;
39386424Skrishna 	crypto_session_data_t *sp = NULL;
39390Sstevel@tonic-gate 	crypto_data_t sign;
39400Sstevel@tonic-gate 	size_t signlen, need = 0;
39410Sstevel@tonic-gate 	int error = 0;
39420Sstevel@tonic-gate 	int rv;
39436424Skrishna 	boolean_t rctl_chk = B_FALSE;
39440Sstevel@tonic-gate 
39450Sstevel@tonic-gate 	STRUCT_INIT(verify_final, mode);
39460Sstevel@tonic-gate 
39470Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
39480Sstevel@tonic-gate 		cmn_err(CE_WARN, "verify_final: failed holding minor");
39490Sstevel@tonic-gate 		return (ENXIO);
39500Sstevel@tonic-gate 	}
39510Sstevel@tonic-gate 
39520Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(verify_final),
39530Sstevel@tonic-gate 	    STRUCT_SIZE(verify_final)) != 0) {
39540Sstevel@tonic-gate 		crypto_release_minor(cm);
39550Sstevel@tonic-gate 		return (EFAULT);
39560Sstevel@tonic-gate 	}
39570Sstevel@tonic-gate 
39580Sstevel@tonic-gate 	sign.cd_raw.iov_base = NULL;
39590Sstevel@tonic-gate 
39600Sstevel@tonic-gate 	signlen = STRUCT_FGET(verify_final, vf_signlen);
39610Sstevel@tonic-gate 	if (signlen > crypto_max_buffer_len) {
39620Sstevel@tonic-gate 		cmn_err(CE_NOTE, "verify_final: buffer greater than %ld "
39630Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
39640Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
39650Sstevel@tonic-gate 		goto release_minor;
39660Sstevel@tonic-gate 	}
39670Sstevel@tonic-gate 
39686424Skrishna 	session_id = STRUCT_FGET(verify_final, vf_session);
39696424Skrishna 
39706424Skrishna 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
39716424Skrishna 		goto release_minor;
39726424Skrishna 	}
39736424Skrishna 
39746424Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, signlen, rctl_chk)) !=
39756424Skrishna 	    CRYPTO_SUCCESS) {
39760Sstevel@tonic-gate 		goto release_minor;
39770Sstevel@tonic-gate 	}
39780Sstevel@tonic-gate 	need = signlen;
39790Sstevel@tonic-gate 
39800Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(sign, signlen);
39810Sstevel@tonic-gate 
39820Sstevel@tonic-gate 	if (signlen != 0 && copyin(STRUCT_FGETP(verify_final, vf_signbuf),
39830Sstevel@tonic-gate 	    sign.cd_raw.iov_base, signlen) != 0) {
39840Sstevel@tonic-gate 		error = EFAULT;
39850Sstevel@tonic-gate 		goto release_minor;
39860Sstevel@tonic-gate 	}
39870Sstevel@tonic-gate 
39880Sstevel@tonic-gate 	rv = crypto_verify_final(sp->sd_verify_ctx, &sign, NULL);
39890Sstevel@tonic-gate 	if (KCF_CONTEXT_DONE(rv))
39900Sstevel@tonic-gate 		sp->sd_verify_ctx = NULL;
39910Sstevel@tonic-gate 
39926424Skrishna release_minor:
39936424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
39940Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
39950Sstevel@tonic-gate 	crypto_release_minor(cm);
39960Sstevel@tonic-gate 
39970Sstevel@tonic-gate 	if (sign.cd_raw.iov_base != NULL)
39980Sstevel@tonic-gate 		kmem_free(sign.cd_raw.iov_base, signlen);
39990Sstevel@tonic-gate 
40000Sstevel@tonic-gate 	if (error != 0)
40010Sstevel@tonic-gate 		return (error);
40020Sstevel@tonic-gate 
40030Sstevel@tonic-gate 	STRUCT_FSET(verify_final, vf_return_value, rv);
40040Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(verify_final), arg,
40050Sstevel@tonic-gate 	    STRUCT_SIZE(verify_final)) != 0) {
40060Sstevel@tonic-gate 		return (EFAULT);
40070Sstevel@tonic-gate 	}
40080Sstevel@tonic-gate 	return (0);
40090Sstevel@tonic-gate }
40100Sstevel@tonic-gate 
40110Sstevel@tonic-gate /* ARGSUSED */
40120Sstevel@tonic-gate static int
40130Sstevel@tonic-gate seed_random(dev_t dev, caddr_t arg, int mode, int *rval)
40140Sstevel@tonic-gate {
40150Sstevel@tonic-gate 	STRUCT_DECL(crypto_seed_random, seed_random);
4016904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
40170Sstevel@tonic-gate 	kcf_req_params_t params;
40180Sstevel@tonic-gate 	crypto_session_id_t session_id;
40190Sstevel@tonic-gate 	crypto_minor_t *cm;
40206424Skrishna 	crypto_session_data_t *sp = NULL;
40210Sstevel@tonic-gate 	uchar_t *seed_buffer = NULL;
40220Sstevel@tonic-gate 	size_t seed_len;
40230Sstevel@tonic-gate 	size_t need = 0;
40240Sstevel@tonic-gate 	int error = 0;
40250Sstevel@tonic-gate 	int rv;
40266424Skrishna 	boolean_t rctl_chk = B_FALSE;
40270Sstevel@tonic-gate 
40280Sstevel@tonic-gate 	STRUCT_INIT(seed_random, mode);
40290Sstevel@tonic-gate 
40300Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
40310Sstevel@tonic-gate 		cmn_err(CE_WARN, "seed_random: failed holding minor");
40320Sstevel@tonic-gate 		return (ENXIO);
40330Sstevel@tonic-gate 	}
40340Sstevel@tonic-gate 
40350Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(seed_random),
40360Sstevel@tonic-gate 	    STRUCT_SIZE(seed_random)) != 0) {
40370Sstevel@tonic-gate 		crypto_release_minor(cm);
40380Sstevel@tonic-gate 		return (EFAULT);
40390Sstevel@tonic-gate 	}
40400Sstevel@tonic-gate 
40410Sstevel@tonic-gate 	seed_len = STRUCT_FGET(seed_random, sr_seedlen);
40420Sstevel@tonic-gate 	if (seed_len > crypto_max_buffer_len) {
40430Sstevel@tonic-gate 		cmn_err(CE_NOTE, "seed_random: buffer greater than %ld "
40440Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
40450Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
40460Sstevel@tonic-gate 		goto release_minor;
40470Sstevel@tonic-gate 	}
40480Sstevel@tonic-gate 
40496424Skrishna 	session_id = STRUCT_FGET(seed_random, sr_session);
40506424Skrishna 
40516424Skrishna 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
40526424Skrishna 		goto release_minor;
40536424Skrishna 	}
40546424Skrishna 
40556424Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, seed_len, rctl_chk)) !=
40566424Skrishna 	    CRYPTO_SUCCESS) {
40570Sstevel@tonic-gate 		goto release_minor;
40580Sstevel@tonic-gate 	}
40590Sstevel@tonic-gate 	need = seed_len;
40600Sstevel@tonic-gate 	seed_buffer = kmem_alloc(seed_len, KM_SLEEP);
40610Sstevel@tonic-gate 
40620Sstevel@tonic-gate 	if (seed_len != 0 && copyin(STRUCT_FGETP(seed_random, sr_seedbuf),
40630Sstevel@tonic-gate 	    seed_buffer, seed_len) != 0) {
40640Sstevel@tonic-gate 		error = EFAULT;
40650Sstevel@tonic-gate 		goto release_minor;
40660Sstevel@tonic-gate 	}
40670Sstevel@tonic-gate 
40681808Smcpowers 	if ((rv = kcf_get_hardware_provider_nomech(
4069904Smcpowers 	    CRYPTO_OPS_OFFSET(random_ops), CRYPTO_RANDOM_OFFSET(seed_random),
4070904Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider))
4071904Smcpowers 	    != CRYPTO_SUCCESS) {
40726424Skrishna 		goto release_minor;
40730Sstevel@tonic-gate 	}
40740Sstevel@tonic-gate 
40750Sstevel@tonic-gate 	KCF_WRAP_RANDOM_OPS_PARAMS(&params, KCF_OP_RANDOM_SEED,
40761920Smcpowers 	    sp->sd_provider_session->ps_session, seed_buffer, seed_len, 0,
40771920Smcpowers 	    CRYPTO_SEED_NOW);
40780Sstevel@tonic-gate 
40790Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
40800Sstevel@tonic-gate 
40816424Skrishna release_minor:
40826424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
40830Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
40840Sstevel@tonic-gate 	crypto_release_minor(cm);
40850Sstevel@tonic-gate 
4086904Smcpowers 	if (real_provider != NULL)
4087904Smcpowers 		KCF_PROV_REFRELE(real_provider);
4088904Smcpowers 
40890Sstevel@tonic-gate 	if (seed_buffer != NULL)
40900Sstevel@tonic-gate 		kmem_free(seed_buffer, seed_len);
40910Sstevel@tonic-gate 
40920Sstevel@tonic-gate 	if (error != 0)
40930Sstevel@tonic-gate 		return (error);
40940Sstevel@tonic-gate 
40950Sstevel@tonic-gate 	STRUCT_FSET(seed_random, sr_return_value, rv);
40960Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(seed_random), arg,
40970Sstevel@tonic-gate 	    STRUCT_SIZE(seed_random)) != 0) {
40980Sstevel@tonic-gate 		return (EFAULT);
40990Sstevel@tonic-gate 	}
41000Sstevel@tonic-gate 	return (0);
41010Sstevel@tonic-gate }
41020Sstevel@tonic-gate 
41030Sstevel@tonic-gate /* ARGSUSED */
41040Sstevel@tonic-gate static int
41050Sstevel@tonic-gate generate_random(dev_t dev, caddr_t arg, int mode, int *rval)
41060Sstevel@tonic-gate {
41070Sstevel@tonic-gate 	STRUCT_DECL(crypto_generate_random, generate_random);
4108904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
41090Sstevel@tonic-gate 	kcf_req_params_t params;
41100Sstevel@tonic-gate 	crypto_session_id_t session_id;
41110Sstevel@tonic-gate 	crypto_minor_t *cm;
41126424Skrishna 	crypto_session_data_t *sp = NULL;
41130Sstevel@tonic-gate 	uchar_t *buffer = NULL;
41140Sstevel@tonic-gate 	size_t len;
41150Sstevel@tonic-gate 	size_t need = 0;
41160Sstevel@tonic-gate 	int error = 0;
41170Sstevel@tonic-gate 	int rv;
41186424Skrishna 	boolean_t rctl_chk = B_FALSE;
41190Sstevel@tonic-gate 
41200Sstevel@tonic-gate 	STRUCT_INIT(generate_random, mode);
41210Sstevel@tonic-gate 
41220Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
41230Sstevel@tonic-gate 		cmn_err(CE_WARN, "generate_random: failed holding minor");
41240Sstevel@tonic-gate 		return (ENXIO);
41250Sstevel@tonic-gate 	}
41260Sstevel@tonic-gate 
41270Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(generate_random),
41280Sstevel@tonic-gate 	    STRUCT_SIZE(generate_random)) != 0) {
41290Sstevel@tonic-gate 		crypto_release_minor(cm);
41300Sstevel@tonic-gate 		return (EFAULT);
41310Sstevel@tonic-gate 	}
41320Sstevel@tonic-gate 
41330Sstevel@tonic-gate 	len = STRUCT_FGET(generate_random, gr_buflen);
41340Sstevel@tonic-gate 	if (len > crypto_max_buffer_len) {
41350Sstevel@tonic-gate 		cmn_err(CE_NOTE, "generate_random: buffer greater than %ld "
41360Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
41370Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
41380Sstevel@tonic-gate 		goto release_minor;
41390Sstevel@tonic-gate 	}
41400Sstevel@tonic-gate 
41416424Skrishna 	session_id = STRUCT_FGET(generate_random, gr_session);
41426424Skrishna 
41436424Skrishna 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
41446424Skrishna 		goto release_minor;
41456424Skrishna 	}
41466424Skrishna 
41476424Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, len, rctl_chk)) !=
41486424Skrishna 	    CRYPTO_SUCCESS) {
41490Sstevel@tonic-gate 		goto release_minor;
41500Sstevel@tonic-gate 	}
41510Sstevel@tonic-gate 	need = len;
41520Sstevel@tonic-gate 	buffer = kmem_alloc(len, KM_SLEEP);
41530Sstevel@tonic-gate 
41541808Smcpowers 	if ((rv = kcf_get_hardware_provider_nomech(
41550Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(random_ops),
4156904Smcpowers 	    CRYPTO_RANDOM_OFFSET(generate_random), CHECK_RESTRICT_FALSE,
41570Sstevel@tonic-gate 	    sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
41586424Skrishna 		goto release_minor;
41590Sstevel@tonic-gate 	}
41600Sstevel@tonic-gate 
41610Sstevel@tonic-gate 	KCF_WRAP_RANDOM_OPS_PARAMS(&params, KCF_OP_RANDOM_GENERATE,
41621920Smcpowers 	    sp->sd_provider_session->ps_session, buffer, len, 0, 0);
41630Sstevel@tonic-gate 
41640Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
41650Sstevel@tonic-gate 
41660Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
41670Sstevel@tonic-gate 		if (len != 0 && copyout(buffer,
41680Sstevel@tonic-gate 		    STRUCT_FGETP(generate_random, gr_buf), len) != 0) {
41690Sstevel@tonic-gate 			error = EFAULT;
41700Sstevel@tonic-gate 		}
41710Sstevel@tonic-gate 	}
41720Sstevel@tonic-gate 
41730Sstevel@tonic-gate release_minor:
41746424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
41756424Skrishna 	CRYPTO_SESSION_RELE(sp);
41760Sstevel@tonic-gate 	crypto_release_minor(cm);
41770Sstevel@tonic-gate 
4178904Smcpowers 	if (real_provider != NULL)
4179904Smcpowers 		KCF_PROV_REFRELE(real_provider);
4180904Smcpowers 
41810Sstevel@tonic-gate 	if (buffer != NULL) {
41820Sstevel@tonic-gate 		/* random numbers are often used to create keys */
41830Sstevel@tonic-gate 		bzero(buffer, len);
41840Sstevel@tonic-gate 		kmem_free(buffer, len);
41850Sstevel@tonic-gate 	}
41860Sstevel@tonic-gate 
41870Sstevel@tonic-gate 	if (error != 0)
41880Sstevel@tonic-gate 		return (error);
41890Sstevel@tonic-gate 
41900Sstevel@tonic-gate 	STRUCT_FSET(generate_random, gr_return_value, rv);
41910Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(generate_random), arg,
41920Sstevel@tonic-gate 	    STRUCT_SIZE(generate_random)) != 0) {
41930Sstevel@tonic-gate 		return (EFAULT);
41940Sstevel@tonic-gate 	}
41950Sstevel@tonic-gate 	return (0);
41960Sstevel@tonic-gate }
41970Sstevel@tonic-gate 
41980Sstevel@tonic-gate /*
41990Sstevel@tonic-gate  * Copyout a kernel array of attributes to user space.
42000Sstevel@tonic-gate  * u_attrs is the corresponding user space array containing
42010Sstevel@tonic-gate  * user space pointers necessary for the copyout.
42020Sstevel@tonic-gate  */
42030Sstevel@tonic-gate /* ARGSUSED */
42040Sstevel@tonic-gate static int
42050Sstevel@tonic-gate copyout_attributes(int mode, caddr_t out, uint_t count,
42060Sstevel@tonic-gate     crypto_object_attribute_t *k_attrs, caddr_t u_attrs)
42070Sstevel@tonic-gate {
42080Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_attribute, oa);
42090Sstevel@tonic-gate 	caddr_t p, valuep;
42100Sstevel@tonic-gate 	size_t value_len;
42110Sstevel@tonic-gate 	size_t len;
42120Sstevel@tonic-gate 	int i;
42130Sstevel@tonic-gate 	int error = 0;
42140Sstevel@tonic-gate 
42150Sstevel@tonic-gate 	if (count == 0)
42160Sstevel@tonic-gate 		return (0);
42170Sstevel@tonic-gate 
42180Sstevel@tonic-gate 	STRUCT_INIT(oa, mode);
42190Sstevel@tonic-gate 
42200Sstevel@tonic-gate 	len = count * STRUCT_SIZE(oa);
42210Sstevel@tonic-gate 
42220Sstevel@tonic-gate 	ASSERT(u_attrs != NULL);
42230Sstevel@tonic-gate 	p = u_attrs;
42240Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
42250Sstevel@tonic-gate 		/* can this bcopy be eliminated? */
42260Sstevel@tonic-gate 		bcopy(p, STRUCT_BUF(oa), STRUCT_SIZE(oa));
42270Sstevel@tonic-gate 		value_len = k_attrs[i].oa_value_len;
42280Sstevel@tonic-gate 		STRUCT_FSET(oa, oa_type, k_attrs[i].oa_type);
42290Sstevel@tonic-gate 		STRUCT_FSET(oa, oa_value_len, value_len);
42300Sstevel@tonic-gate 		valuep = STRUCT_FGETP(oa, oa_value);
42310Sstevel@tonic-gate 		if (valuep != NULL && value_len != -1) {
42320Sstevel@tonic-gate 			if (copyout(k_attrs[i].oa_value,
42330Sstevel@tonic-gate 			    valuep, value_len) != 0) {
42340Sstevel@tonic-gate 				error = EFAULT;
42350Sstevel@tonic-gate 				goto out;
42360Sstevel@tonic-gate 			}
42370Sstevel@tonic-gate 		}
42380Sstevel@tonic-gate 		bcopy(STRUCT_BUF(oa), p, STRUCT_SIZE(oa));
42390Sstevel@tonic-gate 		p += STRUCT_SIZE(oa);
42400Sstevel@tonic-gate 	}
42410Sstevel@tonic-gate 	if (copyout(u_attrs, out, len)) {
42420Sstevel@tonic-gate 		error = EFAULT;
42430Sstevel@tonic-gate 	}
42440Sstevel@tonic-gate out:
42450Sstevel@tonic-gate 	return (error);
42460Sstevel@tonic-gate }
42470Sstevel@tonic-gate 
42480Sstevel@tonic-gate 
42490Sstevel@tonic-gate /* ARGSUSED */
42500Sstevel@tonic-gate static int
42510Sstevel@tonic-gate object_create(dev_t dev, caddr_t arg, int mode, int *rval)
42520Sstevel@tonic-gate {
42530Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_create, object_create);
4254904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
42550Sstevel@tonic-gate 	kcf_req_params_t params;
42560Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
42570Sstevel@tonic-gate 	crypto_session_id_t session_id;
42580Sstevel@tonic-gate 	crypto_minor_t *cm;
42590Sstevel@tonic-gate 	crypto_session_data_t *sp = NULL;
42600Sstevel@tonic-gate 	crypto_object_id_t object_handle;
42610Sstevel@tonic-gate 	caddr_t oc_attributes;
42620Sstevel@tonic-gate 	size_t k_attrs_size;
42630Sstevel@tonic-gate 	size_t rctl_bytes = 0;
42646424Skrishna 	boolean_t rctl_chk = B_FALSE;
42650Sstevel@tonic-gate 	int error = 0;
42660Sstevel@tonic-gate 	int rv;
42670Sstevel@tonic-gate 	uint_t count;
42680Sstevel@tonic-gate 
42690Sstevel@tonic-gate 	STRUCT_INIT(object_create, mode);
42700Sstevel@tonic-gate 
42710Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
42720Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_create: failed holding minor");
42730Sstevel@tonic-gate 		return (ENXIO);
42740Sstevel@tonic-gate 	}
42750Sstevel@tonic-gate 
42760Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(object_create),
42770Sstevel@tonic-gate 	    STRUCT_SIZE(object_create)) != 0) {
42780Sstevel@tonic-gate 		crypto_release_minor(cm);
42790Sstevel@tonic-gate 		return (EFAULT);
42800Sstevel@tonic-gate 	}
42810Sstevel@tonic-gate 
42820Sstevel@tonic-gate 	count = STRUCT_FGET(object_create, oc_count);
42830Sstevel@tonic-gate 	oc_attributes = STRUCT_FGETP(object_create, oc_attributes);
42840Sstevel@tonic-gate 
42850Sstevel@tonic-gate 	session_id = STRUCT_FGET(object_create, oc_session);
42860Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
42870Sstevel@tonic-gate 		goto release_minor;
42880Sstevel@tonic-gate 	}
42896424Skrishna 	if (!copyin_attributes(mode, sp, count, oc_attributes, &k_attrs,
42906424Skrishna 	    &k_attrs_size, NULL, &rv, &error, &rctl_bytes,
42916424Skrishna 	    &rctl_chk, B_TRUE)) {
42926424Skrishna 		goto release_minor;
42936424Skrishna 	}
42940Sstevel@tonic-gate 
42950Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
42960Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
42970Sstevel@tonic-gate 	    CRYPTO_OBJECT_OFFSET(object_create),
4298904Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider))
4299904Smcpowers 	    != CRYPTO_SUCCESS) {
43000Sstevel@tonic-gate 		goto release_minor;
43010Sstevel@tonic-gate 	}
43020Sstevel@tonic-gate 
43030Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_CREATE,
43040Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, 0, k_attrs, count,
43050Sstevel@tonic-gate 	    &object_handle, 0, NULL, NULL, 0, NULL);
43060Sstevel@tonic-gate 
43070Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
43080Sstevel@tonic-gate 
43090Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS)
43100Sstevel@tonic-gate 		STRUCT_FSET(object_create, oc_handle, object_handle);
43110Sstevel@tonic-gate 
43120Sstevel@tonic-gate release_minor:
43136424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk);
43140Sstevel@tonic-gate 
43150Sstevel@tonic-gate 	if (k_attrs != NULL)
43160Sstevel@tonic-gate 		kmem_free(k_attrs, k_attrs_size);
43170Sstevel@tonic-gate 
43180Sstevel@tonic-gate 	if (error != 0)
43190Sstevel@tonic-gate 		goto out;
43200Sstevel@tonic-gate 
43210Sstevel@tonic-gate 	STRUCT_FSET(object_create, oc_return_value, rv);
43220Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(object_create), arg,
43230Sstevel@tonic-gate 	    STRUCT_SIZE(object_create)) != 0) {
43240Sstevel@tonic-gate 		if (rv == CRYPTO_SUCCESS) {
43250Sstevel@tonic-gate 			KCF_WRAP_OBJECT_OPS_PARAMS(&params,
43260Sstevel@tonic-gate 			    KCF_OP_OBJECT_DESTROY,
43270Sstevel@tonic-gate 			    sp->sd_provider_session->ps_session, object_handle,
43280Sstevel@tonic-gate 			    NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
43290Sstevel@tonic-gate 
43300Sstevel@tonic-gate 			(void) kcf_submit_request(real_provider, NULL,
43310Sstevel@tonic-gate 			    NULL, &params, B_FALSE);
43320Sstevel@tonic-gate 
43330Sstevel@tonic-gate 			error = EFAULT;
43340Sstevel@tonic-gate 		}
43350Sstevel@tonic-gate 	}
43360Sstevel@tonic-gate out:
43376424Skrishna 	CRYPTO_SESSION_RELE(sp);
43380Sstevel@tonic-gate 	crypto_release_minor(cm);
4339904Smcpowers 	if (real_provider != NULL)
4340904Smcpowers 		KCF_PROV_REFRELE(real_provider);
43410Sstevel@tonic-gate 	return (error);
43420Sstevel@tonic-gate }
43430Sstevel@tonic-gate 
43440Sstevel@tonic-gate /* ARGSUSED */
43450Sstevel@tonic-gate static int
43460Sstevel@tonic-gate object_copy(dev_t dev, caddr_t arg, int mode, int *rval)
43470Sstevel@tonic-gate {
43480Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_copy, object_copy);
4349904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
43500Sstevel@tonic-gate 	kcf_req_params_t params;
43510Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
43520Sstevel@tonic-gate 	crypto_session_id_t session_id;
43530Sstevel@tonic-gate 	crypto_minor_t *cm;
43540Sstevel@tonic-gate 	crypto_session_data_t *sp = NULL;
43550Sstevel@tonic-gate 	crypto_object_id_t handle, new_handle;
43560Sstevel@tonic-gate 	caddr_t oc_new_attributes;
43570Sstevel@tonic-gate 	size_t k_attrs_size;
43580Sstevel@tonic-gate 	size_t rctl_bytes = 0;
43596424Skrishna 	boolean_t rctl_chk = B_FALSE;
43600Sstevel@tonic-gate 	int error = 0;
43610Sstevel@tonic-gate 	int rv;
43620Sstevel@tonic-gate 	uint_t count;
43630Sstevel@tonic-gate 
43640Sstevel@tonic-gate 	STRUCT_INIT(object_copy, mode);
43650Sstevel@tonic-gate 
43660Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
43670Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_copy: failed holding minor");
43680Sstevel@tonic-gate 		return (ENXIO);
43690Sstevel@tonic-gate 	}
43700Sstevel@tonic-gate 
43710Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(object_copy),
43720Sstevel@tonic-gate 	    STRUCT_SIZE(object_copy)) != 0) {
43730Sstevel@tonic-gate 		crypto_release_minor(cm);
43740Sstevel@tonic-gate 		return (EFAULT);
43750Sstevel@tonic-gate 	}
43760Sstevel@tonic-gate 
43770Sstevel@tonic-gate 	count = STRUCT_FGET(object_copy, oc_count);
43780Sstevel@tonic-gate 	oc_new_attributes = STRUCT_FGETP(object_copy, oc_new_attributes);
43790Sstevel@tonic-gate 
43800Sstevel@tonic-gate 	session_id = STRUCT_FGET(object_copy, oc_session);
43810Sstevel@tonic-gate 
43820Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
43830Sstevel@tonic-gate 		goto release_minor;
43840Sstevel@tonic-gate 	}
43856424Skrishna 	if (!copyin_attributes(mode, sp, count, oc_new_attributes, &k_attrs,
43866424Skrishna 	    &k_attrs_size, NULL, &rv, &error, &rctl_bytes,
43876424Skrishna 	    &rctl_chk, B_TRUE)) {
43886424Skrishna 		goto release_minor;
43896424Skrishna 	}
43900Sstevel@tonic-gate 
43910Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
43920Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
4393904Smcpowers 	    CRYPTO_OBJECT_OFFSET(object_copy), CHECK_RESTRICT_FALSE,
43940Sstevel@tonic-gate 	    sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
43950Sstevel@tonic-gate 		goto release_minor;
43960Sstevel@tonic-gate 	}
43970Sstevel@tonic-gate 
43980Sstevel@tonic-gate 	handle = STRUCT_FGET(object_copy, oc_handle);
43990Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_COPY,
44000Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, handle, k_attrs, count,
44010Sstevel@tonic-gate 	    &new_handle, 0, NULL, NULL, 0, NULL);
44020Sstevel@tonic-gate 
44030Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
44040Sstevel@tonic-gate 
44050Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS)
44060Sstevel@tonic-gate 		STRUCT_FSET(object_copy, oc_new_handle, new_handle);
44070Sstevel@tonic-gate 
44080Sstevel@tonic-gate release_minor:
44096424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk);
44100Sstevel@tonic-gate 
44110Sstevel@tonic-gate 	if (k_attrs != NULL)
44120Sstevel@tonic-gate 		kmem_free(k_attrs, k_attrs_size);
44130Sstevel@tonic-gate 
44140Sstevel@tonic-gate 	if (error != 0)
44150Sstevel@tonic-gate 		goto out;
44160Sstevel@tonic-gate 
44170Sstevel@tonic-gate 	STRUCT_FSET(object_copy, oc_return_value, rv);
44180Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(object_copy), arg,
44190Sstevel@tonic-gate 	    STRUCT_SIZE(object_copy)) != 0) {
44200Sstevel@tonic-gate 		if (rv == CRYPTO_SUCCESS) {
44210Sstevel@tonic-gate 			KCF_WRAP_OBJECT_OPS_PARAMS(&params,
44220Sstevel@tonic-gate 			    KCF_OP_OBJECT_DESTROY,
44230Sstevel@tonic-gate 			    sp->sd_provider_session->ps_session, new_handle,
44240Sstevel@tonic-gate 			    NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
44250Sstevel@tonic-gate 
44260Sstevel@tonic-gate 			(void) kcf_submit_request(real_provider, NULL,
44270Sstevel@tonic-gate 			    NULL, &params, B_FALSE);
44280Sstevel@tonic-gate 
44290Sstevel@tonic-gate 			error = EFAULT;
44300Sstevel@tonic-gate 		}
44310Sstevel@tonic-gate 	}
44320Sstevel@tonic-gate out:
44336424Skrishna 	CRYPTO_SESSION_RELE(sp);
44340Sstevel@tonic-gate 	crypto_release_minor(cm);
4435904Smcpowers 	if (real_provider != NULL)
4436904Smcpowers 		KCF_PROV_REFRELE(real_provider);
44370Sstevel@tonic-gate 	return (error);
44380Sstevel@tonic-gate }
44390Sstevel@tonic-gate 
44400Sstevel@tonic-gate /* ARGSUSED */
44410Sstevel@tonic-gate static int
44420Sstevel@tonic-gate object_destroy(dev_t dev, caddr_t arg, int mode, int *rval)
44430Sstevel@tonic-gate {
44440Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_destroy, object_destroy);
44450Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
44460Sstevel@tonic-gate 	kcf_req_params_t params;
44470Sstevel@tonic-gate 	crypto_session_id_t session_id;
44480Sstevel@tonic-gate 	crypto_minor_t *cm;
44490Sstevel@tonic-gate 	crypto_session_data_t *sp;
44500Sstevel@tonic-gate 	crypto_object_id_t handle;
44510Sstevel@tonic-gate 	int error = 0;
44520Sstevel@tonic-gate 	int rv;
44530Sstevel@tonic-gate 
44540Sstevel@tonic-gate 	STRUCT_INIT(object_destroy, mode);
44550Sstevel@tonic-gate 
44560Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
44570Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_destroy: failed holding minor");
44580Sstevel@tonic-gate 		return (ENXIO);
44590Sstevel@tonic-gate 	}
44600Sstevel@tonic-gate 
44610Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(object_destroy),
44620Sstevel@tonic-gate 	    STRUCT_SIZE(object_destroy)) != 0) {
44630Sstevel@tonic-gate 		crypto_release_minor(cm);
44640Sstevel@tonic-gate 		return (EFAULT);
44650Sstevel@tonic-gate 	}
44660Sstevel@tonic-gate 
44670Sstevel@tonic-gate 	session_id = STRUCT_FGET(object_destroy, od_session);
44680Sstevel@tonic-gate 
44690Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
44700Sstevel@tonic-gate 		goto release_minor;
44710Sstevel@tonic-gate 	}
44720Sstevel@tonic-gate 
44730Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
44740Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
4475904Smcpowers 	    CRYPTO_OBJECT_OFFSET(object_destroy), CHECK_RESTRICT_FALSE,
44760Sstevel@tonic-gate 	    sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
44770Sstevel@tonic-gate 		goto out;
44780Sstevel@tonic-gate 	}
44790Sstevel@tonic-gate 
44800Sstevel@tonic-gate 	handle = STRUCT_FGET(object_destroy, od_handle);
44810Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_DESTROY,
44820Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, handle, NULL, 0, NULL, 0,
44830Sstevel@tonic-gate 	    NULL, NULL, 0, NULL);
44840Sstevel@tonic-gate 
44850Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
4486904Smcpowers 	KCF_PROV_REFRELE(real_provider);
44870Sstevel@tonic-gate 
44880Sstevel@tonic-gate out:
44890Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
44900Sstevel@tonic-gate 
44910Sstevel@tonic-gate release_minor:
44920Sstevel@tonic-gate 	crypto_release_minor(cm);
44930Sstevel@tonic-gate 
44940Sstevel@tonic-gate 	if (error != 0)
44950Sstevel@tonic-gate 		return (error);
44960Sstevel@tonic-gate 
44970Sstevel@tonic-gate 	STRUCT_FSET(object_destroy, od_return_value, rv);
44980Sstevel@tonic-gate 
44990Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(object_destroy), arg,
45000Sstevel@tonic-gate 	    STRUCT_SIZE(object_destroy)) != 0) {
45010Sstevel@tonic-gate 		return (EFAULT);
45020Sstevel@tonic-gate 	}
45030Sstevel@tonic-gate 	return (0);
45040Sstevel@tonic-gate }
45050Sstevel@tonic-gate 
45060Sstevel@tonic-gate /* ARGSUSED */
45070Sstevel@tonic-gate static int
45080Sstevel@tonic-gate object_get_attribute_value(dev_t dev, caddr_t arg, int mode, int *rval)
45090Sstevel@tonic-gate {
45100Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_get_attribute_value, get_attribute_value);
45110Sstevel@tonic-gate 	/* LINTED E_FUNC_SET_NOT_USED */
45120Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_attribute, oa);
45130Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
45140Sstevel@tonic-gate 	kcf_req_params_t params;
45150Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
45160Sstevel@tonic-gate 	crypto_session_id_t session_id;
45170Sstevel@tonic-gate 	crypto_minor_t *cm;
45186424Skrishna 	crypto_session_data_t *sp = NULL;
45190Sstevel@tonic-gate 	crypto_object_id_t handle;
45200Sstevel@tonic-gate 	caddr_t og_attributes;
45210Sstevel@tonic-gate 	caddr_t u_attrs;
45220Sstevel@tonic-gate 	size_t k_attrs_size;
45230Sstevel@tonic-gate 	size_t rctl_bytes = 0;
45246424Skrishna 	boolean_t rctl_chk = B_FALSE;
45250Sstevel@tonic-gate 	int error = 0;
45260Sstevel@tonic-gate 	int rv;
45270Sstevel@tonic-gate 	uint_t count;
45280Sstevel@tonic-gate 
45290Sstevel@tonic-gate 	STRUCT_INIT(get_attribute_value, mode);
45300Sstevel@tonic-gate 	STRUCT_INIT(oa, mode);
45310Sstevel@tonic-gate 
45320Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
45330Sstevel@tonic-gate 		cmn_err(CE_WARN,
45340Sstevel@tonic-gate 		    "object_get_attribute_value: failed holding minor");
45350Sstevel@tonic-gate 		return (ENXIO);
45360Sstevel@tonic-gate 	}
45370Sstevel@tonic-gate 
45380Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(get_attribute_value),
45390Sstevel@tonic-gate 	    STRUCT_SIZE(get_attribute_value)) != 0) {
45400Sstevel@tonic-gate 		crypto_release_minor(cm);
45410Sstevel@tonic-gate 		return (EFAULT);
45420Sstevel@tonic-gate 	}
45430Sstevel@tonic-gate 
45440Sstevel@tonic-gate 	count = STRUCT_FGET(get_attribute_value, og_count);
45450Sstevel@tonic-gate 	og_attributes = STRUCT_FGETP(get_attribute_value, og_attributes);
45460Sstevel@tonic-gate 
45470Sstevel@tonic-gate 	session_id = STRUCT_FGET(get_attribute_value, og_session);
45480Sstevel@tonic-gate 
45490Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
45500Sstevel@tonic-gate 		goto release_minor;
45510Sstevel@tonic-gate 	}
45526424Skrishna 	if (!copyin_attributes(mode, sp, count, og_attributes, &k_attrs,
45536424Skrishna 	    &k_attrs_size, &u_attrs, &rv, &error, &rctl_bytes,
45546424Skrishna 	    &rctl_chk, B_FALSE)) {
45556424Skrishna 		goto release_minor;
45566424Skrishna 	}
45570Sstevel@tonic-gate 
45580Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
45590Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
45600Sstevel@tonic-gate 	    CRYPTO_OBJECT_OFFSET(object_get_attribute_value),
4561904Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider))
4562904Smcpowers 	    != CRYPTO_SUCCESS) {
45630Sstevel@tonic-gate 		goto out;
45640Sstevel@tonic-gate 	}
45650Sstevel@tonic-gate 
45660Sstevel@tonic-gate 	handle = STRUCT_FGET(get_attribute_value, og_handle);
45670Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_GET_ATTRIBUTE_VALUE,
45680Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, handle, k_attrs, count, NULL,
45690Sstevel@tonic-gate 	    0, NULL, NULL, 0, NULL);
45700Sstevel@tonic-gate 
45710Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
4572904Smcpowers 	KCF_PROV_REFRELE(real_provider);
45730Sstevel@tonic-gate 
45740Sstevel@tonic-gate out:
45750Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS || rv == CRYPTO_ATTRIBUTE_SENSITIVE ||
45760Sstevel@tonic-gate 	    rv == CRYPTO_ATTRIBUTE_TYPE_INVALID ||
45770Sstevel@tonic-gate 	    rv == CRYPTO_BUFFER_TOO_SMALL) {
45780Sstevel@tonic-gate 		error = copyout_attributes(mode,
45790Sstevel@tonic-gate 		    STRUCT_FGETP(get_attribute_value, og_attributes),
45800Sstevel@tonic-gate 		    count, k_attrs, u_attrs);
45810Sstevel@tonic-gate 	}
45820Sstevel@tonic-gate 
45830Sstevel@tonic-gate release_minor:
45846424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk);
45856424Skrishna 	CRYPTO_SESSION_RELE(sp);
45860Sstevel@tonic-gate 	crypto_release_minor(cm);
45870Sstevel@tonic-gate 
45880Sstevel@tonic-gate 	if (k_attrs != NULL)
45890Sstevel@tonic-gate 		kmem_free(k_attrs, k_attrs_size);
45900Sstevel@tonic-gate 
45910Sstevel@tonic-gate 	if (u_attrs != NULL)
45920Sstevel@tonic-gate 		kmem_free(u_attrs, count * STRUCT_SIZE(oa));
45930Sstevel@tonic-gate 
45940Sstevel@tonic-gate 	if (error != 0)
45950Sstevel@tonic-gate 		return (error);
45960Sstevel@tonic-gate 
45970Sstevel@tonic-gate 	STRUCT_FSET(get_attribute_value, og_return_value, rv);
45980Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(get_attribute_value), arg,
45990Sstevel@tonic-gate 	    STRUCT_SIZE(get_attribute_value)) != 0) {
46000Sstevel@tonic-gate 		return (EFAULT);
46010Sstevel@tonic-gate 	}
46020Sstevel@tonic-gate 	return (0);
46030Sstevel@tonic-gate }
46040Sstevel@tonic-gate 
46050Sstevel@tonic-gate /* ARGSUSED */
46060Sstevel@tonic-gate static int
46070Sstevel@tonic-gate object_get_size(dev_t dev, caddr_t arg, int mode, int *rval)
46080Sstevel@tonic-gate {
46090Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_get_size, object_get_size);
46100Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
46110Sstevel@tonic-gate 	kcf_req_params_t params;
46120Sstevel@tonic-gate 	crypto_session_id_t session_id;
46130Sstevel@tonic-gate 	crypto_minor_t *cm;
46146424Skrishna 	crypto_session_data_t *sp = NULL;
46150Sstevel@tonic-gate 	crypto_object_id_t handle;
46160Sstevel@tonic-gate 	size_t size;
46170Sstevel@tonic-gate 	int error = 0;
46180Sstevel@tonic-gate 	int rv;
46190Sstevel@tonic-gate 
46200Sstevel@tonic-gate 	STRUCT_INIT(object_get_size, mode);
46210Sstevel@tonic-gate 
46220Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
46230Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_get_size: failed holding minor");
46240Sstevel@tonic-gate 		return (ENXIO);
46250Sstevel@tonic-gate 	}
46260Sstevel@tonic-gate 
46270Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(object_get_size),
46280Sstevel@tonic-gate 	    STRUCT_SIZE(object_get_size)) != 0) {
46290Sstevel@tonic-gate 		crypto_release_minor(cm);
46300Sstevel@tonic-gate 		return (EFAULT);
46310Sstevel@tonic-gate 	}
46320Sstevel@tonic-gate 
46330Sstevel@tonic-gate 	session_id = STRUCT_FGET(object_get_size, gs_session);
46340Sstevel@tonic-gate 
46350Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
46360Sstevel@tonic-gate 		goto release_minor;
46370Sstevel@tonic-gate 	}
46380Sstevel@tonic-gate 
46390Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
46400Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
4641904Smcpowers 	    CRYPTO_OBJECT_OFFSET(object_get_size), CHECK_RESTRICT_FALSE,
46420Sstevel@tonic-gate 	    sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
46436424Skrishna 		goto release_minor;
46440Sstevel@tonic-gate 	}
46450Sstevel@tonic-gate 
46460Sstevel@tonic-gate 	handle = STRUCT_FGET(object_get_size, gs_handle);
46470Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_GET_SIZE,
46480Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, handle, NULL, 0, NULL, &size,
46490Sstevel@tonic-gate 	    NULL, NULL, 0, NULL);
46500Sstevel@tonic-gate 
46510Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
4652904Smcpowers 	KCF_PROV_REFRELE(real_provider);
46530Sstevel@tonic-gate 
46540Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
46550Sstevel@tonic-gate 		STRUCT_FSET(object_get_size, gs_size, size);
46560Sstevel@tonic-gate 	}
46576424Skrishna 
46580Sstevel@tonic-gate release_minor:
46590Sstevel@tonic-gate 	crypto_release_minor(cm);
46606424Skrishna 	CRYPTO_SESSION_RELE(sp);
46610Sstevel@tonic-gate 
46620Sstevel@tonic-gate 	if (error != 0)
46630Sstevel@tonic-gate 		return (error);
46640Sstevel@tonic-gate 
46650Sstevel@tonic-gate 	STRUCT_FSET(object_get_size, gs_return_value, rv);
46660Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(object_get_size), arg,
46670Sstevel@tonic-gate 	    STRUCT_SIZE(object_get_size)) != 0) {
46680Sstevel@tonic-gate 		return (EFAULT);
46690Sstevel@tonic-gate 	}
46700Sstevel@tonic-gate 	return (0);
46710Sstevel@tonic-gate }
46720Sstevel@tonic-gate 
46730Sstevel@tonic-gate /* ARGSUSED */
46740Sstevel@tonic-gate static int
46750Sstevel@tonic-gate object_set_attribute_value(dev_t dev, caddr_t arg, int mode, int *rval)
46760Sstevel@tonic-gate {
46770Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_set_attribute_value, set_attribute_value);
46780Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
46790Sstevel@tonic-gate 	kcf_req_params_t params;
46800Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
46810Sstevel@tonic-gate 	crypto_session_id_t session_id;
46820Sstevel@tonic-gate 	crypto_minor_t *cm;
46836424Skrishna 	crypto_session_data_t *sp = NULL;
46840Sstevel@tonic-gate 	crypto_object_id_t object_handle;
46850Sstevel@tonic-gate 	caddr_t sa_attributes;
46860Sstevel@tonic-gate 	size_t k_attrs_size;
46870Sstevel@tonic-gate 	size_t rctl_bytes = 0;
46886424Skrishna 	boolean_t rctl_chk = B_FALSE;
46890Sstevel@tonic-gate 	int error = 0;
46900Sstevel@tonic-gate 	int rv;
46910Sstevel@tonic-gate 	uint_t count;
46920Sstevel@tonic-gate 
46930Sstevel@tonic-gate 	STRUCT_INIT(set_attribute_value, mode);
46940Sstevel@tonic-gate 
46950Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
46960Sstevel@tonic-gate 		cmn_err(CE_WARN,
46970Sstevel@tonic-gate 		    "object_set_attribute_value: failed holding minor");
46980Sstevel@tonic-gate 		return (ENXIO);
46990Sstevel@tonic-gate 	}
47000Sstevel@tonic-gate 
47010Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(set_attribute_value),
47020Sstevel@tonic-gate 	    STRUCT_SIZE(set_attribute_value)) != 0) {
47030Sstevel@tonic-gate 		crypto_release_minor(cm);
47040Sstevel@tonic-gate 		return (EFAULT);
47050Sstevel@tonic-gate 	}
47060Sstevel@tonic-gate 
47070Sstevel@tonic-gate 	count = STRUCT_FGET(set_attribute_value, sa_count);
47080Sstevel@tonic-gate 	sa_attributes = STRUCT_FGETP(set_attribute_value, sa_attributes);
47090Sstevel@tonic-gate 
47100Sstevel@tonic-gate 	session_id = STRUCT_FGET(set_attribute_value, sa_session);
47110Sstevel@tonic-gate 
47120Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
47130Sstevel@tonic-gate 		goto release_minor;
47140Sstevel@tonic-gate 	}
47156424Skrishna 	if (!copyin_attributes(mode, sp, count, sa_attributes, &k_attrs,
47166424Skrishna 	    &k_attrs_size, NULL, &rv, &error, &rctl_bytes,
47176424Skrishna 	    &rctl_chk, B_TRUE)) {
47186424Skrishna 		goto release_minor;
47196424Skrishna 	}
47200Sstevel@tonic-gate 
47210Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
47220Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
47230Sstevel@tonic-gate 	    CRYPTO_OBJECT_OFFSET(object_set_attribute_value),
4724904Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider))
4725904Smcpowers 	    != CRYPTO_SUCCESS) {
47266424Skrishna 		goto release_minor;
47270Sstevel@tonic-gate 	}
47280Sstevel@tonic-gate 
47290Sstevel@tonic-gate 	object_handle = STRUCT_FGET(set_attribute_value, sa_handle);
47300Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_SET_ATTRIBUTE_VALUE,
47310Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, object_handle, k_attrs, count,
47320Sstevel@tonic-gate 	    NULL, 0, NULL, NULL, 0, NULL);
47330Sstevel@tonic-gate 
47340Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
4735904Smcpowers 	KCF_PROV_REFRELE(real_provider);
47360Sstevel@tonic-gate 
47376424Skrishna release_minor:
47386424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk);
47390Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
47400Sstevel@tonic-gate 	crypto_release_minor(cm);
47410Sstevel@tonic-gate 
47420Sstevel@tonic-gate 	if (k_attrs != NULL)
47430Sstevel@tonic-gate 		kmem_free(k_attrs, k_attrs_size);
47440Sstevel@tonic-gate 
47450Sstevel@tonic-gate 	if (error != 0)
47460Sstevel@tonic-gate 		return (error);
47470Sstevel@tonic-gate 
47480Sstevel@tonic-gate 	STRUCT_FSET(set_attribute_value, sa_return_value, rv);
47490Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(set_attribute_value), arg,
47500Sstevel@tonic-gate 	    STRUCT_SIZE(set_attribute_value)) != 0) {
47510Sstevel@tonic-gate 		return (EFAULT);
47520Sstevel@tonic-gate 	}
47530Sstevel@tonic-gate 	return (0);
47540Sstevel@tonic-gate }
47550Sstevel@tonic-gate 
47560Sstevel@tonic-gate /* ARGSUSED */
47570Sstevel@tonic-gate static int
47580Sstevel@tonic-gate object_find_init(dev_t dev, caddr_t arg, int mode, int *rval)
47590Sstevel@tonic-gate {
47600Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_find_init, find_init);
4761904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
47620Sstevel@tonic-gate 	kcf_req_params_t params;
47630Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
47640Sstevel@tonic-gate 	crypto_session_id_t session_id;
47650Sstevel@tonic-gate 	crypto_minor_t *cm;
47666424Skrishna 	crypto_session_data_t *sp = NULL;
47670Sstevel@tonic-gate 	caddr_t attributes;
47680Sstevel@tonic-gate 	size_t k_attrs_size;
47690Sstevel@tonic-gate 	size_t rctl_bytes = 0;
47706424Skrishna 	boolean_t rctl_chk = B_FALSE;
47710Sstevel@tonic-gate 	int error = 0;
47720Sstevel@tonic-gate 	int rv;
47730Sstevel@tonic-gate 	uint_t count;
47740Sstevel@tonic-gate 	void *cookie;
47750Sstevel@tonic-gate 
47760Sstevel@tonic-gate 	STRUCT_INIT(find_init, mode);
47770Sstevel@tonic-gate 
47780Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
47790Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_find_init: failed holding minor");
47800Sstevel@tonic-gate 		return (ENXIO);
47810Sstevel@tonic-gate 	}
47820Sstevel@tonic-gate 
47830Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(find_init), STRUCT_SIZE(find_init)) != 0) {
47840Sstevel@tonic-gate 		crypto_release_minor(cm);
47850Sstevel@tonic-gate 		return (EFAULT);
47860Sstevel@tonic-gate 	}
47870Sstevel@tonic-gate 
47880Sstevel@tonic-gate 	count = STRUCT_FGET(find_init, fi_count);
47890Sstevel@tonic-gate 	attributes = STRUCT_FGETP(find_init, fi_attributes);
47900Sstevel@tonic-gate 
47910Sstevel@tonic-gate 	session_id = STRUCT_FGET(find_init, fi_session);
47920Sstevel@tonic-gate 
47930Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
47940Sstevel@tonic-gate 		goto release_minor;
47950Sstevel@tonic-gate 	}
47966424Skrishna 	if (!copyin_attributes(mode, sp, count, attributes, &k_attrs,
47976424Skrishna 	    &k_attrs_size, NULL, &rv, &error, &rctl_bytes,
47986424Skrishna 	    &rctl_chk, B_TRUE)) {
47996424Skrishna 		goto release_minor;
48006424Skrishna 	}
48010Sstevel@tonic-gate 
48020Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
48030Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
4804904Smcpowers 	    CRYPTO_OBJECT_OFFSET(object_find_init), CHECK_RESTRICT_FALSE,
48050Sstevel@tonic-gate 	    sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
48066424Skrishna 		goto release_minor;
48070Sstevel@tonic-gate 	}
48080Sstevel@tonic-gate 
48090Sstevel@tonic-gate 	/* check for an active find */
48100Sstevel@tonic-gate 	if (sp->sd_find_init_cookie != NULL) {
48110Sstevel@tonic-gate 		rv = CRYPTO_OPERATION_IS_ACTIVE;
48120Sstevel@tonic-gate 		goto release_minor;
48130Sstevel@tonic-gate 	}
48140Sstevel@tonic-gate 
48150Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_FIND_INIT,
48160Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, 0, k_attrs, count, NULL, 0,
48170Sstevel@tonic-gate 	    &cookie, NULL, 0, NULL);
48180Sstevel@tonic-gate 
48190Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
48200Sstevel@tonic-gate 
48210Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
48220Sstevel@tonic-gate 		/*
48230Sstevel@tonic-gate 		 * The cookie is allocated by a provider at the start of an
48240Sstevel@tonic-gate 		 * object search.  It is freed when the search is terminated
48250Sstevel@tonic-gate 		 * by a final operation, or when the session is closed.
48260Sstevel@tonic-gate 		 * It contains state information about which object handles
48270Sstevel@tonic-gate 		 * have been returned to the caller.
48280Sstevel@tonic-gate 		 */
48290Sstevel@tonic-gate 		sp->sd_find_init_cookie = cookie;
48300Sstevel@tonic-gate 	}
48310Sstevel@tonic-gate 
48326424Skrishna release_minor:
48336424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk);
48340Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
48350Sstevel@tonic-gate 	crypto_release_minor(cm);
48360Sstevel@tonic-gate 
4837904Smcpowers 	if (real_provider != NULL)
4838904Smcpowers 		KCF_PROV_REFRELE(real_provider);
4839904Smcpowers 
48400Sstevel@tonic-gate 	if (k_attrs != NULL)
48410Sstevel@tonic-gate 		kmem_free(k_attrs, k_attrs_size);
48420Sstevel@tonic-gate 
48430Sstevel@tonic-gate 	if (error != 0)
48440Sstevel@tonic-gate 		return (error);
48450Sstevel@tonic-gate 
48460Sstevel@tonic-gate 	STRUCT_FSET(find_init, fi_return_value, rv);
48470Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(find_init), arg, STRUCT_SIZE(find_init)) != 0) {
48480Sstevel@tonic-gate 		return (EFAULT);
48490Sstevel@tonic-gate 	}
48500Sstevel@tonic-gate 	return (0);
48510Sstevel@tonic-gate }
48520Sstevel@tonic-gate 
48530Sstevel@tonic-gate /* ARGSUSED */
48540Sstevel@tonic-gate static int
48550Sstevel@tonic-gate object_find_update(dev_t dev, caddr_t arg, int mode, int *rval)
48560Sstevel@tonic-gate {
48570Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_find_update, find_update);
48580Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
48590Sstevel@tonic-gate 	kcf_req_params_t params;
48600Sstevel@tonic-gate 	crypto_minor_t *cm;
48616424Skrishna 	crypto_session_data_t *sp = NULL;
48620Sstevel@tonic-gate 	crypto_object_id_t *buffer = NULL;
48630Sstevel@tonic-gate 	crypto_session_id_t session_id;
48640Sstevel@tonic-gate 	size_t len, rctl_bytes = 0;
48650Sstevel@tonic-gate 	uint_t count, max_count;
48660Sstevel@tonic-gate 	int rv, error = 0;
48676424Skrishna 	boolean_t rctl_chk = B_FALSE;
48680Sstevel@tonic-gate 
48690Sstevel@tonic-gate 	STRUCT_INIT(find_update, mode);
48700Sstevel@tonic-gate 
48710Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
48720Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_find_update: failed holding minor");
48730Sstevel@tonic-gate 		return (ENXIO);
48740Sstevel@tonic-gate 	}
48750Sstevel@tonic-gate 
48760Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(find_update),
48770Sstevel@tonic-gate 	    STRUCT_SIZE(find_update)) != 0) {
48780Sstevel@tonic-gate 		crypto_release_minor(cm);
48790Sstevel@tonic-gate 		return (EFAULT);
48800Sstevel@tonic-gate 	}
48810Sstevel@tonic-gate 
48820Sstevel@tonic-gate 	max_count = STRUCT_FGET(find_update, fu_max_count);
48830Sstevel@tonic-gate 	if (max_count > CRYPTO_MAX_FIND_COUNT) {
48840Sstevel@tonic-gate 		cmn_err(CE_NOTE, "object_find_update: count greater than %d, "
48850Sstevel@tonic-gate 		    "pid = %d", CRYPTO_MAX_FIND_COUNT, curproc->p_pid);
48860Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
48870Sstevel@tonic-gate 		goto release_minor;
48880Sstevel@tonic-gate 	}
48890Sstevel@tonic-gate 	len = max_count * sizeof (crypto_object_id_t);
48906424Skrishna 	session_id = STRUCT_FGET(find_update, fu_session);
48916424Skrishna 
48926424Skrishna 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
48936424Skrishna 		goto release_minor;
48946424Skrishna 	}
48956424Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, len, rctl_chk)) !=
48966424Skrishna 	    CRYPTO_SUCCESS) {
48970Sstevel@tonic-gate 		goto release_minor;
48980Sstevel@tonic-gate 	}
48990Sstevel@tonic-gate 	rctl_bytes = len;
49000Sstevel@tonic-gate 	buffer = kmem_alloc(len, KM_SLEEP);
49010Sstevel@tonic-gate 
49020Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
49030Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
4904904Smcpowers 	    CRYPTO_OBJECT_OFFSET(object_find), CHECK_RESTRICT_FALSE,
49050Sstevel@tonic-gate 	    sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
49066424Skrishna 		goto release_minor;
49070Sstevel@tonic-gate 	}
49080Sstevel@tonic-gate 
49090Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_FIND,
49100Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, 0, NULL, 0, buffer, 0,
49110Sstevel@tonic-gate 	    NULL, sp->sd_find_init_cookie, max_count, &count);
49120Sstevel@tonic-gate 
49130Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
4914904Smcpowers 	KCF_PROV_REFRELE(real_provider);
49150Sstevel@tonic-gate 
49160Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
49170Sstevel@tonic-gate 		if (count > max_count) {
49180Sstevel@tonic-gate 			/* bad bad provider */
49190Sstevel@tonic-gate 			rv = CRYPTO_FAILED;
49200Sstevel@tonic-gate 			goto release_minor;
49210Sstevel@tonic-gate 		}
49220Sstevel@tonic-gate 		if (count != 0) {
49230Sstevel@tonic-gate 			/* copyout handles */
49240Sstevel@tonic-gate 			if (copyout(buffer,
49250Sstevel@tonic-gate 			    STRUCT_FGETP(find_update, fu_handles),
49260Sstevel@tonic-gate 			    count * sizeof (crypto_object_id_t)) != 0) {
49270Sstevel@tonic-gate 				error = EFAULT;
49280Sstevel@tonic-gate 			}
49290Sstevel@tonic-gate 		}
49300Sstevel@tonic-gate 		STRUCT_FSET(find_update, fu_count, count);
49310Sstevel@tonic-gate 	}
49320Sstevel@tonic-gate 
49330Sstevel@tonic-gate release_minor:
49346424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk);
49356424Skrishna 	CRYPTO_SESSION_RELE(sp);
49360Sstevel@tonic-gate 	crypto_release_minor(cm);
49370Sstevel@tonic-gate 
49380Sstevel@tonic-gate 	if (buffer != NULL)
49390Sstevel@tonic-gate 		kmem_free(buffer, len);
49400Sstevel@tonic-gate 
49410Sstevel@tonic-gate 	if (error != 0)
49420Sstevel@tonic-gate 		return (error);
49430Sstevel@tonic-gate 
49440Sstevel@tonic-gate 	STRUCT_FSET(find_update, fu_return_value, rv);
49450Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(find_update), arg,
49460Sstevel@tonic-gate 	    STRUCT_SIZE(find_update)) != 0) {
49470Sstevel@tonic-gate 		return (EFAULT);
49480Sstevel@tonic-gate 	}
49490Sstevel@tonic-gate 
49500Sstevel@tonic-gate 	return (0);
49510Sstevel@tonic-gate }
49520Sstevel@tonic-gate 
49530Sstevel@tonic-gate /*
49540Sstevel@tonic-gate  * Free provider-allocated storage used for find object searches.
49550Sstevel@tonic-gate  */
49560Sstevel@tonic-gate static int
49570Sstevel@tonic-gate crypto_free_find_ctx(crypto_session_data_t *sp)
49580Sstevel@tonic-gate {
49590Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
49600Sstevel@tonic-gate 	kcf_req_params_t params;
49610Sstevel@tonic-gate 	int rv;
49620Sstevel@tonic-gate 
49630Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
49640Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
4965904Smcpowers 	    CRYPTO_OBJECT_OFFSET(object_find_final), CHECK_RESTRICT_FALSE,
49660Sstevel@tonic-gate 	    sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
49670Sstevel@tonic-gate 		return (rv);
49680Sstevel@tonic-gate 	}
49690Sstevel@tonic-gate 
49700Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_FIND_FINAL,
49710Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, 0, NULL, 0, NULL, 0,
49720Sstevel@tonic-gate 	    NULL, sp->sd_find_init_cookie, 0, NULL);
49730Sstevel@tonic-gate 
4974904Smcpowers 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
4975904Smcpowers 	KCF_PROV_REFRELE(real_provider);
4976904Smcpowers 	return (rv);
49770Sstevel@tonic-gate }
49780Sstevel@tonic-gate 
49790Sstevel@tonic-gate /* ARGSUSED */
49800Sstevel@tonic-gate static int
49810Sstevel@tonic-gate object_find_final(dev_t dev, caddr_t arg, int mode, int *rval)
49820Sstevel@tonic-gate {
49830Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_find_final, object_find_final);
49840Sstevel@tonic-gate 	crypto_session_id_t session_id;
49850Sstevel@tonic-gate 	crypto_minor_t *cm;
49860Sstevel@tonic-gate 	crypto_session_data_t *sp;
49870Sstevel@tonic-gate 	int error = 0;
49880Sstevel@tonic-gate 	int rv;
49890Sstevel@tonic-gate 
49900Sstevel@tonic-gate 	STRUCT_INIT(object_find_final, mode);
49910Sstevel@tonic-gate 
49920Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
49930Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_find_final: failed holding minor");
49940Sstevel@tonic-gate 		return (ENXIO);
49950Sstevel@tonic-gate 	}
49960Sstevel@tonic-gate 
49970Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(object_find_final),
49980Sstevel@tonic-gate 	    STRUCT_SIZE(object_find_final)) != 0) {
49990Sstevel@tonic-gate 		crypto_release_minor(cm);
50000Sstevel@tonic-gate 		return (EFAULT);
50010Sstevel@tonic-gate 	}
50020Sstevel@tonic-gate 
50030Sstevel@tonic-gate 	session_id = STRUCT_FGET(object_find_final, ff_session);
50040Sstevel@tonic-gate 
50050Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
50060Sstevel@tonic-gate 		goto release_minor;
50070Sstevel@tonic-gate 	}
50080Sstevel@tonic-gate 
50090Sstevel@tonic-gate 	if ((rv = crypto_free_find_ctx(sp)) == CRYPTO_SUCCESS) {
50100Sstevel@tonic-gate 		sp->sd_find_init_cookie = NULL;
50110Sstevel@tonic-gate 	}
50120Sstevel@tonic-gate 
50130Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
50140Sstevel@tonic-gate 
50150Sstevel@tonic-gate release_minor:
50160Sstevel@tonic-gate 	crypto_release_minor(cm);
50170Sstevel@tonic-gate 
50180Sstevel@tonic-gate 	if (error != 0)
50190Sstevel@tonic-gate 		return (error);
50200Sstevel@tonic-gate 
50210Sstevel@tonic-gate 	STRUCT_FSET(object_find_final, ff_return_value, rv);
50220Sstevel@tonic-gate 
50230Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(object_find_final), arg,
50240Sstevel@tonic-gate 	    STRUCT_SIZE(object_find_final)) != 0) {
50250Sstevel@tonic-gate 		return (EFAULT);
50260Sstevel@tonic-gate 	}
50270Sstevel@tonic-gate 	return (0);
50280Sstevel@tonic-gate }
50290Sstevel@tonic-gate 
50300Sstevel@tonic-gate /* ARGSUSED */
50310Sstevel@tonic-gate static int
50320Sstevel@tonic-gate object_generate_key(dev_t dev, caddr_t arg, int mode, int *rval)
50330Sstevel@tonic-gate {
50340Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_generate_key, generate_key);
5035904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
50360Sstevel@tonic-gate 	kcf_req_params_t params;
50370Sstevel@tonic-gate 	crypto_mechanism_t mech;
50380Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
50390Sstevel@tonic-gate 	crypto_session_id_t session_id;
50400Sstevel@tonic-gate 	crypto_minor_t *cm;
50410Sstevel@tonic-gate 	crypto_session_data_t *sp = NULL;
50420Sstevel@tonic-gate 	crypto_object_id_t key_handle;
50430Sstevel@tonic-gate 	caddr_t attributes;
50440Sstevel@tonic-gate 	size_t k_attrs_size;
50450Sstevel@tonic-gate 	size_t mech_rctl_bytes = 0, key_rctl_bytes = 0;
50466424Skrishna 	boolean_t mech_rctl_chk = B_FALSE;
50476424Skrishna 	boolean_t key_rctl_chk = B_FALSE;
50480Sstevel@tonic-gate 	uint_t count;
50490Sstevel@tonic-gate 	int error = 0;
50500Sstevel@tonic-gate 	int rv;
5051904Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
50520Sstevel@tonic-gate 
50530Sstevel@tonic-gate 	STRUCT_INIT(generate_key, mode);
50540Sstevel@tonic-gate 
50550Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
50560Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_generate_key: failed holding minor");
50570Sstevel@tonic-gate 		return (ENXIO);
50580Sstevel@tonic-gate 	}
50590Sstevel@tonic-gate 
50600Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(generate_key),
50610Sstevel@tonic-gate 	    STRUCT_SIZE(generate_key)) != 0) {
50620Sstevel@tonic-gate 		crypto_release_minor(cm);
50630Sstevel@tonic-gate 		return (EFAULT);
50640Sstevel@tonic-gate 	}
50650Sstevel@tonic-gate 
50660Sstevel@tonic-gate 	session_id = STRUCT_FGET(generate_key, gk_session);
50670Sstevel@tonic-gate 
50680Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
50690Sstevel@tonic-gate 		goto release_minor;
50700Sstevel@tonic-gate 	}
50710Sstevel@tonic-gate 
5072904Smcpowers 	bcopy(STRUCT_FADDR(generate_key, gk_mechanism), &mech.cm_type,
5073904Smcpowers 	    sizeof (crypto_mech_type_t));
5074904Smcpowers 
5075904Smcpowers 	if ((rv = kcf_get_hardware_provider(mech.cm_type, CRYPTO_MECH_INVALID,
50761808Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider,
50771808Smcpowers 	    CRYPTO_FG_GENERATE)) != CRYPTO_SUCCESS) {
50780Sstevel@tonic-gate 		goto release_minor;
50790Sstevel@tonic-gate 	}
50800Sstevel@tonic-gate 
5081904Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
5082904Smcpowers 	    STRUCT_FADDR(generate_key, gk_mechanism), &mech, mode, &error);
5083904Smcpowers 
5084904Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
5085904Smcpowers 		allocated_by_crypto_module = B_TRUE;
50866424Skrishna 		if (!copyin_mech(mode, sp,
50876424Skrishna 		    STRUCT_FADDR(generate_key, gk_mechanism),
50886424Skrishna 		    &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) {
5089904Smcpowers 			goto release_minor;
5090904Smcpowers 		}
5091904Smcpowers 	} else {
5092904Smcpowers 		if (rv != CRYPTO_SUCCESS)
5093904Smcpowers 			goto release_minor;
50940Sstevel@tonic-gate 	}
50950Sstevel@tonic-gate 
50960Sstevel@tonic-gate 	count = STRUCT_FGET(generate_key, gk_count);
50970Sstevel@tonic-gate 	attributes = STRUCT_FGETP(generate_key, gk_attributes);
50986424Skrishna 	if (!copyin_attributes(mode, sp, count, attributes, &k_attrs,
50996424Skrishna 	    &k_attrs_size, NULL, &rv, &error, &key_rctl_bytes,
51006424Skrishna 	    &key_rctl_chk, B_TRUE)) {
51010Sstevel@tonic-gate 		goto release_minor;
51020Sstevel@tonic-gate 	}
51030Sstevel@tonic-gate 
51040Sstevel@tonic-gate 	KCF_WRAP_KEY_OPS_PARAMS(&params, KCF_OP_KEY_GENERATE,
51050Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, &mech, k_attrs, count,
51060Sstevel@tonic-gate 	    &key_handle, NULL, 0, NULL, NULL, NULL, 0);
51070Sstevel@tonic-gate 
51080Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
51090Sstevel@tonic-gate 
51100Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS)
51110Sstevel@tonic-gate 		STRUCT_FSET(generate_key, gk_handle, key_handle);
51120Sstevel@tonic-gate 
51130Sstevel@tonic-gate release_minor:
51146424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
51156424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk);
51160Sstevel@tonic-gate 
51170Sstevel@tonic-gate 	if (k_attrs != NULL)
51180Sstevel@tonic-gate 		kmem_free(k_attrs, k_attrs_size);
51190Sstevel@tonic-gate 
51200Sstevel@tonic-gate 	if (error != 0)
51210Sstevel@tonic-gate 		goto out;
51220Sstevel@tonic-gate 
51230Sstevel@tonic-gate 	STRUCT_FSET(generate_key, gk_return_value, rv);
51240Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(generate_key), arg,
51250Sstevel@tonic-gate 	    STRUCT_SIZE(generate_key)) != 0) {
51260Sstevel@tonic-gate 		if (rv == CRYPTO_SUCCESS) {
51270Sstevel@tonic-gate 			KCF_WRAP_OBJECT_OPS_PARAMS(&params,
51280Sstevel@tonic-gate 			    KCF_OP_OBJECT_DESTROY,
51290Sstevel@tonic-gate 			    sp->sd_provider_session->ps_session, key_handle,
51300Sstevel@tonic-gate 			    NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
51310Sstevel@tonic-gate 
51320Sstevel@tonic-gate 			(void) kcf_submit_request(real_provider, NULL,
51330Sstevel@tonic-gate 			    NULL, &params, B_FALSE);
51340Sstevel@tonic-gate 
51350Sstevel@tonic-gate 			error = EFAULT;
51360Sstevel@tonic-gate 		}
51370Sstevel@tonic-gate 	}
51380Sstevel@tonic-gate out:
51396424Skrishna 	CRYPTO_SESSION_RELE(sp);
51400Sstevel@tonic-gate 	crypto_release_minor(cm);
5141904Smcpowers 
5142904Smcpowers 	if (real_provider != NULL) {
5143904Smcpowers 		crypto_free_mech(real_provider,
5144904Smcpowers 		    allocated_by_crypto_module, &mech);
5145904Smcpowers 		KCF_PROV_REFRELE(real_provider);
5146904Smcpowers 	}
51470Sstevel@tonic-gate 	return (error);
51480Sstevel@tonic-gate }
51490Sstevel@tonic-gate 
51500Sstevel@tonic-gate /* ARGSUSED */
51510Sstevel@tonic-gate static int
51524219Smcpowers nostore_generate_key(dev_t dev, caddr_t arg, int mode, int *rval)
51534219Smcpowers {
51544219Smcpowers 	STRUCT_DECL(crypto_nostore_generate_key, generate_key);
51554219Smcpowers 	/* LINTED E_FUNC_SET_NOT_USED */
51564219Smcpowers 	STRUCT_DECL(crypto_object_attribute, oa);
51574219Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
51584219Smcpowers 	kcf_req_params_t params;
51594219Smcpowers 	crypto_mechanism_t mech;
51604219Smcpowers 	crypto_object_attribute_t *k_in_attrs = NULL;
51614219Smcpowers 	crypto_object_attribute_t *k_out_attrs = NULL;
51624219Smcpowers 	crypto_session_id_t session_id;
51634219Smcpowers 	crypto_minor_t *cm;
51644219Smcpowers 	crypto_session_data_t *sp = NULL;
51654219Smcpowers 	caddr_t in_attributes;
51664219Smcpowers 	caddr_t out_attributes;
51674219Smcpowers 	size_t k_in_attrs_size;
51684219Smcpowers 	size_t k_out_attrs_size;
51694219Smcpowers 	size_t mech_rctl_bytes = 0;
51706424Skrishna 	boolean_t mech_rctl_chk = B_FALSE;
51714219Smcpowers 	size_t in_key_rctl_bytes = 0, out_key_rctl_bytes = 0;
51726424Skrishna 	boolean_t in_key_rctl_chk = B_FALSE;
51736424Skrishna 	boolean_t out_key_rctl_chk = B_FALSE;
51744219Smcpowers 	uint_t in_count;
51754219Smcpowers 	uint_t out_count;
51764219Smcpowers 	int error = 0;
51774219Smcpowers 	int rv;
51784219Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
51794219Smcpowers 	caddr_t u_attrs = NULL;
51804219Smcpowers 
51814219Smcpowers 	STRUCT_INIT(generate_key, mode);
51824219Smcpowers 	STRUCT_INIT(oa, mode);
51834219Smcpowers 
51844219Smcpowers 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
51854219Smcpowers 		cmn_err(CE_WARN, "nostore_generate_key: failed holding minor");
51864219Smcpowers 		return (ENXIO);
51874219Smcpowers 	}
51884219Smcpowers 
51894219Smcpowers 	if (copyin(arg, STRUCT_BUF(generate_key),
51904219Smcpowers 	    STRUCT_SIZE(generate_key)) != 0) {
51914219Smcpowers 		crypto_release_minor(cm);
51924219Smcpowers 		return (EFAULT);
51934219Smcpowers 	}
51944219Smcpowers 
51954219Smcpowers 	session_id = STRUCT_FGET(generate_key, ngk_session);
51964219Smcpowers 
51974219Smcpowers 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
51984219Smcpowers 		goto release_minor;
51994219Smcpowers 	}
52004219Smcpowers 
52014219Smcpowers 	bcopy(STRUCT_FADDR(generate_key, ngk_mechanism), &mech.cm_type,
52024219Smcpowers 	    sizeof (crypto_mech_type_t));
52034219Smcpowers 
52044219Smcpowers 	if ((rv = kcf_get_hardware_provider(mech.cm_type, CRYPTO_MECH_INVALID,
52054219Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider,
52064219Smcpowers 	    CRYPTO_FG_GENERATE)) != CRYPTO_SUCCESS) {
52074219Smcpowers 		goto release_minor;
52084219Smcpowers 	}
52094219Smcpowers 
52104219Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
52114219Smcpowers 	    STRUCT_FADDR(generate_key, ngk_mechanism), &mech, mode, &error);
52124219Smcpowers 
52134219Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
52144219Smcpowers 		allocated_by_crypto_module = B_TRUE;
52156424Skrishna 		if (!copyin_mech(mode, sp, STRUCT_FADDR(generate_key,
52166424Skrishna 		    ngk_mechanism), &mech, &mech_rctl_bytes,
52176424Skrishna 		    &mech_rctl_chk, &rv, &error)) {
52184219Smcpowers 			goto release_minor;
52194219Smcpowers 		}
52204219Smcpowers 	} else {
52214219Smcpowers 		if (rv != CRYPTO_SUCCESS)
52224219Smcpowers 			goto release_minor;
52234219Smcpowers 	}
52244219Smcpowers 
52254219Smcpowers 	in_count = STRUCT_FGET(generate_key, ngk_in_count);
52264219Smcpowers 	in_attributes = STRUCT_FGETP(generate_key, ngk_in_attributes);
52276424Skrishna 	if (!copyin_attributes(mode, sp, in_count, in_attributes, &k_in_attrs,
52284219Smcpowers 	    &k_in_attrs_size, NULL, &rv, &error, &in_key_rctl_bytes,
52296424Skrishna 	    &in_key_rctl_chk, B_TRUE)) {
52304219Smcpowers 		goto release_minor;
52314219Smcpowers 	}
52324219Smcpowers 
52334219Smcpowers 	out_count = STRUCT_FGET(generate_key, ngk_out_count);
52344219Smcpowers 	out_attributes = STRUCT_FGETP(generate_key, ngk_out_attributes);
52356424Skrishna 	if (!copyin_attributes(mode, sp, out_count, out_attributes,
52366424Skrishna 	    &k_out_attrs,
52374219Smcpowers 	    &k_out_attrs_size, &u_attrs, &rv, &error, &out_key_rctl_bytes,
52386424Skrishna 	    &out_key_rctl_chk, B_FALSE)) {
52394219Smcpowers 		goto release_minor;
52404219Smcpowers 	}
52414219Smcpowers 
52424219Smcpowers 	KCF_WRAP_NOSTORE_KEY_OPS_PARAMS(&params, KCF_OP_KEY_GENERATE,
52434219Smcpowers 	    sp->sd_provider_session->ps_session, &mech, k_in_attrs, in_count,
52444219Smcpowers 	    NULL, 0, NULL, k_out_attrs, out_count, NULL, 0);
52454219Smcpowers 
52464219Smcpowers 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
52474219Smcpowers 
52484219Smcpowers 	if (rv == CRYPTO_SUCCESS) {
52494219Smcpowers 		error = copyout_attributes(mode, out_attributes,
52504219Smcpowers 		    out_count, k_out_attrs, u_attrs);
52514219Smcpowers 	}
52524219Smcpowers release_minor:
52536424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
52546424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, in_key_rctl_bytes, in_key_rctl_chk);
52556424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, out_key_rctl_bytes,
52566424Skrishna 	    out_key_rctl_chk);
52574219Smcpowers 
52584219Smcpowers 	if (k_in_attrs != NULL)
52594219Smcpowers 		kmem_free(k_in_attrs, k_in_attrs_size);
52604219Smcpowers 	if (k_out_attrs != NULL) {
52614219Smcpowers 		bzero(k_out_attrs, k_out_attrs_size);
52624219Smcpowers 		kmem_free(k_out_attrs, k_out_attrs_size);
52634219Smcpowers 	}
52644219Smcpowers 
52654219Smcpowers 	if (u_attrs != NULL)
52664219Smcpowers 		kmem_free(u_attrs, out_count * STRUCT_SIZE(oa));
52674219Smcpowers 
52684219Smcpowers 	if (error != 0)
52694219Smcpowers 		goto out;
52704219Smcpowers 
52714219Smcpowers 	STRUCT_FSET(generate_key, ngk_return_value, rv);
52724219Smcpowers 	if (copyout(STRUCT_BUF(generate_key), arg,
52734219Smcpowers 	    STRUCT_SIZE(generate_key)) != 0) {
52744219Smcpowers 		error = EFAULT;
52754219Smcpowers 	}
52764219Smcpowers out:
52776424Skrishna 	CRYPTO_SESSION_RELE(sp);
52784219Smcpowers 	crypto_release_minor(cm);
52794219Smcpowers 
52804219Smcpowers 	if (real_provider != NULL) {
52814219Smcpowers 		crypto_free_mech(real_provider,
52824219Smcpowers 		    allocated_by_crypto_module, &mech);
52834219Smcpowers 		KCF_PROV_REFRELE(real_provider);
52844219Smcpowers 	}
52854219Smcpowers 	return (error);
52864219Smcpowers }
52874219Smcpowers 
52884219Smcpowers /* ARGSUSED */
52894219Smcpowers static int
52900Sstevel@tonic-gate object_generate_key_pair(dev_t dev, caddr_t arg, int mode, int *rval)
52910Sstevel@tonic-gate {
52920Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_generate_key_pair, generate_key_pair);
5293904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
52940Sstevel@tonic-gate 	kcf_req_params_t params;
52950Sstevel@tonic-gate 	crypto_mechanism_t mech;
52960Sstevel@tonic-gate 	crypto_object_attribute_t *k_pub_attrs = NULL;
52970Sstevel@tonic-gate 	crypto_object_attribute_t *k_pri_attrs = NULL;
52980Sstevel@tonic-gate 	crypto_session_id_t session_id;
52990Sstevel@tonic-gate 	crypto_minor_t *cm;
53000Sstevel@tonic-gate 	crypto_session_data_t *sp = NULL;
53010Sstevel@tonic-gate 	crypto_object_id_t pub_handle;
53020Sstevel@tonic-gate 	crypto_object_id_t pri_handle;
53030Sstevel@tonic-gate 	caddr_t pri_attributes;
53040Sstevel@tonic-gate 	caddr_t pub_attributes;
53050Sstevel@tonic-gate 	size_t k_pub_attrs_size, k_pri_attrs_size;
53060Sstevel@tonic-gate 	size_t mech_rctl_bytes = 0;
53076424Skrishna 	boolean_t mech_rctl_chk = B_FALSE;
53080Sstevel@tonic-gate 	size_t pub_rctl_bytes = 0;
53096424Skrishna 	boolean_t pub_rctl_chk = B_FALSE;
53100Sstevel@tonic-gate 	size_t pri_rctl_bytes = 0;
53116424Skrishna 	boolean_t pri_rctl_chk = B_FALSE;
53120Sstevel@tonic-gate 	uint_t pub_count;
53130Sstevel@tonic-gate 	uint_t pri_count;
53140Sstevel@tonic-gate 	int error = 0;
53150Sstevel@tonic-gate 	int rv;
5316904Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
53170Sstevel@tonic-gate 
53180Sstevel@tonic-gate 	STRUCT_INIT(generate_key_pair, mode);
53190Sstevel@tonic-gate 
53200Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
53210Sstevel@tonic-gate 		cmn_err(CE_WARN,
53220Sstevel@tonic-gate 		    "object_generate_key_pair: failed holding minor");
53230Sstevel@tonic-gate 		return (ENXIO);
53240Sstevel@tonic-gate 	}
53250Sstevel@tonic-gate 
53260Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(generate_key_pair),
53270Sstevel@tonic-gate 	    STRUCT_SIZE(generate_key_pair)) != 0) {
53280Sstevel@tonic-gate 		crypto_release_minor(cm);
53290Sstevel@tonic-gate 		return (EFAULT);
53300Sstevel@tonic-gate 	}
53310Sstevel@tonic-gate 
53320Sstevel@tonic-gate 	session_id = STRUCT_FGET(generate_key_pair, kp_session);
53330Sstevel@tonic-gate 
53340Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
53350Sstevel@tonic-gate 		goto release_minor;
53360Sstevel@tonic-gate 	}
53370Sstevel@tonic-gate 
5338904Smcpowers 	bcopy(STRUCT_FADDR(generate_key_pair, kp_mechanism), &mech.cm_type,
5339904Smcpowers 	    sizeof (crypto_mech_type_t));
5340904Smcpowers 
5341904Smcpowers 	if ((rv = kcf_get_hardware_provider(mech.cm_type, CRYPTO_MECH_INVALID,
53421808Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider,
53431808Smcpowers 	    CRYPTO_FG_GENERATE_KEY_PAIR)) != CRYPTO_SUCCESS) {
53440Sstevel@tonic-gate 		goto release_minor;
53450Sstevel@tonic-gate 	}
53460Sstevel@tonic-gate 
5347904Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
5348904Smcpowers 	    STRUCT_FADDR(generate_key_pair, kp_mechanism), &mech, mode, &error);
5349904Smcpowers 
5350904Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
5351904Smcpowers 		allocated_by_crypto_module = B_TRUE;
53526424Skrishna 		if (!copyin_mech(mode, sp, STRUCT_FADDR(generate_key_pair,
53536424Skrishna 		    kp_mechanism), &mech, &mech_rctl_bytes,
53546424Skrishna 		    &mech_rctl_chk, &rv, &error)) {
5355904Smcpowers 			goto release_minor;
5356904Smcpowers 		}
5357904Smcpowers 	} else {
5358904Smcpowers 		if (rv != CRYPTO_SUCCESS)
5359904Smcpowers 			goto release_minor;
53600Sstevel@tonic-gate 	}
53610Sstevel@tonic-gate 
53620Sstevel@tonic-gate 	pub_count = STRUCT_FGET(generate_key_pair, kp_public_count);
53630Sstevel@tonic-gate 	pri_count = STRUCT_FGET(generate_key_pair, kp_private_count);
53640Sstevel@tonic-gate 
53650Sstevel@tonic-gate 	pub_attributes = STRUCT_FGETP(generate_key_pair, kp_public_attributes);
53666424Skrishna 	if (!copyin_attributes(mode, sp, pub_count, pub_attributes,
53676424Skrishna 	    &k_pub_attrs, &k_pub_attrs_size, NULL, &rv, &error, &pub_rctl_bytes,
53686424Skrishna 	    &pub_rctl_chk, B_TRUE)) {
53690Sstevel@tonic-gate 		goto release_minor;
53700Sstevel@tonic-gate 	}
53710Sstevel@tonic-gate 
53720Sstevel@tonic-gate 	pri_attributes = STRUCT_FGETP(generate_key_pair, kp_private_attributes);
53736424Skrishna 	if (!copyin_attributes(mode, sp, pri_count, pri_attributes,
53746424Skrishna 	    &k_pri_attrs, &k_pri_attrs_size, NULL, &rv, &error,
53756424Skrishna 	    &pri_rctl_bytes, &pri_rctl_chk, B_TRUE)) {
53760Sstevel@tonic-gate 		goto release_minor;
53770Sstevel@tonic-gate 	}
53780Sstevel@tonic-gate 
53790Sstevel@tonic-gate 	KCF_WRAP_KEY_OPS_PARAMS(&params, KCF_OP_KEY_GENERATE_PAIR,
53800Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, &mech, k_pub_attrs,
53810Sstevel@tonic-gate 	    pub_count, &pub_handle, k_pri_attrs, pri_count, &pri_handle,
53820Sstevel@tonic-gate 	    NULL, NULL, 0);
53830Sstevel@tonic-gate 
53840Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
53850Sstevel@tonic-gate 
53860Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
53870Sstevel@tonic-gate 		STRUCT_FSET(generate_key_pair, kp_public_handle, pub_handle);
53880Sstevel@tonic-gate 		STRUCT_FSET(generate_key_pair, kp_private_handle, pri_handle);
53890Sstevel@tonic-gate 	}
53900Sstevel@tonic-gate 
53910Sstevel@tonic-gate release_minor:
53926424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
53936424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, pub_rctl_bytes, pub_rctl_chk);
53946424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, pri_rctl_bytes, pri_rctl_chk);
53950Sstevel@tonic-gate 
53960Sstevel@tonic-gate 	if (k_pub_attrs != NULL)
53970Sstevel@tonic-gate 		kmem_free(k_pub_attrs, k_pub_attrs_size);
53980Sstevel@tonic-gate 
53990Sstevel@tonic-gate 	if (k_pri_attrs != NULL)
54000Sstevel@tonic-gate 		kmem_free(k_pri_attrs, k_pri_attrs_size);
54010Sstevel@tonic-gate 
54020Sstevel@tonic-gate 	if (error != 0)
54030Sstevel@tonic-gate 		goto out;
54040Sstevel@tonic-gate 
54050Sstevel@tonic-gate 	STRUCT_FSET(generate_key_pair, kp_return_value, rv);
54060Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(generate_key_pair), arg,
54070Sstevel@tonic-gate 	    STRUCT_SIZE(generate_key_pair)) != 0) {
54080Sstevel@tonic-gate 		if (rv == CRYPTO_SUCCESS) {
54090Sstevel@tonic-gate 			KCF_WRAP_OBJECT_OPS_PARAMS(&params,
54100Sstevel@tonic-gate 			    KCF_OP_OBJECT_DESTROY,
54110Sstevel@tonic-gate 			    sp->sd_provider_session->ps_session, pub_handle,
54120Sstevel@tonic-gate 			    NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
54130Sstevel@tonic-gate 
54140Sstevel@tonic-gate 			(void) kcf_submit_request(real_provider, NULL,
54150Sstevel@tonic-gate 			    NULL, &params, B_FALSE);
54160Sstevel@tonic-gate 
54170Sstevel@tonic-gate 			KCF_WRAP_OBJECT_OPS_PARAMS(&params,
54180Sstevel@tonic-gate 			    KCF_OP_OBJECT_DESTROY,
54190Sstevel@tonic-gate 			    sp->sd_provider_session->ps_session, pri_handle,
54200Sstevel@tonic-gate 			    NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
54210Sstevel@tonic-gate 
54220Sstevel@tonic-gate 			(void) kcf_submit_request(real_provider, NULL,
54230Sstevel@tonic-gate 			    NULL, &params, B_FALSE);
54240Sstevel@tonic-gate 
54250Sstevel@tonic-gate 			error = EFAULT;
54260Sstevel@tonic-gate 		}
54270Sstevel@tonic-gate 	}
54280Sstevel@tonic-gate out:
54296424Skrishna 	CRYPTO_SESSION_RELE(sp);
54300Sstevel@tonic-gate 	crypto_release_minor(cm);
5431904Smcpowers 
5432904Smcpowers 	if (real_provider != NULL) {
5433904Smcpowers 		crypto_free_mech(real_provider,
5434904Smcpowers 		    allocated_by_crypto_module, &mech);
5435904Smcpowers 		KCF_PROV_REFRELE(real_provider);
5436904Smcpowers 	}
54370Sstevel@tonic-gate 	return (error);
54380Sstevel@tonic-gate }
54390Sstevel@tonic-gate 
54400Sstevel@tonic-gate /* ARGSUSED */
54410Sstevel@tonic-gate static int
54424219Smcpowers nostore_generate_key_pair(dev_t dev, caddr_t arg, int mode, int *rval)
54434219Smcpowers {
54444219Smcpowers 	STRUCT_DECL(crypto_nostore_generate_key_pair, generate_key_pair);
54454219Smcpowers 	/* LINTED E_FUNC_SET_NOT_USED */
54464219Smcpowers 	STRUCT_DECL(crypto_object_attribute, oa);
54474219Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
54484219Smcpowers 	kcf_req_params_t params;
54494219Smcpowers 	crypto_mechanism_t mech;
54504219Smcpowers 	crypto_object_attribute_t *k_in_pub_attrs = NULL;
54514219Smcpowers 	crypto_object_attribute_t *k_in_pri_attrs = NULL;
54524219Smcpowers 	crypto_object_attribute_t *k_out_pub_attrs = NULL;
54534219Smcpowers 	crypto_object_attribute_t *k_out_pri_attrs = NULL;
54544219Smcpowers 	crypto_session_id_t session_id;
54554219Smcpowers 	crypto_minor_t *cm;
54564219Smcpowers 	crypto_session_data_t *sp = NULL;
54574219Smcpowers 	caddr_t in_pri_attributes;
54584219Smcpowers 	caddr_t in_pub_attributes;
54594219Smcpowers 	caddr_t out_pri_attributes;
54604219Smcpowers 	caddr_t out_pub_attributes;
54614219Smcpowers 	size_t k_in_pub_attrs_size, k_in_pri_attrs_size;
54624219Smcpowers 	size_t k_out_pub_attrs_size, k_out_pri_attrs_size;
54634219Smcpowers 	size_t mech_rctl_bytes = 0;
54646424Skrishna 	boolean_t mech_rctl_chk = B_FALSE;
54654219Smcpowers 	size_t in_pub_rctl_bytes = 0;
54666424Skrishna 	boolean_t in_pub_rctl_chk = B_FALSE;
54674219Smcpowers 	size_t in_pri_rctl_bytes = 0;
54686424Skrishna 	boolean_t in_pri_rctl_chk = B_FALSE;
54694219Smcpowers 	size_t out_pub_rctl_bytes = 0;
54706424Skrishna 	boolean_t out_pub_rctl_chk = B_FALSE;
54714219Smcpowers 	size_t out_pri_rctl_bytes = 0;
54726424Skrishna 	boolean_t out_pri_rctl_chk = B_FALSE;
54734219Smcpowers 	uint_t in_pub_count;
54744219Smcpowers 	uint_t in_pri_count;
54754219Smcpowers 	uint_t out_pub_count;
54764219Smcpowers 	uint_t out_pri_count;
54774219Smcpowers 	int error = 0;
54784219Smcpowers 	int rv;
54794219Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
54804219Smcpowers 	caddr_t u_pub_attrs = NULL;
54814219Smcpowers 	caddr_t u_pri_attrs = NULL;
54824219Smcpowers 
54834219Smcpowers 	STRUCT_INIT(generate_key_pair, mode);
54844219Smcpowers 	STRUCT_INIT(oa, mode);
54854219Smcpowers 
54864219Smcpowers 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
54874219Smcpowers 		cmn_err(CE_WARN,
54884219Smcpowers 		    "nostore_generate_key_pair: failed holding minor");
54894219Smcpowers 		return (ENXIO);
54904219Smcpowers 	}
54914219Smcpowers 
54924219Smcpowers 	if (copyin(arg, STRUCT_BUF(generate_key_pair),
54934219Smcpowers 	    STRUCT_SIZE(generate_key_pair)) != 0) {
54944219Smcpowers 		crypto_release_minor(cm);
54954219Smcpowers 		return (EFAULT);
54964219Smcpowers 	}
54974219Smcpowers 
54984219Smcpowers 	session_id = STRUCT_FGET(generate_key_pair, nkp_session);
54994219Smcpowers 
55004219Smcpowers 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
55014219Smcpowers 		goto release_minor;
55024219Smcpowers 	}
55034219Smcpowers 
55044219Smcpowers 	bcopy(STRUCT_FADDR(generate_key_pair, nkp_mechanism), &mech.cm_type,
55054219Smcpowers 	    sizeof (crypto_mech_type_t));
55064219Smcpowers 
55074219Smcpowers 	if ((rv = kcf_get_hardware_provider(mech.cm_type, CRYPTO_MECH_INVALID,
55084219Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider,
55094219Smcpowers 	    CRYPTO_FG_GENERATE_KEY_PAIR)) != CRYPTO_SUCCESS) {
55104219Smcpowers 		goto release_minor;
55114219Smcpowers 	}
55124219Smcpowers 
55134219Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
55144219Smcpowers 	    STRUCT_FADDR(generate_key_pair, nkp_mechanism), &mech, mode,
55154219Smcpowers 	    &error);
55164219Smcpowers 
55174219Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
55184219Smcpowers 		allocated_by_crypto_module = B_TRUE;
55196424Skrishna 		if (!copyin_mech(mode, sp, STRUCT_FADDR(generate_key_pair,
55206424Skrishna 		    nkp_mechanism), &mech, &mech_rctl_bytes,
55216424Skrishna 		    &mech_rctl_chk, &rv, &error)) {
55224219Smcpowers 			goto release_minor;
55234219Smcpowers 		}
55244219Smcpowers 	} else {
55254219Smcpowers 		if (rv != CRYPTO_SUCCESS)
55264219Smcpowers 			goto release_minor;
55274219Smcpowers 	}
55284219Smcpowers 
55294219Smcpowers 	in_pub_count = STRUCT_FGET(generate_key_pair, nkp_in_public_count);
55304219Smcpowers 	in_pri_count = STRUCT_FGET(generate_key_pair, nkp_in_private_count);
55314219Smcpowers 
55324219Smcpowers 	in_pub_attributes = STRUCT_FGETP(generate_key_pair,
55334219Smcpowers 	    nkp_in_public_attributes);
55346424Skrishna 	if (!copyin_attributes(mode, sp, in_pub_count, in_pub_attributes,
55354219Smcpowers 	    &k_in_pub_attrs, &k_in_pub_attrs_size, NULL, &rv, &error,
55366424Skrishna 	    &in_pub_rctl_bytes, &in_pub_rctl_chk, B_TRUE)) {
55374219Smcpowers 		goto release_minor;
55384219Smcpowers 	}
55394219Smcpowers 
55404219Smcpowers 	in_pri_attributes = STRUCT_FGETP(generate_key_pair,
55414219Smcpowers 	    nkp_in_private_attributes);
55426424Skrishna 	if (!copyin_attributes(mode, sp, in_pri_count, in_pri_attributes,
55434219Smcpowers 	    &k_in_pri_attrs, &k_in_pri_attrs_size, NULL, &rv, &error,
55446424Skrishna 	    &in_pri_rctl_bytes, &in_pri_rctl_chk, B_TRUE)) {
55454219Smcpowers 		goto release_minor;
55464219Smcpowers 	}
55474219Smcpowers 
55484219Smcpowers 	out_pub_count = STRUCT_FGET(generate_key_pair, nkp_out_public_count);
55494219Smcpowers 	out_pri_count = STRUCT_FGET(generate_key_pair, nkp_out_private_count);
55504219Smcpowers 
55514219Smcpowers 	out_pub_attributes = STRUCT_FGETP(generate_key_pair,
55524219Smcpowers 	    nkp_out_public_attributes);
55536424Skrishna 	if (!copyin_attributes(mode, sp, out_pub_count, out_pub_attributes,
55544219Smcpowers 	    &k_out_pub_attrs, &k_out_pub_attrs_size, &u_pub_attrs, &rv, &error,
55556424Skrishna 	    &out_pub_rctl_bytes, &out_pub_rctl_chk, B_FALSE)) {
55564219Smcpowers 		goto release_minor;
55574219Smcpowers 	}
55584219Smcpowers 
55594219Smcpowers 	out_pri_attributes = STRUCT_FGETP(generate_key_pair,
55604219Smcpowers 	    nkp_out_private_attributes);
55616424Skrishna 	if (!copyin_attributes(mode, sp, out_pri_count, out_pri_attributes,
55624219Smcpowers 	    &k_out_pri_attrs, &k_out_pri_attrs_size, &u_pri_attrs, &rv, &error,
55636424Skrishna 	    &out_pri_rctl_bytes, &out_pri_rctl_chk, B_FALSE)) {
55644219Smcpowers 		goto release_minor;
55654219Smcpowers 	}
55664219Smcpowers 
55674219Smcpowers 	KCF_WRAP_NOSTORE_KEY_OPS_PARAMS(&params, KCF_OP_KEY_GENERATE_PAIR,
55684219Smcpowers 	    sp->sd_provider_session->ps_session, &mech, k_in_pub_attrs,
55694219Smcpowers 	    in_pub_count, k_in_pri_attrs, in_pri_count, NULL, k_out_pub_attrs,
55704219Smcpowers 	    out_pub_count, k_out_pri_attrs, out_pri_count);
55714219Smcpowers 
55724219Smcpowers 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
55734219Smcpowers 
55744219Smcpowers 	if (rv == CRYPTO_SUCCESS) {
55754219Smcpowers 		error = copyout_attributes(mode, out_pub_attributes,
55764219Smcpowers 		    out_pub_count, k_out_pub_attrs, u_pub_attrs);
55774219Smcpowers 		if (error != CRYPTO_SUCCESS)
55784219Smcpowers 			goto release_minor;
55794219Smcpowers 		error = copyout_attributes(mode, out_pri_attributes,
55804219Smcpowers 		    out_pri_count, k_out_pri_attrs, u_pri_attrs);
55814219Smcpowers 	}
55824219Smcpowers 
55834219Smcpowers release_minor:
55846424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
55856424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, in_pub_rctl_bytes, in_pub_rctl_chk);
55866424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, in_pri_rctl_bytes, in_pri_rctl_chk);
55876424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, out_pub_rctl_bytes,
55886424Skrishna 	    out_pub_rctl_chk);
55896424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, out_pri_rctl_bytes,
55906424Skrishna 	    out_pri_rctl_chk);
55914219Smcpowers 
55924219Smcpowers 	if (k_in_pub_attrs != NULL)
55934219Smcpowers 		kmem_free(k_in_pub_attrs, k_in_pub_attrs_size);
55944219Smcpowers 
55954219Smcpowers 	if (k_in_pri_attrs != NULL)
55964219Smcpowers 		kmem_free(k_in_pri_attrs, k_in_pri_attrs_size);
55974219Smcpowers 
55984219Smcpowers 	if (k_out_pub_attrs != NULL)
55994219Smcpowers 		kmem_free(k_out_pub_attrs, k_out_pub_attrs_size);
56004219Smcpowers 
56014219Smcpowers 	if (k_out_pri_attrs != NULL) {
56024219Smcpowers 		bzero(k_out_pri_attrs, k_out_pri_attrs_size);
56034219Smcpowers 		kmem_free(k_out_pri_attrs, k_out_pri_attrs_size);
56044219Smcpowers 	}
56054219Smcpowers 
56064219Smcpowers 	if (u_pub_attrs != NULL)
56074219Smcpowers 		kmem_free(u_pub_attrs, out_pub_count * STRUCT_SIZE(oa));
56084219Smcpowers 
56094219Smcpowers 	if (u_pri_attrs != NULL)
56104219Smcpowers 		kmem_free(u_pri_attrs, out_pri_count * STRUCT_SIZE(oa));
56114219Smcpowers 
56124219Smcpowers 	if (error != 0)
56134219Smcpowers 		goto out;
56144219Smcpowers 
56154219Smcpowers 	STRUCT_FSET(generate_key_pair, nkp_return_value, rv);
56164219Smcpowers 	if (copyout(STRUCT_BUF(generate_key_pair), arg,
56174219Smcpowers 	    STRUCT_SIZE(generate_key_pair)) != 0) {
56184219Smcpowers 		error = EFAULT;
56194219Smcpowers 	}
56204219Smcpowers out:
56216424Skrishna 	CRYPTO_SESSION_RELE(sp);
56224219Smcpowers 	crypto_release_minor(cm);
56234219Smcpowers 
56244219Smcpowers 	if (real_provider != NULL) {
56254219Smcpowers 		crypto_free_mech(real_provider,
56264219Smcpowers 		    allocated_by_crypto_module, &mech);
56274219Smcpowers 		KCF_PROV_REFRELE(real_provider);
56284219Smcpowers 	}
56294219Smcpowers 	return (error);
56304219Smcpowers }
56314219Smcpowers 
56324219Smcpowers /* ARGSUSED */
56334219Smcpowers static int
56340Sstevel@tonic-gate object_wrap_key(dev_t dev, caddr_t arg, int mode, int *rval)
56350Sstevel@tonic-gate {
56360Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_wrap_key, wrap_key);
5637904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
56380Sstevel@tonic-gate 	kcf_req_params_t params;
56390Sstevel@tonic-gate 	crypto_mechanism_t mech;
56400Sstevel@tonic-gate 	crypto_key_t key;
56410Sstevel@tonic-gate 	crypto_session_id_t session_id;
56420Sstevel@tonic-gate 	crypto_minor_t *cm;
56436424Skrishna 	crypto_session_data_t *sp = NULL;
56440Sstevel@tonic-gate 	crypto_object_id_t handle;
56450Sstevel@tonic-gate 	size_t mech_rctl_bytes = 0, key_rctl_bytes = 0;
56466424Skrishna 	boolean_t mech_rctl_chk = B_FALSE;
56476424Skrishna 	boolean_t key_rctl_chk = B_FALSE;
56480Sstevel@tonic-gate 	size_t wrapped_key_rctl_bytes = 0;
56496424Skrishna 	boolean_t wrapped_key_rctl_chk = B_FALSE;
56500Sstevel@tonic-gate 	size_t wrapped_key_len, new_wrapped_key_len;
56510Sstevel@tonic-gate 	uchar_t *wrapped_key = NULL;
56520Sstevel@tonic-gate 	char *wrapped_key_buffer;
56530Sstevel@tonic-gate 	int error = 0;
56540Sstevel@tonic-gate 	int rv;
5655904Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
56560Sstevel@tonic-gate 
56570Sstevel@tonic-gate 	STRUCT_INIT(wrap_key, mode);
56580Sstevel@tonic-gate 
56590Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
56600Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_wrap_key: failed holding minor");
56610Sstevel@tonic-gate 		return (ENXIO);
56620Sstevel@tonic-gate 	}
56630Sstevel@tonic-gate 
56640Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(wrap_key), STRUCT_SIZE(wrap_key)) != 0) {
56650Sstevel@tonic-gate 		crypto_release_minor(cm);
56660Sstevel@tonic-gate 		return (EFAULT);
56670Sstevel@tonic-gate 	}
56680Sstevel@tonic-gate 
56690Sstevel@tonic-gate 	bzero(&key, sizeof (crypto_key_t));
56700Sstevel@tonic-gate 
56710Sstevel@tonic-gate 	session_id = STRUCT_FGET(wrap_key, wk_session);
56720Sstevel@tonic-gate 
56730Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
56746424Skrishna 		goto out;
56750Sstevel@tonic-gate 	}
56760Sstevel@tonic-gate 
5677904Smcpowers 	bcopy(STRUCT_FADDR(wrap_key, wk_mechanism), &mech.cm_type,
5678904Smcpowers 	    sizeof (crypto_mech_type_t));
5679904Smcpowers 
5680904Smcpowers 	if ((rv = kcf_get_hardware_provider(mech.cm_type, CRYPTO_MECH_INVALID,
56811808Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider,
56821808Smcpowers 	    CRYPTO_FG_WRAP)) != CRYPTO_SUCCESS) {
56830Sstevel@tonic-gate 		goto out;
56840Sstevel@tonic-gate 	}
56850Sstevel@tonic-gate 
5686904Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
5687904Smcpowers 	    STRUCT_FADDR(wrap_key, wk_mechanism), &mech, mode, &error);
5688904Smcpowers 
5689904Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
5690904Smcpowers 		allocated_by_crypto_module = B_TRUE;
56916424Skrishna 		if (!copyin_mech(mode, sp, STRUCT_FADDR(wrap_key, wk_mechanism),
56926424Skrishna 		    &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) {
5693904Smcpowers 			goto out;
5694904Smcpowers 		}
5695904Smcpowers 	} else {
5696904Smcpowers 		if (rv != CRYPTO_SUCCESS)
5697904Smcpowers 			goto out;
56980Sstevel@tonic-gate 	}
56990Sstevel@tonic-gate 
57006424Skrishna 	if (!copyin_key(mode, sp, STRUCT_FADDR(wrap_key, wk_wrapping_key), &key,
57016424Skrishna 	    &key_rctl_bytes, &key_rctl_chk, &rv, &error)) {
57020Sstevel@tonic-gate 		goto out;
57030Sstevel@tonic-gate 	}
57040Sstevel@tonic-gate 
57050Sstevel@tonic-gate 	wrapped_key_len = STRUCT_FGET(wrap_key, wk_wrapped_key_len);
57060Sstevel@tonic-gate 
57070Sstevel@tonic-gate 	/*
57080Sstevel@tonic-gate 	 * Don't allocate output buffer unless both buffer pointer and
57090Sstevel@tonic-gate 	 * buffer length are not NULL or 0 (length).
57100Sstevel@tonic-gate 	 */
57110Sstevel@tonic-gate 	wrapped_key_buffer = STRUCT_FGETP(wrap_key, wk_wrapped_key);
57120Sstevel@tonic-gate 	if (wrapped_key_buffer == NULL || wrapped_key_len == 0) {
57130Sstevel@tonic-gate 		wrapped_key_len = 0;
57140Sstevel@tonic-gate 	}
57150Sstevel@tonic-gate 
57160Sstevel@tonic-gate 	if (wrapped_key_len > crypto_max_buffer_len) {
57170Sstevel@tonic-gate 		cmn_err(CE_NOTE, "object_wrap_key: buffer greater than %ld "
57180Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
57190Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
57200Sstevel@tonic-gate 		goto out;
57210Sstevel@tonic-gate 	}
57220Sstevel@tonic-gate 
57236424Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, wrapped_key_len,
57246424Skrishna 	    wrapped_key_rctl_chk)) != CRYPTO_SUCCESS) {
57250Sstevel@tonic-gate 		goto out;
57260Sstevel@tonic-gate 	}
57270Sstevel@tonic-gate 
57280Sstevel@tonic-gate 	/* new_wrapped_key_len can be modified by the provider */
57290Sstevel@tonic-gate 	wrapped_key_rctl_bytes = new_wrapped_key_len = wrapped_key_len;
57300Sstevel@tonic-gate 	wrapped_key = kmem_alloc(wrapped_key_len, KM_SLEEP);
57310Sstevel@tonic-gate 
57320Sstevel@tonic-gate 	handle = STRUCT_FGET(wrap_key, wk_object_handle);
57330Sstevel@tonic-gate 	KCF_WRAP_KEY_OPS_PARAMS(&params, KCF_OP_KEY_WRAP,
57340Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, &mech, NULL, 0, &handle,
57350Sstevel@tonic-gate 	    NULL, 0, NULL, &key, wrapped_key, &new_wrapped_key_len);
57360Sstevel@tonic-gate 
57370Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
57380Sstevel@tonic-gate 
57390Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
57400Sstevel@tonic-gate 		if (wrapped_key_len != 0 && copyout(wrapped_key,
57410Sstevel@tonic-gate 		    wrapped_key_buffer, new_wrapped_key_len) != 0) {
57420Sstevel@tonic-gate 			error = EFAULT;
57430Sstevel@tonic-gate 		}
57440Sstevel@tonic-gate 		STRUCT_FSET(wrap_key, wk_wrapped_key_len, new_wrapped_key_len);
57450Sstevel@tonic-gate 	}
57460Sstevel@tonic-gate 
57470Sstevel@tonic-gate 	if (rv == CRYPTO_BUFFER_TOO_SMALL) {
57480Sstevel@tonic-gate 		/*
57490Sstevel@tonic-gate 		 * The providers return CRYPTO_BUFFER_TOO_SMALL even for case 1
57500Sstevel@tonic-gate 		 * of section 11.2 of the pkcs11 spec. We catch it here and
57510Sstevel@tonic-gate 		 * provide the correct pkcs11 return value.
57520Sstevel@tonic-gate 		 */
57530Sstevel@tonic-gate 		if (STRUCT_FGETP(wrap_key, wk_wrapped_key) == NULL)
57540Sstevel@tonic-gate 			rv = CRYPTO_SUCCESS;
57550Sstevel@tonic-gate 		STRUCT_FSET(wrap_key, wk_wrapped_key_len, new_wrapped_key_len);
57560Sstevel@tonic-gate 	}
57576424Skrishna 
57580Sstevel@tonic-gate out:
57596424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
57606424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk);
57616424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, wrapped_key_rctl_bytes,
57626424Skrishna 	    wrapped_key_rctl_chk);
57630Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
57640Sstevel@tonic-gate 
57650Sstevel@tonic-gate 	crypto_release_minor(cm);
57660Sstevel@tonic-gate 
5767904Smcpowers 	if (real_provider != NULL) {
5768904Smcpowers 		crypto_free_mech(real_provider,
5769904Smcpowers 		    allocated_by_crypto_module, &mech);
5770904Smcpowers 		KCF_PROV_REFRELE(real_provider);
5771904Smcpowers 	}
5772904Smcpowers 
57730Sstevel@tonic-gate 	if (wrapped_key != NULL)
57740Sstevel@tonic-gate 		kmem_free(wrapped_key, wrapped_key_len);
57750Sstevel@tonic-gate 
57760Sstevel@tonic-gate 	free_crypto_key(&key);
57770Sstevel@tonic-gate 
57780Sstevel@tonic-gate 	if (error != 0)
57790Sstevel@tonic-gate 		return (error);
57800Sstevel@tonic-gate 
57810Sstevel@tonic-gate 	STRUCT_FSET(wrap_key, wk_return_value, rv);
57820Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(wrap_key), arg, STRUCT_SIZE(wrap_key)) != 0) {
57830Sstevel@tonic-gate 		return (EFAULT);
57840Sstevel@tonic-gate 	}
57850Sstevel@tonic-gate 	return (0);
57860Sstevel@tonic-gate }
57870Sstevel@tonic-gate 
57880Sstevel@tonic-gate /* ARGSUSED */
57890Sstevel@tonic-gate static int
57900Sstevel@tonic-gate object_unwrap_key(dev_t dev, caddr_t arg, int mode, int *rval)
57910Sstevel@tonic-gate {
57920Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_unwrap_key, unwrap_key);
5793904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
57940Sstevel@tonic-gate 	kcf_req_params_t params;
57950Sstevel@tonic-gate 	crypto_mechanism_t mech;
57960Sstevel@tonic-gate 	crypto_key_t unwrapping_key;
57970Sstevel@tonic-gate 	crypto_session_id_t session_id;
57980Sstevel@tonic-gate 	crypto_minor_t *cm;
57990Sstevel@tonic-gate 	crypto_session_data_t *sp = NULL;
58000Sstevel@tonic-gate 	crypto_object_id_t handle;
58010Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
58020Sstevel@tonic-gate 	size_t k_attrs_size;
58030Sstevel@tonic-gate 	size_t mech_rctl_bytes = 0, unwrapping_key_rctl_bytes = 0;
58046424Skrishna 	boolean_t mech_rctl_chk = B_FALSE;
58056424Skrishna 	boolean_t unwrapping_key_rctl_chk = B_FALSE;
58060Sstevel@tonic-gate 	size_t wrapped_key_rctl_bytes = 0, k_attrs_rctl_bytes = 0;
58076424Skrishna 	boolean_t wrapped_key_rctl_chk = B_FALSE;
58086424Skrishna 	boolean_t k_attrs_rctl_chk = B_FALSE;
58090Sstevel@tonic-gate 	size_t wrapped_key_len;
58100Sstevel@tonic-gate 	uchar_t *wrapped_key = NULL;
58110Sstevel@tonic-gate 	int error = 0;
58120Sstevel@tonic-gate 	int rv;
58130Sstevel@tonic-gate 	uint_t count;
58140Sstevel@tonic-gate 	caddr_t uk_attributes;
5815904Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
58160Sstevel@tonic-gate 
58170Sstevel@tonic-gate 	STRUCT_INIT(unwrap_key, mode);
58180Sstevel@tonic-gate 
58190Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
58200Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_unwrap_key: failed holding minor");
58210Sstevel@tonic-gate 		return (ENXIO);
58220Sstevel@tonic-gate 	}
58230Sstevel@tonic-gate 
58240Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(unwrap_key), STRUCT_SIZE(unwrap_key)) != 0) {
58250Sstevel@tonic-gate 		crypto_release_minor(cm);
58260Sstevel@tonic-gate 		return (EFAULT);
58270Sstevel@tonic-gate 	}
58280Sstevel@tonic-gate 
58290Sstevel@tonic-gate 	bzero(&unwrapping_key, sizeof (unwrapping_key));
58300Sstevel@tonic-gate 
58310Sstevel@tonic-gate 	session_id = STRUCT_FGET(unwrap_key, uk_session);
58320Sstevel@tonic-gate 
58330Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
58340Sstevel@tonic-gate 		goto release_minor;
58350Sstevel@tonic-gate 	}
58360Sstevel@tonic-gate 
5837904Smcpowers 	bcopy(STRUCT_FADDR(unwrap_key, uk_mechanism), &mech.cm_type,
5838904Smcpowers 	    sizeof (crypto_mech_type_t));
5839904Smcpowers 
5840904Smcpowers 	if ((rv = kcf_get_hardware_provider(mech.cm_type, CRYPTO_MECH_INVALID,
58411808Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider,
58421808Smcpowers 	    CRYPTO_FG_UNWRAP)) != CRYPTO_SUCCESS) {
58430Sstevel@tonic-gate 		goto release_minor;
58440Sstevel@tonic-gate 	}
58450Sstevel@tonic-gate 
5846904Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
5847904Smcpowers 	    STRUCT_FADDR(unwrap_key, uk_mechanism), &mech, mode, &error);
5848904Smcpowers 
5849904Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
5850904Smcpowers 		allocated_by_crypto_module = B_TRUE;
58516424Skrishna 		if (!copyin_mech(mode, sp,
58526424Skrishna 		    STRUCT_FADDR(unwrap_key, uk_mechanism),
58536424Skrishna 		    &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) {
5854904Smcpowers 			goto release_minor;
5855904Smcpowers 		}
5856904Smcpowers 	} else {
5857904Smcpowers 		if (rv != CRYPTO_SUCCESS)
5858904Smcpowers 			goto release_minor;
58590Sstevel@tonic-gate 	}
58600Sstevel@tonic-gate 
58616424Skrishna 	if (!copyin_key(mode, sp, STRUCT_FADDR(unwrap_key, uk_unwrapping_key),
58626424Skrishna 	    &unwrapping_key, &unwrapping_key_rctl_bytes,
58636424Skrishna 	    &unwrapping_key_rctl_chk, &rv, &error)) {
58640Sstevel@tonic-gate 		goto release_minor;
58650Sstevel@tonic-gate 	}
58660Sstevel@tonic-gate 
58670Sstevel@tonic-gate 	count = STRUCT_FGET(unwrap_key, uk_count);
58680Sstevel@tonic-gate 	uk_attributes = STRUCT_FGETP(unwrap_key, uk_attributes);
58696424Skrishna 	if (!copyin_attributes(mode, sp, count, uk_attributes, &k_attrs,
58706424Skrishna 	    &k_attrs_size, NULL, &rv, &error, &k_attrs_rctl_bytes,
58716424Skrishna 	    &k_attrs_rctl_chk, B_TRUE)) {
58720Sstevel@tonic-gate 		goto release_minor;
58730Sstevel@tonic-gate 	}
58740Sstevel@tonic-gate 
58750Sstevel@tonic-gate 	wrapped_key_len = STRUCT_FGET(unwrap_key, uk_wrapped_key_len);
58760Sstevel@tonic-gate 	if (wrapped_key_len > crypto_max_buffer_len) {
58770Sstevel@tonic-gate 		cmn_err(CE_NOTE, "object_unwrap_key: buffer greater than %ld "
58780Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
58790Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
58800Sstevel@tonic-gate 		goto release_minor;
58810Sstevel@tonic-gate 	}
58820Sstevel@tonic-gate 
58836424Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, wrapped_key_len,
58846424Skrishna 	    wrapped_key_rctl_chk)) != CRYPTO_SUCCESS) {
58850Sstevel@tonic-gate 		goto release_minor;
58860Sstevel@tonic-gate 	}
58870Sstevel@tonic-gate 	wrapped_key_rctl_bytes = wrapped_key_len;
58880Sstevel@tonic-gate 	wrapped_key = kmem_alloc(wrapped_key_len, KM_SLEEP);
58890Sstevel@tonic-gate 
58900Sstevel@tonic-gate 	if (wrapped_key_len != 0 && copyin(STRUCT_FGETP(unwrap_key,
58910Sstevel@tonic-gate 	    uk_wrapped_key), wrapped_key, wrapped_key_len) != 0) {
58920Sstevel@tonic-gate 		error = EFAULT;
58930Sstevel@tonic-gate 		goto release_minor;
58940Sstevel@tonic-gate 	}
58950Sstevel@tonic-gate 
58960Sstevel@tonic-gate 	/* wrapped_key_len is not modified by the unwrap operation */
58970Sstevel@tonic-gate 	KCF_WRAP_KEY_OPS_PARAMS(&params, KCF_OP_KEY_UNWRAP,
58980Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, &mech, k_attrs, count, &handle,
58990Sstevel@tonic-gate 	    NULL, 0, NULL, &unwrapping_key, wrapped_key, &wrapped_key_len);
59000Sstevel@tonic-gate 
59010Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
59020Sstevel@tonic-gate 
59030Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS)
59040Sstevel@tonic-gate 		STRUCT_FSET(unwrap_key, uk_object_handle, handle);
59050Sstevel@tonic-gate 
59060Sstevel@tonic-gate release_minor:
59076424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
59086424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, unwrapping_key_rctl_bytes,
59096424Skrishna 	    unwrapping_key_rctl_chk);
59106424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, wrapped_key_rctl_bytes,
59116424Skrishna 	    wrapped_key_rctl_chk);
59126424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, k_attrs_rctl_bytes,
59136424Skrishna 	    k_attrs_rctl_chk);
59140Sstevel@tonic-gate 
59150Sstevel@tonic-gate 	if (k_attrs != NULL)
59160Sstevel@tonic-gate 		kmem_free(k_attrs, k_attrs_size);
59170Sstevel@tonic-gate 
59180Sstevel@tonic-gate 	if (wrapped_key != NULL)
59190Sstevel@tonic-gate 		kmem_free(wrapped_key, wrapped_key_len);
59200Sstevel@tonic-gate 
59210Sstevel@tonic-gate 	free_crypto_key(&unwrapping_key);
59220Sstevel@tonic-gate 
59230Sstevel@tonic-gate 	if (error != 0)
59240Sstevel@tonic-gate 		goto out;
59250Sstevel@tonic-gate 
59260Sstevel@tonic-gate 	STRUCT_FSET(unwrap_key, uk_return_value, rv);
59270Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(unwrap_key), arg,
59280Sstevel@tonic-gate 	    STRUCT_SIZE(unwrap_key)) != 0) {
59290Sstevel@tonic-gate 		if (rv == CRYPTO_SUCCESS) {
59300Sstevel@tonic-gate 			KCF_WRAP_OBJECT_OPS_PARAMS(&params,
59310Sstevel@tonic-gate 			    KCF_OP_OBJECT_DESTROY,
59320Sstevel@tonic-gate 			    sp->sd_provider_session->ps_session, handle,
59330Sstevel@tonic-gate 			    NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
59340Sstevel@tonic-gate 
59350Sstevel@tonic-gate 			(void) kcf_submit_request(real_provider, NULL,
59360Sstevel@tonic-gate 			    NULL, &params, B_FALSE);
59370Sstevel@tonic-gate 
59380Sstevel@tonic-gate 			error = EFAULT;
59390Sstevel@tonic-gate 		}
59400Sstevel@tonic-gate 	}
59410Sstevel@tonic-gate out:
59426424Skrishna 	CRYPTO_SESSION_RELE(sp);
59430Sstevel@tonic-gate 	crypto_release_minor(cm);
5944904Smcpowers 
5945904Smcpowers 	if (real_provider != NULL) {
5946904Smcpowers 		crypto_free_mech(real_provider,
5947904Smcpowers 		    allocated_by_crypto_module, &mech);
5948904Smcpowers 		KCF_PROV_REFRELE(real_provider);
5949904Smcpowers 	}
5950904Smcpowers 
59510Sstevel@tonic-gate 	return (error);
59520Sstevel@tonic-gate }
59530Sstevel@tonic-gate 
59540Sstevel@tonic-gate /* ARGSUSED */
59550Sstevel@tonic-gate static int
59560Sstevel@tonic-gate object_derive_key(dev_t dev, caddr_t arg, int mode, int *rval)
59570Sstevel@tonic-gate {
59580Sstevel@tonic-gate 	STRUCT_DECL(crypto_derive_key, derive_key);
5959904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
59600Sstevel@tonic-gate 	kcf_req_params_t params;
59610Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
59620Sstevel@tonic-gate 	crypto_mechanism_t mech;
59630Sstevel@tonic-gate 	crypto_key_t base_key;
59640Sstevel@tonic-gate 	crypto_session_id_t session_id;
59650Sstevel@tonic-gate 	crypto_minor_t *cm;
59660Sstevel@tonic-gate 	crypto_session_data_t *sp = NULL;
59670Sstevel@tonic-gate 	crypto_object_id_t handle;
59680Sstevel@tonic-gate 	size_t k_attrs_size;
59690Sstevel@tonic-gate 	size_t key_rctl_bytes = 0, mech_rctl_bytes = 0;
59706424Skrishna 	boolean_t mech_rctl_chk = B_FALSE;
59716424Skrishna 	boolean_t key_rctl_chk = B_FALSE;
59720Sstevel@tonic-gate 	size_t attributes_rctl_bytes = 0;
59736424Skrishna 	boolean_t attributes_rctl_chk = B_FALSE;
59740Sstevel@tonic-gate 	caddr_t attributes;
59750Sstevel@tonic-gate 	uint_t count;
59760Sstevel@tonic-gate 	int error = 0;
59770Sstevel@tonic-gate 	int rv;
5978904Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
5979904Smcpowers 	boolean_t please_destroy_object = B_FALSE;
59800Sstevel@tonic-gate 
59810Sstevel@tonic-gate 	STRUCT_INIT(derive_key, mode);
59820Sstevel@tonic-gate 
59830Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
59840Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_derive_key: failed holding minor");
59850Sstevel@tonic-gate 		return (ENXIO);
59860Sstevel@tonic-gate 	}
59870Sstevel@tonic-gate 
59880Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(derive_key), STRUCT_SIZE(derive_key)) != 0) {
59890Sstevel@tonic-gate 		crypto_release_minor(cm);
59900Sstevel@tonic-gate 		return (EFAULT);
59910Sstevel@tonic-gate 	}
59920Sstevel@tonic-gate 
59930Sstevel@tonic-gate 	bzero(&base_key, sizeof (base_key));
59940Sstevel@tonic-gate 
59950Sstevel@tonic-gate 	session_id = STRUCT_FGET(derive_key, dk_session);
59960Sstevel@tonic-gate 
59970Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
59980Sstevel@tonic-gate 		goto release_minor;
59990Sstevel@tonic-gate 	}
60000Sstevel@tonic-gate 
6001904Smcpowers 	bcopy(STRUCT_FADDR(derive_key, dk_mechanism), &mech.cm_type,
6002904Smcpowers 	    sizeof (crypto_mech_type_t));
6003904Smcpowers 
6004904Smcpowers 	if ((rv = kcf_get_hardware_provider(mech.cm_type, CRYPTO_MECH_INVALID,
60051808Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider,
60061808Smcpowers 	    CRYPTO_FG_DERIVE)) != CRYPTO_SUCCESS) {
60070Sstevel@tonic-gate 		goto release_minor;
60080Sstevel@tonic-gate 	}
60090Sstevel@tonic-gate 
6010904Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
6011904Smcpowers 	    STRUCT_FADDR(derive_key, dk_mechanism), &mech, mode, &error);
6012904Smcpowers 
6013904Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
6014904Smcpowers 		allocated_by_crypto_module = B_TRUE;
60156424Skrishna 		if (!copyin_mech(mode, sp,
60166424Skrishna 		    STRUCT_FADDR(derive_key, dk_mechanism),
60176424Skrishna 		    &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) {
6018904Smcpowers 			goto release_minor;
6019904Smcpowers 		}
6020904Smcpowers 	} else {
6021904Smcpowers 		if (rv != CRYPTO_SUCCESS)
6022904Smcpowers 			goto release_minor;
60230Sstevel@tonic-gate 	}
60240Sstevel@tonic-gate 
60256424Skrishna 	if (!copyin_key(mode, sp, STRUCT_FADDR(derive_key, dk_base_key),
60266424Skrishna 	    &base_key, &key_rctl_bytes, &key_rctl_chk, &rv, &error)) {
60270Sstevel@tonic-gate 		goto release_minor;
60280Sstevel@tonic-gate 	}
60290Sstevel@tonic-gate 
60300Sstevel@tonic-gate 	count = STRUCT_FGET(derive_key, dk_count);
60310Sstevel@tonic-gate 
60320Sstevel@tonic-gate 	attributes = STRUCT_FGETP(derive_key, dk_attributes);
60336424Skrishna 	if (!copyin_attributes(mode, sp, count, attributes, &k_attrs,
60343916Skrishna 	    &k_attrs_size, NULL, &rv, &error,
60356424Skrishna 	    &attributes_rctl_bytes, &attributes_rctl_chk, B_TRUE)) {
60360Sstevel@tonic-gate 		goto release_minor;
60370Sstevel@tonic-gate 	}
60380Sstevel@tonic-gate 
60390Sstevel@tonic-gate 	KCF_WRAP_KEY_OPS_PARAMS(&params, KCF_OP_KEY_DERIVE,
60400Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, &mech, k_attrs, count,
60410Sstevel@tonic-gate 	    &handle, NULL, 0, NULL, &base_key, NULL, NULL);
60420Sstevel@tonic-gate 
60430Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
60440Sstevel@tonic-gate 
6045904Smcpowers 	if (rv == CRYPTO_SUCCESS) {
60460Sstevel@tonic-gate 		STRUCT_FSET(derive_key, dk_object_handle, handle);
60470Sstevel@tonic-gate 
6048904Smcpowers 		rv = crypto_provider_copyout_mech_param(real_provider,
6049904Smcpowers 		    &mech, STRUCT_FADDR(derive_key, dk_mechanism),
6050904Smcpowers 		    mode, &error);
6051904Smcpowers 
6052904Smcpowers 		if (rv == CRYPTO_NOT_SUPPORTED) {
6053904Smcpowers 			rv = CRYPTO_SUCCESS;
6054904Smcpowers 			goto release_minor;
6055904Smcpowers 		}
6056904Smcpowers 
6057904Smcpowers 		if (rv != CRYPTO_SUCCESS)
6058904Smcpowers 			please_destroy_object = B_TRUE;
6059904Smcpowers 	}
6060904Smcpowers 
60610Sstevel@tonic-gate release_minor:
60626424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
60636424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk);
60646424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, attributes_rctl_bytes,
60656424Skrishna 	    attributes_rctl_chk);
60660Sstevel@tonic-gate 
60670Sstevel@tonic-gate 	if (k_attrs != NULL)
60680Sstevel@tonic-gate 		kmem_free(k_attrs, k_attrs_size);
60690Sstevel@tonic-gate 
60700Sstevel@tonic-gate 	free_crypto_key(&base_key);
60710Sstevel@tonic-gate 
60720Sstevel@tonic-gate 	if (error != 0)
60730Sstevel@tonic-gate 		goto out;
60740Sstevel@tonic-gate 
60750Sstevel@tonic-gate 	STRUCT_FSET(derive_key, dk_return_value, rv);
60760Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(derive_key), arg,
60770Sstevel@tonic-gate 	    STRUCT_SIZE(derive_key)) != 0) {
60780Sstevel@tonic-gate 		if (rv == CRYPTO_SUCCESS) {
6079904Smcpowers 			please_destroy_object = B_TRUE;
60800Sstevel@tonic-gate 			error = EFAULT;
60810Sstevel@tonic-gate 		}
60820Sstevel@tonic-gate 	}
60830Sstevel@tonic-gate out:
6084904Smcpowers 	if (please_destroy_object) {
6085904Smcpowers 		KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_DESTROY,
6086904Smcpowers 		    sp->sd_provider_session->ps_session, handle,
6087904Smcpowers 		    NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
6088904Smcpowers 
6089904Smcpowers 		(void) kcf_submit_request(real_provider, NULL,
6090904Smcpowers 		    NULL, &params, B_FALSE);
6091904Smcpowers 	}
6092904Smcpowers 
60936424Skrishna 	CRYPTO_SESSION_RELE(sp);
60940Sstevel@tonic-gate 	crypto_release_minor(cm);
6095904Smcpowers 
6096904Smcpowers 	if (real_provider != NULL) {
6097904Smcpowers 		crypto_free_mech(real_provider,
6098904Smcpowers 		    allocated_by_crypto_module, &mech);
6099904Smcpowers 		KCF_PROV_REFRELE(real_provider);
6100904Smcpowers 	}
61010Sstevel@tonic-gate 	return (error);
61020Sstevel@tonic-gate }
61030Sstevel@tonic-gate 
61040Sstevel@tonic-gate /* ARGSUSED */
61050Sstevel@tonic-gate static int
61064219Smcpowers nostore_derive_key(dev_t dev, caddr_t arg, int mode, int *rval)
61074219Smcpowers {
61084219Smcpowers 	STRUCT_DECL(crypto_nostore_derive_key, derive_key);
61094219Smcpowers 	/* LINTED E_FUNC_SET_NOT_USED */
61104219Smcpowers 	STRUCT_DECL(crypto_object_attribute, oa);
61114219Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
61124219Smcpowers 	kcf_req_params_t params;
61134219Smcpowers 	crypto_object_attribute_t *k_in_attrs = NULL;
61144219Smcpowers 	crypto_object_attribute_t *k_out_attrs = NULL;
61154219Smcpowers 	crypto_mechanism_t mech;
61164219Smcpowers 	crypto_key_t base_key;
61174219Smcpowers 	crypto_session_id_t session_id;
61184219Smcpowers 	crypto_minor_t *cm;
61194219Smcpowers 	crypto_session_data_t *sp = NULL;
61204219Smcpowers 	size_t k_in_attrs_size, k_out_attrs_size;
61214219Smcpowers 	size_t key_rctl_bytes = 0, mech_rctl_bytes = 0;
61226424Skrishna 	boolean_t mech_rctl_chk = B_FALSE;
61236424Skrishna 	boolean_t key_rctl_chk = B_FALSE;
61244219Smcpowers 	size_t in_attributes_rctl_bytes = 0;
61254219Smcpowers 	size_t out_attributes_rctl_bytes = 0;
61266424Skrishna 	boolean_t in_attributes_rctl_chk = B_FALSE;
61276424Skrishna 	boolean_t out_attributes_rctl_chk = B_FALSE;
61284219Smcpowers 	caddr_t in_attributes, out_attributes;
61294219Smcpowers 	uint_t in_count, out_count;
61304219Smcpowers 	int error = 0;
61314219Smcpowers 	int rv;
61324219Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
61334219Smcpowers 	caddr_t u_attrs = NULL;
61344219Smcpowers 
61354219Smcpowers 	STRUCT_INIT(derive_key, mode);
61364219Smcpowers 	STRUCT_INIT(oa, mode);
61374219Smcpowers 
61384219Smcpowers 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
61394219Smcpowers 		cmn_err(CE_WARN, "nostore_derive_key: failed holding minor");
61404219Smcpowers 		return (ENXIO);
61414219Smcpowers 	}
61424219Smcpowers 
61434219Smcpowers 	if (copyin(arg, STRUCT_BUF(derive_key), STRUCT_SIZE(derive_key)) != 0) {
61444219Smcpowers 		crypto_release_minor(cm);
61454219Smcpowers 		return (EFAULT);
61464219Smcpowers 	}
61474219Smcpowers 
61484219Smcpowers 	bzero(&base_key, sizeof (base_key));
61494219Smcpowers 
61504219Smcpowers 	session_id = STRUCT_FGET(derive_key, ndk_session);
61514219Smcpowers 
61524219Smcpowers 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
61534219Smcpowers 		goto release_minor;
61544219Smcpowers 	}
61554219Smcpowers 
61564219Smcpowers 	bcopy(STRUCT_FADDR(derive_key, ndk_mechanism), &mech.cm_type,
61574219Smcpowers 	    sizeof (crypto_mech_type_t));
61584219Smcpowers 
61594219Smcpowers 	if ((rv = kcf_get_hardware_provider(mech.cm_type, CRYPTO_MECH_INVALID,
61604219Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider,
61614219Smcpowers 	    CRYPTO_FG_DERIVE)) != CRYPTO_SUCCESS) {
61624219Smcpowers 		goto release_minor;
61634219Smcpowers 	}
61644219Smcpowers 
61654219Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
61664219Smcpowers 	    STRUCT_FADDR(derive_key, ndk_mechanism), &mech, mode, &error);
61674219Smcpowers 
61684219Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
61694219Smcpowers 		allocated_by_crypto_module = B_TRUE;
61706424Skrishna 		if (!copyin_mech(mode, sp,
61716424Skrishna 		    STRUCT_FADDR(derive_key, ndk_mechanism),
61726424Skrishna 		    &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) {
61734219Smcpowers 			goto release_minor;
61744219Smcpowers 		}
61754219Smcpowers 	} else {
61764219Smcpowers 		if (rv != CRYPTO_SUCCESS)
61774219Smcpowers 			goto release_minor;
61784219Smcpowers 	}
61794219Smcpowers 
61806424Skrishna 	if (!copyin_key(mode, sp, STRUCT_FADDR(derive_key, ndk_base_key),
61816424Skrishna 	    &base_key, &key_rctl_bytes, &key_rctl_chk, &rv, &error)) {
61824219Smcpowers 		goto release_minor;
61834219Smcpowers 	}
61844219Smcpowers 
61854219Smcpowers 	in_count = STRUCT_FGET(derive_key, ndk_in_count);
61864219Smcpowers 	out_count = STRUCT_FGET(derive_key, ndk_out_count);
61874219Smcpowers 
61884219Smcpowers 	in_attributes = STRUCT_FGETP(derive_key, ndk_in_attributes);
61896424Skrishna 	if (!copyin_attributes(mode, sp, in_count, in_attributes, &k_in_attrs,
61904219Smcpowers 	    &k_in_attrs_size, NULL, &rv, &error, &in_attributes_rctl_bytes,
61916424Skrishna 	    &in_attributes_rctl_chk, B_TRUE)) {
61924219Smcpowers 		goto release_minor;
61934219Smcpowers 	}
61944219Smcpowers 
61954219Smcpowers 	out_attributes = STRUCT_FGETP(derive_key, ndk_out_attributes);
61966424Skrishna 	if (!copyin_attributes(mode, sp, out_count, out_attributes,
61976424Skrishna 	    &k_out_attrs, &k_out_attrs_size, &u_attrs, &rv, &error,
61986424Skrishna 	    &out_attributes_rctl_bytes,
61996424Skrishna 	    &out_attributes_rctl_chk, B_FALSE)) {
62004219Smcpowers 		goto release_minor;
62014219Smcpowers 	}
62024219Smcpowers 
62034219Smcpowers 	KCF_WRAP_NOSTORE_KEY_OPS_PARAMS(&params, KCF_OP_KEY_DERIVE,
62044219Smcpowers 	    sp->sd_provider_session->ps_session, &mech, k_in_attrs, in_count,
62054219Smcpowers 	    NULL, 0, &base_key, k_out_attrs, out_count, NULL, 0);
62064219Smcpowers 
62074219Smcpowers 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
62084219Smcpowers 
62094219Smcpowers 	if (rv == CRYPTO_SUCCESS) {
62104219Smcpowers 		rv = crypto_provider_copyout_mech_param(real_provider,
62114219Smcpowers 		    &mech, STRUCT_FADDR(derive_key, ndk_mechanism),
62124219Smcpowers 		    mode, &error);
62134219Smcpowers 
62144219Smcpowers 		if (rv == CRYPTO_NOT_SUPPORTED) {
62154219Smcpowers 			rv = CRYPTO_SUCCESS;
62164219Smcpowers 		}
62174219Smcpowers 		/* copyout the derived secret */
62184219Smcpowers 		if (copyout_attributes(mode, out_attributes, out_count,
62194219Smcpowers 		    k_out_attrs, u_attrs) != 0)
62204219Smcpowers 			error = EFAULT;
62214219Smcpowers 	}
62224219Smcpowers 
62234219Smcpowers release_minor:
62246424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
62256424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk);
62266424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, in_attributes_rctl_bytes,
62276424Skrishna 	    in_attributes_rctl_chk);
62286424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, out_attributes_rctl_bytes,
62296424Skrishna 	    out_attributes_rctl_chk);
62304219Smcpowers 
62314219Smcpowers 	if (k_in_attrs != NULL)
62324219Smcpowers 		kmem_free(k_in_attrs, k_in_attrs_size);
62334219Smcpowers 	if (k_out_attrs != NULL) {
62344219Smcpowers 		bzero(k_out_attrs, k_out_attrs_size);
62354219Smcpowers 		kmem_free(k_out_attrs, k_out_attrs_size);
62364219Smcpowers 	}
62374219Smcpowers 
62384219Smcpowers 	if (u_attrs != NULL)
62394219Smcpowers 		kmem_free(u_attrs, out_count * STRUCT_SIZE(oa));
62404219Smcpowers 
62414219Smcpowers 	free_crypto_key(&base_key);
62424219Smcpowers 
62434219Smcpowers 	if (error != 0)
62444219Smcpowers 		goto out;
62454219Smcpowers 
62464219Smcpowers 	STRUCT_FSET(derive_key, ndk_return_value, rv);
62474219Smcpowers 	if (copyout(STRUCT_BUF(derive_key), arg,
62484219Smcpowers 	    STRUCT_SIZE(derive_key)) != 0) {
62494219Smcpowers 		error = EFAULT;
62504219Smcpowers 	}
62514219Smcpowers out:
62526424Skrishna 	CRYPTO_SESSION_RELE(sp);
62534219Smcpowers 	crypto_release_minor(cm);
62544219Smcpowers 
62554219Smcpowers 	if (real_provider != NULL) {
62564219Smcpowers 		crypto_free_mech(real_provider,
62574219Smcpowers 		    allocated_by_crypto_module, &mech);
62584219Smcpowers 		KCF_PROV_REFRELE(real_provider);
62594219Smcpowers 	}
62604219Smcpowers 	return (error);
62614219Smcpowers }
62624219Smcpowers 
62634219Smcpowers /* ARGSUSED */
62644219Smcpowers static int
62650Sstevel@tonic-gate crypto_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *c,
62660Sstevel@tonic-gate     int *rval)
62670Sstevel@tonic-gate {
62680Sstevel@tonic-gate #define	ARG	((caddr_t)arg)
62690Sstevel@tonic-gate 
62700Sstevel@tonic-gate 	switch (cmd) {
62710Sstevel@tonic-gate 	case CRYPTO_GET_FUNCTION_LIST:
62720Sstevel@tonic-gate 		return (get_function_list(dev, ARG, mode, rval));
62730Sstevel@tonic-gate 
62740Sstevel@tonic-gate 	case CRYPTO_GET_MECHANISM_NUMBER:
62750Sstevel@tonic-gate 		return (get_mechanism_number(dev, ARG, mode, rval));
62760Sstevel@tonic-gate 
62770Sstevel@tonic-gate 	case CRYPTO_GET_PROVIDER_LIST:
62780Sstevel@tonic-gate 		return (get_provider_list(dev, ARG, mode, rval));
62790Sstevel@tonic-gate 
62800Sstevel@tonic-gate 	case CRYPTO_GET_PROVIDER_INFO:
62810Sstevel@tonic-gate 		return (get_provider_info(dev, ARG, mode, rval));
62820Sstevel@tonic-gate 
62830Sstevel@tonic-gate 	case CRYPTO_GET_PROVIDER_MECHANISMS:
62840Sstevel@tonic-gate 		return (get_provider_mechanisms(dev, ARG, mode, rval));
62850Sstevel@tonic-gate 
62860Sstevel@tonic-gate 	case CRYPTO_GET_PROVIDER_MECHANISM_INFO:
62870Sstevel@tonic-gate 		return (get_provider_mechanism_info(dev, ARG, mode, rval));
62880Sstevel@tonic-gate 
62890Sstevel@tonic-gate 	case CRYPTO_OPEN_SESSION:
62900Sstevel@tonic-gate 		return (open_session(dev, ARG, mode, rval));
62910Sstevel@tonic-gate 
62920Sstevel@tonic-gate 	case CRYPTO_CLOSE_SESSION:
62930Sstevel@tonic-gate 		return (close_session(dev, ARG, mode, rval));
62940Sstevel@tonic-gate 
62950Sstevel@tonic-gate 	case CRYPTO_ENCRYPT_INIT:
62960Sstevel@tonic-gate 		return (encrypt_init(dev, ARG, mode, rval));
62970Sstevel@tonic-gate 
62980Sstevel@tonic-gate 	case CRYPTO_DECRYPT_INIT:
62990Sstevel@tonic-gate 		return (decrypt_init(dev, ARG, mode, rval));
63000Sstevel@tonic-gate 
63010Sstevel@tonic-gate 	case CRYPTO_ENCRYPT:
63020Sstevel@tonic-gate 		return (encrypt(dev, ARG, mode, rval));
63030Sstevel@tonic-gate 
63040Sstevel@tonic-gate 	case CRYPTO_DECRYPT:
63050Sstevel@tonic-gate 		return (decrypt(dev, ARG, mode, rval));
63060Sstevel@tonic-gate 
63070Sstevel@tonic-gate 	case CRYPTO_ENCRYPT_UPDATE:
63080Sstevel@tonic-gate 		return (encrypt_update(dev, ARG, mode, rval));
63090Sstevel@tonic-gate 
63100Sstevel@tonic-gate 	case CRYPTO_DECRYPT_UPDATE:
63110Sstevel@tonic-gate 		return (decrypt_update(dev, ARG, mode, rval));
63120Sstevel@tonic-gate 
63130Sstevel@tonic-gate 	case CRYPTO_ENCRYPT_FINAL:
63140Sstevel@tonic-gate 		return (encrypt_final(dev, ARG, mode, rval));
63150Sstevel@tonic-gate 
63160Sstevel@tonic-gate 	case CRYPTO_DECRYPT_FINAL:
63170Sstevel@tonic-gate 		return (decrypt_final(dev, ARG, mode, rval));
63180Sstevel@tonic-gate 
63190Sstevel@tonic-gate 	case CRYPTO_DIGEST_INIT:
63200Sstevel@tonic-gate 		return (digest_init(dev, ARG, mode, rval));
63210Sstevel@tonic-gate 
63220Sstevel@tonic-gate 	case CRYPTO_DIGEST:
63230Sstevel@tonic-gate 		return (digest(dev, ARG, mode, rval));
63240Sstevel@tonic-gate 
63250Sstevel@tonic-gate 	case CRYPTO_DIGEST_UPDATE:
63260Sstevel@tonic-gate 		return (digest_update(dev, ARG, mode, rval));
63270Sstevel@tonic-gate 
63280Sstevel@tonic-gate 	case CRYPTO_DIGEST_KEY:
63290Sstevel@tonic-gate 		return (digest_key(dev, ARG, mode, rval));
63300Sstevel@tonic-gate 
63310Sstevel@tonic-gate 	case CRYPTO_DIGEST_FINAL:
63320Sstevel@tonic-gate 		return (digest_final(dev, ARG, mode, rval));
63330Sstevel@tonic-gate 
63340Sstevel@tonic-gate 	case CRYPTO_SIGN_INIT:
63350Sstevel@tonic-gate 		return (sign_init(dev, ARG, mode, rval));
63360Sstevel@tonic-gate 
63370Sstevel@tonic-gate 	case CRYPTO_SIGN:
63380Sstevel@tonic-gate 		return (sign(dev, ARG, mode, rval));
63390Sstevel@tonic-gate 
63400Sstevel@tonic-gate 	case CRYPTO_SIGN_UPDATE:
63410Sstevel@tonic-gate 		return (sign_update(dev, ARG, mode, rval));
63420Sstevel@tonic-gate 
63430Sstevel@tonic-gate 	case CRYPTO_SIGN_FINAL:
63440Sstevel@tonic-gate 		return (sign_final(dev, ARG, mode, rval));
63450Sstevel@tonic-gate 
63460Sstevel@tonic-gate 	case CRYPTO_SIGN_RECOVER_INIT:
63470Sstevel@tonic-gate 		return (sign_recover_init(dev, ARG, mode, rval));
63480Sstevel@tonic-gate 
63490Sstevel@tonic-gate 	case CRYPTO_SIGN_RECOVER:
63500Sstevel@tonic-gate 		return (sign_recover(dev, ARG, mode, rval));
63510Sstevel@tonic-gate 
63520Sstevel@tonic-gate 	case CRYPTO_VERIFY_INIT:
63530Sstevel@tonic-gate 		return (verify_init(dev, ARG, mode, rval));
63540Sstevel@tonic-gate 
63550Sstevel@tonic-gate 	case CRYPTO_VERIFY:
63560Sstevel@tonic-gate 		return (verify(dev, ARG, mode, rval));
63570Sstevel@tonic-gate 
63580Sstevel@tonic-gate 	case CRYPTO_VERIFY_UPDATE:
63590Sstevel@tonic-gate 		return (verify_update(dev, ARG, mode, rval));
63600Sstevel@tonic-gate 
63610Sstevel@tonic-gate 	case CRYPTO_VERIFY_FINAL:
63620Sstevel@tonic-gate 		return (verify_final(dev, ARG, mode, rval));
63630Sstevel@tonic-gate 
63640Sstevel@tonic-gate 	case CRYPTO_VERIFY_RECOVER_INIT:
63650Sstevel@tonic-gate 		return (verify_recover_init(dev, ARG, mode, rval));
63660Sstevel@tonic-gate 
63670Sstevel@tonic-gate 	case CRYPTO_VERIFY_RECOVER:
63680Sstevel@tonic-gate 		return (verify_recover(dev, ARG, mode, rval));
63690Sstevel@tonic-gate 
63700Sstevel@tonic-gate 	case CRYPTO_SET_PIN:
63710Sstevel@tonic-gate 		return (set_pin(dev, ARG, mode, rval));
63720Sstevel@tonic-gate 
63730Sstevel@tonic-gate 	case CRYPTO_LOGIN:
63740Sstevel@tonic-gate 		return (login(dev, ARG, mode, rval));
63750Sstevel@tonic-gate 
63760Sstevel@tonic-gate 	case CRYPTO_LOGOUT:
63770Sstevel@tonic-gate 		return (logout(dev, ARG, mode, rval));
63780Sstevel@tonic-gate 
63790Sstevel@tonic-gate 	case CRYPTO_SEED_RANDOM:
63800Sstevel@tonic-gate 		return (seed_random(dev, ARG, mode, rval));
63810Sstevel@tonic-gate 
63820Sstevel@tonic-gate 	case CRYPTO_GENERATE_RANDOM:
63830Sstevel@tonic-gate 		return (generate_random(dev, ARG, mode, rval));
63840Sstevel@tonic-gate 
63850Sstevel@tonic-gate 	case CRYPTO_OBJECT_CREATE:
63860Sstevel@tonic-gate 		return (object_create(dev, ARG, mode, rval));
63870Sstevel@tonic-gate 
63880Sstevel@tonic-gate 	case CRYPTO_OBJECT_COPY:
63890Sstevel@tonic-gate 		return (object_copy(dev, ARG, mode, rval));
63900Sstevel@tonic-gate 
63910Sstevel@tonic-gate 	case CRYPTO_OBJECT_DESTROY:
63920Sstevel@tonic-gate 		return (object_destroy(dev, ARG, mode, rval));
63930Sstevel@tonic-gate 
63940Sstevel@tonic-gate 	case CRYPTO_OBJECT_GET_ATTRIBUTE_VALUE:
63950Sstevel@tonic-gate 		return (object_get_attribute_value(dev, ARG, mode, rval));
63960Sstevel@tonic-gate 
63970Sstevel@tonic-gate 	case CRYPTO_OBJECT_GET_SIZE:
63980Sstevel@tonic-gate 		return (object_get_size(dev, ARG, mode, rval));
63990Sstevel@tonic-gate 
64000Sstevel@tonic-gate 	case CRYPTO_OBJECT_SET_ATTRIBUTE_VALUE:
64010Sstevel@tonic-gate 		return (object_set_attribute_value(dev, ARG, mode, rval));
64020Sstevel@tonic-gate 
64030Sstevel@tonic-gate 	case CRYPTO_OBJECT_FIND_INIT:
64040Sstevel@tonic-gate 		return (object_find_init(dev, ARG, mode, rval));
64050Sstevel@tonic-gate 
64060Sstevel@tonic-gate 	case CRYPTO_OBJECT_FIND_UPDATE:
64070Sstevel@tonic-gate 		return (object_find_update(dev, ARG, mode, rval));
64080Sstevel@tonic-gate 
64090Sstevel@tonic-gate 	case CRYPTO_OBJECT_FIND_FINAL:
64100Sstevel@tonic-gate 		return (object_find_final(dev, ARG, mode, rval));
64110Sstevel@tonic-gate 
64120Sstevel@tonic-gate 	case CRYPTO_GENERATE_KEY:
64130Sstevel@tonic-gate 		return (object_generate_key(dev, ARG, mode, rval));
64140Sstevel@tonic-gate 
64150Sstevel@tonic-gate 	case CRYPTO_GENERATE_KEY_PAIR:
64160Sstevel@tonic-gate 		return (object_generate_key_pair(dev, ARG, mode, rval));
64170Sstevel@tonic-gate 
64180Sstevel@tonic-gate 	case CRYPTO_WRAP_KEY:
64190Sstevel@tonic-gate 		return (object_wrap_key(dev, ARG, mode, rval));
64200Sstevel@tonic-gate 
64210Sstevel@tonic-gate 	case CRYPTO_UNWRAP_KEY:
64220Sstevel@tonic-gate 		return (object_unwrap_key(dev, ARG, mode, rval));
64230Sstevel@tonic-gate 
64240Sstevel@tonic-gate 	case CRYPTO_DERIVE_KEY:
64250Sstevel@tonic-gate 		return (object_derive_key(dev, ARG, mode, rval));
64264219Smcpowers 
64274219Smcpowers 	case CRYPTO_NOSTORE_GENERATE_KEY:
64284219Smcpowers 		return (nostore_generate_key(dev, ARG, mode, rval));
64294219Smcpowers 
64304219Smcpowers 	case CRYPTO_NOSTORE_GENERATE_KEY_PAIR:
64314219Smcpowers 		return (nostore_generate_key_pair(dev, ARG, mode, rval));
64324219Smcpowers 
64334219Smcpowers 	case CRYPTO_NOSTORE_DERIVE_KEY:
64344219Smcpowers 		return (nostore_derive_key(dev, ARG, mode, rval));
64350Sstevel@tonic-gate 	}
64360Sstevel@tonic-gate 	return (EINVAL);
64370Sstevel@tonic-gate }
64380Sstevel@tonic-gate 
64390Sstevel@tonic-gate /*
64400Sstevel@tonic-gate  * Check for the project.max-crypto-memory resource control.
64410Sstevel@tonic-gate  */
64420Sstevel@tonic-gate static int
64433916Skrishna crypto_buffer_check(size_t need)
64440Sstevel@tonic-gate {
64453620Skrishna 	kproject_t *kpj;
64460Sstevel@tonic-gate 
64470Sstevel@tonic-gate 	if (need == 0)
64480Sstevel@tonic-gate 		return (CRYPTO_SUCCESS);
64490Sstevel@tonic-gate 
64500Sstevel@tonic-gate 	mutex_enter(&curproc->p_lock);
64513620Skrishna 	kpj = curproc->p_task->tk_proj;
64523916Skrishna 	mutex_enter(&(kpj->kpj_data.kpd_crypto_lock));
64533620Skrishna 
64543620Skrishna 	if (kpj->kpj_data.kpd_crypto_mem + need >
64553620Skrishna 	    kpj->kpj_data.kpd_crypto_mem_ctl) {
64563620Skrishna 		if (rctl_test(rc_project_crypto_mem,
64573620Skrishna 		    kpj->kpj_rctls, curproc, need, 0) & RCT_DENY) {
64583916Skrishna 			mutex_exit(&(kpj->kpj_data.kpd_crypto_lock));
64593620Skrishna 			mutex_exit(&curproc->p_lock);
64603620Skrishna 			return (CRYPTO_HOST_MEMORY);
64613620Skrishna 		}
64623620Skrishna 	}
64633620Skrishna 
64643620Skrishna 	kpj->kpj_data.kpd_crypto_mem += need;
64653916Skrishna 	mutex_exit(&(kpj->kpj_data.kpd_crypto_lock));
64663916Skrishna 
64673916Skrishna 	curproc->p_crypto_mem += need;
64680Sstevel@tonic-gate 	mutex_exit(&curproc->p_lock);
64693620Skrishna 
64700Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
64710Sstevel@tonic-gate }
6472