test_tpm2.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright (c) 2018, Bootlin
  3. # Author: Miquel Raynal <miquel.raynal@bootlin.com>
  4. import os.path
  5. import pytest
  6. import u_boot_utils
  7. import re
  8. import time
  9. """
  10. Test the TPMv2.x related commands. You must have a working hardware setup in
  11. order to do these tests.
  12. Notes:
  13. * These tests will prove the password mechanism. The TPM chip must be cleared of
  14. any password.
  15. * Commands like pcr_setauthpolicy and pcr_resetauthpolicy are not implemented
  16. here because they would fail the tests in most cases (TPMs do not implement them
  17. and return an error).
  18. Note:
  19. This test doesn't rely on boardenv_* configuration value but can change test
  20. behavior.
  21. * Setup env__tpm_device_test_skip to True if tests with TPM devices should be
  22. skipped.
  23. """
  24. updates = 0
  25. def force_init(u_boot_console, force=False):
  26. """When a test fails, U-Boot is reset. Because TPM stack must be initialized
  27. after each reboot, we must ensure these lines are always executed before
  28. trying any command or they will fail with no reason. Executing 'tpm init'
  29. twice will spawn an error used to detect that the TPM was not reset and no
  30. initialization code should be run.
  31. """
  32. skip_test = u_boot_console.config.env.get('env__tpm_device_test_skip', False)
  33. if skip_test:
  34. pytest.skip('skip TPM device test')
  35. output = u_boot_console.run_command('tpm2 autostart')
  36. if force or not 'Error' in output:
  37. u_boot_console.run_command('echo --- start of init ---')
  38. u_boot_console.run_command('tpm2 clear TPM2_RH_LOCKOUT')
  39. output = u_boot_console.run_command('echo $?')
  40. if not output.endswith('0'):
  41. u_boot_console.run_command('tpm2 clear TPM2_RH_PLATFORM')
  42. u_boot_console.run_command('echo --- end of init ---')
  43. def is_sandbox(cons):
  44. # Array slice removes leading/trailing quotes.
  45. sys_arch = cons.config.buildconfig.get('config_sys_arch', '"sandbox"')[1:-1]
  46. return sys_arch == 'sandbox'
  47. @pytest.mark.buildconfigspec('cmd_tpm_v2')
  48. def test_tpm2_init(u_boot_console):
  49. """Init the software stack to use TPMv2 commands."""
  50. skip_test = u_boot_console.config.env.get('env__tpm_device_test_skip', False)
  51. if skip_test:
  52. pytest.skip('skip TPM device test')
  53. u_boot_console.run_command('tpm2 init')
  54. output = u_boot_console.run_command('echo $?')
  55. assert output.endswith('0')
  56. @pytest.mark.buildconfigspec('cmd_tpm_v2')
  57. def test_tpm2_startup(u_boot_console):
  58. """Execute a TPM2_Startup command.
  59. Initiate the TPM internal state machine.
  60. """
  61. u_boot_console.run_command('tpm2 startup TPM2_SU_CLEAR')
  62. output = u_boot_console.run_command('echo $?')
  63. assert output.endswith('0')
  64. def tpm2_sandbox_init(u_boot_console):
  65. """Put sandbox back into a known state so we can run a test
  66. This allows all tests to run in parallel, since no test depends on another.
  67. """
  68. u_boot_console.restart_uboot()
  69. u_boot_console.run_command('tpm2 autostart')
  70. output = u_boot_console.run_command('echo $?')
  71. assert output.endswith('0')
  72. skip_test = u_boot_console.config.env.get('env__tpm_device_test_skip', False)
  73. if skip_test:
  74. pytest.skip('skip TPM device test')
  75. @pytest.mark.buildconfigspec('cmd_tpm_v2')
  76. def test_tpm2_sandbox_self_test_full(u_boot_console):
  77. """Execute a TPM2_SelfTest (full) command.
  78. Ask the TPM to perform all self tests to also enable full capabilities.
  79. """
  80. if is_sandbox(u_boot_console):
  81. u_boot_console.restart_uboot()
  82. u_boot_console.run_command('tpm2 init')
  83. output = u_boot_console.run_command('echo $?')
  84. assert output.endswith('0')
  85. u_boot_console.run_command('tpm2 startup TPM2_SU_CLEAR')
  86. output = u_boot_console.run_command('echo $?')
  87. assert output.endswith('0')
  88. skip_test = u_boot_console.config.env.get('env__tpm_device_test_skip', False)
  89. if skip_test:
  90. pytest.skip('skip TPM device test')
  91. u_boot_console.run_command('tpm2 self_test full')
  92. output = u_boot_console.run_command('echo $?')
  93. assert output.endswith('0')
  94. @pytest.mark.buildconfigspec('cmd_tpm_v2')
  95. def test_tpm2_continue_self_test(u_boot_console):
  96. """Execute a TPM2_SelfTest (continued) command.
  97. Ask the TPM to finish its self tests (alternative to the full test) in order
  98. to enter a fully operational state.
  99. """
  100. skip_test = u_boot_console.config.env.get('env__tpm_device_test_skip', False)
  101. if skip_test:
  102. pytest.skip('skip TPM device test')
  103. if is_sandbox(u_boot_console):
  104. tpm2_sandbox_init(u_boot_console)
  105. u_boot_console.run_command('tpm2 self_test continue')
  106. output = u_boot_console.run_command('echo $?')
  107. assert output.endswith('0')
  108. @pytest.mark.buildconfigspec('cmd_tpm_v2')
  109. def test_tpm2_clear(u_boot_console):
  110. """Execute a TPM2_Clear command.
  111. Ask the TPM to reset entirely its internal state (including internal
  112. configuration, passwords, counters and DAM parameters). This is half of the
  113. TAKE_OWNERSHIP command from TPMv1.
  114. Use the LOCKOUT hierarchy for this. The LOCKOUT/PLATFORM hierarchies must
  115. not have a password set, otherwise this test will fail. ENDORSEMENT and
  116. PLATFORM hierarchies are also available.
  117. """
  118. if is_sandbox(u_boot_console):
  119. tpm2_sandbox_init(u_boot_console)
  120. skip_test = u_boot_console.config.env.get('env__tpm_device_test_skip', False)
  121. if skip_test:
  122. pytest.skip('skip TPM device test')
  123. u_boot_console.run_command('tpm2 clear TPM2_RH_LOCKOUT')
  124. output = u_boot_console.run_command('echo $?')
  125. assert output.endswith('0')
  126. u_boot_console.run_command('tpm2 clear TPM2_RH_PLATFORM')
  127. output = u_boot_console.run_command('echo $?')
  128. assert output.endswith('0')
  129. @pytest.mark.buildconfigspec('cmd_tpm_v2')
  130. def test_tpm2_change_auth(u_boot_console):
  131. """Execute a TPM2_HierarchyChangeAuth command.
  132. Ask the TPM to change the owner, ie. set a new password: 'unicorn'
  133. Use the LOCKOUT hierarchy for this. ENDORSEMENT and PLATFORM hierarchies are
  134. also available.
  135. """
  136. if is_sandbox(u_boot_console):
  137. tpm2_sandbox_init(u_boot_console)
  138. force_init(u_boot_console)
  139. u_boot_console.run_command('tpm2 change_auth TPM2_RH_LOCKOUT unicorn')
  140. output = u_boot_console.run_command('echo $?')
  141. assert output.endswith('0')
  142. u_boot_console.run_command('tpm2 clear TPM2_RH_LOCKOUT unicorn')
  143. output = u_boot_console.run_command('echo $?')
  144. u_boot_console.run_command('tpm2 clear TPM2_RH_PLATFORM')
  145. assert output.endswith('0')
  146. @pytest.mark.buildconfigspec('sandbox')
  147. @pytest.mark.buildconfigspec('cmd_tpm_v2')
  148. def test_tpm2_get_capability(u_boot_console):
  149. """Execute a TPM_GetCapability command.
  150. Display one capability. In our test case, let's display the default DAM
  151. lockout counter that should be 0 since the CLEAR:
  152. - TPM_CAP_TPM_PROPERTIES = 0x6
  153. - TPM_PT_LOCKOUT_COUNTER (1st parameter) = PTR_VAR + 14
  154. There is no expected default values because it would depend on the chip
  155. used. We can still save them in order to check they have changed later.
  156. """
  157. if is_sandbox(u_boot_console):
  158. tpm2_sandbox_init(u_boot_console)
  159. force_init(u_boot_console)
  160. ram = u_boot_utils.find_ram_base(u_boot_console)
  161. read_cap = u_boot_console.run_command('tpm2 get_capability 0x6 0x20e 0x200 1') #0x%x 1' % ram)
  162. output = u_boot_console.run_command('echo $?')
  163. assert output.endswith('0')
  164. assert 'Property 0x0000020e: 0x00000000' in read_cap
  165. @pytest.mark.buildconfigspec('cmd_tpm_v2')
  166. def test_tpm2_dam_parameters(u_boot_console):
  167. """Execute a TPM2_DictionaryAttackParameters command.
  168. Change Dictionary Attack Mitigation (DAM) parameters. Ask the TPM to change:
  169. - Max number of failed authentication before lockout: 3
  170. - Time before the failure counter is automatically decremented: 10 sec
  171. - Time after a lockout failure before it can be attempted again: 0 sec
  172. For an unknown reason, the DAM parameters must be changed before changing
  173. the authentication, otherwise the lockout will be engaged after the first
  174. failed authentication attempt.
  175. """
  176. if is_sandbox(u_boot_console):
  177. tpm2_sandbox_init(u_boot_console)
  178. force_init(u_boot_console)
  179. ram = u_boot_utils.find_ram_base(u_boot_console)
  180. # Set the DAM parameters to known values
  181. u_boot_console.run_command('tpm2 dam_parameters 3 10 0')
  182. output = u_boot_console.run_command('echo $?')
  183. assert output.endswith('0')
  184. # Check the values have been saved
  185. read_cap = u_boot_console.run_command('tpm2 get_capability 0x6 0x20f 0x%x 3' % ram)
  186. output = u_boot_console.run_command('echo $?')
  187. assert output.endswith('0')
  188. assert 'Property 0x0000020f: 0x00000003' in read_cap
  189. assert 'Property 0x00000210: 0x0000000a' in read_cap
  190. assert 'Property 0x00000211: 0x00000000' in read_cap
  191. @pytest.mark.buildconfigspec('cmd_tpm_v2')
  192. def test_tpm2_pcr_read(u_boot_console):
  193. """Execute a TPM2_PCR_Read command.
  194. Perform a PCR read of the 0th PCR. Must be zero.
  195. """
  196. if is_sandbox(u_boot_console):
  197. tpm2_sandbox_init(u_boot_console)
  198. force_init(u_boot_console)
  199. ram = u_boot_utils.find_ram_base(u_boot_console)
  200. read_pcr = u_boot_console.run_command('tpm2 pcr_read 0 0x%x' % ram)
  201. output = u_boot_console.run_command('echo $?')
  202. assert output.endswith('0')
  203. # Save the number of PCR updates
  204. str = re.findall(r'\d+ known updates', read_pcr)[0]
  205. global updates
  206. updates = int(re.findall(r'\d+', str)[0])
  207. # Check the output value
  208. assert 'PCR #0 content' in read_pcr
  209. assert '00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00' in read_pcr
  210. @pytest.mark.buildconfigspec('cmd_tpm_v2')
  211. def test_tpm2_pcr_extend(u_boot_console):
  212. """Execute a TPM2_PCR_Extend command.
  213. Perform a PCR extension with a known hash in memory (zeroed since the board
  214. must have been rebooted).
  215. No authentication mechanism is used here, not protecting against packet
  216. replay, yet.
  217. """
  218. if is_sandbox(u_boot_console):
  219. tpm2_sandbox_init(u_boot_console)
  220. force_init(u_boot_console)
  221. ram = u_boot_utils.find_ram_base(u_boot_console)
  222. read_pcr = u_boot_console.run_command('tpm2 pcr_read 0 0x%x' % (ram + 0x20))
  223. output = u_boot_console.run_command('echo $?')
  224. assert output.endswith('0')
  225. str = re.findall(r'\d+ known updates', read_pcr)[0]
  226. updates = int(re.findall(r'\d+', str)[0])
  227. u_boot_console.run_command('tpm2 pcr_extend 0 0x%x' % ram)
  228. output = u_boot_console.run_command('echo $?')
  229. assert output.endswith('0')
  230. # Read the value back into a different place so we can still use 'ram' as
  231. # our zero bytes
  232. read_pcr = u_boot_console.run_command('tpm2 pcr_read 0 0x%x' % (ram + 0x20))
  233. output = u_boot_console.run_command('echo $?')
  234. assert output.endswith('0')
  235. assert 'f5 a5 fd 42 d1 6a 20 30 27 98 ef 6e d3 09 97 9b' in read_pcr
  236. assert '43 00 3d 23 20 d9 f0 e8 ea 98 31 a9 27 59 fb 4b' in read_pcr
  237. str = re.findall(r'\d+ known updates', read_pcr)[0]
  238. new_updates = int(re.findall(r'\d+', str)[0])
  239. assert (updates + 1) == new_updates
  240. u_boot_console.run_command('tpm2 pcr_extend 0 0x%x' % ram)
  241. output = u_boot_console.run_command('echo $?')
  242. assert output.endswith('0')
  243. read_pcr = u_boot_console.run_command('tpm2 pcr_read 0 0x%x' % (ram + 0x20))
  244. output = u_boot_console.run_command('echo $?')
  245. assert output.endswith('0')
  246. assert '7a 05 01 f5 95 7b df 9c b3 a8 ff 49 66 f0 22 65' in read_pcr
  247. assert 'f9 68 65 8b 7a 9c 62 64 2c ba 11 65 e8 66 42 f5' in read_pcr
  248. str = re.findall(r'\d+ known updates', read_pcr)[0]
  249. new_updates = int(re.findall(r'\d+', str)[0])
  250. assert (updates + 2) == new_updates
  251. @pytest.mark.buildconfigspec('cmd_tpm_v2')
  252. def test_tpm2_cleanup(u_boot_console):
  253. """Ensure the TPM is cleared from password or test related configuration."""
  254. force_init(u_boot_console, True)