test_vboot.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright (c) 2016, Google Inc.
  3. #
  4. # U-Boot Verified Boot Test
  5. """
  6. This tests verified boot in the following ways:
  7. For image verification:
  8. - Create FIT (unsigned) with mkimage
  9. - Check that verification shows that no keys are verified
  10. - Sign image
  11. - Check that verification shows that a key is now verified
  12. For configuration verification:
  13. - Corrupt signature and check for failure
  14. - Create FIT (with unsigned configuration) with mkimage
  15. - Check that image verification works
  16. - Sign the FIT and mark the key as 'required' for verification
  17. - Check that image verification works
  18. - Corrupt the signature
  19. - Check that image verification no-longer works
  20. Tests run with both SHA1 and SHA256 hashing.
  21. """
  22. import pytest
  23. import sys
  24. import struct
  25. import u_boot_utils as util
  26. @pytest.mark.boardspec('sandbox')
  27. @pytest.mark.buildconfigspec('fit_signature')
  28. @pytest.mark.requiredtool('dtc')
  29. @pytest.mark.requiredtool('fdtget')
  30. @pytest.mark.requiredtool('fdtput')
  31. @pytest.mark.requiredtool('openssl')
  32. def test_vboot(u_boot_console):
  33. """Test verified boot signing with mkimage and verification with 'bootm'.
  34. This works using sandbox only as it needs to update the device tree used
  35. by U-Boot to hold public keys from the signing process.
  36. The SHA1 and SHA256 tests are combined into a single test since the
  37. key-generation process is quite slow and we want to avoid doing it twice.
  38. """
  39. def dtc(dts):
  40. """Run the device tree compiler to compile a .dts file
  41. The output file will be the same as the input file but with a .dtb
  42. extension.
  43. Args:
  44. dts: Device tree file to compile.
  45. """
  46. dtb = dts.replace('.dts', '.dtb')
  47. util.run_and_log(cons, 'dtc %s %s%s -O dtb '
  48. '-o %s%s' % (dtc_args, datadir, dts, tmpdir, dtb))
  49. def run_bootm(sha_algo, test_type, expect_string, boots):
  50. """Run a 'bootm' command U-Boot.
  51. This always starts a fresh U-Boot instance since the device tree may
  52. contain a new public key.
  53. Args:
  54. test_type: A string identifying the test type.
  55. expect_string: A string which is expected in the output.
  56. sha_algo: Either 'sha1' or 'sha256', to select the algorithm to
  57. use.
  58. boots: A boolean that is True if Linux should boot and False if
  59. we are expected to not boot
  60. """
  61. cons.restart_uboot()
  62. with cons.log.section('Verified boot %s %s' % (sha_algo, test_type)):
  63. output = cons.run_command_list(
  64. ['host load hostfs - 100 %stest.fit' % tmpdir,
  65. 'fdt addr 100',
  66. 'bootm 100'])
  67. assert(expect_string in ''.join(output))
  68. if boots:
  69. assert('sandbox: continuing, as we cannot run' in ''.join(output))
  70. def make_fit(its):
  71. """Make a new FIT from the .its source file.
  72. This runs 'mkimage -f' to create a new FIT.
  73. Args:
  74. its: Filename containing .its source.
  75. """
  76. util.run_and_log(cons, [mkimage, '-D', dtc_args, '-f',
  77. '%s%s' % (datadir, its), fit])
  78. def sign_fit(sha_algo):
  79. """Sign the FIT
  80. Signs the FIT and writes the signature into it. It also writes the
  81. public key into the dtb.
  82. Args:
  83. sha_algo: Either 'sha1' or 'sha256', to select the algorithm to
  84. use.
  85. """
  86. cons.log.action('%s: Sign images' % sha_algo)
  87. util.run_and_log(cons, [mkimage, '-F', '-k', tmpdir, '-K', dtb,
  88. '-r', fit])
  89. def replace_fit_totalsize(size):
  90. """Replace FIT header's totalsize with something greater.
  91. The totalsize must be less than or equal to FIT_SIGNATURE_MAX_SIZE.
  92. If the size is greater, the signature verification should return false.
  93. Args:
  94. size: The new totalsize of the header
  95. Returns:
  96. prev_size: The previous totalsize read from the header
  97. """
  98. total_size = 0
  99. with open(fit, 'r+b') as handle:
  100. handle.seek(4)
  101. total_size = handle.read(4)
  102. handle.seek(4)
  103. handle.write(struct.pack(">I", size))
  104. return struct.unpack(">I", total_size)[0]
  105. def test_with_algo(sha_algo):
  106. """Test verified boot with the given hash algorithm.
  107. This is the main part of the test code. The same procedure is followed
  108. for both hashing algorithms.
  109. Args:
  110. sha_algo: Either 'sha1' or 'sha256', to select the algorithm to
  111. use.
  112. """
  113. # Compile our device tree files for kernel and U-Boot. These are
  114. # regenerated here since mkimage will modify them (by adding a
  115. # public key) below.
  116. dtc('sandbox-kernel.dts')
  117. dtc('sandbox-u-boot.dts')
  118. # Build the FIT, but don't sign anything yet
  119. cons.log.action('%s: Test FIT with signed images' % sha_algo)
  120. make_fit('sign-images-%s.its' % sha_algo)
  121. run_bootm(sha_algo, 'unsigned images', 'dev-', True)
  122. # Sign images with our dev keys
  123. sign_fit(sha_algo)
  124. run_bootm(sha_algo, 'signed images', 'dev+', True)
  125. # Create a fresh .dtb without the public keys
  126. dtc('sandbox-u-boot.dts')
  127. cons.log.action('%s: Test FIT with signed configuration' % sha_algo)
  128. make_fit('sign-configs-%s.its' % sha_algo)
  129. run_bootm(sha_algo, 'unsigned config', '%s+ OK' % sha_algo, True)
  130. # Sign images with our dev keys
  131. sign_fit(sha_algo)
  132. run_bootm(sha_algo, 'signed config', 'dev+', True)
  133. cons.log.action('%s: Check signed config on the host' % sha_algo)
  134. util.run_and_log(cons, [fit_check_sign, '-f', fit, '-k', tmpdir,
  135. '-k', dtb])
  136. # Replace header bytes
  137. bcfg = u_boot_console.config.buildconfig
  138. max_size = int(bcfg.get('config_fit_signature_max_size', 0x10000000), 0)
  139. existing_size = replace_fit_totalsize(max_size + 1)
  140. run_bootm(sha_algo, 'Signed config with bad hash', 'Bad Data Hash', False)
  141. cons.log.action('%s: Check overflowed FIT header totalsize' % sha_algo)
  142. # Replace with existing header bytes
  143. replace_fit_totalsize(existing_size)
  144. run_bootm(sha_algo, 'signed config', 'dev+', True)
  145. cons.log.action('%s: Check default FIT header totalsize' % sha_algo)
  146. # Increment the first byte of the signature, which should cause failure
  147. sig = util.run_and_log(cons, 'fdtget -t bx %s %s value' %
  148. (fit, sig_node))
  149. byte_list = sig.split()
  150. byte = int(byte_list[0], 16)
  151. byte_list[0] = '%x' % (byte + 1)
  152. sig = ' '.join(byte_list)
  153. util.run_and_log(cons, 'fdtput -t bx %s %s value %s' %
  154. (fit, sig_node, sig))
  155. run_bootm(sha_algo, 'Signed config with bad hash', 'Bad Data Hash', False)
  156. cons.log.action('%s: Check bad config on the host' % sha_algo)
  157. util.run_and_log_expect_exception(cons, [fit_check_sign, '-f', fit,
  158. '-k', dtb], 1, 'Failed to verify required signature')
  159. cons = u_boot_console
  160. tmpdir = cons.config.result_dir + '/'
  161. tmp = tmpdir + 'vboot.tmp'
  162. datadir = cons.config.source_dir + '/test/py/tests/vboot/'
  163. fit = '%stest.fit' % tmpdir
  164. mkimage = cons.config.build_dir + '/tools/mkimage'
  165. fit_check_sign = cons.config.build_dir + '/tools/fit_check_sign'
  166. dtc_args = '-I dts -O dtb -i %s' % tmpdir
  167. dtb = '%ssandbox-u-boot.dtb' % tmpdir
  168. sig_node = '/configurations/conf@1/signature@1'
  169. # Create an RSA key pair
  170. public_exponent = 65537
  171. util.run_and_log(cons, 'openssl genpkey -algorithm RSA -out %sdev.key '
  172. '-pkeyopt rsa_keygen_bits:2048 '
  173. '-pkeyopt rsa_keygen_pubexp:%d' %
  174. (tmpdir, public_exponent))
  175. # Create a certificate containing the public key
  176. util.run_and_log(cons, 'openssl req -batch -new -x509 -key %sdev.key -out '
  177. '%sdev.crt' % (tmpdir, tmpdir))
  178. # Create a number kernel image with zeroes
  179. with open('%stest-kernel.bin' % tmpdir, 'w') as fd:
  180. fd.write(5000 * chr(0))
  181. try:
  182. # We need to use our own device tree file. Remember to restore it
  183. # afterwards.
  184. old_dtb = cons.config.dtb
  185. cons.config.dtb = dtb
  186. test_with_algo('sha1')
  187. test_with_algo('sha256')
  188. finally:
  189. # Go back to the original U-Boot with the correct dtb.
  190. cons.config.dtb = old_dtb
  191. cons.restart_uboot()