xref: /illumos-gate/usr/src/uts/common/crypto/core/kcf_mech_tabs.c (revision bfed486ad8de8b8ebc6345a8e10accae08bf2f45)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/types.h>
27 #include <sys/sunddi.h>
28 #include <sys/errno.h>
29 #include <sys/disp.h>
30 #include <sys/modctl.h>
31 #include <sys/modhash.h>
32 #include <sys/crypto/common.h>
33 #include <sys/crypto/api.h>
34 #include <sys/crypto/impl.h>
35 
36 /* Cryptographic mechanisms tables and their access functions */
37 
38 /*
39  * Internal numbers assigned to mechanisms are coded as follows:
40  *
41  * +----------------+----------------+
42  * | mech. class    | mech. index    |
43  * <--- 32-bits --->+<--- 32-bits --->
44  *
45  * the mech_class identifies the table the mechanism belongs to.
46  * mech_index  is the index for that mechanism in the table.
47  * A mechanism belongs to exactly 1 table.
48  * The tables are:
49  * . digest_mechs_tab[] for the msg digest mechs.
50  * . cipher_mechs_tab[] for encrypt/decrypt and wrap/unwrap mechs.
51  * . mac_mechs_tab[] for MAC mechs.
52  * . sign_mechs_tab[] for sign & verify mechs.
53  * . keyops_mechs_tab[] for key/key pair generation, and key derivation.
54  * . misc_mechs_tab[] for mechs that don't belong to any of the above.
55  *
56  * There are no holes in the tables.
57  */
58 
59 /*
60  * Locking conventions:
61  * --------------------
62  * A global mutex, kcf_mech_tabs_lock, serializes writes to the
63  * mechanism table via kcf_create_mech_entry().
64  *
65  * A mutex is associated with every entry of the tables.
66  * The mutex is acquired whenever the entry is accessed for
67  * 1) retrieving the mech_id (comparing the mech name)
68  * 2) finding a provider for an xxx_init() or atomic operation.
69  * 3) altering the mechs entry to add or remove a provider.
70  *
71  * In 2), after a provider is chosen, its prov_desc is held and the
72  * entry's mutex must be dropped. The provider's working function (SPI) is
73  * called outside the mech_entry's mutex.
74  *
75  * The number of providers for a particular mechanism is not expected to be
76  * long enough to justify the cost of using rwlocks, so the per-mechanism
77  * entry mutex won't be very *hot*.
78  *
79  * When both kcf_mech_tabs_lock and a mech_entry mutex need to be held,
80  * kcf_mech_tabs_lock must always be acquired first.
81  *
82  */
83 
84 		/* Mechanisms tables */
85 
86 
87 /* RFE 4687834 Will deal with the extensibility of these tables later */
88 
89 kcf_mech_entry_t kcf_digest_mechs_tab[KCF_MAXDIGEST];
90 kcf_mech_entry_t kcf_cipher_mechs_tab[KCF_MAXCIPHER];
91 kcf_mech_entry_t kcf_mac_mechs_tab[KCF_MAXMAC];
92 kcf_mech_entry_t kcf_sign_mechs_tab[KCF_MAXSIGN];
93 kcf_mech_entry_t kcf_keyops_mechs_tab[KCF_MAXKEYOPS];
94 kcf_mech_entry_t kcf_misc_mechs_tab[KCF_MAXMISC];
95 
96 kcf_mech_entry_tab_t kcf_mech_tabs_tab[KCF_LAST_OPSCLASS + 1] = {
97 	{0, NULL},				/* No class zero */
98 	{KCF_MAXDIGEST, kcf_digest_mechs_tab},
99 	{KCF_MAXCIPHER, kcf_cipher_mechs_tab},
100 	{KCF_MAXMAC, kcf_mac_mechs_tab},
101 	{KCF_MAXSIGN, kcf_sign_mechs_tab},
102 	{KCF_MAXKEYOPS, kcf_keyops_mechs_tab},
103 	{KCF_MAXMISC, kcf_misc_mechs_tab}
104 };
105 
106 /*
107  * Per-algorithm internal threasholds for the minimum input size of before
108  * offloading to hardware provider.
109  * Dispatching a crypto operation  to a hardware provider entails paying the
110  * cost of an additional context switch.  Measurments with Sun Accelerator 4000
111  * shows that 512-byte jobs or smaller are better handled in software.
112  * There is room for refinement here.
113  *
114  */
115 int kcf_md5_threshold = 512;
116 int kcf_sha1_threshold = 512;
117 int kcf_des_threshold = 512;
118 int kcf_des3_threshold = 512;
119 int kcf_aes_threshold = 512;
120 int kcf_bf_threshold = 512;
121 int kcf_rc4_threshold = 512;
122 
123 kmutex_t kcf_mech_tabs_lock;
124 static uint32_t kcf_gen_swprov = 0;
125 
126 int kcf_mech_hash_size = 256;
127 mod_hash_t *kcf_mech_hash;	/* mech name to id hash */
128 
129 static crypto_mech_type_t
130 kcf_mech_hash_find(char *mechname)
131 {
132 	mod_hash_val_t hv;
133 	crypto_mech_type_t mt;
134 
135 	mt = CRYPTO_MECH_INVALID;
136 	if (mod_hash_find(kcf_mech_hash, (mod_hash_key_t)mechname, &hv) == 0) {
137 		mt = *(crypto_mech_type_t *)hv;
138 		ASSERT(mt != CRYPTO_MECH_INVALID);
139 	}
140 
141 	return (mt);
142 }
143 
144 /*
145  * kcf_init_mech_tabs()
146  *
147  * Called by the misc/kcf's _init() routine to initialize the tables
148  * of mech_entry's.
149  */
150 void
151 kcf_init_mech_tabs()
152 {
153 	int i, max;
154 	kcf_ops_class_t class;
155 	kcf_mech_entry_t *me_tab;
156 
157 	/* Initializes the mutex locks. */
158 
159 	mutex_init(&kcf_mech_tabs_lock, NULL, MUTEX_DEFAULT, NULL);
160 
161 	/* Then the pre-defined mechanism entries */
162 
163 	/* Two digests */
164 	(void) strncpy(kcf_digest_mechs_tab[0].me_name, SUN_CKM_MD5,
165 	    CRYPTO_MAX_MECH_NAME);
166 	kcf_digest_mechs_tab[0].me_threshold = kcf_md5_threshold;
167 
168 	(void) strncpy(kcf_digest_mechs_tab[1].me_name, SUN_CKM_SHA1,
169 	    CRYPTO_MAX_MECH_NAME);
170 	kcf_digest_mechs_tab[1].me_threshold = kcf_sha1_threshold;
171 
172 	/* The symmetric ciphers in various modes */
173 	(void) strncpy(kcf_cipher_mechs_tab[0].me_name, SUN_CKM_DES_CBC,
174 	    CRYPTO_MAX_MECH_NAME);
175 	kcf_cipher_mechs_tab[0].me_threshold = kcf_des_threshold;
176 
177 	(void) strncpy(kcf_cipher_mechs_tab[1].me_name, SUN_CKM_DES3_CBC,
178 	    CRYPTO_MAX_MECH_NAME);
179 	kcf_cipher_mechs_tab[1].me_threshold = kcf_des3_threshold;
180 
181 	(void) strncpy(kcf_cipher_mechs_tab[2].me_name, SUN_CKM_DES_ECB,
182 	    CRYPTO_MAX_MECH_NAME);
183 	kcf_cipher_mechs_tab[2].me_threshold = kcf_des_threshold;
184 
185 	(void) strncpy(kcf_cipher_mechs_tab[3].me_name, SUN_CKM_DES3_ECB,
186 	    CRYPTO_MAX_MECH_NAME);
187 	kcf_cipher_mechs_tab[3].me_threshold = kcf_des3_threshold;
188 
189 	(void) strncpy(kcf_cipher_mechs_tab[4].me_name, SUN_CKM_BLOWFISH_CBC,
190 	    CRYPTO_MAX_MECH_NAME);
191 	kcf_cipher_mechs_tab[4].me_threshold = kcf_bf_threshold;
192 
193 	(void) strncpy(kcf_cipher_mechs_tab[5].me_name, SUN_CKM_BLOWFISH_ECB,
194 	    CRYPTO_MAX_MECH_NAME);
195 	kcf_cipher_mechs_tab[5].me_threshold = kcf_bf_threshold;
196 
197 	(void) strncpy(kcf_cipher_mechs_tab[6].me_name, SUN_CKM_AES_CBC,
198 	    CRYPTO_MAX_MECH_NAME);
199 	kcf_cipher_mechs_tab[6].me_threshold = kcf_aes_threshold;
200 
201 	(void) strncpy(kcf_cipher_mechs_tab[7].me_name, SUN_CKM_AES_ECB,
202 	    CRYPTO_MAX_MECH_NAME);
203 	kcf_cipher_mechs_tab[7].me_threshold = kcf_aes_threshold;
204 
205 	(void) strncpy(kcf_cipher_mechs_tab[8].me_name, SUN_CKM_RC4,
206 	    CRYPTO_MAX_MECH_NAME);
207 	kcf_cipher_mechs_tab[8].me_threshold = kcf_rc4_threshold;
208 
209 
210 	/* 4 HMACs */
211 	(void) strncpy(kcf_mac_mechs_tab[0].me_name, SUN_CKM_MD5_HMAC,
212 	    CRYPTO_MAX_MECH_NAME);
213 	kcf_mac_mechs_tab[0].me_threshold = kcf_md5_threshold;
214 
215 	(void) strncpy(kcf_mac_mechs_tab[1].me_name, SUN_CKM_MD5_HMAC_GENERAL,
216 	    CRYPTO_MAX_MECH_NAME);
217 	kcf_mac_mechs_tab[1].me_threshold = kcf_md5_threshold;
218 
219 	(void) strncpy(kcf_mac_mechs_tab[2].me_name, SUN_CKM_SHA1_HMAC,
220 	    CRYPTO_MAX_MECH_NAME);
221 	kcf_mac_mechs_tab[2].me_threshold = kcf_sha1_threshold;
222 
223 	(void) strncpy(kcf_mac_mechs_tab[3].me_name, SUN_CKM_SHA1_HMAC_GENERAL,
224 	    CRYPTO_MAX_MECH_NAME);
225 	kcf_mac_mechs_tab[3].me_threshold = kcf_sha1_threshold;
226 
227 
228 	/* 1 random number generation pseudo mechanism */
229 	(void) strncpy(kcf_misc_mechs_tab[0].me_name, SUN_RANDOM,
230 	    CRYPTO_MAX_MECH_NAME);
231 
232 	kcf_mech_hash = mod_hash_create_strhash("kcf mech2id hash",
233 	    kcf_mech_hash_size, mod_hash_null_valdtor);
234 
235 	for (class = KCF_FIRST_OPSCLASS; class <= KCF_LAST_OPSCLASS; class++) {
236 		max = kcf_mech_tabs_tab[class].met_size;
237 		me_tab = kcf_mech_tabs_tab[class].met_tab;
238 		for (i = 0; i < max; i++) {
239 			mutex_init(&(me_tab[i].me_mutex), NULL,
240 			    MUTEX_DEFAULT, NULL);
241 			if (me_tab[i].me_name[0] != 0) {
242 				me_tab[i].me_mechid = KCF_MECHID(class, i);
243 				(void) mod_hash_insert(kcf_mech_hash,
244 				    (mod_hash_key_t)me_tab[i].me_name,
245 				    (mod_hash_val_t)&(me_tab[i].me_mechid));
246 			}
247 		}
248 	}
249 }
250 
251 /*
252  * kcf_create_mech_entry()
253  *
254  * Arguments:
255  *	. The class of mechanism.
256  *	. the name of the new mechanism.
257  *
258  * Description:
259  *	Creates a new mech_entry for a mechanism not yet known to the
260  *	framework.
261  *	This routine is called by kcf_add_mech_provider, which is
262  *	in turn invoked for each mechanism supported by a provider.
263  *	The'class' argument depends on the crypto_func_group_t bitmask
264  *	in the registering provider's mech_info struct for this mechanism.
265  *	When there is ambiguity in the mapping between the crypto_func_group_t
266  *	and a class (dual ops, ...) the KCF_MISC_CLASS should be used.
267  *
268  * Context:
269  *	User context only.
270  *
271  * Returns:
272  *	KCF_INVALID_MECH_CLASS or KCF_INVALID_MECH_NAME if the class or
273  *	the mechname is bogus.
274  *	KCF_MECH_TAB_FULL when there is no room left in the mech. tabs.
275  *	KCF_SUCCESS otherwise.
276  */
277 static int
278 kcf_create_mech_entry(kcf_ops_class_t class, char *mechname)
279 {
280 	crypto_mech_type_t mt;
281 	kcf_mech_entry_t *me_tab;
282 	int i = 0, size;
283 
284 	if ((class < KCF_FIRST_OPSCLASS) || (class > KCF_LAST_OPSCLASS))
285 		return (KCF_INVALID_MECH_CLASS);
286 
287 	if ((mechname == NULL) || (mechname[0] == 0))
288 		return (KCF_INVALID_MECH_NAME);
289 	/*
290 	 * First check if the mechanism is already in one of the tables.
291 	 * The mech_entry could be in another class.
292 	 */
293 	mutex_enter(&kcf_mech_tabs_lock);
294 	mt = kcf_mech_hash_find(mechname);
295 	if (mt != CRYPTO_MECH_INVALID) {
296 		/* Nothing to do, regardless the suggested class. */
297 		mutex_exit(&kcf_mech_tabs_lock);
298 		return (KCF_SUCCESS);
299 	}
300 	/* Now take the next unused mech entry in the class's tab */
301 	me_tab = kcf_mech_tabs_tab[class].met_tab;
302 	size = kcf_mech_tabs_tab[class].met_size;
303 
304 	while (i < size) {
305 		mutex_enter(&(me_tab[i].me_mutex));
306 		if (me_tab[i].me_name[0] == 0) {
307 			/* Found an empty spot */
308 			(void) strncpy(me_tab[i].me_name, mechname,
309 			    CRYPTO_MAX_MECH_NAME);
310 			me_tab[i].me_name[CRYPTO_MAX_MECH_NAME-1] = '\0';
311 			me_tab[i].me_mechid = KCF_MECHID(class, i);
312 			/*
313 			 * No a-priori information about the new mechanism, so
314 			 * the threshold is set to zero.
315 			 */
316 			me_tab[i].me_threshold = 0;
317 
318 			mutex_exit(&(me_tab[i].me_mutex));
319 			/* Add the new mechanism to the hash table */
320 			(void) mod_hash_insert(kcf_mech_hash,
321 			    (mod_hash_key_t)me_tab[i].me_name,
322 			    (mod_hash_val_t)&(me_tab[i].me_mechid));
323 			break;
324 		}
325 		mutex_exit(&(me_tab[i].me_mutex));
326 		i++;
327 	}
328 
329 	mutex_exit(&kcf_mech_tabs_lock);
330 
331 	if (i == size) {
332 		return (KCF_MECH_TAB_FULL);
333 	}
334 
335 	return (KCF_SUCCESS);
336 }
337 
338 /*
339  * kcf_add_mech_provider()
340  *
341  * Arguments:
342  *	. An index in to  the provider mechanism array
343  *      . A pointer to the provider descriptor
344  *	. A storage for the kcf_prov_mech_desc_t the entry was added at.
345  *
346  * Description:
347  *      Adds  a new provider of a mechanism to the mechanism's mech_entry
348  *	chain.
349  *
350  * Context:
351  *      User context only.
352  *
353  * Returns
354  *      KCF_SUCCESS on success
355  *      KCF_MECH_TAB_FULL otherwise.
356  */
357 int
358 kcf_add_mech_provider(short mech_indx,
359     kcf_provider_desc_t *prov_desc, kcf_prov_mech_desc_t **pmdpp)
360 {
361 	int error;
362 	kcf_mech_entry_t *mech_entry;
363 	crypto_mech_info_t *mech_info;
364 	crypto_mech_type_t kcf_mech_type, mt;
365 	kcf_prov_mech_desc_t *prov_mech, *prov_mech2;
366 	crypto_func_group_t simple_fg_mask, dual_fg_mask;
367 	crypto_mech_info_t *dmi;
368 	crypto_mech_info_list_t *mil, *mil2;
369 	kcf_mech_entry_t *me;
370 	int i;
371 
372 	ASSERT(prov_desc->pd_prov_type != CRYPTO_LOGICAL_PROVIDER);
373 
374 	mech_info = &prov_desc->pd_mechanisms[mech_indx];
375 	/*
376 	 * Do not use the provider for the mechanism if
377 	 * policy does not allow it.
378 	 */
379 	if (is_mech_disabled(prov_desc, mech_info->cm_mech_name)) {
380 		*pmdpp = NULL;
381 		return (KCF_SUCCESS);
382 	}
383 
384 	/*
385 	 * A mechanism belongs to exactly one mechanism table.
386 	 * Find the class corresponding to the function group flag of
387 	 * the mechanism.
388 	 */
389 	kcf_mech_type = kcf_mech_hash_find(mech_info->cm_mech_name);
390 	if (kcf_mech_type == CRYPTO_MECH_INVALID) {
391 		crypto_func_group_t fg = mech_info->cm_func_group_mask;
392 		kcf_ops_class_t class;
393 
394 		if (fg & CRYPTO_FG_DIGEST || fg & CRYPTO_FG_DIGEST_ATOMIC)
395 			class = KCF_DIGEST_CLASS;
396 		else if (fg & CRYPTO_FG_ENCRYPT || fg & CRYPTO_FG_DECRYPT ||
397 		    fg & CRYPTO_FG_ENCRYPT_ATOMIC ||
398 		    fg & CRYPTO_FG_DECRYPT_ATOMIC)
399 			class = KCF_CIPHER_CLASS;
400 		else if (fg & CRYPTO_FG_MAC || fg & CRYPTO_FG_MAC_ATOMIC)
401 			class = KCF_MAC_CLASS;
402 		else if (fg & CRYPTO_FG_SIGN || fg & CRYPTO_FG_VERIFY ||
403 		    fg & CRYPTO_FG_SIGN_ATOMIC ||
404 		    fg & CRYPTO_FG_VERIFY_ATOMIC ||
405 		    fg & CRYPTO_FG_SIGN_RECOVER ||
406 		    fg & CRYPTO_FG_VERIFY_RECOVER)
407 			class = KCF_SIGN_CLASS;
408 		else if (fg & CRYPTO_FG_GENERATE ||
409 		    fg & CRYPTO_FG_GENERATE_KEY_PAIR ||
410 		    fg & CRYPTO_FG_WRAP || fg & CRYPTO_FG_UNWRAP ||
411 		    fg & CRYPTO_FG_DERIVE)
412 			class = KCF_KEYOPS_CLASS;
413 		else
414 			class = KCF_MISC_CLASS;
415 
416 		/*
417 		 * Attempt to create a new mech_entry for the specified
418 		 * mechanism. kcf_create_mech_entry() can handle the case
419 		 * where such an entry already exists.
420 		 */
421 		if ((error = kcf_create_mech_entry(class,
422 		    mech_info->cm_mech_name)) != KCF_SUCCESS) {
423 			return (error);
424 		}
425 		/* get the KCF mech type that was assigned to the mechanism */
426 		kcf_mech_type = kcf_mech_hash_find(mech_info->cm_mech_name);
427 		ASSERT(kcf_mech_type != CRYPTO_MECH_INVALID);
428 	}
429 
430 	error = kcf_get_mech_entry(kcf_mech_type, &mech_entry);
431 	ASSERT(error == KCF_SUCCESS);
432 
433 	/* allocate and initialize new kcf_prov_mech_desc */
434 	prov_mech = kmem_zalloc(sizeof (kcf_prov_mech_desc_t), KM_SLEEP);
435 	bcopy(mech_info, &prov_mech->pm_mech_info, sizeof (crypto_mech_info_t));
436 	prov_mech->pm_prov_desc = prov_desc;
437 	prov_desc->pd_mech_indx[KCF_MECH2CLASS(kcf_mech_type)]
438 	    [KCF_MECH2INDEX(kcf_mech_type)] = mech_indx;
439 
440 	KCF_PROV_REFHOLD(prov_desc);
441 	KCF_PROV_IREFHOLD(prov_desc);
442 
443 	dual_fg_mask = mech_info->cm_func_group_mask & CRYPTO_FG_DUAL_MASK;
444 
445 	if (dual_fg_mask == ((crypto_func_group_t)0))
446 		goto add_entry;
447 
448 	simple_fg_mask = mech_info->cm_func_group_mask &
449 	    CRYPTO_FG_SIMPLEOP_MASK | CRYPTO_FG_RANDOM;
450 
451 	for (i = 0; i < prov_desc->pd_mech_list_count; i++) {
452 		dmi = &prov_desc->pd_mechanisms[i];
453 
454 		/* skip self */
455 		if (dmi->cm_mech_number == mech_info->cm_mech_number)
456 			continue;
457 
458 		/* skip if policy doesn't allow mechanism */
459 		if (is_mech_disabled(prov_desc, dmi->cm_mech_name))
460 			continue;
461 
462 		/* skip if not a dual operation mechanism */
463 		if (!(dmi->cm_func_group_mask & dual_fg_mask) ||
464 		    (dmi->cm_func_group_mask & simple_fg_mask))
465 			continue;
466 
467 		mt = kcf_mech_hash_find(dmi->cm_mech_name);
468 		if (mt == CRYPTO_MECH_INVALID)
469 			continue;
470 
471 		if (kcf_get_mech_entry(mt, &me) != KCF_SUCCESS)
472 			continue;
473 
474 		mil = kmem_zalloc(sizeof (*mil), KM_SLEEP);
475 		mil2 = kmem_zalloc(sizeof (*mil2), KM_SLEEP);
476 
477 		/*
478 		 * Ignore hard-coded entries in the mech table
479 		 * if the provider hasn't registered.
480 		 */
481 		mutex_enter(&me->me_mutex);
482 		if (me->me_hw_prov_chain == NULL && me->me_sw_prov == NULL) {
483 			mutex_exit(&me->me_mutex);
484 			kmem_free(mil, sizeof (*mil));
485 			kmem_free(mil2, sizeof (*mil2));
486 			continue;
487 		}
488 
489 		/*
490 		 * Add other dual mechanisms that have registered
491 		 * with the framework to this mechanism's
492 		 * cross-reference list.
493 		 */
494 		mil->ml_mech_info = *dmi; /* struct assignment */
495 		mil->ml_kcf_mechid = mt;
496 
497 		/* add to head of list */
498 		mil->ml_next = prov_mech->pm_mi_list;
499 		prov_mech->pm_mi_list = mil;
500 
501 		if (prov_desc->pd_prov_type == CRYPTO_HW_PROVIDER)
502 			prov_mech2 = me->me_hw_prov_chain;
503 		else
504 			prov_mech2 = me->me_sw_prov;
505 
506 		if (prov_mech2 == NULL) {
507 			kmem_free(mil2, sizeof (*mil2));
508 			mutex_exit(&me->me_mutex);
509 			continue;
510 		}
511 
512 		/*
513 		 * Update all other cross-reference lists by
514 		 * adding this new mechanism.
515 		 */
516 		while (prov_mech2 != NULL) {
517 			if (prov_mech2->pm_prov_desc == prov_desc) {
518 				/* struct assignment */
519 				mil2->ml_mech_info = *mech_info;
520 				mil2->ml_kcf_mechid = kcf_mech_type;
521 
522 				/* add to head of list */
523 				mil2->ml_next = prov_mech2->pm_mi_list;
524 				prov_mech2->pm_mi_list = mil2;
525 				break;
526 			}
527 			prov_mech2 = prov_mech2->pm_next;
528 		}
529 		if (prov_mech2 == NULL)
530 			kmem_free(mil2, sizeof (*mil2));
531 
532 		mutex_exit(&me->me_mutex);
533 	}
534 
535 add_entry:
536 	/*
537 	 * Add new kcf_prov_mech_desc at the front of HW providers
538 	 * chain.
539 	 */
540 	switch (prov_desc->pd_prov_type) {
541 
542 	case CRYPTO_HW_PROVIDER:
543 		mutex_enter(&mech_entry->me_mutex);
544 		prov_mech->pm_me = mech_entry;
545 		prov_mech->pm_next = mech_entry->me_hw_prov_chain;
546 		mech_entry->me_hw_prov_chain = prov_mech;
547 		mech_entry->me_num_hwprov++;
548 		mutex_exit(&mech_entry->me_mutex);
549 		break;
550 
551 	case CRYPTO_SW_PROVIDER:
552 		mutex_enter(&mech_entry->me_mutex);
553 		if (mech_entry->me_sw_prov != NULL) {
554 			/*
555 			 * There is already a SW provider for this mechanism.
556 			 * Since we allow only one SW provider per mechanism,
557 			 * report this condition.
558 			 */
559 			cmn_err(CE_WARN, "The cryptographic software provider "
560 			    "\"%s\" will not be used for %s. The provider "
561 			    "\"%s\" will be used for this mechanism "
562 			    "instead.", prov_desc->pd_description,
563 			    mech_info->cm_mech_name,
564 			    mech_entry->me_sw_prov->pm_prov_desc->
565 			    pd_description);
566 			KCF_PROV_REFRELE(prov_desc);
567 			kmem_free(prov_mech, sizeof (kcf_prov_mech_desc_t));
568 			prov_mech = NULL;
569 		} else {
570 			/*
571 			 * Set the provider as the software provider for
572 			 * this mechanism.
573 			 */
574 			mech_entry->me_sw_prov = prov_mech;
575 
576 			/* We'll wrap around after 4 billion registrations! */
577 			mech_entry->me_gen_swprov = kcf_gen_swprov++;
578 		}
579 		mutex_exit(&mech_entry->me_mutex);
580 		break;
581 	}
582 
583 	*pmdpp = prov_mech;
584 
585 	return (KCF_SUCCESS);
586 }
587 
588 /*
589  * kcf_remove_mech_provider()
590  *
591  * Arguments:
592  *      . mech_name: the name of the mechanism.
593  *      . prov_desc: The provider descriptor
594  *
595  * Description:
596  *      Removes a provider from chain of provider descriptors.
597  *	The provider is made unavailable to kernel consumers for the specified
598  *	mechanism.
599  *
600  * Context:
601  *      User context only.
602  */
603 void
604 kcf_remove_mech_provider(char *mech_name, kcf_provider_desc_t *prov_desc)
605 {
606 	crypto_mech_type_t mech_type;
607 	kcf_prov_mech_desc_t *prov_mech, *prov_chain;
608 	kcf_prov_mech_desc_t **prev_entry_next;
609 	kcf_mech_entry_t *mech_entry;
610 	crypto_mech_info_list_t *mil, *mil2, *next, **prev_next;
611 
612 	ASSERT(prov_desc->pd_prov_type != CRYPTO_LOGICAL_PROVIDER);
613 
614 	/* get the KCF mech type that was assigned to the mechanism */
615 	if ((mech_type = kcf_mech_hash_find(mech_name)) ==
616 	    CRYPTO_MECH_INVALID) {
617 		/*
618 		 * Provider was not allowed for this mech due to policy or
619 		 * configuration.
620 		 */
621 		return;
622 	}
623 
624 	/* get a ptr to the mech_entry that was created */
625 	if (kcf_get_mech_entry(mech_type, &mech_entry) != KCF_SUCCESS) {
626 		/*
627 		 * Provider was not allowed for this mech due to policy or
628 		 * configuration.
629 		 */
630 		return;
631 	}
632 
633 	mutex_enter(&mech_entry->me_mutex);
634 
635 	switch (prov_desc->pd_prov_type) {
636 
637 	case CRYPTO_HW_PROVIDER:
638 		/* find the provider in the mech_entry chain */
639 		prev_entry_next = &mech_entry->me_hw_prov_chain;
640 		prov_mech = mech_entry->me_hw_prov_chain;
641 		while (prov_mech != NULL &&
642 		    prov_mech->pm_prov_desc != prov_desc) {
643 			prev_entry_next = &prov_mech->pm_next;
644 			prov_mech = prov_mech->pm_next;
645 		}
646 
647 		if (prov_mech == NULL) {
648 			/* entry not found, simply return */
649 			mutex_exit(&mech_entry->me_mutex);
650 			return;
651 		}
652 
653 		/* remove provider entry from mech_entry chain */
654 		*prev_entry_next = prov_mech->pm_next;
655 		ASSERT(mech_entry->me_num_hwprov > 0);
656 		mech_entry->me_num_hwprov--;
657 		break;
658 
659 	case CRYPTO_SW_PROVIDER:
660 		if (mech_entry->me_sw_prov == NULL ||
661 		    mech_entry->me_sw_prov->pm_prov_desc != prov_desc) {
662 			/* not the software provider for this mechanism */
663 			mutex_exit(&mech_entry->me_mutex);
664 			return;
665 		}
666 		prov_mech = mech_entry->me_sw_prov;
667 		mech_entry->me_sw_prov = NULL;
668 		break;
669 	}
670 
671 	mutex_exit(&mech_entry->me_mutex);
672 
673 	/* Free the dual ops cross-reference lists  */
674 	mil = prov_mech->pm_mi_list;
675 	while (mil != NULL) {
676 		next = mil->ml_next;
677 		if (kcf_get_mech_entry(mil->ml_kcf_mechid,
678 		    &mech_entry) != KCF_SUCCESS) {
679 			mil = next;
680 			continue;
681 		}
682 
683 		mutex_enter(&mech_entry->me_mutex);
684 		if (prov_desc->pd_prov_type == CRYPTO_HW_PROVIDER)
685 			prov_chain = mech_entry->me_hw_prov_chain;
686 		else
687 			prov_chain = mech_entry->me_sw_prov;
688 
689 		while (prov_chain != NULL) {
690 			if (prov_chain->pm_prov_desc == prov_desc) {
691 				prev_next = &prov_chain->pm_mi_list;
692 				mil2 = prov_chain->pm_mi_list;
693 				while (mil2 != NULL &&
694 				    mil2->ml_kcf_mechid != mech_type) {
695 					prev_next = &mil2->ml_next;
696 					mil2 = mil2->ml_next;
697 				}
698 				if (mil2 != NULL) {
699 					*prev_next = mil2->ml_next;
700 					kmem_free(mil2, sizeof (*mil2));
701 				}
702 				break;
703 			}
704 			prov_chain = prov_chain->pm_next;
705 		}
706 
707 		mutex_exit(&mech_entry->me_mutex);
708 		kmem_free(mil, sizeof (crypto_mech_info_list_t));
709 		mil = next;
710 	}
711 
712 	/* free entry  */
713 	KCF_PROV_REFRELE(prov_mech->pm_prov_desc);
714 	KCF_PROV_IREFRELE(prov_mech->pm_prov_desc);
715 	kmem_free(prov_mech, sizeof (kcf_prov_mech_desc_t));
716 }
717 
718 /*
719  * kcf_get_mech_entry()
720  *
721  * Arguments:
722  *      . The framework mechanism type
723  *      . Storage for the mechanism entry
724  *
725  * Description:
726  *      Retrieves the mechanism entry for the mech.
727  *
728  * Context:
729  *      User and interrupt contexts.
730  *
731  * Returns:
732  *      KCF_MECHANISM_XXX appropriate error code.
733  *      KCF_SUCCESS otherwise.
734  */
735 int
736 kcf_get_mech_entry(crypto_mech_type_t mech_type, kcf_mech_entry_t **mep)
737 {
738 	kcf_ops_class_t		class;
739 	int			index;
740 	kcf_mech_entry_tab_t	*me_tab;
741 
742 	ASSERT(mep != NULL);
743 
744 	class = KCF_MECH2CLASS(mech_type);
745 
746 	if ((class < KCF_FIRST_OPSCLASS) || (class > KCF_LAST_OPSCLASS)) {
747 		/* the caller won't need to know it's an invalid class */
748 		return (KCF_INVALID_MECH_NUMBER);
749 	}
750 
751 	me_tab = &kcf_mech_tabs_tab[class];
752 	index = KCF_MECH2INDEX(mech_type);
753 
754 	if ((index < 0) || (index >= me_tab->met_size)) {
755 		return (KCF_INVALID_MECH_NUMBER);
756 	}
757 
758 	*mep = &((me_tab->met_tab)[index]);
759 
760 	return (KCF_SUCCESS);
761 }
762 
763 /*
764  * Returns TRUE if the provider is usable and the MOD_NOAUTOUNLOAD flag
765  * is set in the modctl structure.
766  */
767 static boolean_t
768 auto_unload_flag_set(kcf_prov_mech_desc_t *pm)
769 {
770 	kcf_provider_desc_t *pd;
771 	struct modctl *mp;
772 	boolean_t ret = B_FALSE;
773 
774 	if (pm != NULL) {
775 		pd = pm->pm_prov_desc;
776 		KCF_PROV_REFHOLD(pd);
777 
778 		if (KCF_IS_PROV_USABLE(pd)) {
779 			mp = pd->pd_mctlp;
780 			if (mp->mod_loadflags & MOD_NOAUTOUNLOAD) {
781 				ret = B_TRUE;
782 			}
783 		}
784 		KCF_PROV_REFRELE(pd);
785 	}
786 
787 	return (ret);
788 }
789 
790 /*
791  * Lookup the hash table for an entry that matches the mechname.
792  * If there are no hardware or software providers for the mechanism,
793  * but there is an unloaded software provider, this routine will attempt
794  * to load it.
795  *
796  * If the MOD_NOAUTOUNLOAD flag is not set, a software provider is
797  * in constant danger of being unloaded.  For consumers that call
798  * crypto_mech2id() only once, the provider will not be reloaded
799  * if it becomes unloaded.  If a provider gets loaded elsewhere
800  * without the MOD_NOAUTOUNLOAD flag being set, we set it now.
801  */
802 crypto_mech_type_t
803 crypto_mech2id_common(char *mechname, boolean_t load_module)
804 {
805 	crypto_mech_type_t mt;
806 	kcf_mech_entry_t *me;
807 	int i;
808 	kcf_ops_class_t class;
809 	boolean_t second_time = B_FALSE;
810 	boolean_t try_to_load_software_provider = B_FALSE;
811 
812 try_again:
813 	mt = kcf_mech_hash_find(mechname);
814 	if (!load_module || second_time == B_TRUE || servicing_interrupt())
815 		return (mt);
816 
817 	if (mt != CRYPTO_MECH_INVALID) {
818 		class = KCF_MECH2CLASS(mt);
819 		i = KCF_MECH2INDEX(mt);
820 		me = &(kcf_mech_tabs_tab[class].met_tab[i]);
821 		mutex_enter(&(me->me_mutex));
822 		if (load_module && !auto_unload_flag_set(me->me_sw_prov)) {
823 			try_to_load_software_provider = B_TRUE;
824 		}
825 		mutex_exit(&(me->me_mutex));
826 	}
827 
828 	if (mt == CRYPTO_MECH_INVALID || try_to_load_software_provider) {
829 		struct modctl *mcp;
830 		boolean_t load_again = B_FALSE;
831 		char *module_name;
832 		int module_name_size;
833 
834 		/* try to find a software provider for the mechanism */
835 		if (get_sw_provider_for_mech(mechname, &module_name)
836 		    != CRYPTO_SUCCESS) {
837 			/* mt may already be set for a hw provider */
838 			return (mt);
839 		}
840 
841 		module_name_size = strlen(module_name) + 1;
842 		if (modload("crypto", module_name) == -1 ||
843 		    (mcp = mod_hold_by_name(module_name)) == NULL) {
844 			kmem_free(module_name, module_name_size);
845 			/* mt may already be set for a hw provider */
846 			return (mt);
847 		}
848 
849 		mcp->mod_loadflags |= MOD_NOAUTOUNLOAD;
850 
851 		/* memory pressure may have unloaded the module */
852 		if (!mcp->mod_installed)
853 			load_again = B_TRUE;
854 		mod_release_mod(mcp);
855 
856 		if (load_again)
857 			(void) modload("crypto", module_name);
858 
859 		kmem_free(module_name, module_name_size);
860 
861 		/* mt may already be set for a hw provider */
862 		if (mt != CRYPTO_MECH_INVALID)
863 			return (mt);
864 
865 		/*
866 		 * Try again.  Should find a software provider in the
867 		 * table this time around.
868 		 */
869 		second_time = B_TRUE;
870 		goto try_again;
871 	}
872 
873 	return (mt);
874 }
875