hash.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. /* hash.c has unit tests
  2. *
  3. * Copyright (C) 2006-2017 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #include <stdio.h>
  26. #include <wolfssl/wolfcrypt/md4.h>
  27. #include <wolfssl/wolfcrypt/md5.h>
  28. #include <wolfssl/wolfcrypt/sha.h>
  29. #include <wolfssl/wolfcrypt/sha256.h>
  30. #include <wolfssl/wolfcrypt/sha512.h>
  31. #include <wolfssl/wolfcrypt/ripemd.h>
  32. #include <wolfssl/wolfcrypt/hmac.h>
  33. #include <tests/unit.h>
  34. typedef struct testVector {
  35. const char* input;
  36. const char* output;
  37. size_t inLen;
  38. size_t outLen;
  39. } testVector;
  40. int md4_test(void);
  41. int md5_test(void);
  42. int sha_test(void);
  43. int sha224_test(void);
  44. int sha256_test(void);
  45. int sha512_test(void);
  46. int sha384_test(void);
  47. int ripemd_test(void);
  48. int hmac_md5_test(void);
  49. int hmac_sha_test(void);
  50. int hmac_sha224_test(void);
  51. int hmac_sha256_test(void);
  52. int hmac_sha384_test(void);
  53. int HashTest(void)
  54. {
  55. int ret = 0;
  56. printf(" Begin HASH Tests\n");
  57. #ifndef NO_MD4
  58. if ( (ret = md4_test()) ) {
  59. printf( " MD4 test failed!\n");
  60. return ret;
  61. } else
  62. printf( " MD4 test passed!\n");
  63. #endif
  64. #ifndef NO_MD5
  65. if ( (ret = md5_test()) ) {
  66. printf( " MD5 test failed!\n");
  67. return ret;
  68. } else
  69. printf( " MD5 test passed!\n");
  70. #endif
  71. #ifndef NO_SHA
  72. if ( (ret = sha_test()) ) {
  73. printf( " SHA test failed!\n");
  74. return ret;
  75. } else
  76. printf( " SHA test passed!\n");
  77. #endif
  78. #ifdef WOLFSSL_SHA224
  79. if ( (ret = sha224_test()) ) {
  80. printf( " SHA-224 test failed!\n");
  81. return ret;
  82. } else
  83. printf( " SHA-224 test passed!\n");
  84. #endif
  85. #ifndef NO_SHA256
  86. if ( (ret = sha256_test()) ) {
  87. printf( " SHA-256 test failed!\n");
  88. return ret;
  89. } else
  90. printf( " SHA-256 test passed!\n");
  91. #endif
  92. #ifdef WOLFSSL_SHA512
  93. if ( (ret = sha512_test()) ) {
  94. printf( " SHA-512 test failed!\n");
  95. return ret;
  96. } else
  97. printf( " SHA-512 test passed!\n");
  98. #endif
  99. #ifdef WOLFSSL_SHA384
  100. if ( (ret = sha384_test()) ) {
  101. printf( " SHA-384 test failed!\n");
  102. return ret;
  103. } else
  104. printf( " SHA-384 test passed!\n");
  105. #endif
  106. #ifdef WOLFSSL_RIPEMD
  107. if ( (ret = ripemd_test()) ) {
  108. printf( " RIPEMD test failed!\n");
  109. return ret;
  110. } else
  111. printf( " RIPEMD test passed!\n");
  112. #endif
  113. #ifndef NO_HMAC
  114. #ifndef NO_MD5
  115. if ( (ret = hmac_md5_test()) ) {
  116. printf( " HMAC-MD5 test failed!\n");
  117. return ret;
  118. } else
  119. printf( " HMAC-MD5 test passed!\n");
  120. #endif
  121. #ifndef NO_SHA
  122. if ( (ret = hmac_sha_test()) )
  123. printf( " HMAC-SHA test failed!\n");
  124. else
  125. printf( " HMAC-SHA test passed!\n");
  126. #endif
  127. #ifdef WOLFSSL_SHA224
  128. if ( (ret = hmac_sha224_test()) )
  129. printf( " HMAC-SHA224 test failed!\n");
  130. else
  131. printf( " HMAC-SHA224 test passed!\n");
  132. #endif
  133. #ifndef NO_SHA256
  134. if ( (ret = hmac_sha256_test()) )
  135. printf( " HMAC-SHA256 test failed!\n");
  136. else
  137. printf( " HMAC-SHA256 test passed!\n");
  138. #endif
  139. #ifdef WOLFSSL_SHA384
  140. if ( (ret = hmac_sha384_test()) )
  141. printf( " HMAC-SHA384 test failed!\n");
  142. else
  143. printf( " HMAC-SHA384 test passed!\n");
  144. #endif
  145. #endif
  146. printf(" End HASH Tests\n");
  147. return 0;
  148. }
  149. #ifndef NO_MD4
  150. int md4_test(void)
  151. {
  152. Md4 md4;
  153. byte hash[MD4_DIGEST_SIZE];
  154. testVector a, b, c, d, e, f, g;
  155. testVector test_md4[7];
  156. int times = sizeof(test_md4) / sizeof(testVector), i;
  157. a.input = "";
  158. a.output = "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
  159. "\xc0";
  160. a.inLen = XSTRLEN(a.input);
  161. a.outLen = XSTRLEN(a.output);
  162. b.input = "a";
  163. b.output = "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
  164. "\x24";
  165. b.inLen = XSTRLEN(b.input);
  166. b.outLen = XSTRLEN(b.output);
  167. c.input = "abc";
  168. c.output = "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
  169. "\x9d";
  170. c.inLen = XSTRLEN(c.input);
  171. c.outLen = XSTRLEN(c.output);
  172. d.input = "message digest";
  173. d.output = "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
  174. "\x4b";
  175. d.inLen = XSTRLEN(d.input);
  176. d.outLen = XSTRLEN(d.output);
  177. e.input = "abcdefghijklmnopqrstuvwxyz";
  178. e.output = "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
  179. "\xa9";
  180. e.inLen = XSTRLEN(e.input);
  181. e.outLen = XSTRLEN(e.output);
  182. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  183. "6789";
  184. f.output = "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
  185. "\xe4";
  186. f.inLen = XSTRLEN(f.input);
  187. f.outLen = XSTRLEN(f.output);
  188. g.input = "1234567890123456789012345678901234567890123456789012345678"
  189. "9012345678901234567890";
  190. g.output = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
  191. "\x36";
  192. g.inLen = XSTRLEN(g.input);
  193. g.outLen = XSTRLEN(g.output);
  194. test_md4[0] = a;
  195. test_md4[1] = b;
  196. test_md4[2] = c;
  197. test_md4[3] = d;
  198. test_md4[4] = e;
  199. test_md4[5] = f;
  200. test_md4[6] = g;
  201. wc_InitMd4(&md4);
  202. for (i = 0; i < times; ++i) {
  203. wc_Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen);
  204. wc_Md4Final(&md4, hash);
  205. if (XMEMCMP(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0)
  206. return -205 - i;
  207. }
  208. return 0;
  209. }
  210. #endif /* NO_MD4 */
  211. #ifndef NO_MD5
  212. int md5_test(void)
  213. {
  214. int ret;
  215. wc_Md5 md5;
  216. byte hash[WC_MD5_DIGEST_SIZE];
  217. testVector a, b, c, d, e;
  218. testVector test_md5[5];
  219. int times = sizeof(test_md5) / sizeof(testVector), i;
  220. a.input = "abc";
  221. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  222. "\x72";
  223. a.inLen = XSTRLEN(a.input);
  224. a.outLen = XSTRLEN(a.output);
  225. b.input = "message digest";
  226. b.output = "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
  227. "\xd0";
  228. b.inLen = XSTRLEN(b.input);
  229. b.outLen = XSTRLEN(b.output);
  230. c.input = "abcdefghijklmnopqrstuvwxyz";
  231. c.output = "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
  232. "\x3b";
  233. c.inLen = XSTRLEN(c.input);
  234. c.outLen = XSTRLEN(c.output);
  235. d.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  236. "6789";
  237. d.output = "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
  238. "\x9f";
  239. d.inLen = XSTRLEN(d.input);
  240. d.outLen = XSTRLEN(d.output);
  241. e.input = "1234567890123456789012345678901234567890123456789012345678"
  242. "9012345678901234567890";
  243. e.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  244. "\x7a";
  245. e.inLen = XSTRLEN(e.input);
  246. e.outLen = XSTRLEN(e.output);
  247. test_md5[0] = a;
  248. test_md5[1] = b;
  249. test_md5[2] = c;
  250. test_md5[3] = d;
  251. test_md5[4] = e;
  252. ret = wc_InitMd5(&md5);
  253. if (ret) {
  254. return ret;
  255. }
  256. for (i = 0; i < times; ++i) {
  257. ret = wc_Md5Update(&md5, (byte*)test_md5[i].input,
  258. (word32)test_md5[i].inLen);
  259. if (ret) {
  260. return ret;
  261. }
  262. ret = wc_Md5Final(&md5, hash);
  263. if (ret) {
  264. return ret;
  265. }
  266. if (XMEMCMP(hash, test_md5[i].output, WC_MD5_DIGEST_SIZE) != 0)
  267. return -5 - i;
  268. }
  269. return 0;
  270. }
  271. #endif /* NO_MD5 */
  272. #ifndef NO_SHA
  273. int sha_test(void)
  274. {
  275. wc_Sha sha;
  276. byte hash[WC_SHA_DIGEST_SIZE];
  277. testVector a, b, c, d;
  278. testVector test_sha[4];
  279. int ret = 0;
  280. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  281. a.input = "abc";
  282. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  283. "\x6C\x9C\xD0\xD8\x9D";
  284. a.inLen = XSTRLEN(a.input);
  285. a.outLen = XSTRLEN(a.output);
  286. b.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  287. b.output = "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
  288. "\xE5\xE5\x46\x70\xF1";
  289. b.inLen = XSTRLEN(b.input);
  290. b.outLen = XSTRLEN(b.output);
  291. c.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  292. "aaaaaa";
  293. c.output = "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
  294. "\x2A\x25\xEC\x64\x4D";
  295. c.inLen = XSTRLEN(c.input);
  296. c.outLen = XSTRLEN(c.output);
  297. d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  298. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  299. "aaaaaaaaaa";
  300. d.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  301. "\x53\x99\x5E\x26\xA0";
  302. d.inLen = XSTRLEN(d.input);
  303. d.outLen = XSTRLEN(d.output);
  304. test_sha[0] = a;
  305. test_sha[1] = b;
  306. test_sha[2] = c;
  307. test_sha[3] = d;
  308. ret = wc_InitSha(&sha);
  309. if (ret != 0)
  310. return ret;
  311. for (i = 0; i < times; ++i) {
  312. wc_ShaUpdate(&sha, (byte*)test_sha[i].input, (word32)test_sha[i].inLen);
  313. wc_ShaFinal(&sha, hash);
  314. if (XMEMCMP(hash, test_sha[i].output, WC_SHA_DIGEST_SIZE) != 0)
  315. return -10 - i;
  316. }
  317. return 0;
  318. }
  319. #endif /* NO_SHA */
  320. #ifdef WOLFSSL_SHA224
  321. int sha224_test(void)
  322. {
  323. wc_Sha224 sha;
  324. byte hash[WC_SHA224_DIGEST_SIZE];
  325. testVector a, b;
  326. testVector test_sha[2];
  327. int ret;
  328. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  329. a.input = "abc";
  330. a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2\x55"
  331. "\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  332. a.inLen = XSTRLEN(a.input);
  333. a.outLen = WC_SHA224_DIGEST_SIZE;
  334. b.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  335. b.output = "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  336. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  337. b.inLen = XSTRLEN(b.input);
  338. b.outLen = WC_SHA224_DIGEST_SIZE;
  339. test_sha[0] = a;
  340. test_sha[1] = b;
  341. ret = wc_InitSha224(&sha);
  342. if (ret != 0)
  343. return -4005;
  344. for (i = 0; i < times; ++i) {
  345. ret = wc_Sha224Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen);
  346. if (ret != 0)
  347. return ret;
  348. ret = wc_Sha224Final(&sha, hash);
  349. if (ret != 0)
  350. return ret;
  351. if (XMEMCMP(hash, test_sha[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  352. return -10 - i;
  353. }
  354. return 0;
  355. }
  356. #endif
  357. #ifndef NO_SHA256
  358. int sha256_test(void)
  359. {
  360. wc_Sha256 sha;
  361. byte hash[WC_SHA256_DIGEST_SIZE];
  362. testVector a, b;
  363. testVector test_sha[2];
  364. int ret;
  365. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  366. a.input = "abc";
  367. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  368. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  369. "\x15\xAD";
  370. a.inLen = XSTRLEN(a.input);
  371. a.outLen = XSTRLEN(a.output);
  372. b.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  373. b.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  374. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  375. "\x06\xC1";
  376. b.inLen = XSTRLEN(b.input);
  377. b.outLen = XSTRLEN(b.output);
  378. test_sha[0] = a;
  379. test_sha[1] = b;
  380. ret = wc_InitSha256(&sha);
  381. if (ret != 0)
  382. return ret;
  383. for (i = 0; i < times; ++i) {
  384. ret = wc_Sha256Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen);
  385. if (ret != 0)
  386. return ret;
  387. ret = wc_Sha256Final(&sha, hash);
  388. if (ret != 0)
  389. return ret;
  390. if (XMEMCMP(hash, test_sha[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  391. return -10 - i;
  392. }
  393. return 0;
  394. }
  395. #endif
  396. #ifdef WOLFSSL_SHA512
  397. int sha512_test(void)
  398. {
  399. wc_Sha512 sha;
  400. byte hash[WC_SHA512_DIGEST_SIZE];
  401. testVector a, b;
  402. testVector test_sha[2];
  403. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  404. int ret;
  405. a.input = "abc";
  406. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  407. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  408. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  409. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  410. "\xa5\x4c\xa4\x9f";
  411. a.inLen = XSTRLEN(a.input);
  412. a.outLen = XSTRLEN(a.output);
  413. b.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  414. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  415. b.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  416. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  417. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  418. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  419. "\x87\x4b\xe9\x09";
  420. b.inLen = XSTRLEN(b.input);
  421. b.outLen = XSTRLEN(b.output);
  422. test_sha[0] = a;
  423. test_sha[1] = b;
  424. ret = wc_InitSha512(&sha);
  425. if (ret != 0)
  426. return ret;
  427. for (i = 0; i < times; ++i) {
  428. ret = wc_Sha512Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen);
  429. if (ret != 0)
  430. return ret;
  431. ret = wc_Sha512Final(&sha, hash);
  432. if (ret != 0)
  433. return ret;
  434. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  435. return -10 - i;
  436. }
  437. return 0;
  438. }
  439. #endif
  440. #ifdef WOLFSSL_SHA384
  441. int sha384_test()
  442. {
  443. wc_Sha384 sha;
  444. byte hash[WC_SHA384_DIGEST_SIZE];
  445. testVector a, b;
  446. testVector test_sha[2];
  447. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  448. int ret;
  449. a.input = "abc";
  450. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  451. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  452. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  453. "\xc8\x25\xa7";
  454. a.inLen = XSTRLEN(a.input);
  455. a.outLen = XSTRLEN(a.output);
  456. b.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  457. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  458. b.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  459. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  460. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  461. "\x74\x60\x39";
  462. b.inLen = XSTRLEN(b.input);
  463. b.outLen = XSTRLEN(b.output);
  464. test_sha[0] = a;
  465. test_sha[1] = b;
  466. ret = wc_InitSha384(&sha);
  467. if (ret != 0)
  468. return ret;
  469. for (i = 0; i < times; ++i) {
  470. ret = wc_Sha384Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen);
  471. if (ret != 0)
  472. return ret;
  473. ret = wc_Sha384Final(&sha, hash);
  474. if (ret != 0)
  475. return ret;
  476. if (XMEMCMP(hash, test_sha[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  477. return -10 - i;
  478. }
  479. return 0;
  480. }
  481. #endif
  482. #ifdef WOLFSSL_RIPEMD
  483. int ripemd_test(void)
  484. {
  485. RipeMd ripemd;
  486. int ret;
  487. byte hash[RIPEMD_DIGEST_SIZE];
  488. testVector a, b, c, d;
  489. testVector test_ripemd[4];
  490. int times = sizeof(test_ripemd) / sizeof(struct testVector), i;
  491. a.input = "abc";
  492. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  493. "\xb0\x87\xf1\x5a\x0b\xfc";
  494. a.inLen = XSTRLEN(a.input);
  495. a.outLen = XSTRLEN(a.output);
  496. b.input = "message digest";
  497. b.output = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
  498. "\x5f\xfa\x21\x59\x5f\x36";
  499. b.inLen = XSTRLEN(b.input);
  500. b.outLen = XSTRLEN(b.output);
  501. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  502. c.output = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
  503. "\xf4\x9a\xda\x62\xeb\x2b";
  504. c.inLen = XSTRLEN(c.input);
  505. c.outLen = XSTRLEN(c.output);
  506. d.input = "12345678901234567890123456789012345678901234567890123456"
  507. "789012345678901234567890";
  508. d.output = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
  509. "\x82\xbf\x63\x32\x6b\xfb";
  510. d.inLen = XSTRLEN(d.input);
  511. d.outLen = XSTRLEN(d.output);
  512. test_ripemd[0] = a;
  513. test_ripemd[1] = b;
  514. test_ripemd[2] = c;
  515. test_ripemd[3] = d;
  516. ret = wc_InitRipeMd(&ripemd);
  517. if (ret) {
  518. return ret;
  519. }
  520. for (i = 0; i < times; ++i) {
  521. ret = wc_RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input,
  522. (word32)test_ripemd[i].inLen);
  523. if (ret) {
  524. return ret;
  525. }
  526. ret = wc_RipeMdFinal(&ripemd, hash);
  527. if (ret) {
  528. return ret;
  529. }
  530. if (XMEMCMP(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0)
  531. return -10 - i;
  532. }
  533. return 0;
  534. }
  535. #endif /* WOLFSSL_RIPEMD */
  536. #if !defined(NO_HMAC) && !defined(NO_MD5)
  537. int hmac_md5_test(void)
  538. {
  539. Hmac hmac;
  540. byte hash[WC_MD5_DIGEST_SIZE];
  541. const char* keys[]=
  542. {
  543. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  544. "Jefe",
  545. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  546. };
  547. testVector a, b, c;
  548. testVector test_hmac[3];
  549. int ret;
  550. int times = sizeof(test_hmac) / sizeof(testVector), i;
  551. a.input = "Hi There";
  552. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  553. "\x9d";
  554. a.inLen = XSTRLEN(a.input);
  555. a.outLen = XSTRLEN(a.output);
  556. b.input = "what do ya want for nothing?";
  557. b.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
  558. "\x38";
  559. b.inLen = XSTRLEN(b.input);
  560. b.outLen = XSTRLEN(b.output);
  561. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  562. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  563. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  564. "\xDD\xDD\xDD\xDD\xDD\xDD";
  565. c.output = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
  566. "\xf6";
  567. c.inLen = XSTRLEN(c.input);
  568. c.outLen = XSTRLEN(c.output);
  569. test_hmac[0] = a;
  570. test_hmac[1] = b;
  571. test_hmac[2] = c;
  572. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  573. if (ret != 0)
  574. return -20009;
  575. for (i = 0; i < times; ++i) {
  576. #if defined(HAVE_FIPS)
  577. if (i == 1)
  578. continue; /* fips not allowed */
  579. #endif
  580. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[i], (word32)XSTRLEN(keys[i]));
  581. if (ret != 0)
  582. return -4014;
  583. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  584. (word32)test_hmac[i].inLen);
  585. if (ret != 0)
  586. return -4015;
  587. ret = wc_HmacFinal(&hmac, hash);
  588. if (ret != 0)
  589. return -4016;
  590. if (XMEMCMP(hash, test_hmac[i].output, WC_MD5_DIGEST_SIZE) != 0)
  591. return -20 - i;
  592. }
  593. wc_HmacFree(&hmac);
  594. return 0;
  595. }
  596. #endif
  597. #if !defined(NO_HMAC) && !defined(NO_SHA)
  598. int hmac_sha_test(void)
  599. {
  600. Hmac hmac;
  601. byte hash[WC_SHA_DIGEST_SIZE];
  602. const char* keys[]=
  603. {
  604. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  605. "\x0b\x0b\x0b",
  606. "Jefe",
  607. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  608. "\xAA\xAA\xAA"
  609. };
  610. testVector a, b, c;
  611. testVector test_hmac[3];
  612. int ret;
  613. int times = sizeof(test_hmac) / sizeof(testVector), i;
  614. a.input = "Hi There";
  615. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  616. "\x8e\xf1\x46\xbe\x00";
  617. a.inLen = XSTRLEN(a.input);
  618. a.outLen = XSTRLEN(a.output);
  619. b.input = "what do ya want for nothing?";
  620. b.output = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf"
  621. "\x9c\x25\x9a\x7c\x79";
  622. b.inLen = XSTRLEN(b.input);
  623. b.outLen = XSTRLEN(b.output);
  624. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  625. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  626. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  627. "\xDD\xDD\xDD\xDD\xDD\xDD";
  628. c.output = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b"
  629. "\x4f\x63\xf1\x75\xd3";
  630. c.inLen = XSTRLEN(c.input);
  631. c.outLen = XSTRLEN(c.output);
  632. test_hmac[0] = a;
  633. test_hmac[1] = b;
  634. test_hmac[2] = c;
  635. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  636. if (ret != 0)
  637. return -20009;
  638. for (i = 0; i < times; ++i) {
  639. #if defined(HAVE_FIPS)
  640. if (i == 1)
  641. continue; /* fips not allowed */
  642. #endif
  643. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[i], (word32)XSTRLEN(keys[i]));
  644. if (ret != 0)
  645. return -4017;
  646. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  647. (word32)test_hmac[i].inLen);
  648. if (ret != 0)
  649. return -4018;
  650. ret = wc_HmacFinal(&hmac, hash);
  651. if (ret != 0)
  652. return -4019;
  653. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA_DIGEST_SIZE) != 0)
  654. return -20 - i;
  655. }
  656. wc_HmacFree(&hmac);
  657. return 0;
  658. }
  659. #endif
  660. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  661. int hmac_sha224_test(void)
  662. {
  663. Hmac hmac;
  664. byte hash[WC_SHA224_DIGEST_SIZE];
  665. const char* keys[]=
  666. {
  667. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  668. "\x0b\x0b\x0b",
  669. "Jefe",
  670. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  671. "\xAA\xAA\xAA"
  672. };
  673. testVector a, b, c;
  674. testVector test_hmac[3];
  675. int ret;
  676. int times = sizeof(test_hmac) / sizeof(testVector), i;
  677. a.input = "Hi There";
  678. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  679. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  680. a.inLen = XSTRLEN(a.input);
  681. a.outLen = WC_SHA224_DIGEST_SIZE;
  682. b.input = "what do ya want for nothing?";
  683. b.output = "\xa3\x0e\x01\x09\x8b\xc6\xdb\xbf\x45\x69\x0f\x3a\x7e\x9e\x6d"
  684. "\x0f\x8b\xbe\xa2\xa3\x9e\x61\x48\x00\x8f\xd0\x5e\x44";
  685. b.inLen = XSTRLEN(b.input);
  686. b.outLen = WC_SHA224_DIGEST_SIZE;
  687. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  688. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  689. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  690. "\xDD\xDD\xDD\xDD\xDD\xDD";
  691. c.output = "\x7f\xb3\xcb\x35\x88\xc6\xc1\xf6\xff\xa9\x69\x4d\x7d\x6a\xd2"
  692. "\x64\x93\x65\xb0\xc1\xf6\x5d\x69\xd1\xec\x83\x33\xea";
  693. c.inLen = XSTRLEN(c.input);
  694. c.outLen = WC_SHA224_DIGEST_SIZE;
  695. test_hmac[0] = a;
  696. test_hmac[1] = b;
  697. test_hmac[2] = c;
  698. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  699. if (ret != 0)
  700. return -20009;
  701. for (i = 0; i < times; ++i) {
  702. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  703. if (i == 1)
  704. continue; /* cavium can't handle short keys, fips not allowed */
  705. #endif
  706. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[i],(word32)XSTRLEN(keys[i]));
  707. if (ret != 0)
  708. return -4021;
  709. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  710. (word32)test_hmac[i].inLen);
  711. if (ret != 0)
  712. return -4022;
  713. ret = wc_HmacFinal(&hmac, hash);
  714. if (ret != 0)
  715. return -4023;
  716. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  717. return -20 - i;
  718. }
  719. wc_HmacFree(&hmac);
  720. return 0;
  721. }
  722. #endif
  723. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  724. int hmac_sha256_test(void)
  725. {
  726. Hmac hmac;
  727. byte hash[WC_SHA256_DIGEST_SIZE];
  728. const char* keys[]=
  729. {
  730. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  731. "\x0b\x0b\x0b",
  732. "Jefe",
  733. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  734. "\xAA\xAA\xAA"
  735. };
  736. testVector a, b, c;
  737. testVector test_hmac[3];
  738. int ret;
  739. int times = sizeof(test_hmac) / sizeof(testVector), i;
  740. a.input = "Hi There";
  741. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  742. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  743. "\xcf\xf7";
  744. a.inLen = XSTRLEN(a.input);
  745. a.outLen = XSTRLEN(a.output);
  746. b.input = "what do ya want for nothing?";
  747. b.output = "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75"
  748. "\xc7\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec"
  749. "\x38\x43";
  750. b.inLen = XSTRLEN(b.input);
  751. b.outLen = XSTRLEN(b.output);
  752. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  753. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  754. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  755. "\xDD\xDD\xDD\xDD\xDD\xDD";
  756. c.output = "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81"
  757. "\xa7\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5"
  758. "\x65\xfe";
  759. c.inLen = XSTRLEN(c.input);
  760. c.outLen = XSTRLEN(c.output);
  761. test_hmac[0] = a;
  762. test_hmac[1] = b;
  763. test_hmac[2] = c;
  764. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  765. if (ret != 0)
  766. return -20009;
  767. for (i = 0; i < times; ++i) {
  768. #if defined(HAVE_FIPS)
  769. if (i == 1)
  770. continue; /* fips not allowed */
  771. #endif
  772. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[i],
  773. (word32)XSTRLEN(keys[i]));
  774. if (ret != 0)
  775. return -4020;
  776. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  777. (word32)test_hmac[i].inLen);
  778. if (ret != 0)
  779. return -4021;
  780. ret = wc_HmacFinal(&hmac, hash);
  781. if (ret != 0)
  782. return -4022;
  783. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  784. return -20 - i;
  785. }
  786. wc_HmacFree(&hmac);
  787. return 0;
  788. }
  789. #endif
  790. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  791. int hmac_sha384_test(void)
  792. {
  793. Hmac hmac;
  794. byte hash[WC_SHA384_DIGEST_SIZE];
  795. const char* keys[]=
  796. {
  797. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  798. "\x0b\x0b\x0b",
  799. "Jefe",
  800. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  801. "\xAA\xAA\xAA"
  802. };
  803. testVector a, b, c;
  804. testVector test_hmac[3];
  805. int ret;
  806. int times = sizeof(test_hmac) / sizeof(testVector), i;
  807. a.input = "Hi There";
  808. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  809. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  810. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  811. "\xfa\x9c\xb6";
  812. a.inLen = XSTRLEN(a.input);
  813. a.outLen = XSTRLEN(a.output);
  814. b.input = "what do ya want for nothing?";
  815. b.output = "\xaf\x45\xd2\xe3\x76\x48\x40\x31\x61\x7f\x78\xd2\xb5\x8a\x6b"
  816. "\x1b\x9c\x7e\xf4\x64\xf5\xa0\x1b\x47\xe4\x2e\xc3\x73\x63\x22"
  817. "\x44\x5e\x8e\x22\x40\xca\x5e\x69\xe2\xc7\x8b\x32\x39\xec\xfa"
  818. "\xb2\x16\x49";
  819. b.inLen = XSTRLEN(b.input);
  820. b.outLen = XSTRLEN(b.output);
  821. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  822. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  823. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  824. "\xDD\xDD\xDD\xDD\xDD\xDD";
  825. c.output = "\x88\x06\x26\x08\xd3\xe6\xad\x8a\x0a\xa2\xac\xe0\x14\xc8\xa8"
  826. "\x6f\x0a\xa6\x35\xd9\x47\xac\x9f\xeb\xe8\x3e\xf4\xe5\x59\x66"
  827. "\x14\x4b\x2a\x5a\xb3\x9d\xc1\x38\x14\xb9\x4e\x3a\xb6\xe1\x01"
  828. "\xa3\x4f\x27";
  829. c.inLen = XSTRLEN(c.input);
  830. c.outLen = XSTRLEN(c.output);
  831. test_hmac[0] = a;
  832. test_hmac[1] = b;
  833. test_hmac[2] = c;
  834. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  835. if (ret != 0)
  836. return -20009;
  837. for (i = 0; i < times; ++i) {
  838. #if defined(HAVE_FIPS)
  839. if (i == 1)
  840. continue; /* fips not allowed */
  841. #endif
  842. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[i],
  843. (word32)XSTRLEN(keys[i]));
  844. if (ret != 0)
  845. return -4023;
  846. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  847. (word32)test_hmac[i].inLen);
  848. if (ret != 0)
  849. return -4024;
  850. ret = wc_HmacFinal(&hmac, hash);
  851. if (ret != 0)
  852. return -4025;
  853. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  854. return -20 - i;
  855. }
  856. wc_HmacFree(&hmac);
  857. return 0;
  858. }
  859. #endif