4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #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>
34 * Message digest routines
38 * The following are the possible returned values common to all the routines
39 * below. The applicability of some of these return values depends on the
40 * presence of the arguments.
42 * CRYPTO_SUCCESS: The operation completed successfully.
43 * CRYPTO_QUEUED: A request was submitted successfully. The callback
44 * routine will be called when the operation is done.
45 * CRYPTO_MECHANISM_INVALID or CRYPTO_INVALID_MECH_PARAM
46 * for problems with the 'mech'.
47 * CRYPTO_INVALID_DATA for bogus 'data'
48 * CRYPTO_HOST_MEMORY for failure to allocate memory to handle this work.
49 * CRYPTO_INVALID_CONTEXT: Not a valid context.
50 * CRYPTO_BUSY: Cannot process the request now. Schedule a
51 * crypto_bufcall(), or try later.
52 * CRYPTO_NOT_SUPPORTED and CRYPTO_MECH_NOT_SUPPORTED:
53 * No provider is capable of a function or a mechanism.
58 * crypto_digest_prov()
61 * pd: pointer to the descriptor of the provider to use for this
63 * sid: provider session id.
64 * mech: crypto_mechanism_t pointer.
65 * mech_type is a valid value previously returned by
67 * When the mech's parameter is not NULL, its definition depends
68 * on the standard definition of the mechanism.
69 * data: The message to be digested.
70 * digest: Storage for the digest. The length needed depends on the
72 * cr: crypto_call_req_t calling conditions and call back info.
75 * Asynchronously submits a request for, or synchronously performs the
76 * digesting operation of 'data' on the specified
77 * provider with the specified session.
78 * When complete and successful, 'digest' will contain the digest value.
79 * The caller should hold a reference on the specified provider
80 * descriptor before calling this function.
83 * Process or interrupt, according to the semantics dictated by the 'cr'.
86 * See comment in the beginning of the file.
89 crypto_digest_prov(crypto_provider_t provider, crypto_session_id_t sid,
90 crypto_mechanism_t *mech, crypto_data_t *data, crypto_data_t *digest,
91 crypto_call_req_t *crq)
93 kcf_req_params_t params;
94 kcf_provider_desc_t *pd = provider;
95 kcf_provider_desc_t *real_provider = pd;
98 ASSERT(KCF_PROV_REFHELD(pd));
100 if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
101 rv = kcf_get_hardware_provider(mech->cm_type,
102 CRYPTO_MECH_INVALID, CHECK_RESTRICT(crq),
103 pd, &real_provider, CRYPTO_FG_DIGEST_ATOMIC);
105 if (rv != CRYPTO_SUCCESS)
108 KCF_WRAP_DIGEST_OPS_PARAMS(¶ms, KCF_OP_ATOMIC, sid, mech, NULL,
111 /* no crypto context to carry between multiple parts. */
112 rv = kcf_submit_request(real_provider, NULL, crq, ¶ms, B_FALSE);
113 if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
114 KCF_PROV_REFRELE(real_provider);
121 * Same as crypto_digest_prov(), but relies on the KCF scheduler to
122 * choose a provider. See crypto_digest_prov() comments for more information.
125 crypto_digest(crypto_mechanism_t *mech, crypto_data_t *data,
126 crypto_data_t *digest, crypto_call_req_t *crq)
129 kcf_provider_desc_t *pd;
130 kcf_req_params_t params;
131 kcf_prov_tried_t *list = NULL;
134 /* The pd is returned held */
135 if ((pd = kcf_get_mech_provider(mech->cm_type, NULL, &error, list,
136 CRYPTO_FG_DIGEST_ATOMIC, CHECK_RESTRICT(crq),
137 data->cd_length)) == NULL) {
139 kcf_free_triedlist(list);
143 /* The fast path for SW providers. */
144 if (CHECK_FASTPATH(crq, pd)) {
145 crypto_mechanism_t lmech;
148 KCF_SET_PROVIDER_MECHNUM(mech->cm_type, pd, &lmech);
149 error = KCF_PROV_DIGEST_ATOMIC(pd, pd->pd_sid, &lmech, data,
150 digest, KCF_SWFP_RHNDL(crq));
151 KCF_PROV_INCRSTATS(pd, error);
153 if (pd->pd_prov_type == CRYPTO_HW_PROVIDER &&
154 (pd->pd_flags & CRYPTO_HASH_NO_UPDATE) &&
155 (data->cd_length > pd->pd_hash_limit)) {
156 error = CRYPTO_BUFFER_TOO_BIG;
158 KCF_WRAP_DIGEST_OPS_PARAMS(¶ms, KCF_OP_ATOMIC,
159 pd->pd_sid, mech, NULL, data, digest);
161 /* no crypto context to carry between multiple parts. */
162 error = kcf_submit_request(pd, NULL, crq, ¶ms,
167 if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED &&
168 IS_RECOVERABLE(error)) {
169 /* Add pd to the linked list of providers tried. */
170 if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL)
175 kcf_free_triedlist(list);
177 KCF_PROV_REFRELE(pd);
182 * crypto_digest_init_prov()
184 * pd: pointer to the descriptor of the provider to use for this
186 * sid: provider session id.
187 * mech: crypto_mechanism_t pointer.
188 * mech_type is a valid value previously returned by
190 * When the mech's parameter is not NULL, its definition depends
191 * on the standard definition of the mechanism.
192 * ctxp: Pointer to a crypto_context_t.
193 * cr: crypto_call_req_t calling conditions and call back info.
196 * Asynchronously submits a request for, or synchronously performs the
197 * initialization of a message digest operation on the specified
198 * provider with the specified session.
199 * When complete and successful, 'ctxp' will contain a crypto_context_t
200 * valid for later calls to digest_update() and digest_final().
201 * The caller should hold a reference on the specified provider
202 * descriptor before calling this function.
205 crypto_digest_init_prov(crypto_provider_t provider, crypto_session_id_t sid,
206 crypto_mechanism_t *mech, crypto_context_t *ctxp, crypto_call_req_t *crq)
210 kcf_req_params_t params;
211 kcf_provider_desc_t *pd = provider;
212 kcf_provider_desc_t *real_provider = pd;
214 ASSERT(KCF_PROV_REFHELD(pd));
216 if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
217 error = kcf_get_hardware_provider(mech->cm_type,
218 CRYPTO_MECH_INVALID, CHECK_RESTRICT(crq), pd,
219 &real_provider, CRYPTO_FG_DIGEST);
221 if (error != CRYPTO_SUCCESS)
225 /* Allocate and initialize the canonical context */
226 if ((ctx = kcf_new_ctx(crq, real_provider, sid)) == NULL) {
227 if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
228 KCF_PROV_REFRELE(real_provider);
229 return (CRYPTO_HOST_MEMORY);
232 /* The fast path for SW providers. */
233 if (CHECK_FASTPATH(crq, pd)) {
234 crypto_mechanism_t lmech;
237 KCF_SET_PROVIDER_MECHNUM(mech->cm_type, real_provider, &lmech);
238 error = KCF_PROV_DIGEST_INIT(real_provider, ctx, &lmech,
239 KCF_SWFP_RHNDL(crq));
240 KCF_PROV_INCRSTATS(pd, error);
242 KCF_WRAP_DIGEST_OPS_PARAMS(¶ms, KCF_OP_INIT, sid,
243 mech, NULL, NULL, NULL);
244 error = kcf_submit_request(real_provider, ctx, crq, ¶ms,
248 if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
249 KCF_PROV_REFRELE(real_provider);
251 if ((error == CRYPTO_SUCCESS) || (error == CRYPTO_QUEUED))
252 *ctxp = (crypto_context_t)ctx;
254 /* Release the hold done in kcf_new_ctx(). */
255 KCF_CONTEXT_REFRELE((kcf_context_t *)ctx->cc_framework_private);
262 * Same as crypto_digest_init_prov(), but relies on the KCF scheduler
263 * to choose a provider. See crypto_digest_init_prov() comments for
267 crypto_digest_init(crypto_mechanism_t *mech, crypto_context_t *ctxp,
268 crypto_call_req_t *crq)
271 kcf_provider_desc_t *pd;
272 kcf_prov_tried_t *list = NULL;
275 /* The pd is returned held */
276 if ((pd = kcf_get_mech_provider(mech->cm_type, NULL, &error,
277 list, CRYPTO_FG_DIGEST, CHECK_RESTRICT(crq), 0)) == NULL) {
279 kcf_free_triedlist(list);
283 if (pd->pd_prov_type == CRYPTO_HW_PROVIDER &&
284 (pd->pd_flags & CRYPTO_HASH_NO_UPDATE)) {
286 * The hardware provider has limited digest support.
287 * So, we fallback early here to using a software provider.
289 * XXX - need to enhance to do the fallback later in
290 * crypto_digest_update() if the size of accumulated input data
291 * exceeds the maximum size digestable by hardware provider.
293 error = CRYPTO_BUFFER_TOO_BIG;
295 error = crypto_digest_init_prov(pd, pd->pd_sid,
299 if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED &&
300 IS_RECOVERABLE(error)) {
301 /* Add pd to the linked list of providers tried. */
302 if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL)
307 kcf_free_triedlist(list);
308 KCF_PROV_REFRELE(pd);
313 * crypto_digest_update()
316 * context: A crypto_context_t initialized by digest_init().
317 * data: The part of message to be digested.
318 * cr: crypto_call_req_t calling conditions and call back info.
321 * Asynchronously submits a request for, or synchronously performs a
322 * part of a message digest operation.
325 * Process or interrupt, according to the semantics dictated by the 'cr'.
328 * See comment in the beginning of the file.
331 crypto_digest_update(crypto_context_t context, crypto_data_t *data,
332 crypto_call_req_t *cr)
334 crypto_ctx_t *ctx = (crypto_ctx_t *)context;
335 kcf_context_t *kcf_ctx;
336 kcf_provider_desc_t *pd;
338 kcf_req_params_t params;
341 ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
342 ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
343 return (CRYPTO_INVALID_CONTEXT);
346 ASSERT(pd->pd_prov_type != CRYPTO_LOGICAL_PROVIDER);
348 /* The fast path for SW providers. */
349 if (CHECK_FASTPATH(cr, pd)) {
350 error = KCF_PROV_DIGEST_UPDATE(pd, ctx, data, NULL);
351 KCF_PROV_INCRSTATS(pd, error);
353 KCF_WRAP_DIGEST_OPS_PARAMS(¶ms, KCF_OP_UPDATE,
354 ctx->cc_session, NULL, NULL, data, NULL);
355 error = kcf_submit_request(pd, ctx, cr, ¶ms, B_FALSE);
362 * crypto_digest_final()
365 * context: A crypto_context_t initialized by digest_init().
366 * digest: The storage for the digest.
367 * cr: crypto_call_req_t calling conditions and call back info.
370 * Asynchronously submits a request for, or synchronously performs the
371 * final part of a message digest operation.
374 * Process or interrupt, according to the semantics dictated by the 'cr'.
377 * See comment in the beginning of the file.
380 crypto_digest_final(crypto_context_t context, crypto_data_t *digest,
381 crypto_call_req_t *cr)
383 crypto_ctx_t *ctx = (crypto_ctx_t *)context;
384 kcf_context_t *kcf_ctx;
385 kcf_provider_desc_t *pd;
387 kcf_req_params_t params;
390 ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
391 ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
392 return (CRYPTO_INVALID_CONTEXT);
395 ASSERT(pd->pd_prov_type != CRYPTO_LOGICAL_PROVIDER);
397 /* The fast path for SW providers. */
398 if (CHECK_FASTPATH(cr, pd)) {
399 error = KCF_PROV_DIGEST_FINAL(pd, ctx, digest, NULL);
400 KCF_PROV_INCRSTATS(pd, error);
402 KCF_WRAP_DIGEST_OPS_PARAMS(¶ms, KCF_OP_FINAL,
403 ctx->cc_session, NULL, NULL, NULL, digest);
404 error = kcf_submit_request(pd, ctx, cr, ¶ms, B_FALSE);
407 /* Release the hold done in kcf_new_ctx() during init step. */
408 KCF_CONTEXT_COND_RELEASE(error, kcf_ctx);
413 * Performs a digest update on the specified key. Note that there is
414 * no k-API crypto_digest_key() equivalent of this function.
417 crypto_digest_key_prov(crypto_context_t context, crypto_key_t *key,
418 crypto_call_req_t *cr)
420 crypto_ctx_t *ctx = (crypto_ctx_t *)context;
421 kcf_context_t *kcf_ctx;
422 kcf_provider_desc_t *pd;
424 kcf_req_params_t params;
427 ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
428 ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
429 return (CRYPTO_INVALID_CONTEXT);
432 ASSERT(pd->pd_prov_type != CRYPTO_LOGICAL_PROVIDER);
434 /* The fast path for SW providers. */
435 if (CHECK_FASTPATH(cr, pd)) {
436 error = KCF_PROV_DIGEST_KEY(pd, ctx, key, NULL);
437 KCF_PROV_INCRSTATS(pd, error);
439 KCF_WRAP_DIGEST_OPS_PARAMS(¶ms, KCF_OP_DIGEST_KEY,
440 ctx->cc_session, NULL, key, NULL, NULL);
441 error = kcf_submit_request(pd, ctx, cr, ¶ms, B_FALSE);
448 * See comments for crypto_digest_update() and crypto_digest_final().
451 crypto_digest_single(crypto_context_t context, crypto_data_t *data,
452 crypto_data_t *digest, crypto_call_req_t *cr)
454 crypto_ctx_t *ctx = (crypto_ctx_t *)context;
455 kcf_context_t *kcf_ctx;
456 kcf_provider_desc_t *pd;
458 kcf_req_params_t params;
461 ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
462 ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
463 return (CRYPTO_INVALID_CONTEXT);
467 /* The fast path for SW providers. */
468 if (CHECK_FASTPATH(cr, pd)) {
469 error = KCF_PROV_DIGEST(pd, ctx, data, digest, NULL);
470 KCF_PROV_INCRSTATS(pd, error);
472 KCF_WRAP_DIGEST_OPS_PARAMS(¶ms, KCF_OP_SINGLE, pd->pd_sid,
473 NULL, NULL, data, digest);
474 error = kcf_submit_request(pd, ctx, cr, ¶ms, B_FALSE);
477 /* Release the hold done in kcf_new_ctx() during init step. */
478 KCF_CONTEXT_COND_RELEASE(error, kcf_ctx);
483 EXPORT_SYMBOL(crypto_digest_prov);
484 EXPORT_SYMBOL(crypto_digest);
485 EXPORT_SYMBOL(crypto_digest_init_prov);
486 EXPORT_SYMBOL(crypto_digest_init);
487 EXPORT_SYMBOL(crypto_digest_update);
488 EXPORT_SYMBOL(crypto_digest_final);
489 EXPORT_SYMBOL(crypto_digest_key_prov);
490 EXPORT_SYMBOL(crypto_digest_single);