example_test.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. from __future__ import print_function
  2. import binascii
  3. import os
  4. import sys
  5. from collections import namedtuple
  6. from io import BytesIO
  7. import ttfw_idf
  8. try:
  9. import espsecure
  10. except ImportError:
  11. idf_path = os.getenv('IDF_PATH')
  12. if not idf_path or not os.path.exists(idf_path):
  13. raise
  14. sys.path.insert(0, os.path.join(idf_path, 'components', 'esptool_py', 'esptool'))
  15. import espsecure
  16. # To prepare a test runner for this example:
  17. # 1. Generate zero flash encryption key:
  18. # dd if=/dev/zero of=key.bin bs=1 count=32
  19. # 2.Burn Efuses:
  20. # espefuse.py --do-not-confirm -p $ESPPORT burn_efuse FLASH_CRYPT_CONFIG 0xf
  21. # espefuse.py --do-not-confirm -p $ESPPORT burn_efuse FLASH_CRYPT_CNT 0x1
  22. # espefuse.py --do-not-confirm -p $ESPPORT burn_key flash_encryption key.bin
  23. @ttfw_idf.idf_example_test(env_tag='Example_Flash_Encryption', target=['esp32', 'esp32c3'])
  24. def test_examples_security_flash_encryption(env, extra_data):
  25. dut = env.get_dut('flash_encryption', 'examples/security/flash_encryption')
  26. dut.erase_flash()
  27. # start test
  28. dut.start_app()
  29. # calculate the expected ciphertext
  30. flash_addr = dut.app.partition_table['storage']['offset']
  31. plain_hex_str = '00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f'
  32. plain_data = binascii.unhexlify(plain_hex_str.replace(' ', ''))
  33. # espsecure uses the cryptography package for encrypting
  34. # with aes-xts, but does not allow for a symmetric key
  35. # so the key for later chips are not all zeros
  36. if dut.TARGET == 'esp32':
  37. key_bytes = b'\x00' * 32
  38. aes_xts = False
  39. else:
  40. key_bytes = b'\xff' + b'\x00' * 31
  41. aes_xts = True
  42. # Emulate espsecure encrypt_flash_data command
  43. EncryptFlashDataArgs = namedtuple('EncryptFlashDataArgs', ['output', 'plaintext_file', 'address', 'keyfile', 'flash_crypt_conf', 'aes_xts'])
  44. args = EncryptFlashDataArgs(BytesIO(), BytesIO(plain_data), flash_addr, BytesIO(key_bytes), 0xF, aes_xts)
  45. espsecure.encrypt_flash_data(args)
  46. expected_ciphertext = args.output.getvalue()
  47. hex_ciphertext = binascii.hexlify(expected_ciphertext).decode('ascii')
  48. expected_str = (' '.join(hex_ciphertext[i:i + 2] for i in range(0, 16, 2)) + ' ' +
  49. ' '.join(hex_ciphertext[i:i + 2] for i in range(16, 32, 2)))
  50. lines = [
  51. 'FLASH_CRYPT_CNT eFuse value is 1',
  52. 'Flash encryption feature is enabled in DEVELOPMENT mode',
  53. 'with esp_partition_write',
  54. plain_hex_str,
  55. 'with esp_partition_read',
  56. plain_hex_str,
  57. 'with spi_flash_read',
  58. expected_str,
  59. # The status of NVS encryption for the "nvs" partition
  60. 'NVS partition "nvs" is encrypted.'
  61. ]
  62. for line in lines:
  63. dut.expect(line, timeout=2)
  64. if __name__ == '__main__':
  65. test_examples_security_flash_encryption()