test_vboot.py 6.5 KB

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