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 (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. 23 */ 24 25 #ifndef _SYS_CRYPTO_API_H 26 #define _SYS_CRYPTO_API_H 27 28 #ifdef __cplusplus 29 extern "C" { 30 #endif 31 32 #include <sys/types.h> 33 #include <sys/crypto/common.h> 34 35 #ifdef _KERNEL 36 37 38 typedef long crypto_req_id_t; 39 typedef void *crypto_bc_t; 40 typedef void *crypto_context_t; 41 typedef void *crypto_ctx_template_t; 42 43 typedef uint32_t crypto_call_flag_t; 44 45 /* crypto_call_flag's values */ 46 #define CRYPTO_ALWAYS_QUEUE 0x00000001 /* ALWAYS queue the req. */ 47 #define CRYPTO_NOTIFY_OPDONE 0x00000002 /* Notify intermediate steps */ 48 #define CRYPTO_SKIP_REQID 0x00000004 /* Skip request ID generation */ 49 50 typedef struct { 51 crypto_call_flag_t cr_flag; 52 void (*cr_callback_func)(void *, int); 53 void *cr_callback_arg; 54 crypto_req_id_t cr_reqid; 55 } crypto_call_req_t; 56 57 /* 58 * Returns the mechanism type corresponding to a mechanism name. 59 */ 60 61 #define CRYPTO_MECH_INVALID ((uint64_t)-1) 62 extern crypto_mech_type_t crypto_mech2id(crypto_mech_name_t name); 63 64 /* 65 * Create and destroy context templates. 66 */ 67 extern int crypto_create_ctx_template(crypto_mechanism_t *mech, 68 crypto_key_t *key, crypto_ctx_template_t *tmpl, int kmflag); 69 extern void crypto_destroy_ctx_template(crypto_ctx_template_t tmpl); 70 71 /* 72 * Single and multi-part digest operations. 73 */ 74 extern int crypto_digest(crypto_mechanism_t *mech, crypto_data_t *data, 75 crypto_data_t *digest, crypto_call_req_t *cr); 76 extern int crypto_digest_prov(crypto_provider_t, crypto_session_id_t, 77 crypto_mechanism_t *, crypto_data_t *, crypto_data_t *, 78 crypto_call_req_t *); 79 extern int crypto_digest_init(crypto_mechanism_t *mech, crypto_context_t *ctxp, 80 crypto_call_req_t *cr); 81 extern int crypto_digest_init_prov(crypto_provider_t, crypto_session_id_t, 82 crypto_mechanism_t *, crypto_context_t *, crypto_call_req_t *); 83 extern int crypto_digest_update(crypto_context_t ctx, crypto_data_t *data, 84 crypto_call_req_t *cr); 85 extern int crypto_digest_final(crypto_context_t ctx, crypto_data_t *digest, 86 crypto_call_req_t *cr); 87 88 /* 89 * Single and multi-part MAC operations. 90 */ 91 extern int crypto_mac(crypto_mechanism_t *mech, crypto_data_t *data, 92 crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_data_t *mac, 93 crypto_call_req_t *cr); 94 extern int crypto_mac_prov(crypto_provider_t, crypto_session_id_t, 95 crypto_mechanism_t *, crypto_data_t *, crypto_key_t *, 96 crypto_ctx_template_t, crypto_data_t *, crypto_call_req_t *); 97 extern int crypto_mac_verify(crypto_mechanism_t *mech, crypto_data_t *data, 98 crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_data_t *mac, 99 crypto_call_req_t *cr); 100 extern int crypto_mac_verify_prov(crypto_provider_t, crypto_session_id_t, 101 crypto_mechanism_t *, crypto_data_t *, crypto_key_t *, 102 crypto_ctx_template_t, crypto_data_t *, crypto_call_req_t *); 103 extern int crypto_mac_init(crypto_mechanism_t *mech, crypto_key_t *key, 104 crypto_ctx_template_t tmpl, crypto_context_t *ctxp, crypto_call_req_t *cr); 105 extern int crypto_mac_init_prov(crypto_provider_t, crypto_session_id_t, 106 crypto_mechanism_t *, crypto_key_t *, crypto_ctx_template_t, 107 crypto_context_t *, crypto_call_req_t *); 108 extern int crypto_mac_update(crypto_context_t ctx, crypto_data_t *data, 109 crypto_call_req_t *cr); 110 extern int crypto_mac_final(crypto_context_t ctx, crypto_data_t *data, 111 crypto_call_req_t *cr); 112 113 /* 114 * Single and multi-part sign with private key operations. 115 */ 116 extern int crypto_sign(crypto_mechanism_t *mech, crypto_key_t *key, 117 crypto_data_t *data, crypto_ctx_template_t tmpl, 118 crypto_data_t *signature, crypto_call_req_t *cr); 119 extern int crypto_sign_prov(crypto_provider_t, crypto_session_id_t, 120 crypto_mechanism_t *, crypto_key_t *, crypto_data_t *, 121 crypto_ctx_template_t, crypto_data_t *, crypto_call_req_t *); 122 extern int crypto_sign_init(crypto_mechanism_t *mech, crypto_key_t *key, 123 crypto_ctx_template_t tmpl, crypto_context_t *ctxp, crypto_call_req_t *cr); 124 extern int crypto_sign_init_prov(crypto_provider_t, crypto_session_id_t, 125 crypto_mechanism_t *, crypto_key_t *, crypto_ctx_template_t, 126 crypto_context_t *, crypto_call_req_t *); 127 extern int crypto_sign_update(crypto_context_t ctx, crypto_data_t *data, 128 crypto_call_req_t *cr); 129 extern int crypto_sign_final(crypto_context_t ctx, crypto_data_t *signature, 130 crypto_call_req_t *cr); 131 extern int crypto_sign_recover_init_prov(crypto_provider_t, 132 crypto_session_id_t, crypto_mechanism_t *, crypto_key_t *, 133 crypto_ctx_template_t tmpl, crypto_context_t *, crypto_call_req_t *); 134 extern int crypto_sign_recover(crypto_mechanism_t *mech, crypto_key_t *key, 135 crypto_data_t *data, crypto_ctx_template_t tmpl, crypto_data_t *signature, 136 crypto_call_req_t *cr); 137 extern int crypto_sign_recover_prov(crypto_provider_t, crypto_session_id_t, 138 crypto_mechanism_t *, crypto_key_t *, crypto_data_t *, 139 crypto_ctx_template_t, crypto_data_t *, crypto_call_req_t *); 140 141 /* 142 * Single and multi-part verify with public key operations. 143 */ 144 extern int crypto_verify(crypto_mechanism_t *mech, crypto_key_t *key, 145 crypto_data_t *data, crypto_ctx_template_t tmpl, crypto_data_t *signature, 146 crypto_call_req_t *cr); 147 extern int crypto_verify_prov(crypto_provider_t, crypto_session_id_t, 148 crypto_mechanism_t *, crypto_key_t *, crypto_data_t *, 149 crypto_ctx_template_t, crypto_data_t *, crypto_call_req_t *); 150 extern int crypto_verify_init(crypto_mechanism_t *mech, crypto_key_t *key, 151 crypto_ctx_template_t tmpl, crypto_context_t *ctxp, crypto_call_req_t *cr); 152 extern int crypto_verify_init_prov(crypto_provider_t, crypto_session_id_t, 153 crypto_mechanism_t *, crypto_key_t *, crypto_ctx_template_t, 154 crypto_context_t *, crypto_call_req_t *); 155 extern int crypto_verify_update(crypto_context_t ctx, crypto_data_t *data, 156 crypto_call_req_t *cr); 157 extern int crypto_verify_final(crypto_context_t ctx, crypto_data_t *signature, 158 crypto_call_req_t *cr); 159 extern int crypto_verify_recover_init_prov(crypto_provider_t, 160 crypto_session_id_t, crypto_mechanism_t *, crypto_key_t *, 161 crypto_ctx_template_t tmpl, crypto_context_t *, crypto_call_req_t *); 162 extern int crypto_verify_recover(crypto_mechanism_t *mech, crypto_key_t *key, 163 crypto_data_t *signature, crypto_ctx_template_t tmpl, crypto_data_t *data, 164 crypto_call_req_t *cr); 165 extern int crypto_verify_recover_prov(crypto_provider_t, crypto_session_id_t, 166 crypto_mechanism_t *, crypto_key_t *, crypto_data_t *, 167 crypto_ctx_template_t, crypto_data_t *, crypto_call_req_t *); 168 169 /* 170 * Single and multi-part encryption operations. 171 */ 172 extern int crypto_encrypt(crypto_mechanism_t *mech, crypto_data_t *plaintext, 173 crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_data_t *ciphertext, 174 crypto_call_req_t *cr); 175 extern int crypto_encrypt_prov(crypto_provider_t, crypto_session_id_t, 176 crypto_mechanism_t *, crypto_data_t *, crypto_key_t *, 177 crypto_ctx_template_t, crypto_data_t *, crypto_call_req_t *); 178 extern int crypto_encrypt_init(crypto_mechanism_t *mech, crypto_key_t *key, 179 crypto_ctx_template_t tmpl, crypto_context_t *ctxp, crypto_call_req_t *cr); 180 extern int crypto_encrypt_init_prov(crypto_provider_t, crypto_session_id_t, 181 crypto_mechanism_t *, crypto_key_t *, crypto_ctx_template_t, 182 crypto_context_t *, crypto_call_req_t *); 183 extern int crypto_encrypt_update(crypto_context_t ctx, 184 crypto_data_t *plaintext, crypto_data_t *ciphertext, 185 crypto_call_req_t *cr); 186 extern int crypto_encrypt_final(crypto_context_t ctx, 187 crypto_data_t *ciphertext, crypto_call_req_t *cr); 188 189 /* 190 * Single and multi-part decryption operations. 191 */ 192 extern int crypto_decrypt(crypto_mechanism_t *mech, crypto_data_t *ciphertext, 193 crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_data_t *plaintext, 194 crypto_call_req_t *cr); 195 extern int crypto_decrypt_prov(crypto_provider_t, crypto_session_id_t, 196 crypto_mechanism_t *, crypto_data_t *, crypto_key_t *, 197 crypto_ctx_template_t, crypto_data_t *, crypto_call_req_t *); 198 extern int crypto_decrypt_init(crypto_mechanism_t *mech, crypto_key_t *key, 199 crypto_ctx_template_t tmpl, crypto_context_t *ctxp, 200 crypto_call_req_t *cr); 201 extern int crypto_decrypt_init_prov(crypto_provider_t, crypto_session_id_t, 202 crypto_mechanism_t *, crypto_key_t *, crypto_ctx_template_t, 203 crypto_context_t *, crypto_call_req_t *); 204 extern int crypto_decrypt_update(crypto_context_t ctx, 205 crypto_data_t *ciphertext, crypto_data_t *plaintext, 206 crypto_call_req_t *cr); 207 extern int crypto_decrypt_final(crypto_context_t ctx, crypto_data_t *plaintext, 208 crypto_call_req_t *cr); 209 210 /* 211 * Single and multi-part encrypt/MAC dual operations. 212 */ 213 extern int crypto_encrypt_mac(crypto_mechanism_t *encr_mech, 214 crypto_mechanism_t *mac_mech, crypto_data_t *pt, 215 crypto_key_t *encr_key, crypto_key_t *mac_key, 216 crypto_ctx_template_t encr_tmpl, crypto_ctx_template_t mac_tmpl, 217 crypto_dual_data_t *ct, crypto_data_t *mac, crypto_call_req_t *cr); 218 extern int crypto_encrypt_mac_prov(crypto_provider_t, crypto_session_id_t, 219 crypto_mechanism_t *, crypto_mechanism_t *, crypto_data_t *, 220 crypto_key_t *, crypto_key_t *, crypto_ctx_template_t, 221 crypto_ctx_template_t, crypto_dual_data_t *, crypto_data_t *, 222 crypto_call_req_t *); 223 extern int crypto_encrypt_mac_init(crypto_mechanism_t *encr_mech, 224 crypto_mechanism_t *mac_mech, crypto_key_t *encr_key, 225 crypto_key_t *mac_key, crypto_ctx_template_t encr_tmpl, 226 crypto_ctx_template_t mac_tmpl, crypto_context_t *ctxp, 227 crypto_call_req_t *cr); 228 extern int crypto_encrypt_mac_init_prov(crypto_provider_t, crypto_session_id_t, 229 crypto_mechanism_t *, crypto_mechanism_t *, crypto_key_t *, crypto_key_t *, 230 crypto_ctx_template_t, crypto_ctx_template_t, crypto_context_t *, 231 crypto_call_req_t *); 232 extern int crypto_encrypt_mac_update(crypto_context_t ctx, 233 crypto_data_t *pt, crypto_dual_data_t *ct, crypto_call_req_t *cr); 234 extern int crypto_encrypt_mac_final(crypto_context_t ctx, 235 crypto_dual_data_t *ct, crypto_data_t *mac, crypto_call_req_t *cr); 236 237 /* 238 * Single and multi-part MAC/decrypt dual operations. 239 */ 240 extern int crypto_mac_decrypt(crypto_mechanism_t *mac_mech, 241 crypto_mechanism_t *decr_mech, crypto_dual_data_t *ct, 242 crypto_key_t *mac_key, crypto_key_t *decr_key, 243 crypto_ctx_template_t mac_tmpl, crypto_ctx_template_t decr_tmpl, 244 crypto_data_t *mac, crypto_data_t *pt, crypto_call_req_t *cr); 245 extern int crypto_mac_decrypt_prov(crypto_provider_t, crypto_session_id_t, 246 crypto_mechanism_t *mac_mech, crypto_mechanism_t *decr_mech, 247 crypto_dual_data_t *ct, 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_verify_decrypt(crypto_mechanism_t *mac_mech, 251 crypto_mechanism_t *decr_mech, crypto_dual_data_t *ct, 252 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_prov(crypto_provider_t, 256 crypto_session_id_t, crypto_mechanism_t *mac_mech, 257 crypto_mechanism_t *decr_mech, crypto_dual_data_t *ct, 258 crypto_key_t *mac_key, crypto_key_t *decr_key, 259 crypto_ctx_template_t mac_tmpl, crypto_ctx_template_t decr_tmpl, 260 crypto_data_t *mac, crypto_data_t *pt, crypto_call_req_t *cr); 261 extern int crypto_mac_decrypt_init(crypto_mechanism_t *mac_mech, 262 crypto_mechanism_t *decr_mech, crypto_key_t *mac_key, 263 crypto_key_t *decr_key, crypto_ctx_template_t mac_tmpl, 264 crypto_ctx_template_t decr_tmpl, crypto_context_t *ctxp, 265 crypto_call_req_t *cr); 266 extern int crypto_mac_decrypt_init_prov(crypto_provider_t, 267 crypto_session_id_t, crypto_mechanism_t *mac_mech, 268 crypto_mechanism_t *decr_mech, crypto_key_t *mac_key, 269 crypto_key_t *decr_key, crypto_ctx_template_t mac_tmpl, 270 crypto_ctx_template_t decr_tmpl, crypto_context_t *ctxp, 271 crypto_call_req_t *cr); 272 extern int crypto_mac_decrypt_update(crypto_context_t ctx, 273 crypto_dual_data_t *ct, crypto_data_t *pt, crypto_call_req_t *cr); 274 extern int crypto_mac_decrypt_final(crypto_context_t ctx, crypto_data_t *mac, 275 crypto_data_t *pt, crypto_call_req_t *cr); 276 277 /* Session Management */ 278 extern int crypto_session_open(crypto_provider_t, crypto_session_id_t *, 279 crypto_call_req_t *); 280 extern int crypto_session_close(crypto_provider_t, crypto_session_id_t, 281 crypto_call_req_t *); 282 extern int crypto_session_login(crypto_provider_t, crypto_session_id_t, 283 crypto_user_type_t, char *, size_t, crypto_call_req_t *); 284 extern int crypto_session_logout(crypto_provider_t, crypto_session_id_t, 285 crypto_call_req_t *); 286 287 /* Object Management */ 288 extern int crypto_object_copy(crypto_provider_t, crypto_session_id_t, 289 crypto_object_id_t, crypto_object_attribute_t *, uint_t, 290 crypto_object_id_t *, crypto_call_req_t *); 291 extern int crypto_object_create(crypto_provider_t, crypto_session_id_t, 292 crypto_object_attribute_t *, uint_t, crypto_object_id_t *, 293 crypto_call_req_t *); 294 extern int crypto_object_destroy(crypto_provider_t, crypto_session_id_t, 295 crypto_object_id_t, crypto_call_req_t *); 296 extern int crypto_object_get_attribute_value(crypto_provider_t, 297 crypto_session_id_t, crypto_object_id_t, crypto_object_attribute_t *, 298 uint_t, crypto_call_req_t *); 299 extern int crypto_object_get_size(crypto_provider_t, crypto_session_id_t, 300 crypto_object_id_t, size_t *, crypto_call_req_t *); 301 extern int crypto_object_find_final(crypto_provider_t, void *, 302 crypto_call_req_t *); 303 extern int crypto_object_find_init(crypto_provider_t, crypto_session_id_t, 304 crypto_object_attribute_t *, uint_t, void **, crypto_call_req_t *); 305 extern int crypto_object_find(crypto_provider_t, void *, crypto_object_id_t *, 306 uint_t *, uint_t, crypto_call_req_t *); 307 extern int crypto_object_set_attribute_value(crypto_provider_t, 308 crypto_session_id_t, crypto_object_id_t, crypto_object_attribute_t *, 309 uint_t, crypto_call_req_t *); 310 311 /* Key Management */ 312 extern int crypto_key_derive(crypto_provider_t, crypto_session_id_t, 313 crypto_mechanism_t *, crypto_key_t *, crypto_object_attribute_t *, 314 uint_t, crypto_object_id_t *, crypto_call_req_t *); 315 extern int crypto_key_generate(crypto_provider_t, crypto_session_id_t, 316 crypto_mechanism_t *, crypto_object_attribute_t *, uint_t, 317 crypto_object_id_t *, crypto_call_req_t *); 318 extern int crypto_key_generate_pair(crypto_provider_t, crypto_session_id_t, 319 crypto_mechanism_t *, crypto_object_attribute_t *, uint_t, 320 crypto_object_attribute_t *, uint_t, crypto_object_id_t *, 321 crypto_object_id_t *, crypto_call_req_t *); 322 extern int crypto_key_unwrap(crypto_provider_t, crypto_session_id_t, 323 crypto_mechanism_t *, crypto_key_t *, uchar_t *, size_t *, 324 crypto_object_attribute_t *, uint_t, crypto_object_id_t *, 325 crypto_call_req_t *); 326 extern int crypto_key_wrap(crypto_provider_t, crypto_session_id_t, 327 crypto_mechanism_t *, crypto_key_t *, crypto_object_id_t *, uchar_t *, 328 size_t *, crypto_call_req_t *); 329 extern int crypto_key_check_prov(crypto_provider_t, crypto_mechanism_t *mech, 330 crypto_key_t *key); 331 extern int crypto_key_check(crypto_mechanism_t *mech, crypto_key_t *key); 332 333 334 /* 335 * Routines to cancel a single asynchronous request or all asynchronous 336 * requests associated with a particular context. 337 */ 338 extern void crypto_cancel_req(crypto_req_id_t req); 339 extern void crypto_cancel_ctx(crypto_context_t ctx); 340 341 /* 342 * crypto_get_mech_list(9F) allocates and returns the list of currently 343 * supported cryptographic mechanisms. 344 */ 345 extern crypto_mech_name_t *crypto_get_mech_list(uint_t *count, int kmflag); 346 extern void crypto_free_mech_list(crypto_mech_name_t *mech_names, 347 uint_t count); 348 349 extern crypto_provider_t crypto_get_provider(char *, char *, char *); 350 extern int crypto_get_provinfo(crypto_provider_t, crypto_provider_ext_info_t *); 351 extern void crypto_release_provider(crypto_provider_t); 352 353 /* 354 * A kernel consumer can request to be notified when some particular event 355 * occurs. The valid events, callback function type, and functions to 356 * be called to register or unregister for notification are defined below. 357 */ 358 359 #define CRYPTO_EVENT_MECHS_CHANGED 0x00000001 360 #define CRYPTO_EVENT_PROVIDER_REGISTERED 0x00000002 361 #define CRYPTO_EVENT_PROVIDER_UNREGISTERED 0x00000004 362 363 typedef enum { 364 CRYPTO_MECH_ADDED = 1, 365 CRYPTO_MECH_REMOVED 366 } crypto_event_change_t; 367 368 /* The event_arg argument structure for CRYPTO_EVENT_PROVIDERS_CHANGE event */ 369 typedef struct crypto_notify_event_change { 370 crypto_mech_name_t ec_mech_name; 371 crypto_provider_type_t ec_provider_type; 372 crypto_event_change_t ec_change; 373 } crypto_notify_event_change_t; 374 375 typedef void *crypto_notify_handle_t; 376 typedef void (*crypto_notify_callback_t)(uint32_t event_mask, void *event_arg); 377 378 extern crypto_notify_handle_t crypto_notify_events( 379 crypto_notify_callback_t nf, uint32_t event_mask); 380 extern void crypto_unnotify_events(crypto_notify_handle_t); 381 382 /* 383 * crypto_bufcall(9F) group of routines. 384 */ 385 extern crypto_bc_t crypto_bufcall_alloc(void); 386 extern int crypto_bufcall_free(crypto_bc_t bc); 387 extern int crypto_bufcall(crypto_bc_t bc, void (*func)(void *arg), void *arg); 388 extern int crypto_unbufcall(crypto_bc_t bc); 389 390 #endif /* _KERNEL */ 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 #ifdef _KERNEL 410 #ifdef _SYSCALL32 411 412 typedef struct crypto_mechanism_info32 { 413 size32_t mi_min_key_size; 414 size32_t mi_max_key_size; 415 crypto_keysize_unit_t mi_keysize_unit; /* for mi_xxx_key_size */ 416 crypto_mech_usage_t mi_usage; 417 } crypto_mechanism_info32_t; 418 419 #endif /* _SYSCALL32 */ 420 #endif /* _KERNEL */ 421 422 extern int crypto_get_all_mech_info(crypto_mech_type_t, 423 crypto_mechanism_info_t **, uint_t *, int); 424 extern void crypto_free_all_mech_info(crypto_mechanism_info_t *, uint_t); 425 426 #ifdef __cplusplus 427 } 428 #endif 429 430 #endif /* _SYS_CRYPTO_API_H */ 431