Skip to content

Commit

Permalink
crypto/se05x: Allow set_enable_pin to be NULL and fix error handling
Browse files Browse the repository at this point in the history
- 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()
  • Loading branch information
AndreHeinemans-NXP authored and xiaoxiang781216 committed Nov 7, 2024
1 parent ad2db7a commit 4a6548e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 35 deletions.
60 changes: 31 additions & 29 deletions drivers/crypto/pnt/pnt_se05x_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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))
{
ret = -EIO;
goto errout_with_alloc;
}

se05x->pnt->session.skip_applet_select = 0;
se05x->pnt->session.session_resume = 0;
if (Se05x_API_SessionOpen(&(se05x->pnt->session), se05x) != SM_OK)
{
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 */
return OK;

if (ret < 0)
errout_with_alloc:
if (se05x->pnt->session.conn_context != NULL)
{
if (se05x->pnt->session.conn_context != NULL)
{
kmm_free(se05x->pnt->session.conn_context);
}

if (se05x->pnt != NULL)
{
kmm_free(se05x->pnt);
}
kmm_free(se05x->pnt->session.conn_context);
}

kmm_free(se05x->pnt);

errout:
return ret;
}

Expand Down
30 changes: 24 additions & 6 deletions drivers/crypto/se05x.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,22 +233,30 @@ 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;
priv->i2c = 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);
Expand All @@ -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;
}

0 comments on commit 4a6548e

Please sign in to comment.