-
Notifications
You must be signed in to change notification settings - Fork 8.3k
secure_storage: add a ZMS-based implementation of the ITS store module #81235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
60bb1bf
f8dbc31
5f6043b
1177fc9
748253d
9cc9153
28c984e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| /* Copyright (c) 2024 Nordic Semiconductor | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| #include <zephyr/secure_storage/its/store.h> | ||
| #include <zephyr/logging/log.h> | ||
| #include <zephyr/fs/zms.h> | ||
| #include <zephyr/storage/flash_map.h> | ||
| #ifdef CONFIG_SECURE_STORAGE_ITS_IMPLEMENTATION_ZEPHYR | ||
| #include <zephyr/secure_storage/its/transform.h> | ||
| #endif | ||
|
|
||
| LOG_MODULE_DECLARE(secure_storage, CONFIG_SECURE_STORAGE_LOG_LEVEL); | ||
|
|
||
| BUILD_ASSERT(CONFIG_SECURE_STORAGE_ITS_STORE_ZMS_SECTOR_SIZE | ||
| > 2 * CONFIG_SECURE_STORAGE_ITS_MAX_DATA_SIZE); | ||
|
|
||
| #define PARTITION_DT_NODE DT_CHOSEN(secure_storage_its_partition) | ||
|
|
||
| static struct zms_fs s_zms = { | ||
| .flash_device = FIXED_PARTITION_NODE_DEVICE(PARTITION_DT_NODE), | ||
| .offset = FIXED_PARTITION_NODE_OFFSET(PARTITION_DT_NODE), | ||
| .sector_size = CONFIG_SECURE_STORAGE_ITS_STORE_ZMS_SECTOR_SIZE, | ||
| }; | ||
|
|
||
| static int init_zms(void) | ||
| { | ||
| int ret; | ||
|
|
||
| s_zms.sector_count = FIXED_PARTITION_NODE_SIZE(PARTITION_DT_NODE) / s_zms.sector_size; | ||
|
|
||
| ret = zms_mount(&s_zms); | ||
| if (ret) { | ||
| LOG_DBG("Failed. (%d)", ret); | ||
| } | ||
| return ret; | ||
| } | ||
| SYS_INIT(init_zms, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY); | ||
|
|
||
| /* Bit position of the ITS caller ID in the ZMS entry ID. */ | ||
| #define ITS_CALLER_ID_POS 30 | ||
rghaddab marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /* Make sure that every ITS caller ID fits in ZMS entry IDs at the defined position. */ | ||
| BUILD_ASSERT(1 << (32 - ITS_CALLER_ID_POS) >= SECURE_STORAGE_ITS_CALLER_COUNT); | ||
|
|
||
| static bool has_forbidden_bits_set(secure_storage_its_uid_t uid) | ||
| { | ||
| if (uid.uid & GENMASK64(63, ITS_CALLER_ID_POS)) { | ||
| LOG_DBG("UID %u/0x%llx cannot be used as it has bits set past " | ||
| "the first " STRINGIFY(ITS_CALLER_ID_POS) " ones.", | ||
| uid.caller_id, (unsigned long long)uid.uid); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| static uint32_t zms_id_from(secure_storage_its_uid_t uid) | ||
| { | ||
| return (uint32_t)uid.uid | (uid.caller_id << ITS_CALLER_ID_POS); | ||
rghaddab marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| psa_status_t secure_storage_its_store_set(secure_storage_its_uid_t uid, | ||
| size_t data_length, const void *data) | ||
| { | ||
| psa_status_t psa_ret; | ||
| ssize_t zms_ret; | ||
| const uint32_t zms_id = zms_id_from(uid); | ||
|
|
||
| if (has_forbidden_bits_set(uid)) { | ||
| return PSA_ERROR_INVALID_ARGUMENT; | ||
| } | ||
|
|
||
| zms_ret = zms_write(&s_zms, zms_id, data, data_length); | ||
| if (zms_ret == data_length) { | ||
| psa_ret = PSA_SUCCESS; | ||
| } else if (zms_ret == -ENOSPC) { | ||
| psa_ret = PSA_ERROR_INSUFFICIENT_STORAGE; | ||
| } else { | ||
| psa_ret = PSA_ERROR_STORAGE_FAILURE; | ||
| } | ||
| LOG_DBG("%s 0x%x with %zu bytes. (%zd)", (psa_ret == PSA_SUCCESS) ? | ||
| "Wrote" : "Failed to write", zms_id, data_length, zms_ret); | ||
| return psa_ret; | ||
| } | ||
|
|
||
| psa_status_t secure_storage_its_store_get(secure_storage_its_uid_t uid, size_t data_size, | ||
| void *data, size_t *data_length) | ||
| { | ||
| psa_status_t psa_ret; | ||
| ssize_t zms_ret; | ||
| const uint32_t zms_id = zms_id_from(uid); | ||
|
|
||
| if (has_forbidden_bits_set(uid)) { | ||
| return PSA_ERROR_INVALID_ARGUMENT; | ||
| } | ||
|
|
||
| zms_ret = zms_read(&s_zms, zms_id, data, data_size); | ||
| if (zms_ret > 0) { | ||
| *data_length = zms_ret; | ||
| psa_ret = PSA_SUCCESS; | ||
| } else if (zms_ret == -ENOENT) { | ||
| psa_ret = PSA_ERROR_DOES_NOT_EXIST; | ||
| } else { | ||
| psa_ret = PSA_ERROR_STORAGE_FAILURE; | ||
| } | ||
| LOG_DBG("%s 0x%x for up to %zu bytes. (%zd)", (psa_ret != PSA_ERROR_STORAGE_FAILURE) ? | ||
| "Read" : "Failed to read", zms_id, data_size, zms_ret); | ||
| return psa_ret; | ||
| } | ||
|
|
||
| psa_status_t secure_storage_its_store_remove(secure_storage_its_uid_t uid) | ||
| { | ||
| int zms_ret; | ||
| const uint32_t zms_id = zms_id_from(uid); | ||
|
|
||
| if (has_forbidden_bits_set(uid)) { | ||
| return PSA_ERROR_INVALID_ARGUMENT; | ||
| } | ||
|
|
||
| zms_ret = zms_delete(&s_zms, zms_id); | ||
| LOG_DBG("%s 0x%x. (%d)", zms_ret ? "Failed to delete" : "Deleted", zms_id, zms_ret); | ||
| BUILD_ASSERT(PSA_SUCCESS == 0); | ||
| return zms_ret; | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| CONFIG_SECURE_STORAGE_ITS_STORE_IMPLEMENTATION_SETTINGS=y | ||
|
|
||
| # 256 - flags (1) - CONFIG_SECURE_STORAGE_ITS_TRANSFORM_OUTPUT_OVERHEAD (28) | ||
| CONFIG_SECURE_STORAGE_ITS_MAX_DATA_SIZE=227 | ||
|
|
||
| # Limit the space available for the maximum entry test to not take too long with NVS. | ||
| CONFIG_SETTINGS_NVS_SECTOR_COUNT=2 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| CONFIG_SECURE_STORAGE_ITS_TRANSFORM_IMPLEMENTATION_CUSTOM=y | ||
| CONFIG_SECURE_STORAGE_ITS_TRANSFORM_OUTPUT_OVERHEAD=0 | ||
|
|
||
| # SETTINGS_MAX_VAL_LEN (256) - flags (1) | ||
| CONFIG_SECURE_STORAGE_ITS_MAX_DATA_SIZE=255 | ||
| CONFIG_SECURE_STORAGE_ITS_MAX_DATA_SIZE=500 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,42 +1,45 @@ | ||
| common: | ||
| integration_platforms: | ||
| - native_sim | ||
| - nrf54l15dk/nrf54l15/cpuapp | ||
| platform_exclude: | ||
| - qemu_cortex_m0 # settings subsystem initialization fails | ||
| timeout: 600 | ||
| tags: | ||
| - psa.secure_storage | ||
| tests: | ||
| secure_storage.psa.its.secure_storage: | ||
| filter: CONFIG_SECURE_STORAGE and not CONFIG_SECURE_STORAGE_ITS_STORE_IMPLEMENTATION_NONE | ||
| secure_storage.psa.its.secure_storage.zms: | ||
| # DT-based filtering is not possible for this test scenario. | ||
| # Platforms with a storage_partition must be manually added here. | ||
| platform_allow: native_sim mps2/an385 qemu_x86/atom qemu_x86_64/atom | ||
| nrf54l15dk/nrf54l15/cpuapp nrf5340dk/nrf5340/cpuapp nrf52840dk/nrf52840 | ||
| nrf9151dk/nrf9151 nrf9160dk/nrf9160 nrf9161dk/nrf9161 | ||
| extra_args: | ||
| - EXTRA_DTC_OVERLAY_FILE=zms.overlay | ||
| - EXTRA_CONF_FILE=overlay-secure_storage.conf;overlay-transform_default.conf | ||
| - CONFIG_SECURE_STORAGE_ITS_STORE_IMPLEMENTATION_ZMS=y | ||
|
|
||
| secure_storage.psa.its.secure_storage.settings.nvs: | ||
| filter: CONFIG_SECURE_STORAGE_ITS_STORE_IMPLEMENTATION_SETTINGS | ||
| extra_args: "EXTRA_CONF_FILE=\ | ||
| overlay-secure_storage.conf;overlay-default_transform.conf;overlay-default_store.conf" | ||
| integration_platforms: | ||
| - native_sim | ||
| - nrf54l15dk/nrf54l15/cpuapp | ||
| overlay-secure_storage.conf;overlay-transform_default.conf;overlay-store_settings.conf" | ||
|
|
||
| secure_storage.psa.its.secure_storage.custom.transform: | ||
| filter: CONFIG_SECURE_STORAGE and not CONFIG_SECURE_STORAGE_ITS_STORE_IMPLEMENTATION_NONE | ||
| extra_args: "EXTRA_CONF_FILE=\ | ||
| overlay-secure_storage.conf;overlay-custom_transform.conf;overlay-default_store.conf" | ||
| integration_platforms: | ||
| - native_sim | ||
| - nrf54l15dk/nrf54l15/cpuapp | ||
| extra_args: EXTRA_CONF_FILE=overlay-secure_storage.conf;overlay-transform_custom.conf | ||
|
|
||
| secure_storage.psa.its.secure_storage.custom.store: | ||
| filter: CONFIG_SECURE_STORAGE | ||
| extra_args: "EXTRA_CONF_FILE=\ | ||
| overlay-secure_storage.conf;overlay-default_transform.conf;overlay-custom_store.conf" | ||
| integration_platforms: | ||
| - native_sim | ||
| - nrf54l15dk/nrf54l15/cpuapp | ||
| overlay-secure_storage.conf;overlay-transform_default.conf;overlay-store_custom.conf" | ||
|
|
||
| secure_storage.psa.its.secure_storage.custom.both: | ||
| filter: CONFIG_SECURE_STORAGE | ||
| extra_args: "EXTRA_CONF_FILE=\ | ||
| overlay-secure_storage.conf;overlay-custom_transform.conf;overlay-custom_store.conf" | ||
| integration_platforms: | ||
| - native_sim | ||
| - nrf54l15dk/nrf54l15/cpuapp | ||
| overlay-secure_storage.conf;overlay-transform_custom.conf;overlay-store_custom.conf" | ||
|
|
||
| secure_storage.psa.its.tfm: | ||
| filter: CONFIG_BUILD_WITH_TFM | ||
| extra_args: EXTRA_CONF_FILE=overlay-tfm.conf | ||
| integration_platforms: | ||
| - nrf9151dk/nrf9151/ns | ||
| extra_args: EXTRA_CONF_FILE=overlay-tfm.conf |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| / { | ||
| chosen { | ||
| secure_storage_its_partition = &storage_partition; | ||
| }; | ||
| }; |
Uh oh!
There was an error while loading. Please reload this page.