pytest_secure_boot.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. # SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: Unlicense OR CC0-1.0
  3. from __future__ import print_function
  4. import os
  5. import struct
  6. import zlib
  7. import pytest
  8. from pytest_embedded import Dut
  9. # To prepare a runner for these tests,
  10. # 1. Connect an FPGA with C3 image
  11. # 2. Use a COM port for programming and export it as ESPPORT
  12. # e.g export ESPPORT=/dev/ttyUSB0
  13. # 3. Use another COM port for resetting efuses and connect its DTR pin to efuse reset pin on the FPGA board.
  14. # Export it as EFUSEPORT
  15. # e.g export EFUSEPORT=/dev/ttyUSB1
  16. # 4. Run these tests
  17. def corrupt_signature(signed_bootloader, seed=0, corrupt_sig=True, corrupt_crc=False, corrupt_single_block=None):
  18. # type: (bytes, int, bool, bool, int) -> bytes
  19. image = signed_bootloader[:-4096]
  20. signature = signed_bootloader[-4096:]
  21. sig_blocks = (signature[0:1216], signature[1216:2432], signature[2432:3648])
  22. new_blocks = tuple(corrupt_sig_block(s, seed, corrupt_sig, corrupt_crc) for s in sig_blocks)
  23. # if corrupt_single_block is None, corrupt all blocks
  24. # otherwise, only corrupt the one with that index set
  25. corr_sig_blocks = tuple(new_blocks[n] if corrupt_single_block in [None, n] else sig_blocks[n] for n in range(3))
  26. return image + b''.join(corr_sig_blocks) + signature[3648:]
  27. def corrupt_sig_block(sig_block, seed=0, corrupt_sig=True, corrupt_crc=False):
  28. # type: (bytes, int, bool, bool) -> bytes
  29. assert len(sig_block) == 1216
  30. magic = sig_block[0]
  31. assert magic in [0xe7, 0xff]
  32. if magic != 0xe7:
  33. return sig_block # not valid
  34. data = sig_block[:812]
  35. new_sig = sig = sig_block[812:1196]
  36. crc = sig_block[1196:1200]
  37. padding = sig_block[1200:1216]
  38. if corrupt_sig:
  39. corrupt_idx = seed % len(sig)
  40. corrupt_delta = zlib.crc32(bytes(seed)) & 0xFF
  41. if corrupt_delta == 0:
  42. corrupt_delta = 1
  43. new_byte = sig[corrupt_idx] ^ corrupt_delta
  44. new_sig = sig[0:corrupt_idx] + bytes([new_byte]) + sig[corrupt_idx + 1:]
  45. assert new_sig != sig
  46. if not corrupt_crc:
  47. crc = struct.pack('<I', zlib.crc32(data + new_sig) & 0xffffffff)
  48. else:
  49. crc = struct.pack('<I', zlib.crc32(crc))
  50. result = data + new_sig + crc + padding
  51. assert len(result) == len(sig_block)
  52. return result
  53. def dut_start_secure_app(dut: Dut) -> None:
  54. dut.serial.erase_flash()
  55. dut.serial.reset_efuses()
  56. dut.burn_wafer_version()
  57. bootloader_path = os.path.join(dut.app.binary_path, 'bootloader/bootloader.bin')
  58. dut.serial.bootloader_flash(bootloader_path)
  59. dut.serial.flash()
  60. # Test secure boot flow.
  61. # Correctly signed bootloader + correctly signed app should work
  62. @pytest.mark.esp32c3
  63. @pytest.mark.esp32s3
  64. def test_examples_security_secure_boot(dut: Dut) -> None:
  65. dut_start_secure_app(dut)
  66. dut.expect('Secure Boot is enabled', timeout=10)
  67. dut.serial.reset_efuses()
  68. # Test efuse key index and key block combination.
  69. # Any key index can be written to any key block and should work
  70. @pytest.mark.esp32c3
  71. @pytest.mark.esp32s3
  72. def test_examples_security_secure_boot_key_combo(dut: Dut) -> None:
  73. dut_start_secure_app(dut)
  74. dut.expect('Secure Boot is enabled', timeout=10)
  75. for index in range(3):
  76. print(index)
  77. for block in range(6):
  78. dut.serial.reset_efuses()
  79. dut.burn_wafer_version()
  80. dut.secure_boot_burn_en_bit()
  81. dut.secure_boot_burn_digest('test_rsa_3072_key.pem', index, block)
  82. dut.expect('Secure Boot is enabled', timeout=10)
  83. # Test secure boot key revoke.
  84. # If a key is revoked, bootloader signed with that key should fail verification
  85. @pytest.mark.esp32c3
  86. @pytest.mark.esp32s3
  87. def test_examples_security_secure_boot_key_revoke(dut: Dut) -> None:
  88. dut_start_secure_app(dut)
  89. dut.expect('Secure Boot is enabled', timeout=10)
  90. for index in range(3):
  91. dut.serial.reset_efuses()
  92. dut.burn_wafer_version()
  93. dut.secure_boot_burn_en_bit()
  94. dut.serial.burn_efuse('SECURE_BOOT_KEY_REVOKE%d' % index, 1)
  95. dut.secure_boot_burn_digest('test_rsa_3072_key.pem', index, 0)
  96. dut.expect('secure boot verification failed', timeout=5)
  97. # Test bootloader signature corruption.
  98. # Corrupt one byte at a time of bootloader signature and test that the verification fails
  99. @pytest.mark.esp32c3
  100. @pytest.mark.esp32s3
  101. def test_examples_security_secure_boot_corrupt_bl_sig(dut: Dut) -> None:
  102. dut_start_secure_app(dut)
  103. dut.expect('Secure Boot is enabled', timeout=10)
  104. bootloader_bin = os.path.join(dut.app.binary_path, 'bootloader/bootloader.bin')
  105. with open(bootloader_bin, 'rb') as f:
  106. signed_bl = f.read()
  107. seeds = range(0, 384)
  108. max_seed = max(seeds)
  109. for seed in seeds:
  110. print('Case %d / %d' % (seed, max_seed))
  111. corrupt_bl = corrupt_signature(signed_bl, seed=seed)
  112. with open('corrupt_bl.bin', 'wb') as corrupt_file:
  113. corrupt_file.write(corrupt_bl)
  114. dut.serial.reset_efuses()
  115. dut.burn_wafer_version()
  116. dut.serial.bootloader_flash('corrupt_bl.bin')
  117. dut.secure_boot_burn_en_bit()
  118. dut.secure_boot_burn_digest('test_rsa_3072_key.pem', 0, 0)
  119. # Though the order of flashing and burning efuse would not effect the test,
  120. # if we flash bootlader before burning en bit, even with no_stub = True
  121. # it still calls run_stub() and throws an error as it fails to start stub.
  122. dut.expect('Signature Check Failed', timeout=10)
  123. # Test app signature corruption.
  124. # Corrupt app signature, one byte at a time, and test that the verification fails
  125. @pytest.mark.esp32c3
  126. @pytest.mark.esp32s3
  127. def test_examples_security_secure_boot_corrupt_app_sig(dut: Dut) -> None:
  128. dut_start_secure_app(dut)
  129. dut.expect('Secure Boot is enabled', timeout=10)
  130. app_bin = os.path.join(dut.app.binary_path, 'secure_boot.bin')
  131. with open(app_bin, 'rb') as f:
  132. signed_app = f.read()
  133. seeds = range(0, 384)
  134. max_seed = max(seeds)
  135. for seed in seeds:
  136. print('Case %d / %d' % (seed, max_seed))
  137. corrupt_app = corrupt_signature(signed_app, seed=seed)
  138. with open('corrupt_app.bin', 'wb') as corrupt_file:
  139. corrupt_file.write(corrupt_app)
  140. dut.serial.reset_efuses()
  141. dut.burn_wafer_version()
  142. dut.serial.app_flash('corrupt_app.bin')
  143. dut.secure_boot_burn_en_bit()
  144. dut.secure_boot_burn_digest('test_rsa_3072_key.pem', 0, 0)
  145. dut.expect('Signature Check Failed', timeout=10)
  146. dut.expect('image valid, signature bad', timeout=2)
  147. print('Testing invalid CRC...')
  148. # Valid signature but invalid CRC
  149. dut.serial.reset_efuses()
  150. dut.burn_wafer_version()
  151. corrupt_app = corrupt_signature(signed_app, corrupt_sig=False, corrupt_crc=True)
  152. with open('corrupt_app.bin', 'wb') as corrupt_file:
  153. corrupt_file.write(corrupt_app)
  154. dut.serial.app_flash('corrupt_app.bin')
  155. dut.secure_boot_burn_en_bit()
  156. dut.secure_boot_burn_digest('test_rsa_3072_key.pem', 0, 0)
  157. dut.expect('Sig block 0 invalid: Stored CRC ends', timeout=2)
  158. dut.expect('Secure boot signature verification failed', timeout=2)
  159. dut.expect('No bootable app partitions in the partition table', timeout=2)