xref: /illumos-gate/usr/src/uts/common/crypto/io/crypto.c (revision c846684c)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
572eff6e2Smcpowers  * Common Development and Distribution License (the "License").
672eff6e2Smcpowers  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22c630c2c5SVladimir Kotal  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
23cd964fceSMatt Barden  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
24fb261280SJason King  * Copyright 2018, Joyent, Inc.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * The ioctl interface for cryptographic commands.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
347c478bd9Sstevel@tonic-gate #include <sys/conf.h>
357c478bd9Sstevel@tonic-gate #include <sys/stat.h>
367c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
377c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
387c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
397c478bd9Sstevel@tonic-gate #include <sys/errno.h>
407c478bd9Sstevel@tonic-gate #include <sys/ksynch.h>
417c478bd9Sstevel@tonic-gate #include <sys/file.h>
427c478bd9Sstevel@tonic-gate #include <sys/open.h>
437c478bd9Sstevel@tonic-gate #include <sys/cred.h>
447c478bd9Sstevel@tonic-gate #include <sys/proc.h>
457c478bd9Sstevel@tonic-gate #include <sys/task.h>
467c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
477c478bd9Sstevel@tonic-gate #include <sys/model.h>
487c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
497c478bd9Sstevel@tonic-gate #include <sys/crypto/common.h>
507c478bd9Sstevel@tonic-gate #include <sys/crypto/api.h>
517c478bd9Sstevel@tonic-gate #include <sys/crypto/impl.h>
527c478bd9Sstevel@tonic-gate #include <sys/crypto/sched_impl.h>
537c478bd9Sstevel@tonic-gate #include <sys/crypto/ioctl.h>
547c478bd9Sstevel@tonic-gate 
554a5b2e70Shaimay extern int kcf_des3_threshold;
564a5b2e70Shaimay extern int kcf_aes_threshold;
574a5b2e70Shaimay extern int kcf_rc4_threshold;
584a5b2e70Shaimay extern int kcf_md5_threshold;
594a5b2e70Shaimay extern int kcf_sha1_threshold;
604a5b2e70Shaimay 
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate  * Locking notes:
637c478bd9Sstevel@tonic-gate  *
64ef56a3c5SKrishna Yenduri  * crypto_locks protects the global array of minor structures.
65ef56a3c5SKrishna Yenduri  * crypto_locks is an array of locks indexed by the cpuid. A reader needs
66ef56a3c5SKrishna Yenduri  * to hold a single lock while a writer needs to hold all locks.
67ef56a3c5SKrishna Yenduri  * krwlock_t is not an option here because the hold time
68ef56a3c5SKrishna Yenduri  * is very small for these locks.
697c478bd9Sstevel@tonic-gate  *
70ef56a3c5SKrishna Yenduri  * The fields in the minor structure are protected by the cm_lock member
71ef56a3c5SKrishna Yenduri  * of the minor structure. The cm_cv is used to signal decrements
72ef56a3c5SKrishna Yenduri  * in the cm_refcnt, and is used with the cm_lock.
73ef56a3c5SKrishna Yenduri  *
74ef56a3c5SKrishna Yenduri  * The locking order is crypto_locks followed by cm_lock.
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate  * DDI entry points.
797c478bd9Sstevel@tonic-gate  */
807c478bd9Sstevel@tonic-gate static int crypto_attach(dev_info_t *, ddi_attach_cmd_t);
817c478bd9Sstevel@tonic-gate static int crypto_detach(dev_info_t *, ddi_detach_cmd_t);
827c478bd9Sstevel@tonic-gate static int crypto_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
837c478bd9Sstevel@tonic-gate static int crypto_open(dev_t *, int, int, cred_t *);
847c478bd9Sstevel@tonic-gate static int crypto_close(dev_t, int, int, cred_t *);
857c478bd9Sstevel@tonic-gate static int crypto_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
867c478bd9Sstevel@tonic-gate 
87894b2776Smcpowers static int cipher_init(dev_t, caddr_t, int, int (*)(crypto_provider_t,
887c478bd9Sstevel@tonic-gate     crypto_session_id_t, crypto_mechanism_t *, crypto_key_t *,
897c478bd9Sstevel@tonic-gate     crypto_ctx_template_t, crypto_context_t *, crypto_call_req_t *));
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate static int common_digest(dev_t, caddr_t, int, int (*)(crypto_context_t,
927c478bd9Sstevel@tonic-gate     crypto_data_t *, crypto_data_t *, crypto_call_req_t *));
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate static int cipher(dev_t, caddr_t, int, int (*)(crypto_context_t,
957c478bd9Sstevel@tonic-gate     crypto_data_t *, crypto_data_t *, crypto_call_req_t *));
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate static int cipher_update(dev_t, caddr_t, int, int (*)(crypto_context_t,
987c478bd9Sstevel@tonic-gate     crypto_data_t *, crypto_data_t *, crypto_call_req_t *));
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate static int common_final(dev_t, caddr_t, int, int (*)(crypto_context_t,
1017c478bd9Sstevel@tonic-gate     crypto_data_t *, crypto_call_req_t *));
1027c478bd9Sstevel@tonic-gate 
103894b2776Smcpowers static int sign_verify_init(dev_t, caddr_t, int, int (*)(crypto_provider_t,
1047c478bd9Sstevel@tonic-gate     crypto_session_id_t, crypto_mechanism_t *, crypto_key_t *,
1057c478bd9Sstevel@tonic-gate     crypto_ctx_template_t, crypto_context_t *, crypto_call_req_t *));
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate static int sign_verify_update(dev_t dev, caddr_t arg, int mode,
1087c478bd9Sstevel@tonic-gate     int (*)(crypto_context_t, crypto_data_t *, crypto_call_req_t *));
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate static void crypto_release_provider_session(crypto_minor_t *,
1117c478bd9Sstevel@tonic-gate     crypto_provider_session_t *);
112c1a9a9c3Skrishna static int crypto_buffer_check(size_t);
1137c478bd9Sstevel@tonic-gate static int crypto_free_find_ctx(crypto_session_data_t *);
1147c478bd9Sstevel@tonic-gate static int crypto_get_provider_list(crypto_minor_t *, uint_t *,
1157c478bd9Sstevel@tonic-gate     crypto_provider_entry_t **, boolean_t);
1167c478bd9Sstevel@tonic-gate 
117cd964fceSMatt Barden static int crypto_create_provider_session(crypto_minor_t *,
118cd964fceSMatt Barden     kcf_provider_desc_t *, crypto_session_id_t, crypto_provider_session_t **,
119cd964fceSMatt Barden     kcf_provider_desc_t *);
120cd964fceSMatt Barden static int crypto_create_session_ptr(crypto_minor_t *, kcf_provider_desc_t *,
121cd964fceSMatt Barden     crypto_provider_session_t *, crypto_session_id_t *);
122cd964fceSMatt Barden 
1237c478bd9Sstevel@tonic-gate /* number of minor numbers to allocate at a time */
1247c478bd9Sstevel@tonic-gate #define	CRYPTO_MINOR_CHUNK	16
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate /*
1277c478bd9Sstevel@tonic-gate  * There are two limits associated with kernel memory. The first,
1287c478bd9Sstevel@tonic-gate  * CRYPTO_MAX_BUFFER_LEN, is the maximum number of bytes that can be
1297c478bd9Sstevel@tonic-gate  * allocated for a single copyin/copyout buffer. The second limit is
1307c478bd9Sstevel@tonic-gate  * the total number of bytes that can be allocated by a process
1317c478bd9Sstevel@tonic-gate  * for copyin/copyout buffers. The latter is enforced by the
1327c478bd9Sstevel@tonic-gate  * project.max-crypto-memory resource control.
1337c478bd9Sstevel@tonic-gate  */
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate #define	CRYPTO_MAX_BUFFER_LEN	(2 * 1024 * 1024)
1367c478bd9Sstevel@tonic-gate #define	CRYPTO_MAX_FIND_COUNT	512
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate /*
1392d794da1Skrishna  * We preapprove some bytes for each session to avoid making the costly
1402d794da1Skrishna  * crypto_buffer_check() calls. The preapproval is done when a new session
1412d794da1Skrishna  * is created and that cost is amortized over later crypto calls.
1422d794da1Skrishna  * Most applications create a session and then do a bunch of crypto calls
1432d794da1Skrishna  * in that session. So, they benefit from this optimization.
1442d794da1Skrishna  *
1452d794da1Skrishna  * Note that we may hit the project.max-crypto-memory limit a bit sooner
1462d794da1Skrishna  * because of this preapproval. But it is acceptable since the preapproved
1472d794da1Skrishna  * amount is insignificant compared to the default max-crypto-memory limit
1482d794da1Skrishna  * which is quarter of the machine's memory. The preapproved amount is
1492d794da1Skrishna  * roughly 2 * 16K(maximum SSL record size).
1507c478bd9Sstevel@tonic-gate  */
1512d794da1Skrishna #define	CRYPTO_PRE_APPROVED_LIMIT	(32 * 1024)
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate /* The session table grows by CRYPTO_SESSION_CHUNK increments */
1547c478bd9Sstevel@tonic-gate #define	CRYPTO_SESSION_CHUNK	100
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate size_t crypto_max_buffer_len = CRYPTO_MAX_BUFFER_LEN;
1572d794da1Skrishna size_t crypto_pre_approved_limit = CRYPTO_PRE_APPROVED_LIMIT;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate #define	INIT_RAW_CRYPTO_DATA(data, len)				\
1607c478bd9Sstevel@tonic-gate 	(data).cd_format = CRYPTO_DATA_RAW;			\
161fb261280SJason King 	(data).cd_raw.iov_base = (len > 0) ? kmem_alloc(len, KM_SLEEP) : NULL; \
1627c478bd9Sstevel@tonic-gate 	(data).cd_raw.iov_len = len;				\
1637c478bd9Sstevel@tonic-gate 	(data).cd_offset = 0;					\
1647c478bd9Sstevel@tonic-gate 	(data).cd_length = len;
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate static struct kmem_cache *crypto_session_cache;
1677c478bd9Sstevel@tonic-gate static crypto_minor_t **crypto_minors = NULL;
1687c478bd9Sstevel@tonic-gate static dev_info_t *crypto_dip = NULL;
1697c478bd9Sstevel@tonic-gate static minor_t crypto_minor_chunk = CRYPTO_MINOR_CHUNK;
1707c478bd9Sstevel@tonic-gate static minor_t crypto_minors_table_count = 0;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate /*
1737c478bd9Sstevel@tonic-gate  * Minors are started from 1 because vmem_alloc()
1747c478bd9Sstevel@tonic-gate  * returns 0 in case of failure.
1757c478bd9Sstevel@tonic-gate  */
1767c478bd9Sstevel@tonic-gate static vmem_t *crypto_arena = NULL;	/* Arena for device minors */
1777c478bd9Sstevel@tonic-gate static minor_t crypto_minors_count = 0;
178ef56a3c5SKrishna Yenduri static kcf_lock_withpad_t *crypto_locks;
179ef56a3c5SKrishna Yenduri 
180ef56a3c5SKrishna Yenduri #define	CRYPTO_ENTER_ALL_LOCKS()		\
181ef56a3c5SKrishna Yenduri 	for (i = 0; i < max_ncpus; i++)		\
182ef56a3c5SKrishna Yenduri 		mutex_enter(&crypto_locks[i].kl_lock);
183ef56a3c5SKrishna Yenduri 
184ef56a3c5SKrishna Yenduri #define	CRYPTO_EXIT_ALL_LOCKS()			\
185ef56a3c5SKrishna Yenduri 	for (i = 0; i < max_ncpus; i++)		\
186ef56a3c5SKrishna Yenduri 		mutex_exit(&crypto_locks[i].kl_lock);
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate #define	RETURN_LIST			B_TRUE
1897c478bd9Sstevel@tonic-gate #define	DONT_RETURN_LIST		B_FALSE
1907c478bd9Sstevel@tonic-gate 
191894b2776Smcpowers #define	CRYPTO_OPS_OFFSET(f)		offsetof(crypto_ops_t, co_##f)
1927c478bd9Sstevel@tonic-gate #define	CRYPTO_RANDOM_OFFSET(f)		offsetof(crypto_random_number_ops_t, f)
1937c478bd9Sstevel@tonic-gate #define	CRYPTO_SESSION_OFFSET(f)	offsetof(crypto_session_ops_t, f)
1947c478bd9Sstevel@tonic-gate #define	CRYPTO_OBJECT_OFFSET(f)		offsetof(crypto_object_ops_t, f)
1957c478bd9Sstevel@tonic-gate #define	CRYPTO_PROVIDER_OFFSET(f)	\
1967c478bd9Sstevel@tonic-gate 	offsetof(crypto_provider_management_ops_t, f)
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate #define	CRYPTO_CANCEL_CTX(spp) {	\
1997c478bd9Sstevel@tonic-gate 	crypto_cancel_ctx(*(spp));	\
2007c478bd9Sstevel@tonic-gate 	*(spp) = NULL;			\
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate #define	CRYPTO_CANCEL_ALL_CTX(sp) {				\
2047c478bd9Sstevel@tonic-gate 	if ((sp)->sd_digest_ctx != NULL) {			\
2057c478bd9Sstevel@tonic-gate 		crypto_cancel_ctx((sp)->sd_digest_ctx);		\
2067c478bd9Sstevel@tonic-gate 		(sp)->sd_digest_ctx = NULL;			\
2077c478bd9Sstevel@tonic-gate 	}							\
2087c478bd9Sstevel@tonic-gate 	if ((sp)->sd_encr_ctx != NULL) {			\
2097c478bd9Sstevel@tonic-gate 		crypto_cancel_ctx((sp)->sd_encr_ctx);		\
2107c478bd9Sstevel@tonic-gate 		(sp)->sd_encr_ctx = NULL;			\
2117c478bd9Sstevel@tonic-gate 	}							\
2127c478bd9Sstevel@tonic-gate 	if ((sp)->sd_decr_ctx != NULL) {			\
2137c478bd9Sstevel@tonic-gate 		crypto_cancel_ctx((sp)->sd_decr_ctx);		\
2147c478bd9Sstevel@tonic-gate 		(sp)->sd_decr_ctx = NULL;			\
2157c478bd9Sstevel@tonic-gate 	}							\
2167c478bd9Sstevel@tonic-gate 	if ((sp)->sd_sign_ctx != NULL) {			\
2177c478bd9Sstevel@tonic-gate 		crypto_cancel_ctx((sp)->sd_sign_ctx);		\
2187c478bd9Sstevel@tonic-gate 		(sp)->sd_sign_ctx = NULL;			\
2197c478bd9Sstevel@tonic-gate 	}							\
2207c478bd9Sstevel@tonic-gate 	if ((sp)->sd_verify_ctx != NULL) {			\
2217c478bd9Sstevel@tonic-gate 		crypto_cancel_ctx((sp)->sd_verify_ctx);		\
2227c478bd9Sstevel@tonic-gate 		(sp)->sd_verify_ctx = NULL;			\
2237c478bd9Sstevel@tonic-gate 	}							\
2247c478bd9Sstevel@tonic-gate 	if ((sp)->sd_sign_recover_ctx != NULL) {		\
2257c478bd9Sstevel@tonic-gate 		crypto_cancel_ctx((sp)->sd_sign_recover_ctx);	\
2267c478bd9Sstevel@tonic-gate 		(sp)->sd_sign_recover_ctx = NULL;		\
2277c478bd9Sstevel@tonic-gate 	}							\
2287c478bd9Sstevel@tonic-gate 	if ((sp)->sd_verify_recover_ctx != NULL) {		\
2297c478bd9Sstevel@tonic-gate 		crypto_cancel_ctx((sp)->sd_verify_recover_ctx);	\
2307c478bd9Sstevel@tonic-gate 		(sp)->sd_verify_recover_ctx = NULL;		\
2317c478bd9Sstevel@tonic-gate 	}							\
232cd964fceSMatt Barden 	if ((sp)->sd_mac_ctx != NULL) {		\
233cd964fceSMatt Barden 		crypto_cancel_ctx((sp)->sd_mac_ctx);	\
234cd964fceSMatt Barden 		(sp)->sd_mac_ctx = NULL;		\
235cd964fceSMatt Barden 	}							\
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate 
2382d794da1Skrishna #define	CRYPTO_DECREMENT_RCTL(val)	if ((val) != 0) {	\
239c1a9a9c3Skrishna 	kproject_t *projp;					\
240c1a9a9c3Skrishna 	mutex_enter(&curproc->p_lock);				\
241c1a9a9c3Skrishna 	projp = curproc->p_task->tk_proj;			\
242c1a9a9c3Skrishna 	ASSERT(projp != NULL);					\
243c1a9a9c3Skrishna 	mutex_enter(&(projp->kpj_data.kpd_crypto_lock));	\
244c1a9a9c3Skrishna 	projp->kpj_data.kpd_crypto_mem -= (val);		\
245c1a9a9c3Skrishna 	mutex_exit(&(projp->kpj_data.kpd_crypto_lock));		\
246c1a9a9c3Skrishna 	curproc->p_crypto_mem -= (val);				\
247c1a9a9c3Skrishna 	mutex_exit(&curproc->p_lock);				\
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate 
2502d794da1Skrishna /*
2512d794da1Skrishna  * We do not need to hold sd_lock in the macros below
2522d794da1Skrishna  * as they are called after doing a get_session_ptr() which
2532d794da1Skrishna  * sets the CRYPTO_SESSION_IS_BUSY flag.
2542d794da1Skrishna  */
255fb261280SJason King #define	CRYPTO_DECREMENT_RCTL_SESSION(sp, val, rctl_chk)	\
25666761628Skrishna 	if (((val) != 0) && ((sp) != NULL)) {			\
2572d794da1Skrishna 		ASSERT(((sp)->sd_flags & CRYPTO_SESSION_IS_BUSY) != 0);	\
2582d794da1Skrishna 		if (rctl_chk) {				\
2592d794da1Skrishna 			CRYPTO_DECREMENT_RCTL(val);		\
2602d794da1Skrishna 		} else {					\
2612d794da1Skrishna 			(sp)->sd_pre_approved_amount += (val);	\
2622d794da1Skrishna 		}						\
2632d794da1Skrishna 	}
2642d794da1Skrishna 
2652d794da1Skrishna #define	CRYPTO_BUFFER_CHECK(sp, need, rctl_chk)		\
2662d794da1Skrishna 	((sp->sd_pre_approved_amount >= need) ?			\
2672d794da1Skrishna 	(sp->sd_pre_approved_amount -= need,			\
2682d794da1Skrishna 	    rctl_chk = B_FALSE, CRYPTO_SUCCESS) :		\
2692d794da1Skrishna 	    (rctl_chk = B_TRUE, crypto_buffer_check(need)))
2702d794da1Skrishna 
2717c478bd9Sstevel@tonic-gate /*
2727c478bd9Sstevel@tonic-gate  * Module linkage.
2737c478bd9Sstevel@tonic-gate  */
2747c478bd9Sstevel@tonic-gate static struct cb_ops cbops = {
2757c478bd9Sstevel@tonic-gate 	crypto_open,		/* cb_open */
2767c478bd9Sstevel@tonic-gate 	crypto_close,		/* cb_close */
2777c478bd9Sstevel@tonic-gate 	nodev,			/* cb_strategy */
2787c478bd9Sstevel@tonic-gate 	nodev,			/* cb_print */
2797c478bd9Sstevel@tonic-gate 	nodev,			/* cb_dump */
2807c478bd9Sstevel@tonic-gate 	nodev,			/* cb_read */
2817c478bd9Sstevel@tonic-gate 	nodev,			/* cb_write */
2827c478bd9Sstevel@tonic-gate 	crypto_ioctl,		/* cb_ioctl */
2837c478bd9Sstevel@tonic-gate 	nodev,			/* cb_devmap */
2847c478bd9Sstevel@tonic-gate 	nodev,			/* cb_mmap */
2857c478bd9Sstevel@tonic-gate 	nodev,			/* cb_segmap */
2867c478bd9Sstevel@tonic-gate 	nochpoll,		/* cb_chpoll */
2877c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
2887c478bd9Sstevel@tonic-gate 	NULL,			/* cb_streamtab */
2897c478bd9Sstevel@tonic-gate 	D_MP,			/* cb_flag */
2907c478bd9Sstevel@tonic-gate 	CB_REV,			/* cb_rev */
2917c478bd9Sstevel@tonic-gate 	nodev,			/* cb_aread */
2927c478bd9Sstevel@tonic-gate 	nodev,			/* cb_awrite */
2937c478bd9Sstevel@tonic-gate };
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate static struct dev_ops devops = {
2967c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
2977c478bd9Sstevel@tonic-gate 	0,			/* devo_refcnt */
2987c478bd9Sstevel@tonic-gate 	crypto_getinfo,		/* devo_getinfo */
2997c478bd9Sstevel@tonic-gate 	nulldev,		/* devo_identify */
3007c478bd9Sstevel@tonic-gate 	nulldev,		/* devo_probe */
3017c478bd9Sstevel@tonic-gate 	crypto_attach,		/* devo_attach */
3027c478bd9Sstevel@tonic-gate 	crypto_detach,		/* devo_detach */
3037c478bd9Sstevel@tonic-gate 	nodev,			/* devo_reset */
3047c478bd9Sstevel@tonic-gate 	&cbops,			/* devo_cb_ops */
3057c478bd9Sstevel@tonic-gate 	NULL,			/* devo_bus_ops */
3067c478bd9Sstevel@tonic-gate 	NULL,			/* devo_power */
30719397407SSherry Moore 	ddi_quiesce_not_needed,		/* devo_quiesce */
3087c478bd9Sstevel@tonic-gate };
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
3117c478bd9Sstevel@tonic-gate 	&mod_driverops,					/* drv_modops */
312d2b32306Smcpowers 	"Cryptographic Library Interface",	/* drv_linkinfo */
3137c478bd9Sstevel@tonic-gate 	&devops,
3147c478bd9Sstevel@tonic-gate };
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
3177c478bd9Sstevel@tonic-gate 	MODREV_1,		/* ml_rev */
3187c478bd9Sstevel@tonic-gate 	&modldrv,		/* ml_linkage */
3197c478bd9Sstevel@tonic-gate 	NULL
3207c478bd9Sstevel@tonic-gate };
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate /*
3237c478bd9Sstevel@tonic-gate  * DDI entry points.
3247c478bd9Sstevel@tonic-gate  */
3257c478bd9Sstevel@tonic-gate int
_init(void)3267c478bd9Sstevel@tonic-gate _init(void)
3277c478bd9Sstevel@tonic-gate {
3287c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
3297c478bd9Sstevel@tonic-gate }
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate int
_fini(void)3327c478bd9Sstevel@tonic-gate _fini(void)
3337c478bd9Sstevel@tonic-gate {
3347c478bd9Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)3387c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
3397c478bd9Sstevel@tonic-gate {
3407c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
3417c478bd9Sstevel@tonic-gate }
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate /* ARGSUSED */
3447c478bd9Sstevel@tonic-gate static int
crypto_getinfo(dev_info_t * dip,ddi_info_cmd_t cmd,void * arg,void ** result)3457c478bd9Sstevel@tonic-gate crypto_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
3467c478bd9Sstevel@tonic-gate {
3477c478bd9Sstevel@tonic-gate 	switch (cmd) {
3487c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
3497c478bd9Sstevel@tonic-gate 		*result = crypto_dip;
3507c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
3537c478bd9Sstevel@tonic-gate 		*result = (void *)0;
3547c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
3557c478bd9Sstevel@tonic-gate 	}
3567c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate static int
crypto_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)3607c478bd9Sstevel@tonic-gate crypto_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3617c478bd9Sstevel@tonic-gate {
362ef56a3c5SKrishna Yenduri 	int i;
363ef56a3c5SKrishna Yenduri 
3647c478bd9Sstevel@tonic-gate 	if (cmd != DDI_ATTACH) {
3657c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3667c478bd9Sstevel@tonic-gate 	}
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	if (ddi_get_instance(dip) != 0) {
3697c478bd9Sstevel@tonic-gate 		/* we only allow instance 0 to attach */
3707c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	crypto_session_cache = kmem_cache_create("crypto_session_cache",
3747c478bd9Sstevel@tonic-gate 	    sizeof (crypto_session_data_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	if (crypto_session_cache == NULL)
3777c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	/* create the minor node */
3807c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(dip, "crypto", S_IFCHR, 0,
3817c478bd9Sstevel@tonic-gate 	    DDI_PSEUDO, 0) != DDI_SUCCESS) {
3827c478bd9Sstevel@tonic-gate 		kmem_cache_destroy(crypto_session_cache);
3837c478bd9Sstevel@tonic-gate 		crypto_session_cache = NULL;
3847c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "crypto_attach: failed creating minor node");
3857c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
3867c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3877c478bd9Sstevel@tonic-gate 	}
3887c478bd9Sstevel@tonic-gate 
389ef56a3c5SKrishna Yenduri 	crypto_locks = kmem_zalloc(max_ncpus * sizeof (kcf_lock_withpad_t),
390ef56a3c5SKrishna Yenduri 	    KM_SLEEP);
391ef56a3c5SKrishna Yenduri 	for (i = 0; i < max_ncpus; i++)
392ef56a3c5SKrishna Yenduri 		mutex_init(&crypto_locks[i].kl_lock, NULL, MUTEX_DRIVER, NULL);
393ef56a3c5SKrishna Yenduri 
3947c478bd9Sstevel@tonic-gate 	crypto_dip = dip;
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	/* allocate integer space for minor numbers */
3977c478bd9Sstevel@tonic-gate 	crypto_arena = vmem_create("crypto", (void *)1,
3987c478bd9Sstevel@tonic-gate 	    CRYPTO_MINOR_CHUNK, 1, NULL, NULL, NULL, 0,
3997c478bd9Sstevel@tonic-gate 	    VM_SLEEP | VMC_IDENTIFIER);
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate static int
crypto_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)4057c478bd9Sstevel@tonic-gate crypto_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4067c478bd9Sstevel@tonic-gate {
4077c478bd9Sstevel@tonic-gate 	minor_t i;
408ef56a3c5SKrishna Yenduri 	kcf_lock_withpad_t *mp;
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
4117c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4127c478bd9Sstevel@tonic-gate 
413ef56a3c5SKrishna Yenduri 	mp = &crypto_locks[CPU_SEQID];
414ef56a3c5SKrishna Yenduri 	mutex_enter(&mp->kl_lock);
415ef56a3c5SKrishna Yenduri 
4167c478bd9Sstevel@tonic-gate 	/* check if device is open */
4177c478bd9Sstevel@tonic-gate 	for (i = 0; i < crypto_minors_table_count; i++) {
4187c478bd9Sstevel@tonic-gate 		if (crypto_minors[i] != NULL) {
419ef56a3c5SKrishna Yenduri 			mutex_exit(&mp->kl_lock);
4207c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
4217c478bd9Sstevel@tonic-gate 		}
4227c478bd9Sstevel@tonic-gate 	}
423ef56a3c5SKrishna Yenduri 	mutex_exit(&mp->kl_lock);
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	crypto_dip = NULL;
4267c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	kmem_cache_destroy(crypto_session_cache);
4297c478bd9Sstevel@tonic-gate 	crypto_session_cache = NULL;
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	kmem_free(crypto_minors,
4327c478bd9Sstevel@tonic-gate 	    sizeof (crypto_minor_t *) * crypto_minors_table_count);
4337c478bd9Sstevel@tonic-gate 	crypto_minors = NULL;
4347c478bd9Sstevel@tonic-gate 	crypto_minors_table_count = 0;
435ef56a3c5SKrishna Yenduri 	for (i = 0; i < max_ncpus; i++)
436ef56a3c5SKrishna Yenduri 		mutex_destroy(&crypto_locks[i].kl_lock);
4378326c110SKrishna Yenduri 	kmem_free(crypto_locks, max_ncpus * sizeof (kcf_lock_withpad_t));
4388326c110SKrishna Yenduri 	crypto_locks = NULL;
439ef56a3c5SKrishna Yenduri 
4407c478bd9Sstevel@tonic-gate 	vmem_destroy(crypto_arena);
4417c478bd9Sstevel@tonic-gate 	crypto_arena = NULL;
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
4447c478bd9Sstevel@tonic-gate }
4457c478bd9Sstevel@tonic-gate 
44695014fbbSDan OpenSolaris Anderson /* ARGSUSED3 */
4477c478bd9Sstevel@tonic-gate static int
crypto_open(dev_t * devp,int flag,int otyp,cred_t * credp)4487c478bd9Sstevel@tonic-gate crypto_open(dev_t *devp, int flag, int otyp, cred_t *credp)
4497c478bd9Sstevel@tonic-gate {
4507c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm = NULL;
4517c478bd9Sstevel@tonic-gate 	minor_t mn;
452ef56a3c5SKrishna Yenduri 	kcf_lock_withpad_t *mp;
453ef56a3c5SKrishna Yenduri 	int i;
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
4567c478bd9Sstevel@tonic-gate 		return (ENXIO);
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	if (crypto_dip == NULL)
4597c478bd9Sstevel@tonic-gate 		return (ENXIO);
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	/* exclusive opens are not supported */
4627c478bd9Sstevel@tonic-gate 	if (flag & FEXCL)
4637c478bd9Sstevel@tonic-gate 		return (ENOTSUP);
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate again:
466ef56a3c5SKrishna Yenduri 	mp = &crypto_locks[CPU_SEQID];
467ef56a3c5SKrishna Yenduri 	mutex_enter(&mp->kl_lock);
468ef56a3c5SKrishna Yenduri 
4697c478bd9Sstevel@tonic-gate 	/* grow the minors table if needed */
4707c478bd9Sstevel@tonic-gate 	if (crypto_minors_count >= crypto_minors_table_count) {
4717c478bd9Sstevel@tonic-gate 		crypto_minor_t **newtable;
4727c478bd9Sstevel@tonic-gate 		minor_t chunk = crypto_minor_chunk;
4737c478bd9Sstevel@tonic-gate 		minor_t saved_count;
4747c478bd9Sstevel@tonic-gate 		size_t new_size;
4757c478bd9Sstevel@tonic-gate 		ulong_t big_count;
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 		big_count = crypto_minors_count + chunk;
4787c478bd9Sstevel@tonic-gate 		if (big_count > MAXMIN) {
479ef56a3c5SKrishna Yenduri 			mutex_exit(&mp->kl_lock);
4807c478bd9Sstevel@tonic-gate 			return (ENOMEM);
4817c478bd9Sstevel@tonic-gate 		}
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 		saved_count = crypto_minors_table_count;
4847c478bd9Sstevel@tonic-gate 		new_size = sizeof (crypto_minor_t *) *
4857c478bd9Sstevel@tonic-gate 		    (crypto_minors_table_count + chunk);
4867c478bd9Sstevel@tonic-gate 
487ef56a3c5SKrishna Yenduri 		mutex_exit(&mp->kl_lock);
4887c478bd9Sstevel@tonic-gate 
489ef56a3c5SKrishna Yenduri 		newtable = kmem_zalloc(new_size, KM_SLEEP);
490ef56a3c5SKrishna Yenduri 		CRYPTO_ENTER_ALL_LOCKS();
4917c478bd9Sstevel@tonic-gate 		/*
4927c478bd9Sstevel@tonic-gate 		 * Check if table grew while we were sleeping.
4937c478bd9Sstevel@tonic-gate 		 * The minors table never shrinks.
4947c478bd9Sstevel@tonic-gate 		 */
4957c478bd9Sstevel@tonic-gate 		if (crypto_minors_table_count > saved_count) {
496ef56a3c5SKrishna Yenduri 			CRYPTO_EXIT_ALL_LOCKS();
4977c478bd9Sstevel@tonic-gate 			kmem_free(newtable, new_size);
4987c478bd9Sstevel@tonic-gate 			goto again;
4997c478bd9Sstevel@tonic-gate 		}
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 		/* we assume that bcopy() will return if count is 0 */
5027c478bd9Sstevel@tonic-gate 		bcopy(crypto_minors, newtable,
5037c478bd9Sstevel@tonic-gate 		    sizeof (crypto_minor_t *) * crypto_minors_table_count);
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 		kmem_free(crypto_minors,
5067c478bd9Sstevel@tonic-gate 		    sizeof (crypto_minor_t *) * crypto_minors_table_count);
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 		/* grow the minors number space */
5097c478bd9Sstevel@tonic-gate 		if (crypto_minors_table_count != 0) {
5107c478bd9Sstevel@tonic-gate 			(void) vmem_add(crypto_arena,
5117c478bd9Sstevel@tonic-gate 			    (void *)(uintptr_t)(crypto_minors_table_count + 1),
5127c478bd9Sstevel@tonic-gate 			    crypto_minor_chunk, VM_SLEEP);
5137c478bd9Sstevel@tonic-gate 		}
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 		crypto_minors = newtable;
5167c478bd9Sstevel@tonic-gate 		crypto_minors_table_count += chunk;
517ef56a3c5SKrishna Yenduri 		CRYPTO_EXIT_ALL_LOCKS();
518ef56a3c5SKrishna Yenduri 	} else {
519ef56a3c5SKrishna Yenduri 		mutex_exit(&mp->kl_lock);
5207c478bd9Sstevel@tonic-gate 	}
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	/* allocate a new minor number starting with 1 */
5237c478bd9Sstevel@tonic-gate 	mn = (minor_t)(uintptr_t)vmem_alloc(crypto_arena, 1, VM_SLEEP);
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	cm = kmem_zalloc(sizeof (crypto_minor_t), KM_SLEEP);
5267c478bd9Sstevel@tonic-gate 	mutex_init(&cm->cm_lock, NULL, MUTEX_DRIVER, NULL);
5277c478bd9Sstevel@tonic-gate 	cv_init(&cm->cm_cv, NULL, CV_DRIVER, NULL);
5287c478bd9Sstevel@tonic-gate 
529ef56a3c5SKrishna Yenduri 	CRYPTO_ENTER_ALL_LOCKS();
530ac129f9eSKrishna Yenduri 	cm->cm_refcnt = 1;
5317c478bd9Sstevel@tonic-gate 	crypto_minors[mn - 1] = cm;
5327c478bd9Sstevel@tonic-gate 	crypto_minors_count++;
533ef56a3c5SKrishna Yenduri 	CRYPTO_EXIT_ALL_LOCKS();
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	*devp = makedevice(getmajor(*devp), mn);
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	return (0);
5387c478bd9Sstevel@tonic-gate }
5397c478bd9Sstevel@tonic-gate 
54095014fbbSDan OpenSolaris Anderson /* ARGSUSED1 */
5417c478bd9Sstevel@tonic-gate static int
crypto_close(dev_t dev,int flag,int otyp,cred_t * credp)5427c478bd9Sstevel@tonic-gate crypto_close(dev_t dev, int flag, int otyp, cred_t *credp)
5437c478bd9Sstevel@tonic-gate {
5447c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm = NULL;
5457c478bd9Sstevel@tonic-gate 	crypto_session_data_t *sp;
5467c478bd9Sstevel@tonic-gate 	minor_t mn = getminor(dev);
5477c478bd9Sstevel@tonic-gate 	uint_t i;
5482d794da1Skrishna 	size_t total = 0;
549ef56a3c5SKrishna Yenduri 	kcf_lock_withpad_t *mp;
550ef56a3c5SKrishna Yenduri 
551ef56a3c5SKrishna Yenduri 	mp = &crypto_locks[CPU_SEQID];
552ef56a3c5SKrishna Yenduri 	mutex_enter(&mp->kl_lock);
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	if (mn > crypto_minors_table_count) {
555ef56a3c5SKrishna Yenduri 		mutex_exit(&mp->kl_lock);
5567c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "crypto_close: bad minor (too big) %d", mn);
5577c478bd9Sstevel@tonic-gate 		return (ENODEV);
5587c478bd9Sstevel@tonic-gate 	}
5597c478bd9Sstevel@tonic-gate 
560ac129f9eSKrishna Yenduri 	cm = crypto_minors[mn - 1];
5617c478bd9Sstevel@tonic-gate 	if (cm == NULL) {
562ef56a3c5SKrishna Yenduri 		mutex_exit(&mp->kl_lock);
5637c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "crypto_close: duplicate close of minor %d",
5647c478bd9Sstevel@tonic-gate 		    getminor(dev));
5657c478bd9Sstevel@tonic-gate 		return (ENODEV);
5667c478bd9Sstevel@tonic-gate 	}
5677c478bd9Sstevel@tonic-gate 
568ef56a3c5SKrishna Yenduri 	mutex_exit(&mp->kl_lock);
569ac129f9eSKrishna Yenduri 
570ef56a3c5SKrishna Yenduri 	CRYPTO_ENTER_ALL_LOCKS();
571ef56a3c5SKrishna Yenduri 	/*
572ef56a3c5SKrishna Yenduri 	 * We free the minor number, mn, from the crypto_arena
573ef56a3c5SKrishna Yenduri 	 * only later. This ensures that we won't race with another
574ef56a3c5SKrishna Yenduri 	 * thread in crypto_open with the same minor number.
575ef56a3c5SKrishna Yenduri 	 */
5767c478bd9Sstevel@tonic-gate 	crypto_minors[mn - 1] = NULL;
5777c478bd9Sstevel@tonic-gate 	crypto_minors_count--;
578ef56a3c5SKrishna Yenduri 	CRYPTO_EXIT_ALL_LOCKS();
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
581ef56a3c5SKrishna Yenduri 	cm->cm_refcnt --;		/* decrement refcnt held in open */
582ef56a3c5SKrishna Yenduri 	while (cm->cm_refcnt > 0) {
583ef56a3c5SKrishna Yenduri 		cv_wait(&cm->cm_cv, &cm->cm_lock);
584ef56a3c5SKrishna Yenduri 	}
585ef56a3c5SKrishna Yenduri 
586ef56a3c5SKrishna Yenduri 	vmem_free(crypto_arena, (void *)(uintptr_t)mn, 1);
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	/* free all session table entries starting with 1 */
5897c478bd9Sstevel@tonic-gate 	for (i = 1; i < cm->cm_session_table_count; i++) {
5907c478bd9Sstevel@tonic-gate 		if (cm->cm_session_table[i] == NULL)
5917c478bd9Sstevel@tonic-gate 			continue;
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 		sp = cm->cm_session_table[i];
5947c478bd9Sstevel@tonic-gate 		ASSERT((sp->sd_flags & CRYPTO_SESSION_IS_BUSY) == 0);
5952d794da1Skrishna 		ASSERT(sp->sd_pre_approved_amount == 0 ||
5962d794da1Skrishna 		    sp->sd_pre_approved_amount == crypto_pre_approved_limit);
5972d794da1Skrishna 		total += sp->sd_pre_approved_amount;
5987c478bd9Sstevel@tonic-gate 		if (sp->sd_find_init_cookie != NULL) {
5997c478bd9Sstevel@tonic-gate 			(void) crypto_free_find_ctx(sp);
6007c478bd9Sstevel@tonic-gate 		}
6017c478bd9Sstevel@tonic-gate 		crypto_release_provider_session(cm, sp->sd_provider_session);
6027c478bd9Sstevel@tonic-gate 		KCF_PROV_REFRELE(sp->sd_provider);
6037c478bd9Sstevel@tonic-gate 		CRYPTO_CANCEL_ALL_CTX(sp);
6047c478bd9Sstevel@tonic-gate 		mutex_destroy(&sp->sd_lock);
6057c478bd9Sstevel@tonic-gate 		cv_destroy(&sp->sd_cv);
6067c478bd9Sstevel@tonic-gate 		kmem_cache_free(crypto_session_cache, sp);
6077c478bd9Sstevel@tonic-gate 		cm->cm_session_table[i] = NULL;
6087c478bd9Sstevel@tonic-gate 	}
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	/* free the session table */
6117c478bd9Sstevel@tonic-gate 	if (cm->cm_session_table != NULL && cm->cm_session_table_count > 0)
6127c478bd9Sstevel@tonic-gate 		kmem_free(cm->cm_session_table, cm->cm_session_table_count *
6137c478bd9Sstevel@tonic-gate 		    sizeof (void *));
6147c478bd9Sstevel@tonic-gate 
6152d794da1Skrishna 	total += (cm->cm_session_table_count * sizeof (void *));
6162d794da1Skrishna 	CRYPTO_DECREMENT_RCTL(total);
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	kcf_free_provider_tab(cm->cm_provider_count,
6197c478bd9Sstevel@tonic-gate 	    cm->cm_provider_array);
6207c478bd9Sstevel@tonic-gate 
621ef56a3c5SKrishna Yenduri 	mutex_exit(&cm->cm_lock);
6227c478bd9Sstevel@tonic-gate 	mutex_destroy(&cm->cm_lock);
6237c478bd9Sstevel@tonic-gate 	cv_destroy(&cm->cm_cv);
6247c478bd9Sstevel@tonic-gate 	kmem_free(cm, sizeof (crypto_minor_t));
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	return (0);
6277c478bd9Sstevel@tonic-gate }
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate static crypto_minor_t *
crypto_hold_minor(minor_t minor)6307c478bd9Sstevel@tonic-gate crypto_hold_minor(minor_t minor)
6317c478bd9Sstevel@tonic-gate {
632ac129f9eSKrishna Yenduri 	crypto_minor_t *cm;
633ef56a3c5SKrishna Yenduri 	kcf_lock_withpad_t *mp;
634ac129f9eSKrishna Yenduri 
635ac129f9eSKrishna Yenduri 	if (minor > crypto_minors_table_count)
636ac129f9eSKrishna Yenduri 		return (NULL);
6377c478bd9Sstevel@tonic-gate 
638ef56a3c5SKrishna Yenduri 	mp = &crypto_locks[CPU_SEQID];
639ef56a3c5SKrishna Yenduri 	mutex_enter(&mp->kl_lock);
640ef56a3c5SKrishna Yenduri 
641ac129f9eSKrishna Yenduri 	if ((cm = crypto_minors[minor - 1]) != NULL) {
6421a5e258fSJosef 'Jeff' Sipek 		atomic_inc_32(&cm->cm_refcnt);
6437c478bd9Sstevel@tonic-gate 	}
644ef56a3c5SKrishna Yenduri 	mutex_exit(&mp->kl_lock);
6457c478bd9Sstevel@tonic-gate 	return (cm);
6467c478bd9Sstevel@tonic-gate }
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate static void
crypto_release_minor(crypto_minor_t * cm)6497c478bd9Sstevel@tonic-gate crypto_release_minor(crypto_minor_t *cm)
6507c478bd9Sstevel@tonic-gate {
6511a5e258fSJosef 'Jeff' Sipek 	if (atomic_dec_32_nv(&cm->cm_refcnt) == 0) {
652ac129f9eSKrishna Yenduri 		cv_signal(&cm->cm_cv);
6537c478bd9Sstevel@tonic-gate 	}
6547c478bd9Sstevel@tonic-gate }
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate /*
657ba5f469cSkrishna  * Build a list of functions and other information for the provider, pd.
6587c478bd9Sstevel@tonic-gate  */
6597c478bd9Sstevel@tonic-gate static void
crypto_build_function_list(crypto_function_list_t * fl,kcf_provider_desc_t * pd)6607c478bd9Sstevel@tonic-gate crypto_build_function_list(crypto_function_list_t *fl, kcf_provider_desc_t *pd)
6617c478bd9Sstevel@tonic-gate {
6627c478bd9Sstevel@tonic-gate 	crypto_ops_t *ops;
6637c478bd9Sstevel@tonic-gate 	crypto_digest_ops_t *digest_ops;
6647c478bd9Sstevel@tonic-gate 	crypto_cipher_ops_t *cipher_ops;
6657c478bd9Sstevel@tonic-gate 	crypto_mac_ops_t *mac_ops;
6667c478bd9Sstevel@tonic-gate 	crypto_sign_ops_t *sign_ops;
6677c478bd9Sstevel@tonic-gate 	crypto_verify_ops_t *verify_ops;
6687c478bd9Sstevel@tonic-gate 	crypto_dual_ops_t *dual_ops;
6697c478bd9Sstevel@tonic-gate 	crypto_random_number_ops_t *random_number_ops;
6707c478bd9Sstevel@tonic-gate 	crypto_session_ops_t *session_ops;
6717c478bd9Sstevel@tonic-gate 	crypto_object_ops_t *object_ops;
6727c478bd9Sstevel@tonic-gate 	crypto_key_ops_t *key_ops;
6737c478bd9Sstevel@tonic-gate 	crypto_provider_management_ops_t *provider_ops;
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	if ((ops = pd->pd_ops_vector) == NULL)
6767c478bd9Sstevel@tonic-gate 		return;
6777c478bd9Sstevel@tonic-gate 
678894b2776Smcpowers 	if ((digest_ops = ops->co_digest_ops) != NULL) {
6797c478bd9Sstevel@tonic-gate 		if (digest_ops->digest_init != NULL)
6807c478bd9Sstevel@tonic-gate 			fl->fl_digest_init = B_TRUE;
6817c478bd9Sstevel@tonic-gate 		if (digest_ops->digest != NULL)
6827c478bd9Sstevel@tonic-gate 			fl->fl_digest = B_TRUE;
6837c478bd9Sstevel@tonic-gate 		if (digest_ops->digest_update != NULL)
6847c478bd9Sstevel@tonic-gate 			fl->fl_digest_update = B_TRUE;
6857c478bd9Sstevel@tonic-gate 		if (digest_ops->digest_key != NULL)
6867c478bd9Sstevel@tonic-gate 			fl->fl_digest_key = B_TRUE;
6877c478bd9Sstevel@tonic-gate 		if (digest_ops->digest_final != NULL)
6887c478bd9Sstevel@tonic-gate 			fl->fl_digest_final = B_TRUE;
6897c478bd9Sstevel@tonic-gate 	}
690894b2776Smcpowers 	if ((cipher_ops = ops->co_cipher_ops) != NULL) {
6917c478bd9Sstevel@tonic-gate 		if (cipher_ops->encrypt_init != NULL)
6927c478bd9Sstevel@tonic-gate 			fl->fl_encrypt_init = B_TRUE;
6937c478bd9Sstevel@tonic-gate 		if (cipher_ops->encrypt != NULL)
6947c478bd9Sstevel@tonic-gate 			fl->fl_encrypt = B_TRUE;
6957c478bd9Sstevel@tonic-gate 		if (cipher_ops->encrypt_update != NULL)
6967c478bd9Sstevel@tonic-gate 			fl->fl_encrypt_update = B_TRUE;
6977c478bd9Sstevel@tonic-gate 		if (cipher_ops->encrypt_final != NULL)
6987c478bd9Sstevel@tonic-gate 			fl->fl_encrypt_final = B_TRUE;
6997c478bd9Sstevel@tonic-gate 		if (cipher_ops->decrypt_init != NULL)
7007c478bd9Sstevel@tonic-gate 			fl->fl_decrypt_init = B_TRUE;
7017c478bd9Sstevel@tonic-gate 		if (cipher_ops->decrypt != NULL)
7027c478bd9Sstevel@tonic-gate 			fl->fl_decrypt = B_TRUE;
7037c478bd9Sstevel@tonic-gate 		if (cipher_ops->decrypt_update != NULL)
7047c478bd9Sstevel@tonic-gate 			fl->fl_decrypt_update = B_TRUE;
7057c478bd9Sstevel@tonic-gate 		if (cipher_ops->decrypt_final != NULL)
7067c478bd9Sstevel@tonic-gate 			fl->fl_decrypt_final = B_TRUE;
7077c478bd9Sstevel@tonic-gate 	}
708894b2776Smcpowers 	if ((mac_ops = ops->co_mac_ops) != NULL) {
7097c478bd9Sstevel@tonic-gate 		if (mac_ops->mac_init != NULL)
7107c478bd9Sstevel@tonic-gate 			fl->fl_mac_init = B_TRUE;
7117c478bd9Sstevel@tonic-gate 		if (mac_ops->mac != NULL)
7127c478bd9Sstevel@tonic-gate 			fl->fl_mac = B_TRUE;
7137c478bd9Sstevel@tonic-gate 		if (mac_ops->mac_update != NULL)
7147c478bd9Sstevel@tonic-gate 			fl->fl_mac_update = B_TRUE;
7157c478bd9Sstevel@tonic-gate 		if (mac_ops->mac_final != NULL)
7167c478bd9Sstevel@tonic-gate 			fl->fl_mac_final = B_TRUE;
7177c478bd9Sstevel@tonic-gate 	}
718894b2776Smcpowers 	if ((sign_ops = ops->co_sign_ops) != NULL) {
7197c478bd9Sstevel@tonic-gate 		if (sign_ops->sign_init != NULL)
7207c478bd9Sstevel@tonic-gate 			fl->fl_sign_init = B_TRUE;
7217c478bd9Sstevel@tonic-gate 		if (sign_ops->sign != NULL)
7227c478bd9Sstevel@tonic-gate 			fl->fl_sign = B_TRUE;
7237c478bd9Sstevel@tonic-gate 		if (sign_ops->sign_update != NULL)
7247c478bd9Sstevel@tonic-gate 			fl->fl_sign_update = B_TRUE;
7257c478bd9Sstevel@tonic-gate 		if (sign_ops->sign_final != NULL)
7267c478bd9Sstevel@tonic-gate 			fl->fl_sign_final = B_TRUE;
7277c478bd9Sstevel@tonic-gate 		if (sign_ops->sign_recover_init != NULL)
7287c478bd9Sstevel@tonic-gate 			fl->fl_sign_recover_init = B_TRUE;
7297c478bd9Sstevel@tonic-gate 		if (sign_ops->sign_recover != NULL)
7307c478bd9Sstevel@tonic-gate 			fl->fl_sign_recover = B_TRUE;
7317c478bd9Sstevel@tonic-gate 	}
732894b2776Smcpowers 	if ((verify_ops = ops->co_verify_ops) != NULL) {
7337c478bd9Sstevel@tonic-gate 		if (verify_ops->verify_init != NULL)
7347c478bd9Sstevel@tonic-gate 			fl->fl_verify_init = B_TRUE;
7357c478bd9Sstevel@tonic-gate 		if (verify_ops->verify != NULL)
7367c478bd9Sstevel@tonic-gate 			fl->fl_verify = B_TRUE;
7377c478bd9Sstevel@tonic-gate 		if (verify_ops->verify_update != NULL)
7387c478bd9Sstevel@tonic-gate 			fl->fl_verify_update = B_TRUE;
7397c478bd9Sstevel@tonic-gate 		if (verify_ops->verify_final != NULL)
7407c478bd9Sstevel@tonic-gate 			fl->fl_verify_final = B_TRUE;
7417c478bd9Sstevel@tonic-gate 		if (verify_ops->verify_recover_init != NULL)
7427c478bd9Sstevel@tonic-gate 			fl->fl_verify_recover_init = B_TRUE;
7437c478bd9Sstevel@tonic-gate 		if (verify_ops->verify_recover != NULL)
7447c478bd9Sstevel@tonic-gate 			fl->fl_verify_recover = B_TRUE;
7457c478bd9Sstevel@tonic-gate 	}
746894b2776Smcpowers 	if ((dual_ops = ops->co_dual_ops) != NULL) {
7477c478bd9Sstevel@tonic-gate 		if (dual_ops->digest_encrypt_update != NULL)
7487c478bd9Sstevel@tonic-gate 			fl->fl_digest_encrypt_update = B_TRUE;
7497c478bd9Sstevel@tonic-gate 		if (dual_ops->decrypt_digest_update != NULL)
7507c478bd9Sstevel@tonic-gate 			fl->fl_decrypt_digest_update = B_TRUE;
7517c478bd9Sstevel@tonic-gate 		if (dual_ops->sign_encrypt_update != NULL)
7527c478bd9Sstevel@tonic-gate 			fl->fl_sign_encrypt_update = B_TRUE;
7537c478bd9Sstevel@tonic-gate 		if (dual_ops->decrypt_verify_update != NULL)
7547c478bd9Sstevel@tonic-gate 			fl->fl_decrypt_verify_update = B_TRUE;
7557c478bd9Sstevel@tonic-gate 	}
756894b2776Smcpowers 	if ((random_number_ops = ops->co_random_ops) != NULL) {
7577c478bd9Sstevel@tonic-gate 		if (random_number_ops->seed_random != NULL)
7587c478bd9Sstevel@tonic-gate 			fl->fl_seed_random = B_TRUE;
7597c478bd9Sstevel@tonic-gate 		if (random_number_ops->generate_random != NULL)
7607c478bd9Sstevel@tonic-gate 			fl->fl_generate_random = B_TRUE;
7617c478bd9Sstevel@tonic-gate 	}
762894b2776Smcpowers 	if ((session_ops = ops->co_session_ops) != NULL) {
7637c478bd9Sstevel@tonic-gate 		if (session_ops->session_open != NULL)
7647c478bd9Sstevel@tonic-gate 			fl->fl_session_open = B_TRUE;
7657c478bd9Sstevel@tonic-gate 		if (session_ops->session_close != NULL)
7667c478bd9Sstevel@tonic-gate 			fl->fl_session_close = B_TRUE;
7677c478bd9Sstevel@tonic-gate 		if (session_ops->session_login != NULL)
7687c478bd9Sstevel@tonic-gate 			fl->fl_session_login = B_TRUE;
7697c478bd9Sstevel@tonic-gate 		if (session_ops->session_logout != NULL)
7707c478bd9Sstevel@tonic-gate 			fl->fl_session_logout = B_TRUE;
7717c478bd9Sstevel@tonic-gate 	}
772894b2776Smcpowers 	if ((object_ops = ops->co_object_ops) != NULL) {
7737c478bd9Sstevel@tonic-gate 		if (object_ops->object_create != NULL)
7747c478bd9Sstevel@tonic-gate 			fl->fl_object_create = B_TRUE;
7757c478bd9Sstevel@tonic-gate 		if (object_ops->object_copy != NULL)
7767c478bd9Sstevel@tonic-gate 			fl->fl_object_copy = B_TRUE;
7777c478bd9Sstevel@tonic-gate 		if (object_ops->object_destroy != NULL)
7787c478bd9Sstevel@tonic-gate 			fl->fl_object_destroy = B_TRUE;
7797c478bd9Sstevel@tonic-gate 		if (object_ops->object_get_size != NULL)
7807c478bd9Sstevel@tonic-gate 			fl->fl_object_get_size = B_TRUE;
7817c478bd9Sstevel@tonic-gate 		if (object_ops->object_get_attribute_value != NULL)
7827c478bd9Sstevel@tonic-gate 			fl->fl_object_get_attribute_value = B_TRUE;
7837c478bd9Sstevel@tonic-gate 		if (object_ops->object_set_attribute_value != NULL)
7847c478bd9Sstevel@tonic-gate 			fl->fl_object_set_attribute_value = B_TRUE;
7857c478bd9Sstevel@tonic-gate 		if (object_ops->object_find_init != NULL)
7867c478bd9Sstevel@tonic-gate 			fl->fl_object_find_init = B_TRUE;
7877c478bd9Sstevel@tonic-gate 		if (object_ops->object_find != NULL)
7887c478bd9Sstevel@tonic-gate 			fl->fl_object_find = B_TRUE;
7897c478bd9Sstevel@tonic-gate 		if (object_ops->object_find_final != NULL)
7907c478bd9Sstevel@tonic-gate 			fl->fl_object_find_final = B_TRUE;
7917c478bd9Sstevel@tonic-gate 	}
792894b2776Smcpowers 	if ((key_ops = ops->co_key_ops) != NULL) {
7937c478bd9Sstevel@tonic-gate 		if (key_ops->key_generate != NULL)
7947c478bd9Sstevel@tonic-gate 			fl->fl_key_generate = B_TRUE;
7957c478bd9Sstevel@tonic-gate 		if (key_ops->key_generate_pair != NULL)
7967c478bd9Sstevel@tonic-gate 			fl->fl_key_generate_pair = B_TRUE;
7977c478bd9Sstevel@tonic-gate 		if (key_ops->key_wrap != NULL)
7987c478bd9Sstevel@tonic-gate 			fl->fl_key_wrap = B_TRUE;
7997c478bd9Sstevel@tonic-gate 		if (key_ops->key_unwrap != NULL)
8007c478bd9Sstevel@tonic-gate 			fl->fl_key_unwrap = B_TRUE;
8017c478bd9Sstevel@tonic-gate 		if (key_ops->key_derive != NULL)
8027c478bd9Sstevel@tonic-gate 			fl->fl_key_derive = B_TRUE;
8037c478bd9Sstevel@tonic-gate 	}
804894b2776Smcpowers 	if ((provider_ops = ops->co_provider_ops) != NULL) {
8057c478bd9Sstevel@tonic-gate 		if (provider_ops->init_token != NULL)
8067c478bd9Sstevel@tonic-gate 			fl->fl_init_token = B_TRUE;
8077c478bd9Sstevel@tonic-gate 		if (provider_ops->init_pin != NULL)
8087c478bd9Sstevel@tonic-gate 			fl->fl_init_pin = B_TRUE;
8097c478bd9Sstevel@tonic-gate 		if (provider_ops->set_pin != NULL)
8107c478bd9Sstevel@tonic-gate 			fl->fl_set_pin = B_TRUE;
8117c478bd9Sstevel@tonic-gate 	}
812ba5f469cSkrishna 
8134df55fdeSJanie Lu 	fl->prov_is_hash_limited = pd->pd_flags & CRYPTO_HASH_NO_UPDATE;
8144df55fdeSJanie Lu 	if (fl->prov_is_hash_limited) {
8154df55fdeSJanie Lu 		fl->prov_hash_limit = min(pd->pd_hash_limit,
8164df55fdeSJanie Lu 		    min(CRYPTO_MAX_BUFFER_LEN,
8174df55fdeSJanie Lu 		    curproc->p_task->tk_proj->kpj_data.kpd_crypto_mem_ctl));
8184df55fdeSJanie Lu 	}
8194df55fdeSJanie Lu 
8204df55fdeSJanie Lu 	fl->prov_is_hmac_limited = pd->pd_flags & CRYPTO_HMAC_NO_UPDATE;
8214df55fdeSJanie Lu 	if (fl->prov_is_hmac_limited) {
8224df55fdeSJanie Lu 		fl->prov_hmac_limit = min(pd->pd_hmac_limit,
8234df55fdeSJanie Lu 		    min(CRYPTO_MAX_BUFFER_LEN,
8244df55fdeSJanie Lu 		    curproc->p_task->tk_proj->kpj_data.kpd_crypto_mem_ctl));
8254df55fdeSJanie Lu 	}
8264df55fdeSJanie Lu 
8274df55fdeSJanie Lu 	if (fl->prov_is_hash_limited || fl->prov_is_hmac_limited) {
828ba5f469cSkrishna 		/*
8294df55fdeSJanie Lu 		 * XXX - The threshold should ideally be per hash/HMAC
830ba5f469cSkrishna 		 * mechanism. For now, we use the same value for all
8314df55fdeSJanie Lu 		 * hash/HMAC mechanisms. Empirical evidence suggests this
832ba5f469cSkrishna 		 * is fine.
833ba5f469cSkrishna 		 */
834ba5f469cSkrishna 		fl->prov_hash_threshold = kcf_md5_threshold;
835ba5f469cSkrishna 	}
8364a5b2e70Shaimay 
8374a5b2e70Shaimay 	fl->total_threshold_count = MAX_NUM_THRESHOLD;
8384a5b2e70Shaimay 	fl->fl_threshold[0].mech_type = CKM_DES3_CBC;
8394a5b2e70Shaimay 	fl->fl_threshold[0].mech_threshold = kcf_des3_threshold;
8404a5b2e70Shaimay 	fl->fl_threshold[1].mech_type = CKM_DES3_ECB;
8414a5b2e70Shaimay 	fl->fl_threshold[1].mech_threshold = kcf_des3_threshold;
8424a5b2e70Shaimay 	fl->fl_threshold[2].mech_type = CKM_AES_CBC;
8434a5b2e70Shaimay 	fl->fl_threshold[2].mech_threshold = kcf_aes_threshold;
8444a5b2e70Shaimay 	fl->fl_threshold[3].mech_type = CKM_AES_ECB;
8454a5b2e70Shaimay 	fl->fl_threshold[3].mech_threshold = kcf_aes_threshold;
8464a5b2e70Shaimay 	fl->fl_threshold[4].mech_type = CKM_RC4;
8474a5b2e70Shaimay 	fl->fl_threshold[4].mech_threshold = kcf_rc4_threshold;
8484a5b2e70Shaimay 	fl->fl_threshold[5].mech_type = CKM_MD5;
8494a5b2e70Shaimay 	fl->fl_threshold[5].mech_threshold = kcf_md5_threshold;
8504a5b2e70Shaimay 	fl->fl_threshold[6].mech_type = CKM_SHA_1;
8514a5b2e70Shaimay 	fl->fl_threshold[6].mech_threshold = kcf_sha1_threshold;
8527c478bd9Sstevel@tonic-gate }
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate /* ARGSUSED */
8557c478bd9Sstevel@tonic-gate static int
get_function_list(dev_t dev,caddr_t arg,int mode,int * rval)8567c478bd9Sstevel@tonic-gate get_function_list(dev_t dev, caddr_t arg, int mode, int *rval)
8577c478bd9Sstevel@tonic-gate {
8587c478bd9Sstevel@tonic-gate 	crypto_get_function_list_t get_function_list;
8597c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
8607c478bd9Sstevel@tonic-gate 	crypto_provider_id_t provider_id;
8617c478bd9Sstevel@tonic-gate 	crypto_function_list_t *fl;
8627c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t *provider;
8637c478bd9Sstevel@tonic-gate 	int rv;
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
8667c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "get_function_list: failed holding minor");
8677c478bd9Sstevel@tonic-gate 		return (ENXIO);
8687c478bd9Sstevel@tonic-gate 	}
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	if (copyin(arg, &get_function_list, sizeof (get_function_list)) != 0) {
8717c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
8727c478bd9Sstevel@tonic-gate 		return (EFAULT);
8737c478bd9Sstevel@tonic-gate 	}
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	/* initialize provider_array */
8767c478bd9Sstevel@tonic-gate 	if (cm->cm_provider_array == NULL) {
8777c478bd9Sstevel@tonic-gate 		rv = crypto_get_provider_list(cm, NULL, NULL, DONT_RETURN_LIST);
8787c478bd9Sstevel@tonic-gate 		if (rv != CRYPTO_SUCCESS) {
8797c478bd9Sstevel@tonic-gate 			goto release_minor;
8807c478bd9Sstevel@tonic-gate 		}
8817c478bd9Sstevel@tonic-gate 	}
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 	provider_id = get_function_list.fl_provider_id;
8847c478bd9Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
8857c478bd9Sstevel@tonic-gate 	/* index must be less than count of providers */
8867c478bd9Sstevel@tonic-gate 	if (provider_id >= cm->cm_provider_count) {
8877c478bd9Sstevel@tonic-gate 		mutex_exit(&cm->cm_lock);
8887c478bd9Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
8897c478bd9Sstevel@tonic-gate 		goto release_minor;
8907c478bd9Sstevel@tonic-gate 	}
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 	ASSERT(cm->cm_provider_array != NULL);
8937c478bd9Sstevel@tonic-gate 	provider = cm->cm_provider_array[provider_id];
8947c478bd9Sstevel@tonic-gate 	mutex_exit(&cm->cm_lock);
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	fl = &get_function_list.fl_list;
8977c478bd9Sstevel@tonic-gate 	bzero(fl, sizeof (crypto_function_list_t));
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	if (provider->pd_prov_type != CRYPTO_LOGICAL_PROVIDER) {
9007c478bd9Sstevel@tonic-gate 		crypto_build_function_list(fl, provider);
9017c478bd9Sstevel@tonic-gate 	} else {
9027c478bd9Sstevel@tonic-gate 		kcf_provider_desc_t *prev = NULL, *pd;
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 		mutex_enter(&provider->pd_lock);
9057c478bd9Sstevel@tonic-gate 		while (kcf_get_next_logical_provider_member(provider,
9067c478bd9Sstevel@tonic-gate 		    prev, &pd)) {
9077c478bd9Sstevel@tonic-gate 			prev = pd;
9087c478bd9Sstevel@tonic-gate 			crypto_build_function_list(fl, pd);
9097c478bd9Sstevel@tonic-gate 			KCF_PROV_REFRELE(pd);
9107c478bd9Sstevel@tonic-gate 		}
9117c478bd9Sstevel@tonic-gate 		mutex_exit(&provider->pd_lock);
9127c478bd9Sstevel@tonic-gate 	}
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	rv = CRYPTO_SUCCESS;
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate release_minor:
9177c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 	get_function_list.fl_return_value = rv;
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 	if (copyout(&get_function_list, arg, sizeof (get_function_list)) != 0) {
9227c478bd9Sstevel@tonic-gate 		return (EFAULT);
9237c478bd9Sstevel@tonic-gate 	}
9247c478bd9Sstevel@tonic-gate 	return (0);
9257c478bd9Sstevel@tonic-gate }
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate /*
9287c478bd9Sstevel@tonic-gate  * This ioctl maps a PKCS#11 mechanism string into an internal number
9297c478bd9Sstevel@tonic-gate  * that is used by the kernel.  pn_internal_number is set to the
9307c478bd9Sstevel@tonic-gate  * internal number.
9317c478bd9Sstevel@tonic-gate  */
9327c478bd9Sstevel@tonic-gate /* ARGSUSED */
9337c478bd9Sstevel@tonic-gate static int
get_mechanism_number(dev_t dev,caddr_t arg,int mode,int * rval)9347c478bd9Sstevel@tonic-gate get_mechanism_number(dev_t dev, caddr_t arg, int mode, int *rval)
9357c478bd9Sstevel@tonic-gate {
9367c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_get_mechanism_number, get_number);
9377c478bd9Sstevel@tonic-gate 	crypto_mech_type_t number;
9387c478bd9Sstevel@tonic-gate 	size_t len;
9397c478bd9Sstevel@tonic-gate 	char *mechanism_name;
9407c478bd9Sstevel@tonic-gate 	int rv;
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 	STRUCT_INIT(get_number, mode);
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(get_number), STRUCT_SIZE(get_number)) != 0)
9457c478bd9Sstevel@tonic-gate 		return (EFAULT);
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	len = STRUCT_FGET(get_number, pn_mechanism_len);
9487c478bd9Sstevel@tonic-gate 	if (len == 0 || len > CRYPTO_MAX_MECH_NAME) {
9497c478bd9Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
9507c478bd9Sstevel@tonic-gate 		goto out;
9517c478bd9Sstevel@tonic-gate 	}
9527c478bd9Sstevel@tonic-gate 	mechanism_name = kmem_alloc(len, KM_SLEEP);
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 	if (copyin(STRUCT_FGETP(get_number, pn_mechanism_string),
9557c478bd9Sstevel@tonic-gate 	    mechanism_name, len) != 0) {
9567c478bd9Sstevel@tonic-gate 		kmem_free(mechanism_name, len);
9577c478bd9Sstevel@tonic-gate 		return (EFAULT);
9587c478bd9Sstevel@tonic-gate 	}
9597c478bd9Sstevel@tonic-gate 
9606f3f1c68Skrishna 	/*
9616f3f1c68Skrishna 	 * Get mechanism number from kcf. We set the load_module
9626f3f1c68Skrishna 	 * flag to false since we use only hardware providers.
9636f3f1c68Skrishna 	 */
9646f3f1c68Skrishna 	number = crypto_mech2id_common(mechanism_name, B_FALSE);
9657c478bd9Sstevel@tonic-gate 	kmem_free(mechanism_name, len);
9667c478bd9Sstevel@tonic-gate 	if (number == CRYPTO_MECH_INVALID) {
9677c478bd9Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
9687c478bd9Sstevel@tonic-gate 		goto out;
9697c478bd9Sstevel@tonic-gate 	}
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 	bcopy((char *)&number, (char *)STRUCT_FADDR(get_number,
9727c478bd9Sstevel@tonic-gate 	    pn_internal_number), sizeof (number));
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 	rv = CRYPTO_SUCCESS;
9757c478bd9Sstevel@tonic-gate out:
9767c478bd9Sstevel@tonic-gate 	STRUCT_FSET(get_number, pn_return_value, rv);
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(get_number), arg,
9797c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(get_number)) != 0) {
9807c478bd9Sstevel@tonic-gate 		return (EFAULT);
9817c478bd9Sstevel@tonic-gate 	}
9827c478bd9Sstevel@tonic-gate 	return (0);
9837c478bd9Sstevel@tonic-gate }
9847c478bd9Sstevel@tonic-gate 
9857d82f0f8SDina K Nimeh /*
9867d82f0f8SDina K Nimeh  * This ioctl returns an array of crypto_mech_name_t entries.
9877d82f0f8SDina K Nimeh  * It lists all the PKCS#11 mechanisms available in the kernel.
9887d82f0f8SDina K Nimeh  */
9897d82f0f8SDina K Nimeh /* ARGSUSED */
9907d82f0f8SDina K Nimeh static int
get_mechanism_list(dev_t dev,caddr_t arg,int mode,int * rval)9917d82f0f8SDina K Nimeh get_mechanism_list(dev_t dev, caddr_t arg, int mode, int *rval)
9927d82f0f8SDina K Nimeh {
9937d82f0f8SDina K Nimeh 	STRUCT_DECL(crypto_get_mechanism_list, get_list);
9947d82f0f8SDina K Nimeh 	crypto_mech_name_t *entries;
9957d82f0f8SDina K Nimeh 	size_t copyout_size;
9967d82f0f8SDina K Nimeh 	uint_t req_count;
9977d82f0f8SDina K Nimeh 	uint_t count;
9987d82f0f8SDina K Nimeh 	ulong_t offset;
9997d82f0f8SDina K Nimeh 	int error = 0;
10007d82f0f8SDina K Nimeh 
10017d82f0f8SDina K Nimeh 	STRUCT_INIT(get_list, mode);
10027d82f0f8SDina K Nimeh 
10037d82f0f8SDina K Nimeh 	if (copyin(arg, STRUCT_BUF(get_list), STRUCT_SIZE(get_list)) != 0) {
10047d82f0f8SDina K Nimeh 		return (EFAULT);
10057d82f0f8SDina K Nimeh 	}
10067d82f0f8SDina K Nimeh 
10077d82f0f8SDina K Nimeh 	entries = crypto_get_mech_list(&count, KM_SLEEP);
10087d82f0f8SDina K Nimeh 
10097d82f0f8SDina K Nimeh 	/* Number of entries caller thinks we have */
10107d82f0f8SDina K Nimeh 	req_count = STRUCT_FGET(get_list, ml_count);
10117d82f0f8SDina K Nimeh 
10127d82f0f8SDina K Nimeh 	STRUCT_FSET(get_list, ml_count, count);
10137d82f0f8SDina K Nimeh 	STRUCT_FSET(get_list, ml_return_value, CRYPTO_SUCCESS);
10147d82f0f8SDina K Nimeh 
10157d82f0f8SDina K Nimeh 	/* check if buffer is too small */
10167d82f0f8SDina K Nimeh 	if (count > req_count) {
10177d82f0f8SDina K Nimeh 		STRUCT_FSET(get_list, ml_return_value, CRYPTO_BUFFER_TOO_SMALL);
10187d82f0f8SDina K Nimeh 	}
10197d82f0f8SDina K Nimeh 
10207d82f0f8SDina K Nimeh 	/* copyout the first stuff */
10217d82f0f8SDina K Nimeh 	if (copyout(STRUCT_BUF(get_list), arg, STRUCT_SIZE(get_list)) != 0) {
10227d82f0f8SDina K Nimeh 		error = EFAULT;
10237d82f0f8SDina K Nimeh 	}
10247d82f0f8SDina K Nimeh 
10257d82f0f8SDina K Nimeh 	/*
10267d82f0f8SDina K Nimeh 	 * If only requesting number of entries or buffer too small or an
10277d82f0f8SDina K Nimeh 	 * error occurred, stop here
10287d82f0f8SDina K Nimeh 	 */
10297d82f0f8SDina K Nimeh 	if (req_count == 0 || count > req_count || error != 0) {
10307d82f0f8SDina K Nimeh 		goto out;
10317d82f0f8SDina K Nimeh 	}
10327d82f0f8SDina K Nimeh 
10337d82f0f8SDina K Nimeh 	copyout_size = count * sizeof (crypto_mech_name_t);
10347d82f0f8SDina K Nimeh 
10357d82f0f8SDina K Nimeh 	/* copyout entries */
10367d82f0f8SDina K Nimeh 	offset = (ulong_t)STRUCT_FADDR(get_list, ml_list);
10377d82f0f8SDina K Nimeh 	offset -= (ulong_t)STRUCT_BUF(get_list);
10387d82f0f8SDina K Nimeh 	if (copyout(entries, arg + offset, copyout_size) != 0) {
10397d82f0f8SDina K Nimeh 		error = EFAULT;
10407d82f0f8SDina K Nimeh 	}
10417d82f0f8SDina K Nimeh 
10427d82f0f8SDina K Nimeh out:
10437d82f0f8SDina K Nimeh 	crypto_free_mech_list(entries, count);
10447d82f0f8SDina K Nimeh 	return (error);
10457d82f0f8SDina K Nimeh }
10467d82f0f8SDina K Nimeh 
10477d82f0f8SDina K Nimeh /*
10487d82f0f8SDina K Nimeh  * Copyout kernel array of mech_infos to user space.
10497d82f0f8SDina K Nimeh  */
10507d82f0f8SDina K Nimeh /* ARGSUSED */
10517d82f0f8SDina K Nimeh static int
copyout_mechinfos(int mode,caddr_t out,uint_t count,crypto_mechanism_info_t * k_minfos,caddr_t u_minfos)10527d82f0f8SDina K Nimeh copyout_mechinfos(int mode, caddr_t out, uint_t count,
10537d82f0f8SDina K Nimeh     crypto_mechanism_info_t *k_minfos, caddr_t u_minfos)
10547d82f0f8SDina K Nimeh {
10557d82f0f8SDina K Nimeh 	STRUCT_DECL(crypto_mechanism_info, mi);
10567d82f0f8SDina K Nimeh 	caddr_t p;
10577d82f0f8SDina K Nimeh 	size_t len;
10587d82f0f8SDina K Nimeh 	int i;
10597d82f0f8SDina K Nimeh 
10607d82f0f8SDina K Nimeh 	if (count == 0)
10617d82f0f8SDina K Nimeh 		return (0);
10627d82f0f8SDina K Nimeh 
10637d82f0f8SDina K Nimeh 	STRUCT_INIT(mi, mode);
10647d82f0f8SDina K Nimeh 
10657d82f0f8SDina K Nimeh 	len = count * STRUCT_SIZE(mi);
10667d82f0f8SDina K Nimeh 
10677d82f0f8SDina K Nimeh 	ASSERT(u_minfos != NULL);
10687d82f0f8SDina K Nimeh 	p = u_minfos;
10697d82f0f8SDina K Nimeh 	for (i = 0; i < count; i++) {
10707d82f0f8SDina K Nimeh 		STRUCT_FSET(mi, mi_min_key_size, k_minfos[i].mi_min_key_size);
10717d82f0f8SDina K Nimeh 		STRUCT_FSET(mi, mi_max_key_size, k_minfos[i].mi_max_key_size);
10727d82f0f8SDina K Nimeh 		STRUCT_FSET(mi, mi_keysize_unit, k_minfos[i].mi_keysize_unit);
10737d82f0f8SDina K Nimeh 		STRUCT_FSET(mi, mi_usage, k_minfos[i].mi_usage);
10747d82f0f8SDina K Nimeh 		bcopy(STRUCT_BUF(mi), p, STRUCT_SIZE(mi));
10757d82f0f8SDina K Nimeh 		p += STRUCT_SIZE(mi);
10767d82f0f8SDina K Nimeh 	}
10777d82f0f8SDina K Nimeh 
10787d82f0f8SDina K Nimeh 	if (copyout(u_minfos, out, len) != 0)
10797d82f0f8SDina K Nimeh 		return (EFAULT);
10807d82f0f8SDina K Nimeh 
10817d82f0f8SDina K Nimeh 	return (0);
10827d82f0f8SDina K Nimeh }
10837d82f0f8SDina K Nimeh 
10847d82f0f8SDina K Nimeh /*
10857d82f0f8SDina K Nimeh  * This ioctl returns information for the specified mechanism.
10867d82f0f8SDina K Nimeh  */
10877d82f0f8SDina K Nimeh /* ARGSUSED */
10887d82f0f8SDina K Nimeh static int
get_all_mechanism_info(dev_t dev,caddr_t arg,int mode,int * rval)10897d82f0f8SDina K Nimeh get_all_mechanism_info(dev_t dev, caddr_t arg, int mode, int *rval)
10907d82f0f8SDina K Nimeh {
10917d82f0f8SDina K Nimeh 	STRUCT_DECL(crypto_get_all_mechanism_info, get_all_mech);
109295014fbbSDan OpenSolaris Anderson #ifdef _LP64
109395014fbbSDan OpenSolaris Anderson 	STRUCT_DECL(crypto_mechanism_info, mi);
109495014fbbSDan OpenSolaris Anderson #else
10951a010143SDina K Nimeh 	/* LINTED E_FUNC_SET_NOT_USED */
10967d82f0f8SDina K Nimeh 	STRUCT_DECL(crypto_mechanism_info, mi);
109795014fbbSDan OpenSolaris Anderson #endif
10987d82f0f8SDina K Nimeh 	crypto_mech_name_t mech_name;
10997d82f0f8SDina K Nimeh 	crypto_mech_type_t mech_type;
11007d82f0f8SDina K Nimeh 	crypto_mechanism_info_t *mech_infos = NULL;
11017d82f0f8SDina K Nimeh 	uint_t num_mech_infos = 0;
11027d82f0f8SDina K Nimeh 	uint_t req_count;
11037d82f0f8SDina K Nimeh 	caddr_t u_minfos;
11047d82f0f8SDina K Nimeh 	ulong_t offset;
11057d82f0f8SDina K Nimeh 	int error = 0;
11067d82f0f8SDina K Nimeh 	int rv;
11077d82f0f8SDina K Nimeh 
1108*c846684cSToomas Soome 	req_count = 0;
11097d82f0f8SDina K Nimeh 	STRUCT_INIT(get_all_mech, mode);
11107d82f0f8SDina K Nimeh 	STRUCT_INIT(mi, mode);
11117d82f0f8SDina K Nimeh 
11127d82f0f8SDina K Nimeh 	if (copyin(arg, STRUCT_BUF(get_all_mech),
11137d82f0f8SDina K Nimeh 	    STRUCT_SIZE(get_all_mech)) != 0) {
11147d82f0f8SDina K Nimeh 		return (EFAULT);
11157d82f0f8SDina K Nimeh 	}
11167d82f0f8SDina K Nimeh 
11177d82f0f8SDina K Nimeh 	(void) strncpy(mech_name, STRUCT_FGET(get_all_mech, mi_mechanism_name),
11187d82f0f8SDina K Nimeh 	    CRYPTO_MAX_MECH_NAME);
11197d82f0f8SDina K Nimeh 	mech_type = crypto_mech2id(mech_name);
11207d82f0f8SDina K Nimeh 
11217d82f0f8SDina K Nimeh 	if (mech_type == CRYPTO_MECH_INVALID) {
11227d82f0f8SDina K Nimeh 		rv = CRYPTO_ARGUMENTS_BAD;
11237d82f0f8SDina K Nimeh 		goto out1;
11247d82f0f8SDina K Nimeh 	}
11257d82f0f8SDina K Nimeh 
11267d82f0f8SDina K Nimeh 	rv = crypto_get_all_mech_info(mech_type, &mech_infos, &num_mech_infos,
11277d82f0f8SDina K Nimeh 	    KM_SLEEP);
11287d82f0f8SDina K Nimeh 	if (rv != CRYPTO_SUCCESS) {
11297d82f0f8SDina K Nimeh 		goto out1;
11307d82f0f8SDina K Nimeh 	}
11317d82f0f8SDina K Nimeh 	/* rv is CRYPTO_SUCCESS at this point */
11327d82f0f8SDina K Nimeh 
11337d82f0f8SDina K Nimeh 	/* Number of entries caller thinks we have */
11347d82f0f8SDina K Nimeh 	req_count = STRUCT_FGET(get_all_mech, mi_count);
11357d82f0f8SDina K Nimeh 
11367d82f0f8SDina K Nimeh 	STRUCT_FSET(get_all_mech, mi_count, num_mech_infos);
11377d82f0f8SDina K Nimeh 
11387d82f0f8SDina K Nimeh 	/* check if buffer is too small */
11397d82f0f8SDina K Nimeh 	if (num_mech_infos > req_count) {
11407d82f0f8SDina K Nimeh 		rv = CRYPTO_BUFFER_TOO_SMALL;
11417d82f0f8SDina K Nimeh 	}
11427d82f0f8SDina K Nimeh 
11437d82f0f8SDina K Nimeh out1:
11447d82f0f8SDina K Nimeh 	STRUCT_FSET(get_all_mech, mi_return_value, rv);
11457d82f0f8SDina K Nimeh 
11467d82f0f8SDina K Nimeh 	/* copy the first part */
11477d82f0f8SDina K Nimeh 	if (copyout(STRUCT_BUF(get_all_mech), arg,
11487d82f0f8SDina K Nimeh 	    STRUCT_SIZE(get_all_mech)) != 0) {
11497d82f0f8SDina K Nimeh 		error = EFAULT;
11507d82f0f8SDina K Nimeh 	}
11517d82f0f8SDina K Nimeh 
11527d82f0f8SDina K Nimeh 	/*
11537d82f0f8SDina K Nimeh 	 * If only requesting number of entries, or there are no entries,
11547d82f0f8SDina K Nimeh 	 * or rv is not CRYPTO_SUCCESS due to buffer too small or some other
11557d82f0f8SDina K Nimeh 	 * crypto error, or an error occurred with copyout, stop here
11567d82f0f8SDina K Nimeh 	 */
11577d82f0f8SDina K Nimeh 	if (req_count == 0 || num_mech_infos == 0 || rv != CRYPTO_SUCCESS ||
11587d82f0f8SDina K Nimeh 	    error != 0) {
11597d82f0f8SDina K Nimeh 		goto out2;
11607d82f0f8SDina K Nimeh 	}
11617d82f0f8SDina K Nimeh 
11627d82f0f8SDina K Nimeh 	/* copyout mech_infos */
11637d82f0f8SDina K Nimeh 	offset = (ulong_t)STRUCT_FADDR(get_all_mech, mi_list);
11647d82f0f8SDina K Nimeh 	offset -= (ulong_t)STRUCT_BUF(get_all_mech);
11657d82f0f8SDina K Nimeh 
11667d82f0f8SDina K Nimeh 	u_minfos = kmem_alloc(num_mech_infos * STRUCT_SIZE(mi), KM_SLEEP);
11677d82f0f8SDina K Nimeh 	error = copyout_mechinfos(mode, arg + offset, num_mech_infos,
11687d82f0f8SDina K Nimeh 	    mech_infos, u_minfos);
11697d82f0f8SDina K Nimeh 	kmem_free(u_minfos, num_mech_infos * STRUCT_SIZE(mi));
11707d82f0f8SDina K Nimeh out2:
11717d82f0f8SDina K Nimeh 	if (mech_infos != NULL)
11727d82f0f8SDina K Nimeh 		crypto_free_all_mech_info(mech_infos, num_mech_infos);
11737d82f0f8SDina K Nimeh 	return (error);
11747d82f0f8SDina K Nimeh }
11757d82f0f8SDina K Nimeh 
11767c478bd9Sstevel@tonic-gate /*
11777c478bd9Sstevel@tonic-gate  * Side-effects:
11787c478bd9Sstevel@tonic-gate  *  1. This routine stores provider descriptor pointers in an array
11797c478bd9Sstevel@tonic-gate  *     and increments each descriptor's reference count.  The array
11807c478bd9Sstevel@tonic-gate  *     is stored in per-minor number storage.
11817c478bd9Sstevel@tonic-gate  *  2. Destroys the old array and creates a new one every time
11827c478bd9Sstevel@tonic-gate  *     this routine is called.
11837c478bd9Sstevel@tonic-gate  */
11847c478bd9Sstevel@tonic-gate int
crypto_get_provider_list(crypto_minor_t * cm,uint_t * count,crypto_provider_entry_t ** array,boolean_t return_slot_list)11857c478bd9Sstevel@tonic-gate crypto_get_provider_list(crypto_minor_t *cm, uint_t *count,
11867c478bd9Sstevel@tonic-gate     crypto_provider_entry_t **array, boolean_t return_slot_list)
11877c478bd9Sstevel@tonic-gate {
11887c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t **provider_array;
11897c478bd9Sstevel@tonic-gate 	crypto_provider_entry_t *p = NULL;
11907c478bd9Sstevel@tonic-gate 	uint_t provider_count;
11917c478bd9Sstevel@tonic-gate 	int rval;
11927c478bd9Sstevel@tonic-gate 	int i;
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate 	/*
11957c478bd9Sstevel@tonic-gate 	 * Take snapshot of provider table returning only HW entries
11967c478bd9Sstevel@tonic-gate 	 * that are in a usable state. Also returns logical provider entries.
11977c478bd9Sstevel@tonic-gate 	 */
11987c478bd9Sstevel@tonic-gate 	rval =  kcf_get_slot_list(&provider_count, &provider_array, B_FALSE);
11997c478bd9Sstevel@tonic-gate 	if (rval != CRYPTO_SUCCESS)
12007c478bd9Sstevel@tonic-gate 		return (rval);
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 	/* allocate memory before taking cm->cm_lock */
12037c478bd9Sstevel@tonic-gate 	if (return_slot_list) {
12047c478bd9Sstevel@tonic-gate 		if (provider_count != 0) {
12057c478bd9Sstevel@tonic-gate 			p = kmem_alloc(provider_count *
12067c478bd9Sstevel@tonic-gate 			    sizeof (crypto_provider_entry_t), KM_SLEEP);
12077c478bd9Sstevel@tonic-gate 			for (i = 0; i < provider_count; i++) {
12087c478bd9Sstevel@tonic-gate 				p[i].pe_provider_id = i;
12097c478bd9Sstevel@tonic-gate 				p[i].pe_mechanism_count =
12107c478bd9Sstevel@tonic-gate 				    provider_array[i]->pd_mech_list_count;
12117c478bd9Sstevel@tonic-gate 			}
12127c478bd9Sstevel@tonic-gate 		}
12137c478bd9Sstevel@tonic-gate 		*array = p;
12147c478bd9Sstevel@tonic-gate 		*count = provider_count;
12157c478bd9Sstevel@tonic-gate 	}
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate 	/*
12187c478bd9Sstevel@tonic-gate 	 * Free existing array of providers and replace with new list.
12197c478bd9Sstevel@tonic-gate 	 */
12207c478bd9Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
12217c478bd9Sstevel@tonic-gate 	if (cm->cm_provider_array != NULL) {
12227c478bd9Sstevel@tonic-gate 		ASSERT(cm->cm_provider_count > 0);
12237c478bd9Sstevel@tonic-gate 		kcf_free_provider_tab(cm->cm_provider_count,
12247c478bd9Sstevel@tonic-gate 		    cm->cm_provider_array);
12257c478bd9Sstevel@tonic-gate 	}
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate 	cm->cm_provider_array = provider_array;
12287c478bd9Sstevel@tonic-gate 	cm->cm_provider_count = provider_count;
12297c478bd9Sstevel@tonic-gate 	mutex_exit(&cm->cm_lock);
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
12327c478bd9Sstevel@tonic-gate }
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate /*
12357c478bd9Sstevel@tonic-gate  * This ioctl returns an array of crypto_provider_entry_t entries.
12367c478bd9Sstevel@tonic-gate  * This is how consumers learn which hardware providers are available.
12377c478bd9Sstevel@tonic-gate  */
12387c478bd9Sstevel@tonic-gate /* ARGSUSED */
12397c478bd9Sstevel@tonic-gate static int
get_provider_list(dev_t dev,caddr_t arg,int mode,int * rval)12407c478bd9Sstevel@tonic-gate get_provider_list(dev_t dev, caddr_t arg, int mode, int *rval)
12417c478bd9Sstevel@tonic-gate {
12427c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_get_provider_list, get_list);
12437c478bd9Sstevel@tonic-gate 	crypto_provider_entry_t *entries;
12447c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
12457c478bd9Sstevel@tonic-gate 	size_t copyout_size;
12467c478bd9Sstevel@tonic-gate 	uint_t req_count;
12477c478bd9Sstevel@tonic-gate 	uint_t count;
12487c478bd9Sstevel@tonic-gate 	ulong_t offset;
12497c478bd9Sstevel@tonic-gate 	int rv;
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate 	STRUCT_INIT(get_list, mode);
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
12547c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "get_provider_list: failed holding minor");
12557c478bd9Sstevel@tonic-gate 		return (ENXIO);
12567c478bd9Sstevel@tonic-gate 	}
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(get_list), STRUCT_SIZE(get_list)) != 0) {
12597c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
12607c478bd9Sstevel@tonic-gate 		return (EFAULT);
12617c478bd9Sstevel@tonic-gate 	}
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 	rv = crypto_get_provider_list(cm, &count, &entries, RETURN_LIST);
12647c478bd9Sstevel@tonic-gate 	if (rv != CRYPTO_SUCCESS) {
12657c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
12667c478bd9Sstevel@tonic-gate 		STRUCT_FSET(get_list, pl_return_value, rv);
12677c478bd9Sstevel@tonic-gate 		if (copyout(STRUCT_BUF(get_list), arg,
12687c478bd9Sstevel@tonic-gate 		    STRUCT_SIZE(get_list)) != 0) {
12697c478bd9Sstevel@tonic-gate 			return (EFAULT);
12707c478bd9Sstevel@tonic-gate 		}
12717c478bd9Sstevel@tonic-gate 		return (0);
12727c478bd9Sstevel@tonic-gate 	}
12737c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate 	/* Number of slots caller thinks we have */
12767c478bd9Sstevel@tonic-gate 	req_count = STRUCT_FGET(get_list, pl_count);
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 	/* Check if only requesting number of slots */
12797c478bd9Sstevel@tonic-gate 	if (req_count == 0) {
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 		STRUCT_FSET(get_list, pl_count, count);
12827c478bd9Sstevel@tonic-gate 		STRUCT_FSET(get_list, pl_return_value, CRYPTO_SUCCESS);
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate 		crypto_free_provider_list(entries, count);
12857c478bd9Sstevel@tonic-gate 		if (copyout(STRUCT_BUF(get_list), arg,
128687fa5c53Smcpowers 		    STRUCT_SIZE(get_list)) != 0) {
12877c478bd9Sstevel@tonic-gate 			return (EFAULT);
12887c478bd9Sstevel@tonic-gate 		}
12897c478bd9Sstevel@tonic-gate 		return (0);
12907c478bd9Sstevel@tonic-gate 	}
12917c478bd9Sstevel@tonic-gate 
12927c478bd9Sstevel@tonic-gate 	/* check if buffer is too small */
12937c478bd9Sstevel@tonic-gate 	req_count = STRUCT_FGET(get_list, pl_count);
12947c478bd9Sstevel@tonic-gate 	if (count > req_count) {
12957c478bd9Sstevel@tonic-gate 		STRUCT_FSET(get_list, pl_count, count);
12967c478bd9Sstevel@tonic-gate 		STRUCT_FSET(get_list, pl_return_value, CRYPTO_BUFFER_TOO_SMALL);
12977c478bd9Sstevel@tonic-gate 		crypto_free_provider_list(entries, count);
12987c478bd9Sstevel@tonic-gate 		if (copyout(STRUCT_BUF(get_list), arg,
12997c478bd9Sstevel@tonic-gate 		    STRUCT_SIZE(get_list)) != 0) {
13007c478bd9Sstevel@tonic-gate 			return (EFAULT);
13017c478bd9Sstevel@tonic-gate 		}
13027c478bd9Sstevel@tonic-gate 		return (0);
13037c478bd9Sstevel@tonic-gate 	}
13047c478bd9Sstevel@tonic-gate 
13057c478bd9Sstevel@tonic-gate 	STRUCT_FSET(get_list, pl_count, count);
13067c478bd9Sstevel@tonic-gate 	STRUCT_FSET(get_list, pl_return_value, CRYPTO_SUCCESS);
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 	copyout_size = count * sizeof (crypto_provider_entry_t);
13097c478bd9Sstevel@tonic-gate 
13107c478bd9Sstevel@tonic-gate 	/* copyout the first stuff */
13117c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(get_list), arg, STRUCT_SIZE(get_list)) != 0) {
13127c478bd9Sstevel@tonic-gate 		crypto_free_provider_list(entries, count);
13137c478bd9Sstevel@tonic-gate 		return (EFAULT);
13147c478bd9Sstevel@tonic-gate 	}
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 	if (count == 0) {
13177c478bd9Sstevel@tonic-gate 		crypto_free_provider_list(entries, count);
13187c478bd9Sstevel@tonic-gate 		return (0);
13197c478bd9Sstevel@tonic-gate 	}
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 	/* copyout entries */
13227c478bd9Sstevel@tonic-gate 	offset = (ulong_t)STRUCT_FADDR(get_list, pl_list);
13237c478bd9Sstevel@tonic-gate 	offset -= (ulong_t)STRUCT_BUF(get_list);
13247c478bd9Sstevel@tonic-gate 	if (copyout(entries, arg + offset, copyout_size) != 0) {
13257c478bd9Sstevel@tonic-gate 		crypto_free_provider_list(entries, count);
13267c478bd9Sstevel@tonic-gate 		return (EFAULT);
13277c478bd9Sstevel@tonic-gate 	}
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate 	crypto_free_provider_list(entries, count);
13307c478bd9Sstevel@tonic-gate 	return (0);
13317c478bd9Sstevel@tonic-gate }
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate static void
ext_to_provider_data(int mode,kcf_provider_desc_t * provider,crypto_provider_ext_info_t * ei,void * out)13347c478bd9Sstevel@tonic-gate ext_to_provider_data(int mode, kcf_provider_desc_t *provider,
13357c478bd9Sstevel@tonic-gate     crypto_provider_ext_info_t *ei, void *out)
13367c478bd9Sstevel@tonic-gate {
13377c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_provider_data, pd);
13387c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_version, version);
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate 	STRUCT_INIT(pd, mode);
13417c478bd9Sstevel@tonic-gate 	STRUCT_INIT(version, mode);
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 	bcopy(provider->pd_description, STRUCT_FGET(pd, pd_prov_desc),
13447c478bd9Sstevel@tonic-gate 	    CRYPTO_PROVIDER_DESCR_MAX_LEN);
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate 	bcopy(ei->ei_label, STRUCT_FGET(pd, pd_label), CRYPTO_EXT_SIZE_LABEL);
13477c478bd9Sstevel@tonic-gate 	bcopy(ei->ei_manufacturerID, STRUCT_FGET(pd, pd_manufacturerID),
13487c478bd9Sstevel@tonic-gate 	    CRYPTO_EXT_SIZE_MANUF);
13497c478bd9Sstevel@tonic-gate 	bcopy(ei->ei_model, STRUCT_FGET(pd, pd_model), CRYPTO_EXT_SIZE_MODEL);
13507c478bd9Sstevel@tonic-gate 	bcopy(ei->ei_serial_number, STRUCT_FGET(pd, pd_serial_number),
13517c478bd9Sstevel@tonic-gate 	    CRYPTO_EXT_SIZE_SERIAL);
13527c478bd9Sstevel@tonic-gate 	/*
13537c478bd9Sstevel@tonic-gate 	 * We do not support ioctls for dual-function crypto operations yet.
13547c478bd9Sstevel@tonic-gate 	 * So, we clear this flag as it might have been set by a provider.
13557c478bd9Sstevel@tonic-gate 	 */
13567c478bd9Sstevel@tonic-gate 	ei->ei_flags &= ~CRYPTO_EXTF_DUAL_CRYPTO_OPERATIONS;
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_flags, ei->ei_flags);
13597c478bd9Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_max_session_count, ei->ei_max_session_count);
13607c478bd9Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_session_count, (int)CRYPTO_UNAVAILABLE_INFO);
13617c478bd9Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_max_rw_session_count, ei->ei_max_session_count);
13627c478bd9Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_rw_session_count, (int)CRYPTO_UNAVAILABLE_INFO);
13637c478bd9Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_max_pin_len, ei->ei_max_pin_len);
13647c478bd9Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_min_pin_len, ei->ei_min_pin_len);
13657c478bd9Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_total_public_memory, ei->ei_total_public_memory);
13667c478bd9Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_free_public_memory, ei->ei_free_public_memory);
13677c478bd9Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_total_private_memory, ei->ei_total_private_memory);
13687c478bd9Sstevel@tonic-gate 	STRUCT_FSET(pd, pd_free_private_memory, ei->ei_free_private_memory);
13697c478bd9Sstevel@tonic-gate 	STRUCT_FSET(version, cv_major, ei->ei_hardware_version.cv_major);
13707c478bd9Sstevel@tonic-gate 	STRUCT_FSET(version, cv_minor, ei->ei_hardware_version.cv_minor);
13717c478bd9Sstevel@tonic-gate 	bcopy(STRUCT_BUF(version), STRUCT_FADDR(pd, pd_hardware_version),
13727c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(version));
1373a398035bSWyllys Ingersoll 	STRUCT_FSET(version, cv_major, ei->ei_firmware_version.cv_major);
1374a398035bSWyllys Ingersoll 	STRUCT_FSET(version, cv_minor, ei->ei_firmware_version.cv_minor);
13757c478bd9Sstevel@tonic-gate 	bcopy(STRUCT_BUF(version), STRUCT_FADDR(pd, pd_firmware_version),
13767c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(version));
13777c478bd9Sstevel@tonic-gate 	bcopy(ei->ei_time, STRUCT_FGET(pd, pd_time), CRYPTO_EXT_SIZE_TIME);
13787c478bd9Sstevel@tonic-gate 	bcopy(STRUCT_BUF(pd), out, STRUCT_SIZE(pd));
13797c478bd9Sstevel@tonic-gate }
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate /*
13827c478bd9Sstevel@tonic-gate  * Utility routine to construct a crypto_provider_ext_info structure. Some
13837c478bd9Sstevel@tonic-gate  * of the fields are constructed from information in the provider structure.
13847c478bd9Sstevel@tonic-gate  * The rest of the fields have default values. We need to do this for
13857c478bd9Sstevel@tonic-gate  * providers which do not support crypto_provider_management_ops routines.
13867c478bd9Sstevel@tonic-gate  */
13877c478bd9Sstevel@tonic-gate static void
fabricate_ext_info(kcf_provider_desc_t * provider,crypto_provider_ext_info_t * ei)13887c478bd9Sstevel@tonic-gate fabricate_ext_info(kcf_provider_desc_t *provider,
13897c478bd9Sstevel@tonic-gate     crypto_provider_ext_info_t *ei)
13907c478bd9Sstevel@tonic-gate {
13917c478bd9Sstevel@tonic-gate 	/* empty label */
13927c478bd9Sstevel@tonic-gate 	(void) memset(ei->ei_label, ' ', CRYPTO_EXT_SIZE_LABEL);
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate 	(void) memset(ei->ei_manufacturerID, ' ', CRYPTO_EXT_SIZE_MANUF);
13957c478bd9Sstevel@tonic-gate 	(void) strncpy((char *)ei->ei_manufacturerID, "Unknown", 7);
13967c478bd9Sstevel@tonic-gate 
13977c478bd9Sstevel@tonic-gate 	(void) memset(ei->ei_model, ' ', CRYPTO_EXT_SIZE_MODEL);
13987c478bd9Sstevel@tonic-gate 	(void) strncpy((char *)ei->ei_model, "Unknown", 7);
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 	(void) memset(ei->ei_serial_number, ' ', CRYPTO_EXT_SIZE_SERIAL);
14017c478bd9Sstevel@tonic-gate 	(void) strncpy((char *)ei->ei_serial_number, "Unknown", 7);
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate 	if (KCF_PROV_RANDOM_OPS(provider) != NULL)
14047c478bd9Sstevel@tonic-gate 		ei->ei_flags |= CRYPTO_EXTF_RNG;
14057c478bd9Sstevel@tonic-gate 	if (KCF_PROV_DUAL_OPS(provider) != NULL)
14067c478bd9Sstevel@tonic-gate 		ei->ei_flags |= CRYPTO_EXTF_DUAL_CRYPTO_OPERATIONS;
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate 	ei->ei_max_session_count = CRYPTO_UNAVAILABLE_INFO;
14097c478bd9Sstevel@tonic-gate 	ei->ei_max_pin_len = 0;
14107c478bd9Sstevel@tonic-gate 	ei->ei_min_pin_len = 0;
14117c478bd9Sstevel@tonic-gate 	ei->ei_total_public_memory = CRYPTO_UNAVAILABLE_INFO;
14127c478bd9Sstevel@tonic-gate 	ei->ei_free_public_memory = CRYPTO_UNAVAILABLE_INFO;
14137c478bd9Sstevel@tonic-gate 	ei->ei_total_private_memory = CRYPTO_UNAVAILABLE_INFO;
14147c478bd9Sstevel@tonic-gate 	ei->ei_free_private_memory = CRYPTO_UNAVAILABLE_INFO;
14157c478bd9Sstevel@tonic-gate 	ei->ei_hardware_version.cv_major = 1;
14167c478bd9Sstevel@tonic-gate 	ei->ei_hardware_version.cv_minor = 0;
14177c478bd9Sstevel@tonic-gate 	ei->ei_firmware_version.cv_major = 1;
14187c478bd9Sstevel@tonic-gate 	ei->ei_firmware_version.cv_minor = 0;
14197c478bd9Sstevel@tonic-gate }
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate /* ARGSUSED */
14227c478bd9Sstevel@tonic-gate static int
get_provider_info(dev_t dev,caddr_t arg,int mode,int * rval)14237c478bd9Sstevel@tonic-gate get_provider_info(dev_t dev, caddr_t arg, int mode, int *rval)
14247c478bd9Sstevel@tonic-gate {
14257c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_get_provider_info, get_info);
14267c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
14277c478bd9Sstevel@tonic-gate 	crypto_provider_id_t provider_id;
14287c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t *provider, *real_provider;
14297c478bd9Sstevel@tonic-gate 	crypto_provider_ext_info_t *ext_info = NULL;
14307c478bd9Sstevel@tonic-gate 	size_t need;
14317c478bd9Sstevel@tonic-gate 	int error = 0;
14327c478bd9Sstevel@tonic-gate 	int rv;
14337c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 	STRUCT_INIT(get_info, mode);
14367c478bd9Sstevel@tonic-gate 
14377c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
14387c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "get_provider_info: failed holding minor");
14397c478bd9Sstevel@tonic-gate 		return (ENXIO);
14407c478bd9Sstevel@tonic-gate 	}
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(get_info), STRUCT_SIZE(get_info)) != 0) {
14437c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
14447c478bd9Sstevel@tonic-gate 		return (EFAULT);
14457c478bd9Sstevel@tonic-gate 	}
14467c478bd9Sstevel@tonic-gate 
14477c478bd9Sstevel@tonic-gate 	need = sizeof (crypto_provider_ext_info_t);
1448c1a9a9c3Skrishna 	if ((rv = crypto_buffer_check(need)) != CRYPTO_SUCCESS) {
14497c478bd9Sstevel@tonic-gate 		need = 0;
14507c478bd9Sstevel@tonic-gate 		goto release_minor;
14517c478bd9Sstevel@tonic-gate 	}
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 	/* initialize provider_array */
14547c478bd9Sstevel@tonic-gate 	if (cm->cm_provider_array == NULL) {
14557c478bd9Sstevel@tonic-gate 		rv = crypto_get_provider_list(cm, NULL, NULL, DONT_RETURN_LIST);
14567c478bd9Sstevel@tonic-gate 		if (rv != CRYPTO_SUCCESS) {
14577c478bd9Sstevel@tonic-gate 			goto release_minor;
14587c478bd9Sstevel@tonic-gate 		}
14597c478bd9Sstevel@tonic-gate 	}
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 	ext_info = kmem_zalloc(need, KM_SLEEP);
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate 	provider_id = STRUCT_FGET(get_info, gi_provider_id);
14647c478bd9Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
14657c478bd9Sstevel@tonic-gate 	/* index must be less than count of providers */
14667c478bd9Sstevel@tonic-gate 	if (provider_id >= cm->cm_provider_count) {
14677c478bd9Sstevel@tonic-gate 		mutex_exit(&cm->cm_lock);
14687c478bd9Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
14697c478bd9Sstevel@tonic-gate 		goto release_minor;
14707c478bd9Sstevel@tonic-gate 	}
14717c478bd9Sstevel@tonic-gate 
14727c478bd9Sstevel@tonic-gate 	ASSERT(cm->cm_provider_array != NULL);
14737c478bd9Sstevel@tonic-gate 	provider = cm->cm_provider_array[provider_id];
14747c478bd9Sstevel@tonic-gate 	KCF_PROV_REFHOLD(provider);
14757c478bd9Sstevel@tonic-gate 	mutex_exit(&cm->cm_lock);
14767c478bd9Sstevel@tonic-gate 
14777c478bd9Sstevel@tonic-gate 	(void) kcf_get_hardware_provider_nomech(
14787c478bd9Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(provider_ops), CRYPTO_PROVIDER_OFFSET(ext_info),
14799b009fc1SValerie Bubb Fenwick 	    provider, &real_provider);
14807c478bd9Sstevel@tonic-gate 
14817c478bd9Sstevel@tonic-gate 	if (real_provider != NULL) {
14827c478bd9Sstevel@tonic-gate 		ASSERT(real_provider == provider ||
14837c478bd9Sstevel@tonic-gate 		    provider->pd_prov_type == CRYPTO_LOGICAL_PROVIDER);
14847c478bd9Sstevel@tonic-gate 		KCF_WRAP_PROVMGMT_OPS_PARAMS(&params, KCF_OP_MGMT_EXTINFO,
14857c478bd9Sstevel@tonic-gate 		    0, NULL, 0, NULL, 0, NULL, ext_info, provider);
14867c478bd9Sstevel@tonic-gate 		rv = kcf_submit_request(real_provider, NULL, NULL, &params,
14877c478bd9Sstevel@tonic-gate 		    B_FALSE);
14887c478bd9Sstevel@tonic-gate 		ASSERT(rv != CRYPTO_NOT_SUPPORTED);
1489894b2776Smcpowers 		KCF_PROV_REFRELE(real_provider);
14907c478bd9Sstevel@tonic-gate 	} else {
14917c478bd9Sstevel@tonic-gate 		/* do the best we can */
14927c478bd9Sstevel@tonic-gate 		fabricate_ext_info(provider, ext_info);
14937c478bd9Sstevel@tonic-gate 		rv = CRYPTO_SUCCESS;
14947c478bd9Sstevel@tonic-gate 	}
14957c478bd9Sstevel@tonic-gate 	KCF_PROV_REFRELE(provider);
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
14987c478bd9Sstevel@tonic-gate 		ext_to_provider_data(mode, provider, ext_info,
14997c478bd9Sstevel@tonic-gate 		    STRUCT_FADDR(get_info, gi_provider_data));
15007c478bd9Sstevel@tonic-gate 	}
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate release_minor:
15032d794da1Skrishna 	CRYPTO_DECREMENT_RCTL(need);
15047c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
15057c478bd9Sstevel@tonic-gate 
15067c478bd9Sstevel@tonic-gate 	if (ext_info != NULL)
15077c478bd9Sstevel@tonic-gate 		kmem_free(ext_info, sizeof (crypto_provider_ext_info_t));
15087c478bd9Sstevel@tonic-gate 
15097c478bd9Sstevel@tonic-gate 	if (error != 0)
15107c478bd9Sstevel@tonic-gate 		return (error);
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate 	STRUCT_FSET(get_info, gi_return_value, rv);
15137c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(get_info), arg, STRUCT_SIZE(get_info)) != 0) {
15147c478bd9Sstevel@tonic-gate 		return (EFAULT);
15157c478bd9Sstevel@tonic-gate 	}
15167c478bd9Sstevel@tonic-gate 	return (0);
15177c478bd9Sstevel@tonic-gate }
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate /*
15207c478bd9Sstevel@tonic-gate  * This ioctl returns an array of crypto_mech_name_t entries.
15217c478bd9Sstevel@tonic-gate  * This is how consumers learn which mechanisms are permitted
15227c478bd9Sstevel@tonic-gate  * by a provider.
15237c478bd9Sstevel@tonic-gate  */
15247c478bd9Sstevel@tonic-gate /* ARGSUSED */
15257c478bd9Sstevel@tonic-gate static int
get_provider_mechanisms(dev_t dev,caddr_t arg,int mode,int * rval)15267c478bd9Sstevel@tonic-gate get_provider_mechanisms(dev_t dev, caddr_t arg, int mode, int *rval)
15277c478bd9Sstevel@tonic-gate {
15287c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_get_provider_mechanisms, get_mechanisms);
15297c478bd9Sstevel@tonic-gate 	crypto_mech_name_t *entries;
15307c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
15317c478bd9Sstevel@tonic-gate 	size_t copyout_size;
15327c478bd9Sstevel@tonic-gate 	uint_t req_count;
15337c478bd9Sstevel@tonic-gate 	uint_t count;
15347c478bd9Sstevel@tonic-gate 	ulong_t offset;
15357c478bd9Sstevel@tonic-gate 	int err;
15367c478bd9Sstevel@tonic-gate 
15377c478bd9Sstevel@tonic-gate 	STRUCT_INIT(get_mechanisms, mode);
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
15407c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
15417c478bd9Sstevel@tonic-gate 		    "get_provider_mechanisms: failed holding minor");
15427c478bd9Sstevel@tonic-gate 		return (ENXIO);
15437c478bd9Sstevel@tonic-gate 	}
15447c478bd9Sstevel@tonic-gate 
15457c478bd9Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(get_mechanisms),
15467c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(get_mechanisms)) != 0) {
15477c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
15487c478bd9Sstevel@tonic-gate 		return (EFAULT);
15497c478bd9Sstevel@tonic-gate 	}
15507c478bd9Sstevel@tonic-gate 
15517c478bd9Sstevel@tonic-gate 	/* get array of mechanisms from the core module */
15527c478bd9Sstevel@tonic-gate 	if ((err = crypto_get_provider_mechanisms(cm,
15537c478bd9Sstevel@tonic-gate 	    STRUCT_FGET(get_mechanisms, pm_provider_id),
15547c478bd9Sstevel@tonic-gate 	    &count, &entries)) != 0) {
15557c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
15567c478bd9Sstevel@tonic-gate 		STRUCT_FSET(get_mechanisms, pm_return_value, err);
15577c478bd9Sstevel@tonic-gate 		if (copyout(STRUCT_BUF(get_mechanisms), arg,
15587c478bd9Sstevel@tonic-gate 		    STRUCT_SIZE(get_mechanisms)) != 0) {
15597c478bd9Sstevel@tonic-gate 			return (EFAULT);
15607c478bd9Sstevel@tonic-gate 		}
15617c478bd9Sstevel@tonic-gate 		return (0);
15627c478bd9Sstevel@tonic-gate 	}
15637c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
15647c478bd9Sstevel@tonic-gate 	/* Number of mechs caller thinks we have */
15657c478bd9Sstevel@tonic-gate 	req_count = STRUCT_FGET(get_mechanisms, pm_count);
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 	/* Check if caller is just requesting a count of mechanisms */
15687c478bd9Sstevel@tonic-gate 	if (req_count == 0) {
15697c478bd9Sstevel@tonic-gate 		STRUCT_FSET(get_mechanisms, pm_count, count);
15707c478bd9Sstevel@tonic-gate 		STRUCT_FSET(get_mechanisms, pm_return_value, CRYPTO_SUCCESS);
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate 		crypto_free_mech_list(entries, count);
15737c478bd9Sstevel@tonic-gate 		if (copyout(STRUCT_BUF(get_mechanisms), arg,
15747c478bd9Sstevel@tonic-gate 		    STRUCT_SIZE(get_mechanisms)) != 0) {
15757c478bd9Sstevel@tonic-gate 			return (EFAULT);
15767c478bd9Sstevel@tonic-gate 		}
15777c478bd9Sstevel@tonic-gate 		return (0);
15787c478bd9Sstevel@tonic-gate 	}
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate 	/* check if buffer is too small */
15817c478bd9Sstevel@tonic-gate 	if (count > req_count) {
15827c478bd9Sstevel@tonic-gate 		STRUCT_FSET(get_mechanisms, pm_count, count);
15837c478bd9Sstevel@tonic-gate 		STRUCT_FSET(get_mechanisms, pm_return_value,
15847c478bd9Sstevel@tonic-gate 		    CRYPTO_BUFFER_TOO_SMALL);
15857c478bd9Sstevel@tonic-gate 		crypto_free_mech_list(entries, count);
15867c478bd9Sstevel@tonic-gate 		if (copyout(STRUCT_BUF(get_mechanisms), arg,
15877c478bd9Sstevel@tonic-gate 		    STRUCT_SIZE(get_mechanisms)) != 0) {
15887c478bd9Sstevel@tonic-gate 			return (EFAULT);
15897c478bd9Sstevel@tonic-gate 		}
15907c478bd9Sstevel@tonic-gate 		return (0);
15917c478bd9Sstevel@tonic-gate 	}
15927c478bd9Sstevel@tonic-gate 
15937c478bd9Sstevel@tonic-gate 	STRUCT_FSET(get_mechanisms, pm_count, count);
15947c478bd9Sstevel@tonic-gate 	STRUCT_FSET(get_mechanisms, pm_return_value, CRYPTO_SUCCESS);
15957c478bd9Sstevel@tonic-gate 
15967c478bd9Sstevel@tonic-gate 	copyout_size = count * sizeof (crypto_mech_name_t);
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate 	/* copyout the first stuff */
15997c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(get_mechanisms), arg,
16007c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(get_mechanisms)) != 0) {
16017c478bd9Sstevel@tonic-gate 		crypto_free_mech_list(entries, count);
16027c478bd9Sstevel@tonic-gate 		return (EFAULT);
16037c478bd9Sstevel@tonic-gate 	}
16047c478bd9Sstevel@tonic-gate 
16057c478bd9Sstevel@tonic-gate 	if (count == 0) {
16067c478bd9Sstevel@tonic-gate 		return (0);
16077c478bd9Sstevel@tonic-gate 	}
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 	/* copyout entries */
16107c478bd9Sstevel@tonic-gate 	offset = (ulong_t)STRUCT_FADDR(get_mechanisms, pm_list);
16117c478bd9Sstevel@tonic-gate 	offset -= (ulong_t)STRUCT_BUF(get_mechanisms);
16127c478bd9Sstevel@tonic-gate 	if (copyout(entries, arg + offset, copyout_size) != 0) {
16137c478bd9Sstevel@tonic-gate 		crypto_free_mech_list(entries, count);
16147c478bd9Sstevel@tonic-gate 		return (EFAULT);
16157c478bd9Sstevel@tonic-gate 	}
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate 	crypto_free_mech_list(entries, count);
16187c478bd9Sstevel@tonic-gate 	return (0);
16197c478bd9Sstevel@tonic-gate }
16207c478bd9Sstevel@tonic-gate 
16217c478bd9Sstevel@tonic-gate /*
16227c478bd9Sstevel@tonic-gate  * This ioctl returns information about a provider's mechanism.
16237c478bd9Sstevel@tonic-gate  */
16247c478bd9Sstevel@tonic-gate /* ARGSUSED */
16257c478bd9Sstevel@tonic-gate static int
get_provider_mechanism_info(dev_t dev,caddr_t arg,int mode,int * rval)16267c478bd9Sstevel@tonic-gate get_provider_mechanism_info(dev_t dev, caddr_t arg, int mode, int *rval)
16277c478bd9Sstevel@tonic-gate {
16287c478bd9Sstevel@tonic-gate 	crypto_get_provider_mechanism_info_t mechanism_info;
16297c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
16307c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
16317c478bd9Sstevel@tonic-gate 	crypto_mech_info_t *mi = NULL;
16327c478bd9Sstevel@tonic-gate 	int rv = CRYPTO_SUCCESS;
16337c478bd9Sstevel@tonic-gate 	int i;
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
16367c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
16377c478bd9Sstevel@tonic-gate 		    "get_provider_mechanism_info: failed holding minor");
16387c478bd9Sstevel@tonic-gate 		return (ENXIO);
16397c478bd9Sstevel@tonic-gate 	}
16407c478bd9Sstevel@tonic-gate 
16417c478bd9Sstevel@tonic-gate 	if (copyin(arg, &mechanism_info, sizeof (mechanism_info)) != 0) {
16427c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
16437c478bd9Sstevel@tonic-gate 		return (EFAULT);
16447c478bd9Sstevel@tonic-gate 	}
16457c478bd9Sstevel@tonic-gate 
16467c478bd9Sstevel@tonic-gate 	/* initialize provider table */
16477c478bd9Sstevel@tonic-gate 	if (cm->cm_provider_array == NULL) {
16487c478bd9Sstevel@tonic-gate 		rv = crypto_get_provider_list(cm, NULL, NULL, DONT_RETURN_LIST);
16497c478bd9Sstevel@tonic-gate 		if (rv != CRYPTO_SUCCESS) {
16507c478bd9Sstevel@tonic-gate 			mutex_enter(&cm->cm_lock);
16517c478bd9Sstevel@tonic-gate 			goto fail;
16527c478bd9Sstevel@tonic-gate 		}
16537c478bd9Sstevel@tonic-gate 	}
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 	/*
16567c478bd9Sstevel@tonic-gate 	 * Provider ID must be less than the count of providers
16577c478bd9Sstevel@tonic-gate 	 * obtained by calling get_provider_list().
16587c478bd9Sstevel@tonic-gate 	 */
16597c478bd9Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
16607c478bd9Sstevel@tonic-gate 	if (mechanism_info.mi_provider_id >= cm->cm_provider_count) {
16617c478bd9Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
16627c478bd9Sstevel@tonic-gate 		goto fail;
16637c478bd9Sstevel@tonic-gate 	}
16647c478bd9Sstevel@tonic-gate 
16657c478bd9Sstevel@tonic-gate 	pd = cm->cm_provider_array[mechanism_info.mi_provider_id];
16667c478bd9Sstevel@tonic-gate 
1667c630c2c5SVladimir Kotal 	/* First check if the provider supports the mechanism. */
16687c478bd9Sstevel@tonic-gate 	for (i = 0; i < pd->pd_mech_list_count; i++) {
16697c478bd9Sstevel@tonic-gate 		if (strncmp(pd->pd_mechanisms[i].cm_mech_name,
16707c478bd9Sstevel@tonic-gate 		    mechanism_info.mi_mechanism_name,
16717c478bd9Sstevel@tonic-gate 		    CRYPTO_MAX_MECH_NAME) == 0) {
16727c478bd9Sstevel@tonic-gate 			mi = &pd->pd_mechanisms[i];
1673c630c2c5SVladimir Kotal 			break;
16747c478bd9Sstevel@tonic-gate 		}
16757c478bd9Sstevel@tonic-gate 	}
16767c478bd9Sstevel@tonic-gate 
16777c478bd9Sstevel@tonic-gate 	if (mi == NULL) {
16787c478bd9Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
16797c478bd9Sstevel@tonic-gate 		goto fail;
16807c478bd9Sstevel@tonic-gate 	}
16817c478bd9Sstevel@tonic-gate 
1682c630c2c5SVladimir Kotal 	/* Now check if the mechanism is enabled for the provider. */
1683c630c2c5SVladimir Kotal 	if (is_mech_disabled(pd, mechanism_info.mi_mechanism_name)) {
1684c630c2c5SVladimir Kotal 		rv = CRYPTO_MECHANISM_INVALID;
1685c630c2c5SVladimir Kotal 		goto fail;
1686c630c2c5SVladimir Kotal 	}
1687c630c2c5SVladimir Kotal 
16887c478bd9Sstevel@tonic-gate 	mechanism_info.mi_min_key_size = mi->cm_min_key_length;
16897c478bd9Sstevel@tonic-gate 	mechanism_info.mi_max_key_size = mi->cm_max_key_length;
16907c478bd9Sstevel@tonic-gate 	mechanism_info.mi_flags = mi->cm_func_group_mask;
16917c478bd9Sstevel@tonic-gate 
16927c478bd9Sstevel@tonic-gate fail:
16937c478bd9Sstevel@tonic-gate 	mutex_exit(&cm->cm_lock);
16947c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
16957c478bd9Sstevel@tonic-gate 	mechanism_info.mi_return_value = rv;
16967c478bd9Sstevel@tonic-gate 	if (copyout(&mechanism_info, arg, sizeof (mechanism_info)) != 0) {
16977c478bd9Sstevel@tonic-gate 		return (EFAULT);
16987c478bd9Sstevel@tonic-gate 	}
16997c478bd9Sstevel@tonic-gate 
17007c478bd9Sstevel@tonic-gate 	return (0);
17017c478bd9Sstevel@tonic-gate }
17027c478bd9Sstevel@tonic-gate 
17037c478bd9Sstevel@tonic-gate /*
17047c478bd9Sstevel@tonic-gate  * Every open of /dev/crypto multiplexes all PKCS#11 sessions across
17057c478bd9Sstevel@tonic-gate  * a single session to each provider. Calls to open and close session
17067c478bd9Sstevel@tonic-gate  * are not made to providers that do not support sessions. For these
17077c478bd9Sstevel@tonic-gate  * providers, a session number of 0 is passed during subsequent operations,
17087c478bd9Sstevel@tonic-gate  * and it is ignored by the provider.
17097c478bd9Sstevel@tonic-gate  */
17107c478bd9Sstevel@tonic-gate static int
crypto_get_provider_session(crypto_minor_t * cm,crypto_provider_id_t provider_index,crypto_provider_session_t ** output_ps)17117c478bd9Sstevel@tonic-gate crypto_get_provider_session(crypto_minor_t *cm,
17127c478bd9Sstevel@tonic-gate     crypto_provider_id_t provider_index, crypto_provider_session_t **output_ps)
17137c478bd9Sstevel@tonic-gate {
17147c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t *pd, *real_provider;
17157c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
1716cd964fceSMatt Barden 	crypto_provider_session_t *ps;
17177c478bd9Sstevel@tonic-gate 	crypto_session_id_t provider_session_id = 0;
17187c478bd9Sstevel@tonic-gate 	int rv;
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cm->cm_lock));
17217c478bd9Sstevel@tonic-gate 
17227c478bd9Sstevel@tonic-gate 	/* pd may be a logical provider */
17237c478bd9Sstevel@tonic-gate 	pd = cm->cm_provider_array[provider_index];
17247c478bd9Sstevel@tonic-gate 
17257c478bd9Sstevel@tonic-gate again:
17267c478bd9Sstevel@tonic-gate 	/*
17277c478bd9Sstevel@tonic-gate 	 * Check if there is already a session to the provider.
17287c478bd9Sstevel@tonic-gate 	 * Sessions may be to a logical provider or a real provider.
17297c478bd9Sstevel@tonic-gate 	 */
17307c478bd9Sstevel@tonic-gate 	for (ps = cm->cm_provider_session; ps != NULL; ps = ps->ps_next) {
17317c478bd9Sstevel@tonic-gate 		if (ps->ps_provider == pd)
17327c478bd9Sstevel@tonic-gate 			break;
17337c478bd9Sstevel@tonic-gate 	}
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate 	/* found existing session */
17367c478bd9Sstevel@tonic-gate 	if (ps != NULL) {
17377c478bd9Sstevel@tonic-gate 		ps->ps_refcnt++;
17387c478bd9Sstevel@tonic-gate 		*output_ps = ps;
17397c478bd9Sstevel@tonic-gate 		return (CRYPTO_SUCCESS);
17407c478bd9Sstevel@tonic-gate 	}
17417c478bd9Sstevel@tonic-gate 	mutex_exit(&cm->cm_lock);
17427c478bd9Sstevel@tonic-gate 
17437c478bd9Sstevel@tonic-gate 	/* find a hardware provider that supports session ops */
17447c478bd9Sstevel@tonic-gate 	(void) kcf_get_hardware_provider_nomech(CRYPTO_OPS_OFFSET(session_ops),
17459b009fc1SValerie Bubb Fenwick 	    CRYPTO_SESSION_OFFSET(session_open), pd, &real_provider);
17467c478bd9Sstevel@tonic-gate 
17477c478bd9Sstevel@tonic-gate 	if (real_provider != NULL) {
17487c478bd9Sstevel@tonic-gate 		ASSERT(real_provider == pd ||
17497c478bd9Sstevel@tonic-gate 		    pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER);
17507c478bd9Sstevel@tonic-gate 		/* open session to provider */
17517c478bd9Sstevel@tonic-gate 		KCF_WRAP_SESSION_OPS_PARAMS(&params, KCF_OP_SESSION_OPEN,
17527c478bd9Sstevel@tonic-gate 		    &provider_session_id, 0, CRYPTO_USER, NULL, 0, pd);
17537c478bd9Sstevel@tonic-gate 		rv = kcf_submit_request(real_provider, NULL, NULL, &params,
17547c478bd9Sstevel@tonic-gate 		    B_FALSE);
17557c478bd9Sstevel@tonic-gate 		if (rv != CRYPTO_SUCCESS) {
17567c478bd9Sstevel@tonic-gate 			mutex_enter(&cm->cm_lock);
1757894b2776Smcpowers 			KCF_PROV_REFRELE(real_provider);
17587c478bd9Sstevel@tonic-gate 			return (rv);
17597c478bd9Sstevel@tonic-gate 		}
17607c478bd9Sstevel@tonic-gate 	}
17617c478bd9Sstevel@tonic-gate 
17627c478bd9Sstevel@tonic-gate 	/*
17637c478bd9Sstevel@tonic-gate 	 * Check if someone opened a session to the provider
17647c478bd9Sstevel@tonic-gate 	 * while we dropped the lock.
17657c478bd9Sstevel@tonic-gate 	 */
17667c478bd9Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
17677c478bd9Sstevel@tonic-gate 	for (ps = cm->cm_provider_session; ps != NULL; ps = ps->ps_next) {
17687c478bd9Sstevel@tonic-gate 		if (ps->ps_provider == pd) {
17697c478bd9Sstevel@tonic-gate 			mutex_exit(&cm->cm_lock);
17707c478bd9Sstevel@tonic-gate 			if (real_provider != NULL) {
17717c478bd9Sstevel@tonic-gate 				KCF_WRAP_SESSION_OPS_PARAMS(&params,
17727c478bd9Sstevel@tonic-gate 				    KCF_OP_SESSION_CLOSE, NULL,
17737c478bd9Sstevel@tonic-gate 				    provider_session_id, CRYPTO_USER, NULL, 0,
17747c478bd9Sstevel@tonic-gate 				    pd);
17757c478bd9Sstevel@tonic-gate 				(void) kcf_submit_request(real_provider, NULL,
17767c478bd9Sstevel@tonic-gate 				    NULL, &params, B_FALSE);
1777894b2776Smcpowers 				KCF_PROV_REFRELE(real_provider);
17787c478bd9Sstevel@tonic-gate 			}
17797c478bd9Sstevel@tonic-gate 			mutex_enter(&cm->cm_lock);
17807c478bd9Sstevel@tonic-gate 			goto again;
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate 		}
17837c478bd9Sstevel@tonic-gate 	}
17847c478bd9Sstevel@tonic-gate 
1785cd964fceSMatt Barden 	return (crypto_create_provider_session(cm, pd, provider_session_id,
1786cd964fceSMatt Barden 	    output_ps, real_provider));
1787cd964fceSMatt Barden }
1788cd964fceSMatt Barden 
1789cd964fceSMatt Barden static int
crypto_create_provider_session(crypto_minor_t * cm,kcf_provider_desc_t * pd,crypto_session_id_t sid,crypto_provider_session_t ** out_ps,kcf_provider_desc_t * real)1790cd964fceSMatt Barden crypto_create_provider_session(crypto_minor_t *cm, kcf_provider_desc_t *pd,
1791cd964fceSMatt Barden     crypto_session_id_t sid, crypto_provider_session_t **out_ps,
1792cd964fceSMatt Barden     kcf_provider_desc_t *real)
1793cd964fceSMatt Barden {
1794cd964fceSMatt Barden 	crypto_provider_session_t *ps;
1795cd964fceSMatt Barden 
1796cd964fceSMatt Barden 	/* allocate crypto_provider_session structure */
1797cd964fceSMatt Barden 	ps = kmem_zalloc(sizeof (crypto_provider_session_t), KM_SLEEP);
1798cd964fceSMatt Barden 
17997c478bd9Sstevel@tonic-gate 	/* increment refcnt and attach to crypto_minor structure */
1800cd964fceSMatt Barden 	ps->ps_session = sid;
1801cd964fceSMatt Barden 	ps->ps_refcnt = 1;
18027c478bd9Sstevel@tonic-gate 	KCF_PROV_REFHOLD(pd);
1803cd964fceSMatt Barden 	ps->ps_provider = pd;
1804cd964fceSMatt Barden 	if (real != NULL) {
1805cd964fceSMatt Barden 		ps->ps_real_provider = real;
18067c478bd9Sstevel@tonic-gate 	}
1807cd964fceSMatt Barden 	ps->ps_next = cm->cm_provider_session;
1808cd964fceSMatt Barden 	cm->cm_provider_session = ps;
18097c478bd9Sstevel@tonic-gate 
1810cd964fceSMatt Barden 	*out_ps = ps;
18117c478bd9Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
18127c478bd9Sstevel@tonic-gate }
18137c478bd9Sstevel@tonic-gate 
18147c478bd9Sstevel@tonic-gate /*
18157c478bd9Sstevel@tonic-gate  * Release a provider session.
18167c478bd9Sstevel@tonic-gate  * If the reference count goes to zero, then close the session
18177c478bd9Sstevel@tonic-gate  * to the provider.
18187c478bd9Sstevel@tonic-gate  */
18197c478bd9Sstevel@tonic-gate static void
crypto_release_provider_session(crypto_minor_t * cm,crypto_provider_session_t * provider_session)18207c478bd9Sstevel@tonic-gate crypto_release_provider_session(crypto_minor_t *cm,
18217c478bd9Sstevel@tonic-gate     crypto_provider_session_t *provider_session)
18227c478bd9Sstevel@tonic-gate {
18237c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
18247c478bd9Sstevel@tonic-gate 	crypto_provider_session_t *ps = NULL, **prev;
18257c478bd9Sstevel@tonic-gate 
18267c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cm->cm_lock));
18277c478bd9Sstevel@tonic-gate 
18287c478bd9Sstevel@tonic-gate 	/* verify that provider_session is valid */
18297c478bd9Sstevel@tonic-gate 	for (ps = cm->cm_provider_session, prev = &cm->cm_provider_session;
18307c478bd9Sstevel@tonic-gate 	    ps != NULL; prev = &ps->ps_next, ps = ps->ps_next) {
18317c478bd9Sstevel@tonic-gate 		if (ps == provider_session) {
18327c478bd9Sstevel@tonic-gate 			break;
18337c478bd9Sstevel@tonic-gate 		}
18347c478bd9Sstevel@tonic-gate 	}
18357c478bd9Sstevel@tonic-gate 
18367c478bd9Sstevel@tonic-gate 	if (ps == NULL)
18377c478bd9Sstevel@tonic-gate 		return;
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate 	ps->ps_refcnt--;
18407c478bd9Sstevel@tonic-gate 
18417c478bd9Sstevel@tonic-gate 	if (ps->ps_refcnt > 0)
18427c478bd9Sstevel@tonic-gate 		return;
18437c478bd9Sstevel@tonic-gate 
18447c478bd9Sstevel@tonic-gate 	if (ps->ps_real_provider != NULL) {
18457c478bd9Sstevel@tonic-gate 		/* close session with provider */
18467c478bd9Sstevel@tonic-gate 		KCF_WRAP_SESSION_OPS_PARAMS(&params, KCF_OP_SESSION_CLOSE, NULL,
18477c478bd9Sstevel@tonic-gate 		    ps->ps_session, CRYPTO_USER, NULL, 0, ps->ps_provider);
18487c478bd9Sstevel@tonic-gate 		(void) kcf_submit_request(ps->ps_real_provider,
18497c478bd9Sstevel@tonic-gate 		    NULL, NULL, &params, B_FALSE);
18507c478bd9Sstevel@tonic-gate 		KCF_PROV_REFRELE(ps->ps_real_provider);
18517c478bd9Sstevel@tonic-gate 	}
18527c478bd9Sstevel@tonic-gate 	KCF_PROV_REFRELE(ps->ps_provider);
18537c478bd9Sstevel@tonic-gate 	*prev = ps->ps_next;
18547c478bd9Sstevel@tonic-gate 	kmem_free(ps, sizeof (*ps));
18557c478bd9Sstevel@tonic-gate }
18567c478bd9Sstevel@tonic-gate 
18577c478bd9Sstevel@tonic-gate static int
grow_session_table(crypto_minor_t * cm)18587c478bd9Sstevel@tonic-gate grow_session_table(crypto_minor_t *cm)
18597c478bd9Sstevel@tonic-gate {
18607c478bd9Sstevel@tonic-gate 	crypto_session_data_t **session_table;
18617c478bd9Sstevel@tonic-gate 	crypto_session_data_t **new;
18627c478bd9Sstevel@tonic-gate 	uint_t session_table_count;
18637c478bd9Sstevel@tonic-gate 	uint_t need;
18647c478bd9Sstevel@tonic-gate 	size_t current_allocation;
18657c478bd9Sstevel@tonic-gate 	size_t new_allocation;
1866c1a9a9c3Skrishna 	int rv;
18677c478bd9Sstevel@tonic-gate 
18687c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cm->cm_lock));
18697c478bd9Sstevel@tonic-gate 
18707c478bd9Sstevel@tonic-gate 	session_table_count = cm->cm_session_table_count;
18717c478bd9Sstevel@tonic-gate 	session_table = cm->cm_session_table;
18727c478bd9Sstevel@tonic-gate 	need = session_table_count + CRYPTO_SESSION_CHUNK;
18737c478bd9Sstevel@tonic-gate 
18747c478bd9Sstevel@tonic-gate 	current_allocation = session_table_count * sizeof (void *);
18757c478bd9Sstevel@tonic-gate 	new_allocation = need * sizeof (void *);
18767c478bd9Sstevel@tonic-gate 
18777c478bd9Sstevel@tonic-gate 	/*
18787c478bd9Sstevel@tonic-gate 	 * Memory needed to grow the session table is checked
18797c478bd9Sstevel@tonic-gate 	 * against the project.max-crypto-memory resource control.
18807c478bd9Sstevel@tonic-gate 	 */
1881c1a9a9c3Skrishna 	if ((rv = crypto_buffer_check(new_allocation - current_allocation)) !=
1882c1a9a9c3Skrishna 	    CRYPTO_SUCCESS) {
1883c1a9a9c3Skrishna 		return (rv);
18847c478bd9Sstevel@tonic-gate 	}
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate 	/* drop lock while we allocate memory */
18877c478bd9Sstevel@tonic-gate 	mutex_exit(&cm->cm_lock);
18887c478bd9Sstevel@tonic-gate 	new = kmem_zalloc(new_allocation, KM_SLEEP);
18897c478bd9Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate 	/* check if another thread increased the table size */
18927c478bd9Sstevel@tonic-gate 	if (session_table_count != cm->cm_session_table_count) {
18937c478bd9Sstevel@tonic-gate 		kmem_free(new, new_allocation);
18947c478bd9Sstevel@tonic-gate 		return (CRYPTO_SUCCESS);
18957c478bd9Sstevel@tonic-gate 	}
18967c478bd9Sstevel@tonic-gate 
18977c478bd9Sstevel@tonic-gate 	bcopy(session_table, new, current_allocation);
18987c478bd9Sstevel@tonic-gate 	kmem_free(session_table, current_allocation);
18997c478bd9Sstevel@tonic-gate 	cm->cm_session_table = new;
19007c478bd9Sstevel@tonic-gate 	cm->cm_session_table_count += CRYPTO_SESSION_CHUNK;
19017c478bd9Sstevel@tonic-gate 
19027c478bd9Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
19037c478bd9Sstevel@tonic-gate }
19047c478bd9Sstevel@tonic-gate 
19057c478bd9Sstevel@tonic-gate /*
1906cd964fceSMatt Barden  * Find unused entry in session table and return its index.
19077c478bd9Sstevel@tonic-gate  * Initialize session table entry.
19087c478bd9Sstevel@tonic-gate  */
19097c478bd9Sstevel@tonic-gate /* ARGSUSED */
19107c478bd9Sstevel@tonic-gate static int
crypto_open_session(dev_t dev,uint_t flags,crypto_session_id_t * session_index,crypto_provider_id_t provider_id)19117c478bd9Sstevel@tonic-gate crypto_open_session(dev_t dev, uint_t flags, crypto_session_id_t *session_index,
19127c478bd9Sstevel@tonic-gate     crypto_provider_id_t provider_id)
19137c478bd9Sstevel@tonic-gate {
19147c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
19157c478bd9Sstevel@tonic-gate 	int rv;
19167c478bd9Sstevel@tonic-gate 	crypto_provider_session_t *ps;
19177c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t *provider;
19187c478bd9Sstevel@tonic-gate 
19197c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
19207c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "crypto_open_session: failed holding minor");
19217c478bd9Sstevel@tonic-gate 		return (CRYPTO_FAILED);
19227c478bd9Sstevel@tonic-gate 	}
19237c478bd9Sstevel@tonic-gate 
19247c478bd9Sstevel@tonic-gate 	/* initialize provider_array */
19257c478bd9Sstevel@tonic-gate 	if (cm->cm_provider_array == NULL) {
19267c478bd9Sstevel@tonic-gate 		rv = crypto_get_provider_list(cm, NULL, NULL, DONT_RETURN_LIST);
19277c478bd9Sstevel@tonic-gate 		if (rv != 0) {
19287c478bd9Sstevel@tonic-gate 			crypto_release_minor(cm);
19297c478bd9Sstevel@tonic-gate 			return (rv);
19307c478bd9Sstevel@tonic-gate 		}
19317c478bd9Sstevel@tonic-gate 	}
19327c478bd9Sstevel@tonic-gate 
19337c478bd9Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
19347c478bd9Sstevel@tonic-gate 	/* index must be less than count of providers */
19357c478bd9Sstevel@tonic-gate 	if (provider_id >= cm->cm_provider_count) {
19367c478bd9Sstevel@tonic-gate 		mutex_exit(&cm->cm_lock);
19377c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
19387c478bd9Sstevel@tonic-gate 		return (CRYPTO_INVALID_PROVIDER_ID);
19397c478bd9Sstevel@tonic-gate 	}
19407c478bd9Sstevel@tonic-gate 	ASSERT(cm->cm_provider_array != NULL);
19417c478bd9Sstevel@tonic-gate 
19427c478bd9Sstevel@tonic-gate 	rv = crypto_get_provider_session(cm, provider_id, &ps);
19437c478bd9Sstevel@tonic-gate 	if (rv != CRYPTO_SUCCESS) {
19447c478bd9Sstevel@tonic-gate 		mutex_exit(&cm->cm_lock);
19457c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
19467c478bd9Sstevel@tonic-gate 		return (rv);
19477c478bd9Sstevel@tonic-gate 	}
19487c478bd9Sstevel@tonic-gate 	provider = cm->cm_provider_array[provider_id];
19497c478bd9Sstevel@tonic-gate 
1950cd964fceSMatt Barden 	rv = crypto_create_session_ptr(cm, provider, ps, session_index);
1951cd964fceSMatt Barden 	mutex_exit(&cm->cm_lock);
1952cd964fceSMatt Barden 	crypto_release_minor(cm);
1953cd964fceSMatt Barden 	return (rv);
1954cd964fceSMatt Barden 
1955cd964fceSMatt Barden }
1956cd964fceSMatt Barden 
1957cd964fceSMatt Barden static int
crypto_create_session_ptr(crypto_minor_t * cm,kcf_provider_desc_t * provider,crypto_provider_session_t * ps,crypto_session_id_t * session_index)1958cd964fceSMatt Barden crypto_create_session_ptr(crypto_minor_t *cm, kcf_provider_desc_t *provider,
1959cd964fceSMatt Barden     crypto_provider_session_t *ps,  crypto_session_id_t *session_index)
1960cd964fceSMatt Barden {
1961cd964fceSMatt Barden 	crypto_session_data_t **session_table;
1962cd964fceSMatt Barden 	crypto_session_data_t *sp;
1963cd964fceSMatt Barden 	uint_t session_table_count;
1964cd964fceSMatt Barden 	uint_t i;
1965cd964fceSMatt Barden 	int rv;
1966cd964fceSMatt Barden 
1967cd964fceSMatt Barden 	ASSERT(MUTEX_HELD(&cm->cm_lock));
1968cd964fceSMatt Barden 
19697c478bd9Sstevel@tonic-gate again:
19707c478bd9Sstevel@tonic-gate 	session_table_count = cm->cm_session_table_count;
19717c478bd9Sstevel@tonic-gate 	session_table = cm->cm_session_table;
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate 	/* session handles start with 1 */
19747c478bd9Sstevel@tonic-gate 	for (i = 1; i < session_table_count; i++) {
19757c478bd9Sstevel@tonic-gate 		if (session_table[i] == NULL)
19767c478bd9Sstevel@tonic-gate 			break;
19777c478bd9Sstevel@tonic-gate 	}
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate 	if (i == session_table_count || session_table_count == 0) {
19807c478bd9Sstevel@tonic-gate 		if ((rv = grow_session_table(cm)) != CRYPTO_SUCCESS) {
19817c478bd9Sstevel@tonic-gate 			crypto_release_provider_session(cm, ps);
19827c478bd9Sstevel@tonic-gate 			return (rv);
19837c478bd9Sstevel@tonic-gate 		}
19847c478bd9Sstevel@tonic-gate 		goto again;
19857c478bd9Sstevel@tonic-gate 	}
19867c478bd9Sstevel@tonic-gate 
19877c478bd9Sstevel@tonic-gate 	sp = kmem_cache_alloc(crypto_session_cache, KM_SLEEP);
19887c478bd9Sstevel@tonic-gate 	sp->sd_flags = 0;
19897c478bd9Sstevel@tonic-gate 	sp->sd_find_init_cookie = NULL;
19907c478bd9Sstevel@tonic-gate 	sp->sd_digest_ctx = NULL;
19917c478bd9Sstevel@tonic-gate 	sp->sd_encr_ctx = NULL;
19927c478bd9Sstevel@tonic-gate 	sp->sd_decr_ctx = NULL;
19937c478bd9Sstevel@tonic-gate 	sp->sd_sign_ctx = NULL;
19947c478bd9Sstevel@tonic-gate 	sp->sd_verify_ctx = NULL;
1995cd964fceSMatt Barden 	sp->sd_mac_ctx = NULL;
19967c478bd9Sstevel@tonic-gate 	sp->sd_sign_recover_ctx = NULL;
19977c478bd9Sstevel@tonic-gate 	sp->sd_verify_recover_ctx = NULL;
19987c478bd9Sstevel@tonic-gate 	mutex_init(&sp->sd_lock, NULL, MUTEX_DRIVER, NULL);
19997c478bd9Sstevel@tonic-gate 	cv_init(&sp->sd_cv, NULL, CV_DRIVER, NULL);
20007c478bd9Sstevel@tonic-gate 	KCF_PROV_REFHOLD(provider);
20017c478bd9Sstevel@tonic-gate 	sp->sd_provider = provider;
20027c478bd9Sstevel@tonic-gate 	sp->sd_provider_session = ps;
20032d794da1Skrishna 
20042d794da1Skrishna 	/* See the comment for CRYPTO_PRE_APPROVED_LIMIT. */
20052d794da1Skrishna 	if ((rv = crypto_buffer_check(crypto_pre_approved_limit)) !=
20062d794da1Skrishna 	    CRYPTO_SUCCESS) {
20072d794da1Skrishna 		sp->sd_pre_approved_amount = 0;
20082d794da1Skrishna 	} else {
200995014fbbSDan OpenSolaris Anderson 		sp->sd_pre_approved_amount = (int)crypto_pre_approved_limit;
20102d794da1Skrishna 	}
20112d794da1Skrishna 
20127c478bd9Sstevel@tonic-gate 	cm->cm_session_table[i] = sp;
2013cd964fceSMatt Barden 	if (session_index != NULL)
2014cd964fceSMatt Barden 		*session_index = i;
20157c478bd9Sstevel@tonic-gate 
20167c478bd9Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
20177c478bd9Sstevel@tonic-gate }
20187c478bd9Sstevel@tonic-gate 
20197c478bd9Sstevel@tonic-gate /*
20207c478bd9Sstevel@tonic-gate  * Close a session.
20217c478bd9Sstevel@tonic-gate  */
20227c478bd9Sstevel@tonic-gate static int
crypto_close_session(dev_t dev,crypto_session_id_t session_index)20237c478bd9Sstevel@tonic-gate crypto_close_session(dev_t dev, crypto_session_id_t session_index)
20247c478bd9Sstevel@tonic-gate {
20257c478bd9Sstevel@tonic-gate 	crypto_session_data_t **session_table;
20267c478bd9Sstevel@tonic-gate 	crypto_session_data_t *sp;
20277c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
20287c478bd9Sstevel@tonic-gate 
20297c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
20307c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "crypto_close_session: failed holding minor");
20317c478bd9Sstevel@tonic-gate 		return (CRYPTO_FAILED);
20327c478bd9Sstevel@tonic-gate 	}
20337c478bd9Sstevel@tonic-gate 
20347c478bd9Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
20357c478bd9Sstevel@tonic-gate 	session_table = cm->cm_session_table;
20367c478bd9Sstevel@tonic-gate 
20377c478bd9Sstevel@tonic-gate 	if ((session_index) == 0 ||
20387c478bd9Sstevel@tonic-gate 	    (session_index >= cm->cm_session_table_count)) {
20397c478bd9Sstevel@tonic-gate 		mutex_exit(&cm->cm_lock);
20407c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
20417c478bd9Sstevel@tonic-gate 		return (CRYPTO_SESSION_HANDLE_INVALID);
20427c478bd9Sstevel@tonic-gate 	}
20437c478bd9Sstevel@tonic-gate 
20447c478bd9Sstevel@tonic-gate 	sp = session_table[session_index];
20457c478bd9Sstevel@tonic-gate 	if (sp == NULL) {
20467c478bd9Sstevel@tonic-gate 		mutex_exit(&cm->cm_lock);
20477c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
20487c478bd9Sstevel@tonic-gate 		return (CRYPTO_SESSION_HANDLE_INVALID);
20497c478bd9Sstevel@tonic-gate 	}
20507c478bd9Sstevel@tonic-gate 	/*
20517c478bd9Sstevel@tonic-gate 	 * If session is in use, free it when the thread
20527c478bd9Sstevel@tonic-gate 	 * finishes with the session.
20537c478bd9Sstevel@tonic-gate 	 */
20547c478bd9Sstevel@tonic-gate 	mutex_enter(&sp->sd_lock);
20557c478bd9Sstevel@tonic-gate 	if (sp->sd_flags & CRYPTO_SESSION_IS_BUSY) {
20567c478bd9Sstevel@tonic-gate 		sp->sd_flags |= CRYPTO_SESSION_IS_CLOSED;
20577c478bd9Sstevel@tonic-gate 		mutex_exit(&sp->sd_lock);
20587c478bd9Sstevel@tonic-gate 	} else {
20592d794da1Skrishna 		ASSERT(sp->sd_pre_approved_amount == 0 ||
20602d794da1Skrishna 		    sp->sd_pre_approved_amount == crypto_pre_approved_limit);
20612d794da1Skrishna 		CRYPTO_DECREMENT_RCTL(sp->sd_pre_approved_amount);
20622d794da1Skrishna 
20637c478bd9Sstevel@tonic-gate 		if (sp->sd_find_init_cookie != NULL) {
20647c478bd9Sstevel@tonic-gate 			(void) crypto_free_find_ctx(sp);
20657c478bd9Sstevel@tonic-gate 		}
20667c478bd9Sstevel@tonic-gate 
20677c478bd9Sstevel@tonic-gate 		crypto_release_provider_session(cm, sp->sd_provider_session);
20687c478bd9Sstevel@tonic-gate 		KCF_PROV_REFRELE(sp->sd_provider);
20697c478bd9Sstevel@tonic-gate 		CRYPTO_CANCEL_ALL_CTX(sp);
20707c478bd9Sstevel@tonic-gate 		mutex_destroy(&sp->sd_lock);
20717c478bd9Sstevel@tonic-gate 		cv_destroy(&sp->sd_cv);
20727c478bd9Sstevel@tonic-gate 		kmem_cache_free(crypto_session_cache, sp);
20737c478bd9Sstevel@tonic-gate 		session_table[session_index] = NULL;
20747c478bd9Sstevel@tonic-gate 	}
20757c478bd9Sstevel@tonic-gate 
20767c478bd9Sstevel@tonic-gate 	mutex_exit(&cm->cm_lock);
20777c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
20787c478bd9Sstevel@tonic-gate 
20797c478bd9Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
20807c478bd9Sstevel@tonic-gate }
20817c478bd9Sstevel@tonic-gate 
20827c478bd9Sstevel@tonic-gate /*
20837c478bd9Sstevel@tonic-gate  * This ioctl opens a session and returns the session ID in os_session.
20847c478bd9Sstevel@tonic-gate  */
20857c478bd9Sstevel@tonic-gate /* ARGSUSED */
20867c478bd9Sstevel@tonic-gate static int
open_session(dev_t dev,caddr_t arg,int mode,int * rval)20877c478bd9Sstevel@tonic-gate open_session(dev_t dev, caddr_t arg, int mode, int *rval)
20887c478bd9Sstevel@tonic-gate {
20897c478bd9Sstevel@tonic-gate 	crypto_open_session_t open_session;
20907c478bd9Sstevel@tonic-gate 	crypto_session_id_t session;
20917c478bd9Sstevel@tonic-gate 	int rv;
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 	if (copyin(arg, &open_session, sizeof (open_session)) != 0)
20947c478bd9Sstevel@tonic-gate 		return (EFAULT);
20957c478bd9Sstevel@tonic-gate 
20967c478bd9Sstevel@tonic-gate 	rv = crypto_open_session(dev, open_session.os_flags,
20977c478bd9Sstevel@tonic-gate 	    &session, open_session.os_provider_id);
20987c478bd9Sstevel@tonic-gate 	if (rv != CRYPTO_SUCCESS) {
20997c478bd9Sstevel@tonic-gate 		open_session.os_return_value = rv;
21007c478bd9Sstevel@tonic-gate 		if (copyout(&open_session, arg, sizeof (open_session)) != 0) {
21017c478bd9Sstevel@tonic-gate 			return (EFAULT);
21027c478bd9Sstevel@tonic-gate 		}
21037c478bd9Sstevel@tonic-gate 		return (0);
21047c478bd9Sstevel@tonic-gate 	}
21057c478bd9Sstevel@tonic-gate 
21067c478bd9Sstevel@tonic-gate 	open_session.os_session = session;
21077c478bd9Sstevel@tonic-gate 	open_session.os_return_value = CRYPTO_SUCCESS;
21087c478bd9Sstevel@tonic-gate 
21097c478bd9Sstevel@tonic-gate 	if (copyout(&open_session, arg, sizeof (open_session)) != 0) {
21107c478bd9Sstevel@tonic-gate 		return (EFAULT);
21117c478bd9Sstevel@tonic-gate 	}
21127c478bd9Sstevel@tonic-gate 	return (0);
21137c478bd9Sstevel@tonic-gate }
21147c478bd9Sstevel@tonic-gate 
21157c478bd9Sstevel@tonic-gate /*
21167c478bd9Sstevel@tonic-gate  * This ioctl closes a session.
21177c478bd9Sstevel@tonic-gate  */
21187c478bd9Sstevel@tonic-gate /* ARGSUSED */
21197c478bd9Sstevel@tonic-gate static int
close_session(dev_t dev,caddr_t arg,int mode,int * rval)21207c478bd9Sstevel@tonic-gate close_session(dev_t dev, caddr_t arg, int mode, int *rval)
21217c478bd9Sstevel@tonic-gate {
21227c478bd9Sstevel@tonic-gate 	crypto_close_session_t close_session;
21237c478bd9Sstevel@tonic-gate 	int rv;
21247c478bd9Sstevel@tonic-gate 
21257c478bd9Sstevel@tonic-gate 	if (copyin(arg, &close_session, sizeof (close_session)) != 0)
21267c478bd9Sstevel@tonic-gate 		return (EFAULT);
21277c478bd9Sstevel@tonic-gate 
21287c478bd9Sstevel@tonic-gate 	rv = crypto_close_session(dev, close_session.cs_session);
2129894b2776Smcpowers 	close_session.cs_return_value = rv;
21307c478bd9Sstevel@tonic-gate 	if (copyout(&close_session, arg, sizeof (close_session)) != 0) {
21317c478bd9Sstevel@tonic-gate 		return (EFAULT);
21327c478bd9Sstevel@tonic-gate 	}
21337c478bd9Sstevel@tonic-gate 	return (0);
21347c478bd9Sstevel@tonic-gate }
21357c478bd9Sstevel@tonic-gate 
21367c478bd9Sstevel@tonic-gate /*
21377c478bd9Sstevel@tonic-gate  * Copy data model dependent mechanism structure into a kernel mechanism
21387c478bd9Sstevel@tonic-gate  * structure.  Allocate param storage if necessary.
21397c478bd9Sstevel@tonic-gate  */
21407c478bd9Sstevel@tonic-gate static boolean_t
copyin_mech(int mode,crypto_session_data_t * sp,crypto_mechanism_t * in_mech,crypto_mechanism_t * out_mech,size_t * out_rctl_bytes,boolean_t * out_rctl_chk,int * out_rv,int * out_error)21412d794da1Skrishna copyin_mech(int mode, crypto_session_data_t *sp, crypto_mechanism_t *in_mech,
21422d794da1Skrishna     crypto_mechanism_t *out_mech, size_t *out_rctl_bytes,
21432d794da1Skrishna     boolean_t *out_rctl_chk, int *out_rv, int *out_error)
21447c478bd9Sstevel@tonic-gate {
21457c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_mechanism, mech);
21467c478bd9Sstevel@tonic-gate 	caddr_t param;
21477c478bd9Sstevel@tonic-gate 	size_t param_len;
21482d794da1Skrishna 	size_t rctl_bytes = 0;
21497c478bd9Sstevel@tonic-gate 	int error = 0;
21507c478bd9Sstevel@tonic-gate 	int rv = 0;
21517c478bd9Sstevel@tonic-gate 
21527c478bd9Sstevel@tonic-gate 	STRUCT_INIT(mech, mode);
21537c478bd9Sstevel@tonic-gate 	bcopy(in_mech, STRUCT_BUF(mech), STRUCT_SIZE(mech));
21547c478bd9Sstevel@tonic-gate 	param = STRUCT_FGETP(mech, cm_param);
21557c478bd9Sstevel@tonic-gate 	param_len = STRUCT_FGET(mech, cm_param_len);
21567c478bd9Sstevel@tonic-gate 	out_mech->cm_type = STRUCT_FGET(mech, cm_type);
21577c478bd9Sstevel@tonic-gate 	out_mech->cm_param = NULL;
21587c478bd9Sstevel@tonic-gate 	out_mech->cm_param_len = 0;
21597c478bd9Sstevel@tonic-gate 	if (param != NULL && param_len != 0) {
21607c478bd9Sstevel@tonic-gate 		if (param_len > crypto_max_buffer_len) {
21617c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "copyin_mech: buffer greater than "
21627c478bd9Sstevel@tonic-gate 			    "%ld bytes, pid = %d", crypto_max_buffer_len,
21637c478bd9Sstevel@tonic-gate 			    curproc->p_pid);
21647c478bd9Sstevel@tonic-gate 			rv = CRYPTO_ARGUMENTS_BAD;
21657c478bd9Sstevel@tonic-gate 			goto out;
21667c478bd9Sstevel@tonic-gate 		}
21677c478bd9Sstevel@tonic-gate 
21682d794da1Skrishna 		rv = CRYPTO_BUFFER_CHECK(sp, param_len, *out_rctl_chk);
21692d794da1Skrishna 		if (rv != CRYPTO_SUCCESS) {
21702d794da1Skrishna 			goto out;
21717c478bd9Sstevel@tonic-gate 		}
21722d794da1Skrishna 		rctl_bytes = param_len;
21732d794da1Skrishna 
21747c478bd9Sstevel@tonic-gate 		out_mech->cm_param = kmem_alloc(param_len, KM_SLEEP);
21757c478bd9Sstevel@tonic-gate 		if (copyin((char *)param, out_mech->cm_param, param_len) != 0) {
21767c478bd9Sstevel@tonic-gate 			kmem_free(out_mech->cm_param, param_len);
21777c478bd9Sstevel@tonic-gate 			out_mech->cm_param = NULL;
21787c478bd9Sstevel@tonic-gate 			error = EFAULT;
21797c478bd9Sstevel@tonic-gate 			goto out;
21807c478bd9Sstevel@tonic-gate 		}
21817c478bd9Sstevel@tonic-gate 		out_mech->cm_param_len = param_len;
21827c478bd9Sstevel@tonic-gate 	}
21837c478bd9Sstevel@tonic-gate out:
21847c478bd9Sstevel@tonic-gate 	*out_rctl_bytes = rctl_bytes;
21857c478bd9Sstevel@tonic-gate 	*out_rv = rv;
21867c478bd9Sstevel@tonic-gate 	*out_error = error;
21877c478bd9Sstevel@tonic-gate 	return ((rv | error) ? B_FALSE : B_TRUE);
21887c478bd9Sstevel@tonic-gate }
21897c478bd9Sstevel@tonic-gate 
21907c478bd9Sstevel@tonic-gate /*
21917c478bd9Sstevel@tonic-gate  * Free key attributes when key type is CRYPTO_KEY_ATTR_LIST.
21927c478bd9Sstevel@tonic-gate  * The crypto_key structure is not freed.
21937c478bd9Sstevel@tonic-gate  */
21947c478bd9Sstevel@tonic-gate static void
crypto_free_key_attributes(crypto_key_t * key)21957c478bd9Sstevel@tonic-gate crypto_free_key_attributes(crypto_key_t *key)
21967c478bd9Sstevel@tonic-gate {
21977c478bd9Sstevel@tonic-gate 	crypto_object_attribute_t *attrs;
21987c478bd9Sstevel@tonic-gate 	size_t len = 0;
21997c478bd9Sstevel@tonic-gate 	int i;
22007c478bd9Sstevel@tonic-gate 
22017c478bd9Sstevel@tonic-gate 	ASSERT(key->ck_format == CRYPTO_KEY_ATTR_LIST);
22027c478bd9Sstevel@tonic-gate 	if (key->ck_count == 0 || key->ck_attrs == NULL)
22037c478bd9Sstevel@tonic-gate 		return;
22047c478bd9Sstevel@tonic-gate 
22057c478bd9Sstevel@tonic-gate 	/* compute the size of the container */
22067c478bd9Sstevel@tonic-gate 	len = key->ck_count * sizeof (crypto_object_attribute_t);
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate 	/* total up the size of all attributes in the container */
22097c478bd9Sstevel@tonic-gate 	for (i = 0; i < key->ck_count; i++) {
22107c478bd9Sstevel@tonic-gate 		attrs = &key->ck_attrs[i];
22117c478bd9Sstevel@tonic-gate 		if (attrs->oa_value_len != 0 &&
22127c478bd9Sstevel@tonic-gate 		    attrs->oa_value != NULL) {
22137c478bd9Sstevel@tonic-gate 			len += roundup(attrs->oa_value_len, sizeof (caddr_t));
22147c478bd9Sstevel@tonic-gate 		}
22157c478bd9Sstevel@tonic-gate 	}
22167c478bd9Sstevel@tonic-gate 
22177c478bd9Sstevel@tonic-gate 	bzero(key->ck_attrs, len);
22187c478bd9Sstevel@tonic-gate 	kmem_free(key->ck_attrs, len);
22197c478bd9Sstevel@tonic-gate }
22207c478bd9Sstevel@tonic-gate 
22217c478bd9Sstevel@tonic-gate /*
22227c478bd9Sstevel@tonic-gate  * Frees allocated storage in the key structure, but doesn't free
22237c478bd9Sstevel@tonic-gate  * the key structure.
22247c478bd9Sstevel@tonic-gate  */
22257c478bd9Sstevel@tonic-gate static void
free_crypto_key(crypto_key_t * key)22267c478bd9Sstevel@tonic-gate free_crypto_key(crypto_key_t *key)
22277c478bd9Sstevel@tonic-gate {
22287c478bd9Sstevel@tonic-gate 	switch (key->ck_format) {
22297c478bd9Sstevel@tonic-gate 	case CRYPTO_KEY_RAW: {
22307c478bd9Sstevel@tonic-gate 		size_t len;
22317c478bd9Sstevel@tonic-gate 
22327c478bd9Sstevel@tonic-gate 		if (key->ck_length == 0 || key->ck_data == NULL)
22337c478bd9Sstevel@tonic-gate 			break;
22347c478bd9Sstevel@tonic-gate 
22357c478bd9Sstevel@tonic-gate 		len = CRYPTO_BITS2BYTES(key->ck_length);
22367c478bd9Sstevel@tonic-gate 		bzero(key->ck_data, len);
22377c478bd9Sstevel@tonic-gate 		kmem_free(key->ck_data, len);
22387c478bd9Sstevel@tonic-gate 		break;
22397c478bd9Sstevel@tonic-gate 	}
22407c478bd9Sstevel@tonic-gate 
22417c478bd9Sstevel@tonic-gate 	case CRYPTO_KEY_ATTR_LIST:
22427c478bd9Sstevel@tonic-gate 		crypto_free_key_attributes(key);
22437c478bd9Sstevel@tonic-gate 		break;
22447c478bd9Sstevel@tonic-gate 
22457c478bd9Sstevel@tonic-gate 	default:
22467c478bd9Sstevel@tonic-gate 		break;
22477c478bd9Sstevel@tonic-gate 	}
22487c478bd9Sstevel@tonic-gate }
22497c478bd9Sstevel@tonic-gate 
22507c478bd9Sstevel@tonic-gate /*
22517c478bd9Sstevel@tonic-gate  * Copy in an array of crypto_object_attribute structures from user-space.
22527c478bd9Sstevel@tonic-gate  * Kernel memory is allocated for the array and the value of each attribute
22537c478bd9Sstevel@tonic-gate  * in the array.  Since unprivileged users can specify the size of attributes,
22547c478bd9Sstevel@tonic-gate  * the amount of memory needed is charged against the
22557c478bd9Sstevel@tonic-gate  * project.max-crypto-memory resource control.
22567c478bd9Sstevel@tonic-gate  *
22577c478bd9Sstevel@tonic-gate  * Attribute values are copied in from user-space if copyin_value is set to
22587c478bd9Sstevel@tonic-gate  * B_TRUE.  This routine returns B_TRUE if the copyin was successful.
22597c478bd9Sstevel@tonic-gate  */
22607c478bd9Sstevel@tonic-gate static boolean_t
copyin_attributes(int mode,crypto_session_data_t * sp,uint_t count,caddr_t oc_attributes,crypto_object_attribute_t ** k_attrs_out,size_t * k_attrs_size_out,caddr_t * u_attrs_out,int * out_rv,int * out_error,size_t * out_rctl_bytes,boolean_t * out_rctl_chk,boolean_t copyin_value)22612d794da1Skrishna copyin_attributes(int mode, crypto_session_data_t *sp,
22622d794da1Skrishna     uint_t count, caddr_t oc_attributes,
22637c478bd9Sstevel@tonic-gate     crypto_object_attribute_t **k_attrs_out, size_t *k_attrs_size_out,
22647c478bd9Sstevel@tonic-gate     caddr_t *u_attrs_out, int *out_rv, int *out_error, size_t *out_rctl_bytes,
22652d794da1Skrishna     boolean_t *out_rctl_chk, boolean_t copyin_value)
22667c478bd9Sstevel@tonic-gate {
22677c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_attribute, oa);
22687c478bd9Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
22697c478bd9Sstevel@tonic-gate 	caddr_t attrs = NULL, ap, p, value;
22707c478bd9Sstevel@tonic-gate 	caddr_t k_attrs_buf;
22717c478bd9Sstevel@tonic-gate 	size_t k_attrs_len;
22727c478bd9Sstevel@tonic-gate 	size_t k_attrs_buf_len = 0;
22737c478bd9Sstevel@tonic-gate 	size_t k_attrs_total_len = 0;
22747c478bd9Sstevel@tonic-gate 	size_t tmp_len;
22757c478bd9Sstevel@tonic-gate 	size_t rctl_bytes = 0;
22767c478bd9Sstevel@tonic-gate 	size_t len = 0;
22777c478bd9Sstevel@tonic-gate 	size_t value_len;
22787c478bd9Sstevel@tonic-gate 	int error = 0;
22797c478bd9Sstevel@tonic-gate 	int rv = 0;
22807c478bd9Sstevel@tonic-gate 	int i;
22817c478bd9Sstevel@tonic-gate 
22827c478bd9Sstevel@tonic-gate 	STRUCT_INIT(oa, mode);
22837c478bd9Sstevel@tonic-gate 
22847c478bd9Sstevel@tonic-gate 	if (count == 0) {
22857c478bd9Sstevel@tonic-gate 		rv = CRYPTO_SUCCESS;
22867c478bd9Sstevel@tonic-gate 		goto out;
22877c478bd9Sstevel@tonic-gate 	}
22887c478bd9Sstevel@tonic-gate 
22897c478bd9Sstevel@tonic-gate 	if (count > CRYPTO_MAX_ATTRIBUTE_COUNT) {
22907c478bd9Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
22917c478bd9Sstevel@tonic-gate 		goto out;
22927c478bd9Sstevel@tonic-gate 	}
22937c478bd9Sstevel@tonic-gate 
22947c478bd9Sstevel@tonic-gate 	/* compute size of crypto_object_attribute array */
22957c478bd9Sstevel@tonic-gate 	len = count * STRUCT_SIZE(oa);
22967c478bd9Sstevel@tonic-gate 
22977c478bd9Sstevel@tonic-gate 	/* this allocation is not charged against the user's resource limit */
22987c478bd9Sstevel@tonic-gate 	attrs = kmem_alloc(len, KM_SLEEP);
22997c478bd9Sstevel@tonic-gate 	if (copyin(oc_attributes, attrs, len) != 0) {
23007c478bd9Sstevel@tonic-gate 		error = EFAULT;
23017c478bd9Sstevel@tonic-gate 		goto out;
23027c478bd9Sstevel@tonic-gate 	}
23037c478bd9Sstevel@tonic-gate 
23047c478bd9Sstevel@tonic-gate 	/* figure out how much memory to allocate for all of the attributes */
23057c478bd9Sstevel@tonic-gate 	ap = attrs;
23067c478bd9Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
23077c478bd9Sstevel@tonic-gate 		bcopy(ap, STRUCT_BUF(oa), STRUCT_SIZE(oa));
23087c478bd9Sstevel@tonic-gate 		tmp_len = roundup(STRUCT_FGET(oa, oa_value_len),
23097c478bd9Sstevel@tonic-gate 		    sizeof (caddr_t));
23107c478bd9Sstevel@tonic-gate 		if (tmp_len > crypto_max_buffer_len) {
23117c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "copyin_attributes: buffer greater "
23127c478bd9Sstevel@tonic-gate 			    "than %ld bytes, pid = %d", crypto_max_buffer_len,
23137c478bd9Sstevel@tonic-gate 			    curproc->p_pid);
23147c478bd9Sstevel@tonic-gate 			rv = CRYPTO_ARGUMENTS_BAD;
23157c478bd9Sstevel@tonic-gate 			goto out;
23167c478bd9Sstevel@tonic-gate 		}
23177c478bd9Sstevel@tonic-gate 		if (STRUCT_FGETP(oa, oa_value) != NULL)
23187c478bd9Sstevel@tonic-gate 			k_attrs_buf_len += tmp_len;
23197c478bd9Sstevel@tonic-gate 		ap += STRUCT_SIZE(oa);
23207c478bd9Sstevel@tonic-gate 	}
23217c478bd9Sstevel@tonic-gate 
23227c478bd9Sstevel@tonic-gate 	k_attrs_len = count * sizeof (crypto_object_attribute_t);
23237c478bd9Sstevel@tonic-gate 	k_attrs_total_len = k_attrs_buf_len + k_attrs_len;
23242d794da1Skrishna 
23252d794da1Skrishna 	rv = CRYPTO_BUFFER_CHECK(sp, k_attrs_total_len, *out_rctl_chk);
23262d794da1Skrishna 	if (rv != CRYPTO_SUCCESS) {
23272d794da1Skrishna 		goto out;
23287c478bd9Sstevel@tonic-gate 	}
23292d794da1Skrishna 	rctl_bytes = k_attrs_total_len;
23307c478bd9Sstevel@tonic-gate 
23317c478bd9Sstevel@tonic-gate 	/* one big allocation for everything */
23327c478bd9Sstevel@tonic-gate 	k_attrs = kmem_alloc(k_attrs_total_len, KM_SLEEP);
23337c478bd9Sstevel@tonic-gate 	k_attrs_buf = (char *)k_attrs + k_attrs_len;
23347c478bd9Sstevel@tonic-gate 
23357c478bd9Sstevel@tonic-gate 	ap = attrs;
23367c478bd9Sstevel@tonic-gate 	p = k_attrs_buf;
23377c478bd9Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
23387c478bd9Sstevel@tonic-gate 		bcopy(ap, STRUCT_BUF(oa), STRUCT_SIZE(oa));
23397c478bd9Sstevel@tonic-gate 		k_attrs[i].oa_type = STRUCT_FGET(oa, oa_type);
23407c478bd9Sstevel@tonic-gate 		value = STRUCT_FGETP(oa, oa_value);
23417c478bd9Sstevel@tonic-gate 		value_len = STRUCT_FGET(oa, oa_value_len);
23427c478bd9Sstevel@tonic-gate 		if (value != NULL && value_len != 0 && copyin_value) {
23437c478bd9Sstevel@tonic-gate 			if (copyin(value, p, value_len) != 0) {
23447c478bd9Sstevel@tonic-gate 				kmem_free(k_attrs, k_attrs_total_len);
23457c478bd9Sstevel@tonic-gate 				k_attrs = NULL;
23467c478bd9Sstevel@tonic-gate 				error = EFAULT;
23477c478bd9Sstevel@tonic-gate 				goto out;
23487c478bd9Sstevel@tonic-gate 			}
23497c478bd9Sstevel@tonic-gate 		}
23507c478bd9Sstevel@tonic-gate 
23515c2306d7Smcpowers 		if (value != NULL) {
23525c2306d7Smcpowers 			k_attrs[i].oa_value = p;
23535c2306d7Smcpowers 			p += roundup(value_len, sizeof (caddr_t));
23545c2306d7Smcpowers 		} else {
23555c2306d7Smcpowers 			k_attrs[i].oa_value = NULL;
23565c2306d7Smcpowers 		}
23577c478bd9Sstevel@tonic-gate 		k_attrs[i].oa_value_len = value_len;
23587c478bd9Sstevel@tonic-gate 		ap += STRUCT_SIZE(oa);
23597c478bd9Sstevel@tonic-gate 	}
23607c478bd9Sstevel@tonic-gate out:
23617c478bd9Sstevel@tonic-gate 	if (attrs != NULL) {
23627c478bd9Sstevel@tonic-gate 		/*
23637c478bd9Sstevel@tonic-gate 		 * Free the array if there is a failure or the caller
23647c478bd9Sstevel@tonic-gate 		 * doesn't want the array to be returned.
23657c478bd9Sstevel@tonic-gate 		 */
23667c478bd9Sstevel@tonic-gate 		if (error != 0 || rv != CRYPTO_SUCCESS || u_attrs_out == NULL) {
23677c478bd9Sstevel@tonic-gate 			kmem_free(attrs, len);
23687c478bd9Sstevel@tonic-gate 			attrs = NULL;
23697c478bd9Sstevel@tonic-gate 		}
23707c478bd9Sstevel@tonic-gate 	}
23717c478bd9Sstevel@tonic-gate 
23727c478bd9Sstevel@tonic-gate 	if (u_attrs_out != NULL)
23737c478bd9Sstevel@tonic-gate 		*u_attrs_out = attrs;
23747c478bd9Sstevel@tonic-gate 	if (k_attrs_size_out != NULL)
23757c478bd9Sstevel@tonic-gate 		*k_attrs_size_out = k_attrs_total_len;
23767c478bd9Sstevel@tonic-gate 	*k_attrs_out = k_attrs;
23777c478bd9Sstevel@tonic-gate 	*out_rctl_bytes = rctl_bytes;
23787c478bd9Sstevel@tonic-gate 	*out_rv = rv;
23797c478bd9Sstevel@tonic-gate 	*out_error = error;
23807c478bd9Sstevel@tonic-gate 	return ((rv | error) ? B_FALSE : B_TRUE);
23817c478bd9Sstevel@tonic-gate }
23827c478bd9Sstevel@tonic-gate 
23837c478bd9Sstevel@tonic-gate /*
23847c478bd9Sstevel@tonic-gate  * Copy data model dependent raw key into a kernel key
23857c478bd9Sstevel@tonic-gate  * structure.  Checks key length or attribute lengths against
23867c478bd9Sstevel@tonic-gate  * resource controls before allocating memory.  Returns B_TRUE
23877c478bd9Sstevel@tonic-gate  * if both error and rv are set to 0.
23887c478bd9Sstevel@tonic-gate  */
23897c478bd9Sstevel@tonic-gate static boolean_t
copyin_key(int mode,crypto_session_data_t * sp,crypto_key_t * in_key,crypto_key_t * out_key,size_t * out_rctl_bytes,boolean_t * out_rctl_chk,int * out_rv,int * out_error)23902d794da1Skrishna copyin_key(int mode, crypto_session_data_t *sp, crypto_key_t *in_key,
23912d794da1Skrishna     crypto_key_t *out_key, size_t *out_rctl_bytes,
23922d794da1Skrishna     boolean_t *out_rctl_chk, int *out_rv, int *out_error)
23937c478bd9Sstevel@tonic-gate {
23947c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_key, key);
23957c478bd9Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
23967c478bd9Sstevel@tonic-gate 	size_t key_bits;
23977c478bd9Sstevel@tonic-gate 	size_t key_bytes = 0;
23987c478bd9Sstevel@tonic-gate 	size_t rctl_bytes = 0;
23997c478bd9Sstevel@tonic-gate 	int count;
24007c478bd9Sstevel@tonic-gate 	int error = 0;
24017c478bd9Sstevel@tonic-gate 	int rv = CRYPTO_SUCCESS;
24027c478bd9Sstevel@tonic-gate 
24037c478bd9Sstevel@tonic-gate 	STRUCT_INIT(key, mode);
24047c478bd9Sstevel@tonic-gate 	bcopy(in_key, STRUCT_BUF(key), STRUCT_SIZE(key));
24057c478bd9Sstevel@tonic-gate 	out_key->ck_format = STRUCT_FGET(key, ck_format);
24067c478bd9Sstevel@tonic-gate 	switch (out_key->ck_format) {
24077c478bd9Sstevel@tonic-gate 	case CRYPTO_KEY_RAW:
24087c478bd9Sstevel@tonic-gate 		key_bits = STRUCT_FGET(key, ck_length);
24097c478bd9Sstevel@tonic-gate 		if (key_bits != 0) {
241095014fbbSDan OpenSolaris Anderson 			if (key_bits >
241195014fbbSDan OpenSolaris Anderson 			    (CRYPTO_BYTES2BITS(crypto_max_buffer_len))) {
24127c478bd9Sstevel@tonic-gate 				cmn_err(CE_NOTE, "copyin_key: buffer greater "
24137c478bd9Sstevel@tonic-gate 				    "than %ld bytes, pid = %d",
24147c478bd9Sstevel@tonic-gate 				    crypto_max_buffer_len, curproc->p_pid);
24157c478bd9Sstevel@tonic-gate 				rv = CRYPTO_ARGUMENTS_BAD;
24167c478bd9Sstevel@tonic-gate 				goto out;
24177c478bd9Sstevel@tonic-gate 			}
241895014fbbSDan OpenSolaris Anderson 			key_bytes = CRYPTO_BITS2BYTES(key_bits);
24197c478bd9Sstevel@tonic-gate 
24202d794da1Skrishna 			rv = CRYPTO_BUFFER_CHECK(sp, key_bytes,
24212d794da1Skrishna 			    *out_rctl_chk);
24227c478bd9Sstevel@tonic-gate 			if (rv != CRYPTO_SUCCESS) {
24237c478bd9Sstevel@tonic-gate 				goto out;
24247c478bd9Sstevel@tonic-gate 			}
24252d794da1Skrishna 			rctl_bytes = key_bytes;
24267c478bd9Sstevel@tonic-gate 
24277c478bd9Sstevel@tonic-gate 			out_key->ck_data = kmem_alloc(key_bytes, KM_SLEEP);
24287c478bd9Sstevel@tonic-gate 
24297c478bd9Sstevel@tonic-gate 			if (copyin((char *)STRUCT_FGETP(key, ck_data),
24307c478bd9Sstevel@tonic-gate 			    out_key->ck_data, key_bytes) != 0) {
24317c478bd9Sstevel@tonic-gate 				kmem_free(out_key->ck_data, key_bytes);
24327c478bd9Sstevel@tonic-gate 				out_key->ck_data = NULL;
24337c478bd9Sstevel@tonic-gate 				out_key->ck_length = 0;
24347c478bd9Sstevel@tonic-gate 				error = EFAULT;
24357c478bd9Sstevel@tonic-gate 				goto out;
24367c478bd9Sstevel@tonic-gate 			}
24377c478bd9Sstevel@tonic-gate 		}
243895014fbbSDan OpenSolaris Anderson 		out_key->ck_length = (ulong_t)key_bits;
24397c478bd9Sstevel@tonic-gate 		break;
24407c478bd9Sstevel@tonic-gate 
24417c478bd9Sstevel@tonic-gate 	case CRYPTO_KEY_ATTR_LIST:
24427c478bd9Sstevel@tonic-gate 		count = STRUCT_FGET(key, ck_count);
24437c478bd9Sstevel@tonic-gate 
24442d794da1Skrishna 		if (copyin_attributes(mode, sp, count,
24457c478bd9Sstevel@tonic-gate 		    (caddr_t)STRUCT_FGETP(key, ck_attrs), &k_attrs, NULL, NULL,
24462d794da1Skrishna 		    &rv, &error, &rctl_bytes, out_rctl_chk, B_TRUE)) {
24477c478bd9Sstevel@tonic-gate 			out_key->ck_count = count;
24487c478bd9Sstevel@tonic-gate 			out_key->ck_attrs = k_attrs;
24497c478bd9Sstevel@tonic-gate 			k_attrs = NULL;
24507c478bd9Sstevel@tonic-gate 		} else {
24517c478bd9Sstevel@tonic-gate 			out_key->ck_count = 0;
24527c478bd9Sstevel@tonic-gate 			out_key->ck_attrs = NULL;
24537c478bd9Sstevel@tonic-gate 		}
24547c478bd9Sstevel@tonic-gate 		break;
24557c478bd9Sstevel@tonic-gate 
24567c478bd9Sstevel@tonic-gate 	case CRYPTO_KEY_REFERENCE:
24577c478bd9Sstevel@tonic-gate 		out_key->ck_obj_id = STRUCT_FGET(key, ck_obj_id);
24587c478bd9Sstevel@tonic-gate 		break;
24597c478bd9Sstevel@tonic-gate 
24607c478bd9Sstevel@tonic-gate 	default:
24617c478bd9Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
24627c478bd9Sstevel@tonic-gate 	}
24637c478bd9Sstevel@tonic-gate 
24647c478bd9Sstevel@tonic-gate out:
24657c478bd9Sstevel@tonic-gate 	*out_rctl_bytes = rctl_bytes;
24667c478bd9Sstevel@tonic-gate 	*out_rv = rv;
24677c478bd9Sstevel@tonic-gate 	*out_error = error;
24687c478bd9Sstevel@tonic-gate 	return ((rv | error) ? B_FALSE : B_TRUE);
24697c478bd9Sstevel@tonic-gate }
24707c478bd9Sstevel@tonic-gate 
24717c478bd9Sstevel@tonic-gate /*
24727c478bd9Sstevel@tonic-gate  * This routine does two things:
24737c478bd9Sstevel@tonic-gate  * 1. Given a crypto_minor structure and a session ID, it returns
24747c478bd9Sstevel@tonic-gate  *    a valid session pointer.
24757c478bd9Sstevel@tonic-gate  * 2. It checks that the provider, to which the session has been opened,
24767c478bd9Sstevel@tonic-gate  *    has not been removed.
24777c478bd9Sstevel@tonic-gate  */
24787c478bd9Sstevel@tonic-gate static boolean_t
get_session_ptr(crypto_session_id_t i,crypto_minor_t * cm,crypto_session_data_t ** session_ptr,int * out_error,int * out_rv)24797c478bd9Sstevel@tonic-gate get_session_ptr(crypto_session_id_t i, crypto_minor_t *cm,
24807c478bd9Sstevel@tonic-gate     crypto_session_data_t **session_ptr, int *out_error, int *out_rv)
24817c478bd9Sstevel@tonic-gate {
24827c478bd9Sstevel@tonic-gate 	crypto_session_data_t *sp = NULL;
24837c478bd9Sstevel@tonic-gate 	int rv = CRYPTO_SESSION_HANDLE_INVALID;
24847c478bd9Sstevel@tonic-gate 	int error = 0;
24857c478bd9Sstevel@tonic-gate 
24867c478bd9Sstevel@tonic-gate 	mutex_enter(&cm->cm_lock);
24877c478bd9Sstevel@tonic-gate 	if ((i < cm->cm_session_table_count) &&
24887c478bd9Sstevel@tonic-gate 	    (cm->cm_session_table[i] != NULL)) {
24897c478bd9Sstevel@tonic-gate 		sp = cm->cm_session_table[i];
24907c478bd9Sstevel@tonic-gate 		mutex_enter(&sp->sd_lock);
24917c478bd9Sstevel@tonic-gate 		mutex_exit(&cm->cm_lock);
24927c478bd9Sstevel@tonic-gate 		while (sp->sd_flags & CRYPTO_SESSION_IS_BUSY) {
24937c478bd9Sstevel@tonic-gate 			if (cv_wait_sig(&sp->sd_cv, &sp->sd_lock) == 0) {
24947c478bd9Sstevel@tonic-gate 				mutex_exit(&sp->sd_lock);
24957c478bd9Sstevel@tonic-gate 				sp = NULL;
24967c478bd9Sstevel@tonic-gate 				error = EINTR;
24977c478bd9Sstevel@tonic-gate 				goto out;
24987c478bd9Sstevel@tonic-gate 			}
24997c478bd9Sstevel@tonic-gate 		}
25007c478bd9Sstevel@tonic-gate 
25017c478bd9Sstevel@tonic-gate 		if (sp->sd_flags & CRYPTO_SESSION_IS_CLOSED) {
25027c478bd9Sstevel@tonic-gate 			mutex_exit(&sp->sd_lock);
25037c478bd9Sstevel@tonic-gate 			sp = NULL;
25047c478bd9Sstevel@tonic-gate 			goto out;
25057c478bd9Sstevel@tonic-gate 		}
25067c478bd9Sstevel@tonic-gate 
25077c478bd9Sstevel@tonic-gate 		if (KCF_IS_PROV_REMOVED(sp->sd_provider)) {
25087c478bd9Sstevel@tonic-gate 			mutex_exit(&sp->sd_lock);
25097c478bd9Sstevel@tonic-gate 			sp = NULL;
25107c478bd9Sstevel@tonic-gate 			rv = CRYPTO_DEVICE_ERROR;
25117c478bd9Sstevel@tonic-gate 			goto out;
25127c478bd9Sstevel@tonic-gate 		}
25137c478bd9Sstevel@tonic-gate 
25147c478bd9Sstevel@tonic-gate 		rv = CRYPTO_SUCCESS;
25157c478bd9Sstevel@tonic-gate 		sp->sd_flags |= CRYPTO_SESSION_IS_BUSY;
25167c478bd9Sstevel@tonic-gate 		mutex_exit(&sp->sd_lock);
25177c478bd9Sstevel@tonic-gate 	} else {
25187c478bd9Sstevel@tonic-gate 		mutex_exit(&cm->cm_lock);
25197c478bd9Sstevel@tonic-gate 	}
25207c478bd9Sstevel@tonic-gate out:
25217c478bd9Sstevel@tonic-gate 	*session_ptr = sp;
25227c478bd9Sstevel@tonic-gate 	*out_error = error;
25237c478bd9Sstevel@tonic-gate 	*out_rv = rv;
25247c478bd9Sstevel@tonic-gate 	return ((rv == CRYPTO_SUCCESS && error == 0) ? B_TRUE : B_FALSE);
25257c478bd9Sstevel@tonic-gate }
25267c478bd9Sstevel@tonic-gate 
25272d794da1Skrishna #define	CRYPTO_SESSION_RELE(s)	if ((s) != NULL) {	\
25287c478bd9Sstevel@tonic-gate 	mutex_enter(&((s)->sd_lock));			\
25297c478bd9Sstevel@tonic-gate 	(s)->sd_flags &= ~CRYPTO_SESSION_IS_BUSY;	\
25307c478bd9Sstevel@tonic-gate 	cv_broadcast(&(s)->sd_cv);			\
25317c478bd9Sstevel@tonic-gate 	mutex_exit(&((s)->sd_lock));			\
25327c478bd9Sstevel@tonic-gate }
25337c478bd9Sstevel@tonic-gate 
25347c478bd9Sstevel@tonic-gate /* ARGSUSED */
25357c478bd9Sstevel@tonic-gate static int
encrypt_init(dev_t dev,caddr_t arg,int mode,int * rval)25367c478bd9Sstevel@tonic-gate encrypt_init(dev_t dev, caddr_t arg, int mode, int *rval)
25377c478bd9Sstevel@tonic-gate {
25387c478bd9Sstevel@tonic-gate 	return (cipher_init(dev, arg, mode, crypto_encrypt_init_prov));
25397c478bd9Sstevel@tonic-gate }
25407c478bd9Sstevel@tonic-gate 
25417c478bd9Sstevel@tonic-gate /* ARGSUSED */
25427c478bd9Sstevel@tonic-gate static int
decrypt_init(dev_t dev,caddr_t arg,int mode,int * rval)25437c478bd9Sstevel@tonic-gate decrypt_init(dev_t dev, caddr_t arg, int mode, int *rval)
25447c478bd9Sstevel@tonic-gate {
25457c478bd9Sstevel@tonic-gate 	return (cipher_init(dev, arg, mode, crypto_decrypt_init_prov));
25467c478bd9Sstevel@tonic-gate }
25477c478bd9Sstevel@tonic-gate 
2548894b2776Smcpowers /*
2549894b2776Smcpowers  * umech is a mechanism structure that has been copied from user address
2550894b2776Smcpowers  * space into kernel address space. Only one copyin has been done.
2551894b2776Smcpowers  * The mechanism parameter, if non-null, still points to user address space.
2552894b2776Smcpowers  * If the mechanism parameter contains pointers, they are pointers into
2553894b2776Smcpowers  * user address space.
2554894b2776Smcpowers  *
2555894b2776Smcpowers  * kmech is a umech with all pointers and structures in kernel address space.
2556894b2776Smcpowers  *
2557894b2776Smcpowers  * This routine calls the provider's entry point to copy a umech parameter
2558894b2776Smcpowers  * into kernel address space. Kernel memory is allocated by the provider.
2559894b2776Smcpowers  */
2560894b2776Smcpowers static int
crypto_provider_copyin_mech_param(kcf_provider_desc_t * pd,crypto_mechanism_t * umech,crypto_mechanism_t * kmech,int mode,int * error)2561894b2776Smcpowers crypto_provider_copyin_mech_param(kcf_provider_desc_t *pd,
2562894b2776Smcpowers     crypto_mechanism_t *umech, crypto_mechanism_t *kmech, int mode, int *error)
2563894b2776Smcpowers {
2564894b2776Smcpowers 	crypto_mech_type_t provider_mech_type;
2565894b2776Smcpowers 	int rv;
2566894b2776Smcpowers 
2567894b2776Smcpowers 	/* get the provider's mech number */
25686a1073f8Skrishna 	provider_mech_type = KCF_TO_PROV_MECHNUM(pd, umech->cm_type);
2569894b2776Smcpowers 
2570894b2776Smcpowers 	kmech->cm_param = NULL;
2571894b2776Smcpowers 	kmech->cm_param_len = 0;
2572894b2776Smcpowers 	kmech->cm_type = provider_mech_type;
2573894b2776Smcpowers 	rv = KCF_PROV_COPYIN_MECH(pd, umech, kmech, error, mode);
2574894b2776Smcpowers 	kmech->cm_type = umech->cm_type;
2575894b2776Smcpowers 
2576894b2776Smcpowers 	return (rv);
2577894b2776Smcpowers }
2578894b2776Smcpowers 
2579894b2776Smcpowers /*
2580894b2776Smcpowers  * umech is a mechanism structure that has been copied from user address
2581894b2776Smcpowers  * space into kernel address space. Only one copyin has been done.
2582894b2776Smcpowers  * The mechanism parameter, if non-null, still points to user address space.
2583894b2776Smcpowers  * If the mechanism parameter contains pointers, they are pointers into
2584894b2776Smcpowers  * user address space.
2585894b2776Smcpowers  *
2586894b2776Smcpowers  * kmech is a umech with all pointers and structures in kernel address space.
2587894b2776Smcpowers  *
2588894b2776Smcpowers  * This routine calls the provider's entry point to copy a kmech parameter
2589894b2776Smcpowers  * into user address space using umech as a template containing
2590894b2776Smcpowers  * user address pointers.
2591894b2776Smcpowers  */
2592894b2776Smcpowers static int
crypto_provider_copyout_mech_param(kcf_provider_desc_t * pd,crypto_mechanism_t * kmech,crypto_mechanism_t * umech,int mode,int * error)2593894b2776Smcpowers crypto_provider_copyout_mech_param(kcf_provider_desc_t *pd,
2594894b2776Smcpowers     crypto_mechanism_t *kmech, crypto_mechanism_t *umech, int mode, int *error)
2595894b2776Smcpowers {
2596894b2776Smcpowers 	crypto_mech_type_t provider_mech_type;
2597894b2776Smcpowers 	int rv;
2598894b2776Smcpowers 
2599894b2776Smcpowers 	/* get the provider's mech number */
26006a1073f8Skrishna 	provider_mech_type = KCF_TO_PROV_MECHNUM(pd, umech->cm_type);
2601894b2776Smcpowers 
2602894b2776Smcpowers 	kmech->cm_type = provider_mech_type;
2603894b2776Smcpowers 	rv = KCF_PROV_COPYOUT_MECH(pd, kmech, umech, error, mode);
2604894b2776Smcpowers 	kmech->cm_type = umech->cm_type;
2605894b2776Smcpowers 
2606894b2776Smcpowers 	return (rv);
2607894b2776Smcpowers }
2608894b2776Smcpowers 
2609894b2776Smcpowers /*
2610894b2776Smcpowers  * Call the provider's entry point to free kernel memory that has been
2611894b2776Smcpowers  * allocated for the mechanism's parameter.
2612894b2776Smcpowers  */
2613894b2776Smcpowers static void
crypto_free_mech(kcf_provider_desc_t * pd,boolean_t allocated_by_crypto_module,crypto_mechanism_t * mech)2614894b2776Smcpowers crypto_free_mech(kcf_provider_desc_t *pd, boolean_t allocated_by_crypto_module,
2615894b2776Smcpowers     crypto_mechanism_t *mech)
2616894b2776Smcpowers {
2617894b2776Smcpowers 	crypto_mech_type_t provider_mech_type;
2618894b2776Smcpowers 
2619894b2776Smcpowers 	if (allocated_by_crypto_module) {
2620894b2776Smcpowers 		if (mech->cm_param != NULL)
2621894b2776Smcpowers 			kmem_free(mech->cm_param, mech->cm_param_len);
2622894b2776Smcpowers 	} else {
2623894b2776Smcpowers 		/* get the provider's mech number */
26246a1073f8Skrishna 		provider_mech_type = KCF_TO_PROV_MECHNUM(pd, mech->cm_type);
2625894b2776Smcpowers 
2626894b2776Smcpowers 		if (mech->cm_param != NULL && mech->cm_param_len != 0) {
2627894b2776Smcpowers 			mech->cm_type = provider_mech_type;
2628894b2776Smcpowers 			(void) KCF_PROV_FREE_MECH(pd, mech);
2629894b2776Smcpowers 		}
2630894b2776Smcpowers 	}
2631894b2776Smcpowers }
2632894b2776Smcpowers 
26337c478bd9Sstevel@tonic-gate /*
26347c478bd9Sstevel@tonic-gate  * ASSUMPTION: crypto_encrypt_init and crypto_decrypt_init
26357c478bd9Sstevel@tonic-gate  * structures are identical except for field names.
26367c478bd9Sstevel@tonic-gate  */
26377c478bd9Sstevel@tonic-gate static int
cipher_init(dev_t dev,caddr_t arg,int mode,int (* init)(crypto_provider_t,crypto_session_id_t,crypto_mechanism_t *,crypto_key_t *,crypto_ctx_template_t,crypto_context_t *,crypto_call_req_t *))2638894b2776Smcpowers cipher_init(dev_t dev, caddr_t arg, int mode, int (*init)(crypto_provider_t,
26397c478bd9Sstevel@tonic-gate     crypto_session_id_t, crypto_mechanism_t *, crypto_key_t *,
26407c478bd9Sstevel@tonic-gate     crypto_ctx_template_t, crypto_context_t *, crypto_call_req_t *))
26417c478bd9Sstevel@tonic-gate {
26427c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_encrypt_init, encrypt_init);
2643894b2776Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
26447c478bd9Sstevel@tonic-gate 	crypto_session_id_t session_id;
26457c478bd9Sstevel@tonic-gate 	crypto_mechanism_t mech;
26467c478bd9Sstevel@tonic-gate 	crypto_key_t key;
26477c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
26482d794da1Skrishna 	crypto_session_data_t *sp = NULL;
26497c478bd9Sstevel@tonic-gate 	crypto_context_t cc;
26507c478bd9Sstevel@tonic-gate 	crypto_ctx_t **ctxpp;
26517c478bd9Sstevel@tonic-gate 	size_t mech_rctl_bytes = 0;
26522d794da1Skrishna 	boolean_t mech_rctl_chk = B_FALSE;
26537c478bd9Sstevel@tonic-gate 	size_t key_rctl_bytes = 0;
26542d794da1Skrishna 	boolean_t key_rctl_chk = B_FALSE;
26557c478bd9Sstevel@tonic-gate 	int error = 0;
26567c478bd9Sstevel@tonic-gate 	int rv;
2657894b2776Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
265872eff6e2Smcpowers 	crypto_func_group_t fg;
26597c478bd9Sstevel@tonic-gate 
26607c478bd9Sstevel@tonic-gate 	STRUCT_INIT(encrypt_init, mode);
26617c478bd9Sstevel@tonic-gate 
26627c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
26637c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "cipher_init: failed holding minor");
26647c478bd9Sstevel@tonic-gate 		return (ENXIO);
26657c478bd9Sstevel@tonic-gate 	}
26667c478bd9Sstevel@tonic-gate 
26677c478bd9Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(encrypt_init),
26687c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(encrypt_init)) != 0) {
26697c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
26707c478bd9Sstevel@tonic-gate 		return (EFAULT);
26717c478bd9Sstevel@tonic-gate 	}
26727c478bd9Sstevel@tonic-gate 
26737c478bd9Sstevel@tonic-gate 	mech.cm_param = NULL;
26747c478bd9Sstevel@tonic-gate 	bzero(&key, sizeof (crypto_key_t));
26757c478bd9Sstevel@tonic-gate 
26767c478bd9Sstevel@tonic-gate 	session_id = STRUCT_FGET(encrypt_init, ei_session);
26777c478bd9Sstevel@tonic-gate 
26787c478bd9Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
26792d794da1Skrishna 		goto out;
26807c478bd9Sstevel@tonic-gate 	}
26817c478bd9Sstevel@tonic-gate 
2682894b2776Smcpowers 	bcopy(STRUCT_FADDR(encrypt_init, ei_mech), &mech.cm_type,
2683894b2776Smcpowers 	    sizeof (crypto_mech_type_t));
26847c478bd9Sstevel@tonic-gate 
268572eff6e2Smcpowers 	if (init == crypto_encrypt_init_prov) {
268672eff6e2Smcpowers 		fg = CRYPTO_FG_ENCRYPT;
268772eff6e2Smcpowers 	} else {
268872eff6e2Smcpowers 		fg = CRYPTO_FG_DECRYPT;
268972eff6e2Smcpowers 	}
26907c478bd9Sstevel@tonic-gate 
2691436935a1SVladimir Kotal 	/* We need the key length for provider selection so copy it in now. */
2692436935a1SVladimir Kotal 	if (!copyin_key(mode, sp, STRUCT_FADDR(encrypt_init, ei_key), &key,
2693436935a1SVladimir Kotal 	    &key_rctl_bytes, &key_rctl_chk, &rv, &error)) {
2694436935a1SVladimir Kotal 		goto out;
2695436935a1SVladimir Kotal 	}
2696436935a1SVladimir Kotal 
2697436935a1SVladimir Kotal 	if ((rv = kcf_get_hardware_provider(mech.cm_type, &key,
26989b009fc1SValerie Bubb Fenwick 	    CRYPTO_MECH_INVALID, NULL, sp->sd_provider, &real_provider, fg))
269972eff6e2Smcpowers 	    != CRYPTO_SUCCESS) {
27007c478bd9Sstevel@tonic-gate 		goto out;
27017c478bd9Sstevel@tonic-gate 	}
27027c478bd9Sstevel@tonic-gate 
2703894b2776Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
2704894b2776Smcpowers 	    STRUCT_FADDR(encrypt_init, ei_mech), &mech, mode, &error);
2705894b2776Smcpowers 
2706894b2776Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
2707894b2776Smcpowers 		allocated_by_crypto_module = B_TRUE;
27082d794da1Skrishna 		if (!copyin_mech(mode, sp, STRUCT_FADDR(encrypt_init, ei_mech),
27092d794da1Skrishna 		    &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) {
2710894b2776Smcpowers 			goto out;
2711894b2776Smcpowers 		}
2712894b2776Smcpowers 	} else {
2713894b2776Smcpowers 		if (rv != CRYPTO_SUCCESS)
2714894b2776Smcpowers 			goto out;
2715894b2776Smcpowers 	}
2716894b2776Smcpowers 
27177c478bd9Sstevel@tonic-gate 	rv = (init)(real_provider, sp->sd_provider_session->ps_session,
27187c478bd9Sstevel@tonic-gate 	    &mech, &key, NULL, &cc, NULL);
27197c478bd9Sstevel@tonic-gate 
27207c478bd9Sstevel@tonic-gate 	/*
27217c478bd9Sstevel@tonic-gate 	 * Check if a context already exists. If so, it means it is being
27227c478bd9Sstevel@tonic-gate 	 * abandoned. So, cancel it to avoid leaking it.
27237c478bd9Sstevel@tonic-gate 	 */
27247c478bd9Sstevel@tonic-gate 	ctxpp = (init == crypto_encrypt_init_prov) ?
27257c478bd9Sstevel@tonic-gate 	    &sp->sd_encr_ctx : &sp->sd_decr_ctx;
27267c478bd9Sstevel@tonic-gate 
27277c478bd9Sstevel@tonic-gate 	if (*ctxpp != NULL)
27287c478bd9Sstevel@tonic-gate 		CRYPTO_CANCEL_CTX(ctxpp);
27297c478bd9Sstevel@tonic-gate 	*ctxpp = (rv == CRYPTO_SUCCESS) ? cc : NULL;
2730894b2776Smcpowers 
27317c478bd9Sstevel@tonic-gate out:
27322d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
27332d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk);
27347c478bd9Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
27357c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
27367c478bd9Sstevel@tonic-gate 
2737894b2776Smcpowers 	if (real_provider != NULL) {
2738894b2776Smcpowers 		crypto_free_mech(real_provider,
2739894b2776Smcpowers 		    allocated_by_crypto_module, &mech);
2740894b2776Smcpowers 		KCF_PROV_REFRELE(real_provider);
2741894b2776Smcpowers 	}
27427c478bd9Sstevel@tonic-gate 
27437c478bd9Sstevel@tonic-gate 	free_crypto_key(&key);
27447c478bd9Sstevel@tonic-gate 
27457c478bd9Sstevel@tonic-gate 	if (error != 0)
2746894b2776Smcpowers 		/* XXX free context */
27477c478bd9Sstevel@tonic-gate 		return (error);
27487c478bd9Sstevel@tonic-gate 
27497c478bd9Sstevel@tonic-gate 	STRUCT_FSET(encrypt_init, ei_return_value, rv);
27507c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(encrypt_init), arg,
27517c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(encrypt_init)) != 0) {
2752894b2776Smcpowers 		/* XXX free context */
27537c478bd9Sstevel@tonic-gate 		return (EFAULT);
27547c478bd9Sstevel@tonic-gate 	}
27557c478bd9Sstevel@tonic-gate 	return (0);
27567c478bd9Sstevel@tonic-gate }
27577c478bd9Sstevel@tonic-gate 
27587c478bd9Sstevel@tonic-gate /* ARGSUSED */
27597c478bd9Sstevel@tonic-gate static int
encrypt(dev_t dev,caddr_t arg,int mode,int * rval)27607c478bd9Sstevel@tonic-gate encrypt(dev_t dev, caddr_t arg, int mode, int *rval)
27617c478bd9Sstevel@tonic-gate {
27627c478bd9Sstevel@tonic-gate 	return (cipher(dev, arg, mode, crypto_encrypt_single));
27637c478bd9Sstevel@tonic-gate }
27647c478bd9Sstevel@tonic-gate 
27657c478bd9Sstevel@tonic-gate /* ARGSUSED */
27667c478bd9Sstevel@tonic-gate static int
decrypt(dev_t dev,caddr_t arg,int mode,int * rval)27677c478bd9Sstevel@tonic-gate decrypt(dev_t dev, caddr_t arg, int mode, int *rval)
27687c478bd9Sstevel@tonic-gate {
27697c478bd9Sstevel@tonic-gate 	return (cipher(dev, arg, mode, crypto_decrypt_single));
27707c478bd9Sstevel@tonic-gate }
27717c478bd9Sstevel@tonic-gate 
27727c478bd9Sstevel@tonic-gate /*
27737c478bd9Sstevel@tonic-gate  * ASSUMPTION: crypto_encrypt and crypto_decrypt structures
27747c478bd9Sstevel@tonic-gate  * are identical except for field names.
27757c478bd9Sstevel@tonic-gate  */
27767c478bd9Sstevel@tonic-gate static int
cipher(dev_t dev,caddr_t arg,int mode,int (* single)(crypto_context_t,crypto_data_t *,crypto_data_t *,crypto_call_req_t *))27777c478bd9Sstevel@tonic-gate cipher(dev_t dev, caddr_t arg, int mode,
27787c478bd9Sstevel@tonic-gate     int (*single)(crypto_context_t, crypto_data_t *, crypto_data_t *,
27797c478bd9Sstevel@tonic-gate     crypto_call_req_t *))
27807c478bd9Sstevel@tonic-gate {
27817c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_encrypt, encrypt);
27827c478bd9Sstevel@tonic-gate 	crypto_session_id_t session_id;
27837c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
27842d794da1Skrishna 	crypto_session_data_t *sp = NULL;
27857c478bd9Sstevel@tonic-gate 	crypto_ctx_t **ctxpp;
27867c478bd9Sstevel@tonic-gate 	crypto_data_t data, encr;
27877c478bd9Sstevel@tonic-gate 	size_t datalen, encrlen, need = 0;
278887fa5c53Smcpowers 	boolean_t do_inplace;
27897c478bd9Sstevel@tonic-gate 	char *encrbuf;
27907c478bd9Sstevel@tonic-gate 	int error = 0;
27917c478bd9Sstevel@tonic-gate 	int rv;
27922d794da1Skrishna 	boolean_t rctl_chk = B_FALSE;
27937c478bd9Sstevel@tonic-gate 
2794*c846684cSToomas Soome 	do_inplace = B_FALSE;
27957c478bd9Sstevel@tonic-gate 	STRUCT_INIT(encrypt, mode);
27967c478bd9Sstevel@tonic-gate 
27977c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
27987c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "cipher: failed holding minor");
27997c478bd9Sstevel@tonic-gate 		return (ENXIO);
28007c478bd9Sstevel@tonic-gate 	}
28017c478bd9Sstevel@tonic-gate 
28027c478bd9Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(encrypt), STRUCT_SIZE(encrypt)) != 0) {
28037c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
28047c478bd9Sstevel@tonic-gate 		return (EFAULT);
28057c478bd9Sstevel@tonic-gate 	}
28067c478bd9Sstevel@tonic-gate 
28077c478bd9Sstevel@tonic-gate 	data.cd_raw.iov_base = NULL;
28087c478bd9Sstevel@tonic-gate 	encr.cd_raw.iov_base = NULL;
28097c478bd9Sstevel@tonic-gate 
28107c478bd9Sstevel@tonic-gate 	datalen = STRUCT_FGET(encrypt, ce_datalen);
28117c478bd9Sstevel@tonic-gate 	encrlen = STRUCT_FGET(encrypt, ce_encrlen);
28127c478bd9Sstevel@tonic-gate 
28137c478bd9Sstevel@tonic-gate 	/*
28147c478bd9Sstevel@tonic-gate 	 * Don't allocate output buffer unless both buffer pointer and
28157c478bd9Sstevel@tonic-gate 	 * buffer length are not NULL or 0 (length).
28167c478bd9Sstevel@tonic-gate 	 */
28177c478bd9Sstevel@tonic-gate 	encrbuf = STRUCT_FGETP(encrypt, ce_encrbuf);
28187c478bd9Sstevel@tonic-gate 	if (encrbuf == NULL || encrlen == 0) {
28197c478bd9Sstevel@tonic-gate 		encrlen = 0;
28207c478bd9Sstevel@tonic-gate 	}
28217c478bd9Sstevel@tonic-gate 
28227c478bd9Sstevel@tonic-gate 	if (datalen > crypto_max_buffer_len ||
28237c478bd9Sstevel@tonic-gate 	    encrlen > crypto_max_buffer_len) {
28247c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "cipher: buffer greater than %ld bytes, "
28257c478bd9Sstevel@tonic-gate 		    "pid = %d", crypto_max_buffer_len, curproc->p_pid);
28267c478bd9Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
28277c478bd9Sstevel@tonic-gate 		goto release_minor;
28287c478bd9Sstevel@tonic-gate 	}
28297c478bd9Sstevel@tonic-gate 
28302d794da1Skrishna 	session_id = STRUCT_FGET(encrypt, ce_session);
28312d794da1Skrishna 
28322d794da1Skrishna 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv))  {
28332d794da1Skrishna 		goto release_minor;
28342d794da1Skrishna 	}
283566761628Skrishna 
283666761628Skrishna 	do_inplace = (STRUCT_FGET(encrypt, ce_flags) &
283766761628Skrishna 	    CRYPTO_INPLACE_OPERATION) != 0;
283866761628Skrishna 	need = do_inplace ? datalen : datalen + encrlen;
283966761628Skrishna 
28402d794da1Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, need, rctl_chk)) !=
28412d794da1Skrishna 	    CRYPTO_SUCCESS) {
28427c478bd9Sstevel@tonic-gate 		need = 0;
28437c478bd9Sstevel@tonic-gate 		goto release_minor;
28447c478bd9Sstevel@tonic-gate 	}
28457c478bd9Sstevel@tonic-gate 
28467c478bd9Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(data, datalen);
28477c478bd9Sstevel@tonic-gate 	data.cd_miscdata = NULL;
28487c478bd9Sstevel@tonic-gate 
28497c478bd9Sstevel@tonic-gate 	if (datalen != 0 && copyin(STRUCT_FGETP(encrypt, ce_databuf),
28507c478bd9Sstevel@tonic-gate 	    data.cd_raw.iov_base, datalen) != 0) {
28517c478bd9Sstevel@tonic-gate 		error = EFAULT;
28527c478bd9Sstevel@tonic-gate 		goto release_minor;
28537c478bd9Sstevel@tonic-gate 	}
28547c478bd9Sstevel@tonic-gate 
285587fa5c53Smcpowers 	if (do_inplace) {
285687fa5c53Smcpowers 		/* set out = in for in-place */
285787fa5c53Smcpowers 		encr = data;
285887fa5c53Smcpowers 	} else {
285987fa5c53Smcpowers 		INIT_RAW_CRYPTO_DATA(encr, encrlen);
286087fa5c53Smcpowers 	}
28617c478bd9Sstevel@tonic-gate 
28627c478bd9Sstevel@tonic-gate 	ctxpp = (single == crypto_encrypt_single) ?
28637c478bd9Sstevel@tonic-gate 	    &sp->sd_encr_ctx : &sp->sd_decr_ctx;
28647c478bd9Sstevel@tonic-gate 
286587fa5c53Smcpowers 	if (do_inplace)
28666d2259e1SDan OpenSolaris Anderson 		/* specify in-place buffers with output = NULL */
286787fa5c53Smcpowers 		rv = (single)(*ctxpp, &encr, NULL, NULL);
286887fa5c53Smcpowers 	else
286987fa5c53Smcpowers 		rv = (single)(*ctxpp, &data, &encr, NULL);
28706d2259e1SDan OpenSolaris Anderson 
28717c478bd9Sstevel@tonic-gate 	if (KCF_CONTEXT_DONE(rv))
28727c478bd9Sstevel@tonic-gate 		*ctxpp = NULL;
28737c478bd9Sstevel@tonic-gate 
28747c478bd9Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
28757c478bd9Sstevel@tonic-gate 		ASSERT(encr.cd_length <= encrlen);
28767c478bd9Sstevel@tonic-gate 		if (encr.cd_length != 0 && copyout(encr.cd_raw.iov_base,
28777c478bd9Sstevel@tonic-gate 		    encrbuf, encr.cd_length) != 0) {
28787c478bd9Sstevel@tonic-gate 			error = EFAULT;
28797c478bd9Sstevel@tonic-gate 			goto release_minor;
28807c478bd9Sstevel@tonic-gate 		}
288195014fbbSDan OpenSolaris Anderson 		STRUCT_FSET(encrypt, ce_encrlen,
288295014fbbSDan OpenSolaris Anderson 		    (ulong_t)encr.cd_length);
28837c478bd9Sstevel@tonic-gate 	}
28847c478bd9Sstevel@tonic-gate 
28857c478bd9Sstevel@tonic-gate 	if (rv == CRYPTO_BUFFER_TOO_SMALL) {
28867c478bd9Sstevel@tonic-gate 		/*
28877c478bd9Sstevel@tonic-gate 		 * The providers return CRYPTO_BUFFER_TOO_SMALL even for case 1
28887c478bd9Sstevel@tonic-gate 		 * of section 11.2 of the pkcs11 spec. We catch it here and
28897c478bd9Sstevel@tonic-gate 		 * provide the correct pkcs11 return value.
28907c478bd9Sstevel@tonic-gate 		 */
28917c478bd9Sstevel@tonic-gate 		if (STRUCT_FGETP(encrypt, ce_encrbuf) == NULL)
28927c478bd9Sstevel@tonic-gate 			rv = CRYPTO_SUCCESS;
289395014fbbSDan OpenSolaris Anderson 		STRUCT_FSET(encrypt, ce_encrlen,
289495014fbbSDan OpenSolaris Anderson 		    (ulong_t)encr.cd_length);
28957c478bd9Sstevel@tonic-gate 	}
28967c478bd9Sstevel@tonic-gate 
28977c478bd9Sstevel@tonic-gate release_minor:
28982d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
28992d794da1Skrishna 	CRYPTO_SESSION_RELE(sp);
29007c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
29017c478bd9Sstevel@tonic-gate 
29027c478bd9Sstevel@tonic-gate 	if (data.cd_raw.iov_base != NULL)
29037c478bd9Sstevel@tonic-gate 		kmem_free(data.cd_raw.iov_base, datalen);
29047c478bd9Sstevel@tonic-gate 
290587fa5c53Smcpowers 	if (!do_inplace && encr.cd_raw.iov_base != NULL)
29067c478bd9Sstevel@tonic-gate 		kmem_free(encr.cd_raw.iov_base, encrlen);
29077c478bd9Sstevel@tonic-gate 
29087c478bd9Sstevel@tonic-gate 	if (error != 0)
29097c478bd9Sstevel@tonic-gate 		return (error);
29107c478bd9Sstevel@tonic-gate 
29117c478bd9Sstevel@tonic-gate 	STRUCT_FSET(encrypt, ce_return_value, rv);
29127c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(encrypt), arg, STRUCT_SIZE(encrypt)) != 0) {
29137c478bd9Sstevel@tonic-gate 		return (EFAULT);
29147c478bd9Sstevel@tonic-gate 	}
29157c478bd9Sstevel@tonic-gate 	return (0);
29167c478bd9Sstevel@tonic-gate }
29177c478bd9Sstevel@tonic-gate 
29187c478bd9Sstevel@tonic-gate /* ARGSUSED */
29197c478bd9Sstevel@tonic-gate static int
encrypt_update(dev_t dev,caddr_t arg,int mode,int * rval)29207c478bd9Sstevel@tonic-gate encrypt_update(dev_t dev, caddr_t arg, int mode, int *rval)
29217c478bd9Sstevel@tonic-gate {
29227c478bd9Sstevel@tonic-gate 	return (cipher_update(dev, arg, mode, crypto_encrypt_update));
29237c478bd9Sstevel@tonic-gate }
29247c478bd9Sstevel@tonic-gate 
29257c478bd9Sstevel@tonic-gate /* ARGSUSED */
29267c478bd9Sstevel@tonic-gate static int
decrypt_update(dev_t dev,caddr_t arg,int mode,int * rval)29277c478bd9Sstevel@tonic-gate decrypt_update(dev_t dev, caddr_t arg, int mode, int *rval)
29287c478bd9Sstevel@tonic-gate {
29297c478bd9Sstevel@tonic-gate 	return (cipher_update(dev, arg, mode, crypto_decrypt_update));
29307c478bd9Sstevel@tonic-gate }
29317c478bd9Sstevel@tonic-gate 
29327c478bd9Sstevel@tonic-gate /*
29337c478bd9Sstevel@tonic-gate  * ASSUMPTION: crypto_encrypt_update and crypto_decrypt_update
29347c478bd9Sstevel@tonic-gate  * structures are identical except for field names.
29357c478bd9Sstevel@tonic-gate  */
29367c478bd9Sstevel@tonic-gate static int
cipher_update(dev_t dev,caddr_t arg,int mode,int (* update)(crypto_context_t,crypto_data_t *,crypto_data_t *,crypto_call_req_t *))29377c478bd9Sstevel@tonic-gate cipher_update(dev_t dev, caddr_t arg, int mode,
29387c478bd9Sstevel@tonic-gate     int (*update)(crypto_context_t, crypto_data_t *, crypto_data_t *,
29397c478bd9Sstevel@tonic-gate     crypto_call_req_t *))
29407c478bd9Sstevel@tonic-gate {
29417c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_encrypt_update, encrypt_update);
29427c478bd9Sstevel@tonic-gate 	crypto_session_id_t session_id;
29437c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
29442d794da1Skrishna 	crypto_session_data_t *sp = NULL;
29457c478bd9Sstevel@tonic-gate 	crypto_ctx_t **ctxpp;
29467c478bd9Sstevel@tonic-gate 	crypto_data_t data, encr;
29477c478bd9Sstevel@tonic-gate 	size_t datalen, encrlen, need = 0;
29486d2259e1SDan OpenSolaris Anderson 	boolean_t do_inplace;
29497c478bd9Sstevel@tonic-gate 	char *encrbuf;
29507c478bd9Sstevel@tonic-gate 	int error = 0;
29517c478bd9Sstevel@tonic-gate 	int rv;
29522d794da1Skrishna 	boolean_t rctl_chk = B_FALSE;
29537c478bd9Sstevel@tonic-gate 
2954*c846684cSToomas Soome 	do_inplace = B_FALSE;
29557c478bd9Sstevel@tonic-gate 	STRUCT_INIT(encrypt_update, mode);
29567c478bd9Sstevel@tonic-gate 
29577c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
29587c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "cipher_update: failed holding minor");
29597c478bd9Sstevel@tonic-gate 		return (ENXIO);
29607c478bd9Sstevel@tonic-gate 	}
29617c478bd9Sstevel@tonic-gate 
29627c478bd9Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(encrypt_update),
29637c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(encrypt_update)) != 0) {
29647c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
29657c478bd9Sstevel@tonic-gate 		return (EFAULT);
29667c478bd9Sstevel@tonic-gate 	}
29677c478bd9Sstevel@tonic-gate 
29687c478bd9Sstevel@tonic-gate 	data.cd_raw.iov_base = NULL;
29697c478bd9Sstevel@tonic-gate 	encr.cd_raw.iov_base = NULL;
29707c478bd9Sstevel@tonic-gate 
29717c478bd9Sstevel@tonic-gate 	datalen = STRUCT_FGET(encrypt_update, eu_datalen);
29727c478bd9Sstevel@tonic-gate 	encrlen = STRUCT_FGET(encrypt_update, eu_encrlen);
29737c478bd9Sstevel@tonic-gate 
29747c478bd9Sstevel@tonic-gate 	/*
29757c478bd9Sstevel@tonic-gate 	 * Don't allocate output buffer unless both buffer pointer and
29767c478bd9Sstevel@tonic-gate 	 * buffer length are not NULL or 0 (length).
29777c478bd9Sstevel@tonic-gate 	 */
29787c478bd9Sstevel@tonic-gate 	encrbuf = STRUCT_FGETP(encrypt_update, eu_encrbuf);
29797c478bd9Sstevel@tonic-gate 	if (encrbuf == NULL || encrlen == 0) {
29807c478bd9Sstevel@tonic-gate 		encrlen = 0;
29817c478bd9Sstevel@tonic-gate 	}
29827c478bd9Sstevel@tonic-gate 
29837c478bd9Sstevel@tonic-gate 	if (datalen > crypto_max_buffer_len ||
29847c478bd9Sstevel@tonic-gate 	    encrlen > crypto_max_buffer_len) {
29857c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "cipher_update: buffer greater than %ld "
29867c478bd9Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
29877c478bd9Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
29882d794da1Skrishna 		goto out;
29892d794da1Skrishna 	}
29902d794da1Skrishna 
29912d794da1Skrishna 	session_id = STRUCT_FGET(encrypt_update, eu_session);
29922d794da1Skrishna 
29932d794da1Skrishna 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv))  {
29942d794da1Skrishna 		goto out;
29957c478bd9Sstevel@tonic-gate 	}
29967c478bd9Sstevel@tonic-gate 
29976d2259e1SDan OpenSolaris Anderson 	do_inplace = (STRUCT_FGET(encrypt_update, eu_flags) &
29986d2259e1SDan OpenSolaris Anderson 	    CRYPTO_INPLACE_OPERATION) != 0;
29996d2259e1SDan OpenSolaris Anderson 	need = do_inplace ? datalen : datalen + encrlen;
30002d794da1Skrishna 
30012d794da1Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, need, rctl_chk)) !=
30022d794da1Skrishna 	    CRYPTO_SUCCESS) {
30037c478bd9Sstevel@tonic-gate 		need = 0;
30042d794da1Skrishna 		goto out;
30057c478bd9Sstevel@tonic-gate 	}
30067c478bd9Sstevel@tonic-gate 
30077c478bd9Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(data, datalen);
30087c478bd9Sstevel@tonic-gate 	data.cd_miscdata = NULL;
30097c478bd9Sstevel@tonic-gate 
30107c478bd9Sstevel@tonic-gate 	if (datalen != 0 && copyin(STRUCT_FGETP(encrypt_update, eu_databuf),
30117c478bd9Sstevel@tonic-gate 	    data.cd_raw.iov_base, datalen) != 0) {
30127c478bd9Sstevel@tonic-gate 		error = EFAULT;
30132d794da1Skrishna 		goto out;
30147c478bd9Sstevel@tonic-gate 	}
30157c478bd9Sstevel@tonic-gate 
30166d2259e1SDan OpenSolaris Anderson 	if (do_inplace) {
30176d2259e1SDan OpenSolaris Anderson 		/* specify in-place buffers with output = input */
30186d2259e1SDan OpenSolaris Anderson 		encr = data;
30196d2259e1SDan OpenSolaris Anderson 	} else {
30206d2259e1SDan OpenSolaris Anderson 		INIT_RAW_CRYPTO_DATA(encr, encrlen);
30216d2259e1SDan OpenSolaris Anderson 	}
30227c478bd9Sstevel@tonic-gate 
30237c478bd9Sstevel@tonic-gate 	ctxpp = (update == crypto_encrypt_update) ?
30247c478bd9Sstevel@tonic-gate 	    &sp->sd_encr_ctx : &sp->sd_decr_ctx;
30257c478bd9Sstevel@tonic-gate 
30266d2259e1SDan OpenSolaris Anderson 	if (do_inplace)
30276d2259e1SDan OpenSolaris Anderson 		/* specify in-place buffers with output = NULL */
30286d2259e1SDan OpenSolaris Anderson 		rv = (update)(*ctxpp, &encr, NULL, NULL);
30296d2259e1SDan OpenSolaris Anderson 	else
30306d2259e1SDan OpenSolaris Anderson 		rv = (update)(*ctxpp, &data, &encr, NULL);
30317c478bd9Sstevel@tonic-gate 
30327c478bd9Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS || rv == CRYPTO_BUFFER_TOO_SMALL) {
30337c478bd9Sstevel@tonic-gate 		if (rv == CRYPTO_SUCCESS) {
30347c478bd9Sstevel@tonic-gate 			ASSERT(encr.cd_length <= encrlen);
30357c478bd9Sstevel@tonic-gate 			if (encr.cd_length != 0 && copyout(encr.cd_raw.iov_base,
30367c478bd9Sstevel@tonic-gate 			    encrbuf, encr.cd_length) != 0) {
30377c478bd9Sstevel@tonic-gate 				error = EFAULT;
30387c478bd9Sstevel@tonic-gate 				goto out;
30397c478bd9Sstevel@tonic-gate 			}
30407c478bd9Sstevel@tonic-gate 		} else {
30417c478bd9Sstevel@tonic-gate 			/*
30427c478bd9Sstevel@tonic-gate 			 * The providers return CRYPTO_BUFFER_TOO_SMALL even
30437c478bd9Sstevel@tonic-gate 			 * for case 1 of section 11.2 of the pkcs11 spec.
30447c478bd9Sstevel@tonic-gate 			 * We catch it here and provide the correct pkcs11
30457c478bd9Sstevel@tonic-gate 			 * return value.
30467c478bd9Sstevel@tonic-gate 			 */
30477c478bd9Sstevel@tonic-gate 			if (STRUCT_FGETP(encrypt_update, eu_encrbuf) == NULL)
30487c478bd9Sstevel@tonic-gate 				rv = CRYPTO_SUCCESS;
30497c478bd9Sstevel@tonic-gate 		}
305095014fbbSDan OpenSolaris Anderson 		STRUCT_FSET(encrypt_update, eu_encrlen,
305195014fbbSDan OpenSolaris Anderson 		    (ulong_t)encr.cd_length);
30527c478bd9Sstevel@tonic-gate 	} else {
30537c478bd9Sstevel@tonic-gate 		CRYPTO_CANCEL_CTX(ctxpp);
30547c478bd9Sstevel@tonic-gate 	}
30557c478bd9Sstevel@tonic-gate out:
30562d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
30577c478bd9Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
30587c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
30597c478bd9Sstevel@tonic-gate 
30607c478bd9Sstevel@tonic-gate 	if (data.cd_raw.iov_base != NULL)
30617c478bd9Sstevel@tonic-gate 		kmem_free(data.cd_raw.iov_base, datalen);
30627c478bd9Sstevel@tonic-gate 
30636d2259e1SDan OpenSolaris Anderson 	if (!do_inplace && (encr.cd_raw.iov_base != NULL))
30647c478bd9Sstevel@tonic-gate 		kmem_free(encr.cd_raw.iov_base, encrlen);
30657c478bd9Sstevel@tonic-gate 
30667c478bd9Sstevel@tonic-gate 	if (error != 0)
30677c478bd9Sstevel@tonic-gate 		return (error);
30687c478bd9Sstevel@tonic-gate 
30697c478bd9Sstevel@tonic-gate 	STRUCT_FSET(encrypt_update, eu_return_value, rv);
30707c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(encrypt_update), arg,
30717c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(encrypt_update)) != 0) {
30727c478bd9Sstevel@tonic-gate 		return (EFAULT);
30737c478bd9Sstevel@tonic-gate 	}
30747c478bd9Sstevel@tonic-gate 	return (0);
30757c478bd9Sstevel@tonic-gate }
30767c478bd9Sstevel@tonic-gate 
30777c478bd9Sstevel@tonic-gate /* ARGSUSED */
30787c478bd9Sstevel@tonic-gate static int
encrypt_final(dev_t dev,caddr_t arg,int mode,int * rval)30797c478bd9Sstevel@tonic-gate encrypt_final(dev_t dev, caddr_t arg, int mode, int *rval)
30807c478bd9Sstevel@tonic-gate {
30817c478bd9Sstevel@tonic-gate 	return (common_final(dev, arg, mode, crypto_encrypt_final));
30827c478bd9Sstevel@tonic-gate }
30837c478bd9Sstevel@tonic-gate 
30847c478bd9Sstevel@tonic-gate /* ARGSUSED */
30857c478bd9Sstevel@tonic-gate static int
decrypt_final(dev_t dev,caddr_t arg,int mode,int * rval)30867c478bd9Sstevel@tonic-gate decrypt_final(dev_t dev, caddr_t arg, int mode, int *rval)
30877c478bd9Sstevel@tonic-gate {
30887c478bd9Sstevel@tonic-gate 	return (common_final(dev, arg, mode, crypto_decrypt_final));
30897c478bd9Sstevel@tonic-gate }
30907c478bd9Sstevel@tonic-gate 
30917c478bd9Sstevel@tonic-gate /*
30927c478bd9Sstevel@tonic-gate  * ASSUMPTION: crypto_encrypt_final, crypto_decrypt_final, crypto_sign_final,
30937c478bd9Sstevel@tonic-gate  * and crypto_digest_final structures are identical except for field names.
30947c478bd9Sstevel@tonic-gate  */
30957c478bd9Sstevel@tonic-gate static int
common_final(dev_t dev,caddr_t arg,int mode,int (* final)(crypto_context_t,crypto_data_t *,crypto_call_req_t *))30967c478bd9Sstevel@tonic-gate common_final(dev_t dev, caddr_t arg, int mode,
30977c478bd9Sstevel@tonic-gate     int (*final)(crypto_context_t, crypto_data_t *, crypto_call_req_t *))
30987c478bd9Sstevel@tonic-gate {
30997c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_encrypt_final, encrypt_final);
31007c478bd9Sstevel@tonic-gate 	crypto_session_id_t session_id;
31017c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
31022d794da1Skrishna 	crypto_session_data_t *sp = NULL;
31037c478bd9Sstevel@tonic-gate 	crypto_ctx_t **ctxpp;
31047c478bd9Sstevel@tonic-gate 	crypto_data_t encr;
31057c478bd9Sstevel@tonic-gate 	size_t encrlen, need = 0;
31067c478bd9Sstevel@tonic-gate 	char *encrbuf;
31077c478bd9Sstevel@tonic-gate 	int error = 0;
31087c478bd9Sstevel@tonic-gate 	int rv;
31092d794da1Skrishna 	boolean_t rctl_chk = B_FALSE;
31107c478bd9Sstevel@tonic-gate 
31117c478bd9Sstevel@tonic-gate 	STRUCT_INIT(encrypt_final, mode);
31127c478bd9Sstevel@tonic-gate 
31137c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
31147c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "common_final: failed holding minor");
31157c478bd9Sstevel@tonic-gate 		return (ENXIO);
31167c478bd9Sstevel@tonic-gate 	}
31177c478bd9Sstevel@tonic-gate 
31187c478bd9Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(encrypt_final),
31197c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(encrypt_final)) != 0) {
31207c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
31217c478bd9Sstevel@tonic-gate 		return (EFAULT);
31227c478bd9Sstevel@tonic-gate 	}
31237c478bd9Sstevel@tonic-gate 
31247c478bd9Sstevel@tonic-gate 	encr.cd_format = CRYPTO_DATA_RAW;
31257c478bd9Sstevel@tonic-gate 	encr.cd_raw.iov_base = NULL;
31267c478bd9Sstevel@tonic-gate 
31277c478bd9Sstevel@tonic-gate 	encrlen = STRUCT_FGET(encrypt_final, ef_encrlen);
31287c478bd9Sstevel@tonic-gate 
31297c478bd9Sstevel@tonic-gate 	/*
31307c478bd9Sstevel@tonic-gate 	 * Don't allocate output buffer unless both buffer pointer and
31317c478bd9Sstevel@tonic-gate 	 * buffer length are not NULL or 0 (length).
31327c478bd9Sstevel@tonic-gate 	 */
31337c478bd9Sstevel@tonic-gate 	encrbuf = STRUCT_FGETP(encrypt_final, ef_encrbuf);
31347c478bd9Sstevel@tonic-gate 	if (encrbuf == NULL || encrlen == 0) {
31357c478bd9Sstevel@tonic-gate 		encrlen = 0;
31367c478bd9Sstevel@tonic-gate 	}
31377c478bd9Sstevel@tonic-gate 
31387c478bd9Sstevel@tonic-gate 	if (encrlen > crypto_max_buffer_len) {
31397c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "common_final: buffer greater than %ld "
31407c478bd9Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
31417c478bd9Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
31427c478bd9Sstevel@tonic-gate 		goto release_minor;
31437c478bd9Sstevel@tonic-gate 	}
31447c478bd9Sstevel@tonic-gate 
31452d794da1Skrishna 	session_id = STRUCT_FGET(encrypt_final, ef_session);
31462d794da1Skrishna 
31472d794da1Skrishna 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
31482d794da1Skrishna 		goto release_minor;
31492d794da1Skrishna 	}
31502d794da1Skrishna 
31512d794da1Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, encrlen, rctl_chk)) !=
31522d794da1Skrishna 	    CRYPTO_SUCCESS) {
31537c478bd9Sstevel@tonic-gate 		goto release_minor;
31547c478bd9Sstevel@tonic-gate 	}
31557c478bd9Sstevel@tonic-gate 	need = encrlen;
31567c478bd9Sstevel@tonic-gate 	encr.cd_raw.iov_base = kmem_alloc(encrlen, KM_SLEEP);
31577c478bd9Sstevel@tonic-gate 	encr.cd_raw.iov_len = encrlen;
31587c478bd9Sstevel@tonic-gate 
31597c478bd9Sstevel@tonic-gate 	encr.cd_offset = 0;
31607c478bd9Sstevel@tonic-gate 	encr.cd_length = encrlen;
31617c478bd9Sstevel@tonic-gate 
31627c478bd9Sstevel@tonic-gate 	ASSERT(final == crypto_encrypt_final ||
31637c478bd9Sstevel@tonic-gate 	    final == crypto_decrypt_final || final == crypto_sign_final ||
3164cd964fceSMatt Barden 	    final == crypto_digest_final || final == crypto_mac_final);
31657c478bd9Sstevel@tonic-gate 
31667c478bd9Sstevel@tonic-gate 	if (final == crypto_encrypt_final) {
31677c478bd9Sstevel@tonic-gate 		ctxpp = &sp->sd_encr_ctx;
31687c478bd9Sstevel@tonic-gate 	} else if (final == crypto_decrypt_final) {
31697c478bd9Sstevel@tonic-gate 		ctxpp = &sp->sd_decr_ctx;
31707c478bd9Sstevel@tonic-gate 	} else if (final == crypto_sign_final) {
31717c478bd9Sstevel@tonic-gate 		ctxpp = &sp->sd_sign_ctx;
3172cd964fceSMatt Barden 	} else if (final == crypto_mac_final) {
3173cd964fceSMatt Barden 		ctxpp = &sp->sd_mac_ctx;
31747c478bd9Sstevel@tonic-gate 	} else {
31757c478bd9Sstevel@tonic-gate 		ctxpp = &sp->sd_digest_ctx;
31767c478bd9Sstevel@tonic-gate 	}
31777c478bd9Sstevel@tonic-gate 
31787c478bd9Sstevel@tonic-gate 	rv = (final)(*ctxpp, &encr, NULL);
31797c478bd9Sstevel@tonic-gate 	if (KCF_CONTEXT_DONE(rv))
31807c478bd9Sstevel@tonic-gate 		*ctxpp = NULL;
31817c478bd9Sstevel@tonic-gate 
31827c478bd9Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
31837c478bd9Sstevel@tonic-gate 		ASSERT(encr.cd_length <= encrlen);
31847c478bd9Sstevel@tonic-gate 		if (encr.cd_length != 0 && copyout(encr.cd_raw.iov_base,
31857c478bd9Sstevel@tonic-gate 		    encrbuf, encr.cd_length) != 0) {
31867c478bd9Sstevel@tonic-gate 			error = EFAULT;
31877c478bd9Sstevel@tonic-gate 			goto release_minor;
31887c478bd9Sstevel@tonic-gate 		}
318995014fbbSDan OpenSolaris Anderson 		STRUCT_FSET(encrypt_final, ef_encrlen,
319095014fbbSDan OpenSolaris Anderson 		    (ulong_t)encr.cd_length);
31917c478bd9Sstevel@tonic-gate 	}
31927c478bd9Sstevel@tonic-gate 
31937c478bd9Sstevel@tonic-gate 	if (rv == CRYPTO_BUFFER_TOO_SMALL) {
31947c478bd9Sstevel@tonic-gate 		/*
31957c478bd9Sstevel@tonic-gate 		 * The providers return CRYPTO_BUFFER_TOO_SMALL even for case 1
31967c478bd9Sstevel@tonic-gate 		 * of section 11.2 of the pkcs11 spec. We catch it here and
31977c478bd9Sstevel@tonic-gate 		 * provide the correct pkcs11 return value.
31987c478bd9Sstevel@tonic-gate 		 */
31997c478bd9Sstevel@tonic-gate 		if (STRUCT_FGETP(encrypt_final, ef_encrbuf) == NULL)
32007c478bd9Sstevel@tonic-gate 			rv = CRYPTO_SUCCESS;
320195014fbbSDan OpenSolaris Anderson 		STRUCT_FSET(encrypt_final, ef_encrlen,
320295014fbbSDan OpenSolaris Anderson 		    (ulong_t)encr.cd_length);
32037c478bd9Sstevel@tonic-gate 	}
32047c478bd9Sstevel@tonic-gate 
32057c478bd9Sstevel@tonic-gate release_minor:
32062d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
32072d794da1Skrishna 	CRYPTO_SESSION_RELE(sp);
32087c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
32097c478bd9Sstevel@tonic-gate 
32107c478bd9Sstevel@tonic-gate 	if (encr.cd_raw.iov_base != NULL)
32117c478bd9Sstevel@tonic-gate 		kmem_free(encr.cd_raw.iov_base, encrlen);
32127c478bd9Sstevel@tonic-gate 
32137c478bd9Sstevel@tonic-gate 	if (error != 0)
32147c478bd9Sstevel@tonic-gate 		return (error);
32157c478bd9Sstevel@tonic-gate 
32167c478bd9Sstevel@tonic-gate 	STRUCT_FSET(encrypt_final, ef_return_value, rv);
32177c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(encrypt_final), arg,
32187c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(encrypt_final)) != 0) {
32197c478bd9Sstevel@tonic-gate 		return (EFAULT);
32207c478bd9Sstevel@tonic-gate 	}
32217c478bd9Sstevel@tonic-gate 	return (0);
32227c478bd9Sstevel@tonic-gate }
32237c478bd9Sstevel@tonic-gate 
32247c478bd9Sstevel@tonic-gate /* ARGSUSED */
32257c478bd9Sstevel@tonic-gate static int
digest_init(dev_t dev,caddr_t arg,int mode,int * rval)32267c478bd9Sstevel@tonic-gate digest_init(dev_t dev, caddr_t arg, int mode, int *rval)
32277c478bd9Sstevel@tonic-gate {
32287c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_digest_init, digest_init);
3229894b2776Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
32307c478bd9Sstevel@tonic-gate 	crypto_session_id_t session_id;
32317c478bd9Sstevel@tonic-gate 	crypto_mechanism_t mech;
32327c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
32332d794da1Skrishna 	crypto_session_data_t *sp = NULL;
32347c478bd9Sstevel@tonic-gate 	crypto_context_t cc;
32357c478bd9Sstevel@tonic-gate 	size_t rctl_bytes = 0;
32362d794da1Skrishna 	boolean_t rctl_chk = B_FALSE;
32377c478bd9Sstevel@tonic-gate 	int error = 0;
32387c478bd9Sstevel@tonic-gate 	int rv;
32397c478bd9Sstevel@tonic-gate 
32407c478bd9Sstevel@tonic-gate 	STRUCT_INIT(digest_init, mode);
32417c478bd9Sstevel@tonic-gate 
32427c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
32437c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "digest_init: failed holding minor");
32447c478bd9Sstevel@tonic-gate 		return (ENXIO);
32457c478bd9Sstevel@tonic-gate 	}
32467c478bd9Sstevel@tonic-gate 
32477c478bd9Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(digest_init),
32487c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(digest_init)) != 0) {
32497c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
32507c478bd9Sstevel@tonic-gate 		return (EFAULT);
32517c478bd9Sstevel@tonic-gate 	}
32527c478bd9Sstevel@tonic-gate 
32537c478bd9Sstevel@tonic-gate 	mech.cm_param = NULL;
32547c478bd9Sstevel@tonic-gate 
32557c478bd9Sstevel@tonic-gate 	session_id = STRUCT_FGET(digest_init, di_session);
32567c478bd9Sstevel@tonic-gate 
32577c478bd9Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv))  {
32582d794da1Skrishna 		goto out;
32597c478bd9Sstevel@tonic-gate 	}
32607c478bd9Sstevel@tonic-gate 
32612d794da1Skrishna 	if (!copyin_mech(mode, sp, STRUCT_FADDR(digest_init, di_mech), &mech,
32622d794da1Skrishna 	    &rctl_bytes, &rctl_chk, &rv, &error)) {
32637c478bd9Sstevel@tonic-gate 		goto out;
32647c478bd9Sstevel@tonic-gate 	}
32657c478bd9Sstevel@tonic-gate 
3266436935a1SVladimir Kotal 	if ((rv = kcf_get_hardware_provider(mech.cm_type, NULL,
32679b009fc1SValerie Bubb Fenwick 	    CRYPTO_MECH_INVALID, NULL, sp->sd_provider, &real_provider,
32689b009fc1SValerie Bubb Fenwick 	    CRYPTO_FG_DIGEST)) != CRYPTO_SUCCESS) {
32697c478bd9Sstevel@tonic-gate 		goto out;
32707c478bd9Sstevel@tonic-gate 	}
32717c478bd9Sstevel@tonic-gate 
32727c478bd9Sstevel@tonic-gate 	rv = crypto_digest_init_prov(real_provider,
32737c478bd9Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, &mech, &cc, NULL);
32747c478bd9Sstevel@tonic-gate 
32757c478bd9Sstevel@tonic-gate 	/*
32767c478bd9Sstevel@tonic-gate 	 * Check if a context already exists. If so, it means it is being
32777c478bd9Sstevel@tonic-gate 	 * abandoned. So, cancel it to avoid leaking it.
32787c478bd9Sstevel@tonic-gate 	 */
32797c478bd9Sstevel@tonic-gate 	if (sp->sd_digest_ctx != NULL)
32807c478bd9Sstevel@tonic-gate 		CRYPTO_CANCEL_CTX(&sp->sd_digest_ctx);
32817c478bd9Sstevel@tonic-gate 	sp->sd_digest_ctx = (rv == CRYPTO_SUCCESS) ? cc : NULL;
32827c478bd9Sstevel@tonic-gate out:
32832d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk);
32847c478bd9Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
32857c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
32867c478bd9Sstevel@tonic-gate 
3287894b2776Smcpowers 	if (real_provider != NULL)
3288894b2776Smcpowers 		KCF_PROV_REFRELE(real_provider);
3289894b2776Smcpowers 
32907c478bd9Sstevel@tonic-gate 	if (mech.cm_param != NULL)
32917c478bd9Sstevel@tonic-gate 		kmem_free(mech.cm_param, mech.cm_param_len);
32927c478bd9Sstevel@tonic-gate 
32937c478bd9Sstevel@tonic-gate 	if (error != 0)
32947c478bd9Sstevel@tonic-gate 		return (error);
32957c478bd9Sstevel@tonic-gate 
32967c478bd9Sstevel@tonic-gate 	STRUCT_FSET(digest_init, di_return_value, rv);
32977c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(digest_init), arg,
32987c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(digest_init)) != 0) {
32997c478bd9Sstevel@tonic-gate 		return (EFAULT);
33007c478bd9Sstevel@tonic-gate 	}
33017c478bd9Sstevel@tonic-gate 	return (0);
33027c478bd9Sstevel@tonic-gate }
33037c478bd9Sstevel@tonic-gate 
33047c478bd9Sstevel@tonic-gate /* ARGSUSED */
33057c478bd9Sstevel@tonic-gate static int
digest_update(dev_t dev,caddr_t arg,int mode,int * rval)33067c478bd9Sstevel@tonic-gate digest_update(dev_t dev, caddr_t arg, int mode, int *rval)
33077c478bd9Sstevel@tonic-gate {
33087c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_digest_update, digest_update);
33097c478bd9Sstevel@tonic-gate 	crypto_session_id_t session_id;
33107c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
33112d794da1Skrishna 	crypto_session_data_t *sp = NULL;
33127c478bd9Sstevel@tonic-gate 	crypto_data_t data;
33137c478bd9Sstevel@tonic-gate 	size_t datalen, need = 0;
33147c478bd9Sstevel@tonic-gate 	int error = 0;
33157c478bd9Sstevel@tonic-gate 	int rv;
33162d794da1Skrishna 	boolean_t rctl_chk = B_FALSE;
33177c478bd9Sstevel@tonic-gate 
33187c478bd9Sstevel@tonic-gate 	STRUCT_INIT(digest_update, mode);
33197c478bd9Sstevel@tonic-gate 
33207c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
33217c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "digest_update: failed holding minor");
33227c478bd9Sstevel@tonic-gate 		return (ENXIO);
33237c478bd9Sstevel@tonic-gate 	}
33247c478bd9Sstevel@tonic-gate 
33257c478bd9Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(digest_update),
33267c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(digest_update)) != 0) {
33277c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
33287c478bd9Sstevel@tonic-gate 		return (EFAULT);
33297c478bd9Sstevel@tonic-gate 	}
33307c478bd9Sstevel@tonic-gate 
33317c478bd9Sstevel@tonic-gate 	data.cd_format = CRYPTO_DATA_RAW;
33327c478bd9Sstevel@tonic-gate 	data.cd_raw.iov_base = NULL;
33337c478bd9Sstevel@tonic-gate 
33347c478bd9Sstevel@tonic-gate 	datalen = STRUCT_FGET(digest_update, du_datalen);
33357c478bd9Sstevel@tonic-gate 	if (datalen > crypto_max_buffer_len) {
33367c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "digest_update: buffer greater than %ld "
33377c478bd9Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
33387c478bd9Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
33397c478bd9Sstevel@tonic-gate 		goto release_minor;
33407c478bd9Sstevel@tonic-gate 	}
33417c478bd9Sstevel@tonic-gate 
33422d794da1Skrishna 	session_id = STRUCT_FGET(digest_update, du_session);
33432d794da1Skrishna 
33442d794da1Skrishna 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv))  {
33457c478bd9Sstevel@tonic-gate 		goto release_minor;
33467c478bd9Sstevel@tonic-gate 	}
33472d794da1Skrishna 
33482d794da1Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, datalen, rctl_chk)) !=
33492d794da1Skrishna 	    CRYPTO_SUCCESS) {
33502d794da1Skrishna 		goto release_minor;
33512d794da1Skrishna 	}
33522d794da1Skrishna 
33537c478bd9Sstevel@tonic-gate 	need = datalen;
33547c478bd9Sstevel@tonic-gate 	data.cd_raw.iov_base = kmem_alloc(datalen, KM_SLEEP);
33557c478bd9Sstevel@tonic-gate 	data.cd_raw.iov_len = datalen;
33567c478bd9Sstevel@tonic-gate 
33577c478bd9Sstevel@tonic-gate 	if (datalen != 0 && copyin(STRUCT_FGETP(digest_update, du_databuf),
33587c478bd9Sstevel@tonic-gate 	    data.cd_raw.iov_base, datalen) != 0) {
33597c478bd9Sstevel@tonic-gate 		error = EFAULT;
33607c478bd9Sstevel@tonic-gate 		goto release_minor;
33617c478bd9Sstevel@tonic-gate 	}
33627c478bd9Sstevel@tonic-gate 
33637c478bd9Sstevel@tonic-gate 	data.cd_offset = 0;
33647c478bd9Sstevel@tonic-gate 	data.cd_length = datalen;
33657c478bd9Sstevel@tonic-gate 
33667c478bd9Sstevel@tonic-gate 	rv = crypto_digest_update(sp->sd_digest_ctx, &data, NULL);
33677c478bd9Sstevel@tonic-gate 	if (rv != CRYPTO_SUCCESS)
33687c478bd9Sstevel@tonic-gate 		CRYPTO_CANCEL_CTX(&sp->sd_digest_ctx);
33697c478bd9Sstevel@tonic-gate 
33707c478bd9Sstevel@tonic-gate release_minor:
33712d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
33722d794da1Skrishna 	CRYPTO_SESSION_RELE(sp);
33737c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
33747c478bd9Sstevel@tonic-gate 
33757c478bd9Sstevel@tonic-gate 	if (data.cd_raw.iov_base != NULL)
33767c478bd9Sstevel@tonic-gate 		kmem_free(data.cd_raw.iov_base, datalen);
33777c478bd9Sstevel@tonic-gate 
33787c478bd9Sstevel@tonic-gate 	if (error != 0)
33797c478bd9Sstevel@tonic-gate 		return (error);
33807c478bd9Sstevel@tonic-gate 
33817c478bd9Sstevel@tonic-gate 	STRUCT_FSET(digest_update, du_return_value, rv);
33827c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(digest_update), arg,
33837c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(digest_update)) != 0) {
33847c478bd9Sstevel@tonic-gate 		return (EFAULT);
33857c478bd9Sstevel@tonic-gate 	}
33867c478bd9Sstevel@tonic-gate 	return (0);
33877c478bd9Sstevel@tonic-gate }
33887c478bd9Sstevel@tonic-gate 
33897c478bd9Sstevel@tonic-gate /* ARGSUSED */
33907c478bd9Sstevel@tonic-gate static int
digest_key(dev_t dev,caddr_t arg,int mode,int * rval)33917c478bd9Sstevel@tonic-gate digest_key(dev_t dev, caddr_t arg, int mode, int *rval)
33927c478bd9Sstevel@tonic-gate {
33937c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_digest_key, digest_key);
33947c478bd9Sstevel@tonic-gate 	crypto_session_id_t session_id;
33957c478bd9Sstevel@tonic-gate 	crypto_key_t key;
33967c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
33972d794da1Skrishna 	crypto_session_data_t *sp = NULL;
33987c478bd9Sstevel@tonic-gate 	size_t rctl_bytes = 0;
33992d794da1Skrishna 	boolean_t key_rctl_chk = B_FALSE;
34007c478bd9Sstevel@tonic-gate 	int error = 0;
34017c478bd9Sstevel@tonic-gate 	int rv;
34027c478bd9Sstevel@tonic-gate 
34037c478bd9Sstevel@tonic-gate 	STRUCT_INIT(digest_key, mode);
34047c478bd9Sstevel@tonic-gate 
34057c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
34067c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "digest_key: failed holding minor");
34077c478bd9Sstevel@tonic-gate 		return (ENXIO);
34087c478bd9Sstevel@tonic-gate 	}
34097c478bd9Sstevel@tonic-gate 
34107c478bd9Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(digest_key), STRUCT_SIZE(digest_key)) != 0) {
34117c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
34127c478bd9Sstevel@tonic-gate 		return (EFAULT);
34137c478bd9Sstevel@tonic-gate 	}
34147c478bd9Sstevel@tonic-gate 
34157c478bd9Sstevel@tonic-gate 	bzero(&key, sizeof (crypto_key_t));
34167c478bd9Sstevel@tonic-gate 
34177c478bd9Sstevel@tonic-gate 	session_id = STRUCT_FGET(digest_key, dk_session);
34187c478bd9Sstevel@tonic-gate 
34197c478bd9Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv))  {
34202d794da1Skrishna 		goto out;
34217c478bd9Sstevel@tonic-gate 	}
34227c478bd9Sstevel@tonic-gate 
34232d794da1Skrishna 	if (!copyin_key(mode, sp, STRUCT_FADDR(digest_key, dk_key), &key,
34242d794da1Skrishna 	    &rctl_bytes, &key_rctl_chk, &rv, &error)) {
34257c478bd9Sstevel@tonic-gate 		goto out;
34267c478bd9Sstevel@tonic-gate 	}
34277c478bd9Sstevel@tonic-gate 
34287c478bd9Sstevel@tonic-gate 	rv = crypto_digest_key_prov(sp->sd_digest_ctx, &key, NULL);
34297c478bd9Sstevel@tonic-gate 	if (rv != CRYPTO_SUCCESS)
34307c478bd9Sstevel@tonic-gate 		CRYPTO_CANCEL_CTX(&sp->sd_digest_ctx);
34317c478bd9Sstevel@tonic-gate out:
34322d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, key_rctl_chk);
34337c478bd9Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
34347c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
34357c478bd9Sstevel@tonic-gate 
34367c478bd9Sstevel@tonic-gate 	free_crypto_key(&key);
34377c478bd9Sstevel@tonic-gate 
34387c478bd9Sstevel@tonic-gate 	if (error != 0)
34397c478bd9Sstevel@tonic-gate 		return (error);
34407c478bd9Sstevel@tonic-gate 
34417c478bd9Sstevel@tonic-gate 	STRUCT_FSET(digest_key, dk_return_value, rv);
34427c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(digest_key), arg,
34437c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(digest_key)) != 0) {
34447c478bd9Sstevel@tonic-gate 		return (EFAULT);
34457c478bd9Sstevel@tonic-gate 	}
34467c478bd9Sstevel@tonic-gate 	return (0);
34477c478bd9Sstevel@tonic-gate }
34487c478bd9Sstevel@tonic-gate 
34497c478bd9Sstevel@tonic-gate /* ARGSUSED */
34507c478bd9Sstevel@tonic-gate static int
digest_final(dev_t dev,caddr_t arg,int mode,int * rval)34517c478bd9Sstevel@tonic-gate digest_final(dev_t dev, caddr_t arg, int mode, int *rval)
34527c478bd9Sstevel@tonic-gate {
34537c478bd9Sstevel@tonic-gate 	return (common_final(dev, arg, mode, crypto_digest_final));
34547c478bd9Sstevel@tonic-gate }
34557c478bd9Sstevel@tonic-gate 
34567c478bd9Sstevel@tonic-gate /* ARGSUSED */
34577c478bd9Sstevel@tonic-gate static int
digest(dev_t dev,caddr_t arg,int mode,int * rval)34587c478bd9Sstevel@tonic-gate digest(dev_t dev, caddr_t arg, int mode, int *rval)
34597c478bd9Sstevel@tonic-gate {
34607c478bd9Sstevel@tonic-gate 	return (common_digest(dev, arg, mode, crypto_digest_single));
34617c478bd9Sstevel@tonic-gate }
34627c478bd9Sstevel@tonic-gate 
3463cd964fceSMatt Barden static int
mac_init(dev_t dev,caddr_t arg,int mode,int * rval)3464cd964fceSMatt Barden mac_init(dev_t dev, caddr_t arg, int mode, int *rval)
3465cd964fceSMatt Barden {
3466cd964fceSMatt Barden 	_NOTE(ARGUNUSED(rval))
3467cd964fceSMatt Barden 	return (sign_verify_init(dev, arg, mode, crypto_mac_init_prov));
3468cd964fceSMatt Barden }
3469cd964fceSMatt Barden 
3470cd964fceSMatt Barden static int
mac_update(dev_t dev,caddr_t arg,int mode,int * rval)3471cd964fceSMatt Barden mac_update(dev_t dev, caddr_t arg, int mode, int *rval)
3472cd964fceSMatt Barden {
3473cd964fceSMatt Barden 	_NOTE(ARGUNUSED(rval))
3474cd964fceSMatt Barden 	return (sign_verify_update(dev, arg, mode, crypto_mac_update));
3475cd964fceSMatt Barden }
3476cd964fceSMatt Barden 
3477cd964fceSMatt Barden static int
mac_final(dev_t dev,caddr_t arg,int mode,int * rval)3478cd964fceSMatt Barden mac_final(dev_t dev, caddr_t arg, int mode, int *rval)
3479cd964fceSMatt Barden {
3480cd964fceSMatt Barden 	_NOTE(ARGUNUSED(rval))
3481cd964fceSMatt Barden 	return (common_final(dev, arg, mode, crypto_mac_final));
3482cd964fceSMatt Barden }
3483cd964fceSMatt Barden 
3484cd964fceSMatt Barden /* ARGSUSED */
3485cd964fceSMatt Barden static int
mac(dev_t dev,caddr_t arg,int mode,int * rval)3486cd964fceSMatt Barden mac(dev_t dev, caddr_t arg, int mode, int *rval)
3487cd964fceSMatt Barden {
3488cd964fceSMatt Barden 	_NOTE(ARGUNUSED(rval))
3489cd964fceSMatt Barden 	return (common_digest(dev, arg, mode, crypto_mac_single));
3490cd964fceSMatt Barden }
3491cd964fceSMatt Barden 
34927c478bd9Sstevel@tonic-gate /*
34937c478bd9Sstevel@tonic-gate  * ASSUMPTION: crypto_digest, crypto_sign, crypto_sign_recover,
34947c478bd9Sstevel@tonic-gate  * and crypto_verify_recover are identical except for field names.
34957c478bd9Sstevel@tonic-gate  */
34967c478bd9Sstevel@tonic-gate static int
common_digest(dev_t dev,caddr_t arg,int mode,int (* single)(crypto_context_t,crypto_data_t *,crypto_data_t *,crypto_call_req_t *))34977c478bd9Sstevel@tonic-gate common_digest(dev_t dev, caddr_t arg, int mode,
34987c478bd9Sstevel@tonic-gate     int (*single)(crypto_context_t, crypto_data_t *, crypto_data_t *,
34997c478bd9Sstevel@tonic-gate     crypto_call_req_t *))
35007c478bd9Sstevel@tonic-gate {
35017c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_digest, crypto_digest);
35027c478bd9Sstevel@tonic-gate 	crypto_session_id_t session_id;
35037c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
35042d794da1Skrishna 	crypto_session_data_t *sp = NULL;
35057c478bd9Sstevel@tonic-gate 	crypto_data_t data, digest;
35067c478bd9Sstevel@tonic-gate 	crypto_ctx_t **ctxpp;
35077c478bd9Sstevel@tonic-gate 	size_t datalen, digestlen, need = 0;
35087c478bd9Sstevel@tonic-gate 	char *digestbuf;
35097c478bd9Sstevel@tonic-gate 	int error = 0;
35107c478bd9Sstevel@tonic-gate 	int rv;
35112d794da1Skrishna 	boolean_t rctl_chk = B_FALSE;
35127c478bd9Sstevel@tonic-gate 
35137c478bd9Sstevel@tonic-gate 	STRUCT_INIT(crypto_digest, mode);
35147c478bd9Sstevel@tonic-gate 
35157c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
35167c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "common_digest: failed holding minor");
35177c478bd9Sstevel@tonic-gate 		return (ENXIO);
35187c478bd9Sstevel@tonic-gate 	}
35197c478bd9Sstevel@tonic-gate 
35207c478bd9Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(crypto_digest),
35217c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(crypto_digest)) != 0) {
35227c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
35237c478bd9Sstevel@tonic-gate 		return (EFAULT);
35247c478bd9Sstevel@tonic-gate 	}
35257c478bd9Sstevel@tonic-gate 
35267c478bd9Sstevel@tonic-gate 	data.cd_raw.iov_base = NULL;
35277c478bd9Sstevel@tonic-gate 	digest.cd_raw.iov_base = NULL;
3528cd964fceSMatt Barden 	data.cd_miscdata = NULL;
3529cd964fceSMatt Barden 	digest.cd_miscdata = NULL;
35307c478bd9Sstevel@tonic-gate 
35317c478bd9Sstevel@tonic-gate 	datalen = STRUCT_FGET(crypto_digest, cd_datalen);
35327c478bd9Sstevel@tonic-gate 	digestlen = STRUCT_FGET(crypto_digest, cd_digestlen);
35337c478bd9Sstevel@tonic-gate 
35347c478bd9Sstevel@tonic-gate 	/*
35357c478bd9Sstevel@tonic-gate 	 * Don't allocate output buffer unless both buffer pointer and
35367c478bd9Sstevel@tonic-gate 	 * buffer length are not NULL or 0 (length).
35377c478bd9Sstevel@tonic-gate 	 */
35387c478bd9Sstevel@tonic-gate 	digestbuf = STRUCT_FGETP(crypto_digest, cd_digestbuf);
35397c478bd9Sstevel@tonic-gate 	if (digestbuf == NULL || digestlen == 0) {
35407c478bd9Sstevel@tonic-gate 		digestlen = 0;
35417c478bd9Sstevel@tonic-gate 	}
35427c478bd9Sstevel@tonic-gate 
35437c478bd9Sstevel@tonic-gate 	if (datalen > crypto_max_buffer_len ||
35447c478bd9Sstevel@tonic-gate 	    digestlen > crypto_max_buffer_len) {
35457c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "common_digest: buffer greater than %ld "
35467c478bd9Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
35477c478bd9Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
35487c478bd9Sstevel@tonic-gate 		goto release_minor;
35497c478bd9Sstevel@tonic-gate 	}
35507c478bd9Sstevel@tonic-gate 
35512d794da1Skrishna 	session_id = STRUCT_FGET(crypto_digest, cd_session);
35522d794da1Skrishna 
35532d794da1Skrishna 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv))  {
35542d794da1Skrishna 		goto release_minor;
35552d794da1Skrishna 	}
35562d794da1Skrishna 
35577c478bd9Sstevel@tonic-gate 	need = datalen + digestlen;
35582d794da1Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, need, rctl_chk)) !=
35592d794da1Skrishna 	    CRYPTO_SUCCESS) {
35607c478bd9Sstevel@tonic-gate 		need = 0;
35617c478bd9Sstevel@tonic-gate 		goto release_minor;
35627c478bd9Sstevel@tonic-gate 	}
35637c478bd9Sstevel@tonic-gate 
35647c478bd9Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(data, datalen);
35657c478bd9Sstevel@tonic-gate 
35667c478bd9Sstevel@tonic-gate 	if (datalen != 0 && copyin(STRUCT_FGETP(crypto_digest, cd_databuf),
35677c478bd9Sstevel@tonic-gate 	    data.cd_raw.iov_base, datalen) != 0) {
35687c478bd9Sstevel@tonic-gate 		error = EFAULT;
35697c478bd9Sstevel@tonic-gate 		goto release_minor;
35707c478bd9Sstevel@tonic-gate 	}
35717c478bd9Sstevel@tonic-gate 
35727c478bd9Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(digest, digestlen);
35737c478bd9Sstevel@tonic-gate 
35747c478bd9Sstevel@tonic-gate 	ASSERT(single == crypto_digest_single ||
35757c478bd9Sstevel@tonic-gate 	    single == crypto_sign_single ||
35767c478bd9Sstevel@tonic-gate 	    single == crypto_verify_recover_single ||
3577cd964fceSMatt Barden 	    single == crypto_sign_recover_single ||
3578cd964fceSMatt Barden 	    single == crypto_mac_single);
35797c478bd9Sstevel@tonic-gate 
35807c478bd9Sstevel@tonic-gate 	if (single == crypto_digest_single) {
35817c478bd9Sstevel@tonic-gate 		ctxpp = &sp->sd_digest_ctx;
35827c478bd9Sstevel@tonic-gate 	} else if (single == crypto_sign_single) {
35837c478bd9Sstevel@tonic-gate 		ctxpp = &sp->sd_sign_ctx;
35847c478bd9Sstevel@tonic-gate 	} else if (single == crypto_verify_recover_single) {
35857c478bd9Sstevel@tonic-gate 		ctxpp = &sp->sd_verify_recover_ctx;
3586cd964fceSMatt Barden 	} else if (single == crypto_mac_single) {
3587cd964fceSMatt Barden 		ctxpp = &sp->sd_mac_ctx;
35887c478bd9Sstevel@tonic-gate 	} else {
35897c478bd9Sstevel@tonic-gate 		ctxpp = &sp->sd_sign_recover_ctx;
35907c478bd9Sstevel@tonic-gate 	}
35917c478bd9Sstevel@tonic-gate 	rv = (single)(*ctxpp, &data, &digest, NULL);
35927c478bd9Sstevel@tonic-gate 	if (KCF_CONTEXT_DONE(rv))
35937c478bd9Sstevel@tonic-gate 		*ctxpp = NULL;
35947c478bd9Sstevel@tonic-gate 
35957c478bd9Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
35967c478bd9Sstevel@tonic-gate 		ASSERT(digest.cd_length <= digestlen);
35977c478bd9Sstevel@tonic-gate 		if (digest.cd_length != 0 && copyout(digest.cd_raw.iov_base,
35987c478bd9Sstevel@tonic-gate 		    digestbuf, digest.cd_length) != 0) {
35997c478bd9Sstevel@tonic-gate 			error = EFAULT;
36007c478bd9Sstevel@tonic-gate 			goto release_minor;
36017c478bd9Sstevel@tonic-gate 		}
360295014fbbSDan OpenSolaris Anderson 		STRUCT_FSET(crypto_digest, cd_digestlen,
360395014fbbSDan OpenSolaris Anderson 		    (ulong_t)digest.cd_length);
36047c478bd9Sstevel@tonic-gate 	}
36057c478bd9Sstevel@tonic-gate 
36067c478bd9Sstevel@tonic-gate 	if (rv == CRYPTO_BUFFER_TOO_SMALL) {
36077c478bd9Sstevel@tonic-gate 		/*
36087c478bd9Sstevel@tonic-gate 		 * The providers return CRYPTO_BUFFER_TOO_SMALL even for case 1
36097c478bd9Sstevel@tonic-gate 		 * of section 11.2 of the pkcs11 spec. We catch it here and
36107c478bd9Sstevel@tonic-gate 		 * provide the correct pkcs11 return value.
36117c478bd9Sstevel@tonic-gate 		 */
36127c478bd9Sstevel@tonic-gate 		if (STRUCT_FGETP(crypto_digest, cd_digestbuf) == NULL)
36137c478bd9Sstevel@tonic-gate 			rv = CRYPTO_SUCCESS;
361495014fbbSDan OpenSolaris Anderson 		STRUCT_FSET(crypto_digest, cd_digestlen,
361595014fbbSDan OpenSolaris Anderson 		    (ulong_t)digest.cd_length);
36167c478bd9Sstevel@tonic-gate 	}
36177c478bd9Sstevel@tonic-gate 
36187c478bd9Sstevel@tonic-gate release_minor:
36192d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
36202d794da1Skrishna 	CRYPTO_SESSION_RELE(sp);
36217c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
36227c478bd9Sstevel@tonic-gate 
36237c478bd9Sstevel@tonic-gate 	if (data.cd_raw.iov_base != NULL)
36247c478bd9Sstevel@tonic-gate 		kmem_free(data.cd_raw.iov_base, datalen);
36257c478bd9Sstevel@tonic-gate 
36267c478bd9Sstevel@tonic-gate 	if (digest.cd_raw.iov_base != NULL)
36277c478bd9Sstevel@tonic-gate 		kmem_free(digest.cd_raw.iov_base, digestlen);
36287c478bd9Sstevel@tonic-gate 
36297c478bd9Sstevel@tonic-gate 	if (error != 0)
36307c478bd9Sstevel@tonic-gate 		return (error);
36317c478bd9Sstevel@tonic-gate 
36327c478bd9Sstevel@tonic-gate 	STRUCT_FSET(crypto_digest, cd_return_value, rv);
36337c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(crypto_digest), arg,
36347c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(crypto_digest)) != 0) {
36357c478bd9Sstevel@tonic-gate 		return (EFAULT);
36367c478bd9Sstevel@tonic-gate 	}
36377c478bd9Sstevel@tonic-gate 	return (0);
36387c478bd9Sstevel@tonic-gate }
36397c478bd9Sstevel@tonic-gate 
36407c478bd9Sstevel@tonic-gate /*
36417c478bd9Sstevel@tonic-gate  * A helper function that does what the name suggests.
36427c478bd9Sstevel@tonic-gate  * Returns 0 on success and non-zero otherwise.
36437c478bd9Sstevel@tonic-gate  * On failure, out_pin is set to 0.
36447c478bd9Sstevel@tonic-gate  */
36457c478bd9Sstevel@tonic-gate int
get_pin_and_session_ptr(char * in_pin,char ** out_pin,size_t pin_len,crypto_minor_t * cm,crypto_session_id_t sid,crypto_session_data_t ** sp,int * rv,int * error)36467c478bd9Sstevel@tonic-gate get_pin_and_session_ptr(char *in_pin, char **out_pin, size_t pin_len,
36477c478bd9Sstevel@tonic-gate     crypto_minor_t *cm, crypto_session_id_t sid, crypto_session_data_t **sp,
36487c478bd9Sstevel@tonic-gate     int *rv, int *error)
36497c478bd9Sstevel@tonic-gate {
36507c478bd9Sstevel@tonic-gate 	char *tmp_pin = NULL;
36517c478bd9Sstevel@tonic-gate 	int tmp_error = 0, tmp_rv = 0;
36527c478bd9Sstevel@tonic-gate 
36537c478bd9Sstevel@tonic-gate 	if (pin_len > KCF_MAX_PIN_LEN) {
36547c478bd9Sstevel@tonic-gate 		tmp_rv = CRYPTO_PIN_LEN_RANGE;
36557c478bd9Sstevel@tonic-gate 		goto out;
36567c478bd9Sstevel@tonic-gate 	}
36577c478bd9Sstevel@tonic-gate 	tmp_pin = kmem_alloc(pin_len, KM_SLEEP);
36587c478bd9Sstevel@tonic-gate 
36597c478bd9Sstevel@tonic-gate 	if (pin_len != 0 && copyin(in_pin, tmp_pin, pin_len) != 0) {
36607c478bd9Sstevel@tonic-gate 		tmp_error = EFAULT;
36617c478bd9Sstevel@tonic-gate 		goto out;
36627c478bd9Sstevel@tonic-gate 	}
36637c478bd9Sstevel@tonic-gate 
36647c478bd9Sstevel@tonic-gate 	(void) get_session_ptr(sid, cm, sp, &tmp_error, &tmp_rv);
36657c478bd9Sstevel@tonic-gate out:
36667c478bd9Sstevel@tonic-gate 	*out_pin = tmp_pin;
36677c478bd9Sstevel@tonic-gate 	*rv = tmp_rv;
36687c478bd9Sstevel@tonic-gate 	*error = tmp_error;
36697c478bd9Sstevel@tonic-gate 	return (tmp_rv | tmp_error);
36707c478bd9Sstevel@tonic-gate }
36717c478bd9Sstevel@tonic-gate 
36727c478bd9Sstevel@tonic-gate /* ARGSUSED */
36737c478bd9Sstevel@tonic-gate static int
set_pin(dev_t dev,caddr_t arg,int mode,int * rval)36747c478bd9Sstevel@tonic-gate set_pin(dev_t dev, caddr_t arg, int mode, int *rval)
36757c478bd9Sstevel@tonic-gate {
36767c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_set_pin, set_pin);
36777c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
36787c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
36797c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
36807c478bd9Sstevel@tonic-gate 	crypto_session_data_t *sp;
36817c478bd9Sstevel@tonic-gate 	char *old_pin = NULL;
36827c478bd9Sstevel@tonic-gate 	char *new_pin = NULL;
36837c478bd9Sstevel@tonic-gate 	size_t old_pin_len;
36847c478bd9Sstevel@tonic-gate 	size_t new_pin_len;
36857c478bd9Sstevel@tonic-gate 	int error = 0;
36867c478bd9Sstevel@tonic-gate 	int rv;
36877c478bd9Sstevel@tonic-gate 
36887c478bd9Sstevel@tonic-gate 	STRUCT_INIT(set_pin, mode);
36897c478bd9Sstevel@tonic-gate 
36907c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
36917c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "set_pin: failed holding minor");
36927c478bd9Sstevel@tonic-gate 		return (ENXIO);
36937c478bd9Sstevel@tonic-gate 	}
36947c478bd9Sstevel@tonic-gate 
36957c478bd9Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(set_pin),
36967c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(set_pin)) != 0) {
36977c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
36987c478bd9Sstevel@tonic-gate 		return (EFAULT);
36997c478bd9Sstevel@tonic-gate 	}
37007c478bd9Sstevel@tonic-gate 
37017c478bd9Sstevel@tonic-gate 	old_pin_len = STRUCT_FGET(set_pin, sp_old_len);
37027c478bd9Sstevel@tonic-gate 
37037c478bd9Sstevel@tonic-gate 	if (get_pin_and_session_ptr(STRUCT_FGETP(set_pin, sp_old_pin),
37047c478bd9Sstevel@tonic-gate 	    &old_pin, old_pin_len, cm, STRUCT_FGET(set_pin, sp_session),
37057c478bd9Sstevel@tonic-gate 	    &sp, &rv, &error) != 0)
37067c478bd9Sstevel@tonic-gate 		goto release_minor;
37077c478bd9Sstevel@tonic-gate 
37087c478bd9Sstevel@tonic-gate 	new_pin_len = STRUCT_FGET(set_pin, sp_new_len);
37097c478bd9Sstevel@tonic-gate 	if (new_pin_len > KCF_MAX_PIN_LEN) {
37107c478bd9Sstevel@tonic-gate 		rv = CRYPTO_PIN_LEN_RANGE;
37117c478bd9Sstevel@tonic-gate 		goto out;
37127c478bd9Sstevel@tonic-gate 	}
37137c478bd9Sstevel@tonic-gate 	new_pin = kmem_alloc(new_pin_len, KM_SLEEP);
37147c478bd9Sstevel@tonic-gate 
37157c478bd9Sstevel@tonic-gate 	if (new_pin_len != 0 && copyin(STRUCT_FGETP(set_pin, sp_new_pin),
37167c478bd9Sstevel@tonic-gate 	    new_pin, new_pin_len) != 0) {
37177c478bd9Sstevel@tonic-gate 		error = EFAULT;
37187c478bd9Sstevel@tonic-gate 		goto out;
37197c478bd9Sstevel@tonic-gate 	}
37207c478bd9Sstevel@tonic-gate 
37217c478bd9Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
37227c478bd9Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(provider_ops), CRYPTO_PROVIDER_OFFSET(set_pin),
37239b009fc1SValerie Bubb Fenwick 	    sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
37247c478bd9Sstevel@tonic-gate 		goto out;
37257c478bd9Sstevel@tonic-gate 	}
37267c478bd9Sstevel@tonic-gate 
37277c478bd9Sstevel@tonic-gate 	KCF_WRAP_PROVMGMT_OPS_PARAMS(&params, KCF_OP_MGMT_SETPIN,
37287c478bd9Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, old_pin, old_pin_len,
37297c478bd9Sstevel@tonic-gate 	    new_pin, new_pin_len, NULL, NULL, real_provider);
37307c478bd9Sstevel@tonic-gate 
37317c478bd9Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
3732894b2776Smcpowers 	KCF_PROV_REFRELE(real_provider);
37337c478bd9Sstevel@tonic-gate 
37347c478bd9Sstevel@tonic-gate out:
37357c478bd9Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
37367c478bd9Sstevel@tonic-gate 
37377c478bd9Sstevel@tonic-gate release_minor:
37387c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
37397c478bd9Sstevel@tonic-gate 
37407c478bd9Sstevel@tonic-gate 	if (old_pin != NULL) {
37417c478bd9Sstevel@tonic-gate 		bzero(old_pin, old_pin_len);
37427c478bd9Sstevel@tonic-gate 		kmem_free(old_pin, old_pin_len);
37437c478bd9Sstevel@tonic-gate 	}
37447c478bd9Sstevel@tonic-gate 
37457c478bd9Sstevel@tonic-gate 	if (new_pin != NULL) {
37467c478bd9Sstevel@tonic-gate 		bzero(new_pin, new_pin_len);
37477c478bd9Sstevel@tonic-gate 		kmem_free(new_pin, new_pin_len);
37487c478bd9Sstevel@tonic-gate 	}
37497c478bd9Sstevel@tonic-gate 
37507c478bd9Sstevel@tonic-gate 	if (error != 0)
37517c478bd9Sstevel@tonic-gate 		return (error);
37527c478bd9Sstevel@tonic-gate 
37537c478bd9Sstevel@tonic-gate 	STRUCT_FSET(set_pin, sp_return_value, rv);
37547c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(set_pin), arg, STRUCT_SIZE(set_pin)) != 0) {
37557c478bd9Sstevel@tonic-gate 		return (EFAULT);
37567c478bd9Sstevel@tonic-gate 	}
37577c478bd9Sstevel@tonic-gate 	return (0);
37587c478bd9Sstevel@tonic-gate }
37597c478bd9Sstevel@tonic-gate 
37607c478bd9Sstevel@tonic-gate /* ARGSUSED */
37617c478bd9Sstevel@tonic-gate static int
login(dev_t dev,caddr_t arg,int mode,int * rval)37627c478bd9Sstevel@tonic-gate login(dev_t dev, caddr_t arg, int mode, int *rval)
37637c478bd9Sstevel@tonic-gate {
37647c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_login, login);
37657c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
37667c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
37677c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
37687c478bd9Sstevel@tonic-gate 	crypto_session_data_t *sp;
37697c478bd9Sstevel@tonic-gate 	size_t pin_len;
37707c478bd9Sstevel@tonic-gate 	char *pin;
37717c478bd9Sstevel@tonic-gate 	uint_t user_type;
37727c478bd9Sstevel@tonic-gate 	int error = 0;
37737c478bd9Sstevel@tonic-gate 	int rv;
37747c478bd9Sstevel@tonic-gate 
37757c478bd9Sstevel@tonic-gate 	STRUCT_INIT(login, mode);
37767c478bd9Sstevel@tonic-gate 
37777c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
37787c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "login: failed holding minor");
37797c478bd9Sstevel@tonic-gate 		return (ENXIO);
37807c478bd9Sstevel@tonic-gate 	}
37817c478bd9Sstevel@tonic-gate 
37827c478bd9Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(login), STRUCT_SIZE(login)) != 0) {
37837c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
37847c478bd9Sstevel@tonic-gate 		return (EFAULT);
37857c478bd9Sstevel@tonic-gate 	}
37867c478bd9Sstevel@tonic-gate 
37877c478bd9Sstevel@tonic-gate 	user_type = STRUCT_FGET(login, co_user_type);
37887c478bd9Sstevel@tonic-gate 
37897c478bd9Sstevel@tonic-gate 	pin_len = STRUCT_FGET(login, co_pin_len);
37907c478bd9Sstevel@tonic-gate 
37917c478bd9Sstevel@tonic-gate 	if (get_pin_and_session_ptr(STRUCT_FGETP(login, co_pin),
37927c478bd9Sstevel@tonic-gate 	    &pin, pin_len, cm, STRUCT_FGET(login, co_session),
37937c478bd9Sstevel@tonic-gate 	    &sp, &rv, &error) != 0) {
37947c478bd9Sstevel@tonic-gate 		if (rv == CRYPTO_PIN_LEN_RANGE)
37957c478bd9Sstevel@tonic-gate 			rv = CRYPTO_PIN_INCORRECT;
37967c478bd9Sstevel@tonic-gate 		goto release_minor;
37977c478bd9Sstevel@tonic-gate 	}
37987c478bd9Sstevel@tonic-gate 
37997c478bd9Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
38007c478bd9Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(session_ops),
38019b009fc1SValerie Bubb Fenwick 	    CRYPTO_SESSION_OFFSET(session_login), sp->sd_provider,
38029b009fc1SValerie Bubb Fenwick 	    &real_provider)) != CRYPTO_SUCCESS) {
38037c478bd9Sstevel@tonic-gate 		goto out;
38047c478bd9Sstevel@tonic-gate 	}
38057c478bd9Sstevel@tonic-gate 
38067c478bd9Sstevel@tonic-gate 	KCF_WRAP_SESSION_OPS_PARAMS(&params, KCF_OP_SESSION_LOGIN, NULL,
38077c478bd9Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, user_type, pin, pin_len,
38087c478bd9Sstevel@tonic-gate 	    real_provider);
38097c478bd9Sstevel@tonic-gate 
38107c478bd9Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
3811894b2776Smcpowers 	KCF_PROV_REFRELE(real_provider);
38127c478bd9Sstevel@tonic-gate 
38137c478bd9Sstevel@tonic-gate out:
38147c478bd9Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
38157c478bd9Sstevel@tonic-gate 
38167c478bd9Sstevel@tonic-gate release_minor:
38177c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
38187c478bd9Sstevel@tonic-gate 
38197c478bd9Sstevel@tonic-gate 	if (pin != NULL) {
38207c478bd9Sstevel@tonic-gate 		bzero(pin, pin_len);
38217c478bd9Sstevel@tonic-gate 		kmem_free(pin, pin_len);
38227c478bd9Sstevel@tonic-gate 	}
38237c478bd9Sstevel@tonic-gate 
38247c478bd9Sstevel@tonic-gate 	if (error != 0)
38257c478bd9Sstevel@tonic-gate 		return (error);
38267c478bd9Sstevel@tonic-gate 
38277c478bd9Sstevel@tonic-gate 	STRUCT_FSET(login, co_return_value, rv);
38287c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(login), arg, STRUCT_SIZE(login)) != 0) {
38297c478bd9Sstevel@tonic-gate 		return (EFAULT);
38307c478bd9Sstevel@tonic-gate 	}
38317c478bd9Sstevel@tonic-gate 	return (0);
38327c478bd9Sstevel@tonic-gate }
38337c478bd9Sstevel@tonic-gate 
38347c478bd9Sstevel@tonic-gate /* ARGSUSED */
38357c478bd9Sstevel@tonic-gate static int
logout(dev_t dev,caddr_t arg,int mode,int * rval)38367c478bd9Sstevel@tonic-gate logout(dev_t dev, caddr_t arg, int mode, int *rval)
38377c478bd9Sstevel@tonic-gate {
38387c478bd9Sstevel@tonic-gate 	crypto_logout_t logout;
38397c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
38407c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
38417c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
38427c478bd9Sstevel@tonic-gate 	crypto_session_data_t *sp;
38437c478bd9Sstevel@tonic-gate 	int error = 0;
38447c478bd9Sstevel@tonic-gate 	int rv;
38457c478bd9Sstevel@tonic-gate 
38467c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
38477c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "logout: failed holding minor");
38487c478bd9Sstevel@tonic-gate 		return (ENXIO);
38497c478bd9Sstevel@tonic-gate 	}
38507c478bd9Sstevel@tonic-gate 
38517c478bd9Sstevel@tonic-gate 	if (copyin(arg, &logout, sizeof (logout)) != 0) {
38527c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
38537c478bd9Sstevel@tonic-gate 		return (EFAULT);
38547c478bd9Sstevel@tonic-gate 	}
38557c478bd9Sstevel@tonic-gate 
38567c478bd9Sstevel@tonic-gate 	if (!get_session_ptr(logout.cl_session, cm, &sp, &error, &rv))  {
38577c478bd9Sstevel@tonic-gate 		goto release_minor;
38587c478bd9Sstevel@tonic-gate 	}
38597c478bd9Sstevel@tonic-gate 
38607c478bd9Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
38617c478bd9Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(session_ops),
38629b009fc1SValerie Bubb Fenwick 	    CRYPTO_SESSION_OFFSET(session_logout), sp->sd_provider,
38639b009fc1SValerie Bubb Fenwick 	    &real_provider)) != CRYPTO_SUCCESS) {
38647c478bd9Sstevel@tonic-gate 		goto out;
38657c478bd9Sstevel@tonic-gate 	}
38667c478bd9Sstevel@tonic-gate 
38677c478bd9Sstevel@tonic-gate 	KCF_WRAP_SESSION_OPS_PARAMS(&params, KCF_OP_SESSION_LOGOUT, NULL,
38687c478bd9Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, 0, NULL, 0, real_provider);
38697c478bd9Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
3870894b2776Smcpowers 	KCF_PROV_REFRELE(real_provider);
38717c478bd9Sstevel@tonic-gate 
38727c478bd9Sstevel@tonic-gate out:
38737c478bd9Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
38747c478bd9Sstevel@tonic-gate 
38757c478bd9Sstevel@tonic-gate release_minor:
38767c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
38777c478bd9Sstevel@tonic-gate 
38787c478bd9Sstevel@tonic-gate 	if (error != 0)
38797c478bd9Sstevel@tonic-gate 		return (error);
38807c478bd9Sstevel@tonic-gate 
38817c478bd9Sstevel@tonic-gate 	logout.cl_return_value = rv;
38827c478bd9Sstevel@tonic-gate 	if (copyout(&logout, arg, sizeof (logout)) != 0) {
38837c478bd9Sstevel@tonic-gate 		return (EFAULT);
38847c478bd9Sstevel@tonic-gate 	}
38857c478bd9Sstevel@tonic-gate 	return (0);
38867c478bd9Sstevel@tonic-gate }
38877c478bd9Sstevel@tonic-gate 
38887c478bd9Sstevel@tonic-gate /* ARGSUSED */
38897c478bd9Sstevel@tonic-gate static int
sign_init(dev_t dev,caddr_t arg,int mode,int * rval)38907c478bd9Sstevel@tonic-gate sign_init(dev_t dev, caddr_t arg, int mode, int *rval)
38917c478bd9Sstevel@tonic-gate {
38927c478bd9Sstevel@tonic-gate 	return (sign_verify_init(dev, arg, mode, crypto_sign_init_prov));
38937c478bd9Sstevel@tonic-gate }
38947c478bd9Sstevel@tonic-gate 
38957c478bd9Sstevel@tonic-gate /* ARGSUSED */
38967c478bd9Sstevel@tonic-gate static int
sign_recover_init(dev_t dev,caddr_t arg,int mode,int * rval)38977c478bd9Sstevel@tonic-gate sign_recover_init(dev_t dev, caddr_t arg, int mode, int *rval)
38987c478bd9Sstevel@tonic-gate {
38997c478bd9Sstevel@tonic-gate 	return (sign_verify_init(dev, arg, mode,
39007c478bd9Sstevel@tonic-gate 	    crypto_sign_recover_init_prov));
39017c478bd9Sstevel@tonic-gate }
39027c478bd9Sstevel@tonic-gate 
39037c478bd9Sstevel@tonic-gate /* ARGSUSED */
39047c478bd9Sstevel@tonic-gate static int
verify_init(dev_t dev,caddr_t arg,int mode,int * rval)39057c478bd9Sstevel@tonic-gate verify_init(dev_t dev, caddr_t arg, int mode, int *rval)
39067c478bd9Sstevel@tonic-gate {
39077c478bd9Sstevel@tonic-gate 	return (sign_verify_init(dev, arg, mode, crypto_verify_init_prov));
39087c478bd9Sstevel@tonic-gate }
39097c478bd9Sstevel@tonic-gate 
39107c478bd9Sstevel@tonic-gate /* ARGSUSED */
39117c478bd9Sstevel@tonic-gate static int
verify_recover_init(dev_t dev,caddr_t arg,int mode,int * rval)39127c478bd9Sstevel@tonic-gate verify_recover_init(dev_t dev, caddr_t arg, int mode, int *rval)
39137c478bd9Sstevel@tonic-gate {
39147c478bd9Sstevel@tonic-gate 	return (sign_verify_init(dev, arg, mode,
39157c478bd9Sstevel@tonic-gate 	    crypto_verify_recover_init_prov));
39167c478bd9Sstevel@tonic-gate }
39177c478bd9Sstevel@tonic-gate 
39187c478bd9Sstevel@tonic-gate /*
39197c478bd9Sstevel@tonic-gate  * ASSUMPTION: crypto_sign_init, crypto_verify_init, crypto_sign_recover_init,
39207c478bd9Sstevel@tonic-gate  * and crypto_verify_recover_init structures are identical
39217c478bd9Sstevel@tonic-gate  * except for field names.
39227c478bd9Sstevel@tonic-gate  */
39237c478bd9Sstevel@tonic-gate static int
sign_verify_init(dev_t dev,caddr_t arg,int mode,int (* init)(crypto_provider_t,crypto_session_id_t,crypto_mechanism_t *,crypto_key_t *,crypto_ctx_template_t,crypto_context_t *,crypto_call_req_t *))39247c478bd9Sstevel@tonic-gate sign_verify_init(dev_t dev, caddr_t arg, int mode,
3925894b2776Smcpowers     int (*init)(crypto_provider_t, crypto_session_id_t,
39267c478bd9Sstevel@tonic-gate     crypto_mechanism_t *, crypto_key_t *, crypto_ctx_template_t,
39277c478bd9Sstevel@tonic-gate     crypto_context_t *, crypto_call_req_t *))
39287c478bd9Sstevel@tonic-gate {
39297c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_sign_init, sign_init);
3930894b2776Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
39317c478bd9Sstevel@tonic-gate 	crypto_session_id_t session_id;
39327c478bd9Sstevel@tonic-gate 	crypto_mechanism_t mech;
39337c478bd9Sstevel@tonic-gate 	crypto_key_t key;
39347c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
39352d794da1Skrishna 	crypto_session_data_t *sp = NULL;
39367c478bd9Sstevel@tonic-gate 	crypto_context_t cc;
39377c478bd9Sstevel@tonic-gate 	crypto_ctx_t **ctxpp;
39387c478bd9Sstevel@tonic-gate 	size_t mech_rctl_bytes = 0;
39392d794da1Skrishna 	boolean_t mech_rctl_chk = B_FALSE;
39407c478bd9Sstevel@tonic-gate 	size_t key_rctl_bytes = 0;
39412d794da1Skrishna 	boolean_t key_rctl_chk = B_FALSE;
39427c478bd9Sstevel@tonic-gate 	int error = 0;
39437c478bd9Sstevel@tonic-gate 	int rv;
3944894b2776Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
394572eff6e2Smcpowers 	crypto_func_group_t fg;
39467c478bd9Sstevel@tonic-gate 
39477c478bd9Sstevel@tonic-gate 	STRUCT_INIT(sign_init, mode);
39487c478bd9Sstevel@tonic-gate 
39497c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
39507c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "sign_verify_init: failed holding minor");
39517c478bd9Sstevel@tonic-gate 		return (ENXIO);
39527c478bd9Sstevel@tonic-gate 	}
39537c478bd9Sstevel@tonic-gate 
39547c478bd9Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(sign_init), STRUCT_SIZE(sign_init)) != 0) {
39557c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
39567c478bd9Sstevel@tonic-gate 		return (EFAULT);
39577c478bd9Sstevel@tonic-gate 	}
39587c478bd9Sstevel@tonic-gate 
39597c478bd9Sstevel@tonic-gate 	mech.cm_param = NULL;
39607c478bd9Sstevel@tonic-gate 	bzero(&key, sizeof (key));
39617c478bd9Sstevel@tonic-gate 
39627c478bd9Sstevel@tonic-gate 	session_id = STRUCT_FGET(sign_init, si_session);
39637c478bd9Sstevel@tonic-gate 
39647c478bd9Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
39652d794da1Skrishna 		goto out;
39667c478bd9Sstevel@tonic-gate 	}
39677c478bd9Sstevel@tonic-gate 
3968894b2776Smcpowers 	bcopy(STRUCT_FADDR(sign_init, si_mech), &mech.cm_type,
3969894b2776Smcpowers 	    sizeof (crypto_mech_type_t));
39707c478bd9Sstevel@tonic-gate 
39717c478bd9Sstevel@tonic-gate 	ASSERT(init == crypto_sign_init_prov ||
39727c478bd9Sstevel@tonic-gate 	    init == crypto_verify_init_prov ||
39737c478bd9Sstevel@tonic-gate 	    init == crypto_sign_recover_init_prov ||
3974cd964fceSMatt Barden 	    init == crypto_verify_recover_init_prov ||
3975cd964fceSMatt Barden 	    init == crypto_mac_init_prov);
39767c478bd9Sstevel@tonic-gate 
39777c478bd9Sstevel@tonic-gate 	if (init == crypto_sign_init_prov) {
397872eff6e2Smcpowers 		fg =  CRYPTO_FG_SIGN;
39797c478bd9Sstevel@tonic-gate 		ctxpp = &sp->sd_sign_ctx;
39807c478bd9Sstevel@tonic-gate 	} else if (init == crypto_verify_init_prov) {
398172eff6e2Smcpowers 		fg =  CRYPTO_FG_VERIFY;
39827c478bd9Sstevel@tonic-gate 		ctxpp = &sp->sd_verify_ctx;
39837c478bd9Sstevel@tonic-gate 	} else if (init == crypto_sign_recover_init_prov) {
398472eff6e2Smcpowers 		fg =  CRYPTO_FG_SIGN_RECOVER;
39857c478bd9Sstevel@tonic-gate 		ctxpp = &sp->sd_sign_recover_ctx;
3986cd964fceSMatt Barden 	} else if (init == crypto_mac_init_prov) {
3987cd964fceSMatt Barden 		fg =  CRYPTO_FG_MAC;
3988cd964fceSMatt Barden 		ctxpp = &sp->sd_mac_ctx;
39897c478bd9Sstevel@tonic-gate 	} else {
399072eff6e2Smcpowers 		fg =  CRYPTO_FG_VERIFY_RECOVER;
39917c478bd9Sstevel@tonic-gate 		ctxpp = &sp->sd_verify_recover_ctx;
39927c478bd9Sstevel@tonic-gate 	}
39937c478bd9Sstevel@tonic-gate 
3994436935a1SVladimir Kotal 	/* We need the key length for provider selection so copy it in now. */
3995436935a1SVladimir Kotal 	if (!copyin_key(mode, sp, STRUCT_FADDR(sign_init, si_key), &key,
3996436935a1SVladimir Kotal 	    &key_rctl_bytes, &key_rctl_chk, &rv, &error)) {
3997436935a1SVladimir Kotal 		goto out;
3998436935a1SVladimir Kotal 	}
3999436935a1SVladimir Kotal 
4000436935a1SVladimir Kotal 	if ((rv = kcf_get_hardware_provider(mech.cm_type, &key,
40019b009fc1SValerie Bubb Fenwick 	    CRYPTO_MECH_INVALID, NULL, sp->sd_provider, &real_provider,
40029b009fc1SValerie Bubb Fenwick 	    fg)) != CRYPTO_SUCCESS) {
40037c478bd9Sstevel@tonic-gate 		goto out;
40047c478bd9Sstevel@tonic-gate 	}
40057c478bd9Sstevel@tonic-gate 
4006894b2776Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
4007894b2776Smcpowers 	    STRUCT_FADDR(sign_init, si_mech), &mech, mode, &error);
4008894b2776Smcpowers 
4009894b2776Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
4010894b2776Smcpowers 		allocated_by_crypto_module = B_TRUE;
40112d794da1Skrishna 		if (!copyin_mech(mode, sp, STRUCT_FADDR(sign_init, si_mech),
40122d794da1Skrishna 		    &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) {
4013894b2776Smcpowers 			goto out;
4014894b2776Smcpowers 		}
4015894b2776Smcpowers 	} else {
4016894b2776Smcpowers 		if (rv != CRYPTO_SUCCESS)
4017894b2776Smcpowers 			goto out;
4018894b2776Smcpowers 	}
4019894b2776Smcpowers 
40207c478bd9Sstevel@tonic-gate 	rv = (init)(real_provider, sp->sd_provider_session->ps_session,
40217c478bd9Sstevel@tonic-gate 	    &mech, &key, NULL, &cc, NULL);
40227c478bd9Sstevel@tonic-gate 
40237c478bd9Sstevel@tonic-gate 	/*
40247c478bd9Sstevel@tonic-gate 	 * Check if a context already exists. If so, it means it is being
40257c478bd9Sstevel@tonic-gate 	 * abandoned. So, cancel it to avoid leaking it.
40267c478bd9Sstevel@tonic-gate 	 */
40277c478bd9Sstevel@tonic-gate 	if (*ctxpp != NULL)
40287c478bd9Sstevel@tonic-gate 		CRYPTO_CANCEL_CTX(ctxpp);
40297c478bd9Sstevel@tonic-gate 	*ctxpp = (rv == CRYPTO_SUCCESS) ? cc : NULL;
40307c478bd9Sstevel@tonic-gate 
40317c478bd9Sstevel@tonic-gate out:
40322d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
40332d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk);
40347c478bd9Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
40357c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
40367c478bd9Sstevel@tonic-gate 
4037894b2776Smcpowers 	if (real_provider != NULL) {
4038894b2776Smcpowers 		crypto_free_mech(real_provider,
4039894b2776Smcpowers 		    allocated_by_crypto_module, &mech);
4040894b2776Smcpowers 		KCF_PROV_REFRELE(real_provider);
4041894b2776Smcpowers 	}
40427c478bd9Sstevel@tonic-gate 
40437c478bd9Sstevel@tonic-gate 	free_crypto_key(&key);
40447c478bd9Sstevel@tonic-gate 
40457c478bd9Sstevel@tonic-gate 	if (error != 0)
40467c478bd9Sstevel@tonic-gate 		return (error);
40477c478bd9Sstevel@tonic-gate 
40487c478bd9Sstevel@tonic-gate 	STRUCT_FSET(sign_init, si_return_value, rv);
40497c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(sign_init), arg, STRUCT_SIZE(sign_init)) != 0) {
40507c478bd9Sstevel@tonic-gate 		return (EFAULT);
40517c478bd9Sstevel@tonic-gate 	}
40527c478bd9Sstevel@tonic-gate 	return (0);
40537c478bd9Sstevel@tonic-gate }
40547c478bd9Sstevel@tonic-gate 
40557c478bd9Sstevel@tonic-gate /* ARGSUSED */
40567c478bd9Sstevel@tonic-gate static int
sign(dev_t dev,caddr_t arg,int mode,int * rval)40577c478bd9Sstevel@tonic-gate sign(dev_t dev, caddr_t arg, int mode, int *rval)
40587c478bd9Sstevel@tonic-gate {
40597c478bd9Sstevel@tonic-gate 	return (common_digest(dev, arg, mode, crypto_sign_single));
40607c478bd9Sstevel@tonic-gate }
40617c478bd9Sstevel@tonic-gate 
40627c478bd9Sstevel@tonic-gate /* ARGSUSED */
40637c478bd9Sstevel@tonic-gate static int
sign_recover(dev_t dev,caddr_t arg,int mode,int * rval)40647c478bd9Sstevel@tonic-gate sign_recover(dev_t dev, caddr_t arg, int mode, int *rval)
40657c478bd9Sstevel@tonic-gate {
40667c478bd9Sstevel@tonic-gate 	return (common_digest(dev, arg, mode, crypto_sign_recover_single));
40677c478bd9Sstevel@tonic-gate }
40687c478bd9Sstevel@tonic-gate 
40697c478bd9Sstevel@tonic-gate /* ARGSUSED */
40707c478bd9Sstevel@tonic-gate static int
verify(dev_t dev,caddr_t arg,int mode,int * rval)40717c478bd9Sstevel@tonic-gate verify(dev_t dev, caddr_t arg, int mode, int *rval)
40727c478bd9Sstevel@tonic-gate {
40737c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_verify, verify);
40747c478bd9Sstevel@tonic-gate 	crypto_session_id_t session_id;
40757c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
40762d794da1Skrishna 	crypto_session_data_t *sp = NULL;
40777c478bd9Sstevel@tonic-gate 	crypto_data_t data, sign;
40787c478bd9Sstevel@tonic-gate 	size_t datalen, signlen, need = 0;
40797c478bd9Sstevel@tonic-gate 	int error = 0;
40807c478bd9Sstevel@tonic-gate 	int rv;
40812d794da1Skrishna 	boolean_t rctl_chk = B_FALSE;
40827c478bd9Sstevel@tonic-gate 
40837c478bd9Sstevel@tonic-gate 	STRUCT_INIT(verify, mode);
40847c478bd9Sstevel@tonic-gate 
40857c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
40867c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "verify: failed holding minor");
40877c478bd9Sstevel@tonic-gate 		return (ENXIO);
40887c478bd9Sstevel@tonic-gate 	}
40897c478bd9Sstevel@tonic-gate 
40907c478bd9Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(verify), STRUCT_SIZE(verify)) != 0) {
40917c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
40927c478bd9Sstevel@tonic-gate 		return (EFAULT);
40937c478bd9Sstevel@tonic-gate 	}
40947c478bd9Sstevel@tonic-gate 
40957c478bd9Sstevel@tonic-gate 	data.cd_raw.iov_base = NULL;
40967c478bd9Sstevel@tonic-gate 	sign.cd_raw.iov_base = NULL;
40977c478bd9Sstevel@tonic-gate 
40987c478bd9Sstevel@tonic-gate 	datalen = STRUCT_FGET(verify, cv_datalen);
40997c478bd9Sstevel@tonic-gate 	signlen = STRUCT_FGET(verify, cv_signlen);
41007c478bd9Sstevel@tonic-gate 	if (datalen > crypto_max_buffer_len ||
41017c478bd9Sstevel@tonic-gate 	    signlen > crypto_max_buffer_len) {
41027c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "verify: buffer greater than %ld bytes, "
41037c478bd9Sstevel@tonic-gate 		"pid = %d", crypto_max_buffer_len, curproc->p_pid);
41047c478bd9Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
41057c478bd9Sstevel@tonic-gate 		goto release_minor;
41067c478bd9Sstevel@tonic-gate 	}
41077c478bd9Sstevel@tonic-gate 
41082d794da1Skrishna 	session_id = STRUCT_FGET(verify, cv_session);
41092d794da1Skrishna 
41102d794da1Skrishna 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
41112d794da1Skrishna 		goto release_minor;
41122d794da1Skrishna 	}
41132d794da1Skrishna 
41147c478bd9Sstevel@tonic-gate 	need = datalen + signlen;
41152d794da1Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, need, rctl_chk)) !=
41162d794da1Skrishna 	    CRYPTO_SUCCESS) {
41177c478bd9Sstevel@tonic-gate 		need = 0;
41187c478bd9Sstevel@tonic-gate 		goto release_minor;
41197c478bd9Sstevel@tonic-gate 	}
41207c478bd9Sstevel@tonic-gate 
41217c478bd9Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(data, datalen);
41227c478bd9Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(sign, signlen);
41237c478bd9Sstevel@tonic-gate 
41247c478bd9Sstevel@tonic-gate 	if (datalen != 0 && copyin(STRUCT_FGETP(verify, cv_databuf),
41257c478bd9Sstevel@tonic-gate 	    data.cd_raw.iov_base, datalen) != 0) {
41267c478bd9Sstevel@tonic-gate 		error = EFAULT;
41277c478bd9Sstevel@tonic-gate 		goto release_minor;
41287c478bd9Sstevel@tonic-gate 	}
41297c478bd9Sstevel@tonic-gate 
41307c478bd9Sstevel@tonic-gate 	if (signlen != 0 && copyin(STRUCT_FGETP(verify, cv_signbuf),
41317c478bd9Sstevel@tonic-gate 	    sign.cd_raw.iov_base, signlen) != 0) {
41327c478bd9Sstevel@tonic-gate 		error = EFAULT;
41337c478bd9Sstevel@tonic-gate 		goto release_minor;
41347c478bd9Sstevel@tonic-gate 	}
41357c478bd9Sstevel@tonic-gate 
41367c478bd9Sstevel@tonic-gate 	rv = crypto_verify_single(sp->sd_verify_ctx, &data, &sign, NULL);
41377c478bd9Sstevel@tonic-gate 	if (KCF_CONTEXT_DONE(rv))
41387c478bd9Sstevel@tonic-gate 		sp->sd_verify_ctx = NULL;
41397c478bd9Sstevel@tonic-gate 
41407c478bd9Sstevel@tonic-gate release_minor:
41412d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
41422d794da1Skrishna 	CRYPTO_SESSION_RELE(sp);
41437c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
41447c478bd9Sstevel@tonic-gate 
41457c478bd9Sstevel@tonic-gate 	if (data.cd_raw.iov_base != NULL)
41467c478bd9Sstevel@tonic-gate 		kmem_free(data.cd_raw.iov_base, datalen);
41477c478bd9Sstevel@tonic-gate 
41487c478bd9Sstevel@tonic-gate 	if (sign.cd_raw.iov_base != NULL)
41497c478bd9Sstevel@tonic-gate 		kmem_free(sign.cd_raw.iov_base, signlen);
41507c478bd9Sstevel@tonic-gate 
41517c478bd9Sstevel@tonic-gate 	if (error != 0)
41527c478bd9Sstevel@tonic-gate 		return (error);
41537c478bd9Sstevel@tonic-gate 
41547c478bd9Sstevel@tonic-gate 	STRUCT_FSET(verify, cv_return_value, rv);
41557c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(verify), arg, STRUCT_SIZE(verify)) != 0) {
41567c478bd9Sstevel@tonic-gate 		return (EFAULT);
41577c478bd9Sstevel@tonic-gate 	}
41587c478bd9Sstevel@tonic-gate 	return (0);
41597c478bd9Sstevel@tonic-gate }
41607c478bd9Sstevel@tonic-gate 
41617c478bd9Sstevel@tonic-gate /* ARGSUSED */
41627c478bd9Sstevel@tonic-gate static int
verify_recover(dev_t dev,caddr_t arg,int mode,int * rval)41637c478bd9Sstevel@tonic-gate verify_recover(dev_t dev, caddr_t arg, int mode, int *rval)
41647c478bd9Sstevel@tonic-gate {
41657c478bd9Sstevel@tonic-gate 	return (common_digest(dev, arg, mode, crypto_verify_recover_single));
41667c478bd9Sstevel@tonic-gate }
41677c478bd9Sstevel@tonic-gate 
41687c478bd9Sstevel@tonic-gate /* ARGSUSED */
41697c478bd9Sstevel@tonic-gate static int
sign_update(dev_t dev,caddr_t arg,int mode,int * rval)41707c478bd9Sstevel@tonic-gate sign_update(dev_t dev, caddr_t arg, int mode, int *rval)
41717c478bd9Sstevel@tonic-gate {
41727c478bd9Sstevel@tonic-gate 	return (sign_verify_update(dev, arg, mode, crypto_sign_update));
41737c478bd9Sstevel@tonic-gate }
41747c478bd9Sstevel@tonic-gate 
41757c478bd9Sstevel@tonic-gate /* ARGSUSED */
41767c478bd9Sstevel@tonic-gate static int
verify_update(dev_t dev,caddr_t arg,int mode,int * rval)41777c478bd9Sstevel@tonic-gate verify_update(dev_t dev, caddr_t arg, int mode, int *rval)
41787c478bd9Sstevel@tonic-gate {
41797c478bd9Sstevel@tonic-gate 	return (sign_verify_update(dev, arg, mode, crypto_verify_update));
41807c478bd9Sstevel@tonic-gate }
41817c478bd9Sstevel@tonic-gate 
41827c478bd9Sstevel@tonic-gate /*
41837c478bd9Sstevel@tonic-gate  * ASSUMPTION: crypto_sign_update and crypto_verify_update structures
41847c478bd9Sstevel@tonic-gate  * are identical except for field names.
41857c478bd9Sstevel@tonic-gate  */
41867c478bd9Sstevel@tonic-gate static int
sign_verify_update(dev_t dev,caddr_t arg,int mode,int (* update)(crypto_context_t,crypto_data_t *,crypto_call_req_t *))41877c478bd9Sstevel@tonic-gate sign_verify_update(dev_t dev, caddr_t arg, int mode,
41887c478bd9Sstevel@tonic-gate     int (*update)(crypto_context_t, crypto_data_t *, crypto_call_req_t *))
41897c478bd9Sstevel@tonic-gate {
41907c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_sign_update, sign_update);
41917c478bd9Sstevel@tonic-gate 	crypto_session_id_t session_id;
41927c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
41932d794da1Skrishna 	crypto_session_data_t *sp = NULL;
41947c478bd9Sstevel@tonic-gate 	crypto_ctx_t **ctxpp;
41957c478bd9Sstevel@tonic-gate 	crypto_data_t data;
41967c478bd9Sstevel@tonic-gate 	size_t datalen, need = 0;
41977c478bd9Sstevel@tonic-gate 	int error = 0;
41987c478bd9Sstevel@tonic-gate 	int rv;
41992d794da1Skrishna 	boolean_t rctl_chk = B_FALSE;
42007c478bd9Sstevel@tonic-gate 
42017c478bd9Sstevel@tonic-gate 	STRUCT_INIT(sign_update, mode);
42027c478bd9Sstevel@tonic-gate 
42037c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
42047c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "sign_verify_update: failed holding minor");
42057c478bd9Sstevel@tonic-gate 		return (ENXIO);
42067c478bd9Sstevel@tonic-gate 	}
42077c478bd9Sstevel@tonic-gate 
42087c478bd9Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(sign_update),
42097c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(sign_update)) != 0) {
42107c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
42117c478bd9Sstevel@tonic-gate 		return (EFAULT);
42127c478bd9Sstevel@tonic-gate 	}
42137c478bd9Sstevel@tonic-gate 
42147c478bd9Sstevel@tonic-gate 	data.cd_raw.iov_base = NULL;
4215cd964fceSMatt Barden 	data.cd_miscdata = NULL;
42167c478bd9Sstevel@tonic-gate 
42177c478bd9Sstevel@tonic-gate 	datalen = STRUCT_FGET(sign_update, su_datalen);
42187c478bd9Sstevel@tonic-gate 	if (datalen > crypto_max_buffer_len) {
42197c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "sign_verify_update: buffer greater than %ld "
42207c478bd9Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
42217c478bd9Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
42227c478bd9Sstevel@tonic-gate 		goto release_minor;
42237c478bd9Sstevel@tonic-gate 	}
42247c478bd9Sstevel@tonic-gate 
42252d794da1Skrishna 	session_id = STRUCT_FGET(sign_update, su_session);
42262d794da1Skrishna 
42272d794da1Skrishna 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
42282d794da1Skrishna 		goto release_minor;
42292d794da1Skrishna 	}
42302d794da1Skrishna 
42312d794da1Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, datalen, rctl_chk)) !=
42322d794da1Skrishna 	    CRYPTO_SUCCESS) {
42337c478bd9Sstevel@tonic-gate 		goto release_minor;
42347c478bd9Sstevel@tonic-gate 	}
42357c478bd9Sstevel@tonic-gate 	need = datalen;
42367c478bd9Sstevel@tonic-gate 
42377c478bd9Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(data, datalen);
42387c478bd9Sstevel@tonic-gate 
42397c478bd9Sstevel@tonic-gate 	if (datalen != 0 && copyin(STRUCT_FGETP(sign_update, su_databuf),
42407c478bd9Sstevel@tonic-gate 	    data.cd_raw.iov_base, datalen) != 0) {
42417c478bd9Sstevel@tonic-gate 		error = EFAULT;
42427c478bd9Sstevel@tonic-gate 		goto release_minor;
42437c478bd9Sstevel@tonic-gate 	}
42447c478bd9Sstevel@tonic-gate 
4245cd964fceSMatt Barden 	ASSERT(update == crypto_sign_update ||
4246cd964fceSMatt Barden 	    update == crypto_verify_update ||
4247cd964fceSMatt Barden 	    update == crypto_mac_update);
4248cd964fceSMatt Barden 
4249cd964fceSMatt Barden 	if (update == crypto_sign_update)
4250cd964fceSMatt Barden 		ctxpp = &sp->sd_sign_ctx;
4251cd964fceSMatt Barden 	else if (update == crypto_verify_update)
4252cd964fceSMatt Barden 		ctxpp = &sp->sd_verify_ctx;
4253cd964fceSMatt Barden 	else
4254cd964fceSMatt Barden 		ctxpp = &sp->sd_mac_ctx;
42557c478bd9Sstevel@tonic-gate 
42567c478bd9Sstevel@tonic-gate 	rv = (update)(*ctxpp, &data, NULL);
42577c478bd9Sstevel@tonic-gate 	if (rv != CRYPTO_SUCCESS)
42587c478bd9Sstevel@tonic-gate 		CRYPTO_CANCEL_CTX(ctxpp);
42597c478bd9Sstevel@tonic-gate 
42607c478bd9Sstevel@tonic-gate release_minor:
42612d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
42622d794da1Skrishna 	CRYPTO_SESSION_RELE(sp);
42637c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
42647c478bd9Sstevel@tonic-gate 
42657c478bd9Sstevel@tonic-gate 	if (data.cd_raw.iov_base != NULL)
42667c478bd9Sstevel@tonic-gate 		kmem_free(data.cd_raw.iov_base, datalen);
42677c478bd9Sstevel@tonic-gate 
42687c478bd9Sstevel@tonic-gate 	if (error != 0)
42697c478bd9Sstevel@tonic-gate 		return (error);
42707c478bd9Sstevel@tonic-gate 
42717c478bd9Sstevel@tonic-gate 	STRUCT_FSET(sign_update, su_return_value, rv);
42727c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(sign_update), arg,
42737c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(sign_update)) != 0) {
42747c478bd9Sstevel@tonic-gate 		return (EFAULT);
42757c478bd9Sstevel@tonic-gate 	}
42767c478bd9Sstevel@tonic-gate 	return (0);
42777c478bd9Sstevel@tonic-gate }
42787c478bd9Sstevel@tonic-gate 
42797c478bd9Sstevel@tonic-gate /* ARGSUSED */
42807c478bd9Sstevel@tonic-gate static int
sign_final(dev_t dev,caddr_t arg,int mode,int * rval)42817c478bd9Sstevel@tonic-gate sign_final(dev_t dev, caddr_t arg, int mode, int *rval)
42827c478bd9Sstevel@tonic-gate {
42837c478bd9Sstevel@tonic-gate 	return (common_final(dev, arg, mode, crypto_sign_final));
42847c478bd9Sstevel@tonic-gate }
42857c478bd9Sstevel@tonic-gate 
42867c478bd9Sstevel@tonic-gate /*
42877c478bd9Sstevel@tonic-gate  * Can't use the common final because it does a copyout of
42887c478bd9Sstevel@tonic-gate  * the final part.
42897c478bd9Sstevel@tonic-gate  */
42907c478bd9Sstevel@tonic-gate /* ARGSUSED */
42917c478bd9Sstevel@tonic-gate static int
verify_final(dev_t dev,caddr_t arg,int mode,int * rval)42927c478bd9Sstevel@tonic-gate verify_final(dev_t dev, caddr_t arg, int mode, int *rval)
42937c478bd9Sstevel@tonic-gate {
42947c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_verify_final, verify_final);
42957c478bd9Sstevel@tonic-gate 	crypto_session_id_t session_id;
42967c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
42972d794da1Skrishna 	crypto_session_data_t *sp = NULL;
42987c478bd9Sstevel@tonic-gate 	crypto_data_t sign;
42997c478bd9Sstevel@tonic-gate 	size_t signlen, need = 0;
43007c478bd9Sstevel@tonic-gate 	int error = 0;
43017c478bd9Sstevel@tonic-gate 	int rv;
43022d794da1Skrishna 	boolean_t rctl_chk = B_FALSE;
43037c478bd9Sstevel@tonic-gate 
43047c478bd9Sstevel@tonic-gate 	STRUCT_INIT(verify_final, mode);
43057c478bd9Sstevel@tonic-gate 
43067c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
43077c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "verify_final: failed holding minor");
43087c478bd9Sstevel@tonic-gate 		return (ENXIO);
43097c478bd9Sstevel@tonic-gate 	}
43107c478bd9Sstevel@tonic-gate 
43117c478bd9Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(verify_final),
43127c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(verify_final)) != 0) {
43137c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
43147c478bd9Sstevel@tonic-gate 		return (EFAULT);
43157c478bd9Sstevel@tonic-gate 	}
43167c478bd9Sstevel@tonic-gate 
43177c478bd9Sstevel@tonic-gate 	sign.cd_raw.iov_base = NULL;
43187c478bd9Sstevel@tonic-gate 
43197c478bd9Sstevel@tonic-gate 	signlen = STRUCT_FGET(verify_final, vf_signlen);
43207c478bd9Sstevel@tonic-gate 	if (signlen > crypto_max_buffer_len) {
43217c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "verify_final: buffer greater than %ld "
43227c478bd9Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
43237c478bd9Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
43247c478bd9Sstevel@tonic-gate 		goto release_minor;
43257c478bd9Sstevel@tonic-gate 	}
43267c478bd9Sstevel@tonic-gate 
43272d794da1Skrishna 	session_id = STRUCT_FGET(verify_final, vf_session);
43282d794da1Skrishna 
43292d794da1Skrishna 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
43302d794da1Skrishna 		goto release_minor;
43312d794da1Skrishna 	}
43322d794da1Skrishna 
43332d794da1Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, signlen, rctl_chk)) !=
43342d794da1Skrishna 	    CRYPTO_SUCCESS) {
43357c478bd9Sstevel@tonic-gate 		goto release_minor;
43367c478bd9Sstevel@tonic-gate 	}
43377c478bd9Sstevel@tonic-gate 	need = signlen;
43387c478bd9Sstevel@tonic-gate 
43397c478bd9Sstevel@tonic-gate 	INIT_RAW_CRYPTO_DATA(sign, signlen);
43407c478bd9Sstevel@tonic-gate 
43417c478bd9Sstevel@tonic-gate 	if (signlen != 0 && copyin(STRUCT_FGETP(verify_final, vf_signbuf),
43427c478bd9Sstevel@tonic-gate 	    sign.cd_raw.iov_base, signlen) != 0) {
43437c478bd9Sstevel@tonic-gate 		error = EFAULT;
43447c478bd9Sstevel@tonic-gate 		goto release_minor;
43457c478bd9Sstevel@tonic-gate 	}
43467c478bd9Sstevel@tonic-gate 
43477c478bd9Sstevel@tonic-gate 	rv = crypto_verify_final(sp->sd_verify_ctx, &sign, NULL);
43487c478bd9Sstevel@tonic-gate 	if (KCF_CONTEXT_DONE(rv))
43497c478bd9Sstevel@tonic-gate 		sp->sd_verify_ctx = NULL;
43507c478bd9Sstevel@tonic-gate 
43517c478bd9Sstevel@tonic-gate release_minor:
43522d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
43532d794da1Skrishna 	CRYPTO_SESSION_RELE(sp);
43547c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
43557c478bd9Sstevel@tonic-gate 
43567c478bd9Sstevel@tonic-gate 	if (sign.cd_raw.iov_base != NULL)
43577c478bd9Sstevel@tonic-gate 		kmem_free(sign.cd_raw.iov_base, signlen);
43587c478bd9Sstevel@tonic-gate 
43597c478bd9Sstevel@tonic-gate 	if (error != 0)
43607c478bd9Sstevel@tonic-gate 		return (error);
43617c478bd9Sstevel@tonic-gate 
43627c478bd9Sstevel@tonic-gate 	STRUCT_FSET(verify_final, vf_return_value, rv);
43637c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(verify_final), arg,
43647c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(verify_final)) != 0) {
43657c478bd9Sstevel@tonic-gate 		return (EFAULT);
43667c478bd9Sstevel@tonic-gate 	}
43677c478bd9Sstevel@tonic-gate 	return (0);
43687c478bd9Sstevel@tonic-gate }
43697c478bd9Sstevel@tonic-gate 
43707c478bd9Sstevel@tonic-gate /* ARGSUSED */
43717c478bd9Sstevel@tonic-gate static int
seed_random(dev_t dev,caddr_t arg,int mode,int * rval)43727c478bd9Sstevel@tonic-gate seed_random(dev_t dev, caddr_t arg, int mode, int *rval)
43737c478bd9Sstevel@tonic-gate {
43747c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_seed_random, seed_random);
4375894b2776Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
43767c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
43777c478bd9Sstevel@tonic-gate 	crypto_session_id_t session_id;
43787c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
43792d794da1Skrishna 	crypto_session_data_t *sp = NULL;
43807c478bd9Sstevel@tonic-gate 	uchar_t *seed_buffer = NULL;
43817c478bd9Sstevel@tonic-gate 	size_t seed_len;
43827c478bd9Sstevel@tonic-gate 	size_t need = 0;
43837c478bd9Sstevel@tonic-gate 	int error = 0;
43847c478bd9Sstevel@tonic-gate 	int rv;
43852d794da1Skrishna 	boolean_t rctl_chk = B_FALSE;
43867c478bd9Sstevel@tonic-gate 
43877c478bd9Sstevel@tonic-gate 	STRUCT_INIT(seed_random, mode);
43887c478bd9Sstevel@tonic-gate 
43897c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
43907c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "seed_random: failed holding minor");
43917c478bd9Sstevel@tonic-gate 		return (ENXIO);
43927c478bd9Sstevel@tonic-gate 	}
43937c478bd9Sstevel@tonic-gate 
43947c478bd9Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(seed_random),
43957c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(seed_random)) != 0) {
43967c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
43977c478bd9Sstevel@tonic-gate 		return (EFAULT);
43987c478bd9Sstevel@tonic-gate 	}
43997c478bd9Sstevel@tonic-gate 
44007c478bd9Sstevel@tonic-gate 	seed_len = STRUCT_FGET(seed_random, sr_seedlen);
44017c478bd9Sstevel@tonic-gate 	if (seed_len > crypto_max_buffer_len) {
44027c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "seed_random: buffer greater than %ld "
44037c478bd9Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
44047c478bd9Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
44057c478bd9Sstevel@tonic-gate 		goto release_minor;
44067c478bd9Sstevel@tonic-gate 	}
44077c478bd9Sstevel@tonic-gate 
44082d794da1Skrishna 	session_id = STRUCT_FGET(seed_random, sr_session);
44092d794da1Skrishna 
44102d794da1Skrishna 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
44112d794da1Skrishna 		goto release_minor;
44122d794da1Skrishna 	}
44132d794da1Skrishna 
44142d794da1Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, seed_len, rctl_chk)) !=
44152d794da1Skrishna 	    CRYPTO_SUCCESS) {
44167c478bd9Sstevel@tonic-gate 		goto release_minor;
44177c478bd9Sstevel@tonic-gate 	}
44187c478bd9Sstevel@tonic-gate 	need = seed_len;
44197c478bd9Sstevel@tonic-gate 	seed_buffer = kmem_alloc(seed_len, KM_SLEEP);
44207c478bd9Sstevel@tonic-gate 
44217c478bd9Sstevel@tonic-gate 	if (seed_len != 0 && copyin(STRUCT_FGETP(seed_random, sr_seedbuf),
44227c478bd9Sstevel@tonic-gate 	    seed_buffer, seed_len) != 0) {
44237c478bd9Sstevel@tonic-gate 		error = EFAULT;
44247c478bd9Sstevel@tonic-gate 		goto release_minor;
44257c478bd9Sstevel@tonic-gate 	}
44267c478bd9Sstevel@tonic-gate 
442772eff6e2Smcpowers 	if ((rv = kcf_get_hardware_provider_nomech(
4428894b2776Smcpowers 	    CRYPTO_OPS_OFFSET(random_ops), CRYPTO_RANDOM_OFFSET(seed_random),
44299b009fc1SValerie Bubb Fenwick 	    sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
44302d794da1Skrishna 		goto release_minor;
44317c478bd9Sstevel@tonic-gate 	}
44327c478bd9Sstevel@tonic-gate 
44337c478bd9Sstevel@tonic-gate 	KCF_WRAP_RANDOM_OPS_PARAMS(&params, KCF_OP_RANDOM_SEED,
44348047c9fbSmcpowers 	    sp->sd_provider_session->ps_session, seed_buffer, seed_len, 0,
44358047c9fbSmcpowers 	    CRYPTO_SEED_NOW);
44367c478bd9Sstevel@tonic-gate 
44377c478bd9Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
44387c478bd9Sstevel@tonic-gate 
44397c478bd9Sstevel@tonic-gate release_minor:
44402d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
44412d794da1Skrishna 	CRYPTO_SESSION_RELE(sp);
44427c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
44437c478bd9Sstevel@tonic-gate 
4444894b2776Smcpowers 	if (real_provider != NULL)
4445894b2776Smcpowers 		KCF_PROV_REFRELE(real_provider);
4446894b2776Smcpowers 
44477c478bd9Sstevel@tonic-gate 	if (seed_buffer != NULL)
44487c478bd9Sstevel@tonic-gate 		kmem_free(seed_buffer, seed_len);
44497c478bd9Sstevel@tonic-gate 
44507c478bd9Sstevel@tonic-gate 	if (error != 0)
44517c478bd9Sstevel@tonic-gate 		return (error);
44527c478bd9Sstevel@tonic-gate 
44537c478bd9Sstevel@tonic-gate 	STRUCT_FSET(seed_random, sr_return_value, rv);
44547c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(seed_random), arg,
44557c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(seed_random)) != 0) {
44567c478bd9Sstevel@tonic-gate 		return (EFAULT);
44577c478bd9Sstevel@tonic-gate 	}
44587c478bd9Sstevel@tonic-gate 	return (0);
44597c478bd9Sstevel@tonic-gate }
44607c478bd9Sstevel@tonic-gate 
44617c478bd9Sstevel@tonic-gate /* ARGSUSED */
44627c478bd9Sstevel@tonic-gate static int
generate_random(dev_t dev,caddr_t arg,int mode,int * rval)44637c478bd9Sstevel@tonic-gate generate_random(dev_t dev, caddr_t arg, int mode, int *rval)
44647c478bd9Sstevel@tonic-gate {
44657c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_generate_random, generate_random);
4466894b2776Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
44677c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
44687c478bd9Sstevel@tonic-gate 	crypto_session_id_t session_id;
44697c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
44702d794da1Skrishna 	crypto_session_data_t *sp = NULL;
44717c478bd9Sstevel@tonic-gate 	uchar_t *buffer = NULL;
44727c478bd9Sstevel@tonic-gate 	size_t len;
44737c478bd9Sstevel@tonic-gate 	size_t need = 0;
44747c478bd9Sstevel@tonic-gate 	int error = 0;
44757c478bd9Sstevel@tonic-gate 	int rv;
44762d794da1Skrishna 	boolean_t rctl_chk = B_FALSE;
44777c478bd9Sstevel@tonic-gate 
44787c478bd9Sstevel@tonic-gate 	STRUCT_INIT(generate_random, mode);
44797c478bd9Sstevel@tonic-gate 
44807c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
44817c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "generate_random: failed holding minor");
44827c478bd9Sstevel@tonic-gate 		return (ENXIO);
44837c478bd9Sstevel@tonic-gate 	}
44847c478bd9Sstevel@tonic-gate 
44857c478bd9Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(generate_random),
44867c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(generate_random)) != 0) {
44877c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
44887c478bd9Sstevel@tonic-gate 		return (EFAULT);
44897c478bd9Sstevel@tonic-gate 	}
44907c478bd9Sstevel@tonic-gate 
44917c478bd9Sstevel@tonic-gate 	len = STRUCT_FGET(generate_random, gr_buflen);
44927c478bd9Sstevel@tonic-gate 	if (len > crypto_max_buffer_len) {
44937c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "generate_random: buffer greater than %ld "
44947c478bd9Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
44957c478bd9Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
44967c478bd9Sstevel@tonic-gate 		goto release_minor;
44977c478bd9Sstevel@tonic-gate 	}
44987c478bd9Sstevel@tonic-gate 
44997c478bd9Sstevel@tonic-gate 	session_id = STRUCT_FGET(generate_random, gr_session);
45007c478bd9Sstevel@tonic-gate 
45017c478bd9Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
45027c478bd9Sstevel@tonic-gate 		goto release_minor;
45037c478bd9Sstevel@tonic-gate 	}
45047c478bd9Sstevel@tonic-gate 
45052d794da1Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, len, rctl_chk)) !=
45062d794da1Skrishna 	    CRYPTO_SUCCESS) {
45072d794da1Skrishna 		goto release_minor;
45082d794da1Skrishna 	}
45092d794da1Skrishna 	need = len;
45102d794da1Skrishna 	buffer = kmem_alloc(len, KM_SLEEP);
45112d794da1Skrishna 
451272eff6e2Smcpowers 	if ((rv = kcf_get_hardware_provider_nomech(
45137c478bd9Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(random_ops),
45149b009fc1SValerie Bubb Fenwick 	    CRYPTO_RANDOM_OFFSET(generate_random), sp->sd_provider,
45159b009fc1SValerie Bubb Fenwick 	    &real_provider)) != CRYPTO_SUCCESS) {
45162d794da1Skrishna 		goto release_minor;
45177c478bd9Sstevel@tonic-gate 	}
45187c478bd9Sstevel@tonic-gate 
45197c478bd9Sstevel@tonic-gate 	KCF_WRAP_RANDOM_OPS_PARAMS(&params, KCF_OP_RANDOM_GENERATE,
45208047c9fbSmcpowers 	    sp->sd_provider_session->ps_session, buffer, len, 0, 0);
45217c478bd9Sstevel@tonic-gate 
45227c478bd9Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
45237c478bd9Sstevel@tonic-gate 
45247c478bd9Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
45257c478bd9Sstevel@tonic-gate 		if (len != 0 && copyout(buffer,
45267c478bd9Sstevel@tonic-gate 		    STRUCT_FGETP(generate_random, gr_buf), len) != 0) {
45277c478bd9Sstevel@tonic-gate 			error = EFAULT;
45287c478bd9Sstevel@tonic-gate 		}
45297c478bd9Sstevel@tonic-gate 	}
45307c478bd9Sstevel@tonic-gate 
45317c478bd9Sstevel@tonic-gate release_minor:
45322d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
45332d794da1Skrishna 	CRYPTO_SESSION_RELE(sp);
45347c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
45357c478bd9Sstevel@tonic-gate 
4536894b2776Smcpowers 	if (real_provider != NULL)
4537894b2776Smcpowers 		KCF_PROV_REFRELE(real_provider);
4538894b2776Smcpowers 
45397c478bd9Sstevel@tonic-gate 	if (buffer != NULL) {
45407c478bd9Sstevel@tonic-gate 		/* random numbers are often used to create keys */
45417c478bd9Sstevel@tonic-gate 		bzero(buffer, len);
45427c478bd9Sstevel@tonic-gate 		kmem_free(buffer, len);
45437c478bd9Sstevel@tonic-gate 	}
45447c478bd9Sstevel@tonic-gate 
45457c478bd9Sstevel@tonic-gate 	if (error != 0)
45467c478bd9Sstevel@tonic-gate 		return (error);
45477c478bd9Sstevel@tonic-gate 
45487c478bd9Sstevel@tonic-gate 	STRUCT_FSET(generate_random, gr_return_value, rv);
45497c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(generate_random), arg,
45507c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(generate_random)) != 0) {
45517c478bd9Sstevel@tonic-gate 		return (EFAULT);
45527c478bd9Sstevel@tonic-gate 	}
45537c478bd9Sstevel@tonic-gate 	return (0);
45547c478bd9Sstevel@tonic-gate }
45557c478bd9Sstevel@tonic-gate 
45567c478bd9Sstevel@tonic-gate /*
45577c478bd9Sstevel@tonic-gate  * Copyout a kernel array of attributes to user space.
45587c478bd9Sstevel@tonic-gate  * u_attrs is the corresponding user space array containing
45597c478bd9Sstevel@tonic-gate  * user space pointers necessary for the copyout.
45607c478bd9Sstevel@tonic-gate  */
45617c478bd9Sstevel@tonic-gate /* ARGSUSED */
45627c478bd9Sstevel@tonic-gate static int
copyout_attributes(int mode,caddr_t out,uint_t count,crypto_object_attribute_t * k_attrs,caddr_t u_attrs)45637c478bd9Sstevel@tonic-gate copyout_attributes(int mode, caddr_t out, uint_t count,
45647c478bd9Sstevel@tonic-gate     crypto_object_attribute_t *k_attrs, caddr_t u_attrs)
45657c478bd9Sstevel@tonic-gate {
45667c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_attribute, oa);
45677c478bd9Sstevel@tonic-gate 	caddr_t p, valuep;
45687c478bd9Sstevel@tonic-gate 	size_t value_len;
45697c478bd9Sstevel@tonic-gate 	size_t len;
45707c478bd9Sstevel@tonic-gate 	int i;
45717c478bd9Sstevel@tonic-gate 	int error = 0;
45727c478bd9Sstevel@tonic-gate 
45737c478bd9Sstevel@tonic-gate 	if (count == 0)
45747c478bd9Sstevel@tonic-gate 		return (0);
45757c478bd9Sstevel@tonic-gate 
45767c478bd9Sstevel@tonic-gate 	STRUCT_INIT(oa, mode);
45777c478bd9Sstevel@tonic-gate 
45787c478bd9Sstevel@tonic-gate 	len = count * STRUCT_SIZE(oa);
45797c478bd9Sstevel@tonic-gate 
45807c478bd9Sstevel@tonic-gate 	ASSERT(u_attrs != NULL);
45817c478bd9Sstevel@tonic-gate 	p = u_attrs;
45827c478bd9Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
45837c478bd9Sstevel@tonic-gate 		/* can this bcopy be eliminated? */
45847c478bd9Sstevel@tonic-gate 		bcopy(p, STRUCT_BUF(oa), STRUCT_SIZE(oa));
45857c478bd9Sstevel@tonic-gate 		value_len = k_attrs[i].oa_value_len;
45867c478bd9Sstevel@tonic-gate 		STRUCT_FSET(oa, oa_type, k_attrs[i].oa_type);
458795014fbbSDan OpenSolaris Anderson 		STRUCT_FSET(oa, oa_value_len, (ssize_t)value_len);
45887c478bd9Sstevel@tonic-gate 		valuep = STRUCT_FGETP(oa, oa_value);
458995014fbbSDan OpenSolaris Anderson 		if ((valuep != NULL) && (value_len != (size_t)-1)) {
45907c478bd9Sstevel@tonic-gate 			if (copyout(k_attrs[i].oa_value,
45917c478bd9Sstevel@tonic-gate 			    valuep, value_len) != 0) {
45927c478bd9Sstevel@tonic-gate 				error = EFAULT;
45937c478bd9Sstevel@tonic-gate 				goto out;
45947c478bd9Sstevel@tonic-gate 			}
45957c478bd9Sstevel@tonic-gate 		}
45967c478bd9Sstevel@tonic-gate 		bcopy(STRUCT_BUF(oa), p, STRUCT_SIZE(oa));
45977c478bd9Sstevel@tonic-gate 		p += STRUCT_SIZE(oa);
45987c478bd9Sstevel@tonic-gate 	}
45997c478bd9Sstevel@tonic-gate 	if (copyout(u_attrs, out, len)) {
46007c478bd9Sstevel@tonic-gate 		error = EFAULT;
46017c478bd9Sstevel@tonic-gate 	}
46027c478bd9Sstevel@tonic-gate out:
46037c478bd9Sstevel@tonic-gate 	return (error);
46047c478bd9Sstevel@tonic-gate }
46057c478bd9Sstevel@tonic-gate 
46067c478bd9Sstevel@tonic-gate 
46077c478bd9Sstevel@tonic-gate /* ARGSUSED */
46087c478bd9Sstevel@tonic-gate static int
object_create(dev_t dev,caddr_t arg,int mode,int * rval)46097c478bd9Sstevel@tonic-gate object_create(dev_t dev, caddr_t arg, int mode, int *rval)
46107c478bd9Sstevel@tonic-gate {
46117c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_create, object_create);
4612894b2776Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
46137c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
46147c478bd9Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
46157c478bd9Sstevel@tonic-gate 	crypto_session_id_t session_id;
46167c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
46177c478bd9Sstevel@tonic-gate 	crypto_session_data_t *sp = NULL;
46187c478bd9Sstevel@tonic-gate 	crypto_object_id_t object_handle;
46197c478bd9Sstevel@tonic-gate 	caddr_t oc_attributes;
46207c478bd9Sstevel@tonic-gate 	size_t k_attrs_size;
46217c478bd9Sstevel@tonic-gate 	size_t rctl_bytes = 0;
46222d794da1Skrishna 	boolean_t rctl_chk = B_FALSE;
46237c478bd9Sstevel@tonic-gate 	int error = 0;
46247c478bd9Sstevel@tonic-gate 	int rv;
46257c478bd9Sstevel@tonic-gate 	uint_t count;
46267c478bd9Sstevel@tonic-gate 
46277c478bd9Sstevel@tonic-gate 	STRUCT_INIT(object_create, mode);
46287c478bd9Sstevel@tonic-gate 
46297c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
46307c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_create: failed holding minor");
46317c478bd9Sstevel@tonic-gate 		return (ENXIO);
46327c478bd9Sstevel@tonic-gate 	}
46337c478bd9Sstevel@tonic-gate 
46347c478bd9Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(object_create),
46357c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(object_create)) != 0) {
46367c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
46377c478bd9Sstevel@tonic-gate 		return (EFAULT);
46387c478bd9Sstevel@tonic-gate 	}
46397c478bd9Sstevel@tonic-gate 
46407c478bd9Sstevel@tonic-gate 	count = STRUCT_FGET(object_create, oc_count);
46417c478bd9Sstevel@tonic-gate 	oc_attributes = STRUCT_FGETP(object_create, oc_attributes);
46427c478bd9Sstevel@tonic-gate 
46437c478bd9Sstevel@tonic-gate 	session_id = STRUCT_FGET(object_create, oc_session);
46447c478bd9Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
46457c478bd9Sstevel@tonic-gate 		goto release_minor;
46467c478bd9Sstevel@tonic-gate 	}
46472d794da1Skrishna 	if (!copyin_attributes(mode, sp, count, oc_attributes, &k_attrs,
46482d794da1Skrishna 	    &k_attrs_size, NULL, &rv, &error, &rctl_bytes,
46492d794da1Skrishna 	    &rctl_chk, B_TRUE)) {
46502d794da1Skrishna 		goto release_minor;
46512d794da1Skrishna 	}
46527c478bd9Sstevel@tonic-gate 
46537c478bd9Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
46547c478bd9Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
46559b009fc1SValerie Bubb Fenwick 	    CRYPTO_OBJECT_OFFSET(object_create), sp->sd_provider,
46569b009fc1SValerie Bubb Fenwick 	    &real_provider)) != CRYPTO_SUCCESS) {
46577c478bd9Sstevel@tonic-gate 		goto release_minor;
46587c478bd9Sstevel@tonic-gate 	}
46597c478bd9Sstevel@tonic-gate 
46607c478bd9Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_CREATE,
46617c478bd9Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, 0, k_attrs, count,
46627c478bd9Sstevel@tonic-gate 	    &object_handle, 0, NULL, NULL, 0, NULL);
46637c478bd9Sstevel@tonic-gate 
46647c478bd9Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
46657c478bd9Sstevel@tonic-gate 
46667c478bd9Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS)
46677c478bd9Sstevel@tonic-gate 		STRUCT_FSET(object_create, oc_handle, object_handle);
46687c478bd9Sstevel@tonic-gate 
46697c478bd9Sstevel@tonic-gate release_minor:
46702d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk);
46717c478bd9Sstevel@tonic-gate 
46727c478bd9Sstevel@tonic-gate 	if (k_attrs != NULL)
46737c478bd9Sstevel@tonic-gate 		kmem_free(k_attrs, k_attrs_size);
46747c478bd9Sstevel@tonic-gate 
46757c478bd9Sstevel@tonic-gate 	if (error != 0)
46767c478bd9Sstevel@tonic-gate 		goto out;
46777c478bd9Sstevel@tonic-gate 
46787c478bd9Sstevel@tonic-gate 	STRUCT_FSET(object_create, oc_return_value, rv);
46797c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(object_create), arg,
46807c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(object_create)) != 0) {
46817c478bd9Sstevel@tonic-gate 		if (rv == CRYPTO_SUCCESS) {
46827c478bd9Sstevel@tonic-gate 			KCF_WRAP_OBJECT_OPS_PARAMS(&params,
46837c478bd9Sstevel@tonic-gate 			    KCF_OP_OBJECT_DESTROY,
46847c478bd9Sstevel@tonic-gate 			    sp->sd_provider_session->ps_session, object_handle,
46857c478bd9Sstevel@tonic-gate 			    NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
46867c478bd9Sstevel@tonic-gate 
46877c478bd9Sstevel@tonic-gate 			(void) kcf_submit_request(real_provider, NULL,
46887c478bd9Sstevel@tonic-gate 			    NULL, &params, B_FALSE);
46897c478bd9Sstevel@tonic-gate 
46907c478bd9Sstevel@tonic-gate 			error = EFAULT;
46917c478bd9Sstevel@tonic-gate 		}
46927c478bd9Sstevel@tonic-gate 	}
46937c478bd9Sstevel@tonic-gate out:
46942d794da1Skrishna 	CRYPTO_SESSION_RELE(sp);
46957c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
4696894b2776Smcpowers 	if (real_provider != NULL)
4697894b2776Smcpowers 		KCF_PROV_REFRELE(real_provider);
46987c478bd9Sstevel@tonic-gate 	return (error);
46997c478bd9Sstevel@tonic-gate }
47007c478bd9Sstevel@tonic-gate 
47017c478bd9Sstevel@tonic-gate /* ARGSUSED */
47027c478bd9Sstevel@tonic-gate static int
object_copy(dev_t dev,caddr_t arg,int mode,int * rval)47037c478bd9Sstevel@tonic-gate object_copy(dev_t dev, caddr_t arg, int mode, int *rval)
47047c478bd9Sstevel@tonic-gate {
47057c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_copy, object_copy);
4706894b2776Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
47077c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
47087c478bd9Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
47097c478bd9Sstevel@tonic-gate 	crypto_session_id_t session_id;
47107c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
47117c478bd9Sstevel@tonic-gate 	crypto_session_data_t *sp = NULL;
47127c478bd9Sstevel@tonic-gate 	crypto_object_id_t handle, new_handle;
47137c478bd9Sstevel@tonic-gate 	caddr_t oc_new_attributes;
47147c478bd9Sstevel@tonic-gate 	size_t k_attrs_size;
47157c478bd9Sstevel@tonic-gate 	size_t rctl_bytes = 0;
47162d794da1Skrishna 	boolean_t rctl_chk = B_FALSE;
47177c478bd9Sstevel@tonic-gate 	int error = 0;
47187c478bd9Sstevel@tonic-gate 	int rv;
47197c478bd9Sstevel@tonic-gate 	uint_t count;
47207c478bd9Sstevel@tonic-gate 
47217c478bd9Sstevel@tonic-gate 	STRUCT_INIT(object_copy, mode);
47227c478bd9Sstevel@tonic-gate 
47237c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
47247c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_copy: failed holding minor");
47257c478bd9Sstevel@tonic-gate 		return (ENXIO);
47267c478bd9Sstevel@tonic-gate 	}
47277c478bd9Sstevel@tonic-gate 
47287c478bd9Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(object_copy),
47297c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(object_copy)) != 0) {
47307c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
47317c478bd9Sstevel@tonic-gate 		return (EFAULT);
47327c478bd9Sstevel@tonic-gate 	}
47337c478bd9Sstevel@tonic-gate 
47347c478bd9Sstevel@tonic-gate 	count = STRUCT_FGET(object_copy, oc_count);
47357c478bd9Sstevel@tonic-gate 	oc_new_attributes = STRUCT_FGETP(object_copy, oc_new_attributes);
47367c478bd9Sstevel@tonic-gate 
47377c478bd9Sstevel@tonic-gate 	session_id = STRUCT_FGET(object_copy, oc_session);
47387c478bd9Sstevel@tonic-gate 
47397c478bd9Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
47407c478bd9Sstevel@tonic-gate 		goto release_minor;
47417c478bd9Sstevel@tonic-gate 	}
47422d794da1Skrishna 	if (!copyin_attributes(mode, sp, count, oc_new_attributes, &k_attrs,
47432d794da1Skrishna 	    &k_attrs_size, NULL, &rv, &error, &rctl_bytes,
47442d794da1Skrishna 	    &rctl_chk, B_TRUE)) {
47452d794da1Skrishna 		goto release_minor;
47462d794da1Skrishna 	}
47477c478bd9Sstevel@tonic-gate 
47487c478bd9Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
47497c478bd9Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
47509b009fc1SValerie Bubb Fenwick 	    CRYPTO_OBJECT_OFFSET(object_copy), sp->sd_provider,
47519b009fc1SValerie Bubb Fenwick 	    &real_provider)) != CRYPTO_SUCCESS) {
47527c478bd9Sstevel@tonic-gate 		goto release_minor;
47537c478bd9Sstevel@tonic-gate 	}
47547c478bd9Sstevel@tonic-gate 
47557c478bd9Sstevel@tonic-gate 	handle = STRUCT_FGET(object_copy, oc_handle);
47567c478bd9Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_COPY,
47577c478bd9Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, handle, k_attrs, count,
47587c478bd9Sstevel@tonic-gate 	    &new_handle, 0, NULL, NULL, 0, NULL);
47597c478bd9Sstevel@tonic-gate 
47607c478bd9Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
47617c478bd9Sstevel@tonic-gate 
47627c478bd9Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS)
47637c478bd9Sstevel@tonic-gate 		STRUCT_FSET(object_copy, oc_new_handle, new_handle);
47647c478bd9Sstevel@tonic-gate 
47657c478bd9Sstevel@tonic-gate release_minor:
47662d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk);
47677c478bd9Sstevel@tonic-gate 
47687c478bd9Sstevel@tonic-gate 	if (k_attrs != NULL)
47697c478bd9Sstevel@tonic-gate 		kmem_free(k_attrs, k_attrs_size);
47707c478bd9Sstevel@tonic-gate 
47717c478bd9Sstevel@tonic-gate 	if (error != 0)
47727c478bd9Sstevel@tonic-gate 		goto out;
47737c478bd9Sstevel@tonic-gate 
47747c478bd9Sstevel@tonic-gate 	STRUCT_FSET(object_copy, oc_return_value, rv);
47757c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(object_copy), arg,
47767c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(object_copy)) != 0) {
47777c478bd9Sstevel@tonic-gate 		if (rv == CRYPTO_SUCCESS) {
47787c478bd9Sstevel@tonic-gate 			KCF_WRAP_OBJECT_OPS_PARAMS(&params,
47797c478bd9Sstevel@tonic-gate 			    KCF_OP_OBJECT_DESTROY,
47807c478bd9Sstevel@tonic-gate 			    sp->sd_provider_session->ps_session, new_handle,
47817c478bd9Sstevel@tonic-gate 			    NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
47827c478bd9Sstevel@tonic-gate 
47837c478bd9Sstevel@tonic-gate 			(void) kcf_submit_request(real_provider, NULL,
47847c478bd9Sstevel@tonic-gate 			    NULL, &params, B_FALSE);
47857c478bd9Sstevel@tonic-gate 
47867c478bd9Sstevel@tonic-gate 			error = EFAULT;
47877c478bd9Sstevel@tonic-gate 		}
47887c478bd9Sstevel@tonic-gate 	}
47897c478bd9Sstevel@tonic-gate out:
47902d794da1Skrishna 	CRYPTO_SESSION_RELE(sp);
47917c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
4792894b2776Smcpowers 	if (real_provider != NULL)
4793894b2776Smcpowers 		KCF_PROV_REFRELE(real_provider);
47947c478bd9Sstevel@tonic-gate 	return (error);
47957c478bd9Sstevel@tonic-gate }
47967c478bd9Sstevel@tonic-gate 
47977c478bd9Sstevel@tonic-gate /* ARGSUSED */
47987c478bd9Sstevel@tonic-gate static int
object_destroy(dev_t dev,caddr_t arg,int mode,int * rval)47997c478bd9Sstevel@tonic-gate object_destroy(dev_t dev, caddr_t arg, int mode, int *rval)
48007c478bd9Sstevel@tonic-gate {
48017c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_destroy, object_destroy);
48027c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
48037c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
48047c478bd9Sstevel@tonic-gate 	crypto_session_id_t session_id;
48057c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
48067c478bd9Sstevel@tonic-gate 	crypto_session_data_t *sp;
48077c478bd9Sstevel@tonic-gate 	crypto_object_id_t handle;
48087c478bd9Sstevel@tonic-gate 	int error = 0;
48097c478bd9Sstevel@tonic-gate 	int rv;
48107c478bd9Sstevel@tonic-gate 
48117c478bd9Sstevel@tonic-gate 	STRUCT_INIT(object_destroy, mode);
48127c478bd9Sstevel@tonic-gate 
48137c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
48147c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_destroy: failed holding minor");
48157c478bd9Sstevel@tonic-gate 		return (ENXIO);
48167c478bd9Sstevel@tonic-gate 	}
48177c478bd9Sstevel@tonic-gate 
48187c478bd9Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(object_destroy),
48197c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(object_destroy)) != 0) {
48207c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
48217c478bd9Sstevel@tonic-gate 		return (EFAULT);
48227c478bd9Sstevel@tonic-gate 	}
48237c478bd9Sstevel@tonic-gate 
48247c478bd9Sstevel@tonic-gate 	session_id = STRUCT_FGET(object_destroy, od_session);
48257c478bd9Sstevel@tonic-gate 
48267c478bd9Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
48277c478bd9Sstevel@tonic-gate 		goto release_minor;
48287c478bd9Sstevel@tonic-gate 	}
48297c478bd9Sstevel@tonic-gate 
48307c478bd9Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
48317c478bd9Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
48329b009fc1SValerie Bubb Fenwick 	    CRYPTO_OBJECT_OFFSET(object_destroy), sp->sd_provider,
48339b009fc1SValerie Bubb Fenwick 	    &real_provider)) != CRYPTO_SUCCESS) {
48347c478bd9Sstevel@tonic-gate 		goto out;
48357c478bd9Sstevel@tonic-gate 	}
48367c478bd9Sstevel@tonic-gate 
48377c478bd9Sstevel@tonic-gate 	handle = STRUCT_FGET(object_destroy, od_handle);
48387c478bd9Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_DESTROY,
48397c478bd9Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, handle, NULL, 0, NULL, 0,
48407c478bd9Sstevel@tonic-gate 	    NULL, NULL, 0, NULL);
48417c478bd9Sstevel@tonic-gate 
48427c478bd9Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
4843894b2776Smcpowers 	KCF_PROV_REFRELE(real_provider);
48447c478bd9Sstevel@tonic-gate 
48457c478bd9Sstevel@tonic-gate out:
48467c478bd9Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
48477c478bd9Sstevel@tonic-gate 
48487c478bd9Sstevel@tonic-gate release_minor:
48497c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
48507c478bd9Sstevel@tonic-gate 
48517c478bd9Sstevel@tonic-gate 	if (error != 0)
48527c478bd9Sstevel@tonic-gate 		return (error);
48537c478bd9Sstevel@tonic-gate 
48547c478bd9Sstevel@tonic-gate 	STRUCT_FSET(object_destroy, od_return_value, rv);
48557c478bd9Sstevel@tonic-gate 
48567c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(object_destroy), arg,
48577c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(object_destroy)) != 0) {
48587c478bd9Sstevel@tonic-gate 		return (EFAULT);
48597c478bd9Sstevel@tonic-gate 	}
48607c478bd9Sstevel@tonic-gate 	return (0);
48617c478bd9Sstevel@tonic-gate }
48627c478bd9Sstevel@tonic-gate 
48637c478bd9Sstevel@tonic-gate /* ARGSUSED */
48647c478bd9Sstevel@tonic-gate static int
object_get_attribute_value(dev_t dev,caddr_t arg,int mode,int * rval)48657c478bd9Sstevel@tonic-gate object_get_attribute_value(dev_t dev, caddr_t arg, int mode, int *rval)
48667c478bd9Sstevel@tonic-gate {
48677c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_get_attribute_value, get_attribute_value);
486895014fbbSDan OpenSolaris Anderson #ifdef _LP64
486995014fbbSDan OpenSolaris Anderson 	STRUCT_DECL(crypto_object_attribute, oa);
487095014fbbSDan OpenSolaris Anderson #else
48717c478bd9Sstevel@tonic-gate 	/* LINTED E_FUNC_SET_NOT_USED */
48727c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_attribute, oa);
487395014fbbSDan OpenSolaris Anderson #endif
48747c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
48757c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
48767c478bd9Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
48777c478bd9Sstevel@tonic-gate 	crypto_session_id_t session_id;
48787c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
48792d794da1Skrishna 	crypto_session_data_t *sp = NULL;
48807c478bd9Sstevel@tonic-gate 	crypto_object_id_t handle;
48817c478bd9Sstevel@tonic-gate 	caddr_t og_attributes;
4882fdd76261SMark Powers 	caddr_t u_attrs = NULL;
48837c478bd9Sstevel@tonic-gate 	size_t k_attrs_size;
48847c478bd9Sstevel@tonic-gate 	size_t rctl_bytes = 0;
48852d794da1Skrishna 	boolean_t rctl_chk = B_FALSE;
48867c478bd9Sstevel@tonic-gate 	int error = 0;
48877c478bd9Sstevel@tonic-gate 	int rv;
48887c478bd9Sstevel@tonic-gate 	uint_t count;
48897c478bd9Sstevel@tonic-gate 
48907c478bd9Sstevel@tonic-gate 	STRUCT_INIT(get_attribute_value, mode);
48917c478bd9Sstevel@tonic-gate 	STRUCT_INIT(oa, mode);
48927c478bd9Sstevel@tonic-gate 
48937c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
48947c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
48957c478bd9Sstevel@tonic-gate 		    "object_get_attribute_value: failed holding minor");
48967c478bd9Sstevel@tonic-gate 		return (ENXIO);
48977c478bd9Sstevel@tonic-gate 	}
48987c478bd9Sstevel@tonic-gate 
48997c478bd9Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(get_attribute_value),
49007c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(get_attribute_value)) != 0) {
49017c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
49027c478bd9Sstevel@tonic-gate 		return (EFAULT);
49037c478bd9Sstevel@tonic-gate 	}
49047c478bd9Sstevel@tonic-gate 
49057c478bd9Sstevel@tonic-gate 	count = STRUCT_FGET(get_attribute_value, og_count);
49067c478bd9Sstevel@tonic-gate 	og_attributes = STRUCT_FGETP(get_attribute_value, og_attributes);
49077c478bd9Sstevel@tonic-gate 
49087c478bd9Sstevel@tonic-gate 	session_id = STRUCT_FGET(get_attribute_value, og_session);
49097c478bd9Sstevel@tonic-gate 
49107c478bd9Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
49117c478bd9Sstevel@tonic-gate 		goto release_minor;
49127c478bd9Sstevel@tonic-gate 	}
49132d794da1Skrishna 	if (!copyin_attributes(mode, sp, count, og_attributes, &k_attrs,
49142d794da1Skrishna 	    &k_attrs_size, &u_attrs, &rv, &error, &rctl_bytes,
49152d794da1Skrishna 	    &rctl_chk, B_FALSE)) {
49162d794da1Skrishna 		goto release_minor;
49172d794da1Skrishna 	}
49187c478bd9Sstevel@tonic-gate 
49197c478bd9Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
49207c478bd9Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
49217c478bd9Sstevel@tonic-gate 	    CRYPTO_OBJECT_OFFSET(object_get_attribute_value),
49229b009fc1SValerie Bubb Fenwick 	    sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
49237c478bd9Sstevel@tonic-gate 		goto out;
49247c478bd9Sstevel@tonic-gate 	}
49257c478bd9Sstevel@tonic-gate 
49267c478bd9Sstevel@tonic-gate 	handle = STRUCT_FGET(get_attribute_value, og_handle);
49277c478bd9Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_GET_ATTRIBUTE_VALUE,
49287c478bd9Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, handle, k_attrs, count, NULL,
49297c478bd9Sstevel@tonic-gate 	    0, NULL, NULL, 0, NULL);
49307c478bd9Sstevel@tonic-gate 
49317c478bd9Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
4932894b2776Smcpowers 	KCF_PROV_REFRELE(real_provider);
49337c478bd9Sstevel@tonic-gate 
49347c478bd9Sstevel@tonic-gate out:
49357c478bd9Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS || rv == CRYPTO_ATTRIBUTE_SENSITIVE ||
49367c478bd9Sstevel@tonic-gate 	    rv == CRYPTO_ATTRIBUTE_TYPE_INVALID ||
49377c478bd9Sstevel@tonic-gate 	    rv == CRYPTO_BUFFER_TOO_SMALL) {
49387c478bd9Sstevel@tonic-gate 		error = copyout_attributes(mode,
49397c478bd9Sstevel@tonic-gate 		    STRUCT_FGETP(get_attribute_value, og_attributes),
49407c478bd9Sstevel@tonic-gate 		    count, k_attrs, u_attrs);
49417c478bd9Sstevel@tonic-gate 	}
49427c478bd9Sstevel@tonic-gate 
49437c478bd9Sstevel@tonic-gate release_minor:
49442d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk);
49452d794da1Skrishna 	CRYPTO_SESSION_RELE(sp);
49467c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
49477c478bd9Sstevel@tonic-gate 
49487c478bd9Sstevel@tonic-gate 	if (k_attrs != NULL)
49497c478bd9Sstevel@tonic-gate 		kmem_free(k_attrs, k_attrs_size);
49507c478bd9Sstevel@tonic-gate 
49517c478bd9Sstevel@tonic-gate 	if (u_attrs != NULL)
49527c478bd9Sstevel@tonic-gate 		kmem_free(u_attrs, count * STRUCT_SIZE(oa));
49537c478bd9Sstevel@tonic-gate 
49547c478bd9Sstevel@tonic-gate 	if (error != 0)
49557c478bd9Sstevel@tonic-gate 		return (error);
49567c478bd9Sstevel@tonic-gate 
49577c478bd9Sstevel@tonic-gate 	STRUCT_FSET(get_attribute_value, og_return_value, rv);
49587c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(get_attribute_value), arg,
49597c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(get_attribute_value)) != 0) {
49607c478bd9Sstevel@tonic-gate 		return (EFAULT);
49617c478bd9Sstevel@tonic-gate 	}
49627c478bd9Sstevel@tonic-gate 	return (0);
49637c478bd9Sstevel@tonic-gate }
49647c478bd9Sstevel@tonic-gate 
49657c478bd9Sstevel@tonic-gate /* ARGSUSED */
49667c478bd9Sstevel@tonic-gate static int
object_get_size(dev_t dev,caddr_t arg,int mode,int * rval)49677c478bd9Sstevel@tonic-gate object_get_size(dev_t dev, caddr_t arg, int mode, int *rval)
49687c478bd9Sstevel@tonic-gate {
49697c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_get_size, object_get_size);
49707c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
49717c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
49727c478bd9Sstevel@tonic-gate 	crypto_session_id_t session_id;
49737c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
49742d794da1Skrishna 	crypto_session_data_t *sp = NULL;
49757c478bd9Sstevel@tonic-gate 	crypto_object_id_t handle;
49767c478bd9Sstevel@tonic-gate 	size_t size;
49777c478bd9Sstevel@tonic-gate 	int error = 0;
49787c478bd9Sstevel@tonic-gate 	int rv;
49797c478bd9Sstevel@tonic-gate 
49807c478bd9Sstevel@tonic-gate 	STRUCT_INIT(object_get_size, mode);
49817c478bd9Sstevel@tonic-gate 
49827c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
49837c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_get_size: failed holding minor");
49847c478bd9Sstevel@tonic-gate 		return (ENXIO);
49857c478bd9Sstevel@tonic-gate 	}
49867c478bd9Sstevel@tonic-gate 
49877c478bd9Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(object_get_size),
49887c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(object_get_size)) != 0) {
49897c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
49907c478bd9Sstevel@tonic-gate 		return (EFAULT);
49917c478bd9Sstevel@tonic-gate 	}
49927c478bd9Sstevel@tonic-gate 
49937c478bd9Sstevel@tonic-gate 	session_id = STRUCT_FGET(object_get_size, gs_session);
49947c478bd9Sstevel@tonic-gate 
49957c478bd9Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
49967c478bd9Sstevel@tonic-gate 		goto release_minor;
49977c478bd9Sstevel@tonic-gate 	}
49987c478bd9Sstevel@tonic-gate 
49997c478bd9Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
50007c478bd9Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
50019b009fc1SValerie Bubb Fenwick 	    CRYPTO_OBJECT_OFFSET(object_get_size),
50027c478bd9Sstevel@tonic-gate 	    sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
50032d794da1Skrishna 		goto release_minor;
50047c478bd9Sstevel@tonic-gate 	}
50057c478bd9Sstevel@tonic-gate 
50067c478bd9Sstevel@tonic-gate 	handle = STRUCT_FGET(object_get_size, gs_handle);
50077c478bd9Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_GET_SIZE,
50087c478bd9Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, handle, NULL, 0, NULL, &size,
50097c478bd9Sstevel@tonic-gate 	    NULL, NULL, 0, NULL);
50107c478bd9Sstevel@tonic-gate 
50117c478bd9Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
5012894b2776Smcpowers 	KCF_PROV_REFRELE(real_provider);
50137c478bd9Sstevel@tonic-gate 
50147c478bd9Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
501595014fbbSDan OpenSolaris Anderson 		STRUCT_FSET(object_get_size, gs_size, (ulong_t)size);
50167c478bd9Sstevel@tonic-gate 	}
50172d794da1Skrishna 
50187c478bd9Sstevel@tonic-gate release_minor:
50197c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
50202d794da1Skrishna 	CRYPTO_SESSION_RELE(sp);
50217c478bd9Sstevel@tonic-gate 
50227c478bd9Sstevel@tonic-gate 	if (error != 0)
50237c478bd9Sstevel@tonic-gate 		return (error);
50247c478bd9Sstevel@tonic-gate 
50257c478bd9Sstevel@tonic-gate 	STRUCT_FSET(object_get_size, gs_return_value, rv);
50267c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(object_get_size), arg,
50277c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(object_get_size)) != 0) {
50287c478bd9Sstevel@tonic-gate 		return (EFAULT);
50297c478bd9Sstevel@tonic-gate 	}
50307c478bd9Sstevel@tonic-gate 	return (0);
50317c478bd9Sstevel@tonic-gate }
50327c478bd9Sstevel@tonic-gate 
50337c478bd9Sstevel@tonic-gate /* ARGSUSED */
50347c478bd9Sstevel@tonic-gate static int
object_set_attribute_value(dev_t dev,caddr_t arg,int mode,int * rval)50357c478bd9Sstevel@tonic-gate object_set_attribute_value(dev_t dev, caddr_t arg, int mode, int *rval)
50367c478bd9Sstevel@tonic-gate {
50377c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_set_attribute_value, set_attribute_value);
50387c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
50397c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
50407c478bd9Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
50417c478bd9Sstevel@tonic-gate 	crypto_session_id_t session_id;
50427c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
50432d794da1Skrishna 	crypto_session_data_t *sp = NULL;
50447c478bd9Sstevel@tonic-gate 	crypto_object_id_t object_handle;
50457c478bd9Sstevel@tonic-gate 	caddr_t sa_attributes;
50467c478bd9Sstevel@tonic-gate 	size_t k_attrs_size;
50477c478bd9Sstevel@tonic-gate 	size_t rctl_bytes = 0;
50482d794da1Skrishna 	boolean_t rctl_chk = B_FALSE;
50497c478bd9Sstevel@tonic-gate 	int error = 0;
50507c478bd9Sstevel@tonic-gate 	int rv;
50517c478bd9Sstevel@tonic-gate 	uint_t count;
50527c478bd9Sstevel@tonic-gate 
50537c478bd9Sstevel@tonic-gate 	STRUCT_INIT(set_attribute_value, mode);
50547c478bd9Sstevel@tonic-gate 
50557c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
50567c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
50577c478bd9Sstevel@tonic-gate 		    "object_set_attribute_value: failed holding minor");
50587c478bd9Sstevel@tonic-gate 		return (ENXIO);
50597c478bd9Sstevel@tonic-gate 	}
50607c478bd9Sstevel@tonic-gate 
50617c478bd9Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(set_attribute_value),
50627c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(set_attribute_value)) != 0) {
50637c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
50647c478bd9Sstevel@tonic-gate 		return (EFAULT);
50657c478bd9Sstevel@tonic-gate 	}
50667c478bd9Sstevel@tonic-gate 
50677c478bd9Sstevel@tonic-gate 	count = STRUCT_FGET(set_attribute_value, sa_count);
50687c478bd9Sstevel@tonic-gate 	sa_attributes = STRUCT_FGETP(set_attribute_value, sa_attributes);
50697c478bd9Sstevel@tonic-gate 
50707c478bd9Sstevel@tonic-gate 	session_id = STRUCT_FGET(set_attribute_value, sa_session);
50717c478bd9Sstevel@tonic-gate 
50727c478bd9Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
50737c478bd9Sstevel@tonic-gate 		goto release_minor;
50747c478bd9Sstevel@tonic-gate 	}
50752d794da1Skrishna 	if (!copyin_attributes(mode, sp, count, sa_attributes, &k_attrs,
50762d794da1Skrishna 	    &k_attrs_size, NULL, &rv, &error, &rctl_bytes,
50772d794da1Skrishna 	    &rctl_chk, B_TRUE)) {
50782d794da1Skrishna 		goto release_minor;
50792d794da1Skrishna 	}
50807c478bd9Sstevel@tonic-gate 
50817c478bd9Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
50827c478bd9Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
50837c478bd9Sstevel@tonic-gate 	    CRYPTO_OBJECT_OFFSET(object_set_attribute_value),
50849b009fc1SValerie Bubb Fenwick 	    sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
50852d794da1Skrishna 		goto release_minor;
50867c478bd9Sstevel@tonic-gate 	}
50877c478bd9Sstevel@tonic-gate 
50887c478bd9Sstevel@tonic-gate 	object_handle = STRUCT_FGET(set_attribute_value, sa_handle);
50897c478bd9Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_SET_ATTRIBUTE_VALUE,
50907c478bd9Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, object_handle, k_attrs, count,
50917c478bd9Sstevel@tonic-gate 	    NULL, 0, NULL, NULL, 0, NULL);
50927c478bd9Sstevel@tonic-gate 
50937c478bd9Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
5094894b2776Smcpowers 	KCF_PROV_REFRELE(real_provider);
50957c478bd9Sstevel@tonic-gate 
50967c478bd9Sstevel@tonic-gate release_minor:
50972d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk);
50982d794da1Skrishna 	CRYPTO_SESSION_RELE(sp);
50997c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
51007c478bd9Sstevel@tonic-gate 
51017c478bd9Sstevel@tonic-gate 	if (k_attrs != NULL)
51027c478bd9Sstevel@tonic-gate 		kmem_free(k_attrs, k_attrs_size);
51037c478bd9Sstevel@tonic-gate 
51047c478bd9Sstevel@tonic-gate 	if (error != 0)
51057c478bd9Sstevel@tonic-gate 		return (error);
51067c478bd9Sstevel@tonic-gate 
51077c478bd9Sstevel@tonic-gate 	STRUCT_FSET(set_attribute_value, sa_return_value, rv);
51087c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(set_attribute_value), arg,
51097c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(set_attribute_value)) != 0) {
51107c478bd9Sstevel@tonic-gate 		return (EFAULT);
51117c478bd9Sstevel@tonic-gate 	}
51127c478bd9Sstevel@tonic-gate 	return (0);
51137c478bd9Sstevel@tonic-gate }
51147c478bd9Sstevel@tonic-gate 
51157c478bd9Sstevel@tonic-gate /* ARGSUSED */
51167c478bd9Sstevel@tonic-gate static int
object_find_init(dev_t dev,caddr_t arg,int mode,int * rval)51177c478bd9Sstevel@tonic-gate object_find_init(dev_t dev, caddr_t arg, int mode, int *rval)
51187c478bd9Sstevel@tonic-gate {
51197c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_find_init, find_init);
5120894b2776Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
51217c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
51227c478bd9Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
51237c478bd9Sstevel@tonic-gate 	crypto_session_id_t session_id;
51247c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
51252d794da1Skrishna 	crypto_session_data_t *sp = NULL;
51267c478bd9Sstevel@tonic-gate 	caddr_t attributes;
51277c478bd9Sstevel@tonic-gate 	size_t k_attrs_size;
51287c478bd9Sstevel@tonic-gate 	size_t rctl_bytes = 0;
51292d794da1Skrishna 	boolean_t rctl_chk = B_FALSE;
51307c478bd9Sstevel@tonic-gate 	int error = 0;
51317c478bd9Sstevel@tonic-gate 	int rv;
51327c478bd9Sstevel@tonic-gate 	uint_t count;
51337c478bd9Sstevel@tonic-gate 	void *cookie;
51347c478bd9Sstevel@tonic-gate 
51357c478bd9Sstevel@tonic-gate 	STRUCT_INIT(find_init, mode);
51367c478bd9Sstevel@tonic-gate 
51377c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
51387c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_find_init: failed holding minor");
51397c478bd9Sstevel@tonic-gate 		return (ENXIO);
51407c478bd9Sstevel@tonic-gate 	}
51417c478bd9Sstevel@tonic-gate 
51427c478bd9Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(find_init), STRUCT_SIZE(find_init)) != 0) {
51437c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
51447c478bd9Sstevel@tonic-gate 		return (EFAULT);
51457c478bd9Sstevel@tonic-gate 	}
51467c478bd9Sstevel@tonic-gate 
51477c478bd9Sstevel@tonic-gate 	count = STRUCT_FGET(find_init, fi_count);
51487c478bd9Sstevel@tonic-gate 	attributes = STRUCT_FGETP(find_init, fi_attributes);
51497c478bd9Sstevel@tonic-gate 
51507c478bd9Sstevel@tonic-gate 	session_id = STRUCT_FGET(find_init, fi_session);
51517c478bd9Sstevel@tonic-gate 
51527c478bd9Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
51537c478bd9Sstevel@tonic-gate 		goto release_minor;
51547c478bd9Sstevel@tonic-gate 	}
51552d794da1Skrishna 	if (!copyin_attributes(mode, sp, count, attributes, &k_attrs,
51562d794da1Skrishna 	    &k_attrs_size, NULL, &rv, &error, &rctl_bytes,
51572d794da1Skrishna 	    &rctl_chk, B_TRUE)) {
51582d794da1Skrishna 		goto release_minor;
51592d794da1Skrishna 	}
51607c478bd9Sstevel@tonic-gate 
51617c478bd9Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
51627c478bd9Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
51639b009fc1SValerie Bubb Fenwick 	    CRYPTO_OBJECT_OFFSET(object_find_init),
51647c478bd9Sstevel@tonic-gate 	    sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
51652d794da1Skrishna 		goto release_minor;
51667c478bd9Sstevel@tonic-gate 	}
51677c478bd9Sstevel@tonic-gate 
51687c478bd9Sstevel@tonic-gate 	/* check for an active find */
51697c478bd9Sstevel@tonic-gate 	if (sp->sd_find_init_cookie != NULL) {
51707c478bd9Sstevel@tonic-gate 		rv = CRYPTO_OPERATION_IS_ACTIVE;
51717c478bd9Sstevel@tonic-gate 		goto release_minor;
51727c478bd9Sstevel@tonic-gate 	}
51737c478bd9Sstevel@tonic-gate 
51747c478bd9Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_FIND_INIT,
51757c478bd9Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, 0, k_attrs, count, NULL, 0,
51767c478bd9Sstevel@tonic-gate 	    &cookie, NULL, 0, NULL);
51777c478bd9Sstevel@tonic-gate 
51787c478bd9Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
51797c478bd9Sstevel@tonic-gate 
51807c478bd9Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
51817c478bd9Sstevel@tonic-gate 		/*
51827c478bd9Sstevel@tonic-gate 		 * The cookie is allocated by a provider at the start of an
51837c478bd9Sstevel@tonic-gate 		 * object search.  It is freed when the search is terminated
51847c478bd9Sstevel@tonic-gate 		 * by a final operation, or when the session is closed.
51857c478bd9Sstevel@tonic-gate 		 * It contains state information about which object handles
51867c478bd9Sstevel@tonic-gate 		 * have been returned to the caller.
51877c478bd9Sstevel@tonic-gate 		 */
51887c478bd9Sstevel@tonic-gate 		sp->sd_find_init_cookie = cookie;
51897c478bd9Sstevel@tonic-gate 	}
51907c478bd9Sstevel@tonic-gate 
51917c478bd9Sstevel@tonic-gate release_minor:
51922d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk);
51932d794da1Skrishna 	CRYPTO_SESSION_RELE(sp);
51947c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
51957c478bd9Sstevel@tonic-gate 
5196894b2776Smcpowers 	if (real_provider != NULL)
5197894b2776Smcpowers 		KCF_PROV_REFRELE(real_provider);
5198894b2776Smcpowers 
51997c478bd9Sstevel@tonic-gate 	if (k_attrs != NULL)
52007c478bd9Sstevel@tonic-gate 		kmem_free(k_attrs, k_attrs_size);
52017c478bd9Sstevel@tonic-gate 
52027c478bd9Sstevel@tonic-gate 	if (error != 0)
52037c478bd9Sstevel@tonic-gate 		return (error);
52047c478bd9Sstevel@tonic-gate 
52057c478bd9Sstevel@tonic-gate 	STRUCT_FSET(find_init, fi_return_value, rv);
52067c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(find_init), arg, STRUCT_SIZE(find_init)) != 0) {
52077c478bd9Sstevel@tonic-gate 		return (EFAULT);
52087c478bd9Sstevel@tonic-gate 	}
52097c478bd9Sstevel@tonic-gate 	return (0);
52107c478bd9Sstevel@tonic-gate }
52117c478bd9Sstevel@tonic-gate 
52127c478bd9Sstevel@tonic-gate /* ARGSUSED */
52137c478bd9Sstevel@tonic-gate static int
object_find_update(dev_t dev,caddr_t arg,int mode,int * rval)52147c478bd9Sstevel@tonic-gate object_find_update(dev_t dev, caddr_t arg, int mode, int *rval)
52157c478bd9Sstevel@tonic-gate {
52167c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_find_update, find_update);
52177c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
52187c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
52197c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
52202d794da1Skrishna 	crypto_session_data_t *sp = NULL;
52217c478bd9Sstevel@tonic-gate 	crypto_object_id_t *buffer = NULL;
52227c478bd9Sstevel@tonic-gate 	crypto_session_id_t session_id;
52237c478bd9Sstevel@tonic-gate 	size_t len, rctl_bytes = 0;
52247c478bd9Sstevel@tonic-gate 	uint_t count, max_count;
52257c478bd9Sstevel@tonic-gate 	int rv, error = 0;
52262d794da1Skrishna 	boolean_t rctl_chk = B_FALSE;
52277c478bd9Sstevel@tonic-gate 
52287c478bd9Sstevel@tonic-gate 	STRUCT_INIT(find_update, mode);
52297c478bd9Sstevel@tonic-gate 
52307c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
52317c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_find_update: failed holding minor");
52327c478bd9Sstevel@tonic-gate 		return (ENXIO);
52337c478bd9Sstevel@tonic-gate 	}
52347c478bd9Sstevel@tonic-gate 
52357c478bd9Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(find_update),
52367c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(find_update)) != 0) {
52377c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
52387c478bd9Sstevel@tonic-gate 		return (EFAULT);
52397c478bd9Sstevel@tonic-gate 	}
52407c478bd9Sstevel@tonic-gate 
52417c478bd9Sstevel@tonic-gate 	max_count = STRUCT_FGET(find_update, fu_max_count);
52427c478bd9Sstevel@tonic-gate 	if (max_count > CRYPTO_MAX_FIND_COUNT) {
52437c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "object_find_update: count greater than %d, "
52447c478bd9Sstevel@tonic-gate 		    "pid = %d", CRYPTO_MAX_FIND_COUNT, curproc->p_pid);
52457c478bd9Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
52467c478bd9Sstevel@tonic-gate 		goto release_minor;
52477c478bd9Sstevel@tonic-gate 	}
52487c478bd9Sstevel@tonic-gate 	len = max_count * sizeof (crypto_object_id_t);
52497c478bd9Sstevel@tonic-gate 	session_id = STRUCT_FGET(find_update, fu_session);
52507c478bd9Sstevel@tonic-gate 
52517c478bd9Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
52527c478bd9Sstevel@tonic-gate 		goto release_minor;
52537c478bd9Sstevel@tonic-gate 	}
52542d794da1Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, len, rctl_chk)) !=
52552d794da1Skrishna 	    CRYPTO_SUCCESS) {
52562d794da1Skrishna 		goto release_minor;
52572d794da1Skrishna 	}
52582d794da1Skrishna 	rctl_bytes = len;
52592d794da1Skrishna 	buffer = kmem_alloc(len, KM_SLEEP);
52607c478bd9Sstevel@tonic-gate 
52617c478bd9Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
52627c478bd9Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
52639b009fc1SValerie Bubb Fenwick 	    CRYPTO_OBJECT_OFFSET(object_find), sp->sd_provider,
52649b009fc1SValerie Bubb Fenwick 	    &real_provider)) != CRYPTO_SUCCESS) {
52652d794da1Skrishna 		goto release_minor;
52667c478bd9Sstevel@tonic-gate 	}
52677c478bd9Sstevel@tonic-gate 
52687c478bd9Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_FIND,
52697c478bd9Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, 0, NULL, 0, buffer, 0,
52707c478bd9Sstevel@tonic-gate 	    NULL, sp->sd_find_init_cookie, max_count, &count);
52717c478bd9Sstevel@tonic-gate 
52727c478bd9Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
5273894b2776Smcpowers 	KCF_PROV_REFRELE(real_provider);
52747c478bd9Sstevel@tonic-gate 
52757c478bd9Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
52767c478bd9Sstevel@tonic-gate 		if (count > max_count) {
52777c478bd9Sstevel@tonic-gate 			/* bad bad provider */
52787c478bd9Sstevel@tonic-gate 			rv = CRYPTO_FAILED;
52797c478bd9Sstevel@tonic-gate 			goto release_minor;
52807c478bd9Sstevel@tonic-gate 		}
52817c478bd9Sstevel@tonic-gate 		if (count != 0) {
52827c478bd9Sstevel@tonic-gate 			/* copyout handles */
52837c478bd9Sstevel@tonic-gate 			if (copyout(buffer,
52847c478bd9Sstevel@tonic-gate 			    STRUCT_FGETP(find_update, fu_handles),
52857c478bd9Sstevel@tonic-gate 			    count * sizeof (crypto_object_id_t)) != 0) {
52867c478bd9Sstevel@tonic-gate 				error = EFAULT;
52877c478bd9Sstevel@tonic-gate 			}
52887c478bd9Sstevel@tonic-gate 		}
52897c478bd9Sstevel@tonic-gate 		STRUCT_FSET(find_update, fu_count, count);
52907c478bd9Sstevel@tonic-gate 	}
52917c478bd9Sstevel@tonic-gate 
52927c478bd9Sstevel@tonic-gate release_minor:
52932d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk);
52942d794da1Skrishna 	CRYPTO_SESSION_RELE(sp);
52957c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
52967c478bd9Sstevel@tonic-gate 
52977c478bd9Sstevel@tonic-gate 	if (buffer != NULL)
52987c478bd9Sstevel@tonic-gate 		kmem_free(buffer, len);
52997c478bd9Sstevel@tonic-gate 
53007c478bd9Sstevel@tonic-gate 	if (error != 0)
53017c478bd9Sstevel@tonic-gate 		return (error);
53027c478bd9Sstevel@tonic-gate 
53037c478bd9Sstevel@tonic-gate 	STRUCT_FSET(find_update, fu_return_value, rv);
53047c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(find_update), arg,
53057c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(find_update)) != 0) {
53067c478bd9Sstevel@tonic-gate 		return (EFAULT);
53077c478bd9Sstevel@tonic-gate 	}
53087c478bd9Sstevel@tonic-gate 
53097c478bd9Sstevel@tonic-gate 	return (0);
53107c478bd9Sstevel@tonic-gate }
53117c478bd9Sstevel@tonic-gate 
53127c478bd9Sstevel@tonic-gate /*
53137c478bd9Sstevel@tonic-gate  * Free provider-allocated storage used for find object searches.
53147c478bd9Sstevel@tonic-gate  */
53157c478bd9Sstevel@tonic-gate static int
crypto_free_find_ctx(crypto_session_data_t * sp)53167c478bd9Sstevel@tonic-gate crypto_free_find_ctx(crypto_session_data_t *sp)
53177c478bd9Sstevel@tonic-gate {
53187c478bd9Sstevel@tonic-gate 	kcf_provider_desc_t *real_provider;
53197c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
53207c478bd9Sstevel@tonic-gate 	int rv;
53217c478bd9Sstevel@tonic-gate 
53227c478bd9Sstevel@tonic-gate 	if ((rv = kcf_get_hardware_provider_nomech(
53237c478bd9Sstevel@tonic-gate 	    CRYPTO_OPS_OFFSET(object_ops),
53249b009fc1SValerie Bubb Fenwick 	    CRYPTO_OBJECT_OFFSET(object_find_final),
53257c478bd9Sstevel@tonic-gate 	    sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
53267c478bd9Sstevel@tonic-gate 		return (rv);
53277c478bd9Sstevel@tonic-gate 	}
53287c478bd9Sstevel@tonic-gate 
53297c478bd9Sstevel@tonic-gate 	KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_FIND_FINAL,
53307c478bd9Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, 0, NULL, 0, NULL, 0,
53317c478bd9Sstevel@tonic-gate 	    NULL, sp->sd_find_init_cookie, 0, NULL);
53327c478bd9Sstevel@tonic-gate 
5333894b2776Smcpowers 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
5334894b2776Smcpowers 	KCF_PROV_REFRELE(real_provider);
5335894b2776Smcpowers 	return (rv);
53367c478bd9Sstevel@tonic-gate }
53377c478bd9Sstevel@tonic-gate 
53387c478bd9Sstevel@tonic-gate /* ARGSUSED */
53397c478bd9Sstevel@tonic-gate static int
object_find_final(dev_t dev,caddr_t arg,int mode,int * rval)53407c478bd9Sstevel@tonic-gate object_find_final(dev_t dev, caddr_t arg, int mode, int *rval)
53417c478bd9Sstevel@tonic-gate {
53427c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_find_final, object_find_final);
53437c478bd9Sstevel@tonic-gate 	crypto_session_id_t session_id;
53447c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
53457c478bd9Sstevel@tonic-gate 	crypto_session_data_t *sp;
53467c478bd9Sstevel@tonic-gate 	int error = 0;
53477c478bd9Sstevel@tonic-gate 	int rv;
53487c478bd9Sstevel@tonic-gate 
53497c478bd9Sstevel@tonic-gate 	STRUCT_INIT(object_find_final, mode);
53507c478bd9Sstevel@tonic-gate 
53517c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
53527c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_find_final: failed holding minor");
53537c478bd9Sstevel@tonic-gate 		return (ENXIO);
53547c478bd9Sstevel@tonic-gate 	}
53557c478bd9Sstevel@tonic-gate 
53567c478bd9Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(object_find_final),
53577c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(object_find_final)) != 0) {
53587c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
53597c478bd9Sstevel@tonic-gate 		return (EFAULT);
53607c478bd9Sstevel@tonic-gate 	}
53617c478bd9Sstevel@tonic-gate 
53627c478bd9Sstevel@tonic-gate 	session_id = STRUCT_FGET(object_find_final, ff_session);
53637c478bd9Sstevel@tonic-gate 
53647c478bd9Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
53657c478bd9Sstevel@tonic-gate 		goto release_minor;
53667c478bd9Sstevel@tonic-gate 	}
53677c478bd9Sstevel@tonic-gate 
53687c478bd9Sstevel@tonic-gate 	if ((rv = crypto_free_find_ctx(sp)) == CRYPTO_SUCCESS) {
53697c478bd9Sstevel@tonic-gate 		sp->sd_find_init_cookie = NULL;
53707c478bd9Sstevel@tonic-gate 	}
53717c478bd9Sstevel@tonic-gate 
53727c478bd9Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
53737c478bd9Sstevel@tonic-gate 
53747c478bd9Sstevel@tonic-gate release_minor:
53757c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
53767c478bd9Sstevel@tonic-gate 
53777c478bd9Sstevel@tonic-gate 	if (error != 0)
53787c478bd9Sstevel@tonic-gate 		return (error);
53797c478bd9Sstevel@tonic-gate 
53807c478bd9Sstevel@tonic-gate 	STRUCT_FSET(object_find_final, ff_return_value, rv);
53817c478bd9Sstevel@tonic-gate 
53827c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(object_find_final), arg,
53837c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(object_find_final)) != 0) {
53847c478bd9Sstevel@tonic-gate 		return (EFAULT);
53857c478bd9Sstevel@tonic-gate 	}
53867c478bd9Sstevel@tonic-gate 	return (0);
53877c478bd9Sstevel@tonic-gate }
53887c478bd9Sstevel@tonic-gate 
53897c478bd9Sstevel@tonic-gate /* ARGSUSED */
53907c478bd9Sstevel@tonic-gate static int
object_generate_key(dev_t dev,caddr_t arg,int mode,int * rval)53917c478bd9Sstevel@tonic-gate object_generate_key(dev_t dev, caddr_t arg, int mode, int *rval)
53927c478bd9Sstevel@tonic-gate {
53937c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_generate_key, generate_key);
5394894b2776Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
53957c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
53967c478bd9Sstevel@tonic-gate 	crypto_mechanism_t mech;
53977c478bd9Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
53987c478bd9Sstevel@tonic-gate 	crypto_session_id_t session_id;
53997c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
54007c478bd9Sstevel@tonic-gate 	crypto_session_data_t *sp = NULL;
54017c478bd9Sstevel@tonic-gate 	crypto_object_id_t key_handle;
54027c478bd9Sstevel@tonic-gate 	caddr_t attributes;
54037c478bd9Sstevel@tonic-gate 	size_t k_attrs_size;
54047c478bd9Sstevel@tonic-gate 	size_t mech_rctl_bytes = 0, key_rctl_bytes = 0;
54052d794da1Skrishna 	boolean_t mech_rctl_chk = B_FALSE;
54062d794da1Skrishna 	boolean_t key_rctl_chk = B_FALSE;
54077c478bd9Sstevel@tonic-gate 	uint_t count;
54087c478bd9Sstevel@tonic-gate 	int error = 0;
54097c478bd9Sstevel@tonic-gate 	int rv;
5410894b2776Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
54117c478bd9Sstevel@tonic-gate 
54127c478bd9Sstevel@tonic-gate 	STRUCT_INIT(generate_key, mode);
54137c478bd9Sstevel@tonic-gate 
54147c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
54157c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_generate_key: failed holding minor");
54167c478bd9Sstevel@tonic-gate 		return (ENXIO);
54177c478bd9Sstevel@tonic-gate 	}
54187c478bd9Sstevel@tonic-gate 
54197c478bd9Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(generate_key),
54207c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(generate_key)) != 0) {
54217c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
54227c478bd9Sstevel@tonic-gate 		return (EFAULT);
54237c478bd9Sstevel@tonic-gate 	}
54247c478bd9Sstevel@tonic-gate 
54257c478bd9Sstevel@tonic-gate 	session_id = STRUCT_FGET(generate_key, gk_session);
54267c478bd9Sstevel@tonic-gate 
54277c478bd9Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
54287c478bd9Sstevel@tonic-gate 		goto release_minor;
54297c478bd9Sstevel@tonic-gate 	}
54307c478bd9Sstevel@tonic-gate 
5431894b2776Smcpowers 	bcopy(STRUCT_FADDR(generate_key, gk_mechanism), &mech.cm_type,
5432894b2776Smcpowers 	    sizeof (crypto_mech_type_t));
5433894b2776Smcpowers 
5434436935a1SVladimir Kotal 	if ((rv = kcf_get_hardware_provider(mech.cm_type, NULL,
54359b009fc1SValerie Bubb Fenwick 	    CRYPTO_MECH_INVALID, NULL, sp->sd_provider,
5436436935a1SVladimir Kotal 	    &real_provider, CRYPTO_FG_GENERATE)) != CRYPTO_SUCCESS) {
54377c478bd9Sstevel@tonic-gate 		goto release_minor;
54387c478bd9Sstevel@tonic-gate 	}
54397c478bd9Sstevel@tonic-gate 
5440894b2776Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
5441894b2776Smcpowers 	    STRUCT_FADDR(generate_key, gk_mechanism), &mech, mode, &error);
5442894b2776Smcpowers 
5443894b2776Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
5444894b2776Smcpowers 		allocated_by_crypto_module = B_TRUE;
54452d794da1Skrishna 		if (!copyin_mech(mode, sp,
54462d794da1Skrishna 		    STRUCT_FADDR(generate_key, gk_mechanism),
54472d794da1Skrishna 		    &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) {
5448894b2776Smcpowers 			goto release_minor;
5449894b2776Smcpowers 		}
5450894b2776Smcpowers 	} else {
5451894b2776Smcpowers 		if (rv != CRYPTO_SUCCESS)
5452894b2776Smcpowers 			goto release_minor;
54537c478bd9Sstevel@tonic-gate 	}
54547c478bd9Sstevel@tonic-gate 
54557c478bd9Sstevel@tonic-gate 	count = STRUCT_FGET(generate_key, gk_count);
54567c478bd9Sstevel@tonic-gate 	attributes = STRUCT_FGETP(generate_key, gk_attributes);
54572d794da1Skrishna 	if (!copyin_attributes(mode, sp, count, attributes, &k_attrs,
54582d794da1Skrishna 	    &k_attrs_size, NULL, &rv, &error, &key_rctl_bytes,
54592d794da1Skrishna 	    &key_rctl_chk, B_TRUE)) {
54607c478bd9Sstevel@tonic-gate 		goto release_minor;
54617c478bd9Sstevel@tonic-gate 	}
54627c478bd9Sstevel@tonic-gate 
54637c478bd9Sstevel@tonic-gate 	KCF_WRAP_KEY_OPS_PARAMS(&params, KCF_OP_KEY_GENERATE,
54647c478bd9Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, &mech, k_attrs, count,
54657c478bd9Sstevel@tonic-gate 	    &key_handle, NULL, 0, NULL, NULL, NULL, 0);
54667c478bd9Sstevel@tonic-gate 
54677c478bd9Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
54687c478bd9Sstevel@tonic-gate 
54697c478bd9Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS)
54707c478bd9Sstevel@tonic-gate 		STRUCT_FSET(generate_key, gk_handle, key_handle);
54717c478bd9Sstevel@tonic-gate 
54727c478bd9Sstevel@tonic-gate release_minor:
54732d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
54742d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk);
54757c478bd9Sstevel@tonic-gate 
54767c478bd9Sstevel@tonic-gate 	if (k_attrs != NULL)
54777c478bd9Sstevel@tonic-gate 		kmem_free(k_attrs, k_attrs_size);
54787c478bd9Sstevel@tonic-gate 
54797c478bd9Sstevel@tonic-gate 	if (error != 0)
54807c478bd9Sstevel@tonic-gate 		goto out;
54817c478bd9Sstevel@tonic-gate 
54827c478bd9Sstevel@tonic-gate 	STRUCT_FSET(generate_key, gk_return_value, rv);
54837c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(generate_key), arg,
54847c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(generate_key)) != 0) {
54857c478bd9Sstevel@tonic-gate 		if (rv == CRYPTO_SUCCESS) {
54867c478bd9Sstevel@tonic-gate 			KCF_WRAP_OBJECT_OPS_PARAMS(&params,
54877c478bd9Sstevel@tonic-gate 			    KCF_OP_OBJECT_DESTROY,
54887c478bd9Sstevel@tonic-gate 			    sp->sd_provider_session->ps_session, key_handle,
54897c478bd9Sstevel@tonic-gate 			    NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
54907c478bd9Sstevel@tonic-gate 
54917c478bd9Sstevel@tonic-gate 			(void) kcf_submit_request(real_provider, NULL,
54927c478bd9Sstevel@tonic-gate 			    NULL, &params, B_FALSE);
54937c478bd9Sstevel@tonic-gate 
54947c478bd9Sstevel@tonic-gate 			error = EFAULT;
54957c478bd9Sstevel@tonic-gate 		}
54967c478bd9Sstevel@tonic-gate 	}
54977c478bd9Sstevel@tonic-gate out:
54982d794da1Skrishna 	CRYPTO_SESSION_RELE(sp);
54997c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
5500894b2776Smcpowers 
5501894b2776Smcpowers 	if (real_provider != NULL) {
5502894b2776Smcpowers 		crypto_free_mech(real_provider,
5503894b2776Smcpowers 		    allocated_by_crypto_module, &mech);
5504894b2776Smcpowers 		KCF_PROV_REFRELE(real_provider);
5505894b2776Smcpowers 	}
55067c478bd9Sstevel@tonic-gate 	return (error);
55077c478bd9Sstevel@tonic-gate }
55087c478bd9Sstevel@tonic-gate 
5509034448feSmcpowers /* ARGSUSED */
5510034448feSmcpowers static int
nostore_generate_key(dev_t dev,caddr_t arg,int mode,int * rval)5511034448feSmcpowers nostore_generate_key(dev_t dev, caddr_t arg, int mode, int *rval)
5512034448feSmcpowers {
5513034448feSmcpowers 	STRUCT_DECL(crypto_nostore_generate_key, generate_key);
551495014fbbSDan OpenSolaris Anderson #ifdef _LP64
551595014fbbSDan OpenSolaris Anderson 	STRUCT_DECL(crypto_object_attribute, oa);
551695014fbbSDan OpenSolaris Anderson #else
5517034448feSmcpowers 	/* LINTED E_FUNC_SET_NOT_USED */
5518034448feSmcpowers 	STRUCT_DECL(crypto_object_attribute, oa);
551995014fbbSDan OpenSolaris Anderson #endif
5520034448feSmcpowers 	kcf_provider_desc_t *real_provider = NULL;
5521034448feSmcpowers 	kcf_req_params_t params;
5522034448feSmcpowers 	crypto_mechanism_t mech;
5523034448feSmcpowers 	crypto_object_attribute_t *k_in_attrs = NULL;
5524034448feSmcpowers 	crypto_object_attribute_t *k_out_attrs = NULL;
5525034448feSmcpowers 	crypto_session_id_t session_id;
5526034448feSmcpowers 	crypto_minor_t *cm;
5527034448feSmcpowers 	crypto_session_data_t *sp = NULL;
5528034448feSmcpowers 	caddr_t in_attributes;
5529034448feSmcpowers 	caddr_t out_attributes;
5530034448feSmcpowers 	size_t k_in_attrs_size;
5531034448feSmcpowers 	size_t k_out_attrs_size;
5532034448feSmcpowers 	size_t mech_rctl_bytes = 0;
55332d794da1Skrishna 	boolean_t mech_rctl_chk = B_FALSE;
5534034448feSmcpowers 	size_t in_key_rctl_bytes = 0, out_key_rctl_bytes = 0;
55352d794da1Skrishna 	boolean_t in_key_rctl_chk = B_FALSE;
55362d794da1Skrishna 	boolean_t out_key_rctl_chk = B_FALSE;
5537034448feSmcpowers 	uint_t in_count;
5538034448feSmcpowers 	uint_t out_count;
5539034448feSmcpowers 	int error = 0;
5540034448feSmcpowers 	int rv;
5541034448feSmcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
5542034448feSmcpowers 	caddr_t u_attrs = NULL;
5543034448feSmcpowers 
5544*c846684cSToomas Soome 	out_count = 0;
5545034448feSmcpowers 	STRUCT_INIT(generate_key, mode);
5546034448feSmcpowers 	STRUCT_INIT(oa, mode);
5547034448feSmcpowers 
5548034448feSmcpowers 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
5549034448feSmcpowers 		cmn_err(CE_WARN, "nostore_generate_key: failed holding minor");
5550034448feSmcpowers 		return (ENXIO);
5551034448feSmcpowers 	}
5552034448feSmcpowers 
5553034448feSmcpowers 	if (copyin(arg, STRUCT_BUF(generate_key),
5554034448feSmcpowers 	    STRUCT_SIZE(generate_key)) != 0) {
5555034448feSmcpowers 		crypto_release_minor(cm);
5556034448feSmcpowers 		return (EFAULT);
5557034448feSmcpowers 	}
5558034448feSmcpowers 
5559034448feSmcpowers 	session_id = STRUCT_FGET(generate_key, ngk_session);
5560034448feSmcpowers 
5561034448feSmcpowers 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
5562034448feSmcpowers 		goto release_minor;
5563034448feSmcpowers 	}
5564034448feSmcpowers 
5565034448feSmcpowers 	bcopy(STRUCT_FADDR(generate_key, ngk_mechanism), &mech.cm_type,
5566034448feSmcpowers 	    sizeof (crypto_mech_type_t));
5567034448feSmcpowers 
5568436935a1SVladimir Kotal 	if ((rv = kcf_get_hardware_provider(mech.cm_type, NULL,
55699b009fc1SValerie Bubb Fenwick 	    CRYPTO_MECH_INVALID, NULL, sp->sd_provider,
5570436935a1SVladimir Kotal 	    &real_provider, CRYPTO_FG_GENERATE)) != CRYPTO_SUCCESS) {
5571034448feSmcpowers 		goto release_minor;
5572034448feSmcpowers 	}
5573034448feSmcpowers 
5574034448feSmcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
5575034448feSmcpowers 	    STRUCT_FADDR(generate_key, ngk_mechanism), &mech, mode, &error);
5576034448feSmcpowers 
5577034448feSmcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
5578034448feSmcpowers 		allocated_by_crypto_module = B_TRUE;
55792d794da1Skrishna 		if (!copyin_mech(mode, sp, STRUCT_FADDR(generate_key,
55802d794da1Skrishna 		    ngk_mechanism), &mech, &mech_rctl_bytes,
55812d794da1Skrishna 		    &mech_rctl_chk, &rv, &error)) {
5582034448feSmcpowers 			goto release_minor;
5583034448feSmcpowers 		}
5584034448feSmcpowers 	} else {
5585034448feSmcpowers 		if (rv != CRYPTO_SUCCESS)
5586034448feSmcpowers 			goto release_minor;
5587034448feSmcpowers 	}
5588034448feSmcpowers 
5589034448feSmcpowers 	in_count = STRUCT_FGET(generate_key, ngk_in_count);
5590034448feSmcpowers 	in_attributes = STRUCT_FGETP(generate_key, ngk_in_attributes);
55912d794da1Skrishna 	if (!copyin_attributes(mode, sp, in_count, in_attributes, &k_in_attrs,
5592034448feSmcpowers 	    &k_in_attrs_size, NULL, &rv, &error, &in_key_rctl_bytes,
55932d794da1Skrishna 	    &in_key_rctl_chk, B_TRUE)) {
5594034448feSmcpowers 		goto release_minor;
5595034448feSmcpowers 	}
5596034448feSmcpowers 
5597034448feSmcpowers 	out_count = STRUCT_FGET(generate_key, ngk_out_count);
5598034448feSmcpowers 	out_attributes = STRUCT_FGETP(generate_key, ngk_out_attributes);
55992d794da1Skrishna 	if (!copyin_attributes(mode, sp, out_count, out_attributes,
56002d794da1Skrishna 	    &k_out_attrs,
5601034448feSmcpowers 	    &k_out_attrs_size, &u_attrs, &rv, &error, &out_key_rctl_bytes,
56022d794da1Skrishna 	    &out_key_rctl_chk, B_FALSE)) {
5603034448feSmcpowers 		goto release_minor;
5604034448feSmcpowers 	}
5605034448feSmcpowers 
5606034448feSmcpowers 	KCF_WRAP_NOSTORE_KEY_OPS_PARAMS(&params, KCF_OP_KEY_GENERATE,
5607034448feSmcpowers 	    sp->sd_provider_session->ps_session, &mech, k_in_attrs, in_count,
5608034448feSmcpowers 	    NULL, 0, NULL, k_out_attrs, out_count, NULL, 0);
5609034448feSmcpowers 
5610034448feSmcpowers 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
5611034448feSmcpowers 
5612034448feSmcpowers 	if (rv == CRYPTO_SUCCESS) {
5613034448feSmcpowers 		error = copyout_attributes(mode, out_attributes,
5614034448feSmcpowers 		    out_count, k_out_attrs, u_attrs);
5615034448feSmcpowers 	}
5616034448feSmcpowers release_minor:
56172d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
56182d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, in_key_rctl_bytes, in_key_rctl_chk);
56192d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, out_key_rctl_bytes,
56202d794da1Skrishna 	    out_key_rctl_chk);
5621034448feSmcpowers 
5622034448feSmcpowers 	if (k_in_attrs != NULL)
5623034448feSmcpowers 		kmem_free(k_in_attrs, k_in_attrs_size);
5624034448feSmcpowers 	if (k_out_attrs != NULL) {
5625034448feSmcpowers 		bzero(k_out_attrs, k_out_attrs_size);
5626034448feSmcpowers 		kmem_free(k_out_attrs, k_out_attrs_size);
5627034448feSmcpowers 	}
5628034448feSmcpowers 
5629034448feSmcpowers 	if (u_attrs != NULL)
5630034448feSmcpowers 		kmem_free(u_attrs, out_count * STRUCT_SIZE(oa));
5631034448feSmcpowers 
5632034448feSmcpowers 	if (error != 0)
5633034448feSmcpowers 		goto out;
5634034448feSmcpowers 
5635034448feSmcpowers 	STRUCT_FSET(generate_key, ngk_return_value, rv);
5636034448feSmcpowers 	if (copyout(STRUCT_BUF(generate_key), arg,
5637034448feSmcpowers 	    STRUCT_SIZE(generate_key)) != 0) {
5638034448feSmcpowers 		error = EFAULT;
5639034448feSmcpowers 	}
5640034448feSmcpowers out:
56412d794da1Skrishna 	CRYPTO_SESSION_RELE(sp);
5642034448feSmcpowers 	crypto_release_minor(cm);
5643034448feSmcpowers 
5644034448feSmcpowers 	if (real_provider != NULL) {
5645034448feSmcpowers 		crypto_free_mech(real_provider,
5646034448feSmcpowers 		    allocated_by_crypto_module, &mech);
5647034448feSmcpowers 		KCF_PROV_REFRELE(real_provider);
5648034448feSmcpowers 	}
5649034448feSmcpowers 	return (error);
5650034448feSmcpowers }
5651034448feSmcpowers 
56527c478bd9Sstevel@tonic-gate /* ARGSUSED */
56537c478bd9Sstevel@tonic-gate static int
object_generate_key_pair(dev_t dev,caddr_t arg,int mode,int * rval)56547c478bd9Sstevel@tonic-gate object_generate_key_pair(dev_t dev, caddr_t arg, int mode, int *rval)
56557c478bd9Sstevel@tonic-gate {
56567c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_generate_key_pair, generate_key_pair);
5657894b2776Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
56587c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
56597c478bd9Sstevel@tonic-gate 	crypto_mechanism_t mech;
56607c478bd9Sstevel@tonic-gate 	crypto_object_attribute_t *k_pub_attrs = NULL;
56617c478bd9Sstevel@tonic-gate 	crypto_object_attribute_t *k_pri_attrs = NULL;
56627c478bd9Sstevel@tonic-gate 	crypto_session_id_t session_id;
56637c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
56647c478bd9Sstevel@tonic-gate 	crypto_session_data_t *sp = NULL;
56657c478bd9Sstevel@tonic-gate 	crypto_object_id_t pub_handle;
56667c478bd9Sstevel@tonic-gate 	crypto_object_id_t pri_handle;
56677c478bd9Sstevel@tonic-gate 	caddr_t pri_attributes;
56687c478bd9Sstevel@tonic-gate 	caddr_t pub_attributes;
56697c478bd9Sstevel@tonic-gate 	size_t k_pub_attrs_size, k_pri_attrs_size;
56707c478bd9Sstevel@tonic-gate 	size_t mech_rctl_bytes = 0;
56712d794da1Skrishna 	boolean_t mech_rctl_chk = B_FALSE;
56727c478bd9Sstevel@tonic-gate 	size_t pub_rctl_bytes = 0;
56732d794da1Skrishna 	boolean_t pub_rctl_chk = B_FALSE;
56747c478bd9Sstevel@tonic-gate 	size_t pri_rctl_bytes = 0;
56752d794da1Skrishna 	boolean_t pri_rctl_chk = B_FALSE;
56767c478bd9Sstevel@tonic-gate 	uint_t pub_count;
56777c478bd9Sstevel@tonic-gate 	uint_t pri_count;
56787c478bd9Sstevel@tonic-gate 	int error = 0;
56797c478bd9Sstevel@tonic-gate 	int rv;
5680894b2776Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
56817c478bd9Sstevel@tonic-gate 
56827c478bd9Sstevel@tonic-gate 	STRUCT_INIT(generate_key_pair, mode);
56837c478bd9Sstevel@tonic-gate 
56847c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
56857c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
56867c478bd9Sstevel@tonic-gate 		    "object_generate_key_pair: failed holding minor");
56877c478bd9Sstevel@tonic-gate 		return (ENXIO);
56887c478bd9Sstevel@tonic-gate 	}
56897c478bd9Sstevel@tonic-gate 
56907c478bd9Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(generate_key_pair),
56917c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(generate_key_pair)) != 0) {
56927c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
56937c478bd9Sstevel@tonic-gate 		return (EFAULT);
56947c478bd9Sstevel@tonic-gate 	}
56957c478bd9Sstevel@tonic-gate 
56967c478bd9Sstevel@tonic-gate 	session_id = STRUCT_FGET(generate_key_pair, kp_session);
56977c478bd9Sstevel@tonic-gate 
56987c478bd9Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
56997c478bd9Sstevel@tonic-gate 		goto release_minor;
57007c478bd9Sstevel@tonic-gate 	}
57017c478bd9Sstevel@tonic-gate 
5702894b2776Smcpowers 	bcopy(STRUCT_FADDR(generate_key_pair, kp_mechanism), &mech.cm_type,
5703894b2776Smcpowers 	    sizeof (crypto_mech_type_t));
5704894b2776Smcpowers 
5705436935a1SVladimir Kotal 	if ((rv = kcf_get_hardware_provider(mech.cm_type, NULL,
57069b009fc1SValerie Bubb Fenwick 	    CRYPTO_MECH_INVALID, NULL, sp->sd_provider,
5707436935a1SVladimir Kotal 	    &real_provider, CRYPTO_FG_GENERATE_KEY_PAIR)) != CRYPTO_SUCCESS) {
57087c478bd9Sstevel@tonic-gate 		goto release_minor;
57097c478bd9Sstevel@tonic-gate 	}
57107c478bd9Sstevel@tonic-gate 
5711894b2776Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
5712894b2776Smcpowers 	    STRUCT_FADDR(generate_key_pair, kp_mechanism), &mech, mode, &error);
5713894b2776Smcpowers 
5714894b2776Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
5715894b2776Smcpowers 		allocated_by_crypto_module = B_TRUE;
57162d794da1Skrishna 		if (!copyin_mech(mode, sp, STRUCT_FADDR(generate_key_pair,
57172d794da1Skrishna 		    kp_mechanism), &mech, &mech_rctl_bytes,
57182d794da1Skrishna 		    &mech_rctl_chk, &rv, &error)) {
5719894b2776Smcpowers 			goto release_minor;
5720894b2776Smcpowers 		}
5721894b2776Smcpowers 	} else {
5722894b2776Smcpowers 		if (rv != CRYPTO_SUCCESS)
5723894b2776Smcpowers 			goto release_minor;
57247c478bd9Sstevel@tonic-gate 	}
57257c478bd9Sstevel@tonic-gate 
57267c478bd9Sstevel@tonic-gate 	pub_count = STRUCT_FGET(generate_key_pair, kp_public_count);
57277c478bd9Sstevel@tonic-gate 	pri_count = STRUCT_FGET(generate_key_pair, kp_private_count);
57287c478bd9Sstevel@tonic-gate 
57297c478bd9Sstevel@tonic-gate 	pub_attributes = STRUCT_FGETP(generate_key_pair, kp_public_attributes);
57302d794da1Skrishna 	if (!copyin_attributes(mode, sp, pub_count, pub_attributes,
57312d794da1Skrishna 	    &k_pub_attrs, &k_pub_attrs_size, NULL, &rv, &error, &pub_rctl_bytes,
57322d794da1Skrishna 	    &pub_rctl_chk, B_TRUE)) {
57337c478bd9Sstevel@tonic-gate 		goto release_minor;
57347c478bd9Sstevel@tonic-gate 	}
57357c478bd9Sstevel@tonic-gate 
57367c478bd9Sstevel@tonic-gate 	pri_attributes = STRUCT_FGETP(generate_key_pair, kp_private_attributes);
57372d794da1Skrishna 	if (!copyin_attributes(mode, sp, pri_count, pri_attributes,
57382d794da1Skrishna 	    &k_pri_attrs, &k_pri_attrs_size, NULL, &rv, &error,
57392d794da1Skrishna 	    &pri_rctl_bytes, &pri_rctl_chk, B_TRUE)) {
57407c478bd9Sstevel@tonic-gate 		goto release_minor;
57417c478bd9Sstevel@tonic-gate 	}
57427c478bd9Sstevel@tonic-gate 
57437c478bd9Sstevel@tonic-gate 	KCF_WRAP_KEY_OPS_PARAMS(&params, KCF_OP_KEY_GENERATE_PAIR,
57447c478bd9Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, &mech, k_pub_attrs,
57457c478bd9Sstevel@tonic-gate 	    pub_count, &pub_handle, k_pri_attrs, pri_count, &pri_handle,
57467c478bd9Sstevel@tonic-gate 	    NULL, NULL, 0);
57477c478bd9Sstevel@tonic-gate 
57487c478bd9Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
57497c478bd9Sstevel@tonic-gate 
57507c478bd9Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
57517c478bd9Sstevel@tonic-gate 		STRUCT_FSET(generate_key_pair, kp_public_handle, pub_handle);
57527c478bd9Sstevel@tonic-gate 		STRUCT_FSET(generate_key_pair, kp_private_handle, pri_handle);
57537c478bd9Sstevel@tonic-gate 	}
57547c478bd9Sstevel@tonic-gate 
57557c478bd9Sstevel@tonic-gate release_minor:
57562d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
57572d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, pub_rctl_bytes, pub_rctl_chk);
57582d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, pri_rctl_bytes, pri_rctl_chk);
57597c478bd9Sstevel@tonic-gate 
57607c478bd9Sstevel@tonic-gate 	if (k_pub_attrs != NULL)
57617c478bd9Sstevel@tonic-gate 		kmem_free(k_pub_attrs, k_pub_attrs_size);
57627c478bd9Sstevel@tonic-gate 
57637c478bd9Sstevel@tonic-gate 	if (k_pri_attrs != NULL)
57647c478bd9Sstevel@tonic-gate 		kmem_free(k_pri_attrs, k_pri_attrs_size);
57657c478bd9Sstevel@tonic-gate 
57667c478bd9Sstevel@tonic-gate 	if (error != 0)
57677c478bd9Sstevel@tonic-gate 		goto out;
57687c478bd9Sstevel@tonic-gate 
57697c478bd9Sstevel@tonic-gate 	STRUCT_FSET(generate_key_pair, kp_return_value, rv);
57707c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(generate_key_pair), arg,
57717c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(generate_key_pair)) != 0) {
57727c478bd9Sstevel@tonic-gate 		if (rv == CRYPTO_SUCCESS) {
57737c478bd9Sstevel@tonic-gate 			KCF_WRAP_OBJECT_OPS_PARAMS(&params,
57747c478bd9Sstevel@tonic-gate 			    KCF_OP_OBJECT_DESTROY,
57757c478bd9Sstevel@tonic-gate 			    sp->sd_provider_session->ps_session, pub_handle,
57767c478bd9Sstevel@tonic-gate 			    NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
57777c478bd9Sstevel@tonic-gate 
57787c478bd9Sstevel@tonic-gate 			(void) kcf_submit_request(real_provider, NULL,
57797c478bd9Sstevel@tonic-gate 			    NULL, &params, B_FALSE);
57807c478bd9Sstevel@tonic-gate 
57817c478bd9Sstevel@tonic-gate 			KCF_WRAP_OBJECT_OPS_PARAMS(&params,
57827c478bd9Sstevel@tonic-gate 			    KCF_OP_OBJECT_DESTROY,
57837c478bd9Sstevel@tonic-gate 			    sp->sd_provider_session->ps_session, pri_handle,
57847c478bd9Sstevel@tonic-gate 			    NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
57857c478bd9Sstevel@tonic-gate 
57867c478bd9Sstevel@tonic-gate 			(void) kcf_submit_request(real_provider, NULL,
57877c478bd9Sstevel@tonic-gate 			    NULL, &params, B_FALSE);
57887c478bd9Sstevel@tonic-gate 
57897c478bd9Sstevel@tonic-gate 			error = EFAULT;
57907c478bd9Sstevel@tonic-gate 		}
57917c478bd9Sstevel@tonic-gate 	}
57927c478bd9Sstevel@tonic-gate out:
57932d794da1Skrishna 	CRYPTO_SESSION_RELE(sp);
57947c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
5795894b2776Smcpowers 
5796894b2776Smcpowers 	if (real_provider != NULL) {
5797894b2776Smcpowers 		crypto_free_mech(real_provider,
5798894b2776Smcpowers 		    allocated_by_crypto_module, &mech);
5799894b2776Smcpowers 		KCF_PROV_REFRELE(real_provider);
5800894b2776Smcpowers 	}
58017c478bd9Sstevel@tonic-gate 	return (error);
58027c478bd9Sstevel@tonic-gate }
58037c478bd9Sstevel@tonic-gate 
5804034448feSmcpowers /* ARGSUSED */
5805034448feSmcpowers static int
nostore_generate_key_pair(dev_t dev,caddr_t arg,int mode,int * rval)5806034448feSmcpowers nostore_generate_key_pair(dev_t dev, caddr_t arg, int mode, int *rval)
5807034448feSmcpowers {
5808034448feSmcpowers 	STRUCT_DECL(crypto_nostore_generate_key_pair, generate_key_pair);
580995014fbbSDan OpenSolaris Anderson #ifdef _LP64
581095014fbbSDan OpenSolaris Anderson 	STRUCT_DECL(crypto_object_attribute, oa);
581195014fbbSDan OpenSolaris Anderson #else
5812034448feSmcpowers 	/* LINTED E_FUNC_SET_NOT_USED */
5813034448feSmcpowers 	STRUCT_DECL(crypto_object_attribute, oa);
581495014fbbSDan OpenSolaris Anderson #endif
5815034448feSmcpowers 	kcf_provider_desc_t *real_provider = NULL;
5816034448feSmcpowers 	kcf_req_params_t params;
5817034448feSmcpowers 	crypto_mechanism_t mech;
5818034448feSmcpowers 	crypto_object_attribute_t *k_in_pub_attrs = NULL;
5819034448feSmcpowers 	crypto_object_attribute_t *k_in_pri_attrs = NULL;
5820034448feSmcpowers 	crypto_object_attribute_t *k_out_pub_attrs = NULL;
5821034448feSmcpowers 	crypto_object_attribute_t *k_out_pri_attrs = NULL;
5822034448feSmcpowers 	crypto_session_id_t session_id;
5823034448feSmcpowers 	crypto_minor_t *cm;
5824034448feSmcpowers 	crypto_session_data_t *sp = NULL;
5825034448feSmcpowers 	caddr_t in_pri_attributes;
5826034448feSmcpowers 	caddr_t in_pub_attributes;
5827034448feSmcpowers 	caddr_t out_pri_attributes;
5828034448feSmcpowers 	caddr_t out_pub_attributes;
5829034448feSmcpowers 	size_t k_in_pub_attrs_size, k_in_pri_attrs_size;
5830034448feSmcpowers 	size_t k_out_pub_attrs_size, k_out_pri_attrs_size;
5831034448feSmcpowers 	size_t mech_rctl_bytes = 0;
58322d794da1Skrishna 	boolean_t mech_rctl_chk = B_FALSE;
5833034448feSmcpowers 	size_t in_pub_rctl_bytes = 0;
58342d794da1Skrishna 	boolean_t in_pub_rctl_chk = B_FALSE;
5835034448feSmcpowers 	size_t in_pri_rctl_bytes = 0;
58362d794da1Skrishna 	boolean_t in_pri_rctl_chk = B_FALSE;
5837034448feSmcpowers 	size_t out_pub_rctl_bytes = 0;
58382d794da1Skrishna 	boolean_t out_pub_rctl_chk = B_FALSE;
5839034448feSmcpowers 	size_t out_pri_rctl_bytes = 0;
58402d794da1Skrishna 	boolean_t out_pri_rctl_chk = B_FALSE;
5841034448feSmcpowers 	uint_t in_pub_count;
5842034448feSmcpowers 	uint_t in_pri_count;
5843034448feSmcpowers 	uint_t out_pub_count;
5844034448feSmcpowers 	uint_t out_pri_count;
5845034448feSmcpowers 	int error = 0;
5846034448feSmcpowers 	int rv;
5847034448feSmcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
5848034448feSmcpowers 	caddr_t u_pub_attrs = NULL;
5849034448feSmcpowers 	caddr_t u_pri_attrs = NULL;
5850034448feSmcpowers 
5851*c846684cSToomas Soome 	out_pub_count = 0;
5852*c846684cSToomas Soome 	out_pri_count = 0;
5853034448feSmcpowers 	STRUCT_INIT(generate_key_pair, mode);
5854034448feSmcpowers 	STRUCT_INIT(oa, mode);
5855034448feSmcpowers 
5856034448feSmcpowers 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
5857034448feSmcpowers 		cmn_err(CE_WARN,
5858034448feSmcpowers 		    "nostore_generate_key_pair: failed holding minor");
5859034448feSmcpowers 		return (ENXIO);
5860034448feSmcpowers 	}
5861034448feSmcpowers 
5862034448feSmcpowers 	if (copyin(arg, STRUCT_BUF(generate_key_pair),
5863034448feSmcpowers 	    STRUCT_SIZE(generate_key_pair)) != 0) {
5864034448feSmcpowers 		crypto_release_minor(cm);
5865034448feSmcpowers 		return (EFAULT);
5866034448feSmcpowers 	}
5867034448feSmcpowers 
5868034448feSmcpowers 	session_id = STRUCT_FGET(generate_key_pair, nkp_session);
5869034448feSmcpowers 
5870034448feSmcpowers 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
5871034448feSmcpowers 		goto release_minor;
5872034448feSmcpowers 	}
5873034448feSmcpowers 
5874034448feSmcpowers 	bcopy(STRUCT_FADDR(generate_key_pair, nkp_mechanism), &mech.cm_type,
5875034448feSmcpowers 	    sizeof (crypto_mech_type_t));
5876034448feSmcpowers 
5877436935a1SVladimir Kotal 	if ((rv = kcf_get_hardware_provider(mech.cm_type, NULL,
58789b009fc1SValerie Bubb Fenwick 	    CRYPTO_MECH_INVALID, NULL, sp->sd_provider,
5879436935a1SVladimir Kotal 	    &real_provider, CRYPTO_FG_GENERATE_KEY_PAIR)) != CRYPTO_SUCCESS) {
5880034448feSmcpowers 		goto release_minor;
5881034448feSmcpowers 	}
5882034448feSmcpowers 
5883034448feSmcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
5884034448feSmcpowers 	    STRUCT_FADDR(generate_key_pair, nkp_mechanism), &mech, mode,
5885034448feSmcpowers 	    &error);
5886034448feSmcpowers 
5887034448feSmcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
5888034448feSmcpowers 		allocated_by_crypto_module = B_TRUE;
58892d794da1Skrishna 		if (!copyin_mech(mode, sp, STRUCT_FADDR(generate_key_pair,
58902d794da1Skrishna 		    nkp_mechanism), &mech, &mech_rctl_bytes,
58912d794da1Skrishna 		    &mech_rctl_chk, &rv, &error)) {
5892034448feSmcpowers 			goto release_minor;
5893034448feSmcpowers 		}
5894034448feSmcpowers 	} else {
5895034448feSmcpowers 		if (rv != CRYPTO_SUCCESS)
5896034448feSmcpowers 			goto release_minor;
5897034448feSmcpowers 	}
5898034448feSmcpowers 
5899034448feSmcpowers 	in_pub_count = STRUCT_FGET(generate_key_pair, nkp_in_public_count);
5900034448feSmcpowers 	in_pri_count = STRUCT_FGET(generate_key_pair, nkp_in_private_count);
5901034448feSmcpowers 
5902034448feSmcpowers 	in_pub_attributes = STRUCT_FGETP(generate_key_pair,
5903034448feSmcpowers 	    nkp_in_public_attributes);
59042d794da1Skrishna 	if (!copyin_attributes(mode, sp, in_pub_count, in_pub_attributes,
5905034448feSmcpowers 	    &k_in_pub_attrs, &k_in_pub_attrs_size, NULL, &rv, &error,
59062d794da1Skrishna 	    &in_pub_rctl_bytes, &in_pub_rctl_chk, B_TRUE)) {
5907034448feSmcpowers 		goto release_minor;
5908034448feSmcpowers 	}
5909034448feSmcpowers 
5910034448feSmcpowers 	in_pri_attributes = STRUCT_FGETP(generate_key_pair,
5911034448feSmcpowers 	    nkp_in_private_attributes);
59122d794da1Skrishna 	if (!copyin_attributes(mode, sp, in_pri_count, in_pri_attributes,
5913034448feSmcpowers 	    &k_in_pri_attrs, &k_in_pri_attrs_size, NULL, &rv, &error,
59142d794da1Skrishna 	    &in_pri_rctl_bytes, &in_pri_rctl_chk, B_TRUE)) {
5915034448feSmcpowers 		goto release_minor;
5916034448feSmcpowers 	}
5917034448feSmcpowers 
5918034448feSmcpowers 	out_pub_count = STRUCT_FGET(generate_key_pair, nkp_out_public_count);
5919034448feSmcpowers 	out_pri_count = STRUCT_FGET(generate_key_pair, nkp_out_private_count);
5920034448feSmcpowers 
5921034448feSmcpowers 	out_pub_attributes = STRUCT_FGETP(generate_key_pair,
5922034448feSmcpowers 	    nkp_out_public_attributes);
59232d794da1Skrishna 	if (!copyin_attributes(mode, sp, out_pub_count, out_pub_attributes,
5924034448feSmcpowers 	    &k_out_pub_attrs, &k_out_pub_attrs_size, &u_pub_attrs, &rv, &error,
59252d794da1Skrishna 	    &out_pub_rctl_bytes, &out_pub_rctl_chk, B_FALSE)) {
5926034448feSmcpowers 		goto release_minor;
5927034448feSmcpowers 	}
5928034448feSmcpowers 
5929034448feSmcpowers 	out_pri_attributes = STRUCT_FGETP(generate_key_pair,
5930034448feSmcpowers 	    nkp_out_private_attributes);
59312d794da1Skrishna 	if (!copyin_attributes(mode, sp, out_pri_count, out_pri_attributes,
5932034448feSmcpowers 	    &k_out_pri_attrs, &k_out_pri_attrs_size, &u_pri_attrs, &rv, &error,
59332d794da1Skrishna 	    &out_pri_rctl_bytes, &out_pri_rctl_chk, B_FALSE)) {
5934034448feSmcpowers 		goto release_minor;
5935034448feSmcpowers 	}
5936034448feSmcpowers 
5937034448feSmcpowers 	KCF_WRAP_NOSTORE_KEY_OPS_PARAMS(&params, KCF_OP_KEY_GENERATE_PAIR,
5938034448feSmcpowers 	    sp->sd_provider_session->ps_session, &mech, k_in_pub_attrs,
5939034448feSmcpowers 	    in_pub_count, k_in_pri_attrs, in_pri_count, NULL, k_out_pub_attrs,
5940034448feSmcpowers 	    out_pub_count, k_out_pri_attrs, out_pri_count);
5941034448feSmcpowers 
5942034448feSmcpowers 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
5943034448feSmcpowers 
5944034448feSmcpowers 	if (rv == CRYPTO_SUCCESS) {
5945034448feSmcpowers 		error = copyout_attributes(mode, out_pub_attributes,
5946034448feSmcpowers 		    out_pub_count, k_out_pub_attrs, u_pub_attrs);
5947034448feSmcpowers 		if (error != CRYPTO_SUCCESS)
5948034448feSmcpowers 			goto release_minor;
5949034448feSmcpowers 		error = copyout_attributes(mode, out_pri_attributes,
5950034448feSmcpowers 		    out_pri_count, k_out_pri_attrs, u_pri_attrs);
5951034448feSmcpowers 	}
5952034448feSmcpowers 
5953034448feSmcpowers release_minor:
59542d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
59552d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, in_pub_rctl_bytes, in_pub_rctl_chk);
59562d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, in_pri_rctl_bytes, in_pri_rctl_chk);
59572d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, out_pub_rctl_bytes,
59582d794da1Skrishna 	    out_pub_rctl_chk);
59592d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, out_pri_rctl_bytes,
59602d794da1Skrishna 	    out_pri_rctl_chk);
5961034448feSmcpowers 
5962034448feSmcpowers 	if (k_in_pub_attrs != NULL)
5963034448feSmcpowers 		kmem_free(k_in_pub_attrs, k_in_pub_attrs_size);
5964034448feSmcpowers 
5965034448feSmcpowers 	if (k_in_pri_attrs != NULL)
5966034448feSmcpowers 		kmem_free(k_in_pri_attrs, k_in_pri_attrs_size);
5967034448feSmcpowers 
5968034448feSmcpowers 	if (k_out_pub_attrs != NULL)
5969034448feSmcpowers 		kmem_free(k_out_pub_attrs, k_out_pub_attrs_size);
5970034448feSmcpowers 
5971034448feSmcpowers 	if (k_out_pri_attrs != NULL) {
5972034448feSmcpowers 		bzero(k_out_pri_attrs, k_out_pri_attrs_size);
5973034448feSmcpowers 		kmem_free(k_out_pri_attrs, k_out_pri_attrs_size);
5974034448feSmcpowers 	}
5975034448feSmcpowers 
5976034448feSmcpowers 	if (u_pub_attrs != NULL)
5977034448feSmcpowers 		kmem_free(u_pub_attrs, out_pub_count * STRUCT_SIZE(oa));
5978034448feSmcpowers 
5979034448feSmcpowers 	if (u_pri_attrs != NULL)
5980034448feSmcpowers 		kmem_free(u_pri_attrs, out_pri_count * STRUCT_SIZE(oa));
5981034448feSmcpowers 
5982034448feSmcpowers 	if (error != 0)
5983034448feSmcpowers 		goto out;
5984034448feSmcpowers 
5985034448feSmcpowers 	STRUCT_FSET(generate_key_pair, nkp_return_value, rv);
5986034448feSmcpowers 	if (copyout(STRUCT_BUF(generate_key_pair), arg,
5987034448feSmcpowers 	    STRUCT_SIZE(generate_key_pair)) != 0) {
5988034448feSmcpowers 		error = EFAULT;
5989034448feSmcpowers 	}
5990034448feSmcpowers out:
59912d794da1Skrishna 	CRYPTO_SESSION_RELE(sp);
5992034448feSmcpowers 	crypto_release_minor(cm);
5993034448feSmcpowers 
5994034448feSmcpowers 	if (real_provider != NULL) {
5995034448feSmcpowers 		crypto_free_mech(real_provider,
5996034448feSmcpowers 		    allocated_by_crypto_module, &mech);
5997034448feSmcpowers 		KCF_PROV_REFRELE(real_provider);
5998034448feSmcpowers 	}
5999034448feSmcpowers 	return (error);
6000034448feSmcpowers }
6001034448feSmcpowers 
60027c478bd9Sstevel@tonic-gate /* ARGSUSED */
60037c478bd9Sstevel@tonic-gate static int
object_wrap_key(dev_t dev,caddr_t arg,int mode,int * rval)60047c478bd9Sstevel@tonic-gate object_wrap_key(dev_t dev, caddr_t arg, int mode, int *rval)
60057c478bd9Sstevel@tonic-gate {
60067c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_wrap_key, wrap_key);
6007894b2776Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
60087c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
60097c478bd9Sstevel@tonic-gate 	crypto_mechanism_t mech;
60107c478bd9Sstevel@tonic-gate 	crypto_key_t key;
60117c478bd9Sstevel@tonic-gate 	crypto_session_id_t session_id;
60127c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
60132d794da1Skrishna 	crypto_session_data_t *sp = NULL;
60147c478bd9Sstevel@tonic-gate 	crypto_object_id_t handle;
60157c478bd9Sstevel@tonic-gate 	size_t mech_rctl_bytes = 0, key_rctl_bytes = 0;
60162d794da1Skrishna 	boolean_t mech_rctl_chk = B_FALSE;
60172d794da1Skrishna 	boolean_t key_rctl_chk = B_FALSE;
60187c478bd9Sstevel@tonic-gate 	size_t wrapped_key_rctl_bytes = 0;
60192d794da1Skrishna 	boolean_t wrapped_key_rctl_chk = B_FALSE;
60207c478bd9Sstevel@tonic-gate 	size_t wrapped_key_len, new_wrapped_key_len;
60217c478bd9Sstevel@tonic-gate 	uchar_t *wrapped_key = NULL;
60227c478bd9Sstevel@tonic-gate 	char *wrapped_key_buffer;
60237c478bd9Sstevel@tonic-gate 	int error = 0;
60247c478bd9Sstevel@tonic-gate 	int rv;
6025894b2776Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
60267c478bd9Sstevel@tonic-gate 
60277c478bd9Sstevel@tonic-gate 	STRUCT_INIT(wrap_key, mode);
60287c478bd9Sstevel@tonic-gate 
60297c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
60307c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_wrap_key: failed holding minor");
60317c478bd9Sstevel@tonic-gate 		return (ENXIO);
60327c478bd9Sstevel@tonic-gate 	}
60337c478bd9Sstevel@tonic-gate 
60347c478bd9Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(wrap_key), STRUCT_SIZE(wrap_key)) != 0) {
60357c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
60367c478bd9Sstevel@tonic-gate 		return (EFAULT);
60377c478bd9Sstevel@tonic-gate 	}
60387c478bd9Sstevel@tonic-gate 
60397c478bd9Sstevel@tonic-gate 	bzero(&key, sizeof (crypto_key_t));
60407c478bd9Sstevel@tonic-gate 
60417c478bd9Sstevel@tonic-gate 	session_id = STRUCT_FGET(wrap_key, wk_session);
60427c478bd9Sstevel@tonic-gate 
60437c478bd9Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
60442d794da1Skrishna 		goto out;
60457c478bd9Sstevel@tonic-gate 	}
60467c478bd9Sstevel@tonic-gate 
6047894b2776Smcpowers 	bcopy(STRUCT_FADDR(wrap_key, wk_mechanism), &mech.cm_type,
6048894b2776Smcpowers 	    sizeof (crypto_mech_type_t));
6049894b2776Smcpowers 
6050436935a1SVladimir Kotal 	/* We need the key length for provider selection so copy it in now. */
6051436935a1SVladimir Kotal 	if (!copyin_key(mode, sp, STRUCT_FADDR(wrap_key, wk_wrapping_key), &key,
6052436935a1SVladimir Kotal 	    &key_rctl_bytes, &key_rctl_chk, &rv, &error)) {
6053436935a1SVladimir Kotal 		goto out;
6054436935a1SVladimir Kotal 	}
6055436935a1SVladimir Kotal 
6056436935a1SVladimir Kotal 	wrapped_key_len = STRUCT_FGET(wrap_key, wk_wrapped_key_len);
6057436935a1SVladimir Kotal 
6058436935a1SVladimir Kotal 	if ((rv = kcf_get_hardware_provider(mech.cm_type, &key,
60599b009fc1SValerie Bubb Fenwick 	    CRYPTO_MECH_INVALID, NULL,  sp->sd_provider,
6060436935a1SVladimir Kotal 	    &real_provider, CRYPTO_FG_WRAP)) != CRYPTO_SUCCESS) {
60617c478bd9Sstevel@tonic-gate 		goto out;
60627c478bd9Sstevel@tonic-gate 	}
60637c478bd9Sstevel@tonic-gate 
6064894b2776Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
6065894b2776Smcpowers 	    STRUCT_FADDR(wrap_key, wk_mechanism), &mech, mode, &error);
6066894b2776Smcpowers 
6067894b2776Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
6068894b2776Smcpowers 		allocated_by_crypto_module = B_TRUE;
60692d794da1Skrishna 		if (!copyin_mech(mode, sp, STRUCT_FADDR(wrap_key, wk_mechanism),
60702d794da1Skrishna 		    &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) {
6071894b2776Smcpowers 			goto out;
6072894b2776Smcpowers 		}
6073894b2776Smcpowers 	} else {
6074894b2776Smcpowers 		if (rv != CRYPTO_SUCCESS)
6075894b2776Smcpowers 			goto out;
60767c478bd9Sstevel@tonic-gate 	}
60777c478bd9Sstevel@tonic-gate 
60787c478bd9Sstevel@tonic-gate 	/*
60797c478bd9Sstevel@tonic-gate 	 * Don't allocate output buffer unless both buffer pointer and
60807c478bd9Sstevel@tonic-gate 	 * buffer length are not NULL or 0 (length).
60817c478bd9Sstevel@tonic-gate 	 */
60827c478bd9Sstevel@tonic-gate 	wrapped_key_buffer = STRUCT_FGETP(wrap_key, wk_wrapped_key);
60837c478bd9Sstevel@tonic-gate 	if (wrapped_key_buffer == NULL || wrapped_key_len == 0) {
60847c478bd9Sstevel@tonic-gate 		wrapped_key_len = 0;
60857c478bd9Sstevel@tonic-gate 	}
60867c478bd9Sstevel@tonic-gate 
60877c478bd9Sstevel@tonic-gate 	if (wrapped_key_len > crypto_max_buffer_len) {
60887c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "object_wrap_key: buffer greater than %ld "
60897c478bd9Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
60907c478bd9Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
60917c478bd9Sstevel@tonic-gate 		goto out;
60927c478bd9Sstevel@tonic-gate 	}
60937c478bd9Sstevel@tonic-gate 
60942d794da1Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, wrapped_key_len,
60952d794da1Skrishna 	    wrapped_key_rctl_chk)) != CRYPTO_SUCCESS) {
60967c478bd9Sstevel@tonic-gate 		goto out;
60977c478bd9Sstevel@tonic-gate 	}
60987c478bd9Sstevel@tonic-gate 
60997c478bd9Sstevel@tonic-gate 	/* new_wrapped_key_len can be modified by the provider */
61007c478bd9Sstevel@tonic-gate 	wrapped_key_rctl_bytes = new_wrapped_key_len = wrapped_key_len;
61017c478bd9Sstevel@tonic-gate 	wrapped_key = kmem_alloc(wrapped_key_len, KM_SLEEP);
61027c478bd9Sstevel@tonic-gate 
61037c478bd9Sstevel@tonic-gate 	handle = STRUCT_FGET(wrap_key, wk_object_handle);
61047c478bd9Sstevel@tonic-gate 	KCF_WRAP_KEY_OPS_PARAMS(&params, KCF_OP_KEY_WRAP,
61057c478bd9Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, &mech, NULL, 0, &handle,
61067c478bd9Sstevel@tonic-gate 	    NULL, 0, NULL, &key, wrapped_key, &new_wrapped_key_len);
61077c478bd9Sstevel@tonic-gate 
61087c478bd9Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
61097c478bd9Sstevel@tonic-gate 
61107c478bd9Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS) {
61117c478bd9Sstevel@tonic-gate 		if (wrapped_key_len != 0 && copyout(wrapped_key,
61127c478bd9Sstevel@tonic-gate 		    wrapped_key_buffer, new_wrapped_key_len) != 0) {
61137c478bd9Sstevel@tonic-gate 			error = EFAULT;
61147c478bd9Sstevel@tonic-gate 		}
611595014fbbSDan OpenSolaris Anderson 		STRUCT_FSET(wrap_key, wk_wrapped_key_len,
611695014fbbSDan OpenSolaris Anderson 		    (ulong_t)new_wrapped_key_len);
61177c478bd9Sstevel@tonic-gate 	}
61187c478bd9Sstevel@tonic-gate 
61197c478bd9Sstevel@tonic-gate 	if (rv == CRYPTO_BUFFER_TOO_SMALL) {
61207c478bd9Sstevel@tonic-gate 		/*
61217c478bd9Sstevel@tonic-gate 		 * The providers return CRYPTO_BUFFER_TOO_SMALL even for case 1
61227c478bd9Sstevel@tonic-gate 		 * of section 11.2 of the pkcs11 spec. We catch it here and
61237c478bd9Sstevel@tonic-gate 		 * provide the correct pkcs11 return value.
61247c478bd9Sstevel@tonic-gate 		 */
61257c478bd9Sstevel@tonic-gate 		if (STRUCT_FGETP(wrap_key, wk_wrapped_key) == NULL)
61267c478bd9Sstevel@tonic-gate 			rv = CRYPTO_SUCCESS;
612795014fbbSDan OpenSolaris Anderson 		STRUCT_FSET(wrap_key, wk_wrapped_key_len,
612895014fbbSDan OpenSolaris Anderson 		    (ulong_t)new_wrapped_key_len);
61297c478bd9Sstevel@tonic-gate 	}
61302d794da1Skrishna 
61317c478bd9Sstevel@tonic-gate out:
61322d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
61332d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk);
61342d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, wrapped_key_rctl_bytes,
61352d794da1Skrishna 	    wrapped_key_rctl_chk);
61367c478bd9Sstevel@tonic-gate 	CRYPTO_SESSION_RELE(sp);
61377c478bd9Sstevel@tonic-gate 
61387c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
61397c478bd9Sstevel@tonic-gate 
6140894b2776Smcpowers 	if (real_provider != NULL) {
6141894b2776Smcpowers 		crypto_free_mech(real_provider,
6142894b2776Smcpowers 		    allocated_by_crypto_module, &mech);
6143894b2776Smcpowers 		KCF_PROV_REFRELE(real_provider);
6144894b2776Smcpowers 	}
6145894b2776Smcpowers 
61467c478bd9Sstevel@tonic-gate 	if (wrapped_key != NULL)
61477c478bd9Sstevel@tonic-gate 		kmem_free(wrapped_key, wrapped_key_len);
61487c478bd9Sstevel@tonic-gate 
61497c478bd9Sstevel@tonic-gate 	free_crypto_key(&key);
61507c478bd9Sstevel@tonic-gate 
61517c478bd9Sstevel@tonic-gate 	if (error != 0)
61527c478bd9Sstevel@tonic-gate 		return (error);
61537c478bd9Sstevel@tonic-gate 
61547c478bd9Sstevel@tonic-gate 	STRUCT_FSET(wrap_key, wk_return_value, rv);
61557c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(wrap_key), arg, STRUCT_SIZE(wrap_key)) != 0) {
61567c478bd9Sstevel@tonic-gate 		return (EFAULT);
61577c478bd9Sstevel@tonic-gate 	}
61587c478bd9Sstevel@tonic-gate 	return (0);
61597c478bd9Sstevel@tonic-gate }
61607c478bd9Sstevel@tonic-gate 
61617c478bd9Sstevel@tonic-gate /* ARGSUSED */
61627c478bd9Sstevel@tonic-gate static int
object_unwrap_key(dev_t dev,caddr_t arg,int mode,int * rval)61637c478bd9Sstevel@tonic-gate object_unwrap_key(dev_t dev, caddr_t arg, int mode, int *rval)
61647c478bd9Sstevel@tonic-gate {
61657c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_object_unwrap_key, unwrap_key);
6166894b2776Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
61677c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
61687c478bd9Sstevel@tonic-gate 	crypto_mechanism_t mech;
61697c478bd9Sstevel@tonic-gate 	crypto_key_t unwrapping_key;
61707c478bd9Sstevel@tonic-gate 	crypto_session_id_t session_id;
61717c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
61727c478bd9Sstevel@tonic-gate 	crypto_session_data_t *sp = NULL;
61737c478bd9Sstevel@tonic-gate 	crypto_object_id_t handle;
61747c478bd9Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
61757c478bd9Sstevel@tonic-gate 	size_t k_attrs_size;
61767c478bd9Sstevel@tonic-gate 	size_t mech_rctl_bytes = 0, unwrapping_key_rctl_bytes = 0;
61772d794da1Skrishna 	boolean_t mech_rctl_chk = B_FALSE;
61782d794da1Skrishna 	boolean_t unwrapping_key_rctl_chk = B_FALSE;
61797c478bd9Sstevel@tonic-gate 	size_t wrapped_key_rctl_bytes = 0, k_attrs_rctl_bytes = 0;
61802d794da1Skrishna 	boolean_t wrapped_key_rctl_chk = B_FALSE;
61812d794da1Skrishna 	boolean_t k_attrs_rctl_chk = B_FALSE;
61827c478bd9Sstevel@tonic-gate 	size_t wrapped_key_len;
61837c478bd9Sstevel@tonic-gate 	uchar_t *wrapped_key = NULL;
61847c478bd9Sstevel@tonic-gate 	int error = 0;
61857c478bd9Sstevel@tonic-gate 	int rv;
61867c478bd9Sstevel@tonic-gate 	uint_t count;
61877c478bd9Sstevel@tonic-gate 	caddr_t uk_attributes;
6188894b2776Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
61897c478bd9Sstevel@tonic-gate 
61907c478bd9Sstevel@tonic-gate 	STRUCT_INIT(unwrap_key, mode);
61917c478bd9Sstevel@tonic-gate 
61927c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
61937c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_unwrap_key: failed holding minor");
61947c478bd9Sstevel@tonic-gate 		return (ENXIO);
61957c478bd9Sstevel@tonic-gate 	}
61967c478bd9Sstevel@tonic-gate 
61977c478bd9Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(unwrap_key), STRUCT_SIZE(unwrap_key)) != 0) {
61987c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
61997c478bd9Sstevel@tonic-gate 		return (EFAULT);
62007c478bd9Sstevel@tonic-gate 	}
62017c478bd9Sstevel@tonic-gate 
62027c478bd9Sstevel@tonic-gate 	bzero(&unwrapping_key, sizeof (unwrapping_key));
62037c478bd9Sstevel@tonic-gate 
62047c478bd9Sstevel@tonic-gate 	session_id = STRUCT_FGET(unwrap_key, uk_session);
62057c478bd9Sstevel@tonic-gate 
62067c478bd9Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
62077c478bd9Sstevel@tonic-gate 		goto release_minor;
62087c478bd9Sstevel@tonic-gate 	}
62097c478bd9Sstevel@tonic-gate 
6210894b2776Smcpowers 	bcopy(STRUCT_FADDR(unwrap_key, uk_mechanism), &mech.cm_type,
6211894b2776Smcpowers 	    sizeof (crypto_mech_type_t));
6212894b2776Smcpowers 
6213436935a1SVladimir Kotal 	/* We need the key length for provider selection so copy it in now. */
6214436935a1SVladimir Kotal 	if (!copyin_key(mode, sp, STRUCT_FADDR(unwrap_key, uk_unwrapping_key),
6215436935a1SVladimir Kotal 	    &unwrapping_key, &unwrapping_key_rctl_bytes,
6216436935a1SVladimir Kotal 	    &unwrapping_key_rctl_chk, &rv, &error)) {
6217436935a1SVladimir Kotal 		goto release_minor;
6218436935a1SVladimir Kotal 	}
6219436935a1SVladimir Kotal 
6220436935a1SVladimir Kotal 	if ((rv = kcf_get_hardware_provider(mech.cm_type, &unwrapping_key,
62219b009fc1SValerie Bubb Fenwick 	    CRYPTO_MECH_INVALID, NULL, sp->sd_provider,
6222436935a1SVladimir Kotal 	    &real_provider, CRYPTO_FG_UNWRAP)) != CRYPTO_SUCCESS) {
62237c478bd9Sstevel@tonic-gate 		goto release_minor;
62247c478bd9Sstevel@tonic-gate 	}
62257c478bd9Sstevel@tonic-gate 
6226894b2776Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
6227894b2776Smcpowers 	    STRUCT_FADDR(unwrap_key, uk_mechanism), &mech, mode, &error);
6228894b2776Smcpowers 
6229894b2776Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
6230894b2776Smcpowers 		allocated_by_crypto_module = B_TRUE;
62312d794da1Skrishna 		if (!copyin_mech(mode, sp,
62322d794da1Skrishna 		    STRUCT_FADDR(unwrap_key, uk_mechanism),
62332d794da1Skrishna 		    &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) {
6234894b2776Smcpowers 			goto release_minor;
6235894b2776Smcpowers 		}
6236894b2776Smcpowers 	} else {
6237894b2776Smcpowers 		if (rv != CRYPTO_SUCCESS)
6238894b2776Smcpowers 			goto release_minor;
62397c478bd9Sstevel@tonic-gate 	}
62407c478bd9Sstevel@tonic-gate 
62417c478bd9Sstevel@tonic-gate 	count = STRUCT_FGET(unwrap_key, uk_count);
62427c478bd9Sstevel@tonic-gate 	uk_attributes = STRUCT_FGETP(unwrap_key, uk_attributes);
62432d794da1Skrishna 	if (!copyin_attributes(mode, sp, count, uk_attributes, &k_attrs,
62442d794da1Skrishna 	    &k_attrs_size, NULL, &rv, &error, &k_attrs_rctl_bytes,
62452d794da1Skrishna 	    &k_attrs_rctl_chk, B_TRUE)) {
62467c478bd9Sstevel@tonic-gate 		goto release_minor;
62477c478bd9Sstevel@tonic-gate 	}
62487c478bd9Sstevel@tonic-gate 
62497c478bd9Sstevel@tonic-gate 	wrapped_key_len = STRUCT_FGET(unwrap_key, uk_wrapped_key_len);
62507c478bd9Sstevel@tonic-gate 	if (wrapped_key_len > crypto_max_buffer_len) {
62517c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "object_unwrap_key: buffer greater than %ld "
62527c478bd9Sstevel@tonic-gate 		    "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
62537c478bd9Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
62547c478bd9Sstevel@tonic-gate 		goto release_minor;
62557c478bd9Sstevel@tonic-gate 	}
62567c478bd9Sstevel@tonic-gate 
62572d794da1Skrishna 	if ((rv = CRYPTO_BUFFER_CHECK(sp, wrapped_key_len,
62582d794da1Skrishna 	    wrapped_key_rctl_chk)) != CRYPTO_SUCCESS) {
62597c478bd9Sstevel@tonic-gate 		goto release_minor;
62607c478bd9Sstevel@tonic-gate 	}
62617c478bd9Sstevel@tonic-gate 	wrapped_key_rctl_bytes = wrapped_key_len;
62627c478bd9Sstevel@tonic-gate 	wrapped_key = kmem_alloc(wrapped_key_len, KM_SLEEP);
62637c478bd9Sstevel@tonic-gate 
62647c478bd9Sstevel@tonic-gate 	if (wrapped_key_len != 0 && copyin(STRUCT_FGETP(unwrap_key,
62657c478bd9Sstevel@tonic-gate 	    uk_wrapped_key), wrapped_key, wrapped_key_len) != 0) {
62667c478bd9Sstevel@tonic-gate 		error = EFAULT;
62677c478bd9Sstevel@tonic-gate 		goto release_minor;
62687c478bd9Sstevel@tonic-gate 	}
62697c478bd9Sstevel@tonic-gate 
62707c478bd9Sstevel@tonic-gate 	/* wrapped_key_len is not modified by the unwrap operation */
62717c478bd9Sstevel@tonic-gate 	KCF_WRAP_KEY_OPS_PARAMS(&params, KCF_OP_KEY_UNWRAP,
62727c478bd9Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, &mech, k_attrs, count, &handle,
62737c478bd9Sstevel@tonic-gate 	    NULL, 0, NULL, &unwrapping_key, wrapped_key, &wrapped_key_len);
62747c478bd9Sstevel@tonic-gate 
62757c478bd9Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
62767c478bd9Sstevel@tonic-gate 
62777c478bd9Sstevel@tonic-gate 	if (rv == CRYPTO_SUCCESS)
62787c478bd9Sstevel@tonic-gate 		STRUCT_FSET(unwrap_key, uk_object_handle, handle);
62797c478bd9Sstevel@tonic-gate 
62807c478bd9Sstevel@tonic-gate release_minor:
62812d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
62822d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, unwrapping_key_rctl_bytes,
62832d794da1Skrishna 	    unwrapping_key_rctl_chk);
62842d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, wrapped_key_rctl_bytes,
62852d794da1Skrishna 	    wrapped_key_rctl_chk);
62862d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, k_attrs_rctl_bytes,
62872d794da1Skrishna 	    k_attrs_rctl_chk);
62887c478bd9Sstevel@tonic-gate 
62897c478bd9Sstevel@tonic-gate 	if (k_attrs != NULL)
62907c478bd9Sstevel@tonic-gate 		kmem_free(k_attrs, k_attrs_size);
62917c478bd9Sstevel@tonic-gate 
62927c478bd9Sstevel@tonic-gate 	if (wrapped_key != NULL)
62937c478bd9Sstevel@tonic-gate 		kmem_free(wrapped_key, wrapped_key_len);
62947c478bd9Sstevel@tonic-gate 
62957c478bd9Sstevel@tonic-gate 	free_crypto_key(&unwrapping_key);
62967c478bd9Sstevel@tonic-gate 
62977c478bd9Sstevel@tonic-gate 	if (error != 0)
62987c478bd9Sstevel@tonic-gate 		goto out;
62997c478bd9Sstevel@tonic-gate 
63007c478bd9Sstevel@tonic-gate 	STRUCT_FSET(unwrap_key, uk_return_value, rv);
63017c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(unwrap_key), arg,
63027c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(unwrap_key)) != 0) {
63037c478bd9Sstevel@tonic-gate 		if (rv == CRYPTO_SUCCESS) {
63047c478bd9Sstevel@tonic-gate 			KCF_WRAP_OBJECT_OPS_PARAMS(&params,
63057c478bd9Sstevel@tonic-gate 			    KCF_OP_OBJECT_DESTROY,
63067c478bd9Sstevel@tonic-gate 			    sp->sd_provider_session->ps_session, handle,
63077c478bd9Sstevel@tonic-gate 			    NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
63087c478bd9Sstevel@tonic-gate 
63097c478bd9Sstevel@tonic-gate 			(void) kcf_submit_request(real_provider, NULL,
63107c478bd9Sstevel@tonic-gate 			    NULL, &params, B_FALSE);
63117c478bd9Sstevel@tonic-gate 
63127c478bd9Sstevel@tonic-gate 			error = EFAULT;
63137c478bd9Sstevel@tonic-gate 		}
63147c478bd9Sstevel@tonic-gate 	}
63157c478bd9Sstevel@tonic-gate out:
63162d794da1Skrishna 	CRYPTO_SESSION_RELE(sp);
63177c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
6318894b2776Smcpowers 
6319894b2776Smcpowers 	if (real_provider != NULL) {
6320894b2776Smcpowers 		crypto_free_mech(real_provider,
6321894b2776Smcpowers 		    allocated_by_crypto_module, &mech);
6322894b2776Smcpowers 		KCF_PROV_REFRELE(real_provider);
6323894b2776Smcpowers 	}
6324894b2776Smcpowers 
63257c478bd9Sstevel@tonic-gate 	return (error);
63267c478bd9Sstevel@tonic-gate }
63277c478bd9Sstevel@tonic-gate 
63287c478bd9Sstevel@tonic-gate /* ARGSUSED */
63297c478bd9Sstevel@tonic-gate static int
object_derive_key(dev_t dev,caddr_t arg,int mode,int * rval)63307c478bd9Sstevel@tonic-gate object_derive_key(dev_t dev, caddr_t arg, int mode, int *rval)
63317c478bd9Sstevel@tonic-gate {
63327c478bd9Sstevel@tonic-gate 	STRUCT_DECL(crypto_derive_key, derive_key);
6333894b2776Smcpowers 	kcf_provider_desc_t *real_provider = NULL;
63347c478bd9Sstevel@tonic-gate 	kcf_req_params_t params;
63357c478bd9Sstevel@tonic-gate 	crypto_object_attribute_t *k_attrs = NULL;
63367c478bd9Sstevel@tonic-gate 	crypto_mechanism_t mech;
63377c478bd9Sstevel@tonic-gate 	crypto_key_t base_key;
63387c478bd9Sstevel@tonic-gate 	crypto_session_id_t session_id;
63397c478bd9Sstevel@tonic-gate 	crypto_minor_t *cm;
63407c478bd9Sstevel@tonic-gate 	crypto_session_data_t *sp = NULL;
63417c478bd9Sstevel@tonic-gate 	crypto_object_id_t handle;
63427c478bd9Sstevel@tonic-gate 	size_t k_attrs_size;
63437c478bd9Sstevel@tonic-gate 	size_t key_rctl_bytes = 0, mech_rctl_bytes = 0;
63442d794da1Skrishna 	boolean_t mech_rctl_chk = B_FALSE;
63452d794da1Skrishna 	boolean_t key_rctl_chk = B_FALSE;
63467c478bd9Sstevel@tonic-gate 	size_t attributes_rctl_bytes = 0;
63472d794da1Skrishna 	boolean_t attributes_rctl_chk = B_FALSE;
63487c478bd9Sstevel@tonic-gate 	caddr_t attributes;
63497c478bd9Sstevel@tonic-gate 	uint_t count;
63507c478bd9Sstevel@tonic-gate 	int error = 0;
63517c478bd9Sstevel@tonic-gate 	int rv;
6352894b2776Smcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
6353894b2776Smcpowers 	boolean_t please_destroy_object = B_FALSE;
63547c478bd9Sstevel@tonic-gate 
63557c478bd9Sstevel@tonic-gate 	STRUCT_INIT(derive_key, mode);
63567c478bd9Sstevel@tonic-gate 
63577c478bd9Sstevel@tonic-gate 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
63587c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "object_derive_key: failed holding minor");
63597c478bd9Sstevel@tonic-gate 		return (ENXIO);
63607c478bd9Sstevel@tonic-gate 	}
63617c478bd9Sstevel@tonic-gate 
63627c478bd9Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(derive_key), STRUCT_SIZE(derive_key)) != 0) {
63637c478bd9Sstevel@tonic-gate 		crypto_release_minor(cm);
63647c478bd9Sstevel@tonic-gate 		return (EFAULT);
63657c478bd9Sstevel@tonic-gate 	}
63667c478bd9Sstevel@tonic-gate 
63677c478bd9Sstevel@tonic-gate 	bzero(&base_key, sizeof (base_key));
63687c478bd9Sstevel@tonic-gate 
63697c478bd9Sstevel@tonic-gate 	session_id = STRUCT_FGET(derive_key, dk_session);
63707c478bd9Sstevel@tonic-gate 
63717c478bd9Sstevel@tonic-gate 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
63727c478bd9Sstevel@tonic-gate 		goto release_minor;
63737c478bd9Sstevel@tonic-gate 	}
63747c478bd9Sstevel@tonic-gate 
6375894b2776Smcpowers 	bcopy(STRUCT_FADDR(derive_key, dk_mechanism), &mech.cm_type,
6376894b2776Smcpowers 	    sizeof (crypto_mech_type_t));
6377894b2776Smcpowers 
6378436935a1SVladimir Kotal 	/* We need the key length for provider selection so copy it in now. */
6379436935a1SVladimir Kotal 	if (!copyin_key(mode, sp, STRUCT_FADDR(derive_key, dk_base_key),
6380436935a1SVladimir Kotal 	    &base_key, &key_rctl_bytes, &key_rctl_chk, &rv, &error)) {
6381436935a1SVladimir Kotal 		goto release_minor;
6382436935a1SVladimir Kotal 	}
6383436935a1SVladimir Kotal 
6384436935a1SVladimir Kotal 	if ((rv = kcf_get_hardware_provider(mech.cm_type, &base_key,
63859b009fc1SValerie Bubb Fenwick 	    CRYPTO_MECH_INVALID, NULL, sp->sd_provider,
6386436935a1SVladimir Kotal 	    &real_provider, CRYPTO_FG_DERIVE)) != CRYPTO_SUCCESS) {
63877c478bd9Sstevel@tonic-gate 		goto release_minor;
63887c478bd9Sstevel@tonic-gate 	}
63897c478bd9Sstevel@tonic-gate 
6390894b2776Smcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
6391894b2776Smcpowers 	    STRUCT_FADDR(derive_key, dk_mechanism), &mech, mode, &error);
6392894b2776Smcpowers 
6393894b2776Smcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
6394894b2776Smcpowers 		allocated_by_crypto_module = B_TRUE;
63952d794da1Skrishna 		if (!copyin_mech(mode, sp,
63962d794da1Skrishna 		    STRUCT_FADDR(derive_key, dk_mechanism),
63972d794da1Skrishna 		    &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) {
6398894b2776Smcpowers 			goto release_minor;
6399894b2776Smcpowers 		}
6400894b2776Smcpowers 	} else {
6401894b2776Smcpowers 		if (rv != CRYPTO_SUCCESS)
6402894b2776Smcpowers 			goto release_minor;
64037c478bd9Sstevel@tonic-gate 	}
64047c478bd9Sstevel@tonic-gate 
64057c478bd9Sstevel@tonic-gate 	count = STRUCT_FGET(derive_key, dk_count);
64067c478bd9Sstevel@tonic-gate 
64077c478bd9Sstevel@tonic-gate 	attributes = STRUCT_FGETP(derive_key, dk_attributes);
64082d794da1Skrishna 	if (!copyin_attributes(mode, sp, count, attributes, &k_attrs,
6409c1a9a9c3Skrishna 	    &k_attrs_size, NULL, &rv, &error,
64102d794da1Skrishna 	    &attributes_rctl_bytes, &attributes_rctl_chk, B_TRUE)) {
64117c478bd9Sstevel@tonic-gate 		goto release_minor;
64127c478bd9Sstevel@tonic-gate 	}
64137c478bd9Sstevel@tonic-gate 
64147c478bd9Sstevel@tonic-gate 	KCF_WRAP_KEY_OPS_PARAMS(&params, KCF_OP_KEY_DERIVE,
64157c478bd9Sstevel@tonic-gate 	    sp->sd_provider_session->ps_session, &mech, k_attrs, count,
64167c478bd9Sstevel@tonic-gate 	    &handle, NULL, 0, NULL, &base_key, NULL, NULL);
64177c478bd9Sstevel@tonic-gate 
64187c478bd9Sstevel@tonic-gate 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
64197c478bd9Sstevel@tonic-gate 
6420894b2776Smcpowers 	if (rv == CRYPTO_SUCCESS) {
64217c478bd9Sstevel@tonic-gate 		STRUCT_FSET(derive_key, dk_object_handle, handle);
64227c478bd9Sstevel@tonic-gate 
6423894b2776Smcpowers 		rv = crypto_provider_copyout_mech_param(real_provider,
6424894b2776Smcpowers 		    &mech, STRUCT_FADDR(derive_key, dk_mechanism),
6425894b2776Smcpowers 		    mode, &error);
6426894b2776Smcpowers 
6427894b2776Smcpowers 		if (rv == CRYPTO_NOT_SUPPORTED) {
6428894b2776Smcpowers 			rv = CRYPTO_SUCCESS;
6429894b2776Smcpowers 			goto release_minor;
6430894b2776Smcpowers 		}
6431894b2776Smcpowers 
6432894b2776Smcpowers 		if (rv != CRYPTO_SUCCESS)
6433894b2776Smcpowers 			please_destroy_object = B_TRUE;
6434894b2776Smcpowers 	}
6435894b2776Smcpowers 
64367c478bd9Sstevel@tonic-gate release_minor:
64372d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
64382d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk);
64392d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, attributes_rctl_bytes,
64402d794da1Skrishna 	    attributes_rctl_chk);
64417c478bd9Sstevel@tonic-gate 
64427c478bd9Sstevel@tonic-gate 	if (k_attrs != NULL)
64437c478bd9Sstevel@tonic-gate 		kmem_free(k_attrs, k_attrs_size);
64447c478bd9Sstevel@tonic-gate 
64457c478bd9Sstevel@tonic-gate 	free_crypto_key(&base_key);
64467c478bd9Sstevel@tonic-gate 
64477c478bd9Sstevel@tonic-gate 	if (error != 0)
64487c478bd9Sstevel@tonic-gate 		goto out;
64497c478bd9Sstevel@tonic-gate 
64507c478bd9Sstevel@tonic-gate 	STRUCT_FSET(derive_key, dk_return_value, rv);
64517c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(derive_key), arg,
64527c478bd9Sstevel@tonic-gate 	    STRUCT_SIZE(derive_key)) != 0) {
64537c478bd9Sstevel@tonic-gate 		if (rv == CRYPTO_SUCCESS) {
6454894b2776Smcpowers 			please_destroy_object = B_TRUE;
64557c478bd9Sstevel@tonic-gate 			error = EFAULT;
64567c478bd9Sstevel@tonic-gate 		}
64577c478bd9Sstevel@tonic-gate 	}
64587c478bd9Sstevel@tonic-gate out:
6459894b2776Smcpowers 	if (please_destroy_object) {
6460894b2776Smcpowers 		KCF_WRAP_OBJECT_OPS_PARAMS(&params, KCF_OP_OBJECT_DESTROY,
6461894b2776Smcpowers 		    sp->sd_provider_session->ps_session, handle,
6462894b2776Smcpowers 		    NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
6463894b2776Smcpowers 
6464894b2776Smcpowers 		(void) kcf_submit_request(real_provider, NULL,
6465894b2776Smcpowers 		    NULL, &params, B_FALSE);
6466894b2776Smcpowers 	}
6467894b2776Smcpowers 
64682d794da1Skrishna 	CRYPTO_SESSION_RELE(sp);
64697c478bd9Sstevel@tonic-gate 	crypto_release_minor(cm);
6470894b2776Smcpowers 
6471894b2776Smcpowers 	if (real_provider != NULL) {
6472894b2776Smcpowers 		crypto_free_mech(real_provider,
6473894b2776Smcpowers 		    allocated_by_crypto_module, &mech);
6474894b2776Smcpowers 		KCF_PROV_REFRELE(real_provider);
6475894b2776Smcpowers 	}
64767c478bd9Sstevel@tonic-gate 	return (error);
64777c478bd9Sstevel@tonic-gate }
64787c478bd9Sstevel@tonic-gate 
6479034448feSmcpowers /* ARGSUSED */
6480034448feSmcpowers static int
nostore_derive_key(dev_t dev,caddr_t arg,int mode,int * rval)6481034448feSmcpowers nostore_derive_key(dev_t dev, caddr_t arg, int mode, int *rval)
6482034448feSmcpowers {
6483034448feSmcpowers 	STRUCT_DECL(crypto_nostore_derive_key, derive_key);
648495014fbbSDan OpenSolaris Anderson #ifdef _LP64
648595014fbbSDan OpenSolaris Anderson 	STRUCT_DECL(crypto_object_attribute, oa);
648695014fbbSDan OpenSolaris Anderson #else
6487034448feSmcpowers 	/* LINTED E_FUNC_SET_NOT_USED */
6488034448feSmcpowers 	STRUCT_DECL(crypto_object_attribute, oa);
648995014fbbSDan OpenSolaris Anderson #endif
6490034448feSmcpowers 	kcf_provider_desc_t *real_provider = NULL;
6491034448feSmcpowers 	kcf_req_params_t params;
6492034448feSmcpowers 	crypto_object_attribute_t *k_in_attrs = NULL;
6493034448feSmcpowers 	crypto_object_attribute_t *k_out_attrs = NULL;
6494034448feSmcpowers 	crypto_mechanism_t mech;
6495034448feSmcpowers 	crypto_key_t base_key;
6496034448feSmcpowers 	crypto_session_id_t session_id;
6497034448feSmcpowers 	crypto_minor_t *cm;
6498034448feSmcpowers 	crypto_session_data_t *sp = NULL;
6499034448feSmcpowers 	size_t k_in_attrs_size, k_out_attrs_size;
6500034448feSmcpowers 	size_t key_rctl_bytes = 0, mech_rctl_bytes = 0;
65012d794da1Skrishna 	boolean_t mech_rctl_chk = B_FALSE;
65022d794da1Skrishna 	boolean_t key_rctl_chk = B_FALSE;
6503034448feSmcpowers 	size_t in_attributes_rctl_bytes = 0;
6504034448feSmcpowers 	size_t out_attributes_rctl_bytes = 0;
65052d794da1Skrishna 	boolean_t in_attributes_rctl_chk = B_FALSE;
65062d794da1Skrishna 	boolean_t out_attributes_rctl_chk = B_FALSE;
6507034448feSmcpowers 	caddr_t in_attributes, out_attributes;
6508034448feSmcpowers 	uint_t in_count, out_count;
6509034448feSmcpowers 	int error = 0;
6510034448feSmcpowers 	int rv;
6511034448feSmcpowers 	boolean_t allocated_by_crypto_module = B_FALSE;
6512034448feSmcpowers 	caddr_t u_attrs = NULL;
6513034448feSmcpowers 
6514*c846684cSToomas Soome 	out_count = 0;
6515034448feSmcpowers 	STRUCT_INIT(derive_key, mode);
6516034448feSmcpowers 	STRUCT_INIT(oa, mode);
6517034448feSmcpowers 
6518034448feSmcpowers 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
6519034448feSmcpowers 		cmn_err(CE_WARN, "nostore_derive_key: failed holding minor");
6520034448feSmcpowers 		return (ENXIO);
6521034448feSmcpowers 	}
6522034448feSmcpowers 
6523034448feSmcpowers 	if (copyin(arg, STRUCT_BUF(derive_key), STRUCT_SIZE(derive_key)) != 0) {
6524034448feSmcpowers 		crypto_release_minor(cm);
6525034448feSmcpowers 		return (EFAULT);
6526034448feSmcpowers 	}
6527034448feSmcpowers 
6528034448feSmcpowers 	bzero(&base_key, sizeof (base_key));
6529034448feSmcpowers 
6530034448feSmcpowers 	session_id = STRUCT_FGET(derive_key, ndk_session);
6531034448feSmcpowers 
6532034448feSmcpowers 	if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
6533034448feSmcpowers 		goto release_minor;
6534034448feSmcpowers 	}
6535034448feSmcpowers 
6536034448feSmcpowers 	bcopy(STRUCT_FADDR(derive_key, ndk_mechanism), &mech.cm_type,
6537034448feSmcpowers 	    sizeof (crypto_mech_type_t));
6538034448feSmcpowers 
6539436935a1SVladimir Kotal 	/* We need the key length for provider selection so copy it in now. */
6540436935a1SVladimir Kotal 	if (!copyin_key(mode, sp, STRUCT_FADDR(derive_key, ndk_base_key),
6541436935a1SVladimir Kotal 	    &base_key, &key_rctl_bytes, &key_rctl_chk, &rv, &error)) {
6542436935a1SVladimir Kotal 		goto release_minor;
6543436935a1SVladimir Kotal 	}
6544436935a1SVladimir Kotal 
6545436935a1SVladimir Kotal 	if ((rv = kcf_get_hardware_provider(mech.cm_type, &base_key,
65469b009fc1SValerie Bubb Fenwick 	    CRYPTO_MECH_INVALID, NULL, sp->sd_provider,
6547436935a1SVladimir Kotal 	    &real_provider, CRYPTO_FG_DERIVE)) != CRYPTO_SUCCESS) {
6548034448feSmcpowers 		goto release_minor;
6549034448feSmcpowers 	}
6550034448feSmcpowers 
6551034448feSmcpowers 	rv = crypto_provider_copyin_mech_param(real_provider,
6552034448feSmcpowers 	    STRUCT_FADDR(derive_key, ndk_mechanism), &mech, mode, &error);
6553034448feSmcpowers 
6554034448feSmcpowers 	if (rv == CRYPTO_NOT_SUPPORTED) {
6555034448feSmcpowers 		allocated_by_crypto_module = B_TRUE;
65562d794da1Skrishna 		if (!copyin_mech(mode, sp,
65572d794da1Skrishna 		    STRUCT_FADDR(derive_key, ndk_mechanism),
65582d794da1Skrishna 		    &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) {
6559034448feSmcpowers 			goto release_minor;
6560034448feSmcpowers 		}
6561034448feSmcpowers 	} else {
6562034448feSmcpowers 		if (rv != CRYPTO_SUCCESS)
6563034448feSmcpowers 			goto release_minor;
6564034448feSmcpowers 	}
6565034448feSmcpowers 
6566034448feSmcpowers 	in_count = STRUCT_FGET(derive_key, ndk_in_count);
6567034448feSmcpowers 	out_count = STRUCT_FGET(derive_key, ndk_out_count);
6568034448feSmcpowers 
6569034448feSmcpowers 	in_attributes = STRUCT_FGETP(derive_key, ndk_in_attributes);
65702d794da1Skrishna 	if (!copyin_attributes(mode, sp, in_count, in_attributes, &k_in_attrs,
6571034448feSmcpowers 	    &k_in_attrs_size, NULL, &rv, &error, &in_attributes_rctl_bytes,
65722d794da1Skrishna 	    &in_attributes_rctl_chk, B_TRUE)) {
6573034448feSmcpowers 		goto release_minor;
6574034448feSmcpowers 	}
6575034448feSmcpowers 
6576034448feSmcpowers 	out_attributes = STRUCT_FGETP(derive_key, ndk_out_attributes);
65772d794da1Skrishna 	if (!copyin_attributes(mode, sp, out_count, out_attributes,
65782d794da1Skrishna 	    &k_out_attrs, &k_out_attrs_size, &u_attrs, &rv, &error,
65792d794da1Skrishna 	    &out_attributes_rctl_bytes,
65802d794da1Skrishna 	    &out_attributes_rctl_chk, B_FALSE)) {
6581034448feSmcpowers 		goto release_minor;
6582034448feSmcpowers 	}
6583034448feSmcpowers 
6584034448feSmcpowers 	KCF_WRAP_NOSTORE_KEY_OPS_PARAMS(&params, KCF_OP_KEY_DERIVE,
6585034448feSmcpowers 	    sp->sd_provider_session->ps_session, &mech, k_in_attrs, in_count,
6586034448feSmcpowers 	    NULL, 0, &base_key, k_out_attrs, out_count, NULL, 0);
6587034448feSmcpowers 
6588034448feSmcpowers 	rv = kcf_submit_request(real_provider, NULL, NULL, &params, B_FALSE);
6589034448feSmcpowers 
6590034448feSmcpowers 	if (rv == CRYPTO_SUCCESS) {
6591034448feSmcpowers 		rv = crypto_provider_copyout_mech_param(real_provider,
6592034448feSmcpowers 		    &mech, STRUCT_FADDR(derive_key, ndk_mechanism),
6593034448feSmcpowers 		    mode, &error);
6594034448feSmcpowers 
6595034448feSmcpowers 		if (rv == CRYPTO_NOT_SUPPORTED) {
6596034448feSmcpowers 			rv = CRYPTO_SUCCESS;
6597034448feSmcpowers 		}
6598034448feSmcpowers 		/* copyout the derived secret */
6599034448feSmcpowers 		if (copyout_attributes(mode, out_attributes, out_count,
6600034448feSmcpowers 		    k_out_attrs, u_attrs) != 0)
6601034448feSmcpowers 			error = EFAULT;
6602034448feSmcpowers 	}
6603034448feSmcpowers 
6604034448feSmcpowers release_minor:
66052d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
66062d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk);
66072d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, in_attributes_rctl_bytes,
66082d794da1Skrishna 	    in_attributes_rctl_chk);
66092d794da1Skrishna 	CRYPTO_DECREMENT_RCTL_SESSION(sp, out_attributes_rctl_bytes,
66102d794da1Skrishna 	    out_attributes_rctl_chk);
6611034448feSmcpowers 
6612034448feSmcpowers 	if (k_in_attrs != NULL)
6613034448feSmcpowers 		kmem_free(k_in_attrs, k_in_attrs_size);
6614034448feSmcpowers 	if (k_out_attrs != NULL) {
6615034448feSmcpowers 		bzero(k_out_attrs, k_out_attrs_size);
6616034448feSmcpowers 		kmem_free(k_out_attrs, k_out_attrs_size);
6617034448feSmcpowers 	}
6618034448feSmcpowers 
6619034448feSmcpowers 	if (u_attrs != NULL)
6620034448feSmcpowers 		kmem_free(u_attrs, out_count * STRUCT_SIZE(oa));
6621034448feSmcpowers 
6622034448feSmcpowers 	free_crypto_key(&base_key);
6623034448feSmcpowers 
6624034448feSmcpowers 	if (error != 0)
6625034448feSmcpowers 		goto out;
6626034448feSmcpowers 
6627034448feSmcpowers 	STRUCT_FSET(derive_key, ndk_return_value, rv);
6628034448feSmcpowers 	if (copyout(STRUCT_BUF(derive_key), arg,
6629034448feSmcpowers 	    STRUCT_SIZE(derive_key)) != 0) {
6630034448feSmcpowers 		error = EFAULT;
6631034448feSmcpowers 	}
6632034448feSmcpowers out:
66332d794da1Skrishna 	CRYPTO_SESSION_RELE(sp);
6634034448feSmcpowers 	crypto_release_minor(cm);
6635034448feSmcpowers 
6636034448feSmcpowers 	if (real_provider != NULL) {
6637034448feSmcpowers 		crypto_free_mech(real_provider,
6638034448feSmcpowers 		    allocated_by_crypto_module, &mech);
6639034448feSmcpowers 		KCF_PROV_REFRELE(real_provider);
6640034448feSmcpowers 	}
6641034448feSmcpowers 	return (error);
6642034448feSmcpowers }
6643034448feSmcpowers 
6644cd964fceSMatt Barden static int
get_provider_by_mech(dev_t dev,caddr_t arg,int mode,int * rval)6645cd964fceSMatt Barden get_provider_by_mech(dev_t dev, caddr_t arg, int mode, int *rval)
6646cd964fceSMatt Barden {
6647cd964fceSMatt Barden 	_NOTE(ARGUNUSED(mode, rval))
6648cd964fceSMatt Barden 	kcf_mech_entry_t *me;
6649cd964fceSMatt Barden 	kcf_provider_desc_t *pd;
6650cd964fceSMatt Barden 	crypto_key_t key;
6651cd964fceSMatt Barden 	crypto_by_mech_t mech;
6652cd964fceSMatt Barden 	crypto_provider_session_t *ps;
6653cd964fceSMatt Barden 	crypto_minor_t *cm;
6654cd964fceSMatt Barden 	int rv, error;
6655cd964fceSMatt Barden 
6656cd964fceSMatt Barden 	if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
6657cd964fceSMatt Barden 		cmn_err(CE_WARN, "get_provider_by_mech: failed holding minor");
6658cd964fceSMatt Barden 		return (ENXIO);
6659cd964fceSMatt Barden 	}
6660cd964fceSMatt Barden 
6661cd964fceSMatt Barden 	bzero(&key, sizeof (key));
6662cd964fceSMatt Barden 	key.ck_format = CRYPTO_KEY_RAW;
6663cd964fceSMatt Barden 
6664cd964fceSMatt Barden 	if (copyin(arg, &mech, sizeof (mech)) != 0) {
6665cd964fceSMatt Barden 		crypto_release_minor(cm);
6666cd964fceSMatt Barden 		return (EFAULT);
6667cd964fceSMatt Barden 	}
6668cd964fceSMatt Barden 
6669cd964fceSMatt Barden 	key.ck_length = mech.mech_keylen;
6670cd964fceSMatt Barden 	/* pd is returned held */
6671cd964fceSMatt Barden 	if ((pd = kcf_get_mech_provider(mech.mech_type, &key, &me, &error,
6672cd964fceSMatt Barden 	    NULL, mech.mech_fg, 0)) == NULL) {
6673cd964fceSMatt Barden 		rv = error;
6674cd964fceSMatt Barden 		goto release_minor;
6675cd964fceSMatt Barden 	}
6676cd964fceSMatt Barden 
6677cd964fceSMatt Barden 	/* don't want to allow direct access to software providers */
6678cd964fceSMatt Barden 	if (pd->pd_prov_type == CRYPTO_SW_PROVIDER) {
6679cd964fceSMatt Barden 		rv = CRYPTO_MECHANISM_INVALID;
6680cd964fceSMatt Barden 		KCF_PROV_REFRELE(pd);
6681cd964fceSMatt Barden 		cmn_err(CE_WARN, "software mech_type given");
6682cd964fceSMatt Barden 		goto release_minor;
6683cd964fceSMatt Barden 	}
6684cd964fceSMatt Barden 
6685cd964fceSMatt Barden 	mutex_enter(&cm->cm_lock);
6686cd964fceSMatt Barden 	if ((rv = crypto_create_provider_session(cm, pd, pd->pd_sid, &ps, NULL))
6687cd964fceSMatt Barden 	    == CRYPTO_SUCCESS)
6688cd964fceSMatt Barden 		rv = crypto_create_session_ptr(cm, pd, ps, &mech.session_id);
6689cd964fceSMatt Barden 
6690cd964fceSMatt Barden 	mutex_exit(&cm->cm_lock);
6691cd964fceSMatt Barden release_minor:
6692cd964fceSMatt Barden 	crypto_release_minor(cm);
6693cd964fceSMatt Barden 	mech.rv = rv;
6694cd964fceSMatt Barden 	if (copyout(&mech, arg, sizeof (mech)) != 0)
6695cd964fceSMatt Barden 		return (EFAULT);
6696cd964fceSMatt Barden 
6697cd964fceSMatt Barden 	return (rv);
6698cd964fceSMatt Barden }
6699cd964fceSMatt Barden 
67007c478bd9Sstevel@tonic-gate /* ARGSUSED */
67017c478bd9Sstevel@tonic-gate static int
crypto_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * c,int * rval)67027c478bd9Sstevel@tonic-gate crypto_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *c,
67037c478bd9Sstevel@tonic-gate     int *rval)
67047c478bd9Sstevel@tonic-gate {
67057c478bd9Sstevel@tonic-gate #define	ARG	((caddr_t)arg)
67067c478bd9Sstevel@tonic-gate 
67077c478bd9Sstevel@tonic-gate 	switch (cmd) {
67087c478bd9Sstevel@tonic-gate 	case CRYPTO_GET_FUNCTION_LIST:
67097c478bd9Sstevel@tonic-gate 		return (get_function_list(dev, ARG, mode, rval));
67107c478bd9Sstevel@tonic-gate 
67117c478bd9Sstevel@tonic-gate 	case CRYPTO_GET_MECHANISM_NUMBER:
67127c478bd9Sstevel@tonic-gate 		return (get_mechanism_number(dev, ARG, mode, rval));
67137c478bd9Sstevel@tonic-gate 
67147d82f0f8SDina K Nimeh 	case CRYPTO_GET_MECHANISM_LIST:
67157d82f0f8SDina K Nimeh 		return (get_mechanism_list(dev, ARG, mode, rval));
67167d82f0f8SDina K Nimeh 
67177d82f0f8SDina K Nimeh 	case CRYPTO_GET_ALL_MECHANISM_INFO:
67187d82f0f8SDina K Nimeh 		return (get_all_mechanism_info(dev, ARG, mode, rval));
67197d82f0f8SDina K Nimeh 
67207c478bd9Sstevel@tonic-gate 	case CRYPTO_GET_PROVIDER_LIST:
67217c478bd9Sstevel@tonic-gate 		return (get_provider_list(dev, ARG, mode, rval));
67227c478bd9Sstevel@tonic-gate 
6723cd964fceSMatt Barden 	case CRYPTO_GET_PROVIDER_BY_MECH:
6724cd964fceSMatt Barden 		return (get_provider_by_mech(dev, ARG, mode, rval));
6725cd964fceSMatt Barden 
67267c478bd9Sstevel@tonic-gate 	case CRYPTO_GET_PROVIDER_INFO:
67277c478bd9Sstevel@tonic-gate 		return (get_provider_info(dev, ARG, mode, rval));
67287c478bd9Sstevel@tonic-gate 
67297c478bd9Sstevel@tonic-gate 	case CRYPTO_GET_PROVIDER_MECHANISMS:
67307c478bd9Sstevel@tonic-gate 		return (get_provider_mechanisms(dev, ARG, mode, rval));
67317c478bd9Sstevel@tonic-gate 
67327c478bd9Sstevel@tonic-gate 	case CRYPTO_GET_PROVIDER_MECHANISM_INFO:
67337c478bd9Sstevel@tonic-gate 		return (get_provider_mechanism_info(dev, ARG, mode, rval));
67347c478bd9Sstevel@tonic-gate 
67357c478bd9Sstevel@tonic-gate 	case CRYPTO_OPEN_SESSION:
67367c478bd9Sstevel@tonic-gate 		return (open_session(dev, ARG, mode, rval));
67377c478bd9Sstevel@tonic-gate 
67387c478bd9Sstevel@tonic-gate 	case CRYPTO_CLOSE_SESSION:
67397c478bd9Sstevel@tonic-gate 		return (close_session(dev, ARG, mode, rval));
67407c478bd9Sstevel@tonic-gate 
67417c478bd9Sstevel@tonic-gate 	case CRYPTO_ENCRYPT_INIT:
67427c478bd9Sstevel@tonic-gate 		return (encrypt_init(dev, ARG, mode, rval));
67437c478bd9Sstevel@tonic-gate 
67447c478bd9Sstevel@tonic-gate 	case CRYPTO_DECRYPT_INIT:
67457c478bd9Sstevel@tonic-gate 		return (decrypt_init(dev, ARG, mode, rval));
67467c478bd9Sstevel@tonic-gate 
67477c478bd9Sstevel@tonic-gate 	case CRYPTO_ENCRYPT:
67487c478bd9Sstevel@tonic-gate 		return (encrypt(dev, ARG, mode, rval));
67497c478bd9Sstevel@tonic-gate 
67507c478bd9Sstevel@tonic-gate 	case CRYPTO_DECRYPT:
67517c478bd9Sstevel@tonic-gate 		return (decrypt(dev, ARG, mode, rval));
67527c478bd9Sstevel@tonic-gate 
67537c478bd9Sstevel@tonic-gate 	case CRYPTO_ENCRYPT_UPDATE:
67547c478bd9Sstevel@tonic-gate 		return (encrypt_update(dev, ARG, mode, rval));
67557c478bd9Sstevel@tonic-gate 
67567c478bd9Sstevel@tonic-gate 	case CRYPTO_DECRYPT_UPDATE:
67577c478bd9Sstevel@tonic-gate 		return (decrypt_update(dev, ARG, mode, rval));
67587c478bd9Sstevel@tonic-gate 
67597c478bd9Sstevel@tonic-gate 	case CRYPTO_ENCRYPT_FINAL:
67607c478bd9Sstevel@tonic-gate 		return (encrypt_final(dev, ARG, mode, rval));
67617c478bd9Sstevel@tonic-gate 
67627c478bd9Sstevel@tonic-gate 	case CRYPTO_DECRYPT_FINAL:
67637c478bd9Sstevel@tonic-gate 		return (decrypt_final(dev, ARG, mode, rval));
67647c478bd9Sstevel@tonic-gate 
67657c478bd9Sstevel@tonic-gate 	case CRYPTO_DIGEST_INIT:
67667c478bd9Sstevel@tonic-gate 		return (digest_init(dev, ARG, mode, rval));
67677c478bd9Sstevel@tonic-gate 
67687c478bd9Sstevel@tonic-gate 	case CRYPTO_DIGEST:
67697c478bd9Sstevel@tonic-gate 		return (digest(dev, ARG, mode, rval));
67707c478bd9Sstevel@tonic-gate 
67717c478bd9Sstevel@tonic-gate 	case CRYPTO_DIGEST_UPDATE:
67727c478bd9Sstevel@tonic-gate 		return (digest_update(dev, ARG, mode, rval));
67737c478bd9Sstevel@tonic-gate 
67747c478bd9Sstevel@tonic-gate 	case CRYPTO_DIGEST_KEY:
67757c478bd9Sstevel@tonic-gate 		return (digest_key(dev, ARG, mode, rval));
67767c478bd9Sstevel@tonic-gate 
67777c478bd9Sstevel@tonic-gate 	case CRYPTO_DIGEST_FINAL:
67787c478bd9Sstevel@tonic-gate 		return (digest_final(dev, ARG, mode, rval));
67797c478bd9Sstevel@tonic-gate 
67807c478bd9Sstevel@tonic-gate 	case CRYPTO_SIGN_INIT:
67817c478bd9Sstevel@tonic-gate 		return (sign_init(dev, ARG, mode, rval));
67827c478bd9Sstevel@tonic-gate 
67837c478bd9Sstevel@tonic-gate 	case CRYPTO_SIGN:
67847c478bd9Sstevel@tonic-gate 		return (sign(dev, ARG, mode, rval));
67857c478bd9Sstevel@tonic-gate 
67867c478bd9Sstevel@tonic-gate 	case CRYPTO_SIGN_UPDATE:
67877c478bd9Sstevel@tonic-gate 		return (sign_update(dev, ARG, mode, rval));
67887c478bd9Sstevel@tonic-gate 
67897c478bd9Sstevel@tonic-gate 	case CRYPTO_SIGN_FINAL:
67907c478bd9Sstevel@tonic-gate 		return (sign_final(dev, ARG, mode, rval));
67917c478bd9Sstevel@tonic-gate 
67927c478bd9Sstevel@tonic-gate 	case CRYPTO_SIGN_RECOVER_INIT:
67937c478bd9Sstevel@tonic-gate 		return (sign_recover_init(dev, ARG, mode, rval));
67947c478bd9Sstevel@tonic-gate 
67957c478bd9Sstevel@tonic-gate 	case CRYPTO_SIGN_RECOVER:
67967c478bd9Sstevel@tonic-gate 		return (sign_recover(dev, ARG, mode, rval));
67977c478bd9Sstevel@tonic-gate 
67987c478bd9Sstevel@tonic-gate 	case CRYPTO_VERIFY_INIT:
67997c478bd9Sstevel@tonic-gate 		return (verify_init(dev, ARG, mode, rval));
68007c478bd9Sstevel@tonic-gate 
68017c478bd9Sstevel@tonic-gate 	case CRYPTO_VERIFY:
68027c478bd9Sstevel@tonic-gate 		return (verify(dev, ARG, mode, rval));
68037c478bd9Sstevel@tonic-gate 
68047c478bd9Sstevel@tonic-gate 	case CRYPTO_VERIFY_UPDATE:
68057c478bd9Sstevel@tonic-gate 		return (verify_update(dev, ARG, mode, rval));
68067c478bd9Sstevel@tonic-gate 
68077c478bd9Sstevel@tonic-gate 	case CRYPTO_VERIFY_FINAL:
68087c478bd9Sstevel@tonic-gate 		return (verify_final(dev, ARG, mode, rval));
68097c478bd9Sstevel@tonic-gate 
68107c478bd9Sstevel@tonic-gate 	case CRYPTO_VERIFY_RECOVER_INIT:
68117c478bd9Sstevel@tonic-gate 		return (verify_recover_init(dev, ARG, mode, rval));
68127c478bd9Sstevel@tonic-gate 
68137c478bd9Sstevel@tonic-gate 	case CRYPTO_VERIFY_RECOVER:
68147c478bd9Sstevel@tonic-gate 		return (verify_recover(dev, ARG, mode, rval));
68157c478bd9Sstevel@tonic-gate 
6816cd964fceSMatt Barden 	case CRYPTO_MAC_INIT:
6817cd964fceSMatt Barden 		return (mac_init(dev, ARG, mode, rval));
6818cd964fceSMatt Barden 
6819cd964fceSMatt Barden 	case CRYPTO_MAC:
6820cd964fceSMatt Barden 		return (mac(dev, ARG, mode, rval));
6821cd964fceSMatt Barden 
6822cd964fceSMatt Barden 	case CRYPTO_MAC_UPDATE:
6823cd964fceSMatt Barden 		return (mac_update(dev, ARG, mode, rval));
6824cd964fceSMatt Barden 
6825cd964fceSMatt Barden 	case CRYPTO_MAC_FINAL:
6826cd964fceSMatt Barden 		return (mac_final(dev, ARG, mode, rval));
6827cd964fceSMatt Barden 
68287c478bd9Sstevel@tonic-gate 	case CRYPTO_SET_PIN:
68297c478bd9Sstevel@tonic-gate 		return (set_pin(dev, ARG, mode, rval));
68307c478bd9Sstevel@tonic-gate 
68317c478bd9Sstevel@tonic-gate 	case CRYPTO_LOGIN:
68327c478bd9Sstevel@tonic-gate 		return (login(dev, ARG, mode, rval));
68337c478bd9Sstevel@tonic-gate 
68347c478bd9Sstevel@tonic-gate 	case CRYPTO_LOGOUT:
68357c478bd9Sstevel@tonic-gate 		return (logout(dev, ARG, mode, rval));
68367c478bd9Sstevel@tonic-gate 
68377c478bd9Sstevel@tonic-gate 	case CRYPTO_SEED_RANDOM:
68387c478bd9Sstevel@tonic-gate 		return (seed_random(dev, ARG, mode, rval));
68397c478bd9Sstevel@tonic-gate 
68407c478bd9Sstevel@tonic-gate 	case CRYPTO_GENERATE_RANDOM:
68417c478bd9Sstevel@tonic-gate 		return (generate_random(dev, ARG, mode, rval));
68427c478bd9Sstevel@tonic-gate 
68437c478bd9Sstevel@tonic-gate 	case CRYPTO_OBJECT_CREATE:
68447c478bd9Sstevel@tonic-gate 		return (object_create(dev, ARG, mode, rval));
68457c478bd9Sstevel@tonic-gate 
68467c478bd9Sstevel@tonic-gate 	case CRYPTO_OBJECT_COPY:
68477c478bd9Sstevel@tonic-gate 		return (object_copy(dev, ARG, mode, rval));
68487c478bd9Sstevel@tonic-gate 
68497c478bd9Sstevel@tonic-gate 	case CRYPTO_OBJECT_DESTROY:
68507c478bd9Sstevel@tonic-gate 		return (object_destroy(dev, ARG, mode, rval));
68517c478bd9Sstevel@tonic-gate 
68527c478bd9Sstevel@tonic-gate 	case CRYPTO_OBJECT_GET_ATTRIBUTE_VALUE:
68537c478bd9Sstevel@tonic-gate 		return (object_get_attribute_value(dev, ARG, mode, rval));
68547c478bd9Sstevel@tonic-gate 
68557c478bd9Sstevel@tonic-gate 	case CRYPTO_OBJECT_GET_SIZE:
68567c478bd9Sstevel@tonic-gate 		return (object_get_size(dev, ARG, mode, rval));
68577c478bd9Sstevel@tonic-gate 
68587c478bd9Sstevel@tonic-gate 	case CRYPTO_OBJECT_SET_ATTRIBUTE_VALUE:
68597c478bd9Sstevel@tonic-gate 		return (object_set_attribute_value(dev, ARG, mode, rval));
68607c478bd9Sstevel@tonic-gate 
68617c478bd9Sstevel@tonic-gate 	case CRYPTO_OBJECT_FIND_INIT:
68627c478bd9Sstevel@tonic-gate 		return (object_find_init(dev, ARG, mode, rval));
68637c478bd9Sstevel@tonic-gate 
68647c478bd9Sstevel@tonic-gate 	case CRYPTO_OBJECT_FIND_UPDATE:
68657c478bd9Sstevel@tonic-gate 		return (object_find_update(dev, ARG, mode, rval));
68667c478bd9Sstevel@tonic-gate 
68677c478bd9Sstevel@tonic-gate 	case CRYPTO_OBJECT_FIND_FINAL:
68687c478bd9Sstevel@tonic-gate 		return (object_find_final(dev, ARG, mode, rval));
68697c478bd9Sstevel@tonic-gate 
68707c478bd9Sstevel@tonic-gate 	case CRYPTO_GENERATE_KEY:
68717c478bd9Sstevel@tonic-gate 		return (object_generate_key(dev, ARG, mode, rval));
68727c478bd9Sstevel@tonic-gate 
68737c478bd9Sstevel@tonic-gate 	case CRYPTO_GENERATE_KEY_PAIR:
68747c478bd9Sstevel@tonic-gate 		return (object_generate_key_pair(dev, ARG, mode, rval));
68757c478bd9Sstevel@tonic-gate 
68767c478bd9Sstevel@tonic-gate 	case CRYPTO_WRAP_KEY:
68777c478bd9Sstevel@tonic-gate 		return (object_wrap_key(dev, ARG, mode, rval));
68787c478bd9Sstevel@tonic-gate 
68797c478bd9Sstevel@tonic-gate 	case CRYPTO_UNWRAP_KEY:
68807c478bd9Sstevel@tonic-gate 		return (object_unwrap_key(dev, ARG, mode, rval));
68817c478bd9Sstevel@tonic-gate 
68827c478bd9Sstevel@tonic-gate 	case CRYPTO_DERIVE_KEY:
68837c478bd9Sstevel@tonic-gate 		return (object_derive_key(dev, ARG, mode, rval));
6884034448feSmcpowers 
6885034448feSmcpowers 	case CRYPTO_NOSTORE_GENERATE_KEY:
6886034448feSmcpowers 		return (nostore_generate_key(dev, ARG, mode, rval));
6887034448feSmcpowers 
6888034448feSmcpowers 	case CRYPTO_NOSTORE_GENERATE_KEY_PAIR:
6889034448feSmcpowers 		return (nostore_generate_key_pair(dev, ARG, mode, rval));
6890034448feSmcpowers 
6891034448feSmcpowers 	case CRYPTO_NOSTORE_DERIVE_KEY:
6892034448feSmcpowers 		return (nostore_derive_key(dev, ARG, mode, rval));
68937c478bd9Sstevel@tonic-gate 	}
68947c478bd9Sstevel@tonic-gate 	return (EINVAL);
68957c478bd9Sstevel@tonic-gate }
68967c478bd9Sstevel@tonic-gate 
68977c478bd9Sstevel@tonic-gate /*
68987c478bd9Sstevel@tonic-gate  * Check for the project.max-crypto-memory resource control.
68997c478bd9Sstevel@tonic-gate  */
69007c478bd9Sstevel@tonic-gate static int
crypto_buffer_check(size_t need)6901c1a9a9c3Skrishna crypto_buffer_check(size_t need)
69027c478bd9Sstevel@tonic-gate {
6903db1cde31Skrishna 	kproject_t *kpj;
69047c478bd9Sstevel@tonic-gate 
69057c478bd9Sstevel@tonic-gate 	if (need == 0)
69067c478bd9Sstevel@tonic-gate 		return (CRYPTO_SUCCESS);
69077c478bd9Sstevel@tonic-gate 
69087c478bd9Sstevel@tonic-gate 	mutex_enter(&curproc->p_lock);
6909db1cde31Skrishna 	kpj = curproc->p_task->tk_proj;
6910c1a9a9c3Skrishna 	mutex_enter(&(kpj->kpj_data.kpd_crypto_lock));
6911db1cde31Skrishna 
6912db1cde31Skrishna 	if (kpj->kpj_data.kpd_crypto_mem + need >
6913db1cde31Skrishna 	    kpj->kpj_data.kpd_crypto_mem_ctl) {
6914db1cde31Skrishna 		if (rctl_test(rc_project_crypto_mem,
6915db1cde31Skrishna 		    kpj->kpj_rctls, curproc, need, 0) & RCT_DENY) {
6916c1a9a9c3Skrishna 			mutex_exit(&(kpj->kpj_data.kpd_crypto_lock));
6917db1cde31Skrishna 			mutex_exit(&curproc->p_lock);
6918db1cde31Skrishna 			return (CRYPTO_HOST_MEMORY);
6919db1cde31Skrishna 		}
69207c478bd9Sstevel@tonic-gate 	}
69217c478bd9Sstevel@tonic-gate 
6922db1cde31Skrishna 	kpj->kpj_data.kpd_crypto_mem += need;
6923c1a9a9c3Skrishna 	mutex_exit(&(kpj->kpj_data.kpd_crypto_lock));
69247c478bd9Sstevel@tonic-gate 
6925c1a9a9c3Skrishna 	curproc->p_crypto_mem += need;
69267c478bd9Sstevel@tonic-gate 	mutex_exit(&curproc->p_lock);
6927db1cde31Skrishna 
69287c478bd9Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
69297c478bd9Sstevel@tonic-gate }
6930