hash.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. /* hash.c has unit tests
  2. *
  3. * Copyright (C) 2006-2014 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 ret = 0;
  252. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  253. a.input = "abc";
  254. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  255. "\x6C\x9C\xD0\xD8\x9D";
  256. a.inLen = strlen(a.input);
  257. a.outLen = strlen(a.output);
  258. b.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  259. b.output = "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
  260. "\xE5\xE5\x46\x70\xF1";
  261. b.inLen = strlen(b.input);
  262. b.outLen = strlen(b.output);
  263. c.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  264. "aaaaaa";
  265. c.output = "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
  266. "\x2A\x25\xEC\x64\x4D";
  267. c.inLen = strlen(c.input);
  268. c.outLen = strlen(c.output);
  269. d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  270. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  271. "aaaaaaaaaa";
  272. d.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  273. "\x53\x99\x5E\x26\xA0";
  274. d.inLen = strlen(d.input);
  275. d.outLen = strlen(d.output);
  276. test_sha[0] = a;
  277. test_sha[1] = b;
  278. test_sha[2] = c;
  279. test_sha[3] = d;
  280. ret = InitSha(&sha);
  281. if (ret != 0)
  282. return ret;
  283. for (i = 0; i < times; ++i) {
  284. ShaUpdate(&sha, (byte*)test_sha[i].input, (word32)test_sha[i].inLen);
  285. ShaFinal(&sha, hash);
  286. if (memcmp(hash, test_sha[i].output, SHA_DIGEST_SIZE) != 0)
  287. return -10 - i;
  288. }
  289. return 0;
  290. }
  291. #endif /* NO_SHA */
  292. #ifndef NO_SHA256
  293. int sha256_test(void)
  294. {
  295. Sha256 sha;
  296. byte hash[SHA256_DIGEST_SIZE];
  297. testVector a, b;
  298. testVector test_sha[2];
  299. int ret;
  300. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  301. a.input = "abc";
  302. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  303. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  304. "\x15\xAD";
  305. a.inLen = strlen(a.input);
  306. a.outLen = strlen(a.output);
  307. b.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  308. b.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  309. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  310. "\x06\xC1";
  311. b.inLen = strlen(b.input);
  312. b.outLen = strlen(b.output);
  313. test_sha[0] = a;
  314. test_sha[1] = b;
  315. ret = InitSha256(&sha);
  316. if (ret != 0)
  317. return ret;
  318. for (i = 0; i < times; ++i) {
  319. ret = Sha256Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen);
  320. if (ret != 0)
  321. return ret;
  322. ret = Sha256Final(&sha, hash);
  323. if (ret != 0)
  324. return ret;
  325. if (memcmp(hash, test_sha[i].output, SHA256_DIGEST_SIZE) != 0)
  326. return -10 - i;
  327. }
  328. return 0;
  329. }
  330. #endif
  331. #ifdef CYASSL_SHA512
  332. int sha512_test(void)
  333. {
  334. Sha512 sha;
  335. byte hash[SHA512_DIGEST_SIZE];
  336. testVector a, b;
  337. testVector test_sha[2];
  338. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  339. int ret;
  340. a.input = "abc";
  341. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  342. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  343. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  344. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  345. "\xa5\x4c\xa4\x9f";
  346. a.inLen = strlen(a.input);
  347. a.outLen = strlen(a.output);
  348. b.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  349. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  350. b.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  351. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  352. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  353. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  354. "\x87\x4b\xe9\x09";
  355. b.inLen = strlen(b.input);
  356. b.outLen = strlen(b.output);
  357. test_sha[0] = a;
  358. test_sha[1] = b;
  359. ret = InitSha512(&sha);
  360. if (ret != 0)
  361. return ret;
  362. for (i = 0; i < times; ++i) {
  363. ret = Sha512Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen);
  364. if (ret != 0)
  365. return ret;
  366. ret = Sha512Final(&sha, hash);
  367. if (ret != 0)
  368. return ret;
  369. if (memcmp(hash, test_sha[i].output, SHA512_DIGEST_SIZE) != 0)
  370. return -10 - i;
  371. }
  372. return 0;
  373. }
  374. #endif
  375. #ifdef CYASSL_SHA384
  376. int sha384_test()
  377. {
  378. Sha384 sha;
  379. byte hash[SHA384_DIGEST_SIZE];
  380. testVector a, b;
  381. testVector test_sha[2];
  382. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  383. int ret;
  384. a.input = "abc";
  385. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  386. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  387. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  388. "\xc8\x25\xa7";
  389. a.inLen = strlen(a.input);
  390. a.outLen = strlen(a.output);
  391. b.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  392. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  393. b.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  394. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  395. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  396. "\x74\x60\x39";
  397. b.inLen = strlen(b.input);
  398. b.outLen = strlen(b.output);
  399. test_sha[0] = a;
  400. test_sha[1] = b;
  401. ret = InitSha384(&sha);
  402. if (ret != 0)
  403. return ret;
  404. for (i = 0; i < times; ++i) {
  405. ret = Sha384Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen);
  406. if (ret != 0)
  407. return ret;
  408. ret = Sha384Final(&sha, hash);
  409. if (ret != 0)
  410. return ret;
  411. if (memcmp(hash, test_sha[i].output, SHA384_DIGEST_SIZE) != 0)
  412. return -10 - i;
  413. }
  414. return 0;
  415. }
  416. #endif
  417. #ifdef CYASSL_RIPEMD
  418. int ripemd_test(void)
  419. {
  420. RipeMd ripemd;
  421. byte hash[RIPEMD_DIGEST_SIZE];
  422. testVector a, b, c, d;
  423. testVector test_ripemd[4];
  424. int times = sizeof(test_ripemd) / sizeof(struct testVector), i;
  425. a.input = "abc";
  426. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  427. "\xb0\x87\xf1\x5a\x0b\xfc";
  428. a.inLen = strlen(a.input);
  429. a.outLen = strlen(a.output);
  430. b.input = "message digest";
  431. b.output = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
  432. "\x5f\xfa\x21\x59\x5f\x36";
  433. b.inLen = strlen(b.input);
  434. b.outLen = strlen(b.output);
  435. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  436. c.output = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
  437. "\xf4\x9a\xda\x62\xeb\x2b";
  438. c.inLen = strlen(c.input);
  439. c.outLen = strlen(c.output);
  440. d.input = "12345678901234567890123456789012345678901234567890123456"
  441. "789012345678901234567890";
  442. d.output = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
  443. "\x82\xbf\x63\x32\x6b\xfb";
  444. d.inLen = strlen(d.input);
  445. d.outLen = strlen(d.output);
  446. test_ripemd[0] = a;
  447. test_ripemd[1] = b;
  448. test_ripemd[2] = c;
  449. test_ripemd[3] = d;
  450. InitRipeMd(&ripemd);
  451. for (i = 0; i < times; ++i) {
  452. RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input,
  453. (word32)test_ripemd[i].inLen);
  454. RipeMdFinal(&ripemd, hash);
  455. if (memcmp(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0)
  456. return -10 - i;
  457. }
  458. return 0;
  459. }
  460. #endif /* CYASSL_RIPEMD */
  461. #if !defined(NO_HMAC) && !defined(NO_MD5)
  462. int hmac_md5_test(void)
  463. {
  464. Hmac hmac;
  465. byte hash[MD5_DIGEST_SIZE];
  466. const char* keys[]=
  467. {
  468. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  469. "Jefe",
  470. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  471. };
  472. testVector a, b, c;
  473. testVector test_hmac[3];
  474. int ret;
  475. int times = sizeof(test_hmac) / sizeof(testVector), i;
  476. a.input = "Hi There";
  477. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  478. "\x9d";
  479. a.inLen = strlen(a.input);
  480. a.outLen = strlen(a.output);
  481. b.input = "what do ya want for nothing?";
  482. b.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
  483. "\x38";
  484. b.inLen = strlen(b.input);
  485. b.outLen = strlen(b.output);
  486. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  487. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  488. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  489. "\xDD\xDD\xDD\xDD\xDD\xDD";
  490. c.output = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
  491. "\xf6";
  492. c.inLen = strlen(c.input);
  493. c.outLen = strlen(c.output);
  494. test_hmac[0] = a;
  495. test_hmac[1] = b;
  496. test_hmac[2] = c;
  497. for (i = 0; i < times; ++i) {
  498. #if defined(HAVE_FIPS)
  499. if (i == 1)
  500. continue; /* fips not allowed */
  501. #endif
  502. ret = HmacSetKey(&hmac, MD5, (byte*)keys[i], (word32)strlen(keys[i]));
  503. if (ret != 0)
  504. return -4014;
  505. ret = HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  506. (word32)test_hmac[i].inLen);
  507. if (ret != 0)
  508. return -4015;
  509. ret = HmacFinal(&hmac, hash);
  510. if (ret != 0)
  511. return -4016;
  512. if (memcmp(hash, test_hmac[i].output, MD5_DIGEST_SIZE) != 0)
  513. return -20 - i;
  514. }
  515. return 0;
  516. }
  517. #endif
  518. #ifndef NO_HMAC
  519. int hmac_sha_test(void)
  520. {
  521. Hmac hmac;
  522. byte hash[SHA_DIGEST_SIZE];
  523. const char* keys[]=
  524. {
  525. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  526. "\x0b\x0b\x0b",
  527. "Jefe",
  528. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  529. "\xAA\xAA\xAA"
  530. };
  531. testVector a, b, c;
  532. testVector test_hmac[3];
  533. int ret;
  534. int times = sizeof(test_hmac) / sizeof(testVector), i;
  535. a.input = "Hi There";
  536. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  537. "\x8e\xf1\x46\xbe\x00";
  538. a.inLen = strlen(a.input);
  539. a.outLen = strlen(a.output);
  540. b.input = "what do ya want for nothing?";
  541. b.output = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf"
  542. "\x9c\x25\x9a\x7c\x79";
  543. b.inLen = strlen(b.input);
  544. b.outLen = strlen(b.output);
  545. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  546. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  547. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  548. "\xDD\xDD\xDD\xDD\xDD\xDD";
  549. c.output = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b"
  550. "\x4f\x63\xf1\x75\xd3";
  551. c.inLen = strlen(c.input);
  552. c.outLen = strlen(c.output);
  553. test_hmac[0] = a;
  554. test_hmac[1] = b;
  555. test_hmac[2] = c;
  556. for (i = 0; i < times; ++i) {
  557. #if defined(HAVE_FIPS)
  558. if (i == 1)
  559. continue; /* fips not allowed */
  560. #endif
  561. ret = HmacSetKey(&hmac, SHA, (byte*)keys[i], (word32)strlen(keys[i]));
  562. if (ret != 0)
  563. return -4017;
  564. ret = HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  565. (word32)test_hmac[i].inLen);
  566. if (ret != 0)
  567. return -4018;
  568. ret = HmacFinal(&hmac, hash);
  569. if (ret != 0)
  570. return -4019;
  571. if (memcmp(hash, test_hmac[i].output, SHA_DIGEST_SIZE) != 0)
  572. return -20 - i;
  573. }
  574. return 0;
  575. }
  576. #endif
  577. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  578. int hmac_sha256_test(void)
  579. {
  580. Hmac hmac;
  581. byte hash[SHA256_DIGEST_SIZE];
  582. const char* keys[]=
  583. {
  584. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  585. "\x0b\x0b\x0b",
  586. "Jefe",
  587. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  588. "\xAA\xAA\xAA"
  589. };
  590. testVector a, b, c;
  591. testVector test_hmac[3];
  592. int ret;
  593. int times = sizeof(test_hmac) / sizeof(testVector), i;
  594. a.input = "Hi There";
  595. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  596. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  597. "\xcf\xf7";
  598. a.inLen = strlen(a.input);
  599. a.outLen = strlen(a.output);
  600. b.input = "what do ya want for nothing?";
  601. b.output = "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75"
  602. "\xc7\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec"
  603. "\x38\x43";
  604. b.inLen = strlen(b.input);
  605. b.outLen = strlen(b.output);
  606. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  607. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  608. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  609. "\xDD\xDD\xDD\xDD\xDD\xDD";
  610. c.output = "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81"
  611. "\xa7\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5"
  612. "\x65\xfe";
  613. c.inLen = strlen(c.input);
  614. c.outLen = strlen(c.output);
  615. test_hmac[0] = a;
  616. test_hmac[1] = b;
  617. test_hmac[2] = c;
  618. for (i = 0; i < times; ++i) {
  619. #if defined(HAVE_FIPS)
  620. if (i == 1)
  621. continue; /* fips not allowed */
  622. #endif
  623. ret = HmacSetKey(&hmac,SHA256, (byte*)keys[i], (word32)strlen(keys[i]));
  624. if (ret != 0)
  625. return -4020;
  626. ret = HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  627. (word32)test_hmac[i].inLen);
  628. if (ret != 0)
  629. return -4021;
  630. ret = HmacFinal(&hmac, hash);
  631. if (ret != 0)
  632. return -4022;
  633. if (memcmp(hash, test_hmac[i].output, SHA256_DIGEST_SIZE) != 0)
  634. return -20 - i;
  635. }
  636. return 0;
  637. }
  638. #endif
  639. #if !defined(NO_HMAC) && defined(CYASSL_SHA384)
  640. int hmac_sha384_test(void)
  641. {
  642. Hmac hmac;
  643. byte hash[SHA384_DIGEST_SIZE];
  644. const char* keys[]=
  645. {
  646. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  647. "\x0b\x0b\x0b",
  648. "Jefe",
  649. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  650. "\xAA\xAA\xAA"
  651. };
  652. testVector a, b, c;
  653. testVector test_hmac[3];
  654. int ret;
  655. int times = sizeof(test_hmac) / sizeof(testVector), i;
  656. a.input = "Hi There";
  657. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  658. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  659. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  660. "\xfa\x9c\xb6";
  661. a.inLen = strlen(a.input);
  662. a.outLen = strlen(a.output);
  663. b.input = "what do ya want for nothing?";
  664. b.output = "\xaf\x45\xd2\xe3\x76\x48\x40\x31\x61\x7f\x78\xd2\xb5\x8a\x6b"
  665. "\x1b\x9c\x7e\xf4\x64\xf5\xa0\x1b\x47\xe4\x2e\xc3\x73\x63\x22"
  666. "\x44\x5e\x8e\x22\x40\xca\x5e\x69\xe2\xc7\x8b\x32\x39\xec\xfa"
  667. "\xb2\x16\x49";
  668. b.inLen = strlen(b.input);
  669. b.outLen = strlen(b.output);
  670. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  671. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  672. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  673. "\xDD\xDD\xDD\xDD\xDD\xDD";
  674. c.output = "\x88\x06\x26\x08\xd3\xe6\xad\x8a\x0a\xa2\xac\xe0\x14\xc8\xa8"
  675. "\x6f\x0a\xa6\x35\xd9\x47\xac\x9f\xeb\xe8\x3e\xf4\xe5\x59\x66"
  676. "\x14\x4b\x2a\x5a\xb3\x9d\xc1\x38\x14\xb9\x4e\x3a\xb6\xe1\x01"
  677. "\xa3\x4f\x27";
  678. c.inLen = strlen(c.input);
  679. c.outLen = strlen(c.output);
  680. test_hmac[0] = a;
  681. test_hmac[1] = b;
  682. test_hmac[2] = c;
  683. for (i = 0; i < times; ++i) {
  684. #if defined(HAVE_FIPS)
  685. if (i == 1)
  686. continue; /* fips not allowed */
  687. #endif
  688. ret = HmacSetKey(&hmac,SHA384, (byte*)keys[i], (word32)strlen(keys[i]));
  689. if (ret != 0)
  690. return -4023;
  691. ret = HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  692. (word32)test_hmac[i].inLen);
  693. if (ret != 0)
  694. return -4024;
  695. ret = HmacFinal(&hmac, hash);
  696. if (ret != 0)
  697. return -4025;
  698. if (memcmp(hash, test_hmac[i].output, SHA384_DIGEST_SIZE) != 0)
  699. return -20 - i;
  700. }
  701. return 0;
  702. }
  703. #endif