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 /*
2212223SVladimir.Kotal@Sun.COM * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate
250Sstevel@tonic-gate
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate * The ioctl interface for cryptographic commands.
280Sstevel@tonic-gate */
290Sstevel@tonic-gate
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/modctl.h>
320Sstevel@tonic-gate #include <sys/conf.h>
330Sstevel@tonic-gate #include <sys/stat.h>
340Sstevel@tonic-gate #include <sys/ddi.h>
350Sstevel@tonic-gate #include <sys/sunddi.h>
360Sstevel@tonic-gate #include <sys/kmem.h>
370Sstevel@tonic-gate #include <sys/errno.h>
380Sstevel@tonic-gate #include <sys/ksynch.h>
390Sstevel@tonic-gate #include <sys/file.h>
400Sstevel@tonic-gate #include <sys/open.h>
410Sstevel@tonic-gate #include <sys/cred.h>
420Sstevel@tonic-gate #include <sys/proc.h>
430Sstevel@tonic-gate #include <sys/task.h>
440Sstevel@tonic-gate #include <sys/mkdev.h>
450Sstevel@tonic-gate #include <sys/model.h>
460Sstevel@tonic-gate #include <sys/sysmacros.h>
470Sstevel@tonic-gate #include <sys/crypto/common.h>
480Sstevel@tonic-gate #include <sys/crypto/api.h>
490Sstevel@tonic-gate #include <sys/crypto/impl.h>
500Sstevel@tonic-gate #include <sys/crypto/sched_impl.h>
510Sstevel@tonic-gate #include <sys/crypto/ioctl.h>
520Sstevel@tonic-gate
534841Shaimay extern int kcf_des3_threshold;
544841Shaimay extern int kcf_aes_threshold;
554841Shaimay extern int kcf_rc4_threshold;
564841Shaimay extern int kcf_md5_threshold;
574841Shaimay extern int kcf_sha1_threshold;
584841Shaimay
590Sstevel@tonic-gate /*
600Sstevel@tonic-gate * Locking notes:
610Sstevel@tonic-gate *
629505SBhargava.Yenduri@Sun.COM * crypto_locks protects the global array of minor structures.
639505SBhargava.Yenduri@Sun.COM * crypto_locks is an array of locks indexed by the cpuid. A reader needs
649505SBhargava.Yenduri@Sun.COM * to hold a single lock while a writer needs to hold all locks.
659505SBhargava.Yenduri@Sun.COM * krwlock_t is not an option here because the hold time
669505SBhargava.Yenduri@Sun.COM * is very small for these locks.
670Sstevel@tonic-gate *
689505SBhargava.Yenduri@Sun.COM * The fields in the minor structure are protected by the cm_lock member
699505SBhargava.Yenduri@Sun.COM * of the minor structure. The cm_cv is used to signal decrements
709505SBhargava.Yenduri@Sun.COM * in the cm_refcnt, and is used with the cm_lock.
719505SBhargava.Yenduri@Sun.COM *
729505SBhargava.Yenduri@Sun.COM * The locking order is crypto_locks followed by cm_lock.
730Sstevel@tonic-gate */
740Sstevel@tonic-gate
750Sstevel@tonic-gate /*
760Sstevel@tonic-gate * DDI entry points.
770Sstevel@tonic-gate */
780Sstevel@tonic-gate static int crypto_attach(dev_info_t *, ddi_attach_cmd_t);
790Sstevel@tonic-gate static int crypto_detach(dev_info_t *, ddi_detach_cmd_t);
800Sstevel@tonic-gate static int crypto_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
810Sstevel@tonic-gate static int crypto_open(dev_t *, int, int, cred_t *);
820Sstevel@tonic-gate static int crypto_close(dev_t, int, int, cred_t *);
830Sstevel@tonic-gate static int crypto_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
840Sstevel@tonic-gate
85904Smcpowers static int cipher_init(dev_t, caddr_t, int, int (*)(crypto_provider_t,
860Sstevel@tonic-gate crypto_session_id_t, crypto_mechanism_t *, crypto_key_t *,
870Sstevel@tonic-gate crypto_ctx_template_t, crypto_context_t *, crypto_call_req_t *));
880Sstevel@tonic-gate
890Sstevel@tonic-gate static int common_digest(dev_t, caddr_t, int, int (*)(crypto_context_t,
900Sstevel@tonic-gate crypto_data_t *, crypto_data_t *, crypto_call_req_t *));
910Sstevel@tonic-gate
920Sstevel@tonic-gate static int cipher(dev_t, caddr_t, int, int (*)(crypto_context_t,
930Sstevel@tonic-gate crypto_data_t *, crypto_data_t *, crypto_call_req_t *));
940Sstevel@tonic-gate
950Sstevel@tonic-gate static int cipher_update(dev_t, caddr_t, int, int (*)(crypto_context_t,
960Sstevel@tonic-gate crypto_data_t *, crypto_data_t *, crypto_call_req_t *));
970Sstevel@tonic-gate
980Sstevel@tonic-gate static int common_final(dev_t, caddr_t, int, int (*)(crypto_context_t,
990Sstevel@tonic-gate crypto_data_t *, crypto_call_req_t *));
1000Sstevel@tonic-gate
101904Smcpowers static int sign_verify_init(dev_t, caddr_t, int, int (*)(crypto_provider_t,
1020Sstevel@tonic-gate crypto_session_id_t, crypto_mechanism_t *, crypto_key_t *,
1030Sstevel@tonic-gate crypto_ctx_template_t, crypto_context_t *, crypto_call_req_t *));
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate static int sign_verify_update(dev_t dev, caddr_t arg, int mode,
1060Sstevel@tonic-gate int (*)(crypto_context_t, crypto_data_t *, crypto_call_req_t *));
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate static void crypto_release_provider_session(crypto_minor_t *,
1090Sstevel@tonic-gate crypto_provider_session_t *);
1103916Skrishna static int crypto_buffer_check(size_t);
1110Sstevel@tonic-gate static int crypto_free_find_ctx(crypto_session_data_t *);
1120Sstevel@tonic-gate static int crypto_get_provider_list(crypto_minor_t *, uint_t *,
1130Sstevel@tonic-gate crypto_provider_entry_t **, boolean_t);
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate /* number of minor numbers to allocate at a time */
1160Sstevel@tonic-gate #define CRYPTO_MINOR_CHUNK 16
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate /*
1190Sstevel@tonic-gate * There are two limits associated with kernel memory. The first,
1200Sstevel@tonic-gate * CRYPTO_MAX_BUFFER_LEN, is the maximum number of bytes that can be
1210Sstevel@tonic-gate * allocated for a single copyin/copyout buffer. The second limit is
1220Sstevel@tonic-gate * the total number of bytes that can be allocated by a process
1230Sstevel@tonic-gate * for copyin/copyout buffers. The latter is enforced by the
1240Sstevel@tonic-gate * project.max-crypto-memory resource control.
1250Sstevel@tonic-gate */
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate #define CRYPTO_MAX_BUFFER_LEN (2 * 1024 * 1024)
1280Sstevel@tonic-gate #define CRYPTO_MAX_FIND_COUNT 512
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate /*
1316424Skrishna * We preapprove some bytes for each session to avoid making the costly
1326424Skrishna * crypto_buffer_check() calls. The preapproval is done when a new session
1336424Skrishna * is created and that cost is amortized over later crypto calls.
1346424Skrishna * Most applications create a session and then do a bunch of crypto calls
1356424Skrishna * in that session. So, they benefit from this optimization.
1366424Skrishna *
1376424Skrishna * Note that we may hit the project.max-crypto-memory limit a bit sooner
1386424Skrishna * because of this preapproval. But it is acceptable since the preapproved
1396424Skrishna * amount is insignificant compared to the default max-crypto-memory limit
1406424Skrishna * which is quarter of the machine's memory. The preapproved amount is
1416424Skrishna * roughly 2 * 16K(maximum SSL record size).
1420Sstevel@tonic-gate */
1436424Skrishna #define CRYPTO_PRE_APPROVED_LIMIT (32 * 1024)
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate /* The session table grows by CRYPTO_SESSION_CHUNK increments */
1460Sstevel@tonic-gate #define CRYPTO_SESSION_CHUNK 100
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate size_t crypto_max_buffer_len = CRYPTO_MAX_BUFFER_LEN;
1496424Skrishna size_t crypto_pre_approved_limit = CRYPTO_PRE_APPROVED_LIMIT;
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate #define INIT_RAW_CRYPTO_DATA(data, len) \
1520Sstevel@tonic-gate (data).cd_format = CRYPTO_DATA_RAW; \
1530Sstevel@tonic-gate (data).cd_raw.iov_base = kmem_alloc(len, KM_SLEEP); \
1540Sstevel@tonic-gate (data).cd_raw.iov_len = len; \
1550Sstevel@tonic-gate (data).cd_offset = 0; \
1560Sstevel@tonic-gate (data).cd_length = len;
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate static struct kmem_cache *crypto_session_cache;
1590Sstevel@tonic-gate static crypto_minor_t **crypto_minors = NULL;
1600Sstevel@tonic-gate static dev_info_t *crypto_dip = NULL;
1610Sstevel@tonic-gate static minor_t crypto_minor_chunk = CRYPTO_MINOR_CHUNK;
1620Sstevel@tonic-gate static minor_t crypto_minors_table_count = 0;
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate /*
1650Sstevel@tonic-gate * Minors are started from 1 because vmem_alloc()
1660Sstevel@tonic-gate * returns 0 in case of failure.
1670Sstevel@tonic-gate */
1680Sstevel@tonic-gate static vmem_t *crypto_arena = NULL; /* Arena for device minors */
1690Sstevel@tonic-gate static minor_t crypto_minors_count = 0;
1709505SBhargava.Yenduri@Sun.COM static kcf_lock_withpad_t *crypto_locks;
1719505SBhargava.Yenduri@Sun.COM
1729505SBhargava.Yenduri@Sun.COM #define CRYPTO_ENTER_ALL_LOCKS() \
1739505SBhargava.Yenduri@Sun.COM for (i = 0; i < max_ncpus; i++) \
1749505SBhargava.Yenduri@Sun.COM mutex_enter(&crypto_locks[i].kl_lock);
1759505SBhargava.Yenduri@Sun.COM
1769505SBhargava.Yenduri@Sun.COM #define CRYPTO_EXIT_ALL_LOCKS() \
1779505SBhargava.Yenduri@Sun.COM for (i = 0; i < max_ncpus; i++) \
1789505SBhargava.Yenduri@Sun.COM mutex_exit(&crypto_locks[i].kl_lock);
1790Sstevel@tonic-gate
1800Sstevel@tonic-gate #define RETURN_LIST B_TRUE
1810Sstevel@tonic-gate #define DONT_RETURN_LIST B_FALSE
1820Sstevel@tonic-gate
183904Smcpowers #define CRYPTO_OPS_OFFSET(f) offsetof(crypto_ops_t, co_##f)
1840Sstevel@tonic-gate #define CRYPTO_RANDOM_OFFSET(f) offsetof(crypto_random_number_ops_t, f)
1850Sstevel@tonic-gate #define CRYPTO_SESSION_OFFSET(f) offsetof(crypto_session_ops_t, f)
1860Sstevel@tonic-gate #define CRYPTO_OBJECT_OFFSET(f) offsetof(crypto_object_ops_t, f)
1870Sstevel@tonic-gate #define CRYPTO_PROVIDER_OFFSET(f) \
1880Sstevel@tonic-gate offsetof(crypto_provider_management_ops_t, f)
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate #define CRYPTO_CANCEL_CTX(spp) { \
1910Sstevel@tonic-gate crypto_cancel_ctx(*(spp)); \
1920Sstevel@tonic-gate *(spp) = NULL; \
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate #define CRYPTO_CANCEL_ALL_CTX(sp) { \
1960Sstevel@tonic-gate if ((sp)->sd_digest_ctx != NULL) { \
1970Sstevel@tonic-gate crypto_cancel_ctx((sp)->sd_digest_ctx); \
1980Sstevel@tonic-gate (sp)->sd_digest_ctx = NULL; \
1990Sstevel@tonic-gate } \
2000Sstevel@tonic-gate if ((sp)->sd_encr_ctx != NULL) { \
2010Sstevel@tonic-gate crypto_cancel_ctx((sp)->sd_encr_ctx); \
2020Sstevel@tonic-gate (sp)->sd_encr_ctx = NULL; \
2030Sstevel@tonic-gate } \
2040Sstevel@tonic-gate if ((sp)->sd_decr_ctx != NULL) { \
2050Sstevel@tonic-gate crypto_cancel_ctx((sp)->sd_decr_ctx); \
2060Sstevel@tonic-gate (sp)->sd_decr_ctx = NULL; \
2070Sstevel@tonic-gate } \
2080Sstevel@tonic-gate if ((sp)->sd_sign_ctx != NULL) { \
2090Sstevel@tonic-gate crypto_cancel_ctx((sp)->sd_sign_ctx); \
2100Sstevel@tonic-gate (sp)->sd_sign_ctx = NULL; \
2110Sstevel@tonic-gate } \
2120Sstevel@tonic-gate if ((sp)->sd_verify_ctx != NULL) { \
2130Sstevel@tonic-gate crypto_cancel_ctx((sp)->sd_verify_ctx); \
2140Sstevel@tonic-gate (sp)->sd_verify_ctx = NULL; \
2150Sstevel@tonic-gate } \
2160Sstevel@tonic-gate if ((sp)->sd_sign_recover_ctx != NULL) { \
2170Sstevel@tonic-gate crypto_cancel_ctx((sp)->sd_sign_recover_ctx); \
2180Sstevel@tonic-gate (sp)->sd_sign_recover_ctx = NULL; \
2190Sstevel@tonic-gate } \
2200Sstevel@tonic-gate if ((sp)->sd_verify_recover_ctx != NULL) { \
2210Sstevel@tonic-gate crypto_cancel_ctx((sp)->sd_verify_recover_ctx); \
2220Sstevel@tonic-gate (sp)->sd_verify_recover_ctx = NULL; \
2230Sstevel@tonic-gate } \
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate
2266424Skrishna #define CRYPTO_DECREMENT_RCTL(val) if ((val) != 0) { \
2273916Skrishna kproject_t *projp; \
2283916Skrishna mutex_enter(&curproc->p_lock); \
2293916Skrishna projp = curproc->p_task->tk_proj; \
2303916Skrishna ASSERT(projp != NULL); \
2313916Skrishna mutex_enter(&(projp->kpj_data.kpd_crypto_lock)); \
2323916Skrishna projp->kpj_data.kpd_crypto_mem -= (val); \
2333916Skrishna mutex_exit(&(projp->kpj_data.kpd_crypto_lock)); \
2343916Skrishna curproc->p_crypto_mem -= (val); \
2353916Skrishna mutex_exit(&curproc->p_lock); \
2360Sstevel@tonic-gate }
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate /*
2396424Skrishna * We do not need to hold sd_lock in the macros below
2406424Skrishna * as they are called after doing a get_session_ptr() which
2416424Skrishna * sets the CRYPTO_SESSION_IS_BUSY flag.
2426424Skrishna */
2436424Skrishna #define CRYPTO_DECREMENT_RCTL_SESSION(sp, val, rctl_chk) \
2446867Skrishna if (((val) != 0) && ((sp) != NULL)) { \
2456424Skrishna ASSERT(((sp)->sd_flags & CRYPTO_SESSION_IS_BUSY) != 0); \
2466424Skrishna if (rctl_chk) { \
2476424Skrishna CRYPTO_DECREMENT_RCTL(val); \
2486424Skrishna } else { \
2496424Skrishna (sp)->sd_pre_approved_amount += (val); \
2506424Skrishna } \
2516424Skrishna }
2526424Skrishna
2536424Skrishna #define CRYPTO_BUFFER_CHECK(sp, need, rctl_chk) \
2546424Skrishna ((sp->sd_pre_approved_amount >= need) ? \
2556424Skrishna (sp->sd_pre_approved_amount -= need, \
2566424Skrishna rctl_chk = B_FALSE, CRYPTO_SUCCESS) : \
2576424Skrishna (rctl_chk = B_TRUE, crypto_buffer_check(need)))
2586424Skrishna
2596424Skrishna /*
2600Sstevel@tonic-gate * Module linkage.
2610Sstevel@tonic-gate */
2620Sstevel@tonic-gate static struct cb_ops cbops = {
2630Sstevel@tonic-gate crypto_open, /* cb_open */
2640Sstevel@tonic-gate crypto_close, /* cb_close */
2650Sstevel@tonic-gate nodev, /* cb_strategy */
2660Sstevel@tonic-gate nodev, /* cb_print */
2670Sstevel@tonic-gate nodev, /* cb_dump */
2680Sstevel@tonic-gate nodev, /* cb_read */
2690Sstevel@tonic-gate nodev, /* cb_write */
2700Sstevel@tonic-gate crypto_ioctl, /* cb_ioctl */
2710Sstevel@tonic-gate nodev, /* cb_devmap */
2720Sstevel@tonic-gate nodev, /* cb_mmap */
2730Sstevel@tonic-gate nodev, /* cb_segmap */
2740Sstevel@tonic-gate nochpoll, /* cb_chpoll */
2750Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */
2760Sstevel@tonic-gate NULL, /* cb_streamtab */
2770Sstevel@tonic-gate D_MP, /* cb_flag */
2780Sstevel@tonic-gate CB_REV, /* cb_rev */
2790Sstevel@tonic-gate nodev, /* cb_aread */
2800Sstevel@tonic-gate nodev, /* cb_awrite */
2810Sstevel@tonic-gate };
2820Sstevel@tonic-gate
2830Sstevel@tonic-gate static struct dev_ops devops = {
2840Sstevel@tonic-gate DEVO_REV, /* devo_rev */
2850Sstevel@tonic-gate 0, /* devo_refcnt */
2860Sstevel@tonic-gate crypto_getinfo, /* devo_getinfo */
2870Sstevel@tonic-gate nulldev, /* devo_identify */
2880Sstevel@tonic-gate nulldev, /* devo_probe */
2890Sstevel@tonic-gate crypto_attach, /* devo_attach */
2900Sstevel@tonic-gate crypto_detach, /* devo_detach */
2910Sstevel@tonic-gate nodev, /* devo_reset */
2920Sstevel@tonic-gate &cbops, /* devo_cb_ops */
2930Sstevel@tonic-gate NULL, /* devo_bus_ops */
2940Sstevel@tonic-gate NULL, /* devo_power */
2957656SSherry.Moore@Sun.COM ddi_quiesce_not_needed, /* devo_quiesce */
2960Sstevel@tonic-gate };
2970Sstevel@tonic-gate
2980Sstevel@tonic-gate static struct modldrv modldrv = {
2990Sstevel@tonic-gate &mod_driverops, /* drv_modops */
3005072Smcpowers "Cryptographic Library Interface", /* drv_linkinfo */
3010Sstevel@tonic-gate &devops,
3020Sstevel@tonic-gate };
3030Sstevel@tonic-gate
3040Sstevel@tonic-gate static struct modlinkage modlinkage = {
3050Sstevel@tonic-gate MODREV_1, /* ml_rev */
3060Sstevel@tonic-gate &modldrv, /* ml_linkage */
3070Sstevel@tonic-gate NULL
3080Sstevel@tonic-gate };
3090Sstevel@tonic-gate
3100Sstevel@tonic-gate /*
3110Sstevel@tonic-gate * DDI entry points.
3120Sstevel@tonic-gate */
3130Sstevel@tonic-gate int
_init(void)3140Sstevel@tonic-gate _init(void)
3150Sstevel@tonic-gate {
3160Sstevel@tonic-gate return (mod_install(&modlinkage));
3170Sstevel@tonic-gate }
3180Sstevel@tonic-gate
3190Sstevel@tonic-gate int
_fini(void)3200Sstevel@tonic-gate _fini(void)
3210Sstevel@tonic-gate {
3220Sstevel@tonic-gate return (mod_remove(&modlinkage));
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate
3250Sstevel@tonic-gate int
_info(struct modinfo * modinfop)3260Sstevel@tonic-gate _info(struct modinfo *modinfop)
3270Sstevel@tonic-gate {
3280Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
3290Sstevel@tonic-gate }
3300Sstevel@tonic-gate
3310Sstevel@tonic-gate /* ARGSUSED */
3320Sstevel@tonic-gate static int
crypto_getinfo(dev_info_t * dip,ddi_info_cmd_t cmd,void * arg,void ** result)3330Sstevel@tonic-gate crypto_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
3340Sstevel@tonic-gate {
3350Sstevel@tonic-gate switch (cmd) {
3360Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO:
3370Sstevel@tonic-gate *result = crypto_dip;
3380Sstevel@tonic-gate return (DDI_SUCCESS);
3390Sstevel@tonic-gate
3400Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE:
3410Sstevel@tonic-gate *result = (void *)0;
3420Sstevel@tonic-gate return (DDI_SUCCESS);
3430Sstevel@tonic-gate }
3440Sstevel@tonic-gate return (DDI_FAILURE);
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate
3470Sstevel@tonic-gate static int
crypto_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)3480Sstevel@tonic-gate crypto_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3490Sstevel@tonic-gate {
3509505SBhargava.Yenduri@Sun.COM int i;
3519505SBhargava.Yenduri@Sun.COM
3520Sstevel@tonic-gate if (cmd != DDI_ATTACH) {
3530Sstevel@tonic-gate return (DDI_FAILURE);
3540Sstevel@tonic-gate }
3550Sstevel@tonic-gate
3560Sstevel@tonic-gate if (ddi_get_instance(dip) != 0) {
3570Sstevel@tonic-gate /* we only allow instance 0 to attach */
3580Sstevel@tonic-gate return (DDI_FAILURE);
3590Sstevel@tonic-gate }
3600Sstevel@tonic-gate
3610Sstevel@tonic-gate crypto_session_cache = kmem_cache_create("crypto_session_cache",
3620Sstevel@tonic-gate sizeof (crypto_session_data_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
3630Sstevel@tonic-gate
3640Sstevel@tonic-gate if (crypto_session_cache == NULL)
3650Sstevel@tonic-gate return (DDI_FAILURE);
3660Sstevel@tonic-gate
3670Sstevel@tonic-gate /* create the minor node */
3680Sstevel@tonic-gate if (ddi_create_minor_node(dip, "crypto", S_IFCHR, 0,
3690Sstevel@tonic-gate DDI_PSEUDO, 0) != DDI_SUCCESS) {
3700Sstevel@tonic-gate kmem_cache_destroy(crypto_session_cache);
3710Sstevel@tonic-gate crypto_session_cache = NULL;
3720Sstevel@tonic-gate cmn_err(CE_WARN, "crypto_attach: failed creating minor node");
3730Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL);
3740Sstevel@tonic-gate return (DDI_FAILURE);
3750Sstevel@tonic-gate }
3760Sstevel@tonic-gate
3779505SBhargava.Yenduri@Sun.COM crypto_locks = kmem_zalloc(max_ncpus * sizeof (kcf_lock_withpad_t),
3789505SBhargava.Yenduri@Sun.COM KM_SLEEP);
3799505SBhargava.Yenduri@Sun.COM for (i = 0; i < max_ncpus; i++)
3809505SBhargava.Yenduri@Sun.COM mutex_init(&crypto_locks[i].kl_lock, NULL, MUTEX_DRIVER, NULL);
3819505SBhargava.Yenduri@Sun.COM
3820Sstevel@tonic-gate crypto_dip = dip;
3830Sstevel@tonic-gate
3840Sstevel@tonic-gate /* allocate integer space for minor numbers */
3850Sstevel@tonic-gate crypto_arena = vmem_create("crypto", (void *)1,
3860Sstevel@tonic-gate CRYPTO_MINOR_CHUNK, 1, NULL, NULL, NULL, 0,
3870Sstevel@tonic-gate VM_SLEEP | VMC_IDENTIFIER);
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate return (DDI_SUCCESS);
3900Sstevel@tonic-gate }
3910Sstevel@tonic-gate
3920Sstevel@tonic-gate static int
crypto_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)3930Sstevel@tonic-gate crypto_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3940Sstevel@tonic-gate {
3950Sstevel@tonic-gate minor_t i;
3969505SBhargava.Yenduri@Sun.COM kcf_lock_withpad_t *mp;
3970Sstevel@tonic-gate
3980Sstevel@tonic-gate if (cmd != DDI_DETACH)
3990Sstevel@tonic-gate return (DDI_FAILURE);
4000Sstevel@tonic-gate
4019505SBhargava.Yenduri@Sun.COM mp = &crypto_locks[CPU_SEQID];
4029505SBhargava.Yenduri@Sun.COM mutex_enter(&mp->kl_lock);
4039505SBhargava.Yenduri@Sun.COM
4040Sstevel@tonic-gate /* check if device is open */
4050Sstevel@tonic-gate for (i = 0; i < crypto_minors_table_count; i++) {
4060Sstevel@tonic-gate if (crypto_minors[i] != NULL) {
4079505SBhargava.Yenduri@Sun.COM mutex_exit(&mp->kl_lock);
4080Sstevel@tonic-gate return (DDI_FAILURE);
4090Sstevel@tonic-gate }
4100Sstevel@tonic-gate }
4119505SBhargava.Yenduri@Sun.COM mutex_exit(&mp->kl_lock);
4120Sstevel@tonic-gate
4130Sstevel@tonic-gate crypto_dip = NULL;
4140Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL);
4150Sstevel@tonic-gate
4160Sstevel@tonic-gate kmem_cache_destroy(crypto_session_cache);
4170Sstevel@tonic-gate crypto_session_cache = NULL;
4180Sstevel@tonic-gate
4190Sstevel@tonic-gate kmem_free(crypto_minors,
4200Sstevel@tonic-gate sizeof (crypto_minor_t *) * crypto_minors_table_count);
4210Sstevel@tonic-gate crypto_minors = NULL;
4220Sstevel@tonic-gate crypto_minors_table_count = 0;
4239505SBhargava.Yenduri@Sun.COM for (i = 0; i < max_ncpus; i++)
4249505SBhargava.Yenduri@Sun.COM mutex_destroy(&crypto_locks[i].kl_lock);
4259662SBhargava.Yenduri@Sun.COM kmem_free(crypto_locks, max_ncpus * sizeof (kcf_lock_withpad_t));
4269662SBhargava.Yenduri@Sun.COM crypto_locks = NULL;
4279505SBhargava.Yenduri@Sun.COM
4280Sstevel@tonic-gate vmem_destroy(crypto_arena);
4290Sstevel@tonic-gate crypto_arena = NULL;
4300Sstevel@tonic-gate
4310Sstevel@tonic-gate return (DDI_SUCCESS);
4320Sstevel@tonic-gate }
4330Sstevel@tonic-gate
43411413Sopensolaris@drydog.com /* ARGSUSED3 */
4350Sstevel@tonic-gate static int
crypto_open(dev_t * devp,int flag,int otyp,cred_t * credp)4360Sstevel@tonic-gate crypto_open(dev_t *devp, int flag, int otyp, cred_t *credp)
4370Sstevel@tonic-gate {
4380Sstevel@tonic-gate crypto_minor_t *cm = NULL;
4390Sstevel@tonic-gate minor_t mn;
4409505SBhargava.Yenduri@Sun.COM kcf_lock_withpad_t *mp;
4419505SBhargava.Yenduri@Sun.COM int i;
4420Sstevel@tonic-gate
4430Sstevel@tonic-gate if (otyp != OTYP_CHR)
4440Sstevel@tonic-gate return (ENXIO);
4450Sstevel@tonic-gate
4460Sstevel@tonic-gate if (crypto_dip == NULL)
4470Sstevel@tonic-gate return (ENXIO);
4480Sstevel@tonic-gate
4490Sstevel@tonic-gate /* exclusive opens are not supported */
4500Sstevel@tonic-gate if (flag & FEXCL)
4510Sstevel@tonic-gate return (ENOTSUP);
4520Sstevel@tonic-gate
4530Sstevel@tonic-gate again:
4549505SBhargava.Yenduri@Sun.COM mp = &crypto_locks[CPU_SEQID];
4559505SBhargava.Yenduri@Sun.COM mutex_enter(&mp->kl_lock);
4569505SBhargava.Yenduri@Sun.COM
4570Sstevel@tonic-gate /* grow the minors table if needed */
4580Sstevel@tonic-gate if (crypto_minors_count >= crypto_minors_table_count) {
4590Sstevel@tonic-gate crypto_minor_t **newtable;
4600Sstevel@tonic-gate minor_t chunk = crypto_minor_chunk;
4610Sstevel@tonic-gate minor_t saved_count;
4620Sstevel@tonic-gate size_t new_size;
4630Sstevel@tonic-gate ulong_t big_count;
4640Sstevel@tonic-gate
4650Sstevel@tonic-gate big_count = crypto_minors_count + chunk;
4660Sstevel@tonic-gate if (big_count > MAXMIN) {
4679505SBhargava.Yenduri@Sun.COM mutex_exit(&mp->kl_lock);
4680Sstevel@tonic-gate return (ENOMEM);
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate
4710Sstevel@tonic-gate saved_count = crypto_minors_table_count;
4720Sstevel@tonic-gate new_size = sizeof (crypto_minor_t *) *
4730Sstevel@tonic-gate (crypto_minors_table_count + chunk);
4740Sstevel@tonic-gate
4759505SBhargava.Yenduri@Sun.COM mutex_exit(&mp->kl_lock);
4769505SBhargava.Yenduri@Sun.COM
4770Sstevel@tonic-gate newtable = kmem_zalloc(new_size, KM_SLEEP);
4789505SBhargava.Yenduri@Sun.COM CRYPTO_ENTER_ALL_LOCKS();
4790Sstevel@tonic-gate /*
4800Sstevel@tonic-gate * Check if table grew while we were sleeping.
4810Sstevel@tonic-gate * The minors table never shrinks.
4820Sstevel@tonic-gate */
4830Sstevel@tonic-gate if (crypto_minors_table_count > saved_count) {
4849505SBhargava.Yenduri@Sun.COM CRYPTO_EXIT_ALL_LOCKS();
4850Sstevel@tonic-gate kmem_free(newtable, new_size);
4860Sstevel@tonic-gate goto again;
4870Sstevel@tonic-gate }
4880Sstevel@tonic-gate
4890Sstevel@tonic-gate /* we assume that bcopy() will return if count is 0 */
4900Sstevel@tonic-gate bcopy(crypto_minors, newtable,
4910Sstevel@tonic-gate sizeof (crypto_minor_t *) * crypto_minors_table_count);
4920Sstevel@tonic-gate
4930Sstevel@tonic-gate kmem_free(crypto_minors,
4940Sstevel@tonic-gate sizeof (crypto_minor_t *) * crypto_minors_table_count);
4950Sstevel@tonic-gate
4960Sstevel@tonic-gate /* grow the minors number space */
4970Sstevel@tonic-gate if (crypto_minors_table_count != 0) {
4980Sstevel@tonic-gate (void) vmem_add(crypto_arena,
4990Sstevel@tonic-gate (void *)(uintptr_t)(crypto_minors_table_count + 1),
5000Sstevel@tonic-gate crypto_minor_chunk, VM_SLEEP);
5010Sstevel@tonic-gate }
5020Sstevel@tonic-gate
5030Sstevel@tonic-gate crypto_minors = newtable;
5040Sstevel@tonic-gate crypto_minors_table_count += chunk;
5059505SBhargava.Yenduri@Sun.COM CRYPTO_EXIT_ALL_LOCKS();
5069505SBhargava.Yenduri@Sun.COM } else {
5079505SBhargava.Yenduri@Sun.COM mutex_exit(&mp->kl_lock);
5089505SBhargava.Yenduri@Sun.COM }
5090Sstevel@tonic-gate
5100Sstevel@tonic-gate /* allocate a new minor number starting with 1 */
5110Sstevel@tonic-gate mn = (minor_t)(uintptr_t)vmem_alloc(crypto_arena, 1, VM_SLEEP);
5120Sstevel@tonic-gate
5130Sstevel@tonic-gate cm = kmem_zalloc(sizeof (crypto_minor_t), KM_SLEEP);
5140Sstevel@tonic-gate mutex_init(&cm->cm_lock, NULL, MUTEX_DRIVER, NULL);
5150Sstevel@tonic-gate cv_init(&cm->cm_cv, NULL, CV_DRIVER, NULL);
5160Sstevel@tonic-gate
5179505SBhargava.Yenduri@Sun.COM CRYPTO_ENTER_ALL_LOCKS();
5187882SBhargava.Yenduri@Sun.COM cm->cm_refcnt = 1;
5190Sstevel@tonic-gate crypto_minors[mn - 1] = cm;
5200Sstevel@tonic-gate crypto_minors_count++;
5219505SBhargava.Yenduri@Sun.COM CRYPTO_EXIT_ALL_LOCKS();
5220Sstevel@tonic-gate
5230Sstevel@tonic-gate *devp = makedevice(getmajor(*devp), mn);
5240Sstevel@tonic-gate
5250Sstevel@tonic-gate return (0);
5260Sstevel@tonic-gate }
5270Sstevel@tonic-gate
52811413Sopensolaris@drydog.com /* ARGSUSED1 */
5290Sstevel@tonic-gate static int
crypto_close(dev_t dev,int flag,int otyp,cred_t * credp)5300Sstevel@tonic-gate crypto_close(dev_t dev, int flag, int otyp, cred_t *credp)
5310Sstevel@tonic-gate {
5320Sstevel@tonic-gate crypto_minor_t *cm = NULL;
5330Sstevel@tonic-gate crypto_session_data_t *sp;
5340Sstevel@tonic-gate minor_t mn = getminor(dev);
5350Sstevel@tonic-gate uint_t i;
5366424Skrishna size_t total = 0;
5379505SBhargava.Yenduri@Sun.COM kcf_lock_withpad_t *mp;
5389505SBhargava.Yenduri@Sun.COM
5399505SBhargava.Yenduri@Sun.COM mp = &crypto_locks[CPU_SEQID];
5409505SBhargava.Yenduri@Sun.COM mutex_enter(&mp->kl_lock);
5419505SBhargava.Yenduri@Sun.COM
5420Sstevel@tonic-gate if (mn > crypto_minors_table_count) {
5439505SBhargava.Yenduri@Sun.COM mutex_exit(&mp->kl_lock);
5440Sstevel@tonic-gate cmn_err(CE_WARN, "crypto_close: bad minor (too big) %d", mn);
5450Sstevel@tonic-gate return (ENODEV);
5460Sstevel@tonic-gate }
5470Sstevel@tonic-gate
5487882SBhargava.Yenduri@Sun.COM cm = crypto_minors[mn - 1];
5490Sstevel@tonic-gate if (cm == NULL) {
5509505SBhargava.Yenduri@Sun.COM mutex_exit(&mp->kl_lock);
5510Sstevel@tonic-gate cmn_err(CE_WARN, "crypto_close: duplicate close of minor %d",
5520Sstevel@tonic-gate getminor(dev));
5530Sstevel@tonic-gate return (ENODEV);
5540Sstevel@tonic-gate }
5550Sstevel@tonic-gate
5569505SBhargava.Yenduri@Sun.COM mutex_exit(&mp->kl_lock);
5579505SBhargava.Yenduri@Sun.COM
5589505SBhargava.Yenduri@Sun.COM CRYPTO_ENTER_ALL_LOCKS();
5599505SBhargava.Yenduri@Sun.COM /*
5609505SBhargava.Yenduri@Sun.COM * We free the minor number, mn, from the crypto_arena
5619505SBhargava.Yenduri@Sun.COM * only later. This ensures that we won't race with another
5629505SBhargava.Yenduri@Sun.COM * thread in crypto_open with the same minor number.
5639505SBhargava.Yenduri@Sun.COM */
5649505SBhargava.Yenduri@Sun.COM crypto_minors[mn - 1] = NULL;
5659505SBhargava.Yenduri@Sun.COM crypto_minors_count--;
5669505SBhargava.Yenduri@Sun.COM CRYPTO_EXIT_ALL_LOCKS();
5679505SBhargava.Yenduri@Sun.COM
5689505SBhargava.Yenduri@Sun.COM mutex_enter(&cm->cm_lock);
5697882SBhargava.Yenduri@Sun.COM cm->cm_refcnt --; /* decrement refcnt held in open */
5707882SBhargava.Yenduri@Sun.COM while (cm->cm_refcnt > 0) {
5719505SBhargava.Yenduri@Sun.COM cv_wait(&cm->cm_cv, &cm->cm_lock);
5729505SBhargava.Yenduri@Sun.COM }
5730Sstevel@tonic-gate
5740Sstevel@tonic-gate vmem_free(crypto_arena, (void *)(uintptr_t)mn, 1);
5750Sstevel@tonic-gate
5760Sstevel@tonic-gate /* free all session table entries starting with 1 */
5770Sstevel@tonic-gate for (i = 1; i < cm->cm_session_table_count; i++) {
5780Sstevel@tonic-gate if (cm->cm_session_table[i] == NULL)
5790Sstevel@tonic-gate continue;
5800Sstevel@tonic-gate
5810Sstevel@tonic-gate sp = cm->cm_session_table[i];
5820Sstevel@tonic-gate ASSERT((sp->sd_flags & CRYPTO_SESSION_IS_BUSY) == 0);
5836424Skrishna ASSERT(sp->sd_pre_approved_amount == 0 ||
5846424Skrishna sp->sd_pre_approved_amount == crypto_pre_approved_limit);
5856424Skrishna total += sp->sd_pre_approved_amount;
5860Sstevel@tonic-gate if (sp->sd_find_init_cookie != NULL) {
5870Sstevel@tonic-gate (void) crypto_free_find_ctx(sp);
5880Sstevel@tonic-gate }
5890Sstevel@tonic-gate crypto_release_provider_session(cm, sp->sd_provider_session);
5900Sstevel@tonic-gate KCF_PROV_REFRELE(sp->sd_provider);
5910Sstevel@tonic-gate CRYPTO_CANCEL_ALL_CTX(sp);
5920Sstevel@tonic-gate mutex_destroy(&sp->sd_lock);
5930Sstevel@tonic-gate cv_destroy(&sp->sd_cv);
5940Sstevel@tonic-gate kmem_cache_free(crypto_session_cache, sp);
5950Sstevel@tonic-gate cm->cm_session_table[i] = NULL;
5960Sstevel@tonic-gate }
5970Sstevel@tonic-gate
5980Sstevel@tonic-gate /* free the session table */
5990Sstevel@tonic-gate if (cm->cm_session_table != NULL && cm->cm_session_table_count > 0)
6000Sstevel@tonic-gate kmem_free(cm->cm_session_table, cm->cm_session_table_count *
6010Sstevel@tonic-gate sizeof (void *));
6020Sstevel@tonic-gate
6036424Skrishna total += (cm->cm_session_table_count * sizeof (void *));
6046424Skrishna CRYPTO_DECREMENT_RCTL(total);
6050Sstevel@tonic-gate
6060Sstevel@tonic-gate kcf_free_provider_tab(cm->cm_provider_count,
6070Sstevel@tonic-gate cm->cm_provider_array);
6080Sstevel@tonic-gate
6099505SBhargava.Yenduri@Sun.COM mutex_exit(&cm->cm_lock);
6100Sstevel@tonic-gate mutex_destroy(&cm->cm_lock);
6110Sstevel@tonic-gate cv_destroy(&cm->cm_cv);
6120Sstevel@tonic-gate kmem_free(cm, sizeof (crypto_minor_t));
6130Sstevel@tonic-gate
6140Sstevel@tonic-gate return (0);
6150Sstevel@tonic-gate }
6160Sstevel@tonic-gate
6170Sstevel@tonic-gate static crypto_minor_t *
crypto_hold_minor(minor_t minor)6180Sstevel@tonic-gate crypto_hold_minor(minor_t minor)
6190Sstevel@tonic-gate {
6207882SBhargava.Yenduri@Sun.COM crypto_minor_t *cm;
6219505SBhargava.Yenduri@Sun.COM kcf_lock_withpad_t *mp;
6227882SBhargava.Yenduri@Sun.COM
6237882SBhargava.Yenduri@Sun.COM if (minor > crypto_minors_table_count)
6247882SBhargava.Yenduri@Sun.COM return (NULL);
6250Sstevel@tonic-gate
6269505SBhargava.Yenduri@Sun.COM mp = &crypto_locks[CPU_SEQID];
6279505SBhargava.Yenduri@Sun.COM mutex_enter(&mp->kl_lock);
6289505SBhargava.Yenduri@Sun.COM
6297882SBhargava.Yenduri@Sun.COM if ((cm = crypto_minors[minor - 1]) != NULL) {
6309505SBhargava.Yenduri@Sun.COM atomic_add_32(&cm->cm_refcnt, 1);
6319505SBhargava.Yenduri@Sun.COM }
6329505SBhargava.Yenduri@Sun.COM mutex_exit(&mp->kl_lock);
6330Sstevel@tonic-gate return (cm);
6340Sstevel@tonic-gate }
6350Sstevel@tonic-gate
6360Sstevel@tonic-gate static void
crypto_release_minor(crypto_minor_t * cm)6370Sstevel@tonic-gate crypto_release_minor(crypto_minor_t *cm)
6380Sstevel@tonic-gate {
6399505SBhargava.Yenduri@Sun.COM if (atomic_add_32_nv(&cm->cm_refcnt, -1) == 0) {
6407882SBhargava.Yenduri@Sun.COM cv_signal(&cm->cm_cv);
6410Sstevel@tonic-gate }
6420Sstevel@tonic-gate }
6430Sstevel@tonic-gate
6440Sstevel@tonic-gate /*
6454072Skrishna * Build a list of functions and other information for the provider, pd.
6460Sstevel@tonic-gate */
6470Sstevel@tonic-gate static void
crypto_build_function_list(crypto_function_list_t * fl,kcf_provider_desc_t * pd)6480Sstevel@tonic-gate crypto_build_function_list(crypto_function_list_t *fl, kcf_provider_desc_t *pd)
6490Sstevel@tonic-gate {
6500Sstevel@tonic-gate crypto_ops_t *ops;
6510Sstevel@tonic-gate crypto_digest_ops_t *digest_ops;
6520Sstevel@tonic-gate crypto_cipher_ops_t *cipher_ops;
6530Sstevel@tonic-gate crypto_mac_ops_t *mac_ops;
6540Sstevel@tonic-gate crypto_sign_ops_t *sign_ops;
6550Sstevel@tonic-gate crypto_verify_ops_t *verify_ops;
6560Sstevel@tonic-gate crypto_dual_ops_t *dual_ops;
6570Sstevel@tonic-gate crypto_random_number_ops_t *random_number_ops;
6580Sstevel@tonic-gate crypto_session_ops_t *session_ops;
6590Sstevel@tonic-gate crypto_object_ops_t *object_ops;
6600Sstevel@tonic-gate crypto_key_ops_t *key_ops;
6610Sstevel@tonic-gate crypto_provider_management_ops_t *provider_ops;
6620Sstevel@tonic-gate
6630Sstevel@tonic-gate if ((ops = pd->pd_ops_vector) == NULL)
6640Sstevel@tonic-gate return;
6650Sstevel@tonic-gate
666904Smcpowers if ((digest_ops = ops->co_digest_ops) != NULL) {
6670Sstevel@tonic-gate if (digest_ops->digest_init != NULL)
6680Sstevel@tonic-gate fl->fl_digest_init = B_TRUE;
6690Sstevel@tonic-gate if (digest_ops->digest != NULL)
6700Sstevel@tonic-gate fl->fl_digest = B_TRUE;
6710Sstevel@tonic-gate if (digest_ops->digest_update != NULL)
6720Sstevel@tonic-gate fl->fl_digest_update = B_TRUE;
6730Sstevel@tonic-gate if (digest_ops->digest_key != NULL)
6740Sstevel@tonic-gate fl->fl_digest_key = B_TRUE;
6750Sstevel@tonic-gate if (digest_ops->digest_final != NULL)
6760Sstevel@tonic-gate fl->fl_digest_final = B_TRUE;
6770Sstevel@tonic-gate }
678904Smcpowers if ((cipher_ops = ops->co_cipher_ops) != NULL) {
6790Sstevel@tonic-gate if (cipher_ops->encrypt_init != NULL)
6800Sstevel@tonic-gate fl->fl_encrypt_init = B_TRUE;
6810Sstevel@tonic-gate if (cipher_ops->encrypt != NULL)
6820Sstevel@tonic-gate fl->fl_encrypt = B_TRUE;
6830Sstevel@tonic-gate if (cipher_ops->encrypt_update != NULL)
6840Sstevel@tonic-gate fl->fl_encrypt_update = B_TRUE;
6850Sstevel@tonic-gate if (cipher_ops->encrypt_final != NULL)
6860Sstevel@tonic-gate fl->fl_encrypt_final = B_TRUE;
6870Sstevel@tonic-gate if (cipher_ops->decrypt_init != NULL)
6880Sstevel@tonic-gate fl->fl_decrypt_init = B_TRUE;
6890Sstevel@tonic-gate if (cipher_ops->decrypt != NULL)
6900Sstevel@tonic-gate fl->fl_decrypt = B_TRUE;
6910Sstevel@tonic-gate if (cipher_ops->decrypt_update != NULL)
6920Sstevel@tonic-gate fl->fl_decrypt_update = B_TRUE;
6930Sstevel@tonic-gate if (cipher_ops->decrypt_final != NULL)
6940Sstevel@tonic-gate fl->fl_decrypt_final = B_TRUE;
6950Sstevel@tonic-gate }
696904Smcpowers if ((mac_ops = ops->co_mac_ops) != NULL) {
6970Sstevel@tonic-gate if (mac_ops->mac_init != NULL)
6980Sstevel@tonic-gate fl->fl_mac_init = B_TRUE;
6990Sstevel@tonic-gate if (mac_ops->mac != NULL)
7000Sstevel@tonic-gate fl->fl_mac = B_TRUE;
7010Sstevel@tonic-gate if (mac_ops->mac_update != NULL)
7020Sstevel@tonic-gate fl->fl_mac_update = B_TRUE;
7030Sstevel@tonic-gate if (mac_ops->mac_final != NULL)
7040Sstevel@tonic-gate fl->fl_mac_final = B_TRUE;
7050Sstevel@tonic-gate }
706904Smcpowers if ((sign_ops = ops->co_sign_ops) != NULL) {
7070Sstevel@tonic-gate if (sign_ops->sign_init != NULL)
7080Sstevel@tonic-gate fl->fl_sign_init = B_TRUE;
7090Sstevel@tonic-gate if (sign_ops->sign != NULL)
7100Sstevel@tonic-gate fl->fl_sign = B_TRUE;
7110Sstevel@tonic-gate if (sign_ops->sign_update != NULL)
7120Sstevel@tonic-gate fl->fl_sign_update = B_TRUE;
7130Sstevel@tonic-gate if (sign_ops->sign_final != NULL)
7140Sstevel@tonic-gate fl->fl_sign_final = B_TRUE;
7150Sstevel@tonic-gate if (sign_ops->sign_recover_init != NULL)
7160Sstevel@tonic-gate fl->fl_sign_recover_init = B_TRUE;
7170Sstevel@tonic-gate if (sign_ops->sign_recover != NULL)
7180Sstevel@tonic-gate fl->fl_sign_recover = B_TRUE;
7190Sstevel@tonic-gate }
720904Smcpowers if ((verify_ops = ops->co_verify_ops) != NULL) {
7210Sstevel@tonic-gate if (verify_ops->verify_init != NULL)
7220Sstevel@tonic-gate fl->fl_verify_init = B_TRUE;
7230Sstevel@tonic-gate if (verify_ops->verify != NULL)
7240Sstevel@tonic-gate fl->fl_verify = B_TRUE;
7250Sstevel@tonic-gate if (verify_ops->verify_update != NULL)
7260Sstevel@tonic-gate fl->fl_verify_update = B_TRUE;
7270Sstevel@tonic-gate if (verify_ops->verify_final != NULL)
7280Sstevel@tonic-gate fl->fl_verify_final = B_TRUE;
7290Sstevel@tonic-gate if (verify_ops->verify_recover_init != NULL)
7300Sstevel@tonic-gate fl->fl_verify_recover_init = B_TRUE;
7310Sstevel@tonic-gate if (verify_ops->verify_recover != NULL)
7320Sstevel@tonic-gate fl->fl_verify_recover = B_TRUE;
7330Sstevel@tonic-gate }
734904Smcpowers if ((dual_ops = ops->co_dual_ops) != NULL) {
7350Sstevel@tonic-gate if (dual_ops->digest_encrypt_update != NULL)
7360Sstevel@tonic-gate fl->fl_digest_encrypt_update = B_TRUE;
7370Sstevel@tonic-gate if (dual_ops->decrypt_digest_update != NULL)
7380Sstevel@tonic-gate fl->fl_decrypt_digest_update = B_TRUE;
7390Sstevel@tonic-gate if (dual_ops->sign_encrypt_update != NULL)
7400Sstevel@tonic-gate fl->fl_sign_encrypt_update = B_TRUE;
7410Sstevel@tonic-gate if (dual_ops->decrypt_verify_update != NULL)
7420Sstevel@tonic-gate fl->fl_decrypt_verify_update = B_TRUE;
7430Sstevel@tonic-gate }
744904Smcpowers if ((random_number_ops = ops->co_random_ops) != NULL) {
7450Sstevel@tonic-gate if (random_number_ops->seed_random != NULL)
7460Sstevel@tonic-gate fl->fl_seed_random = B_TRUE;
7470Sstevel@tonic-gate if (random_number_ops->generate_random != NULL)
7480Sstevel@tonic-gate fl->fl_generate_random = B_TRUE;
7490Sstevel@tonic-gate }
750904Smcpowers if ((session_ops = ops->co_session_ops) != NULL) {
7510Sstevel@tonic-gate if (session_ops->session_open != NULL)
7520Sstevel@tonic-gate fl->fl_session_open = B_TRUE;
7530Sstevel@tonic-gate if (session_ops->session_close != NULL)
7540Sstevel@tonic-gate fl->fl_session_close = B_TRUE;
7550Sstevel@tonic-gate if (session_ops->session_login != NULL)
7560Sstevel@tonic-gate fl->fl_session_login = B_TRUE;
7570Sstevel@tonic-gate if (session_ops->session_logout != NULL)
7580Sstevel@tonic-gate fl->fl_session_logout = B_TRUE;
7590Sstevel@tonic-gate }
760904Smcpowers if ((object_ops = ops->co_object_ops) != NULL) {
7610Sstevel@tonic-gate if (object_ops->object_create != NULL)
7620Sstevel@tonic-gate fl->fl_object_create = B_TRUE;
7630Sstevel@tonic-gate if (object_ops->object_copy != NULL)
7640Sstevel@tonic-gate fl->fl_object_copy = B_TRUE;
7650Sstevel@tonic-gate if (object_ops->object_destroy != NULL)
7660Sstevel@tonic-gate fl->fl_object_destroy = B_TRUE;
7670Sstevel@tonic-gate if (object_ops->object_get_size != NULL)
7680Sstevel@tonic-gate fl->fl_object_get_size = B_TRUE;
7690Sstevel@tonic-gate if (object_ops->object_get_attribute_value != NULL)
7700Sstevel@tonic-gate fl->fl_object_get_attribute_value = B_TRUE;
7710Sstevel@tonic-gate if (object_ops->object_set_attribute_value != NULL)
7720Sstevel@tonic-gate fl->fl_object_set_attribute_value = B_TRUE;
7730Sstevel@tonic-gate if (object_ops->object_find_init != NULL)
7740Sstevel@tonic-gate fl->fl_object_find_init = B_TRUE;
7750Sstevel@tonic-gate if (object_ops->object_find != NULL)
7760Sstevel@tonic-gate fl->fl_object_find = B_TRUE;
7770Sstevel@tonic-gate if (object_ops->object_find_final != NULL)
7780Sstevel@tonic-gate fl->fl_object_find_final = B_TRUE;
7790Sstevel@tonic-gate }
780904Smcpowers if ((key_ops = ops->co_key_ops) != NULL) {
7810Sstevel@tonic-gate if (key_ops->key_generate != NULL)
7820Sstevel@tonic-gate fl->fl_key_generate = B_TRUE;
7830Sstevel@tonic-gate if (key_ops->key_generate_pair != NULL)
7840Sstevel@tonic-gate fl->fl_key_generate_pair = B_TRUE;
7850Sstevel@tonic-gate if (key_ops->key_wrap != NULL)
7860Sstevel@tonic-gate fl->fl_key_wrap = B_TRUE;
7870Sstevel@tonic-gate if (key_ops->key_unwrap != NULL)
7880Sstevel@tonic-gate fl->fl_key_unwrap = B_TRUE;
7890Sstevel@tonic-gate if (key_ops->key_derive != NULL)
7900Sstevel@tonic-gate fl->fl_key_derive = B_TRUE;
7910Sstevel@tonic-gate }
792904Smcpowers if ((provider_ops = ops->co_provider_ops) != NULL) {
7930Sstevel@tonic-gate if (provider_ops->init_token != NULL)
7940Sstevel@tonic-gate fl->fl_init_token = B_TRUE;
7950Sstevel@tonic-gate if (provider_ops->init_pin != NULL)
7960Sstevel@tonic-gate fl->fl_init_pin = B_TRUE;
7970Sstevel@tonic-gate if (provider_ops->set_pin != NULL)
7980Sstevel@tonic-gate fl->fl_set_pin = B_TRUE;
7990Sstevel@tonic-gate }
8004072Skrishna
80111304SJanie.Lu@Sun.COM fl->prov_is_hash_limited = pd->pd_flags & CRYPTO_HASH_NO_UPDATE;
80211304SJanie.Lu@Sun.COM if (fl->prov_is_hash_limited) {
80311304SJanie.Lu@Sun.COM fl->prov_hash_limit = min(pd->pd_hash_limit,
80411304SJanie.Lu@Sun.COM min(CRYPTO_MAX_BUFFER_LEN,
80511304SJanie.Lu@Sun.COM curproc->p_task->tk_proj->kpj_data.kpd_crypto_mem_ctl));
80611304SJanie.Lu@Sun.COM }
80711304SJanie.Lu@Sun.COM
80811304SJanie.Lu@Sun.COM fl->prov_is_hmac_limited = pd->pd_flags & CRYPTO_HMAC_NO_UPDATE;
80911304SJanie.Lu@Sun.COM if (fl->prov_is_hmac_limited) {
81011304SJanie.Lu@Sun.COM fl->prov_hmac_limit = min(pd->pd_hmac_limit,
81111304SJanie.Lu@Sun.COM min(CRYPTO_MAX_BUFFER_LEN,
81211304SJanie.Lu@Sun.COM curproc->p_task->tk_proj->kpj_data.kpd_crypto_mem_ctl));
81311304SJanie.Lu@Sun.COM }
81411304SJanie.Lu@Sun.COM
81511304SJanie.Lu@Sun.COM if (fl->prov_is_hash_limited || fl->prov_is_hmac_limited) {
8164072Skrishna /*
81711304SJanie.Lu@Sun.COM * XXX - The threshold should ideally be per hash/HMAC
8184072Skrishna * mechanism. For now, we use the same value for all
81911304SJanie.Lu@Sun.COM * hash/HMAC mechanisms. Empirical evidence suggests this
8204072Skrishna * is fine.
8214072Skrishna */
8224072Skrishna fl->prov_hash_threshold = kcf_md5_threshold;
8234072Skrishna }
8244841Shaimay
8254841Shaimay fl->total_threshold_count = MAX_NUM_THRESHOLD;
8264841Shaimay fl->fl_threshold[0].mech_type = CKM_DES3_CBC;
8274841Shaimay fl->fl_threshold[0].mech_threshold = kcf_des3_threshold;
8284841Shaimay fl->fl_threshold[1].mech_type = CKM_DES3_ECB;
8294841Shaimay fl->fl_threshold[1].mech_threshold = kcf_des3_threshold;
8304841Shaimay fl->fl_threshold[2].mech_type = CKM_AES_CBC;
8314841Shaimay fl->fl_threshold[2].mech_threshold = kcf_aes_threshold;
8324841Shaimay fl->fl_threshold[3].mech_type = CKM_AES_ECB;
8334841Shaimay fl->fl_threshold[3].mech_threshold = kcf_aes_threshold;
8344841Shaimay fl->fl_threshold[4].mech_type = CKM_RC4;
8354841Shaimay fl->fl_threshold[4].mech_threshold = kcf_rc4_threshold;
8364841Shaimay fl->fl_threshold[5].mech_type = CKM_MD5;
8374841Shaimay fl->fl_threshold[5].mech_threshold = kcf_md5_threshold;
8384841Shaimay fl->fl_threshold[6].mech_type = CKM_SHA_1;
8394841Shaimay fl->fl_threshold[6].mech_threshold = kcf_sha1_threshold;
8400Sstevel@tonic-gate }
8410Sstevel@tonic-gate
8420Sstevel@tonic-gate /* ARGSUSED */
8430Sstevel@tonic-gate static int
get_function_list(dev_t dev,caddr_t arg,int mode,int * rval)8440Sstevel@tonic-gate get_function_list(dev_t dev, caddr_t arg, int mode, int *rval)
8450Sstevel@tonic-gate {
8460Sstevel@tonic-gate crypto_get_function_list_t get_function_list;
8470Sstevel@tonic-gate crypto_minor_t *cm;
8480Sstevel@tonic-gate crypto_provider_id_t provider_id;
8490Sstevel@tonic-gate crypto_function_list_t *fl;
8500Sstevel@tonic-gate kcf_provider_desc_t *provider;
8510Sstevel@tonic-gate int rv;
8520Sstevel@tonic-gate
8530Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
8540Sstevel@tonic-gate cmn_err(CE_WARN, "get_function_list: failed holding minor");
8550Sstevel@tonic-gate return (ENXIO);
8560Sstevel@tonic-gate }
8570Sstevel@tonic-gate
8580Sstevel@tonic-gate if (copyin(arg, &get_function_list, sizeof (get_function_list)) != 0) {
8590Sstevel@tonic-gate crypto_release_minor(cm);
8600Sstevel@tonic-gate return (EFAULT);
8610Sstevel@tonic-gate }
8620Sstevel@tonic-gate
8630Sstevel@tonic-gate /* initialize provider_array */
8640Sstevel@tonic-gate if (cm->cm_provider_array == NULL) {
8650Sstevel@tonic-gate rv = crypto_get_provider_list(cm, NULL, NULL, DONT_RETURN_LIST);
8660Sstevel@tonic-gate if (rv != CRYPTO_SUCCESS) {
8670Sstevel@tonic-gate goto release_minor;
8680Sstevel@tonic-gate }
8690Sstevel@tonic-gate }
8700Sstevel@tonic-gate
8710Sstevel@tonic-gate provider_id = get_function_list.fl_provider_id;
8720Sstevel@tonic-gate mutex_enter(&cm->cm_lock);
8730Sstevel@tonic-gate /* index must be less than count of providers */
8740Sstevel@tonic-gate if (provider_id >= cm->cm_provider_count) {
8750Sstevel@tonic-gate mutex_exit(&cm->cm_lock);
8760Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD;
8770Sstevel@tonic-gate goto release_minor;
8780Sstevel@tonic-gate }
8790Sstevel@tonic-gate
8800Sstevel@tonic-gate ASSERT(cm->cm_provider_array != NULL);
8810Sstevel@tonic-gate provider = cm->cm_provider_array[provider_id];
8820Sstevel@tonic-gate mutex_exit(&cm->cm_lock);
8830Sstevel@tonic-gate
8840Sstevel@tonic-gate fl = &get_function_list.fl_list;
8850Sstevel@tonic-gate bzero(fl, sizeof (crypto_function_list_t));
8860Sstevel@tonic-gate
8870Sstevel@tonic-gate if (provider->pd_prov_type != CRYPTO_LOGICAL_PROVIDER) {
8880Sstevel@tonic-gate crypto_build_function_list(fl, provider);
8890Sstevel@tonic-gate } else {
8900Sstevel@tonic-gate kcf_provider_desc_t *prev = NULL, *pd;
8910Sstevel@tonic-gate
8920Sstevel@tonic-gate mutex_enter(&provider->pd_lock);
8930Sstevel@tonic-gate while (kcf_get_next_logical_provider_member(provider,
8940Sstevel@tonic-gate prev, &pd)) {
8950Sstevel@tonic-gate prev = pd;
8960Sstevel@tonic-gate crypto_build_function_list(fl, pd);
8970Sstevel@tonic-gate KCF_PROV_REFRELE(pd);
8980Sstevel@tonic-gate }
8990Sstevel@tonic-gate mutex_exit(&provider->pd_lock);
9000Sstevel@tonic-gate }
9010Sstevel@tonic-gate
9020Sstevel@tonic-gate rv = CRYPTO_SUCCESS;
9030Sstevel@tonic-gate
9040Sstevel@tonic-gate release_minor:
9050Sstevel@tonic-gate crypto_release_minor(cm);
9060Sstevel@tonic-gate
9070Sstevel@tonic-gate get_function_list.fl_return_value = rv;
9080Sstevel@tonic-gate
9090Sstevel@tonic-gate if (copyout(&get_function_list, arg, sizeof (get_function_list)) != 0) {
9100Sstevel@tonic-gate return (EFAULT);
9110Sstevel@tonic-gate }
9120Sstevel@tonic-gate return (0);
9130Sstevel@tonic-gate }
9140Sstevel@tonic-gate
9150Sstevel@tonic-gate /*
9160Sstevel@tonic-gate * This ioctl maps a PKCS#11 mechanism string into an internal number
9170Sstevel@tonic-gate * that is used by the kernel. pn_internal_number is set to the
9180Sstevel@tonic-gate * internal number.
9190Sstevel@tonic-gate */
9200Sstevel@tonic-gate /* ARGSUSED */
9210Sstevel@tonic-gate static int
get_mechanism_number(dev_t dev,caddr_t arg,int mode,int * rval)9220Sstevel@tonic-gate get_mechanism_number(dev_t dev, caddr_t arg, int mode, int *rval)
9230Sstevel@tonic-gate {
9240Sstevel@tonic-gate STRUCT_DECL(crypto_get_mechanism_number, get_number);
9250Sstevel@tonic-gate crypto_mech_type_t number;
9260Sstevel@tonic-gate size_t len;
9270Sstevel@tonic-gate char *mechanism_name;
9280Sstevel@tonic-gate int rv;
9290Sstevel@tonic-gate
9300Sstevel@tonic-gate STRUCT_INIT(get_number, mode);
9310Sstevel@tonic-gate
9320Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(get_number), STRUCT_SIZE(get_number)) != 0)
9330Sstevel@tonic-gate return (EFAULT);
9340Sstevel@tonic-gate
9350Sstevel@tonic-gate len = STRUCT_FGET(get_number, pn_mechanism_len);
9360Sstevel@tonic-gate if (len == 0 || len > CRYPTO_MAX_MECH_NAME) {
9370Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD;
9380Sstevel@tonic-gate goto out;
9390Sstevel@tonic-gate }
9400Sstevel@tonic-gate mechanism_name = kmem_alloc(len, KM_SLEEP);
9410Sstevel@tonic-gate
9420Sstevel@tonic-gate if (copyin(STRUCT_FGETP(get_number, pn_mechanism_string),
9430Sstevel@tonic-gate mechanism_name, len) != 0) {
9440Sstevel@tonic-gate kmem_free(mechanism_name, len);
9450Sstevel@tonic-gate return (EFAULT);
9460Sstevel@tonic-gate }
9470Sstevel@tonic-gate
9482935Skrishna /*
9492935Skrishna * Get mechanism number from kcf. We set the load_module
9502935Skrishna * flag to false since we use only hardware providers.
9512935Skrishna */
9522935Skrishna number = crypto_mech2id_common(mechanism_name, B_FALSE);
9530Sstevel@tonic-gate kmem_free(mechanism_name, len);
9540Sstevel@tonic-gate if (number == CRYPTO_MECH_INVALID) {
9550Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD;
9560Sstevel@tonic-gate goto out;
9570Sstevel@tonic-gate }
9580Sstevel@tonic-gate
9590Sstevel@tonic-gate bcopy((char *)&number, (char *)STRUCT_FADDR(get_number,
9600Sstevel@tonic-gate pn_internal_number), sizeof (number));
9610Sstevel@tonic-gate
9620Sstevel@tonic-gate rv = CRYPTO_SUCCESS;
9630Sstevel@tonic-gate out:
9640Sstevel@tonic-gate STRUCT_FSET(get_number, pn_return_value, rv);
9650Sstevel@tonic-gate
9660Sstevel@tonic-gate if (copyout(STRUCT_BUF(get_number), arg,
9670Sstevel@tonic-gate STRUCT_SIZE(get_number)) != 0) {
9680Sstevel@tonic-gate return (EFAULT);
9690Sstevel@tonic-gate }
9700Sstevel@tonic-gate return (0);
9710Sstevel@tonic-gate }
9720Sstevel@tonic-gate
9730Sstevel@tonic-gate /*
9748313SDina.Nimeh@Sun.Com * This ioctl returns an array of crypto_mech_name_t entries.
9758313SDina.Nimeh@Sun.Com * It lists all the PKCS#11 mechanisms available in the kernel.
9768313SDina.Nimeh@Sun.Com */
9778313SDina.Nimeh@Sun.Com /* ARGSUSED */
9788313SDina.Nimeh@Sun.Com static int
get_mechanism_list(dev_t dev,caddr_t arg,int mode,int * rval)9798313SDina.Nimeh@Sun.Com get_mechanism_list(dev_t dev, caddr_t arg, int mode, int *rval)
9808313SDina.Nimeh@Sun.Com {
9818313SDina.Nimeh@Sun.Com STRUCT_DECL(crypto_get_mechanism_list, get_list);
9828313SDina.Nimeh@Sun.Com crypto_mech_name_t *entries;
9838313SDina.Nimeh@Sun.Com size_t copyout_size;
9848313SDina.Nimeh@Sun.Com uint_t req_count;
9858313SDina.Nimeh@Sun.Com uint_t count;
9868313SDina.Nimeh@Sun.Com ulong_t offset;
9878313SDina.Nimeh@Sun.Com int error = 0;
9888313SDina.Nimeh@Sun.Com
9898313SDina.Nimeh@Sun.Com STRUCT_INIT(get_list, mode);
9908313SDina.Nimeh@Sun.Com
9918313SDina.Nimeh@Sun.Com if (copyin(arg, STRUCT_BUF(get_list), STRUCT_SIZE(get_list)) != 0) {
9928313SDina.Nimeh@Sun.Com return (EFAULT);
9938313SDina.Nimeh@Sun.Com }
9948313SDina.Nimeh@Sun.Com
9958313SDina.Nimeh@Sun.Com entries = crypto_get_mech_list(&count, KM_SLEEP);
9968313SDina.Nimeh@Sun.Com
9978313SDina.Nimeh@Sun.Com /* Number of entries caller thinks we have */
9988313SDina.Nimeh@Sun.Com req_count = STRUCT_FGET(get_list, ml_count);
9998313SDina.Nimeh@Sun.Com
10008313SDina.Nimeh@Sun.Com STRUCT_FSET(get_list, ml_count, count);
10018313SDina.Nimeh@Sun.Com STRUCT_FSET(get_list, ml_return_value, CRYPTO_SUCCESS);
10028313SDina.Nimeh@Sun.Com
10038313SDina.Nimeh@Sun.Com /* check if buffer is too small */
10048313SDina.Nimeh@Sun.Com if (count > req_count) {
10058313SDina.Nimeh@Sun.Com STRUCT_FSET(get_list, ml_return_value, CRYPTO_BUFFER_TOO_SMALL);
10068313SDina.Nimeh@Sun.Com }
10078313SDina.Nimeh@Sun.Com
10088313SDina.Nimeh@Sun.Com /* copyout the first stuff */
10098313SDina.Nimeh@Sun.Com if (copyout(STRUCT_BUF(get_list), arg, STRUCT_SIZE(get_list)) != 0) {
10108313SDina.Nimeh@Sun.Com error = EFAULT;
10118313SDina.Nimeh@Sun.Com }
10128313SDina.Nimeh@Sun.Com
10138313SDina.Nimeh@Sun.Com /*
10148313SDina.Nimeh@Sun.Com * If only requesting number of entries or buffer too small or an
10158313SDina.Nimeh@Sun.Com * error occurred, stop here
10168313SDina.Nimeh@Sun.Com */
10178313SDina.Nimeh@Sun.Com if (req_count == 0 || count > req_count || error != 0) {
10188313SDina.Nimeh@Sun.Com goto out;
10198313SDina.Nimeh@Sun.Com }
10208313SDina.Nimeh@Sun.Com
10218313SDina.Nimeh@Sun.Com copyout_size = count * sizeof (crypto_mech_name_t);
10228313SDina.Nimeh@Sun.Com
10238313SDina.Nimeh@Sun.Com /* copyout entries */
10248313SDina.Nimeh@Sun.Com offset = (ulong_t)STRUCT_FADDR(get_list, ml_list);
10258313SDina.Nimeh@Sun.Com offset -= (ulong_t)STRUCT_BUF(get_list);
10268313SDina.Nimeh@Sun.Com if (copyout(entries, arg + offset, copyout_size) != 0) {
10278313SDina.Nimeh@Sun.Com error = EFAULT;
10288313SDina.Nimeh@Sun.Com }
10298313SDina.Nimeh@Sun.Com
10308313SDina.Nimeh@Sun.Com out:
10318313SDina.Nimeh@Sun.Com crypto_free_mech_list(entries, count);
10328313SDina.Nimeh@Sun.Com return (error);
10338313SDina.Nimeh@Sun.Com }
10348313SDina.Nimeh@Sun.Com
10358313SDina.Nimeh@Sun.Com /*
10368313SDina.Nimeh@Sun.Com * Copyout kernel array of mech_infos to user space.
10378313SDina.Nimeh@Sun.Com */
10388313SDina.Nimeh@Sun.Com /* ARGSUSED */
10398313SDina.Nimeh@Sun.Com static int
copyout_mechinfos(int mode,caddr_t out,uint_t count,crypto_mechanism_info_t * k_minfos,caddr_t u_minfos)10408313SDina.Nimeh@Sun.Com copyout_mechinfos(int mode, caddr_t out, uint_t count,
10418313SDina.Nimeh@Sun.Com crypto_mechanism_info_t *k_minfos, caddr_t u_minfos)
10428313SDina.Nimeh@Sun.Com {
10438313SDina.Nimeh@Sun.Com STRUCT_DECL(crypto_mechanism_info, mi);
10448313SDina.Nimeh@Sun.Com caddr_t p;
10458313SDina.Nimeh@Sun.Com size_t len;
10468313SDina.Nimeh@Sun.Com int i;
10478313SDina.Nimeh@Sun.Com
10488313SDina.Nimeh@Sun.Com if (count == 0)
10498313SDina.Nimeh@Sun.Com return (0);
10508313SDina.Nimeh@Sun.Com
10518313SDina.Nimeh@Sun.Com STRUCT_INIT(mi, mode);
10528313SDina.Nimeh@Sun.Com
10538313SDina.Nimeh@Sun.Com len = count * STRUCT_SIZE(mi);
10548313SDina.Nimeh@Sun.Com
10558313SDina.Nimeh@Sun.Com ASSERT(u_minfos != NULL);
10568313SDina.Nimeh@Sun.Com p = u_minfos;
10578313SDina.Nimeh@Sun.Com for (i = 0; i < count; i++) {
10588313SDina.Nimeh@Sun.Com STRUCT_FSET(mi, mi_min_key_size, k_minfos[i].mi_min_key_size);
10598313SDina.Nimeh@Sun.Com STRUCT_FSET(mi, mi_max_key_size, k_minfos[i].mi_max_key_size);
10608313SDina.Nimeh@Sun.Com STRUCT_FSET(mi, mi_keysize_unit, k_minfos[i].mi_keysize_unit);
10618313SDina.Nimeh@Sun.Com STRUCT_FSET(mi, mi_usage, k_minfos[i].mi_usage);
10628313SDina.Nimeh@Sun.Com bcopy(STRUCT_BUF(mi), p, STRUCT_SIZE(mi));
10638313SDina.Nimeh@Sun.Com p += STRUCT_SIZE(mi);
10648313SDina.Nimeh@Sun.Com }
10658313SDina.Nimeh@Sun.Com
10668313SDina.Nimeh@Sun.Com if (copyout(u_minfos, out, len) != 0)
10678313SDina.Nimeh@Sun.Com return (EFAULT);
10688313SDina.Nimeh@Sun.Com
10698313SDina.Nimeh@Sun.Com return (0);
10708313SDina.Nimeh@Sun.Com }
10718313SDina.Nimeh@Sun.Com
10728313SDina.Nimeh@Sun.Com /*
10738313SDina.Nimeh@Sun.Com * This ioctl returns information for the specified mechanism.
10748313SDina.Nimeh@Sun.Com */
10758313SDina.Nimeh@Sun.Com /* ARGSUSED */
10768313SDina.Nimeh@Sun.Com static int
get_all_mechanism_info(dev_t dev,caddr_t arg,int mode,int * rval)10778313SDina.Nimeh@Sun.Com get_all_mechanism_info(dev_t dev, caddr_t arg, int mode, int *rval)
10788313SDina.Nimeh@Sun.Com {
10798313SDina.Nimeh@Sun.Com STRUCT_DECL(crypto_get_all_mechanism_info, get_all_mech);
108011413Sopensolaris@drydog.com #ifdef _LP64
108111413Sopensolaris@drydog.com STRUCT_DECL(crypto_mechanism_info, mi);
108211413Sopensolaris@drydog.com #else
10838319SDina.Nimeh@Sun.Com /* LINTED E_FUNC_SET_NOT_USED */
10848313SDina.Nimeh@Sun.Com STRUCT_DECL(crypto_mechanism_info, mi);
108511413Sopensolaris@drydog.com #endif
10868313SDina.Nimeh@Sun.Com crypto_mech_name_t mech_name;
10878313SDina.Nimeh@Sun.Com crypto_mech_type_t mech_type;
10888313SDina.Nimeh@Sun.Com crypto_mechanism_info_t *mech_infos = NULL;
10898313SDina.Nimeh@Sun.Com uint_t num_mech_infos = 0;
10908313SDina.Nimeh@Sun.Com uint_t req_count;
10918313SDina.Nimeh@Sun.Com caddr_t u_minfos;
10928313SDina.Nimeh@Sun.Com ulong_t offset;
10938313SDina.Nimeh@Sun.Com int error = 0;
10948313SDina.Nimeh@Sun.Com int rv;
10958313SDina.Nimeh@Sun.Com
10968313SDina.Nimeh@Sun.Com STRUCT_INIT(get_all_mech, mode);
10978313SDina.Nimeh@Sun.Com STRUCT_INIT(mi, mode);
10988313SDina.Nimeh@Sun.Com
10998313SDina.Nimeh@Sun.Com if (copyin(arg, STRUCT_BUF(get_all_mech),
11008313SDina.Nimeh@Sun.Com STRUCT_SIZE(get_all_mech)) != 0) {
11018313SDina.Nimeh@Sun.Com return (EFAULT);
11028313SDina.Nimeh@Sun.Com }
11038313SDina.Nimeh@Sun.Com
11048313SDina.Nimeh@Sun.Com (void) strncpy(mech_name, STRUCT_FGET(get_all_mech, mi_mechanism_name),
11058313SDina.Nimeh@Sun.Com CRYPTO_MAX_MECH_NAME);
11068313SDina.Nimeh@Sun.Com mech_type = crypto_mech2id(mech_name);
11078313SDina.Nimeh@Sun.Com
11088313SDina.Nimeh@Sun.Com if (mech_type == CRYPTO_MECH_INVALID) {
11098313SDina.Nimeh@Sun.Com rv = CRYPTO_ARGUMENTS_BAD;
11108313SDina.Nimeh@Sun.Com goto out1;
11118313SDina.Nimeh@Sun.Com }
11128313SDina.Nimeh@Sun.Com
11138313SDina.Nimeh@Sun.Com rv = crypto_get_all_mech_info(mech_type, &mech_infos, &num_mech_infos,
11148313SDina.Nimeh@Sun.Com KM_SLEEP);
11158313SDina.Nimeh@Sun.Com if (rv != CRYPTO_SUCCESS) {
11168313SDina.Nimeh@Sun.Com goto out1;
11178313SDina.Nimeh@Sun.Com }
11188313SDina.Nimeh@Sun.Com /* rv is CRYPTO_SUCCESS at this point */
11198313SDina.Nimeh@Sun.Com
11208313SDina.Nimeh@Sun.Com /* Number of entries caller thinks we have */
11218313SDina.Nimeh@Sun.Com req_count = STRUCT_FGET(get_all_mech, mi_count);
11228313SDina.Nimeh@Sun.Com
11238313SDina.Nimeh@Sun.Com STRUCT_FSET(get_all_mech, mi_count, num_mech_infos);
11248313SDina.Nimeh@Sun.Com
11258313SDina.Nimeh@Sun.Com /* check if buffer is too small */
11268313SDina.Nimeh@Sun.Com if (num_mech_infos > req_count) {
11278313SDina.Nimeh@Sun.Com rv = CRYPTO_BUFFER_TOO_SMALL;
11288313SDina.Nimeh@Sun.Com }
11298313SDina.Nimeh@Sun.Com
11308313SDina.Nimeh@Sun.Com out1:
11318313SDina.Nimeh@Sun.Com STRUCT_FSET(get_all_mech, mi_return_value, rv);
11328313SDina.Nimeh@Sun.Com
11338313SDina.Nimeh@Sun.Com /* copy the first part */
11348313SDina.Nimeh@Sun.Com if (copyout(STRUCT_BUF(get_all_mech), arg,
11358313SDina.Nimeh@Sun.Com STRUCT_SIZE(get_all_mech)) != 0) {
11368313SDina.Nimeh@Sun.Com error = EFAULT;
11378313SDina.Nimeh@Sun.Com }
11388313SDina.Nimeh@Sun.Com
11398313SDina.Nimeh@Sun.Com /*
11408313SDina.Nimeh@Sun.Com * If only requesting number of entries, or there are no entries,
11418313SDina.Nimeh@Sun.Com * or rv is not CRYPTO_SUCCESS due to buffer too small or some other
11428313SDina.Nimeh@Sun.Com * crypto error, or an error occurred with copyout, stop here
11438313SDina.Nimeh@Sun.Com */
11448313SDina.Nimeh@Sun.Com if (req_count == 0 || num_mech_infos == 0 || rv != CRYPTO_SUCCESS ||
11458313SDina.Nimeh@Sun.Com error != 0) {
11468313SDina.Nimeh@Sun.Com goto out2;
11478313SDina.Nimeh@Sun.Com }
11488313SDina.Nimeh@Sun.Com
11498313SDina.Nimeh@Sun.Com /* copyout mech_infos */
11508313SDina.Nimeh@Sun.Com offset = (ulong_t)STRUCT_FADDR(get_all_mech, mi_list);
11518313SDina.Nimeh@Sun.Com offset -= (ulong_t)STRUCT_BUF(get_all_mech);
11528313SDina.Nimeh@Sun.Com
11538313SDina.Nimeh@Sun.Com u_minfos = kmem_alloc(num_mech_infos * STRUCT_SIZE(mi), KM_SLEEP);
11548313SDina.Nimeh@Sun.Com error = copyout_mechinfos(mode, arg + offset, num_mech_infos,
11558313SDina.Nimeh@Sun.Com mech_infos, u_minfos);
11568313SDina.Nimeh@Sun.Com kmem_free(u_minfos, num_mech_infos * STRUCT_SIZE(mi));
11578313SDina.Nimeh@Sun.Com out2:
11588313SDina.Nimeh@Sun.Com if (mech_infos != NULL)
11598313SDina.Nimeh@Sun.Com crypto_free_all_mech_info(mech_infos, num_mech_infos);
11608313SDina.Nimeh@Sun.Com return (error);
11618313SDina.Nimeh@Sun.Com }
11628313SDina.Nimeh@Sun.Com
11638313SDina.Nimeh@Sun.Com /*
11640Sstevel@tonic-gate * Side-effects:
11650Sstevel@tonic-gate * 1. This routine stores provider descriptor pointers in an array
11660Sstevel@tonic-gate * and increments each descriptor's reference count. The array
11670Sstevel@tonic-gate * is stored in per-minor number storage.
11680Sstevel@tonic-gate * 2. Destroys the old array and creates a new one every time
11690Sstevel@tonic-gate * this routine is called.
11700Sstevel@tonic-gate */
11710Sstevel@tonic-gate int
crypto_get_provider_list(crypto_minor_t * cm,uint_t * count,crypto_provider_entry_t ** array,boolean_t return_slot_list)11720Sstevel@tonic-gate crypto_get_provider_list(crypto_minor_t *cm, uint_t *count,
11730Sstevel@tonic-gate crypto_provider_entry_t **array, boolean_t return_slot_list)
11740Sstevel@tonic-gate {
11750Sstevel@tonic-gate kcf_provider_desc_t **provider_array;
11760Sstevel@tonic-gate crypto_provider_entry_t *p = NULL;
11770Sstevel@tonic-gate uint_t provider_count;
11780Sstevel@tonic-gate int rval;
11790Sstevel@tonic-gate int i;
11800Sstevel@tonic-gate
11810Sstevel@tonic-gate /*
11820Sstevel@tonic-gate * Take snapshot of provider table returning only HW entries
11830Sstevel@tonic-gate * that are in a usable state. Also returns logical provider entries.
11840Sstevel@tonic-gate */
11850Sstevel@tonic-gate rval = kcf_get_slot_list(&provider_count, &provider_array, B_FALSE);
11860Sstevel@tonic-gate if (rval != CRYPTO_SUCCESS)
11870Sstevel@tonic-gate return (rval);
11880Sstevel@tonic-gate
11890Sstevel@tonic-gate /* allocate memory before taking cm->cm_lock */
11900Sstevel@tonic-gate if (return_slot_list) {
11910Sstevel@tonic-gate if (provider_count != 0) {
11920Sstevel@tonic-gate p = kmem_alloc(provider_count *
11930Sstevel@tonic-gate sizeof (crypto_provider_entry_t), KM_SLEEP);
11940Sstevel@tonic-gate for (i = 0; i < provider_count; i++) {
11950Sstevel@tonic-gate p[i].pe_provider_id = i;
11960Sstevel@tonic-gate p[i].pe_mechanism_count =
11970Sstevel@tonic-gate provider_array[i]->pd_mech_list_count;
11980Sstevel@tonic-gate }
11990Sstevel@tonic-gate }
12000Sstevel@tonic-gate *array = p;
12010Sstevel@tonic-gate *count = provider_count;
12020Sstevel@tonic-gate }
12030Sstevel@tonic-gate
12040Sstevel@tonic-gate /*
12050Sstevel@tonic-gate * Free existing array of providers and replace with new list.
12060Sstevel@tonic-gate */
12070Sstevel@tonic-gate mutex_enter(&cm->cm_lock);
12080Sstevel@tonic-gate if (cm->cm_provider_array != NULL) {
12090Sstevel@tonic-gate ASSERT(cm->cm_provider_count > 0);
12100Sstevel@tonic-gate kcf_free_provider_tab(cm->cm_provider_count,
12110Sstevel@tonic-gate cm->cm_provider_array);
12120Sstevel@tonic-gate }
12130Sstevel@tonic-gate
12140Sstevel@tonic-gate cm->cm_provider_array = provider_array;
12150Sstevel@tonic-gate cm->cm_provider_count = provider_count;
12160Sstevel@tonic-gate mutex_exit(&cm->cm_lock);
12170Sstevel@tonic-gate
12180Sstevel@tonic-gate return (CRYPTO_SUCCESS);
12190Sstevel@tonic-gate }
12200Sstevel@tonic-gate
12210Sstevel@tonic-gate /*
12220Sstevel@tonic-gate * This ioctl returns an array of crypto_provider_entry_t entries.
12230Sstevel@tonic-gate * This is how consumers learn which hardware providers are available.
12240Sstevel@tonic-gate */
12250Sstevel@tonic-gate /* ARGSUSED */
12260Sstevel@tonic-gate static int
get_provider_list(dev_t dev,caddr_t arg,int mode,int * rval)12270Sstevel@tonic-gate get_provider_list(dev_t dev, caddr_t arg, int mode, int *rval)
12280Sstevel@tonic-gate {
12290Sstevel@tonic-gate STRUCT_DECL(crypto_get_provider_list, get_list);
12300Sstevel@tonic-gate crypto_provider_entry_t *entries;
12310Sstevel@tonic-gate crypto_minor_t *cm;
12320Sstevel@tonic-gate size_t copyout_size;
12330Sstevel@tonic-gate uint_t req_count;
12340Sstevel@tonic-gate uint_t count;
12350Sstevel@tonic-gate ulong_t offset;
12360Sstevel@tonic-gate int rv;
12370Sstevel@tonic-gate
12380Sstevel@tonic-gate STRUCT_INIT(get_list, mode);
12390Sstevel@tonic-gate
12400Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
12410Sstevel@tonic-gate cmn_err(CE_WARN, "get_provider_list: failed holding minor");
12420Sstevel@tonic-gate return (ENXIO);
12430Sstevel@tonic-gate }
12440Sstevel@tonic-gate
12450Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(get_list), STRUCT_SIZE(get_list)) != 0) {
12460Sstevel@tonic-gate crypto_release_minor(cm);
12470Sstevel@tonic-gate return (EFAULT);
12480Sstevel@tonic-gate }
12490Sstevel@tonic-gate
12500Sstevel@tonic-gate rv = crypto_get_provider_list(cm, &count, &entries, RETURN_LIST);
12510Sstevel@tonic-gate if (rv != CRYPTO_SUCCESS) {
12520Sstevel@tonic-gate crypto_release_minor(cm);
12530Sstevel@tonic-gate STRUCT_FSET(get_list, pl_return_value, rv);
12540Sstevel@tonic-gate if (copyout(STRUCT_BUF(get_list), arg,
12550Sstevel@tonic-gate STRUCT_SIZE(get_list)) != 0) {
12560Sstevel@tonic-gate return (EFAULT);
12570Sstevel@tonic-gate }
12580Sstevel@tonic-gate return (0);
12590Sstevel@tonic-gate }
12600Sstevel@tonic-gate crypto_release_minor(cm);
12610Sstevel@tonic-gate
12620Sstevel@tonic-gate /* Number of slots caller thinks we have */
12630Sstevel@tonic-gate req_count = STRUCT_FGET(get_list, pl_count);
12640Sstevel@tonic-gate
12650Sstevel@tonic-gate /* Check if only requesting number of slots */
12660Sstevel@tonic-gate if (req_count == 0) {
12670Sstevel@tonic-gate
12680Sstevel@tonic-gate STRUCT_FSET(get_list, pl_count, count);
12690Sstevel@tonic-gate STRUCT_FSET(get_list, pl_return_value, CRYPTO_SUCCESS);
12700Sstevel@tonic-gate
12710Sstevel@tonic-gate crypto_free_provider_list(entries, count);
12720Sstevel@tonic-gate if (copyout(STRUCT_BUF(get_list), arg,
12734632Smcpowers STRUCT_SIZE(get_list)) != 0) {
12740Sstevel@tonic-gate return (EFAULT);
12750Sstevel@tonic-gate }
12760Sstevel@tonic-gate return (0);
12770Sstevel@tonic-gate }
12780Sstevel@tonic-gate
12790Sstevel@tonic-gate /* check if buffer is too small */
12800Sstevel@tonic-gate req_count = STRUCT_FGET(get_list, pl_count);
12810Sstevel@tonic-gate if (count > req_count) {
12820Sstevel@tonic-gate STRUCT_FSET(get_list, pl_count, count);
12830Sstevel@tonic-gate STRUCT_FSET(get_list, pl_return_value, CRYPTO_BUFFER_TOO_SMALL);
12840Sstevel@tonic-gate crypto_free_provider_list(entries, count);
12850Sstevel@tonic-gate if (copyout(STRUCT_BUF(get_list), arg,
12860Sstevel@tonic-gate STRUCT_SIZE(get_list)) != 0) {
12870Sstevel@tonic-gate return (EFAULT);
12880Sstevel@tonic-gate }
12890Sstevel@tonic-gate return (0);
12900Sstevel@tonic-gate }
12910Sstevel@tonic-gate
12920Sstevel@tonic-gate STRUCT_FSET(get_list, pl_count, count);
12930Sstevel@tonic-gate STRUCT_FSET(get_list, pl_return_value, CRYPTO_SUCCESS);
12940Sstevel@tonic-gate
12950Sstevel@tonic-gate copyout_size = count * sizeof (crypto_provider_entry_t);
12960Sstevel@tonic-gate
12970Sstevel@tonic-gate /* copyout the first stuff */
12980Sstevel@tonic-gate if (copyout(STRUCT_BUF(get_list), arg, STRUCT_SIZE(get_list)) != 0) {
12990Sstevel@tonic-gate crypto_free_provider_list(entries, count);
13000Sstevel@tonic-gate return (EFAULT);
13010Sstevel@tonic-gate }
13020Sstevel@tonic-gate
13030Sstevel@tonic-gate if (count == 0) {
13040Sstevel@tonic-gate crypto_free_provider_list(entries, count);
13050Sstevel@tonic-gate return (0);
13060Sstevel@tonic-gate }
13070Sstevel@tonic-gate
13080Sstevel@tonic-gate /* copyout entries */
13090Sstevel@tonic-gate offset = (ulong_t)STRUCT_FADDR(get_list, pl_list);
13100Sstevel@tonic-gate offset -= (ulong_t)STRUCT_BUF(get_list);
13110Sstevel@tonic-gate if (copyout(entries, arg + offset, copyout_size) != 0) {
13120Sstevel@tonic-gate crypto_free_provider_list(entries, count);
13130Sstevel@tonic-gate return (EFAULT);
13140Sstevel@tonic-gate }
13150Sstevel@tonic-gate
13160Sstevel@tonic-gate crypto_free_provider_list(entries, count);
13170Sstevel@tonic-gate return (0);
13180Sstevel@tonic-gate }
13190Sstevel@tonic-gate
13200Sstevel@tonic-gate static void
ext_to_provider_data(int mode,kcf_provider_desc_t * provider,crypto_provider_ext_info_t * ei,void * out)13210Sstevel@tonic-gate ext_to_provider_data(int mode, kcf_provider_desc_t *provider,
13220Sstevel@tonic-gate crypto_provider_ext_info_t *ei, void *out)
13230Sstevel@tonic-gate {
13240Sstevel@tonic-gate STRUCT_DECL(crypto_provider_data, pd);
13250Sstevel@tonic-gate STRUCT_DECL(crypto_version, version);
13260Sstevel@tonic-gate
13270Sstevel@tonic-gate STRUCT_INIT(pd, mode);
13280Sstevel@tonic-gate STRUCT_INIT(version, mode);
13290Sstevel@tonic-gate
13300Sstevel@tonic-gate bcopy(provider->pd_description, STRUCT_FGET(pd, pd_prov_desc),
13310Sstevel@tonic-gate CRYPTO_PROVIDER_DESCR_MAX_LEN);
13320Sstevel@tonic-gate
13330Sstevel@tonic-gate bcopy(ei->ei_label, STRUCT_FGET(pd, pd_label), CRYPTO_EXT_SIZE_LABEL);
13340Sstevel@tonic-gate bcopy(ei->ei_manufacturerID, STRUCT_FGET(pd, pd_manufacturerID),
13350Sstevel@tonic-gate CRYPTO_EXT_SIZE_MANUF);
13360Sstevel@tonic-gate bcopy(ei->ei_model, STRUCT_FGET(pd, pd_model), CRYPTO_EXT_SIZE_MODEL);
13370Sstevel@tonic-gate bcopy(ei->ei_serial_number, STRUCT_FGET(pd, pd_serial_number),
13380Sstevel@tonic-gate CRYPTO_EXT_SIZE_SERIAL);
13390Sstevel@tonic-gate /*
13400Sstevel@tonic-gate * We do not support ioctls for dual-function crypto operations yet.
13410Sstevel@tonic-gate * So, we clear this flag as it might have been set by a provider.
13420Sstevel@tonic-gate */
13430Sstevel@tonic-gate ei->ei_flags &= ~CRYPTO_EXTF_DUAL_CRYPTO_OPERATIONS;
13440Sstevel@tonic-gate
13450Sstevel@tonic-gate STRUCT_FSET(pd, pd_flags, ei->ei_flags);
13460Sstevel@tonic-gate STRUCT_FSET(pd, pd_max_session_count, ei->ei_max_session_count);
13470Sstevel@tonic-gate STRUCT_FSET(pd, pd_session_count, (int)CRYPTO_UNAVAILABLE_INFO);
13480Sstevel@tonic-gate STRUCT_FSET(pd, pd_max_rw_session_count, ei->ei_max_session_count);
13490Sstevel@tonic-gate STRUCT_FSET(pd, pd_rw_session_count, (int)CRYPTO_UNAVAILABLE_INFO);
13500Sstevel@tonic-gate STRUCT_FSET(pd, pd_max_pin_len, ei->ei_max_pin_len);
13510Sstevel@tonic-gate STRUCT_FSET(pd, pd_min_pin_len, ei->ei_min_pin_len);
13520Sstevel@tonic-gate STRUCT_FSET(pd, pd_total_public_memory, ei->ei_total_public_memory);
13530Sstevel@tonic-gate STRUCT_FSET(pd, pd_free_public_memory, ei->ei_free_public_memory);
13540Sstevel@tonic-gate STRUCT_FSET(pd, pd_total_private_memory, ei->ei_total_private_memory);
13550Sstevel@tonic-gate STRUCT_FSET(pd, pd_free_private_memory, ei->ei_free_private_memory);
13560Sstevel@tonic-gate STRUCT_FSET(version, cv_major, ei->ei_hardware_version.cv_major);
13570Sstevel@tonic-gate STRUCT_FSET(version, cv_minor, ei->ei_hardware_version.cv_minor);
13580Sstevel@tonic-gate bcopy(STRUCT_BUF(version), STRUCT_FADDR(pd, pd_hardware_version),
13590Sstevel@tonic-gate STRUCT_SIZE(version));
136010028Swyllys.ingersoll@sun.com STRUCT_FSET(version, cv_major, ei->ei_firmware_version.cv_major);
136110028Swyllys.ingersoll@sun.com STRUCT_FSET(version, cv_minor, ei->ei_firmware_version.cv_minor);
13620Sstevel@tonic-gate bcopy(STRUCT_BUF(version), STRUCT_FADDR(pd, pd_firmware_version),
13630Sstevel@tonic-gate STRUCT_SIZE(version));
13640Sstevel@tonic-gate bcopy(ei->ei_time, STRUCT_FGET(pd, pd_time), CRYPTO_EXT_SIZE_TIME);
13650Sstevel@tonic-gate bcopy(STRUCT_BUF(pd), out, STRUCT_SIZE(pd));
13660Sstevel@tonic-gate }
13670Sstevel@tonic-gate
13680Sstevel@tonic-gate /*
13690Sstevel@tonic-gate * Utility routine to construct a crypto_provider_ext_info structure. Some
13700Sstevel@tonic-gate * of the fields are constructed from information in the provider structure.
13710Sstevel@tonic-gate * The rest of the fields have default values. We need to do this for
13720Sstevel@tonic-gate * providers which do not support crypto_provider_management_ops routines.
13730Sstevel@tonic-gate */
13740Sstevel@tonic-gate static void
fabricate_ext_info(kcf_provider_desc_t * provider,crypto_provider_ext_info_t * ei)13750Sstevel@tonic-gate fabricate_ext_info(kcf_provider_desc_t *provider,
13760Sstevel@tonic-gate crypto_provider_ext_info_t *ei)
13770Sstevel@tonic-gate {
13780Sstevel@tonic-gate /* empty label */
13790Sstevel@tonic-gate (void) memset(ei->ei_label, ' ', CRYPTO_EXT_SIZE_LABEL);
13800Sstevel@tonic-gate
13810Sstevel@tonic-gate (void) memset(ei->ei_manufacturerID, ' ', CRYPTO_EXT_SIZE_MANUF);
13820Sstevel@tonic-gate (void) strncpy((char *)ei->ei_manufacturerID, "Unknown", 7);
13830Sstevel@tonic-gate
13840Sstevel@tonic-gate (void) memset(ei->ei_model, ' ', CRYPTO_EXT_SIZE_MODEL);
13850Sstevel@tonic-gate (void) strncpy((char *)ei->ei_model, "Unknown", 7);
13860Sstevel@tonic-gate
13870Sstevel@tonic-gate (void) memset(ei->ei_serial_number, ' ', CRYPTO_EXT_SIZE_SERIAL);
13880Sstevel@tonic-gate (void) strncpy((char *)ei->ei_serial_number, "Unknown", 7);
13890Sstevel@tonic-gate
13900Sstevel@tonic-gate if (KCF_PROV_RANDOM_OPS(provider) != NULL)
13910Sstevel@tonic-gate ei->ei_flags |= CRYPTO_EXTF_RNG;
13920Sstevel@tonic-gate if (KCF_PROV_DUAL_OPS(provider) != NULL)
13930Sstevel@tonic-gate ei->ei_flags |= CRYPTO_EXTF_DUAL_CRYPTO_OPERATIONS;
13940Sstevel@tonic-gate
13950Sstevel@tonic-gate ei->ei_max_session_count = CRYPTO_UNAVAILABLE_INFO;
13960Sstevel@tonic-gate ei->ei_max_pin_len = 0;
13970Sstevel@tonic-gate ei->ei_min_pin_len = 0;
13980Sstevel@tonic-gate ei->ei_total_public_memory = CRYPTO_UNAVAILABLE_INFO;
13990Sstevel@tonic-gate ei->ei_free_public_memory = CRYPTO_UNAVAILABLE_INFO;
14000Sstevel@tonic-gate ei->ei_total_private_memory = CRYPTO_UNAVAILABLE_INFO;
14010Sstevel@tonic-gate ei->ei_free_private_memory = CRYPTO_UNAVAILABLE_INFO;
14020Sstevel@tonic-gate ei->ei_hardware_version.cv_major = 1;
14030Sstevel@tonic-gate ei->ei_hardware_version.cv_minor = 0;
14040Sstevel@tonic-gate ei->ei_firmware_version.cv_major = 1;
14050Sstevel@tonic-gate ei->ei_firmware_version.cv_minor = 0;
14060Sstevel@tonic-gate }
14070Sstevel@tonic-gate
14080Sstevel@tonic-gate /* ARGSUSED */
14090Sstevel@tonic-gate static int
get_provider_info(dev_t dev,caddr_t arg,int mode,int * rval)14100Sstevel@tonic-gate get_provider_info(dev_t dev, caddr_t arg, int mode, int *rval)
14110Sstevel@tonic-gate {
14120Sstevel@tonic-gate STRUCT_DECL(crypto_get_provider_info, get_info);
14130Sstevel@tonic-gate crypto_minor_t *cm;
14140Sstevel@tonic-gate crypto_provider_id_t provider_id;
14150Sstevel@tonic-gate kcf_provider_desc_t *provider, *real_provider;
14160Sstevel@tonic-gate crypto_provider_ext_info_t *ext_info = NULL;
14170Sstevel@tonic-gate size_t need;
14180Sstevel@tonic-gate int error = 0;
14190Sstevel@tonic-gate int rv;
14200Sstevel@tonic-gate kcf_req_params_t params;
14210Sstevel@tonic-gate
14220Sstevel@tonic-gate STRUCT_INIT(get_info, mode);
14230Sstevel@tonic-gate
14240Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
14250Sstevel@tonic-gate cmn_err(CE_WARN, "get_provider_info: failed holding minor");
14260Sstevel@tonic-gate return (ENXIO);
14270Sstevel@tonic-gate }
14280Sstevel@tonic-gate
14290Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(get_info), STRUCT_SIZE(get_info)) != 0) {
14300Sstevel@tonic-gate crypto_release_minor(cm);
14310Sstevel@tonic-gate return (EFAULT);
14320Sstevel@tonic-gate }
14330Sstevel@tonic-gate
14340Sstevel@tonic-gate need = sizeof (crypto_provider_ext_info_t);
14353916Skrishna if ((rv = crypto_buffer_check(need)) != CRYPTO_SUCCESS) {
14360Sstevel@tonic-gate need = 0;
14370Sstevel@tonic-gate goto release_minor;
14380Sstevel@tonic-gate }
14390Sstevel@tonic-gate
14400Sstevel@tonic-gate /* initialize provider_array */
14410Sstevel@tonic-gate if (cm->cm_provider_array == NULL) {
14420Sstevel@tonic-gate rv = crypto_get_provider_list(cm, NULL, NULL, DONT_RETURN_LIST);
14430Sstevel@tonic-gate if (rv != CRYPTO_SUCCESS) {
14440Sstevel@tonic-gate goto release_minor;
14450Sstevel@tonic-gate }
14460Sstevel@tonic-gate }
14470Sstevel@tonic-gate
14480Sstevel@tonic-gate ext_info = kmem_zalloc(need, KM_SLEEP);
14490Sstevel@tonic-gate
14500Sstevel@tonic-gate provider_id = STRUCT_FGET(get_info, gi_provider_id);
14510Sstevel@tonic-gate mutex_enter(&cm->cm_lock);
14520Sstevel@tonic-gate /* index must be less than count of providers */
14530Sstevel@tonic-gate if (provider_id >= cm->cm_provider_count) {
14540Sstevel@tonic-gate mutex_exit(&cm->cm_lock);
14550Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD;
14560Sstevel@tonic-gate goto release_minor;
14570Sstevel@tonic-gate }
14580Sstevel@tonic-gate
14590Sstevel@tonic-gate ASSERT(cm->cm_provider_array != NULL);
14600Sstevel@tonic-gate provider = cm->cm_provider_array[provider_id];
14610Sstevel@tonic-gate KCF_PROV_REFHOLD(provider);
14620Sstevel@tonic-gate mutex_exit(&cm->cm_lock);
14630Sstevel@tonic-gate
14640Sstevel@tonic-gate (void) kcf_get_hardware_provider_nomech(
14650Sstevel@tonic-gate CRYPTO_OPS_OFFSET(provider_ops), CRYPTO_PROVIDER_OFFSET(ext_info),
1466*12304SValerie.Fenwick@Oracle.COM provider, &real_provider);
14670Sstevel@tonic-gate
14680Sstevel@tonic-gate if (real_provider != NULL) {
14690Sstevel@tonic-gate ASSERT(real_provider == provider ||
14700Sstevel@tonic-gate provider->pd_prov_type == CRYPTO_LOGICAL_PROVIDER);
14710Sstevel@tonic-gate KCF_WRAP_PROVMGMT_OPS_PARAMS(¶ms, KCF_OP_MGMT_EXTINFO,
14720Sstevel@tonic-gate 0, NULL, 0, NULL, 0, NULL, ext_info, provider);
14730Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms,
14740Sstevel@tonic-gate B_FALSE);
14750Sstevel@tonic-gate ASSERT(rv != CRYPTO_NOT_SUPPORTED);
1476904Smcpowers KCF_PROV_REFRELE(real_provider);
14770Sstevel@tonic-gate } else {
14780Sstevel@tonic-gate /* do the best we can */
14790Sstevel@tonic-gate fabricate_ext_info(provider, ext_info);
14800Sstevel@tonic-gate rv = CRYPTO_SUCCESS;
14810Sstevel@tonic-gate }
14820Sstevel@tonic-gate KCF_PROV_REFRELE(provider);
14830Sstevel@tonic-gate
14840Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) {
14850Sstevel@tonic-gate ext_to_provider_data(mode, provider, ext_info,
14860Sstevel@tonic-gate STRUCT_FADDR(get_info, gi_provider_data));
14870Sstevel@tonic-gate }
14880Sstevel@tonic-gate
14890Sstevel@tonic-gate release_minor:
14906424Skrishna CRYPTO_DECREMENT_RCTL(need);
14910Sstevel@tonic-gate crypto_release_minor(cm);
14920Sstevel@tonic-gate
14930Sstevel@tonic-gate if (ext_info != NULL)
14940Sstevel@tonic-gate kmem_free(ext_info, sizeof (crypto_provider_ext_info_t));
14950Sstevel@tonic-gate
14960Sstevel@tonic-gate if (error != 0)
14970Sstevel@tonic-gate return (error);
14980Sstevel@tonic-gate
14990Sstevel@tonic-gate STRUCT_FSET(get_info, gi_return_value, rv);
15000Sstevel@tonic-gate if (copyout(STRUCT_BUF(get_info), arg, STRUCT_SIZE(get_info)) != 0) {
15010Sstevel@tonic-gate return (EFAULT);
15020Sstevel@tonic-gate }
15030Sstevel@tonic-gate return (0);
15040Sstevel@tonic-gate }
15050Sstevel@tonic-gate
15060Sstevel@tonic-gate /*
15070Sstevel@tonic-gate * This ioctl returns an array of crypto_mech_name_t entries.
15080Sstevel@tonic-gate * This is how consumers learn which mechanisms are permitted
15090Sstevel@tonic-gate * by a provider.
15100Sstevel@tonic-gate */
15110Sstevel@tonic-gate /* ARGSUSED */
15120Sstevel@tonic-gate static int
get_provider_mechanisms(dev_t dev,caddr_t arg,int mode,int * rval)15130Sstevel@tonic-gate get_provider_mechanisms(dev_t dev, caddr_t arg, int mode, int *rval)
15140Sstevel@tonic-gate {
15150Sstevel@tonic-gate STRUCT_DECL(crypto_get_provider_mechanisms, get_mechanisms);
15160Sstevel@tonic-gate crypto_mech_name_t *entries;
15170Sstevel@tonic-gate crypto_minor_t *cm;
15180Sstevel@tonic-gate size_t copyout_size;
15190Sstevel@tonic-gate uint_t req_count;
15200Sstevel@tonic-gate uint_t count;
15210Sstevel@tonic-gate ulong_t offset;
15220Sstevel@tonic-gate int err;
15230Sstevel@tonic-gate
15240Sstevel@tonic-gate STRUCT_INIT(get_mechanisms, mode);
15250Sstevel@tonic-gate
15260Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
15270Sstevel@tonic-gate cmn_err(CE_WARN,
15280Sstevel@tonic-gate "get_provider_mechanisms: failed holding minor");
15290Sstevel@tonic-gate return (ENXIO);
15300Sstevel@tonic-gate }
15310Sstevel@tonic-gate
15320Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(get_mechanisms),
15330Sstevel@tonic-gate STRUCT_SIZE(get_mechanisms)) != 0) {
15340Sstevel@tonic-gate crypto_release_minor(cm);
15350Sstevel@tonic-gate return (EFAULT);
15360Sstevel@tonic-gate }
15370Sstevel@tonic-gate
15380Sstevel@tonic-gate /* get array of mechanisms from the core module */
15390Sstevel@tonic-gate if ((err = crypto_get_provider_mechanisms(cm,
15400Sstevel@tonic-gate STRUCT_FGET(get_mechanisms, pm_provider_id),
15410Sstevel@tonic-gate &count, &entries)) != 0) {
15420Sstevel@tonic-gate crypto_release_minor(cm);
15430Sstevel@tonic-gate STRUCT_FSET(get_mechanisms, pm_return_value, err);
15440Sstevel@tonic-gate if (copyout(STRUCT_BUF(get_mechanisms), arg,
15450Sstevel@tonic-gate STRUCT_SIZE(get_mechanisms)) != 0) {
15460Sstevel@tonic-gate return (EFAULT);
15470Sstevel@tonic-gate }
15480Sstevel@tonic-gate return (0);
15490Sstevel@tonic-gate }
15500Sstevel@tonic-gate crypto_release_minor(cm);
15510Sstevel@tonic-gate /* Number of mechs caller thinks we have */
15520Sstevel@tonic-gate req_count = STRUCT_FGET(get_mechanisms, pm_count);
15530Sstevel@tonic-gate
15540Sstevel@tonic-gate /* Check if caller is just requesting a count of mechanisms */
15550Sstevel@tonic-gate if (req_count == 0) {
15560Sstevel@tonic-gate STRUCT_FSET(get_mechanisms, pm_count, count);
15570Sstevel@tonic-gate STRUCT_FSET(get_mechanisms, pm_return_value, CRYPTO_SUCCESS);
15580Sstevel@tonic-gate
15590Sstevel@tonic-gate crypto_free_mech_list(entries, count);
15600Sstevel@tonic-gate if (copyout(STRUCT_BUF(get_mechanisms), arg,
15610Sstevel@tonic-gate STRUCT_SIZE(get_mechanisms)) != 0) {
15620Sstevel@tonic-gate return (EFAULT);
15630Sstevel@tonic-gate }
15640Sstevel@tonic-gate return (0);
15650Sstevel@tonic-gate }
15660Sstevel@tonic-gate
15670Sstevel@tonic-gate /* check if buffer is too small */
15680Sstevel@tonic-gate if (count > req_count) {
15690Sstevel@tonic-gate STRUCT_FSET(get_mechanisms, pm_count, count);
15700Sstevel@tonic-gate STRUCT_FSET(get_mechanisms, pm_return_value,
15710Sstevel@tonic-gate CRYPTO_BUFFER_TOO_SMALL);
15720Sstevel@tonic-gate crypto_free_mech_list(entries, count);
15730Sstevel@tonic-gate if (copyout(STRUCT_BUF(get_mechanisms), arg,
15740Sstevel@tonic-gate STRUCT_SIZE(get_mechanisms)) != 0) {
15750Sstevel@tonic-gate return (EFAULT);
15760Sstevel@tonic-gate }
15770Sstevel@tonic-gate return (0);
15780Sstevel@tonic-gate }
15790Sstevel@tonic-gate
15800Sstevel@tonic-gate STRUCT_FSET(get_mechanisms, pm_count, count);
15810Sstevel@tonic-gate STRUCT_FSET(get_mechanisms, pm_return_value, CRYPTO_SUCCESS);
15820Sstevel@tonic-gate
15830Sstevel@tonic-gate copyout_size = count * sizeof (crypto_mech_name_t);
15840Sstevel@tonic-gate
15850Sstevel@tonic-gate /* copyout the first stuff */
15860Sstevel@tonic-gate if (copyout(STRUCT_BUF(get_mechanisms), arg,
15870Sstevel@tonic-gate STRUCT_SIZE(get_mechanisms)) != 0) {
15880Sstevel@tonic-gate crypto_free_mech_list(entries, count);
15890Sstevel@tonic-gate return (EFAULT);
15900Sstevel@tonic-gate }
15910Sstevel@tonic-gate
15920Sstevel@tonic-gate if (count == 0) {
15930Sstevel@tonic-gate return (0);
15940Sstevel@tonic-gate }
15950Sstevel@tonic-gate
15960Sstevel@tonic-gate /* copyout entries */
15970Sstevel@tonic-gate offset = (ulong_t)STRUCT_FADDR(get_mechanisms, pm_list);
15980Sstevel@tonic-gate offset -= (ulong_t)STRUCT_BUF(get_mechanisms);
15990Sstevel@tonic-gate if (copyout(entries, arg + offset, copyout_size) != 0) {
16000Sstevel@tonic-gate crypto_free_mech_list(entries, count);
16010Sstevel@tonic-gate return (EFAULT);
16020Sstevel@tonic-gate }
16030Sstevel@tonic-gate
16040Sstevel@tonic-gate crypto_free_mech_list(entries, count);
16050Sstevel@tonic-gate return (0);
16060Sstevel@tonic-gate }
16070Sstevel@tonic-gate
16080Sstevel@tonic-gate /*
16090Sstevel@tonic-gate * This ioctl returns information about a provider's mechanism.
16100Sstevel@tonic-gate */
16110Sstevel@tonic-gate /* ARGSUSED */
16120Sstevel@tonic-gate static int
get_provider_mechanism_info(dev_t dev,caddr_t arg,int mode,int * rval)16130Sstevel@tonic-gate get_provider_mechanism_info(dev_t dev, caddr_t arg, int mode, int *rval)
16140Sstevel@tonic-gate {
16150Sstevel@tonic-gate crypto_get_provider_mechanism_info_t mechanism_info;
16160Sstevel@tonic-gate crypto_minor_t *cm;
16170Sstevel@tonic-gate kcf_provider_desc_t *pd;
16180Sstevel@tonic-gate crypto_mech_info_t *mi = NULL;
16190Sstevel@tonic-gate int rv = CRYPTO_SUCCESS;
16200Sstevel@tonic-gate int i;
16210Sstevel@tonic-gate
16220Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
16230Sstevel@tonic-gate cmn_err(CE_WARN,
16240Sstevel@tonic-gate "get_provider_mechanism_info: failed holding minor");
16250Sstevel@tonic-gate return (ENXIO);
16260Sstevel@tonic-gate }
16270Sstevel@tonic-gate
16280Sstevel@tonic-gate if (copyin(arg, &mechanism_info, sizeof (mechanism_info)) != 0) {
16290Sstevel@tonic-gate crypto_release_minor(cm);
16300Sstevel@tonic-gate return (EFAULT);
16310Sstevel@tonic-gate }
16320Sstevel@tonic-gate
16330Sstevel@tonic-gate /* initialize provider table */
16340Sstevel@tonic-gate if (cm->cm_provider_array == NULL) {
16350Sstevel@tonic-gate rv = crypto_get_provider_list(cm, NULL, NULL, DONT_RETURN_LIST);
16360Sstevel@tonic-gate if (rv != CRYPTO_SUCCESS) {
16370Sstevel@tonic-gate mutex_enter(&cm->cm_lock);
16380Sstevel@tonic-gate goto fail;
16390Sstevel@tonic-gate }
16400Sstevel@tonic-gate }
16410Sstevel@tonic-gate
16420Sstevel@tonic-gate /*
16430Sstevel@tonic-gate * Provider ID must be less than the count of providers
16440Sstevel@tonic-gate * obtained by calling get_provider_list().
16450Sstevel@tonic-gate */
16460Sstevel@tonic-gate mutex_enter(&cm->cm_lock);
16470Sstevel@tonic-gate if (mechanism_info.mi_provider_id >= cm->cm_provider_count) {
16480Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD;
16490Sstevel@tonic-gate goto fail;
16500Sstevel@tonic-gate }
16510Sstevel@tonic-gate
16520Sstevel@tonic-gate pd = cm->cm_provider_array[mechanism_info.mi_provider_id];
16530Sstevel@tonic-gate
165412223SVladimir.Kotal@Sun.COM /* First check if the provider supports the mechanism. */
16550Sstevel@tonic-gate for (i = 0; i < pd->pd_mech_list_count; i++) {
16560Sstevel@tonic-gate if (strncmp(pd->pd_mechanisms[i].cm_mech_name,
16570Sstevel@tonic-gate mechanism_info.mi_mechanism_name,
16580Sstevel@tonic-gate CRYPTO_MAX_MECH_NAME) == 0) {
16590Sstevel@tonic-gate mi = &pd->pd_mechanisms[i];
166012223SVladimir.Kotal@Sun.COM break;
16610Sstevel@tonic-gate }
16620Sstevel@tonic-gate }
16630Sstevel@tonic-gate
16640Sstevel@tonic-gate if (mi == NULL) {
16650Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD;
16660Sstevel@tonic-gate goto fail;
16670Sstevel@tonic-gate }
16680Sstevel@tonic-gate
166912223SVladimir.Kotal@Sun.COM /* Now check if the mechanism is enabled for the provider. */
167012223SVladimir.Kotal@Sun.COM if (is_mech_disabled(pd, mechanism_info.mi_mechanism_name)) {
167112223SVladimir.Kotal@Sun.COM rv = CRYPTO_MECHANISM_INVALID;
167212223SVladimir.Kotal@Sun.COM goto fail;
167312223SVladimir.Kotal@Sun.COM }
167412223SVladimir.Kotal@Sun.COM
16750Sstevel@tonic-gate mechanism_info.mi_min_key_size = mi->cm_min_key_length;
16760Sstevel@tonic-gate mechanism_info.mi_max_key_size = mi->cm_max_key_length;
16770Sstevel@tonic-gate mechanism_info.mi_flags = mi->cm_func_group_mask;
16780Sstevel@tonic-gate
16790Sstevel@tonic-gate fail:
16800Sstevel@tonic-gate mutex_exit(&cm->cm_lock);
16810Sstevel@tonic-gate crypto_release_minor(cm);
16820Sstevel@tonic-gate mechanism_info.mi_return_value = rv;
16830Sstevel@tonic-gate if (copyout(&mechanism_info, arg, sizeof (mechanism_info)) != 0) {
16840Sstevel@tonic-gate return (EFAULT);
16850Sstevel@tonic-gate }
16860Sstevel@tonic-gate
16870Sstevel@tonic-gate return (0);
16880Sstevel@tonic-gate }
16890Sstevel@tonic-gate
16900Sstevel@tonic-gate /*
16910Sstevel@tonic-gate * Every open of /dev/crypto multiplexes all PKCS#11 sessions across
16920Sstevel@tonic-gate * a single session to each provider. Calls to open and close session
16930Sstevel@tonic-gate * are not made to providers that do not support sessions. For these
16940Sstevel@tonic-gate * providers, a session number of 0 is passed during subsequent operations,
16950Sstevel@tonic-gate * and it is ignored by the provider.
16960Sstevel@tonic-gate */
16970Sstevel@tonic-gate static int
crypto_get_provider_session(crypto_minor_t * cm,crypto_provider_id_t provider_index,crypto_provider_session_t ** output_ps)16980Sstevel@tonic-gate crypto_get_provider_session(crypto_minor_t *cm,
16990Sstevel@tonic-gate crypto_provider_id_t provider_index, crypto_provider_session_t **output_ps)
17000Sstevel@tonic-gate {
17010Sstevel@tonic-gate kcf_provider_desc_t *pd, *real_provider;
17020Sstevel@tonic-gate kcf_req_params_t params;
17030Sstevel@tonic-gate crypto_provider_session_t *ps, *new_ps;
17040Sstevel@tonic-gate crypto_session_id_t provider_session_id = 0;
17050Sstevel@tonic-gate int rv;
17060Sstevel@tonic-gate
17070Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cm->cm_lock));
17080Sstevel@tonic-gate
17090Sstevel@tonic-gate /* pd may be a logical provider */
17100Sstevel@tonic-gate pd = cm->cm_provider_array[provider_index];
17110Sstevel@tonic-gate
17120Sstevel@tonic-gate again:
17130Sstevel@tonic-gate /*
17140Sstevel@tonic-gate * Check if there is already a session to the provider.
17150Sstevel@tonic-gate * Sessions may be to a logical provider or a real provider.
17160Sstevel@tonic-gate */
17170Sstevel@tonic-gate for (ps = cm->cm_provider_session; ps != NULL; ps = ps->ps_next) {
17180Sstevel@tonic-gate if (ps->ps_provider == pd)
17190Sstevel@tonic-gate break;
17200Sstevel@tonic-gate }
17210Sstevel@tonic-gate
17220Sstevel@tonic-gate /* found existing session */
17230Sstevel@tonic-gate if (ps != NULL) {
17240Sstevel@tonic-gate ps->ps_refcnt++;
17250Sstevel@tonic-gate *output_ps = ps;
17260Sstevel@tonic-gate return (CRYPTO_SUCCESS);
17270Sstevel@tonic-gate }
17280Sstevel@tonic-gate mutex_exit(&cm->cm_lock);
17290Sstevel@tonic-gate
17300Sstevel@tonic-gate /* find a hardware provider that supports session ops */
17310Sstevel@tonic-gate (void) kcf_get_hardware_provider_nomech(CRYPTO_OPS_OFFSET(session_ops),
1732*12304SValerie.Fenwick@Oracle.COM CRYPTO_SESSION_OFFSET(session_open), pd, &real_provider);
17330Sstevel@tonic-gate
17340Sstevel@tonic-gate if (real_provider != NULL) {
17350Sstevel@tonic-gate ASSERT(real_provider == pd ||
17360Sstevel@tonic-gate pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER);
17370Sstevel@tonic-gate /* open session to provider */
17380Sstevel@tonic-gate KCF_WRAP_SESSION_OPS_PARAMS(¶ms, KCF_OP_SESSION_OPEN,
17390Sstevel@tonic-gate &provider_session_id, 0, CRYPTO_USER, NULL, 0, pd);
17400Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms,
17410Sstevel@tonic-gate B_FALSE);
17420Sstevel@tonic-gate if (rv != CRYPTO_SUCCESS) {
17430Sstevel@tonic-gate mutex_enter(&cm->cm_lock);
1744904Smcpowers KCF_PROV_REFRELE(real_provider);
17450Sstevel@tonic-gate return (rv);
17460Sstevel@tonic-gate }
17470Sstevel@tonic-gate }
17480Sstevel@tonic-gate
17490Sstevel@tonic-gate /* allocate crypto_provider_session structure */
17500Sstevel@tonic-gate new_ps = kmem_zalloc(sizeof (crypto_provider_session_t), KM_SLEEP);
17510Sstevel@tonic-gate
17520Sstevel@tonic-gate /*
17530Sstevel@tonic-gate * Check if someone opened a session to the provider
17540Sstevel@tonic-gate * while we dropped the lock.
17550Sstevel@tonic-gate */
17560Sstevel@tonic-gate mutex_enter(&cm->cm_lock);
17570Sstevel@tonic-gate for (ps = cm->cm_provider_session; ps != NULL; ps = ps->ps_next) {
17580Sstevel@tonic-gate if (ps->ps_provider == pd) {
17590Sstevel@tonic-gate mutex_exit(&cm->cm_lock);
17600Sstevel@tonic-gate kmem_free(new_ps, sizeof (crypto_provider_session_t));
17610Sstevel@tonic-gate if (real_provider != NULL) {
17620Sstevel@tonic-gate KCF_WRAP_SESSION_OPS_PARAMS(¶ms,
17630Sstevel@tonic-gate KCF_OP_SESSION_CLOSE, NULL,
17640Sstevel@tonic-gate provider_session_id, CRYPTO_USER, NULL, 0,
17650Sstevel@tonic-gate pd);
17660Sstevel@tonic-gate (void) kcf_submit_request(real_provider, NULL,
17670Sstevel@tonic-gate NULL, ¶ms, B_FALSE);
1768904Smcpowers KCF_PROV_REFRELE(real_provider);
17690Sstevel@tonic-gate }
17700Sstevel@tonic-gate mutex_enter(&cm->cm_lock);
17710Sstevel@tonic-gate goto again;
17720Sstevel@tonic-gate
17730Sstevel@tonic-gate }
17740Sstevel@tonic-gate }
17750Sstevel@tonic-gate
17760Sstevel@tonic-gate /* increment refcnt and attach to crypto_minor structure */
17770Sstevel@tonic-gate new_ps->ps_session = provider_session_id;
17780Sstevel@tonic-gate new_ps->ps_refcnt = 1;
17790Sstevel@tonic-gate KCF_PROV_REFHOLD(pd);
17800Sstevel@tonic-gate new_ps->ps_provider = pd;
17810Sstevel@tonic-gate if (real_provider != NULL) {
17820Sstevel@tonic-gate new_ps->ps_real_provider = real_provider;
17830Sstevel@tonic-gate }
17840Sstevel@tonic-gate new_ps->ps_next = cm->cm_provider_session;
17850Sstevel@tonic-gate cm->cm_provider_session = new_ps;
17860Sstevel@tonic-gate
17870Sstevel@tonic-gate *output_ps = new_ps;
17880Sstevel@tonic-gate return (CRYPTO_SUCCESS);
17890Sstevel@tonic-gate }
17900Sstevel@tonic-gate
17910Sstevel@tonic-gate /*
17920Sstevel@tonic-gate * Release a provider session.
17930Sstevel@tonic-gate * If the reference count goes to zero, then close the session
17940Sstevel@tonic-gate * to the provider.
17950Sstevel@tonic-gate */
17960Sstevel@tonic-gate static void
crypto_release_provider_session(crypto_minor_t * cm,crypto_provider_session_t * provider_session)17970Sstevel@tonic-gate crypto_release_provider_session(crypto_minor_t *cm,
17980Sstevel@tonic-gate crypto_provider_session_t *provider_session)
17990Sstevel@tonic-gate {
18000Sstevel@tonic-gate kcf_req_params_t params;
18010Sstevel@tonic-gate crypto_provider_session_t *ps = NULL, **prev;
18020Sstevel@tonic-gate
18030Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cm->cm_lock));
18040Sstevel@tonic-gate
18050Sstevel@tonic-gate /* verify that provider_session is valid */
18060Sstevel@tonic-gate for (ps = cm->cm_provider_session, prev = &cm->cm_provider_session;
18070Sstevel@tonic-gate ps != NULL; prev = &ps->ps_next, ps = ps->ps_next) {
18080Sstevel@tonic-gate if (ps == provider_session) {
18090Sstevel@tonic-gate break;
18100Sstevel@tonic-gate }
18110Sstevel@tonic-gate }
18120Sstevel@tonic-gate
18130Sstevel@tonic-gate if (ps == NULL)
18140Sstevel@tonic-gate return;
18150Sstevel@tonic-gate
18160Sstevel@tonic-gate ps->ps_refcnt--;
18170Sstevel@tonic-gate
18180Sstevel@tonic-gate if (ps->ps_refcnt > 0)
18190Sstevel@tonic-gate return;
18200Sstevel@tonic-gate
18210Sstevel@tonic-gate if (ps->ps_real_provider != NULL) {
18220Sstevel@tonic-gate /* close session with provider */
18230Sstevel@tonic-gate KCF_WRAP_SESSION_OPS_PARAMS(¶ms, KCF_OP_SESSION_CLOSE, NULL,
18240Sstevel@tonic-gate ps->ps_session, CRYPTO_USER, NULL, 0, ps->ps_provider);
18250Sstevel@tonic-gate (void) kcf_submit_request(ps->ps_real_provider,
18260Sstevel@tonic-gate NULL, NULL, ¶ms, B_FALSE);
18270Sstevel@tonic-gate KCF_PROV_REFRELE(ps->ps_real_provider);
18280Sstevel@tonic-gate }
18290Sstevel@tonic-gate KCF_PROV_REFRELE(ps->ps_provider);
18300Sstevel@tonic-gate *prev = ps->ps_next;
18310Sstevel@tonic-gate kmem_free(ps, sizeof (*ps));
18320Sstevel@tonic-gate }
18330Sstevel@tonic-gate
18340Sstevel@tonic-gate static int
grow_session_table(crypto_minor_t * cm)18350Sstevel@tonic-gate grow_session_table(crypto_minor_t *cm)
18360Sstevel@tonic-gate {
18370Sstevel@tonic-gate crypto_session_data_t **session_table;
18380Sstevel@tonic-gate crypto_session_data_t **new;
18390Sstevel@tonic-gate uint_t session_table_count;
18400Sstevel@tonic-gate uint_t need;
18410Sstevel@tonic-gate size_t current_allocation;
18420Sstevel@tonic-gate size_t new_allocation;
18433916Skrishna int rv;
18440Sstevel@tonic-gate
18450Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cm->cm_lock));
18460Sstevel@tonic-gate
18470Sstevel@tonic-gate session_table_count = cm->cm_session_table_count;
18480Sstevel@tonic-gate session_table = cm->cm_session_table;
18490Sstevel@tonic-gate need = session_table_count + CRYPTO_SESSION_CHUNK;
18500Sstevel@tonic-gate
18510Sstevel@tonic-gate current_allocation = session_table_count * sizeof (void *);
18520Sstevel@tonic-gate new_allocation = need * sizeof (void *);
18530Sstevel@tonic-gate
18540Sstevel@tonic-gate /*
18550Sstevel@tonic-gate * Memory needed to grow the session table is checked
18560Sstevel@tonic-gate * against the project.max-crypto-memory resource control.
18570Sstevel@tonic-gate */
18583916Skrishna if ((rv = crypto_buffer_check(new_allocation - current_allocation)) !=
18593916Skrishna CRYPTO_SUCCESS) {
18603916Skrishna return (rv);
18613916Skrishna }
18620Sstevel@tonic-gate
18630Sstevel@tonic-gate /* drop lock while we allocate memory */
18640Sstevel@tonic-gate mutex_exit(&cm->cm_lock);
18650Sstevel@tonic-gate new = kmem_zalloc(new_allocation, KM_SLEEP);
18660Sstevel@tonic-gate mutex_enter(&cm->cm_lock);
18670Sstevel@tonic-gate
18680Sstevel@tonic-gate /* check if another thread increased the table size */
18690Sstevel@tonic-gate if (session_table_count != cm->cm_session_table_count) {
18700Sstevel@tonic-gate kmem_free(new, new_allocation);
18710Sstevel@tonic-gate return (CRYPTO_SUCCESS);
18720Sstevel@tonic-gate }
18730Sstevel@tonic-gate
18740Sstevel@tonic-gate bcopy(session_table, new, current_allocation);
18750Sstevel@tonic-gate kmem_free(session_table, current_allocation);
18760Sstevel@tonic-gate cm->cm_session_table = new;
18770Sstevel@tonic-gate cm->cm_session_table_count += CRYPTO_SESSION_CHUNK;
18780Sstevel@tonic-gate
18790Sstevel@tonic-gate return (CRYPTO_SUCCESS);
18800Sstevel@tonic-gate }
18810Sstevel@tonic-gate
18820Sstevel@tonic-gate /*
18830Sstevel@tonic-gate * Find unused entry in session table and return it's index.
18840Sstevel@tonic-gate * Initialize session table entry.
18850Sstevel@tonic-gate */
18860Sstevel@tonic-gate /* ARGSUSED */
18870Sstevel@tonic-gate static int
crypto_open_session(dev_t dev,uint_t flags,crypto_session_id_t * session_index,crypto_provider_id_t provider_id)18880Sstevel@tonic-gate crypto_open_session(dev_t dev, uint_t flags, crypto_session_id_t *session_index,
18890Sstevel@tonic-gate crypto_provider_id_t provider_id)
18900Sstevel@tonic-gate {
18910Sstevel@tonic-gate crypto_session_data_t **session_table;
18920Sstevel@tonic-gate crypto_session_data_t *sp;
18930Sstevel@tonic-gate crypto_minor_t *cm;
18940Sstevel@tonic-gate uint_t session_table_count;
18950Sstevel@tonic-gate uint_t i;
18960Sstevel@tonic-gate int rv;
18970Sstevel@tonic-gate crypto_provider_session_t *ps;
18980Sstevel@tonic-gate kcf_provider_desc_t *provider;
18990Sstevel@tonic-gate
19000Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
19010Sstevel@tonic-gate cmn_err(CE_WARN, "crypto_open_session: failed holding minor");
19020Sstevel@tonic-gate return (CRYPTO_FAILED);
19030Sstevel@tonic-gate }
19040Sstevel@tonic-gate
19050Sstevel@tonic-gate /* initialize provider_array */
19060Sstevel@tonic-gate if (cm->cm_provider_array == NULL) {
19070Sstevel@tonic-gate rv = crypto_get_provider_list(cm, NULL, NULL, DONT_RETURN_LIST);
19080Sstevel@tonic-gate if (rv != 0) {
19090Sstevel@tonic-gate crypto_release_minor(cm);
19100Sstevel@tonic-gate return (rv);
19110Sstevel@tonic-gate }
19120Sstevel@tonic-gate }
19130Sstevel@tonic-gate
19140Sstevel@tonic-gate mutex_enter(&cm->cm_lock);
19150Sstevel@tonic-gate /* index must be less than count of providers */
19160Sstevel@tonic-gate if (provider_id >= cm->cm_provider_count) {
19170Sstevel@tonic-gate mutex_exit(&cm->cm_lock);
19180Sstevel@tonic-gate crypto_release_minor(cm);
19190Sstevel@tonic-gate return (CRYPTO_INVALID_PROVIDER_ID);
19200Sstevel@tonic-gate }
19210Sstevel@tonic-gate ASSERT(cm->cm_provider_array != NULL);
19220Sstevel@tonic-gate
19230Sstevel@tonic-gate rv = crypto_get_provider_session(cm, provider_id, &ps);
19240Sstevel@tonic-gate if (rv != CRYPTO_SUCCESS) {
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 provider = cm->cm_provider_array[provider_id];
19300Sstevel@tonic-gate
19310Sstevel@tonic-gate again:
19320Sstevel@tonic-gate session_table_count = cm->cm_session_table_count;
19330Sstevel@tonic-gate session_table = cm->cm_session_table;
19340Sstevel@tonic-gate
19350Sstevel@tonic-gate /* session handles start with 1 */
19360Sstevel@tonic-gate for (i = 1; i < session_table_count; i++) {
19370Sstevel@tonic-gate if (session_table[i] == NULL)
19380Sstevel@tonic-gate break;
19390Sstevel@tonic-gate }
19400Sstevel@tonic-gate
19410Sstevel@tonic-gate if (i == session_table_count || session_table_count == 0) {
19420Sstevel@tonic-gate if ((rv = grow_session_table(cm)) != CRYPTO_SUCCESS) {
19430Sstevel@tonic-gate crypto_release_provider_session(cm, ps);
19440Sstevel@tonic-gate mutex_exit(&cm->cm_lock);
19450Sstevel@tonic-gate crypto_release_minor(cm);
19460Sstevel@tonic-gate return (rv);
19470Sstevel@tonic-gate }
19480Sstevel@tonic-gate goto again;
19490Sstevel@tonic-gate }
19500Sstevel@tonic-gate
19510Sstevel@tonic-gate sp = kmem_cache_alloc(crypto_session_cache, KM_SLEEP);
19520Sstevel@tonic-gate sp->sd_flags = 0;
19530Sstevel@tonic-gate sp->sd_find_init_cookie = NULL;
19540Sstevel@tonic-gate sp->sd_digest_ctx = NULL;
19550Sstevel@tonic-gate sp->sd_encr_ctx = NULL;
19560Sstevel@tonic-gate sp->sd_decr_ctx = NULL;
19570Sstevel@tonic-gate sp->sd_sign_ctx = NULL;
19580Sstevel@tonic-gate sp->sd_verify_ctx = NULL;
19590Sstevel@tonic-gate sp->sd_sign_recover_ctx = NULL;
19600Sstevel@tonic-gate sp->sd_verify_recover_ctx = NULL;
19610Sstevel@tonic-gate mutex_init(&sp->sd_lock, NULL, MUTEX_DRIVER, NULL);
19620Sstevel@tonic-gate cv_init(&sp->sd_cv, NULL, CV_DRIVER, NULL);
19630Sstevel@tonic-gate KCF_PROV_REFHOLD(provider);
19640Sstevel@tonic-gate sp->sd_provider = provider;
19650Sstevel@tonic-gate sp->sd_provider_session = ps;
19666424Skrishna
19676424Skrishna /* See the comment for CRYPTO_PRE_APPROVED_LIMIT. */
19686424Skrishna if ((rv = crypto_buffer_check(crypto_pre_approved_limit)) !=
19696424Skrishna CRYPTO_SUCCESS) {
19706424Skrishna sp->sd_pre_approved_amount = 0;
19716424Skrishna } else {
197211413Sopensolaris@drydog.com sp->sd_pre_approved_amount = (int)crypto_pre_approved_limit;
19736424Skrishna }
19746424Skrishna
19750Sstevel@tonic-gate cm->cm_session_table[i] = sp;
19760Sstevel@tonic-gate mutex_exit(&cm->cm_lock);
19770Sstevel@tonic-gate crypto_release_minor(cm);
19780Sstevel@tonic-gate *session_index = i;
19790Sstevel@tonic-gate
19800Sstevel@tonic-gate return (CRYPTO_SUCCESS);
19810Sstevel@tonic-gate }
19820Sstevel@tonic-gate
19830Sstevel@tonic-gate /*
19840Sstevel@tonic-gate * Close a session.
19850Sstevel@tonic-gate */
19860Sstevel@tonic-gate static int
crypto_close_session(dev_t dev,crypto_session_id_t session_index)19870Sstevel@tonic-gate crypto_close_session(dev_t dev, crypto_session_id_t session_index)
19880Sstevel@tonic-gate {
19890Sstevel@tonic-gate crypto_session_data_t **session_table;
19900Sstevel@tonic-gate crypto_session_data_t *sp;
19910Sstevel@tonic-gate crypto_minor_t *cm;
19920Sstevel@tonic-gate
19930Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
19940Sstevel@tonic-gate cmn_err(CE_WARN, "crypto_close_session: failed holding minor");
19950Sstevel@tonic-gate return (CRYPTO_FAILED);
19960Sstevel@tonic-gate }
19970Sstevel@tonic-gate
19980Sstevel@tonic-gate mutex_enter(&cm->cm_lock);
19990Sstevel@tonic-gate session_table = cm->cm_session_table;
20000Sstevel@tonic-gate
20010Sstevel@tonic-gate if ((session_index) == 0 ||
20020Sstevel@tonic-gate (session_index >= cm->cm_session_table_count)) {
20030Sstevel@tonic-gate mutex_exit(&cm->cm_lock);
20040Sstevel@tonic-gate crypto_release_minor(cm);
20050Sstevel@tonic-gate return (CRYPTO_SESSION_HANDLE_INVALID);
20060Sstevel@tonic-gate }
20070Sstevel@tonic-gate
20080Sstevel@tonic-gate sp = session_table[session_index];
20090Sstevel@tonic-gate if (sp == NULL) {
20100Sstevel@tonic-gate mutex_exit(&cm->cm_lock);
20110Sstevel@tonic-gate crypto_release_minor(cm);
20120Sstevel@tonic-gate return (CRYPTO_SESSION_HANDLE_INVALID);
20130Sstevel@tonic-gate }
20140Sstevel@tonic-gate /*
20150Sstevel@tonic-gate * If session is in use, free it when the thread
20160Sstevel@tonic-gate * finishes with the session.
20170Sstevel@tonic-gate */
20180Sstevel@tonic-gate mutex_enter(&sp->sd_lock);
20190Sstevel@tonic-gate if (sp->sd_flags & CRYPTO_SESSION_IS_BUSY) {
20200Sstevel@tonic-gate sp->sd_flags |= CRYPTO_SESSION_IS_CLOSED;
20210Sstevel@tonic-gate mutex_exit(&sp->sd_lock);
20220Sstevel@tonic-gate } else {
20236424Skrishna ASSERT(sp->sd_pre_approved_amount == 0 ||
20246424Skrishna sp->sd_pre_approved_amount == crypto_pre_approved_limit);
20256424Skrishna CRYPTO_DECREMENT_RCTL(sp->sd_pre_approved_amount);
20266424Skrishna
20270Sstevel@tonic-gate if (sp->sd_find_init_cookie != NULL) {
20280Sstevel@tonic-gate (void) crypto_free_find_ctx(sp);
20290Sstevel@tonic-gate }
20300Sstevel@tonic-gate
20310Sstevel@tonic-gate crypto_release_provider_session(cm, sp->sd_provider_session);
20320Sstevel@tonic-gate KCF_PROV_REFRELE(sp->sd_provider);
20330Sstevel@tonic-gate CRYPTO_CANCEL_ALL_CTX(sp);
20340Sstevel@tonic-gate mutex_destroy(&sp->sd_lock);
20350Sstevel@tonic-gate cv_destroy(&sp->sd_cv);
20360Sstevel@tonic-gate kmem_cache_free(crypto_session_cache, sp);
20370Sstevel@tonic-gate session_table[session_index] = NULL;
20380Sstevel@tonic-gate }
20390Sstevel@tonic-gate
20400Sstevel@tonic-gate mutex_exit(&cm->cm_lock);
20410Sstevel@tonic-gate crypto_release_minor(cm);
20420Sstevel@tonic-gate
20430Sstevel@tonic-gate return (CRYPTO_SUCCESS);
20440Sstevel@tonic-gate }
20450Sstevel@tonic-gate
20460Sstevel@tonic-gate /*
20470Sstevel@tonic-gate * This ioctl opens a session and returns the session ID in os_session.
20480Sstevel@tonic-gate */
20490Sstevel@tonic-gate /* ARGSUSED */
20500Sstevel@tonic-gate static int
open_session(dev_t dev,caddr_t arg,int mode,int * rval)20510Sstevel@tonic-gate open_session(dev_t dev, caddr_t arg, int mode, int *rval)
20520Sstevel@tonic-gate {
20530Sstevel@tonic-gate crypto_open_session_t open_session;
20540Sstevel@tonic-gate crypto_session_id_t session;
20550Sstevel@tonic-gate int rv;
20560Sstevel@tonic-gate
20570Sstevel@tonic-gate if (copyin(arg, &open_session, sizeof (open_session)) != 0)
20580Sstevel@tonic-gate return (EFAULT);
20590Sstevel@tonic-gate
20600Sstevel@tonic-gate rv = crypto_open_session(dev, open_session.os_flags,
20610Sstevel@tonic-gate &session, open_session.os_provider_id);
20620Sstevel@tonic-gate if (rv != CRYPTO_SUCCESS) {
20630Sstevel@tonic-gate open_session.os_return_value = rv;
20640Sstevel@tonic-gate if (copyout(&open_session, arg, sizeof (open_session)) != 0) {
20650Sstevel@tonic-gate return (EFAULT);
20660Sstevel@tonic-gate }
20670Sstevel@tonic-gate return (0);
20680Sstevel@tonic-gate }
20690Sstevel@tonic-gate
20700Sstevel@tonic-gate open_session.os_session = session;
20710Sstevel@tonic-gate open_session.os_return_value = CRYPTO_SUCCESS;
20720Sstevel@tonic-gate
20730Sstevel@tonic-gate if (copyout(&open_session, arg, sizeof (open_session)) != 0) {
20740Sstevel@tonic-gate return (EFAULT);
20750Sstevel@tonic-gate }
20760Sstevel@tonic-gate return (0);
20770Sstevel@tonic-gate }
20780Sstevel@tonic-gate
20790Sstevel@tonic-gate /*
20800Sstevel@tonic-gate * This ioctl closes a session.
20810Sstevel@tonic-gate */
20820Sstevel@tonic-gate /* ARGSUSED */
20830Sstevel@tonic-gate static int
close_session(dev_t dev,caddr_t arg,int mode,int * rval)20840Sstevel@tonic-gate close_session(dev_t dev, caddr_t arg, int mode, int *rval)
20850Sstevel@tonic-gate {
20860Sstevel@tonic-gate crypto_close_session_t close_session;
20870Sstevel@tonic-gate int rv;
20880Sstevel@tonic-gate
20890Sstevel@tonic-gate if (copyin(arg, &close_session, sizeof (close_session)) != 0)
20900Sstevel@tonic-gate return (EFAULT);
20910Sstevel@tonic-gate
20920Sstevel@tonic-gate rv = crypto_close_session(dev, close_session.cs_session);
2093904Smcpowers close_session.cs_return_value = rv;
20940Sstevel@tonic-gate if (copyout(&close_session, arg, sizeof (close_session)) != 0) {
20950Sstevel@tonic-gate return (EFAULT);
20960Sstevel@tonic-gate }
20970Sstevel@tonic-gate return (0);
20980Sstevel@tonic-gate }
20990Sstevel@tonic-gate
21000Sstevel@tonic-gate /*
21010Sstevel@tonic-gate * Copy data model dependent mechanism structure into a kernel mechanism
21020Sstevel@tonic-gate * structure. Allocate param storage if necessary.
21030Sstevel@tonic-gate */
21040Sstevel@tonic-gate static boolean_t
copyin_mech(int mode,crypto_session_data_t * sp,crypto_mechanism_t * in_mech,crypto_mechanism_t * out_mech,size_t * out_rctl_bytes,boolean_t * out_rctl_chk,int * out_rv,int * out_error)21056424Skrishna copyin_mech(int mode, crypto_session_data_t *sp, crypto_mechanism_t *in_mech,
21066424Skrishna crypto_mechanism_t *out_mech, size_t *out_rctl_bytes,
21076424Skrishna boolean_t *out_rctl_chk, int *out_rv, int *out_error)
21080Sstevel@tonic-gate {
21090Sstevel@tonic-gate STRUCT_DECL(crypto_mechanism, mech);
21100Sstevel@tonic-gate caddr_t param;
21110Sstevel@tonic-gate size_t param_len;
21126424Skrishna size_t rctl_bytes = 0;
21130Sstevel@tonic-gate int error = 0;
21140Sstevel@tonic-gate int rv = 0;
21150Sstevel@tonic-gate
21160Sstevel@tonic-gate STRUCT_INIT(mech, mode);
21170Sstevel@tonic-gate bcopy(in_mech, STRUCT_BUF(mech), STRUCT_SIZE(mech));
21180Sstevel@tonic-gate param = STRUCT_FGETP(mech, cm_param);
21190Sstevel@tonic-gate param_len = STRUCT_FGET(mech, cm_param_len);
21200Sstevel@tonic-gate out_mech->cm_type = STRUCT_FGET(mech, cm_type);
21210Sstevel@tonic-gate out_mech->cm_param = NULL;
21220Sstevel@tonic-gate out_mech->cm_param_len = 0;
21230Sstevel@tonic-gate if (param != NULL && param_len != 0) {
21240Sstevel@tonic-gate if (param_len > crypto_max_buffer_len) {
21250Sstevel@tonic-gate cmn_err(CE_NOTE, "copyin_mech: buffer greater than "
21260Sstevel@tonic-gate "%ld bytes, pid = %d", crypto_max_buffer_len,
21270Sstevel@tonic-gate curproc->p_pid);
21280Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD;
21290Sstevel@tonic-gate goto out;
21300Sstevel@tonic-gate }
21310Sstevel@tonic-gate
21326424Skrishna rv = CRYPTO_BUFFER_CHECK(sp, param_len, *out_rctl_chk);
21336424Skrishna if (rv != CRYPTO_SUCCESS) {
21346424Skrishna goto out;
21350Sstevel@tonic-gate }
21366424Skrishna rctl_bytes = param_len;
21376424Skrishna
21380Sstevel@tonic-gate out_mech->cm_param = kmem_alloc(param_len, KM_SLEEP);
21390Sstevel@tonic-gate if (copyin((char *)param, out_mech->cm_param, param_len) != 0) {
21400Sstevel@tonic-gate kmem_free(out_mech->cm_param, param_len);
21410Sstevel@tonic-gate out_mech->cm_param = NULL;
21420Sstevel@tonic-gate error = EFAULT;
21430Sstevel@tonic-gate goto out;
21440Sstevel@tonic-gate }
21450Sstevel@tonic-gate out_mech->cm_param_len = param_len;
21460Sstevel@tonic-gate }
21470Sstevel@tonic-gate out:
21480Sstevel@tonic-gate *out_rctl_bytes = rctl_bytes;
21490Sstevel@tonic-gate *out_rv = rv;
21500Sstevel@tonic-gate *out_error = error;
21510Sstevel@tonic-gate return ((rv | error) ? B_FALSE : B_TRUE);
21520Sstevel@tonic-gate }
21530Sstevel@tonic-gate
21540Sstevel@tonic-gate /*
21550Sstevel@tonic-gate * Free key attributes when key type is CRYPTO_KEY_ATTR_LIST.
21560Sstevel@tonic-gate * The crypto_key structure is not freed.
21570Sstevel@tonic-gate */
21580Sstevel@tonic-gate static void
crypto_free_key_attributes(crypto_key_t * key)21590Sstevel@tonic-gate crypto_free_key_attributes(crypto_key_t *key)
21600Sstevel@tonic-gate {
21610Sstevel@tonic-gate crypto_object_attribute_t *attrs;
21620Sstevel@tonic-gate size_t len = 0;
21630Sstevel@tonic-gate int i;
21640Sstevel@tonic-gate
21650Sstevel@tonic-gate ASSERT(key->ck_format == CRYPTO_KEY_ATTR_LIST);
21660Sstevel@tonic-gate if (key->ck_count == 0 || key->ck_attrs == NULL)
21670Sstevel@tonic-gate return;
21680Sstevel@tonic-gate
21690Sstevel@tonic-gate /* compute the size of the container */
21700Sstevel@tonic-gate len = key->ck_count * sizeof (crypto_object_attribute_t);
21710Sstevel@tonic-gate
21720Sstevel@tonic-gate /* total up the size of all attributes in the container */
21730Sstevel@tonic-gate for (i = 0; i < key->ck_count; i++) {
21740Sstevel@tonic-gate attrs = &key->ck_attrs[i];
21750Sstevel@tonic-gate if (attrs->oa_value_len != 0 &&
21760Sstevel@tonic-gate attrs->oa_value != NULL) {
21770Sstevel@tonic-gate len += roundup(attrs->oa_value_len, sizeof (caddr_t));
21780Sstevel@tonic-gate }
21790Sstevel@tonic-gate }
21800Sstevel@tonic-gate
21810Sstevel@tonic-gate bzero(key->ck_attrs, len);
21820Sstevel@tonic-gate kmem_free(key->ck_attrs, len);
21830Sstevel@tonic-gate }
21840Sstevel@tonic-gate
21850Sstevel@tonic-gate /*
21860Sstevel@tonic-gate * Frees allocated storage in the key structure, but doesn't free
21870Sstevel@tonic-gate * the key structure.
21880Sstevel@tonic-gate */
21890Sstevel@tonic-gate static void
free_crypto_key(crypto_key_t * key)21900Sstevel@tonic-gate free_crypto_key(crypto_key_t *key)
21910Sstevel@tonic-gate {
21920Sstevel@tonic-gate switch (key->ck_format) {
21930Sstevel@tonic-gate case CRYPTO_KEY_RAW: {
21940Sstevel@tonic-gate size_t len;
21950Sstevel@tonic-gate
21960Sstevel@tonic-gate if (key->ck_length == 0 || key->ck_data == NULL)
21970Sstevel@tonic-gate break;
21980Sstevel@tonic-gate
21990Sstevel@tonic-gate len = CRYPTO_BITS2BYTES(key->ck_length);
22000Sstevel@tonic-gate bzero(key->ck_data, len);
22010Sstevel@tonic-gate kmem_free(key->ck_data, len);
22020Sstevel@tonic-gate break;
22030Sstevel@tonic-gate }
22040Sstevel@tonic-gate
22050Sstevel@tonic-gate case CRYPTO_KEY_ATTR_LIST:
22060Sstevel@tonic-gate crypto_free_key_attributes(key);
22070Sstevel@tonic-gate break;
22080Sstevel@tonic-gate
22090Sstevel@tonic-gate default:
22100Sstevel@tonic-gate break;
22110Sstevel@tonic-gate }
22120Sstevel@tonic-gate }
22130Sstevel@tonic-gate
22140Sstevel@tonic-gate /*
22150Sstevel@tonic-gate * Copy in an array of crypto_object_attribute structures from user-space.
22160Sstevel@tonic-gate * Kernel memory is allocated for the array and the value of each attribute
22170Sstevel@tonic-gate * in the array. Since unprivileged users can specify the size of attributes,
22180Sstevel@tonic-gate * the amount of memory needed is charged against the
22190Sstevel@tonic-gate * project.max-crypto-memory resource control.
22200Sstevel@tonic-gate *
22210Sstevel@tonic-gate * Attribute values are copied in from user-space if copyin_value is set to
22220Sstevel@tonic-gate * B_TRUE. This routine returns B_TRUE if the copyin was successful.
22230Sstevel@tonic-gate */
22240Sstevel@tonic-gate static boolean_t
copyin_attributes(int mode,crypto_session_data_t * sp,uint_t count,caddr_t oc_attributes,crypto_object_attribute_t ** k_attrs_out,size_t * k_attrs_size_out,caddr_t * u_attrs_out,int * out_rv,int * out_error,size_t * out_rctl_bytes,boolean_t * out_rctl_chk,boolean_t copyin_value)22256424Skrishna copyin_attributes(int mode, crypto_session_data_t *sp,
22266424Skrishna uint_t count, caddr_t oc_attributes,
22270Sstevel@tonic-gate crypto_object_attribute_t **k_attrs_out, size_t *k_attrs_size_out,
22280Sstevel@tonic-gate caddr_t *u_attrs_out, int *out_rv, int *out_error, size_t *out_rctl_bytes,
22296424Skrishna boolean_t *out_rctl_chk, boolean_t copyin_value)
22300Sstevel@tonic-gate {
22310Sstevel@tonic-gate STRUCT_DECL(crypto_object_attribute, oa);
22320Sstevel@tonic-gate crypto_object_attribute_t *k_attrs = NULL;
22330Sstevel@tonic-gate caddr_t attrs = NULL, ap, p, value;
22340Sstevel@tonic-gate caddr_t k_attrs_buf;
22350Sstevel@tonic-gate size_t k_attrs_len;
22360Sstevel@tonic-gate size_t k_attrs_buf_len = 0;
22370Sstevel@tonic-gate size_t k_attrs_total_len = 0;
22380Sstevel@tonic-gate size_t tmp_len;
22390Sstevel@tonic-gate size_t rctl_bytes = 0;
22400Sstevel@tonic-gate size_t len = 0;
22410Sstevel@tonic-gate size_t value_len;
22420Sstevel@tonic-gate int error = 0;
22430Sstevel@tonic-gate int rv = 0;
22440Sstevel@tonic-gate int i;
22450Sstevel@tonic-gate
22460Sstevel@tonic-gate STRUCT_INIT(oa, mode);
22470Sstevel@tonic-gate
22480Sstevel@tonic-gate if (count == 0) {
22490Sstevel@tonic-gate rv = CRYPTO_SUCCESS;
22500Sstevel@tonic-gate goto out;
22510Sstevel@tonic-gate }
22520Sstevel@tonic-gate
22530Sstevel@tonic-gate if (count > CRYPTO_MAX_ATTRIBUTE_COUNT) {
22540Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD;
22550Sstevel@tonic-gate goto out;
22560Sstevel@tonic-gate }
22570Sstevel@tonic-gate
22580Sstevel@tonic-gate /* compute size of crypto_object_attribute array */
22590Sstevel@tonic-gate len = count * STRUCT_SIZE(oa);
22600Sstevel@tonic-gate
22610Sstevel@tonic-gate /* this allocation is not charged against the user's resource limit */
22620Sstevel@tonic-gate attrs = kmem_alloc(len, KM_SLEEP);
22630Sstevel@tonic-gate if (copyin(oc_attributes, attrs, len) != 0) {
22640Sstevel@tonic-gate error = EFAULT;
22650Sstevel@tonic-gate goto out;
22660Sstevel@tonic-gate }
22670Sstevel@tonic-gate
22680Sstevel@tonic-gate /* figure out how much memory to allocate for all of the attributes */
22690Sstevel@tonic-gate ap = attrs;
22700Sstevel@tonic-gate for (i = 0; i < count; i++) {
22710Sstevel@tonic-gate bcopy(ap, STRUCT_BUF(oa), STRUCT_SIZE(oa));
22720Sstevel@tonic-gate tmp_len = roundup(STRUCT_FGET(oa, oa_value_len),
22730Sstevel@tonic-gate sizeof (caddr_t));
22740Sstevel@tonic-gate if (tmp_len > crypto_max_buffer_len) {
22750Sstevel@tonic-gate cmn_err(CE_NOTE, "copyin_attributes: buffer greater "
22760Sstevel@tonic-gate "than %ld bytes, pid = %d", crypto_max_buffer_len,
22770Sstevel@tonic-gate curproc->p_pid);
22780Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD;
22790Sstevel@tonic-gate goto out;
22800Sstevel@tonic-gate }
22810Sstevel@tonic-gate if (STRUCT_FGETP(oa, oa_value) != NULL)
22820Sstevel@tonic-gate k_attrs_buf_len += tmp_len;
22830Sstevel@tonic-gate ap += STRUCT_SIZE(oa);
22840Sstevel@tonic-gate }
22850Sstevel@tonic-gate
22860Sstevel@tonic-gate k_attrs_len = count * sizeof (crypto_object_attribute_t);
22870Sstevel@tonic-gate k_attrs_total_len = k_attrs_buf_len + k_attrs_len;
22886424Skrishna
22896424Skrishna rv = CRYPTO_BUFFER_CHECK(sp, k_attrs_total_len, *out_rctl_chk);
22906424Skrishna if (rv != CRYPTO_SUCCESS) {
22916424Skrishna goto out;
22926424Skrishna }
22936424Skrishna rctl_bytes = k_attrs_total_len;
22940Sstevel@tonic-gate
22950Sstevel@tonic-gate /* one big allocation for everything */
22960Sstevel@tonic-gate k_attrs = kmem_alloc(k_attrs_total_len, KM_SLEEP);
22970Sstevel@tonic-gate k_attrs_buf = (char *)k_attrs + k_attrs_len;
22980Sstevel@tonic-gate
22990Sstevel@tonic-gate ap = attrs;
23000Sstevel@tonic-gate p = k_attrs_buf;
23010Sstevel@tonic-gate for (i = 0; i < count; i++) {
23020Sstevel@tonic-gate bcopy(ap, STRUCT_BUF(oa), STRUCT_SIZE(oa));
23030Sstevel@tonic-gate k_attrs[i].oa_type = STRUCT_FGET(oa, oa_type);
23040Sstevel@tonic-gate value = STRUCT_FGETP(oa, oa_value);
23050Sstevel@tonic-gate value_len = STRUCT_FGET(oa, oa_value_len);
23060Sstevel@tonic-gate if (value != NULL && value_len != 0 && copyin_value) {
23070Sstevel@tonic-gate if (copyin(value, p, value_len) != 0) {
23080Sstevel@tonic-gate kmem_free(k_attrs, k_attrs_total_len);
23090Sstevel@tonic-gate k_attrs = NULL;
23100Sstevel@tonic-gate error = EFAULT;
23110Sstevel@tonic-gate goto out;
23120Sstevel@tonic-gate }
23130Sstevel@tonic-gate }
23140Sstevel@tonic-gate
2315292Smcpowers if (value != NULL) {
2316292Smcpowers k_attrs[i].oa_value = p;
2317292Smcpowers p += roundup(value_len, sizeof (caddr_t));
2318292Smcpowers } else {
2319292Smcpowers k_attrs[i].oa_value = NULL;
2320292Smcpowers }
23210Sstevel@tonic-gate k_attrs[i].oa_value_len = value_len;
23220Sstevel@tonic-gate ap += STRUCT_SIZE(oa);
23230Sstevel@tonic-gate }
23240Sstevel@tonic-gate out:
23250Sstevel@tonic-gate if (attrs != NULL) {
23260Sstevel@tonic-gate /*
23270Sstevel@tonic-gate * Free the array if there is a failure or the caller
23280Sstevel@tonic-gate * doesn't want the array to be returned.
23290Sstevel@tonic-gate */
23300Sstevel@tonic-gate if (error != 0 || rv != CRYPTO_SUCCESS || u_attrs_out == NULL) {
23310Sstevel@tonic-gate kmem_free(attrs, len);
23320Sstevel@tonic-gate attrs = NULL;
23330Sstevel@tonic-gate }
23340Sstevel@tonic-gate }
23350Sstevel@tonic-gate
23360Sstevel@tonic-gate if (u_attrs_out != NULL)
23370Sstevel@tonic-gate *u_attrs_out = attrs;
23380Sstevel@tonic-gate if (k_attrs_size_out != NULL)
23390Sstevel@tonic-gate *k_attrs_size_out = k_attrs_total_len;
23400Sstevel@tonic-gate *k_attrs_out = k_attrs;
23410Sstevel@tonic-gate *out_rctl_bytes = rctl_bytes;
23420Sstevel@tonic-gate *out_rv = rv;
23430Sstevel@tonic-gate *out_error = error;
23440Sstevel@tonic-gate return ((rv | error) ? B_FALSE : B_TRUE);
23450Sstevel@tonic-gate }
23460Sstevel@tonic-gate
23470Sstevel@tonic-gate /*
23480Sstevel@tonic-gate * Copy data model dependent raw key into a kernel key
23490Sstevel@tonic-gate * structure. Checks key length or attribute lengths against
23500Sstevel@tonic-gate * resource controls before allocating memory. Returns B_TRUE
23510Sstevel@tonic-gate * if both error and rv are set to 0.
23520Sstevel@tonic-gate */
23530Sstevel@tonic-gate static boolean_t
copyin_key(int mode,crypto_session_data_t * sp,crypto_key_t * in_key,crypto_key_t * out_key,size_t * out_rctl_bytes,boolean_t * out_rctl_chk,int * out_rv,int * out_error)23546424Skrishna copyin_key(int mode, crypto_session_data_t *sp, crypto_key_t *in_key,
23556424Skrishna crypto_key_t *out_key, size_t *out_rctl_bytes,
23566424Skrishna boolean_t *out_rctl_chk, int *out_rv, int *out_error)
23570Sstevel@tonic-gate {
23580Sstevel@tonic-gate STRUCT_DECL(crypto_key, key);
23590Sstevel@tonic-gate crypto_object_attribute_t *k_attrs = NULL;
23600Sstevel@tonic-gate size_t key_bits;
23610Sstevel@tonic-gate size_t key_bytes = 0;
23620Sstevel@tonic-gate size_t rctl_bytes = 0;
23630Sstevel@tonic-gate int count;
23640Sstevel@tonic-gate int error = 0;
23650Sstevel@tonic-gate int rv = CRYPTO_SUCCESS;
23660Sstevel@tonic-gate
23670Sstevel@tonic-gate STRUCT_INIT(key, mode);
23680Sstevel@tonic-gate bcopy(in_key, STRUCT_BUF(key), STRUCT_SIZE(key));
23690Sstevel@tonic-gate out_key->ck_format = STRUCT_FGET(key, ck_format);
23700Sstevel@tonic-gate switch (out_key->ck_format) {
23710Sstevel@tonic-gate case CRYPTO_KEY_RAW:
23720Sstevel@tonic-gate key_bits = STRUCT_FGET(key, ck_length);
23730Sstevel@tonic-gate if (key_bits != 0) {
237411413Sopensolaris@drydog.com if (key_bits >
237511413Sopensolaris@drydog.com (CRYPTO_BYTES2BITS(crypto_max_buffer_len))) {
23760Sstevel@tonic-gate cmn_err(CE_NOTE, "copyin_key: buffer greater "
23770Sstevel@tonic-gate "than %ld bytes, pid = %d",
23780Sstevel@tonic-gate crypto_max_buffer_len, curproc->p_pid);
23790Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD;
23800Sstevel@tonic-gate goto out;
23810Sstevel@tonic-gate }
238211413Sopensolaris@drydog.com key_bytes = CRYPTO_BITS2BYTES(key_bits);
23830Sstevel@tonic-gate
23846424Skrishna rv = CRYPTO_BUFFER_CHECK(sp, key_bytes,
23856424Skrishna *out_rctl_chk);
23860Sstevel@tonic-gate if (rv != CRYPTO_SUCCESS) {
23870Sstevel@tonic-gate goto out;
23880Sstevel@tonic-gate }
23896424Skrishna rctl_bytes = key_bytes;
23900Sstevel@tonic-gate
23910Sstevel@tonic-gate out_key->ck_data = kmem_alloc(key_bytes, KM_SLEEP);
23920Sstevel@tonic-gate
23930Sstevel@tonic-gate if (copyin((char *)STRUCT_FGETP(key, ck_data),
23940Sstevel@tonic-gate out_key->ck_data, key_bytes) != 0) {
23950Sstevel@tonic-gate kmem_free(out_key->ck_data, key_bytes);
23960Sstevel@tonic-gate out_key->ck_data = NULL;
23970Sstevel@tonic-gate out_key->ck_length = 0;
23980Sstevel@tonic-gate error = EFAULT;
23990Sstevel@tonic-gate goto out;
24000Sstevel@tonic-gate }
24010Sstevel@tonic-gate }
240211413Sopensolaris@drydog.com out_key->ck_length = (ulong_t)key_bits;
24030Sstevel@tonic-gate break;
24040Sstevel@tonic-gate
24050Sstevel@tonic-gate case CRYPTO_KEY_ATTR_LIST:
24060Sstevel@tonic-gate count = STRUCT_FGET(key, ck_count);
24070Sstevel@tonic-gate
24086424Skrishna if (copyin_attributes(mode, sp, count,
24090Sstevel@tonic-gate (caddr_t)STRUCT_FGETP(key, ck_attrs), &k_attrs, NULL, NULL,
24106424Skrishna &rv, &error, &rctl_bytes, out_rctl_chk, B_TRUE)) {
24110Sstevel@tonic-gate out_key->ck_count = count;
24120Sstevel@tonic-gate out_key->ck_attrs = k_attrs;
24130Sstevel@tonic-gate k_attrs = NULL;
24140Sstevel@tonic-gate } else {
24150Sstevel@tonic-gate out_key->ck_count = 0;
24160Sstevel@tonic-gate out_key->ck_attrs = NULL;
24170Sstevel@tonic-gate }
24180Sstevel@tonic-gate break;
24190Sstevel@tonic-gate
24200Sstevel@tonic-gate case CRYPTO_KEY_REFERENCE:
24210Sstevel@tonic-gate out_key->ck_obj_id = STRUCT_FGET(key, ck_obj_id);
24220Sstevel@tonic-gate break;
24230Sstevel@tonic-gate
24240Sstevel@tonic-gate default:
24250Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD;
24260Sstevel@tonic-gate }
24270Sstevel@tonic-gate
24280Sstevel@tonic-gate out:
24290Sstevel@tonic-gate *out_rctl_bytes = rctl_bytes;
24300Sstevel@tonic-gate *out_rv = rv;
24310Sstevel@tonic-gate *out_error = error;
24320Sstevel@tonic-gate return ((rv | error) ? B_FALSE : B_TRUE);
24330Sstevel@tonic-gate }
24340Sstevel@tonic-gate
24350Sstevel@tonic-gate /*
24360Sstevel@tonic-gate * This routine does two things:
24370Sstevel@tonic-gate * 1. Given a crypto_minor structure and a session ID, it returns
24380Sstevel@tonic-gate * a valid session pointer.
24390Sstevel@tonic-gate * 2. It checks that the provider, to which the session has been opened,
24400Sstevel@tonic-gate * has not been removed.
24410Sstevel@tonic-gate */
24420Sstevel@tonic-gate static boolean_t
get_session_ptr(crypto_session_id_t i,crypto_minor_t * cm,crypto_session_data_t ** session_ptr,int * out_error,int * out_rv)24430Sstevel@tonic-gate get_session_ptr(crypto_session_id_t i, crypto_minor_t *cm,
24440Sstevel@tonic-gate crypto_session_data_t **session_ptr, int *out_error, int *out_rv)
24450Sstevel@tonic-gate {
24460Sstevel@tonic-gate crypto_session_data_t *sp = NULL;
24470Sstevel@tonic-gate int rv = CRYPTO_SESSION_HANDLE_INVALID;
24480Sstevel@tonic-gate int error = 0;
24490Sstevel@tonic-gate
24500Sstevel@tonic-gate mutex_enter(&cm->cm_lock);
24510Sstevel@tonic-gate if ((i < cm->cm_session_table_count) &&
24520Sstevel@tonic-gate (cm->cm_session_table[i] != NULL)) {
24530Sstevel@tonic-gate sp = cm->cm_session_table[i];
24540Sstevel@tonic-gate mutex_enter(&sp->sd_lock);
24550Sstevel@tonic-gate mutex_exit(&cm->cm_lock);
24560Sstevel@tonic-gate while (sp->sd_flags & CRYPTO_SESSION_IS_BUSY) {
24570Sstevel@tonic-gate if (cv_wait_sig(&sp->sd_cv, &sp->sd_lock) == 0) {
24580Sstevel@tonic-gate mutex_exit(&sp->sd_lock);
24590Sstevel@tonic-gate sp = NULL;
24600Sstevel@tonic-gate error = EINTR;
24610Sstevel@tonic-gate goto out;
24620Sstevel@tonic-gate }
24630Sstevel@tonic-gate }
24640Sstevel@tonic-gate
24650Sstevel@tonic-gate if (sp->sd_flags & CRYPTO_SESSION_IS_CLOSED) {
24660Sstevel@tonic-gate mutex_exit(&sp->sd_lock);
24670Sstevel@tonic-gate sp = NULL;
24680Sstevel@tonic-gate goto out;
24690Sstevel@tonic-gate }
24700Sstevel@tonic-gate
24710Sstevel@tonic-gate if (KCF_IS_PROV_REMOVED(sp->sd_provider)) {
24720Sstevel@tonic-gate mutex_exit(&sp->sd_lock);
24730Sstevel@tonic-gate sp = NULL;
24740Sstevel@tonic-gate rv = CRYPTO_DEVICE_ERROR;
24750Sstevel@tonic-gate goto out;
24760Sstevel@tonic-gate }
24770Sstevel@tonic-gate
24780Sstevel@tonic-gate rv = CRYPTO_SUCCESS;
24790Sstevel@tonic-gate sp->sd_flags |= CRYPTO_SESSION_IS_BUSY;
24800Sstevel@tonic-gate mutex_exit(&sp->sd_lock);
24810Sstevel@tonic-gate } else {
24820Sstevel@tonic-gate mutex_exit(&cm->cm_lock);
24830Sstevel@tonic-gate }
24840Sstevel@tonic-gate out:
24850Sstevel@tonic-gate *session_ptr = sp;
24860Sstevel@tonic-gate *out_error = error;
24870Sstevel@tonic-gate *out_rv = rv;
24880Sstevel@tonic-gate return ((rv == CRYPTO_SUCCESS && error == 0) ? B_TRUE : B_FALSE);
24890Sstevel@tonic-gate }
24900Sstevel@tonic-gate
24916424Skrishna #define CRYPTO_SESSION_RELE(s) if ((s) != NULL) { \
24920Sstevel@tonic-gate mutex_enter(&((s)->sd_lock)); \
24930Sstevel@tonic-gate (s)->sd_flags &= ~CRYPTO_SESSION_IS_BUSY; \
24940Sstevel@tonic-gate cv_broadcast(&(s)->sd_cv); \
24950Sstevel@tonic-gate mutex_exit(&((s)->sd_lock)); \
24960Sstevel@tonic-gate }
24970Sstevel@tonic-gate
24980Sstevel@tonic-gate /* ARGSUSED */
24990Sstevel@tonic-gate static int
encrypt_init(dev_t dev,caddr_t arg,int mode,int * rval)25000Sstevel@tonic-gate encrypt_init(dev_t dev, caddr_t arg, int mode, int *rval)
25010Sstevel@tonic-gate {
25020Sstevel@tonic-gate return (cipher_init(dev, arg, mode, crypto_encrypt_init_prov));
25030Sstevel@tonic-gate }
25040Sstevel@tonic-gate
25050Sstevel@tonic-gate /* ARGSUSED */
25060Sstevel@tonic-gate static int
decrypt_init(dev_t dev,caddr_t arg,int mode,int * rval)25070Sstevel@tonic-gate decrypt_init(dev_t dev, caddr_t arg, int mode, int *rval)
25080Sstevel@tonic-gate {
25090Sstevel@tonic-gate return (cipher_init(dev, arg, mode, crypto_decrypt_init_prov));
25100Sstevel@tonic-gate }
25110Sstevel@tonic-gate
25120Sstevel@tonic-gate /*
2513904Smcpowers * umech is a mechanism structure that has been copied from user address
2514904Smcpowers * space into kernel address space. Only one copyin has been done.
2515904Smcpowers * The mechanism parameter, if non-null, still points to user address space.
2516904Smcpowers * If the mechanism parameter contains pointers, they are pointers into
2517904Smcpowers * user address space.
2518904Smcpowers *
2519904Smcpowers * kmech is a umech with all pointers and structures in kernel address space.
2520904Smcpowers *
2521904Smcpowers * This routine calls the provider's entry point to copy a umech parameter
2522904Smcpowers * into kernel address space. Kernel memory is allocated by the provider.
2523904Smcpowers */
2524904Smcpowers static int
crypto_provider_copyin_mech_param(kcf_provider_desc_t * pd,crypto_mechanism_t * umech,crypto_mechanism_t * kmech,int mode,int * error)2525904Smcpowers crypto_provider_copyin_mech_param(kcf_provider_desc_t *pd,
2526904Smcpowers crypto_mechanism_t *umech, crypto_mechanism_t *kmech, int mode, int *error)
2527904Smcpowers {
2528904Smcpowers crypto_mech_type_t provider_mech_type;
2529904Smcpowers int rv;
2530904Smcpowers
2531904Smcpowers /* get the provider's mech number */
25323708Skrishna provider_mech_type = KCF_TO_PROV_MECHNUM(pd, umech->cm_type);
2533904Smcpowers
2534904Smcpowers kmech->cm_param = NULL;
2535904Smcpowers kmech->cm_param_len = 0;
2536904Smcpowers kmech->cm_type = provider_mech_type;
2537904Smcpowers rv = KCF_PROV_COPYIN_MECH(pd, umech, kmech, error, mode);
2538904Smcpowers kmech->cm_type = umech->cm_type;
2539904Smcpowers
2540904Smcpowers return (rv);
2541904Smcpowers }
2542904Smcpowers
2543904Smcpowers /*
2544904Smcpowers * umech is a mechanism structure that has been copied from user address
2545904Smcpowers * space into kernel address space. Only one copyin has been done.
2546904Smcpowers * The mechanism parameter, if non-null, still points to user address space.
2547904Smcpowers * If the mechanism parameter contains pointers, they are pointers into
2548904Smcpowers * user address space.
2549904Smcpowers *
2550904Smcpowers * kmech is a umech with all pointers and structures in kernel address space.
2551904Smcpowers *
2552904Smcpowers * This routine calls the provider's entry point to copy a kmech parameter
2553904Smcpowers * into user address space using umech as a template containing
2554904Smcpowers * user address pointers.
2555904Smcpowers */
2556904Smcpowers static int
crypto_provider_copyout_mech_param(kcf_provider_desc_t * pd,crypto_mechanism_t * kmech,crypto_mechanism_t * umech,int mode,int * error)2557904Smcpowers crypto_provider_copyout_mech_param(kcf_provider_desc_t *pd,
2558904Smcpowers crypto_mechanism_t *kmech, crypto_mechanism_t *umech, int mode, int *error)
2559904Smcpowers {
2560904Smcpowers crypto_mech_type_t provider_mech_type;
2561904Smcpowers int rv;
2562904Smcpowers
2563904Smcpowers /* get the provider's mech number */
25643708Skrishna provider_mech_type = KCF_TO_PROV_MECHNUM(pd, umech->cm_type);
2565904Smcpowers
2566904Smcpowers kmech->cm_type = provider_mech_type;
2567904Smcpowers rv = KCF_PROV_COPYOUT_MECH(pd, kmech, umech, error, mode);
2568904Smcpowers kmech->cm_type = umech->cm_type;
2569904Smcpowers
2570904Smcpowers return (rv);
2571904Smcpowers }
2572904Smcpowers
2573904Smcpowers /*
2574904Smcpowers * Call the provider's entry point to free kernel memory that has been
2575904Smcpowers * allocated for the mechanism's parameter.
2576904Smcpowers */
2577904Smcpowers static void
crypto_free_mech(kcf_provider_desc_t * pd,boolean_t allocated_by_crypto_module,crypto_mechanism_t * mech)2578904Smcpowers crypto_free_mech(kcf_provider_desc_t *pd, boolean_t allocated_by_crypto_module,
2579904Smcpowers crypto_mechanism_t *mech)
2580904Smcpowers {
2581904Smcpowers crypto_mech_type_t provider_mech_type;
2582904Smcpowers
2583904Smcpowers if (allocated_by_crypto_module) {
2584904Smcpowers if (mech->cm_param != NULL)
2585904Smcpowers kmem_free(mech->cm_param, mech->cm_param_len);
2586904Smcpowers } else {
2587904Smcpowers /* get the provider's mech number */
25883708Skrishna provider_mech_type = KCF_TO_PROV_MECHNUM(pd, mech->cm_type);
2589904Smcpowers
2590904Smcpowers if (mech->cm_param != NULL && mech->cm_param_len != 0) {
2591904Smcpowers mech->cm_type = provider_mech_type;
2592904Smcpowers (void) KCF_PROV_FREE_MECH(pd, mech);
2593904Smcpowers }
2594904Smcpowers }
2595904Smcpowers }
2596904Smcpowers
2597904Smcpowers /*
25980Sstevel@tonic-gate * ASSUMPTION: crypto_encrypt_init and crypto_decrypt_init
25990Sstevel@tonic-gate * structures are identical except for field names.
26000Sstevel@tonic-gate */
26010Sstevel@tonic-gate static int
cipher_init(dev_t dev,caddr_t arg,int mode,int (* init)(crypto_provider_t,crypto_session_id_t,crypto_mechanism_t *,crypto_key_t *,crypto_ctx_template_t,crypto_context_t *,crypto_call_req_t *))2602904Smcpowers cipher_init(dev_t dev, caddr_t arg, int mode, int (*init)(crypto_provider_t,
26030Sstevel@tonic-gate crypto_session_id_t, crypto_mechanism_t *, crypto_key_t *,
26040Sstevel@tonic-gate crypto_ctx_template_t, crypto_context_t *, crypto_call_req_t *))
26050Sstevel@tonic-gate {
26060Sstevel@tonic-gate STRUCT_DECL(crypto_encrypt_init, encrypt_init);
2607904Smcpowers kcf_provider_desc_t *real_provider = NULL;
26080Sstevel@tonic-gate crypto_session_id_t session_id;
26090Sstevel@tonic-gate crypto_mechanism_t mech;
26100Sstevel@tonic-gate crypto_key_t key;
26110Sstevel@tonic-gate crypto_minor_t *cm;
26126424Skrishna crypto_session_data_t *sp = NULL;
26130Sstevel@tonic-gate crypto_context_t cc;
26140Sstevel@tonic-gate crypto_ctx_t **ctxpp;
26150Sstevel@tonic-gate size_t mech_rctl_bytes = 0;
26166424Skrishna boolean_t mech_rctl_chk = B_FALSE;
26170Sstevel@tonic-gate size_t key_rctl_bytes = 0;
26186424Skrishna boolean_t key_rctl_chk = B_FALSE;
26190Sstevel@tonic-gate int error = 0;
26200Sstevel@tonic-gate int rv;
2621904Smcpowers boolean_t allocated_by_crypto_module = B_FALSE;
26221808Smcpowers crypto_func_group_t fg;
26230Sstevel@tonic-gate
26240Sstevel@tonic-gate STRUCT_INIT(encrypt_init, mode);
26250Sstevel@tonic-gate
26260Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
26270Sstevel@tonic-gate cmn_err(CE_WARN, "cipher_init: failed holding minor");
26280Sstevel@tonic-gate return (ENXIO);
26290Sstevel@tonic-gate }
26300Sstevel@tonic-gate
26310Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(encrypt_init),
26320Sstevel@tonic-gate STRUCT_SIZE(encrypt_init)) != 0) {
26330Sstevel@tonic-gate crypto_release_minor(cm);
26340Sstevel@tonic-gate return (EFAULT);
26350Sstevel@tonic-gate }
26360Sstevel@tonic-gate
26370Sstevel@tonic-gate mech.cm_param = NULL;
26380Sstevel@tonic-gate bzero(&key, sizeof (crypto_key_t));
26390Sstevel@tonic-gate
26400Sstevel@tonic-gate session_id = STRUCT_FGET(encrypt_init, ei_session);
26410Sstevel@tonic-gate
26420Sstevel@tonic-gate if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
26436424Skrishna goto out;
26440Sstevel@tonic-gate }
26450Sstevel@tonic-gate
2646904Smcpowers bcopy(STRUCT_FADDR(encrypt_init, ei_mech), &mech.cm_type,
2647904Smcpowers sizeof (crypto_mech_type_t));
26480Sstevel@tonic-gate
26491808Smcpowers if (init == crypto_encrypt_init_prov) {
26501808Smcpowers fg = CRYPTO_FG_ENCRYPT;
26511808Smcpowers } else {
26521808Smcpowers fg = CRYPTO_FG_DECRYPT;
26531808Smcpowers }
26540Sstevel@tonic-gate
265510444SVladimir.Kotal@Sun.COM /* We need the key length for provider selection so copy it in now. */
265610444SVladimir.Kotal@Sun.COM if (!copyin_key(mode, sp, STRUCT_FADDR(encrypt_init, ei_key), &key,
265710444SVladimir.Kotal@Sun.COM &key_rctl_bytes, &key_rctl_chk, &rv, &error)) {
265810444SVladimir.Kotal@Sun.COM goto out;
265910444SVladimir.Kotal@Sun.COM }
266010444SVladimir.Kotal@Sun.COM
266110444SVladimir.Kotal@Sun.COM if ((rv = kcf_get_hardware_provider(mech.cm_type, &key,
2662*12304SValerie.Fenwick@Oracle.COM CRYPTO_MECH_INVALID, NULL, sp->sd_provider, &real_provider, fg))
26631808Smcpowers != CRYPTO_SUCCESS) {
26640Sstevel@tonic-gate goto out;
26650Sstevel@tonic-gate }
26660Sstevel@tonic-gate
2667904Smcpowers rv = crypto_provider_copyin_mech_param(real_provider,
2668904Smcpowers STRUCT_FADDR(encrypt_init, ei_mech), &mech, mode, &error);
2669904Smcpowers
2670904Smcpowers if (rv == CRYPTO_NOT_SUPPORTED) {
2671904Smcpowers allocated_by_crypto_module = B_TRUE;
26726424Skrishna if (!copyin_mech(mode, sp, STRUCT_FADDR(encrypt_init, ei_mech),
26736424Skrishna &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) {
2674904Smcpowers goto out;
2675904Smcpowers }
2676904Smcpowers } else {
2677904Smcpowers if (rv != CRYPTO_SUCCESS)
2678904Smcpowers goto out;
2679904Smcpowers }
2680904Smcpowers
26810Sstevel@tonic-gate rv = (init)(real_provider, sp->sd_provider_session->ps_session,
26820Sstevel@tonic-gate &mech, &key, NULL, &cc, NULL);
26830Sstevel@tonic-gate
26840Sstevel@tonic-gate /*
26850Sstevel@tonic-gate * Check if a context already exists. If so, it means it is being
26860Sstevel@tonic-gate * abandoned. So, cancel it to avoid leaking it.
26870Sstevel@tonic-gate */
26880Sstevel@tonic-gate ctxpp = (init == crypto_encrypt_init_prov) ?
26890Sstevel@tonic-gate &sp->sd_encr_ctx : &sp->sd_decr_ctx;
26900Sstevel@tonic-gate
26910Sstevel@tonic-gate if (*ctxpp != NULL)
26920Sstevel@tonic-gate CRYPTO_CANCEL_CTX(ctxpp);
26930Sstevel@tonic-gate *ctxpp = (rv == CRYPTO_SUCCESS) ? cc : NULL;
2694904Smcpowers
26950Sstevel@tonic-gate out:
26966424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
26976424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk);
26980Sstevel@tonic-gate CRYPTO_SESSION_RELE(sp);
26990Sstevel@tonic-gate crypto_release_minor(cm);
27000Sstevel@tonic-gate
2701904Smcpowers if (real_provider != NULL) {
2702904Smcpowers crypto_free_mech(real_provider,
2703904Smcpowers allocated_by_crypto_module, &mech);
2704904Smcpowers KCF_PROV_REFRELE(real_provider);
2705904Smcpowers }
27060Sstevel@tonic-gate
27070Sstevel@tonic-gate free_crypto_key(&key);
27080Sstevel@tonic-gate
27090Sstevel@tonic-gate if (error != 0)
2710904Smcpowers /* XXX free context */
27110Sstevel@tonic-gate return (error);
27120Sstevel@tonic-gate
27130Sstevel@tonic-gate STRUCT_FSET(encrypt_init, ei_return_value, rv);
27140Sstevel@tonic-gate if (copyout(STRUCT_BUF(encrypt_init), arg,
27150Sstevel@tonic-gate STRUCT_SIZE(encrypt_init)) != 0) {
2716904Smcpowers /* XXX free context */
27170Sstevel@tonic-gate return (EFAULT);
27180Sstevel@tonic-gate }
27190Sstevel@tonic-gate return (0);
27200Sstevel@tonic-gate }
27210Sstevel@tonic-gate
27220Sstevel@tonic-gate /* ARGSUSED */
27230Sstevel@tonic-gate static int
encrypt(dev_t dev,caddr_t arg,int mode,int * rval)27240Sstevel@tonic-gate encrypt(dev_t dev, caddr_t arg, int mode, int *rval)
27250Sstevel@tonic-gate {
27260Sstevel@tonic-gate return (cipher(dev, arg, mode, crypto_encrypt_single));
27270Sstevel@tonic-gate }
27280Sstevel@tonic-gate
27290Sstevel@tonic-gate /* ARGSUSED */
27300Sstevel@tonic-gate static int
decrypt(dev_t dev,caddr_t arg,int mode,int * rval)27310Sstevel@tonic-gate decrypt(dev_t dev, caddr_t arg, int mode, int *rval)
27320Sstevel@tonic-gate {
27330Sstevel@tonic-gate return (cipher(dev, arg, mode, crypto_decrypt_single));
27340Sstevel@tonic-gate }
27350Sstevel@tonic-gate
27360Sstevel@tonic-gate /*
27370Sstevel@tonic-gate * ASSUMPTION: crypto_encrypt and crypto_decrypt structures
27380Sstevel@tonic-gate * are identical except for field names.
27390Sstevel@tonic-gate */
27400Sstevel@tonic-gate static int
cipher(dev_t dev,caddr_t arg,int mode,int (* single)(crypto_context_t,crypto_data_t *,crypto_data_t *,crypto_call_req_t *))27410Sstevel@tonic-gate cipher(dev_t dev, caddr_t arg, int mode,
27420Sstevel@tonic-gate int (*single)(crypto_context_t, crypto_data_t *, crypto_data_t *,
27430Sstevel@tonic-gate crypto_call_req_t *))
27440Sstevel@tonic-gate {
27450Sstevel@tonic-gate STRUCT_DECL(crypto_encrypt, encrypt);
27460Sstevel@tonic-gate crypto_session_id_t session_id;
27470Sstevel@tonic-gate crypto_minor_t *cm;
27486424Skrishna crypto_session_data_t *sp = NULL;
27490Sstevel@tonic-gate crypto_ctx_t **ctxpp;
27500Sstevel@tonic-gate crypto_data_t data, encr;
27510Sstevel@tonic-gate size_t datalen, encrlen, need = 0;
27524632Smcpowers boolean_t do_inplace;
27530Sstevel@tonic-gate char *encrbuf;
27540Sstevel@tonic-gate int error = 0;
27550Sstevel@tonic-gate int rv;
27566424Skrishna boolean_t rctl_chk = B_FALSE;
27570Sstevel@tonic-gate
27580Sstevel@tonic-gate STRUCT_INIT(encrypt, mode);
27590Sstevel@tonic-gate
27600Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
27610Sstevel@tonic-gate cmn_err(CE_WARN, "cipher: failed holding minor");
27620Sstevel@tonic-gate return (ENXIO);
27630Sstevel@tonic-gate }
27640Sstevel@tonic-gate
27650Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(encrypt), STRUCT_SIZE(encrypt)) != 0) {
27660Sstevel@tonic-gate crypto_release_minor(cm);
27670Sstevel@tonic-gate return (EFAULT);
27680Sstevel@tonic-gate }
27690Sstevel@tonic-gate
27700Sstevel@tonic-gate data.cd_raw.iov_base = NULL;
27710Sstevel@tonic-gate encr.cd_raw.iov_base = NULL;
27720Sstevel@tonic-gate
27730Sstevel@tonic-gate datalen = STRUCT_FGET(encrypt, ce_datalen);
27740Sstevel@tonic-gate encrlen = STRUCT_FGET(encrypt, ce_encrlen);
27750Sstevel@tonic-gate
27760Sstevel@tonic-gate /*
27770Sstevel@tonic-gate * Don't allocate output buffer unless both buffer pointer and
27780Sstevel@tonic-gate * buffer length are not NULL or 0 (length).
27790Sstevel@tonic-gate */
27800Sstevel@tonic-gate encrbuf = STRUCT_FGETP(encrypt, ce_encrbuf);
27810Sstevel@tonic-gate if (encrbuf == NULL || encrlen == 0) {
27820Sstevel@tonic-gate encrlen = 0;
27830Sstevel@tonic-gate }
27840Sstevel@tonic-gate
27850Sstevel@tonic-gate if (datalen > crypto_max_buffer_len ||
27860Sstevel@tonic-gate encrlen > crypto_max_buffer_len) {
27870Sstevel@tonic-gate cmn_err(CE_NOTE, "cipher: buffer greater than %ld bytes, "
27880Sstevel@tonic-gate "pid = %d", crypto_max_buffer_len, curproc->p_pid);
27890Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD;
27900Sstevel@tonic-gate goto release_minor;
27910Sstevel@tonic-gate }
27920Sstevel@tonic-gate
27936424Skrishna session_id = STRUCT_FGET(encrypt, ce_session);
27946424Skrishna
27956424Skrishna if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
27966424Skrishna goto release_minor;
27976424Skrishna }
27986867Skrishna
27996867Skrishna do_inplace = (STRUCT_FGET(encrypt, ce_flags) &
28006867Skrishna CRYPTO_INPLACE_OPERATION) != 0;
28016867Skrishna need = do_inplace ? datalen : datalen + encrlen;
28026867Skrishna
28036424Skrishna if ((rv = CRYPTO_BUFFER_CHECK(sp, need, rctl_chk)) !=
28046424Skrishna CRYPTO_SUCCESS) {
28050Sstevel@tonic-gate need = 0;
28060Sstevel@tonic-gate goto release_minor;
28070Sstevel@tonic-gate }
28080Sstevel@tonic-gate
28090Sstevel@tonic-gate INIT_RAW_CRYPTO_DATA(data, datalen);
28100Sstevel@tonic-gate data.cd_miscdata = NULL;
28110Sstevel@tonic-gate
28120Sstevel@tonic-gate if (datalen != 0 && copyin(STRUCT_FGETP(encrypt, ce_databuf),
28130Sstevel@tonic-gate data.cd_raw.iov_base, datalen) != 0) {
28140Sstevel@tonic-gate error = EFAULT;
28150Sstevel@tonic-gate goto release_minor;
28160Sstevel@tonic-gate }
28170Sstevel@tonic-gate
28184632Smcpowers if (do_inplace) {
28194632Smcpowers /* set out = in for in-place */
28204632Smcpowers encr = data;
28214632Smcpowers } else {
28224632Smcpowers INIT_RAW_CRYPTO_DATA(encr, encrlen);
28234632Smcpowers }
28240Sstevel@tonic-gate
28250Sstevel@tonic-gate ctxpp = (single == crypto_encrypt_single) ?
28260Sstevel@tonic-gate &sp->sd_encr_ctx : &sp->sd_decr_ctx;
28270Sstevel@tonic-gate
28284632Smcpowers if (do_inplace)
282911030Sopensolaris@drydog.com /* specify in-place buffers with output = NULL */
28304632Smcpowers rv = (single)(*ctxpp, &encr, NULL, NULL);
28314632Smcpowers else
28324632Smcpowers rv = (single)(*ctxpp, &data, &encr, NULL);
283311030Sopensolaris@drydog.com
28340Sstevel@tonic-gate if (KCF_CONTEXT_DONE(rv))
28350Sstevel@tonic-gate *ctxpp = NULL;
28360Sstevel@tonic-gate
28370Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) {
28380Sstevel@tonic-gate ASSERT(encr.cd_length <= encrlen);
28390Sstevel@tonic-gate if (encr.cd_length != 0 && copyout(encr.cd_raw.iov_base,
28400Sstevel@tonic-gate encrbuf, encr.cd_length) != 0) {
28410Sstevel@tonic-gate error = EFAULT;
28420Sstevel@tonic-gate goto release_minor;
28430Sstevel@tonic-gate }
284411413Sopensolaris@drydog.com STRUCT_FSET(encrypt, ce_encrlen,
284511413Sopensolaris@drydog.com (ulong_t)encr.cd_length);
28460Sstevel@tonic-gate }
28470Sstevel@tonic-gate
28480Sstevel@tonic-gate if (rv == CRYPTO_BUFFER_TOO_SMALL) {
28490Sstevel@tonic-gate /*
28500Sstevel@tonic-gate * The providers return CRYPTO_BUFFER_TOO_SMALL even for case 1
28510Sstevel@tonic-gate * of section 11.2 of the pkcs11 spec. We catch it here and
28520Sstevel@tonic-gate * provide the correct pkcs11 return value.
28530Sstevel@tonic-gate */
28540Sstevel@tonic-gate if (STRUCT_FGETP(encrypt, ce_encrbuf) == NULL)
28550Sstevel@tonic-gate rv = CRYPTO_SUCCESS;
285611413Sopensolaris@drydog.com STRUCT_FSET(encrypt, ce_encrlen,
285711413Sopensolaris@drydog.com (ulong_t)encr.cd_length);
28580Sstevel@tonic-gate }
28590Sstevel@tonic-gate
28600Sstevel@tonic-gate release_minor:
28616424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
28626424Skrishna CRYPTO_SESSION_RELE(sp);
28630Sstevel@tonic-gate crypto_release_minor(cm);
28640Sstevel@tonic-gate
28650Sstevel@tonic-gate if (data.cd_raw.iov_base != NULL)
28660Sstevel@tonic-gate kmem_free(data.cd_raw.iov_base, datalen);
28670Sstevel@tonic-gate
28684632Smcpowers if (!do_inplace && encr.cd_raw.iov_base != NULL)
28690Sstevel@tonic-gate kmem_free(encr.cd_raw.iov_base, encrlen);
28700Sstevel@tonic-gate
28710Sstevel@tonic-gate if (error != 0)
28720Sstevel@tonic-gate return (error);
28730Sstevel@tonic-gate
28740Sstevel@tonic-gate STRUCT_FSET(encrypt, ce_return_value, rv);
28750Sstevel@tonic-gate if (copyout(STRUCT_BUF(encrypt), arg, STRUCT_SIZE(encrypt)) != 0) {
28760Sstevel@tonic-gate return (EFAULT);
28770Sstevel@tonic-gate }
28780Sstevel@tonic-gate return (0);
28790Sstevel@tonic-gate }
28800Sstevel@tonic-gate
28810Sstevel@tonic-gate /* ARGSUSED */
28820Sstevel@tonic-gate static int
encrypt_update(dev_t dev,caddr_t arg,int mode,int * rval)28830Sstevel@tonic-gate encrypt_update(dev_t dev, caddr_t arg, int mode, int *rval)
28840Sstevel@tonic-gate {
28850Sstevel@tonic-gate return (cipher_update(dev, arg, mode, crypto_encrypt_update));
28860Sstevel@tonic-gate }
28870Sstevel@tonic-gate
28880Sstevel@tonic-gate /* ARGSUSED */
28890Sstevel@tonic-gate static int
decrypt_update(dev_t dev,caddr_t arg,int mode,int * rval)28900Sstevel@tonic-gate decrypt_update(dev_t dev, caddr_t arg, int mode, int *rval)
28910Sstevel@tonic-gate {
28920Sstevel@tonic-gate return (cipher_update(dev, arg, mode, crypto_decrypt_update));
28930Sstevel@tonic-gate }
28940Sstevel@tonic-gate
28950Sstevel@tonic-gate /*
28960Sstevel@tonic-gate * ASSUMPTION: crypto_encrypt_update and crypto_decrypt_update
28970Sstevel@tonic-gate * structures are identical except for field names.
28980Sstevel@tonic-gate */
28990Sstevel@tonic-gate static int
cipher_update(dev_t dev,caddr_t arg,int mode,int (* update)(crypto_context_t,crypto_data_t *,crypto_data_t *,crypto_call_req_t *))29000Sstevel@tonic-gate cipher_update(dev_t dev, caddr_t arg, int mode,
29010Sstevel@tonic-gate int (*update)(crypto_context_t, crypto_data_t *, crypto_data_t *,
29020Sstevel@tonic-gate crypto_call_req_t *))
29030Sstevel@tonic-gate {
29040Sstevel@tonic-gate STRUCT_DECL(crypto_encrypt_update, encrypt_update);
29050Sstevel@tonic-gate crypto_session_id_t session_id;
29060Sstevel@tonic-gate crypto_minor_t *cm;
29076424Skrishna crypto_session_data_t *sp = NULL;
29080Sstevel@tonic-gate crypto_ctx_t **ctxpp;
29090Sstevel@tonic-gate crypto_data_t data, encr;
29100Sstevel@tonic-gate size_t datalen, encrlen, need = 0;
291111030Sopensolaris@drydog.com boolean_t do_inplace;
29120Sstevel@tonic-gate char *encrbuf;
29130Sstevel@tonic-gate int error = 0;
29140Sstevel@tonic-gate int rv;
29156424Skrishna boolean_t rctl_chk = B_FALSE;
29160Sstevel@tonic-gate
29170Sstevel@tonic-gate STRUCT_INIT(encrypt_update, mode);
29180Sstevel@tonic-gate
29190Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
29200Sstevel@tonic-gate cmn_err(CE_WARN, "cipher_update: failed holding minor");
29210Sstevel@tonic-gate return (ENXIO);
29220Sstevel@tonic-gate }
29230Sstevel@tonic-gate
29240Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(encrypt_update),
29250Sstevel@tonic-gate STRUCT_SIZE(encrypt_update)) != 0) {
29260Sstevel@tonic-gate crypto_release_minor(cm);
29270Sstevel@tonic-gate return (EFAULT);
29280Sstevel@tonic-gate }
29290Sstevel@tonic-gate
29300Sstevel@tonic-gate data.cd_raw.iov_base = NULL;
29310Sstevel@tonic-gate encr.cd_raw.iov_base = NULL;
29320Sstevel@tonic-gate
29330Sstevel@tonic-gate datalen = STRUCT_FGET(encrypt_update, eu_datalen);
29340Sstevel@tonic-gate encrlen = STRUCT_FGET(encrypt_update, eu_encrlen);
29350Sstevel@tonic-gate
29360Sstevel@tonic-gate /*
29370Sstevel@tonic-gate * Don't allocate output buffer unless both buffer pointer and
29380Sstevel@tonic-gate * buffer length are not NULL or 0 (length).
29390Sstevel@tonic-gate */
29400Sstevel@tonic-gate encrbuf = STRUCT_FGETP(encrypt_update, eu_encrbuf);
29410Sstevel@tonic-gate if (encrbuf == NULL || encrlen == 0) {
29420Sstevel@tonic-gate encrlen = 0;
29430Sstevel@tonic-gate }
29440Sstevel@tonic-gate
29450Sstevel@tonic-gate if (datalen > crypto_max_buffer_len ||
29460Sstevel@tonic-gate encrlen > crypto_max_buffer_len) {
29470Sstevel@tonic-gate cmn_err(CE_NOTE, "cipher_update: buffer greater than %ld "
29480Sstevel@tonic-gate "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
29490Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD;
29506424Skrishna goto out;
29516424Skrishna }
29526424Skrishna
29536424Skrishna session_id = STRUCT_FGET(encrypt_update, eu_session);
29546424Skrishna
29556424Skrishna if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
29566424Skrishna goto out;
29570Sstevel@tonic-gate }
29580Sstevel@tonic-gate
295911030Sopensolaris@drydog.com do_inplace = (STRUCT_FGET(encrypt_update, eu_flags) &
296011030Sopensolaris@drydog.com CRYPTO_INPLACE_OPERATION) != 0;
296111030Sopensolaris@drydog.com need = do_inplace ? datalen : datalen + encrlen;
29626424Skrishna
29636424Skrishna if ((rv = CRYPTO_BUFFER_CHECK(sp, need, rctl_chk)) !=
29646424Skrishna CRYPTO_SUCCESS) {
29650Sstevel@tonic-gate need = 0;
29666424Skrishna goto out;
29670Sstevel@tonic-gate }
29680Sstevel@tonic-gate
29690Sstevel@tonic-gate INIT_RAW_CRYPTO_DATA(data, datalen);
29700Sstevel@tonic-gate data.cd_miscdata = NULL;
29710Sstevel@tonic-gate
29720Sstevel@tonic-gate if (datalen != 0 && copyin(STRUCT_FGETP(encrypt_update, eu_databuf),
29730Sstevel@tonic-gate data.cd_raw.iov_base, datalen) != 0) {
29740Sstevel@tonic-gate error = EFAULT;
29756424Skrishna goto out;
29760Sstevel@tonic-gate }
29770Sstevel@tonic-gate
297811030Sopensolaris@drydog.com if (do_inplace) {
297911030Sopensolaris@drydog.com /* specify in-place buffers with output = input */
298011030Sopensolaris@drydog.com encr = data;
298111030Sopensolaris@drydog.com } else {
298211030Sopensolaris@drydog.com INIT_RAW_CRYPTO_DATA(encr, encrlen);
298311030Sopensolaris@drydog.com }
29840Sstevel@tonic-gate
29850Sstevel@tonic-gate ctxpp = (update == crypto_encrypt_update) ?
29860Sstevel@tonic-gate &sp->sd_encr_ctx : &sp->sd_decr_ctx;
29870Sstevel@tonic-gate
298811030Sopensolaris@drydog.com if (do_inplace)
298911030Sopensolaris@drydog.com /* specify in-place buffers with output = NULL */
299011030Sopensolaris@drydog.com rv = (update)(*ctxpp, &encr, NULL, NULL);
299111030Sopensolaris@drydog.com else
299211030Sopensolaris@drydog.com rv = (update)(*ctxpp, &data, &encr, NULL);
29930Sstevel@tonic-gate
29940Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS || rv == CRYPTO_BUFFER_TOO_SMALL) {
29950Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) {
29960Sstevel@tonic-gate ASSERT(encr.cd_length <= encrlen);
29970Sstevel@tonic-gate if (encr.cd_length != 0 && copyout(encr.cd_raw.iov_base,
29980Sstevel@tonic-gate encrbuf, encr.cd_length) != 0) {
29990Sstevel@tonic-gate error = EFAULT;
30000Sstevel@tonic-gate goto out;
30010Sstevel@tonic-gate }
30020Sstevel@tonic-gate } else {
30030Sstevel@tonic-gate /*
30040Sstevel@tonic-gate * The providers return CRYPTO_BUFFER_TOO_SMALL even
30050Sstevel@tonic-gate * for case 1 of section 11.2 of the pkcs11 spec.
30060Sstevel@tonic-gate * We catch it here and provide the correct pkcs11
30070Sstevel@tonic-gate * return value.
30080Sstevel@tonic-gate */
30090Sstevel@tonic-gate if (STRUCT_FGETP(encrypt_update, eu_encrbuf) == NULL)
30100Sstevel@tonic-gate rv = CRYPTO_SUCCESS;
30110Sstevel@tonic-gate }
301211413Sopensolaris@drydog.com STRUCT_FSET(encrypt_update, eu_encrlen,
301311413Sopensolaris@drydog.com (ulong_t)encr.cd_length);
30140Sstevel@tonic-gate } else {
30150Sstevel@tonic-gate CRYPTO_CANCEL_CTX(ctxpp);
30160Sstevel@tonic-gate }
30170Sstevel@tonic-gate out:
30186424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
30190Sstevel@tonic-gate CRYPTO_SESSION_RELE(sp);
30200Sstevel@tonic-gate crypto_release_minor(cm);
30210Sstevel@tonic-gate
30220Sstevel@tonic-gate if (data.cd_raw.iov_base != NULL)
30230Sstevel@tonic-gate kmem_free(data.cd_raw.iov_base, datalen);
30240Sstevel@tonic-gate
302511030Sopensolaris@drydog.com if (!do_inplace && (encr.cd_raw.iov_base != NULL))
30260Sstevel@tonic-gate kmem_free(encr.cd_raw.iov_base, encrlen);
30270Sstevel@tonic-gate
30280Sstevel@tonic-gate if (error != 0)
30290Sstevel@tonic-gate return (error);
30300Sstevel@tonic-gate
30310Sstevel@tonic-gate STRUCT_FSET(encrypt_update, eu_return_value, rv);
30320Sstevel@tonic-gate if (copyout(STRUCT_BUF(encrypt_update), arg,
30330Sstevel@tonic-gate STRUCT_SIZE(encrypt_update)) != 0) {
30340Sstevel@tonic-gate return (EFAULT);
30350Sstevel@tonic-gate }
30360Sstevel@tonic-gate return (0);
30370Sstevel@tonic-gate }
30380Sstevel@tonic-gate
30390Sstevel@tonic-gate /* ARGSUSED */
30400Sstevel@tonic-gate static int
encrypt_final(dev_t dev,caddr_t arg,int mode,int * rval)30410Sstevel@tonic-gate encrypt_final(dev_t dev, caddr_t arg, int mode, int *rval)
30420Sstevel@tonic-gate {
30430Sstevel@tonic-gate return (common_final(dev, arg, mode, crypto_encrypt_final));
30440Sstevel@tonic-gate }
30450Sstevel@tonic-gate
30460Sstevel@tonic-gate /* ARGSUSED */
30470Sstevel@tonic-gate static int
decrypt_final(dev_t dev,caddr_t arg,int mode,int * rval)30480Sstevel@tonic-gate decrypt_final(dev_t dev, caddr_t arg, int mode, int *rval)
30490Sstevel@tonic-gate {
30500Sstevel@tonic-gate return (common_final(dev, arg, mode, crypto_decrypt_final));
30510Sstevel@tonic-gate }
30520Sstevel@tonic-gate
30530Sstevel@tonic-gate /*
30540Sstevel@tonic-gate * ASSUMPTION: crypto_encrypt_final, crypto_decrypt_final, crypto_sign_final,
30550Sstevel@tonic-gate * and crypto_digest_final structures are identical except for field names.
30560Sstevel@tonic-gate */
30570Sstevel@tonic-gate static int
common_final(dev_t dev,caddr_t arg,int mode,int (* final)(crypto_context_t,crypto_data_t *,crypto_call_req_t *))30580Sstevel@tonic-gate common_final(dev_t dev, caddr_t arg, int mode,
30590Sstevel@tonic-gate int (*final)(crypto_context_t, crypto_data_t *, crypto_call_req_t *))
30600Sstevel@tonic-gate {
30610Sstevel@tonic-gate STRUCT_DECL(crypto_encrypt_final, encrypt_final);
30620Sstevel@tonic-gate crypto_session_id_t session_id;
30630Sstevel@tonic-gate crypto_minor_t *cm;
30646424Skrishna crypto_session_data_t *sp = NULL;
30650Sstevel@tonic-gate crypto_ctx_t **ctxpp;
30660Sstevel@tonic-gate crypto_data_t encr;
30670Sstevel@tonic-gate size_t encrlen, need = 0;
30680Sstevel@tonic-gate char *encrbuf;
30690Sstevel@tonic-gate int error = 0;
30700Sstevel@tonic-gate int rv;
30716424Skrishna boolean_t rctl_chk = B_FALSE;
30720Sstevel@tonic-gate
30730Sstevel@tonic-gate STRUCT_INIT(encrypt_final, mode);
30740Sstevel@tonic-gate
30750Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
30760Sstevel@tonic-gate cmn_err(CE_WARN, "common_final: failed holding minor");
30770Sstevel@tonic-gate return (ENXIO);
30780Sstevel@tonic-gate }
30790Sstevel@tonic-gate
30800Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(encrypt_final),
30810Sstevel@tonic-gate STRUCT_SIZE(encrypt_final)) != 0) {
30820Sstevel@tonic-gate crypto_release_minor(cm);
30830Sstevel@tonic-gate return (EFAULT);
30840Sstevel@tonic-gate }
30850Sstevel@tonic-gate
30860Sstevel@tonic-gate encr.cd_format = CRYPTO_DATA_RAW;
30870Sstevel@tonic-gate encr.cd_raw.iov_base = NULL;
30880Sstevel@tonic-gate
30890Sstevel@tonic-gate encrlen = STRUCT_FGET(encrypt_final, ef_encrlen);
30900Sstevel@tonic-gate
30910Sstevel@tonic-gate /*
30920Sstevel@tonic-gate * Don't allocate output buffer unless both buffer pointer and
30930Sstevel@tonic-gate * buffer length are not NULL or 0 (length).
30940Sstevel@tonic-gate */
30950Sstevel@tonic-gate encrbuf = STRUCT_FGETP(encrypt_final, ef_encrbuf);
30960Sstevel@tonic-gate if (encrbuf == NULL || encrlen == 0) {
30970Sstevel@tonic-gate encrlen = 0;
30980Sstevel@tonic-gate }
30990Sstevel@tonic-gate
31000Sstevel@tonic-gate if (encrlen > crypto_max_buffer_len) {
31010Sstevel@tonic-gate cmn_err(CE_NOTE, "common_final: buffer greater than %ld "
31020Sstevel@tonic-gate "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
31030Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD;
31040Sstevel@tonic-gate goto release_minor;
31050Sstevel@tonic-gate }
31060Sstevel@tonic-gate
31076424Skrishna session_id = STRUCT_FGET(encrypt_final, ef_session);
31086424Skrishna
31096424Skrishna if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
31106424Skrishna goto release_minor;
31116424Skrishna }
31126424Skrishna
31136424Skrishna if ((rv = CRYPTO_BUFFER_CHECK(sp, encrlen, rctl_chk)) !=
31146424Skrishna CRYPTO_SUCCESS) {
31150Sstevel@tonic-gate goto release_minor;
31160Sstevel@tonic-gate }
31170Sstevel@tonic-gate need = encrlen;
31180Sstevel@tonic-gate encr.cd_raw.iov_base = kmem_alloc(encrlen, KM_SLEEP);
31190Sstevel@tonic-gate encr.cd_raw.iov_len = encrlen;
31200Sstevel@tonic-gate
31210Sstevel@tonic-gate encr.cd_offset = 0;
31220Sstevel@tonic-gate encr.cd_length = encrlen;
31230Sstevel@tonic-gate
31240Sstevel@tonic-gate ASSERT(final == crypto_encrypt_final ||
31250Sstevel@tonic-gate final == crypto_decrypt_final || final == crypto_sign_final ||
31260Sstevel@tonic-gate final == crypto_digest_final);
31270Sstevel@tonic-gate
31280Sstevel@tonic-gate if (final == crypto_encrypt_final) {
31290Sstevel@tonic-gate ctxpp = &sp->sd_encr_ctx;
31300Sstevel@tonic-gate } else if (final == crypto_decrypt_final) {
31310Sstevel@tonic-gate ctxpp = &sp->sd_decr_ctx;
31320Sstevel@tonic-gate } else if (final == crypto_sign_final) {
31330Sstevel@tonic-gate ctxpp = &sp->sd_sign_ctx;
31340Sstevel@tonic-gate } else {
31350Sstevel@tonic-gate ctxpp = &sp->sd_digest_ctx;
31360Sstevel@tonic-gate }
31370Sstevel@tonic-gate
31380Sstevel@tonic-gate rv = (final)(*ctxpp, &encr, NULL);
31390Sstevel@tonic-gate if (KCF_CONTEXT_DONE(rv))
31400Sstevel@tonic-gate *ctxpp = NULL;
31410Sstevel@tonic-gate
31420Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) {
31430Sstevel@tonic-gate ASSERT(encr.cd_length <= encrlen);
31440Sstevel@tonic-gate if (encr.cd_length != 0 && copyout(encr.cd_raw.iov_base,
31450Sstevel@tonic-gate encrbuf, encr.cd_length) != 0) {
31460Sstevel@tonic-gate error = EFAULT;
31470Sstevel@tonic-gate goto release_minor;
31480Sstevel@tonic-gate }
314911413Sopensolaris@drydog.com STRUCT_FSET(encrypt_final, ef_encrlen,
315011413Sopensolaris@drydog.com (ulong_t)encr.cd_length);
31510Sstevel@tonic-gate }
31520Sstevel@tonic-gate
31530Sstevel@tonic-gate if (rv == CRYPTO_BUFFER_TOO_SMALL) {
31540Sstevel@tonic-gate /*
31550Sstevel@tonic-gate * The providers return CRYPTO_BUFFER_TOO_SMALL even for case 1
31560Sstevel@tonic-gate * of section 11.2 of the pkcs11 spec. We catch it here and
31570Sstevel@tonic-gate * provide the correct pkcs11 return value.
31580Sstevel@tonic-gate */
31590Sstevel@tonic-gate if (STRUCT_FGETP(encrypt_final, ef_encrbuf) == NULL)
31600Sstevel@tonic-gate rv = CRYPTO_SUCCESS;
316111413Sopensolaris@drydog.com STRUCT_FSET(encrypt_final, ef_encrlen,
316211413Sopensolaris@drydog.com (ulong_t)encr.cd_length);
31630Sstevel@tonic-gate }
31640Sstevel@tonic-gate
31650Sstevel@tonic-gate release_minor:
31666424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
31676424Skrishna CRYPTO_SESSION_RELE(sp);
31680Sstevel@tonic-gate crypto_release_minor(cm);
31690Sstevel@tonic-gate
31700Sstevel@tonic-gate if (encr.cd_raw.iov_base != NULL)
31710Sstevel@tonic-gate kmem_free(encr.cd_raw.iov_base, encrlen);
31720Sstevel@tonic-gate
31730Sstevel@tonic-gate if (error != 0)
31740Sstevel@tonic-gate return (error);
31750Sstevel@tonic-gate
31760Sstevel@tonic-gate STRUCT_FSET(encrypt_final, ef_return_value, rv);
31770Sstevel@tonic-gate if (copyout(STRUCT_BUF(encrypt_final), arg,
31780Sstevel@tonic-gate STRUCT_SIZE(encrypt_final)) != 0) {
31790Sstevel@tonic-gate return (EFAULT);
31800Sstevel@tonic-gate }
31810Sstevel@tonic-gate return (0);
31820Sstevel@tonic-gate }
31830Sstevel@tonic-gate
31840Sstevel@tonic-gate /* ARGSUSED */
31850Sstevel@tonic-gate static int
digest_init(dev_t dev,caddr_t arg,int mode,int * rval)31860Sstevel@tonic-gate digest_init(dev_t dev, caddr_t arg, int mode, int *rval)
31870Sstevel@tonic-gate {
31880Sstevel@tonic-gate STRUCT_DECL(crypto_digest_init, digest_init);
3189904Smcpowers kcf_provider_desc_t *real_provider = NULL;
31900Sstevel@tonic-gate crypto_session_id_t session_id;
31910Sstevel@tonic-gate crypto_mechanism_t mech;
31920Sstevel@tonic-gate crypto_minor_t *cm;
31936424Skrishna crypto_session_data_t *sp = NULL;
31940Sstevel@tonic-gate crypto_context_t cc;
31950Sstevel@tonic-gate size_t rctl_bytes = 0;
31966424Skrishna boolean_t rctl_chk = B_FALSE;
31970Sstevel@tonic-gate int error = 0;
31980Sstevel@tonic-gate int rv;
31990Sstevel@tonic-gate
32000Sstevel@tonic-gate STRUCT_INIT(digest_init, mode);
32010Sstevel@tonic-gate
32020Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
32030Sstevel@tonic-gate cmn_err(CE_WARN, "digest_init: failed holding minor");
32040Sstevel@tonic-gate return (ENXIO);
32050Sstevel@tonic-gate }
32060Sstevel@tonic-gate
32070Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(digest_init),
32080Sstevel@tonic-gate STRUCT_SIZE(digest_init)) != 0) {
32090Sstevel@tonic-gate crypto_release_minor(cm);
32100Sstevel@tonic-gate return (EFAULT);
32110Sstevel@tonic-gate }
32120Sstevel@tonic-gate
32130Sstevel@tonic-gate mech.cm_param = NULL;
32140Sstevel@tonic-gate
32150Sstevel@tonic-gate session_id = STRUCT_FGET(digest_init, di_session);
32160Sstevel@tonic-gate
32170Sstevel@tonic-gate if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
32186424Skrishna goto out;
32196424Skrishna }
32206424Skrishna
32216424Skrishna if (!copyin_mech(mode, sp, STRUCT_FADDR(digest_init, di_mech), &mech,
32226424Skrishna &rctl_bytes, &rctl_chk, &rv, &error)) {
32230Sstevel@tonic-gate goto out;
32240Sstevel@tonic-gate }
32250Sstevel@tonic-gate
322610444SVladimir.Kotal@Sun.COM if ((rv = kcf_get_hardware_provider(mech.cm_type, NULL,
3227*12304SValerie.Fenwick@Oracle.COM CRYPTO_MECH_INVALID, NULL, sp->sd_provider, &real_provider,
3228*12304SValerie.Fenwick@Oracle.COM CRYPTO_FG_DIGEST)) != CRYPTO_SUCCESS) {
32290Sstevel@tonic-gate goto out;
32300Sstevel@tonic-gate }
32310Sstevel@tonic-gate
32320Sstevel@tonic-gate rv = crypto_digest_init_prov(real_provider,
32330Sstevel@tonic-gate sp->sd_provider_session->ps_session, &mech, &cc, NULL);
32340Sstevel@tonic-gate
32350Sstevel@tonic-gate /*
32360Sstevel@tonic-gate * Check if a context already exists. If so, it means it is being
32370Sstevel@tonic-gate * abandoned. So, cancel it to avoid leaking it.
32380Sstevel@tonic-gate */
32390Sstevel@tonic-gate if (sp->sd_digest_ctx != NULL)
32400Sstevel@tonic-gate CRYPTO_CANCEL_CTX(&sp->sd_digest_ctx);
32410Sstevel@tonic-gate sp->sd_digest_ctx = (rv == CRYPTO_SUCCESS) ? cc : NULL;
32420Sstevel@tonic-gate out:
32436424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk);
32440Sstevel@tonic-gate CRYPTO_SESSION_RELE(sp);
32450Sstevel@tonic-gate crypto_release_minor(cm);
32460Sstevel@tonic-gate
3247904Smcpowers if (real_provider != NULL)
3248904Smcpowers KCF_PROV_REFRELE(real_provider);
3249904Smcpowers
32500Sstevel@tonic-gate if (mech.cm_param != NULL)
32510Sstevel@tonic-gate kmem_free(mech.cm_param, mech.cm_param_len);
32520Sstevel@tonic-gate
32530Sstevel@tonic-gate if (error != 0)
32540Sstevel@tonic-gate return (error);
32550Sstevel@tonic-gate
32560Sstevel@tonic-gate STRUCT_FSET(digest_init, di_return_value, rv);
32570Sstevel@tonic-gate if (copyout(STRUCT_BUF(digest_init), arg,
32580Sstevel@tonic-gate STRUCT_SIZE(digest_init)) != 0) {
32590Sstevel@tonic-gate return (EFAULT);
32600Sstevel@tonic-gate }
32610Sstevel@tonic-gate return (0);
32620Sstevel@tonic-gate }
32630Sstevel@tonic-gate
32640Sstevel@tonic-gate /* ARGSUSED */
32650Sstevel@tonic-gate static int
digest_update(dev_t dev,caddr_t arg,int mode,int * rval)32660Sstevel@tonic-gate digest_update(dev_t dev, caddr_t arg, int mode, int *rval)
32670Sstevel@tonic-gate {
32680Sstevel@tonic-gate STRUCT_DECL(crypto_digest_update, digest_update);
32690Sstevel@tonic-gate crypto_session_id_t session_id;
32700Sstevel@tonic-gate crypto_minor_t *cm;
32716424Skrishna crypto_session_data_t *sp = NULL;
32720Sstevel@tonic-gate crypto_data_t data;
32730Sstevel@tonic-gate size_t datalen, need = 0;
32740Sstevel@tonic-gate int error = 0;
32750Sstevel@tonic-gate int rv;
32766424Skrishna boolean_t rctl_chk = B_FALSE;
32770Sstevel@tonic-gate
32780Sstevel@tonic-gate STRUCT_INIT(digest_update, mode);
32790Sstevel@tonic-gate
32800Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
32810Sstevel@tonic-gate cmn_err(CE_WARN, "digest_update: failed holding minor");
32820Sstevel@tonic-gate return (ENXIO);
32830Sstevel@tonic-gate }
32840Sstevel@tonic-gate
32850Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(digest_update),
32860Sstevel@tonic-gate STRUCT_SIZE(digest_update)) != 0) {
32870Sstevel@tonic-gate crypto_release_minor(cm);
32880Sstevel@tonic-gate return (EFAULT);
32890Sstevel@tonic-gate }
32900Sstevel@tonic-gate
32910Sstevel@tonic-gate data.cd_format = CRYPTO_DATA_RAW;
32920Sstevel@tonic-gate data.cd_raw.iov_base = NULL;
32930Sstevel@tonic-gate
32940Sstevel@tonic-gate datalen = STRUCT_FGET(digest_update, du_datalen);
32950Sstevel@tonic-gate if (datalen > crypto_max_buffer_len) {
32960Sstevel@tonic-gate cmn_err(CE_NOTE, "digest_update: buffer greater than %ld "
32970Sstevel@tonic-gate "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
32980Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD;
32990Sstevel@tonic-gate goto release_minor;
33000Sstevel@tonic-gate }
33010Sstevel@tonic-gate
33026424Skrishna session_id = STRUCT_FGET(digest_update, du_session);
33036424Skrishna
33046424Skrishna if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
33050Sstevel@tonic-gate goto release_minor;
33060Sstevel@tonic-gate }
33076424Skrishna
33086424Skrishna if ((rv = CRYPTO_BUFFER_CHECK(sp, datalen, rctl_chk)) !=
33096424Skrishna CRYPTO_SUCCESS) {
33106424Skrishna goto release_minor;
33116424Skrishna }
33126424Skrishna
33130Sstevel@tonic-gate need = datalen;
33140Sstevel@tonic-gate data.cd_raw.iov_base = kmem_alloc(datalen, KM_SLEEP);
33150Sstevel@tonic-gate data.cd_raw.iov_len = datalen;
33160Sstevel@tonic-gate
33170Sstevel@tonic-gate if (datalen != 0 && copyin(STRUCT_FGETP(digest_update, du_databuf),
33180Sstevel@tonic-gate data.cd_raw.iov_base, datalen) != 0) {
33190Sstevel@tonic-gate error = EFAULT;
33200Sstevel@tonic-gate goto release_minor;
33210Sstevel@tonic-gate }
33220Sstevel@tonic-gate
33230Sstevel@tonic-gate data.cd_offset = 0;
33240Sstevel@tonic-gate data.cd_length = datalen;
33250Sstevel@tonic-gate
33260Sstevel@tonic-gate rv = crypto_digest_update(sp->sd_digest_ctx, &data, NULL);
33270Sstevel@tonic-gate if (rv != CRYPTO_SUCCESS)
33280Sstevel@tonic-gate CRYPTO_CANCEL_CTX(&sp->sd_digest_ctx);
33290Sstevel@tonic-gate
33300Sstevel@tonic-gate release_minor:
33316424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
33326424Skrishna CRYPTO_SESSION_RELE(sp);
33330Sstevel@tonic-gate crypto_release_minor(cm);
33340Sstevel@tonic-gate
33350Sstevel@tonic-gate if (data.cd_raw.iov_base != NULL)
33360Sstevel@tonic-gate kmem_free(data.cd_raw.iov_base, datalen);
33370Sstevel@tonic-gate
33380Sstevel@tonic-gate if (error != 0)
33390Sstevel@tonic-gate return (error);
33400Sstevel@tonic-gate
33410Sstevel@tonic-gate STRUCT_FSET(digest_update, du_return_value, rv);
33420Sstevel@tonic-gate if (copyout(STRUCT_BUF(digest_update), arg,
33430Sstevel@tonic-gate STRUCT_SIZE(digest_update)) != 0) {
33440Sstevel@tonic-gate return (EFAULT);
33450Sstevel@tonic-gate }
33460Sstevel@tonic-gate return (0);
33470Sstevel@tonic-gate }
33480Sstevel@tonic-gate
33490Sstevel@tonic-gate /* ARGSUSED */
33500Sstevel@tonic-gate static int
digest_key(dev_t dev,caddr_t arg,int mode,int * rval)33510Sstevel@tonic-gate digest_key(dev_t dev, caddr_t arg, int mode, int *rval)
33520Sstevel@tonic-gate {
33530Sstevel@tonic-gate STRUCT_DECL(crypto_digest_key, digest_key);
33540Sstevel@tonic-gate crypto_session_id_t session_id;
33550Sstevel@tonic-gate crypto_key_t key;
33560Sstevel@tonic-gate crypto_minor_t *cm;
33576424Skrishna crypto_session_data_t *sp = NULL;
33580Sstevel@tonic-gate size_t rctl_bytes = 0;
33596424Skrishna boolean_t key_rctl_chk = B_FALSE;
33600Sstevel@tonic-gate int error = 0;
33610Sstevel@tonic-gate int rv;
33620Sstevel@tonic-gate
33630Sstevel@tonic-gate STRUCT_INIT(digest_key, mode);
33640Sstevel@tonic-gate
33650Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
33660Sstevel@tonic-gate cmn_err(CE_WARN, "digest_key: failed holding minor");
33670Sstevel@tonic-gate return (ENXIO);
33680Sstevel@tonic-gate }
33690Sstevel@tonic-gate
33700Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(digest_key), STRUCT_SIZE(digest_key)) != 0) {
33710Sstevel@tonic-gate crypto_release_minor(cm);
33720Sstevel@tonic-gate return (EFAULT);
33730Sstevel@tonic-gate }
33740Sstevel@tonic-gate
33750Sstevel@tonic-gate bzero(&key, sizeof (crypto_key_t));
33760Sstevel@tonic-gate
33770Sstevel@tonic-gate session_id = STRUCT_FGET(digest_key, dk_session);
33780Sstevel@tonic-gate
33790Sstevel@tonic-gate if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
33806424Skrishna goto out;
33816424Skrishna }
33826424Skrishna
33836424Skrishna if (!copyin_key(mode, sp, STRUCT_FADDR(digest_key, dk_key), &key,
33846424Skrishna &rctl_bytes, &key_rctl_chk, &rv, &error)) {
33850Sstevel@tonic-gate goto out;
33860Sstevel@tonic-gate }
33870Sstevel@tonic-gate
33880Sstevel@tonic-gate rv = crypto_digest_key_prov(sp->sd_digest_ctx, &key, NULL);
33890Sstevel@tonic-gate if (rv != CRYPTO_SUCCESS)
33900Sstevel@tonic-gate CRYPTO_CANCEL_CTX(&sp->sd_digest_ctx);
33910Sstevel@tonic-gate out:
33926424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, key_rctl_chk);
33930Sstevel@tonic-gate CRYPTO_SESSION_RELE(sp);
33940Sstevel@tonic-gate crypto_release_minor(cm);
33950Sstevel@tonic-gate
33960Sstevel@tonic-gate free_crypto_key(&key);
33970Sstevel@tonic-gate
33980Sstevel@tonic-gate if (error != 0)
33990Sstevel@tonic-gate return (error);
34000Sstevel@tonic-gate
34010Sstevel@tonic-gate STRUCT_FSET(digest_key, dk_return_value, rv);
34020Sstevel@tonic-gate if (copyout(STRUCT_BUF(digest_key), arg,
34030Sstevel@tonic-gate STRUCT_SIZE(digest_key)) != 0) {
34040Sstevel@tonic-gate return (EFAULT);
34050Sstevel@tonic-gate }
34060Sstevel@tonic-gate return (0);
34070Sstevel@tonic-gate }
34080Sstevel@tonic-gate
34090Sstevel@tonic-gate /* ARGSUSED */
34100Sstevel@tonic-gate static int
digest_final(dev_t dev,caddr_t arg,int mode,int * rval)34110Sstevel@tonic-gate digest_final(dev_t dev, caddr_t arg, int mode, int *rval)
34120Sstevel@tonic-gate {
34130Sstevel@tonic-gate return (common_final(dev, arg, mode, crypto_digest_final));
34140Sstevel@tonic-gate }
34150Sstevel@tonic-gate
34160Sstevel@tonic-gate /* ARGSUSED */
34170Sstevel@tonic-gate static int
digest(dev_t dev,caddr_t arg,int mode,int * rval)34180Sstevel@tonic-gate digest(dev_t dev, caddr_t arg, int mode, int *rval)
34190Sstevel@tonic-gate {
34200Sstevel@tonic-gate return (common_digest(dev, arg, mode, crypto_digest_single));
34210Sstevel@tonic-gate }
34220Sstevel@tonic-gate
34230Sstevel@tonic-gate /*
34240Sstevel@tonic-gate * ASSUMPTION: crypto_digest, crypto_sign, crypto_sign_recover,
34250Sstevel@tonic-gate * and crypto_verify_recover are identical except for field names.
34260Sstevel@tonic-gate */
34270Sstevel@tonic-gate static int
common_digest(dev_t dev,caddr_t arg,int mode,int (* single)(crypto_context_t,crypto_data_t *,crypto_data_t *,crypto_call_req_t *))34280Sstevel@tonic-gate common_digest(dev_t dev, caddr_t arg, int mode,
34290Sstevel@tonic-gate int (*single)(crypto_context_t, crypto_data_t *, crypto_data_t *,
34300Sstevel@tonic-gate crypto_call_req_t *))
34310Sstevel@tonic-gate {
34320Sstevel@tonic-gate STRUCT_DECL(crypto_digest, crypto_digest);
34330Sstevel@tonic-gate crypto_session_id_t session_id;
34340Sstevel@tonic-gate crypto_minor_t *cm;
34356424Skrishna crypto_session_data_t *sp = NULL;
34360Sstevel@tonic-gate crypto_data_t data, digest;
34370Sstevel@tonic-gate crypto_ctx_t **ctxpp;
34380Sstevel@tonic-gate size_t datalen, digestlen, need = 0;
34390Sstevel@tonic-gate char *digestbuf;
34400Sstevel@tonic-gate int error = 0;
34410Sstevel@tonic-gate int rv;
34426424Skrishna boolean_t rctl_chk = B_FALSE;
34430Sstevel@tonic-gate
34440Sstevel@tonic-gate STRUCT_INIT(crypto_digest, mode);
34450Sstevel@tonic-gate
34460Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
34470Sstevel@tonic-gate cmn_err(CE_WARN, "common_digest: failed holding minor");
34480Sstevel@tonic-gate return (ENXIO);
34490Sstevel@tonic-gate }
34500Sstevel@tonic-gate
34510Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(crypto_digest),
34520Sstevel@tonic-gate STRUCT_SIZE(crypto_digest)) != 0) {
34530Sstevel@tonic-gate crypto_release_minor(cm);
34540Sstevel@tonic-gate return (EFAULT);
34550Sstevel@tonic-gate }
34560Sstevel@tonic-gate
34570Sstevel@tonic-gate data.cd_raw.iov_base = NULL;
34580Sstevel@tonic-gate digest.cd_raw.iov_base = NULL;
34590Sstevel@tonic-gate
34600Sstevel@tonic-gate datalen = STRUCT_FGET(crypto_digest, cd_datalen);
34610Sstevel@tonic-gate digestlen = STRUCT_FGET(crypto_digest, cd_digestlen);
34620Sstevel@tonic-gate
34630Sstevel@tonic-gate /*
34640Sstevel@tonic-gate * Don't allocate output buffer unless both buffer pointer and
34650Sstevel@tonic-gate * buffer length are not NULL or 0 (length).
34660Sstevel@tonic-gate */
34670Sstevel@tonic-gate digestbuf = STRUCT_FGETP(crypto_digest, cd_digestbuf);
34680Sstevel@tonic-gate if (digestbuf == NULL || digestlen == 0) {
34690Sstevel@tonic-gate digestlen = 0;
34700Sstevel@tonic-gate }
34710Sstevel@tonic-gate
34720Sstevel@tonic-gate if (datalen > crypto_max_buffer_len ||
34730Sstevel@tonic-gate digestlen > crypto_max_buffer_len) {
34740Sstevel@tonic-gate cmn_err(CE_NOTE, "common_digest: buffer greater than %ld "
34750Sstevel@tonic-gate "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
34760Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD;
34770Sstevel@tonic-gate goto release_minor;
34780Sstevel@tonic-gate }
34790Sstevel@tonic-gate
34806424Skrishna session_id = STRUCT_FGET(crypto_digest, cd_session);
34816424Skrishna
34826424Skrishna if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
34836424Skrishna goto release_minor;
34846424Skrishna }
34856424Skrishna
34860Sstevel@tonic-gate need = datalen + digestlen;
34876424Skrishna if ((rv = CRYPTO_BUFFER_CHECK(sp, need, rctl_chk)) !=
34886424Skrishna CRYPTO_SUCCESS) {
34890Sstevel@tonic-gate need = 0;
34900Sstevel@tonic-gate goto release_minor;
34910Sstevel@tonic-gate }
34920Sstevel@tonic-gate
34930Sstevel@tonic-gate INIT_RAW_CRYPTO_DATA(data, datalen);
34940Sstevel@tonic-gate
34950Sstevel@tonic-gate if (datalen != 0 && copyin(STRUCT_FGETP(crypto_digest, cd_databuf),
34960Sstevel@tonic-gate data.cd_raw.iov_base, datalen) != 0) {
34970Sstevel@tonic-gate error = EFAULT;
34980Sstevel@tonic-gate goto release_minor;
34990Sstevel@tonic-gate }
35000Sstevel@tonic-gate
35010Sstevel@tonic-gate INIT_RAW_CRYPTO_DATA(digest, digestlen);
35020Sstevel@tonic-gate
35030Sstevel@tonic-gate ASSERT(single == crypto_digest_single ||
35040Sstevel@tonic-gate single == crypto_sign_single ||
35050Sstevel@tonic-gate single == crypto_verify_recover_single ||
35060Sstevel@tonic-gate single == crypto_sign_recover_single);
35070Sstevel@tonic-gate
35080Sstevel@tonic-gate if (single == crypto_digest_single) {
35090Sstevel@tonic-gate ctxpp = &sp->sd_digest_ctx;
35100Sstevel@tonic-gate } else if (single == crypto_sign_single) {
35110Sstevel@tonic-gate ctxpp = &sp->sd_sign_ctx;
35120Sstevel@tonic-gate } else if (single == crypto_verify_recover_single) {
35130Sstevel@tonic-gate ctxpp = &sp->sd_verify_recover_ctx;
35140Sstevel@tonic-gate } else {
35150Sstevel@tonic-gate ctxpp = &sp->sd_sign_recover_ctx;
35160Sstevel@tonic-gate }
35170Sstevel@tonic-gate rv = (single)(*ctxpp, &data, &digest, NULL);
35180Sstevel@tonic-gate if (KCF_CONTEXT_DONE(rv))
35190Sstevel@tonic-gate *ctxpp = NULL;
35200Sstevel@tonic-gate
35210Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) {
35220Sstevel@tonic-gate ASSERT(digest.cd_length <= digestlen);
35230Sstevel@tonic-gate if (digest.cd_length != 0 && copyout(digest.cd_raw.iov_base,
35240Sstevel@tonic-gate digestbuf, digest.cd_length) != 0) {
35250Sstevel@tonic-gate error = EFAULT;
35260Sstevel@tonic-gate goto release_minor;
35270Sstevel@tonic-gate }
352811413Sopensolaris@drydog.com STRUCT_FSET(crypto_digest, cd_digestlen,
352911413Sopensolaris@drydog.com (ulong_t)digest.cd_length);
35300Sstevel@tonic-gate }
35310Sstevel@tonic-gate
35320Sstevel@tonic-gate if (rv == CRYPTO_BUFFER_TOO_SMALL) {
35330Sstevel@tonic-gate /*
35340Sstevel@tonic-gate * The providers return CRYPTO_BUFFER_TOO_SMALL even for case 1
35350Sstevel@tonic-gate * of section 11.2 of the pkcs11 spec. We catch it here and
35360Sstevel@tonic-gate * provide the correct pkcs11 return value.
35370Sstevel@tonic-gate */
35380Sstevel@tonic-gate if (STRUCT_FGETP(crypto_digest, cd_digestbuf) == NULL)
35390Sstevel@tonic-gate rv = CRYPTO_SUCCESS;
354011413Sopensolaris@drydog.com STRUCT_FSET(crypto_digest, cd_digestlen,
354111413Sopensolaris@drydog.com (ulong_t)digest.cd_length);
35420Sstevel@tonic-gate }
35430Sstevel@tonic-gate
35440Sstevel@tonic-gate release_minor:
35456424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
35466424Skrishna CRYPTO_SESSION_RELE(sp);
35470Sstevel@tonic-gate crypto_release_minor(cm);
35480Sstevel@tonic-gate
35490Sstevel@tonic-gate if (data.cd_raw.iov_base != NULL)
35500Sstevel@tonic-gate kmem_free(data.cd_raw.iov_base, datalen);
35510Sstevel@tonic-gate
35520Sstevel@tonic-gate if (digest.cd_raw.iov_base != NULL)
35530Sstevel@tonic-gate kmem_free(digest.cd_raw.iov_base, digestlen);
35540Sstevel@tonic-gate
35550Sstevel@tonic-gate if (error != 0)
35560Sstevel@tonic-gate return (error);
35570Sstevel@tonic-gate
35580Sstevel@tonic-gate STRUCT_FSET(crypto_digest, cd_return_value, rv);
35590Sstevel@tonic-gate if (copyout(STRUCT_BUF(crypto_digest), arg,
35600Sstevel@tonic-gate STRUCT_SIZE(crypto_digest)) != 0) {
35610Sstevel@tonic-gate return (EFAULT);
35620Sstevel@tonic-gate }
35630Sstevel@tonic-gate return (0);
35640Sstevel@tonic-gate }
35650Sstevel@tonic-gate
35660Sstevel@tonic-gate /*
35670Sstevel@tonic-gate * A helper function that does what the name suggests.
35680Sstevel@tonic-gate * Returns 0 on success and non-zero otherwise.
35690Sstevel@tonic-gate * On failure, out_pin is set to 0.
35700Sstevel@tonic-gate */
35710Sstevel@tonic-gate int
get_pin_and_session_ptr(char * in_pin,char ** out_pin,size_t pin_len,crypto_minor_t * cm,crypto_session_id_t sid,crypto_session_data_t ** sp,int * rv,int * error)35720Sstevel@tonic-gate get_pin_and_session_ptr(char *in_pin, char **out_pin, size_t pin_len,
35730Sstevel@tonic-gate crypto_minor_t *cm, crypto_session_id_t sid, crypto_session_data_t **sp,
35740Sstevel@tonic-gate int *rv, int *error)
35750Sstevel@tonic-gate {
35760Sstevel@tonic-gate char *tmp_pin = NULL;
35770Sstevel@tonic-gate int tmp_error = 0, tmp_rv = 0;
35780Sstevel@tonic-gate
35790Sstevel@tonic-gate if (pin_len > KCF_MAX_PIN_LEN) {
35800Sstevel@tonic-gate tmp_rv = CRYPTO_PIN_LEN_RANGE;
35810Sstevel@tonic-gate goto out;
35820Sstevel@tonic-gate }
35830Sstevel@tonic-gate tmp_pin = kmem_alloc(pin_len, KM_SLEEP);
35840Sstevel@tonic-gate
35850Sstevel@tonic-gate if (pin_len != 0 && copyin(in_pin, tmp_pin, pin_len) != 0) {
35860Sstevel@tonic-gate tmp_error = EFAULT;
35870Sstevel@tonic-gate goto out;
35880Sstevel@tonic-gate }
35890Sstevel@tonic-gate
35900Sstevel@tonic-gate (void) get_session_ptr(sid, cm, sp, &tmp_error, &tmp_rv);
35910Sstevel@tonic-gate out:
35920Sstevel@tonic-gate *out_pin = tmp_pin;
35930Sstevel@tonic-gate *rv = tmp_rv;
35940Sstevel@tonic-gate *error = tmp_error;
35950Sstevel@tonic-gate return (tmp_rv | tmp_error);
35960Sstevel@tonic-gate }
35970Sstevel@tonic-gate
35980Sstevel@tonic-gate /* ARGSUSED */
35990Sstevel@tonic-gate static int
set_pin(dev_t dev,caddr_t arg,int mode,int * rval)36000Sstevel@tonic-gate set_pin(dev_t dev, caddr_t arg, int mode, int *rval)
36010Sstevel@tonic-gate {
36020Sstevel@tonic-gate STRUCT_DECL(crypto_set_pin, set_pin);
36030Sstevel@tonic-gate kcf_provider_desc_t *real_provider;
36040Sstevel@tonic-gate kcf_req_params_t params;
36050Sstevel@tonic-gate crypto_minor_t *cm;
36060Sstevel@tonic-gate crypto_session_data_t *sp;
36070Sstevel@tonic-gate char *old_pin = NULL;
36080Sstevel@tonic-gate char *new_pin = NULL;
36090Sstevel@tonic-gate size_t old_pin_len;
36100Sstevel@tonic-gate size_t new_pin_len;
36110Sstevel@tonic-gate int error = 0;
36120Sstevel@tonic-gate int rv;
36130Sstevel@tonic-gate
36140Sstevel@tonic-gate STRUCT_INIT(set_pin, mode);
36150Sstevel@tonic-gate
36160Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
36170Sstevel@tonic-gate cmn_err(CE_WARN, "set_pin: failed holding minor");
36180Sstevel@tonic-gate return (ENXIO);
36190Sstevel@tonic-gate }
36200Sstevel@tonic-gate
36210Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(set_pin),
36220Sstevel@tonic-gate STRUCT_SIZE(set_pin)) != 0) {
36230Sstevel@tonic-gate crypto_release_minor(cm);
36240Sstevel@tonic-gate return (EFAULT);
36250Sstevel@tonic-gate }
36260Sstevel@tonic-gate
36270Sstevel@tonic-gate old_pin_len = STRUCT_FGET(set_pin, sp_old_len);
36280Sstevel@tonic-gate
36290Sstevel@tonic-gate if (get_pin_and_session_ptr(STRUCT_FGETP(set_pin, sp_old_pin),
36300Sstevel@tonic-gate &old_pin, old_pin_len, cm, STRUCT_FGET(set_pin, sp_session),
36310Sstevel@tonic-gate &sp, &rv, &error) != 0)
36320Sstevel@tonic-gate goto release_minor;
36330Sstevel@tonic-gate
36340Sstevel@tonic-gate new_pin_len = STRUCT_FGET(set_pin, sp_new_len);
36350Sstevel@tonic-gate if (new_pin_len > KCF_MAX_PIN_LEN) {
36360Sstevel@tonic-gate rv = CRYPTO_PIN_LEN_RANGE;
36370Sstevel@tonic-gate goto out;
36380Sstevel@tonic-gate }
36390Sstevel@tonic-gate new_pin = kmem_alloc(new_pin_len, KM_SLEEP);
36400Sstevel@tonic-gate
36410Sstevel@tonic-gate if (new_pin_len != 0 && copyin(STRUCT_FGETP(set_pin, sp_new_pin),
36420Sstevel@tonic-gate new_pin, new_pin_len) != 0) {
36430Sstevel@tonic-gate error = EFAULT;
36440Sstevel@tonic-gate goto out;
36450Sstevel@tonic-gate }
36460Sstevel@tonic-gate
36470Sstevel@tonic-gate if ((rv = kcf_get_hardware_provider_nomech(
36480Sstevel@tonic-gate CRYPTO_OPS_OFFSET(provider_ops), CRYPTO_PROVIDER_OFFSET(set_pin),
3649*12304SValerie.Fenwick@Oracle.COM sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
36500Sstevel@tonic-gate goto out;
36510Sstevel@tonic-gate }
36520Sstevel@tonic-gate
36530Sstevel@tonic-gate KCF_WRAP_PROVMGMT_OPS_PARAMS(¶ms, KCF_OP_MGMT_SETPIN,
36540Sstevel@tonic-gate sp->sd_provider_session->ps_session, old_pin, old_pin_len,
36550Sstevel@tonic-gate new_pin, new_pin_len, NULL, NULL, real_provider);
36560Sstevel@tonic-gate
36570Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
3658904Smcpowers KCF_PROV_REFRELE(real_provider);
36590Sstevel@tonic-gate
36600Sstevel@tonic-gate out:
36610Sstevel@tonic-gate CRYPTO_SESSION_RELE(sp);
36620Sstevel@tonic-gate
36630Sstevel@tonic-gate release_minor:
36640Sstevel@tonic-gate crypto_release_minor(cm);
36650Sstevel@tonic-gate
36660Sstevel@tonic-gate if (old_pin != NULL) {
36670Sstevel@tonic-gate bzero(old_pin, old_pin_len);
36680Sstevel@tonic-gate kmem_free(old_pin, old_pin_len);
36690Sstevel@tonic-gate }
36700Sstevel@tonic-gate
36710Sstevel@tonic-gate if (new_pin != NULL) {
36720Sstevel@tonic-gate bzero(new_pin, new_pin_len);
36730Sstevel@tonic-gate kmem_free(new_pin, new_pin_len);
36740Sstevel@tonic-gate }
36750Sstevel@tonic-gate
36760Sstevel@tonic-gate if (error != 0)
36770Sstevel@tonic-gate return (error);
36780Sstevel@tonic-gate
36790Sstevel@tonic-gate STRUCT_FSET(set_pin, sp_return_value, rv);
36800Sstevel@tonic-gate if (copyout(STRUCT_BUF(set_pin), arg, STRUCT_SIZE(set_pin)) != 0) {
36810Sstevel@tonic-gate return (EFAULT);
36820Sstevel@tonic-gate }
36830Sstevel@tonic-gate return (0);
36840Sstevel@tonic-gate }
36850Sstevel@tonic-gate
36860Sstevel@tonic-gate /* ARGSUSED */
36870Sstevel@tonic-gate static int
login(dev_t dev,caddr_t arg,int mode,int * rval)36880Sstevel@tonic-gate login(dev_t dev, caddr_t arg, int mode, int *rval)
36890Sstevel@tonic-gate {
36900Sstevel@tonic-gate STRUCT_DECL(crypto_login, login);
36910Sstevel@tonic-gate kcf_provider_desc_t *real_provider;
36920Sstevel@tonic-gate kcf_req_params_t params;
36930Sstevel@tonic-gate crypto_minor_t *cm;
36940Sstevel@tonic-gate crypto_session_data_t *sp;
36950Sstevel@tonic-gate size_t pin_len;
36960Sstevel@tonic-gate char *pin;
36970Sstevel@tonic-gate uint_t user_type;
36980Sstevel@tonic-gate int error = 0;
36990Sstevel@tonic-gate int rv;
37000Sstevel@tonic-gate
37010Sstevel@tonic-gate STRUCT_INIT(login, mode);
37020Sstevel@tonic-gate
37030Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
37040Sstevel@tonic-gate cmn_err(CE_WARN, "login: failed holding minor");
37050Sstevel@tonic-gate return (ENXIO);
37060Sstevel@tonic-gate }
37070Sstevel@tonic-gate
37080Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(login), STRUCT_SIZE(login)) != 0) {
37090Sstevel@tonic-gate crypto_release_minor(cm);
37100Sstevel@tonic-gate return (EFAULT);
37110Sstevel@tonic-gate }
37120Sstevel@tonic-gate
37130Sstevel@tonic-gate user_type = STRUCT_FGET(login, co_user_type);
37140Sstevel@tonic-gate
37150Sstevel@tonic-gate pin_len = STRUCT_FGET(login, co_pin_len);
37160Sstevel@tonic-gate
37170Sstevel@tonic-gate if (get_pin_and_session_ptr(STRUCT_FGETP(login, co_pin),
37180Sstevel@tonic-gate &pin, pin_len, cm, STRUCT_FGET(login, co_session),
37190Sstevel@tonic-gate &sp, &rv, &error) != 0) {
37200Sstevel@tonic-gate if (rv == CRYPTO_PIN_LEN_RANGE)
37210Sstevel@tonic-gate rv = CRYPTO_PIN_INCORRECT;
37220Sstevel@tonic-gate goto release_minor;
37230Sstevel@tonic-gate }
37240Sstevel@tonic-gate
37250Sstevel@tonic-gate if ((rv = kcf_get_hardware_provider_nomech(
37260Sstevel@tonic-gate CRYPTO_OPS_OFFSET(session_ops),
3727*12304SValerie.Fenwick@Oracle.COM CRYPTO_SESSION_OFFSET(session_login), sp->sd_provider,
3728*12304SValerie.Fenwick@Oracle.COM &real_provider)) != CRYPTO_SUCCESS) {
37290Sstevel@tonic-gate goto out;
37300Sstevel@tonic-gate }
37310Sstevel@tonic-gate
37320Sstevel@tonic-gate KCF_WRAP_SESSION_OPS_PARAMS(¶ms, KCF_OP_SESSION_LOGIN, NULL,
37330Sstevel@tonic-gate sp->sd_provider_session->ps_session, user_type, pin, pin_len,
37340Sstevel@tonic-gate real_provider);
37350Sstevel@tonic-gate
37360Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
3737904Smcpowers KCF_PROV_REFRELE(real_provider);
37380Sstevel@tonic-gate
37390Sstevel@tonic-gate out:
37400Sstevel@tonic-gate CRYPTO_SESSION_RELE(sp);
37410Sstevel@tonic-gate
37420Sstevel@tonic-gate release_minor:
37430Sstevel@tonic-gate crypto_release_minor(cm);
37440Sstevel@tonic-gate
37450Sstevel@tonic-gate if (pin != NULL) {
37460Sstevel@tonic-gate bzero(pin, pin_len);
37470Sstevel@tonic-gate kmem_free(pin, pin_len);
37480Sstevel@tonic-gate }
37490Sstevel@tonic-gate
37500Sstevel@tonic-gate if (error != 0)
37510Sstevel@tonic-gate return (error);
37520Sstevel@tonic-gate
37530Sstevel@tonic-gate STRUCT_FSET(login, co_return_value, rv);
37540Sstevel@tonic-gate if (copyout(STRUCT_BUF(login), arg, STRUCT_SIZE(login)) != 0) {
37550Sstevel@tonic-gate return (EFAULT);
37560Sstevel@tonic-gate }
37570Sstevel@tonic-gate return (0);
37580Sstevel@tonic-gate }
37590Sstevel@tonic-gate
37600Sstevel@tonic-gate /* ARGSUSED */
37610Sstevel@tonic-gate static int
logout(dev_t dev,caddr_t arg,int mode,int * rval)37620Sstevel@tonic-gate logout(dev_t dev, caddr_t arg, int mode, int *rval)
37630Sstevel@tonic-gate {
37640Sstevel@tonic-gate crypto_logout_t logout;
37650Sstevel@tonic-gate kcf_provider_desc_t *real_provider;
37660Sstevel@tonic-gate kcf_req_params_t params;
37670Sstevel@tonic-gate crypto_minor_t *cm;
37680Sstevel@tonic-gate crypto_session_data_t *sp;
37690Sstevel@tonic-gate int error = 0;
37700Sstevel@tonic-gate int rv;
37710Sstevel@tonic-gate
37720Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
37730Sstevel@tonic-gate cmn_err(CE_WARN, "logout: failed holding minor");
37740Sstevel@tonic-gate return (ENXIO);
37750Sstevel@tonic-gate }
37760Sstevel@tonic-gate
37770Sstevel@tonic-gate if (copyin(arg, &logout, sizeof (logout)) != 0) {
37780Sstevel@tonic-gate crypto_release_minor(cm);
37790Sstevel@tonic-gate return (EFAULT);
37800Sstevel@tonic-gate }
37810Sstevel@tonic-gate
37820Sstevel@tonic-gate if (!get_session_ptr(logout.cl_session, cm, &sp, &error, &rv)) {
37830Sstevel@tonic-gate goto release_minor;
37840Sstevel@tonic-gate }
37850Sstevel@tonic-gate
37860Sstevel@tonic-gate if ((rv = kcf_get_hardware_provider_nomech(
37870Sstevel@tonic-gate CRYPTO_OPS_OFFSET(session_ops),
3788*12304SValerie.Fenwick@Oracle.COM CRYPTO_SESSION_OFFSET(session_logout), sp->sd_provider,
3789*12304SValerie.Fenwick@Oracle.COM &real_provider)) != CRYPTO_SUCCESS) {
37900Sstevel@tonic-gate goto out;
37910Sstevel@tonic-gate }
37920Sstevel@tonic-gate
37930Sstevel@tonic-gate KCF_WRAP_SESSION_OPS_PARAMS(¶ms, KCF_OP_SESSION_LOGOUT, NULL,
37940Sstevel@tonic-gate sp->sd_provider_session->ps_session, 0, NULL, 0, real_provider);
37950Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
3796904Smcpowers KCF_PROV_REFRELE(real_provider);
37970Sstevel@tonic-gate
37980Sstevel@tonic-gate out:
37990Sstevel@tonic-gate CRYPTO_SESSION_RELE(sp);
38000Sstevel@tonic-gate
38010Sstevel@tonic-gate release_minor:
38020Sstevel@tonic-gate crypto_release_minor(cm);
38030Sstevel@tonic-gate
38040Sstevel@tonic-gate if (error != 0)
38050Sstevel@tonic-gate return (error);
38060Sstevel@tonic-gate
38070Sstevel@tonic-gate logout.cl_return_value = rv;
38080Sstevel@tonic-gate if (copyout(&logout, arg, sizeof (logout)) != 0) {
38090Sstevel@tonic-gate return (EFAULT);
38100Sstevel@tonic-gate }
38110Sstevel@tonic-gate return (0);
38120Sstevel@tonic-gate }
38130Sstevel@tonic-gate
38140Sstevel@tonic-gate /* ARGSUSED */
38150Sstevel@tonic-gate static int
sign_init(dev_t dev,caddr_t arg,int mode,int * rval)38160Sstevel@tonic-gate sign_init(dev_t dev, caddr_t arg, int mode, int *rval)
38170Sstevel@tonic-gate {
38180Sstevel@tonic-gate return (sign_verify_init(dev, arg, mode, crypto_sign_init_prov));
38190Sstevel@tonic-gate }
38200Sstevel@tonic-gate
38210Sstevel@tonic-gate /* ARGSUSED */
38220Sstevel@tonic-gate static int
sign_recover_init(dev_t dev,caddr_t arg,int mode,int * rval)38230Sstevel@tonic-gate sign_recover_init(dev_t dev, caddr_t arg, int mode, int *rval)
38240Sstevel@tonic-gate {
38250Sstevel@tonic-gate return (sign_verify_init(dev, arg, mode,
38260Sstevel@tonic-gate crypto_sign_recover_init_prov));
38270Sstevel@tonic-gate }
38280Sstevel@tonic-gate
38290Sstevel@tonic-gate /* ARGSUSED */
38300Sstevel@tonic-gate static int
verify_init(dev_t dev,caddr_t arg,int mode,int * rval)38310Sstevel@tonic-gate verify_init(dev_t dev, caddr_t arg, int mode, int *rval)
38320Sstevel@tonic-gate {
38330Sstevel@tonic-gate return (sign_verify_init(dev, arg, mode, crypto_verify_init_prov));
38340Sstevel@tonic-gate }
38350Sstevel@tonic-gate
38360Sstevel@tonic-gate /* ARGSUSED */
38370Sstevel@tonic-gate static int
verify_recover_init(dev_t dev,caddr_t arg,int mode,int * rval)38380Sstevel@tonic-gate verify_recover_init(dev_t dev, caddr_t arg, int mode, int *rval)
38390Sstevel@tonic-gate {
38400Sstevel@tonic-gate return (sign_verify_init(dev, arg, mode,
38410Sstevel@tonic-gate crypto_verify_recover_init_prov));
38420Sstevel@tonic-gate }
38430Sstevel@tonic-gate
38440Sstevel@tonic-gate /*
38450Sstevel@tonic-gate * ASSUMPTION: crypto_sign_init, crypto_verify_init, crypto_sign_recover_init,
38460Sstevel@tonic-gate * and crypto_verify_recover_init structures are identical
38470Sstevel@tonic-gate * except for field names.
38480Sstevel@tonic-gate */
38490Sstevel@tonic-gate static int
sign_verify_init(dev_t dev,caddr_t arg,int mode,int (* init)(crypto_provider_t,crypto_session_id_t,crypto_mechanism_t *,crypto_key_t *,crypto_ctx_template_t,crypto_context_t *,crypto_call_req_t *))38500Sstevel@tonic-gate sign_verify_init(dev_t dev, caddr_t arg, int mode,
3851904Smcpowers int (*init)(crypto_provider_t, crypto_session_id_t,
38520Sstevel@tonic-gate crypto_mechanism_t *, crypto_key_t *, crypto_ctx_template_t,
38530Sstevel@tonic-gate crypto_context_t *, crypto_call_req_t *))
38540Sstevel@tonic-gate {
38550Sstevel@tonic-gate STRUCT_DECL(crypto_sign_init, sign_init);
3856904Smcpowers kcf_provider_desc_t *real_provider = NULL;
38570Sstevel@tonic-gate crypto_session_id_t session_id;
38580Sstevel@tonic-gate crypto_mechanism_t mech;
38590Sstevel@tonic-gate crypto_key_t key;
38600Sstevel@tonic-gate crypto_minor_t *cm;
38616424Skrishna crypto_session_data_t *sp = NULL;
38620Sstevel@tonic-gate crypto_context_t cc;
38630Sstevel@tonic-gate crypto_ctx_t **ctxpp;
38640Sstevel@tonic-gate size_t mech_rctl_bytes = 0;
38656424Skrishna boolean_t mech_rctl_chk = B_FALSE;
38660Sstevel@tonic-gate size_t key_rctl_bytes = 0;
38676424Skrishna boolean_t key_rctl_chk = B_FALSE;
38680Sstevel@tonic-gate int error = 0;
38690Sstevel@tonic-gate int rv;
3870904Smcpowers boolean_t allocated_by_crypto_module = B_FALSE;
38711808Smcpowers crypto_func_group_t fg;
38720Sstevel@tonic-gate
38730Sstevel@tonic-gate STRUCT_INIT(sign_init, mode);
38740Sstevel@tonic-gate
38750Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
38760Sstevel@tonic-gate cmn_err(CE_WARN, "sign_verify_init: failed holding minor");
38770Sstevel@tonic-gate return (ENXIO);
38780Sstevel@tonic-gate }
38790Sstevel@tonic-gate
38800Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(sign_init), STRUCT_SIZE(sign_init)) != 0) {
38810Sstevel@tonic-gate crypto_release_minor(cm);
38820Sstevel@tonic-gate return (EFAULT);
38830Sstevel@tonic-gate }
38840Sstevel@tonic-gate
38850Sstevel@tonic-gate mech.cm_param = NULL;
38860Sstevel@tonic-gate bzero(&key, sizeof (key));
38870Sstevel@tonic-gate
38880Sstevel@tonic-gate session_id = STRUCT_FGET(sign_init, si_session);
38890Sstevel@tonic-gate
38900Sstevel@tonic-gate if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
38916424Skrishna goto out;
38920Sstevel@tonic-gate }
38930Sstevel@tonic-gate
3894904Smcpowers bcopy(STRUCT_FADDR(sign_init, si_mech), &mech.cm_type,
3895904Smcpowers sizeof (crypto_mech_type_t));
38960Sstevel@tonic-gate
38970Sstevel@tonic-gate ASSERT(init == crypto_sign_init_prov ||
38980Sstevel@tonic-gate init == crypto_verify_init_prov ||
38990Sstevel@tonic-gate init == crypto_sign_recover_init_prov ||
39000Sstevel@tonic-gate init == crypto_verify_recover_init_prov);
39010Sstevel@tonic-gate
39020Sstevel@tonic-gate if (init == crypto_sign_init_prov) {
39031808Smcpowers fg = CRYPTO_FG_SIGN;
39040Sstevel@tonic-gate ctxpp = &sp->sd_sign_ctx;
39050Sstevel@tonic-gate } else if (init == crypto_verify_init_prov) {
39061808Smcpowers fg = CRYPTO_FG_VERIFY;
39070Sstevel@tonic-gate ctxpp = &sp->sd_verify_ctx;
39080Sstevel@tonic-gate } else if (init == crypto_sign_recover_init_prov) {
39091808Smcpowers fg = CRYPTO_FG_SIGN_RECOVER;
39100Sstevel@tonic-gate ctxpp = &sp->sd_sign_recover_ctx;
39110Sstevel@tonic-gate } else {
39121808Smcpowers fg = CRYPTO_FG_VERIFY_RECOVER;
39130Sstevel@tonic-gate ctxpp = &sp->sd_verify_recover_ctx;
39140Sstevel@tonic-gate }
39150Sstevel@tonic-gate
391610444SVladimir.Kotal@Sun.COM /* We need the key length for provider selection so copy it in now. */
391710444SVladimir.Kotal@Sun.COM if (!copyin_key(mode, sp, STRUCT_FADDR(sign_init, si_key), &key,
391810444SVladimir.Kotal@Sun.COM &key_rctl_bytes, &key_rctl_chk, &rv, &error)) {
391910444SVladimir.Kotal@Sun.COM goto out;
392010444SVladimir.Kotal@Sun.COM }
392110444SVladimir.Kotal@Sun.COM
392210444SVladimir.Kotal@Sun.COM if ((rv = kcf_get_hardware_provider(mech.cm_type, &key,
3923*12304SValerie.Fenwick@Oracle.COM CRYPTO_MECH_INVALID, NULL, sp->sd_provider, &real_provider,
3924*12304SValerie.Fenwick@Oracle.COM fg)) != CRYPTO_SUCCESS) {
39250Sstevel@tonic-gate goto out;
39260Sstevel@tonic-gate }
39270Sstevel@tonic-gate
3928904Smcpowers rv = crypto_provider_copyin_mech_param(real_provider,
3929904Smcpowers STRUCT_FADDR(sign_init, si_mech), &mech, mode, &error);
3930904Smcpowers
3931904Smcpowers if (rv == CRYPTO_NOT_SUPPORTED) {
3932904Smcpowers allocated_by_crypto_module = B_TRUE;
39336424Skrishna if (!copyin_mech(mode, sp, STRUCT_FADDR(sign_init, si_mech),
39346424Skrishna &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) {
3935904Smcpowers goto out;
3936904Smcpowers }
3937904Smcpowers } else {
3938904Smcpowers if (rv != CRYPTO_SUCCESS)
3939904Smcpowers goto out;
3940904Smcpowers }
3941904Smcpowers
39420Sstevel@tonic-gate rv = (init)(real_provider, sp->sd_provider_session->ps_session,
39430Sstevel@tonic-gate &mech, &key, NULL, &cc, NULL);
39440Sstevel@tonic-gate
39450Sstevel@tonic-gate /*
39460Sstevel@tonic-gate * Check if a context already exists. If so, it means it is being
39470Sstevel@tonic-gate * abandoned. So, cancel it to avoid leaking it.
39480Sstevel@tonic-gate */
39490Sstevel@tonic-gate if (*ctxpp != NULL)
39500Sstevel@tonic-gate CRYPTO_CANCEL_CTX(ctxpp);
39510Sstevel@tonic-gate *ctxpp = (rv == CRYPTO_SUCCESS) ? cc : NULL;
39520Sstevel@tonic-gate
39530Sstevel@tonic-gate out:
39546424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
39556424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk);
39560Sstevel@tonic-gate CRYPTO_SESSION_RELE(sp);
39570Sstevel@tonic-gate crypto_release_minor(cm);
39580Sstevel@tonic-gate
3959904Smcpowers if (real_provider != NULL) {
3960904Smcpowers crypto_free_mech(real_provider,
3961904Smcpowers allocated_by_crypto_module, &mech);
3962904Smcpowers KCF_PROV_REFRELE(real_provider);
3963904Smcpowers }
39640Sstevel@tonic-gate
39650Sstevel@tonic-gate free_crypto_key(&key);
39660Sstevel@tonic-gate
39670Sstevel@tonic-gate if (error != 0)
39680Sstevel@tonic-gate return (error);
39690Sstevel@tonic-gate
39700Sstevel@tonic-gate STRUCT_FSET(sign_init, si_return_value, rv);
39710Sstevel@tonic-gate if (copyout(STRUCT_BUF(sign_init), arg, STRUCT_SIZE(sign_init)) != 0) {
39720Sstevel@tonic-gate return (EFAULT);
39730Sstevel@tonic-gate }
39740Sstevel@tonic-gate return (0);
39750Sstevel@tonic-gate }
39760Sstevel@tonic-gate
39770Sstevel@tonic-gate /* ARGSUSED */
39780Sstevel@tonic-gate static int
sign(dev_t dev,caddr_t arg,int mode,int * rval)39790Sstevel@tonic-gate sign(dev_t dev, caddr_t arg, int mode, int *rval)
39800Sstevel@tonic-gate {
39810Sstevel@tonic-gate return (common_digest(dev, arg, mode, crypto_sign_single));
39820Sstevel@tonic-gate }
39830Sstevel@tonic-gate
39840Sstevel@tonic-gate /* ARGSUSED */
39850Sstevel@tonic-gate static int
sign_recover(dev_t dev,caddr_t arg,int mode,int * rval)39860Sstevel@tonic-gate sign_recover(dev_t dev, caddr_t arg, int mode, int *rval)
39870Sstevel@tonic-gate {
39880Sstevel@tonic-gate return (common_digest(dev, arg, mode, crypto_sign_recover_single));
39890Sstevel@tonic-gate }
39900Sstevel@tonic-gate
39910Sstevel@tonic-gate /* ARGSUSED */
39920Sstevel@tonic-gate static int
verify(dev_t dev,caddr_t arg,int mode,int * rval)39930Sstevel@tonic-gate verify(dev_t dev, caddr_t arg, int mode, int *rval)
39940Sstevel@tonic-gate {
39950Sstevel@tonic-gate STRUCT_DECL(crypto_verify, verify);
39960Sstevel@tonic-gate crypto_session_id_t session_id;
39970Sstevel@tonic-gate crypto_minor_t *cm;
39986424Skrishna crypto_session_data_t *sp = NULL;
39990Sstevel@tonic-gate crypto_data_t data, sign;
40000Sstevel@tonic-gate size_t datalen, signlen, need = 0;
40010Sstevel@tonic-gate int error = 0;
40020Sstevel@tonic-gate int rv;
40036424Skrishna boolean_t rctl_chk = B_FALSE;
40040Sstevel@tonic-gate
40050Sstevel@tonic-gate STRUCT_INIT(verify, mode);
40060Sstevel@tonic-gate
40070Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
40080Sstevel@tonic-gate cmn_err(CE_WARN, "verify: failed holding minor");
40090Sstevel@tonic-gate return (ENXIO);
40100Sstevel@tonic-gate }
40110Sstevel@tonic-gate
40120Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(verify), STRUCT_SIZE(verify)) != 0) {
40130Sstevel@tonic-gate crypto_release_minor(cm);
40140Sstevel@tonic-gate return (EFAULT);
40150Sstevel@tonic-gate }
40160Sstevel@tonic-gate
40170Sstevel@tonic-gate data.cd_raw.iov_base = NULL;
40180Sstevel@tonic-gate sign.cd_raw.iov_base = NULL;
40190Sstevel@tonic-gate
40200Sstevel@tonic-gate datalen = STRUCT_FGET(verify, cv_datalen);
40210Sstevel@tonic-gate signlen = STRUCT_FGET(verify, cv_signlen);
40220Sstevel@tonic-gate if (datalen > crypto_max_buffer_len ||
40230Sstevel@tonic-gate signlen > crypto_max_buffer_len) {
40240Sstevel@tonic-gate cmn_err(CE_NOTE, "verify: buffer greater than %ld bytes, "
40250Sstevel@tonic-gate "pid = %d", crypto_max_buffer_len, curproc->p_pid);
40260Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD;
40270Sstevel@tonic-gate goto release_minor;
40280Sstevel@tonic-gate }
40290Sstevel@tonic-gate
40306424Skrishna session_id = STRUCT_FGET(verify, cv_session);
40316424Skrishna
40326424Skrishna if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
40336424Skrishna goto release_minor;
40346424Skrishna }
40356424Skrishna
40360Sstevel@tonic-gate need = datalen + signlen;
40376424Skrishna if ((rv = CRYPTO_BUFFER_CHECK(sp, need, rctl_chk)) !=
40386424Skrishna CRYPTO_SUCCESS) {
40390Sstevel@tonic-gate need = 0;
40400Sstevel@tonic-gate goto release_minor;
40410Sstevel@tonic-gate }
40420Sstevel@tonic-gate
40430Sstevel@tonic-gate INIT_RAW_CRYPTO_DATA(data, datalen);
40440Sstevel@tonic-gate INIT_RAW_CRYPTO_DATA(sign, signlen);
40450Sstevel@tonic-gate
40460Sstevel@tonic-gate if (datalen != 0 && copyin(STRUCT_FGETP(verify, cv_databuf),
40470Sstevel@tonic-gate data.cd_raw.iov_base, datalen) != 0) {
40480Sstevel@tonic-gate error = EFAULT;
40490Sstevel@tonic-gate goto release_minor;
40500Sstevel@tonic-gate }
40510Sstevel@tonic-gate
40520Sstevel@tonic-gate if (signlen != 0 && copyin(STRUCT_FGETP(verify, cv_signbuf),
40530Sstevel@tonic-gate sign.cd_raw.iov_base, signlen) != 0) {
40540Sstevel@tonic-gate error = EFAULT;
40550Sstevel@tonic-gate goto release_minor;
40560Sstevel@tonic-gate }
40570Sstevel@tonic-gate
40580Sstevel@tonic-gate rv = crypto_verify_single(sp->sd_verify_ctx, &data, &sign, NULL);
40590Sstevel@tonic-gate if (KCF_CONTEXT_DONE(rv))
40600Sstevel@tonic-gate sp->sd_verify_ctx = NULL;
40610Sstevel@tonic-gate
40626424Skrishna release_minor:
40636424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
40640Sstevel@tonic-gate CRYPTO_SESSION_RELE(sp);
40650Sstevel@tonic-gate crypto_release_minor(cm);
40660Sstevel@tonic-gate
40670Sstevel@tonic-gate if (data.cd_raw.iov_base != NULL)
40680Sstevel@tonic-gate kmem_free(data.cd_raw.iov_base, datalen);
40690Sstevel@tonic-gate
40700Sstevel@tonic-gate if (sign.cd_raw.iov_base != NULL)
40710Sstevel@tonic-gate kmem_free(sign.cd_raw.iov_base, signlen);
40720Sstevel@tonic-gate
40730Sstevel@tonic-gate if (error != 0)
40740Sstevel@tonic-gate return (error);
40750Sstevel@tonic-gate
40760Sstevel@tonic-gate STRUCT_FSET(verify, cv_return_value, rv);
40770Sstevel@tonic-gate if (copyout(STRUCT_BUF(verify), arg, STRUCT_SIZE(verify)) != 0) {
40780Sstevel@tonic-gate return (EFAULT);
40790Sstevel@tonic-gate }
40800Sstevel@tonic-gate return (0);
40810Sstevel@tonic-gate }
40820Sstevel@tonic-gate
40830Sstevel@tonic-gate /* ARGSUSED */
40840Sstevel@tonic-gate static int
verify_recover(dev_t dev,caddr_t arg,int mode,int * rval)40850Sstevel@tonic-gate verify_recover(dev_t dev, caddr_t arg, int mode, int *rval)
40860Sstevel@tonic-gate {
40870Sstevel@tonic-gate return (common_digest(dev, arg, mode, crypto_verify_recover_single));
40880Sstevel@tonic-gate }
40890Sstevel@tonic-gate
40900Sstevel@tonic-gate /* ARGSUSED */
40910Sstevel@tonic-gate static int
sign_update(dev_t dev,caddr_t arg,int mode,int * rval)40920Sstevel@tonic-gate sign_update(dev_t dev, caddr_t arg, int mode, int *rval)
40930Sstevel@tonic-gate {
40940Sstevel@tonic-gate return (sign_verify_update(dev, arg, mode, crypto_sign_update));
40950Sstevel@tonic-gate }
40960Sstevel@tonic-gate
40970Sstevel@tonic-gate /* ARGSUSED */
40980Sstevel@tonic-gate static int
verify_update(dev_t dev,caddr_t arg,int mode,int * rval)40990Sstevel@tonic-gate verify_update(dev_t dev, caddr_t arg, int mode, int *rval)
41000Sstevel@tonic-gate {
41010Sstevel@tonic-gate return (sign_verify_update(dev, arg, mode, crypto_verify_update));
41020Sstevel@tonic-gate }
41030Sstevel@tonic-gate
41040Sstevel@tonic-gate /*
41050Sstevel@tonic-gate * ASSUMPTION: crypto_sign_update and crypto_verify_update structures
41060Sstevel@tonic-gate * are identical except for field names.
41070Sstevel@tonic-gate */
41080Sstevel@tonic-gate static int
sign_verify_update(dev_t dev,caddr_t arg,int mode,int (* update)(crypto_context_t,crypto_data_t *,crypto_call_req_t *))41090Sstevel@tonic-gate sign_verify_update(dev_t dev, caddr_t arg, int mode,
41100Sstevel@tonic-gate int (*update)(crypto_context_t, crypto_data_t *, crypto_call_req_t *))
41110Sstevel@tonic-gate {
41120Sstevel@tonic-gate STRUCT_DECL(crypto_sign_update, sign_update);
41130Sstevel@tonic-gate crypto_session_id_t session_id;
41140Sstevel@tonic-gate crypto_minor_t *cm;
41156424Skrishna crypto_session_data_t *sp = NULL;
41160Sstevel@tonic-gate crypto_ctx_t **ctxpp;
41170Sstevel@tonic-gate crypto_data_t data;
41180Sstevel@tonic-gate size_t datalen, need = 0;
41190Sstevel@tonic-gate int error = 0;
41200Sstevel@tonic-gate int rv;
41216424Skrishna boolean_t rctl_chk = B_FALSE;
41220Sstevel@tonic-gate
41230Sstevel@tonic-gate STRUCT_INIT(sign_update, mode);
41240Sstevel@tonic-gate
41250Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
41260Sstevel@tonic-gate cmn_err(CE_WARN, "sign_verify_update: failed holding minor");
41270Sstevel@tonic-gate return (ENXIO);
41280Sstevel@tonic-gate }
41290Sstevel@tonic-gate
41300Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(sign_update),
41310Sstevel@tonic-gate STRUCT_SIZE(sign_update)) != 0) {
41320Sstevel@tonic-gate crypto_release_minor(cm);
41330Sstevel@tonic-gate return (EFAULT);
41340Sstevel@tonic-gate }
41350Sstevel@tonic-gate
41360Sstevel@tonic-gate data.cd_raw.iov_base = NULL;
41370Sstevel@tonic-gate
41380Sstevel@tonic-gate datalen = STRUCT_FGET(sign_update, su_datalen);
41390Sstevel@tonic-gate if (datalen > crypto_max_buffer_len) {
41400Sstevel@tonic-gate cmn_err(CE_NOTE, "sign_verify_update: buffer greater than %ld "
41410Sstevel@tonic-gate "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
41420Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD;
41430Sstevel@tonic-gate goto release_minor;
41440Sstevel@tonic-gate }
41450Sstevel@tonic-gate
41466424Skrishna session_id = STRUCT_FGET(sign_update, su_session);
41476424Skrishna
41486424Skrishna if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
41496424Skrishna goto release_minor;
41506424Skrishna }
41516424Skrishna
41526424Skrishna if ((rv = CRYPTO_BUFFER_CHECK(sp, datalen, rctl_chk)) !=
41536424Skrishna CRYPTO_SUCCESS) {
41540Sstevel@tonic-gate goto release_minor;
41550Sstevel@tonic-gate }
41560Sstevel@tonic-gate need = datalen;
41570Sstevel@tonic-gate
41580Sstevel@tonic-gate INIT_RAW_CRYPTO_DATA(data, datalen);
41590Sstevel@tonic-gate
41600Sstevel@tonic-gate if (datalen != 0 && copyin(STRUCT_FGETP(sign_update, su_databuf),
41610Sstevel@tonic-gate data.cd_raw.iov_base, datalen) != 0) {
41620Sstevel@tonic-gate error = EFAULT;
41630Sstevel@tonic-gate goto release_minor;
41640Sstevel@tonic-gate }
41650Sstevel@tonic-gate
41660Sstevel@tonic-gate ctxpp = (update == crypto_sign_update) ?
41670Sstevel@tonic-gate &sp->sd_sign_ctx : &sp->sd_verify_ctx;
41680Sstevel@tonic-gate
41690Sstevel@tonic-gate rv = (update)(*ctxpp, &data, NULL);
41700Sstevel@tonic-gate if (rv != CRYPTO_SUCCESS)
41710Sstevel@tonic-gate CRYPTO_CANCEL_CTX(ctxpp);
41720Sstevel@tonic-gate
41730Sstevel@tonic-gate release_minor:
41746424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
41756424Skrishna CRYPTO_SESSION_RELE(sp);
41760Sstevel@tonic-gate crypto_release_minor(cm);
41770Sstevel@tonic-gate
41780Sstevel@tonic-gate if (data.cd_raw.iov_base != NULL)
41790Sstevel@tonic-gate kmem_free(data.cd_raw.iov_base, datalen);
41800Sstevel@tonic-gate
41810Sstevel@tonic-gate if (error != 0)
41820Sstevel@tonic-gate return (error);
41830Sstevel@tonic-gate
41840Sstevel@tonic-gate STRUCT_FSET(sign_update, su_return_value, rv);
41850Sstevel@tonic-gate if (copyout(STRUCT_BUF(sign_update), arg,
41860Sstevel@tonic-gate STRUCT_SIZE(sign_update)) != 0) {
41870Sstevel@tonic-gate return (EFAULT);
41880Sstevel@tonic-gate }
41890Sstevel@tonic-gate return (0);
41900Sstevel@tonic-gate }
41910Sstevel@tonic-gate
41920Sstevel@tonic-gate /* ARGSUSED */
41930Sstevel@tonic-gate static int
sign_final(dev_t dev,caddr_t arg,int mode,int * rval)41940Sstevel@tonic-gate sign_final(dev_t dev, caddr_t arg, int mode, int *rval)
41950Sstevel@tonic-gate {
41960Sstevel@tonic-gate return (common_final(dev, arg, mode, crypto_sign_final));
41970Sstevel@tonic-gate }
41980Sstevel@tonic-gate
41990Sstevel@tonic-gate /*
42000Sstevel@tonic-gate * Can't use the common final because it does a copyout of
42010Sstevel@tonic-gate * the final part.
42020Sstevel@tonic-gate */
42030Sstevel@tonic-gate /* ARGSUSED */
42040Sstevel@tonic-gate static int
verify_final(dev_t dev,caddr_t arg,int mode,int * rval)42050Sstevel@tonic-gate verify_final(dev_t dev, caddr_t arg, int mode, int *rval)
42060Sstevel@tonic-gate {
42070Sstevel@tonic-gate STRUCT_DECL(crypto_verify_final, verify_final);
42080Sstevel@tonic-gate crypto_session_id_t session_id;
42090Sstevel@tonic-gate crypto_minor_t *cm;
42106424Skrishna crypto_session_data_t *sp = NULL;
42110Sstevel@tonic-gate crypto_data_t sign;
42120Sstevel@tonic-gate size_t signlen, need = 0;
42130Sstevel@tonic-gate int error = 0;
42140Sstevel@tonic-gate int rv;
42156424Skrishna boolean_t rctl_chk = B_FALSE;
42160Sstevel@tonic-gate
42170Sstevel@tonic-gate STRUCT_INIT(verify_final, mode);
42180Sstevel@tonic-gate
42190Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
42200Sstevel@tonic-gate cmn_err(CE_WARN, "verify_final: failed holding minor");
42210Sstevel@tonic-gate return (ENXIO);
42220Sstevel@tonic-gate }
42230Sstevel@tonic-gate
42240Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(verify_final),
42250Sstevel@tonic-gate STRUCT_SIZE(verify_final)) != 0) {
42260Sstevel@tonic-gate crypto_release_minor(cm);
42270Sstevel@tonic-gate return (EFAULT);
42280Sstevel@tonic-gate }
42290Sstevel@tonic-gate
42300Sstevel@tonic-gate sign.cd_raw.iov_base = NULL;
42310Sstevel@tonic-gate
42320Sstevel@tonic-gate signlen = STRUCT_FGET(verify_final, vf_signlen);
42330Sstevel@tonic-gate if (signlen > crypto_max_buffer_len) {
42340Sstevel@tonic-gate cmn_err(CE_NOTE, "verify_final: buffer greater than %ld "
42350Sstevel@tonic-gate "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
42360Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD;
42370Sstevel@tonic-gate goto release_minor;
42380Sstevel@tonic-gate }
42390Sstevel@tonic-gate
42406424Skrishna session_id = STRUCT_FGET(verify_final, vf_session);
42416424Skrishna
42426424Skrishna if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
42436424Skrishna goto release_minor;
42446424Skrishna }
42456424Skrishna
42466424Skrishna if ((rv = CRYPTO_BUFFER_CHECK(sp, signlen, rctl_chk)) !=
42476424Skrishna CRYPTO_SUCCESS) {
42480Sstevel@tonic-gate goto release_minor;
42490Sstevel@tonic-gate }
42500Sstevel@tonic-gate need = signlen;
42510Sstevel@tonic-gate
42520Sstevel@tonic-gate INIT_RAW_CRYPTO_DATA(sign, signlen);
42530Sstevel@tonic-gate
42540Sstevel@tonic-gate if (signlen != 0 && copyin(STRUCT_FGETP(verify_final, vf_signbuf),
42550Sstevel@tonic-gate sign.cd_raw.iov_base, signlen) != 0) {
42560Sstevel@tonic-gate error = EFAULT;
42570Sstevel@tonic-gate goto release_minor;
42580Sstevel@tonic-gate }
42590Sstevel@tonic-gate
42600Sstevel@tonic-gate rv = crypto_verify_final(sp->sd_verify_ctx, &sign, NULL);
42610Sstevel@tonic-gate if (KCF_CONTEXT_DONE(rv))
42620Sstevel@tonic-gate sp->sd_verify_ctx = NULL;
42630Sstevel@tonic-gate
42646424Skrishna release_minor:
42656424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
42660Sstevel@tonic-gate CRYPTO_SESSION_RELE(sp);
42670Sstevel@tonic-gate crypto_release_minor(cm);
42680Sstevel@tonic-gate
42690Sstevel@tonic-gate if (sign.cd_raw.iov_base != NULL)
42700Sstevel@tonic-gate kmem_free(sign.cd_raw.iov_base, signlen);
42710Sstevel@tonic-gate
42720Sstevel@tonic-gate if (error != 0)
42730Sstevel@tonic-gate return (error);
42740Sstevel@tonic-gate
42750Sstevel@tonic-gate STRUCT_FSET(verify_final, vf_return_value, rv);
42760Sstevel@tonic-gate if (copyout(STRUCT_BUF(verify_final), arg,
42770Sstevel@tonic-gate STRUCT_SIZE(verify_final)) != 0) {
42780Sstevel@tonic-gate return (EFAULT);
42790Sstevel@tonic-gate }
42800Sstevel@tonic-gate return (0);
42810Sstevel@tonic-gate }
42820Sstevel@tonic-gate
42830Sstevel@tonic-gate /* ARGSUSED */
42840Sstevel@tonic-gate static int
seed_random(dev_t dev,caddr_t arg,int mode,int * rval)42850Sstevel@tonic-gate seed_random(dev_t dev, caddr_t arg, int mode, int *rval)
42860Sstevel@tonic-gate {
42870Sstevel@tonic-gate STRUCT_DECL(crypto_seed_random, seed_random);
4288904Smcpowers kcf_provider_desc_t *real_provider = NULL;
42890Sstevel@tonic-gate kcf_req_params_t params;
42900Sstevel@tonic-gate crypto_session_id_t session_id;
42910Sstevel@tonic-gate crypto_minor_t *cm;
42926424Skrishna crypto_session_data_t *sp = NULL;
42930Sstevel@tonic-gate uchar_t *seed_buffer = NULL;
42940Sstevel@tonic-gate size_t seed_len;
42950Sstevel@tonic-gate size_t need = 0;
42960Sstevel@tonic-gate int error = 0;
42970Sstevel@tonic-gate int rv;
42986424Skrishna boolean_t rctl_chk = B_FALSE;
42990Sstevel@tonic-gate
43000Sstevel@tonic-gate STRUCT_INIT(seed_random, mode);
43010Sstevel@tonic-gate
43020Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
43030Sstevel@tonic-gate cmn_err(CE_WARN, "seed_random: failed holding minor");
43040Sstevel@tonic-gate return (ENXIO);
43050Sstevel@tonic-gate }
43060Sstevel@tonic-gate
43070Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(seed_random),
43080Sstevel@tonic-gate STRUCT_SIZE(seed_random)) != 0) {
43090Sstevel@tonic-gate crypto_release_minor(cm);
43100Sstevel@tonic-gate return (EFAULT);
43110Sstevel@tonic-gate }
43120Sstevel@tonic-gate
43130Sstevel@tonic-gate seed_len = STRUCT_FGET(seed_random, sr_seedlen);
43140Sstevel@tonic-gate if (seed_len > crypto_max_buffer_len) {
43150Sstevel@tonic-gate cmn_err(CE_NOTE, "seed_random: buffer greater than %ld "
43160Sstevel@tonic-gate "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
43170Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD;
43180Sstevel@tonic-gate goto release_minor;
43190Sstevel@tonic-gate }
43200Sstevel@tonic-gate
43216424Skrishna session_id = STRUCT_FGET(seed_random, sr_session);
43226424Skrishna
43236424Skrishna if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
43246424Skrishna goto release_minor;
43256424Skrishna }
43266424Skrishna
43276424Skrishna if ((rv = CRYPTO_BUFFER_CHECK(sp, seed_len, rctl_chk)) !=
43286424Skrishna CRYPTO_SUCCESS) {
43290Sstevel@tonic-gate goto release_minor;
43300Sstevel@tonic-gate }
43310Sstevel@tonic-gate need = seed_len;
43320Sstevel@tonic-gate seed_buffer = kmem_alloc(seed_len, KM_SLEEP);
43330Sstevel@tonic-gate
43340Sstevel@tonic-gate if (seed_len != 0 && copyin(STRUCT_FGETP(seed_random, sr_seedbuf),
43350Sstevel@tonic-gate seed_buffer, seed_len) != 0) {
43360Sstevel@tonic-gate error = EFAULT;
43370Sstevel@tonic-gate goto release_minor;
43380Sstevel@tonic-gate }
43390Sstevel@tonic-gate
43401808Smcpowers if ((rv = kcf_get_hardware_provider_nomech(
4341904Smcpowers CRYPTO_OPS_OFFSET(random_ops), CRYPTO_RANDOM_OFFSET(seed_random),
4342*12304SValerie.Fenwick@Oracle.COM sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
43436424Skrishna goto release_minor;
43440Sstevel@tonic-gate }
43450Sstevel@tonic-gate
43460Sstevel@tonic-gate KCF_WRAP_RANDOM_OPS_PARAMS(¶ms, KCF_OP_RANDOM_SEED,
43471920Smcpowers sp->sd_provider_session->ps_session, seed_buffer, seed_len, 0,
43481920Smcpowers CRYPTO_SEED_NOW);
43490Sstevel@tonic-gate
43500Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
43510Sstevel@tonic-gate
43526424Skrishna release_minor:
43536424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
43540Sstevel@tonic-gate CRYPTO_SESSION_RELE(sp);
43550Sstevel@tonic-gate crypto_release_minor(cm);
43560Sstevel@tonic-gate
4357904Smcpowers if (real_provider != NULL)
4358904Smcpowers KCF_PROV_REFRELE(real_provider);
4359904Smcpowers
43600Sstevel@tonic-gate if (seed_buffer != NULL)
43610Sstevel@tonic-gate kmem_free(seed_buffer, seed_len);
43620Sstevel@tonic-gate
43630Sstevel@tonic-gate if (error != 0)
43640Sstevel@tonic-gate return (error);
43650Sstevel@tonic-gate
43660Sstevel@tonic-gate STRUCT_FSET(seed_random, sr_return_value, rv);
43670Sstevel@tonic-gate if (copyout(STRUCT_BUF(seed_random), arg,
43680Sstevel@tonic-gate STRUCT_SIZE(seed_random)) != 0) {
43690Sstevel@tonic-gate return (EFAULT);
43700Sstevel@tonic-gate }
43710Sstevel@tonic-gate return (0);
43720Sstevel@tonic-gate }
43730Sstevel@tonic-gate
43740Sstevel@tonic-gate /* ARGSUSED */
43750Sstevel@tonic-gate static int
generate_random(dev_t dev,caddr_t arg,int mode,int * rval)43760Sstevel@tonic-gate generate_random(dev_t dev, caddr_t arg, int mode, int *rval)
43770Sstevel@tonic-gate {
43780Sstevel@tonic-gate STRUCT_DECL(crypto_generate_random, generate_random);
4379904Smcpowers kcf_provider_desc_t *real_provider = NULL;
43800Sstevel@tonic-gate kcf_req_params_t params;
43810Sstevel@tonic-gate crypto_session_id_t session_id;
43820Sstevel@tonic-gate crypto_minor_t *cm;
43836424Skrishna crypto_session_data_t *sp = NULL;
43840Sstevel@tonic-gate uchar_t *buffer = NULL;
43850Sstevel@tonic-gate size_t len;
43860Sstevel@tonic-gate size_t need = 0;
43870Sstevel@tonic-gate int error = 0;
43880Sstevel@tonic-gate int rv;
43896424Skrishna boolean_t rctl_chk = B_FALSE;
43900Sstevel@tonic-gate
43910Sstevel@tonic-gate STRUCT_INIT(generate_random, mode);
43920Sstevel@tonic-gate
43930Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
43940Sstevel@tonic-gate cmn_err(CE_WARN, "generate_random: failed holding minor");
43950Sstevel@tonic-gate return (ENXIO);
43960Sstevel@tonic-gate }
43970Sstevel@tonic-gate
43980Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(generate_random),
43990Sstevel@tonic-gate STRUCT_SIZE(generate_random)) != 0) {
44000Sstevel@tonic-gate crypto_release_minor(cm);
44010Sstevel@tonic-gate return (EFAULT);
44020Sstevel@tonic-gate }
44030Sstevel@tonic-gate
44040Sstevel@tonic-gate len = STRUCT_FGET(generate_random, gr_buflen);
44050Sstevel@tonic-gate if (len > crypto_max_buffer_len) {
44060Sstevel@tonic-gate cmn_err(CE_NOTE, "generate_random: buffer greater than %ld "
44070Sstevel@tonic-gate "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
44080Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD;
44090Sstevel@tonic-gate goto release_minor;
44100Sstevel@tonic-gate }
44110Sstevel@tonic-gate
44126424Skrishna session_id = STRUCT_FGET(generate_random, gr_session);
44136424Skrishna
44146424Skrishna if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
44156424Skrishna goto release_minor;
44166424Skrishna }
44176424Skrishna
44186424Skrishna if ((rv = CRYPTO_BUFFER_CHECK(sp, len, rctl_chk)) !=
44196424Skrishna CRYPTO_SUCCESS) {
44200Sstevel@tonic-gate goto release_minor;
44210Sstevel@tonic-gate }
44220Sstevel@tonic-gate need = len;
44230Sstevel@tonic-gate buffer = kmem_alloc(len, KM_SLEEP);
44240Sstevel@tonic-gate
44251808Smcpowers if ((rv = kcf_get_hardware_provider_nomech(
44260Sstevel@tonic-gate CRYPTO_OPS_OFFSET(random_ops),
4427*12304SValerie.Fenwick@Oracle.COM CRYPTO_RANDOM_OFFSET(generate_random), sp->sd_provider,
4428*12304SValerie.Fenwick@Oracle.COM &real_provider)) != CRYPTO_SUCCESS) {
44296424Skrishna goto release_minor;
44300Sstevel@tonic-gate }
44310Sstevel@tonic-gate
44320Sstevel@tonic-gate KCF_WRAP_RANDOM_OPS_PARAMS(¶ms, KCF_OP_RANDOM_GENERATE,
44331920Smcpowers sp->sd_provider_session->ps_session, buffer, len, 0, 0);
44340Sstevel@tonic-gate
44350Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
44360Sstevel@tonic-gate
44370Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) {
44380Sstevel@tonic-gate if (len != 0 && copyout(buffer,
44390Sstevel@tonic-gate STRUCT_FGETP(generate_random, gr_buf), len) != 0) {
44400Sstevel@tonic-gate error = EFAULT;
44410Sstevel@tonic-gate }
44420Sstevel@tonic-gate }
44430Sstevel@tonic-gate
44440Sstevel@tonic-gate release_minor:
44456424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, need, rctl_chk);
44466424Skrishna CRYPTO_SESSION_RELE(sp);
44470Sstevel@tonic-gate crypto_release_minor(cm);
44480Sstevel@tonic-gate
4449904Smcpowers if (real_provider != NULL)
4450904Smcpowers KCF_PROV_REFRELE(real_provider);
4451904Smcpowers
44520Sstevel@tonic-gate if (buffer != NULL) {
44530Sstevel@tonic-gate /* random numbers are often used to create keys */
44540Sstevel@tonic-gate bzero(buffer, len);
44550Sstevel@tonic-gate kmem_free(buffer, len);
44560Sstevel@tonic-gate }
44570Sstevel@tonic-gate
44580Sstevel@tonic-gate if (error != 0)
44590Sstevel@tonic-gate return (error);
44600Sstevel@tonic-gate
44610Sstevel@tonic-gate STRUCT_FSET(generate_random, gr_return_value, rv);
44620Sstevel@tonic-gate if (copyout(STRUCT_BUF(generate_random), arg,
44630Sstevel@tonic-gate STRUCT_SIZE(generate_random)) != 0) {
44640Sstevel@tonic-gate return (EFAULT);
44650Sstevel@tonic-gate }
44660Sstevel@tonic-gate return (0);
44670Sstevel@tonic-gate }
44680Sstevel@tonic-gate
44690Sstevel@tonic-gate /*
44700Sstevel@tonic-gate * Copyout a kernel array of attributes to user space.
44710Sstevel@tonic-gate * u_attrs is the corresponding user space array containing
44720Sstevel@tonic-gate * user space pointers necessary for the copyout.
44730Sstevel@tonic-gate */
44740Sstevel@tonic-gate /* ARGSUSED */
44750Sstevel@tonic-gate static int
copyout_attributes(int mode,caddr_t out,uint_t count,crypto_object_attribute_t * k_attrs,caddr_t u_attrs)44760Sstevel@tonic-gate copyout_attributes(int mode, caddr_t out, uint_t count,
44770Sstevel@tonic-gate crypto_object_attribute_t *k_attrs, caddr_t u_attrs)
44780Sstevel@tonic-gate {
44790Sstevel@tonic-gate STRUCT_DECL(crypto_object_attribute, oa);
44800Sstevel@tonic-gate caddr_t p, valuep;
44810Sstevel@tonic-gate size_t value_len;
44820Sstevel@tonic-gate size_t len;
44830Sstevel@tonic-gate int i;
44840Sstevel@tonic-gate int error = 0;
44850Sstevel@tonic-gate
44860Sstevel@tonic-gate if (count == 0)
44870Sstevel@tonic-gate return (0);
44880Sstevel@tonic-gate
44890Sstevel@tonic-gate STRUCT_INIT(oa, mode);
44900Sstevel@tonic-gate
44910Sstevel@tonic-gate len = count * STRUCT_SIZE(oa);
44920Sstevel@tonic-gate
44930Sstevel@tonic-gate ASSERT(u_attrs != NULL);
44940Sstevel@tonic-gate p = u_attrs;
44950Sstevel@tonic-gate for (i = 0; i < count; i++) {
44960Sstevel@tonic-gate /* can this bcopy be eliminated? */
44970Sstevel@tonic-gate bcopy(p, STRUCT_BUF(oa), STRUCT_SIZE(oa));
44980Sstevel@tonic-gate value_len = k_attrs[i].oa_value_len;
44990Sstevel@tonic-gate STRUCT_FSET(oa, oa_type, k_attrs[i].oa_type);
450011413Sopensolaris@drydog.com STRUCT_FSET(oa, oa_value_len, (ssize_t)value_len);
45010Sstevel@tonic-gate valuep = STRUCT_FGETP(oa, oa_value);
450211413Sopensolaris@drydog.com if ((valuep != NULL) && (value_len != (size_t)-1)) {
45030Sstevel@tonic-gate if (copyout(k_attrs[i].oa_value,
45040Sstevel@tonic-gate valuep, value_len) != 0) {
45050Sstevel@tonic-gate error = EFAULT;
45060Sstevel@tonic-gate goto out;
45070Sstevel@tonic-gate }
45080Sstevel@tonic-gate }
45090Sstevel@tonic-gate bcopy(STRUCT_BUF(oa), p, STRUCT_SIZE(oa));
45100Sstevel@tonic-gate p += STRUCT_SIZE(oa);
45110Sstevel@tonic-gate }
45120Sstevel@tonic-gate if (copyout(u_attrs, out, len)) {
45130Sstevel@tonic-gate error = EFAULT;
45140Sstevel@tonic-gate }
45150Sstevel@tonic-gate out:
45160Sstevel@tonic-gate return (error);
45170Sstevel@tonic-gate }
45180Sstevel@tonic-gate
45190Sstevel@tonic-gate
45200Sstevel@tonic-gate /* ARGSUSED */
45210Sstevel@tonic-gate static int
object_create(dev_t dev,caddr_t arg,int mode,int * rval)45220Sstevel@tonic-gate object_create(dev_t dev, caddr_t arg, int mode, int *rval)
45230Sstevel@tonic-gate {
45240Sstevel@tonic-gate STRUCT_DECL(crypto_object_create, object_create);
4525904Smcpowers kcf_provider_desc_t *real_provider = NULL;
45260Sstevel@tonic-gate kcf_req_params_t params;
45270Sstevel@tonic-gate crypto_object_attribute_t *k_attrs = NULL;
45280Sstevel@tonic-gate crypto_session_id_t session_id;
45290Sstevel@tonic-gate crypto_minor_t *cm;
45300Sstevel@tonic-gate crypto_session_data_t *sp = NULL;
45310Sstevel@tonic-gate crypto_object_id_t object_handle;
45320Sstevel@tonic-gate caddr_t oc_attributes;
45330Sstevel@tonic-gate size_t k_attrs_size;
45340Sstevel@tonic-gate size_t rctl_bytes = 0;
45356424Skrishna boolean_t rctl_chk = B_FALSE;
45360Sstevel@tonic-gate int error = 0;
45370Sstevel@tonic-gate int rv;
45380Sstevel@tonic-gate uint_t count;
45390Sstevel@tonic-gate
45400Sstevel@tonic-gate STRUCT_INIT(object_create, mode);
45410Sstevel@tonic-gate
45420Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
45430Sstevel@tonic-gate cmn_err(CE_WARN, "object_create: failed holding minor");
45440Sstevel@tonic-gate return (ENXIO);
45450Sstevel@tonic-gate }
45460Sstevel@tonic-gate
45470Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(object_create),
45480Sstevel@tonic-gate STRUCT_SIZE(object_create)) != 0) {
45490Sstevel@tonic-gate crypto_release_minor(cm);
45500Sstevel@tonic-gate return (EFAULT);
45510Sstevel@tonic-gate }
45520Sstevel@tonic-gate
45530Sstevel@tonic-gate count = STRUCT_FGET(object_create, oc_count);
45540Sstevel@tonic-gate oc_attributes = STRUCT_FGETP(object_create, oc_attributes);
45550Sstevel@tonic-gate
45560Sstevel@tonic-gate session_id = STRUCT_FGET(object_create, oc_session);
45570Sstevel@tonic-gate if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
45580Sstevel@tonic-gate goto release_minor;
45590Sstevel@tonic-gate }
45606424Skrishna if (!copyin_attributes(mode, sp, count, oc_attributes, &k_attrs,
45616424Skrishna &k_attrs_size, NULL, &rv, &error, &rctl_bytes,
45626424Skrishna &rctl_chk, B_TRUE)) {
45636424Skrishna goto release_minor;
45646424Skrishna }
45650Sstevel@tonic-gate
45660Sstevel@tonic-gate if ((rv = kcf_get_hardware_provider_nomech(
45670Sstevel@tonic-gate CRYPTO_OPS_OFFSET(object_ops),
4568*12304SValerie.Fenwick@Oracle.COM CRYPTO_OBJECT_OFFSET(object_create), sp->sd_provider,
4569*12304SValerie.Fenwick@Oracle.COM &real_provider)) != CRYPTO_SUCCESS) {
45700Sstevel@tonic-gate goto release_minor;
45710Sstevel@tonic-gate }
45720Sstevel@tonic-gate
45730Sstevel@tonic-gate KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, KCF_OP_OBJECT_CREATE,
45740Sstevel@tonic-gate sp->sd_provider_session->ps_session, 0, k_attrs, count,
45750Sstevel@tonic-gate &object_handle, 0, NULL, NULL, 0, NULL);
45760Sstevel@tonic-gate
45770Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
45780Sstevel@tonic-gate
45790Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS)
45800Sstevel@tonic-gate STRUCT_FSET(object_create, oc_handle, object_handle);
45810Sstevel@tonic-gate
45820Sstevel@tonic-gate release_minor:
45836424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk);
45840Sstevel@tonic-gate
45850Sstevel@tonic-gate if (k_attrs != NULL)
45860Sstevel@tonic-gate kmem_free(k_attrs, k_attrs_size);
45870Sstevel@tonic-gate
45880Sstevel@tonic-gate if (error != 0)
45890Sstevel@tonic-gate goto out;
45900Sstevel@tonic-gate
45910Sstevel@tonic-gate STRUCT_FSET(object_create, oc_return_value, rv);
45920Sstevel@tonic-gate if (copyout(STRUCT_BUF(object_create), arg,
45930Sstevel@tonic-gate STRUCT_SIZE(object_create)) != 0) {
45940Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) {
45950Sstevel@tonic-gate KCF_WRAP_OBJECT_OPS_PARAMS(¶ms,
45960Sstevel@tonic-gate KCF_OP_OBJECT_DESTROY,
45970Sstevel@tonic-gate sp->sd_provider_session->ps_session, object_handle,
45980Sstevel@tonic-gate NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
45990Sstevel@tonic-gate
46000Sstevel@tonic-gate (void) kcf_submit_request(real_provider, NULL,
46010Sstevel@tonic-gate NULL, ¶ms, B_FALSE);
46020Sstevel@tonic-gate
46030Sstevel@tonic-gate error = EFAULT;
46040Sstevel@tonic-gate }
46050Sstevel@tonic-gate }
46060Sstevel@tonic-gate out:
46076424Skrishna CRYPTO_SESSION_RELE(sp);
46080Sstevel@tonic-gate crypto_release_minor(cm);
4609904Smcpowers if (real_provider != NULL)
4610904Smcpowers KCF_PROV_REFRELE(real_provider);
46110Sstevel@tonic-gate return (error);
46120Sstevel@tonic-gate }
46130Sstevel@tonic-gate
46140Sstevel@tonic-gate /* ARGSUSED */
46150Sstevel@tonic-gate static int
object_copy(dev_t dev,caddr_t arg,int mode,int * rval)46160Sstevel@tonic-gate object_copy(dev_t dev, caddr_t arg, int mode, int *rval)
46170Sstevel@tonic-gate {
46180Sstevel@tonic-gate STRUCT_DECL(crypto_object_copy, object_copy);
4619904Smcpowers kcf_provider_desc_t *real_provider = NULL;
46200Sstevel@tonic-gate kcf_req_params_t params;
46210Sstevel@tonic-gate crypto_object_attribute_t *k_attrs = NULL;
46220Sstevel@tonic-gate crypto_session_id_t session_id;
46230Sstevel@tonic-gate crypto_minor_t *cm;
46240Sstevel@tonic-gate crypto_session_data_t *sp = NULL;
46250Sstevel@tonic-gate crypto_object_id_t handle, new_handle;
46260Sstevel@tonic-gate caddr_t oc_new_attributes;
46270Sstevel@tonic-gate size_t k_attrs_size;
46280Sstevel@tonic-gate size_t rctl_bytes = 0;
46296424Skrishna boolean_t rctl_chk = B_FALSE;
46300Sstevel@tonic-gate int error = 0;
46310Sstevel@tonic-gate int rv;
46320Sstevel@tonic-gate uint_t count;
46330Sstevel@tonic-gate
46340Sstevel@tonic-gate STRUCT_INIT(object_copy, mode);
46350Sstevel@tonic-gate
46360Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
46370Sstevel@tonic-gate cmn_err(CE_WARN, "object_copy: failed holding minor");
46380Sstevel@tonic-gate return (ENXIO);
46390Sstevel@tonic-gate }
46400Sstevel@tonic-gate
46410Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(object_copy),
46420Sstevel@tonic-gate STRUCT_SIZE(object_copy)) != 0) {
46430Sstevel@tonic-gate crypto_release_minor(cm);
46440Sstevel@tonic-gate return (EFAULT);
46450Sstevel@tonic-gate }
46460Sstevel@tonic-gate
46470Sstevel@tonic-gate count = STRUCT_FGET(object_copy, oc_count);
46480Sstevel@tonic-gate oc_new_attributes = STRUCT_FGETP(object_copy, oc_new_attributes);
46490Sstevel@tonic-gate
46500Sstevel@tonic-gate session_id = STRUCT_FGET(object_copy, oc_session);
46510Sstevel@tonic-gate
46520Sstevel@tonic-gate if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
46530Sstevel@tonic-gate goto release_minor;
46540Sstevel@tonic-gate }
46556424Skrishna if (!copyin_attributes(mode, sp, count, oc_new_attributes, &k_attrs,
46566424Skrishna &k_attrs_size, NULL, &rv, &error, &rctl_bytes,
46576424Skrishna &rctl_chk, B_TRUE)) {
46586424Skrishna goto release_minor;
46596424Skrishna }
46600Sstevel@tonic-gate
46610Sstevel@tonic-gate if ((rv = kcf_get_hardware_provider_nomech(
46620Sstevel@tonic-gate CRYPTO_OPS_OFFSET(object_ops),
4663*12304SValerie.Fenwick@Oracle.COM CRYPTO_OBJECT_OFFSET(object_copy), sp->sd_provider,
4664*12304SValerie.Fenwick@Oracle.COM &real_provider)) != CRYPTO_SUCCESS) {
46650Sstevel@tonic-gate goto release_minor;
46660Sstevel@tonic-gate }
46670Sstevel@tonic-gate
46680Sstevel@tonic-gate handle = STRUCT_FGET(object_copy, oc_handle);
46690Sstevel@tonic-gate KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, KCF_OP_OBJECT_COPY,
46700Sstevel@tonic-gate sp->sd_provider_session->ps_session, handle, k_attrs, count,
46710Sstevel@tonic-gate &new_handle, 0, NULL, NULL, 0, NULL);
46720Sstevel@tonic-gate
46730Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
46740Sstevel@tonic-gate
46750Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS)
46760Sstevel@tonic-gate STRUCT_FSET(object_copy, oc_new_handle, new_handle);
46770Sstevel@tonic-gate
46780Sstevel@tonic-gate release_minor:
46796424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk);
46800Sstevel@tonic-gate
46810Sstevel@tonic-gate if (k_attrs != NULL)
46820Sstevel@tonic-gate kmem_free(k_attrs, k_attrs_size);
46830Sstevel@tonic-gate
46840Sstevel@tonic-gate if (error != 0)
46850Sstevel@tonic-gate goto out;
46860Sstevel@tonic-gate
46870Sstevel@tonic-gate STRUCT_FSET(object_copy, oc_return_value, rv);
46880Sstevel@tonic-gate if (copyout(STRUCT_BUF(object_copy), arg,
46890Sstevel@tonic-gate STRUCT_SIZE(object_copy)) != 0) {
46900Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) {
46910Sstevel@tonic-gate KCF_WRAP_OBJECT_OPS_PARAMS(¶ms,
46920Sstevel@tonic-gate KCF_OP_OBJECT_DESTROY,
46930Sstevel@tonic-gate sp->sd_provider_session->ps_session, new_handle,
46940Sstevel@tonic-gate NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
46950Sstevel@tonic-gate
46960Sstevel@tonic-gate (void) kcf_submit_request(real_provider, NULL,
46970Sstevel@tonic-gate NULL, ¶ms, B_FALSE);
46980Sstevel@tonic-gate
46990Sstevel@tonic-gate error = EFAULT;
47000Sstevel@tonic-gate }
47010Sstevel@tonic-gate }
47020Sstevel@tonic-gate out:
47036424Skrishna CRYPTO_SESSION_RELE(sp);
47040Sstevel@tonic-gate crypto_release_minor(cm);
4705904Smcpowers if (real_provider != NULL)
4706904Smcpowers KCF_PROV_REFRELE(real_provider);
47070Sstevel@tonic-gate return (error);
47080Sstevel@tonic-gate }
47090Sstevel@tonic-gate
47100Sstevel@tonic-gate /* ARGSUSED */
47110Sstevel@tonic-gate static int
object_destroy(dev_t dev,caddr_t arg,int mode,int * rval)47120Sstevel@tonic-gate object_destroy(dev_t dev, caddr_t arg, int mode, int *rval)
47130Sstevel@tonic-gate {
47140Sstevel@tonic-gate STRUCT_DECL(crypto_object_destroy, object_destroy);
47150Sstevel@tonic-gate kcf_provider_desc_t *real_provider;
47160Sstevel@tonic-gate kcf_req_params_t params;
47170Sstevel@tonic-gate crypto_session_id_t session_id;
47180Sstevel@tonic-gate crypto_minor_t *cm;
47190Sstevel@tonic-gate crypto_session_data_t *sp;
47200Sstevel@tonic-gate crypto_object_id_t handle;
47210Sstevel@tonic-gate int error = 0;
47220Sstevel@tonic-gate int rv;
47230Sstevel@tonic-gate
47240Sstevel@tonic-gate STRUCT_INIT(object_destroy, mode);
47250Sstevel@tonic-gate
47260Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
47270Sstevel@tonic-gate cmn_err(CE_WARN, "object_destroy: failed holding minor");
47280Sstevel@tonic-gate return (ENXIO);
47290Sstevel@tonic-gate }
47300Sstevel@tonic-gate
47310Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(object_destroy),
47320Sstevel@tonic-gate STRUCT_SIZE(object_destroy)) != 0) {
47330Sstevel@tonic-gate crypto_release_minor(cm);
47340Sstevel@tonic-gate return (EFAULT);
47350Sstevel@tonic-gate }
47360Sstevel@tonic-gate
47370Sstevel@tonic-gate session_id = STRUCT_FGET(object_destroy, od_session);
47380Sstevel@tonic-gate
47390Sstevel@tonic-gate if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
47400Sstevel@tonic-gate goto release_minor;
47410Sstevel@tonic-gate }
47420Sstevel@tonic-gate
47430Sstevel@tonic-gate if ((rv = kcf_get_hardware_provider_nomech(
47440Sstevel@tonic-gate CRYPTO_OPS_OFFSET(object_ops),
4745*12304SValerie.Fenwick@Oracle.COM CRYPTO_OBJECT_OFFSET(object_destroy), sp->sd_provider,
4746*12304SValerie.Fenwick@Oracle.COM &real_provider)) != CRYPTO_SUCCESS) {
47470Sstevel@tonic-gate goto out;
47480Sstevel@tonic-gate }
47490Sstevel@tonic-gate
47500Sstevel@tonic-gate handle = STRUCT_FGET(object_destroy, od_handle);
47510Sstevel@tonic-gate KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, KCF_OP_OBJECT_DESTROY,
47520Sstevel@tonic-gate sp->sd_provider_session->ps_session, handle, NULL, 0, NULL, 0,
47530Sstevel@tonic-gate NULL, NULL, 0, NULL);
47540Sstevel@tonic-gate
47550Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
4756904Smcpowers KCF_PROV_REFRELE(real_provider);
47570Sstevel@tonic-gate
47580Sstevel@tonic-gate out:
47590Sstevel@tonic-gate CRYPTO_SESSION_RELE(sp);
47600Sstevel@tonic-gate
47610Sstevel@tonic-gate release_minor:
47620Sstevel@tonic-gate crypto_release_minor(cm);
47630Sstevel@tonic-gate
47640Sstevel@tonic-gate if (error != 0)
47650Sstevel@tonic-gate return (error);
47660Sstevel@tonic-gate
47670Sstevel@tonic-gate STRUCT_FSET(object_destroy, od_return_value, rv);
47680Sstevel@tonic-gate
47690Sstevel@tonic-gate if (copyout(STRUCT_BUF(object_destroy), arg,
47700Sstevel@tonic-gate STRUCT_SIZE(object_destroy)) != 0) {
47710Sstevel@tonic-gate return (EFAULT);
47720Sstevel@tonic-gate }
47730Sstevel@tonic-gate return (0);
47740Sstevel@tonic-gate }
47750Sstevel@tonic-gate
47760Sstevel@tonic-gate /* ARGSUSED */
47770Sstevel@tonic-gate static int
object_get_attribute_value(dev_t dev,caddr_t arg,int mode,int * rval)47780Sstevel@tonic-gate object_get_attribute_value(dev_t dev, caddr_t arg, int mode, int *rval)
47790Sstevel@tonic-gate {
47800Sstevel@tonic-gate STRUCT_DECL(crypto_object_get_attribute_value, get_attribute_value);
478111413Sopensolaris@drydog.com #ifdef _LP64
478211413Sopensolaris@drydog.com STRUCT_DECL(crypto_object_attribute, oa);
478311413Sopensolaris@drydog.com #else
47840Sstevel@tonic-gate /* LINTED E_FUNC_SET_NOT_USED */
47850Sstevel@tonic-gate STRUCT_DECL(crypto_object_attribute, oa);
478611413Sopensolaris@drydog.com #endif
47870Sstevel@tonic-gate kcf_provider_desc_t *real_provider;
47880Sstevel@tonic-gate kcf_req_params_t params;
47890Sstevel@tonic-gate crypto_object_attribute_t *k_attrs = NULL;
47900Sstevel@tonic-gate crypto_session_id_t session_id;
47910Sstevel@tonic-gate crypto_minor_t *cm;
47926424Skrishna crypto_session_data_t *sp = NULL;
47930Sstevel@tonic-gate crypto_object_id_t handle;
47940Sstevel@tonic-gate caddr_t og_attributes;
47958043SMark.Powers@Sun.COM caddr_t u_attrs = NULL;
47960Sstevel@tonic-gate size_t k_attrs_size;
47970Sstevel@tonic-gate size_t rctl_bytes = 0;
47986424Skrishna boolean_t rctl_chk = B_FALSE;
47990Sstevel@tonic-gate int error = 0;
48000Sstevel@tonic-gate int rv;
48010Sstevel@tonic-gate uint_t count;
48020Sstevel@tonic-gate
48030Sstevel@tonic-gate STRUCT_INIT(get_attribute_value, mode);
48040Sstevel@tonic-gate STRUCT_INIT(oa, mode);
48050Sstevel@tonic-gate
48060Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
48070Sstevel@tonic-gate cmn_err(CE_WARN,
48080Sstevel@tonic-gate "object_get_attribute_value: failed holding minor");
48090Sstevel@tonic-gate return (ENXIO);
48100Sstevel@tonic-gate }
48110Sstevel@tonic-gate
48120Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(get_attribute_value),
48130Sstevel@tonic-gate STRUCT_SIZE(get_attribute_value)) != 0) {
48140Sstevel@tonic-gate crypto_release_minor(cm);
48150Sstevel@tonic-gate return (EFAULT);
48160Sstevel@tonic-gate }
48170Sstevel@tonic-gate
48180Sstevel@tonic-gate count = STRUCT_FGET(get_attribute_value, og_count);
48190Sstevel@tonic-gate og_attributes = STRUCT_FGETP(get_attribute_value, og_attributes);
48200Sstevel@tonic-gate
48210Sstevel@tonic-gate session_id = STRUCT_FGET(get_attribute_value, og_session);
48220Sstevel@tonic-gate
48230Sstevel@tonic-gate if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
48240Sstevel@tonic-gate goto release_minor;
48250Sstevel@tonic-gate }
48266424Skrishna if (!copyin_attributes(mode, sp, count, og_attributes, &k_attrs,
48276424Skrishna &k_attrs_size, &u_attrs, &rv, &error, &rctl_bytes,
48286424Skrishna &rctl_chk, B_FALSE)) {
48296424Skrishna goto release_minor;
48306424Skrishna }
48310Sstevel@tonic-gate
48320Sstevel@tonic-gate if ((rv = kcf_get_hardware_provider_nomech(
48330Sstevel@tonic-gate CRYPTO_OPS_OFFSET(object_ops),
48340Sstevel@tonic-gate CRYPTO_OBJECT_OFFSET(object_get_attribute_value),
4835*12304SValerie.Fenwick@Oracle.COM sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
48360Sstevel@tonic-gate goto out;
48370Sstevel@tonic-gate }
48380Sstevel@tonic-gate
48390Sstevel@tonic-gate handle = STRUCT_FGET(get_attribute_value, og_handle);
48400Sstevel@tonic-gate KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, KCF_OP_OBJECT_GET_ATTRIBUTE_VALUE,
48410Sstevel@tonic-gate sp->sd_provider_session->ps_session, handle, k_attrs, count, NULL,
48420Sstevel@tonic-gate 0, NULL, NULL, 0, NULL);
48430Sstevel@tonic-gate
48440Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
4845904Smcpowers KCF_PROV_REFRELE(real_provider);
48460Sstevel@tonic-gate
48470Sstevel@tonic-gate out:
48480Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS || rv == CRYPTO_ATTRIBUTE_SENSITIVE ||
48490Sstevel@tonic-gate rv == CRYPTO_ATTRIBUTE_TYPE_INVALID ||
48500Sstevel@tonic-gate rv == CRYPTO_BUFFER_TOO_SMALL) {
48510Sstevel@tonic-gate error = copyout_attributes(mode,
48520Sstevel@tonic-gate STRUCT_FGETP(get_attribute_value, og_attributes),
48530Sstevel@tonic-gate count, k_attrs, u_attrs);
48540Sstevel@tonic-gate }
48550Sstevel@tonic-gate
48560Sstevel@tonic-gate release_minor:
48576424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk);
48586424Skrishna CRYPTO_SESSION_RELE(sp);
48590Sstevel@tonic-gate crypto_release_minor(cm);
48600Sstevel@tonic-gate
48610Sstevel@tonic-gate if (k_attrs != NULL)
48620Sstevel@tonic-gate kmem_free(k_attrs, k_attrs_size);
48630Sstevel@tonic-gate
48640Sstevel@tonic-gate if (u_attrs != NULL)
48650Sstevel@tonic-gate kmem_free(u_attrs, count * STRUCT_SIZE(oa));
48660Sstevel@tonic-gate
48670Sstevel@tonic-gate if (error != 0)
48680Sstevel@tonic-gate return (error);
48690Sstevel@tonic-gate
48700Sstevel@tonic-gate STRUCT_FSET(get_attribute_value, og_return_value, rv);
48710Sstevel@tonic-gate if (copyout(STRUCT_BUF(get_attribute_value), arg,
48720Sstevel@tonic-gate STRUCT_SIZE(get_attribute_value)) != 0) {
48730Sstevel@tonic-gate return (EFAULT);
48740Sstevel@tonic-gate }
48750Sstevel@tonic-gate return (0);
48760Sstevel@tonic-gate }
48770Sstevel@tonic-gate
48780Sstevel@tonic-gate /* ARGSUSED */
48790Sstevel@tonic-gate static int
object_get_size(dev_t dev,caddr_t arg,int mode,int * rval)48800Sstevel@tonic-gate object_get_size(dev_t dev, caddr_t arg, int mode, int *rval)
48810Sstevel@tonic-gate {
48820Sstevel@tonic-gate STRUCT_DECL(crypto_object_get_size, object_get_size);
48830Sstevel@tonic-gate kcf_provider_desc_t *real_provider;
48840Sstevel@tonic-gate kcf_req_params_t params;
48850Sstevel@tonic-gate crypto_session_id_t session_id;
48860Sstevel@tonic-gate crypto_minor_t *cm;
48876424Skrishna crypto_session_data_t *sp = NULL;
48880Sstevel@tonic-gate crypto_object_id_t handle;
48890Sstevel@tonic-gate size_t size;
48900Sstevel@tonic-gate int error = 0;
48910Sstevel@tonic-gate int rv;
48920Sstevel@tonic-gate
48930Sstevel@tonic-gate STRUCT_INIT(object_get_size, mode);
48940Sstevel@tonic-gate
48950Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
48960Sstevel@tonic-gate cmn_err(CE_WARN, "object_get_size: failed holding minor");
48970Sstevel@tonic-gate return (ENXIO);
48980Sstevel@tonic-gate }
48990Sstevel@tonic-gate
49000Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(object_get_size),
49010Sstevel@tonic-gate STRUCT_SIZE(object_get_size)) != 0) {
49020Sstevel@tonic-gate crypto_release_minor(cm);
49030Sstevel@tonic-gate return (EFAULT);
49040Sstevel@tonic-gate }
49050Sstevel@tonic-gate
49060Sstevel@tonic-gate session_id = STRUCT_FGET(object_get_size, gs_session);
49070Sstevel@tonic-gate
49080Sstevel@tonic-gate if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
49090Sstevel@tonic-gate goto release_minor;
49100Sstevel@tonic-gate }
49110Sstevel@tonic-gate
49120Sstevel@tonic-gate if ((rv = kcf_get_hardware_provider_nomech(
49130Sstevel@tonic-gate CRYPTO_OPS_OFFSET(object_ops),
4914*12304SValerie.Fenwick@Oracle.COM CRYPTO_OBJECT_OFFSET(object_get_size),
49150Sstevel@tonic-gate sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
49166424Skrishna goto release_minor;
49170Sstevel@tonic-gate }
49180Sstevel@tonic-gate
49190Sstevel@tonic-gate handle = STRUCT_FGET(object_get_size, gs_handle);
49200Sstevel@tonic-gate KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, KCF_OP_OBJECT_GET_SIZE,
49210Sstevel@tonic-gate sp->sd_provider_session->ps_session, handle, NULL, 0, NULL, &size,
49220Sstevel@tonic-gate NULL, NULL, 0, NULL);
49230Sstevel@tonic-gate
49240Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
4925904Smcpowers KCF_PROV_REFRELE(real_provider);
49260Sstevel@tonic-gate
49270Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) {
492811413Sopensolaris@drydog.com STRUCT_FSET(object_get_size, gs_size, (ulong_t)size);
49290Sstevel@tonic-gate }
49306424Skrishna
49310Sstevel@tonic-gate release_minor:
49320Sstevel@tonic-gate crypto_release_minor(cm);
49336424Skrishna CRYPTO_SESSION_RELE(sp);
49340Sstevel@tonic-gate
49350Sstevel@tonic-gate if (error != 0)
49360Sstevel@tonic-gate return (error);
49370Sstevel@tonic-gate
49380Sstevel@tonic-gate STRUCT_FSET(object_get_size, gs_return_value, rv);
49390Sstevel@tonic-gate if (copyout(STRUCT_BUF(object_get_size), arg,
49400Sstevel@tonic-gate STRUCT_SIZE(object_get_size)) != 0) {
49410Sstevel@tonic-gate return (EFAULT);
49420Sstevel@tonic-gate }
49430Sstevel@tonic-gate return (0);
49440Sstevel@tonic-gate }
49450Sstevel@tonic-gate
49460Sstevel@tonic-gate /* ARGSUSED */
49470Sstevel@tonic-gate static int
object_set_attribute_value(dev_t dev,caddr_t arg,int mode,int * rval)49480Sstevel@tonic-gate object_set_attribute_value(dev_t dev, caddr_t arg, int mode, int *rval)
49490Sstevel@tonic-gate {
49500Sstevel@tonic-gate STRUCT_DECL(crypto_object_set_attribute_value, set_attribute_value);
49510Sstevel@tonic-gate kcf_provider_desc_t *real_provider;
49520Sstevel@tonic-gate kcf_req_params_t params;
49530Sstevel@tonic-gate crypto_object_attribute_t *k_attrs = NULL;
49540Sstevel@tonic-gate crypto_session_id_t session_id;
49550Sstevel@tonic-gate crypto_minor_t *cm;
49566424Skrishna crypto_session_data_t *sp = NULL;
49570Sstevel@tonic-gate crypto_object_id_t object_handle;
49580Sstevel@tonic-gate caddr_t sa_attributes;
49590Sstevel@tonic-gate size_t k_attrs_size;
49600Sstevel@tonic-gate size_t rctl_bytes = 0;
49616424Skrishna boolean_t rctl_chk = B_FALSE;
49620Sstevel@tonic-gate int error = 0;
49630Sstevel@tonic-gate int rv;
49640Sstevel@tonic-gate uint_t count;
49650Sstevel@tonic-gate
49660Sstevel@tonic-gate STRUCT_INIT(set_attribute_value, mode);
49670Sstevel@tonic-gate
49680Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
49690Sstevel@tonic-gate cmn_err(CE_WARN,
49700Sstevel@tonic-gate "object_set_attribute_value: failed holding minor");
49710Sstevel@tonic-gate return (ENXIO);
49720Sstevel@tonic-gate }
49730Sstevel@tonic-gate
49740Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(set_attribute_value),
49750Sstevel@tonic-gate STRUCT_SIZE(set_attribute_value)) != 0) {
49760Sstevel@tonic-gate crypto_release_minor(cm);
49770Sstevel@tonic-gate return (EFAULT);
49780Sstevel@tonic-gate }
49790Sstevel@tonic-gate
49800Sstevel@tonic-gate count = STRUCT_FGET(set_attribute_value, sa_count);
49810Sstevel@tonic-gate sa_attributes = STRUCT_FGETP(set_attribute_value, sa_attributes);
49820Sstevel@tonic-gate
49830Sstevel@tonic-gate session_id = STRUCT_FGET(set_attribute_value, sa_session);
49840Sstevel@tonic-gate
49850Sstevel@tonic-gate if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
49860Sstevel@tonic-gate goto release_minor;
49870Sstevel@tonic-gate }
49886424Skrishna if (!copyin_attributes(mode, sp, count, sa_attributes, &k_attrs,
49896424Skrishna &k_attrs_size, NULL, &rv, &error, &rctl_bytes,
49906424Skrishna &rctl_chk, B_TRUE)) {
49916424Skrishna goto release_minor;
49926424Skrishna }
49930Sstevel@tonic-gate
49940Sstevel@tonic-gate if ((rv = kcf_get_hardware_provider_nomech(
49950Sstevel@tonic-gate CRYPTO_OPS_OFFSET(object_ops),
49960Sstevel@tonic-gate CRYPTO_OBJECT_OFFSET(object_set_attribute_value),
4997*12304SValerie.Fenwick@Oracle.COM sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
49986424Skrishna goto release_minor;
49990Sstevel@tonic-gate }
50000Sstevel@tonic-gate
50010Sstevel@tonic-gate object_handle = STRUCT_FGET(set_attribute_value, sa_handle);
50020Sstevel@tonic-gate KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, KCF_OP_OBJECT_SET_ATTRIBUTE_VALUE,
50030Sstevel@tonic-gate sp->sd_provider_session->ps_session, object_handle, k_attrs, count,
50040Sstevel@tonic-gate NULL, 0, NULL, NULL, 0, NULL);
50050Sstevel@tonic-gate
50060Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
5007904Smcpowers KCF_PROV_REFRELE(real_provider);
50080Sstevel@tonic-gate
50096424Skrishna release_minor:
50106424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk);
50110Sstevel@tonic-gate CRYPTO_SESSION_RELE(sp);
50120Sstevel@tonic-gate crypto_release_minor(cm);
50130Sstevel@tonic-gate
50140Sstevel@tonic-gate if (k_attrs != NULL)
50150Sstevel@tonic-gate kmem_free(k_attrs, k_attrs_size);
50160Sstevel@tonic-gate
50170Sstevel@tonic-gate if (error != 0)
50180Sstevel@tonic-gate return (error);
50190Sstevel@tonic-gate
50200Sstevel@tonic-gate STRUCT_FSET(set_attribute_value, sa_return_value, rv);
50210Sstevel@tonic-gate if (copyout(STRUCT_BUF(set_attribute_value), arg,
50220Sstevel@tonic-gate STRUCT_SIZE(set_attribute_value)) != 0) {
50230Sstevel@tonic-gate return (EFAULT);
50240Sstevel@tonic-gate }
50250Sstevel@tonic-gate return (0);
50260Sstevel@tonic-gate }
50270Sstevel@tonic-gate
50280Sstevel@tonic-gate /* ARGSUSED */
50290Sstevel@tonic-gate static int
object_find_init(dev_t dev,caddr_t arg,int mode,int * rval)50300Sstevel@tonic-gate object_find_init(dev_t dev, caddr_t arg, int mode, int *rval)
50310Sstevel@tonic-gate {
50320Sstevel@tonic-gate STRUCT_DECL(crypto_object_find_init, find_init);
5033904Smcpowers kcf_provider_desc_t *real_provider = NULL;
50340Sstevel@tonic-gate kcf_req_params_t params;
50350Sstevel@tonic-gate crypto_object_attribute_t *k_attrs = NULL;
50360Sstevel@tonic-gate crypto_session_id_t session_id;
50370Sstevel@tonic-gate crypto_minor_t *cm;
50386424Skrishna crypto_session_data_t *sp = NULL;
50390Sstevel@tonic-gate caddr_t attributes;
50400Sstevel@tonic-gate size_t k_attrs_size;
50410Sstevel@tonic-gate size_t rctl_bytes = 0;
50426424Skrishna boolean_t rctl_chk = B_FALSE;
50430Sstevel@tonic-gate int error = 0;
50440Sstevel@tonic-gate int rv;
50450Sstevel@tonic-gate uint_t count;
50460Sstevel@tonic-gate void *cookie;
50470Sstevel@tonic-gate
50480Sstevel@tonic-gate STRUCT_INIT(find_init, mode);
50490Sstevel@tonic-gate
50500Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
50510Sstevel@tonic-gate cmn_err(CE_WARN, "object_find_init: failed holding minor");
50520Sstevel@tonic-gate return (ENXIO);
50530Sstevel@tonic-gate }
50540Sstevel@tonic-gate
50550Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(find_init), STRUCT_SIZE(find_init)) != 0) {
50560Sstevel@tonic-gate crypto_release_minor(cm);
50570Sstevel@tonic-gate return (EFAULT);
50580Sstevel@tonic-gate }
50590Sstevel@tonic-gate
50600Sstevel@tonic-gate count = STRUCT_FGET(find_init, fi_count);
50610Sstevel@tonic-gate attributes = STRUCT_FGETP(find_init, fi_attributes);
50620Sstevel@tonic-gate
50630Sstevel@tonic-gate session_id = STRUCT_FGET(find_init, fi_session);
50640Sstevel@tonic-gate
50650Sstevel@tonic-gate if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
50660Sstevel@tonic-gate goto release_minor;
50670Sstevel@tonic-gate }
50686424Skrishna if (!copyin_attributes(mode, sp, count, attributes, &k_attrs,
50696424Skrishna &k_attrs_size, NULL, &rv, &error, &rctl_bytes,
50706424Skrishna &rctl_chk, B_TRUE)) {
50716424Skrishna goto release_minor;
50726424Skrishna }
50730Sstevel@tonic-gate
50740Sstevel@tonic-gate if ((rv = kcf_get_hardware_provider_nomech(
50750Sstevel@tonic-gate CRYPTO_OPS_OFFSET(object_ops),
5076*12304SValerie.Fenwick@Oracle.COM CRYPTO_OBJECT_OFFSET(object_find_init),
50770Sstevel@tonic-gate sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
50786424Skrishna goto release_minor;
50790Sstevel@tonic-gate }
50800Sstevel@tonic-gate
50810Sstevel@tonic-gate /* check for an active find */
50820Sstevel@tonic-gate if (sp->sd_find_init_cookie != NULL) {
50830Sstevel@tonic-gate rv = CRYPTO_OPERATION_IS_ACTIVE;
50840Sstevel@tonic-gate goto release_minor;
50850Sstevel@tonic-gate }
50860Sstevel@tonic-gate
50870Sstevel@tonic-gate KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, KCF_OP_OBJECT_FIND_INIT,
50880Sstevel@tonic-gate sp->sd_provider_session->ps_session, 0, k_attrs, count, NULL, 0,
50890Sstevel@tonic-gate &cookie, NULL, 0, NULL);
50900Sstevel@tonic-gate
50910Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
50920Sstevel@tonic-gate
50930Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) {
50940Sstevel@tonic-gate /*
50950Sstevel@tonic-gate * The cookie is allocated by a provider at the start of an
50960Sstevel@tonic-gate * object search. It is freed when the search is terminated
50970Sstevel@tonic-gate * by a final operation, or when the session is closed.
50980Sstevel@tonic-gate * It contains state information about which object handles
50990Sstevel@tonic-gate * have been returned to the caller.
51000Sstevel@tonic-gate */
51010Sstevel@tonic-gate sp->sd_find_init_cookie = cookie;
51020Sstevel@tonic-gate }
51030Sstevel@tonic-gate
51046424Skrishna release_minor:
51056424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk);
51060Sstevel@tonic-gate CRYPTO_SESSION_RELE(sp);
51070Sstevel@tonic-gate crypto_release_minor(cm);
51080Sstevel@tonic-gate
5109904Smcpowers if (real_provider != NULL)
5110904Smcpowers KCF_PROV_REFRELE(real_provider);
5111904Smcpowers
51120Sstevel@tonic-gate if (k_attrs != NULL)
51130Sstevel@tonic-gate kmem_free(k_attrs, k_attrs_size);
51140Sstevel@tonic-gate
51150Sstevel@tonic-gate if (error != 0)
51160Sstevel@tonic-gate return (error);
51170Sstevel@tonic-gate
51180Sstevel@tonic-gate STRUCT_FSET(find_init, fi_return_value, rv);
51190Sstevel@tonic-gate if (copyout(STRUCT_BUF(find_init), arg, STRUCT_SIZE(find_init)) != 0) {
51200Sstevel@tonic-gate return (EFAULT);
51210Sstevel@tonic-gate }
51220Sstevel@tonic-gate return (0);
51230Sstevel@tonic-gate }
51240Sstevel@tonic-gate
51250Sstevel@tonic-gate /* ARGSUSED */
51260Sstevel@tonic-gate static int
object_find_update(dev_t dev,caddr_t arg,int mode,int * rval)51270Sstevel@tonic-gate object_find_update(dev_t dev, caddr_t arg, int mode, int *rval)
51280Sstevel@tonic-gate {
51290Sstevel@tonic-gate STRUCT_DECL(crypto_object_find_update, find_update);
51300Sstevel@tonic-gate kcf_provider_desc_t *real_provider;
51310Sstevel@tonic-gate kcf_req_params_t params;
51320Sstevel@tonic-gate crypto_minor_t *cm;
51336424Skrishna crypto_session_data_t *sp = NULL;
51340Sstevel@tonic-gate crypto_object_id_t *buffer = NULL;
51350Sstevel@tonic-gate crypto_session_id_t session_id;
51360Sstevel@tonic-gate size_t len, rctl_bytes = 0;
51370Sstevel@tonic-gate uint_t count, max_count;
51380Sstevel@tonic-gate int rv, error = 0;
51396424Skrishna boolean_t rctl_chk = B_FALSE;
51400Sstevel@tonic-gate
51410Sstevel@tonic-gate STRUCT_INIT(find_update, mode);
51420Sstevel@tonic-gate
51430Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
51440Sstevel@tonic-gate cmn_err(CE_WARN, "object_find_update: failed holding minor");
51450Sstevel@tonic-gate return (ENXIO);
51460Sstevel@tonic-gate }
51470Sstevel@tonic-gate
51480Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(find_update),
51490Sstevel@tonic-gate STRUCT_SIZE(find_update)) != 0) {
51500Sstevel@tonic-gate crypto_release_minor(cm);
51510Sstevel@tonic-gate return (EFAULT);
51520Sstevel@tonic-gate }
51530Sstevel@tonic-gate
51540Sstevel@tonic-gate max_count = STRUCT_FGET(find_update, fu_max_count);
51550Sstevel@tonic-gate if (max_count > CRYPTO_MAX_FIND_COUNT) {
51560Sstevel@tonic-gate cmn_err(CE_NOTE, "object_find_update: count greater than %d, "
51570Sstevel@tonic-gate "pid = %d", CRYPTO_MAX_FIND_COUNT, curproc->p_pid);
51580Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD;
51590Sstevel@tonic-gate goto release_minor;
51600Sstevel@tonic-gate }
51610Sstevel@tonic-gate len = max_count * sizeof (crypto_object_id_t);
51626424Skrishna session_id = STRUCT_FGET(find_update, fu_session);
51636424Skrishna
51646424Skrishna if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
51656424Skrishna goto release_minor;
51666424Skrishna }
51676424Skrishna if ((rv = CRYPTO_BUFFER_CHECK(sp, len, rctl_chk)) !=
51686424Skrishna CRYPTO_SUCCESS) {
51690Sstevel@tonic-gate goto release_minor;
51700Sstevel@tonic-gate }
51710Sstevel@tonic-gate rctl_bytes = len;
51720Sstevel@tonic-gate buffer = kmem_alloc(len, KM_SLEEP);
51730Sstevel@tonic-gate
51740Sstevel@tonic-gate if ((rv = kcf_get_hardware_provider_nomech(
51750Sstevel@tonic-gate CRYPTO_OPS_OFFSET(object_ops),
5176*12304SValerie.Fenwick@Oracle.COM CRYPTO_OBJECT_OFFSET(object_find), sp->sd_provider,
5177*12304SValerie.Fenwick@Oracle.COM &real_provider)) != CRYPTO_SUCCESS) {
51786424Skrishna goto release_minor;
51790Sstevel@tonic-gate }
51800Sstevel@tonic-gate
51810Sstevel@tonic-gate KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, KCF_OP_OBJECT_FIND,
51820Sstevel@tonic-gate sp->sd_provider_session->ps_session, 0, NULL, 0, buffer, 0,
51830Sstevel@tonic-gate NULL, sp->sd_find_init_cookie, max_count, &count);
51840Sstevel@tonic-gate
51850Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
5186904Smcpowers KCF_PROV_REFRELE(real_provider);
51870Sstevel@tonic-gate
51880Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) {
51890Sstevel@tonic-gate if (count > max_count) {
51900Sstevel@tonic-gate /* bad bad provider */
51910Sstevel@tonic-gate rv = CRYPTO_FAILED;
51920Sstevel@tonic-gate goto release_minor;
51930Sstevel@tonic-gate }
51940Sstevel@tonic-gate if (count != 0) {
51950Sstevel@tonic-gate /* copyout handles */
51960Sstevel@tonic-gate if (copyout(buffer,
51970Sstevel@tonic-gate STRUCT_FGETP(find_update, fu_handles),
51980Sstevel@tonic-gate count * sizeof (crypto_object_id_t)) != 0) {
51990Sstevel@tonic-gate error = EFAULT;
52000Sstevel@tonic-gate }
52010Sstevel@tonic-gate }
52020Sstevel@tonic-gate STRUCT_FSET(find_update, fu_count, count);
52030Sstevel@tonic-gate }
52040Sstevel@tonic-gate
52050Sstevel@tonic-gate release_minor:
52066424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, rctl_bytes, rctl_chk);
52076424Skrishna CRYPTO_SESSION_RELE(sp);
52080Sstevel@tonic-gate crypto_release_minor(cm);
52090Sstevel@tonic-gate
52100Sstevel@tonic-gate if (buffer != NULL)
52110Sstevel@tonic-gate kmem_free(buffer, len);
52120Sstevel@tonic-gate
52130Sstevel@tonic-gate if (error != 0)
52140Sstevel@tonic-gate return (error);
52150Sstevel@tonic-gate
52160Sstevel@tonic-gate STRUCT_FSET(find_update, fu_return_value, rv);
52170Sstevel@tonic-gate if (copyout(STRUCT_BUF(find_update), arg,
52180Sstevel@tonic-gate STRUCT_SIZE(find_update)) != 0) {
52190Sstevel@tonic-gate return (EFAULT);
52200Sstevel@tonic-gate }
52210Sstevel@tonic-gate
52220Sstevel@tonic-gate return (0);
52230Sstevel@tonic-gate }
52240Sstevel@tonic-gate
52250Sstevel@tonic-gate /*
52260Sstevel@tonic-gate * Free provider-allocated storage used for find object searches.
52270Sstevel@tonic-gate */
52280Sstevel@tonic-gate static int
crypto_free_find_ctx(crypto_session_data_t * sp)52290Sstevel@tonic-gate crypto_free_find_ctx(crypto_session_data_t *sp)
52300Sstevel@tonic-gate {
52310Sstevel@tonic-gate kcf_provider_desc_t *real_provider;
52320Sstevel@tonic-gate kcf_req_params_t params;
52330Sstevel@tonic-gate int rv;
52340Sstevel@tonic-gate
52350Sstevel@tonic-gate if ((rv = kcf_get_hardware_provider_nomech(
52360Sstevel@tonic-gate CRYPTO_OPS_OFFSET(object_ops),
5237*12304SValerie.Fenwick@Oracle.COM CRYPTO_OBJECT_OFFSET(object_find_final),
52380Sstevel@tonic-gate sp->sd_provider, &real_provider)) != CRYPTO_SUCCESS) {
52390Sstevel@tonic-gate return (rv);
52400Sstevel@tonic-gate }
52410Sstevel@tonic-gate
52420Sstevel@tonic-gate KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, KCF_OP_OBJECT_FIND_FINAL,
52430Sstevel@tonic-gate sp->sd_provider_session->ps_session, 0, NULL, 0, NULL, 0,
52440Sstevel@tonic-gate NULL, sp->sd_find_init_cookie, 0, NULL);
52450Sstevel@tonic-gate
5246904Smcpowers rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
5247904Smcpowers KCF_PROV_REFRELE(real_provider);
5248904Smcpowers return (rv);
52490Sstevel@tonic-gate }
52500Sstevel@tonic-gate
52510Sstevel@tonic-gate /* ARGSUSED */
52520Sstevel@tonic-gate static int
object_find_final(dev_t dev,caddr_t arg,int mode,int * rval)52530Sstevel@tonic-gate object_find_final(dev_t dev, caddr_t arg, int mode, int *rval)
52540Sstevel@tonic-gate {
52550Sstevel@tonic-gate STRUCT_DECL(crypto_object_find_final, object_find_final);
52560Sstevel@tonic-gate crypto_session_id_t session_id;
52570Sstevel@tonic-gate crypto_minor_t *cm;
52580Sstevel@tonic-gate crypto_session_data_t *sp;
52590Sstevel@tonic-gate int error = 0;
52600Sstevel@tonic-gate int rv;
52610Sstevel@tonic-gate
52620Sstevel@tonic-gate STRUCT_INIT(object_find_final, mode);
52630Sstevel@tonic-gate
52640Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
52650Sstevel@tonic-gate cmn_err(CE_WARN, "object_find_final: failed holding minor");
52660Sstevel@tonic-gate return (ENXIO);
52670Sstevel@tonic-gate }
52680Sstevel@tonic-gate
52690Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(object_find_final),
52700Sstevel@tonic-gate STRUCT_SIZE(object_find_final)) != 0) {
52710Sstevel@tonic-gate crypto_release_minor(cm);
52720Sstevel@tonic-gate return (EFAULT);
52730Sstevel@tonic-gate }
52740Sstevel@tonic-gate
52750Sstevel@tonic-gate session_id = STRUCT_FGET(object_find_final, ff_session);
52760Sstevel@tonic-gate
52770Sstevel@tonic-gate if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
52780Sstevel@tonic-gate goto release_minor;
52790Sstevel@tonic-gate }
52800Sstevel@tonic-gate
52810Sstevel@tonic-gate if ((rv = crypto_free_find_ctx(sp)) == CRYPTO_SUCCESS) {
52820Sstevel@tonic-gate sp->sd_find_init_cookie = NULL;
52830Sstevel@tonic-gate }
52840Sstevel@tonic-gate
52850Sstevel@tonic-gate CRYPTO_SESSION_RELE(sp);
52860Sstevel@tonic-gate
52870Sstevel@tonic-gate release_minor:
52880Sstevel@tonic-gate crypto_release_minor(cm);
52890Sstevel@tonic-gate
52900Sstevel@tonic-gate if (error != 0)
52910Sstevel@tonic-gate return (error);
52920Sstevel@tonic-gate
52930Sstevel@tonic-gate STRUCT_FSET(object_find_final, ff_return_value, rv);
52940Sstevel@tonic-gate
52950Sstevel@tonic-gate if (copyout(STRUCT_BUF(object_find_final), arg,
52960Sstevel@tonic-gate STRUCT_SIZE(object_find_final)) != 0) {
52970Sstevel@tonic-gate return (EFAULT);
52980Sstevel@tonic-gate }
52990Sstevel@tonic-gate return (0);
53000Sstevel@tonic-gate }
53010Sstevel@tonic-gate
53020Sstevel@tonic-gate /* ARGSUSED */
53030Sstevel@tonic-gate static int
object_generate_key(dev_t dev,caddr_t arg,int mode,int * rval)53040Sstevel@tonic-gate object_generate_key(dev_t dev, caddr_t arg, int mode, int *rval)
53050Sstevel@tonic-gate {
53060Sstevel@tonic-gate STRUCT_DECL(crypto_object_generate_key, generate_key);
5307904Smcpowers kcf_provider_desc_t *real_provider = NULL;
53080Sstevel@tonic-gate kcf_req_params_t params;
53090Sstevel@tonic-gate crypto_mechanism_t mech;
53100Sstevel@tonic-gate crypto_object_attribute_t *k_attrs = NULL;
53110Sstevel@tonic-gate crypto_session_id_t session_id;
53120Sstevel@tonic-gate crypto_minor_t *cm;
53130Sstevel@tonic-gate crypto_session_data_t *sp = NULL;
53140Sstevel@tonic-gate crypto_object_id_t key_handle;
53150Sstevel@tonic-gate caddr_t attributes;
53160Sstevel@tonic-gate size_t k_attrs_size;
53170Sstevel@tonic-gate size_t mech_rctl_bytes = 0, key_rctl_bytes = 0;
53186424Skrishna boolean_t mech_rctl_chk = B_FALSE;
53196424Skrishna boolean_t key_rctl_chk = B_FALSE;
53200Sstevel@tonic-gate uint_t count;
53210Sstevel@tonic-gate int error = 0;
53220Sstevel@tonic-gate int rv;
5323904Smcpowers boolean_t allocated_by_crypto_module = B_FALSE;
53240Sstevel@tonic-gate
53250Sstevel@tonic-gate STRUCT_INIT(generate_key, mode);
53260Sstevel@tonic-gate
53270Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
53280Sstevel@tonic-gate cmn_err(CE_WARN, "object_generate_key: failed holding minor");
53290Sstevel@tonic-gate return (ENXIO);
53300Sstevel@tonic-gate }
53310Sstevel@tonic-gate
53320Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(generate_key),
53330Sstevel@tonic-gate STRUCT_SIZE(generate_key)) != 0) {
53340Sstevel@tonic-gate crypto_release_minor(cm);
53350Sstevel@tonic-gate return (EFAULT);
53360Sstevel@tonic-gate }
53370Sstevel@tonic-gate
53380Sstevel@tonic-gate session_id = STRUCT_FGET(generate_key, gk_session);
53390Sstevel@tonic-gate
53400Sstevel@tonic-gate if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
53410Sstevel@tonic-gate goto release_minor;
53420Sstevel@tonic-gate }
53430Sstevel@tonic-gate
5344904Smcpowers bcopy(STRUCT_FADDR(generate_key, gk_mechanism), &mech.cm_type,
5345904Smcpowers sizeof (crypto_mech_type_t));
5346904Smcpowers
534710444SVladimir.Kotal@Sun.COM if ((rv = kcf_get_hardware_provider(mech.cm_type, NULL,
5348*12304SValerie.Fenwick@Oracle.COM CRYPTO_MECH_INVALID, NULL, sp->sd_provider,
534910444SVladimir.Kotal@Sun.COM &real_provider, CRYPTO_FG_GENERATE)) != CRYPTO_SUCCESS) {
53500Sstevel@tonic-gate goto release_minor;
53510Sstevel@tonic-gate }
53520Sstevel@tonic-gate
5353904Smcpowers rv = crypto_provider_copyin_mech_param(real_provider,
5354904Smcpowers STRUCT_FADDR(generate_key, gk_mechanism), &mech, mode, &error);
5355904Smcpowers
5356904Smcpowers if (rv == CRYPTO_NOT_SUPPORTED) {
5357904Smcpowers allocated_by_crypto_module = B_TRUE;
53586424Skrishna if (!copyin_mech(mode, sp,
53596424Skrishna STRUCT_FADDR(generate_key, gk_mechanism),
53606424Skrishna &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) {
5361904Smcpowers goto release_minor;
5362904Smcpowers }
5363904Smcpowers } else {
5364904Smcpowers if (rv != CRYPTO_SUCCESS)
5365904Smcpowers goto release_minor;
53660Sstevel@tonic-gate }
53670Sstevel@tonic-gate
53680Sstevel@tonic-gate count = STRUCT_FGET(generate_key, gk_count);
53690Sstevel@tonic-gate attributes = STRUCT_FGETP(generate_key, gk_attributes);
53706424Skrishna if (!copyin_attributes(mode, sp, count, attributes, &k_attrs,
53716424Skrishna &k_attrs_size, NULL, &rv, &error, &key_rctl_bytes,
53726424Skrishna &key_rctl_chk, B_TRUE)) {
53730Sstevel@tonic-gate goto release_minor;
53740Sstevel@tonic-gate }
53750Sstevel@tonic-gate
53760Sstevel@tonic-gate KCF_WRAP_KEY_OPS_PARAMS(¶ms, KCF_OP_KEY_GENERATE,
53770Sstevel@tonic-gate sp->sd_provider_session->ps_session, &mech, k_attrs, count,
53780Sstevel@tonic-gate &key_handle, NULL, 0, NULL, NULL, NULL, 0);
53790Sstevel@tonic-gate
53800Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
53810Sstevel@tonic-gate
53820Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS)
53830Sstevel@tonic-gate STRUCT_FSET(generate_key, gk_handle, key_handle);
53840Sstevel@tonic-gate
53850Sstevel@tonic-gate release_minor:
53866424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
53876424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk);
53880Sstevel@tonic-gate
53890Sstevel@tonic-gate if (k_attrs != NULL)
53900Sstevel@tonic-gate kmem_free(k_attrs, k_attrs_size);
53910Sstevel@tonic-gate
53920Sstevel@tonic-gate if (error != 0)
53930Sstevel@tonic-gate goto out;
53940Sstevel@tonic-gate
53950Sstevel@tonic-gate STRUCT_FSET(generate_key, gk_return_value, rv);
53960Sstevel@tonic-gate if (copyout(STRUCT_BUF(generate_key), arg,
53970Sstevel@tonic-gate STRUCT_SIZE(generate_key)) != 0) {
53980Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) {
53990Sstevel@tonic-gate KCF_WRAP_OBJECT_OPS_PARAMS(¶ms,
54000Sstevel@tonic-gate KCF_OP_OBJECT_DESTROY,
54010Sstevel@tonic-gate sp->sd_provider_session->ps_session, key_handle,
54020Sstevel@tonic-gate NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
54030Sstevel@tonic-gate
54040Sstevel@tonic-gate (void) kcf_submit_request(real_provider, NULL,
54050Sstevel@tonic-gate NULL, ¶ms, B_FALSE);
54060Sstevel@tonic-gate
54070Sstevel@tonic-gate error = EFAULT;
54080Sstevel@tonic-gate }
54090Sstevel@tonic-gate }
54100Sstevel@tonic-gate out:
54116424Skrishna CRYPTO_SESSION_RELE(sp);
54120Sstevel@tonic-gate crypto_release_minor(cm);
5413904Smcpowers
5414904Smcpowers if (real_provider != NULL) {
5415904Smcpowers crypto_free_mech(real_provider,
5416904Smcpowers allocated_by_crypto_module, &mech);
5417904Smcpowers KCF_PROV_REFRELE(real_provider);
5418904Smcpowers }
54190Sstevel@tonic-gate return (error);
54200Sstevel@tonic-gate }
54210Sstevel@tonic-gate
54220Sstevel@tonic-gate /* ARGSUSED */
54230Sstevel@tonic-gate static int
nostore_generate_key(dev_t dev,caddr_t arg,int mode,int * rval)54244219Smcpowers nostore_generate_key(dev_t dev, caddr_t arg, int mode, int *rval)
54254219Smcpowers {
54264219Smcpowers STRUCT_DECL(crypto_nostore_generate_key, generate_key);
542711413Sopensolaris@drydog.com #ifdef _LP64
542811413Sopensolaris@drydog.com STRUCT_DECL(crypto_object_attribute, oa);
542911413Sopensolaris@drydog.com #else
54304219Smcpowers /* LINTED E_FUNC_SET_NOT_USED */
54314219Smcpowers STRUCT_DECL(crypto_object_attribute, oa);
543211413Sopensolaris@drydog.com #endif
54334219Smcpowers kcf_provider_desc_t *real_provider = NULL;
54344219Smcpowers kcf_req_params_t params;
54354219Smcpowers crypto_mechanism_t mech;
54364219Smcpowers crypto_object_attribute_t *k_in_attrs = NULL;
54374219Smcpowers crypto_object_attribute_t *k_out_attrs = NULL;
54384219Smcpowers crypto_session_id_t session_id;
54394219Smcpowers crypto_minor_t *cm;
54404219Smcpowers crypto_session_data_t *sp = NULL;
54414219Smcpowers caddr_t in_attributes;
54424219Smcpowers caddr_t out_attributes;
54434219Smcpowers size_t k_in_attrs_size;
54444219Smcpowers size_t k_out_attrs_size;
54454219Smcpowers size_t mech_rctl_bytes = 0;
54466424Skrishna boolean_t mech_rctl_chk = B_FALSE;
54474219Smcpowers size_t in_key_rctl_bytes = 0, out_key_rctl_bytes = 0;
54486424Skrishna boolean_t in_key_rctl_chk = B_FALSE;
54496424Skrishna boolean_t out_key_rctl_chk = B_FALSE;
54504219Smcpowers uint_t in_count;
54514219Smcpowers uint_t out_count;
54524219Smcpowers int error = 0;
54534219Smcpowers int rv;
54544219Smcpowers boolean_t allocated_by_crypto_module = B_FALSE;
54554219Smcpowers caddr_t u_attrs = NULL;
54564219Smcpowers
54574219Smcpowers STRUCT_INIT(generate_key, mode);
54584219Smcpowers STRUCT_INIT(oa, mode);
54594219Smcpowers
54604219Smcpowers if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
54614219Smcpowers cmn_err(CE_WARN, "nostore_generate_key: failed holding minor");
54624219Smcpowers return (ENXIO);
54634219Smcpowers }
54644219Smcpowers
54654219Smcpowers if (copyin(arg, STRUCT_BUF(generate_key),
54664219Smcpowers STRUCT_SIZE(generate_key)) != 0) {
54674219Smcpowers crypto_release_minor(cm);
54684219Smcpowers return (EFAULT);
54694219Smcpowers }
54704219Smcpowers
54714219Smcpowers session_id = STRUCT_FGET(generate_key, ngk_session);
54724219Smcpowers
54734219Smcpowers if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
54744219Smcpowers goto release_minor;
54754219Smcpowers }
54764219Smcpowers
54774219Smcpowers bcopy(STRUCT_FADDR(generate_key, ngk_mechanism), &mech.cm_type,
54784219Smcpowers sizeof (crypto_mech_type_t));
54794219Smcpowers
548010444SVladimir.Kotal@Sun.COM if ((rv = kcf_get_hardware_provider(mech.cm_type, NULL,
5481*12304SValerie.Fenwick@Oracle.COM CRYPTO_MECH_INVALID, NULL, sp->sd_provider,
548210444SVladimir.Kotal@Sun.COM &real_provider, CRYPTO_FG_GENERATE)) != CRYPTO_SUCCESS) {
54834219Smcpowers goto release_minor;
54844219Smcpowers }
54854219Smcpowers
54864219Smcpowers rv = crypto_provider_copyin_mech_param(real_provider,
54874219Smcpowers STRUCT_FADDR(generate_key, ngk_mechanism), &mech, mode, &error);
54884219Smcpowers
54894219Smcpowers if (rv == CRYPTO_NOT_SUPPORTED) {
54904219Smcpowers allocated_by_crypto_module = B_TRUE;
54916424Skrishna if (!copyin_mech(mode, sp, STRUCT_FADDR(generate_key,
54926424Skrishna ngk_mechanism), &mech, &mech_rctl_bytes,
54936424Skrishna &mech_rctl_chk, &rv, &error)) {
54944219Smcpowers goto release_minor;
54954219Smcpowers }
54964219Smcpowers } else {
54974219Smcpowers if (rv != CRYPTO_SUCCESS)
54984219Smcpowers goto release_minor;
54994219Smcpowers }
55004219Smcpowers
55014219Smcpowers in_count = STRUCT_FGET(generate_key, ngk_in_count);
55024219Smcpowers in_attributes = STRUCT_FGETP(generate_key, ngk_in_attributes);
55036424Skrishna if (!copyin_attributes(mode, sp, in_count, in_attributes, &k_in_attrs,
55044219Smcpowers &k_in_attrs_size, NULL, &rv, &error, &in_key_rctl_bytes,
55056424Skrishna &in_key_rctl_chk, B_TRUE)) {
55064219Smcpowers goto release_minor;
55074219Smcpowers }
55084219Smcpowers
55094219Smcpowers out_count = STRUCT_FGET(generate_key, ngk_out_count);
55104219Smcpowers out_attributes = STRUCT_FGETP(generate_key, ngk_out_attributes);
55116424Skrishna if (!copyin_attributes(mode, sp, out_count, out_attributes,
55126424Skrishna &k_out_attrs,
55134219Smcpowers &k_out_attrs_size, &u_attrs, &rv, &error, &out_key_rctl_bytes,
55146424Skrishna &out_key_rctl_chk, B_FALSE)) {
55154219Smcpowers goto release_minor;
55164219Smcpowers }
55174219Smcpowers
55184219Smcpowers KCF_WRAP_NOSTORE_KEY_OPS_PARAMS(¶ms, KCF_OP_KEY_GENERATE,
55194219Smcpowers sp->sd_provider_session->ps_session, &mech, k_in_attrs, in_count,
55204219Smcpowers NULL, 0, NULL, k_out_attrs, out_count, NULL, 0);
55214219Smcpowers
55224219Smcpowers rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
55234219Smcpowers
55244219Smcpowers if (rv == CRYPTO_SUCCESS) {
55254219Smcpowers error = copyout_attributes(mode, out_attributes,
55264219Smcpowers out_count, k_out_attrs, u_attrs);
55274219Smcpowers }
55284219Smcpowers release_minor:
55296424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
55306424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, in_key_rctl_bytes, in_key_rctl_chk);
55316424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, out_key_rctl_bytes,
55326424Skrishna out_key_rctl_chk);
55334219Smcpowers
55344219Smcpowers if (k_in_attrs != NULL)
55354219Smcpowers kmem_free(k_in_attrs, k_in_attrs_size);
55364219Smcpowers if (k_out_attrs != NULL) {
55374219Smcpowers bzero(k_out_attrs, k_out_attrs_size);
55384219Smcpowers kmem_free(k_out_attrs, k_out_attrs_size);
55394219Smcpowers }
55404219Smcpowers
55414219Smcpowers if (u_attrs != NULL)
55424219Smcpowers kmem_free(u_attrs, out_count * STRUCT_SIZE(oa));
55434219Smcpowers
55444219Smcpowers if (error != 0)
55454219Smcpowers goto out;
55464219Smcpowers
55474219Smcpowers STRUCT_FSET(generate_key, ngk_return_value, rv);
55484219Smcpowers if (copyout(STRUCT_BUF(generate_key), arg,
55494219Smcpowers STRUCT_SIZE(generate_key)) != 0) {
55504219Smcpowers error = EFAULT;
55514219Smcpowers }
55524219Smcpowers out:
55536424Skrishna CRYPTO_SESSION_RELE(sp);
55544219Smcpowers crypto_release_minor(cm);
55554219Smcpowers
55564219Smcpowers if (real_provider != NULL) {
55574219Smcpowers crypto_free_mech(real_provider,
55584219Smcpowers allocated_by_crypto_module, &mech);
55594219Smcpowers KCF_PROV_REFRELE(real_provider);
55604219Smcpowers }
55614219Smcpowers return (error);
55624219Smcpowers }
55634219Smcpowers
55644219Smcpowers /* ARGSUSED */
55654219Smcpowers static int
object_generate_key_pair(dev_t dev,caddr_t arg,int mode,int * rval)55660Sstevel@tonic-gate object_generate_key_pair(dev_t dev, caddr_t arg, int mode, int *rval)
55670Sstevel@tonic-gate {
55680Sstevel@tonic-gate STRUCT_DECL(crypto_object_generate_key_pair, generate_key_pair);
5569904Smcpowers kcf_provider_desc_t *real_provider = NULL;
55700Sstevel@tonic-gate kcf_req_params_t params;
55710Sstevel@tonic-gate crypto_mechanism_t mech;
55720Sstevel@tonic-gate crypto_object_attribute_t *k_pub_attrs = NULL;
55730Sstevel@tonic-gate crypto_object_attribute_t *k_pri_attrs = NULL;
55740Sstevel@tonic-gate crypto_session_id_t session_id;
55750Sstevel@tonic-gate crypto_minor_t *cm;
55760Sstevel@tonic-gate crypto_session_data_t *sp = NULL;
55770Sstevel@tonic-gate crypto_object_id_t pub_handle;
55780Sstevel@tonic-gate crypto_object_id_t pri_handle;
55790Sstevel@tonic-gate caddr_t pri_attributes;
55800Sstevel@tonic-gate caddr_t pub_attributes;
55810Sstevel@tonic-gate size_t k_pub_attrs_size, k_pri_attrs_size;
55820Sstevel@tonic-gate size_t mech_rctl_bytes = 0;
55836424Skrishna boolean_t mech_rctl_chk = B_FALSE;
55840Sstevel@tonic-gate size_t pub_rctl_bytes = 0;
55856424Skrishna boolean_t pub_rctl_chk = B_FALSE;
55860Sstevel@tonic-gate size_t pri_rctl_bytes = 0;
55876424Skrishna boolean_t pri_rctl_chk = B_FALSE;
55880Sstevel@tonic-gate uint_t pub_count;
55890Sstevel@tonic-gate uint_t pri_count;
55900Sstevel@tonic-gate int error = 0;
55910Sstevel@tonic-gate int rv;
5592904Smcpowers boolean_t allocated_by_crypto_module = B_FALSE;
55930Sstevel@tonic-gate
55940Sstevel@tonic-gate STRUCT_INIT(generate_key_pair, mode);
55950Sstevel@tonic-gate
55960Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
55970Sstevel@tonic-gate cmn_err(CE_WARN,
55980Sstevel@tonic-gate "object_generate_key_pair: failed holding minor");
55990Sstevel@tonic-gate return (ENXIO);
56000Sstevel@tonic-gate }
56010Sstevel@tonic-gate
56020Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(generate_key_pair),
56030Sstevel@tonic-gate STRUCT_SIZE(generate_key_pair)) != 0) {
56040Sstevel@tonic-gate crypto_release_minor(cm);
56050Sstevel@tonic-gate return (EFAULT);
56060Sstevel@tonic-gate }
56070Sstevel@tonic-gate
56080Sstevel@tonic-gate session_id = STRUCT_FGET(generate_key_pair, kp_session);
56090Sstevel@tonic-gate
56100Sstevel@tonic-gate if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
56110Sstevel@tonic-gate goto release_minor;
56120Sstevel@tonic-gate }
56130Sstevel@tonic-gate
5614904Smcpowers bcopy(STRUCT_FADDR(generate_key_pair, kp_mechanism), &mech.cm_type,
5615904Smcpowers sizeof (crypto_mech_type_t));
5616904Smcpowers
561710444SVladimir.Kotal@Sun.COM if ((rv = kcf_get_hardware_provider(mech.cm_type, NULL,
5618*12304SValerie.Fenwick@Oracle.COM CRYPTO_MECH_INVALID, NULL, sp->sd_provider,
561910444SVladimir.Kotal@Sun.COM &real_provider, CRYPTO_FG_GENERATE_KEY_PAIR)) != CRYPTO_SUCCESS) {
56200Sstevel@tonic-gate goto release_minor;
56210Sstevel@tonic-gate }
56220Sstevel@tonic-gate
5623904Smcpowers rv = crypto_provider_copyin_mech_param(real_provider,
5624904Smcpowers STRUCT_FADDR(generate_key_pair, kp_mechanism), &mech, mode, &error);
5625904Smcpowers
5626904Smcpowers if (rv == CRYPTO_NOT_SUPPORTED) {
5627904Smcpowers allocated_by_crypto_module = B_TRUE;
56286424Skrishna if (!copyin_mech(mode, sp, STRUCT_FADDR(generate_key_pair,
56296424Skrishna kp_mechanism), &mech, &mech_rctl_bytes,
56306424Skrishna &mech_rctl_chk, &rv, &error)) {
5631904Smcpowers goto release_minor;
5632904Smcpowers }
5633904Smcpowers } else {
5634904Smcpowers if (rv != CRYPTO_SUCCESS)
5635904Smcpowers goto release_minor;
56360Sstevel@tonic-gate }
56370Sstevel@tonic-gate
56380Sstevel@tonic-gate pub_count = STRUCT_FGET(generate_key_pair, kp_public_count);
56390Sstevel@tonic-gate pri_count = STRUCT_FGET(generate_key_pair, kp_private_count);
56400Sstevel@tonic-gate
56410Sstevel@tonic-gate pub_attributes = STRUCT_FGETP(generate_key_pair, kp_public_attributes);
56426424Skrishna if (!copyin_attributes(mode, sp, pub_count, pub_attributes,
56436424Skrishna &k_pub_attrs, &k_pub_attrs_size, NULL, &rv, &error, &pub_rctl_bytes,
56446424Skrishna &pub_rctl_chk, B_TRUE)) {
56450Sstevel@tonic-gate goto release_minor;
56460Sstevel@tonic-gate }
56470Sstevel@tonic-gate
56480Sstevel@tonic-gate pri_attributes = STRUCT_FGETP(generate_key_pair, kp_private_attributes);
56496424Skrishna if (!copyin_attributes(mode, sp, pri_count, pri_attributes,
56506424Skrishna &k_pri_attrs, &k_pri_attrs_size, NULL, &rv, &error,
56516424Skrishna &pri_rctl_bytes, &pri_rctl_chk, B_TRUE)) {
56520Sstevel@tonic-gate goto release_minor;
56530Sstevel@tonic-gate }
56540Sstevel@tonic-gate
56550Sstevel@tonic-gate KCF_WRAP_KEY_OPS_PARAMS(¶ms, KCF_OP_KEY_GENERATE_PAIR,
56560Sstevel@tonic-gate sp->sd_provider_session->ps_session, &mech, k_pub_attrs,
56570Sstevel@tonic-gate pub_count, &pub_handle, k_pri_attrs, pri_count, &pri_handle,
56580Sstevel@tonic-gate NULL, NULL, 0);
56590Sstevel@tonic-gate
56600Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
56610Sstevel@tonic-gate
56620Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) {
56630Sstevel@tonic-gate STRUCT_FSET(generate_key_pair, kp_public_handle, pub_handle);
56640Sstevel@tonic-gate STRUCT_FSET(generate_key_pair, kp_private_handle, pri_handle);
56650Sstevel@tonic-gate }
56660Sstevel@tonic-gate
56670Sstevel@tonic-gate release_minor:
56686424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
56696424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, pub_rctl_bytes, pub_rctl_chk);
56706424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, pri_rctl_bytes, pri_rctl_chk);
56710Sstevel@tonic-gate
56720Sstevel@tonic-gate if (k_pub_attrs != NULL)
56730Sstevel@tonic-gate kmem_free(k_pub_attrs, k_pub_attrs_size);
56740Sstevel@tonic-gate
56750Sstevel@tonic-gate if (k_pri_attrs != NULL)
56760Sstevel@tonic-gate kmem_free(k_pri_attrs, k_pri_attrs_size);
56770Sstevel@tonic-gate
56780Sstevel@tonic-gate if (error != 0)
56790Sstevel@tonic-gate goto out;
56800Sstevel@tonic-gate
56810Sstevel@tonic-gate STRUCT_FSET(generate_key_pair, kp_return_value, rv);
56820Sstevel@tonic-gate if (copyout(STRUCT_BUF(generate_key_pair), arg,
56830Sstevel@tonic-gate STRUCT_SIZE(generate_key_pair)) != 0) {
56840Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) {
56850Sstevel@tonic-gate KCF_WRAP_OBJECT_OPS_PARAMS(¶ms,
56860Sstevel@tonic-gate KCF_OP_OBJECT_DESTROY,
56870Sstevel@tonic-gate sp->sd_provider_session->ps_session, pub_handle,
56880Sstevel@tonic-gate NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
56890Sstevel@tonic-gate
56900Sstevel@tonic-gate (void) kcf_submit_request(real_provider, NULL,
56910Sstevel@tonic-gate NULL, ¶ms, B_FALSE);
56920Sstevel@tonic-gate
56930Sstevel@tonic-gate KCF_WRAP_OBJECT_OPS_PARAMS(¶ms,
56940Sstevel@tonic-gate KCF_OP_OBJECT_DESTROY,
56950Sstevel@tonic-gate sp->sd_provider_session->ps_session, pri_handle,
56960Sstevel@tonic-gate NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
56970Sstevel@tonic-gate
56980Sstevel@tonic-gate (void) kcf_submit_request(real_provider, NULL,
56990Sstevel@tonic-gate NULL, ¶ms, B_FALSE);
57000Sstevel@tonic-gate
57010Sstevel@tonic-gate error = EFAULT;
57020Sstevel@tonic-gate }
57030Sstevel@tonic-gate }
57040Sstevel@tonic-gate out:
57056424Skrishna CRYPTO_SESSION_RELE(sp);
57060Sstevel@tonic-gate crypto_release_minor(cm);
5707904Smcpowers
5708904Smcpowers if (real_provider != NULL) {
5709904Smcpowers crypto_free_mech(real_provider,
5710904Smcpowers allocated_by_crypto_module, &mech);
5711904Smcpowers KCF_PROV_REFRELE(real_provider);
5712904Smcpowers }
57130Sstevel@tonic-gate return (error);
57140Sstevel@tonic-gate }
57150Sstevel@tonic-gate
57160Sstevel@tonic-gate /* ARGSUSED */
57170Sstevel@tonic-gate static int
nostore_generate_key_pair(dev_t dev,caddr_t arg,int mode,int * rval)57184219Smcpowers nostore_generate_key_pair(dev_t dev, caddr_t arg, int mode, int *rval)
57194219Smcpowers {
57204219Smcpowers STRUCT_DECL(crypto_nostore_generate_key_pair, generate_key_pair);
572111413Sopensolaris@drydog.com #ifdef _LP64
572211413Sopensolaris@drydog.com STRUCT_DECL(crypto_object_attribute, oa);
572311413Sopensolaris@drydog.com #else
57244219Smcpowers /* LINTED E_FUNC_SET_NOT_USED */
57254219Smcpowers STRUCT_DECL(crypto_object_attribute, oa);
572611413Sopensolaris@drydog.com #endif
57274219Smcpowers kcf_provider_desc_t *real_provider = NULL;
57284219Smcpowers kcf_req_params_t params;
57294219Smcpowers crypto_mechanism_t mech;
57304219Smcpowers crypto_object_attribute_t *k_in_pub_attrs = NULL;
57314219Smcpowers crypto_object_attribute_t *k_in_pri_attrs = NULL;
57324219Smcpowers crypto_object_attribute_t *k_out_pub_attrs = NULL;
57334219Smcpowers crypto_object_attribute_t *k_out_pri_attrs = NULL;
57344219Smcpowers crypto_session_id_t session_id;
57354219Smcpowers crypto_minor_t *cm;
57364219Smcpowers crypto_session_data_t *sp = NULL;
57374219Smcpowers caddr_t in_pri_attributes;
57384219Smcpowers caddr_t in_pub_attributes;
57394219Smcpowers caddr_t out_pri_attributes;
57404219Smcpowers caddr_t out_pub_attributes;
57414219Smcpowers size_t k_in_pub_attrs_size, k_in_pri_attrs_size;
57424219Smcpowers size_t k_out_pub_attrs_size, k_out_pri_attrs_size;
57434219Smcpowers size_t mech_rctl_bytes = 0;
57446424Skrishna boolean_t mech_rctl_chk = B_FALSE;
57454219Smcpowers size_t in_pub_rctl_bytes = 0;
57466424Skrishna boolean_t in_pub_rctl_chk = B_FALSE;
57474219Smcpowers size_t in_pri_rctl_bytes = 0;
57486424Skrishna boolean_t in_pri_rctl_chk = B_FALSE;
57494219Smcpowers size_t out_pub_rctl_bytes = 0;
57506424Skrishna boolean_t out_pub_rctl_chk = B_FALSE;
57514219Smcpowers size_t out_pri_rctl_bytes = 0;
57526424Skrishna boolean_t out_pri_rctl_chk = B_FALSE;
57534219Smcpowers uint_t in_pub_count;
57544219Smcpowers uint_t in_pri_count;
57554219Smcpowers uint_t out_pub_count;
57564219Smcpowers uint_t out_pri_count;
57574219Smcpowers int error = 0;
57584219Smcpowers int rv;
57594219Smcpowers boolean_t allocated_by_crypto_module = B_FALSE;
57604219Smcpowers caddr_t u_pub_attrs = NULL;
57614219Smcpowers caddr_t u_pri_attrs = NULL;
57624219Smcpowers
57634219Smcpowers STRUCT_INIT(generate_key_pair, mode);
57644219Smcpowers STRUCT_INIT(oa, mode);
57654219Smcpowers
57664219Smcpowers if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
57674219Smcpowers cmn_err(CE_WARN,
57684219Smcpowers "nostore_generate_key_pair: failed holding minor");
57694219Smcpowers return (ENXIO);
57704219Smcpowers }
57714219Smcpowers
57724219Smcpowers if (copyin(arg, STRUCT_BUF(generate_key_pair),
57734219Smcpowers STRUCT_SIZE(generate_key_pair)) != 0) {
57744219Smcpowers crypto_release_minor(cm);
57754219Smcpowers return (EFAULT);
57764219Smcpowers }
57774219Smcpowers
57784219Smcpowers session_id = STRUCT_FGET(generate_key_pair, nkp_session);
57794219Smcpowers
57804219Smcpowers if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
57814219Smcpowers goto release_minor;
57824219Smcpowers }
57834219Smcpowers
57844219Smcpowers bcopy(STRUCT_FADDR(generate_key_pair, nkp_mechanism), &mech.cm_type,
57854219Smcpowers sizeof (crypto_mech_type_t));
57864219Smcpowers
578710444SVladimir.Kotal@Sun.COM if ((rv = kcf_get_hardware_provider(mech.cm_type, NULL,
5788*12304SValerie.Fenwick@Oracle.COM CRYPTO_MECH_INVALID, NULL, sp->sd_provider,
578910444SVladimir.Kotal@Sun.COM &real_provider, CRYPTO_FG_GENERATE_KEY_PAIR)) != CRYPTO_SUCCESS) {
57904219Smcpowers goto release_minor;
57914219Smcpowers }
57924219Smcpowers
57934219Smcpowers rv = crypto_provider_copyin_mech_param(real_provider,
57944219Smcpowers STRUCT_FADDR(generate_key_pair, nkp_mechanism), &mech, mode,
57954219Smcpowers &error);
57964219Smcpowers
57974219Smcpowers if (rv == CRYPTO_NOT_SUPPORTED) {
57984219Smcpowers allocated_by_crypto_module = B_TRUE;
57996424Skrishna if (!copyin_mech(mode, sp, STRUCT_FADDR(generate_key_pair,
58006424Skrishna nkp_mechanism), &mech, &mech_rctl_bytes,
58016424Skrishna &mech_rctl_chk, &rv, &error)) {
58024219Smcpowers goto release_minor;
58034219Smcpowers }
58044219Smcpowers } else {
58054219Smcpowers if (rv != CRYPTO_SUCCESS)
58064219Smcpowers goto release_minor;
58074219Smcpowers }
58084219Smcpowers
58094219Smcpowers in_pub_count = STRUCT_FGET(generate_key_pair, nkp_in_public_count);
58104219Smcpowers in_pri_count = STRUCT_FGET(generate_key_pair, nkp_in_private_count);
58114219Smcpowers
58124219Smcpowers in_pub_attributes = STRUCT_FGETP(generate_key_pair,
58134219Smcpowers nkp_in_public_attributes);
58146424Skrishna if (!copyin_attributes(mode, sp, in_pub_count, in_pub_attributes,
58154219Smcpowers &k_in_pub_attrs, &k_in_pub_attrs_size, NULL, &rv, &error,
58166424Skrishna &in_pub_rctl_bytes, &in_pub_rctl_chk, B_TRUE)) {
58174219Smcpowers goto release_minor;
58184219Smcpowers }
58194219Smcpowers
58204219Smcpowers in_pri_attributes = STRUCT_FGETP(generate_key_pair,
58214219Smcpowers nkp_in_private_attributes);
58226424Skrishna if (!copyin_attributes(mode, sp, in_pri_count, in_pri_attributes,
58234219Smcpowers &k_in_pri_attrs, &k_in_pri_attrs_size, NULL, &rv, &error,
58246424Skrishna &in_pri_rctl_bytes, &in_pri_rctl_chk, B_TRUE)) {
58254219Smcpowers goto release_minor;
58264219Smcpowers }
58274219Smcpowers
58284219Smcpowers out_pub_count = STRUCT_FGET(generate_key_pair, nkp_out_public_count);
58294219Smcpowers out_pri_count = STRUCT_FGET(generate_key_pair, nkp_out_private_count);
58304219Smcpowers
58314219Smcpowers out_pub_attributes = STRUCT_FGETP(generate_key_pair,
58324219Smcpowers nkp_out_public_attributes);
58336424Skrishna if (!copyin_attributes(mode, sp, out_pub_count, out_pub_attributes,
58344219Smcpowers &k_out_pub_attrs, &k_out_pub_attrs_size, &u_pub_attrs, &rv, &error,
58356424Skrishna &out_pub_rctl_bytes, &out_pub_rctl_chk, B_FALSE)) {
58364219Smcpowers goto release_minor;
58374219Smcpowers }
58384219Smcpowers
58394219Smcpowers out_pri_attributes = STRUCT_FGETP(generate_key_pair,
58404219Smcpowers nkp_out_private_attributes);
58416424Skrishna if (!copyin_attributes(mode, sp, out_pri_count, out_pri_attributes,
58424219Smcpowers &k_out_pri_attrs, &k_out_pri_attrs_size, &u_pri_attrs, &rv, &error,
58436424Skrishna &out_pri_rctl_bytes, &out_pri_rctl_chk, B_FALSE)) {
58444219Smcpowers goto release_minor;
58454219Smcpowers }
58464219Smcpowers
58474219Smcpowers KCF_WRAP_NOSTORE_KEY_OPS_PARAMS(¶ms, KCF_OP_KEY_GENERATE_PAIR,
58484219Smcpowers sp->sd_provider_session->ps_session, &mech, k_in_pub_attrs,
58494219Smcpowers in_pub_count, k_in_pri_attrs, in_pri_count, NULL, k_out_pub_attrs,
58504219Smcpowers out_pub_count, k_out_pri_attrs, out_pri_count);
58514219Smcpowers
58524219Smcpowers rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
58534219Smcpowers
58544219Smcpowers if (rv == CRYPTO_SUCCESS) {
58554219Smcpowers error = copyout_attributes(mode, out_pub_attributes,
58564219Smcpowers out_pub_count, k_out_pub_attrs, u_pub_attrs);
58574219Smcpowers if (error != CRYPTO_SUCCESS)
58584219Smcpowers goto release_minor;
58594219Smcpowers error = copyout_attributes(mode, out_pri_attributes,
58604219Smcpowers out_pri_count, k_out_pri_attrs, u_pri_attrs);
58614219Smcpowers }
58624219Smcpowers
58634219Smcpowers release_minor:
58646424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
58656424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, in_pub_rctl_bytes, in_pub_rctl_chk);
58666424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, in_pri_rctl_bytes, in_pri_rctl_chk);
58676424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, out_pub_rctl_bytes,
58686424Skrishna out_pub_rctl_chk);
58696424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, out_pri_rctl_bytes,
58706424Skrishna out_pri_rctl_chk);
58714219Smcpowers
58724219Smcpowers if (k_in_pub_attrs != NULL)
58734219Smcpowers kmem_free(k_in_pub_attrs, k_in_pub_attrs_size);
58744219Smcpowers
58754219Smcpowers if (k_in_pri_attrs != NULL)
58764219Smcpowers kmem_free(k_in_pri_attrs, k_in_pri_attrs_size);
58774219Smcpowers
58784219Smcpowers if (k_out_pub_attrs != NULL)
58794219Smcpowers kmem_free(k_out_pub_attrs, k_out_pub_attrs_size);
58804219Smcpowers
58814219Smcpowers if (k_out_pri_attrs != NULL) {
58824219Smcpowers bzero(k_out_pri_attrs, k_out_pri_attrs_size);
58834219Smcpowers kmem_free(k_out_pri_attrs, k_out_pri_attrs_size);
58844219Smcpowers }
58854219Smcpowers
58864219Smcpowers if (u_pub_attrs != NULL)
58874219Smcpowers kmem_free(u_pub_attrs, out_pub_count * STRUCT_SIZE(oa));
58884219Smcpowers
58894219Smcpowers if (u_pri_attrs != NULL)
58904219Smcpowers kmem_free(u_pri_attrs, out_pri_count * STRUCT_SIZE(oa));
58914219Smcpowers
58924219Smcpowers if (error != 0)
58934219Smcpowers goto out;
58944219Smcpowers
58954219Smcpowers STRUCT_FSET(generate_key_pair, nkp_return_value, rv);
58964219Smcpowers if (copyout(STRUCT_BUF(generate_key_pair), arg,
58974219Smcpowers STRUCT_SIZE(generate_key_pair)) != 0) {
58984219Smcpowers error = EFAULT;
58994219Smcpowers }
59004219Smcpowers out:
59016424Skrishna CRYPTO_SESSION_RELE(sp);
59024219Smcpowers crypto_release_minor(cm);
59034219Smcpowers
59044219Smcpowers if (real_provider != NULL) {
59054219Smcpowers crypto_free_mech(real_provider,
59064219Smcpowers allocated_by_crypto_module, &mech);
59074219Smcpowers KCF_PROV_REFRELE(real_provider);
59084219Smcpowers }
59094219Smcpowers return (error);
59104219Smcpowers }
59114219Smcpowers
59124219Smcpowers /* ARGSUSED */
59134219Smcpowers static int
object_wrap_key(dev_t dev,caddr_t arg,int mode,int * rval)59140Sstevel@tonic-gate object_wrap_key(dev_t dev, caddr_t arg, int mode, int *rval)
59150Sstevel@tonic-gate {
59160Sstevel@tonic-gate STRUCT_DECL(crypto_object_wrap_key, wrap_key);
5917904Smcpowers kcf_provider_desc_t *real_provider = NULL;
59180Sstevel@tonic-gate kcf_req_params_t params;
59190Sstevel@tonic-gate crypto_mechanism_t mech;
59200Sstevel@tonic-gate crypto_key_t key;
59210Sstevel@tonic-gate crypto_session_id_t session_id;
59220Sstevel@tonic-gate crypto_minor_t *cm;
59236424Skrishna crypto_session_data_t *sp = NULL;
59240Sstevel@tonic-gate crypto_object_id_t handle;
59250Sstevel@tonic-gate size_t mech_rctl_bytes = 0, key_rctl_bytes = 0;
59266424Skrishna boolean_t mech_rctl_chk = B_FALSE;
59276424Skrishna boolean_t key_rctl_chk = B_FALSE;
59280Sstevel@tonic-gate size_t wrapped_key_rctl_bytes = 0;
59296424Skrishna boolean_t wrapped_key_rctl_chk = B_FALSE;
59300Sstevel@tonic-gate size_t wrapped_key_len, new_wrapped_key_len;
59310Sstevel@tonic-gate uchar_t *wrapped_key = NULL;
59320Sstevel@tonic-gate char *wrapped_key_buffer;
59330Sstevel@tonic-gate int error = 0;
59340Sstevel@tonic-gate int rv;
5935904Smcpowers boolean_t allocated_by_crypto_module = B_FALSE;
59360Sstevel@tonic-gate
59370Sstevel@tonic-gate STRUCT_INIT(wrap_key, mode);
59380Sstevel@tonic-gate
59390Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
59400Sstevel@tonic-gate cmn_err(CE_WARN, "object_wrap_key: failed holding minor");
59410Sstevel@tonic-gate return (ENXIO);
59420Sstevel@tonic-gate }
59430Sstevel@tonic-gate
59440Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(wrap_key), STRUCT_SIZE(wrap_key)) != 0) {
59450Sstevel@tonic-gate crypto_release_minor(cm);
59460Sstevel@tonic-gate return (EFAULT);
59470Sstevel@tonic-gate }
59480Sstevel@tonic-gate
59490Sstevel@tonic-gate bzero(&key, sizeof (crypto_key_t));
59500Sstevel@tonic-gate
59510Sstevel@tonic-gate session_id = STRUCT_FGET(wrap_key, wk_session);
59520Sstevel@tonic-gate
59530Sstevel@tonic-gate if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
59546424Skrishna goto out;
59550Sstevel@tonic-gate }
59560Sstevel@tonic-gate
5957904Smcpowers bcopy(STRUCT_FADDR(wrap_key, wk_mechanism), &mech.cm_type,
5958904Smcpowers sizeof (crypto_mech_type_t));
5959904Smcpowers
596010444SVladimir.Kotal@Sun.COM /* We need the key length for provider selection so copy it in now. */
596110444SVladimir.Kotal@Sun.COM if (!copyin_key(mode, sp, STRUCT_FADDR(wrap_key, wk_wrapping_key), &key,
596210444SVladimir.Kotal@Sun.COM &key_rctl_bytes, &key_rctl_chk, &rv, &error)) {
596310444SVladimir.Kotal@Sun.COM goto out;
596410444SVladimir.Kotal@Sun.COM }
596510444SVladimir.Kotal@Sun.COM
596610444SVladimir.Kotal@Sun.COM wrapped_key_len = STRUCT_FGET(wrap_key, wk_wrapped_key_len);
596710444SVladimir.Kotal@Sun.COM
596810444SVladimir.Kotal@Sun.COM if ((rv = kcf_get_hardware_provider(mech.cm_type, &key,
5969*12304SValerie.Fenwick@Oracle.COM CRYPTO_MECH_INVALID, NULL, sp->sd_provider,
597010444SVladimir.Kotal@Sun.COM &real_provider, CRYPTO_FG_WRAP)) != CRYPTO_SUCCESS) {
59710Sstevel@tonic-gate goto out;
59720Sstevel@tonic-gate }
59730Sstevel@tonic-gate
5974904Smcpowers rv = crypto_provider_copyin_mech_param(real_provider,
5975904Smcpowers STRUCT_FADDR(wrap_key, wk_mechanism), &mech, mode, &error);
5976904Smcpowers
5977904Smcpowers if (rv == CRYPTO_NOT_SUPPORTED) {
5978904Smcpowers allocated_by_crypto_module = B_TRUE;
59796424Skrishna if (!copyin_mech(mode, sp, STRUCT_FADDR(wrap_key, wk_mechanism),
59806424Skrishna &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) {
5981904Smcpowers goto out;
5982904Smcpowers }
5983904Smcpowers } else {
5984904Smcpowers if (rv != CRYPTO_SUCCESS)
5985904Smcpowers goto out;
59860Sstevel@tonic-gate }
59870Sstevel@tonic-gate
59880Sstevel@tonic-gate /*
59890Sstevel@tonic-gate * Don't allocate output buffer unless both buffer pointer and
59900Sstevel@tonic-gate * buffer length are not NULL or 0 (length).
59910Sstevel@tonic-gate */
59920Sstevel@tonic-gate wrapped_key_buffer = STRUCT_FGETP(wrap_key, wk_wrapped_key);
59930Sstevel@tonic-gate if (wrapped_key_buffer == NULL || wrapped_key_len == 0) {
59940Sstevel@tonic-gate wrapped_key_len = 0;
59950Sstevel@tonic-gate }
59960Sstevel@tonic-gate
59970Sstevel@tonic-gate if (wrapped_key_len > crypto_max_buffer_len) {
59980Sstevel@tonic-gate cmn_err(CE_NOTE, "object_wrap_key: buffer greater than %ld "
59990Sstevel@tonic-gate "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
60000Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD;
60010Sstevel@tonic-gate goto out;
60020Sstevel@tonic-gate }
60030Sstevel@tonic-gate
60046424Skrishna if ((rv = CRYPTO_BUFFER_CHECK(sp, wrapped_key_len,
60056424Skrishna wrapped_key_rctl_chk)) != CRYPTO_SUCCESS) {
60060Sstevel@tonic-gate goto out;
60070Sstevel@tonic-gate }
60080Sstevel@tonic-gate
60090Sstevel@tonic-gate /* new_wrapped_key_len can be modified by the provider */
60100Sstevel@tonic-gate wrapped_key_rctl_bytes = new_wrapped_key_len = wrapped_key_len;
60110Sstevel@tonic-gate wrapped_key = kmem_alloc(wrapped_key_len, KM_SLEEP);
60120Sstevel@tonic-gate
60130Sstevel@tonic-gate handle = STRUCT_FGET(wrap_key, wk_object_handle);
60140Sstevel@tonic-gate KCF_WRAP_KEY_OPS_PARAMS(¶ms, KCF_OP_KEY_WRAP,
60150Sstevel@tonic-gate sp->sd_provider_session->ps_session, &mech, NULL, 0, &handle,
60160Sstevel@tonic-gate NULL, 0, NULL, &key, wrapped_key, &new_wrapped_key_len);
60170Sstevel@tonic-gate
60180Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
60190Sstevel@tonic-gate
60200Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) {
60210Sstevel@tonic-gate if (wrapped_key_len != 0 && copyout(wrapped_key,
60220Sstevel@tonic-gate wrapped_key_buffer, new_wrapped_key_len) != 0) {
60230Sstevel@tonic-gate error = EFAULT;
60240Sstevel@tonic-gate }
602511413Sopensolaris@drydog.com STRUCT_FSET(wrap_key, wk_wrapped_key_len,
602611413Sopensolaris@drydog.com (ulong_t)new_wrapped_key_len);
60270Sstevel@tonic-gate }
60280Sstevel@tonic-gate
60290Sstevel@tonic-gate if (rv == CRYPTO_BUFFER_TOO_SMALL) {
60300Sstevel@tonic-gate /*
60310Sstevel@tonic-gate * The providers return CRYPTO_BUFFER_TOO_SMALL even for case 1
60320Sstevel@tonic-gate * of section 11.2 of the pkcs11 spec. We catch it here and
60330Sstevel@tonic-gate * provide the correct pkcs11 return value.
60340Sstevel@tonic-gate */
60350Sstevel@tonic-gate if (STRUCT_FGETP(wrap_key, wk_wrapped_key) == NULL)
60360Sstevel@tonic-gate rv = CRYPTO_SUCCESS;
603711413Sopensolaris@drydog.com STRUCT_FSET(wrap_key, wk_wrapped_key_len,
603811413Sopensolaris@drydog.com (ulong_t)new_wrapped_key_len);
60390Sstevel@tonic-gate }
60406424Skrishna
60410Sstevel@tonic-gate out:
60426424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
60436424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk);
60446424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, wrapped_key_rctl_bytes,
60456424Skrishna wrapped_key_rctl_chk);
60460Sstevel@tonic-gate CRYPTO_SESSION_RELE(sp);
60470Sstevel@tonic-gate
60480Sstevel@tonic-gate crypto_release_minor(cm);
60490Sstevel@tonic-gate
6050904Smcpowers if (real_provider != NULL) {
6051904Smcpowers crypto_free_mech(real_provider,
6052904Smcpowers allocated_by_crypto_module, &mech);
6053904Smcpowers KCF_PROV_REFRELE(real_provider);
6054904Smcpowers }
6055904Smcpowers
60560Sstevel@tonic-gate if (wrapped_key != NULL)
60570Sstevel@tonic-gate kmem_free(wrapped_key, wrapped_key_len);
60580Sstevel@tonic-gate
60590Sstevel@tonic-gate free_crypto_key(&key);
60600Sstevel@tonic-gate
60610Sstevel@tonic-gate if (error != 0)
60620Sstevel@tonic-gate return (error);
60630Sstevel@tonic-gate
60640Sstevel@tonic-gate STRUCT_FSET(wrap_key, wk_return_value, rv);
60650Sstevel@tonic-gate if (copyout(STRUCT_BUF(wrap_key), arg, STRUCT_SIZE(wrap_key)) != 0) {
60660Sstevel@tonic-gate return (EFAULT);
60670Sstevel@tonic-gate }
60680Sstevel@tonic-gate return (0);
60690Sstevel@tonic-gate }
60700Sstevel@tonic-gate
60710Sstevel@tonic-gate /* ARGSUSED */
60720Sstevel@tonic-gate static int
object_unwrap_key(dev_t dev,caddr_t arg,int mode,int * rval)60730Sstevel@tonic-gate object_unwrap_key(dev_t dev, caddr_t arg, int mode, int *rval)
60740Sstevel@tonic-gate {
60750Sstevel@tonic-gate STRUCT_DECL(crypto_object_unwrap_key, unwrap_key);
6076904Smcpowers kcf_provider_desc_t *real_provider = NULL;
60770Sstevel@tonic-gate kcf_req_params_t params;
60780Sstevel@tonic-gate crypto_mechanism_t mech;
60790Sstevel@tonic-gate crypto_key_t unwrapping_key;
60800Sstevel@tonic-gate crypto_session_id_t session_id;
60810Sstevel@tonic-gate crypto_minor_t *cm;
60820Sstevel@tonic-gate crypto_session_data_t *sp = NULL;
60830Sstevel@tonic-gate crypto_object_id_t handle;
60840Sstevel@tonic-gate crypto_object_attribute_t *k_attrs = NULL;
60850Sstevel@tonic-gate size_t k_attrs_size;
60860Sstevel@tonic-gate size_t mech_rctl_bytes = 0, unwrapping_key_rctl_bytes = 0;
60876424Skrishna boolean_t mech_rctl_chk = B_FALSE;
60886424Skrishna boolean_t unwrapping_key_rctl_chk = B_FALSE;
60890Sstevel@tonic-gate size_t wrapped_key_rctl_bytes = 0, k_attrs_rctl_bytes = 0;
60906424Skrishna boolean_t wrapped_key_rctl_chk = B_FALSE;
60916424Skrishna boolean_t k_attrs_rctl_chk = B_FALSE;
60920Sstevel@tonic-gate size_t wrapped_key_len;
60930Sstevel@tonic-gate uchar_t *wrapped_key = NULL;
60940Sstevel@tonic-gate int error = 0;
60950Sstevel@tonic-gate int rv;
60960Sstevel@tonic-gate uint_t count;
60970Sstevel@tonic-gate caddr_t uk_attributes;
6098904Smcpowers boolean_t allocated_by_crypto_module = B_FALSE;
60990Sstevel@tonic-gate
61000Sstevel@tonic-gate STRUCT_INIT(unwrap_key, mode);
61010Sstevel@tonic-gate
61020Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
61030Sstevel@tonic-gate cmn_err(CE_WARN, "object_unwrap_key: failed holding minor");
61040Sstevel@tonic-gate return (ENXIO);
61050Sstevel@tonic-gate }
61060Sstevel@tonic-gate
61070Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(unwrap_key), STRUCT_SIZE(unwrap_key)) != 0) {
61080Sstevel@tonic-gate crypto_release_minor(cm);
61090Sstevel@tonic-gate return (EFAULT);
61100Sstevel@tonic-gate }
61110Sstevel@tonic-gate
61120Sstevel@tonic-gate bzero(&unwrapping_key, sizeof (unwrapping_key));
61130Sstevel@tonic-gate
61140Sstevel@tonic-gate session_id = STRUCT_FGET(unwrap_key, uk_session);
61150Sstevel@tonic-gate
61160Sstevel@tonic-gate if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
61170Sstevel@tonic-gate goto release_minor;
61180Sstevel@tonic-gate }
61190Sstevel@tonic-gate
6120904Smcpowers bcopy(STRUCT_FADDR(unwrap_key, uk_mechanism), &mech.cm_type,
6121904Smcpowers sizeof (crypto_mech_type_t));
6122904Smcpowers
612310444SVladimir.Kotal@Sun.COM /* We need the key length for provider selection so copy it in now. */
612410444SVladimir.Kotal@Sun.COM if (!copyin_key(mode, sp, STRUCT_FADDR(unwrap_key, uk_unwrapping_key),
612510444SVladimir.Kotal@Sun.COM &unwrapping_key, &unwrapping_key_rctl_bytes,
612610444SVladimir.Kotal@Sun.COM &unwrapping_key_rctl_chk, &rv, &error)) {
612710444SVladimir.Kotal@Sun.COM goto release_minor;
612810444SVladimir.Kotal@Sun.COM }
612910444SVladimir.Kotal@Sun.COM
613010444SVladimir.Kotal@Sun.COM if ((rv = kcf_get_hardware_provider(mech.cm_type, &unwrapping_key,
6131*12304SValerie.Fenwick@Oracle.COM CRYPTO_MECH_INVALID, NULL, sp->sd_provider,
613210444SVladimir.Kotal@Sun.COM &real_provider, CRYPTO_FG_UNWRAP)) != CRYPTO_SUCCESS) {
61330Sstevel@tonic-gate goto release_minor;
61340Sstevel@tonic-gate }
61350Sstevel@tonic-gate
6136904Smcpowers rv = crypto_provider_copyin_mech_param(real_provider,
6137904Smcpowers STRUCT_FADDR(unwrap_key, uk_mechanism), &mech, mode, &error);
6138904Smcpowers
6139904Smcpowers if (rv == CRYPTO_NOT_SUPPORTED) {
6140904Smcpowers allocated_by_crypto_module = B_TRUE;
61416424Skrishna if (!copyin_mech(mode, sp,
61426424Skrishna STRUCT_FADDR(unwrap_key, uk_mechanism),
61436424Skrishna &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) {
6144904Smcpowers goto release_minor;
6145904Smcpowers }
6146904Smcpowers } else {
6147904Smcpowers if (rv != CRYPTO_SUCCESS)
6148904Smcpowers goto release_minor;
61490Sstevel@tonic-gate }
61500Sstevel@tonic-gate
61510Sstevel@tonic-gate count = STRUCT_FGET(unwrap_key, uk_count);
61520Sstevel@tonic-gate uk_attributes = STRUCT_FGETP(unwrap_key, uk_attributes);
61536424Skrishna if (!copyin_attributes(mode, sp, count, uk_attributes, &k_attrs,
61546424Skrishna &k_attrs_size, NULL, &rv, &error, &k_attrs_rctl_bytes,
61556424Skrishna &k_attrs_rctl_chk, B_TRUE)) {
61560Sstevel@tonic-gate goto release_minor;
61570Sstevel@tonic-gate }
61580Sstevel@tonic-gate
61590Sstevel@tonic-gate wrapped_key_len = STRUCT_FGET(unwrap_key, uk_wrapped_key_len);
61600Sstevel@tonic-gate if (wrapped_key_len > crypto_max_buffer_len) {
61610Sstevel@tonic-gate cmn_err(CE_NOTE, "object_unwrap_key: buffer greater than %ld "
61620Sstevel@tonic-gate "bytes, pid = %d", crypto_max_buffer_len, curproc->p_pid);
61630Sstevel@tonic-gate rv = CRYPTO_ARGUMENTS_BAD;
61640Sstevel@tonic-gate goto release_minor;
61650Sstevel@tonic-gate }
61660Sstevel@tonic-gate
61676424Skrishna if ((rv = CRYPTO_BUFFER_CHECK(sp, wrapped_key_len,
61686424Skrishna wrapped_key_rctl_chk)) != CRYPTO_SUCCESS) {
61690Sstevel@tonic-gate goto release_minor;
61700Sstevel@tonic-gate }
61710Sstevel@tonic-gate wrapped_key_rctl_bytes = wrapped_key_len;
61720Sstevel@tonic-gate wrapped_key = kmem_alloc(wrapped_key_len, KM_SLEEP);
61730Sstevel@tonic-gate
61740Sstevel@tonic-gate if (wrapped_key_len != 0 && copyin(STRUCT_FGETP(unwrap_key,
61750Sstevel@tonic-gate uk_wrapped_key), wrapped_key, wrapped_key_len) != 0) {
61760Sstevel@tonic-gate error = EFAULT;
61770Sstevel@tonic-gate goto release_minor;
61780Sstevel@tonic-gate }
61790Sstevel@tonic-gate
61800Sstevel@tonic-gate /* wrapped_key_len is not modified by the unwrap operation */
61810Sstevel@tonic-gate KCF_WRAP_KEY_OPS_PARAMS(¶ms, KCF_OP_KEY_UNWRAP,
61820Sstevel@tonic-gate sp->sd_provider_session->ps_session, &mech, k_attrs, count, &handle,
61830Sstevel@tonic-gate NULL, 0, NULL, &unwrapping_key, wrapped_key, &wrapped_key_len);
61840Sstevel@tonic-gate
61850Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
61860Sstevel@tonic-gate
61870Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS)
61880Sstevel@tonic-gate STRUCT_FSET(unwrap_key, uk_object_handle, handle);
61890Sstevel@tonic-gate
61900Sstevel@tonic-gate release_minor:
61916424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
61926424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, unwrapping_key_rctl_bytes,
61936424Skrishna unwrapping_key_rctl_chk);
61946424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, wrapped_key_rctl_bytes,
61956424Skrishna wrapped_key_rctl_chk);
61966424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, k_attrs_rctl_bytes,
61976424Skrishna k_attrs_rctl_chk);
61980Sstevel@tonic-gate
61990Sstevel@tonic-gate if (k_attrs != NULL)
62000Sstevel@tonic-gate kmem_free(k_attrs, k_attrs_size);
62010Sstevel@tonic-gate
62020Sstevel@tonic-gate if (wrapped_key != NULL)
62030Sstevel@tonic-gate kmem_free(wrapped_key, wrapped_key_len);
62040Sstevel@tonic-gate
62050Sstevel@tonic-gate free_crypto_key(&unwrapping_key);
62060Sstevel@tonic-gate
62070Sstevel@tonic-gate if (error != 0)
62080Sstevel@tonic-gate goto out;
62090Sstevel@tonic-gate
62100Sstevel@tonic-gate STRUCT_FSET(unwrap_key, uk_return_value, rv);
62110Sstevel@tonic-gate if (copyout(STRUCT_BUF(unwrap_key), arg,
62120Sstevel@tonic-gate STRUCT_SIZE(unwrap_key)) != 0) {
62130Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) {
62140Sstevel@tonic-gate KCF_WRAP_OBJECT_OPS_PARAMS(¶ms,
62150Sstevel@tonic-gate KCF_OP_OBJECT_DESTROY,
62160Sstevel@tonic-gate sp->sd_provider_session->ps_session, handle,
62170Sstevel@tonic-gate NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
62180Sstevel@tonic-gate
62190Sstevel@tonic-gate (void) kcf_submit_request(real_provider, NULL,
62200Sstevel@tonic-gate NULL, ¶ms, B_FALSE);
62210Sstevel@tonic-gate
62220Sstevel@tonic-gate error = EFAULT;
62230Sstevel@tonic-gate }
62240Sstevel@tonic-gate }
62250Sstevel@tonic-gate out:
62266424Skrishna CRYPTO_SESSION_RELE(sp);
62270Sstevel@tonic-gate crypto_release_minor(cm);
6228904Smcpowers
6229904Smcpowers if (real_provider != NULL) {
6230904Smcpowers crypto_free_mech(real_provider,
6231904Smcpowers allocated_by_crypto_module, &mech);
6232904Smcpowers KCF_PROV_REFRELE(real_provider);
6233904Smcpowers }
6234904Smcpowers
62350Sstevel@tonic-gate return (error);
62360Sstevel@tonic-gate }
62370Sstevel@tonic-gate
62380Sstevel@tonic-gate /* ARGSUSED */
62390Sstevel@tonic-gate static int
object_derive_key(dev_t dev,caddr_t arg,int mode,int * rval)62400Sstevel@tonic-gate object_derive_key(dev_t dev, caddr_t arg, int mode, int *rval)
62410Sstevel@tonic-gate {
62420Sstevel@tonic-gate STRUCT_DECL(crypto_derive_key, derive_key);
6243904Smcpowers kcf_provider_desc_t *real_provider = NULL;
62440Sstevel@tonic-gate kcf_req_params_t params;
62450Sstevel@tonic-gate crypto_object_attribute_t *k_attrs = NULL;
62460Sstevel@tonic-gate crypto_mechanism_t mech;
62470Sstevel@tonic-gate crypto_key_t base_key;
62480Sstevel@tonic-gate crypto_session_id_t session_id;
62490Sstevel@tonic-gate crypto_minor_t *cm;
62500Sstevel@tonic-gate crypto_session_data_t *sp = NULL;
62510Sstevel@tonic-gate crypto_object_id_t handle;
62520Sstevel@tonic-gate size_t k_attrs_size;
62530Sstevel@tonic-gate size_t key_rctl_bytes = 0, mech_rctl_bytes = 0;
62546424Skrishna boolean_t mech_rctl_chk = B_FALSE;
62556424Skrishna boolean_t key_rctl_chk = B_FALSE;
62560Sstevel@tonic-gate size_t attributes_rctl_bytes = 0;
62576424Skrishna boolean_t attributes_rctl_chk = B_FALSE;
62580Sstevel@tonic-gate caddr_t attributes;
62590Sstevel@tonic-gate uint_t count;
62600Sstevel@tonic-gate int error = 0;
62610Sstevel@tonic-gate int rv;
6262904Smcpowers boolean_t allocated_by_crypto_module = B_FALSE;
6263904Smcpowers boolean_t please_destroy_object = B_FALSE;
62640Sstevel@tonic-gate
62650Sstevel@tonic-gate STRUCT_INIT(derive_key, mode);
62660Sstevel@tonic-gate
62670Sstevel@tonic-gate if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
62680Sstevel@tonic-gate cmn_err(CE_WARN, "object_derive_key: failed holding minor");
62690Sstevel@tonic-gate return (ENXIO);
62700Sstevel@tonic-gate }
62710Sstevel@tonic-gate
62720Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(derive_key), STRUCT_SIZE(derive_key)) != 0) {
62730Sstevel@tonic-gate crypto_release_minor(cm);
62740Sstevel@tonic-gate return (EFAULT);
62750Sstevel@tonic-gate }
62760Sstevel@tonic-gate
62770Sstevel@tonic-gate bzero(&base_key, sizeof (base_key));
62780Sstevel@tonic-gate
62790Sstevel@tonic-gate session_id = STRUCT_FGET(derive_key, dk_session);
62800Sstevel@tonic-gate
62810Sstevel@tonic-gate if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
62820Sstevel@tonic-gate goto release_minor;
62830Sstevel@tonic-gate }
62840Sstevel@tonic-gate
6285904Smcpowers bcopy(STRUCT_FADDR(derive_key, dk_mechanism), &mech.cm_type,
6286904Smcpowers sizeof (crypto_mech_type_t));
6287904Smcpowers
628810444SVladimir.Kotal@Sun.COM /* We need the key length for provider selection so copy it in now. */
628910444SVladimir.Kotal@Sun.COM if (!copyin_key(mode, sp, STRUCT_FADDR(derive_key, dk_base_key),
629010444SVladimir.Kotal@Sun.COM &base_key, &key_rctl_bytes, &key_rctl_chk, &rv, &error)) {
629110444SVladimir.Kotal@Sun.COM goto release_minor;
629210444SVladimir.Kotal@Sun.COM }
629310444SVladimir.Kotal@Sun.COM
629410444SVladimir.Kotal@Sun.COM if ((rv = kcf_get_hardware_provider(mech.cm_type, &base_key,
6295*12304SValerie.Fenwick@Oracle.COM CRYPTO_MECH_INVALID, NULL, sp->sd_provider,
629610444SVladimir.Kotal@Sun.COM &real_provider, CRYPTO_FG_DERIVE)) != CRYPTO_SUCCESS) {
62970Sstevel@tonic-gate goto release_minor;
62980Sstevel@tonic-gate }
62990Sstevel@tonic-gate
6300904Smcpowers rv = crypto_provider_copyin_mech_param(real_provider,
6301904Smcpowers STRUCT_FADDR(derive_key, dk_mechanism), &mech, mode, &error);
6302904Smcpowers
6303904Smcpowers if (rv == CRYPTO_NOT_SUPPORTED) {
6304904Smcpowers allocated_by_crypto_module = B_TRUE;
63056424Skrishna if (!copyin_mech(mode, sp,
63066424Skrishna STRUCT_FADDR(derive_key, dk_mechanism),
63076424Skrishna &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) {
6308904Smcpowers goto release_minor;
6309904Smcpowers }
6310904Smcpowers } else {
6311904Smcpowers if (rv != CRYPTO_SUCCESS)
6312904Smcpowers goto release_minor;
63130Sstevel@tonic-gate }
63140Sstevel@tonic-gate
63150Sstevel@tonic-gate count = STRUCT_FGET(derive_key, dk_count);
63160Sstevel@tonic-gate
63170Sstevel@tonic-gate attributes = STRUCT_FGETP(derive_key, dk_attributes);
63186424Skrishna if (!copyin_attributes(mode, sp, count, attributes, &k_attrs,
63193916Skrishna &k_attrs_size, NULL, &rv, &error,
63206424Skrishna &attributes_rctl_bytes, &attributes_rctl_chk, B_TRUE)) {
63210Sstevel@tonic-gate goto release_minor;
63220Sstevel@tonic-gate }
63230Sstevel@tonic-gate
63240Sstevel@tonic-gate KCF_WRAP_KEY_OPS_PARAMS(¶ms, KCF_OP_KEY_DERIVE,
63250Sstevel@tonic-gate sp->sd_provider_session->ps_session, &mech, k_attrs, count,
63260Sstevel@tonic-gate &handle, NULL, 0, NULL, &base_key, NULL, NULL);
63270Sstevel@tonic-gate
63280Sstevel@tonic-gate rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
63290Sstevel@tonic-gate
6330904Smcpowers if (rv == CRYPTO_SUCCESS) {
63310Sstevel@tonic-gate STRUCT_FSET(derive_key, dk_object_handle, handle);
63320Sstevel@tonic-gate
6333904Smcpowers rv = crypto_provider_copyout_mech_param(real_provider,
6334904Smcpowers &mech, STRUCT_FADDR(derive_key, dk_mechanism),
6335904Smcpowers mode, &error);
6336904Smcpowers
6337904Smcpowers if (rv == CRYPTO_NOT_SUPPORTED) {
6338904Smcpowers rv = CRYPTO_SUCCESS;
6339904Smcpowers goto release_minor;
6340904Smcpowers }
6341904Smcpowers
6342904Smcpowers if (rv != CRYPTO_SUCCESS)
6343904Smcpowers please_destroy_object = B_TRUE;
6344904Smcpowers }
6345904Smcpowers
63460Sstevel@tonic-gate release_minor:
63476424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
63486424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk);
63496424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, attributes_rctl_bytes,
63506424Skrishna attributes_rctl_chk);
63510Sstevel@tonic-gate
63520Sstevel@tonic-gate if (k_attrs != NULL)
63530Sstevel@tonic-gate kmem_free(k_attrs, k_attrs_size);
63540Sstevel@tonic-gate
63550Sstevel@tonic-gate free_crypto_key(&base_key);
63560Sstevel@tonic-gate
63570Sstevel@tonic-gate if (error != 0)
63580Sstevel@tonic-gate goto out;
63590Sstevel@tonic-gate
63600Sstevel@tonic-gate STRUCT_FSET(derive_key, dk_return_value, rv);
63610Sstevel@tonic-gate if (copyout(STRUCT_BUF(derive_key), arg,
63620Sstevel@tonic-gate STRUCT_SIZE(derive_key)) != 0) {
63630Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) {
6364904Smcpowers please_destroy_object = B_TRUE;
63650Sstevel@tonic-gate error = EFAULT;
63660Sstevel@tonic-gate }
63670Sstevel@tonic-gate }
63680Sstevel@tonic-gate out:
6369904Smcpowers if (please_destroy_object) {
6370904Smcpowers KCF_WRAP_OBJECT_OPS_PARAMS(¶ms, KCF_OP_OBJECT_DESTROY,
6371904Smcpowers sp->sd_provider_session->ps_session, handle,
6372904Smcpowers NULL, 0, NULL, 0, NULL, NULL, 0, NULL);
6373904Smcpowers
6374904Smcpowers (void) kcf_submit_request(real_provider, NULL,
6375904Smcpowers NULL, ¶ms, B_FALSE);
6376904Smcpowers }
6377904Smcpowers
63786424Skrishna CRYPTO_SESSION_RELE(sp);
63790Sstevel@tonic-gate crypto_release_minor(cm);
6380904Smcpowers
6381904Smcpowers if (real_provider != NULL) {
6382904Smcpowers crypto_free_mech(real_provider,
6383904Smcpowers allocated_by_crypto_module, &mech);
6384904Smcpowers KCF_PROV_REFRELE(real_provider);
6385904Smcpowers }
63860Sstevel@tonic-gate return (error);
63870Sstevel@tonic-gate }
63880Sstevel@tonic-gate
63890Sstevel@tonic-gate /* ARGSUSED */
63900Sstevel@tonic-gate static int
nostore_derive_key(dev_t dev,caddr_t arg,int mode,int * rval)63914219Smcpowers nostore_derive_key(dev_t dev, caddr_t arg, int mode, int *rval)
63924219Smcpowers {
63934219Smcpowers STRUCT_DECL(crypto_nostore_derive_key, derive_key);
639411413Sopensolaris@drydog.com #ifdef _LP64
639511413Sopensolaris@drydog.com STRUCT_DECL(crypto_object_attribute, oa);
639611413Sopensolaris@drydog.com #else
63974219Smcpowers /* LINTED E_FUNC_SET_NOT_USED */
63984219Smcpowers STRUCT_DECL(crypto_object_attribute, oa);
639911413Sopensolaris@drydog.com #endif
64004219Smcpowers kcf_provider_desc_t *real_provider = NULL;
64014219Smcpowers kcf_req_params_t params;
64024219Smcpowers crypto_object_attribute_t *k_in_attrs = NULL;
64034219Smcpowers crypto_object_attribute_t *k_out_attrs = NULL;
64044219Smcpowers crypto_mechanism_t mech;
64054219Smcpowers crypto_key_t base_key;
64064219Smcpowers crypto_session_id_t session_id;
64074219Smcpowers crypto_minor_t *cm;
64084219Smcpowers crypto_session_data_t *sp = NULL;
64094219Smcpowers size_t k_in_attrs_size, k_out_attrs_size;
64104219Smcpowers size_t key_rctl_bytes = 0, mech_rctl_bytes = 0;
64116424Skrishna boolean_t mech_rctl_chk = B_FALSE;
64126424Skrishna boolean_t key_rctl_chk = B_FALSE;
64134219Smcpowers size_t in_attributes_rctl_bytes = 0;
64144219Smcpowers size_t out_attributes_rctl_bytes = 0;
64156424Skrishna boolean_t in_attributes_rctl_chk = B_FALSE;
64166424Skrishna boolean_t out_attributes_rctl_chk = B_FALSE;
64174219Smcpowers caddr_t in_attributes, out_attributes;
64184219Smcpowers uint_t in_count, out_count;
64194219Smcpowers int error = 0;
64204219Smcpowers int rv;
64214219Smcpowers boolean_t allocated_by_crypto_module = B_FALSE;
64224219Smcpowers caddr_t u_attrs = NULL;
64234219Smcpowers
64244219Smcpowers STRUCT_INIT(derive_key, mode);
64254219Smcpowers STRUCT_INIT(oa, mode);
64264219Smcpowers
64274219Smcpowers if ((cm = crypto_hold_minor(getminor(dev))) == NULL) {
64284219Smcpowers cmn_err(CE_WARN, "nostore_derive_key: failed holding minor");
64294219Smcpowers return (ENXIO);
64304219Smcpowers }
64314219Smcpowers
64324219Smcpowers if (copyin(arg, STRUCT_BUF(derive_key), STRUCT_SIZE(derive_key)) != 0) {
64334219Smcpowers crypto_release_minor(cm);
64344219Smcpowers return (EFAULT);
64354219Smcpowers }
64364219Smcpowers
64374219Smcpowers bzero(&base_key, sizeof (base_key));
64384219Smcpowers
64394219Smcpowers session_id = STRUCT_FGET(derive_key, ndk_session);
64404219Smcpowers
64414219Smcpowers if (!get_session_ptr(session_id, cm, &sp, &error, &rv)) {
64424219Smcpowers goto release_minor;
64434219Smcpowers }
64444219Smcpowers
64454219Smcpowers bcopy(STRUCT_FADDR(derive_key, ndk_mechanism), &mech.cm_type,
64464219Smcpowers sizeof (crypto_mech_type_t));
64474219Smcpowers
644810444SVladimir.Kotal@Sun.COM /* We need the key length for provider selection so copy it in now. */
644910444SVladimir.Kotal@Sun.COM if (!copyin_key(mode, sp, STRUCT_FADDR(derive_key, ndk_base_key),
645010444SVladimir.Kotal@Sun.COM &base_key, &key_rctl_bytes, &key_rctl_chk, &rv, &error)) {
645110444SVladimir.Kotal@Sun.COM goto release_minor;
645210444SVladimir.Kotal@Sun.COM }
645310444SVladimir.Kotal@Sun.COM
645410444SVladimir.Kotal@Sun.COM if ((rv = kcf_get_hardware_provider(mech.cm_type, &base_key,
6455*12304SValerie.Fenwick@Oracle.COM CRYPTO_MECH_INVALID, NULL, sp->sd_provider,
645610444SVladimir.Kotal@Sun.COM &real_provider, CRYPTO_FG_DERIVE)) != CRYPTO_SUCCESS) {
64574219Smcpowers goto release_minor;
64584219Smcpowers }
64594219Smcpowers
64604219Smcpowers rv = crypto_provider_copyin_mech_param(real_provider,
64614219Smcpowers STRUCT_FADDR(derive_key, ndk_mechanism), &mech, mode, &error);
64624219Smcpowers
64634219Smcpowers if (rv == CRYPTO_NOT_SUPPORTED) {
64644219Smcpowers allocated_by_crypto_module = B_TRUE;
64656424Skrishna if (!copyin_mech(mode, sp,
64666424Skrishna STRUCT_FADDR(derive_key, ndk_mechanism),
64676424Skrishna &mech, &mech_rctl_bytes, &mech_rctl_chk, &rv, &error)) {
64684219Smcpowers goto release_minor;
64694219Smcpowers }
64704219Smcpowers } else {
64714219Smcpowers if (rv != CRYPTO_SUCCESS)
64724219Smcpowers goto release_minor;
64734219Smcpowers }
64744219Smcpowers
64754219Smcpowers in_count = STRUCT_FGET(derive_key, ndk_in_count);
64764219Smcpowers out_count = STRUCT_FGET(derive_key, ndk_out_count);
64774219Smcpowers
64784219Smcpowers in_attributes = STRUCT_FGETP(derive_key, ndk_in_attributes);
64796424Skrishna if (!copyin_attributes(mode, sp, in_count, in_attributes, &k_in_attrs,
64804219Smcpowers &k_in_attrs_size, NULL, &rv, &error, &in_attributes_rctl_bytes,
64816424Skrishna &in_attributes_rctl_chk, B_TRUE)) {
64824219Smcpowers goto release_minor;
64834219Smcpowers }
64844219Smcpowers
64854219Smcpowers out_attributes = STRUCT_FGETP(derive_key, ndk_out_attributes);
64866424Skrishna if (!copyin_attributes(mode, sp, out_count, out_attributes,
64876424Skrishna &k_out_attrs, &k_out_attrs_size, &u_attrs, &rv, &error,
64886424Skrishna &out_attributes_rctl_bytes,
64896424Skrishna &out_attributes_rctl_chk, B_FALSE)) {
64904219Smcpowers goto release_minor;
64914219Smcpowers }
64924219Smcpowers
64934219Smcpowers KCF_WRAP_NOSTORE_KEY_OPS_PARAMS(¶ms, KCF_OP_KEY_DERIVE,
64944219Smcpowers sp->sd_provider_session->ps_session, &mech, k_in_attrs, in_count,
64954219Smcpowers NULL, 0, &base_key, k_out_attrs, out_count, NULL, 0);
64964219Smcpowers
64974219Smcpowers rv = kcf_submit_request(real_provider, NULL, NULL, ¶ms, B_FALSE);
64984219Smcpowers
64994219Smcpowers if (rv == CRYPTO_SUCCESS) {
65004219Smcpowers rv = crypto_provider_copyout_mech_param(real_provider,
65014219Smcpowers &mech, STRUCT_FADDR(derive_key, ndk_mechanism),
65024219Smcpowers mode, &error);
65034219Smcpowers
65044219Smcpowers if (rv == CRYPTO_NOT_SUPPORTED) {
65054219Smcpowers rv = CRYPTO_SUCCESS;
65064219Smcpowers }
65074219Smcpowers /* copyout the derived secret */
65084219Smcpowers if (copyout_attributes(mode, out_attributes, out_count,
65094219Smcpowers k_out_attrs, u_attrs) != 0)
65104219Smcpowers error = EFAULT;
65114219Smcpowers }
65124219Smcpowers
65134219Smcpowers release_minor:
65146424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, mech_rctl_bytes, mech_rctl_chk);
65156424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, key_rctl_bytes, key_rctl_chk);
65166424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, in_attributes_rctl_bytes,
65176424Skrishna in_attributes_rctl_chk);
65186424Skrishna CRYPTO_DECREMENT_RCTL_SESSION(sp, out_attributes_rctl_bytes,
65196424Skrishna out_attributes_rctl_chk);
65204219Smcpowers
65214219Smcpowers if (k_in_attrs != NULL)
65224219Smcpowers kmem_free(k_in_attrs, k_in_attrs_size);
65234219Smcpowers if (k_out_attrs != NULL) {
65244219Smcpowers bzero(k_out_attrs, k_out_attrs_size);
65254219Smcpowers kmem_free(k_out_attrs, k_out_attrs_size);
65264219Smcpowers }
65274219Smcpowers
65284219Smcpowers if (u_attrs != NULL)
65294219Smcpowers kmem_free(u_attrs, out_count * STRUCT_SIZE(oa));
65304219Smcpowers
65314219Smcpowers free_crypto_key(&base_key);
65324219Smcpowers
65334219Smcpowers if (error != 0)
65344219Smcpowers goto out;
65354219Smcpowers
65364219Smcpowers STRUCT_FSET(derive_key, ndk_return_value, rv);
65374219Smcpowers if (copyout(STRUCT_BUF(derive_key), arg,
65384219Smcpowers STRUCT_SIZE(derive_key)) != 0) {
65394219Smcpowers error = EFAULT;
65404219Smcpowers }
65414219Smcpowers out:
65426424Skrishna CRYPTO_SESSION_RELE(sp);
65434219Smcpowers crypto_release_minor(cm);
65444219Smcpowers
65454219Smcpowers if (real_provider != NULL) {
65464219Smcpowers crypto_free_mech(real_provider,
65474219Smcpowers allocated_by_crypto_module, &mech);
65484219Smcpowers KCF_PROV_REFRELE(real_provider);
65494219Smcpowers }
65504219Smcpowers return (error);
65514219Smcpowers }
65524219Smcpowers
65534219Smcpowers /* ARGSUSED */
65544219Smcpowers static int
crypto_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * c,int * rval)65550Sstevel@tonic-gate crypto_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *c,
65560Sstevel@tonic-gate int *rval)
65570Sstevel@tonic-gate {
65580Sstevel@tonic-gate #define ARG ((caddr_t)arg)
65590Sstevel@tonic-gate
65600Sstevel@tonic-gate switch (cmd) {
65610Sstevel@tonic-gate case CRYPTO_GET_FUNCTION_LIST:
65620Sstevel@tonic-gate return (get_function_list(dev, ARG, mode, rval));
65630Sstevel@tonic-gate
65640Sstevel@tonic-gate case CRYPTO_GET_MECHANISM_NUMBER:
65650Sstevel@tonic-gate return (get_mechanism_number(dev, ARG, mode, rval));
65660Sstevel@tonic-gate
65678313SDina.Nimeh@Sun.Com case CRYPTO_GET_MECHANISM_LIST:
65688313SDina.Nimeh@Sun.Com return (get_mechanism_list(dev, ARG, mode, rval));
65698313SDina.Nimeh@Sun.Com
65708313SDina.Nimeh@Sun.Com case CRYPTO_GET_ALL_MECHANISM_INFO:
65718313SDina.Nimeh@Sun.Com return (get_all_mechanism_info(dev, ARG, mode, rval));
65728313SDina.Nimeh@Sun.Com
65730Sstevel@tonic-gate case CRYPTO_GET_PROVIDER_LIST:
65740Sstevel@tonic-gate return (get_provider_list(dev, ARG, mode, rval));
65750Sstevel@tonic-gate
65760Sstevel@tonic-gate case CRYPTO_GET_PROVIDER_INFO:
65770Sstevel@tonic-gate return (get_provider_info(dev, ARG, mode, rval));
65780Sstevel@tonic-gate
65790Sstevel@tonic-gate case CRYPTO_GET_PROVIDER_MECHANISMS:
65800Sstevel@tonic-gate return (get_provider_mechanisms(dev, ARG, mode, rval));
65810Sstevel@tonic-gate
65820Sstevel@tonic-gate case CRYPTO_GET_PROVIDER_MECHANISM_INFO:
65830Sstevel@tonic-gate return (get_provider_mechanism_info(dev, ARG, mode, rval));
65840Sstevel@tonic-gate
65850Sstevel@tonic-gate case CRYPTO_OPEN_SESSION:
65860Sstevel@tonic-gate return (open_session(dev, ARG, mode, rval));
65870Sstevel@tonic-gate
65880Sstevel@tonic-gate case CRYPTO_CLOSE_SESSION:
65890Sstevel@tonic-gate return (close_session(dev, ARG, mode, rval));
65900Sstevel@tonic-gate
65910Sstevel@tonic-gate case CRYPTO_ENCRYPT_INIT:
65920Sstevel@tonic-gate return (encrypt_init(dev, ARG, mode, rval));
65930Sstevel@tonic-gate
65940Sstevel@tonic-gate case CRYPTO_DECRYPT_INIT:
65950Sstevel@tonic-gate return (decrypt_init(dev, ARG, mode, rval));
65960Sstevel@tonic-gate
65970Sstevel@tonic-gate case CRYPTO_ENCRYPT:
65980Sstevel@tonic-gate return (encrypt(dev, ARG, mode, rval));
65990Sstevel@tonic-gate
66000Sstevel@tonic-gate case CRYPTO_DECRYPT:
66010Sstevel@tonic-gate return (decrypt(dev, ARG, mode, rval));
66020Sstevel@tonic-gate
66030Sstevel@tonic-gate case CRYPTO_ENCRYPT_UPDATE:
66040Sstevel@tonic-gate return (encrypt_update(dev, ARG, mode, rval));
66050Sstevel@tonic-gate
66060Sstevel@tonic-gate case CRYPTO_DECRYPT_UPDATE:
66070Sstevel@tonic-gate return (decrypt_update(dev, ARG, mode, rval));
66080Sstevel@tonic-gate
66090Sstevel@tonic-gate case CRYPTO_ENCRYPT_FINAL:
66100Sstevel@tonic-gate return (encrypt_final(dev, ARG, mode, rval));
66110Sstevel@tonic-gate
66120Sstevel@tonic-gate case CRYPTO_DECRYPT_FINAL:
66130Sstevel@tonic-gate return (decrypt_final(dev, ARG, mode, rval));
66140Sstevel@tonic-gate
66150Sstevel@tonic-gate case CRYPTO_DIGEST_INIT:
66160Sstevel@tonic-gate return (digest_init(dev, ARG, mode, rval));
66170Sstevel@tonic-gate
66180Sstevel@tonic-gate case CRYPTO_DIGEST:
66190Sstevel@tonic-gate return (digest(dev, ARG, mode, rval));
66200Sstevel@tonic-gate
66210Sstevel@tonic-gate case CRYPTO_DIGEST_UPDATE:
66220Sstevel@tonic-gate return (digest_update(dev, ARG, mode, rval));
66230Sstevel@tonic-gate
66240Sstevel@tonic-gate case CRYPTO_DIGEST_KEY:
66250Sstevel@tonic-gate return (digest_key(dev, ARG, mode, rval));
66260Sstevel@tonic-gate
66270Sstevel@tonic-gate case CRYPTO_DIGEST_FINAL:
66280Sstevel@tonic-gate return (digest_final(dev, ARG, mode, rval));
66290Sstevel@tonic-gate
66300Sstevel@tonic-gate case CRYPTO_SIGN_INIT:
66310Sstevel@tonic-gate return (sign_init(dev, ARG, mode, rval));
66320Sstevel@tonic-gate
66330Sstevel@tonic-gate case CRYPTO_SIGN:
66340Sstevel@tonic-gate return (sign(dev, ARG, mode, rval));
66350Sstevel@tonic-gate
66360Sstevel@tonic-gate case CRYPTO_SIGN_UPDATE:
66370Sstevel@tonic-gate return (sign_update(dev, ARG, mode, rval));
66380Sstevel@tonic-gate
66390Sstevel@tonic-gate case CRYPTO_SIGN_FINAL:
66400Sstevel@tonic-gate return (sign_final(dev, ARG, mode, rval));
66410Sstevel@tonic-gate
66420Sstevel@tonic-gate case CRYPTO_SIGN_RECOVER_INIT:
66430Sstevel@tonic-gate return (sign_recover_init(dev, ARG, mode, rval));
66440Sstevel@tonic-gate
66450Sstevel@tonic-gate case CRYPTO_SIGN_RECOVER:
66460Sstevel@tonic-gate return (sign_recover(dev, ARG, mode, rval));
66470Sstevel@tonic-gate
66480Sstevel@tonic-gate case CRYPTO_VERIFY_INIT:
66490Sstevel@tonic-gate return (verify_init(dev, ARG, mode, rval));
66500Sstevel@tonic-gate
66510Sstevel@tonic-gate case CRYPTO_VERIFY:
66520Sstevel@tonic-gate return (verify(dev, ARG, mode, rval));
66530Sstevel@tonic-gate
66540Sstevel@tonic-gate case CRYPTO_VERIFY_UPDATE:
66550Sstevel@tonic-gate return (verify_update(dev, ARG, mode, rval));
66560Sstevel@tonic-gate
66570Sstevel@tonic-gate case CRYPTO_VERIFY_FINAL:
66580Sstevel@tonic-gate return (verify_final(dev, ARG, mode, rval));
66590Sstevel@tonic-gate
66600Sstevel@tonic-gate case CRYPTO_VERIFY_RECOVER_INIT:
66610Sstevel@tonic-gate return (verify_recover_init(dev, ARG, mode, rval));
66620Sstevel@tonic-gate
66630Sstevel@tonic-gate case CRYPTO_VERIFY_RECOVER:
66640Sstevel@tonic-gate return (verify_recover(dev, ARG, mode, rval));
66650Sstevel@tonic-gate
66660Sstevel@tonic-gate case CRYPTO_SET_PIN:
66670Sstevel@tonic-gate return (set_pin(dev, ARG, mode, rval));
66680Sstevel@tonic-gate
66690Sstevel@tonic-gate case CRYPTO_LOGIN:
66700Sstevel@tonic-gate return (login(dev, ARG, mode, rval));
66710Sstevel@tonic-gate
66720Sstevel@tonic-gate case CRYPTO_LOGOUT:
66730Sstevel@tonic-gate return (logout(dev, ARG, mode, rval));
66740Sstevel@tonic-gate
66750Sstevel@tonic-gate case CRYPTO_SEED_RANDOM:
66760Sstevel@tonic-gate return (seed_random(dev, ARG, mode, rval));
66770Sstevel@tonic-gate
66780Sstevel@tonic-gate case CRYPTO_GENERATE_RANDOM:
66790Sstevel@tonic-gate return (generate_random(dev, ARG, mode, rval));
66800Sstevel@tonic-gate
66810Sstevel@tonic-gate case CRYPTO_OBJECT_CREATE:
66820Sstevel@tonic-gate return (object_create(dev, ARG, mode, rval));
66830Sstevel@tonic-gate
66840Sstevel@tonic-gate case CRYPTO_OBJECT_COPY:
66850Sstevel@tonic-gate return (object_copy(dev, ARG, mode, rval));
66860Sstevel@tonic-gate
66870Sstevel@tonic-gate case CRYPTO_OBJECT_DESTROY:
66880Sstevel@tonic-gate return (object_destroy(dev, ARG, mode, rval));
66890Sstevel@tonic-gate
66900Sstevel@tonic-gate case CRYPTO_OBJECT_GET_ATTRIBUTE_VALUE:
66910Sstevel@tonic-gate return (object_get_attribute_value(dev, ARG, mode, rval));
66920Sstevel@tonic-gate
66930Sstevel@tonic-gate case CRYPTO_OBJECT_GET_SIZE:
66940Sstevel@tonic-gate return (object_get_size(dev, ARG, mode, rval));
66950Sstevel@tonic-gate
66960Sstevel@tonic-gate case CRYPTO_OBJECT_SET_ATTRIBUTE_VALUE:
66970Sstevel@tonic-gate return (object_set_attribute_value(dev, ARG, mode, rval));
66980Sstevel@tonic-gate
66990Sstevel@tonic-gate case CRYPTO_OBJECT_FIND_INIT:
67000Sstevel@tonic-gate return (object_find_init(dev, ARG, mode, rval));
67010Sstevel@tonic-gate
67020Sstevel@tonic-gate case CRYPTO_OBJECT_FIND_UPDATE:
67030Sstevel@tonic-gate return (object_find_update(dev, ARG, mode, rval));
67040Sstevel@tonic-gate
67050Sstevel@tonic-gate case CRYPTO_OBJECT_FIND_FINAL:
67060Sstevel@tonic-gate return (object_find_final(dev, ARG, mode, rval));
67070Sstevel@tonic-gate
67080Sstevel@tonic-gate case CRYPTO_GENERATE_KEY:
67090Sstevel@tonic-gate return (object_generate_key(dev, ARG, mode, rval));
67100Sstevel@tonic-gate
67110Sstevel@tonic-gate case CRYPTO_GENERATE_KEY_PAIR:
67120Sstevel@tonic-gate return (object_generate_key_pair(dev, ARG, mode, rval));
67130Sstevel@tonic-gate
67140Sstevel@tonic-gate case CRYPTO_WRAP_KEY:
67150Sstevel@tonic-gate return (object_wrap_key(dev, ARG, mode, rval));
67160Sstevel@tonic-gate
67170Sstevel@tonic-gate case CRYPTO_UNWRAP_KEY:
67180Sstevel@tonic-gate return (object_unwrap_key(dev, ARG, mode, rval));
67190Sstevel@tonic-gate
67200Sstevel@tonic-gate case CRYPTO_DERIVE_KEY:
67210Sstevel@tonic-gate return (object_derive_key(dev, ARG, mode, rval));
67224219Smcpowers
67234219Smcpowers case CRYPTO_NOSTORE_GENERATE_KEY:
67244219Smcpowers return (nostore_generate_key(dev, ARG, mode, rval));
67254219Smcpowers
67264219Smcpowers case CRYPTO_NOSTORE_GENERATE_KEY_PAIR:
67274219Smcpowers return (nostore_generate_key_pair(dev, ARG, mode, rval));
67284219Smcpowers
67294219Smcpowers case CRYPTO_NOSTORE_DERIVE_KEY:
67304219Smcpowers return (nostore_derive_key(dev, ARG, mode, rval));
67310Sstevel@tonic-gate }
67320Sstevel@tonic-gate return (EINVAL);
67330Sstevel@tonic-gate }
67340Sstevel@tonic-gate
67350Sstevel@tonic-gate /*
67360Sstevel@tonic-gate * Check for the project.max-crypto-memory resource control.
67370Sstevel@tonic-gate */
67380Sstevel@tonic-gate static int
crypto_buffer_check(size_t need)67393916Skrishna crypto_buffer_check(size_t need)
67400Sstevel@tonic-gate {
67413620Skrishna kproject_t *kpj;
67420Sstevel@tonic-gate
67430Sstevel@tonic-gate if (need == 0)
67440Sstevel@tonic-gate return (CRYPTO_SUCCESS);
67450Sstevel@tonic-gate
67460Sstevel@tonic-gate mutex_enter(&curproc->p_lock);
67473620Skrishna kpj = curproc->p_task->tk_proj;
67483916Skrishna mutex_enter(&(kpj->kpj_data.kpd_crypto_lock));
67493620Skrishna
67503620Skrishna if (kpj->kpj_data.kpd_crypto_mem + need >
67513620Skrishna kpj->kpj_data.kpd_crypto_mem_ctl) {
67523620Skrishna if (rctl_test(rc_project_crypto_mem,
67533620Skrishna kpj->kpj_rctls, curproc, need, 0) & RCT_DENY) {
67543916Skrishna mutex_exit(&(kpj->kpj_data.kpd_crypto_lock));
67553620Skrishna mutex_exit(&curproc->p_lock);
67563620Skrishna return (CRYPTO_HOST_MEMORY);
67573620Skrishna }
67583620Skrishna }
67593620Skrishna
67603620Skrishna kpj->kpj_data.kpd_crypto_mem += need;
67613916Skrishna mutex_exit(&(kpj->kpj_data.kpd_crypto_lock));
67623916Skrishna
67633916Skrishna curproc->p_crypto_mem += need;
67640Sstevel@tonic-gate mutex_exit(&curproc->p_lock);
67653620Skrishna
67660Sstevel@tonic-gate return (CRYPTO_SUCCESS);
67670Sstevel@tonic-gate }
6768