mirror of
https://github.com/espressif/esp-idf.git
synced 2026-08-13 04:42:01 +03:00
feat(mbedtls): Introduce ESP-HMAC PSA opaque driver
This commit is contained in:
@@ -71,7 +71,7 @@ Key purpose value: 8
|
||||
|
||||
In this case, the HMAC is given out to the software, e.g., to authenticate a message.
|
||||
|
||||
The API to calculate the HMAC is :cpp:func:`esp_hmac_calculate`. The input arguments for the function are the message, message length, and the eFuse key block ID which contains the secret and has the efuse key purpose set to Upstream mode.
|
||||
The API to calculate the HMAC is :cpp:func:`psa_mac_compute` with an opaque PSA key with the eFuse key block ID which contains the secret and has the efuse key purpose set to Upstream mode.
|
||||
|
||||
HMAC for Digital Signature
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
@@ -160,20 +160,53 @@ We use ``esp_efuse_write_key`` to set physical key block 4 in the eFuse for the
|
||||
// writing key failed, maybe written already
|
||||
}
|
||||
|
||||
Now we can use the saved key to calculate an HMAC for software usage.
|
||||
Now we can use the saved key to calculate an HMAC for software usage using the PSA Crypto API.
|
||||
|
||||
.. code-block:: c
|
||||
|
||||
#include "esp_hmac.h"
|
||||
#include "psa/crypto.h"
|
||||
#include "psa_crypto_driver_esp_hmac_opaque.h"
|
||||
|
||||
uint8_t hmac[32];
|
||||
size_t hmac_length = 0;
|
||||
|
||||
const char *message = "Hello, HMAC!";
|
||||
const size_t msg_len = 12;
|
||||
|
||||
esp_err_t result = esp_hmac_calculate(HMAC_KEY4, message, msg_len, hmac);
|
||||
// Setup key attributes for ESP-HMAC opaque driver
|
||||
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
|
||||
psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(PSA_ALG_SHA_256));
|
||||
psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
|
||||
psa_set_key_bits(&attributes, 256);
|
||||
psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_ESP_HMAC_VOLATILE);
|
||||
|
||||
if (result == ESP_OK) {
|
||||
// Create opaque key reference
|
||||
esp_hmac_opaque_key_t opaque_key = {
|
||||
.use_km_key = false,
|
||||
.efuse_block = EFUSE_BLK_KEY4,
|
||||
};
|
||||
|
||||
// Import the opaque key
|
||||
psa_key_id_t key_id = 0;
|
||||
psa_status_t status = psa_import_key(&attributes, (uint8_t *)&opaque_key,
|
||||
sizeof(opaque_key), &key_id);
|
||||
if (status != PSA_SUCCESS) {
|
||||
// Failed to import key
|
||||
psa_reset_key_attributes(&attributes);
|
||||
return;
|
||||
}
|
||||
|
||||
// Compute HMAC
|
||||
status = psa_mac_compute(key_id, PSA_ALG_HMAC(PSA_ALG_SHA_256),
|
||||
(uint8_t *)message, msg_len,
|
||||
hmac, sizeof(hmac), &hmac_length);
|
||||
|
||||
// Clean up
|
||||
psa_destroy_key(key_id);
|
||||
psa_reset_key_attributes(&attributes);
|
||||
|
||||
if (status == PSA_SUCCESS) {
|
||||
// HMAC written to hmac now
|
||||
} else {
|
||||
// failure calculating HMAC
|
||||
@@ -182,4 +215,4 @@ Now we can use the saved key to calculate an HMAC for software usage.
|
||||
API Reference
|
||||
-------------
|
||||
|
||||
.. include-build-file:: inc/esp_hmac.inc
|
||||
.. include-build-file:: inc/psa_crypto_driver_esp_hmac_opaque_contexts.inc
|
||||
|
||||
Reference in New Issue
Block a user