10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51808Smcpowers * Common Development and Distribution License (the "License"). 61808Smcpowers * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 229505SBhargava.Yenduri@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate 270Sstevel@tonic-gate /* 280Sstevel@tonic-gate * The ioctl interface for cryptographic commands. 290Sstevel@tonic-gate */ 300Sstevel@tonic-gate 310Sstevel@tonic-gate #include <sys/types.h> 320Sstevel@tonic-gate #include <sys/modctl.h> 330Sstevel@tonic-gate #include <sys/conf.h> 340Sstevel@tonic-gate #include <sys/stat.h> 350Sstevel@tonic-gate #include <sys/ddi.h> 360Sstevel@tonic-gate #include <sys/sunddi.h> 370Sstevel@tonic-gate #include <sys/kmem.h> 380Sstevel@tonic-gate #include <sys/errno.h> 390Sstevel@tonic-gate #include <sys/ksynch.h> 400Sstevel@tonic-gate #include <sys/file.h> 410Sstevel@tonic-gate #include <sys/open.h> 420Sstevel@tonic-gate #include <sys/cred.h> 430Sstevel@tonic-gate #include <sys/proc.h> 440Sstevel@tonic-gate #include <sys/task.h> 450Sstevel@tonic-gate #include <sys/mkdev.h> 460Sstevel@tonic-gate #include <sys/model.h> 470Sstevel@tonic-gate #include <sys/sysmacros.h> 480Sstevel@tonic-gate #include <sys/crypto/common.h> 490Sstevel@tonic-gate #include <sys/crypto/api.h> 500Sstevel@tonic-gate #include <sys/crypto/impl.h> 510Sstevel@tonic-gate #include <sys/crypto/sched_impl.h> 520Sstevel@tonic-gate #include <sys/crypto/ioctl.h> 530Sstevel@tonic-gate 544841Shaimay extern int kcf_des3_threshold; 554841Shaimay extern int kcf_aes_threshold; 564841Shaimay extern int kcf_rc4_threshold; 574841Shaimay extern int kcf_md5_threshold; 584841Shaimay extern int kcf_sha1_threshold; 594841Shaimay 600Sstevel@tonic-gate /* 610Sstevel@tonic-gate * Locking notes: 620Sstevel@tonic-gate * 639505SBhargava.Yenduri@Sun.COM * crypto_locks protects the global array of minor structures. 649505SBhargava.Yenduri@Sun.COM * crypto_locks is an array of locks indexed by the cpuid. A reader needs 659505SBhargava.Yenduri@Sun.COM * to hold a single lock while a writer needs to hold all locks. 669505SBhargava.Yenduri@Sun.COM * krwlock_t is not an option here because the hold time 679505SBhargava.Yenduri@Sun.COM * is very small for these locks. 680Sstevel@tonic-gate * 699505SBhargava.Yenduri@Sun.COM * The fields in the minor structure are protected by the cm_lock member 709505SBhargava.Yenduri@Sun.COM * of the minor structure. The cm_cv is used to signal decrements 719505SBhargava.Yenduri@Sun.COM * in the cm_refcnt, and is used with the cm_lock. 729505SBhargava.Yenduri@Sun.COM * 739505SBhargava.Yenduri@Sun.COM * The locking order is crypto_locks followed by cm_lock. 740Sstevel@tonic-gate */ 750Sstevel@tonic-gate 760Sstevel@tonic-gate /* 770Sstevel@tonic-gate * DDI entry points. 780Sstevel@tonic-gate */ 790Sstevel@tonic-gate static int crypto_attach(dev_info_t *, ddi_attach_cmd_t); 800Sstevel@tonic-gate static int crypto_detach(dev_info_t *, ddi_detach_cmd_t); 810Sstevel@tonic-gate static int crypto_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **); 820Sstevel@tonic-gate static int crypto_open(dev_t *, int, int, cred_t *); 830Sstevel@tonic-gate static int crypto_close(dev_t, int, int, cred_t *); 840Sstevel@tonic-gate static int crypto_ioctl(dev_t, int, intptr_t, int, cred_t *, int *); 850Sstevel@tonic-gate 86904Smcpowers static int cipher_init(dev_t, caddr_t, int, int (*)(crypto_provider_t, 870Sstevel@tonic-gate crypto_session_id_t, crypto_mechanism_t *, crypto_key_t *, 880Sstevel@tonic-gate crypto_ctx_template_t, crypto_context_t *, crypto_call_req_t *)); 890Sstevel@tonic-gate 900Sstevel@tonic-gate static int common_digest(dev_t, caddr_t, int, int (*)(crypto_context_t, 910Sstevel@tonic-gate crypto_data_t *, crypto_data_t *, crypto_call_req_t *)); 920Sstevel@tonic-gate 930Sstevel@tonic-gate static int cipher(dev_t, caddr_t, int, int (*)(crypto_context_t, 940Sstevel@tonic-gate crypto_data_t *, crypto_data_t *, crypto_call_req_t *)); 950Sstevel@tonic-gate 960Sstevel@tonic-gate static int cipher_update(dev_t, caddr_t, int, int (*)(crypto_context_t, 970Sstevel@tonic-gate crypto_data_t *, crypto_data_t *, crypto_call_req_t *)); 980Sstevel@tonic-gate 990Sstevel@tonic-gate static int common_final(dev_t, caddr_t, int, int (*)(crypto_context_t, 1000Sstevel@tonic-gate crypto_data_t *, crypto_call_req_t *)); 1010Sstevel@tonic-gate 102904Smcpowers static int sign_verify_init(dev_t, caddr_t, int, int (*)(crypto_provider_t, 1030Sstevel@tonic-gate crypto_session_id_t, crypto_mechanism_t *, crypto_key_t *, 1040Sstevel@tonic-gate crypto_ctx_template_t, crypto_context_t *, crypto_call_req_t *)); 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate static int sign_verify_update(dev_t dev, caddr_t arg, int mode, 1070Sstevel@tonic-gate int (*)(crypto_context_t, crypto_data_t *, crypto_call_req_t *)); 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate static void crypto_initialize_rctl(void); 1100Sstevel@tonic-gate static void crypto_release_provider_session(crypto_minor_t *, 1110Sstevel@tonic-gate crypto_provider_session_t *); 1123916Skrishna static int crypto_buffer_check(size_t); 1130Sstevel@tonic-gate static int crypto_free_find_ctx(crypto_session_data_t *); 1140Sstevel@tonic-gate static int crypto_get_provider_list(crypto_minor_t *, uint_t *, 1150Sstevel@tonic-gate crypto_provider_entry_t **, boolean_t); 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate /* number of minor numbers to allocate at a time */ 1180Sstevel@tonic-gate #define CRYPTO_MINOR_CHUNK 16 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate /* 1210Sstevel@tonic-gate * There are two limits associated with kernel memory. The first, 1220Sstevel@tonic-gate * CRYPTO_MAX_BUFFER_LEN, is the maximum number of bytes that can be 1230Sstevel@tonic-gate * allocated for a single copyin/copyout buffer. The second limit is 1240Sstevel@tonic-gate * the total number of bytes that can be allocated by a process 1250Sstevel@tonic-gate * for copyin/copyout buffers. The latter is enforced by the 1260Sstevel@tonic-gate * project.max-crypto-memory resource control. 1270Sstevel@tonic-gate */ 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate #define CRYPTO_MAX_BUFFER_LEN (2 * 1024 * 1024) 1300Sstevel@tonic-gate #define CRYPTO_MAX_FIND_COUNT 512 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate /* 1336424Skrishna * We preapprove some bytes for each session to avoid making the costly 1346424Skrishna * crypto_buffer_check() calls. The preapproval is done when a new session 1356424Skrishna * is created and that cost is amortized over later crypto calls. 1366424Skrishna * Most applications create a session and then do a bunch of crypto calls 1376424Skrishna * in that session. So, they benefit from this optimization. 1386424Skrishna * 1396424Skrishna * Note that we may hit the project.max-crypto-memory limit a bit sooner 1406424Skrishna * because of this preapproval. But it is acceptable since the preapproved 1416424Skrishna * amount is insignificant compared to the default max-crypto-memory limit 1426424Skrishna * which is quarter of the machine's memory. The preapproved amount is 1436424Skrishna * roughly 2 * 16K(maximum SSL record size). 1440Sstevel@tonic-gate */ 1456424Skrishna #define CRYPTO_PRE_APPROVED_LIMIT (32 * 1024) 1460Sstevel@tonic-gate 1470Sstevel@tonic-gate /* The session table grows by CRYPTO_SESSION_CHUNK increments */ 1480Sstevel@tonic-gate #define CRYPTO_SESSION_CHUNK 100 1490Sstevel@tonic-gate 1500Sstevel@tonic-gate size_t crypto_max_buffer_len = CRYPTO_MAX_BUFFER_LEN; 1516424Skrishna size_t crypto_pre_approved_limit = CRYPTO_PRE_APPROVED_LIMIT; 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate #define INIT_RAW_CRYPTO_DATA(data, len) \ 1540Sstevel@tonic-gate (data).cd_format = CRYPTO_DATA_RAW; \ 1550Sstevel@tonic-gate (data).cd_raw.iov_base = kmem_alloc(len, KM_SLEEP); \ 1560Sstevel@tonic-gate (data).cd_raw.iov_len = len; \ 1570Sstevel@tonic-gate (data).cd_offset = 0; \ 1580Sstevel@tonic-gate (data).cd_length = len; 1590Sstevel@tonic-gate 1600Sstevel@tonic-gate static struct kmem_cache *crypto_session_cache; 1610Sstevel@tonic-gate static crypto_minor_t **crypto_minors = NULL; 1620Sstevel@tonic-gate static dev_info_t *crypto_dip = NULL; 1630Sstevel@tonic-gate static minor_t crypto_minor_chunk = CRYPTO_MINOR_CHUNK; 1640Sstevel@tonic-gate static minor_t crypto_minors_table_count = 0; 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate /* 1670Sstevel@tonic-gate * Minors are started from 1 because vmem_alloc() 1680Sstevel@tonic-gate * returns 0 in case of failure. 1690Sstevel@tonic-gate */ 1700Sstevel@tonic-gate static vmem_t *crypto_arena = NULL; /* Arena for device minors */ 1710Sstevel@tonic-gate static minor_t crypto_minors_count = 0; 1729505SBhargava.Yenduri@Sun.COM static kcf_lock_withpad_t *crypto_locks; 1739505SBhargava.Yenduri@Sun.COM 1749505SBhargava.Yenduri@Sun.COM #define CRYPTO_ENTER_ALL_LOCKS() \ 1759505SBhargava.Yenduri@Sun.COM for (i = 0; i < max_ncpus; i++) \ 1769505SBhargava.Yenduri@Sun.COM mutex_enter(&crypto_locks[i].kl_lock); 1779505SBhargava.Yenduri@Sun.COM 1789505SBhargava.Yenduri@Sun.COM #define CRYPTO_EXIT_ALL_LOCKS() \ 1799505SBhargava.Yenduri@Sun.COM for (i = 0; i < max_ncpus; i++) \ 1809505SBhargava.Yenduri@Sun.COM mutex_exit(&crypto_locks[i].kl_lock); 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate #define RETURN_LIST B_TRUE 1830Sstevel@tonic-gate #define DONT_RETURN_LIST B_FALSE 1840Sstevel@tonic-gate 185904Smcpowers #define CRYPTO_OPS_OFFSET(f) offsetof(crypto_ops_t, co_##f) 1860Sstevel@tonic-gate #define CRYPTO_RANDOM_OFFSET(f) offsetof(crypto_random_number_ops_t, f) 1870Sstevel@tonic-gate #define CRYPTO_SESSION_OFFSET(f) offsetof(crypto_session_ops_t, f) 1880Sstevel@tonic-gate #define CRYPTO_OBJECT_OFFSET(f) offsetof(crypto_object_ops_t, f) 1890Sstevel@tonic-gate #define CRYPTO_PROVIDER_OFFSET(f) \ 1900Sstevel@tonic-gate offsetof(crypto_provider_management_ops_t, f) 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate #define CRYPTO_CANCEL_CTX(spp) { \ 1930Sstevel@tonic-gate crypto_cancel_ctx(*(spp)); \ 1940Sstevel@tonic-gate *(spp) = NULL; \ 1950Sstevel@tonic-gate } 1960Sstevel@tonic-gate 1970Sstevel@tonic-gate #define CRYPTO_CANCEL_ALL_CTX(sp) { \ 1980Sstevel@tonic-gate if ((sp)->sd_digest_ctx != NULL) { \ 1990Sstevel@tonic-gate crypto_cancel_ctx((sp)->sd_digest_ctx); \ 2000Sstevel@tonic-gate (sp)->sd_digest_ctx = NULL; \ 2010Sstevel@tonic-gate } \ 2020Sstevel@tonic-gate if ((sp)->sd_encr_ctx != NULL) { \ 2030Sstevel@tonic-gate crypto_cancel_ctx((sp)->sd_encr_ctx); \ 2040Sstevel@tonic-gate (sp)->sd_encr_ctx = NULL; \ 2050Sstevel@tonic-gate } \ 2060Sstevel@tonic-gate if ((sp)->sd_decr_ctx != NULL) { \ 2070Sstevel@tonic-gate crypto_cancel_ctx((sp)->sd_decr_ctx); \ 2080Sstevel@tonic-gate (sp)->sd_decr_ctx = NULL; \ 2090Sstevel@tonic-gate } \ 2100Sstevel@tonic-gate if ((sp)->sd_sign_ctx != NULL) { \ 2110Sstevel@tonic-gate crypto_cancel_ctx((sp)->sd_sign_ctx); \ 2120Sstevel@tonic-gate (sp)->sd_sign_ctx = NULL; \ 2130Sstevel@tonic-gate } \ 2140Sstevel@tonic-gate if ((sp)->sd_verify_ctx != NULL) { \ 2150Sstevel@tonic-gate crypto_cancel_ctx((sp)->sd_verify_ctx); \ 2160Sstevel@tonic-gate (sp)->sd_verify_ctx = NULL; \ 2170Sstevel@tonic-gate } \ 2180Sstevel@tonic-gate if ((sp)->sd_sign_recover_ctx != NULL) { \ 2190Sstevel@tonic-gate crypto_cancel_ctx((sp)->sd_sign_recover_ctx); \ 2200Sstevel@tonic-gate (sp)->sd_sign_recover_ctx = NULL; \ 2210Sstevel@tonic-gate } \ 2220Sstevel@tonic-gate if ((sp)->sd_verify_recover_ctx != NULL) { \ 2230Sstevel@tonic-gate crypto_cancel_ctx((sp)->sd_verify_recover_ctx); \ 2240Sstevel@tonic-gate (sp)->sd_verify_recover_ctx = NULL; \ 2250Sstevel@tonic-gate } \ 2260Sstevel@tonic-gate } 2270Sstevel@tonic-gate 2286424Skrishna #define CRYPTO_DECREMENT_RCTL(val) if ((val) != 0) { \ 2293916Skrishna kproject_t *projp; \ 2303916Skrishna mutex_enter(&curproc->p_lock); \ 2313916Skrishna projp = curproc->p_task->tk_proj; \ 2323916Skrishna ASSERT(projp != NULL); \ 2333916Skrishna mutex_enter(&(projp->kpj_data.kpd_crypto_lock)); \ 2343916Skrishna projp->kpj_data.kpd_crypto_mem -= (val); \ 2353916Skrishna mutex_exit(&(projp->kpj_data.kpd_crypto_lock)); \ 2363916Skrishna curproc->p_crypto_mem -= (val); \ 2373916Skrishna mutex_exit(&curproc->p_lock); \ 2380Sstevel@tonic-gate } 2390Sstevel@tonic-gate 2400Sstevel@tonic-gate /* 2416424Skrishna * We do not need to hold sd_lock in the macros below 2426424Skrishna * as they are called after doing a get_session_ptr() which 2436424Skrishna * sets the CRYPTO_SESSION_IS_BUSY flag. 2446424Skrishna */ 2456424Skrishna #define CRYPTO_DECREMENT_RCTL_SESSION(sp, val, rctl_chk) \ 2466867Skrishna if (((val) != 0) && ((sp) != NULL)) { \ 2476424Skrishna ASSERT(((sp)->sd_flags & CRYPTO_SESSION_IS_BUSY) != 0); \ 2486424Skrishna if (rctl_chk) { \ 2496424Skrishna CRYPTO_DECREMENT_RCTL(val); \ 2506424Skrishna } else { \ 2516424Skrishna (sp)->sd_pre_approved_amount += (val); \ 2526424Skrishna } \ 2536424Skrishna } 2546424Skrishna 2556424Skrishna #define CRYPTO_BUFFER_CHECK(sp, need, rctl_chk) \ 2566424Skrishna ((sp->sd_pre_approved_amount >= need) ? \ 2576424Skrishna (sp->sd_pre_approved_amount -= need, \ 2586424Skrishna rctl_chk = B_FALSE, CRYPTO_SUCCESS) : \ 2596424Skrishna (rctl_chk = B_TRUE, crypto_buffer_check(need))) 2606424Skrishna 2616424Skrishna /* 2620Sstevel@tonic-gate * Module linkage. 2630Sstevel@tonic-gate */ 2640Sstevel@tonic-gate static struct cb_ops cbops = { 2650Sstevel@tonic-gate crypto_open, /* cb_open */ 2660Sstevel@tonic-gate crypto_close, /* cb_close */ 2670Sstevel@tonic-gate nodev, /* cb_strategy */ 2680Sstevel@tonic-gate nodev, /* cb_print */ 2690Sstevel@tonic-gate nodev, /* cb_dump */ 2700Sstevel@tonic-gate nodev, /* cb_read */ 2710Sstevel@tonic-gate nodev, /* cb_write */ 2720Sstevel@tonic-gate crypto_ioctl, /* cb_ioctl */ 2730Sstevel@tonic-gate nodev, /* cb_devmap */ 2740Sstevel@tonic-gate nodev, /* cb_mmap */ 2750Sstevel@tonic-gate nodev, /* cb_segmap */ 2760Sstevel@tonic-gate nochpoll, /* cb_chpoll */ 2770Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */ 2780Sstevel@tonic-gate NULL, /* cb_streamtab */ 2790Sstevel@tonic-gate D_MP, /* cb_flag */ 2800Sstevel@tonic-gate CB_REV, /* cb_rev */ 2810Sstevel@tonic-gate nodev, /* cb_aread */ 2820Sstevel@tonic-gate nodev, /* cb_awrite */ 2830Sstevel@tonic-gate }; 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate static struct dev_ops devops = { 2860Sstevel@tonic-gate DEVO_REV, /* devo_rev */ 2870Sstevel@tonic-gate 0, /* devo_refcnt */ 2880Sstevel@tonic-gate crypto_getinfo, /* devo_getinfo */ 2890Sstevel@tonic-gate nulldev, /* devo_identify */ 2900Sstevel@tonic-gate nulldev, /* devo_probe */ 2910Sstevel@tonic-gate crypto_attach, /* devo_attach */ 2920Sstevel@tonic-gate crypto_detach, /* devo_detach */ 2930Sstevel@tonic-gate nodev, /* devo_reset */ 2940Sstevel@tonic-gate &cbops, /* devo_cb_ops */ 2950Sstevel@tonic-gate NULL, /* devo_bus_ops */ 2960Sstevel@tonic-gate NULL, /* devo_power */ 2977656SSherry.Moore@Sun.COM ddi_quiesce_not_needed, /* devo_quiesce */ 2980Sstevel@tonic-gate }; 2990Sstevel@tonic-gate 3000Sstevel@tonic-gate static struct modldrv modldrv = { 3010Sstevel@tonic-gate &mod_driverops, /* drv_modops */ 3025072Smcpowers "Cryptographic Library Interface", /* drv_linkinfo */ 3030Sstevel@tonic-gate &devops, 3040Sstevel@tonic-gate }; 3050Sstevel@tonic-gate 3060Sstevel@tonic-gate static struct modlinkage modlinkage = { 3070Sstevel@tonic-gate MODREV_1, /* ml_rev */ 3080Sstevel@tonic-gate &modldrv, /* ml_linkage */ 3090Sstevel@tonic-gate NULL 3100Sstevel@tonic-gate }; 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate /* 3130Sstevel@tonic-gate * DDI entry points. 3140Sstevel@tonic-gate */ 3150Sstevel@tonic-gate int 3160Sstevel@tonic-gate _init(void) 3170Sstevel@tonic-gate { 3180Sstevel@tonic-gate return (mod_install(&modlinkage)); 3190Sstevel@tonic-gate } 3200Sstevel@tonic-gate 3210Sstevel@tonic-gate int 3220Sstevel@tonic-gate _fini(void) 3230Sstevel@tonic-gate { 3240Sstevel@tonic-gate return (mod_remove(&modlinkage)); 3250Sstevel@tonic-gate } 3260Sstevel@tonic-gate 3270Sstevel@tonic-gate int 3280Sstevel@tonic-gate _info(struct modinfo *modinfop) 3290Sstevel@tonic-gate { 3300Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 3310Sstevel@tonic-gate } 3320Sstevel@tonic-gate 3330Sstevel@tonic-gate /* ARGSUSED */ 3340Sstevel@tonic-gate static int 3350Sstevel@tonic-gate crypto_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result) 3360Sstevel@tonic-gate { 3370Sstevel@tonic-gate switch (cmd) { 3380Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 3390Sstevel@tonic-gate *result = crypto_dip; 3400Sstevel@tonic-gate return (DDI_SUCCESS); 3410Sstevel@tonic-gate 3420Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 3430Sstevel@tonic-gate *result = (void *)0; 3440Sstevel@tonic-gate return (DDI_SUCCESS); 3450Sstevel@tonic-gate } 3460Sstevel@tonic-gate return (DDI_FAILURE); 3470Sstevel@tonic-gate } 3480Sstevel@tonic-gate 3490Sstevel@tonic-gate static int 3500Sstevel@tonic-gate crypto_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 3510Sstevel@tonic-gate { 3529505SBhargava.Yenduri@Sun.COM int i; 3539505SBhargava.Yenduri@Sun.COM 3540Sstevel@tonic-gate if (cmd != DDI_ATTACH) { 3550Sstevel@tonic-gate return (DDI_FAILURE); 3560Sstevel@tonic-gate } 3570Sstevel@tonic-gate 3580Sstevel@tonic-gate if (ddi_get_instance(dip) != 0) { 3590Sstevel@tonic-gate /* we only allow instance 0 to attach */ 3600Sstevel@tonic-gate return (DDI_FAILURE); 3610Sstevel@tonic-gate } 3620Sstevel@tonic-gate 3630Sstevel@tonic-gate crypto_session_cache = kmem_cache_create("crypto_session_cache", 3640Sstevel@tonic-gate sizeof (crypto_session_data_t), 0, NULL, NULL, NULL, NULL, NULL, 0); 3650Sstevel@tonic-gate 3660Sstevel@tonic-gate if (crypto_session_cache == NULL) 3670Sstevel@tonic-gate return (DDI_FAILURE); 3680Sstevel@tonic-gate 3690Sstevel@tonic-gate /* create the minor node */ 3700Sstevel@tonic-gate if (ddi_create_minor_node(dip, "crypto", S_IFCHR, 0, 3710Sstevel@tonic-gate DDI_PSEUDO, 0) != DDI_SUCCESS) { 3720Sstevel@tonic-gate kmem_cache_destroy(crypto_session_cache); 3730Sstevel@tonic-gate crypto_session_cache = NULL; 3740Sstevel@tonic-gate cmn_err(CE_WARN, "crypto_attach: failed creating minor node"); 3750Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 3760Sstevel@tonic-gate return (DDI_FAILURE); 3770Sstevel@tonic-gate } 3780Sstevel@tonic-gate 3799505SBhargava.Yenduri@Sun.COM crypto_locks = kmem_zalloc(max_ncpus * sizeof (kcf_lock_withpad_t), 3809505SBhargava.Yenduri@Sun.COM KM_SLEEP); 3819505SBhargava.Yenduri@Sun.COM for (i = 0; i < max_ncpus; i++) 3829505SBhargava.Yenduri@Sun.COM mutex_init(&crypto_locks[i].kl_lock, NULL, MUTEX_DRIVER, NULL); 3839505SBhargava.Yenduri@Sun.COM 3840Sstevel@tonic-gate crypto_dip = dip; 3850Sstevel@tonic-gate 3860Sstevel@tonic-gate /* allocate integer space for minor numbers */ 3870Sstevel@tonic-gate crypto_arena = vmem_create("crypto", (void *)1, 3880Sstevel@tonic-gate CRYPTO_MINOR_CHUNK, 1, NULL, NULL, NULL, 0, 3890Sstevel@tonic-gate VM_SLEEP | VMC_IDENTIFIER); 3900Sstevel@tonic-gate 3910Sstevel@tonic-gate return (DDI_SUCCESS); 3920Sstevel@tonic-gate } 3930Sstevel@tonic-gate 3940Sstevel@tonic-gate static int 3950Sstevel@tonic-gate crypto_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 3960Sstevel@tonic-gate { 3970Sstevel@tonic-gate minor_t i; 3989505SBhargava.Yenduri@Sun.COM kcf_lock_withpad_t *mp; 3990Sstevel@tonic-gate 4000Sstevel@tonic-gate if (cmd != DDI_DETACH) 4010Sstevel@tonic-gate return (DDI_FAILURE); 4020Sstevel@tonic-gate 4039505SBhargava.Yenduri@Sun.COM mp = &crypto_locks[CPU_SEQID]; 4049505SBhargava.Yenduri@Sun.COM mutex_enter(&mp->kl_lock); 4059505SBhargava.Yenduri@Sun.COM 4060Sstevel@tonic-gate /* check if device is open */ 4070Sstevel@tonic-gate for (i = 0; i < crypto_minors_table_count; i++) { 4080Sstevel@tonic-gate if (crypto_minors[i] != NULL) { 4099505SBhargava.Yenduri@Sun.COM mutex_exit(&mp->kl_lock); 4100Sstevel@tonic-gate return (DDI_FAILURE); 4110Sstevel@tonic-gate } 4120Sstevel@tonic-gate } 4139505SBhargava.Yenduri@Sun.COM mutex_exit(&mp->kl_lock); 4140Sstevel@tonic-gate 4150Sstevel@tonic-gate crypto_dip = NULL; 4160Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 4170Sstevel@tonic-gate 4180Sstevel@tonic-gate kmem_cache_destroy(crypto_session_cache); 4190Sstevel@tonic-gate crypto_session_cache = NULL; 4200Sstevel@tonic-gate 4210Sstevel@tonic-gate kmem_free(crypto_minors, 4220Sstevel@tonic-gate sizeof (crypto_minor_t *) * crypto_minors_table_count); 4230Sstevel@tonic-gate crypto_minors = NULL; 4240Sstevel@tonic-gate crypto_minors_table_count = 0; 4259505SBhargava.Yenduri@Sun.COM for (i = 0; i < max_ncpus; i++) 4269505SBhargava.Yenduri@Sun.COM mutex_destroy(&crypto_locks[i].kl_lock); 4279662SBhargava.Yenduri@Sun.COM kmem_free(crypto_locks, max_ncpus * sizeof (kcf_lock_withpad_t)); 4289662SBhargava.Yenduri@Sun.COM crypto_locks = NULL; 4299505SBhargava.Yenduri@Sun.COM 4300Sstevel@tonic-gate vmem_destroy(crypto_arena); 4310Sstevel@tonic-gate crypto_arena = NULL; 4320Sstevel@tonic-gate 4330Sstevel@tonic-gate return (DDI_SUCCESS); 4340Sstevel@tonic-gate } 4350Sstevel@tonic-gate 4360Sstevel@tonic-gate /* ARGSUSED */ 4370Sstevel@tonic-gate static int 4380Sstevel@tonic-gate crypto_open(dev_t *devp, int flag, int otyp, cred_t *credp) 4390Sstevel@tonic-gate { 4400Sstevel@tonic-gate crypto_minor_t *cm = NULL; 4410Sstevel@tonic-gate minor_t mn; 4429505SBhargava.Yenduri@Sun.COM kcf_lock_withpad_t *mp; 4439505SBhargava.Yenduri@Sun.COM int i; 4440Sstevel@tonic-gate 4450Sstevel@tonic-gate if (otyp != OTYP_CHR) 4460Sstevel@tonic-gate return (ENXIO); 4470Sstevel@tonic-gate 4480Sstevel@tonic-gate if (crypto_dip == NULL) 4490Sstevel@tonic-gate return (ENXIO); 4500Sstevel@tonic-gate 4510Sstevel@tonic-gate /* exclusive opens are not supported */ 4520Sstevel@tonic-gate if (flag & FEXCL) 4530Sstevel@tonic-gate return (ENOTSUP); 4540Sstevel@tonic-gate 4550Sstevel@tonic-gate again: 4569505SBhargava.Yenduri@Sun.COM mp = &crypto_locks[CPU_SEQID]; 4579505SBhargava.Yenduri@Sun.COM mutex_enter(&mp->kl_lock); 4589505SBhargava.Yenduri@Sun.COM 4590Sstevel@tonic-gate /* grow the minors table if needed */ 4600Sstevel@tonic-gate if (crypto_minors_count >= crypto_minors_table_count) { 4610Sstevel@tonic-gate crypto_minor_t **newtable; 4620Sstevel@tonic-gate minor_t chunk = crypto_minor_chunk; 4630Sstevel@tonic-gate minor_t saved_count; 4640Sstevel@tonic-gate size_t new_size; 4650Sstevel@tonic-gate ulong_t big_count; 4660Sstevel@tonic-gate 4670Sstevel@tonic-gate big_count = crypto_minors_count + chunk; 4680Sstevel@tonic-gate if (big_count > MAXMIN) { 4699505SBhargava.Yenduri@Sun.COM mutex_exit(&mp->kl_lock); 4700Sstevel@tonic-gate return (ENOMEM); 4710Sstevel@tonic-gate } 4720Sstevel@tonic-gate 4730Sstevel@tonic-gate saved_count = crypto_minors_table_count; 4740Sstevel@tonic-gate new_size = sizeof (crypto_minor_t *) * 4750Sstevel@tonic-gate (crypto_minors_table_count + chunk); 4760Sstevel@tonic-gate 4779505SBhargava.Yenduri@Sun.COM mutex_exit(&mp->kl_lock); 4789505SBhargava.Yenduri@Sun.COM 4790Sstevel@tonic-gate newtable = kmem_zalloc(new_size, KM_SLEEP); 4809505SBhargava.Yenduri@Sun.COM CRYPTO_ENTER_ALL_LOCKS(); 4810Sstevel@tonic-gate /* 4820Sstevel@tonic-gate * Check if table grew while we were sleeping. 4830Sstevel@tonic-gate * The minors table never shrinks. 4840Sstevel@tonic-gate */ 4850Sstevel@tonic-gate if (crypto_minors_table_count > saved_count) { 4869505SBhargava.Yenduri@Sun.COM CRYPTO_EXIT_ALL_LOCKS(); 4870Sstevel@tonic-gate kmem_free(newtable, new_size); 4880Sstevel@tonic-gate goto again; 4890Sstevel@tonic-gate } 4900Sstevel@tonic-gate 4910Sstevel@tonic-gate /* we assume that bcopy() will return if count is 0 */ 4920Sstevel@tonic-gate bcopy(crypto_minors, newtable, 4930Sstevel@tonic-gate sizeof (crypto_minor_t *) * crypto_minors_table_count); 4940Sstevel@tonic-gate 4950Sstevel@tonic-gate kmem_free(crypto_minors, 4960Sstevel@tonic-gate sizeof (crypto_minor_t *) * crypto_minors_table_count); 4970Sstevel@tonic-gate 4980Sstevel@tonic-gate /* grow the minors number space */ 4990Sstevel@tonic-gate if (crypto_minors_table_count != 0) { 5000Sstevel@tonic-gate (void) vmem_add(crypto_arena, 5010Sstevel@tonic-gate (void *)(uintptr_t)(crypto_minors_table_count + 1), 5020Sstevel@tonic-gate crypto_minor_chunk, VM_SLEEP); 5030Sstevel@tonic-gate } 5040Sstevel@tonic-gate 5050Sstevel@tonic-gate crypto_minors = newtable; 5060Sstevel@tonic-gate crypto_minors_table_count += chunk; 5079505SBhargava.Yenduri@Sun.COM CRYPTO_EXIT_ALL_LOCKS(); 5089505SBhargava.Yenduri@Sun.COM } else { 5099505SBhargava.Yenduri@Sun.COM mutex_exit(&mp->kl_lock); 5109505SBhargava.Yenduri@Sun.COM } 5110Sstevel@tonic-gate 5120Sstevel@tonic-gate /* allocate a new minor number starting with 1 */ 5130Sstevel@tonic-gate mn = (minor_t)(uintptr_t)vmem_alloc(crypto_arena, 1, VM_SLEEP); 5140Sstevel@tonic-gate 5150Sstevel@tonic-gate cm = kmem_zalloc(sizeof (crypto_minor_t), KM_SLEEP); 5160Sstevel@tonic-gate mutex_init(&cm->cm_lock, NULL, MUTEX_DRIVER, NULL); 5170Sstevel@tonic-gate cv_init(&cm->cm_cv, NULL, CV_DRIVER, NULL); 5180Sstevel@tonic-gate 5199505SBhargava.Yenduri@Sun.COM CRYPTO_ENTER_ALL_LOCKS(); 5207882SBhargava.Yenduri@Sun.COM cm->cm_refcnt = 1; 5210Sstevel@tonic-gate crypto_minors[mn - 1] = cm; 5220Sstevel@tonic-gate crypto_minors_count++; 5239505SBhargava.Yenduri@Sun.COM CRYPTO_EXIT_ALL_LOCKS(); 5240Sstevel@tonic-gate 5250Sstevel@tonic-gate *devp = makedevice(getmajor(*devp), mn); 5260Sstevel@tonic-gate 5270Sstevel@tonic-gate return (0); 5280Sstevel@tonic-gate } 5290Sstevel@tonic-gate 5300Sstevel@tonic-gate /* ARGSUSED */ 5310Sstevel@tonic-gate static int 5320Sstevel@tonic-gate crypto_close(dev_t dev, int flag, int otyp, cred_t *credp) 5330Sstevel@tonic-gate { 5340Sstevel@tonic-gate crypto_minor_t *cm = NULL; 5350Sstevel@tonic-gate crypto_session_data_t *sp; 5360Sstevel@tonic-gate minor_t mn = getminor(dev); 5370Sstevel@tonic-gate uint_t i; 5386424Skrishna size_t total = 0; 5399505SBhargava.Yenduri@Sun.COM kcf_lock_withpad_t *mp; 5409505SBhargava.Yenduri@Sun.COM 5419505SBhargava.Yenduri@Sun.COM mp = &crypto_locks[CPU_SEQID]; 5429505SBhargava.Yenduri@Sun.COM mutex_enter(&mp->kl_lock); 5439505SBhargava.Yenduri@Sun.COM 5440Sstevel@tonic-gate if (mn > crypto_minors_table_count) { 5459505SBhargava.Yenduri@Sun.COM mutex_exit(&mp->kl_lock); 5460Sstevel@tonic-gate cmn_err(CE_WARN, "crypto_close: bad minor (too big) %d", mn); 5470Sstevel@tonic-gate return (ENODEV); 5480Sstevel@tonic-gate } 5490Sstevel@tonic-gate 5507882SBhargava.Yenduri@Sun.COM cm = crypto_minors[mn - 1]; 5510Sstevel@tonic-gate if (cm == NULL) { 5529505SBhargava.Yenduri@Sun.COM mutex_exit(&mp->kl_lock); 5530Sstevel@tonic-gate cmn_err(CE_WARN, "crypto_close: duplicate close of minor %d", 5540Sstevel@tonic-gate getminor(dev)); 5550Sstevel@tonic-gate return (ENODEV); 5560Sstevel@tonic-gate } 5570Sstevel@tonic-gate 5589505SBhargava.Yenduri@Sun.COM mutex_exit(&mp->kl_lock); 5599505SBhargava.Yenduri@Sun.COM 5609505SBhargava.Yenduri@Sun.COM CRYPTO_ENTER_ALL_LOCKS(); 5619505SBhargava.Yenduri@Sun.COM /* 5629505SBhargava.Yenduri@Sun.COM * We free the minor number, mn, from the crypto_arena 5639505SBhargava.Yenduri@Sun.COM * only later. This ensures that we won't race with another 5649505SBhargava.Yenduri@Sun.COM * thread in crypto_open with the same minor number. 5659505SBhargava.Yenduri@Sun.COM */ 5669505SBhargava.Yenduri@Sun.COM crypto_minors[mn - 1] = NULL; 5679505SBhargava.Yenduri@Sun.COM crypto_minors_count--; 5689505SBhargava.Yenduri@Sun.COM CRYPTO_EXIT_ALL_LOCKS(); 5699505SBhargava.Yenduri@Sun.COM 5709505SBhargava.Yenduri@Sun.COM mutex_enter(&cm->cm_lock); 5717882SBhargava.Yenduri@Sun.COM cm->cm_refcnt --; /* decrement refcnt held in open */ 5727882SBhargava.Yenduri@Sun.COM while (cm->cm_refcnt > 0) { 5739505SBhargava.Yenduri@Sun.COM cv_wait(&cm->cm_cv, &cm->cm_lock); 5749505SBhargava.Yenduri@Sun.COM } 5750Sstevel@tonic-gate 5760Sstevel@tonic-gate vmem_free(crypto_arena, (void *)(uintptr_t)mn, 1); 5770Sstevel@tonic-gate 5780Sstevel@tonic-gate /* free all session table entries starting with 1 */ 5790Sstevel@tonic-gate for (i = 1; i < cm->cm_session_table_count; i++) { 5800Sstevel@tonic-gate if (cm->cm_session_table[i] == NULL) 5810Sstevel@tonic-gate continue; 5820Sstevel@tonic-gate 5830Sstevel@tonic-gate sp = cm->cm_session_table[i]; 5840Sstevel@tonic-gate ASSERT((sp->sd_flags & CRYPTO_SESSION_IS_BUSY) == 0); 5856424Skrishna ASSERT(sp->sd_pre_approved_amount == 0 || 5866424Skrishna sp->sd_pre_approved_amount == crypto_pre_approved_limit); 5876424Skrishna total += sp->sd_pre_approved_amount; 5880Sstevel@tonic-gate if (sp->sd_find_init_cookie != NULL) { 5890Sstevel@tonic-gate (void) crypto_free_find_ctx(sp); 5900Sstevel@tonic-gate } 5910Sstevel@tonic-gate crypto_release_provider_session(cm, sp->sd_provider_session); 5920Sstevel@tonic-gate KCF_PROV_REFRELE(sp->sd_provider); 5930Sstevel@tonic-gate CRYPTO_CANCEL_ALL_CTX(sp); 5940Sstevel@tonic-gate mutex_destroy(&sp->sd_lock); 5950Sstevel@tonic-gate cv_destroy(&sp->sd_cv); 5960Sstevel@tonic-gate kmem_cache_free(crypto_session_cache, sp); 5970Sstevel@tonic-gate cm->cm_session_table[i] = NULL; 5980Sstevel@tonic-gate } 5990Sstevel@tonic-gate 6000Sstevel@tonic-gate /* free the session table */ 6010Sstevel@tonic-gate if (cm->cm_session_table != NULL && cm->cm_session_table_count > 0) 6020Sstevel@tonic-gate kmem_free(cm->cm_session_table, cm->cm_session_table_count * 6030Sstevel@tonic-gate sizeof (void *)); 6040Sstevel@tonic-gate 6056424Skrishna total += (cm->cm_session_table_count * sizeof (void *)); 6066424Skrishna CRYPTO_DECREMENT_RCTL(total); 6070Sstevel@tonic-gate 6080Sstevel@tonic-gate kcf_free_provider_tab(cm->cm_provider_count, 6090Sstevel@tonic-gate cm->cm_provider_array); 6100Sstevel@tonic-gate 6119505SBhargava.Yenduri@Sun.COM mutex_exit(&cm->cm_lock); 6120Sstevel@tonic-gate mutex_destroy(&cm->cm_lock); 6130Sstevel@tonic-gate cv_destroy(&cm->cm_cv); 6140Sstevel@tonic-gate kmem_free(cm, sizeof (crypto_minor_t)); 6150Sstevel@tonic-gate 6160Sstevel@tonic-gate return (0); 6170Sstevel@tonic-gate } 6180Sstevel@tonic-gate 6190Sstevel@tonic-gate static crypto_minor_t * 6200Sstevel@tonic-gate crypto_hold_minor(minor_t minor) 6210Sstevel@tonic-gate { 6227882SBhargava.Yenduri@Sun.COM crypto_minor_t *cm; 6239505SBhargava.Yenduri@Sun.COM kcf_lock_withpad_t *mp; 6247882SBhargava.Yenduri@Sun.COM 6257882SBhargava.Yenduri@Sun.COM if (minor > crypto_minors_table_count) 6267882SBhargava.Yenduri@Sun.COM return (NULL); 6270Sstevel@tonic-gate 6289505SBhargava.Yenduri@Sun.COM mp = &crypto_locks[CPU_SEQID]; 6299505SBhargava.Yenduri@Sun.COM mutex_enter(&mp->kl_lock); 6309505SBhargava.Yenduri@Sun.COM 6317882SBhargava.Yenduri@Sun.COM if ((cm = crypto_minors[minor - 1]) != NULL) { 6329505SBhargava.Yenduri@Sun.COM atomic_add_32(&cm->cm_refcnt, 1); 6339505SBhargava.Yenduri@Sun.COM } 6349505SBhargava.Yenduri@Sun.COM mutex_exit(&mp->kl_lock); 6350Sstevel@tonic-gate return (cm); 6360Sstevel@tonic-gate } 6370Sstevel@tonic-gate 6380Sstevel@tonic-gate static void 6390Sstevel@tonic-gate crypto_release_minor(crypto_minor_t *cm) 6400Sstevel@tonic-gate { 6419505SBhargava.Yenduri@Sun.COM if (atomic_add_32_nv(&cm->cm_refcnt, -1) == 0) { 6427882SBhargava.Yenduri@Sun.COM cv_signal(&cm->cm_cv); 6430Sstevel@tonic-gate } 6440Sstevel@tonic-gate } 6450Sstevel@tonic-gate 6460Sstevel@tonic-gate /* 6474072Skrishna * Build a list of functions and other information for the provider, pd. 6480Sstevel@tonic-gate */ 6490Sstevel@tonic-gate static void 6500Sstevel@tonic-gate crypto_build_function_list(crypto_function_list_t *fl, kcf_provider_desc_t *pd) 6510Sstevel@tonic-gate { 6520Sstevel@tonic-gate crypto_ops_t *ops; 6530Sstevel@tonic-gate crypto_digest_ops_t *digest_ops; 6540Sstevel@tonic-gate crypto_cipher_ops_t *cipher_ops; 6550Sstevel@tonic-gate crypto_mac_ops_t *mac_ops; 6560Sstevel@tonic-gate crypto_sign_ops_t *sign_ops; 6570Sstevel@tonic-gate crypto_verify_ops_t *verify_ops; 6580Sstevel@tonic-gate crypto_dual_ops_t *dual_ops; 6590Sstevel@tonic-gate crypto_random_number_ops_t *random_number_ops; 6600Sstevel@tonic-gate crypto_session_ops_t *session_ops; 6610Sstevel@tonic-gate crypto_object_ops_t *object_ops; 6620Sstevel@tonic-gate crypto_key_ops_t *key_ops; 6630Sstevel@tonic-gate crypto_provider_management_ops_t *provider_ops; 6640Sstevel@tonic-gate 6650Sstevel@tonic-gate if ((ops = pd->pd_ops_vector) == NULL) 6660Sstevel@tonic-gate return; 6670Sstevel@tonic-gate 668904Smcpowers if ((digest_ops = ops->co_digest_ops) != NULL) { 6690Sstevel@tonic-gate if (digest_ops->digest_init != NULL) 6700Sstevel@tonic-gate fl->fl_digest_init = B_TRUE; 6710Sstevel@tonic-gate if (digest_ops->digest != NULL) 6720Sstevel@tonic-gate fl->fl_digest = B_TRUE; 6730Sstevel@tonic-gate if (digest_ops->digest_update != NULL) 6740Sstevel@tonic-gate fl->fl_digest_update = B_TRUE; 6750Sstevel@tonic-gate if (digest_ops->digest_key != NULL) 6760Sstevel@tonic-gate fl->fl_digest_key = B_TRUE; 6770Sstevel@tonic-gate if (digest_ops->digest_final != NULL) 6780Sstevel@tonic-gate fl->fl_digest_final = B_TRUE; 6790Sstevel@tonic-gate } 680904Smcpowers if ((cipher_ops = ops->co_cipher_ops) != NULL) { 6810Sstevel@tonic-gate if (cipher_ops->encrypt_init != NULL) 6820Sstevel@tonic-gate fl->fl_encrypt_init = B_TRUE; 6830Sstevel@tonic-gate if (cipher_ops->encrypt != NULL) 6840Sstevel@tonic-gate fl->fl_encrypt = B_TRUE; 6850Sstevel@tonic-gate if (cipher_ops->encrypt_update != NULL) 6860Sstevel@tonic-gate fl->fl_encrypt_update = B_TRUE; 6870Sstevel@tonic-gate if (cipher_ops->encrypt_final != NULL) 6880Sstevel@tonic-gate fl->fl_encrypt_final = B_TRUE; 6890Sstevel@tonic-gate if (cipher_ops->decrypt_init != NULL) 6900Sstevel@tonic-gate fl->fl_decrypt_init = B_TRUE; 6910Sstevel@tonic-gate if (cipher_ops->decrypt != NULL) 6920Sstevel@tonic-gate fl->fl_decrypt = B_TRUE; 6930Sstevel@tonic-gate if (cipher_ops->decrypt_update != NULL) 6940Sstevel@tonic-gate fl->fl_decrypt_update = B_TRUE; 6950Sstevel@tonic-gate if (cipher_ops->decrypt_final != NULL) 6960Sstevel@tonic-gate fl->fl_decrypt_final = B_TRUE; 6970Sstevel@tonic-gate } 698904Smcpowers if ((mac_ops = ops->co_mac_ops) != NULL) { 6990Sstevel@tonic-gate if (mac_ops->mac_init != NULL) 7000Sstevel@tonic-gate fl->fl_mac_init = B_TRUE; 7010Sstevel@tonic-gate if (mac_ops->mac != NULL) 7020Sstevel@tonic-gate fl->fl_mac = B_TRUE; 7030Sstevel@tonic-gate if (mac_ops->mac_update != NULL) 7040Sstevel@tonic-gate fl->fl_mac_update = B_TRUE; 7050Sstevel@tonic-gate if (mac_ops->mac_final != NULL) 7060Sstevel@tonic-gate fl->fl_mac_final = B_TRUE; 7070Sstevel@tonic-gate } 708904Smcpowers if ((sign_ops = ops->co_sign_ops) != NULL) { 7090Sstevel@tonic-gate if (sign_ops->sign_init != NULL) 7100Sstevel@tonic-gate fl->fl_sign_init = B_TRUE; 7110Sstevel@tonic-gate if (sign_ops->sign != NULL) 7120Sstevel@tonic-gate fl->fl_sign = B_TRUE; 7130Sstevel@tonic-gate if (sign_ops->sign_update != NULL) 7140Sstevel@tonic-gate fl->fl_sign_update = B_TRUE; 7150Sstevel@tonic-gate if (sign_ops->sign_final != NULL) 7160Sstevel@tonic-gate fl->fl_sign_final = B_TRUE; 7170Sstevel@tonic-gate if (sign_ops->sign_recover_init != NULL) 7180Sstevel@tonic-gate fl->fl_sign_recover_init = B_TRUE; 7190Sstevel@tonic-gate if (sign_ops->sign_recover != NULL) 7200Sstevel@tonic-gate fl->fl_sign_recover = B_TRUE; 7210Sstevel@tonic-gate } 722904Smcpowers if ((verify_ops = ops->co_verify_ops) != NULL) { 7230Sstevel@tonic-gate if (verify_ops->verify_init != NULL) 7240Sstevel@tonic-gate fl->fl_verify_init = B_TRUE; 7250Sstevel@tonic-gate if (verify_ops->verify != NULL) 7260Sstevel@tonic-gate fl->fl_verify = B_TRUE; 7270Sstevel@tonic-gate if (verify_ops->verify_update != NULL) 7280Sstevel@tonic-gate fl->fl_verify_update = B_TRUE; 7290Sstevel@tonic-gate if (verify_ops->verify_final != NULL) 7300Sstevel@tonic-gate fl->fl_verify_final = B_TRUE; 7310Sstevel@tonic-gate if (verify_ops->verify_recover_init != NULL) 7320Sstevel@tonic-gate fl->fl_verify_recover_init = B_TRUE; 7330Sstevel@tonic-gate if (verify_ops->verify_recover != NULL) 7340Sstevel@tonic-gate fl->fl_verify_recover = B_TRUE; 7350Sstevel@tonic-gate } 736904Smcpowers if ((dual_ops = ops->co_dual_ops) != NULL) { 7370Sstevel@tonic-gate if (dual_ops->digest_encrypt_update != NULL) 7380Sstevel@tonic-gate fl->fl_digest_encrypt_update = B_TRUE; 7390Sstevel@tonic-gate if (dual_ops->decrypt_digest_update != NULL) 7400Sstevel@tonic-gate fl->fl_decrypt_digest_update = B_TRUE; 7410Sstevel@tonic-gate if (dual_ops->sign_encrypt_update != NULL) 7420Sstevel@tonic-gate fl->fl_sign_encrypt_update = B_TRUE; 7430Sstevel@tonic-gate if (dual_ops->decrypt_verify_update != NULL) 7440Sstevel@tonic-gate fl->fl_decrypt_verify_update = B_TRUE; 7450Sstevel@tonic-gate } 746904Smcpowers if ((random_number_ops = ops->co_random_ops) != NULL) { 7470Sstevel@tonic-gate if (random_number_ops->seed_random != NULL) 7480Sstevel@tonic-gate fl->fl_seed_random = B_TRUE; 7490Sstevel@tonic-gate if (random_number_ops->generate_random != NULL) 7500Sstevel@tonic-gate fl->fl_generate_random = B_TRUE; 7510Sstevel@tonic-gate } 752904Smcpowers if ((session_ops = ops->co_session_ops) != NULL) { 7530Sstevel@tonic-gate if (session_ops->session_open != NULL) 7540Sstevel@tonic-gate fl->fl_session_open = B_TRUE; 7550Sstevel@tonic-gate if (session_ops->session_close != NULL) 7560Sstevel@tonic-gate fl->fl_session_close = B_TRUE; 7570Sstevel@tonic-gate if (session_ops->session_login != NULL) 7580Sstevel@tonic-gate fl->fl_session_login = B_TRUE; 7590Sstevel@tonic-gate if (session_ops->session_logout != NULL) 7600Sstevel@tonic-gate fl->fl_session_logout = B_TRUE; 7610Sstevel@tonic-gate } 762904Smcpowers if ((object_ops = ops->co_object_ops) != NULL) { 7630Sstevel@tonic-gate if (object_ops->object_create != NULL) 7640Sstevel@tonic-gate fl->fl_object_create = B_TRUE; 7650Sstevel@tonic-gate if (object_ops->object_copy != NULL) 7660Sstevel@tonic-gate fl->fl_object_copy = B_TRUE; 7670Sstevel@tonic-gate if (object_ops->object_destroy != NULL) 7680Sstevel@tonic-gate fl->fl_object_destroy = B_TRUE; 7690Sstevel@tonic-gate if (object_ops->object_get_size != NULL) 7700Sstevel@tonic-gate fl->fl_object_get_size = B_TRUE; 7710Sstevel@tonic-gate if (object_ops->object_get_attribute_value != NULL) 7720Sstevel@tonic-gate fl->fl_object_get_attribute_value = B_TRUE; 7730Sstevel@tonic-gate if (object_ops->object_set_attribute_value != NULL) 7740Sstevel@tonic-gate fl->fl_object_set_attribute_value = B_TRUE; 7750Sstevel@tonic-gate if (object_ops->object_find_init != NULL) 7760Sstevel@tonic-gate fl->fl_object_find_init = B_TRUE; 7770Sstevel@tonic-gate if (object_ops->object_find != NULL) 7780Sstevel@tonic-gate fl->fl_object_find = B_TRUE; 7790Sstevel@tonic-gate if (object_ops->object_find_final != NULL) 7800Sstevel@tonic-gate fl->fl_object_find_final = B_TRUE; 7810Sstevel@tonic-gate } 782904Smcpowers if ((key_ops = ops->co_key_ops) != NULL) { 7830Sstevel@tonic-gate if (key_ops->key_generate != NULL) 7840Sstevel@tonic-gate fl->fl_key_generate = B_TRUE; 7850Sstevel@tonic-gate if (key_ops->key_generate_pair != NULL) 7860Sstevel@tonic-gate fl->fl_key_generate_pair = B_TRUE; 7870Sstevel@tonic-gate if (key_ops->key_wrap != NULL) 7880Sstevel@tonic-gate fl->fl_key_wrap = B_TRUE; 7890Sstevel@tonic-gate if (key_ops->key_unwrap != NULL) 7900Sstevel@tonic-gate fl->fl_key_unwrap = B_TRUE; 7910Sstevel@tonic-gate if (key_ops->key_derive != NULL) 7920Sstevel@tonic-gate fl->fl_key_derive = B_TRUE; 7930Sstevel@tonic-gate } 794904Smcpowers if ((provider_ops = ops->co_provider_ops) != NULL) { 7950Sstevel@tonic-gate if (provider_ops->init_token != NULL) 7960Sstevel@tonic-gate fl->fl_init_token = B_TRUE; 7970Sstevel@tonic-gate if (provider_ops->init_pin != NULL) 7980Sstevel@tonic-gate fl->fl_init_pin = B_TRUE; 7990Sstevel@tonic-gate if (provider_ops->set_pin != NULL) 8000Sstevel@tonic-gate fl->fl_set_pin = B_TRUE; 8010Sstevel@tonic-gate } 8024072Skrishna 8034072Skrishna fl->prov_is_limited = pd->pd_flags & CRYPTO_HASH_NO_UPDATE; 8044072Skrishna if (fl->prov_is_limited) { 8054072Skrishna /* 8064072Skrishna * XXX - The threshold should ideally be per hash 8074072Skrishna * mechanism. For now, we use the same value for all 8084072Skrishna * hash mechanisms. Empirical evidence suggests this 8094072Skrishna * is fine. 8104072Skrishna */ 8114072Skrishna fl->prov_hash_threshold = kcf_md5_threshold; 8124072Skrishna fl->prov_hash_limit = min(pd->pd_hash_limit, 8134072Skrishna min(CRYPTO_MAX_BUFFER_LEN, 8144072Skrishna curproc->p_task->tk_proj->kpj_data.kpd_crypto_mem_ctl)); 8154072Skrishna } 8164841Shaimay 8174841Shaimay fl->total_threshold_count = MAX_NUM_THRESHOLD; 8184841Shaimay fl->fl_threshold[0].mech_type = CKM_DES3_CBC; 8194841Shaimay fl->fl_threshold[0].mech_threshold = kcf_des3_threshold; 8204841Shaimay fl->fl_threshold[1].mech_type = CKM_DES3_ECB; 8214841Shaimay fl->fl_threshold[1].mech_threshold = kcf_des3_threshold; 8224841Shaimay fl->fl_threshold[2].mech_type = CKM_AES_CBC; 8234841Shaimay fl->fl_threshold[2].mech_threshold = kcf_aes_threshold; 8244841Shaimay fl->fl_threshold[3].mech_type = CKM_AES_ECB; 8254841Shaimay fl->fl_threshold[3].mech_threshold = kcf_aes_threshold; 8264841Shaimay fl->fl_threshold[4].mech_type = CKM_RC4; 8274841Shaimay fl->fl_threshold[4].mech_threshold = kcf_rc4_threshold; 8284841Shaimay fl->fl_threshold[5].mech_type = CKM_MD5; 8294841Shaimay fl->fl_threshold[5].mech_threshold = kcf_md5_threshold; 8304841Shaimay fl->fl_threshold[6].mech_type = CKM_SHA_1; 8314841Shaimay fl->fl_threshold[6].mech_threshold = kcf_sha1_threshold; 8320Sstevel@tonic-gate } 8330Sstevel@tonic-gate 8340Sstevel@tonic-gate /* ARGSUSED */ 8350Sstevel@tonic-gate static int 8360Sstevel@tonic-gate get_function_list(dev_t dev, caddr_t arg, int mode, int *rval) 8370Sstevel@tonic-gate { 8380Sstevel@tonic-gate crypto_get_function_list_t get_function_list; 8390Sstevel@tonic-gate crypto_minor_t *cm; 8400Sstevel@tonic-gate crypto_provider_id_t provider_id; 8410Sstevel@tonic-gate crypto_function_list_t *fl; 8420Sstevel@tonic-gate kcf_provider_desc_t *provider; 8430Sstevel@tonic-gate int rv; 8440Sstevel@tonic-gate 8450Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 8460Sstevel@tonic-gate cmn_err(CE_WARN, "get_function_list: failed holding minor"); 8470Sstevel@tonic-gate return (ENXIO); 8480Sstevel@tonic-gate } 8490Sstevel@tonic-gate 8500Sstevel@tonic-gate if (copyin(arg, &get_function_list, sizeof (get_function_list)) != 0) { 8510Sstevel@tonic-gate crypto_release_minor(cm); 8520Sstevel@tonic-gate return (EFAULT); 8530Sstevel@tonic-gate } 8540Sstevel@tonic-gate 8550Sstevel@tonic-gate /* initialize provider_array */ 8560Sstevel@tonic-gate if (cm->cm_provider_array == NULL) { 8570Sstevel@tonic-gate rv = crypto_get_provider_list(cm, NULL, NULL, DONT_RETURN_LIST); 8580Sstevel@tonic-gate if (rv != CRYPTO_SUCCESS) { 8590Sstevel@tonic-gate goto release_minor; 8600Sstevel@tonic-gate } 8610Sstevel@tonic-gate } 8620Sstevel@tonic-gate 8630Sstevel@tonic-gate provider_id = get_function_list.fl_provider_id; 8640Sstevel@tonic-gate mutex_enter(&cm->cm_lock); 8650Sstevel@tonic-gate /* index must be less than count of providers */ 8660Sstevel@tonic-gate if (provider_id >= cm->cm_provider_count) { 8670Sstevel@tonic-gate mutex_exit(&cm->cm_lock); 8680Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD; 8690Sstevel@tonic-gate goto release_minor; 8700Sstevel@tonic-gate } 8710Sstevel@tonic-gate 8720Sstevel@tonic-gate ASSERT(cm->cm_provider_array != NULL); 8730Sstevel@tonic-gate provider = cm->cm_provider_array[provider_id]; 8740Sstevel@tonic-gate mutex_exit(&cm->cm_lock); 8750Sstevel@tonic-gate 8760Sstevel@tonic-gate fl = &get_function_list.fl_list; 8770Sstevel@tonic-gate bzero(fl, sizeof (crypto_function_list_t)); 8780Sstevel@tonic-gate 8790Sstevel@tonic-gate if (provider->pd_prov_type != CRYPTO_LOGICAL_PROVIDER) { 8800Sstevel@tonic-gate crypto_build_function_list(fl, provider); 8810Sstevel@tonic-gate } else { 8820Sstevel@tonic-gate kcf_provider_desc_t *prev = NULL, *pd; 8830Sstevel@tonic-gate 8840Sstevel@tonic-gate mutex_enter(&provider->pd_lock); 8850Sstevel@tonic-gate while (kcf_get_next_logical_provider_member(provider, 8860Sstevel@tonic-gate prev, &pd)) { 8870Sstevel@tonic-gate prev = pd; 8880Sstevel@tonic-gate crypto_build_function_list(fl, pd); 8890Sstevel@tonic-gate KCF_PROV_REFRELE(pd); 8900Sstevel@tonic-gate } 8910Sstevel@tonic-gate mutex_exit(&provider->pd_lock); 8920Sstevel@tonic-gate } 8930Sstevel@tonic-gate 8940Sstevel@tonic-gate rv = CRYPTO_SUCCESS; 8950Sstevel@tonic-gate 8960Sstevel@tonic-gate release_minor: 8970Sstevel@tonic-gate crypto_release_minor(cm); 8980Sstevel@tonic-gate 8990Sstevel@tonic-gate get_function_list.fl_return_value = rv; 9000Sstevel@tonic-gate 9010Sstevel@tonic-gate if (copyout(&get_function_list, arg, sizeof (get_function_list)) != 0) { 9020Sstevel@tonic-gate return (EFAULT); 9030Sstevel@tonic-gate } 9040Sstevel@tonic-gate return (0); 9050Sstevel@tonic-gate } 9060Sstevel@tonic-gate 9070Sstevel@tonic-gate /* 9080Sstevel@tonic-gate * This ioctl maps a PKCS#11 mechanism string into an internal number 9090Sstevel@tonic-gate * that is used by the kernel. pn_internal_number is set to the 9100Sstevel@tonic-gate * internal number. 9110Sstevel@tonic-gate */ 9120Sstevel@tonic-gate /* ARGSUSED */ 9130Sstevel@tonic-gate static int 9140Sstevel@tonic-gate get_mechanism_number(dev_t dev, caddr_t arg, int mode, int *rval) 9150Sstevel@tonic-gate { 9160Sstevel@tonic-gate STRUCT_DECL(crypto_get_mechanism_number, get_number); 9170Sstevel@tonic-gate crypto_mech_type_t number; 9180Sstevel@tonic-gate size_t len; 9190Sstevel@tonic-gate char *mechanism_name; 9200Sstevel@tonic-gate int rv; 9210Sstevel@tonic-gate 9220Sstevel@tonic-gate STRUCT_INIT(get_number, mode); 9230Sstevel@tonic-gate 9240Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(get_number), STRUCT_SIZE(get_number)) != 0) 9250Sstevel@tonic-gate return (EFAULT); 9260Sstevel@tonic-gate 9270Sstevel@tonic-gate len = STRUCT_FGET(get_number, pn_mechanism_len); 9280Sstevel@tonic-gate if (len == 0 || len > CRYPTO_MAX_MECH_NAME) { 9290Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD; 9300Sstevel@tonic-gate goto out; 9310Sstevel@tonic-gate } 9320Sstevel@tonic-gate mechanism_name = kmem_alloc(len, KM_SLEEP); 9330Sstevel@tonic-gate 9340Sstevel@tonic-gate if (copyin(STRUCT_FGETP(get_number, pn_mechanism_string), 9350Sstevel@tonic-gate mechanism_name, len) != 0) { 9360Sstevel@tonic-gate kmem_free(mechanism_name, len); 9370Sstevel@tonic-gate return (EFAULT); 9380Sstevel@tonic-gate } 9390Sstevel@tonic-gate 9402935Skrishna /* 9412935Skrishna * Get mechanism number from kcf. We set the load_module 9422935Skrishna * flag to false since we use only hardware providers. 9432935Skrishna */ 9442935Skrishna number = crypto_mech2id_common(mechanism_name, B_FALSE); 9450Sstevel@tonic-gate kmem_free(mechanism_name, len); 9460Sstevel@tonic-gate if (number == CRYPTO_MECH_INVALID) { 9470Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD; 9480Sstevel@tonic-gate goto out; 9490Sstevel@tonic-gate } 9500Sstevel@tonic-gate 9510Sstevel@tonic-gate bcopy((char *)&number, (char *)STRUCT_FADDR(get_number, 9520Sstevel@tonic-gate pn_internal_number), sizeof (number)); 9530Sstevel@tonic-gate 9540Sstevel@tonic-gate rv = CRYPTO_SUCCESS; 9550Sstevel@tonic-gate out: 9560Sstevel@tonic-gate STRUCT_FSET(get_number, pn_return_value, rv); 9570Sstevel@tonic-gate 9580Sstevel@tonic-gate if (copyout(STRUCT_BUF(get_number), arg, 9590Sstevel@tonic-gate STRUCT_SIZE(get_number)) != 0) { 9600Sstevel@tonic-gate return (EFAULT); 9610Sstevel@tonic-gate } 9620Sstevel@tonic-gate return (0); 9630Sstevel@tonic-gate } 9640Sstevel@tonic-gate 9650Sstevel@tonic-gate /* 9668313SDina.Nimeh@Sun.Com * This ioctl returns an array of crypto_mech_name_t entries. 9678313SDina.Nimeh@Sun.Com * It lists all the PKCS#11 mechanisms available in the kernel. 9688313SDina.Nimeh@Sun.Com */ 9698313SDina.Nimeh@Sun.Com /* ARGSUSED */ 9708313SDina.Nimeh@Sun.Com static int 9718313SDina.Nimeh@Sun.Com get_mechanism_list(dev_t dev, caddr_t arg, int mode, int *rval) 9728313SDina.Nimeh@Sun.Com { 9738313SDina.Nimeh@Sun.Com STRUCT_DECL(crypto_get_mechanism_list, get_list); 9748313SDina.Nimeh@Sun.Com crypto_mech_name_t *entries; 9758313SDina.Nimeh@Sun.Com size_t copyout_size; 9768313SDina.Nimeh@Sun.Com uint_t req_count; 9778313SDina.Nimeh@Sun.Com uint_t count; 9788313SDina.Nimeh@Sun.Com ulong_t offset; 9798313SDina.Nimeh@Sun.Com int error = 0; 9808313SDina.Nimeh@Sun.Com 9818313SDina.Nimeh@Sun.Com STRUCT_INIT(get_list, mode); 9828313SDina.Nimeh@Sun.Com 9838313SDina.Nimeh@Sun.Com if (copyin(arg, STRUCT_BUF(get_list), STRUCT_SIZE(get_list)) != 0) { 9848313SDina.Nimeh@Sun.Com return (EFAULT); 9858313SDina.Nimeh@Sun.Com } 9868313SDina.Nimeh@Sun.Com 9878313SDina.Nimeh@Sun.Com entries = crypto_get_mech_list(&count, KM_SLEEP); 9888313SDina.Nimeh@Sun.Com 9898313SDina.Nimeh@Sun.Com /* Number of entries caller thinks we have */ 9908313SDina.Nimeh@Sun.Com req_count = STRUCT_FGET(get_list, ml_count); 9918313SDina.Nimeh@Sun.Com 9928313SDina.Nimeh@Sun.Com STRUCT_FSET(get_list, ml_count, count); 9938313SDina.Nimeh@Sun.Com STRUCT_FSET(get_list, ml_return_value, CRYPTO_SUCCESS); 9948313SDina.Nimeh@Sun.Com 9958313SDina.Nimeh@Sun.Com /* check if buffer is too small */ 9968313SDina.Nimeh@Sun.Com if (count > req_count) { 9978313SDina.Nimeh@Sun.Com STRUCT_FSET(get_list, ml_return_value, CRYPTO_BUFFER_TOO_SMALL); 9988313SDina.Nimeh@Sun.Com } 9998313SDina.Nimeh@Sun.Com 10008313SDina.Nimeh@Sun.Com /* copyout the first stuff */ 10018313SDina.Nimeh@Sun.Com if (copyout(STRUCT_BUF(get_list), arg, STRUCT_SIZE(get_list)) != 0) { 10028313SDina.Nimeh@Sun.Com error = EFAULT; 10038313SDina.Nimeh@Sun.Com } 10048313SDina.Nimeh@Sun.Com 10058313SDina.Nimeh@Sun.Com /* 10068313SDina.Nimeh@Sun.Com * If only requesting number of entries or buffer too small or an 10078313SDina.Nimeh@Sun.Com * error occurred, stop here 10088313SDina.Nimeh@Sun.Com */ 10098313SDina.Nimeh@Sun.Com if (req_count == 0 || count > req_count || error != 0) { 10108313SDina.Nimeh@Sun.Com goto out; 10118313SDina.Nimeh@Sun.Com } 10128313SDina.Nimeh@Sun.Com 10138313SDina.Nimeh@Sun.Com copyout_size = count * sizeof (crypto_mech_name_t); 10148313SDina.Nimeh@Sun.Com 10158313SDina.Nimeh@Sun.Com /* copyout entries */ 10168313SDina.Nimeh@Sun.Com offset = (ulong_t)STRUCT_FADDR(get_list, ml_list); 10178313SDina.Nimeh@Sun.Com offset -= (ulong_t)STRUCT_BUF(get_list); 10188313SDina.Nimeh@Sun.Com if (copyout(entries, arg + offset, copyout_size) != 0) { 10198313SDina.Nimeh@Sun.Com error = EFAULT; 10208313SDina.Nimeh@Sun.Com } 10218313SDina.Nimeh@Sun.Com 10228313SDina.Nimeh@Sun.Com out: 10238313SDina.Nimeh@Sun.Com crypto_free_mech_list(entries, count); 10248313SDina.Nimeh@Sun.Com return (error); 10258313SDina.Nimeh@Sun.Com } 10268313SDina.Nimeh@Sun.Com 10278313SDina.Nimeh@Sun.Com /* 10288313SDina.Nimeh@Sun.Com * Copyout kernel array of mech_infos to user space. 10298313SDina.Nimeh@Sun.Com */ 10308313SDina.Nimeh@Sun.Com /* ARGSUSED */ 10318313SDina.Nimeh@Sun.Com static int 10328313SDina.Nimeh@Sun.Com copyout_mechinfos(int mode, caddr_t out, uint_t count, 10338313SDina.Nimeh@Sun.Com crypto_mechanism_info_t *k_minfos, caddr_t u_minfos) 10348313SDina.Nimeh@Sun.Com { 10358313SDina.Nimeh@Sun.Com STRUCT_DECL(crypto_mechanism_info, mi); 10368313SDina.Nimeh@Sun.Com caddr_t p; 10378313SDina.Nimeh@Sun.Com size_t len; 10388313SDina.Nimeh@Sun.Com int i; 10398313SDina.Nimeh@Sun.Com 10408313SDina.Nimeh@Sun.Com if (count == 0) 10418313SDina.Nimeh@Sun.Com return (0); 10428313SDina.Nimeh@Sun.Com 10438313SDina.Nimeh@Sun.Com STRUCT_INIT(mi, mode); 10448313SDina.Nimeh@Sun.Com 10458313SDina.Nimeh@Sun.Com len = count * STRUCT_SIZE(mi); 10468313SDina.Nimeh@Sun.Com 10478313SDina.Nimeh@Sun.Com ASSERT(u_minfos != NULL); 10488313SDina.Nimeh@Sun.Com p = u_minfos; 10498313SDina.Nimeh@Sun.Com for (i = 0; i < count; i++) { 10508313SDina.Nimeh@Sun.Com STRUCT_FSET(mi, mi_min_key_size, k_minfos[i].mi_min_key_size); 10518313SDina.Nimeh@Sun.Com STRUCT_FSET(mi, mi_max_key_size, k_minfos[i].mi_max_key_size); 10528313SDina.Nimeh@Sun.Com STRUCT_FSET(mi, mi_keysize_unit, k_minfos[i].mi_keysize_unit); 10538313SDina.Nimeh@Sun.Com STRUCT_FSET(mi, mi_usage, k_minfos[i].mi_usage); 10548313SDina.Nimeh@Sun.Com bcopy(STRUCT_BUF(mi), p, STRUCT_SIZE(mi)); 10558313SDina.Nimeh@Sun.Com p += STRUCT_SIZE(mi); 10568313SDina.Nimeh@Sun.Com } 10578313SDina.Nimeh@Sun.Com 10588313SDina.Nimeh@Sun.Com if (copyout(u_minfos, out, len) != 0) 10598313SDina.Nimeh@Sun.Com return (EFAULT); 10608313SDina.Nimeh@Sun.Com 10618313SDina.Nimeh@Sun.Com return (0); 10628313SDina.Nimeh@Sun.Com } 10638313SDina.Nimeh@Sun.Com 10648313SDina.Nimeh@Sun.Com /* 10658313SDina.Nimeh@Sun.Com * This ioctl returns information for the specified mechanism. 10668313SDina.Nimeh@Sun.Com */ 10678313SDina.Nimeh@Sun.Com /* ARGSUSED */ 10688313SDina.Nimeh@Sun.Com static int 10698313SDina.Nimeh@Sun.Com get_all_mechanism_info(dev_t dev, caddr_t arg, int mode, int *rval) 10708313SDina.Nimeh@Sun.Com { 10718313SDina.Nimeh@Sun.Com STRUCT_DECL(crypto_get_all_mechanism_info, get_all_mech); 10728319SDina.Nimeh@Sun.Com /* LINTED E_FUNC_SET_NOT_USED */ 10738313SDina.Nimeh@Sun.Com STRUCT_DECL(crypto_mechanism_info, mi); 10748313SDina.Nimeh@Sun.Com crypto_mech_name_t mech_name; 10758313SDina.Nimeh@Sun.Com crypto_mech_type_t mech_type; 10768313SDina.Nimeh@Sun.Com crypto_mechanism_info_t *mech_infos = NULL; 10778313SDina.Nimeh@Sun.Com uint_t num_mech_infos = 0; 10788313SDina.Nimeh@Sun.Com uint_t req_count; 10798313SDina.Nimeh@Sun.Com caddr_t u_minfos; 10808313SDina.Nimeh@Sun.Com ulong_t offset; 10818313SDina.Nimeh@Sun.Com int error = 0; 10828313SDina.Nimeh@Sun.Com int rv; 10838313SDina.Nimeh@Sun.Com 10848313SDina.Nimeh@Sun.Com STRUCT_INIT(get_all_mech, mode); 10858313SDina.Nimeh@Sun.Com STRUCT_INIT(mi, mode); 10868313SDina.Nimeh@Sun.Com 10878313SDina.Nimeh@Sun.Com if (copyin(arg, STRUCT_BUF(get_all_mech), 10888313SDina.Nimeh@Sun.Com STRUCT_SIZE(get_all_mech)) != 0) { 10898313SDina.Nimeh@Sun.Com return (EFAULT); 10908313SDina.Nimeh@Sun.Com } 10918313SDina.Nimeh@Sun.Com 10928313SDina.Nimeh@Sun.Com (void) strncpy(mech_name, STRUCT_FGET(get_all_mech, mi_mechanism_name), 10938313SDina.Nimeh@Sun.Com CRYPTO_MAX_MECH_NAME); 10948313SDina.Nimeh@Sun.Com mech_type = crypto_mech2id(mech_name); 10958313SDina.Nimeh@Sun.Com 10968313SDina.Nimeh@Sun.Com if (mech_type == CRYPTO_MECH_INVALID) { 10978313SDina.Nimeh@Sun.Com rv = CRYPTO_ARGUMENTS_BAD; 10988313SDina.Nimeh@Sun.Com goto out1; 10998313SDina.Nimeh@Sun.Com } 11008313SDina.Nimeh@Sun.Com 11018313SDina.Nimeh@Sun.Com rv = crypto_get_all_mech_info(mech_type, &mech_infos, &num_mech_infos, 11028313SDina.Nimeh@Sun.Com KM_SLEEP); 11038313SDina.Nimeh@Sun.Com if (rv != CRYPTO_SUCCESS) { 11048313SDina.Nimeh@Sun.Com goto out1; 11058313SDina.Nimeh@Sun.Com } 11068313SDina.Nimeh@Sun.Com /* rv is CRYPTO_SUCCESS at this point */ 11078313SDina.Nimeh@Sun.Com 11088313SDina.Nimeh@Sun.Com /* Number of entries caller thinks we have */ 11098313SDina.Nimeh@Sun.Com req_count = STRUCT_FGET(get_all_mech, mi_count); 11108313SDina.Nimeh@Sun.Com 11118313SDina.Nimeh@Sun.Com STRUCT_FSET(get_all_mech, mi_count, num_mech_infos); 11128313SDina.Nimeh@Sun.Com 11138313SDina.Nimeh@Sun.Com /* check if buffer is too small */ 11148313SDina.Nimeh@Sun.Com if (num_mech_infos > req_count) { 11158313SDina.Nimeh@Sun.Com rv = CRYPTO_BUFFER_TOO_SMALL; 11168313SDina.Nimeh@Sun.Com } 11178313SDina.Nimeh@Sun.Com 11188313SDina.Nimeh@Sun.Com out1: 11198313SDina.Nimeh@Sun.Com STRUCT_FSET(get_all_mech, mi_return_value, rv); 11208313SDina.Nimeh@Sun.Com 11218313SDina.Nimeh@Sun.Com /* copy the first part */ 11228313SDina.Nimeh@Sun.Com if (copyout(STRUCT_BUF(get_all_mech), arg, 11238313SDina.Nimeh@Sun.Com STRUCT_SIZE(get_all_mech)) != 0) { 11248313SDina.Nimeh@Sun.Com error = EFAULT; 11258313SDina.Nimeh@Sun.Com } 11268313SDina.Nimeh@Sun.Com 11278313SDina.Nimeh@Sun.Com /* 11288313SDina.Nimeh@Sun.Com * If only requesting number of entries, or there are no entries, 11298313SDina.Nimeh@Sun.Com * or rv is not CRYPTO_SUCCESS due to buffer too small or some other 11308313SDina.Nimeh@Sun.Com * crypto error, or an error occurred with copyout, stop here 11318313SDina.Nimeh@Sun.Com */ 11328313SDina.Nimeh@Sun.Com if (req_count == 0 || num_mech_infos == 0 || rv != CRYPTO_SUCCESS || 11338313SDina.Nimeh@Sun.Com error != 0) { 11348313SDina.Nimeh@Sun.Com goto out2; 11358313SDina.Nimeh@Sun.Com } 11368313SDina.Nimeh@Sun.Com 11378313SDina.Nimeh@Sun.Com /* copyout mech_infos */ 11388313SDina.Nimeh@Sun.Com offset = (ulong_t)STRUCT_FADDR(get_all_mech, mi_list); 11398313SDina.Nimeh@Sun.Com offset -= (ulong_t)STRUCT_BUF(get_all_mech); 11408313SDina.Nimeh@Sun.Com 11418313SDina.Nimeh@Sun.Com u_minfos = kmem_alloc(num_mech_infos * STRUCT_SIZE(mi), KM_SLEEP); 11428313SDina.Nimeh@Sun.Com error = copyout_mechinfos(mode, arg + offset, num_mech_infos, 11438313SDina.Nimeh@Sun.Com mech_infos, u_minfos); 11448313SDina.Nimeh@Sun.Com kmem_free(u_minfos, num_mech_infos * STRUCT_SIZE(mi)); 11458313SDina.Nimeh@Sun.Com out2: 11468313SDina.Nimeh@Sun.Com if (mech_infos != NULL) 11478313SDina.Nimeh@Sun.Com crypto_free_all_mech_info(mech_infos, num_mech_infos); 11488313SDina.Nimeh@Sun.Com return (error); 11498313SDina.Nimeh@Sun.Com } 11508313SDina.Nimeh@Sun.Com 11518313SDina.Nimeh@Sun.Com /* 11520Sstevel@tonic-gate * Side-effects: 11530Sstevel@tonic-gate * 1. This routine stores provider descriptor pointers in an array 11540Sstevel@tonic-gate * and increments each descriptor's reference count. The array 11550Sstevel@tonic-gate * is stored in per-minor number storage. 11560Sstevel@tonic-gate * 2. Destroys the old array and creates a new one every time 11570Sstevel@tonic-gate * this routine is called. 11580Sstevel@tonic-gate */ 11590Sstevel@tonic-gate int 11600Sstevel@tonic-gate crypto_get_provider_list(crypto_minor_t *cm, uint_t *count, 11610Sstevel@tonic-gate crypto_provider_entry_t **array, boolean_t return_slot_list) 11620Sstevel@tonic-gate { 11630Sstevel@tonic-gate kcf_provider_desc_t **provider_array; 11640Sstevel@tonic-gate crypto_provider_entry_t *p = NULL; 11650Sstevel@tonic-gate uint_t provider_count; 11660Sstevel@tonic-gate int rval; 11670Sstevel@tonic-gate int i; 11680Sstevel@tonic-gate 11690Sstevel@tonic-gate /* 11700Sstevel@tonic-gate * Take snapshot of provider table returning only HW entries 11710Sstevel@tonic-gate * that are in a usable state. Also returns logical provider entries. 11720Sstevel@tonic-gate */ 11730Sstevel@tonic-gate rval = kcf_get_slot_list(&provider_count, &provider_array, B_FALSE); 11740Sstevel@tonic-gate if (rval != CRYPTO_SUCCESS) 11750Sstevel@tonic-gate return (rval); 11760Sstevel@tonic-gate 11770Sstevel@tonic-gate /* allocate memory before taking cm->cm_lock */ 11780Sstevel@tonic-gate if (return_slot_list) { 11790Sstevel@tonic-gate if (provider_count != 0) { 11800Sstevel@tonic-gate p = kmem_alloc(provider_count * 11810Sstevel@tonic-gate sizeof (crypto_provider_entry_t), KM_SLEEP); 11820Sstevel@tonic-gate for (i = 0; i < provider_count; i++) { 11830Sstevel@tonic-gate p[i].pe_provider_id = i; 11840Sstevel@tonic-gate p[i].pe_mechanism_count = 11850Sstevel@tonic-gate provider_array[i]->pd_mech_list_count; 11860Sstevel@tonic-gate } 11870Sstevel@tonic-gate } 11880Sstevel@tonic-gate *array = p; 11890Sstevel@tonic-gate *count = provider_count; 11900Sstevel@tonic-gate } 11910Sstevel@tonic-gate 11920Sstevel@tonic-gate /* 11930Sstevel@tonic-gate * Free existing array of providers and replace with new list. 11940Sstevel@tonic-gate */ 11950Sstevel@tonic-gate mutex_enter(&cm->cm_lock); 11960Sstevel@tonic-gate if (cm->cm_provider_array != NULL) { 11970Sstevel@tonic-gate ASSERT(cm->cm_provider_count > 0); 11980Sstevel@tonic-gate kcf_free_provider_tab(cm->cm_provider_count, 11990Sstevel@tonic-gate cm->cm_provider_array); 12000Sstevel@tonic-gate } 12010Sstevel@tonic-gate 12020Sstevel@tonic-gate cm->cm_provider_array = provider_array; 12030Sstevel@tonic-gate cm->cm_provider_count = provider_count; 12040Sstevel@tonic-gate mutex_exit(&cm->cm_lock); 12050Sstevel@tonic-gate 12060Sstevel@tonic-gate return (CRYPTO_SUCCESS); 12070Sstevel@tonic-gate } 12080Sstevel@tonic-gate 12090Sstevel@tonic-gate /* 12100Sstevel@tonic-gate * This ioctl returns an array of crypto_provider_entry_t entries. 12110Sstevel@tonic-gate * This is how consumers learn which hardware providers are available. 12120Sstevel@tonic-gate */ 12130Sstevel@tonic-gate /* ARGSUSED */ 12140Sstevel@tonic-gate static int 12150Sstevel@tonic-gate get_provider_list(dev_t dev, caddr_t arg, int mode, int *rval) 12160Sstevel@tonic-gate { 12170Sstevel@tonic-gate STRUCT_DECL(crypto_get_provider_list, get_list); 12180Sstevel@tonic-gate crypto_provider_entry_t *entries; 12190Sstevel@tonic-gate crypto_minor_t *cm; 12200Sstevel@tonic-gate size_t copyout_size; 12210Sstevel@tonic-gate uint_t req_count; 12220Sstevel@tonic-gate uint_t count; 12230Sstevel@tonic-gate ulong_t offset; 12240Sstevel@tonic-gate int rv; 12250Sstevel@tonic-gate 12260Sstevel@tonic-gate STRUCT_INIT(get_list, mode); 12270Sstevel@tonic-gate 12280Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 12290Sstevel@tonic-gate cmn_err(CE_WARN, "get_provider_list: failed holding minor"); 12300Sstevel@tonic-gate return (ENXIO); 12310Sstevel@tonic-gate } 12320Sstevel@tonic-gate 12330Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(get_list), STRUCT_SIZE(get_list)) != 0) { 12340Sstevel@tonic-gate crypto_release_minor(cm); 12350Sstevel@tonic-gate return (EFAULT); 12360Sstevel@tonic-gate } 12370Sstevel@tonic-gate 12380Sstevel@tonic-gate rv = crypto_get_provider_list(cm, &count, &entries, RETURN_LIST); 12390Sstevel@tonic-gate if (rv != CRYPTO_SUCCESS) { 12400Sstevel@tonic-gate crypto_release_minor(cm); 12410Sstevel@tonic-gate STRUCT_FSET(get_list, pl_return_value, rv); 12420Sstevel@tonic-gate if (copyout(STRUCT_BUF(get_list), arg, 12430Sstevel@tonic-gate STRUCT_SIZE(get_list)) != 0) { 12440Sstevel@tonic-gate return (EFAULT); 12450Sstevel@tonic-gate } 12460Sstevel@tonic-gate return (0); 12470Sstevel@tonic-gate } 12480Sstevel@tonic-gate crypto_release_minor(cm); 12490Sstevel@tonic-gate 12500Sstevel@tonic-gate /* Number of slots caller thinks we have */ 12510Sstevel@tonic-gate req_count = STRUCT_FGET(get_list, pl_count); 12520Sstevel@tonic-gate 12530Sstevel@tonic-gate /* Check if only requesting number of slots */ 12540Sstevel@tonic-gate if (req_count == 0) { 12550Sstevel@tonic-gate 12560Sstevel@tonic-gate STRUCT_FSET(get_list, pl_count, count); 12570Sstevel@tonic-gate STRUCT_FSET(get_list, pl_return_value, CRYPTO_SUCCESS); 12580Sstevel@tonic-gate 12590Sstevel@tonic-gate crypto_free_provider_list(entries, count); 12600Sstevel@tonic-gate if (copyout(STRUCT_BUF(get_list), arg, 12614632Smcpowers STRUCT_SIZE(get_list)) != 0) { 12620Sstevel@tonic-gate return (EFAULT); 12630Sstevel@tonic-gate } 12640Sstevel@tonic-gate return (0); 12650Sstevel@tonic-gate } 12660Sstevel@tonic-gate 12670Sstevel@tonic-gate /* check if buffer is too small */ 12680Sstevel@tonic-gate req_count = STRUCT_FGET(get_list, pl_count); 12690Sstevel@tonic-gate if (count > req_count) { 12700Sstevel@tonic-gate STRUCT_FSET(get_list, pl_count, count); 12710Sstevel@tonic-gate STRUCT_FSET(get_list, pl_return_value, CRYPTO_BUFFER_TOO_SMALL); 12720Sstevel@tonic-gate crypto_free_provider_list(entries, count); 12730Sstevel@tonic-gate if (copyout(STRUCT_BUF(get_list), arg, 12740Sstevel@tonic-gate STRUCT_SIZE(get_list)) != 0) { 12750Sstevel@tonic-gate return (EFAULT); 12760Sstevel@tonic-gate } 12770Sstevel@tonic-gate return (0); 12780Sstevel@tonic-gate } 12790Sstevel@tonic-gate 12800Sstevel@tonic-gate STRUCT_FSET(get_list, pl_count, count); 12810Sstevel@tonic-gate STRUCT_FSET(get_list, pl_return_value, CRYPTO_SUCCESS); 12820Sstevel@tonic-gate 12830Sstevel@tonic-gate copyout_size = count * sizeof (crypto_provider_entry_t); 12840Sstevel@tonic-gate 12850Sstevel@tonic-gate /* copyout the first stuff */ 12860Sstevel@tonic-gate if (copyout(STRUCT_BUF(get_list), arg, STRUCT_SIZE(get_list)) != 0) { 12870Sstevel@tonic-gate crypto_free_provider_list(entries, count); 12880Sstevel@tonic-gate return (EFAULT); 12890Sstevel@tonic-gate } 12900Sstevel@tonic-gate 12910Sstevel@tonic-gate if (count == 0) { 12920Sstevel@tonic-gate crypto_free_provider_list(entries, count); 12930Sstevel@tonic-gate return (0); 12940Sstevel@tonic-gate } 12950Sstevel@tonic-gate 12960Sstevel@tonic-gate /* copyout entries */ 12970Sstevel@tonic-gate offset = (ulong_t)STRUCT_FADDR(get_list, pl_list); 12980Sstevel@tonic-gate offset -= (ulong_t)STRUCT_BUF(get_list); 12990Sstevel@tonic-gate if (copyout(entries, arg + offset, copyout_size) != 0) { 13000Sstevel@tonic-gate crypto_free_provider_list(entries, count); 13010Sstevel@tonic-gate return (EFAULT); 13020Sstevel@tonic-gate } 13030Sstevel@tonic-gate 13040Sstevel@tonic-gate crypto_free_provider_list(entries, count); 13050Sstevel@tonic-gate return (0); 13060Sstevel@tonic-gate } 13070Sstevel@tonic-gate 13080Sstevel@tonic-gate static void 13090Sstevel@tonic-gate ext_to_provider_data(int mode, kcf_provider_desc_t *provider, 13100Sstevel@tonic-gate crypto_provider_ext_info_t *ei, void *out) 13110Sstevel@tonic-gate { 13120Sstevel@tonic-gate STRUCT_DECL(crypto_provider_data, pd); 13130Sstevel@tonic-gate STRUCT_DECL(crypto_version, version); 13140Sstevel@tonic-gate 13150Sstevel@tonic-gate STRUCT_INIT(pd, mode); 13160Sstevel@tonic-gate STRUCT_INIT(version, mode); 13170Sstevel@tonic-gate 13180Sstevel@tonic-gate bcopy(provider->pd_description, STRUCT_FGET(pd, pd_prov_desc), 13190Sstevel@tonic-gate CRYPTO_PROVIDER_DESCR_MAX_LEN); 13200Sstevel@tonic-gate 13210Sstevel@tonic-gate bcopy(ei->ei_label, STRUCT_FGET(pd, pd_label), CRYPTO_EXT_SIZE_LABEL); 13220Sstevel@tonic-gate bcopy(ei->ei_manufacturerID, STRUCT_FGET(pd, pd_manufacturerID), 13230Sstevel@tonic-gate CRYPTO_EXT_SIZE_MANUF); 13240Sstevel@tonic-gate bcopy(ei->ei_model, STRUCT_FGET(pd, pd_model), CRYPTO_EXT_SIZE_MODEL); 13250Sstevel@tonic-gate bcopy(ei->ei_serial_number, STRUCT_FGET(pd, pd_serial_number), 13260Sstevel@tonic-gate CRYPTO_EXT_SIZE_SERIAL); 13270Sstevel@tonic-gate /* 13280Sstevel@tonic-gate * We do not support ioctls for dual-function crypto operations yet. 13290Sstevel@tonic-gate * So, we clear this flag as it might have been set by a provider. 13300Sstevel@tonic-gate */ 13310Sstevel@tonic-gate ei->ei_flags &= ~CRYPTO_EXTF_DUAL_CRYPTO_OPERATIONS; 13320Sstevel@tonic-gate 13330Sstevel@tonic-gate STRUCT_FSET(pd, pd_flags, ei->ei_flags); 13340Sstevel@tonic-gate STRUCT_FSET(pd, pd_max_session_count, ei->ei_max_session_count); 13350Sstevel@tonic-gate STRUCT_FSET(pd, pd_session_count, (int)CRYPTO_UNAVAILABLE_INFO); 13360Sstevel@tonic-gate STRUCT_FSET(pd, pd_max_rw_session_count, ei->ei_max_session_count); 13370Sstevel@tonic-gate STRUCT_FSET(pd, pd_rw_session_count, (int)CRYPTO_UNAVAILABLE_INFO); 13380Sstevel@tonic-gate STRUCT_FSET(pd, pd_max_pin_len, ei->ei_max_pin_len); 13390Sstevel@tonic-gate STRUCT_FSET(pd, pd_min_pin_len, ei->ei_min_pin_len); 13400Sstevel@tonic-gate STRUCT_FSET(pd, pd_total_public_memory, ei->ei_total_public_memory); 13410Sstevel@tonic-gate STRUCT_FSET(pd, pd_free_public_memory, ei->ei_free_public_memory); 13420Sstevel@tonic-gate STRUCT_FSET(pd, pd_total_private_memory, ei->ei_total_private_memory); 13430Sstevel@tonic-gate STRUCT_FSET(pd, pd_free_private_memory, ei->ei_free_private_memory); 13440Sstevel@tonic-gate STRUCT_FSET(version, cv_major, ei->ei_hardware_version.cv_major); 13450Sstevel@tonic-gate STRUCT_FSET(version, cv_minor, ei->ei_hardware_version.cv_minor); 13460Sstevel@tonic-gate bcopy(STRUCT_BUF(version), STRUCT_FADDR(pd, pd_hardware_version), 13470Sstevel@tonic-gate STRUCT_SIZE(version)); 134810028Swyllys.ingersoll@sun.com STRUCT_FSET(version, cv_major, ei->ei_firmware_version.cv_major); 134910028Swyllys.ingersoll@sun.com STRUCT_FSET(version, cv_minor, ei->ei_firmware_version.cv_minor); 13500Sstevel@tonic-gate bcopy(STRUCT_BUF(version), STRUCT_FADDR(pd, pd_firmware_version), 13510Sstevel@tonic-gate STRUCT_SIZE(version)); 13520Sstevel@tonic-gate bcopy(ei->ei_time, STRUCT_FGET(pd, pd_time), CRYPTO_EXT_SIZE_TIME); 13530Sstevel@tonic-gate bcopy(STRUCT_BUF(pd), out, STRUCT_SIZE(pd)); 13540Sstevel@tonic-gate } 13550Sstevel@tonic-gate 13560Sstevel@tonic-gate /* 13570Sstevel@tonic-gate * Utility routine to construct a crypto_provider_ext_info structure. Some 13580Sstevel@tonic-gate * of the fields are constructed from information in the provider structure. 13590Sstevel@tonic-gate * The rest of the fields have default values. We need to do this for 13600Sstevel@tonic-gate * providers which do not support crypto_provider_management_ops routines. 13610Sstevel@tonic-gate */ 13620Sstevel@tonic-gate static void 13630Sstevel@tonic-gate fabricate_ext_info(kcf_provider_desc_t *provider, 13640Sstevel@tonic-gate crypto_provider_ext_info_t *ei) 13650Sstevel@tonic-gate { 13660Sstevel@tonic-gate /* empty label */ 13670Sstevel@tonic-gate (void) memset(ei->ei_label, ' ', CRYPTO_EXT_SIZE_LABEL); 13680Sstevel@tonic-gate 13690Sstevel@tonic-gate (void) memset(ei->ei_manufacturerID, ' ', CRYPTO_EXT_SIZE_MANUF); 13700Sstevel@tonic-gate (void) strncpy((char *)ei->ei_manufacturerID, "Unknown", 7); 13710Sstevel@tonic-gate 13720Sstevel@tonic-gate (void) memset(ei->ei_model, ' ', CRYPTO_EXT_SIZE_MODEL); 13730Sstevel@tonic-gate (void) strncpy((char *)ei->ei_model, "Unknown", 7); 13740Sstevel@tonic-gate 13750Sstevel@tonic-gate (void) memset(ei->ei_serial_number, ' ', CRYPTO_EXT_SIZE_SERIAL); 13760Sstevel@tonic-gate (void) strncpy((char *)ei->ei_serial_number, "Unknown", 7); 13770Sstevel@tonic-gate 13780Sstevel@tonic-gate if (KCF_PROV_RANDOM_OPS(provider) != NULL) 13790Sstevel@tonic-gate ei->ei_flags |= CRYPTO_EXTF_RNG; 13800Sstevel@tonic-gate if (KCF_PROV_DUAL_OPS(provider) != NULL) 13810Sstevel@tonic-gate ei->ei_flags |= CRYPTO_EXTF_DUAL_CRYPTO_OPERATIONS; 13820Sstevel@tonic-gate 13830Sstevel@tonic-gate ei->ei_max_session_count = CRYPTO_UNAVAILABLE_INFO; 13840Sstevel@tonic-gate ei->ei_max_pin_len = 0; 13850Sstevel@tonic-gate ei->ei_min_pin_len = 0; 13860Sstevel@tonic-gate ei->ei_total_public_memory = CRYPTO_UNAVAILABLE_INFO; 13870Sstevel@tonic-gate ei->ei_free_public_memory = CRYPTO_UNAVAILABLE_INFO; 13880Sstevel@tonic-gate ei->ei_total_private_memory = CRYPTO_UNAVAILABLE_INFO; 13890Sstevel@tonic-gate ei->ei_free_private_memory = CRYPTO_UNAVAILABLE_INFO; 13900Sstevel@tonic-gate ei->ei_hardware_version.cv_major = 1; 13910Sstevel@tonic-gate ei->ei_hardware_version.cv_minor = 0; 13920Sstevel@tonic-gate ei->ei_firmware_version.cv_major = 1; 13930Sstevel@tonic-gate ei->ei_firmware_version.cv_minor = 0; 13940Sstevel@tonic-gate } 13950Sstevel@tonic-gate 13960Sstevel@tonic-gate /* ARGSUSED */ 13970Sstevel@tonic-gate static int 13980Sstevel@tonic-gate get_provider_info(dev_t dev, caddr_t arg, int mode, int *rval) 13990Sstevel@tonic-gate { 14000Sstevel@tonic-gate STRUCT_DECL(crypto_get_provider_info, get_info); 14010Sstevel@tonic-gate crypto_minor_t *cm; 14020Sstevel@tonic-gate crypto_provider_id_t provider_id; 14030Sstevel@tonic-gate kcf_provider_desc_t *provider, *real_provider; 14040Sstevel@tonic-gate crypto_provider_ext_info_t *ext_info = NULL; 14050Sstevel@tonic-gate size_t need; 14060Sstevel@tonic-gate int error = 0; 14070Sstevel@tonic-gate int rv; 14080Sstevel@tonic-gate kcf_req_params_t params; 14090Sstevel@tonic-gate 14100Sstevel@tonic-gate STRUCT_INIT(get_info, mode); 14110Sstevel@tonic-gate 14120Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 14130Sstevel@tonic-gate cmn_err(CE_WARN, "get_provider_info: failed holding minor"); 14140Sstevel@tonic-gate return (ENXIO); 14150Sstevel@tonic-gate } 14160Sstevel@tonic-gate 14170Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(get_info), STRUCT_SIZE(get_info)) != 0) { 14180Sstevel@tonic-gate crypto_release_minor(cm); 14190Sstevel@tonic-gate return (EFAULT); 14200Sstevel@tonic-gate } 14210Sstevel@tonic-gate 14220Sstevel@tonic-gate need = sizeof (crypto_provider_ext_info_t); 14233916Skrishna if ((rv = crypto_buffer_check(need)) != CRYPTO_SUCCESS) { 14240Sstevel@tonic-gate need = 0; 14250Sstevel@tonic-gate goto release_minor; 14260Sstevel@tonic-gate } 14270Sstevel@tonic-gate 14280Sstevel@tonic-gate /* initialize provider_array */ 14290Sstevel@tonic-gate if (cm->cm_provider_array == NULL) { 14300Sstevel@tonic-gate rv = crypto_get_provider_list(cm, NULL, NULL, DONT_RETURN_LIST); 14310Sstevel@tonic-gate if (rv != CRYPTO_SUCCESS) { 14320Sstevel@tonic-gate goto release_minor; 14330Sstevel@tonic-gate } 14340Sstevel@tonic-gate } 14350Sstevel@tonic-gate 14360Sstevel@tonic-gate ext_info = kmem_zalloc(need, KM_SLEEP); 14370Sstevel@tonic-gate 14380Sstevel@tonic-gate provider_id = STRUCT_FGET(get_info, gi_provider_id); 14390Sstevel@tonic-gate mutex_enter(&cm->cm_lock); 14400Sstevel@tonic-gate /* index must be less than count of providers */ 14410Sstevel@tonic-gate if (provider_id >= cm->cm_provider_count) { 14420Sstevel@tonic-gate mutex_exit(&cm->cm_lock); 14430Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD; 14440Sstevel@tonic-gate goto release_minor; 14450Sstevel@tonic-gate } 14460Sstevel@tonic-gate 14470Sstevel@tonic-gate ASSERT(cm->cm_provider_array != NULL); 14480Sstevel@tonic-gate provider = cm->cm_provider_array[provider_id]; 14490Sstevel@tonic-gate KCF_PROV_REFHOLD(provider); 14500Sstevel@tonic-gate mutex_exit(&cm->cm_lock); 14510Sstevel@tonic-gate 14520Sstevel@tonic-gate (void) kcf_get_hardware_provider_nomech( 14530Sstevel@tonic-gate CRYPTO_OPS_OFFSET(provider_ops), CRYPTO_PROVIDER_OFFSET(ext_info), 1454904Smcpowers CHECK_RESTRICT_FALSE, provider, &real_provider); 14550Sstevel@tonic-gate 14560Sstevel@tonic-gate if (real_provider != NULL) { 14570Sstevel@tonic-gate ASSERT(real_provider == provider || 14580Sstevel@tonic-gate provider->pd_prov_type == CRYPTO_LOGICAL_PROVIDER); 14590Sstevel@tonic-gate KCF_WRAP_PROVMGMT_OPS_PARAMS(¶ms, KCF_OP_MGMT_EXTINFO, 14600Sstevel@tonic-gate 0, NULL, 0, NULL, 0, NULL, ext_info, provider); 14610Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, 14620Sstevel@tonic-gate B_FALSE); 14630Sstevel@tonic-gate ASSERT(rv != CRYPTO_NOT_SUPPORTED); 1464904Smcpowers KCF_PROV_REFRELE(real_provider); 14650Sstevel@tonic-gate } else { 14660Sstevel@tonic-gate /* do the best we can */ 14670Sstevel@tonic-gate fabricate_ext_info(provider, ext_info); 14680Sstevel@tonic-gate rv = CRYPTO_SUCCESS; 14690Sstevel@tonic-gate } 14700Sstevel@tonic-gate KCF_PROV_REFRELE(provider); 14710Sstevel@tonic-gate 14720Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) { 14730Sstevel@tonic-gate ext_to_provider_data(mode, provider, ext_info, 14740Sstevel@tonic-gate STRUCT_FADDR(get_info, gi_provider_data)); 14750Sstevel@tonic-gate } 14760Sstevel@tonic-gate 14770Sstevel@tonic-gate release_minor: 14786424Skrishna CRYPTO_DECREMENT_RCTL(need); 14790Sstevel@tonic-gate crypto_release_minor(cm); 14800Sstevel@tonic-gate 14810Sstevel@tonic-gate if (ext_info != NULL) 14820Sstevel@tonic-gate kmem_free(ext_info, sizeof (crypto_provider_ext_info_t)); 14830Sstevel@tonic-gate 14840Sstevel@tonic-gate if (error != 0) 14850Sstevel@tonic-gate return (error); 14860Sstevel@tonic-gate 14870Sstevel@tonic-gate STRUCT_FSET(get_info, gi_return_value, rv); 14880Sstevel@tonic-gate if (copyout(STRUCT_BUF(get_info), arg, STRUCT_SIZE(get_info)) != 0) { 14890Sstevel@tonic-gate return (EFAULT); 14900Sstevel@tonic-gate } 14910Sstevel@tonic-gate return (0); 14920Sstevel@tonic-gate } 14930Sstevel@tonic-gate 14940Sstevel@tonic-gate /* 14950Sstevel@tonic-gate * This ioctl returns an array of crypto_mech_name_t entries. 14960Sstevel@tonic-gate * This is how consumers learn which mechanisms are permitted 14970Sstevel@tonic-gate * by a provider. 14980Sstevel@tonic-gate */ 14990Sstevel@tonic-gate /* ARGSUSED */ 15000Sstevel@tonic-gate static int 15010Sstevel@tonic-gate get_provider_mechanisms(dev_t dev, caddr_t arg, int mode, int *rval) 15020Sstevel@tonic-gate { 15030Sstevel@tonic-gate STRUCT_DECL(crypto_get_provider_mechanisms, get_mechanisms); 15040Sstevel@tonic-gate crypto_mech_name_t *entries; 15050Sstevel@tonic-gate crypto_minor_t *cm; 15060Sstevel@tonic-gate size_t copyout_size; 15070Sstevel@tonic-gate uint_t req_count; 15080Sstevel@tonic-gate uint_t count; 15090Sstevel@tonic-gate ulong_t offset; 15100Sstevel@tonic-gate int err; 15110Sstevel@tonic-gate 15120Sstevel@tonic-gate STRUCT_INIT(get_mechanisms, mode); 15130Sstevel@tonic-gate 15140Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 15150Sstevel@tonic-gate cmn_err(CE_WARN, 15160Sstevel@tonic-gate "get_provider_mechanisms: failed holding minor"); 15170Sstevel@tonic-gate return (ENXIO); 15180Sstevel@tonic-gate } 15190Sstevel@tonic-gate 15200Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(get_mechanisms), 15210Sstevel@tonic-gate STRUCT_SIZE(get_mechanisms)) != 0) { 15220Sstevel@tonic-gate crypto_release_minor(cm); 15230Sstevel@tonic-gate return (EFAULT); 15240Sstevel@tonic-gate } 15250Sstevel@tonic-gate 15260Sstevel@tonic-gate /* get array of mechanisms from the core module */ 15270Sstevel@tonic-gate if ((err = crypto_get_provider_mechanisms(cm, 15280Sstevel@tonic-gate STRUCT_FGET(get_mechanisms, pm_provider_id), 15290Sstevel@tonic-gate &count, &entries)) != 0) { 15300Sstevel@tonic-gate crypto_release_minor(cm); 15310Sstevel@tonic-gate STRUCT_FSET(get_mechanisms, pm_return_value, err); 15320Sstevel@tonic-gate if (copyout(STRUCT_BUF(get_mechanisms), arg, 15330Sstevel@tonic-gate STRUCT_SIZE(get_mechanisms)) != 0) { 15340Sstevel@tonic-gate return (EFAULT); 15350Sstevel@tonic-gate } 15360Sstevel@tonic-gate return (0); 15370Sstevel@tonic-gate } 15380Sstevel@tonic-gate crypto_release_minor(cm); 15390Sstevel@tonic-gate /* Number of mechs caller thinks we have */ 15400Sstevel@tonic-gate req_count = STRUCT_FGET(get_mechanisms, pm_count); 15410Sstevel@tonic-gate 15420Sstevel@tonic-gate /* Check if caller is just requesting a count of mechanisms */ 15430Sstevel@tonic-gate if (req_count == 0) { 15440Sstevel@tonic-gate STRUCT_FSET(get_mechanisms, pm_count, count); 15450Sstevel@tonic-gate STRUCT_FSET(get_mechanisms, pm_return_value, CRYPTO_SUCCESS); 15460Sstevel@tonic-gate 15470Sstevel@tonic-gate crypto_free_mech_list(entries, count); 15480Sstevel@tonic-gate if (copyout(STRUCT_BUF(get_mechanisms), arg, 15490Sstevel@tonic-gate STRUCT_SIZE(get_mechanisms)) != 0) { 15500Sstevel@tonic-gate return (EFAULT); 15510Sstevel@tonic-gate } 15520Sstevel@tonic-gate return (0); 15530Sstevel@tonic-gate } 15540Sstevel@tonic-gate 15550Sstevel@tonic-gate /* check if buffer is too small */ 15560Sstevel@tonic-gate if (count > req_count) { 15570Sstevel@tonic-gate STRUCT_FSET(get_mechanisms, pm_count, count); 15580Sstevel@tonic-gate STRUCT_FSET(get_mechanisms, pm_return_value, 15590Sstevel@tonic-gate CRYPTO_BUFFER_TOO_SMALL); 15600Sstevel@tonic-gate crypto_free_mech_list(entries, count); 15610Sstevel@tonic-gate if (copyout(STRUCT_BUF(get_mechanisms), arg, 15620Sstevel@tonic-gate STRUCT_SIZE(get_mechanisms)) != 0) { 15630Sstevel@tonic-gate return (EFAULT); 15640Sstevel@tonic-gate } 15650Sstevel@tonic-gate return (0); 15660Sstevel@tonic-gate } 15670Sstevel@tonic-gate 15680Sstevel@tonic-gate STRUCT_FSET(get_mechanisms, pm_count, count); 15690Sstevel@tonic-gate STRUCT_FSET(get_mechanisms, pm_return_value, CRYPTO_SUCCESS); 15700Sstevel@tonic-gate 15710Sstevel@tonic-gate copyout_size = count * sizeof (crypto_mech_name_t); 15720Sstevel@tonic-gate 15730Sstevel@tonic-gate /* copyout the first stuff */ 15740Sstevel@tonic-gate if (copyout(STRUCT_BUF(get_mechanisms), arg, 15750Sstevel@tonic-gate STRUCT_SIZE(get_mechanisms)) != 0) { 15760Sstevel@tonic-gate crypto_free_mech_list(entries, count); 15770Sstevel@tonic-gate return (EFAULT); 15780Sstevel@tonic-gate } 15790Sstevel@tonic-gate 15800Sstevel@tonic-gate if (count == 0) { 15810Sstevel@tonic-gate return (0); 15820Sstevel@tonic-gate } 15830Sstevel@tonic-gate 15840Sstevel@tonic-gate /* copyout entries */ 15850Sstevel@tonic-gate offset = (ulong_t)STRUCT_FADDR(get_mechanisms, pm_list); 15860Sstevel@tonic-gate offset -= (ulong_t)STRUCT_BUF(get_mechanisms); 15870Sstevel@tonic-gate if (copyout(entries, arg + offset, copyout_size) != 0) { 15880Sstevel@tonic-gate crypto_free_mech_list(entries, count); 15890Sstevel@tonic-gate return (EFAULT); 15900Sstevel@tonic-gate } 15910Sstevel@tonic-gate 15920Sstevel@tonic-gate crypto_free_mech_list(entries, count); 15930Sstevel@tonic-gate return (0); 15940Sstevel@tonic-gate } 15950Sstevel@tonic-gate 15960Sstevel@tonic-gate /* 15970Sstevel@tonic-gate * This ioctl returns information about a provider's mechanism. 15980Sstevel@tonic-gate */ 15990Sstevel@tonic-gate /* ARGSUSED */ 16000Sstevel@tonic-gate static int 16010Sstevel@tonic-gate get_provider_mechanism_info(dev_t dev, caddr_t arg, int mode, int *rval) 16020Sstevel@tonic-gate { 16030Sstevel@tonic-gate crypto_get_provider_mechanism_info_t mechanism_info; 16040Sstevel@tonic-gate crypto_minor_t *cm; 16050Sstevel@tonic-gate kcf_provider_desc_t *pd; 16060Sstevel@tonic-gate crypto_mech_info_t *mi = NULL; 16070Sstevel@tonic-gate int rv = CRYPTO_SUCCESS; 16080Sstevel@tonic-gate int i; 16090Sstevel@tonic-gate 16100Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 16110Sstevel@tonic-gate cmn_err(CE_WARN, 16120Sstevel@tonic-gate "get_provider_mechanism_info: failed holding minor"); 16130Sstevel@tonic-gate return (ENXIO); 16140Sstevel@tonic-gate } 16150Sstevel@tonic-gate 16160Sstevel@tonic-gate if (copyin(arg, &mechanism_info, sizeof (mechanism_info)) != 0) { 16170Sstevel@tonic-gate crypto_release_minor(cm); 16180Sstevel@tonic-gate return (EFAULT); 16190Sstevel@tonic-gate } 16200Sstevel@tonic-gate 16210Sstevel@tonic-gate /* initialize provider table */ 16220Sstevel@tonic-gate if (cm->cm_provider_array == NULL) { 16230Sstevel@tonic-gate rv = crypto_get_provider_list(cm, NULL, NULL, DONT_RETURN_LIST); 16240Sstevel@tonic-gate if (rv != CRYPTO_SUCCESS) { 16250Sstevel@tonic-gate mutex_enter(&cm->cm_lock); 16260Sstevel@tonic-gate goto fail; 16270Sstevel@tonic-gate } 16280Sstevel@tonic-gate } 16290Sstevel@tonic-gate 16300Sstevel@tonic-gate /* 16310Sstevel@tonic-gate * Provider ID must be less than the count of providers 16320Sstevel@tonic-gate * obtained by calling get_provider_list(). 16330Sstevel@tonic-gate */ 16340Sstevel@tonic-gate mutex_enter(&cm->cm_lock); 16350Sstevel@tonic-gate if (mechanism_info.mi_provider_id >= cm->cm_provider_count) { 16360Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD; 16370Sstevel@tonic-gate goto fail; 16380Sstevel@tonic-gate } 16390Sstevel@tonic-gate 16400Sstevel@tonic-gate pd = cm->cm_provider_array[mechanism_info.mi_provider_id]; 16410Sstevel@tonic-gate 16420Sstevel@tonic-gate for (i = 0; i < pd->pd_mech_list_count; i++) { 16430Sstevel@tonic-gate if (strncmp(pd->pd_mechanisms[i].cm_mech_name, 16440Sstevel@tonic-gate mechanism_info.mi_mechanism_name, 16450Sstevel@tonic-gate CRYPTO_MAX_MECH_NAME) == 0) { 16460Sstevel@tonic-gate mi = &pd->pd_mechanisms[i]; 16470Sstevel@tonic-gate } 16480Sstevel@tonic-gate } 16490Sstevel@tonic-gate 16500Sstevel@tonic-gate if (mi == NULL) { 16510Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD; 16520Sstevel@tonic-gate goto fail; 16530Sstevel@tonic-gate } 16540Sstevel@tonic-gate 16550Sstevel@tonic-gate mechanism_info.mi_min_key_size = mi->cm_min_key_length; 16560Sstevel@tonic-gate mechanism_info.mi_max_key_size = mi->cm_max_key_length; 16570Sstevel@tonic-gate mechanism_info.mi_flags = mi->cm_func_group_mask; 16580Sstevel@tonic-gate 16590Sstevel@tonic-gate fail: 16600Sstevel@tonic-gate mutex_exit(&cm->cm_lock); 16610Sstevel@tonic-gate crypto_release_minor(cm); 16620Sstevel@tonic-gate mechanism_info.mi_return_value = rv; 16630Sstevel@tonic-gate if (copyout(&mechanism_info, arg, sizeof (mechanism_info)) != 0) { 16640Sstevel@tonic-gate return (EFAULT); 16650Sstevel@tonic-gate } 16660Sstevel@tonic-gate 16670Sstevel@tonic-gate return (0); 16680Sstevel@tonic-gate } 16690Sstevel@tonic-gate 16700Sstevel@tonic-gate /* 16710Sstevel@tonic-gate * Every open of /dev/crypto multiplexes all PKCS#11 sessions across 16720Sstevel@tonic-gate * a single session to each provider. Calls to open and close session 16730Sstevel@tonic-gate * are not made to providers that do not support sessions. For these 16740Sstevel@tonic-gate * providers, a session number of 0 is passed during subsequent operations, 16750Sstevel@tonic-gate * and it is ignored by the provider. 16760Sstevel@tonic-gate */ 16770Sstevel@tonic-gate static int 16780Sstevel@tonic-gate crypto_get_provider_session(crypto_minor_t *cm, 16790Sstevel@tonic-gate crypto_provider_id_t provider_index, crypto_provider_session_t **output_ps) 16800Sstevel@tonic-gate { 16810Sstevel@tonic-gate kcf_provider_desc_t *pd, *real_provider; 16820Sstevel@tonic-gate kcf_req_params_t params; 16830Sstevel@tonic-gate crypto_provider_session_t *ps, *new_ps; 16840Sstevel@tonic-gate crypto_session_id_t provider_session_id = 0; 16850Sstevel@tonic-gate int rv; 16860Sstevel@tonic-gate 16870Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cm->cm_lock)); 16880Sstevel@tonic-gate 16890Sstevel@tonic-gate /* pd may be a logical provider */ 16900Sstevel@tonic-gate pd = cm->cm_provider_array[provider_index]; 16910Sstevel@tonic-gate 16920Sstevel@tonic-gate again: 16930Sstevel@tonic-gate /* 16940Sstevel@tonic-gate * Check if there is already a session to the provider. 16950Sstevel@tonic-gate * Sessions may be to a logical provider or a real provider. 16960Sstevel@tonic-gate */ 16970Sstevel@tonic-gate for (ps = cm->cm_provider_session; ps != NULL; ps = ps->ps_next) { 16980Sstevel@tonic-gate if (ps->ps_provider == pd) 16990Sstevel@tonic-gate break; 17000Sstevel@tonic-gate } 17010Sstevel@tonic-gate 17020Sstevel@tonic-gate /* found existing session */ 17030Sstevel@tonic-gate if (ps != NULL) { 17040Sstevel@tonic-gate ps->ps_refcnt++; 17050Sstevel@tonic-gate *output_ps = ps; 17060Sstevel@tonic-gate return (CRYPTO_SUCCESS); 17070Sstevel@tonic-gate } 17080Sstevel@tonic-gate mutex_exit(&cm->cm_lock); 17090Sstevel@tonic-gate 17100Sstevel@tonic-gate /* find a hardware provider that supports session ops */ 17110Sstevel@tonic-gate (void) kcf_get_hardware_provider_nomech(CRYPTO_OPS_OFFSET(session_ops), 1712904Smcpowers CRYPTO_SESSION_OFFSET(session_open), CHECK_RESTRICT_FALSE, 1713904Smcpowers pd, &real_provider); 17140Sstevel@tonic-gate 17150Sstevel@tonic-gate if (real_provider != NULL) { 17160Sstevel@tonic-gate ASSERT(real_provider == pd || 17170Sstevel@tonic-gate pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER); 17180Sstevel@tonic-gate /* open session to provider */ 17190Sstevel@tonic-gate KCF_WRAP_SESSION_OPS_PARAMS(¶ms, KCF_OP_SESSION_OPEN, 17200Sstevel@tonic-gate &provider_session_id, 0, CRYPTO_USER, NULL, 0, pd); 17210Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, 17220Sstevel@tonic-gate B_FALSE); 17230Sstevel@tonic-gate if (rv != CRYPTO_SUCCESS) { 17240Sstevel@tonic-gate mutex_enter(&cm->cm_lock); 1725904Smcpowers KCF_PROV_REFRELE(real_provider); 17260Sstevel@tonic-gate return (rv); 17270Sstevel@tonic-gate } 17280Sstevel@tonic-gate } 17290Sstevel@tonic-gate 17300Sstevel@tonic-gate /* allocate crypto_provider_session structure */ 17310Sstevel@tonic-gate new_ps = kmem_zalloc(sizeof (crypto_provider_session_t), KM_SLEEP); 17320Sstevel@tonic-gate 17330Sstevel@tonic-gate /* 17340Sstevel@tonic-gate * Check if someone opened a session to the provider 17350Sstevel@tonic-gate * while we dropped the lock. 17360Sstevel@tonic-gate */ 17370Sstevel@tonic-gate mutex_enter(&cm->cm_lock); 17380Sstevel@tonic-gate for (ps = cm->cm_provider_session; ps != NULL; ps = ps->ps_next) { 17390Sstevel@tonic-gate if (ps->ps_provider == pd) { 17400Sstevel@tonic-gate mutex_exit(&cm->cm_lock); 17410Sstevel@tonic-gate kmem_free(new_ps, sizeof (crypto_provider_session_t)); 17420Sstevel@tonic-gate if (real_provider != NULL) { 17430Sstevel@tonic-gate KCF_WRAP_SESSION_OPS_PARAMS(¶ms, 17440Sstevel@tonic-gate KCF_OP_SESSION_CLOSE, NULL, 17450Sstevel@tonic-gate provider_session_id, CRYPTO_USER, NULL, 0, 17460Sstevel@tonic-gate pd); 17470Sstevel@tonic-gate (void) kcf_submit_request(real_provider, NULL, 17480Sstevel@tonic-gate NULL, ¶ms, B_FALSE); 1749904Smcpowers KCF_PROV_REFRELE(real_provider); 17500Sstevel@tonic-gate } 17510Sstevel@tonic-gate mutex_enter(&cm->cm_lock); 17520Sstevel@tonic-gate goto again; 17530Sstevel@tonic-gate 17540Sstevel@tonic-gate } 17550Sstevel@tonic-gate } 17560Sstevel@tonic-gate 17570Sstevel@tonic-gate /* increment refcnt and attach to crypto_minor structure */ 17580Sstevel@tonic-gate new_ps->ps_session = provider_session_id; 17590Sstevel@tonic-gate new_ps->ps_refcnt = 1; 17600Sstevel@tonic-gate KCF_PROV_REFHOLD(pd); 17610Sstevel@tonic-gate new_ps->ps_provider = pd; 17620Sstevel@tonic-gate if (real_provider != NULL) { 17630Sstevel@tonic-gate new_ps->ps_real_provider = real_provider; 17640Sstevel@tonic-gate } 17650Sstevel@tonic-gate new_ps->ps_next = cm->cm_provider_session; 17660Sstevel@tonic-gate cm->cm_provider_session = new_ps; 17670Sstevel@tonic-gate 17680Sstevel@tonic-gate *output_ps = new_ps; 17690Sstevel@tonic-gate return (CRYPTO_SUCCESS); 17700Sstevel@tonic-gate } 17710Sstevel@tonic-gate 17720Sstevel@tonic-gate /* 17730Sstevel@tonic-gate * Release a provider session. 17740Sstevel@tonic-gate * If the reference count goes to zero, then close the session 17750Sstevel@tonic-gate * to the provider. 17760Sstevel@tonic-gate */ 17770Sstevel@tonic-gate static void 17780Sstevel@tonic-gate crypto_release_provider_session(crypto_minor_t *cm, 17790Sstevel@tonic-gate crypto_provider_session_t *provider_session) 17800Sstevel@tonic-gate { 17810Sstevel@tonic-gate kcf_req_params_t params; 17820Sstevel@tonic-gate crypto_provider_session_t *ps = NULL, **prev; 17830Sstevel@tonic-gate 17840Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cm->cm_lock)); 17850Sstevel@tonic-gate 17860Sstevel@tonic-gate /* verify that provider_session is valid */ 17870Sstevel@tonic-gate for (ps = cm->cm_provider_session, prev = &cm->cm_provider_session; 17880Sstevel@tonic-gate ps != NULL; prev = &ps->ps_next, ps = ps->ps_next) { 17890Sstevel@tonic-gate if (ps == provider_session) { 17900Sstevel@tonic-gate break; 17910Sstevel@tonic-gate } 17920Sstevel@tonic-gate } 17930Sstevel@tonic-gate 17940Sstevel@tonic-gate if (ps == NULL) 17950Sstevel@tonic-gate return; 17960Sstevel@tonic-gate 17970Sstevel@tonic-gate ps->ps_refcnt--; 17980Sstevel@tonic-gate 17990Sstevel@tonic-gate if (ps->ps_refcnt > 0) 18000Sstevel@tonic-gate return; 18010Sstevel@tonic-gate 18020Sstevel@tonic-gate if (ps->ps_real_provider != NULL) { 18030Sstevel@tonic-gate /* close session with provider */ 18040Sstevel@tonic-gate KCF_WRAP_SESSION_OPS_PARAMS(¶ms, KCF_OP_SESSION_CLOSE, NULL, 18050Sstevel@tonic-gate ps->ps_session, CRYPTO_USER, NULL, 0, ps->ps_provider); 18060Sstevel@tonic-gate (void) kcf_submit_request(ps->ps_real_provider, 18070Sstevel@tonic-gate NULL, NULL, ¶ms, B_FALSE); 18080Sstevel@tonic-gate KCF_PROV_REFRELE(ps->ps_real_provider); 18090Sstevel@tonic-gate } 18100Sstevel@tonic-gate KCF_PROV_REFRELE(ps->ps_provider); 18110Sstevel@tonic-gate *prev = ps->ps_next; 18120Sstevel@tonic-gate kmem_free(ps, sizeof (*ps)); 18130Sstevel@tonic-gate } 18140Sstevel@tonic-gate 18150Sstevel@tonic-gate static int 18160Sstevel@tonic-gate grow_session_table(crypto_minor_t *cm) 18170Sstevel@tonic-gate { 18180Sstevel@tonic-gate crypto_session_data_t **session_table; 18190Sstevel@tonic-gate crypto_session_data_t **new; 18200Sstevel@tonic-gate uint_t session_table_count; 18210Sstevel@tonic-gate uint_t need; 18220Sstevel@tonic-gate size_t current_allocation; 18230Sstevel@tonic-gate size_t new_allocation; 18243916Skrishna int rv; 18250Sstevel@tonic-gate 18260Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cm->cm_lock)); 18270Sstevel@tonic-gate 18280Sstevel@tonic-gate session_table_count = cm->cm_session_table_count; 18290Sstevel@tonic-gate session_table = cm->cm_session_table; 18300Sstevel@tonic-gate need = session_table_count + CRYPTO_SESSION_CHUNK; 18310Sstevel@tonic-gate 18320Sstevel@tonic-gate current_allocation = session_table_count * sizeof (void *); 18330Sstevel@tonic-gate new_allocation = need * sizeof (void *); 18340Sstevel@tonic-gate 18350Sstevel@tonic-gate /* 18360Sstevel@tonic-gate * Memory needed to grow the session table is checked 18370Sstevel@tonic-gate * against the project.max-crypto-memory resource control. 18380Sstevel@tonic-gate */ 18393916Skrishna if ((rv = crypto_buffer_check(new_allocation - current_allocation)) != 18403916Skrishna CRYPTO_SUCCESS) { 18413916Skrishna return (rv); 18423916Skrishna } 18430Sstevel@tonic-gate 18440Sstevel@tonic-gate /* drop lock while we allocate memory */ 18450Sstevel@tonic-gate mutex_exit(&cm->cm_lock); 18460Sstevel@tonic-gate new = kmem_zalloc(new_allocation, KM_SLEEP); 18470Sstevel@tonic-gate mutex_enter(&cm->cm_lock); 18480Sstevel@tonic-gate 18490Sstevel@tonic-gate /* check if another thread increased the table size */ 18500Sstevel@tonic-gate if (session_table_count != cm->cm_session_table_count) { 18510Sstevel@tonic-gate kmem_free(new, new_allocation); 18520Sstevel@tonic-gate return (CRYPTO_SUCCESS); 18530Sstevel@tonic-gate } 18540Sstevel@tonic-gate 18550Sstevel@tonic-gate bcopy(session_table, new, current_allocation); 18560Sstevel@tonic-gate kmem_free(session_table, current_allocation); 18570Sstevel@tonic-gate cm->cm_session_table = new; 18580Sstevel@tonic-gate cm->cm_session_table_count += CRYPTO_SESSION_CHUNK; 18590Sstevel@tonic-gate 18600Sstevel@tonic-gate return (CRYPTO_SUCCESS); 18610Sstevel@tonic-gate } 18620Sstevel@tonic-gate 18630Sstevel@tonic-gate /* 18640Sstevel@tonic-gate * Find unused entry in session table and return it's index. 18650Sstevel@tonic-gate * Initialize session table entry. 18660Sstevel@tonic-gate */ 18670Sstevel@tonic-gate /* ARGSUSED */ 18680Sstevel@tonic-gate static int 18690Sstevel@tonic-gate crypto_open_session(dev_t dev, uint_t flags, crypto_session_id_t *session_index, 18700Sstevel@tonic-gate crypto_provider_id_t provider_id) 18710Sstevel@tonic-gate { 18720Sstevel@tonic-gate crypto_session_data_t **session_table; 18730Sstevel@tonic-gate crypto_session_data_t *sp; 18740Sstevel@tonic-gate crypto_minor_t *cm; 18750Sstevel@tonic-gate uint_t session_table_count; 18760Sstevel@tonic-gate uint_t i; 18770Sstevel@tonic-gate int rv; 18780Sstevel@tonic-gate crypto_provider_session_t *ps; 18790Sstevel@tonic-gate kcf_provider_desc_t *provider; 18800Sstevel@tonic-gate 18810Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 18820Sstevel@tonic-gate cmn_err(CE_WARN, "crypto_open_session: failed holding minor"); 18830Sstevel@tonic-gate return (CRYPTO_FAILED); 18840Sstevel@tonic-gate } 18850Sstevel@tonic-gate 18860Sstevel@tonic-gate /* initialize provider_array */ 18870Sstevel@tonic-gate if (cm->cm_provider_array == NULL) { 18880Sstevel@tonic-gate rv = crypto_get_provider_list(cm, NULL, NULL, DONT_RETURN_LIST); 18890Sstevel@tonic-gate if (rv != 0) { 18900Sstevel@tonic-gate crypto_release_minor(cm); 18910Sstevel@tonic-gate return (rv); 18920Sstevel@tonic-gate } 18930Sstevel@tonic-gate } 18940Sstevel@tonic-gate 18950Sstevel@tonic-gate mutex_enter(&cm->cm_lock); 18960Sstevel@tonic-gate /* index must be less than count of providers */ 18970Sstevel@tonic-gate if (provider_id >= cm->cm_provider_count) { 18980Sstevel@tonic-gate mutex_exit(&cm->cm_lock); 18990Sstevel@tonic-gate crypto_release_minor(cm); 19000Sstevel@tonic-gate return (CRYPTO_INVALID_PROVIDER_ID); 19010Sstevel@tonic-gate } 19020Sstevel@tonic-gate ASSERT(cm->cm_provider_array != NULL); 19030Sstevel@tonic-gate 19040Sstevel@tonic-gate rv = crypto_get_provider_session(cm, provider_id, &ps); 19050Sstevel@tonic-gate if (rv != CRYPTO_SUCCESS) { 19060Sstevel@tonic-gate mutex_exit(&cm->cm_lock); 19070Sstevel@tonic-gate crypto_release_minor(cm); 19080Sstevel@tonic-gate return (rv); 19090Sstevel@tonic-gate } 19100Sstevel@tonic-gate provider = cm->cm_provider_array[provider_id]; 19110Sstevel@tonic-gate 19120Sstevel@tonic-gate again: 19130Sstevel@tonic-gate session_table_count = cm->cm_session_table_count; 19140Sstevel@tonic-gate session_table = cm->cm_session_table; 19150Sstevel@tonic-gate 19160Sstevel@tonic-gate /* session handles start with 1 */ 19170Sstevel@tonic-gate for (i = 1; i < session_table_count; i++) { 19180Sstevel@tonic-gate if (session_table[i] == NULL) 19190Sstevel@tonic-gate break; 19200Sstevel@tonic-gate } 19210Sstevel@tonic-gate 19220Sstevel@tonic-gate if (i == session_table_count || session_table_count == 0) { 19230Sstevel@tonic-gate if ((rv = grow_session_table(cm)) != CRYPTO_SUCCESS) { 19240Sstevel@tonic-gate crypto_release_provider_session(cm, ps); 19250Sstevel@tonic-gate mutex_exit(&cm->cm_lock); 19260Sstevel@tonic-gate crypto_release_minor(cm); 19270Sstevel@tonic-gate return (rv); 19280Sstevel@tonic-gate } 19290Sstevel@tonic-gate goto again; 19300Sstevel@tonic-gate } 19310Sstevel@tonic-gate 19320Sstevel@tonic-gate sp = kmem_cache_alloc(crypto_session_cache, KM_SLEEP); 19330Sstevel@tonic-gate sp->sd_flags = 0; 19340Sstevel@tonic-gate sp->sd_find_init_cookie = NULL; 19350Sstevel@tonic-gate sp->sd_digest_ctx = NULL; 19360Sstevel@tonic-gate sp->sd_encr_ctx = NULL; 19370Sstevel@tonic-gate sp->sd_decr_ctx = NULL; 19380Sstevel@tonic-gate sp->sd_sign_ctx = NULL; 19390Sstevel@tonic-gate sp->sd_verify_ctx = NULL; 19400Sstevel@tonic-gate sp->sd_sign_recover_ctx = NULL; 19410Sstevel@tonic-gate sp->sd_verify_recover_ctx = NULL; 19420Sstevel@tonic-gate mutex_init(&sp->sd_lock, NULL, MUTEX_DRIVER, NULL); 19430Sstevel@tonic-gate cv_init(&sp->sd_cv, NULL, CV_DRIVER, NULL); 19440Sstevel@tonic-gate KCF_PROV_REFHOLD(provider); 19450Sstevel@tonic-gate sp->sd_provider = provider; 19460Sstevel@tonic-gate sp->sd_provider_session = ps; 19476424Skrishna 19486424Skrishna /* See the comment for CRYPTO_PRE_APPROVED_LIMIT. */ 19496424Skrishna if ((rv = crypto_buffer_check(crypto_pre_approved_limit)) != 19506424Skrishna CRYPTO_SUCCESS) { 19516424Skrishna sp->sd_pre_approved_amount = 0; 19526424Skrishna } else { 19536424Skrishna sp->sd_pre_approved_amount = crypto_pre_approved_limit; 19546424Skrishna } 19556424Skrishna 19560Sstevel@tonic-gate cm->cm_session_table[i] = sp; 19570Sstevel@tonic-gate mutex_exit(&cm->cm_lock); 19580Sstevel@tonic-gate crypto_release_minor(cm); 19590Sstevel@tonic-gate *session_index = i; 19600Sstevel@tonic-gate 19610Sstevel@tonic-gate return (CRYPTO_SUCCESS); 19620Sstevel@tonic-gate } 19630Sstevel@tonic-gate 19640Sstevel@tonic-gate /* 19650Sstevel@tonic-gate * Close a session. 19660Sstevel@tonic-gate */ 19670Sstevel@tonic-gate static int 19680Sstevel@tonic-gate crypto_close_session(dev_t dev, crypto_session_id_t session_index) 19690Sstevel@tonic-gate { 19700Sstevel@tonic-gate crypto_session_data_t **session_table; 19710Sstevel@tonic-gate crypto_session_data_t *sp; 19720Sstevel@tonic-gate crypto_minor_t *cm; 19730Sstevel@tonic-gate 19740Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 19750Sstevel@tonic-gate cmn_err(CE_WARN, "crypto_close_session: failed holding minor"); 19760Sstevel@tonic-gate return (CRYPTO_FAILED); 19770Sstevel@tonic-gate } 19780Sstevel@tonic-gate 19790Sstevel@tonic-gate mutex_enter(&cm->cm_lock); 19800Sstevel@tonic-gate session_table = cm->cm_session_table; 19810Sstevel@tonic-gate 19820Sstevel@tonic-gate if ((session_index) == 0 || 19830Sstevel@tonic-gate (session_index >= cm->cm_session_table_count)) { 19840Sstevel@tonic-gate mutex_exit(&cm->cm_lock); 19850Sstevel@tonic-gate crypto_release_minor(cm); 19860Sstevel@tonic-gate return (CRYPTO_SESSION_HANDLE_INVALID); 19870Sstevel@tonic-gate } 19880Sstevel@tonic-gate 19890Sstevel@tonic-gate sp = session_table[session_index]; 19900Sstevel@tonic-gate if (sp == NULL) { 19910Sstevel@tonic-gate mutex_exit(&cm->cm_lock); 19920Sstevel@tonic-gate crypto_release_minor(cm); 19930Sstevel@tonic-gate return (CRYPTO_SESSION_HANDLE_INVALID); 19940Sstevel@tonic-gate } 19950Sstevel@tonic-gate /* 19960Sstevel@tonic-gate * If session is in use, free it when the thread 19970Sstevel@tonic-gate * finishes with the session. 19980Sstevel@tonic-gate */ 19990Sstevel@tonic-gate mutex_enter(&sp->sd_lock); 20000Sstevel@tonic-gate if (sp->sd_flags & CRYPTO_SESSION_IS_BUSY) { 20010Sstevel@tonic-gate sp->sd_flags |= CRYPTO_SESSION_IS_CLOSED; 20020Sstevel@tonic-gate mutex_exit(&sp->sd_lock); 20030Sstevel@tonic-gate } else { 20046424Skrishna ASSERT(sp->sd_pre_approved_amount == 0 || 20056424Skrishna sp->sd_pre_approved_amount == crypto_pre_approved_limit); 20066424Skrishna CRYPTO_DECREMENT_RCTL(sp->sd_pre_approved_amount); 20076424Skrishna 20080Sstevel@tonic-gate if (sp->sd_find_init_cookie != NULL) { 20090Sstevel@tonic-gate (void) crypto_free_find_ctx(sp); 20100Sstevel@tonic-gate } 20110Sstevel@tonic-gate 20120Sstevel@tonic-gate crypto_release_provider_session(cm, sp->sd_provider_session); 20130Sstevel@tonic-gate KCF_PROV_REFRELE(sp->sd_provider); 20140Sstevel@tonic-gate CRYPTO_CANCEL_ALL_CTX(sp); 20150Sstevel@tonic-gate mutex_destroy(&sp->sd_lock); 20160Sstevel@tonic-gate cv_destroy(&sp->sd_cv); 20170Sstevel@tonic-gate kmem_cache_free(crypto_session_cache, sp); 20180Sstevel@tonic-gate session_table[session_index] = NULL; 20190Sstevel@tonic-gate } 20200Sstevel@tonic-gate 20210Sstevel@tonic-gate mutex_exit(&cm->cm_lock); 20220Sstevel@tonic-gate crypto_release_minor(cm); 20230Sstevel@tonic-gate 20240Sstevel@tonic-gate return (CRYPTO_SUCCESS); 20250Sstevel@tonic-gate } 20260Sstevel@tonic-gate 20270Sstevel@tonic-gate /* 20280Sstevel@tonic-gate * This ioctl opens a session and returns the session ID in os_session. 20290Sstevel@tonic-gate */ 20300Sstevel@tonic-gate /* ARGSUSED */ 20310Sstevel@tonic-gate static int 20320Sstevel@tonic-gate open_session(dev_t dev, caddr_t arg, int mode, int *rval) 20330Sstevel@tonic-gate { 20340Sstevel@tonic-gate crypto_open_session_t open_session; 20350Sstevel@tonic-gate crypto_session_id_t session; 20360Sstevel@tonic-gate int rv; 20370Sstevel@tonic-gate 20380Sstevel@tonic-gate if (copyin(arg, &open_session, sizeof (open_session)) != 0) 20390Sstevel@tonic-gate return (EFAULT); 20400Sstevel@tonic-gate 20410Sstevel@tonic-gate rv = crypto_open_session(dev, open_session.os_flags, 20420Sstevel@tonic-gate &session, open_session.os_provider_id); 20430Sstevel@tonic-gate if (rv != CRYPTO_SUCCESS) { 20440Sstevel@tonic-gate open_session.os_return_value = rv; 20450Sstevel@tonic-gate if (copyout(&open_session, arg, sizeof (open_session)) != 0) { 20460Sstevel@tonic-gate return (EFAULT); 20470Sstevel@tonic-gate } 20480Sstevel@tonic-gate return (0); 20490Sstevel@tonic-gate } 20500Sstevel@tonic-gate 20510Sstevel@tonic-gate open_session.os_session = session; 20520Sstevel@tonic-gate open_session.os_return_value = CRYPTO_SUCCESS; 20530Sstevel@tonic-gate 20540Sstevel@tonic-gate if (copyout(&open_session, arg, sizeof (open_session)) != 0) { 20550Sstevel@tonic-gate return (EFAULT); 20560Sstevel@tonic-gate } 20570Sstevel@tonic-gate return (0); 20580Sstevel@tonic-gate } 20590Sstevel@tonic-gate 20600Sstevel@tonic-gate /* 20610Sstevel@tonic-gate * This ioctl closes a session. 20620Sstevel@tonic-gate */ 20630Sstevel@tonic-gate /* ARGSUSED */ 20640Sstevel@tonic-gate static int 20650Sstevel@tonic-gate close_session(dev_t dev, caddr_t arg, int mode, int *rval) 20660Sstevel@tonic-gate { 20670Sstevel@tonic-gate crypto_close_session_t close_session; 20680Sstevel@tonic-gate int rv; 20690Sstevel@tonic-gate 20700Sstevel@tonic-gate if (copyin(arg, &close_session, sizeof (close_session)) != 0) 20710Sstevel@tonic-gate return (EFAULT); 20720Sstevel@tonic-gate 20730Sstevel@tonic-gate rv = crypto_close_session(dev, close_session.cs_session); 2074904Smcpowers close_session.cs_return_value = rv; 20750Sstevel@tonic-gate if (copyout(&close_session, arg, sizeof (close_session)) != 0) { 20760Sstevel@tonic-gate return (EFAULT); 20770Sstevel@tonic-gate } 20780Sstevel@tonic-gate return (0); 20790Sstevel@tonic-gate } 20800Sstevel@tonic-gate 20810Sstevel@tonic-gate /* 20820Sstevel@tonic-gate * Copy data model dependent mechanism structure into a kernel mechanism 20830Sstevel@tonic-gate * structure. Allocate param storage if necessary. 20840Sstevel@tonic-gate */ 20850Sstevel@tonic-gate static boolean_t 20866424Skrishna copyin_mech(int mode, crypto_session_data_t *sp, crypto_mechanism_t *in_mech, 20876424Skrishna crypto_mechanism_t *out_mech, size_t *out_rctl_bytes, 20886424Skrishna boolean_t *out_rctl_chk, int *out_rv, int *out_error) 20890Sstevel@tonic-gate { 20900Sstevel@tonic-gate STRUCT_DECL(crypto_mechanism, mech); 20910Sstevel@tonic-gate caddr_t param; 20920Sstevel@tonic-gate size_t param_len; 20936424Skrishna size_t rctl_bytes = 0; 20940Sstevel@tonic-gate int error = 0; 20950Sstevel@tonic-gate int rv = 0; 20960Sstevel@tonic-gate 20970Sstevel@tonic-gate STRUCT_INIT(mech, mode); 20980Sstevel@tonic-gate bcopy(in_mech, STRUCT_BUF(mech), STRUCT_SIZE(mech)); 20990Sstevel@tonic-gate param = STRUCT_FGETP(mech, cm_param); 21000Sstevel@tonic-gate param_len = STRUCT_FGET(mech, cm_param_len); 21010Sstevel@tonic-gate out_mech->cm_type = STRUCT_FGET(mech, cm_type); 21020Sstevel@tonic-gate out_mech->cm_param = NULL; 21030Sstevel@tonic-gate out_mech->cm_param_len = 0; 21040Sstevel@tonic-gate if (param != NULL && param_len != 0) { 21050Sstevel@tonic-gate if (param_len > crypto_max_buffer_len) { 21060Sstevel@tonic-gate cmn_err(CE_NOTE, "copyin_mech: buffer greater than " 21070Sstevel@tonic-gate "%ld bytes, pid = %d", crypto_max_buffer_len, 21080Sstevel@tonic-gate curproc->p_pid); 21090Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD; 21100Sstevel@tonic-gate goto out; 21110Sstevel@tonic-gate } 21120Sstevel@tonic-gate 21136424Skrishna rv = CRYPTO_BUFFER_CHECK(sp, param_len, *out_rctl_chk); 21146424Skrishna if (rv != CRYPTO_SUCCESS) { 21156424Skrishna goto out; 21160Sstevel@tonic-gate } 21176424Skrishna rctl_bytes = param_len; 21186424Skrishna 21190Sstevel@tonic-gate out_mech->cm_param = kmem_alloc(param_len, KM_SLEEP); 21200Sstevel@tonic-gate if (copyin((char *)param, out_mech->cm_param, param_len) != 0) { 21210Sstevel@tonic-gate kmem_free(out_mech->cm_param, param_len); 21220Sstevel@tonic-gate out_mech->cm_param = NULL; 21230Sstevel@tonic-gate error = EFAULT; 21240Sstevel@tonic-gate goto out; 21250Sstevel@tonic-gate } 21260Sstevel@tonic-gate out_mech->cm_param_len = param_len; 21270Sstevel@tonic-gate } 21280Sstevel@tonic-gate out: 21290Sstevel@tonic-gate *out_rctl_bytes = rctl_bytes; 21300Sstevel@tonic-gate *out_rv = rv; 21310Sstevel@tonic-gate *out_error = error; 21320Sstevel@tonic-gate return ((rv | error) ? B_FALSE : B_TRUE); 21330Sstevel@tonic-gate } 21340Sstevel@tonic-gate 21350Sstevel@tonic-gate /* 21360Sstevel@tonic-gate * Free key attributes when key type is CRYPTO_KEY_ATTR_LIST. 21370Sstevel@tonic-gate * The crypto_key structure is not freed. 21380Sstevel@tonic-gate */ 21390Sstevel@tonic-gate static void 21400Sstevel@tonic-gate crypto_free_key_attributes(crypto_key_t *key) 21410Sstevel@tonic-gate { 21420Sstevel@tonic-gate crypto_object_attribute_t *attrs; 21430Sstevel@tonic-gate size_t len = 0; 21440Sstevel@tonic-gate int i; 21450Sstevel@tonic-gate 21460Sstevel@tonic-gate ASSERT(key->ck_format == CRYPTO_KEY_ATTR_LIST); 21470Sstevel@tonic-gate if (key->ck_count == 0 || key->ck_attrs == NULL) 21480Sstevel@tonic-gate return; 21490Sstevel@tonic-gate 21500Sstevel@tonic-gate /* compute the size of the container */ 21510Sstevel@tonic-gate len = key->ck_count * sizeof (crypto_object_attribute_t); 21520Sstevel@tonic-gate 21530Sstevel@tonic-gate /* total up the size of all attributes in the container */ 21540Sstevel@tonic-gate for (i = 0; i < key->ck_count; i++) { 21550Sstevel@tonic-gate attrs = &key->ck_attrs[i]; 21560Sstevel@tonic-gate if (attrs->oa_value_len != 0 && 21570Sstevel@tonic-gate attrs->oa_value != NULL) { 21580Sstevel@tonic-gate len += roundup(attrs->oa_value_len, sizeof (caddr_t)); 21590Sstevel@tonic-gate } 21600Sstevel@tonic-gate } 21610Sstevel@tonic-gate 21620Sstevel@tonic-gate bzero(key->ck_attrs, len); 21630Sstevel@tonic-gate kmem_free(key->ck_attrs, len); 21640Sstevel@tonic-gate } 21650Sstevel@tonic-gate 21660Sstevel@tonic-gate /* 21670Sstevel@tonic-gate * Frees allocated storage in the key structure, but doesn't free 21680Sstevel@tonic-gate * the key structure. 21690Sstevel@tonic-gate */ 21700Sstevel@tonic-gate static void 21710Sstevel@tonic-gate free_crypto_key(crypto_key_t *key) 21720Sstevel@tonic-gate { 21730Sstevel@tonic-gate switch (key->ck_format) { 21740Sstevel@tonic-gate case CRYPTO_KEY_RAW: { 21750Sstevel@tonic-gate size_t len; 21760Sstevel@tonic-gate 21770Sstevel@tonic-gate if (key->ck_length == 0 || key->ck_data == NULL) 21780Sstevel@tonic-gate break; 21790Sstevel@tonic-gate 21800Sstevel@tonic-gate len = CRYPTO_BITS2BYTES(key->ck_length); 21810Sstevel@tonic-gate bzero(key->ck_data, len); 21820Sstevel@tonic-gate kmem_free(key->ck_data, len); 21830Sstevel@tonic-gate break; 21840Sstevel@tonic-gate } 21850Sstevel@tonic-gate 21860Sstevel@tonic-gate case CRYPTO_KEY_ATTR_LIST: 21870Sstevel@tonic-gate crypto_free_key_attributes(key); 21880Sstevel@tonic-gate break; 21890Sstevel@tonic-gate 21900Sstevel@tonic-gate default: 21910Sstevel@tonic-gate break; 21920Sstevel@tonic-gate } 21930Sstevel@tonic-gate } 21940Sstevel@tonic-gate 21950Sstevel@tonic-gate /* 21960Sstevel@tonic-gate * Copy in an array of crypto_object_attribute structures from user-space. 21970Sstevel@tonic-gate * Kernel memory is allocated for the array and the value of each attribute 21980Sstevel@tonic-gate * in the array. Since unprivileged users can specify the size of attributes, 21990Sstevel@tonic-gate * the amount of memory needed is charged against the 22000Sstevel@tonic-gate * project.max-crypto-memory resource control. 22010Sstevel@tonic-gate * 22020Sstevel@tonic-gate * Attribute values are copied in from user-space if copyin_value is set to 22030Sstevel@tonic-gate * B_TRUE. This routine returns B_TRUE if the copyin was successful. 22040Sstevel@tonic-gate */ 22050Sstevel@tonic-gate static boolean_t 22066424Skrishna copyin_attributes(int mode, crypto_session_data_t *sp, 22076424Skrishna uint_t count, caddr_t oc_attributes, 22080Sstevel@tonic-gate crypto_object_attribute_t **k_attrs_out, size_t *k_attrs_size_out, 22090Sstevel@tonic-gate caddr_t *u_attrs_out, int *out_rv, int *out_error, size_t *out_rctl_bytes, 22106424Skrishna boolean_t *out_rctl_chk, boolean_t copyin_value) 22110Sstevel@tonic-gate { 22120Sstevel@tonic-gate STRUCT_DECL(crypto_object_attribute, oa); 22130Sstevel@tonic-gate crypto_object_attribute_t *k_attrs = NULL; 22140Sstevel@tonic-gate caddr_t attrs = NULL, ap, p, value; 22150Sstevel@tonic-gate caddr_t k_attrs_buf; 22160Sstevel@tonic-gate size_t k_attrs_len; 22170Sstevel@tonic-gate size_t k_attrs_buf_len = 0; 22180Sstevel@tonic-gate size_t k_attrs_total_len = 0; 22190Sstevel@tonic-gate size_t tmp_len; 22200Sstevel@tonic-gate size_t rctl_bytes = 0; 22210Sstevel@tonic-gate size_t len = 0; 22220Sstevel@tonic-gate size_t value_len; 22230Sstevel@tonic-gate int error = 0; 22240Sstevel@tonic-gate int rv = 0; 22250Sstevel@tonic-gate int i; 22260Sstevel@tonic-gate 22270Sstevel@tonic-gate STRUCT_INIT(oa, mode); 22280Sstevel@tonic-gate 22290Sstevel@tonic-gate if (count == 0) { 22300Sstevel@tonic-gate rv = CRYPTO_SUCCESS; 22310Sstevel@tonic-gate goto out; 22320Sstevel@tonic-gate } 22330Sstevel@tonic-gate 22340Sstevel@tonic-gate if (count > CRYPTO_MAX_ATTRIBUTE_COUNT) { 22350Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD; 22360Sstevel@tonic-gate goto out; 22370Sstevel@tonic-gate } 22380Sstevel@tonic-gate 22390Sstevel@tonic-gate /* compute size of crypto_object_attribute array */ 22400Sstevel@tonic-gate len = count * STRUCT_SIZE(oa); 22410Sstevel@tonic-gate 22420Sstevel@tonic-gate /* this allocation is not charged against the user's resource limit */ 22430Sstevel@tonic-gate attrs = kmem_alloc(len, KM_SLEEP); 22440Sstevel@tonic-gate if (copyin(oc_attributes, attrs, len) != 0) { 22450Sstevel@tonic-gate error = EFAULT; 22460Sstevel@tonic-gate goto out; 22470Sstevel@tonic-gate } 22480Sstevel@tonic-gate 22490Sstevel@tonic-gate /* figure out how much memory to allocate for all of the attributes */ 22500Sstevel@tonic-gate ap = attrs; 22510Sstevel@tonic-gate for (i = 0; i < count; i++) { 22520Sstevel@tonic-gate bcopy(ap, STRUCT_BUF(oa), STRUCT_SIZE(oa)); 22530Sstevel@tonic-gate tmp_len = roundup(STRUCT_FGET(oa, oa_value_len), 22540Sstevel@tonic-gate sizeof (caddr_t)); 22550Sstevel@tonic-gate if (tmp_len > crypto_max_buffer_len) { 22560Sstevel@tonic-gate cmn_err(CE_NOTE, "copyin_attributes: buffer greater " 22570Sstevel@tonic-gate "than %ld bytes, pid = %d", crypto_max_buffer_len, 22580Sstevel@tonic-gate curproc->p_pid); 22590Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD; 22600Sstevel@tonic-gate goto out; 22610Sstevel@tonic-gate } 22620Sstevel@tonic-gate if (STRUCT_FGETP(oa, oa_value) != NULL) 22630Sstevel@tonic-gate k_attrs_buf_len += tmp_len; 22640Sstevel@tonic-gate ap += STRUCT_SIZE(oa); 22650Sstevel@tonic-gate } 22660Sstevel@tonic-gate 22670Sstevel@tonic-gate k_attrs_len = count * sizeof (crypto_object_attribute_t); 22680Sstevel@tonic-gate k_attrs_total_len = k_attrs_buf_len + k_attrs_len; 22696424Skrishna 22706424Skrishna rv = CRYPTO_BUFFER_CHECK(sp, k_attrs_total_len, *out_rctl_chk); 22716424Skrishna if (rv != CRYPTO_SUCCESS) { 22726424Skrishna goto out; 22736424Skrishna } 22746424Skrishna rctl_bytes = k_attrs_total_len; 22750Sstevel@tonic-gate 22760Sstevel@tonic-gate /* one big allocation for everything */ 22770Sstevel@tonic-gate k_attrs = kmem_alloc(k_attrs_total_len, KM_SLEEP); 22780Sstevel@tonic-gate k_attrs_buf = (char *)k_attrs + k_attrs_len; 22790Sstevel@tonic-gate 22800Sstevel@tonic-gate ap = attrs; 22810Sstevel@tonic-gate p = k_attrs_buf; 22820Sstevel@tonic-gate for (i = 0; i < count; i++) { 22830Sstevel@tonic-gate bcopy(ap, STRUCT_BUF(oa), STRUCT_SIZE(oa)); 22840Sstevel@tonic-gate k_attrs[i].oa_type = STRUCT_FGET(oa, oa_type); 22850Sstevel@tonic-gate value = STRUCT_FGETP(oa, oa_value); 22860Sstevel@tonic-gate value_len = STRUCT_FGET(oa, oa_value_len); 22870Sstevel@tonic-gate if (value != NULL && value_len != 0 && copyin_value) { 22880Sstevel@tonic-gate if (copyin(value, p, value_len) != 0) { 22890Sstevel@tonic-gate kmem_free(k_attrs, k_attrs_total_len); 22900Sstevel@tonic-gate k_attrs = NULL; 22910Sstevel@tonic-gate error = EFAULT; 22920Sstevel@tonic-gate goto out; 22930Sstevel@tonic-gate } 22940Sstevel@tonic-gate } 22950Sstevel@tonic-gate 2296292Smcpowers if (value != NULL) { 2297292Smcpowers k_attrs[i].oa_value = p; 2298292Smcpowers p += roundup(value_len, sizeof (caddr_t)); 2299292Smcpowers } else { 2300292Smcpowers k_attrs[i].oa_value = NULL; 2301292Smcpowers } 23020Sstevel@tonic-gate k_attrs[i].oa_value_len = value_len; 23030Sstevel@tonic-gate ap += STRUCT_SIZE(oa); 23040Sstevel@tonic-gate } 23050Sstevel@tonic-gate out: 23060Sstevel@tonic-gate if (attrs != NULL) { 23070Sstevel@tonic-gate /* 23080Sstevel@tonic-gate * Free the array if there is a failure or the caller 23090Sstevel@tonic-gate * doesn't want the array to be returned. 23100Sstevel@tonic-gate */ 23110Sstevel@tonic-gate if (error != 0 || rv != CRYPTO_SUCCESS || u_attrs_out == NULL) { 23120Sstevel@tonic-gate kmem_free(attrs, len); 23130Sstevel@tonic-gate attrs = NULL; 23140Sstevel@tonic-gate } 23150Sstevel@tonic-gate } 23160Sstevel@tonic-gate 23170Sstevel@tonic-gate if (u_attrs_out != NULL) 23180Sstevel@tonic-gate *u_attrs_out = attrs; 23190Sstevel@tonic-gate if (k_attrs_size_out != NULL) 23200Sstevel@tonic-gate *k_attrs_size_out = k_attrs_total_len; 23210Sstevel@tonic-gate *k_attrs_out = k_attrs; 23220Sstevel@tonic-gate *out_rctl_bytes = rctl_bytes; 23230Sstevel@tonic-gate *out_rv = rv; 23240Sstevel@tonic-gate *out_error = error; 23250Sstevel@tonic-gate return ((rv | error) ? B_FALSE : B_TRUE); 23260Sstevel@tonic-gate } 23270Sstevel@tonic-gate 23280Sstevel@tonic-gate /* 23290Sstevel@tonic-gate * Copy data model dependent raw key into a kernel key 23300Sstevel@tonic-gate * structure. Checks key length or attribute lengths against 23310Sstevel@tonic-gate * resource controls before allocating memory. Returns B_TRUE 23320Sstevel@tonic-gate * if both error and rv are set to 0. 23330Sstevel@tonic-gate */ 23340Sstevel@tonic-gate static boolean_t 23356424Skrishna copyin_key(int mode, crypto_session_data_t *sp, crypto_key_t *in_key, 23366424Skrishna crypto_key_t *out_key, size_t *out_rctl_bytes, 23376424Skrishna boolean_t *out_rctl_chk, int *out_rv, int *out_error) 23380Sstevel@tonic-gate { 23390Sstevel@tonic-gate STRUCT_DECL(crypto_key, key); 23400Sstevel@tonic-gate crypto_object_attribute_t *k_attrs = NULL; 23410Sstevel@tonic-gate size_t key_bits; 23420Sstevel@tonic-gate size_t key_bytes = 0; 23430Sstevel@tonic-gate size_t rctl_bytes = 0; 23440Sstevel@tonic-gate int count; 23450Sstevel@tonic-gate int error = 0; 23460Sstevel@tonic-gate int rv = CRYPTO_SUCCESS; 23470Sstevel@tonic-gate 23480Sstevel@tonic-gate STRUCT_INIT(key, mode); 23490Sstevel@tonic-gate bcopy(in_key, STRUCT_BUF(key), STRUCT_SIZE(key)); 23500Sstevel@tonic-gate out_key->ck_format = STRUCT_FGET(key, ck_format); 23510Sstevel@tonic-gate switch (out_key->ck_format) { 23520Sstevel@tonic-gate case CRYPTO_KEY_RAW: 23530Sstevel@tonic-gate key_bits = STRUCT_FGET(key, ck_length); 23540Sstevel@tonic-gate if (key_bits != 0) { 23550Sstevel@tonic-gate key_bytes = CRYPTO_BITS2BYTES(key_bits); 23560Sstevel@tonic-gate if (key_bytes > crypto_max_buffer_len) { 23570Sstevel@tonic-gate cmn_err(CE_NOTE, "copyin_key: buffer greater " 23580Sstevel@tonic-gate "than %ld bytes, pid = %d", 23590Sstevel@tonic-gate crypto_max_buffer_len, curproc->p_pid); 23600Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD; 23610Sstevel@tonic-gate goto out; 23620Sstevel@tonic-gate } 23630Sstevel@tonic-gate 23646424Skrishna rv = CRYPTO_BUFFER_CHECK(sp, key_bytes, 23656424Skrishna *out_rctl_chk); 23660Sstevel@tonic-gate if (rv != CRYPTO_SUCCESS) { 23670Sstevel@tonic-gate goto out; 23680Sstevel@tonic-gate } 23696424Skrishna rctl_bytes = key_bytes; 23700Sstevel@tonic-gate 23710Sstevel@tonic-gate out_key->ck_data = kmem_alloc(key_bytes, KM_SLEEP); 23720Sstevel@tonic-gate 23730Sstevel@tonic-gate if (copyin((char *)STRUCT_FGETP(key, ck_data), 23740Sstevel@tonic-gate out_key->ck_data, key_bytes) != 0) { 23750Sstevel@tonic-gate kmem_free(out_key->ck_data, key_bytes); 23760Sstevel@tonic-gate out_key->ck_data = NULL; 23770Sstevel@tonic-gate out_key->ck_length = 0; 23780Sstevel@tonic-gate error = EFAULT; 23790Sstevel@tonic-gate goto out; 23800Sstevel@tonic-gate } 23810Sstevel@tonic-gate } 23820Sstevel@tonic-gate out_key->ck_length = key_bits; 23830Sstevel@tonic-gate break; 23840Sstevel@tonic-gate 23850Sstevel@tonic-gate case CRYPTO_KEY_ATTR_LIST: 23860Sstevel@tonic-gate count = STRUCT_FGET(key, ck_count); 23870Sstevel@tonic-gate 23886424Skrishna if (copyin_attributes(mode, sp, count, 23890Sstevel@tonic-gate (caddr_t)STRUCT_FGETP(key, ck_attrs), &k_attrs, NULL, NULL, 23906424Skrishna &rv, &error, &rctl_bytes, out_rctl_chk, B_TRUE)) { 23910Sstevel@tonic-gate out_key->ck_count = count; 23920Sstevel@tonic-gate out_key->ck_attrs = k_attrs; 23930Sstevel@tonic-gate k_attrs = NULL; 23940Sstevel@tonic-gate } else { 23950Sstevel@tonic-gate out_key->ck_count = 0; 23960Sstevel@tonic-gate out_key->ck_attrs = NULL; 23970Sstevel@tonic-gate } 23980Sstevel@tonic-gate break; 23990Sstevel@tonic-gate 24000Sstevel@tonic-gate case CRYPTO_KEY_REFERENCE: 24010Sstevel@tonic-gate out_key->ck_obj_id = STRUCT_FGET(key, ck_obj_id); 24020Sstevel@tonic-gate break; 24030Sstevel@tonic-gate 24040Sstevel@tonic-gate default: 24050Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD; 24060Sstevel@tonic-gate } 24070Sstevel@tonic-gate 24080Sstevel@tonic-gate out: 24090Sstevel@tonic-gate *out_rctl_bytes = rctl_bytes; 24100Sstevel@tonic-gate *out_rv = rv; 24110Sstevel@tonic-gate *out_error = error; 24120Sstevel@tonic-gate return ((rv | error) ? B_FALSE : B_TRUE); 24130Sstevel@tonic-gate } 24140Sstevel@tonic-gate 24150Sstevel@tonic-gate /* 24160Sstevel@tonic-gate * This routine does two things: 24170Sstevel@tonic-gate * 1. Given a crypto_minor structure and a session ID, it returns 24180Sstevel@tonic-gate * a valid session pointer. 24190Sstevel@tonic-gate * 2. It checks that the provider, to which the session has been opened, 24200Sstevel@tonic-gate * has not been removed. 24210Sstevel@tonic-gate */ 24220Sstevel@tonic-gate static boolean_t 24230Sstevel@tonic-gate get_session_ptr(crypto_session_id_t i, crypto_minor_t *cm, 24240Sstevel@tonic-gate crypto_session_data_t **session_ptr, int *out_error, int *out_rv) 24250Sstevel@tonic-gate { 24260Sstevel@tonic-gate crypto_session_data_t *sp = NULL; 24270Sstevel@tonic-gate int rv = CRYPTO_SESSION_HANDLE_INVALID; 24280Sstevel@tonic-gate int error = 0; 24290Sstevel@tonic-gate 24300Sstevel@tonic-gate mutex_enter(&cm->cm_lock); 24310Sstevel@tonic-gate if ((i < cm->cm_session_table_count) && 24320Sstevel@tonic-gate (cm->cm_session_table[i] != NULL)) { 24330Sstevel@tonic-gate sp = cm->cm_session_table[i]; 24340Sstevel@tonic-gate mutex_enter(&sp->sd_lock); 24350Sstevel@tonic-gate mutex_exit(&cm->cm_lock); 24360Sstevel@tonic-gate while (sp->sd_flags & CRYPTO_SESSION_IS_BUSY) { 24370Sstevel@tonic-gate if (cv_wait_sig(&sp->sd_cv, &sp->sd_lock) == 0) { 24380Sstevel@tonic-gate mutex_exit(&sp->sd_lock); 24390Sstevel@tonic-gate sp = NULL; 24400Sstevel@tonic-gate error = EINTR; 24410Sstevel@tonic-gate goto out; 24420Sstevel@tonic-gate } 24430Sstevel@tonic-gate } 24440Sstevel@tonic-gate 24450Sstevel@tonic-gate if (sp->sd_flags & CRYPTO_SESSION_IS_CLOSED) { 24460Sstevel@tonic-gate mutex_exit(&sp->sd_lock); 24470Sstevel@tonic-gate sp = NULL; 24480Sstevel@tonic-gate goto out; 24490Sstevel@tonic-gate } 24500Sstevel@tonic-gate 24510Sstevel@tonic-gate if (KCF_IS_PROV_REMOVED(sp->sd_provider)) { 24520Sstevel@tonic-gate mutex_exit(&sp->sd_lock); 24530Sstevel@tonic-gate sp = NULL; 24540Sstevel@tonic-gate rv = CRYPTO_DEVICE_ERROR; 24550Sstevel@tonic-gate goto out; 24560Sstevel@tonic-gate } 24570Sstevel@tonic-gate 24580Sstevel@tonic-gate rv = CRYPTO_SUCCESS; 24590Sstevel@tonic-gate sp->sd_flags |= CRYPTO_SESSION_IS_BUSY; 24600Sstevel@tonic-gate mutex_exit(&sp->sd_lock); 24610Sstevel@tonic-gate } else { 24620Sstevel@tonic-gate mutex_exit(&cm->cm_lock); 24630Sstevel@tonic-gate } 24640Sstevel@tonic-gate out: 24650Sstevel@tonic-gate *session_ptr = sp; 24660Sstevel@tonic-gate *out_error = error; 24670Sstevel@tonic-gate *out_rv = rv; 24680Sstevel@tonic-gate return ((rv == CRYPTO_SUCCESS && error == 0) ? B_TRUE : B_FALSE); 24690Sstevel@tonic-gate } 24700Sstevel@tonic-gate 24716424Skrishna #define CRYPTO_SESSION_RELE(s) if ((s) != NULL) { \ 24720Sstevel@tonic-gate mutex_enter(&((s)->sd_lock)); \ 24730Sstevel@tonic-gate (s)->sd_flags &= ~CRYPTO_SESSION_IS_BUSY; \ 24740Sstevel@tonic-gate cv_broadcast(&(s)->sd_cv); \ 24750Sstevel@tonic-gate mutex_exit(&((s)->sd_lock)); \ 24760Sstevel@tonic-gate } 24770Sstevel@tonic-gate 24780Sstevel@tonic-gate /* ARGSUSED */ 24790Sstevel@tonic-gate static int 24800Sstevel@tonic-gate encrypt_init(dev_t dev, caddr_t arg, int mode, int *rval) 24810Sstevel@tonic-gate { 24820Sstevel@tonic-gate return (cipher_init(dev, arg, mode, crypto_encrypt_init_prov)); 24830Sstevel@tonic-gate } 24840Sstevel@tonic-gate 24850Sstevel@tonic-gate /* ARGSUSED */ 24860Sstevel@tonic-gate static int 24870Sstevel@tonic-gate decrypt_init(dev_t dev, caddr_t arg, int mode, int *rval) 24880Sstevel@tonic-gate { 24890Sstevel@tonic-gate return (cipher_init(dev, arg, mode, crypto_decrypt_init_prov)); 24900Sstevel@tonic-gate } 24910Sstevel@tonic-gate 24920Sstevel@tonic-gate /* 2493904Smcpowers * umech is a mechanism structure that has been copied from user address 2494904Smcpowers * space into kernel address space. Only one copyin has been done. 2495904Smcpowers * The mechanism parameter, if non-null, still points to user address space. 2496904Smcpowers * If the mechanism parameter contains pointers, they are pointers into 2497904Smcpowers * user address space. 2498904Smcpowers * 2499904Smcpowers * kmech is a umech with all pointers and structures in kernel address space. 2500904Smcpowers * 2501904Smcpowers * This routine calls the provider's entry point to copy a umech parameter 2502904Smcpowers * into kernel address space. Kernel memory is allocated by the provider. 2503904Smcpowers */ 2504904Smcpowers static int 2505904Smcpowers crypto_provider_copyin_mech_param(kcf_provider_desc_t *pd, 2506904Smcpowers crypto_mechanism_t *umech, crypto_mechanism_t *kmech, int mode, int *error) 2507904Smcpowers { 2508904Smcpowers crypto_mech_type_t provider_mech_type; 2509904Smcpowers int rv; 2510904Smcpowers 2511904Smcpowers /* get the provider's mech number */ 25123708Skrishna provider_mech_type = KCF_TO_PROV_MECHNUM(pd, umech->cm_type); 2513904Smcpowers 2514904Smcpowers kmech->cm_param = NULL; 2515904Smcpowers kmech->cm_param_len = 0; 2516904Smcpowers kmech->cm_type = provider_mech_type; 2517904Smcpowers rv = KCF_PROV_COPYIN_MECH(pd, umech, kmech, error, mode); 2518904Smcpowers kmech->cm_type = umech->cm_type; 2519904Smcpowers 2520904Smcpowers return (rv); 2521904Smcpowers } 2522904Smcpowers 2523904Smcpowers /* 2524904Smcpowers * umech is a mechanism structure that has been copied from user address 2525904Smcpowers * space into kernel address space. Only one copyin has been done. 2526904Smcpowers * The mechanism parameter, if non-null, still points to user address space. 2527904Smcpowers * If the mechanism parameter contains pointers, they are pointers into 2528904Smcpowers * user address space. 2529904Smcpowers * 2530904Smcpowers * kmech is a umech with all pointers and structures in kernel address space. 2531904Smcpowers * 2532904Smcpowers * This routine calls the provider's entry point to copy a kmech parameter 2533904Smcpowers * into user address space using umech as a template containing 2534904Smcpowers * user address pointers. 2535904Smcpowers */ 2536904Smcpowers static int 2537904Smcpowers crypto_provider_copyout_mech_param(kcf_provider_desc_t *pd, 2538904Smcpowers crypto_mechanism_t *kmech, crypto_mechanism_t *umech, int mode, int *error) 2539904Smcpowers { 2540904Smcpowers crypto_mech_type_t provider_mech_type; 2541904Smcpowers int rv; 2542904Smcpowers 2543904Smcpowers /* get the provider's mech number */ 25443708Skrishna provider_mech_type = KCF_TO_PROV_MECHNUM(pd, umech->cm_type); 2545904Smcpowers 2546904Smcpowers kmech->cm_type = provider_mech_type; 2547904Smcpowers rv = KCF_PROV_COPYOUT_MECH(pd, kmech, umech, error, mode); 2548904Smcpowers kmech->cm_type = umech->cm_type; 2549904Smcpowers 2550904Smcpowers return (rv); 2551904Smcpowers } 2552904Smcpowers 2553904Smcpowers /* 2554904Smcpowers * Call the provider's entry point to free kernel memory that has been 2555904Smcpowers * allocated for the mechanism's parameter. 2556904Smcpowers */ 2557904Smcpowers static void 2558904Smcpowers crypto_free_mech(kcf_provider_desc_t *pd, boolean_t allocated_by_crypto_module, 2559904Smcpowers crypto_mechanism_t *mech) 2560904Smcpowers { 2561904Smcpowers crypto_mech_type_t provider_mech_type; 2562904Smcpowers 2563904Smcpowers if (allocated_by_crypto_module) { 2564904Smcpowers if (mech->cm_param != NULL) 2565904Smcpowers kmem_free(mech->cm_param, mech->cm_param_len); 2566904Smcpowers } else { 2567904Smcpowers /* get the provider's mech number */ 25683708Skrishna provider_mech_type = KCF_TO_PROV_MECHNUM(pd, mech->cm_type); 2569904Smcpowers 2570904Smcpowers if (mech->cm_param != NULL && mech->cm_param_len != 0) { 2571904Smcpowers mech->cm_type = provider_mech_type; 2572904Smcpowers (void) KCF_PROV_FREE_MECH(pd, mech); 2573904Smcpowers } 2574904Smcpowers } 2575904Smcpowers } 2576904Smcpowers 2577904Smcpowers /* 25780Sstevel@tonic-gate * ASSUMPTION: crypto_encrypt_init and crypto_decrypt_init 25790Sstevel@tonic-gate * structures are identical except for field names. 25800Sstevel@tonic-gate */ 25810Sstevel@tonic-gate static int 2582904Smcpowers cipher_init(dev_t dev, caddr_t arg, int mode, int (*init)(crypto_provider_t, 25830Sstevel@tonic-gate crypto_session_id_t, crypto_mechanism_t *, crypto_key_t *, 25840Sstevel@tonic-gate crypto_ctx_template_t, crypto_context_t *, crypto_call_req_t *)) 25850Sstevel@tonic-gate { 25860Sstevel@tonic-gate STRUCT_DECL(crypto_encrypt_init, encrypt_init); 2587904Smcpowers kcf_provider_desc_t *real_provider = NULL; 25880Sstevel@tonic-gate crypto_session_id_t session_id; 25890Sstevel@tonic-gate crypto_mechanism_t mech; 25900Sstevel@tonic-gate crypto_key_t key; 25910Sstevel@tonic-gate crypto_minor_t *cm; 25926424Skrishna crypto_session_data_t *sp = NULL; 25930Sstevel@tonic-gate crypto_context_t cc; 25940Sstevel@tonic-gate crypto_ctx_t **ctxpp; 25950Sstevel@tonic-gate size_t mech_rctl_bytes = 0; 25966424Skrishna boolean_t mech_rctl_chk = B_FALSE; 25970Sstevel@tonic-gate size_t key_rctl_bytes = 0; 25986424Skrishna boolean_t key_rctl_chk = B_FALSE; 25990Sstevel@tonic-gate int error = 0; 26000Sstevel@tonic-gate int rv; 2601904Smcpowers boolean_t allocated_by_crypto_module = B_FALSE; 26021808Smcpowers crypto_func_group_t fg; 26030Sstevel@tonic-gate 26040Sstevel@tonic-gate STRUCT_INIT(encrypt_init, mode); 26050Sstevel@tonic-gate 26060Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 26070Sstevel@tonic-gate cmn_err(CE_WARN, "cipher_init: failed holding minor"); 26080Sstevel@tonic-gate return (ENXIO); 26090Sstevel@tonic-gate } 26100Sstevel@tonic-gate 26110Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(encrypt_init), 26120Sstevel@tonic-gate STRUCT_SIZE(encrypt_init)) != 0) { 26130Sstevel@tonic-gate crypto_release_minor(cm); 26140Sstevel@tonic-gate return (EFAULT); 26150Sstevel@tonic-gate } 26160Sstevel@tonic-gate 26170Sstevel@tonic-gate mech.cm_param = NULL; 26180Sstevel@tonic-gate bzero(&key, sizeof (crypto_key_t)); 26190Sstevel@tonic-gate 26200Sstevel@tonic-gate session_id = STRUCT_FGET(encrypt_init, ei_session); 26210Sstevel@tonic-gate 26220Sstevel@tonic-gate if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) { 26236424Skrishna goto out; 26240Sstevel@tonic-gate } 26250Sstevel@tonic-gate 2626904Smcpowers bcopy(STRUCT_FADDR(encrypt_init, ei_mech), &mech.cm_type, 2627904Smcpowers sizeof (crypto_mech_type_t)); 26280Sstevel@tonic-gate 26291808Smcpowers if (init == crypto_encrypt_init_prov) { 26301808Smcpowers fg = CRYPTO_FG_ENCRYPT; 26311808Smcpowers } else { 26321808Smcpowers fg = CRYPTO_FG_DECRYPT; 26331808Smcpowers } 26340Sstevel@tonic-gate 263510444SVladimir.Kotal@Sun.COM /* We need the key length for provider selection so copy it in now. */ 263610444SVladimir.Kotal@Sun.COM if (!copyin_key(mode, sp, STRUCT_FADDR(encrypt_init, ei_key), &key, 263710444SVladimir.Kotal@Sun.COM &key_rctl_bytes, &key_rctl_chk, &rv, &error)) { 263810444SVladimir.Kotal@Sun.COM goto out; 263910444SVladimir.Kotal@Sun.COM } 264010444SVladimir.Kotal@Sun.COM 264110444SVladimir.Kotal@Sun.COM if ((rv = kcf_get_hardware_provider(mech.cm_type, &key, 264210444SVladimir.Kotal@Sun.COM CRYPTO_MECH_INVALID, NULL, CHECK_RESTRICT_FALSE, sp->sd_provider, 264310444SVladimir.Kotal@Sun.COM &real_provider, fg)) 26441808Smcpowers != CRYPTO_SUCCESS) { 26450Sstevel@tonic-gate goto out; 26460Sstevel@tonic-gate } 26470Sstevel@tonic-gate 2648904Smcpowers rv = crypto_provider_copyin_mech_param(real_provider, 2649904Smcpowers STRUCT_FADDR(encrypt_init, ei_mech), &mech, mode, &error); 2650904Smcpowers 2651904Smcpowers if (rv == CRYPTO_NOT_SUPPORTED) { 2652904Smcpowers allocated_by_crypto_module = B_TRUE; 26536424Skrishna if (!copyin_mech(mode, sp, STRUCT_FADDR(encrypt_init, ei_mech), 26546424Skrishna &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) { 2655904Smcpowers goto out; 2656904Smcpowers } 2657904Smcpowers } else { 2658904Smcpowers if (rv != CRYPTO_SUCCESS) 2659904Smcpowers goto out; 2660904Smcpowers } 2661904Smcpowers 26620Sstevel@tonic-gate rv = (init)(real_provider, sp->sd_provider_session->ps_session, 26630Sstevel@tonic-gate &mech, &key, NULL, &cc, NULL); 26640Sstevel@tonic-gate 26650Sstevel@tonic-gate /* 26660Sstevel@tonic-gate * Check if a context already exists. If so, it means it is being 26670Sstevel@tonic-gate * abandoned. So, cancel it to avoid leaking it. 26680Sstevel@tonic-gate */ 26690Sstevel@tonic-gate ctxpp = (init == crypto_encrypt_init_prov) ? 26700Sstevel@tonic-gate &sp->sd_encr_ctx : &sp->sd_decr_ctx; 26710Sstevel@tonic-gate 26720Sstevel@tonic-gate if (*ctxpp != NULL) 26730Sstevel@tonic-gate CRYPTO_CANCEL_CTX(ctxpp); 26740Sstevel@tonic-gate *ctxpp = (rv == CRYPTO_SUCCESS) ? cc : NULL; 2675904Smcpowers 26760Sstevel@tonic-gate out: 26776424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk); 26786424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk); 26790Sstevel@tonic-gate CRYPTO_SESSION_RELE(sp); 26800Sstevel@tonic-gate crypto_release_minor(cm); 26810Sstevel@tonic-gate 2682904Smcpowers if (real_provider != NULL) { 2683904Smcpowers crypto_free_mech(real_provider, 2684904Smcpowers allocated_by_crypto_module, &mech); 2685904Smcpowers KCF_PROV_REFRELE(real_provider); 2686904Smcpowers } 26870Sstevel@tonic-gate 26880Sstevel@tonic-gate free_crypto_key(&key); 26890Sstevel@tonic-gate 26900Sstevel@tonic-gate if (error != 0) 2691904Smcpowers /* XXX free context */ 26920Sstevel@tonic-gate return (error); 26930Sstevel@tonic-gate 26940Sstevel@tonic-gate STRUCT_FSET(encrypt_init, ei_return_value, rv); 26950Sstevel@tonic-gate if (copyout(STRUCT_BUF(encrypt_init), arg, 26960Sstevel@tonic-gate STRUCT_SIZE(encrypt_init)) != 0) { 2697904Smcpowers /* XXX free context */ 26980Sstevel@tonic-gate return (EFAULT); 26990Sstevel@tonic-gate } 27000Sstevel@tonic-gate return (0); 27010Sstevel@tonic-gate } 27020Sstevel@tonic-gate 27030Sstevel@tonic-gate /* ARGSUSED */ 27040Sstevel@tonic-gate static int 27050Sstevel@tonic-gate encrypt(dev_t dev, caddr_t arg, int mode, int *rval) 27060Sstevel@tonic-gate { 27070Sstevel@tonic-gate return (cipher(dev, arg, mode, crypto_encrypt_single)); 27080Sstevel@tonic-gate } 27090Sstevel@tonic-gate 27100Sstevel@tonic-gate /* ARGSUSED */ 27110Sstevel@tonic-gate static int 27120Sstevel@tonic-gate decrypt(dev_t dev, caddr_t arg, int mode, int *rval) 27130Sstevel@tonic-gate { 27140Sstevel@tonic-gate return (cipher(dev, arg, mode, crypto_decrypt_single)); 27150Sstevel@tonic-gate } 27160Sstevel@tonic-gate 27170Sstevel@tonic-gate /* 27180Sstevel@tonic-gate * ASSUMPTION: crypto_encrypt and crypto_decrypt structures 27190Sstevel@tonic-gate * are identical except for field names. 27200Sstevel@tonic-gate */ 27210Sstevel@tonic-gate static int 27220Sstevel@tonic-gate cipher(dev_t dev, caddr_t arg, int mode, 27230Sstevel@tonic-gate int (*single)(crypto_context_t, crypto_data_t *, crypto_data_t *, 27240Sstevel@tonic-gate crypto_call_req_t *)) 27250Sstevel@tonic-gate { 27260Sstevel@tonic-gate STRUCT_DECL(crypto_encrypt, encrypt); 27270Sstevel@tonic-gate crypto_session_id_t session_id; 27280Sstevel@tonic-gate crypto_minor_t *cm; 27296424Skrishna crypto_session_data_t *sp = NULL; 27300Sstevel@tonic-gate crypto_ctx_t **ctxpp; 27310Sstevel@tonic-gate crypto_data_t data, encr; 27320Sstevel@tonic-gate size_t datalen, encrlen, need = 0; 27334632Smcpowers boolean_t do_inplace; 27340Sstevel@tonic-gate char *encrbuf; 27350Sstevel@tonic-gate int error = 0; 27360Sstevel@tonic-gate int rv; 27376424Skrishna boolean_t rctl_chk = B_FALSE; 27380Sstevel@tonic-gate 27390Sstevel@tonic-gate STRUCT_INIT(encrypt, mode); 27400Sstevel@tonic-gate 27410Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 27420Sstevel@tonic-gate cmn_err(CE_WARN, "cipher: failed holding minor"); 27430Sstevel@tonic-gate return (ENXIO); 27440Sstevel@tonic-gate } 27450Sstevel@tonic-gate 27460Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(encrypt), STRUCT_SIZE(encrypt)) != 0) { 27470Sstevel@tonic-gate crypto_release_minor(cm); 27480Sstevel@tonic-gate return (EFAULT); 27490Sstevel@tonic-gate } 27500Sstevel@tonic-gate 27510Sstevel@tonic-gate data.cd_raw.iov_base = NULL; 27520Sstevel@tonic-gate encr.cd_raw.iov_base = NULL; 27530Sstevel@tonic-gate 27540Sstevel@tonic-gate datalen = STRUCT_FGET(encrypt, ce_datalen); 27550Sstevel@tonic-gate encrlen = STRUCT_FGET(encrypt, ce_encrlen); 27560Sstevel@tonic-gate 27570Sstevel@tonic-gate /* 27580Sstevel@tonic-gate * Don't allocate output buffer unless both buffer pointer and 27590Sstevel@tonic-gate * buffer length are not NULL or 0 (length). 27600Sstevel@tonic-gate */ 27610Sstevel@tonic-gate encrbuf = STRUCT_FGETP(encrypt, ce_encrbuf); 27620Sstevel@tonic-gate if (encrbuf == NULL || encrlen == 0) { 27630Sstevel@tonic-gate encrlen = 0; 27640Sstevel@tonic-gate } 27650Sstevel@tonic-gate 27660Sstevel@tonic-gate if (datalen > crypto_max_buffer_len || 27670Sstevel@tonic-gate encrlen > crypto_max_buffer_len) { 27680Sstevel@tonic-gate cmn_err(CE_NOTE, "cipher: buffer greater than %ld bytes, " 27690Sstevel@tonic-gate "pid = %d", crypto_max_buffer_len, curproc->p_pid); 27700Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD; 27710Sstevel@tonic-gate goto release_minor; 27720Sstevel@tonic-gate } 27730Sstevel@tonic-gate 27746424Skrishna session_id = STRUCT_FGET(encrypt, ce_session); 27756424Skrishna 27766424Skrishna if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) { 27776424Skrishna goto release_minor; 27786424Skrishna } 27796867Skrishna 27806867Skrishna do_inplace = (STRUCT_FGET(encrypt, ce_flags) & 27816867Skrishna CRYPTO_INPLACE_OPERATION) != 0; 27826867Skrishna need = do_inplace ? datalen : datalen + encrlen; 27836867Skrishna 27846424Skrishna if ((rv = CRYPTO_BUFFER_CHECK(sp, need, rctl_chk)) != 27856424Skrishna CRYPTO_SUCCESS) { 27860Sstevel@tonic-gate need = 0; 27870Sstevel@tonic-gate goto release_minor; 27880Sstevel@tonic-gate } 27890Sstevel@tonic-gate 27900Sstevel@tonic-gate INIT_RAW_CRYPTO_DATA(data, datalen); 27910Sstevel@tonic-gate data.cd_miscdata = NULL; 27920Sstevel@tonic-gate 27930Sstevel@tonic-gate if (datalen != 0 && copyin(STRUCT_FGETP(encrypt, ce_databuf), 27940Sstevel@tonic-gate data.cd_raw.iov_base, datalen) != 0) { 27950Sstevel@tonic-gate error = EFAULT; 27960Sstevel@tonic-gate goto release_minor; 27970Sstevel@tonic-gate } 27980Sstevel@tonic-gate 27994632Smcpowers if (do_inplace) { 28004632Smcpowers /* set out = in for in-place */ 28014632Smcpowers encr = data; 28024632Smcpowers } else { 28034632Smcpowers INIT_RAW_CRYPTO_DATA(encr, encrlen); 28044632Smcpowers } 28050Sstevel@tonic-gate 28060Sstevel@tonic-gate ctxpp = (single == crypto_encrypt_single) ? 28070Sstevel@tonic-gate &sp->sd_encr_ctx : &sp->sd_decr_ctx; 28080Sstevel@tonic-gate 28094632Smcpowers if (do_inplace) 2810*11030Sopensolaris@drydog.com /* specify in-place buffers with output = NULL */ 28114632Smcpowers rv = (single)(*ctxpp, &encr, NULL, NULL); 28124632Smcpowers else 28134632Smcpowers rv = (single)(*ctxpp, &data, &encr, NULL); 2814*11030Sopensolaris@drydog.com 28150Sstevel@tonic-gate if (KCF_CONTEXT_DONE(rv)) 28160Sstevel@tonic-gate *ctxpp = NULL; 28170Sstevel@tonic-gate 28180Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) { 28190Sstevel@tonic-gate ASSERT(encr.cd_length <= encrlen); 28200Sstevel@tonic-gate if (encr.cd_length != 0 && copyout(encr.cd_raw.iov_base, 28210Sstevel@tonic-gate encrbuf, encr.cd_length) != 0) { 28220Sstevel@tonic-gate error = EFAULT; 28230Sstevel@tonic-gate goto release_minor; 28240Sstevel@tonic-gate } 28250Sstevel@tonic-gate STRUCT_FSET(encrypt, ce_encrlen, encr.cd_length); 28260Sstevel@tonic-gate } 28270Sstevel@tonic-gate 28280Sstevel@tonic-gate if (rv == CRYPTO_BUFFER_TOO_SMALL) { 28290Sstevel@tonic-gate /* 28300Sstevel@tonic-gate * The providers return CRYPTO_BUFFER_TOO_SMALL even for case 1 28310Sstevel@tonic-gate * of section 11.2 of the pkcs11 spec. We catch it here and 28320Sstevel@tonic-gate * provide the correct pkcs11 return value. 28330Sstevel@tonic-gate */ 28340Sstevel@tonic-gate if (STRUCT_FGETP(encrypt, ce_encrbuf) == NULL) 28350Sstevel@tonic-gate rv = CRYPTO_SUCCESS; 28360Sstevel@tonic-gate STRUCT_FSET(encrypt, ce_encrlen, encr.cd_length); 28370Sstevel@tonic-gate } 28380Sstevel@tonic-gate 28390Sstevel@tonic-gate release_minor: 28406424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk); 28416424Skrishna CRYPTO_SESSION_RELE(sp); 28420Sstevel@tonic-gate crypto_release_minor(cm); 28430Sstevel@tonic-gate 28440Sstevel@tonic-gate if (data.cd_raw.iov_base != NULL) 28450Sstevel@tonic-gate kmem_free(data.cd_raw.iov_base, datalen); 28460Sstevel@tonic-gate 28474632Smcpowers if (!do_inplace && encr.cd_raw.iov_base != NULL) 28480Sstevel@tonic-gate kmem_free(encr.cd_raw.iov_base, encrlen); 28490Sstevel@tonic-gate 28500Sstevel@tonic-gate if (error != 0) 28510Sstevel@tonic-gate return (error); 28520Sstevel@tonic-gate 28530Sstevel@tonic-gate STRUCT_FSET(encrypt, ce_return_value, rv); 28540Sstevel@tonic-gate if (copyout(STRUCT_BUF(encrypt), arg, STRUCT_SIZE(encrypt)) != 0) { 28550Sstevel@tonic-gate return (EFAULT); 28560Sstevel@tonic-gate } 28570Sstevel@tonic-gate return (0); 28580Sstevel@tonic-gate } 28590Sstevel@tonic-gate 28600Sstevel@tonic-gate /* ARGSUSED */ 28610Sstevel@tonic-gate static int 28620Sstevel@tonic-gate encrypt_update(dev_t dev, caddr_t arg, int mode, int *rval) 28630Sstevel@tonic-gate { 28640Sstevel@tonic-gate return (cipher_update(dev, arg, mode, crypto_encrypt_update)); 28650Sstevel@tonic-gate } 28660Sstevel@tonic-gate 28670Sstevel@tonic-gate /* ARGSUSED */ 28680Sstevel@tonic-gate static int 28690Sstevel@tonic-gate decrypt_update(dev_t dev, caddr_t arg, int mode, int *rval) 28700Sstevel@tonic-gate { 28710Sstevel@tonic-gate return (cipher_update(dev, arg, mode, crypto_decrypt_update)); 28720Sstevel@tonic-gate } 28730Sstevel@tonic-gate 28740Sstevel@tonic-gate /* 28750Sstevel@tonic-gate * ASSUMPTION: crypto_encrypt_update and crypto_decrypt_update 28760Sstevel@tonic-gate * structures are identical except for field names. 28770Sstevel@tonic-gate */ 28780Sstevel@tonic-gate static int 28790Sstevel@tonic-gate cipher_update(dev_t dev, caddr_t arg, int mode, 28800Sstevel@tonic-gate int (*update)(crypto_context_t, crypto_data_t *, crypto_data_t *, 28810Sstevel@tonic-gate crypto_call_req_t *)) 28820Sstevel@tonic-gate { 28830Sstevel@tonic-gate STRUCT_DECL(crypto_encrypt_update, encrypt_update); 28840Sstevel@tonic-gate crypto_session_id_t session_id; 28850Sstevel@tonic-gate crypto_minor_t *cm; 28866424Skrishna crypto_session_data_t *sp = NULL; 28870Sstevel@tonic-gate crypto_ctx_t **ctxpp; 28880Sstevel@tonic-gate crypto_data_t data, encr; 28890Sstevel@tonic-gate size_t datalen, encrlen, need = 0; 2890*11030Sopensolaris@drydog.com boolean_t do_inplace; 28910Sstevel@tonic-gate char *encrbuf; 28920Sstevel@tonic-gate int error = 0; 28930Sstevel@tonic-gate int rv; 28946424Skrishna boolean_t rctl_chk = B_FALSE; 28950Sstevel@tonic-gate 28960Sstevel@tonic-gate STRUCT_INIT(encrypt_update, mode); 28970Sstevel@tonic-gate 28980Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 28990Sstevel@tonic-gate cmn_err(CE_WARN, "cipher_update: failed holding minor"); 29000Sstevel@tonic-gate return (ENXIO); 29010Sstevel@tonic-gate } 29020Sstevel@tonic-gate 29030Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(encrypt_update), 29040Sstevel@tonic-gate STRUCT_SIZE(encrypt_update)) != 0) { 29050Sstevel@tonic-gate crypto_release_minor(cm); 29060Sstevel@tonic-gate return (EFAULT); 29070Sstevel@tonic-gate } 29080Sstevel@tonic-gate 29090Sstevel@tonic-gate data.cd_raw.iov_base = NULL; 29100Sstevel@tonic-gate encr.cd_raw.iov_base = NULL; 29110Sstevel@tonic-gate 29120Sstevel@tonic-gate datalen = STRUCT_FGET(encrypt_update, eu_datalen); 29130Sstevel@tonic-gate encrlen = STRUCT_FGET(encrypt_update, eu_encrlen); 29140Sstevel@tonic-gate 29150Sstevel@tonic-gate /* 29160Sstevel@tonic-gate * Don't allocate output buffer unless both buffer pointer and 29170Sstevel@tonic-gate * buffer length are not NULL or 0 (length). 29180Sstevel@tonic-gate */ 29190Sstevel@tonic-gate encrbuf = STRUCT_FGETP(encrypt_update, eu_encrbuf); 29200Sstevel@tonic-gate if (encrbuf == NULL || encrlen == 0) { 29210Sstevel@tonic-gate encrlen = 0; 29220Sstevel@tonic-gate } 29230Sstevel@tonic-gate 29240Sstevel@tonic-gate if (datalen > crypto_max_buffer_len || 29250Sstevel@tonic-gate encrlen > crypto_max_buffer_len) { 29260Sstevel@tonic-gate cmn_err(CE_NOTE, "cipher_update: buffer greater than %ld " 29270Sstevel@tonic-gate "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid); 29280Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD; 29296424Skrishna goto out; 29306424Skrishna } 29316424Skrishna 29326424Skrishna session_id = STRUCT_FGET(encrypt_update, eu_session); 29336424Skrishna 29346424Skrishna if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) { 29356424Skrishna goto out; 29360Sstevel@tonic-gate } 29370Sstevel@tonic-gate 2938*11030Sopensolaris@drydog.com do_inplace = (STRUCT_FGET(encrypt_update, eu_flags) & 2939*11030Sopensolaris@drydog.com CRYPTO_INPLACE_OPERATION) != 0; 2940*11030Sopensolaris@drydog.com need = do_inplace ? datalen : datalen + encrlen; 29416424Skrishna 29426424Skrishna if ((rv = CRYPTO_BUFFER_CHECK(sp, need, rctl_chk)) != 29436424Skrishna CRYPTO_SUCCESS) { 29440Sstevel@tonic-gate need = 0; 29456424Skrishna goto out; 29460Sstevel@tonic-gate } 29470Sstevel@tonic-gate 29480Sstevel@tonic-gate INIT_RAW_CRYPTO_DATA(data, datalen); 29490Sstevel@tonic-gate data.cd_miscdata = NULL; 29500Sstevel@tonic-gate 29510Sstevel@tonic-gate if (datalen != 0 && copyin(STRUCT_FGETP(encrypt_update, eu_databuf), 29520Sstevel@tonic-gate data.cd_raw.iov_base, datalen) != 0) { 29530Sstevel@tonic-gate error = EFAULT; 29546424Skrishna goto out; 29550Sstevel@tonic-gate } 29560Sstevel@tonic-gate 2957*11030Sopensolaris@drydog.com if (do_inplace) { 2958*11030Sopensolaris@drydog.com /* specify in-place buffers with output = input */ 2959*11030Sopensolaris@drydog.com encr = data; 2960*11030Sopensolaris@drydog.com } else { 2961*11030Sopensolaris@drydog.com INIT_RAW_CRYPTO_DATA(encr, encrlen); 2962*11030Sopensolaris@drydog.com } 29630Sstevel@tonic-gate 29640Sstevel@tonic-gate ctxpp = (update == crypto_encrypt_update) ? 29650Sstevel@tonic-gate &sp->sd_encr_ctx : &sp->sd_decr_ctx; 29660Sstevel@tonic-gate 2967*11030Sopensolaris@drydog.com if (do_inplace) 2968*11030Sopensolaris@drydog.com /* specify in-place buffers with output = NULL */ 2969*11030Sopensolaris@drydog.com rv = (update)(*ctxpp, &encr, NULL, NULL); 2970*11030Sopensolaris@drydog.com else 2971*11030Sopensolaris@drydog.com rv = (update)(*ctxpp, &data, &encr, NULL); 29720Sstevel@tonic-gate 29730Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS || rv == CRYPTO_BUFFER_TOO_SMALL) { 29740Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) { 29750Sstevel@tonic-gate ASSERT(encr.cd_length <= encrlen); 29760Sstevel@tonic-gate if (encr.cd_length != 0 && copyout(encr.cd_raw.iov_base, 29770Sstevel@tonic-gate encrbuf, encr.cd_length) != 0) { 29780Sstevel@tonic-gate error = EFAULT; 29790Sstevel@tonic-gate goto out; 29800Sstevel@tonic-gate } 29810Sstevel@tonic-gate } else { 29820Sstevel@tonic-gate /* 29830Sstevel@tonic-gate * The providers return CRYPTO_BUFFER_TOO_SMALL even 29840Sstevel@tonic-gate * for case 1 of section 11.2 of the pkcs11 spec. 29850Sstevel@tonic-gate * We catch it here and provide the correct pkcs11 29860Sstevel@tonic-gate * return value. 29870Sstevel@tonic-gate */ 29880Sstevel@tonic-gate if (STRUCT_FGETP(encrypt_update, eu_encrbuf) == NULL) 29890Sstevel@tonic-gate rv = CRYPTO_SUCCESS; 29900Sstevel@tonic-gate } 29910Sstevel@tonic-gate STRUCT_FSET(encrypt_update, eu_encrlen, encr.cd_length); 29920Sstevel@tonic-gate } else { 29930Sstevel@tonic-gate CRYPTO_CANCEL_CTX(ctxpp); 29940Sstevel@tonic-gate } 29950Sstevel@tonic-gate out: 29966424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk); 29970Sstevel@tonic-gate CRYPTO_SESSION_RELE(sp); 29980Sstevel@tonic-gate crypto_release_minor(cm); 29990Sstevel@tonic-gate 30000Sstevel@tonic-gate if (data.cd_raw.iov_base != NULL) 30010Sstevel@tonic-gate kmem_free(data.cd_raw.iov_base, datalen); 30020Sstevel@tonic-gate 3003*11030Sopensolaris@drydog.com if (!do_inplace && (encr.cd_raw.iov_base != NULL)) 30040Sstevel@tonic-gate kmem_free(encr.cd_raw.iov_base, encrlen); 30050Sstevel@tonic-gate 30060Sstevel@tonic-gate if (error != 0) 30070Sstevel@tonic-gate return (error); 30080Sstevel@tonic-gate 30090Sstevel@tonic-gate STRUCT_FSET(encrypt_update, eu_return_value, rv); 30100Sstevel@tonic-gate if (copyout(STRUCT_BUF(encrypt_update), arg, 30110Sstevel@tonic-gate STRUCT_SIZE(encrypt_update)) != 0) { 30120Sstevel@tonic-gate return (EFAULT); 30130Sstevel@tonic-gate } 30140Sstevel@tonic-gate return (0); 30150Sstevel@tonic-gate } 30160Sstevel@tonic-gate 30170Sstevel@tonic-gate /* ARGSUSED */ 30180Sstevel@tonic-gate static int 30190Sstevel@tonic-gate encrypt_final(dev_t dev, caddr_t arg, int mode, int *rval) 30200Sstevel@tonic-gate { 30210Sstevel@tonic-gate return (common_final(dev, arg, mode, crypto_encrypt_final)); 30220Sstevel@tonic-gate } 30230Sstevel@tonic-gate 30240Sstevel@tonic-gate /* ARGSUSED */ 30250Sstevel@tonic-gate static int 30260Sstevel@tonic-gate decrypt_final(dev_t dev, caddr_t arg, int mode, int *rval) 30270Sstevel@tonic-gate { 30280Sstevel@tonic-gate return (common_final(dev, arg, mode, crypto_decrypt_final)); 30290Sstevel@tonic-gate } 30300Sstevel@tonic-gate 30310Sstevel@tonic-gate /* 30320Sstevel@tonic-gate * ASSUMPTION: crypto_encrypt_final, crypto_decrypt_final, crypto_sign_final, 30330Sstevel@tonic-gate * and crypto_digest_final structures are identical except for field names. 30340Sstevel@tonic-gate */ 30350Sstevel@tonic-gate static int 30360Sstevel@tonic-gate common_final(dev_t dev, caddr_t arg, int mode, 30370Sstevel@tonic-gate int (*final)(crypto_context_t, crypto_data_t *, crypto_call_req_t *)) 30380Sstevel@tonic-gate { 30390Sstevel@tonic-gate STRUCT_DECL(crypto_encrypt_final, encrypt_final); 30400Sstevel@tonic-gate crypto_session_id_t session_id; 30410Sstevel@tonic-gate crypto_minor_t *cm; 30426424Skrishna crypto_session_data_t *sp = NULL; 30430Sstevel@tonic-gate crypto_ctx_t **ctxpp; 30440Sstevel@tonic-gate crypto_data_t encr; 30450Sstevel@tonic-gate size_t encrlen, need = 0; 30460Sstevel@tonic-gate char *encrbuf; 30470Sstevel@tonic-gate int error = 0; 30480Sstevel@tonic-gate int rv; 30496424Skrishna boolean_t rctl_chk = B_FALSE; 30500Sstevel@tonic-gate 30510Sstevel@tonic-gate STRUCT_INIT(encrypt_final, mode); 30520Sstevel@tonic-gate 30530Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 30540Sstevel@tonic-gate cmn_err(CE_WARN, "common_final: failed holding minor"); 30550Sstevel@tonic-gate return (ENXIO); 30560Sstevel@tonic-gate } 30570Sstevel@tonic-gate 30580Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(encrypt_final), 30590Sstevel@tonic-gate STRUCT_SIZE(encrypt_final)) != 0) { 30600Sstevel@tonic-gate crypto_release_minor(cm); 30610Sstevel@tonic-gate return (EFAULT); 30620Sstevel@tonic-gate } 30630Sstevel@tonic-gate 30640Sstevel@tonic-gate encr.cd_format = CRYPTO_DATA_RAW; 30650Sstevel@tonic-gate encr.cd_raw.iov_base = NULL; 30660Sstevel@tonic-gate 30670Sstevel@tonic-gate encrlen = STRUCT_FGET(encrypt_final, ef_encrlen); 30680Sstevel@tonic-gate 30690Sstevel@tonic-gate /* 30700Sstevel@tonic-gate * Don't allocate output buffer unless both buffer pointer and 30710Sstevel@tonic-gate * buffer length are not NULL or 0 (length). 30720Sstevel@tonic-gate */ 30730Sstevel@tonic-gate encrbuf = STRUCT_FGETP(encrypt_final, ef_encrbuf); 30740Sstevel@tonic-gate if (encrbuf == NULL || encrlen == 0) { 30750Sstevel@tonic-gate encrlen = 0; 30760Sstevel@tonic-gate } 30770Sstevel@tonic-gate 30780Sstevel@tonic-gate if (encrlen > crypto_max_buffer_len) { 30790Sstevel@tonic-gate cmn_err(CE_NOTE, "common_final: buffer greater than %ld " 30800Sstevel@tonic-gate "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid); 30810Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD; 30820Sstevel@tonic-gate goto release_minor; 30830Sstevel@tonic-gate } 30840Sstevel@tonic-gate 30856424Skrishna session_id = STRUCT_FGET(encrypt_final, ef_session); 30866424Skrishna 30876424Skrishna if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) { 30886424Skrishna goto release_minor; 30896424Skrishna } 30906424Skrishna 30916424Skrishna if ((rv = CRYPTO_BUFFER_CHECK(sp, encrlen, rctl_chk)) != 30926424Skrishna CRYPTO_SUCCESS) { 30930Sstevel@tonic-gate goto release_minor; 30940Sstevel@tonic-gate } 30950Sstevel@tonic-gate need = encrlen; 30960Sstevel@tonic-gate encr.cd_raw.iov_base = kmem_alloc(encrlen, KM_SLEEP); 30970Sstevel@tonic-gate encr.cd_raw.iov_len = encrlen; 30980Sstevel@tonic-gate 30990Sstevel@tonic-gate encr.cd_offset = 0; 31000Sstevel@tonic-gate encr.cd_length = encrlen; 31010Sstevel@tonic-gate 31020Sstevel@tonic-gate ASSERT(final == crypto_encrypt_final || 31030Sstevel@tonic-gate final == crypto_decrypt_final || final == crypto_sign_final || 31040Sstevel@tonic-gate final == crypto_digest_final); 31050Sstevel@tonic-gate 31060Sstevel@tonic-gate if (final == crypto_encrypt_final) { 31070Sstevel@tonic-gate ctxpp = &sp->sd_encr_ctx; 31080Sstevel@tonic-gate } else if (final == crypto_decrypt_final) { 31090Sstevel@tonic-gate ctxpp = &sp->sd_decr_ctx; 31100Sstevel@tonic-gate } else if (final == crypto_sign_final) { 31110Sstevel@tonic-gate ctxpp = &sp->sd_sign_ctx; 31120Sstevel@tonic-gate } else { 31130Sstevel@tonic-gate ctxpp = &sp->sd_digest_ctx; 31140Sstevel@tonic-gate } 31150Sstevel@tonic-gate 31160Sstevel@tonic-gate rv = (final)(*ctxpp, &encr, NULL); 31170Sstevel@tonic-gate if (KCF_CONTEXT_DONE(rv)) 31180Sstevel@tonic-gate *ctxpp = NULL; 31190Sstevel@tonic-gate 31200Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) { 31210Sstevel@tonic-gate ASSERT(encr.cd_length <= encrlen); 31220Sstevel@tonic-gate if (encr.cd_length != 0 && copyout(encr.cd_raw.iov_base, 31230Sstevel@tonic-gate encrbuf, encr.cd_length) != 0) { 31240Sstevel@tonic-gate error = EFAULT; 31250Sstevel@tonic-gate goto release_minor; 31260Sstevel@tonic-gate } 31270Sstevel@tonic-gate STRUCT_FSET(encrypt_final, ef_encrlen, encr.cd_length); 31280Sstevel@tonic-gate } 31290Sstevel@tonic-gate 31300Sstevel@tonic-gate if (rv == CRYPTO_BUFFER_TOO_SMALL) { 31310Sstevel@tonic-gate /* 31320Sstevel@tonic-gate * The providers return CRYPTO_BUFFER_TOO_SMALL even for case 1 31330Sstevel@tonic-gate * of section 11.2 of the pkcs11 spec. We catch it here and 31340Sstevel@tonic-gate * provide the correct pkcs11 return value. 31350Sstevel@tonic-gate */ 31360Sstevel@tonic-gate if (STRUCT_FGETP(encrypt_final, ef_encrbuf) == NULL) 31370Sstevel@tonic-gate rv = CRYPTO_SUCCESS; 31380Sstevel@tonic-gate STRUCT_FSET(encrypt_final, ef_encrlen, encr.cd_length); 31390Sstevel@tonic-gate } 31400Sstevel@tonic-gate 31410Sstevel@tonic-gate release_minor: 31426424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk); 31436424Skrishna CRYPTO_SESSION_RELE(sp); 31440Sstevel@tonic-gate crypto_release_minor(cm); 31450Sstevel@tonic-gate 31460Sstevel@tonic-gate if (encr.cd_raw.iov_base != NULL) 31470Sstevel@tonic-gate kmem_free(encr.cd_raw.iov_base, encrlen); 31480Sstevel@tonic-gate 31490Sstevel@tonic-gate if (error != 0) 31500Sstevel@tonic-gate return (error); 31510Sstevel@tonic-gate 31520Sstevel@tonic-gate STRUCT_FSET(encrypt_final, ef_return_value, rv); 31530Sstevel@tonic-gate if (copyout(STRUCT_BUF(encrypt_final), arg, 31540Sstevel@tonic-gate STRUCT_SIZE(encrypt_final)) != 0) { 31550Sstevel@tonic-gate return (EFAULT); 31560Sstevel@tonic-gate } 31570Sstevel@tonic-gate return (0); 31580Sstevel@tonic-gate } 31590Sstevel@tonic-gate 31600Sstevel@tonic-gate /* ARGSUSED */ 31610Sstevel@tonic-gate static int 31620Sstevel@tonic-gate digest_init(dev_t dev, caddr_t arg, int mode, int *rval) 31630Sstevel@tonic-gate { 31640Sstevel@tonic-gate STRUCT_DECL(crypto_digest_init, digest_init); 3165904Smcpowers kcf_provider_desc_t *real_provider = NULL; 31660Sstevel@tonic-gate crypto_session_id_t session_id; 31670Sstevel@tonic-gate crypto_mechanism_t mech; 31680Sstevel@tonic-gate crypto_minor_t *cm; 31696424Skrishna crypto_session_data_t *sp = NULL; 31700Sstevel@tonic-gate crypto_context_t cc; 31710Sstevel@tonic-gate size_t rctl_bytes = 0; 31726424Skrishna boolean_t rctl_chk = B_FALSE; 31730Sstevel@tonic-gate int error = 0; 31740Sstevel@tonic-gate int rv; 31750Sstevel@tonic-gate 31760Sstevel@tonic-gate STRUCT_INIT(digest_init, mode); 31770Sstevel@tonic-gate 31780Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 31790Sstevel@tonic-gate cmn_err(CE_WARN, "digest_init: failed holding minor"); 31800Sstevel@tonic-gate return (ENXIO); 31810Sstevel@tonic-gate } 31820Sstevel@tonic-gate 31830Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(digest_init), 31840Sstevel@tonic-gate STRUCT_SIZE(digest_init)) != 0) { 31850Sstevel@tonic-gate crypto_release_minor(cm); 31860Sstevel@tonic-gate return (EFAULT); 31870Sstevel@tonic-gate } 31880Sstevel@tonic-gate 31890Sstevel@tonic-gate mech.cm_param = NULL; 31900Sstevel@tonic-gate 31910Sstevel@tonic-gate session_id = STRUCT_FGET(digest_init, di_session); 31920Sstevel@tonic-gate 31930Sstevel@tonic-gate if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) { 31946424Skrishna goto out; 31956424Skrishna } 31966424Skrishna 31976424Skrishna if (!copyin_mech(mode, sp, STRUCT_FADDR(digest_init, di_mech), &mech, 31986424Skrishna &rctl_bytes, &rctl_chk, &rv, &error)) { 31990Sstevel@tonic-gate goto out; 32000Sstevel@tonic-gate } 32010Sstevel@tonic-gate 320210444SVladimir.Kotal@Sun.COM if ((rv = kcf_get_hardware_provider(mech.cm_type, NULL, 320310444SVladimir.Kotal@Sun.COM CRYPTO_MECH_INVALID, NULL, CHECK_RESTRICT_FALSE, sp->sd_provider, 320410444SVladimir.Kotal@Sun.COM &real_provider, CRYPTO_FG_DIGEST)) != CRYPTO_SUCCESS) { 32050Sstevel@tonic-gate goto out; 32060Sstevel@tonic-gate } 32070Sstevel@tonic-gate 32080Sstevel@tonic-gate rv = crypto_digest_init_prov(real_provider, 32090Sstevel@tonic-gate sp->sd_provider_session->ps_session, &mech, &cc, NULL); 32100Sstevel@tonic-gate 32110Sstevel@tonic-gate /* 32120Sstevel@tonic-gate * Check if a context already exists. If so, it means it is being 32130Sstevel@tonic-gate * abandoned. So, cancel it to avoid leaking it. 32140Sstevel@tonic-gate */ 32150Sstevel@tonic-gate if (sp->sd_digest_ctx != NULL) 32160Sstevel@tonic-gate CRYPTO_CANCEL_CTX(&sp->sd_digest_ctx); 32170Sstevel@tonic-gate sp->sd_digest_ctx = (rv == CRYPTO_SUCCESS) ? cc : NULL; 32180Sstevel@tonic-gate out: 32196424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk); 32200Sstevel@tonic-gate CRYPTO_SESSION_RELE(sp); 32210Sstevel@tonic-gate crypto_release_minor(cm); 32220Sstevel@tonic-gate 3223904Smcpowers if (real_provider != NULL) 3224904Smcpowers KCF_PROV_REFRELE(real_provider); 3225904Smcpowers 32260Sstevel@tonic-gate if (mech.cm_param != NULL) 32270Sstevel@tonic-gate kmem_free(mech.cm_param, mech.cm_param_len); 32280Sstevel@tonic-gate 32290Sstevel@tonic-gate if (error != 0) 32300Sstevel@tonic-gate return (error); 32310Sstevel@tonic-gate 32320Sstevel@tonic-gate STRUCT_FSET(digest_init, di_return_value, rv); 32330Sstevel@tonic-gate if (copyout(STRUCT_BUF(digest_init), arg, 32340Sstevel@tonic-gate STRUCT_SIZE(digest_init)) != 0) { 32350Sstevel@tonic-gate return (EFAULT); 32360Sstevel@tonic-gate } 32370Sstevel@tonic-gate return (0); 32380Sstevel@tonic-gate } 32390Sstevel@tonic-gate 32400Sstevel@tonic-gate /* ARGSUSED */ 32410Sstevel@tonic-gate static int 32420Sstevel@tonic-gate digest_update(dev_t dev, caddr_t arg, int mode, int *rval) 32430Sstevel@tonic-gate { 32440Sstevel@tonic-gate STRUCT_DECL(crypto_digest_update, digest_update); 32450Sstevel@tonic-gate crypto_session_id_t session_id; 32460Sstevel@tonic-gate crypto_minor_t *cm; 32476424Skrishna crypto_session_data_t *sp = NULL; 32480Sstevel@tonic-gate crypto_data_t data; 32490Sstevel@tonic-gate size_t datalen, need = 0; 32500Sstevel@tonic-gate int error = 0; 32510Sstevel@tonic-gate int rv; 32526424Skrishna boolean_t rctl_chk = B_FALSE; 32530Sstevel@tonic-gate 32540Sstevel@tonic-gate STRUCT_INIT(digest_update, mode); 32550Sstevel@tonic-gate 32560Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 32570Sstevel@tonic-gate cmn_err(CE_WARN, "digest_update: failed holding minor"); 32580Sstevel@tonic-gate return (ENXIO); 32590Sstevel@tonic-gate } 32600Sstevel@tonic-gate 32610Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(digest_update), 32620Sstevel@tonic-gate STRUCT_SIZE(digest_update)) != 0) { 32630Sstevel@tonic-gate crypto_release_minor(cm); 32640Sstevel@tonic-gate return (EFAULT); 32650Sstevel@tonic-gate } 32660Sstevel@tonic-gate 32670Sstevel@tonic-gate data.cd_format = CRYPTO_DATA_RAW; 32680Sstevel@tonic-gate data.cd_raw.iov_base = NULL; 32690Sstevel@tonic-gate 32700Sstevel@tonic-gate datalen = STRUCT_FGET(digest_update, du_datalen); 32710Sstevel@tonic-gate if (datalen > crypto_max_buffer_len) { 32720Sstevel@tonic-gate cmn_err(CE_NOTE, "digest_update: buffer greater than %ld " 32730Sstevel@tonic-gate "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid); 32740Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD; 32750Sstevel@tonic-gate goto release_minor; 32760Sstevel@tonic-gate } 32770Sstevel@tonic-gate 32786424Skrishna session_id = STRUCT_FGET(digest_update, du_session); 32796424Skrishna 32806424Skrishna if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) { 32810Sstevel@tonic-gate goto release_minor; 32820Sstevel@tonic-gate } 32836424Skrishna 32846424Skrishna if ((rv = CRYPTO_BUFFER_CHECK(sp, datalen, rctl_chk)) != 32856424Skrishna CRYPTO_SUCCESS) { 32866424Skrishna goto release_minor; 32876424Skrishna } 32886424Skrishna 32890Sstevel@tonic-gate need = datalen; 32900Sstevel@tonic-gate data.cd_raw.iov_base = kmem_alloc(datalen, KM_SLEEP); 32910Sstevel@tonic-gate data.cd_raw.iov_len = datalen; 32920Sstevel@tonic-gate 32930Sstevel@tonic-gate if (datalen != 0 && copyin(STRUCT_FGETP(digest_update, du_databuf), 32940Sstevel@tonic-gate data.cd_raw.iov_base, datalen) != 0) { 32950Sstevel@tonic-gate error = EFAULT; 32960Sstevel@tonic-gate goto release_minor; 32970Sstevel@tonic-gate } 32980Sstevel@tonic-gate 32990Sstevel@tonic-gate data.cd_offset = 0; 33000Sstevel@tonic-gate data.cd_length = datalen; 33010Sstevel@tonic-gate 33020Sstevel@tonic-gate rv = crypto_digest_update(sp->sd_digest_ctx, &data, NULL); 33030Sstevel@tonic-gate if (rv != CRYPTO_SUCCESS) 33040Sstevel@tonic-gate CRYPTO_CANCEL_CTX(&sp->sd_digest_ctx); 33050Sstevel@tonic-gate 33060Sstevel@tonic-gate release_minor: 33076424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk); 33086424Skrishna CRYPTO_SESSION_RELE(sp); 33090Sstevel@tonic-gate crypto_release_minor(cm); 33100Sstevel@tonic-gate 33110Sstevel@tonic-gate if (data.cd_raw.iov_base != NULL) 33120Sstevel@tonic-gate kmem_free(data.cd_raw.iov_base, datalen); 33130Sstevel@tonic-gate 33140Sstevel@tonic-gate if (error != 0) 33150Sstevel@tonic-gate return (error); 33160Sstevel@tonic-gate 33170Sstevel@tonic-gate STRUCT_FSET(digest_update, du_return_value, rv); 33180Sstevel@tonic-gate if (copyout(STRUCT_BUF(digest_update), arg, 33190Sstevel@tonic-gate STRUCT_SIZE(digest_update)) != 0) { 33200Sstevel@tonic-gate return (EFAULT); 33210Sstevel@tonic-gate } 33220Sstevel@tonic-gate return (0); 33230Sstevel@tonic-gate } 33240Sstevel@tonic-gate 33250Sstevel@tonic-gate /* ARGSUSED */ 33260Sstevel@tonic-gate static int 33270Sstevel@tonic-gate digest_key(dev_t dev, caddr_t arg, int mode, int *rval) 33280Sstevel@tonic-gate { 33290Sstevel@tonic-gate STRUCT_DECL(crypto_digest_key, digest_key); 33300Sstevel@tonic-gate crypto_session_id_t session_id; 33310Sstevel@tonic-gate crypto_key_t key; 33320Sstevel@tonic-gate crypto_minor_t *cm; 33336424Skrishna crypto_session_data_t *sp = NULL; 33340Sstevel@tonic-gate size_t rctl_bytes = 0; 33356424Skrishna boolean_t key_rctl_chk = B_FALSE; 33360Sstevel@tonic-gate int error = 0; 33370Sstevel@tonic-gate int rv; 33380Sstevel@tonic-gate 33390Sstevel@tonic-gate STRUCT_INIT(digest_key, mode); 33400Sstevel@tonic-gate 33410Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 33420Sstevel@tonic-gate cmn_err(CE_WARN, "digest_key: failed holding minor"); 33430Sstevel@tonic-gate return (ENXIO); 33440Sstevel@tonic-gate } 33450Sstevel@tonic-gate 33460Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(digest_key), STRUCT_SIZE(digest_key)) != 0) { 33470Sstevel@tonic-gate crypto_release_minor(cm); 33480Sstevel@tonic-gate return (EFAULT); 33490Sstevel@tonic-gate } 33500Sstevel@tonic-gate 33510Sstevel@tonic-gate bzero(&key, sizeof (crypto_key_t)); 33520Sstevel@tonic-gate 33530Sstevel@tonic-gate session_id = STRUCT_FGET(digest_key, dk_session); 33540Sstevel@tonic-gate 33550Sstevel@tonic-gate if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) { 33566424Skrishna goto out; 33576424Skrishna } 33586424Skrishna 33596424Skrishna if (!copyin_key(mode, sp, STRUCT_FADDR(digest_key, dk_key), &key, 33606424Skrishna &rctl_bytes, &key_rctl_chk, &rv, &error)) { 33610Sstevel@tonic-gate goto out; 33620Sstevel@tonic-gate } 33630Sstevel@tonic-gate 33640Sstevel@tonic-gate rv = crypto_digest_key_prov(sp->sd_digest_ctx, &key, NULL); 33650Sstevel@tonic-gate if (rv != CRYPTO_SUCCESS) 33660Sstevel@tonic-gate CRYPTO_CANCEL_CTX(&sp->sd_digest_ctx); 33670Sstevel@tonic-gate out: 33686424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, key_rctl_chk); 33690Sstevel@tonic-gate CRYPTO_SESSION_RELE(sp); 33700Sstevel@tonic-gate crypto_release_minor(cm); 33710Sstevel@tonic-gate 33720Sstevel@tonic-gate free_crypto_key(&key); 33730Sstevel@tonic-gate 33740Sstevel@tonic-gate if (error != 0) 33750Sstevel@tonic-gate return (error); 33760Sstevel@tonic-gate 33770Sstevel@tonic-gate STRUCT_FSET(digest_key, dk_return_value, rv); 33780Sstevel@tonic-gate if (copyout(STRUCT_BUF(digest_key), arg, 33790Sstevel@tonic-gate STRUCT_SIZE(digest_key)) != 0) { 33800Sstevel@tonic-gate return (EFAULT); 33810Sstevel@tonic-gate } 33820Sstevel@tonic-gate return (0); 33830Sstevel@tonic-gate } 33840Sstevel@tonic-gate 33850Sstevel@tonic-gate /* ARGSUSED */ 33860Sstevel@tonic-gate static int 33870Sstevel@tonic-gate digest_final(dev_t dev, caddr_t arg, int mode, int *rval) 33880Sstevel@tonic-gate { 33890Sstevel@tonic-gate return (common_final(dev, arg, mode, crypto_digest_final)); 33900Sstevel@tonic-gate } 33910Sstevel@tonic-gate 33920Sstevel@tonic-gate /* ARGSUSED */ 33930Sstevel@tonic-gate static int 33940Sstevel@tonic-gate digest(dev_t dev, caddr_t arg, int mode, int *rval) 33950Sstevel@tonic-gate { 33960Sstevel@tonic-gate return (common_digest(dev, arg, mode, crypto_digest_single)); 33970Sstevel@tonic-gate } 33980Sstevel@tonic-gate 33990Sstevel@tonic-gate /* 34000Sstevel@tonic-gate * ASSUMPTION: crypto_digest, crypto_sign, crypto_sign_recover, 34010Sstevel@tonic-gate * and crypto_verify_recover are identical except for field names. 34020Sstevel@tonic-gate */ 34030Sstevel@tonic-gate static int 34040Sstevel@tonic-gate common_digest(dev_t dev, caddr_t arg, int mode, 34050Sstevel@tonic-gate int (*single)(crypto_context_t, crypto_data_t *, crypto_data_t *, 34060Sstevel@tonic-gate crypto_call_req_t *)) 34070Sstevel@tonic-gate { 34080Sstevel@tonic-gate STRUCT_DECL(crypto_digest, crypto_digest); 34090Sstevel@tonic-gate crypto_session_id_t session_id; 34100Sstevel@tonic-gate crypto_minor_t *cm; 34116424Skrishna crypto_session_data_t *sp = NULL; 34120Sstevel@tonic-gate crypto_data_t data, digest; 34130Sstevel@tonic-gate crypto_ctx_t **ctxpp; 34140Sstevel@tonic-gate size_t datalen, digestlen, need = 0; 34150Sstevel@tonic-gate char *digestbuf; 34160Sstevel@tonic-gate int error = 0; 34170Sstevel@tonic-gate int rv; 34186424Skrishna boolean_t rctl_chk = B_FALSE; 34190Sstevel@tonic-gate 34200Sstevel@tonic-gate STRUCT_INIT(crypto_digest, mode); 34210Sstevel@tonic-gate 34220Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 34230Sstevel@tonic-gate cmn_err(CE_WARN, "common_digest: failed holding minor"); 34240Sstevel@tonic-gate return (ENXIO); 34250Sstevel@tonic-gate } 34260Sstevel@tonic-gate 34270Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(crypto_digest), 34280Sstevel@tonic-gate STRUCT_SIZE(crypto_digest)) != 0) { 34290Sstevel@tonic-gate crypto_release_minor(cm); 34300Sstevel@tonic-gate return (EFAULT); 34310Sstevel@tonic-gate } 34320Sstevel@tonic-gate 34330Sstevel@tonic-gate data.cd_raw.iov_base = NULL; 34340Sstevel@tonic-gate digest.cd_raw.iov_base = NULL; 34350Sstevel@tonic-gate 34360Sstevel@tonic-gate datalen = STRUCT_FGET(crypto_digest, cd_datalen); 34370Sstevel@tonic-gate digestlen = STRUCT_FGET(crypto_digest, cd_digestlen); 34380Sstevel@tonic-gate 34390Sstevel@tonic-gate /* 34400Sstevel@tonic-gate * Don't allocate output buffer unless both buffer pointer and 34410Sstevel@tonic-gate * buffer length are not NULL or 0 (length). 34420Sstevel@tonic-gate */ 34430Sstevel@tonic-gate digestbuf = STRUCT_FGETP(crypto_digest, cd_digestbuf); 34440Sstevel@tonic-gate if (digestbuf == NULL || digestlen == 0) { 34450Sstevel@tonic-gate digestlen = 0; 34460Sstevel@tonic-gate } 34470Sstevel@tonic-gate 34480Sstevel@tonic-gate if (datalen > crypto_max_buffer_len || 34490Sstevel@tonic-gate digestlen > crypto_max_buffer_len) { 34500Sstevel@tonic-gate cmn_err(CE_NOTE, "common_digest: buffer greater than %ld " 34510Sstevel@tonic-gate "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid); 34520Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD; 34530Sstevel@tonic-gate goto release_minor; 34540Sstevel@tonic-gate } 34550Sstevel@tonic-gate 34566424Skrishna session_id = STRUCT_FGET(crypto_digest, cd_session); 34576424Skrishna 34586424Skrishna if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) { 34596424Skrishna goto release_minor; 34606424Skrishna } 34616424Skrishna 34620Sstevel@tonic-gate need = datalen + digestlen; 34636424Skrishna if ((rv = CRYPTO_BUFFER_CHECK(sp, need, rctl_chk)) != 34646424Skrishna CRYPTO_SUCCESS) { 34650Sstevel@tonic-gate need = 0; 34660Sstevel@tonic-gate goto release_minor; 34670Sstevel@tonic-gate } 34680Sstevel@tonic-gate 34690Sstevel@tonic-gate INIT_RAW_CRYPTO_DATA(data, datalen); 34700Sstevel@tonic-gate 34710Sstevel@tonic-gate if (datalen != 0 && copyin(STRUCT_FGETP(crypto_digest, cd_databuf), 34720Sstevel@tonic-gate data.cd_raw.iov_base, datalen) != 0) { 34730Sstevel@tonic-gate error = EFAULT; 34740Sstevel@tonic-gate goto release_minor; 34750Sstevel@tonic-gate } 34760Sstevel@tonic-gate 34770Sstevel@tonic-gate INIT_RAW_CRYPTO_DATA(digest, digestlen); 34780Sstevel@tonic-gate 34790Sstevel@tonic-gate ASSERT(single == crypto_digest_single || 34800Sstevel@tonic-gate single == crypto_sign_single || 34810Sstevel@tonic-gate single == crypto_verify_recover_single || 34820Sstevel@tonic-gate single == crypto_sign_recover_single); 34830Sstevel@tonic-gate 34840Sstevel@tonic-gate if (single == crypto_digest_single) { 34850Sstevel@tonic-gate ctxpp = &sp->sd_digest_ctx; 34860Sstevel@tonic-gate } else if (single == crypto_sign_single) { 34870Sstevel@tonic-gate ctxpp = &sp->sd_sign_ctx; 34880Sstevel@tonic-gate } else if (single == crypto_verify_recover_single) { 34890Sstevel@tonic-gate ctxpp = &sp->sd_verify_recover_ctx; 34900Sstevel@tonic-gate } else { 34910Sstevel@tonic-gate ctxpp = &sp->sd_sign_recover_ctx; 34920Sstevel@tonic-gate } 34930Sstevel@tonic-gate rv = (single)(*ctxpp, &data, &digest, NULL); 34940Sstevel@tonic-gate if (KCF_CONTEXT_DONE(rv)) 34950Sstevel@tonic-gate *ctxpp = NULL; 34960Sstevel@tonic-gate 34970Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) { 34980Sstevel@tonic-gate ASSERT(digest.cd_length <= digestlen); 34990Sstevel@tonic-gate if (digest.cd_length != 0 && copyout(digest.cd_raw.iov_base, 35000Sstevel@tonic-gate digestbuf, digest.cd_length) != 0) { 35010Sstevel@tonic-gate error = EFAULT; 35020Sstevel@tonic-gate goto release_minor; 35030Sstevel@tonic-gate } 35040Sstevel@tonic-gate STRUCT_FSET(crypto_digest, cd_digestlen, digest.cd_length); 35050Sstevel@tonic-gate } 35060Sstevel@tonic-gate 35070Sstevel@tonic-gate if (rv == CRYPTO_BUFFER_TOO_SMALL) { 35080Sstevel@tonic-gate /* 35090Sstevel@tonic-gate * The providers return CRYPTO_BUFFER_TOO_SMALL even for case 1 35100Sstevel@tonic-gate * of section 11.2 of the pkcs11 spec. We catch it here and 35110Sstevel@tonic-gate * provide the correct pkcs11 return value. 35120Sstevel@tonic-gate */ 35130Sstevel@tonic-gate if (STRUCT_FGETP(crypto_digest, cd_digestbuf) == NULL) 35140Sstevel@tonic-gate rv = CRYPTO_SUCCESS; 35150Sstevel@tonic-gate STRUCT_FSET(crypto_digest, cd_digestlen, digest.cd_length); 35160Sstevel@tonic-gate } 35170Sstevel@tonic-gate 35180Sstevel@tonic-gate release_minor: 35196424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk); 35206424Skrishna CRYPTO_SESSION_RELE(sp); 35210Sstevel@tonic-gate crypto_release_minor(cm); 35220Sstevel@tonic-gate 35230Sstevel@tonic-gate if (data.cd_raw.iov_base != NULL) 35240Sstevel@tonic-gate kmem_free(data.cd_raw.iov_base, datalen); 35250Sstevel@tonic-gate 35260Sstevel@tonic-gate if (digest.cd_raw.iov_base != NULL) 35270Sstevel@tonic-gate kmem_free(digest.cd_raw.iov_base, digestlen); 35280Sstevel@tonic-gate 35290Sstevel@tonic-gate if (error != 0) 35300Sstevel@tonic-gate return (error); 35310Sstevel@tonic-gate 35320Sstevel@tonic-gate STRUCT_FSET(crypto_digest, cd_return_value, rv); 35330Sstevel@tonic-gate if (copyout(STRUCT_BUF(crypto_digest), arg, 35340Sstevel@tonic-gate STRUCT_SIZE(crypto_digest)) != 0) { 35350Sstevel@tonic-gate return (EFAULT); 35360Sstevel@tonic-gate } 35370Sstevel@tonic-gate return (0); 35380Sstevel@tonic-gate } 35390Sstevel@tonic-gate 35400Sstevel@tonic-gate /* 35410Sstevel@tonic-gate * A helper function that does what the name suggests. 35420Sstevel@tonic-gate * Returns 0 on success and non-zero otherwise. 35430Sstevel@tonic-gate * On failure, out_pin is set to 0. 35440Sstevel@tonic-gate */ 35450Sstevel@tonic-gate int 35460Sstevel@tonic-gate get_pin_and_session_ptr(char *in_pin, char **out_pin, size_t pin_len, 35470Sstevel@tonic-gate crypto_minor_t *cm, crypto_session_id_t sid, crypto_session_data_t **sp, 35480Sstevel@tonic-gate int *rv, int *error) 35490Sstevel@tonic-gate { 35500Sstevel@tonic-gate char *tmp_pin = NULL; 35510Sstevel@tonic-gate int tmp_error = 0, tmp_rv = 0; 35520Sstevel@tonic-gate 35530Sstevel@tonic-gate if (pin_len > KCF_MAX_PIN_LEN) { 35540Sstevel@tonic-gate tmp_rv = CRYPTO_PIN_LEN_RANGE; 35550Sstevel@tonic-gate goto out; 35560Sstevel@tonic-gate } 35570Sstevel@tonic-gate tmp_pin = kmem_alloc(pin_len, KM_SLEEP); 35580Sstevel@tonic-gate 35590Sstevel@tonic-gate if (pin_len != 0 && copyin(in_pin, tmp_pin, pin_len) != 0) { 35600Sstevel@tonic-gate tmp_error = EFAULT; 35610Sstevel@tonic-gate goto out; 35620Sstevel@tonic-gate } 35630Sstevel@tonic-gate 35640Sstevel@tonic-gate (void) get_session_ptr(sid, cm, sp, &tmp_error, &tmp_rv); 35650Sstevel@tonic-gate out: 35660Sstevel@tonic-gate *out_pin = tmp_pin; 35670Sstevel@tonic-gate *rv = tmp_rv; 35680Sstevel@tonic-gate *error = tmp_error; 35690Sstevel@tonic-gate return (tmp_rv | tmp_error); 35700Sstevel@tonic-gate } 35710Sstevel@tonic-gate 35720Sstevel@tonic-gate /* ARGSUSED */ 35730Sstevel@tonic-gate static int 35740Sstevel@tonic-gate set_pin(dev_t dev, caddr_t arg, int mode, int *rval) 35750Sstevel@tonic-gate { 35760Sstevel@tonic-gate STRUCT_DECL(crypto_set_pin, set_pin); 35770Sstevel@tonic-gate kcf_provider_desc_t *real_provider; 35780Sstevel@tonic-gate kcf_req_params_t params; 35790Sstevel@tonic-gate crypto_minor_t *cm; 35800Sstevel@tonic-gate crypto_session_data_t *sp; 35810Sstevel@tonic-gate char *old_pin = NULL; 35820Sstevel@tonic-gate char *new_pin = NULL; 35830Sstevel@tonic-gate size_t old_pin_len; 35840Sstevel@tonic-gate size_t new_pin_len; 35850Sstevel@tonic-gate int error = 0; 35860Sstevel@tonic-gate int rv; 35870Sstevel@tonic-gate 35880Sstevel@tonic-gate STRUCT_INIT(set_pin, mode); 35890Sstevel@tonic-gate 35900Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 35910Sstevel@tonic-gate cmn_err(CE_WARN, "set_pin: failed holding minor"); 35920Sstevel@tonic-gate return (ENXIO); 35930Sstevel@tonic-gate } 35940Sstevel@tonic-gate 35950Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(set_pin), 35960Sstevel@tonic-gate STRUCT_SIZE(set_pin)) != 0) { 35970Sstevel@tonic-gate crypto_release_minor(cm); 35980Sstevel@tonic-gate return (EFAULT); 35990Sstevel@tonic-gate } 36000Sstevel@tonic-gate 36010Sstevel@tonic-gate old_pin_len = STRUCT_FGET(set_pin, sp_old_len); 36020Sstevel@tonic-gate 36030Sstevel@tonic-gate if (get_pin_and_session_ptr(STRUCT_FGETP(set_pin, sp_old_pin), 36040Sstevel@tonic-gate &old_pin, old_pin_len, cm, STRUCT_FGET(set_pin, sp_session), 36050Sstevel@tonic-gate &sp, &rv, &error) != 0) 36060Sstevel@tonic-gate goto release_minor; 36070Sstevel@tonic-gate 36080Sstevel@tonic-gate new_pin_len = STRUCT_FGET(set_pin, sp_new_len); 36090Sstevel@tonic-gate if (new_pin_len > KCF_MAX_PIN_LEN) { 36100Sstevel@tonic-gate rv = CRYPTO_PIN_LEN_RANGE; 36110Sstevel@tonic-gate goto out; 36120Sstevel@tonic-gate } 36130Sstevel@tonic-gate new_pin = kmem_alloc(new_pin_len, KM_SLEEP); 36140Sstevel@tonic-gate 36150Sstevel@tonic-gate if (new_pin_len != 0 && copyin(STRUCT_FGETP(set_pin, sp_new_pin), 36160Sstevel@tonic-gate new_pin, new_pin_len) != 0) { 36170Sstevel@tonic-gate error = EFAULT; 36180Sstevel@tonic-gate goto out; 36190Sstevel@tonic-gate } 36200Sstevel@tonic-gate 36210Sstevel@tonic-gate if ((rv = kcf_get_hardware_provider_nomech( 36220Sstevel@tonic-gate CRYPTO_OPS_OFFSET(provider_ops), CRYPTO_PROVIDER_OFFSET(set_pin), 3623904Smcpowers CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider)) 3624904Smcpowers != CRYPTO_SUCCESS) { 36250Sstevel@tonic-gate goto out; 36260Sstevel@tonic-gate } 36270Sstevel@tonic-gate 36280Sstevel@tonic-gate KCF_WRAP_PROVMGMT_OPS_PARAMS(¶ms, KCF_OP_MGMT_SETPIN, 36290Sstevel@tonic-gate sp->sd_provider_session->ps_session, old_pin, old_pin_len, 36300Sstevel@tonic-gate new_pin, new_pin_len, NULL, NULL, real_provider); 36310Sstevel@tonic-gate 36320Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE); 3633904Smcpowers KCF_PROV_REFRELE(real_provider); 36340Sstevel@tonic-gate 36350Sstevel@tonic-gate out: 36360Sstevel@tonic-gate CRYPTO_SESSION_RELE(sp); 36370Sstevel@tonic-gate 36380Sstevel@tonic-gate release_minor: 36390Sstevel@tonic-gate crypto_release_minor(cm); 36400Sstevel@tonic-gate 36410Sstevel@tonic-gate if (old_pin != NULL) { 36420Sstevel@tonic-gate bzero(old_pin, old_pin_len); 36430Sstevel@tonic-gate kmem_free(old_pin, old_pin_len); 36440Sstevel@tonic-gate } 36450Sstevel@tonic-gate 36460Sstevel@tonic-gate if (new_pin != NULL) { 36470Sstevel@tonic-gate bzero(new_pin, new_pin_len); 36480Sstevel@tonic-gate kmem_free(new_pin, new_pin_len); 36490Sstevel@tonic-gate } 36500Sstevel@tonic-gate 36510Sstevel@tonic-gate if (error != 0) 36520Sstevel@tonic-gate return (error); 36530Sstevel@tonic-gate 36540Sstevel@tonic-gate STRUCT_FSET(set_pin, sp_return_value, rv); 36550Sstevel@tonic-gate if (copyout(STRUCT_BUF(set_pin), arg, STRUCT_SIZE(set_pin)) != 0) { 36560Sstevel@tonic-gate return (EFAULT); 36570Sstevel@tonic-gate } 36580Sstevel@tonic-gate return (0); 36590Sstevel@tonic-gate } 36600Sstevel@tonic-gate 36610Sstevel@tonic-gate /* ARGSUSED */ 36620Sstevel@tonic-gate static int 36630Sstevel@tonic-gate login(dev_t dev, caddr_t arg, int mode, int *rval) 36640Sstevel@tonic-gate { 36650Sstevel@tonic-gate STRUCT_DECL(crypto_login, login); 36660Sstevel@tonic-gate kcf_provider_desc_t *real_provider; 36670Sstevel@tonic-gate kcf_req_params_t params; 36680Sstevel@tonic-gate crypto_minor_t *cm; 36690Sstevel@tonic-gate crypto_session_data_t *sp; 36700Sstevel@tonic-gate size_t pin_len; 36710Sstevel@tonic-gate char *pin; 36720Sstevel@tonic-gate uint_t user_type; 36730Sstevel@tonic-gate int error = 0; 36740Sstevel@tonic-gate int rv; 36750Sstevel@tonic-gate 36760Sstevel@tonic-gate STRUCT_INIT(login, mode); 36770Sstevel@tonic-gate 36780Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 36790Sstevel@tonic-gate cmn_err(CE_WARN, "login: failed holding minor"); 36800Sstevel@tonic-gate return (ENXIO); 36810Sstevel@tonic-gate } 36820Sstevel@tonic-gate 36830Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(login), STRUCT_SIZE(login)) != 0) { 36840Sstevel@tonic-gate crypto_release_minor(cm); 36850Sstevel@tonic-gate return (EFAULT); 36860Sstevel@tonic-gate } 36870Sstevel@tonic-gate 36880Sstevel@tonic-gate user_type = STRUCT_FGET(login, co_user_type); 36890Sstevel@tonic-gate 36900Sstevel@tonic-gate pin_len = STRUCT_FGET(login, co_pin_len); 36910Sstevel@tonic-gate 36920Sstevel@tonic-gate if (get_pin_and_session_ptr(STRUCT_FGETP(login, co_pin), 36930Sstevel@tonic-gate &pin, pin_len, cm, STRUCT_FGET(login, co_session), 36940Sstevel@tonic-gate &sp, &rv, &error) != 0) { 36950Sstevel@tonic-gate if (rv == CRYPTO_PIN_LEN_RANGE) 36960Sstevel@tonic-gate rv = CRYPTO_PIN_INCORRECT; 36970Sstevel@tonic-gate goto release_minor; 36980Sstevel@tonic-gate } 36990Sstevel@tonic-gate 37000Sstevel@tonic-gate if ((rv = kcf_get_hardware_provider_nomech( 37010Sstevel@tonic-gate CRYPTO_OPS_OFFSET(session_ops), 37020Sstevel@tonic-gate CRYPTO_SESSION_OFFSET(session_login), 3703904Smcpowers CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider)) 3704904Smcpowers != CRYPTO_SUCCESS) { 37050Sstevel@tonic-gate goto out; 37060Sstevel@tonic-gate } 37070Sstevel@tonic-gate 37080Sstevel@tonic-gate KCF_WRAP_SESSION_OPS_PARAMS(¶ms, KCF_OP_SESSION_LOGIN, NULL, 37090Sstevel@tonic-gate sp->sd_provider_session->ps_session, user_type, pin, pin_len, 37100Sstevel@tonic-gate real_provider); 37110Sstevel@tonic-gate 37120Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE); 3713904Smcpowers KCF_PROV_REFRELE(real_provider); 37140Sstevel@tonic-gate 37150Sstevel@tonic-gate out: 37160Sstevel@tonic-gate CRYPTO_SESSION_RELE(sp); 37170Sstevel@tonic-gate 37180Sstevel@tonic-gate release_minor: 37190Sstevel@tonic-gate crypto_release_minor(cm); 37200Sstevel@tonic-gate 37210Sstevel@tonic-gate if (pin != NULL) { 37220Sstevel@tonic-gate bzero(pin, pin_len); 37230Sstevel@tonic-gate kmem_free(pin, pin_len); 37240Sstevel@tonic-gate } 37250Sstevel@tonic-gate 37260Sstevel@tonic-gate if (error != 0) 37270Sstevel@tonic-gate return (error); 37280Sstevel@tonic-gate 37290Sstevel@tonic-gate STRUCT_FSET(login, co_return_value, rv); 37300Sstevel@tonic-gate if (copyout(STRUCT_BUF(login), arg, STRUCT_SIZE(login)) != 0) { 37310Sstevel@tonic-gate return (EFAULT); 37320Sstevel@tonic-gate } 37330Sstevel@tonic-gate return (0); 37340Sstevel@tonic-gate } 37350Sstevel@tonic-gate 37360Sstevel@tonic-gate /* ARGSUSED */ 37370Sstevel@tonic-gate static int 37380Sstevel@tonic-gate logout(dev_t dev, caddr_t arg, int mode, int *rval) 37390Sstevel@tonic-gate { 37400Sstevel@tonic-gate crypto_logout_t logout; 37410Sstevel@tonic-gate kcf_provider_desc_t *real_provider; 37420Sstevel@tonic-gate kcf_req_params_t params; 37430Sstevel@tonic-gate crypto_minor_t *cm; 37440Sstevel@tonic-gate crypto_session_data_t *sp; 37450Sstevel@tonic-gate int error = 0; 37460Sstevel@tonic-gate int rv; 37470Sstevel@tonic-gate 37480Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 37490Sstevel@tonic-gate cmn_err(CE_WARN, "logout: failed holding minor"); 37500Sstevel@tonic-gate return (ENXIO); 37510Sstevel@tonic-gate } 37520Sstevel@tonic-gate 37530Sstevel@tonic-gate if (copyin(arg, &logout, sizeof (logout)) != 0) { 37540Sstevel@tonic-gate crypto_release_minor(cm); 37550Sstevel@tonic-gate return (EFAULT); 37560Sstevel@tonic-gate } 37570Sstevel@tonic-gate 37580Sstevel@tonic-gate if (!get_session_ptr(logout.cl_session, cm, &sp, &error, &rv)) { 37590Sstevel@tonic-gate goto release_minor; 37600Sstevel@tonic-gate } 37610Sstevel@tonic-gate 37620Sstevel@tonic-gate if ((rv = kcf_get_hardware_provider_nomech( 37630Sstevel@tonic-gate CRYPTO_OPS_OFFSET(session_ops), 3764904Smcpowers CRYPTO_SESSION_OFFSET(session_logout), CHECK_RESTRICT_FALSE, 37650Sstevel@tonic-gate sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) { 37660Sstevel@tonic-gate goto out; 37670Sstevel@tonic-gate } 37680Sstevel@tonic-gate 37690Sstevel@tonic-gate KCF_WRAP_SESSION_OPS_PARAMS(¶ms, KCF_OP_SESSION_LOGOUT, NULL, 37700Sstevel@tonic-gate sp->sd_provider_session->ps_session, 0, NULL, 0, real_provider); 37710Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE); 3772904Smcpowers KCF_PROV_REFRELE(real_provider); 37730Sstevel@tonic-gate 37740Sstevel@tonic-gate out: 37750Sstevel@tonic-gate CRYPTO_SESSION_RELE(sp); 37760Sstevel@tonic-gate 37770Sstevel@tonic-gate release_minor: 37780Sstevel@tonic-gate crypto_release_minor(cm); 37790Sstevel@tonic-gate 37800Sstevel@tonic-gate if (error != 0) 37810Sstevel@tonic-gate return (error); 37820Sstevel@tonic-gate 37830Sstevel@tonic-gate logout.cl_return_value = rv; 37840Sstevel@tonic-gate if (copyout(&logout, arg, sizeof (logout)) != 0) { 37850Sstevel@tonic-gate return (EFAULT); 37860Sstevel@tonic-gate } 37870Sstevel@tonic-gate return (0); 37880Sstevel@tonic-gate } 37890Sstevel@tonic-gate 37900Sstevel@tonic-gate /* ARGSUSED */ 37910Sstevel@tonic-gate static int 37920Sstevel@tonic-gate sign_init(dev_t dev, caddr_t arg, int mode, int *rval) 37930Sstevel@tonic-gate { 37940Sstevel@tonic-gate return (sign_verify_init(dev, arg, mode, crypto_sign_init_prov)); 37950Sstevel@tonic-gate } 37960Sstevel@tonic-gate 37970Sstevel@tonic-gate /* ARGSUSED */ 37980Sstevel@tonic-gate static int 37990Sstevel@tonic-gate sign_recover_init(dev_t dev, caddr_t arg, int mode, int *rval) 38000Sstevel@tonic-gate { 38010Sstevel@tonic-gate return (sign_verify_init(dev, arg, mode, 38020Sstevel@tonic-gate crypto_sign_recover_init_prov)); 38030Sstevel@tonic-gate } 38040Sstevel@tonic-gate 38050Sstevel@tonic-gate /* ARGSUSED */ 38060Sstevel@tonic-gate static int 38070Sstevel@tonic-gate verify_init(dev_t dev, caddr_t arg, int mode, int *rval) 38080Sstevel@tonic-gate { 38090Sstevel@tonic-gate return (sign_verify_init(dev, arg, mode, crypto_verify_init_prov)); 38100Sstevel@tonic-gate } 38110Sstevel@tonic-gate 38120Sstevel@tonic-gate /* ARGSUSED */ 38130Sstevel@tonic-gate static int 38140Sstevel@tonic-gate verify_recover_init(dev_t dev, caddr_t arg, int mode, int *rval) 38150Sstevel@tonic-gate { 38160Sstevel@tonic-gate return (sign_verify_init(dev, arg, mode, 38170Sstevel@tonic-gate crypto_verify_recover_init_prov)); 38180Sstevel@tonic-gate } 38190Sstevel@tonic-gate 38200Sstevel@tonic-gate /* 38210Sstevel@tonic-gate * ASSUMPTION: crypto_sign_init, crypto_verify_init, crypto_sign_recover_init, 38220Sstevel@tonic-gate * and crypto_verify_recover_init structures are identical 38230Sstevel@tonic-gate * except for field names. 38240Sstevel@tonic-gate */ 38250Sstevel@tonic-gate static int 38260Sstevel@tonic-gate sign_verify_init(dev_t dev, caddr_t arg, int mode, 3827904Smcpowers int (*init)(crypto_provider_t, crypto_session_id_t, 38280Sstevel@tonic-gate crypto_mechanism_t *, crypto_key_t *, crypto_ctx_template_t, 38290Sstevel@tonic-gate crypto_context_t *, crypto_call_req_t *)) 38300Sstevel@tonic-gate { 38310Sstevel@tonic-gate STRUCT_DECL(crypto_sign_init, sign_init); 3832904Smcpowers kcf_provider_desc_t *real_provider = NULL; 38330Sstevel@tonic-gate crypto_session_id_t session_id; 38340Sstevel@tonic-gate crypto_mechanism_t mech; 38350Sstevel@tonic-gate crypto_key_t key; 38360Sstevel@tonic-gate crypto_minor_t *cm; 38376424Skrishna crypto_session_data_t *sp = NULL; 38380Sstevel@tonic-gate crypto_context_t cc; 38390Sstevel@tonic-gate crypto_ctx_t **ctxpp; 38400Sstevel@tonic-gate size_t mech_rctl_bytes = 0; 38416424Skrishna boolean_t mech_rctl_chk = B_FALSE; 38420Sstevel@tonic-gate size_t key_rctl_bytes = 0; 38436424Skrishna boolean_t key_rctl_chk = B_FALSE; 38440Sstevel@tonic-gate int error = 0; 38450Sstevel@tonic-gate int rv; 3846904Smcpowers boolean_t allocated_by_crypto_module = B_FALSE; 38471808Smcpowers crypto_func_group_t fg; 38480Sstevel@tonic-gate 38490Sstevel@tonic-gate STRUCT_INIT(sign_init, mode); 38500Sstevel@tonic-gate 38510Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 38520Sstevel@tonic-gate cmn_err(CE_WARN, "sign_verify_init: failed holding minor"); 38530Sstevel@tonic-gate return (ENXIO); 38540Sstevel@tonic-gate } 38550Sstevel@tonic-gate 38560Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(sign_init), STRUCT_SIZE(sign_init)) != 0) { 38570Sstevel@tonic-gate crypto_release_minor(cm); 38580Sstevel@tonic-gate return (EFAULT); 38590Sstevel@tonic-gate } 38600Sstevel@tonic-gate 38610Sstevel@tonic-gate mech.cm_param = NULL; 38620Sstevel@tonic-gate bzero(&key, sizeof (key)); 38630Sstevel@tonic-gate 38640Sstevel@tonic-gate session_id = STRUCT_FGET(sign_init, si_session); 38650Sstevel@tonic-gate 38660Sstevel@tonic-gate if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) { 38676424Skrishna goto out; 38680Sstevel@tonic-gate } 38690Sstevel@tonic-gate 3870904Smcpowers bcopy(STRUCT_FADDR(sign_init, si_mech), &mech.cm_type, 3871904Smcpowers sizeof (crypto_mech_type_t)); 38720Sstevel@tonic-gate 38730Sstevel@tonic-gate ASSERT(init == crypto_sign_init_prov || 38740Sstevel@tonic-gate init == crypto_verify_init_prov || 38750Sstevel@tonic-gate init == crypto_sign_recover_init_prov || 38760Sstevel@tonic-gate init == crypto_verify_recover_init_prov); 38770Sstevel@tonic-gate 38780Sstevel@tonic-gate if (init == crypto_sign_init_prov) { 38791808Smcpowers fg = CRYPTO_FG_SIGN; 38800Sstevel@tonic-gate ctxpp = &sp->sd_sign_ctx; 38810Sstevel@tonic-gate } else if (init == crypto_verify_init_prov) { 38821808Smcpowers fg = CRYPTO_FG_VERIFY; 38830Sstevel@tonic-gate ctxpp = &sp->sd_verify_ctx; 38840Sstevel@tonic-gate } else if (init == crypto_sign_recover_init_prov) { 38851808Smcpowers fg = CRYPTO_FG_SIGN_RECOVER; 38860Sstevel@tonic-gate ctxpp = &sp->sd_sign_recover_ctx; 38870Sstevel@tonic-gate } else { 38881808Smcpowers fg = CRYPTO_FG_VERIFY_RECOVER; 38890Sstevel@tonic-gate ctxpp = &sp->sd_verify_recover_ctx; 38900Sstevel@tonic-gate } 38910Sstevel@tonic-gate 389210444SVladimir.Kotal@Sun.COM /* We need the key length for provider selection so copy it in now. */ 389310444SVladimir.Kotal@Sun.COM if (!copyin_key(mode, sp, STRUCT_FADDR(sign_init, si_key), &key, 389410444SVladimir.Kotal@Sun.COM &key_rctl_bytes, &key_rctl_chk, &rv, &error)) { 389510444SVladimir.Kotal@Sun.COM goto out; 389610444SVladimir.Kotal@Sun.COM } 389710444SVladimir.Kotal@Sun.COM 389810444SVladimir.Kotal@Sun.COM if ((rv = kcf_get_hardware_provider(mech.cm_type, &key, 389910444SVladimir.Kotal@Sun.COM CRYPTO_MECH_INVALID, NULL, CHECK_RESTRICT_FALSE, sp->sd_provider, 390010444SVladimir.Kotal@Sun.COM &real_provider, fg)) != CRYPTO_SUCCESS) { 39010Sstevel@tonic-gate goto out; 39020Sstevel@tonic-gate } 39030Sstevel@tonic-gate 3904904Smcpowers rv = crypto_provider_copyin_mech_param(real_provider, 3905904Smcpowers STRUCT_FADDR(sign_init, si_mech), &mech, mode, &error); 3906904Smcpowers 3907904Smcpowers if (rv == CRYPTO_NOT_SUPPORTED) { 3908904Smcpowers allocated_by_crypto_module = B_TRUE; 39096424Skrishna if (!copyin_mech(mode, sp, STRUCT_FADDR(sign_init, si_mech), 39106424Skrishna &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) { 3911904Smcpowers goto out; 3912904Smcpowers } 3913904Smcpowers } else { 3914904Smcpowers if (rv != CRYPTO_SUCCESS) 3915904Smcpowers goto out; 3916904Smcpowers } 3917904Smcpowers 39180Sstevel@tonic-gate rv = (init)(real_provider, sp->sd_provider_session->ps_session, 39190Sstevel@tonic-gate &mech, &key, NULL, &cc, NULL); 39200Sstevel@tonic-gate 39210Sstevel@tonic-gate /* 39220Sstevel@tonic-gate * Check if a context already exists. If so, it means it is being 39230Sstevel@tonic-gate * abandoned. So, cancel it to avoid leaking it. 39240Sstevel@tonic-gate */ 39250Sstevel@tonic-gate if (*ctxpp != NULL) 39260Sstevel@tonic-gate CRYPTO_CANCEL_CTX(ctxpp); 39270Sstevel@tonic-gate *ctxpp = (rv == CRYPTO_SUCCESS) ? cc : NULL; 39280Sstevel@tonic-gate 39290Sstevel@tonic-gate out: 39306424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk); 39316424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk); 39320Sstevel@tonic-gate CRYPTO_SESSION_RELE(sp); 39330Sstevel@tonic-gate crypto_release_minor(cm); 39340Sstevel@tonic-gate 3935904Smcpowers if (real_provider != NULL) { 3936904Smcpowers crypto_free_mech(real_provider, 3937904Smcpowers allocated_by_crypto_module, &mech); 3938904Smcpowers KCF_PROV_REFRELE(real_provider); 3939904Smcpowers } 39400Sstevel@tonic-gate 39410Sstevel@tonic-gate free_crypto_key(&key); 39420Sstevel@tonic-gate 39430Sstevel@tonic-gate if (error != 0) 39440Sstevel@tonic-gate return (error); 39450Sstevel@tonic-gate 39460Sstevel@tonic-gate STRUCT_FSET(sign_init, si_return_value, rv); 39470Sstevel@tonic-gate if (copyout(STRUCT_BUF(sign_init), arg, STRUCT_SIZE(sign_init)) != 0) { 39480Sstevel@tonic-gate return (EFAULT); 39490Sstevel@tonic-gate } 39500Sstevel@tonic-gate return (0); 39510Sstevel@tonic-gate } 39520Sstevel@tonic-gate 39530Sstevel@tonic-gate /* ARGSUSED */ 39540Sstevel@tonic-gate static int 39550Sstevel@tonic-gate sign(dev_t dev, caddr_t arg, int mode, int *rval) 39560Sstevel@tonic-gate { 39570Sstevel@tonic-gate return (common_digest(dev, arg, mode, crypto_sign_single)); 39580Sstevel@tonic-gate } 39590Sstevel@tonic-gate 39600Sstevel@tonic-gate /* ARGSUSED */ 39610Sstevel@tonic-gate static int 39620Sstevel@tonic-gate sign_recover(dev_t dev, caddr_t arg, int mode, int *rval) 39630Sstevel@tonic-gate { 39640Sstevel@tonic-gate return (common_digest(dev, arg, mode, crypto_sign_recover_single)); 39650Sstevel@tonic-gate } 39660Sstevel@tonic-gate 39670Sstevel@tonic-gate /* ARGSUSED */ 39680Sstevel@tonic-gate static int 39690Sstevel@tonic-gate verify(dev_t dev, caddr_t arg, int mode, int *rval) 39700Sstevel@tonic-gate { 39710Sstevel@tonic-gate STRUCT_DECL(crypto_verify, verify); 39720Sstevel@tonic-gate crypto_session_id_t session_id; 39730Sstevel@tonic-gate crypto_minor_t *cm; 39746424Skrishna crypto_session_data_t *sp = NULL; 39750Sstevel@tonic-gate crypto_data_t data, sign; 39760Sstevel@tonic-gate size_t datalen, signlen, need = 0; 39770Sstevel@tonic-gate int error = 0; 39780Sstevel@tonic-gate int rv; 39796424Skrishna boolean_t rctl_chk = B_FALSE; 39800Sstevel@tonic-gate 39810Sstevel@tonic-gate STRUCT_INIT(verify, mode); 39820Sstevel@tonic-gate 39830Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 39840Sstevel@tonic-gate cmn_err(CE_WARN, "verify: failed holding minor"); 39850Sstevel@tonic-gate return (ENXIO); 39860Sstevel@tonic-gate } 39870Sstevel@tonic-gate 39880Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(verify), STRUCT_SIZE(verify)) != 0) { 39890Sstevel@tonic-gate crypto_release_minor(cm); 39900Sstevel@tonic-gate return (EFAULT); 39910Sstevel@tonic-gate } 39920Sstevel@tonic-gate 39930Sstevel@tonic-gate data.cd_raw.iov_base = NULL; 39940Sstevel@tonic-gate sign.cd_raw.iov_base = NULL; 39950Sstevel@tonic-gate 39960Sstevel@tonic-gate datalen = STRUCT_FGET(verify, cv_datalen); 39970Sstevel@tonic-gate signlen = STRUCT_FGET(verify, cv_signlen); 39980Sstevel@tonic-gate if (datalen > crypto_max_buffer_len || 39990Sstevel@tonic-gate signlen > crypto_max_buffer_len) { 40000Sstevel@tonic-gate cmn_err(CE_NOTE, "verify: buffer greater than %ld bytes, " 40010Sstevel@tonic-gate "pid = %d", crypto_max_buffer_len, curproc->p_pid); 40020Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD; 40030Sstevel@tonic-gate goto release_minor; 40040Sstevel@tonic-gate } 40050Sstevel@tonic-gate 40066424Skrishna session_id = STRUCT_FGET(verify, cv_session); 40076424Skrishna 40086424Skrishna if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) { 40096424Skrishna goto release_minor; 40106424Skrishna } 40116424Skrishna 40120Sstevel@tonic-gate need = datalen + signlen; 40136424Skrishna if ((rv = CRYPTO_BUFFER_CHECK(sp, need, rctl_chk)) != 40146424Skrishna CRYPTO_SUCCESS) { 40150Sstevel@tonic-gate need = 0; 40160Sstevel@tonic-gate goto release_minor; 40170Sstevel@tonic-gate } 40180Sstevel@tonic-gate 40190Sstevel@tonic-gate INIT_RAW_CRYPTO_DATA(data, datalen); 40200Sstevel@tonic-gate INIT_RAW_CRYPTO_DATA(sign, signlen); 40210Sstevel@tonic-gate 40220Sstevel@tonic-gate if (datalen != 0 && copyin(STRUCT_FGETP(verify, cv_databuf), 40230Sstevel@tonic-gate data.cd_raw.iov_base, datalen) != 0) { 40240Sstevel@tonic-gate error = EFAULT; 40250Sstevel@tonic-gate goto release_minor; 40260Sstevel@tonic-gate } 40270Sstevel@tonic-gate 40280Sstevel@tonic-gate if (signlen != 0 && copyin(STRUCT_FGETP(verify, cv_signbuf), 40290Sstevel@tonic-gate sign.cd_raw.iov_base, signlen) != 0) { 40300Sstevel@tonic-gate error = EFAULT; 40310Sstevel@tonic-gate goto release_minor; 40320Sstevel@tonic-gate } 40330Sstevel@tonic-gate 40340Sstevel@tonic-gate rv = crypto_verify_single(sp->sd_verify_ctx, &data, &sign, NULL); 40350Sstevel@tonic-gate if (KCF_CONTEXT_DONE(rv)) 40360Sstevel@tonic-gate sp->sd_verify_ctx = NULL; 40370Sstevel@tonic-gate 40386424Skrishna release_minor: 40396424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk); 40400Sstevel@tonic-gate CRYPTO_SESSION_RELE(sp); 40410Sstevel@tonic-gate crypto_release_minor(cm); 40420Sstevel@tonic-gate 40430Sstevel@tonic-gate if (data.cd_raw.iov_base != NULL) 40440Sstevel@tonic-gate kmem_free(data.cd_raw.iov_base, datalen); 40450Sstevel@tonic-gate 40460Sstevel@tonic-gate if (sign.cd_raw.iov_base != NULL) 40470Sstevel@tonic-gate kmem_free(sign.cd_raw.iov_base, signlen); 40480Sstevel@tonic-gate 40490Sstevel@tonic-gate if (error != 0) 40500Sstevel@tonic-gate return (error); 40510Sstevel@tonic-gate 40520Sstevel@tonic-gate STRUCT_FSET(verify, cv_return_value, rv); 40530Sstevel@tonic-gate if (copyout(STRUCT_BUF(verify), arg, STRUCT_SIZE(verify)) != 0) { 40540Sstevel@tonic-gate return (EFAULT); 40550Sstevel@tonic-gate } 40560Sstevel@tonic-gate return (0); 40570Sstevel@tonic-gate } 40580Sstevel@tonic-gate 40590Sstevel@tonic-gate /* ARGSUSED */ 40600Sstevel@tonic-gate static int 40610Sstevel@tonic-gate verify_recover(dev_t dev, caddr_t arg, int mode, int *rval) 40620Sstevel@tonic-gate { 40630Sstevel@tonic-gate return (common_digest(dev, arg, mode, crypto_verify_recover_single)); 40640Sstevel@tonic-gate } 40650Sstevel@tonic-gate 40660Sstevel@tonic-gate /* ARGSUSED */ 40670Sstevel@tonic-gate static int 40680Sstevel@tonic-gate sign_update(dev_t dev, caddr_t arg, int mode, int *rval) 40690Sstevel@tonic-gate { 40700Sstevel@tonic-gate return (sign_verify_update(dev, arg, mode, crypto_sign_update)); 40710Sstevel@tonic-gate } 40720Sstevel@tonic-gate 40730Sstevel@tonic-gate /* ARGSUSED */ 40740Sstevel@tonic-gate static int 40750Sstevel@tonic-gate verify_update(dev_t dev, caddr_t arg, int mode, int *rval) 40760Sstevel@tonic-gate { 40770Sstevel@tonic-gate return (sign_verify_update(dev, arg, mode, crypto_verify_update)); 40780Sstevel@tonic-gate } 40790Sstevel@tonic-gate 40800Sstevel@tonic-gate /* 40810Sstevel@tonic-gate * ASSUMPTION: crypto_sign_update and crypto_verify_update structures 40820Sstevel@tonic-gate * are identical except for field names. 40830Sstevel@tonic-gate */ 40840Sstevel@tonic-gate static int 40850Sstevel@tonic-gate sign_verify_update(dev_t dev, caddr_t arg, int mode, 40860Sstevel@tonic-gate int (*update)(crypto_context_t, crypto_data_t *, crypto_call_req_t *)) 40870Sstevel@tonic-gate { 40880Sstevel@tonic-gate STRUCT_DECL(crypto_sign_update, sign_update); 40890Sstevel@tonic-gate crypto_session_id_t session_id; 40900Sstevel@tonic-gate crypto_minor_t *cm; 40916424Skrishna crypto_session_data_t *sp = NULL; 40920Sstevel@tonic-gate crypto_ctx_t **ctxpp; 40930Sstevel@tonic-gate crypto_data_t data; 40940Sstevel@tonic-gate size_t datalen, need = 0; 40950Sstevel@tonic-gate int error = 0; 40960Sstevel@tonic-gate int rv; 40976424Skrishna boolean_t rctl_chk = B_FALSE; 40980Sstevel@tonic-gate 40990Sstevel@tonic-gate STRUCT_INIT(sign_update, mode); 41000Sstevel@tonic-gate 41010Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 41020Sstevel@tonic-gate cmn_err(CE_WARN, "sign_verify_update: failed holding minor"); 41030Sstevel@tonic-gate return (ENXIO); 41040Sstevel@tonic-gate } 41050Sstevel@tonic-gate 41060Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(sign_update), 41070Sstevel@tonic-gate STRUCT_SIZE(sign_update)) != 0) { 41080Sstevel@tonic-gate crypto_release_minor(cm); 41090Sstevel@tonic-gate return (EFAULT); 41100Sstevel@tonic-gate } 41110Sstevel@tonic-gate 41120Sstevel@tonic-gate data.cd_raw.iov_base = NULL; 41130Sstevel@tonic-gate 41140Sstevel@tonic-gate datalen = STRUCT_FGET(sign_update, su_datalen); 41150Sstevel@tonic-gate if (datalen > crypto_max_buffer_len) { 41160Sstevel@tonic-gate cmn_err(CE_NOTE, "sign_verify_update: buffer greater than %ld " 41170Sstevel@tonic-gate "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid); 41180Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD; 41190Sstevel@tonic-gate goto release_minor; 41200Sstevel@tonic-gate } 41210Sstevel@tonic-gate 41226424Skrishna session_id = STRUCT_FGET(sign_update, su_session); 41236424Skrishna 41246424Skrishna if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) { 41256424Skrishna goto release_minor; 41266424Skrishna } 41276424Skrishna 41286424Skrishna if ((rv = CRYPTO_BUFFER_CHECK(sp, datalen, rctl_chk)) != 41296424Skrishna CRYPTO_SUCCESS) { 41300Sstevel@tonic-gate goto release_minor; 41310Sstevel@tonic-gate } 41320Sstevel@tonic-gate need = datalen; 41330Sstevel@tonic-gate 41340Sstevel@tonic-gate INIT_RAW_CRYPTO_DATA(data, datalen); 41350Sstevel@tonic-gate 41360Sstevel@tonic-gate if (datalen != 0 && copyin(STRUCT_FGETP(sign_update, su_databuf), 41370Sstevel@tonic-gate data.cd_raw.iov_base, datalen) != 0) { 41380Sstevel@tonic-gate error = EFAULT; 41390Sstevel@tonic-gate goto release_minor; 41400Sstevel@tonic-gate } 41410Sstevel@tonic-gate 41420Sstevel@tonic-gate ctxpp = (update == crypto_sign_update) ? 41430Sstevel@tonic-gate &sp->sd_sign_ctx : &sp->sd_verify_ctx; 41440Sstevel@tonic-gate 41450Sstevel@tonic-gate rv = (update)(*ctxpp, &data, NULL); 41460Sstevel@tonic-gate if (rv != CRYPTO_SUCCESS) 41470Sstevel@tonic-gate CRYPTO_CANCEL_CTX(ctxpp); 41480Sstevel@tonic-gate 41490Sstevel@tonic-gate release_minor: 41506424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk); 41516424Skrishna CRYPTO_SESSION_RELE(sp); 41520Sstevel@tonic-gate crypto_release_minor(cm); 41530Sstevel@tonic-gate 41540Sstevel@tonic-gate if (data.cd_raw.iov_base != NULL) 41550Sstevel@tonic-gate kmem_free(data.cd_raw.iov_base, datalen); 41560Sstevel@tonic-gate 41570Sstevel@tonic-gate if (error != 0) 41580Sstevel@tonic-gate return (error); 41590Sstevel@tonic-gate 41600Sstevel@tonic-gate STRUCT_FSET(sign_update, su_return_value, rv); 41610Sstevel@tonic-gate if (copyout(STRUCT_BUF(sign_update), arg, 41620Sstevel@tonic-gate STRUCT_SIZE(sign_update)) != 0) { 41630Sstevel@tonic-gate return (EFAULT); 41640Sstevel@tonic-gate } 41650Sstevel@tonic-gate return (0); 41660Sstevel@tonic-gate } 41670Sstevel@tonic-gate 41680Sstevel@tonic-gate /* ARGSUSED */ 41690Sstevel@tonic-gate static int 41700Sstevel@tonic-gate sign_final(dev_t dev, caddr_t arg, int mode, int *rval) 41710Sstevel@tonic-gate { 41720Sstevel@tonic-gate return (common_final(dev, arg, mode, crypto_sign_final)); 41730Sstevel@tonic-gate } 41740Sstevel@tonic-gate 41750Sstevel@tonic-gate /* 41760Sstevel@tonic-gate * Can't use the common final because it does a copyout of 41770Sstevel@tonic-gate * the final part. 41780Sstevel@tonic-gate */ 41790Sstevel@tonic-gate /* ARGSUSED */ 41800Sstevel@tonic-gate static int 41810Sstevel@tonic-gate verify_final(dev_t dev, caddr_t arg, int mode, int *rval) 41820Sstevel@tonic-gate { 41830Sstevel@tonic-gate STRUCT_DECL(crypto_verify_final, verify_final); 41840Sstevel@tonic-gate crypto_session_id_t session_id; 41850Sstevel@tonic-gate crypto_minor_t *cm; 41866424Skrishna crypto_session_data_t *sp = NULL; 41870Sstevel@tonic-gate crypto_data_t sign; 41880Sstevel@tonic-gate size_t signlen, need = 0; 41890Sstevel@tonic-gate int error = 0; 41900Sstevel@tonic-gate int rv; 41916424Skrishna boolean_t rctl_chk = B_FALSE; 41920Sstevel@tonic-gate 41930Sstevel@tonic-gate STRUCT_INIT(verify_final, mode); 41940Sstevel@tonic-gate 41950Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 41960Sstevel@tonic-gate cmn_err(CE_WARN, "verify_final: failed holding minor"); 41970Sstevel@tonic-gate return (ENXIO); 41980Sstevel@tonic-gate } 41990Sstevel@tonic-gate 42000Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(verify_final), 42010Sstevel@tonic-gate STRUCT_SIZE(verify_final)) != 0) { 42020Sstevel@tonic-gate crypto_release_minor(cm); 42030Sstevel@tonic-gate return (EFAULT); 42040Sstevel@tonic-gate } 42050Sstevel@tonic-gate 42060Sstevel@tonic-gate sign.cd_raw.iov_base = NULL; 42070Sstevel@tonic-gate 42080Sstevel@tonic-gate signlen = STRUCT_FGET(verify_final, vf_signlen); 42090Sstevel@tonic-gate if (signlen > crypto_max_buffer_len) { 42100Sstevel@tonic-gate cmn_err(CE_NOTE, "verify_final: buffer greater than %ld " 42110Sstevel@tonic-gate "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid); 42120Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD; 42130Sstevel@tonic-gate goto release_minor; 42140Sstevel@tonic-gate } 42150Sstevel@tonic-gate 42166424Skrishna session_id = STRUCT_FGET(verify_final, vf_session); 42176424Skrishna 42186424Skrishna if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) { 42196424Skrishna goto release_minor; 42206424Skrishna } 42216424Skrishna 42226424Skrishna if ((rv = CRYPTO_BUFFER_CHECK(sp, signlen, rctl_chk)) != 42236424Skrishna CRYPTO_SUCCESS) { 42240Sstevel@tonic-gate goto release_minor; 42250Sstevel@tonic-gate } 42260Sstevel@tonic-gate need = signlen; 42270Sstevel@tonic-gate 42280Sstevel@tonic-gate INIT_RAW_CRYPTO_DATA(sign, signlen); 42290Sstevel@tonic-gate 42300Sstevel@tonic-gate if (signlen != 0 && copyin(STRUCT_FGETP(verify_final, vf_signbuf), 42310Sstevel@tonic-gate sign.cd_raw.iov_base, signlen) != 0) { 42320Sstevel@tonic-gate error = EFAULT; 42330Sstevel@tonic-gate goto release_minor; 42340Sstevel@tonic-gate } 42350Sstevel@tonic-gate 42360Sstevel@tonic-gate rv = crypto_verify_final(sp->sd_verify_ctx, &sign, NULL); 42370Sstevel@tonic-gate if (KCF_CONTEXT_DONE(rv)) 42380Sstevel@tonic-gate sp->sd_verify_ctx = NULL; 42390Sstevel@tonic-gate 42406424Skrishna release_minor: 42416424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk); 42420Sstevel@tonic-gate CRYPTO_SESSION_RELE(sp); 42430Sstevel@tonic-gate crypto_release_minor(cm); 42440Sstevel@tonic-gate 42450Sstevel@tonic-gate if (sign.cd_raw.iov_base != NULL) 42460Sstevel@tonic-gate kmem_free(sign.cd_raw.iov_base, signlen); 42470Sstevel@tonic-gate 42480Sstevel@tonic-gate if (error != 0) 42490Sstevel@tonic-gate return (error); 42500Sstevel@tonic-gate 42510Sstevel@tonic-gate STRUCT_FSET(verify_final, vf_return_value, rv); 42520Sstevel@tonic-gate if (copyout(STRUCT_BUF(verify_final), arg, 42530Sstevel@tonic-gate STRUCT_SIZE(verify_final)) != 0) { 42540Sstevel@tonic-gate return (EFAULT); 42550Sstevel@tonic-gate } 42560Sstevel@tonic-gate return (0); 42570Sstevel@tonic-gate } 42580Sstevel@tonic-gate 42590Sstevel@tonic-gate /* ARGSUSED */ 42600Sstevel@tonic-gate static int 42610Sstevel@tonic-gate seed_random(dev_t dev, caddr_t arg, int mode, int *rval) 42620Sstevel@tonic-gate { 42630Sstevel@tonic-gate STRUCT_DECL(crypto_seed_random, seed_random); 4264904Smcpowers kcf_provider_desc_t *real_provider = NULL; 42650Sstevel@tonic-gate kcf_req_params_t params; 42660Sstevel@tonic-gate crypto_session_id_t session_id; 42670Sstevel@tonic-gate crypto_minor_t *cm; 42686424Skrishna crypto_session_data_t *sp = NULL; 42690Sstevel@tonic-gate uchar_t *seed_buffer = NULL; 42700Sstevel@tonic-gate size_t seed_len; 42710Sstevel@tonic-gate size_t need = 0; 42720Sstevel@tonic-gate int error = 0; 42730Sstevel@tonic-gate int rv; 42746424Skrishna boolean_t rctl_chk = B_FALSE; 42750Sstevel@tonic-gate 42760Sstevel@tonic-gate STRUCT_INIT(seed_random, mode); 42770Sstevel@tonic-gate 42780Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 42790Sstevel@tonic-gate cmn_err(CE_WARN, "seed_random: failed holding minor"); 42800Sstevel@tonic-gate return (ENXIO); 42810Sstevel@tonic-gate } 42820Sstevel@tonic-gate 42830Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(seed_random), 42840Sstevel@tonic-gate STRUCT_SIZE(seed_random)) != 0) { 42850Sstevel@tonic-gate crypto_release_minor(cm); 42860Sstevel@tonic-gate return (EFAULT); 42870Sstevel@tonic-gate } 42880Sstevel@tonic-gate 42890Sstevel@tonic-gate seed_len = STRUCT_FGET(seed_random, sr_seedlen); 42900Sstevel@tonic-gate if (seed_len > crypto_max_buffer_len) { 42910Sstevel@tonic-gate cmn_err(CE_NOTE, "seed_random: buffer greater than %ld " 42920Sstevel@tonic-gate "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid); 42930Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD; 42940Sstevel@tonic-gate goto release_minor; 42950Sstevel@tonic-gate } 42960Sstevel@tonic-gate 42976424Skrishna session_id = STRUCT_FGET(seed_random, sr_session); 42986424Skrishna 42996424Skrishna if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) { 43006424Skrishna goto release_minor; 43016424Skrishna } 43026424Skrishna 43036424Skrishna if ((rv = CRYPTO_BUFFER_CHECK(sp, seed_len, rctl_chk)) != 43046424Skrishna CRYPTO_SUCCESS) { 43050Sstevel@tonic-gate goto release_minor; 43060Sstevel@tonic-gate } 43070Sstevel@tonic-gate need = seed_len; 43080Sstevel@tonic-gate seed_buffer = kmem_alloc(seed_len, KM_SLEEP); 43090Sstevel@tonic-gate 43100Sstevel@tonic-gate if (seed_len != 0 && copyin(STRUCT_FGETP(seed_random, sr_seedbuf), 43110Sstevel@tonic-gate seed_buffer, seed_len) != 0) { 43120Sstevel@tonic-gate error = EFAULT; 43130Sstevel@tonic-gate goto release_minor; 43140Sstevel@tonic-gate } 43150Sstevel@tonic-gate 43161808Smcpowers if ((rv = kcf_get_hardware_provider_nomech( 4317904Smcpowers CRYPTO_OPS_OFFSET(random_ops), CRYPTO_RANDOM_OFFSET(seed_random), 4318904Smcpowers CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider)) 4319904Smcpowers != CRYPTO_SUCCESS) { 43206424Skrishna goto release_minor; 43210Sstevel@tonic-gate } 43220Sstevel@tonic-gate 43230Sstevel@tonic-gate KCF_WRAP_RANDOM_OPS_PARAMS(¶ms, KCF_OP_RANDOM_SEED, 43241920Smcpowers sp->sd_provider_session->ps_session, seed_buffer, seed_len, 0, 43251920Smcpowers CRYPTO_SEED_NOW); 43260Sstevel@tonic-gate 43270Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE); 43280Sstevel@tonic-gate 43296424Skrishna release_minor: 43306424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk); 43310Sstevel@tonic-gate CRYPTO_SESSION_RELE(sp); 43320Sstevel@tonic-gate crypto_release_minor(cm); 43330Sstevel@tonic-gate 4334904Smcpowers if (real_provider != NULL) 4335904Smcpowers KCF_PROV_REFRELE(real_provider); 4336904Smcpowers 43370Sstevel@tonic-gate if (seed_buffer != NULL) 43380Sstevel@tonic-gate kmem_free(seed_buffer, seed_len); 43390Sstevel@tonic-gate 43400Sstevel@tonic-gate if (error != 0) 43410Sstevel@tonic-gate return (error); 43420Sstevel@tonic-gate 43430Sstevel@tonic-gate STRUCT_FSET(seed_random, sr_return_value, rv); 43440Sstevel@tonic-gate if (copyout(STRUCT_BUF(seed_random), arg, 43450Sstevel@tonic-gate STRUCT_SIZE(seed_random)) != 0) { 43460Sstevel@tonic-gate return (EFAULT); 43470Sstevel@tonic-gate } 43480Sstevel@tonic-gate return (0); 43490Sstevel@tonic-gate } 43500Sstevel@tonic-gate 43510Sstevel@tonic-gate /* ARGSUSED */ 43520Sstevel@tonic-gate static int 43530Sstevel@tonic-gate generate_random(dev_t dev, caddr_t arg, int mode, int *rval) 43540Sstevel@tonic-gate { 43550Sstevel@tonic-gate STRUCT_DECL(crypto_generate_random, generate_random); 4356904Smcpowers kcf_provider_desc_t *real_provider = NULL; 43570Sstevel@tonic-gate kcf_req_params_t params; 43580Sstevel@tonic-gate crypto_session_id_t session_id; 43590Sstevel@tonic-gate crypto_minor_t *cm; 43606424Skrishna crypto_session_data_t *sp = NULL; 43610Sstevel@tonic-gate uchar_t *buffer = NULL; 43620Sstevel@tonic-gate size_t len; 43630Sstevel@tonic-gate size_t need = 0; 43640Sstevel@tonic-gate int error = 0; 43650Sstevel@tonic-gate int rv; 43666424Skrishna boolean_t rctl_chk = B_FALSE; 43670Sstevel@tonic-gate 43680Sstevel@tonic-gate STRUCT_INIT(generate_random, mode); 43690Sstevel@tonic-gate 43700Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 43710Sstevel@tonic-gate cmn_err(CE_WARN, "generate_random: failed holding minor"); 43720Sstevel@tonic-gate return (ENXIO); 43730Sstevel@tonic-gate } 43740Sstevel@tonic-gate 43750Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(generate_random), 43760Sstevel@tonic-gate STRUCT_SIZE(generate_random)) != 0) { 43770Sstevel@tonic-gate crypto_release_minor(cm); 43780Sstevel@tonic-gate return (EFAULT); 43790Sstevel@tonic-gate } 43800Sstevel@tonic-gate 43810Sstevel@tonic-gate len = STRUCT_FGET(generate_random, gr_buflen); 43820Sstevel@tonic-gate if (len > crypto_max_buffer_len) { 43830Sstevel@tonic-gate cmn_err(CE_NOTE, "generate_random: buffer greater than %ld " 43840Sstevel@tonic-gate "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid); 43850Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD; 43860Sstevel@tonic-gate goto release_minor; 43870Sstevel@tonic-gate } 43880Sstevel@tonic-gate 43896424Skrishna session_id = STRUCT_FGET(generate_random, gr_session); 43906424Skrishna 43916424Skrishna if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) { 43926424Skrishna goto release_minor; 43936424Skrishna } 43946424Skrishna 43956424Skrishna if ((rv = CRYPTO_BUFFER_CHECK(sp, len, rctl_chk)) != 43966424Skrishna CRYPTO_SUCCESS) { 43970Sstevel@tonic-gate goto release_minor; 43980Sstevel@tonic-gate } 43990Sstevel@tonic-gate need = len; 44000Sstevel@tonic-gate buffer = kmem_alloc(len, KM_SLEEP); 44010Sstevel@tonic-gate 44021808Smcpowers if ((rv = kcf_get_hardware_provider_nomech( 44030Sstevel@tonic-gate CRYPTO_OPS_OFFSET(random_ops), 4404904Smcpowers CRYPTO_RANDOM_OFFSET(generate_random), CHECK_RESTRICT_FALSE, 44050Sstevel@tonic-gate sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) { 44066424Skrishna goto release_minor; 44070Sstevel@tonic-gate } 44080Sstevel@tonic-gate 44090Sstevel@tonic-gate KCF_WRAP_RANDOM_OPS_PARAMS(¶ms, KCF_OP_RANDOM_GENERATE, 44101920Smcpowers sp->sd_provider_session->ps_session, buffer, len, 0, 0); 44110Sstevel@tonic-gate 44120Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE); 44130Sstevel@tonic-gate 44140Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) { 44150Sstevel@tonic-gate if (len != 0 && copyout(buffer, 44160Sstevel@tonic-gate STRUCT_FGETP(generate_random, gr_buf), len) != 0) { 44170Sstevel@tonic-gate error = EFAULT; 44180Sstevel@tonic-gate } 44190Sstevel@tonic-gate } 44200Sstevel@tonic-gate 44210Sstevel@tonic-gate release_minor: 44226424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk); 44236424Skrishna CRYPTO_SESSION_RELE(sp); 44240Sstevel@tonic-gate crypto_release_minor(cm); 44250Sstevel@tonic-gate 4426904Smcpowers if (real_provider != NULL) 4427904Smcpowers KCF_PROV_REFRELE(real_provider); 4428904Smcpowers 44290Sstevel@tonic-gate if (buffer != NULL) { 44300Sstevel@tonic-gate /* random numbers are often used to create keys */ 44310Sstevel@tonic-gate bzero(buffer, len); 44320Sstevel@tonic-gate kmem_free(buffer, len); 44330Sstevel@tonic-gate } 44340Sstevel@tonic-gate 44350Sstevel@tonic-gate if (error != 0) 44360Sstevel@tonic-gate return (error); 44370Sstevel@tonic-gate 44380Sstevel@tonic-gate STRUCT_FSET(generate_random, gr_return_value, rv); 44390Sstevel@tonic-gate if (copyout(STRUCT_BUF(generate_random), arg, 44400Sstevel@tonic-gate STRUCT_SIZE(generate_random)) != 0) { 44410Sstevel@tonic-gate return (EFAULT); 44420Sstevel@tonic-gate } 44430Sstevel@tonic-gate return (0); 44440Sstevel@tonic-gate } 44450Sstevel@tonic-gate 44460Sstevel@tonic-gate /* 44470Sstevel@tonic-gate * Copyout a kernel array of attributes to user space. 44480Sstevel@tonic-gate * u_attrs is the corresponding user space array containing 44490Sstevel@tonic-gate * user space pointers necessary for the copyout. 44500Sstevel@tonic-gate */ 44510Sstevel@tonic-gate /* ARGSUSED */ 44520Sstevel@tonic-gate static int 44530Sstevel@tonic-gate copyout_attributes(int mode, caddr_t out, uint_t count, 44540Sstevel@tonic-gate crypto_object_attribute_t *k_attrs, caddr_t u_attrs) 44550Sstevel@tonic-gate { 44560Sstevel@tonic-gate STRUCT_DECL(crypto_object_attribute, oa); 44570Sstevel@tonic-gate caddr_t p, valuep; 44580Sstevel@tonic-gate size_t value_len; 44590Sstevel@tonic-gate size_t len; 44600Sstevel@tonic-gate int i; 44610Sstevel@tonic-gate int error = 0; 44620Sstevel@tonic-gate 44630Sstevel@tonic-gate if (count == 0) 44640Sstevel@tonic-gate return (0); 44650Sstevel@tonic-gate 44660Sstevel@tonic-gate STRUCT_INIT(oa, mode); 44670Sstevel@tonic-gate 44680Sstevel@tonic-gate len = count * STRUCT_SIZE(oa); 44690Sstevel@tonic-gate 44700Sstevel@tonic-gate ASSERT(u_attrs != NULL); 44710Sstevel@tonic-gate p = u_attrs; 44720Sstevel@tonic-gate for (i = 0; i < count; i++) { 44730Sstevel@tonic-gate /* can this bcopy be eliminated? */ 44740Sstevel@tonic-gate bcopy(p, STRUCT_BUF(oa), STRUCT_SIZE(oa)); 44750Sstevel@tonic-gate value_len = k_attrs[i].oa_value_len; 44760Sstevel@tonic-gate STRUCT_FSET(oa, oa_type, k_attrs[i].oa_type); 44770Sstevel@tonic-gate STRUCT_FSET(oa, oa_value_len, value_len); 44780Sstevel@tonic-gate valuep = STRUCT_FGETP(oa, oa_value); 44790Sstevel@tonic-gate if (valuep != NULL && value_len != -1) { 44800Sstevel@tonic-gate if (copyout(k_attrs[i].oa_value, 44810Sstevel@tonic-gate valuep, value_len) != 0) { 44820Sstevel@tonic-gate error = EFAULT; 44830Sstevel@tonic-gate goto out; 44840Sstevel@tonic-gate } 44850Sstevel@tonic-gate } 44860Sstevel@tonic-gate bcopy(STRUCT_BUF(oa), p, STRUCT_SIZE(oa)); 44870Sstevel@tonic-gate p += STRUCT_SIZE(oa); 44880Sstevel@tonic-gate } 44890Sstevel@tonic-gate if (copyout(u_attrs, out, len)) { 44900Sstevel@tonic-gate error = EFAULT; 44910Sstevel@tonic-gate } 44920Sstevel@tonic-gate out: 44930Sstevel@tonic-gate return (error); 44940Sstevel@tonic-gate } 44950Sstevel@tonic-gate 44960Sstevel@tonic-gate 44970Sstevel@tonic-gate /* ARGSUSED */ 44980Sstevel@tonic-gate static int 44990Sstevel@tonic-gate object_create(dev_t dev, caddr_t arg, int mode, int *rval) 45000Sstevel@tonic-gate { 45010Sstevel@tonic-gate STRUCT_DECL(crypto_object_create, object_create); 4502904Smcpowers kcf_provider_desc_t *real_provider = NULL; 45030Sstevel@tonic-gate kcf_req_params_t params; 45040Sstevel@tonic-gate crypto_object_attribute_t *k_attrs = NULL; 45050Sstevel@tonic-gate crypto_session_id_t session_id; 45060Sstevel@tonic-gate crypto_minor_t *cm; 45070Sstevel@tonic-gate crypto_session_data_t *sp = NULL; 45080Sstevel@tonic-gate crypto_object_id_t object_handle; 45090Sstevel@tonic-gate caddr_t oc_attributes; 45100Sstevel@tonic-gate size_t k_attrs_size; 45110Sstevel@tonic-gate size_t rctl_bytes = 0; 45126424Skrishna boolean_t rctl_chk = B_FALSE; 45130Sstevel@tonic-gate int error = 0; 45140Sstevel@tonic-gate int rv; 45150Sstevel@tonic-gate uint_t count; 45160Sstevel@tonic-gate 45170Sstevel@tonic-gate STRUCT_INIT(object_create, mode); 45180Sstevel@tonic-gate 45190Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 45200Sstevel@tonic-gate cmn_err(CE_WARN, "object_create: failed holding minor"); 45210Sstevel@tonic-gate return (ENXIO); 45220Sstevel@tonic-gate } 45230Sstevel@tonic-gate 45240Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(object_create), 45250Sstevel@tonic-gate STRUCT_SIZE(object_create)) != 0) { 45260Sstevel@tonic-gate crypto_release_minor(cm); 45270Sstevel@tonic-gate return (EFAULT); 45280Sstevel@tonic-gate } 45290Sstevel@tonic-gate 45300Sstevel@tonic-gate count = STRUCT_FGET(object_create, oc_count); 45310Sstevel@tonic-gate oc_attributes = STRUCT_FGETP(object_create, oc_attributes); 45320Sstevel@tonic-gate 45330Sstevel@tonic-gate session_id = STRUCT_FGET(object_create, oc_session); 45340Sstevel@tonic-gate if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) { 45350Sstevel@tonic-gate goto release_minor; 45360Sstevel@tonic-gate } 45376424Skrishna if (!copyin_attributes(mode, sp, count, oc_attributes, &k_attrs, 45386424Skrishna &k_attrs_size, NULL, &rv, &error, &rctl_bytes, 45396424Skrishna &rctl_chk, B_TRUE)) { 45406424Skrishna goto release_minor; 45416424Skrishna } 45420Sstevel@tonic-gate 45430Sstevel@tonic-gate if ((rv = kcf_get_hardware_provider_nomech( 45440Sstevel@tonic-gate CRYPTO_OPS_OFFSET(object_ops), 45450Sstevel@tonic-gate CRYPTO_OBJECT_OFFSET(object_create), 4546904Smcpowers CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider)) 4547904Smcpowers != CRYPTO_SUCCESS) { 45480Sstevel@tonic-gate goto release_minor; 45490Sstevel@tonic-gate } 45500Sstevel@tonic-gate 45510Sstevel@tonic-gate KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, KCF_OP_OBJECT_CREATE, 45520Sstevel@tonic-gate sp->sd_provider_session->ps_session, 0, k_attrs, count, 45530Sstevel@tonic-gate &object_handle, 0, NULL, NULL, 0, NULL); 45540Sstevel@tonic-gate 45550Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE); 45560Sstevel@tonic-gate 45570Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) 45580Sstevel@tonic-gate STRUCT_FSET(object_create, oc_handle, object_handle); 45590Sstevel@tonic-gate 45600Sstevel@tonic-gate release_minor: 45616424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk); 45620Sstevel@tonic-gate 45630Sstevel@tonic-gate if (k_attrs != NULL) 45640Sstevel@tonic-gate kmem_free(k_attrs, k_attrs_size); 45650Sstevel@tonic-gate 45660Sstevel@tonic-gate if (error != 0) 45670Sstevel@tonic-gate goto out; 45680Sstevel@tonic-gate 45690Sstevel@tonic-gate STRUCT_FSET(object_create, oc_return_value, rv); 45700Sstevel@tonic-gate if (copyout(STRUCT_BUF(object_create), arg, 45710Sstevel@tonic-gate STRUCT_SIZE(object_create)) != 0) { 45720Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) { 45730Sstevel@tonic-gate KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, 45740Sstevel@tonic-gate KCF_OP_OBJECT_DESTROY, 45750Sstevel@tonic-gate sp->sd_provider_session->ps_session, object_handle, 45760Sstevel@tonic-gate NULL, 0, NULL, 0, NULL, NULL, 0, NULL); 45770Sstevel@tonic-gate 45780Sstevel@tonic-gate (void) kcf_submit_request(real_provider, NULL, 45790Sstevel@tonic-gate NULL, ¶ms, B_FALSE); 45800Sstevel@tonic-gate 45810Sstevel@tonic-gate error = EFAULT; 45820Sstevel@tonic-gate } 45830Sstevel@tonic-gate } 45840Sstevel@tonic-gate out: 45856424Skrishna CRYPTO_SESSION_RELE(sp); 45860Sstevel@tonic-gate crypto_release_minor(cm); 4587904Smcpowers if (real_provider != NULL) 4588904Smcpowers KCF_PROV_REFRELE(real_provider); 45890Sstevel@tonic-gate return (error); 45900Sstevel@tonic-gate } 45910Sstevel@tonic-gate 45920Sstevel@tonic-gate /* ARGSUSED */ 45930Sstevel@tonic-gate static int 45940Sstevel@tonic-gate object_copy(dev_t dev, caddr_t arg, int mode, int *rval) 45950Sstevel@tonic-gate { 45960Sstevel@tonic-gate STRUCT_DECL(crypto_object_copy, object_copy); 4597904Smcpowers kcf_provider_desc_t *real_provider = NULL; 45980Sstevel@tonic-gate kcf_req_params_t params; 45990Sstevel@tonic-gate crypto_object_attribute_t *k_attrs = NULL; 46000Sstevel@tonic-gate crypto_session_id_t session_id; 46010Sstevel@tonic-gate crypto_minor_t *cm; 46020Sstevel@tonic-gate crypto_session_data_t *sp = NULL; 46030Sstevel@tonic-gate crypto_object_id_t handle, new_handle; 46040Sstevel@tonic-gate caddr_t oc_new_attributes; 46050Sstevel@tonic-gate size_t k_attrs_size; 46060Sstevel@tonic-gate size_t rctl_bytes = 0; 46076424Skrishna boolean_t rctl_chk = B_FALSE; 46080Sstevel@tonic-gate int error = 0; 46090Sstevel@tonic-gate int rv; 46100Sstevel@tonic-gate uint_t count; 46110Sstevel@tonic-gate 46120Sstevel@tonic-gate STRUCT_INIT(object_copy, mode); 46130Sstevel@tonic-gate 46140Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 46150Sstevel@tonic-gate cmn_err(CE_WARN, "object_copy: failed holding minor"); 46160Sstevel@tonic-gate return (ENXIO); 46170Sstevel@tonic-gate } 46180Sstevel@tonic-gate 46190Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(object_copy), 46200Sstevel@tonic-gate STRUCT_SIZE(object_copy)) != 0) { 46210Sstevel@tonic-gate crypto_release_minor(cm); 46220Sstevel@tonic-gate return (EFAULT); 46230Sstevel@tonic-gate } 46240Sstevel@tonic-gate 46250Sstevel@tonic-gate count = STRUCT_FGET(object_copy, oc_count); 46260Sstevel@tonic-gate oc_new_attributes = STRUCT_FGETP(object_copy, oc_new_attributes); 46270Sstevel@tonic-gate 46280Sstevel@tonic-gate session_id = STRUCT_FGET(object_copy, oc_session); 46290Sstevel@tonic-gate 46300Sstevel@tonic-gate if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) { 46310Sstevel@tonic-gate goto release_minor; 46320Sstevel@tonic-gate } 46336424Skrishna if (!copyin_attributes(mode, sp, count, oc_new_attributes, &k_attrs, 46346424Skrishna &k_attrs_size, NULL, &rv, &error, &rctl_bytes, 46356424Skrishna &rctl_chk, B_TRUE)) { 46366424Skrishna goto release_minor; 46376424Skrishna } 46380Sstevel@tonic-gate 46390Sstevel@tonic-gate if ((rv = kcf_get_hardware_provider_nomech( 46400Sstevel@tonic-gate CRYPTO_OPS_OFFSET(object_ops), 4641904Smcpowers CRYPTO_OBJECT_OFFSET(object_copy), CHECK_RESTRICT_FALSE, 46420Sstevel@tonic-gate sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) { 46430Sstevel@tonic-gate goto release_minor; 46440Sstevel@tonic-gate } 46450Sstevel@tonic-gate 46460Sstevel@tonic-gate handle = STRUCT_FGET(object_copy, oc_handle); 46470Sstevel@tonic-gate KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, KCF_OP_OBJECT_COPY, 46480Sstevel@tonic-gate sp->sd_provider_session->ps_session, handle, k_attrs, count, 46490Sstevel@tonic-gate &new_handle, 0, NULL, NULL, 0, NULL); 46500Sstevel@tonic-gate 46510Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE); 46520Sstevel@tonic-gate 46530Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) 46540Sstevel@tonic-gate STRUCT_FSET(object_copy, oc_new_handle, new_handle); 46550Sstevel@tonic-gate 46560Sstevel@tonic-gate release_minor: 46576424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk); 46580Sstevel@tonic-gate 46590Sstevel@tonic-gate if (k_attrs != NULL) 46600Sstevel@tonic-gate kmem_free(k_attrs, k_attrs_size); 46610Sstevel@tonic-gate 46620Sstevel@tonic-gate if (error != 0) 46630Sstevel@tonic-gate goto out; 46640Sstevel@tonic-gate 46650Sstevel@tonic-gate STRUCT_FSET(object_copy, oc_return_value, rv); 46660Sstevel@tonic-gate if (copyout(STRUCT_BUF(object_copy), arg, 46670Sstevel@tonic-gate STRUCT_SIZE(object_copy)) != 0) { 46680Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) { 46690Sstevel@tonic-gate KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, 46700Sstevel@tonic-gate KCF_OP_OBJECT_DESTROY, 46710Sstevel@tonic-gate sp->sd_provider_session->ps_session, new_handle, 46720Sstevel@tonic-gate NULL, 0, NULL, 0, NULL, NULL, 0, NULL); 46730Sstevel@tonic-gate 46740Sstevel@tonic-gate (void) kcf_submit_request(real_provider, NULL, 46750Sstevel@tonic-gate NULL, ¶ms, B_FALSE); 46760Sstevel@tonic-gate 46770Sstevel@tonic-gate error = EFAULT; 46780Sstevel@tonic-gate } 46790Sstevel@tonic-gate } 46800Sstevel@tonic-gate out: 46816424Skrishna CRYPTO_SESSION_RELE(sp); 46820Sstevel@tonic-gate crypto_release_minor(cm); 4683904Smcpowers if (real_provider != NULL) 4684904Smcpowers KCF_PROV_REFRELE(real_provider); 46850Sstevel@tonic-gate return (error); 46860Sstevel@tonic-gate } 46870Sstevel@tonic-gate 46880Sstevel@tonic-gate /* ARGSUSED */ 46890Sstevel@tonic-gate static int 46900Sstevel@tonic-gate object_destroy(dev_t dev, caddr_t arg, int mode, int *rval) 46910Sstevel@tonic-gate { 46920Sstevel@tonic-gate STRUCT_DECL(crypto_object_destroy, object_destroy); 46930Sstevel@tonic-gate kcf_provider_desc_t *real_provider; 46940Sstevel@tonic-gate kcf_req_params_t params; 46950Sstevel@tonic-gate crypto_session_id_t session_id; 46960Sstevel@tonic-gate crypto_minor_t *cm; 46970Sstevel@tonic-gate crypto_session_data_t *sp; 46980Sstevel@tonic-gate crypto_object_id_t handle; 46990Sstevel@tonic-gate int error = 0; 47000Sstevel@tonic-gate int rv; 47010Sstevel@tonic-gate 47020Sstevel@tonic-gate STRUCT_INIT(object_destroy, mode); 47030Sstevel@tonic-gate 47040Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 47050Sstevel@tonic-gate cmn_err(CE_WARN, "object_destroy: failed holding minor"); 47060Sstevel@tonic-gate return (ENXIO); 47070Sstevel@tonic-gate } 47080Sstevel@tonic-gate 47090Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(object_destroy), 47100Sstevel@tonic-gate STRUCT_SIZE(object_destroy)) != 0) { 47110Sstevel@tonic-gate crypto_release_minor(cm); 47120Sstevel@tonic-gate return (EFAULT); 47130Sstevel@tonic-gate } 47140Sstevel@tonic-gate 47150Sstevel@tonic-gate session_id = STRUCT_FGET(object_destroy, od_session); 47160Sstevel@tonic-gate 47170Sstevel@tonic-gate if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) { 47180Sstevel@tonic-gate goto release_minor; 47190Sstevel@tonic-gate } 47200Sstevel@tonic-gate 47210Sstevel@tonic-gate if ((rv = kcf_get_hardware_provider_nomech( 47220Sstevel@tonic-gate CRYPTO_OPS_OFFSET(object_ops), 4723904Smcpowers CRYPTO_OBJECT_OFFSET(object_destroy), CHECK_RESTRICT_FALSE, 47240Sstevel@tonic-gate sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) { 47250Sstevel@tonic-gate goto out; 47260Sstevel@tonic-gate } 47270Sstevel@tonic-gate 47280Sstevel@tonic-gate handle = STRUCT_FGET(object_destroy, od_handle); 47290Sstevel@tonic-gate KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, KCF_OP_OBJECT_DESTROY, 47300Sstevel@tonic-gate sp->sd_provider_session->ps_session, handle, NULL, 0, NULL, 0, 47310Sstevel@tonic-gate NULL, NULL, 0, NULL); 47320Sstevel@tonic-gate 47330Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE); 4734904Smcpowers KCF_PROV_REFRELE(real_provider); 47350Sstevel@tonic-gate 47360Sstevel@tonic-gate out: 47370Sstevel@tonic-gate CRYPTO_SESSION_RELE(sp); 47380Sstevel@tonic-gate 47390Sstevel@tonic-gate release_minor: 47400Sstevel@tonic-gate crypto_release_minor(cm); 47410Sstevel@tonic-gate 47420Sstevel@tonic-gate if (error != 0) 47430Sstevel@tonic-gate return (error); 47440Sstevel@tonic-gate 47450Sstevel@tonic-gate STRUCT_FSET(object_destroy, od_return_value, rv); 47460Sstevel@tonic-gate 47470Sstevel@tonic-gate if (copyout(STRUCT_BUF(object_destroy), arg, 47480Sstevel@tonic-gate STRUCT_SIZE(object_destroy)) != 0) { 47490Sstevel@tonic-gate return (EFAULT); 47500Sstevel@tonic-gate } 47510Sstevel@tonic-gate return (0); 47520Sstevel@tonic-gate } 47530Sstevel@tonic-gate 47540Sstevel@tonic-gate /* ARGSUSED */ 47550Sstevel@tonic-gate static int 47560Sstevel@tonic-gate object_get_attribute_value(dev_t dev, caddr_t arg, int mode, int *rval) 47570Sstevel@tonic-gate { 47580Sstevel@tonic-gate STRUCT_DECL(crypto_object_get_attribute_value, get_attribute_value); 47590Sstevel@tonic-gate /* LINTED E_FUNC_SET_NOT_USED */ 47600Sstevel@tonic-gate STRUCT_DECL(crypto_object_attribute, oa); 47610Sstevel@tonic-gate kcf_provider_desc_t *real_provider; 47620Sstevel@tonic-gate kcf_req_params_t params; 47630Sstevel@tonic-gate crypto_object_attribute_t *k_attrs = NULL; 47640Sstevel@tonic-gate crypto_session_id_t session_id; 47650Sstevel@tonic-gate crypto_minor_t *cm; 47666424Skrishna crypto_session_data_t *sp = NULL; 47670Sstevel@tonic-gate crypto_object_id_t handle; 47680Sstevel@tonic-gate caddr_t og_attributes; 47698043SMark.Powers@Sun.COM caddr_t u_attrs = NULL; 47700Sstevel@tonic-gate size_t k_attrs_size; 47710Sstevel@tonic-gate size_t rctl_bytes = 0; 47726424Skrishna boolean_t rctl_chk = B_FALSE; 47730Sstevel@tonic-gate int error = 0; 47740Sstevel@tonic-gate int rv; 47750Sstevel@tonic-gate uint_t count; 47760Sstevel@tonic-gate 47770Sstevel@tonic-gate STRUCT_INIT(get_attribute_value, mode); 47780Sstevel@tonic-gate STRUCT_INIT(oa, mode); 47790Sstevel@tonic-gate 47800Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 47810Sstevel@tonic-gate cmn_err(CE_WARN, 47820Sstevel@tonic-gate "object_get_attribute_value: failed holding minor"); 47830Sstevel@tonic-gate return (ENXIO); 47840Sstevel@tonic-gate } 47850Sstevel@tonic-gate 47860Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(get_attribute_value), 47870Sstevel@tonic-gate STRUCT_SIZE(get_attribute_value)) != 0) { 47880Sstevel@tonic-gate crypto_release_minor(cm); 47890Sstevel@tonic-gate return (EFAULT); 47900Sstevel@tonic-gate } 47910Sstevel@tonic-gate 47920Sstevel@tonic-gate count = STRUCT_FGET(get_attribute_value, og_count); 47930Sstevel@tonic-gate og_attributes = STRUCT_FGETP(get_attribute_value, og_attributes); 47940Sstevel@tonic-gate 47950Sstevel@tonic-gate session_id = STRUCT_FGET(get_attribute_value, og_session); 47960Sstevel@tonic-gate 47970Sstevel@tonic-gate if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) { 47980Sstevel@tonic-gate goto release_minor; 47990Sstevel@tonic-gate } 48006424Skrishna if (!copyin_attributes(mode, sp, count, og_attributes, &k_attrs, 48016424Skrishna &k_attrs_size, &u_attrs, &rv, &error, &rctl_bytes, 48026424Skrishna &rctl_chk, B_FALSE)) { 48036424Skrishna goto release_minor; 48046424Skrishna } 48050Sstevel@tonic-gate 48060Sstevel@tonic-gate if ((rv = kcf_get_hardware_provider_nomech( 48070Sstevel@tonic-gate CRYPTO_OPS_OFFSET(object_ops), 48080Sstevel@tonic-gate CRYPTO_OBJECT_OFFSET(object_get_attribute_value), 4809904Smcpowers CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider)) 4810904Smcpowers != CRYPTO_SUCCESS) { 48110Sstevel@tonic-gate goto out; 48120Sstevel@tonic-gate } 48130Sstevel@tonic-gate 48140Sstevel@tonic-gate handle = STRUCT_FGET(get_attribute_value, og_handle); 48150Sstevel@tonic-gate KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, KCF_OP_OBJECT_GET_ATTRIBUTE_VALUE, 48160Sstevel@tonic-gate sp->sd_provider_session->ps_session, handle, k_attrs, count, NULL, 48170Sstevel@tonic-gate 0, NULL, NULL, 0, NULL); 48180Sstevel@tonic-gate 48190Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE); 4820904Smcpowers KCF_PROV_REFRELE(real_provider); 48210Sstevel@tonic-gate 48220Sstevel@tonic-gate out: 48230Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS || rv == CRYPTO_ATTRIBUTE_SENSITIVE || 48240Sstevel@tonic-gate rv == CRYPTO_ATTRIBUTE_TYPE_INVALID || 48250Sstevel@tonic-gate rv == CRYPTO_BUFFER_TOO_SMALL) { 48260Sstevel@tonic-gate error = copyout_attributes(mode, 48270Sstevel@tonic-gate STRUCT_FGETP(get_attribute_value, og_attributes), 48280Sstevel@tonic-gate count, k_attrs, u_attrs); 48290Sstevel@tonic-gate } 48300Sstevel@tonic-gate 48310Sstevel@tonic-gate release_minor: 48326424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk); 48336424Skrishna CRYPTO_SESSION_RELE(sp); 48340Sstevel@tonic-gate crypto_release_minor(cm); 48350Sstevel@tonic-gate 48360Sstevel@tonic-gate if (k_attrs != NULL) 48370Sstevel@tonic-gate kmem_free(k_attrs, k_attrs_size); 48380Sstevel@tonic-gate 48390Sstevel@tonic-gate if (u_attrs != NULL) 48400Sstevel@tonic-gate kmem_free(u_attrs, count * STRUCT_SIZE(oa)); 48410Sstevel@tonic-gate 48420Sstevel@tonic-gate if (error != 0) 48430Sstevel@tonic-gate return (error); 48440Sstevel@tonic-gate 48450Sstevel@tonic-gate STRUCT_FSET(get_attribute_value, og_return_value, rv); 48460Sstevel@tonic-gate if (copyout(STRUCT_BUF(get_attribute_value), arg, 48470Sstevel@tonic-gate STRUCT_SIZE(get_attribute_value)) != 0) { 48480Sstevel@tonic-gate return (EFAULT); 48490Sstevel@tonic-gate } 48500Sstevel@tonic-gate return (0); 48510Sstevel@tonic-gate } 48520Sstevel@tonic-gate 48530Sstevel@tonic-gate /* ARGSUSED */ 48540Sstevel@tonic-gate static int 48550Sstevel@tonic-gate object_get_size(dev_t dev, caddr_t arg, int mode, int *rval) 48560Sstevel@tonic-gate { 48570Sstevel@tonic-gate STRUCT_DECL(crypto_object_get_size, object_get_size); 48580Sstevel@tonic-gate kcf_provider_desc_t *real_provider; 48590Sstevel@tonic-gate kcf_req_params_t params; 48600Sstevel@tonic-gate crypto_session_id_t session_id; 48610Sstevel@tonic-gate crypto_minor_t *cm; 48626424Skrishna crypto_session_data_t *sp = NULL; 48630Sstevel@tonic-gate crypto_object_id_t handle; 48640Sstevel@tonic-gate size_t size; 48650Sstevel@tonic-gate int error = 0; 48660Sstevel@tonic-gate int rv; 48670Sstevel@tonic-gate 48680Sstevel@tonic-gate STRUCT_INIT(object_get_size, mode); 48690Sstevel@tonic-gate 48700Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 48710Sstevel@tonic-gate cmn_err(CE_WARN, "object_get_size: failed holding minor"); 48720Sstevel@tonic-gate return (ENXIO); 48730Sstevel@tonic-gate } 48740Sstevel@tonic-gate 48750Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(object_get_size), 48760Sstevel@tonic-gate STRUCT_SIZE(object_get_size)) != 0) { 48770Sstevel@tonic-gate crypto_release_minor(cm); 48780Sstevel@tonic-gate return (EFAULT); 48790Sstevel@tonic-gate } 48800Sstevel@tonic-gate 48810Sstevel@tonic-gate session_id = STRUCT_FGET(object_get_size, gs_session); 48820Sstevel@tonic-gate 48830Sstevel@tonic-gate if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) { 48840Sstevel@tonic-gate goto release_minor; 48850Sstevel@tonic-gate } 48860Sstevel@tonic-gate 48870Sstevel@tonic-gate if ((rv = kcf_get_hardware_provider_nomech( 48880Sstevel@tonic-gate CRYPTO_OPS_OFFSET(object_ops), 4889904Smcpowers CRYPTO_OBJECT_OFFSET(object_get_size), CHECK_RESTRICT_FALSE, 48900Sstevel@tonic-gate sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) { 48916424Skrishna goto release_minor; 48920Sstevel@tonic-gate } 48930Sstevel@tonic-gate 48940Sstevel@tonic-gate handle = STRUCT_FGET(object_get_size, gs_handle); 48950Sstevel@tonic-gate KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, KCF_OP_OBJECT_GET_SIZE, 48960Sstevel@tonic-gate sp->sd_provider_session->ps_session, handle, NULL, 0, NULL, &size, 48970Sstevel@tonic-gate NULL, NULL, 0, NULL); 48980Sstevel@tonic-gate 48990Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE); 4900904Smcpowers KCF_PROV_REFRELE(real_provider); 49010Sstevel@tonic-gate 49020Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) { 49030Sstevel@tonic-gate STRUCT_FSET(object_get_size, gs_size, size); 49040Sstevel@tonic-gate } 49056424Skrishna 49060Sstevel@tonic-gate release_minor: 49070Sstevel@tonic-gate crypto_release_minor(cm); 49086424Skrishna CRYPTO_SESSION_RELE(sp); 49090Sstevel@tonic-gate 49100Sstevel@tonic-gate if (error != 0) 49110Sstevel@tonic-gate return (error); 49120Sstevel@tonic-gate 49130Sstevel@tonic-gate STRUCT_FSET(object_get_size, gs_return_value, rv); 49140Sstevel@tonic-gate if (copyout(STRUCT_BUF(object_get_size), arg, 49150Sstevel@tonic-gate STRUCT_SIZE(object_get_size)) != 0) { 49160Sstevel@tonic-gate return (EFAULT); 49170Sstevel@tonic-gate } 49180Sstevel@tonic-gate return (0); 49190Sstevel@tonic-gate } 49200Sstevel@tonic-gate 49210Sstevel@tonic-gate /* ARGSUSED */ 49220Sstevel@tonic-gate static int 49230Sstevel@tonic-gate object_set_attribute_value(dev_t dev, caddr_t arg, int mode, int *rval) 49240Sstevel@tonic-gate { 49250Sstevel@tonic-gate STRUCT_DECL(crypto_object_set_attribute_value, set_attribute_value); 49260Sstevel@tonic-gate kcf_provider_desc_t *real_provider; 49270Sstevel@tonic-gate kcf_req_params_t params; 49280Sstevel@tonic-gate crypto_object_attribute_t *k_attrs = NULL; 49290Sstevel@tonic-gate crypto_session_id_t session_id; 49300Sstevel@tonic-gate crypto_minor_t *cm; 49316424Skrishna crypto_session_data_t *sp = NULL; 49320Sstevel@tonic-gate crypto_object_id_t object_handle; 49330Sstevel@tonic-gate caddr_t sa_attributes; 49340Sstevel@tonic-gate size_t k_attrs_size; 49350Sstevel@tonic-gate size_t rctl_bytes = 0; 49366424Skrishna boolean_t rctl_chk = B_FALSE; 49370Sstevel@tonic-gate int error = 0; 49380Sstevel@tonic-gate int rv; 49390Sstevel@tonic-gate uint_t count; 49400Sstevel@tonic-gate 49410Sstevel@tonic-gate STRUCT_INIT(set_attribute_value, mode); 49420Sstevel@tonic-gate 49430Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 49440Sstevel@tonic-gate cmn_err(CE_WARN, 49450Sstevel@tonic-gate "object_set_attribute_value: failed holding minor"); 49460Sstevel@tonic-gate return (ENXIO); 49470Sstevel@tonic-gate } 49480Sstevel@tonic-gate 49490Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(set_attribute_value), 49500Sstevel@tonic-gate STRUCT_SIZE(set_attribute_value)) != 0) { 49510Sstevel@tonic-gate crypto_release_minor(cm); 49520Sstevel@tonic-gate return (EFAULT); 49530Sstevel@tonic-gate } 49540Sstevel@tonic-gate 49550Sstevel@tonic-gate count = STRUCT_FGET(set_attribute_value, sa_count); 49560Sstevel@tonic-gate sa_attributes = STRUCT_FGETP(set_attribute_value, sa_attributes); 49570Sstevel@tonic-gate 49580Sstevel@tonic-gate session_id = STRUCT_FGET(set_attribute_value, sa_session); 49590Sstevel@tonic-gate 49600Sstevel@tonic-gate if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) { 49610Sstevel@tonic-gate goto release_minor; 49620Sstevel@tonic-gate } 49636424Skrishna if (!copyin_attributes(mode, sp, count, sa_attributes, &k_attrs, 49646424Skrishna &k_attrs_size, NULL, &rv, &error, &rctl_bytes, 49656424Skrishna &rctl_chk, B_TRUE)) { 49666424Skrishna goto release_minor; 49676424Skrishna } 49680Sstevel@tonic-gate 49690Sstevel@tonic-gate if ((rv = kcf_get_hardware_provider_nomech( 49700Sstevel@tonic-gate CRYPTO_OPS_OFFSET(object_ops), 49710Sstevel@tonic-gate CRYPTO_OBJECT_OFFSET(object_set_attribute_value), 4972904Smcpowers CHECK_RESTRICT_FALSE, sp->sd_provider, &real_provider)) 4973904Smcpowers != CRYPTO_SUCCESS) { 49746424Skrishna goto release_minor; 49750Sstevel@tonic-gate } 49760Sstevel@tonic-gate 49770Sstevel@tonic-gate object_handle = STRUCT_FGET(set_attribute_value, sa_handle); 49780Sstevel@tonic-gate KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, KCF_OP_OBJECT_SET_ATTRIBUTE_VALUE, 49790Sstevel@tonic-gate sp->sd_provider_session->ps_session, object_handle, k_attrs, count, 49800Sstevel@tonic-gate NULL, 0, NULL, NULL, 0, NULL); 49810Sstevel@tonic-gate 49820Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE); 4983904Smcpowers KCF_PROV_REFRELE(real_provider); 49840Sstevel@tonic-gate 49856424Skrishna release_minor: 49866424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk); 49870Sstevel@tonic-gate CRYPTO_SESSION_RELE(sp); 49880Sstevel@tonic-gate crypto_release_minor(cm); 49890Sstevel@tonic-gate 49900Sstevel@tonic-gate if (k_attrs != NULL) 49910Sstevel@tonic-gate kmem_free(k_attrs, k_attrs_size); 49920Sstevel@tonic-gate 49930Sstevel@tonic-gate if (error != 0) 49940Sstevel@tonic-gate return (error); 49950Sstevel@tonic-gate 49960Sstevel@tonic-gate STRUCT_FSET(set_attribute_value, sa_return_value, rv); 49970Sstevel@tonic-gate if (copyout(STRUCT_BUF(set_attribute_value), arg, 49980Sstevel@tonic-gate STRUCT_SIZE(set_attribute_value)) != 0) { 49990Sstevel@tonic-gate return (EFAULT); 50000Sstevel@tonic-gate } 50010Sstevel@tonic-gate return (0); 50020Sstevel@tonic-gate } 50030Sstevel@tonic-gate 50040Sstevel@tonic-gate /* ARGSUSED */ 50050Sstevel@tonic-gate static int 50060Sstevel@tonic-gate object_find_init(dev_t dev, caddr_t arg, int mode, int *rval) 50070Sstevel@tonic-gate { 50080Sstevel@tonic-gate STRUCT_DECL(crypto_object_find_init, find_init); 5009904Smcpowers kcf_provider_desc_t *real_provider = NULL; 50100Sstevel@tonic-gate kcf_req_params_t params; 50110Sstevel@tonic-gate crypto_object_attribute_t *k_attrs = NULL; 50120Sstevel@tonic-gate crypto_session_id_t session_id; 50130Sstevel@tonic-gate crypto_minor_t *cm; 50146424Skrishna crypto_session_data_t *sp = NULL; 50150Sstevel@tonic-gate caddr_t attributes; 50160Sstevel@tonic-gate size_t k_attrs_size; 50170Sstevel@tonic-gate size_t rctl_bytes = 0; 50186424Skrishna boolean_t rctl_chk = B_FALSE; 50190Sstevel@tonic-gate int error = 0; 50200Sstevel@tonic-gate int rv; 50210Sstevel@tonic-gate uint_t count; 50220Sstevel@tonic-gate void *cookie; 50230Sstevel@tonic-gate 50240Sstevel@tonic-gate STRUCT_INIT(find_init, mode); 50250Sstevel@tonic-gate 50260Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 50270Sstevel@tonic-gate cmn_err(CE_WARN, "object_find_init: failed holding minor"); 50280Sstevel@tonic-gate return (ENXIO); 50290Sstevel@tonic-gate } 50300Sstevel@tonic-gate 50310Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(find_init), STRUCT_SIZE(find_init)) != 0) { 50320Sstevel@tonic-gate crypto_release_minor(cm); 50330Sstevel@tonic-gate return (EFAULT); 50340Sstevel@tonic-gate } 50350Sstevel@tonic-gate 50360Sstevel@tonic-gate count = STRUCT_FGET(find_init, fi_count); 50370Sstevel@tonic-gate attributes = STRUCT_FGETP(find_init, fi_attributes); 50380Sstevel@tonic-gate 50390Sstevel@tonic-gate session_id = STRUCT_FGET(find_init, fi_session); 50400Sstevel@tonic-gate 50410Sstevel@tonic-gate if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) { 50420Sstevel@tonic-gate goto release_minor; 50430Sstevel@tonic-gate } 50446424Skrishna if (!copyin_attributes(mode, sp, count, attributes, &k_attrs, 50456424Skrishna &k_attrs_size, NULL, &rv, &error, &rctl_bytes, 50466424Skrishna &rctl_chk, B_TRUE)) { 50476424Skrishna goto release_minor; 50486424Skrishna } 50490Sstevel@tonic-gate 50500Sstevel@tonic-gate if ((rv = kcf_get_hardware_provider_nomech( 50510Sstevel@tonic-gate CRYPTO_OPS_OFFSET(object_ops), 5052904Smcpowers CRYPTO_OBJECT_OFFSET(object_find_init), CHECK_RESTRICT_FALSE, 50530Sstevel@tonic-gate sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) { 50546424Skrishna goto release_minor; 50550Sstevel@tonic-gate } 50560Sstevel@tonic-gate 50570Sstevel@tonic-gate /* check for an active find */ 50580Sstevel@tonic-gate if (sp->sd_find_init_cookie != NULL) { 50590Sstevel@tonic-gate rv = CRYPTO_OPERATION_IS_ACTIVE; 50600Sstevel@tonic-gate goto release_minor; 50610Sstevel@tonic-gate } 50620Sstevel@tonic-gate 50630Sstevel@tonic-gate KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, KCF_OP_OBJECT_FIND_INIT, 50640Sstevel@tonic-gate sp->sd_provider_session->ps_session, 0, k_attrs, count, NULL, 0, 50650Sstevel@tonic-gate &cookie, NULL, 0, NULL); 50660Sstevel@tonic-gate 50670Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE); 50680Sstevel@tonic-gate 50690Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) { 50700Sstevel@tonic-gate /* 50710Sstevel@tonic-gate * The cookie is allocated by a provider at the start of an 50720Sstevel@tonic-gate * object search. It is freed when the search is terminated 50730Sstevel@tonic-gate * by a final operation, or when the session is closed. 50740Sstevel@tonic-gate * It contains state information about which object handles 50750Sstevel@tonic-gate * have been returned to the caller. 50760Sstevel@tonic-gate */ 50770Sstevel@tonic-gate sp->sd_find_init_cookie = cookie; 50780Sstevel@tonic-gate } 50790Sstevel@tonic-gate 50806424Skrishna release_minor: 50816424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk); 50820Sstevel@tonic-gate CRYPTO_SESSION_RELE(sp); 50830Sstevel@tonic-gate crypto_release_minor(cm); 50840Sstevel@tonic-gate 5085904Smcpowers if (real_provider != NULL) 5086904Smcpowers KCF_PROV_REFRELE(real_provider); 5087904Smcpowers 50880Sstevel@tonic-gate if (k_attrs != NULL) 50890Sstevel@tonic-gate kmem_free(k_attrs, k_attrs_size); 50900Sstevel@tonic-gate 50910Sstevel@tonic-gate if (error != 0) 50920Sstevel@tonic-gate return (error); 50930Sstevel@tonic-gate 50940Sstevel@tonic-gate STRUCT_FSET(find_init, fi_return_value, rv); 50950Sstevel@tonic-gate if (copyout(STRUCT_BUF(find_init), arg, STRUCT_SIZE(find_init)) != 0) { 50960Sstevel@tonic-gate return (EFAULT); 50970Sstevel@tonic-gate } 50980Sstevel@tonic-gate return (0); 50990Sstevel@tonic-gate } 51000Sstevel@tonic-gate 51010Sstevel@tonic-gate /* ARGSUSED */ 51020Sstevel@tonic-gate static int 51030Sstevel@tonic-gate object_find_update(dev_t dev, caddr_t arg, int mode, int *rval) 51040Sstevel@tonic-gate { 51050Sstevel@tonic-gate STRUCT_DECL(crypto_object_find_update, find_update); 51060Sstevel@tonic-gate kcf_provider_desc_t *real_provider; 51070Sstevel@tonic-gate kcf_req_params_t params; 51080Sstevel@tonic-gate crypto_minor_t *cm; 51096424Skrishna crypto_session_data_t *sp = NULL; 51100Sstevel@tonic-gate crypto_object_id_t *buffer = NULL; 51110Sstevel@tonic-gate crypto_session_id_t session_id; 51120Sstevel@tonic-gate size_t len, rctl_bytes = 0; 51130Sstevel@tonic-gate uint_t count, max_count; 51140Sstevel@tonic-gate int rv, error = 0; 51156424Skrishna boolean_t rctl_chk = B_FALSE; 51160Sstevel@tonic-gate 51170Sstevel@tonic-gate STRUCT_INIT(find_update, mode); 51180Sstevel@tonic-gate 51190Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 51200Sstevel@tonic-gate cmn_err(CE_WARN, "object_find_update: failed holding minor"); 51210Sstevel@tonic-gate return (ENXIO); 51220Sstevel@tonic-gate } 51230Sstevel@tonic-gate 51240Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(find_update), 51250Sstevel@tonic-gate STRUCT_SIZE(find_update)) != 0) { 51260Sstevel@tonic-gate crypto_release_minor(cm); 51270Sstevel@tonic-gate return (EFAULT); 51280Sstevel@tonic-gate } 51290Sstevel@tonic-gate 51300Sstevel@tonic-gate max_count = STRUCT_FGET(find_update, fu_max_count); 51310Sstevel@tonic-gate if (max_count > CRYPTO_MAX_FIND_COUNT) { 51320Sstevel@tonic-gate cmn_err(CE_NOTE, "object_find_update: count greater than %d, " 51330Sstevel@tonic-gate "pid = %d", CRYPTO_MAX_FIND_COUNT, curproc->p_pid); 51340Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD; 51350Sstevel@tonic-gate goto release_minor; 51360Sstevel@tonic-gate } 51370Sstevel@tonic-gate len = max_count * sizeof (crypto_object_id_t); 51386424Skrishna session_id = STRUCT_FGET(find_update, fu_session); 51396424Skrishna 51406424Skrishna if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) { 51416424Skrishna goto release_minor; 51426424Skrishna } 51436424Skrishna if ((rv = CRYPTO_BUFFER_CHECK(sp, len, rctl_chk)) != 51446424Skrishna CRYPTO_SUCCESS) { 51450Sstevel@tonic-gate goto release_minor; 51460Sstevel@tonic-gate } 51470Sstevel@tonic-gate rctl_bytes = len; 51480Sstevel@tonic-gate buffer = kmem_alloc(len, KM_SLEEP); 51490Sstevel@tonic-gate 51500Sstevel@tonic-gate if ((rv = kcf_get_hardware_provider_nomech( 51510Sstevel@tonic-gate CRYPTO_OPS_OFFSET(object_ops), 5152904Smcpowers CRYPTO_OBJECT_OFFSET(object_find), CHECK_RESTRICT_FALSE, 51530Sstevel@tonic-gate sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) { 51546424Skrishna goto release_minor; 51550Sstevel@tonic-gate } 51560Sstevel@tonic-gate 51570Sstevel@tonic-gate KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, KCF_OP_OBJECT_FIND, 51580Sstevel@tonic-gate sp->sd_provider_session->ps_session, 0, NULL, 0, buffer, 0, 51590Sstevel@tonic-gate NULL, sp->sd_find_init_cookie, max_count, &count); 51600Sstevel@tonic-gate 51610Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE); 5162904Smcpowers KCF_PROV_REFRELE(real_provider); 51630Sstevel@tonic-gate 51640Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) { 51650Sstevel@tonic-gate if (count > max_count) { 51660Sstevel@tonic-gate /* bad bad provider */ 51670Sstevel@tonic-gate rv = CRYPTO_FAILED; 51680Sstevel@tonic-gate goto release_minor; 51690Sstevel@tonic-gate } 51700Sstevel@tonic-gate if (count != 0) { 51710Sstevel@tonic-gate /* copyout handles */ 51720Sstevel@tonic-gate if (copyout(buffer, 51730Sstevel@tonic-gate STRUCT_FGETP(find_update, fu_handles), 51740Sstevel@tonic-gate count * sizeof (crypto_object_id_t)) != 0) { 51750Sstevel@tonic-gate error = EFAULT; 51760Sstevel@tonic-gate } 51770Sstevel@tonic-gate } 51780Sstevel@tonic-gate STRUCT_FSET(find_update, fu_count, count); 51790Sstevel@tonic-gate } 51800Sstevel@tonic-gate 51810Sstevel@tonic-gate release_minor: 51826424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk); 51836424Skrishna CRYPTO_SESSION_RELE(sp); 51840Sstevel@tonic-gate crypto_release_minor(cm); 51850Sstevel@tonic-gate 51860Sstevel@tonic-gate if (buffer != NULL) 51870Sstevel@tonic-gate kmem_free(buffer, len); 51880Sstevel@tonic-gate 51890Sstevel@tonic-gate if (error != 0) 51900Sstevel@tonic-gate return (error); 51910Sstevel@tonic-gate 51920Sstevel@tonic-gate STRUCT_FSET(find_update, fu_return_value, rv); 51930Sstevel@tonic-gate if (copyout(STRUCT_BUF(find_update), arg, 51940Sstevel@tonic-gate STRUCT_SIZE(find_update)) != 0) { 51950Sstevel@tonic-gate return (EFAULT); 51960Sstevel@tonic-gate } 51970Sstevel@tonic-gate 51980Sstevel@tonic-gate return (0); 51990Sstevel@tonic-gate } 52000Sstevel@tonic-gate 52010Sstevel@tonic-gate /* 52020Sstevel@tonic-gate * Free provider-allocated storage used for find object searches. 52030Sstevel@tonic-gate */ 52040Sstevel@tonic-gate static int 52050Sstevel@tonic-gate crypto_free_find_ctx(crypto_session_data_t *sp) 52060Sstevel@tonic-gate { 52070Sstevel@tonic-gate kcf_provider_desc_t *real_provider; 52080Sstevel@tonic-gate kcf_req_params_t params; 52090Sstevel@tonic-gate int rv; 52100Sstevel@tonic-gate 52110Sstevel@tonic-gate if ((rv = kcf_get_hardware_provider_nomech( 52120Sstevel@tonic-gate CRYPTO_OPS_OFFSET(object_ops), 5213904Smcpowers CRYPTO_OBJECT_OFFSET(object_find_final), CHECK_RESTRICT_FALSE, 52140Sstevel@tonic-gate sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) { 52150Sstevel@tonic-gate return (rv); 52160Sstevel@tonic-gate } 52170Sstevel@tonic-gate 52180Sstevel@tonic-gate KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, KCF_OP_OBJECT_FIND_FINAL, 52190Sstevel@tonic-gate sp->sd_provider_session->ps_session, 0, NULL, 0, NULL, 0, 52200Sstevel@tonic-gate NULL, sp->sd_find_init_cookie, 0, NULL); 52210Sstevel@tonic-gate 5222904Smcpowers rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE); 5223904Smcpowers KCF_PROV_REFRELE(real_provider); 5224904Smcpowers return (rv); 52250Sstevel@tonic-gate } 52260Sstevel@tonic-gate 52270Sstevel@tonic-gate /* ARGSUSED */ 52280Sstevel@tonic-gate static int 52290Sstevel@tonic-gate object_find_final(dev_t dev, caddr_t arg, int mode, int *rval) 52300Sstevel@tonic-gate { 52310Sstevel@tonic-gate STRUCT_DECL(crypto_object_find_final, object_find_final); 52320Sstevel@tonic-gate crypto_session_id_t session_id; 52330Sstevel@tonic-gate crypto_minor_t *cm; 52340Sstevel@tonic-gate crypto_session_data_t *sp; 52350Sstevel@tonic-gate int error = 0; 52360Sstevel@tonic-gate int rv; 52370Sstevel@tonic-gate 52380Sstevel@tonic-gate STRUCT_INIT(object_find_final, mode); 52390Sstevel@tonic-gate 52400Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 52410Sstevel@tonic-gate cmn_err(CE_WARN, "object_find_final: failed holding minor"); 52420Sstevel@tonic-gate return (ENXIO); 52430Sstevel@tonic-gate } 52440Sstevel@tonic-gate 52450Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(object_find_final), 52460Sstevel@tonic-gate STRUCT_SIZE(object_find_final)) != 0) { 52470Sstevel@tonic-gate crypto_release_minor(cm); 52480Sstevel@tonic-gate return (EFAULT); 52490Sstevel@tonic-gate } 52500Sstevel@tonic-gate 52510Sstevel@tonic-gate session_id = STRUCT_FGET(object_find_final, ff_session); 52520Sstevel@tonic-gate 52530Sstevel@tonic-gate if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) { 52540Sstevel@tonic-gate goto release_minor; 52550Sstevel@tonic-gate } 52560Sstevel@tonic-gate 52570Sstevel@tonic-gate if ((rv = crypto_free_find_ctx(sp)) == CRYPTO_SUCCESS) { 52580Sstevel@tonic-gate sp->sd_find_init_cookie = NULL; 52590Sstevel@tonic-gate } 52600Sstevel@tonic-gate 52610Sstevel@tonic-gate CRYPTO_SESSION_RELE(sp); 52620Sstevel@tonic-gate 52630Sstevel@tonic-gate release_minor: 52640Sstevel@tonic-gate crypto_release_minor(cm); 52650Sstevel@tonic-gate 52660Sstevel@tonic-gate if (error != 0) 52670Sstevel@tonic-gate return (error); 52680Sstevel@tonic-gate 52690Sstevel@tonic-gate STRUCT_FSET(object_find_final, ff_return_value, rv); 52700Sstevel@tonic-gate 52710Sstevel@tonic-gate if (copyout(STRUCT_BUF(object_find_final), arg, 52720Sstevel@tonic-gate STRUCT_SIZE(object_find_final)) != 0) { 52730Sstevel@tonic-gate return (EFAULT); 52740Sstevel@tonic-gate } 52750Sstevel@tonic-gate return (0); 52760Sstevel@tonic-gate } 52770Sstevel@tonic-gate 52780Sstevel@tonic-gate /* ARGSUSED */ 52790Sstevel@tonic-gate static int 52800Sstevel@tonic-gate object_generate_key(dev_t dev, caddr_t arg, int mode, int *rval) 52810Sstevel@tonic-gate { 52820Sstevel@tonic-gate STRUCT_DECL(crypto_object_generate_key, generate_key); 5283904Smcpowers kcf_provider_desc_t *real_provider = NULL; 52840Sstevel@tonic-gate kcf_req_params_t params; 52850Sstevel@tonic-gate crypto_mechanism_t mech; 52860Sstevel@tonic-gate crypto_object_attribute_t *k_attrs = NULL; 52870Sstevel@tonic-gate crypto_session_id_t session_id; 52880Sstevel@tonic-gate crypto_minor_t *cm; 52890Sstevel@tonic-gate crypto_session_data_t *sp = NULL; 52900Sstevel@tonic-gate crypto_object_id_t key_handle; 52910Sstevel@tonic-gate caddr_t attributes; 52920Sstevel@tonic-gate size_t k_attrs_size; 52930Sstevel@tonic-gate size_t mech_rctl_bytes = 0, key_rctl_bytes = 0; 52946424Skrishna boolean_t mech_rctl_chk = B_FALSE; 52956424Skrishna boolean_t key_rctl_chk = B_FALSE; 52960Sstevel@tonic-gate uint_t count; 52970Sstevel@tonic-gate int error = 0; 52980Sstevel@tonic-gate int rv; 5299904Smcpowers boolean_t allocated_by_crypto_module = B_FALSE; 53000Sstevel@tonic-gate 53010Sstevel@tonic-gate STRUCT_INIT(generate_key, mode); 53020Sstevel@tonic-gate 53030Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 53040Sstevel@tonic-gate cmn_err(CE_WARN, "object_generate_key: failed holding minor"); 53050Sstevel@tonic-gate return (ENXIO); 53060Sstevel@tonic-gate } 53070Sstevel@tonic-gate 53080Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(generate_key), 53090Sstevel@tonic-gate STRUCT_SIZE(generate_key)) != 0) { 53100Sstevel@tonic-gate crypto_release_minor(cm); 53110Sstevel@tonic-gate return (EFAULT); 53120Sstevel@tonic-gate } 53130Sstevel@tonic-gate 53140Sstevel@tonic-gate session_id = STRUCT_FGET(generate_key, gk_session); 53150Sstevel@tonic-gate 53160Sstevel@tonic-gate if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) { 53170Sstevel@tonic-gate goto release_minor; 53180Sstevel@tonic-gate } 53190Sstevel@tonic-gate 5320904Smcpowers bcopy(STRUCT_FADDR(generate_key, gk_mechanism), &mech.cm_type, 5321904Smcpowers sizeof (crypto_mech_type_t)); 5322904Smcpowers 532310444SVladimir.Kotal@Sun.COM if ((rv = kcf_get_hardware_provider(mech.cm_type, NULL, 532410444SVladimir.Kotal@Sun.COM CRYPTO_MECH_INVALID, NULL, CHECK_RESTRICT_FALSE, sp->sd_provider, 532510444SVladimir.Kotal@Sun.COM &real_provider, CRYPTO_FG_GENERATE)) != CRYPTO_SUCCESS) { 53260Sstevel@tonic-gate goto release_minor; 53270Sstevel@tonic-gate } 53280Sstevel@tonic-gate 5329904Smcpowers rv = crypto_provider_copyin_mech_param(real_provider, 5330904Smcpowers STRUCT_FADDR(generate_key, gk_mechanism), &mech, mode, &error); 5331904Smcpowers 5332904Smcpowers if (rv == CRYPTO_NOT_SUPPORTED) { 5333904Smcpowers allocated_by_crypto_module = B_TRUE; 53346424Skrishna if (!copyin_mech(mode, sp, 53356424Skrishna STRUCT_FADDR(generate_key, gk_mechanism), 53366424Skrishna &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) { 5337904Smcpowers goto release_minor; 5338904Smcpowers } 5339904Smcpowers } else { 5340904Smcpowers if (rv != CRYPTO_SUCCESS) 5341904Smcpowers goto release_minor; 53420Sstevel@tonic-gate } 53430Sstevel@tonic-gate 53440Sstevel@tonic-gate count = STRUCT_FGET(generate_key, gk_count); 53450Sstevel@tonic-gate attributes = STRUCT_FGETP(generate_key, gk_attributes); 53466424Skrishna if (!copyin_attributes(mode, sp, count, attributes, &k_attrs, 53476424Skrishna &k_attrs_size, NULL, &rv, &error, &key_rctl_bytes, 53486424Skrishna &key_rctl_chk, B_TRUE)) { 53490Sstevel@tonic-gate goto release_minor; 53500Sstevel@tonic-gate } 53510Sstevel@tonic-gate 53520Sstevel@tonic-gate KCF_WRAP_KEY_OPS_PARAMS(¶ms, KCF_OP_KEY_GENERATE, 53530Sstevel@tonic-gate sp->sd_provider_session->ps_session, &mech, k_attrs, count, 53540Sstevel@tonic-gate &key_handle, NULL, 0, NULL, NULL, NULL, 0); 53550Sstevel@tonic-gate 53560Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE); 53570Sstevel@tonic-gate 53580Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) 53590Sstevel@tonic-gate STRUCT_FSET(generate_key, gk_handle, key_handle); 53600Sstevel@tonic-gate 53610Sstevel@tonic-gate release_minor: 53626424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk); 53636424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk); 53640Sstevel@tonic-gate 53650Sstevel@tonic-gate if (k_attrs != NULL) 53660Sstevel@tonic-gate kmem_free(k_attrs, k_attrs_size); 53670Sstevel@tonic-gate 53680Sstevel@tonic-gate if (error != 0) 53690Sstevel@tonic-gate goto out; 53700Sstevel@tonic-gate 53710Sstevel@tonic-gate STRUCT_FSET(generate_key, gk_return_value, rv); 53720Sstevel@tonic-gate if (copyout(STRUCT_BUF(generate_key), arg, 53730Sstevel@tonic-gate STRUCT_SIZE(generate_key)) != 0) { 53740Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) { 53750Sstevel@tonic-gate KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, 53760Sstevel@tonic-gate KCF_OP_OBJECT_DESTROY, 53770Sstevel@tonic-gate sp->sd_provider_session->ps_session, key_handle, 53780Sstevel@tonic-gate NULL, 0, NULL, 0, NULL, NULL, 0, NULL); 53790Sstevel@tonic-gate 53800Sstevel@tonic-gate (void) kcf_submit_request(real_provider, NULL, 53810Sstevel@tonic-gate NULL, ¶ms, B_FALSE); 53820Sstevel@tonic-gate 53830Sstevel@tonic-gate error = EFAULT; 53840Sstevel@tonic-gate } 53850Sstevel@tonic-gate } 53860Sstevel@tonic-gate out: 53876424Skrishna CRYPTO_SESSION_RELE(sp); 53880Sstevel@tonic-gate crypto_release_minor(cm); 5389904Smcpowers 5390904Smcpowers if (real_provider != NULL) { 5391904Smcpowers crypto_free_mech(real_provider, 5392904Smcpowers allocated_by_crypto_module, &mech); 5393904Smcpowers KCF_PROV_REFRELE(real_provider); 5394904Smcpowers } 53950Sstevel@tonic-gate return (error); 53960Sstevel@tonic-gate } 53970Sstevel@tonic-gate 53980Sstevel@tonic-gate /* ARGSUSED */ 53990Sstevel@tonic-gate static int 54004219Smcpowers nostore_generate_key(dev_t dev, caddr_t arg, int mode, int *rval) 54014219Smcpowers { 54024219Smcpowers STRUCT_DECL(crypto_nostore_generate_key, generate_key); 54034219Smcpowers /* LINTED E_FUNC_SET_NOT_USED */ 54044219Smcpowers STRUCT_DECL(crypto_object_attribute, oa); 54054219Smcpowers kcf_provider_desc_t *real_provider = NULL; 54064219Smcpowers kcf_req_params_t params; 54074219Smcpowers crypto_mechanism_t mech; 54084219Smcpowers crypto_object_attribute_t *k_in_attrs = NULL; 54094219Smcpowers crypto_object_attribute_t *k_out_attrs = NULL; 54104219Smcpowers crypto_session_id_t session_id; 54114219Smcpowers crypto_minor_t *cm; 54124219Smcpowers crypto_session_data_t *sp = NULL; 54134219Smcpowers caddr_t in_attributes; 54144219Smcpowers caddr_t out_attributes; 54154219Smcpowers size_t k_in_attrs_size; 54164219Smcpowers size_t k_out_attrs_size; 54174219Smcpowers size_t mech_rctl_bytes = 0; 54186424Skrishna boolean_t mech_rctl_chk = B_FALSE; 54194219Smcpowers size_t in_key_rctl_bytes = 0, out_key_rctl_bytes = 0; 54206424Skrishna boolean_t in_key_rctl_chk = B_FALSE; 54216424Skrishna boolean_t out_key_rctl_chk = B_FALSE; 54224219Smcpowers uint_t in_count; 54234219Smcpowers uint_t out_count; 54244219Smcpowers int error = 0; 54254219Smcpowers int rv; 54264219Smcpowers boolean_t allocated_by_crypto_module = B_FALSE; 54274219Smcpowers caddr_t u_attrs = NULL; 54284219Smcpowers 54294219Smcpowers STRUCT_INIT(generate_key, mode); 54304219Smcpowers STRUCT_INIT(oa, mode); 54314219Smcpowers 54324219Smcpowers if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 54334219Smcpowers cmn_err(CE_WARN, "nostore_generate_key: failed holding minor"); 54344219Smcpowers return (ENXIO); 54354219Smcpowers } 54364219Smcpowers 54374219Smcpowers if (copyin(arg, STRUCT_BUF(generate_key), 54384219Smcpowers STRUCT_SIZE(generate_key)) != 0) { 54394219Smcpowers crypto_release_minor(cm); 54404219Smcpowers return (EFAULT); 54414219Smcpowers } 54424219Smcpowers 54434219Smcpowers session_id = STRUCT_FGET(generate_key, ngk_session); 54444219Smcpowers 54454219Smcpowers if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) { 54464219Smcpowers goto release_minor; 54474219Smcpowers } 54484219Smcpowers 54494219Smcpowers bcopy(STRUCT_FADDR(generate_key, ngk_mechanism), &mech.cm_type, 54504219Smcpowers sizeof (crypto_mech_type_t)); 54514219Smcpowers 545210444SVladimir.Kotal@Sun.COM if ((rv = kcf_get_hardware_provider(mech.cm_type, NULL, 545310444SVladimir.Kotal@Sun.COM CRYPTO_MECH_INVALID, NULL, CHECK_RESTRICT_FALSE, sp->sd_provider, 545410444SVladimir.Kotal@Sun.COM &real_provider, CRYPTO_FG_GENERATE)) != CRYPTO_SUCCESS) { 54554219Smcpowers goto release_minor; 54564219Smcpowers } 54574219Smcpowers 54584219Smcpowers rv = crypto_provider_copyin_mech_param(real_provider, 54594219Smcpowers STRUCT_FADDR(generate_key, ngk_mechanism), &mech, mode, &error); 54604219Smcpowers 54614219Smcpowers if (rv == CRYPTO_NOT_SUPPORTED) { 54624219Smcpowers allocated_by_crypto_module = B_TRUE; 54636424Skrishna if (!copyin_mech(mode, sp, STRUCT_FADDR(generate_key, 54646424Skrishna ngk_mechanism), &mech, &mech_rctl_bytes, 54656424Skrishna &mech_rctl_chk, &rv, &error)) { 54664219Smcpowers goto release_minor; 54674219Smcpowers } 54684219Smcpowers } else { 54694219Smcpowers if (rv != CRYPTO_SUCCESS) 54704219Smcpowers goto release_minor; 54714219Smcpowers } 54724219Smcpowers 54734219Smcpowers in_count = STRUCT_FGET(generate_key, ngk_in_count); 54744219Smcpowers in_attributes = STRUCT_FGETP(generate_key, ngk_in_attributes); 54756424Skrishna if (!copyin_attributes(mode, sp, in_count, in_attributes, &k_in_attrs, 54764219Smcpowers &k_in_attrs_size, NULL, &rv, &error, &in_key_rctl_bytes, 54776424Skrishna &in_key_rctl_chk, B_TRUE)) { 54784219Smcpowers goto release_minor; 54794219Smcpowers } 54804219Smcpowers 54814219Smcpowers out_count = STRUCT_FGET(generate_key, ngk_out_count); 54824219Smcpowers out_attributes = STRUCT_FGETP(generate_key, ngk_out_attributes); 54836424Skrishna if (!copyin_attributes(mode, sp, out_count, out_attributes, 54846424Skrishna &k_out_attrs, 54854219Smcpowers &k_out_attrs_size, &u_attrs, &rv, &error, &out_key_rctl_bytes, 54866424Skrishna &out_key_rctl_chk, B_FALSE)) { 54874219Smcpowers goto release_minor; 54884219Smcpowers } 54894219Smcpowers 54904219Smcpowers KCF_WRAP_NOSTORE_KEY_OPS_PARAMS(¶ms, KCF_OP_KEY_GENERATE, 54914219Smcpowers sp->sd_provider_session->ps_session, &mech, k_in_attrs, in_count, 54924219Smcpowers NULL, 0, NULL, k_out_attrs, out_count, NULL, 0); 54934219Smcpowers 54944219Smcpowers rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE); 54954219Smcpowers 54964219Smcpowers if (rv == CRYPTO_SUCCESS) { 54974219Smcpowers error = copyout_attributes(mode, out_attributes, 54984219Smcpowers out_count, k_out_attrs, u_attrs); 54994219Smcpowers } 55004219Smcpowers release_minor: 55016424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk); 55026424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, in_key_rctl_bytes, in_key_rctl_chk); 55036424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, out_key_rctl_bytes, 55046424Skrishna out_key_rctl_chk); 55054219Smcpowers 55064219Smcpowers if (k_in_attrs != NULL) 55074219Smcpowers kmem_free(k_in_attrs, k_in_attrs_size); 55084219Smcpowers if (k_out_attrs != NULL) { 55094219Smcpowers bzero(k_out_attrs, k_out_attrs_size); 55104219Smcpowers kmem_free(k_out_attrs, k_out_attrs_size); 55114219Smcpowers } 55124219Smcpowers 55134219Smcpowers if (u_attrs != NULL) 55144219Smcpowers kmem_free(u_attrs, out_count * STRUCT_SIZE(oa)); 55154219Smcpowers 55164219Smcpowers if (error != 0) 55174219Smcpowers goto out; 55184219Smcpowers 55194219Smcpowers STRUCT_FSET(generate_key, ngk_return_value, rv); 55204219Smcpowers if (copyout(STRUCT_BUF(generate_key), arg, 55214219Smcpowers STRUCT_SIZE(generate_key)) != 0) { 55224219Smcpowers error = EFAULT; 55234219Smcpowers } 55244219Smcpowers out: 55256424Skrishna CRYPTO_SESSION_RELE(sp); 55264219Smcpowers crypto_release_minor(cm); 55274219Smcpowers 55284219Smcpowers if (real_provider != NULL) { 55294219Smcpowers crypto_free_mech(real_provider, 55304219Smcpowers allocated_by_crypto_module, &mech); 55314219Smcpowers KCF_PROV_REFRELE(real_provider); 55324219Smcpowers } 55334219Smcpowers return (error); 55344219Smcpowers } 55354219Smcpowers 55364219Smcpowers /* ARGSUSED */ 55374219Smcpowers static int 55380Sstevel@tonic-gate object_generate_key_pair(dev_t dev, caddr_t arg, int mode, int *rval) 55390Sstevel@tonic-gate { 55400Sstevel@tonic-gate STRUCT_DECL(crypto_object_generate_key_pair, generate_key_pair); 5541904Smcpowers kcf_provider_desc_t *real_provider = NULL; 55420Sstevel@tonic-gate kcf_req_params_t params; 55430Sstevel@tonic-gate crypto_mechanism_t mech; 55440Sstevel@tonic-gate crypto_object_attribute_t *k_pub_attrs = NULL; 55450Sstevel@tonic-gate crypto_object_attribute_t *k_pri_attrs = NULL; 55460Sstevel@tonic-gate crypto_session_id_t session_id; 55470Sstevel@tonic-gate crypto_minor_t *cm; 55480Sstevel@tonic-gate crypto_session_data_t *sp = NULL; 55490Sstevel@tonic-gate crypto_object_id_t pub_handle; 55500Sstevel@tonic-gate crypto_object_id_t pri_handle; 55510Sstevel@tonic-gate caddr_t pri_attributes; 55520Sstevel@tonic-gate caddr_t pub_attributes; 55530Sstevel@tonic-gate size_t k_pub_attrs_size, k_pri_attrs_size; 55540Sstevel@tonic-gate size_t mech_rctl_bytes = 0; 55556424Skrishna boolean_t mech_rctl_chk = B_FALSE; 55560Sstevel@tonic-gate size_t pub_rctl_bytes = 0; 55576424Skrishna boolean_t pub_rctl_chk = B_FALSE; 55580Sstevel@tonic-gate size_t pri_rctl_bytes = 0; 55596424Skrishna boolean_t pri_rctl_chk = B_FALSE; 55600Sstevel@tonic-gate uint_t pub_count; 55610Sstevel@tonic-gate uint_t pri_count; 55620Sstevel@tonic-gate int error = 0; 55630Sstevel@tonic-gate int rv; 5564904Smcpowers boolean_t allocated_by_crypto_module = B_FALSE; 55650Sstevel@tonic-gate 55660Sstevel@tonic-gate STRUCT_INIT(generate_key_pair, mode); 55670Sstevel@tonic-gate 55680Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 55690Sstevel@tonic-gate cmn_err(CE_WARN, 55700Sstevel@tonic-gate "object_generate_key_pair: failed holding minor"); 55710Sstevel@tonic-gate return (ENXIO); 55720Sstevel@tonic-gate } 55730Sstevel@tonic-gate 55740Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(generate_key_pair), 55750Sstevel@tonic-gate STRUCT_SIZE(generate_key_pair)) != 0) { 55760Sstevel@tonic-gate crypto_release_minor(cm); 55770Sstevel@tonic-gate return (EFAULT); 55780Sstevel@tonic-gate } 55790Sstevel@tonic-gate 55800Sstevel@tonic-gate session_id = STRUCT_FGET(generate_key_pair, kp_session); 55810Sstevel@tonic-gate 55820Sstevel@tonic-gate if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) { 55830Sstevel@tonic-gate goto release_minor; 55840Sstevel@tonic-gate } 55850Sstevel@tonic-gate 5586904Smcpowers bcopy(STRUCT_FADDR(generate_key_pair, kp_mechanism), &mech.cm_type, 5587904Smcpowers sizeof (crypto_mech_type_t)); 5588904Smcpowers 558910444SVladimir.Kotal@Sun.COM if ((rv = kcf_get_hardware_provider(mech.cm_type, NULL, 559010444SVladimir.Kotal@Sun.COM CRYPTO_MECH_INVALID, NULL, CHECK_RESTRICT_FALSE, sp->sd_provider, 559110444SVladimir.Kotal@Sun.COM &real_provider, CRYPTO_FG_GENERATE_KEY_PAIR)) != CRYPTO_SUCCESS) { 55920Sstevel@tonic-gate goto release_minor; 55930Sstevel@tonic-gate } 55940Sstevel@tonic-gate 5595904Smcpowers rv = crypto_provider_copyin_mech_param(real_provider, 5596904Smcpowers STRUCT_FADDR(generate_key_pair, kp_mechanism), &mech, mode, &error); 5597904Smcpowers 5598904Smcpowers if (rv == CRYPTO_NOT_SUPPORTED) { 5599904Smcpowers allocated_by_crypto_module = B_TRUE; 56006424Skrishna if (!copyin_mech(mode, sp, STRUCT_FADDR(generate_key_pair, 56016424Skrishna kp_mechanism), &mech, &mech_rctl_bytes, 56026424Skrishna &mech_rctl_chk, &rv, &error)) { 5603904Smcpowers goto release_minor; 5604904Smcpowers } 5605904Smcpowers } else { 5606904Smcpowers if (rv != CRYPTO_SUCCESS) 5607904Smcpowers goto release_minor; 56080Sstevel@tonic-gate } 56090Sstevel@tonic-gate 56100Sstevel@tonic-gate pub_count = STRUCT_FGET(generate_key_pair, kp_public_count); 56110Sstevel@tonic-gate pri_count = STRUCT_FGET(generate_key_pair, kp_private_count); 56120Sstevel@tonic-gate 56130Sstevel@tonic-gate pub_attributes = STRUCT_FGETP(generate_key_pair, kp_public_attributes); 56146424Skrishna if (!copyin_attributes(mode, sp, pub_count, pub_attributes, 56156424Skrishna &k_pub_attrs, &k_pub_attrs_size, NULL, &rv, &error, &pub_rctl_bytes, 56166424Skrishna &pub_rctl_chk, B_TRUE)) { 56170Sstevel@tonic-gate goto release_minor; 56180Sstevel@tonic-gate } 56190Sstevel@tonic-gate 56200Sstevel@tonic-gate pri_attributes = STRUCT_FGETP(generate_key_pair, kp_private_attributes); 56216424Skrishna if (!copyin_attributes(mode, sp, pri_count, pri_attributes, 56226424Skrishna &k_pri_attrs, &k_pri_attrs_size, NULL, &rv, &error, 56236424Skrishna &pri_rctl_bytes, &pri_rctl_chk, B_TRUE)) { 56240Sstevel@tonic-gate goto release_minor; 56250Sstevel@tonic-gate } 56260Sstevel@tonic-gate 56270Sstevel@tonic-gate KCF_WRAP_KEY_OPS_PARAMS(¶ms, KCF_OP_KEY_GENERATE_PAIR, 56280Sstevel@tonic-gate sp->sd_provider_session->ps_session, &mech, k_pub_attrs, 56290Sstevel@tonic-gate pub_count, &pub_handle, k_pri_attrs, pri_count, &pri_handle, 56300Sstevel@tonic-gate NULL, NULL, 0); 56310Sstevel@tonic-gate 56320Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE); 56330Sstevel@tonic-gate 56340Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) { 56350Sstevel@tonic-gate STRUCT_FSET(generate_key_pair, kp_public_handle, pub_handle); 56360Sstevel@tonic-gate STRUCT_FSET(generate_key_pair, kp_private_handle, pri_handle); 56370Sstevel@tonic-gate } 56380Sstevel@tonic-gate 56390Sstevel@tonic-gate release_minor: 56406424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk); 56416424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, pub_rctl_bytes, pub_rctl_chk); 56426424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, pri_rctl_bytes, pri_rctl_chk); 56430Sstevel@tonic-gate 56440Sstevel@tonic-gate if (k_pub_attrs != NULL) 56450Sstevel@tonic-gate kmem_free(k_pub_attrs, k_pub_attrs_size); 56460Sstevel@tonic-gate 56470Sstevel@tonic-gate if (k_pri_attrs != NULL) 56480Sstevel@tonic-gate kmem_free(k_pri_attrs, k_pri_attrs_size); 56490Sstevel@tonic-gate 56500Sstevel@tonic-gate if (error != 0) 56510Sstevel@tonic-gate goto out; 56520Sstevel@tonic-gate 56530Sstevel@tonic-gate STRUCT_FSET(generate_key_pair, kp_return_value, rv); 56540Sstevel@tonic-gate if (copyout(STRUCT_BUF(generate_key_pair), arg, 56550Sstevel@tonic-gate STRUCT_SIZE(generate_key_pair)) != 0) { 56560Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) { 56570Sstevel@tonic-gate KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, 56580Sstevel@tonic-gate KCF_OP_OBJECT_DESTROY, 56590Sstevel@tonic-gate sp->sd_provider_session->ps_session, pub_handle, 56600Sstevel@tonic-gate NULL, 0, NULL, 0, NULL, NULL, 0, NULL); 56610Sstevel@tonic-gate 56620Sstevel@tonic-gate (void) kcf_submit_request(real_provider, NULL, 56630Sstevel@tonic-gate NULL, ¶ms, B_FALSE); 56640Sstevel@tonic-gate 56650Sstevel@tonic-gate KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, 56660Sstevel@tonic-gate KCF_OP_OBJECT_DESTROY, 56670Sstevel@tonic-gate sp->sd_provider_session->ps_session, pri_handle, 56680Sstevel@tonic-gate NULL, 0, NULL, 0, NULL, NULL, 0, NULL); 56690Sstevel@tonic-gate 56700Sstevel@tonic-gate (void) kcf_submit_request(real_provider, NULL, 56710Sstevel@tonic-gate NULL, ¶ms, B_FALSE); 56720Sstevel@tonic-gate 56730Sstevel@tonic-gate error = EFAULT; 56740Sstevel@tonic-gate } 56750Sstevel@tonic-gate } 56760Sstevel@tonic-gate out: 56776424Skrishna CRYPTO_SESSION_RELE(sp); 56780Sstevel@tonic-gate crypto_release_minor(cm); 5679904Smcpowers 5680904Smcpowers if (real_provider != NULL) { 5681904Smcpowers crypto_free_mech(real_provider, 5682904Smcpowers allocated_by_crypto_module, &mech); 5683904Smcpowers KCF_PROV_REFRELE(real_provider); 5684904Smcpowers } 56850Sstevel@tonic-gate return (error); 56860Sstevel@tonic-gate } 56870Sstevel@tonic-gate 56880Sstevel@tonic-gate /* ARGSUSED */ 56890Sstevel@tonic-gate static int 56904219Smcpowers nostore_generate_key_pair(dev_t dev, caddr_t arg, int mode, int *rval) 56914219Smcpowers { 56924219Smcpowers STRUCT_DECL(crypto_nostore_generate_key_pair, generate_key_pair); 56934219Smcpowers /* LINTED E_FUNC_SET_NOT_USED */ 56944219Smcpowers STRUCT_DECL(crypto_object_attribute, oa); 56954219Smcpowers kcf_provider_desc_t *real_provider = NULL; 56964219Smcpowers kcf_req_params_t params; 56974219Smcpowers crypto_mechanism_t mech; 56984219Smcpowers crypto_object_attribute_t *k_in_pub_attrs = NULL; 56994219Smcpowers crypto_object_attribute_t *k_in_pri_attrs = NULL; 57004219Smcpowers crypto_object_attribute_t *k_out_pub_attrs = NULL; 57014219Smcpowers crypto_object_attribute_t *k_out_pri_attrs = NULL; 57024219Smcpowers crypto_session_id_t session_id; 57034219Smcpowers crypto_minor_t *cm; 57044219Smcpowers crypto_session_data_t *sp = NULL; 57054219Smcpowers caddr_t in_pri_attributes; 57064219Smcpowers caddr_t in_pub_attributes; 57074219Smcpowers caddr_t out_pri_attributes; 57084219Smcpowers caddr_t out_pub_attributes; 57094219Smcpowers size_t k_in_pub_attrs_size, k_in_pri_attrs_size; 57104219Smcpowers size_t k_out_pub_attrs_size, k_out_pri_attrs_size; 57114219Smcpowers size_t mech_rctl_bytes = 0; 57126424Skrishna boolean_t mech_rctl_chk = B_FALSE; 57134219Smcpowers size_t in_pub_rctl_bytes = 0; 57146424Skrishna boolean_t in_pub_rctl_chk = B_FALSE; 57154219Smcpowers size_t in_pri_rctl_bytes = 0; 57166424Skrishna boolean_t in_pri_rctl_chk = B_FALSE; 57174219Smcpowers size_t out_pub_rctl_bytes = 0; 57186424Skrishna boolean_t out_pub_rctl_chk = B_FALSE; 57194219Smcpowers size_t out_pri_rctl_bytes = 0; 57206424Skrishna boolean_t out_pri_rctl_chk = B_FALSE; 57214219Smcpowers uint_t in_pub_count; 57224219Smcpowers uint_t in_pri_count; 57234219Smcpowers uint_t out_pub_count; 57244219Smcpowers uint_t out_pri_count; 57254219Smcpowers int error = 0; 57264219Smcpowers int rv; 57274219Smcpowers boolean_t allocated_by_crypto_module = B_FALSE; 57284219Smcpowers caddr_t u_pub_attrs = NULL; 57294219Smcpowers caddr_t u_pri_attrs = NULL; 57304219Smcpowers 57314219Smcpowers STRUCT_INIT(generate_key_pair, mode); 57324219Smcpowers STRUCT_INIT(oa, mode); 57334219Smcpowers 57344219Smcpowers if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 57354219Smcpowers cmn_err(CE_WARN, 57364219Smcpowers "nostore_generate_key_pair: failed holding minor"); 57374219Smcpowers return (ENXIO); 57384219Smcpowers } 57394219Smcpowers 57404219Smcpowers if (copyin(arg, STRUCT_BUF(generate_key_pair), 57414219Smcpowers STRUCT_SIZE(generate_key_pair)) != 0) { 57424219Smcpowers crypto_release_minor(cm); 57434219Smcpowers return (EFAULT); 57444219Smcpowers } 57454219Smcpowers 57464219Smcpowers session_id = STRUCT_FGET(generate_key_pair, nkp_session); 57474219Smcpowers 57484219Smcpowers if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) { 57494219Smcpowers goto release_minor; 57504219Smcpowers } 57514219Smcpowers 57524219Smcpowers bcopy(STRUCT_FADDR(generate_key_pair, nkp_mechanism), &mech.cm_type, 57534219Smcpowers sizeof (crypto_mech_type_t)); 57544219Smcpowers 575510444SVladimir.Kotal@Sun.COM if ((rv = kcf_get_hardware_provider(mech.cm_type, NULL, 575610444SVladimir.Kotal@Sun.COM CRYPTO_MECH_INVALID, NULL, CHECK_RESTRICT_FALSE, sp->sd_provider, 575710444SVladimir.Kotal@Sun.COM &real_provider, CRYPTO_FG_GENERATE_KEY_PAIR)) != CRYPTO_SUCCESS) { 57584219Smcpowers goto release_minor; 57594219Smcpowers } 57604219Smcpowers 57614219Smcpowers rv = crypto_provider_copyin_mech_param(real_provider, 57624219Smcpowers STRUCT_FADDR(generate_key_pair, nkp_mechanism), &mech, mode, 57634219Smcpowers &error); 57644219Smcpowers 57654219Smcpowers if (rv == CRYPTO_NOT_SUPPORTED) { 57664219Smcpowers allocated_by_crypto_module = B_TRUE; 57676424Skrishna if (!copyin_mech(mode, sp, STRUCT_FADDR(generate_key_pair, 57686424Skrishna nkp_mechanism), &mech, &mech_rctl_bytes, 57696424Skrishna &mech_rctl_chk, &rv, &error)) { 57704219Smcpowers goto release_minor; 57714219Smcpowers } 57724219Smcpowers } else { 57734219Smcpowers if (rv != CRYPTO_SUCCESS) 57744219Smcpowers goto release_minor; 57754219Smcpowers } 57764219Smcpowers 57774219Smcpowers in_pub_count = STRUCT_FGET(generate_key_pair, nkp_in_public_count); 57784219Smcpowers in_pri_count = STRUCT_FGET(generate_key_pair, nkp_in_private_count); 57794219Smcpowers 57804219Smcpowers in_pub_attributes = STRUCT_FGETP(generate_key_pair, 57814219Smcpowers nkp_in_public_attributes); 57826424Skrishna if (!copyin_attributes(mode, sp, in_pub_count, in_pub_attributes, 57834219Smcpowers &k_in_pub_attrs, &k_in_pub_attrs_size, NULL, &rv, &error, 57846424Skrishna &in_pub_rctl_bytes, &in_pub_rctl_chk, B_TRUE)) { 57854219Smcpowers goto release_minor; 57864219Smcpowers } 57874219Smcpowers 57884219Smcpowers in_pri_attributes = STRUCT_FGETP(generate_key_pair, 57894219Smcpowers nkp_in_private_attributes); 57906424Skrishna if (!copyin_attributes(mode, sp, in_pri_count, in_pri_attributes, 57914219Smcpowers &k_in_pri_attrs, &k_in_pri_attrs_size, NULL, &rv, &error, 57926424Skrishna &in_pri_rctl_bytes, &in_pri_rctl_chk, B_TRUE)) { 57934219Smcpowers goto release_minor; 57944219Smcpowers } 57954219Smcpowers 57964219Smcpowers out_pub_count = STRUCT_FGET(generate_key_pair, nkp_out_public_count); 57974219Smcpowers out_pri_count = STRUCT_FGET(generate_key_pair, nkp_out_private_count); 57984219Smcpowers 57994219Smcpowers out_pub_attributes = STRUCT_FGETP(generate_key_pair, 58004219Smcpowers nkp_out_public_attributes); 58016424Skrishna if (!copyin_attributes(mode, sp, out_pub_count, out_pub_attributes, 58024219Smcpowers &k_out_pub_attrs, &k_out_pub_attrs_size, &u_pub_attrs, &rv, &error, 58036424Skrishna &out_pub_rctl_bytes, &out_pub_rctl_chk, B_FALSE)) { 58044219Smcpowers goto release_minor; 58054219Smcpowers } 58064219Smcpowers 58074219Smcpowers out_pri_attributes = STRUCT_FGETP(generate_key_pair, 58084219Smcpowers nkp_out_private_attributes); 58096424Skrishna if (!copyin_attributes(mode, sp, out_pri_count, out_pri_attributes, 58104219Smcpowers &k_out_pri_attrs, &k_out_pri_attrs_size, &u_pri_attrs, &rv, &error, 58116424Skrishna &out_pri_rctl_bytes, &out_pri_rctl_chk, B_FALSE)) { 58124219Smcpowers goto release_minor; 58134219Smcpowers } 58144219Smcpowers 58154219Smcpowers KCF_WRAP_NOSTORE_KEY_OPS_PARAMS(¶ms, KCF_OP_KEY_GENERATE_PAIR, 58164219Smcpowers sp->sd_provider_session->ps_session, &mech, k_in_pub_attrs, 58174219Smcpowers in_pub_count, k_in_pri_attrs, in_pri_count, NULL, k_out_pub_attrs, 58184219Smcpowers out_pub_count, k_out_pri_attrs, out_pri_count); 58194219Smcpowers 58204219Smcpowers rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE); 58214219Smcpowers 58224219Smcpowers if (rv == CRYPTO_SUCCESS) { 58234219Smcpowers error = copyout_attributes(mode, out_pub_attributes, 58244219Smcpowers out_pub_count, k_out_pub_attrs, u_pub_attrs); 58254219Smcpowers if (error != CRYPTO_SUCCESS) 58264219Smcpowers goto release_minor; 58274219Smcpowers error = copyout_attributes(mode, out_pri_attributes, 58284219Smcpowers out_pri_count, k_out_pri_attrs, u_pri_attrs); 58294219Smcpowers } 58304219Smcpowers 58314219Smcpowers release_minor: 58326424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk); 58336424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, in_pub_rctl_bytes, in_pub_rctl_chk); 58346424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, in_pri_rctl_bytes, in_pri_rctl_chk); 58356424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, out_pub_rctl_bytes, 58366424Skrishna out_pub_rctl_chk); 58376424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, out_pri_rctl_bytes, 58386424Skrishna out_pri_rctl_chk); 58394219Smcpowers 58404219Smcpowers if (k_in_pub_attrs != NULL) 58414219Smcpowers kmem_free(k_in_pub_attrs, k_in_pub_attrs_size); 58424219Smcpowers 58434219Smcpowers if (k_in_pri_attrs != NULL) 58444219Smcpowers kmem_free(k_in_pri_attrs, k_in_pri_attrs_size); 58454219Smcpowers 58464219Smcpowers if (k_out_pub_attrs != NULL) 58474219Smcpowers kmem_free(k_out_pub_attrs, k_out_pub_attrs_size); 58484219Smcpowers 58494219Smcpowers if (k_out_pri_attrs != NULL) { 58504219Smcpowers bzero(k_out_pri_attrs, k_out_pri_attrs_size); 58514219Smcpowers kmem_free(k_out_pri_attrs, k_out_pri_attrs_size); 58524219Smcpowers } 58534219Smcpowers 58544219Smcpowers if (u_pub_attrs != NULL) 58554219Smcpowers kmem_free(u_pub_attrs, out_pub_count * STRUCT_SIZE(oa)); 58564219Smcpowers 58574219Smcpowers if (u_pri_attrs != NULL) 58584219Smcpowers kmem_free(u_pri_attrs, out_pri_count * STRUCT_SIZE(oa)); 58594219Smcpowers 58604219Smcpowers if (error != 0) 58614219Smcpowers goto out; 58624219Smcpowers 58634219Smcpowers STRUCT_FSET(generate_key_pair, nkp_return_value, rv); 58644219Smcpowers if (copyout(STRUCT_BUF(generate_key_pair), arg, 58654219Smcpowers STRUCT_SIZE(generate_key_pair)) != 0) { 58664219Smcpowers error = EFAULT; 58674219Smcpowers } 58684219Smcpowers out: 58696424Skrishna CRYPTO_SESSION_RELE(sp); 58704219Smcpowers crypto_release_minor(cm); 58714219Smcpowers 58724219Smcpowers if (real_provider != NULL) { 58734219Smcpowers crypto_free_mech(real_provider, 58744219Smcpowers allocated_by_crypto_module, &mech); 58754219Smcpowers KCF_PROV_REFRELE(real_provider); 58764219Smcpowers } 58774219Smcpowers return (error); 58784219Smcpowers } 58794219Smcpowers 58804219Smcpowers /* ARGSUSED */ 58814219Smcpowers static int 58820Sstevel@tonic-gate object_wrap_key(dev_t dev, caddr_t arg, int mode, int *rval) 58830Sstevel@tonic-gate { 58840Sstevel@tonic-gate STRUCT_DECL(crypto_object_wrap_key, wrap_key); 5885904Smcpowers kcf_provider_desc_t *real_provider = NULL; 58860Sstevel@tonic-gate kcf_req_params_t params; 58870Sstevel@tonic-gate crypto_mechanism_t mech; 58880Sstevel@tonic-gate crypto_key_t key; 58890Sstevel@tonic-gate crypto_session_id_t session_id; 58900Sstevel@tonic-gate crypto_minor_t *cm; 58916424Skrishna crypto_session_data_t *sp = NULL; 58920Sstevel@tonic-gate crypto_object_id_t handle; 58930Sstevel@tonic-gate size_t mech_rctl_bytes = 0, key_rctl_bytes = 0; 58946424Skrishna boolean_t mech_rctl_chk = B_FALSE; 58956424Skrishna boolean_t key_rctl_chk = B_FALSE; 58960Sstevel@tonic-gate size_t wrapped_key_rctl_bytes = 0; 58976424Skrishna boolean_t wrapped_key_rctl_chk = B_FALSE; 58980Sstevel@tonic-gate size_t wrapped_key_len, new_wrapped_key_len; 58990Sstevel@tonic-gate uchar_t *wrapped_key = NULL; 59000Sstevel@tonic-gate char *wrapped_key_buffer; 59010Sstevel@tonic-gate int error = 0; 59020Sstevel@tonic-gate int rv; 5903904Smcpowers boolean_t allocated_by_crypto_module = B_FALSE; 59040Sstevel@tonic-gate 59050Sstevel@tonic-gate STRUCT_INIT(wrap_key, mode); 59060Sstevel@tonic-gate 59070Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 59080Sstevel@tonic-gate cmn_err(CE_WARN, "object_wrap_key: failed holding minor"); 59090Sstevel@tonic-gate return (ENXIO); 59100Sstevel@tonic-gate } 59110Sstevel@tonic-gate 59120Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(wrap_key), STRUCT_SIZE(wrap_key)) != 0) { 59130Sstevel@tonic-gate crypto_release_minor(cm); 59140Sstevel@tonic-gate return (EFAULT); 59150Sstevel@tonic-gate } 59160Sstevel@tonic-gate 59170Sstevel@tonic-gate bzero(&key, sizeof (crypto_key_t)); 59180Sstevel@tonic-gate 59190Sstevel@tonic-gate session_id = STRUCT_FGET(wrap_key, wk_session); 59200Sstevel@tonic-gate 59210Sstevel@tonic-gate if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) { 59226424Skrishna goto out; 59230Sstevel@tonic-gate } 59240Sstevel@tonic-gate 5925904Smcpowers bcopy(STRUCT_FADDR(wrap_key, wk_mechanism), &mech.cm_type, 5926904Smcpowers sizeof (crypto_mech_type_t)); 5927904Smcpowers 592810444SVladimir.Kotal@Sun.COM /* We need the key length for provider selection so copy it in now. */ 592910444SVladimir.Kotal@Sun.COM if (!copyin_key(mode, sp, STRUCT_FADDR(wrap_key, wk_wrapping_key), &key, 593010444SVladimir.Kotal@Sun.COM &key_rctl_bytes, &key_rctl_chk, &rv, &error)) { 593110444SVladimir.Kotal@Sun.COM goto out; 593210444SVladimir.Kotal@Sun.COM } 593310444SVladimir.Kotal@Sun.COM 593410444SVladimir.Kotal@Sun.COM wrapped_key_len = STRUCT_FGET(wrap_key, wk_wrapped_key_len); 593510444SVladimir.Kotal@Sun.COM 593610444SVladimir.Kotal@Sun.COM if ((rv = kcf_get_hardware_provider(mech.cm_type, &key, 593710444SVladimir.Kotal@Sun.COM CRYPTO_MECH_INVALID, NULL, CHECK_RESTRICT_FALSE, sp->sd_provider, 593810444SVladimir.Kotal@Sun.COM &real_provider, CRYPTO_FG_WRAP)) != CRYPTO_SUCCESS) { 59390Sstevel@tonic-gate goto out; 59400Sstevel@tonic-gate } 59410Sstevel@tonic-gate 5942904Smcpowers rv = crypto_provider_copyin_mech_param(real_provider, 5943904Smcpowers STRUCT_FADDR(wrap_key, wk_mechanism), &mech, mode, &error); 5944904Smcpowers 5945904Smcpowers if (rv == CRYPTO_NOT_SUPPORTED) { 5946904Smcpowers allocated_by_crypto_module = B_TRUE; 59476424Skrishna if (!copyin_mech(mode, sp, STRUCT_FADDR(wrap_key, wk_mechanism), 59486424Skrishna &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) { 5949904Smcpowers goto out; 5950904Smcpowers } 5951904Smcpowers } else { 5952904Smcpowers if (rv != CRYPTO_SUCCESS) 5953904Smcpowers goto out; 59540Sstevel@tonic-gate } 59550Sstevel@tonic-gate 59560Sstevel@tonic-gate /* 59570Sstevel@tonic-gate * Don't allocate output buffer unless both buffer pointer and 59580Sstevel@tonic-gate * buffer length are not NULL or 0 (length). 59590Sstevel@tonic-gate */ 59600Sstevel@tonic-gate wrapped_key_buffer = STRUCT_FGETP(wrap_key, wk_wrapped_key); 59610Sstevel@tonic-gate if (wrapped_key_buffer == NULL || wrapped_key_len == 0) { 59620Sstevel@tonic-gate wrapped_key_len = 0; 59630Sstevel@tonic-gate } 59640Sstevel@tonic-gate 59650Sstevel@tonic-gate if (wrapped_key_len > crypto_max_buffer_len) { 59660Sstevel@tonic-gate cmn_err(CE_NOTE, "object_wrap_key: buffer greater than %ld " 59670Sstevel@tonic-gate "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid); 59680Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD; 59690Sstevel@tonic-gate goto out; 59700Sstevel@tonic-gate } 59710Sstevel@tonic-gate 59726424Skrishna if ((rv = CRYPTO_BUFFER_CHECK(sp, wrapped_key_len, 59736424Skrishna wrapped_key_rctl_chk)) != CRYPTO_SUCCESS) { 59740Sstevel@tonic-gate goto out; 59750Sstevel@tonic-gate } 59760Sstevel@tonic-gate 59770Sstevel@tonic-gate /* new_wrapped_key_len can be modified by the provider */ 59780Sstevel@tonic-gate wrapped_key_rctl_bytes = new_wrapped_key_len = wrapped_key_len; 59790Sstevel@tonic-gate wrapped_key = kmem_alloc(wrapped_key_len, KM_SLEEP); 59800Sstevel@tonic-gate 59810Sstevel@tonic-gate handle = STRUCT_FGET(wrap_key, wk_object_handle); 59820Sstevel@tonic-gate KCF_WRAP_KEY_OPS_PARAMS(¶ms, KCF_OP_KEY_WRAP, 59830Sstevel@tonic-gate sp->sd_provider_session->ps_session, &mech, NULL, 0, &handle, 59840Sstevel@tonic-gate NULL, 0, NULL, &key, wrapped_key, &new_wrapped_key_len); 59850Sstevel@tonic-gate 59860Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE); 59870Sstevel@tonic-gate 59880Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) { 59890Sstevel@tonic-gate if (wrapped_key_len != 0 && copyout(wrapped_key, 59900Sstevel@tonic-gate wrapped_key_buffer, new_wrapped_key_len) != 0) { 59910Sstevel@tonic-gate error = EFAULT; 59920Sstevel@tonic-gate } 59930Sstevel@tonic-gate STRUCT_FSET(wrap_key, wk_wrapped_key_len, new_wrapped_key_len); 59940Sstevel@tonic-gate } 59950Sstevel@tonic-gate 59960Sstevel@tonic-gate if (rv == CRYPTO_BUFFER_TOO_SMALL) { 59970Sstevel@tonic-gate /* 59980Sstevel@tonic-gate * The providers return CRYPTO_BUFFER_TOO_SMALL even for case 1 59990Sstevel@tonic-gate * of section 11.2 of the pkcs11 spec. We catch it here and 60000Sstevel@tonic-gate * provide the correct pkcs11 return value. 60010Sstevel@tonic-gate */ 60020Sstevel@tonic-gate if (STRUCT_FGETP(wrap_key, wk_wrapped_key) == NULL) 60030Sstevel@tonic-gate rv = CRYPTO_SUCCESS; 60040Sstevel@tonic-gate STRUCT_FSET(wrap_key, wk_wrapped_key_len, new_wrapped_key_len); 60050Sstevel@tonic-gate } 60066424Skrishna 60070Sstevel@tonic-gate out: 60086424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk); 60096424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk); 60106424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, wrapped_key_rctl_bytes, 60116424Skrishna wrapped_key_rctl_chk); 60120Sstevel@tonic-gate CRYPTO_SESSION_RELE(sp); 60130Sstevel@tonic-gate 60140Sstevel@tonic-gate crypto_release_minor(cm); 60150Sstevel@tonic-gate 6016904Smcpowers if (real_provider != NULL) { 6017904Smcpowers crypto_free_mech(real_provider, 6018904Smcpowers allocated_by_crypto_module, &mech); 6019904Smcpowers KCF_PROV_REFRELE(real_provider); 6020904Smcpowers } 6021904Smcpowers 60220Sstevel@tonic-gate if (wrapped_key != NULL) 60230Sstevel@tonic-gate kmem_free(wrapped_key, wrapped_key_len); 60240Sstevel@tonic-gate 60250Sstevel@tonic-gate free_crypto_key(&key); 60260Sstevel@tonic-gate 60270Sstevel@tonic-gate if (error != 0) 60280Sstevel@tonic-gate return (error); 60290Sstevel@tonic-gate 60300Sstevel@tonic-gate STRUCT_FSET(wrap_key, wk_return_value, rv); 60310Sstevel@tonic-gate if (copyout(STRUCT_BUF(wrap_key), arg, STRUCT_SIZE(wrap_key)) != 0) { 60320Sstevel@tonic-gate return (EFAULT); 60330Sstevel@tonic-gate } 60340Sstevel@tonic-gate return (0); 60350Sstevel@tonic-gate } 60360Sstevel@tonic-gate 60370Sstevel@tonic-gate /* ARGSUSED */ 60380Sstevel@tonic-gate static int 60390Sstevel@tonic-gate object_unwrap_key(dev_t dev, caddr_t arg, int mode, int *rval) 60400Sstevel@tonic-gate { 60410Sstevel@tonic-gate STRUCT_DECL(crypto_object_unwrap_key, unwrap_key); 6042904Smcpowers kcf_provider_desc_t *real_provider = NULL; 60430Sstevel@tonic-gate kcf_req_params_t params; 60440Sstevel@tonic-gate crypto_mechanism_t mech; 60450Sstevel@tonic-gate crypto_key_t unwrapping_key; 60460Sstevel@tonic-gate crypto_session_id_t session_id; 60470Sstevel@tonic-gate crypto_minor_t *cm; 60480Sstevel@tonic-gate crypto_session_data_t *sp = NULL; 60490Sstevel@tonic-gate crypto_object_id_t handle; 60500Sstevel@tonic-gate crypto_object_attribute_t *k_attrs = NULL; 60510Sstevel@tonic-gate size_t k_attrs_size; 60520Sstevel@tonic-gate size_t mech_rctl_bytes = 0, unwrapping_key_rctl_bytes = 0; 60536424Skrishna boolean_t mech_rctl_chk = B_FALSE; 60546424Skrishna boolean_t unwrapping_key_rctl_chk = B_FALSE; 60550Sstevel@tonic-gate size_t wrapped_key_rctl_bytes = 0, k_attrs_rctl_bytes = 0; 60566424Skrishna boolean_t wrapped_key_rctl_chk = B_FALSE; 60576424Skrishna boolean_t k_attrs_rctl_chk = B_FALSE; 60580Sstevel@tonic-gate size_t wrapped_key_len; 60590Sstevel@tonic-gate uchar_t *wrapped_key = NULL; 60600Sstevel@tonic-gate int error = 0; 60610Sstevel@tonic-gate int rv; 60620Sstevel@tonic-gate uint_t count; 60630Sstevel@tonic-gate caddr_t uk_attributes; 6064904Smcpowers boolean_t allocated_by_crypto_module = B_FALSE; 60650Sstevel@tonic-gate 60660Sstevel@tonic-gate STRUCT_INIT(unwrap_key, mode); 60670Sstevel@tonic-gate 60680Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 60690Sstevel@tonic-gate cmn_err(CE_WARN, "object_unwrap_key: failed holding minor"); 60700Sstevel@tonic-gate return (ENXIO); 60710Sstevel@tonic-gate } 60720Sstevel@tonic-gate 60730Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(unwrap_key), STRUCT_SIZE(unwrap_key)) != 0) { 60740Sstevel@tonic-gate crypto_release_minor(cm); 60750Sstevel@tonic-gate return (EFAULT); 60760Sstevel@tonic-gate } 60770Sstevel@tonic-gate 60780Sstevel@tonic-gate bzero(&unwrapping_key, sizeof (unwrapping_key)); 60790Sstevel@tonic-gate 60800Sstevel@tonic-gate session_id = STRUCT_FGET(unwrap_key, uk_session); 60810Sstevel@tonic-gate 60820Sstevel@tonic-gate if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) { 60830Sstevel@tonic-gate goto release_minor; 60840Sstevel@tonic-gate } 60850Sstevel@tonic-gate 6086904Smcpowers bcopy(STRUCT_FADDR(unwrap_key, uk_mechanism), &mech.cm_type, 6087904Smcpowers sizeof (crypto_mech_type_t)); 6088904Smcpowers 608910444SVladimir.Kotal@Sun.COM /* We need the key length for provider selection so copy it in now. */ 609010444SVladimir.Kotal@Sun.COM if (!copyin_key(mode, sp, STRUCT_FADDR(unwrap_key, uk_unwrapping_key), 609110444SVladimir.Kotal@Sun.COM &unwrapping_key, &unwrapping_key_rctl_bytes, 609210444SVladimir.Kotal@Sun.COM &unwrapping_key_rctl_chk, &rv, &error)) { 609310444SVladimir.Kotal@Sun.COM goto release_minor; 609410444SVladimir.Kotal@Sun.COM } 609510444SVladimir.Kotal@Sun.COM 609610444SVladimir.Kotal@Sun.COM if ((rv = kcf_get_hardware_provider(mech.cm_type, &unwrapping_key, 609710444SVladimir.Kotal@Sun.COM CRYPTO_MECH_INVALID, NULL, CHECK_RESTRICT_FALSE, sp->sd_provider, 609810444SVladimir.Kotal@Sun.COM &real_provider, CRYPTO_FG_UNWRAP)) != CRYPTO_SUCCESS) { 60990Sstevel@tonic-gate goto release_minor; 61000Sstevel@tonic-gate } 61010Sstevel@tonic-gate 6102904Smcpowers rv = crypto_provider_copyin_mech_param(real_provider, 6103904Smcpowers STRUCT_FADDR(unwrap_key, uk_mechanism), &mech, mode, &error); 6104904Smcpowers 6105904Smcpowers if (rv == CRYPTO_NOT_SUPPORTED) { 6106904Smcpowers allocated_by_crypto_module = B_TRUE; 61076424Skrishna if (!copyin_mech(mode, sp, 61086424Skrishna STRUCT_FADDR(unwrap_key, uk_mechanism), 61096424Skrishna &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) { 6110904Smcpowers goto release_minor; 6111904Smcpowers } 6112904Smcpowers } else { 6113904Smcpowers if (rv != CRYPTO_SUCCESS) 6114904Smcpowers goto release_minor; 61150Sstevel@tonic-gate } 61160Sstevel@tonic-gate 61170Sstevel@tonic-gate count = STRUCT_FGET(unwrap_key, uk_count); 61180Sstevel@tonic-gate uk_attributes = STRUCT_FGETP(unwrap_key, uk_attributes); 61196424Skrishna if (!copyin_attributes(mode, sp, count, uk_attributes, &k_attrs, 61206424Skrishna &k_attrs_size, NULL, &rv, &error, &k_attrs_rctl_bytes, 61216424Skrishna &k_attrs_rctl_chk, B_TRUE)) { 61220Sstevel@tonic-gate goto release_minor; 61230Sstevel@tonic-gate } 61240Sstevel@tonic-gate 61250Sstevel@tonic-gate wrapped_key_len = STRUCT_FGET(unwrap_key, uk_wrapped_key_len); 61260Sstevel@tonic-gate if (wrapped_key_len > crypto_max_buffer_len) { 61270Sstevel@tonic-gate cmn_err(CE_NOTE, "object_unwrap_key: buffer greater than %ld " 61280Sstevel@tonic-gate "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid); 61290Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD; 61300Sstevel@tonic-gate goto release_minor; 61310Sstevel@tonic-gate } 61320Sstevel@tonic-gate 61336424Skrishna if ((rv = CRYPTO_BUFFER_CHECK(sp, wrapped_key_len, 61346424Skrishna wrapped_key_rctl_chk)) != CRYPTO_SUCCESS) { 61350Sstevel@tonic-gate goto release_minor; 61360Sstevel@tonic-gate } 61370Sstevel@tonic-gate wrapped_key_rctl_bytes = wrapped_key_len; 61380Sstevel@tonic-gate wrapped_key = kmem_alloc(wrapped_key_len, KM_SLEEP); 61390Sstevel@tonic-gate 61400Sstevel@tonic-gate if (wrapped_key_len != 0 && copyin(STRUCT_FGETP(unwrap_key, 61410Sstevel@tonic-gate uk_wrapped_key), wrapped_key, wrapped_key_len) != 0) { 61420Sstevel@tonic-gate error = EFAULT; 61430Sstevel@tonic-gate goto release_minor; 61440Sstevel@tonic-gate } 61450Sstevel@tonic-gate 61460Sstevel@tonic-gate /* wrapped_key_len is not modified by the unwrap operation */ 61470Sstevel@tonic-gate KCF_WRAP_KEY_OPS_PARAMS(¶ms, KCF_OP_KEY_UNWRAP, 61480Sstevel@tonic-gate sp->sd_provider_session->ps_session, &mech, k_attrs, count, &handle, 61490Sstevel@tonic-gate NULL, 0, NULL, &unwrapping_key, wrapped_key, &wrapped_key_len); 61500Sstevel@tonic-gate 61510Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE); 61520Sstevel@tonic-gate 61530Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) 61540Sstevel@tonic-gate STRUCT_FSET(unwrap_key, uk_object_handle, handle); 61550Sstevel@tonic-gate 61560Sstevel@tonic-gate release_minor: 61576424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk); 61586424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, unwrapping_key_rctl_bytes, 61596424Skrishna unwrapping_key_rctl_chk); 61606424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, wrapped_key_rctl_bytes, 61616424Skrishna wrapped_key_rctl_chk); 61626424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, k_attrs_rctl_bytes, 61636424Skrishna k_attrs_rctl_chk); 61640Sstevel@tonic-gate 61650Sstevel@tonic-gate if (k_attrs != NULL) 61660Sstevel@tonic-gate kmem_free(k_attrs, k_attrs_size); 61670Sstevel@tonic-gate 61680Sstevel@tonic-gate if (wrapped_key != NULL) 61690Sstevel@tonic-gate kmem_free(wrapped_key, wrapped_key_len); 61700Sstevel@tonic-gate 61710Sstevel@tonic-gate free_crypto_key(&unwrapping_key); 61720Sstevel@tonic-gate 61730Sstevel@tonic-gate if (error != 0) 61740Sstevel@tonic-gate goto out; 61750Sstevel@tonic-gate 61760Sstevel@tonic-gate STRUCT_FSET(unwrap_key, uk_return_value, rv); 61770Sstevel@tonic-gate if (copyout(STRUCT_BUF(unwrap_key), arg, 61780Sstevel@tonic-gate STRUCT_SIZE(unwrap_key)) != 0) { 61790Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) { 61800Sstevel@tonic-gate KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, 61810Sstevel@tonic-gate KCF_OP_OBJECT_DESTROY, 61820Sstevel@tonic-gate sp->sd_provider_session->ps_session, handle, 61830Sstevel@tonic-gate NULL, 0, NULL, 0, NULL, NULL, 0, NULL); 61840Sstevel@tonic-gate 61850Sstevel@tonic-gate (void) kcf_submit_request(real_provider, NULL, 61860Sstevel@tonic-gate NULL, ¶ms, B_FALSE); 61870Sstevel@tonic-gate 61880Sstevel@tonic-gate error = EFAULT; 61890Sstevel@tonic-gate } 61900Sstevel@tonic-gate } 61910Sstevel@tonic-gate out: 61926424Skrishna CRYPTO_SESSION_RELE(sp); 61930Sstevel@tonic-gate crypto_release_minor(cm); 6194904Smcpowers 6195904Smcpowers if (real_provider != NULL) { 6196904Smcpowers crypto_free_mech(real_provider, 6197904Smcpowers allocated_by_crypto_module, &mech); 6198904Smcpowers KCF_PROV_REFRELE(real_provider); 6199904Smcpowers } 6200904Smcpowers 62010Sstevel@tonic-gate return (error); 62020Sstevel@tonic-gate } 62030Sstevel@tonic-gate 62040Sstevel@tonic-gate /* ARGSUSED */ 62050Sstevel@tonic-gate static int 62060Sstevel@tonic-gate object_derive_key(dev_t dev, caddr_t arg, int mode, int *rval) 62070Sstevel@tonic-gate { 62080Sstevel@tonic-gate STRUCT_DECL(crypto_derive_key, derive_key); 6209904Smcpowers kcf_provider_desc_t *real_provider = NULL; 62100Sstevel@tonic-gate kcf_req_params_t params; 62110Sstevel@tonic-gate crypto_object_attribute_t *k_attrs = NULL; 62120Sstevel@tonic-gate crypto_mechanism_t mech; 62130Sstevel@tonic-gate crypto_key_t base_key; 62140Sstevel@tonic-gate crypto_session_id_t session_id; 62150Sstevel@tonic-gate crypto_minor_t *cm; 62160Sstevel@tonic-gate crypto_session_data_t *sp = NULL; 62170Sstevel@tonic-gate crypto_object_id_t handle; 62180Sstevel@tonic-gate size_t k_attrs_size; 62190Sstevel@tonic-gate size_t key_rctl_bytes = 0, mech_rctl_bytes = 0; 62206424Skrishna boolean_t mech_rctl_chk = B_FALSE; 62216424Skrishna boolean_t key_rctl_chk = B_FALSE; 62220Sstevel@tonic-gate size_t attributes_rctl_bytes = 0; 62236424Skrishna boolean_t attributes_rctl_chk = B_FALSE; 62240Sstevel@tonic-gate caddr_t attributes; 62250Sstevel@tonic-gate uint_t count; 62260Sstevel@tonic-gate int error = 0; 62270Sstevel@tonic-gate int rv; 6228904Smcpowers boolean_t allocated_by_crypto_module = B_FALSE; 6229904Smcpowers boolean_t please_destroy_object = B_FALSE; 62300Sstevel@tonic-gate 62310Sstevel@tonic-gate STRUCT_INIT(derive_key, mode); 62320Sstevel@tonic-gate 62330Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 62340Sstevel@tonic-gate cmn_err(CE_WARN, "object_derive_key: failed holding minor"); 62350Sstevel@tonic-gate return (ENXIO); 62360Sstevel@tonic-gate } 62370Sstevel@tonic-gate 62380Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(derive_key), STRUCT_SIZE(derive_key)) != 0) { 62390Sstevel@tonic-gate crypto_release_minor(cm); 62400Sstevel@tonic-gate return (EFAULT); 62410Sstevel@tonic-gate } 62420Sstevel@tonic-gate 62430Sstevel@tonic-gate bzero(&base_key, sizeof (base_key)); 62440Sstevel@tonic-gate 62450Sstevel@tonic-gate session_id = STRUCT_FGET(derive_key, dk_session); 62460Sstevel@tonic-gate 62470Sstevel@tonic-gate if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) { 62480Sstevel@tonic-gate goto release_minor; 62490Sstevel@tonic-gate } 62500Sstevel@tonic-gate 6251904Smcpowers bcopy(STRUCT_FADDR(derive_key, dk_mechanism), &mech.cm_type, 6252904Smcpowers sizeof (crypto_mech_type_t)); 6253904Smcpowers 625410444SVladimir.Kotal@Sun.COM /* We need the key length for provider selection so copy it in now. */ 625510444SVladimir.Kotal@Sun.COM if (!copyin_key(mode, sp, STRUCT_FADDR(derive_key, dk_base_key), 625610444SVladimir.Kotal@Sun.COM &base_key, &key_rctl_bytes, &key_rctl_chk, &rv, &error)) { 625710444SVladimir.Kotal@Sun.COM goto release_minor; 625810444SVladimir.Kotal@Sun.COM } 625910444SVladimir.Kotal@Sun.COM 626010444SVladimir.Kotal@Sun.COM if ((rv = kcf_get_hardware_provider(mech.cm_type, &base_key, 626110444SVladimir.Kotal@Sun.COM CRYPTO_MECH_INVALID, NULL, CHECK_RESTRICT_FALSE, sp->sd_provider, 626210444SVladimir.Kotal@Sun.COM &real_provider, CRYPTO_FG_DERIVE)) != CRYPTO_SUCCESS) { 62630Sstevel@tonic-gate goto release_minor; 62640Sstevel@tonic-gate } 62650Sstevel@tonic-gate 6266904Smcpowers rv = crypto_provider_copyin_mech_param(real_provider, 6267904Smcpowers STRUCT_FADDR(derive_key, dk_mechanism), &mech, mode, &error); 6268904Smcpowers 6269904Smcpowers if (rv == CRYPTO_NOT_SUPPORTED) { 6270904Smcpowers allocated_by_crypto_module = B_TRUE; 62716424Skrishna if (!copyin_mech(mode, sp, 62726424Skrishna STRUCT_FADDR(derive_key, dk_mechanism), 62736424Skrishna &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) { 6274904Smcpowers goto release_minor; 6275904Smcpowers } 6276904Smcpowers } else { 6277904Smcpowers if (rv != CRYPTO_SUCCESS) 6278904Smcpowers goto release_minor; 62790Sstevel@tonic-gate } 62800Sstevel@tonic-gate 62810Sstevel@tonic-gate count = STRUCT_FGET(derive_key, dk_count); 62820Sstevel@tonic-gate 62830Sstevel@tonic-gate attributes = STRUCT_FGETP(derive_key, dk_attributes); 62846424Skrishna if (!copyin_attributes(mode, sp, count, attributes, &k_attrs, 62853916Skrishna &k_attrs_size, NULL, &rv, &error, 62866424Skrishna &attributes_rctl_bytes, &attributes_rctl_chk, B_TRUE)) { 62870Sstevel@tonic-gate goto release_minor; 62880Sstevel@tonic-gate } 62890Sstevel@tonic-gate 62900Sstevel@tonic-gate KCF_WRAP_KEY_OPS_PARAMS(¶ms, KCF_OP_KEY_DERIVE, 62910Sstevel@tonic-gate sp->sd_provider_session->ps_session, &mech, k_attrs, count, 62920Sstevel@tonic-gate &handle, NULL, 0, NULL, &base_key, NULL, NULL); 62930Sstevel@tonic-gate 62940Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE); 62950Sstevel@tonic-gate 6296904Smcpowers if (rv == CRYPTO_SUCCESS) { 62970Sstevel@tonic-gate STRUCT_FSET(derive_key, dk_object_handle, handle); 62980Sstevel@tonic-gate 6299904Smcpowers rv = crypto_provider_copyout_mech_param(real_provider, 6300904Smcpowers &mech, STRUCT_FADDR(derive_key, dk_mechanism), 6301904Smcpowers mode, &error); 6302904Smcpowers 6303904Smcpowers if (rv == CRYPTO_NOT_SUPPORTED) { 6304904Smcpowers rv = CRYPTO_SUCCESS; 6305904Smcpowers goto release_minor; 6306904Smcpowers } 6307904Smcpowers 6308904Smcpowers if (rv != CRYPTO_SUCCESS) 6309904Smcpowers please_destroy_object = B_TRUE; 6310904Smcpowers } 6311904Smcpowers 63120Sstevel@tonic-gate release_minor: 63136424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk); 63146424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk); 63156424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, attributes_rctl_bytes, 63166424Skrishna attributes_rctl_chk); 63170Sstevel@tonic-gate 63180Sstevel@tonic-gate if (k_attrs != NULL) 63190Sstevel@tonic-gate kmem_free(k_attrs, k_attrs_size); 63200Sstevel@tonic-gate 63210Sstevel@tonic-gate free_crypto_key(&base_key); 63220Sstevel@tonic-gate 63230Sstevel@tonic-gate if (error != 0) 63240Sstevel@tonic-gate goto out; 63250Sstevel@tonic-gate 63260Sstevel@tonic-gate STRUCT_FSET(derive_key, dk_return_value, rv); 63270Sstevel@tonic-gate if (copyout(STRUCT_BUF(derive_key), arg, 63280Sstevel@tonic-gate STRUCT_SIZE(derive_key)) != 0) { 63290Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) { 6330904Smcpowers please_destroy_object = B_TRUE; 63310Sstevel@tonic-gate error = EFAULT; 63320Sstevel@tonic-gate } 63330Sstevel@tonic-gate } 63340Sstevel@tonic-gate out: 6335904Smcpowers if (please_destroy_object) { 6336904Smcpowers KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, KCF_OP_OBJECT_DESTROY, 6337904Smcpowers sp->sd_provider_session->ps_session, handle, 6338904Smcpowers NULL, 0, NULL, 0, NULL, NULL, 0, NULL); 6339904Smcpowers 6340904Smcpowers (void) kcf_submit_request(real_provider, NULL, 6341904Smcpowers NULL, ¶ms, B_FALSE); 6342904Smcpowers } 6343904Smcpowers 63446424Skrishna CRYPTO_SESSION_RELE(sp); 63450Sstevel@tonic-gate crypto_release_minor(cm); 6346904Smcpowers 6347904Smcpowers if (real_provider != NULL) { 6348904Smcpowers crypto_free_mech(real_provider, 6349904Smcpowers allocated_by_crypto_module, &mech); 6350904Smcpowers KCF_PROV_REFRELE(real_provider); 6351904Smcpowers } 63520Sstevel@tonic-gate return (error); 63530Sstevel@tonic-gate } 63540Sstevel@tonic-gate 63550Sstevel@tonic-gate /* ARGSUSED */ 63560Sstevel@tonic-gate static int 63574219Smcpowers nostore_derive_key(dev_t dev, caddr_t arg, int mode, int *rval) 63584219Smcpowers { 63594219Smcpowers STRUCT_DECL(crypto_nostore_derive_key, derive_key); 63604219Smcpowers /* LINTED E_FUNC_SET_NOT_USED */ 63614219Smcpowers STRUCT_DECL(crypto_object_attribute, oa); 63624219Smcpowers kcf_provider_desc_t *real_provider = NULL; 63634219Smcpowers kcf_req_params_t params; 63644219Smcpowers crypto_object_attribute_t *k_in_attrs = NULL; 63654219Smcpowers crypto_object_attribute_t *k_out_attrs = NULL; 63664219Smcpowers crypto_mechanism_t mech; 63674219Smcpowers crypto_key_t base_key; 63684219Smcpowers crypto_session_id_t session_id; 63694219Smcpowers crypto_minor_t *cm; 63704219Smcpowers crypto_session_data_t *sp = NULL; 63714219Smcpowers size_t k_in_attrs_size, k_out_attrs_size; 63724219Smcpowers size_t key_rctl_bytes = 0, mech_rctl_bytes = 0; 63736424Skrishna boolean_t mech_rctl_chk = B_FALSE; 63746424Skrishna boolean_t key_rctl_chk = B_FALSE; 63754219Smcpowers size_t in_attributes_rctl_bytes = 0; 63764219Smcpowers size_t out_attributes_rctl_bytes = 0; 63776424Skrishna boolean_t in_attributes_rctl_chk = B_FALSE; 63786424Skrishna boolean_t out_attributes_rctl_chk = B_FALSE; 63794219Smcpowers caddr_t in_attributes, out_attributes; 63804219Smcpowers uint_t in_count, out_count; 63814219Smcpowers int error = 0; 63824219Smcpowers int rv; 63834219Smcpowers boolean_t allocated_by_crypto_module = B_FALSE; 63844219Smcpowers caddr_t u_attrs = NULL; 63854219Smcpowers 63864219Smcpowers STRUCT_INIT(derive_key, mode); 63874219Smcpowers STRUCT_INIT(oa, mode); 63884219Smcpowers 63894219Smcpowers if ((cm = crypto_hold_minor(getminor(dev))) == NULL) { 63904219Smcpowers cmn_err(CE_WARN, "nostore_derive_key: failed holding minor"); 63914219Smcpowers return (ENXIO); 63924219Smcpowers } 63934219Smcpowers 63944219Smcpowers if (copyin(arg, STRUCT_BUF(derive_key), STRUCT_SIZE(derive_key)) != 0) { 63954219Smcpowers crypto_release_minor(cm); 63964219Smcpowers return (EFAULT); 63974219Smcpowers } 63984219Smcpowers 63994219Smcpowers bzero(&base_key, sizeof (base_key)); 64004219Smcpowers 64014219Smcpowers session_id = STRUCT_FGET(derive_key, ndk_session); 64024219Smcpowers 64034219Smcpowers if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) { 64044219Smcpowers goto release_minor; 64054219Smcpowers } 64064219Smcpowers 64074219Smcpowers bcopy(STRUCT_FADDR(derive_key, ndk_mechanism), &mech.cm_type, 64084219Smcpowers sizeof (crypto_mech_type_t)); 64094219Smcpowers 641010444SVladimir.Kotal@Sun.COM /* We need the key length for provider selection so copy it in now. */ 641110444SVladimir.Kotal@Sun.COM if (!copyin_key(mode, sp, STRUCT_FADDR(derive_key, ndk_base_key), 641210444SVladimir.Kotal@Sun.COM &base_key, &key_rctl_bytes, &key_rctl_chk, &rv, &error)) { 641310444SVladimir.Kotal@Sun.COM goto release_minor; 641410444SVladimir.Kotal@Sun.COM } 641510444SVladimir.Kotal@Sun.COM 641610444SVladimir.Kotal@Sun.COM if ((rv = kcf_get_hardware_provider(mech.cm_type, &base_key, 641710444SVladimir.Kotal@Sun.COM CRYPTO_MECH_INVALID, NULL, CHECK_RESTRICT_FALSE, sp->sd_provider, 641810444SVladimir.Kotal@Sun.COM &real_provider, CRYPTO_FG_DERIVE)) != CRYPTO_SUCCESS) { 64194219Smcpowers goto release_minor; 64204219Smcpowers } 64214219Smcpowers 64224219Smcpowers rv = crypto_provider_copyin_mech_param(real_provider, 64234219Smcpowers STRUCT_FADDR(derive_key, ndk_mechanism), &mech, mode, &error); 64244219Smcpowers 64254219Smcpowers if (rv == CRYPTO_NOT_SUPPORTED) { 64264219Smcpowers allocated_by_crypto_module = B_TRUE; 64276424Skrishna if (!copyin_mech(mode, sp, 64286424Skrishna STRUCT_FADDR(derive_key, ndk_mechanism), 64296424Skrishna &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) { 64304219Smcpowers goto release_minor; 64314219Smcpowers } 64324219Smcpowers } else { 64334219Smcpowers if (rv != CRYPTO_SUCCESS) 64344219Smcpowers goto release_minor; 64354219Smcpowers } 64364219Smcpowers 64374219Smcpowers in_count = STRUCT_FGET(derive_key, ndk_in_count); 64384219Smcpowers out_count = STRUCT_FGET(derive_key, ndk_out_count); 64394219Smcpowers 64404219Smcpowers in_attributes = STRUCT_FGETP(derive_key, ndk_in_attributes); 64416424Skrishna if (!copyin_attributes(mode, sp, in_count, in_attributes, &k_in_attrs, 64424219Smcpowers &k_in_attrs_size, NULL, &rv, &error, &in_attributes_rctl_bytes, 64436424Skrishna &in_attributes_rctl_chk, B_TRUE)) { 64444219Smcpowers goto release_minor; 64454219Smcpowers } 64464219Smcpowers 64474219Smcpowers out_attributes = STRUCT_FGETP(derive_key, ndk_out_attributes); 64486424Skrishna if (!copyin_attributes(mode, sp, out_count, out_attributes, 64496424Skrishna &k_out_attrs, &k_out_attrs_size, &u_attrs, &rv, &error, 64506424Skrishna &out_attributes_rctl_bytes, 64516424Skrishna &out_attributes_rctl_chk, B_FALSE)) { 64524219Smcpowers goto release_minor; 64534219Smcpowers } 64544219Smcpowers 64554219Smcpowers KCF_WRAP_NOSTORE_KEY_OPS_PARAMS(¶ms, KCF_OP_KEY_DERIVE, 64564219Smcpowers sp->sd_provider_session->ps_session, &mech, k_in_attrs, in_count, 64574219Smcpowers NULL, 0, &base_key, k_out_attrs, out_count, NULL, 0); 64584219Smcpowers 64594219Smcpowers rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE); 64604219Smcpowers 64614219Smcpowers if (rv == CRYPTO_SUCCESS) { 64624219Smcpowers rv = crypto_provider_copyout_mech_param(real_provider, 64634219Smcpowers &mech, STRUCT_FADDR(derive_key, ndk_mechanism), 64644219Smcpowers mode, &error); 64654219Smcpowers 64664219Smcpowers if (rv == CRYPTO_NOT_SUPPORTED) { 64674219Smcpowers rv = CRYPTO_SUCCESS; 64684219Smcpowers } 64694219Smcpowers /* copyout the derived secret */ 64704219Smcpowers if (copyout_attributes(mode, out_attributes, out_count, 64714219Smcpowers k_out_attrs, u_attrs) != 0) 64724219Smcpowers error = EFAULT; 64734219Smcpowers } 64744219Smcpowers 64754219Smcpowers release_minor: 64766424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk); 64776424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk); 64786424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, in_attributes_rctl_bytes, 64796424Skrishna in_attributes_rctl_chk); 64806424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, out_attributes_rctl_bytes, 64816424Skrishna out_attributes_rctl_chk); 64824219Smcpowers 64834219Smcpowers if (k_in_attrs != NULL) 64844219Smcpowers kmem_free(k_in_attrs, k_in_attrs_size); 64854219Smcpowers if (k_out_attrs != NULL) { 64864219Smcpowers bzero(k_out_attrs, k_out_attrs_size); 64874219Smcpowers kmem_free(k_out_attrs, k_out_attrs_size); 64884219Smcpowers } 64894219Smcpowers 64904219Smcpowers if (u_attrs != NULL) 64914219Smcpowers kmem_free(u_attrs, out_count * STRUCT_SIZE(oa)); 64924219Smcpowers 64934219Smcpowers free_crypto_key(&base_key); 64944219Smcpowers 64954219Smcpowers if (error != 0) 64964219Smcpowers goto out; 64974219Smcpowers 64984219Smcpowers STRUCT_FSET(derive_key, ndk_return_value, rv); 64994219Smcpowers if (copyout(STRUCT_BUF(derive_key), arg, 65004219Smcpowers STRUCT_SIZE(derive_key)) != 0) { 65014219Smcpowers error = EFAULT; 65024219Smcpowers } 65034219Smcpowers out: 65046424Skrishna CRYPTO_SESSION_RELE(sp); 65054219Smcpowers crypto_release_minor(cm); 65064219Smcpowers 65074219Smcpowers if (real_provider != NULL) { 65084219Smcpowers crypto_free_mech(real_provider, 65094219Smcpowers allocated_by_crypto_module, &mech); 65104219Smcpowers KCF_PROV_REFRELE(real_provider); 65114219Smcpowers } 65124219Smcpowers return (error); 65134219Smcpowers } 65144219Smcpowers 65154219Smcpowers /* ARGSUSED */ 65164219Smcpowers static int 65170Sstevel@tonic-gate crypto_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *c, 65180Sstevel@tonic-gate int *rval) 65190Sstevel@tonic-gate { 65200Sstevel@tonic-gate #define ARG ((caddr_t)arg) 65210Sstevel@tonic-gate 65220Sstevel@tonic-gate switch (cmd) { 65230Sstevel@tonic-gate case CRYPTO_GET_FUNCTION_LIST: 65240Sstevel@tonic-gate return (get_function_list(dev, ARG, mode, rval)); 65250Sstevel@tonic-gate 65260Sstevel@tonic-gate case CRYPTO_GET_MECHANISM_NUMBER: 65270Sstevel@tonic-gate return (get_mechanism_number(dev, ARG, mode, rval)); 65280Sstevel@tonic-gate 65298313SDina.Nimeh@Sun.Com case CRYPTO_GET_MECHANISM_LIST: 65308313SDina.Nimeh@Sun.Com return (get_mechanism_list(dev, ARG, mode, rval)); 65318313SDina.Nimeh@Sun.Com 65328313SDina.Nimeh@Sun.Com case CRYPTO_GET_ALL_MECHANISM_INFO: 65338313SDina.Nimeh@Sun.Com return (get_all_mechanism_info(dev, ARG, mode, rval)); 65348313SDina.Nimeh@Sun.Com 65350Sstevel@tonic-gate case CRYPTO_GET_PROVIDER_LIST: 65360Sstevel@tonic-gate return (get_provider_list(dev, ARG, mode, rval)); 65370Sstevel@tonic-gate 65380Sstevel@tonic-gate case CRYPTO_GET_PROVIDER_INFO: 65390Sstevel@tonic-gate return (get_provider_info(dev, ARG, mode, rval)); 65400Sstevel@tonic-gate 65410Sstevel@tonic-gate case CRYPTO_GET_PROVIDER_MECHANISMS: 65420Sstevel@tonic-gate return (get_provider_mechanisms(dev, ARG, mode, rval)); 65430Sstevel@tonic-gate 65440Sstevel@tonic-gate case CRYPTO_GET_PROVIDER_MECHANISM_INFO: 65450Sstevel@tonic-gate return (get_provider_mechanism_info(dev, ARG, mode, rval)); 65460Sstevel@tonic-gate 65470Sstevel@tonic-gate case CRYPTO_OPEN_SESSION: 65480Sstevel@tonic-gate return (open_session(dev, ARG, mode, rval)); 65490Sstevel@tonic-gate 65500Sstevel@tonic-gate case CRYPTO_CLOSE_SESSION: 65510Sstevel@tonic-gate return (close_session(dev, ARG, mode, rval)); 65520Sstevel@tonic-gate 65530Sstevel@tonic-gate case CRYPTO_ENCRYPT_INIT: 65540Sstevel@tonic-gate return (encrypt_init(dev, ARG, mode, rval)); 65550Sstevel@tonic-gate 65560Sstevel@tonic-gate case CRYPTO_DECRYPT_INIT: 65570Sstevel@tonic-gate return (decrypt_init(dev, ARG, mode, rval)); 65580Sstevel@tonic-gate 65590Sstevel@tonic-gate case CRYPTO_ENCRYPT: 65600Sstevel@tonic-gate return (encrypt(dev, ARG, mode, rval)); 65610Sstevel@tonic-gate 65620Sstevel@tonic-gate case CRYPTO_DECRYPT: 65630Sstevel@tonic-gate return (decrypt(dev, ARG, mode, rval)); 65640Sstevel@tonic-gate 65650Sstevel@tonic-gate case CRYPTO_ENCRYPT_UPDATE: 65660Sstevel@tonic-gate return (encrypt_update(dev, ARG, mode, rval)); 65670Sstevel@tonic-gate 65680Sstevel@tonic-gate case CRYPTO_DECRYPT_UPDATE: 65690Sstevel@tonic-gate return (decrypt_update(dev, ARG, mode, rval)); 65700Sstevel@tonic-gate 65710Sstevel@tonic-gate case CRYPTO_ENCRYPT_FINAL: 65720Sstevel@tonic-gate return (encrypt_final(dev, ARG, mode, rval)); 65730Sstevel@tonic-gate 65740Sstevel@tonic-gate case CRYPTO_DECRYPT_FINAL: 65750Sstevel@tonic-gate return (decrypt_final(dev, ARG, mode, rval)); 65760Sstevel@tonic-gate 65770Sstevel@tonic-gate case CRYPTO_DIGEST_INIT: 65780Sstevel@tonic-gate return (digest_init(dev, ARG, mode, rval)); 65790Sstevel@tonic-gate 65800Sstevel@tonic-gate case CRYPTO_DIGEST: 65810Sstevel@tonic-gate return (digest(dev, ARG, mode, rval)); 65820Sstevel@tonic-gate 65830Sstevel@tonic-gate case CRYPTO_DIGEST_UPDATE: 65840Sstevel@tonic-gate return (digest_update(dev, ARG, mode, rval)); 65850Sstevel@tonic-gate 65860Sstevel@tonic-gate case CRYPTO_DIGEST_KEY: 65870Sstevel@tonic-gate return (digest_key(dev, ARG, mode, rval)); 65880Sstevel@tonic-gate 65890Sstevel@tonic-gate case CRYPTO_DIGEST_FINAL: 65900Sstevel@tonic-gate return (digest_final(dev, ARG, mode, rval)); 65910Sstevel@tonic-gate 65920Sstevel@tonic-gate case CRYPTO_SIGN_INIT: 65930Sstevel@tonic-gate return (sign_init(dev, ARG, mode, rval)); 65940Sstevel@tonic-gate 65950Sstevel@tonic-gate case CRYPTO_SIGN: 65960Sstevel@tonic-gate return (sign(dev, ARG, mode, rval)); 65970Sstevel@tonic-gate 65980Sstevel@tonic-gate case CRYPTO_SIGN_UPDATE: 65990Sstevel@tonic-gate return (sign_update(dev, ARG, mode, rval)); 66000Sstevel@tonic-gate 66010Sstevel@tonic-gate case CRYPTO_SIGN_FINAL: 66020Sstevel@tonic-gate return (sign_final(dev, ARG, mode, rval)); 66030Sstevel@tonic-gate 66040Sstevel@tonic-gate case CRYPTO_SIGN_RECOVER_INIT: 66050Sstevel@tonic-gate return (sign_recover_init(dev, ARG, mode, rval)); 66060Sstevel@tonic-gate 66070Sstevel@tonic-gate case CRYPTO_SIGN_RECOVER: 66080Sstevel@tonic-gate return (sign_recover(dev, ARG, mode, rval)); 66090Sstevel@tonic-gate 66100Sstevel@tonic-gate case CRYPTO_VERIFY_INIT: 66110Sstevel@tonic-gate return (verify_init(dev, ARG, mode, rval)); 66120Sstevel@tonic-gate 66130Sstevel@tonic-gate case CRYPTO_VERIFY: 66140Sstevel@tonic-gate return (verify(dev, ARG, mode, rval)); 66150Sstevel@tonic-gate 66160Sstevel@tonic-gate case CRYPTO_VERIFY_UPDATE: 66170Sstevel@tonic-gate return (verify_update(dev, ARG, mode, rval)); 66180Sstevel@tonic-gate 66190Sstevel@tonic-gate case CRYPTO_VERIFY_FINAL: 66200Sstevel@tonic-gate return (verify_final(dev, ARG, mode, rval)); 66210Sstevel@tonic-gate 66220Sstevel@tonic-gate case CRYPTO_VERIFY_RECOVER_INIT: 66230Sstevel@tonic-gate return (verify_recover_init(dev, ARG, mode, rval)); 66240Sstevel@tonic-gate 66250Sstevel@tonic-gate case CRYPTO_VERIFY_RECOVER: 66260Sstevel@tonic-gate return (verify_recover(dev, ARG, mode, rval)); 66270Sstevel@tonic-gate 66280Sstevel@tonic-gate case CRYPTO_SET_PIN: 66290Sstevel@tonic-gate return (set_pin(dev, ARG, mode, rval)); 66300Sstevel@tonic-gate 66310Sstevel@tonic-gate case CRYPTO_LOGIN: 66320Sstevel@tonic-gate return (login(dev, ARG, mode, rval)); 66330Sstevel@tonic-gate 66340Sstevel@tonic-gate case CRYPTO_LOGOUT: 66350Sstevel@tonic-gate return (logout(dev, ARG, mode, rval)); 66360Sstevel@tonic-gate 66370Sstevel@tonic-gate case CRYPTO_SEED_RANDOM: 66380Sstevel@tonic-gate return (seed_random(dev, ARG, mode, rval)); 66390Sstevel@tonic-gate 66400Sstevel@tonic-gate case CRYPTO_GENERATE_RANDOM: 66410Sstevel@tonic-gate return (generate_random(dev, ARG, mode, rval)); 66420Sstevel@tonic-gate 66430Sstevel@tonic-gate case CRYPTO_OBJECT_CREATE: 66440Sstevel@tonic-gate return (object_create(dev, ARG, mode, rval)); 66450Sstevel@tonic-gate 66460Sstevel@tonic-gate case CRYPTO_OBJECT_COPY: 66470Sstevel@tonic-gate return (object_copy(dev, ARG, mode, rval)); 66480Sstevel@tonic-gate 66490Sstevel@tonic-gate case CRYPTO_OBJECT_DESTROY: 66500Sstevel@tonic-gate return (object_destroy(dev, ARG, mode, rval)); 66510Sstevel@tonic-gate 66520Sstevel@tonic-gate case CRYPTO_OBJECT_GET_ATTRIBUTE_VALUE: 66530Sstevel@tonic-gate return (object_get_attribute_value(dev, ARG, mode, rval)); 66540Sstevel@tonic-gate 66550Sstevel@tonic-gate case CRYPTO_OBJECT_GET_SIZE: 66560Sstevel@tonic-gate return (object_get_size(dev, ARG, mode, rval)); 66570Sstevel@tonic-gate 66580Sstevel@tonic-gate case CRYPTO_OBJECT_SET_ATTRIBUTE_VALUE: 66590Sstevel@tonic-gate return (object_set_attribute_value(dev, ARG, mode, rval)); 66600Sstevel@tonic-gate 66610Sstevel@tonic-gate case CRYPTO_OBJECT_FIND_INIT: 66620Sstevel@tonic-gate return (object_find_init(dev, ARG, mode, rval)); 66630Sstevel@tonic-gate 66640Sstevel@tonic-gate case CRYPTO_OBJECT_FIND_UPDATE: 66650Sstevel@tonic-gate return (object_find_update(dev, ARG, mode, rval)); 66660Sstevel@tonic-gate 66670Sstevel@tonic-gate case CRYPTO_OBJECT_FIND_FINAL: 66680Sstevel@tonic-gate return (object_find_final(dev, ARG, mode, rval)); 66690Sstevel@tonic-gate 66700Sstevel@tonic-gate case CRYPTO_GENERATE_KEY: 66710Sstevel@tonic-gate return (object_generate_key(dev, ARG, mode, rval)); 66720Sstevel@tonic-gate 66730Sstevel@tonic-gate case CRYPTO_GENERATE_KEY_PAIR: 66740Sstevel@tonic-gate return (object_generate_key_pair(dev, ARG, mode, rval)); 66750Sstevel@tonic-gate 66760Sstevel@tonic-gate case CRYPTO_WRAP_KEY: 66770Sstevel@tonic-gate return (object_wrap_key(dev, ARG, mode, rval)); 66780Sstevel@tonic-gate 66790Sstevel@tonic-gate case CRYPTO_UNWRAP_KEY: 66800Sstevel@tonic-gate return (object_unwrap_key(dev, ARG, mode, rval)); 66810Sstevel@tonic-gate 66820Sstevel@tonic-gate case CRYPTO_DERIVE_KEY: 66830Sstevel@tonic-gate return (object_derive_key(dev, ARG, mode, rval)); 66844219Smcpowers 66854219Smcpowers case CRYPTO_NOSTORE_GENERATE_KEY: 66864219Smcpowers return (nostore_generate_key(dev, ARG, mode, rval)); 66874219Smcpowers 66884219Smcpowers case CRYPTO_NOSTORE_GENERATE_KEY_PAIR: 66894219Smcpowers return (nostore_generate_key_pair(dev, ARG, mode, rval)); 66904219Smcpowers 66914219Smcpowers case CRYPTO_NOSTORE_DERIVE_KEY: 66924219Smcpowers return (nostore_derive_key(dev, ARG, mode, rval)); 66930Sstevel@tonic-gate } 66940Sstevel@tonic-gate return (EINVAL); 66950Sstevel@tonic-gate } 66960Sstevel@tonic-gate 66970Sstevel@tonic-gate /* 66980Sstevel@tonic-gate * Check for the project.max-crypto-memory resource control. 66990Sstevel@tonic-gate */ 67000Sstevel@tonic-gate static int 67013916Skrishna crypto_buffer_check(size_t need) 67020Sstevel@tonic-gate { 67033620Skrishna kproject_t *kpj; 67040Sstevel@tonic-gate 67050Sstevel@tonic-gate if (need == 0) 67060Sstevel@tonic-gate return (CRYPTO_SUCCESS); 67070Sstevel@tonic-gate 67080Sstevel@tonic-gate mutex_enter(&curproc->p_lock); 67093620Skrishna kpj = curproc->p_task->tk_proj; 67103916Skrishna mutex_enter(&(kpj->kpj_data.kpd_crypto_lock)); 67113620Skrishna 67123620Skrishna if (kpj->kpj_data.kpd_crypto_mem + need > 67133620Skrishna kpj->kpj_data.kpd_crypto_mem_ctl) { 67143620Skrishna if (rctl_test(rc_project_crypto_mem, 67153620Skrishna kpj->kpj_rctls, curproc, need, 0) & RCT_DENY) { 67163916Skrishna mutex_exit(&(kpj->kpj_data.kpd_crypto_lock)); 67173620Skrishna mutex_exit(&curproc->p_lock); 67183620Skrishna return (CRYPTO_HOST_MEMORY); 67193620Skrishna } 67203620Skrishna } 67213620Skrishna 67223620Skrishna kpj->kpj_data.kpd_crypto_mem += need; 67233916Skrishna mutex_exit(&(kpj->kpj_data.kpd_crypto_lock)); 67243916Skrishna 67253916Skrishna curproc->p_crypto_mem += need; 67260Sstevel@tonic-gate mutex_exit(&curproc->p_lock); 67273620Skrishna 67280Sstevel@tonic-gate return (CRYPTO_SUCCESS); 67290Sstevel@tonic-gate } 6730