Merge branch 'feature/esp32c3_ds_mbedtls_integration' into 'master'

esp32c3/Digital Signature: mbedtls integration through ESP-TLS

Closes IDF-2267

See merge request espressif/esp-idf!12033
This commit is contained in:
Mahavir Jain 2021-01-22 17:06:46 +08:00
commit f5e51e7c1b
10 changed files with 91 additions and 70 deletions

View File

@ -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

View File

@ -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

View File

@ -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.

View File

@ -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"

View File

@ -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"

View File

@ -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 -

View File

@ -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("<II", mprime, length) + \
md_in = number_as_bytes(Y, max_len) + \
number_as_bytes(M, max_len) + \
number_as_bytes(rinv, max_len) + \
struct.pack('<II', mprime, length) + \
iv
assert len(md_in) == 12480 / 8
md = hashlib.sha256(md_in).digest()
# expected_len = max_len_Y + max_len_M + max_len_rinv + (mprime + length packed (8 bytes))+ iv (16 bytes)
expected_len = (max_len / 8) * 3 + 8 + 16
assert len(md_in) == expected_len
md = hashlib.sha256(md_in).digest()
# In case of ESP32-S2
# Y4096 || M4096 || Rb4096 || M_prime32 || LENGTH32 || MD256 || 0x08*8
p = number_as_bytes(Y, 4096) + \
number_as_bytes(M, 4096) + \
number_as_bytes(rinv, 4096) + \
# In case of ESP32-C3
# Y3072 || M3072 || Rb3072 || M_prime32 || LENGTH32 || MD256 || 0x08*8
p = number_as_bytes(Y, max_len) + \
number_as_bytes(M, max_len) + \
number_as_bytes(rinv, max_len) + \
md + \
struct.pack("<II", mprime, length) + \
struct.pack('<II', mprime, length) + \
b'\x08' * 8
assert len(p) == 12672 / 8
# expected_len = max_len_Y + max_len_M + max_len_rinv + md (32 bytes) + (mprime + length packed (8bytes)) + padding (8 bytes)
expected_len = (max_len / 8) * 3 + 32 + 8 + 8
assert len(p) == expected_len
cipher = Cipher(algorithms.AES(aes_key), modes.CBC(iv), backend=default_backend())
encryptor = cipher.encryptor()
@ -146,7 +158,7 @@ def calculate_ds_parameters(privkey, priv_key_pass, hmac_key):
# @info
# The function makes use of the "espefuse.py" script to read the efuse summary
def efuse_summary(args, idf_target):
os.system("python $IDF_PATH/components/esptool_py/esptool/espefuse.py --chip {0} -p {1} summary".format(idf_target, (args.port)))
os.system('python $IDF_PATH/components/esptool_py/esptool/espefuse.py --chip {0} -p {1} summary'.format(idf_target, (args.port)))
# @info
@ -160,9 +172,9 @@ def efuse_burn_key(args, idf_target):
# read protection will be enabled as the default behaviour of the command
key_block_status = ' '
os.system("python $IDF_PATH/components/esptool_py/esptool/espefuse.py --chip {0} -p {1} burn_key "
"{2} {3} HMAC_DOWN_DIGITAL_SIGNATURE {4}"
.format((idf_target), (args.port), ("BLOCK_KEY" + str(args.efuse_key_id)), (hmac_key_file), (key_block_status)))
os.system('python $IDF_PATH/components/esptool_py/esptool/espefuse.py --chip {0} -p {1} burn_key '
'{2} {3} HMAC_DOWN_DIGITAL_SIGNATURE {4}'
.format((idf_target), (args.port), ('BLOCK_KEY' + str(args.efuse_key_id)), (hmac_key_file), (key_block_status)))
# @info
@ -171,12 +183,12 @@ def efuse_burn_key(args, idf_target):
def generate_csv_file(c, iv, hmac_key_id, key_size, csv_file):
with open(csv_file, 'wt', encoding='utf8') as f:
f.write("# This is a generated csv file containing required parameters for the Digital Signature operaiton\n")
f.write("key,type,encoding,value\nesp_ds_ns,namespace,,\n")
f.write("esp_ds_c,data,hex2bin,%s\n" % (c.hex()))
f.write("esp_ds_iv,data,hex2bin,%s\n" % (iv.hex()))
f.write("esp_ds_key_id,data,u8,%d\n" % (hmac_key_id))
f.write("esp_ds_rsa_len,data,u16,%d\n" % (key_size))
f.write('# This is a generated csv file containing required parameters for the Digital Signature operation\n')
f.write('key,type,encoding,value\nesp_ds_ns,namespace,,\n')
f.write('esp_ds_c,data,hex2bin,%s\n' % (c.hex()))
f.write('esp_ds_iv,data,hex2bin,%s\n' % (iv.hex()))
f.write('esp_ds_key_id,data,u8,%d\n' % (hmac_key_id))
f.write('esp_ds_rsa_len,data,u16,%d\n' % (key_size))
class DefineArgs(object):
@ -207,8 +219,8 @@ def generate_nvs_partition(input_filename, output_filename):
def get_efuse_summary_json(args, idf_target):
_efuse_summary = None
try:
_efuse_summary = subprocess.check_output(("python $IDF_PATH/components/esptool_py/esptool/espefuse.py "
"--chip {0} -p {1} summary --format json".format(idf_target, (args.port))), shell=True)
_efuse_summary = subprocess.check_output(('python $IDF_PATH/components/esptool_py/esptool/espefuse.py '
'--chip {0} -p {1} summary --format json'.format(idf_target, (args.port))), shell=True)
except subprocess.CalledProcessError as e:
print((e.output).decode('UTF-8'))
sys.exit(-1)
@ -260,8 +272,8 @@ def configure_efuse_key_block(args, idf_target):
if new_hmac_key == hmac_key_read:
print('Key was successfully written to the efuse (KEY BLOCK %1d)' % (args.efuse_key_id))
else:
print("ERROR: Failed to burn the hmac key to efuse (KEY BLOCK %1d),"
"\nPlease execute the script again using a different key id" % (args.efuse_key_id))
print('ERROR: Failed to burn the hmac key to efuse (KEY BLOCK %1d),'
'\nPlease execute the script again using a different key id' % (args.efuse_key_id))
return None
else:
# If the efuse key block is redable, then read the key from efuse block and use it for encrypting the RSA private key parameters.
@ -269,20 +281,20 @@ def configure_efuse_key_block(args, idf_target):
# value than "HMAC_DOWN_DIGITAL_SIGNATURE" then we cannot use it for DS operation
if kb_readable is True:
if efuse_summary_json[key_purpose]['value'] == 'HMAC_DOWN_DIGITAL_SIGNATURE':
print("Provided efuse key block (KEY BLOCK %1d) already contains a key with key_purpose=HMAC_DOWN_DIGITAL_SIGNATURE,"
"\nusing the same key for encrypting the private key data...\n" % (args.efuse_key_id))
print('Provided efuse key block (KEY BLOCK %1d) already contains a key with key_purpose=HMAC_DOWN_DIGITAL_SIGNATURE,'
'\nusing the same key for encrypting the private key data...\n' % (args.efuse_key_id))
hmac_key_read = efuse_summary_json[key_blk]['value']
hmac_key_read = bytes.fromhex(hmac_key_read)
if args.keep_ds_data is True:
with open(hmac_key_file, 'wb') as key_file:
key_file.write(hmac_key_read)
else:
print("ERROR: Provided efuse key block ((KEY BLOCK %1d)) contains a key with key purpose different"
"than HMAC_DOWN_DIGITAL_SIGNATURE,\nplease execute the script again with a different value of the efuse key id." % (args.efuse_key_id))
print('ERROR: Provided efuse key block ((KEY BLOCK %1d)) contains a key with key purpose different'
'than HMAC_DOWN_DIGITAL_SIGNATURE,\nplease execute the script again with a different value of the efuse key id.' % (args.efuse_key_id))
return None
else:
print("ERROR: Provided efuse key block (KEY BLOCK %1d) is not readable and writeable,"
"\nplease execute the script again with a different value of the efuse key id." % (args.efuse_key_id))
print('ERROR: Provided efuse key block (KEY BLOCK %1d) is not readable and writeable,'
'\nplease execute the script again with a different value of the efuse key id.' % (args.efuse_key_id))
return None
# Return the hmac key read from the efuse
@ -310,7 +322,7 @@ def main():
help='relative path to client private key')
parser.add_argument(
"--pwd", '--password',
'--pwd', '--password',
dest='priv_key_pass',
metavar='[password]',
help='the password associated with the private key')
@ -328,7 +340,7 @@ def main():
help='Provide the efuse key_id which contains/will contain HMAC_KEY, default is 1')
parser.add_argument(
"--port", '-p',
'--port', '-p',
dest='port',
metavar='[port]',
required=True,
@ -371,7 +383,7 @@ def main():
sys.exit(-1)
# Calculate the encrypted private key data along with all other parameters
c, iv, key_size = calculate_ds_parameters(args.privkey, args.priv_key_pass, hmac_key_read)
c, iv, key_size = calculate_ds_parameters(args.privkey, args.priv_key_pass, hmac_key_read, idf_target)
# Generate csv file for the DS data and generate an NVS partition.
generate_csv_file(c, iv, args.efuse_key_id, key_size, csv_filename)
@ -379,5 +391,5 @@ def main():
cleanup(args)
if __name__ == "__main__":
if __name__ == '__main__':
main()

View File

@ -1,2 +1,3 @@
idf_component_register(SRCS "app_main.c"
INCLUDE_DIRS ".")
INCLUDE_DIRS "."
REQUIRED_IDF_TARGETS esp32s2 esp32c3)

View File

@ -117,40 +117,40 @@ void *esp_read_ds_data_from_nvs(void)
esp_err_t esp_ret;
esp_ret = nvs_flash_init_partition(NVS_PARTITION_NAME);
if (esp_ret != ESP_OK) {
ESP_LOGE(TAG, "Error in esp_ds_nvs partition init, returned %02x", esp_ret);
ESP_LOGE(TAG, "Error in esp_ds_nvs partition init,\nreturned %02x (%s)", esp_ret, esp_err_to_name(esp_ret));
goto exit;
}
esp_ret = nvs_open_from_partition(NVS_PARTITION_NAME, NVS_NAMESPACE,
NVS_READONLY, &esp_ds_nvs_handle);
if (esp_ret != ESP_OK) {
ESP_LOGE(TAG, "Error in esp_ds_nvs partition open, returned %02x", esp_ret);
ESP_LOGE(TAG, "Error in esp_ds_nvs partition open,\nreturned %02x (%s)", esp_ret, esp_err_to_name(esp_ret));
goto exit;
}
esp_ret = nvs_get_u8(esp_ds_nvs_handle, NVS_EFUSE_KEY_ID, &ds_data_ctx->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;
}