htest.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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="htest_tmp1.txt" # Temporary file, used for hash eip120 output
  20. O2="htest_tmp2.txt" # Temporary file, used for hash 'internal' output
  21. HKEY=hkey.txt # HMAC key file
  22. CKEY=ckey.txt # CMAC key file
  23. RED='\033[0;31m'
  24. GRN='\033[0;32m'
  25. NC='\033[0m' # No Color
  26. DATESTAMP=$(date +%y%m%d_%H%M)
  27. TEMPFILE=tempfile.txt
  28. VALGRIND_CMD='valgrind --leak-check=full --show-leak-kinds=all --log-file='$TEMPFILE
  29. ############################ functions ############################
  30. validate_environment()
  31. {
  32. if [ ! -f $OPENSSL_DIR/apps/openssl ]; then
  33. echo -e "${RED}Error: openssl client not found${NC}"
  34. exit 1
  35. fi
  36. if [ -z $OPENSSL_ENGINES ]; then
  37. echo -e "${RED}Error: Environment variable OPENSSL_ENGINES is undefined${NC}"
  38. exit 1
  39. fi
  40. if [ $(lsmod | grep "$KERNEL_MODULE" -c) -ne 1 ]; then
  41. echo -e "${RED}Error: kernel module $KERNEL_MODULE not found${NC}"
  42. exit 1
  43. fi
  44. ${OPENSSL} engine -t ${ENGINE_ID_120} &> /dev/null
  45. if [ $? -ne 0 ]; then
  46. echo -e "\n${RED}Error: OpenSSL engine ${ENGINE_ID_120} is not loaded${NC}"
  47. exit 1
  48. fi
  49. }
  50. check_diff()
  51. {
  52. diff ${O1} ${O2} > /dev/null
  53. if [ $? -ne 0 ]; then
  54. echo $1 "(psize "$2")" FAILED
  55. result=1
  56. return
  57. fi
  58. echo $1 "(psize "$2")" PASSED
  59. }
  60. test_hmac()
  61. {
  62. if [ $DO_VALGRIND ]; then
  63. echo "// $1 $2" >> htest_$DATESTAMP.log
  64. fi
  65. head -c $2 lorum.txt | $VALGRIND ${OPENSSL} dgst -engine ${ENGINE_ID_120} -$1 -out ${O1} -mac HMAC -macopt hexkey:$hkey &> /dev/null
  66. if [ $? -ne 0 ]; then
  67. echo "openssl with " $1 "(psize "$2")" FAILED
  68. result=1
  69. return
  70. fi
  71. if [ $DO_VALGRIND ]; then
  72. cat $TEMPFILE >> htest_$DATESTAMP.log
  73. fi
  74. head -c $2 lorum.txt | ${OPENSSL} dgst -$1 -out ${O2} -mac HMAC -macopt hexkey:$hkey > /dev/null
  75. check_diff HMAC-$1 $2
  76. }
  77. test_hmacs()
  78. {
  79. psizes=$2
  80. for psize in ${psizes[@]}; do
  81. test_hmac $1 $psize
  82. done
  83. }
  84. test_cmac()
  85. {
  86. if [ $DO_VALGRIND ]; then
  87. echo "// $1 $2" >> htest_$DATESTAMP.log
  88. fi
  89. head -c $2 lorum.txt | $VALGRIND ${OPENSSL} dgst -engine ${ENGINE_ID_120} -out ${O1} -mac CMAC -macopt cipher:$1 -macopt hexkey:$ckey &> /dev/null
  90. if [ $? -ne 0 ]; then
  91. echo "openssl with cipher " $1 "(psize "$2")" FAILED
  92. result=1
  93. return
  94. fi
  95. if [ $DO_VALGRIND ]; then
  96. cat $TEMPFILE >> htest_$DATESTAMP.log
  97. fi
  98. head -c $2 lorum.txt | ${OPENSSL} dgst -out ${O2} -mac CMAC -macopt cipher:$1 -macopt hexkey:$ckey > /dev/null
  99. check_diff CMAC-$1 $2
  100. }
  101. test_cmacs()
  102. {
  103. psizes=$2
  104. for psize in ${psizes[@]}; do
  105. test_cmac $1 $psize
  106. done
  107. }
  108. test_hash()
  109. {
  110. if [ $DO_VALGRIND ]; then
  111. echo "// $1 $2" >> htest_$DATESTAMP.log
  112. fi
  113. head -c $2 lorum.txt | $VALGRIND ${OPENSSL} dgst -out ${O1} -$1 -engine eip120 -engine_impl &> /dev/null
  114. if [ $? -ne 0 ]; then
  115. echo "openssl with" $1 "(psize "$2")" FAILED
  116. result=1
  117. return
  118. fi
  119. if [ $DO_VALGRIND ]; then
  120. cat $TEMPFILE >> htest_$DATESTAMP.log
  121. fi
  122. head -c $2 lorum.txt | ${OPENSSL} dgst -out ${O2} -$1 &> /dev/null
  123. check_diff HASH-$1 $2
  124. }
  125. test_hashes()
  126. {
  127. psizes=$2
  128. for psize in ${psizes[@]}; do
  129. test_hash $1 $psize
  130. done
  131. }
  132. main () {
  133. ${OPENSSL} version
  134. validate_environment
  135. if [ "$1" = "-v" ]; then
  136. DO_VALGRIND=1
  137. VALGRIND=$VALGRIND_CMD
  138. echo "// OS_IK eip120 hash valgrind results - $DATESTAMP" > htest_$DATESTAMP.log
  139. fi
  140. hkey=$(<${HKEY})
  141. test_hmacs sha1 '0 3 56 64 128 192 256 320 1023'
  142. test_hmacs sha224 '0 3 56 64 128 192 256 320 1023'
  143. test_hmacs sha256 '0 3 56 64 128 192 256 320 1023'
  144. test_hmacs sha384 '0 3 56 128 256 384 512 640 1023'
  145. test_hmacs sha512 '0 3 56 128 256 384 512 640 1023'
  146. test_hmacs sha3-224 '0 3 56 144 288 432 576 720 1023'
  147. test_hmacs sha3-256 '0 3 56 136 272 408 544 680 1023'
  148. test_hmacs sha3-384 '0 3 56 104 208 312 416 520 1023'
  149. test_hmacs sha3-512 '0 3 56 72 144 216 288 360 1023'
  150. test_hmacs sm3 '0 3 56 64 128 256 384 512 640 1023'
  151. ckey=$(<${CKEY})
  152. test_cmacs aes-128-ecb '0 16 32 48 64 80 1023'
  153. test_cmacs sm4-ecb '0 16 32 48 64 80 1023'
  154. test_hashes sha1 '128 192 256 320 1023'
  155. test_hashes sha224 '128 192 256 320 1023'
  156. test_hashes sha256 '128 192 256 320 1023'
  157. test_hashes sha384 '256 384 512 640 1023'
  158. test_hashes sha512 '256 384 512 640 1023'
  159. test_hashes sha3-224 '288 432 576 720 1023'
  160. test_hashes sha3-256 '272 408 544 680 1023'
  161. test_hashes sha3-384 '208 312 416 520 1023'
  162. test_hashes sha3-512 '144 216 288 360 1023'
  163. test_hashes sm3 '128 256 384 512 640 1023'
  164. if [ $DO_VALGRIND ]; then
  165. rm $TEMPFILE
  166. fi
  167. unset DO_VALGRIND
  168. unset VALGRIND
  169. test_hashes sha1 '3 56 64'
  170. test_hashes sha224 '3 56 64'
  171. test_hashes sha256 '3 56 64'
  172. test_hashes sha384 '3 56 128'
  173. test_hashes sha512 '3 56 128'
  174. test_hashes sha3-224 '3 56 144'
  175. test_hashes sha3-256 '3 56 136'
  176. test_hashes sha3-384 '3 56 104'
  177. test_hashes sha3-512 '3 56 72'
  178. test_hashes sm3 '3 56 64'
  179. rm ${O1} ${O2}
  180. if [ ${result} -ne 0 ]; then
  181. echo -e "\n${RED}Hash tests FAILED${NC}"
  182. return
  183. fi
  184. echo -e "\n${GRN}Hash tests PASSED${NC}"
  185. }
  186. main "$@"