psa_crypto_slot_management.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 \emptydescription
  85. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  86. * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
  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 \emptydescription
  112. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  113. * \retval #PSA_ERROR_BAD_STATE \emptydescription
  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. }
  134. slot->lock_count++;
  135. return PSA_SUCCESS;
  136. }
  137. /** Unlock a key slot.
  138. *
  139. * This function decrements the key slot lock counter by one.
  140. *
  141. * \note To ease the handling of errors in retrieving a key slot
  142. * a NULL input pointer is valid, and the function returns
  143. * successfully without doing anything in that case.
  144. *
  145. * \param[in] slot The key slot.
  146. * \retval #PSA_SUCCESS
  147. * \p slot is NULL or the key slot lock counter has been
  148. * decremented successfully.
  149. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  150. * The lock counter was equal to 0.
  151. *
  152. */
  153. psa_status_t psa_unlock_key_slot(psa_key_slot_t *slot);
  154. /** Test whether a lifetime designates a key in an external cryptoprocessor.
  155. *
  156. * \param lifetime The lifetime to test.
  157. *
  158. * \retval 1
  159. * The lifetime designates an external key. There should be a
  160. * registered driver for this lifetime, otherwise the key cannot
  161. * be created or manipulated.
  162. * \retval 0
  163. * The lifetime designates a key that is volatile or in internal
  164. * storage.
  165. */
  166. static inline int psa_key_lifetime_is_external(psa_key_lifetime_t lifetime)
  167. {
  168. return PSA_KEY_LIFETIME_GET_LOCATION(lifetime)
  169. != PSA_KEY_LOCATION_LOCAL_STORAGE;
  170. }
  171. /** Validate a key's location.
  172. *
  173. * This function checks whether the key's attributes point to a location that
  174. * is known to the PSA Core, and returns the driver function table if the key
  175. * is to be found in an external location.
  176. *
  177. * \param[in] lifetime The key lifetime attribute.
  178. * \param[out] p_drv On success, when a key is located in external
  179. * storage, returns a pointer to the driver table
  180. * associated with the key's storage location.
  181. *
  182. * \retval #PSA_SUCCESS \emptydescription
  183. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  184. */
  185. psa_status_t psa_validate_key_location(psa_key_lifetime_t lifetime,
  186. psa_se_drv_table_entry_t **p_drv);
  187. /** Validate the persistence of a key.
  188. *
  189. * \param[in] lifetime The key lifetime attribute.
  190. *
  191. * \retval #PSA_SUCCESS \emptydescription
  192. * \retval #PSA_ERROR_NOT_SUPPORTED The key is persistent but persistent keys
  193. * are not supported.
  194. */
  195. psa_status_t psa_validate_key_persistence(psa_key_lifetime_t lifetime);
  196. /** Validate a key identifier.
  197. *
  198. * \param[in] key The key identifier.
  199. * \param[in] vendor_ok Non-zero to indicate that key identifiers in the
  200. * vendor range are allowed, volatile key identifiers
  201. * excepted \c 0 otherwise.
  202. *
  203. * \retval <> 0 if the key identifier is valid, 0 otherwise.
  204. */
  205. int psa_is_valid_key_id(mbedtls_svc_key_id_t key, int vendor_ok);
  206. #endif /* PSA_CRYPTO_SLOT_MANAGEMENT_H */