1 /* Copyright (c) 2024 Nordic Semiconductor
2 * SPDX-License-Identifier: Apache-2.0
3 */
4 #include <psa/internal_trusted_storage.h>
5 #include <zephyr/logging/log.h>
6
7 LOG_MODULE_REGISTER(psa_its);
8
9 #define SAMPLE_DATA_UID (psa_storage_uid_t)1
10 #define SAMPLE_DATA_FLAGS PSA_STORAGE_FLAG_NONE
11
12 #ifdef CONFIG_SECURE_STORAGE
13 #define SAMPLE_DATA_SIZE CONFIG_SECURE_STORAGE_ITS_MAX_DATA_SIZE
14 #else
15 #define SAMPLE_DATA_SIZE 128
16 #endif
17
write_and_read_data(void)18 static int write_and_read_data(void)
19 {
20 LOG_INF("Writing to and reading back from ITS...");
21 psa_status_t ret;
22
23 /* Data to be written to ITS. */
24 uint8_t p_data_write[SAMPLE_DATA_SIZE];
25
26 for (unsigned int i = 0; i != sizeof(p_data_write); ++i) {
27 p_data_write[i] = i;
28 }
29
30 ret = psa_its_set(SAMPLE_DATA_UID, sizeof(p_data_write), p_data_write, SAMPLE_DATA_FLAGS);
31 if (ret != PSA_SUCCESS) {
32 LOG_ERR("Writing the data to ITS failed. (%d)", ret);
33 return -1;
34 }
35
36 /* Data to be read from ITS. */
37 uint8_t p_data_read[SAMPLE_DATA_SIZE];
38
39 /* Read back the data starting from an offset. */
40 const size_t data_offset = SAMPLE_DATA_SIZE / 2;
41
42 /* Number of bytes read. */
43 size_t p_data_length = 0;
44
45 ret = psa_its_get(SAMPLE_DATA_UID, data_offset, sizeof(p_data_read), p_data_read,
46 &p_data_length);
47 if (ret != PSA_SUCCESS) {
48 LOG_ERR("Reading back the data from ITS failed. (%d).", ret);
49 return -1;
50 }
51
52 if (p_data_length != SAMPLE_DATA_SIZE - data_offset) {
53 LOG_ERR("Unexpected amount of bytes read back. (%zu != %zu)",
54 p_data_length, SAMPLE_DATA_SIZE - data_offset);
55 return -1;
56 }
57
58 if (memcmp(p_data_write + data_offset, p_data_read, p_data_length)) {
59 LOG_HEXDUMP_INF(p_data_write + data_offset, p_data_length, "Data written:");
60 LOG_HEXDUMP_INF(p_data_read, p_data_length, "Data read back:");
61 LOG_ERR("The data read back doesn't match the data written.");
62 return -1;
63 }
64
65 LOG_INF("Successfully wrote to ITS and read back what was written.");
66 return 0;
67 }
68
read_info(void)69 static int read_info(void)
70 {
71 LOG_INF("Reading the written entry's metadata...");
72 psa_status_t ret;
73
74 /* The entry's metadata. */
75 struct psa_storage_info_t p_info;
76
77 ret = psa_its_get_info(SAMPLE_DATA_UID, &p_info);
78 if (ret != PSA_SUCCESS) {
79 LOG_ERR("Failed to retrieve the entry's metadata. (%d)", ret);
80 return -1;
81 }
82
83 if (p_info.capacity != SAMPLE_DATA_SIZE
84 || p_info.size != SAMPLE_DATA_SIZE
85 || p_info.flags != SAMPLE_DATA_FLAGS) {
86 LOG_ERR("Entry metadata unexpected. (capacity:%zu size:%zu flags:0x%x)",
87 p_info.capacity, p_info.size, p_info.flags);
88 return -1;
89 }
90
91 LOG_INF("Successfully read the entry's metadata.");
92 return 0;
93 }
94
remove_entry(void)95 static int remove_entry(void)
96 {
97 LOG_INF("Removing the entry from ITS...");
98 psa_status_t ret;
99
100 ret = psa_its_remove(SAMPLE_DATA_UID);
101 if (ret != PSA_SUCCESS) {
102 LOG_ERR("Failed to remove the entry. (%d)", ret);
103 return -1;
104 }
105
106 LOG_INF("Entry removed from ITS.");
107 return 0;
108 }
109
main(void)110 int main(void)
111 {
112 LOG_INF("PSA ITS sample started.");
113
114 if (write_and_read_data()) {
115 return -1;
116 }
117
118 if (read_info()) {
119 return -1;
120 }
121
122 if (remove_entry()) {
123 return -1;
124 }
125
126 LOG_INF("Sample finished successfully.");
127 return 0;
128 }
129