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()
This commit is contained in:
Andre Heinemans 2024-11-06 13:36:47 +01:00 committed by Xiang Xiao
parent ad2db7a387
commit 4a6548e2d8
2 changed files with 56 additions and 36 deletions

View file

@ -77,7 +77,7 @@ static bool set_enable_pin(FAR struct se05x_dev_s *se05x, bool state)
{ {
if (se05x->config->set_enable_pin == NULL) if (se05x->config->set_enable_pin == NULL)
{ {
return FALSE; return TRUE;
} }
return se05x->config->set_enable_pin(state); 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 pnt_se05x_open(FAR struct se05x_dev_s *se05x)
{ {
int ret;
se05x->pnt = kmm_malloc(sizeof(struct pnt_handle)); 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)); ret = -EIO;
goto errout;
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;
} }
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; ret = -EIO;
se05x->pnt->session.session_resume = 0; goto errout_with_alloc;
smStatus_t status =
Se05x_API_SessionOpen(&(se05x->pnt->session), se05x);
ret = status == SM_OK ? 0 : -EIO;
} }
/* if error */ se05x->pnt->session.skip_applet_select = 0;
se05x->pnt->session.session_resume = 0;
if (ret < 0) if (Se05x_API_SessionOpen(&(se05x->pnt->session), se05x) != SM_OK)
{ {
if (se05x->pnt->session.conn_context != NULL) ret = -EIO;
{ goto errout_with_alloc;
kmm_free(se05x->pnt->session.conn_context);
}
if (se05x->pnt != NULL)
{
kmm_free(se05x->pnt);
}
} }
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; return ret;
} }

View file

@ -233,7 +233,8 @@ int se05x_register(FAR const char *devpath, FAR struct i2c_master_s *i2c,
if (priv == NULL) if (priv == NULL)
{ {
crypterr("ERROR: Failed to allocate instance\n"); crypterr("ERROR: Failed to allocate instance\n");
return -ENOMEM; ret = -ENOMEM;
goto errout;
} }
priv->config = config; priv->config = config;
@ -241,14 +242,21 @@ int se05x_register(FAR const char *devpath, FAR struct i2c_master_s *i2c,
/* Check se05x availability */ /* 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; struct se05x_uid_s uid;
ret = pnt_se05x_get_uid(priv, &uid); ret = pnt_se05x_get_uid(priv, &uid);
if (ret < 0) if (ret < 0)
{ {
crypterr("ERROR: Failed to register driver: %d\n", ret); crypterr("ERROR: Failed to probe se05x driver: %d\n", ret);
kmm_free(priv); ret = -ENODEV;
return -ENODEV; goto errout_with_alloc_and_open;
} }
pnt_se05x_close(priv); pnt_se05x_close(priv);
@ -259,10 +267,20 @@ int se05x_register(FAR const char *devpath, FAR struct i2c_master_s *i2c,
if (ret < 0) if (ret < 0)
{ {
crypterr("ERROR: Failed to register driver: %d\n", ret); crypterr("ERROR: Failed to register driver: %d\n", ret);
kmm_free(priv); ret = -ENODEV;
goto errout_with_alloc;
} }
nxmutex_init(&priv->mutex); nxmutex_init(&priv->mutex);
return OK;
errout_with_alloc_and_open:
pnt_se05x_close(priv);
errout_with_alloc:
kmm_free(priv);
errout:
return ret; return ret;
} }