hmac.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (C) 2012 Martin Storsjo
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <string.h>
  21. #include "hmac.h"
  22. #include "md5.h"
  23. #include "sha.h"
  24. #include "mem.h"
  25. #define MAX_HASHLEN 20
  26. #define MAX_BLOCKLEN 64
  27. struct AVHMAC {
  28. void *hash;
  29. int blocklen, hashlen;
  30. void (*final)(void*, uint8_t*);
  31. void (*update)(void*, const uint8_t*, int len);
  32. void (*init)(void*);
  33. uint8_t key[MAX_BLOCKLEN];
  34. int keylen;
  35. };
  36. static void sha1_init(void *ctx)
  37. {
  38. av_sha_init(ctx, 160);
  39. }
  40. AVHMAC *av_hmac_alloc(enum AVHMACType type)
  41. {
  42. AVHMAC *c = av_mallocz(sizeof(*c));
  43. if (!c)
  44. return NULL;
  45. switch (type) {
  46. case AV_HMAC_MD5:
  47. c->blocklen = 64;
  48. c->hashlen = 16;
  49. c->init = av_md5_init;
  50. c->update = av_md5_update;
  51. c->final = av_md5_final;
  52. c->hash = av_md5_alloc();
  53. break;
  54. case AV_HMAC_SHA1:
  55. c->blocklen = 64;
  56. c->hashlen = 20;
  57. c->init = sha1_init;
  58. c->update = av_sha_update;
  59. c->final = av_sha_final;
  60. c->hash = av_sha_alloc();
  61. break;
  62. default:
  63. av_free(c);
  64. return NULL;
  65. }
  66. if (!c->hash) {
  67. av_free(c);
  68. return NULL;
  69. }
  70. return c;
  71. }
  72. void av_hmac_free(AVHMAC *c)
  73. {
  74. if (!c)
  75. return;
  76. av_free(c->hash);
  77. av_free(c);
  78. }
  79. void av_hmac_init(AVHMAC *c, const uint8_t *key, unsigned int keylen)
  80. {
  81. int i;
  82. uint8_t block[MAX_BLOCKLEN];
  83. if (keylen > c->blocklen) {
  84. c->init(c->hash);
  85. c->update(c->hash, key, keylen);
  86. c->final(c->hash, c->key);
  87. c->keylen = c->hashlen;
  88. } else {
  89. memcpy(c->key, key, keylen);
  90. c->keylen = keylen;
  91. }
  92. c->init(c->hash);
  93. for (i = 0; i < c->keylen; i++)
  94. block[i] = c->key[i] ^ 0x36;
  95. for (i = c->keylen; i < c->blocklen; i++)
  96. block[i] = 0x36;
  97. c->update(c->hash, block, c->blocklen);
  98. }
  99. void av_hmac_update(AVHMAC *c, const uint8_t *data, unsigned int len)
  100. {
  101. c->update(c->hash, data, len);
  102. }
  103. int av_hmac_final(AVHMAC *c, uint8_t *out, unsigned int outlen)
  104. {
  105. uint8_t block[MAX_BLOCKLEN];
  106. int i;
  107. if (outlen < c->hashlen)
  108. return AVERROR(EINVAL);
  109. c->final(c->hash, out);
  110. c->init(c->hash);
  111. for (i = 0; i < c->keylen; i++)
  112. block[i] = c->key[i] ^ 0x5C;
  113. for (i = c->keylen; i < c->blocklen; i++)
  114. block[i] = 0x5C;
  115. c->update(c->hash, block, c->blocklen);
  116. c->update(c->hash, out, c->hashlen);
  117. c->final(c->hash, out);
  118. return c->hashlen;
  119. }
  120. int av_hmac_calc(AVHMAC *c, const uint8_t *data, unsigned int len,
  121. const uint8_t *key, unsigned int keylen,
  122. uint8_t *out, unsigned int outlen)
  123. {
  124. av_hmac_init(c, key, keylen);
  125. av_hmac_update(c, data, len);
  126. return av_hmac_final(c, out, outlen);
  127. }
  128. #ifdef TEST
  129. #include <stdio.h>
  130. static void test(AVHMAC *hmac, const uint8_t *key, int keylen,
  131. const uint8_t *data, int datalen)
  132. {
  133. uint8_t buf[MAX_HASHLEN];
  134. int out, i;
  135. // Some of the test vectors are strings, where sizeof() includes the
  136. // trailing null byte - remove that.
  137. if (!key[keylen - 1])
  138. keylen--;
  139. if (!data[datalen - 1])
  140. datalen--;
  141. out = av_hmac_calc(hmac, data, datalen, key, keylen, buf, sizeof(buf));
  142. for (i = 0; i < out; i++)
  143. printf("%02x", buf[i]);
  144. printf("\n");
  145. }
  146. int main(void)
  147. {
  148. uint8_t key1[16], key3[16], data3[50], key4[63], key5[64], key6[65];
  149. const uint8_t key2[] = "Jefe";
  150. const uint8_t data1[] = "Hi There";
  151. const uint8_t data2[] = "what do ya want for nothing?";
  152. AVHMAC *hmac = av_hmac_alloc(AV_HMAC_MD5);
  153. if (!hmac)
  154. return 1;
  155. memset(key1, 0x0b, sizeof(key1));
  156. memset(key3, 0xaa, sizeof(key3));
  157. memset(key4, 0x44, sizeof(key4));
  158. memset(key5, 0x55, sizeof(key5));
  159. memset(key6, 0x66, sizeof(key6));
  160. memset(data3, 0xdd, sizeof(data3));
  161. // RFC 2104 test vectors
  162. test(hmac, key1, sizeof(key1), data1, sizeof(data1));
  163. test(hmac, key2, sizeof(key2), data2, sizeof(data2));
  164. test(hmac, key3, sizeof(key3), data3, sizeof(data3));
  165. // Additional tests, to test cases where the key is too long
  166. test(hmac, key4, sizeof(key4), data1, sizeof(data1));
  167. test(hmac, key5, sizeof(key5), data2, sizeof(data2));
  168. test(hmac, key6, sizeof(key6), data3, sizeof(data3));
  169. av_hmac_free(hmac);
  170. return 0;
  171. }
  172. #endif /* TEST */