1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
|
/*
* Copyright (c) 2017-2021, Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*
*/
#include "ps_object_system.h"
#include <stddef.h>
#include <string.h>
#include "cmsis_compiler.h"
#include "psa/internal_trusted_storage.h"
#ifdef PS_ENCRYPTION
#include "ps_encrypted_object.h"
#endif
#include "ps_object_defs.h"
#include "ps_object_table.h"
#include "ps_utils.h"
#include "tfm_ps_req_mngr.h"
#ifndef PS_ENCRYPTION
/* Gets the size of object written to the object system below */
#define PS_OBJECT_SIZE(max_size) (PS_OBJECT_HEADER_SIZE + (max_size))
#define PS_OBJECT_START_POSITION 0
#endif /* PS_ENCRYPTION */
/* Allocate static variables to process objects */
static struct ps_object_t g_ps_object;
static struct ps_obj_table_info_t g_obj_tbl_info;
/**
* \brief Initialize g_ps_object based on the input parameters and empty data.
*
* \param[in] create_flags Object create flags
* \param[in] size Object size
* \param[out] obj Object to initialize
*
*/
__attribute__ ((always_inline))
__STATIC_INLINE void ps_init_empty_object(
psa_storage_create_flags_t create_flags,
uint32_t size,
struct ps_object_t *obj)
{
/* Set all object data to 0 */
(void)memset(obj, PS_DEFAULT_EMPTY_BUFF_VAL, PS_MAX_OBJECT_SIZE);
#ifndef PS_ENCRYPTION
/* Initialize object version */
obj->header.version = 0;
#endif
/* Set object header based on input parameters */
obj->header.info.max_size = size;
obj->header.info.create_flags = create_flags;
}
/**
* \brief Removes the old object table and object from the file system.
*
* \param[in] old_fid Old file ID to remove.
*
* \return Returns error code as specified in \ref psa_status_t
*/
static psa_status_t ps_remove_old_data(uint32_t old_fid)
{
psa_status_t err;
/* Delete old object table from the persistent area */
err = ps_object_table_delete_old_table();
if (err != PSA_SUCCESS) {
return err;
}
/* Delete old file from the persistent area */
return psa_its_remove(old_fid);
}
#ifndef PS_ENCRYPTION
enum read_type_t {
READ_HEADER_ONLY = 0,
READ_ALL_OBJECT,
};
/**
* \brief Reads and validates an object header based on its object table info
* stored in g_obj_tbl_info.
*
* \param[in] type Read type as specified in \ref read_type_t
*
* \return Returns error code as specified in \ref psa_status_t
*/
static psa_status_t ps_read_object(enum read_type_t type)
{
psa_status_t err;
size_t data_length;
/* Read object header */
err = psa_its_get(g_obj_tbl_info.fid,
PS_OBJECT_START_POSITION,
PS_OBJECT_HEADER_SIZE,
(void *)&g_ps_object.header,
&data_length);
if (err != PSA_SUCCESS) {
return err;
}
/* As PS encryption support is not enabled, check file ID and version to
* detect inconsistency after read the object header from flash.
*/
if (g_ps_object.header.fid != g_obj_tbl_info.fid ||
g_ps_object.header.version != g_obj_tbl_info.version) {
return PSA_ERROR_DATA_CORRUPT;
}
/* Read object data if any */
if (type == READ_ALL_OBJECT && g_ps_object.header.info.current_size > 0) {
err = psa_its_get(g_obj_tbl_info.fid,
PS_OBJECT_HEADER_SIZE,
g_ps_object.header.info.current_size,
(void *)g_ps_object.data,
&data_length);
if (err != PSA_SUCCESS) {
return err;
}
}
return PSA_SUCCESS;
}
/**
* \brief Writes an object based on its object table info stored in
* g_obj_tbl_info and the input parameter.
*
* \param[in] wrt_size Number of bytes to write
*
* \return Returns error code as specified in \ref psa_status_t
*/
static psa_status_t ps_write_object(uint32_t wrt_size)
{
/* Add object identification and increase object version */
g_ps_object.header.fid = g_obj_tbl_info.fid;
g_ps_object.header.version++;
/* Save object version to be stored in the object table */
g_obj_tbl_info.version = g_ps_object.header.version;
return psa_its_set(g_obj_tbl_info.fid, wrt_size,
(const void *)&g_ps_object,
PSA_STORAGE_FLAG_NONE);
}
#endif /* !PS_ENCRYPTION */
psa_status_t ps_system_prepare(void)
{
psa_status_t err;
/* Reuse the allocated g_ps_object.data to store a temporary object table
* data to be validate inside the function.
* The stored date will be cleaned up when the g_ps_object.data will
* be used for the first time in the object system.
*/
err = ps_object_table_init(g_ps_object.data);
#ifdef PS_ENCRYPTION
g_obj_tbl_info.tag = g_ps_object.header.crypto.ref.tag;
#endif
return err;
}
psa_status_t ps_object_read(psa_storage_uid_t uid, int32_t client_id,
uint32_t offset, uint32_t size,
size_t *p_data_length)
{
psa_status_t err;
/* Retrieve the object information from the object table if the object
* exists.
*/
err = ps_object_table_get_obj_tbl_info(uid, client_id, &g_obj_tbl_info);
if (err != PSA_SUCCESS) {
return err;
}
/* Read object */
#ifdef PS_ENCRYPTION
g_ps_object.header.crypto.ref.uid = uid;
g_ps_object.header.crypto.ref.client_id = client_id;
err = ps_encrypted_object_read(g_obj_tbl_info.fid, &g_ps_object);
#else
/* Read object header */
err = ps_read_object(READ_ALL_OBJECT);
#endif
if (err != PSA_SUCCESS) {
goto clear_data_and_return;
}
/* Boundary check the incoming request */
if (offset > g_ps_object.header.info.current_size) {
err = PSA_ERROR_INVALID_ARGUMENT;
goto clear_data_and_return;
}
size = PS_UTILS_MIN(size,
g_ps_object.header.info.current_size - offset);
/* Copy the decrypted object data to the output buffer */
ps_req_mngr_write_asset_data(g_ps_object.data + offset, size);
*p_data_length = size;
clear_data_and_return:
/* Remove data stored in the object before leaving the function */
(void)memset(&g_ps_object, PS_DEFAULT_EMPTY_BUFF_VAL,
PS_MAX_OBJECT_SIZE);
return err;
}
psa_status_t ps_object_create(psa_storage_uid_t uid, int32_t client_id,
psa_storage_create_flags_t create_flags,
uint32_t size)
{
psa_status_t err;
uint32_t old_fid = PS_INVALID_FID;
uint32_t fid_am_reserved = 1;
#ifndef PS_ENCRYPTION
uint32_t wrt_size;
#endif
/* Boundary check the incoming request */
if (size > PS_MAX_ASSET_SIZE) {
return PSA_ERROR_INVALID_ARGUMENT;
}
/* Retrieve the object information from the object table if the object
* exists.
*/
err = ps_object_table_get_obj_tbl_info(uid, client_id, &g_obj_tbl_info);
if (err == PSA_SUCCESS) {
#ifdef PS_ENCRYPTION
/* Read the object */
g_ps_object.header.crypto.ref.uid = uid;
g_ps_object.header.crypto.ref.client_id = client_id;
err = ps_encrypted_object_read(g_obj_tbl_info.fid, &g_ps_object);
#else
/* Read the object header */
err = ps_read_object(READ_HEADER_ONLY);
#endif
if (err != PSA_SUCCESS) {
goto clear_data_and_return;
}
/* If the object exists and has the write once flag set, then it cannot
* be modified.
*/
if (g_ps_object.header.info.create_flags
& PSA_STORAGE_FLAG_WRITE_ONCE) {
err = PSA_ERROR_NOT_PERMITTED;
goto clear_data_and_return;
}
/* Update the create flags and max object size */
g_ps_object.header.info.create_flags = create_flags;
g_ps_object.header.info.max_size = size;
/* Save old file ID */
old_fid = g_obj_tbl_info.fid;
} else if (err == PSA_ERROR_DOES_NOT_EXIST) {
/* If the object does not exist, then initialize it based on the input
* arguments and empty content. Requests 2 FIDs to prevent exhaustion.
*/
fid_am_reserved = 2;
ps_init_empty_object(create_flags, size, &g_ps_object);
} else {
goto clear_data_and_return;
}
/* Update the object data */
err = ps_req_mngr_read_asset_data(g_ps_object.data, size);
if (err != PSA_SUCCESS) {
goto clear_data_and_return;
}
/* Update the current object size */
g_ps_object.header.info.current_size = size;
/* Get new file ID */
err = ps_object_table_get_free_fid(fid_am_reserved,
&g_obj_tbl_info.fid);
if (err != PSA_SUCCESS) {
goto clear_data_and_return;
}
#ifdef PS_ENCRYPTION
g_ps_object.header.crypto.ref.uid = uid;
g_ps_object.header.crypto.ref.client_id = client_id;
err = ps_encrypted_object_write(g_obj_tbl_info.fid, &g_ps_object);
#else
wrt_size = PS_OBJECT_SIZE(g_ps_object.header.info.current_size);
/* Write g_ps_object */
err = ps_write_object(wrt_size);
#endif
if (err != PSA_SUCCESS) {
goto clear_data_and_return;
}
/* Update the table with the new internal ID and version for the object, and
* store it in the persistent area.
*/
err = ps_object_table_set_obj_tbl_info(uid, client_id, &g_obj_tbl_info);
if (err != PSA_SUCCESS) {
/* Remove new object as object table is not persistent and propagate
* object table manipulation error.
*/
(void)psa_its_remove(g_obj_tbl_info.fid);
goto clear_data_and_return;
}
if (old_fid == PS_INVALID_FID) {
/* Delete old object table from the persistent area */
err = ps_object_table_delete_old_table();
} else {
/* Remove old object and delete old object table */
err = ps_remove_old_data(old_fid);
}
clear_data_and_return:
/* Remove data stored in the object before leaving the function */
(void)memset(&g_ps_object, PS_DEFAULT_EMPTY_BUFF_VAL, PS_MAX_OBJECT_SIZE);
return err;
}
psa_status_t ps_object_write(psa_storage_uid_t uid, int32_t client_id,
uint32_t offset, uint32_t size)
{
psa_status_t err;
uint32_t old_fid;
#ifndef PS_ENCRYPTION
uint32_t wrt_size;
#endif
/* Retrieve the object information from the object table if the object
* exists.
*/
err = ps_object_table_get_obj_tbl_info(uid, client_id, &g_obj_tbl_info);
if (err != PSA_SUCCESS) {
return err;
}
/* Read the object */
#ifdef PS_ENCRYPTION
g_ps_object.header.crypto.ref.uid = uid;
g_ps_object.header.crypto.ref.client_id = client_id;
err = ps_encrypted_object_read(g_obj_tbl_info.fid, &g_ps_object);
#else
err = ps_read_object(READ_ALL_OBJECT);
#endif
if (err != PSA_SUCCESS) {
goto clear_data_and_return;
}
/* If the object has the write once flag set, then it cannot be modified. */
if (g_ps_object.header.info.create_flags & PSA_STORAGE_FLAG_WRITE_ONCE) {
err = PSA_ERROR_NOT_PERMITTED;
goto clear_data_and_return;
}
/* Offset must not be larger than the object's current size to prevent gaps
* being created in the object data.
*/
if (offset > g_ps_object.header.info.current_size) {
err = PSA_ERROR_INVALID_ARGUMENT;
goto clear_data_and_return;
}
/* Boundary check the incoming request */
err = ps_utils_check_contained_in(g_ps_object.header.info.max_size,
offset, size);
if (err != PSA_SUCCESS) {
goto clear_data_and_return;
}
/* Update the object data */
err = ps_req_mngr_read_asset_data(g_ps_object.data + offset, size);
if (err != PSA_SUCCESS) {
goto clear_data_and_return;
}
/* Update the current object size if necessary */
if ((offset + size) > g_ps_object.header.info.current_size) {
g_ps_object.header.info.current_size = offset + size;
}
/* Save old file ID */
old_fid = g_obj_tbl_info.fid;
/* Get new file ID */
err = ps_object_table_get_free_fid(1, &g_obj_tbl_info.fid);
if (err != PSA_SUCCESS) {
goto clear_data_and_return;
}
#ifdef PS_ENCRYPTION
g_ps_object.header.crypto.ref.uid = uid;
g_ps_object.header.crypto.ref.client_id = client_id;
err = ps_encrypted_object_write(g_obj_tbl_info.fid, &g_ps_object);
#else
wrt_size = PS_OBJECT_SIZE(g_ps_object.header.info.current_size);
/* Write g_ps_object */
err = ps_write_object(wrt_size);
#endif
if (err != PSA_SUCCESS) {
goto clear_data_and_return;
}
/* Update the table with the new internal ID and version for the object, and
* store it in the persistent area.
*/
err = ps_object_table_set_obj_tbl_info(uid, client_id, &g_obj_tbl_info);
if (err != PSA_SUCCESS) {
/* Remove new object as object table is not persistent and propagate
* object table manipulation error.
*/
(void)psa_its_remove(g_obj_tbl_info.fid);
goto clear_data_and_return;
}
/* Remove old object table and object */
err = ps_remove_old_data(old_fid);
clear_data_and_return:
/* Remove data stored in the object before leaving the function */
(void)memset(&g_ps_object, PS_DEFAULT_EMPTY_BUFF_VAL,
PS_MAX_OBJECT_SIZE);
return err;
}
psa_status_t ps_object_get_info(psa_storage_uid_t uid, int32_t client_id,
struct psa_storage_info_t *info)
{
psa_status_t err;
/* Retrieve the object information from the object table if the object
* exists.
*/
err = ps_object_table_get_obj_tbl_info(uid, client_id, &g_obj_tbl_info);
if (err != PSA_SUCCESS) {
return err;
}
#ifdef PS_ENCRYPTION
g_ps_object.header.crypto.ref.uid = uid;
g_ps_object.header.crypto.ref.client_id = client_id;
err = ps_encrypted_object_read(g_obj_tbl_info.fid, &g_ps_object);
#else
err = ps_read_object(READ_HEADER_ONLY);
#endif
if (err != PSA_SUCCESS) {
goto clear_data_and_return;
}
/* Copy PS object info to the PSA PS info struct */
info->size = g_ps_object.header.info.current_size;
info->flags = g_ps_object.header.info.create_flags;
clear_data_and_return:
/* Remove data stored in the object before leaving the function */
(void)memset(&g_ps_object, PS_DEFAULT_EMPTY_BUFF_VAL,
PS_MAX_OBJECT_SIZE);
return err;
}
psa_status_t ps_object_delete(psa_storage_uid_t uid, int32_t client_id)
{
psa_status_t err;
/* Retrieve the object information from the object table if the object
* exists.
*/
err = ps_object_table_get_obj_tbl_info(uid, client_id, &g_obj_tbl_info);
if (err != PSA_SUCCESS) {
return err;
}
#ifdef PS_ENCRYPTION
g_ps_object.header.crypto.ref.uid = uid;
g_ps_object.header.crypto.ref.client_id = client_id;
err = ps_encrypted_object_read(g_obj_tbl_info.fid, &g_ps_object);
#else
err = ps_read_object(READ_HEADER_ONLY);
#endif
if (err != PSA_SUCCESS) {
goto clear_data_and_return;
}
/* Check that the write once flag is not set */
if (g_ps_object.header.info.create_flags & PSA_STORAGE_FLAG_WRITE_ONCE) {
err = PSA_ERROR_NOT_PERMITTED;
goto clear_data_and_return;
}
/* Delete object from the table and stores the table in the persistent
* area.
*/
err = ps_object_table_delete_object(uid, client_id);
if (err != PSA_SUCCESS) {
goto clear_data_and_return;
}
/* Remove old object table and file */
err = ps_remove_old_data(g_obj_tbl_info.fid);
clear_data_and_return:
/* Remove data stored in the object before leaving the function */
(void)memset(&g_ps_object, PS_DEFAULT_EMPTY_BUFF_VAL,
PS_MAX_OBJECT_SIZE);
return err;
}
psa_status_t ps_system_wipe_all(void)
{
/* This function may get called as a corrective action
* if a system level security violation is detected.
* This could be asynchronous to normal system operation
* and state of the ps system lock is unknown. Hence
* this function doesn't block on the lock and directly
* moves to erasing the flash instead.
*/
return ps_object_table_create();
}
|