psa_crypto_se.h 6.8 KB

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