1 /* Copyright (c) 2024 Nordic Semiconductor
2  * SPDX-License-Identifier: Apache-2.0
3  */
4 #include <zephyr/secure_storage/its/store.h>
5 #include <zephyr/logging/log.h>
6 #include <zephyr/fs/zms.h>
7 #include <zephyr/storage/flash_map.h>
8 
9 LOG_MODULE_DECLARE(secure_storage, CONFIG_SECURE_STORAGE_LOG_LEVEL);
10 
11 BUILD_ASSERT(CONFIG_SECURE_STORAGE_ITS_STORE_ZMS_SECTOR_SIZE
12 	     > 2 * CONFIG_SECURE_STORAGE_ITS_MAX_DATA_SIZE);
13 
14 #define PARTITION_DT_NODE DT_CHOSEN(secure_storage_its_partition)
15 
16 static struct zms_fs s_zms = {
17 	.flash_device = FIXED_PARTITION_NODE_DEVICE(PARTITION_DT_NODE),
18 	.offset = FIXED_PARTITION_NODE_OFFSET(PARTITION_DT_NODE),
19 	.sector_size = CONFIG_SECURE_STORAGE_ITS_STORE_ZMS_SECTOR_SIZE,
20 };
21 
init_zms(void)22 static int init_zms(void)
23 {
24 	int ret;
25 
26 	s_zms.sector_count = FIXED_PARTITION_NODE_SIZE(PARTITION_DT_NODE) / s_zms.sector_size;
27 
28 	ret = zms_mount(&s_zms);
29 	if (ret) {
30 		LOG_DBG("Failed. (%d)", ret);
31 	}
32 	return ret;
33 }
34 SYS_INIT(init_zms, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);
35 
36 /* Bit position of the ITS caller ID in the ZMS entry ID. */
37 #define ITS_CALLER_ID_POS 30
38 /* Make sure that every ITS caller ID fits in ZMS entry IDs at the defined position. */
39 BUILD_ASSERT(1 << (32 - ITS_CALLER_ID_POS) >= SECURE_STORAGE_ITS_CALLER_COUNT);
40 
has_forbidden_bits_set(secure_storage_its_uid_t uid)41 static bool has_forbidden_bits_set(secure_storage_its_uid_t uid)
42 {
43 	if (uid.uid & GENMASK64(63, ITS_CALLER_ID_POS)) {
44 		LOG_DBG("UID %u/0x%llx cannot be used as it has bits set past "
45 			"the first " STRINGIFY(ITS_CALLER_ID_POS) " ones.",
46 			uid.caller_id, (unsigned long long)uid.uid);
47 		return true;
48 	}
49 	return false;
50 }
51 
zms_id_from(secure_storage_its_uid_t uid)52 static uint32_t zms_id_from(secure_storage_its_uid_t uid)
53 {
54 	return (uint32_t)uid.uid | (uid.caller_id << ITS_CALLER_ID_POS);
55 }
56 
secure_storage_its_store_set(secure_storage_its_uid_t uid,size_t data_length,const void * data)57 psa_status_t secure_storage_its_store_set(secure_storage_its_uid_t uid,
58 					  size_t data_length, const void *data)
59 {
60 	psa_status_t psa_ret;
61 	ssize_t zms_ret;
62 	const uint32_t zms_id = zms_id_from(uid);
63 
64 	if (has_forbidden_bits_set(uid)) {
65 		return PSA_ERROR_INVALID_ARGUMENT;
66 	}
67 
68 	zms_ret = zms_write(&s_zms, zms_id, data, data_length);
69 	if (zms_ret == data_length) {
70 		psa_ret = PSA_SUCCESS;
71 	} else if (zms_ret == -ENOSPC) {
72 		psa_ret = PSA_ERROR_INSUFFICIENT_STORAGE;
73 	} else {
74 		psa_ret = PSA_ERROR_STORAGE_FAILURE;
75 	}
76 	LOG_DBG("%s 0x%x with %zu bytes. (%zd)", (psa_ret == PSA_SUCCESS) ?
77 		"Wrote" : "Failed to write", zms_id, data_length, zms_ret);
78 	return psa_ret;
79 }
80 
secure_storage_its_store_get(secure_storage_its_uid_t uid,size_t data_size,void * data,size_t * data_length)81 psa_status_t secure_storage_its_store_get(secure_storage_its_uid_t uid, size_t data_size,
82 					  void *data, size_t *data_length)
83 {
84 	psa_status_t psa_ret;
85 	ssize_t zms_ret;
86 	const uint32_t zms_id = zms_id_from(uid);
87 
88 	if (has_forbidden_bits_set(uid)) {
89 		return PSA_ERROR_INVALID_ARGUMENT;
90 	}
91 
92 	zms_ret = zms_read(&s_zms, zms_id, data, data_size);
93 	if (zms_ret > 0) {
94 		*data_length = zms_ret;
95 		psa_ret = PSA_SUCCESS;
96 	} else if (zms_ret == -ENOENT) {
97 		psa_ret = PSA_ERROR_DOES_NOT_EXIST;
98 	} else {
99 		psa_ret = PSA_ERROR_STORAGE_FAILURE;
100 	}
101 	LOG_DBG("%s 0x%x for up to %zu bytes. (%zd)", (psa_ret != PSA_ERROR_STORAGE_FAILURE) ?
102 		"Read" : "Failed to read", zms_id, data_size, zms_ret);
103 	return psa_ret;
104 }
105 
secure_storage_its_store_remove(secure_storage_its_uid_t uid)106 psa_status_t secure_storage_its_store_remove(secure_storage_its_uid_t uid)
107 {
108 	int ret;
109 	const uint32_t zms_id = zms_id_from(uid);
110 
111 	if (has_forbidden_bits_set(uid)) {
112 		return PSA_ERROR_INVALID_ARGUMENT;
113 	}
114 
115 	ret = zms_delete(&s_zms, zms_id);
116 	LOG_DBG("%s 0x%x. (%d)", ret ? "Failed to delete" : "Deleted", zms_id, ret);
117 
118 	return ret ? PSA_ERROR_STORAGE_FAILURE : PSA_SUCCESS;
119 }
120