hmactest.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the License); you may
  5. * not use this file except in compliance with the License.
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <gmssl/hex.h>
  13. #include <gmssl/hmac.h>
  14. // FIXME: md5, sha1, sm3 test vectors
  15. struct {
  16. char *key;
  17. char *data;
  18. char *hmac_sha224;
  19. char *hmac_sha256;
  20. char *hmac_sha384;
  21. char *hmac_sha512;
  22. } hmac_tests[] = {
  23. // rfc 4231 test vectors
  24. {
  25. "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"
  26. "0b0b0b0b",
  27. "4869205468657265",
  28. "896fb1128abbdf196832107cd49df33f"
  29. "47b4b1169912ba4f53684b22",
  30. "b0344c61d8db38535ca8afceaf0bf12b"
  31. "881dc200c9833da726e9376c2e32cff7",
  32. "afd03944d84895626b0825f4ab46907f"
  33. "15f9dadbe4101ec682aa034c7cebc59c"
  34. "faea9ea9076ede7f4af152e8b2fa9cb6",
  35. "87aa7cdea5ef619d4ff0b4241a1d6cb0"
  36. "2379f4e2ce4ec2787ad0b30545e17cde"
  37. "daa833b7d6b8a702038b274eaea3f4e4"
  38. "be9d914eeb61f1702e696c203a126854",
  39. },
  40. {
  41. "4a656665",
  42. "7768617420646f2079612077616e7420"
  43. "666f72206e6f7468696e673f",
  44. "a30e01098bc6dbbf45690f3a7e9e6d0f"
  45. "8bbea2a39e6148008fd05e44",
  46. "5bdcc146bf60754e6a042426089575c7"
  47. "5a003f089d2739839dec58b964ec3843",
  48. "af45d2e376484031617f78d2b58a6b1b"
  49. "9c7ef464f5a01b47e42ec3736322445e"
  50. "8e2240ca5e69e2c78b3239ecfab21649",
  51. "164b7a7bfcf819e2e395fbe73b56e0a3"
  52. "87bd64222e831fd610270cd7ea250554"
  53. "9758bf75c05a994a6d034f65f8f0e6fd"
  54. "caeab1a34d4a6b4b636e070a38bce737",
  55. },
  56. {
  57. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  58. "aaaaaaaa",
  59. "dddddddddddddddddddddddddddddddd"
  60. "dddddddddddddddddddddddddddddddd"
  61. "dddddddddddddddddddddddddddddddd"
  62. "dddd",
  63. "7fb3cb3588c6c1f6ffa9694d7d6ad264"
  64. "9365b0c1f65d69d1ec8333ea",
  65. "773ea91e36800e46854db8ebd09181a7"
  66. "2959098b3ef8c122d9635514ced565fe",
  67. "88062608d3e6ad8a0aa2ace014c8a86f"
  68. "0aa635d947ac9febe83ef4e55966144b"
  69. "2a5ab39dc13814b94e3ab6e101a34f27",
  70. "fa73b0089d56a284efb0f0756c890be9"
  71. "b1b5dbdd8ee81a3655f83e33b2279d39"
  72. "bf3e848279a722c806b485a47e67c807"
  73. "b946a337bee8942674278859e13292fb",
  74. },
  75. };
  76. int test_hmac(const DIGEST *digest, const char *key_hex, const char *data_hex, const char *hmac_hex)
  77. {
  78. HMAC_CTX ctx;
  79. uint8_t *key = (uint8_t *)malloc(strlen(key_hex)/2);
  80. uint8_t *data = (uint8_t *)malloc(strlen(data_hex)/2);
  81. uint8_t *hmac = (uint8_t *)malloc(strlen(hmac_hex) / 2);
  82. size_t keylen, datalen, hmaclen;
  83. uint8_t buf[64];
  84. size_t buflen;
  85. hex_to_bytes(key_hex, strlen(key_hex), key, &keylen);
  86. hex_to_bytes(data_hex, strlen(data_hex), data, &datalen);
  87. hex_to_bytes(hmac_hex, strlen(hmac_hex), hmac, &hmaclen);
  88. hmac_init(&ctx, digest, key, keylen);
  89. hmac_update(&ctx, data, datalen);
  90. hmac_finish(&ctx, buf, &buflen);
  91. if (buflen != hmaclen || memcmp(buf, hmac, hmaclen) != 0) {
  92. printf("failed\n");
  93. return 0;
  94. }
  95. printf("ok\n");
  96. if (key) free(key);
  97. if (data) free(data);
  98. if (hmac) free(hmac);
  99. return 1;
  100. }
  101. int main(void)
  102. {
  103. int i;
  104. for (i = 0; i < sizeof(hmac_tests)/sizeof(hmac_tests[0]); i++) {
  105. test_hmac(DIGEST_sha224(), hmac_tests[i].key, hmac_tests[i].data, hmac_tests[i].hmac_sha224);
  106. test_hmac(DIGEST_sha256(), hmac_tests[i].key, hmac_tests[i].data, hmac_tests[i].hmac_sha256);
  107. test_hmac(DIGEST_sha384(), hmac_tests[i].key, hmac_tests[i].data, hmac_tests[i].hmac_sha384);
  108. test_hmac(DIGEST_sha512(), hmac_tests[i].key, hmac_tests[i].data, hmac_tests[i].hmac_sha512);
  109. };
  110. return 0;
  111. };