hash.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. /* hash.c has unit tests
  2. *
  3. * Copyright (C) 2006-2013 wolfSSL Inc.
  4. *
  5. * This file is part of CyaSSL.
  6. *
  7. * CyaSSL 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. * CyaSSL 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <cyassl/ctaocrypt/settings.h>
  25. #include <stdio.h>
  26. #include <cyassl/ctaocrypt/md4.h>
  27. #include <cyassl/ctaocrypt/md5.h>
  28. #include <cyassl/ctaocrypt/sha.h>
  29. #include <cyassl/ctaocrypt/sha256.h>
  30. #include <cyassl/ctaocrypt/sha512.h>
  31. #include <cyassl/ctaocrypt/ripemd.h>
  32. #include <cyassl/ctaocrypt/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 CYASSL_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 CYASSL_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 CYASSL_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. if ( (ret = hmac_sha_test()) )
  113. printf( " HMAC-SHA test failed!\n");
  114. else
  115. printf( " HMAC-SHA test passed!\n");
  116. #ifndef NO_SHA256
  117. if ( (ret = hmac_sha256_test()) )
  118. printf( " HMAC-SHA256 test failed!\n");
  119. else
  120. printf( " HMAC-SHA256 test passed!\n");
  121. #endif
  122. #ifdef CYASSL_SHA384
  123. if ( (ret = hmac_sha384_test()) )
  124. printf( " HMAC-SHA384 test failed!\n");
  125. else
  126. printf( " HMAC-SHA384 test passed!\n");
  127. #endif
  128. #endif
  129. printf(" End HASH Tests\n");
  130. return 0;
  131. }
  132. #ifndef NO_MD4
  133. int md4_test(void)
  134. {
  135. Md4 md4;
  136. byte hash[MD4_DIGEST_SIZE];
  137. testVector a, b, c, d, e, f, g;
  138. testVector test_md4[7];
  139. int times = sizeof(test_md4) / sizeof(testVector), i;
  140. a.input = "";
  141. a.output = "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
  142. "\xc0";
  143. a.inLen = strlen(a.input);
  144. a.outLen = strlen(a.output);
  145. b.input = "a";
  146. b.output = "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
  147. "\x24";
  148. b.inLen = strlen(b.input);
  149. b.outLen = strlen(b.output);
  150. c.input = "abc";
  151. c.output = "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
  152. "\x9d";
  153. c.inLen = strlen(c.input);
  154. c.outLen = strlen(c.output);
  155. d.input = "message digest";
  156. d.output = "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
  157. "\x4b";
  158. d.inLen = strlen(d.input);
  159. d.outLen = strlen(d.output);
  160. e.input = "abcdefghijklmnopqrstuvwxyz";
  161. e.output = "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
  162. "\xa9";
  163. e.inLen = strlen(e.input);
  164. e.outLen = strlen(e.output);
  165. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  166. "6789";
  167. f.output = "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
  168. "\xe4";
  169. f.inLen = strlen(f.input);
  170. f.outLen = strlen(f.output);
  171. g.input = "1234567890123456789012345678901234567890123456789012345678"
  172. "9012345678901234567890";
  173. g.output = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
  174. "\x36";
  175. g.inLen = strlen(g.input);
  176. g.outLen = strlen(g.output);
  177. test_md4[0] = a;
  178. test_md4[1] = b;
  179. test_md4[2] = c;
  180. test_md4[3] = d;
  181. test_md4[4] = e;
  182. test_md4[5] = f;
  183. test_md4[6] = g;
  184. InitMd4(&md4);
  185. for (i = 0; i < times; ++i) {
  186. Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen);
  187. Md4Final(&md4, hash);
  188. if (memcmp(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0)
  189. return -205 - i;
  190. }
  191. return 0;
  192. }
  193. #endif /* NO_MD4 */
  194. #ifndef NO_MD5
  195. int md5_test(void)
  196. {
  197. Md5 md5;
  198. byte hash[MD5_DIGEST_SIZE];
  199. testVector a, b, c, d, e;
  200. testVector test_md5[5];
  201. int times = sizeof(test_md5) / sizeof(testVector), i;
  202. a.input = "abc";
  203. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  204. "\x72";
  205. a.inLen = strlen(a.input);
  206. a.outLen = strlen(a.output);
  207. b.input = "message digest";
  208. b.output = "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
  209. "\xd0";
  210. b.inLen = strlen(b.input);
  211. b.outLen = strlen(b.output);
  212. c.input = "abcdefghijklmnopqrstuvwxyz";
  213. c.output = "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
  214. "\x3b";
  215. c.inLen = strlen(c.input);
  216. c.outLen = strlen(c.output);
  217. d.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  218. "6789";
  219. d.output = "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
  220. "\x9f";
  221. d.inLen = strlen(d.input);
  222. d.outLen = strlen(d.output);
  223. e.input = "1234567890123456789012345678901234567890123456789012345678"
  224. "9012345678901234567890";
  225. e.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  226. "\x7a";
  227. e.inLen = strlen(e.input);
  228. e.outLen = strlen(e.output);
  229. test_md5[0] = a;
  230. test_md5[1] = b;
  231. test_md5[2] = c;
  232. test_md5[3] = d;
  233. test_md5[4] = e;
  234. InitMd5(&md5);
  235. for (i = 0; i < times; ++i) {
  236. Md5Update(&md5, (byte*)test_md5[i].input, (word32)test_md5[i].inLen);
  237. Md5Final(&md5, hash);
  238. if (memcmp(hash, test_md5[i].output, MD5_DIGEST_SIZE) != 0)
  239. return -5 - i;
  240. }
  241. return 0;
  242. }
  243. #endif /* NO_MD5 */
  244. #ifndef NO_SHA
  245. int sha_test(void)
  246. {
  247. Sha sha;
  248. byte hash[SHA_DIGEST_SIZE];
  249. testVector a, b, c, d;
  250. testVector test_sha[4];
  251. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  252. a.input = "abc";
  253. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  254. "\x6C\x9C\xD0\xD8\x9D";
  255. a.inLen = strlen(a.input);
  256. a.outLen = strlen(a.output);
  257. b.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  258. b.output = "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
  259. "\xE5\xE5\x46\x70\xF1";
  260. b.inLen = strlen(b.input);
  261. b.outLen = strlen(b.output);
  262. c.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  263. "aaaaaa";
  264. c.output = "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
  265. "\x2A\x25\xEC\x64\x4D";
  266. c.inLen = strlen(c.input);
  267. c.outLen = strlen(c.output);
  268. d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  269. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  270. "aaaaaaaaaa";
  271. d.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  272. "\x53\x99\x5E\x26\xA0";
  273. d.inLen = strlen(d.input);
  274. d.outLen = strlen(d.output);
  275. test_sha[0] = a;
  276. test_sha[1] = b;
  277. test_sha[2] = c;
  278. test_sha[3] = d;
  279. InitSha(&sha);
  280. for (i = 0; i < times; ++i) {
  281. ShaUpdate(&sha, (byte*)test_sha[i].input, (word32)test_sha[i].inLen);
  282. ShaFinal(&sha, hash);
  283. if (memcmp(hash, test_sha[i].output, SHA_DIGEST_SIZE) != 0)
  284. return -10 - i;
  285. }
  286. return 0;
  287. }
  288. #endif /* NO_SHA */
  289. #ifndef NO_SHA256
  290. int sha256_test(void)
  291. {
  292. Sha256 sha;
  293. byte hash[SHA256_DIGEST_SIZE];
  294. testVector a, b;
  295. testVector test_sha[2];
  296. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  297. a.input = "abc";
  298. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  299. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  300. "\x15\xAD";
  301. a.inLen = strlen(a.input);
  302. a.outLen = strlen(a.output);
  303. b.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  304. b.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  305. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  306. "\x06\xC1";
  307. b.inLen = strlen(b.input);
  308. b.outLen = strlen(b.output);
  309. test_sha[0] = a;
  310. test_sha[1] = b;
  311. InitSha256(&sha);
  312. for (i = 0; i < times; ++i) {
  313. Sha256Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen);
  314. Sha256Final(&sha, hash);
  315. if (memcmp(hash, test_sha[i].output, SHA256_DIGEST_SIZE) != 0)
  316. return -10 - i;
  317. }
  318. return 0;
  319. }
  320. #endif
  321. #ifdef CYASSL_SHA512
  322. int sha512_test(void)
  323. {
  324. Sha512 sha;
  325. byte hash[SHA512_DIGEST_SIZE];
  326. testVector a, b;
  327. testVector test_sha[2];
  328. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  329. a.input = "abc";
  330. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  331. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  332. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  333. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  334. "\xa5\x4c\xa4\x9f";
  335. a.inLen = strlen(a.input);
  336. a.outLen = strlen(a.output);
  337. b.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  338. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  339. b.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  340. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  341. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  342. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  343. "\x87\x4b\xe9\x09";
  344. b.inLen = strlen(b.input);
  345. b.outLen = strlen(b.output);
  346. test_sha[0] = a;
  347. test_sha[1] = b;
  348. InitSha512(&sha);
  349. for (i = 0; i < times; ++i) {
  350. Sha512Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen);
  351. Sha512Final(&sha, hash);
  352. if (memcmp(hash, test_sha[i].output, SHA512_DIGEST_SIZE) != 0)
  353. return -10 - i;
  354. }
  355. return 0;
  356. }
  357. #endif
  358. #ifdef CYASSL_SHA384
  359. int sha384_test()
  360. {
  361. Sha384 sha;
  362. byte hash[SHA384_DIGEST_SIZE];
  363. testVector a, b;
  364. testVector test_sha[2];
  365. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  366. a.input = "abc";
  367. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  368. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  369. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  370. "\xc8\x25\xa7";
  371. a.inLen = strlen(a.input);
  372. a.outLen = strlen(a.output);
  373. b.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  374. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  375. b.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  376. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  377. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  378. "\x74\x60\x39";
  379. b.inLen = strlen(b.input);
  380. b.outLen = strlen(b.output);
  381. test_sha[0] = a;
  382. test_sha[1] = b;
  383. InitSha384(&sha);
  384. for (i = 0; i < times; ++i) {
  385. Sha384Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen);
  386. Sha384Final(&sha, hash);
  387. if (memcmp(hash, test_sha[i].output, SHA384_DIGEST_SIZE) != 0)
  388. return -10 - i;
  389. }
  390. return 0;
  391. }
  392. #endif
  393. #ifdef CYASSL_RIPEMD
  394. int ripemd_test(void)
  395. {
  396. RipeMd ripemd;
  397. byte hash[RIPEMD_DIGEST_SIZE];
  398. testVector a, b, c, d;
  399. testVector test_ripemd[4];
  400. int times = sizeof(test_ripemd) / sizeof(struct testVector), i;
  401. a.input = "abc";
  402. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  403. "\xb0\x87\xf1\x5a\x0b\xfc";
  404. a.inLen = strlen(a.input);
  405. a.outLen = strlen(a.output);
  406. b.input = "message digest";
  407. b.output = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
  408. "\x5f\xfa\x21\x59\x5f\x36";
  409. b.inLen = strlen(b.input);
  410. b.outLen = strlen(b.output);
  411. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  412. c.output = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
  413. "\xf4\x9a\xda\x62\xeb\x2b";
  414. c.inLen = strlen(c.input);
  415. c.outLen = strlen(c.output);
  416. d.input = "12345678901234567890123456789012345678901234567890123456"
  417. "789012345678901234567890";
  418. d.output = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
  419. "\x82\xbf\x63\x32\x6b\xfb";
  420. d.inLen = strlen(d.input);
  421. d.outLen = strlen(d.output);
  422. test_ripemd[0] = a;
  423. test_ripemd[1] = b;
  424. test_ripemd[2] = c;
  425. test_ripemd[3] = d;
  426. InitRipeMd(&ripemd);
  427. for (i = 0; i < times; ++i) {
  428. RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input,
  429. (word32)test_ripemd[i].inLen);
  430. RipeMdFinal(&ripemd, hash);
  431. if (memcmp(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0)
  432. return -10 - i;
  433. }
  434. return 0;
  435. }
  436. #endif /* CYASSL_RIPEMD */
  437. #if !defined(NO_HMAC) && !defined(NO_MD5)
  438. int hmac_md5_test(void)
  439. {
  440. Hmac hmac;
  441. byte hash[MD5_DIGEST_SIZE];
  442. const char* keys[]=
  443. {
  444. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  445. "Jefe",
  446. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  447. };
  448. testVector a, b, c;
  449. testVector test_hmac[3];
  450. int times = sizeof(test_hmac) / sizeof(testVector), i;
  451. a.input = "Hi There";
  452. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  453. "\x9d";
  454. a.inLen = strlen(a.input);
  455. a.outLen = strlen(a.output);
  456. b.input = "what do ya want for nothing?";
  457. b.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
  458. "\x38";
  459. b.inLen = strlen(b.input);
  460. b.outLen = strlen(b.output);
  461. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  462. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  463. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  464. "\xDD\xDD\xDD\xDD\xDD\xDD";
  465. c.output = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
  466. "\xf6";
  467. c.inLen = strlen(c.input);
  468. c.outLen = strlen(c.output);
  469. test_hmac[0] = a;
  470. test_hmac[1] = b;
  471. test_hmac[2] = c;
  472. for (i = 0; i < times; ++i) {
  473. HmacSetKey(&hmac, MD5, (byte*)keys[i], (word32)strlen(keys[i]));
  474. HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  475. (word32)test_hmac[i].inLen);
  476. HmacFinal(&hmac, hash);
  477. if (memcmp(hash, test_hmac[i].output, MD5_DIGEST_SIZE) != 0)
  478. return -20 - i;
  479. }
  480. return 0;
  481. }
  482. #endif
  483. #ifndef NO_HMAC
  484. int hmac_sha_test(void)
  485. {
  486. Hmac hmac;
  487. byte hash[SHA_DIGEST_SIZE];
  488. const char* keys[]=
  489. {
  490. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  491. "\x0b\x0b\x0b",
  492. "Jefe",
  493. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  494. "\xAA\xAA\xAA"
  495. };
  496. testVector a, b, c;
  497. testVector test_hmac[3];
  498. int times = sizeof(test_hmac) / sizeof(testVector), i;
  499. a.input = "Hi There";
  500. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  501. "\x8e\xf1\x46\xbe\x00";
  502. a.inLen = strlen(a.input);
  503. a.outLen = strlen(a.output);
  504. b.input = "what do ya want for nothing?";
  505. b.output = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf"
  506. "\x9c\x25\x9a\x7c\x79";
  507. b.inLen = strlen(b.input);
  508. b.outLen = strlen(b.output);
  509. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  510. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  511. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  512. "\xDD\xDD\xDD\xDD\xDD\xDD";
  513. c.output = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b"
  514. "\x4f\x63\xf1\x75\xd3";
  515. c.inLen = strlen(c.input);
  516. c.outLen = strlen(c.output);
  517. test_hmac[0] = a;
  518. test_hmac[1] = b;
  519. test_hmac[2] = c;
  520. for (i = 0; i < times; ++i) {
  521. HmacSetKey(&hmac, SHA, (byte*)keys[i], (word32)strlen(keys[i]));
  522. HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  523. (word32)test_hmac[i].inLen);
  524. HmacFinal(&hmac, hash);
  525. if (memcmp(hash, test_hmac[i].output, SHA_DIGEST_SIZE) != 0)
  526. return -20 - i;
  527. }
  528. return 0;
  529. }
  530. #endif
  531. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  532. int hmac_sha256_test(void)
  533. {
  534. Hmac hmac;
  535. byte hash[SHA256_DIGEST_SIZE];
  536. const char* keys[]=
  537. {
  538. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  539. "\x0b\x0b\x0b",
  540. "Jefe",
  541. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  542. "\xAA\xAA\xAA"
  543. };
  544. testVector a, b, c;
  545. testVector test_hmac[3];
  546. int times = sizeof(test_hmac) / sizeof(testVector), i;
  547. a.input = "Hi There";
  548. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  549. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  550. "\xcf\xf7";
  551. a.inLen = strlen(a.input);
  552. a.outLen = strlen(a.output);
  553. b.input = "what do ya want for nothing?";
  554. b.output = "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75"
  555. "\xc7\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec"
  556. "\x38\x43";
  557. b.inLen = strlen(b.input);
  558. b.outLen = strlen(b.output);
  559. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  560. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  561. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  562. "\xDD\xDD\xDD\xDD\xDD\xDD";
  563. c.output = "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81"
  564. "\xa7\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5"
  565. "\x65\xfe";
  566. c.inLen = strlen(c.input);
  567. c.outLen = strlen(c.output);
  568. test_hmac[0] = a;
  569. test_hmac[1] = b;
  570. test_hmac[2] = c;
  571. for (i = 0; i < times; ++i) {
  572. HmacSetKey(&hmac, SHA256, (byte*)keys[i], (word32)strlen(keys[i]));
  573. HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  574. (word32)test_hmac[i].inLen);
  575. HmacFinal(&hmac, hash);
  576. if (memcmp(hash, test_hmac[i].output, SHA256_DIGEST_SIZE) != 0)
  577. return -20 - i;
  578. }
  579. return 0;
  580. }
  581. #endif
  582. #if !defined(NO_HMAC) && defined(CYASSL_SHA384)
  583. int hmac_sha384_test(void)
  584. {
  585. Hmac hmac;
  586. byte hash[SHA384_DIGEST_SIZE];
  587. const char* keys[]=
  588. {
  589. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  590. "\x0b\x0b\x0b",
  591. "Jefe",
  592. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  593. "\xAA\xAA\xAA"
  594. };
  595. testVector a, b, c;
  596. testVector test_hmac[3];
  597. int times = sizeof(test_hmac) / sizeof(testVector), i;
  598. a.input = "Hi There";
  599. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  600. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  601. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  602. "\xfa\x9c\xb6";
  603. a.inLen = strlen(a.input);
  604. a.outLen = strlen(a.output);
  605. b.input = "what do ya want for nothing?";
  606. b.output = "\xaf\x45\xd2\xe3\x76\x48\x40\x31\x61\x7f\x78\xd2\xb5\x8a\x6b"
  607. "\x1b\x9c\x7e\xf4\x64\xf5\xa0\x1b\x47\xe4\x2e\xc3\x73\x63\x22"
  608. "\x44\x5e\x8e\x22\x40\xca\x5e\x69\xe2\xc7\x8b\x32\x39\xec\xfa"
  609. "\xb2\x16\x49";
  610. b.inLen = strlen(b.input);
  611. b.outLen = strlen(b.output);
  612. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  613. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  614. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  615. "\xDD\xDD\xDD\xDD\xDD\xDD";
  616. c.output = "\x88\x06\x26\x08\xd3\xe6\xad\x8a\x0a\xa2\xac\xe0\x14\xc8\xa8"
  617. "\x6f\x0a\xa6\x35\xd9\x47\xac\x9f\xeb\xe8\x3e\xf4\xe5\x59\x66"
  618. "\x14\x4b\x2a\x5a\xb3\x9d\xc1\x38\x14\xb9\x4e\x3a\xb6\xe1\x01"
  619. "\xa3\x4f\x27";
  620. c.inLen = strlen(c.input);
  621. c.outLen = strlen(c.output);
  622. test_hmac[0] = a;
  623. test_hmac[1] = b;
  624. test_hmac[2] = c;
  625. for (i = 0; i < times; ++i) {
  626. HmacSetKey(&hmac, SHA384, (byte*)keys[i], (word32)strlen(keys[i]));
  627. HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  628. (word32)test_hmac[i].inLen);
  629. HmacFinal(&hmac, hash);
  630. if (memcmp(hash, test_hmac[i].output, SHA384_DIGEST_SIZE) != 0)
  631. return -20 - i;
  632. }
  633. return 0;
  634. }
  635. #endif