psa_crypto_se.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. #include "common.h"
  9. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  10. #include <stdint.h>
  11. #include <string.h>
  12. #include "psa/crypto_se_driver.h"
  13. #include "psa_crypto_se.h"
  14. #if defined(MBEDTLS_PSA_ITS_FILE_C)
  15. #include "psa_crypto_its.h"
  16. #else /* Native ITS implementation */
  17. #include "psa/error.h"
  18. #include "psa/internal_trusted_storage.h"
  19. #endif
  20. #include "mbedtls/platform.h"
  21. /****************************************************************/
  22. /* Driver lookup */
  23. /****************************************************************/
  24. /* This structure is identical to psa_drv_se_context_t declared in
  25. * `crypto_se_driver.h`, except that some parts are writable here
  26. * (non-const, or pointer to non-const). */
  27. typedef struct {
  28. void *persistent_data;
  29. size_t persistent_data_size;
  30. uintptr_t transient_data;
  31. } psa_drv_se_internal_context_t;
  32. struct psa_se_drv_table_entry_s {
  33. psa_key_location_t location;
  34. const psa_drv_se_t *methods;
  35. union {
  36. psa_drv_se_internal_context_t internal;
  37. psa_drv_se_context_t context;
  38. } u;
  39. };
  40. static psa_se_drv_table_entry_t driver_table[PSA_MAX_SE_DRIVERS];
  41. psa_se_drv_table_entry_t *psa_get_se_driver_entry(
  42. psa_key_lifetime_t lifetime)
  43. {
  44. size_t i;
  45. psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(lifetime);
  46. /* In the driver table, location=0 means an entry that isn't used.
  47. * No driver has a location of 0 because it's a reserved value
  48. * (which designates transparent keys). Make sure we never return
  49. * a driver entry for location 0. */
  50. if (location == 0) {
  51. return NULL;
  52. }
  53. for (i = 0; i < PSA_MAX_SE_DRIVERS; i++) {
  54. if (driver_table[i].location == location) {
  55. return &driver_table[i];
  56. }
  57. }
  58. return NULL;
  59. }
  60. const psa_drv_se_t *psa_get_se_driver_methods(
  61. const psa_se_drv_table_entry_t *driver)
  62. {
  63. return driver->methods;
  64. }
  65. psa_drv_se_context_t *psa_get_se_driver_context(
  66. psa_se_drv_table_entry_t *driver)
  67. {
  68. return &driver->u.context;
  69. }
  70. int psa_get_se_driver(psa_key_lifetime_t lifetime,
  71. const psa_drv_se_t **p_methods,
  72. psa_drv_se_context_t **p_drv_context)
  73. {
  74. psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry(lifetime);
  75. if (p_methods != NULL) {
  76. *p_methods = (driver ? driver->methods : NULL);
  77. }
  78. if (p_drv_context != NULL) {
  79. *p_drv_context = (driver ? &driver->u.context : NULL);
  80. }
  81. return driver != NULL;
  82. }
  83. /****************************************************************/
  84. /* Persistent data management */
  85. /****************************************************************/
  86. static psa_status_t psa_get_se_driver_its_file_uid(
  87. const psa_se_drv_table_entry_t *driver,
  88. psa_storage_uid_t *uid)
  89. {
  90. if (driver->location > PSA_MAX_SE_LOCATION) {
  91. return PSA_ERROR_NOT_SUPPORTED;
  92. }
  93. /* ITS file sizes are limited to 32 bits. */
  94. if (driver->u.internal.persistent_data_size > UINT32_MAX) {
  95. return PSA_ERROR_NOT_SUPPORTED;
  96. }
  97. /* See the documentation of PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE. */
  98. *uid = PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE + driver->location;
  99. return PSA_SUCCESS;
  100. }
  101. psa_status_t psa_load_se_persistent_data(
  102. const psa_se_drv_table_entry_t *driver)
  103. {
  104. psa_status_t status;
  105. psa_storage_uid_t uid;
  106. size_t length;
  107. status = psa_get_se_driver_its_file_uid(driver, &uid);
  108. if (status != PSA_SUCCESS) {
  109. return status;
  110. }
  111. /* Read the amount of persistent data that the driver requests.
  112. * If the data in storage is larger, it is truncated. If the data
  113. * in storage is smaller, silently keep what is already at the end
  114. * of the output buffer. */
  115. /* psa_get_se_driver_its_file_uid ensures that the size_t
  116. * persistent_data_size is in range, but compilers don't know that,
  117. * so cast to reassure them. */
  118. return psa_its_get(uid, 0,
  119. (uint32_t) driver->u.internal.persistent_data_size,
  120. driver->u.internal.persistent_data,
  121. &length);
  122. }
  123. psa_status_t psa_save_se_persistent_data(
  124. const psa_se_drv_table_entry_t *driver)
  125. {
  126. psa_status_t status;
  127. psa_storage_uid_t uid;
  128. status = psa_get_se_driver_its_file_uid(driver, &uid);
  129. if (status != PSA_SUCCESS) {
  130. return status;
  131. }
  132. /* psa_get_se_driver_its_file_uid ensures that the size_t
  133. * persistent_data_size is in range, but compilers don't know that,
  134. * so cast to reassure them. */
  135. return psa_its_set(uid,
  136. (uint32_t) driver->u.internal.persistent_data_size,
  137. driver->u.internal.persistent_data,
  138. 0);
  139. }
  140. psa_status_t psa_destroy_se_persistent_data(psa_key_location_t location)
  141. {
  142. psa_storage_uid_t uid;
  143. if (location > PSA_MAX_SE_LOCATION) {
  144. return PSA_ERROR_NOT_SUPPORTED;
  145. }
  146. uid = PSA_CRYPTO_SE_DRIVER_ITS_UID_BASE + location;
  147. return psa_its_remove(uid);
  148. }
  149. psa_status_t psa_find_se_slot_for_key(
  150. const psa_key_attributes_t *attributes,
  151. psa_key_creation_method_t method,
  152. psa_se_drv_table_entry_t *driver,
  153. psa_key_slot_number_t *slot_number)
  154. {
  155. psa_status_t status;
  156. psa_key_location_t key_location =
  157. PSA_KEY_LIFETIME_GET_LOCATION(psa_get_key_lifetime(attributes));
  158. /* If the location is wrong, it's a bug in the library. */
  159. if (driver->location != key_location) {
  160. return PSA_ERROR_CORRUPTION_DETECTED;
  161. }
  162. /* If the driver doesn't support key creation in any way, give up now. */
  163. if (driver->methods->key_management == NULL) {
  164. return PSA_ERROR_NOT_SUPPORTED;
  165. }
  166. if (psa_get_key_slot_number(attributes, slot_number) == PSA_SUCCESS) {
  167. /* The application wants to use a specific slot. Allow it if
  168. * the driver supports it. On a system with isolation,
  169. * the crypto service must check that the application is
  170. * permitted to request this slot. */
  171. psa_drv_se_validate_slot_number_t p_validate_slot_number =
  172. driver->methods->key_management->p_validate_slot_number;
  173. if (p_validate_slot_number == NULL) {
  174. return PSA_ERROR_NOT_SUPPORTED;
  175. }
  176. status = p_validate_slot_number(&driver->u.context,
  177. driver->u.internal.persistent_data,
  178. attributes, method,
  179. *slot_number);
  180. } else if (method == PSA_KEY_CREATION_REGISTER) {
  181. /* The application didn't specify a slot number. This doesn't
  182. * make sense when registering a slot. */
  183. return PSA_ERROR_INVALID_ARGUMENT;
  184. } else {
  185. /* The application didn't tell us which slot to use. Let the driver
  186. * choose. This is the normal case. */
  187. psa_drv_se_allocate_key_t p_allocate =
  188. driver->methods->key_management->p_allocate;
  189. if (p_allocate == NULL) {
  190. return PSA_ERROR_NOT_SUPPORTED;
  191. }
  192. status = p_allocate(&driver->u.context,
  193. driver->u.internal.persistent_data,
  194. attributes, method,
  195. slot_number);
  196. }
  197. return status;
  198. }
  199. psa_status_t psa_destroy_se_key(psa_se_drv_table_entry_t *driver,
  200. psa_key_slot_number_t slot_number)
  201. {
  202. psa_status_t status;
  203. psa_status_t storage_status;
  204. /* Normally a missing method would mean that the action is not
  205. * supported. But psa_destroy_key() is not supposed to return
  206. * PSA_ERROR_NOT_SUPPORTED: if you can create a key, you should
  207. * be able to destroy it. The only use case for a driver that
  208. * does not have a way to destroy keys at all is if the keys are
  209. * locked in a read-only state: we can use the keys but not
  210. * destroy them. Hence, if the driver doesn't support destroying
  211. * keys, it's really a lack of permission. */
  212. if (driver->methods->key_management == NULL ||
  213. driver->methods->key_management->p_destroy == NULL) {
  214. return PSA_ERROR_NOT_PERMITTED;
  215. }
  216. status = driver->methods->key_management->p_destroy(
  217. &driver->u.context,
  218. driver->u.internal.persistent_data,
  219. slot_number);
  220. storage_status = psa_save_se_persistent_data(driver);
  221. return status == PSA_SUCCESS ? storage_status : status;
  222. }
  223. psa_status_t psa_init_all_se_drivers(void)
  224. {
  225. size_t i;
  226. for (i = 0; i < PSA_MAX_SE_DRIVERS; i++) {
  227. psa_se_drv_table_entry_t *driver = &driver_table[i];
  228. if (driver->location == 0) {
  229. continue; /* skipping unused entry */
  230. }
  231. const psa_drv_se_t *methods = psa_get_se_driver_methods(driver);
  232. if (methods->p_init != NULL) {
  233. psa_status_t status = methods->p_init(
  234. &driver->u.context,
  235. driver->u.internal.persistent_data,
  236. driver->location);
  237. if (status != PSA_SUCCESS) {
  238. return status;
  239. }
  240. status = psa_save_se_persistent_data(driver);
  241. if (status != PSA_SUCCESS) {
  242. return status;
  243. }
  244. }
  245. }
  246. return PSA_SUCCESS;
  247. }
  248. /****************************************************************/
  249. /* Driver registration */
  250. /****************************************************************/
  251. psa_status_t psa_register_se_driver(
  252. psa_key_location_t location,
  253. const psa_drv_se_t *methods)
  254. {
  255. size_t i;
  256. psa_status_t status;
  257. if (methods->hal_version != PSA_DRV_SE_HAL_VERSION) {
  258. return PSA_ERROR_NOT_SUPPORTED;
  259. }
  260. /* Driver table entries are 0-initialized. 0 is not a valid driver
  261. * location because it means a transparent key. */
  262. MBEDTLS_STATIC_ASSERT(PSA_KEY_LOCATION_LOCAL_STORAGE == 0,
  263. "Secure element support requires 0 to mean a local key");
  264. if (location == PSA_KEY_LOCATION_LOCAL_STORAGE) {
  265. return PSA_ERROR_INVALID_ARGUMENT;
  266. }
  267. if (location > PSA_MAX_SE_LOCATION) {
  268. return PSA_ERROR_NOT_SUPPORTED;
  269. }
  270. for (i = 0; i < PSA_MAX_SE_DRIVERS; i++) {
  271. if (driver_table[i].location == 0) {
  272. break;
  273. }
  274. /* Check that location isn't already in use up to the first free
  275. * entry. Since entries are created in order and never deleted,
  276. * there can't be a used entry after the first free entry. */
  277. if (driver_table[i].location == location) {
  278. return PSA_ERROR_ALREADY_EXISTS;
  279. }
  280. }
  281. if (i == PSA_MAX_SE_DRIVERS) {
  282. return PSA_ERROR_INSUFFICIENT_MEMORY;
  283. }
  284. driver_table[i].location = location;
  285. driver_table[i].methods = methods;
  286. driver_table[i].u.internal.persistent_data_size =
  287. methods->persistent_data_size;
  288. if (methods->persistent_data_size != 0) {
  289. driver_table[i].u.internal.persistent_data =
  290. mbedtls_calloc(1, methods->persistent_data_size);
  291. if (driver_table[i].u.internal.persistent_data == NULL) {
  292. status = PSA_ERROR_INSUFFICIENT_MEMORY;
  293. goto error;
  294. }
  295. /* Load the driver's persistent data. On first use, the persistent
  296. * data does not exist in storage, and is initialized to
  297. * all-bits-zero by the calloc call just above. */
  298. status = psa_load_se_persistent_data(&driver_table[i]);
  299. if (status != PSA_SUCCESS && status != PSA_ERROR_DOES_NOT_EXIST) {
  300. goto error;
  301. }
  302. }
  303. return PSA_SUCCESS;
  304. error:
  305. memset(&driver_table[i], 0, sizeof(driver_table[i]));
  306. return status;
  307. }
  308. void psa_unregister_all_se_drivers(void)
  309. {
  310. size_t i;
  311. for (i = 0; i < PSA_MAX_SE_DRIVERS; i++) {
  312. if (driver_table[i].u.internal.persistent_data != NULL) {
  313. mbedtls_free(driver_table[i].u.internal.persistent_data);
  314. }
  315. }
  316. memset(driver_table, 0, sizeof(driver_table));
  317. }
  318. /****************************************************************/
  319. /* The end */
  320. /****************************************************************/
  321. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */