psa_crypto_se.h 6.6 KB

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