psa_crypto_slot_management.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * PSA crypto layer on top of Mbed TLS crypto
  3. */
  4. /*
  5. * Copyright The Mbed TLS Contributors
  6. * SPDX-License-Identifier: Apache-2.0
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  9. * not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. #ifndef PSA_CRYPTO_SLOT_MANAGEMENT_H
  21. #define PSA_CRYPTO_SLOT_MANAGEMENT_H
  22. #include "psa/crypto.h"
  23. #include "psa_crypto_core.h"
  24. #include "psa_crypto_se.h"
  25. /** Range of volatile key identifiers.
  26. *
  27. * The last #MBEDTLS_PSA_KEY_SLOT_COUNT identifiers of the implementation
  28. * range of key identifiers are reserved for volatile key identifiers.
  29. * A volatile key identifier is equal to #PSA_KEY_ID_VOLATILE_MIN plus the
  30. * index of the key slot containing the volatile key definition.
  31. */
  32. /** The minimum value for a volatile key identifier.
  33. */
  34. #define PSA_KEY_ID_VOLATILE_MIN ( PSA_KEY_ID_VENDOR_MAX - \
  35. MBEDTLS_PSA_KEY_SLOT_COUNT + 1 )
  36. /** The maximum value for a volatile key identifier.
  37. */
  38. #define PSA_KEY_ID_VOLATILE_MAX PSA_KEY_ID_VENDOR_MAX
  39. /** Test whether a key identifier is a volatile key identifier.
  40. *
  41. * \param key_id Key identifier to test.
  42. *
  43. * \retval 1
  44. * The key identifier is a volatile key identifier.
  45. * \retval 0
  46. * The key identifier is not a volatile key identifier.
  47. */
  48. static inline int psa_key_id_is_volatile( psa_key_id_t key_id )
  49. {
  50. return( ( key_id >= PSA_KEY_ID_VOLATILE_MIN ) &&
  51. ( key_id <= PSA_KEY_ID_VOLATILE_MAX ) );
  52. }
  53. /** Get the description of a key given its identifier and lock it.
  54. *
  55. * The descriptions of volatile keys and loaded persistent keys are stored in
  56. * key slots. This function returns a pointer to the key slot containing the
  57. * description of a key given its identifier.
  58. *
  59. * In case of a persistent key, the function loads the description of the key
  60. * into a key slot if not already done.
  61. *
  62. * On success, the returned key slot is locked. It is the responsibility of
  63. * the caller to unlock the key slot when it does not access it anymore.
  64. *
  65. * \param key Key identifier to query.
  66. * \param[out] p_slot On success, `*p_slot` contains a pointer to the
  67. * key slot containing the description of the key
  68. * identified by \p key.
  69. *
  70. * \retval #PSA_SUCCESS
  71. * \p *p_slot contains a pointer to the key slot containing the
  72. * description of the key identified by \p key.
  73. * The key slot counter has been incremented.
  74. * \retval #PSA_ERROR_BAD_STATE
  75. * The library has not been initialized.
  76. * \retval #PSA_ERROR_INVALID_HANDLE
  77. * \p key is not a valid key identifier.
  78. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  79. * \p key is a persistent key identifier. The implementation does not
  80. * have sufficient resources to load the persistent key. This can be
  81. * due to a lack of empty key slot, or available memory.
  82. * \retval #PSA_ERROR_DOES_NOT_EXIST
  83. * There is no key with key identifier \p key.
  84. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  85. * \retval #PSA_ERROR_STORAGE_FAILURE
  86. * \retval #PSA_ERROR_DATA_CORRUPT
  87. */
  88. psa_status_t psa_get_and_lock_key_slot( mbedtls_svc_key_id_t key,
  89. psa_key_slot_t **p_slot );
  90. /** Initialize the key slot structures.
  91. *
  92. * \retval #PSA_SUCCESS
  93. * Currently this function always succeeds.
  94. */
  95. psa_status_t psa_initialize_key_slots( void );
  96. /** Delete all data from key slots in memory.
  97. *
  98. * This does not affect persistent storage. */
  99. void psa_wipe_all_key_slots( void );
  100. /** Find a free key slot.
  101. *
  102. * This function returns a key slot that is available for use and is in its
  103. * ground state (all-bits-zero). On success, the key slot is locked. It is
  104. * the responsibility of the caller to unlock the key slot when it does not
  105. * access it anymore.
  106. *
  107. * \param[out] volatile_key_id On success, volatile key identifier
  108. * associated to the returned slot.
  109. * \param[out] p_slot On success, a pointer to the slot.
  110. *
  111. * \retval #PSA_SUCCESS
  112. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  113. * \retval #PSA_ERROR_BAD_STATE
  114. */
  115. psa_status_t psa_get_empty_key_slot( psa_key_id_t *volatile_key_id,
  116. psa_key_slot_t **p_slot );
  117. /** Lock a key slot.
  118. *
  119. * This function increments the key slot lock counter by one.
  120. *
  121. * \param[in] slot The key slot.
  122. *
  123. * \retval #PSA_SUCCESS
  124. The key slot lock counter was incremented.
  125. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  126. * The lock counter already reached its maximum value and was not
  127. * increased.
  128. */
  129. static inline psa_status_t psa_lock_key_slot( psa_key_slot_t *slot )
  130. {
  131. if( slot->lock_count >= SIZE_MAX )
  132. return( PSA_ERROR_CORRUPTION_DETECTED );
  133. slot->lock_count++;
  134. return( PSA_SUCCESS );
  135. }
  136. /** Unlock a key slot.
  137. *
  138. * This function decrements the key slot lock counter by one.
  139. *
  140. * \note To ease the handling of errors in retrieving a key slot
  141. * a NULL input pointer is valid, and the function returns
  142. * successfully without doing anything in that case.
  143. *
  144. * \param[in] slot The key slot.
  145. * \retval #PSA_SUCCESS
  146. * \p slot is NULL or the key slot lock counter has been
  147. * decremented successfully.
  148. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  149. * The lock counter was equal to 0.
  150. *
  151. */
  152. psa_status_t psa_unlock_key_slot( psa_key_slot_t *slot );
  153. /** Test whether a lifetime designates a key in an external cryptoprocessor.
  154. *
  155. * \param lifetime The lifetime to test.
  156. *
  157. * \retval 1
  158. * The lifetime designates an external key. There should be a
  159. * registered driver for this lifetime, otherwise the key cannot
  160. * be created or manipulated.
  161. * \retval 0
  162. * The lifetime designates a key that is volatile or in internal
  163. * storage.
  164. */
  165. static inline int psa_key_lifetime_is_external( psa_key_lifetime_t lifetime )
  166. {
  167. return( PSA_KEY_LIFETIME_GET_LOCATION( lifetime )
  168. != PSA_KEY_LOCATION_LOCAL_STORAGE );
  169. }
  170. /** Validate a key's location.
  171. *
  172. * This function checks whether the key's attributes point to a location that
  173. * is known to the PSA Core, and returns the driver function table if the key
  174. * is to be found in an external location.
  175. *
  176. * \param[in] lifetime The key lifetime attribute.
  177. * \param[out] p_drv On success, when a key is located in external
  178. * storage, returns a pointer to the driver table
  179. * associated with the key's storage location.
  180. *
  181. * \retval #PSA_SUCCESS
  182. * \retval #PSA_ERROR_INVALID_ARGUMENT
  183. */
  184. psa_status_t psa_validate_key_location( psa_key_lifetime_t lifetime,
  185. psa_se_drv_table_entry_t **p_drv );
  186. /** Validate the persistence of a key.
  187. *
  188. * \param[in] lifetime The key lifetime attribute.
  189. *
  190. * \retval #PSA_SUCCESS
  191. * \retval #PSA_ERROR_NOT_SUPPORTED The key is persistent but persistent keys
  192. * are not supported.
  193. */
  194. psa_status_t psa_validate_key_persistence( psa_key_lifetime_t lifetime );
  195. /** Validate a key identifier.
  196. *
  197. * \param[in] key The key identifier.
  198. * \param[in] vendor_ok Non-zero to indicate that key identifiers in the
  199. * vendor range are allowed, volatile key identifiers
  200. * excepted \c 0 otherwise.
  201. *
  202. * \retval <> 0 if the key identifier is valid, 0 otherwise.
  203. */
  204. int psa_is_valid_key_id( mbedtls_svc_key_id_t key, int vendor_ok );
  205. #endif /* PSA_CRYPTO_SLOT_MANAGEMENT_H */