1 /*
2  * Copyright (c) 2019-2020, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2020-01-10     Kevin/Karl   Add PS demo
9  *
10  */
11 
12 #include <rtdevice.h>
13 #include <string.h>
14 #include "tfm_ns_lock.h"
15 #include "psa_protected_storage.h"
16 
17 #define TEST_UID_A      2U
18 #define ASSET_A         "THEQUICKBROWNFOXJUMPSOVERALAZYDOG"
19 #define ASSET_A_SIZE    (sizeof( ASSET_A ) - 1)
20 #define RESETDATA       "THISIS"
21 #define RESETDATA_SIZE  (sizeof( RESETDATA ) - 1)
22 #define READ_LENGTH     (ASSET_A_SIZE > RESETDATA_SIZE ? \
23                          ASSET_A_SIZE : RESETDATA_SIZE)
24 
protected_storage_demo_thread(void * parameters)25 void protected_storage_demo_thread(void * parameters)
26 {
27     psa_ps_status_t status;
28     const psa_ps_uid_t uid = TEST_UID_A;
29     const psa_ps_create_flags_t flags = PSA_PS_FLAG_NONE;
30     uint8_t write_data[] = ASSET_A;
31     const uint32_t data_length = ASSET_A_SIZE;
32     uint8_t rewrite_data[] = RESETDATA;
33     const uint32_t reset_data_length = RESETDATA_SIZE;
34     uint8_t get_data[READ_LENGTH];
35     uint32_t counter = 0;
36 
37     tfm_ns_lock_init();
38 
39     for ( ; ; )
40     {
41         /* Call TF-M protected storage service and set the asset. */
42         status = psa_ps_set(uid, data_length, write_data, flags);
43         if (status != PSA_PS_SUCCESS)
44         {
45             rt_kprintf("[Protected Storage Asset A Set Round %ld] Fail\r\n", counter);
46             for( ; ; );
47         }
48 
49         rt_kprintf("[Protected Storage Asset A Set Round %ld] Success\r\n", counter);
50 
51         /* Read the asset. */
52         status = psa_ps_get(uid, 0, data_length, get_data);
53         if (status != PSA_PS_SUCCESS)
54         {
55             rt_kprintf("[Protected Storage Asset A Get Round %ld] Fail\r\n", counter);
56             for ( ; ; );
57         }
58 
59         rt_kprintf("[Protected Storage Asset A Get Round %ld] Success\r\n", counter);
60 
61         /* Check the read data. */
62         if (memcmp(write_data, get_data, sizeof(write_data) - 1) != 0)
63         {
64             rt_kprintf("[Protected Storage Asset A Get Round %ld] Get the wrong data\r\n", counter);
65             for ( ; ; );
66         }
67 
68         /* Change the asset. */
69         status = psa_ps_set(uid, reset_data_length, rewrite_data, flags);
70         if (status != PSA_PS_SUCCESS)
71         {
72             rt_kprintf("[Protected Storage Asset A Reset Round %ld] Fail\r\n", counter);
73         }
74 
75         rt_kprintf("[Protected Storage Asset A Reset Round %ld] Success\r\n", counter);
76 
77         /* Read the asset. */
78         status = psa_ps_get(uid, 0, reset_data_length, get_data);
79         if (status != PSA_PS_SUCCESS)
80         {
81             rt_kprintf("[Protected Storage Asset A Get Round %ld] Fail\r\n", counter);
82             for ( ; ; );
83         }
84 
85         rt_kprintf("[Protected Storage Asset A Get Round %ld] Success\r\n", counter);
86 
87         /* Check the read data. */
88         if (memcmp(rewrite_data, get_data, sizeof(rewrite_data) - 1) != 0)
89         {
90             rt_kprintf("[Protected Storage Asset A Get Round %ld] Get the wrong data\r\n", counter);
91             for ( ; ; );
92         }
93 
94         /* Remove the asset. */
95         status = psa_ps_remove(uid);
96         if (status != PSA_PS_SUCCESS)
97         {
98             rt_kprintf("[Protected Storage Asset A Remove Round %ld] Fail\r\n", counter);
99             for ( ; ; );
100         }
101 
102         rt_kprintf("[Protected Storage Asset A Remove Round %ld] Success\r\n\n", counter);
103 
104         /* Wait for a second. */
105         rt_thread_mdelay(1000);
106         counter++;
107     }
108 }
109 
110 // end file
111