1
0

fips-check.sh 13 KB

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