rsadecrypt.sh 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # SPDX-License-Identifier: BSD-3-Clause
  2. source helpers.sh
  3. file_primary_key_ctx=context.p_B1
  4. file_rsaencrypt_key_pub=opuB1_B8
  5. file_rsaencrypt_key_priv=oprB1_B8
  6. file_rsaencrypt_key_ctx=context_loadext_out_B1_B8
  7. file_rsadecrypt_key_ctx=context_load_out_B1_B8
  8. file_rsaencrypt_key_name=name.load.B1_B8
  9. file_rsa_en_output_data=rsa_en.out
  10. file_rsa_de_output_data=rsa_de.out
  11. file_input_data=secret.data
  12. alg_hash=sha256
  13. alg_primary_key=rsa
  14. alg_rsaencrypt_key=rsa
  15. cleanup() {
  16. rm -f $file_input_data $file_primary_key_ctx $file_rsaencrypt_key_pub \
  17. $file_rsaencrypt_key_priv $file_rsaencrypt_key_ctx \
  18. $file_rsaencrypt_key_name $file_output_data $file_rsa_en_output_data \
  19. $file_rsa_de_output_data $file_rsadecrypt_key_ctx label.dat
  20. if [ "$1" != "no-shut-down" ]; then
  21. shut_down
  22. fi
  23. }
  24. trap cleanup EXIT
  25. start_up
  26. cleanup "no-shut-down"
  27. echo "12345678" > $file_input_data
  28. tpm2 clear
  29. tpm2 createprimary -Q -C e -g $alg_hash -G $alg_primary_key \
  30. -c $file_primary_key_ctx
  31. tpm2 create -Q -g $alg_hash -p foo -G $alg_rsaencrypt_key \
  32. -u $file_rsaencrypt_key_pub -r $file_rsaencrypt_key_priv \
  33. -C $file_primary_key_ctx
  34. tpm2 loadexternal -Q -C n -u $file_rsaencrypt_key_pub \
  35. -c $file_rsaencrypt_key_ctx
  36. tpm2 rsaencrypt -Q -c $file_rsaencrypt_key_ctx -o $file_rsa_en_output_data < \
  37. $file_input_data
  38. tpm2 load -Q -C $file_primary_key_ctx -u $file_rsaencrypt_key_pub \
  39. -r $file_rsaencrypt_key_priv -n $file_rsaencrypt_key_name \
  40. -c $file_rsadecrypt_key_ctx
  41. tpm2 rsadecrypt -Q -c $file_rsadecrypt_key_ctx -p foo -o \
  42. $file_rsa_de_output_data $file_rsa_en_output_data
  43. # Test the diffeent padding schemes ...
  44. tpm2 rsaencrypt -Q -c $file_rsaencrypt_key_ctx -o $file_rsa_en_output_data \
  45. -s rsaes < $file_input_data
  46. tpm2 rsadecrypt -Q -c $file_rsadecrypt_key_ctx -p foo -o \
  47. $file_rsa_de_output_data -s rsaes $file_rsa_en_output_data
  48. tpm2 rsaencrypt -Q -c $file_rsaencrypt_key_ctx -o $file_rsa_en_output_data \
  49. -s null < $file_input_data
  50. tpm2 rsadecrypt -Q -c $file_rsadecrypt_key_ctx -p foo -o \
  51. $file_rsa_de_output_data -s null $file_rsa_en_output_data
  52. # Test the label option with a string
  53. tpm2 rsaencrypt -Q -c $file_rsaencrypt_key_ctx -l mylabel \
  54. -o $file_rsa_en_output_data < $file_input_data
  55. tpm2 rsadecrypt -Q -c $file_rsadecrypt_key_ctx -l mylabel -p foo \
  56. -o $file_rsa_de_output_data $file_rsa_en_output_data
  57. # Test the label option with a file
  58. echo "my file label" > label.dat
  59. tpm2 rsaencrypt -Q -c $file_rsaencrypt_key_ctx -l label.dat \
  60. -o $file_rsa_en_output_data < $file_input_data
  61. tpm2 rsadecrypt -Q -c $file_rsadecrypt_key_ctx -l label.dat -p foo \
  62. -o $file_rsa_de_output_data $file_rsa_en_output_data
  63. # Test RSA encryption/ decryption with OAEP padding mode
  64. tpm2 rsaencrypt -Q -c $file_rsaencrypt_key_ctx -o $file_rsa_en_output_data \
  65. -s oaep < $file_input_data
  66. tpm2 rsadecrypt -Q -c $file_rsadecrypt_key_ctx -p foo -o \
  67. $file_rsa_de_output_data -s oaep $file_rsa_en_output_data
  68. exit 0