From 4a6548e2d806ba26bbb4f8cc5e0dc68f2d0c6992 Mon Sep 17 00:00:00 2001 From: Andre Heinemans Date: Wed, 6 Nov 2024 13:36:47 +0100 Subject: [PATCH] crypto/se05x: Allow set_enable_pin to be NULL and fix error handling - set_enable_pin can be set to NULL when the enable pin(ENA) of the SE05x cannot be controlled - Fixed error handling in se05x_register() --- drivers/crypto/pnt/pnt_se05x_api.c | 62 +++++++++++++++--------------- drivers/crypto/se05x.c | 30 ++++++++++++--- 2 files changed, 56 insertions(+), 36 deletions(-) diff --git a/drivers/crypto/pnt/pnt_se05x_api.c b/drivers/crypto/pnt/pnt_se05x_api.c index 7339a4ab05..aef7471e06 100644 --- a/drivers/crypto/pnt/pnt_se05x_api.c +++ b/drivers/crypto/pnt/pnt_se05x_api.c @@ -77,7 +77,7 @@ static bool set_enable_pin(FAR struct se05x_dev_s *se05x, bool state) { if (se05x->config->set_enable_pin == NULL) { - return FALSE; + return TRUE; } return se05x->config->set_enable_pin(state); @@ -93,46 +93,48 @@ static bool set_enable_pin(FAR struct se05x_dev_s *se05x, bool state) int pnt_se05x_open(FAR struct se05x_dev_s *se05x) { + int ret; se05x->pnt = kmm_malloc(sizeof(struct pnt_handle)); - int ret = se05x->pnt != NULL ? 0 : -EIO; - if (ret == 0) + if (se05x->pnt == NULL) { - memset(&(se05x->pnt->session), 0, sizeof(Se05xSession_t)); - - se05x->pnt->session.pScp03_enc_key = (FAR uint8_t *)scp03_enc_key; - se05x->pnt->session.pScp03_mac_key = (FAR uint8_t *)scp03_mac_key; - se05x->pnt->session.pScp03_dek_key = (FAR uint8_t *)scp03_dek_key; - se05x->pnt->session.scp03_enc_key_len = SCP03_KEY_SIZE; - se05x->pnt->session.scp03_mac_key_len = SCP03_KEY_SIZE; - se05x->pnt->session.scp03_dek_key_len = SCP03_KEY_SIZE; - ret = set_enable_pin(se05x, true) ? 0 : -EIO; + ret = -EIO; + goto errout; } - if (ret == 0) + memset(&(se05x->pnt->session), 0, sizeof(Se05xSession_t)); + + se05x->pnt->session.pScp03_enc_key = (FAR uint8_t *)scp03_enc_key; + se05x->pnt->session.pScp03_mac_key = (FAR uint8_t *)scp03_mac_key; + se05x->pnt->session.pScp03_dek_key = (FAR uint8_t *)scp03_dek_key; + se05x->pnt->session.scp03_enc_key_len = SCP03_KEY_SIZE; + se05x->pnt->session.scp03_mac_key_len = SCP03_KEY_SIZE; + se05x->pnt->session.scp03_dek_key_len = SCP03_KEY_SIZE; + if (!set_enable_pin(se05x, true)) { - se05x->pnt->session.skip_applet_select = 0; - se05x->pnt->session.session_resume = 0; - smStatus_t status = - Se05x_API_SessionOpen(&(se05x->pnt->session), se05x); - ret = status == SM_OK ? 0 : -EIO; + ret = -EIO; + goto errout_with_alloc; } - /* if error */ - - if (ret < 0) + se05x->pnt->session.skip_applet_select = 0; + se05x->pnt->session.session_resume = 0; + if (Se05x_API_SessionOpen(&(se05x->pnt->session), se05x) != SM_OK) { - if (se05x->pnt->session.conn_context != NULL) - { - kmm_free(se05x->pnt->session.conn_context); - } - - if (se05x->pnt != NULL) - { - kmm_free(se05x->pnt); - } + ret = -EIO; + goto errout_with_alloc; } + return OK; + +errout_with_alloc: + if (se05x->pnt->session.conn_context != NULL) + { + kmm_free(se05x->pnt->session.conn_context); + } + + kmm_free(se05x->pnt); + +errout: return ret; } diff --git a/drivers/crypto/se05x.c b/drivers/crypto/se05x.c index 127ae1bfe6..6963250a8b 100644 --- a/drivers/crypto/se05x.c +++ b/drivers/crypto/se05x.c @@ -233,7 +233,8 @@ int se05x_register(FAR const char *devpath, FAR struct i2c_master_s *i2c, if (priv == NULL) { crypterr("ERROR: Failed to allocate instance\n"); - return -ENOMEM; + ret = -ENOMEM; + goto errout; } priv->config = config; @@ -241,14 +242,21 @@ int se05x_register(FAR const char *devpath, FAR struct i2c_master_s *i2c, /* Check se05x availability */ - pnt_se05x_open(priv); + ret = pnt_se05x_open(priv); + if (ret < 0) + { + crypterr("ERROR: Failed to open se05x driver: %d\n", ret); + ret = -ENODEV; + goto errout_with_alloc; + } + struct se05x_uid_s uid; ret = pnt_se05x_get_uid(priv, &uid); if (ret < 0) { - crypterr("ERROR: Failed to register driver: %d\n", ret); - kmm_free(priv); - return -ENODEV; + crypterr("ERROR: Failed to probe se05x driver: %d\n", ret); + ret = -ENODEV; + goto errout_with_alloc_and_open; } pnt_se05x_close(priv); @@ -259,10 +267,20 @@ int se05x_register(FAR const char *devpath, FAR struct i2c_master_s *i2c, if (ret < 0) { crypterr("ERROR: Failed to register driver: %d\n", ret); - kmm_free(priv); + ret = -ENODEV; + goto errout_with_alloc; } nxmutex_init(&priv->mutex); + return OK; + +errout_with_alloc_and_open: + pnt_se05x_close(priv); + +errout_with_alloc: + kmm_free(priv); + +errout: return ret; }