xref: /onnv-gate/usr/src/uts/common/crypto/spi/kcf_spi.c (revision 9505:cec264b91738)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52800Skrishna  * Common Development and Distribution License (the "License").
62800Skrishna  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*9505SBhargava.Yenduri@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * This file is part of the core Kernel Cryptographic Framework.
280Sstevel@tonic-gate  * It implements the SPI functions exported to cryptographic
290Sstevel@tonic-gate  * providers.
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <sys/ksynch.h>
330Sstevel@tonic-gate #include <sys/cmn_err.h>
340Sstevel@tonic-gate #include <sys/ddi.h>
350Sstevel@tonic-gate #include <sys/sunddi.h>
360Sstevel@tonic-gate #include <sys/modctl.h>
370Sstevel@tonic-gate #include <sys/crypto/common.h>
380Sstevel@tonic-gate #include <sys/crypto/impl.h>
390Sstevel@tonic-gate #include <sys/crypto/sched_impl.h>
400Sstevel@tonic-gate #include <sys/crypto/spi.h>
410Sstevel@tonic-gate #include <sys/taskq.h>
420Sstevel@tonic-gate #include <sys/disp.h>
430Sstevel@tonic-gate #include <sys/kstat.h>
440Sstevel@tonic-gate #include <sys/policy.h>
454494Skrishna #include <sys/cpuvar.h>
460Sstevel@tonic-gate 
470Sstevel@tonic-gate /*
480Sstevel@tonic-gate  * minalloc and maxalloc values to be used for taskq_create().
490Sstevel@tonic-gate  */
504494Skrishna int crypto_taskq_threads = CRYPTO_TASKQ_THREADS;
510Sstevel@tonic-gate int crypto_taskq_minalloc = CYRPTO_TASKQ_MIN;
520Sstevel@tonic-gate int crypto_taskq_maxalloc = CRYPTO_TASKQ_MAX;
530Sstevel@tonic-gate 
540Sstevel@tonic-gate static void remove_provider(kcf_provider_desc_t *);
550Sstevel@tonic-gate static void process_logical_providers(crypto_provider_info_t *,
560Sstevel@tonic-gate     kcf_provider_desc_t *);
570Sstevel@tonic-gate static int init_prov_mechs(crypto_provider_info_t *, kcf_provider_desc_t *);
580Sstevel@tonic-gate static int kcf_prov_kstat_update(kstat_t *, int);
594373Skrishna static void undo_register_provider_extra(kcf_provider_desc_t *);
604373Skrishna static void delete_kstat(kcf_provider_desc_t *);
610Sstevel@tonic-gate 
620Sstevel@tonic-gate static kcf_prov_stats_t kcf_stats_ks_data_template = {
630Sstevel@tonic-gate 	{ "kcf_ops_total",		KSTAT_DATA_UINT64 },
640Sstevel@tonic-gate 	{ "kcf_ops_passed",		KSTAT_DATA_UINT64 },
650Sstevel@tonic-gate 	{ "kcf_ops_failed",		KSTAT_DATA_UINT64 },
660Sstevel@tonic-gate 	{ "kcf_ops_returned_busy",	KSTAT_DATA_UINT64 }
670Sstevel@tonic-gate };
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #define	KCF_SPI_COPY_OPS(src, dst, ops) if ((src)->ops != NULL) \
700Sstevel@tonic-gate 	*((dst)->ops) = *((src)->ops);
710Sstevel@tonic-gate 
720Sstevel@tonic-gate /*
734219Smcpowers  * Copy an ops vector from src to dst. Used during provider registration
744219Smcpowers  * to copy the ops vector from the provider info structure to the
754219Smcpowers  * provider descriptor maintained by KCF.
764219Smcpowers  * Copying the ops vector specified by the provider is needed since the
774219Smcpowers  * framework does not require the provider info structure to be
784219Smcpowers  * persistent.
794219Smcpowers  */
804219Smcpowers static void
814219Smcpowers copy_ops_vector_v1(crypto_ops_t *src_ops, crypto_ops_t *dst_ops)
824219Smcpowers {
834219Smcpowers 	KCF_SPI_COPY_OPS(src_ops, dst_ops, co_control_ops);
844219Smcpowers 	KCF_SPI_COPY_OPS(src_ops, dst_ops, co_digest_ops);
854219Smcpowers 	KCF_SPI_COPY_OPS(src_ops, dst_ops, co_cipher_ops);
864219Smcpowers 	KCF_SPI_COPY_OPS(src_ops, dst_ops, co_mac_ops);
874219Smcpowers 	KCF_SPI_COPY_OPS(src_ops, dst_ops, co_sign_ops);
884219Smcpowers 	KCF_SPI_COPY_OPS(src_ops, dst_ops, co_verify_ops);
894219Smcpowers 	KCF_SPI_COPY_OPS(src_ops, dst_ops, co_dual_ops);
904219Smcpowers 	KCF_SPI_COPY_OPS(src_ops, dst_ops, co_dual_cipher_mac_ops);
914219Smcpowers 	KCF_SPI_COPY_OPS(src_ops, dst_ops, co_random_ops);
924219Smcpowers 	KCF_SPI_COPY_OPS(src_ops, dst_ops, co_session_ops);
934219Smcpowers 	KCF_SPI_COPY_OPS(src_ops, dst_ops, co_object_ops);
944219Smcpowers 	KCF_SPI_COPY_OPS(src_ops, dst_ops, co_key_ops);
954219Smcpowers 	KCF_SPI_COPY_OPS(src_ops, dst_ops, co_provider_ops);
964219Smcpowers 	KCF_SPI_COPY_OPS(src_ops, dst_ops, co_ctx_ops);
974219Smcpowers }
984219Smcpowers 
994219Smcpowers static void
1004219Smcpowers copy_ops_vector_v2(crypto_ops_t *src_ops, crypto_ops_t *dst_ops)
1014219Smcpowers {
1024219Smcpowers 	KCF_SPI_COPY_OPS(src_ops, dst_ops, co_mech_ops);
1034219Smcpowers }
1044219Smcpowers 
1054219Smcpowers static void
1064219Smcpowers copy_ops_vector_v3(crypto_ops_t *src_ops, crypto_ops_t *dst_ops)
1074219Smcpowers {
1084219Smcpowers 	KCF_SPI_COPY_OPS(src_ops, dst_ops, co_nostore_key_ops);
1094219Smcpowers }
1104219Smcpowers 
1114219Smcpowers /*
1120Sstevel@tonic-gate  * This routine is used to add cryptographic providers to the KEF framework.
1130Sstevel@tonic-gate  * Providers pass a crypto_provider_info structure to crypto_register_provider()
1140Sstevel@tonic-gate  * and get back a handle.  The crypto_provider_info structure contains a
1150Sstevel@tonic-gate  * list of mechanisms supported by the provider and an ops vector containing
1160Sstevel@tonic-gate  * provider entry points.  Hardware providers call this routine in their attach
1170Sstevel@tonic-gate  * routines.  Software providers call this routine in their _init() routine.
1180Sstevel@tonic-gate  */
1190Sstevel@tonic-gate int
1200Sstevel@tonic-gate crypto_register_provider(crypto_provider_info_t *info,
1210Sstevel@tonic-gate     crypto_kcf_provider_handle_t *handle)
1220Sstevel@tonic-gate {
1234373Skrishna 	int need_verify;
1240Sstevel@tonic-gate 	struct modctl *mcp;
1250Sstevel@tonic-gate 	char *name;
1260Sstevel@tonic-gate 	char ks_name[KSTAT_STRLEN];
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	kcf_provider_desc_t *prov_desc = NULL;
1290Sstevel@tonic-gate 	int ret = CRYPTO_ARGUMENTS_BAD;
1300Sstevel@tonic-gate 
1314219Smcpowers 	if (info->pi_interface_version > CRYPTO_SPI_VERSION_3)
1320Sstevel@tonic-gate 		return (CRYPTO_VERSION_MISMATCH);
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	/*
1350Sstevel@tonic-gate 	 * Check provider type, must be software, hardware, or logical.
1360Sstevel@tonic-gate 	 */
1370Sstevel@tonic-gate 	if (info->pi_provider_type != CRYPTO_HW_PROVIDER &&
1380Sstevel@tonic-gate 	    info->pi_provider_type != CRYPTO_SW_PROVIDER &&
1390Sstevel@tonic-gate 	    info->pi_provider_type != CRYPTO_LOGICAL_PROVIDER)
1400Sstevel@tonic-gate 		return (CRYPTO_ARGUMENTS_BAD);
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	/*
1430Sstevel@tonic-gate 	 * Allocate and initialize a new provider descriptor. We also
1440Sstevel@tonic-gate 	 * hold it and release it when done.
1450Sstevel@tonic-gate 	 */
1460Sstevel@tonic-gate 	prov_desc = kcf_alloc_provider_desc(info);
1470Sstevel@tonic-gate 	KCF_PROV_REFHOLD(prov_desc);
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	prov_desc->pd_prov_type = info->pi_provider_type;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	/* provider-private handle, opaque to KCF */
1520Sstevel@tonic-gate 	prov_desc->pd_prov_handle = info->pi_provider_handle;
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	/* copy provider description string */
1550Sstevel@tonic-gate 	if (info->pi_provider_description != NULL) {
1560Sstevel@tonic-gate 		/*
1570Sstevel@tonic-gate 		 * pi_provider_descriptor is a string that can contain
1580Sstevel@tonic-gate 		 * up to CRYPTO_PROVIDER_DESCR_MAX_LEN + 1 characters
1590Sstevel@tonic-gate 		 * INCLUDING the terminating null character. A bcopy()
1600Sstevel@tonic-gate 		 * is necessary here as pd_description should not have
1610Sstevel@tonic-gate 		 * a null character. See comments in kcf_alloc_provider_desc()
1620Sstevel@tonic-gate 		 * for details on pd_description field.
1630Sstevel@tonic-gate 		 */
1640Sstevel@tonic-gate 		bcopy(info->pi_provider_description, prov_desc->pd_description,
1650Sstevel@tonic-gate 		    min(strlen(info->pi_provider_description),
1660Sstevel@tonic-gate 		    CRYPTO_PROVIDER_DESCR_MAX_LEN));
1670Sstevel@tonic-gate 	}
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	if (info->pi_provider_type != CRYPTO_LOGICAL_PROVIDER) {
1700Sstevel@tonic-gate 		if (info->pi_ops_vector == NULL) {
1714219Smcpowers 			goto bail;
1720Sstevel@tonic-gate 		}
173904Smcpowers 		copy_ops_vector_v1(info->pi_ops_vector,
174904Smcpowers 		    prov_desc->pd_ops_vector);
1754219Smcpowers 		if (info->pi_interface_version >= CRYPTO_SPI_VERSION_2) {
176904Smcpowers 			copy_ops_vector_v2(info->pi_ops_vector,
177904Smcpowers 			    prov_desc->pd_ops_vector);
178904Smcpowers 			prov_desc->pd_flags = info->pi_flags;
179904Smcpowers 		}
1804219Smcpowers 		if (info->pi_interface_version == CRYPTO_SPI_VERSION_3) {
1814219Smcpowers 			copy_ops_vector_v3(info->pi_ops_vector,
1824219Smcpowers 			    prov_desc->pd_ops_vector);
1834219Smcpowers 		}
1840Sstevel@tonic-gate 	}
1850Sstevel@tonic-gate 
1864219Smcpowers 	/* object_ops and nostore_key_ops are mutually exclusive */
1874219Smcpowers 	if (prov_desc->pd_ops_vector->co_object_ops &&
1884219Smcpowers 	    prov_desc->pd_ops_vector->co_nostore_key_ops) {
1894219Smcpowers 		goto bail;
1904219Smcpowers 	}
1910Sstevel@tonic-gate 	/*
1920Sstevel@tonic-gate 	 * For software providers, copy the module name and module ID.
1930Sstevel@tonic-gate 	 * For hardware providers, copy the driver name and instance.
1940Sstevel@tonic-gate 	 */
1950Sstevel@tonic-gate 	switch (info->pi_provider_type) {
1960Sstevel@tonic-gate 	case  CRYPTO_SW_PROVIDER:
1970Sstevel@tonic-gate 		if (info->pi_provider_dev.pd_sw == NULL)
1980Sstevel@tonic-gate 			goto bail;
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 		if ((mcp = mod_getctl(info->pi_provider_dev.pd_sw)) == NULL)
2010Sstevel@tonic-gate 			goto bail;
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 		prov_desc->pd_module_id = mcp->mod_id;
2040Sstevel@tonic-gate 		name = mcp->mod_modname;
2050Sstevel@tonic-gate 		break;
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	case CRYPTO_HW_PROVIDER:
2080Sstevel@tonic-gate 	case CRYPTO_LOGICAL_PROVIDER:
2090Sstevel@tonic-gate 		if (info->pi_provider_dev.pd_hw == NULL)
2100Sstevel@tonic-gate 			goto bail;
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 		prov_desc->pd_instance =
2130Sstevel@tonic-gate 		    ddi_get_instance(info->pi_provider_dev.pd_hw);
2140Sstevel@tonic-gate 		name = (char *)ddi_driver_name(info->pi_provider_dev.pd_hw);
2150Sstevel@tonic-gate 		break;
2160Sstevel@tonic-gate 	}
2170Sstevel@tonic-gate 	if (name == NULL)
2180Sstevel@tonic-gate 		goto bail;
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	prov_desc->pd_name = kmem_alloc(strlen(name) + 1, KM_SLEEP);
2210Sstevel@tonic-gate 	(void) strcpy(prov_desc->pd_name, name);
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	if ((prov_desc->pd_mctlp = kcf_get_modctl(info)) == NULL)
2240Sstevel@tonic-gate 		goto bail;
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	/* process the mechanisms supported by the provider */
2270Sstevel@tonic-gate 	if ((ret = init_prov_mechs(info, prov_desc)) != CRYPTO_SUCCESS)
2280Sstevel@tonic-gate 		goto bail;
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	/*
2310Sstevel@tonic-gate 	 * Add provider to providers tables, also sets the descriptor
2320Sstevel@tonic-gate 	 * pd_prov_id field.
2330Sstevel@tonic-gate 	 */
2340Sstevel@tonic-gate 	if ((ret = kcf_prov_tab_add_provider(prov_desc)) != CRYPTO_SUCCESS) {
2350Sstevel@tonic-gate 		undo_register_provider(prov_desc, B_FALSE);
2360Sstevel@tonic-gate 		goto bail;
2370Sstevel@tonic-gate 	}
2380Sstevel@tonic-gate 
2394373Skrishna 	if ((need_verify = kcf_need_signature_verification(prov_desc)) == -1) {
2404373Skrishna 		undo_register_provider(prov_desc, B_TRUE);
2414373Skrishna 		ret = CRYPTO_MODVERIFICATION_FAILED;
2424373Skrishna 		goto bail;
2430Sstevel@tonic-gate 	}
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	/*
2460Sstevel@tonic-gate 	 * We create a taskq only for a hardware provider. The global
2474494Skrishna 	 * software queue is used for software providers. We handle ordering
2484494Skrishna 	 * of multi-part requests in the taskq routine. So, it is safe to
2494494Skrishna 	 * have multiple threads for the taskq. We pass TASKQ_PREPOPULATE flag
2504494Skrishna 	 * to keep some entries cached to improve performance.
2510Sstevel@tonic-gate 	 */
2520Sstevel@tonic-gate 	if (prov_desc->pd_prov_type == CRYPTO_HW_PROVIDER)
253*9505SBhargava.Yenduri@Sun.COM 		prov_desc->pd_taskq = taskq_create("kcf_taskq",
2544494Skrishna 		    crypto_taskq_threads, minclsyspri,
2554494Skrishna 		    crypto_taskq_minalloc, crypto_taskq_maxalloc,
2564494Skrishna 		    TASKQ_PREPOPULATE);
2570Sstevel@tonic-gate 	else
258*9505SBhargava.Yenduri@Sun.COM 		prov_desc->pd_taskq = NULL;
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	/* no kernel session to logical providers */
2610Sstevel@tonic-gate 	if (prov_desc->pd_prov_type != CRYPTO_LOGICAL_PROVIDER) {
2620Sstevel@tonic-gate 		/*
2630Sstevel@tonic-gate 		 * Open a session for session-oriented providers. This session
2640Sstevel@tonic-gate 		 * is used for all kernel consumers. This is fine as a provider
2650Sstevel@tonic-gate 		 * is required to support multiple thread access to a session.
2660Sstevel@tonic-gate 		 * We can do this only after the taskq has been created as we
2670Sstevel@tonic-gate 		 * do a kcf_submit_request() to open the session.
2680Sstevel@tonic-gate 		 */
2690Sstevel@tonic-gate 		if (KCF_PROV_SESSION_OPS(prov_desc) != NULL) {
2700Sstevel@tonic-gate 			kcf_req_params_t params;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 			KCF_WRAP_SESSION_OPS_PARAMS(&params,
2730Sstevel@tonic-gate 			    KCF_OP_SESSION_OPEN, &prov_desc->pd_sid, 0,
2740Sstevel@tonic-gate 			    CRYPTO_USER, NULL, 0, prov_desc);
2750Sstevel@tonic-gate 			ret = kcf_submit_request(prov_desc, NULL, NULL, &params,
2760Sstevel@tonic-gate 			    B_FALSE);
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 			if (ret != CRYPTO_SUCCESS) {
2790Sstevel@tonic-gate 				undo_register_provider(prov_desc, B_TRUE);
2800Sstevel@tonic-gate 				ret = CRYPTO_FAILED;
2810Sstevel@tonic-gate 				goto bail;
2820Sstevel@tonic-gate 			}
2830Sstevel@tonic-gate 		}
2840Sstevel@tonic-gate 	}
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	if (prov_desc->pd_prov_type != CRYPTO_LOGICAL_PROVIDER) {
2870Sstevel@tonic-gate 		/*
2880Sstevel@tonic-gate 		 * Create the kstat for this provider. There is a kstat
2890Sstevel@tonic-gate 		 * installed for each successfully registered provider.
2900Sstevel@tonic-gate 		 * This kstat is deleted, when the provider unregisters.
2910Sstevel@tonic-gate 		 */
2920Sstevel@tonic-gate 		if (prov_desc->pd_prov_type == CRYPTO_SW_PROVIDER) {
2930Sstevel@tonic-gate 			(void) snprintf(ks_name, KSTAT_STRLEN, "%s_%s",
2940Sstevel@tonic-gate 			    prov_desc->pd_name, "provider_stats");
2950Sstevel@tonic-gate 		} else {
2960Sstevel@tonic-gate 			(void) snprintf(ks_name, KSTAT_STRLEN, "%s_%d_%u_%s",
2970Sstevel@tonic-gate 			    prov_desc->pd_name, prov_desc->pd_instance,
2980Sstevel@tonic-gate 			    prov_desc->pd_prov_id, "provider_stats");
2990Sstevel@tonic-gate 		}
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 		prov_desc->pd_kstat = kstat_create("kcf", 0, ks_name, "crypto",
3020Sstevel@tonic-gate 		    KSTAT_TYPE_NAMED, sizeof (kcf_prov_stats_t) /
3030Sstevel@tonic-gate 		    sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 		if (prov_desc->pd_kstat != NULL) {
3060Sstevel@tonic-gate 			bcopy(&kcf_stats_ks_data_template,
3070Sstevel@tonic-gate 			    &prov_desc->pd_ks_data,
3080Sstevel@tonic-gate 			    sizeof (kcf_stats_ks_data_template));
3090Sstevel@tonic-gate 			prov_desc->pd_kstat->ks_data = &prov_desc->pd_ks_data;
3100Sstevel@tonic-gate 			KCF_PROV_REFHOLD(prov_desc);
3110Sstevel@tonic-gate 			prov_desc->pd_kstat->ks_private = prov_desc;
3120Sstevel@tonic-gate 			prov_desc->pd_kstat->ks_update = kcf_prov_kstat_update;
3130Sstevel@tonic-gate 			kstat_install(prov_desc->pd_kstat);
3140Sstevel@tonic-gate 		}
3150Sstevel@tonic-gate 	}
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	if (prov_desc->pd_prov_type == CRYPTO_HW_PROVIDER)
3180Sstevel@tonic-gate 		process_logical_providers(info, prov_desc);
3190Sstevel@tonic-gate 
3204373Skrishna 	if (need_verify == 1) {
321*9505SBhargava.Yenduri@Sun.COM 		/* kcf_verify_signature routine will release this hold */
3224373Skrishna 		KCF_PROV_REFHOLD(prov_desc);
3230Sstevel@tonic-gate 
3244373Skrishna 		if (prov_desc->pd_prov_type == CRYPTO_HW_PROVIDER) {
3254373Skrishna 			/*
3264373Skrishna 			 * It is not safe to make the door upcall to kcfd from
3274373Skrishna 			 * this context since the kcfd thread could reenter
3284373Skrishna 			 * devfs. So, we dispatch a taskq job to do the
3294373Skrishna 			 * verification and return to the provider.
3304373Skrishna 			 */
3314373Skrishna 			(void) taskq_dispatch(system_taskq,
3324373Skrishna 			    kcf_verify_signature, (void *)prov_desc, TQ_SLEEP);
3334373Skrishna 		} else if (prov_desc->pd_prov_type == CRYPTO_SW_PROVIDER) {
3344373Skrishna 			kcf_verify_signature(prov_desc);
3354373Skrishna 			if (prov_desc->pd_state ==
3364373Skrishna 			    KCF_PROV_VERIFICATION_FAILED) {
3374373Skrishna 				undo_register_provider_extra(prov_desc);
3384373Skrishna 				ret = CRYPTO_MODVERIFICATION_FAILED;
3394373Skrishna 				goto bail;
3404373Skrishna 			}
3410Sstevel@tonic-gate 		}
3424373Skrishna 	} else {
3434373Skrishna 		mutex_enter(&prov_desc->pd_lock);
3444373Skrishna 		prov_desc->pd_state = KCF_PROV_READY;
3454373Skrishna 		mutex_exit(&prov_desc->pd_lock);
3464373Skrishna 		kcf_do_notify(prov_desc, B_TRUE);
3470Sstevel@tonic-gate 	}
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	*handle = prov_desc->pd_kcf_prov_handle;
3504373Skrishna 	ret = CRYPTO_SUCCESS;
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate bail:
3530Sstevel@tonic-gate 	KCF_PROV_REFRELE(prov_desc);
3540Sstevel@tonic-gate 	return (ret);
3550Sstevel@tonic-gate }
3560Sstevel@tonic-gate 
357*9505SBhargava.Yenduri@Sun.COM /* Return the number of holds on a provider. */
358*9505SBhargava.Yenduri@Sun.COM int
359*9505SBhargava.Yenduri@Sun.COM kcf_get_refcnt(kcf_provider_desc_t *pd, boolean_t do_lock)
360*9505SBhargava.Yenduri@Sun.COM {
361*9505SBhargava.Yenduri@Sun.COM 	int i;
362*9505SBhargava.Yenduri@Sun.COM 	int refcnt = 0;
363*9505SBhargava.Yenduri@Sun.COM 
364*9505SBhargava.Yenduri@Sun.COM 	if (do_lock)
365*9505SBhargava.Yenduri@Sun.COM 		for (i = 0; i < pd->pd_nbins; i++)
366*9505SBhargava.Yenduri@Sun.COM 			mutex_enter(&(pd->pd_percpu_bins[i].kp_lock));
367*9505SBhargava.Yenduri@Sun.COM 
368*9505SBhargava.Yenduri@Sun.COM 	for (i = 0; i < pd->pd_nbins; i++)
369*9505SBhargava.Yenduri@Sun.COM 		refcnt += pd->pd_percpu_bins[i].kp_holdcnt;
370*9505SBhargava.Yenduri@Sun.COM 
371*9505SBhargava.Yenduri@Sun.COM 	if (do_lock)
372*9505SBhargava.Yenduri@Sun.COM 		for (i = 0; i < pd->pd_nbins; i++)
373*9505SBhargava.Yenduri@Sun.COM 			mutex_exit(&(pd->pd_percpu_bins[i].kp_lock));
374*9505SBhargava.Yenduri@Sun.COM 
375*9505SBhargava.Yenduri@Sun.COM 	return (refcnt);
376*9505SBhargava.Yenduri@Sun.COM }
377*9505SBhargava.Yenduri@Sun.COM 
3780Sstevel@tonic-gate /*
3790Sstevel@tonic-gate  * This routine is used to notify the framework when a provider is being
3800Sstevel@tonic-gate  * removed.  Hardware providers call this routine in their detach routines.
3810Sstevel@tonic-gate  * Software providers call this routine in their _fini() routine.
3820Sstevel@tonic-gate  */
3830Sstevel@tonic-gate int
3840Sstevel@tonic-gate crypto_unregister_provider(crypto_kcf_provider_handle_t handle)
3850Sstevel@tonic-gate {
3860Sstevel@tonic-gate 	uint_t mech_idx;
3870Sstevel@tonic-gate 	kcf_provider_desc_t *desc;
3880Sstevel@tonic-gate 	kcf_prov_state_t saved_state;
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	/* lookup provider descriptor */
3910Sstevel@tonic-gate 	if ((desc = kcf_prov_tab_lookup((crypto_provider_id_t)handle)) == NULL)
3920Sstevel@tonic-gate 		return (CRYPTO_UNKNOWN_PROVIDER);
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	mutex_enter(&desc->pd_lock);
3950Sstevel@tonic-gate 	/*
3960Sstevel@tonic-gate 	 * Check if any other thread is disabling or removing
3970Sstevel@tonic-gate 	 * this provider. We return if this is the case.
3980Sstevel@tonic-gate 	 */
3990Sstevel@tonic-gate 	if (desc->pd_state >= KCF_PROV_DISABLED) {
4000Sstevel@tonic-gate 		mutex_exit(&desc->pd_lock);
4010Sstevel@tonic-gate 		/* Release reference held by kcf_prov_tab_lookup(). */
4020Sstevel@tonic-gate 		KCF_PROV_REFRELE(desc);
4030Sstevel@tonic-gate 		return (CRYPTO_BUSY);
4040Sstevel@tonic-gate 	}
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	saved_state = desc->pd_state;
407*9505SBhargava.Yenduri@Sun.COM 	desc->pd_state = KCF_PROV_UNREGISTERING;
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 	if (saved_state == KCF_PROV_BUSY) {
4100Sstevel@tonic-gate 		/*
4114912Skrishna 		 * The per-provider taskq threads may be waiting. We
4124912Skrishna 		 * signal them so that they can start failing requests.
4130Sstevel@tonic-gate 		 */
4144912Skrishna 		cv_broadcast(&desc->pd_resume_cv);
4150Sstevel@tonic-gate 	}
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	mutex_exit(&desc->pd_lock);
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	if (desc->pd_prov_type != CRYPTO_SW_PROVIDER) {
4200Sstevel@tonic-gate 		remove_provider(desc);
4210Sstevel@tonic-gate 	}
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	if (desc->pd_prov_type != CRYPTO_LOGICAL_PROVIDER) {
4240Sstevel@tonic-gate 		/* remove the provider from the mechanisms tables */
4250Sstevel@tonic-gate 		for (mech_idx = 0; mech_idx < desc->pd_mech_list_count;
4260Sstevel@tonic-gate 		    mech_idx++) {
4270Sstevel@tonic-gate 			kcf_remove_mech_provider(
4280Sstevel@tonic-gate 			    desc->pd_mechanisms[mech_idx].cm_mech_name, desc);
4290Sstevel@tonic-gate 		}
4300Sstevel@tonic-gate 	}
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	/* remove provider from providers table */
4330Sstevel@tonic-gate 	if (kcf_prov_tab_rem_provider((crypto_provider_id_t)handle) !=
4340Sstevel@tonic-gate 	    CRYPTO_SUCCESS) {
4350Sstevel@tonic-gate 		/* Release reference held by kcf_prov_tab_lookup(). */
4360Sstevel@tonic-gate 		KCF_PROV_REFRELE(desc);
4370Sstevel@tonic-gate 		return (CRYPTO_UNKNOWN_PROVIDER);
4380Sstevel@tonic-gate 	}
4390Sstevel@tonic-gate 
4404373Skrishna 	delete_kstat(desc);
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	if (desc->pd_prov_type == CRYPTO_SW_PROVIDER) {
4430Sstevel@tonic-gate 		/*
444*9505SBhargava.Yenduri@Sun.COM 		 * Wait till the existing requests with the provider complete
445*9505SBhargava.Yenduri@Sun.COM 		 * and all the holds are released. All the holds on a software
446*9505SBhargava.Yenduri@Sun.COM 		 * provider are from kernel clients and the hold time
447*9505SBhargava.Yenduri@Sun.COM 		 * is expected to be short. So, we won't be stuck here forever.
4480Sstevel@tonic-gate 		 */
449*9505SBhargava.Yenduri@Sun.COM 		while (kcf_get_refcnt(desc, B_TRUE) > 1) {
450*9505SBhargava.Yenduri@Sun.COM 			/* wait 1 second and try again. */
451*9505SBhargava.Yenduri@Sun.COM 			delay(1 * drv_usectohz(1000000));
452*9505SBhargava.Yenduri@Sun.COM 		}
4530Sstevel@tonic-gate 	} else {
454*9505SBhargava.Yenduri@Sun.COM 		int i;
455*9505SBhargava.Yenduri@Sun.COM 		kcf_prov_cpu_t *mp;
456*9505SBhargava.Yenduri@Sun.COM 
4570Sstevel@tonic-gate 		/*
4580Sstevel@tonic-gate 		 * Wait until requests that have been sent to the provider
4590Sstevel@tonic-gate 		 * complete.
4600Sstevel@tonic-gate 		 */
461*9505SBhargava.Yenduri@Sun.COM 		for (i = 0; i < desc->pd_nbins; i++) {
462*9505SBhargava.Yenduri@Sun.COM 			mp = &(desc->pd_percpu_bins[i]);
463*9505SBhargava.Yenduri@Sun.COM 
464*9505SBhargava.Yenduri@Sun.COM 			mutex_enter(&mp->kp_lock);
465*9505SBhargava.Yenduri@Sun.COM 			while (mp->kp_jobcnt > 0) {
466*9505SBhargava.Yenduri@Sun.COM 				cv_wait(&mp->kp_cv, &mp->kp_lock);
467*9505SBhargava.Yenduri@Sun.COM 			}
468*9505SBhargava.Yenduri@Sun.COM 			mutex_exit(&mp->kp_lock);
469*9505SBhargava.Yenduri@Sun.COM 		}
4700Sstevel@tonic-gate 	}
4710Sstevel@tonic-gate 
472*9505SBhargava.Yenduri@Sun.COM 	mutex_enter(&desc->pd_lock);
473*9505SBhargava.Yenduri@Sun.COM 	desc->pd_state = KCF_PROV_UNREGISTERED;
474*9505SBhargava.Yenduri@Sun.COM 	mutex_exit(&desc->pd_lock);
475*9505SBhargava.Yenduri@Sun.COM 
4764373Skrishna 	kcf_do_notify(desc, B_FALSE);
4772800Skrishna 
478*9505SBhargava.Yenduri@Sun.COM 	/* Release reference held by kcf_prov_tab_lookup(). */
479*9505SBhargava.Yenduri@Sun.COM 	KCF_PROV_REFRELE(desc);
480*9505SBhargava.Yenduri@Sun.COM 
481*9505SBhargava.Yenduri@Sun.COM 	if (kcf_get_refcnt(desc, B_TRUE) == 0) {
4820Sstevel@tonic-gate 		kcf_free_provider_desc(desc);
4830Sstevel@tonic-gate 	} else {
484*9505SBhargava.Yenduri@Sun.COM 		ASSERT(desc->pd_prov_type != CRYPTO_SW_PROVIDER);
485*9505SBhargava.Yenduri@Sun.COM 		/*
486*9505SBhargava.Yenduri@Sun.COM 		 * We could avoid this if /dev/crypto can proactively
487*9505SBhargava.Yenduri@Sun.COM 		 * remove any holds on us from a dormant PKCS #11 app.
488*9505SBhargava.Yenduri@Sun.COM 		 * For now, a kcfd thread does a periodic check of the
489*9505SBhargava.Yenduri@Sun.COM 		 * provider table for KCF_PROV_UNREGISTERED entries
490*9505SBhargava.Yenduri@Sun.COM 		 * and frees them when refcnt reaches zero.
491*9505SBhargava.Yenduri@Sun.COM 		 */
492*9505SBhargava.Yenduri@Sun.COM 		mutex_enter(&prov_tab_mutex);
493*9505SBhargava.Yenduri@Sun.COM 		kcf_need_provtab_walk = B_TRUE;
494*9505SBhargava.Yenduri@Sun.COM 		mutex_exit(&prov_tab_mutex);
4950Sstevel@tonic-gate 	}
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
4980Sstevel@tonic-gate }
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate /*
5010Sstevel@tonic-gate  * This routine is used to notify the framework that the state of
5020Sstevel@tonic-gate  * a cryptographic provider has changed. Valid state codes are:
5030Sstevel@tonic-gate  *
5040Sstevel@tonic-gate  * CRYPTO_PROVIDER_READY
5050Sstevel@tonic-gate  * 	The provider indicates that it can process more requests. A provider
5060Sstevel@tonic-gate  *	will notify with this event if it previously has notified us with a
5070Sstevel@tonic-gate  *	CRYPTO_PROVIDER_BUSY.
5080Sstevel@tonic-gate  *
5090Sstevel@tonic-gate  * CRYPTO_PROVIDER_BUSY
5100Sstevel@tonic-gate  * 	The provider can not take more requests.
5110Sstevel@tonic-gate  *
5120Sstevel@tonic-gate  * CRYPTO_PROVIDER_FAILED
5130Sstevel@tonic-gate  *	The provider encountered an internal error. The framework will not
5140Sstevel@tonic-gate  * 	be sending any more requests to the provider. The provider may notify
5150Sstevel@tonic-gate  *	with a CRYPTO_PROVIDER_READY, if it is able to recover from the error.
5160Sstevel@tonic-gate  *
5170Sstevel@tonic-gate  * This routine can be called from user or interrupt context.
5180Sstevel@tonic-gate  */
5190Sstevel@tonic-gate void
5200Sstevel@tonic-gate crypto_provider_notification(crypto_kcf_provider_handle_t handle, uint_t state)
5210Sstevel@tonic-gate {
5220Sstevel@tonic-gate 	kcf_provider_desc_t *pd;
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 	/* lookup the provider from the given handle */
5250Sstevel@tonic-gate 	if ((pd = kcf_prov_tab_lookup((crypto_provider_id_t)handle)) == NULL)
5260Sstevel@tonic-gate 		return;
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	mutex_enter(&pd->pd_lock);
5290Sstevel@tonic-gate 
5304373Skrishna 	if (pd->pd_state <= KCF_PROV_VERIFICATION_FAILED)
5314373Skrishna 		goto out;
5324373Skrishna 
5330Sstevel@tonic-gate 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
5340Sstevel@tonic-gate 		cmn_err(CE_WARN, "crypto_provider_notification: "
5350Sstevel@tonic-gate 		    "logical provider (%x) ignored\n", handle);
5360Sstevel@tonic-gate 		goto out;
5370Sstevel@tonic-gate 	}
5380Sstevel@tonic-gate 	switch (state) {
5390Sstevel@tonic-gate 	case CRYPTO_PROVIDER_READY:
5400Sstevel@tonic-gate 		switch (pd->pd_state) {
5410Sstevel@tonic-gate 		case KCF_PROV_BUSY:
5420Sstevel@tonic-gate 			pd->pd_state = KCF_PROV_READY;
5430Sstevel@tonic-gate 			/*
5444912Skrishna 			 * Signal the per-provider taskq threads that they
5454912Skrishna 			 * can start submitting requests.
5460Sstevel@tonic-gate 			 */
5474912Skrishna 			cv_broadcast(&pd->pd_resume_cv);
5480Sstevel@tonic-gate 			break;
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 		case KCF_PROV_FAILED:
5510Sstevel@tonic-gate 			/*
5520Sstevel@tonic-gate 			 * The provider recovered from the error. Let us
5530Sstevel@tonic-gate 			 * use it now.
5540Sstevel@tonic-gate 			 */
5550Sstevel@tonic-gate 			pd->pd_state = KCF_PROV_READY;
5560Sstevel@tonic-gate 			break;
5570Sstevel@tonic-gate 		}
5580Sstevel@tonic-gate 		break;
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	case CRYPTO_PROVIDER_BUSY:
5610Sstevel@tonic-gate 		switch (pd->pd_state) {
5620Sstevel@tonic-gate 		case KCF_PROV_READY:
5630Sstevel@tonic-gate 			pd->pd_state = KCF_PROV_BUSY;
5640Sstevel@tonic-gate 			break;
5650Sstevel@tonic-gate 		}
5660Sstevel@tonic-gate 		break;
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	case CRYPTO_PROVIDER_FAILED:
5690Sstevel@tonic-gate 		/*
5700Sstevel@tonic-gate 		 * We note the failure and return. The per-provider taskq
5714912Skrishna 		 * threads check this flag and start failing the
5720Sstevel@tonic-gate 		 * requests, if it is set. See process_req_hwp() for details.
5730Sstevel@tonic-gate 		 */
5740Sstevel@tonic-gate 		switch (pd->pd_state) {
5750Sstevel@tonic-gate 		case KCF_PROV_READY:
5760Sstevel@tonic-gate 			pd->pd_state = KCF_PROV_FAILED;
5770Sstevel@tonic-gate 			break;
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 		case KCF_PROV_BUSY:
5800Sstevel@tonic-gate 			pd->pd_state = KCF_PROV_FAILED;
5810Sstevel@tonic-gate 			/*
5824912Skrishna 			 * The per-provider taskq threads may be waiting. We
5834912Skrishna 			 * signal them so that they can start failing requests.
5840Sstevel@tonic-gate 			 */
5854912Skrishna 			cv_broadcast(&pd->pd_resume_cv);
5860Sstevel@tonic-gate 			break;
5870Sstevel@tonic-gate 		}
5880Sstevel@tonic-gate 		break;
5890Sstevel@tonic-gate 	}
5900Sstevel@tonic-gate out:
5910Sstevel@tonic-gate 	mutex_exit(&pd->pd_lock);
5920Sstevel@tonic-gate 	KCF_PROV_REFRELE(pd);
5930Sstevel@tonic-gate }
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate /*
5960Sstevel@tonic-gate  * This routine is used to notify the framework the result of
5970Sstevel@tonic-gate  * an asynchronous request handled by a provider. Valid error
5980Sstevel@tonic-gate  * codes are the same as the CRYPTO_* errors defined in common.h.
5990Sstevel@tonic-gate  *
6000Sstevel@tonic-gate  * This routine can be called from user or interrupt context.
6010Sstevel@tonic-gate  */
6020Sstevel@tonic-gate void
6030Sstevel@tonic-gate crypto_op_notification(crypto_req_handle_t handle, int error)
6040Sstevel@tonic-gate {
6050Sstevel@tonic-gate 	kcf_call_type_t ctype;
6060Sstevel@tonic-gate 
6078384SBhargava.Yenduri@Sun.COM 	if (handle == NULL)
6088384SBhargava.Yenduri@Sun.COM 		return;
6098384SBhargava.Yenduri@Sun.COM 
6100Sstevel@tonic-gate 	if ((ctype = GET_REQ_TYPE(handle)) == CRYPTO_SYNCH) {
6110Sstevel@tonic-gate 		kcf_sreq_node_t *sreq = (kcf_sreq_node_t *)handle;
6120Sstevel@tonic-gate 
613*9505SBhargava.Yenduri@Sun.COM 		KCF_PROV_JOB_RELE_STAT(sreq->sn_mp, (error != CRYPTO_SUCCESS));
6140Sstevel@tonic-gate 		kcf_sop_done(sreq, error);
6150Sstevel@tonic-gate 	} else {
6160Sstevel@tonic-gate 		kcf_areq_node_t *areq = (kcf_areq_node_t *)handle;
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 		ASSERT(ctype == CRYPTO_ASYNCH);
619*9505SBhargava.Yenduri@Sun.COM 		KCF_PROV_JOB_RELE_STAT(areq->an_mp, (error != CRYPTO_SUCCESS));
6200Sstevel@tonic-gate 		kcf_aop_done(areq, error);
6210Sstevel@tonic-gate 	}
6220Sstevel@tonic-gate }
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate /*
6250Sstevel@tonic-gate  * This routine is used by software providers to determine
6260Sstevel@tonic-gate  * whether to use KM_SLEEP or KM_NOSLEEP during memory allocation.
6270Sstevel@tonic-gate  * Note that hardware providers can always use KM_SLEEP. So,
6280Sstevel@tonic-gate  * they do not need to call this routine.
6290Sstevel@tonic-gate  *
6300Sstevel@tonic-gate  * This routine can be called from user or interrupt context.
6310Sstevel@tonic-gate  */
6320Sstevel@tonic-gate int
6330Sstevel@tonic-gate crypto_kmflag(crypto_req_handle_t handle)
6340Sstevel@tonic-gate {
6350Sstevel@tonic-gate 	return (REQHNDL2_KMFLAG(handle));
6360Sstevel@tonic-gate }
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate /*
6390Sstevel@tonic-gate  * Process the mechanism info structures specified by the provider
6400Sstevel@tonic-gate  * during registration. A NULL crypto_provider_info_t indicates
6410Sstevel@tonic-gate  * an already initialized provider descriptor.
6420Sstevel@tonic-gate  *
6430Sstevel@tonic-gate  * Mechanisms are not added to the kernel's mechanism table if the
6440Sstevel@tonic-gate  * provider is a logical provider.
6450Sstevel@tonic-gate  *
6460Sstevel@tonic-gate  * Returns CRYPTO_SUCCESS on success, CRYPTO_ARGUMENTS if one
6470Sstevel@tonic-gate  * of the specified mechanisms was malformed, or CRYPTO_HOST_MEMORY
6480Sstevel@tonic-gate  * if the table of mechanisms is full.
6490Sstevel@tonic-gate  */
6500Sstevel@tonic-gate static int
6510Sstevel@tonic-gate init_prov_mechs(crypto_provider_info_t *info, kcf_provider_desc_t *desc)
6520Sstevel@tonic-gate {
6530Sstevel@tonic-gate 	uint_t mech_idx;
6540Sstevel@tonic-gate 	uint_t cleanup_idx;
6550Sstevel@tonic-gate 	int err = CRYPTO_SUCCESS;
6560Sstevel@tonic-gate 	kcf_prov_mech_desc_t *pmd;
6570Sstevel@tonic-gate 	int desc_use_count = 0;
6580Sstevel@tonic-gate 	int mcount = desc->pd_mech_list_count;
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	if (desc->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
6610Sstevel@tonic-gate 		if (info != NULL) {
6620Sstevel@tonic-gate 			ASSERT(info->pi_mechanisms != NULL);
6630Sstevel@tonic-gate 			bcopy(info->pi_mechanisms, desc->pd_mechanisms,
6640Sstevel@tonic-gate 			    sizeof (crypto_mech_info_t) * mcount);
6650Sstevel@tonic-gate 		}
6660Sstevel@tonic-gate 		return (CRYPTO_SUCCESS);
6670Sstevel@tonic-gate 	}
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	/*
6700Sstevel@tonic-gate 	 * Copy the mechanism list from the provider info to the provider
6710Sstevel@tonic-gate 	 * descriptor. desc->pd_mechanisms has an extra crypto_mech_info_t
6720Sstevel@tonic-gate 	 * element if the provider has random_ops since we keep an internal
6730Sstevel@tonic-gate 	 * mechanism, SUN_RANDOM, in this case.
6740Sstevel@tonic-gate 	 */
6750Sstevel@tonic-gate 	if (info != NULL) {
676904Smcpowers 		if (info->pi_ops_vector->co_random_ops != NULL) {
6770Sstevel@tonic-gate 			crypto_mech_info_t *rand_mi;
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 			/*
6800Sstevel@tonic-gate 			 * Need the following check as it is possible to have
6810Sstevel@tonic-gate 			 * a provider that implements just random_ops and has
6820Sstevel@tonic-gate 			 * pi_mechanisms == NULL.
6830Sstevel@tonic-gate 			 */
6840Sstevel@tonic-gate 			if (info->pi_mechanisms != NULL) {
6850Sstevel@tonic-gate 				bcopy(info->pi_mechanisms, desc->pd_mechanisms,
6860Sstevel@tonic-gate 				    sizeof (crypto_mech_info_t) * (mcount - 1));
6870Sstevel@tonic-gate 			}
6880Sstevel@tonic-gate 			rand_mi = &desc->pd_mechanisms[mcount - 1];
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 			bzero(rand_mi, sizeof (crypto_mech_info_t));
6910Sstevel@tonic-gate 			(void) strncpy(rand_mi->cm_mech_name, SUN_RANDOM,
6920Sstevel@tonic-gate 			    CRYPTO_MAX_MECH_NAME);
6930Sstevel@tonic-gate 			rand_mi->cm_func_group_mask = CRYPTO_FG_RANDOM;
6940Sstevel@tonic-gate 		} else {
6950Sstevel@tonic-gate 			ASSERT(info->pi_mechanisms != NULL);
6960Sstevel@tonic-gate 			bcopy(info->pi_mechanisms, desc->pd_mechanisms,
6970Sstevel@tonic-gate 			    sizeof (crypto_mech_info_t) * mcount);
6980Sstevel@tonic-gate 		}
6990Sstevel@tonic-gate 	}
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 	/*
7020Sstevel@tonic-gate 	 * For each mechanism support by the provider, add the provider
7030Sstevel@tonic-gate 	 * to the corresponding KCF mechanism mech_entry chain.
7040Sstevel@tonic-gate 	 */
7050Sstevel@tonic-gate 	for (mech_idx = 0; mech_idx < desc->pd_mech_list_count; mech_idx++) {
7060Sstevel@tonic-gate 		crypto_mech_info_t *mi = &desc->pd_mechanisms[mech_idx];
7070Sstevel@tonic-gate 
7083708Skrishna 		if ((mi->cm_mech_flags & CRYPTO_KEYSIZE_UNIT_IN_BITS) &&
7093708Skrishna 		    (mi->cm_mech_flags & CRYPTO_KEYSIZE_UNIT_IN_BYTES)) {
7100Sstevel@tonic-gate 			err = CRYPTO_ARGUMENTS_BAD;
7110Sstevel@tonic-gate 			break;
7120Sstevel@tonic-gate 		}
7130Sstevel@tonic-gate 
7144072Skrishna 		if (desc->pd_flags & CRYPTO_HASH_NO_UPDATE &&
7154072Skrishna 		    mi->cm_func_group_mask & CRYPTO_FG_DIGEST) {
7164072Skrishna 			/*
7174072Skrishna 			 * We ask the provider to specify the limit
7184072Skrishna 			 * per hash mechanism. But, in practice, a
7194072Skrishna 			 * hardware limitation means all hash mechanisms
7204072Skrishna 			 * will have the same maximum size allowed for
7214072Skrishna 			 * input data. So, we make it a per provider
7224072Skrishna 			 * limit to keep it simple.
7234072Skrishna 			 */
7244072Skrishna 			if (mi->cm_max_input_length == 0) {
7254072Skrishna 				err = CRYPTO_ARGUMENTS_BAD;
7264072Skrishna 				break;
7274072Skrishna 			} else {
7284072Skrishna 				desc->pd_hash_limit = mi->cm_max_input_length;
7294072Skrishna 			}
7304072Skrishna 		}
7314072Skrishna 
7328384SBhargava.Yenduri@Sun.COM 		if ((err = kcf_add_mech_provider(mech_idx, desc, &pmd)) !=
7338384SBhargava.Yenduri@Sun.COM 		    KCF_SUCCESS)
7340Sstevel@tonic-gate 			break;
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 		if (pmd == NULL)
7370Sstevel@tonic-gate 			continue;
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 		/* The provider will be used for this mechanism */
7400Sstevel@tonic-gate 		desc_use_count++;
7410Sstevel@tonic-gate 	}
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 	/*
7442817Smcpowers 	 * Don't allow multiple software providers with disabled mechanisms
7452817Smcpowers 	 * to register. Subsequent enabling of mechanisms will result in
7462817Smcpowers 	 * an unsupported configuration, i.e. multiple software providers
7472817Smcpowers 	 * per mechanism.
7480Sstevel@tonic-gate 	 */
7492817Smcpowers 	if (desc_use_count == 0 && desc->pd_prov_type == CRYPTO_SW_PROVIDER)
7500Sstevel@tonic-gate 		return (CRYPTO_ARGUMENTS_BAD);
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 	if (err == KCF_SUCCESS)
7530Sstevel@tonic-gate 		return (CRYPTO_SUCCESS);
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	/*
7560Sstevel@tonic-gate 	 * An error occurred while adding the mechanism, cleanup
7570Sstevel@tonic-gate 	 * and bail.
7580Sstevel@tonic-gate 	 */
7590Sstevel@tonic-gate 	for (cleanup_idx = 0; cleanup_idx < mech_idx; cleanup_idx++) {
7600Sstevel@tonic-gate 		kcf_remove_mech_provider(
7610Sstevel@tonic-gate 		    desc->pd_mechanisms[cleanup_idx].cm_mech_name, desc);
7620Sstevel@tonic-gate 	}
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	if (err == KCF_MECH_TAB_FULL)
7650Sstevel@tonic-gate 		return (CRYPTO_HOST_MEMORY);
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 	return (CRYPTO_ARGUMENTS_BAD);
7680Sstevel@tonic-gate }
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate /*
7710Sstevel@tonic-gate  * Update routine for kstat. Only privileged users are allowed to
7720Sstevel@tonic-gate  * access this information, since this information is sensitive.
7730Sstevel@tonic-gate  * There are some cryptographic attacks (e.g. traffic analysis)
7740Sstevel@tonic-gate  * which can use this information.
7750Sstevel@tonic-gate  */
7760Sstevel@tonic-gate static int
7770Sstevel@tonic-gate kcf_prov_kstat_update(kstat_t *ksp, int rw)
7780Sstevel@tonic-gate {
7790Sstevel@tonic-gate 	kcf_prov_stats_t *ks_data;
7800Sstevel@tonic-gate 	kcf_provider_desc_t *pd = (kcf_provider_desc_t *)ksp->ks_private;
781*9505SBhargava.Yenduri@Sun.COM 	int i;
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
7840Sstevel@tonic-gate 		return (EACCES);
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 	ks_data = ksp->ks_data;
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 	if (secpolicy_sys_config(CRED(), B_TRUE) != 0) {
7890Sstevel@tonic-gate 		ks_data->ps_ops_total.value.ui64 = 0;
7900Sstevel@tonic-gate 		ks_data->ps_ops_passed.value.ui64 = 0;
7910Sstevel@tonic-gate 		ks_data->ps_ops_failed.value.ui64 = 0;
7920Sstevel@tonic-gate 		ks_data->ps_ops_busy_rval.value.ui64 = 0;
7930Sstevel@tonic-gate 	} else {
794*9505SBhargava.Yenduri@Sun.COM 		uint64_t dtotal, ftotal, btotal;
795*9505SBhargava.Yenduri@Sun.COM 
796*9505SBhargava.Yenduri@Sun.COM 		dtotal = ftotal = btotal = 0;
797*9505SBhargava.Yenduri@Sun.COM 		/* No locking done since an exact count is not required. */
798*9505SBhargava.Yenduri@Sun.COM 		for (i = 0; i < pd->pd_nbins; i++) {
799*9505SBhargava.Yenduri@Sun.COM 			dtotal += pd->pd_percpu_bins[i].kp_ndispatches;
800*9505SBhargava.Yenduri@Sun.COM 			ftotal += pd->pd_percpu_bins[i].kp_nfails;
801*9505SBhargava.Yenduri@Sun.COM 			btotal += pd->pd_percpu_bins[i].kp_nbusy_rval;
802*9505SBhargava.Yenduri@Sun.COM 		}
803*9505SBhargava.Yenduri@Sun.COM 
804*9505SBhargava.Yenduri@Sun.COM 		ks_data->ps_ops_total.value.ui64 = dtotal;
805*9505SBhargava.Yenduri@Sun.COM 		ks_data->ps_ops_failed.value.ui64 = ftotal;
806*9505SBhargava.Yenduri@Sun.COM 		ks_data->ps_ops_busy_rval.value.ui64 = btotal;
807*9505SBhargava.Yenduri@Sun.COM 		ks_data->ps_ops_passed.value.ui64 = dtotal - ftotal - btotal;
8080Sstevel@tonic-gate 	}
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 	return (0);
8110Sstevel@tonic-gate }
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate /*
8150Sstevel@tonic-gate  * Utility routine called from failure paths in crypto_register_provider()
8160Sstevel@tonic-gate  * and from crypto_load_soft_disabled().
8170Sstevel@tonic-gate  */
8180Sstevel@tonic-gate void
8190Sstevel@tonic-gate undo_register_provider(kcf_provider_desc_t *desc, boolean_t remove_prov)
8200Sstevel@tonic-gate {
8210Sstevel@tonic-gate 	uint_t mech_idx;
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 	/* remove the provider from the mechanisms tables */
8240Sstevel@tonic-gate 	for (mech_idx = 0; mech_idx < desc->pd_mech_list_count;
8250Sstevel@tonic-gate 	    mech_idx++) {
8260Sstevel@tonic-gate 		kcf_remove_mech_provider(
8270Sstevel@tonic-gate 		    desc->pd_mechanisms[mech_idx].cm_mech_name, desc);
8280Sstevel@tonic-gate 	}
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 	/* remove provider from providers table */
8310Sstevel@tonic-gate 	if (remove_prov)
8320Sstevel@tonic-gate 		(void) kcf_prov_tab_rem_provider(desc->pd_prov_id);
8330Sstevel@tonic-gate }
8340Sstevel@tonic-gate 
8354373Skrishna static void
8364373Skrishna undo_register_provider_extra(kcf_provider_desc_t *desc)
8374373Skrishna {
8384373Skrishna 	delete_kstat(desc);
8394373Skrishna 	undo_register_provider(desc, B_TRUE);
8404373Skrishna }
8414373Skrishna 
8420Sstevel@tonic-gate /*
8430Sstevel@tonic-gate  * Utility routine called from crypto_load_soft_disabled(). Callers
8440Sstevel@tonic-gate  * should have done a prior undo_register_provider().
8450Sstevel@tonic-gate  */
8460Sstevel@tonic-gate void
8470Sstevel@tonic-gate redo_register_provider(kcf_provider_desc_t *pd)
8480Sstevel@tonic-gate {
8490Sstevel@tonic-gate 	/* process the mechanisms supported by the provider */
8500Sstevel@tonic-gate 	(void) init_prov_mechs(NULL, pd);
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 	/*
8530Sstevel@tonic-gate 	 * Hold provider in providers table. We should not call
8540Sstevel@tonic-gate 	 * kcf_prov_tab_add_provider() here as the provider descriptor
8550Sstevel@tonic-gate 	 * is still valid which means it has an entry in the provider
8560Sstevel@tonic-gate 	 * table.
8570Sstevel@tonic-gate 	 */
8580Sstevel@tonic-gate 	KCF_PROV_REFHOLD(pd);
8590Sstevel@tonic-gate }
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate /*
8620Sstevel@tonic-gate  * Add provider (p1) to another provider's array of providers (p2).
8630Sstevel@tonic-gate  * Hardware and logical providers use this array to cross-reference
8640Sstevel@tonic-gate  * each other.
8650Sstevel@tonic-gate  */
8660Sstevel@tonic-gate static void
8670Sstevel@tonic-gate add_provider_to_array(kcf_provider_desc_t *p1, kcf_provider_desc_t *p2)
8680Sstevel@tonic-gate {
8690Sstevel@tonic-gate 	kcf_provider_list_t *new;
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 	new = kmem_alloc(sizeof (kcf_provider_list_t), KM_SLEEP);
8720Sstevel@tonic-gate 	mutex_enter(&p2->pd_lock);
8730Sstevel@tonic-gate 	new->pl_next = p2->pd_provider_list;
8740Sstevel@tonic-gate 	p2->pd_provider_list = new;
8750Sstevel@tonic-gate 	new->pl_provider = p1;
8760Sstevel@tonic-gate 	mutex_exit(&p2->pd_lock);
8770Sstevel@tonic-gate }
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate /*
8800Sstevel@tonic-gate  * Remove provider (p1) from another provider's array of providers (p2).
8810Sstevel@tonic-gate  * Hardware and logical providers use this array to cross-reference
8820Sstevel@tonic-gate  * each other.
8830Sstevel@tonic-gate  */
8840Sstevel@tonic-gate static void
8850Sstevel@tonic-gate remove_provider_from_array(kcf_provider_desc_t *p1, kcf_provider_desc_t *p2)
8860Sstevel@tonic-gate {
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 	kcf_provider_list_t *pl = NULL, **prev;
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 	mutex_enter(&p2->pd_lock);
8910Sstevel@tonic-gate 	for (pl = p2->pd_provider_list, prev = &p2->pd_provider_list;
8920Sstevel@tonic-gate 	    pl != NULL; prev = &pl->pl_next, pl = pl->pl_next) {
8930Sstevel@tonic-gate 		if (pl->pl_provider == p1) {
8940Sstevel@tonic-gate 			break;
8950Sstevel@tonic-gate 		}
8960Sstevel@tonic-gate 	}
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 	if (p1 == NULL) {
8990Sstevel@tonic-gate 		mutex_exit(&p2->pd_lock);
9000Sstevel@tonic-gate 		return;
9010Sstevel@tonic-gate 	}
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 	/* detach and free kcf_provider_list structure */
9040Sstevel@tonic-gate 	*prev = pl->pl_next;
9050Sstevel@tonic-gate 	kmem_free(pl, sizeof (*pl));
9060Sstevel@tonic-gate 	mutex_exit(&p2->pd_lock);
9070Sstevel@tonic-gate }
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate /*
9100Sstevel@tonic-gate  * Convert an array of logical provider handles (crypto_provider_id)
9110Sstevel@tonic-gate  * stored in a crypto_provider_info structure into an array of provider
9120Sstevel@tonic-gate  * descriptors (kcf_provider_desc_t) attached to a logical provider.
9130Sstevel@tonic-gate  */
9140Sstevel@tonic-gate static void
9150Sstevel@tonic-gate process_logical_providers(crypto_provider_info_t *info, kcf_provider_desc_t *hp)
9160Sstevel@tonic-gate {
9170Sstevel@tonic-gate 	kcf_provider_desc_t *lp;
9180Sstevel@tonic-gate 	crypto_provider_id_t handle;
9190Sstevel@tonic-gate 	int count = info->pi_logical_provider_count;
9200Sstevel@tonic-gate 	int i;
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 	/* add hardware provider to each logical provider */
9230Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
9240Sstevel@tonic-gate 		handle = info->pi_logical_providers[i];
9250Sstevel@tonic-gate 		lp = kcf_prov_tab_lookup((crypto_provider_id_t)handle);
9260Sstevel@tonic-gate 		if (lp == NULL) {
9270Sstevel@tonic-gate 			continue;
9280Sstevel@tonic-gate 		}
9290Sstevel@tonic-gate 		add_provider_to_array(hp, lp);
9302800Skrishna 		hp->pd_flags |= KCF_LPROV_MEMBER;
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 		/*
9330Sstevel@tonic-gate 		 * A hardware provider has to have the provider descriptor of
9340Sstevel@tonic-gate 		 * every logical provider it belongs to, so it can be removed
9350Sstevel@tonic-gate 		 * from the logical provider if the hardware provider
9360Sstevel@tonic-gate 		 * unregisters from the framework.
9370Sstevel@tonic-gate 		 */
9380Sstevel@tonic-gate 		add_provider_to_array(lp, hp);
9390Sstevel@tonic-gate 		KCF_PROV_REFRELE(lp);
9400Sstevel@tonic-gate 	}
9410Sstevel@tonic-gate }
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate /*
9440Sstevel@tonic-gate  * This routine removes a provider from all of the logical or
9450Sstevel@tonic-gate  * hardware providers it belongs to, and frees the provider's
9460Sstevel@tonic-gate  * array of pointers to providers.
9470Sstevel@tonic-gate  */
9480Sstevel@tonic-gate static void
9490Sstevel@tonic-gate remove_provider(kcf_provider_desc_t *pp)
9500Sstevel@tonic-gate {
9510Sstevel@tonic-gate 	kcf_provider_desc_t *p;
9520Sstevel@tonic-gate 	kcf_provider_list_t *e, *next;
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 	mutex_enter(&pp->pd_lock);
9550Sstevel@tonic-gate 	for (e = pp->pd_provider_list; e != NULL; e = next) {
9560Sstevel@tonic-gate 		p = e->pl_provider;
9570Sstevel@tonic-gate 		remove_provider_from_array(pp, p);
9582800Skrishna 		if (p->pd_prov_type == CRYPTO_HW_PROVIDER &&
9592800Skrishna 		    p->pd_provider_list == NULL)
9602800Skrishna 			p->pd_flags &= ~KCF_LPROV_MEMBER;
9610Sstevel@tonic-gate 		next = e->pl_next;
9620Sstevel@tonic-gate 		kmem_free(e, sizeof (*e));
9630Sstevel@tonic-gate 	}
9640Sstevel@tonic-gate 	pp->pd_provider_list = NULL;
9650Sstevel@tonic-gate 	mutex_exit(&pp->pd_lock);
9660Sstevel@tonic-gate }
9674373Skrishna 
9684373Skrishna /*
9694373Skrishna  * Dispatch events as needed for a provider. is_added flag tells
9704373Skrishna  * whether the provider is registering or unregistering.
9714373Skrishna  */
9724373Skrishna void
9734373Skrishna kcf_do_notify(kcf_provider_desc_t *prov_desc, boolean_t is_added)
9744373Skrishna {
9754373Skrishna 	int i;
9764373Skrishna 	crypto_notify_event_change_t ec;
9774373Skrishna 
9784373Skrishna 	ASSERT(prov_desc->pd_state > KCF_PROV_VERIFICATION_FAILED);
9794373Skrishna 
9804373Skrishna 	/*
9814373Skrishna 	 * Inform interested clients of the mechanisms becoming
9824373Skrishna 	 * available/unavailable. We skip this for logical providers
9834373Skrishna 	 * as they do not affect mechanisms.
9844373Skrishna 	 */
9854373Skrishna 	if (prov_desc->pd_prov_type != CRYPTO_LOGICAL_PROVIDER) {
9864373Skrishna 		ec.ec_provider_type = prov_desc->pd_prov_type;
9874373Skrishna 		ec.ec_change = is_added ? CRYPTO_MECH_ADDED :
9884373Skrishna 		    CRYPTO_MECH_REMOVED;
9894373Skrishna 		for (i = 0; i < prov_desc->pd_mech_list_count; i++) {
9904373Skrishna 			/* Skip any mechanisms not allowed by the policy */
9914373Skrishna 			if (is_mech_disabled(prov_desc,
9924373Skrishna 			    prov_desc->pd_mechanisms[i].cm_mech_name))
9934373Skrishna 				continue;
9944373Skrishna 
9954373Skrishna 			(void) strncpy(ec.ec_mech_name,
9964373Skrishna 			    prov_desc->pd_mechanisms[i].cm_mech_name,
9974373Skrishna 			    CRYPTO_MAX_MECH_NAME);
9984373Skrishna 			kcf_walk_ntfylist(CRYPTO_EVENT_MECHS_CHANGED, &ec);
9994373Skrishna 		}
10004373Skrishna 
10014373Skrishna 	}
10024373Skrishna 
10034373Skrishna 	/*
10044373Skrishna 	 * Inform interested clients about the new or departing provider.
10054373Skrishna 	 * In case of a logical provider, we need to notify the event only
10064373Skrishna 	 * for the logical provider and not for the underlying
10074373Skrishna 	 * providers which are known by the KCF_LPROV_MEMBER bit.
10084373Skrishna 	 */
10094373Skrishna 	if (prov_desc->pd_prov_type == CRYPTO_LOGICAL_PROVIDER ||
10104373Skrishna 	    (prov_desc->pd_flags & KCF_LPROV_MEMBER) == 0) {
10114373Skrishna 		kcf_walk_ntfylist(is_added ? CRYPTO_EVENT_PROVIDER_REGISTERED :
10124373Skrishna 		    CRYPTO_EVENT_PROVIDER_UNREGISTERED, prov_desc);
10134373Skrishna 	}
10144373Skrishna }
10154373Skrishna 
10164373Skrishna static void
10174373Skrishna delete_kstat(kcf_provider_desc_t *desc)
10184373Skrishna {
10194373Skrishna 	/* destroy the kstat created for this provider */
10204373Skrishna 	if (desc->pd_kstat != NULL) {
10214373Skrishna 		kcf_provider_desc_t *kspd = desc->pd_kstat->ks_private;
10224373Skrishna 
10234373Skrishna 		/* release reference held by desc->pd_kstat->ks_private */
10244373Skrishna 		ASSERT(desc == kspd);
10254373Skrishna 		kstat_delete(kspd->pd_kstat);
10264373Skrishna 		desc->pd_kstat = NULL;
10274373Skrishna 		KCF_PROV_REFRELE(kspd);
10284373Skrishna 	}
10294373Skrishna }
1030