fipsalgtest.pl 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. #!/usr/bin/perl -w
  2. # Perl utility to run or verify FIPS 140-2 CAVP algorithm tests based on the
  3. # pathnames of input algorithm test files actually present (the unqualified
  4. # file names are consistent but the pathnames are not).
  5. #
  6. # FIPS test definitions
  7. # List of all the unqualified file names we expect and command lines to run
  8. # DSA tests
  9. my @fips_dsa_test_list = (
  10. "DSA",
  11. [ "PQGGen", "fips_dssvs pqg" ],
  12. [ "KeyPair", "fips_dssvs keypair" ],
  13. [ "SigGen", "fips_dssvs siggen" ],
  14. [ "SigVer", "fips_dssvs sigver" ]
  15. );
  16. my @fips_dsa_pqgver_test_list = (
  17. [ "PQGVer", "fips_dssvs pqgver" ]
  18. );
  19. # RSA tests
  20. my @fips_rsa_test_list = (
  21. "RSA",
  22. [ "SigGen15", "fips_rsastest" ],
  23. [ "SigVer15", "fips_rsavtest" ],
  24. [ "SigVerRSA", "fips_rsavtest -x931" ],
  25. [ "KeyGenRSA", "fips_rsagtest" ],
  26. [ "SigGenRSA", "fips_rsastest -x931" ]
  27. );
  28. # Special cases for PSS. The filename itself is
  29. # not sufficient to determine the test. Addditionally we
  30. # need to examine the file contents to determine the salt length
  31. # In these cases the test filename has (saltlen) appended.
  32. # RSA PSS salt length 0 tests
  33. my @fips_rsa_pss0_test_list = (
  34. [ "SigGenPSS(0)", "fips_rsastest -saltlen 0",
  35. '^\s*#\s*salt\s+len:\s+0\s*$' ],
  36. [ "SigVerPSS(0)", "fips_rsavtest -saltlen 0",
  37. '^\s*#\s*salt\s+len:\s+0\s*$' ],
  38. );
  39. # RSA PSS salt length 62 tests
  40. my @fips_rsa_pss62_test_list = (
  41. [ "SigGenPSS(62)", "fips_rsastest -saltlen 62",
  42. '^\s*#\s*salt\s+len:\s+62\s*$' ],
  43. [ "SigVerPSS(62)", "fips_rsavtest -saltlen 62",
  44. '^\s*#\s*salt\s+len:\s+62\s*$' ],
  45. );
  46. # SHA tests
  47. my @fips_sha_test_list = (
  48. "SHA",
  49. [ "SHA1LongMsg", "fips_shatest" ],
  50. [ "SHA1Monte", "fips_shatest" ],
  51. [ "SHA1ShortMsg", "fips_shatest" ],
  52. [ "SHA224LongMsg", "fips_shatest" ],
  53. [ "SHA224Monte", "fips_shatest" ],
  54. [ "SHA224ShortMsg", "fips_shatest" ],
  55. [ "SHA256LongMsg", "fips_shatest" ],
  56. [ "SHA256Monte", "fips_shatest" ],
  57. [ "SHA256ShortMsg", "fips_shatest" ],
  58. [ "SHA384LongMsg", "fips_shatest" ],
  59. [ "SHA384Monte", "fips_shatest" ],
  60. [ "SHA384ShortMsg", "fips_shatest" ],
  61. [ "SHA512LongMsg", "fips_shatest" ],
  62. [ "SHA512Monte", "fips_shatest" ],
  63. [ "SHA512ShortMsg", "fips_shatest" ]
  64. );
  65. # HMAC
  66. my @fips_hmac_test_list = (
  67. "HMAC",
  68. [ "HMAC", "fips_hmactest" ]
  69. );
  70. # CMAC
  71. my @fips_cmac_test_list = (
  72. "CMAC",
  73. [ "CMACGenAES128", "fips_cmactest -a aes128 -g" ],
  74. [ "CMACVerAES128", "fips_cmactest -a aes128 -v" ],
  75. [ "CMACGenAES192", "fips_cmactest -a aes192 -g" ],
  76. [ "CMACVerAES192", "fips_cmactest -a aes192 -v" ],
  77. [ "CMACGenAES256", "fips_cmactest -a aes256 -g" ],
  78. [ "CMACVerAES256", "fips_cmactest -a aes256 -v" ],
  79. [ "CMACGenTDES3", "fips_cmactest -a tdes3 -g" ],
  80. [ "CMACVerTDES3", "fips_cmactest -a tdes3 -v" ],
  81. );
  82. # RAND tests, AES version
  83. my @fips_rand_aes_test_list = (
  84. "RAND (AES)",
  85. [ "ANSI931_AES128MCT", "fips_rngvs mct" ],
  86. [ "ANSI931_AES192MCT", "fips_rngvs mct" ],
  87. [ "ANSI931_AES256MCT", "fips_rngvs mct" ],
  88. [ "ANSI931_AES128VST", "fips_rngvs vst" ],
  89. [ "ANSI931_AES192VST", "fips_rngvs vst" ],
  90. [ "ANSI931_AES256VST", "fips_rngvs vst" ]
  91. );
  92. # RAND tests, DES2 version
  93. my @fips_rand_des2_test_list = (
  94. "RAND (DES2)",
  95. [ "ANSI931_TDES2MCT", "fips_rngvs mct" ],
  96. [ "ANSI931_TDES2VST", "fips_rngvs vst" ]
  97. );
  98. # AES tests
  99. my @fips_aes_test_list = (
  100. "AES",
  101. [ "CBCGFSbox128", "fips_aesavs -f" ],
  102. [ "CBCGFSbox192", "fips_aesavs -f" ],
  103. [ "CBCGFSbox256", "fips_aesavs -f" ],
  104. [ "CBCKeySbox128", "fips_aesavs -f" ],
  105. [ "CBCKeySbox192", "fips_aesavs -f" ],
  106. [ "CBCKeySbox256", "fips_aesavs -f" ],
  107. [ "CBCMCT128", "fips_aesavs -f" ],
  108. [ "CBCMCT192", "fips_aesavs -f" ],
  109. [ "CBCMCT256", "fips_aesavs -f" ],
  110. [ "CBCMMT128", "fips_aesavs -f" ],
  111. [ "CBCMMT192", "fips_aesavs -f" ],
  112. [ "CBCMMT256", "fips_aesavs -f" ],
  113. [ "CBCVarKey128", "fips_aesavs -f" ],
  114. [ "CBCVarKey192", "fips_aesavs -f" ],
  115. [ "CBCVarKey256", "fips_aesavs -f" ],
  116. [ "CBCVarTxt128", "fips_aesavs -f" ],
  117. [ "CBCVarTxt192", "fips_aesavs -f" ],
  118. [ "CBCVarTxt256", "fips_aesavs -f" ],
  119. [ "CFB128GFSbox128", "fips_aesavs -f" ],
  120. [ "CFB128GFSbox192", "fips_aesavs -f" ],
  121. [ "CFB128GFSbox256", "fips_aesavs -f" ],
  122. [ "CFB128KeySbox128", "fips_aesavs -f" ],
  123. [ "CFB128KeySbox192", "fips_aesavs -f" ],
  124. [ "CFB128KeySbox256", "fips_aesavs -f" ],
  125. [ "CFB128MCT128", "fips_aesavs -f" ],
  126. [ "CFB128MCT192", "fips_aesavs -f" ],
  127. [ "CFB128MCT256", "fips_aesavs -f" ],
  128. [ "CFB128MMT128", "fips_aesavs -f" ],
  129. [ "CFB128MMT192", "fips_aesavs -f" ],
  130. [ "CFB128MMT256", "fips_aesavs -f" ],
  131. [ "CFB128VarKey128", "fips_aesavs -f" ],
  132. [ "CFB128VarKey192", "fips_aesavs -f" ],
  133. [ "CFB128VarKey256", "fips_aesavs -f" ],
  134. [ "CFB128VarTxt128", "fips_aesavs -f" ],
  135. [ "CFB128VarTxt192", "fips_aesavs -f" ],
  136. [ "CFB128VarTxt256", "fips_aesavs -f" ],
  137. [ "CFB8GFSbox128", "fips_aesavs -f" ],
  138. [ "CFB8GFSbox192", "fips_aesavs -f" ],
  139. [ "CFB8GFSbox256", "fips_aesavs -f" ],
  140. [ "CFB8KeySbox128", "fips_aesavs -f" ],
  141. [ "CFB8KeySbox192", "fips_aesavs -f" ],
  142. [ "CFB8KeySbox256", "fips_aesavs -f" ],
  143. [ "CFB8MCT128", "fips_aesavs -f" ],
  144. [ "CFB8MCT192", "fips_aesavs -f" ],
  145. [ "CFB8MCT256", "fips_aesavs -f" ],
  146. [ "CFB8MMT128", "fips_aesavs -f" ],
  147. [ "CFB8MMT192", "fips_aesavs -f" ],
  148. [ "CFB8MMT256", "fips_aesavs -f" ],
  149. [ "CFB8VarKey128", "fips_aesavs -f" ],
  150. [ "CFB8VarKey192", "fips_aesavs -f" ],
  151. [ "CFB8VarKey256", "fips_aesavs -f" ],
  152. [ "CFB8VarTxt128", "fips_aesavs -f" ],
  153. [ "CFB8VarTxt192", "fips_aesavs -f" ],
  154. [ "CFB8VarTxt256", "fips_aesavs -f" ],
  155. [ "ECBGFSbox128", "fips_aesavs -f" ],
  156. [ "ECBGFSbox192", "fips_aesavs -f" ],
  157. [ "ECBGFSbox256", "fips_aesavs -f" ],
  158. [ "ECBKeySbox128", "fips_aesavs -f" ],
  159. [ "ECBKeySbox192", "fips_aesavs -f" ],
  160. [ "ECBKeySbox256", "fips_aesavs -f" ],
  161. [ "ECBMCT128", "fips_aesavs -f" ],
  162. [ "ECBMCT192", "fips_aesavs -f" ],
  163. [ "ECBMCT256", "fips_aesavs -f" ],
  164. [ "ECBMMT128", "fips_aesavs -f" ],
  165. [ "ECBMMT192", "fips_aesavs -f" ],
  166. [ "ECBMMT256", "fips_aesavs -f" ],
  167. [ "ECBVarKey128", "fips_aesavs -f" ],
  168. [ "ECBVarKey192", "fips_aesavs -f" ],
  169. [ "ECBVarKey256", "fips_aesavs -f" ],
  170. [ "ECBVarTxt128", "fips_aesavs -f" ],
  171. [ "ECBVarTxt192", "fips_aesavs -f" ],
  172. [ "ECBVarTxt256", "fips_aesavs -f" ],
  173. [ "OFBGFSbox128", "fips_aesavs -f" ],
  174. [ "OFBGFSbox192", "fips_aesavs -f" ],
  175. [ "OFBGFSbox256", "fips_aesavs -f" ],
  176. [ "OFBKeySbox128", "fips_aesavs -f" ],
  177. [ "OFBKeySbox192", "fips_aesavs -f" ],
  178. [ "OFBKeySbox256", "fips_aesavs -f" ],
  179. [ "OFBMCT128", "fips_aesavs -f" ],
  180. [ "OFBMCT192", "fips_aesavs -f" ],
  181. [ "OFBMCT256", "fips_aesavs -f" ],
  182. [ "OFBMMT128", "fips_aesavs -f" ],
  183. [ "OFBMMT192", "fips_aesavs -f" ],
  184. [ "OFBMMT256", "fips_aesavs -f" ],
  185. [ "OFBVarKey128", "fips_aesavs -f" ],
  186. [ "OFBVarKey192", "fips_aesavs -f" ],
  187. [ "OFBVarKey256", "fips_aesavs -f" ],
  188. [ "OFBVarTxt128", "fips_aesavs -f" ],
  189. [ "OFBVarTxt192", "fips_aesavs -f" ],
  190. [ "OFBVarTxt256", "fips_aesavs -f" ]
  191. );
  192. my @fips_aes_cfb1_test_list = (
  193. # AES CFB1 tests
  194. [ "CFB1GFSbox128", "fips_aesavs -f" ],
  195. [ "CFB1GFSbox192", "fips_aesavs -f" ],
  196. [ "CFB1GFSbox256", "fips_aesavs -f" ],
  197. [ "CFB1KeySbox128", "fips_aesavs -f" ],
  198. [ "CFB1KeySbox192", "fips_aesavs -f" ],
  199. [ "CFB1KeySbox256", "fips_aesavs -f" ],
  200. [ "CFB1MCT128", "fips_aesavs -f" ],
  201. [ "CFB1MCT192", "fips_aesavs -f" ],
  202. [ "CFB1MCT256", "fips_aesavs -f" ],
  203. [ "CFB1MMT128", "fips_aesavs -f" ],
  204. [ "CFB1MMT192", "fips_aesavs -f" ],
  205. [ "CFB1MMT256", "fips_aesavs -f" ],
  206. [ "CFB1VarKey128", "fips_aesavs -f" ],
  207. [ "CFB1VarKey192", "fips_aesavs -f" ],
  208. [ "CFB1VarKey256", "fips_aesavs -f" ],
  209. [ "CFB1VarTxt128", "fips_aesavs -f" ],
  210. [ "CFB1VarTxt192", "fips_aesavs -f" ],
  211. [ "CFB1VarTxt256", "fips_aesavs -f" ]
  212. );
  213. my @fips_aes_ccm_test_list = (
  214. # AES CCM tests
  215. "AES CCM",
  216. [ "DVPT128", "fips_gcmtest -ccm" ],
  217. [ "DVPT192", "fips_gcmtest -ccm" ],
  218. [ "DVPT256", "fips_gcmtest -ccm" ],
  219. [ "VADT128", "fips_gcmtest -ccm" ],
  220. [ "VADT192", "fips_gcmtest -ccm" ],
  221. [ "VADT256", "fips_gcmtest -ccm" ],
  222. [ "VNT128", "fips_gcmtest -ccm" ],
  223. [ "VNT192", "fips_gcmtest -ccm" ],
  224. [ "VNT256", "fips_gcmtest -ccm" ],
  225. [ "VPT128", "fips_gcmtest -ccm" ],
  226. [ "VPT192", "fips_gcmtest -ccm" ],
  227. [ "VPT256", "fips_gcmtest -ccm" ],
  228. [ "VTT128", "fips_gcmtest -ccm" ],
  229. [ "VTT192", "fips_gcmtest -ccm" ],
  230. [ "VTT256", "fips_gcmtest -ccm" ]
  231. );
  232. my @fips_aes_gcm_test_list = (
  233. # AES GCM tests
  234. "AES GCM",
  235. [ "gcmDecrypt128", "fips_gcmtest -decrypt" ],
  236. [ "gcmDecrypt192", "fips_gcmtest -decrypt" ],
  237. [ "gcmDecrypt256", "fips_gcmtest -decrypt" ],
  238. );
  239. my @fips_aes_xts_test_list = (
  240. # AES XTS tests
  241. "AES XTS",
  242. [ "XTSGenAES128", "fips_gcmtest -xts" ],
  243. [ "XTSGenAES256", "fips_gcmtest -xts" ],
  244. );
  245. # Triple DES tests
  246. my @fips_des3_test_list = (
  247. "Triple DES",
  248. [ "TCBCinvperm", "fips_desmovs -f" ],
  249. [ "TCBCMMT1", "fips_desmovs -f" ],
  250. [ "TCBCMMT2", "fips_desmovs -f" ],
  251. [ "TCBCMMT3", "fips_desmovs -f" ],
  252. [ "TCBCMonte1", "fips_desmovs -f" ],
  253. [ "TCBCMonte2", "fips_desmovs -f" ],
  254. [ "TCBCMonte3", "fips_desmovs -f" ],
  255. [ "TCBCpermop", "fips_desmovs -f" ],
  256. [ "TCBCsubtab", "fips_desmovs -f" ],
  257. [ "TCBCvarkey", "fips_desmovs -f" ],
  258. [ "TCBCvartext", "fips_desmovs -f" ],
  259. [ "TCFB64invperm", "fips_desmovs -f" ],
  260. [ "TCFB64MMT1", "fips_desmovs -f" ],
  261. [ "TCFB64MMT2", "fips_desmovs -f" ],
  262. [ "TCFB64MMT3", "fips_desmovs -f" ],
  263. [ "TCFB64Monte1", "fips_desmovs -f" ],
  264. [ "TCFB64Monte2", "fips_desmovs -f" ],
  265. [ "TCFB64Monte3", "fips_desmovs -f" ],
  266. [ "TCFB64permop", "fips_desmovs -f" ],
  267. [ "TCFB64subtab", "fips_desmovs -f" ],
  268. [ "TCFB64varkey", "fips_desmovs -f" ],
  269. [ "TCFB64vartext", "fips_desmovs -f" ],
  270. [ "TCFB8invperm", "fips_desmovs -f" ],
  271. [ "TCFB8MMT1", "fips_desmovs -f" ],
  272. [ "TCFB8MMT2", "fips_desmovs -f" ],
  273. [ "TCFB8MMT3", "fips_desmovs -f" ],
  274. [ "TCFB8Monte1", "fips_desmovs -f" ],
  275. [ "TCFB8Monte2", "fips_desmovs -f" ],
  276. [ "TCFB8Monte3", "fips_desmovs -f" ],
  277. [ "TCFB8permop", "fips_desmovs -f" ],
  278. [ "TCFB8subtab", "fips_desmovs -f" ],
  279. [ "TCFB8varkey", "fips_desmovs -f" ],
  280. [ "TCFB8vartext", "fips_desmovs -f" ],
  281. [ "TECBinvperm", "fips_desmovs -f" ],
  282. [ "TECBMMT1", "fips_desmovs -f" ],
  283. [ "TECBMMT2", "fips_desmovs -f" ],
  284. [ "TECBMMT3", "fips_desmovs -f" ],
  285. [ "TECBMonte1", "fips_desmovs -f" ],
  286. [ "TECBMonte2", "fips_desmovs -f" ],
  287. [ "TECBMonte3", "fips_desmovs -f" ],
  288. [ "TECBpermop", "fips_desmovs -f" ],
  289. [ "TECBsubtab", "fips_desmovs -f" ],
  290. [ "TECBvarkey", "fips_desmovs -f" ],
  291. [ "TECBvartext", "fips_desmovs -f" ],
  292. [ "TOFBinvperm", "fips_desmovs -f" ],
  293. [ "TOFBMMT1", "fips_desmovs -f" ],
  294. [ "TOFBMMT2", "fips_desmovs -f" ],
  295. [ "TOFBMMT3", "fips_desmovs -f" ],
  296. [ "TOFBMonte1", "fips_desmovs -f" ],
  297. [ "TOFBMonte2", "fips_desmovs -f" ],
  298. [ "TOFBMonte3", "fips_desmovs -f" ],
  299. [ "TOFBpermop", "fips_desmovs -f" ],
  300. [ "TOFBsubtab", "fips_desmovs -f" ],
  301. [ "TOFBvarkey", "fips_desmovs -f" ],
  302. [ "TOFBvartext", "fips_desmovs -f" ]
  303. );
  304. my @fips_des3_cfb1_test_list = (
  305. # DES3 CFB1 tests
  306. [ "TCFB1invperm", "fips_desmovs -f" ],
  307. [ "TCFB1MMT1", "fips_desmovs -f" ],
  308. [ "TCFB1MMT2", "fips_desmovs -f" ],
  309. [ "TCFB1MMT3", "fips_desmovs -f" ],
  310. [ "TCFB1Monte1", "fips_desmovs -f" ],
  311. [ "TCFB1Monte2", "fips_desmovs -f" ],
  312. [ "TCFB1Monte3", "fips_desmovs -f" ],
  313. [ "TCFB1permop", "fips_desmovs -f" ],
  314. [ "TCFB1subtab", "fips_desmovs -f" ],
  315. [ "TCFB1varkey", "fips_desmovs -f" ],
  316. [ "TCFB1vartext", "fips_desmovs -f" ],
  317. );
  318. my @fips_drbg_test_list = (
  319. # SP800-90 DRBG tests
  320. "SP800-90 DRBG",
  321. [ "CTR_DRBG", "fips_drbgvs" ],
  322. [ "Hash_DRBG", "fips_drbgvs" ]
  323. );
  324. my @fips_dh_test_list = (
  325. # DH
  326. "DH Ephemeral Primitives Only",
  327. [ "KASValidityTest_FFCEphem_NOKC_ZZOnly_init", "fips_dhvs dhver" ],
  328. [ "KASValidityTest_FFCEphem_NOKC_ZZOnly_resp", "fips_dhvs dhver" ],
  329. );
  330. my @fips_ecdh_test_list = (
  331. # ECDH
  332. "ECDH Ephemeral Primitives Only",
  333. [ "KASValidityTest_ECCEphemeralUnified_NOKC_ZZOnly_init",
  334. "fips_ecdhvs ecdhver" ],
  335. [ "KASValidityTest_ECCEphemeralUnified_NOKC_ZZOnly_resp",
  336. "fips_ecdhvs ecdhver" ],
  337. );
  338. # Verification special cases.
  339. # In most cases the output of a test is deterministic and
  340. # it can be compared to a known good result. A few involve
  341. # the genration and use of random keys and the output will
  342. # be different each time. In thoses cases we perform special tests
  343. # to simply check their consistency. For example signature generation
  344. # output will be run through signature verification to see if all outputs
  345. # show as valid.
  346. #
  347. my %verify_special = (
  348. "PQGGen" => "fips_dssvs pqgver",
  349. "KeyPair" => "fips_dssvs keyver",
  350. "SigGen" => "fips_dssvs sigver",
  351. "SigGen15" => "fips_rsavtest",
  352. "SigGenRSA" => "fips_rsavtest -x931",
  353. "SigGenPSS(0)" => "fips_rsavtest -saltlen 0",
  354. "SigGenPSS(62)" => "fips_rsavtest -saltlen 62",
  355. );
  356. my $win32 = $^O =~ m/mswin/i;
  357. my $onedir = 0;
  358. my $filter = "";
  359. my $tvdir;
  360. my $tprefix;
  361. my $debug = 0;
  362. my $quiet = 0;
  363. my $notest = 0;
  364. my $verify = 1;
  365. my $rspdir = "rsp";
  366. my $ignore_missing = 0;
  367. my $ignore_bogus = 0;
  368. my $bufout = '';
  369. my $list_tests = 0;
  370. my $minimal_script = 0;
  371. my $outfile = '';
  372. my $no_warn_missing = 0;
  373. my $no_warn_bogus = 0;
  374. my $rmcmd = "rm -rf";
  375. my $mkcmd = "mkdir";
  376. my %fips_enabled = (
  377. dsa => 1,
  378. "dsa-pqgver" => 0,
  379. rsa => 1,
  380. "rsa-pss0" => 0,
  381. "rsa-pss62" => 1,
  382. sha => 1,
  383. hmac => 1,
  384. cmac => 0,
  385. "rand-aes" => 1,
  386. "rand-des2" => 0,
  387. aes => 1,
  388. "aes-cfb1" => 0,
  389. des3 => 1,
  390. "des3-cfb1" => 0,
  391. drbg => 0,
  392. ccm => 0,
  393. "aes-xts" => 0,
  394. gcm => 0,
  395. dh => 0,
  396. ecdh => 0,
  397. );
  398. foreach (@ARGV) {
  399. if ( $_ eq "--win32" ) {
  400. $win32 = 1;
  401. }
  402. elsif ( $_ eq "--onedir" ) {
  403. $onedir = 1;
  404. }
  405. elsif ( $_ eq "--debug" ) {
  406. $debug = 1;
  407. }
  408. elsif ( $_ eq "--quiet-missing" ) {
  409. $ignore_missing = 1;
  410. $no_warn_missing = 1;
  411. }
  412. elsif ( $_ eq "--ignore-missing" ) {
  413. $ignore_missing = 1;
  414. }
  415. elsif ( $_ eq "--quiet-bogus" ) {
  416. $ignore_bogus = 1;
  417. $no_warn_bogus = 1;
  418. }
  419. elsif ( $_ eq "--ignore-bogus" ) {
  420. $ignore_bogus = 1;
  421. }
  422. elsif ( $_ eq "--minimal-script" ) {
  423. $minimal_script = 1;
  424. }
  425. elsif (/--generate-script=(.*)$/) {
  426. $outfile = $1;
  427. $verify = 0;
  428. } elsif ( $_ eq "--generate" ) {
  429. $verify = 0;
  430. }
  431. elsif ( $_ eq "--notest" ) {
  432. $notest = 1;
  433. }
  434. elsif ( $_ eq "--quiet" ) {
  435. $quiet = 1;
  436. }
  437. elsif (/--dir=(.*)$/) {
  438. $tvdir = $1;
  439. }
  440. elsif (/--rspdir=(.*)$/) {
  441. $rspdir = $1;
  442. }
  443. elsif (/--tprefix=(.*)$/) {
  444. $tprefix = $1;
  445. }
  446. elsif (/^--(enable|disable)-(.*)$/) {
  447. if ( !exists $fips_enabled{$2} ) {
  448. print STDERR "Unknown test $2\n";
  449. exit(1);
  450. }
  451. if ( $1 eq "enable" ) {
  452. $fips_enabled{$2} = 1;
  453. }
  454. else {
  455. $fips_enabled{$2} = 0;
  456. }
  457. }
  458. elsif (/--filter=(.*)$/) {
  459. $filter = $1;
  460. }
  461. elsif (/--rm=(.*)$/) {
  462. $rmcmd = $1;
  463. }
  464. elsif (/--script-tprefix=(.*)$/) {
  465. $stprefix = $1;
  466. }
  467. elsif (/--mkdir=(.*)$/) {
  468. $mkcmd = $1;
  469. }
  470. elsif (/^--list-tests$/) {
  471. $list_tests = 1;
  472. }
  473. else {
  474. Help();
  475. exit(1);
  476. }
  477. }
  478. my @fips_test_list;
  479. push @fips_test_list, @fips_dsa_test_list if $fips_enabled{"dsa"};
  480. push @fips_test_list, @fips_dsa_pqgver_test_list if $fips_enabled{"dsa-pqgver"};
  481. push @fips_test_list, @fips_rsa_test_list if $fips_enabled{"rsa"};
  482. push @fips_test_list, @fips_rsa_pss0_test_list if $fips_enabled{"rsa-pss0"};
  483. push @fips_test_list, @fips_rsa_pss62_test_list if $fips_enabled{"rsa-pss62"};
  484. push @fips_test_list, @fips_sha_test_list if $fips_enabled{"sha"};
  485. push @fips_test_list, @fips_hmac_test_list if $fips_enabled{"hmac"};
  486. push @fips_test_list, @fips_cmac_test_list if $fips_enabled{"cmac"};
  487. push @fips_test_list, @fips_rand_aes_test_list if $fips_enabled{"rand-aes"};
  488. push @fips_test_list, @fips_rand_des2_test_list if $fips_enabled{"rand-des2"};
  489. push @fips_test_list, @fips_aes_test_list if $fips_enabled{"aes"};
  490. push @fips_test_list, @fips_aes_cfb1_test_list if $fips_enabled{"aes-cfb1"};
  491. push @fips_test_list, @fips_des3_test_list if $fips_enabled{"des3"};
  492. push @fips_test_list, @fips_des3_cfb1_test_list if $fips_enabled{"des3-cfb1"};
  493. push @fips_test_list, @fips_drbg_test_list if $fips_enabled{"drbg"};
  494. push @fips_test_list, @fips_aes_ccm_test_list if $fips_enabled{"aes-ccm"};
  495. push @fips_test_list, @fips_aes_gcm_test_list if $fips_enabled{"aes-gcm"};
  496. push @fips_test_list, @fips_aes_xts_test_list if $fips_enabled{"aes-xts"};
  497. push @fips_test_list, @fips_dh_test_list if $fips_enabled{"dh"};
  498. push @fips_test_list, @fips_ecdh_test_list if $fips_enabled{"ecdh"};
  499. if ($list_tests) {
  500. my ( $test, $en );
  501. print "=====TEST LIST=====\n";
  502. foreach $test ( sort keys %fips_enabled ) {
  503. $en = $fips_enabled{$test};
  504. $test =~ tr/[a-z]/[A-Z]/;
  505. printf "%-10s %s\n", $test, $en ? "enabled" : "disabled";
  506. }
  507. exit(0);
  508. }
  509. foreach (@fips_test_list) {
  510. next unless ref($_);
  511. my $nm = $$_[0];
  512. $$_[3] = "";
  513. $$_[4] = "";
  514. print STDERR "Duplicate test $nm\n" if exists $fips_tests{$nm};
  515. $fips_tests{$nm} = $_;
  516. }
  517. $tvdir = "." unless defined $tvdir;
  518. if ($win32) {
  519. if ( !defined $tprefix ) {
  520. if ($onedir) {
  521. $tprefix = ".\\";
  522. }
  523. else {
  524. $tprefix = "..\\out32dll\\";
  525. }
  526. }
  527. }
  528. else {
  529. if ($onedir) {
  530. $tprefix = "./" unless defined $tprefix;
  531. }
  532. else {
  533. $tprefix = "../test/" unless defined $tprefix;
  534. }
  535. }
  536. sanity_check_exe( $win32, $tprefix) if $outfile eq "";
  537. find_files( $filter, $tvdir );
  538. sanity_check_files();
  539. my ( $runerr, $cmperr, $cmpok, $scheckrunerr, $scheckerr, $scheckok, $skipcnt )
  540. = ( 0, 0, 0, 0, 0, 0, 0 );
  541. exit(0) if $notest;
  542. print "Outputting commands to $outfile\n" if $outfile ne "";
  543. run_tests( $verify, $win32, $tprefix, $filter, $tvdir, $outfile );
  544. if ($verify) {
  545. print "ALGORITHM TEST VERIFY SUMMARY REPORT:\n";
  546. print "Tests skipped due to missing files: $skipcnt\n";
  547. print "Algorithm test program execution failures: $runerr\n";
  548. print "Test comparisons successful: $cmpok\n";
  549. print "Test comparisons failed: $cmperr\n";
  550. print "Test sanity checks successful: $scheckok\n";
  551. print "Test sanity checks failed: $scheckerr\n";
  552. print "Sanity check program execution failures: $scheckrunerr\n";
  553. if ( $runerr || $cmperr || $scheckrunerr || $scheckerr ) {
  554. print "***TEST FAILURE***\n";
  555. }
  556. else {
  557. print "***ALL TESTS SUCCESSFUL***\n";
  558. }
  559. }
  560. elsif ($outfile eq "") {
  561. print "ALGORITHM TEST SUMMARY REPORT:\n";
  562. print "Tests skipped due to missing files: $skipcnt\n";
  563. print "Algorithm test program execution failures: $runerr\n";
  564. if ($runerr) {
  565. print "***TEST FAILURE***\n";
  566. }
  567. else {
  568. print "***ALL TESTS SUCCESSFUL***\n";
  569. }
  570. }
  571. #--------------------------------
  572. sub Help {
  573. ( my $cmd ) = ( $0 =~ m#([^/]+)$# );
  574. print <<EOF;
  575. $cmd: generate run CAVP algorithm tests
  576. --debug Enable debug output
  577. --dir=<dirname> Optional root for *.req file search
  578. --filter=<regexp>
  579. --onedir <dirname> Assume all components in current directory
  580. --rspdir=<dirname> Name of subdirectories containing *.rsp files, default "rsp"
  581. --tprefix=<prefix>
  582. --ignore-bogus Ignore duplicate or bogus files
  583. --ignore-missing Ignore missing test files
  584. --quiet Shhh....
  585. --generate Generate algorithm test output
  586. --win32 Win32 environment
  587. --enable-<alg> Enable algorithm set <alg>.
  588. --disable-<alg> Disable algorithm set <alg>.
  589. Where <alg> can be one of:
  590. EOF
  591. while (my ($key, $value) = each %fips_enabled)
  592. {
  593. printf "\t\t%-20s(%s by default)\n", $key ,
  594. $value ? "enabled" : "disabled";
  595. }
  596. }
  597. # Sanity check to see if all necessary executables exist
  598. sub sanity_check_exe {
  599. my ( $win32, $tprefix, ) = @_;
  600. my %exe_list;
  601. my $bad = 0;
  602. foreach (@fips_test_list) {
  603. next unless ref($_);
  604. my $cmd = $_->[1];
  605. $cmd =~ s/ .*$//;
  606. $cmd = $tprefix . $cmd;
  607. $cmd .= ".exe" if $win32;
  608. $exe_list{$cmd} = 1;
  609. }
  610. foreach ( sort keys %exe_list ) {
  611. if ( !-f $_ ) {
  612. print STDERR "ERROR: can't find executable $_\n";
  613. $bad = 1;
  614. }
  615. }
  616. if ($bad) {
  617. print STDERR "FATAL ERROR: executables missing\n";
  618. exit(1);
  619. }
  620. elsif ($debug) {
  621. print STDERR "Executable sanity check passed OK\n";
  622. }
  623. }
  624. # Search for all request and response files
  625. sub find_files {
  626. my ( $filter, $dir ) = @_;
  627. my ( $dirh, $testname, $tref );
  628. opendir( $dirh, $dir );
  629. while ( $_ = readdir($dirh) ) {
  630. next if ( $_ eq "." || $_ eq ".." );
  631. $_ = "$dir/$_";
  632. if ( -f "$_" ) {
  633. if (/\/([^\/]*)\.rsp$/) {
  634. $tref = find_test($1, $_);
  635. if ( defined $tref ) {
  636. $testname = $$tref[0];
  637. if ( $$tref[4] eq "" ) {
  638. $$tref[4] = $_;
  639. }
  640. else {
  641. print STDERR
  642. "WARNING: duplicate response file $_ for test $testname\n";
  643. $nbogus++;
  644. }
  645. }
  646. else {
  647. print STDERR "WARNING: bogus file $_\n" unless $no_warn_bogus;
  648. $nbogus++;
  649. }
  650. }
  651. next unless /$filter.*\.req$/i;
  652. if (/\/([^\/]*)\.req$/) {
  653. $tref = find_test($1, $_);
  654. if ( defined $tref ) {
  655. $testname = $$tref[0];
  656. if ( $$tref[3] eq "" ) {
  657. $$tref[3] = $_;
  658. }
  659. else {
  660. print STDERR
  661. "WARNING: duplicate request file $_ for test $testname\n";
  662. $nbogus++;
  663. }
  664. }
  665. elsif ( !/SHAmix\.req$/ ) {
  666. print STDERR "WARNING: unrecognized filename $_\n" unless $no_warn_bogus;
  667. $nbogus++;
  668. }
  669. }
  670. }
  671. elsif ( -d "$_" ) {
  672. find_files( $filter, $_ );
  673. }
  674. }
  675. closedir($dirh);
  676. }
  677. #
  678. # Find test based on filename.
  679. # In ambiguous cases search file contents for a match
  680. #
  681. sub find_test {
  682. my ( $test, $path ) = @_;
  683. foreach $tref (@fips_test_list) {
  684. next unless ref($tref);
  685. my ( $tst, $cmd, $regexp, $req, $resp ) = @$tref;
  686. $tst =~ s/\(.*$//;
  687. if ($tst eq $test) {
  688. return $tref if (!defined $regexp);
  689. my $found = 0;
  690. my $line;
  691. open( IN, $path ) || die "Can't Open File $path";
  692. while ($line = <IN>) {
  693. if ($line =~ /$regexp/i) {
  694. $found = 1;
  695. last;
  696. }
  697. }
  698. close IN;
  699. return $tref if $found == 1;
  700. }
  701. }
  702. return undef;
  703. }
  704. sub sanity_check_files {
  705. my $bad = 0;
  706. foreach (@fips_test_list) {
  707. next unless ref($_);
  708. my ( $tst, $cmd, $regexp, $req, $resp ) = @$_;
  709. #print STDERR "FILES $tst, $cmd, $req, $resp\n";
  710. if ( $req eq "" ) {
  711. print STDERR "WARNING: missing request file for $tst\n" unless $no_warn_missing;
  712. $bad = 1;
  713. next;
  714. }
  715. if ( $verify && $resp eq "" ) {
  716. print STDERR "WARNING: no response file for test $tst\n";
  717. $bad = 1;
  718. }
  719. elsif ( !$verify && $resp ne "" ) {
  720. print STDERR "WARNING: response file $resp will be overwritten\n";
  721. }
  722. }
  723. if ($bad) {
  724. print STDERR "ERROR: test vector file set not complete\n";
  725. exit(1) unless $ignore_missing;
  726. }
  727. if ($nbogus) {
  728. print STDERR
  729. "ERROR: $nbogus bogus or duplicate request and response files\n";
  730. exit(1) unless $ignore_bogus;
  731. }
  732. if ( $debug && !$nbogus && !$bad ) {
  733. print STDERR "test vector file set complete\n";
  734. }
  735. }
  736. sub run_tests {
  737. my ( $verify, $win32, $tprefix, $filter, $tvdir, $outfile ) = @_;
  738. my ( $tname, $tref );
  739. my $bad = 0;
  740. my $lastdir = "";
  741. if ($outfile ne "") {
  742. open OUT, ">$outfile" || die "Can't open $outfile";
  743. }
  744. if ($outfile ne "" && !$minimal_script) {
  745. if ($win32) {
  746. print OUT <<\END;
  747. @echo off
  748. rem Test vector run script
  749. rem Auto generated by fipsalgtest.pl script
  750. rem Do not edit
  751. echo Running Algorithm Tests
  752. END
  753. } else {
  754. $stprefix = $tprefix unless defined $stprefix;
  755. print OUT <<END;
  756. #!/bin/sh
  757. # Test vector run script
  758. # Auto generated by fipsalgtest.pl script
  759. # Do not edit
  760. echo Running Algorithm Tests
  761. RM="$rmcmd";
  762. MKDIR="$mkcmd";
  763. TPREFIX=$stprefix
  764. END
  765. }
  766. }
  767. foreach (@fips_test_list) {
  768. if ( !ref($_) ) {
  769. if ($outfile ne "") {
  770. print "Generating script for $_ tests\n";
  771. print OUT "\n\n\necho \"Running $_ tests\"\n" unless $minimal_script;
  772. } else {
  773. print "Running $_ tests\n" unless $quiet;
  774. }
  775. next;
  776. }
  777. my ( $tname, $tcmd, $regexp, $req, $rsp ) = @$_;
  778. my $out = $rsp;
  779. if ($verify) {
  780. $out =~ s/\.rsp$/.tst/;
  781. }
  782. if ( $req eq "" ) {
  783. print STDERR
  784. "WARNING: Request file for $tname missing: test skipped\n" unless $no_warn_missing;
  785. $skipcnt++;
  786. next;
  787. }
  788. if ( $verify && $rsp eq "" ) {
  789. print STDERR
  790. "WARNING: Response file for $tname missing: test skipped\n";
  791. $skipcnt++;
  792. next;
  793. }
  794. elsif ( !$verify ) {
  795. if ( $rsp ne "" ) {
  796. print STDERR "WARNING: Response file for $tname deleted\n";
  797. unlink $rsp;
  798. }
  799. $out = $req;
  800. $out =~ s|/req/(\S+)\.req|/$rspdir/$1.rsp|;
  801. my $outdir = $out;
  802. $outdir =~ s|/[^/]*$||;
  803. if ($outfile ne "") {
  804. if ($win32) {
  805. $outdir =~ tr|/|\\|;
  806. $req =~ tr|/|\\|;
  807. $out =~ tr|/|\\|;
  808. }
  809. if ($outdir ne $lastdir && !$minimal_script) {
  810. if ($win32) {
  811. print OUT <<END
  812. if exist \"$outdir\" rd /s /q "$outdir"
  813. md \"$outdir\"
  814. END
  815. } else {
  816. print OUT <<END
  817. \$RM \"$outdir\"
  818. \$MKDIR \"$outdir\"
  819. END
  820. }
  821. $lastdir = $outdir;
  822. }
  823. } elsif ( !-d $outdir ) {
  824. print STDERR "DEBUG: Creating directory $outdir\n" if $debug;
  825. mkdir($outdir) || die "Can't create directory $outdir";
  826. }
  827. }
  828. my $cmd = "$tcmd \"$req\" \"$out\"";
  829. print STDERR "DEBUG: running test $tname\n" if ( $debug && !$verify );
  830. if ($outfile ne "") {
  831. print OUT "echo \" running $tname test\"\n" unless $minimal_script;
  832. print OUT "\${TPREFIX}$cmd\n";
  833. } else {
  834. $cmd = "$tprefix$cmd";
  835. system($cmd);
  836. if ( $? != 0 ) {
  837. print STDERR
  838. "WARNING: error executing test $tname for command: $cmd\n";
  839. $runerr++;
  840. next;
  841. }
  842. }
  843. if ($verify) {
  844. if ( exists $verify_special{$tname} ) {
  845. my $vout = $rsp;
  846. $vout =~ s/\.rsp$/.ver/;
  847. $tcmd = $verify_special{$tname};
  848. $cmd = "$tprefix$tcmd ";
  849. $cmd .= "\"$out\" \"$vout\"";
  850. system($cmd);
  851. if ( $? != 0 ) {
  852. print STDERR
  853. "WARNING: error executing verify test $tname $cmd\n";
  854. $scheckrunerr++;
  855. next;
  856. }
  857. my ( $fcount, $pcount ) = ( 0, 0 );
  858. open VER, "$vout";
  859. while (<VER>) {
  860. if (/^Result\s*=\s*(\S*)\s*$/i)
  861. {
  862. if ( $1 eq "F" ) {
  863. $fcount++;
  864. }
  865. else {
  866. $pcount++;
  867. }
  868. }
  869. }
  870. close VER;
  871. unlink $vout;
  872. if ( $fcount || $debug ) {
  873. print STDERR "DEBUG: $tname, Pass=$pcount, Fail=$fcount\n";
  874. }
  875. if ( $fcount || !$pcount ) {
  876. $scheckerr++;
  877. }
  878. else {
  879. $scheckok++;
  880. }
  881. }
  882. elsif ( !cmp_file( $tname, $rsp, $out ) ) {
  883. $cmperr++;
  884. }
  885. else {
  886. $cmpok++;
  887. }
  888. unlink $out;
  889. }
  890. }
  891. if ($outfile ne "") {
  892. print OUT "\n\necho All Tests Completed\n" unless $minimal_script;
  893. close OUT;
  894. }
  895. }
  896. sub cmp_file {
  897. my ( $tname, $rsp, $tst ) = @_;
  898. my ( $rspf, $tstf );
  899. my ( $rspline, $tstline );
  900. if ( !open( $rspf, $rsp ) ) {
  901. print STDERR "ERROR: can't open request file $rsp\n";
  902. return 0;
  903. }
  904. if ( !open( $tstf, $tst ) ) {
  905. print STDERR "ERROR: can't open output file $tst\n";
  906. return 0;
  907. }
  908. for ( ; ; ) {
  909. $rspline = next_line($rspf);
  910. $tstline = next_line($tstf);
  911. if ( !defined($rspline) && !defined($tstline) ) {
  912. print STDERR "DEBUG: $tname file comparison OK\n" if $debug;
  913. return 1;
  914. }
  915. if ( !defined($rspline) ) {
  916. print STDERR "ERROR: $tname EOF on $rsp\n";
  917. return 0;
  918. }
  919. if ( !defined($tstline) ) {
  920. print STDERR "ERROR: $tname EOF on $tst\n";
  921. return 0;
  922. }
  923. # Workaround for bug in RAND des2 test output */
  924. if ( $tstline =~ /^Key2 =/ && $rspline =~ /^Key1 =/ ) {
  925. $rspline =~ s/^Key1/Key2/;
  926. }
  927. if ( $tstline ne $rspline ) {
  928. print STDERR "ERROR: $tname mismatch:\n";
  929. print STDERR "\t \"$tstline\" != \"$rspline\"\n";
  930. return 0;
  931. }
  932. }
  933. return 1;
  934. }
  935. sub next_line {
  936. my ($in) = @_;
  937. while (<$in>) {
  938. chomp;
  939. # Delete comments
  940. s/#.*$//;
  941. # Ignore blank lines
  942. next if (/^\s*$/);
  943. # Translate multiple space into one
  944. s/\s+/ /g;
  945. # Delete trailing whitespace
  946. s/\s+$//;
  947. # Remove leading zeroes
  948. s/= 00/= /;
  949. # Translate to upper case
  950. return uc $_;
  951. }
  952. return undef;
  953. }