test_vboot.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. else:
  71. assert('sandbox: continuing, as we cannot run' not in ''.join(output))
  72. def make_fit(its):
  73. """Make a new FIT from the .its source file.
  74. This runs 'mkimage -f' to create a new FIT.
  75. Args:
  76. its: Filename containing .its source.
  77. """
  78. util.run_and_log(cons, [mkimage, '-D', dtc_args, '-f',
  79. '%s%s' % (datadir, its), fit])
  80. def sign_fit(sha_algo):
  81. """Sign the FIT
  82. Signs the FIT and writes the signature into it. It also writes the
  83. public key into the dtb.
  84. Args:
  85. sha_algo: Either 'sha1' or 'sha256', to select the algorithm to
  86. use.
  87. """
  88. cons.log.action('%s: Sign images' % sha_algo)
  89. util.run_and_log(cons, [mkimage, '-F', '-k', tmpdir, '-K', dtb,
  90. '-r', fit])
  91. def sign_fit_norequire(sha_algo):
  92. """Sign the FIT
  93. Signs the FIT and writes the signature into it. It also writes the
  94. public key into the dtb.
  95. Args:
  96. sha_algo: Either 'sha1' or 'sha256', to select the algorithm to
  97. use.
  98. """
  99. cons.log.action('%s: Sign images' % sha_algo)
  100. util.run_and_log(cons, [mkimage, '-F', '-k', tmpdir, '-K', dtb,
  101. fit])
  102. def replace_fit_totalsize(size):
  103. """Replace FIT header's totalsize with something greater.
  104. The totalsize must be less than or equal to FIT_SIGNATURE_MAX_SIZE.
  105. If the size is greater, the signature verification should return false.
  106. Args:
  107. size: The new totalsize of the header
  108. Returns:
  109. prev_size: The previous totalsize read from the header
  110. """
  111. total_size = 0
  112. with open(fit, 'r+b') as handle:
  113. handle.seek(4)
  114. total_size = handle.read(4)
  115. handle.seek(4)
  116. handle.write(struct.pack(">I", size))
  117. return struct.unpack(">I", total_size)[0]
  118. def test_with_algo(sha_algo, padding):
  119. """Test verified boot with the given hash algorithm.
  120. This is the main part of the test code. The same procedure is followed
  121. for both hashing algorithms.
  122. Args:
  123. sha_algo: Either 'sha1' or 'sha256', to select the algorithm to
  124. use.
  125. """
  126. # Compile our device tree files for kernel and U-Boot. These are
  127. # regenerated here since mkimage will modify them (by adding a
  128. # public key) below.
  129. dtc('sandbox-kernel.dts')
  130. dtc('sandbox-u-boot.dts')
  131. # Build the FIT, but don't sign anything yet
  132. cons.log.action('%s: Test FIT with signed images' % sha_algo)
  133. make_fit('sign-images-%s%s.its' % (sha_algo , padding))
  134. run_bootm(sha_algo, 'unsigned images', 'dev-', True)
  135. # Sign images with our dev keys
  136. sign_fit(sha_algo)
  137. run_bootm(sha_algo, 'signed images', 'dev+', True)
  138. # Create a fresh .dtb without the public keys
  139. dtc('sandbox-u-boot.dts')
  140. cons.log.action('%s: Test FIT with signed configuration' % sha_algo)
  141. make_fit('sign-configs-%s%s.its' % (sha_algo , padding))
  142. run_bootm(sha_algo, 'unsigned config', '%s+ OK' % sha_algo, True)
  143. # Sign images with our dev keys
  144. sign_fit(sha_algo)
  145. run_bootm(sha_algo, 'signed config', 'dev+', True)
  146. cons.log.action('%s: Check signed config on the host' % sha_algo)
  147. util.run_and_log(cons, [fit_check_sign, '-f', fit, '-k', tmpdir,
  148. '-k', dtb])
  149. # Replace header bytes
  150. bcfg = u_boot_console.config.buildconfig
  151. max_size = int(bcfg.get('config_fit_signature_max_size', 0x10000000), 0)
  152. existing_size = replace_fit_totalsize(max_size + 1)
  153. run_bootm(sha_algo, 'Signed config with bad hash', 'Bad Data Hash', False)
  154. cons.log.action('%s: Check overflowed FIT header totalsize' % sha_algo)
  155. # Replace with existing header bytes
  156. replace_fit_totalsize(existing_size)
  157. run_bootm(sha_algo, 'signed config', 'dev+', True)
  158. cons.log.action('%s: Check default FIT header totalsize' % sha_algo)
  159. # Increment the first byte of the signature, which should cause failure
  160. sig = util.run_and_log(cons, 'fdtget -t bx %s %s value' %
  161. (fit, sig_node))
  162. byte_list = sig.split()
  163. byte = int(byte_list[0], 16)
  164. byte_list[0] = '%x' % (byte + 1)
  165. sig = ' '.join(byte_list)
  166. util.run_and_log(cons, 'fdtput -t bx %s %s value %s' %
  167. (fit, sig_node, sig))
  168. run_bootm(sha_algo, 'Signed config with bad hash', 'Bad Data Hash', False)
  169. cons.log.action('%s: Check bad config on the host' % sha_algo)
  170. util.run_and_log_expect_exception(cons, [fit_check_sign, '-f', fit,
  171. '-k', dtb], 1, 'Failed to verify required signature')
  172. def test_required_key(sha_algo, padding):
  173. """Test verified boot with the given hash algorithm.
  174. This function test if u-boot reject an image when a required
  175. key isn't used to sign a FIT.
  176. Args:
  177. sha_algo: Either 'sha1' or 'sha256', to select the algorithm to
  178. use.
  179. """
  180. # Compile our device tree files for kernel and U-Boot. These are
  181. # regenerated here since mkimage will modify them (by adding a
  182. # public key) below.
  183. dtc('sandbox-kernel.dts')
  184. dtc('sandbox-u-boot.dts')
  185. # Build the FIT with prod key (keys required)
  186. # Build the FIT with dev key (keys NOT required)
  187. # The dtb contain the key prod and dev and the key prod are set as required.
  188. # Then try to boot the FIT with dev key
  189. # This FIT should not be accepted by u-boot because the key prod is required
  190. cons.log.action('%s: Test FIT with configs images' % sha_algo)
  191. make_fit('sign-configs-%s%s-prod.its' % (sha_algo , padding))
  192. sign_fit(sha_algo)
  193. make_fit('sign-configs-%s%s.its' % (sha_algo , padding))
  194. sign_fit(sha_algo)
  195. run_bootm(sha_algo, 'signed configs', '', False)
  196. cons = u_boot_console
  197. tmpdir = cons.config.result_dir + '/'
  198. tmp = tmpdir + 'vboot.tmp'
  199. datadir = cons.config.source_dir + '/test/py/tests/vboot/'
  200. fit = '%stest.fit' % tmpdir
  201. mkimage = cons.config.build_dir + '/tools/mkimage'
  202. fit_check_sign = cons.config.build_dir + '/tools/fit_check_sign'
  203. dtc_args = '-I dts -O dtb -i %s' % tmpdir
  204. dtb = '%ssandbox-u-boot.dtb' % tmpdir
  205. sig_node = '/configurations/conf-1/signature'
  206. # Create an RSA key pair
  207. public_exponent = 65537
  208. util.run_and_log(cons, 'openssl genpkey -algorithm RSA -out %sdev.key '
  209. '-pkeyopt rsa_keygen_bits:2048 '
  210. '-pkeyopt rsa_keygen_pubexp:%d' %
  211. (tmpdir, public_exponent))
  212. # Create a certificate containing the public key
  213. util.run_and_log(cons, 'openssl req -batch -new -x509 -key %sdev.key -out '
  214. '%sdev.crt' % (tmpdir, tmpdir))
  215. # Create an RSA key pair (prod)
  216. public_exponent = 65537
  217. util.run_and_log(cons, 'openssl genpkey -algorithm RSA -out %sprod.key '
  218. '-pkeyopt rsa_keygen_bits:2048 '
  219. '-pkeyopt rsa_keygen_pubexp:%d' %
  220. (tmpdir, public_exponent))
  221. # Create a certificate containing the public key (prod)
  222. util.run_and_log(cons, 'openssl req -batch -new -x509 -key %sprod.key -out '
  223. '%sprod.crt' % (tmpdir, tmpdir))
  224. # Create a number kernel image with zeroes
  225. with open('%stest-kernel.bin' % tmpdir, 'w') as fd:
  226. fd.write(5000 * chr(0))
  227. try:
  228. # We need to use our own device tree file. Remember to restore it
  229. # afterwards.
  230. old_dtb = cons.config.dtb
  231. cons.config.dtb = dtb
  232. test_with_algo('sha1','')
  233. test_with_algo('sha1','-pss')
  234. test_with_algo('sha256','')
  235. test_with_algo('sha256','-pss')
  236. test_required_key('sha256','-pss')
  237. finally:
  238. # Go back to the original U-Boot with the correct dtb.
  239. cons.config.dtb = old_dtb
  240. cons.restart_uboot()