fips_test_suite.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  1. /* ====================================================================
  2. * Copyright (c) 2003 The OpenSSL Project. All rights reserved.
  3. *
  4. *
  5. * This command is intended as a test driver for the FIPS-140 testing
  6. * lab performing FIPS-140 validation. It demonstrates the use of the
  7. * OpenSSL library ito perform a variety of common cryptographic
  8. * functions. A power-up self test is demonstrated by deliberately
  9. * pointing to an invalid executable hash
  10. *
  11. * Contributed by Steve Marquess.
  12. *
  13. */
  14. #define OPENSSL_FIPSAPI
  15. #include <stdio.h>
  16. #include <assert.h>
  17. #include <ctype.h>
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include <openssl/evp.h>
  21. #include <openssl/hmac.h>
  22. #include <openssl/cmac.h>
  23. #include <openssl/sha.h>
  24. #include <openssl/err.h>
  25. #include <openssl/bn.h>
  26. #include <openssl/rand.h>
  27. #ifndef OPENSSL_FIPS
  28. int main(int argc, char *argv[])
  29. {
  30. printf("No FIPS support\n");
  31. return(0);
  32. }
  33. #else
  34. #define ERR_clear_error() while(0)
  35. #include <openssl/rsa.h>
  36. #include <openssl/dsa.h>
  37. #include <openssl/dh.h>
  38. #include <openssl/fips.h>
  39. #include <openssl/fips_rand.h>
  40. #include "fips_utl.h"
  41. /* AES: encrypt and decrypt known plaintext, verify result matches original plaintext
  42. */
  43. static int FIPS_aes_test(void)
  44. {
  45. int ret = 0;
  46. unsigned char pltmp[16];
  47. unsigned char citmp[16];
  48. unsigned char key[16] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
  49. unsigned char plaintext[16] = "etaonrishdlcu";
  50. EVP_CIPHER_CTX ctx;
  51. FIPS_cipher_ctx_init(&ctx);
  52. if (FIPS_cipherinit(&ctx, EVP_aes_128_ecb(), key, NULL, 1) <= 0)
  53. goto err;
  54. FIPS_cipher(&ctx, citmp, plaintext, 16);
  55. if (FIPS_cipherinit(&ctx, EVP_aes_128_ecb(), key, NULL, 0) <= 0)
  56. goto err;
  57. FIPS_cipher(&ctx, pltmp, citmp, 16);
  58. if (memcmp(pltmp, plaintext, 16))
  59. goto err;
  60. ret = 1;
  61. err:
  62. FIPS_cipher_ctx_cleanup(&ctx);
  63. return ret;
  64. }
  65. static int FIPS_aes_gcm_test(void)
  66. {
  67. int ret = 0;
  68. unsigned char pltmp[16];
  69. unsigned char citmp[16];
  70. unsigned char tagtmp[16];
  71. unsigned char key[16] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
  72. unsigned char iv[16] = {21,22,23,24,25,26,27,28,29,30,31,32};
  73. unsigned char aad[] = "Some text AAD";
  74. unsigned char plaintext[16] = "etaonrishdlcu";
  75. EVP_CIPHER_CTX ctx;
  76. FIPS_cipher_ctx_init(&ctx);
  77. if (FIPS_cipherinit(&ctx, EVP_aes_128_gcm(), key, iv, 1) <= 0)
  78. goto err;
  79. FIPS_cipher(&ctx, NULL, aad, sizeof(aad));
  80. FIPS_cipher(&ctx, citmp, plaintext, 16);
  81. FIPS_cipher(&ctx, NULL, NULL, 0);
  82. if (!FIPS_cipher_ctx_ctrl(&ctx, EVP_CTRL_GCM_GET_TAG, 16, tagtmp))
  83. goto err;
  84. if (FIPS_cipherinit(&ctx, EVP_aes_128_gcm(), key, iv, 0) <= 0)
  85. goto err;
  86. if (!FIPS_cipher_ctx_ctrl(&ctx, EVP_CTRL_GCM_SET_TAG, 16, tagtmp))
  87. goto err;
  88. FIPS_cipher(&ctx, NULL, aad, sizeof(aad));
  89. FIPS_cipher(&ctx, pltmp, citmp, 16);
  90. if (FIPS_cipher(&ctx, NULL, NULL, 0) < 0)
  91. goto err;
  92. if (memcmp(pltmp, plaintext, 16))
  93. goto err;
  94. ret = 1;
  95. err:
  96. FIPS_cipher_ctx_cleanup(&ctx);
  97. return ret;
  98. }
  99. static int FIPS_des3_test(void)
  100. {
  101. int ret = 0;
  102. unsigned char pltmp[8];
  103. unsigned char citmp[8];
  104. unsigned char key[] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,
  105. 19,20,21,22,23,24};
  106. unsigned char plaintext[] = { 'e', 't', 'a', 'o', 'n', 'r', 'i', 's' };
  107. EVP_CIPHER_CTX ctx;
  108. FIPS_cipher_ctx_init(&ctx);
  109. if (FIPS_cipherinit(&ctx, EVP_des_ede3_ecb(), key, NULL, 1) <= 0)
  110. goto err;
  111. FIPS_cipher(&ctx, citmp, plaintext, 8);
  112. if (FIPS_cipherinit(&ctx, EVP_des_ede3_ecb(), key, NULL, 0) <= 0)
  113. goto err;
  114. FIPS_cipher(&ctx, pltmp, citmp, 8);
  115. if (memcmp(pltmp, plaintext, 8))
  116. goto err;
  117. ret = 1;
  118. err:
  119. FIPS_cipher_ctx_cleanup(&ctx);
  120. return ret;
  121. }
  122. /*
  123. * DSA: generate keys and sign, verify input plaintext.
  124. */
  125. static int FIPS_dsa_test(int bad)
  126. {
  127. DSA *dsa = NULL;
  128. unsigned char dgst[] = "etaonrishdlc";
  129. int r = 0;
  130. DSA_SIG *sig = NULL;
  131. ERR_clear_error();
  132. dsa = FIPS_dsa_new();
  133. if (!dsa)
  134. goto end;
  135. if (!DSA_generate_parameters_ex(dsa, 1024,NULL,0,NULL,NULL,NULL))
  136. goto end;
  137. if (!DSA_generate_key(dsa))
  138. goto end;
  139. if (bad)
  140. BN_add_word(dsa->pub_key, 1);
  141. sig = FIPS_dsa_sign(dsa, dgst, sizeof(dgst) -1, EVP_sha256());
  142. if (!sig)
  143. goto end;
  144. r = FIPS_dsa_verify(dsa, dgst, sizeof(dgst) -1, EVP_sha256(), sig);
  145. end:
  146. if (sig)
  147. FIPS_dsa_sig_free(sig);
  148. if (dsa)
  149. FIPS_dsa_free(dsa);
  150. if (r != 1)
  151. return 0;
  152. return 1;
  153. }
  154. /*
  155. * RSA: generate keys and sign, verify input plaintext.
  156. */
  157. static int FIPS_rsa_test(int bad)
  158. {
  159. RSA *key;
  160. unsigned char input_ptext[] = "etaonrishdlc";
  161. unsigned char buf[256];
  162. unsigned int slen;
  163. BIGNUM *bn;
  164. int r = 0;
  165. ERR_clear_error();
  166. key = FIPS_rsa_new();
  167. bn = BN_new();
  168. if (!key || !bn)
  169. return 0;
  170. BN_set_word(bn, 65537);
  171. if (!RSA_generate_key_ex(key, 2048,bn,NULL))
  172. return 0;
  173. BN_free(bn);
  174. if (bad)
  175. BN_add_word(key->n, 1);
  176. if (!FIPS_rsa_sign(key, input_ptext, sizeof(input_ptext) - 1, EVP_sha256(),
  177. RSA_PKCS1_PADDING, 0, NULL, buf, &slen))
  178. goto end;
  179. r = FIPS_rsa_verify(key, input_ptext, sizeof(input_ptext) - 1, EVP_sha256(),
  180. RSA_PKCS1_PADDING, 0, NULL, buf, slen);
  181. end:
  182. if (key)
  183. FIPS_rsa_free(key);
  184. if (r != 1)
  185. return 0;
  186. return 1;
  187. }
  188. /* SHA1: generate hash of known digest value and compare to known
  189. precomputed correct hash
  190. */
  191. static int FIPS_sha1_test()
  192. {
  193. unsigned char digest[SHA_DIGEST_LENGTH] =
  194. { 0x11, 0xf1, 0x9a, 0x3a, 0xec, 0x1a, 0x1e, 0x8e, 0x65, 0xd4, 0x9a, 0x38, 0x0c, 0x8b, 0x1e, 0x2c, 0xe8, 0xb3, 0xc5, 0x18 };
  195. unsigned char str[] = "etaonrishd";
  196. unsigned char md[SHA_DIGEST_LENGTH];
  197. ERR_clear_error();
  198. if (!FIPS_digest(str,sizeof(str) - 1,md, NULL, EVP_sha1())) return 0;
  199. if (memcmp(md,digest,sizeof(md)))
  200. return 0;
  201. return 1;
  202. }
  203. /* SHA256: generate hash of known digest value and compare to known
  204. precomputed correct hash
  205. */
  206. static int FIPS_sha256_test()
  207. {
  208. unsigned char digest[SHA256_DIGEST_LENGTH] =
  209. {0xf5, 0x53, 0xcd, 0xb8, 0xcf, 0x1, 0xee, 0x17, 0x9b, 0x93, 0xc9, 0x68, 0xc0, 0xea, 0x40, 0x91,
  210. 0x6, 0xec, 0x8e, 0x11, 0x96, 0xc8, 0x5d, 0x1c, 0xaf, 0x64, 0x22, 0xe6, 0x50, 0x4f, 0x47, 0x57};
  211. unsigned char str[] = "etaonrishd";
  212. unsigned char md[SHA256_DIGEST_LENGTH];
  213. ERR_clear_error();
  214. if (!FIPS_digest(str,sizeof(str) - 1,md, NULL, EVP_sha256())) return 0;
  215. if (memcmp(md,digest,sizeof(md)))
  216. return 0;
  217. return 1;
  218. }
  219. /* SHA512: generate hash of known digest value and compare to known
  220. precomputed correct hash
  221. */
  222. static int FIPS_sha512_test()
  223. {
  224. unsigned char digest[SHA512_DIGEST_LENGTH] =
  225. {0x99, 0xc9, 0xe9, 0x5b, 0x88, 0xd4, 0x78, 0x88, 0xdf, 0x88, 0x5f, 0x94, 0x71, 0x64, 0x28, 0xca,
  226. 0x16, 0x1f, 0x3d, 0xf4, 0x1f, 0xf3, 0x0f, 0xc5, 0x03, 0x99, 0xb2, 0xd0, 0xe7, 0x0b, 0x94, 0x4a,
  227. 0x45, 0xd2, 0x6c, 0x4f, 0x20, 0x06, 0xef, 0x71, 0xa9, 0x25, 0x7f, 0x24, 0xb1, 0xd9, 0x40, 0x22,
  228. 0x49, 0x54, 0x10, 0xc2, 0x22, 0x9d, 0x27, 0xfe, 0xbd, 0xd6, 0xd6, 0xeb, 0x2d, 0x42, 0x1d, 0xa3};
  229. unsigned char str[] = "etaonrishd";
  230. unsigned char md[SHA512_DIGEST_LENGTH];
  231. ERR_clear_error();
  232. if (!FIPS_digest(str,sizeof(str) - 1,md, NULL, EVP_sha512())) return 0;
  233. if (memcmp(md,digest,sizeof(md)))
  234. return 0;
  235. return 1;
  236. }
  237. /* HMAC-SHA1: generate hash of known digest value and compare to known
  238. precomputed correct hash
  239. */
  240. static int FIPS_hmac_sha1_test()
  241. {
  242. unsigned char key[] = "etaonrishd";
  243. unsigned char iv[] = "Sample text";
  244. unsigned char kaval[EVP_MAX_MD_SIZE] =
  245. {0x73, 0xf7, 0xa0, 0x48, 0xf8, 0x94, 0xed, 0xdd, 0x0a, 0xea, 0xea, 0x56, 0x1b, 0x61, 0x2e, 0x70,
  246. 0xb2, 0xfb, 0xec, 0xc6};
  247. unsigned char out[EVP_MAX_MD_SIZE];
  248. unsigned int outlen;
  249. ERR_clear_error();
  250. if (!HMAC(EVP_sha1(),key,sizeof(key)-1,iv,sizeof(iv)-1,out,&outlen)) return 0;
  251. if (memcmp(out,kaval,outlen))
  252. return 0;
  253. return 1;
  254. }
  255. /* HMAC-SHA224: generate hash of known digest value and compare to known
  256. precomputed correct hash
  257. */
  258. static int FIPS_hmac_sha224_test()
  259. {
  260. unsigned char key[] = "etaonrishd";
  261. unsigned char iv[] = "Sample text";
  262. unsigned char kaval[EVP_MAX_MD_SIZE] =
  263. {0x75, 0x58, 0xd5, 0xbd, 0x55, 0x6d, 0x87, 0x0f, 0x75, 0xff, 0xbe, 0x1c, 0xb2, 0xf0, 0x20, 0x35,
  264. 0xe5, 0x62, 0x49, 0xb6, 0x94, 0xb9, 0xfc, 0x65, 0x34, 0x33, 0x3a, 0x19};
  265. unsigned char out[EVP_MAX_MD_SIZE];
  266. unsigned int outlen;
  267. ERR_clear_error();
  268. if (!HMAC(EVP_sha224(),key,sizeof(key)-1,iv,sizeof(iv)-1,out,&outlen)) return 0;
  269. if (memcmp(out,kaval,outlen))
  270. return 0;
  271. return 1;
  272. }
  273. /* HMAC-SHA256: generate hash of known digest value and compare to known
  274. precomputed correct hash
  275. */
  276. static int FIPS_hmac_sha256_test()
  277. {
  278. unsigned char key[] = "etaonrishd";
  279. unsigned char iv[] = "Sample text";
  280. unsigned char kaval[EVP_MAX_MD_SIZE] =
  281. {0xe9, 0x17, 0xc1, 0x7b, 0x4c, 0x6b, 0x77, 0xda, 0xd2, 0x30, 0x36, 0x02, 0xf5, 0x72, 0x33, 0x87,
  282. 0x9f, 0xc6, 0x6e, 0x7b, 0x7e, 0xa8, 0xea, 0xaa, 0x9f, 0xba, 0xee, 0x51, 0xff, 0xda, 0x24, 0xf4};
  283. unsigned char out[EVP_MAX_MD_SIZE];
  284. unsigned int outlen;
  285. ERR_clear_error();
  286. if (!HMAC(EVP_sha256(),key,sizeof(key)-1,iv,sizeof(iv)-1,out,&outlen)) return 0;
  287. if (memcmp(out,kaval,outlen))
  288. return 0;
  289. return 1;
  290. }
  291. /* HMAC-SHA384: generate hash of known digest value and compare to known
  292. precomputed correct hash
  293. */
  294. static int FIPS_hmac_sha384_test()
  295. {
  296. unsigned char key[] = "etaonrishd";
  297. unsigned char iv[] = "Sample text";
  298. unsigned char kaval[EVP_MAX_MD_SIZE] =
  299. {0xb2, 0x9d, 0x40, 0x58, 0x32, 0xc4, 0xe3, 0x31, 0xb6, 0x63, 0x08, 0x26, 0x99, 0xef, 0x3b, 0x10,
  300. 0xe2, 0xdf, 0xf8, 0xff, 0xc6, 0xe1, 0x03, 0x29, 0x81, 0x2a, 0x1b, 0xac, 0xb0, 0x07, 0x39, 0x08,
  301. 0xf3, 0x91, 0x35, 0x11, 0x76, 0xd6, 0x4c, 0x20, 0xfb, 0x4d, 0xc3, 0xf3, 0xb8, 0x9b, 0x88, 0x1c};
  302. unsigned char out[EVP_MAX_MD_SIZE];
  303. unsigned int outlen;
  304. ERR_clear_error();
  305. if (!HMAC(EVP_sha384(),key,sizeof(key)-1,iv,sizeof(iv)-1,out,&outlen)) return 0;
  306. if (memcmp(out,kaval,outlen))
  307. return 0;
  308. return 1;
  309. }
  310. /* HMAC-SHA512: generate hash of known digest value and compare to known
  311. precomputed correct hash
  312. */
  313. static int FIPS_hmac_sha512_test()
  314. {
  315. unsigned char key[] = "etaonrishd";
  316. unsigned char iv[] = "Sample text";
  317. unsigned char kaval[EVP_MAX_MD_SIZE] =
  318. {0xcd, 0x3e, 0xb9, 0x51, 0xb8, 0xbc, 0x7f, 0x9a, 0x23, 0xaf, 0xf3, 0x77, 0x59, 0x85, 0xa9, 0xe6,
  319. 0xf7, 0xd1, 0x51, 0x96, 0x17, 0xe0, 0x92, 0xd8, 0xa6, 0x3b, 0xc1, 0xad, 0x7e, 0x24, 0xca, 0xb1,
  320. 0xd7, 0x79, 0x0a, 0xa5, 0xea, 0x2c, 0x02, 0x58, 0x0b, 0xa6, 0x52, 0x6b, 0x61, 0x7f, 0xeb, 0x9c,
  321. 0x47, 0x86, 0x5d, 0x74, 0x2b, 0x88, 0xdf, 0xee, 0x46, 0x69, 0x96, 0x3d, 0xa6, 0xd9, 0x2a, 0x53};
  322. unsigned char out[EVP_MAX_MD_SIZE];
  323. unsigned int outlen;
  324. ERR_clear_error();
  325. if (!HMAC(EVP_sha512(),key,sizeof(key)-1,iv,sizeof(iv)-1,out,&outlen)) return 0;
  326. if (memcmp(out,kaval,outlen))
  327. return 0;
  328. return 1;
  329. }
  330. /* CMAC-AES128: generate hash of known digest value and compare to known
  331. precomputed correct hash
  332. */
  333. static int FIPS_cmac_aes128_test()
  334. {
  335. unsigned char key[16] = { 0x2b,0x7e,0x15,0x16, 0x28,0xae,0xd2,0xa6,
  336. 0xab,0xf7,0x15,0x88, 0x09,0xcf,0x4f,0x3c, };
  337. unsigned char data[] = "Sample text";
  338. unsigned char kaval[EVP_MAX_MD_SIZE] =
  339. { 0x16,0x83,0xfe,0xac, 0x52,0x9b,0xae,0x23,
  340. 0xd7,0xd5,0x66,0xf5, 0xd2,0x8d,0xbd,0x2a, };
  341. unsigned char *out = NULL;
  342. size_t outlen;
  343. CMAC_CTX *ctx = CMAC_CTX_new();
  344. int r = 0;
  345. ERR_clear_error();
  346. if (!ctx)
  347. goto end;
  348. if (!CMAC_Init(ctx,key,sizeof(key),EVP_aes_128_cbc(),NULL))
  349. goto end;
  350. if (!CMAC_Update(ctx,data,sizeof(data)-1))
  351. goto end;
  352. /* This should return 1. If not, there's a programming error... */
  353. if (!CMAC_Final(ctx, out, &outlen))
  354. goto end;
  355. out = OPENSSL_malloc(outlen);
  356. if (!CMAC_Final(ctx, out, &outlen))
  357. goto end;
  358. #if 0
  359. {
  360. char *hexout = OPENSSL_malloc(outlen * 2 + 1);
  361. bin2hex(out, outlen, hexout);
  362. printf("CMAC-AES128: res = %s\n", hexout);
  363. OPENSSL_free(hexout);
  364. }
  365. r = 1;
  366. #else
  367. if (!memcmp(out,kaval,outlen))
  368. r = 1;
  369. #endif
  370. end:
  371. CMAC_CTX_free(ctx);
  372. if (out)
  373. OPENSSL_free(out);
  374. return r;
  375. }
  376. /* CMAC-AES192: generate hash of known digest value and compare to known
  377. precomputed correct hash
  378. */
  379. static int FIPS_cmac_aes192_test()
  380. {
  381. unsigned char key[] = { 0x8e,0x73,0xb0,0xf7, 0xda,0x0e,0x64,0x52,
  382. 0xc8,0x10,0xf3,0x2b, 0x80,0x90,0x79,0xe5,
  383. 0x62,0xf8,0xea,0xd2, 0x52,0x2c,0x6b,0x7b, };
  384. unsigned char data[] = "Sample text";
  385. unsigned char kaval[] =
  386. { 0xd6,0x99,0x19,0x25, 0xe5,0x1d,0x95,0x48,
  387. 0xb1,0x4a,0x0b,0xf2, 0xc6,0x3c,0x47,0x1f, };
  388. unsigned char *out = NULL;
  389. size_t outlen;
  390. CMAC_CTX *ctx = CMAC_CTX_new();
  391. int r = 0;
  392. ERR_clear_error();
  393. if (!ctx)
  394. goto end;
  395. if (!CMAC_Init(ctx,key,sizeof(key),EVP_aes_192_cbc(),NULL))
  396. goto end;
  397. if (!CMAC_Update(ctx,data,sizeof(data)-1))
  398. goto end;
  399. /* This should return 1. If not, there's a programming error... */
  400. if (!CMAC_Final(ctx, out, &outlen))
  401. goto end;
  402. out = OPENSSL_malloc(outlen);
  403. if (!CMAC_Final(ctx, out, &outlen))
  404. goto end;
  405. #if 0
  406. {
  407. char *hexout = OPENSSL_malloc(outlen * 2 + 1);
  408. bin2hex(out, outlen, hexout);
  409. printf("CMAC-AES192: res = %s\n", hexout);
  410. OPENSSL_free(hexout);
  411. }
  412. r = 1;
  413. #else
  414. if (!memcmp(out,kaval,outlen))
  415. r = 1;
  416. #endif
  417. end:
  418. CMAC_CTX_free(ctx);
  419. if (out)
  420. OPENSSL_free(out);
  421. return r;
  422. }
  423. /* CMAC-AES256: generate hash of known digest value and compare to known
  424. precomputed correct hash
  425. */
  426. static int FIPS_cmac_aes256_test()
  427. {
  428. unsigned char key[] = { 0x60,0x3d,0xeb,0x10, 0x15,0xca,0x71,0xbe,
  429. 0x2b,0x73,0xae,0xf0, 0x85,0x7d,0x77,0x81,
  430. 0x1f,0x35,0x2c,0x07, 0x3b,0x61,0x08,0xd7,
  431. 0x2d,0x98,0x10,0xa3, 0x09,0x14,0xdf,0xf4, };
  432. unsigned char data[] = "Sample text";
  433. unsigned char kaval[] =
  434. { 0xec,0xc2,0xcf,0x63, 0xc7,0xce,0xfc,0xa4,
  435. 0xb0,0x86,0x37,0x5f, 0x15,0x60,0xba,0x1f, };
  436. unsigned char *out = NULL;
  437. size_t outlen;
  438. CMAC_CTX *ctx = CMAC_CTX_new();
  439. int r = 0;
  440. ERR_clear_error();
  441. if (!ctx)
  442. goto end;
  443. if (!CMAC_Init(ctx,key,sizeof(key),EVP_aes_256_cbc(),NULL))
  444. goto end;
  445. if (!CMAC_Update(ctx,data,sizeof(data)-1))
  446. goto end;
  447. /* This should return 1. If not, there's a programming error... */
  448. if (!CMAC_Final(ctx, out, &outlen))
  449. goto end;
  450. out = OPENSSL_malloc(outlen);
  451. if (!CMAC_Final(ctx, out, &outlen))
  452. goto end;
  453. #if 0
  454. {
  455. char *hexout = OPENSSL_malloc(outlen * 2 + 1);
  456. bin2hex(out, outlen, hexout);
  457. printf("CMAC-AES256: res = %s\n", hexout);
  458. OPENSSL_free(hexout);
  459. }
  460. r = 1;
  461. #else
  462. if (!memcmp(out,kaval,outlen))
  463. r = 1;
  464. #endif
  465. end:
  466. CMAC_CTX_free(ctx);
  467. if (out)
  468. OPENSSL_free(out);
  469. return r;
  470. }
  471. /* CMAC-TDEA3: generate hash of known digest value and compare to known
  472. precomputed correct hash
  473. */
  474. static int FIPS_cmac_tdea3_test()
  475. {
  476. unsigned char key[] = { 0x8a,0xa8,0x3b,0xf8, 0xcb,0xda,0x10,0x62,
  477. 0x0b,0xc1,0xbf,0x19, 0xfb,0xb6,0xcd,0x58,
  478. 0xbc,0x31,0x3d,0x4a, 0x37,0x1c,0xa8,0xb5, };
  479. unsigned char data[] = "Sample text";
  480. unsigned char kaval[EVP_MAX_MD_SIZE] =
  481. { 0xb4,0x06,0x4e,0xbf, 0x59,0x89,0xba,0x68, };
  482. unsigned char *out = NULL;
  483. size_t outlen;
  484. CMAC_CTX *ctx = CMAC_CTX_new();
  485. int r = 0;
  486. ERR_clear_error();
  487. if (!ctx)
  488. goto end;
  489. if (!CMAC_Init(ctx,key,sizeof(key),EVP_des_ede3_cbc(),NULL))
  490. goto end;
  491. if (!CMAC_Update(ctx,data,sizeof(data)-1))
  492. goto end;
  493. /* This should return 1. If not, there's a programming error... */
  494. if (!CMAC_Final(ctx, out, &outlen))
  495. goto end;
  496. out = OPENSSL_malloc(outlen);
  497. if (!CMAC_Final(ctx, out, &outlen))
  498. goto end;
  499. #if 0
  500. {
  501. char *hexout = OPENSSL_malloc(outlen * 2 + 1);
  502. bin2hex(out, outlen, hexout);
  503. printf("CMAC-TDEA3: res = %s\n", hexout);
  504. OPENSSL_free(hexout);
  505. }
  506. r = 1;
  507. #else
  508. if (!memcmp(out,kaval,outlen))
  509. r = 1;
  510. #endif
  511. end:
  512. CMAC_CTX_free(ctx);
  513. if (out)
  514. OPENSSL_free(out);
  515. return r;
  516. }
  517. /* DH: generate shared parameters
  518. */
  519. static int dh_test()
  520. {
  521. DH *dh;
  522. ERR_clear_error();
  523. dh = FIPS_dh_new();
  524. if (!dh)
  525. return 0;
  526. if (!DH_generate_parameters_ex(dh, 1024, 2, NULL))
  527. return 0;
  528. FIPS_dh_free(dh);
  529. return 1;
  530. }
  531. /* Zeroize
  532. */
  533. static int Zeroize()
  534. {
  535. RSA *key;
  536. BIGNUM *bn;
  537. unsigned char userkey[16] =
  538. { 0x48, 0x50, 0xf0, 0xa3, 0x3a, 0xed, 0xd3, 0xaf, 0x6e, 0x47, 0x7f, 0x83, 0x02, 0xb1, 0x09, 0x68 };
  539. size_t i;
  540. int n;
  541. key = FIPS_rsa_new();
  542. bn = BN_new();
  543. if (!key || !bn)
  544. return 0;
  545. BN_set_word(bn, 65537);
  546. if (!RSA_generate_key_ex(key, 1024,bn,NULL))
  547. return 0;
  548. BN_free(bn);
  549. n = BN_num_bytes(key->d);
  550. printf(" Generated %d byte RSA private key\n", n);
  551. printf("\tBN key before overwriting:\n");
  552. do_bn_print(stdout, key->d);
  553. BN_rand(key->d,n*8,-1,0);
  554. printf("\tBN key after overwriting:\n");
  555. do_bn_print(stdout, key->d);
  556. printf("\tchar buffer key before overwriting: \n\t\t");
  557. for(i = 0; i < sizeof(userkey); i++) printf("%02x", userkey[i]);
  558. printf("\n");
  559. RAND_bytes(userkey, sizeof userkey);
  560. printf("\tchar buffer key after overwriting: \n\t\t");
  561. for(i = 0; i < sizeof(userkey); i++) printf("%02x", userkey[i]);
  562. printf("\n");
  563. FIPS_rsa_free(key);
  564. return 1;
  565. }
  566. /* Dummy Entropy for DRBG tests. WARNING: THIS IS TOTALLY BOGUS
  567. * HAS ZERO SECURITY AND MUST NOT BE USED IN REAL APPLICATIONS.
  568. */
  569. static unsigned char dummy_drbg_entropy[1024];
  570. static size_t drbg_test_cb(DRBG_CTX *ctx, unsigned char **pout,
  571. int entropy, size_t min_len, size_t max_len)
  572. {
  573. *pout = dummy_drbg_entropy;
  574. /* Round up to multiple of block size */
  575. return (min_len + 0xf) & ~0xf;
  576. }
  577. /* DRBG test: just generate lots of data and trigger health checks */
  578. static int do_drbg_test(int type, int flags)
  579. {
  580. DRBG_CTX *dctx;
  581. int rv = 0;
  582. size_t i;
  583. unsigned char randout[1024];
  584. dctx = FIPS_drbg_new(type, flags);
  585. if (!dctx)
  586. return 0;
  587. FIPS_drbg_set_callbacks(dctx, drbg_test_cb, 0, 0x10, drbg_test_cb, 0);
  588. for (i = 0; i < sizeof(dummy_drbg_entropy); i++)
  589. {
  590. dummy_drbg_entropy[i] = i & 0xff;
  591. }
  592. if (!FIPS_drbg_instantiate(dctx, dummy_drbg_entropy, 10))
  593. goto err;
  594. FIPS_drbg_set_check_interval(dctx, 10);
  595. for (i = 0; i < 32; i++)
  596. {
  597. if (!FIPS_drbg_generate(dctx, randout, sizeof(randout), 0, NULL, 0))
  598. goto err;
  599. if (!FIPS_drbg_generate(dctx, randout, sizeof(randout), 0, dummy_drbg_entropy, 1))
  600. goto err;
  601. }
  602. rv = 1;
  603. err:
  604. FIPS_drbg_free(dctx);
  605. return rv;
  606. }
  607. typedef struct
  608. {
  609. int type, flags;
  610. } DRBG_LIST;
  611. static int do_drbg_all(void)
  612. {
  613. static DRBG_LIST drbg_types[] =
  614. {
  615. {NID_sha1, 0},
  616. {NID_sha224, 0},
  617. {NID_sha256, 0},
  618. {NID_sha384, 0},
  619. {NID_sha512, 0},
  620. {NID_hmacWithSHA1, 0},
  621. {NID_hmacWithSHA224, 0},
  622. {NID_hmacWithSHA256, 0},
  623. {NID_hmacWithSHA384, 0},
  624. {NID_hmacWithSHA512, 0},
  625. {NID_aes_128_ctr, 0},
  626. {NID_aes_192_ctr, 0},
  627. {NID_aes_256_ctr, 0},
  628. {NID_aes_128_ctr, DRBG_FLAG_CTR_USE_DF},
  629. {NID_aes_192_ctr, DRBG_FLAG_CTR_USE_DF},
  630. {NID_aes_256_ctr, DRBG_FLAG_CTR_USE_DF},
  631. {(NID_X9_62_prime256v1 << 16)|NID_sha1, 0},
  632. {(NID_X9_62_prime256v1 << 16)|NID_sha224, 0},
  633. {(NID_X9_62_prime256v1 << 16)|NID_sha256, 0},
  634. {(NID_X9_62_prime256v1 << 16)|NID_sha384, 0},
  635. {(NID_X9_62_prime256v1 << 16)|NID_sha512, 0},
  636. {(NID_secp384r1 << 16)|NID_sha224, 0},
  637. {(NID_secp384r1 << 16)|NID_sha256, 0},
  638. {(NID_secp384r1 << 16)|NID_sha384, 0},
  639. {(NID_secp384r1 << 16)|NID_sha512, 0},
  640. {(NID_secp521r1 << 16)|NID_sha256, 0},
  641. {(NID_secp521r1 << 16)|NID_sha384, 0},
  642. {(NID_secp521r1 << 16)|NID_sha512, 0},
  643. {0, 0}
  644. };
  645. DRBG_LIST *lst;
  646. int rv = 1;
  647. for (lst = drbg_types;; lst++)
  648. {
  649. if (lst->type == 0)
  650. break;
  651. if (!do_drbg_test(lst->type, lst->flags))
  652. rv = 0;
  653. }
  654. return rv;
  655. }
  656. static int Error;
  657. static const char * Fail(const char *msg)
  658. {
  659. Error++;
  660. return msg;
  661. }
  662. static void test_msg(const char *msg, int result)
  663. {
  664. printf("%s...%s\n", msg, result ? "successful" : Fail("Failed!"));
  665. }
  666. /* Table of IDs for POST translating between NIDs and names */
  667. typedef struct
  668. {
  669. int id;
  670. const char *name;
  671. } POST_ID;
  672. POST_ID id_list[] = {
  673. {NID_sha1, "SHA1"},
  674. {NID_sha224, "SHA224"},
  675. {NID_sha256, "SHA256"},
  676. {NID_sha384, "SHA384"},
  677. {NID_sha512, "SHA512"},
  678. {NID_hmacWithSHA1, "HMAC-SHA1"},
  679. {NID_hmacWithSHA224, "HMAC-SHA224"},
  680. {NID_hmacWithSHA256, "HMAC-SHA256"},
  681. {NID_hmacWithSHA384, "HMAC-SHA384"},
  682. {NID_hmacWithSHA512, "HMAC-SHA512"},
  683. {EVP_PKEY_RSA, "RSA"},
  684. {EVP_PKEY_DSA, "DSA"},
  685. {EVP_PKEY_EC, "ECDSA"},
  686. {NID_aes_128_cbc, "AES-128-CBC"},
  687. {NID_aes_192_cbc, "AES-192-CBC"},
  688. {NID_aes_256_cbc, "AES-256-CBC"},
  689. {NID_aes_128_ctr, "AES-128-CTR"},
  690. {NID_aes_192_ctr, "AES-192-CTR"},
  691. {NID_aes_256_ctr, "AES-256-CTR"},
  692. {NID_aes_128_ecb, "AES-128-ECB"},
  693. {NID_aes_128_xts, "AES-128-XTS"},
  694. {NID_aes_256_xts, "AES-256-XTS"},
  695. {NID_des_ede3_cbc, "DES-EDE3-CBC"},
  696. {NID_des_ede3_ecb, "DES-EDE3-ECB"},
  697. {NID_secp224r1, "P-224"},
  698. {NID_sect233r1, "B-233"},
  699. {NID_sect233k1, "K-233"},
  700. {NID_X9_62_prime256v1, "P-256"},
  701. {NID_secp384r1, "P-384"},
  702. {NID_secp521r1, "P-521"},
  703. {0, NULL}
  704. };
  705. static const char *lookup_id(int id)
  706. {
  707. POST_ID *n;
  708. static char out[40];
  709. for (n = id_list; n->name; n++)
  710. {
  711. if (n->id == id)
  712. return n->name;
  713. }
  714. sprintf(out, "ID=%d", id);
  715. return out;
  716. }
  717. static int fail_id = -1;
  718. static int fail_sub = -1;
  719. static int fail_key = -1;
  720. static int post_cb(int op, int id, int subid, void *ex)
  721. {
  722. const char *idstr, *exstr = "";
  723. char asctmp[20];
  724. int keytype = -1;
  725. #ifdef FIPS_POST_TIME
  726. static struct timespec start, end, tstart, tend;
  727. #endif
  728. switch(id)
  729. {
  730. case FIPS_TEST_INTEGRITY:
  731. idstr = "Integrity";
  732. break;
  733. case FIPS_TEST_DIGEST:
  734. idstr = "Digest";
  735. exstr = lookup_id(subid);
  736. break;
  737. case FIPS_TEST_CIPHER:
  738. exstr = lookup_id(subid);
  739. idstr = "Cipher";
  740. break;
  741. case FIPS_TEST_SIGNATURE:
  742. if (ex)
  743. {
  744. EVP_PKEY *pkey = ex;
  745. keytype = pkey->type;
  746. if (keytype == EVP_PKEY_EC)
  747. {
  748. const EC_GROUP *grp;
  749. int cnid;
  750. grp = EC_KEY_get0_group(pkey->pkey.ec);
  751. cnid = EC_GROUP_get_curve_name(grp);
  752. sprintf(asctmp, "ECDSA %s", lookup_id(cnid));
  753. exstr = asctmp;
  754. }
  755. else
  756. exstr = lookup_id(keytype);
  757. }
  758. idstr = "Signature";
  759. break;
  760. case FIPS_TEST_HMAC:
  761. exstr = lookup_id(subid);
  762. idstr = "HMAC";
  763. break;
  764. case FIPS_TEST_CMAC:
  765. idstr = "CMAC";
  766. exstr = lookup_id(subid);
  767. break;
  768. case FIPS_TEST_GCM:
  769. idstr = "GCM";
  770. break;
  771. case FIPS_TEST_XTS:
  772. idstr = "XTS";
  773. exstr = lookup_id(subid);
  774. break;
  775. case FIPS_TEST_CCM:
  776. idstr = "CCM";
  777. break;
  778. case FIPS_TEST_X931:
  779. idstr = "X9.31 PRNG";
  780. sprintf(asctmp, "keylen=%d", subid);
  781. exstr = asctmp;
  782. break;
  783. case FIPS_TEST_DRBG:
  784. idstr = "DRBG";
  785. if (*(int *)ex & DRBG_FLAG_CTR_USE_DF)
  786. {
  787. sprintf(asctmp, "%s DF", lookup_id(subid));
  788. exstr = asctmp;
  789. }
  790. else if (subid >> 16)
  791. {
  792. sprintf(asctmp, "%s %s",
  793. lookup_id(subid >> 16),
  794. lookup_id(subid & 0xFFFF));
  795. exstr = asctmp;
  796. }
  797. else
  798. exstr = lookup_id(subid);
  799. break;
  800. case FIPS_TEST_PAIRWISE:
  801. if (ex)
  802. {
  803. EVP_PKEY *pkey = ex;
  804. keytype = pkey->type;
  805. exstr = lookup_id(keytype);
  806. }
  807. idstr = "Pairwise Consistency";
  808. break;
  809. case FIPS_TEST_CONTINUOUS:
  810. idstr = "Continuous PRNG";
  811. break;
  812. case FIPS_TEST_ECDH:
  813. idstr = "ECDH";
  814. exstr = lookup_id(subid);
  815. break;
  816. default:
  817. idstr = "Unknown";
  818. break;
  819. }
  820. switch(op)
  821. {
  822. case FIPS_POST_BEGIN:
  823. #ifdef FIPS_POST_TIME
  824. clock_getres(CLOCK_REALTIME, &tstart);
  825. printf("\tTimer resolution %ld s, %ld ns\n",
  826. (long)tstart.tv_sec, (long)tstart.tv_nsec);
  827. clock_gettime(CLOCK_REALTIME, &tstart);
  828. #endif
  829. printf("\tPOST started\n");
  830. break;
  831. case FIPS_POST_END:
  832. printf("\tPOST %s\n", id ? "Success" : "Failed");
  833. #ifdef FIPS_POST_TIME
  834. clock_gettime(CLOCK_REALTIME, &tend);
  835. printf("\t\tTook %f seconds\n",
  836. (double)((tend.tv_sec+tend.tv_nsec*1e-9)
  837. - (tstart.tv_sec+tstart.tv_nsec*1e-9)));
  838. #endif
  839. break;
  840. case FIPS_POST_STARTED:
  841. printf("\t\t%s %s test started\n", idstr, exstr);
  842. #ifdef FIPS_POST_TIME
  843. clock_gettime(CLOCK_REALTIME, &start);
  844. #endif
  845. break;
  846. case FIPS_POST_SUCCESS:
  847. printf("\t\t%s %s test OK\n", idstr, exstr);
  848. #ifdef FIPS_POST_TIME
  849. clock_gettime(CLOCK_REALTIME, &end);
  850. printf("\t\t\tTook %f seconds\n",
  851. (double)((end.tv_sec+end.tv_nsec*1e-9)
  852. - (start.tv_sec+start.tv_nsec*1e-9)));
  853. #endif
  854. break;
  855. case FIPS_POST_FAIL:
  856. printf("\t\t%s %s test FAILED!!\n", idstr, exstr);
  857. break;
  858. case FIPS_POST_CORRUPT:
  859. if (fail_id == id
  860. && (fail_key == -1 || fail_key == keytype)
  861. && (fail_sub == -1 || fail_sub == subid))
  862. {
  863. printf("\t\t%s %s test failure induced\n", idstr, exstr);
  864. return 0;
  865. }
  866. break;
  867. }
  868. return 1;
  869. }
  870. #ifdef FIPS_ALGVS
  871. int fips_test_suite_main(int argc, char **argv)
  872. #else
  873. int main(int argc, char **argv)
  874. #endif
  875. {
  876. int bad_rsa = 0, bad_dsa = 0;
  877. int do_rng_stick = 0;
  878. int do_drbg_stick = 0;
  879. int no_exit = 0;
  880. int no_dh = 0;
  881. char *pass = FIPS_AUTH_USER_PASS;
  882. FIPS_post_set_callback(post_cb);
  883. printf("\tFIPS-mode test application\n");
  884. printf("\t%s\n\n", FIPS_module_version_text());
  885. if (argv[1]) {
  886. /* Corrupted KAT tests */
  887. if (!strcmp(argv[1], "integrity")) {
  888. fail_id = FIPS_TEST_INTEGRITY;
  889. } else if (!strcmp(argv[1], "aes")) {
  890. fail_id = FIPS_TEST_CIPHER;
  891. fail_sub = NID_aes_128_ecb;
  892. } else if (!strcmp(argv[1], "aes-ccm")) {
  893. fail_id = FIPS_TEST_CCM;
  894. } else if (!strcmp(argv[1], "aes-gcm")) {
  895. fail_id = FIPS_TEST_GCM;
  896. } else if (!strcmp(argv[1], "aes-xts")) {
  897. fail_id = FIPS_TEST_XTS;
  898. } else if (!strcmp(argv[1], "des")) {
  899. fail_id = FIPS_TEST_CIPHER;
  900. fail_sub = NID_des_ede3_ecb;
  901. } else if (!strcmp(argv[1], "dsa")) {
  902. fail_id = FIPS_TEST_SIGNATURE;
  903. fail_key = EVP_PKEY_DSA;
  904. } else if (!strcmp(argv[1], "ecdh")) {
  905. fail_id = FIPS_TEST_ECDH;
  906. } else if (!strcmp(argv[1], "ecdsa")) {
  907. fail_id = FIPS_TEST_SIGNATURE;
  908. fail_key = EVP_PKEY_EC;
  909. } else if (!strcmp(argv[1], "rsa")) {
  910. fail_id = FIPS_TEST_SIGNATURE;
  911. fail_key = EVP_PKEY_RSA;
  912. } else if (!strcmp(argv[1], "rsakey")) {
  913. printf("RSA key generation and signature validation with corrupted key...\n");
  914. bad_rsa = 1;
  915. no_exit = 1;
  916. } else if (!strcmp(argv[1], "rsakeygen")) {
  917. fail_id = FIPS_TEST_PAIRWISE;
  918. fail_key = EVP_PKEY_RSA;
  919. no_exit = 1;
  920. } else if (!strcmp(argv[1], "dsakey")) {
  921. printf("DSA key generation and signature validation with corrupted key...\n");
  922. bad_dsa = 1;
  923. no_exit = 1;
  924. } else if (!strcmp(argv[1], "dsakeygen")) {
  925. fail_id = FIPS_TEST_PAIRWISE;
  926. fail_key = EVP_PKEY_DSA;
  927. no_exit = 1;
  928. } else if (!strcmp(argv[1], "sha1")) {
  929. fail_id = FIPS_TEST_DIGEST;
  930. } else if (!strcmp(argv[1], "hmac")) {
  931. fail_id = FIPS_TEST_HMAC;
  932. } else if (!strcmp(argv[1], "cmac")) {
  933. fail_id = FIPS_TEST_CMAC;
  934. } else if (!strcmp(argv[1], "drbg")) {
  935. fail_id = FIPS_TEST_DRBG;
  936. } else if (!strcmp(argv[1], "rng")) {
  937. fail_id = FIPS_TEST_X931;
  938. } else if (!strcmp(argv[1], "nodh")) {
  939. no_dh = 1;
  940. no_exit = 1;
  941. } else if (!strcmp(argv[1], "post")) {
  942. fail_id = -1;
  943. } else if (!strcmp(argv[1], "rngstick")) {
  944. do_rng_stick = 1;
  945. no_exit = 1;
  946. printf("RNG test with stuck continuous test...\n");
  947. } else if (!strcmp(argv[1], "drbgentstick")) {
  948. do_entropy_stick();
  949. } else if (!strcmp(argv[1], "drbgstick")) {
  950. do_drbg_stick = 1;
  951. no_exit = 1;
  952. printf("DRBG test with stuck continuous test...\n");
  953. } else if (!strcmp(argv[1], "user")) {
  954. pass = FIPS_AUTH_USER_PASS;
  955. } else if (!strcmp(argv[1], "officer")) {
  956. pass = FIPS_AUTH_OFFICER_PASS;
  957. } else if (!strcmp(argv[1], "badpass")) {
  958. pass = "bad invalid password";
  959. } else if (!strcmp(argv[1], "nopass")) {
  960. pass = "";
  961. } else {
  962. printf("Bad argument \"%s\"\n", argv[1]);
  963. return 1;
  964. }
  965. if (!no_exit) {
  966. fips_algtest_init_nofips();
  967. if (!FIPS_module_mode_set(1, pass)) {
  968. printf("Power-up self test failed\n");
  969. return 1;
  970. }
  971. printf("Power-up self test successful\n");
  972. return 0;
  973. }
  974. }
  975. fips_algtest_init_nofips();
  976. /* Non-Approved cryptographic operation
  977. */
  978. printf("1. Non-Approved cryptographic operation test...\n");
  979. if (no_dh)
  980. printf("\t D-H test skipped\n");
  981. else
  982. test_msg("\ta. Included algorithm (D-H)...", dh_test());
  983. /* Power-up self test
  984. */
  985. ERR_clear_error();
  986. test_msg("2. Automatic power-up self test", FIPS_module_mode_set(1, pass));
  987. if (!FIPS_module_mode())
  988. return 1;
  989. if (do_drbg_stick)
  990. FIPS_drbg_stick();
  991. if (do_rng_stick)
  992. FIPS_x931_stick();
  993. /* AES encryption/decryption
  994. */
  995. test_msg("3a. AES encryption/decryption", FIPS_aes_test());
  996. /* AES GCM encryption/decryption
  997. */
  998. test_msg("3b. AES-GCM encryption/decryption", FIPS_aes_gcm_test());
  999. /* RSA key generation and encryption/decryption
  1000. */
  1001. test_msg("4. RSA key generation and encryption/decryption",
  1002. FIPS_rsa_test(bad_rsa));
  1003. /* DES-CBC encryption/decryption
  1004. */
  1005. test_msg("5. DES-ECB encryption/decryption", FIPS_des3_test());
  1006. /* DSA key generation and signature validation
  1007. */
  1008. test_msg("6. DSA key generation and signature validation",
  1009. FIPS_dsa_test(bad_dsa));
  1010. /* SHA-1 hash
  1011. */
  1012. test_msg("7a. SHA-1 hash", FIPS_sha1_test());
  1013. /* SHA-256 hash
  1014. */
  1015. test_msg("7b. SHA-256 hash", FIPS_sha256_test());
  1016. /* SHA-512 hash
  1017. */
  1018. test_msg("7c. SHA-512 hash", FIPS_sha512_test());
  1019. /* HMAC-SHA-1 hash
  1020. */
  1021. test_msg("7d. HMAC-SHA-1 hash", FIPS_hmac_sha1_test());
  1022. /* HMAC-SHA-224 hash
  1023. */
  1024. test_msg("7e. HMAC-SHA-224 hash", FIPS_hmac_sha224_test());
  1025. /* HMAC-SHA-256 hash
  1026. */
  1027. test_msg("7f. HMAC-SHA-256 hash", FIPS_hmac_sha256_test());
  1028. /* HMAC-SHA-384 hash
  1029. */
  1030. test_msg("7g. HMAC-SHA-384 hash", FIPS_hmac_sha384_test());
  1031. /* HMAC-SHA-512 hash
  1032. */
  1033. test_msg("7h. HMAC-SHA-512 hash", FIPS_hmac_sha512_test());
  1034. /* CMAC-AES-128 hash
  1035. */
  1036. test_msg("8a. CMAC-AES-128 hash", FIPS_cmac_aes128_test());
  1037. /* CMAC-AES-192 hash
  1038. */
  1039. test_msg("8b. CMAC-AES-192 hash", FIPS_cmac_aes192_test());
  1040. /* CMAC-AES-256 hash
  1041. */
  1042. test_msg("8c. CMAC-AES-256 hash", FIPS_cmac_aes256_test());
  1043. # if 0 /* Not a FIPS algorithm */
  1044. /* CMAC-TDEA-2 hash
  1045. */
  1046. test_msg("8d. CMAC-TDEA-2 hash", FIPS_cmac_tdea2_test());
  1047. #endif
  1048. /* CMAC-TDEA-3 hash
  1049. */
  1050. test_msg("8e. CMAC-TDEA-3 hash", FIPS_cmac_tdea3_test());
  1051. /* Non-Approved cryptographic operation
  1052. */
  1053. printf("9. Non-Approved cryptographic operation test...\n");
  1054. printf("\ta. Included algorithm (D-H)...%s\n",
  1055. no_dh ? "skipped" :
  1056. dh_test() ? "successful as expected"
  1057. : Fail("failed INCORRECTLY!") );
  1058. /* Zeroization
  1059. */
  1060. printf("10. Zero-ization...\n\t%s\n",
  1061. Zeroize() ? "successful as expected"
  1062. : Fail("failed INCORRECTLY!") );
  1063. printf("11. Complete DRBG health check...\n");
  1064. printf("\t%s\n", FIPS_selftest_drbg_all() ? "successful as expected"
  1065. : Fail("failed INCORRECTLY!") );
  1066. printf("12. DRBG generation check...\n");
  1067. printf("\t%s\n", do_drbg_all() ? "successful as expected"
  1068. : Fail("failed INCORRECTLY!") );
  1069. printf("\nAll tests completed with %d errors\n", Error);
  1070. return Error ? 1 : 0;
  1071. }
  1072. #endif