diff --git a/components/esp-tls/Kconfig b/components/esp-tls/Kconfig index b92b788828..45ca958d26 100644 --- a/components/esp-tls/Kconfig +++ b/components/esp-tls/Kconfig @@ -26,7 +26,7 @@ menu "ESP-TLS" config ESP_TLS_USE_DS_PERIPHERAL bool "Use Digital Signature (DS) Peripheral with ESP-TLS" - depends on IDF_TARGET_ESP32S2 && ESP_TLS_USING_MBEDTLS + depends on (IDF_TARGET_ESP32C3 || IDF_TARGET_ESP32S2) && ESP_TLS_USING_MBEDTLS default y help Enable use of the Digital Signature Peripheral for ESP-TLS.The DS peripheral diff --git a/components/esp32c3/include/esp_ds.h b/components/esp32c3/include/esp_ds.h index cf54539b91..911e44c837 100644 --- a/components/esp32c3/include/esp_ds.h +++ b/components/esp32c3/include/esp_ds.h @@ -30,6 +30,7 @@ extern "C" { is produced anyway and can be read*/ #define ESP_DS_IV_BIT_LEN 128 +#define ESP_DS_IV_LEN (ESP_DS_IV_BIT_LEN / 8) #define ESP_DS_SIGNATURE_MAX_BIT_LEN 3072 #define ESP_DS_SIGNATURE_MD_BIT_LEN 256 #define ESP_DS_SIGNATURE_M_PRIME_BIT_LEN 32 diff --git a/components/mbedtls/CMakeLists.txt b/components/mbedtls/CMakeLists.txt index 36b7b0e5c5..5c4d6dc025 100644 --- a/components/mbedtls/CMakeLists.txt +++ b/components/mbedtls/CMakeLists.txt @@ -122,8 +122,9 @@ target_sources(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/esp_hardware.c" "${AES_DMA_SRCS}" ) +# CONFIG_ESP_TLS_USE_DS_PERIPHERAL can be enabled only for the supported targets. if(CONFIG_ESP_TLS_USE_DS_PERIPHERAL) - target_sources(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/esp32s2/esp_rsa_sign_alt.c") + target_sources(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/esp_ds/esp_rsa_sign_alt.c") endif() # Note: some mbedTLS hardware acceleration can be enabled/disabled by config. diff --git a/components/mbedtls/port/esp32s2/esp_rsa_sign_alt.c b/components/mbedtls/port/esp_ds/esp_rsa_sign_alt.c similarity index 97% rename from components/mbedtls/port/esp32s2/esp_rsa_sign_alt.c rename to components/mbedtls/port/esp_ds/esp_rsa_sign_alt.c index 9b8aff895e..258f05094e 100644 --- a/components/mbedtls/port/esp32s2/esp_rsa_sign_alt.c +++ b/components/mbedtls/port/esp_ds/esp_rsa_sign_alt.c @@ -14,7 +14,15 @@ #include "esp_ds.h" #include "rsa_sign_alt.h" + +#ifdef CONFIG_IDF_TARGET_ESP32S2 #include "esp32s2/rom/digital_signature.h" +#elif CONFIG_IDF_TARGET_ESP32C3 +#include "esp32c3/rom/digital_signature.h" +#else +#error "Selected target does not support esp_rsa_sign_alt (for DS)" +#endif + #include "esp_log.h" #include "esp_heap_caps.h" #include "freertos/FreeRTOS.h" diff --git a/components/mbedtls/port/include/esp32s2/esp_rsa_sign_alt.h b/components/mbedtls/port/include/esp_ds/esp_rsa_sign_alt.h similarity index 100% rename from components/mbedtls/port/include/esp32s2/esp_rsa_sign_alt.h rename to components/mbedtls/port/include/esp_ds/esp_rsa_sign_alt.h diff --git a/components/mbedtls/port/include/rsa_sign_alt.h b/components/mbedtls/port/include/rsa_sign_alt.h index 8eb3550102..aacac95563 100644 --- a/components/mbedtls/port/include/rsa_sign_alt.h +++ b/components/mbedtls/port/include/rsa_sign_alt.h @@ -22,9 +22,7 @@ extern "C" { #endif #ifdef CONFIG_ESP_TLS_USE_DS_PERIPHERAL - -#include "esp32s2/esp_rsa_sign_alt.h" - +#include "esp_ds/esp_rsa_sign_alt.h" #else #error "DS configuration flags not activated, please enable required menuconfig flags" diff --git a/examples/protocols/mqtt/ssl_ds/README.md b/examples/protocols/mqtt/ssl_ds/README.md index d3f8cdbc5f..20a06d09a7 100644 --- a/examples/protocols/mqtt/ssl_ds/README.md +++ b/examples/protocols/mqtt/ssl_ds/README.md @@ -1,8 +1,8 @@ -| Supported Targets | ESP32-S2 | +| Supported Targets | ESP32-S2 | ESP32-C3 | # ESP-MQTT SSL Mutual Authentication with Digital Signature (See the README.md file in the upper level 'examples' directory for more information about examples.) -Espressif's ESP32-S2 MCU has a built-in Digital Signature (DS) Peripheral, which provides hardware acceleration for RSA signature. More details can be found at [Digital Signature with ESP-TLS](https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/api-reference/protocols/esp_tls.html#digital-signature-with-esp-tls). +Espressif's ESP32-S2 and ESP32-C3 MCU have a built-in Digital Signature (DS) Peripheral, which provides hardware acceleration for RSA signature. More details can be found at [Digital Signature with ESP-TLS](https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/api-reference/protocols/esp_tls.html#digital-signature-with-esp-tls). This example connects to the broker test.mosquitto.org using ssl transport with client certificate(RSA) and as a demonstration subscribes/unsubscribes and sends a message on certain topic.The RSA signature operation required in the ssl connection is performed with help of the Digital Signature (DS) peripheral. (Please note that the public broker is maintained by the community so may not be always available, for details please visit http://test.mosquitto.org) @@ -12,14 +12,14 @@ It uses ESP-MQTT library which implements mqtt client to connect to mqtt broker. ### Hardware Required -This example can be executed on any ESP32-S2 board (which has a built-in DS peripheral), the only required interface is WiFi and connection to internet. +This example can be executed on any ESP32-S2, ESP32-C3 board (which has a built-in DS peripheral), the only required interface is WiFi and connection to internet. ### Configure the project #### 1) Selecting the target -As the project is to be built for the target ESP32-S2, it should be selected with the following command +As the project is to be built for the target ESP32-S2, ESP32-C3 it should be selected with the following command ``` -idf.py set-target esp32s2 +idf.py set-target /* target */ ``` More detials can be found at [Selecting the target](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/build-system.html#selecting-the-target). @@ -50,7 +50,7 @@ Please note, that the supplied file `client.crt` in the `main` directory is only python configure_ds.py --port /* USB COM port */ --private-key /* RSA priv key */ ``` -In the command USB COM port is nothing but the serial port to which the ESP32-S2 chip is connected. see +In the command USB COM port is nothing but the serial port to which the ESP chip is connected. see [check serial port](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/establish-serial-connection.html#check-port-on-windows) for more details. RSA private key is nothing but the client private key ( RSA ) generated in Step 2. @@ -99,7 +99,7 @@ DATA=data ### configure_ds.py -The script [configure_ds.py](./configure_ds.py) is used for configuring the DS peripheral on the ESP32-S2 SoC. The steps in the script are based on technical details of certain operations in the Digital Signature calculation, which can be found at Digital Signature Section of [ESP32-S2 TRM](https://www.espressif.com/sites/default/files/documentation/esp32-s2_technical_reference_manual_en.pdf) +The script [configure_ds.py](./configure_ds.py) is used for configuring the DS peripheral on the ESP32-S2/ESP32-C3 SoC. The steps in the script are based on technical details of certain operations in the Digital Signature calculation, which can be found at Digital Signature Section of [ESP32-S2 TRM](https://www.espressif.com/sites/default/files/documentation/esp32-s2_technical_reference_manual_en.pdf) The configuration script performs the following steps - diff --git a/examples/protocols/mqtt/ssl_ds/configure_ds.py b/examples/protocols/mqtt/ssl_ds/configure_ds.py index 0d5b29433e..b718052301 100644 --- a/examples/protocols/mqtt/ssl_ds/configure_ds.py +++ b/examples/protocols/mqtt/ssl_ds/configure_ds.py @@ -12,13 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. import argparse -import os -import sys import hashlib import hmac +import json +import os import struct import subprocess -import json +import sys + from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import serialization from cryptography.hazmat.primitives.asymmetric import rsa @@ -28,14 +29,14 @@ from cryptography.utils import int_to_bytes try: import nvs_partition_gen as nvs_gen except ImportError: - idf_path = os.getenv("IDF_PATH") + idf_path = os.getenv('IDF_PATH') if not idf_path or not os.path.exists(idf_path): - raise Exception("IDF_PATH not found") - sys.path.insert(0, os.path.join(idf_path, "components", "nvs_flash", "nvs_partition_generator")) + raise Exception('IDF_PATH not found') + sys.path.insert(0, os.path.join(idf_path, 'components', 'nvs_flash', 'nvs_partition_generator')) import nvs_partition_gen as nvs_gen # Check python version is proper or not to avoid script failure -assert sys.version_info >= (3, 6, 0), "Python version too low." +assert sys.version_info >= (3, 6, 0), 'Python version too low.' esp_ds_data_dir = 'esp_ds_data' # hmac_key_file is generated when HMAC_KEY is calculated, it is used when burning HMAC_KEY to efuse @@ -45,7 +46,8 @@ csv_filename = esp_ds_data_dir + '/pre_prov.csv' bin_filename = esp_ds_data_dir + '/pre_prov.bin' expected_json_path = os.path.join('build', 'config', 'sdkconfig.json') # Targets supported by the script -supported_targets = {'esp32s2'} +supported_targets = {'esp32s2', 'esp32c3'} +supported_key_size = {'esp32s2':[1024, 2048, 3072, 4096], 'esp32c3':[1024, 2048, 3072]} # @return @@ -57,7 +59,7 @@ def get_idf_target(): idf_target_read = sdkconfig['IDF_TARGET'] return idf_target_read else: - print("ERROR: IDF_TARGET has not been set for the supported targets," + print('ERROR: IDF_TARGET has not been set for the supported targets,' "\nplase execute command \"idf.py set-target {TARGET}\" in the example directory") return None @@ -87,16 +89,17 @@ def number_as_bytes(number, pad_bits=None): # privkey : path to the RSA private key # priv_key_pass : path to the RSA privaete key password # hmac_key : HMAC key value ( to calculate DS params) +# idf_target : The target chip for the script (e.g. esp32s2, esp32c3) # @info # The function calculates the encrypted private key parameters. # Consult the DS documentation (available for the ESP32-S2) in the esp-idf programming guide for more details about the variables and calculations. -def calculate_ds_parameters(privkey, priv_key_pass, hmac_key): +def calculate_ds_parameters(privkey, priv_key_pass, hmac_key, idf_target): private_key = load_privatekey(privkey, priv_key_pass) if not isinstance(private_key, rsa.RSAPrivateKey): - print("ERROR: Only RSA private keys are supported") + print('ERROR: Only RSA private keys are supported') sys.exit(-1) if hmac_key is None: - print("ERROR: hmac_key cannot be None") + print('ERROR: hmac_key cannot be None') sys.exit(-2) priv_numbers = private_key.private_numbers() @@ -104,9 +107,9 @@ def calculate_ds_parameters(privkey, priv_key_pass, hmac_key): Y = priv_numbers.d M = pub_numbers.n key_size = private_key.key_size - supported_key_size = [1024, 2048, 3072, 4096] - if key_size not in supported_key_size: - print("Key size not supported, supported sizes are" + str(supported_key_size)) + if key_size not in supported_key_size[idf_target]: + print('ERROR: Private key size {0} not supported for the target {1},\nthe supported key sizes are {2}' + .format(key_size, idf_target, str(supported_key_size[idf_target]))) sys.exit(-1) iv = os.urandom(16) @@ -117,25 +120,34 @@ def calculate_ds_parameters(privkey, priv_key_pass, hmac_key): mprime &= 0xFFFFFFFF length = key_size // 32 - 1 - aes_key = hmac.HMAC(hmac_key, b"\xFF" * 32, hashlib.sha256).digest() + # get max supported key size for the respective target + max_len = max(supported_key_size[idf_target]) + aes_key = hmac.HMAC(hmac_key, b'\xFF' * 32, hashlib.sha256).digest() - md_in = number_as_bytes(Y, 4096) + \ - number_as_bytes(M, 4096) + \ - number_as_bytes(rinv, 4096) + \ - struct.pack("efuse_key_id); if (esp_ret != ESP_OK) { - ESP_LOGE(TAG, "Error in efuse_key_id value from nvs, returned %02x", esp_ret); + ESP_LOGE(TAG, "Error in efuse_key_id value from nvs,\nreturned %02x (%s)", esp_ret, esp_err_to_name(esp_ret)); goto exit; } esp_ret = nvs_get_u16(esp_ds_nvs_handle, NVS_RSA_LEN, &ds_data_ctx->rsa_length_bits); if (esp_ret != ESP_OK) { - ESP_LOGE(TAG, "Error in reading rsa key length value from nvs, returned %02x", esp_ret); + ESP_LOGE(TAG, "Error in reading rsa key length value from nvs,\nreturned %02x (%s)", esp_ret, esp_err_to_name(esp_ret)); goto exit; } size_t blob_length = ESP_DS_C_LEN; esp_ret = nvs_get_blob(esp_ds_nvs_handle, NVS_CIPHER_C, (void *)(ds_data_ctx->esp_ds_data->c), &blob_length); if ((esp_ret != ESP_OK) || (blob_length != ESP_DS_C_LEN)) { - ESP_LOGE(TAG, "Error in reading initialization vector value from nvs,bytes_read = %d, returned %02x", blob_length, esp_ret); + ESP_LOGE(TAG, "Error in reading ciphertext_c value from nvs,bytes_read = %d,\nreturned %02x (%s)", blob_length, esp_ret, esp_err_to_name(esp_ret)); goto exit; } blob_length = ESP_DS_IV_LEN; esp_ret = nvs_get_blob(esp_ds_nvs_handle, NVS_IV, (void *)(ds_data_ctx->esp_ds_data->iv), &blob_length); if ((esp_ret != ESP_OK) || (blob_length != ESP_DS_IV_LEN)) { - ESP_LOGE(TAG, "Error in reading initialization vector value from nvs,bytes_read = %d, returned %02x", blob_length, esp_ret); + ESP_LOGE(TAG, "Error in reading initialization vector value from nvs,bytes_read = %d,\nreturned %02x (%s)", blob_length, esp_ret, esp_err_to_name(esp_ret)); goto exit; }