test_tpm2.py 9.1 KB

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