You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
5.1KB

  1. /*
  2. * Copyright (C) 2012 Martin Storsjo
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; 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 "attributes.h"
  22. #include "hmac.h"
  23. #include "md5.h"
  24. #include "sha.h"
  25. #include "mem.h"
  26. #define MAX_HASHLEN 20
  27. #define MAX_BLOCKLEN 64
  28. struct AVHMAC {
  29. void *hash;
  30. int blocklen, hashlen;
  31. void (*final)(void*, uint8_t*);
  32. void (*update)(void*, const uint8_t*, int len);
  33. void (*init)(void*);
  34. uint8_t key[MAX_BLOCKLEN];
  35. int keylen;
  36. };
  37. static av_cold void sha1_init(void *ctx)
  38. {
  39. av_sha_init(ctx, 160);
  40. }
  41. AVHMAC *av_hmac_alloc(enum AVHMACType type)
  42. {
  43. AVHMAC *c = av_mallocz(sizeof(*c));
  44. if (!c)
  45. return NULL;
  46. switch (type) {
  47. case AV_HMAC_MD5:
  48. c->blocklen = 64;
  49. c->hashlen = 16;
  50. c->init = av_md5_init;
  51. c->update = av_md5_update;
  52. c->final = av_md5_final;
  53. c->hash = av_md5_alloc();
  54. break;
  55. case AV_HMAC_SHA1:
  56. c->blocklen = 64;
  57. c->hashlen = 20;
  58. c->init = sha1_init;
  59. c->update = av_sha_update;
  60. c->final = av_sha_final;
  61. c->hash = av_sha_alloc();
  62. break;
  63. default:
  64. av_free(c);
  65. return NULL;
  66. }
  67. if (!c->hash) {
  68. av_free(c);
  69. return NULL;
  70. }
  71. return c;
  72. }
  73. void av_hmac_free(AVHMAC *c)
  74. {
  75. if (!c)
  76. return;
  77. av_free(c->hash);
  78. av_free(c);
  79. }
  80. void av_hmac_init(AVHMAC *c, const uint8_t *key, unsigned int keylen)
  81. {
  82. int i;
  83. uint8_t block[MAX_BLOCKLEN];
  84. if (keylen > c->blocklen) {
  85. c->init(c->hash);
  86. c->update(c->hash, key, keylen);
  87. c->final(c->hash, c->key);
  88. c->keylen = c->hashlen;
  89. } else {
  90. memcpy(c->key, key, keylen);
  91. c->keylen = keylen;
  92. }
  93. c->init(c->hash);
  94. for (i = 0; i < c->keylen; i++)
  95. block[i] = c->key[i] ^ 0x36;
  96. for (i = c->keylen; i < c->blocklen; i++)
  97. block[i] = 0x36;
  98. c->update(c->hash, block, c->blocklen);
  99. }
  100. void av_hmac_update(AVHMAC *c, const uint8_t *data, unsigned int len)
  101. {
  102. c->update(c->hash, data, len);
  103. }
  104. int av_hmac_final(AVHMAC *c, uint8_t *out, unsigned int outlen)
  105. {
  106. uint8_t block[MAX_BLOCKLEN];
  107. int i;
  108. if (outlen < c->hashlen)
  109. return AVERROR(EINVAL);
  110. c->final(c->hash, out);
  111. c->init(c->hash);
  112. for (i = 0; i < c->keylen; i++)
  113. block[i] = c->key[i] ^ 0x5C;
  114. for (i = c->keylen; i < c->blocklen; i++)
  115. block[i] = 0x5C;
  116. c->update(c->hash, block, c->blocklen);
  117. c->update(c->hash, out, c->hashlen);
  118. c->final(c->hash, out);
  119. return c->hashlen;
  120. }
  121. int av_hmac_calc(AVHMAC *c, const uint8_t *data, unsigned int len,
  122. const uint8_t *key, unsigned int keylen,
  123. uint8_t *out, unsigned int outlen)
  124. {
  125. av_hmac_init(c, key, keylen);
  126. av_hmac_update(c, data, len);
  127. return av_hmac_final(c, out, outlen);
  128. }
  129. #ifdef TEST
  130. #include <stdio.h>
  131. static void test(AVHMAC *hmac, const uint8_t *key, int keylen,
  132. const uint8_t *data, int datalen)
  133. {
  134. uint8_t buf[MAX_HASHLEN];
  135. int out, i;
  136. // Some of the test vectors are strings, where sizeof() includes the
  137. // trailing null byte - remove that.
  138. if (!key[keylen - 1])
  139. keylen--;
  140. if (!data[datalen - 1])
  141. datalen--;
  142. out = av_hmac_calc(hmac, data, datalen, key, keylen, buf, sizeof(buf));
  143. for (i = 0; i < out; i++)
  144. printf("%02x", buf[i]);
  145. printf("\n");
  146. }
  147. int main(void)
  148. {
  149. uint8_t key1[16], key3[16], data3[50], key4[63], key5[64], key6[65];
  150. const uint8_t key2[] = "Jefe";
  151. const uint8_t data1[] = "Hi There";
  152. const uint8_t data2[] = "what do ya want for nothing?";
  153. AVHMAC *hmac = av_hmac_alloc(AV_HMAC_MD5);
  154. if (!hmac)
  155. return 1;
  156. memset(key1, 0x0b, sizeof(key1));
  157. memset(key3, 0xaa, sizeof(key3));
  158. memset(key4, 0x44, sizeof(key4));
  159. memset(key5, 0x55, sizeof(key5));
  160. memset(key6, 0x66, sizeof(key6));
  161. memset(data3, 0xdd, sizeof(data3));
  162. // RFC 2104 test vectors
  163. test(hmac, key1, sizeof(key1), data1, sizeof(data1));
  164. test(hmac, key2, sizeof(key2), data2, sizeof(data2));
  165. test(hmac, key3, sizeof(key3), data3, sizeof(data3));
  166. // Additional tests, to test cases where the key is too long
  167. test(hmac, key4, sizeof(key4), data1, sizeof(data1));
  168. test(hmac, key5, sizeof(key5), data2, sizeof(data2));
  169. test(hmac, key6, sizeof(key6), data3, sizeof(data3));
  170. av_hmac_free(hmac);
  171. return 0;
  172. }
  173. #endif /* TEST */