hmac.sh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # SPDX-License-Identifier: BSD-3-Clause
  2. source helpers.sh
  3. alg_primary_obj=sha256
  4. alg_primary_key=rsa
  5. alg_create_key=hmac
  6. handle_hmac_key=0x81010013
  7. file_primary_key_ctx=primary.ctx
  8. file_hmac_key_pub=key.pub
  9. file_hmac_key_priv=key.priv
  10. file_hmac_key_name=name.dat
  11. file_hmac_key_ctx=key.ctx
  12. file_hmac_output=hmac.out
  13. file_hmac_key_handle=key.handle
  14. file_input_data=secret.data
  15. cleanup() {
  16. rm -f $file_primary_key_ctx $file_hmac_key_pub $file_hmac_key_priv \
  17. $file_hmac_key_name $file_hmac_output ticket.out
  18. if [ $(ina "$@" "keep-context") -ne 0 ]; then
  19. rm -f $file_hmac_key_ctx $file_input_data
  20. # attempt to evict the hmac persistent key handle, but don't cause failures
  21. # if this fails as it may not be loaded.
  22. tpm2 evictcontrol -c $file_hmac_key_handle 2>/dev/null || true
  23. fi
  24. if [ $(ina "$@" "no-shut-down") -ne 0 ]; then
  25. shut_down
  26. fi
  27. }
  28. trap cleanup EXIT
  29. start_up
  30. cleanup "no-shut-down"
  31. echo "12345678" > $file_input_data
  32. tpm2 clear
  33. tpm2 createprimary -Q -C e -g $alg_primary_obj -G $alg_primary_key \
  34. -c $file_primary_key_ctx
  35. tpm2 create -Q -G $alg_create_key -u $file_hmac_key_pub -r $file_hmac_key_priv \
  36. -C $file_primary_key_ctx
  37. tpm2 load -Q -C $file_primary_key_ctx -u $file_hmac_key_pub \
  38. -r $file_hmac_key_priv -n $file_hmac_key_name -c $file_hmac_key_ctx
  39. # verify that persistent object can be used via a serialized handle
  40. tpm2 evictcontrol -C o -c $file_hmac_key_ctx -o $file_hmac_key_handle
  41. cat $file_input_data | tpm2 hmac -Q -c $file_hmac_key_handle \
  42. -o $file_hmac_output
  43. cleanup "keep-context" "no-shut-down"
  44. # Test large file, ie sequence hmac'ing.
  45. dd if=/dev/urandom of=$file_input_data bs=2093 count=1 2>/dev/null
  46. tpm2 hmac -Q -c $file_hmac_key_ctx -o $file_hmac_output $file_input_data
  47. ####handle test
  48. rm -f $file_hmac_output
  49. cleanup "no-shut-down"
  50. # Test stdin
  51. echo "12345678" > $file_input_data
  52. tpm2 clear
  53. tpm2 createprimary -Q -C e -g $alg_primary_obj -G $alg_primary_key \
  54. -c $file_primary_key_ctx
  55. tpm2 create -Q -G $alg_create_key -u $file_hmac_key_pub -r $file_hmac_key_priv \
  56. -C $file_primary_key_ctx
  57. tpm2 load -Q -C $file_primary_key_ctx -u $file_hmac_key_pub \
  58. -r $file_hmac_key_priv -n $file_hmac_key_name -c $file_hmac_key_ctx
  59. cat $file_input_data | tpm2 hmac -Q -c $file_hmac_key_ctx -o $file_hmac_output
  60. # test ticket option
  61. cat $file_input_data | tpm2 hmac -Q -c $file_hmac_key_ctx -o $file_hmac_output \
  62. -t ticket.out
  63. test -f ticket.out
  64. # test no output file
  65. cat $file_input_data | tpm2 hmac -c $file_hmac_key_ctx 1>/dev/null
  66. # verify that silent is indeed silent
  67. stdout=`cat $file_input_data | tpm2 hmac -Q -c $file_hmac_key_ctx`
  68. if [ -n "$stdout" ]; then
  69. echo "Expected no output when run in quiet mode, got\"$stdout\""
  70. exit 1
  71. fi
  72. exit 0