ciphertest.sh 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #!/bin/bash
  2. ##############################################################################
  3. # Copyright (c) 2021 by Rambus, Inc. and/or its subsidiaries
  4. # All rights reserved. Unauthorized use (including, without limitation,
  5. # distribution and copying) is strictly prohibited. All use requires,
  6. # and is subject to, explicit written authorization and nondisclosure
  7. # Rambus, Inc. and/or its subsidiaries
  8. #
  9. # For more information or support, please go to our online support system at
  10. # https://sipsupport.rambus.com.
  11. # In case you do not have an account for this system, please send an e-mail
  12. # to sipsupport@rambus.com.
  13. ##############################################################################
  14. ############################ constants ############################
  15. result=0
  16. ENGINE_ID_120=eip120
  17. KERNEL_MODULE=umpci_k
  18. OPENSSL=$OPENSSL_DIR/apps/openssl
  19. O1="cipherout1.txt" # Temporary file, used for cipher eip120 output
  20. O2="cipherout2.txt" # Temporary file, used for cipher 'internal' output
  21. #MES=testdatain.txt #Data to encrypt or Decrypt
  22. MES=message.txt
  23. BIGMES=testdatain.txt
  24. IVFILE=iv.txt # IV file for test application
  25. KEYFILE=key.txt # Key file for test application
  26. TAGFILE=tag.txt # tag file for test application
  27. AADFILE=aad.txt # aad file for test application
  28. RED='\033[0;31m'
  29. GRN='\033[0;32m'
  30. NC='\033[0m' # No Color
  31. testarray=(aes-128-ecb aes-192-ecb aes-256-ecb
  32. aes-128-cbc aes-192-cbc aes-256-cbc
  33. aes-128-ctr aes-192-ctr aes-256-ctr
  34. des-ecb des-cbc des-cfb des-ofb
  35. des-ede3-ecb des-ede3-cbc des-ede3-cfb des-ede3-ofb
  36. sm4-ecb sm4-cbc sm4-ctr)
  37. keysizearray=(16 24 32
  38. 16 24 32
  39. 16 24 32
  40. 8 8 8 8
  41. 24 24 24 24
  42. 16 16 16)
  43. ivsizearray=(0 0 0
  44. 16 16 16
  45. 16 16 16
  46. 0 8 8 8
  47. 0 8 8 8
  48. 0 16 16)
  49. testarrayxts=(aes-128-xts aes-256-xts)
  50. testarraygcm=(aes-128-gcm aes-192-gcm aes-256-gcm)
  51. DATESTAMP=$(date +%y%m%d_%H%M)
  52. TEMPFILE=tempfile.txt
  53. VALGRIND_CMD='valgrind --leak-check=full --show-leak-kinds=all --log-file='$TEMPFILE
  54. ############################ functions ############################
  55. validate_environment()
  56. {
  57. if [ ! -f $OPENSSL ]; then
  58. echo "Error: openssl client not found"
  59. exit 1
  60. fi
  61. if [ -z $OPENSSL_ENGINES ]; then
  62. echo "Error: Environment variable OPENSSL_ENGINES is undefined"
  63. exit 1
  64. fi
  65. if [ $(lsmod | grep "$KERNEL_MODULE" -c) -ne 1 ]; then
  66. echo "Error: kernel module $KERNEL_MODULE not found"
  67. exit 1
  68. fi
  69. ${OPENSSL} engine -t ${ENGINE_ID_120} &> /dev/null
  70. if [ $? -ne 0 ]; then
  71. echo -e "\n${RED}Error: OpenSSL engine ${ENGINE_ID_120} is not loaded${NC}"
  72. exit 1
  73. fi
  74. }
  75. check_diff()
  76. {
  77. diff ${O1} ${O2} > /dev/null
  78. if [ $? -ne 0 ]; then
  79. echo -e "\n${RED}$1 $2 tests FAILED${NC}"
  80. result=1
  81. return
  82. fi
  83. if [ $2 = e ]; then
  84. echo $1 ENCRYPTION PASSED
  85. else
  86. echo $1 DECRYPTION PASSED
  87. fi
  88. }
  89. test_aes()
  90. {
  91. if [ $DO_VALGRIND ]; then
  92. echo "// test_aes $1 $2" >> ciphertest_$DATESTAMP.log
  93. fi
  94. $VALGRIND ./../build/cipher --type $1 --engine ${ENGINE_ID_120} --in ${BIGMES} --out ${O1} --op $2 --iv ${IVFILE} --key ${KEYFILE} > /dev/null
  95. if [ $1 = aes-128-ecb ] || [ $1 = aes-192-ecb ] || [ $1 = aes-256-ecb ] ||
  96. [ $1 = des-ecb ] || [ $1 = des-ede3-ecb ] || [ $1 = sm4-ecb ]; then
  97. ${OPENSSL} $1 -nosalt -in ${BIGMES} -out ${O2} -$2 -K ${ckey:0:$3} -nopad > /dev/null
  98. else
  99. ${OPENSSL} $1 -nosalt -in ${BIGMES} -out ${O2} -$2 -K ${ckey:0:$3} -iv ${ivkey:0:$4} -nopad > /dev/null
  100. fi
  101. check_diff $1 $2
  102. if [ $DO_VALGRIND ]; then
  103. cat $TEMPFILE >> ciphertest_$DATESTAMP.log
  104. fi
  105. }
  106. test_xts()
  107. {
  108. if [ $DO_VALGRIND ]; then
  109. echo "// test_xts $1 $2" >> ciphertest_$DATESTAMP.log
  110. fi
  111. $VALGRIND ./../build/cipher --type $1 --engine ${ENGINE_ID_120} --in ${MES} --out ${O1} --outdef ${O2} --op $2 --iv ${IVFILE} --key ${KEYFILE} > /dev/null
  112. check_diff $1 $2
  113. if [ $DO_VALGRIND ]; then
  114. cat $TEMPFILE >> ciphertest_$DATESTAMP.log
  115. fi
  116. }
  117. test_gcm()
  118. {
  119. if [ $DO_VALGRIND ]; then
  120. echo "// test_gcm $1 $2" >> ciphertest_$DATESTAMP.log
  121. fi
  122. if [ $2 = e ]; then
  123. $VALGRIND ./../build/cipher --type $1 --engine ${ENGINE_ID_120} --in ${MES} --out ${O1} --outdef ${O2} --op $2 --iv ${IVFILE} --key ${KEYFILE} --tag ${TAGFILE} --aad ${AADFILE} > /dev/null
  124. check_diff $1 $2
  125. else
  126. $VALGRIND ./../build/cipher --type $1 --engine ${ENGINE_ID_120} --in ${O1} --out ${O1} --outdef ${O2} --op $2 --iv ${IVFILE} --key ${KEYFILE} --tag ${TAGFILE} --aad ${AADFILE} > /dev/null
  127. check_diff $1 $2
  128. fi
  129. if [ $DO_VALGRIND ]; then
  130. cat $TEMPFILE >> ciphertest_$DATESTAMP.log
  131. fi
  132. }
  133. main () {
  134. ${OPENSSL} version
  135. validate_environment
  136. if [ "$1" = "-v" ]; then
  137. DO_VALGRIND=1
  138. VALGRIND=$VALGRIND_CMD
  139. echo "// OS_IK ciphertest valgrind results - $DATESTAMP" > ciphertest_$DATESTAMP.log
  140. fi
  141. ckey=$(<${KEYFILE})
  142. ivkey=$(<${IVFILE})
  143. for ((loop = 0; loop < ${#testarray[@]}; loop++)); do
  144. test_aes ${testarray[loop]} e ${keysizearray[loop]}*2 ${ivsizearray[loop]}*2
  145. test_aes ${testarray[loop]} d ${keysizearray[loop]}*2 ${ivsizearray[loop]}*2
  146. done
  147. for loop in ${testarraygcm[@]}; do
  148. test_gcm $loop e
  149. test_gcm $loop d
  150. done
  151. for loop in ${testarrayxts[@]}; do
  152. test_xts $loop e
  153. test_xts $loop d
  154. done
  155. rm ${O1} ${O2}
  156. if [ ${result} -ne 0 ]; then
  157. echo -e "\n${RED}Cipher tests FAILED${NC}"
  158. return
  159. fi
  160. echo -e "\n${GRN}Cipher tests PASSED${NC}"
  161. if [ $DO_VALGRIND ]; then
  162. rm $TEMPFILE
  163. fi
  164. }
  165. main "$@"