/* * Same as crypto_digest_init_prov(), but relies on the KCF scheduler * to choose a provider. See crypto_digest_init_prov() comments for * more information. */ int crypto_digest_init(crypto_mechanism_t *mech, crypto_context_t *ctxp, crypto_call_req_t *crq) { int error; kcf_provider_desc_t *pd; kcf_prov_tried_t *list = NULL; retry: /* The pd is returned held */ if ((pd = kcf_get_mech_provider(mech->cm_type, NULL, &error, list, CRYPTO_FG_DIGEST, CHECK_RESTRICT(crq), 0)) == NULL) { if (list != NULL) kcf_free_triedlist(list); return (error); } error = crypto_digest_init_prov(pd, pd->pd_sid, mech, ctxp, crq); if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED && IS_RECOVERABLE(error)) { /* Add pd to the linked list of providers tried. */ if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL) goto retry; } if (list != NULL) kcf_free_triedlist(list); KCF_PROV_REFRELE(pd); return (error); }
int crypto_sign_init(crypto_mechanism_t *mech, crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_context_t *ctxp, crypto_call_req_t *crq) { int error; kcf_mech_entry_t *me; kcf_provider_desc_t *pd; kcf_prov_tried_t *list = NULL; kcf_ctx_template_t *ctx_tmpl; crypto_spi_ctx_template_t spi_ctx_tmpl = NULL; retry: /* The pd is returned held */ if ((pd = kcf_get_mech_provider(mech->cm_type, &me, &error, list, CRYPTO_FG_SIGN, CHECK_RESTRICT(crq), 0)) == NULL) { if (list != NULL) kcf_free_triedlist(list); return (error); } /* * For SW providers, check the validity of the context template * It is very rare that the generation number mis-matches, so * it is acceptable to fail here, and let the consumer recover by * freeing this tmpl and create a new one for the key and new SW * provider. */ if ((pd->pd_prov_type == CRYPTO_SW_PROVIDER) && ((ctx_tmpl = (kcf_ctx_template_t *)tmpl) != NULL)) { if (ctx_tmpl->ct_generation != me->me_gen_swprov) { if (list != NULL) kcf_free_triedlist(list); KCF_PROV_REFRELE(pd); return (CRYPTO_OLD_CTX_TEMPLATE); } else { spi_ctx_tmpl = ctx_tmpl->ct_prov_tmpl; } } error = crypto_sign_init_prov(pd, pd->pd_sid, mech, key, spi_ctx_tmpl, ctxp, crq); if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED && IS_RECOVERABLE(error)) { /* Add pd to the linked list of providers tried. */ if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL) goto retry; } if (list != NULL) kcf_free_triedlist(list); KCF_PROV_REFRELE(pd); return (error); }
/* * Same as crypto_digest_prov(), but relies on the KCF scheduler to * choose a provider. See crypto_digest_prov() comments for more information. */ int crypto_digest(crypto_mechanism_t *mech, crypto_data_t *data, crypto_data_t *digest, crypto_call_req_t *crq) { int error; kcf_provider_desc_t *pd; kcf_req_params_t params; kcf_prov_tried_t *list = NULL; retry: /* The pd is returned held */ if ((pd = kcf_get_mech_provider(mech->cm_type, NULL, &error, list, CRYPTO_FG_DIGEST_ATOMIC, CHECK_RESTRICT(crq), data->cd_length)) == NULL) { if (list != NULL) kcf_free_triedlist(list); return (error); } /* The fast path for SW providers. */ if (CHECK_FASTPATH(crq, pd)) { crypto_mechanism_t lmech; lmech = *mech; KCF_SET_PROVIDER_MECHNUM(mech->cm_type, pd, &lmech); error = KCF_PROV_DIGEST_ATOMIC(pd, pd->pd_sid, &lmech, data, digest, KCF_SWFP_RHNDL(crq)); KCF_PROV_INCRSTATS(pd, error); } else { KCF_WRAP_DIGEST_OPS_PARAMS(¶ms, KCF_OP_ATOMIC, pd->pd_sid, mech, NULL, data, digest); /* no crypto context to carry between multiple parts. */ error = kcf_submit_request(pd, NULL, crq, ¶ms, B_FALSE); } if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED && IS_RECOVERABLE(error)) { /* Add pd to the linked list of providers tried. */ if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL) goto retry; } if (list != NULL) kcf_free_triedlist(list); KCF_PROV_REFRELE(pd); return (error); }
kcf_dual_req_t * kcf_alloc_req(crypto_call_req_t *crq) { kcf_dual_req_t *kcr; kcr = kmem_alloc(sizeof (kcf_dual_req_t), KCF_KMFLAG(crq)); if (kcr == NULL) return (NULL); /* Copy the whole crypto_call_req struct, as it isn't persistant */ if (crq != NULL) kcr->kr_callreq = *crq; else bzero(&(kcr->kr_callreq), sizeof (crypto_call_req_t)); kcr->kr_areq = NULL; kcr->kr_saveoffset = 0; kcr->kr_savelen = 0; return (kcr); }
/* * Same as crypto_mac_init_prov(), but relies on the KCF scheduler to * choose a provider. See crypto_mac_init_prov() comments for more * information. */ int crypto_mac_init(crypto_mechanism_t *mech, crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_context_t *ctxp, crypto_call_req_t *crq) { int error; kcf_mech_entry_t *me; kcf_provider_desc_t *pd; kcf_ctx_template_t *ctx_tmpl; crypto_spi_ctx_template_t spi_ctx_tmpl = NULL; kcf_prov_tried_t *list = NULL; retry: /* The pd is returned held */ if ((pd = kcf_get_mech_provider(mech->cm_type, &me, &error, list, CRYPTO_FG_MAC, CHECK_RESTRICT(crq), 0)) == NULL) { if (list != NULL) kcf_free_triedlist(list); return (error); } /* * For SW providers, check the validity of the context template * It is very rare that the generation number mis-matches, so * is acceptable to fail here, and let the consumer recover by * freeing this tmpl and create a new one for the key and new SW * provider */ if ((pd->pd_prov_type == CRYPTO_SW_PROVIDER) && ((ctx_tmpl = (kcf_ctx_template_t *)tmpl) != NULL)) { if (ctx_tmpl->ct_generation != me->me_gen_swprov) { if (list != NULL) kcf_free_triedlist(list); KCF_PROV_REFRELE(pd); return (CRYPTO_OLD_CTX_TEMPLATE); } else { spi_ctx_tmpl = ctx_tmpl->ct_prov_tmpl; } } if (pd->pd_prov_type == CRYPTO_HW_PROVIDER && (pd->pd_flags & CRYPTO_HASH_NO_UPDATE)) { /* * The hardware provider has limited HMAC support. * So, we fallback early here to using a software provider. * * XXX - need to enhance to do the fallback later in * crypto_mac_update() if the size of accumulated input data * exceeds the maximum size digestable by hardware provider. */ error = CRYPTO_BUFFER_TOO_BIG; } else { error = crypto_mac_init_prov(pd, pd->pd_sid, mech, key, spi_ctx_tmpl, ctxp, crq); } if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED && IS_RECOVERABLE(error)) { /* Add pd to the linked list of providers tried. */ if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL) goto retry; } if (list != NULL) kcf_free_triedlist(list); KCF_PROV_REFRELE(pd); return (error); }
/* * Same as crypto_mac_verify_prov(), but relies on the KCF scheduler to choose * a provider. See crypto_mac_verify_prov() comments for more information. */ int crypto_mac_verify(crypto_mechanism_t *mech, crypto_data_t *data, crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_data_t *mac, crypto_call_req_t *crq) { int error; kcf_mech_entry_t *me; kcf_req_params_t params; kcf_provider_desc_t *pd; kcf_ctx_template_t *ctx_tmpl; crypto_spi_ctx_template_t spi_ctx_tmpl = NULL; kcf_prov_tried_t *list = NULL; retry: /* The pd is returned held */ if ((pd = kcf_get_mech_provider(mech->cm_type, &me, &error, list, CRYPTO_FG_MAC_ATOMIC, CHECK_RESTRICT(crq), data->cd_length)) == NULL) { if (list != NULL) kcf_free_triedlist(list); return (error); } /* * For SW providers, check the validity of the context template * It is very rare that the generation number mis-matches, so * is acceptable to fail here, and let the consumer recover by * freeing this tmpl and create a new one for the key and new SW * provider */ if ((pd->pd_prov_type == CRYPTO_SW_PROVIDER) && ((ctx_tmpl = (kcf_ctx_template_t *)tmpl) != NULL)) { if (ctx_tmpl->ct_generation != me->me_gen_swprov) { if (list != NULL) kcf_free_triedlist(list); KCF_PROV_REFRELE(pd); return (CRYPTO_OLD_CTX_TEMPLATE); } else { spi_ctx_tmpl = ctx_tmpl->ct_prov_tmpl; } } /* The fast path for SW providers. */ if (CHECK_FASTPATH(crq, pd)) { crypto_mechanism_t lmech; lmech = *mech; KCF_SET_PROVIDER_MECHNUM(mech->cm_type, pd, &lmech); error = KCF_PROV_MAC_VERIFY_ATOMIC(pd, pd->pd_sid, &lmech, key, data, mac, spi_ctx_tmpl, KCF_SWFP_RHNDL(crq)); KCF_PROV_INCRSTATS(pd, error); } else { if (pd->pd_prov_type == CRYPTO_HW_PROVIDER && (pd->pd_flags & CRYPTO_HASH_NO_UPDATE) && (data->cd_length > pd->pd_hash_limit)) { /* see comments in crypto_mac() */ error = CRYPTO_BUFFER_TOO_BIG; } else { KCF_WRAP_MAC_OPS_PARAMS(¶ms, KCF_OP_MAC_VERIFY_ATOMIC, pd->pd_sid, mech, key, data, mac, spi_ctx_tmpl); error = kcf_submit_request(pd, NULL, crq, ¶ms, KCF_ISDUALREQ(crq)); } } if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED && IS_RECOVERABLE(error)) { /* Add pd to the linked list of providers tried. */ if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL) goto retry; } if (list != NULL) kcf_free_triedlist(list); KCF_PROV_REFRELE(pd); return (error); }
static int sign_sr_atomic_common(crypto_mechanism_t *mech, crypto_key_t *key, crypto_data_t *data, crypto_ctx_template_t tmpl, crypto_data_t *signature, crypto_call_req_t *crq, crypto_func_group_t fg) { int error; kcf_mech_entry_t *me; kcf_provider_desc_t *pd; kcf_req_params_t params; kcf_prov_tried_t *list = NULL; kcf_ctx_template_t *ctx_tmpl; crypto_spi_ctx_template_t spi_ctx_tmpl = NULL; retry: /* The pd is returned held */ if ((pd = kcf_get_mech_provider(mech->cm_type, &me, &error, list, fg, CHECK_RESTRICT(crq), data->cd_length)) == NULL) { if (list != NULL) kcf_free_triedlist(list); return (error); } /* * For SW providers, check the validity of the context template * It is very rare that the generation number mis-matches, so * it is acceptable to fail here, and let the consumer recover by * freeing this tmpl and create a new one for the key and new SW * provider. */ if ((pd->pd_prov_type == CRYPTO_SW_PROVIDER) && ((ctx_tmpl = (kcf_ctx_template_t *)tmpl) != NULL)) { if (ctx_tmpl->ct_generation != me->me_gen_swprov) { if (list != NULL) kcf_free_triedlist(list); KCF_PROV_REFRELE(pd); return (CRYPTO_OLD_CTX_TEMPLATE); } else { spi_ctx_tmpl = ctx_tmpl->ct_prov_tmpl; } } /* The fast path for SW providers. */ if (CHECK_FASTPATH(crq, pd)) { crypto_mechanism_t lmech; lmech = *mech; KCF_SET_PROVIDER_MECHNUM(mech->cm_type, pd, &lmech); if (fg == CRYPTO_FG_SIGN_ATOMIC) error = KCF_PROV_SIGN_ATOMIC(pd, pd->pd_sid, &lmech, key, data, spi_ctx_tmpl, signature, KCF_SWFP_RHNDL(crq)); else error = KCF_PROV_SIGN_RECOVER_ATOMIC(pd, pd->pd_sid, &lmech, key, data, spi_ctx_tmpl, signature, KCF_SWFP_RHNDL(crq)); KCF_PROV_INCRSTATS(pd, error); } else { kcf_op_type_t op = ((fg == CRYPTO_FG_SIGN_ATOMIC) ? KCF_OP_ATOMIC : KCF_OP_SIGN_RECOVER_ATOMIC); KCF_WRAP_SIGN_OPS_PARAMS(¶ms, op, pd->pd_sid, mech, key, data, signature, spi_ctx_tmpl); /* no crypto context to carry between multiple parts. */ error = kcf_submit_request(pd, NULL, crq, ¶ms, B_FALSE); } if (error != CRYPTO_SUCCESS && error != CRYPTO_QUEUED && IS_RECOVERABLE(error)) { /* Add pd to the linked list of providers tried. */ if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL) goto retry; } if (list != NULL) kcf_free_triedlist(list); KCF_PROV_REFRELE(pd); return (error); }