fapi-encrypt-decrypt_ecc.sh 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. set -e
  2. source helpers.sh
  3. start_up
  4. CRYPTO_PROFILE="ECC"
  5. setup_fapi $CRYPTO_PROFILE
  6. function cleanup {
  7. # In case the test is skipped no key is created and a
  8. # failure is expected here. Therefore, we need to pass a successful
  9. # execution in any case
  10. tss2 delete --path=/ && true
  11. shut_down
  12. }
  13. trap cleanup EXIT
  14. PLAIN_TEXT=$TEMP_DIR/plaintext.file
  15. KEY_PATH="HS/SRK/myRSACrypt"
  16. ENCRYPTED_FILE=$TEMP_DIR/encrypted.file
  17. DECRYPTED_FILE=$TEMP_DIR/decrypted.file
  18. PCR_POLICY_DATA=$TEMP_DIR/pol_pcr16_0.json
  19. POLICY_PCR=policy/pcr-policy
  20. TYPES="noDa,decrypt"
  21. EMPTY_FILE=$TEMP_DIR/empty.file
  22. BIG_FILE=$TEMP_DIR/big_file.file
  23. LOG_FILE=$TEMP_DIR/log.file
  24. touch $LOG_FILE
  25. echo -n "Secret Text!" > $PLAIN_TEXT
  26. set -x
  27. if [ "$CRYPTO_PROFILE" = "ECC" ]; then
  28. echo ECC currently not supported for encryption. Skipping test.
  29. exit 077
  30. fi
  31. tss2 provision
  32. expect <<EOF
  33. # Try interactive prompt with 2 different passwords
  34. spawn tss2 createkey --path=$KEY_PATH --type=$TYPES
  35. expect "Authorize object Password: "
  36. send "1\r"
  37. expect "Authorize object Retype password: "
  38. send "2\r"
  39. expect {
  40. "Passwords do not match." {
  41. } eof {
  42. send_user "Expected password mismatch, but got nothing, or
  43. rather EOF\n"
  44. exit 1
  45. }
  46. }
  47. set ret [wait]
  48. if {[lindex \$ret 2] || [lindex \$ret 3] != 1} {
  49. send_user "Using interactive prompt with different passwords
  50. has not failed\n"
  51. exit 1
  52. }
  53. EOF
  54. expect <<EOF
  55. # Try with missing path
  56. spawn tss2 createkey --authValue=abc --type="noDa, decrypt"
  57. set ret [wait]
  58. if {[lindex \$ret 2] || [lindex \$ret 3] != 1} {
  59. send_user "Command has not failed as expected\n"
  60. exit 1
  61. }
  62. EOF
  63. tss2 import --path=$POLICY_PCR --importData=$PCR_POLICY_DATA
  64. expect <<EOF
  65. # Try interactive prompt with empty passwords
  66. spawn tss2 createkey --path=$KEY_PATH --type=$TYPES
  67. expect "Authorize object Password: "
  68. send "\r"
  69. expect "Authorize object Retype password: "
  70. send "\r"
  71. set ret [wait]
  72. if {[lindex \$ret 2] || [lindex \$ret 3] != 0} {
  73. send_user "Using interactive prompt with null password
  74. has failed\n"
  75. exit 1
  76. }
  77. EOF
  78. echo "tss2 encrypt with EMPTY_FILE" # Expected to succeed
  79. tss2 encrypt --keyPath=$KEY_PATH --plainText=$EMPTY_FILE \
  80. --cipherText=$ENCRYPTED_FILE --force
  81. echo "tss2 encrypt with BIG_FILE" # Expected to fail
  82. expect <<EOF
  83. spawn sh -c "tss2 encrypt --keyPath=$KEY_PATH --plainText=$BIG_FILE \
  84. --cipherText=$ENCRYPTED_FILE --force 2> $LOG_FILE"
  85. set ret [wait]
  86. if {[lindex \$ret 2] || [lindex \$ret 3] != 1} {
  87. set file [open $LOG_FILE r]
  88. set log [read \$file]
  89. close $file
  90. send_user "[lindex \$log]\n"
  91. exit 1
  92. }
  93. EOF
  94. if [[ "`cat $LOG_FILE`" == $SANITIZER_FILTER ]]; then
  95. echo "Error: AddressSanitizer triggered."
  96. cat $LOG_FILE
  97. exit 1
  98. fi
  99. tss2 encrypt --keyPath=$KEY_PATH --plainText=$PLAIN_TEXT \
  100. --cipherText=$ENCRYPTED_FILE --force
  101. expect <<EOF
  102. # Try with missing keypath
  103. spawn tss2 encrypt --plainText=$PLAIN_TEXT --cipherText=$ENCRYPTED_FILE
  104. set ret [wait]
  105. if {[lindex \$ret 2] || [lindex \$ret 3] != 1} {
  106. send_user "Command has not failed as expected\n"
  107. exit 1
  108. }
  109. EOF
  110. expect <<EOF
  111. # Try with missing plaintext
  112. spawn tss2 encrypt --keyPath=$KEY_PATH --cipherText=$ENCRYPTED_FILE
  113. set ret [wait]
  114. if {[lindex \$ret 2] || [lindex \$ret 3] != 1} {
  115. send_user "Command has not failed as expected\n"
  116. exit 1
  117. }
  118. EOF
  119. expect <<EOF
  120. # Try with missing ciphertext
  121. spawn tss2 encrypt --keyPath=$KEY_PATH --plainText=$PLAIN_TEXT
  122. set ret [wait]
  123. if {[lindex \$ret 2] || [lindex \$ret 3] != 1} {
  124. send_user "Command has not failed as expected\n"
  125. exit 1
  126. }
  127. EOF
  128. expect <<EOF
  129. # Try with wrong plaintext file
  130. spawn tss2 encrypt --keyPath=$KEY_PATH --plainText=abc \
  131. --cipherText=$ENCRYPTED_FILE
  132. set ret [wait]
  133. if {[lindex \$ret 2] || [lindex \$ret 3] != 1} {
  134. send_user "Command has not failed as expected\n"
  135. exit 1
  136. }
  137. EOF
  138. expect <<EOF
  139. # Try with missing ciphertext
  140. spawn tss2 decrypt --keyPath=$KEY_PATH --plainText=$DECRYPTED_FILE
  141. set ret [wait]
  142. if {[lindex \$ret 2] || [lindex \$ret 3] != 1} {
  143. send_user "Command has not failed as expected\n"
  144. exit 1
  145. }
  146. EOF
  147. expect <<EOF
  148. # Try with missing plaintext
  149. spawn tss2 decrypt --keyPath=$KEY_PATH --cipherText=$ENCRYPTED_FILE
  150. set ret [wait]
  151. if {[lindex \$ret 2] || [lindex \$ret 3] != 1} {
  152. send_user "Command has not failed as expected\n"
  153. exit 1
  154. }
  155. EOF
  156. expect <<EOF
  157. # Try with missing keyPath
  158. spawn tss2 decrypt --cipherText=$ENCRYPTED_FILE --plainText=$DECRYPTED_FILE
  159. set ret [wait]
  160. if {[lindex \$ret 2] || [lindex \$ret 3] != 1} {
  161. send_user "Command has not failed as expected\n"
  162. exit 1
  163. }
  164. EOF
  165. tss2 decrypt --keyPath=$KEY_PATH --cipherText=$ENCRYPTED_FILE \
  166. --plainText=$DECRYPTED_FILE --force
  167. if [ "`cat $DECRYPTED_FILE`" != "`cat $PLAIN_TEXT`" ]; then
  168. echo "Encryption/Decryption failed"
  169. exit 1
  170. fi
  171. echo "tss2 decrypt with EMPTY_FILE" # Expected to fail
  172. expect <<EOF
  173. spawn sh -c "tss2 decrypt --keyPath=$KEY_PATH --cipherText=$EMPTY_FILE \
  174. --plainText=$DECRYPTED_FILE --force 2> $LOG_FILE"
  175. set ret [wait]
  176. if {[lindex \$ret 2] || [lindex \$ret 3] != 1} {
  177. set file [open $LOG_FILE r]
  178. set log [read \$file]
  179. close $file
  180. send_user "[lindex \$log]\n"
  181. exit 1
  182. }
  183. EOF
  184. if [[ "`cat $LOG_FILE`" == $SANITIZER_FILTER ]]; then
  185. echo "Error: AddressSanitizer triggered."
  186. cat $LOG_FILE
  187. exit 1
  188. fi
  189. echo "tss2 decrypt with BIG_FILE" # Expected to fail
  190. expect <<EOF
  191. spawn sh -c "tss2 decrypt --keyPath=$KEY_PATH --cipherText=$BIG_FILE \
  192. --plainText=$DECRYPTED_FILE --force 2> $LOG_FILE"
  193. set ret [wait]
  194. if {[lindex \$ret 2] || [lindex \$ret 3] != 1} {
  195. set file [open $LOG_FILE r]
  196. set log [read \$file]
  197. close $file
  198. send_user "[lindex \$log]\n"
  199. exit 1
  200. }
  201. EOF
  202. if [[ "`cat $LOG_FILE`" == $SANITIZER_FILTER ]]; then
  203. echo "Error: AddressSanitizer triggered."
  204. cat $LOG_FILE
  205. exit 1
  206. fi
  207. tss2 delete --path=$KEY_PATH
  208. # Encrypt/Decrypt with password
  209. tss2 createkey --path=$KEY_PATH --type="noDa, decrypt" --authValue=abc
  210. tss2 encrypt --keyPath=$KEY_PATH --plainText=$PLAIN_TEXT \
  211. --cipherText=$ENCRYPTED_FILE --force
  212. echo -n "Fail" > $DECRYPTED_FILE
  213. expect <<EOF
  214. spawn tss2 decrypt --keyPath=$KEY_PATH --cipherText=$ENCRYPTED_FILE \
  215. --plainText=$DECRYPTED_FILE --force
  216. expect "Authorize object : "
  217. send "abc\r"
  218. set ret [wait]
  219. if {[lindex \$ret 2] || [lindex \$ret 3] != 0} {
  220. send_user "Authorization failed\n"
  221. exit 1
  222. }
  223. EOF
  224. if [ "`cat $DECRYPTED_FILE`" != "`cat $PLAIN_TEXT`" ]; then
  225. echo "Encryption/Decryption failed"
  226. exit 1
  227. fi
  228. # Try tss2 createkey with missing type. This only works for tpm2-tss >=2.4.2.
  229. # Therefore, make the test conditional
  230. VERSION="$(tss2 createkey -v | grep -Po 'fapi-version=.*' | grep -Eo '([0-9]+\.{1})+[0-9]' | sed 's/[^0-9]*//g')"
  231. if [ $VERSION -ge "242" ]; then
  232. tss2 delete --path=$KEY_PATH
  233. tss2 createkey --path=$KEY_PATH --authValue=abc
  234. fi
  235. exit 0