fips-check.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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, fips-ready, stm32l4-v2
  14. #
  15. # - keep: (default off) XXX-fips-test temp dir around for inspection
  16. #
  17. Usage() {
  18. cat <<usageText
  19. Usage: $0 [platform [keep]]
  20. Platform is one of:
  21. linux (default)
  22. ios
  23. android
  24. windows
  25. freertos
  26. openrtos-3.9.2
  27. linux-ecc
  28. netbsd-selftest
  29. sgx
  30. netos-7.6
  31. linuxv2 (FIPSv2, use for Win10)
  32. fips-ready
  33. stm32l4-v2 (FIPSv2, use for STM32L4)
  34. wolfrand
  35. Keep (default off) retains the XXX-fips-test temp dir for inspection.
  36. Example:
  37. $0 windows keep
  38. usageText
  39. }
  40. LINUX_FIPS_VERSION=v3.2.6
  41. LINUX_FIPS_REPO=git@github.com:wolfSSL/fips.git
  42. LINUX_CRYPT_VERSION=v3.2.6
  43. LINUX_CRYPT_REPO=git@github.com:cyassl/cyassl.git
  44. LINUX_ECC_FIPS_VERSION=v3.10.3
  45. LINUX_ECC_FIPS_REPO=git@github.com:wolfSSL/fips.git
  46. LINUX_ECC_CRYPT_VERSION=v3.2.6
  47. LINUX_ECC_CRYPT_REPO=git@github.com:cyassl/cyassl.git
  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.2b
  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. STM32L4_V2_FIPS_VERSION=WCv4.0.1-stable
  87. STM32L4_V2_FIPS_REPO=git@github.com:wolfSSL/fips.git
  88. STM32L4_V2_CRYPT_VERSION=WCv4.0.1-stable
  89. FIPS_SRCS=( fips.c fips_test.c )
  90. WC_MODS=( aes des3 sha sha256 sha512 rsa hmac random )
  91. TEST_DIR=XXX-fips-test
  92. CRYPT_INC_PATH=cyassl/ctaocrypt
  93. CRYPT_SRC_PATH=ctaocrypt/src
  94. RNG_VERSION=v3.6.0
  95. FIPS_OPTION=v1
  96. CAVP_SELFTEST_ONLY="no"
  97. GIT="git -c advice.detachedHead=false"
  98. if [ "x$1" == "x" ]; then PLATFORM="linux"; else PLATFORM=$1; fi
  99. if [ "x$2" == "xkeep" ]; then KEEP="yes"; else KEEP="no"; fi
  100. case $PLATFORM in
  101. ios)
  102. FIPS_VERSION=$IOS_FIPS_VERSION
  103. FIPS_REPO=$IOS_FIPS_REPO
  104. CRYPT_VERSION=$IOS_CRYPT_VERSION
  105. CRYPT_REPO=$IOS_CRYPT_REPO
  106. ;;
  107. android)
  108. FIPS_VERSION=$ANDROID_FIPS_VERSION
  109. FIPS_REPO=$ANDROID_FIPS_REPO
  110. CRYPT_VERSION=$ANDROID_CRYPT_VERSION
  111. CRYPT_REPO=$ANDROID_CRYPT_REPO
  112. ;;
  113. windows)
  114. FIPS_VERSION=$WINDOWS_FIPS_VERSION
  115. FIPS_REPO=$WINDOWS_FIPS_REPO
  116. CRYPT_VERSION=$WINDOWS_CRYPT_VERSION
  117. CRYPT_REPO=$WINDOWS_CRYPT_REPO
  118. ;;
  119. freertos)
  120. FIPS_VERSION=$FREERTOS_FIPS_VERSION
  121. FIPS_REPO=$FREERTOS_FIPS_REPO
  122. CRYPT_VERSION=$FREERTOS_CRYPT_VERSION
  123. CRYPT_REPO=$FREERTOS_CRYPT_REPO
  124. ;;
  125. openrtos-3.9.2)
  126. FIPS_VERSION=$OPENRTOS_3_9_2_FIPS_VERSION
  127. FIPS_REPO=$OPENRTOS_3_9_2_FIPS_REPO
  128. CRYPT_VERSION=$OPENRTOS_3_9_2_CRYPT_VERSION
  129. CRYPT_REPO=$OPENRTOS_3_9_2_CRYPT_REPO
  130. FIPS_CONFLICTS=( aes hmac random sha256 )
  131. ;;
  132. linux)
  133. FIPS_VERSION=$LINUX_FIPS_VERSION
  134. FIPS_REPO=$LINUX_FIPS_REPO
  135. CRYPT_VERSION=$LINUX_CRYPT_VERSION
  136. CRYPT_REPO=$LINUX_CRYPT_REPO
  137. ;;
  138. linux-ecc)
  139. FIPS_VERSION=$LINUX_ECC_FIPS_VERSION
  140. FIPS_REPO=$LINUX_ECC_FIPS_REPO
  141. CRYPT_VERSION=$LINUX_ECC_CRYPT_VERSION
  142. CRYPT_REPO=$LINUX_ECC_CRYPT_REPO
  143. ;;
  144. linuxv2)
  145. FIPS_VERSION=WCv4-stable
  146. FIPS_REPO=git@github.com:wolfssl/fips.git
  147. CRYPT_VERSION=WCv4-stable
  148. CRYPT_INC_PATH=wolfssl/wolfcrypt
  149. CRYPT_SRC_PATH=wolfcrypt/src
  150. WC_MODS+=( cmac dh ecc sha3 )
  151. RNG_VERSION=WCv4-rng-stable
  152. FIPS_SRCS+=( wolfcrypt_first.c wolfcrypt_last.c )
  153. FIPS_INCS=( fips.h )
  154. FIPS_OPTION=v2
  155. ;;
  156. netbsd-selftest)
  157. FIPS_VERSION=$NETBSD_FIPS_VERSION
  158. FIPS_REPO=$NETBSD_FIPS_REPO
  159. CRYPT_VERSION=$NETBSD_CRYPT_VERSION
  160. CRYPT_REPO=$NETBSD_CRYPT_REPO
  161. FIPS_SRCS=( selftest.c )
  162. WC_MODS=( dh ecc rsa dsa aes sha sha256 sha512 hmac random )
  163. CRYPT_INC_PATH=wolfssl/wolfcrypt
  164. CRYPT_SRC_PATH=wolfcrypt/src
  165. CAVP_SELFTEST_ONLY="yes"
  166. ;;
  167. sgx)
  168. FIPS_VERSION=$SGX_FIPS_VERSION
  169. FIPS_REPO=$SGX_FIPS_REPO
  170. CRYPT_VERSION=$SGX_CRYPT_VERSION
  171. CRYPT_REPO=$SGX_CRYPT_REPO
  172. ;;
  173. netos-7.6)
  174. FIPS_VERSION=$NETOS_7_6_FIPS_VERSION
  175. FIPS_REPO=$NETOS_7_6_FIPS_REPO
  176. CRYPT_VERSION=$NETOS_7_6_CRYPT_VERSION
  177. CRYPT_REPO=$NETOS_7_6_CRYPT_REPO
  178. ;;
  179. fips-ready)
  180. FIPS_REPO="git@github.com:wolfssl/fips.git"
  181. CRYPT_REPO="git@github.com:wolfssl/wolfssl.git"
  182. CRYPT_INC_PATH=wolfssl/wolfcrypt
  183. CRYPT_SRC_PATH=wolfcrypt/src
  184. FIPS_SRCS+=( wolfcrypt_first.c wolfcrypt_last.c )
  185. FIPS_INCS=( fips.h )
  186. FIPS_OPTION=ready
  187. ;;
  188. stm32l4-v2)
  189. FIPS_VERSION=$STM32L4_V2_FIPS_VERSION
  190. FIPS_REPO=$STM32L4_V2_FIPS_REPO
  191. CRYPT_VERSION=$STM32L4_V2_CRYPT_VERSION
  192. CRYPT_INC_PATH=wolfssl/wolfcrypt
  193. CRYPT_SRC_PATH=wolfcrypt/src
  194. # Replace the WC_MODS list for now. Do not want to copy over random.c yet.
  195. WC_MODS=( aes des3 sha sha256 sha512 rsa hmac )
  196. WC_MODS+=( cmac dh ecc )
  197. FIPS_SRCS+=( wolfcrypt_first.c wolfcrypt_last.c )
  198. FIPS_INCS=( fips.h )
  199. FIPS_OPTION=v2
  200. ;;
  201. wolfrand)
  202. FIPS_REPO=git@github.com:wolfssl/fips.git
  203. FIPS_VERSION=WRv4-stable
  204. CRYPT_REPO=git@github.com:wolfssl/wolfssl.git
  205. CRYPT_VERSION=WCv4-stable
  206. CRYPT_INC_PATH=wolfssl/wolfcrypt
  207. CRYPT_SRC_PATH=wolfcrypt/src
  208. RNG_VERSION=WCv4-rng-stable
  209. WC_MODS=( hmac sha256 random )
  210. FIPS_SRCS+=( wolfcrypt_first.c wolfcrypt_last.c )
  211. FIPS_INCS=( fips.h )
  212. FIPS_OPTION=rand
  213. ;;
  214. *)
  215. Usage
  216. exit 1
  217. esac
  218. if ! $GIT clone . $TEST_DIR; then
  219. echo "fips-check: Couldn't duplicate current working directory."
  220. exit 1
  221. fi
  222. pushd $TEST_DIR || exit 2
  223. if [ "x$FIPS_OPTION" == "xv1" ];
  224. then
  225. # make a clone of the last FIPS release tag
  226. if ! $GIT clone --depth 1 -b $CRYPT_VERSION $CRYPT_REPO old-tree; then
  227. echo "fips-check: Couldn't checkout the FIPS release."
  228. exit 1
  229. fi
  230. for MOD in "${WC_MODS[@]}"
  231. do
  232. cp "old-tree/$CRYPT_SRC_PATH/${MOD}.c" $CRYPT_SRC_PATH
  233. cp "old-tree/$CRYPT_INC_PATH/${MOD}.h" $CRYPT_INC_PATH
  234. done
  235. # We are using random.c from a separate release.
  236. # This is forcefully overwriting any other checkout of the cyassl sources.
  237. # Removing this as default behavior for SGX and netos projects.
  238. if [ "x$CAVP_SELFTEST_ONLY" == "xno" ] && [ "x$PLATFORM" != "xsgx" ] && \
  239. [ "x$PLATFORM" != "xnetos-7.6" ];
  240. then
  241. pushd old-tree || exit 2
  242. $GIT fetch origin $RNG_VERSION
  243. $GIT checkout FETCH_HEAD
  244. popd || exit 2
  245. cp "old-tree/$CRYPT_SRC_PATH/random.c" $CRYPT_SRC_PATH
  246. cp "old-tree/$CRYPT_INC_PATH/random.h" $CRYPT_INC_PATH
  247. fi
  248. elif [ "x$FIPS_OPTION" == "xv2" ] || [ "x$FIPS_OPTION" == "xrand" ]
  249. then
  250. $GIT branch --no-track "my$CRYPT_VERSION" $CRYPT_VERSION
  251. # Checkout the fips versions of the wolfCrypt files from the repo.
  252. for MOD in "${WC_MODS[@]}"
  253. do
  254. $GIT checkout "my$CRYPT_VERSION" -- "$CRYPT_SRC_PATH/$MOD.c" "$CRYPT_INC_PATH/$MOD.h"
  255. done
  256. $GIT branch --no-track "my$RNG_VERSION" $RNG_VERSION
  257. # Checkout the fips versions of the wolfCrypt files from the repo.
  258. $GIT checkout "my$RNG_VERSION" -- "$CRYPT_SRC_PATH/random.c" "$CRYPT_INC_PATH/random.h"
  259. elif [ "x$FIPS_OPTION" == "xready" ]
  260. then
  261. echo "Don't need to copy anything in particular for FIPS Ready."
  262. else
  263. echo "fips-check: Invalid FIPS option."
  264. exit 1
  265. fi
  266. # clone the FIPS repository
  267. if [ "x$FIPS_OPTION" != "xready" ]
  268. then
  269. if ! $GIT clone --depth 1 -b $FIPS_VERSION $FIPS_REPO fips; then
  270. echo "fips-check: Couldn't checkout the FIPS repository."
  271. exit 1
  272. fi
  273. else
  274. if ! $GIT clone --depth 1 $FIPS_REPO fips; then
  275. echo "fips-check: Couldn't checkout the FIPS repository."
  276. exit 1
  277. fi
  278. FIPS_OPTION="v2"
  279. fi
  280. for SRC in "${FIPS_SRCS[@]}"
  281. do
  282. cp "fips/$SRC" $CRYPT_SRC_PATH
  283. done
  284. for INC in "${FIPS_INCS[@]}"
  285. do
  286. cp "fips/$INC" $CRYPT_INC_PATH
  287. done
  288. # run the make test
  289. ./autogen.sh
  290. if [ "x$CAVP_SELFTEST_ONLY" == "xyes" ];
  291. then
  292. ./configure --enable-selftest
  293. else
  294. ./configure --enable-fips=$FIPS_OPTION
  295. fi
  296. if ! make; then
  297. echo "fips-check: Make failed. Debris left for analysis."
  298. exit 3
  299. fi
  300. if [ "x$CAVP_SELFTEST_ONLY" == "xno" ];
  301. then
  302. NEWHASH=$(./wolfcrypt/test/testwolfcrypt | sed -n 's/hash = \(.*\)/\1/p')
  303. if [ -n "$NEWHASH" ]; then
  304. sed -i.bak "s/^\".*\";/\"${NEWHASH}\";/" $CRYPT_SRC_PATH/fips_test.c
  305. make clean
  306. fi
  307. fi
  308. if ! make test; then
  309. echo "fips-check: Test failed. Debris left for analysis."
  310. exit 3
  311. fi
  312. if [ ${#FIPS_CONFLICTS[@]} -ne 0 ];
  313. then
  314. echo "Due to the way this package is compiled by the customer duplicate"
  315. echo "source file names are an issue, renaming:"
  316. for FNAME in "${FIPS_CONFLICTS[@]}"
  317. do
  318. echo "wolfcrypt/src/$FNAME.c to wolfcrypt/src/wc_$FNAME.c"
  319. mv "./wolfcrypt/src/$FNAME.c" "./wolfcrypt/src/wc_$FNAME.c"
  320. done
  321. echo "Confirming files were renamed..."
  322. ls -la ./wolfcrypt/src/wc_*.c
  323. fi
  324. # Clean up
  325. popd || exit 2
  326. if [ "x$KEEP" == "xno" ];
  327. then
  328. rm -rf $TEST_DIR
  329. fi