2024-02-22 05:05:47 -05:00
|
|
|
# SPDX-FileCopyrightText: 2018-2024 Espressif Systems (Shanghai) CO LTD
|
2022-04-14 00:54:04 -04:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2020-09-16 18:06:54 -04:00
|
|
|
import binascii
|
2021-01-25 21:49:01 -05:00
|
|
|
from collections import namedtuple
|
|
|
|
from io import BytesIO
|
2020-09-16 18:06:54 -04:00
|
|
|
|
2022-04-28 03:14:47 -04:00
|
|
|
import espsecure
|
2022-04-14 00:54:04 -04:00
|
|
|
import pytest
|
|
|
|
from pytest_embedded import Dut
|
2021-01-25 21:49:01 -05:00
|
|
|
|
2019-08-23 00:37:55 -04: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
|
2024-02-22 05:05:47 -05:00
|
|
|
def _test_flash_encryption(dut: Dut) -> None:
|
2022-04-28 03:14:47 -04:00
|
|
|
# Erase the nvs_key partition
|
|
|
|
dut.serial.erase_partition('nvs_key')
|
2020-09-16 18:06:54 -04:00
|
|
|
# calculate the expected ciphertext
|
2021-01-25 21:49:01 -05:00
|
|
|
flash_addr = dut.app.partition_table['storage']['offset']
|
2020-09-16 18:06:54 -04:00
|
|
|
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(' ', ''))
|
|
|
|
|
2021-03-17 04:11:03 -04:00
|
|
|
# espsecure uses the cryptography package for encrypting
|
|
|
|
# with aes-xts, but does not allow for a symmetric key
|
|
|
|
# so the key for later chips are not all zeros
|
2022-04-14 00:54:04 -04:00
|
|
|
if dut.target == 'esp32':
|
2021-03-17 04:11:03 -04:00
|
|
|
key_bytes = b'\x00' * 32
|
|
|
|
aes_xts = False
|
|
|
|
else:
|
|
|
|
key_bytes = b'\xff' + b'\x00' * 31
|
|
|
|
aes_xts = True
|
|
|
|
|
2020-09-16 18:06:54 -04:00
|
|
|
# Emulate espsecure encrypt_flash_data command
|
2021-01-15 03:04:59 -05:00
|
|
|
EncryptFlashDataArgs = namedtuple('EncryptFlashDataArgs', ['output', 'plaintext_file', 'address', 'keyfile', 'flash_crypt_conf', 'aes_xts'])
|
2021-03-17 04:11:03 -04:00
|
|
|
args = EncryptFlashDataArgs(BytesIO(), BytesIO(plain_data), flash_addr, BytesIO(key_bytes), 0xF, aes_xts)
|
2020-09-16 18:06:54 -04: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 00:37:55 -04:00
|
|
|
lines = [
|
|
|
|
'FLASH_CRYPT_CNT eFuse value is 1',
|
|
|
|
'Flash encryption feature is enabled in DEVELOPMENT mode',
|
|
|
|
'with esp_partition_write',
|
2020-09-16 18:06:54 -04:00
|
|
|
plain_hex_str,
|
2019-08-23 00:37:55 -04:00
|
|
|
'with esp_partition_read',
|
2020-09-16 18:06:54 -04:00
|
|
|
plain_hex_str,
|
2022-06-27 03:24:07 -04:00
|
|
|
'with esp_flash_read',
|
2020-12-14 22:01:39 -05:00
|
|
|
expected_str,
|
|
|
|
# The status of NVS encryption for the "nvs" partition
|
2022-06-02 08:44:21 -04:00
|
|
|
'NVS partition "nvs" is encrypted.',
|
|
|
|
# The status of NVS encryption for the "custom_nvs" partition
|
|
|
|
'NVS partition "custom_nvs" is encrypted.'
|
2019-08-23 00:37:55 -04:00
|
|
|
]
|
|
|
|
for line in lines:
|
2022-06-27 09:27:58 -04:00
|
|
|
dut.expect(line, timeout=20)
|
2024-02-22 05:05:47 -05:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.esp32
|
|
|
|
@pytest.mark.esp32c3
|
|
|
|
@pytest.mark.flash_encryption
|
|
|
|
def test_examples_security_flash_encryption(dut: Dut) -> None:
|
|
|
|
_test_flash_encryption(dut)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.esp32c3
|
|
|
|
@pytest.mark.flash_encryption
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'config',
|
|
|
|
[
|
|
|
|
'rom_impl',
|
|
|
|
],
|
|
|
|
indirect=True,
|
|
|
|
)
|
|
|
|
def test_examples_security_flash_encryption_rom_impl(dut: Dut) -> None:
|
|
|
|
_test_flash_encryption(dut)
|