1 /*
2  * Copyright (c) 2021 Nordic Semiconductor ASA.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <string.h>
9 #include <zephyr/sys/printk.h>
10 #include <psa/storage_common.h>
11 #include <psa/protected_storage.h>
12 
13 #define TEST_STRING_1 "The quick brown fox jumps over the lazy dog"
14 #define TEST_STRING_2 "Lorem ipsum dolor sit amet"
15 
main(void)16 int main(void)
17 {
18 	psa_status_t status = 0;
19 
20 	printk("Protected Storage sample started.\n");
21 	printk("PSA Protected Storage API Version %d.%d\n",
22 			PSA_PS_API_VERSION_MAJOR, PSA_PS_API_VERSION_MINOR);
23 
24 	printk("Writing data to UID1: %s\n", TEST_STRING_1);
25 	psa_storage_uid_t uid1 = 1;
26 	psa_storage_create_flags_t uid1_flag = PSA_STORAGE_FLAG_NONE;
27 
28 	status = psa_ps_set(uid1, sizeof(TEST_STRING_1), TEST_STRING_1, uid1_flag);
29 	if (status != PSA_SUCCESS) {
30 		printk("Failed to store data! (%d)\n", status);
31 		return 0;
32 	}
33 
34 	/* Get info on UID1 */
35 	struct psa_storage_info_t uid1_info;
36 
37 	status = psa_ps_get_info(uid1, &uid1_info);
38 	if (status != PSA_SUCCESS) {
39 		printk("Failed to get info! (%d)\n", status);
40 		return 0;
41 	}
42 	printk("Info on data stored in UID1:\n");
43 	printk("- Size: %d\n", uid1_info.size);
44 	printk("- Capacity: 0x%2x\n", uid1_info.capacity);
45 	printk("- Flags: 0x%2x\n", uid1_info.flags);
46 
47 	printk("Read and compare data stored in UID1\n");
48 	size_t bytes_read;
49 	char stored_data[sizeof(TEST_STRING_1)];
50 
51 	status = psa_ps_get(uid1, 0, sizeof(TEST_STRING_1), &stored_data, &bytes_read);
52 	if (status != PSA_SUCCESS) {
53 		printk("Failed to get data stored in UID1! (%d)\n", status);
54 		return 0;
55 	}
56 	printk("Data stored in UID1: %s\n", stored_data);
57 
58 	printk("Overwriting data stored in UID1: %s\n", TEST_STRING_2);
59 	status = psa_ps_set(uid1, sizeof(TEST_STRING_2), TEST_STRING_2, uid1_flag);
60 	if (status != PSA_SUCCESS) {
61 		printk("Failed to overwrite UID1! (%d)\n", status);
62 		return 0;
63 	}
64 
65 	printk("Writing data to UID2 with overwrite protection: %s\n", TEST_STRING_1);
66 	psa_storage_uid_t uid2 = 2;
67 	psa_storage_create_flags_t uid2_flag = PSA_STORAGE_FLAG_WRITE_ONCE;
68 
69 	status = psa_ps_set(uid2, sizeof(TEST_STRING_1), TEST_STRING_1, uid2_flag);
70 	if (status != PSA_SUCCESS) {
71 		printk("Failed to set write once flag! (%d)\n", status);
72 		return 0;
73 	}
74 
75 	printk("Attempting to write '%s' to UID2\n", TEST_STRING_2);
76 	status = psa_ps_set(uid2, sizeof(TEST_STRING_2), TEST_STRING_2, uid2_flag);
77 	if (status != PSA_ERROR_NOT_PERMITTED) {
78 		printk("Got unexpected status when overwriting! (%d)\n", status);
79 		return 0;
80 	}
81 	printk("Got expected error (PSA_ERROR_NOT_PERMITTED) when writing to protected UID\n");
82 
83 	printk("Removing UID1\n");
84 	status = psa_ps_remove(uid1);
85 	if (status != PSA_SUCCESS) {
86 		printk("Failed to remove UID1! (%d)\n", status);
87 		return 0;
88 	}
89 	return 0;
90 }
91