Path: blob/main/sys/contrib/openzfs/module/icp/api/kcf_ctxops.c
48531 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License (the "License").6* You may not use this file except in compliance with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/21/*22* Copyright 2007 Sun Microsystems, Inc. All rights reserved.23* Use is subject to license terms.24*/2526#include <sys/zfs_context.h>27#include <sys/crypto/common.h>28#include <sys/crypto/impl.h>29#include <sys/crypto/api.h>30#include <sys/crypto/spi.h>31#include <sys/crypto/sched_impl.h>3233/*34* Crypto contexts manipulation routines35*/3637/*38* crypto_create_ctx_template()39*40* Arguments:41*42* mech: crypto_mechanism_t pointer.43* mech_type is a valid value previously returned by44* crypto_mech2id();45* When the mech's parameter is not NULL, its definition depends46* on the standard definition of the mechanism.47* key: pointer to a crypto_key_t structure.48* ptmpl: a storage for the opaque crypto_ctx_template_t, allocated and49* initialized by the software provider this routine is50* dispatched to.51*52* Description:53* Redirects the call to the software provider of the specified54* mechanism. That provider will allocate and pre-compute/pre-expand55* the context template, reusable by later calls to crypto_xxx_init().56* The size and address of that provider context template are stored57* in an internal structure, kcf_ctx_template_t. The address of that58* structure is given back to the caller in *ptmpl.59*60* Context:61* Process or interrupt.62*63* Returns:64* CRYPTO_SUCCESS when the context template is successfully created.65* CRYPTO_HOST_MEMORY: mem alloc failure66* CRYPTO_ARGUMENTS_BAD: NULL storage for the ctx template.67* RYPTO_MECHANISM_INVALID: invalid mechanism 'mech'.68*/69int70crypto_create_ctx_template(crypto_mechanism_t *mech, crypto_key_t *key,71crypto_ctx_template_t *ptmpl)72{73int error;74kcf_mech_entry_t *me;75kcf_provider_desc_t *pd;76kcf_ctx_template_t *ctx_tmpl;77crypto_mechanism_t prov_mech;7879/* A few args validation */8081if (ptmpl == NULL)82return (CRYPTO_ARGUMENTS_BAD);8384if (mech == NULL)85return (CRYPTO_MECHANISM_INVALID);8687error = kcf_get_sw_prov(mech->cm_type, &pd, &me, B_TRUE);88if (error != CRYPTO_SUCCESS)89return (error);9091if ((ctx_tmpl = kmem_alloc(92sizeof (kcf_ctx_template_t), KM_SLEEP)) == NULL) {93KCF_PROV_REFRELE(pd);94return (CRYPTO_HOST_MEMORY);95}9697/* Pass a mechtype that the provider understands */98prov_mech.cm_type = KCF_TO_PROV_MECHNUM(pd, mech->cm_type);99prov_mech.cm_param = mech->cm_param;100prov_mech.cm_param_len = mech->cm_param_len;101102error = KCF_PROV_CREATE_CTX_TEMPLATE(pd, &prov_mech, key,103&(ctx_tmpl->ct_prov_tmpl), &(ctx_tmpl->ct_size));104105if (error == CRYPTO_SUCCESS) {106*ptmpl = ctx_tmpl;107} else {108kmem_free(ctx_tmpl, sizeof (kcf_ctx_template_t));109}110KCF_PROV_REFRELE(pd);111112return (error);113}114115/*116* crypto_destroy_ctx_template()117*118* Arguments:119*120* tmpl: an opaque crypto_ctx_template_t previously created by121* crypto_create_ctx_template()122*123* Description:124* Frees the embedded crypto_spi_ctx_template_t, then the125* kcf_ctx_template_t.126*127* Context:128* Process or interrupt.129*130*/131void132crypto_destroy_ctx_template(crypto_ctx_template_t tmpl)133{134kcf_ctx_template_t *ctx_tmpl = (kcf_ctx_template_t *)tmpl;135136if (ctx_tmpl == NULL)137return;138139ASSERT(ctx_tmpl->ct_prov_tmpl != NULL);140141memset(ctx_tmpl->ct_prov_tmpl, 0, ctx_tmpl->ct_size);142kmem_free(ctx_tmpl->ct_prov_tmpl, ctx_tmpl->ct_size);143kmem_free(ctx_tmpl, sizeof (kcf_ctx_template_t));144}145146#if defined(_KERNEL)147EXPORT_SYMBOL(crypto_create_ctx_template);148EXPORT_SYMBOL(crypto_destroy_ctx_template);149#endif150151152