xref: /titanic_41/usr/src/uts/common/sys/crypto/api.h (revision ba2e4443695ee6a6f420a35cd4fc3d3346d22932)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef	_SYS_CRYPTO_API_H
28 #define	_SYS_CRYPTO_API_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #ifdef	__cplusplus
33 extern "C" {
34 #endif
35 
36 #include <sys/types.h>
37 #include <sys/crypto/common.h>
38 
39 #ifdef	_KERNEL
40 
41 
42 typedef long crypto_req_id_t;
43 typedef void *crypto_bc_t;
44 typedef void *crypto_context_t;
45 typedef void *crypto_ctx_template_t;
46 
47 typedef uint32_t crypto_call_flag_t;
48 
49 /* crypto_call_flag's values */
50 #define	CRYPTO_ALWAYS_QUEUE	0x00000001	/* ALWAYS queue the req. */
51 #define	CRYPTO_NOTIFY_OPDONE	0x00000002	/* Notify intermediate steps */
52 #define	CRYPTO_SKIP_REQID	0x00000004	/* Skip request ID generation */
53 #define	CRYPTO_RESTRICTED	0x00000008	/* cannot use restricted prov */
54 
55 typedef struct {
56 	crypto_call_flag_t	cr_flag;
57 	void			(*cr_callback_func)(void *, int);
58 	void			*cr_callback_arg;
59 	crypto_req_id_t		cr_reqid;
60 } crypto_call_req_t;
61 
62 /*
63  * Returns the mechanism type corresponding to a mechanism name.
64  */
65 
66 #define	CRYPTO_MECH_INVALID	((uint64_t)-1)
67 extern crypto_mech_type_t crypto_mech2id(crypto_mech_name_t name);
68 
69 /*
70  * Create and destroy context templates.
71  */
72 extern int crypto_create_ctx_template(crypto_mechanism_t *mech,
73     crypto_key_t *key, crypto_ctx_template_t *tmpl, int kmflag);
74 extern void crypto_destroy_ctx_template(crypto_ctx_template_t tmpl);
75 
76 /*
77  * Single and multi-part digest operations.
78  */
79 extern int crypto_digest(crypto_mechanism_t *mech, crypto_data_t *data,
80     crypto_data_t *digest, crypto_call_req_t *cr);
81 extern int crypto_digest_prov(crypto_provider_t, crypto_session_id_t,
82     crypto_mechanism_t *, crypto_data_t *, crypto_data_t *,
83     crypto_call_req_t *);
84 extern int crypto_digest_init(crypto_mechanism_t *mech, crypto_context_t *ctxp,
85     crypto_call_req_t *cr);
86 extern int crypto_digest_init_prov(crypto_provider_t, crypto_session_id_t,
87     crypto_mechanism_t *, crypto_context_t *, crypto_call_req_t *);
88 extern int crypto_digest_update(crypto_context_t ctx, crypto_data_t *data,
89     crypto_call_req_t *cr);
90 extern int crypto_digest_final(crypto_context_t ctx, crypto_data_t *digest,
91     crypto_call_req_t *cr);
92 
93 /*
94  * Single and multi-part MAC operations.
95  */
96 extern int crypto_mac(crypto_mechanism_t *mech, crypto_data_t *data,
97     crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_data_t *mac,
98     crypto_call_req_t *cr);
99 extern int crypto_mac_prov(crypto_provider_t, crypto_session_id_t,
100     crypto_mechanism_t *, crypto_data_t *, crypto_key_t *,
101     crypto_ctx_template_t, crypto_data_t *, crypto_call_req_t *);
102 extern int crypto_mac_verify(crypto_mechanism_t *mech, crypto_data_t *data,
103     crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_data_t *mac,
104     crypto_call_req_t *cr);
105 extern int crypto_mac_verify_prov(crypto_provider_t, crypto_session_id_t,
106     crypto_mechanism_t *, crypto_data_t *, crypto_key_t *,
107     crypto_ctx_template_t, crypto_data_t *, crypto_call_req_t *);
108 extern int crypto_mac_init(crypto_mechanism_t *mech, crypto_key_t *key,
109     crypto_ctx_template_t tmpl, crypto_context_t *ctxp, crypto_call_req_t *cr);
110 extern int crypto_mac_init_prov(crypto_provider_t, crypto_session_id_t,
111     crypto_mechanism_t *, crypto_key_t *, crypto_ctx_template_t,
112     crypto_context_t *, crypto_call_req_t *);
113 extern int crypto_mac_update(crypto_context_t ctx, crypto_data_t *data,
114     crypto_call_req_t *cr);
115 extern int crypto_mac_final(crypto_context_t ctx, crypto_data_t *data,
116     crypto_call_req_t *cr);
117 
118 /*
119  * Single and multi-part sign with private key operations.
120  */
121 extern int crypto_sign(crypto_mechanism_t *mech, crypto_key_t *key,
122     crypto_data_t *data, crypto_ctx_template_t tmpl,
123     crypto_data_t *signature, crypto_call_req_t *cr);
124 extern int crypto_sign_prov(crypto_provider_t, crypto_session_id_t,
125     crypto_mechanism_t *, crypto_key_t *, crypto_data_t *,
126     crypto_ctx_template_t, crypto_data_t *, crypto_call_req_t *);
127 extern int crypto_sign_init(crypto_mechanism_t *mech, crypto_key_t *key,
128     crypto_ctx_template_t tmpl, crypto_context_t *ctxp, crypto_call_req_t *cr);
129 extern int crypto_sign_init_prov(crypto_provider_t, crypto_session_id_t,
130     crypto_mechanism_t *, crypto_key_t *, crypto_ctx_template_t,
131     crypto_context_t *, crypto_call_req_t *);
132 extern int crypto_sign_update(crypto_context_t ctx, crypto_data_t *data,
133     crypto_call_req_t *cr);
134 extern int crypto_sign_final(crypto_context_t ctx, crypto_data_t *signature,
135     crypto_call_req_t *cr);
136 extern int crypto_sign_recover_init_prov(crypto_provider_t,
137     crypto_session_id_t, crypto_mechanism_t *, crypto_key_t *,
138     crypto_ctx_template_t tmpl, crypto_context_t *, crypto_call_req_t *);
139 extern int crypto_sign_recover(crypto_mechanism_t *mech, crypto_key_t *key,
140     crypto_data_t *data, crypto_ctx_template_t tmpl, crypto_data_t *signature,
141     crypto_call_req_t *cr);
142 extern int crypto_sign_recover_prov(crypto_provider_t, crypto_session_id_t,
143     crypto_mechanism_t *, crypto_key_t *, crypto_data_t *,
144     crypto_ctx_template_t, crypto_data_t *, crypto_call_req_t *);
145 
146 /*
147  * Single and multi-part verify with public key operations.
148  */
149 extern int crypto_verify(crypto_mechanism_t *mech, crypto_key_t *key,
150     crypto_data_t *data, crypto_ctx_template_t tmpl, crypto_data_t *signature,
151     crypto_call_req_t *cr);
152 extern int crypto_verify_prov(crypto_provider_t, crypto_session_id_t,
153     crypto_mechanism_t *, crypto_key_t *, crypto_data_t *,
154     crypto_ctx_template_t, crypto_data_t *, crypto_call_req_t *);
155 extern int crypto_verify_init(crypto_mechanism_t *mech, crypto_key_t *key,
156     crypto_ctx_template_t tmpl, crypto_context_t *ctxp, crypto_call_req_t *cr);
157 extern int crypto_verify_init_prov(crypto_provider_t, crypto_session_id_t,
158     crypto_mechanism_t *, crypto_key_t *, crypto_ctx_template_t,
159     crypto_context_t *, crypto_call_req_t *);
160 extern int crypto_verify_update(crypto_context_t ctx, crypto_data_t *data,
161     crypto_call_req_t *cr);
162 extern int crypto_verify_final(crypto_context_t ctx, crypto_data_t *signature,
163     crypto_call_req_t *cr);
164 extern int crypto_verify_recover_init_prov(crypto_provider_t,
165     crypto_session_id_t, crypto_mechanism_t *, crypto_key_t *,
166     crypto_ctx_template_t tmpl, crypto_context_t *, crypto_call_req_t *);
167 extern int crypto_verify_recover(crypto_mechanism_t *mech, crypto_key_t *key,
168     crypto_data_t *signature, crypto_ctx_template_t tmpl, crypto_data_t *data,
169     crypto_call_req_t *cr);
170 extern int crypto_verify_recover_prov(crypto_provider_t, crypto_session_id_t,
171     crypto_mechanism_t *, crypto_key_t *, crypto_data_t *,
172     crypto_ctx_template_t, crypto_data_t *, crypto_call_req_t *);
173 
174 /*
175  * Single and multi-part encryption operations.
176  */
177 extern int crypto_encrypt(crypto_mechanism_t *mech, crypto_data_t *plaintext,
178     crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_data_t *ciphertext,
179     crypto_call_req_t *cr);
180 extern int crypto_encrypt_prov(crypto_provider_t, crypto_session_id_t,
181     crypto_mechanism_t *, crypto_data_t *, crypto_key_t *,
182     crypto_ctx_template_t, crypto_data_t *, crypto_call_req_t *);
183 extern int crypto_encrypt_init(crypto_mechanism_t *mech, crypto_key_t *key,
184     crypto_ctx_template_t tmpl, crypto_context_t *ctxp, crypto_call_req_t *cr);
185 extern int crypto_encrypt_init_prov(crypto_provider_t, crypto_session_id_t,
186     crypto_mechanism_t *, crypto_key_t *, crypto_ctx_template_t,
187     crypto_context_t *, crypto_call_req_t *);
188 extern int crypto_encrypt_update(crypto_context_t ctx,
189     crypto_data_t *plaintext, crypto_data_t *ciphertext,
190     crypto_call_req_t *cr);
191 extern int crypto_encrypt_final(crypto_context_t ctx,
192     crypto_data_t *ciphertext, crypto_call_req_t *cr);
193 
194 /*
195  * Single and multi-part decryption operations.
196  */
197 extern int crypto_decrypt(crypto_mechanism_t *mech, crypto_data_t *ciphertext,
198     crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_data_t *plaintext,
199     crypto_call_req_t *cr);
200 extern int crypto_decrypt_prov(crypto_provider_t, crypto_session_id_t,
201     crypto_mechanism_t *, crypto_data_t *, crypto_key_t *,
202     crypto_ctx_template_t, crypto_data_t *, crypto_call_req_t *);
203 extern int crypto_decrypt_init(crypto_mechanism_t *mech, crypto_key_t *key,
204     crypto_ctx_template_t tmpl, crypto_context_t *ctxp,
205     crypto_call_req_t *cr);
206 extern int crypto_decrypt_init_prov(crypto_provider_t, crypto_session_id_t,
207     crypto_mechanism_t *, crypto_key_t *, crypto_ctx_template_t,
208     crypto_context_t *, crypto_call_req_t *);
209 extern int crypto_decrypt_update(crypto_context_t ctx,
210     crypto_data_t *ciphertext, crypto_data_t *plaintext,
211     crypto_call_req_t *cr);
212 extern int crypto_decrypt_final(crypto_context_t ctx, crypto_data_t *plaintext,
213     crypto_call_req_t *cr);
214 
215 /*
216  * Single and multi-part encrypt/MAC dual operations.
217  */
218 extern int crypto_encrypt_mac(crypto_mechanism_t *encr_mech,
219     crypto_mechanism_t *mac_mech, crypto_data_t *pt,
220     crypto_key_t *encr_key, crypto_key_t *mac_key,
221     crypto_ctx_template_t encr_tmpl, crypto_ctx_template_t mac_tmpl,
222     crypto_dual_data_t *ct, crypto_data_t *mac, crypto_call_req_t *cr);
223 extern int crypto_encrypt_mac_prov(crypto_provider_t, crypto_session_id_t,
224     crypto_mechanism_t *, crypto_mechanism_t *, crypto_data_t *,
225     crypto_key_t *, crypto_key_t *, crypto_ctx_template_t,
226     crypto_ctx_template_t, crypto_dual_data_t *, crypto_data_t *,
227     crypto_call_req_t *);
228 extern int crypto_encrypt_mac_init(crypto_mechanism_t *encr_mech,
229     crypto_mechanism_t *mac_mech, crypto_key_t *encr_key,
230     crypto_key_t *mac_key, crypto_ctx_template_t encr_tmpl,
231     crypto_ctx_template_t mac_tmpl, crypto_context_t *ctxp,
232     crypto_call_req_t *cr);
233 extern int crypto_encrypt_mac_init_prov(crypto_provider_t, crypto_session_id_t,
234     crypto_mechanism_t *, crypto_mechanism_t *, crypto_key_t *, crypto_key_t *,
235     crypto_ctx_template_t, crypto_ctx_template_t, crypto_context_t *,
236     crypto_call_req_t *);
237 extern int crypto_encrypt_mac_update(crypto_context_t ctx,
238     crypto_data_t *pt, crypto_dual_data_t *ct, crypto_call_req_t *cr);
239 extern int crypto_encrypt_mac_final(crypto_context_t ctx,
240     crypto_dual_data_t *ct, crypto_data_t *mac, crypto_call_req_t *cr);
241 
242 /*
243  * Single and multi-part MAC/decrypt dual operations.
244  */
245 extern int crypto_mac_decrypt(crypto_mechanism_t *mac_mech,
246     crypto_mechanism_t *decr_mech, crypto_dual_data_t *ct,
247     crypto_key_t *mac_key, crypto_key_t *decr_key,
248     crypto_ctx_template_t mac_tmpl, crypto_ctx_template_t decr_tmpl,
249     crypto_data_t *mac, crypto_data_t *pt, crypto_call_req_t *cr);
250 extern int crypto_mac_decrypt_prov(crypto_provider_t, crypto_session_id_t,
251     crypto_mechanism_t *mac_mech, crypto_mechanism_t *decr_mech,
252     crypto_dual_data_t *ct, crypto_key_t *mac_key, crypto_key_t *decr_key,
253     crypto_ctx_template_t mac_tmpl, crypto_ctx_template_t decr_tmpl,
254     crypto_data_t *mac, crypto_data_t *pt, crypto_call_req_t *cr);
255 extern int crypto_mac_verify_decrypt(crypto_mechanism_t *mac_mech,
256     crypto_mechanism_t *decr_mech, crypto_dual_data_t *ct,
257     crypto_key_t *mac_key, crypto_key_t *decr_key,
258     crypto_ctx_template_t mac_tmpl, crypto_ctx_template_t decr_tmpl,
259     crypto_data_t *mac, crypto_data_t *pt, crypto_call_req_t *cr);
260 extern int crypto_mac_verify_decrypt_prov(crypto_provider_t,
261     crypto_session_id_t, crypto_mechanism_t *mac_mech,
262     crypto_mechanism_t *decr_mech, crypto_dual_data_t *ct,
263     crypto_key_t *mac_key, crypto_key_t *decr_key,
264     crypto_ctx_template_t mac_tmpl, crypto_ctx_template_t decr_tmpl,
265     crypto_data_t *mac, crypto_data_t *pt, crypto_call_req_t *cr);
266 extern int crypto_mac_decrypt_init(crypto_mechanism_t *mac_mech,
267     crypto_mechanism_t *decr_mech, crypto_key_t *mac_key,
268     crypto_key_t *decr_key, crypto_ctx_template_t mac_tmpl,
269     crypto_ctx_template_t decr_tmpl, crypto_context_t *ctxp,
270     crypto_call_req_t *cr);
271 extern int crypto_mac_decrypt_init_prov(crypto_provider_t,
272     crypto_session_id_t, crypto_mechanism_t *mac_mech,
273     crypto_mechanism_t *decr_mech, crypto_key_t *mac_key,
274     crypto_key_t *decr_key, crypto_ctx_template_t mac_tmpl,
275     crypto_ctx_template_t decr_tmpl, crypto_context_t *ctxp,
276     crypto_call_req_t *cr);
277 extern int crypto_mac_decrypt_update(crypto_context_t ctx,
278     crypto_dual_data_t *ct, crypto_data_t *pt, crypto_call_req_t *cr);
279 extern int crypto_mac_decrypt_final(crypto_context_t ctx, crypto_data_t *mac,
280     crypto_data_t *pt, crypto_call_req_t *cr);
281 
282 /* Session Management */
283 extern int crypto_session_open(crypto_provider_t, crypto_session_id_t *,
284     crypto_call_req_t *);
285 extern int crypto_session_close(crypto_provider_t, crypto_session_id_t,
286     crypto_call_req_t *);
287 extern int crypto_session_login(crypto_provider_t, crypto_session_id_t,
288     crypto_user_type_t, char *, size_t, crypto_call_req_t *);
289 extern int crypto_session_logout(crypto_provider_t, crypto_session_id_t,
290     crypto_call_req_t *);
291 
292 /* Object Management */
293 extern int crypto_object_copy(crypto_provider_t, crypto_session_id_t,
294     crypto_object_id_t, crypto_object_attribute_t *, uint_t,
295     crypto_object_id_t *, crypto_call_req_t *);
296 extern int crypto_object_create(crypto_provider_t, crypto_session_id_t,
297     crypto_object_attribute_t *, uint_t, crypto_object_id_t *,
298     crypto_call_req_t *);
299 extern int crypto_object_destroy(crypto_provider_t, crypto_session_id_t,
300     crypto_object_id_t, crypto_call_req_t *);
301 extern int crypto_object_get_attribute_value(crypto_provider_t,
302     crypto_session_id_t, crypto_object_id_t, crypto_object_attribute_t *,
303     uint_t, crypto_call_req_t *);
304 extern int crypto_object_get_size(crypto_provider_t, crypto_session_id_t,
305     crypto_object_id_t, size_t *, crypto_call_req_t *);
306 extern int crypto_object_find_final(crypto_provider_t, void *,
307     crypto_call_req_t *);
308 extern int crypto_object_find_init(crypto_provider_t, crypto_session_id_t,
309     crypto_object_attribute_t *, uint_t, void **, crypto_call_req_t *);
310 extern int crypto_object_find(crypto_provider_t, void *, crypto_object_id_t *,
311     uint_t *, uint_t, crypto_call_req_t *);
312 extern int crypto_object_set_attribute_value(crypto_provider_t,
313     crypto_session_id_t, crypto_object_id_t, crypto_object_attribute_t *,
314     uint_t, crypto_call_req_t *);
315 
316 /* Key Management */
317 extern int crypto_key_derive(crypto_provider_t, crypto_session_id_t,
318     crypto_mechanism_t *, crypto_key_t *, crypto_object_attribute_t *,
319     uint_t, crypto_object_id_t *, crypto_call_req_t *);
320 extern int crypto_key_generate(crypto_provider_t, crypto_session_id_t,
321     crypto_mechanism_t *, crypto_object_attribute_t *, uint_t,
322     crypto_object_id_t *, crypto_call_req_t *);
323 extern int crypto_key_generate_pair(crypto_provider_t, crypto_session_id_t,
324     crypto_mechanism_t *, crypto_object_attribute_t *, uint_t,
325     crypto_object_attribute_t *, uint_t, crypto_object_id_t *,
326     crypto_object_id_t *, crypto_call_req_t *);
327 extern int crypto_key_unwrap(crypto_provider_t, crypto_session_id_t,
328     crypto_mechanism_t *, crypto_key_t *, uchar_t *, size_t *,
329     crypto_object_attribute_t *, uint_t, crypto_object_id_t *,
330     crypto_call_req_t *);
331 extern int crypto_key_wrap(crypto_provider_t, crypto_session_id_t,
332     crypto_mechanism_t *, crypto_key_t *, crypto_object_id_t *, uchar_t *,
333     size_t *, crypto_call_req_t *);
334 extern int crypto_key_check_prov(crypto_provider_t, crypto_mechanism_t *mech,
335     crypto_key_t *key);
336 extern int crypto_key_check(crypto_mechanism_t *mech, crypto_key_t *key);
337 
338 
339 /*
340  * Routines to cancel a single asynchronous request or all asynchronous
341  * requests associated with a particular context.
342  */
343 extern void crypto_cancel_req(crypto_req_id_t req);
344 extern void crypto_cancel_ctx(crypto_context_t ctx);
345 
346 /*
347  * crypto_get_mech_list(9F) allocates and returns the list of currently
348  * supported cryptographic mechanisms.
349  */
350 extern crypto_mech_name_t *crypto_get_mech_list(uint_t *count, int kmflag);
351 extern void crypto_free_mech_list(crypto_mech_name_t *mech_names,
352     uint_t count);
353 
354 extern crypto_provider_t crypto_get_provider(char *, char *, char *);
355 extern void crypto_release_provider(crypto_provider_t);
356 
357 /*
358  * A kernel consumer can request to be notified when some particular event
359  * occurs. The valid events, callback function type, and functions to
360  * be called to register or unregister for notification are defined below.
361  */
362 
363 #define	CRYPTO_EVENT_PROVIDERS_CHANGE	0x00000001
364 
365 typedef enum crypto_provider_type {
366 	CRYPTO_EVENT_CHANGE_ADDED = 1,
367 	CRYPTO_EVENT_CHANGE_REMOVED
368 } crypto_event_change_t;
369 
370 /* The event_arg argument structure for CRYPTO_EVENT_PROVIDERS_CHANGE event */
371 typedef struct crypto_notify_event_change {
372 	crypto_mech_name_t ec_mech_name;
373 	crypto_provider_type_t ec_provider_type;
374 	crypto_event_change_t ec_change;
375 } crypto_notify_event_change_t;
376 
377 typedef void *crypto_notify_handle_t;
378 typedef void (*crypto_notify_callback_t)(uint32_t event_mask, void *event_arg);
379 
380 extern crypto_notify_handle_t crypto_notify_events(
381     crypto_notify_callback_t nf, uint32_t event_mask);
382 extern void crypto_unnotify_events(crypto_notify_handle_t);
383 
384 /*
385  * crypto_bufcall(9F) group of routines.
386  */
387 extern crypto_bc_t crypto_bufcall_alloc(void);
388 extern int crypto_bufcall_free(crypto_bc_t bc);
389 extern int crypto_bufcall(crypto_bc_t bc, void (*func)(void *arg), void *arg);
390 extern int crypto_unbufcall(crypto_bc_t bc);
391 
392 /*
393  * To obtain the list of key size ranges supported by a mechanism.
394  */
395 
396 #define	CRYPTO_MECH_USAGE_ENCRYPT	0x00000001
397 #define	CRYPTO_MECH_USAGE_DECRYPT	0x00000002
398 #define	CRYPTO_MECH_USAGE_MAC		0x00000004
399 
400 typedef	uint32_t crypto_mech_usage_t;
401 
402 typedef struct crypto_mechanism_info {
403 	size_t mi_min_key_size;
404 	size_t mi_max_key_size;
405 	crypto_keysize_unit_t mi_keysize_unit; /* for mi_xxx_key_size */
406 	crypto_mech_usage_t mi_usage;
407 } crypto_mechanism_info_t;
408 
409 extern int crypto_get_all_mech_info(crypto_mech_type_t,
410     crypto_mechanism_info_t **, uint_t *, int);
411 
412 #endif	/* _KERNEL */
413 
414 #ifdef	__cplusplus
415 }
416 #endif
417 
418 #endif	/* _SYS_CRYPTO_API_H */
419