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 /*
22*12304SValerie.Fenwick@Oracle.COM * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate
250Sstevel@tonic-gate #include <sys/errno.h>
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/kmem.h>
280Sstevel@tonic-gate #include <sys/cmn_err.h>
29904Smcpowers #include <sys/sysmacros.h>
300Sstevel@tonic-gate #include <sys/crypto/common.h>
310Sstevel@tonic-gate #include <sys/crypto/impl.h>
320Sstevel@tonic-gate #include <sys/crypto/api.h>
330Sstevel@tonic-gate #include <sys/crypto/spi.h>
340Sstevel@tonic-gate #include <sys/crypto/sched_impl.h>
350Sstevel@tonic-gate
36904Smcpowers #define CRYPTO_OPS_OFFSET(f) offsetof(crypto_ops_t, co_##f)
37904Smcpowers #define CRYPTO_DIGEST_OFFSET(f) offsetof(crypto_digest_ops_t, f)
38904Smcpowers
390Sstevel@tonic-gate /*
400Sstevel@tonic-gate * Message digest routines
410Sstevel@tonic-gate */
420Sstevel@tonic-gate
430Sstevel@tonic-gate /*
440Sstevel@tonic-gate * The following are the possible returned values common to all the routines
450Sstevel@tonic-gate * below. The applicability of some of these return values depends on the
460Sstevel@tonic-gate * presence of the arguments.
470Sstevel@tonic-gate *
480Sstevel@tonic-gate * CRYPTO_SUCCESS: The operation completed successfully.
490Sstevel@tonic-gate * CRYPTO_QUEUED: A request was submitted successfully. The callback
500Sstevel@tonic-gate * routine will be called when the operation is done.
510Sstevel@tonic-gate * CRYPTO_MECHANISM_INVALID or CRYPTO_INVALID_MECH_PARAM
520Sstevel@tonic-gate * for problems with the 'mech'.
530Sstevel@tonic-gate * CRYPTO_INVALID_DATA for bogus 'data'
540Sstevel@tonic-gate * CRYPTO_HOST_MEMORY for failure to allocate memory to handle this work.
550Sstevel@tonic-gate * CRYPTO_INVALID_CONTEXT: Not a valid context.
560Sstevel@tonic-gate * CRYPTO_BUSY: Cannot process the request now. Schedule a
570Sstevel@tonic-gate * crypto_bufcall(), or try later.
580Sstevel@tonic-gate * CRYPTO_NOT_SUPPORTED and CRYPTO_MECH_NOT_SUPPORTED:
590Sstevel@tonic-gate * No provider is capable of a function or a mechanism.
600Sstevel@tonic-gate */
610Sstevel@tonic-gate
620Sstevel@tonic-gate
630Sstevel@tonic-gate /*
640Sstevel@tonic-gate * crypto_digest_prov()
650Sstevel@tonic-gate *
660Sstevel@tonic-gate * Arguments:
670Sstevel@tonic-gate * pd: pointer to the descriptor of the provider to use for this
680Sstevel@tonic-gate * operation.
690Sstevel@tonic-gate * sid: provider session id.
700Sstevel@tonic-gate * mech: crypto_mechanism_t pointer.
710Sstevel@tonic-gate * mech_type is a valid value previously returned by
720Sstevel@tonic-gate * crypto_mech2id();
730Sstevel@tonic-gate * When the mech's parameter is not NULL, its definition depends
740Sstevel@tonic-gate * on the standard definition of the mechanism.
750Sstevel@tonic-gate * data: The message to be digested.
760Sstevel@tonic-gate * digest: Storage for the digest. The length needed depends on the
770Sstevel@tonic-gate * mechanism.
780Sstevel@tonic-gate * cr: crypto_call_req_t calling conditions and call back info.
790Sstevel@tonic-gate *
800Sstevel@tonic-gate * Description:
810Sstevel@tonic-gate * Asynchronously submits a request for, or synchronously performs the
820Sstevel@tonic-gate * digesting operation of 'data' on the specified
830Sstevel@tonic-gate * provider with the specified session.
840Sstevel@tonic-gate * When complete and successful, 'digest' will contain the digest value.
850Sstevel@tonic-gate * The caller should hold a reference on the specified provider
860Sstevel@tonic-gate * descriptor before calling this function.
870Sstevel@tonic-gate *
880Sstevel@tonic-gate * Context:
890Sstevel@tonic-gate * Process or interrupt, according to the semantics dictated by the 'cr'.
900Sstevel@tonic-gate *
910Sstevel@tonic-gate * Returns:
920Sstevel@tonic-gate * See comment in the beginning of the file.
930Sstevel@tonic-gate */
940Sstevel@tonic-gate int
crypto_digest_prov(crypto_provider_t provider,crypto_session_id_t sid,crypto_mechanism_t * mech,crypto_data_t * data,crypto_data_t * digest,crypto_call_req_t * crq)95904Smcpowers crypto_digest_prov(crypto_provider_t provider, crypto_session_id_t sid,
96904Smcpowers crypto_mechanism_t *mech, crypto_data_t *data, crypto_data_t *digest,
97904Smcpowers crypto_call_req_t *crq)
980Sstevel@tonic-gate {
990Sstevel@tonic-gate kcf_req_params_t params;
100904Smcpowers kcf_provider_desc_t *pd = provider;
101904Smcpowers kcf_provider_desc_t *real_provider = pd;
102904Smcpowers int rv;
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate ASSERT(KCF_PROV_REFHELD(pd));
105904Smcpowers
106904Smcpowers if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
10710444SVladimir.Kotal@Sun.COM rv = kcf_get_hardware_provider(mech->cm_type, NULL,
108*12304SValerie.Fenwick@Oracle.COM CRYPTO_MECH_INVALID, NULL, pd, &real_provider,
109*12304SValerie.Fenwick@Oracle.COM CRYPTO_FG_DIGEST_ATOMIC);
110904Smcpowers
111904Smcpowers if (rv != CRYPTO_SUCCESS)
112904Smcpowers return (rv);
113904Smcpowers }
1140Sstevel@tonic-gate KCF_WRAP_DIGEST_OPS_PARAMS(¶ms, KCF_OP_ATOMIC, sid, mech, NULL,
1150Sstevel@tonic-gate data, digest);
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate /* no crypto context to carry between multiple parts. */
118904Smcpowers rv = kcf_submit_request(real_provider, NULL, crq, ¶ms, B_FALSE);
119904Smcpowers if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
120904Smcpowers KCF_PROV_REFRELE(real_provider);
121904Smcpowers
122904Smcpowers return (rv);
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate /*
1260Sstevel@tonic-gate * Same as crypto_digest_prov(), but relies on the KCF scheduler to
1270Sstevel@tonic-gate * choose a provider. See crypto_digest_prov() comments for more information.
1280Sstevel@tonic-gate */
1290Sstevel@tonic-gate int
crypto_digest(crypto_mechanism_t * mech,crypto_data_t * data,crypto_data_t * digest,crypto_call_req_t * crq)1300Sstevel@tonic-gate crypto_digest(crypto_mechanism_t *mech, crypto_data_t *data,
1310Sstevel@tonic-gate crypto_data_t *digest, crypto_call_req_t *crq)
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate int error;
1340Sstevel@tonic-gate kcf_provider_desc_t *pd;
1350Sstevel@tonic-gate kcf_req_params_t params;
1360Sstevel@tonic-gate kcf_prov_tried_t *list = NULL;
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate retry:
1390Sstevel@tonic-gate /* The pd is returned held */
14010444SVladimir.Kotal@Sun.COM if ((pd = kcf_get_mech_provider(mech->cm_type, NULL, NULL, &error,
141*12304SValerie.Fenwick@Oracle.COM list, CRYPTO_FG_DIGEST_ATOMIC, data->cd_length)) == NULL) {
1420Sstevel@tonic-gate if (list != NULL)
1430Sstevel@tonic-gate kcf_free_triedlist(list);
1440Sstevel@tonic-gate return (error);
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate /* The fast path for SW providers. */
1480Sstevel@tonic-gate if (CHECK_FASTPATH(crq, pd)) {
1490Sstevel@tonic-gate crypto_mechanism_t lmech;
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate lmech = *mech;
1520Sstevel@tonic-gate KCF_SET_PROVIDER_MECHNUM(mech->cm_type, pd, &lmech);
1530Sstevel@tonic-gate error = KCF_PROV_DIGEST_ATOMIC(pd, pd->pd_sid, &lmech, data,
1540Sstevel@tonic-gate digest, KCF_SWFP_RHNDL(crq));
1550Sstevel@tonic-gate KCF_PROV_INCRSTATS(pd, error);
1560Sstevel@tonic-gate } else {
1574072Skrishna if (pd->pd_prov_type == CRYPTO_HW_PROVIDER &&
1584072Skrishna (pd->pd_flags & CRYPTO_HASH_NO_UPDATE) &&
1594072Skrishna (data->cd_length > pd->pd_hash_limit)) {
1604072Skrishna error = CRYPTO_BUFFER_TOO_BIG;
1614072Skrishna } else {
1624072Skrishna KCF_WRAP_DIGEST_OPS_PARAMS(¶ms, KCF_OP_ATOMIC,
1634072Skrishna pd->pd_sid, mech, NULL, data, digest);
1640Sstevel@tonic-gate
1654072Skrishna /* no crypto context to carry between multiple parts. */
1664072Skrishna error = kcf_submit_request(pd, NULL, crq, ¶ms,
1674072Skrishna B_FALSE);
1684072Skrishna }
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED &&
1720Sstevel@tonic-gate IS_RECOVERABLE(error)) {
1730Sstevel@tonic-gate /* Add pd to the linked list of providers tried. */
1740Sstevel@tonic-gate if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL)
1750Sstevel@tonic-gate goto retry;
1760Sstevel@tonic-gate }
1770Sstevel@tonic-gate
1780Sstevel@tonic-gate if (list != NULL)
1790Sstevel@tonic-gate kcf_free_triedlist(list);
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate KCF_PROV_REFRELE(pd);
1820Sstevel@tonic-gate return (error);
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate /*
1860Sstevel@tonic-gate * crypto_digest_init_prov()
1870Sstevel@tonic-gate *
1880Sstevel@tonic-gate * pd: pointer to the descriptor of the provider to use for this
1890Sstevel@tonic-gate * operation.
1900Sstevel@tonic-gate * sid: provider session id.
1910Sstevel@tonic-gate * mech: crypto_mechanism_t pointer.
1920Sstevel@tonic-gate * mech_type is a valid value previously returned by
1930Sstevel@tonic-gate * crypto_mech2id();
1940Sstevel@tonic-gate * When the mech's parameter is not NULL, its definition depends
1950Sstevel@tonic-gate * on the standard definition of the mechanism.
1960Sstevel@tonic-gate * ctxp: Pointer to a crypto_context_t.
1970Sstevel@tonic-gate * cr: crypto_call_req_t calling conditions and call back info.
1980Sstevel@tonic-gate *
1990Sstevel@tonic-gate * Description:
2000Sstevel@tonic-gate * Asynchronously submits a request for, or synchronously performs the
2010Sstevel@tonic-gate * initialization of a message digest operation on the specified
2020Sstevel@tonic-gate * provider with the specified session.
2030Sstevel@tonic-gate * When complete and successful, 'ctxp' will contain a crypto_context_t
2040Sstevel@tonic-gate * valid for later calls to digest_update() and digest_final().
2050Sstevel@tonic-gate * The caller should hold a reference on the specified provider
2060Sstevel@tonic-gate * descriptor before calling this function.
2070Sstevel@tonic-gate */
2080Sstevel@tonic-gate int
crypto_digest_init_prov(crypto_provider_t provider,crypto_session_id_t sid,crypto_mechanism_t * mech,crypto_context_t * ctxp,crypto_call_req_t * crq)209904Smcpowers crypto_digest_init_prov(crypto_provider_t provider, crypto_session_id_t sid,
210904Smcpowers crypto_mechanism_t *mech, crypto_context_t *ctxp, crypto_call_req_t *crq)
2110Sstevel@tonic-gate {
2120Sstevel@tonic-gate int error;
2130Sstevel@tonic-gate crypto_ctx_t *ctx;
2140Sstevel@tonic-gate kcf_req_params_t params;
215904Smcpowers kcf_provider_desc_t *pd = provider;
216904Smcpowers kcf_provider_desc_t *real_provider = pd;
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate ASSERT(KCF_PROV_REFHELD(pd));
2190Sstevel@tonic-gate
220904Smcpowers if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
22110444SVladimir.Kotal@Sun.COM error = kcf_get_hardware_provider(mech->cm_type, NULL,
222*12304SValerie.Fenwick@Oracle.COM CRYPTO_MECH_INVALID, NULL, pd, &real_provider,
223*12304SValerie.Fenwick@Oracle.COM CRYPTO_FG_DIGEST);
224904Smcpowers
225904Smcpowers if (error != CRYPTO_SUCCESS)
226904Smcpowers return (error);
227904Smcpowers }
228904Smcpowers
229904Smcpowers /* Allocate and initialize the canonical context */
230904Smcpowers if ((ctx = kcf_new_ctx(crq, real_provider, sid)) == NULL) {
231904Smcpowers if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
232904Smcpowers KCF_PROV_REFRELE(real_provider);
2330Sstevel@tonic-gate return (CRYPTO_HOST_MEMORY);
234904Smcpowers }
2350Sstevel@tonic-gate
2360Sstevel@tonic-gate /* The fast path for SW providers. */
2370Sstevel@tonic-gate if (CHECK_FASTPATH(crq, pd)) {
2380Sstevel@tonic-gate crypto_mechanism_t lmech;
2390Sstevel@tonic-gate
2400Sstevel@tonic-gate lmech = *mech;
241904Smcpowers KCF_SET_PROVIDER_MECHNUM(mech->cm_type, real_provider, &lmech);
242904Smcpowers error = KCF_PROV_DIGEST_INIT(real_provider, ctx, &lmech,
2430Sstevel@tonic-gate KCF_SWFP_RHNDL(crq));
2440Sstevel@tonic-gate KCF_PROV_INCRSTATS(pd, error);
2450Sstevel@tonic-gate } else {
2460Sstevel@tonic-gate KCF_WRAP_DIGEST_OPS_PARAMS(¶ms, KCF_OP_INIT, sid,
2470Sstevel@tonic-gate mech, NULL, NULL, NULL);
248904Smcpowers error = kcf_submit_request(real_provider, ctx, crq, ¶ms,
249904Smcpowers B_FALSE);
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate
252904Smcpowers if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
253904Smcpowers KCF_PROV_REFRELE(real_provider);
254904Smcpowers
2550Sstevel@tonic-gate if ((error == CRYPTO_SUCCESS) || (error == CRYPTO_QUEUED))
2560Sstevel@tonic-gate *ctxp = (crypto_context_t)ctx;
2570Sstevel@tonic-gate else {
2580Sstevel@tonic-gate /* Release the hold done in kcf_new_ctx(). */
2590Sstevel@tonic-gate KCF_CONTEXT_REFRELE((kcf_context_t *)ctx->cc_framework_private);
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate return (error);
2630Sstevel@tonic-gate }
2640Sstevel@tonic-gate
2650Sstevel@tonic-gate
2660Sstevel@tonic-gate /*
2670Sstevel@tonic-gate * Same as crypto_digest_init_prov(), but relies on the KCF scheduler
2680Sstevel@tonic-gate * to choose a provider. See crypto_digest_init_prov() comments for
2690Sstevel@tonic-gate * more information.
2700Sstevel@tonic-gate */
2710Sstevel@tonic-gate int
crypto_digest_init(crypto_mechanism_t * mech,crypto_context_t * ctxp,crypto_call_req_t * crq)2720Sstevel@tonic-gate crypto_digest_init(crypto_mechanism_t *mech, crypto_context_t *ctxp,
2730Sstevel@tonic-gate crypto_call_req_t *crq)
2740Sstevel@tonic-gate {
2750Sstevel@tonic-gate int error;
2760Sstevel@tonic-gate kcf_provider_desc_t *pd;
2770Sstevel@tonic-gate kcf_prov_tried_t *list = NULL;
2780Sstevel@tonic-gate
2790Sstevel@tonic-gate retry:
2800Sstevel@tonic-gate /* The pd is returned held */
28110444SVladimir.Kotal@Sun.COM if ((pd = kcf_get_mech_provider(mech->cm_type, NULL, NULL, &error,
282*12304SValerie.Fenwick@Oracle.COM list, CRYPTO_FG_DIGEST, 0)) == NULL) {
2830Sstevel@tonic-gate if (list != NULL)
2840Sstevel@tonic-gate kcf_free_triedlist(list);
2850Sstevel@tonic-gate return (error);
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate
2884072Skrishna if (pd->pd_prov_type == CRYPTO_HW_PROVIDER &&
2894072Skrishna (pd->pd_flags & CRYPTO_HASH_NO_UPDATE)) {
2904072Skrishna /*
2914072Skrishna * The hardware provider has limited digest support.
2924072Skrishna * So, we fallback early here to using a software provider.
2934072Skrishna *
2944072Skrishna * XXX - need to enhance to do the fallback later in
2954072Skrishna * crypto_digest_update() if the size of accumulated input data
2964072Skrishna * exceeds the maximum size digestable by hardware provider.
2974072Skrishna */
2984072Skrishna error = CRYPTO_BUFFER_TOO_BIG;
2994072Skrishna } else {
3004072Skrishna error = crypto_digest_init_prov(pd, pd->pd_sid,
3014072Skrishna mech, ctxp, crq);
3024072Skrishna }
3030Sstevel@tonic-gate
3040Sstevel@tonic-gate if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED &&
3050Sstevel@tonic-gate IS_RECOVERABLE(error)) {
3060Sstevel@tonic-gate /* Add pd to the linked list of providers tried. */
3070Sstevel@tonic-gate if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL)
3080Sstevel@tonic-gate goto retry;
3090Sstevel@tonic-gate }
3100Sstevel@tonic-gate
3110Sstevel@tonic-gate if (list != NULL)
3120Sstevel@tonic-gate kcf_free_triedlist(list);
3130Sstevel@tonic-gate KCF_PROV_REFRELE(pd);
3140Sstevel@tonic-gate return (error);
3150Sstevel@tonic-gate }
3160Sstevel@tonic-gate
3170Sstevel@tonic-gate /*
3180Sstevel@tonic-gate * crypto_digest_update()
3190Sstevel@tonic-gate *
3200Sstevel@tonic-gate * Arguments:
3210Sstevel@tonic-gate * context: A crypto_context_t initialized by digest_init().
3220Sstevel@tonic-gate * data: The part of message to be digested.
3230Sstevel@tonic-gate * cr: crypto_call_req_t calling conditions and call back info.
3240Sstevel@tonic-gate *
3250Sstevel@tonic-gate * Description:
3260Sstevel@tonic-gate * Asynchronously submits a request for, or synchronously performs a
3270Sstevel@tonic-gate * part of a message digest operation.
3280Sstevel@tonic-gate *
3290Sstevel@tonic-gate * Context:
3300Sstevel@tonic-gate * Process or interrupt, according to the semantics dictated by the 'cr'.
3310Sstevel@tonic-gate *
3320Sstevel@tonic-gate * Returns:
3330Sstevel@tonic-gate * See comment in the beginning of the file.
3340Sstevel@tonic-gate */
3350Sstevel@tonic-gate int
crypto_digest_update(crypto_context_t context,crypto_data_t * data,crypto_call_req_t * cr)3360Sstevel@tonic-gate crypto_digest_update(crypto_context_t context, crypto_data_t *data,
3370Sstevel@tonic-gate crypto_call_req_t *cr)
3380Sstevel@tonic-gate {
3390Sstevel@tonic-gate crypto_ctx_t *ctx = (crypto_ctx_t *)context;
3400Sstevel@tonic-gate kcf_context_t *kcf_ctx;
3410Sstevel@tonic-gate kcf_provider_desc_t *pd;
3420Sstevel@tonic-gate int error;
3430Sstevel@tonic-gate kcf_req_params_t params;
3440Sstevel@tonic-gate
3450Sstevel@tonic-gate if ((ctx == NULL) ||
3460Sstevel@tonic-gate ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
3470Sstevel@tonic-gate ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
3480Sstevel@tonic-gate return (CRYPTO_INVALID_CONTEXT);
3490Sstevel@tonic-gate }
3500Sstevel@tonic-gate
351904Smcpowers ASSERT(pd->pd_prov_type != CRYPTO_LOGICAL_PROVIDER);
3520Sstevel@tonic-gate
3530Sstevel@tonic-gate /* The fast path for SW providers. */
3540Sstevel@tonic-gate if (CHECK_FASTPATH(cr, pd)) {
3550Sstevel@tonic-gate error = KCF_PROV_DIGEST_UPDATE(pd, ctx, data, NULL);
3560Sstevel@tonic-gate KCF_PROV_INCRSTATS(pd, error);
3570Sstevel@tonic-gate } else {
358904Smcpowers KCF_WRAP_DIGEST_OPS_PARAMS(¶ms, KCF_OP_UPDATE,
359904Smcpowers ctx->cc_session, NULL, NULL, data, NULL);
3600Sstevel@tonic-gate error = kcf_submit_request(pd, ctx, cr, ¶ms, B_FALSE);
3610Sstevel@tonic-gate }
3620Sstevel@tonic-gate
3630Sstevel@tonic-gate return (error);
3640Sstevel@tonic-gate }
3650Sstevel@tonic-gate
3660Sstevel@tonic-gate /*
3670Sstevel@tonic-gate * crypto_digest_final()
3680Sstevel@tonic-gate *
3690Sstevel@tonic-gate * Arguments:
3700Sstevel@tonic-gate * context: A crypto_context_t initialized by digest_init().
3710Sstevel@tonic-gate * digest: The storage for the digest.
3720Sstevel@tonic-gate * cr: crypto_call_req_t calling conditions and call back info.
3730Sstevel@tonic-gate *
3740Sstevel@tonic-gate * Description:
3750Sstevel@tonic-gate * Asynchronously submits a request for, or synchronously performs the
3760Sstevel@tonic-gate * final part of a message digest operation.
3770Sstevel@tonic-gate *
3780Sstevel@tonic-gate * Context:
3790Sstevel@tonic-gate * Process or interrupt, according to the semantics dictated by the 'cr'.
3800Sstevel@tonic-gate *
3810Sstevel@tonic-gate * Returns:
3820Sstevel@tonic-gate * See comment in the beginning of the file.
3830Sstevel@tonic-gate */
3840Sstevel@tonic-gate int
crypto_digest_final(crypto_context_t context,crypto_data_t * digest,crypto_call_req_t * cr)3850Sstevel@tonic-gate crypto_digest_final(crypto_context_t context, crypto_data_t *digest,
3860Sstevel@tonic-gate crypto_call_req_t *cr)
3870Sstevel@tonic-gate {
3880Sstevel@tonic-gate crypto_ctx_t *ctx = (crypto_ctx_t *)context;
3890Sstevel@tonic-gate kcf_context_t *kcf_ctx;
3900Sstevel@tonic-gate kcf_provider_desc_t *pd;
3910Sstevel@tonic-gate int error;
3920Sstevel@tonic-gate kcf_req_params_t params;
3930Sstevel@tonic-gate
3940Sstevel@tonic-gate if ((ctx == NULL) ||
3950Sstevel@tonic-gate ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
3960Sstevel@tonic-gate ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
3970Sstevel@tonic-gate return (CRYPTO_INVALID_CONTEXT);
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate
400904Smcpowers ASSERT(pd->pd_prov_type != CRYPTO_LOGICAL_PROVIDER);
4010Sstevel@tonic-gate
4020Sstevel@tonic-gate /* The fast path for SW providers. */
4030Sstevel@tonic-gate if (CHECK_FASTPATH(cr, pd)) {
4040Sstevel@tonic-gate error = KCF_PROV_DIGEST_FINAL(pd, ctx, digest, NULL);
4050Sstevel@tonic-gate KCF_PROV_INCRSTATS(pd, error);
4060Sstevel@tonic-gate } else {
407904Smcpowers KCF_WRAP_DIGEST_OPS_PARAMS(¶ms, KCF_OP_FINAL,
408904Smcpowers ctx->cc_session, NULL, NULL, NULL, digest);
4090Sstevel@tonic-gate error = kcf_submit_request(pd, ctx, cr, ¶ms, B_FALSE);
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate
4120Sstevel@tonic-gate /* Release the hold done in kcf_new_ctx() during init step. */
4130Sstevel@tonic-gate KCF_CONTEXT_COND_RELEASE(error, kcf_ctx);
4140Sstevel@tonic-gate return (error);
4150Sstevel@tonic-gate }
4160Sstevel@tonic-gate
4170Sstevel@tonic-gate /*
4180Sstevel@tonic-gate * Performs a digest update on the specified key. Note that there is
4190Sstevel@tonic-gate * no k-API crypto_digest_key() equivalent of this function.
4200Sstevel@tonic-gate */
4210Sstevel@tonic-gate int
crypto_digest_key_prov(crypto_context_t context,crypto_key_t * key,crypto_call_req_t * cr)4220Sstevel@tonic-gate crypto_digest_key_prov(crypto_context_t context, crypto_key_t *key,
4230Sstevel@tonic-gate crypto_call_req_t *cr)
4240Sstevel@tonic-gate {
4250Sstevel@tonic-gate crypto_ctx_t *ctx = (crypto_ctx_t *)context;
4260Sstevel@tonic-gate kcf_context_t *kcf_ctx;
4270Sstevel@tonic-gate kcf_provider_desc_t *pd;
4280Sstevel@tonic-gate int error;
4290Sstevel@tonic-gate kcf_req_params_t params;
4300Sstevel@tonic-gate
4310Sstevel@tonic-gate if ((ctx == NULL) ||
4320Sstevel@tonic-gate ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
4330Sstevel@tonic-gate ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
4340Sstevel@tonic-gate return (CRYPTO_INVALID_CONTEXT);
4350Sstevel@tonic-gate }
4360Sstevel@tonic-gate
437904Smcpowers ASSERT(pd->pd_prov_type != CRYPTO_LOGICAL_PROVIDER);
4380Sstevel@tonic-gate
4390Sstevel@tonic-gate /* The fast path for SW providers. */
4400Sstevel@tonic-gate if (CHECK_FASTPATH(cr, pd)) {
4410Sstevel@tonic-gate error = KCF_PROV_DIGEST_KEY(pd, ctx, key, NULL);
4420Sstevel@tonic-gate KCF_PROV_INCRSTATS(pd, error);
4430Sstevel@tonic-gate } else {
4440Sstevel@tonic-gate KCF_WRAP_DIGEST_OPS_PARAMS(¶ms, KCF_OP_DIGEST_KEY,
445904Smcpowers ctx->cc_session, NULL, key, NULL, NULL);
4460Sstevel@tonic-gate error = kcf_submit_request(pd, ctx, cr, ¶ms, B_FALSE);
4470Sstevel@tonic-gate }
4480Sstevel@tonic-gate
4490Sstevel@tonic-gate return (error);
4500Sstevel@tonic-gate }
4510Sstevel@tonic-gate
4520Sstevel@tonic-gate /*
4530Sstevel@tonic-gate * See comments for crypto_digest_update() and crypto_digest_final().
4540Sstevel@tonic-gate */
4550Sstevel@tonic-gate int
crypto_digest_single(crypto_context_t context,crypto_data_t * data,crypto_data_t * digest,crypto_call_req_t * cr)4560Sstevel@tonic-gate crypto_digest_single(crypto_context_t context, crypto_data_t *data,
4570Sstevel@tonic-gate crypto_data_t *digest, crypto_call_req_t *cr)
4580Sstevel@tonic-gate {
4590Sstevel@tonic-gate crypto_ctx_t *ctx = (crypto_ctx_t *)context;
4600Sstevel@tonic-gate kcf_context_t *kcf_ctx;
4610Sstevel@tonic-gate kcf_provider_desc_t *pd;
4620Sstevel@tonic-gate int error;
4630Sstevel@tonic-gate kcf_req_params_t params;
4640Sstevel@tonic-gate
4650Sstevel@tonic-gate if ((ctx == NULL) ||
4660Sstevel@tonic-gate ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
4670Sstevel@tonic-gate ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
4680Sstevel@tonic-gate return (CRYPTO_INVALID_CONTEXT);
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate
4710Sstevel@tonic-gate
4720Sstevel@tonic-gate /* The fast path for SW providers. */
4730Sstevel@tonic-gate if (CHECK_FASTPATH(cr, pd)) {
4740Sstevel@tonic-gate error = KCF_PROV_DIGEST(pd, ctx, data, digest, NULL);
4750Sstevel@tonic-gate KCF_PROV_INCRSTATS(pd, error);
4760Sstevel@tonic-gate } else {
4770Sstevel@tonic-gate KCF_WRAP_DIGEST_OPS_PARAMS(¶ms, KCF_OP_SINGLE, pd->pd_sid,
4780Sstevel@tonic-gate NULL, NULL, data, digest);
4790Sstevel@tonic-gate error = kcf_submit_request(pd, ctx, cr, ¶ms, B_FALSE);
4800Sstevel@tonic-gate }
4810Sstevel@tonic-gate
4820Sstevel@tonic-gate /* Release the hold done in kcf_new_ctx() during init step. */
4830Sstevel@tonic-gate KCF_CONTEXT_COND_RELEASE(error, kcf_ctx);
4840Sstevel@tonic-gate return (error);
4850Sstevel@tonic-gate }
486