1
0

fips-check.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 flavors. The command line
  9. # option selects the flavor. The keep option keeps the output
  10. # directory.
  11. # These variables may be overridden on the command line.
  12. MAKE="${MAKE:-make}"
  13. GIT="${GIT:-git -c advice.detachedHead=false}"
  14. TEST_DIR="${TEST_DIR:-XXX-fips-test}"
  15. FLAVOR="${FLAVOR:-linux}"
  16. KEEP="${KEEP:-no}"
  17. FIPS_REPO="${FIPS_REPO:-git@github.com:wolfssl/fips.git}"
  18. Usage() {
  19. cat <<usageText
  20. Usage: $0 [flavor] [keep]
  21. Flavor is one of:
  22. linuxv2 (FIPSv2, use for Win10)
  23. fipsv2-OE-ready (ready FIPSv2)
  24. solaris
  25. netbsd-selftest
  26. marvell-linux-selftest
  27. linuxv5 (current FIPS 140-3)
  28. fips-ready (ready FIPS 140-3)
  29. fips-dev (dev FIPS 140-3)
  30. wolfrand
  31. Keep (default off) retains the temp dir $TEST_DIR for inspection.
  32. Example:
  33. $0 windows keep
  34. usageText
  35. }
  36. while [ "$1" ]; do
  37. if [ "$1" = 'keep' ]; then KEEP='yes'; else FLAVOR="$1"; fi
  38. shift
  39. done
  40. case "$FLAVOR" in
  41. linuxv2|fipsv2-OE-ready|solaris)
  42. FIPS_OPTION='v2'
  43. FIPS_FILES=(
  44. 'wolfcrypt/src/fips.c:WCv4-stable'
  45. 'wolfcrypt/src/fips_test.c:WCv4-stable'
  46. 'wolfcrypt/src/wolfcrypt_first.c:WCv4-stable'
  47. 'wolfcrypt/src/wolfcrypt_last.c:WCv4-stable'
  48. 'wolfssl/wolfcrypt/fips.h:WCv4-stable'
  49. )
  50. WOLFCRYPT_FILES=(
  51. 'wolfcrypt/src/aes.c:WCv4-stable'
  52. 'wolfcrypt/src/aes_asm.asm:WCv4-stable'
  53. 'wolfcrypt/src/aes_asm.S:WCv4-stable'
  54. 'wolfcrypt/src/cmac.c:WCv4-stable'
  55. 'wolfcrypt/src/des3.c:WCv4-stable'
  56. 'wolfcrypt/src/dh.c:WCv4-stable'
  57. 'wolfcrypt/src/ecc.c:WCv4-stable'
  58. 'wolfcrypt/src/hmac.c:WCv4-stable'
  59. 'wolfcrypt/src/random.c:WCv4-rng-stable'
  60. 'wolfcrypt/src/rsa.c:WCv4-stable'
  61. 'wolfcrypt/src/sha.c:WCv4-stable'
  62. 'wolfcrypt/src/sha256.c:WCv4-stable'
  63. 'wolfcrypt/src/sha3.c:WCv4-stable'
  64. 'wolfcrypt/src/sha512.c:WCv4-stable'
  65. 'wolfssl/wolfcrypt/aes.h:WCv4-stable'
  66. 'wolfssl/wolfcrypt/cmac.h:WCv4-stable'
  67. 'wolfssl/wolfcrypt/des3.h:WCv4-stable'
  68. 'wolfssl/wolfcrypt/dh.h:WCv4-stable'
  69. 'wolfssl/wolfcrypt/ecc.h:WCv4-stable'
  70. 'wolfssl/wolfcrypt/hmac.h:WCv4-stable'
  71. 'wolfssl/wolfcrypt/random.h:WCv4-rng-stable'
  72. 'wolfssl/wolfcrypt/rsa.h:WCv4-stable'
  73. 'wolfssl/wolfcrypt/sha.h:WCv4-stable'
  74. 'wolfssl/wolfcrypt/sha256.h:WCv4-stable'
  75. 'wolfssl/wolfcrypt/sha3.h:WCv4-stable'
  76. 'wolfssl/wolfcrypt/sha512.h:WCv4-stable'
  77. )
  78. if [ "$FLAVOR" = 'solaris' ]; then MAKE='gmake'; fi
  79. ;;
  80. netbsd-selftest)
  81. # non-FIPS, CAVP only but pull in selftest
  82. FIPS_OPTION='cavp-selftest'
  83. FIPS_FILES=('wolfcrypt/src/selftest.c:v3.14.2b')
  84. WOLFCRYPT_FILES=(
  85. 'wolfcrypt/src/aes.c:v3.14.2'
  86. 'wolfcrypt/src/dh.c:v3.14.2'
  87. 'wolfcrypt/src/dsa.c:v3.14.2'
  88. 'wolfcrypt/src/ecc.c:v3.14.2'
  89. 'wolfcrypt/src/hmac.c:v3.14.2'
  90. 'wolfcrypt/src/random.c:v3.14.2'
  91. 'wolfcrypt/src/rsa.c:v3.14.2'
  92. 'wolfcrypt/src/sha.c:v3.14.2'
  93. 'wolfcrypt/src/sha256.c:v3.14.2'
  94. 'wolfcrypt/src/sha512.c:v3.14.2'
  95. 'wolfssl/wolfcrypt/aes.h:v3.14.2'
  96. 'wolfssl/wolfcrypt/dh.h:v3.14.2'
  97. 'wolfssl/wolfcrypt/dsa.h:v3.14.2'
  98. 'wolfssl/wolfcrypt/ecc.h:v3.14.2'
  99. 'wolfssl/wolfcrypt/hmac.h:v3.14.2'
  100. 'wolfssl/wolfcrypt/random.h:v3.14.2'
  101. 'wolfssl/wolfcrypt/rsa.h:v3.14.2'
  102. 'wolfssl/wolfcrypt/sha.h:v3.14.2'
  103. 'wolfssl/wolfcrypt/sha256.h:v3.14.2'
  104. 'wolfssl/wolfcrypt/sha512.h:v3.14.2'
  105. )
  106. ;;
  107. marvell-linux-selftest)
  108. # non-FIPS, CAVP only but pull in selftest
  109. FIPS_OPTION='cavp-selftest-v2'
  110. FIPS_FILES=('wolfcrypt/src/selftest.c:v3.14.2b')
  111. WOLFCRYPT_FILES=(
  112. 'wolfcrypt/src/aes.c:v4.1.0-stable'
  113. 'wolfcrypt/src/dh.c:v4.1.0-stable'
  114. 'wolfcrypt/src/dsa.c:v4.1.0-stable'
  115. 'wolfcrypt/src/ecc.c:v4.1.0-stable'
  116. 'wolfcrypt/src/hmac.c:v4.1.0-stable'
  117. 'wolfcrypt/src/random.c:v4.1.0-stable'
  118. 'wolfcrypt/src/rsa.c:v4.1.0-stable'
  119. 'wolfcrypt/src/sha.c:v4.1.0-stable'
  120. 'wolfcrypt/src/sha256.c:v4.1.0-stable'
  121. 'wolfcrypt/src/sha512.c:v4.1.0-stable'
  122. 'wolfssl/wolfcrypt/aes.h:v4.1.0-stable'
  123. 'wolfssl/wolfcrypt/dh.h:v4.1.0-stable'
  124. 'wolfssl/wolfcrypt/dsa.h:v4.1.0-stable'
  125. 'wolfssl/wolfcrypt/ecc.h:v4.1.0-stable'
  126. 'wolfssl/wolfcrypt/hmac.h:v4.1.0-stable'
  127. 'wolfssl/wolfcrypt/random.h:v4.1.0-stable'
  128. 'wolfssl/wolfcrypt/rsa.h:v4.1.0-stable'
  129. 'wolfssl/wolfcrypt/sha.h:v4.1.0-stable'
  130. 'wolfssl/wolfcrypt/sha256.h:v4.1.0-stable'
  131. 'wolfssl/wolfcrypt/sha512.h:v4.1.0-stable'
  132. )
  133. ;;
  134. linuxv5)
  135. FIPS_OPTION='v5'
  136. FIPS_FILES=(
  137. 'wolfcrypt/src/fips.c:WCv5.0-RC12'
  138. 'wolfcrypt/src/fips_test.c:WCv5.0-RC12'
  139. 'wolfcrypt/src/wolfcrypt_first.c:WCv5.0-RC12'
  140. 'wolfcrypt/src/wolfcrypt_last.c:WCv5.0-RC12'
  141. 'wolfssl/wolfcrypt/fips.h:WCv5.0-RC12'
  142. )
  143. WOLFCRYPT_FILES=(
  144. 'wolfcrypt/src/aes.c:WCv5.0-RC12'
  145. 'wolfcrypt/src/aes_asm.asm:WCv5.0-RC12'
  146. 'wolfcrypt/src/aes_asm.S:WCv5.0-RC12'
  147. 'wolfcrypt/src/aes_gcm_asm.S:WCv5.0-RC12'
  148. 'wolfcrypt/src/cmac.c:WCv5.0-RC12'
  149. 'wolfcrypt/src/dh.c:WCv5.0-RC12'
  150. 'wolfcrypt/src/ecc.c:WCv5.0-RC12'
  151. 'wolfcrypt/src/hmac.c:WCv5.0-RC12'
  152. 'wolfcrypt/src/kdf.c:WCv5.0-RC12'
  153. 'wolfcrypt/src/random.c:WCv5.0-RC12'
  154. 'wolfcrypt/src/rsa.c:WCv5.0-RC12'
  155. 'wolfcrypt/src/sha.c:WCv5.0-RC12'
  156. 'wolfcrypt/src/sha256.c:WCv5.0-RC12'
  157. 'wolfcrypt/src/sha256_asm.S:WCv5.0-RC12'
  158. 'wolfcrypt/src/sha3.c:WCv5.0-RC12'
  159. 'wolfcrypt/src/sha512.c:WCv5.0-RC12'
  160. 'wolfcrypt/src/sha512_asm.S:WCv5.0-RC12'
  161. 'wolfssl/wolfcrypt/aes.h:WCv5.0-RC12'
  162. 'wolfssl/wolfcrypt/cmac.h:WCv5.0-RC12'
  163. 'wolfssl/wolfcrypt/dh.h:WCv5.0-RC12'
  164. 'wolfssl/wolfcrypt/ecc.h:WCv5.0-RC12'
  165. 'wolfssl/wolfcrypt/fips_test.h:WCv5.0-RC12'
  166. 'wolfssl/wolfcrypt/hmac.h:WCv5.0-RC12'
  167. 'wolfssl/wolfcrypt/kdf.h:WCv5.0-RC12'
  168. 'wolfssl/wolfcrypt/random.h:WCv5.0-RC12'
  169. 'wolfssl/wolfcrypt/rsa.h:WCv5.0-RC12'
  170. 'wolfssl/wolfcrypt/sha.h:WCv5.0-RC12'
  171. 'wolfssl/wolfcrypt/sha256.h:WCv5.0-RC12'
  172. 'wolfssl/wolfcrypt/sha3.h:WCv5.0-RC12'
  173. 'wolfssl/wolfcrypt/sha512.h:WCv5.0-RC12'
  174. )
  175. ;;
  176. linuxv5.2.1)
  177. FIPS_OPTION='v5'
  178. FIPS_FILES=(
  179. 'wolfcrypt/src/fips.c:v5.2.1-stable'
  180. 'wolfcrypt/src/fips_test.c:v5.2.1-stable'
  181. 'wolfcrypt/src/wolfcrypt_first.c:v5.2.1-stable'
  182. 'wolfcrypt/src/wolfcrypt_last.c:v5.2.1-stable'
  183. 'wolfssl/wolfcrypt/fips.h:v5.2.1-stable'
  184. )
  185. WOLFCRYPT_FILES=(
  186. 'wolfcrypt/src/aes.c:v5.2.1-stable'
  187. 'wolfcrypt/src/aes_asm.asm:v5.2.1-stable'
  188. 'wolfcrypt/src/aes_asm.S:v5.2.1-stable'
  189. 'wolfcrypt/src/aes_gcm_asm.S:v5.2.1-stable'
  190. 'wolfcrypt/src/cmac.c:v5.2.1-stable'
  191. 'wolfcrypt/src/dh.c:v5.2.1-stable'
  192. 'wolfcrypt/src/ecc.c:v5.2.1-stable'
  193. 'wolfcrypt/src/hmac.c:v5.2.1-stable'
  194. 'wolfcrypt/src/kdf.c:v5.2.1-stable'
  195. 'wolfcrypt/src/random.c:v5.2.1-stable'
  196. 'wolfcrypt/src/rsa.c:v5.2.1-stable'
  197. 'wolfcrypt/src/sha.c:v5.2.1-stable'
  198. 'wolfcrypt/src/sha256.c:v5.2.1-stable'
  199. 'wolfcrypt/src/sha256_asm.S:v5.2.1-stable'
  200. 'wolfcrypt/src/sha3.c:v5.2.1-stable'
  201. 'wolfcrypt/src/sha512.c:v5.2.1-stable'
  202. 'wolfcrypt/src/sha512_asm.S:v5.2.1-stable'
  203. 'wolfssl/wolfcrypt/aes.h:v5.2.1-stable'
  204. 'wolfssl/wolfcrypt/cmac.h:v5.2.1-stable'
  205. 'wolfssl/wolfcrypt/dh.h:v5.2.1-stable'
  206. 'wolfssl/wolfcrypt/ecc.h:v5.2.1-stable'
  207. 'wolfssl/wolfcrypt/fips_test.h:v5.2.1-stable'
  208. 'wolfssl/wolfcrypt/hmac.h:v5.2.1-stable'
  209. 'wolfssl/wolfcrypt/kdf.h:v5.2.1-stable'
  210. 'wolfssl/wolfcrypt/random.h:v5.2.1-stable'
  211. 'wolfssl/wolfcrypt/rsa.h:v5.2.1-stable'
  212. 'wolfssl/wolfcrypt/sha.h:v5.2.1-stable'
  213. 'wolfssl/wolfcrypt/sha256.h:v5.2.1-stable'
  214. 'wolfssl/wolfcrypt/sha3.h:v5.2.1-stable'
  215. 'wolfssl/wolfcrypt/sha512.h:v5.2.1-stable'
  216. )
  217. ;;
  218. fips-ready|fips-dev)
  219. FIPS_OPTION='ready'
  220. FIPS_FILES=(
  221. 'wolfcrypt/src/fips.c:master'
  222. 'wolfcrypt/src/fips_test.c:master'
  223. 'wolfcrypt/src/wolfcrypt_first.c:master'
  224. 'wolfcrypt/src/wolfcrypt_last.c:master'
  225. 'wolfssl/wolfcrypt/fips.h:master'
  226. )
  227. WOLFCRYPT_FILES=()
  228. if [ "$FLAVOR" = 'fips-dev' ]; then FIPS_OPTION='dev'; fi
  229. ;;
  230. wolfrand)
  231. FIPS_OPTION='rand'
  232. FIPS_FILES=(
  233. 'wolfcrypt/src/fips.c:WRv4-stable'
  234. 'wolfcrypt/src/fips_test.c:WRv4-stable'
  235. 'wolfcrypt/src/wolfcrypt_first.c:WRv4-stable'
  236. 'wolfcrypt/src/wolfcrypt_last.c:WRv4-stable'
  237. 'wolfssl/wolfcrypt/fips.h:WRv4-stable'
  238. )
  239. WOLFCRYPT_FILES=(
  240. 'wolfcrypt/src/hmac.c:WCv4-stable'
  241. 'wolfcrypt/src/random.c:WCv4-rng-stable'
  242. 'wolfcrypt/src/sha256.c:WCv4-stable'
  243. 'wolfssl/wolfcrypt/hmac.h:WCv4-stable'
  244. 'wolfssl/wolfcrypt/random.h:WCv4-rng-stable'
  245. 'wolfssl/wolfcrypt/sha256.h:WCv4-stable'
  246. )
  247. ;;
  248. *)
  249. Usage
  250. exit 1
  251. esac
  252. # checkout_files takes an array of pairs of file paths and git tags to
  253. # checkout. It will check to see if mytag exists and if not will make that
  254. # tag a branch.
  255. function checkout_files() {
  256. local name
  257. local tag
  258. for file_entry in "$@"
  259. do
  260. name=${file_entry%%:*}
  261. tag=${file_entry#*:}
  262. if ! $GIT rev-parse -q --verify "my$tag" >/dev/null
  263. then
  264. $GIT branch --no-track "my$tag" "$tag" || exit $?
  265. fi
  266. $GIT checkout "my$tag" -- "$name" || exit $?
  267. done
  268. }
  269. # copy_fips_files takes an array of pairs of file paths and git tags to
  270. # checkout. It will check to see if mytag exists and if now will make that
  271. # tag a branch. It breaks the filepath apart into file name and path, then
  272. # copies it from the file from the fips directory to the path.
  273. function copy_fips_files() {
  274. local name
  275. local bname
  276. local dname
  277. local tag
  278. for file_entry in "$@"
  279. do
  280. name=${file_entry%%:*}
  281. tag=${file_entry#*:}
  282. bname=$(basename "$name")
  283. dname=$(dirname "$name")
  284. if ! $GIT rev-parse -q --verify "my$tag" >/dev/null
  285. then
  286. $GIT branch --no-track "my$tag" "$tag" || exit $?
  287. fi
  288. $GIT checkout "my$tag" -- "$bname" || exit $?
  289. cp "$bname" "../$dname"
  290. done
  291. }
  292. if ! $GIT clone . "$TEST_DIR"; then
  293. echo "fips-check: Couldn't duplicate current working directory."
  294. exit 1
  295. fi
  296. pushd "$TEST_DIR" || exit 2
  297. if ! $GIT clone "$FIPS_REPO" fips
  298. then
  299. echo "fips-check: Couldn't check out FIPS repository."
  300. exit 1
  301. fi
  302. checkout_files "${WOLFCRYPT_FILES[@]}" || exit 3
  303. pushd fips || exit 2
  304. copy_fips_files "${FIPS_FILES[@]}" || exit 3
  305. popd || exit 2
  306. # When checking out cert 3389 ready code, NIST will no longer perform
  307. # new certifications on 140-2 modules. If we were to use the latest files from
  308. # master that would require re-cert due to changes in the module boundary.
  309. # Since OE additions can still be processed for cert3389 we will call 140-2
  310. # ready "fipsv2-OE-ready" indicating it is ready to use for an OE addition but
  311. # would not be good for a new certification effort with the latest files.
  312. if [ "$FLAVOR" = 'fipsv2-OE-ready' ] && [ -s wolfcrypt/src/fips.c ]
  313. then
  314. cp wolfcrypt/src/fips.c wolfcrypt/src/fips.c.bak
  315. sed "s/v4.0.0-alpha/fipsv2-OE-ready/" wolfcrypt/src/fips.c.bak >wolfcrypt/src/fips.c
  316. fi
  317. # run the make test
  318. ./autogen.sh
  319. case "$FIPS_OPTION" in
  320. cavp-selftest)
  321. ./configure --enable-selftest
  322. ;;
  323. cavp-selftest-v2)
  324. ./configure --enable-selftest=v2
  325. ;;
  326. *)
  327. ./configure --enable-fips=$FIPS_OPTION
  328. ;;
  329. esac
  330. if ! $MAKE
  331. then
  332. echo 'fips-check: Make failed. Debris left for analysis.'
  333. exit 3
  334. fi
  335. if [ -s wolfcrypt/src/fips_test.c ]
  336. then
  337. NEWHASH=$(./wolfcrypt/test/testwolfcrypt | sed -n 's/hash = \(.*\)/\1/p')
  338. if [ -n "$NEWHASH" ]; then
  339. cp wolfcrypt/src/fips_test.c wolfcrypt/src/fips_test.c.bak
  340. sed "s/^\".*\";/\"${NEWHASH}\";/" wolfcrypt/src/fips_test.c.bak >wolfcrypt/src/fips_test.c
  341. make clean
  342. fi
  343. fi
  344. if ! $MAKE check
  345. then
  346. echo 'fips-check: Test failed. Debris left for analysis.'
  347. exit 3
  348. fi
  349. # Clean up
  350. popd || exit 2
  351. if [ "$KEEP" = 'no' ];
  352. then
  353. rm -rf "$TEST_DIR"
  354. fi