Skip to content

Commit fd538dc

Browse files
tomi-fontkartben
authored andcommitted
secure_storage: its: store: settings: allow using custom setting names
Allow replacing the default naming scheme of the stored settings by providing a custom function that fills a name buffer based on the ITS entry UID. Signed-off-by: Tomi Fontanilles <[email protected]>
1 parent 09228de commit fd538dc

File tree

5 files changed

+78
-15
lines changed

5 files changed

+78
-15
lines changed

subsys/secure_storage/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,7 @@ if(CONFIG_SECURE_STORAGE_ITS_TRANSFORM_AEAD_SCHEME_CUSTOM
4646
OR CONFIG_SECURE_STORAGE_ITS_TRANSFORM_AEAD_NONCE_PROVIDER_CUSTOM)
4747
make_available(its/transform/aead_get.h)
4848
endif()
49+
50+
if(CONFIG_SECURE_STORAGE_ITS_STORE_SETTINGS_NAME_CUSTOM)
51+
make_available(its/store/settings_get.h)
52+
endif()

subsys/secure_storage/Kconfig.its_store

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,25 @@ endif # SECURE_STORAGE_ITS_STORE_IMPLEMENTATION_ZMS
6060

6161
if SECURE_STORAGE_ITS_STORE_IMPLEMENTATION_SETTINGS
6262

63+
config SECURE_STORAGE_ITS_STORE_SETTINGS_NAME_CUSTOM
64+
bool "Custom naming scheme for the stored settings"
65+
help
66+
This allows to use custom names for the settings that the implementation uses
67+
instead of the default naming scheme.
68+
When enabling this, implement the secure_storage_its_store_settings_get_name()
69+
function declared in <zephyr/secure_storage/its/store/settings_get.h>
70+
and set CONFIG_SECURE_STORAGE_ITS_STORE_SETTINGS_NAME_MAX_LEN appropriately.
71+
The header file is made available when this Kconfig option is enabled.
72+
6373
config SECURE_STORAGE_ITS_STORE_SETTINGS_PREFIX
6474
string "Subtree in which to store the settings, with a trailing slash. Can be empty."
6575
default "its/"
76+
depends on !SECURE_STORAGE_ITS_STORE_SETTINGS_NAME_CUSTOM
77+
78+
config SECURE_STORAGE_ITS_STORE_SETTINGS_NAME_MAX_LEN
79+
int "Maximum setting name length"
80+
range 2 64
81+
default 22 if !SECURE_STORAGE_ITS_STORE_SETTINGS_NAME_CUSTOM
82+
default 0
6683

6784
endif # SECURE_STORAGE_ITS_STORE_IMPLEMENTATION_SETTINGS
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* Copyright (c) 2024 Nordic Semiconductor
2+
* SPDX-License-Identifier: Apache-2.0
3+
*/
4+
#ifndef SECURE_STORAGE_ITS_STORE_SETTINGS_GET_H
5+
#define SECURE_STORAGE_ITS_STORE_SETTINGS_GET_H
6+
7+
/** @file zephyr/secure_storage/its/store/settings_get.h The settings ITS store module API.
8+
*
9+
* The functions declared in this header allow customization
10+
* of the settings implementation of the ITS store module.
11+
* They are not meant to be called directly other than by the settings ITS store module.
12+
* This header file may and must be included when providing a custom implementation of one
13+
* or more of these functions (@kconfig{CONFIG_SECURE_STORAGE_ITS_STORE_SETTINGS_*_CUSTOM}).
14+
*/
15+
#include <zephyr/secure_storage/its/common.h>
16+
17+
enum { SECURE_STORAGE_ITS_STORE_SETTINGS_NAME_BUF_SIZE
18+
= CONFIG_SECURE_STORAGE_ITS_STORE_SETTINGS_NAME_MAX_LEN + 1 };
19+
20+
/** @brief Returns the setting name to use for an ITS entry.
21+
*
22+
* @param[in] uid The UID of the ITS entry for which the setting name is used.
23+
* @param[out] name The setting name.
24+
*/
25+
void secure_storage_its_store_settings_get_name(
26+
secure_storage_its_uid_t uid,
27+
char name[static SECURE_STORAGE_ITS_STORE_SETTINGS_NAME_BUF_SIZE]);
28+
29+
#endif

subsys/secure_storage/include/internal/zephyr/secure_storage/its/transform/aead_get.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* The functions declared in this header allow customization
1010
* of the AEAD implementation of the ITS transform module.
1111
* They are not meant to be called directly other than by the AEAD ITS transform module.
12-
* This header may be included when providing a custom implementation of one
12+
* This header file may and must be included when providing a custom implementation of one
1313
* or more of these functions (@kconfig{CONFIG_SECURE_STORAGE_ITS_TRANSFORM_AEAD_*_CUSTOM}).
1414
*/
1515
#include <zephyr/secure_storage/its/common.h>
@@ -24,7 +24,7 @@ void secure_storage_its_transform_aead_get_scheme(psa_key_type_t *key_type, psa_
2424

2525
/** @brief Returns the encryption key to use for an ITS entry's AEAD operations.
2626
*
27-
* @param[in] uid The UID of the ITS entry for whom the returned key is used.
27+
* @param[in] uid The UID of the ITS entry for which the key is used.
2828
* @param[out] key The encryption key.
2929
*
3030
* @return `PSA_SUCCESS` on success, anything else on failure.

subsys/secure_storage/src/its/store/settings.c

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* SPDX-License-Identifier: Apache-2.0
33
*/
44
#include <zephyr/secure_storage/its/store.h>
5+
#include <zephyr/secure_storage/its/store/settings_get.h>
56
#include <zephyr/init.h>
67
#include <zephyr/logging/log.h>
78
#include <zephyr/settings/settings.h>
@@ -26,26 +27,37 @@ static int init_settings_subsys(void)
2627
}
2728
SYS_INIT(init_settings_subsys, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);
2829

29-
enum { NAME_BUF_SIZE = sizeof(CONFIG_SECURE_STORAGE_ITS_STORE_SETTINGS_PREFIX) - 1
30-
+ 2 * (sizeof(secure_storage_its_uid_t) + 1) };
31-
BUILD_ASSERT(NAME_BUF_SIZE <= SETTINGS_MAX_NAME_LEN + 1);
30+
BUILD_ASSERT(CONFIG_SECURE_STORAGE_ITS_STORE_SETTINGS_NAME_MAX_LEN <= SETTINGS_MAX_NAME_LEN);
3231

33-
static void make_name(secure_storage_its_uid_t uid, char name[static NAME_BUF_SIZE])
32+
#ifndef CONFIG_SECURE_STORAGE_ITS_STORE_SETTINGS_NAME_CUSTOM
33+
34+
BUILD_ASSERT(CONFIG_SECURE_STORAGE_ITS_STORE_SETTINGS_NAME_MAX_LEN ==
35+
sizeof(CONFIG_SECURE_STORAGE_ITS_STORE_SETTINGS_PREFIX) - 1
36+
+ 1 + 1 /* caller ID + '/' */
37+
+ 2 * sizeof(psa_storage_uid_t) /* hex UID */);
38+
39+
void secure_storage_its_store_settings_get_name(
40+
secure_storage_its_uid_t uid,
41+
char name[static SECURE_STORAGE_ITS_STORE_SETTINGS_NAME_BUF_SIZE])
3442
{
3543
int ret;
3644

37-
ret = snprintf(name, NAME_BUF_SIZE, CONFIG_SECURE_STORAGE_ITS_STORE_SETTINGS_PREFIX
38-
"%x/%llx", uid.caller_id, (unsigned long long)uid.uid);
39-
__ASSERT_NO_MSG(ret > 0 && ret < NAME_BUF_SIZE);
45+
ret = snprintf(name, SECURE_STORAGE_ITS_STORE_SETTINGS_NAME_BUF_SIZE,
46+
CONFIG_SECURE_STORAGE_ITS_STORE_SETTINGS_PREFIX "%x/%llx",
47+
uid.caller_id, (unsigned long long)uid.uid);
48+
__ASSERT_NO_MSG(ret > 0 && ret < SECURE_STORAGE_ITS_STORE_SETTINGS_NAME_BUF_SIZE);
4049
}
4150

51+
#endif /* !CONFIG_SECURE_STORAGE_ITS_STORE_SETTINGS_NAME_CUSTOM */
52+
4253
psa_status_t secure_storage_its_store_set(secure_storage_its_uid_t uid,
4354
size_t data_length, const void *data)
4455
{
4556
int ret;
46-
char name[NAME_BUF_SIZE];
57+
char name[SECURE_STORAGE_ITS_STORE_SETTINGS_NAME_BUF_SIZE];
58+
59+
secure_storage_its_store_settings_get_name(uid, name);
4760

48-
make_name(uid, name);
4961
ret = settings_save_one(name, data, data_length);
5062
LOG_DBG("%s %s with %zu bytes. (%d)",
5163
(ret == 0) ? "Saved" : "Failed to save", name, data_length, ret);
@@ -81,10 +93,10 @@ psa_status_t secure_storage_its_store_get(secure_storage_its_uid_t uid, size_t d
8193
void *data, size_t *data_length)
8294
{
8395
psa_status_t ret;
84-
char name[NAME_BUF_SIZE];
96+
char name[SECURE_STORAGE_ITS_STORE_SETTINGS_NAME_BUF_SIZE];
8597
struct load_params load_params = {.data_size = data_size, .data = data, .ret = -ENOENT};
8698

87-
make_name(uid, name);
99+
secure_storage_its_store_settings_get_name(uid, name);
88100

89101
settings_load_subtree_direct(name, load_direct_setting, &load_params);
90102
if (load_params.ret > 0) {
@@ -103,9 +115,10 @@ psa_status_t secure_storage_its_store_get(secure_storage_its_uid_t uid, size_t d
103115
psa_status_t secure_storage_its_store_remove(secure_storage_its_uid_t uid)
104116
{
105117
int ret;
106-
char name[NAME_BUF_SIZE];
118+
char name[SECURE_STORAGE_ITS_STORE_SETTINGS_NAME_BUF_SIZE];
119+
120+
secure_storage_its_store_settings_get_name(uid, name);
107121

108-
make_name(uid, name);
109122
ret = settings_delete(name);
110123

111124
LOG_DBG("%s %s. (%d)", ret ? "Failed to delete" : "Deleted", name, ret);

0 commit comments

Comments
 (0)