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 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 23*904Smcpowers * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <sys/errno.h> 300Sstevel@tonic-gate #include <sys/types.h> 310Sstevel@tonic-gate #include <sys/kmem.h> 32*904Smcpowers #include <sys/sysmacros.h> 330Sstevel@tonic-gate #include <sys/crypto/common.h> 340Sstevel@tonic-gate #include <sys/crypto/impl.h> 350Sstevel@tonic-gate #include <sys/crypto/api.h> 360Sstevel@tonic-gate #include <sys/crypto/spi.h> 370Sstevel@tonic-gate #include <sys/crypto/sched_impl.h> 380Sstevel@tonic-gate 39*904Smcpowers #define CRYPTO_OPS_OFFSET(f) offsetof(crypto_ops_t, co_##f) 40*904Smcpowers #define CRYPTO_MAC_OFFSET(f) offsetof(crypto_mac_ops_t, f) 41*904Smcpowers 420Sstevel@tonic-gate /* 430Sstevel@tonic-gate * Message authentication codes routines. 440Sstevel@tonic-gate */ 450Sstevel@tonic-gate 460Sstevel@tonic-gate /* 470Sstevel@tonic-gate * The following are the possible returned values common to all the routines 480Sstevel@tonic-gate * below. The applicability of some of these return values depends on the 490Sstevel@tonic-gate * presence of the arguments. 500Sstevel@tonic-gate * 510Sstevel@tonic-gate * CRYPTO_SUCCESS: The operation completed successfully. 520Sstevel@tonic-gate * CRYPTO_QUEUED: A request was submitted successfully. The callback 530Sstevel@tonic-gate * routine will be called when the operation is done. 540Sstevel@tonic-gate * CRYPTO_INVALID_MECH_NUMBER, CRYPTO_INVALID_MECH_PARAM, or 550Sstevel@tonic-gate * CRYPTO_INVALID_MECH for problems with the 'mech'. 560Sstevel@tonic-gate * CRYPTO_INVALID_DATA for bogus 'data' 570Sstevel@tonic-gate * CRYPTO_HOST_MEMORY for failure to allocate memory to handle this work. 580Sstevel@tonic-gate * CRYPTO_INVALID_CONTEXT: Not a valid context. 590Sstevel@tonic-gate * CRYPTO_BUSY: Cannot process the request now. Schedule a 600Sstevel@tonic-gate * crypto_bufcall(), or try later. 610Sstevel@tonic-gate * CRYPTO_NOT_SUPPORTED and CRYPTO_MECH_NOT_SUPPORTED: No provider is 620Sstevel@tonic-gate * capable of a function or a mechanism. 630Sstevel@tonic-gate * CRYPTO_INVALID_KEY: bogus 'key' argument. 640Sstevel@tonic-gate * CRYPTO_INVALID_MAC: bogus 'mac' argument. 650Sstevel@tonic-gate */ 660Sstevel@tonic-gate 670Sstevel@tonic-gate /* 680Sstevel@tonic-gate * crypto_mac_prov() 690Sstevel@tonic-gate * 700Sstevel@tonic-gate * Arguments: 710Sstevel@tonic-gate * mech: crypto_mechanism_t pointer. 720Sstevel@tonic-gate * mech_type is a valid value previously returned by 730Sstevel@tonic-gate * crypto_mech2id(); 740Sstevel@tonic-gate * When the mech's parameter is not NULL, its definition depends 750Sstevel@tonic-gate * on the standard definition of the mechanism. 760Sstevel@tonic-gate * key: pointer to a crypto_key_t structure. 770Sstevel@tonic-gate * data: The message to compute the MAC for. 780Sstevel@tonic-gate * mac: Storage for the MAC. The length needed depends on the mechanism. 790Sstevel@tonic-gate * tmpl: a crypto_ctx_template_t, opaque template of a context of a 800Sstevel@tonic-gate * MAC with the 'mech' using 'key'. 'tmpl' is created by 810Sstevel@tonic-gate * a previous call to crypto_create_ctx_template(). 820Sstevel@tonic-gate * cr: crypto_call_req_t calling conditions and call back info. 830Sstevel@tonic-gate * 840Sstevel@tonic-gate * Description: 850Sstevel@tonic-gate * Asynchronously submits a request for, or synchronously performs a 860Sstevel@tonic-gate * single-part message authentication of 'data' with the mechanism 870Sstevel@tonic-gate * 'mech', using * the key 'key', on the specified provider with 880Sstevel@tonic-gate * the specified session id. 890Sstevel@tonic-gate * When complete and successful, 'mac' will contain the message 900Sstevel@tonic-gate * authentication code. 910Sstevel@tonic-gate * 920Sstevel@tonic-gate * Context: 930Sstevel@tonic-gate * Process or interrupt, according to the semantics dictated by the 'crq'. 940Sstevel@tonic-gate * 950Sstevel@tonic-gate * Returns: 960Sstevel@tonic-gate * See comment in the beginning of the file. 970Sstevel@tonic-gate */ 980Sstevel@tonic-gate int 99*904Smcpowers crypto_mac_prov(crypto_provider_t provider, crypto_session_id_t sid, 100*904Smcpowers crypto_mechanism_t *mech, crypto_data_t *data, crypto_key_t *key, 101*904Smcpowers crypto_ctx_template_t tmpl, crypto_data_t *mac, crypto_call_req_t *crq) 1020Sstevel@tonic-gate { 1030Sstevel@tonic-gate kcf_req_params_t params; 104*904Smcpowers kcf_provider_desc_t *pd = provider; 105*904Smcpowers kcf_provider_desc_t *real_provider = pd; 106*904Smcpowers int rv; 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate ASSERT(KCF_PROV_REFHELD(pd)); 109*904Smcpowers 110*904Smcpowers if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) { 111*904Smcpowers rv = kcf_get_hardware_provider(mech->cm_type, 112*904Smcpowers CRYPTO_MECH_INVALID, CRYPTO_OPS_OFFSET(mac_ops), 113*904Smcpowers CRYPTO_MAC_OFFSET(mac_atomic), 114*904Smcpowers CHECK_RESTRICT(crq), pd, &real_provider); 115*904Smcpowers 116*904Smcpowers if (rv != CRYPTO_SUCCESS) 117*904Smcpowers return (rv); 118*904Smcpowers } 119*904Smcpowers 1200Sstevel@tonic-gate KCF_WRAP_MAC_OPS_PARAMS(¶ms, KCF_OP_ATOMIC, sid, mech, key, 1210Sstevel@tonic-gate data, mac, tmpl); 122*904Smcpowers rv = kcf_submit_request(real_provider, NULL, crq, ¶ms, B_FALSE); 123*904Smcpowers if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) 124*904Smcpowers KCF_PROV_REFRELE(real_provider); 1250Sstevel@tonic-gate 126*904Smcpowers return (rv); 1270Sstevel@tonic-gate } 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate /* 1300Sstevel@tonic-gate * Same as crypto_mac_prov(), but relies on the KCF scheduler to choose 1310Sstevel@tonic-gate * a provider. See crypto_mac() comments for more information. 1320Sstevel@tonic-gate */ 1330Sstevel@tonic-gate int 1340Sstevel@tonic-gate crypto_mac(crypto_mechanism_t *mech, crypto_data_t *data, 1350Sstevel@tonic-gate crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_data_t *mac, 1360Sstevel@tonic-gate crypto_call_req_t *crq) 1370Sstevel@tonic-gate { 1380Sstevel@tonic-gate int error; 1390Sstevel@tonic-gate kcf_mech_entry_t *me; 1400Sstevel@tonic-gate kcf_req_params_t params; 1410Sstevel@tonic-gate kcf_provider_desc_t *pd; 1420Sstevel@tonic-gate kcf_ctx_template_t *ctx_tmpl; 1430Sstevel@tonic-gate crypto_spi_ctx_template_t spi_ctx_tmpl = NULL; 1440Sstevel@tonic-gate kcf_prov_tried_t *list = NULL; 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate retry: 1470Sstevel@tonic-gate /* The pd is returned held */ 1480Sstevel@tonic-gate if ((pd = kcf_get_mech_provider(mech->cm_type, &me, &error, 1490Sstevel@tonic-gate list, CRYPTO_FG_MAC_ATOMIC, CHECK_RESTRICT(crq), 1500Sstevel@tonic-gate data->cd_length)) == NULL) { 1510Sstevel@tonic-gate if (list != NULL) 1520Sstevel@tonic-gate kcf_free_triedlist(list); 1530Sstevel@tonic-gate return (error); 1540Sstevel@tonic-gate } 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate /* 1570Sstevel@tonic-gate * For SW providers, check the validity of the context template 1580Sstevel@tonic-gate * It is very rare that the generation number mis-matches, so 1590Sstevel@tonic-gate * is acceptable to fail here, and let the consumer recover by 1600Sstevel@tonic-gate * freeing this tmpl and create a new one for the key and new SW 1610Sstevel@tonic-gate * provider 1620Sstevel@tonic-gate */ 1630Sstevel@tonic-gate if ((pd->pd_prov_type == CRYPTO_SW_PROVIDER) && 1640Sstevel@tonic-gate ((ctx_tmpl = (kcf_ctx_template_t *)tmpl) != NULL)) { 1650Sstevel@tonic-gate if (ctx_tmpl->ct_generation != me->me_gen_swprov) { 1660Sstevel@tonic-gate if (list != NULL) 1670Sstevel@tonic-gate kcf_free_triedlist(list); 1680Sstevel@tonic-gate KCF_PROV_REFRELE(pd); 1690Sstevel@tonic-gate return (CRYPTO_OLD_CTX_TEMPLATE); 1700Sstevel@tonic-gate } else { 1710Sstevel@tonic-gate spi_ctx_tmpl = ctx_tmpl->ct_prov_tmpl; 1720Sstevel@tonic-gate } 1730Sstevel@tonic-gate } 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate /* The fast path for SW providers. */ 1760Sstevel@tonic-gate if (CHECK_FASTPATH(crq, pd)) { 1770Sstevel@tonic-gate crypto_mechanism_t lmech; 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate lmech = *mech; 1800Sstevel@tonic-gate KCF_SET_PROVIDER_MECHNUM(mech->cm_type, pd, &lmech); 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate error = KCF_PROV_MAC_ATOMIC(pd, pd->pd_sid, &lmech, key, data, 1830Sstevel@tonic-gate mac, spi_ctx_tmpl, KCF_SWFP_RHNDL(crq)); 1840Sstevel@tonic-gate KCF_PROV_INCRSTATS(pd, error); 1850Sstevel@tonic-gate } else { 1860Sstevel@tonic-gate KCF_WRAP_MAC_OPS_PARAMS(¶ms, KCF_OP_ATOMIC, pd->pd_sid, 1870Sstevel@tonic-gate mech, key, data, mac, spi_ctx_tmpl); 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate error = kcf_submit_request(pd, NULL, crq, ¶ms, 1900Sstevel@tonic-gate KCF_ISDUALREQ(crq)); 1910Sstevel@tonic-gate } 1920Sstevel@tonic-gate 1930Sstevel@tonic-gate if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED && 1940Sstevel@tonic-gate IS_RECOVERABLE(error)) { 1950Sstevel@tonic-gate /* Add pd to the linked list of providers tried. */ 1960Sstevel@tonic-gate if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL) 1970Sstevel@tonic-gate goto retry; 1980Sstevel@tonic-gate } 1990Sstevel@tonic-gate 2000Sstevel@tonic-gate if (list != NULL) 2010Sstevel@tonic-gate kcf_free_triedlist(list); 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate KCF_PROV_REFRELE(pd); 2040Sstevel@tonic-gate return (error); 2050Sstevel@tonic-gate } 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate /* 2080Sstevel@tonic-gate * Single part operation to compute the MAC corresponding to the specified 2090Sstevel@tonic-gate * 'data' and to verify that it matches the MAC specified by 'mac'. 2100Sstevel@tonic-gate * The other arguments are the same as the function crypto_mac_prov(). 2110Sstevel@tonic-gate */ 2120Sstevel@tonic-gate int 213*904Smcpowers crypto_mac_verify_prov(crypto_provider_t provider, crypto_session_id_t sid, 214*904Smcpowers crypto_mechanism_t *mech, crypto_data_t *data, crypto_key_t *key, 215*904Smcpowers crypto_ctx_template_t tmpl, crypto_data_t *mac, crypto_call_req_t *crq) 2160Sstevel@tonic-gate { 2170Sstevel@tonic-gate kcf_req_params_t params; 218*904Smcpowers kcf_provider_desc_t *pd = provider; 219*904Smcpowers kcf_provider_desc_t *real_provider = pd; 220*904Smcpowers int rv; 2210Sstevel@tonic-gate 2220Sstevel@tonic-gate ASSERT(KCF_PROV_REFHELD(pd)); 223*904Smcpowers 224*904Smcpowers if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) { 225*904Smcpowers rv = kcf_get_hardware_provider(mech->cm_type, 226*904Smcpowers CRYPTO_MECH_INVALID, CRYPTO_OPS_OFFSET(mac_ops), 227*904Smcpowers CRYPTO_MAC_OFFSET(mac_verify_atomic), 228*904Smcpowers CHECK_RESTRICT(crq), pd, &real_provider); 229*904Smcpowers 230*904Smcpowers if (rv != CRYPTO_SUCCESS) 231*904Smcpowers return (rv); 232*904Smcpowers } 233*904Smcpowers 2340Sstevel@tonic-gate KCF_WRAP_MAC_OPS_PARAMS(¶ms, KCF_OP_MAC_VERIFY_ATOMIC, sid, mech, 2350Sstevel@tonic-gate key, data, mac, tmpl); 236*904Smcpowers rv = kcf_submit_request(real_provider, NULL, crq, ¶ms, B_FALSE); 237*904Smcpowers if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) 238*904Smcpowers KCF_PROV_REFRELE(real_provider); 2390Sstevel@tonic-gate 240*904Smcpowers return (rv); 2410Sstevel@tonic-gate } 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate /* 2440Sstevel@tonic-gate * Same as crypto_mac_verify_prov(), but relies on the KCF scheduler to choose 2450Sstevel@tonic-gate * a provider. See crypto_mac_verify_prov() comments for more information. 2460Sstevel@tonic-gate */ 2470Sstevel@tonic-gate int 2480Sstevel@tonic-gate crypto_mac_verify(crypto_mechanism_t *mech, crypto_data_t *data, 2490Sstevel@tonic-gate crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_data_t *mac, 2500Sstevel@tonic-gate crypto_call_req_t *crq) 2510Sstevel@tonic-gate { 2520Sstevel@tonic-gate int error; 2530Sstevel@tonic-gate kcf_mech_entry_t *me; 2540Sstevel@tonic-gate kcf_req_params_t params; 2550Sstevel@tonic-gate kcf_provider_desc_t *pd; 2560Sstevel@tonic-gate kcf_ctx_template_t *ctx_tmpl; 2570Sstevel@tonic-gate crypto_spi_ctx_template_t spi_ctx_tmpl = NULL; 2580Sstevel@tonic-gate kcf_prov_tried_t *list = NULL; 2590Sstevel@tonic-gate 2600Sstevel@tonic-gate retry: 2610Sstevel@tonic-gate /* The pd is returned held */ 2620Sstevel@tonic-gate if ((pd = kcf_get_mech_provider(mech->cm_type, &me, &error, 2630Sstevel@tonic-gate list, CRYPTO_FG_MAC_ATOMIC, CHECK_RESTRICT(crq), 2640Sstevel@tonic-gate data->cd_length)) == NULL) { 2650Sstevel@tonic-gate if (list != NULL) 2660Sstevel@tonic-gate kcf_free_triedlist(list); 2670Sstevel@tonic-gate return (error); 2680Sstevel@tonic-gate } 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate /* 2710Sstevel@tonic-gate * For SW providers, check the validity of the context template 2720Sstevel@tonic-gate * It is very rare that the generation number mis-matches, so 2730Sstevel@tonic-gate * is acceptable to fail here, and let the consumer recover by 2740Sstevel@tonic-gate * freeing this tmpl and create a new one for the key and new SW 2750Sstevel@tonic-gate * provider 2760Sstevel@tonic-gate */ 2770Sstevel@tonic-gate if ((pd->pd_prov_type == CRYPTO_SW_PROVIDER) && 2780Sstevel@tonic-gate ((ctx_tmpl = (kcf_ctx_template_t *)tmpl) != NULL)) { 2790Sstevel@tonic-gate if (ctx_tmpl->ct_generation != me->me_gen_swprov) { 2800Sstevel@tonic-gate if (list != NULL) 2810Sstevel@tonic-gate kcf_free_triedlist(list); 2820Sstevel@tonic-gate KCF_PROV_REFRELE(pd); 2830Sstevel@tonic-gate return (CRYPTO_OLD_CTX_TEMPLATE); 2840Sstevel@tonic-gate } else { 2850Sstevel@tonic-gate spi_ctx_tmpl = ctx_tmpl->ct_prov_tmpl; 2860Sstevel@tonic-gate } 2870Sstevel@tonic-gate } 2880Sstevel@tonic-gate 2890Sstevel@tonic-gate /* The fast path for SW providers. */ 2900Sstevel@tonic-gate if (CHECK_FASTPATH(crq, pd)) { 2910Sstevel@tonic-gate crypto_mechanism_t lmech; 2920Sstevel@tonic-gate 2930Sstevel@tonic-gate lmech = *mech; 2940Sstevel@tonic-gate KCF_SET_PROVIDER_MECHNUM(mech->cm_type, pd, &lmech); 2950Sstevel@tonic-gate 2960Sstevel@tonic-gate error = KCF_PROV_MAC_VERIFY_ATOMIC(pd, pd->pd_sid, &lmech, key, 2970Sstevel@tonic-gate data, mac, spi_ctx_tmpl, KCF_SWFP_RHNDL(crq)); 2980Sstevel@tonic-gate KCF_PROV_INCRSTATS(pd, error); 2990Sstevel@tonic-gate } else { 3000Sstevel@tonic-gate KCF_WRAP_MAC_OPS_PARAMS(¶ms, KCF_OP_MAC_VERIFY_ATOMIC, 3010Sstevel@tonic-gate pd->pd_sid, mech, key, data, mac, spi_ctx_tmpl); 3020Sstevel@tonic-gate 3030Sstevel@tonic-gate error = kcf_submit_request(pd, NULL, crq, ¶ms, 3040Sstevel@tonic-gate KCF_ISDUALREQ(crq)); 3050Sstevel@tonic-gate } 3060Sstevel@tonic-gate 3070Sstevel@tonic-gate if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED && 3080Sstevel@tonic-gate IS_RECOVERABLE(error)) { 3090Sstevel@tonic-gate /* Add pd to the linked list of providers tried. */ 3100Sstevel@tonic-gate if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL) 3110Sstevel@tonic-gate goto retry; 3120Sstevel@tonic-gate } 3130Sstevel@tonic-gate 3140Sstevel@tonic-gate if (list != NULL) 3150Sstevel@tonic-gate kcf_free_triedlist(list); 3160Sstevel@tonic-gate 3170Sstevel@tonic-gate KCF_PROV_REFRELE(pd); 3180Sstevel@tonic-gate return (error); 3190Sstevel@tonic-gate } 3200Sstevel@tonic-gate 3210Sstevel@tonic-gate 3220Sstevel@tonic-gate /* 3230Sstevel@tonic-gate * crypto_mac_init_prov() 3240Sstevel@tonic-gate * 3250Sstevel@tonic-gate * Arguments: 3260Sstevel@tonic-gate * pd: pointer to the descriptor of the provider to use for this 3270Sstevel@tonic-gate * operation. 3280Sstevel@tonic-gate * sid: provider session id. 3290Sstevel@tonic-gate * mech: crypto_mechanism_t pointer. 3300Sstevel@tonic-gate * mech_type is a valid value previously returned by 3310Sstevel@tonic-gate * crypto_mech2id(); 3320Sstevel@tonic-gate * When the mech's parameter is not NULL, its definition depends 3330Sstevel@tonic-gate * on the standard definition of the mechanism. 3340Sstevel@tonic-gate * key: pointer to a crypto_key_t structure. 3350Sstevel@tonic-gate * tmpl: a crypto_ctx_template_t, opaque template of a context of a 3360Sstevel@tonic-gate * MAC with the 'mech' using 'key'. 'tmpl' is created by 3370Sstevel@tonic-gate * a previous call to crypto_create_ctx_template(). 3380Sstevel@tonic-gate * ctxp: Pointer to a crypto_context_t. 3390Sstevel@tonic-gate * cr: crypto_call_req_t calling conditions and call back info. 3400Sstevel@tonic-gate * 3410Sstevel@tonic-gate * Description: 3420Sstevel@tonic-gate * Asynchronously submits a request for, or synchronously performs the 3430Sstevel@tonic-gate * initialization of a MAC operation on the specified provider with 3440Sstevel@tonic-gate * the specified session. 3450Sstevel@tonic-gate * When possible and applicable, will internally use the pre-computed MAC 3460Sstevel@tonic-gate * context from the context template, tmpl. 3470Sstevel@tonic-gate * When complete and successful, 'ctxp' will contain a crypto_context_t 3480Sstevel@tonic-gate * valid for later calls to mac_update() and mac_final(). 3490Sstevel@tonic-gate * The caller should hold a reference on the specified provider 3500Sstevel@tonic-gate * descriptor before calling this function. 3510Sstevel@tonic-gate * 3520Sstevel@tonic-gate * Context: 3530Sstevel@tonic-gate * Process or interrupt, according to the semantics dictated by the 'cr'. 3540Sstevel@tonic-gate * 3550Sstevel@tonic-gate * Returns: 3560Sstevel@tonic-gate * See comment in the beginning of the file. 3570Sstevel@tonic-gate */ 3580Sstevel@tonic-gate int 359*904Smcpowers crypto_mac_init_prov(crypto_provider_t provider, crypto_session_id_t sid, 3600Sstevel@tonic-gate crypto_mechanism_t *mech, crypto_key_t *key, crypto_spi_ctx_template_t tmpl, 3610Sstevel@tonic-gate crypto_context_t *ctxp, crypto_call_req_t *crq) 3620Sstevel@tonic-gate { 363*904Smcpowers int rv; 3640Sstevel@tonic-gate crypto_ctx_t *ctx; 3650Sstevel@tonic-gate kcf_req_params_t params; 366*904Smcpowers kcf_provider_desc_t *pd = provider; 367*904Smcpowers kcf_provider_desc_t *real_provider = pd; 3680Sstevel@tonic-gate 3690Sstevel@tonic-gate ASSERT(KCF_PROV_REFHELD(pd)); 3700Sstevel@tonic-gate 371*904Smcpowers if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) { 372*904Smcpowers rv = kcf_get_hardware_provider(mech->cm_type, 373*904Smcpowers CRYPTO_MECH_INVALID, CRYPTO_OPS_OFFSET(mac_ops), 374*904Smcpowers CRYPTO_MAC_OFFSET(mac_init), 375*904Smcpowers CHECK_RESTRICT(crq), pd, &real_provider); 376*904Smcpowers 377*904Smcpowers if (rv != CRYPTO_SUCCESS) 378*904Smcpowers return (rv); 379*904Smcpowers } 380*904Smcpowers 381*904Smcpowers /* Allocate and initialize the canonical context */ 382*904Smcpowers if ((ctx = kcf_new_ctx(crq, real_provider, sid)) == NULL) { 383*904Smcpowers if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) 384*904Smcpowers KCF_PROV_REFRELE(real_provider); 3850Sstevel@tonic-gate return (CRYPTO_HOST_MEMORY); 386*904Smcpowers } 3870Sstevel@tonic-gate 3880Sstevel@tonic-gate /* The fast path for SW providers. */ 3890Sstevel@tonic-gate if (CHECK_FASTPATH(crq, pd)) { 3900Sstevel@tonic-gate crypto_mechanism_t lmech; 3910Sstevel@tonic-gate 3920Sstevel@tonic-gate lmech = *mech; 393*904Smcpowers KCF_SET_PROVIDER_MECHNUM(mech->cm_type, real_provider, &lmech); 394*904Smcpowers rv = KCF_PROV_MAC_INIT(real_provider, ctx, &lmech, key, tmpl, 3950Sstevel@tonic-gate KCF_SWFP_RHNDL(crq)); 396*904Smcpowers KCF_PROV_INCRSTATS(pd, rv); 3970Sstevel@tonic-gate } else { 3980Sstevel@tonic-gate KCF_WRAP_MAC_OPS_PARAMS(¶ms, KCF_OP_INIT, sid, mech, key, 3990Sstevel@tonic-gate NULL, NULL, tmpl); 400*904Smcpowers rv = kcf_submit_request(real_provider, ctx, crq, ¶ms, 401*904Smcpowers B_FALSE); 4020Sstevel@tonic-gate } 4030Sstevel@tonic-gate 404*904Smcpowers if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) 405*904Smcpowers KCF_PROV_REFRELE(real_provider); 406*904Smcpowers 407*904Smcpowers if ((rv == CRYPTO_SUCCESS) || (rv == CRYPTO_QUEUED)) 4080Sstevel@tonic-gate *ctxp = (crypto_context_t)ctx; 4090Sstevel@tonic-gate else { 4100Sstevel@tonic-gate /* Release the hold done in kcf_new_ctx(). */ 4110Sstevel@tonic-gate KCF_CONTEXT_REFRELE((kcf_context_t *)ctx->cc_framework_private); 4120Sstevel@tonic-gate } 4130Sstevel@tonic-gate 414*904Smcpowers return (rv); 4150Sstevel@tonic-gate } 4160Sstevel@tonic-gate 4170Sstevel@tonic-gate /* 4180Sstevel@tonic-gate * Same as crypto_mac_init_prov(), but relies on the KCF scheduler to 4190Sstevel@tonic-gate * choose a provider. See crypto_mac_init_prov() comments for more 4200Sstevel@tonic-gate * information. 4210Sstevel@tonic-gate */ 4220Sstevel@tonic-gate int 4230Sstevel@tonic-gate crypto_mac_init(crypto_mechanism_t *mech, crypto_key_t *key, 4240Sstevel@tonic-gate crypto_ctx_template_t tmpl, crypto_context_t *ctxp, 4250Sstevel@tonic-gate crypto_call_req_t *crq) 4260Sstevel@tonic-gate { 4270Sstevel@tonic-gate int error; 4280Sstevel@tonic-gate kcf_mech_entry_t *me; 4290Sstevel@tonic-gate kcf_provider_desc_t *pd; 4300Sstevel@tonic-gate kcf_ctx_template_t *ctx_tmpl; 4310Sstevel@tonic-gate crypto_spi_ctx_template_t spi_ctx_tmpl = NULL; 4320Sstevel@tonic-gate kcf_prov_tried_t *list = NULL; 4330Sstevel@tonic-gate 4340Sstevel@tonic-gate retry: 4350Sstevel@tonic-gate /* The pd is returned held */ 4360Sstevel@tonic-gate if ((pd = kcf_get_mech_provider(mech->cm_type, &me, &error, 4370Sstevel@tonic-gate list, CRYPTO_FG_MAC, CHECK_RESTRICT(crq), 0)) == NULL) { 4380Sstevel@tonic-gate if (list != NULL) 4390Sstevel@tonic-gate kcf_free_triedlist(list); 4400Sstevel@tonic-gate return (error); 4410Sstevel@tonic-gate } 4420Sstevel@tonic-gate 4430Sstevel@tonic-gate /* 4440Sstevel@tonic-gate * For SW providers, check the validity of the context template 4450Sstevel@tonic-gate * It is very rare that the generation number mis-matches, so 4460Sstevel@tonic-gate * is acceptable to fail here, and let the consumer recover by 4470Sstevel@tonic-gate * freeing this tmpl and create a new one for the key and new SW 4480Sstevel@tonic-gate * provider 4490Sstevel@tonic-gate */ 4500Sstevel@tonic-gate 4510Sstevel@tonic-gate if ((pd->pd_prov_type == CRYPTO_SW_PROVIDER) && 4520Sstevel@tonic-gate ((ctx_tmpl = (kcf_ctx_template_t *)tmpl) != NULL)) { 4530Sstevel@tonic-gate if (ctx_tmpl->ct_generation != me->me_gen_swprov) { 4540Sstevel@tonic-gate if (list != NULL) 4550Sstevel@tonic-gate kcf_free_triedlist(list); 4560Sstevel@tonic-gate KCF_PROV_REFRELE(pd); 4570Sstevel@tonic-gate return (CRYPTO_OLD_CTX_TEMPLATE); 4580Sstevel@tonic-gate } else { 4590Sstevel@tonic-gate spi_ctx_tmpl = ctx_tmpl->ct_prov_tmpl; 4600Sstevel@tonic-gate } 4610Sstevel@tonic-gate } 4620Sstevel@tonic-gate 4630Sstevel@tonic-gate error = crypto_mac_init_prov(pd, pd->pd_sid, mech, key, spi_ctx_tmpl, 4640Sstevel@tonic-gate ctxp, crq); 4650Sstevel@tonic-gate if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED && 4660Sstevel@tonic-gate IS_RECOVERABLE(error)) { 4670Sstevel@tonic-gate /* Add pd to the linked list of providers tried. */ 4680Sstevel@tonic-gate if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL) 4690Sstevel@tonic-gate goto retry; 4700Sstevel@tonic-gate } 4710Sstevel@tonic-gate 4720Sstevel@tonic-gate if (list != NULL) 4730Sstevel@tonic-gate kcf_free_triedlist(list); 4740Sstevel@tonic-gate 4750Sstevel@tonic-gate KCF_PROV_REFRELE(pd); 4760Sstevel@tonic-gate return (error); 4770Sstevel@tonic-gate } 4780Sstevel@tonic-gate 4790Sstevel@tonic-gate /* 4800Sstevel@tonic-gate * crypto_mac_update() 4810Sstevel@tonic-gate * 4820Sstevel@tonic-gate * Arguments: 4830Sstevel@tonic-gate * context: A crypto_context_t initialized by mac_init(). 4840Sstevel@tonic-gate * data: The message part to be MAC'ed 4850Sstevel@tonic-gate * cr: crypto_call_req_t calling conditions and call back info. 4860Sstevel@tonic-gate * 4870Sstevel@tonic-gate * Description: 4880Sstevel@tonic-gate * Asynchronously submits a request for, or synchronously performs a 4890Sstevel@tonic-gate * part of a MAC operation. 4900Sstevel@tonic-gate * 4910Sstevel@tonic-gate * Context: 4920Sstevel@tonic-gate * Process or interrupt, according to the semantics dictated by the 'cr'. 4930Sstevel@tonic-gate * 4940Sstevel@tonic-gate * Returns: 4950Sstevel@tonic-gate * See comment in the beginning of the file. 4960Sstevel@tonic-gate */ 4970Sstevel@tonic-gate int 4980Sstevel@tonic-gate crypto_mac_update(crypto_context_t context, crypto_data_t *data, 4990Sstevel@tonic-gate crypto_call_req_t *cr) 5000Sstevel@tonic-gate { 5010Sstevel@tonic-gate crypto_ctx_t *ctx = (crypto_ctx_t *)context; 5020Sstevel@tonic-gate kcf_context_t *kcf_ctx; 5030Sstevel@tonic-gate kcf_provider_desc_t *pd; 5040Sstevel@tonic-gate kcf_req_params_t params; 505*904Smcpowers int rv; 5060Sstevel@tonic-gate 5070Sstevel@tonic-gate if ((ctx == NULL) || 5080Sstevel@tonic-gate ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) || 5090Sstevel@tonic-gate ((pd = kcf_ctx->kc_prov_desc) == NULL)) { 5100Sstevel@tonic-gate return (CRYPTO_INVALID_CONTEXT); 5110Sstevel@tonic-gate } 5120Sstevel@tonic-gate 513*904Smcpowers ASSERT(pd->pd_prov_type != CRYPTO_LOGICAL_PROVIDER); 5140Sstevel@tonic-gate KCF_PROV_REFHOLD(pd); 5150Sstevel@tonic-gate 5160Sstevel@tonic-gate /* The fast path for SW providers. */ 5170Sstevel@tonic-gate if (CHECK_FASTPATH(cr, pd)) { 518*904Smcpowers rv = KCF_PROV_MAC_UPDATE(pd, ctx, data, NULL); 519*904Smcpowers KCF_PROV_INCRSTATS(pd, rv); 5200Sstevel@tonic-gate } else { 521*904Smcpowers KCF_WRAP_MAC_OPS_PARAMS(¶ms, KCF_OP_UPDATE, 522*904Smcpowers ctx->cc_session, NULL, NULL, data, NULL, NULL); 523*904Smcpowers rv = kcf_submit_request(pd, ctx, cr, ¶ms, B_FALSE); 5240Sstevel@tonic-gate } 5250Sstevel@tonic-gate 5260Sstevel@tonic-gate KCF_PROV_REFRELE(pd); 527*904Smcpowers return (rv); 5280Sstevel@tonic-gate } 5290Sstevel@tonic-gate 5300Sstevel@tonic-gate /* 5310Sstevel@tonic-gate * crypto_mac_final() 5320Sstevel@tonic-gate * 5330Sstevel@tonic-gate * Arguments: 5340Sstevel@tonic-gate * context: A crypto_context_t initialized by mac_init(). 5350Sstevel@tonic-gate * mac: Storage for the message authentication code. 5360Sstevel@tonic-gate * cr: crypto_call_req_t calling conditions and call back info. 5370Sstevel@tonic-gate * 5380Sstevel@tonic-gate * Description: 5390Sstevel@tonic-gate * Asynchronously submits a request for, or synchronously performs a 5400Sstevel@tonic-gate * part of a message authentication operation. 5410Sstevel@tonic-gate * 5420Sstevel@tonic-gate * Context: 5430Sstevel@tonic-gate * Process or interrupt, according to the semantics dictated by the 'cr'. 5440Sstevel@tonic-gate * 5450Sstevel@tonic-gate * Returns: 5460Sstevel@tonic-gate * See comment in the beginning of the file. 5470Sstevel@tonic-gate */ 5480Sstevel@tonic-gate int 5490Sstevel@tonic-gate crypto_mac_final(crypto_context_t context, crypto_data_t *mac, 5500Sstevel@tonic-gate crypto_call_req_t *cr) 5510Sstevel@tonic-gate { 5520Sstevel@tonic-gate crypto_ctx_t *ctx = (crypto_ctx_t *)context; 5530Sstevel@tonic-gate kcf_context_t *kcf_ctx; 5540Sstevel@tonic-gate kcf_provider_desc_t *pd; 5550Sstevel@tonic-gate kcf_req_params_t params; 556*904Smcpowers int rv; 5570Sstevel@tonic-gate 5580Sstevel@tonic-gate if ((ctx == NULL) || 5590Sstevel@tonic-gate ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) || 5600Sstevel@tonic-gate ((pd = kcf_ctx->kc_prov_desc) == NULL)) { 5610Sstevel@tonic-gate return (CRYPTO_INVALID_CONTEXT); 5620Sstevel@tonic-gate } 5630Sstevel@tonic-gate 564*904Smcpowers ASSERT(pd->pd_prov_type != CRYPTO_LOGICAL_PROVIDER); 5650Sstevel@tonic-gate KCF_PROV_REFHOLD(pd); 5660Sstevel@tonic-gate 5670Sstevel@tonic-gate /* The fast path for SW providers. */ 5680Sstevel@tonic-gate if (CHECK_FASTPATH(cr, pd)) { 569*904Smcpowers rv = KCF_PROV_MAC_FINAL(pd, ctx, mac, NULL); 570*904Smcpowers KCF_PROV_INCRSTATS(pd, rv); 5710Sstevel@tonic-gate } else { 572*904Smcpowers KCF_WRAP_MAC_OPS_PARAMS(¶ms, KCF_OP_FINAL, 573*904Smcpowers ctx->cc_session, NULL, NULL, NULL, mac, NULL); 574*904Smcpowers rv = kcf_submit_request(pd, ctx, cr, ¶ms, B_FALSE); 5750Sstevel@tonic-gate } 5760Sstevel@tonic-gate 5770Sstevel@tonic-gate KCF_PROV_REFRELE(pd); 5780Sstevel@tonic-gate /* Release the hold done in kcf_new_ctx() during init step. */ 579*904Smcpowers KCF_CONTEXT_COND_RELEASE(rv, kcf_ctx); 580*904Smcpowers return (rv); 5810Sstevel@tonic-gate } 5820Sstevel@tonic-gate 5830Sstevel@tonic-gate /* 5840Sstevel@tonic-gate * See comments for crypto_mac_update() and crypto_mac_final(). 5850Sstevel@tonic-gate */ 5860Sstevel@tonic-gate int 5870Sstevel@tonic-gate crypto_mac_single(crypto_context_t context, crypto_data_t *data, 5880Sstevel@tonic-gate crypto_data_t *mac, crypto_call_req_t *cr) 5890Sstevel@tonic-gate { 5900Sstevel@tonic-gate crypto_ctx_t *ctx = (crypto_ctx_t *)context; 5910Sstevel@tonic-gate kcf_context_t *kcf_ctx; 5920Sstevel@tonic-gate kcf_provider_desc_t *pd; 5930Sstevel@tonic-gate int error; 5940Sstevel@tonic-gate kcf_req_params_t params; 5950Sstevel@tonic-gate 5960Sstevel@tonic-gate 5970Sstevel@tonic-gate if ((ctx == NULL) || 5980Sstevel@tonic-gate ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) || 5990Sstevel@tonic-gate ((pd = kcf_ctx->kc_prov_desc) == NULL)) { 6000Sstevel@tonic-gate return (CRYPTO_INVALID_CONTEXT); 6010Sstevel@tonic-gate } 6020Sstevel@tonic-gate 6030Sstevel@tonic-gate KCF_PROV_REFHOLD(pd); 6040Sstevel@tonic-gate 6050Sstevel@tonic-gate /* The fast path for SW providers. */ 6060Sstevel@tonic-gate if (CHECK_FASTPATH(cr, pd)) { 6070Sstevel@tonic-gate error = KCF_PROV_MAC(pd, ctx, data, mac, NULL); 6080Sstevel@tonic-gate KCF_PROV_INCRSTATS(pd, error); 6090Sstevel@tonic-gate } else { 6100Sstevel@tonic-gate KCF_WRAP_MAC_OPS_PARAMS(¶ms, KCF_OP_SINGLE, pd->pd_sid, 6110Sstevel@tonic-gate NULL, NULL, data, mac, NULL); 6120Sstevel@tonic-gate error = kcf_submit_request(pd, ctx, cr, ¶ms, B_FALSE); 6130Sstevel@tonic-gate } 6140Sstevel@tonic-gate 6150Sstevel@tonic-gate KCF_PROV_REFRELE(pd); 6160Sstevel@tonic-gate /* Release the hold done in kcf_new_ctx() during init step. */ 6170Sstevel@tonic-gate KCF_CONTEXT_COND_RELEASE(error, kcf_ctx); 6180Sstevel@tonic-gate return (error); 6190Sstevel@tonic-gate } 620