feat: migrates storage examples to PSA APIs

This commit is contained in:
Ashish Sharma
2025-12-23 11:09:37 +08:00
committed by Mahavir Jain
parent ac79c16f89
commit e78bdb1aff
2 changed files with 25 additions and 9 deletions
+1 -1
View File
@@ -9,7 +9,7 @@ examples/storage/nvs/nvs_bootloader:
- if: CONFIG_NAME == "nvs_enc_hmac" and (SOC_HMAC_SUPPORTED != 1 or (SOC_HMAC_SUPPORTED == 1 and (SOC_AES_SUPPORTED != 1 and ESP_ROM_HAS_MBEDTLS_CRYPTO_LIB != 1))) - if: CONFIG_NAME == "nvs_enc_hmac" and (SOC_HMAC_SUPPORTED != 1 or (SOC_HMAC_SUPPORTED == 1 and (SOC_AES_SUPPORTED != 1 and ESP_ROM_HAS_MBEDTLS_CRYPTO_LIB != 1)))
reason: As of now in such cases, we do not have any way to perform AES operations in the bootloader build reason: As of now in such cases, we do not have any way to perform AES operations in the bootloader build
# TODO: IDF-15012 # TODO: IDF-15012
- if: IDF_TARGET in ["esp32c2"] and CONFIG_NAME in ["nvs_enc_flash_enc", "nvs_enc_hmac"] - if: IDF_TARGET in ["esp32c2"] and CONFIG_NAME in ["nvs_enc_flash_enc", "nvs_enc_hmac", "default"]
reason: PSA is not yet available for ESP32-C2 reason: PSA is not yet available for ESP32-C2
examples/storage/nvs/nvs_console: examples/storage/nvs/nvs_console:
@@ -1,6 +1,6 @@
/* SPIFFS Image Generation on Build Example /* SPIFFS Image Generation on Build Example
* *
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Unlicense or CC0-1.0 * SPDX-License-Identifier: Unlicense or CC0-1.0
*/ */
@@ -11,7 +11,7 @@
#include "esp_err.h" #include "esp_err.h"
#include "esp_log.h" #include "esp_log.h"
#include "esp_spiffs.h" #include "esp_spiffs.h"
#include "mbedtls/md5.h" #include "psa/crypto.h"
static const char *TAG = "example"; static const char *TAG = "example";
@@ -50,20 +50,36 @@ static void compute_alice_txt_md5(void)
#define MD5_MAX_LEN 16 #define MD5_MAX_LEN 16
char buf[64]; char buf[64];
mbedtls_md5_context ctx; psa_status_t status;
unsigned char digest[MD5_MAX_LEN]; psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
psa_algorithm_t alg = PSA_ALG_MD5;
status = psa_hash_setup(&operation, alg);
if (status != PSA_SUCCESS) {
ESP_LOGE(TAG, "Failed to setup hash operation");
return;
}
mbedtls_md5_init(&ctx); size_t md5_len = PSA_HASH_LENGTH(alg);
mbedtls_md5_starts(&ctx);
unsigned char digest[md5_len];
size_t read; size_t read;
do { do {
read = fread((void*) buf, 1, sizeof(buf), f); read = fread((void*) buf, 1, sizeof(buf), f);
mbedtls_md5_update(&ctx, (unsigned const char*) buf, read); status = psa_hash_update(&operation, (unsigned const char*) buf, read);
if (status != PSA_SUCCESS) {
ESP_LOGE(TAG, "Failed to update hash operation");
return;
}
} while(read == sizeof(buf)); } while(read == sizeof(buf));
mbedtls_md5_finish(&ctx, digest); size_t md5len = 0;
status = psa_hash_finish(&operation, digest, md5_len, &md5len);
if (status != PSA_SUCCESS) {
ESP_LOGE(TAG, "Failed to finish hash operation");
return;
}
// Create a string of the digest // Create a string of the digest
char digest_str[MD5_MAX_LEN * 2]; char digest_str[MD5_MAX_LEN * 2];