psa_crypto_se.c 13 KB

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