|
@@ -1,41 +1,51 @@
|
|
|
/*
|
|
/*
|
|
|
- * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
|
|
|
|
|
|
|
+ * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
|
|
|
*
|
|
*
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
*/
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
#include <string.h>
|
|
|
-#include "esp_private/periph_ctrl.h"
|
|
|
|
|
-#include "esp32s3/rom/efuse.h"
|
|
|
|
|
-#include "esp32s3/rom/hmac.h"
|
|
|
|
|
-#include "esp32s3/rom/ets_sys.h"
|
|
|
|
|
|
|
+#include "rom/efuse.h"
|
|
|
|
|
+#include "rom/hmac.h"
|
|
|
|
|
+#include "rom/ets_sys.h"
|
|
|
#include "esp_efuse.h"
|
|
#include "esp_efuse.h"
|
|
|
#include "esp_efuse_table.h"
|
|
#include "esp_efuse_table.h"
|
|
|
#include "esp_hmac.h"
|
|
#include "esp_hmac.h"
|
|
|
#include "esp_log.h"
|
|
#include "esp_log.h"
|
|
|
#include "esp_crypto_lock.h"
|
|
#include "esp_crypto_lock.h"
|
|
|
#include "soc/hwcrypto_reg.h"
|
|
#include "soc/hwcrypto_reg.h"
|
|
|
|
|
+#include "soc/system_reg.h"
|
|
|
|
|
|
|
|
|
|
+#if !CONFIG_IDF_TARGET_ESP32S2
|
|
|
#include "hal/hmac_hal.h"
|
|
#include "hal/hmac_hal.h"
|
|
|
|
|
+#include "esp_private/periph_ctrl.h"
|
|
|
|
|
+#endif
|
|
|
|
|
|
|
|
#define SHA256_BLOCK_SZ 64
|
|
#define SHA256_BLOCK_SZ 64
|
|
|
#define SHA256_PAD_SZ 8
|
|
#define SHA256_PAD_SZ 8
|
|
|
|
|
|
|
|
|
|
+#if defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32S2)
|
|
|
|
|
+#define JTAG_STATUS_BIT ESP_EFUSE_HARD_DIS_JTAG
|
|
|
|
|
+#else
|
|
|
|
|
+/* For ESP32C3, ESP32C6, ESP32H2 */
|
|
|
|
|
+#define JTAG_STATUS_BIT ESP_EFUSE_DIS_PAD_JTAG
|
|
|
|
|
+#endif
|
|
|
static const char *TAG = "esp_hmac";
|
|
static const char *TAG = "esp_hmac";
|
|
|
|
|
|
|
|
|
|
+#if !CONFIG_IDF_TARGET_ESP32S2
|
|
|
/**
|
|
/**
|
|
|
* @brief Apply the HMAC padding without the embedded length.
|
|
* @brief Apply the HMAC padding without the embedded length.
|
|
|
*
|
|
*
|
|
|
* @note This function does not check the data length, it is the responsibility of the other functions in this
|
|
* @note This function does not check the data length, it is the responsibility of the other functions in this
|
|
|
* module to make sure that \c data_len is at most SHA256_BLOCK_SZ - 1 so the padding fits in.
|
|
* module to make sure that \c data_len is at most SHA256_BLOCK_SZ - 1 so the padding fits in.
|
|
|
* Otherwise, this function has undefined behavior.
|
|
* Otherwise, this function has undefined behavior.
|
|
|
- * Note however, that for the actual HMAC implementation on ESP32S3, the length also needs to be applied at the end
|
|
|
|
|
|
|
+ * Note however, that for the actual HMAC implementation, the length also needs to be applied at the end
|
|
|
* of the block. This function alone deosn't do that.
|
|
* of the block. This function alone deosn't do that.
|
|
|
*/
|
|
*/
|
|
|
static void write_and_padd(uint8_t *block, const uint8_t *data, uint16_t data_len)
|
|
static void write_and_padd(uint8_t *block, const uint8_t *data, uint16_t data_len)
|
|
|
{
|
|
{
|
|
|
memcpy(block, data, data_len);
|
|
memcpy(block, data, data_len);
|
|
|
- // Apply a one bit, followed by zero bits (refer to the ESP32S3 TRM).
|
|
|
|
|
|
|
+ // Apply a one bit, followed by zero bits (refer to the TRM of respective target).
|
|
|
block[data_len] = 0x80;
|
|
block[data_len] = 0x80;
|
|
|
bzero(block + data_len + 1, SHA256_BLOCK_SZ - data_len - 1);
|
|
bzero(block + data_len + 1, SHA256_BLOCK_SZ - data_len - 1);
|
|
|
}
|
|
}
|
|
@@ -143,7 +153,7 @@ esp_err_t esp_hmac_jtag_enable(hmac_key_id_t key_id, const uint8_t *token)
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
|
|
|
|
/* Check if JTAG is permanently disabled by HW Disable eFuse */
|
|
/* Check if JTAG is permanently disabled by HW Disable eFuse */
|
|
|
- if (esp_efuse_read_field_bit(ESP_EFUSE_HARD_DIS_JTAG)) {
|
|
|
|
|
|
|
+ if (esp_efuse_read_field_bit(JTAG_STATUS_BIT)) {
|
|
|
ESP_LOGE(TAG, "JTAG disabled permanently.");
|
|
ESP_LOGE(TAG, "JTAG disabled permanently.");
|
|
|
return ESP_FAIL;
|
|
return ESP_FAIL;
|
|
|
}
|
|
}
|
|
@@ -170,12 +180,94 @@ esp_err_t esp_hmac_jtag_enable(hmac_key_id_t key_id, const uint8_t *token)
|
|
|
esp_err_t esp_hmac_jtag_disable()
|
|
esp_err_t esp_hmac_jtag_disable()
|
|
|
{
|
|
{
|
|
|
esp_crypto_hmac_lock_acquire();
|
|
esp_crypto_hmac_lock_acquire();
|
|
|
|
|
+ REG_WRITE(HMAC_SET_INVALIDATE_JTAG_REG, 1);
|
|
|
|
|
+ esp_crypto_hmac_lock_release();
|
|
|
|
|
|
|
|
- REG_SET_BIT(HMAC_SET_INVALIDATE_JTAG_REG, HMAC_INVALIDATE_JTAG);
|
|
|
|
|
|
|
+ ESP_LOGD(TAG, "Invalidate JTAG result register. JTAG disabled.");
|
|
|
|
|
|
|
|
- esp_crypto_hmac_lock_release();
|
|
|
|
|
|
|
+ return ESP_OK;
|
|
|
|
|
+}
|
|
|
|
|
+#else /* !CONFIG_IDF_TARGET_ESP32S2 */
|
|
|
|
|
+
|
|
|
|
|
+static ets_efuse_block_t convert_key_type(hmac_key_id_t key_id) {
|
|
|
|
|
+ return ETS_EFUSE_BLOCK_KEY0 + (ets_efuse_block_t) key_id;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+esp_err_t esp_hmac_calculate(hmac_key_id_t key_id,
|
|
|
|
|
+ const void *message,
|
|
|
|
|
+ size_t message_len,
|
|
|
|
|
+ uint8_t *hmac)
|
|
|
|
|
+{
|
|
|
|
|
+ int hmac_ret;
|
|
|
|
|
+ if (!message || !hmac) return ESP_ERR_INVALID_ARG;
|
|
|
|
|
+ if (key_id >= HMAC_KEY_MAX) return ESP_ERR_INVALID_ARG;
|
|
|
|
|
+
|
|
|
|
|
+ esp_crypto_dma_lock_acquire();
|
|
|
|
|
+
|
|
|
|
|
+ ets_hmac_enable();
|
|
|
|
|
+ hmac_ret = ets_hmac_calculate_message(convert_key_type(key_id), message, message_len, hmac);
|
|
|
|
|
+ ets_hmac_disable();
|
|
|
|
|
+
|
|
|
|
|
+ esp_crypto_dma_lock_release();
|
|
|
|
|
+
|
|
|
|
|
+ if (hmac_ret != 0) {
|
|
|
|
|
+ return ESP_FAIL;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return ESP_OK;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+esp_err_t esp_hmac_jtag_enable(hmac_key_id_t key_id, const uint8_t *token)
|
|
|
|
|
+{
|
|
|
|
|
+ int ets_status;
|
|
|
|
|
+ esp_err_t err = ESP_OK;
|
|
|
|
|
+
|
|
|
|
|
+ if ((!token) || (key_id >= HMAC_KEY_MAX))
|
|
|
|
|
+ return ESP_ERR_INVALID_ARG;
|
|
|
|
|
+
|
|
|
|
|
+ /* Check if JTAG is permanently disabled by HW Disable eFuse */
|
|
|
|
|
+ if (esp_efuse_read_field_bit(ESP_EFUSE_HARD_DIS_JTAG)) {
|
|
|
|
|
+ ESP_LOGE(TAG, "JTAG disabled permanently.");
|
|
|
|
|
+ return ESP_FAIL;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ esp_crypto_dma_lock_acquire();
|
|
|
|
|
+
|
|
|
|
|
+ ets_hmac_enable();
|
|
|
|
|
+
|
|
|
|
|
+ /* Token updating into HMAC module. */
|
|
|
|
|
+ for (int i = 0; i < 32; i += 4) {
|
|
|
|
|
+ uint32_t key_word;
|
|
|
|
|
+ memcpy(&key_word, &token[i], 4);
|
|
|
|
|
+ REG_WRITE(DPORT_JTAG_CTRL_0_REG + i, __builtin_bswap32(key_word));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ ets_status = ets_hmac_calculate_downstream(convert_key_type(key_id), ETS_EFUSE_KEY_PURPOSE_HMAC_DOWN_JTAG);
|
|
|
|
|
+ if (ets_status != ETS_OK) {
|
|
|
|
|
+ err = ESP_FAIL;
|
|
|
|
|
+ ESP_LOGE(TAG, "HMAC downstream JTAG enable mode setting failed. (%d)", err);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ ESP_LOGD(TAG, "HMAC computation in downstream mode is completed.");
|
|
|
|
|
+
|
|
|
|
|
+ ets_hmac_disable();
|
|
|
|
|
+
|
|
|
|
|
+ esp_crypto_dma_lock_release();
|
|
|
|
|
+
|
|
|
|
|
+ return err;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+esp_err_t esp_hmac_jtag_disable()
|
|
|
|
|
+{
|
|
|
|
|
+ esp_crypto_dma_lock_acquire();
|
|
|
|
|
+
|
|
|
|
|
+ REG_WRITE(HMAC_SET_INVALIDATE_JTAG_REG, 1);
|
|
|
|
|
+
|
|
|
|
|
+ esp_crypto_dma_lock_release();
|
|
|
|
|
|
|
|
ESP_LOGD(TAG, "Invalidate JTAG result register. JTAG disabled.");
|
|
ESP_LOGD(TAG, "Invalidate JTAG result register. JTAG disabled.");
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
return ESP_OK;
|
|
|
}
|
|
}
|
|
|
|
|
+#endif /* CONFIG_IDF_TARGET_ESP32S2*/
|