test_vboot.py 6.8 KB

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