test_vboot.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 u_boot_utils as util
  25. @pytest.mark.boardspec('sandbox')
  26. @pytest.mark.buildconfigspec('fit_signature')
  27. @pytest.mark.requiredtool('dtc')
  28. @pytest.mark.requiredtool('fdtget')
  29. @pytest.mark.requiredtool('fdtput')
  30. @pytest.mark.requiredtool('openssl')
  31. def test_vboot(u_boot_console):
  32. """Test verified boot signing with mkimage and verification with 'bootm'.
  33. This works using sandbox only as it needs to update the device tree used
  34. by U-Boot to hold public keys from the signing process.
  35. The SHA1 and SHA256 tests are combined into a single test since the
  36. key-generation process is quite slow and we want to avoid doing it twice.
  37. """
  38. def dtc(dts):
  39. """Run the device tree compiler to compile a .dts file
  40. The output file will be the same as the input file but with a .dtb
  41. extension.
  42. Args:
  43. dts: Device tree file to compile.
  44. """
  45. dtb = dts.replace('.dts', '.dtb')
  46. util.run_and_log(cons, 'dtc %s %s%s -O dtb '
  47. '-o %s%s' % (dtc_args, datadir, dts, tmpdir, dtb))
  48. def run_bootm(sha_algo, test_type, expect_string, boots):
  49. """Run a 'bootm' command U-Boot.
  50. This always starts a fresh U-Boot instance since the device tree may
  51. contain a new public key.
  52. Args:
  53. test_type: A string identifying the test type.
  54. expect_string: A string which is expected in the output.
  55. sha_algo: Either 'sha1' or 'sha256', to select the algorithm to
  56. use.
  57. boots: A boolean that is True if Linux should boot and False if
  58. we are expected to not boot
  59. """
  60. cons.restart_uboot()
  61. with cons.log.section('Verified boot %s %s' % (sha_algo, test_type)):
  62. output = cons.run_command_list(
  63. ['sb load hostfs - 100 %stest.fit' % tmpdir,
  64. 'fdt addr 100',
  65. 'bootm 100'])
  66. assert(expect_string in ''.join(output))
  67. if boots:
  68. assert('sandbox: continuing, as we cannot run' in ''.join(output))
  69. def make_fit(its):
  70. """Make a new FIT from the .its source file.
  71. This runs 'mkimage -f' to create a new FIT.
  72. Args:
  73. its: Filename containing .its source.
  74. """
  75. util.run_and_log(cons, [mkimage, '-D', dtc_args, '-f',
  76. '%s%s' % (datadir, its), fit])
  77. def sign_fit(sha_algo):
  78. """Sign the FIT
  79. Signs the FIT and writes the signature into it. It also writes the
  80. public key into the dtb.
  81. Args:
  82. sha_algo: Either 'sha1' or 'sha256', to select the algorithm to
  83. use.
  84. """
  85. cons.log.action('%s: Sign images' % sha_algo)
  86. util.run_and_log(cons, [mkimage, '-F', '-k', tmpdir, '-K', dtb,
  87. '-r', fit])
  88. def test_with_algo(sha_algo):
  89. """Test verified boot with the given hash algorithm.
  90. This is the main part of the test code. The same procedure is followed
  91. for both hashing algorithms.
  92. Args:
  93. sha_algo: Either 'sha1' or 'sha256', to select the algorithm to
  94. use.
  95. """
  96. # Compile our device tree files for kernel and U-Boot. These are
  97. # regenerated here since mkimage will modify them (by adding a
  98. # public key) below.
  99. dtc('sandbox-kernel.dts')
  100. dtc('sandbox-u-boot.dts')
  101. # Build the FIT, but don't sign anything yet
  102. cons.log.action('%s: Test FIT with signed images' % sha_algo)
  103. make_fit('sign-images-%s.its' % sha_algo)
  104. run_bootm(sha_algo, 'unsigned images', 'dev-', True)
  105. # Sign images with our dev keys
  106. sign_fit(sha_algo)
  107. run_bootm(sha_algo, 'signed images', 'dev+', True)
  108. # Create a fresh .dtb without the public keys
  109. dtc('sandbox-u-boot.dts')
  110. cons.log.action('%s: Test FIT with signed configuration' % sha_algo)
  111. make_fit('sign-configs-%s.its' % sha_algo)
  112. run_bootm(sha_algo, 'unsigned config', '%s+ OK' % sha_algo, True)
  113. # Sign images with our dev keys
  114. sign_fit(sha_algo)
  115. run_bootm(sha_algo, 'signed config', 'dev+', True)
  116. cons.log.action('%s: Check signed config on the host' % sha_algo)
  117. util.run_and_log(cons, [fit_check_sign, '-f', fit, '-k', tmpdir,
  118. '-k', dtb])
  119. # Increment the first byte of the signature, which should cause failure
  120. sig = util.run_and_log(cons, 'fdtget -t bx %s %s value' %
  121. (fit, sig_node))
  122. byte_list = sig.split()
  123. byte = int(byte_list[0], 16)
  124. byte_list[0] = '%x' % (byte + 1)
  125. sig = ' '.join(byte_list)
  126. util.run_and_log(cons, 'fdtput -t bx %s %s value %s' %
  127. (fit, sig_node, sig))
  128. run_bootm(sha_algo, 'Signed config with bad hash', 'Bad Data Hash', False)
  129. cons.log.action('%s: Check bad config on the host' % sha_algo)
  130. util.run_and_log_expect_exception(cons, [fit_check_sign, '-f', fit,
  131. '-k', dtb], 1, 'Failed to verify required signature')
  132. cons = u_boot_console
  133. tmpdir = cons.config.result_dir + '/'
  134. tmp = tmpdir + 'vboot.tmp'
  135. datadir = cons.config.source_dir + '/test/py/tests/vboot/'
  136. fit = '%stest.fit' % tmpdir
  137. mkimage = cons.config.build_dir + '/tools/mkimage'
  138. fit_check_sign = cons.config.build_dir + '/tools/fit_check_sign'
  139. dtc_args = '-I dts -O dtb -i %s' % tmpdir
  140. dtb = '%ssandbox-u-boot.dtb' % tmpdir
  141. sig_node = '/configurations/conf@1/signature@1'
  142. # Create an RSA key pair
  143. public_exponent = 65537
  144. util.run_and_log(cons, 'openssl genpkey -algorithm RSA -out %sdev.key '
  145. '-pkeyopt rsa_keygen_bits:2048 '
  146. '-pkeyopt rsa_keygen_pubexp:%d '
  147. '2>/dev/null' % (tmpdir, public_exponent))
  148. # Create a certificate containing the public key
  149. util.run_and_log(cons, 'openssl req -batch -new -x509 -key %sdev.key -out '
  150. '%sdev.crt' % (tmpdir, tmpdir))
  151. # Create a number kernel image with zeroes
  152. with open('%stest-kernel.bin' % tmpdir, 'w') as fd:
  153. fd.write(5000 * chr(0))
  154. try:
  155. # We need to use our own device tree file. Remember to restore it
  156. # afterwards.
  157. old_dtb = cons.config.dtb
  158. cons.config.dtb = dtb
  159. test_with_algo('sha1')
  160. test_with_algo('sha256')
  161. finally:
  162. # Go back to the original U-Boot with the correct dtb.
  163. cons.config.dtb = old_dtb
  164. cons.restart_uboot()