xref: /titanic_52/usr/src/uts/common/crypto/api/kcf_sign.c (revision f841f6ad96ea6675d6c6b35c749eaac601799fdf)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/errno.h>
29 #include <sys/types.h>
30 #include <sys/kmem.h>
31 #include <sys/sysmacros.h>
32 #include <sys/crypto/common.h>
33 #include <sys/crypto/impl.h>
34 #include <sys/crypto/api.h>
35 #include <sys/crypto/spi.h>
36 #include <sys/crypto/sched_impl.h>
37 
38 #define	CRYPTO_OPS_OFFSET(f)		offsetof(crypto_ops_t, co_##f)
39 #define	CRYPTO_SIGN_OFFSET(f)		offsetof(crypto_sign_ops_t, f)
40 
41 /*
42  * Sign entry points.
43  */
44 
45 /*
46  * See comments for crypto_digest_init_prov().
47  */
48 int
49 crypto_sign_init_prov(crypto_provider_t provider, crypto_session_id_t sid,
50     crypto_mechanism_t *mech, crypto_key_t *key, crypto_ctx_template_t tmpl,
51     crypto_context_t *ctxp, crypto_call_req_t *crq)
52 {
53 	int rv;
54 	crypto_ctx_t *ctx;
55 	kcf_req_params_t params;
56 	kcf_provider_desc_t *pd = provider;
57 	kcf_provider_desc_t *real_provider = pd;
58 
59 	ASSERT(KCF_PROV_REFHELD(pd));
60 
61 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
62 		rv = kcf_get_hardware_provider(mech->cm_type,
63 		    CRYPTO_MECH_INVALID, CHECK_RESTRICT(crq), pd,
64 		    &real_provider, CRYPTO_FG_SIGN);
65 
66 		if (rv != CRYPTO_SUCCESS)
67 			return (rv);
68 	}
69 
70 	/* Allocate and initialize the canonical context */
71 	if ((ctx = kcf_new_ctx(crq, real_provider, sid)) == NULL) {
72 		if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
73 			KCF_PROV_REFRELE(real_provider);
74 		return (CRYPTO_HOST_MEMORY);
75 	}
76 
77 	KCF_WRAP_SIGN_OPS_PARAMS(&params, KCF_OP_INIT, sid, mech,
78 	    key, NULL, NULL, tmpl);
79 	rv = kcf_submit_request(real_provider, ctx, crq, &params, B_FALSE);
80 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
81 		KCF_PROV_REFRELE(real_provider);
82 
83 	if ((rv == CRYPTO_SUCCESS) || (rv == CRYPTO_QUEUED))
84 		*ctxp = (crypto_context_t)ctx;
85 	else {
86 		/* Release the hold done in kcf_new_ctx(). */
87 		KCF_CONTEXT_REFRELE((kcf_context_t *)ctx->cc_framework_private);
88 	}
89 
90 	return (rv);
91 }
92 
93 int
94 crypto_sign_init(crypto_mechanism_t *mech, crypto_key_t *key,
95     crypto_ctx_template_t tmpl, crypto_context_t *ctxp, crypto_call_req_t *crq)
96 {
97 	int error;
98 	kcf_mech_entry_t *me;
99 	kcf_provider_desc_t *pd;
100 	kcf_prov_tried_t *list = NULL;
101 	kcf_ctx_template_t *ctx_tmpl;
102 	crypto_spi_ctx_template_t spi_ctx_tmpl = NULL;
103 
104 retry:
105 	/* The pd is returned held */
106 	if ((pd = kcf_get_mech_provider(mech->cm_type, &me, &error,
107 	    list, CRYPTO_FG_SIGN, CHECK_RESTRICT(crq), 0)) == NULL) {
108 		if (list != NULL)
109 			kcf_free_triedlist(list);
110 		return (error);
111 	}
112 
113 	/*
114 	 * For SW providers, check the validity of the context template
115 	 * It is very rare that the generation number mis-matches, so
116 	 * it is acceptable to fail here, and let the consumer recover by
117 	 * freeing this tmpl and create a new one for the key and new SW
118 	 * provider.
119 	 */
120 	if ((pd->pd_prov_type == CRYPTO_SW_PROVIDER) &&
121 	    ((ctx_tmpl = (kcf_ctx_template_t *)tmpl) != NULL)) {
122 		if (ctx_tmpl->ct_generation != me->me_gen_swprov) {
123 			if (list != NULL)
124 				kcf_free_triedlist(list);
125 			KCF_PROV_REFRELE(pd);
126 			return (CRYPTO_OLD_CTX_TEMPLATE);
127 		} else {
128 			spi_ctx_tmpl = ctx_tmpl->ct_prov_tmpl;
129 		}
130 	}
131 
132 	error = crypto_sign_init_prov(pd, pd->pd_sid, mech, key, spi_ctx_tmpl,
133 	    ctxp, crq);
134 
135 	if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED &&
136 	    IS_RECOVERABLE(error)) {
137 		/* Add pd to the linked list of providers tried. */
138 		if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL)
139 			goto retry;
140 	}
141 
142 	if (list != NULL)
143 		kcf_free_triedlist(list);
144 	KCF_PROV_REFRELE(pd);
145 	return (error);
146 }
147 
148 int
149 crypto_sign_single(crypto_context_t context, crypto_data_t *data,
150     crypto_data_t *signature, crypto_call_req_t *cr)
151 {
152 	crypto_ctx_t *ctx = (crypto_ctx_t *)context;
153 	kcf_context_t *kcf_ctx;
154 	kcf_provider_desc_t *pd;
155 	int error;
156 	kcf_req_params_t params;
157 
158 	if ((ctx == NULL) ||
159 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
160 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
161 		return (CRYPTO_INVALID_CONTEXT);
162 	}
163 
164 	KCF_PROV_REFHOLD(pd);
165 	KCF_WRAP_SIGN_OPS_PARAMS(&params, KCF_OP_SINGLE, 0, NULL,
166 	    NULL, data, signature, NULL);
167 	error = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
168 	KCF_PROV_REFRELE(pd);
169 
170 	/* Release the hold done in kcf_new_ctx() during init step. */
171 	KCF_CONTEXT_COND_RELEASE(error, kcf_ctx);
172 	return (error);
173 }
174 
175 /*
176  * See comments for crypto_digest_update().
177  */
178 int
179 crypto_sign_update(crypto_context_t context, crypto_data_t *data,
180     crypto_call_req_t *cr)
181 {
182 	crypto_ctx_t *ctx = (crypto_ctx_t *)context;
183 	kcf_context_t *kcf_ctx;
184 	kcf_provider_desc_t *pd;
185 	kcf_req_params_t params;
186 	int rv;
187 
188 	if ((ctx == NULL) ||
189 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
190 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
191 		return (CRYPTO_INVALID_CONTEXT);
192 	}
193 
194 	ASSERT(pd->pd_prov_type != CRYPTO_LOGICAL_PROVIDER);
195 	KCF_PROV_REFHOLD(pd);
196 	KCF_WRAP_SIGN_OPS_PARAMS(&params, KCF_OP_UPDATE, ctx->cc_session, NULL,
197 	    NULL, data, NULL, NULL);
198 	rv = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
199 	KCF_PROV_REFRELE(pd);
200 
201 	return (rv);
202 }
203 
204 /*
205  * See comments for crypto_digest_final().
206  */
207 int
208 crypto_sign_final(crypto_context_t context, crypto_data_t *signature,
209     crypto_call_req_t *cr)
210 {
211 	crypto_ctx_t *ctx = (crypto_ctx_t *)context;
212 	kcf_context_t *kcf_ctx;
213 	kcf_provider_desc_t *pd;
214 	int rv;
215 	kcf_req_params_t params;
216 
217 	if ((ctx == NULL) ||
218 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
219 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
220 		return (CRYPTO_INVALID_CONTEXT);
221 	}
222 
223 	ASSERT(pd->pd_prov_type != CRYPTO_LOGICAL_PROVIDER);
224 	KCF_PROV_REFHOLD(pd);
225 	KCF_WRAP_SIGN_OPS_PARAMS(&params, KCF_OP_FINAL, ctx->cc_session, NULL,
226 	    NULL, NULL, signature, NULL);
227 	rv = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
228 	KCF_PROV_REFRELE(pd);
229 
230 	/* Release the hold done in kcf_new_ctx() during init step. */
231 	KCF_CONTEXT_COND_RELEASE(rv, kcf_ctx);
232 	return (rv);
233 }
234 
235 int
236 crypto_sign_prov(crypto_provider_t provider, crypto_session_id_t sid,
237     crypto_mechanism_t *mech, crypto_key_t *key, crypto_data_t *data,
238     crypto_ctx_template_t tmpl, crypto_data_t *signature,
239     crypto_call_req_t *crq)
240 {
241 	kcf_req_params_t params;
242 	kcf_provider_desc_t *pd = provider;
243 	kcf_provider_desc_t *real_provider = pd;
244 	int rv;
245 
246 	ASSERT(KCF_PROV_REFHELD(pd));
247 
248 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
249 		rv = kcf_get_hardware_provider(mech->cm_type,
250 		    CRYPTO_MECH_INVALID, CHECK_RESTRICT(crq),
251 		    pd, &real_provider, CRYPTO_FG_SIGN_ATOMIC);
252 
253 		if (rv != CRYPTO_SUCCESS)
254 			return (rv);
255 	}
256 	KCF_WRAP_SIGN_OPS_PARAMS(&params, KCF_OP_ATOMIC, sid, mech,
257 	    key, data, signature, tmpl);
258 	rv = kcf_submit_request(real_provider, NULL, crq, &params, B_FALSE);
259 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
260 		KCF_PROV_REFRELE(real_provider);
261 
262 	return (rv);
263 }
264 
265 static int
266 sign_sr_atomic_common(crypto_mechanism_t *mech, crypto_key_t *key,
267     crypto_data_t *data, crypto_ctx_template_t tmpl, crypto_data_t *signature,
268     crypto_call_req_t *crq, crypto_func_group_t fg)
269 {
270 	int error;
271 	kcf_mech_entry_t *me;
272 	kcf_provider_desc_t *pd;
273 	kcf_req_params_t params;
274 	kcf_prov_tried_t *list = NULL;
275 	kcf_ctx_template_t *ctx_tmpl;
276 	crypto_spi_ctx_template_t spi_ctx_tmpl = NULL;
277 
278 retry:
279 	/* The pd is returned held */
280 	if ((pd = kcf_get_mech_provider(mech->cm_type, &me, &error, list, fg,
281 	    CHECK_RESTRICT(crq), data->cd_length)) == NULL) {
282 		if (list != NULL)
283 			kcf_free_triedlist(list);
284 		return (error);
285 	}
286 
287 	/*
288 	 * For SW providers, check the validity of the context template
289 	 * It is very rare that the generation number mis-matches, so
290 	 * it is acceptable to fail here, and let the consumer recover by
291 	 * freeing this tmpl and create a new one for the key and new SW
292 	 * provider.
293 	 */
294 	if ((pd->pd_prov_type == CRYPTO_SW_PROVIDER) &&
295 	    ((ctx_tmpl = (kcf_ctx_template_t *)tmpl) != NULL)) {
296 		if (ctx_tmpl->ct_generation != me->me_gen_swprov) {
297 			if (list != NULL)
298 				kcf_free_triedlist(list);
299 			KCF_PROV_REFRELE(pd);
300 			return (CRYPTO_OLD_CTX_TEMPLATE);
301 		} else {
302 			spi_ctx_tmpl = ctx_tmpl->ct_prov_tmpl;
303 		}
304 	}
305 
306 	/* The fast path for SW providers. */
307 	if (CHECK_FASTPATH(crq, pd)) {
308 		crypto_mechanism_t lmech;
309 
310 		lmech = *mech;
311 		KCF_SET_PROVIDER_MECHNUM(mech->cm_type, pd, &lmech);
312 		if (fg == CRYPTO_FG_SIGN_ATOMIC)
313 			error = KCF_PROV_SIGN_ATOMIC(pd, pd->pd_sid, &lmech,
314 			    key, data, spi_ctx_tmpl, signature,
315 			    KCF_SWFP_RHNDL(crq));
316 		else
317 			error = KCF_PROV_SIGN_RECOVER_ATOMIC(pd, pd->pd_sid,
318 			    &lmech, key, data, spi_ctx_tmpl, signature,
319 			    KCF_SWFP_RHNDL(crq));
320 		KCF_PROV_INCRSTATS(pd, error);
321 	} else {
322 		kcf_op_type_t op = ((fg == CRYPTO_FG_SIGN_ATOMIC) ?
323 		    KCF_OP_ATOMIC : KCF_OP_SIGN_RECOVER_ATOMIC);
324 
325 		KCF_WRAP_SIGN_OPS_PARAMS(&params, op, pd->pd_sid,
326 		    mech, key, data, signature, spi_ctx_tmpl);
327 
328 		/* no crypto context to carry between multiple parts. */
329 		error = kcf_submit_request(pd, NULL, crq, &params, B_FALSE);
330 	}
331 
332 	if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED &&
333 	    IS_RECOVERABLE(error)) {
334 		/* Add pd to the linked list of providers tried. */
335 		if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL)
336 			goto retry;
337 	}
338 
339 	if (list != NULL)
340 		kcf_free_triedlist(list);
341 
342 	KCF_PROV_REFRELE(pd);
343 	return (error);
344 }
345 
346 int
347 crypto_sign(crypto_mechanism_t *mech, crypto_key_t *key, crypto_data_t *data,
348     crypto_ctx_template_t tmpl, crypto_data_t *signature,
349     crypto_call_req_t *crq)
350 {
351 	return (sign_sr_atomic_common(mech, key, data, tmpl, signature, crq,
352 	    CRYPTO_FG_SIGN_ATOMIC));
353 }
354 
355 int
356 crypto_sign_recover_prov(crypto_provider_t provider, crypto_session_id_t sid,
357     crypto_mechanism_t *mech, crypto_key_t *key, crypto_data_t *data,
358     crypto_ctx_template_t tmpl, crypto_data_t *signature,
359     crypto_call_req_t *crq)
360 {
361 	kcf_req_params_t params;
362 	kcf_provider_desc_t *pd = provider;
363 	kcf_provider_desc_t *real_provider = pd;
364 	int rv;
365 
366 	ASSERT(KCF_PROV_REFHELD(pd));
367 
368 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
369 		rv = kcf_get_hardware_provider(mech->cm_type,
370 		    CRYPTO_MECH_INVALID, CHECK_RESTRICT(crq), pd,
371 		    &real_provider, CRYPTO_FG_SIGN_RECOVER_ATOMIC);
372 
373 		if (rv != CRYPTO_SUCCESS)
374 			return (rv);
375 	}
376 	KCF_WRAP_SIGN_OPS_PARAMS(&params, KCF_OP_SIGN_RECOVER_ATOMIC, sid, mech,
377 	    key, data, signature, tmpl);
378 	rv = kcf_submit_request(real_provider, NULL, crq, &params, B_FALSE);
379 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
380 		KCF_PROV_REFRELE(real_provider);
381 
382 	return (rv);
383 }
384 
385 int
386 crypto_sign_recover(crypto_mechanism_t *mech, crypto_key_t *key,
387     crypto_data_t *data, crypto_ctx_template_t tmpl, crypto_data_t *signature,
388     crypto_call_req_t *crq)
389 {
390 	return (sign_sr_atomic_common(mech, key, data, tmpl, signature, crq,
391 	    CRYPTO_FG_SIGN_RECOVER_ATOMIC));
392 }
393 
394 int
395 crypto_sign_recover_init_prov(crypto_provider_t provider,
396     crypto_session_id_t sid, crypto_mechanism_t *mech, crypto_key_t *key,
397     crypto_ctx_template_t tmpl, crypto_context_t *ctxp, crypto_call_req_t *crq)
398 {
399 	int rv;
400 	crypto_ctx_t *ctx;
401 	kcf_req_params_t params;
402 	kcf_provider_desc_t *pd = provider;
403 	kcf_provider_desc_t *real_provider = pd;
404 
405 	ASSERT(KCF_PROV_REFHELD(pd));
406 
407 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) {
408 		rv = kcf_get_hardware_provider(mech->cm_type,
409 		    CRYPTO_MECH_INVALID, CHECK_RESTRICT(crq), pd,
410 		    &real_provider, CRYPTO_FG_SIGN_RECOVER);
411 
412 		if (rv != CRYPTO_SUCCESS)
413 			return (rv);
414 	}
415 
416 	/* Allocate and initialize the canonical context */
417 	if ((ctx = kcf_new_ctx(crq, real_provider, sid)) == NULL) {
418 		if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
419 			KCF_PROV_REFRELE(real_provider);
420 		return (CRYPTO_HOST_MEMORY);
421 	}
422 
423 	KCF_WRAP_SIGN_OPS_PARAMS(&params, KCF_OP_SIGN_RECOVER_INIT, sid, mech,
424 	    key, NULL, NULL, tmpl);
425 	rv = kcf_submit_request(real_provider, ctx, crq, &params, B_FALSE);
426 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
427 		KCF_PROV_REFRELE(real_provider);
428 
429 	if ((rv == CRYPTO_SUCCESS) || (rv == CRYPTO_QUEUED))
430 		*ctxp = (crypto_context_t)ctx;
431 	else {
432 		/* Release the hold done in kcf_new_ctx(). */
433 		KCF_CONTEXT_REFRELE((kcf_context_t *)ctx->cc_framework_private);
434 	}
435 
436 	return (rv);
437 }
438 
439 int
440 crypto_sign_recover_single(crypto_context_t context, crypto_data_t *data,
441     crypto_data_t *signature, crypto_call_req_t *cr)
442 {
443 	crypto_ctx_t *ctx = (crypto_ctx_t *)context;
444 	kcf_context_t *kcf_ctx;
445 	kcf_provider_desc_t *pd;
446 	int error;
447 	kcf_req_params_t params;
448 
449 	if ((ctx == NULL) ||
450 	    ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) ||
451 	    ((pd = kcf_ctx->kc_prov_desc) == NULL)) {
452 		return (CRYPTO_INVALID_CONTEXT);
453 	}
454 
455 	KCF_PROV_REFHOLD(pd);
456 	KCF_WRAP_SIGN_OPS_PARAMS(&params, KCF_OP_SIGN_RECOVER, 0, NULL,
457 	    NULL, data, signature, NULL);
458 	error = kcf_submit_request(pd, ctx, cr, &params, B_FALSE);
459 	KCF_PROV_REFRELE(pd);
460 
461 	/* Release the hold done in kcf_new_ctx() during init step. */
462 	KCF_CONTEXT_COND_RELEASE(error, kcf_ctx);
463 	return (error);
464 }
465