Skip to content

Commit

Permalink
pal: crypto keys name refactor
Browse files Browse the repository at this point in the history
[KRKNWK-19108]

Signed-off-by: Krzysztof Taborowski <[email protected]>
  • Loading branch information
ktaborowski committed Aug 1, 2024
1 parent 09b08e8 commit 05bb0dd
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 34 deletions.
22 changes: 12 additions & 10 deletions subsys/sal/sid_pal/include/sid_crypto_keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
*/
Expand Down
2 changes: 1 addition & 1 deletion subsys/sal/sid_pal/src/sid_crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions subsys/sal/sid_pal/src/sid_crypto_keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)) {
Expand All @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions subsys/sal/sid_pal/src/sid_mfg_storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:");
Expand All @@ -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:");
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions subsys/sal/sid_pal/src/sid_storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
26 changes: 13 additions & 13 deletions tests/functional/crypto_keys/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
Expand Down

0 comments on commit 05bb0dd

Please sign in to comment.