2019-08-23 04:37:55 +00:00
|
|
|
from __future__ import print_function
|
2020-09-16 22:06:54 +00:00
|
|
|
import binascii
|
|
|
|
from io import BytesIO
|
|
|
|
from collections import namedtuple
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2019-11-27 03:58:07 +00:00
|
|
|
import ttfw_idf
|
2020-09-16 22:06:54 +00:00
|
|
|
try:
|
|
|
|
import espsecure
|
|
|
|
except ImportError:
|
|
|
|
idf_path = os.getenv("IDF_PATH")
|
|
|
|
if not idf_path or not os.path.exists(idf_path):
|
|
|
|
raise
|
|
|
|
sys.path.insert(0, os.path.join(idf_path, "components", "esptool_py", "esptool"))
|
|
|
|
import espsecure
|
2019-08-23 04:37:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
# To prepare a test runner for this example:
|
|
|
|
# 1. Generate zero flash encryption key:
|
|
|
|
# dd if=/dev/zero of=key.bin bs=1 count=32
|
|
|
|
# 2.Burn Efuses:
|
|
|
|
# espefuse.py --do-not-confirm -p $ESPPORT burn_efuse FLASH_CRYPT_CONFIG 0xf
|
|
|
|
# espefuse.py --do-not-confirm -p $ESPPORT burn_efuse FLASH_CRYPT_CNT 0x1
|
|
|
|
# espefuse.py --do-not-confirm -p $ESPPORT burn_key flash_encryption key.bin
|
2019-11-27 03:58:07 +00:00
|
|
|
@ttfw_idf.idf_example_test(env_tag='Example_Flash_Encryption')
|
2019-08-23 04:37:55 +00:00
|
|
|
def test_examples_security_flash_encryption(env, extra_data):
|
2019-11-27 03:58:07 +00:00
|
|
|
dut = env.get_dut('flash_encryption', 'examples/security/flash_encryption', dut_class=ttfw_idf.ESP32DUT)
|
2019-08-23 04:37:55 +00:00
|
|
|
# start test
|
|
|
|
dut.start_app()
|
2020-09-16 22:06:54 +00:00
|
|
|
|
|
|
|
# calculate the expected ciphertext
|
|
|
|
flash_addr = dut.app.partition_table["storage"]["offset"]
|
|
|
|
plain_hex_str = '00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f'
|
|
|
|
plain_data = binascii.unhexlify(plain_hex_str.replace(' ', ''))
|
|
|
|
|
|
|
|
# Emulate espsecure encrypt_flash_data command
|
2021-01-15 08:04:59 +00:00
|
|
|
EncryptFlashDataArgs = namedtuple('EncryptFlashDataArgs', ['output', 'plaintext_file', 'address', 'keyfile', 'flash_crypt_conf', 'aes_xts'])
|
|
|
|
args = EncryptFlashDataArgs(BytesIO(), BytesIO(plain_data), flash_addr, BytesIO(b'\x00' * 32), 0xF, None)
|
2020-09-16 22:06:54 +00:00
|
|
|
espsecure.encrypt_flash_data(args)
|
|
|
|
|
|
|
|
expected_ciphertext = args.output.getvalue()
|
|
|
|
hex_ciphertext = binascii.hexlify(expected_ciphertext).decode('ascii')
|
|
|
|
expected_str = (' '.join(hex_ciphertext[i:i + 2] for i in range(0, 16, 2)) + ' ' +
|
|
|
|
' '.join(hex_ciphertext[i:i + 2] for i in range(16, 32, 2)))
|
|
|
|
|
2019-08-23 04:37:55 +00:00
|
|
|
lines = [
|
|
|
|
'FLASH_CRYPT_CNT eFuse value is 1',
|
|
|
|
'Flash encryption feature is enabled in DEVELOPMENT mode',
|
|
|
|
'with esp_partition_write',
|
2020-09-16 22:06:54 +00:00
|
|
|
plain_hex_str,
|
2019-08-23 04:37:55 +00:00
|
|
|
'with esp_partition_read',
|
2020-09-16 22:06:54 +00:00
|
|
|
plain_hex_str,
|
2019-08-23 04:37:55 +00:00
|
|
|
'with spi_flash_read',
|
2020-12-15 03:01:39 +00:00
|
|
|
expected_str,
|
|
|
|
# The status of NVS encryption for the "nvs" partition
|
|
|
|
'NVS partition "nvs" is encrypted.'
|
2019-08-23 04:37:55 +00:00
|
|
|
]
|
|
|
|
for line in lines:
|
|
|
|
dut.expect(line, timeout=2)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test_examples_security_flash_encryption()
|