xref: /illumos-gate/usr/src/uts/common/sys/crypto/impl.h (revision fb9b0aa8c76ff829a7069d38161a2cecc656b091)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef	_SYS_CRYPTO_IMPL_H
27 #define	_SYS_CRYPTO_IMPL_H
28 
29 /*
30  * Kernel Cryptographic Framework private implementation definitions.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/param.h>
35 
36 #ifdef _KERNEL
37 #include <sys/crypto/common.h>
38 #include <sys/crypto/api.h>
39 #include <sys/crypto/spi.h>
40 #include <sys/crypto/ioctl.h>
41 #include <sys/tnf_probe.h>
42 #include <sys/atomic.h>
43 #include <sys/project.h>
44 #include <sys/taskq.h>
45 #include <sys/rctl.h>
46 #include <sys/cpuvar.h>
47 #endif /* _KERNEL */
48 
49 #ifdef	__cplusplus
50 extern "C" {
51 #endif
52 
53 #ifdef _KERNEL
54 
55 /*
56  * Prefixes convention: structures internal to the kernel cryptographic
57  * framework start with 'kcf_'. Exposed structure start with 'crypto_'.
58  */
59 
60 /* Provider stats. Not protected. */
61 typedef	struct kcf_prov_stats {
62 	kstat_named_t	ps_ops_total;
63 	kstat_named_t	ps_ops_passed;
64 	kstat_named_t	ps_ops_failed;
65 	kstat_named_t	ps_ops_busy_rval;
66 } kcf_prov_stats_t;
67 
68 /* Various kcf stats. Not protected. */
69 typedef	struct kcf_stats {
70 	kstat_named_t	ks_thrs_in_pool;
71 	kstat_named_t	ks_idle_thrs;
72 	kstat_named_t	ks_minthrs;
73 	kstat_named_t	ks_maxthrs;
74 	kstat_named_t	ks_swq_njobs;
75 	kstat_named_t	ks_swq_maxjobs;
76 	kstat_named_t	ks_taskq_threads;
77 	kstat_named_t	ks_taskq_minalloc;
78 	kstat_named_t	ks_taskq_maxalloc;
79 } kcf_stats_t;
80 
81 #define	CPU_SEQID	(CPU->cpu_seqid)
82 
83 typedef struct kcf_lock_withpad {
84 	kmutex_t	kl_lock;
85 	uint8_t		kl_pad[64 - sizeof (kmutex_t)];
86 } kcf_lock_withpad_t;
87 
88 /*
89  * Per-CPU structure used by a provider to keep track of
90  * various counters.
91  */
92 typedef struct kcf_prov_cpu {
93 	kmutex_t	kp_lock;
94 	int		kp_holdcnt;	/* can go negative! */
95 	uint_t		kp_jobcnt;
96 
97 	uint64_t	kp_ndispatches;
98 	uint64_t	kp_nfails;
99 	uint64_t	kp_nbusy_rval;
100 	kcondvar_t	kp_cv;
101 
102 	uint8_t		kp_pad[64 - sizeof (kmutex_t) - 2 * sizeof (int) -
103 	    3 * sizeof (uint64_t) - sizeof (kcondvar_t)];
104 } kcf_prov_cpu_t;
105 
106 /*
107  * kcf_get_refcnt(pd) is the number of inflight requests to the
108  * provider. So, it is a good measure of the load on a provider when
109  * it is not in a busy state. Once a provider notifies it is busy, requests
110  * backup in the taskq. So, we use tq_nalloc in that case which gives
111  * the number of task entries in the task queue. Note that we do not
112  * acquire any locks here as it is not critical to get the exact number
113  * and the lock contention is too costly for this code path.
114  */
115 #define	KCF_PROV_LOAD(pd)	((pd)->pd_state != KCF_PROV_BUSY ?	\
116 	kcf_get_refcnt(pd, B_FALSE) : (pd)->pd_taskq->tq_nalloc)
117 
118 
119 /*
120  * The following two macros should be
121  * #define KCF_OPS_CLASSSIZE (KCF_LAST_OPSCLASS - KCF_FIRST_OPSCLASS + 2)
122  * #define KCF_MAXMECHTAB KCF_MAXCIPHER
123  *
124  * However, doing that would involve reorganizing the header file a bit.
125  * When impl.h is broken up (bug# 4703218), this will be done. For now,
126  * we hardcode these values.
127  */
128 #define	KCF_OPS_CLASSSIZE	8
129 #define	KCF_MAXMECHTAB		32
130 
131 /*
132  * Valid values for the state of a provider. The order of
133  * the elements is important.
134  *
135  * Routines which get a provider or the list of providers
136  * should pick only those that are either in KCF_PROV_READY state
137  * or in KCF_PROV_BUSY state.
138  */
139 typedef enum {
140 	KCF_PROV_ALLOCATED = 1,
141 	KCF_PROV_UNVERIFIED,
142 	KCF_PROV_VERIFICATION_FAILED,
143 	/*
144 	 * state < KCF_PROV_READY means the provider can not
145 	 * be used at all.
146 	 */
147 	KCF_PROV_READY,
148 	KCF_PROV_BUSY,
149 	/*
150 	 * state > KCF_PROV_BUSY means the provider can not
151 	 * be used for new requests.
152 	 */
153 	KCF_PROV_FAILED,
154 	/*
155 	 * Threads setting the following two states should do so only
156 	 * if the current state < KCF_PROV_DISABLED.
157 	 */
158 	KCF_PROV_DISABLED,
159 	KCF_PROV_UNREGISTERING,
160 	KCF_PROV_UNREGISTERED
161 } kcf_prov_state_t;
162 
163 #define	KCF_IS_PROV_UNVERIFIED(pd) ((pd)->pd_state == KCF_PROV_UNVERIFIED)
164 #define	KCF_IS_PROV_USABLE(pd) ((pd)->pd_state == KCF_PROV_READY || \
165 	(pd)->pd_state == KCF_PROV_BUSY)
166 #define	KCF_IS_PROV_REMOVED(pd)	((pd)->pd_state >= KCF_PROV_UNREGISTERING)
167 
168 /* Internal flags valid for pd_flags field */
169 #define	KCF_PROV_RESTRICTED	0x40000000
170 #define	KCF_LPROV_MEMBER	0x80000000 /* is member of a logical provider */
171 
172 /*
173  * A provider descriptor structure. There is one such structure per
174  * provider. It is allocated and initialized at registration time and
175  * freed when the provider unregisters.
176  *
177  * pd_prov_type:	Provider type, hardware or software
178  * pd_sid:		Session ID of the provider used by kernel clients.
179  *			This is valid only for session-oriented providers.
180  * pd_taskq:		taskq used to dispatch crypto requests
181  * pd_nbins:		number of bins in pd_percpu_bins
182  * pd_percpu_bins:	Pointer to an array of per-CPU structures
183  *			containing a lock, a cv and various counters.
184  * pd_lock:		lock protects pd_state and pd_provider_list
185  * pd_state:		State value of the provider
186  * pd_provider_list:	Used to cross-reference logical providers and their
187  *			members. Not used for software providers.
188  * pd_resume_cv:	cv to wait for state to change from KCF_PROV_BUSY
189  * pd_prov_handle:	Provider handle specified by provider
190  * pd_ops_vector:	The ops vector specified by Provider
191  * pd_mech_indx:	Lookup table which maps a core framework mechanism
192  *			number to an index in pd_mechanisms array
193  * pd_mechanisms:	Array of mechanisms supported by the provider, specified
194  *			by the provider during registration
195  * pd_mech_list_count:	The number of entries in pi_mechanisms, specified
196  *			by the provider during registration
197  * pd_name:		Device name or module name
198  * pd_instance:		Device instance
199  * pd_module_id:	Module ID returned by modload
200  * pd_mctlp:		Pointer to modctl structure for this provider
201  * pd_description:	Provider description string
202  * pd_flags:		bitwise OR of pi_flags from crypto_provider_info_t
203  *			and other internal flags defined above.
204  * pd_hash_limit:	Maximum data size that hash mechanisms of this provider
205  * 			can support.
206  * pd_kcf_prov_handle:	KCF-private handle assigned by KCF
207  * pd_prov_id:		Identification # assigned by KCF to provider
208  * pd_kstat:		kstat associated with the provider
209  * pd_ks_data:		kstat data
210  */
211 typedef struct kcf_provider_desc {
212 	crypto_provider_type_t		pd_prov_type;
213 	crypto_session_id_t		pd_sid;
214 	taskq_t				*pd_taskq;
215 	uint_t				pd_nbins;
216 	kcf_prov_cpu_t			*pd_percpu_bins;
217 	kmutex_t			pd_lock;
218 	kcf_prov_state_t		pd_state;
219 	struct kcf_provider_list	*pd_provider_list;
220 	kcondvar_t			pd_resume_cv;
221 	crypto_provider_handle_t	pd_prov_handle;
222 	crypto_ops_t			*pd_ops_vector;
223 	ushort_t			pd_mech_indx[KCF_OPS_CLASSSIZE]\
224 					    [KCF_MAXMECHTAB];
225 	crypto_mech_info_t		*pd_mechanisms;
226 	uint_t				pd_mech_list_count;
227 	char				*pd_name;
228 	uint_t				pd_instance;
229 	int				pd_module_id;
230 	struct modctl			*pd_mctlp;
231 	char				*pd_description;
232 	uint_t				pd_flags;
233 	uint_t				pd_hash_limit;
234 	crypto_kcf_provider_handle_t	pd_kcf_prov_handle;
235 	crypto_provider_id_t		pd_prov_id;
236 	kstat_t				*pd_kstat;
237 	kcf_prov_stats_t		pd_ks_data;
238 } kcf_provider_desc_t;
239 
240 /* useful for making a list of providers */
241 typedef struct kcf_provider_list {
242 	struct kcf_provider_list *pl_next;
243 	struct kcf_provider_desc *pl_provider;
244 } kcf_provider_list_t;
245 
246 /*
247  * If a component has a reference to a kcf_provider_desc_t,
248  * it REFHOLD()s. A new provider descriptor which is referenced only
249  * by the providers table has a reference counter of one.
250  */
251 #define	KCF_PROV_REFHOLD(desc) {			\
252 	kcf_prov_cpu_t	*mp;				\
253 							\
254 	mp = &((desc)->pd_percpu_bins[CPU_SEQID]);	\
255 	mutex_enter(&mp->kp_lock);			\
256 	mp->kp_holdcnt++;				\
257 	mutex_exit(&mp->kp_lock);			\
258 }
259 
260 #define	KCF_PROV_REFRELE(desc) {			\
261 	kcf_prov_cpu_t	*mp;				\
262 							\
263 	mp = &((desc)->pd_percpu_bins[CPU_SEQID]);	\
264 	mutex_enter(&mp->kp_lock);			\
265 	mp->kp_holdcnt--;				\
266 	mutex_exit(&mp->kp_lock);			\
267 }
268 
269 #define	KCF_PROV_REFHELD(desc)	(kcf_get_refcnt(desc, B_TRUE) >= 1)
270 
271 /*
272  * The JOB macros are used only for a hardware provider.
273  * Hardware providers can have holds that stay forever.
274  * So, the job counter is used to check if it is safe to
275  * unregister a provider.
276  */
277 #define	KCF_PROV_JOB_HOLD(mp) {			\
278 	mutex_enter(&(mp)->kp_lock);		\
279 	(mp)->kp_jobcnt++;			\
280 	mutex_exit(&(mp)->kp_lock);		\
281 }
282 
283 #define	KCF_PROV_JOB_RELE(mp) {			\
284 	mutex_enter(&(mp)->kp_lock);		\
285 	(mp)->kp_jobcnt--;			\
286 	if ((mp)->kp_jobcnt == 0)		\
287 		cv_signal(&(mp)->kp_cv);	\
288 	mutex_exit(&(mp)->kp_lock);		\
289 }
290 
291 #define	KCF_PROV_JOB_RELE_STAT(mp, doincr) {	\
292 	if (doincr)				\
293 		(mp)->kp_nfails++;		\
294 	KCF_PROV_JOB_RELE(mp);			\
295 }
296 
297 #define	KCF_PROV_INCRSTATS(pd, error)	{				\
298 	kcf_prov_cpu_t	*mp;						\
299 									\
300 	mp = &((pd)->pd_percpu_bins[CPU_SEQID]);			\
301 	mp->kp_ndispatches++;						\
302 	if ((error) == CRYPTO_BUSY)					\
303 		mp->kp_nbusy_rval++;					\
304 	else if ((error) != CRYPTO_SUCCESS && (error) != CRYPTO_QUEUED)	\
305 		mp->kp_nfails++;					\
306 }
307 
308 /* list of crypto_mech_info_t valid as the second mech in a dual operation */
309 
310 typedef	struct crypto_mech_info_list {
311 	struct crypto_mech_info_list	*ml_next;
312 	crypto_mech_type_t		ml_kcf_mechid;	/* KCF's id */
313 	crypto_mech_info_t		ml_mech_info;
314 } crypto_mech_info_list_t;
315 
316 /*
317  * An element in a mechanism provider descriptors chain.
318  * The kcf_prov_mech_desc_t is duplicated in every chain the provider belongs
319  * to. This is a small tradeoff memory vs mutex spinning time to access the
320  * common provider field.
321  */
322 
323 typedef struct kcf_prov_mech_desc {
324 	struct kcf_mech_entry		*pm_me;		/* Back to the head */
325 	struct kcf_prov_mech_desc	*pm_next;	/* Next in the chain */
326 	crypto_mech_info_t		pm_mech_info;	/* Provider mech info */
327 	crypto_mech_info_list_t		*pm_mi_list;	/* list for duals */
328 	kcf_provider_desc_t		*pm_prov_desc;	/* Common desc. */
329 } kcf_prov_mech_desc_t;
330 
331 /* and the notation shortcuts ... */
332 #define	pm_provider_type	pm_prov_desc.pd_provider_type
333 #define	pm_provider_handle	pm_prov_desc.pd_provider_handle
334 #define	pm_ops_vector		pm_prov_desc.pd_ops_vector
335 
336 extern kcf_lock_withpad_t *me_mutexes;
337 
338 #define	KCF_CPU_PAD (128 - sizeof (crypto_mech_name_t) - \
339     sizeof (crypto_mech_type_t) - \
340     2 * sizeof (kcf_prov_mech_desc_t *) - \
341     sizeof (int) - sizeof (uint32_t) - sizeof (size_t))
342 
343 /*
344  * A mechanism entry in an xxx_mech_tab[]. KCF_CPU_PAD needs
345  * to be adjusted if this structure is changed.
346  */
347 typedef	struct kcf_mech_entry {
348 	crypto_mech_name_t	me_name;	/* mechanism name */
349 	crypto_mech_type_t	me_mechid;	/* Internal id for mechanism */
350 	kcf_prov_mech_desc_t	*me_hw_prov_chain;  /* list of HW providers */
351 	kcf_prov_mech_desc_t	*me_sw_prov;    /* SW provider */
352 	/*
353 	 * Number of HW providers in the chain. There is only one
354 	 * SW provider. So, we need only a count of HW providers.
355 	 */
356 	int			me_num_hwprov;
357 	/*
358 	 * When a SW provider is present, this is the generation number that
359 	 * ensures no objects from old SW providers are used in the new one
360 	 */
361 	uint32_t		me_gen_swprov;
362 	/*
363 	 *  threshold for using hardware providers for this mech
364 	 */
365 	size_t			me_threshold;
366 	uint8_t			me_pad[KCF_CPU_PAD];
367 } kcf_mech_entry_t;
368 
369 /*
370  * A policy descriptor structure. It is allocated and initialized
371  * when administrative ioctls load disabled mechanisms.
372  *
373  * pd_prov_type:	Provider type, hardware or software
374  * pd_name:		Device name or module name.
375  * pd_instance:		Device instance.
376  * pd_refcnt:		Reference counter for this policy descriptor
377  * pd_mutex:		Protects array and count of disabled mechanisms.
378  * pd_disabled_count:	Count of disabled mechanisms.
379  * pd_disabled_mechs:	Array of disabled mechanisms.
380  */
381 typedef struct kcf_policy_desc {
382 	crypto_provider_type_t	pd_prov_type;
383 	char			*pd_name;
384 	uint_t			pd_instance;
385 	uint_t			pd_refcnt;
386 	kmutex_t		pd_mutex;
387 	uint_t			pd_disabled_count;
388 	crypto_mech_name_t	*pd_disabled_mechs;
389 } kcf_policy_desc_t;
390 
391 /*
392  * If a component has a reference to a kcf_policy_desc_t,
393  * it REFHOLD()s. A new policy descriptor which is referenced only
394  * by the policy table has a reference count of one.
395  */
396 #define	KCF_POLICY_REFHOLD(desc) {		\
397 	atomic_add_32(&(desc)->pd_refcnt, 1);	\
398 	ASSERT((desc)->pd_refcnt != 0);		\
399 }
400 
401 /*
402  * Releases a reference to a policy descriptor. When the last
403  * reference is released, the descriptor is freed.
404  */
405 #define	KCF_POLICY_REFRELE(desc) {				\
406 	ASSERT((desc)->pd_refcnt != 0);				\
407 	membar_exit();						\
408 	if (atomic_add_32_nv(&(desc)->pd_refcnt, -1) == 0)	\
409 		kcf_policy_free_desc(desc);			\
410 }
411 
412 /*
413  * This entry stores the name of a software module and its
414  * mechanisms.  The mechanisms are 'hints' that are used to
415  * trigger loading of the module.
416  */
417 typedef struct kcf_soft_conf_entry {
418 	struct kcf_soft_conf_entry	*ce_next;
419 	char				*ce_name;
420 	crypto_mech_name_t		*ce_mechs;
421 	uint_t				ce_count;
422 } kcf_soft_conf_entry_t;
423 
424 extern kmutex_t soft_config_mutex;
425 extern kcf_soft_conf_entry_t *soft_config_list;
426 
427 /*
428  * Global tables. The sizes are from the predefined PKCS#11 v2.20 mechanisms,
429  * with a margin of few extra empty entry points
430  */
431 
432 #define	KCF_MAXDIGEST		16	/* Digests */
433 #define	KCF_MAXCIPHER		64	/* Ciphers */
434 #define	KCF_MAXMAC		40	/* Message authentication codes */
435 #define	KCF_MAXSIGN		24	/* Sign/Verify */
436 #define	KCF_MAXKEYOPS		116	/* Key generation and derivation */
437 #define	KCF_MAXMISC		16	/* Others ... */
438 
439 #define	KCF_MAXMECHS		KCF_MAXDIGEST + KCF_MAXCIPHER + KCF_MAXMAC + \
440 				KCF_MAXSIGN + KCF_MAXKEYOPS + \
441 				KCF_MAXMISC
442 
443 extern kcf_mech_entry_t kcf_digest_mechs_tab[];
444 extern kcf_mech_entry_t kcf_cipher_mechs_tab[];
445 extern kcf_mech_entry_t kcf_mac_mechs_tab[];
446 extern kcf_mech_entry_t kcf_sign_mechs_tab[];
447 extern kcf_mech_entry_t kcf_keyops_mechs_tab[];
448 extern kcf_mech_entry_t kcf_misc_mechs_tab[];
449 
450 extern kmutex_t kcf_mech_tabs_lock;
451 
452 typedef	enum {
453 	KCF_DIGEST_CLASS = 1,
454 	KCF_CIPHER_CLASS,
455 	KCF_MAC_CLASS,
456 	KCF_SIGN_CLASS,
457 	KCF_KEYOPS_CLASS,
458 	KCF_MISC_CLASS
459 } kcf_ops_class_t;
460 
461 #define	KCF_FIRST_OPSCLASS	KCF_DIGEST_CLASS
462 #define	KCF_LAST_OPSCLASS	KCF_MISC_CLASS
463 
464 /* The table of all the kcf_xxx_mech_tab[]s, indexed by kcf_ops_class */
465 
466 typedef	struct kcf_mech_entry_tab {
467 	int			met_size;	/* Size of the met_tab[] */
468 	kcf_mech_entry_t	*met_tab;	/* the table		 */
469 } kcf_mech_entry_tab_t;
470 
471 extern kcf_mech_entry_tab_t kcf_mech_tabs_tab[];
472 
473 #define	KCF_MECHID(class, index)				\
474 	(((crypto_mech_type_t)(class) << 32) | (crypto_mech_type_t)(index))
475 
476 #define	KCF_MECH2CLASS(mech_type) ((kcf_ops_class_t)((mech_type) >> 32))
477 
478 #define	KCF_MECH2INDEX(mech_type) ((int)(mech_type))
479 
480 #define	KCF_TO_PROV_MECH_INDX(pd, mech_type) 			\
481 	((pd)->pd_mech_indx[KCF_MECH2CLASS(mech_type)] 		\
482 	[KCF_MECH2INDEX(mech_type)])
483 
484 #define	KCF_TO_PROV_MECHINFO(pd, mech_type)			\
485 	((pd)->pd_mechanisms[KCF_TO_PROV_MECH_INDX(pd, mech_type)])
486 
487 #define	KCF_TO_PROV_MECHNUM(pd, mech_type)			\
488 	(KCF_TO_PROV_MECHINFO(pd, mech_type).cm_mech_number)
489 
490 #define	KCF_CAN_SHARE_OPSTATE(pd, mech_type)			\
491 	((KCF_TO_PROV_MECHINFO(pd, mech_type).cm_mech_flags) &	\
492 	CRYPTO_CAN_SHARE_OPSTATE)
493 
494 /* ps_refcnt is protected by cm_lock in the crypto_minor structure */
495 typedef struct crypto_provider_session {
496 	struct crypto_provider_session *ps_next;
497 	crypto_session_id_t		ps_session;
498 	kcf_provider_desc_t		*ps_provider;
499 	kcf_provider_desc_t		*ps_real_provider;
500 	uint_t				ps_refcnt;
501 } crypto_provider_session_t;
502 
503 typedef struct crypto_session_data {
504 	kmutex_t			sd_lock;
505 	kcondvar_t			sd_cv;
506 	uint32_t			sd_flags;
507 	int				sd_pre_approved_amount;
508 	crypto_ctx_t			*sd_digest_ctx;
509 	crypto_ctx_t			*sd_encr_ctx;
510 	crypto_ctx_t			*sd_decr_ctx;
511 	crypto_ctx_t			*sd_sign_ctx;
512 	crypto_ctx_t			*sd_verify_ctx;
513 	crypto_ctx_t			*sd_sign_recover_ctx;
514 	crypto_ctx_t			*sd_verify_recover_ctx;
515 	kcf_provider_desc_t		*sd_provider;
516 	void				*sd_find_init_cookie;
517 	crypto_provider_session_t	*sd_provider_session;
518 } crypto_session_data_t;
519 
520 #define	CRYPTO_SESSION_IN_USE		0x00000001
521 #define	CRYPTO_SESSION_IS_BUSY		0x00000002
522 #define	CRYPTO_SESSION_IS_CLOSED	0x00000004
523 
524 #define	KCF_MAX_PIN_LEN			1024
525 
526 /*
527  * Per-minor info.
528  *
529  * cm_lock protects everything in this structure except for cm_refcnt.
530  */
531 typedef struct crypto_minor {
532 	uint_t				cm_refcnt;
533 	kmutex_t			cm_lock;
534 	kcondvar_t			cm_cv;
535 	crypto_session_data_t		**cm_session_table;
536 	uint_t				cm_session_table_count;
537 	kcf_provider_desc_t		**cm_provider_array;
538 	uint_t				cm_provider_count;
539 	crypto_provider_session_t	*cm_provider_session;
540 } crypto_minor_t;
541 
542 /* resource control framework handle used by /dev/crypto */
543 extern rctl_hndl_t rc_project_crypto_mem;
544 /*
545  * Return codes for internal functions
546  */
547 #define	KCF_SUCCESS		0x0	/* Successful call */
548 #define	KCF_INVALID_MECH_NUMBER	0x1	/* invalid mechanism number */
549 #define	KCF_INVALID_MECH_NAME	0x2	/* invalid mechanism name */
550 #define	KCF_INVALID_MECH_CLASS	0x3	/* invalid mechanism class */
551 #define	KCF_MECH_TAB_FULL	0x4	/* Need more room in the mech tabs. */
552 #define	KCF_INVALID_INDX	((ushort_t)-1)
553 
554 /*
555  * kCF internal mechanism and function group for tracking RNG providers.
556  */
557 #define	SUN_RANDOM		"random"
558 #define	CRYPTO_FG_RANDOM	0x80000000	/* generate_random() */
559 
560 /*
561  * Wrappers for ops vectors. In the wrapper definitions below, the pd
562  * argument always corresponds to a pointer to a provider descriptor
563  * of type kcf_prov_desc_t.
564  */
565 
566 #define	KCF_PROV_CONTROL_OPS(pd)	((pd)->pd_ops_vector->co_control_ops)
567 #define	KCF_PROV_CTX_OPS(pd)		((pd)->pd_ops_vector->co_ctx_ops)
568 #define	KCF_PROV_DIGEST_OPS(pd)		((pd)->pd_ops_vector->co_digest_ops)
569 #define	KCF_PROV_CIPHER_OPS(pd)		((pd)->pd_ops_vector->co_cipher_ops)
570 #define	KCF_PROV_MAC_OPS(pd)		((pd)->pd_ops_vector->co_mac_ops)
571 #define	KCF_PROV_SIGN_OPS(pd)		((pd)->pd_ops_vector->co_sign_ops)
572 #define	KCF_PROV_VERIFY_OPS(pd)		((pd)->pd_ops_vector->co_verify_ops)
573 #define	KCF_PROV_DUAL_OPS(pd)		((pd)->pd_ops_vector->co_dual_ops)
574 #define	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd) \
575 	((pd)->pd_ops_vector->co_dual_cipher_mac_ops)
576 #define	KCF_PROV_RANDOM_OPS(pd)		((pd)->pd_ops_vector->co_random_ops)
577 #define	KCF_PROV_SESSION_OPS(pd)	((pd)->pd_ops_vector->co_session_ops)
578 #define	KCF_PROV_OBJECT_OPS(pd)		((pd)->pd_ops_vector->co_object_ops)
579 #define	KCF_PROV_KEY_OPS(pd)		((pd)->pd_ops_vector->co_key_ops)
580 #define	KCF_PROV_PROVIDER_OPS(pd)	((pd)->pd_ops_vector->co_provider_ops)
581 #define	KCF_PROV_MECH_OPS(pd)		((pd)->pd_ops_vector->co_mech_ops)
582 #define	KCF_PROV_NOSTORE_KEY_OPS(pd)	\
583 	((pd)->pd_ops_vector->co_nostore_key_ops)
584 
585 /*
586  * Wrappers for crypto_control_ops(9S) entry points.
587  */
588 
589 #define	KCF_PROV_STATUS(pd, status) ( \
590 	(KCF_PROV_CONTROL_OPS(pd) && \
591 	KCF_PROV_CONTROL_OPS(pd)->provider_status) ? \
592 	KCF_PROV_CONTROL_OPS(pd)->provider_status( \
593 	    (pd)->pd_prov_handle, status) : \
594 	CRYPTO_NOT_SUPPORTED)
595 
596 /*
597  * Wrappers for crypto_ctx_ops(9S) entry points.
598  */
599 
600 #define	KCF_PROV_CREATE_CTX_TEMPLATE(pd, mech, key, template, size, req) ( \
601 	(KCF_PROV_CTX_OPS(pd) && KCF_PROV_CTX_OPS(pd)->create_ctx_template) ? \
602 	KCF_PROV_CTX_OPS(pd)->create_ctx_template( \
603 	    (pd)->pd_prov_handle, mech, key, template, size, req) : \
604 	CRYPTO_NOT_SUPPORTED)
605 
606 #define	KCF_PROV_FREE_CONTEXT(pd, ctx) ( \
607 	(KCF_PROV_CTX_OPS(pd) && KCF_PROV_CTX_OPS(pd)->free_context) ? \
608 	KCF_PROV_CTX_OPS(pd)->free_context(ctx) : CRYPTO_NOT_SUPPORTED)
609 
610 #define	KCF_PROV_COPYIN_MECH(pd, umech, kmech, errorp, mode) ( \
611 	(KCF_PROV_MECH_OPS(pd) && KCF_PROV_MECH_OPS(pd)->copyin_mechanism) ? \
612 	KCF_PROV_MECH_OPS(pd)->copyin_mechanism( \
613 	    (pd)->pd_prov_handle, umech, kmech, errorp, mode) : \
614 	CRYPTO_NOT_SUPPORTED)
615 
616 #define	KCF_PROV_COPYOUT_MECH(pd, kmech, umech, errorp, mode) ( \
617 	(KCF_PROV_MECH_OPS(pd) && KCF_PROV_MECH_OPS(pd)->copyout_mechanism) ? \
618 	KCF_PROV_MECH_OPS(pd)->copyout_mechanism( \
619 	    (pd)->pd_prov_handle, kmech, umech, errorp, mode) : \
620 	CRYPTO_NOT_SUPPORTED)
621 
622 #define	KCF_PROV_FREE_MECH(pd, prov_mech) ( \
623 	(KCF_PROV_MECH_OPS(pd) && KCF_PROV_MECH_OPS(pd)->free_mechanism) ? \
624 	KCF_PROV_MECH_OPS(pd)->free_mechanism( \
625 	    (pd)->pd_prov_handle, prov_mech) : CRYPTO_NOT_SUPPORTED)
626 
627 /*
628  * Wrappers for crypto_digest_ops(9S) entry points.
629  */
630 
631 #define	KCF_PROV_DIGEST_INIT(pd, ctx, mech, req) ( \
632 	(KCF_PROV_DIGEST_OPS(pd) && KCF_PROV_DIGEST_OPS(pd)->digest_init) ? \
633 	KCF_PROV_DIGEST_OPS(pd)->digest_init(ctx, mech, req) : \
634 	CRYPTO_NOT_SUPPORTED)
635 
636 /*
637  * The _ (underscore) in _digest is needed to avoid replacing the
638  * function digest().
639  */
640 #define	KCF_PROV_DIGEST(pd, ctx, data, _digest, req) ( \
641 	(KCF_PROV_DIGEST_OPS(pd) && KCF_PROV_DIGEST_OPS(pd)->digest) ? \
642 	KCF_PROV_DIGEST_OPS(pd)->digest(ctx, data, _digest, req) : \
643 	CRYPTO_NOT_SUPPORTED)
644 
645 #define	KCF_PROV_DIGEST_UPDATE(pd, ctx, data, req) ( \
646 	(KCF_PROV_DIGEST_OPS(pd) && KCF_PROV_DIGEST_OPS(pd)->digest_update) ? \
647 	KCF_PROV_DIGEST_OPS(pd)->digest_update(ctx, data, req) : \
648 	CRYPTO_NOT_SUPPORTED)
649 
650 #define	KCF_PROV_DIGEST_KEY(pd, ctx, key, req) ( \
651 	(KCF_PROV_DIGEST_OPS(pd) && KCF_PROV_DIGEST_OPS(pd)->digest_key) ? \
652 	KCF_PROV_DIGEST_OPS(pd)->digest_key(ctx, key, req) : \
653 	CRYPTO_NOT_SUPPORTED)
654 
655 #define	KCF_PROV_DIGEST_FINAL(pd, ctx, digest, req) ( \
656 	(KCF_PROV_DIGEST_OPS(pd) && KCF_PROV_DIGEST_OPS(pd)->digest_final) ? \
657 	KCF_PROV_DIGEST_OPS(pd)->digest_final(ctx, digest, req) : \
658 	CRYPTO_NOT_SUPPORTED)
659 
660 #define	KCF_PROV_DIGEST_ATOMIC(pd, session, mech, data, digest, req) ( \
661 	(KCF_PROV_DIGEST_OPS(pd) && KCF_PROV_DIGEST_OPS(pd)->digest_atomic) ? \
662 	KCF_PROV_DIGEST_OPS(pd)->digest_atomic( \
663 	    (pd)->pd_prov_handle, session, mech, data, digest, req) : \
664 	CRYPTO_NOT_SUPPORTED)
665 
666 /*
667  * Wrappers for crypto_cipher_ops(9S) entry points.
668  */
669 
670 #define	KCF_PROV_ENCRYPT_INIT(pd, ctx, mech, key, template, req) ( \
671 	(KCF_PROV_CIPHER_OPS(pd) && KCF_PROV_CIPHER_OPS(pd)->encrypt_init) ? \
672 	KCF_PROV_CIPHER_OPS(pd)->encrypt_init(ctx, mech, key, template, \
673 	    req) : \
674 	CRYPTO_NOT_SUPPORTED)
675 
676 #define	KCF_PROV_ENCRYPT(pd, ctx, plaintext, ciphertext, req) ( \
677 	(KCF_PROV_CIPHER_OPS(pd) && KCF_PROV_CIPHER_OPS(pd)->encrypt) ? \
678 	KCF_PROV_CIPHER_OPS(pd)->encrypt(ctx, plaintext, ciphertext, req) : \
679 	CRYPTO_NOT_SUPPORTED)
680 
681 #define	KCF_PROV_ENCRYPT_UPDATE(pd, ctx, plaintext, ciphertext, req) ( \
682 	(KCF_PROV_CIPHER_OPS(pd) && KCF_PROV_CIPHER_OPS(pd)->encrypt_update) ? \
683 	KCF_PROV_CIPHER_OPS(pd)->encrypt_update(ctx, plaintext, \
684 	    ciphertext, req) : \
685 	CRYPTO_NOT_SUPPORTED)
686 
687 #define	KCF_PROV_ENCRYPT_FINAL(pd, ctx, ciphertext, req) ( \
688 	(KCF_PROV_CIPHER_OPS(pd) && KCF_PROV_CIPHER_OPS(pd)->encrypt_final) ? \
689 	KCF_PROV_CIPHER_OPS(pd)->encrypt_final(ctx, ciphertext, req) : \
690 	CRYPTO_NOT_SUPPORTED)
691 
692 #define	KCF_PROV_ENCRYPT_ATOMIC(pd, session, mech, key, plaintext, ciphertext, \
693 	    template, req) ( \
694 	(KCF_PROV_CIPHER_OPS(pd) && KCF_PROV_CIPHER_OPS(pd)->encrypt_atomic) ? \
695 	KCF_PROV_CIPHER_OPS(pd)->encrypt_atomic( \
696 	    (pd)->pd_prov_handle, session, mech, key, plaintext, ciphertext, \
697 	    template, req) : \
698 	CRYPTO_NOT_SUPPORTED)
699 
700 #define	KCF_PROV_DECRYPT_INIT(pd, ctx, mech, key, template, req) ( \
701 	(KCF_PROV_CIPHER_OPS(pd) && KCF_PROV_CIPHER_OPS(pd)->decrypt_init) ? \
702 	KCF_PROV_CIPHER_OPS(pd)->decrypt_init(ctx, mech, key, template, \
703 	    req) : \
704 	CRYPTO_NOT_SUPPORTED)
705 
706 #define	KCF_PROV_DECRYPT(pd, ctx, ciphertext, plaintext, req) ( \
707 	(KCF_PROV_CIPHER_OPS(pd) && KCF_PROV_CIPHER_OPS(pd)->decrypt) ? \
708 	KCF_PROV_CIPHER_OPS(pd)->decrypt(ctx, ciphertext, plaintext, req) : \
709 	CRYPTO_NOT_SUPPORTED)
710 
711 #define	KCF_PROV_DECRYPT_UPDATE(pd, ctx, ciphertext, plaintext, req) ( \
712 	(KCF_PROV_CIPHER_OPS(pd) && KCF_PROV_CIPHER_OPS(pd)->decrypt_update) ? \
713 	KCF_PROV_CIPHER_OPS(pd)->decrypt_update(ctx, ciphertext, \
714 	    plaintext, req) : \
715 	CRYPTO_NOT_SUPPORTED)
716 
717 #define	KCF_PROV_DECRYPT_FINAL(pd, ctx, plaintext, req) ( \
718 	(KCF_PROV_CIPHER_OPS(pd) && KCF_PROV_CIPHER_OPS(pd)->decrypt_final) ? \
719 	KCF_PROV_CIPHER_OPS(pd)->decrypt_final(ctx, plaintext, req) : \
720 	CRYPTO_NOT_SUPPORTED)
721 
722 #define	KCF_PROV_DECRYPT_ATOMIC(pd, session, mech, key, ciphertext, plaintext, \
723 	    template, req) ( \
724 	(KCF_PROV_CIPHER_OPS(pd) && KCF_PROV_CIPHER_OPS(pd)->decrypt_atomic) ? \
725 	KCF_PROV_CIPHER_OPS(pd)->decrypt_atomic( \
726 	    (pd)->pd_prov_handle, session, mech, key, ciphertext, plaintext, \
727 	    template, req) : \
728 	CRYPTO_NOT_SUPPORTED)
729 
730 /*
731  * Wrappers for crypto_mac_ops(9S) entry points.
732  */
733 
734 #define	KCF_PROV_MAC_INIT(pd, ctx, mech, key, template, req) ( \
735 	(KCF_PROV_MAC_OPS(pd) && KCF_PROV_MAC_OPS(pd)->mac_init) ? \
736 	KCF_PROV_MAC_OPS(pd)->mac_init(ctx, mech, key, template, req) \
737 	: CRYPTO_NOT_SUPPORTED)
738 
739 /*
740  * The _ (underscore) in _mac is needed to avoid replacing the
741  * function mac().
742  */
743 #define	KCF_PROV_MAC(pd, ctx, data, _mac, req) ( \
744 	(KCF_PROV_MAC_OPS(pd) && KCF_PROV_MAC_OPS(pd)->mac) ? \
745 	KCF_PROV_MAC_OPS(pd)->mac(ctx, data, _mac, req) : \
746 	CRYPTO_NOT_SUPPORTED)
747 
748 #define	KCF_PROV_MAC_UPDATE(pd, ctx, data, req) ( \
749 	(KCF_PROV_MAC_OPS(pd) && KCF_PROV_MAC_OPS(pd)->mac_update) ? \
750 	KCF_PROV_MAC_OPS(pd)->mac_update(ctx, data, req) : \
751 	CRYPTO_NOT_SUPPORTED)
752 
753 #define	KCF_PROV_MAC_FINAL(pd, ctx, mac, req) ( \
754 	(KCF_PROV_MAC_OPS(pd) && KCF_PROV_MAC_OPS(pd)->mac_final) ? \
755 	KCF_PROV_MAC_OPS(pd)->mac_final(ctx, mac, req) : \
756 	CRYPTO_NOT_SUPPORTED)
757 
758 #define	KCF_PROV_MAC_ATOMIC(pd, session, mech, key, data, mac, template, \
759 	    req) ( \
760 	(KCF_PROV_MAC_OPS(pd) && KCF_PROV_MAC_OPS(pd)->mac_atomic) ? \
761 	KCF_PROV_MAC_OPS(pd)->mac_atomic( \
762 	    (pd)->pd_prov_handle, session, mech, key, data, mac, template, \
763 	    req) : \
764 	CRYPTO_NOT_SUPPORTED)
765 
766 #define	KCF_PROV_MAC_VERIFY_ATOMIC(pd, session, mech, key, data, mac, \
767 	    template, req) ( \
768 	(KCF_PROV_MAC_OPS(pd) && KCF_PROV_MAC_OPS(pd)->mac_verify_atomic) ? \
769 	KCF_PROV_MAC_OPS(pd)->mac_verify_atomic( \
770 	    (pd)->pd_prov_handle, session, mech, key, data, mac, template, \
771 	    req) : \
772 	CRYPTO_NOT_SUPPORTED)
773 
774 /*
775  * Wrappers for crypto_sign_ops(9S) entry points.
776  */
777 
778 #define	KCF_PROV_SIGN_INIT(pd, ctx, mech, key, template, req) ( \
779 	(KCF_PROV_SIGN_OPS(pd) && KCF_PROV_SIGN_OPS(pd)->sign_init) ? \
780 	KCF_PROV_SIGN_OPS(pd)->sign_init( \
781 	    ctx, mech, key, template, req) : CRYPTO_NOT_SUPPORTED)
782 
783 #define	KCF_PROV_SIGN(pd, ctx, data, sig, req) ( \
784 	(KCF_PROV_SIGN_OPS(pd) && KCF_PROV_SIGN_OPS(pd)->sign) ? \
785 	KCF_PROV_SIGN_OPS(pd)->sign(ctx, data, sig, req) : \
786 	CRYPTO_NOT_SUPPORTED)
787 
788 #define	KCF_PROV_SIGN_UPDATE(pd, ctx, data, req) ( \
789 	(KCF_PROV_SIGN_OPS(pd) && KCF_PROV_SIGN_OPS(pd)->sign_update) ? \
790 	KCF_PROV_SIGN_OPS(pd)->sign_update(ctx, data, req) : \
791 	CRYPTO_NOT_SUPPORTED)
792 
793 #define	KCF_PROV_SIGN_FINAL(pd, ctx, sig, req) ( \
794 	(KCF_PROV_SIGN_OPS(pd) && KCF_PROV_SIGN_OPS(pd)->sign_final) ? \
795 	KCF_PROV_SIGN_OPS(pd)->sign_final(ctx, sig, req) : \
796 	CRYPTO_NOT_SUPPORTED)
797 
798 #define	KCF_PROV_SIGN_ATOMIC(pd, session, mech, key, data, template, \
799 	    sig, req) ( \
800 	(KCF_PROV_SIGN_OPS(pd) && KCF_PROV_SIGN_OPS(pd)->sign_atomic) ? \
801 	KCF_PROV_SIGN_OPS(pd)->sign_atomic( \
802 	    (pd)->pd_prov_handle, session, mech, key, data, sig, template, \
803 	    req) : CRYPTO_NOT_SUPPORTED)
804 
805 #define	KCF_PROV_SIGN_RECOVER_INIT(pd, ctx, mech, key, template, \
806 	    req) ( \
807 	(KCF_PROV_SIGN_OPS(pd) && KCF_PROV_SIGN_OPS(pd)->sign_recover_init) ? \
808 	KCF_PROV_SIGN_OPS(pd)->sign_recover_init(ctx, mech, key, template, \
809 	    req) : CRYPTO_NOT_SUPPORTED)
810 
811 #define	KCF_PROV_SIGN_RECOVER(pd, ctx, data, sig, req) ( \
812 	(KCF_PROV_SIGN_OPS(pd) && KCF_PROV_SIGN_OPS(pd)->sign_recover) ? \
813 	KCF_PROV_SIGN_OPS(pd)->sign_recover(ctx, data, sig, req) : \
814 	CRYPTO_NOT_SUPPORTED)
815 
816 #define	KCF_PROV_SIGN_RECOVER_ATOMIC(pd, session, mech, key, data, template, \
817 	    sig, req) ( \
818 	(KCF_PROV_SIGN_OPS(pd) && \
819 	KCF_PROV_SIGN_OPS(pd)->sign_recover_atomic) ? \
820 	KCF_PROV_SIGN_OPS(pd)->sign_recover_atomic( \
821 	    (pd)->pd_prov_handle, session, mech, key, data, sig, template, \
822 	    req) : CRYPTO_NOT_SUPPORTED)
823 
824 /*
825  * Wrappers for crypto_verify_ops(9S) entry points.
826  */
827 
828 #define	KCF_PROV_VERIFY_INIT(pd, ctx, mech, key, template, req) ( \
829 	(KCF_PROV_VERIFY_OPS(pd) && KCF_PROV_VERIFY_OPS(pd)->verify_init) ? \
830 	KCF_PROV_VERIFY_OPS(pd)->verify_init(ctx, mech, key, template, \
831 	    req) : CRYPTO_NOT_SUPPORTED)
832 
833 #define	KCF_PROV_VERIFY(pd, ctx, data, sig, req) ( \
834 	(KCF_PROV_VERIFY_OPS(pd) && KCF_PROV_VERIFY_OPS(pd)->verify) ? \
835 	KCF_PROV_VERIFY_OPS(pd)->verify(ctx, data, sig, req) : \
836 	CRYPTO_NOT_SUPPORTED)
837 
838 #define	KCF_PROV_VERIFY_UPDATE(pd, ctx, data, req) ( \
839 	(KCF_PROV_VERIFY_OPS(pd) && KCF_PROV_VERIFY_OPS(pd)->verify_update) ? \
840 	KCF_PROV_VERIFY_OPS(pd)->verify_update(ctx, data, req) : \
841 	CRYPTO_NOT_SUPPORTED)
842 
843 #define	KCF_PROV_VERIFY_FINAL(pd, ctx, sig, req) ( \
844 	(KCF_PROV_VERIFY_OPS(pd) && KCF_PROV_VERIFY_OPS(pd)->verify_final) ? \
845 	KCF_PROV_VERIFY_OPS(pd)->verify_final(ctx, sig, req) : \
846 	CRYPTO_NOT_SUPPORTED)
847 
848 #define	KCF_PROV_VERIFY_ATOMIC(pd, session, mech, key, data, template, sig, \
849 	    req) ( \
850 	(KCF_PROV_VERIFY_OPS(pd) && KCF_PROV_VERIFY_OPS(pd)->verify_atomic) ? \
851 	KCF_PROV_VERIFY_OPS(pd)->verify_atomic( \
852 	    (pd)->pd_prov_handle, session, mech, key, data, sig, template, \
853 	    req) : CRYPTO_NOT_SUPPORTED)
854 
855 #define	KCF_PROV_VERIFY_RECOVER_INIT(pd, ctx, mech, key, template, \
856 	    req) ( \
857 	(KCF_PROV_VERIFY_OPS(pd) && \
858 	KCF_PROV_VERIFY_OPS(pd)->verify_recover_init) ? \
859 	KCF_PROV_VERIFY_OPS(pd)->verify_recover_init(ctx, mech, key, \
860 	    template, req) : CRYPTO_NOT_SUPPORTED)
861 
862 /* verify_recover() CSPI routine has different argument order than verify() */
863 #define	KCF_PROV_VERIFY_RECOVER(pd, ctx, sig, data, req) ( \
864 	(KCF_PROV_VERIFY_OPS(pd) && KCF_PROV_VERIFY_OPS(pd)->verify_recover) ? \
865 	KCF_PROV_VERIFY_OPS(pd)->verify_recover(ctx, sig, data, req) : \
866 	CRYPTO_NOT_SUPPORTED)
867 
868 /*
869  * verify_recover_atomic() CSPI routine has different argument order
870  * than verify_atomic().
871  */
872 #define	KCF_PROV_VERIFY_RECOVER_ATOMIC(pd, session, mech, key, sig, \
873 	    template, data,  req) ( \
874 	(KCF_PROV_VERIFY_OPS(pd) && \
875 	KCF_PROV_VERIFY_OPS(pd)->verify_recover_atomic) ? \
876 	KCF_PROV_VERIFY_OPS(pd)->verify_recover_atomic( \
877 	    (pd)->pd_prov_handle, session, mech, key, sig, data, template, \
878 	    req) : CRYPTO_NOT_SUPPORTED)
879 
880 /*
881  * Wrappers for crypto_dual_ops(9S) entry points.
882  */
883 
884 #define	KCF_PROV_DIGEST_ENCRYPT_UPDATE(digest_ctx, encrypt_ctx, plaintext, \
885 	    ciphertext, req) ( \
886 	(KCF_PROV_DUAL_OPS(pd) && \
887 	KCF_PROV_DUAL_OPS(pd)->digest_encrypt_update) ? \
888 	KCF_PROV_DUAL_OPS(pd)->digest_encrypt_update( \
889 	    digest_ctx, encrypt_ctx, plaintext, ciphertext, req) : \
890 	CRYPTO_NOT_SUPPORTED)
891 
892 #define	KCF_PROV_DECRYPT_DIGEST_UPDATE(decrypt_ctx, digest_ctx, ciphertext, \
893 	    plaintext, req) ( \
894 	(KCF_PROV_DUAL_OPS(pd) && \
895 	KCF_PROV_DUAL_OPS(pd)->decrypt_digest_update) ? \
896 	KCF_PROV_DUAL_OPS(pd)->decrypt_digest_update( \
897 	    decrypt_ctx, digest_ctx, ciphertext, plaintext, req) : \
898 	CRYPTO_NOT_SUPPORTED)
899 
900 #define	KCF_PROV_SIGN_ENCRYPT_UPDATE(sign_ctx, encrypt_ctx, plaintext, \
901 	    ciphertext, req) ( \
902 	(KCF_PROV_DUAL_OPS(pd) && \
903 	KCF_PROV_DUAL_OPS(pd)->sign_encrypt_update) ? \
904 	KCF_PROV_DUAL_OPS(pd)->sign_encrypt_update( \
905 	    sign_ctx, encrypt_ctx, plaintext, ciphertext, req) : \
906 	CRYPTO_NOT_SUPPORTED)
907 
908 #define	KCF_PROV_DECRYPT_VERIFY_UPDATE(decrypt_ctx, verify_ctx, ciphertext, \
909 	    plaintext, req) ( \
910 	(KCF_PROV_DUAL_OPS(pd) && \
911 	KCF_PROV_DUAL_OPS(pd)->decrypt_verify_update) ? \
912 	KCF_PROV_DUAL_OPS(pd)->decrypt_verify_update( \
913 	    decrypt_ctx, verify_ctx, ciphertext, plaintext, req) : \
914 	CRYPTO_NOT_SUPPORTED)
915 
916 /*
917  * Wrappers for crypto_dual_cipher_mac_ops(9S) entry points.
918  */
919 
920 #define	KCF_PROV_ENCRYPT_MAC_INIT(pd, ctx, encr_mech, encr_key, mac_mech, \
921 	    mac_key, encr_ctx_template, mac_ctx_template, req) ( \
922 	(KCF_PROV_DUAL_CIPHER_MAC_OPS(pd) && \
923 	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->encrypt_mac_init) ? \
924 	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->encrypt_mac_init( \
925 	    ctx, encr_mech, encr_key, mac_mech, mac_key, encr_ctx_template, \
926 	    mac_ctx_template, req) : \
927 	CRYPTO_NOT_SUPPORTED)
928 
929 #define	KCF_PROV_ENCRYPT_MAC(pd, ctx, plaintext, ciphertext, mac, req) ( \
930 	(KCF_PROV_DUAL_CIPHER_MAC_OPS(pd) && \
931 	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->encrypt_mac) ? \
932 	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->encrypt_mac( \
933 	    ctx, plaintext, ciphertext, mac, req) : \
934 	CRYPTO_NOT_SUPPORTED)
935 
936 #define	KCF_PROV_ENCRYPT_MAC_UPDATE(pd, ctx, plaintext, ciphertext, req) ( \
937 	(KCF_PROV_DUAL_CIPHER_MAC_OPS(pd) && \
938 	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->encrypt_mac_update) ? \
939 	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->encrypt_mac_update( \
940 	    ctx, plaintext, ciphertext, req) : \
941 	CRYPTO_NOT_SUPPORTED)
942 
943 #define	KCF_PROV_ENCRYPT_MAC_FINAL(pd, ctx, ciphertext, mac, req) ( \
944 	(KCF_PROV_DUAL_CIPHER_MAC_OPS(pd) && \
945 	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->encrypt_mac_final) ? \
946 	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->encrypt_mac_final( \
947 	    ctx, ciphertext, mac, req) : \
948 	CRYPTO_NOT_SUPPORTED)
949 
950 #define	KCF_PROV_ENCRYPT_MAC_ATOMIC(pd, session, encr_mech, encr_key, \
951 	    mac_mech, mac_key, plaintext, ciphertext, mac, \
952 	    encr_ctx_template, mac_ctx_template, req) ( \
953 	(KCF_PROV_DUAL_CIPHER_MAC_OPS(pd) && \
954 	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->encrypt_mac_atomic) ? \
955 	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->encrypt_mac_atomic( \
956 	    (pd)->pd_prov_handle, session, encr_mech, encr_key, \
957 	    mac_mech, mac_key, plaintext, ciphertext, mac, \
958 	    encr_ctx_template, mac_ctx_template, req) : \
959 	CRYPTO_NOT_SUPPORTED)
960 
961 #define	KCF_PROV_MAC_DECRYPT_INIT(pd, ctx, mac_mech, mac_key, decr_mech, \
962 	    decr_key, mac_ctx_template, decr_ctx_template, req) ( \
963 	(KCF_PROV_DUAL_CIPHER_MAC_OPS(pd) && \
964 	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->mac_decrypt_init) ? \
965 	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->mac_decrypt_init( \
966 	    ctx, mac_mech, mac_key, decr_mech, decr_key, mac_ctx_template, \
967 	    decr_ctx_template, req) : \
968 	CRYPTO_NOT_SUPPORTED)
969 
970 #define	KCF_PROV_MAC_DECRYPT(pd, ctx, ciphertext, mac, plaintext, req) ( \
971 	(KCF_PROV_DUAL_CIPHER_MAC_OPS(pd) && \
972 	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->mac_decrypt) ? \
973 	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->mac_decrypt( \
974 	    ctx, ciphertext, mac, plaintext, req) : \
975 	CRYPTO_NOT_SUPPORTED)
976 
977 #define	KCF_PROV_MAC_DECRYPT_UPDATE(pd, ctx, ciphertext, plaintext, req) ( \
978 	(KCF_PROV_DUAL_CIPHER_MAC_OPS(pd) && \
979 	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->mac_decrypt_update) ? \
980 	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->mac_decrypt_update( \
981 	    ctx, ciphertext, plaintext, req) : \
982 	CRYPTO_NOT_SUPPORTED)
983 
984 #define	KCF_PROV_MAC_DECRYPT_FINAL(pd, ctx, mac, plaintext, req) ( \
985 	(KCF_PROV_DUAL_CIPHER_MAC_OPS(pd) && \
986 	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->mac_decrypt_final) ? \
987 	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->mac_decrypt_final( \
988 	    ctx, mac, plaintext, req) : \
989 	CRYPTO_NOT_SUPPORTED)
990 
991 #define	KCF_PROV_MAC_DECRYPT_ATOMIC(pd, session, mac_mech, mac_key, \
992 	    decr_mech, decr_key, ciphertext, mac, plaintext, \
993 	    mac_ctx_template, decr_ctx_template, req) ( \
994 	(KCF_PROV_DUAL_CIPHER_MAC_OPS(pd) && \
995 	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->mac_decrypt_atomic) ? \
996 	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->mac_decrypt_atomic( \
997 	    (pd)->pd_prov_handle, session, mac_mech, mac_key, \
998 	    decr_mech, decr_key, ciphertext, mac, plaintext, \
999 	    mac_ctx_template, decr_ctx_template, req) : \
1000 	CRYPTO_NOT_SUPPORTED)
1001 
1002 #define	KCF_PROV_MAC_VERIFY_DECRYPT_ATOMIC(pd, session, mac_mech, mac_key, \
1003 	    decr_mech, decr_key, ciphertext, mac, plaintext, \
1004 	    mac_ctx_template, decr_ctx_template, req) ( \
1005 	(KCF_PROV_DUAL_CIPHER_MAC_OPS(pd) && \
1006 	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->mac_verify_decrypt_atomic \
1007 	    != NULL) ? \
1008 	KCF_PROV_DUAL_CIPHER_MAC_OPS(pd)->mac_verify_decrypt_atomic( \
1009 	    (pd)->pd_prov_handle, session, mac_mech, mac_key, \
1010 	    decr_mech, decr_key, ciphertext, mac, plaintext, \
1011 	    mac_ctx_template, decr_ctx_template, req) : \
1012 	CRYPTO_NOT_SUPPORTED)
1013 
1014 /*
1015  * Wrappers for crypto_random_number_ops(9S) entry points.
1016  */
1017 
1018 #define	KCF_PROV_SEED_RANDOM(pd, session, buf, len, est, flags, req) ( \
1019 	(KCF_PROV_RANDOM_OPS(pd) && KCF_PROV_RANDOM_OPS(pd)->seed_random) ? \
1020 	KCF_PROV_RANDOM_OPS(pd)->seed_random((pd)->pd_prov_handle, \
1021 	    session, buf, len, est, flags, req) : CRYPTO_NOT_SUPPORTED)
1022 
1023 #define	KCF_PROV_GENERATE_RANDOM(pd, session, buf, len, req) ( \
1024 	(KCF_PROV_RANDOM_OPS(pd) && \
1025 	KCF_PROV_RANDOM_OPS(pd)->generate_random) ? \
1026 	KCF_PROV_RANDOM_OPS(pd)->generate_random((pd)->pd_prov_handle, \
1027 	    session, buf, len, req) : CRYPTO_NOT_SUPPORTED)
1028 
1029 /*
1030  * Wrappers for crypto_session_ops(9S) entry points.
1031  *
1032  * ops_pd is the provider descriptor that supplies the ops_vector.
1033  * pd is the descriptor that supplies the provider handle.
1034  * Only session open/close needs two handles.
1035  */
1036 
1037 #define	KCF_PROV_SESSION_OPEN(ops_pd, session, req, pd) ( \
1038 	(KCF_PROV_SESSION_OPS(ops_pd) && \
1039 	KCF_PROV_SESSION_OPS(ops_pd)->session_open) ? \
1040 	KCF_PROV_SESSION_OPS(ops_pd)->session_open((pd)->pd_prov_handle, \
1041 	    session, req) : CRYPTO_NOT_SUPPORTED)
1042 
1043 #define	KCF_PROV_SESSION_CLOSE(ops_pd, session, req, pd) ( \
1044 	(KCF_PROV_SESSION_OPS(ops_pd) && \
1045 	KCF_PROV_SESSION_OPS(ops_pd)->session_close) ? \
1046 	KCF_PROV_SESSION_OPS(ops_pd)->session_close((pd)->pd_prov_handle, \
1047 	    session, req) : CRYPTO_NOT_SUPPORTED)
1048 
1049 #define	KCF_PROV_SESSION_LOGIN(pd, session, user_type, pin, len, req) ( \
1050 	(KCF_PROV_SESSION_OPS(pd) && \
1051 	KCF_PROV_SESSION_OPS(pd)->session_login) ? \
1052 	KCF_PROV_SESSION_OPS(pd)->session_login((pd)->pd_prov_handle, \
1053 	    session, user_type, pin, len, req) : CRYPTO_NOT_SUPPORTED)
1054 
1055 #define	KCF_PROV_SESSION_LOGOUT(pd, session, req) ( \
1056 	(KCF_PROV_SESSION_OPS(pd) && \
1057 	KCF_PROV_SESSION_OPS(pd)->session_logout) ? \
1058 	KCF_PROV_SESSION_OPS(pd)->session_logout((pd)->pd_prov_handle, \
1059 	    session, req) : CRYPTO_NOT_SUPPORTED)
1060 
1061 /*
1062  * Wrappers for crypto_object_ops(9S) entry points.
1063  */
1064 
1065 #define	KCF_PROV_OBJECT_CREATE(pd, session, template, count, object, req) ( \
1066 	(KCF_PROV_OBJECT_OPS(pd) && KCF_PROV_OBJECT_OPS(pd)->object_create) ? \
1067 	KCF_PROV_OBJECT_OPS(pd)->object_create((pd)->pd_prov_handle, \
1068 	    session, template, count, object, req) : CRYPTO_NOT_SUPPORTED)
1069 
1070 #define	KCF_PROV_OBJECT_COPY(pd, session, object, template, count, \
1071 	    new_object, req) ( \
1072 	(KCF_PROV_OBJECT_OPS(pd) && KCF_PROV_OBJECT_OPS(pd)->object_copy) ? \
1073 	KCF_PROV_OBJECT_OPS(pd)->object_copy((pd)->pd_prov_handle, \
1074 	session, object, template, count, new_object, req) : \
1075 	    CRYPTO_NOT_SUPPORTED)
1076 
1077 #define	KCF_PROV_OBJECT_DESTROY(pd, session, object, req) ( \
1078 	(KCF_PROV_OBJECT_OPS(pd) && KCF_PROV_OBJECT_OPS(pd)->object_destroy) ? \
1079 	KCF_PROV_OBJECT_OPS(pd)->object_destroy((pd)->pd_prov_handle, \
1080 	    session, object, req) : CRYPTO_NOT_SUPPORTED)
1081 
1082 #define	KCF_PROV_OBJECT_GET_SIZE(pd, session, object, size, req) ( \
1083 	(KCF_PROV_OBJECT_OPS(pd) && \
1084 	KCF_PROV_OBJECT_OPS(pd)->object_get_size) ? \
1085 	KCF_PROV_OBJECT_OPS(pd)->object_get_size((pd)->pd_prov_handle, \
1086 	    session, object, size, req) : CRYPTO_NOT_SUPPORTED)
1087 
1088 #define	KCF_PROV_OBJECT_GET_ATTRIBUTE_VALUE(pd, session, object, template, \
1089 	    count, req) ( \
1090 	(KCF_PROV_OBJECT_OPS(pd) && \
1091 	KCF_PROV_OBJECT_OPS(pd)->object_get_attribute_value) ? \
1092 	KCF_PROV_OBJECT_OPS(pd)->object_get_attribute_value( \
1093 	(pd)->pd_prov_handle, session, object, template, count, req) : \
1094 	    CRYPTO_NOT_SUPPORTED)
1095 
1096 #define	KCF_PROV_OBJECT_SET_ATTRIBUTE_VALUE(pd, session, object, template, \
1097 	    count, req) ( \
1098 	(KCF_PROV_OBJECT_OPS(pd) && \
1099 	KCF_PROV_OBJECT_OPS(pd)->object_set_attribute_value) ? \
1100 	KCF_PROV_OBJECT_OPS(pd)->object_set_attribute_value( \
1101 	(pd)->pd_prov_handle, session, object, template, count, req) : \
1102 	    CRYPTO_NOT_SUPPORTED)
1103 
1104 #define	KCF_PROV_OBJECT_FIND_INIT(pd, session, template, count, ppriv, \
1105 	    req) ( \
1106 	(KCF_PROV_OBJECT_OPS(pd) && \
1107 	KCF_PROV_OBJECT_OPS(pd)->object_find_init) ? \
1108 	KCF_PROV_OBJECT_OPS(pd)->object_find_init((pd)->pd_prov_handle, \
1109 	session, template, count, ppriv, req) : CRYPTO_NOT_SUPPORTED)
1110 
1111 #define	KCF_PROV_OBJECT_FIND(pd, ppriv, objects, max_objects, object_count, \
1112 	    req) ( \
1113 	(KCF_PROV_OBJECT_OPS(pd) && KCF_PROV_OBJECT_OPS(pd)->object_find) ? \
1114 	KCF_PROV_OBJECT_OPS(pd)->object_find( \
1115 	(pd)->pd_prov_handle, ppriv, objects, max_objects, object_count, \
1116 	req) : CRYPTO_NOT_SUPPORTED)
1117 
1118 #define	KCF_PROV_OBJECT_FIND_FINAL(pd, ppriv, req) ( \
1119 	(KCF_PROV_OBJECT_OPS(pd) && \
1120 	KCF_PROV_OBJECT_OPS(pd)->object_find_final) ? \
1121 	KCF_PROV_OBJECT_OPS(pd)->object_find_final( \
1122 	    (pd)->pd_prov_handle, ppriv, req) : CRYPTO_NOT_SUPPORTED)
1123 
1124 /*
1125  * Wrappers for crypto_key_ops(9S) entry points.
1126  */
1127 
1128 #define	KCF_PROV_KEY_GENERATE(pd, session, mech, template, count, object, \
1129 	    req) ( \
1130 	(KCF_PROV_KEY_OPS(pd) && KCF_PROV_KEY_OPS(pd)->key_generate) ? \
1131 	KCF_PROV_KEY_OPS(pd)->key_generate((pd)->pd_prov_handle, \
1132 	    session, mech, template, count, object, req) : \
1133 	CRYPTO_NOT_SUPPORTED)
1134 
1135 #define	KCF_PROV_KEY_GENERATE_PAIR(pd, session, mech, pub_template, \
1136 	    pub_count, priv_template, priv_count, pub_key, priv_key, req) ( \
1137 	(KCF_PROV_KEY_OPS(pd) && KCF_PROV_KEY_OPS(pd)->key_generate_pair) ? \
1138 	KCF_PROV_KEY_OPS(pd)->key_generate_pair((pd)->pd_prov_handle, \
1139 	    session, mech, pub_template, pub_count, priv_template, \
1140 	    priv_count, pub_key, priv_key, req) : \
1141 	CRYPTO_NOT_SUPPORTED)
1142 
1143 #define	KCF_PROV_KEY_WRAP(pd, session, mech, wrapping_key, key, wrapped_key, \
1144 	    wrapped_key_len, req) ( \
1145 	(KCF_PROV_KEY_OPS(pd) && KCF_PROV_KEY_OPS(pd)->key_wrap) ? \
1146 	KCF_PROV_KEY_OPS(pd)->key_wrap((pd)->pd_prov_handle, \
1147 	    session, mech, wrapping_key, key, wrapped_key, wrapped_key_len, \
1148 	    req) : \
1149 	CRYPTO_NOT_SUPPORTED)
1150 
1151 #define	KCF_PROV_KEY_UNWRAP(pd, session, mech, unwrapping_key, wrapped_key, \
1152 	    wrapped_key_len, template, count, key, req) ( \
1153 	(KCF_PROV_KEY_OPS(pd) && KCF_PROV_KEY_OPS(pd)->key_unwrap) ? \
1154 	KCF_PROV_KEY_OPS(pd)->key_unwrap((pd)->pd_prov_handle, \
1155 	    session, mech, unwrapping_key, wrapped_key, wrapped_key_len, \
1156 	    template, count, key, req) : \
1157 	CRYPTO_NOT_SUPPORTED)
1158 
1159 #define	KCF_PROV_KEY_DERIVE(pd, session, mech, base_key, template, count, \
1160 	    key, req) ( \
1161 	(KCF_PROV_KEY_OPS(pd) && KCF_PROV_KEY_OPS(pd)->key_derive) ? \
1162 	KCF_PROV_KEY_OPS(pd)->key_derive((pd)->pd_prov_handle, \
1163 	    session, mech, base_key, template, count, key, req) : \
1164 	CRYPTO_NOT_SUPPORTED)
1165 
1166 #define	KCF_PROV_KEY_CHECK(pd, mech, key) ( \
1167 	(KCF_PROV_KEY_OPS(pd) && KCF_PROV_KEY_OPS(pd)->key_check) ? \
1168 	KCF_PROV_KEY_OPS(pd)->key_check((pd)->pd_prov_handle, mech, key) : \
1169 	CRYPTO_NOT_SUPPORTED)
1170 
1171 /*
1172  * Wrappers for crypto_provider_management_ops(9S) entry points.
1173  *
1174  * ops_pd is the provider descriptor that supplies the ops_vector.
1175  * pd is the descriptor that supplies the provider handle.
1176  * Only ext_info needs two handles.
1177  */
1178 
1179 #define	KCF_PROV_EXT_INFO(ops_pd, provext_info, req, pd) ( \
1180 	(KCF_PROV_PROVIDER_OPS(ops_pd) && \
1181 	KCF_PROV_PROVIDER_OPS(ops_pd)->ext_info) ? \
1182 	KCF_PROV_PROVIDER_OPS(ops_pd)->ext_info((pd)->pd_prov_handle, \
1183 	    provext_info, req) : CRYPTO_NOT_SUPPORTED)
1184 
1185 #define	KCF_PROV_INIT_TOKEN(pd, pin, pin_len, label, req) ( \
1186 	(KCF_PROV_PROVIDER_OPS(pd) && KCF_PROV_PROVIDER_OPS(pd)->init_token) ? \
1187 	KCF_PROV_PROVIDER_OPS(pd)->init_token((pd)->pd_prov_handle, \
1188 	    pin, pin_len, label, req) : CRYPTO_NOT_SUPPORTED)
1189 
1190 #define	KCF_PROV_INIT_PIN(pd, session, pin, pin_len, req) ( \
1191 	(KCF_PROV_PROVIDER_OPS(pd) && KCF_PROV_PROVIDER_OPS(pd)->init_pin) ? \
1192 	KCF_PROV_PROVIDER_OPS(pd)->init_pin((pd)->pd_prov_handle, \
1193 	    session, pin, pin_len, req) : CRYPTO_NOT_SUPPORTED)
1194 
1195 #define	KCF_PROV_SET_PIN(pd, session, old_pin, old_len, new_pin, new_len, \
1196 	    req) ( \
1197 	(KCF_PROV_PROVIDER_OPS(pd) && KCF_PROV_PROVIDER_OPS(pd)->set_pin) ? \
1198 	KCF_PROV_PROVIDER_OPS(pd)->set_pin((pd)->pd_prov_handle, \
1199 	session, old_pin, old_len, new_pin, new_len, req) : \
1200 	    CRYPTO_NOT_SUPPORTED)
1201 
1202 /*
1203  * Wrappers for crypto_nostore_key_ops(9S) entry points.
1204  */
1205 
1206 #define	KCF_PROV_NOSTORE_KEY_GENERATE(pd, session, mech, template, count, \
1207 	    out_template, out_count, req) ( \
1208 	(KCF_PROV_NOSTORE_KEY_OPS(pd) && \
1209 	    KCF_PROV_NOSTORE_KEY_OPS(pd)->nostore_key_generate) ? \
1210 	KCF_PROV_NOSTORE_KEY_OPS(pd)->nostore_key_generate( \
1211 	    (pd)->pd_prov_handle, session, mech, template, count, \
1212 	    out_template, out_count, req) : CRYPTO_NOT_SUPPORTED)
1213 
1214 #define	KCF_PROV_NOSTORE_KEY_GENERATE_PAIR(pd, session, mech, pub_template, \
1215 	    pub_count, priv_template, priv_count, out_pub_template, \
1216 	    out_pub_count, out_priv_template, out_priv_count, req) ( \
1217 	(KCF_PROV_NOSTORE_KEY_OPS(pd) && \
1218 	    KCF_PROV_NOSTORE_KEY_OPS(pd)->nostore_key_generate_pair) ? \
1219 	KCF_PROV_NOSTORE_KEY_OPS(pd)->nostore_key_generate_pair( \
1220 	    (pd)->pd_prov_handle, session, mech, pub_template, pub_count, \
1221 	    priv_template, priv_count, out_pub_template, out_pub_count, \
1222 	    out_priv_template, out_priv_count, req) : CRYPTO_NOT_SUPPORTED)
1223 
1224 #define	KCF_PROV_NOSTORE_KEY_DERIVE(pd, session, mech, base_key, template, \
1225 	    count, out_template, out_count, req) ( \
1226 	(KCF_PROV_NOSTORE_KEY_OPS(pd) && \
1227 	    KCF_PROV_NOSTORE_KEY_OPS(pd)->nostore_key_derive) ? \
1228 	KCF_PROV_NOSTORE_KEY_OPS(pd)->nostore_key_derive( \
1229 	    (pd)->pd_prov_handle, session, mech, base_key, template, count, \
1230 	    out_template, out_count, req) : CRYPTO_NOT_SUPPORTED)
1231 
1232 /*
1233  * The following routines are exported by the kcf module (/kernel/misc/kcf)
1234  * to the crypto and cryptoadmin modules.
1235  */
1236 
1237 /* Digest/mac/cipher entry points that take a provider descriptor and session */
1238 extern int crypto_digest_single(crypto_context_t, crypto_data_t *,
1239     crypto_data_t *, crypto_call_req_t *);
1240 
1241 extern int crypto_mac_single(crypto_context_t, crypto_data_t *,
1242     crypto_data_t *, crypto_call_req_t *);
1243 
1244 extern int crypto_encrypt_single(crypto_context_t, crypto_data_t *,
1245     crypto_data_t *, crypto_call_req_t *);
1246 
1247 extern int crypto_decrypt_single(crypto_context_t, crypto_data_t *,
1248     crypto_data_t *, crypto_call_req_t *);
1249 
1250 
1251 /* Other private digest/mac/cipher entry points not exported through k-API */
1252 extern int crypto_digest_key_prov(crypto_context_t, crypto_key_t *,
1253     crypto_call_req_t *);
1254 
1255 /* Private sign entry points exported by KCF */
1256 extern int crypto_sign_single(crypto_context_t, crypto_data_t *,
1257     crypto_data_t *, crypto_call_req_t *);
1258 
1259 extern int crypto_sign_recover_single(crypto_context_t, crypto_data_t *,
1260     crypto_data_t *, crypto_call_req_t *);
1261 
1262 /* Private verify entry points exported by KCF */
1263 extern int crypto_verify_single(crypto_context_t, crypto_data_t *,
1264     crypto_data_t *, crypto_call_req_t *);
1265 
1266 extern int crypto_verify_recover_single(crypto_context_t, crypto_data_t *,
1267     crypto_data_t *, crypto_call_req_t *);
1268 
1269 /* Private dual operations entry points exported by KCF */
1270 extern int crypto_digest_encrypt_update(crypto_context_t, crypto_context_t,
1271     crypto_data_t *, crypto_data_t *, crypto_call_req_t *);
1272 extern int crypto_decrypt_digest_update(crypto_context_t, crypto_context_t,
1273     crypto_data_t *, crypto_data_t *, crypto_call_req_t *);
1274 extern int crypto_sign_encrypt_update(crypto_context_t, crypto_context_t,
1275     crypto_data_t *, crypto_data_t *, crypto_call_req_t *);
1276 extern int crypto_decrypt_verify_update(crypto_context_t, crypto_context_t,
1277     crypto_data_t *, crypto_data_t *, crypto_call_req_t *);
1278 
1279 /* Random Number Generation */
1280 int crypto_seed_random(crypto_provider_handle_t provider, uchar_t *buf,
1281     size_t len, crypto_call_req_t *req);
1282 int crypto_generate_random(crypto_provider_handle_t provider, uchar_t *buf,
1283     size_t len, crypto_call_req_t *req);
1284 
1285 /* Provider Management */
1286 int crypto_get_provider_info(crypto_provider_id_t id,
1287     crypto_provider_info_t **info, crypto_call_req_t *req);
1288 int crypto_get_provider_mechanisms(crypto_minor_t *, crypto_provider_id_t id,
1289     uint_t *count, crypto_mech_name_t **list);
1290 int crypto_init_token(crypto_provider_handle_t provider, char *pin,
1291     size_t pin_len, char *label, crypto_call_req_t *);
1292 int crypto_init_pin(crypto_provider_handle_t provider, char *pin,
1293     size_t pin_len, crypto_call_req_t *req);
1294 int crypto_set_pin(crypto_provider_handle_t provider, char *old_pin,
1295     size_t old_len, char *new_pin, size_t new_len, crypto_call_req_t *req);
1296 void crypto_free_provider_list(crypto_provider_entry_t *list, uint_t count);
1297 void crypto_free_provider_info(crypto_provider_info_t *info);
1298 
1299 /* Administrative */
1300 int crypto_get_dev_list(uint_t *count, crypto_dev_list_entry_t **list);
1301 int crypto_get_soft_list(uint_t *count, char **list, size_t *len);
1302 int crypto_get_dev_info(char *name, uint_t instance, uint_t *count,
1303     crypto_mech_name_t **list);
1304 int crypto_get_soft_info(caddr_t name, uint_t *count,
1305     crypto_mech_name_t **list);
1306 int crypto_load_dev_disabled(char *name, uint_t instance, uint_t count,
1307     crypto_mech_name_t *list);
1308 int crypto_load_soft_disabled(caddr_t name, uint_t count,
1309     crypto_mech_name_t *list);
1310 int crypto_unload_soft_module(caddr_t path);
1311 int crypto_load_soft_config(caddr_t name, uint_t count,
1312     crypto_mech_name_t *list);
1313 int crypto_load_door(uint_t did);
1314 void crypto_free_mech_list(crypto_mech_name_t *list, uint_t count);
1315 void crypto_free_dev_list(crypto_dev_list_entry_t *list, uint_t count);
1316 
1317 /* Miscellaneous */
1318 int crypto_get_mechanism_number(caddr_t name, crypto_mech_type_t *number);
1319 int crypto_get_function_list(crypto_provider_id_t id,
1320     crypto_function_list_t **list, int kmflag);
1321 void crypto_free_function_list(crypto_function_list_t *list);
1322 int crypto_build_permitted_mech_names(kcf_provider_desc_t *,
1323     crypto_mech_name_t **, uint_t *, int);
1324 extern void kcf_init_mech_tabs(void);
1325 extern int kcf_add_mech_provider(short, kcf_provider_desc_t *,
1326     kcf_prov_mech_desc_t **);
1327 extern void kcf_remove_mech_provider(char *, kcf_provider_desc_t *);
1328 extern int kcf_get_mech_entry(crypto_mech_type_t, kcf_mech_entry_t **);
1329 extern kcf_provider_desc_t *kcf_alloc_provider_desc(crypto_provider_info_t *);
1330 extern void kcf_free_provider_desc(kcf_provider_desc_t *);
1331 extern void kcf_soft_config_init(void);
1332 extern int get_sw_provider_for_mech(crypto_mech_name_t, char **);
1333 extern crypto_mech_type_t crypto_mech2id_common(char *, boolean_t);
1334 extern void undo_register_provider(kcf_provider_desc_t *, boolean_t);
1335 extern void redo_register_provider(kcf_provider_desc_t *);
1336 extern void kcf_rnd_init();
1337 extern boolean_t kcf_rngprov_check(void);
1338 extern int kcf_rnd_get_pseudo_bytes(uint8_t *, size_t);
1339 extern int kcf_rnd_get_bytes(uint8_t *, size_t, boolean_t);
1340 extern int random_add_pseudo_entropy(uint8_t *, size_t, uint_t);
1341 extern void kcf_rnd_chpoll(short, int, short *, struct pollhead **);
1342 extern void kcf_rnd_schedule_timeout(boolean_t);
1343 extern int crypto_uio_data(crypto_data_t *, uchar_t *, int, cmd_type_t,
1344     void *, void (*update)());
1345 extern int crypto_mblk_data(crypto_data_t *, uchar_t *, int, cmd_type_t,
1346     void *, void (*update)());
1347 extern int crypto_put_output_data(uchar_t *, crypto_data_t *, int);
1348 extern int crypto_get_input_data(crypto_data_t *, uchar_t **, uchar_t *);
1349 extern int crypto_copy_key_to_ctx(crypto_key_t *, crypto_key_t **, size_t *,
1350     int kmflag);
1351 extern int crypto_digest_data(crypto_data_t *, void *, uchar_t *,
1352     void (*update)(), void (*final)(), uchar_t);
1353 extern int crypto_update_iov(void *, crypto_data_t *, crypto_data_t *,
1354     int (*cipher)(void *, caddr_t, size_t, crypto_data_t *),
1355     void (*copy_block)(uint8_t *, uint64_t *));
1356 extern int crypto_update_uio(void *, crypto_data_t *, crypto_data_t *,
1357     int (*cipher)(void *, caddr_t, size_t, crypto_data_t *),
1358     void (*copy_block)(uint8_t *, uint64_t *));
1359 extern int crypto_update_mp(void *, crypto_data_t *, crypto_data_t *,
1360     int (*cipher)(void *, caddr_t, size_t, crypto_data_t *),
1361     void (*copy_block)(uint8_t *, uint64_t *));
1362 extern int crypto_get_key_attr(crypto_key_t *, crypto_attr_type_t, uchar_t **,
1363     ssize_t *);
1364 
1365 /* Access to the provider's table */
1366 extern void kcf_prov_tab_init(void);
1367 extern int kcf_prov_tab_add_provider(kcf_provider_desc_t *);
1368 extern int kcf_prov_tab_rem_provider(crypto_provider_id_t);
1369 extern kcf_provider_desc_t *kcf_prov_tab_lookup_by_name(char *);
1370 extern kcf_provider_desc_t *kcf_prov_tab_lookup_by_dev(char *, uint_t);
1371 extern int kcf_get_hw_prov_tab(uint_t *, kcf_provider_desc_t ***, int,
1372     char *, uint_t, boolean_t);
1373 extern int kcf_get_slot_list(uint_t *, kcf_provider_desc_t ***, boolean_t);
1374 extern void kcf_free_provider_tab(uint_t, kcf_provider_desc_t **);
1375 extern kcf_provider_desc_t *kcf_prov_tab_lookup(crypto_provider_id_t);
1376 extern int kcf_get_sw_prov(crypto_mech_type_t, kcf_provider_desc_t **,
1377     kcf_mech_entry_t **, boolean_t);
1378 
1379 extern kmutex_t prov_tab_mutex;
1380 extern boolean_t kcf_need_provtab_walk;
1381 extern void kcf_free_unregistered_provs();
1382 extern int kcf_get_refcnt(kcf_provider_desc_t *, boolean_t);
1383 
1384 /* Access to the policy table */
1385 extern boolean_t is_mech_disabled(kcf_provider_desc_t *, crypto_mech_name_t);
1386 extern boolean_t is_mech_disabled_byname(crypto_provider_type_t, char *,
1387     uint_t, crypto_mech_name_t);
1388 extern void kcf_policy_tab_init(void);
1389 extern void kcf_policy_free_desc(kcf_policy_desc_t *);
1390 extern void kcf_policy_remove_by_name(char *, uint_t *, crypto_mech_name_t **);
1391 extern void kcf_policy_remove_by_dev(char *, uint_t, uint_t *,
1392     crypto_mech_name_t **);
1393 extern kcf_policy_desc_t *kcf_policy_lookup_by_name(char *);
1394 extern kcf_policy_desc_t *kcf_policy_lookup_by_dev(char *, uint_t);
1395 extern int kcf_policy_load_soft_disabled(char *, uint_t, crypto_mech_name_t *,
1396     uint_t *, crypto_mech_name_t **);
1397 extern int kcf_policy_load_dev_disabled(char *, uint_t, uint_t,
1398     crypto_mech_name_t *, uint_t *, crypto_mech_name_t **);
1399 extern boolean_t in_soft_config_list(char *);
1400 
1401 #endif	/* _KERNEL */
1402 
1403 #ifdef	__cplusplus
1404 }
1405 #endif
1406 
1407 #endif	/* _SYS_CRYPTO_IMPL_H */
1408