From 05bb0dd599c83d45a64a6d16b5a83caf6d76d833 Mon Sep 17 00:00:00 2001 From: Krzysztof Taborowski Date: Thu, 1 Aug 2024 11:41:47 +0200 Subject: [PATCH] pal: crypto keys name refactor [KRKNWK-19108] Signed-off-by: Krzysztof Taborowski --- subsys/sal/sid_pal/include/sid_crypto_keys.h | 22 +++++++++-------- subsys/sal/sid_pal/src/sid_crypto.c | 2 +- subsys/sal/sid_pal/src/sid_crypto_keys.c | 8 +++--- subsys/sal/sid_pal/src/sid_mfg_storage.c | 8 +++--- subsys/sal/sid_pal/src/sid_storage.c | 4 +-- tests/functional/crypto_keys/src/main.c | 26 ++++++++++---------- 6 files changed, 36 insertions(+), 34 deletions(-) diff --git a/subsys/sal/sid_pal/include/sid_crypto_keys.h b/subsys/sal/sid_pal/include/sid_crypto_keys.h index f15e6c3cf9..de2be59d11 100644 --- a/subsys/sal/sid_pal/include/sid_crypto_keys.h +++ b/subsys/sal/sid_pal/include/sid_crypto_keys.h @@ -36,11 +36,11 @@ int sid_crypto_keys_init(void); * @note key value under given key id will be overwritten. * * @param id [in] Key id to import data. - * @param data [in] raw key data buffer on input. + * @param data [in] raw key data on input. * @param size [in] size of raw key data buffer. * @return 0 on success, or -errno on failure. */ -int sid_crypto_keys_import(psa_key_id_t id, uint8_t *data, size_t size); +int sid_crypto_keys_new_import(psa_key_id_t id, uint8_t *data, size_t size); /** * @brief Generate a new key value. @@ -50,33 +50,35 @@ int sid_crypto_keys_import(psa_key_id_t id, uint8_t *data, size_t size); * @param id [in] Key id to generate new. * @return 0 on success, or -errno on failure. */ -int sid_crypto_keys_generate(psa_key_id_t id); +int sid_crypto_keys_new_generate(psa_key_id_t id); /** - * @brief Set key id in data buffer. + * @brief Set key id in buffer. * * @param id [in] Key id to write to the data buffer. - * @param data [out] key id fulfilled with zeros. + * @param buffer [out] key id fulfilled with zeros. * @param size [in] size of raw key data buffer. * @return 0 on success, or -errno on failure. */ -int sid_crypto_keys_set(psa_key_id_t id, uint8_t *data, size_t size); +int sid_crypto_keys_buffer_set(psa_key_id_t id, uint8_t *buffer, size_t size); /** - * @brief Get key id by data. + * @brief Get key id from buffer. * * @param id [out] psa key id from key data buffer. * If key not found set to PSA_KEY_ID_NULL. - * @param data [in] key data buffer. + * @param buffer [in] key data buffer. * @param size [in] size of key data buffer. * @return 0 on success, or -errno on failure. - * -ENOENT - if key not found. + * -ENOENT - if no key in buffer. */ -int sid_crypto_keys_get(psa_key_id_t *id, uint8_t *data, size_t size); +int sid_crypto_keys_buffer_get(psa_key_id_t *id, uint8_t *buffer, size_t size); /** * @brief Destroy key. * + * @note This operation is irreversible. + * * @param id [in] psa key id to be permanently removed. * @return 0 on success, or -errno on failure. */ diff --git a/subsys/sal/sid_pal/src/sid_crypto.c b/subsys/sal/sid_pal/src/sid_crypto.c index ea6f9e07ab..0db2edbf75 100644 --- a/subsys/sal/sid_pal/src/sid_crypto.c +++ b/subsys/sal/sid_pal/src/sid_crypto.c @@ -155,7 +155,7 @@ static psa_status_t prepare_key(const uint8_t *key, size_t key_length, size_t ke } #ifdef CONFIG_SIDEWALK_CRYPTO_PSA_KEY_STORAGE - int err = sid_crypto_keys_get(key_handle, (uint8_t *)key, key_length); + int err = sid_crypto_keys_buffer_get(key_handle, (uint8_t *)key, key_length); if (!err && key_handle != PSA_KEY_ID_NULL) { return PSA_SUCCESS; } diff --git a/subsys/sal/sid_pal/src/sid_crypto_keys.c b/subsys/sal/sid_pal/src/sid_crypto_keys.c index 5d9fe3f8b2..ea6daa0b23 100644 --- a/subsys/sal/sid_pal/src/sid_crypto_keys.c +++ b/subsys/sal/sid_pal/src/sid_crypto_keys.c @@ -90,7 +90,7 @@ static void sid_crypto_keys_attributes_set(sid_crypto_key_id_t sid_key_id, psa_set_key_id(attr, sid_key_id); } -int sid_crypto_keys_import(psa_key_id_t id, uint8_t *data, size_t size) +int sid_crypto_keys_new_import(psa_key_id_t id, uint8_t *data, size_t size) { /* Check arguments */ if (PSA_KEY_ID_NULL == id || !data || !size) { @@ -130,7 +130,7 @@ int sid_crypto_keys_import(psa_key_id_t id, uint8_t *data, size_t size) return ESUCCESS; } -int sid_crypto_keys_generate(psa_key_id_t id) +int sid_crypto_keys_new_generate(psa_key_id_t id) { /* Check arguments */ if (PSA_KEY_ID_NULL == id) { @@ -170,7 +170,7 @@ int sid_crypto_keys_generate(psa_key_id_t id) return ESUCCESS; } -int sid_crypto_keys_set(psa_key_id_t id, uint8_t *data, size_t size) +int sid_crypto_keys_buffer_set(psa_key_id_t id, uint8_t *data, size_t size) { /* Check arguments */ if (PSA_KEY_ID_NULL == id || !data || size < sizeof(psa_key_id_t)) { @@ -186,7 +186,7 @@ int sid_crypto_keys_set(psa_key_id_t id, uint8_t *data, size_t size) return ESUCCESS; } -int sid_crypto_keys_get(psa_key_id_t *id, uint8_t *data, size_t size) +int sid_crypto_keys_buffer_get(psa_key_id_t *id, uint8_t *data, size_t size) { if (!id || !data || !size) { return -EINVAL; diff --git a/subsys/sal/sid_pal/src/sid_mfg_storage.c b/subsys/sal/sid_pal/src/sid_mfg_storage.c index c01e727f84..1afb83d462 100644 --- a/subsys/sal/sid_pal/src/sid_mfg_storage.c +++ b/subsys/sal/sid_pal/src/sid_mfg_storage.c @@ -299,7 +299,7 @@ void sid_pal_mfg_store_init(sid_pal_mfg_store_region_t mfg_store_region) sid_pal_mfg_store_read(SID_PAL_MFG_STORE_DEVICE_PRIV_ED25519_RAW, raw_key, sizeof(raw_key)); if (0 != memcmp(raw_key, zeros, sizeof(raw_key))) { - rc = sid_crypto_keys_import(SID_CRYPTO_MFG_ED25519_PRIV_KEY_ID, raw_key, + rc = sid_crypto_keys_new_import(SID_CRYPTO_MFG_ED25519_PRIV_KEY_ID, raw_key, sizeof(raw_key)); LOG_INF("MFG_ED25519 import %s", (0 == rc) ? "success" : "failure"); LOG_HEXDUMP_INF(raw_key, sizeof(raw_key), "value:"); @@ -311,7 +311,7 @@ void sid_pal_mfg_store_init(sid_pal_mfg_store_region_t mfg_store_region) sid_pal_mfg_store_read(SID_PAL_MFG_STORE_DEVICE_PRIV_P256R1_RAW, raw_key, sizeof(raw_key)); if (0 != memcmp(raw_key, zeros, sizeof(raw_key))) { - rc = sid_crypto_keys_import(SID_CRYPTO_MFG_SECP_256R1_PRIV_KEY_ID, raw_key, + rc = sid_crypto_keys_new_import(SID_CRYPTO_MFG_SECP_256R1_PRIV_KEY_ID, raw_key, sizeof(raw_key)); LOG_INF("MFG_SECP_256R1 import %s", (0 == rc) ? "success" : "failure"); LOG_HEXDUMP_INF(raw_key, sizeof(raw_key), "value:"); @@ -436,12 +436,12 @@ void sid_pal_mfg_store_read(uint16_t value, uint8_t *buffer, uint16_t length) #ifdef CONFIG_SIDEWALK_CRYPTO_PSA_KEY_STORAGE switch (value) { case SID_PAL_MFG_STORE_DEVICE_PRIV_ED25519: - if (sid_crypto_keys_set(SID_CRYPTO_MFG_ED25519_PRIV_KEY_ID, buffer, length)) { + if (sid_crypto_keys_buffer_set(SID_CRYPTO_MFG_ED25519_PRIV_KEY_ID, buffer, length)) { LOG_ERR("DEVICE_PRIV_ED25519 read fail"); } return; case SID_PAL_MFG_STORE_DEVICE_PRIV_P256R1: - if (sid_crypto_keys_set(SID_CRYPTO_MFG_SECP_256R1_PRIV_KEY_ID, buffer, length)) { + if (sid_crypto_keys_buffer_set(SID_CRYPTO_MFG_SECP_256R1_PRIV_KEY_ID, buffer, length)) { LOG_ERR("DEVICE_PRIV_P256R1 read fail"); } return; diff --git a/subsys/sal/sid_pal/src/sid_storage.c b/subsys/sal/sid_pal/src/sid_storage.c index 13f0593f42..81ccd0a6be 100644 --- a/subsys/sal/sid_pal/src/sid_storage.c +++ b/subsys/sal/sid_pal/src/sid_storage.c @@ -73,7 +73,7 @@ sid_error_t sid_pal_storage_kv_record_get(uint16_t group, uint16_t key, void *p_ #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_set(key_id, (uint8_t *)p_data, len); + int err = sid_crypto_keys_buffer_set(key_id, (uint8_t *)p_data, len); if (err) { LOG_ERR("Failed to read secure key id %d", key_id); } else { @@ -118,7 +118,7 @@ sid_error_t sid_pal_storage_kv_record_set(uint16_t group, uint16_t key, void con #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_import(key_id, (uint8_t *)p_data, len); + int err = sid_crypto_keys_new_import(key_id, (uint8_t *)p_data, len); if (err) { LOG_ERR("Failed to write secure key id %d", key_id); } else { diff --git a/tests/functional/crypto_keys/src/main.c b/tests/functional/crypto_keys/src/main.c index dea41f3e78..a2e035c9e8 100644 --- a/tests/functional/crypto_keys/src/main.c +++ b/tests/functional/crypto_keys/src/main.c @@ -38,35 +38,35 @@ ZTEST(crypto_keys, test_sid_crypto_key_invalid_args) /* Invalid data */ psa_key_id_t new_key_id = PSA_KEY_ID_NULL; - err = sid_crypto_keys_get(&new_key_id, NULL, TEST_KEY_SIZE); + err = sid_crypto_keys_buffer_get(&new_key_id, NULL, TEST_KEY_SIZE); zassert_equal(-EINVAL, err, "err: %d", err); - err = sid_crypto_keys_get(&new_key_id, key_data, 0); + err = sid_crypto_keys_buffer_get(&new_key_id, key_data, 0); zassert_equal(-EINVAL, err, "err: %d", err); - err = sid_crypto_keys_set(test_key_id, NULL, TEST_KEY_SIZE); + err = sid_crypto_keys_buffer_set(test_key_id, NULL, TEST_KEY_SIZE); zassert_equal(-EINVAL, err, "err: %d", err); - err = sid_crypto_keys_set(test_key_id, key_data, 0); + err = sid_crypto_keys_buffer_set(test_key_id, key_data, 0); zassert_equal(-EINVAL, err, "err: %d", err); - err = sid_crypto_keys_import(test_key_id, NULL, TEST_KEY_SIZE); + err = sid_crypto_keys_new_import(test_key_id, NULL, TEST_KEY_SIZE); zassert_equal(-EINVAL, err, "err: %d", err); - err = sid_crypto_keys_import(test_key_id, key_data, 0); + err = sid_crypto_keys_new_import(test_key_id, key_data, 0); zassert_equal(-EINVAL, err, "err: %d", err); /* Invalid key id */ - err = sid_crypto_keys_get(NULL, key_data, TEST_KEY_SIZE); + err = sid_crypto_keys_buffer_get(NULL, key_data, TEST_KEY_SIZE); zassert_equal(-EINVAL, err, "err: %d", err); - err = sid_crypto_keys_set(PSA_KEY_ID_NULL, key_data, TEST_KEY_SIZE); + err = sid_crypto_keys_buffer_set(PSA_KEY_ID_NULL, key_data, TEST_KEY_SIZE); zassert_equal(-EINVAL, err, "err: %d", err); - err = sid_crypto_keys_import(PSA_KEY_ID_NULL, key_data, TEST_KEY_SIZE); + err = sid_crypto_keys_new_import(PSA_KEY_ID_NULL, key_data, TEST_KEY_SIZE); zassert_equal(-EINVAL, err, "err: %d", err); - err = sid_crypto_keys_generate(PSA_KEY_ID_NULL); + err = sid_crypto_keys_new_generate(PSA_KEY_ID_NULL); zassert_equal(-EINVAL, err, "err: %d", err); } @@ -79,13 +79,13 @@ ZTEST(crypto_keys, test_sid_crypto_key_positive) err = sid_crypto_keys_init(); zassert_equal(0, err, "err: %d", err); - err = sid_crypto_keys_import(test_key_id, test_key_data, TEST_KEY_SIZE); + err = sid_crypto_keys_new_import(test_key_id, test_key_data, TEST_KEY_SIZE); zassert_equal(0, err, "err: %d", err); - err = sid_crypto_keys_set(test_key_id, test_key_data, TEST_KEY_SIZE); + err = sid_crypto_keys_buffer_set(test_key_id, test_key_data, TEST_KEY_SIZE); zassert_equal(0, err, "err: %d", err); - err = sid_crypto_keys_get(&new_key_id, test_key_data, TEST_KEY_SIZE); + err = sid_crypto_keys_buffer_get(&new_key_id, test_key_data, TEST_KEY_SIZE); zassert_equal(0, err, "err: %d", err); zassert_equal(new_key_id, test_key_id);