psa_crypto_se.c 13 KB

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