rsaencrypt.sh 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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_load_out_B1_B8
  7. file_rsaencrypt_key_name=name.load.B1_B8
  8. file_rsa_en_output_data=rsa_en.out
  9. file_input_data=secret.data
  10. alg_hash=sha256
  11. alg_primary_key=rsa
  12. alg_rsaencrypt_key=rsa
  13. cleanup() {
  14. rm -f $file_input_data $file_primary_key_ctx $file_rsaencrypt_key_pub \
  15. $file_rsaencrypt_key_priv $file_rsaencrypt_key_ctx \
  16. $file_rsaencrypt_key_name $file_rsa_en_output_data
  17. if [ "$1" != "no-shut-down" ]; then
  18. shut_down
  19. fi
  20. }
  21. trap cleanup EXIT
  22. start_up
  23. cleanup "no-shut-down"
  24. echo "12345678" > $file_input_data
  25. tpm2 clear
  26. tpm2 createprimary -Q -C e -g $alg_hash -G $alg_primary_key \
  27. -c $file_primary_key_ctx
  28. tpm2 create -Q -g $alg_hash -G $alg_rsaencrypt_key -u $file_rsaencrypt_key_pub \
  29. -r $file_rsaencrypt_key_priv -C $file_primary_key_ctx
  30. tpm2 loadexternal -Q -C n -u $file_rsaencrypt_key_pub \
  31. -c $file_rsaencrypt_key_ctx
  32. #./tpm2 rsaencrypt -c context_loadexternal_out6.out -I secret.data -o rsa_en.out
  33. tpm2 rsaencrypt -Q -c $file_rsaencrypt_key_ctx -o $file_rsa_en_output_data \
  34. $file_input_data
  35. # Test stdout output and test that stdin pipe works as well.
  36. cat $file_input_data | tpm2 rsaencrypt -c $file_rsaencrypt_key_ctx > /dev/null
  37. # Test if RSA encryption is possible with OAEP padding scheme
  38. tpm2 rsaencrypt -c $file_rsaencrypt_key_ctx -o $file_rsa_en_output_data \
  39. -s oaep < $file_input_data
  40. exit 0