From 5ee161822c478da38af8142feb882b5cf95f39d8 Mon Sep 17 00:00:00 2001 From: "harshal.patil" Date: Thu, 14 May 2026 14:43:02 +0530 Subject: [PATCH] feat(tf-psa-crypto): Expose persistent key storage blob-size helper Add psa_persistent_key_storage_blob_size() so callers outside tf-psa-crypto can size the output buffer for psa_format_key_data_for_storage() without needing to know the internal psa_persistent_key_storage_format struct. --- tf-psa-crypto/core/psa_crypto_storage.c | 5 +++++ tf-psa-crypto/core/psa_crypto_storage.h | 15 +++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/tf-psa-crypto/core/psa_crypto_storage.c b/tf-psa-crypto/core/psa_crypto_storage.c index 05755c312..7b54a7718 100644 --- a/tf-psa-crypto/core/psa_crypto_storage.c +++ b/tf-psa-crypto/core/psa_crypto_storage.c @@ -233,6 +233,11 @@ typedef struct { uint8_t key_data[]; } psa_persistent_key_storage_format; +size_t psa_persistent_key_storage_blob_size(size_t key_data_length) +{ + return sizeof(psa_persistent_key_storage_format) + key_data_length; +} + void psa_format_key_data_for_storage(const uint8_t *data, const size_t data_length, const psa_key_attributes_t *attr, diff --git a/tf-psa-crypto/core/psa_crypto_storage.h b/tf-psa-crypto/core/psa_crypto_storage.h index b1b4c0a7a..30b3fe144 100644 --- a/tf-psa-crypto/core/psa_crypto_storage.h +++ b/tf-psa-crypto/core/psa_crypto_storage.h @@ -158,6 +158,21 @@ psa_status_t psa_destroy_persistent_key(const mbedtls_svc_key_id_t key); */ void psa_free_persistent_key_data(uint8_t *key_data, size_t key_data_length); +/** + * \brief Return the on-disk size of the PSA persistent key storage blob that + * psa_format_key_data_for_storage() will produce for a given key data + * length. + * + * Custom PSA ITS backends that synthesize blobs on read (rather than + * persisting them through psa_its_set()) can use this to size the output + * buffer without knowing the internal storage format layout. + * + * \param key_data_length Length of the key_data payload that will be passed + * to psa_format_key_data_for_storage(). + * \return Total blob size in bytes (storage header + key_data). + */ +size_t psa_persistent_key_storage_blob_size(size_t key_data_length); + /** * \brief Formats key data and metadata for persistent storage *