hash.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. /* hash.c has unit tests
  2. *
  3. * Copyright (C) 2006-2015 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL. (formerly known as CyaSSL)
  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-1301, 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 sha256_test(void);
  44. int sha512_test(void);
  45. int sha384_test(void);
  46. int ripemd_test(void);
  47. int hmac_md5_test(void);
  48. int hmac_sha_test(void);
  49. int hmac_sha256_test(void);
  50. int hmac_sha384_test(void);
  51. int HashTest(void)
  52. {
  53. int ret = 0;
  54. printf(" Begin HASH Tests\n");
  55. #ifndef NO_MD4
  56. if ( (ret = md4_test()) ) {
  57. printf( " MD4 test failed!\n");
  58. return ret;
  59. } else
  60. printf( " MD4 test passed!\n");
  61. #endif
  62. #ifndef NO_MD5
  63. if ( (ret = md5_test()) ) {
  64. printf( " MD5 test failed!\n");
  65. return ret;
  66. } else
  67. printf( " MD5 test passed!\n");
  68. #endif
  69. #ifndef NO_SHA
  70. if ( (ret = sha_test()) ) {
  71. printf( " SHA test failed!\n");
  72. return ret;
  73. } else
  74. printf( " SHA test passed!\n");
  75. #endif
  76. #ifndef NO_SHA256
  77. if ( (ret = sha256_test()) ) {
  78. printf( " SHA-256 test failed!\n");
  79. return ret;
  80. } else
  81. printf( " SHA-256 test passed!\n");
  82. #endif
  83. #ifdef WOLFSSL_SHA512
  84. if ( (ret = sha512_test()) ) {
  85. printf( " SHA-512 test failed!\n");
  86. return ret;
  87. } else
  88. printf( " SHA-512 test passed!\n");
  89. #endif
  90. #ifdef WOLFSSL_SHA384
  91. if ( (ret = sha384_test()) ) {
  92. printf( " SHA-384 test failed!\n");
  93. return ret;
  94. } else
  95. printf( " SHA-384 test passed!\n");
  96. #endif
  97. #ifdef WOLFSSL_RIPEMD
  98. if ( (ret = ripemd_test()) ) {
  99. printf( " RIPEMD test failed!\n");
  100. return ret;
  101. } else
  102. printf( " RIPEMD test passed!\n");
  103. #endif
  104. #ifndef NO_HMAC
  105. #ifndef NO_MD5
  106. if ( (ret = hmac_md5_test()) ) {
  107. printf( " HMAC-MD5 test failed!\n");
  108. return ret;
  109. } else
  110. printf( " HMAC-MD5 test passed!\n");
  111. #endif
  112. #ifndef NO_SHA
  113. if ( (ret = hmac_sha_test()) )
  114. printf( " HMAC-SHA test failed!\n");
  115. else
  116. printf( " HMAC-SHA test passed!\n");
  117. #endif
  118. #ifndef NO_SHA256
  119. if ( (ret = hmac_sha256_test()) )
  120. printf( " HMAC-SHA256 test failed!\n");
  121. else
  122. printf( " HMAC-SHA256 test passed!\n");
  123. #endif
  124. #ifdef WOLFSSL_SHA384
  125. if ( (ret = hmac_sha384_test()) )
  126. printf( " HMAC-SHA384 test failed!\n");
  127. else
  128. printf( " HMAC-SHA384 test passed!\n");
  129. #endif
  130. #endif
  131. printf(" End HASH Tests\n");
  132. return 0;
  133. }
  134. #ifndef NO_MD4
  135. int md4_test(void)
  136. {
  137. Md4 md4;
  138. byte hash[MD4_DIGEST_SIZE];
  139. testVector a, b, c, d, e, f, g;
  140. testVector test_md4[7];
  141. int times = sizeof(test_md4) / sizeof(testVector), i;
  142. a.input = "";
  143. a.output = "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
  144. "\xc0";
  145. a.inLen = strlen(a.input);
  146. a.outLen = strlen(a.output);
  147. b.input = "a";
  148. b.output = "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
  149. "\x24";
  150. b.inLen = strlen(b.input);
  151. b.outLen = strlen(b.output);
  152. c.input = "abc";
  153. c.output = "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
  154. "\x9d";
  155. c.inLen = strlen(c.input);
  156. c.outLen = strlen(c.output);
  157. d.input = "message digest";
  158. d.output = "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
  159. "\x4b";
  160. d.inLen = strlen(d.input);
  161. d.outLen = strlen(d.output);
  162. e.input = "abcdefghijklmnopqrstuvwxyz";
  163. e.output = "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
  164. "\xa9";
  165. e.inLen = strlen(e.input);
  166. e.outLen = strlen(e.output);
  167. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  168. "6789";
  169. f.output = "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
  170. "\xe4";
  171. f.inLen = strlen(f.input);
  172. f.outLen = strlen(f.output);
  173. g.input = "1234567890123456789012345678901234567890123456789012345678"
  174. "9012345678901234567890";
  175. g.output = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
  176. "\x36";
  177. g.inLen = strlen(g.input);
  178. g.outLen = strlen(g.output);
  179. test_md4[0] = a;
  180. test_md4[1] = b;
  181. test_md4[2] = c;
  182. test_md4[3] = d;
  183. test_md4[4] = e;
  184. test_md4[5] = f;
  185. test_md4[6] = g;
  186. wc_InitMd4(&md4);
  187. for (i = 0; i < times; ++i) {
  188. wc_Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen);
  189. wc_Md4Final(&md4, hash);
  190. if (memcmp(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0)
  191. return -205 - i;
  192. }
  193. return 0;
  194. }
  195. #endif /* NO_MD4 */
  196. #ifndef NO_MD5
  197. int md5_test(void)
  198. {
  199. Md5 md5;
  200. byte hash[MD5_DIGEST_SIZE];
  201. testVector a, b, c, d, e;
  202. testVector test_md5[5];
  203. int times = sizeof(test_md5) / sizeof(testVector), i;
  204. a.input = "abc";
  205. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  206. "\x72";
  207. a.inLen = strlen(a.input);
  208. a.outLen = strlen(a.output);
  209. b.input = "message digest";
  210. b.output = "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
  211. "\xd0";
  212. b.inLen = strlen(b.input);
  213. b.outLen = strlen(b.output);
  214. c.input = "abcdefghijklmnopqrstuvwxyz";
  215. c.output = "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
  216. "\x3b";
  217. c.inLen = strlen(c.input);
  218. c.outLen = strlen(c.output);
  219. d.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  220. "6789";
  221. d.output = "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
  222. "\x9f";
  223. d.inLen = strlen(d.input);
  224. d.outLen = strlen(d.output);
  225. e.input = "1234567890123456789012345678901234567890123456789012345678"
  226. "9012345678901234567890";
  227. e.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  228. "\x7a";
  229. e.inLen = strlen(e.input);
  230. e.outLen = strlen(e.output);
  231. test_md5[0] = a;
  232. test_md5[1] = b;
  233. test_md5[2] = c;
  234. test_md5[3] = d;
  235. test_md5[4] = e;
  236. wc_InitMd5(&md5);
  237. for (i = 0; i < times; ++i) {
  238. wc_Md5Update(&md5, (byte*)test_md5[i].input, (word32)test_md5[i].inLen);
  239. wc_Md5Final(&md5, hash);
  240. if (memcmp(hash, test_md5[i].output, MD5_DIGEST_SIZE) != 0)
  241. return -5 - i;
  242. }
  243. return 0;
  244. }
  245. #endif /* NO_MD5 */
  246. #ifndef NO_SHA
  247. int sha_test(void)
  248. {
  249. Sha sha;
  250. byte hash[SHA_DIGEST_SIZE];
  251. testVector a, b, c, d;
  252. testVector test_sha[4];
  253. int ret = 0;
  254. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  255. a.input = "abc";
  256. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  257. "\x6C\x9C\xD0\xD8\x9D";
  258. a.inLen = strlen(a.input);
  259. a.outLen = strlen(a.output);
  260. b.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  261. b.output = "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
  262. "\xE5\xE5\x46\x70\xF1";
  263. b.inLen = strlen(b.input);
  264. b.outLen = strlen(b.output);
  265. c.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  266. "aaaaaa";
  267. c.output = "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
  268. "\x2A\x25\xEC\x64\x4D";
  269. c.inLen = strlen(c.input);
  270. c.outLen = strlen(c.output);
  271. d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  272. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  273. "aaaaaaaaaa";
  274. d.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  275. "\x53\x99\x5E\x26\xA0";
  276. d.inLen = strlen(d.input);
  277. d.outLen = strlen(d.output);
  278. test_sha[0] = a;
  279. test_sha[1] = b;
  280. test_sha[2] = c;
  281. test_sha[3] = d;
  282. ret = wc_InitSha(&sha);
  283. if (ret != 0)
  284. return ret;
  285. for (i = 0; i < times; ++i) {
  286. wc_ShaUpdate(&sha, (byte*)test_sha[i].input, (word32)test_sha[i].inLen);
  287. wc_ShaFinal(&sha, hash);
  288. if (memcmp(hash, test_sha[i].output, SHA_DIGEST_SIZE) != 0)
  289. return -10 - i;
  290. }
  291. return 0;
  292. }
  293. #endif /* NO_SHA */
  294. #ifndef NO_SHA256
  295. int sha256_test(void)
  296. {
  297. Sha256 sha;
  298. byte hash[SHA256_DIGEST_SIZE];
  299. testVector a, b;
  300. testVector test_sha[2];
  301. int ret;
  302. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  303. a.input = "abc";
  304. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  305. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  306. "\x15\xAD";
  307. a.inLen = strlen(a.input);
  308. a.outLen = strlen(a.output);
  309. b.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  310. b.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  311. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  312. "\x06\xC1";
  313. b.inLen = strlen(b.input);
  314. b.outLen = strlen(b.output);
  315. test_sha[0] = a;
  316. test_sha[1] = b;
  317. ret = wc_InitSha256(&sha);
  318. if (ret != 0)
  319. return ret;
  320. for (i = 0; i < times; ++i) {
  321. ret = wc_Sha256Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen);
  322. if (ret != 0)
  323. return ret;
  324. ret = wc_Sha256Final(&sha, hash);
  325. if (ret != 0)
  326. return ret;
  327. if (memcmp(hash, test_sha[i].output, SHA256_DIGEST_SIZE) != 0)
  328. return -10 - i;
  329. }
  330. return 0;
  331. }
  332. #endif
  333. #ifdef WOLFSSL_SHA512
  334. int sha512_test(void)
  335. {
  336. Sha512 sha;
  337. byte hash[SHA512_DIGEST_SIZE];
  338. testVector a, b;
  339. testVector test_sha[2];
  340. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  341. int ret;
  342. a.input = "abc";
  343. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  344. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  345. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  346. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  347. "\xa5\x4c\xa4\x9f";
  348. a.inLen = strlen(a.input);
  349. a.outLen = strlen(a.output);
  350. b.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  351. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  352. b.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  353. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  354. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  355. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  356. "\x87\x4b\xe9\x09";
  357. b.inLen = strlen(b.input);
  358. b.outLen = strlen(b.output);
  359. test_sha[0] = a;
  360. test_sha[1] = b;
  361. ret = wc_InitSha512(&sha);
  362. if (ret != 0)
  363. return ret;
  364. for (i = 0; i < times; ++i) {
  365. ret = wc_Sha512Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen);
  366. if (ret != 0)
  367. return ret;
  368. ret = wc_Sha512Final(&sha, hash);
  369. if (ret != 0)
  370. return ret;
  371. if (memcmp(hash, test_sha[i].output, SHA512_DIGEST_SIZE) != 0)
  372. return -10 - i;
  373. }
  374. return 0;
  375. }
  376. #endif
  377. #ifdef WOLFSSL_SHA384
  378. int sha384_test()
  379. {
  380. Sha384 sha;
  381. byte hash[SHA384_DIGEST_SIZE];
  382. testVector a, b;
  383. testVector test_sha[2];
  384. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  385. int ret;
  386. a.input = "abc";
  387. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  388. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  389. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  390. "\xc8\x25\xa7";
  391. a.inLen = strlen(a.input);
  392. a.outLen = strlen(a.output);
  393. b.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  394. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  395. b.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  396. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  397. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  398. "\x74\x60\x39";
  399. b.inLen = strlen(b.input);
  400. b.outLen = strlen(b.output);
  401. test_sha[0] = a;
  402. test_sha[1] = b;
  403. ret = wc_InitSha384(&sha);
  404. if (ret != 0)
  405. return ret;
  406. for (i = 0; i < times; ++i) {
  407. ret = wc_Sha384Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen);
  408. if (ret != 0)
  409. return ret;
  410. ret = wc_Sha384Final(&sha, hash);
  411. if (ret != 0)
  412. return ret;
  413. if (memcmp(hash, test_sha[i].output, SHA384_DIGEST_SIZE) != 0)
  414. return -10 - i;
  415. }
  416. return 0;
  417. }
  418. #endif
  419. #ifdef WOLFSSL_RIPEMD
  420. int ripemd_test(void)
  421. {
  422. RipeMd ripemd;
  423. byte hash[RIPEMD_DIGEST_SIZE];
  424. testVector a, b, c, d;
  425. testVector test_ripemd[4];
  426. int times = sizeof(test_ripemd) / sizeof(struct testVector), i;
  427. a.input = "abc";
  428. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  429. "\xb0\x87\xf1\x5a\x0b\xfc";
  430. a.inLen = strlen(a.input);
  431. a.outLen = strlen(a.output);
  432. b.input = "message digest";
  433. b.output = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
  434. "\x5f\xfa\x21\x59\x5f\x36";
  435. b.inLen = strlen(b.input);
  436. b.outLen = strlen(b.output);
  437. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  438. c.output = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
  439. "\xf4\x9a\xda\x62\xeb\x2b";
  440. c.inLen = strlen(c.input);
  441. c.outLen = strlen(c.output);
  442. d.input = "12345678901234567890123456789012345678901234567890123456"
  443. "789012345678901234567890";
  444. d.output = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
  445. "\x82\xbf\x63\x32\x6b\xfb";
  446. d.inLen = strlen(d.input);
  447. d.outLen = strlen(d.output);
  448. test_ripemd[0] = a;
  449. test_ripemd[1] = b;
  450. test_ripemd[2] = c;
  451. test_ripemd[3] = d;
  452. wc_InitRipeMd(&ripemd);
  453. for (i = 0; i < times; ++i) {
  454. wc_RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input,
  455. (word32)test_ripemd[i].inLen);
  456. wc_RipeMdFinal(&ripemd, hash);
  457. if (memcmp(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0)
  458. return -10 - i;
  459. }
  460. return 0;
  461. }
  462. #endif /* WOLFSSL_RIPEMD */
  463. #if !defined(NO_HMAC) && !defined(NO_MD5)
  464. int hmac_md5_test(void)
  465. {
  466. Hmac hmac;
  467. byte hash[MD5_DIGEST_SIZE];
  468. const char* keys[]=
  469. {
  470. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  471. "Jefe",
  472. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  473. };
  474. testVector a, b, c;
  475. testVector test_hmac[3];
  476. int ret;
  477. int times = sizeof(test_hmac) / sizeof(testVector), i;
  478. a.input = "Hi There";
  479. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  480. "\x9d";
  481. a.inLen = strlen(a.input);
  482. a.outLen = strlen(a.output);
  483. b.input = "what do ya want for nothing?";
  484. b.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
  485. "\x38";
  486. b.inLen = strlen(b.input);
  487. b.outLen = strlen(b.output);
  488. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  489. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  490. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  491. "\xDD\xDD\xDD\xDD\xDD\xDD";
  492. c.output = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
  493. "\xf6";
  494. c.inLen = strlen(c.input);
  495. c.outLen = strlen(c.output);
  496. test_hmac[0] = a;
  497. test_hmac[1] = b;
  498. test_hmac[2] = c;
  499. for (i = 0; i < times; ++i) {
  500. #if defined(HAVE_FIPS)
  501. if (i == 1)
  502. continue; /* fips not allowed */
  503. #endif
  504. ret = wc_HmacSetKey(&hmac, MD5, (byte*)keys[i], (word32)strlen(keys[i]));
  505. if (ret != 0)
  506. return -4014;
  507. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  508. (word32)test_hmac[i].inLen);
  509. if (ret != 0)
  510. return -4015;
  511. ret = wc_HmacFinal(&hmac, hash);
  512. if (ret != 0)
  513. return -4016;
  514. if (memcmp(hash, test_hmac[i].output, MD5_DIGEST_SIZE) != 0)
  515. return -20 - i;
  516. }
  517. return 0;
  518. }
  519. #endif
  520. #if !defined(NO_HMAC) && !defined(NO_SHA)
  521. int hmac_sha_test(void)
  522. {
  523. Hmac hmac;
  524. byte hash[SHA_DIGEST_SIZE];
  525. const char* keys[]=
  526. {
  527. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  528. "\x0b\x0b\x0b",
  529. "Jefe",
  530. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  531. "\xAA\xAA\xAA"
  532. };
  533. testVector a, b, c;
  534. testVector test_hmac[3];
  535. int ret;
  536. int times = sizeof(test_hmac) / sizeof(testVector), i;
  537. a.input = "Hi There";
  538. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  539. "\x8e\xf1\x46\xbe\x00";
  540. a.inLen = strlen(a.input);
  541. a.outLen = strlen(a.output);
  542. b.input = "what do ya want for nothing?";
  543. b.output = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf"
  544. "\x9c\x25\x9a\x7c\x79";
  545. b.inLen = strlen(b.input);
  546. b.outLen = strlen(b.output);
  547. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  548. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  549. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  550. "\xDD\xDD\xDD\xDD\xDD\xDD";
  551. c.output = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b"
  552. "\x4f\x63\xf1\x75\xd3";
  553. c.inLen = strlen(c.input);
  554. c.outLen = strlen(c.output);
  555. test_hmac[0] = a;
  556. test_hmac[1] = b;
  557. test_hmac[2] = c;
  558. for (i = 0; i < times; ++i) {
  559. #if defined(HAVE_FIPS)
  560. if (i == 1)
  561. continue; /* fips not allowed */
  562. #endif
  563. ret = wc_HmacSetKey(&hmac, SHA, (byte*)keys[i], (word32)strlen(keys[i]));
  564. if (ret != 0)
  565. return -4017;
  566. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  567. (word32)test_hmac[i].inLen);
  568. if (ret != 0)
  569. return -4018;
  570. ret = wc_HmacFinal(&hmac, hash);
  571. if (ret != 0)
  572. return -4019;
  573. if (memcmp(hash, test_hmac[i].output, SHA_DIGEST_SIZE) != 0)
  574. return -20 - i;
  575. }
  576. return 0;
  577. }
  578. #endif
  579. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  580. int hmac_sha256_test(void)
  581. {
  582. Hmac hmac;
  583. byte hash[SHA256_DIGEST_SIZE];
  584. const char* keys[]=
  585. {
  586. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  587. "\x0b\x0b\x0b",
  588. "Jefe",
  589. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  590. "\xAA\xAA\xAA"
  591. };
  592. testVector a, b, c;
  593. testVector test_hmac[3];
  594. int ret;
  595. int times = sizeof(test_hmac) / sizeof(testVector), i;
  596. a.input = "Hi There";
  597. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  598. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  599. "\xcf\xf7";
  600. a.inLen = strlen(a.input);
  601. a.outLen = strlen(a.output);
  602. b.input = "what do ya want for nothing?";
  603. b.output = "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75"
  604. "\xc7\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec"
  605. "\x38\x43";
  606. b.inLen = strlen(b.input);
  607. b.outLen = strlen(b.output);
  608. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  609. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  610. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  611. "\xDD\xDD\xDD\xDD\xDD\xDD";
  612. c.output = "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81"
  613. "\xa7\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5"
  614. "\x65\xfe";
  615. c.inLen = strlen(c.input);
  616. c.outLen = strlen(c.output);
  617. test_hmac[0] = a;
  618. test_hmac[1] = b;
  619. test_hmac[2] = c;
  620. for (i = 0; i < times; ++i) {
  621. #if defined(HAVE_FIPS)
  622. if (i == 1)
  623. continue; /* fips not allowed */
  624. #endif
  625. ret = wc_HmacSetKey(&hmac,SHA256, (byte*)keys[i], (word32)strlen(keys[i]));
  626. if (ret != 0)
  627. return -4020;
  628. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  629. (word32)test_hmac[i].inLen);
  630. if (ret != 0)
  631. return -4021;
  632. ret = wc_HmacFinal(&hmac, hash);
  633. if (ret != 0)
  634. return -4022;
  635. if (memcmp(hash, test_hmac[i].output, SHA256_DIGEST_SIZE) != 0)
  636. return -20 - i;
  637. }
  638. return 0;
  639. }
  640. #endif
  641. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  642. int hmac_sha384_test(void)
  643. {
  644. Hmac hmac;
  645. byte hash[SHA384_DIGEST_SIZE];
  646. const char* keys[]=
  647. {
  648. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  649. "\x0b\x0b\x0b",
  650. "Jefe",
  651. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  652. "\xAA\xAA\xAA"
  653. };
  654. testVector a, b, c;
  655. testVector test_hmac[3];
  656. int ret;
  657. int times = sizeof(test_hmac) / sizeof(testVector), i;
  658. a.input = "Hi There";
  659. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  660. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  661. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  662. "\xfa\x9c\xb6";
  663. a.inLen = strlen(a.input);
  664. a.outLen = strlen(a.output);
  665. b.input = "what do ya want for nothing?";
  666. b.output = "\xaf\x45\xd2\xe3\x76\x48\x40\x31\x61\x7f\x78\xd2\xb5\x8a\x6b"
  667. "\x1b\x9c\x7e\xf4\x64\xf5\xa0\x1b\x47\xe4\x2e\xc3\x73\x63\x22"
  668. "\x44\x5e\x8e\x22\x40\xca\x5e\x69\xe2\xc7\x8b\x32\x39\xec\xfa"
  669. "\xb2\x16\x49";
  670. b.inLen = strlen(b.input);
  671. b.outLen = strlen(b.output);
  672. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  673. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  674. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  675. "\xDD\xDD\xDD\xDD\xDD\xDD";
  676. c.output = "\x88\x06\x26\x08\xd3\xe6\xad\x8a\x0a\xa2\xac\xe0\x14\xc8\xa8"
  677. "\x6f\x0a\xa6\x35\xd9\x47\xac\x9f\xeb\xe8\x3e\xf4\xe5\x59\x66"
  678. "\x14\x4b\x2a\x5a\xb3\x9d\xc1\x38\x14\xb9\x4e\x3a\xb6\xe1\x01"
  679. "\xa3\x4f\x27";
  680. c.inLen = strlen(c.input);
  681. c.outLen = strlen(c.output);
  682. test_hmac[0] = a;
  683. test_hmac[1] = b;
  684. test_hmac[2] = c;
  685. for (i = 0; i < times; ++i) {
  686. #if defined(HAVE_FIPS)
  687. if (i == 1)
  688. continue; /* fips not allowed */
  689. #endif
  690. ret = wc_HmacSetKey(&hmac,SHA384, (byte*)keys[i], (word32)strlen(keys[i]));
  691. if (ret != 0)
  692. return -4023;
  693. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  694. (word32)test_hmac[i].inLen);
  695. if (ret != 0)
  696. return -4024;
  697. ret = wc_HmacFinal(&hmac, hash);
  698. if (ret != 0)
  699. return -4025;
  700. if (memcmp(hash, test_hmac[i].output, SHA384_DIGEST_SIZE) != 0)
  701. return -20 - i;
  702. }
  703. return 0;
  704. }
  705. #endif