psa_crypto_slot_management.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 OR GPL-2.0-or-later
  7. */
  8. #ifndef PSA_CRYPTO_SLOT_MANAGEMENT_H
  9. #define PSA_CRYPTO_SLOT_MANAGEMENT_H
  10. #include "psa/crypto.h"
  11. #include "psa_crypto_core.h"
  12. #include "psa_crypto_se.h"
  13. /** Range of volatile key identifiers.
  14. *
  15. * The first #MBEDTLS_PSA_KEY_SLOT_COUNT identifiers of the implementation
  16. * range of key identifiers are reserved for volatile key identifiers.
  17. *
  18. * If \c id is a a volatile key identifier, #PSA_KEY_ID_VOLATILE_MIN - \c id
  19. * indicates the key slot containing the volatile key definition. See
  20. * psa_crypto_slot_management.c for details.
  21. */
  22. /** The minimum value for a volatile key identifier.
  23. */
  24. #define PSA_KEY_ID_VOLATILE_MIN PSA_KEY_ID_VENDOR_MIN
  25. /** The maximum value for a volatile key identifier.
  26. */
  27. #if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
  28. #define PSA_KEY_ID_VOLATILE_MAX (MBEDTLS_PSA_KEY_ID_BUILTIN_MIN - 1)
  29. #else /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
  30. #define PSA_KEY_ID_VOLATILE_MAX \
  31. (PSA_KEY_ID_VOLATILE_MIN + MBEDTLS_PSA_KEY_SLOT_COUNT - 1)
  32. #endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
  33. /** Test whether a key identifier is a volatile key identifier.
  34. *
  35. * \param key_id Key identifier to test.
  36. *
  37. * \retval 1
  38. * The key identifier is a volatile key identifier.
  39. * \retval 0
  40. * The key identifier is not a volatile key identifier.
  41. */
  42. static inline int psa_key_id_is_volatile(psa_key_id_t key_id)
  43. {
  44. return (key_id >= PSA_KEY_ID_VOLATILE_MIN) &&
  45. (key_id <= PSA_KEY_ID_VOLATILE_MAX);
  46. }
  47. /** Get the description of a key given its identifier and lock it.
  48. *
  49. * The descriptions of volatile keys and loaded persistent keys are stored in
  50. * key slots. This function returns a pointer to the key slot containing the
  51. * description of a key given its identifier.
  52. *
  53. * In case of a persistent key, the function loads the description of the key
  54. * into a key slot if not already done.
  55. *
  56. * On success, the returned key slot has been registered for reading.
  57. * It is the responsibility of the caller to call psa_unregister_read(slot)
  58. * when they have finished reading the contents of the slot.
  59. *
  60. * On failure, `*p_slot` is set to NULL. This ensures that it is always valid
  61. * to call psa_unregister_read on the returned slot.
  62. *
  63. * \param key Key identifier to query.
  64. * \param[out] p_slot On success, `*p_slot` contains a pointer to the
  65. * key slot containing the description of the key
  66. * identified by \p key.
  67. *
  68. * \retval #PSA_SUCCESS
  69. * \p *p_slot contains a pointer to the key slot containing the
  70. * description of the key identified by \p key.
  71. * The key slot counter has been incremented.
  72. * \retval #PSA_ERROR_BAD_STATE
  73. * The library has not been initialized.
  74. * \retval #PSA_ERROR_INVALID_HANDLE
  75. * \p key is not a valid key identifier.
  76. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  77. * \p key is a persistent key identifier. The implementation does not
  78. * have sufficient resources to load the persistent key. This can be
  79. * due to a lack of empty key slot, or available memory.
  80. * \retval #PSA_ERROR_DOES_NOT_EXIST
  81. * There is no key with key identifier \p key.
  82. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  83. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  84. * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
  85. */
  86. psa_status_t psa_get_and_lock_key_slot(mbedtls_svc_key_id_t key,
  87. psa_key_slot_t **p_slot);
  88. /** Initialize the key slot structures.
  89. *
  90. * \retval #PSA_SUCCESS
  91. * Currently this function always succeeds.
  92. */
  93. psa_status_t psa_initialize_key_slots(void);
  94. #if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
  95. /* Allow test code to customize the key slice length. We use this in tests
  96. * that exhaust the key store to reach a full key store in reasonable time
  97. * and memory.
  98. *
  99. * The length of each slice must be between 1 and
  100. * (1 << KEY_ID_SLOT_INDEX_WIDTH) inclusive.
  101. *
  102. * The length for a given slice index must not change while
  103. * the key store is initialized.
  104. */
  105. extern size_t (*mbedtls_test_hook_psa_volatile_key_slice_length)(
  106. size_t slice_idx);
  107. /* The number of volatile key slices. */
  108. size_t psa_key_slot_volatile_slice_count(void);
  109. #endif
  110. /** Delete all data from key slots in memory.
  111. * This function is not thread safe, it wipes every key slot regardless of
  112. * state and reader count. It should only be called when no slot is in use.
  113. *
  114. * This does not affect persistent storage. */
  115. void psa_wipe_all_key_slots(void);
  116. /** Find a free key slot and reserve it to be filled with a key.
  117. *
  118. * This function finds a key slot that is free,
  119. * sets its state to PSA_SLOT_FILLING and then returns the slot.
  120. *
  121. * On success, the key slot's state is PSA_SLOT_FILLING.
  122. * It is the responsibility of the caller to change the slot's state to
  123. * PSA_SLOT_EMPTY/FULL once key creation has finished.
  124. *
  125. * If multi-threading is enabled, the caller must hold the
  126. * global key slot mutex.
  127. *
  128. * \param[out] volatile_key_id - If null, reserve a cache slot for
  129. * a persistent or built-in key.
  130. * - If non-null, allocate a slot for
  131. * a volatile key. On success,
  132. * \p *volatile_key_id is the
  133. * identifier corresponding to the
  134. * returned slot. It is the caller's
  135. * responsibility to set this key identifier
  136. * in the attributes.
  137. * \param[out] p_slot On success, a pointer to the slot.
  138. *
  139. * \retval #PSA_SUCCESS \emptydescription
  140. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  141. * There were no free key slots.
  142. * When #MBEDTLS_PSA_KEY_STORE_DYNAMIC is enabled, there was not
  143. * enough memory to allocate more slots.
  144. * \retval #PSA_ERROR_BAD_STATE \emptydescription
  145. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  146. * This function attempted to operate on a key slot which was in an
  147. * unexpected state.
  148. */
  149. psa_status_t psa_reserve_free_key_slot(psa_key_id_t *volatile_key_id,
  150. psa_key_slot_t **p_slot);
  151. #if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
  152. /** Return a key slot to the free list.
  153. *
  154. * Call this function when a slot obtained from psa_reserve_free_key_slot()
  155. * is no longer in use.
  156. *
  157. * If multi-threading is enabled, the caller must hold the
  158. * global key slot mutex.
  159. *
  160. * \param slice_idx The slice containing the slot.
  161. * This is `slot->slice_index` when the slot
  162. * is obtained from psa_reserve_free_key_slot().
  163. * \param slot The key slot.
  164. *
  165. * \retval #PSA_SUCCESS \emptydescription
  166. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  167. * This function attempted to operate on a key slot which was in an
  168. * unexpected state.
  169. */
  170. psa_status_t psa_free_key_slot(size_t slice_idx,
  171. psa_key_slot_t *slot);
  172. #endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
  173. /** Change the state of a key slot.
  174. *
  175. * This function changes the state of the key slot from expected_state to
  176. * new state. If the state of the slot was not expected_state, the state is
  177. * unchanged.
  178. *
  179. * If multi-threading is enabled, the caller must hold the
  180. * global key slot mutex.
  181. *
  182. * \param[in] slot The key slot.
  183. * \param[in] expected_state The current state of the slot.
  184. * \param[in] new_state The new state of the slot.
  185. *
  186. * \retval #PSA_SUCCESS
  187. The key slot's state variable is new_state.
  188. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  189. * The slot's state was not expected_state.
  190. */
  191. static inline psa_status_t psa_key_slot_state_transition(
  192. psa_key_slot_t *slot, psa_key_slot_state_t expected_state,
  193. psa_key_slot_state_t new_state)
  194. {
  195. if (slot->state != expected_state) {
  196. return PSA_ERROR_CORRUPTION_DETECTED;
  197. }
  198. slot->state = new_state;
  199. return PSA_SUCCESS;
  200. }
  201. /** Register as a reader of a key slot.
  202. *
  203. * This function increments the key slot registered reader counter by one.
  204. * If multi-threading is enabled, the caller must hold the
  205. * global key slot mutex.
  206. *
  207. * \param[in] slot The key slot.
  208. *
  209. * \retval #PSA_SUCCESS
  210. The key slot registered reader counter was incremented.
  211. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  212. * The reader counter already reached its maximum value and was not
  213. * increased, or the slot's state was not PSA_SLOT_FULL.
  214. */
  215. static inline psa_status_t psa_register_read(psa_key_slot_t *slot)
  216. {
  217. if ((slot->state != PSA_SLOT_FULL) ||
  218. (slot->var.occupied.registered_readers >= SIZE_MAX)) {
  219. return PSA_ERROR_CORRUPTION_DETECTED;
  220. }
  221. slot->var.occupied.registered_readers++;
  222. return PSA_SUCCESS;
  223. }
  224. /** Unregister from reading a key slot.
  225. *
  226. * This function decrements the key slot registered reader counter by one.
  227. * If the state of the slot is PSA_SLOT_PENDING_DELETION,
  228. * and there is only one registered reader (the caller),
  229. * this function will call psa_wipe_key_slot().
  230. * If multi-threading is enabled, the caller must hold the
  231. * global key slot mutex.
  232. *
  233. * \note To ease the handling of errors in retrieving a key slot
  234. * a NULL input pointer is valid, and the function returns
  235. * successfully without doing anything in that case.
  236. *
  237. * \param[in] slot The key slot.
  238. * \retval #PSA_SUCCESS
  239. * \p slot is NULL or the key slot reader counter has been
  240. * decremented (and potentially wiped) successfully.
  241. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  242. * The slot's state was neither PSA_SLOT_FULL nor
  243. * PSA_SLOT_PENDING_DELETION.
  244. * Or a wipe was attempted and the slot's state was not
  245. * PSA_SLOT_PENDING_DELETION.
  246. * Or registered_readers was equal to 0.
  247. */
  248. psa_status_t psa_unregister_read(psa_key_slot_t *slot);
  249. /** Wrap a call to psa_unregister_read in the global key slot mutex.
  250. *
  251. * If threading is disabled, this simply calls psa_unregister_read.
  252. *
  253. * \note To ease the handling of errors in retrieving a key slot
  254. * a NULL input pointer is valid, and the function returns
  255. * successfully without doing anything in that case.
  256. *
  257. * \param[in] slot The key slot.
  258. * \retval #PSA_SUCCESS
  259. * \p slot is NULL or the key slot reader counter has been
  260. * decremented (and potentially wiped) successfully.
  261. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  262. * The slot's state was neither PSA_SLOT_FULL nor
  263. * PSA_SLOT_PENDING_DELETION.
  264. * Or a wipe was attempted and the slot's state was not
  265. * PSA_SLOT_PENDING_DELETION.
  266. * Or registered_readers was equal to 0.
  267. */
  268. psa_status_t psa_unregister_read_under_mutex(psa_key_slot_t *slot);
  269. /** Test whether a lifetime designates a key in an external cryptoprocessor.
  270. *
  271. * \param lifetime The lifetime to test.
  272. *
  273. * \retval 1
  274. * The lifetime designates an external key. There should be a
  275. * registered driver for this lifetime, otherwise the key cannot
  276. * be created or manipulated.
  277. * \retval 0
  278. * The lifetime designates a key that is volatile or in internal
  279. * storage.
  280. */
  281. static inline int psa_key_lifetime_is_external(psa_key_lifetime_t lifetime)
  282. {
  283. return PSA_KEY_LIFETIME_GET_LOCATION(lifetime)
  284. != PSA_KEY_LOCATION_LOCAL_STORAGE;
  285. }
  286. /** Validate a key's location.
  287. *
  288. * This function checks whether the key's attributes point to a location that
  289. * is known to the PSA Core, and returns the driver function table if the key
  290. * is to be found in an external location.
  291. *
  292. * \param[in] lifetime The key lifetime attribute.
  293. * \param[out] p_drv On success, when a key is located in external
  294. * storage, returns a pointer to the driver table
  295. * associated with the key's storage location.
  296. *
  297. * \retval #PSA_SUCCESS \emptydescription
  298. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  299. */
  300. psa_status_t psa_validate_key_location(psa_key_lifetime_t lifetime,
  301. psa_se_drv_table_entry_t **p_drv);
  302. /** Validate the persistence of a key.
  303. *
  304. * \param[in] lifetime The key lifetime attribute.
  305. *
  306. * \retval #PSA_SUCCESS \emptydescription
  307. * \retval #PSA_ERROR_NOT_SUPPORTED The key is persistent but persistent keys
  308. * are not supported.
  309. */
  310. psa_status_t psa_validate_key_persistence(psa_key_lifetime_t lifetime);
  311. /** Validate a key identifier.
  312. *
  313. * \param[in] key The key identifier.
  314. * \param[in] vendor_ok Non-zero to indicate that key identifiers in the
  315. * vendor range are allowed, volatile key identifiers
  316. * excepted \c 0 otherwise.
  317. *
  318. * \retval <> 0 if the key identifier is valid, 0 otherwise.
  319. */
  320. int psa_is_valid_key_id(mbedtls_svc_key_id_t key, int vendor_ok);
  321. #endif /* PSA_CRYPTO_SLOT_MANAGEMENT_H */