test_tpm2.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 init')
  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 startup TPM2_SU_CLEAR')
  39. u_boot_console.run_command('tpm2 self_test full')
  40. u_boot_console.run_command('tpm2 clear TPM2_RH_LOCKOUT')
  41. output = u_boot_console.run_command('echo $?')
  42. if not output.endswith('0'):
  43. u_boot_console.run_command('tpm2 clear TPM2_RH_PLATFORM')
  44. u_boot_console.run_command('echo --- end of init ---')
  45. @pytest.mark.buildconfigspec('cmd_tpm_v2')
  46. def test_tpm2_init(u_boot_console):
  47. """Init the software stack to use TPMv2 commands."""
  48. skip_test = u_boot_console.config.env.get('env__tpm_device_test_skip', False)
  49. if skip_test:
  50. pytest.skip('skip TPM device test')
  51. u_boot_console.run_command('tpm2 init')
  52. output = u_boot_console.run_command('echo $?')
  53. assert output.endswith('0')
  54. @pytest.mark.buildconfigspec('cmd_tpm_v2')
  55. def test_tpm2_startup(u_boot_console):
  56. """Execute a TPM2_Startup command.
  57. Initiate the TPM internal state machine.
  58. """
  59. skip_test = u_boot_console.config.env.get('env__tpm_device_test_skip', False)
  60. if skip_test:
  61. pytest.skip('skip TPM device test')
  62. u_boot_console.run_command('tpm2 startup TPM2_SU_CLEAR')
  63. output = u_boot_console.run_command('echo $?')
  64. assert output.endswith('0')
  65. @pytest.mark.buildconfigspec('cmd_tpm_v2')
  66. def test_tpm2_self_test_full(u_boot_console):
  67. """Execute a TPM2_SelfTest (full) command.
  68. Ask the TPM to perform all self tests to also enable full capabilities.
  69. """
  70. skip_test = u_boot_console.config.env.get('env__tpm_device_test_skip', False)
  71. if skip_test:
  72. pytest.skip('skip TPM device test')
  73. u_boot_console.run_command('tpm2 self_test full')
  74. output = u_boot_console.run_command('echo $?')
  75. assert output.endswith('0')
  76. @pytest.mark.buildconfigspec('cmd_tpm_v2')
  77. def test_tpm2_continue_self_test(u_boot_console):
  78. """Execute a TPM2_SelfTest (continued) command.
  79. Ask the TPM to finish its self tests (alternative to the full test) in order
  80. to enter a fully operational state.
  81. """
  82. skip_test = u_boot_console.config.env.get('env__tpm_device_test_skip', False)
  83. if skip_test:
  84. pytest.skip('skip TPM device test')
  85. u_boot_console.run_command('tpm2 self_test continue')
  86. output = u_boot_console.run_command('echo $?')
  87. assert output.endswith('0')
  88. @pytest.mark.buildconfigspec('cmd_tpm_v2')
  89. def test_tpm2_clear(u_boot_console):
  90. """Execute a TPM2_Clear command.
  91. Ask the TPM to reset entirely its internal state (including internal
  92. configuration, passwords, counters and DAM parameters). This is half of the
  93. TAKE_OWNERSHIP command from TPMv1.
  94. Use the LOCKOUT hierarchy for this. The LOCKOUT/PLATFORM hierarchies must
  95. not have a password set, otherwise this test will fail. ENDORSEMENT and
  96. PLATFORM hierarchies are also available.
  97. """
  98. skip_test = u_boot_console.config.env.get('env__tpm_device_test_skip', False)
  99. if skip_test:
  100. pytest.skip('skip TPM device test')
  101. u_boot_console.run_command('tpm2 clear TPM2_RH_LOCKOUT')
  102. output = u_boot_console.run_command('echo $?')
  103. assert output.endswith('0')
  104. u_boot_console.run_command('tpm2 clear TPM2_RH_PLATFORM')
  105. output = u_boot_console.run_command('echo $?')
  106. assert output.endswith('0')
  107. @pytest.mark.buildconfigspec('cmd_tpm_v2')
  108. def test_tpm2_change_auth(u_boot_console):
  109. """Execute a TPM2_HierarchyChangeAuth command.
  110. Ask the TPM to change the owner, ie. set a new password: 'unicorn'
  111. Use the LOCKOUT hierarchy for this. ENDORSEMENT and PLATFORM hierarchies are
  112. also available.
  113. """
  114. force_init(u_boot_console)
  115. u_boot_console.run_command('tpm2 change_auth TPM2_RH_LOCKOUT unicorn')
  116. output = u_boot_console.run_command('echo $?')
  117. assert output.endswith('0')
  118. u_boot_console.run_command('tpm2 clear TPM2_RH_LOCKOUT unicorn')
  119. output = u_boot_console.run_command('echo $?')
  120. u_boot_console.run_command('tpm2 clear TPM2_RH_PLATFORM')
  121. assert output.endswith('0')
  122. @pytest.mark.buildconfigspec('cmd_tpm_v2')
  123. def test_tpm2_get_capability(u_boot_console):
  124. """Execute a TPM_GetCapability command.
  125. Display one capability. In our test case, let's display the default DAM
  126. lockout counter that should be 0 since the CLEAR:
  127. - TPM_CAP_TPM_PROPERTIES = 0x6
  128. - TPM_PT_LOCKOUT_COUNTER (1st parameter) = PTR_VAR + 14
  129. There is no expected default values because it would depend on the chip
  130. used. We can still save them in order to check they have changed later.
  131. """
  132. force_init(u_boot_console)
  133. ram = u_boot_utils.find_ram_base(u_boot_console)
  134. read_cap = u_boot_console.run_command('tpm2 get_capability 0x6 0x20e 0x200 1') #0x%x 1' % ram)
  135. output = u_boot_console.run_command('echo $?')
  136. assert output.endswith('0')
  137. assert 'Property 0x0000020e: 0x00000000' in read_cap
  138. @pytest.mark.buildconfigspec('cmd_tpm_v2')
  139. def test_tpm2_dam_parameters(u_boot_console):
  140. """Execute a TPM2_DictionaryAttackParameters command.
  141. Change Dictionary Attack Mitigation (DAM) parameters. Ask the TPM to change:
  142. - Max number of failed authentication before lockout: 3
  143. - Time before the failure counter is automatically decremented: 10 sec
  144. - Time after a lockout failure before it can be attempted again: 0 sec
  145. For an unknown reason, the DAM parameters must be changed before changing
  146. the authentication, otherwise the lockout will be engaged after the first
  147. failed authentication attempt.
  148. """
  149. force_init(u_boot_console)
  150. ram = u_boot_utils.find_ram_base(u_boot_console)
  151. # Set the DAM parameters to known values
  152. u_boot_console.run_command('tpm2 dam_parameters 3 10 0')
  153. output = u_boot_console.run_command('echo $?')
  154. assert output.endswith('0')
  155. # Check the values have been saved
  156. read_cap = u_boot_console.run_command('tpm2 get_capability 0x6 0x20f 0x%x 3' % ram)
  157. output = u_boot_console.run_command('echo $?')
  158. assert output.endswith('0')
  159. assert 'Property 0x0000020f: 0x00000003' in read_cap
  160. assert 'Property 0x00000210: 0x0000000a' in read_cap
  161. assert 'Property 0x00000211: 0x00000000' in read_cap
  162. @pytest.mark.buildconfigspec('cmd_tpm_v2')
  163. def test_tpm2_pcr_read(u_boot_console):
  164. """Execute a TPM2_PCR_Read command.
  165. Perform a PCR read of the 0th PCR. Must be zero.
  166. """
  167. force_init(u_boot_console)
  168. ram = u_boot_utils.find_ram_base(u_boot_console)
  169. read_pcr = u_boot_console.run_command('tpm2 pcr_read 0 0x%x' % ram)
  170. output = u_boot_console.run_command('echo $?')
  171. assert output.endswith('0')
  172. # Save the number of PCR updates
  173. str = re.findall(r'\d+ known updates', read_pcr)[0]
  174. global updates
  175. updates = int(re.findall(r'\d+', str)[0])
  176. # Check the output value
  177. assert 'PCR #0 content' in read_pcr
  178. assert '00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00' in read_pcr
  179. @pytest.mark.buildconfigspec('cmd_tpm_v2')
  180. def test_tpm2_pcr_extend(u_boot_console):
  181. """Execute a TPM2_PCR_Extend command.
  182. Perform a PCR extension with a known hash in memory (zeroed since the board
  183. must have been rebooted).
  184. No authentication mechanism is used here, not protecting against packet
  185. replay, yet.
  186. """
  187. force_init(u_boot_console)
  188. ram = u_boot_utils.find_ram_base(u_boot_console)
  189. u_boot_console.run_command('tpm2 pcr_extend 0 0x%x' % ram)
  190. output = u_boot_console.run_command('echo $?')
  191. assert output.endswith('0')
  192. # Read the value back into a different place so we can still use 'ram' as
  193. # our zero bytes
  194. read_pcr = u_boot_console.run_command('tpm2 pcr_read 0 0x%x' % (ram + 0x20))
  195. output = u_boot_console.run_command('echo $?')
  196. assert output.endswith('0')
  197. assert 'f5 a5 fd 42 d1 6a 20 30 27 98 ef 6e d3 09 97 9b' in read_pcr
  198. assert '43 00 3d 23 20 d9 f0 e8 ea 98 31 a9 27 59 fb 4b' in read_pcr
  199. str = re.findall(r'\d+ known updates', read_pcr)[0]
  200. new_updates = int(re.findall(r'\d+', str)[0])
  201. assert (updates + 1) == new_updates
  202. u_boot_console.run_command('tpm2 pcr_extend 0 0x%x' % ram)
  203. output = u_boot_console.run_command('echo $?')
  204. assert output.endswith('0')
  205. read_pcr = u_boot_console.run_command('tpm2 pcr_read 0 0x%x' % (ram + 0x20))
  206. output = u_boot_console.run_command('echo $?')
  207. assert output.endswith('0')
  208. assert '7a 05 01 f5 95 7b df 9c b3 a8 ff 49 66 f0 22 65' in read_pcr
  209. assert 'f9 68 65 8b 7a 9c 62 64 2c ba 11 65 e8 66 42 f5' in read_pcr
  210. str = re.findall(r'\d+ known updates', read_pcr)[0]
  211. new_updates = int(re.findall(r'\d+', str)[0])
  212. assert (updates + 2) == new_updates
  213. @pytest.mark.buildconfigspec('cmd_tpm_v2')
  214. def test_tpm2_cleanup(u_boot_console):
  215. """Ensure the TPM is cleared from password or test related configuration."""
  216. force_init(u_boot_console, True)