drivers: ieee802154: nrf5: refactor storing mac keys

This commit makes nrf5_config_mac_keys function more generic.
Is uses lookup table for storing keys to override. It removes old keys
before storing new ones.

Signed-off-by: Lukasz Maciejonczyk <lukasz.maciejonczyk@nordicsemi.no>
This commit is contained in:
Lukasz Maciejonczyk 2021-11-04 13:58:08 +01:00 committed by Carles Cufí
parent 2d7328af99
commit c930262fd8

View file

@ -702,36 +702,36 @@ static void nrf5_iface_init(struct net_if *iface)
#if defined(CONFIG_NRF_802154_ENCRYPTION)
static void nrf5_config_mac_keys(struct ieee802154_key *mac_keys)
{
nrf_802154_security_error_t err;
nrf_802154_key_t key;
uint8_t key_id_to_remove;
static nrf_802154_key_id_t stored_key_ids[NRF_802154_SECURITY_KEY_STORAGE_SIZE];
static uint8_t stored_ids[NRF_802154_SECURITY_KEY_STORAGE_SIZE];
uint8_t i;
__ASSERT(mac_keys, "Invalid argument.");
/* Remove old invalid key assuming that its index is first_valid_key_id - 1.
* TODO: This is Thread specific assumption, need to be changed when RD will provided
* API for removing all keys or handling this internally.
*/
key_id_to_remove = mac_keys->key_index == 1 ? 0x80 : mac_keys->key_index - 1;
key.id.mode = mac_keys->key_id_mode;
key.id.p_key_id = &key_id_to_remove;
nrf_802154_security_key_remove(&key.id);
for (i = 0; i < NRF_802154_SECURITY_KEY_STORAGE_SIZE && stored_key_ids[i].p_key_id; i++) {
nrf_802154_security_key_remove(&stored_key_ids[i]);
stored_key_ids[i].p_key_id = NULL;
}
i = 0;
for (struct ieee802154_key *keys = mac_keys; keys->key_value; keys++) {
key.value.p_cleartext_key = keys->key_value;
key.id.mode = keys->key_id_mode;
key.id.p_key_id = &(keys->key_index);
key.type = NRF_802154_KEY_CLEARTEXT;
key.frame_counter = 0;
key.use_global_frame_counter = !(keys->frame_counter_per_key);
nrf_802154_key_t key = {
.value.p_cleartext_key = keys->key_value,
.id.mode = keys->key_id_mode,
.id.p_key_id = &(keys->key_index),
.type = NRF_802154_KEY_CLEARTEXT,
.frame_counter = 0,
.use_global_frame_counter = !(keys->frame_counter_per_key),
};
nrf_802154_security_key_remove(&key.id);
err = nrf_802154_security_key_store(&key);
nrf_802154_security_error_t err = nrf_802154_security_key_store(&key);
__ASSERT(err == NRF_802154_SECURITY_ERROR_NONE ||
err == NRF_802154_SECURITY_ERROR_ALREADY_PRESENT,
"Storing key failed, err: %d", err);
__ASSERT(i < NRF_802154_SECURITY_KEY_STORAGE_SIZE, "Store buffer is full");
stored_ids[i] = *key.id.p_key_id;
stored_key_ids[i].mode = key.id.mode;
stored_key_ids[i].p_key_id = &stored_ids[i];
i++;
};
}
#endif /* CONFIG_NRF_802154_ENCRYPTION */