Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/contrib/openzfs/module/icp/api/kcf_ctxops.c
48531 views
1
// SPDX-License-Identifier: CDDL-1.0
2
/*
3
* CDDL HEADER START
4
*
5
* The contents of this file are subject to the terms of the
6
* Common Development and Distribution License (the "License").
7
* You may not use this file except in compliance with the License.
8
*
9
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10
* or https://opensource.org/licenses/CDDL-1.0.
11
* See the License for the specific language governing permissions
12
* and limitations under the License.
13
*
14
* When distributing Covered Code, include this CDDL HEADER in each
15
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16
* If applicable, add the following below this CDDL HEADER, with the
17
* fields enclosed by brackets "[]" replaced with your own identifying
18
* information: Portions Copyright [yyyy] [name of copyright owner]
19
*
20
* CDDL HEADER END
21
*/
22
/*
23
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24
* Use is subject to license terms.
25
*/
26
27
#include <sys/zfs_context.h>
28
#include <sys/crypto/common.h>
29
#include <sys/crypto/impl.h>
30
#include <sys/crypto/api.h>
31
#include <sys/crypto/spi.h>
32
#include <sys/crypto/sched_impl.h>
33
34
/*
35
* Crypto contexts manipulation routines
36
*/
37
38
/*
39
* crypto_create_ctx_template()
40
*
41
* Arguments:
42
*
43
* mech: crypto_mechanism_t pointer.
44
* mech_type is a valid value previously returned by
45
* crypto_mech2id();
46
* When the mech's parameter is not NULL, its definition depends
47
* on the standard definition of the mechanism.
48
* key: pointer to a crypto_key_t structure.
49
* ptmpl: a storage for the opaque crypto_ctx_template_t, allocated and
50
* initialized by the software provider this routine is
51
* dispatched to.
52
*
53
* Description:
54
* Redirects the call to the software provider of the specified
55
* mechanism. That provider will allocate and pre-compute/pre-expand
56
* the context template, reusable by later calls to crypto_xxx_init().
57
* The size and address of that provider context template are stored
58
* in an internal structure, kcf_ctx_template_t. The address of that
59
* structure is given back to the caller in *ptmpl.
60
*
61
* Context:
62
* Process or interrupt.
63
*
64
* Returns:
65
* CRYPTO_SUCCESS when the context template is successfully created.
66
* CRYPTO_HOST_MEMORY: mem alloc failure
67
* CRYPTO_ARGUMENTS_BAD: NULL storage for the ctx template.
68
* RYPTO_MECHANISM_INVALID: invalid mechanism 'mech'.
69
*/
70
int
71
crypto_create_ctx_template(crypto_mechanism_t *mech, crypto_key_t *key,
72
crypto_ctx_template_t *ptmpl)
73
{
74
int error;
75
kcf_mech_entry_t *me;
76
kcf_provider_desc_t *pd;
77
kcf_ctx_template_t *ctx_tmpl;
78
crypto_mechanism_t prov_mech;
79
80
/* A few args validation */
81
82
if (ptmpl == NULL)
83
return (CRYPTO_ARGUMENTS_BAD);
84
85
if (mech == NULL)
86
return (CRYPTO_MECHANISM_INVALID);
87
88
error = kcf_get_sw_prov(mech->cm_type, &pd, &me, B_TRUE);
89
if (error != CRYPTO_SUCCESS)
90
return (error);
91
92
if ((ctx_tmpl = kmem_alloc(
93
sizeof (kcf_ctx_template_t), KM_SLEEP)) == NULL) {
94
KCF_PROV_REFRELE(pd);
95
return (CRYPTO_HOST_MEMORY);
96
}
97
98
/* Pass a mechtype that the provider understands */
99
prov_mech.cm_type = KCF_TO_PROV_MECHNUM(pd, mech->cm_type);
100
prov_mech.cm_param = mech->cm_param;
101
prov_mech.cm_param_len = mech->cm_param_len;
102
103
error = KCF_PROV_CREATE_CTX_TEMPLATE(pd, &prov_mech, key,
104
&(ctx_tmpl->ct_prov_tmpl), &(ctx_tmpl->ct_size));
105
106
if (error == CRYPTO_SUCCESS) {
107
*ptmpl = ctx_tmpl;
108
} else {
109
kmem_free(ctx_tmpl, sizeof (kcf_ctx_template_t));
110
}
111
KCF_PROV_REFRELE(pd);
112
113
return (error);
114
}
115
116
/*
117
* crypto_destroy_ctx_template()
118
*
119
* Arguments:
120
*
121
* tmpl: an opaque crypto_ctx_template_t previously created by
122
* crypto_create_ctx_template()
123
*
124
* Description:
125
* Frees the embedded crypto_spi_ctx_template_t, then the
126
* kcf_ctx_template_t.
127
*
128
* Context:
129
* Process or interrupt.
130
*
131
*/
132
void
133
crypto_destroy_ctx_template(crypto_ctx_template_t tmpl)
134
{
135
kcf_ctx_template_t *ctx_tmpl = (kcf_ctx_template_t *)tmpl;
136
137
if (ctx_tmpl == NULL)
138
return;
139
140
ASSERT(ctx_tmpl->ct_prov_tmpl != NULL);
141
142
memset(ctx_tmpl->ct_prov_tmpl, 0, ctx_tmpl->ct_size);
143
kmem_free(ctx_tmpl->ct_prov_tmpl, ctx_tmpl->ct_size);
144
kmem_free(ctx_tmpl, sizeof (kcf_ctx_template_t));
145
}
146
147
#if defined(_KERNEL)
148
EXPORT_SYMBOL(crypto_create_ctx_template);
149
EXPORT_SYMBOL(crypto_destroy_ctx_template);
150
#endif
151
152