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 2007 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(¶ms, KCF_OP_INIT, sid, mech, 78 key, NULL, NULL, tmpl); 79 rv = kcf_submit_request(real_provider, ctx, crq, ¶ms, 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_WRAP_SIGN_OPS_PARAMS(¶ms, KCF_OP_SINGLE, 0, NULL, 165 NULL, data, signature, NULL); 166 error = kcf_submit_request(pd, ctx, cr, ¶ms, B_FALSE); 167 168 /* Release the hold done in kcf_new_ctx() during init step. */ 169 KCF_CONTEXT_COND_RELEASE(error, kcf_ctx); 170 return (error); 171 } 172 173 /* 174 * See comments for crypto_digest_update(). 175 */ 176 int 177 crypto_sign_update(crypto_context_t context, crypto_data_t *data, 178 crypto_call_req_t *cr) 179 { 180 crypto_ctx_t *ctx = (crypto_ctx_t *)context; 181 kcf_context_t *kcf_ctx; 182 kcf_provider_desc_t *pd; 183 kcf_req_params_t params; 184 int rv; 185 186 if ((ctx == NULL) || 187 ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) || 188 ((pd = kcf_ctx->kc_prov_desc) == NULL)) { 189 return (CRYPTO_INVALID_CONTEXT); 190 } 191 192 ASSERT(pd->pd_prov_type != CRYPTO_LOGICAL_PROVIDER); 193 KCF_WRAP_SIGN_OPS_PARAMS(¶ms, KCF_OP_UPDATE, ctx->cc_session, NULL, 194 NULL, data, NULL, NULL); 195 rv = kcf_submit_request(pd, ctx, cr, ¶ms, B_FALSE); 196 197 return (rv); 198 } 199 200 /* 201 * See comments for crypto_digest_final(). 202 */ 203 int 204 crypto_sign_final(crypto_context_t context, crypto_data_t *signature, 205 crypto_call_req_t *cr) 206 { 207 crypto_ctx_t *ctx = (crypto_ctx_t *)context; 208 kcf_context_t *kcf_ctx; 209 kcf_provider_desc_t *pd; 210 int rv; 211 kcf_req_params_t params; 212 213 if ((ctx == NULL) || 214 ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) || 215 ((pd = kcf_ctx->kc_prov_desc) == NULL)) { 216 return (CRYPTO_INVALID_CONTEXT); 217 } 218 219 ASSERT(pd->pd_prov_type != CRYPTO_LOGICAL_PROVIDER); 220 KCF_WRAP_SIGN_OPS_PARAMS(¶ms, KCF_OP_FINAL, ctx->cc_session, NULL, 221 NULL, NULL, signature, NULL); 222 rv = kcf_submit_request(pd, ctx, cr, ¶ms, B_FALSE); 223 224 /* Release the hold done in kcf_new_ctx() during init step. */ 225 KCF_CONTEXT_COND_RELEASE(rv, kcf_ctx); 226 return (rv); 227 } 228 229 int 230 crypto_sign_prov(crypto_provider_t provider, crypto_session_id_t sid, 231 crypto_mechanism_t *mech, crypto_key_t *key, crypto_data_t *data, 232 crypto_ctx_template_t tmpl, crypto_data_t *signature, 233 crypto_call_req_t *crq) 234 { 235 kcf_req_params_t params; 236 kcf_provider_desc_t *pd = provider; 237 kcf_provider_desc_t *real_provider = pd; 238 int rv; 239 240 ASSERT(KCF_PROV_REFHELD(pd)); 241 242 if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) { 243 rv = kcf_get_hardware_provider(mech->cm_type, 244 CRYPTO_MECH_INVALID, CHECK_RESTRICT(crq), 245 pd, &real_provider, CRYPTO_FG_SIGN_ATOMIC); 246 247 if (rv != CRYPTO_SUCCESS) 248 return (rv); 249 } 250 KCF_WRAP_SIGN_OPS_PARAMS(¶ms, KCF_OP_ATOMIC, sid, mech, 251 key, data, signature, tmpl); 252 rv = kcf_submit_request(real_provider, NULL, crq, ¶ms, B_FALSE); 253 if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) 254 KCF_PROV_REFRELE(real_provider); 255 256 return (rv); 257 } 258 259 static int 260 sign_sr_atomic_common(crypto_mechanism_t *mech, crypto_key_t *key, 261 crypto_data_t *data, crypto_ctx_template_t tmpl, crypto_data_t *signature, 262 crypto_call_req_t *crq, crypto_func_group_t fg) 263 { 264 int error; 265 kcf_mech_entry_t *me; 266 kcf_provider_desc_t *pd; 267 kcf_req_params_t params; 268 kcf_prov_tried_t *list = NULL; 269 kcf_ctx_template_t *ctx_tmpl; 270 crypto_spi_ctx_template_t spi_ctx_tmpl = NULL; 271 272 retry: 273 /* The pd is returned held */ 274 if ((pd = kcf_get_mech_provider(mech->cm_type, &me, &error, list, fg, 275 CHECK_RESTRICT(crq), data->cd_length)) == NULL) { 276 if (list != NULL) 277 kcf_free_triedlist(list); 278 return (error); 279 } 280 281 /* 282 * For SW providers, check the validity of the context template 283 * It is very rare that the generation number mis-matches, so 284 * it is acceptable to fail here, and let the consumer recover by 285 * freeing this tmpl and create a new one for the key and new SW 286 * provider. 287 */ 288 if ((pd->pd_prov_type == CRYPTO_SW_PROVIDER) && 289 ((ctx_tmpl = (kcf_ctx_template_t *)tmpl) != NULL)) { 290 if (ctx_tmpl->ct_generation != me->me_gen_swprov) { 291 if (list != NULL) 292 kcf_free_triedlist(list); 293 KCF_PROV_REFRELE(pd); 294 return (CRYPTO_OLD_CTX_TEMPLATE); 295 } else { 296 spi_ctx_tmpl = ctx_tmpl->ct_prov_tmpl; 297 } 298 } 299 300 /* The fast path for SW providers. */ 301 if (CHECK_FASTPATH(crq, pd)) { 302 crypto_mechanism_t lmech; 303 304 lmech = *mech; 305 KCF_SET_PROVIDER_MECHNUM(mech->cm_type, pd, &lmech); 306 if (fg == CRYPTO_FG_SIGN_ATOMIC) 307 error = KCF_PROV_SIGN_ATOMIC(pd, pd->pd_sid, &lmech, 308 key, data, spi_ctx_tmpl, signature, 309 KCF_SWFP_RHNDL(crq)); 310 else 311 error = KCF_PROV_SIGN_RECOVER_ATOMIC(pd, pd->pd_sid, 312 &lmech, key, data, spi_ctx_tmpl, signature, 313 KCF_SWFP_RHNDL(crq)); 314 KCF_PROV_INCRSTATS(pd, error); 315 } else { 316 kcf_op_type_t op = ((fg == CRYPTO_FG_SIGN_ATOMIC) ? 317 KCF_OP_ATOMIC : KCF_OP_SIGN_RECOVER_ATOMIC); 318 319 KCF_WRAP_SIGN_OPS_PARAMS(¶ms, op, pd->pd_sid, 320 mech, key, data, signature, spi_ctx_tmpl); 321 322 /* no crypto context to carry between multiple parts. */ 323 error = kcf_submit_request(pd, NULL, crq, ¶ms, B_FALSE); 324 } 325 326 if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED && 327 IS_RECOVERABLE(error)) { 328 /* Add pd to the linked list of providers tried. */ 329 if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL) 330 goto retry; 331 } 332 333 if (list != NULL) 334 kcf_free_triedlist(list); 335 336 KCF_PROV_REFRELE(pd); 337 return (error); 338 } 339 340 int 341 crypto_sign(crypto_mechanism_t *mech, crypto_key_t *key, crypto_data_t *data, 342 crypto_ctx_template_t tmpl, crypto_data_t *signature, 343 crypto_call_req_t *crq) 344 { 345 return (sign_sr_atomic_common(mech, key, data, tmpl, signature, crq, 346 CRYPTO_FG_SIGN_ATOMIC)); 347 } 348 349 int 350 crypto_sign_recover_prov(crypto_provider_t provider, crypto_session_id_t sid, 351 crypto_mechanism_t *mech, crypto_key_t *key, crypto_data_t *data, 352 crypto_ctx_template_t tmpl, crypto_data_t *signature, 353 crypto_call_req_t *crq) 354 { 355 kcf_req_params_t params; 356 kcf_provider_desc_t *pd = provider; 357 kcf_provider_desc_t *real_provider = pd; 358 int rv; 359 360 ASSERT(KCF_PROV_REFHELD(pd)); 361 362 if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) { 363 rv = kcf_get_hardware_provider(mech->cm_type, 364 CRYPTO_MECH_INVALID, CHECK_RESTRICT(crq), pd, 365 &real_provider, CRYPTO_FG_SIGN_RECOVER_ATOMIC); 366 367 if (rv != CRYPTO_SUCCESS) 368 return (rv); 369 } 370 KCF_WRAP_SIGN_OPS_PARAMS(¶ms, KCF_OP_SIGN_RECOVER_ATOMIC, sid, mech, 371 key, data, signature, tmpl); 372 rv = kcf_submit_request(real_provider, NULL, crq, ¶ms, B_FALSE); 373 if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) 374 KCF_PROV_REFRELE(real_provider); 375 376 return (rv); 377 } 378 379 int 380 crypto_sign_recover(crypto_mechanism_t *mech, crypto_key_t *key, 381 crypto_data_t *data, crypto_ctx_template_t tmpl, crypto_data_t *signature, 382 crypto_call_req_t *crq) 383 { 384 return (sign_sr_atomic_common(mech, key, data, tmpl, signature, crq, 385 CRYPTO_FG_SIGN_RECOVER_ATOMIC)); 386 } 387 388 int 389 crypto_sign_recover_init_prov(crypto_provider_t provider, 390 crypto_session_id_t sid, crypto_mechanism_t *mech, crypto_key_t *key, 391 crypto_ctx_template_t tmpl, crypto_context_t *ctxp, crypto_call_req_t *crq) 392 { 393 int rv; 394 crypto_ctx_t *ctx; 395 kcf_req_params_t params; 396 kcf_provider_desc_t *pd = provider; 397 kcf_provider_desc_t *real_provider = pd; 398 399 ASSERT(KCF_PROV_REFHELD(pd)); 400 401 if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) { 402 rv = kcf_get_hardware_provider(mech->cm_type, 403 CRYPTO_MECH_INVALID, CHECK_RESTRICT(crq), pd, 404 &real_provider, CRYPTO_FG_SIGN_RECOVER); 405 406 if (rv != CRYPTO_SUCCESS) 407 return (rv); 408 } 409 410 /* Allocate and initialize the canonical context */ 411 if ((ctx = kcf_new_ctx(crq, real_provider, sid)) == NULL) { 412 if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) 413 KCF_PROV_REFRELE(real_provider); 414 return (CRYPTO_HOST_MEMORY); 415 } 416 417 KCF_WRAP_SIGN_OPS_PARAMS(¶ms, KCF_OP_SIGN_RECOVER_INIT, sid, mech, 418 key, NULL, NULL, tmpl); 419 rv = kcf_submit_request(real_provider, ctx, crq, ¶ms, B_FALSE); 420 if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) 421 KCF_PROV_REFRELE(real_provider); 422 423 if ((rv == CRYPTO_SUCCESS) || (rv == CRYPTO_QUEUED)) 424 *ctxp = (crypto_context_t)ctx; 425 else { 426 /* Release the hold done in kcf_new_ctx(). */ 427 KCF_CONTEXT_REFRELE((kcf_context_t *)ctx->cc_framework_private); 428 } 429 430 return (rv); 431 } 432 433 int 434 crypto_sign_recover_single(crypto_context_t context, crypto_data_t *data, 435 crypto_data_t *signature, crypto_call_req_t *cr) 436 { 437 crypto_ctx_t *ctx = (crypto_ctx_t *)context; 438 kcf_context_t *kcf_ctx; 439 kcf_provider_desc_t *pd; 440 int error; 441 kcf_req_params_t params; 442 443 if ((ctx == NULL) || 444 ((kcf_ctx = (kcf_context_t *)ctx->cc_framework_private) == NULL) || 445 ((pd = kcf_ctx->kc_prov_desc) == NULL)) { 446 return (CRYPTO_INVALID_CONTEXT); 447 } 448 449 KCF_WRAP_SIGN_OPS_PARAMS(¶ms, KCF_OP_SIGN_RECOVER, 0, NULL, 450 NULL, data, signature, NULL); 451 error = kcf_submit_request(pd, ctx, cr, ¶ms, B_FALSE); 452 453 /* Release the hold done in kcf_new_ctx() during init step. */ 454 KCF_CONTEXT_COND_RELEASE(error, kcf_ctx); 455 return (error); 456 } 457