hmac_sha2.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /*
  2. * HMAC-SHA-224/256/384/512 implementation
  3. * Last update: 06/15/2005
  4. * Issue date: 06/15/2005
  5. *
  6. * Since this code has been incorporated into a GPLv2 project, it is
  7. * distributed under GPLv2 inside mmc-utils. The original BSD license
  8. * that the code was released under is included below for clarity.
  9. *
  10. * Copyright (C) 2005 Olivier Gay <olivier.gay@a3.epfl.ch>
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. * 1. Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. * 3. Neither the name of the project nor the names of its contributors
  22. * may be used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  26. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35. * SUCH DAMAGE.
  36. */
  37. #include <string.h>
  38. #include "hmac_sha2.h"
  39. /* HMAC-SHA-224 functions */
  40. void hmac_sha224_init(hmac_sha224_ctx *ctx, const unsigned char *key,
  41. unsigned int key_size)
  42. {
  43. unsigned int fill;
  44. unsigned int num;
  45. const unsigned char *key_used;
  46. unsigned char key_temp[SHA224_DIGEST_SIZE];
  47. int i;
  48. if (key_size == SHA224_BLOCK_SIZE) {
  49. key_used = key;
  50. num = SHA224_BLOCK_SIZE;
  51. } else {
  52. if (key_size > SHA224_BLOCK_SIZE){
  53. num = SHA224_DIGEST_SIZE;
  54. sha224(key, key_size, key_temp);
  55. key_used = key_temp;
  56. } else { /* key_size > SHA224_BLOCK_SIZE */
  57. key_used = key;
  58. num = key_size;
  59. }
  60. fill = SHA224_BLOCK_SIZE - num;
  61. memset(ctx->block_ipad + num, 0x36, fill);
  62. memset(ctx->block_opad + num, 0x5c, fill);
  63. }
  64. for (i = 0; i < (int) num; i++) {
  65. ctx->block_ipad[i] = key_used[i] ^ 0x36;
  66. ctx->block_opad[i] = key_used[i] ^ 0x5c;
  67. }
  68. sha224_init(&ctx->ctx_inside);
  69. sha224_update(&ctx->ctx_inside, ctx->block_ipad, SHA224_BLOCK_SIZE);
  70. sha224_init(&ctx->ctx_outside);
  71. sha224_update(&ctx->ctx_outside, ctx->block_opad,
  72. SHA224_BLOCK_SIZE);
  73. /* for hmac_reinit */
  74. memcpy(&ctx->ctx_inside_reinit, &ctx->ctx_inside,
  75. sizeof(sha224_ctx));
  76. memcpy(&ctx->ctx_outside_reinit, &ctx->ctx_outside,
  77. sizeof(sha224_ctx));
  78. }
  79. void hmac_sha224_reinit(hmac_sha224_ctx *ctx)
  80. {
  81. memcpy(&ctx->ctx_inside, &ctx->ctx_inside_reinit,
  82. sizeof(sha224_ctx));
  83. memcpy(&ctx->ctx_outside, &ctx->ctx_outside_reinit,
  84. sizeof(sha224_ctx));
  85. }
  86. void hmac_sha224_update(hmac_sha224_ctx *ctx, const unsigned char *message,
  87. unsigned int message_len)
  88. {
  89. sha224_update(&ctx->ctx_inside, message, message_len);
  90. }
  91. void hmac_sha224_final(hmac_sha224_ctx *ctx, unsigned char *mac,
  92. unsigned int mac_size)
  93. {
  94. unsigned char digest_inside[SHA224_DIGEST_SIZE];
  95. unsigned char mac_temp[SHA224_DIGEST_SIZE];
  96. sha224_final(&ctx->ctx_inside, digest_inside);
  97. sha224_update(&ctx->ctx_outside, digest_inside, SHA224_DIGEST_SIZE);
  98. sha224_final(&ctx->ctx_outside, mac_temp);
  99. memcpy(mac, mac_temp, mac_size);
  100. }
  101. void hmac_sha224(const unsigned char *key, unsigned int key_size,
  102. const unsigned char *message, unsigned int message_len,
  103. unsigned char *mac, unsigned mac_size)
  104. {
  105. hmac_sha224_ctx ctx;
  106. hmac_sha224_init(&ctx, key, key_size);
  107. hmac_sha224_update(&ctx, message, message_len);
  108. hmac_sha224_final(&ctx, mac, mac_size);
  109. }
  110. /* HMAC-SHA-256 functions */
  111. void hmac_sha256_init(hmac_sha256_ctx *ctx, const unsigned char *key,
  112. unsigned int key_size)
  113. {
  114. unsigned int fill;
  115. unsigned int num;
  116. const unsigned char *key_used;
  117. unsigned char key_temp[SHA256_DIGEST_SIZE];
  118. int i;
  119. if (key_size == SHA256_BLOCK_SIZE) {
  120. key_used = key;
  121. num = SHA256_BLOCK_SIZE;
  122. } else {
  123. if (key_size > SHA256_BLOCK_SIZE){
  124. num = SHA256_DIGEST_SIZE;
  125. sha256(key, key_size, key_temp);
  126. key_used = key_temp;
  127. } else { /* key_size > SHA256_BLOCK_SIZE */
  128. key_used = key;
  129. num = key_size;
  130. }
  131. fill = SHA256_BLOCK_SIZE - num;
  132. memset(ctx->block_ipad + num, 0x36, fill);
  133. memset(ctx->block_opad + num, 0x5c, fill);
  134. }
  135. for (i = 0; i < (int) num; i++) {
  136. ctx->block_ipad[i] = key_used[i] ^ 0x36;
  137. ctx->block_opad[i] = key_used[i] ^ 0x5c;
  138. }
  139. sha256_init(&ctx->ctx_inside);
  140. sha256_update(&ctx->ctx_inside, ctx->block_ipad, SHA256_BLOCK_SIZE);
  141. sha256_init(&ctx->ctx_outside);
  142. sha256_update(&ctx->ctx_outside, ctx->block_opad,
  143. SHA256_BLOCK_SIZE);
  144. /* for hmac_reinit */
  145. memcpy(&ctx->ctx_inside_reinit, &ctx->ctx_inside,
  146. sizeof(sha256_ctx));
  147. memcpy(&ctx->ctx_outside_reinit, &ctx->ctx_outside,
  148. sizeof(sha256_ctx));
  149. }
  150. void hmac_sha256_reinit(hmac_sha256_ctx *ctx)
  151. {
  152. memcpy(&ctx->ctx_inside, &ctx->ctx_inside_reinit,
  153. sizeof(sha256_ctx));
  154. memcpy(&ctx->ctx_outside, &ctx->ctx_outside_reinit,
  155. sizeof(sha256_ctx));
  156. }
  157. void hmac_sha256_update(hmac_sha256_ctx *ctx, const unsigned char *message,
  158. unsigned int message_len)
  159. {
  160. sha256_update(&ctx->ctx_inside, message, message_len);
  161. }
  162. void hmac_sha256_final(hmac_sha256_ctx *ctx, unsigned char *mac,
  163. unsigned int mac_size)
  164. {
  165. unsigned char digest_inside[SHA256_DIGEST_SIZE];
  166. unsigned char mac_temp[SHA256_DIGEST_SIZE];
  167. sha256_final(&ctx->ctx_inside, digest_inside);
  168. sha256_update(&ctx->ctx_outside, digest_inside, SHA256_DIGEST_SIZE);
  169. sha256_final(&ctx->ctx_outside, mac_temp);
  170. memcpy(mac, mac_temp, mac_size);
  171. }
  172. void hmac_sha256(const unsigned char *key, unsigned int key_size,
  173. const unsigned char *message, unsigned int message_len,
  174. unsigned char *mac, unsigned mac_size)
  175. {
  176. hmac_sha256_ctx ctx;
  177. hmac_sha256_init(&ctx, key, key_size);
  178. hmac_sha256_update(&ctx, message, message_len);
  179. hmac_sha256_final(&ctx, mac, mac_size);
  180. }
  181. /* HMAC-SHA-384 functions */
  182. void hmac_sha384_init(hmac_sha384_ctx *ctx, const unsigned char *key,
  183. unsigned int key_size)
  184. {
  185. unsigned int fill;
  186. unsigned int num;
  187. const unsigned char *key_used;
  188. unsigned char key_temp[SHA384_DIGEST_SIZE];
  189. int i;
  190. if (key_size == SHA384_BLOCK_SIZE) {
  191. key_used = key;
  192. num = SHA384_BLOCK_SIZE;
  193. } else {
  194. if (key_size > SHA384_BLOCK_SIZE){
  195. num = SHA384_DIGEST_SIZE;
  196. sha384(key, key_size, key_temp);
  197. key_used = key_temp;
  198. } else { /* key_size > SHA384_BLOCK_SIZE */
  199. key_used = key;
  200. num = key_size;
  201. }
  202. fill = SHA384_BLOCK_SIZE - num;
  203. memset(ctx->block_ipad + num, 0x36, fill);
  204. memset(ctx->block_opad + num, 0x5c, fill);
  205. }
  206. for (i = 0; i < (int) num; i++) {
  207. ctx->block_ipad[i] = key_used[i] ^ 0x36;
  208. ctx->block_opad[i] = key_used[i] ^ 0x5c;
  209. }
  210. sha384_init(&ctx->ctx_inside);
  211. sha384_update(&ctx->ctx_inside, ctx->block_ipad, SHA384_BLOCK_SIZE);
  212. sha384_init(&ctx->ctx_outside);
  213. sha384_update(&ctx->ctx_outside, ctx->block_opad,
  214. SHA384_BLOCK_SIZE);
  215. /* for hmac_reinit */
  216. memcpy(&ctx->ctx_inside_reinit, &ctx->ctx_inside,
  217. sizeof(sha384_ctx));
  218. memcpy(&ctx->ctx_outside_reinit, &ctx->ctx_outside,
  219. sizeof(sha384_ctx));
  220. }
  221. void hmac_sha384_reinit(hmac_sha384_ctx *ctx)
  222. {
  223. memcpy(&ctx->ctx_inside, &ctx->ctx_inside_reinit,
  224. sizeof(sha384_ctx));
  225. memcpy(&ctx->ctx_outside, &ctx->ctx_outside_reinit,
  226. sizeof(sha384_ctx));
  227. }
  228. void hmac_sha384_update(hmac_sha384_ctx *ctx, const unsigned char *message,
  229. unsigned int message_len)
  230. {
  231. sha384_update(&ctx->ctx_inside, message, message_len);
  232. }
  233. void hmac_sha384_final(hmac_sha384_ctx *ctx, unsigned char *mac,
  234. unsigned int mac_size)
  235. {
  236. unsigned char digest_inside[SHA384_DIGEST_SIZE];
  237. unsigned char mac_temp[SHA384_DIGEST_SIZE];
  238. sha384_final(&ctx->ctx_inside, digest_inside);
  239. sha384_update(&ctx->ctx_outside, digest_inside, SHA384_DIGEST_SIZE);
  240. sha384_final(&ctx->ctx_outside, mac_temp);
  241. memcpy(mac, mac_temp, mac_size);
  242. }
  243. void hmac_sha384(const unsigned char *key, unsigned int key_size,
  244. const unsigned char *message, unsigned int message_len,
  245. unsigned char *mac, unsigned mac_size)
  246. {
  247. hmac_sha384_ctx ctx;
  248. hmac_sha384_init(&ctx, key, key_size);
  249. hmac_sha384_update(&ctx, message, message_len);
  250. hmac_sha384_final(&ctx, mac, mac_size);
  251. }
  252. /* HMAC-SHA-512 functions */
  253. void hmac_sha512_init(hmac_sha512_ctx *ctx, const unsigned char *key,
  254. unsigned int key_size)
  255. {
  256. unsigned int fill;
  257. unsigned int num;
  258. const unsigned char *key_used;
  259. unsigned char key_temp[SHA512_DIGEST_SIZE];
  260. int i;
  261. if (key_size == SHA512_BLOCK_SIZE) {
  262. key_used = key;
  263. num = SHA512_BLOCK_SIZE;
  264. } else {
  265. if (key_size > SHA512_BLOCK_SIZE){
  266. num = SHA512_DIGEST_SIZE;
  267. sha512(key, key_size, key_temp);
  268. key_used = key_temp;
  269. } else { /* key_size > SHA512_BLOCK_SIZE */
  270. key_used = key;
  271. num = key_size;
  272. }
  273. fill = SHA512_BLOCK_SIZE - num;
  274. memset(ctx->block_ipad + num, 0x36, fill);
  275. memset(ctx->block_opad + num, 0x5c, fill);
  276. }
  277. for (i = 0; i < (int) num; i++) {
  278. ctx->block_ipad[i] = key_used[i] ^ 0x36;
  279. ctx->block_opad[i] = key_used[i] ^ 0x5c;
  280. }
  281. sha512_init(&ctx->ctx_inside);
  282. sha512_update(&ctx->ctx_inside, ctx->block_ipad, SHA512_BLOCK_SIZE);
  283. sha512_init(&ctx->ctx_outside);
  284. sha512_update(&ctx->ctx_outside, ctx->block_opad,
  285. SHA512_BLOCK_SIZE);
  286. /* for hmac_reinit */
  287. memcpy(&ctx->ctx_inside_reinit, &ctx->ctx_inside,
  288. sizeof(sha512_ctx));
  289. memcpy(&ctx->ctx_outside_reinit, &ctx->ctx_outside,
  290. sizeof(sha512_ctx));
  291. }
  292. void hmac_sha512_reinit(hmac_sha512_ctx *ctx)
  293. {
  294. memcpy(&ctx->ctx_inside, &ctx->ctx_inside_reinit,
  295. sizeof(sha512_ctx));
  296. memcpy(&ctx->ctx_outside, &ctx->ctx_outside_reinit,
  297. sizeof(sha512_ctx));
  298. }
  299. void hmac_sha512_update(hmac_sha512_ctx *ctx, const unsigned char *message,
  300. unsigned int message_len)
  301. {
  302. sha512_update(&ctx->ctx_inside, message, message_len);
  303. }
  304. void hmac_sha512_final(hmac_sha512_ctx *ctx, unsigned char *mac,
  305. unsigned int mac_size)
  306. {
  307. unsigned char digest_inside[SHA512_DIGEST_SIZE];
  308. unsigned char mac_temp[SHA512_DIGEST_SIZE];
  309. sha512_final(&ctx->ctx_inside, digest_inside);
  310. sha512_update(&ctx->ctx_outside, digest_inside, SHA512_DIGEST_SIZE);
  311. sha512_final(&ctx->ctx_outside, mac_temp);
  312. memcpy(mac, mac_temp, mac_size);
  313. }
  314. void hmac_sha512(const unsigned char *key, unsigned int key_size,
  315. const unsigned char *message, unsigned int message_len,
  316. unsigned char *mac, unsigned mac_size)
  317. {
  318. hmac_sha512_ctx ctx;
  319. hmac_sha512_init(&ctx, key, key_size);
  320. hmac_sha512_update(&ctx, message, message_len);
  321. hmac_sha512_final(&ctx, mac, mac_size);
  322. }
  323. #ifdef TEST_VECTORS
  324. /* IETF Validation tests */
  325. #include <stdio.h>
  326. #include <stdlib.h>
  327. void test(const char *vector, unsigned char *digest,
  328. unsigned int digest_size)
  329. {
  330. char output[2 * SHA512_DIGEST_SIZE + 1];
  331. int i;
  332. output[2 * digest_size] = '\0';
  333. for (i = 0; i < (int) digest_size ; i++) {
  334. sprintf(output + 2*i, "%02x", digest[i]);
  335. }
  336. printf("H: %s\n", output);
  337. if (strcmp(vector, output)) {
  338. fprintf(stderr, "Test failed.\n");
  339. exit(1);
  340. }
  341. }
  342. int main(void)
  343. {
  344. static const char *vectors[] =
  345. {
  346. /* HMAC-SHA-224 */
  347. "896fb1128abbdf196832107cd49df33f47b4b1169912ba4f53684b22",
  348. "a30e01098bc6dbbf45690f3a7e9e6d0f8bbea2a39e6148008fd05e44",
  349. "7fb3cb3588c6c1f6ffa9694d7d6ad2649365b0c1f65d69d1ec8333ea",
  350. "6c11506874013cac6a2abc1bb382627cec6a90d86efc012de7afec5a",
  351. "0e2aea68a90c8d37c988bcdb9fca6fa8",
  352. "95e9a0db962095adaebe9b2d6f0dbce2d499f112f2d2b7273fa6870e",
  353. "3a854166ac5d9f023f54d517d0b39dbd946770db9c2b95c9f6f565d1",
  354. /* HMAC-SHA-256 */
  355. "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7",
  356. "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843",
  357. "773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe",
  358. "82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b",
  359. "a3b6167473100ee06e0c796c2955552b",
  360. "60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54",
  361. "9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2",
  362. /* HMAC-SHA-384 */
  363. "afd03944d84895626b0825f4ab46907f15f9dadbe4101ec682aa034c7cebc59c"
  364. "faea9ea9076ede7f4af152e8b2fa9cb6",
  365. "af45d2e376484031617f78d2b58a6b1b9c7ef464f5a01b47e42ec3736322445e"
  366. "8e2240ca5e69e2c78b3239ecfab21649",
  367. "88062608d3e6ad8a0aa2ace014c8a86f0aa635d947ac9febe83ef4e55966144b"
  368. "2a5ab39dc13814b94e3ab6e101a34f27",
  369. "3e8a69b7783c25851933ab6290af6ca77a9981480850009cc5577c6e1f573b4e"
  370. "6801dd23c4a7d679ccf8a386c674cffb",
  371. "3abf34c3503b2a23a46efc619baef897",
  372. "4ece084485813e9088d2c63a041bc5b44f9ef1012a2b588f3cd11f05033ac4c6"
  373. "0c2ef6ab4030fe8296248df163f44952",
  374. "6617178e941f020d351e2f254e8fd32c602420feb0b8fb9adccebb82461e99c5"
  375. "a678cc31e799176d3860e6110c46523e",
  376. /* HMAC-SHA-512 */
  377. "87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cde"
  378. "daa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854",
  379. "164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea250554"
  380. "9758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737",
  381. "fa73b0089d56a284efb0f0756c890be9b1b5dbdd8ee81a3655f83e33b2279d39"
  382. "bf3e848279a722c806b485a47e67c807b946a337bee8942674278859e13292fb",
  383. "b0ba465637458c6990e5a8c5f61d4af7e576d97ff94b872de76f8050361ee3db"
  384. "a91ca5c11aa25eb4d679275cc5788063a5f19741120c4f2de2adebeb10a298dd",
  385. "415fad6271580a531d4179bc891d87a6",
  386. "80b24263c7c1a3ebb71493c1dd7be8b49b46d1f41b4aeec1121b013783f8f352"
  387. "6b56d037e05f2598bd0fd2215d6a1e5295e64f73f63f0aec8b915a985d786598",
  388. "e37b6a775dc87dbaa4dfa9f96e5e3ffddebd71f8867289865df5a32d20cdc944"
  389. "b6022cac3c4982b10d5eeb55c3e4de15134676fb6de0446065c97440fa8c6a58"
  390. };
  391. static char *messages[] =
  392. {
  393. "Hi There",
  394. "what do ya want for nothing?",
  395. NULL,
  396. NULL,
  397. "Test With Truncation",
  398. "Test Using Larger Than Block-Size Key - Hash Key First",
  399. "This is a test using a larger than block-size key "
  400. "and a larger than block-size data. The key needs"
  401. " to be hashed before being used by the HMAC algorithm."
  402. };
  403. unsigned char mac[SHA512_DIGEST_SIZE];
  404. unsigned char *keys[7];
  405. unsigned int keys_len[7] = {20, 4, 20, 25, 20, 131, 131};
  406. unsigned int messages2and3_len = 50;
  407. unsigned int mac_224_size, mac_256_size, mac_384_size, mac_512_size;
  408. int i;
  409. for (i = 0; i < 7; i++) {
  410. keys[i] = malloc(keys_len[i]);
  411. if (keys[i] == NULL) {
  412. fprintf(stderr, "Can't allocate memory\n");
  413. return 1;
  414. }
  415. }
  416. memset(keys[0], 0x0b, keys_len[0]);
  417. strcpy((char *) keys[1], "Jefe");
  418. memset(keys[2], 0xaa, keys_len[2]);
  419. for (i = 0; i < (int) keys_len[3]; i++)
  420. keys[3][i] = (unsigned char) i + 1;
  421. memset(keys[4], 0x0c, keys_len[4]);
  422. memset(keys[5], 0xaa, keys_len[5]);
  423. memset(keys[6], 0xaa, keys_len[6]);
  424. messages[2] = malloc(messages2and3_len + 1);
  425. messages[3] = malloc(messages2and3_len + 1);
  426. if (messages[2] == NULL || messages[3] == NULL) {
  427. fprintf(stderr, "Can't allocate memory\n");
  428. return 1;
  429. }
  430. messages[2][messages2and3_len] = '\0';
  431. messages[3][messages2and3_len] = '\0';
  432. memset(messages[2], 0xdd, messages2and3_len);
  433. memset(messages[3], 0xcd, messages2and3_len);
  434. printf("HMAC-SHA-2 IETF Validation tests\n\n");
  435. for (i = 0; i < 7; i++) {
  436. if (i != 4) {
  437. mac_224_size = SHA224_DIGEST_SIZE;
  438. mac_256_size = SHA256_DIGEST_SIZE;
  439. mac_384_size = SHA384_DIGEST_SIZE;
  440. mac_512_size = SHA512_DIGEST_SIZE;
  441. } else {
  442. mac_224_size = 128 / 8; mac_256_size = 128 / 8;
  443. mac_384_size = 128 / 8; mac_512_size = 128 / 8;
  444. }
  445. printf("Test %d:\n", i + 1);
  446. hmac_sha224(keys[i], keys_len[i], (unsigned char *) messages[i],
  447. strlen(messages[i]), mac, mac_224_size);
  448. test(vectors[i], mac, mac_224_size);
  449. hmac_sha256(keys[i], keys_len[i], (unsigned char *) messages[i],
  450. strlen(messages[i]), mac, mac_256_size);
  451. test(vectors[7 + i], mac, mac_256_size);
  452. hmac_sha384(keys[i], keys_len[i], (unsigned char *) messages[i],
  453. strlen(messages[i]), mac, mac_384_size);
  454. test(vectors[14 + i], mac, mac_384_size);
  455. hmac_sha512(keys[i], keys_len[i], (unsigned char *) messages[i],
  456. strlen(messages[i]), mac, mac_512_size);
  457. test(vectors[21 + i], mac, mac_512_size);
  458. }
  459. printf("All tests passed.\n");
  460. return 0;
  461. }
  462. #endif /* TEST_VECTORS */