mirror of
https://github.com/apache/nuttx.git
synced 2026-05-23 14:58:13 +08:00
crypto/cryptodev: optimize without dynamic memory in crypto process
Replace dynamic memory allocation with stack-based variables in cryptodev_op(). This eliminates kmm_malloc/kmm_free overhead and simplifies error handling by removing the need for goto bail cleanup paths. Signed-off-by: makejian <makejian@xiaomi.com>
This commit is contained in:
@@ -624,65 +624,6 @@ migrate:
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Release a set of crypto descriptors. */
|
||||
|
||||
void crypto_freereq(FAR struct cryptop *crp)
|
||||
{
|
||||
FAR struct cryptodesc *crd;
|
||||
|
||||
if (crp == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
nxmutex_lock(&g_crypto_lock);
|
||||
|
||||
while ((crd = crp->crp_desc) != NULL)
|
||||
{
|
||||
crp->crp_desc = crd->crd_next;
|
||||
kmm_free(crd);
|
||||
}
|
||||
|
||||
kmm_free(crp);
|
||||
nxmutex_unlock(&g_crypto_lock);
|
||||
}
|
||||
|
||||
/* Acquire a set of crypto descriptors. */
|
||||
|
||||
FAR struct cryptop *crypto_getreq(int num)
|
||||
{
|
||||
FAR struct cryptodesc *crd;
|
||||
FAR struct cryptop *crp;
|
||||
|
||||
nxmutex_lock(&g_crypto_lock);
|
||||
|
||||
crp = kmm_malloc(sizeof(struct cryptop));
|
||||
if (crp == NULL)
|
||||
{
|
||||
nxmutex_unlock(&g_crypto_lock);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bzero(crp, sizeof(struct cryptop));
|
||||
|
||||
while (num--)
|
||||
{
|
||||
crd = kmm_calloc(1, sizeof(struct cryptodesc));
|
||||
if (crd == NULL)
|
||||
{
|
||||
nxmutex_unlock(&g_crypto_lock);
|
||||
crypto_freereq(crp);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
crd->crd_next = crp->crp_desc;
|
||||
crp->crp_desc = crd;
|
||||
}
|
||||
|
||||
nxmutex_unlock(&g_crypto_lock);
|
||||
return crp;
|
||||
}
|
||||
|
||||
int crypto_getfeat(FAR int *featp)
|
||||
{
|
||||
extern int cryptodevallowsoft;
|
||||
|
||||
+56
-118
@@ -390,172 +390,110 @@ bail:
|
||||
static int cryptodev_op(FAR struct csession *cse,
|
||||
FAR struct crypt_op *cop)
|
||||
{
|
||||
FAR struct cryptop *crp = NULL;
|
||||
FAR struct cryptodesc *crde = NULL;
|
||||
FAR struct cryptodesc *crda = NULL;
|
||||
struct cryptop crp;
|
||||
struct cryptodesc crda;
|
||||
struct cryptodesc crde;
|
||||
int error = OK;
|
||||
uint32_t hid;
|
||||
|
||||
/* number of requests, not logical and */
|
||||
|
||||
crp = crypto_getreq(cse->txform + cse->thash);
|
||||
if (crp == NULL)
|
||||
{
|
||||
error = -ENOMEM;
|
||||
goto bail;
|
||||
}
|
||||
|
||||
bzero(&crp, sizeof(struct cryptop));
|
||||
bzero(&crda, sizeof(struct cryptodesc));
|
||||
bzero(&crde, sizeof(struct cryptodesc));
|
||||
if (cse->thash)
|
||||
{
|
||||
crda = crp->crp_desc;
|
||||
if (cse->txform)
|
||||
crde = crda->crd_next;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (cse->txform)
|
||||
{
|
||||
crde = crp->crp_desc;
|
||||
}
|
||||
else
|
||||
{
|
||||
error = -EINVAL;
|
||||
goto bail;
|
||||
}
|
||||
}
|
||||
crp.crp_desc = &crda;
|
||||
crda.crd_skip = 0;
|
||||
crda.crd_len = cop->len;
|
||||
crda.crd_inject = 0;
|
||||
|
||||
if (crda)
|
||||
{
|
||||
crda->crd_skip = 0;
|
||||
crda->crd_len = cop->len;
|
||||
crda->crd_inject = 0;
|
||||
|
||||
crda->crd_alg = cse->mac;
|
||||
crda->crd_key = cse->mackey;
|
||||
crda->crd_klen = cse->mackeylen * 8;
|
||||
crda.crd_alg = cse->mac;
|
||||
crda.crd_key = cse->mackey;
|
||||
crda.crd_klen = cse->mackeylen * 8;
|
||||
if (cop->flags & COP_FLAG_UPDATE)
|
||||
{
|
||||
crda->crd_flags |= CRD_F_UPDATE;
|
||||
crda.crd_flags |= CRD_F_UPDATE;
|
||||
}
|
||||
else
|
||||
{
|
||||
crda->crd_flags &= ~CRD_F_UPDATE;
|
||||
crda.crd_flags &= ~CRD_F_UPDATE;
|
||||
}
|
||||
}
|
||||
|
||||
if (crde)
|
||||
if (cse->txform)
|
||||
{
|
||||
if (cse->thash)
|
||||
{
|
||||
crda.crd_next = &crde;
|
||||
}
|
||||
else
|
||||
{
|
||||
crp.crp_desc = &crde;
|
||||
}
|
||||
|
||||
if (cop->op == COP_ENCRYPT)
|
||||
{
|
||||
crde->crd_flags |= CRD_F_ENCRYPT;
|
||||
crde.crd_flags |= CRD_F_ENCRYPT;
|
||||
}
|
||||
else
|
||||
{
|
||||
crde->crd_flags &= ~CRD_F_ENCRYPT;
|
||||
crde.crd_flags &= ~CRD_F_ENCRYPT;
|
||||
}
|
||||
|
||||
crde->crd_len = cop->len;
|
||||
crde->crd_inject = 0;
|
||||
crde->crd_alg = cse->cipher;
|
||||
crde->crd_key = cse->key;
|
||||
crde->crd_klen = cse->keylen * 8;
|
||||
crde.crd_len = cop->len;
|
||||
crde.crd_inject = 0;
|
||||
crde.crd_alg = cse->cipher;
|
||||
crde.crd_key = cse->key;
|
||||
crde.crd_klen = cse->keylen * 8;
|
||||
}
|
||||
|
||||
crp->crp_ilen = cop->len;
|
||||
crp->crp_olen = cop->olen;
|
||||
crp->crp_buf = cop->src;
|
||||
crp->crp_sid = cse->sid;
|
||||
crp->crp_opaque = cse;
|
||||
crp.crp_ilen = cop->len;
|
||||
crp.crp_olen = cop->olen;
|
||||
crp.crp_buf = cop->src;
|
||||
crp.crp_sid = cse->sid;
|
||||
crp.crp_opaque = cse;
|
||||
|
||||
if (cop->iv)
|
||||
{
|
||||
if (crde == NULL)
|
||||
{
|
||||
error = -EINVAL;
|
||||
goto bail;
|
||||
}
|
||||
|
||||
crp->crp_iv = cop->iv;
|
||||
crp->crp_ivlen = cop->ivlen;
|
||||
crp.crp_iv = cop->iv;
|
||||
crp.crp_ivlen = cop->ivlen;
|
||||
}
|
||||
|
||||
if (cop->dst)
|
||||
{
|
||||
if (crde == NULL)
|
||||
{
|
||||
error = -EINVAL;
|
||||
goto bail;
|
||||
}
|
||||
|
||||
crp->crp_dst = cop->dst;
|
||||
crp.crp_dst = cop->dst;
|
||||
}
|
||||
|
||||
if (cop->mac)
|
||||
{
|
||||
if (crda == NULL)
|
||||
{
|
||||
error = -EINVAL;
|
||||
goto bail;
|
||||
}
|
||||
|
||||
crp->crp_mac = cop->mac;
|
||||
crp.crp_mac = cop->mac;
|
||||
}
|
||||
|
||||
/* try the fast path first */
|
||||
|
||||
crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_NOQUEUE;
|
||||
hid = (crp->crp_sid >> 32) & 0xffffffff;
|
||||
if (hid >= crypto_drivers_num)
|
||||
crp.crp_flags = CRYPTO_F_IOV | CRYPTO_F_NOQUEUE;
|
||||
hid = (crp.crp_sid >> 32) & 0xffffffff;
|
||||
if (hid >= crypto_drivers_num ||
|
||||
(crypto_drivers[hid].cc_flags & CRYPTOCAP_F_SOFTWARE) ||
|
||||
crypto_drivers[hid].cc_process == NULL)
|
||||
{
|
||||
goto dispatch;
|
||||
crp.crp_flags = CRYPTO_F_IOV;
|
||||
crypto_invoke(&crp);
|
||||
}
|
||||
else
|
||||
{
|
||||
error = crypto_drivers[hid].cc_process(&crp);
|
||||
}
|
||||
|
||||
if (crypto_drivers[hid].cc_flags & CRYPTOCAP_F_SOFTWARE)
|
||||
if ((cop->flags & COP_FLAG_UPDATE) == 0)
|
||||
{
|
||||
goto dispatch;
|
||||
crde.crd_flags &= ~CRD_F_IV_EXPLICIT;
|
||||
}
|
||||
|
||||
if (crypto_drivers[hid].cc_process == NULL)
|
||||
if (!error && crp.crp_etype != 0)
|
||||
{
|
||||
goto dispatch;
|
||||
}
|
||||
|
||||
error = crypto_drivers[hid].cc_process(crp);
|
||||
if (error)
|
||||
{
|
||||
/* clear error */
|
||||
|
||||
crp->crp_etype = 0;
|
||||
goto dispatch;
|
||||
}
|
||||
|
||||
goto processed;
|
||||
dispatch:
|
||||
crp->crp_flags = CRYPTO_F_IOV;
|
||||
crypto_invoke(crp);
|
||||
processed:
|
||||
|
||||
if (crde && (cop->flags & COP_FLAG_UPDATE) == 0)
|
||||
{
|
||||
crde->crd_flags &= ~CRD_F_IV_EXPLICIT;
|
||||
}
|
||||
|
||||
if (cse->error)
|
||||
{
|
||||
error = cse->error;
|
||||
goto bail;
|
||||
}
|
||||
|
||||
if (crp->crp_etype != 0)
|
||||
{
|
||||
error = crp->crp_etype;
|
||||
goto bail;
|
||||
}
|
||||
|
||||
bail:
|
||||
if (crp)
|
||||
{
|
||||
crypto_freereq(crp);
|
||||
error = crp.crp_etype;
|
||||
}
|
||||
|
||||
return error;
|
||||
|
||||
@@ -445,10 +445,6 @@ int crypto_kinvoke(FAR struct cryptkop *);
|
||||
int crypto_getfeat(FAR int *);
|
||||
int crypto_driver_set_priv(uint32_t, FAR void *);
|
||||
FAR void *crypto_driver_get_priv(uint32_t);
|
||||
|
||||
FAR struct cryptop *crypto_getreq(int);
|
||||
void crypto_freereq(FAR struct cryptop *);
|
||||
|
||||
#ifdef CONFIG_CRYPTO_CRYPTODEV_HARDWARE
|
||||
void hwcr_init(void);
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user