Jamie Fox | efb8b37 | 2018-12-14 14:52:15 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #ifndef __PSA_PROTECTED_STORAGE_H__ |
| 9 | #define __PSA_PROTECTED_STORAGE_H__ |
| 10 | |
| 11 | /** @file |
| 12 | @brief This file describes the PSA Protected Storage API |
| 13 | */ |
| 14 | |
| 15 | #include <stddef.h> |
| 16 | #include <stdint.h> |
| 17 | |
| 18 | #ifdef __cplusplus |
| 19 | extern "C" { |
| 20 | #endif |
| 21 | |
| 22 | #define PSA_PS_API_VERSION_MAJOR 1 /**< The major version number of the PSA PS API. It will be incremented on significant updates that may include breaking changes */ |
| 23 | #define PSA_PS_API_VERSION_MINOR 0 /**< The minor version number of the PSA PS API. It will be incremented in small updates that are unlikely to include breaking changes */ |
| 24 | |
| 25 | /** \brief Flags used when creating a data entry |
| 26 | */ |
| 27 | typedef uint32_t psa_ps_create_flags_t; |
| 28 | |
| 29 | /** \brief A type for UIDs used for identifying data |
| 30 | */ |
| 31 | typedef uint64_t psa_ps_uid_t; |
| 32 | |
| 33 | #define PSA_PS_FLAG_NONE 0 /**< No flags to pass */ |
| 34 | #define PSA_PS_FLAG_WRITE_ONCE ( 1 << 0 ) /**< The data associated with the uid will not be able to be modified or deleted. Intended to be used to set bits in `psa_ps_create_flags_t`*/ |
| 35 | |
| 36 | /** |
| 37 | * \brief A container for metadata associated with a specific uid |
| 38 | */ |
| 39 | struct psa_ps_info_t { |
| 40 | uint32_t size; /**< The size of the data associated with a uid **/ |
| 41 | psa_ps_create_flags_t flags; /**< The flags set when the uid was created **/ |
| 42 | }; |
| 43 | /** |
| 44 | * \brief The return status type for the PSA Trusted Storage functions |
| 45 | */ |
| 46 | typedef uint32_t psa_ps_status_t; |
| 47 | |
| 48 | #define PSA_PS_SUCCESS 0 /**< The operation completed successfully */ |
| 49 | #define PSA_PS_ERROR_WRITE_ONCE 1 /**< The operation failed because the provided key value was already created with PSA_PS_WRITE_ONCE_FLAG */ |
| 50 | #define PSA_PS_ERROR_FLAGS_NOT_SUPPORTED 2 /**< The operation failed because one or more of the flags provided in `create_flags` is not supported or is not valid */ |
| 51 | #define PSA_PS_ERROR_INSUFFICIENT_SPACE 3 /**< The operation failed because there was insufficient space on the storage medium */ |
| 52 | #define PSA_PS_ERROR_STORAGE_FAILURE 4 /**< The operation failed because the physical storage has failed (Fatal error) */ |
| 53 | #define PSA_PS_ERROR_UID_NOT_FOUND 5 /**< The operation failed because the provided key value was not found in the storage */ |
| 54 | #define PSA_PS_ERROR_INCORRECT_SIZE 6 /**< The operation failed because the data associated with provided key is not the same size as `data_size`, or `offset+data_size` is too large for the data, but `offset` is less than the size */ |
| 55 | #define PSA_PS_ERROR_OFFSET_INVALID 7 /**< The operation failed because an offset was supplied that is invalid for the existing data associated with the uid. For example, offset is greater that the size of the data */ |
| 56 | #define PSA_PS_ERROR_INVALID_ARGUMENT 8 /**< The operation failed because one or more of the given arguments were invalid (null pointer, wrong flags etc.) */ |
| 57 | #define PSA_PS_ERROR_DATA_CORRUPT 9 /**< The operation failed because data was corrupt when attempting to get the key */ |
| 58 | #define PSA_PS_ERROR_AUTH_FAILED 10 /**< The operation failed because of an authentication failure when attempting to get the key */ |
| 59 | #define PSA_PS_ERROR_OPERATION_FAILED 11 /**< The operation failed because of an unspecified/internal failure */ |
| 60 | #define PSA_PS_ERROR_NOT_SUPPORTED 12 /**< The returning function is not supported in this implementation of the API */ |
| 61 | |
| 62 | /** Flag indicating that \ref psa_ps_create and \ref psa_ps_set_extended are supported */ |
| 63 | #define PSA_PS_SUPPORT_SET_EXTENDED (1 << 0) |
| 64 | |
| 65 | /** |
| 66 | * \brief create a new or modify an existing key/value pair |
| 67 | * |
| 68 | * \param[in] uid the identifier for the data |
| 69 | * \param[in] data_length The size in bytes of the data in `p_data` |
| 70 | * \param[in] p_data A buffer containing the data |
| 71 | * \param[in] create_flags The flags indicating the properties of the data |
| 72 | * |
| 73 | * \return A status indicating the success/failure of the operation |
| 74 | |
| 75 | * \retval PSA_PS_SUCCESS The operation completed successfully |
| 76 | * \retval PSA_PS_ERROR_WRITE_ONCE The operation failed because the provided uid value was already created with PSA_PS_WRITE_ONCE_FLAG |
| 77 | * \retval PSA_PS_ERROR_INVALID_ARGUMENT The operation failed because one or more of the given arguments were invalid. |
| 78 | * \retval PSA_PS_ERROR_FLAGS_NOT_SUPPORTED The operation failed because one or more of the flags provided in `create_flags` is not supported or is not valid |
| 79 | * \retval PSA_PS_ERROR_INSUFFICIENT_SPACE The operation failed because there was insufficient space on the storage medium |
| 80 | * \retval PSA_PS_ERROR_STORAGE_FAILURE The operation failed because the physical storage has failed (Fatal error) |
| 81 | * \retval PSA_PS_ERROR_OPERATION_FAILED The operation failed because of an unspecified internal failure |
| 82 | */ |
| 83 | psa_ps_status_t psa_ps_set( psa_ps_uid_t uid, |
| 84 | uint32_t data_length, |
| 85 | const void *p_data, |
| 86 | psa_ps_create_flags_t create_flags ); |
| 87 | |
| 88 | /** |
| 89 | * \brief Retrieve the value for a provided uid |
| 90 | * |
| 91 | * \param[in] uid The identifier for the data |
| 92 | * \param[in] data_offset The offset within the data associated with the `uid` to start retrieving data |
| 93 | * \param[in] data_length The amount of data to read (and the minimum allocated size of the `p_data` buffer) |
| 94 | * \param[out] p_data The buffer where the data will be placed upon successful completion |
| 95 | * |
| 96 | * \return A status indicating the success/failure of the operation |
| 97 | * |
| 98 | * \retval PSA_PS_SUCCESS The operation completed successfully |
| 99 | * \retval PSA_PS_ERROR_INVALID_ARGUMENT The operation failed because one or more of the given arguments were invalid (null pointer, wrong flags etc.) |
| 100 | * \retval PSA_PS_ERROR_UID_NOT_FOUND The operation failed because the provided uid value was not found in the storage |
| 101 | * \retval PSA_PS_ERROR_INCORRECT_SIZE The operation failed because the data associated with provided uid is not the same size as `data_size` |
| 102 | * \retval PSA_PS_ERROR_STORAGE_FAILURE The operation failed because the physical storage has failed (Fatal error) |
| 103 | * \retval PSA_PS_ERROR_OPERATION_FAILED The operation failed because of an unspecified internal failure |
| 104 | * \retval PSA_PS_ERROR_DATA_CORRUPT The operation failed because of an authentication failure when attempting to get the key |
| 105 | * \retval PSA_PS_ERROR_AUTH_FAILED The operation failed because the data associated with the UID failed authentication |
| 106 | */ |
| 107 | psa_ps_status_t psa_ps_get( psa_ps_uid_t uid, |
| 108 | uint32_t data_offset, |
| 109 | uint32_t data_length, |
| 110 | void *p_data ); |
| 111 | |
| 112 | /** |
| 113 | * \brief Retrieve the metadata about the provided uid |
| 114 | * |
| 115 | * \param[in] uid The identifier for the data |
| 116 | * \param[out] p_info A pointer to the `psa_ps_info_t` struct that will be populated with the metadata |
| 117 | * |
| 118 | * \return A status indicating the success/failure of the operation |
| 119 | * |
| 120 | * \retval PSA_PS_SUCCESS The operation completed successfully |
| 121 | * \retval PSA_PS_ERROR_INVALID_ARGUMENT The operation failed because one or more of the given arguments were invalid (null pointer, wrong flags etc.) |
| 122 | * \retval PSA_PS_ERROR_UID_NOT_FOUND The operation failed because the provided uid value was not found in the storage |
| 123 | * \retval PSA_PS_ERROR_STORAGE_FAILURE The operation failed because the physical storage has failed (Fatal error) |
| 124 | * \retval PSA_PS_ERROR_OPERATION_FAILED The operation failed because of an unspecified internal failure |
| 125 | * \retval PSA_PS_ERROR_DATA_CORRUPT The operation failed because of an authentication failure when attempting to get the key |
| 126 | * \retval PSA_PS_ERROR_AUTH_FAILED The operation failed because the data associated with the UID failed authentication |
| 127 | */ |
| 128 | psa_ps_status_t psa_ps_get_info( psa_ps_uid_t uid, struct psa_ps_info_t *p_info ); |
| 129 | |
| 130 | /** |
| 131 | * \brief Remove the provided uid and its associated data from the storage |
| 132 | * |
| 133 | * \param[in] uid The identifier for the data to be removed |
| 134 | * |
| 135 | * \return A status indicating the success/failure of the operation |
| 136 | * |
| 137 | * \retval PSA_PS_SUCCESS The operation completed successfully |
| 138 | * \retval PSA_PS_ERROR_INVALID_ARGUMENT The operation failed because one or more of the given arguments were invalid (null pointer, wrong flags etc.) |
| 139 | * \retval PSA_PS_ERROR_UID_NOT_FOUND The operation failed because the provided uid value was not found in the storage |
| 140 | * \retval PSA_PS_ERROR_WRITE_ONCE The operation failed because the provided uid value was created with psa_eps_WRITE_ONCE_FLAG |
| 141 | * \retval PSA_PS_ERROR_STORAGE_FAILURE The operation failed because the physical storage has failed (Fatal error) |
| 142 | * \retval PSA_PS_ERROR_OPERATION_FAILED The operation failed because of an unspecified internal failure |
| 143 | */ |
| 144 | psa_ps_status_t psa_ps_remove( psa_ps_uid_t uid ); |
| 145 | |
| 146 | /** |
| 147 | * Creates an asset based on the given identifier, the maximum size and |
| 148 | * creation flags. This create allocates the space in the secure storage |
| 149 | * area without setting any data in the asset. |
| 150 | * |
| 151 | * It is only necessary to call this function for items that will be written |
| 152 | * with the \ref psa_ps_set_extended function. If only the \ref psa_ps_set function |
| 153 | * is needed, calls to this function are redundant. |
| 154 | * |
| 155 | * If the \ref PSA_PS_FLAG_WRITE_ONCE flag is passed, implementations should |
| 156 | * return \ref PSA_PS_ERROR_FLAGS_NOT_SUPPORTED. |
| 157 | * |
| 158 | * This function is optional. Not all PSA Protected Storage Implementations |
| 159 | * will implement this function. Consult the documentation of your chosen |
| 160 | * platform to determine if it is present. |
| 161 | * |
| 162 | * \param[in] uid A unique identifier for the asset. |
| 163 | * \param[in] size The maximum size in bytes of the asset. |
| 164 | * \param[in] create_flags Create flags \ref psa_ps_create_flags_t. |
| 165 | * |
| 166 | * \retval PSA_PS_SUCCESS The assest does not exist and the input parameters are correct or |
| 167 | * the asset already exists, the input parameters are the same that |
| 168 | * have been used to create the asset and the owner is the same and the current asset content is kept |
| 169 | * TDB: "Owner is the same" doesn't really make sense from a PSA perspective, as each partition |
| 170 | * has its own UID space, making other partitions' data unadressable |
| 171 | * \retval PSA_PS_ERROR_STORAGE_FAILURE The create action has a physical storage error |
| 172 | * \retval PSA_PS_ERROR_INSUFFICIENT_SPACE The maximum size is bigger of the current available space |
| 173 | * \retval PSA_PS_ERROR_FLAGS_NOT_SUPPORTED One or more create_flags are not valid or supported |
| 174 | * \retval PSA_PS_ERROR_INVALID_ARGUMENT The asset exists and the input paramters are not the same as the existing asset |
| 175 | * \retval PSA_PS_ERROR_NOT_SUPPORTED The implementation of the API does not support this function |
| 176 | * \retval PSA_PS_ERROR_OPERATION_FAILED The operation has failed due to an unspecified error |
| 177 | */ |
| 178 | psa_ps_status_t psa_ps_create( psa_ps_uid_t uid, |
| 179 | uint32_t size, |
| 180 | psa_ps_create_flags_t create_flags ); |
| 181 | |
| 182 | /** |
| 183 | * Sets partial data into an asset based on the given identifier, data_offset, |
| 184 | * data length and p_data. |
| 185 | * |
| 186 | * Before calling this function, the asset must have been created with a call |
| 187 | * to \ref psa_ps_create. |
| 188 | * |
| 189 | * This function is optional. Not all PSA Protected Storage Implementations |
| 190 | * will implement this function. Consult the documentation of your chosen |
| 191 | * platform to determine if it is present. |
| 192 | * |
| 193 | * \param[in] uid The unique identifier for the asset. |
| 194 | * \param[in] data_offset Offset within the asset to start the write. |
| 195 | * \param[in] data_length The size in bytes of the data in p_data to write. |
| 196 | * \param[in] p_data Pointer to a buffer which contains the data to write. |
| 197 | * |
| 198 | * \retval PSA_SUCCESS If the asset exists, the input parameters are correct and the data |
| 199 | * is correctly written in the physical storage |
| 200 | * \retval PSA_PS_ERROR_STORAGE_FAILURE If the data is not written correctly in the physical storage |
| 201 | * \retval PSA_PS_ERROR_OFFSET_INVALID The operation failed because an offset was supplied that is invalid |
| 202 | * for the allocated size of the space |
Gyorgy Szing | 40a7af0 | 2019-02-06 14:19:47 +0100 | [diff] [blame] | 203 | * reserved for the `uid` when \ref psa_ps_create |
Jamie Fox | efb8b37 | 2018-12-14 14:52:15 +0000 | [diff] [blame] | 204 | * was called. For example, offset + size |
| 205 | * is too large |
| 206 | * \retval PSA_PS_ERROR_INVALID_ARGUMENT The operation failed because one or more of the given arguments were invalid (null pointer, wrong flags, etc) |
| 207 | * \retval PSA_PS_ERROR_UID_NOT_FOUND The specified UID was not found |
| 208 | * \retval PSA_PS_ERROR_NOT_SUPPORTED The implementation of the API does not support this function |
| 209 | * \retval PSA_PS_ERROR_OPERATION_FAILED The operation failed due to an unspecified error |
| 210 | * \retval PSA_PS_ERROR_DATA_CORRUPT The operation failed because the existing data has been corrupted |
| 211 | * \retval PSA_PS_ERROR_AUTH_FAILED The operation failed because the existing data failed authentication (MAC check failed) |
| 212 | */ |
| 213 | psa_ps_status_t psa_ps_set_extended( psa_ps_uid_t uid, |
| 214 | uint32_t data_offset, |
| 215 | uint32_t data_length, |
| 216 | const void *p_data ); |
| 217 | |
| 218 | /** |
| 219 | * Returns a bitmask with flags set for all of the optional features supported |
| 220 | * by the implementation. |
| 221 | * |
| 222 | * Currently defined flags are limited to: |
| 223 | * - \ref PSA_PS_SUPPORT_SET_EXTENDED |
| 224 | */ |
| 225 | uint32_t psa_ps_get_support( void ); |
| 226 | |
| 227 | #ifdef __cplusplus |
| 228 | } |
| 229 | #endif |
| 230 | |
| 231 | |
| 232 | #endif // __PSA_PROTECTED_STORAGE_H__ |