xref: /onnv-gate/usr/src/uts/common/crypto/io/crypto.c (revision 8319:65ee8df1970c)
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 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * The ioctl interface for cryptographic commands.
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <sys/modctl.h>
330Sstevel@tonic-gate #include <sys/conf.h>
340Sstevel@tonic-gate #include <sys/stat.h>
350Sstevel@tonic-gate #include <sys/ddi.h>
360Sstevel@tonic-gate #include <sys/sunddi.h>
370Sstevel@tonic-gate #include <sys/kmem.h>
380Sstevel@tonic-gate #include <sys/errno.h>
390Sstevel@tonic-gate #include <sys/ksynch.h>
400Sstevel@tonic-gate #include <sys/file.h>
410Sstevel@tonic-gate #include <sys/open.h>
420Sstevel@tonic-gate #include <sys/cred.h>
430Sstevel@tonic-gate #include <sys/proc.h>
440Sstevel@tonic-gate #include <sys/task.h>
450Sstevel@tonic-gate #include <sys/mkdev.h>
460Sstevel@tonic-gate #include <sys/model.h>
470Sstevel@tonic-gate #include <sys/sysmacros.h>
480Sstevel@tonic-gate #include <sys/crypto/common.h>
490Sstevel@tonic-gate #include <sys/crypto/api.h>
500Sstevel@tonic-gate #include <sys/crypto/impl.h>
510Sstevel@tonic-gate #include <sys/crypto/sched_impl.h>
520Sstevel@tonic-gate #include <sys/crypto/ioctl.h>
530Sstevel@tonic-gate 
544841Shaimay extern int kcf_des3_threshold;
554841Shaimay extern int kcf_aes_threshold;
564841Shaimay extern int kcf_rc4_threshold;
574841Shaimay extern int kcf_md5_threshold;
584841Shaimay extern int kcf_sha1_threshold;
594841Shaimay 
600Sstevel@tonic-gate /*
610Sstevel@tonic-gate  * Locking notes:
620Sstevel@tonic-gate  *
630Sstevel@tonic-gate  * crypto_lock protects the global array of minor structures.  It
640Sstevel@tonic-gate  * also protects the cm_refcnt member of each of these structures.
657882SBhargava.Yenduri@Sun.COM  * The cm_cv is used to signal decrements in the cm_refcnt,
660Sstevel@tonic-gate  * and is used with the global crypto_lock.
670Sstevel@tonic-gate  *
680Sstevel@tonic-gate  * Other fields in the minor structure are protected by the
690Sstevel@tonic-gate  * cm_lock member of the minor structure.
700Sstevel@tonic-gate  */
710Sstevel@tonic-gate 
720Sstevel@tonic-gate /*
730Sstevel@tonic-gate  * DDI entry points.
740Sstevel@tonic-gate  */
750Sstevel@tonic-gate static int crypto_attach(dev_info_t *, ddi_attach_cmd_t);
760Sstevel@tonic-gate static int crypto_detach(dev_info_t *, ddi_detach_cmd_t);
770Sstevel@tonic-gate static int crypto_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
780Sstevel@tonic-gate static int crypto_open(dev_t *, int, int, cred_t *);
790Sstevel@tonic-gate static int crypto_close(dev_t, int, int, cred_t *);
800Sstevel@tonic-gate static int crypto_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
810Sstevel@tonic-gate 
82904Smcpowers static int cipher_init(dev_t, caddr_t, int, int (*)(crypto_provider_t,
830Sstevel@tonic-gate     crypto_session_id_t, crypto_mechanism_t *, crypto_key_t *,
840Sstevel@tonic-gate     crypto_ctx_template_t, crypto_context_t *, crypto_call_req_t *));
850Sstevel@tonic-gate 
860Sstevel@tonic-gate static int common_digest(dev_t, caddr_t, int, int (*)(crypto_context_t,
870Sstevel@tonic-gate     crypto_data_t *, crypto_data_t *, crypto_call_req_t *));
880Sstevel@tonic-gate 
890Sstevel@tonic-gate static int cipher(dev_t, caddr_t, int, int (*)(crypto_context_t,
900Sstevel@tonic-gate     crypto_data_t *, crypto_data_t *, crypto_call_req_t *));
910Sstevel@tonic-gate 
920Sstevel@tonic-gate static int cipher_update(dev_t, caddr_t, int, int (*)(crypto_context_t,
930Sstevel@tonic-gate     crypto_data_t *, crypto_data_t *, crypto_call_req_t *));
940Sstevel@tonic-gate 
950Sstevel@tonic-gate static int common_final(dev_t, caddr_t, int, int (*)(crypto_context_t,
960Sstevel@tonic-gate     crypto_data_t *, crypto_call_req_t *));
970Sstevel@tonic-gate 
98904Smcpowers static int sign_verify_init(dev_t, caddr_t, int, int (*)(crypto_provider_t,
990Sstevel@tonic-gate     crypto_session_id_t, crypto_mechanism_t *, crypto_key_t *,
1000Sstevel@tonic-gate     crypto_ctx_template_t, crypto_context_t *, crypto_call_req_t *));
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate static int sign_verify_update(dev_t dev, caddr_t arg, int mode,
1030Sstevel@tonic-gate     int (*)(crypto_context_t, crypto_data_t *, crypto_call_req_t *));
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate static void crypto_initialize_rctl(void);
1060Sstevel@tonic-gate static void crypto_release_provider_session(crypto_minor_t *,
1070Sstevel@tonic-gate     crypto_provider_session_t *);
1083916Skrishna static int crypto_buffer_check(size_t);
1090Sstevel@tonic-gate static int crypto_free_find_ctx(crypto_session_data_t *);
1100Sstevel@tonic-gate static int crypto_get_provider_list(crypto_minor_t *, uint_t *,
1110Sstevel@tonic-gate     crypto_provider_entry_t **, boolean_t);
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate /* number of minor numbers to allocate at a time */
1140Sstevel@tonic-gate #define	CRYPTO_MINOR_CHUNK	16
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate /*
1170Sstevel@tonic-gate  * There are two limits associated with kernel memory. The first,
1180Sstevel@tonic-gate  * CRYPTO_MAX_BUFFER_LEN, is the maximum number of bytes that can be
1190Sstevel@tonic-gate  * allocated for a single copyin/copyout buffer. The second limit is
1200Sstevel@tonic-gate  * the total number of bytes that can be allocated by a process
1210Sstevel@tonic-gate  * for copyin/copyout buffers. The latter is enforced by the
1220Sstevel@tonic-gate  * project.max-crypto-memory resource control.
1230Sstevel@tonic-gate  */
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate #define	CRYPTO_MAX_BUFFER_LEN	(2 * 1024 * 1024)
1260Sstevel@tonic-gate #define	CRYPTO_MAX_FIND_COUNT	512
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate /*
1296424Skrishna  * We preapprove some bytes for each session to avoid making the costly
1306424Skrishna  * crypto_buffer_check() calls. The preapproval is done when a new session
1316424Skrishna  * is created and that cost is amortized over later crypto calls.
1326424Skrishna  * Most applications create a session and then do a bunch of crypto calls
1336424Skrishna  * in that session. So, they benefit from this optimization.
1346424Skrishna  *
1356424Skrishna  * Note that we may hit the project.max-crypto-memory limit a bit sooner
1366424Skrishna  * because of this preapproval. But it is acceptable since the preapproved
1376424Skrishna  * amount is insignificant compared to the default max-crypto-memory limit
1386424Skrishna  * which is quarter of the machine's memory. The preapproved amount is
1396424Skrishna  * roughly 2 * 16K(maximum SSL record size).
1400Sstevel@tonic-gate  */
1416424Skrishna #define	CRYPTO_PRE_APPROVED_LIMIT	(32 * 1024)
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate /* The session table grows by CRYPTO_SESSION_CHUNK increments */
1440Sstevel@tonic-gate #define	CRYPTO_SESSION_CHUNK	100
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate size_t crypto_max_buffer_len = CRYPTO_MAX_BUFFER_LEN;
1476424Skrishna size_t crypto_pre_approved_limit = CRYPTO_PRE_APPROVED_LIMIT;
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate #define	INIT_RAW_CRYPTO_DATA(data, len)				\
1500Sstevel@tonic-gate 	(data).cd_format = CRYPTO_DATA_RAW;			\
1510Sstevel@tonic-gate 	(data).cd_raw.iov_base = kmem_alloc(len, KM_SLEEP);	\
1520Sstevel@tonic-gate 	(data).cd_raw.iov_len = len;				\
1530Sstevel@tonic-gate 	(data).cd_offset = 0;					\
1540Sstevel@tonic-gate 	(data).cd_length = len;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate static struct kmem_cache *crypto_session_cache;
1570Sstevel@tonic-gate static crypto_minor_t **crypto_minors = NULL;
1580Sstevel@tonic-gate static dev_info_t *crypto_dip = NULL;
1590Sstevel@tonic-gate static minor_t crypto_minor_chunk = CRYPTO_MINOR_CHUNK;
1600Sstevel@tonic-gate static minor_t crypto_minors_table_count = 0;
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate /*
1630Sstevel@tonic-gate  * Minors are started from 1 because vmem_alloc()
1640Sstevel@tonic-gate  * returns 0 in case of failure.
1650Sstevel@tonic-gate  */
1660Sstevel@tonic-gate static vmem_t *crypto_arena = NULL;	/* Arena for device minors */
1670Sstevel@tonic-gate static minor_t crypto_minors_count = 0;
1680Sstevel@tonic-gate static kmutex_t crypto_lock;
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate #define	RETURN_LIST			B_TRUE
1710Sstevel@tonic-gate #define	DONT_RETURN_LIST		B_FALSE
1720Sstevel@tonic-gate 
173904Smcpowers #define	CRYPTO_OPS_OFFSET(f)		offsetof(crypto_ops_t, co_##f)
1740Sstevel@tonic-gate #define	CRYPTO_RANDOM_OFFSET(f)		offsetof(crypto_random_number_ops_t, f)
1750Sstevel@tonic-gate #define	CRYPTO_SESSION_OFFSET(f)	offsetof(crypto_session_ops_t, f)
1760Sstevel@tonic-gate #define	CRYPTO_OBJECT_OFFSET(f)		offsetof(crypto_object_ops_t, f)
1770Sstevel@tonic-gate #define	CRYPTO_PROVIDER_OFFSET(f)	\
1780Sstevel@tonic-gate 	offsetof(crypto_provider_management_ops_t, f)
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate #define	CRYPTO_CANCEL_CTX(spp) {	\
1810Sstevel@tonic-gate 	crypto_cancel_ctx(*(spp));	\
1820Sstevel@tonic-gate 	*(spp) = NULL;			\
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate #define	CRYPTO_CANCEL_ALL_CTX(sp) {				\
1860Sstevel@tonic-gate 	if ((sp)->sd_digest_ctx != NULL) {			\
1870Sstevel@tonic-gate 		crypto_cancel_ctx((sp)->sd_digest_ctx);		\
1880Sstevel@tonic-gate 		(sp)->sd_digest_ctx = NULL;			\
1890Sstevel@tonic-gate 	}							\
1900Sstevel@tonic-gate 	if ((sp)->sd_encr_ctx != NULL) {			\
1910Sstevel@tonic-gate 		crypto_cancel_ctx((sp)->sd_encr_ctx);		\
1920Sstevel@tonic-gate 		(sp)->sd_encr_ctx = NULL;			\
1930Sstevel@tonic-gate 	}							\
1940Sstevel@tonic-gate 	if ((sp)->sd_decr_ctx != NULL) {			\
1950Sstevel@tonic-gate 		crypto_cancel_ctx((sp)->sd_decr_ctx);		\
1960Sstevel@tonic-gate 		(sp)->sd_decr_ctx = NULL;			\
1970Sstevel@tonic-gate 	}							\
1980Sstevel@tonic-gate 	if ((sp)->sd_sign_ctx != NULL) {			\
1990Sstevel@tonic-gate 		crypto_cancel_ctx((sp)->sd_sign_ctx);		\
2000Sstevel@tonic-gate 		(sp)->sd_sign_ctx = NULL;			\
2010Sstevel@tonic-gate 	}							\
2020Sstevel@tonic-gate 	if ((sp)->sd_verify_ctx != NULL) {			\
2030Sstevel@tonic-gate 		crypto_cancel_ctx((sp)->sd_verify_ctx);		\
2040Sstevel@tonic-gate 		(sp)->sd_verify_ctx = NULL;			\
2050Sstevel@tonic-gate 	}							\
2060Sstevel@tonic-gate 	if ((sp)->sd_sign_recover_ctx != NULL) {		\
2070Sstevel@tonic-gate 		crypto_cancel_ctx((sp)->sd_sign_recover_ctx);	\
2080Sstevel@tonic-gate 		(sp)->sd_sign_recover_ctx = NULL;		\
2090Sstevel@tonic-gate 	}							\
2100Sstevel@tonic-gate 	if ((sp)->sd_verify_recover_ctx != NULL) {		\
2110Sstevel@tonic-gate 		crypto_cancel_ctx((sp)->sd_verify_recover_ctx);	\
2120Sstevel@tonic-gate 		(sp)->sd_verify_recover_ctx = NULL;		\
2130Sstevel@tonic-gate 	}							\
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate 
2166424Skrishna #define	CRYPTO_DECREMENT_RCTL(val)	if ((val) != 0) {	\
2173916Skrishna 	kproject_t *projp;					\
2183916Skrishna 	mutex_enter(&curproc->p_lock);				\
2193916Skrishna 	projp = curproc->p_task->tk_proj;			\
2203916Skrishna 	ASSERT(projp != NULL);					\
2213916Skrishna 	mutex_enter(&(projp->kpj_data.kpd_crypto_lock));	\
2223916Skrishna 	projp->kpj_data.kpd_crypto_mem -= (val);		\
2233916Skrishna 	mutex_exit(&(projp->kpj_data.kpd_crypto_lock));		\
2243916Skrishna 	curproc->p_crypto_mem -= (val);				\
2253916Skrishna 	mutex_exit(&curproc->p_lock);				\
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate /*
2296424Skrishna  * We do not need to hold sd_lock in the macros below
2306424Skrishna  * as they are called after doing a get_session_ptr() which
2316424Skrishna  * sets the CRYPTO_SESSION_IS_BUSY flag.
2326424Skrishna  */
2336424Skrishna #define	CRYPTO_DECREMENT_RCTL_SESSION(sp, val, rctl_chk) 	\
2346867Skrishna 	if (((val) != 0) && ((sp) != NULL)) {			\
2356424Skrishna 		ASSERT(((sp)->sd_flags & CRYPTO_SESSION_IS_BUSY) != 0);	\
2366424Skrishna 		if (rctl_chk) {				\
2376424Skrishna 			CRYPTO_DECREMENT_RCTL(val);		\
2386424Skrishna 		} else {					\
2396424Skrishna 			(sp)->sd_pre_approved_amount += (val);	\
2406424Skrishna 		}						\
2416424Skrishna 	}
2426424Skrishna 
2436424Skrishna #define	CRYPTO_BUFFER_CHECK(sp, need, rctl_chk)		\
2446424Skrishna 	((sp->sd_pre_approved_amount >= need) ?			\
2456424Skrishna 	(sp->sd_pre_approved_amount -= need,			\
2466424Skrishna 	    rctl_chk = B_FALSE, CRYPTO_SUCCESS) :		\
2476424Skrishna 	    (rctl_chk = B_TRUE, crypto_buffer_check(need)))
2486424Skrishna 
2496424Skrishna /*
2500Sstevel@tonic-gate  * Module linkage.
2510Sstevel@tonic-gate  */
2520Sstevel@tonic-gate static struct cb_ops cbops = {
2530Sstevel@tonic-gate 	crypto_open,		/* cb_open */
2540Sstevel@tonic-gate 	crypto_close,		/* cb_close */
2550Sstevel@tonic-gate 	nodev,			/* cb_strategy */
2560Sstevel@tonic-gate 	nodev,			/* cb_print */
2570Sstevel@tonic-gate 	nodev,			/* cb_dump */
2580Sstevel@tonic-gate 	nodev,			/* cb_read */
2590Sstevel@tonic-gate 	nodev,			/* cb_write */
2600Sstevel@tonic-gate 	crypto_ioctl,		/* cb_ioctl */
2610Sstevel@tonic-gate 	nodev,			/* cb_devmap */
2620Sstevel@tonic-gate 	nodev,			/* cb_mmap */
2630Sstevel@tonic-gate 	nodev,			/* cb_segmap */
2640Sstevel@tonic-gate 	nochpoll,		/* cb_chpoll */
2650Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
2660Sstevel@tonic-gate 	NULL,			/* cb_streamtab */
2670Sstevel@tonic-gate 	D_MP,			/* cb_flag */
2680Sstevel@tonic-gate 	CB_REV,			/* cb_rev */
2690Sstevel@tonic-gate 	nodev,			/* cb_aread */
2700Sstevel@tonic-gate 	nodev,			/* cb_awrite */
2710Sstevel@tonic-gate };
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate static struct dev_ops devops = {
2740Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
2750Sstevel@tonic-gate 	0,			/* devo_refcnt */
2760Sstevel@tonic-gate 	crypto_getinfo,		/* devo_getinfo */
2770Sstevel@tonic-gate 	nulldev,		/* devo_identify */
2780Sstevel@tonic-gate 	nulldev,		/* devo_probe */
2790Sstevel@tonic-gate 	crypto_attach,		/* devo_attach */
2800Sstevel@tonic-gate 	crypto_detach,		/* devo_detach */
2810Sstevel@tonic-gate 	nodev,			/* devo_reset */
2820Sstevel@tonic-gate 	&cbops,			/* devo_cb_ops */
2830Sstevel@tonic-gate 	NULL,			/* devo_bus_ops */
2840Sstevel@tonic-gate 	NULL,			/* devo_power */
2857656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,		/* devo_quiesce */
2860Sstevel@tonic-gate };
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate static struct modldrv modldrv = {
2890Sstevel@tonic-gate 	&mod_driverops,					/* drv_modops */
2905072Smcpowers 	"Cryptographic Library Interface",	/* drv_linkinfo */
2910Sstevel@tonic-gate 	&devops,
2920Sstevel@tonic-gate };
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate static struct modlinkage modlinkage = {
2950Sstevel@tonic-gate 	MODREV_1,		/* ml_rev */
2960Sstevel@tonic-gate 	&modldrv,		/* ml_linkage */
2970Sstevel@tonic-gate 	NULL
2980Sstevel@tonic-gate };
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate /*
3010Sstevel@tonic-gate  * DDI entry points.
3020Sstevel@tonic-gate  */
3030Sstevel@tonic-gate int
3040Sstevel@tonic-gate _init(void)
3050Sstevel@tonic-gate {
3060Sstevel@tonic-gate 	return (mod_install(&modlinkage));
3070Sstevel@tonic-gate }
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate int
3100Sstevel@tonic-gate _fini(void)
3110Sstevel@tonic-gate {
3120Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate int
3160Sstevel@tonic-gate _info(struct modinfo *modinfop)
3170Sstevel@tonic-gate {
3180Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
3190Sstevel@tonic-gate }
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate /* ARGSUSED */
3220Sstevel@tonic-gate static int
3230Sstevel@tonic-gate crypto_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
3240Sstevel@tonic-gate {
3250Sstevel@tonic-gate 	switch (cmd) {
3260Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
3270Sstevel@tonic-gate 		*result = crypto_dip;
3280Sstevel@tonic-gate 		return (DDI_SUCCESS);
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
3310Sstevel@tonic-gate 		*result = (void *)0;
3320Sstevel@tonic-gate 		return (DDI_SUCCESS);
3330Sstevel@tonic-gate 	}
3340Sstevel@tonic-gate 	return (DDI_FAILURE);
3350Sstevel@tonic-gate }
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate static int
3380Sstevel@tonic-gate crypto_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3390Sstevel@tonic-gate {
3400Sstevel@tonic-gate 	if (cmd != DDI_ATTACH) {
3410Sstevel@tonic-gate 		return (DDI_FAILURE);
3420Sstevel@tonic-gate 	}
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	if (ddi_get_instance(dip) != 0) {
3450Sstevel@tonic-gate 		/* we only allow instance 0 to attach */
3460Sstevel@tonic-gate 		return (DDI_FAILURE);
3470Sstevel@tonic-gate 	}
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	crypto_session_cache = kmem_cache_create("crypto_session_cache",
3500Sstevel@tonic-gate 	    sizeof (crypto_session_data_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	if (crypto_session_cache == NULL)
3530Sstevel@tonic-gate 		return (DDI_FAILURE);
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	/* create the minor node */
3560Sstevel@tonic-gate 	if (ddi_create_minor_node(dip, "crypto", S_IFCHR, 0,
3570Sstevel@tonic-gate 	    DDI_PSEUDO, 0) != DDI_SUCCESS) {
3580Sstevel@tonic-gate 		kmem_cache_destroy(crypto_session_cache);
3590Sstevel@tonic-gate 		crypto_session_cache = NULL;
3600Sstevel@tonic-gate 		cmn_err(CE_WARN, "crypto_attach: failed creating minor node");
3610Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
3620Sstevel@tonic-gate 		return (DDI_FAILURE);
3630Sstevel@tonic-gate 	}
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	mutex_init(&crypto_lock, NULL, MUTEX_DRIVER, NULL);
3660Sstevel@tonic-gate 	crypto_dip = dip;
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	/* allocate integer space for minor numbers */
3690Sstevel@tonic-gate 	crypto_arena = vmem_create("crypto", (void *)1,
3700Sstevel@tonic-gate 	    CRYPTO_MINOR_CHUNK, 1, NULL, NULL, NULL, 0,
3710Sstevel@tonic-gate 	    VM_SLEEP | VMC_IDENTIFIER);
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	return (DDI_SUCCESS);
3740Sstevel@tonic-gate }
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate static int
3770Sstevel@tonic-gate crypto_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3780Sstevel@tonic-gate {
3790Sstevel@tonic-gate 	minor_t i;
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
3820Sstevel@tonic-gate 		return (DDI_FAILURE);
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	/* check if device is open */
3850Sstevel@tonic-gate 	mutex_enter(&crypto_lock);
3860Sstevel@tonic-gate 	for (i = 0; i < crypto_minors_table_count; i++) {
3870Sstevel@tonic-gate 		if (crypto_minors[i] != NULL) {
3880Sstevel@tonic-gate 			mutex_exit(&crypto_lock);
3890Sstevel@tonic-gate 			return (DDI_FAILURE);
3900Sstevel@tonic-gate 		}
3910Sstevel@tonic-gate 	}
3920Sstevel@tonic-gate 	mutex_exit(&crypto_lock);
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	crypto_dip = NULL;
3950Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	kmem_cache_destroy(crypto_session_cache);
3980Sstevel@tonic-gate 	crypto_session_cache = NULL;
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	kmem_free(crypto_minors,
4010Sstevel@tonic-gate 	    sizeof (crypto_minor_t *) * crypto_minors_table_count);
4020Sstevel@tonic-gate 	crypto_minors = NULL;
4030Sstevel@tonic-gate 	crypto_minors_table_count = 0;
4040Sstevel@tonic-gate 	mutex_destroy(&crypto_lock);
4050Sstevel@tonic-gate 	vmem_destroy(crypto_arena);
4060Sstevel@tonic-gate 	crypto_arena = NULL;
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	return (DDI_SUCCESS);
4090Sstevel@tonic-gate }
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate /* ARGSUSED */
4120Sstevel@tonic-gate static int
4130Sstevel@tonic-gate crypto_open(dev_t *devp, int flag, int otyp, cred_t *credp)
4140Sstevel@tonic-gate {
4150Sstevel@tonic-gate 	crypto_minor_t *cm = NULL;
4160Sstevel@tonic-gate 	minor_t mn;
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
4190Sstevel@tonic-gate 		return (ENXIO);
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	if (crypto_dip == NULL)
4220Sstevel@tonic-gate 		return (ENXIO);
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	/* exclusive opens are not supported */
4250Sstevel@tonic-gate 	if (flag & FEXCL)
4260Sstevel@tonic-gate 		return (ENOTSUP);
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	mutex_enter(&crypto_lock);
4290Sstevel@tonic-gate again:
4300Sstevel@tonic-gate 	/* grow the minors table if needed */
4310Sstevel@tonic-gate 	if (crypto_minors_count >= crypto_minors_table_count) {
4320Sstevel@tonic-gate 		crypto_minor_t **newtable;
4330Sstevel@tonic-gate 		minor_t chunk = crypto_minor_chunk;
4340Sstevel@tonic-gate 		minor_t saved_count;
4350Sstevel@tonic-gate 		size_t new_size;
4360Sstevel@tonic-gate 		ulong_t big_count;
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 		big_count = crypto_minors_count + chunk;
4390Sstevel@tonic-gate 		if (big_count > MAXMIN) {
4400Sstevel@tonic-gate 			mutex_exit(&crypto_lock);
4410Sstevel@tonic-gate 			return (ENOMEM);
4420Sstevel@tonic-gate 		}
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 		saved_count = crypto_minors_table_count;
4450Sstevel@tonic-gate 		new_size = sizeof (crypto_minor_t *) *
4460Sstevel@tonic-gate 		    (crypto_minors_table_count + chunk);
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 		mutex_exit(&crypto_lock);
4490Sstevel@tonic-gate 		newtable = kmem_zalloc(new_size, KM_SLEEP);
4500Sstevel@tonic-gate 		mutex_enter(&crypto_lock);
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 		/*
4530Sstevel@tonic-gate 		 * Check if table grew while we were sleeping.
4540Sstevel@tonic-gate 		 * The minors table never shrinks.
4550Sstevel@tonic-gate 		 */
4560Sstevel@tonic-gate 		if (crypto_minors_table_count > saved_count) {
4570Sstevel@tonic-gate 			kmem_free(newtable, new_size);
4580Sstevel@tonic-gate 			goto again;
4590Sstevel@tonic-gate 		}
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 		/* we assume that bcopy() will return if count is 0 */
4620Sstevel@tonic-gate 		bcopy(crypto_minors, newtable,
4630Sstevel@tonic-gate 		    sizeof (crypto_minor_t *) * crypto_minors_table_count);
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 		kmem_free(crypto_minors,
4660Sstevel@tonic-gate 		    sizeof (crypto_minor_t *) * crypto_minors_table_count);
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 		/* grow the minors number space */
4690Sstevel@tonic-gate 		if (crypto_minors_table_count != 0) {
4700Sstevel@tonic-gate 			(void) vmem_add(crypto_arena,
4710Sstevel@tonic-gate 			    (void *)(uintptr_t)(crypto_minors_table_count + 1),
4720Sstevel@tonic-gate 			    crypto_minor_chunk, VM_SLEEP);
4730Sstevel@tonic-gate 		}
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 		crypto_minors = newtable;
4760Sstevel@tonic-gate 		crypto_minors_table_count += chunk;
4770Sstevel@tonic-gate 	}
4780Sstevel@tonic-gate 	mutex_exit(&crypto_lock);
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	/* allocate a new minor number starting with 1 */
4810Sstevel@tonic-gate 	mn = (minor_t)(uintptr_t)vmem_alloc(crypto_arena, 1, VM_SLEEP);
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	cm = kmem_zalloc(sizeof (crypto_minor_t), KM_SLEEP);
4840Sstevel@tonic-gate 	mutex_init(&cm->cm_lock, NULL, MUTEX_DRIVER, NULL);
4850Sstevel@tonic-gate 	cv_init(&cm->cm_cv, NULL, CV_DRIVER, NULL);
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	mutex_enter(&crypto_lock);
4887882SBhargava.Yenduri@Sun.COM 	cm->cm_refcnt = 1;
4890Sstevel@tonic-gate 	crypto_minors[mn - 1] = cm;
4900Sstevel@tonic-gate 	crypto_minors_count++;
4910Sstevel@tonic-gate 	mutex_exit(&crypto_lock);
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	*devp = makedevice(getmajor(*devp), mn);
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	return (0);
4960Sstevel@tonic-gate }
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate /* ARGSUSED */
4990Sstevel@tonic-gate static int
5000Sstevel@tonic-gate crypto_close(dev_t dev, int flag, int otyp, cred_t *credp)
5010Sstevel@tonic-gate {
5020Sstevel@tonic-gate 	crypto_minor_t *cm = NULL;
5030Sstevel@tonic-gate 	crypto_session_data_t *sp;
5040Sstevel@tonic-gate 	minor_t mn = getminor(dev);
5050Sstevel@tonic-gate 	uint_t i;
5066424Skrishna 	size_t total = 0;
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	mutex_enter(&crypto_lock);
5090Sstevel@tonic-gate 	if (mn > crypto_minors_table_count) {
5100Sstevel@tonic-gate 		mutex_exit(&crypto_lock);
5110Sstevel@tonic-gate 		cmn_err(CE_WARN, "crypto_close: bad minor (too big) %d", mn);
5120Sstevel@tonic-gate 		return (ENODEV);
5130Sstevel@tonic-gate 	}
5140Sstevel@tonic-gate 
5157882SBhargava.Yenduri@Sun.COM 	cm = crypto_minors[mn - 1];
5160Sstevel@tonic-gate 	if (cm == NULL) {
5170Sstevel@tonic-gate 		mutex_exit(&crypto_lock);
5180Sstevel@tonic-gate 		cmn_err(CE_WARN, "crypto_close: duplicate close of minor %d",
5190Sstevel@tonic-gate 		    getminor(dev));
5200Sstevel@tonic-gate 		return (ENODEV);
5210Sstevel@tonic-gate 	}
5220Sstevel@tonic-gate 
5237882SBhargava.Yenduri@Sun.COM 	cm->cm_refcnt --;		/* decrement refcnt held in open */
5247882SBhargava.Yenduri@Sun.COM 	while (cm->cm_refcnt > 0) {
5257882SBhargava.Yenduri@Sun.COM 		cv_wait(&cm->cm_cv, &crypto_lock);
5267882SBhargava.Yenduri@Sun.COM 	}
5277882SBhargava.Yenduri@Sun.COM 
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 {
5807882SBhargava.Yenduri@Sun.COM 	crypto_minor_t *cm;
5817882SBhargava.Yenduri@Sun.COM 
5827882SBhargava.Yenduri@Sun.COM 	if (minor > crypto_minors_table_count)
5837882SBhargava.Yenduri@Sun.COM 		return (NULL);
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 	mutex_enter(&crypto_lock);
5867882SBhargava.Yenduri@Sun.COM 	if ((cm = crypto_minors[minor - 1]) != NULL) {
5870Sstevel@tonic-gate 		cm->cm_refcnt++;
5880Sstevel@tonic-gate 	}
5890Sstevel@tonic-gate 	mutex_exit(&crypto_lock);
5900Sstevel@tonic-gate 	return (cm);
5910Sstevel@tonic-gate }
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate static void
5940Sstevel@tonic-gate crypto_release_minor(crypto_minor_t *cm)
5950Sstevel@tonic-gate {
5960Sstevel@tonic-gate 	mutex_enter(&crypto_lock);
5970Sstevel@tonic-gate 	cm->cm_refcnt--;
5980Sstevel@tonic-gate 	if (cm->cm_refcnt == 0) {
5997882SBhargava.Yenduri@Sun.COM 		cv_signal(&cm->cm_cv);
6000Sstevel@tonic-gate 	}
6010Sstevel@tonic-gate 	mutex_exit(&crypto_lock);
6020Sstevel@tonic-gate }
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate /*
6054072Skrishna  * Build a list of functions and other information for the provider, pd.
6060Sstevel@tonic-gate  */
6070Sstevel@tonic-gate static void
6080Sstevel@tonic-gate crypto_build_function_list(crypto_function_list_t *fl, kcf_provider_desc_t *pd)
6090Sstevel@tonic-gate {
6100Sstevel@tonic-gate 	crypto_ops_t *ops;
6110Sstevel@tonic-gate 	crypto_digest_ops_t *digest_ops;
6120Sstevel@tonic-gate 	crypto_cipher_ops_t *cipher_ops;
6130Sstevel@tonic-gate 	crypto_mac_ops_t *mac_ops;
6140Sstevel@tonic-gate 	crypto_sign_ops_t *sign_ops;
6150Sstevel@tonic-gate 	crypto_verify_ops_t *verify_ops;
6160Sstevel@tonic-gate 	crypto_dual_ops_t *dual_ops;
6170Sstevel@tonic-gate 	crypto_random_number_ops_t *random_number_ops;
6180Sstevel@tonic-gate 	crypto_session_ops_t *session_ops;
6190Sstevel@tonic-gate 	crypto_object_ops_t *object_ops;
6200Sstevel@tonic-gate 	crypto_key_ops_t *key_ops;
6210Sstevel@tonic-gate 	crypto_provider_management_ops_t *provider_ops;
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	if ((ops = pd->pd_ops_vector) == NULL)
6240Sstevel@tonic-gate 		return;
6250Sstevel@tonic-gate 
626904Smcpowers 	if ((digest_ops = ops->co_digest_ops) != NULL) {
6270Sstevel@tonic-gate 		if (digest_ops->digest_init != NULL)
6280Sstevel@tonic-gate 			fl->fl_digest_init = B_TRUE;
6290Sstevel@tonic-gate 		if (digest_ops->digest != NULL)
6300Sstevel@tonic-gate 			fl->fl_digest = B_TRUE;
6310Sstevel@tonic-gate 		if (digest_ops->digest_update != NULL)
6320Sstevel@tonic-gate 			fl->fl_digest_update = B_TRUE;
6330Sstevel@tonic-gate 		if (digest_ops->digest_key != NULL)
6340Sstevel@tonic-gate 			fl->fl_digest_key = B_TRUE;
6350Sstevel@tonic-gate 		if (digest_ops->digest_final != NULL)
6360Sstevel@tonic-gate 			fl->fl_digest_final = B_TRUE;
6370Sstevel@tonic-gate 	}
638904Smcpowers 	if ((cipher_ops = ops->co_cipher_ops) != NULL) {
6390Sstevel@tonic-gate 		if (cipher_ops->encrypt_init != NULL)
6400Sstevel@tonic-gate 			fl->fl_encrypt_init = B_TRUE;
6410Sstevel@tonic-gate 		if (cipher_ops->encrypt != NULL)
6420Sstevel@tonic-gate 			fl->fl_encrypt = B_TRUE;
6430Sstevel@tonic-gate 		if (cipher_ops->encrypt_update != NULL)
6440Sstevel@tonic-gate 			fl->fl_encrypt_update = B_TRUE;
6450Sstevel@tonic-gate 		if (cipher_ops->encrypt_final != NULL)
6460Sstevel@tonic-gate 			fl->fl_encrypt_final = B_TRUE;
6470Sstevel@tonic-gate 		if (cipher_ops->decrypt_init != NULL)
6480Sstevel@tonic-gate 			fl->fl_decrypt_init = B_TRUE;
6490Sstevel@tonic-gate 		if (cipher_ops->decrypt != NULL)
6500Sstevel@tonic-gate 			fl->fl_decrypt = B_TRUE;
6510Sstevel@tonic-gate 		if (cipher_ops->decrypt_update != NULL)
6520Sstevel@tonic-gate 			fl->fl_decrypt_update = B_TRUE;
6530Sstevel@tonic-gate 		if (cipher_ops->decrypt_final != NULL)
6540Sstevel@tonic-gate 			fl->fl_decrypt_final = B_TRUE;
6550Sstevel@tonic-gate 	}
656904Smcpowers 	if ((mac_ops = ops->co_mac_ops) != NULL) {
6570Sstevel@tonic-gate 		if (mac_ops->mac_init != NULL)
6580Sstevel@tonic-gate 			fl->fl_mac_init = B_TRUE;
6590Sstevel@tonic-gate 		if (mac_ops->mac != NULL)
6600Sstevel@tonic-gate 			fl->fl_mac = B_TRUE;
6610Sstevel@tonic-gate 		if (mac_ops->mac_update != NULL)
6620Sstevel@tonic-gate 			fl->fl_mac_update = B_TRUE;
6630Sstevel@tonic-gate 		if (mac_ops->mac_final != NULL)
6640Sstevel@tonic-gate 			fl->fl_mac_final = B_TRUE;
6650Sstevel@tonic-gate 	}
666904Smcpowers 	if ((sign_ops = ops->co_sign_ops) != NULL) {
6670Sstevel@tonic-gate 		if (sign_ops->sign_init != NULL)
6680Sstevel@tonic-gate 			fl->fl_sign_init = B_TRUE;
6690Sstevel@tonic-gate 		if (sign_ops->sign != NULL)
6700Sstevel@tonic-gate 			fl->fl_sign = B_TRUE;
6710Sstevel@tonic-gate 		if (sign_ops->sign_update != NULL)
6720Sstevel@tonic-gate 			fl->fl_sign_update = B_TRUE;
6730Sstevel@tonic-gate 		if (sign_ops->sign_final != NULL)
6740Sstevel@tonic-gate 			fl->fl_sign_final = B_TRUE;
6750Sstevel@tonic-gate 		if (sign_ops->sign_recover_init != NULL)
6760Sstevel@tonic-gate 			fl->fl_sign_recover_init = B_TRUE;
6770Sstevel@tonic-gate 		if (sign_ops->sign_recover != NULL)
6780Sstevel@tonic-gate 			fl->fl_sign_recover = B_TRUE;
6790Sstevel@tonic-gate 	}
680904Smcpowers 	if ((verify_ops = ops->co_verify_ops) != NULL) {
6810Sstevel@tonic-gate 		if (verify_ops->verify_init != NULL)
6820Sstevel@tonic-gate 			fl->fl_verify_init = B_TRUE;
6830Sstevel@tonic-gate 		if (verify_ops->verify != NULL)
6840Sstevel@tonic-gate 			fl->fl_verify = B_TRUE;
6850Sstevel@tonic-gate 		if (verify_ops->verify_update != NULL)
6860Sstevel@tonic-gate 			fl->fl_verify_update = B_TRUE;
6870Sstevel@tonic-gate 		if (verify_ops->verify_final != NULL)
6880Sstevel@tonic-gate 			fl->fl_verify_final = B_TRUE;
6890Sstevel@tonic-gate 		if (verify_ops->verify_recover_init != NULL)
6900Sstevel@tonic-gate 			fl->fl_verify_recover_init = B_TRUE;
6910Sstevel@tonic-gate 		if (verify_ops->verify_recover != NULL)
6920Sstevel@tonic-gate 			fl->fl_verify_recover = B_TRUE;
6930Sstevel@tonic-gate 	}
694904Smcpowers 	if ((dual_ops = ops->co_dual_ops) != NULL) {
6950Sstevel@tonic-gate 		if (dual_ops->digest_encrypt_update != NULL)
6960Sstevel@tonic-gate 			fl->fl_digest_encrypt_update = B_TRUE;
6970Sstevel@tonic-gate 		if (dual_ops->decrypt_digest_update != NULL)
6980Sstevel@tonic-gate 			fl->fl_decrypt_digest_update = B_TRUE;
6990Sstevel@tonic-gate 		if (dual_ops->sign_encrypt_update != NULL)
7000Sstevel@tonic-gate 			fl->fl_sign_encrypt_update = B_TRUE;
7010Sstevel@tonic-gate 		if (dual_ops->decrypt_verify_update != NULL)
7020Sstevel@tonic-gate 			fl->fl_decrypt_verify_update = B_TRUE;
7030Sstevel@tonic-gate 	}
704904Smcpowers 	if ((random_number_ops = ops->co_random_ops) != NULL) {
7050Sstevel@tonic-gate 		if (random_number_ops->seed_random != NULL)
7060Sstevel@tonic-gate 			fl->fl_seed_random = B_TRUE;
7070Sstevel@tonic-gate 		if (random_number_ops->generate_random != NULL)
7080Sstevel@tonic-gate 			fl->fl_generate_random = B_TRUE;
7090Sstevel@tonic-gate 	}
710904Smcpowers 	if ((session_ops = ops->co_session_ops) != NULL) {
7110Sstevel@tonic-gate 		if (session_ops->session_open != NULL)
7120Sstevel@tonic-gate 			fl->fl_session_open = B_TRUE;
7130Sstevel@tonic-gate 		if (session_ops->session_close != NULL)
7140Sstevel@tonic-gate 			fl->fl_session_close = B_TRUE;
7150Sstevel@tonic-gate 		if (session_ops->session_login != NULL)
7160Sstevel@tonic-gate 			fl->fl_session_login = B_TRUE;
7170Sstevel@tonic-gate 		if (session_ops->session_logout != NULL)
7180Sstevel@tonic-gate 			fl->fl_session_logout = B_TRUE;
7190Sstevel@tonic-gate 	}
720904Smcpowers 	if ((object_ops = ops->co_object_ops) != NULL) {
7210Sstevel@tonic-gate 		if (object_ops->object_create != NULL)
7220Sstevel@tonic-gate 			fl->fl_object_create = B_TRUE;
7230Sstevel@tonic-gate 		if (object_ops->object_copy != NULL)
7240Sstevel@tonic-gate 			fl->fl_object_copy = B_TRUE;
7250Sstevel@tonic-gate 		if (object_ops->object_destroy != NULL)
7260Sstevel@tonic-gate 			fl->fl_object_destroy = B_TRUE;
7270Sstevel@tonic-gate 		if (object_ops->object_get_size != NULL)
7280Sstevel@tonic-gate 			fl->fl_object_get_size = B_TRUE;
7290Sstevel@tonic-gate 		if (object_ops->object_get_attribute_value != NULL)
7300Sstevel@tonic-gate 			fl->fl_object_get_attribute_value = B_TRUE;
7310Sstevel@tonic-gate 		if (object_ops->object_set_attribute_value != NULL)
7320Sstevel@tonic-gate 			fl->fl_object_set_attribute_value = B_TRUE;
7330Sstevel@tonic-gate 		if (object_ops->object_find_init != NULL)
7340Sstevel@tonic-gate 			fl->fl_object_find_init = B_TRUE;
7350Sstevel@tonic-gate 		if (object_ops->object_find != NULL)
7360Sstevel@tonic-gate 			fl->fl_object_find = B_TRUE;
7370Sstevel@tonic-gate 		if (object_ops->object_find_final != NULL)
7380Sstevel@tonic-gate 			fl->fl_object_find_final = B_TRUE;
7390Sstevel@tonic-gate 	}
740904Smcpowers 	if ((key_ops = ops->co_key_ops) != NULL) {
7410Sstevel@tonic-gate 		if (key_ops->key_generate != NULL)
7420Sstevel@tonic-gate 			fl->fl_key_generate = B_TRUE;
7430Sstevel@tonic-gate 		if (key_ops->key_generate_pair != NULL)
7440Sstevel@tonic-gate 			fl->fl_key_generate_pair = B_TRUE;
7450Sstevel@tonic-gate 		if (key_ops->key_wrap != NULL)
7460Sstevel@tonic-gate 			fl->fl_key_wrap = B_TRUE;
7470Sstevel@tonic-gate 		if (key_ops->key_unwrap != NULL)
7480Sstevel@tonic-gate 			fl->fl_key_unwrap = B_TRUE;
7490Sstevel@tonic-gate 		if (key_ops->key_derive != NULL)
7500Sstevel@tonic-gate 			fl->fl_key_derive = B_TRUE;
7510Sstevel@tonic-gate 	}
752904Smcpowers 	if ((provider_ops = ops->co_provider_ops) != NULL) {
7530Sstevel@tonic-gate 		if (provider_ops->init_token != NULL)
7540Sstevel@tonic-gate 			fl->fl_init_token = B_TRUE;
7550Sstevel@tonic-gate 		if (provider_ops->init_pin != NULL)
7560Sstevel@tonic-gate 			fl->fl_init_pin = B_TRUE;
7570Sstevel@tonic-gate 		if (provider_ops->set_pin != NULL)
7580Sstevel@tonic-gate 			fl->fl_set_pin = B_TRUE;
7590Sstevel@tonic-gate 	}
7604072Skrishna 
7614072Skrishna 	fl->prov_is_limited = pd->pd_flags & CRYPTO_HASH_NO_UPDATE;
7624072Skrishna 	if (fl->prov_is_limited) {
7634072Skrishna 		/*
7644072Skrishna 		 * XXX - The threshold should ideally be per hash
7654072Skrishna 		 * mechanism. For now, we use the same value for all
7664072Skrishna 		 * hash mechanisms. Empirical evidence suggests this
7674072Skrishna 		 * is fine.
7684072Skrishna 		 */
7694072Skrishna 		fl->prov_hash_threshold = kcf_md5_threshold;
7704072Skrishna 		fl->prov_hash_limit = min(pd->pd_hash_limit,
7714072Skrishna 		    min(CRYPTO_MAX_BUFFER_LEN,
7724072Skrishna 		    curproc->p_task->tk_proj->kpj_data.kpd_crypto_mem_ctl));
7734072Skrishna 	}
7744841Shaimay 
7754841Shaimay 	fl->total_threshold_count = MAX_NUM_THRESHOLD;
7764841Shaimay 	fl->fl_threshold[0].mech_type = CKM_DES3_CBC;
7774841Shaimay 	fl->fl_threshold[0].mech_threshold = kcf_des3_threshold;
7784841Shaimay 	fl->fl_threshold[1].mech_type = CKM_DES3_ECB;
7794841Shaimay 	fl->fl_threshold[1].mech_threshold = kcf_des3_threshold;
7804841Shaimay 	fl->fl_threshold[2].mech_type = CKM_AES_CBC;
7814841Shaimay 	fl->fl_threshold[2].mech_threshold = kcf_aes_threshold;
7824841Shaimay 	fl->fl_threshold[3].mech_type = CKM_AES_ECB;
7834841Shaimay 	fl->fl_threshold[3].mech_threshold = kcf_aes_threshold;
7844841Shaimay 	fl->fl_threshold[4].mech_type = CKM_RC4;
7854841Shaimay 	fl->fl_threshold[4].mech_threshold = kcf_rc4_threshold;
7864841Shaimay 	fl->fl_threshold[5].mech_type = CKM_MD5;
7874841Shaimay 	fl->fl_threshold[5].mech_threshold = kcf_md5_threshold;
7884841Shaimay 	fl->fl_threshold[6].mech_type = CKM_SHA_1;
7894841Shaimay 	fl->fl_threshold[6].mech_threshold = kcf_sha1_threshold;
7900Sstevel@tonic-gate }
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate /* ARGSUSED */
7930Sstevel@tonic-gate static int
7940Sstevel@tonic-gate get_function_list(dev_t dev, caddr_t arg, int mode, int *rval)
7950Sstevel@tonic-gate {
7960Sstevel@tonic-gate 	crypto_get_function_list_t get_function_list;
7970Sstevel@tonic-gate 	crypto_minor_t *cm;
7980Sstevel@tonic-gate 	crypto_provider_id_t provider_id;
7990Sstevel@tonic-gate 	crypto_function_list_t *fl;
8000Sstevel@tonic-gate 	kcf_provider_desc_t *provider;
8010Sstevel@tonic-gate 	int rv;
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
8040Sstevel@tonic-gate 		cmn_err(CE_WARN, "get_function_list: failed holding minor");
8050Sstevel@tonic-gate 		return (ENXIO);
8060Sstevel@tonic-gate 	}
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 	if (copyin(arg, &get_function_list, sizeof (get_function_list)) != 0) {
8090Sstevel@tonic-gate 		crypto_release_minor(cm);
8100Sstevel@tonic-gate 		return (EFAULT);
8110Sstevel@tonic-gate 	}
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 	/* initialize provider_array */
8140Sstevel@tonic-gate 	if (cm->cm_provider_array == NULL) {
8150Sstevel@tonic-gate 		rv = crypto_get_provider_list(cm, NULL, NULL, DONT_RETURN_LIST);
8160Sstevel@tonic-gate 		if (rv != CRYPTO_SUCCESS) {
8170Sstevel@tonic-gate 			goto release_minor;
8180Sstevel@tonic-gate 		}
8190Sstevel@tonic-gate 	}
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 	provider_id = get_function_list.fl_provider_id;
8220Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
8230Sstevel@tonic-gate 	/* index must be less than count of providers */
8240Sstevel@tonic-gate 	if (provider_id >= cm->cm_provider_count) {
8250Sstevel@tonic-gate 		mutex_exit(&cm->cm_lock);
8260Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
8270Sstevel@tonic-gate 		goto release_minor;
8280Sstevel@tonic-gate 	}
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 	ASSERT(cm->cm_provider_array != NULL);
8310Sstevel@tonic-gate 	provider = cm->cm_provider_array[provider_id];
8320Sstevel@tonic-gate 	mutex_exit(&cm->cm_lock);
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 	fl = &get_function_list.fl_list;
8350Sstevel@tonic-gate 	bzero(fl, sizeof (crypto_function_list_t));
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	if (provider->pd_prov_type != CRYPTO_LOGICAL_PROVIDER) {
8380Sstevel@tonic-gate 		crypto_build_function_list(fl, provider);
8390Sstevel@tonic-gate 	} else {
8400Sstevel@tonic-gate 		kcf_provider_desc_t *prev = NULL, *pd;
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate 		mutex_enter(&provider->pd_lock);
8430Sstevel@tonic-gate 		while (kcf_get_next_logical_provider_member(provider,
8440Sstevel@tonic-gate 		    prev, &pd)) {
8450Sstevel@tonic-gate 			prev = pd;
8460Sstevel@tonic-gate 			crypto_build_function_list(fl, pd);
8470Sstevel@tonic-gate 			KCF_PROV_REFRELE(pd);
8480Sstevel@tonic-gate 		}
8490Sstevel@tonic-gate 		mutex_exit(&provider->pd_lock);
8500Sstevel@tonic-gate 	}
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 	rv = CRYPTO_SUCCESS;
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate release_minor:
8550Sstevel@tonic-gate 	crypto_release_minor(cm);
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	get_function_list.fl_return_value = rv;
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 	if (copyout(&get_function_list, arg, sizeof (get_function_list)) != 0) {
8600Sstevel@tonic-gate 		return (EFAULT);
8610Sstevel@tonic-gate 	}
8620Sstevel@tonic-gate 	return (0);
8630Sstevel@tonic-gate }
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate /*
8660Sstevel@tonic-gate  * This ioctl maps a PKCS#11 mechanism string into an internal number
8670Sstevel@tonic-gate  * that is used by the kernel.  pn_internal_number is set to the
8680Sstevel@tonic-gate  * internal number.
8690Sstevel@tonic-gate  */
8700Sstevel@tonic-gate /* ARGSUSED */
8710Sstevel@tonic-gate static int
8720Sstevel@tonic-gate get_mechanism_number(dev_t dev, caddr_t arg, int mode, int *rval)
8730Sstevel@tonic-gate {
8740Sstevel@tonic-gate 	STRUCT_DECL(crypto_get_mechanism_number, get_number);
8750Sstevel@tonic-gate 	crypto_mech_type_t number;
8760Sstevel@tonic-gate 	size_t len;
8770Sstevel@tonic-gate 	char *mechanism_name;
8780Sstevel@tonic-gate 	int rv;
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 	STRUCT_INIT(get_number, mode);
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(get_number), STRUCT_SIZE(get_number)) != 0)
8830Sstevel@tonic-gate 		return (EFAULT);
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 	len = STRUCT_FGET(get_number, pn_mechanism_len);
8860Sstevel@tonic-gate 	if (len == 0 || len > CRYPTO_MAX_MECH_NAME) {
8870Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
8880Sstevel@tonic-gate 		goto out;
8890Sstevel@tonic-gate 	}
8900Sstevel@tonic-gate 	mechanism_name = kmem_alloc(len, KM_SLEEP);
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 	if (copyin(STRUCT_FGETP(get_number, pn_mechanism_string),
8930Sstevel@tonic-gate 	    mechanism_name, len) != 0) {
8940Sstevel@tonic-gate 		kmem_free(mechanism_name, len);
8950Sstevel@tonic-gate 		return (EFAULT);
8960Sstevel@tonic-gate 	}
8970Sstevel@tonic-gate 
8982935Skrishna 	/*
8992935Skrishna 	 * Get mechanism number from kcf. We set the load_module
9002935Skrishna 	 * flag to false since we use only hardware providers.
9012935Skrishna 	 */
9022935Skrishna 	number = crypto_mech2id_common(mechanism_name, B_FALSE);
9030Sstevel@tonic-gate 	kmem_free(mechanism_name, len);
9040Sstevel@tonic-gate 	if (number == CRYPTO_MECH_INVALID) {
9050Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
9060Sstevel@tonic-gate 		goto out;
9070Sstevel@tonic-gate 	}
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 	bcopy((char *)&number, (char *)STRUCT_FADDR(get_number,
9100Sstevel@tonic-gate 	    pn_internal_number), sizeof (number));
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	rv = CRYPTO_SUCCESS;
9130Sstevel@tonic-gate out:
9140Sstevel@tonic-gate 	STRUCT_FSET(get_number, pn_return_value, rv);
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(get_number), arg,
9170Sstevel@tonic-gate 	    STRUCT_SIZE(get_number)) != 0) {
9180Sstevel@tonic-gate 		return (EFAULT);
9190Sstevel@tonic-gate 	}
9200Sstevel@tonic-gate 	return (0);
9210Sstevel@tonic-gate }
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate /*
9248313SDina.Nimeh@Sun.Com  * This ioctl returns an array of crypto_mech_name_t entries.
9258313SDina.Nimeh@Sun.Com  * It lists all the PKCS#11 mechanisms available in the kernel.
9268313SDina.Nimeh@Sun.Com  */
9278313SDina.Nimeh@Sun.Com /* ARGSUSED */
9288313SDina.Nimeh@Sun.Com static int
9298313SDina.Nimeh@Sun.Com get_mechanism_list(dev_t dev, caddr_t arg, int mode, int *rval)
9308313SDina.Nimeh@Sun.Com {
9318313SDina.Nimeh@Sun.Com 	STRUCT_DECL(crypto_get_mechanism_list, get_list);
9328313SDina.Nimeh@Sun.Com 	crypto_mech_name_t *entries;
9338313SDina.Nimeh@Sun.Com 	size_t copyout_size;
9348313SDina.Nimeh@Sun.Com 	uint_t req_count;
9358313SDina.Nimeh@Sun.Com 	uint_t count;
9368313SDina.Nimeh@Sun.Com 	ulong_t offset;
9378313SDina.Nimeh@Sun.Com 	int error = 0;
9388313SDina.Nimeh@Sun.Com 
9398313SDina.Nimeh@Sun.Com 	STRUCT_INIT(get_list, mode);
9408313SDina.Nimeh@Sun.Com 
9418313SDina.Nimeh@Sun.Com 	if (copyin(arg, STRUCT_BUF(get_list), STRUCT_SIZE(get_list)) != 0) {
9428313SDina.Nimeh@Sun.Com 		return (EFAULT);
9438313SDina.Nimeh@Sun.Com 	}
9448313SDina.Nimeh@Sun.Com 
9458313SDina.Nimeh@Sun.Com 	entries = crypto_get_mech_list(&count, KM_SLEEP);
9468313SDina.Nimeh@Sun.Com 
9478313SDina.Nimeh@Sun.Com 	/* Number of entries caller thinks we have */
9488313SDina.Nimeh@Sun.Com 	req_count = STRUCT_FGET(get_list, ml_count);
9498313SDina.Nimeh@Sun.Com 
9508313SDina.Nimeh@Sun.Com 	STRUCT_FSET(get_list, ml_count, count);
9518313SDina.Nimeh@Sun.Com 	STRUCT_FSET(get_list, ml_return_value, CRYPTO_SUCCESS);
9528313SDina.Nimeh@Sun.Com 
9538313SDina.Nimeh@Sun.Com 	/* check if buffer is too small */
9548313SDina.Nimeh@Sun.Com 	if (count > req_count) {
9558313SDina.Nimeh@Sun.Com 		STRUCT_FSET(get_list, ml_return_value, CRYPTO_BUFFER_TOO_SMALL);
9568313SDina.Nimeh@Sun.Com 	}
9578313SDina.Nimeh@Sun.Com 
9588313SDina.Nimeh@Sun.Com 	/* copyout the first stuff */
9598313SDina.Nimeh@Sun.Com 	if (copyout(STRUCT_BUF(get_list), arg, STRUCT_SIZE(get_list)) != 0) {
9608313SDina.Nimeh@Sun.Com 		error = EFAULT;
9618313SDina.Nimeh@Sun.Com 	}
9628313SDina.Nimeh@Sun.Com 
9638313SDina.Nimeh@Sun.Com 	/*
9648313SDina.Nimeh@Sun.Com 	 * If only requesting number of entries or buffer too small or an
9658313SDina.Nimeh@Sun.Com 	 * error occurred, stop here
9668313SDina.Nimeh@Sun.Com 	 */
9678313SDina.Nimeh@Sun.Com 	if (req_count == 0 || count > req_count || error != 0) {
9688313SDina.Nimeh@Sun.Com 		goto out;
9698313SDina.Nimeh@Sun.Com 	}
9708313SDina.Nimeh@Sun.Com 
9718313SDina.Nimeh@Sun.Com 	copyout_size = count * sizeof (crypto_mech_name_t);
9728313SDina.Nimeh@Sun.Com 
9738313SDina.Nimeh@Sun.Com 	/* copyout entries */
9748313SDina.Nimeh@Sun.Com 	offset = (ulong_t)STRUCT_FADDR(get_list, ml_list);
9758313SDina.Nimeh@Sun.Com 	offset -= (ulong_t)STRUCT_BUF(get_list);
9768313SDina.Nimeh@Sun.Com 	if (copyout(entries, arg + offset, copyout_size) != 0) {
9778313SDina.Nimeh@Sun.Com 		error = EFAULT;
9788313SDina.Nimeh@Sun.Com 	}
9798313SDina.Nimeh@Sun.Com 
9808313SDina.Nimeh@Sun.Com out:
9818313SDina.Nimeh@Sun.Com 	crypto_free_mech_list(entries, count);
9828313SDina.Nimeh@Sun.Com 	return (error);
9838313SDina.Nimeh@Sun.Com }
9848313SDina.Nimeh@Sun.Com 
9858313SDina.Nimeh@Sun.Com /*
9868313SDina.Nimeh@Sun.Com  * Copyout kernel array of mech_infos to user space.
9878313SDina.Nimeh@Sun.Com  */
9888313SDina.Nimeh@Sun.Com /* ARGSUSED */
9898313SDina.Nimeh@Sun.Com static int
9908313SDina.Nimeh@Sun.Com copyout_mechinfos(int mode, caddr_t out, uint_t count,
9918313SDina.Nimeh@Sun.Com     crypto_mechanism_info_t *k_minfos, caddr_t u_minfos)
9928313SDina.Nimeh@Sun.Com {
9938313SDina.Nimeh@Sun.Com 	STRUCT_DECL(crypto_mechanism_info, mi);
9948313SDina.Nimeh@Sun.Com 	caddr_t p;
9958313SDina.Nimeh@Sun.Com 	size_t len;
9968313SDina.Nimeh@Sun.Com 	int i;
9978313SDina.Nimeh@Sun.Com 
9988313SDina.Nimeh@Sun.Com 	if (count == 0)
9998313SDina.Nimeh@Sun.Com 		return (0);
10008313SDina.Nimeh@Sun.Com 
10018313SDina.Nimeh@Sun.Com 	STRUCT_INIT(mi, mode);
10028313SDina.Nimeh@Sun.Com 
10038313SDina.Nimeh@Sun.Com 	len = count * STRUCT_SIZE(mi);
10048313SDina.Nimeh@Sun.Com 
10058313SDina.Nimeh@Sun.Com 	ASSERT(u_minfos != NULL);
10068313SDina.Nimeh@Sun.Com 	p = u_minfos;
10078313SDina.Nimeh@Sun.Com 	for (i = 0; i < count; i++) {
10088313SDina.Nimeh@Sun.Com 		STRUCT_FSET(mi, mi_min_key_size, k_minfos[i].mi_min_key_size);
10098313SDina.Nimeh@Sun.Com 		STRUCT_FSET(mi, mi_max_key_size, k_minfos[i].mi_max_key_size);
10108313SDina.Nimeh@Sun.Com 		STRUCT_FSET(mi, mi_keysize_unit, k_minfos[i].mi_keysize_unit);
10118313SDina.Nimeh@Sun.Com 		STRUCT_FSET(mi, mi_usage, k_minfos[i].mi_usage);
10128313SDina.Nimeh@Sun.Com 		bcopy(STRUCT_BUF(mi), p, STRUCT_SIZE(mi));
10138313SDina.Nimeh@Sun.Com 		p += STRUCT_SIZE(mi);
10148313SDina.Nimeh@Sun.Com 	}
10158313SDina.Nimeh@Sun.Com 
10168313SDina.Nimeh@Sun.Com 	if (copyout(u_minfos, out, len) != 0)
10178313SDina.Nimeh@Sun.Com 		return (EFAULT);
10188313SDina.Nimeh@Sun.Com 
10198313SDina.Nimeh@Sun.Com 	return (0);
10208313SDina.Nimeh@Sun.Com }
10218313SDina.Nimeh@Sun.Com 
10228313SDina.Nimeh@Sun.Com /*
10238313SDina.Nimeh@Sun.Com  * This ioctl returns information for the specified mechanism.
10248313SDina.Nimeh@Sun.Com  */
10258313SDina.Nimeh@Sun.Com /* ARGSUSED */
10268313SDina.Nimeh@Sun.Com static int
10278313SDina.Nimeh@Sun.Com get_all_mechanism_info(dev_t dev, caddr_t arg, int mode, int *rval)
10288313SDina.Nimeh@Sun.Com {
10298313SDina.Nimeh@Sun.Com 	STRUCT_DECL(crypto_get_all_mechanism_info, get_all_mech);
1030*8319SDina.Nimeh@Sun.Com 	/* LINTED E_FUNC_SET_NOT_USED */
10318313SDina.Nimeh@Sun.Com 	STRUCT_DECL(crypto_mechanism_info, mi);
10328313SDina.Nimeh@Sun.Com 	crypto_mech_name_t mech_name;
10338313SDina.Nimeh@Sun.Com 	crypto_mech_type_t mech_type;
10348313SDina.Nimeh@Sun.Com 	crypto_mechanism_info_t *mech_infos = NULL;
10358313SDina.Nimeh@Sun.Com 	uint_t num_mech_infos = 0;
10368313SDina.Nimeh@Sun.Com 	uint_t req_count;
10378313SDina.Nimeh@Sun.Com 	caddr_t u_minfos;
10388313SDina.Nimeh@Sun.Com 	ulong_t offset;
10398313SDina.Nimeh@Sun.Com 	int error = 0;
10408313SDina.Nimeh@Sun.Com 	int rv;
10418313SDina.Nimeh@Sun.Com 
10428313SDina.Nimeh@Sun.Com 	STRUCT_INIT(get_all_mech, mode);
10438313SDina.Nimeh@Sun.Com 	STRUCT_INIT(mi, mode);
10448313SDina.Nimeh@Sun.Com 
10458313SDina.Nimeh@Sun.Com 	if (copyin(arg, STRUCT_BUF(get_all_mech),
10468313SDina.Nimeh@Sun.Com 	    STRUCT_SIZE(get_all_mech)) != 0) {
10478313SDina.Nimeh@Sun.Com 		return (EFAULT);
10488313SDina.Nimeh@Sun.Com 	}
10498313SDina.Nimeh@Sun.Com 
10508313SDina.Nimeh@Sun.Com 	(void) strncpy(mech_name, STRUCT_FGET(get_all_mech, mi_mechanism_name),
10518313SDina.Nimeh@Sun.Com 	    CRYPTO_MAX_MECH_NAME);
10528313SDina.Nimeh@Sun.Com 	mech_type = crypto_mech2id(mech_name);
10538313SDina.Nimeh@Sun.Com 
10548313SDina.Nimeh@Sun.Com 	if (mech_type == CRYPTO_MECH_INVALID) {
10558313SDina.Nimeh@Sun.Com 		rv = CRYPTO_ARGUMENTS_BAD;
10568313SDina.Nimeh@Sun.Com 		goto out1;
10578313SDina.Nimeh@Sun.Com 	}
10588313SDina.Nimeh@Sun.Com 
10598313SDina.Nimeh@Sun.Com 	rv = crypto_get_all_mech_info(mech_type, &mech_infos, &num_mech_infos,
10608313SDina.Nimeh@Sun.Com 	    KM_SLEEP);
10618313SDina.Nimeh@Sun.Com 	if (rv != CRYPTO_SUCCESS) {
10628313SDina.Nimeh@Sun.Com 		goto out1;
10638313SDina.Nimeh@Sun.Com 	}
10648313SDina.Nimeh@Sun.Com 	/* rv is CRYPTO_SUCCESS at this point */
10658313SDina.Nimeh@Sun.Com 
10668313SDina.Nimeh@Sun.Com 	/* Number of entries caller thinks we have */
10678313SDina.Nimeh@Sun.Com 	req_count = STRUCT_FGET(get_all_mech, mi_count);
10688313SDina.Nimeh@Sun.Com 
10698313SDina.Nimeh@Sun.Com 	STRUCT_FSET(get_all_mech, mi_count, num_mech_infos);
10708313SDina.Nimeh@Sun.Com 
10718313SDina.Nimeh@Sun.Com 	/* check if buffer is too small */
10728313SDina.Nimeh@Sun.Com 	if (num_mech_infos > req_count) {
10738313SDina.Nimeh@Sun.Com 		rv = CRYPTO_BUFFER_TOO_SMALL;
10748313SDina.Nimeh@Sun.Com 	}
10758313SDina.Nimeh@Sun.Com 
10768313SDina.Nimeh@Sun.Com out1:
10778313SDina.Nimeh@Sun.Com 	STRUCT_FSET(get_all_mech, mi_return_value, rv);
10788313SDina.Nimeh@Sun.Com 
10798313SDina.Nimeh@Sun.Com 	/* copy the first part */
10808313SDina.Nimeh@Sun.Com 	if (copyout(STRUCT_BUF(get_all_mech), arg,
10818313SDina.Nimeh@Sun.Com 	    STRUCT_SIZE(get_all_mech)) != 0) {
10828313SDina.Nimeh@Sun.Com 		error = EFAULT;
10838313SDina.Nimeh@Sun.Com 	}
10848313SDina.Nimeh@Sun.Com 
10858313SDina.Nimeh@Sun.Com 	/*
10868313SDina.Nimeh@Sun.Com 	 * If only requesting number of entries, or there are no entries,
10878313SDina.Nimeh@Sun.Com 	 * or rv is not CRYPTO_SUCCESS due to buffer too small or some other
10888313SDina.Nimeh@Sun.Com 	 * crypto error, or an error occurred with copyout, stop here
10898313SDina.Nimeh@Sun.Com 	 */
10908313SDina.Nimeh@Sun.Com 	if (req_count == 0 || num_mech_infos == 0 || rv != CRYPTO_SUCCESS ||
10918313SDina.Nimeh@Sun.Com 	    error != 0) {
10928313SDina.Nimeh@Sun.Com 		goto out2;
10938313SDina.Nimeh@Sun.Com 	}
10948313SDina.Nimeh@Sun.Com 
10958313SDina.Nimeh@Sun.Com 	/* copyout mech_infos */
10968313SDina.Nimeh@Sun.Com 	offset = (ulong_t)STRUCT_FADDR(get_all_mech, mi_list);
10978313SDina.Nimeh@Sun.Com 	offset -= (ulong_t)STRUCT_BUF(get_all_mech);
10988313SDina.Nimeh@Sun.Com 
10998313SDina.Nimeh@Sun.Com 	u_minfos = kmem_alloc(num_mech_infos * STRUCT_SIZE(mi), KM_SLEEP);
11008313SDina.Nimeh@Sun.Com 	error = copyout_mechinfos(mode, arg + offset, num_mech_infos,
11018313SDina.Nimeh@Sun.Com 	    mech_infos, u_minfos);
11028313SDina.Nimeh@Sun.Com 	kmem_free(u_minfos, num_mech_infos * STRUCT_SIZE(mi));
11038313SDina.Nimeh@Sun.Com out2:
11048313SDina.Nimeh@Sun.Com 	if (mech_infos != NULL)
11058313SDina.Nimeh@Sun.Com 		crypto_free_all_mech_info(mech_infos, num_mech_infos);
11068313SDina.Nimeh@Sun.Com 	return (error);
11078313SDina.Nimeh@Sun.Com }
11088313SDina.Nimeh@Sun.Com 
11098313SDina.Nimeh@Sun.Com /*
11100Sstevel@tonic-gate  * Side-effects:
11110Sstevel@tonic-gate  *  1. This routine stores provider descriptor pointers in an array
11120Sstevel@tonic-gate  *     and increments each descriptor's reference count.  The array
11130Sstevel@tonic-gate  *     is stored in per-minor number storage.
11140Sstevel@tonic-gate  *  2. Destroys the old array and creates a new one every time
11150Sstevel@tonic-gate  *     this routine is called.
11160Sstevel@tonic-gate  */
11170Sstevel@tonic-gate int
11180Sstevel@tonic-gate crypto_get_provider_list(crypto_minor_t *cm, uint_t *count,
11190Sstevel@tonic-gate     crypto_provider_entry_t **array, boolean_t return_slot_list)
11200Sstevel@tonic-gate {
11210Sstevel@tonic-gate 	kcf_provider_desc_t **provider_array;
11220Sstevel@tonic-gate 	crypto_provider_entry_t *p = NULL;
11230Sstevel@tonic-gate 	uint_t provider_count;
11240Sstevel@tonic-gate 	int rval;
11250Sstevel@tonic-gate 	int i;
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 	/*
11280Sstevel@tonic-gate 	 * Take snapshot of provider table returning only HW entries
11290Sstevel@tonic-gate 	 * that are in a usable state. Also returns logical provider entries.
11300Sstevel@tonic-gate 	 */
11310Sstevel@tonic-gate 	rval =  kcf_get_slot_list(&provider_count, &provider_array, B_FALSE);
11320Sstevel@tonic-gate 	if (rval != CRYPTO_SUCCESS)
11330Sstevel@tonic-gate 		return (rval);
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 	/* allocate memory before taking cm->cm_lock */
11360Sstevel@tonic-gate 	if (return_slot_list) {
11370Sstevel@tonic-gate 		if (provider_count != 0) {
11380Sstevel@tonic-gate 			p = kmem_alloc(provider_count *
11390Sstevel@tonic-gate 			    sizeof (crypto_provider_entry_t), KM_SLEEP);
11400Sstevel@tonic-gate 			for (i = 0; i < provider_count; i++) {
11410Sstevel@tonic-gate 				p[i].pe_provider_id = i;
11420Sstevel@tonic-gate 				p[i].pe_mechanism_count =
11430Sstevel@tonic-gate 				    provider_array[i]->pd_mech_list_count;
11440Sstevel@tonic-gate 			}
11450Sstevel@tonic-gate 		}
11460Sstevel@tonic-gate 		*array = p;
11470Sstevel@tonic-gate 		*count = provider_count;
11480Sstevel@tonic-gate 	}
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 	/*
11510Sstevel@tonic-gate 	 * Free existing array of providers and replace with new list.
11520Sstevel@tonic-gate 	 */
11530Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
11540Sstevel@tonic-gate 	if (cm->cm_provider_array != NULL) {
11550Sstevel@tonic-gate 		ASSERT(cm->cm_provider_count > 0);
11560Sstevel@tonic-gate 		kcf_free_provider_tab(cm->cm_provider_count,
11570Sstevel@tonic-gate 		    cm->cm_provider_array);
11580Sstevel@tonic-gate 	}
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate 	cm->cm_provider_array = provider_array;
11610Sstevel@tonic-gate 	cm->cm_provider_count = provider_count;
11620Sstevel@tonic-gate 	mutex_exit(&cm->cm_lock);
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
11650Sstevel@tonic-gate }
11660Sstevel@tonic-gate 
11670Sstevel@tonic-gate /*
11680Sstevel@tonic-gate  * This ioctl returns an array of crypto_provider_entry_t entries.
11690Sstevel@tonic-gate  * This is how consumers learn which hardware providers are available.
11700Sstevel@tonic-gate  */
11710Sstevel@tonic-gate /* ARGSUSED */
11720Sstevel@tonic-gate static int
11730Sstevel@tonic-gate get_provider_list(dev_t dev, caddr_t arg, int mode, int *rval)
11740Sstevel@tonic-gate {
11750Sstevel@tonic-gate 	STRUCT_DECL(crypto_get_provider_list, get_list);
11760Sstevel@tonic-gate 	crypto_provider_entry_t *entries;
11770Sstevel@tonic-gate 	crypto_minor_t *cm;
11780Sstevel@tonic-gate 	size_t copyout_size;
11790Sstevel@tonic-gate 	uint_t req_count;
11800Sstevel@tonic-gate 	uint_t count;
11810Sstevel@tonic-gate 	ulong_t offset;
11820Sstevel@tonic-gate 	int rv;
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 	STRUCT_INIT(get_list, mode);
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
11870Sstevel@tonic-gate 		cmn_err(CE_WARN, "get_provider_list: failed holding minor");
11880Sstevel@tonic-gate 		return (ENXIO);
11890Sstevel@tonic-gate 	}
11900Sstevel@tonic-gate 
11910Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(get_list), STRUCT_SIZE(get_list)) != 0) {
11920Sstevel@tonic-gate 		crypto_release_minor(cm);
11930Sstevel@tonic-gate 		return (EFAULT);
11940Sstevel@tonic-gate 	}
11950Sstevel@tonic-gate 
11960Sstevel@tonic-gate 	rv = crypto_get_provider_list(cm, &count, &entries, RETURN_LIST);
11970Sstevel@tonic-gate 	if (rv != CRYPTO_SUCCESS) {
11980Sstevel@tonic-gate 		crypto_release_minor(cm);
11990Sstevel@tonic-gate 		STRUCT_FSET(get_list, pl_return_value, rv);
12000Sstevel@tonic-gate 		if (copyout(STRUCT_BUF(get_list), arg,
12010Sstevel@tonic-gate 		    STRUCT_SIZE(get_list)) != 0) {
12020Sstevel@tonic-gate 			return (EFAULT);
12030Sstevel@tonic-gate 		}
12040Sstevel@tonic-gate 		return (0);
12050Sstevel@tonic-gate 	}
12060Sstevel@tonic-gate 	crypto_release_minor(cm);
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate 	/* Number of slots caller thinks we have */
12090Sstevel@tonic-gate 	req_count = STRUCT_FGET(get_list, pl_count);
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate 	/* Check if only requesting number of slots */
12120Sstevel@tonic-gate 	if (req_count == 0) {
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate 		STRUCT_FSET(get_list, pl_count, count);
12150Sstevel@tonic-gate 		STRUCT_FSET(get_list, pl_return_value, CRYPTO_SUCCESS);
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate 		crypto_free_provider_list(entries, count);
12180Sstevel@tonic-gate 		if (copyout(STRUCT_BUF(get_list), arg,
12194632Smcpowers 		    STRUCT_SIZE(get_list)) != 0) {
12200Sstevel@tonic-gate 			return (EFAULT);
12210Sstevel@tonic-gate 		}
12220Sstevel@tonic-gate 		return (0);
12230Sstevel@tonic-gate 	}
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate 	/* check if buffer is too small */
12260Sstevel@tonic-gate 	req_count = STRUCT_FGET(get_list, pl_count);
12270Sstevel@tonic-gate 	if (count > req_count) {
12280Sstevel@tonic-gate 		STRUCT_FSET(get_list, pl_count, count);
12290Sstevel@tonic-gate 		STRUCT_FSET(get_list, pl_return_value, CRYPTO_BUFFER_TOO_SMALL);
12300Sstevel@tonic-gate 		crypto_free_provider_list(entries, count);
12310Sstevel@tonic-gate 		if (copyout(STRUCT_BUF(get_list), arg,
12320Sstevel@tonic-gate 		    STRUCT_SIZE(get_list)) != 0) {
12330Sstevel@tonic-gate 			return (EFAULT);
12340Sstevel@tonic-gate 		}
12350Sstevel@tonic-gate 		return (0);
12360Sstevel@tonic-gate 	}
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 	STRUCT_FSET(get_list, pl_count, count);
12390Sstevel@tonic-gate 	STRUCT_FSET(get_list, pl_return_value, CRYPTO_SUCCESS);
12400Sstevel@tonic-gate 
12410Sstevel@tonic-gate 	copyout_size = count * sizeof (crypto_provider_entry_t);
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 	/* copyout the first stuff */
12440Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(get_list), arg, STRUCT_SIZE(get_list)) != 0) {
12450Sstevel@tonic-gate 		crypto_free_provider_list(entries, count);
12460Sstevel@tonic-gate 		return (EFAULT);
12470Sstevel@tonic-gate 	}
12480Sstevel@tonic-gate 
12490Sstevel@tonic-gate 	if (count == 0) {
12500Sstevel@tonic-gate 		crypto_free_provider_list(entries, count);
12510Sstevel@tonic-gate 		return (0);
12520Sstevel@tonic-gate 	}
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate 	/* copyout entries */
12550Sstevel@tonic-gate 	offset = (ulong_t)STRUCT_FADDR(get_list, pl_list);
12560Sstevel@tonic-gate 	offset -= (ulong_t)STRUCT_BUF(get_list);
12570Sstevel@tonic-gate 	if (copyout(entries, arg + offset, copyout_size) != 0) {
12580Sstevel@tonic-gate 		crypto_free_provider_list(entries, count);
12590Sstevel@tonic-gate 		return (EFAULT);
12600Sstevel@tonic-gate 	}
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate 	crypto_free_provider_list(entries, count);
12630Sstevel@tonic-gate 	return (0);
12640Sstevel@tonic-gate }
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate static void
12670Sstevel@tonic-gate ext_to_provider_data(int mode, kcf_provider_desc_t *provider,
12680Sstevel@tonic-gate     crypto_provider_ext_info_t *ei, void *out)
12690Sstevel@tonic-gate {
12700Sstevel@tonic-gate 	STRUCT_DECL(crypto_provider_data, pd);
12710Sstevel@tonic-gate 	STRUCT_DECL(crypto_version, version);
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate 	STRUCT_INIT(pd, mode);
12740Sstevel@tonic-gate 	STRUCT_INIT(version, mode);
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate 	bcopy(provider->pd_description, STRUCT_FGET(pd, pd_prov_desc),
12770Sstevel@tonic-gate 	    CRYPTO_PROVIDER_DESCR_MAX_LEN);
12780Sstevel@tonic-gate 
12790Sstevel@tonic-gate 	bcopy(ei->ei_label, STRUCT_FGET(pd, pd_label), CRYPTO_EXT_SIZE_LABEL);
12800Sstevel@tonic-gate 	bcopy(ei->ei_manufacturerID, STRUCT_FGET(pd, pd_manufacturerID),
12810Sstevel@tonic-gate 	    CRYPTO_EXT_SIZE_MANUF);
12820Sstevel@tonic-gate 	bcopy(ei->ei_model, STRUCT_FGET(pd, pd_model), CRYPTO_EXT_SIZE_MODEL);
12830Sstevel@tonic-gate 	bcopy(ei->ei_serial_number, STRUCT_FGET(pd, pd_serial_number),
12840Sstevel@tonic-gate 	    CRYPTO_EXT_SIZE_SERIAL);
12850Sstevel@tonic-gate 	/*
12860Sstevel@tonic-gate 	 * We do not support ioctls for dual-function crypto operations yet.
12870Sstevel@tonic-gate 	 * So, we clear this flag as it might have been set by a provider.
12880Sstevel@tonic-gate 	 */
12890Sstevel@tonic-gate 	ei->ei_flags &= ~CRYPTO_EXTF_DUAL_CRYPTO_OPERATIONS;
12900Sstevel@tonic-gate 
12910Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_flags, ei->ei_flags);
12920Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_max_session_count, ei->ei_max_session_count);
12930Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_session_count, (int)CRYPTO_UNAVAILABLE_INFO);
12940Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_max_rw_session_count, ei->ei_max_session_count);
12950Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_rw_session_count, (int)CRYPTO_UNAVAILABLE_INFO);
12960Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_max_pin_len, ei->ei_max_pin_len);
12970Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_min_pin_len, ei->ei_min_pin_len);
12980Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_total_public_memory, ei->ei_total_public_memory);
12990Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_free_public_memory, ei->ei_free_public_memory);
13000Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_total_private_memory, ei->ei_total_private_memory);
13010Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_free_private_memory, ei->ei_free_private_memory);
13020Sstevel@tonic-gate 	STRUCT_FSET(version, cv_major, ei->ei_hardware_version.cv_major);
13030Sstevel@tonic-gate 	STRUCT_FSET(version, cv_minor, ei->ei_hardware_version.cv_minor);
13040Sstevel@tonic-gate 	bcopy(STRUCT_BUF(version), STRUCT_FADDR(pd, pd_hardware_version),
13050Sstevel@tonic-gate 	    STRUCT_SIZE(version));
13060Sstevel@tonic-gate 	bcopy(STRUCT_BUF(version), STRUCT_FADDR(pd, pd_firmware_version),
13070Sstevel@tonic-gate 	    STRUCT_SIZE(version));
13080Sstevel@tonic-gate 	bcopy(ei->ei_time, STRUCT_FGET(pd, pd_time), CRYPTO_EXT_SIZE_TIME);
13090Sstevel@tonic-gate 	bcopy(STRUCT_BUF(pd), out, STRUCT_SIZE(pd));
13100Sstevel@tonic-gate }
13110Sstevel@tonic-gate 
13120Sstevel@tonic-gate /*
13130Sstevel@tonic-gate  * Utility routine to construct a crypto_provider_ext_info structure. Some
13140Sstevel@tonic-gate  * of the fields are constructed from information in the provider structure.
13150Sstevel@tonic-gate  * The rest of the fields have default values. We need to do this for
13160Sstevel@tonic-gate  * providers which do not support crypto_provider_management_ops routines.
13170Sstevel@tonic-gate  */
13180Sstevel@tonic-gate static void
13190Sstevel@tonic-gate fabricate_ext_info(kcf_provider_desc_t *provider,
13200Sstevel@tonic-gate     crypto_provider_ext_info_t *ei)
13210Sstevel@tonic-gate {
13220Sstevel@tonic-gate 	/* empty label */
13230Sstevel@tonic-gate 	(void) memset(ei->ei_label, ' ', CRYPTO_EXT_SIZE_LABEL);
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 	(void) memset(ei->ei_manufacturerID, ' ', CRYPTO_EXT_SIZE_MANUF);
13260Sstevel@tonic-gate 	(void) strncpy((char *)ei->ei_manufacturerID, "Unknown", 7);
13270Sstevel@tonic-gate 
13280Sstevel@tonic-gate 	(void) memset(ei->ei_model, ' ', CRYPTO_EXT_SIZE_MODEL);
13290Sstevel@tonic-gate 	(void) strncpy((char *)ei->ei_model, "Unknown", 7);
13300Sstevel@tonic-gate 
13310Sstevel@tonic-gate 	(void) memset(ei->ei_serial_number, ' ', CRYPTO_EXT_SIZE_SERIAL);
13320Sstevel@tonic-gate 	(void) strncpy((char *)ei->ei_serial_number, "Unknown", 7);
13330Sstevel@tonic-gate 
13340Sstevel@tonic-gate 	if (KCF_PROV_RANDOM_OPS(provider) != NULL)
13350Sstevel@tonic-gate 		ei->ei_flags |= CRYPTO_EXTF_RNG;
13360Sstevel@tonic-gate 	if (KCF_PROV_DUAL_OPS(provider) != NULL)
13370Sstevel@tonic-gate 		ei->ei_flags |= CRYPTO_EXTF_DUAL_CRYPTO_OPERATIONS;
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 	ei->ei_max_session_count = CRYPTO_UNAVAILABLE_INFO;
13400Sstevel@tonic-gate 	ei->ei_max_pin_len = 0;
13410Sstevel@tonic-gate 	ei->ei_min_pin_len = 0;
13420Sstevel@tonic-gate 	ei->ei_total_public_memory = CRYPTO_UNAVAILABLE_INFO;
13430Sstevel@tonic-gate 	ei->ei_free_public_memory = CRYPTO_UNAVAILABLE_INFO;
13440Sstevel@tonic-gate 	ei->ei_total_private_memory = CRYPTO_UNAVAILABLE_INFO;
13450Sstevel@tonic-gate 	ei->ei_free_private_memory = CRYPTO_UNAVAILABLE_INFO;
13460Sstevel@tonic-gate 	ei->ei_hardware_version.cv_major = 1;
13470Sstevel@tonic-gate 	ei->ei_hardware_version.cv_minor = 0;
13480Sstevel@tonic-gate 	ei->ei_firmware_version.cv_major = 1;
13490Sstevel@tonic-gate 	ei->ei_firmware_version.cv_minor = 0;
13500Sstevel@tonic-gate }
13510Sstevel@tonic-gate 
13520Sstevel@tonic-gate /* ARGSUSED */
13530Sstevel@tonic-gate static int
13540Sstevel@tonic-gate get_provider_info(dev_t dev, caddr_t arg, int mode, int *rval)
13550Sstevel@tonic-gate {
13560Sstevel@tonic-gate 	STRUCT_DECL(crypto_get_provider_info, get_info);
13570Sstevel@tonic-gate 	crypto_minor_t *cm;
13580Sstevel@tonic-gate 	crypto_provider_id_t provider_id;
13590Sstevel@tonic-gate 	kcf_provider_desc_t *provider, *real_provider;
13600Sstevel@tonic-gate 	crypto_provider_ext_info_t *ext_info = NULL;
13610Sstevel@tonic-gate 	size_t need;
13620Sstevel@tonic-gate 	int error = 0;
13630Sstevel@tonic-gate 	int rv;
13640Sstevel@tonic-gate 	kcf_req_params_t params;
13650Sstevel@tonic-gate 
13660Sstevel@tonic-gate 	STRUCT_INIT(get_info, mode);
13670Sstevel@tonic-gate 
13680Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
13690Sstevel@tonic-gate 		cmn_err(CE_WARN, "get_provider_info: failed holding minor");
13700Sstevel@tonic-gate 		return (ENXIO);
13710Sstevel@tonic-gate 	}
13720Sstevel@tonic-gate 
13730Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(get_info), STRUCT_SIZE(get_info)) != 0) {
13740Sstevel@tonic-gate 		crypto_release_minor(cm);
13750Sstevel@tonic-gate 		return (EFAULT);
13760Sstevel@tonic-gate 	}
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate 	need = sizeof (crypto_provider_ext_info_t);
13793916Skrishna 	if ((rv = crypto_buffer_check(need)) != CRYPTO_SUCCESS) {
13800Sstevel@tonic-gate 		need = 0;
13810Sstevel@tonic-gate 		goto release_minor;
13820Sstevel@tonic-gate 	}
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate 	/* initialize provider_array */
13850Sstevel@tonic-gate 	if (cm->cm_provider_array == NULL) {
13860Sstevel@tonic-gate 		rv = crypto_get_provider_list(cm, NULL, NULL, DONT_RETURN_LIST);
13870Sstevel@tonic-gate 		if (rv != CRYPTO_SUCCESS) {
13880Sstevel@tonic-gate 			goto release_minor;
13890Sstevel@tonic-gate 		}
13900Sstevel@tonic-gate 	}
13910Sstevel@tonic-gate 
13920Sstevel@tonic-gate 	ext_info = kmem_zalloc(need, KM_SLEEP);
13930Sstevel@tonic-gate 
13940Sstevel@tonic-gate 	provider_id = STRUCT_FGET(get_info, gi_provider_id);
13950Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
13960Sstevel@tonic-gate 	/* index must be less than count of providers */
13970Sstevel@tonic-gate 	if (provider_id >= cm->cm_provider_count) {
13980Sstevel@tonic-gate 		mutex_exit(&cm->cm_lock);
13990Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
14000Sstevel@tonic-gate 		goto release_minor;
14010Sstevel@tonic-gate 	}
14020Sstevel@tonic-gate 
14030Sstevel@tonic-gate 	ASSERT(cm->cm_provider_array != NULL);
14040Sstevel@tonic-gate 	provider = cm->cm_provider_array[provider_id];
14050Sstevel@tonic-gate 	KCF_PROV_REFHOLD(provider);
14060Sstevel@tonic-gate 	mutex_exit(&cm->cm_lock);
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate 	(void) kcf_get_hardware_provider_nomech(
14090Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(provider_ops), CRYPTO_PROVIDER_OFFSET(ext_info),
1410904Smcpowers 	    CHECK_RESTRICT_FALSE, provider, &real_provider);
14110Sstevel@tonic-gate 
14120Sstevel@tonic-gate 	if (real_provider != NULL) {
14130Sstevel@tonic-gate 		ASSERT(real_provider == provider ||
14140Sstevel@tonic-gate 		    provider->pd_prov_type == CRYPTO_LOGICAL_PROVIDER);
14150Sstevel@tonic-gate 		KCF_WRAP_PROVMGMT_OPS_PARAMS(&params, KCF_OP_MGMT_EXTINFO,
14160Sstevel@tonic-gate 		    0, NULL, 0, NULL, 0, NULL, ext_info, provider);
14170Sstevel@tonic-gate 		rv = kcf_submit_request(real_provider, NULL, NULL, &params,
14180Sstevel@tonic-gate 		    B_FALSE);
14190Sstevel@tonic-gate 		ASSERT(rv != CRYPTO_NOT_SUPPORTED);
1420904Smcpowers 		KCF_PROV_REFRELE(real_provider);
14210Sstevel@tonic-gate 	} else {
14220Sstevel@tonic-gate 		/* do the best we can */
14230Sstevel@tonic-gate 		fabricate_ext_info(provider, ext_info);
14240Sstevel@tonic-gate 		rv = CRYPTO_SUCCESS;
14250Sstevel@tonic-gate 	}
14260Sstevel@tonic-gate 	KCF_PROV_REFRELE(provider);
14270Sstevel@tonic-gate 
14280Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
14290Sstevel@tonic-gate 		ext_to_provider_data(mode, provider, ext_info,
14300Sstevel@tonic-gate 		    STRUCT_FADDR(get_info, gi_provider_data));
14310Sstevel@tonic-gate 	}
14320Sstevel@tonic-gate 
14330Sstevel@tonic-gate release_minor:
14346424Skrishna 	CRYPTO_DECREMENT_RCTL(need);
14350Sstevel@tonic-gate 	crypto_release_minor(cm);
14360Sstevel@tonic-gate 
14370Sstevel@tonic-gate 	if (ext_info != NULL)
14380Sstevel@tonic-gate 		kmem_free(ext_info, sizeof (crypto_provider_ext_info_t));
14390Sstevel@tonic-gate 
14400Sstevel@tonic-gate 	if (error != 0)
14410Sstevel@tonic-gate 		return (error);
14420Sstevel@tonic-gate 
14430Sstevel@tonic-gate 	STRUCT_FSET(get_info, gi_return_value, rv);
14440Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(get_info), arg, STRUCT_SIZE(get_info)) != 0) {
14450Sstevel@tonic-gate 		return (EFAULT);
14460Sstevel@tonic-gate 	}
14470Sstevel@tonic-gate 	return (0);
14480Sstevel@tonic-gate }
14490Sstevel@tonic-gate 
14500Sstevel@tonic-gate /*
14510Sstevel@tonic-gate  * This ioctl returns an array of crypto_mech_name_t entries.
14520Sstevel@tonic-gate  * This is how consumers learn which mechanisms are permitted
14530Sstevel@tonic-gate  * by a provider.
14540Sstevel@tonic-gate  */
14550Sstevel@tonic-gate /* ARGSUSED */
14560Sstevel@tonic-gate static int
14570Sstevel@tonic-gate get_provider_mechanisms(dev_t dev, caddr_t arg, int mode, int *rval)
14580Sstevel@tonic-gate {
14590Sstevel@tonic-gate 	STRUCT_DECL(crypto_get_provider_mechanisms, get_mechanisms);
14600Sstevel@tonic-gate 	crypto_mech_name_t *entries;
14610Sstevel@tonic-gate 	crypto_minor_t *cm;
14620Sstevel@tonic-gate 	size_t copyout_size;
14630Sstevel@tonic-gate 	uint_t req_count;
14640Sstevel@tonic-gate 	uint_t count;
14650Sstevel@tonic-gate 	ulong_t offset;
14660Sstevel@tonic-gate 	int err;
14670Sstevel@tonic-gate 
14680Sstevel@tonic-gate 	STRUCT_INIT(get_mechanisms, mode);
14690Sstevel@tonic-gate 
14700Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
14710Sstevel@tonic-gate 		cmn_err(CE_WARN,
14720Sstevel@tonic-gate 		    "get_provider_mechanisms: failed holding minor");
14730Sstevel@tonic-gate 		return (ENXIO);
14740Sstevel@tonic-gate 	}
14750Sstevel@tonic-gate 
14760Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(get_mechanisms),
14770Sstevel@tonic-gate 	    STRUCT_SIZE(get_mechanisms)) != 0) {
14780Sstevel@tonic-gate 		crypto_release_minor(cm);
14790Sstevel@tonic-gate 		return (EFAULT);
14800Sstevel@tonic-gate 	}
14810Sstevel@tonic-gate 
14820Sstevel@tonic-gate 	/* get array of mechanisms from the core module */
14830Sstevel@tonic-gate 	if ((err = crypto_get_provider_mechanisms(cm,
14840Sstevel@tonic-gate 	    STRUCT_FGET(get_mechanisms, pm_provider_id),
14850Sstevel@tonic-gate 	    &count, &entries)) != 0) {
14860Sstevel@tonic-gate 		crypto_release_minor(cm);
14870Sstevel@tonic-gate 		STRUCT_FSET(get_mechanisms, pm_return_value, err);
14880Sstevel@tonic-gate 		if (copyout(STRUCT_BUF(get_mechanisms), arg,
14890Sstevel@tonic-gate 		    STRUCT_SIZE(get_mechanisms)) != 0) {
14900Sstevel@tonic-gate 			return (EFAULT);
14910Sstevel@tonic-gate 		}
14920Sstevel@tonic-gate 		return (0);
14930Sstevel@tonic-gate 	}
14940Sstevel@tonic-gate 	crypto_release_minor(cm);
14950Sstevel@tonic-gate 	/* Number of mechs caller thinks we have */
14960Sstevel@tonic-gate 	req_count = STRUCT_FGET(get_mechanisms, pm_count);
14970Sstevel@tonic-gate 
14980Sstevel@tonic-gate 	/* Check if caller is just requesting a count of mechanisms */
14990Sstevel@tonic-gate 	if (req_count == 0) {
15000Sstevel@tonic-gate 		STRUCT_FSET(get_mechanisms, pm_count, count);
15010Sstevel@tonic-gate 		STRUCT_FSET(get_mechanisms, pm_return_value, CRYPTO_SUCCESS);
15020Sstevel@tonic-gate 
15030Sstevel@tonic-gate 		crypto_free_mech_list(entries, count);
15040Sstevel@tonic-gate 		if (copyout(STRUCT_BUF(get_mechanisms), arg,
15050Sstevel@tonic-gate 		    STRUCT_SIZE(get_mechanisms)) != 0) {
15060Sstevel@tonic-gate 			return (EFAULT);
15070Sstevel@tonic-gate 		}
15080Sstevel@tonic-gate 		return (0);
15090Sstevel@tonic-gate 	}
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate 	/* check if buffer is too small */
15120Sstevel@tonic-gate 	if (count > req_count) {
15130Sstevel@tonic-gate 		STRUCT_FSET(get_mechanisms, pm_count, count);
15140Sstevel@tonic-gate 		STRUCT_FSET(get_mechanisms, pm_return_value,
15150Sstevel@tonic-gate 		    CRYPTO_BUFFER_TOO_SMALL);
15160Sstevel@tonic-gate 		crypto_free_mech_list(entries, count);
15170Sstevel@tonic-gate 		if (copyout(STRUCT_BUF(get_mechanisms), arg,
15180Sstevel@tonic-gate 		    STRUCT_SIZE(get_mechanisms)) != 0) {
15190Sstevel@tonic-gate 			return (EFAULT);
15200Sstevel@tonic-gate 		}
15210Sstevel@tonic-gate 		return (0);
15220Sstevel@tonic-gate 	}
15230Sstevel@tonic-gate 
15240Sstevel@tonic-gate 	STRUCT_FSET(get_mechanisms, pm_count, count);
15250Sstevel@tonic-gate 	STRUCT_FSET(get_mechanisms, pm_return_value, CRYPTO_SUCCESS);
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate 	copyout_size = count * sizeof (crypto_mech_name_t);
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate 	/* copyout the first stuff */
15300Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(get_mechanisms), arg,
15310Sstevel@tonic-gate 	    STRUCT_SIZE(get_mechanisms)) != 0) {
15320Sstevel@tonic-gate 		crypto_free_mech_list(entries, count);
15330Sstevel@tonic-gate 		return (EFAULT);
15340Sstevel@tonic-gate 	}
15350Sstevel@tonic-gate 
15360Sstevel@tonic-gate 	if (count == 0) {
15370Sstevel@tonic-gate 		return (0);
15380Sstevel@tonic-gate 	}
15390Sstevel@tonic-gate 
15400Sstevel@tonic-gate 	/* copyout entries */
15410Sstevel@tonic-gate 	offset = (ulong_t)STRUCT_FADDR(get_mechanisms, pm_list);
15420Sstevel@tonic-gate 	offset -= (ulong_t)STRUCT_BUF(get_mechanisms);
15430Sstevel@tonic-gate 	if (copyout(entries, arg + offset, copyout_size) != 0) {
15440Sstevel@tonic-gate 		crypto_free_mech_list(entries, count);
15450Sstevel@tonic-gate 		return (EFAULT);
15460Sstevel@tonic-gate 	}
15470Sstevel@tonic-gate 
15480Sstevel@tonic-gate 	crypto_free_mech_list(entries, count);
15490Sstevel@tonic-gate 	return (0);
15500Sstevel@tonic-gate }
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate /*
15530Sstevel@tonic-gate  * This ioctl returns information about a provider's mechanism.
15540Sstevel@tonic-gate  */
15550Sstevel@tonic-gate /* ARGSUSED */
15560Sstevel@tonic-gate static int
15570Sstevel@tonic-gate get_provider_mechanism_info(dev_t dev, caddr_t arg, int mode, int *rval)
15580Sstevel@tonic-gate {
15590Sstevel@tonic-gate 	crypto_get_provider_mechanism_info_t mechanism_info;
15600Sstevel@tonic-gate 	crypto_minor_t *cm;
15610Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
15620Sstevel@tonic-gate 	crypto_mech_info_t *mi = NULL;
15630Sstevel@tonic-gate 	int rv = CRYPTO_SUCCESS;
15640Sstevel@tonic-gate 	int i;
15650Sstevel@tonic-gate 
15660Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
15670Sstevel@tonic-gate 		cmn_err(CE_WARN,
15680Sstevel@tonic-gate 		    "get_provider_mechanism_info: failed holding minor");
15690Sstevel@tonic-gate 		return (ENXIO);
15700Sstevel@tonic-gate 	}
15710Sstevel@tonic-gate 
15720Sstevel@tonic-gate 	if (copyin(arg, &mechanism_info, sizeof (mechanism_info)) != 0) {
15730Sstevel@tonic-gate 		crypto_release_minor(cm);
15740Sstevel@tonic-gate 		return (EFAULT);
15750Sstevel@tonic-gate 	}
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate 	/* initialize provider table */
15780Sstevel@tonic-gate 	if (cm->cm_provider_array == NULL) {
15790Sstevel@tonic-gate 		rv = crypto_get_provider_list(cm, NULL, NULL, DONT_RETURN_LIST);
15800Sstevel@tonic-gate 		if (rv != CRYPTO_SUCCESS) {
15810Sstevel@tonic-gate 			mutex_enter(&cm->cm_lock);
15820Sstevel@tonic-gate 			goto fail;
15830Sstevel@tonic-gate 		}
15840Sstevel@tonic-gate 	}
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate 	/*
15870Sstevel@tonic-gate 	 * Provider ID must be less than the count of providers
15880Sstevel@tonic-gate 	 * obtained by calling get_provider_list().
15890Sstevel@tonic-gate 	 */
15900Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
15910Sstevel@tonic-gate 	if (mechanism_info.mi_provider_id >= cm->cm_provider_count) {
15920Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
15930Sstevel@tonic-gate 		goto fail;
15940Sstevel@tonic-gate 	}
15950Sstevel@tonic-gate 
15960Sstevel@tonic-gate 	pd = cm->cm_provider_array[mechanism_info.mi_provider_id];
15970Sstevel@tonic-gate 
15980Sstevel@tonic-gate 	for (i = 0; i < pd->pd_mech_list_count; i++) {
15990Sstevel@tonic-gate 		if (strncmp(pd->pd_mechanisms[i].cm_mech_name,
16000Sstevel@tonic-gate 		    mechanism_info.mi_mechanism_name,
16010Sstevel@tonic-gate 		    CRYPTO_MAX_MECH_NAME) == 0) {
16020Sstevel@tonic-gate 			mi = &pd->pd_mechanisms[i];
16030Sstevel@tonic-gate 		}
16040Sstevel@tonic-gate 	}
16050Sstevel@tonic-gate 
16060Sstevel@tonic-gate 	if (mi == NULL) {
16070Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
16080Sstevel@tonic-gate 		goto fail;
16090Sstevel@tonic-gate 	}
16100Sstevel@tonic-gate 
16110Sstevel@tonic-gate 	mechanism_info.mi_min_key_size = mi->cm_min_key_length;
16120Sstevel@tonic-gate 	mechanism_info.mi_max_key_size = mi->cm_max_key_length;
16130Sstevel@tonic-gate 	mechanism_info.mi_flags = mi->cm_func_group_mask;
16140Sstevel@tonic-gate 
16150Sstevel@tonic-gate fail:
16160Sstevel@tonic-gate 	mutex_exit(&cm->cm_lock);
16170Sstevel@tonic-gate 	crypto_release_minor(cm);
16180Sstevel@tonic-gate 	mechanism_info.mi_return_value = rv;
16190Sstevel@tonic-gate 	if (copyout(&mechanism_info, arg, sizeof (mechanism_info)) != 0) {
16200Sstevel@tonic-gate 		return (EFAULT);
16210Sstevel@tonic-gate 	}
16220Sstevel@tonic-gate 
16230Sstevel@tonic-gate 	return (0);
16240Sstevel@tonic-gate }
16250Sstevel@tonic-gate 
16260Sstevel@tonic-gate /*
16270Sstevel@tonic-gate  * Every open of /dev/crypto multiplexes all PKCS#11 sessions across
16280Sstevel@tonic-gate  * a single session to each provider. Calls to open and close session
16290Sstevel@tonic-gate  * are not made to providers that do not support sessions. For these
16300Sstevel@tonic-gate  * providers, a session number of 0 is passed during subsequent operations,
16310Sstevel@tonic-gate  * and it is ignored by the provider.
16320Sstevel@tonic-gate  */
16330Sstevel@tonic-gate static int
16340Sstevel@tonic-gate crypto_get_provider_session(crypto_minor_t *cm,
16350Sstevel@tonic-gate     crypto_provider_id_t provider_index, crypto_provider_session_t **output_ps)
16360Sstevel@tonic-gate {
16370Sstevel@tonic-gate 	kcf_provider_desc_t *pd, *real_provider;
16380Sstevel@tonic-gate 	kcf_req_params_t params;
16390Sstevel@tonic-gate 	crypto_provider_session_t *ps, *new_ps;
16400Sstevel@tonic-gate 	crypto_session_id_t provider_session_id = 0;
16410Sstevel@tonic-gate 	int rv;
16420Sstevel@tonic-gate 
16430Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cm->cm_lock));
16440Sstevel@tonic-gate 
16450Sstevel@tonic-gate 	/* pd may be a logical provider */
16460Sstevel@tonic-gate 	pd = cm->cm_provider_array[provider_index];
16470Sstevel@tonic-gate 
16480Sstevel@tonic-gate again:
16490Sstevel@tonic-gate 	/*
16500Sstevel@tonic-gate 	 * Check if there is already a session to the provider.
16510Sstevel@tonic-gate 	 * Sessions may be to a logical provider or a real provider.
16520Sstevel@tonic-gate 	 */
16530Sstevel@tonic-gate 	for (ps = cm->cm_provider_session; ps != NULL; ps = ps->ps_next) {
16540Sstevel@tonic-gate 		if (ps->ps_provider == pd)
16550Sstevel@tonic-gate 			break;
16560Sstevel@tonic-gate 	}
16570Sstevel@tonic-gate 
16580Sstevel@tonic-gate 	/* found existing session */
16590Sstevel@tonic-gate 	if (ps != NULL) {
16600Sstevel@tonic-gate 		ps->ps_refcnt++;
16610Sstevel@tonic-gate 		*output_ps = ps;
16620Sstevel@tonic-gate 		return (CRYPTO_SUCCESS);
16630Sstevel@tonic-gate 	}
16640Sstevel@tonic-gate 	mutex_exit(&cm->cm_lock);
16650Sstevel@tonic-gate 
16660Sstevel@tonic-gate 	/* find a hardware provider that supports session ops */
16670Sstevel@tonic-gate 	(void) kcf_get_hardware_provider_nomech(CRYPTO_OPS_OFFSET(session_ops),
1668904Smcpowers 	    CRYPTO_SESSION_OFFSET(session_open), CHECK_RESTRICT_FALSE,
1669904Smcpowers 	    pd, &real_provider);
16700Sstevel@tonic-gate 
16710Sstevel@tonic-gate 	if (real_provider != NULL) {
16720Sstevel@tonic-gate 		ASSERT(real_provider == pd ||
16730Sstevel@tonic-gate 		    pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER);
16740Sstevel@tonic-gate 		/* open session to provider */
16750Sstevel@tonic-gate 		KCF_WRAP_SESSION_OPS_PARAMS(&params, KCF_OP_SESSION_OPEN,
16760Sstevel@tonic-gate 		    &provider_session_id, 0, CRYPTO_USER, NULL, 0, pd);
16770Sstevel@tonic-gate 		rv = kcf_submit_request(real_provider, NULL, NULL, &params,
16780Sstevel@tonic-gate 		    B_FALSE);
16790Sstevel@tonic-gate 		if (rv != CRYPTO_SUCCESS) {
16800Sstevel@tonic-gate 			mutex_enter(&cm->cm_lock);
1681904Smcpowers 			KCF_PROV_REFRELE(real_provider);
16820Sstevel@tonic-gate 			return (rv);
16830Sstevel@tonic-gate 		}
16840Sstevel@tonic-gate 	}
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate 	/* allocate crypto_provider_session structure */
16870Sstevel@tonic-gate 	new_ps = kmem_zalloc(sizeof (crypto_provider_session_t), KM_SLEEP);
16880Sstevel@tonic-gate 
16890Sstevel@tonic-gate 	/*
16900Sstevel@tonic-gate 	 * Check if someone opened a session to the provider
16910Sstevel@tonic-gate 	 * while we dropped the lock.
16920Sstevel@tonic-gate 	 */
16930Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
16940Sstevel@tonic-gate 	for (ps = cm->cm_provider_session; ps != NULL; ps = ps->ps_next) {
16950Sstevel@tonic-gate 		if (ps->ps_provider == pd) {
16960Sstevel@tonic-gate 			mutex_exit(&cm->cm_lock);
16970Sstevel@tonic-gate 			kmem_free(new_ps, sizeof (crypto_provider_session_t));
16980Sstevel@tonic-gate 			if (real_provider != NULL) {
16990Sstevel@tonic-gate 				KCF_WRAP_SESSION_OPS_PARAMS(&params,
17000Sstevel@tonic-gate 				    KCF_OP_SESSION_CLOSE, NULL,
17010Sstevel@tonic-gate 				    provider_session_id, CRYPTO_USER, NULL, 0,
17020Sstevel@tonic-gate 				    pd);
17030Sstevel@tonic-gate 				(void) kcf_submit_request(real_provider, NULL,
17040Sstevel@tonic-gate 				    NULL, &params, B_FALSE);
1705904Smcpowers 				KCF_PROV_REFRELE(real_provider);
17060Sstevel@tonic-gate 			}
17070Sstevel@tonic-gate 			mutex_enter(&cm->cm_lock);
17080Sstevel@tonic-gate 			goto again;
17090Sstevel@tonic-gate 
17100Sstevel@tonic-gate 		}
17110Sstevel@tonic-gate 	}
17120Sstevel@tonic-gate 
17130Sstevel@tonic-gate 	/* increment refcnt and attach to crypto_minor structure */
17140Sstevel@tonic-gate 	new_ps->ps_session = provider_session_id;
17150Sstevel@tonic-gate 	new_ps->ps_refcnt = 1;
17160Sstevel@tonic-gate 	KCF_PROV_REFHOLD(pd);
17170Sstevel@tonic-gate 	new_ps->ps_provider = pd;
17180Sstevel@tonic-gate 	if (real_provider != NULL) {
17190Sstevel@tonic-gate 		new_ps->ps_real_provider = real_provider;
17200Sstevel@tonic-gate 	}
17210Sstevel@tonic-gate 	new_ps->ps_next = cm->cm_provider_session;
17220Sstevel@tonic-gate 	cm->cm_provider_session = new_ps;
17230Sstevel@tonic-gate 
17240Sstevel@tonic-gate 	*output_ps = new_ps;
17250Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
17260Sstevel@tonic-gate }
17270Sstevel@tonic-gate 
17280Sstevel@tonic-gate /*
17290Sstevel@tonic-gate  * Release a provider session.
17300Sstevel@tonic-gate  * If the reference count goes to zero, then close the session
17310Sstevel@tonic-gate  * to the provider.
17320Sstevel@tonic-gate  */
17330Sstevel@tonic-gate static void
17340Sstevel@tonic-gate crypto_release_provider_session(crypto_minor_t *cm,
17350Sstevel@tonic-gate     crypto_provider_session_t *provider_session)
17360Sstevel@tonic-gate {
17370Sstevel@tonic-gate 	kcf_req_params_t params;
17380Sstevel@tonic-gate 	crypto_provider_session_t *ps = NULL, **prev;
17390Sstevel@tonic-gate 
17400Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cm->cm_lock));
17410Sstevel@tonic-gate 
17420Sstevel@tonic-gate 	/* verify that provider_session is valid */
17430Sstevel@tonic-gate 	for (ps = cm->cm_provider_session, prev = &cm->cm_provider_session;
17440Sstevel@tonic-gate 	    ps != NULL; prev = &ps->ps_next, ps = ps->ps_next) {
17450Sstevel@tonic-gate 		if (ps == provider_session) {
17460Sstevel@tonic-gate 			break;
17470Sstevel@tonic-gate 		}
17480Sstevel@tonic-gate 	}
17490Sstevel@tonic-gate 
17500Sstevel@tonic-gate 	if (ps == NULL)
17510Sstevel@tonic-gate 		return;
17520Sstevel@tonic-gate 
17530Sstevel@tonic-gate 	ps->ps_refcnt--;
17540Sstevel@tonic-gate 
17550Sstevel@tonic-gate 	if (ps->ps_refcnt > 0)
17560Sstevel@tonic-gate 		return;
17570Sstevel@tonic-gate 
17580Sstevel@tonic-gate 	if (ps->ps_real_provider != NULL) {
17590Sstevel@tonic-gate 		/* close session with provider */
17600Sstevel@tonic-gate 		KCF_WRAP_SESSION_OPS_PARAMS(&params, KCF_OP_SESSION_CLOSE, NULL,
17610Sstevel@tonic-gate 		    ps->ps_session, CRYPTO_USER, NULL, 0, ps->ps_provider);
17620Sstevel@tonic-gate 		(void) kcf_submit_request(ps->ps_real_provider,
17630Sstevel@tonic-gate 		    NULL, NULL, &params, B_FALSE);
17640Sstevel@tonic-gate 		KCF_PROV_REFRELE(ps->ps_real_provider);
17650Sstevel@tonic-gate 	}
17660Sstevel@tonic-gate 	KCF_PROV_REFRELE(ps->ps_provider);
17670Sstevel@tonic-gate 	*prev = ps->ps_next;
17680Sstevel@tonic-gate 	kmem_free(ps, sizeof (*ps));
17690Sstevel@tonic-gate }
17700Sstevel@tonic-gate 
17710Sstevel@tonic-gate static int
17720Sstevel@tonic-gate grow_session_table(crypto_minor_t *cm)
17730Sstevel@tonic-gate {
17740Sstevel@tonic-gate 	crypto_session_data_t **session_table;
17750Sstevel@tonic-gate 	crypto_session_data_t **new;
17760Sstevel@tonic-gate 	uint_t session_table_count;
17770Sstevel@tonic-gate 	uint_t need;
17780Sstevel@tonic-gate 	size_t current_allocation;
17790Sstevel@tonic-gate 	size_t new_allocation;
17803916Skrishna 	int rv;
17810Sstevel@tonic-gate 
17820Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cm->cm_lock));
17830Sstevel@tonic-gate 
17840Sstevel@tonic-gate 	session_table_count = cm->cm_session_table_count;
17850Sstevel@tonic-gate 	session_table = cm->cm_session_table;
17860Sstevel@tonic-gate 	need = session_table_count + CRYPTO_SESSION_CHUNK;
17870Sstevel@tonic-gate 
17880Sstevel@tonic-gate 	current_allocation = session_table_count * sizeof (void *);
17890Sstevel@tonic-gate 	new_allocation = need * sizeof (void *);
17900Sstevel@tonic-gate 
17910Sstevel@tonic-gate 	/*
17920Sstevel@tonic-gate 	 * Memory needed to grow the session table is checked
17930Sstevel@tonic-gate 	 * against the project.max-crypto-memory resource control.
17940Sstevel@tonic-gate 	 */
17953916Skrishna 	if ((rv = crypto_buffer_check(new_allocation - current_allocation)) !=
17963916Skrishna 	    CRYPTO_SUCCESS) {
17973916Skrishna 		return (rv);
17983916Skrishna 	}
17990Sstevel@tonic-gate 
18000Sstevel@tonic-gate 	/* drop lock while we allocate memory */
18010Sstevel@tonic-gate 	mutex_exit(&cm->cm_lock);
18020Sstevel@tonic-gate 	new = kmem_zalloc(new_allocation, KM_SLEEP);
18030Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
18040Sstevel@tonic-gate 
18050Sstevel@tonic-gate 	/* check if another thread increased the table size */
18060Sstevel@tonic-gate 	if (session_table_count != cm->cm_session_table_count) {
18070Sstevel@tonic-gate 		kmem_free(new, new_allocation);
18080Sstevel@tonic-gate 		return (CRYPTO_SUCCESS);
18090Sstevel@tonic-gate 	}
18100Sstevel@tonic-gate 
18110Sstevel@tonic-gate 	bcopy(session_table, new, current_allocation);
18120Sstevel@tonic-gate 	kmem_free(session_table, current_allocation);
18130Sstevel@tonic-gate 	cm->cm_session_table = new;
18140Sstevel@tonic-gate 	cm->cm_session_table_count += CRYPTO_SESSION_CHUNK;
18150Sstevel@tonic-gate 
18160Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
18170Sstevel@tonic-gate }
18180Sstevel@tonic-gate 
18190Sstevel@tonic-gate /*
18200Sstevel@tonic-gate  * Find unused entry in session table and return it's index.
18210Sstevel@tonic-gate  * Initialize session table entry.
18220Sstevel@tonic-gate  */
18230Sstevel@tonic-gate /* ARGSUSED */
18240Sstevel@tonic-gate static int
18250Sstevel@tonic-gate crypto_open_session(dev_t dev, uint_t flags, crypto_session_id_t *session_index,
18260Sstevel@tonic-gate     crypto_provider_id_t provider_id)
18270Sstevel@tonic-gate {
18280Sstevel@tonic-gate 	crypto_session_data_t **session_table;
18290Sstevel@tonic-gate 	crypto_session_data_t *sp;
18300Sstevel@tonic-gate 	crypto_minor_t *cm;
18310Sstevel@tonic-gate 	uint_t session_table_count;
18320Sstevel@tonic-gate 	uint_t i;
18330Sstevel@tonic-gate 	int rv;
18340Sstevel@tonic-gate 	crypto_provider_session_t *ps;
18350Sstevel@tonic-gate 	kcf_provider_desc_t *provider;
18360Sstevel@tonic-gate 
18370Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
18380Sstevel@tonic-gate 		cmn_err(CE_WARN, "crypto_open_session: failed holding minor");
18390Sstevel@tonic-gate 		return (CRYPTO_FAILED);
18400Sstevel@tonic-gate 	}
18410Sstevel@tonic-gate 
18420Sstevel@tonic-gate 	/* initialize provider_array */
18430Sstevel@tonic-gate 	if (cm->cm_provider_array == NULL) {
18440Sstevel@tonic-gate 		rv = crypto_get_provider_list(cm, NULL, NULL, DONT_RETURN_LIST);
18450Sstevel@tonic-gate 		if (rv != 0) {
18460Sstevel@tonic-gate 			crypto_release_minor(cm);
18470Sstevel@tonic-gate 			return (rv);
18480Sstevel@tonic-gate 		}
18490Sstevel@tonic-gate 	}
18500Sstevel@tonic-gate 
18510Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
18520Sstevel@tonic-gate 	/* index must be less than count of providers */
18530Sstevel@tonic-gate 	if (provider_id >= cm->cm_provider_count) {
18540Sstevel@tonic-gate 		mutex_exit(&cm->cm_lock);
18550Sstevel@tonic-gate 		crypto_release_minor(cm);
18560Sstevel@tonic-gate 		return (CRYPTO_INVALID_PROVIDER_ID);
18570Sstevel@tonic-gate 	}
18580Sstevel@tonic-gate 	ASSERT(cm->cm_provider_array != NULL);
18590Sstevel@tonic-gate 
18600Sstevel@tonic-gate 	rv = crypto_get_provider_session(cm, provider_id, &ps);
18610Sstevel@tonic-gate 	if (rv != CRYPTO_SUCCESS) {
18620Sstevel@tonic-gate 		mutex_exit(&cm->cm_lock);
18630Sstevel@tonic-gate 		crypto_release_minor(cm);
18640Sstevel@tonic-gate 		return (rv);
18650Sstevel@tonic-gate 	}
18660Sstevel@tonic-gate 	provider = cm->cm_provider_array[provider_id];
18670Sstevel@tonic-gate 
18680Sstevel@tonic-gate again:
18690Sstevel@tonic-gate 	session_table_count = cm->cm_session_table_count;
18700Sstevel@tonic-gate 	session_table = cm->cm_session_table;
18710Sstevel@tonic-gate 
18720Sstevel@tonic-gate 	/* session handles start with 1 */
18730Sstevel@tonic-gate 	for (i = 1; i < session_table_count; i++) {
18740Sstevel@tonic-gate 		if (session_table[i] == NULL)
18750Sstevel@tonic-gate 			break;
18760Sstevel@tonic-gate 	}
18770Sstevel@tonic-gate 
18780Sstevel@tonic-gate 	if (i == session_table_count || session_table_count == 0) {
18790Sstevel@tonic-gate 		if ((rv = grow_session_table(cm)) != CRYPTO_SUCCESS) {
18800Sstevel@tonic-gate 			crypto_release_provider_session(cm, ps);
18810Sstevel@tonic-gate 			mutex_exit(&cm->cm_lock);
18820Sstevel@tonic-gate 			crypto_release_minor(cm);
18830Sstevel@tonic-gate 			return (rv);
18840Sstevel@tonic-gate 		}
18850Sstevel@tonic-gate 		goto again;
18860Sstevel@tonic-gate 	}
18870Sstevel@tonic-gate 
18880Sstevel@tonic-gate 	sp = kmem_cache_alloc(crypto_session_cache, KM_SLEEP);
18890Sstevel@tonic-gate 	sp->sd_flags = 0;
18900Sstevel@tonic-gate 	sp->sd_find_init_cookie = NULL;
18910Sstevel@tonic-gate 	sp->sd_digest_ctx = NULL;
18920Sstevel@tonic-gate 	sp->sd_encr_ctx = NULL;
18930Sstevel@tonic-gate 	sp->sd_decr_ctx = NULL;
18940Sstevel@tonic-gate 	sp->sd_sign_ctx = NULL;
18950Sstevel@tonic-gate 	sp->sd_verify_ctx = NULL;
18960Sstevel@tonic-gate 	sp->sd_sign_recover_ctx = NULL;
18970Sstevel@tonic-gate 	sp->sd_verify_recover_ctx = NULL;
18980Sstevel@tonic-gate 	mutex_init(&sp->sd_lock, NULL, MUTEX_DRIVER, NULL);
18990Sstevel@tonic-gate 	cv_init(&sp->sd_cv, NULL, CV_DRIVER, NULL);
19000Sstevel@tonic-gate 	KCF_PROV_REFHOLD(provider);
19010Sstevel@tonic-gate 	sp->sd_provider = provider;
19020Sstevel@tonic-gate 	sp->sd_provider_session = ps;
19036424Skrishna 
19046424Skrishna 	/* See the comment for CRYPTO_PRE_APPROVED_LIMIT. */
19056424Skrishna 	if ((rv = crypto_buffer_check(crypto_pre_approved_limit)) !=
19066424Skrishna 	    CRYPTO_SUCCESS) {
19076424Skrishna 		sp->sd_pre_approved_amount = 0;
19086424Skrishna 	} else {
19096424Skrishna 		sp->sd_pre_approved_amount = crypto_pre_approved_limit;
19106424Skrishna 	}
19116424Skrishna 
19120Sstevel@tonic-gate 	cm->cm_session_table[i] = sp;
19130Sstevel@tonic-gate 	mutex_exit(&cm->cm_lock);
19140Sstevel@tonic-gate 	crypto_release_minor(cm);
19150Sstevel@tonic-gate 	*session_index = i;
19160Sstevel@tonic-gate 
19170Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
19180Sstevel@tonic-gate }
19190Sstevel@tonic-gate 
19200Sstevel@tonic-gate /*
19210Sstevel@tonic-gate  * Close a session.
19220Sstevel@tonic-gate  */
19230Sstevel@tonic-gate static int
19240Sstevel@tonic-gate crypto_close_session(dev_t dev, crypto_session_id_t session_index)
19250Sstevel@tonic-gate {
19260Sstevel@tonic-gate 	crypto_session_data_t **session_table;
19270Sstevel@tonic-gate 	crypto_session_data_t *sp;
19280Sstevel@tonic-gate 	crypto_minor_t *cm;
19290Sstevel@tonic-gate 
19300Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
19310Sstevel@tonic-gate 		cmn_err(CE_WARN, "crypto_close_session: failed holding minor");
19320Sstevel@tonic-gate 		return (CRYPTO_FAILED);
19330Sstevel@tonic-gate 	}
19340Sstevel@tonic-gate 
19350Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
19360Sstevel@tonic-gate 	session_table = cm->cm_session_table;
19370Sstevel@tonic-gate 
19380Sstevel@tonic-gate 	if ((session_index) == 0 ||
19390Sstevel@tonic-gate 	    (session_index >= cm->cm_session_table_count)) {
19400Sstevel@tonic-gate 		mutex_exit(&cm->cm_lock);
19410Sstevel@tonic-gate 		crypto_release_minor(cm);
19420Sstevel@tonic-gate 		return (CRYPTO_SESSION_HANDLE_INVALID);
19430Sstevel@tonic-gate 	}
19440Sstevel@tonic-gate 
19450Sstevel@tonic-gate 	sp = session_table[session_index];
19460Sstevel@tonic-gate 	if (sp == NULL) {
19470Sstevel@tonic-gate 		mutex_exit(&cm->cm_lock);
19480Sstevel@tonic-gate 		crypto_release_minor(cm);
19490Sstevel@tonic-gate 		return (CRYPTO_SESSION_HANDLE_INVALID);
19500Sstevel@tonic-gate 	}
19510Sstevel@tonic-gate 	/*
19520Sstevel@tonic-gate 	 * If session is in use, free it when the thread
19530Sstevel@tonic-gate 	 * finishes with the session.
19540Sstevel@tonic-gate 	 */
19550Sstevel@tonic-gate 	mutex_enter(&sp->sd_lock);
19560Sstevel@tonic-gate 	if (sp->sd_flags & CRYPTO_SESSION_IS_BUSY) {
19570Sstevel@tonic-gate 		sp->sd_flags |= CRYPTO_SESSION_IS_CLOSED;
19580Sstevel@tonic-gate 		mutex_exit(&sp->sd_lock);
19590Sstevel@tonic-gate 	} else {
19606424Skrishna 		ASSERT(sp->sd_pre_approved_amount == 0 ||
19616424Skrishna 		    sp->sd_pre_approved_amount == crypto_pre_approved_limit);
19626424Skrishna 		CRYPTO_DECREMENT_RCTL(sp->sd_pre_approved_amount);
19636424Skrishna 
19640Sstevel@tonic-gate 		if (sp->sd_find_init_cookie != NULL) {
19650Sstevel@tonic-gate 			(void) crypto_free_find_ctx(sp);
19660Sstevel@tonic-gate 		}
19670Sstevel@tonic-gate 
19680Sstevel@tonic-gate 		crypto_release_provider_session(cm, sp->sd_provider_session);
19690Sstevel@tonic-gate 		KCF_PROV_REFRELE(sp->sd_provider);
19700Sstevel@tonic-gate 		CRYPTO_CANCEL_ALL_CTX(sp);
19710Sstevel@tonic-gate 		mutex_destroy(&sp->sd_lock);
19720Sstevel@tonic-gate 		cv_destroy(&sp->sd_cv);
19730Sstevel@tonic-gate 		kmem_cache_free(crypto_session_cache, sp);
19740Sstevel@tonic-gate 		session_table[session_index] = NULL;
19750Sstevel@tonic-gate 	}
19760Sstevel@tonic-gate 
19770Sstevel@tonic-gate 	mutex_exit(&cm->cm_lock);
19780Sstevel@tonic-gate 	crypto_release_minor(cm);
19790Sstevel@tonic-gate 
19800Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
19810Sstevel@tonic-gate }
19820Sstevel@tonic-gate 
19830Sstevel@tonic-gate /*
19840Sstevel@tonic-gate  * This ioctl opens a session and returns the session ID in os_session.
19850Sstevel@tonic-gate  */
19860Sstevel@tonic-gate /* ARGSUSED */
19870Sstevel@tonic-gate static int
19880Sstevel@tonic-gate open_session(dev_t dev, caddr_t arg, int mode, int *rval)
19890Sstevel@tonic-gate {
19900Sstevel@tonic-gate 	crypto_open_session_t open_session;
19910Sstevel@tonic-gate 	crypto_session_id_t session;
19920Sstevel@tonic-gate 	int rv;
19930Sstevel@tonic-gate 
19940Sstevel@tonic-gate 	if (copyin(arg, &open_session, sizeof (open_session)) != 0)
19950Sstevel@tonic-gate 		return (EFAULT);
19960Sstevel@tonic-gate 
19970Sstevel@tonic-gate 	rv = crypto_open_session(dev, open_session.os_flags,
19980Sstevel@tonic-gate 	    &session, open_session.os_provider_id);
19990Sstevel@tonic-gate 	if (rv != CRYPTO_SUCCESS) {
20000Sstevel@tonic-gate 		open_session.os_return_value = rv;
20010Sstevel@tonic-gate 		if (copyout(&open_session, arg, sizeof (open_session)) != 0) {
20020Sstevel@tonic-gate 			return (EFAULT);
20030Sstevel@tonic-gate 		}
20040Sstevel@tonic-gate 		return (0);
20050Sstevel@tonic-gate 	}
20060Sstevel@tonic-gate 
20070Sstevel@tonic-gate 	open_session.os_session = session;
20080Sstevel@tonic-gate 	open_session.os_return_value = CRYPTO_SUCCESS;
20090Sstevel@tonic-gate 
20100Sstevel@tonic-gate 	if (copyout(&open_session, arg, sizeof (open_session)) != 0) {
20110Sstevel@tonic-gate 		return (EFAULT);
20120Sstevel@tonic-gate 	}
20130Sstevel@tonic-gate 	return (0);
20140Sstevel@tonic-gate }
20150Sstevel@tonic-gate 
20160Sstevel@tonic-gate /*
20170Sstevel@tonic-gate  * This ioctl closes a session.
20180Sstevel@tonic-gate  */
20190Sstevel@tonic-gate /* ARGSUSED */
20200Sstevel@tonic-gate static int
20210Sstevel@tonic-gate close_session(dev_t dev, caddr_t arg, int mode, int *rval)
20220Sstevel@tonic-gate {
20230Sstevel@tonic-gate 	crypto_close_session_t close_session;
20240Sstevel@tonic-gate 	int rv;
20250Sstevel@tonic-gate 
20260Sstevel@tonic-gate 	if (copyin(arg, &close_session, sizeof (close_session)) != 0)
20270Sstevel@tonic-gate 		return (EFAULT);
20280Sstevel@tonic-gate 
20290Sstevel@tonic-gate 	rv = crypto_close_session(dev, close_session.cs_session);
2030904Smcpowers 	close_session.cs_return_value = rv;
20310Sstevel@tonic-gate 	if (copyout(&close_session, arg, sizeof (close_session)) != 0) {
20320Sstevel@tonic-gate 		return (EFAULT);
20330Sstevel@tonic-gate 	}
20340Sstevel@tonic-gate 	return (0);
20350Sstevel@tonic-gate }
20360Sstevel@tonic-gate 
20370Sstevel@tonic-gate /*
20380Sstevel@tonic-gate  * Copy data model dependent mechanism structure into a kernel mechanism
20390Sstevel@tonic-gate  * structure.  Allocate param storage if necessary.
20400Sstevel@tonic-gate  */
20410Sstevel@tonic-gate static boolean_t
20426424Skrishna copyin_mech(int mode, crypto_session_data_t *sp, crypto_mechanism_t *in_mech,
20436424Skrishna     crypto_mechanism_t *out_mech, size_t *out_rctl_bytes,
20446424Skrishna     boolean_t *out_rctl_chk, int *out_rv, int *out_error)
20450Sstevel@tonic-gate {
20460Sstevel@tonic-gate 	STRUCT_DECL(crypto_mechanism, mech);
20470Sstevel@tonic-gate 	caddr_t param;
20480Sstevel@tonic-gate 	size_t param_len;
20496424Skrishna 	size_t rctl_bytes = 0;
20500Sstevel@tonic-gate 	int error = 0;
20510Sstevel@tonic-gate 	int rv = 0;
20520Sstevel@tonic-gate 
20530Sstevel@tonic-gate 	STRUCT_INIT(mech, mode);
20540Sstevel@tonic-gate 	bcopy(in_mech, STRUCT_BUF(mech), STRUCT_SIZE(mech));
20550Sstevel@tonic-gate 	param = STRUCT_FGETP(mech, cm_param);
20560Sstevel@tonic-gate 	param_len = STRUCT_FGET(mech, cm_param_len);
20570Sstevel@tonic-gate 	out_mech->cm_type = STRUCT_FGET(mech, cm_type);
20580Sstevel@tonic-gate 	out_mech->cm_param = NULL;
20590Sstevel@tonic-gate 	out_mech->cm_param_len = 0;
20600Sstevel@tonic-gate 	if (param != NULL && param_len != 0) {
20610Sstevel@tonic-gate 		if (param_len > crypto_max_buffer_len) {
20620Sstevel@tonic-gate 			cmn_err(CE_NOTE, "copyin_mech: buffer greater than "
20630Sstevel@tonic-gate 			    "%ld bytes, pid = %d", crypto_max_buffer_len,
20640Sstevel@tonic-gate 			    curproc->p_pid);
20650Sstevel@tonic-gate 			rv = CRYPTO_ARGUMENTS_BAD;
20660Sstevel@tonic-gate 			goto out;
20670Sstevel@tonic-gate 		}
20680Sstevel@tonic-gate 
20696424Skrishna 		rv = CRYPTO_BUFFER_CHECK(sp, param_len, *out_rctl_chk);
20706424Skrishna 		if (rv != CRYPTO_SUCCESS) {
20716424Skrishna 			goto out;
20720Sstevel@tonic-gate 		}
20736424Skrishna 		rctl_bytes = param_len;
20746424Skrishna 
20750Sstevel@tonic-gate 		out_mech->cm_param = kmem_alloc(param_len, KM_SLEEP);
20760Sstevel@tonic-gate 		if (copyin((char *)param, out_mech->cm_param, param_len) != 0) {
20770Sstevel@tonic-gate 			kmem_free(out_mech->cm_param, param_len);
20780Sstevel@tonic-gate 			out_mech->cm_param = NULL;
20790Sstevel@tonic-gate 			error = EFAULT;
20800Sstevel@tonic-gate 			goto out;
20810Sstevel@tonic-gate 		}
20820Sstevel@tonic-gate 		out_mech->cm_param_len = param_len;
20830Sstevel@tonic-gate 	}
20840Sstevel@tonic-gate out:
20850Sstevel@tonic-gate 	*out_rctl_bytes = rctl_bytes;
20860Sstevel@tonic-gate 	*out_rv = rv;
20870Sstevel@tonic-gate 	*out_error = error;
20880Sstevel@tonic-gate 	return ((rv | error) ? B_FALSE : B_TRUE);
20890Sstevel@tonic-gate }
20900Sstevel@tonic-gate 
20910Sstevel@tonic-gate /*
20920Sstevel@tonic-gate  * Free key attributes when key type is CRYPTO_KEY_ATTR_LIST.
20930Sstevel@tonic-gate  * The crypto_key structure is not freed.
20940Sstevel@tonic-gate  */
20950Sstevel@tonic-gate static void
20960Sstevel@tonic-gate crypto_free_key_attributes(crypto_key_t *key)
20970Sstevel@tonic-gate {
20980Sstevel@tonic-gate 	crypto_object_attribute_t *attrs;
20990Sstevel@tonic-gate 	size_t len = 0;
21000Sstevel@tonic-gate 	int i;
21010Sstevel@tonic-gate 
21020Sstevel@tonic-gate 	ASSERT(key->ck_format == CRYPTO_KEY_ATTR_LIST);
21030Sstevel@tonic-gate 	if (key->ck_count == 0 || key->ck_attrs == NULL)
21040Sstevel@tonic-gate 		return;
21050Sstevel@tonic-gate 
21060Sstevel@tonic-gate 	/* compute the size of the container */
21070Sstevel@tonic-gate 	len = key->ck_count * sizeof (crypto_object_attribute_t);
21080Sstevel@tonic-gate 
21090Sstevel@tonic-gate 	/* total up the size of all attributes in the container */
21100Sstevel@tonic-gate 	for (i = 0; i < key->ck_count; i++) {
21110Sstevel@tonic-gate 		attrs = &key->ck_attrs[i];
21120Sstevel@tonic-gate 		if (attrs->oa_value_len != 0 &&
21130Sstevel@tonic-gate 		    attrs->oa_value != NULL) {
21140Sstevel@tonic-gate 			len += roundup(attrs->oa_value_len, sizeof (caddr_t));
21150Sstevel@tonic-gate 		}
21160Sstevel@tonic-gate 	}
21170Sstevel@tonic-gate 
21180Sstevel@tonic-gate 	bzero(key->ck_attrs, len);
21190Sstevel@tonic-gate 	kmem_free(key->ck_attrs, len);
21200Sstevel@tonic-gate }
21210Sstevel@tonic-gate 
21220Sstevel@tonic-gate /*
21230Sstevel@tonic-gate  * Frees allocated storage in the key structure, but doesn't free
21240Sstevel@tonic-gate  * the key structure.
21250Sstevel@tonic-gate  */
21260Sstevel@tonic-gate static void
21270Sstevel@tonic-gate free_crypto_key(crypto_key_t *key)
21280Sstevel@tonic-gate {
21290Sstevel@tonic-gate 	switch (key->ck_format) {
21300Sstevel@tonic-gate 	case CRYPTO_KEY_RAW: {
21310Sstevel@tonic-gate 		size_t len;
21320Sstevel@tonic-gate 
21330Sstevel@tonic-gate 		if (key->ck_length == 0 || key->ck_data == NULL)
21340Sstevel@tonic-gate 			break;
21350Sstevel@tonic-gate 
21360Sstevel@tonic-gate 		len = CRYPTO_BITS2BYTES(key->ck_length);
21370Sstevel@tonic-gate 		bzero(key->ck_data, len);
21380Sstevel@tonic-gate 		kmem_free(key->ck_data, len);
21390Sstevel@tonic-gate 		break;
21400Sstevel@tonic-gate 	}
21410Sstevel@tonic-gate 
21420Sstevel@tonic-gate 	case CRYPTO_KEY_ATTR_LIST:
21430Sstevel@tonic-gate 		crypto_free_key_attributes(key);
21440Sstevel@tonic-gate 		break;
21450Sstevel@tonic-gate 
21460Sstevel@tonic-gate 	default:
21470Sstevel@tonic-gate 		break;
21480Sstevel@tonic-gate 	}
21490Sstevel@tonic-gate }
21500Sstevel@tonic-gate 
21510Sstevel@tonic-gate /*
21520Sstevel@tonic-gate  * Copy in an array of crypto_object_attribute structures from user-space.
21530Sstevel@tonic-gate  * Kernel memory is allocated for the array and the value of each attribute
21540Sstevel@tonic-gate  * in the array.  Since unprivileged users can specify the size of attributes,
21550Sstevel@tonic-gate  * the amount of memory needed is charged against the
21560Sstevel@tonic-gate  * project.max-crypto-memory resource control.
21570Sstevel@tonic-gate  *
21580Sstevel@tonic-gate  * Attribute values are copied in from user-space if copyin_value is set to
21590Sstevel@tonic-gate  * B_TRUE.  This routine returns B_TRUE if the copyin was successful.
21600Sstevel@tonic-gate  */
21610Sstevel@tonic-gate static boolean_t
21626424Skrishna copyin_attributes(int mode, crypto_session_data_t *sp,
21636424Skrishna     uint_t count, caddr_t oc_attributes,
21640Sstevel@tonic-gate     crypto_object_attribute_t **k_attrs_out, size_t *k_attrs_size_out,
21650Sstevel@tonic-gate     caddr_t *u_attrs_out, int *out_rv, int *out_error, size_t *out_rctl_bytes,
21666424Skrishna     boolean_t *out_rctl_chk, boolean_t copyin_value)
21670Sstevel@tonic-gate {
21680Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_attribute, oa);
21690Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
21700Sstevel@tonic-gate 	caddr_t attrs = NULL, ap, p, value;
21710Sstevel@tonic-gate 	caddr_t k_attrs_buf;
21720Sstevel@tonic-gate 	size_t k_attrs_len;
21730Sstevel@tonic-gate 	size_t k_attrs_buf_len = 0;
21740Sstevel@tonic-gate 	size_t k_attrs_total_len = 0;
21750Sstevel@tonic-gate 	size_t tmp_len;
21760Sstevel@tonic-gate 	size_t rctl_bytes = 0;
21770Sstevel@tonic-gate 	size_t len = 0;
21780Sstevel@tonic-gate 	size_t value_len;
21790Sstevel@tonic-gate 	int error = 0;
21800Sstevel@tonic-gate 	int rv = 0;
21810Sstevel@tonic-gate 	int i;
21820Sstevel@tonic-gate 
21830Sstevel@tonic-gate 	STRUCT_INIT(oa, mode);
21840Sstevel@tonic-gate 
21850Sstevel@tonic-gate 	if (count == 0) {
21860Sstevel@tonic-gate 		rv = CRYPTO_SUCCESS;
21870Sstevel@tonic-gate 		goto out;
21880Sstevel@tonic-gate 	}
21890Sstevel@tonic-gate 
21900Sstevel@tonic-gate 	if (count > CRYPTO_MAX_ATTRIBUTE_COUNT) {
21910Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
21920Sstevel@tonic-gate 		goto out;
21930Sstevel@tonic-gate 	}
21940Sstevel@tonic-gate 
21950Sstevel@tonic-gate 	/* compute size of crypto_object_attribute array */
21960Sstevel@tonic-gate 	len = count * STRUCT_SIZE(oa);
21970Sstevel@tonic-gate 
21980Sstevel@tonic-gate 	/* this allocation is not charged against the user's resource limit */
21990Sstevel@tonic-gate 	attrs = kmem_alloc(len, KM_SLEEP);
22000Sstevel@tonic-gate 	if (copyin(oc_attributes, attrs, len) != 0) {
22010Sstevel@tonic-gate 		error = EFAULT;
22020Sstevel@tonic-gate 		goto out;
22030Sstevel@tonic-gate 	}
22040Sstevel@tonic-gate 
22050Sstevel@tonic-gate 	/* figure out how much memory to allocate for all of the attributes */
22060Sstevel@tonic-gate 	ap = attrs;
22070Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
22080Sstevel@tonic-gate 		bcopy(ap, STRUCT_BUF(oa), STRUCT_SIZE(oa));
22090Sstevel@tonic-gate 		tmp_len = roundup(STRUCT_FGET(oa, oa_value_len),
22100Sstevel@tonic-gate 		    sizeof (caddr_t));
22110Sstevel@tonic-gate 		if (tmp_len > crypto_max_buffer_len) {
22120Sstevel@tonic-gate 			cmn_err(CE_NOTE, "copyin_attributes: buffer greater "
22130Sstevel@tonic-gate 			    "than %ld bytes, pid = %d", crypto_max_buffer_len,
22140Sstevel@tonic-gate 			    curproc->p_pid);
22150Sstevel@tonic-gate 			rv = CRYPTO_ARGUMENTS_BAD;
22160Sstevel@tonic-gate 			goto out;
22170Sstevel@tonic-gate 		}
22180Sstevel@tonic-gate 		if (STRUCT_FGETP(oa, oa_value) != NULL)
22190Sstevel@tonic-gate 			k_attrs_buf_len += tmp_len;
22200Sstevel@tonic-gate 		ap += STRUCT_SIZE(oa);
22210Sstevel@tonic-gate 	}
22220Sstevel@tonic-gate 
22230Sstevel@tonic-gate 	k_attrs_len = count * sizeof (crypto_object_attribute_t);
22240Sstevel@tonic-gate 	k_attrs_total_len = k_attrs_buf_len + k_attrs_len;
22256424Skrishna 
22266424Skrishna 	rv = CRYPTO_BUFFER_CHECK(sp, k_attrs_total_len, *out_rctl_chk);
22276424Skrishna 	if (rv != CRYPTO_SUCCESS) {
22286424Skrishna 		goto out;
22296424Skrishna 	}
22306424Skrishna 	rctl_bytes = k_attrs_total_len;
22310Sstevel@tonic-gate 
22320Sstevel@tonic-gate 	/* one big allocation for everything */
22330Sstevel@tonic-gate 	k_attrs = kmem_alloc(k_attrs_total_len, KM_SLEEP);
22340Sstevel@tonic-gate 	k_attrs_buf = (char *)k_attrs + k_attrs_len;
22350Sstevel@tonic-gate 
22360Sstevel@tonic-gate 	ap = attrs;
22370Sstevel@tonic-gate 	p = k_attrs_buf;
22380Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
22390Sstevel@tonic-gate 		bcopy(ap, STRUCT_BUF(oa), STRUCT_SIZE(oa));
22400Sstevel@tonic-gate 		k_attrs[i].oa_type = STRUCT_FGET(oa, oa_type);
22410Sstevel@tonic-gate 		value = STRUCT_FGETP(oa, oa_value);
22420Sstevel@tonic-gate 		value_len = STRUCT_FGET(oa, oa_value_len);
22430Sstevel@tonic-gate 		if (value != NULL && value_len != 0 && copyin_value) {
22440Sstevel@tonic-gate 			if (copyin(value, p, value_len) != 0) {
22450Sstevel@tonic-gate 				kmem_free(k_attrs, k_attrs_total_len);
22460Sstevel@tonic-gate 				k_attrs = NULL;
22470Sstevel@tonic-gate 				error = EFAULT;
22480Sstevel@tonic-gate 				goto out;
22490Sstevel@tonic-gate 			}
22500Sstevel@tonic-gate 		}
22510Sstevel@tonic-gate 
2252292Smcpowers 		if (value != NULL) {
2253292Smcpowers 			k_attrs[i].oa_value = p;
2254292Smcpowers 			p += roundup(value_len, sizeof (caddr_t));
2255292Smcpowers 		} else {
2256292Smcpowers 			k_attrs[i].oa_value = NULL;
2257292Smcpowers 		}
22580Sstevel@tonic-gate 		k_attrs[i].oa_value_len = value_len;
22590Sstevel@tonic-gate 		ap += STRUCT_SIZE(oa);
22600Sstevel@tonic-gate 	}
22610Sstevel@tonic-gate out:
22620Sstevel@tonic-gate 	if (attrs != NULL) {
22630Sstevel@tonic-gate 		/*
22640Sstevel@tonic-gate 		 * Free the array if there is a failure or the caller
22650Sstevel@tonic-gate 		 * doesn't want the array to be returned.
22660Sstevel@tonic-gate 		 */
22670Sstevel@tonic-gate 		if (error != 0 || rv != CRYPTO_SUCCESS || u_attrs_out == NULL) {
22680Sstevel@tonic-gate 			kmem_free(attrs, len);
22690Sstevel@tonic-gate 			attrs = NULL;
22700Sstevel@tonic-gate 		}
22710Sstevel@tonic-gate 	}
22720Sstevel@tonic-gate 
22730Sstevel@tonic-gate 	if (u_attrs_out != NULL)
22740Sstevel@tonic-gate 		*u_attrs_out = attrs;
22750Sstevel@tonic-gate 	if (k_attrs_size_out != NULL)
22760Sstevel@tonic-gate 		*k_attrs_size_out = k_attrs_total_len;
22770Sstevel@tonic-gate 	*k_attrs_out = k_attrs;
22780Sstevel@tonic-gate 	*out_rctl_bytes = rctl_bytes;
22790Sstevel@tonic-gate 	*out_rv = rv;
22800Sstevel@tonic-gate 	*out_error = error;
22810Sstevel@tonic-gate 	return ((rv | error) ? B_FALSE : B_TRUE);
22820Sstevel@tonic-gate }
22830Sstevel@tonic-gate 
22840Sstevel@tonic-gate /*
22850Sstevel@tonic-gate  * Copy data model dependent raw key into a kernel key
22860Sstevel@tonic-gate  * structure.  Checks key length or attribute lengths against
22870Sstevel@tonic-gate  * resource controls before allocating memory.  Returns B_TRUE
22880Sstevel@tonic-gate  * if both error and rv are set to 0.
22890Sstevel@tonic-gate  */
22900Sstevel@tonic-gate static boolean_t
22916424Skrishna copyin_key(int mode, crypto_session_data_t *sp, crypto_key_t *in_key,
22926424Skrishna     crypto_key_t *out_key, size_t *out_rctl_bytes,
22936424Skrishna     boolean_t *out_rctl_chk, int *out_rv, int *out_error)
22940Sstevel@tonic-gate {
22950Sstevel@tonic-gate 	STRUCT_DECL(crypto_key, key);
22960Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
22970Sstevel@tonic-gate 	size_t key_bits;
22980Sstevel@tonic-gate 	size_t key_bytes = 0;
22990Sstevel@tonic-gate 	size_t rctl_bytes = 0;
23000Sstevel@tonic-gate 	int count;
23010Sstevel@tonic-gate 	int error = 0;
23020Sstevel@tonic-gate 	int rv = CRYPTO_SUCCESS;
23030Sstevel@tonic-gate 
23040Sstevel@tonic-gate 	STRUCT_INIT(key, mode);
23050Sstevel@tonic-gate 	bcopy(in_key, STRUCT_BUF(key), STRUCT_SIZE(key));
23060Sstevel@tonic-gate 	out_key->ck_format = STRUCT_FGET(key, ck_format);
23070Sstevel@tonic-gate 	switch (out_key->ck_format) {
23080Sstevel@tonic-gate 	case CRYPTO_KEY_RAW:
23090Sstevel@tonic-gate 		key_bits = STRUCT_FGET(key, ck_length);
23100Sstevel@tonic-gate 		if (key_bits != 0) {
23110Sstevel@tonic-gate 			key_bytes = CRYPTO_BITS2BYTES(key_bits);
23120Sstevel@tonic-gate 			if (key_bytes > crypto_max_buffer_len) {
23130Sstevel@tonic-gate 				cmn_err(CE_NOTE, "copyin_key: buffer greater "
23140Sstevel@tonic-gate 				    "than %ld bytes, pid = %d",
23150Sstevel@tonic-gate 				    crypto_max_buffer_len, curproc->p_pid);
23160Sstevel@tonic-gate 				rv = CRYPTO_ARGUMENTS_BAD;
23170Sstevel@tonic-gate 				goto out;
23180Sstevel@tonic-gate 			}
23190Sstevel@tonic-gate 
23206424Skrishna 			rv = CRYPTO_BUFFER_CHECK(sp, key_bytes,
23216424Skrishna 			    *out_rctl_chk);
23220Sstevel@tonic-gate 			if (rv != CRYPTO_SUCCESS) {
23230Sstevel@tonic-gate 				goto out;
23240Sstevel@tonic-gate 			}
23256424Skrishna 			rctl_bytes = key_bytes;
23260Sstevel@tonic-gate 
23270Sstevel@tonic-gate 			out_key->ck_data = kmem_alloc(key_bytes, KM_SLEEP);
23280Sstevel@tonic-gate 
23290Sstevel@tonic-gate 			if (copyin((char *)STRUCT_FGETP(key, ck_data),
23300Sstevel@tonic-gate 			    out_key->ck_data, key_bytes) != 0) {
23310Sstevel@tonic-gate 				kmem_free(out_key->ck_data, key_bytes);
23320Sstevel@tonic-gate 				out_key->ck_data = NULL;
23330Sstevel@tonic-gate 				out_key->ck_length = 0;
23340Sstevel@tonic-gate 				error = EFAULT;
23350Sstevel@tonic-gate 				goto out;
23360Sstevel@tonic-gate 			}
23370Sstevel@tonic-gate 		}
23380Sstevel@tonic-gate 		out_key->ck_length = key_bits;
23390Sstevel@tonic-gate 		break;
23400Sstevel@tonic-gate 
23410Sstevel@tonic-gate 	case CRYPTO_KEY_ATTR_LIST:
23420Sstevel@tonic-gate 		count = STRUCT_FGET(key, ck_count);
23430Sstevel@tonic-gate 
23446424Skrishna 		if (copyin_attributes(mode, sp, count,
23450Sstevel@tonic-gate 		    (caddr_t)STRUCT_FGETP(key, ck_attrs), &k_attrs, NULL, NULL,
23466424Skrishna 		    &rv, &error, &rctl_bytes, out_rctl_chk, B_TRUE)) {
23470Sstevel@tonic-gate 			out_key->ck_count = count;
23480Sstevel@tonic-gate 			out_key->ck_attrs = k_attrs;
23490Sstevel@tonic-gate 			k_attrs = NULL;
23500Sstevel@tonic-gate 		} else {
23510Sstevel@tonic-gate 			out_key->ck_count = 0;
23520Sstevel@tonic-gate 			out_key->ck_attrs = NULL;
23530Sstevel@tonic-gate 		}
23540Sstevel@tonic-gate 		break;
23550Sstevel@tonic-gate 
23560Sstevel@tonic-gate 	case CRYPTO_KEY_REFERENCE:
23570Sstevel@tonic-gate 		out_key->ck_obj_id = STRUCT_FGET(key, ck_obj_id);
23580Sstevel@tonic-gate 		break;
23590Sstevel@tonic-gate 
23600Sstevel@tonic-gate 	default:
23610Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
23620Sstevel@tonic-gate 	}
23630Sstevel@tonic-gate 
23640Sstevel@tonic-gate out:
23650Sstevel@tonic-gate 	*out_rctl_bytes = rctl_bytes;
23660Sstevel@tonic-gate 	*out_rv = rv;
23670Sstevel@tonic-gate 	*out_error = error;
23680Sstevel@tonic-gate 	return ((rv | error) ? B_FALSE : B_TRUE);
23690Sstevel@tonic-gate }
23700Sstevel@tonic-gate 
23710Sstevel@tonic-gate /*
23720Sstevel@tonic-gate  * This routine does two things:
23730Sstevel@tonic-gate  * 1. Given a crypto_minor structure and a session ID, it returns
23740Sstevel@tonic-gate  *    a valid session pointer.
23750Sstevel@tonic-gate  * 2. It checks that the provider, to which the session has been opened,
23760Sstevel@tonic-gate  *    has not been removed.
23770Sstevel@tonic-gate  */
23780Sstevel@tonic-gate static boolean_t
23790Sstevel@tonic-gate get_session_ptr(crypto_session_id_t i, crypto_minor_t *cm,
23800Sstevel@tonic-gate     crypto_session_data_t **session_ptr, int *out_error, int *out_rv)
23810Sstevel@tonic-gate {
23820Sstevel@tonic-gate 	crypto_session_data_t *sp = NULL;
23830Sstevel@tonic-gate 	int rv = CRYPTO_SESSION_HANDLE_INVALID;
23840Sstevel@tonic-gate 	int error = 0;
23850Sstevel@tonic-gate 
23860Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
23870Sstevel@tonic-gate 	if ((i < cm->cm_session_table_count) &&
23880Sstevel@tonic-gate 	    (cm->cm_session_table[i] != NULL)) {
23890Sstevel@tonic-gate 		sp = cm->cm_session_table[i];
23900Sstevel@tonic-gate 		mutex_enter(&sp->sd_lock);
23910Sstevel@tonic-gate 		mutex_exit(&cm->cm_lock);
23920Sstevel@tonic-gate 		while (sp->sd_flags & CRYPTO_SESSION_IS_BUSY) {
23930Sstevel@tonic-gate 			if (cv_wait_sig(&sp->sd_cv, &sp->sd_lock) == 0) {
23940Sstevel@tonic-gate 				mutex_exit(&sp->sd_lock);
23950Sstevel@tonic-gate 				sp = NULL;
23960Sstevel@tonic-gate 				error = EINTR;
23970Sstevel@tonic-gate 				goto out;
23980Sstevel@tonic-gate 			}
23990Sstevel@tonic-gate 		}
24000Sstevel@tonic-gate 
24010Sstevel@tonic-gate 		if (sp->sd_flags & CRYPTO_SESSION_IS_CLOSED) {
24020Sstevel@tonic-gate 			mutex_exit(&sp->sd_lock);
24030Sstevel@tonic-gate 			sp = NULL;
24040Sstevel@tonic-gate 			goto out;
24050Sstevel@tonic-gate 		}
24060Sstevel@tonic-gate 
24070Sstevel@tonic-gate 		if (KCF_IS_PROV_REMOVED(sp->sd_provider)) {
24080Sstevel@tonic-gate 			mutex_exit(&sp->sd_lock);
24090Sstevel@tonic-gate 			sp = NULL;
24100Sstevel@tonic-gate 			rv = CRYPTO_DEVICE_ERROR;
24110Sstevel@tonic-gate 			goto out;
24120Sstevel@tonic-gate 		}
24130Sstevel@tonic-gate 
24140Sstevel@tonic-gate 		rv = CRYPTO_SUCCESS;
24150Sstevel@tonic-gate 		sp->sd_flags |= CRYPTO_SESSION_IS_BUSY;
24160Sstevel@tonic-gate 		mutex_exit(&sp->sd_lock);
24170Sstevel@tonic-gate 	} else {
24180Sstevel@tonic-gate 		mutex_exit(&cm->cm_lock);
24190Sstevel@tonic-gate 	}
24200Sstevel@tonic-gate out:
24210Sstevel@tonic-gate 	*session_ptr = sp;
24220Sstevel@tonic-gate 	*out_error = error;
24230Sstevel@tonic-gate 	*out_rv = rv;
24240Sstevel@tonic-gate 	return ((rv == CRYPTO_SUCCESS && error == 0) ? B_TRUE : B_FALSE);
24250Sstevel@tonic-gate }
24260Sstevel@tonic-gate 
24276424Skrishna #define	CRYPTO_SESSION_RELE(s)	if ((s) != NULL) {	\
24280Sstevel@tonic-gate 	mutex_enter(&((s)->sd_lock));			\
24290Sstevel@tonic-gate 	(s)->sd_flags &= ~CRYPTO_SESSION_IS_BUSY;	\
24300Sstevel@tonic-gate 	cv_broadcast(&(s)->sd_cv);			\
24310Sstevel@tonic-gate 	mutex_exit(&((s)->sd_lock));			\
24320Sstevel@tonic-gate }
24330Sstevel@tonic-gate 
24340Sstevel@tonic-gate /* ARGSUSED */
24350Sstevel@tonic-gate static int
24360Sstevel@tonic-gate encrypt_init(dev_t dev, caddr_t arg, int mode, int *rval)
24370Sstevel@tonic-gate {
24380Sstevel@tonic-gate 	return (cipher_init(dev, arg, mode, crypto_encrypt_init_prov));
24390Sstevel@tonic-gate }
24400Sstevel@tonic-gate 
24410Sstevel@tonic-gate /* ARGSUSED */
24420Sstevel@tonic-gate static int
24430Sstevel@tonic-gate decrypt_init(dev_t dev, caddr_t arg, int mode, int *rval)
24440Sstevel@tonic-gate {
24450Sstevel@tonic-gate 	return (cipher_init(dev, arg, mode, crypto_decrypt_init_prov));
24460Sstevel@tonic-gate }
24470Sstevel@tonic-gate 
24480Sstevel@tonic-gate /*
2449904Smcpowers  * umech is a mechanism structure that has been copied from user address
2450904Smcpowers  * space into kernel address space. Only one copyin has been done.
2451904Smcpowers  * The mechanism parameter, if non-null, still points to user address space.
2452904Smcpowers  * If the mechanism parameter contains pointers, they are pointers into
2453904Smcpowers  * user address space.
2454904Smcpowers  *
2455904Smcpowers  * kmech is a umech with all pointers and structures in kernel address space.
2456904Smcpowers  *
2457904Smcpowers  * This routine calls the provider's entry point to copy a umech parameter
2458904Smcpowers  * into kernel address space. Kernel memory is allocated by the provider.
2459904Smcpowers  */
2460904Smcpowers static int
2461904Smcpowers crypto_provider_copyin_mech_param(kcf_provider_desc_t *pd,
2462904Smcpowers     crypto_mechanism_t *umech, crypto_mechanism_t *kmech, int mode, int *error)
2463904Smcpowers {
2464904Smcpowers 	crypto_mech_type_t provider_mech_type;
2465904Smcpowers 	int rv;
2466904Smcpowers 
2467904Smcpowers 	/* get the provider's mech number */
24683708Skrishna 	provider_mech_type = KCF_TO_PROV_MECHNUM(pd, umech->cm_type);
2469904Smcpowers 
2470904Smcpowers 	kmech->cm_param = NULL;
2471904Smcpowers 	kmech->cm_param_len = 0;
2472904Smcpowers 	kmech->cm_type = provider_mech_type;
2473904Smcpowers 	rv = KCF_PROV_COPYIN_MECH(pd, umech, kmech, error, mode);
2474904Smcpowers 	kmech->cm_type = umech->cm_type;
2475904Smcpowers 
2476904Smcpowers 	return (rv);
2477904Smcpowers }
2478904Smcpowers 
2479904Smcpowers /*
2480904Smcpowers  * umech is a mechanism structure that has been copied from user address
2481904Smcpowers  * space into kernel address space. Only one copyin has been done.
2482904Smcpowers  * The mechanism parameter, if non-null, still points to user address space.
2483904Smcpowers  * If the mechanism parameter contains pointers, they are pointers into
2484904Smcpowers  * user address space.
2485904Smcpowers  *
2486904Smcpowers  * kmech is a umech with all pointers and structures in kernel address space.
2487904Smcpowers  *
2488904Smcpowers  * This routine calls the provider's entry point to copy a kmech parameter
2489904Smcpowers  * into user address space using umech as a template containing
2490904Smcpowers  * user address pointers.
2491904Smcpowers  */
2492904Smcpowers static int
2493904Smcpowers crypto_provider_copyout_mech_param(kcf_provider_desc_t *pd,
2494904Smcpowers     crypto_mechanism_t *kmech, crypto_mechanism_t *umech, int mode, int *error)
2495904Smcpowers {
2496904Smcpowers 	crypto_mech_type_t provider_mech_type;
2497904Smcpowers 	int rv;
2498904Smcpowers 
2499904Smcpowers 	/* get the provider's mech number */
25003708Skrishna 	provider_mech_type = KCF_TO_PROV_MECHNUM(pd, umech->cm_type);
2501904Smcpowers 
2502904Smcpowers 	kmech->cm_type = provider_mech_type;
2503904Smcpowers 	rv = KCF_PROV_COPYOUT_MECH(pd, kmech, umech, error, mode);
2504904Smcpowers 	kmech->cm_type = umech->cm_type;
2505904Smcpowers 
2506904Smcpowers 	return (rv);
2507904Smcpowers }
2508904Smcpowers 
2509904Smcpowers /*
2510904Smcpowers  * Call the provider's entry point to free kernel memory that has been
2511904Smcpowers  * allocated for the mechanism's parameter.
2512904Smcpowers  */
2513904Smcpowers static void
2514904Smcpowers crypto_free_mech(kcf_provider_desc_t *pd, boolean_t allocated_by_crypto_module,
2515904Smcpowers     crypto_mechanism_t *mech)
2516904Smcpowers {
2517904Smcpowers 	crypto_mech_type_t provider_mech_type;
2518904Smcpowers 
2519904Smcpowers 	if (allocated_by_crypto_module) {
2520904Smcpowers 		if (mech->cm_param != NULL)
2521904Smcpowers 			kmem_free(mech->cm_param, mech->cm_param_len);
2522904Smcpowers 	} else {
2523904Smcpowers 		/* get the provider's mech number */
25243708Skrishna 		provider_mech_type = KCF_TO_PROV_MECHNUM(pd, mech->cm_type);
2525904Smcpowers 
2526904Smcpowers 		if (mech->cm_param != NULL && mech->cm_param_len != 0) {
2527904Smcpowers 			mech->cm_type = provider_mech_type;
2528904Smcpowers 			(void) KCF_PROV_FREE_MECH(pd, mech);
2529904Smcpowers 		}
2530904Smcpowers 	}
2531904Smcpowers }
2532904Smcpowers 
2533904Smcpowers /*
25340Sstevel@tonic-gate  * ASSUMPTION: crypto_encrypt_init and crypto_decrypt_init
25350Sstevel@tonic-gate  * structures are identical except for field names.
25360Sstevel@tonic-gate  */
25370Sstevel@tonic-gate static int
2538904Smcpowers cipher_init(dev_t dev, caddr_t arg, int mode, int (*init)(crypto_provider_t,
25390Sstevel@tonic-gate     crypto_session_id_t, crypto_mechanism_t *, crypto_key_t *,
25400Sstevel@tonic-gate     crypto_ctx_template_t, crypto_context_t *, crypto_call_req_t *))
25410Sstevel@tonic-gate {
25420Sstevel@tonic-gate 	STRUCT_DECL(crypto_encrypt_init, encrypt_init);
2543904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
25440Sstevel@tonic-gate 	crypto_session_id_t session_id;
25450Sstevel@tonic-gate 	crypto_mechanism_t mech;
25460Sstevel@tonic-gate 	crypto_key_t key;
25470Sstevel@tonic-gate 	crypto_minor_t *cm;
25486424Skrishna 	crypto_session_data_t *sp = NULL;
25490Sstevel@tonic-gate 	crypto_context_t cc;
25500Sstevel@tonic-gate 	crypto_ctx_t **ctxpp;
25510Sstevel@tonic-gate 	size_t mech_rctl_bytes = 0;
25526424Skrishna 	boolean_t mech_rctl_chk = B_FALSE;
25530Sstevel@tonic-gate 	size_t key_rctl_bytes = 0;
25546424Skrishna 	boolean_t key_rctl_chk = B_FALSE;
25550Sstevel@tonic-gate 	int error = 0;
25560Sstevel@tonic-gate 	int rv;
2557904Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
25581808Smcpowers 	crypto_func_group_t fg;
25590Sstevel@tonic-gate 
25600Sstevel@tonic-gate 	STRUCT_INIT(encrypt_init, mode);
25610Sstevel@tonic-gate 
25620Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
25630Sstevel@tonic-gate 		cmn_err(CE_WARN, "cipher_init: failed holding minor");
25640Sstevel@tonic-gate 		return (ENXIO);
25650Sstevel@tonic-gate 	}
25660Sstevel@tonic-gate 
25670Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(encrypt_init),
25680Sstevel@tonic-gate 	    STRUCT_SIZE(encrypt_init)) != 0) {
25690Sstevel@tonic-gate 		crypto_release_minor(cm);
25700Sstevel@tonic-gate 		return (EFAULT);
25710Sstevel@tonic-gate 	}
25720Sstevel@tonic-gate 
25730Sstevel@tonic-gate 	mech.cm_param = NULL;
25740Sstevel@tonic-gate 	bzero(&key, sizeof (crypto_key_t));
25750Sstevel@tonic-gate 
25760Sstevel@tonic-gate 	session_id = STRUCT_FGET(encrypt_init, ei_session);
25770Sstevel@tonic-gate 
25780Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
25796424Skrishna 		goto out;
25800Sstevel@tonic-gate 	}
25810Sstevel@tonic-gate 
2582904Smcpowers 	bcopy(STRUCT_FADDR(encrypt_init, ei_mech), &mech.cm_type,
2583904Smcpowers 	    sizeof (crypto_mech_type_t));
25840Sstevel@tonic-gate 
25851808Smcpowers 	if (init == crypto_encrypt_init_prov) {
25861808Smcpowers 		fg = CRYPTO_FG_ENCRYPT;
25871808Smcpowers 	} else {
25881808Smcpowers 		fg = CRYPTO_FG_DECRYPT;
25891808Smcpowers 	}
25900Sstevel@tonic-gate 
2591904Smcpowers 	if ((rv = kcf_get_hardware_provider(mech.cm_type, CRYPTO_MECH_INVALID,
25921808Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider, fg))
25931808Smcpowers 	    != CRYPTO_SUCCESS) {
25940Sstevel@tonic-gate 		goto out;
25950Sstevel@tonic-gate 	}
25960Sstevel@tonic-gate 
2597904Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
2598904Smcpowers 	    STRUCT_FADDR(encrypt_init, ei_mech), &mech, mode, &error);
2599904Smcpowers 
2600904Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
2601904Smcpowers 		allocated_by_crypto_module = B_TRUE;
26026424Skrishna 		if (!copyin_mech(mode, sp, STRUCT_FADDR(encrypt_init, ei_mech),
26036424Skrishna 		    &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) {
2604904Smcpowers 			goto out;
2605904Smcpowers 		}
2606904Smcpowers 	} else {
2607904Smcpowers 		if (rv != CRYPTO_SUCCESS)
2608904Smcpowers 			goto out;
2609904Smcpowers 	}
2610904Smcpowers 
26116424Skrishna 	if (!copyin_key(mode, sp, STRUCT_FADDR(encrypt_init, ei_key), &key,
26126424Skrishna 	    &key_rctl_bytes, &key_rctl_chk, &rv, &error)) {
26130Sstevel@tonic-gate 		goto out;
26140Sstevel@tonic-gate 	}
26150Sstevel@tonic-gate 
26160Sstevel@tonic-gate 	rv = (init)(real_provider, sp->sd_provider_session->ps_session,
26170Sstevel@tonic-gate 	    &mech, &key, NULL, &cc, NULL);
26180Sstevel@tonic-gate 
26190Sstevel@tonic-gate 	/*
26200Sstevel@tonic-gate 	 * Check if a context already exists. If so, it means it is being
26210Sstevel@tonic-gate 	 * abandoned. So, cancel it to avoid leaking it.
26220Sstevel@tonic-gate 	 */
26230Sstevel@tonic-gate 	ctxpp = (init == crypto_encrypt_init_prov) ?
26240Sstevel@tonic-gate 	    &sp->sd_encr_ctx : &sp->sd_decr_ctx;
26250Sstevel@tonic-gate 
26260Sstevel@tonic-gate 	if (*ctxpp != NULL)
26270Sstevel@tonic-gate 		CRYPTO_CANCEL_CTX(ctxpp);
26280Sstevel@tonic-gate 	*ctxpp = (rv == CRYPTO_SUCCESS) ? cc : NULL;
2629904Smcpowers 
26300Sstevel@tonic-gate out:
26316424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
26326424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk);
26330Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
26340Sstevel@tonic-gate 	crypto_release_minor(cm);
26350Sstevel@tonic-gate 
2636904Smcpowers 	if (real_provider != NULL) {
2637904Smcpowers 		crypto_free_mech(real_provider,
2638904Smcpowers 		    allocated_by_crypto_module, &mech);
2639904Smcpowers 		KCF_PROV_REFRELE(real_provider);
2640904Smcpowers 	}
26410Sstevel@tonic-gate 
26420Sstevel@tonic-gate 	free_crypto_key(&key);
26430Sstevel@tonic-gate 
26440Sstevel@tonic-gate 	if (error != 0)
2645904Smcpowers 		/* XXX free context */
26460Sstevel@tonic-gate 		return (error);
26470Sstevel@tonic-gate 
26480Sstevel@tonic-gate 	STRUCT_FSET(encrypt_init, ei_return_value, rv);
26490Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(encrypt_init), arg,
26500Sstevel@tonic-gate 	    STRUCT_SIZE(encrypt_init)) != 0) {
2651904Smcpowers 		/* XXX free context */
26520Sstevel@tonic-gate 		return (EFAULT);
26530Sstevel@tonic-gate 	}
26540Sstevel@tonic-gate 	return (0);
26550Sstevel@tonic-gate }
26560Sstevel@tonic-gate 
26570Sstevel@tonic-gate /* ARGSUSED */
26580Sstevel@tonic-gate static int
26590Sstevel@tonic-gate encrypt(dev_t dev, caddr_t arg, int mode, int *rval)
26600Sstevel@tonic-gate {
26610Sstevel@tonic-gate 	return (cipher(dev, arg, mode, crypto_encrypt_single));
26620Sstevel@tonic-gate }
26630Sstevel@tonic-gate 
26640Sstevel@tonic-gate /* ARGSUSED */
26650Sstevel@tonic-gate static int
26660Sstevel@tonic-gate decrypt(dev_t dev, caddr_t arg, int mode, int *rval)
26670Sstevel@tonic-gate {
26680Sstevel@tonic-gate 	return (cipher(dev, arg, mode, crypto_decrypt_single));
26690Sstevel@tonic-gate }
26700Sstevel@tonic-gate 
26710Sstevel@tonic-gate /*
26720Sstevel@tonic-gate  * ASSUMPTION: crypto_encrypt and crypto_decrypt structures
26730Sstevel@tonic-gate  * are identical except for field names.
26740Sstevel@tonic-gate  */
26750Sstevel@tonic-gate static int
26760Sstevel@tonic-gate cipher(dev_t dev, caddr_t arg, int mode,
26770Sstevel@tonic-gate     int (*single)(crypto_context_t, crypto_data_t *, crypto_data_t *,
26780Sstevel@tonic-gate     crypto_call_req_t *))
26790Sstevel@tonic-gate {
26800Sstevel@tonic-gate 	STRUCT_DECL(crypto_encrypt, encrypt);
26810Sstevel@tonic-gate 	crypto_session_id_t session_id;
26820Sstevel@tonic-gate 	crypto_minor_t *cm;
26836424Skrishna 	crypto_session_data_t *sp = NULL;
26840Sstevel@tonic-gate 	crypto_ctx_t **ctxpp;
26850Sstevel@tonic-gate 	crypto_data_t data, encr;
26860Sstevel@tonic-gate 	size_t datalen, encrlen, need = 0;
26874632Smcpowers 	boolean_t do_inplace;
26880Sstevel@tonic-gate 	char *encrbuf;
26890Sstevel@tonic-gate 	int error = 0;
26900Sstevel@tonic-gate 	int rv;
26916424Skrishna 	boolean_t rctl_chk = B_FALSE;
26920Sstevel@tonic-gate 
26930Sstevel@tonic-gate 	STRUCT_INIT(encrypt, mode);
26940Sstevel@tonic-gate 
26950Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
26960Sstevel@tonic-gate 		cmn_err(CE_WARN, "cipher: failed holding minor");
26970Sstevel@tonic-gate 		return (ENXIO);
26980Sstevel@tonic-gate 	}
26990Sstevel@tonic-gate 
27000Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(encrypt), STRUCT_SIZE(encrypt)) != 0) {
27010Sstevel@tonic-gate 		crypto_release_minor(cm);
27020Sstevel@tonic-gate 		return (EFAULT);
27030Sstevel@tonic-gate 	}
27040Sstevel@tonic-gate 
27050Sstevel@tonic-gate 	data.cd_raw.iov_base = NULL;
27060Sstevel@tonic-gate 	encr.cd_raw.iov_base = NULL;
27070Sstevel@tonic-gate 
27080Sstevel@tonic-gate 	datalen = STRUCT_FGET(encrypt, ce_datalen);
27090Sstevel@tonic-gate 	encrlen = STRUCT_FGET(encrypt, ce_encrlen);
27100Sstevel@tonic-gate 
27110Sstevel@tonic-gate 	/*
27120Sstevel@tonic-gate 	 * Don't allocate output buffer unless both buffer pointer and
27130Sstevel@tonic-gate 	 * buffer length are not NULL or 0 (length).
27140Sstevel@tonic-gate 	 */
27150Sstevel@tonic-gate 	encrbuf = STRUCT_FGETP(encrypt, ce_encrbuf);
27160Sstevel@tonic-gate 	if (encrbuf == NULL || encrlen == 0) {
27170Sstevel@tonic-gate 		encrlen = 0;
27180Sstevel@tonic-gate 	}
27190Sstevel@tonic-gate 
27200Sstevel@tonic-gate 	if (datalen > crypto_max_buffer_len ||
27210Sstevel@tonic-gate 	    encrlen > crypto_max_buffer_len) {
27220Sstevel@tonic-gate 		cmn_err(CE_NOTE, "cipher: buffer greater than %ld bytes, "
27230Sstevel@tonic-gate 		    "pid = %d", crypto_max_buffer_len, curproc->p_pid);
27240Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
27250Sstevel@tonic-gate 		goto release_minor;
27260Sstevel@tonic-gate 	}
27270Sstevel@tonic-gate 
27286424Skrishna 	session_id = STRUCT_FGET(encrypt, ce_session);
27296424Skrishna 
27306424Skrishna 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv))  {
27316424Skrishna 		goto release_minor;
27326424Skrishna 	}
27336867Skrishna 
27346867Skrishna 	do_inplace = (STRUCT_FGET(encrypt, ce_flags) &
27356867Skrishna 	    CRYPTO_INPLACE_OPERATION) != 0;
27366867Skrishna 	need = do_inplace ? datalen : datalen + encrlen;
27376867Skrishna 
27386424Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, need, rctl_chk)) !=
27396424Skrishna 	    CRYPTO_SUCCESS) {
27400Sstevel@tonic-gate 		need = 0;
27410Sstevel@tonic-gate 		goto release_minor;
27420Sstevel@tonic-gate 	}
27430Sstevel@tonic-gate 
27440Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(data, datalen);
27450Sstevel@tonic-gate 	data.cd_miscdata = NULL;
27460Sstevel@tonic-gate 
27470Sstevel@tonic-gate 	if (datalen != 0 && copyin(STRUCT_FGETP(encrypt, ce_databuf),
27480Sstevel@tonic-gate 	    data.cd_raw.iov_base, datalen) != 0) {
27490Sstevel@tonic-gate 		error = EFAULT;
27500Sstevel@tonic-gate 		goto release_minor;
27510Sstevel@tonic-gate 	}
27520Sstevel@tonic-gate 
27534632Smcpowers 	if (do_inplace) {
27544632Smcpowers 		/* set out = in for in-place */
27554632Smcpowers 		encr = data;
27564632Smcpowers 	} else {
27574632Smcpowers 		INIT_RAW_CRYPTO_DATA(encr, encrlen);
27584632Smcpowers 	}
27590Sstevel@tonic-gate 
27600Sstevel@tonic-gate 	ctxpp = (single == crypto_encrypt_single) ?
27610Sstevel@tonic-gate 	    &sp->sd_encr_ctx : &sp->sd_decr_ctx;
27620Sstevel@tonic-gate 
27634632Smcpowers 	/* in-place is specified by setting output NULL */
27644632Smcpowers 	if (do_inplace)
27654632Smcpowers 		rv = (single)(*ctxpp, &encr, NULL, NULL);
27664632Smcpowers 	else
27674632Smcpowers 		rv = (single)(*ctxpp, &data, &encr, NULL);
27680Sstevel@tonic-gate 	if (KCF_CONTEXT_DONE(rv))
27690Sstevel@tonic-gate 		*ctxpp = NULL;
27700Sstevel@tonic-gate 
27710Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
27720Sstevel@tonic-gate 		ASSERT(encr.cd_length <= encrlen);
27730Sstevel@tonic-gate 		if (encr.cd_length != 0 && copyout(encr.cd_raw.iov_base,
27740Sstevel@tonic-gate 		    encrbuf, encr.cd_length) != 0) {
27750Sstevel@tonic-gate 			error = EFAULT;
27760Sstevel@tonic-gate 			goto release_minor;
27770Sstevel@tonic-gate 		}
27780Sstevel@tonic-gate 		STRUCT_FSET(encrypt, ce_encrlen, encr.cd_length);
27790Sstevel@tonic-gate 	}
27800Sstevel@tonic-gate 
27810Sstevel@tonic-gate 	if (rv == CRYPTO_BUFFER_TOO_SMALL) {
27820Sstevel@tonic-gate 		/*
27830Sstevel@tonic-gate 		 * The providers return CRYPTO_BUFFER_TOO_SMALL even for case 1
27840Sstevel@tonic-gate 		 * of section 11.2 of the pkcs11 spec. We catch it here and
27850Sstevel@tonic-gate 		 * provide the correct pkcs11 return value.
27860Sstevel@tonic-gate 		 */
27870Sstevel@tonic-gate 		if (STRUCT_FGETP(encrypt, ce_encrbuf) == NULL)
27880Sstevel@tonic-gate 			rv = CRYPTO_SUCCESS;
27890Sstevel@tonic-gate 		STRUCT_FSET(encrypt, ce_encrlen, encr.cd_length);
27900Sstevel@tonic-gate 	}
27910Sstevel@tonic-gate 
27920Sstevel@tonic-gate release_minor:
27936424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
27946424Skrishna 	CRYPTO_SESSION_RELE(sp);
27950Sstevel@tonic-gate 	crypto_release_minor(cm);
27960Sstevel@tonic-gate 
27970Sstevel@tonic-gate 	if (data.cd_raw.iov_base != NULL)
27980Sstevel@tonic-gate 		kmem_free(data.cd_raw.iov_base, datalen);
27990Sstevel@tonic-gate 
28004632Smcpowers 	if (!do_inplace && encr.cd_raw.iov_base != NULL)
28010Sstevel@tonic-gate 		kmem_free(encr.cd_raw.iov_base, encrlen);
28020Sstevel@tonic-gate 
28030Sstevel@tonic-gate 	if (error != 0)
28040Sstevel@tonic-gate 		return (error);
28050Sstevel@tonic-gate 
28060Sstevel@tonic-gate 	STRUCT_FSET(encrypt, ce_return_value, rv);
28070Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(encrypt), arg, STRUCT_SIZE(encrypt)) != 0) {
28080Sstevel@tonic-gate 		return (EFAULT);
28090Sstevel@tonic-gate 	}
28100Sstevel@tonic-gate 	return (0);
28110Sstevel@tonic-gate }
28120Sstevel@tonic-gate 
28130Sstevel@tonic-gate /* ARGSUSED */
28140Sstevel@tonic-gate static int
28150Sstevel@tonic-gate encrypt_update(dev_t dev, caddr_t arg, int mode, int *rval)
28160Sstevel@tonic-gate {
28170Sstevel@tonic-gate 	return (cipher_update(dev, arg, mode, crypto_encrypt_update));
28180Sstevel@tonic-gate }
28190Sstevel@tonic-gate 
28200Sstevel@tonic-gate /* ARGSUSED */
28210Sstevel@tonic-gate static int
28220Sstevel@tonic-gate decrypt_update(dev_t dev, caddr_t arg, int mode, int *rval)
28230Sstevel@tonic-gate {
28240Sstevel@tonic-gate 	return (cipher_update(dev, arg, mode, crypto_decrypt_update));
28250Sstevel@tonic-gate }
28260Sstevel@tonic-gate 
28270Sstevel@tonic-gate /*
28280Sstevel@tonic-gate  * ASSUMPTION: crypto_encrypt_update and crypto_decrypt_update
28290Sstevel@tonic-gate  * structures are identical except for field names.
28300Sstevel@tonic-gate  */
28310Sstevel@tonic-gate static int
28320Sstevel@tonic-gate cipher_update(dev_t dev, caddr_t arg, int mode,
28330Sstevel@tonic-gate     int (*update)(crypto_context_t, crypto_data_t *, crypto_data_t *,
28340Sstevel@tonic-gate     crypto_call_req_t *))
28350Sstevel@tonic-gate {
28360Sstevel@tonic-gate 	STRUCT_DECL(crypto_encrypt_update, encrypt_update);
28370Sstevel@tonic-gate 	crypto_session_id_t session_id;
28380Sstevel@tonic-gate 	crypto_minor_t *cm;
28396424Skrishna 	crypto_session_data_t *sp = NULL;
28400Sstevel@tonic-gate 	crypto_ctx_t **ctxpp;
28410Sstevel@tonic-gate 	crypto_data_t data, encr;
28420Sstevel@tonic-gate 	size_t datalen, encrlen, need = 0;
28430Sstevel@tonic-gate 	char *encrbuf;
28440Sstevel@tonic-gate 	int error = 0;
28450Sstevel@tonic-gate 	int rv;
28466424Skrishna 	boolean_t rctl_chk = B_FALSE;
28470Sstevel@tonic-gate 
28480Sstevel@tonic-gate 	STRUCT_INIT(encrypt_update, mode);
28490Sstevel@tonic-gate 
28500Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
28510Sstevel@tonic-gate 		cmn_err(CE_WARN, "cipher_update: failed holding minor");
28520Sstevel@tonic-gate 		return (ENXIO);
28530Sstevel@tonic-gate 	}
28540Sstevel@tonic-gate 
28550Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(encrypt_update),
28560Sstevel@tonic-gate 	    STRUCT_SIZE(encrypt_update)) != 0) {
28570Sstevel@tonic-gate 		crypto_release_minor(cm);
28580Sstevel@tonic-gate 		return (EFAULT);
28590Sstevel@tonic-gate 	}
28600Sstevel@tonic-gate 
28610Sstevel@tonic-gate 	data.cd_raw.iov_base = NULL;
28620Sstevel@tonic-gate 	encr.cd_raw.iov_base = NULL;
28630Sstevel@tonic-gate 
28640Sstevel@tonic-gate 	datalen = STRUCT_FGET(encrypt_update, eu_datalen);
28650Sstevel@tonic-gate 	encrlen = STRUCT_FGET(encrypt_update, eu_encrlen);
28660Sstevel@tonic-gate 
28670Sstevel@tonic-gate 	/*
28680Sstevel@tonic-gate 	 * Don't allocate output buffer unless both buffer pointer and
28690Sstevel@tonic-gate 	 * buffer length are not NULL or 0 (length).
28700Sstevel@tonic-gate 	 */
28710Sstevel@tonic-gate 	encrbuf = STRUCT_FGETP(encrypt_update, eu_encrbuf);
28720Sstevel@tonic-gate 	if (encrbuf == NULL || encrlen == 0) {
28730Sstevel@tonic-gate 		encrlen = 0;
28740Sstevel@tonic-gate 	}
28750Sstevel@tonic-gate 
28760Sstevel@tonic-gate 	if (datalen > crypto_max_buffer_len ||
28770Sstevel@tonic-gate 	    encrlen > crypto_max_buffer_len) {
28780Sstevel@tonic-gate 		cmn_err(CE_NOTE, "cipher_update: buffer greater than %ld "
28790Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
28800Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
28816424Skrishna 		goto out;
28826424Skrishna 	}
28836424Skrishna 
28846424Skrishna 	session_id = STRUCT_FGET(encrypt_update, eu_session);
28856424Skrishna 
28866424Skrishna 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv))  {
28876424Skrishna 		goto out;
28880Sstevel@tonic-gate 	}
28890Sstevel@tonic-gate 
28900Sstevel@tonic-gate 	need = datalen + encrlen;
28916424Skrishna 
28926424Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, need, rctl_chk)) !=
28936424Skrishna 	    CRYPTO_SUCCESS) {
28940Sstevel@tonic-gate 		need = 0;
28956424Skrishna 		goto out;
28960Sstevel@tonic-gate 	}
28970Sstevel@tonic-gate 
28980Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(data, datalen);
28990Sstevel@tonic-gate 	data.cd_miscdata = NULL;
29000Sstevel@tonic-gate 
29010Sstevel@tonic-gate 	if (datalen != 0 && copyin(STRUCT_FGETP(encrypt_update, eu_databuf),
29020Sstevel@tonic-gate 	    data.cd_raw.iov_base, datalen) != 0) {
29030Sstevel@tonic-gate 		error = EFAULT;
29046424Skrishna 		goto out;
29050Sstevel@tonic-gate 	}
29060Sstevel@tonic-gate 
29070Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(encr, encrlen);
29080Sstevel@tonic-gate 
29090Sstevel@tonic-gate 	ctxpp = (update == crypto_encrypt_update) ?
29100Sstevel@tonic-gate 	    &sp->sd_encr_ctx : &sp->sd_decr_ctx;
29110Sstevel@tonic-gate 
29120Sstevel@tonic-gate 	rv = (update)(*ctxpp, &data, &encr, NULL);
29130Sstevel@tonic-gate 
29140Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS || rv == CRYPTO_BUFFER_TOO_SMALL) {
29150Sstevel@tonic-gate 		if (rv == CRYPTO_SUCCESS) {
29160Sstevel@tonic-gate 			ASSERT(encr.cd_length <= encrlen);
29170Sstevel@tonic-gate 			if (encr.cd_length != 0 && copyout(encr.cd_raw.iov_base,
29180Sstevel@tonic-gate 			    encrbuf, encr.cd_length) != 0) {
29190Sstevel@tonic-gate 				error = EFAULT;
29200Sstevel@tonic-gate 				goto out;
29210Sstevel@tonic-gate 			}
29220Sstevel@tonic-gate 		} else {
29230Sstevel@tonic-gate 			/*
29240Sstevel@tonic-gate 			 * The providers return CRYPTO_BUFFER_TOO_SMALL even
29250Sstevel@tonic-gate 			 * for case 1 of section 11.2 of the pkcs11 spec.
29260Sstevel@tonic-gate 			 * We catch it here and provide the correct pkcs11
29270Sstevel@tonic-gate 			 * return value.
29280Sstevel@tonic-gate 			 */
29290Sstevel@tonic-gate 			if (STRUCT_FGETP(encrypt_update, eu_encrbuf) == NULL)
29300Sstevel@tonic-gate 				rv = CRYPTO_SUCCESS;
29310Sstevel@tonic-gate 		}
29320Sstevel@tonic-gate 		STRUCT_FSET(encrypt_update, eu_encrlen, encr.cd_length);
29330Sstevel@tonic-gate 	} else {
29340Sstevel@tonic-gate 		CRYPTO_CANCEL_CTX(ctxpp);
29350Sstevel@tonic-gate 	}
29360Sstevel@tonic-gate out:
29376424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
29380Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
29390Sstevel@tonic-gate 	crypto_release_minor(cm);
29400Sstevel@tonic-gate 
29410Sstevel@tonic-gate 	if (data.cd_raw.iov_base != NULL)
29420Sstevel@tonic-gate 		kmem_free(data.cd_raw.iov_base, datalen);
29430Sstevel@tonic-gate 
29440Sstevel@tonic-gate 	if (encr.cd_raw.iov_base != NULL)
29450Sstevel@tonic-gate 		kmem_free(encr.cd_raw.iov_base, encrlen);
29460Sstevel@tonic-gate 
29470Sstevel@tonic-gate 	if (error != 0)
29480Sstevel@tonic-gate 		return (error);
29490Sstevel@tonic-gate 
29500Sstevel@tonic-gate 	STRUCT_FSET(encrypt_update, eu_return_value, rv);
29510Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(encrypt_update), arg,
29520Sstevel@tonic-gate 	    STRUCT_SIZE(encrypt_update)) != 0) {
29530Sstevel@tonic-gate 		return (EFAULT);
29540Sstevel@tonic-gate 	}
29550Sstevel@tonic-gate 	return (0);
29560Sstevel@tonic-gate }
29570Sstevel@tonic-gate 
29580Sstevel@tonic-gate /* ARGSUSED */
29590Sstevel@tonic-gate static int
29600Sstevel@tonic-gate encrypt_final(dev_t dev, caddr_t arg, int mode, int *rval)
29610Sstevel@tonic-gate {
29620Sstevel@tonic-gate 	return (common_final(dev, arg, mode, crypto_encrypt_final));
29630Sstevel@tonic-gate }
29640Sstevel@tonic-gate 
29650Sstevel@tonic-gate /* ARGSUSED */
29660Sstevel@tonic-gate static int
29670Sstevel@tonic-gate decrypt_final(dev_t dev, caddr_t arg, int mode, int *rval)
29680Sstevel@tonic-gate {
29690Sstevel@tonic-gate 	return (common_final(dev, arg, mode, crypto_decrypt_final));
29700Sstevel@tonic-gate }
29710Sstevel@tonic-gate 
29720Sstevel@tonic-gate /*
29730Sstevel@tonic-gate  * ASSUMPTION: crypto_encrypt_final, crypto_decrypt_final, crypto_sign_final,
29740Sstevel@tonic-gate  * and crypto_digest_final structures are identical except for field names.
29750Sstevel@tonic-gate  */
29760Sstevel@tonic-gate static int
29770Sstevel@tonic-gate common_final(dev_t dev, caddr_t arg, int mode,
29780Sstevel@tonic-gate     int (*final)(crypto_context_t, crypto_data_t *, crypto_call_req_t *))
29790Sstevel@tonic-gate {
29800Sstevel@tonic-gate 	STRUCT_DECL(crypto_encrypt_final, encrypt_final);
29810Sstevel@tonic-gate 	crypto_session_id_t session_id;
29820Sstevel@tonic-gate 	crypto_minor_t *cm;
29836424Skrishna 	crypto_session_data_t *sp = NULL;
29840Sstevel@tonic-gate 	crypto_ctx_t **ctxpp;
29850Sstevel@tonic-gate 	crypto_data_t encr;
29860Sstevel@tonic-gate 	size_t encrlen, need = 0;
29870Sstevel@tonic-gate 	char *encrbuf;
29880Sstevel@tonic-gate 	int error = 0;
29890Sstevel@tonic-gate 	int rv;
29906424Skrishna 	boolean_t rctl_chk = B_FALSE;
29910Sstevel@tonic-gate 
29920Sstevel@tonic-gate 	STRUCT_INIT(encrypt_final, mode);
29930Sstevel@tonic-gate 
29940Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
29950Sstevel@tonic-gate 		cmn_err(CE_WARN, "common_final: failed holding minor");
29960Sstevel@tonic-gate 		return (ENXIO);
29970Sstevel@tonic-gate 	}
29980Sstevel@tonic-gate 
29990Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(encrypt_final),
30000Sstevel@tonic-gate 	    STRUCT_SIZE(encrypt_final)) != 0) {
30010Sstevel@tonic-gate 		crypto_release_minor(cm);
30020Sstevel@tonic-gate 		return (EFAULT);
30030Sstevel@tonic-gate 	}
30040Sstevel@tonic-gate 
30050Sstevel@tonic-gate 	encr.cd_format = CRYPTO_DATA_RAW;
30060Sstevel@tonic-gate 	encr.cd_raw.iov_base = NULL;
30070Sstevel@tonic-gate 
30080Sstevel@tonic-gate 	encrlen = STRUCT_FGET(encrypt_final, ef_encrlen);
30090Sstevel@tonic-gate 
30100Sstevel@tonic-gate 	/*
30110Sstevel@tonic-gate 	 * Don't allocate output buffer unless both buffer pointer and
30120Sstevel@tonic-gate 	 * buffer length are not NULL or 0 (length).
30130Sstevel@tonic-gate 	 */
30140Sstevel@tonic-gate 	encrbuf = STRUCT_FGETP(encrypt_final, ef_encrbuf);
30150Sstevel@tonic-gate 	if (encrbuf == NULL || encrlen == 0) {
30160Sstevel@tonic-gate 		encrlen = 0;
30170Sstevel@tonic-gate 	}
30180Sstevel@tonic-gate 
30190Sstevel@tonic-gate 	if (encrlen > crypto_max_buffer_len) {
30200Sstevel@tonic-gate 		cmn_err(CE_NOTE, "common_final: buffer greater than %ld "
30210Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
30220Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
30230Sstevel@tonic-gate 		goto release_minor;
30240Sstevel@tonic-gate 	}
30250Sstevel@tonic-gate 
30266424Skrishna 	session_id = STRUCT_FGET(encrypt_final, ef_session);
30276424Skrishna 
30286424Skrishna 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
30296424Skrishna 		goto release_minor;
30306424Skrishna 	}
30316424Skrishna 
30326424Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, encrlen, rctl_chk)) !=
30336424Skrishna 	    CRYPTO_SUCCESS) {
30340Sstevel@tonic-gate 		goto release_minor;
30350Sstevel@tonic-gate 	}
30360Sstevel@tonic-gate 	need = encrlen;
30370Sstevel@tonic-gate 	encr.cd_raw.iov_base = kmem_alloc(encrlen, KM_SLEEP);
30380Sstevel@tonic-gate 	encr.cd_raw.iov_len = encrlen;
30390Sstevel@tonic-gate 
30400Sstevel@tonic-gate 	encr.cd_offset = 0;
30410Sstevel@tonic-gate 	encr.cd_length = encrlen;
30420Sstevel@tonic-gate 
30430Sstevel@tonic-gate 	ASSERT(final == crypto_encrypt_final ||
30440Sstevel@tonic-gate 	    final == crypto_decrypt_final || final == crypto_sign_final ||
30450Sstevel@tonic-gate 	    final == crypto_digest_final);
30460Sstevel@tonic-gate 
30470Sstevel@tonic-gate 	if (final == crypto_encrypt_final) {
30480Sstevel@tonic-gate 		ctxpp = &sp->sd_encr_ctx;
30490Sstevel@tonic-gate 	} else if (final == crypto_decrypt_final) {
30500Sstevel@tonic-gate 		ctxpp = &sp->sd_decr_ctx;
30510Sstevel@tonic-gate 	} else if (final == crypto_sign_final) {
30520Sstevel@tonic-gate 		ctxpp = &sp->sd_sign_ctx;
30530Sstevel@tonic-gate 	} else {
30540Sstevel@tonic-gate 		ctxpp = &sp->sd_digest_ctx;
30550Sstevel@tonic-gate 	}
30560Sstevel@tonic-gate 
30570Sstevel@tonic-gate 	rv = (final)(*ctxpp, &encr, NULL);
30580Sstevel@tonic-gate 	if (KCF_CONTEXT_DONE(rv))
30590Sstevel@tonic-gate 		*ctxpp = NULL;
30600Sstevel@tonic-gate 
30610Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
30620Sstevel@tonic-gate 		ASSERT(encr.cd_length <= encrlen);
30630Sstevel@tonic-gate 		if (encr.cd_length != 0 && copyout(encr.cd_raw.iov_base,
30640Sstevel@tonic-gate 		    encrbuf, encr.cd_length) != 0) {
30650Sstevel@tonic-gate 			error = EFAULT;
30660Sstevel@tonic-gate 			goto release_minor;
30670Sstevel@tonic-gate 		}
30680Sstevel@tonic-gate 		STRUCT_FSET(encrypt_final, ef_encrlen, encr.cd_length);
30690Sstevel@tonic-gate 	}
30700Sstevel@tonic-gate 
30710Sstevel@tonic-gate 	if (rv == CRYPTO_BUFFER_TOO_SMALL) {
30720Sstevel@tonic-gate 		/*
30730Sstevel@tonic-gate 		 * The providers return CRYPTO_BUFFER_TOO_SMALL even for case 1
30740Sstevel@tonic-gate 		 * of section 11.2 of the pkcs11 spec. We catch it here and
30750Sstevel@tonic-gate 		 * provide the correct pkcs11 return value.
30760Sstevel@tonic-gate 		 */
30770Sstevel@tonic-gate 		if (STRUCT_FGETP(encrypt_final, ef_encrbuf) == NULL)
30780Sstevel@tonic-gate 			rv = CRYPTO_SUCCESS;
30790Sstevel@tonic-gate 		STRUCT_FSET(encrypt_final, ef_encrlen, encr.cd_length);
30800Sstevel@tonic-gate 	}
30810Sstevel@tonic-gate 
30820Sstevel@tonic-gate release_minor:
30836424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
30846424Skrishna 	CRYPTO_SESSION_RELE(sp);
30850Sstevel@tonic-gate 	crypto_release_minor(cm);
30860Sstevel@tonic-gate 
30870Sstevel@tonic-gate 	if (encr.cd_raw.iov_base != NULL)
30880Sstevel@tonic-gate 		kmem_free(encr.cd_raw.iov_base, encrlen);
30890Sstevel@tonic-gate 
30900Sstevel@tonic-gate 	if (error != 0)
30910Sstevel@tonic-gate 		return (error);
30920Sstevel@tonic-gate 
30930Sstevel@tonic-gate 	STRUCT_FSET(encrypt_final, ef_return_value, rv);
30940Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(encrypt_final), arg,
30950Sstevel@tonic-gate 	    STRUCT_SIZE(encrypt_final)) != 0) {
30960Sstevel@tonic-gate 		return (EFAULT);
30970Sstevel@tonic-gate 	}
30980Sstevel@tonic-gate 	return (0);
30990Sstevel@tonic-gate }
31000Sstevel@tonic-gate 
31010Sstevel@tonic-gate /* ARGSUSED */
31020Sstevel@tonic-gate static int
31030Sstevel@tonic-gate digest_init(dev_t dev, caddr_t arg, int mode, int *rval)
31040Sstevel@tonic-gate {
31050Sstevel@tonic-gate 	STRUCT_DECL(crypto_digest_init, digest_init);
3106904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
31070Sstevel@tonic-gate 	crypto_session_id_t session_id;
31080Sstevel@tonic-gate 	crypto_mechanism_t mech;
31090Sstevel@tonic-gate 	crypto_minor_t *cm;
31106424Skrishna 	crypto_session_data_t *sp = NULL;
31110Sstevel@tonic-gate 	crypto_context_t cc;
31120Sstevel@tonic-gate 	size_t rctl_bytes = 0;
31136424Skrishna 	boolean_t rctl_chk = B_FALSE;
31140Sstevel@tonic-gate 	int error = 0;
31150Sstevel@tonic-gate 	int rv;
31160Sstevel@tonic-gate 
31170Sstevel@tonic-gate 	STRUCT_INIT(digest_init, mode);
31180Sstevel@tonic-gate 
31190Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
31200Sstevel@tonic-gate 		cmn_err(CE_WARN, "digest_init: failed holding minor");
31210Sstevel@tonic-gate 		return (ENXIO);
31220Sstevel@tonic-gate 	}
31230Sstevel@tonic-gate 
31240Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(digest_init),
31250Sstevel@tonic-gate 	    STRUCT_SIZE(digest_init)) != 0) {
31260Sstevel@tonic-gate 		crypto_release_minor(cm);
31270Sstevel@tonic-gate 		return (EFAULT);
31280Sstevel@tonic-gate 	}
31290Sstevel@tonic-gate 
31300Sstevel@tonic-gate 	mech.cm_param = NULL;
31310Sstevel@tonic-gate 
31320Sstevel@tonic-gate 	session_id = STRUCT_FGET(digest_init, di_session);
31330Sstevel@tonic-gate 
31340Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv))  {
31356424Skrishna 		goto out;
31366424Skrishna 	}
31376424Skrishna 
31386424Skrishna 	if (!copyin_mech(mode, sp, STRUCT_FADDR(digest_init, di_mech), &mech,
31396424Skrishna 	    &rctl_bytes, &rctl_chk, &rv, &error)) {
31400Sstevel@tonic-gate 		goto out;
31410Sstevel@tonic-gate 	}
31420Sstevel@tonic-gate 
3143904Smcpowers 	if ((rv = kcf_get_hardware_provider(mech.cm_type, CRYPTO_MECH_INVALID,
31441808Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider,
31451808Smcpowers 	    CRYPTO_FG_DIGEST)) != CRYPTO_SUCCESS) {
31460Sstevel@tonic-gate 		goto out;
31470Sstevel@tonic-gate 	}
31480Sstevel@tonic-gate 
31490Sstevel@tonic-gate 	rv = crypto_digest_init_prov(real_provider,
31500Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, &mech, &cc, NULL);
31510Sstevel@tonic-gate 
31520Sstevel@tonic-gate 	/*
31530Sstevel@tonic-gate 	 * Check if a context already exists. If so, it means it is being
31540Sstevel@tonic-gate 	 * abandoned. So, cancel it to avoid leaking it.
31550Sstevel@tonic-gate 	 */
31560Sstevel@tonic-gate 	if (sp->sd_digest_ctx != NULL)
31570Sstevel@tonic-gate 		CRYPTO_CANCEL_CTX(&sp->sd_digest_ctx);
31580Sstevel@tonic-gate 	sp->sd_digest_ctx = (rv == CRYPTO_SUCCESS) ? cc : NULL;
31590Sstevel@tonic-gate out:
31606424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk);
31610Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
31620Sstevel@tonic-gate 	crypto_release_minor(cm);
31630Sstevel@tonic-gate 
3164904Smcpowers 	if (real_provider != NULL)
3165904Smcpowers 		KCF_PROV_REFRELE(real_provider);
3166904Smcpowers 
31670Sstevel@tonic-gate 	if (mech.cm_param != NULL)
31680Sstevel@tonic-gate 		kmem_free(mech.cm_param, mech.cm_param_len);
31690Sstevel@tonic-gate 
31700Sstevel@tonic-gate 	if (error != 0)
31710Sstevel@tonic-gate 		return (error);
31720Sstevel@tonic-gate 
31730Sstevel@tonic-gate 	STRUCT_FSET(digest_init, di_return_value, rv);
31740Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(digest_init), arg,
31750Sstevel@tonic-gate 	    STRUCT_SIZE(digest_init)) != 0) {
31760Sstevel@tonic-gate 		return (EFAULT);
31770Sstevel@tonic-gate 	}
31780Sstevel@tonic-gate 	return (0);
31790Sstevel@tonic-gate }
31800Sstevel@tonic-gate 
31810Sstevel@tonic-gate /* ARGSUSED */
31820Sstevel@tonic-gate static int
31830Sstevel@tonic-gate digest_update(dev_t dev, caddr_t arg, int mode, int *rval)
31840Sstevel@tonic-gate {
31850Sstevel@tonic-gate 	STRUCT_DECL(crypto_digest_update, digest_update);
31860Sstevel@tonic-gate 	crypto_session_id_t session_id;
31870Sstevel@tonic-gate 	crypto_minor_t *cm;
31886424Skrishna 	crypto_session_data_t *sp = NULL;
31890Sstevel@tonic-gate 	crypto_data_t data;
31900Sstevel@tonic-gate 	size_t datalen, need = 0;
31910Sstevel@tonic-gate 	int error = 0;
31920Sstevel@tonic-gate 	int rv;
31936424Skrishna 	boolean_t rctl_chk = B_FALSE;
31940Sstevel@tonic-gate 
31950Sstevel@tonic-gate 	STRUCT_INIT(digest_update, mode);
31960Sstevel@tonic-gate 
31970Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
31980Sstevel@tonic-gate 		cmn_err(CE_WARN, "digest_update: failed holding minor");
31990Sstevel@tonic-gate 		return (ENXIO);
32000Sstevel@tonic-gate 	}
32010Sstevel@tonic-gate 
32020Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(digest_update),
32030Sstevel@tonic-gate 	    STRUCT_SIZE(digest_update)) != 0) {
32040Sstevel@tonic-gate 		crypto_release_minor(cm);
32050Sstevel@tonic-gate 		return (EFAULT);
32060Sstevel@tonic-gate 	}
32070Sstevel@tonic-gate 
32080Sstevel@tonic-gate 	data.cd_format = CRYPTO_DATA_RAW;
32090Sstevel@tonic-gate 	data.cd_raw.iov_base = NULL;
32100Sstevel@tonic-gate 
32110Sstevel@tonic-gate 	datalen = STRUCT_FGET(digest_update, du_datalen);
32120Sstevel@tonic-gate 	if (datalen > crypto_max_buffer_len) {
32130Sstevel@tonic-gate 		cmn_err(CE_NOTE, "digest_update: buffer greater than %ld "
32140Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
32150Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
32160Sstevel@tonic-gate 		goto release_minor;
32170Sstevel@tonic-gate 	}
32180Sstevel@tonic-gate 
32196424Skrishna 	session_id = STRUCT_FGET(digest_update, du_session);
32206424Skrishna 
32216424Skrishna 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv))  {
32220Sstevel@tonic-gate 		goto release_minor;
32230Sstevel@tonic-gate 	}
32246424Skrishna 
32256424Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, datalen, rctl_chk)) !=
32266424Skrishna 	    CRYPTO_SUCCESS) {
32276424Skrishna 		goto release_minor;
32286424Skrishna 	}
32296424Skrishna 
32300Sstevel@tonic-gate 	need = datalen;
32310Sstevel@tonic-gate 	data.cd_raw.iov_base = kmem_alloc(datalen, KM_SLEEP);
32320Sstevel@tonic-gate 	data.cd_raw.iov_len = datalen;
32330Sstevel@tonic-gate 
32340Sstevel@tonic-gate 	if (datalen != 0 && copyin(STRUCT_FGETP(digest_update, du_databuf),
32350Sstevel@tonic-gate 	    data.cd_raw.iov_base, datalen) != 0) {
32360Sstevel@tonic-gate 		error = EFAULT;
32370Sstevel@tonic-gate 		goto release_minor;
32380Sstevel@tonic-gate 	}
32390Sstevel@tonic-gate 
32400Sstevel@tonic-gate 	data.cd_offset = 0;
32410Sstevel@tonic-gate 	data.cd_length = datalen;
32420Sstevel@tonic-gate 
32430Sstevel@tonic-gate 	rv = crypto_digest_update(sp->sd_digest_ctx, &data, NULL);
32440Sstevel@tonic-gate 	if (rv != CRYPTO_SUCCESS)
32450Sstevel@tonic-gate 		CRYPTO_CANCEL_CTX(&sp->sd_digest_ctx);
32460Sstevel@tonic-gate 
32470Sstevel@tonic-gate release_minor:
32486424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
32496424Skrishna 	CRYPTO_SESSION_RELE(sp);
32500Sstevel@tonic-gate 	crypto_release_minor(cm);
32510Sstevel@tonic-gate 
32520Sstevel@tonic-gate 	if (data.cd_raw.iov_base != NULL)
32530Sstevel@tonic-gate 		kmem_free(data.cd_raw.iov_base, datalen);
32540Sstevel@tonic-gate 
32550Sstevel@tonic-gate 	if (error != 0)
32560Sstevel@tonic-gate 		return (error);
32570Sstevel@tonic-gate 
32580Sstevel@tonic-gate 	STRUCT_FSET(digest_update, du_return_value, rv);
32590Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(digest_update), arg,
32600Sstevel@tonic-gate 	    STRUCT_SIZE(digest_update)) != 0) {
32610Sstevel@tonic-gate 		return (EFAULT);
32620Sstevel@tonic-gate 	}
32630Sstevel@tonic-gate 	return (0);
32640Sstevel@tonic-gate }
32650Sstevel@tonic-gate 
32660Sstevel@tonic-gate /* ARGSUSED */
32670Sstevel@tonic-gate static int
32680Sstevel@tonic-gate digest_key(dev_t dev, caddr_t arg, int mode, int *rval)
32690Sstevel@tonic-gate {
32700Sstevel@tonic-gate 	STRUCT_DECL(crypto_digest_key, digest_key);
32710Sstevel@tonic-gate 	crypto_session_id_t session_id;
32720Sstevel@tonic-gate 	crypto_key_t key;
32730Sstevel@tonic-gate 	crypto_minor_t *cm;
32746424Skrishna 	crypto_session_data_t *sp = NULL;
32750Sstevel@tonic-gate 	size_t rctl_bytes = 0;
32766424Skrishna 	boolean_t key_rctl_chk = B_FALSE;
32770Sstevel@tonic-gate 	int error = 0;
32780Sstevel@tonic-gate 	int rv;
32790Sstevel@tonic-gate 
32800Sstevel@tonic-gate 	STRUCT_INIT(digest_key, mode);
32810Sstevel@tonic-gate 
32820Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
32830Sstevel@tonic-gate 		cmn_err(CE_WARN, "digest_key: failed holding minor");
32840Sstevel@tonic-gate 		return (ENXIO);
32850Sstevel@tonic-gate 	}
32860Sstevel@tonic-gate 
32870Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(digest_key), STRUCT_SIZE(digest_key)) != 0) {
32880Sstevel@tonic-gate 		crypto_release_minor(cm);
32890Sstevel@tonic-gate 		return (EFAULT);
32900Sstevel@tonic-gate 	}
32910Sstevel@tonic-gate 
32920Sstevel@tonic-gate 	bzero(&key, sizeof (crypto_key_t));
32930Sstevel@tonic-gate 
32940Sstevel@tonic-gate 	session_id = STRUCT_FGET(digest_key, dk_session);
32950Sstevel@tonic-gate 
32960Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv))  {
32976424Skrishna 		goto out;
32986424Skrishna 	}
32996424Skrishna 
33006424Skrishna 	if (!copyin_key(mode, sp, STRUCT_FADDR(digest_key, dk_key), &key,
33016424Skrishna 	    &rctl_bytes, &key_rctl_chk, &rv, &error)) {
33020Sstevel@tonic-gate 		goto out;
33030Sstevel@tonic-gate 	}
33040Sstevel@tonic-gate 
33050Sstevel@tonic-gate 	rv = crypto_digest_key_prov(sp->sd_digest_ctx, &key, NULL);
33060Sstevel@tonic-gate 	if (rv != CRYPTO_SUCCESS)
33070Sstevel@tonic-gate 		CRYPTO_CANCEL_CTX(&sp->sd_digest_ctx);
33080Sstevel@tonic-gate out:
33096424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, key_rctl_chk);
33100Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
33110Sstevel@tonic-gate 	crypto_release_minor(cm);
33120Sstevel@tonic-gate 
33130Sstevel@tonic-gate 	free_crypto_key(&key);
33140Sstevel@tonic-gate 
33150Sstevel@tonic-gate 	if (error != 0)
33160Sstevel@tonic-gate 		return (error);
33170Sstevel@tonic-gate 
33180Sstevel@tonic-gate 	STRUCT_FSET(digest_key, dk_return_value, rv);
33190Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(digest_key), arg,
33200Sstevel@tonic-gate 	    STRUCT_SIZE(digest_key)) != 0) {
33210Sstevel@tonic-gate 		return (EFAULT);
33220Sstevel@tonic-gate 	}
33230Sstevel@tonic-gate 	return (0);
33240Sstevel@tonic-gate }
33250Sstevel@tonic-gate 
33260Sstevel@tonic-gate /* ARGSUSED */
33270Sstevel@tonic-gate static int
33280Sstevel@tonic-gate digest_final(dev_t dev, caddr_t arg, int mode, int *rval)
33290Sstevel@tonic-gate {
33300Sstevel@tonic-gate 	return (common_final(dev, arg, mode, crypto_digest_final));
33310Sstevel@tonic-gate }
33320Sstevel@tonic-gate 
33330Sstevel@tonic-gate /* ARGSUSED */
33340Sstevel@tonic-gate static int
33350Sstevel@tonic-gate digest(dev_t dev, caddr_t arg, int mode, int *rval)
33360Sstevel@tonic-gate {
33370Sstevel@tonic-gate 	return (common_digest(dev, arg, mode, crypto_digest_single));
33380Sstevel@tonic-gate }
33390Sstevel@tonic-gate 
33400Sstevel@tonic-gate /*
33410Sstevel@tonic-gate  * ASSUMPTION: crypto_digest, crypto_sign, crypto_sign_recover,
33420Sstevel@tonic-gate  * and crypto_verify_recover are identical except for field names.
33430Sstevel@tonic-gate  */
33440Sstevel@tonic-gate static int
33450Sstevel@tonic-gate common_digest(dev_t dev, caddr_t arg, int mode,
33460Sstevel@tonic-gate     int (*single)(crypto_context_t, crypto_data_t *, crypto_data_t *,
33470Sstevel@tonic-gate     crypto_call_req_t *))
33480Sstevel@tonic-gate {
33490Sstevel@tonic-gate 	STRUCT_DECL(crypto_digest, crypto_digest);
33500Sstevel@tonic-gate 	crypto_session_id_t session_id;
33510Sstevel@tonic-gate 	crypto_minor_t *cm;
33526424Skrishna 	crypto_session_data_t *sp = NULL;
33530Sstevel@tonic-gate 	crypto_data_t data, digest;
33540Sstevel@tonic-gate 	crypto_ctx_t **ctxpp;
33550Sstevel@tonic-gate 	size_t datalen, digestlen, need = 0;
33560Sstevel@tonic-gate 	char *digestbuf;
33570Sstevel@tonic-gate 	int error = 0;
33580Sstevel@tonic-gate 	int rv;
33596424Skrishna 	boolean_t rctl_chk = B_FALSE;
33600Sstevel@tonic-gate 
33610Sstevel@tonic-gate 	STRUCT_INIT(crypto_digest, mode);
33620Sstevel@tonic-gate 
33630Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
33640Sstevel@tonic-gate 		cmn_err(CE_WARN, "common_digest: failed holding minor");
33650Sstevel@tonic-gate 		return (ENXIO);
33660Sstevel@tonic-gate 	}
33670Sstevel@tonic-gate 
33680Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(crypto_digest),
33690Sstevel@tonic-gate 	    STRUCT_SIZE(crypto_digest)) != 0) {
33700Sstevel@tonic-gate 		crypto_release_minor(cm);
33710Sstevel@tonic-gate 		return (EFAULT);
33720Sstevel@tonic-gate 	}
33730Sstevel@tonic-gate 
33740Sstevel@tonic-gate 	data.cd_raw.iov_base = NULL;
33750Sstevel@tonic-gate 	digest.cd_raw.iov_base = NULL;
33760Sstevel@tonic-gate 
33770Sstevel@tonic-gate 	datalen = STRUCT_FGET(crypto_digest, cd_datalen);
33780Sstevel@tonic-gate 	digestlen = STRUCT_FGET(crypto_digest, cd_digestlen);
33790Sstevel@tonic-gate 
33800Sstevel@tonic-gate 	/*
33810Sstevel@tonic-gate 	 * Don't allocate output buffer unless both buffer pointer and
33820Sstevel@tonic-gate 	 * buffer length are not NULL or 0 (length).
33830Sstevel@tonic-gate 	 */
33840Sstevel@tonic-gate 	digestbuf = STRUCT_FGETP(crypto_digest, cd_digestbuf);
33850Sstevel@tonic-gate 	if (digestbuf == NULL || digestlen == 0) {
33860Sstevel@tonic-gate 		digestlen = 0;
33870Sstevel@tonic-gate 	}
33880Sstevel@tonic-gate 
33890Sstevel@tonic-gate 	if (datalen > crypto_max_buffer_len ||
33900Sstevel@tonic-gate 	    digestlen > crypto_max_buffer_len) {
33910Sstevel@tonic-gate 		cmn_err(CE_NOTE, "common_digest: buffer greater than %ld "
33920Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
33930Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
33940Sstevel@tonic-gate 		goto release_minor;
33950Sstevel@tonic-gate 	}
33960Sstevel@tonic-gate 
33976424Skrishna 	session_id = STRUCT_FGET(crypto_digest, cd_session);
33986424Skrishna 
33996424Skrishna 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv))  {
34006424Skrishna 		goto release_minor;
34016424Skrishna 	}
34026424Skrishna 
34030Sstevel@tonic-gate 	need = datalen + digestlen;
34046424Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, need, rctl_chk)) !=
34056424Skrishna 	    CRYPTO_SUCCESS) {
34060Sstevel@tonic-gate 		need = 0;
34070Sstevel@tonic-gate 		goto release_minor;
34080Sstevel@tonic-gate 	}
34090Sstevel@tonic-gate 
34100Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(data, datalen);
34110Sstevel@tonic-gate 
34120Sstevel@tonic-gate 	if (datalen != 0 && copyin(STRUCT_FGETP(crypto_digest, cd_databuf),
34130Sstevel@tonic-gate 	    data.cd_raw.iov_base, datalen) != 0) {
34140Sstevel@tonic-gate 		error = EFAULT;
34150Sstevel@tonic-gate 		goto release_minor;
34160Sstevel@tonic-gate 	}
34170Sstevel@tonic-gate 
34180Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(digest, digestlen);
34190Sstevel@tonic-gate 
34200Sstevel@tonic-gate 	ASSERT(single == crypto_digest_single ||
34210Sstevel@tonic-gate 	    single == crypto_sign_single ||
34220Sstevel@tonic-gate 	    single == crypto_verify_recover_single ||
34230Sstevel@tonic-gate 	    single == crypto_sign_recover_single);
34240Sstevel@tonic-gate 
34250Sstevel@tonic-gate 	if (single == crypto_digest_single) {
34260Sstevel@tonic-gate 		ctxpp = &sp->sd_digest_ctx;
34270Sstevel@tonic-gate 	} else if (single == crypto_sign_single) {
34280Sstevel@tonic-gate 		ctxpp = &sp->sd_sign_ctx;
34290Sstevel@tonic-gate 	} else if (single == crypto_verify_recover_single) {
34300Sstevel@tonic-gate 		ctxpp = &sp->sd_verify_recover_ctx;
34310Sstevel@tonic-gate 	} else {
34320Sstevel@tonic-gate 		ctxpp = &sp->sd_sign_recover_ctx;
34330Sstevel@tonic-gate 	}
34340Sstevel@tonic-gate 	rv = (single)(*ctxpp, &data, &digest, NULL);
34350Sstevel@tonic-gate 	if (KCF_CONTEXT_DONE(rv))
34360Sstevel@tonic-gate 		*ctxpp = NULL;
34370Sstevel@tonic-gate 
34380Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
34390Sstevel@tonic-gate 		ASSERT(digest.cd_length <= digestlen);
34400Sstevel@tonic-gate 		if (digest.cd_length != 0 && copyout(digest.cd_raw.iov_base,
34410Sstevel@tonic-gate 		    digestbuf, digest.cd_length) != 0) {
34420Sstevel@tonic-gate 			error = EFAULT;
34430Sstevel@tonic-gate 			goto release_minor;
34440Sstevel@tonic-gate 		}
34450Sstevel@tonic-gate 		STRUCT_FSET(crypto_digest, cd_digestlen, digest.cd_length);
34460Sstevel@tonic-gate 	}
34470Sstevel@tonic-gate 
34480Sstevel@tonic-gate 	if (rv == CRYPTO_BUFFER_TOO_SMALL) {
34490Sstevel@tonic-gate 		/*
34500Sstevel@tonic-gate 		 * The providers return CRYPTO_BUFFER_TOO_SMALL even for case 1
34510Sstevel@tonic-gate 		 * of section 11.2 of the pkcs11 spec. We catch it here and
34520Sstevel@tonic-gate 		 * provide the correct pkcs11 return value.
34530Sstevel@tonic-gate 		 */
34540Sstevel@tonic-gate 		if (STRUCT_FGETP(crypto_digest, cd_digestbuf) == NULL)
34550Sstevel@tonic-gate 			rv = CRYPTO_SUCCESS;
34560Sstevel@tonic-gate 		STRUCT_FSET(crypto_digest, cd_digestlen, digest.cd_length);
34570Sstevel@tonic-gate 	}
34580Sstevel@tonic-gate 
34590Sstevel@tonic-gate release_minor:
34606424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
34616424Skrishna 	CRYPTO_SESSION_RELE(sp);
34620Sstevel@tonic-gate 	crypto_release_minor(cm);
34630Sstevel@tonic-gate 
34640Sstevel@tonic-gate 	if (data.cd_raw.iov_base != NULL)
34650Sstevel@tonic-gate 		kmem_free(data.cd_raw.iov_base, datalen);
34660Sstevel@tonic-gate 
34670Sstevel@tonic-gate 	if (digest.cd_raw.iov_base != NULL)
34680Sstevel@tonic-gate 		kmem_free(digest.cd_raw.iov_base, digestlen);
34690Sstevel@tonic-gate 
34700Sstevel@tonic-gate 	if (error != 0)
34710Sstevel@tonic-gate 		return (error);
34720Sstevel@tonic-gate 
34730Sstevel@tonic-gate 	STRUCT_FSET(crypto_digest, cd_return_value, rv);
34740Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(crypto_digest), arg,
34750Sstevel@tonic-gate 	    STRUCT_SIZE(crypto_digest)) != 0) {
34760Sstevel@tonic-gate 		return (EFAULT);
34770Sstevel@tonic-gate 	}
34780Sstevel@tonic-gate 	return (0);
34790Sstevel@tonic-gate }
34800Sstevel@tonic-gate 
34810Sstevel@tonic-gate /*
34820Sstevel@tonic-gate  * A helper function that does what the name suggests.
34830Sstevel@tonic-gate  * Returns 0 on success and non-zero otherwise.
34840Sstevel@tonic-gate  * On failure, out_pin is set to 0.
34850Sstevel@tonic-gate  */
34860Sstevel@tonic-gate int
34870Sstevel@tonic-gate get_pin_and_session_ptr(char *in_pin, char **out_pin, size_t pin_len,
34880Sstevel@tonic-gate     crypto_minor_t *cm, crypto_session_id_t sid, crypto_session_data_t **sp,
34890Sstevel@tonic-gate     int *rv, int *error)
34900Sstevel@tonic-gate {
34910Sstevel@tonic-gate 	char *tmp_pin = NULL;
34920Sstevel@tonic-gate 	int tmp_error = 0, tmp_rv = 0;
34930Sstevel@tonic-gate 
34940Sstevel@tonic-gate 	if (pin_len > KCF_MAX_PIN_LEN) {
34950Sstevel@tonic-gate 		tmp_rv = CRYPTO_PIN_LEN_RANGE;
34960Sstevel@tonic-gate 		goto out;
34970Sstevel@tonic-gate 	}
34980Sstevel@tonic-gate 	tmp_pin = kmem_alloc(pin_len, KM_SLEEP);
34990Sstevel@tonic-gate 
35000Sstevel@tonic-gate 	if (pin_len != 0 && copyin(in_pin, tmp_pin, pin_len) != 0) {
35010Sstevel@tonic-gate 		tmp_error = EFAULT;
35020Sstevel@tonic-gate 		goto out;
35030Sstevel@tonic-gate 	}
35040Sstevel@tonic-gate 
35050Sstevel@tonic-gate 	(void) get_session_ptr(sid, cm, sp, &tmp_error, &tmp_rv);
35060Sstevel@tonic-gate out:
35070Sstevel@tonic-gate 	*out_pin = tmp_pin;
35080Sstevel@tonic-gate 	*rv = tmp_rv;
35090Sstevel@tonic-gate 	*error = tmp_error;
35100Sstevel@tonic-gate 	return (tmp_rv | tmp_error);
35110Sstevel@tonic-gate }
35120Sstevel@tonic-gate 
35130Sstevel@tonic-gate /* ARGSUSED */
35140Sstevel@tonic-gate static int
35150Sstevel@tonic-gate set_pin(dev_t dev, caddr_t arg, int mode, int *rval)
35160Sstevel@tonic-gate {
35170Sstevel@tonic-gate 	STRUCT_DECL(crypto_set_pin, set_pin);
35180Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
35190Sstevel@tonic-gate 	kcf_req_params_t params;
35200Sstevel@tonic-gate 	crypto_minor_t *cm;
35210Sstevel@tonic-gate 	crypto_session_data_t *sp;
35220Sstevel@tonic-gate 	char *old_pin = NULL;
35230Sstevel@tonic-gate 	char *new_pin = NULL;
35240Sstevel@tonic-gate 	size_t old_pin_len;
35250Sstevel@tonic-gate 	size_t new_pin_len;
35260Sstevel@tonic-gate 	int error = 0;
35270Sstevel@tonic-gate 	int rv;
35280Sstevel@tonic-gate 
35290Sstevel@tonic-gate 	STRUCT_INIT(set_pin, mode);
35300Sstevel@tonic-gate 
35310Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
35320Sstevel@tonic-gate 		cmn_err(CE_WARN, "set_pin: failed holding minor");
35330Sstevel@tonic-gate 		return (ENXIO);
35340Sstevel@tonic-gate 	}
35350Sstevel@tonic-gate 
35360Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(set_pin),
35370Sstevel@tonic-gate 	    STRUCT_SIZE(set_pin)) != 0) {
35380Sstevel@tonic-gate 		crypto_release_minor(cm);
35390Sstevel@tonic-gate 		return (EFAULT);
35400Sstevel@tonic-gate 	}
35410Sstevel@tonic-gate 
35420Sstevel@tonic-gate 	old_pin_len = STRUCT_FGET(set_pin, sp_old_len);
35430Sstevel@tonic-gate 
35440Sstevel@tonic-gate 	if (get_pin_and_session_ptr(STRUCT_FGETP(set_pin, sp_old_pin),
35450Sstevel@tonic-gate 	    &old_pin, old_pin_len, cm, STRUCT_FGET(set_pin, sp_session),
35460Sstevel@tonic-gate 	    &sp, &rv, &error) != 0)
35470Sstevel@tonic-gate 		goto release_minor;
35480Sstevel@tonic-gate 
35490Sstevel@tonic-gate 	new_pin_len = STRUCT_FGET(set_pin, sp_new_len);
35500Sstevel@tonic-gate 	if (new_pin_len > KCF_MAX_PIN_LEN) {
35510Sstevel@tonic-gate 		rv = CRYPTO_PIN_LEN_RANGE;
35520Sstevel@tonic-gate 		goto out;
35530Sstevel@tonic-gate 	}
35540Sstevel@tonic-gate 	new_pin = kmem_alloc(new_pin_len, KM_SLEEP);
35550Sstevel@tonic-gate 
35560Sstevel@tonic-gate 	if (new_pin_len != 0 && copyin(STRUCT_FGETP(set_pin, sp_new_pin),
35570Sstevel@tonic-gate 	    new_pin, new_pin_len) != 0) {
35580Sstevel@tonic-gate 		error = EFAULT;
35590Sstevel@tonic-gate 		goto out;
35600Sstevel@tonic-gate 	}
35610Sstevel@tonic-gate 
35620Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
35630Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(provider_ops), CRYPTO_PROVIDER_OFFSET(set_pin),
3564904Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider))
3565904Smcpowers 	    != CRYPTO_SUCCESS) {
35660Sstevel@tonic-gate 		goto out;
35670Sstevel@tonic-gate 	}
35680Sstevel@tonic-gate 
35690Sstevel@tonic-gate 	KCF_WRAP_PROVMGMT_OPS_PARAMS(&params, KCF_OP_MGMT_SETPIN,
35700Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, old_pin, old_pin_len,
35710Sstevel@tonic-gate 	    new_pin, new_pin_len, NULL, NULL, real_provider);
35720Sstevel@tonic-gate 
35730Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
3574904Smcpowers 	KCF_PROV_REFRELE(real_provider);
35750Sstevel@tonic-gate 
35760Sstevel@tonic-gate out:
35770Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
35780Sstevel@tonic-gate 
35790Sstevel@tonic-gate release_minor:
35800Sstevel@tonic-gate 	crypto_release_minor(cm);
35810Sstevel@tonic-gate 
35820Sstevel@tonic-gate 	if (old_pin != NULL) {
35830Sstevel@tonic-gate 		bzero(old_pin, old_pin_len);
35840Sstevel@tonic-gate 		kmem_free(old_pin, old_pin_len);
35850Sstevel@tonic-gate 	}
35860Sstevel@tonic-gate 
35870Sstevel@tonic-gate 	if (new_pin != NULL) {
35880Sstevel@tonic-gate 		bzero(new_pin, new_pin_len);
35890Sstevel@tonic-gate 		kmem_free(new_pin, new_pin_len);
35900Sstevel@tonic-gate 	}
35910Sstevel@tonic-gate 
35920Sstevel@tonic-gate 	if (error != 0)
35930Sstevel@tonic-gate 		return (error);
35940Sstevel@tonic-gate 
35950Sstevel@tonic-gate 	STRUCT_FSET(set_pin, sp_return_value, rv);
35960Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(set_pin), arg, STRUCT_SIZE(set_pin)) != 0) {
35970Sstevel@tonic-gate 		return (EFAULT);
35980Sstevel@tonic-gate 	}
35990Sstevel@tonic-gate 	return (0);
36000Sstevel@tonic-gate }
36010Sstevel@tonic-gate 
36020Sstevel@tonic-gate /* ARGSUSED */
36030Sstevel@tonic-gate static int
36040Sstevel@tonic-gate login(dev_t dev, caddr_t arg, int mode, int *rval)
36050Sstevel@tonic-gate {
36060Sstevel@tonic-gate 	STRUCT_DECL(crypto_login, login);
36070Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
36080Sstevel@tonic-gate 	kcf_req_params_t params;
36090Sstevel@tonic-gate 	crypto_minor_t *cm;
36100Sstevel@tonic-gate 	crypto_session_data_t *sp;
36110Sstevel@tonic-gate 	size_t pin_len;
36120Sstevel@tonic-gate 	char *pin;
36130Sstevel@tonic-gate 	uint_t user_type;
36140Sstevel@tonic-gate 	int error = 0;
36150Sstevel@tonic-gate 	int rv;
36160Sstevel@tonic-gate 
36170Sstevel@tonic-gate 	STRUCT_INIT(login, mode);
36180Sstevel@tonic-gate 
36190Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
36200Sstevel@tonic-gate 		cmn_err(CE_WARN, "login: failed holding minor");
36210Sstevel@tonic-gate 		return (ENXIO);
36220Sstevel@tonic-gate 	}
36230Sstevel@tonic-gate 
36240Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(login), STRUCT_SIZE(login)) != 0) {
36250Sstevel@tonic-gate 		crypto_release_minor(cm);
36260Sstevel@tonic-gate 		return (EFAULT);
36270Sstevel@tonic-gate 	}
36280Sstevel@tonic-gate 
36290Sstevel@tonic-gate 	user_type = STRUCT_FGET(login, co_user_type);
36300Sstevel@tonic-gate 
36310Sstevel@tonic-gate 	pin_len = STRUCT_FGET(login, co_pin_len);
36320Sstevel@tonic-gate 
36330Sstevel@tonic-gate 	if (get_pin_and_session_ptr(STRUCT_FGETP(login, co_pin),
36340Sstevel@tonic-gate 	    &pin, pin_len, cm, STRUCT_FGET(login, co_session),
36350Sstevel@tonic-gate 	    &sp, &rv, &error) != 0) {
36360Sstevel@tonic-gate 		if (rv == CRYPTO_PIN_LEN_RANGE)
36370Sstevel@tonic-gate 			rv = CRYPTO_PIN_INCORRECT;
36380Sstevel@tonic-gate 		goto release_minor;
36390Sstevel@tonic-gate 	}
36400Sstevel@tonic-gate 
36410Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
36420Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(session_ops),
36430Sstevel@tonic-gate 	    CRYPTO_SESSION_OFFSET(session_login),
3644904Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider))
3645904Smcpowers 	    != CRYPTO_SUCCESS) {
36460Sstevel@tonic-gate 		goto out;
36470Sstevel@tonic-gate 	}
36480Sstevel@tonic-gate 
36490Sstevel@tonic-gate 	KCF_WRAP_SESSION_OPS_PARAMS(&params, KCF_OP_SESSION_LOGIN, NULL,
36500Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, user_type, pin, pin_len,
36510Sstevel@tonic-gate 	    real_provider);
36520Sstevel@tonic-gate 
36530Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
3654904Smcpowers 	KCF_PROV_REFRELE(real_provider);
36550Sstevel@tonic-gate 
36560Sstevel@tonic-gate out:
36570Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
36580Sstevel@tonic-gate 
36590Sstevel@tonic-gate release_minor:
36600Sstevel@tonic-gate 	crypto_release_minor(cm);
36610Sstevel@tonic-gate 
36620Sstevel@tonic-gate 	if (pin != NULL) {
36630Sstevel@tonic-gate 		bzero(pin, pin_len);
36640Sstevel@tonic-gate 		kmem_free(pin, pin_len);
36650Sstevel@tonic-gate 	}
36660Sstevel@tonic-gate 
36670Sstevel@tonic-gate 	if (error != 0)
36680Sstevel@tonic-gate 		return (error);
36690Sstevel@tonic-gate 
36700Sstevel@tonic-gate 	STRUCT_FSET(login, co_return_value, rv);
36710Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(login), arg, STRUCT_SIZE(login)) != 0) {
36720Sstevel@tonic-gate 		return (EFAULT);
36730Sstevel@tonic-gate 	}
36740Sstevel@tonic-gate 	return (0);
36750Sstevel@tonic-gate }
36760Sstevel@tonic-gate 
36770Sstevel@tonic-gate /* ARGSUSED */
36780Sstevel@tonic-gate static int
36790Sstevel@tonic-gate logout(dev_t dev, caddr_t arg, int mode, int *rval)
36800Sstevel@tonic-gate {
36810Sstevel@tonic-gate 	crypto_logout_t logout;
36820Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
36830Sstevel@tonic-gate 	kcf_req_params_t params;
36840Sstevel@tonic-gate 	crypto_minor_t *cm;
36850Sstevel@tonic-gate 	crypto_session_data_t *sp;
36860Sstevel@tonic-gate 	int error = 0;
36870Sstevel@tonic-gate 	int rv;
36880Sstevel@tonic-gate 
36890Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
36900Sstevel@tonic-gate 		cmn_err(CE_WARN, "logout: failed holding minor");
36910Sstevel@tonic-gate 		return (ENXIO);
36920Sstevel@tonic-gate 	}
36930Sstevel@tonic-gate 
36940Sstevel@tonic-gate 	if (copyin(arg, &logout, sizeof (logout)) != 0) {
36950Sstevel@tonic-gate 		crypto_release_minor(cm);
36960Sstevel@tonic-gate 		return (EFAULT);
36970Sstevel@tonic-gate 	}
36980Sstevel@tonic-gate 
36990Sstevel@tonic-gate 	if (!get_session_ptr(logout.cl_session, cm, &sp, &error, &rv))  {
37000Sstevel@tonic-gate 		goto release_minor;
37010Sstevel@tonic-gate 	}
37020Sstevel@tonic-gate 
37030Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
37040Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(session_ops),
3705904Smcpowers 	    CRYPTO_SESSION_OFFSET(session_logout), CHECK_RESTRICT_FALSE,
37060Sstevel@tonic-gate 	    sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
37070Sstevel@tonic-gate 		goto out;
37080Sstevel@tonic-gate 	}
37090Sstevel@tonic-gate 
37100Sstevel@tonic-gate 	KCF_WRAP_SESSION_OPS_PARAMS(&params, KCF_OP_SESSION_LOGOUT, NULL,
37110Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, 0, NULL, 0, real_provider);
37120Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
3713904Smcpowers 	KCF_PROV_REFRELE(real_provider);
37140Sstevel@tonic-gate 
37150Sstevel@tonic-gate out:
37160Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
37170Sstevel@tonic-gate 
37180Sstevel@tonic-gate release_minor:
37190Sstevel@tonic-gate 	crypto_release_minor(cm);
37200Sstevel@tonic-gate 
37210Sstevel@tonic-gate 	if (error != 0)
37220Sstevel@tonic-gate 		return (error);
37230Sstevel@tonic-gate 
37240Sstevel@tonic-gate 	logout.cl_return_value = rv;
37250Sstevel@tonic-gate 	if (copyout(&logout, arg, sizeof (logout)) != 0) {
37260Sstevel@tonic-gate 		return (EFAULT);
37270Sstevel@tonic-gate 	}
37280Sstevel@tonic-gate 	return (0);
37290Sstevel@tonic-gate }
37300Sstevel@tonic-gate 
37310Sstevel@tonic-gate /* ARGSUSED */
37320Sstevel@tonic-gate static int
37330Sstevel@tonic-gate sign_init(dev_t dev, caddr_t arg, int mode, int *rval)
37340Sstevel@tonic-gate {
37350Sstevel@tonic-gate 	return (sign_verify_init(dev, arg, mode, crypto_sign_init_prov));
37360Sstevel@tonic-gate }
37370Sstevel@tonic-gate 
37380Sstevel@tonic-gate /* ARGSUSED */
37390Sstevel@tonic-gate static int
37400Sstevel@tonic-gate sign_recover_init(dev_t dev, caddr_t arg, int mode, int *rval)
37410Sstevel@tonic-gate {
37420Sstevel@tonic-gate 	return (sign_verify_init(dev, arg, mode,
37430Sstevel@tonic-gate 	    crypto_sign_recover_init_prov));
37440Sstevel@tonic-gate }
37450Sstevel@tonic-gate 
37460Sstevel@tonic-gate /* ARGSUSED */
37470Sstevel@tonic-gate static int
37480Sstevel@tonic-gate verify_init(dev_t dev, caddr_t arg, int mode, int *rval)
37490Sstevel@tonic-gate {
37500Sstevel@tonic-gate 	return (sign_verify_init(dev, arg, mode, crypto_verify_init_prov));
37510Sstevel@tonic-gate }
37520Sstevel@tonic-gate 
37530Sstevel@tonic-gate /* ARGSUSED */
37540Sstevel@tonic-gate static int
37550Sstevel@tonic-gate verify_recover_init(dev_t dev, caddr_t arg, int mode, int *rval)
37560Sstevel@tonic-gate {
37570Sstevel@tonic-gate 	return (sign_verify_init(dev, arg, mode,
37580Sstevel@tonic-gate 	    crypto_verify_recover_init_prov));
37590Sstevel@tonic-gate }
37600Sstevel@tonic-gate 
37610Sstevel@tonic-gate /*
37620Sstevel@tonic-gate  * ASSUMPTION: crypto_sign_init, crypto_verify_init, crypto_sign_recover_init,
37630Sstevel@tonic-gate  * and crypto_verify_recover_init structures are identical
37640Sstevel@tonic-gate  * except for field names.
37650Sstevel@tonic-gate  */
37660Sstevel@tonic-gate static int
37670Sstevel@tonic-gate sign_verify_init(dev_t dev, caddr_t arg, int mode,
3768904Smcpowers     int (*init)(crypto_provider_t, crypto_session_id_t,
37690Sstevel@tonic-gate     crypto_mechanism_t *, crypto_key_t *, crypto_ctx_template_t,
37700Sstevel@tonic-gate     crypto_context_t *, crypto_call_req_t *))
37710Sstevel@tonic-gate {
37720Sstevel@tonic-gate 	STRUCT_DECL(crypto_sign_init, sign_init);
3773904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
37740Sstevel@tonic-gate 	crypto_session_id_t session_id;
37750Sstevel@tonic-gate 	crypto_mechanism_t mech;
37760Sstevel@tonic-gate 	crypto_key_t key;
37770Sstevel@tonic-gate 	crypto_minor_t *cm;
37786424Skrishna 	crypto_session_data_t *sp = NULL;
37790Sstevel@tonic-gate 	crypto_context_t cc;
37800Sstevel@tonic-gate 	crypto_ctx_t **ctxpp;
37810Sstevel@tonic-gate 	size_t mech_rctl_bytes = 0;
37826424Skrishna 	boolean_t mech_rctl_chk = B_FALSE;
37830Sstevel@tonic-gate 	size_t key_rctl_bytes = 0;
37846424Skrishna 	boolean_t key_rctl_chk = B_FALSE;
37850Sstevel@tonic-gate 	int error = 0;
37860Sstevel@tonic-gate 	int rv;
3787904Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
37881808Smcpowers 	crypto_func_group_t fg;
37890Sstevel@tonic-gate 
37900Sstevel@tonic-gate 	STRUCT_INIT(sign_init, mode);
37910Sstevel@tonic-gate 
37920Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
37930Sstevel@tonic-gate 		cmn_err(CE_WARN, "sign_verify_init: failed holding minor");
37940Sstevel@tonic-gate 		return (ENXIO);
37950Sstevel@tonic-gate 	}
37960Sstevel@tonic-gate 
37970Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(sign_init), STRUCT_SIZE(sign_init)) != 0) {
37980Sstevel@tonic-gate 		crypto_release_minor(cm);
37990Sstevel@tonic-gate 		return (EFAULT);
38000Sstevel@tonic-gate 	}
38010Sstevel@tonic-gate 
38020Sstevel@tonic-gate 	mech.cm_param = NULL;
38030Sstevel@tonic-gate 	bzero(&key, sizeof (key));
38040Sstevel@tonic-gate 
38050Sstevel@tonic-gate 	session_id = STRUCT_FGET(sign_init, si_session);
38060Sstevel@tonic-gate 
38070Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
38086424Skrishna 		goto out;
38090Sstevel@tonic-gate 	}
38100Sstevel@tonic-gate 
3811904Smcpowers 	bcopy(STRUCT_FADDR(sign_init, si_mech), &mech.cm_type,
3812904Smcpowers 	    sizeof (crypto_mech_type_t));
38130Sstevel@tonic-gate 
38140Sstevel@tonic-gate 	ASSERT(init == crypto_sign_init_prov ||
38150Sstevel@tonic-gate 	    init == crypto_verify_init_prov ||
38160Sstevel@tonic-gate 	    init == crypto_sign_recover_init_prov ||
38170Sstevel@tonic-gate 	    init == crypto_verify_recover_init_prov);
38180Sstevel@tonic-gate 
38190Sstevel@tonic-gate 	if (init == crypto_sign_init_prov) {
38201808Smcpowers 		fg =  CRYPTO_FG_SIGN;
38210Sstevel@tonic-gate 		ctxpp = &sp->sd_sign_ctx;
38220Sstevel@tonic-gate 	} else if (init == crypto_verify_init_prov) {
38231808Smcpowers 		fg =  CRYPTO_FG_VERIFY;
38240Sstevel@tonic-gate 		ctxpp = &sp->sd_verify_ctx;
38250Sstevel@tonic-gate 	} else if (init == crypto_sign_recover_init_prov) {
38261808Smcpowers 		fg =  CRYPTO_FG_SIGN_RECOVER;
38270Sstevel@tonic-gate 		ctxpp = &sp->sd_sign_recover_ctx;
38280Sstevel@tonic-gate 	} else {
38291808Smcpowers 		fg =  CRYPTO_FG_VERIFY_RECOVER;
38300Sstevel@tonic-gate 		ctxpp = &sp->sd_verify_recover_ctx;
38310Sstevel@tonic-gate 	}
38320Sstevel@tonic-gate 
3833904Smcpowers 	if ((rv = kcf_get_hardware_provider(mech.cm_type, CRYPTO_MECH_INVALID,
38341808Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider, fg))
38351808Smcpowers 	    != CRYPTO_SUCCESS) {
38360Sstevel@tonic-gate 		goto out;
38370Sstevel@tonic-gate 	}
38380Sstevel@tonic-gate 
3839904Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
3840904Smcpowers 	    STRUCT_FADDR(sign_init, si_mech), &mech, mode, &error);
3841904Smcpowers 
3842904Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
3843904Smcpowers 		allocated_by_crypto_module = B_TRUE;
38446424Skrishna 		if (!copyin_mech(mode, sp, STRUCT_FADDR(sign_init, si_mech),
38456424Skrishna 		    &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) {
3846904Smcpowers 			goto out;
3847904Smcpowers 		}
3848904Smcpowers 	} else {
3849904Smcpowers 		if (rv != CRYPTO_SUCCESS)
3850904Smcpowers 			goto out;
3851904Smcpowers 	}
3852904Smcpowers 
38536424Skrishna 	if (!copyin_key(mode, sp, STRUCT_FADDR(sign_init, si_key), &key,
38546424Skrishna 	    &key_rctl_bytes, &key_rctl_chk, &rv, &error)) {
38550Sstevel@tonic-gate 		goto out;
38560Sstevel@tonic-gate 	}
38570Sstevel@tonic-gate 
38580Sstevel@tonic-gate 	rv = (init)(real_provider, sp->sd_provider_session->ps_session,
38590Sstevel@tonic-gate 	    &mech, &key, NULL, &cc, NULL);
38600Sstevel@tonic-gate 
38610Sstevel@tonic-gate 	/*
38620Sstevel@tonic-gate 	 * Check if a context already exists. If so, it means it is being
38630Sstevel@tonic-gate 	 * abandoned. So, cancel it to avoid leaking it.
38640Sstevel@tonic-gate 	 */
38650Sstevel@tonic-gate 	if (*ctxpp != NULL)
38660Sstevel@tonic-gate 		CRYPTO_CANCEL_CTX(ctxpp);
38670Sstevel@tonic-gate 	*ctxpp = (rv == CRYPTO_SUCCESS) ? cc : NULL;
38680Sstevel@tonic-gate 
38690Sstevel@tonic-gate out:
38706424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
38716424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk);
38720Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
38730Sstevel@tonic-gate 	crypto_release_minor(cm);
38740Sstevel@tonic-gate 
3875904Smcpowers 	if (real_provider != NULL) {
3876904Smcpowers 		crypto_free_mech(real_provider,
3877904Smcpowers 		    allocated_by_crypto_module, &mech);
3878904Smcpowers 		KCF_PROV_REFRELE(real_provider);
3879904Smcpowers 	}
38800Sstevel@tonic-gate 
38810Sstevel@tonic-gate 	free_crypto_key(&key);
38820Sstevel@tonic-gate 
38830Sstevel@tonic-gate 	if (error != 0)
38840Sstevel@tonic-gate 		return (error);
38850Sstevel@tonic-gate 
38860Sstevel@tonic-gate 	STRUCT_FSET(sign_init, si_return_value, rv);
38870Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(sign_init), arg, STRUCT_SIZE(sign_init)) != 0) {
38880Sstevel@tonic-gate 		return (EFAULT);
38890Sstevel@tonic-gate 	}
38900Sstevel@tonic-gate 	return (0);
38910Sstevel@tonic-gate }
38920Sstevel@tonic-gate 
38930Sstevel@tonic-gate /* ARGSUSED */
38940Sstevel@tonic-gate static int
38950Sstevel@tonic-gate sign(dev_t dev, caddr_t arg, int mode, int *rval)
38960Sstevel@tonic-gate {
38970Sstevel@tonic-gate 	return (common_digest(dev, arg, mode, crypto_sign_single));
38980Sstevel@tonic-gate }
38990Sstevel@tonic-gate 
39000Sstevel@tonic-gate /* ARGSUSED */
39010Sstevel@tonic-gate static int
39020Sstevel@tonic-gate sign_recover(dev_t dev, caddr_t arg, int mode, int *rval)
39030Sstevel@tonic-gate {
39040Sstevel@tonic-gate 	return (common_digest(dev, arg, mode, crypto_sign_recover_single));
39050Sstevel@tonic-gate }
39060Sstevel@tonic-gate 
39070Sstevel@tonic-gate /* ARGSUSED */
39080Sstevel@tonic-gate static int
39090Sstevel@tonic-gate verify(dev_t dev, caddr_t arg, int mode, int *rval)
39100Sstevel@tonic-gate {
39110Sstevel@tonic-gate 	STRUCT_DECL(crypto_verify, verify);
39120Sstevel@tonic-gate 	crypto_session_id_t session_id;
39130Sstevel@tonic-gate 	crypto_minor_t *cm;
39146424Skrishna 	crypto_session_data_t *sp = NULL;
39150Sstevel@tonic-gate 	crypto_data_t data, sign;
39160Sstevel@tonic-gate 	size_t datalen, signlen, need = 0;
39170Sstevel@tonic-gate 	int error = 0;
39180Sstevel@tonic-gate 	int rv;
39196424Skrishna 	boolean_t rctl_chk = B_FALSE;
39200Sstevel@tonic-gate 
39210Sstevel@tonic-gate 	STRUCT_INIT(verify, mode);
39220Sstevel@tonic-gate 
39230Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
39240Sstevel@tonic-gate 		cmn_err(CE_WARN, "verify: failed holding minor");
39250Sstevel@tonic-gate 		return (ENXIO);
39260Sstevel@tonic-gate 	}
39270Sstevel@tonic-gate 
39280Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(verify), STRUCT_SIZE(verify)) != 0) {
39290Sstevel@tonic-gate 		crypto_release_minor(cm);
39300Sstevel@tonic-gate 		return (EFAULT);
39310Sstevel@tonic-gate 	}
39320Sstevel@tonic-gate 
39330Sstevel@tonic-gate 	data.cd_raw.iov_base = NULL;
39340Sstevel@tonic-gate 	sign.cd_raw.iov_base = NULL;
39350Sstevel@tonic-gate 
39360Sstevel@tonic-gate 	datalen = STRUCT_FGET(verify, cv_datalen);
39370Sstevel@tonic-gate 	signlen = STRUCT_FGET(verify, cv_signlen);
39380Sstevel@tonic-gate 	if (datalen > crypto_max_buffer_len ||
39390Sstevel@tonic-gate 	    signlen > crypto_max_buffer_len) {
39400Sstevel@tonic-gate 		cmn_err(CE_NOTE, "verify: buffer greater than %ld bytes, "
39410Sstevel@tonic-gate 		"pid = %d", crypto_max_buffer_len, curproc->p_pid);
39420Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
39430Sstevel@tonic-gate 		goto release_minor;
39440Sstevel@tonic-gate 	}
39450Sstevel@tonic-gate 
39466424Skrishna 	session_id = STRUCT_FGET(verify, cv_session);
39476424Skrishna 
39486424Skrishna 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
39496424Skrishna 		goto release_minor;
39506424Skrishna 	}
39516424Skrishna 
39520Sstevel@tonic-gate 	need = datalen + signlen;
39536424Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, need, rctl_chk)) !=
39546424Skrishna 	    CRYPTO_SUCCESS) {
39550Sstevel@tonic-gate 		need = 0;
39560Sstevel@tonic-gate 		goto release_minor;
39570Sstevel@tonic-gate 	}
39580Sstevel@tonic-gate 
39590Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(data, datalen);
39600Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(sign, signlen);
39610Sstevel@tonic-gate 
39620Sstevel@tonic-gate 	if (datalen != 0 && copyin(STRUCT_FGETP(verify, cv_databuf),
39630Sstevel@tonic-gate 	    data.cd_raw.iov_base, datalen) != 0) {
39640Sstevel@tonic-gate 		error = EFAULT;
39650Sstevel@tonic-gate 		goto release_minor;
39660Sstevel@tonic-gate 	}
39670Sstevel@tonic-gate 
39680Sstevel@tonic-gate 	if (signlen != 0 && copyin(STRUCT_FGETP(verify, cv_signbuf),
39690Sstevel@tonic-gate 	    sign.cd_raw.iov_base, signlen) != 0) {
39700Sstevel@tonic-gate 		error = EFAULT;
39710Sstevel@tonic-gate 		goto release_minor;
39720Sstevel@tonic-gate 	}
39730Sstevel@tonic-gate 
39740Sstevel@tonic-gate 	rv = crypto_verify_single(sp->sd_verify_ctx, &data, &sign, NULL);
39750Sstevel@tonic-gate 	if (KCF_CONTEXT_DONE(rv))
39760Sstevel@tonic-gate 		sp->sd_verify_ctx = NULL;
39770Sstevel@tonic-gate 
39786424Skrishna release_minor:
39796424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
39800Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
39810Sstevel@tonic-gate 	crypto_release_minor(cm);
39820Sstevel@tonic-gate 
39830Sstevel@tonic-gate 	if (data.cd_raw.iov_base != NULL)
39840Sstevel@tonic-gate 		kmem_free(data.cd_raw.iov_base, datalen);
39850Sstevel@tonic-gate 
39860Sstevel@tonic-gate 	if (sign.cd_raw.iov_base != NULL)
39870Sstevel@tonic-gate 		kmem_free(sign.cd_raw.iov_base, signlen);
39880Sstevel@tonic-gate 
39890Sstevel@tonic-gate 	if (error != 0)
39900Sstevel@tonic-gate 		return (error);
39910Sstevel@tonic-gate 
39920Sstevel@tonic-gate 	STRUCT_FSET(verify, cv_return_value, rv);
39930Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(verify), arg, STRUCT_SIZE(verify)) != 0) {
39940Sstevel@tonic-gate 		return (EFAULT);
39950Sstevel@tonic-gate 	}
39960Sstevel@tonic-gate 	return (0);
39970Sstevel@tonic-gate }
39980Sstevel@tonic-gate 
39990Sstevel@tonic-gate /* ARGSUSED */
40000Sstevel@tonic-gate static int
40010Sstevel@tonic-gate verify_recover(dev_t dev, caddr_t arg, int mode, int *rval)
40020Sstevel@tonic-gate {
40030Sstevel@tonic-gate 	return (common_digest(dev, arg, mode, crypto_verify_recover_single));
40040Sstevel@tonic-gate }
40050Sstevel@tonic-gate 
40060Sstevel@tonic-gate /* ARGSUSED */
40070Sstevel@tonic-gate static int
40080Sstevel@tonic-gate sign_update(dev_t dev, caddr_t arg, int mode, int *rval)
40090Sstevel@tonic-gate {
40100Sstevel@tonic-gate 	return (sign_verify_update(dev, arg, mode, crypto_sign_update));
40110Sstevel@tonic-gate }
40120Sstevel@tonic-gate 
40130Sstevel@tonic-gate /* ARGSUSED */
40140Sstevel@tonic-gate static int
40150Sstevel@tonic-gate verify_update(dev_t dev, caddr_t arg, int mode, int *rval)
40160Sstevel@tonic-gate {
40170Sstevel@tonic-gate 	return (sign_verify_update(dev, arg, mode, crypto_verify_update));
40180Sstevel@tonic-gate }
40190Sstevel@tonic-gate 
40200Sstevel@tonic-gate /*
40210Sstevel@tonic-gate  * ASSUMPTION: crypto_sign_update and crypto_verify_update structures
40220Sstevel@tonic-gate  * are identical except for field names.
40230Sstevel@tonic-gate  */
40240Sstevel@tonic-gate static int
40250Sstevel@tonic-gate sign_verify_update(dev_t dev, caddr_t arg, int mode,
40260Sstevel@tonic-gate     int (*update)(crypto_context_t, crypto_data_t *, crypto_call_req_t *))
40270Sstevel@tonic-gate {
40280Sstevel@tonic-gate 	STRUCT_DECL(crypto_sign_update, sign_update);
40290Sstevel@tonic-gate 	crypto_session_id_t session_id;
40300Sstevel@tonic-gate 	crypto_minor_t *cm;
40316424Skrishna 	crypto_session_data_t *sp = NULL;
40320Sstevel@tonic-gate 	crypto_ctx_t **ctxpp;
40330Sstevel@tonic-gate 	crypto_data_t data;
40340Sstevel@tonic-gate 	size_t datalen, need = 0;
40350Sstevel@tonic-gate 	int error = 0;
40360Sstevel@tonic-gate 	int rv;
40376424Skrishna 	boolean_t rctl_chk = B_FALSE;
40380Sstevel@tonic-gate 
40390Sstevel@tonic-gate 	STRUCT_INIT(sign_update, mode);
40400Sstevel@tonic-gate 
40410Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
40420Sstevel@tonic-gate 		cmn_err(CE_WARN, "sign_verify_update: failed holding minor");
40430Sstevel@tonic-gate 		return (ENXIO);
40440Sstevel@tonic-gate 	}
40450Sstevel@tonic-gate 
40460Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(sign_update),
40470Sstevel@tonic-gate 	    STRUCT_SIZE(sign_update)) != 0) {
40480Sstevel@tonic-gate 		crypto_release_minor(cm);
40490Sstevel@tonic-gate 		return (EFAULT);
40500Sstevel@tonic-gate 	}
40510Sstevel@tonic-gate 
40520Sstevel@tonic-gate 	data.cd_raw.iov_base = NULL;
40530Sstevel@tonic-gate 
40540Sstevel@tonic-gate 	datalen = STRUCT_FGET(sign_update, su_datalen);
40550Sstevel@tonic-gate 	if (datalen > crypto_max_buffer_len) {
40560Sstevel@tonic-gate 		cmn_err(CE_NOTE, "sign_verify_update: buffer greater than %ld "
40570Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
40580Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
40590Sstevel@tonic-gate 		goto release_minor;
40600Sstevel@tonic-gate 	}
40610Sstevel@tonic-gate 
40626424Skrishna 	session_id = STRUCT_FGET(sign_update, su_session);
40636424Skrishna 
40646424Skrishna 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
40656424Skrishna 		goto release_minor;
40666424Skrishna 	}
40676424Skrishna 
40686424Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, datalen, rctl_chk)) !=
40696424Skrishna 	    CRYPTO_SUCCESS) {
40700Sstevel@tonic-gate 		goto release_minor;
40710Sstevel@tonic-gate 	}
40720Sstevel@tonic-gate 	need = datalen;
40730Sstevel@tonic-gate 
40740Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(data, datalen);
40750Sstevel@tonic-gate 
40760Sstevel@tonic-gate 	if (datalen != 0 && copyin(STRUCT_FGETP(sign_update, su_databuf),
40770Sstevel@tonic-gate 	    data.cd_raw.iov_base, datalen) != 0) {
40780Sstevel@tonic-gate 		error = EFAULT;
40790Sstevel@tonic-gate 		goto release_minor;
40800Sstevel@tonic-gate 	}
40810Sstevel@tonic-gate 
40820Sstevel@tonic-gate 	ctxpp = (update == crypto_sign_update) ?
40830Sstevel@tonic-gate 	    &sp->sd_sign_ctx : &sp->sd_verify_ctx;
40840Sstevel@tonic-gate 
40850Sstevel@tonic-gate 	rv = (update)(*ctxpp, &data, NULL);
40860Sstevel@tonic-gate 	if (rv != CRYPTO_SUCCESS)
40870Sstevel@tonic-gate 		CRYPTO_CANCEL_CTX(ctxpp);
40880Sstevel@tonic-gate 
40890Sstevel@tonic-gate release_minor:
40906424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
40916424Skrishna 	CRYPTO_SESSION_RELE(sp);
40920Sstevel@tonic-gate 	crypto_release_minor(cm);
40930Sstevel@tonic-gate 
40940Sstevel@tonic-gate 	if (data.cd_raw.iov_base != NULL)
40950Sstevel@tonic-gate 		kmem_free(data.cd_raw.iov_base, datalen);
40960Sstevel@tonic-gate 
40970Sstevel@tonic-gate 	if (error != 0)
40980Sstevel@tonic-gate 		return (error);
40990Sstevel@tonic-gate 
41000Sstevel@tonic-gate 	STRUCT_FSET(sign_update, su_return_value, rv);
41010Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(sign_update), arg,
41020Sstevel@tonic-gate 	    STRUCT_SIZE(sign_update)) != 0) {
41030Sstevel@tonic-gate 		return (EFAULT);
41040Sstevel@tonic-gate 	}
41050Sstevel@tonic-gate 	return (0);
41060Sstevel@tonic-gate }
41070Sstevel@tonic-gate 
41080Sstevel@tonic-gate /* ARGSUSED */
41090Sstevel@tonic-gate static int
41100Sstevel@tonic-gate sign_final(dev_t dev, caddr_t arg, int mode, int *rval)
41110Sstevel@tonic-gate {
41120Sstevel@tonic-gate 	return (common_final(dev, arg, mode, crypto_sign_final));
41130Sstevel@tonic-gate }
41140Sstevel@tonic-gate 
41150Sstevel@tonic-gate /*
41160Sstevel@tonic-gate  * Can't use the common final because it does a copyout of
41170Sstevel@tonic-gate  * the final part.
41180Sstevel@tonic-gate  */
41190Sstevel@tonic-gate /* ARGSUSED */
41200Sstevel@tonic-gate static int
41210Sstevel@tonic-gate verify_final(dev_t dev, caddr_t arg, int mode, int *rval)
41220Sstevel@tonic-gate {
41230Sstevel@tonic-gate 	STRUCT_DECL(crypto_verify_final, verify_final);
41240Sstevel@tonic-gate 	crypto_session_id_t session_id;
41250Sstevel@tonic-gate 	crypto_minor_t *cm;
41266424Skrishna 	crypto_session_data_t *sp = NULL;
41270Sstevel@tonic-gate 	crypto_data_t sign;
41280Sstevel@tonic-gate 	size_t signlen, need = 0;
41290Sstevel@tonic-gate 	int error = 0;
41300Sstevel@tonic-gate 	int rv;
41316424Skrishna 	boolean_t rctl_chk = B_FALSE;
41320Sstevel@tonic-gate 
41330Sstevel@tonic-gate 	STRUCT_INIT(verify_final, mode);
41340Sstevel@tonic-gate 
41350Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
41360Sstevel@tonic-gate 		cmn_err(CE_WARN, "verify_final: failed holding minor");
41370Sstevel@tonic-gate 		return (ENXIO);
41380Sstevel@tonic-gate 	}
41390Sstevel@tonic-gate 
41400Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(verify_final),
41410Sstevel@tonic-gate 	    STRUCT_SIZE(verify_final)) != 0) {
41420Sstevel@tonic-gate 		crypto_release_minor(cm);
41430Sstevel@tonic-gate 		return (EFAULT);
41440Sstevel@tonic-gate 	}
41450Sstevel@tonic-gate 
41460Sstevel@tonic-gate 	sign.cd_raw.iov_base = NULL;
41470Sstevel@tonic-gate 
41480Sstevel@tonic-gate 	signlen = STRUCT_FGET(verify_final, vf_signlen);
41490Sstevel@tonic-gate 	if (signlen > crypto_max_buffer_len) {
41500Sstevel@tonic-gate 		cmn_err(CE_NOTE, "verify_final: buffer greater than %ld "
41510Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
41520Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
41530Sstevel@tonic-gate 		goto release_minor;
41540Sstevel@tonic-gate 	}
41550Sstevel@tonic-gate 
41566424Skrishna 	session_id = STRUCT_FGET(verify_final, vf_session);
41576424Skrishna 
41586424Skrishna 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
41596424Skrishna 		goto release_minor;
41606424Skrishna 	}
41616424Skrishna 
41626424Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, signlen, rctl_chk)) !=
41636424Skrishna 	    CRYPTO_SUCCESS) {
41640Sstevel@tonic-gate 		goto release_minor;
41650Sstevel@tonic-gate 	}
41660Sstevel@tonic-gate 	need = signlen;
41670Sstevel@tonic-gate 
41680Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(sign, signlen);
41690Sstevel@tonic-gate 
41700Sstevel@tonic-gate 	if (signlen != 0 && copyin(STRUCT_FGETP(verify_final, vf_signbuf),
41710Sstevel@tonic-gate 	    sign.cd_raw.iov_base, signlen) != 0) {
41720Sstevel@tonic-gate 		error = EFAULT;
41730Sstevel@tonic-gate 		goto release_minor;
41740Sstevel@tonic-gate 	}
41750Sstevel@tonic-gate 
41760Sstevel@tonic-gate 	rv = crypto_verify_final(sp->sd_verify_ctx, &sign, NULL);
41770Sstevel@tonic-gate 	if (KCF_CONTEXT_DONE(rv))
41780Sstevel@tonic-gate 		sp->sd_verify_ctx = NULL;
41790Sstevel@tonic-gate 
41806424Skrishna release_minor:
41816424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
41820Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
41830Sstevel@tonic-gate 	crypto_release_minor(cm);
41840Sstevel@tonic-gate 
41850Sstevel@tonic-gate 	if (sign.cd_raw.iov_base != NULL)
41860Sstevel@tonic-gate 		kmem_free(sign.cd_raw.iov_base, signlen);
41870Sstevel@tonic-gate 
41880Sstevel@tonic-gate 	if (error != 0)
41890Sstevel@tonic-gate 		return (error);
41900Sstevel@tonic-gate 
41910Sstevel@tonic-gate 	STRUCT_FSET(verify_final, vf_return_value, rv);
41920Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(verify_final), arg,
41930Sstevel@tonic-gate 	    STRUCT_SIZE(verify_final)) != 0) {
41940Sstevel@tonic-gate 		return (EFAULT);
41950Sstevel@tonic-gate 	}
41960Sstevel@tonic-gate 	return (0);
41970Sstevel@tonic-gate }
41980Sstevel@tonic-gate 
41990Sstevel@tonic-gate /* ARGSUSED */
42000Sstevel@tonic-gate static int
42010Sstevel@tonic-gate seed_random(dev_t dev, caddr_t arg, int mode, int *rval)
42020Sstevel@tonic-gate {
42030Sstevel@tonic-gate 	STRUCT_DECL(crypto_seed_random, seed_random);
4204904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
42050Sstevel@tonic-gate 	kcf_req_params_t params;
42060Sstevel@tonic-gate 	crypto_session_id_t session_id;
42070Sstevel@tonic-gate 	crypto_minor_t *cm;
42086424Skrishna 	crypto_session_data_t *sp = NULL;
42090Sstevel@tonic-gate 	uchar_t *seed_buffer = NULL;
42100Sstevel@tonic-gate 	size_t seed_len;
42110Sstevel@tonic-gate 	size_t need = 0;
42120Sstevel@tonic-gate 	int error = 0;
42130Sstevel@tonic-gate 	int rv;
42146424Skrishna 	boolean_t rctl_chk = B_FALSE;
42150Sstevel@tonic-gate 
42160Sstevel@tonic-gate 	STRUCT_INIT(seed_random, mode);
42170Sstevel@tonic-gate 
42180Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
42190Sstevel@tonic-gate 		cmn_err(CE_WARN, "seed_random: failed holding minor");
42200Sstevel@tonic-gate 		return (ENXIO);
42210Sstevel@tonic-gate 	}
42220Sstevel@tonic-gate 
42230Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(seed_random),
42240Sstevel@tonic-gate 	    STRUCT_SIZE(seed_random)) != 0) {
42250Sstevel@tonic-gate 		crypto_release_minor(cm);
42260Sstevel@tonic-gate 		return (EFAULT);
42270Sstevel@tonic-gate 	}
42280Sstevel@tonic-gate 
42290Sstevel@tonic-gate 	seed_len = STRUCT_FGET(seed_random, sr_seedlen);
42300Sstevel@tonic-gate 	if (seed_len > crypto_max_buffer_len) {
42310Sstevel@tonic-gate 		cmn_err(CE_NOTE, "seed_random: buffer greater than %ld "
42320Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
42330Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
42340Sstevel@tonic-gate 		goto release_minor;
42350Sstevel@tonic-gate 	}
42360Sstevel@tonic-gate 
42376424Skrishna 	session_id = STRUCT_FGET(seed_random, sr_session);
42386424Skrishna 
42396424Skrishna 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
42406424Skrishna 		goto release_minor;
42416424Skrishna 	}
42426424Skrishna 
42436424Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, seed_len, rctl_chk)) !=
42446424Skrishna 	    CRYPTO_SUCCESS) {
42450Sstevel@tonic-gate 		goto release_minor;
42460Sstevel@tonic-gate 	}
42470Sstevel@tonic-gate 	need = seed_len;
42480Sstevel@tonic-gate 	seed_buffer = kmem_alloc(seed_len, KM_SLEEP);
42490Sstevel@tonic-gate 
42500Sstevel@tonic-gate 	if (seed_len != 0 && copyin(STRUCT_FGETP(seed_random, sr_seedbuf),
42510Sstevel@tonic-gate 	    seed_buffer, seed_len) != 0) {
42520Sstevel@tonic-gate 		error = EFAULT;
42530Sstevel@tonic-gate 		goto release_minor;
42540Sstevel@tonic-gate 	}
42550Sstevel@tonic-gate 
42561808Smcpowers 	if ((rv = kcf_get_hardware_provider_nomech(
4257904Smcpowers 	    CRYPTO_OPS_OFFSET(random_ops), CRYPTO_RANDOM_OFFSET(seed_random),
4258904Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider))
4259904Smcpowers 	    != CRYPTO_SUCCESS) {
42606424Skrishna 		goto release_minor;
42610Sstevel@tonic-gate 	}
42620Sstevel@tonic-gate 
42630Sstevel@tonic-gate 	KCF_WRAP_RANDOM_OPS_PARAMS(&params, KCF_OP_RANDOM_SEED,
42641920Smcpowers 	    sp->sd_provider_session->ps_session, seed_buffer, seed_len, 0,
42651920Smcpowers 	    CRYPTO_SEED_NOW);
42660Sstevel@tonic-gate 
42670Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
42680Sstevel@tonic-gate 
42696424Skrishna release_minor:
42706424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
42710Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
42720Sstevel@tonic-gate 	crypto_release_minor(cm);
42730Sstevel@tonic-gate 
4274904Smcpowers 	if (real_provider != NULL)
4275904Smcpowers 		KCF_PROV_REFRELE(real_provider);
4276904Smcpowers 
42770Sstevel@tonic-gate 	if (seed_buffer != NULL)
42780Sstevel@tonic-gate 		kmem_free(seed_buffer, seed_len);
42790Sstevel@tonic-gate 
42800Sstevel@tonic-gate 	if (error != 0)
42810Sstevel@tonic-gate 		return (error);
42820Sstevel@tonic-gate 
42830Sstevel@tonic-gate 	STRUCT_FSET(seed_random, sr_return_value, rv);
42840Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(seed_random), arg,
42850Sstevel@tonic-gate 	    STRUCT_SIZE(seed_random)) != 0) {
42860Sstevel@tonic-gate 		return (EFAULT);
42870Sstevel@tonic-gate 	}
42880Sstevel@tonic-gate 	return (0);
42890Sstevel@tonic-gate }
42900Sstevel@tonic-gate 
42910Sstevel@tonic-gate /* ARGSUSED */
42920Sstevel@tonic-gate static int
42930Sstevel@tonic-gate generate_random(dev_t dev, caddr_t arg, int mode, int *rval)
42940Sstevel@tonic-gate {
42950Sstevel@tonic-gate 	STRUCT_DECL(crypto_generate_random, generate_random);
4296904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
42970Sstevel@tonic-gate 	kcf_req_params_t params;
42980Sstevel@tonic-gate 	crypto_session_id_t session_id;
42990Sstevel@tonic-gate 	crypto_minor_t *cm;
43006424Skrishna 	crypto_session_data_t *sp = NULL;
43010Sstevel@tonic-gate 	uchar_t *buffer = NULL;
43020Sstevel@tonic-gate 	size_t len;
43030Sstevel@tonic-gate 	size_t need = 0;
43040Sstevel@tonic-gate 	int error = 0;
43050Sstevel@tonic-gate 	int rv;
43066424Skrishna 	boolean_t rctl_chk = B_FALSE;
43070Sstevel@tonic-gate 
43080Sstevel@tonic-gate 	STRUCT_INIT(generate_random, mode);
43090Sstevel@tonic-gate 
43100Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
43110Sstevel@tonic-gate 		cmn_err(CE_WARN, "generate_random: failed holding minor");
43120Sstevel@tonic-gate 		return (ENXIO);
43130Sstevel@tonic-gate 	}
43140Sstevel@tonic-gate 
43150Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(generate_random),
43160Sstevel@tonic-gate 	    STRUCT_SIZE(generate_random)) != 0) {
43170Sstevel@tonic-gate 		crypto_release_minor(cm);
43180Sstevel@tonic-gate 		return (EFAULT);
43190Sstevel@tonic-gate 	}
43200Sstevel@tonic-gate 
43210Sstevel@tonic-gate 	len = STRUCT_FGET(generate_random, gr_buflen);
43220Sstevel@tonic-gate 	if (len > crypto_max_buffer_len) {
43230Sstevel@tonic-gate 		cmn_err(CE_NOTE, "generate_random: buffer greater than %ld "
43240Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
43250Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
43260Sstevel@tonic-gate 		goto release_minor;
43270Sstevel@tonic-gate 	}
43280Sstevel@tonic-gate 
43296424Skrishna 	session_id = STRUCT_FGET(generate_random, gr_session);
43306424Skrishna 
43316424Skrishna 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
43326424Skrishna 		goto release_minor;
43336424Skrishna 	}
43346424Skrishna 
43356424Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, len, rctl_chk)) !=
43366424Skrishna 	    CRYPTO_SUCCESS) {
43370Sstevel@tonic-gate 		goto release_minor;
43380Sstevel@tonic-gate 	}
43390Sstevel@tonic-gate 	need = len;
43400Sstevel@tonic-gate 	buffer = kmem_alloc(len, KM_SLEEP);
43410Sstevel@tonic-gate 
43421808Smcpowers 	if ((rv = kcf_get_hardware_provider_nomech(
43430Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(random_ops),
4344904Smcpowers 	    CRYPTO_RANDOM_OFFSET(generate_random), CHECK_RESTRICT_FALSE,
43450Sstevel@tonic-gate 	    sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
43466424Skrishna 		goto release_minor;
43470Sstevel@tonic-gate 	}
43480Sstevel@tonic-gate 
43490Sstevel@tonic-gate 	KCF_WRAP_RANDOM_OPS_PARAMS(&params, KCF_OP_RANDOM_GENERATE,
43501920Smcpowers 	    sp->sd_provider_session->ps_session, buffer, len, 0, 0);
43510Sstevel@tonic-gate 
43520Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
43530Sstevel@tonic-gate 
43540Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
43550Sstevel@tonic-gate 		if (len != 0 && copyout(buffer,
43560Sstevel@tonic-gate 		    STRUCT_FGETP(generate_random, gr_buf), len) != 0) {
43570Sstevel@tonic-gate 			error = EFAULT;
43580Sstevel@tonic-gate 		}
43590Sstevel@tonic-gate 	}
43600Sstevel@tonic-gate 
43610Sstevel@tonic-gate release_minor:
43626424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
43636424Skrishna 	CRYPTO_SESSION_RELE(sp);
43640Sstevel@tonic-gate 	crypto_release_minor(cm);
43650Sstevel@tonic-gate 
4366904Smcpowers 	if (real_provider != NULL)
4367904Smcpowers 		KCF_PROV_REFRELE(real_provider);
4368904Smcpowers 
43690Sstevel@tonic-gate 	if (buffer != NULL) {
43700Sstevel@tonic-gate 		/* random numbers are often used to create keys */
43710Sstevel@tonic-gate 		bzero(buffer, len);
43720Sstevel@tonic-gate 		kmem_free(buffer, len);
43730Sstevel@tonic-gate 	}
43740Sstevel@tonic-gate 
43750Sstevel@tonic-gate 	if (error != 0)
43760Sstevel@tonic-gate 		return (error);
43770Sstevel@tonic-gate 
43780Sstevel@tonic-gate 	STRUCT_FSET(generate_random, gr_return_value, rv);
43790Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(generate_random), arg,
43800Sstevel@tonic-gate 	    STRUCT_SIZE(generate_random)) != 0) {
43810Sstevel@tonic-gate 		return (EFAULT);
43820Sstevel@tonic-gate 	}
43830Sstevel@tonic-gate 	return (0);
43840Sstevel@tonic-gate }
43850Sstevel@tonic-gate 
43860Sstevel@tonic-gate /*
43870Sstevel@tonic-gate  * Copyout a kernel array of attributes to user space.
43880Sstevel@tonic-gate  * u_attrs is the corresponding user space array containing
43890Sstevel@tonic-gate  * user space pointers necessary for the copyout.
43900Sstevel@tonic-gate  */
43910Sstevel@tonic-gate /* ARGSUSED */
43920Sstevel@tonic-gate static int
43930Sstevel@tonic-gate copyout_attributes(int mode, caddr_t out, uint_t count,
43940Sstevel@tonic-gate     crypto_object_attribute_t *k_attrs, caddr_t u_attrs)
43950Sstevel@tonic-gate {
43960Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_attribute, oa);
43970Sstevel@tonic-gate 	caddr_t p, valuep;
43980Sstevel@tonic-gate 	size_t value_len;
43990Sstevel@tonic-gate 	size_t len;
44000Sstevel@tonic-gate 	int i;
44010Sstevel@tonic-gate 	int error = 0;
44020Sstevel@tonic-gate 
44030Sstevel@tonic-gate 	if (count == 0)
44040Sstevel@tonic-gate 		return (0);
44050Sstevel@tonic-gate 
44060Sstevel@tonic-gate 	STRUCT_INIT(oa, mode);
44070Sstevel@tonic-gate 
44080Sstevel@tonic-gate 	len = count * STRUCT_SIZE(oa);
44090Sstevel@tonic-gate 
44100Sstevel@tonic-gate 	ASSERT(u_attrs != NULL);
44110Sstevel@tonic-gate 	p = u_attrs;
44120Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
44130Sstevel@tonic-gate 		/* can this bcopy be eliminated? */
44140Sstevel@tonic-gate 		bcopy(p, STRUCT_BUF(oa), STRUCT_SIZE(oa));
44150Sstevel@tonic-gate 		value_len = k_attrs[i].oa_value_len;
44160Sstevel@tonic-gate 		STRUCT_FSET(oa, oa_type, k_attrs[i].oa_type);
44170Sstevel@tonic-gate 		STRUCT_FSET(oa, oa_value_len, value_len);
44180Sstevel@tonic-gate 		valuep = STRUCT_FGETP(oa, oa_value);
44190Sstevel@tonic-gate 		if (valuep != NULL && value_len != -1) {
44200Sstevel@tonic-gate 			if (copyout(k_attrs[i].oa_value,
44210Sstevel@tonic-gate 			    valuep, value_len) != 0) {
44220Sstevel@tonic-gate 				error = EFAULT;
44230Sstevel@tonic-gate 				goto out;
44240Sstevel@tonic-gate 			}
44250Sstevel@tonic-gate 		}
44260Sstevel@tonic-gate 		bcopy(STRUCT_BUF(oa), p, STRUCT_SIZE(oa));
44270Sstevel@tonic-gate 		p += STRUCT_SIZE(oa);
44280Sstevel@tonic-gate 	}
44290Sstevel@tonic-gate 	if (copyout(u_attrs, out, len)) {
44300Sstevel@tonic-gate 		error = EFAULT;
44310Sstevel@tonic-gate 	}
44320Sstevel@tonic-gate out:
44330Sstevel@tonic-gate 	return (error);
44340Sstevel@tonic-gate }
44350Sstevel@tonic-gate 
44360Sstevel@tonic-gate 
44370Sstevel@tonic-gate /* ARGSUSED */
44380Sstevel@tonic-gate static int
44390Sstevel@tonic-gate object_create(dev_t dev, caddr_t arg, int mode, int *rval)
44400Sstevel@tonic-gate {
44410Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_create, object_create);
4442904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
44430Sstevel@tonic-gate 	kcf_req_params_t params;
44440Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
44450Sstevel@tonic-gate 	crypto_session_id_t session_id;
44460Sstevel@tonic-gate 	crypto_minor_t *cm;
44470Sstevel@tonic-gate 	crypto_session_data_t *sp = NULL;
44480Sstevel@tonic-gate 	crypto_object_id_t object_handle;
44490Sstevel@tonic-gate 	caddr_t oc_attributes;
44500Sstevel@tonic-gate 	size_t k_attrs_size;
44510Sstevel@tonic-gate 	size_t rctl_bytes = 0;
44526424Skrishna 	boolean_t rctl_chk = B_FALSE;
44530Sstevel@tonic-gate 	int error = 0;
44540Sstevel@tonic-gate 	int rv;
44550Sstevel@tonic-gate 	uint_t count;
44560Sstevel@tonic-gate 
44570Sstevel@tonic-gate 	STRUCT_INIT(object_create, mode);
44580Sstevel@tonic-gate 
44590Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
44600Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_create: failed holding minor");
44610Sstevel@tonic-gate 		return (ENXIO);
44620Sstevel@tonic-gate 	}
44630Sstevel@tonic-gate 
44640Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(object_create),
44650Sstevel@tonic-gate 	    STRUCT_SIZE(object_create)) != 0) {
44660Sstevel@tonic-gate 		crypto_release_minor(cm);
44670Sstevel@tonic-gate 		return (EFAULT);
44680Sstevel@tonic-gate 	}
44690Sstevel@tonic-gate 
44700Sstevel@tonic-gate 	count = STRUCT_FGET(object_create, oc_count);
44710Sstevel@tonic-gate 	oc_attributes = STRUCT_FGETP(object_create, oc_attributes);
44720Sstevel@tonic-gate 
44730Sstevel@tonic-gate 	session_id = STRUCT_FGET(object_create, oc_session);
44740Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
44750Sstevel@tonic-gate 		goto release_minor;
44760Sstevel@tonic-gate 	}
44776424Skrishna 	if (!copyin_attributes(mode, sp, count, oc_attributes, &k_attrs,
44786424Skrishna 	    &k_attrs_size, NULL, &rv, &error, &rctl_bytes,
44796424Skrishna 	    &rctl_chk, B_TRUE)) {
44806424Skrishna 		goto release_minor;
44816424Skrishna 	}
44820Sstevel@tonic-gate 
44830Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
44840Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
44850Sstevel@tonic-gate 	    CRYPTO_OBJECT_OFFSET(object_create),
4486904Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider))
4487904Smcpowers 	    != CRYPTO_SUCCESS) {
44880Sstevel@tonic-gate 		goto release_minor;
44890Sstevel@tonic-gate 	}
44900Sstevel@tonic-gate 
44910Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_CREATE,
44920Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, 0, k_attrs, count,
44930Sstevel@tonic-gate 	    &object_handle, 0, NULL, NULL, 0, NULL);
44940Sstevel@tonic-gate 
44950Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
44960Sstevel@tonic-gate 
44970Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS)
44980Sstevel@tonic-gate 		STRUCT_FSET(object_create, oc_handle, object_handle);
44990Sstevel@tonic-gate 
45000Sstevel@tonic-gate release_minor:
45016424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk);
45020Sstevel@tonic-gate 
45030Sstevel@tonic-gate 	if (k_attrs != NULL)
45040Sstevel@tonic-gate 		kmem_free(k_attrs, k_attrs_size);
45050Sstevel@tonic-gate 
45060Sstevel@tonic-gate 	if (error != 0)
45070Sstevel@tonic-gate 		goto out;
45080Sstevel@tonic-gate 
45090Sstevel@tonic-gate 	STRUCT_FSET(object_create, oc_return_value, rv);
45100Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(object_create), arg,
45110Sstevel@tonic-gate 	    STRUCT_SIZE(object_create)) != 0) {
45120Sstevel@tonic-gate 		if (rv == CRYPTO_SUCCESS) {
45130Sstevel@tonic-gate 			KCF_WRAP_OBJECT_OPS_PARAMS(&params,
45140Sstevel@tonic-gate 			    KCF_OP_OBJECT_DESTROY,
45150Sstevel@tonic-gate 			    sp->sd_provider_session->ps_session, object_handle,
45160Sstevel@tonic-gate 			    NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
45170Sstevel@tonic-gate 
45180Sstevel@tonic-gate 			(void) kcf_submit_request(real_provider, NULL,
45190Sstevel@tonic-gate 			    NULL, &params, B_FALSE);
45200Sstevel@tonic-gate 
45210Sstevel@tonic-gate 			error = EFAULT;
45220Sstevel@tonic-gate 		}
45230Sstevel@tonic-gate 	}
45240Sstevel@tonic-gate out:
45256424Skrishna 	CRYPTO_SESSION_RELE(sp);
45260Sstevel@tonic-gate 	crypto_release_minor(cm);
4527904Smcpowers 	if (real_provider != NULL)
4528904Smcpowers 		KCF_PROV_REFRELE(real_provider);
45290Sstevel@tonic-gate 	return (error);
45300Sstevel@tonic-gate }
45310Sstevel@tonic-gate 
45320Sstevel@tonic-gate /* ARGSUSED */
45330Sstevel@tonic-gate static int
45340Sstevel@tonic-gate object_copy(dev_t dev, caddr_t arg, int mode, int *rval)
45350Sstevel@tonic-gate {
45360Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_copy, object_copy);
4537904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
45380Sstevel@tonic-gate 	kcf_req_params_t params;
45390Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
45400Sstevel@tonic-gate 	crypto_session_id_t session_id;
45410Sstevel@tonic-gate 	crypto_minor_t *cm;
45420Sstevel@tonic-gate 	crypto_session_data_t *sp = NULL;
45430Sstevel@tonic-gate 	crypto_object_id_t handle, new_handle;
45440Sstevel@tonic-gate 	caddr_t oc_new_attributes;
45450Sstevel@tonic-gate 	size_t k_attrs_size;
45460Sstevel@tonic-gate 	size_t rctl_bytes = 0;
45476424Skrishna 	boolean_t rctl_chk = B_FALSE;
45480Sstevel@tonic-gate 	int error = 0;
45490Sstevel@tonic-gate 	int rv;
45500Sstevel@tonic-gate 	uint_t count;
45510Sstevel@tonic-gate 
45520Sstevel@tonic-gate 	STRUCT_INIT(object_copy, mode);
45530Sstevel@tonic-gate 
45540Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
45550Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_copy: failed holding minor");
45560Sstevel@tonic-gate 		return (ENXIO);
45570Sstevel@tonic-gate 	}
45580Sstevel@tonic-gate 
45590Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(object_copy),
45600Sstevel@tonic-gate 	    STRUCT_SIZE(object_copy)) != 0) {
45610Sstevel@tonic-gate 		crypto_release_minor(cm);
45620Sstevel@tonic-gate 		return (EFAULT);
45630Sstevel@tonic-gate 	}
45640Sstevel@tonic-gate 
45650Sstevel@tonic-gate 	count = STRUCT_FGET(object_copy, oc_count);
45660Sstevel@tonic-gate 	oc_new_attributes = STRUCT_FGETP(object_copy, oc_new_attributes);
45670Sstevel@tonic-gate 
45680Sstevel@tonic-gate 	session_id = STRUCT_FGET(object_copy, oc_session);
45690Sstevel@tonic-gate 
45700Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
45710Sstevel@tonic-gate 		goto release_minor;
45720Sstevel@tonic-gate 	}
45736424Skrishna 	if (!copyin_attributes(mode, sp, count, oc_new_attributes, &k_attrs,
45746424Skrishna 	    &k_attrs_size, NULL, &rv, &error, &rctl_bytes,
45756424Skrishna 	    &rctl_chk, B_TRUE)) {
45766424Skrishna 		goto release_minor;
45776424Skrishna 	}
45780Sstevel@tonic-gate 
45790Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
45800Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
4581904Smcpowers 	    CRYPTO_OBJECT_OFFSET(object_copy), CHECK_RESTRICT_FALSE,
45820Sstevel@tonic-gate 	    sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
45830Sstevel@tonic-gate 		goto release_minor;
45840Sstevel@tonic-gate 	}
45850Sstevel@tonic-gate 
45860Sstevel@tonic-gate 	handle = STRUCT_FGET(object_copy, oc_handle);
45870Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_COPY,
45880Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, handle, k_attrs, count,
45890Sstevel@tonic-gate 	    &new_handle, 0, NULL, NULL, 0, NULL);
45900Sstevel@tonic-gate 
45910Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
45920Sstevel@tonic-gate 
45930Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS)
45940Sstevel@tonic-gate 		STRUCT_FSET(object_copy, oc_new_handle, new_handle);
45950Sstevel@tonic-gate 
45960Sstevel@tonic-gate release_minor:
45976424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk);
45980Sstevel@tonic-gate 
45990Sstevel@tonic-gate 	if (k_attrs != NULL)
46000Sstevel@tonic-gate 		kmem_free(k_attrs, k_attrs_size);
46010Sstevel@tonic-gate 
46020Sstevel@tonic-gate 	if (error != 0)
46030Sstevel@tonic-gate 		goto out;
46040Sstevel@tonic-gate 
46050Sstevel@tonic-gate 	STRUCT_FSET(object_copy, oc_return_value, rv);
46060Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(object_copy), arg,
46070Sstevel@tonic-gate 	    STRUCT_SIZE(object_copy)) != 0) {
46080Sstevel@tonic-gate 		if (rv == CRYPTO_SUCCESS) {
46090Sstevel@tonic-gate 			KCF_WRAP_OBJECT_OPS_PARAMS(&params,
46100Sstevel@tonic-gate 			    KCF_OP_OBJECT_DESTROY,
46110Sstevel@tonic-gate 			    sp->sd_provider_session->ps_session, new_handle,
46120Sstevel@tonic-gate 			    NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
46130Sstevel@tonic-gate 
46140Sstevel@tonic-gate 			(void) kcf_submit_request(real_provider, NULL,
46150Sstevel@tonic-gate 			    NULL, &params, B_FALSE);
46160Sstevel@tonic-gate 
46170Sstevel@tonic-gate 			error = EFAULT;
46180Sstevel@tonic-gate 		}
46190Sstevel@tonic-gate 	}
46200Sstevel@tonic-gate out:
46216424Skrishna 	CRYPTO_SESSION_RELE(sp);
46220Sstevel@tonic-gate 	crypto_release_minor(cm);
4623904Smcpowers 	if (real_provider != NULL)
4624904Smcpowers 		KCF_PROV_REFRELE(real_provider);
46250Sstevel@tonic-gate 	return (error);
46260Sstevel@tonic-gate }
46270Sstevel@tonic-gate 
46280Sstevel@tonic-gate /* ARGSUSED */
46290Sstevel@tonic-gate static int
46300Sstevel@tonic-gate object_destroy(dev_t dev, caddr_t arg, int mode, int *rval)
46310Sstevel@tonic-gate {
46320Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_destroy, object_destroy);
46330Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
46340Sstevel@tonic-gate 	kcf_req_params_t params;
46350Sstevel@tonic-gate 	crypto_session_id_t session_id;
46360Sstevel@tonic-gate 	crypto_minor_t *cm;
46370Sstevel@tonic-gate 	crypto_session_data_t *sp;
46380Sstevel@tonic-gate 	crypto_object_id_t handle;
46390Sstevel@tonic-gate 	int error = 0;
46400Sstevel@tonic-gate 	int rv;
46410Sstevel@tonic-gate 
46420Sstevel@tonic-gate 	STRUCT_INIT(object_destroy, mode);
46430Sstevel@tonic-gate 
46440Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
46450Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_destroy: failed holding minor");
46460Sstevel@tonic-gate 		return (ENXIO);
46470Sstevel@tonic-gate 	}
46480Sstevel@tonic-gate 
46490Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(object_destroy),
46500Sstevel@tonic-gate 	    STRUCT_SIZE(object_destroy)) != 0) {
46510Sstevel@tonic-gate 		crypto_release_minor(cm);
46520Sstevel@tonic-gate 		return (EFAULT);
46530Sstevel@tonic-gate 	}
46540Sstevel@tonic-gate 
46550Sstevel@tonic-gate 	session_id = STRUCT_FGET(object_destroy, od_session);
46560Sstevel@tonic-gate 
46570Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
46580Sstevel@tonic-gate 		goto release_minor;
46590Sstevel@tonic-gate 	}
46600Sstevel@tonic-gate 
46610Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
46620Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
4663904Smcpowers 	    CRYPTO_OBJECT_OFFSET(object_destroy), CHECK_RESTRICT_FALSE,
46640Sstevel@tonic-gate 	    sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
46650Sstevel@tonic-gate 		goto out;
46660Sstevel@tonic-gate 	}
46670Sstevel@tonic-gate 
46680Sstevel@tonic-gate 	handle = STRUCT_FGET(object_destroy, od_handle);
46690Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_DESTROY,
46700Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, handle, NULL, 0, NULL, 0,
46710Sstevel@tonic-gate 	    NULL, NULL, 0, NULL);
46720Sstevel@tonic-gate 
46730Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
4674904Smcpowers 	KCF_PROV_REFRELE(real_provider);
46750Sstevel@tonic-gate 
46760Sstevel@tonic-gate out:
46770Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
46780Sstevel@tonic-gate 
46790Sstevel@tonic-gate release_minor:
46800Sstevel@tonic-gate 	crypto_release_minor(cm);
46810Sstevel@tonic-gate 
46820Sstevel@tonic-gate 	if (error != 0)
46830Sstevel@tonic-gate 		return (error);
46840Sstevel@tonic-gate 
46850Sstevel@tonic-gate 	STRUCT_FSET(object_destroy, od_return_value, rv);
46860Sstevel@tonic-gate 
46870Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(object_destroy), arg,
46880Sstevel@tonic-gate 	    STRUCT_SIZE(object_destroy)) != 0) {
46890Sstevel@tonic-gate 		return (EFAULT);
46900Sstevel@tonic-gate 	}
46910Sstevel@tonic-gate 	return (0);
46920Sstevel@tonic-gate }
46930Sstevel@tonic-gate 
46940Sstevel@tonic-gate /* ARGSUSED */
46950Sstevel@tonic-gate static int
46960Sstevel@tonic-gate object_get_attribute_value(dev_t dev, caddr_t arg, int mode, int *rval)
46970Sstevel@tonic-gate {
46980Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_get_attribute_value, get_attribute_value);
46990Sstevel@tonic-gate 	/* LINTED E_FUNC_SET_NOT_USED */
47000Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_attribute, oa);
47010Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
47020Sstevel@tonic-gate 	kcf_req_params_t params;
47030Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
47040Sstevel@tonic-gate 	crypto_session_id_t session_id;
47050Sstevel@tonic-gate 	crypto_minor_t *cm;
47066424Skrishna 	crypto_session_data_t *sp = NULL;
47070Sstevel@tonic-gate 	crypto_object_id_t handle;
47080Sstevel@tonic-gate 	caddr_t og_attributes;
47098043SMark.Powers@Sun.COM 	caddr_t u_attrs = NULL;
47100Sstevel@tonic-gate 	size_t k_attrs_size;
47110Sstevel@tonic-gate 	size_t rctl_bytes = 0;
47126424Skrishna 	boolean_t rctl_chk = B_FALSE;
47130Sstevel@tonic-gate 	int error = 0;
47140Sstevel@tonic-gate 	int rv;
47150Sstevel@tonic-gate 	uint_t count;
47160Sstevel@tonic-gate 
47170Sstevel@tonic-gate 	STRUCT_INIT(get_attribute_value, mode);
47180Sstevel@tonic-gate 	STRUCT_INIT(oa, mode);
47190Sstevel@tonic-gate 
47200Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
47210Sstevel@tonic-gate 		cmn_err(CE_WARN,
47220Sstevel@tonic-gate 		    "object_get_attribute_value: failed holding minor");
47230Sstevel@tonic-gate 		return (ENXIO);
47240Sstevel@tonic-gate 	}
47250Sstevel@tonic-gate 
47260Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(get_attribute_value),
47270Sstevel@tonic-gate 	    STRUCT_SIZE(get_attribute_value)) != 0) {
47280Sstevel@tonic-gate 		crypto_release_minor(cm);
47290Sstevel@tonic-gate 		return (EFAULT);
47300Sstevel@tonic-gate 	}
47310Sstevel@tonic-gate 
47320Sstevel@tonic-gate 	count = STRUCT_FGET(get_attribute_value, og_count);
47330Sstevel@tonic-gate 	og_attributes = STRUCT_FGETP(get_attribute_value, og_attributes);
47340Sstevel@tonic-gate 
47350Sstevel@tonic-gate 	session_id = STRUCT_FGET(get_attribute_value, og_session);
47360Sstevel@tonic-gate 
47370Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
47380Sstevel@tonic-gate 		goto release_minor;
47390Sstevel@tonic-gate 	}
47406424Skrishna 	if (!copyin_attributes(mode, sp, count, og_attributes, &k_attrs,
47416424Skrishna 	    &k_attrs_size, &u_attrs, &rv, &error, &rctl_bytes,
47426424Skrishna 	    &rctl_chk, B_FALSE)) {
47436424Skrishna 		goto release_minor;
47446424Skrishna 	}
47450Sstevel@tonic-gate 
47460Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
47470Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
47480Sstevel@tonic-gate 	    CRYPTO_OBJECT_OFFSET(object_get_attribute_value),
4749904Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider))
4750904Smcpowers 	    != CRYPTO_SUCCESS) {
47510Sstevel@tonic-gate 		goto out;
47520Sstevel@tonic-gate 	}
47530Sstevel@tonic-gate 
47540Sstevel@tonic-gate 	handle = STRUCT_FGET(get_attribute_value, og_handle);
47550Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_GET_ATTRIBUTE_VALUE,
47560Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, handle, k_attrs, count, NULL,
47570Sstevel@tonic-gate 	    0, NULL, NULL, 0, NULL);
47580Sstevel@tonic-gate 
47590Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
4760904Smcpowers 	KCF_PROV_REFRELE(real_provider);
47610Sstevel@tonic-gate 
47620Sstevel@tonic-gate out:
47630Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS || rv == CRYPTO_ATTRIBUTE_SENSITIVE ||
47640Sstevel@tonic-gate 	    rv == CRYPTO_ATTRIBUTE_TYPE_INVALID ||
47650Sstevel@tonic-gate 	    rv == CRYPTO_BUFFER_TOO_SMALL) {
47660Sstevel@tonic-gate 		error = copyout_attributes(mode,
47670Sstevel@tonic-gate 		    STRUCT_FGETP(get_attribute_value, og_attributes),
47680Sstevel@tonic-gate 		    count, k_attrs, u_attrs);
47690Sstevel@tonic-gate 	}
47700Sstevel@tonic-gate 
47710Sstevel@tonic-gate release_minor:
47726424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk);
47736424Skrishna 	CRYPTO_SESSION_RELE(sp);
47740Sstevel@tonic-gate 	crypto_release_minor(cm);
47750Sstevel@tonic-gate 
47760Sstevel@tonic-gate 	if (k_attrs != NULL)
47770Sstevel@tonic-gate 		kmem_free(k_attrs, k_attrs_size);
47780Sstevel@tonic-gate 
47790Sstevel@tonic-gate 	if (u_attrs != NULL)
47800Sstevel@tonic-gate 		kmem_free(u_attrs, count * STRUCT_SIZE(oa));
47810Sstevel@tonic-gate 
47820Sstevel@tonic-gate 	if (error != 0)
47830Sstevel@tonic-gate 		return (error);
47840Sstevel@tonic-gate 
47850Sstevel@tonic-gate 	STRUCT_FSET(get_attribute_value, og_return_value, rv);
47860Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(get_attribute_value), arg,
47870Sstevel@tonic-gate 	    STRUCT_SIZE(get_attribute_value)) != 0) {
47880Sstevel@tonic-gate 		return (EFAULT);
47890Sstevel@tonic-gate 	}
47900Sstevel@tonic-gate 	return (0);
47910Sstevel@tonic-gate }
47920Sstevel@tonic-gate 
47930Sstevel@tonic-gate /* ARGSUSED */
47940Sstevel@tonic-gate static int
47950Sstevel@tonic-gate object_get_size(dev_t dev, caddr_t arg, int mode, int *rval)
47960Sstevel@tonic-gate {
47970Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_get_size, object_get_size);
47980Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
47990Sstevel@tonic-gate 	kcf_req_params_t params;
48000Sstevel@tonic-gate 	crypto_session_id_t session_id;
48010Sstevel@tonic-gate 	crypto_minor_t *cm;
48026424Skrishna 	crypto_session_data_t *sp = NULL;
48030Sstevel@tonic-gate 	crypto_object_id_t handle;
48040Sstevel@tonic-gate 	size_t size;
48050Sstevel@tonic-gate 	int error = 0;
48060Sstevel@tonic-gate 	int rv;
48070Sstevel@tonic-gate 
48080Sstevel@tonic-gate 	STRUCT_INIT(object_get_size, mode);
48090Sstevel@tonic-gate 
48100Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
48110Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_get_size: failed holding minor");
48120Sstevel@tonic-gate 		return (ENXIO);
48130Sstevel@tonic-gate 	}
48140Sstevel@tonic-gate 
48150Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(object_get_size),
48160Sstevel@tonic-gate 	    STRUCT_SIZE(object_get_size)) != 0) {
48170Sstevel@tonic-gate 		crypto_release_minor(cm);
48180Sstevel@tonic-gate 		return (EFAULT);
48190Sstevel@tonic-gate 	}
48200Sstevel@tonic-gate 
48210Sstevel@tonic-gate 	session_id = STRUCT_FGET(object_get_size, gs_session);
48220Sstevel@tonic-gate 
48230Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
48240Sstevel@tonic-gate 		goto release_minor;
48250Sstevel@tonic-gate 	}
48260Sstevel@tonic-gate 
48270Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
48280Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
4829904Smcpowers 	    CRYPTO_OBJECT_OFFSET(object_get_size), CHECK_RESTRICT_FALSE,
48300Sstevel@tonic-gate 	    sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
48316424Skrishna 		goto release_minor;
48320Sstevel@tonic-gate 	}
48330Sstevel@tonic-gate 
48340Sstevel@tonic-gate 	handle = STRUCT_FGET(object_get_size, gs_handle);
48350Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_GET_SIZE,
48360Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, handle, NULL, 0, NULL, &size,
48370Sstevel@tonic-gate 	    NULL, NULL, 0, NULL);
48380Sstevel@tonic-gate 
48390Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
4840904Smcpowers 	KCF_PROV_REFRELE(real_provider);
48410Sstevel@tonic-gate 
48420Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
48430Sstevel@tonic-gate 		STRUCT_FSET(object_get_size, gs_size, size);
48440Sstevel@tonic-gate 	}
48456424Skrishna 
48460Sstevel@tonic-gate release_minor:
48470Sstevel@tonic-gate 	crypto_release_minor(cm);
48486424Skrishna 	CRYPTO_SESSION_RELE(sp);
48490Sstevel@tonic-gate 
48500Sstevel@tonic-gate 	if (error != 0)
48510Sstevel@tonic-gate 		return (error);
48520Sstevel@tonic-gate 
48530Sstevel@tonic-gate 	STRUCT_FSET(object_get_size, gs_return_value, rv);
48540Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(object_get_size), arg,
48550Sstevel@tonic-gate 	    STRUCT_SIZE(object_get_size)) != 0) {
48560Sstevel@tonic-gate 		return (EFAULT);
48570Sstevel@tonic-gate 	}
48580Sstevel@tonic-gate 	return (0);
48590Sstevel@tonic-gate }
48600Sstevel@tonic-gate 
48610Sstevel@tonic-gate /* ARGSUSED */
48620Sstevel@tonic-gate static int
48630Sstevel@tonic-gate object_set_attribute_value(dev_t dev, caddr_t arg, int mode, int *rval)
48640Sstevel@tonic-gate {
48650Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_set_attribute_value, set_attribute_value);
48660Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
48670Sstevel@tonic-gate 	kcf_req_params_t params;
48680Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
48690Sstevel@tonic-gate 	crypto_session_id_t session_id;
48700Sstevel@tonic-gate 	crypto_minor_t *cm;
48716424Skrishna 	crypto_session_data_t *sp = NULL;
48720Sstevel@tonic-gate 	crypto_object_id_t object_handle;
48730Sstevel@tonic-gate 	caddr_t sa_attributes;
48740Sstevel@tonic-gate 	size_t k_attrs_size;
48750Sstevel@tonic-gate 	size_t rctl_bytes = 0;
48766424Skrishna 	boolean_t rctl_chk = B_FALSE;
48770Sstevel@tonic-gate 	int error = 0;
48780Sstevel@tonic-gate 	int rv;
48790Sstevel@tonic-gate 	uint_t count;
48800Sstevel@tonic-gate 
48810Sstevel@tonic-gate 	STRUCT_INIT(set_attribute_value, mode);
48820Sstevel@tonic-gate 
48830Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
48840Sstevel@tonic-gate 		cmn_err(CE_WARN,
48850Sstevel@tonic-gate 		    "object_set_attribute_value: failed holding minor");
48860Sstevel@tonic-gate 		return (ENXIO);
48870Sstevel@tonic-gate 	}
48880Sstevel@tonic-gate 
48890Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(set_attribute_value),
48900Sstevel@tonic-gate 	    STRUCT_SIZE(set_attribute_value)) != 0) {
48910Sstevel@tonic-gate 		crypto_release_minor(cm);
48920Sstevel@tonic-gate 		return (EFAULT);
48930Sstevel@tonic-gate 	}
48940Sstevel@tonic-gate 
48950Sstevel@tonic-gate 	count = STRUCT_FGET(set_attribute_value, sa_count);
48960Sstevel@tonic-gate 	sa_attributes = STRUCT_FGETP(set_attribute_value, sa_attributes);
48970Sstevel@tonic-gate 
48980Sstevel@tonic-gate 	session_id = STRUCT_FGET(set_attribute_value, sa_session);
48990Sstevel@tonic-gate 
49000Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
49010Sstevel@tonic-gate 		goto release_minor;
49020Sstevel@tonic-gate 	}
49036424Skrishna 	if (!copyin_attributes(mode, sp, count, sa_attributes, &k_attrs,
49046424Skrishna 	    &k_attrs_size, NULL, &rv, &error, &rctl_bytes,
49056424Skrishna 	    &rctl_chk, B_TRUE)) {
49066424Skrishna 		goto release_minor;
49076424Skrishna 	}
49080Sstevel@tonic-gate 
49090Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
49100Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
49110Sstevel@tonic-gate 	    CRYPTO_OBJECT_OFFSET(object_set_attribute_value),
4912904Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider))
4913904Smcpowers 	    != CRYPTO_SUCCESS) {
49146424Skrishna 		goto release_minor;
49150Sstevel@tonic-gate 	}
49160Sstevel@tonic-gate 
49170Sstevel@tonic-gate 	object_handle = STRUCT_FGET(set_attribute_value, sa_handle);
49180Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_SET_ATTRIBUTE_VALUE,
49190Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, object_handle, k_attrs, count,
49200Sstevel@tonic-gate 	    NULL, 0, NULL, NULL, 0, NULL);
49210Sstevel@tonic-gate 
49220Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
4923904Smcpowers 	KCF_PROV_REFRELE(real_provider);
49240Sstevel@tonic-gate 
49256424Skrishna release_minor:
49266424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk);
49270Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
49280Sstevel@tonic-gate 	crypto_release_minor(cm);
49290Sstevel@tonic-gate 
49300Sstevel@tonic-gate 	if (k_attrs != NULL)
49310Sstevel@tonic-gate 		kmem_free(k_attrs, k_attrs_size);
49320Sstevel@tonic-gate 
49330Sstevel@tonic-gate 	if (error != 0)
49340Sstevel@tonic-gate 		return (error);
49350Sstevel@tonic-gate 
49360Sstevel@tonic-gate 	STRUCT_FSET(set_attribute_value, sa_return_value, rv);
49370Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(set_attribute_value), arg,
49380Sstevel@tonic-gate 	    STRUCT_SIZE(set_attribute_value)) != 0) {
49390Sstevel@tonic-gate 		return (EFAULT);
49400Sstevel@tonic-gate 	}
49410Sstevel@tonic-gate 	return (0);
49420Sstevel@tonic-gate }
49430Sstevel@tonic-gate 
49440Sstevel@tonic-gate /* ARGSUSED */
49450Sstevel@tonic-gate static int
49460Sstevel@tonic-gate object_find_init(dev_t dev, caddr_t arg, int mode, int *rval)
49470Sstevel@tonic-gate {
49480Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_find_init, find_init);
4949904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
49500Sstevel@tonic-gate 	kcf_req_params_t params;
49510Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
49520Sstevel@tonic-gate 	crypto_session_id_t session_id;
49530Sstevel@tonic-gate 	crypto_minor_t *cm;
49546424Skrishna 	crypto_session_data_t *sp = NULL;
49550Sstevel@tonic-gate 	caddr_t attributes;
49560Sstevel@tonic-gate 	size_t k_attrs_size;
49570Sstevel@tonic-gate 	size_t rctl_bytes = 0;
49586424Skrishna 	boolean_t rctl_chk = B_FALSE;
49590Sstevel@tonic-gate 	int error = 0;
49600Sstevel@tonic-gate 	int rv;
49610Sstevel@tonic-gate 	uint_t count;
49620Sstevel@tonic-gate 	void *cookie;
49630Sstevel@tonic-gate 
49640Sstevel@tonic-gate 	STRUCT_INIT(find_init, mode);
49650Sstevel@tonic-gate 
49660Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
49670Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_find_init: failed holding minor");
49680Sstevel@tonic-gate 		return (ENXIO);
49690Sstevel@tonic-gate 	}
49700Sstevel@tonic-gate 
49710Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(find_init), STRUCT_SIZE(find_init)) != 0) {
49720Sstevel@tonic-gate 		crypto_release_minor(cm);
49730Sstevel@tonic-gate 		return (EFAULT);
49740Sstevel@tonic-gate 	}
49750Sstevel@tonic-gate 
49760Sstevel@tonic-gate 	count = STRUCT_FGET(find_init, fi_count);
49770Sstevel@tonic-gate 	attributes = STRUCT_FGETP(find_init, fi_attributes);
49780Sstevel@tonic-gate 
49790Sstevel@tonic-gate 	session_id = STRUCT_FGET(find_init, fi_session);
49800Sstevel@tonic-gate 
49810Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
49820Sstevel@tonic-gate 		goto release_minor;
49830Sstevel@tonic-gate 	}
49846424Skrishna 	if (!copyin_attributes(mode, sp, count, attributes, &k_attrs,
49856424Skrishna 	    &k_attrs_size, NULL, &rv, &error, &rctl_bytes,
49866424Skrishna 	    &rctl_chk, B_TRUE)) {
49876424Skrishna 		goto release_minor;
49886424Skrishna 	}
49890Sstevel@tonic-gate 
49900Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
49910Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
4992904Smcpowers 	    CRYPTO_OBJECT_OFFSET(object_find_init), CHECK_RESTRICT_FALSE,
49930Sstevel@tonic-gate 	    sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
49946424Skrishna 		goto release_minor;
49950Sstevel@tonic-gate 	}
49960Sstevel@tonic-gate 
49970Sstevel@tonic-gate 	/* check for an active find */
49980Sstevel@tonic-gate 	if (sp->sd_find_init_cookie != NULL) {
49990Sstevel@tonic-gate 		rv = CRYPTO_OPERATION_IS_ACTIVE;
50000Sstevel@tonic-gate 		goto release_minor;
50010Sstevel@tonic-gate 	}
50020Sstevel@tonic-gate 
50030Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_FIND_INIT,
50040Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, 0, k_attrs, count, NULL, 0,
50050Sstevel@tonic-gate 	    &cookie, NULL, 0, NULL);
50060Sstevel@tonic-gate 
50070Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
50080Sstevel@tonic-gate 
50090Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
50100Sstevel@tonic-gate 		/*
50110Sstevel@tonic-gate 		 * The cookie is allocated by a provider at the start of an
50120Sstevel@tonic-gate 		 * object search.  It is freed when the search is terminated
50130Sstevel@tonic-gate 		 * by a final operation, or when the session is closed.
50140Sstevel@tonic-gate 		 * It contains state information about which object handles
50150Sstevel@tonic-gate 		 * have been returned to the caller.
50160Sstevel@tonic-gate 		 */
50170Sstevel@tonic-gate 		sp->sd_find_init_cookie = cookie;
50180Sstevel@tonic-gate 	}
50190Sstevel@tonic-gate 
50206424Skrishna release_minor:
50216424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk);
50220Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
50230Sstevel@tonic-gate 	crypto_release_minor(cm);
50240Sstevel@tonic-gate 
5025904Smcpowers 	if (real_provider != NULL)
5026904Smcpowers 		KCF_PROV_REFRELE(real_provider);
5027904Smcpowers 
50280Sstevel@tonic-gate 	if (k_attrs != NULL)
50290Sstevel@tonic-gate 		kmem_free(k_attrs, k_attrs_size);
50300Sstevel@tonic-gate 
50310Sstevel@tonic-gate 	if (error != 0)
50320Sstevel@tonic-gate 		return (error);
50330Sstevel@tonic-gate 
50340Sstevel@tonic-gate 	STRUCT_FSET(find_init, fi_return_value, rv);
50350Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(find_init), arg, STRUCT_SIZE(find_init)) != 0) {
50360Sstevel@tonic-gate 		return (EFAULT);
50370Sstevel@tonic-gate 	}
50380Sstevel@tonic-gate 	return (0);
50390Sstevel@tonic-gate }
50400Sstevel@tonic-gate 
50410Sstevel@tonic-gate /* ARGSUSED */
50420Sstevel@tonic-gate static int
50430Sstevel@tonic-gate object_find_update(dev_t dev, caddr_t arg, int mode, int *rval)
50440Sstevel@tonic-gate {
50450Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_find_update, find_update);
50460Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
50470Sstevel@tonic-gate 	kcf_req_params_t params;
50480Sstevel@tonic-gate 	crypto_minor_t *cm;
50496424Skrishna 	crypto_session_data_t *sp = NULL;
50500Sstevel@tonic-gate 	crypto_object_id_t *buffer = NULL;
50510Sstevel@tonic-gate 	crypto_session_id_t session_id;
50520Sstevel@tonic-gate 	size_t len, rctl_bytes = 0;
50530Sstevel@tonic-gate 	uint_t count, max_count;
50540Sstevel@tonic-gate 	int rv, error = 0;
50556424Skrishna 	boolean_t rctl_chk = B_FALSE;
50560Sstevel@tonic-gate 
50570Sstevel@tonic-gate 	STRUCT_INIT(find_update, mode);
50580Sstevel@tonic-gate 
50590Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
50600Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_find_update: failed holding minor");
50610Sstevel@tonic-gate 		return (ENXIO);
50620Sstevel@tonic-gate 	}
50630Sstevel@tonic-gate 
50640Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(find_update),
50650Sstevel@tonic-gate 	    STRUCT_SIZE(find_update)) != 0) {
50660Sstevel@tonic-gate 		crypto_release_minor(cm);
50670Sstevel@tonic-gate 		return (EFAULT);
50680Sstevel@tonic-gate 	}
50690Sstevel@tonic-gate 
50700Sstevel@tonic-gate 	max_count = STRUCT_FGET(find_update, fu_max_count);
50710Sstevel@tonic-gate 	if (max_count > CRYPTO_MAX_FIND_COUNT) {
50720Sstevel@tonic-gate 		cmn_err(CE_NOTE, "object_find_update: count greater than %d, "
50730Sstevel@tonic-gate 		    "pid = %d", CRYPTO_MAX_FIND_COUNT, curproc->p_pid);
50740Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
50750Sstevel@tonic-gate 		goto release_minor;
50760Sstevel@tonic-gate 	}
50770Sstevel@tonic-gate 	len = max_count * sizeof (crypto_object_id_t);
50786424Skrishna 	session_id = STRUCT_FGET(find_update, fu_session);
50796424Skrishna 
50806424Skrishna 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
50816424Skrishna 		goto release_minor;
50826424Skrishna 	}
50836424Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, len, rctl_chk)) !=
50846424Skrishna 	    CRYPTO_SUCCESS) {
50850Sstevel@tonic-gate 		goto release_minor;
50860Sstevel@tonic-gate 	}
50870Sstevel@tonic-gate 	rctl_bytes = len;
50880Sstevel@tonic-gate 	buffer = kmem_alloc(len, KM_SLEEP);
50890Sstevel@tonic-gate 
50900Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
50910Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
5092904Smcpowers 	    CRYPTO_OBJECT_OFFSET(object_find), CHECK_RESTRICT_FALSE,
50930Sstevel@tonic-gate 	    sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
50946424Skrishna 		goto release_minor;
50950Sstevel@tonic-gate 	}
50960Sstevel@tonic-gate 
50970Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_FIND,
50980Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, 0, NULL, 0, buffer, 0,
50990Sstevel@tonic-gate 	    NULL, sp->sd_find_init_cookie, max_count, &count);
51000Sstevel@tonic-gate 
51010Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
5102904Smcpowers 	KCF_PROV_REFRELE(real_provider);
51030Sstevel@tonic-gate 
51040Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
51050Sstevel@tonic-gate 		if (count > max_count) {
51060Sstevel@tonic-gate 			/* bad bad provider */
51070Sstevel@tonic-gate 			rv = CRYPTO_FAILED;
51080Sstevel@tonic-gate 			goto release_minor;
51090Sstevel@tonic-gate 		}
51100Sstevel@tonic-gate 		if (count != 0) {
51110Sstevel@tonic-gate 			/* copyout handles */
51120Sstevel@tonic-gate 			if (copyout(buffer,
51130Sstevel@tonic-gate 			    STRUCT_FGETP(find_update, fu_handles),
51140Sstevel@tonic-gate 			    count * sizeof (crypto_object_id_t)) != 0) {
51150Sstevel@tonic-gate 				error = EFAULT;
51160Sstevel@tonic-gate 			}
51170Sstevel@tonic-gate 		}
51180Sstevel@tonic-gate 		STRUCT_FSET(find_update, fu_count, count);
51190Sstevel@tonic-gate 	}
51200Sstevel@tonic-gate 
51210Sstevel@tonic-gate release_minor:
51226424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk);
51236424Skrishna 	CRYPTO_SESSION_RELE(sp);
51240Sstevel@tonic-gate 	crypto_release_minor(cm);
51250Sstevel@tonic-gate 
51260Sstevel@tonic-gate 	if (buffer != NULL)
51270Sstevel@tonic-gate 		kmem_free(buffer, len);
51280Sstevel@tonic-gate 
51290Sstevel@tonic-gate 	if (error != 0)
51300Sstevel@tonic-gate 		return (error);
51310Sstevel@tonic-gate 
51320Sstevel@tonic-gate 	STRUCT_FSET(find_update, fu_return_value, rv);
51330Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(find_update), arg,
51340Sstevel@tonic-gate 	    STRUCT_SIZE(find_update)) != 0) {
51350Sstevel@tonic-gate 		return (EFAULT);
51360Sstevel@tonic-gate 	}
51370Sstevel@tonic-gate 
51380Sstevel@tonic-gate 	return (0);
51390Sstevel@tonic-gate }
51400Sstevel@tonic-gate 
51410Sstevel@tonic-gate /*
51420Sstevel@tonic-gate  * Free provider-allocated storage used for find object searches.
51430Sstevel@tonic-gate  */
51440Sstevel@tonic-gate static int
51450Sstevel@tonic-gate crypto_free_find_ctx(crypto_session_data_t *sp)
51460Sstevel@tonic-gate {
51470Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
51480Sstevel@tonic-gate 	kcf_req_params_t params;
51490Sstevel@tonic-gate 	int rv;
51500Sstevel@tonic-gate 
51510Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
51520Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
5153904Smcpowers 	    CRYPTO_OBJECT_OFFSET(object_find_final), CHECK_RESTRICT_FALSE,
51540Sstevel@tonic-gate 	    sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
51550Sstevel@tonic-gate 		return (rv);
51560Sstevel@tonic-gate 	}
51570Sstevel@tonic-gate 
51580Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_FIND_FINAL,
51590Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, 0, NULL, 0, NULL, 0,
51600Sstevel@tonic-gate 	    NULL, sp->sd_find_init_cookie, 0, NULL);
51610Sstevel@tonic-gate 
5162904Smcpowers 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
5163904Smcpowers 	KCF_PROV_REFRELE(real_provider);
5164904Smcpowers 	return (rv);
51650Sstevel@tonic-gate }
51660Sstevel@tonic-gate 
51670Sstevel@tonic-gate /* ARGSUSED */
51680Sstevel@tonic-gate static int
51690Sstevel@tonic-gate object_find_final(dev_t dev, caddr_t arg, int mode, int *rval)
51700Sstevel@tonic-gate {
51710Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_find_final, object_find_final);
51720Sstevel@tonic-gate 	crypto_session_id_t session_id;
51730Sstevel@tonic-gate 	crypto_minor_t *cm;
51740Sstevel@tonic-gate 	crypto_session_data_t *sp;
51750Sstevel@tonic-gate 	int error = 0;
51760Sstevel@tonic-gate 	int rv;
51770Sstevel@tonic-gate 
51780Sstevel@tonic-gate 	STRUCT_INIT(object_find_final, mode);
51790Sstevel@tonic-gate 
51800Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
51810Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_find_final: failed holding minor");
51820Sstevel@tonic-gate 		return (ENXIO);
51830Sstevel@tonic-gate 	}
51840Sstevel@tonic-gate 
51850Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(object_find_final),
51860Sstevel@tonic-gate 	    STRUCT_SIZE(object_find_final)) != 0) {
51870Sstevel@tonic-gate 		crypto_release_minor(cm);
51880Sstevel@tonic-gate 		return (EFAULT);
51890Sstevel@tonic-gate 	}
51900Sstevel@tonic-gate 
51910Sstevel@tonic-gate 	session_id = STRUCT_FGET(object_find_final, ff_session);
51920Sstevel@tonic-gate 
51930Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
51940Sstevel@tonic-gate 		goto release_minor;
51950Sstevel@tonic-gate 	}
51960Sstevel@tonic-gate 
51970Sstevel@tonic-gate 	if ((rv = crypto_free_find_ctx(sp)) == CRYPTO_SUCCESS) {
51980Sstevel@tonic-gate 		sp->sd_find_init_cookie = NULL;
51990Sstevel@tonic-gate 	}
52000Sstevel@tonic-gate 
52010Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
52020Sstevel@tonic-gate 
52030Sstevel@tonic-gate release_minor:
52040Sstevel@tonic-gate 	crypto_release_minor(cm);
52050Sstevel@tonic-gate 
52060Sstevel@tonic-gate 	if (error != 0)
52070Sstevel@tonic-gate 		return (error);
52080Sstevel@tonic-gate 
52090Sstevel@tonic-gate 	STRUCT_FSET(object_find_final, ff_return_value, rv);
52100Sstevel@tonic-gate 
52110Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(object_find_final), arg,
52120Sstevel@tonic-gate 	    STRUCT_SIZE(object_find_final)) != 0) {
52130Sstevel@tonic-gate 		return (EFAULT);
52140Sstevel@tonic-gate 	}
52150Sstevel@tonic-gate 	return (0);
52160Sstevel@tonic-gate }
52170Sstevel@tonic-gate 
52180Sstevel@tonic-gate /* ARGSUSED */
52190Sstevel@tonic-gate static int
52200Sstevel@tonic-gate object_generate_key(dev_t dev, caddr_t arg, int mode, int *rval)
52210Sstevel@tonic-gate {
52220Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_generate_key, generate_key);
5223904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
52240Sstevel@tonic-gate 	kcf_req_params_t params;
52250Sstevel@tonic-gate 	crypto_mechanism_t mech;
52260Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
52270Sstevel@tonic-gate 	crypto_session_id_t session_id;
52280Sstevel@tonic-gate 	crypto_minor_t *cm;
52290Sstevel@tonic-gate 	crypto_session_data_t *sp = NULL;
52300Sstevel@tonic-gate 	crypto_object_id_t key_handle;
52310Sstevel@tonic-gate 	caddr_t attributes;
52320Sstevel@tonic-gate 	size_t k_attrs_size;
52330Sstevel@tonic-gate 	size_t mech_rctl_bytes = 0, key_rctl_bytes = 0;
52346424Skrishna 	boolean_t mech_rctl_chk = B_FALSE;
52356424Skrishna 	boolean_t key_rctl_chk = B_FALSE;
52360Sstevel@tonic-gate 	uint_t count;
52370Sstevel@tonic-gate 	int error = 0;
52380Sstevel@tonic-gate 	int rv;
5239904Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
52400Sstevel@tonic-gate 
52410Sstevel@tonic-gate 	STRUCT_INIT(generate_key, mode);
52420Sstevel@tonic-gate 
52430Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
52440Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_generate_key: failed holding minor");
52450Sstevel@tonic-gate 		return (ENXIO);
52460Sstevel@tonic-gate 	}
52470Sstevel@tonic-gate 
52480Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(generate_key),
52490Sstevel@tonic-gate 	    STRUCT_SIZE(generate_key)) != 0) {
52500Sstevel@tonic-gate 		crypto_release_minor(cm);
52510Sstevel@tonic-gate 		return (EFAULT);
52520Sstevel@tonic-gate 	}
52530Sstevel@tonic-gate 
52540Sstevel@tonic-gate 	session_id = STRUCT_FGET(generate_key, gk_session);
52550Sstevel@tonic-gate 
52560Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
52570Sstevel@tonic-gate 		goto release_minor;
52580Sstevel@tonic-gate 	}
52590Sstevel@tonic-gate 
5260904Smcpowers 	bcopy(STRUCT_FADDR(generate_key, gk_mechanism), &mech.cm_type,
5261904Smcpowers 	    sizeof (crypto_mech_type_t));
5262904Smcpowers 
5263904Smcpowers 	if ((rv = kcf_get_hardware_provider(mech.cm_type, CRYPTO_MECH_INVALID,
52641808Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider,
52651808Smcpowers 	    CRYPTO_FG_GENERATE)) != CRYPTO_SUCCESS) {
52660Sstevel@tonic-gate 		goto release_minor;
52670Sstevel@tonic-gate 	}
52680Sstevel@tonic-gate 
5269904Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
5270904Smcpowers 	    STRUCT_FADDR(generate_key, gk_mechanism), &mech, mode, &error);
5271904Smcpowers 
5272904Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
5273904Smcpowers 		allocated_by_crypto_module = B_TRUE;
52746424Skrishna 		if (!copyin_mech(mode, sp,
52756424Skrishna 		    STRUCT_FADDR(generate_key, gk_mechanism),
52766424Skrishna 		    &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) {
5277904Smcpowers 			goto release_minor;
5278904Smcpowers 		}
5279904Smcpowers 	} else {
5280904Smcpowers 		if (rv != CRYPTO_SUCCESS)
5281904Smcpowers 			goto release_minor;
52820Sstevel@tonic-gate 	}
52830Sstevel@tonic-gate 
52840Sstevel@tonic-gate 	count = STRUCT_FGET(generate_key, gk_count);
52850Sstevel@tonic-gate 	attributes = STRUCT_FGETP(generate_key, gk_attributes);
52866424Skrishna 	if (!copyin_attributes(mode, sp, count, attributes, &k_attrs,
52876424Skrishna 	    &k_attrs_size, NULL, &rv, &error, &key_rctl_bytes,
52886424Skrishna 	    &key_rctl_chk, B_TRUE)) {
52890Sstevel@tonic-gate 		goto release_minor;
52900Sstevel@tonic-gate 	}
52910Sstevel@tonic-gate 
52920Sstevel@tonic-gate 	KCF_WRAP_KEY_OPS_PARAMS(&params, KCF_OP_KEY_GENERATE,
52930Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, &mech, k_attrs, count,
52940Sstevel@tonic-gate 	    &key_handle, NULL, 0, NULL, NULL, NULL, 0);
52950Sstevel@tonic-gate 
52960Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
52970Sstevel@tonic-gate 
52980Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS)
52990Sstevel@tonic-gate 		STRUCT_FSET(generate_key, gk_handle, key_handle);
53000Sstevel@tonic-gate 
53010Sstevel@tonic-gate release_minor:
53026424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
53036424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk);
53040Sstevel@tonic-gate 
53050Sstevel@tonic-gate 	if (k_attrs != NULL)
53060Sstevel@tonic-gate 		kmem_free(k_attrs, k_attrs_size);
53070Sstevel@tonic-gate 
53080Sstevel@tonic-gate 	if (error != 0)
53090Sstevel@tonic-gate 		goto out;
53100Sstevel@tonic-gate 
53110Sstevel@tonic-gate 	STRUCT_FSET(generate_key, gk_return_value, rv);
53120Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(generate_key), arg,
53130Sstevel@tonic-gate 	    STRUCT_SIZE(generate_key)) != 0) {
53140Sstevel@tonic-gate 		if (rv == CRYPTO_SUCCESS) {
53150Sstevel@tonic-gate 			KCF_WRAP_OBJECT_OPS_PARAMS(&params,
53160Sstevel@tonic-gate 			    KCF_OP_OBJECT_DESTROY,
53170Sstevel@tonic-gate 			    sp->sd_provider_session->ps_session, key_handle,
53180Sstevel@tonic-gate 			    NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
53190Sstevel@tonic-gate 
53200Sstevel@tonic-gate 			(void) kcf_submit_request(real_provider, NULL,
53210Sstevel@tonic-gate 			    NULL, &params, B_FALSE);
53220Sstevel@tonic-gate 
53230Sstevel@tonic-gate 			error = EFAULT;
53240Sstevel@tonic-gate 		}
53250Sstevel@tonic-gate 	}
53260Sstevel@tonic-gate out:
53276424Skrishna 	CRYPTO_SESSION_RELE(sp);
53280Sstevel@tonic-gate 	crypto_release_minor(cm);
5329904Smcpowers 
5330904Smcpowers 	if (real_provider != NULL) {
5331904Smcpowers 		crypto_free_mech(real_provider,
5332904Smcpowers 		    allocated_by_crypto_module, &mech);
5333904Smcpowers 		KCF_PROV_REFRELE(real_provider);
5334904Smcpowers 	}
53350Sstevel@tonic-gate 	return (error);
53360Sstevel@tonic-gate }
53370Sstevel@tonic-gate 
53380Sstevel@tonic-gate /* ARGSUSED */
53390Sstevel@tonic-gate static int
53404219Smcpowers nostore_generate_key(dev_t dev, caddr_t arg, int mode, int *rval)
53414219Smcpowers {
53424219Smcpowers 	STRUCT_DECL(crypto_nostore_generate_key, generate_key);
53434219Smcpowers 	/* LINTED E_FUNC_SET_NOT_USED */
53444219Smcpowers 	STRUCT_DECL(crypto_object_attribute, oa);
53454219Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
53464219Smcpowers 	kcf_req_params_t params;
53474219Smcpowers 	crypto_mechanism_t mech;
53484219Smcpowers 	crypto_object_attribute_t *k_in_attrs = NULL;
53494219Smcpowers 	crypto_object_attribute_t *k_out_attrs = NULL;
53504219Smcpowers 	crypto_session_id_t session_id;
53514219Smcpowers 	crypto_minor_t *cm;
53524219Smcpowers 	crypto_session_data_t *sp = NULL;
53534219Smcpowers 	caddr_t in_attributes;
53544219Smcpowers 	caddr_t out_attributes;
53554219Smcpowers 	size_t k_in_attrs_size;
53564219Smcpowers 	size_t k_out_attrs_size;
53574219Smcpowers 	size_t mech_rctl_bytes = 0;
53586424Skrishna 	boolean_t mech_rctl_chk = B_FALSE;
53594219Smcpowers 	size_t in_key_rctl_bytes = 0, out_key_rctl_bytes = 0;
53606424Skrishna 	boolean_t in_key_rctl_chk = B_FALSE;
53616424Skrishna 	boolean_t out_key_rctl_chk = B_FALSE;
53624219Smcpowers 	uint_t in_count;
53634219Smcpowers 	uint_t out_count;
53644219Smcpowers 	int error = 0;
53654219Smcpowers 	int rv;
53664219Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
53674219Smcpowers 	caddr_t u_attrs = NULL;
53684219Smcpowers 
53694219Smcpowers 	STRUCT_INIT(generate_key, mode);
53704219Smcpowers 	STRUCT_INIT(oa, mode);
53714219Smcpowers 
53724219Smcpowers 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
53734219Smcpowers 		cmn_err(CE_WARN, "nostore_generate_key: failed holding minor");
53744219Smcpowers 		return (ENXIO);
53754219Smcpowers 	}
53764219Smcpowers 
53774219Smcpowers 	if (copyin(arg, STRUCT_BUF(generate_key),
53784219Smcpowers 	    STRUCT_SIZE(generate_key)) != 0) {
53794219Smcpowers 		crypto_release_minor(cm);
53804219Smcpowers 		return (EFAULT);
53814219Smcpowers 	}
53824219Smcpowers 
53834219Smcpowers 	session_id = STRUCT_FGET(generate_key, ngk_session);
53844219Smcpowers 
53854219Smcpowers 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
53864219Smcpowers 		goto release_minor;
53874219Smcpowers 	}
53884219Smcpowers 
53894219Smcpowers 	bcopy(STRUCT_FADDR(generate_key, ngk_mechanism), &mech.cm_type,
53904219Smcpowers 	    sizeof (crypto_mech_type_t));
53914219Smcpowers 
53924219Smcpowers 	if ((rv = kcf_get_hardware_provider(mech.cm_type, CRYPTO_MECH_INVALID,
53934219Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider,
53944219Smcpowers 	    CRYPTO_FG_GENERATE)) != CRYPTO_SUCCESS) {
53954219Smcpowers 		goto release_minor;
53964219Smcpowers 	}
53974219Smcpowers 
53984219Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
53994219Smcpowers 	    STRUCT_FADDR(generate_key, ngk_mechanism), &mech, mode, &error);
54004219Smcpowers 
54014219Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
54024219Smcpowers 		allocated_by_crypto_module = B_TRUE;
54036424Skrishna 		if (!copyin_mech(mode, sp, STRUCT_FADDR(generate_key,
54046424Skrishna 		    ngk_mechanism), &mech, &mech_rctl_bytes,
54056424Skrishna 		    &mech_rctl_chk, &rv, &error)) {
54064219Smcpowers 			goto release_minor;
54074219Smcpowers 		}
54084219Smcpowers 	} else {
54094219Smcpowers 		if (rv != CRYPTO_SUCCESS)
54104219Smcpowers 			goto release_minor;
54114219Smcpowers 	}
54124219Smcpowers 
54134219Smcpowers 	in_count = STRUCT_FGET(generate_key, ngk_in_count);
54144219Smcpowers 	in_attributes = STRUCT_FGETP(generate_key, ngk_in_attributes);
54156424Skrishna 	if (!copyin_attributes(mode, sp, in_count, in_attributes, &k_in_attrs,
54164219Smcpowers 	    &k_in_attrs_size, NULL, &rv, &error, &in_key_rctl_bytes,
54176424Skrishna 	    &in_key_rctl_chk, B_TRUE)) {
54184219Smcpowers 		goto release_minor;
54194219Smcpowers 	}
54204219Smcpowers 
54214219Smcpowers 	out_count = STRUCT_FGET(generate_key, ngk_out_count);
54224219Smcpowers 	out_attributes = STRUCT_FGETP(generate_key, ngk_out_attributes);
54236424Skrishna 	if (!copyin_attributes(mode, sp, out_count, out_attributes,
54246424Skrishna 	    &k_out_attrs,
54254219Smcpowers 	    &k_out_attrs_size, &u_attrs, &rv, &error, &out_key_rctl_bytes,
54266424Skrishna 	    &out_key_rctl_chk, B_FALSE)) {
54274219Smcpowers 		goto release_minor;
54284219Smcpowers 	}
54294219Smcpowers 
54304219Smcpowers 	KCF_WRAP_NOSTORE_KEY_OPS_PARAMS(&params, KCF_OP_KEY_GENERATE,
54314219Smcpowers 	    sp->sd_provider_session->ps_session, &mech, k_in_attrs, in_count,
54324219Smcpowers 	    NULL, 0, NULL, k_out_attrs, out_count, NULL, 0);
54334219Smcpowers 
54344219Smcpowers 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
54354219Smcpowers 
54364219Smcpowers 	if (rv == CRYPTO_SUCCESS) {
54374219Smcpowers 		error = copyout_attributes(mode, out_attributes,
54384219Smcpowers 		    out_count, k_out_attrs, u_attrs);
54394219Smcpowers 	}
54404219Smcpowers release_minor:
54416424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
54426424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, in_key_rctl_bytes, in_key_rctl_chk);
54436424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, out_key_rctl_bytes,
54446424Skrishna 	    out_key_rctl_chk);
54454219Smcpowers 
54464219Smcpowers 	if (k_in_attrs != NULL)
54474219Smcpowers 		kmem_free(k_in_attrs, k_in_attrs_size);
54484219Smcpowers 	if (k_out_attrs != NULL) {
54494219Smcpowers 		bzero(k_out_attrs, k_out_attrs_size);
54504219Smcpowers 		kmem_free(k_out_attrs, k_out_attrs_size);
54514219Smcpowers 	}
54524219Smcpowers 
54534219Smcpowers 	if (u_attrs != NULL)
54544219Smcpowers 		kmem_free(u_attrs, out_count * STRUCT_SIZE(oa));
54554219Smcpowers 
54564219Smcpowers 	if (error != 0)
54574219Smcpowers 		goto out;
54584219Smcpowers 
54594219Smcpowers 	STRUCT_FSET(generate_key, ngk_return_value, rv);
54604219Smcpowers 	if (copyout(STRUCT_BUF(generate_key), arg,
54614219Smcpowers 	    STRUCT_SIZE(generate_key)) != 0) {
54624219Smcpowers 		error = EFAULT;
54634219Smcpowers 	}
54644219Smcpowers out:
54656424Skrishna 	CRYPTO_SESSION_RELE(sp);
54664219Smcpowers 	crypto_release_minor(cm);
54674219Smcpowers 
54684219Smcpowers 	if (real_provider != NULL) {
54694219Smcpowers 		crypto_free_mech(real_provider,
54704219Smcpowers 		    allocated_by_crypto_module, &mech);
54714219Smcpowers 		KCF_PROV_REFRELE(real_provider);
54724219Smcpowers 	}
54734219Smcpowers 	return (error);
54744219Smcpowers }
54754219Smcpowers 
54764219Smcpowers /* ARGSUSED */
54774219Smcpowers static int
54780Sstevel@tonic-gate object_generate_key_pair(dev_t dev, caddr_t arg, int mode, int *rval)
54790Sstevel@tonic-gate {
54800Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_generate_key_pair, generate_key_pair);
5481904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
54820Sstevel@tonic-gate 	kcf_req_params_t params;
54830Sstevel@tonic-gate 	crypto_mechanism_t mech;
54840Sstevel@tonic-gate 	crypto_object_attribute_t *k_pub_attrs = NULL;
54850Sstevel@tonic-gate 	crypto_object_attribute_t *k_pri_attrs = NULL;
54860Sstevel@tonic-gate 	crypto_session_id_t session_id;
54870Sstevel@tonic-gate 	crypto_minor_t *cm;
54880Sstevel@tonic-gate 	crypto_session_data_t *sp = NULL;
54890Sstevel@tonic-gate 	crypto_object_id_t pub_handle;
54900Sstevel@tonic-gate 	crypto_object_id_t pri_handle;
54910Sstevel@tonic-gate 	caddr_t pri_attributes;
54920Sstevel@tonic-gate 	caddr_t pub_attributes;
54930Sstevel@tonic-gate 	size_t k_pub_attrs_size, k_pri_attrs_size;
54940Sstevel@tonic-gate 	size_t mech_rctl_bytes = 0;
54956424Skrishna 	boolean_t mech_rctl_chk = B_FALSE;
54960Sstevel@tonic-gate 	size_t pub_rctl_bytes = 0;
54976424Skrishna 	boolean_t pub_rctl_chk = B_FALSE;
54980Sstevel@tonic-gate 	size_t pri_rctl_bytes = 0;
54996424Skrishna 	boolean_t pri_rctl_chk = B_FALSE;
55000Sstevel@tonic-gate 	uint_t pub_count;
55010Sstevel@tonic-gate 	uint_t pri_count;
55020Sstevel@tonic-gate 	int error = 0;
55030Sstevel@tonic-gate 	int rv;
5504904Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
55050Sstevel@tonic-gate 
55060Sstevel@tonic-gate 	STRUCT_INIT(generate_key_pair, mode);
55070Sstevel@tonic-gate 
55080Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
55090Sstevel@tonic-gate 		cmn_err(CE_WARN,
55100Sstevel@tonic-gate 		    "object_generate_key_pair: failed holding minor");
55110Sstevel@tonic-gate 		return (ENXIO);
55120Sstevel@tonic-gate 	}
55130Sstevel@tonic-gate 
55140Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(generate_key_pair),
55150Sstevel@tonic-gate 	    STRUCT_SIZE(generate_key_pair)) != 0) {
55160Sstevel@tonic-gate 		crypto_release_minor(cm);
55170Sstevel@tonic-gate 		return (EFAULT);
55180Sstevel@tonic-gate 	}
55190Sstevel@tonic-gate 
55200Sstevel@tonic-gate 	session_id = STRUCT_FGET(generate_key_pair, kp_session);
55210Sstevel@tonic-gate 
55220Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
55230Sstevel@tonic-gate 		goto release_minor;
55240Sstevel@tonic-gate 	}
55250Sstevel@tonic-gate 
5526904Smcpowers 	bcopy(STRUCT_FADDR(generate_key_pair, kp_mechanism), &mech.cm_type,
5527904Smcpowers 	    sizeof (crypto_mech_type_t));
5528904Smcpowers 
5529904Smcpowers 	if ((rv = kcf_get_hardware_provider(mech.cm_type, CRYPTO_MECH_INVALID,
55301808Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider,
55311808Smcpowers 	    CRYPTO_FG_GENERATE_KEY_PAIR)) != CRYPTO_SUCCESS) {
55320Sstevel@tonic-gate 		goto release_minor;
55330Sstevel@tonic-gate 	}
55340Sstevel@tonic-gate 
5535904Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
5536904Smcpowers 	    STRUCT_FADDR(generate_key_pair, kp_mechanism), &mech, mode, &error);
5537904Smcpowers 
5538904Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
5539904Smcpowers 		allocated_by_crypto_module = B_TRUE;
55406424Skrishna 		if (!copyin_mech(mode, sp, STRUCT_FADDR(generate_key_pair,
55416424Skrishna 		    kp_mechanism), &mech, &mech_rctl_bytes,
55426424Skrishna 		    &mech_rctl_chk, &rv, &error)) {
5543904Smcpowers 			goto release_minor;
5544904Smcpowers 		}
5545904Smcpowers 	} else {
5546904Smcpowers 		if (rv != CRYPTO_SUCCESS)
5547904Smcpowers 			goto release_minor;
55480Sstevel@tonic-gate 	}
55490Sstevel@tonic-gate 
55500Sstevel@tonic-gate 	pub_count = STRUCT_FGET(generate_key_pair, kp_public_count);
55510Sstevel@tonic-gate 	pri_count = STRUCT_FGET(generate_key_pair, kp_private_count);
55520Sstevel@tonic-gate 
55530Sstevel@tonic-gate 	pub_attributes = STRUCT_FGETP(generate_key_pair, kp_public_attributes);
55546424Skrishna 	if (!copyin_attributes(mode, sp, pub_count, pub_attributes,
55556424Skrishna 	    &k_pub_attrs, &k_pub_attrs_size, NULL, &rv, &error, &pub_rctl_bytes,
55566424Skrishna 	    &pub_rctl_chk, B_TRUE)) {
55570Sstevel@tonic-gate 		goto release_minor;
55580Sstevel@tonic-gate 	}
55590Sstevel@tonic-gate 
55600Sstevel@tonic-gate 	pri_attributes = STRUCT_FGETP(generate_key_pair, kp_private_attributes);
55616424Skrishna 	if (!copyin_attributes(mode, sp, pri_count, pri_attributes,
55626424Skrishna 	    &k_pri_attrs, &k_pri_attrs_size, NULL, &rv, &error,
55636424Skrishna 	    &pri_rctl_bytes, &pri_rctl_chk, B_TRUE)) {
55640Sstevel@tonic-gate 		goto release_minor;
55650Sstevel@tonic-gate 	}
55660Sstevel@tonic-gate 
55670Sstevel@tonic-gate 	KCF_WRAP_KEY_OPS_PARAMS(&params, KCF_OP_KEY_GENERATE_PAIR,
55680Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, &mech, k_pub_attrs,
55690Sstevel@tonic-gate 	    pub_count, &pub_handle, k_pri_attrs, pri_count, &pri_handle,
55700Sstevel@tonic-gate 	    NULL, NULL, 0);
55710Sstevel@tonic-gate 
55720Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
55730Sstevel@tonic-gate 
55740Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
55750Sstevel@tonic-gate 		STRUCT_FSET(generate_key_pair, kp_public_handle, pub_handle);
55760Sstevel@tonic-gate 		STRUCT_FSET(generate_key_pair, kp_private_handle, pri_handle);
55770Sstevel@tonic-gate 	}
55780Sstevel@tonic-gate 
55790Sstevel@tonic-gate release_minor:
55806424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
55816424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, pub_rctl_bytes, pub_rctl_chk);
55826424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, pri_rctl_bytes, pri_rctl_chk);
55830Sstevel@tonic-gate 
55840Sstevel@tonic-gate 	if (k_pub_attrs != NULL)
55850Sstevel@tonic-gate 		kmem_free(k_pub_attrs, k_pub_attrs_size);
55860Sstevel@tonic-gate 
55870Sstevel@tonic-gate 	if (k_pri_attrs != NULL)
55880Sstevel@tonic-gate 		kmem_free(k_pri_attrs, k_pri_attrs_size);
55890Sstevel@tonic-gate 
55900Sstevel@tonic-gate 	if (error != 0)
55910Sstevel@tonic-gate 		goto out;
55920Sstevel@tonic-gate 
55930Sstevel@tonic-gate 	STRUCT_FSET(generate_key_pair, kp_return_value, rv);
55940Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(generate_key_pair), arg,
55950Sstevel@tonic-gate 	    STRUCT_SIZE(generate_key_pair)) != 0) {
55960Sstevel@tonic-gate 		if (rv == CRYPTO_SUCCESS) {
55970Sstevel@tonic-gate 			KCF_WRAP_OBJECT_OPS_PARAMS(&params,
55980Sstevel@tonic-gate 			    KCF_OP_OBJECT_DESTROY,
55990Sstevel@tonic-gate 			    sp->sd_provider_session->ps_session, pub_handle,
56000Sstevel@tonic-gate 			    NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
56010Sstevel@tonic-gate 
56020Sstevel@tonic-gate 			(void) kcf_submit_request(real_provider, NULL,
56030Sstevel@tonic-gate 			    NULL, &params, B_FALSE);
56040Sstevel@tonic-gate 
56050Sstevel@tonic-gate 			KCF_WRAP_OBJECT_OPS_PARAMS(&params,
56060Sstevel@tonic-gate 			    KCF_OP_OBJECT_DESTROY,
56070Sstevel@tonic-gate 			    sp->sd_provider_session->ps_session, pri_handle,
56080Sstevel@tonic-gate 			    NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
56090Sstevel@tonic-gate 
56100Sstevel@tonic-gate 			(void) kcf_submit_request(real_provider, NULL,
56110Sstevel@tonic-gate 			    NULL, &params, B_FALSE);
56120Sstevel@tonic-gate 
56130Sstevel@tonic-gate 			error = EFAULT;
56140Sstevel@tonic-gate 		}
56150Sstevel@tonic-gate 	}
56160Sstevel@tonic-gate out:
56176424Skrishna 	CRYPTO_SESSION_RELE(sp);
56180Sstevel@tonic-gate 	crypto_release_minor(cm);
5619904Smcpowers 
5620904Smcpowers 	if (real_provider != NULL) {
5621904Smcpowers 		crypto_free_mech(real_provider,
5622904Smcpowers 		    allocated_by_crypto_module, &mech);
5623904Smcpowers 		KCF_PROV_REFRELE(real_provider);
5624904Smcpowers 	}
56250Sstevel@tonic-gate 	return (error);
56260Sstevel@tonic-gate }
56270Sstevel@tonic-gate 
56280Sstevel@tonic-gate /* ARGSUSED */
56290Sstevel@tonic-gate static int
56304219Smcpowers nostore_generate_key_pair(dev_t dev, caddr_t arg, int mode, int *rval)
56314219Smcpowers {
56324219Smcpowers 	STRUCT_DECL(crypto_nostore_generate_key_pair, generate_key_pair);
56334219Smcpowers 	/* LINTED E_FUNC_SET_NOT_USED */
56344219Smcpowers 	STRUCT_DECL(crypto_object_attribute, oa);
56354219Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
56364219Smcpowers 	kcf_req_params_t params;
56374219Smcpowers 	crypto_mechanism_t mech;
56384219Smcpowers 	crypto_object_attribute_t *k_in_pub_attrs = NULL;
56394219Smcpowers 	crypto_object_attribute_t *k_in_pri_attrs = NULL;
56404219Smcpowers 	crypto_object_attribute_t *k_out_pub_attrs = NULL;
56414219Smcpowers 	crypto_object_attribute_t *k_out_pri_attrs = NULL;
56424219Smcpowers 	crypto_session_id_t session_id;
56434219Smcpowers 	crypto_minor_t *cm;
56444219Smcpowers 	crypto_session_data_t *sp = NULL;
56454219Smcpowers 	caddr_t in_pri_attributes;
56464219Smcpowers 	caddr_t in_pub_attributes;
56474219Smcpowers 	caddr_t out_pri_attributes;
56484219Smcpowers 	caddr_t out_pub_attributes;
56494219Smcpowers 	size_t k_in_pub_attrs_size, k_in_pri_attrs_size;
56504219Smcpowers 	size_t k_out_pub_attrs_size, k_out_pri_attrs_size;
56514219Smcpowers 	size_t mech_rctl_bytes = 0;
56526424Skrishna 	boolean_t mech_rctl_chk = B_FALSE;
56534219Smcpowers 	size_t in_pub_rctl_bytes = 0;
56546424Skrishna 	boolean_t in_pub_rctl_chk = B_FALSE;
56554219Smcpowers 	size_t in_pri_rctl_bytes = 0;
56566424Skrishna 	boolean_t in_pri_rctl_chk = B_FALSE;
56574219Smcpowers 	size_t out_pub_rctl_bytes = 0;
56586424Skrishna 	boolean_t out_pub_rctl_chk = B_FALSE;
56594219Smcpowers 	size_t out_pri_rctl_bytes = 0;
56606424Skrishna 	boolean_t out_pri_rctl_chk = B_FALSE;
56614219Smcpowers 	uint_t in_pub_count;
56624219Smcpowers 	uint_t in_pri_count;
56634219Smcpowers 	uint_t out_pub_count;
56644219Smcpowers 	uint_t out_pri_count;
56654219Smcpowers 	int error = 0;
56664219Smcpowers 	int rv;
56674219Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
56684219Smcpowers 	caddr_t u_pub_attrs = NULL;
56694219Smcpowers 	caddr_t u_pri_attrs = NULL;
56704219Smcpowers 
56714219Smcpowers 	STRUCT_INIT(generate_key_pair, mode);
56724219Smcpowers 	STRUCT_INIT(oa, mode);
56734219Smcpowers 
56744219Smcpowers 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
56754219Smcpowers 		cmn_err(CE_WARN,
56764219Smcpowers 		    "nostore_generate_key_pair: failed holding minor");
56774219Smcpowers 		return (ENXIO);
56784219Smcpowers 	}
56794219Smcpowers 
56804219Smcpowers 	if (copyin(arg, STRUCT_BUF(generate_key_pair),
56814219Smcpowers 	    STRUCT_SIZE(generate_key_pair)) != 0) {
56824219Smcpowers 		crypto_release_minor(cm);
56834219Smcpowers 		return (EFAULT);
56844219Smcpowers 	}
56854219Smcpowers 
56864219Smcpowers 	session_id = STRUCT_FGET(generate_key_pair, nkp_session);
56874219Smcpowers 
56884219Smcpowers 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
56894219Smcpowers 		goto release_minor;
56904219Smcpowers 	}
56914219Smcpowers 
56924219Smcpowers 	bcopy(STRUCT_FADDR(generate_key_pair, nkp_mechanism), &mech.cm_type,
56934219Smcpowers 	    sizeof (crypto_mech_type_t));
56944219Smcpowers 
56954219Smcpowers 	if ((rv = kcf_get_hardware_provider(mech.cm_type, CRYPTO_MECH_INVALID,
56964219Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider,
56974219Smcpowers 	    CRYPTO_FG_GENERATE_KEY_PAIR)) != CRYPTO_SUCCESS) {
56984219Smcpowers 		goto release_minor;
56994219Smcpowers 	}
57004219Smcpowers 
57014219Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
57024219Smcpowers 	    STRUCT_FADDR(generate_key_pair, nkp_mechanism), &mech, mode,
57034219Smcpowers 	    &error);
57044219Smcpowers 
57054219Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
57064219Smcpowers 		allocated_by_crypto_module = B_TRUE;
57076424Skrishna 		if (!copyin_mech(mode, sp, STRUCT_FADDR(generate_key_pair,
57086424Skrishna 		    nkp_mechanism), &mech, &mech_rctl_bytes,
57096424Skrishna 		    &mech_rctl_chk, &rv, &error)) {
57104219Smcpowers 			goto release_minor;
57114219Smcpowers 		}
57124219Smcpowers 	} else {
57134219Smcpowers 		if (rv != CRYPTO_SUCCESS)
57144219Smcpowers 			goto release_minor;
57154219Smcpowers 	}
57164219Smcpowers 
57174219Smcpowers 	in_pub_count = STRUCT_FGET(generate_key_pair, nkp_in_public_count);
57184219Smcpowers 	in_pri_count = STRUCT_FGET(generate_key_pair, nkp_in_private_count);
57194219Smcpowers 
57204219Smcpowers 	in_pub_attributes = STRUCT_FGETP(generate_key_pair,
57214219Smcpowers 	    nkp_in_public_attributes);
57226424Skrishna 	if (!copyin_attributes(mode, sp, in_pub_count, in_pub_attributes,
57234219Smcpowers 	    &k_in_pub_attrs, &k_in_pub_attrs_size, NULL, &rv, &error,
57246424Skrishna 	    &in_pub_rctl_bytes, &in_pub_rctl_chk, B_TRUE)) {
57254219Smcpowers 		goto release_minor;
57264219Smcpowers 	}
57274219Smcpowers 
57284219Smcpowers 	in_pri_attributes = STRUCT_FGETP(generate_key_pair,
57294219Smcpowers 	    nkp_in_private_attributes);
57306424Skrishna 	if (!copyin_attributes(mode, sp, in_pri_count, in_pri_attributes,
57314219Smcpowers 	    &k_in_pri_attrs, &k_in_pri_attrs_size, NULL, &rv, &error,
57326424Skrishna 	    &in_pri_rctl_bytes, &in_pri_rctl_chk, B_TRUE)) {
57334219Smcpowers 		goto release_minor;
57344219Smcpowers 	}
57354219Smcpowers 
57364219Smcpowers 	out_pub_count = STRUCT_FGET(generate_key_pair, nkp_out_public_count);
57374219Smcpowers 	out_pri_count = STRUCT_FGET(generate_key_pair, nkp_out_private_count);
57384219Smcpowers 
57394219Smcpowers 	out_pub_attributes = STRUCT_FGETP(generate_key_pair,
57404219Smcpowers 	    nkp_out_public_attributes);
57416424Skrishna 	if (!copyin_attributes(mode, sp, out_pub_count, out_pub_attributes,
57424219Smcpowers 	    &k_out_pub_attrs, &k_out_pub_attrs_size, &u_pub_attrs, &rv, &error,
57436424Skrishna 	    &out_pub_rctl_bytes, &out_pub_rctl_chk, B_FALSE)) {
57444219Smcpowers 		goto release_minor;
57454219Smcpowers 	}
57464219Smcpowers 
57474219Smcpowers 	out_pri_attributes = STRUCT_FGETP(generate_key_pair,
57484219Smcpowers 	    nkp_out_private_attributes);
57496424Skrishna 	if (!copyin_attributes(mode, sp, out_pri_count, out_pri_attributes,
57504219Smcpowers 	    &k_out_pri_attrs, &k_out_pri_attrs_size, &u_pri_attrs, &rv, &error,
57516424Skrishna 	    &out_pri_rctl_bytes, &out_pri_rctl_chk, B_FALSE)) {
57524219Smcpowers 		goto release_minor;
57534219Smcpowers 	}
57544219Smcpowers 
57554219Smcpowers 	KCF_WRAP_NOSTORE_KEY_OPS_PARAMS(&params, KCF_OP_KEY_GENERATE_PAIR,
57564219Smcpowers 	    sp->sd_provider_session->ps_session, &mech, k_in_pub_attrs,
57574219Smcpowers 	    in_pub_count, k_in_pri_attrs, in_pri_count, NULL, k_out_pub_attrs,
57584219Smcpowers 	    out_pub_count, k_out_pri_attrs, out_pri_count);
57594219Smcpowers 
57604219Smcpowers 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
57614219Smcpowers 
57624219Smcpowers 	if (rv == CRYPTO_SUCCESS) {
57634219Smcpowers 		error = copyout_attributes(mode, out_pub_attributes,
57644219Smcpowers 		    out_pub_count, k_out_pub_attrs, u_pub_attrs);
57654219Smcpowers 		if (error != CRYPTO_SUCCESS)
57664219Smcpowers 			goto release_minor;
57674219Smcpowers 		error = copyout_attributes(mode, out_pri_attributes,
57684219Smcpowers 		    out_pri_count, k_out_pri_attrs, u_pri_attrs);
57694219Smcpowers 	}
57704219Smcpowers 
57714219Smcpowers release_minor:
57726424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
57736424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, in_pub_rctl_bytes, in_pub_rctl_chk);
57746424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, in_pri_rctl_bytes, in_pri_rctl_chk);
57756424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, out_pub_rctl_bytes,
57766424Skrishna 	    out_pub_rctl_chk);
57776424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, out_pri_rctl_bytes,
57786424Skrishna 	    out_pri_rctl_chk);
57794219Smcpowers 
57804219Smcpowers 	if (k_in_pub_attrs != NULL)
57814219Smcpowers 		kmem_free(k_in_pub_attrs, k_in_pub_attrs_size);
57824219Smcpowers 
57834219Smcpowers 	if (k_in_pri_attrs != NULL)
57844219Smcpowers 		kmem_free(k_in_pri_attrs, k_in_pri_attrs_size);
57854219Smcpowers 
57864219Smcpowers 	if (k_out_pub_attrs != NULL)
57874219Smcpowers 		kmem_free(k_out_pub_attrs, k_out_pub_attrs_size);
57884219Smcpowers 
57894219Smcpowers 	if (k_out_pri_attrs != NULL) {
57904219Smcpowers 		bzero(k_out_pri_attrs, k_out_pri_attrs_size);
57914219Smcpowers 		kmem_free(k_out_pri_attrs, k_out_pri_attrs_size);
57924219Smcpowers 	}
57934219Smcpowers 
57944219Smcpowers 	if (u_pub_attrs != NULL)
57954219Smcpowers 		kmem_free(u_pub_attrs, out_pub_count * STRUCT_SIZE(oa));
57964219Smcpowers 
57974219Smcpowers 	if (u_pri_attrs != NULL)
57984219Smcpowers 		kmem_free(u_pri_attrs, out_pri_count * STRUCT_SIZE(oa));
57994219Smcpowers 
58004219Smcpowers 	if (error != 0)
58014219Smcpowers 		goto out;
58024219Smcpowers 
58034219Smcpowers 	STRUCT_FSET(generate_key_pair, nkp_return_value, rv);
58044219Smcpowers 	if (copyout(STRUCT_BUF(generate_key_pair), arg,
58054219Smcpowers 	    STRUCT_SIZE(generate_key_pair)) != 0) {
58064219Smcpowers 		error = EFAULT;
58074219Smcpowers 	}
58084219Smcpowers out:
58096424Skrishna 	CRYPTO_SESSION_RELE(sp);
58104219Smcpowers 	crypto_release_minor(cm);
58114219Smcpowers 
58124219Smcpowers 	if (real_provider != NULL) {
58134219Smcpowers 		crypto_free_mech(real_provider,
58144219Smcpowers 		    allocated_by_crypto_module, &mech);
58154219Smcpowers 		KCF_PROV_REFRELE(real_provider);
58164219Smcpowers 	}
58174219Smcpowers 	return (error);
58184219Smcpowers }
58194219Smcpowers 
58204219Smcpowers /* ARGSUSED */
58214219Smcpowers static int
58220Sstevel@tonic-gate object_wrap_key(dev_t dev, caddr_t arg, int mode, int *rval)
58230Sstevel@tonic-gate {
58240Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_wrap_key, wrap_key);
5825904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
58260Sstevel@tonic-gate 	kcf_req_params_t params;
58270Sstevel@tonic-gate 	crypto_mechanism_t mech;
58280Sstevel@tonic-gate 	crypto_key_t key;
58290Sstevel@tonic-gate 	crypto_session_id_t session_id;
58300Sstevel@tonic-gate 	crypto_minor_t *cm;
58316424Skrishna 	crypto_session_data_t *sp = NULL;
58320Sstevel@tonic-gate 	crypto_object_id_t handle;
58330Sstevel@tonic-gate 	size_t mech_rctl_bytes = 0, key_rctl_bytes = 0;
58346424Skrishna 	boolean_t mech_rctl_chk = B_FALSE;
58356424Skrishna 	boolean_t key_rctl_chk = B_FALSE;
58360Sstevel@tonic-gate 	size_t wrapped_key_rctl_bytes = 0;
58376424Skrishna 	boolean_t wrapped_key_rctl_chk = B_FALSE;
58380Sstevel@tonic-gate 	size_t wrapped_key_len, new_wrapped_key_len;
58390Sstevel@tonic-gate 	uchar_t *wrapped_key = NULL;
58400Sstevel@tonic-gate 	char *wrapped_key_buffer;
58410Sstevel@tonic-gate 	int error = 0;
58420Sstevel@tonic-gate 	int rv;
5843904Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
58440Sstevel@tonic-gate 
58450Sstevel@tonic-gate 	STRUCT_INIT(wrap_key, mode);
58460Sstevel@tonic-gate 
58470Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
58480Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_wrap_key: failed holding minor");
58490Sstevel@tonic-gate 		return (ENXIO);
58500Sstevel@tonic-gate 	}
58510Sstevel@tonic-gate 
58520Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(wrap_key), STRUCT_SIZE(wrap_key)) != 0) {
58530Sstevel@tonic-gate 		crypto_release_minor(cm);
58540Sstevel@tonic-gate 		return (EFAULT);
58550Sstevel@tonic-gate 	}
58560Sstevel@tonic-gate 
58570Sstevel@tonic-gate 	bzero(&key, sizeof (crypto_key_t));
58580Sstevel@tonic-gate 
58590Sstevel@tonic-gate 	session_id = STRUCT_FGET(wrap_key, wk_session);
58600Sstevel@tonic-gate 
58610Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
58626424Skrishna 		goto out;
58630Sstevel@tonic-gate 	}
58640Sstevel@tonic-gate 
5865904Smcpowers 	bcopy(STRUCT_FADDR(wrap_key, wk_mechanism), &mech.cm_type,
5866904Smcpowers 	    sizeof (crypto_mech_type_t));
5867904Smcpowers 
5868904Smcpowers 	if ((rv = kcf_get_hardware_provider(mech.cm_type, CRYPTO_MECH_INVALID,
58691808Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider,
58701808Smcpowers 	    CRYPTO_FG_WRAP)) != CRYPTO_SUCCESS) {
58710Sstevel@tonic-gate 		goto out;
58720Sstevel@tonic-gate 	}
58730Sstevel@tonic-gate 
5874904Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
5875904Smcpowers 	    STRUCT_FADDR(wrap_key, wk_mechanism), &mech, mode, &error);
5876904Smcpowers 
5877904Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
5878904Smcpowers 		allocated_by_crypto_module = B_TRUE;
58796424Skrishna 		if (!copyin_mech(mode, sp, STRUCT_FADDR(wrap_key, wk_mechanism),
58806424Skrishna 		    &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) {
5881904Smcpowers 			goto out;
5882904Smcpowers 		}
5883904Smcpowers 	} else {
5884904Smcpowers 		if (rv != CRYPTO_SUCCESS)
5885904Smcpowers 			goto out;
58860Sstevel@tonic-gate 	}
58870Sstevel@tonic-gate 
58886424Skrishna 	if (!copyin_key(mode, sp, STRUCT_FADDR(wrap_key, wk_wrapping_key), &key,
58896424Skrishna 	    &key_rctl_bytes, &key_rctl_chk, &rv, &error)) {
58900Sstevel@tonic-gate 		goto out;
58910Sstevel@tonic-gate 	}
58920Sstevel@tonic-gate 
58930Sstevel@tonic-gate 	wrapped_key_len = STRUCT_FGET(wrap_key, wk_wrapped_key_len);
58940Sstevel@tonic-gate 
58950Sstevel@tonic-gate 	/*
58960Sstevel@tonic-gate 	 * Don't allocate output buffer unless both buffer pointer and
58970Sstevel@tonic-gate 	 * buffer length are not NULL or 0 (length).
58980Sstevel@tonic-gate 	 */
58990Sstevel@tonic-gate 	wrapped_key_buffer = STRUCT_FGETP(wrap_key, wk_wrapped_key);
59000Sstevel@tonic-gate 	if (wrapped_key_buffer == NULL || wrapped_key_len == 0) {
59010Sstevel@tonic-gate 		wrapped_key_len = 0;
59020Sstevel@tonic-gate 	}
59030Sstevel@tonic-gate 
59040Sstevel@tonic-gate 	if (wrapped_key_len > crypto_max_buffer_len) {
59050Sstevel@tonic-gate 		cmn_err(CE_NOTE, "object_wrap_key: buffer greater than %ld "
59060Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
59070Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
59080Sstevel@tonic-gate 		goto out;
59090Sstevel@tonic-gate 	}
59100Sstevel@tonic-gate 
59116424Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, wrapped_key_len,
59126424Skrishna 	    wrapped_key_rctl_chk)) != CRYPTO_SUCCESS) {
59130Sstevel@tonic-gate 		goto out;
59140Sstevel@tonic-gate 	}
59150Sstevel@tonic-gate 
59160Sstevel@tonic-gate 	/* new_wrapped_key_len can be modified by the provider */
59170Sstevel@tonic-gate 	wrapped_key_rctl_bytes = new_wrapped_key_len = wrapped_key_len;
59180Sstevel@tonic-gate 	wrapped_key = kmem_alloc(wrapped_key_len, KM_SLEEP);
59190Sstevel@tonic-gate 
59200Sstevel@tonic-gate 	handle = STRUCT_FGET(wrap_key, wk_object_handle);
59210Sstevel@tonic-gate 	KCF_WRAP_KEY_OPS_PARAMS(&params, KCF_OP_KEY_WRAP,
59220Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, &mech, NULL, 0, &handle,
59230Sstevel@tonic-gate 	    NULL, 0, NULL, &key, wrapped_key, &new_wrapped_key_len);
59240Sstevel@tonic-gate 
59250Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
59260Sstevel@tonic-gate 
59270Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
59280Sstevel@tonic-gate 		if (wrapped_key_len != 0 && copyout(wrapped_key,
59290Sstevel@tonic-gate 		    wrapped_key_buffer, new_wrapped_key_len) != 0) {
59300Sstevel@tonic-gate 			error = EFAULT;
59310Sstevel@tonic-gate 		}
59320Sstevel@tonic-gate 		STRUCT_FSET(wrap_key, wk_wrapped_key_len, new_wrapped_key_len);
59330Sstevel@tonic-gate 	}
59340Sstevel@tonic-gate 
59350Sstevel@tonic-gate 	if (rv == CRYPTO_BUFFER_TOO_SMALL) {
59360Sstevel@tonic-gate 		/*
59370Sstevel@tonic-gate 		 * The providers return CRYPTO_BUFFER_TOO_SMALL even for case 1
59380Sstevel@tonic-gate 		 * of section 11.2 of the pkcs11 spec. We catch it here and
59390Sstevel@tonic-gate 		 * provide the correct pkcs11 return value.
59400Sstevel@tonic-gate 		 */
59410Sstevel@tonic-gate 		if (STRUCT_FGETP(wrap_key, wk_wrapped_key) == NULL)
59420Sstevel@tonic-gate 			rv = CRYPTO_SUCCESS;
59430Sstevel@tonic-gate 		STRUCT_FSET(wrap_key, wk_wrapped_key_len, new_wrapped_key_len);
59440Sstevel@tonic-gate 	}
59456424Skrishna 
59460Sstevel@tonic-gate out:
59476424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
59486424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk);
59496424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, wrapped_key_rctl_bytes,
59506424Skrishna 	    wrapped_key_rctl_chk);
59510Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
59520Sstevel@tonic-gate 
59530Sstevel@tonic-gate 	crypto_release_minor(cm);
59540Sstevel@tonic-gate 
5955904Smcpowers 	if (real_provider != NULL) {
5956904Smcpowers 		crypto_free_mech(real_provider,
5957904Smcpowers 		    allocated_by_crypto_module, &mech);
5958904Smcpowers 		KCF_PROV_REFRELE(real_provider);
5959904Smcpowers 	}
5960904Smcpowers 
59610Sstevel@tonic-gate 	if (wrapped_key != NULL)
59620Sstevel@tonic-gate 		kmem_free(wrapped_key, wrapped_key_len);
59630Sstevel@tonic-gate 
59640Sstevel@tonic-gate 	free_crypto_key(&key);
59650Sstevel@tonic-gate 
59660Sstevel@tonic-gate 	if (error != 0)
59670Sstevel@tonic-gate 		return (error);
59680Sstevel@tonic-gate 
59690Sstevel@tonic-gate 	STRUCT_FSET(wrap_key, wk_return_value, rv);
59700Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(wrap_key), arg, STRUCT_SIZE(wrap_key)) != 0) {
59710Sstevel@tonic-gate 		return (EFAULT);
59720Sstevel@tonic-gate 	}
59730Sstevel@tonic-gate 	return (0);
59740Sstevel@tonic-gate }
59750Sstevel@tonic-gate 
59760Sstevel@tonic-gate /* ARGSUSED */
59770Sstevel@tonic-gate static int
59780Sstevel@tonic-gate object_unwrap_key(dev_t dev, caddr_t arg, int mode, int *rval)
59790Sstevel@tonic-gate {
59800Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_unwrap_key, unwrap_key);
5981904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
59820Sstevel@tonic-gate 	kcf_req_params_t params;
59830Sstevel@tonic-gate 	crypto_mechanism_t mech;
59840Sstevel@tonic-gate 	crypto_key_t unwrapping_key;
59850Sstevel@tonic-gate 	crypto_session_id_t session_id;
59860Sstevel@tonic-gate 	crypto_minor_t *cm;
59870Sstevel@tonic-gate 	crypto_session_data_t *sp = NULL;
59880Sstevel@tonic-gate 	crypto_object_id_t handle;
59890Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
59900Sstevel@tonic-gate 	size_t k_attrs_size;
59910Sstevel@tonic-gate 	size_t mech_rctl_bytes = 0, unwrapping_key_rctl_bytes = 0;
59926424Skrishna 	boolean_t mech_rctl_chk = B_FALSE;
59936424Skrishna 	boolean_t unwrapping_key_rctl_chk = B_FALSE;
59940Sstevel@tonic-gate 	size_t wrapped_key_rctl_bytes = 0, k_attrs_rctl_bytes = 0;
59956424Skrishna 	boolean_t wrapped_key_rctl_chk = B_FALSE;
59966424Skrishna 	boolean_t k_attrs_rctl_chk = B_FALSE;
59970Sstevel@tonic-gate 	size_t wrapped_key_len;
59980Sstevel@tonic-gate 	uchar_t *wrapped_key = NULL;
59990Sstevel@tonic-gate 	int error = 0;
60000Sstevel@tonic-gate 	int rv;
60010Sstevel@tonic-gate 	uint_t count;
60020Sstevel@tonic-gate 	caddr_t uk_attributes;
6003904Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
60040Sstevel@tonic-gate 
60050Sstevel@tonic-gate 	STRUCT_INIT(unwrap_key, mode);
60060Sstevel@tonic-gate 
60070Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
60080Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_unwrap_key: failed holding minor");
60090Sstevel@tonic-gate 		return (ENXIO);
60100Sstevel@tonic-gate 	}
60110Sstevel@tonic-gate 
60120Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(unwrap_key), STRUCT_SIZE(unwrap_key)) != 0) {
60130Sstevel@tonic-gate 		crypto_release_minor(cm);
60140Sstevel@tonic-gate 		return (EFAULT);
60150Sstevel@tonic-gate 	}
60160Sstevel@tonic-gate 
60170Sstevel@tonic-gate 	bzero(&unwrapping_key, sizeof (unwrapping_key));
60180Sstevel@tonic-gate 
60190Sstevel@tonic-gate 	session_id = STRUCT_FGET(unwrap_key, uk_session);
60200Sstevel@tonic-gate 
60210Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
60220Sstevel@tonic-gate 		goto release_minor;
60230Sstevel@tonic-gate 	}
60240Sstevel@tonic-gate 
6025904Smcpowers 	bcopy(STRUCT_FADDR(unwrap_key, uk_mechanism), &mech.cm_type,
6026904Smcpowers 	    sizeof (crypto_mech_type_t));
6027904Smcpowers 
6028904Smcpowers 	if ((rv = kcf_get_hardware_provider(mech.cm_type, CRYPTO_MECH_INVALID,
60291808Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider,
60301808Smcpowers 	    CRYPTO_FG_UNWRAP)) != CRYPTO_SUCCESS) {
60310Sstevel@tonic-gate 		goto release_minor;
60320Sstevel@tonic-gate 	}
60330Sstevel@tonic-gate 
6034904Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
6035904Smcpowers 	    STRUCT_FADDR(unwrap_key, uk_mechanism), &mech, mode, &error);
6036904Smcpowers 
6037904Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
6038904Smcpowers 		allocated_by_crypto_module = B_TRUE;
60396424Skrishna 		if (!copyin_mech(mode, sp,
60406424Skrishna 		    STRUCT_FADDR(unwrap_key, uk_mechanism),
60416424Skrishna 		    &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) {
6042904Smcpowers 			goto release_minor;
6043904Smcpowers 		}
6044904Smcpowers 	} else {
6045904Smcpowers 		if (rv != CRYPTO_SUCCESS)
6046904Smcpowers 			goto release_minor;
60470Sstevel@tonic-gate 	}
60480Sstevel@tonic-gate 
60496424Skrishna 	if (!copyin_key(mode, sp, STRUCT_FADDR(unwrap_key, uk_unwrapping_key),
60506424Skrishna 	    &unwrapping_key, &unwrapping_key_rctl_bytes,
60516424Skrishna 	    &unwrapping_key_rctl_chk, &rv, &error)) {
60520Sstevel@tonic-gate 		goto release_minor;
60530Sstevel@tonic-gate 	}
60540Sstevel@tonic-gate 
60550Sstevel@tonic-gate 	count = STRUCT_FGET(unwrap_key, uk_count);
60560Sstevel@tonic-gate 	uk_attributes = STRUCT_FGETP(unwrap_key, uk_attributes);
60576424Skrishna 	if (!copyin_attributes(mode, sp, count, uk_attributes, &k_attrs,
60586424Skrishna 	    &k_attrs_size, NULL, &rv, &error, &k_attrs_rctl_bytes,
60596424Skrishna 	    &k_attrs_rctl_chk, B_TRUE)) {
60600Sstevel@tonic-gate 		goto release_minor;
60610Sstevel@tonic-gate 	}
60620Sstevel@tonic-gate 
60630Sstevel@tonic-gate 	wrapped_key_len = STRUCT_FGET(unwrap_key, uk_wrapped_key_len);
60640Sstevel@tonic-gate 	if (wrapped_key_len > crypto_max_buffer_len) {
60650Sstevel@tonic-gate 		cmn_err(CE_NOTE, "object_unwrap_key: buffer greater than %ld "
60660Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
60670Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
60680Sstevel@tonic-gate 		goto release_minor;
60690Sstevel@tonic-gate 	}
60700Sstevel@tonic-gate 
60716424Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, wrapped_key_len,
60726424Skrishna 	    wrapped_key_rctl_chk)) != CRYPTO_SUCCESS) {
60730Sstevel@tonic-gate 		goto release_minor;
60740Sstevel@tonic-gate 	}
60750Sstevel@tonic-gate 	wrapped_key_rctl_bytes = wrapped_key_len;
60760Sstevel@tonic-gate 	wrapped_key = kmem_alloc(wrapped_key_len, KM_SLEEP);
60770Sstevel@tonic-gate 
60780Sstevel@tonic-gate 	if (wrapped_key_len != 0 && copyin(STRUCT_FGETP(unwrap_key,
60790Sstevel@tonic-gate 	    uk_wrapped_key), wrapped_key, wrapped_key_len) != 0) {
60800Sstevel@tonic-gate 		error = EFAULT;
60810Sstevel@tonic-gate 		goto release_minor;
60820Sstevel@tonic-gate 	}
60830Sstevel@tonic-gate 
60840Sstevel@tonic-gate 	/* wrapped_key_len is not modified by the unwrap operation */
60850Sstevel@tonic-gate 	KCF_WRAP_KEY_OPS_PARAMS(&params, KCF_OP_KEY_UNWRAP,
60860Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, &mech, k_attrs, count, &handle,
60870Sstevel@tonic-gate 	    NULL, 0, NULL, &unwrapping_key, wrapped_key, &wrapped_key_len);
60880Sstevel@tonic-gate 
60890Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
60900Sstevel@tonic-gate 
60910Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS)
60920Sstevel@tonic-gate 		STRUCT_FSET(unwrap_key, uk_object_handle, handle);
60930Sstevel@tonic-gate 
60940Sstevel@tonic-gate release_minor:
60956424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
60966424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, unwrapping_key_rctl_bytes,
60976424Skrishna 	    unwrapping_key_rctl_chk);
60986424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, wrapped_key_rctl_bytes,
60996424Skrishna 	    wrapped_key_rctl_chk);
61006424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, k_attrs_rctl_bytes,
61016424Skrishna 	    k_attrs_rctl_chk);
61020Sstevel@tonic-gate 
61030Sstevel@tonic-gate 	if (k_attrs != NULL)
61040Sstevel@tonic-gate 		kmem_free(k_attrs, k_attrs_size);
61050Sstevel@tonic-gate 
61060Sstevel@tonic-gate 	if (wrapped_key != NULL)
61070Sstevel@tonic-gate 		kmem_free(wrapped_key, wrapped_key_len);
61080Sstevel@tonic-gate 
61090Sstevel@tonic-gate 	free_crypto_key(&unwrapping_key);
61100Sstevel@tonic-gate 
61110Sstevel@tonic-gate 	if (error != 0)
61120Sstevel@tonic-gate 		goto out;
61130Sstevel@tonic-gate 
61140Sstevel@tonic-gate 	STRUCT_FSET(unwrap_key, uk_return_value, rv);
61150Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(unwrap_key), arg,
61160Sstevel@tonic-gate 	    STRUCT_SIZE(unwrap_key)) != 0) {
61170Sstevel@tonic-gate 		if (rv == CRYPTO_SUCCESS) {
61180Sstevel@tonic-gate 			KCF_WRAP_OBJECT_OPS_PARAMS(&params,
61190Sstevel@tonic-gate 			    KCF_OP_OBJECT_DESTROY,
61200Sstevel@tonic-gate 			    sp->sd_provider_session->ps_session, handle,
61210Sstevel@tonic-gate 			    NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
61220Sstevel@tonic-gate 
61230Sstevel@tonic-gate 			(void) kcf_submit_request(real_provider, NULL,
61240Sstevel@tonic-gate 			    NULL, &params, B_FALSE);
61250Sstevel@tonic-gate 
61260Sstevel@tonic-gate 			error = EFAULT;
61270Sstevel@tonic-gate 		}
61280Sstevel@tonic-gate 	}
61290Sstevel@tonic-gate out:
61306424Skrishna 	CRYPTO_SESSION_RELE(sp);
61310Sstevel@tonic-gate 	crypto_release_minor(cm);
6132904Smcpowers 
6133904Smcpowers 	if (real_provider != NULL) {
6134904Smcpowers 		crypto_free_mech(real_provider,
6135904Smcpowers 		    allocated_by_crypto_module, &mech);
6136904Smcpowers 		KCF_PROV_REFRELE(real_provider);
6137904Smcpowers 	}
6138904Smcpowers 
61390Sstevel@tonic-gate 	return (error);
61400Sstevel@tonic-gate }
61410Sstevel@tonic-gate 
61420Sstevel@tonic-gate /* ARGSUSED */
61430Sstevel@tonic-gate static int
61440Sstevel@tonic-gate object_derive_key(dev_t dev, caddr_t arg, int mode, int *rval)
61450Sstevel@tonic-gate {
61460Sstevel@tonic-gate 	STRUCT_DECL(crypto_derive_key, derive_key);
6147904Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
61480Sstevel@tonic-gate 	kcf_req_params_t params;
61490Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
61500Sstevel@tonic-gate 	crypto_mechanism_t mech;
61510Sstevel@tonic-gate 	crypto_key_t base_key;
61520Sstevel@tonic-gate 	crypto_session_id_t session_id;
61530Sstevel@tonic-gate 	crypto_minor_t *cm;
61540Sstevel@tonic-gate 	crypto_session_data_t *sp = NULL;
61550Sstevel@tonic-gate 	crypto_object_id_t handle;
61560Sstevel@tonic-gate 	size_t k_attrs_size;
61570Sstevel@tonic-gate 	size_t key_rctl_bytes = 0, mech_rctl_bytes = 0;
61586424Skrishna 	boolean_t mech_rctl_chk = B_FALSE;
61596424Skrishna 	boolean_t key_rctl_chk = B_FALSE;
61600Sstevel@tonic-gate 	size_t attributes_rctl_bytes = 0;
61616424Skrishna 	boolean_t attributes_rctl_chk = B_FALSE;
61620Sstevel@tonic-gate 	caddr_t attributes;
61630Sstevel@tonic-gate 	uint_t count;
61640Sstevel@tonic-gate 	int error = 0;
61650Sstevel@tonic-gate 	int rv;
6166904Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
6167904Smcpowers 	boolean_t please_destroy_object = B_FALSE;
61680Sstevel@tonic-gate 
61690Sstevel@tonic-gate 	STRUCT_INIT(derive_key, mode);
61700Sstevel@tonic-gate 
61710Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
61720Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_derive_key: failed holding minor");
61730Sstevel@tonic-gate 		return (ENXIO);
61740Sstevel@tonic-gate 	}
61750Sstevel@tonic-gate 
61760Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(derive_key), STRUCT_SIZE(derive_key)) != 0) {
61770Sstevel@tonic-gate 		crypto_release_minor(cm);
61780Sstevel@tonic-gate 		return (EFAULT);
61790Sstevel@tonic-gate 	}
61800Sstevel@tonic-gate 
61810Sstevel@tonic-gate 	bzero(&base_key, sizeof (base_key));
61820Sstevel@tonic-gate 
61830Sstevel@tonic-gate 	session_id = STRUCT_FGET(derive_key, dk_session);
61840Sstevel@tonic-gate 
61850Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
61860Sstevel@tonic-gate 		goto release_minor;
61870Sstevel@tonic-gate 	}
61880Sstevel@tonic-gate 
6189904Smcpowers 	bcopy(STRUCT_FADDR(derive_key, dk_mechanism), &mech.cm_type,
6190904Smcpowers 	    sizeof (crypto_mech_type_t));
6191904Smcpowers 
6192904Smcpowers 	if ((rv = kcf_get_hardware_provider(mech.cm_type, CRYPTO_MECH_INVALID,
61931808Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider,
61941808Smcpowers 	    CRYPTO_FG_DERIVE)) != CRYPTO_SUCCESS) {
61950Sstevel@tonic-gate 		goto release_minor;
61960Sstevel@tonic-gate 	}
61970Sstevel@tonic-gate 
6198904Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
6199904Smcpowers 	    STRUCT_FADDR(derive_key, dk_mechanism), &mech, mode, &error);
6200904Smcpowers 
6201904Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
6202904Smcpowers 		allocated_by_crypto_module = B_TRUE;
62036424Skrishna 		if (!copyin_mech(mode, sp,
62046424Skrishna 		    STRUCT_FADDR(derive_key, dk_mechanism),
62056424Skrishna 		    &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) {
6206904Smcpowers 			goto release_minor;
6207904Smcpowers 		}
6208904Smcpowers 	} else {
6209904Smcpowers 		if (rv != CRYPTO_SUCCESS)
6210904Smcpowers 			goto release_minor;
62110Sstevel@tonic-gate 	}
62120Sstevel@tonic-gate 
62136424Skrishna 	if (!copyin_key(mode, sp, STRUCT_FADDR(derive_key, dk_base_key),
62146424Skrishna 	    &base_key, &key_rctl_bytes, &key_rctl_chk, &rv, &error)) {
62150Sstevel@tonic-gate 		goto release_minor;
62160Sstevel@tonic-gate 	}
62170Sstevel@tonic-gate 
62180Sstevel@tonic-gate 	count = STRUCT_FGET(derive_key, dk_count);
62190Sstevel@tonic-gate 
62200Sstevel@tonic-gate 	attributes = STRUCT_FGETP(derive_key, dk_attributes);
62216424Skrishna 	if (!copyin_attributes(mode, sp, count, attributes, &k_attrs,
62223916Skrishna 	    &k_attrs_size, NULL, &rv, &error,
62236424Skrishna 	    &attributes_rctl_bytes, &attributes_rctl_chk, B_TRUE)) {
62240Sstevel@tonic-gate 		goto release_minor;
62250Sstevel@tonic-gate 	}
62260Sstevel@tonic-gate 
62270Sstevel@tonic-gate 	KCF_WRAP_KEY_OPS_PARAMS(&params, KCF_OP_KEY_DERIVE,
62280Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, &mech, k_attrs, count,
62290Sstevel@tonic-gate 	    &handle, NULL, 0, NULL, &base_key, NULL, NULL);
62300Sstevel@tonic-gate 
62310Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
62320Sstevel@tonic-gate 
6233904Smcpowers 	if (rv == CRYPTO_SUCCESS) {
62340Sstevel@tonic-gate 		STRUCT_FSET(derive_key, dk_object_handle, handle);
62350Sstevel@tonic-gate 
6236904Smcpowers 		rv = crypto_provider_copyout_mech_param(real_provider,
6237904Smcpowers 		    &mech, STRUCT_FADDR(derive_key, dk_mechanism),
6238904Smcpowers 		    mode, &error);
6239904Smcpowers 
6240904Smcpowers 		if (rv == CRYPTO_NOT_SUPPORTED) {
6241904Smcpowers 			rv = CRYPTO_SUCCESS;
6242904Smcpowers 			goto release_minor;
6243904Smcpowers 		}
6244904Smcpowers 
6245904Smcpowers 		if (rv != CRYPTO_SUCCESS)
6246904Smcpowers 			please_destroy_object = B_TRUE;
6247904Smcpowers 	}
6248904Smcpowers 
62490Sstevel@tonic-gate release_minor:
62506424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
62516424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk);
62526424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, attributes_rctl_bytes,
62536424Skrishna 	    attributes_rctl_chk);
62540Sstevel@tonic-gate 
62550Sstevel@tonic-gate 	if (k_attrs != NULL)
62560Sstevel@tonic-gate 		kmem_free(k_attrs, k_attrs_size);
62570Sstevel@tonic-gate 
62580Sstevel@tonic-gate 	free_crypto_key(&base_key);
62590Sstevel@tonic-gate 
62600Sstevel@tonic-gate 	if (error != 0)
62610Sstevel@tonic-gate 		goto out;
62620Sstevel@tonic-gate 
62630Sstevel@tonic-gate 	STRUCT_FSET(derive_key, dk_return_value, rv);
62640Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(derive_key), arg,
62650Sstevel@tonic-gate 	    STRUCT_SIZE(derive_key)) != 0) {
62660Sstevel@tonic-gate 		if (rv == CRYPTO_SUCCESS) {
6267904Smcpowers 			please_destroy_object = B_TRUE;
62680Sstevel@tonic-gate 			error = EFAULT;
62690Sstevel@tonic-gate 		}
62700Sstevel@tonic-gate 	}
62710Sstevel@tonic-gate out:
6272904Smcpowers 	if (please_destroy_object) {
6273904Smcpowers 		KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_DESTROY,
6274904Smcpowers 		    sp->sd_provider_session->ps_session, handle,
6275904Smcpowers 		    NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
6276904Smcpowers 
6277904Smcpowers 		(void) kcf_submit_request(real_provider, NULL,
6278904Smcpowers 		    NULL, &params, B_FALSE);
6279904Smcpowers 	}
6280904Smcpowers 
62816424Skrishna 	CRYPTO_SESSION_RELE(sp);
62820Sstevel@tonic-gate 	crypto_release_minor(cm);
6283904Smcpowers 
6284904Smcpowers 	if (real_provider != NULL) {
6285904Smcpowers 		crypto_free_mech(real_provider,
6286904Smcpowers 		    allocated_by_crypto_module, &mech);
6287904Smcpowers 		KCF_PROV_REFRELE(real_provider);
6288904Smcpowers 	}
62890Sstevel@tonic-gate 	return (error);
62900Sstevel@tonic-gate }
62910Sstevel@tonic-gate 
62920Sstevel@tonic-gate /* ARGSUSED */
62930Sstevel@tonic-gate static int
62944219Smcpowers nostore_derive_key(dev_t dev, caddr_t arg, int mode, int *rval)
62954219Smcpowers {
62964219Smcpowers 	STRUCT_DECL(crypto_nostore_derive_key, derive_key);
62974219Smcpowers 	/* LINTED E_FUNC_SET_NOT_USED */
62984219Smcpowers 	STRUCT_DECL(crypto_object_attribute, oa);
62994219Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
63004219Smcpowers 	kcf_req_params_t params;
63014219Smcpowers 	crypto_object_attribute_t *k_in_attrs = NULL;
63024219Smcpowers 	crypto_object_attribute_t *k_out_attrs = NULL;
63034219Smcpowers 	crypto_mechanism_t mech;
63044219Smcpowers 	crypto_key_t base_key;
63054219Smcpowers 	crypto_session_id_t session_id;
63064219Smcpowers 	crypto_minor_t *cm;
63074219Smcpowers 	crypto_session_data_t *sp = NULL;
63084219Smcpowers 	size_t k_in_attrs_size, k_out_attrs_size;
63094219Smcpowers 	size_t key_rctl_bytes = 0, mech_rctl_bytes = 0;
63106424Skrishna 	boolean_t mech_rctl_chk = B_FALSE;
63116424Skrishna 	boolean_t key_rctl_chk = B_FALSE;
63124219Smcpowers 	size_t in_attributes_rctl_bytes = 0;
63134219Smcpowers 	size_t out_attributes_rctl_bytes = 0;
63146424Skrishna 	boolean_t in_attributes_rctl_chk = B_FALSE;
63156424Skrishna 	boolean_t out_attributes_rctl_chk = B_FALSE;
63164219Smcpowers 	caddr_t in_attributes, out_attributes;
63174219Smcpowers 	uint_t in_count, out_count;
63184219Smcpowers 	int error = 0;
63194219Smcpowers 	int rv;
63204219Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
63214219Smcpowers 	caddr_t u_attrs = NULL;
63224219Smcpowers 
63234219Smcpowers 	STRUCT_INIT(derive_key, mode);
63244219Smcpowers 	STRUCT_INIT(oa, mode);
63254219Smcpowers 
63264219Smcpowers 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
63274219Smcpowers 		cmn_err(CE_WARN, "nostore_derive_key: failed holding minor");
63284219Smcpowers 		return (ENXIO);
63294219Smcpowers 	}
63304219Smcpowers 
63314219Smcpowers 	if (copyin(arg, STRUCT_BUF(derive_key), STRUCT_SIZE(derive_key)) != 0) {
63324219Smcpowers 		crypto_release_minor(cm);
63334219Smcpowers 		return (EFAULT);
63344219Smcpowers 	}
63354219Smcpowers 
63364219Smcpowers 	bzero(&base_key, sizeof (base_key));
63374219Smcpowers 
63384219Smcpowers 	session_id = STRUCT_FGET(derive_key, ndk_session);
63394219Smcpowers 
63404219Smcpowers 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
63414219Smcpowers 		goto release_minor;
63424219Smcpowers 	}
63434219Smcpowers 
63444219Smcpowers 	bcopy(STRUCT_FADDR(derive_key, ndk_mechanism), &mech.cm_type,
63454219Smcpowers 	    sizeof (crypto_mech_type_t));
63464219Smcpowers 
63474219Smcpowers 	if ((rv = kcf_get_hardware_provider(mech.cm_type, CRYPTO_MECH_INVALID,
63484219Smcpowers 	    CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider,
63494219Smcpowers 	    CRYPTO_FG_DERIVE)) != CRYPTO_SUCCESS) {
63504219Smcpowers 		goto release_minor;
63514219Smcpowers 	}
63524219Smcpowers 
63534219Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
63544219Smcpowers 	    STRUCT_FADDR(derive_key, ndk_mechanism), &mech, mode, &error);
63554219Smcpowers 
63564219Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
63574219Smcpowers 		allocated_by_crypto_module = B_TRUE;
63586424Skrishna 		if (!copyin_mech(mode, sp,
63596424Skrishna 		    STRUCT_FADDR(derive_key, ndk_mechanism),
63606424Skrishna 		    &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) {
63614219Smcpowers 			goto release_minor;
63624219Smcpowers 		}
63634219Smcpowers 	} else {
63644219Smcpowers 		if (rv != CRYPTO_SUCCESS)
63654219Smcpowers 			goto release_minor;
63664219Smcpowers 	}
63674219Smcpowers 
63686424Skrishna 	if (!copyin_key(mode, sp, STRUCT_FADDR(derive_key, ndk_base_key),
63696424Skrishna 	    &base_key, &key_rctl_bytes, &key_rctl_chk, &rv, &error)) {
63704219Smcpowers 		goto release_minor;
63714219Smcpowers 	}
63724219Smcpowers 
63734219Smcpowers 	in_count = STRUCT_FGET(derive_key, ndk_in_count);
63744219Smcpowers 	out_count = STRUCT_FGET(derive_key, ndk_out_count);
63754219Smcpowers 
63764219Smcpowers 	in_attributes = STRUCT_FGETP(derive_key, ndk_in_attributes);
63776424Skrishna 	if (!copyin_attributes(mode, sp, in_count, in_attributes, &k_in_attrs,
63784219Smcpowers 	    &k_in_attrs_size, NULL, &rv, &error, &in_attributes_rctl_bytes,
63796424Skrishna 	    &in_attributes_rctl_chk, B_TRUE)) {
63804219Smcpowers 		goto release_minor;
63814219Smcpowers 	}
63824219Smcpowers 
63834219Smcpowers 	out_attributes = STRUCT_FGETP(derive_key, ndk_out_attributes);
63846424Skrishna 	if (!copyin_attributes(mode, sp, out_count, out_attributes,
63856424Skrishna 	    &k_out_attrs, &k_out_attrs_size, &u_attrs, &rv, &error,
63866424Skrishna 	    &out_attributes_rctl_bytes,
63876424Skrishna 	    &out_attributes_rctl_chk, B_FALSE)) {
63884219Smcpowers 		goto release_minor;
63894219Smcpowers 	}
63904219Smcpowers 
63914219Smcpowers 	KCF_WRAP_NOSTORE_KEY_OPS_PARAMS(&params, KCF_OP_KEY_DERIVE,
63924219Smcpowers 	    sp->sd_provider_session->ps_session, &mech, k_in_attrs, in_count,
63934219Smcpowers 	    NULL, 0, &base_key, k_out_attrs, out_count, NULL, 0);
63944219Smcpowers 
63954219Smcpowers 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
63964219Smcpowers 
63974219Smcpowers 	if (rv == CRYPTO_SUCCESS) {
63984219Smcpowers 		rv = crypto_provider_copyout_mech_param(real_provider,
63994219Smcpowers 		    &mech, STRUCT_FADDR(derive_key, ndk_mechanism),
64004219Smcpowers 		    mode, &error);
64014219Smcpowers 
64024219Smcpowers 		if (rv == CRYPTO_NOT_SUPPORTED) {
64034219Smcpowers 			rv = CRYPTO_SUCCESS;
64044219Smcpowers 		}
64054219Smcpowers 		/* copyout the derived secret */
64064219Smcpowers 		if (copyout_attributes(mode, out_attributes, out_count,
64074219Smcpowers 		    k_out_attrs, u_attrs) != 0)
64084219Smcpowers 			error = EFAULT;
64094219Smcpowers 	}
64104219Smcpowers 
64114219Smcpowers release_minor:
64126424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
64136424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk);
64146424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, in_attributes_rctl_bytes,
64156424Skrishna 	    in_attributes_rctl_chk);
64166424Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, out_attributes_rctl_bytes,
64176424Skrishna 	    out_attributes_rctl_chk);
64184219Smcpowers 
64194219Smcpowers 	if (k_in_attrs != NULL)
64204219Smcpowers 		kmem_free(k_in_attrs, k_in_attrs_size);
64214219Smcpowers 	if (k_out_attrs != NULL) {
64224219Smcpowers 		bzero(k_out_attrs, k_out_attrs_size);
64234219Smcpowers 		kmem_free(k_out_attrs, k_out_attrs_size);
64244219Smcpowers 	}
64254219Smcpowers 
64264219Smcpowers 	if (u_attrs != NULL)
64274219Smcpowers 		kmem_free(u_attrs, out_count * STRUCT_SIZE(oa));
64284219Smcpowers 
64294219Smcpowers 	free_crypto_key(&base_key);
64304219Smcpowers 
64314219Smcpowers 	if (error != 0)
64324219Smcpowers 		goto out;
64334219Smcpowers 
64344219Smcpowers 	STRUCT_FSET(derive_key, ndk_return_value, rv);
64354219Smcpowers 	if (copyout(STRUCT_BUF(derive_key), arg,
64364219Smcpowers 	    STRUCT_SIZE(derive_key)) != 0) {
64374219Smcpowers 		error = EFAULT;
64384219Smcpowers 	}
64394219Smcpowers out:
64406424Skrishna 	CRYPTO_SESSION_RELE(sp);
64414219Smcpowers 	crypto_release_minor(cm);
64424219Smcpowers 
64434219Smcpowers 	if (real_provider != NULL) {
64444219Smcpowers 		crypto_free_mech(real_provider,
64454219Smcpowers 		    allocated_by_crypto_module, &mech);
64464219Smcpowers 		KCF_PROV_REFRELE(real_provider);
64474219Smcpowers 	}
64484219Smcpowers 	return (error);
64494219Smcpowers }
64504219Smcpowers 
64514219Smcpowers /* ARGSUSED */
64524219Smcpowers static int
64530Sstevel@tonic-gate crypto_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *c,
64540Sstevel@tonic-gate     int *rval)
64550Sstevel@tonic-gate {
64560Sstevel@tonic-gate #define	ARG	((caddr_t)arg)
64570Sstevel@tonic-gate 
64580Sstevel@tonic-gate 	switch (cmd) {
64590Sstevel@tonic-gate 	case CRYPTO_GET_FUNCTION_LIST:
64600Sstevel@tonic-gate 		return (get_function_list(dev, ARG, mode, rval));
64610Sstevel@tonic-gate 
64620Sstevel@tonic-gate 	case CRYPTO_GET_MECHANISM_NUMBER:
64630Sstevel@tonic-gate 		return (get_mechanism_number(dev, ARG, mode, rval));
64640Sstevel@tonic-gate 
64658313SDina.Nimeh@Sun.Com 	case CRYPTO_GET_MECHANISM_LIST:
64668313SDina.Nimeh@Sun.Com 		return (get_mechanism_list(dev, ARG, mode, rval));
64678313SDina.Nimeh@Sun.Com 
64688313SDina.Nimeh@Sun.Com 	case CRYPTO_GET_ALL_MECHANISM_INFO:
64698313SDina.Nimeh@Sun.Com 		return (get_all_mechanism_info(dev, ARG, mode, rval));
64708313SDina.Nimeh@Sun.Com 
64710Sstevel@tonic-gate 	case CRYPTO_GET_PROVIDER_LIST:
64720Sstevel@tonic-gate 		return (get_provider_list(dev, ARG, mode, rval));
64730Sstevel@tonic-gate 
64740Sstevel@tonic-gate 	case CRYPTO_GET_PROVIDER_INFO:
64750Sstevel@tonic-gate 		return (get_provider_info(dev, ARG, mode, rval));
64760Sstevel@tonic-gate 
64770Sstevel@tonic-gate 	case CRYPTO_GET_PROVIDER_MECHANISMS:
64780Sstevel@tonic-gate 		return (get_provider_mechanisms(dev, ARG, mode, rval));
64790Sstevel@tonic-gate 
64800Sstevel@tonic-gate 	case CRYPTO_GET_PROVIDER_MECHANISM_INFO:
64810Sstevel@tonic-gate 		return (get_provider_mechanism_info(dev, ARG, mode, rval));
64820Sstevel@tonic-gate 
64830Sstevel@tonic-gate 	case CRYPTO_OPEN_SESSION:
64840Sstevel@tonic-gate 		return (open_session(dev, ARG, mode, rval));
64850Sstevel@tonic-gate 
64860Sstevel@tonic-gate 	case CRYPTO_CLOSE_SESSION:
64870Sstevel@tonic-gate 		return (close_session(dev, ARG, mode, rval));
64880Sstevel@tonic-gate 
64890Sstevel@tonic-gate 	case CRYPTO_ENCRYPT_INIT:
64900Sstevel@tonic-gate 		return (encrypt_init(dev, ARG, mode, rval));
64910Sstevel@tonic-gate 
64920Sstevel@tonic-gate 	case CRYPTO_DECRYPT_INIT:
64930Sstevel@tonic-gate 		return (decrypt_init(dev, ARG, mode, rval));
64940Sstevel@tonic-gate 
64950Sstevel@tonic-gate 	case CRYPTO_ENCRYPT:
64960Sstevel@tonic-gate 		return (encrypt(dev, ARG, mode, rval));
64970Sstevel@tonic-gate 
64980Sstevel@tonic-gate 	case CRYPTO_DECRYPT:
64990Sstevel@tonic-gate 		return (decrypt(dev, ARG, mode, rval));
65000Sstevel@tonic-gate 
65010Sstevel@tonic-gate 	case CRYPTO_ENCRYPT_UPDATE:
65020Sstevel@tonic-gate 		return (encrypt_update(dev, ARG, mode, rval));
65030Sstevel@tonic-gate 
65040Sstevel@tonic-gate 	case CRYPTO_DECRYPT_UPDATE:
65050Sstevel@tonic-gate 		return (decrypt_update(dev, ARG, mode, rval));
65060Sstevel@tonic-gate 
65070Sstevel@tonic-gate 	case CRYPTO_ENCRYPT_FINAL:
65080Sstevel@tonic-gate 		return (encrypt_final(dev, ARG, mode, rval));
65090Sstevel@tonic-gate 
65100Sstevel@tonic-gate 	case CRYPTO_DECRYPT_FINAL:
65110Sstevel@tonic-gate 		return (decrypt_final(dev, ARG, mode, rval));
65120Sstevel@tonic-gate 
65130Sstevel@tonic-gate 	case CRYPTO_DIGEST_INIT:
65140Sstevel@tonic-gate 		return (digest_init(dev, ARG, mode, rval));
65150Sstevel@tonic-gate 
65160Sstevel@tonic-gate 	case CRYPTO_DIGEST:
65170Sstevel@tonic-gate 		return (digest(dev, ARG, mode, rval));
65180Sstevel@tonic-gate 
65190Sstevel@tonic-gate 	case CRYPTO_DIGEST_UPDATE:
65200Sstevel@tonic-gate 		return (digest_update(dev, ARG, mode, rval));
65210Sstevel@tonic-gate 
65220Sstevel@tonic-gate 	case CRYPTO_DIGEST_KEY:
65230Sstevel@tonic-gate 		return (digest_key(dev, ARG, mode, rval));
65240Sstevel@tonic-gate 
65250Sstevel@tonic-gate 	case CRYPTO_DIGEST_FINAL:
65260Sstevel@tonic-gate 		return (digest_final(dev, ARG, mode, rval));
65270Sstevel@tonic-gate 
65280Sstevel@tonic-gate 	case CRYPTO_SIGN_INIT:
65290Sstevel@tonic-gate 		return (sign_init(dev, ARG, mode, rval));
65300Sstevel@tonic-gate 
65310Sstevel@tonic-gate 	case CRYPTO_SIGN:
65320Sstevel@tonic-gate 		return (sign(dev, ARG, mode, rval));
65330Sstevel@tonic-gate 
65340Sstevel@tonic-gate 	case CRYPTO_SIGN_UPDATE:
65350Sstevel@tonic-gate 		return (sign_update(dev, ARG, mode, rval));
65360Sstevel@tonic-gate 
65370Sstevel@tonic-gate 	case CRYPTO_SIGN_FINAL:
65380Sstevel@tonic-gate 		return (sign_final(dev, ARG, mode, rval));
65390Sstevel@tonic-gate 
65400Sstevel@tonic-gate 	case CRYPTO_SIGN_RECOVER_INIT:
65410Sstevel@tonic-gate 		return (sign_recover_init(dev, ARG, mode, rval));
65420Sstevel@tonic-gate 
65430Sstevel@tonic-gate 	case CRYPTO_SIGN_RECOVER:
65440Sstevel@tonic-gate 		return (sign_recover(dev, ARG, mode, rval));
65450Sstevel@tonic-gate 
65460Sstevel@tonic-gate 	case CRYPTO_VERIFY_INIT:
65470Sstevel@tonic-gate 		return (verify_init(dev, ARG, mode, rval));
65480Sstevel@tonic-gate 
65490Sstevel@tonic-gate 	case CRYPTO_VERIFY:
65500Sstevel@tonic-gate 		return (verify(dev, ARG, mode, rval));
65510Sstevel@tonic-gate 
65520Sstevel@tonic-gate 	case CRYPTO_VERIFY_UPDATE:
65530Sstevel@tonic-gate 		return (verify_update(dev, ARG, mode, rval));
65540Sstevel@tonic-gate 
65550Sstevel@tonic-gate 	case CRYPTO_VERIFY_FINAL:
65560Sstevel@tonic-gate 		return (verify_final(dev, ARG, mode, rval));
65570Sstevel@tonic-gate 
65580Sstevel@tonic-gate 	case CRYPTO_VERIFY_RECOVER_INIT:
65590Sstevel@tonic-gate 		return (verify_recover_init(dev, ARG, mode, rval));
65600Sstevel@tonic-gate 
65610Sstevel@tonic-gate 	case CRYPTO_VERIFY_RECOVER:
65620Sstevel@tonic-gate 		return (verify_recover(dev, ARG, mode, rval));
65630Sstevel@tonic-gate 
65640Sstevel@tonic-gate 	case CRYPTO_SET_PIN:
65650Sstevel@tonic-gate 		return (set_pin(dev, ARG, mode, rval));
65660Sstevel@tonic-gate 
65670Sstevel@tonic-gate 	case CRYPTO_LOGIN:
65680Sstevel@tonic-gate 		return (login(dev, ARG, mode, rval));
65690Sstevel@tonic-gate 
65700Sstevel@tonic-gate 	case CRYPTO_LOGOUT:
65710Sstevel@tonic-gate 		return (logout(dev, ARG, mode, rval));
65720Sstevel@tonic-gate 
65730Sstevel@tonic-gate 	case CRYPTO_SEED_RANDOM:
65740Sstevel@tonic-gate 		return (seed_random(dev, ARG, mode, rval));
65750Sstevel@tonic-gate 
65760Sstevel@tonic-gate 	case CRYPTO_GENERATE_RANDOM:
65770Sstevel@tonic-gate 		return (generate_random(dev, ARG, mode, rval));
65780Sstevel@tonic-gate 
65790Sstevel@tonic-gate 	case CRYPTO_OBJECT_CREATE:
65800Sstevel@tonic-gate 		return (object_create(dev, ARG, mode, rval));
65810Sstevel@tonic-gate 
65820Sstevel@tonic-gate 	case CRYPTO_OBJECT_COPY:
65830Sstevel@tonic-gate 		return (object_copy(dev, ARG, mode, rval));
65840Sstevel@tonic-gate 
65850Sstevel@tonic-gate 	case CRYPTO_OBJECT_DESTROY:
65860Sstevel@tonic-gate 		return (object_destroy(dev, ARG, mode, rval));
65870Sstevel@tonic-gate 
65880Sstevel@tonic-gate 	case CRYPTO_OBJECT_GET_ATTRIBUTE_VALUE:
65890Sstevel@tonic-gate 		return (object_get_attribute_value(dev, ARG, mode, rval));
65900Sstevel@tonic-gate 
65910Sstevel@tonic-gate 	case CRYPTO_OBJECT_GET_SIZE:
65920Sstevel@tonic-gate 		return (object_get_size(dev, ARG, mode, rval));
65930Sstevel@tonic-gate 
65940Sstevel@tonic-gate 	case CRYPTO_OBJECT_SET_ATTRIBUTE_VALUE:
65950Sstevel@tonic-gate 		return (object_set_attribute_value(dev, ARG, mode, rval));
65960Sstevel@tonic-gate 
65970Sstevel@tonic-gate 	case CRYPTO_OBJECT_FIND_INIT:
65980Sstevel@tonic-gate 		return (object_find_init(dev, ARG, mode, rval));
65990Sstevel@tonic-gate 
66000Sstevel@tonic-gate 	case CRYPTO_OBJECT_FIND_UPDATE:
66010Sstevel@tonic-gate 		return (object_find_update(dev, ARG, mode, rval));
66020Sstevel@tonic-gate 
66030Sstevel@tonic-gate 	case CRYPTO_OBJECT_FIND_FINAL:
66040Sstevel@tonic-gate 		return (object_find_final(dev, ARG, mode, rval));
66050Sstevel@tonic-gate 
66060Sstevel@tonic-gate 	case CRYPTO_GENERATE_KEY:
66070Sstevel@tonic-gate 		return (object_generate_key(dev, ARG, mode, rval));
66080Sstevel@tonic-gate 
66090Sstevel@tonic-gate 	case CRYPTO_GENERATE_KEY_PAIR:
66100Sstevel@tonic-gate 		return (object_generate_key_pair(dev, ARG, mode, rval));
66110Sstevel@tonic-gate 
66120Sstevel@tonic-gate 	case CRYPTO_WRAP_KEY:
66130Sstevel@tonic-gate 		return (object_wrap_key(dev, ARG, mode, rval));
66140Sstevel@tonic-gate 
66150Sstevel@tonic-gate 	case CRYPTO_UNWRAP_KEY:
66160Sstevel@tonic-gate 		return (object_unwrap_key(dev, ARG, mode, rval));
66170Sstevel@tonic-gate 
66180Sstevel@tonic-gate 	case CRYPTO_DERIVE_KEY:
66190Sstevel@tonic-gate 		return (object_derive_key(dev, ARG, mode, rval));
66204219Smcpowers 
66214219Smcpowers 	case CRYPTO_NOSTORE_GENERATE_KEY:
66224219Smcpowers 		return (nostore_generate_key(dev, ARG, mode, rval));
66234219Smcpowers 
66244219Smcpowers 	case CRYPTO_NOSTORE_GENERATE_KEY_PAIR:
66254219Smcpowers 		return (nostore_generate_key_pair(dev, ARG, mode, rval));
66264219Smcpowers 
66274219Smcpowers 	case CRYPTO_NOSTORE_DERIVE_KEY:
66284219Smcpowers 		return (nostore_derive_key(dev, ARG, mode, rval));
66290Sstevel@tonic-gate 	}
66300Sstevel@tonic-gate 	return (EINVAL);
66310Sstevel@tonic-gate }
66320Sstevel@tonic-gate 
66330Sstevel@tonic-gate /*
66340Sstevel@tonic-gate  * Check for the project.max-crypto-memory resource control.
66350Sstevel@tonic-gate  */
66360Sstevel@tonic-gate static int
66373916Skrishna crypto_buffer_check(size_t need)
66380Sstevel@tonic-gate {
66393620Skrishna 	kproject_t *kpj;
66400Sstevel@tonic-gate 
66410Sstevel@tonic-gate 	if (need == 0)
66420Sstevel@tonic-gate 		return (CRYPTO_SUCCESS);
66430Sstevel@tonic-gate 
66440Sstevel@tonic-gate 	mutex_enter(&curproc->p_lock);
66453620Skrishna 	kpj = curproc->p_task->tk_proj;
66463916Skrishna 	mutex_enter(&(kpj->kpj_data.kpd_crypto_lock));
66473620Skrishna 
66483620Skrishna 	if (kpj->kpj_data.kpd_crypto_mem + need >
66493620Skrishna 	    kpj->kpj_data.kpd_crypto_mem_ctl) {
66503620Skrishna 		if (rctl_test(rc_project_crypto_mem,
66513620Skrishna 		    kpj->kpj_rctls, curproc, need, 0) & RCT_DENY) {
66523916Skrishna 			mutex_exit(&(kpj->kpj_data.kpd_crypto_lock));
66533620Skrishna 			mutex_exit(&curproc->p_lock);
66543620Skrishna 			return (CRYPTO_HOST_MEMORY);
66553620Skrishna 		}
66563620Skrishna 	}
66573620Skrishna 
66583620Skrishna 	kpj->kpj_data.kpd_crypto_mem += need;
66593916Skrishna 	mutex_exit(&(kpj->kpj_data.kpd_crypto_lock));
66603916Skrishna 
66613916Skrishna 	curproc->p_crypto_mem += need;
66620Sstevel@tonic-gate 	mutex_exit(&curproc->p_lock);
66633620Skrishna 
66640Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
66650Sstevel@tonic-gate }
6666