nvinc.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # SPDX-License-Identifier: BSD-3-Clause
  2. #;**********************************************************************;
  3. source helpers.sh
  4. nv_test_index=0x1500018
  5. pcr_specification=sha256:0,1,2,3+sha1:0,1,2,3
  6. file_pcr_value=pcr.bin
  7. file_policy=policy.data
  8. cleanup() {
  9. tpm2 nvundefine -Q $nv_test_index -C o 2>/dev/null || true
  10. tpm2 nvundefine -Q 0x1500016 -C o 2>/dev/null || true
  11. tpm2 nvundefine -Q 0x1500015 -C o -P owner 2>/dev/null || true
  12. rm -f policy.bin test.bin nv.readlock foo.dat $file_pcr_value $file_policy \
  13. nv.out cap.out
  14. if [ "$1" != "no-shut-down" ]; then
  15. shut_down
  16. fi
  17. }
  18. trap cleanup EXIT
  19. start_up
  20. cleanup "no-shut-down"
  21. tpm2 clear
  22. tpm2 nvdefine -Q $nv_test_index -C o -s 8 \
  23. -a "ownerread|policywrite|ownerwrite|nt=1"
  24. tpm2 nvincrement -Q $nv_test_index -C o
  25. a=0x$(tpm2 nvread $nv_test_index -C o -s 8 | xxd -p)
  26. tpm2 nvreadpublic > nv.out
  27. yaml_get_kv nv.out "$nv_test_index" > /dev/null
  28. # Test writing to and reading from an offset by:
  29. # 1. incrementing the nv counter
  30. # 2. reading back the index
  31. # 3. comparing the result.
  32. tpm2 nvincrement -Q $nv_test_index -C o
  33. b=0x$(tpm2 nvread $nv_test_index -C o -s 8 | xxd -p)
  34. if [ $(($a+1)) -ne $(($b)) ]; then
  35. echo "Failed to increment: $(($a)) -> $(($b))."
  36. exit 1
  37. fi
  38. tpm2 nvundefine $nv_test_index -C o
  39. tpm2 pcrread -Q -o $file_pcr_value $pcr_specification
  40. tpm2 createpolicy -Q --policy-pcr -l $pcr_specification \
  41. -f $file_pcr_value -L $file_policy
  42. tpm2 nvdefine -Q 0x1500016 -C o -s 8 -L $file_policy \
  43. -a "policyread|policywrite|nt=1"
  44. # Increment with index authorization for now, since tpm2 nvincrement does not
  45. # support pcr policy.
  46. # Counter is initialised to highest value previously seen (in this case 2) then
  47. # incremented
  48. tpm2 nvincrement -Q 0x1500016 -C 0x1500016 \
  49. -P pcr:$pcr_specification=$file_pcr_value
  50. c=0x$(tpm2 nvread 0x1500016 -C 0x1500016 -P pcr:$pcr_specification=$file_pcr_value -s 8 | xxd -p)
  51. if [ $(($b+1)) -ne $(($c)) ]; then
  52. echo "Failed to increment: $(($b)) -> $(($c))."
  53. exit 1
  54. fi
  55. # this should fail because authread is not allowed
  56. trap - ERR
  57. tpm2 nvread 0x1500016 -C 0x1500016 -P "index" 2>/dev/null
  58. trap onerror ERR
  59. tpm2 nvundefine -Q 0x1500016 -C o
  60. #
  61. # Test NV access locked
  62. #
  63. tpm2 nvdefine -Q $nv_test_index -C o -s 8 \
  64. -a "ownerread|policywrite|ownerwrite|read_stclear|nt=1"
  65. tpm2 nvincrement -Q $nv_test_index -C o
  66. tpm2 nvread -Q $nv_test_index -C o -s 8
  67. tpm2 nvreadlock -Q $nv_test_index -C o
  68. # Reset ERR signal handler to test for expected nvread error
  69. trap - ERR
  70. tpm2 nvread -Q $nv_test_index -C o -s 8 2> /dev/null
  71. if [ $? != 1 ];then
  72. echo "nvread didn't fail!"
  73. exit 1
  74. fi
  75. #
  76. # Test that owner and index passwords work by
  77. # 1. Setting up the owner password
  78. # 2. Defining an nv index that can be satisfied by an:
  79. # a. Owner authorization
  80. # b. Index authorization
  81. # 3. Using index and owner based auth during write/read operations
  82. # 4. Testing that auth is needed or a failure occurs.
  83. #
  84. trap onerror ERR
  85. tpm2 changeauth -c o owner
  86. tpm2 nvdefine 0x1500015 -C o -s 8 \
  87. -a "policyread|policywrite|authread|authwrite|ownerwrite|ownerread|nt=1" \
  88. -p "index" -P "owner"
  89. # Use index password write/read, implicit -C
  90. tpm2 nvincrement -Q 0x1500015 -P "index"
  91. tpm2 nvread -Q 0x1500015 -P "index"
  92. # Use index password write/read, explicit -C
  93. tpm2 nvincrement -Q 0x1500015 -C 0x1500015 -P "index"
  94. tpm2 nvread -Q 0x1500015 -C 0x1500015 -P "index"
  95. # use owner password
  96. tpm2 nvincrement -Q 0x1500015 -C o -P "owner"
  97. tpm2 nvread -Q 0x1500015 -C o -P "owner"
  98. # Check a bad password fails
  99. trap - ERR
  100. tpm2 nvincrement -Q 0x1500015 -C 0x1500015 -P "wrong" 2>/dev/null
  101. if [ $? -eq 0 ];then
  102. echo "nvincrement with bad password should fail!"
  103. exit 1
  104. fi
  105. # Check using authorisation with tpm2 nvundefine
  106. trap onerror ERR
  107. tpm2 nvundefine 0x1500015 -C o -P "owner"
  108. exit 0