Skip to content

Commit 7d418ec

Browse files
MarcusJGStreetsathoelke
authored andcommitted
Added the iterator and rename commands to Protected storage.
Signed-off-by: Marcus Streets <marcus.streets@arm.com>
1 parent ef8ae85 commit 7d418ec

2 files changed

Lines changed: 229 additions & 13 deletions

File tree

doc/storage/about.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@
4141

4242
Provide a Security Risk Assessment.
4343

44+
.. release:: 1.1.0
45+
:date: January 2024
46+
:confidentiality: Non-confidential
47+
48+
Added rename and iteration
49+
4450
.. release-info::
4551
:extend:
4652

doc/storage/api/api.rst

Lines changed: 223 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,19 @@ These definitions must be defined in the header file :file:`psa/storage_common.h
6363
.. field:: psa_storage_create_flags_t flags
6464
The flags set when the ``uid`` was create
6565

66+
.. struct:: psa_storage_iterator_t
6667

68+
.. summary::
69+
An implementation-defined opaque structure containing the context for an iterator.
70+
The structure MUST contain all all the state required by the iterator.
71+
That is, further state MUST NOT be retained by the implementation.
72+
73+
The structure is initilaised by the `ps_iterator_start()` function.
74+
It is modified by the `ps_iterator_next()` function.
75+
76+
the caller can discard or reuse the iterator object once it has finished using it. This can be before, or after, the iterator has reached the end of the iteration.
77+
78+
The header file is only required to define this structure if PSA_STORAGE_SUPPORT_ITERATION is true.
6779

6880
.. typedef:: uint32_t psa_storage_create_flags_t
6981

@@ -97,11 +109,35 @@ These definitions must be defined in the header file :file:`psa/storage_common.h
97109

98110
The data associated with the ``uid`` does not require replay protection. This can permit faster storage --- but it permits an attacker with physical access to revert to an earlier version of the data.
99111

112+
.. macro:: PSA_STORAGE_FLAG_REPLACE
113+
(1u << 3)
114+
115+
Flag instructing the `psa_ps_rename()` function to replace existing stored data.
116+
100117
.. macro:: PSA_STORAGE_SUPPORT_SET_EXTENDED
101118
(1u << 0)
102119

103120
Flag indicating that `psa_ps_create()` and `psa_ps_set_extended()` are supported.
104121

122+
123+
.. macro:: PSA_STORAGE_SUPPORT_RENAME
124+
(1u << 1)
125+
126+
Flag indicating that `psa_ps_rename()` is supported.
127+
128+
.. macro:: PSA_STORAGE_SUPPORT_ITERATION
129+
(1u << 2)
130+
131+
Flag indicating that `psa_ps_iterator_start()` and `psa_ps_iterator_next()` are supported.
132+
133+
.. macro:: PSA_STORAGE_ITERATOR_CTX__SIZE
134+
:definition: /* implementation-defined value */
135+
136+
.. summary::
137+
A sufficient buffer size for a iterator context, in bytes.
138+
This macro is only required if PSA_STORAGE_SUPPORT_ITERATION is true
139+
140+
105141
.. _ITS-API:
106142

107143
Internal Trusted Storage API
@@ -181,7 +217,7 @@ These definitions must be defined in the header file :file:`psa/internal_trusted
181217

182218
* The ``uid`` value must not be zero.
183219

184-
* If ``uid`` exists it must not have been created as with `PSA_STORAGE_FLAG_WRITE_ONCE` --- would result in ``PSA_ERROR_NOT_PERMITTED``
220+
* If ``uid`` exists it must not have been created as with `PSA_STORAGE_FLAG_WRITE_ONCE` --- would result in `PSA_ERROR_NOT_PERMITTED`
185221

186222
* The caller must have access all memory from ``p_data`` to ``p_data + data_length``.
187223

@@ -235,9 +271,9 @@ These definitions must be defined in the header file :file:`psa/internal_trusted
235271

236272
* The value of ``data_offset`` must be less than or equal to the length of ``uid``.
237273

238-
* If ``data_ffset`` is greater than ``uid.size``, no data is retrieved and the functions returns PSA_INVALID_ARGUMENT.
274+
* If ``data_offset`` is greater than ``uid.size``, no data is retrieved and the functions returns PSA_INVALID_ARGUMENT.
239275

240-
* If ``data_size`` is not zero, ``p_data`` must mot be ``NULL``.
276+
* If ``data_size`` is not zero, ``p_data`` must not be ``NULL``.
241277

242278
* The call must have access to the memory from ``p_data`` to ``p_data + data_size - 1``.
243279

@@ -353,14 +389,14 @@ These definitions must be defined in the header file :file:`psa/protected_storag
353389
.. function:: psa_ps_set
354390

355391
.. summary::
356-
Set the data associated with the specified ``uid``.
392+
Set the data associated with the specified ``uid``, replacing any previous data.
357393

358394
.. param:: psa_storage_uid_t uid
359395
The identifier for the data.
360396
.. param:: size_t data_length
361397
The size in bytes of the data in ``p_data``.
362398
If ``data_length == 0`` the implementation will create a zero-length asset associated with the ``uid``.
363-
While no data can be stored in such an asset, a call to `psa_ps_get_info()` will return ``PSA_SUCCESS``.
399+
While no data can be stored in such an asset, a call to `psa_ps_get_info()` will return `PSA_SUCCESS`.
364400
.. param:: const void * p_data
365401
A buffer containing the data.
366402
.. param:: psa_storage_create_flags_t create_flags
@@ -379,6 +415,9 @@ These definitions must be defined in the header file :file:`psa/protected_storag
379415
* The ``uid`` is ``0``.
380416

381417
* The operation failed because caller cannot access some or all of the memory in the range [``p_data``, ``p_data + data_length - 1``].
418+
419+
* the uid exists and ``data_length`` is greater then ```capacity``
420+
382421
.. retval:: PSA_ERROR_NOT_SUPPORTED
383422
The operation failed because one or more of the flags provided in ``create_flags`` is not supported or is not valid.
384423
.. retval:: PSA_ERROR_INSUFFICIENT_STORAGE
@@ -388,8 +427,9 @@ These definitions must be defined in the header file :file:`psa/protected_storag
388427
.. retval:: PSA_ERROR_GENERIC_ERROR
389428
The operation failed because of an unspecified internal failure.
390429

391-
The newly created asset has a capacity and size that are equal to ``data_length``.
430+
If ``uid`` does not already exist, creates a new asset, the newly created asset has a capacity and size that are equal to ``data_length``.
392431

432+
If ``uid`` exists and was not created with `PSA_STORAGE_FLAG_WRITE_ONCE`, replaces the existing contents with ``p_data``. ``uid.size`` is set to ``data_length``. If ``data_length`` is greater than ``uid.capcity``, ``uid.capcity`` is set to ``data_length``.
393433

394434
* The ``uid`` value must not be zero.
395435

@@ -454,7 +494,7 @@ These definitions must be defined in the header file :file:`psa/protected_storag
454494

455495
* If ``data_offset`` is greater than ``uid.size`` the function retrieves no data and returns ``PSA_ERROR_INVALID_ARGUMENT``
456496

457-
* If ``data_size`` is not zero, ``p_data`` must mot be ``NULL``.
497+
* If ``data_size`` is not zero, ``p_data`` must not be ``NULL``.
458498

459499
* The call must have access to the memory from ``p_data`` to ``p_data + data_size - 1``.
460500

@@ -508,6 +548,7 @@ These definitions must be defined in the header file :file:`psa/protected_storag
508548
* Even if all parameters are correct, the function can fail in the case of a storage failure.
509549

510550

551+
511552
.. function:: psa_ps_remove
512553

513554
.. summary::
@@ -542,6 +583,7 @@ These definitions must be defined in the header file :file:`psa/protected_storag
542583
* Even if all parameters are correct, the function can fail in the case of a storage failure.
543584

544585

586+
545587
.. function:: psa_ps_create
546588

547589
.. summary::
@@ -594,7 +636,7 @@ These definitions must be defined in the header file :file:`psa/protected_storag
594636
.. function:: psa_ps_set_extended
595637

596638
.. summary::
597-
Overwrite part of the data of the specified ``uid``.
639+
Overwrite part of the data of the specified ``uid``, leaving remaining data unchanged.
598640

599641
.. param:: psa_storage_uid_t uid
600642
The unique identifier for the asset.
@@ -638,13 +680,13 @@ These definitions must be defined in the header file :file:`psa/protected_storag
638680

639681
Calling this function with ``data_length == 0`` is permitted. This makes no change to the stored data.
640682

641-
This function can overwrite existing data and/or extend it up to the capacity for the ``uid`` specified in `psa_ps_create()` but cannot create gaps.
683+
This function can overwrite existing data and/or extend it up to the capacity for the ``uid`` specified in ``psa_ps_create()`` but cannot create gaps.
642684

643-
This function is optional. Consult the platform documentation to determine if it is implemented or perform a call to `psa_ps_get_support()`. This function must be implemented if `psa_ps_get_support()` returns `PSA_STORAGE_SUPPORT_SET_EXTENDED`.
685+
This function is optional. Consult the platform documentation to determine if it is implemented or perform a call to ``psa_ps_get_support()``. This function must be implemented if ``psa_ps_get_support()`` returns ``PSA_STORAGE_SUPPORT_SET_EXTENDED``.
644686

645687
* The ``uid`` value must not be zero.
646688

647-
* If ``uid`` exists it must not have been created as with `PSA_STORAGE_FLAG_WRITE_ONCE` - would result in ``PSA_ERROR_NOT_PERMITTED``
689+
* If ``uid`` exists it must not have been created as with ``PSA_STORAGE_FLAG_WRITE_ONCE`` - would result in ``PSA_ERROR_NOT_PERMITTED``
648690

649691
* ``data_offset <= size``
650692

@@ -658,15 +700,183 @@ These definitions must be defined in the header file :file:`psa/protected_storag
658700

659701
* ``capacity`` unchanged.
660702

703+
* Data in the ranges 0 to ``data_offset`` is not modified.
661704

705+
* If ``data_offset + data_length < size`` then data in the range ``data_offset + data_length` to `size`` is not modified.
662706

663-
.. function:: psa_ps_get_support
664707

665-
.. return:: uint32_t
708+
709+
.. function:: psa_ps_rename
710+
711+
.. summary::
712+
Atomically renames the storage location with the specified ``uid`` to a ``uid_new``.
713+
714+
.. param:: psa_storage_uid_t uid
715+
The current identifier for the data.
716+
717+
.. param:: psa_storage_uid_t uid_new
718+
The new identifier for the data.
719+
720+
.. param:: psa_storage_rename_flags_t rename_flags
721+
The flags must be either ``PSA_STORAGE_FLAG_NONE`` or ``PSA_STORAGE_FLAG_REPLACE``
722+
723+
.. return:: psa_status_t
724+
A status indicating the success or failure of the operation.
725+
726+
.. retval:: PSA_SUCCESS
727+
The operation completed successfully.
728+
729+
.. retval:: PSA_ERROR_ALREADY_EXISTS
730+
Storage with the specified ``uid_new`` already exists and ``rename_flags`` is `PSA_STORAGE_FLAG_NONE`
731+
732+
.. retval:: PSA_ERROR_DOES_NOT_EXIST
733+
Storage with the specified ``uid`` does not exist.
734+
735+
.. retval:: PSA_ERROR_GENERIC_ERROR
736+
The operation failed because of an unspecified internal failure.
737+
738+
.. retval:: PSA_ERROR_INVALID_ARGUMENT
739+
The operation failed because either:
740+
741+
* ``uid`` is ``0``.
742+
* ``uid_new`` is ``0``
743+
* the ``psa_storage_rename_flags_t`` has a value set other than `PSA_STORAGE_FLAG_REPLACE`
744+
745+
.. retval:: PSA_ERROR_NOT_PERMITTED
746+
The operation failed because ``uid_new`` exists and was created with `PSA_STORAGE_FLAG_WRITE_ONCE`.
747+
748+
.. retval:: PSA_ERROR_NOT_SUPPORTED
749+
The implementation does not support the operation.
750+
751+
.. retval:: PSA_ERROR_STORAGE_FAILURE
752+
The operation failed because the physical storage has failed (Fatal error).
753+
754+
755+
The function renames ``uid`` to ``uid_new`` retaining the storage flags that ``uid`` was created with.
756+
757+
If the caller specifies `PSA_STORAGE_FLAG_REPLACE` the operation atomically replaces the exisitng contents of ```uid_new`` with those of ``uid``.
758+
759+
Except in the case of ``PSA_ERROR_STORAGE_FAILURE``, in which case no guarantees can be made, the operation shall either succeed or leave storage unchanged.
760+
761+
762+
.. function:: psa_ps_iterator_start
763+
764+
.. summary::
765+
Initialises an iterator that can be used to return a list of uids in the Protected Storage .
766+
767+
.. param:: psa_storage_iterator_t* context
768+
A pointer to a context for this iterator. The pointer may be NULL. This is set to a new value on success and is undefined on error. The content of the iterator is implementation defined.
769+
770+
.. param:: psa_storage_uid_t filter
771+
A value used to filter the results included in this iteration.
772+
773+
.. param:: int_t filter_length
774+
A length of the filter to use, this must be a value ``0 < filter_lemngth < 63``.
775+
776+
.. param:: psa_storage_uid_t *result
777+
A pointer to the location in which to store ``uid``. On success the contents of this location will be updated with the first matching ``uid``. On error, the contents are undefined.
778+
779+
.. return:: psa_status_t
780+
A status indicating the success or failure of the operation.
781+
782+
.. retval:: PSA_SUCCESS
783+
The operation completed successfully.
784+
785+
.. retval:: PSA_ERROR_DOES_NOT_EXIST
786+
No ``uid`` matches this iteration.
787+
788+
.. retval:: PSA_ERROR_STORAGE_FAILURE
789+
The operation failed because the physical storage has failed (Fatal error).
790+
791+
The iterator returns those values where the ``filter_length`` bits of the ``uid`` matches the left most bits in ``filter``.
792+
793+
The iterator will only returns those ``uid`` that were created by the caller. It MUST not return any ``uid`` created by a different user.
794+
795+
An iterator is not required to return uids in any specific order, but MUST return them in a consistent order each time it is called. For example, if an implementation returns entries in numerical order, it should not arbitrarily change to returning them in creation order. However, the caller should not make assumptions as to the order in which entries are returned, except that each ``uid`` will be returned only once in each iteration.
796+
797+
Changes to storage by other users MUST NOT affect any open iterations.
798+
799+
A caller may initialize multiple iteration contexts at the same time. Each iteration shall be independent. Calling ``psa_ps_iterator_next()`` on one iterator MUST not effect any other open iteration.
800+
801+
An iterator MUST return all data objects whose ``uid`` matches the filter that are extant when the filter was created, unless these are deleted or renamed before the iteration would return them, or the caller stops before all matching objects have been returned.
802+
803+
A caller may delete a ``uid`` with `psa_ps_remove(uid)` without invalidating the iteration context. the iterator MUST never return a ``uid`` that has been deleted. However, if the caller is multi-threaded it ia possible another thread may delete a ``uid``.
804+
805+
A caller may read the contents of any ``uid`` with `psa_ps_get()` or write with `psa_ps_set(` or `psa_ps_set_extended()` without invalidating the iteration context.
806+
807+
A caller may create a ``uid`` with `psa_ps_set()` or `psa_ps_create()` without invalidating the iteration context. However, the iterator is NOT guaranteed to return the new object, ``uid``, the behavior is dependent on both implementation and identity. In particular, the iterator is not expected to return ``uid`` if the iteration is already past the point at which it would naturally be returned.
808+
809+
A caller may call `psa_ps_rename(uid, uid_new)` without invalidating the iteration context. The iterator must not return ``uid``. The iterator is not guaranteed to return ``uid_new``, the behavior is dependent on both implementation and identity.
810+
811+
The following code snippet uses a linked list to store the matching files before iterating over that list and removing them.
812+
813+
.. code-block:: c
814+
815+
my_context = NULL
816+
my_filter = 0x1111 0000 0000 0000
817+
my_length = 0x0020
818+
my_result = NULL
819+
if psa_ps_iterator_start(my_context, my_filter, my-length, my_result) == PSA_SUCCESS
820+
{
821+
do
822+
{
823+
// do something with my_result
824+
psa_ps_iterator_next(my_context, my_result)
825+
// we will get an does not exist error when we reach the last item, any other error is a storage failure
826+
if my_reult <> PSA_ERROR_DOES_NOT_EXIST
827+
{
828+
/* deal with storage failure */
829+
}
830+
}
831+
while my_result == PSA_SUCCES ;
832+
};
833+
834+
835+
836+
837+
.. function:: psa_ps_iterator_next
838+
839+
.. summary::
840+
841+
Returns the next ``uid`` in this iteration.
842+
843+
.. param:: psa_storage_iterator_t* context
844+
A pointer to a context for this iterator as returned by `psa_ps_iterator_start()` or updated by a previous call to `psa_ps_iterator_next()`. The content of the iterator will change on success and is undefined on error.
845+
846+
.. param:: psa_storage_uid_t *result
847+
A pointer to the location in which to store ``uid``. On success the contents of this location will be updated with the next matching ``uid``. On error, the contents are undefined.
848+
849+
.. return:: psa_status_t
850+
A status indicating the success or failure of the operation.
851+
852+
.. retval:: PSA_SUCCESS
853+
The operation completed successfully.
854+
855+
.. retval:: PSA_ERROR_DOES_NOT_EXIST
856+
The iterator has returned all the uids that match this iteration.
857+
858+
.. retval:: PSA_ERROR_STORAGE_FAILURE
859+
The operation failed because the physical storage has failed (Fatal error).
860+
861+
.. retval:: PSA_ERROR_DATA_CORRUPT
862+
The operation failed because the contents of the iteration have changed. That is a ``uid`` matching the filter has either been created or deleted.
863+
864+
.. retval:: PSA_ERROR_INVALID_ARGUMENT
865+
The operation failed because either:
866+
867+
* The provided context is not valid.
868+
869+
* The caller cannot access the memory at `result`
870+
871+
.. function:: psa_ps_get_support
666872

667873
.. summary::
668874
Returns a bitmask with flags set for the optional features supported by the implementation.
669875

670876
Currently defined flags are limited to:
671877

672878
* `PSA_STORAGE_SUPPORT_SET_EXTENDED`
879+
* `PSA_STORAGE_SUPPORT_RENAME`
880+
* `PSA_STORAGE_SUPPORT_ITERATION`
881+
882+
.. return:: uint32_t

0 commit comments

Comments
 (0)