Skip to content

Commit

Permalink
pal: crypto keys error code when not found
Browse files Browse the repository at this point in the history
fix error when deleting keys
when they were never added

Signed-off-by: Krzysztof Taborowski <[email protected]>
  • Loading branch information
ktaborowski committed Sep 9, 2024
1 parent 2a126fe commit 7238295
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 26 deletions.
5 changes: 4 additions & 1 deletion subsys/sal/sid_pal/src/sid_crypto_keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,10 @@ int sid_crypto_keys_delete(psa_key_id_t id)
}

psa_status_t status = psa_destroy_key(id);
if (status != PSA_SUCCESS) {
if (status == PSA_ERROR_INVALID_HANDLE) {
LOG_WRN("psa_destroy_key invalid id %d", id);
return -EINVAL;
} else if (status != PSA_SUCCESS) {
LOG_ERR("psa_destroy_key failed! (err %d id %d)", status, id);
return -EFAULT;
}
Expand Down
45 changes: 20 additions & 25 deletions subsys/sal/sid_pal/src/sid_storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ static void storage_key_save_secure(uint16_t group, uint16_t key)
return;
}
}

static bool storage_key_delete_secure(sid_crypto_key_id_t id)
{
int err = sid_crypto_keys_delete(id);
if (-EINVAL == err) {
LOG_INF("Not found secure key id %d", id);
} else if (err) {
LOG_ERR("Failed to delete secure key id %d", id);
return false;
}

return true;
}

#endif /* CONFIG_SIDEWALK_CRYPTO_PSA_KEY_STORAGE */

sid_error_t sid_pal_storage_kv_init()
Expand Down Expand Up @@ -206,13 +220,7 @@ sid_error_t sid_pal_storage_kv_record_delete(uint16_t group, uint16_t key)
#ifdef CONFIG_SIDEWALK_CRYPTO_PSA_KEY_STORAGE
psa_key_id_t key_id = storage2key_id(group, key);
if (SID_CRYPTO_KEYS_ID_IS_SIDEWALK_KEY(key_id)) {
int err = sid_crypto_keys_delete(key_id);
if (err) {
LOG_ERR("Failed to delete secure key id %d", key_id);
return SID_ERROR_STORAGE_ERASE_FAIL;
} else {
return SID_ERROR_NONE;
}
return storage_key_delete_secure(key_id) ? SID_ERROR_NONE : SID_ERROR_STORAGE_ERASE_FAIL;
}
#endif /* CONFIG_SIDEWALK_CRYPTO_PSA_KEY_STORAGE */

Expand Down Expand Up @@ -256,26 +264,13 @@ sid_error_t sid_pal_storage_kv_group_delete(uint16_t group)
}

#ifdef CONFIG_SIDEWALK_CRYPTO_PSA_KEY_STORAGE
bool key_delete_fail = false;
bool success = true;
if (STORAGE_KV_INTERNAL_PROTOCOL_GROUP_ID == group) {
int err = sid_crypto_keys_delete(SID_CRYPTO_KV_WAN_MASTER_KEY_ID);
if (err) {
LOG_ERR("Failed to delete secure key id %d",
SID_CRYPTO_KV_WAN_MASTER_KEY_ID);
key_delete_fail = true;
}
err = sid_crypto_keys_delete(SID_CRYPTO_KV_APP_KEY_KEY_ID);
if (err) {
LOG_ERR("Failed to delete secure key id %d", SID_CRYPTO_KV_APP_KEY_KEY_ID);
key_delete_fail = true;
}
err = sid_crypto_keys_delete(SID_CRYPTO_KV_D2D_KEY_ID);
if (err) {
LOG_ERR("Failed to delete secure key id %d", SID_CRYPTO_KV_D2D_KEY_ID);
key_delete_fail = true;
}
success = success && storage_key_delete_secure(SID_CRYPTO_KV_WAN_MASTER_KEY_ID);
success = success && storage_key_delete_secure(SID_CRYPTO_KV_APP_KEY_KEY_ID);
success = success && storage_key_delete_secure(SID_CRYPTO_KV_D2D_KEY_ID);
}
if (key_delete_fail) {
if (!success) {
return SID_ERROR_STORAGE_ERASE_FAIL;
}
#endif /* CONFIG_SIDEWALK_CRYPTO_PSA_KEY_STORAGE */
Expand Down

0 comments on commit 7238295

Please sign in to comment.