Skip to content

Commit

Permalink
module: icp: rip out insane crypto_req_handle_t mechanism, inline KM_…
Browse files Browse the repository at this point in the history
…SLEEP

Reviewed-by: Brian Behlendorf <[email protected]>
Signed-off-by: Ahelenia Ziemiańska <[email protected]>
Closes openzfs#12901
  • Loading branch information
nabijaczleweli authored and andrewc12 committed Sep 23, 2022
1 parent 9d699ed commit 7a16ab1
Show file tree
Hide file tree
Showing 16 changed files with 187 additions and 339 deletions.
15 changes: 5 additions & 10 deletions include/sys/crypto/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ extern "C" {
typedef void *crypto_context_t;
typedef void *crypto_ctx_template_t;

typedef struct {} crypto_call_req_t;

/*
* Returns the mechanism type corresponding to a mechanism name.
*/
Expand All @@ -48,29 +46,26 @@ extern crypto_mech_type_t crypto_mech2id(const char *name);
* Create and destroy context templates.
*/
extern int crypto_create_ctx_template(crypto_mechanism_t *mech,
crypto_key_t *key, crypto_ctx_template_t *tmpl, int kmflag);
crypto_key_t *key, crypto_ctx_template_t *tmpl);
extern void crypto_destroy_ctx_template(crypto_ctx_template_t tmpl);

/*
* Single and multi-part MAC operations.
*/
extern int crypto_mac(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 *cr);
crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_data_t *mac);
extern 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 *cr);
crypto_ctx_template_t tmpl, crypto_context_t *ctxp);
extern int crypto_mac_update(crypto_context_t ctx, crypto_data_t *data);
extern int crypto_mac_final(crypto_context_t ctx, crypto_data_t *data);

/*
* Single-part encryption/decryption operations.
*/
extern int crypto_encrypt(crypto_mechanism_t *mech, crypto_data_t *plaintext,
crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_data_t *ciphertext,
crypto_call_req_t *cr);
crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_data_t *ciphertext);
extern int crypto_decrypt(crypto_mechanism_t *mech, crypto_data_t *ciphertext,
crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_data_t *plaintext,
crypto_call_req_t *cr);
crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_data_t *plaintext);

#ifdef __cplusplus
}
Expand Down
14 changes: 4 additions & 10 deletions module/icp/algs/modes/gcm.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ gcm_mode_decrypt_contiguous_blocks(gcm_ctx_t *ctx, char *data, size_t length,
*/
if (length > 0) {
new_len = ctx->gcm_pt_buf_len + length;
new = vmem_alloc(new_len, ctx->gcm_kmflag);
new = vmem_alloc(new_len, KM_SLEEP);
if (new == NULL) {
vmem_free(ctx->gcm_pt_buf, ctx->gcm_pt_buf_len);
ctx->gcm_pt_buf = NULL;
Expand Down Expand Up @@ -654,7 +654,7 @@ gcm_init_ctx(gcm_ctx_t *gcm_ctx, char *param, size_t block_size,
}
gcm_ctx->gcm_htab_len = htab_len;
gcm_ctx->gcm_Htable =
(uint64_t *)kmem_alloc(htab_len, gcm_ctx->gcm_kmflag);
(uint64_t *)kmem_alloc(htab_len, KM_SLEEP);

if (gcm_ctx->gcm_Htable == NULL) {
return (CRYPTO_HOST_MEMORY);
Expand Down Expand Up @@ -729,7 +729,7 @@ gmac_init_ctx(gcm_ctx_t *gcm_ctx, char *param, size_t block_size,
}
gcm_ctx->gcm_htab_len = htab_len;
gcm_ctx->gcm_Htable =
(uint64_t *)kmem_alloc(htab_len, gcm_ctx->gcm_kmflag);
(uint64_t *)kmem_alloc(htab_len, KM_SLEEP);

if (gcm_ctx->gcm_Htable == NULL) {
return (CRYPTO_HOST_MEMORY);
Expand Down Expand Up @@ -780,12 +780,6 @@ gmac_alloc_ctx(int kmflag)
return (gcm_ctx);
}

void
gcm_set_kmflag(gcm_ctx_t *ctx, int kmflag)
{
ctx->gcm_kmflag = kmflag;
}

/* GCM implementation that contains the fastest methods */
static gcm_impl_ops_t gcm_fastest_impl = {
.name = "fastest"
Expand Down Expand Up @@ -1212,7 +1206,7 @@ gcm_mode_encrypt_contiguous_blocks_avx(gcm_ctx_t *ctx, char *data,

/* Allocate a buffer to encrypt to if there is enough input. */
if (bleft >= GCM_AVX_MIN_ENCRYPT_BYTES) {
ct_buf = vmem_alloc(chunk_size, ctx->gcm_kmflag);
ct_buf = vmem_alloc(chunk_size, KM_SLEEP);
if (ct_buf == NULL) {
return (CRYPTO_HOST_MEMORY);
}
Expand Down
22 changes: 6 additions & 16 deletions module/icp/api/kcf_cipher.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
* tmpl: a crypto_ctx_template_t, opaque template of a context of an
* encryption with the 'mech' using 'key'. 'tmpl' is created by
* a previous call to crypto_create_ctx_template().
* cr: crypto_call_req_t calling conditions and call back info.
*
* Description:
* Asynchronously submits a request for, or synchronously performs a
Expand All @@ -62,16 +61,12 @@
* message.
* Relies on the KCF scheduler to pick a provider.
*
* Context:
* Process or interrupt, according to the semantics dictated by the 'cr'.
*
* Returns:
* See comment in the beginning of the file.
*/
int
crypto_encrypt(crypto_mechanism_t *mech, crypto_data_t *plaintext,
crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_data_t *ciphertext,
crypto_call_req_t *crq)
crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_data_t *ciphertext)
{
int error;
kcf_mech_entry_t *me;
Expand All @@ -95,12 +90,12 @@ crypto_encrypt(crypto_mechanism_t *mech, crypto_data_t *plaintext,
crypto_mechanism_t lmech = *mech;
KCF_SET_PROVIDER_MECHNUM(mech->cm_type, pd, &lmech);
error = KCF_PROV_ENCRYPT_ATOMIC(pd, pd->pd_sid, &lmech, key,
plaintext, ciphertext, spi_ctx_tmpl, KCF_SWFP_RHNDL(crq));
plaintext, ciphertext, spi_ctx_tmpl);
KCF_PROV_INCRSTATS(pd, error);

if (error != CRYPTO_SUCCESS && IS_RECOVERABLE(error)) {
/* Add pd to the linked list of providers tried. */
if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL)
if (kcf_insert_triedlist(&list, pd, KM_SLEEP) != NULL)
goto retry;
}

Expand Down Expand Up @@ -129,7 +124,6 @@ crypto_encrypt(crypto_mechanism_t *mech, crypto_data_t *plaintext,
* tmpl: a crypto_ctx_template_t, opaque template of a context of an
* encryption with the 'mech' using 'key'. 'tmpl' is created by
* a previous call to crypto_create_ctx_template().
* cr: crypto_call_req_t calling conditions and call back info.
*
* Description:
* Asynchronously submits a request for, or synchronously performs a
Expand All @@ -139,16 +133,12 @@ crypto_encrypt(crypto_mechanism_t *mech, crypto_data_t *plaintext,
* message.
* Relies on the KCF scheduler to choose a provider.
*
* Context:
* Process or interrupt, according to the semantics dictated by the 'cr'.
*
* Returns:
* See comment in the beginning of the file.
*/
int
crypto_decrypt(crypto_mechanism_t *mech, crypto_data_t *ciphertext,
crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_data_t *plaintext,
crypto_call_req_t *crq)
crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_data_t *plaintext)
{
int error;
kcf_mech_entry_t *me;
Expand All @@ -173,12 +163,12 @@ crypto_decrypt(crypto_mechanism_t *mech, crypto_data_t *ciphertext,
KCF_SET_PROVIDER_MECHNUM(mech->cm_type, pd, &lmech);

error = KCF_PROV_DECRYPT_ATOMIC(pd, pd->pd_sid, &lmech, key,
ciphertext, plaintext, spi_ctx_tmpl, KCF_SWFP_RHNDL(crq));
ciphertext, plaintext, spi_ctx_tmpl);
KCF_PROV_INCRSTATS(pd, error);

if (error != CRYPTO_SUCCESS && IS_RECOVERABLE(error)) {
/* Add pd to the linked list of providers tried. */
if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL)
if (kcf_insert_triedlist(&list, pd, KM_SLEEP) != NULL)
goto retry;
}

Expand Down
7 changes: 3 additions & 4 deletions module/icp/api/kcf_ctxops.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
* ptmpl: a storage for the opaque crypto_ctx_template_t, allocated and
* initialized by the software provider this routine is
* dispatched to.
* kmflag: KM_SLEEP/KM_NOSLEEP mem. alloc. flag.
*
* Description:
* Redirects the call to the software provider of the specified
Expand All @@ -69,7 +68,7 @@
*/
int
crypto_create_ctx_template(crypto_mechanism_t *mech, crypto_key_t *key,
crypto_ctx_template_t *ptmpl, int kmflag)
crypto_ctx_template_t *ptmpl)
{
int error;
kcf_mech_entry_t *me;
Expand All @@ -90,7 +89,7 @@ crypto_create_ctx_template(crypto_mechanism_t *mech, crypto_key_t *key,
return (error);

if ((ctx_tmpl = (kcf_ctx_template_t *)kmem_alloc(
sizeof (kcf_ctx_template_t), kmflag)) == NULL) {
sizeof (kcf_ctx_template_t), KM_SLEEP)) == NULL) {
KCF_PROV_REFRELE(pd);
return (CRYPTO_HOST_MEMORY);
}
Expand All @@ -101,7 +100,7 @@ crypto_create_ctx_template(crypto_mechanism_t *mech, crypto_key_t *key,
prov_mech.cm_param_len = mech->cm_param_len;

error = KCF_PROV_CREATE_CTX_TEMPLATE(pd, &prov_mech, key,
&(ctx_tmpl->ct_prov_tmpl), &(ctx_tmpl->ct_size), KCF_RHNDL(kmflag));
&(ctx_tmpl->ct_prov_tmpl), &(ctx_tmpl->ct_size));

if (error == CRYPTO_SUCCESS) {
*ptmpl = ctx_tmpl;
Expand Down
39 changes: 11 additions & 28 deletions module/icp/api/kcf_mac.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
* tmpl: a crypto_ctx_template_t, opaque template of a context of a
* MAC with the 'mech' using 'key'. 'tmpl' is created by
* a previous call to crypto_create_ctx_template().
* cr: crypto_call_req_t calling conditions and call back info.
*
* Description:
* Asynchronously submits a request for, or synchronously performs a
Expand All @@ -78,16 +77,12 @@
* authentication code.
* Relies on the KCF scheduler to choose a provider.
*
* Context:
* Process or interrupt, according to the semantics dictated by the 'crq'.
*
* Returns:
* See comment in the beginning of the file.
*/
int
crypto_mac(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)
crypto_key_t *key, crypto_ctx_template_t tmpl, crypto_data_t *mac)
{
int error;
kcf_mech_entry_t *me;
Expand All @@ -111,12 +106,12 @@ crypto_mac(crypto_mechanism_t *mech, crypto_data_t *data,
crypto_mechanism_t lmech = *mech;
KCF_SET_PROVIDER_MECHNUM(mech->cm_type, pd, &lmech);
error = KCF_PROV_MAC_ATOMIC(pd, pd->pd_sid, &lmech, key, data,
mac, spi_ctx_tmpl, KCF_SWFP_RHNDL(crq));
mac, spi_ctx_tmpl);
KCF_PROV_INCRSTATS(pd, error);

if (error != CRYPTO_SUCCESS && IS_RECOVERABLE(error)) {
/* Add pd to the linked list of providers tried. */
if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL)
if (kcf_insert_triedlist(&list, pd, KM_SLEEP) != NULL)
goto retry;
}

Expand All @@ -143,7 +138,6 @@ crypto_mac(crypto_mechanism_t *mech, crypto_data_t *data,
* MAC with the 'mech' using 'key'. 'tmpl' is created by
* a previous call to crypto_create_ctx_template().
* ctxp: Pointer to a crypto_context_t.
* cr: crypto_call_req_t calling conditions and call back info.
*
* Description:
* Asynchronously submits a request for, or synchronously performs the
Expand All @@ -156,16 +150,13 @@ crypto_mac(crypto_mechanism_t *mech, crypto_data_t *data,
* The caller should hold a reference on the specified provider
* descriptor before calling this function.
*
* Context:
* Process or interrupt, according to the semantics dictated by the 'cr'.
*
* Returns:
* See comment in the beginning of the file.
*/
static int
crypto_mac_init_prov(kcf_provider_desc_t *pd,
crypto_mechanism_t *mech, crypto_key_t *key, crypto_spi_ctx_template_t tmpl,
crypto_context_t *ctxp, crypto_call_req_t *crq)
crypto_context_t *ctxp)
{
int rv;
crypto_ctx_t *ctx;
Expand All @@ -174,13 +165,12 @@ crypto_mac_init_prov(kcf_provider_desc_t *pd,
ASSERT(KCF_PROV_REFHELD(pd));

/* Allocate and initialize the canonical context */
if ((ctx = kcf_new_ctx(crq, real_provider)) == NULL)
if ((ctx = kcf_new_ctx(real_provider)) == NULL)
return (CRYPTO_HOST_MEMORY);

crypto_mechanism_t lmech = *mech;
KCF_SET_PROVIDER_MECHNUM(mech->cm_type, real_provider, &lmech);
rv = KCF_PROV_MAC_INIT(real_provider, ctx, &lmech, key, tmpl,
KCF_SWFP_RHNDL(crq));
rv = KCF_PROV_MAC_INIT(real_provider, ctx, &lmech, key, tmpl);
KCF_PROV_INCRSTATS(pd, rv);

if (rv == CRYPTO_SUCCESS)
Expand All @@ -200,8 +190,7 @@ crypto_mac_init_prov(kcf_provider_desc_t *pd,
*/
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)
crypto_ctx_template_t tmpl, crypto_context_t *ctxp)
{
int error;
kcf_mech_entry_t *me;
Expand Down Expand Up @@ -230,10 +219,10 @@ crypto_mac_init(crypto_mechanism_t *mech, crypto_key_t *key,
spi_ctx_tmpl = ctx_tmpl->ct_prov_tmpl;

error = crypto_mac_init_prov(pd, mech, key,
spi_ctx_tmpl, ctxp, crq);
spi_ctx_tmpl, ctxp);
if (error != CRYPTO_SUCCESS && IS_RECOVERABLE(error)) {
/* Add pd to the linked list of providers tried. */
if (kcf_insert_triedlist(&list, pd, KCF_KMFLAG(crq)) != NULL)
if (kcf_insert_triedlist(&list, pd, KM_SLEEP) != NULL)
goto retry;
}

Expand All @@ -254,9 +243,6 @@ crypto_mac_init(crypto_mechanism_t *mech, crypto_key_t *key,
* Description:
* Synchronously performs a part of a MAC operation.
*
* Context:
* Process or interrupt, according to the semantics dictated by the 'cr'.
*
* Returns:
* See comment in the beginning of the file.
*/
Expand All @@ -273,7 +259,7 @@ crypto_mac_update(crypto_context_t context, crypto_data_t *data)
return (CRYPTO_INVALID_CONTEXT);
}

int rv = KCF_PROV_MAC_UPDATE(pd, ctx, data, NULL);
int rv = KCF_PROV_MAC_UPDATE(pd, ctx, data);
KCF_PROV_INCRSTATS(pd, rv);
return (rv);
}
Expand All @@ -288,9 +274,6 @@ crypto_mac_update(crypto_context_t context, crypto_data_t *data)
* Description:
* Synchronously performs a part of a message authentication operation.
*
* Context:
* Process or interrupt, according to the semantics dictated by the 'cr'.
*
* Returns:
* See comment in the beginning of the file.
*/
Expand All @@ -307,7 +290,7 @@ crypto_mac_final(crypto_context_t context, crypto_data_t *mac)
return (CRYPTO_INVALID_CONTEXT);
}

int rv = KCF_PROV_MAC_FINAL(pd, ctx, mac, NULL);
int rv = KCF_PROV_MAC_FINAL(pd, ctx, mac);
KCF_PROV_INCRSTATS(pd, rv);

/* Release the hold done in kcf_new_ctx() during init step. */
Expand Down
6 changes: 2 additions & 4 deletions module/icp/core/kcf_sched.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,17 @@

/* kmem caches used by the scheduler */
static kmem_cache_t *kcf_context_cache;
ulong_t kcf_swprov_hndl = 0;

/*
* Create a new context.
*/
crypto_ctx_t *
kcf_new_ctx(crypto_call_req_t *crq, kcf_provider_desc_t *pd)
kcf_new_ctx(kcf_provider_desc_t *pd)
{
crypto_ctx_t *ctx;
kcf_context_t *kcf_ctx;

kcf_ctx = kmem_cache_alloc(kcf_context_cache,
(crq == NULL) ? KM_SLEEP : KM_NOSLEEP);
kcf_ctx = kmem_cache_alloc(kcf_context_cache, KM_SLEEP);
if (kcf_ctx == NULL)
return (NULL);

Expand Down
Loading

0 comments on commit 7a16ab1

Please sign in to comment.