2
0

fips-check.sh 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #!/bin/bash
  2. # fips-check.sh
  3. # This script checks the current revision of the code against the
  4. # previous release of the FIPS code. While wolfSSL and wolfCrypt
  5. # may be advancing, they must work correctly with the last tested
  6. # copy of our FIPS approved code.
  7. #
  8. # This should check out all the approved versions. The command line
  9. # option selects the version.
  10. #
  11. # $ ./fips-check [version] [keep]
  12. #
  13. # - version: linux (default), ios, android, windows, freertos, linux-ecc, netbsd-selftest, linuxv2
  14. #
  15. # - keep: (default off) XXX-fips-test temp dir around for inspection
  16. #
  17. function Usage() {
  18. printf '\n%s\n' "Usage: $0 [platform] [keep]"
  19. printf '%s\n\n' "Where \"platform\" is one of:"
  20. printf '\t%s\n' "linux (default)"
  21. printf '\t%s\n' "ios"
  22. printf '\t%s\n' "android"
  23. printf '\t%s\n' "windows"
  24. printf '\t%s\n' "freertos"
  25. printf '\t%s\n' "openrtos-3.9.2"
  26. printf '\t%s\n' "linux-ecc"
  27. printf '\t%s\n' "netbsd-selftest"
  28. printf '\t%s\n' "sgx"
  29. printf '\t%s\n' "netos-7.6"
  30. printf '\t%s\n' "linuxv2"
  31. printf '\n%s\n\n' "Where \"keep\" means keep (default off) XXX-fips-test temp dir around for inspection"
  32. printf '%s\n' "EXAMPLE:"
  33. printf '%s\n' "---------------------------------"
  34. printf '%s\n' "./fips-check.sh windows keep"
  35. printf '%s\n\n' "---------------------------------"
  36. }
  37. LINUX_FIPS_VERSION=v3.2.6
  38. LINUX_FIPS_REPO=git@github.com:wolfSSL/fips.git
  39. LINUX_CRYPT_VERSION=v3.2.6
  40. LINUX_CRYPT_REPO=git@github.com:cyassl/cyassl.git
  41. LINUX_ECC_FIPS_VERSION=v3.10.3
  42. LINUX_ECC_FIPS_REPO=git@github.com:wolfSSL/fips.git
  43. LINUX_ECC_CRYPT_VERSION=v3.2.6
  44. LINUX_ECC_CRYPT_REPO=git@github.com:cyassl/cyassl.git
  45. LINUXV2_FIPS_VERSION=fipsv2
  46. LINUXV2_FIPS_REPO=git@github.com:ejohnstown/fips.git
  47. LINUXV2_CRYPT_VERSION=fipsv2
  48. IOS_FIPS_VERSION=v3.4.8a
  49. IOS_FIPS_REPO=git@github.com:wolfSSL/fips.git
  50. IOS_CRYPT_VERSION=v3.4.8.fips
  51. IOS_CRYPT_REPO=git@github.com:cyassl/cyassl.git
  52. ANDROID_FIPS_VERSION=v3.5.0
  53. ANDROID_FIPS_REPO=git@github.com:wolfSSL/fips.git
  54. ANDROID_CRYPT_VERSION=v3.5.0
  55. ANDROID_CRYPT_REPO=git@github.com:cyassl/cyassl.git
  56. WINDOWS_FIPS_VERSION=v3.6.6
  57. WINDOWS_FIPS_REPO=git@github.com:wolfSSL/fips.git
  58. WINDOWS_CRYPT_VERSION=v3.6.6
  59. WINDOWS_CRYPT_REPO=git@github.com:cyassl/cyassl.git
  60. FREERTOS_FIPS_VERSION=v3.6.1-FreeRTOS
  61. FREERTOS_FIPS_REPO=git@github.com:wolfSSL/fips.git
  62. FREERTOS_CRYPT_VERSION=v3.6.1
  63. FREERTOS_CRYPT_REPO=git@github.com:cyassl/cyassl.git
  64. OPENRTOS_3_9_2_FIPS_VERSION=v3.9.2-OpenRTOS
  65. OPENRTOS_3_9_2_FIPS_REPO=git@github.com:wolfSSL/fips.git
  66. OPENRTOS_3_9_2_CRYPT_VERSION=v3.6.1
  67. OPENRTOS_3_9_2_CRYPT_REPO=git@github.com:cyassl/cyassl.git
  68. #NOTE: Does not include the SGX examples yet, update version once fipsv2 is
  69. # finished and merge conflicts can be resolved. This will be tagged as
  70. # v3.12.4.sgx-examples
  71. #SGX_FIPS_VERSION=v3.12.4.sgx-examples
  72. SGX_FIPS_VERSION=v3.6.6
  73. SGX_FIPS_REPO=git@github.com:wolfSSL/fips.git
  74. SGX_CRYPT_VERSION=v3.12.4
  75. SGX_CRYPT_REPO=git@github.com:cyassl/cyassl.git
  76. NETOS_7_6_FIPS_VERSION=v3.12.6
  77. NETOS_7_6_FIPS_REPO=git@github.com:wolfSSL/fips.git
  78. NETOS_7_6_CRYPT_VERSION=v3.12.4
  79. NETOS_7_6_CRYPT_REPO=git@github.com:cyassl/cyassl.git
  80. # non-FIPS, CAVP only but pull in selftest
  81. # will reset above variables below in platform switch
  82. NETBSD_FIPS_VERSION=v3.14.2a
  83. NETBSD_FIPS_REPO=git@github.com:wolfssl/fips.git
  84. NETBSD_CRYPT_VERSION=v3.14.2
  85. NETBSD_CRYPT_REPO=git@github.com:wolfssl/wolfssl.git
  86. FIPS_SRCS=( fips.c fips_test.c )
  87. WC_MODS=( aes des3 sha sha256 sha512 rsa hmac random )
  88. TEST_DIR=XXX-fips-test
  89. CRYPT_INC_PATH=cyassl/ctaocrypt
  90. CRYPT_SRC_PATH=ctaocrypt/src
  91. FIPS_OPTION=v1
  92. CAVP_SELFTEST_ONLY="no"
  93. if [ "x$1" == "x" ]; then PLATFORM="linux"; else PLATFORM=$1; fi
  94. if [ "x$2" == "xkeep" ]; then KEEP="yes"; else KEEP="no"; fi
  95. case $PLATFORM in
  96. ios)
  97. FIPS_VERSION=$IOS_FIPS_VERSION
  98. FIPS_REPO=$IOS_FIPS_REPO
  99. CRYPT_VERSION=$IOS_CRYPT_VERSION
  100. CRYPT_REPO=$IOS_CRYPT_REPO
  101. ;;
  102. android)
  103. FIPS_VERSION=$ANDROID_FIPS_VERSION
  104. FIPS_REPO=$ANDROID_FIPS_REPO
  105. CRYPT_VERSION=$ANDROID_CRYPT_VERSION
  106. CRYPT_REPO=$ANDROID_CRYPT_REPO
  107. ;;
  108. windows)
  109. FIPS_VERSION=$WINDOWS_FIPS_VERSION
  110. FIPS_REPO=$WINDOWS_FIPS_REPO
  111. CRYPT_VERSION=$WINDOWS_CRYPT_VERSION
  112. CRYPT_REPO=$WINDOWS_CRYPT_REPO
  113. ;;
  114. freertos)
  115. FIPS_VERSION=$FREERTOS_FIPS_VERSION
  116. FIPS_REPO=$FREERTOS_FIPS_REPO
  117. CRYPT_VERSION=$FREERTOS_CRYPT_VERSION
  118. CRYPT_REPO=$FREERTOS_CRYPT_REPO
  119. ;;
  120. openrtos-3.9.2)
  121. FIPS_VERSION=$OPENRTOS_3_9_2_FIPS_VERSION
  122. FIPS_REPO=$OPENRTOS_3_9_2_FIPS_REPO
  123. CRYPT_VERSION=$OPENRTOS_3_9_2_CRYPT_VERSION
  124. CRYPT_REPO=$OPENRTOS_3_9_2_CRYPT_REPO
  125. FIPS_CONFLICTS=( aes hmac random sha256 )
  126. ;;
  127. linux)
  128. FIPS_VERSION=$LINUX_FIPS_VERSION
  129. FIPS_REPO=$LINUX_FIPS_REPO
  130. CRYPT_VERSION=$LINUX_CRYPT_VERSION
  131. CRYPT_REPO=$LINUX_CRYPT_REPO
  132. ;;
  133. linux-ecc)
  134. FIPS_VERSION=$LINUX_ECC_FIPS_VERSION
  135. FIPS_REPO=$LINUX_ECC_FIPS_REPO
  136. CRYPT_VERSION=$LINUX_ECC_CRYPT_VERSION
  137. CRYPT_REPO=$LINUX_ECC_CRYPT_REPO
  138. ;;
  139. linuxv2)
  140. FIPS_VERSION=$LINUXV2_FIPS_VERSION
  141. FIPS_REPO=$LINUXV2_FIPS_REPO
  142. CRYPT_VERSION=$LINUXV2_CRYPT_VERSION
  143. CRYPT_INC_PATH=wolfssl/wolfcrypt
  144. CRYPT_SRC_PATH=wolfcrypt/src
  145. WC_MODS+=( cmac dh )
  146. FIPS_SRCS+=( wolfcrypt_first.c wolfcrypt_last.c )
  147. FIPS_INCS=( fips.h )
  148. FIPS_OPTION=v2
  149. ;;
  150. netbsd-selftest)
  151. FIPS_VERSION=$NETBSD_FIPS_VERSION
  152. FIPS_REPO=$NETBSD_FIPS_REPO
  153. CRYPT_VERSION=$NETBSD_CRYPT_VERSION
  154. CRYPT_REPO=$NETBSD_CRYPT_REPO
  155. FIPS_SRCS=( selftest.c )
  156. WC_MODS=( dh ecc rsa dsa aes sha sha256 sha512 hmac random )
  157. CRYPT_INC_PATH=wolfssl/wolfcrypt
  158. CRYPT_SRC_PATH=wolfcrypt/src
  159. CAVP_SELFTEST_ONLY="yes"
  160. ;;
  161. sgx)
  162. FIPS_VERSION=$SGX_FIPS_VERSION
  163. FIPS_REPO=$SGX_FIPS_REPO
  164. CRYPT_VERSION=$SGX_CRYPT_VERSION
  165. CRYPT_REPO=$SGX_CRYPT_REPO
  166. ;;
  167. netos-7.6)
  168. FIPS_VERSION=$NETOS_7_6_FIPS_VERSION
  169. FIPS_REPO=$NETOS_7_6_FIPS_REPO
  170. CRYPT_VERSION=$NETOS_7_6_CRYPT_VERSION
  171. CRYPT_REPO=$NETOS_7_6_CRYPT_REPO
  172. ;;
  173. *)
  174. Usage
  175. exit 1
  176. esac
  177. git clone . $TEST_DIR
  178. [ $? -ne 0 ] && echo "\n\nCouldn't duplicate current working directory.\n\n" && exit 1
  179. pushd $TEST_DIR
  180. if [ "x$FIPS_OPTION" == "xv1" ];
  181. then
  182. # make a clone of the last FIPS release tag
  183. git clone -b $CRYPT_VERSION $CRYPT_REPO old-tree
  184. [ $? -ne 0 ] && echo "\n\nCouldn't checkout the FIPS release.\n\n" && exit 1
  185. for MOD in ${WC_MODS[@]}
  186. do
  187. cp old-tree/$CRYPT_SRC_PATH/${MOD}.c $CRYPT_SRC_PATH
  188. cp old-tree/$CRYPT_INC_PATH/${MOD}.h $CRYPT_INC_PATH
  189. done
  190. # The following is temporary. We are using random.c from a separate release
  191. # This is forcefully overwriting any other checkout of the cyassl sources.
  192. # Removing this as default behavior for SGX and netos projects.
  193. if [ "x$CAVP_SELFTEST_ONLY" == "xno" ] && [ "x$PLATFORM" != "xsgx" ] && \
  194. [ "x$PLATFORM" != "xnetos-7.6" ];
  195. then
  196. pushd old-tree
  197. git checkout v3.6.0
  198. popd
  199. cp old-tree/$CRYPT_SRC_PATH/random.c $CRYPT_SRC_PATH
  200. cp old-tree/$CRYPT_INC_PATH/random.h $CRYPT_INC_PATH
  201. fi
  202. else
  203. git branch --track $CRYPT_VERSION origin/$CRYPT_VERSION
  204. # Checkout the fips versions of the wolfCrypt files from the repo.
  205. for MOD in ${WC_MODS[@]}
  206. do
  207. git checkout $CRYPT_VERSION -- $CRYPT_SRC_PATH/$MOD.c $CRYPT_INC_PATH/$MOD.h
  208. done
  209. fi
  210. # clone the FIPS repository
  211. git clone -b $FIPS_VERSION $FIPS_REPO fips
  212. [ $? -ne 0 ] && echo "\n\nCouldn't checkout the FIPS repository.\n\n" && exit 1
  213. for SRC in ${FIPS_SRCS[@]}
  214. do
  215. cp fips/$SRC $CRYPT_SRC_PATH
  216. done
  217. for INC in ${FIPS_INCS[@]}
  218. do
  219. cp fips/$INC $CRYPT_INC_PATH
  220. done
  221. # run the make test
  222. ./autogen.sh
  223. if [ "x$CAVP_SELFTEST_ONLY" == "xyes" ];
  224. then
  225. ./configure --enable-selftest
  226. else
  227. ./configure --enable-fips=$FIPS_OPTION
  228. fi
  229. make
  230. [ $? -ne 0 ] && echo "\n\nMake failed. Debris left for analysis." && exit 1
  231. if [ "x$CAVP_SELFTEST_ONLY" == "xno" ];
  232. then
  233. NEWHASH=`./wolfcrypt/test/testwolfcrypt | sed -n 's/hash = \(.*\)/\1/p'`
  234. if [ -n "$NEWHASH" ]; then
  235. sed -i.bak "s/^\".*\";/\"${NEWHASH}\";/" $CRYPT_SRC_PATH/fips_test.c
  236. make clean
  237. fi
  238. fi
  239. make test
  240. [ $? -ne 0 ] && echo "\n\nTest failed. Debris left for analysis." && exit 1
  241. if [ ${#FIPS_CONFLICTS[@]} -ne 0 ];
  242. then
  243. echo "Due to the way this package is compiled by the customer duplicate"
  244. echo "source file names are an issue, renaming:"
  245. for FNAME in ${FIPS_CONFLICTS[@]}
  246. do
  247. echo "wolfcrypt/src/$FNAME.c to wolfcrypt/src/wc_$FNAME.c"
  248. mv ./wolfcrypt/src/$FNAME.c ./wolfcrypt/src/wc_$FNAME.c
  249. done
  250. echo "Confirming files were renamed..."
  251. ls -la ./wolfcrypt/src/wc_*.c
  252. fi
  253. # Clean up
  254. popd
  255. if [ "x$KEEP" == "xno" ];
  256. then
  257. rm -rf $TEST_DIR
  258. fi