getrandom.sh 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # SPDX-License-Identifier: BSD-3-Clause
  2. source helpers.sh
  3. cleanup() {
  4. rm -f random.out
  5. if [ "$1" != "no-shut-down" ]; then
  6. shut_down
  7. fi
  8. }
  9. trap cleanup EXIT
  10. start_up
  11. cleanup "no-shut-down"
  12. # test file output
  13. tpm2 getrandom -o random.out 32
  14. s=`ls -l random.out | awk {'print $5'}`
  15. test $s -eq 32
  16. #test stdout
  17. tpm2 getrandom --hex 4 > random.out
  18. s=`ls -l random.out | awk {'print $5'}`
  19. test $s -eq 8
  20. yaml_verify random.out
  21. # test stdout and -Q
  22. tpm2 getrandom -Q --hex 4 > random.out
  23. s=`ls -l random.out | awk {'print $5'}`
  24. test $s -eq 0
  25. # test if multiple sessions can be specified
  26. tpm2 createprimary -C o -c prim.ctx -Q
  27. tpm2 startauthsession -S audit_session.ctx --audit-session
  28. tpm2 startauthsession -S enc_session.ctx --hmac-session --tpmkey-context prim.ctx
  29. tpm2 sessionconfig enc_session.ctx --enable-encrypt
  30. tpm2 getrandom 8 -S enc_session.ctx -S audit_session.ctx
  31. # negative tests
  32. trap - ERR
  33. # larger than any known hash size should fail
  34. tpm2 getrandom 2000 &> /dev/null
  35. if [ $? -eq 0 ]; then
  36. echo "tpm2 getrandom should fail with too big of request"
  37. exit 1
  38. fi
  39. # verify that tpm2 getrandom requires a TCTI
  40. ./tools/tpm2 getrandom -T none &> /dev/null
  41. if [ $? -eq 0 ]; then
  42. echo "tpm2 getrandom should fail with tcti: \"none\""
  43. exit 1
  44. fi
  45. exit 0