psa_crypto_slot_management.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /*
  2. * PSA crypto layer on top of Mbed TLS crypto
  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_C)
  22. #include "psa/crypto.h"
  23. #include "psa_crypto_core.h"
  24. #include "psa_crypto_driver_wrappers.h"
  25. #include "psa_crypto_slot_management.h"
  26. #include "psa_crypto_storage.h"
  27. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  28. #include "psa_crypto_se.h"
  29. #endif
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include "mbedtls/platform.h"
  33. #define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array)))
  34. typedef struct {
  35. psa_key_slot_t key_slots[MBEDTLS_PSA_KEY_SLOT_COUNT];
  36. unsigned key_slots_initialized : 1;
  37. } psa_global_data_t;
  38. static psa_global_data_t global_data;
  39. int psa_is_valid_key_id(mbedtls_svc_key_id_t key, int vendor_ok)
  40. {
  41. psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
  42. if ((PSA_KEY_ID_USER_MIN <= key_id) &&
  43. (key_id <= PSA_KEY_ID_USER_MAX)) {
  44. return 1;
  45. }
  46. if (vendor_ok &&
  47. (PSA_KEY_ID_VENDOR_MIN <= key_id) &&
  48. (key_id <= PSA_KEY_ID_VENDOR_MAX)) {
  49. return 1;
  50. }
  51. return 0;
  52. }
  53. /** Get the description in memory of a key given its identifier and lock it.
  54. *
  55. * The descriptions of volatile keys and loaded persistent keys are
  56. * stored in key slots. This function returns a pointer to the key slot
  57. * containing the description of a key given its identifier.
  58. *
  59. * The function searches the key slots containing the description of the key
  60. * with \p key identifier. The function does only read accesses to the key
  61. * slots. The function does not load any persistent key thus does not access
  62. * any storage.
  63. *
  64. * For volatile key identifiers, only one key slot is queried as a volatile
  65. * key with identifier key_id can only be stored in slot of index
  66. * ( key_id - #PSA_KEY_ID_VOLATILE_MIN ).
  67. *
  68. * On success, the function locks the key slot. It is the responsibility of
  69. * the caller to unlock the key slot when it does not access it anymore.
  70. *
  71. * \param key Key identifier to query.
  72. * \param[out] p_slot On success, `*p_slot` contains a pointer to the
  73. * key slot containing the description of the key
  74. * identified by \p key.
  75. *
  76. * \retval #PSA_SUCCESS
  77. * The pointer to the key slot containing the description of the key
  78. * identified by \p key was returned.
  79. * \retval #PSA_ERROR_INVALID_HANDLE
  80. * \p key is not a valid key identifier.
  81. * \retval #PSA_ERROR_DOES_NOT_EXIST
  82. * There is no key with key identifier \p key in the key slots.
  83. */
  84. static psa_status_t psa_get_and_lock_key_slot_in_memory(
  85. mbedtls_svc_key_id_t key, psa_key_slot_t **p_slot)
  86. {
  87. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  88. psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
  89. size_t slot_idx;
  90. psa_key_slot_t *slot = NULL;
  91. if (psa_key_id_is_volatile(key_id)) {
  92. slot = &global_data.key_slots[key_id - PSA_KEY_ID_VOLATILE_MIN];
  93. /*
  94. * Check if both the PSA key identifier key_id and the owner
  95. * identifier of key match those of the key slot.
  96. *
  97. * Note that, if the key slot is not occupied, its PSA key identifier
  98. * is equal to zero. This is an invalid value for a PSA key identifier
  99. * and thus cannot be equal to the valid PSA key identifier key_id.
  100. */
  101. status = mbedtls_svc_key_id_equal(key, slot->attr.id) ?
  102. PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
  103. } else {
  104. if (!psa_is_valid_key_id(key, 1)) {
  105. return PSA_ERROR_INVALID_HANDLE;
  106. }
  107. for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
  108. slot = &global_data.key_slots[slot_idx];
  109. if (mbedtls_svc_key_id_equal(key, slot->attr.id)) {
  110. break;
  111. }
  112. }
  113. status = (slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT) ?
  114. PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
  115. }
  116. if (status == PSA_SUCCESS) {
  117. status = psa_lock_key_slot(slot);
  118. if (status == PSA_SUCCESS) {
  119. *p_slot = slot;
  120. }
  121. }
  122. return status;
  123. }
  124. psa_status_t psa_initialize_key_slots(void)
  125. {
  126. /* Nothing to do: program startup and psa_wipe_all_key_slots() both
  127. * guarantee that the key slots are initialized to all-zero, which
  128. * means that all the key slots are in a valid, empty state. */
  129. global_data.key_slots_initialized = 1;
  130. return PSA_SUCCESS;
  131. }
  132. void psa_wipe_all_key_slots(void)
  133. {
  134. size_t slot_idx;
  135. for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
  136. psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
  137. slot->lock_count = 1;
  138. (void) psa_wipe_key_slot(slot);
  139. }
  140. global_data.key_slots_initialized = 0;
  141. }
  142. psa_status_t psa_get_empty_key_slot(psa_key_id_t *volatile_key_id,
  143. psa_key_slot_t **p_slot)
  144. {
  145. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  146. size_t slot_idx;
  147. psa_key_slot_t *selected_slot, *unlocked_persistent_key_slot;
  148. if (!global_data.key_slots_initialized) {
  149. status = PSA_ERROR_BAD_STATE;
  150. goto error;
  151. }
  152. selected_slot = unlocked_persistent_key_slot = NULL;
  153. for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
  154. psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
  155. if (!psa_is_key_slot_occupied(slot)) {
  156. selected_slot = slot;
  157. break;
  158. }
  159. if ((unlocked_persistent_key_slot == NULL) &&
  160. (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) &&
  161. (!psa_is_key_slot_locked(slot))) {
  162. unlocked_persistent_key_slot = slot;
  163. }
  164. }
  165. /*
  166. * If there is no unused key slot and there is at least one unlocked key
  167. * slot containing the description of a persistent key, recycle the first
  168. * such key slot we encountered. If we later need to operate on the
  169. * persistent key we are evicting now, we will reload its description from
  170. * storage.
  171. */
  172. if ((selected_slot == NULL) &&
  173. (unlocked_persistent_key_slot != NULL)) {
  174. selected_slot = unlocked_persistent_key_slot;
  175. selected_slot->lock_count = 1;
  176. psa_wipe_key_slot(selected_slot);
  177. }
  178. if (selected_slot != NULL) {
  179. status = psa_lock_key_slot(selected_slot);
  180. if (status != PSA_SUCCESS) {
  181. goto error;
  182. }
  183. *volatile_key_id = PSA_KEY_ID_VOLATILE_MIN +
  184. ((psa_key_id_t) (selected_slot - global_data.key_slots));
  185. *p_slot = selected_slot;
  186. return PSA_SUCCESS;
  187. }
  188. status = PSA_ERROR_INSUFFICIENT_MEMORY;
  189. error:
  190. *p_slot = NULL;
  191. *volatile_key_id = 0;
  192. return status;
  193. }
  194. #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
  195. static psa_status_t psa_load_persistent_key_into_slot(psa_key_slot_t *slot)
  196. {
  197. psa_status_t status = PSA_SUCCESS;
  198. uint8_t *key_data = NULL;
  199. size_t key_data_length = 0;
  200. status = psa_load_persistent_key(&slot->attr,
  201. &key_data, &key_data_length);
  202. if (status != PSA_SUCCESS) {
  203. goto exit;
  204. }
  205. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  206. /* Special handling is required for loading keys associated with a
  207. * dynamically registered SE interface. */
  208. const psa_drv_se_t *drv;
  209. psa_drv_se_context_t *drv_context;
  210. if (psa_get_se_driver(slot->attr.lifetime, &drv, &drv_context)) {
  211. psa_se_key_data_storage_t *data;
  212. if (key_data_length != sizeof(*data)) {
  213. status = PSA_ERROR_DATA_INVALID;
  214. goto exit;
  215. }
  216. data = (psa_se_key_data_storage_t *) key_data;
  217. status = psa_copy_key_material_into_slot(
  218. slot, data->slot_number, sizeof(data->slot_number));
  219. goto exit;
  220. }
  221. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  222. status = psa_copy_key_material_into_slot(slot, key_data, key_data_length);
  223. exit:
  224. psa_free_persistent_key_data(key_data, key_data_length);
  225. return status;
  226. }
  227. #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
  228. #if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
  229. static psa_status_t psa_load_builtin_key_into_slot(psa_key_slot_t *slot)
  230. {
  231. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  232. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  233. psa_key_lifetime_t lifetime = PSA_KEY_LIFETIME_VOLATILE;
  234. psa_drv_slot_number_t slot_number = 0;
  235. size_t key_buffer_size = 0;
  236. size_t key_buffer_length = 0;
  237. if (!psa_key_id_is_builtin(
  238. MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id))) {
  239. return PSA_ERROR_DOES_NOT_EXIST;
  240. }
  241. /* Check the platform function to see whether this key actually exists */
  242. status = mbedtls_psa_platform_get_builtin_key(
  243. slot->attr.id, &lifetime, &slot_number);
  244. if (status != PSA_SUCCESS) {
  245. return status;
  246. }
  247. /* Set required key attributes to ensure get_builtin_key can retrieve the
  248. * full attributes. */
  249. psa_set_key_id(&attributes, slot->attr.id);
  250. psa_set_key_lifetime(&attributes, lifetime);
  251. /* Get the full key attributes from the driver in order to be able to
  252. * calculate the required buffer size. */
  253. status = psa_driver_wrapper_get_builtin_key(
  254. slot_number, &attributes,
  255. NULL, 0, NULL);
  256. if (status != PSA_ERROR_BUFFER_TOO_SMALL) {
  257. /* Builtin keys cannot be defined by the attributes alone */
  258. if (status == PSA_SUCCESS) {
  259. status = PSA_ERROR_CORRUPTION_DETECTED;
  260. }
  261. return status;
  262. }
  263. /* If the key should exist according to the platform, then ask the driver
  264. * what its expected size is. */
  265. status = psa_driver_wrapper_get_key_buffer_size(&attributes,
  266. &key_buffer_size);
  267. if (status != PSA_SUCCESS) {
  268. return status;
  269. }
  270. /* Allocate a buffer of the required size and load the builtin key directly
  271. * into the (now properly sized) slot buffer. */
  272. status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
  273. if (status != PSA_SUCCESS) {
  274. return status;
  275. }
  276. status = psa_driver_wrapper_get_builtin_key(
  277. slot_number, &attributes,
  278. slot->key.data, slot->key.bytes, &key_buffer_length);
  279. if (status != PSA_SUCCESS) {
  280. goto exit;
  281. }
  282. /* Copy actual key length and core attributes into the slot on success */
  283. slot->key.bytes = key_buffer_length;
  284. slot->attr = attributes.core;
  285. exit:
  286. if (status != PSA_SUCCESS) {
  287. psa_remove_key_data_from_memory(slot);
  288. }
  289. return status;
  290. }
  291. #endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
  292. psa_status_t psa_get_and_lock_key_slot(mbedtls_svc_key_id_t key,
  293. psa_key_slot_t **p_slot)
  294. {
  295. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  296. *p_slot = NULL;
  297. if (!global_data.key_slots_initialized) {
  298. return PSA_ERROR_BAD_STATE;
  299. }
  300. /*
  301. * On success, the pointer to the slot is passed directly to the caller
  302. * thus no need to unlock the key slot here.
  303. */
  304. status = psa_get_and_lock_key_slot_in_memory(key, p_slot);
  305. if (status != PSA_ERROR_DOES_NOT_EXIST) {
  306. return status;
  307. }
  308. /* Loading keys from storage requires support for such a mechanism */
  309. #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
  310. defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
  311. psa_key_id_t volatile_key_id;
  312. status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
  313. if (status != PSA_SUCCESS) {
  314. return status;
  315. }
  316. (*p_slot)->attr.id = key;
  317. (*p_slot)->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
  318. status = PSA_ERROR_DOES_NOT_EXIST;
  319. #if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
  320. /* Load keys in the 'builtin' range through their own interface */
  321. status = psa_load_builtin_key_into_slot(*p_slot);
  322. #endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
  323. #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
  324. if (status == PSA_ERROR_DOES_NOT_EXIST) {
  325. status = psa_load_persistent_key_into_slot(*p_slot);
  326. }
  327. #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
  328. if (status != PSA_SUCCESS) {
  329. psa_wipe_key_slot(*p_slot);
  330. if (status == PSA_ERROR_DOES_NOT_EXIST) {
  331. status = PSA_ERROR_INVALID_HANDLE;
  332. }
  333. } else {
  334. /* Add implicit usage flags. */
  335. psa_extend_key_usage_flags(&(*p_slot)->attr.policy.usage);
  336. }
  337. return status;
  338. #else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
  339. return PSA_ERROR_INVALID_HANDLE;
  340. #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
  341. }
  342. psa_status_t psa_unlock_key_slot(psa_key_slot_t *slot)
  343. {
  344. if (slot == NULL) {
  345. return PSA_SUCCESS;
  346. }
  347. if (slot->lock_count > 0) {
  348. slot->lock_count--;
  349. return PSA_SUCCESS;
  350. }
  351. /*
  352. * As the return error code may not be handled in case of multiple errors,
  353. * do our best to report if the lock counter is equal to zero. Assert with
  354. * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is strictly greater
  355. * than zero: if the MBEDTLS_TEST_HOOKS configuration option is enabled and
  356. * the function is called as part of the execution of a test suite, the
  357. * execution of the test suite is stopped in error if the assertion fails.
  358. */
  359. MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count > 0);
  360. return PSA_ERROR_CORRUPTION_DETECTED;
  361. }
  362. psa_status_t psa_validate_key_location(psa_key_lifetime_t lifetime,
  363. psa_se_drv_table_entry_t **p_drv)
  364. {
  365. if (psa_key_lifetime_is_external(lifetime)) {
  366. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  367. /* Check whether a driver is registered against this lifetime */
  368. psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry(lifetime);
  369. if (driver != NULL) {
  370. if (p_drv != NULL) {
  371. *p_drv = driver;
  372. }
  373. return PSA_SUCCESS;
  374. }
  375. #else /* MBEDTLS_PSA_CRYPTO_SE_C */
  376. (void) p_drv;
  377. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  378. #if defined(MBEDTLS_PSA_CRYPTO_DRIVERS)
  379. /* Key location for external keys gets checked by the wrapper */
  380. return PSA_SUCCESS;
  381. #else /* MBEDTLS_PSA_CRYPTO_DRIVERS */
  382. /* No support for external lifetimes at all, or dynamic interface
  383. * did not find driver for requested lifetime. */
  384. return PSA_ERROR_INVALID_ARGUMENT;
  385. #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */
  386. } else {
  387. /* Local/internal keys are always valid */
  388. return PSA_SUCCESS;
  389. }
  390. }
  391. psa_status_t psa_validate_key_persistence(psa_key_lifetime_t lifetime)
  392. {
  393. if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
  394. /* Volatile keys are always supported */
  395. return PSA_SUCCESS;
  396. } else {
  397. /* Persistent keys require storage support */
  398. #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
  399. if (PSA_KEY_LIFETIME_IS_READ_ONLY(lifetime)) {
  400. return PSA_ERROR_INVALID_ARGUMENT;
  401. } else {
  402. return PSA_SUCCESS;
  403. }
  404. #else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
  405. return PSA_ERROR_NOT_SUPPORTED;
  406. #endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
  407. }
  408. }
  409. psa_status_t psa_open_key(mbedtls_svc_key_id_t key, psa_key_handle_t *handle)
  410. {
  411. #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
  412. defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
  413. psa_status_t status;
  414. psa_key_slot_t *slot;
  415. status = psa_get_and_lock_key_slot(key, &slot);
  416. if (status != PSA_SUCCESS) {
  417. *handle = PSA_KEY_HANDLE_INIT;
  418. if (status == PSA_ERROR_INVALID_HANDLE) {
  419. status = PSA_ERROR_DOES_NOT_EXIST;
  420. }
  421. return status;
  422. }
  423. *handle = key;
  424. return psa_unlock_key_slot(slot);
  425. #else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
  426. (void) key;
  427. *handle = PSA_KEY_HANDLE_INIT;
  428. return PSA_ERROR_NOT_SUPPORTED;
  429. #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
  430. }
  431. psa_status_t psa_close_key(psa_key_handle_t handle)
  432. {
  433. psa_status_t status;
  434. psa_key_slot_t *slot;
  435. if (psa_key_handle_is_null(handle)) {
  436. return PSA_SUCCESS;
  437. }
  438. status = psa_get_and_lock_key_slot_in_memory(handle, &slot);
  439. if (status != PSA_SUCCESS) {
  440. if (status == PSA_ERROR_DOES_NOT_EXIST) {
  441. status = PSA_ERROR_INVALID_HANDLE;
  442. }
  443. return status;
  444. }
  445. if (slot->lock_count <= 1) {
  446. return psa_wipe_key_slot(slot);
  447. } else {
  448. return psa_unlock_key_slot(slot);
  449. }
  450. }
  451. psa_status_t psa_purge_key(mbedtls_svc_key_id_t key)
  452. {
  453. psa_status_t status;
  454. psa_key_slot_t *slot;
  455. status = psa_get_and_lock_key_slot_in_memory(key, &slot);
  456. if (status != PSA_SUCCESS) {
  457. return status;
  458. }
  459. if ((!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) &&
  460. (slot->lock_count <= 1)) {
  461. return psa_wipe_key_slot(slot);
  462. } else {
  463. return psa_unlock_key_slot(slot);
  464. }
  465. }
  466. void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats)
  467. {
  468. size_t slot_idx;
  469. memset(stats, 0, sizeof(*stats));
  470. for (slot_idx = 0; slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT; slot_idx++) {
  471. const psa_key_slot_t *slot = &global_data.key_slots[slot_idx];
  472. if (psa_is_key_slot_locked(slot)) {
  473. ++stats->locked_slots;
  474. }
  475. if (!psa_is_key_slot_occupied(slot)) {
  476. ++stats->empty_slots;
  477. continue;
  478. }
  479. if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
  480. ++stats->volatile_slots;
  481. } else {
  482. psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
  483. ++stats->persistent_slots;
  484. if (id > stats->max_open_internal_key_id) {
  485. stats->max_open_internal_key_id = id;
  486. }
  487. }
  488. if (PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime) !=
  489. PSA_KEY_LOCATION_LOCAL_STORAGE) {
  490. psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
  491. ++stats->external_slots;
  492. if (id > stats->max_open_external_key_id) {
  493. stats->max_open_external_key_id = id;
  494. }
  495. }
  496. }
  497. }
  498. #endif /* MBEDTLS_PSA_CRYPTO_C */