hmac.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "libavutil/hmac.c"
  19. #include <stdio.h>
  20. #include <string.h>
  21. static void test(AVHMAC *hmac, const uint8_t *key, int keylen,
  22. const uint8_t *data, int datalen)
  23. {
  24. uint8_t buf[MAX_HASHLEN];
  25. int out, i;
  26. // Some of the test vectors are strings, where sizeof() includes the
  27. // trailing null byte - remove that.
  28. if (!key[keylen - 1])
  29. keylen--;
  30. if (!data[datalen - 1])
  31. datalen--;
  32. out = av_hmac_calc(hmac, data, datalen, key, keylen, buf, sizeof(buf));
  33. for (i = 0; i < out; i++)
  34. printf("%02x", buf[i]);
  35. printf("\n");
  36. }
  37. int main(void)
  38. {
  39. uint8_t key1[20], key3[131], data3[50];
  40. AVHMAC *hmac;
  41. enum AVHMACType i;
  42. static const uint8_t key2[] = "Jefe";
  43. static const uint8_t data1[] = "Hi There";
  44. static const uint8_t data2[] = "what do ya want for nothing?";
  45. static const uint8_t data4[] = "Test Using Larger Than Block-Size Key - Hash Key First";
  46. static const uint8_t data5[] = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data";
  47. static const uint8_t data6[] = "This is a test using a larger than block-size key and a larger "
  48. "than block-size data. The key needs to be hashed before being used"
  49. " by the HMAC algorithm.";
  50. memset(key1, 0x0b, sizeof(key1));
  51. memset(key3, 0xaa, sizeof(key3));
  52. memset(data3, 0xdd, sizeof(data3));
  53. /* MD5, SHA-1 */
  54. for (i = AV_HMAC_MD5; i <= AV_HMAC_SHA1; i++) {
  55. hmac = av_hmac_alloc(i);
  56. if (!hmac)
  57. return 1;
  58. // RFC 2202 test vectors
  59. test(hmac, key1, hmac->hashlen, data1, sizeof(data1));
  60. test(hmac, key2, sizeof(key2), data2, sizeof(data2));
  61. test(hmac, key3, hmac->hashlen, data3, sizeof(data3));
  62. test(hmac, key3, 80, data4, sizeof(data4));
  63. test(hmac, key3, 80, data5, sizeof(data5));
  64. av_hmac_free(hmac);
  65. }
  66. /* SHA-2 */
  67. for (i = AV_HMAC_SHA224; i <= AV_HMAC_SHA512; i++) {
  68. hmac = av_hmac_alloc(i);
  69. if (!hmac)
  70. return 1;
  71. // RFC 4231 test vectors
  72. test(hmac, key1, sizeof(key1), data1, sizeof(data1));
  73. test(hmac, key2, sizeof(key2), data2, sizeof(data2));
  74. test(hmac, key3, 20, data3, sizeof(data3));
  75. test(hmac, key3, sizeof(key3), data4, sizeof(data4));
  76. test(hmac, key3, sizeof(key3), data6, sizeof(data6));
  77. av_hmac_free(hmac);
  78. }
  79. return 0;
  80. }