hash.sh 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # SPDX-License-Identifier: BSD-3-Clause
  2. #this script is for hash case testing
  3. source helpers.sh
  4. ticket_file=ticket.out
  5. hash_out_file=hash.out
  6. hash_in_file=hash.in
  7. out=out.yaml
  8. cleanup() {
  9. rm -f $ticket_file $hash_out_file $hash_in_file $out
  10. if [ "$1" != "no-shut-down" ]; then
  11. shut_down
  12. fi
  13. }
  14. trap cleanup EXIT
  15. start_up
  16. cleanup "no-shut-down"
  17. echo "T0naX0u123abc" > $hash_in_file
  18. # Test with ticket and hash output files (binary) and verify that the output
  19. # hash is correct. Ticket is not stable and changes run to run, don't verify it.
  20. tpm2 hash -C e -g sha1 -o $hash_out_file -t $ticket_file $hash_in_file
  21. expected=`shasum -a 1 $hash_in_file | awk '{print $1}'`
  22. actual=`cat $hash_out_file | xxd -p -c 20`
  23. test "$expected" == "$actual"
  24. cleanup "no-shut-down"
  25. # Test platform hierarchy with multiple files & verify output against sha256sum
  26. # Test a file redirection as well. Output files are binary.
  27. echo "T0naX0u123abc" > $hash_in_file
  28. tpm2 hash -C p -g sha256 -o $hash_out_file -t $ticket_file < $hash_in_file
  29. expected=`shasum -a 256 $hash_in_file | awk '{print $1}'`
  30. actual=`cat $hash_out_file | xxd -p -c 32`
  31. test "$expected" == "$actual"
  32. cleanup "no-shut-down"
  33. # Test stdout output as well as no options.
  34. # Validate that hash outputs are in hex as expected.
  35. tpm_hash_val=`echo 1234 | tpm2 hash -C n --hex`
  36. sha1sum_val=`echo 1234 | shasum -a 1 | cut -d\ -f 1-2 | tr -d '[:space:]'`
  37. if [ "$tpm_hash_val" != "$sha1sum_val" ]; then
  38. echo "Expected tpm and sha1sum to produce same hashes."
  39. echo "Got:"
  40. echo " tpm2 hash: $tpm_hash_val"
  41. echo " sha1sum: $sha1sum_val"
  42. exit 1
  43. fi
  44. # Test a file that cannot be done in 1 update call.
  45. # The tpm works on a 1024 block size.
  46. dd if=/dev/urandom of=$hash_in_file bs=2093 count=1 2>/dev/null
  47. tpm_hash_val=`tpm2 hash --hex $hash_in_file`
  48. sha1sum_val=`shasum -a 1 $hash_in_file | cut -d\ -f 1-2 | tr -d '[:space:]'`
  49. if [ "$tpm_hash_val" != "$sha1sum_val" ]; then
  50. echo "Expected tpm and sha1sum to produce same hashes"
  51. echo "Got:"
  52. echo " tpm2 hash: $tpm_hash_val"
  53. echo " sha1sum: $sha1sum_val"
  54. exit 1
  55. fi
  56. exit 0