psa_crypto_se.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * PSA crypto support for secure element drivers
  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_SE_H
  21. #define PSA_CRYPTO_SE_H
  22. #if !defined(MBEDTLS_CONFIG_FILE)
  23. #include "mbedtls/config.h"
  24. #else
  25. #include MBEDTLS_CONFIG_FILE
  26. #endif
  27. #include "psa/crypto.h"
  28. #include "psa/crypto_se_driver.h"
  29. /** The maximum location value that this implementation supports
  30. * for a secure element.
  31. *
  32. * This is not a characteristic that each PSA implementation has, but a
  33. * limitation of the current implementation due to the constraints imposed
  34. * by storage. See #PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE.
  35. *
  36. * The minimum location value for a secure element is 1, like on any
  37. * PSA implementation (0 means a transparent key).
  38. */
  39. #define PSA_MAX_SE_LOCATION 255
  40. /** The base of the range of ITS file identifiers for secure element
  41. * driver persistent data.
  42. *
  43. * We use a slice of the implementation reserved range 0xffff0000..0xffffffff,
  44. * specifically the range 0xfffffe00..0xfffffeff. The length of this range
  45. * drives the value of #PSA_MAX_SE_LOCATION. The identifier 0xfffffe00 is
  46. * actually not used since it corresponds to #PSA_KEY_LOCATION_LOCAL_STORAGE
  47. * which doesn't have a driver.
  48. */
  49. #define PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE ( (psa_key_id_t) 0xfffffe00 )
  50. /** The maximum number of registered secure element driver locations. */
  51. #define PSA_MAX_SE_DRIVERS 4
  52. /** Unregister all secure element drivers.
  53. *
  54. * \warning Do not call this function while the library is in the initialized
  55. * state. This function is only intended to be called at the end
  56. * of mbedtls_psa_crypto_free().
  57. */
  58. void psa_unregister_all_se_drivers( void );
  59. /** Initialize all secure element drivers.
  60. *
  61. * Called from psa_crypto_init().
  62. */
  63. psa_status_t psa_init_all_se_drivers( void );
  64. /** A structure that describes a registered secure element driver.
  65. *
  66. * A secure element driver table entry contains a pointer to the
  67. * driver's method table as well as the driver context structure.
  68. */
  69. typedef struct psa_se_drv_table_entry_s psa_se_drv_table_entry_t;
  70. /** Return the secure element driver information for a lifetime value.
  71. *
  72. * \param lifetime The lifetime value to query.
  73. * \param[out] p_methods On output, if there is a driver,
  74. * \c *methods points to its method table.
  75. * Otherwise \c *methods is \c NULL.
  76. * \param[out] p_drv_context On output, if there is a driver,
  77. * \c *drv_context points to its context
  78. * structure.
  79. * Otherwise \c *drv_context is \c NULL.
  80. *
  81. * \retval 1
  82. * \p lifetime corresponds to a registered driver.
  83. * \retval 0
  84. * \p lifetime does not correspond to a registered driver.
  85. */
  86. int psa_get_se_driver( psa_key_lifetime_t lifetime,
  87. const psa_drv_se_t **p_methods,
  88. psa_drv_se_context_t **p_drv_context);
  89. /** Return the secure element driver table entry for a lifetime value.
  90. *
  91. * \param lifetime The lifetime value to query.
  92. *
  93. * \return The driver table entry for \p lifetime, or
  94. * \p NULL if \p lifetime does not correspond to a registered driver.
  95. */
  96. psa_se_drv_table_entry_t *psa_get_se_driver_entry(
  97. psa_key_lifetime_t lifetime );
  98. /** Return the method table for a secure element driver.
  99. *
  100. * \param[in] driver The driver table entry to access, or \c NULL.
  101. *
  102. * \return The driver's method table.
  103. * \c NULL if \p driver is \c NULL.
  104. */
  105. const psa_drv_se_t *psa_get_se_driver_methods(
  106. const psa_se_drv_table_entry_t *driver );
  107. /** Return the context of a secure element driver.
  108. *
  109. * \param[in] driver The driver table entry to access, or \c NULL.
  110. *
  111. * \return A pointer to the driver context.
  112. * \c NULL if \p driver is \c NULL.
  113. */
  114. psa_drv_se_context_t *psa_get_se_driver_context(
  115. psa_se_drv_table_entry_t *driver );
  116. /** Find a free slot for a key that is to be created.
  117. *
  118. * This function calls the relevant method in the driver to find a suitable
  119. * slot for a key with the given attributes.
  120. *
  121. * \param[in] attributes Metadata about the key that is about to be created.
  122. * \param[in] driver The driver table entry to query.
  123. * \param[out] slot_number On success, a slot number that is free in this
  124. * secure element.
  125. */
  126. psa_status_t psa_find_se_slot_for_key(
  127. const psa_key_attributes_t *attributes,
  128. psa_key_creation_method_t method,
  129. psa_se_drv_table_entry_t *driver,
  130. psa_key_slot_number_t *slot_number );
  131. /** Destoy a key in a secure element.
  132. *
  133. * This function calls the relevant driver method to destroy a key
  134. * and updates the driver's persistent data.
  135. */
  136. psa_status_t psa_destroy_se_key( psa_se_drv_table_entry_t *driver,
  137. psa_key_slot_number_t slot_number );
  138. /** Load the persistent data of a secure element driver.
  139. *
  140. * \param driver The driver table entry containing the persistent
  141. * data to load from storage.
  142. *
  143. * \return #PSA_SUCCESS
  144. * \return #PSA_ERROR_NOT_SUPPORTED
  145. * \return #PSA_ERROR_DOES_NOT_EXIST
  146. * \return #PSA_ERROR_STORAGE_FAILURE
  147. * \return #PSA_ERROR_DATA_CORRUPT
  148. * \return #PSA_ERROR_INVALID_ARGUMENT
  149. */
  150. psa_status_t psa_load_se_persistent_data(
  151. const psa_se_drv_table_entry_t *driver );
  152. /** Save the persistent data of a secure element driver.
  153. *
  154. * \param[in] driver The driver table entry containing the persistent
  155. * data to save to storage.
  156. *
  157. * \return #PSA_SUCCESS
  158. * \return #PSA_ERROR_NOT_SUPPORTED
  159. * \return #PSA_ERROR_NOT_PERMITTED
  160. * \return #PSA_ERROR_NOT_SUPPORTED
  161. * \return #PSA_ERROR_INSUFFICIENT_STORAGE
  162. * \return #PSA_ERROR_STORAGE_FAILURE
  163. * \return #PSA_ERROR_INVALID_ARGUMENT
  164. */
  165. psa_status_t psa_save_se_persistent_data(
  166. const psa_se_drv_table_entry_t *driver );
  167. /** Destroy the persistent data of a secure element driver.
  168. *
  169. * This is currently only used for testing.
  170. *
  171. * \param[in] location The location identifier for the driver whose
  172. * persistent data is to be erased.
  173. */
  174. psa_status_t psa_destroy_se_persistent_data( psa_key_location_t location );
  175. /** The storage representation of a key whose data is in a secure element.
  176. */
  177. typedef struct
  178. {
  179. uint8_t slot_number[sizeof( psa_key_slot_number_t )];
  180. } psa_se_key_data_storage_t;
  181. #endif /* PSA_CRYPTO_SE_H */