test_vboot.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. # Copyright (c) 2016, Google Inc.
  2. #
  3. # SPDX-License-Identifier: GPL-2.0+
  4. #
  5. # U-Boot Verified Boot Test
  6. """
  7. This tests verified boot in the following ways:
  8. For image verification:
  9. - Create FIT (unsigned) with mkimage
  10. - Check that verification shows that no keys are verified
  11. - Sign image
  12. - Check that verification shows that a key is now verified
  13. For configuration verification:
  14. - Corrupt signature and check for failure
  15. - Create FIT (with unsigned configuration) with mkimage
  16. - Check that image verification works
  17. - Sign the FIT and mark the key as 'required' for verification
  18. - Check that image verification works
  19. - Corrupt the signature
  20. - Check that image verification no-longer works
  21. Tests run with both SHA1 and SHA256 hashing.
  22. """
  23. import pytest
  24. import sys
  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. ['sb 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 test_with_algo(sha_algo):
  90. """Test verified boot with the given hash algorithm.
  91. This is the main part of the test code. The same procedure is followed
  92. for both hashing algorithms.
  93. Args:
  94. sha_algo: Either 'sha1' or 'sha256', to select the algorithm to
  95. use.
  96. """
  97. # Compile our device tree files for kernel and U-Boot. These are
  98. # regenerated here since mkimage will modify them (by adding a
  99. # public key) below.
  100. dtc('sandbox-kernel.dts')
  101. dtc('sandbox-u-boot.dts')
  102. # Build the FIT, but don't sign anything yet
  103. cons.log.action('%s: Test FIT with signed images' % sha_algo)
  104. make_fit('sign-images-%s.its' % sha_algo)
  105. run_bootm(sha_algo, 'unsigned images', 'dev-', True)
  106. # Sign images with our dev keys
  107. sign_fit(sha_algo)
  108. run_bootm(sha_algo, 'signed images', 'dev+', True)
  109. # Create a fresh .dtb without the public keys
  110. dtc('sandbox-u-boot.dts')
  111. cons.log.action('%s: Test FIT with signed configuration' % sha_algo)
  112. make_fit('sign-configs-%s.its' % sha_algo)
  113. run_bootm(sha_algo, 'unsigned config', '%s+ OK' % sha_algo, True)
  114. # Sign images with our dev keys
  115. sign_fit(sha_algo)
  116. run_bootm(sha_algo, 'signed config', 'dev+', True)
  117. cons.log.action('%s: Check signed config on the host' % sha_algo)
  118. util.run_and_log(cons, [fit_check_sign, '-f', fit, '-k', tmpdir,
  119. '-k', dtb])
  120. # Increment the first byte of the signature, which should cause failure
  121. sig = util.run_and_log(cons, 'fdtget -t bx %s %s value' %
  122. (fit, sig_node))
  123. byte_list = sig.split()
  124. byte = int(byte_list[0], 16)
  125. byte_list[0] = '%x' % (byte + 1)
  126. sig = ' '.join(byte_list)
  127. util.run_and_log(cons, 'fdtput -t bx %s %s value %s' %
  128. (fit, sig_node, sig))
  129. run_bootm(sha_algo, 'Signed config with bad hash', 'Bad Data Hash', False)
  130. cons.log.action('%s: Check bad config on the host' % sha_algo)
  131. util.run_and_log_expect_exception(cons, [fit_check_sign, '-f', fit,
  132. '-k', dtb], 1, 'Failed to verify required signature')
  133. cons = u_boot_console
  134. tmpdir = cons.config.result_dir + '/'
  135. tmp = tmpdir + 'vboot.tmp'
  136. datadir = cons.config.source_dir + '/test/py/tests/vboot/'
  137. fit = '%stest.fit' % tmpdir
  138. mkimage = cons.config.build_dir + '/tools/mkimage'
  139. fit_check_sign = cons.config.build_dir + '/tools/fit_check_sign'
  140. dtc_args = '-I dts -O dtb -i %s' % tmpdir
  141. dtb = '%ssandbox-u-boot.dtb' % tmpdir
  142. sig_node = '/configurations/conf@1/signature@1'
  143. # Create an RSA key pair
  144. public_exponent = 65537
  145. util.run_and_log(cons, 'openssl genpkey -algorithm RSA -out %sdev.key '
  146. '-pkeyopt rsa_keygen_bits:2048 '
  147. '-pkeyopt rsa_keygen_pubexp:%d '
  148. '2>/dev/null' % (tmpdir, public_exponent))
  149. # Create a certificate containing the public key
  150. util.run_and_log(cons, 'openssl req -batch -new -x509 -key %sdev.key -out '
  151. '%sdev.crt' % (tmpdir, tmpdir))
  152. # Create a number kernel image with zeroes
  153. with open('%stest-kernel.bin' % tmpdir, 'w') as fd:
  154. fd.write(5000 * chr(0))
  155. try:
  156. # We need to use our own device tree file. Remember to restore it
  157. # afterwards.
  158. old_dtb = cons.config.dtb
  159. cons.config.dtb = dtb
  160. test_with_algo('sha1')
  161. test_with_algo('sha256')
  162. finally:
  163. # Go back to the original U-Boot with the correct dtb.
  164. cons.config.dtb = old_dtb
  165. cons.restart_uboot()