fips-check.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. marvell-linux-selftest
  30. sgx
  31. netos-7.6
  32. linuxv2 (FIPSv2, use for Win10)
  33. fips-ready
  34. stm32l4-v2 (FIPSv2, use for STM32L4)
  35. wolfrand
  36. solaris
  37. linuxv5-RC8 (old FIPS 140-3)
  38. linuxv5 (current FIPS 140-3)
  39. linuxv5-ready (ready FIPS 140-3)
  40. Keep (default off) retains the XXX-fips-test temp dir for inspection.
  41. Example:
  42. $0 windows keep
  43. usageText
  44. }
  45. MAKE=make
  46. LINUX_FIPS_VERSION=v3.2.6
  47. LINUX_FIPS_REPO=git@github.com:wolfSSL/fips.git
  48. LINUX_CRYPT_VERSION=v3.2.6
  49. LINUX_CRYPT_REPO=git@github.com:cyassl/cyassl.git
  50. LINUX_ECC_FIPS_VERSION=v3.10.3
  51. LINUX_ECC_FIPS_REPO=git@github.com:wolfSSL/fips.git
  52. LINUX_ECC_CRYPT_VERSION=v3.2.6
  53. LINUX_ECC_CRYPT_REPO=git@github.com:cyassl/cyassl.git
  54. IOS_FIPS_VERSION=v3.4.8a
  55. IOS_FIPS_REPO=git@github.com:wolfSSL/fips.git
  56. IOS_CRYPT_VERSION=v3.4.8.fips
  57. IOS_CRYPT_REPO=git@github.com:cyassl/cyassl.git
  58. ANDROID_FIPS_VERSION=v3.5.0
  59. ANDROID_FIPS_REPO=git@github.com:wolfSSL/fips.git
  60. ANDROID_CRYPT_VERSION=v3.5.0
  61. ANDROID_CRYPT_REPO=git@github.com:cyassl/cyassl.git
  62. WINDOWS_FIPS_VERSION=v3.6.6
  63. WINDOWS_FIPS_REPO=git@github.com:wolfSSL/fips.git
  64. WINDOWS_CRYPT_VERSION=v3.6.6
  65. WINDOWS_CRYPT_REPO=git@github.com:cyassl/cyassl.git
  66. FREERTOS_FIPS_VERSION=v3.6.1-FreeRTOS
  67. FREERTOS_FIPS_REPO=git@github.com:wolfSSL/fips.git
  68. FREERTOS_CRYPT_VERSION=v3.6.1
  69. FREERTOS_CRYPT_REPO=git@github.com:cyassl/cyassl.git
  70. OPENRTOS_3_9_2_FIPS_VERSION=v3.9.2-OpenRTOS
  71. OPENRTOS_3_9_2_FIPS_REPO=git@github.com:wolfSSL/fips.git
  72. OPENRTOS_3_9_2_CRYPT_VERSION=v3.6.1
  73. OPENRTOS_3_9_2_CRYPT_REPO=git@github.com:cyassl/cyassl.git
  74. #NOTE: Does not include the SGX examples yet, update version once fipsv2 is
  75. # finished and merge conflicts can be resolved. This will be tagged as
  76. # v3.12.4.sgx-examples
  77. #SGX_FIPS_VERSION=v3.12.4.sgx-examples
  78. SGX_FIPS_VERSION=v3.6.6
  79. SGX_FIPS_REPO=git@github.com:wolfSSL/fips.git
  80. SGX_CRYPT_VERSION=v3.12.4
  81. SGX_CRYPT_REPO=git@github.com:cyassl/cyassl.git
  82. NETOS_7_6_FIPS_VERSION=v3.12.6
  83. NETOS_7_6_FIPS_REPO=git@github.com:wolfSSL/fips.git
  84. NETOS_7_6_CRYPT_VERSION=v3.12.4
  85. NETOS_7_6_CRYPT_REPO=git@github.com:cyassl/cyassl.git
  86. # non-FIPS, CAVP only but pull in selftest
  87. # will reset above variables below in platform switch
  88. NETBSD_FIPS_VERSION=v3.14.2b
  89. NETBSD_FIPS_REPO=git@github.com:wolfssl/fips.git
  90. NETBSD_CRYPT_VERSION=v3.14.2
  91. NETBSD_CRYPT_REPO=git@github.com:wolfssl/wolfssl.git
  92. # non-FIPS, CAVP only but pull in selftest
  93. # will reset above variables below in platform switch
  94. MARVELL_LINUX_FIPS_VERSION=v3.14.2b
  95. MARVELL_LINUX_FIPS_REPO=git@github.com:wolfssl/fips.git
  96. MARVELL_LINUX_CRYPT_VERSION=v4.1.0-stable
  97. MARVELL_LINUX_CRYPT_REPO=git@github.com:wolfssl/wolfssl.git
  98. STM32L4_V2_FIPS_VERSION=WCv4.0.1-stable
  99. STM32L4_V2_FIPS_REPO=git@github.com:wolfSSL/fips.git
  100. STM32L4_V2_CRYPT_VERSION=WCv4.0.1-stable
  101. FIPS_SRCS=( fips.c fips_test.c )
  102. WC_MODS=( aes des3 sha sha256 sha512 rsa hmac random )
  103. TEST_DIR=XXX-fips-test
  104. CRYPT_INC_PATH=cyassl/ctaocrypt
  105. CRYPT_SRC_PATH=ctaocrypt/src
  106. RNG_VERSION=v3.6.0
  107. FIPS_OPTION=v1
  108. CAVP_SELFTEST_ONLY="no"
  109. GIT="git -c advice.detachedHead=false"
  110. if [ "x$1" == "x" ]; then PLATFORM="linux"; else PLATFORM=$1; fi
  111. if [ "x$2" == "xkeep" ]; then KEEP="yes"; else KEEP="no"; fi
  112. case $PLATFORM in
  113. ios)
  114. FIPS_VERSION=$IOS_FIPS_VERSION
  115. FIPS_REPO=$IOS_FIPS_REPO
  116. CRYPT_VERSION=$IOS_CRYPT_VERSION
  117. CRYPT_REPO=$IOS_CRYPT_REPO
  118. ;;
  119. android)
  120. FIPS_VERSION=$ANDROID_FIPS_VERSION
  121. FIPS_REPO=$ANDROID_FIPS_REPO
  122. CRYPT_VERSION=$ANDROID_CRYPT_VERSION
  123. CRYPT_REPO=$ANDROID_CRYPT_REPO
  124. ;;
  125. windows)
  126. FIPS_VERSION=$WINDOWS_FIPS_VERSION
  127. FIPS_REPO=$WINDOWS_FIPS_REPO
  128. CRYPT_VERSION=$WINDOWS_CRYPT_VERSION
  129. CRYPT_REPO=$WINDOWS_CRYPT_REPO
  130. ;;
  131. freertos)
  132. FIPS_VERSION=$FREERTOS_FIPS_VERSION
  133. FIPS_REPO=$FREERTOS_FIPS_REPO
  134. CRYPT_VERSION=$FREERTOS_CRYPT_VERSION
  135. CRYPT_REPO=$FREERTOS_CRYPT_REPO
  136. ;;
  137. openrtos-3.9.2)
  138. FIPS_VERSION=$OPENRTOS_3_9_2_FIPS_VERSION
  139. FIPS_REPO=$OPENRTOS_3_9_2_FIPS_REPO
  140. CRYPT_VERSION=$OPENRTOS_3_9_2_CRYPT_VERSION
  141. CRYPT_REPO=$OPENRTOS_3_9_2_CRYPT_REPO
  142. FIPS_CONFLICTS=( aes hmac random sha256 )
  143. ;;
  144. linux)
  145. FIPS_VERSION=$LINUX_FIPS_VERSION
  146. FIPS_REPO=$LINUX_FIPS_REPO
  147. CRYPT_VERSION=$LINUX_CRYPT_VERSION
  148. CRYPT_REPO=$LINUX_CRYPT_REPO
  149. ;;
  150. linux-ecc)
  151. FIPS_VERSION=$LINUX_ECC_FIPS_VERSION
  152. FIPS_REPO=$LINUX_ECC_FIPS_REPO
  153. CRYPT_VERSION=$LINUX_ECC_CRYPT_VERSION
  154. CRYPT_REPO=$LINUX_ECC_CRYPT_REPO
  155. ;;
  156. linuxv2)
  157. FIPS_VERSION=WCv4-stable
  158. FIPS_REPO=git@github.com:wolfssl/fips.git
  159. CRYPT_VERSION=WCv4-stable
  160. CRYPT_INC_PATH=wolfssl/wolfcrypt
  161. CRYPT_SRC_PATH=wolfcrypt/src
  162. WC_MODS+=( cmac dh ecc sha3 )
  163. RNG_VERSION=WCv4-rng-stable
  164. FIPS_SRCS+=( wolfcrypt_first.c wolfcrypt_last.c )
  165. FIPS_INCS=( fips.h )
  166. FIPS_OPTION=v2
  167. ;;
  168. netbsd-selftest)
  169. FIPS_VERSION=$NETBSD_FIPS_VERSION
  170. FIPS_REPO=$NETBSD_FIPS_REPO
  171. CRYPT_VERSION=$NETBSD_CRYPT_VERSION
  172. CRYPT_REPO=$NETBSD_CRYPT_REPO
  173. FIPS_SRCS=( selftest.c )
  174. WC_MODS=( dh ecc rsa dsa aes sha sha256 sha512 hmac random )
  175. CRYPT_INC_PATH=wolfssl/wolfcrypt
  176. CRYPT_SRC_PATH=wolfcrypt/src
  177. CAVP_SELFTEST_ONLY="yes"
  178. ;;
  179. marvell-linux-selftest)
  180. FIPS_VERSION=$MARVELL_LINUX_FIPS_VERSION
  181. FIPS_REPO=$MARVELL_LINUX_FIPS_REPO
  182. CRYPT_VERSION=$MARVELL_LINUX_CRYPT_VERSION
  183. CRYPT_REPO=$MARVELL_LINUX_CRYPT_REPO
  184. FIPS_SRCS=( selftest.c )
  185. WC_MODS=( dh ecc rsa dsa aes sha sha256 sha512 hmac random )
  186. CRYPT_INC_PATH=wolfssl/wolfcrypt
  187. CRYPT_SRC_PATH=wolfcrypt/src
  188. CAVP_SELFTEST_ONLY="yes"
  189. CAVP_SELFTEST_OPTION=v2
  190. ;;
  191. sgx)
  192. FIPS_VERSION=$SGX_FIPS_VERSION
  193. FIPS_REPO=$SGX_FIPS_REPO
  194. CRYPT_VERSION=$SGX_CRYPT_VERSION
  195. CRYPT_REPO=$SGX_CRYPT_REPO
  196. ;;
  197. netos-7.6)
  198. FIPS_VERSION=$NETOS_7_6_FIPS_VERSION
  199. FIPS_REPO=$NETOS_7_6_FIPS_REPO
  200. CRYPT_VERSION=$NETOS_7_6_CRYPT_VERSION
  201. CRYPT_REPO=$NETOS_7_6_CRYPT_REPO
  202. ;;
  203. fips-v3-ready)
  204. FIPS_REPO="git@github.com:wolfssl/fips.git"
  205. FIPS_VERSION="v4.1.1"
  206. CRYPT_INC_PATH=wolfssl/wolfcrypt
  207. CRYPT_SRC_PATH=wolfcrypt/src
  208. FIPS_SRCS+=( wolfcrypt_first.c wolfcrypt_last.c )
  209. FIPS_INCS=( fips.h )
  210. FIPS_OPTION=v3-ready
  211. ;;
  212. fips-ready|fips-v5-ready|linuxv5-ready)
  213. FIPS_REPO="git@github.com:wolfSSL/fips.git"
  214. FIPS_VERSION="master"
  215. CRYPT_INC_PATH=wolfssl/wolfcrypt
  216. CRYPT_SRC_PATH=wolfcrypt/src
  217. FIPS_SRCS+=( wolfcrypt_first.c wolfcrypt_last.c )
  218. FIPS_INCS=( fips.h )
  219. FIPS_OPTION=v5-ready
  220. ;;
  221. stm32l4-v2)
  222. FIPS_VERSION=$STM32L4_V2_FIPS_VERSION
  223. FIPS_REPO=$STM32L4_V2_FIPS_REPO
  224. CRYPT_VERSION=$STM32L4_V2_CRYPT_VERSION
  225. CRYPT_INC_PATH=wolfssl/wolfcrypt
  226. CRYPT_SRC_PATH=wolfcrypt/src
  227. # Replace the WC_MODS list for now. Do not want to copy over random.c yet.
  228. WC_MODS=( aes des3 sha sha256 sha512 rsa hmac )
  229. WC_MODS+=( cmac dh ecc )
  230. FIPS_SRCS+=( wolfcrypt_first.c wolfcrypt_last.c )
  231. FIPS_INCS=( fips.h )
  232. FIPS_OPTION=v2
  233. ;;
  234. wolfrand)
  235. FIPS_REPO=git@github.com:wolfssl/fips.git
  236. FIPS_VERSION=WRv4-stable
  237. CRYPT_REPO=git@github.com:wolfssl/wolfssl.git
  238. CRYPT_VERSION=WCv4-stable
  239. CRYPT_INC_PATH=wolfssl/wolfcrypt
  240. CRYPT_SRC_PATH=wolfcrypt/src
  241. RNG_VERSION=WCv4-rng-stable
  242. WC_MODS=( hmac sha256 random )
  243. FIPS_SRCS+=( wolfcrypt_first.c wolfcrypt_last.c )
  244. FIPS_INCS=( fips.h )
  245. FIPS_OPTION=rand
  246. ;;
  247. solaris)
  248. FIPS_VERSION=WCv4-stable
  249. FIPS_REPO=git@github.com:wolfssl/fips.git
  250. CRYPT_VERSION=WCv4-stable
  251. CRYPT_INC_PATH=wolfssl/wolfcrypt
  252. CRYPT_SRC_PATH=wolfcrypt/src
  253. WC_MODS+=( cmac dh ecc sha3 )
  254. RNG_VERSION=WCv4-rng-stable
  255. FIPS_SRCS+=( wolfcrypt_first.c wolfcrypt_last.c )
  256. FIPS_INCS=( fips.h )
  257. FIPS_OPTION=v2
  258. MAKE=gmake
  259. ;;
  260. linuxv5-RC8)
  261. FIPS_REPO="git@github.com:wolfSSL/fips.git"
  262. FIPS_VERSION="WCv5.0-RC8"
  263. CRYPT_REPO="git@github.com:wolfSSL/wolfssl.git"
  264. CRYPT_VERSION="WCv5.0-RC8"
  265. CRYPT_INC_PATH="wolfssl/wolfcrypt"
  266. CRYPT_SRC_PATH="wolfcrypt/src"
  267. WC_MODS=( aes sha sha256 sha512 rsa hmac random cmac dh ecc sha3 kdf )
  268. RNG_VERSION="WCv5.0-RC8"
  269. FIPS_SRCS=( fips.c fips_test.c wolfcrypt_first.c wolfcrypt_last.c )
  270. FIPS_INCS=( fips.h )
  271. FIPS_OPTION="v5-RC8"
  272. COPY_DIRECT=( wolfcrypt/src/aes_asm.S wolfcrypt/src/aes_asm.asm
  273. wolfcrypt/src/sha256_asm.S wolfcrypt/src/sha512_asm.S )
  274. ;;
  275. linuxv5|linuxv5-RC9)
  276. FIPS_REPO="git@github.com:wolfSSL/fips.git"
  277. FIPS_VERSION="WCv5.0-RC9"
  278. CRYPT_REPO="git@github.com:wolfSSL/wolfssl.git"
  279. CRYPT_VERSION="WCv5.0-RC9"
  280. CRYPT_INC_PATH="wolfssl/wolfcrypt"
  281. CRYPT_SRC_PATH="wolfcrypt/src"
  282. WC_MODS=( aes sha sha256 sha512 rsa hmac random cmac dh ecc sha3 kdf )
  283. RNG_VERSION="WCv5.0-RC9"
  284. FIPS_SRCS=( fips.c fips_test.c wolfcrypt_first.c wolfcrypt_last.c )
  285. FIPS_INCS=( fips.h )
  286. FIPS_OPTION="v5-RC9"
  287. COPY_DIRECT=( wolfcrypt/src/aes_asm.S wolfcrypt/src/aes_asm.asm
  288. wolfcrypt/src/sha256_asm.S wolfcrypt/src/sha512_asm.S )
  289. ;;
  290. *)
  291. Usage
  292. exit 1
  293. esac
  294. if ! $GIT clone . $TEST_DIR; then
  295. echo "fips-check: Couldn't duplicate current working directory."
  296. exit 1
  297. fi
  298. pushd $TEST_DIR || exit 2
  299. case "$FIPS_OPTION" in
  300. v1)
  301. # make a clone of the last FIPS release tag
  302. if ! $GIT clone --depth 1 -b $CRYPT_VERSION $CRYPT_REPO old-tree; then
  303. echo "fips-check: Couldn't checkout the FIPS release."
  304. exit 1
  305. fi
  306. for MOD in "${WC_MODS[@]}"
  307. do
  308. cp "old-tree/$CRYPT_SRC_PATH/${MOD}.c" $CRYPT_SRC_PATH
  309. cp "old-tree/$CRYPT_INC_PATH/${MOD}.h" $CRYPT_INC_PATH
  310. done
  311. # We are using random.c from a separate release.
  312. # This is forcefully overwriting any other checkout of the cyassl sources.
  313. # Removing this as default behavior for SGX and netos projects.
  314. if [ "x$CAVP_SELFTEST_ONLY" == "xno" ] && [ "x$PLATFORM" != "xsgx" ] && \
  315. [ "x$PLATFORM" != "xnetos-7.6" ];
  316. then
  317. pushd old-tree || exit 2
  318. $GIT fetch origin $RNG_VERSION || exit $?
  319. $GIT checkout FETCH_HEAD || exit $?
  320. popd || exit 2
  321. cp "old-tree/$CRYPT_SRC_PATH/random.c" $CRYPT_SRC_PATH
  322. cp "old-tree/$CRYPT_INC_PATH/random.h" $CRYPT_INC_PATH
  323. fi
  324. ;;
  325. v2|rand|v5-RC8|v5-RC9)
  326. $GIT branch --no-track "my$CRYPT_VERSION" $CRYPT_VERSION || exit $?
  327. # Checkout the fips versions of the wolfCrypt files from the repo.
  328. for MOD in "${WC_MODS[@]}"
  329. do
  330. $GIT checkout "my$CRYPT_VERSION" -- "$CRYPT_SRC_PATH/$MOD.c" "$CRYPT_INC_PATH/$MOD.h" || exit $?
  331. done
  332. for MOD in "${COPY_DIRECT[@]}"
  333. do
  334. $GIT checkout "my$CRYPT_VERSION" -- "$MOD" || exit $?
  335. done
  336. $GIT branch --no-track "myrng$RNG_VERSION" $RNG_VERSION || exit $?
  337. # Checkout the fips versions of the wolfCrypt files from the repo.
  338. $GIT checkout "myrng$RNG_VERSION" -- "$CRYPT_SRC_PATH/random.c" "$CRYPT_INC_PATH/random.h" || exit $?
  339. ;;
  340. *ready*)
  341. echo "Don't need to copy anything in particular for FIPS Ready."
  342. ;;
  343. *)
  344. echo "fips-check: Invalid FIPS option \"${FIPS_OPTION}\"."
  345. exit 1
  346. ;;
  347. esac
  348. # clone the FIPS repository
  349. if [ "x$FIPS_OPTION" = "xready" ]
  350. then
  351. if ! $GIT clone --depth 1 $FIPS_REPO fips; then
  352. echo "fips-check: Couldn't checkout the FIPS repository for FIPS Ready."
  353. exit 1
  354. fi
  355. else
  356. if ! $GIT clone --depth 1 -b $FIPS_VERSION $FIPS_REPO fips; then
  357. echo "fips-check: Couldn't checkout the FIPS repository."
  358. exit 1
  359. fi
  360. fi
  361. for SRC in "${FIPS_SRCS[@]}"
  362. do
  363. cp "fips/$SRC" $CRYPT_SRC_PATH
  364. done
  365. for INC in "${FIPS_INCS[@]}"
  366. do
  367. cp "fips/$INC" $CRYPT_INC_PATH
  368. done
  369. # run the make test
  370. ./autogen.sh
  371. if [ "x$CAVP_SELFTEST_ONLY" == "xyes" ];
  372. then
  373. if [ "x$CAVP_SELFTEST_OPTION" == "xv2" ]
  374. then
  375. ./configure --enable-selftest=v2
  376. else
  377. ./configure --enable-selftest
  378. fi
  379. else
  380. ./configure --enable-fips=$FIPS_OPTION
  381. fi
  382. if ! $MAKE; then
  383. echo "fips-check: Make failed. Debris left for analysis."
  384. exit 3
  385. fi
  386. if [ "x$CAVP_SELFTEST_ONLY" == "xno" ];
  387. then
  388. NEWHASH=$(./wolfcrypt/test/testwolfcrypt | sed -n 's/hash = \(.*\)/\1/p')
  389. if [ -n "$NEWHASH" ]; then
  390. cp $CRYPT_SRC_PATH/fips_test.c $CRYPT_SRC_PATH/fips_test.c.bak
  391. sed "s/^\".*\";/\"${NEWHASH}\";/" $CRYPT_SRC_PATH/fips_test.c.bak >$CRYPT_SRC_PATH/fips_test.c
  392. make clean
  393. fi
  394. fi
  395. if ! $MAKE test; then
  396. echo "fips-check: Test failed. Debris left for analysis."
  397. exit 3
  398. fi
  399. if [ ${#FIPS_CONFLICTS[@]} -ne 0 ];
  400. then
  401. echo "Due to the way this package is compiled by the customer duplicate"
  402. echo "source file names are an issue, renaming:"
  403. for FNAME in "${FIPS_CONFLICTS[@]}"
  404. do
  405. echo "wolfcrypt/src/$FNAME.c to wolfcrypt/src/wc_$FNAME.c"
  406. mv "./wolfcrypt/src/$FNAME.c" "./wolfcrypt/src/wc_$FNAME.c"
  407. done
  408. echo "Confirming files were renamed..."
  409. ls -la ./wolfcrypt/src/wc_*.c
  410. fi
  411. # Clean up
  412. popd || exit 2
  413. if [ "x$KEEP" == "xno" ];
  414. then
  415. rm -rf $TEST_DIR
  416. fi