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.

235 lines
6.7KB

  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 32
  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. #define DEFINE_SHA(bits) \
  38. static av_cold void sha ## bits ##_init(void *ctx) \
  39. { \
  40. av_sha_init(ctx, bits); \
  41. }
  42. DEFINE_SHA(160)
  43. DEFINE_SHA(224)
  44. DEFINE_SHA(256)
  45. AVHMAC *av_hmac_alloc(enum AVHMACType type)
  46. {
  47. AVHMAC *c = av_mallocz(sizeof(*c));
  48. if (!c)
  49. return NULL;
  50. switch (type) {
  51. case AV_HMAC_MD5:
  52. c->blocklen = 64;
  53. c->hashlen = 16;
  54. c->init = av_md5_init;
  55. c->update = av_md5_update;
  56. c->final = av_md5_final;
  57. c->hash = av_md5_alloc();
  58. break;
  59. case AV_HMAC_SHA1:
  60. c->blocklen = 64;
  61. c->hashlen = 20;
  62. c->init = sha160_init;
  63. c->update = av_sha_update;
  64. c->final = av_sha_final;
  65. c->hash = av_sha_alloc();
  66. break;
  67. case AV_HMAC_SHA224:
  68. c->blocklen = 64;
  69. c->hashlen = 28;
  70. c->init = sha224_init;
  71. c->update = av_sha_update;
  72. c->final = av_sha_final;
  73. c->hash = av_sha_alloc();
  74. break;
  75. case AV_HMAC_SHA256:
  76. c->blocklen = 64;
  77. c->hashlen = 32;
  78. c->init = sha256_init;
  79. c->update = av_sha_update;
  80. c->final = av_sha_final;
  81. c->hash = av_sha_alloc();
  82. break;
  83. default:
  84. av_free(c);
  85. return NULL;
  86. }
  87. if (!c->hash) {
  88. av_free(c);
  89. return NULL;
  90. }
  91. return c;
  92. }
  93. void av_hmac_free(AVHMAC *c)
  94. {
  95. if (!c)
  96. return;
  97. av_free(c->hash);
  98. av_free(c);
  99. }
  100. void av_hmac_init(AVHMAC *c, const uint8_t *key, unsigned int keylen)
  101. {
  102. int i;
  103. uint8_t block[MAX_BLOCKLEN];
  104. if (keylen > c->blocklen) {
  105. c->init(c->hash);
  106. c->update(c->hash, key, keylen);
  107. c->final(c->hash, c->key);
  108. c->keylen = c->hashlen;
  109. } else {
  110. memcpy(c->key, key, keylen);
  111. c->keylen = keylen;
  112. }
  113. c->init(c->hash);
  114. for (i = 0; i < c->keylen; i++)
  115. block[i] = c->key[i] ^ 0x36;
  116. for (i = c->keylen; i < c->blocklen; i++)
  117. block[i] = 0x36;
  118. c->update(c->hash, block, c->blocklen);
  119. }
  120. void av_hmac_update(AVHMAC *c, const uint8_t *data, unsigned int len)
  121. {
  122. c->update(c->hash, data, len);
  123. }
  124. int av_hmac_final(AVHMAC *c, uint8_t *out, unsigned int outlen)
  125. {
  126. uint8_t block[MAX_BLOCKLEN];
  127. int i;
  128. if (outlen < c->hashlen)
  129. return AVERROR(EINVAL);
  130. c->final(c->hash, out);
  131. c->init(c->hash);
  132. for (i = 0; i < c->keylen; i++)
  133. block[i] = c->key[i] ^ 0x5C;
  134. for (i = c->keylen; i < c->blocklen; i++)
  135. block[i] = 0x5C;
  136. c->update(c->hash, block, c->blocklen);
  137. c->update(c->hash, out, c->hashlen);
  138. c->final(c->hash, out);
  139. return c->hashlen;
  140. }
  141. int av_hmac_calc(AVHMAC *c, const uint8_t *data, unsigned int len,
  142. const uint8_t *key, unsigned int keylen,
  143. uint8_t *out, unsigned int outlen)
  144. {
  145. av_hmac_init(c, key, keylen);
  146. av_hmac_update(c, data, len);
  147. return av_hmac_final(c, out, outlen);
  148. }
  149. #ifdef TEST
  150. #include <stdio.h>
  151. static void test(AVHMAC *hmac, const uint8_t *key, int keylen,
  152. const uint8_t *data, int datalen)
  153. {
  154. uint8_t buf[MAX_HASHLEN];
  155. int out, i;
  156. // Some of the test vectors are strings, where sizeof() includes the
  157. // trailing null byte - remove that.
  158. if (!key[keylen - 1])
  159. keylen--;
  160. if (!data[datalen - 1])
  161. datalen--;
  162. out = av_hmac_calc(hmac, data, datalen, key, keylen, buf, sizeof(buf));
  163. for (i = 0; i < out; i++)
  164. printf("%02x", buf[i]);
  165. printf("\n");
  166. }
  167. int main(void)
  168. {
  169. uint8_t key1[20], key3[131], data3[50];
  170. enum AVHMACType i = AV_HMAC_SHA224;
  171. const uint8_t key2[] = "Jefe";
  172. const uint8_t data1[] = "Hi There";
  173. const uint8_t data2[] = "what do ya want for nothing?";
  174. const uint8_t data4[] = "Test Using Larger Than Block-Size Key - Hash Key First";
  175. const uint8_t data5[] = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data";
  176. const uint8_t data6[] = "This is a test using a larger than block-size key and a larger "
  177. "than block-size data. The key needs to be hashed before being used"
  178. " by the HMAC algorithm.";
  179. AVHMAC *hmac = av_hmac_alloc(AV_HMAC_MD5);
  180. if (!hmac)
  181. return 1;
  182. memset(key1, 0x0b, sizeof(key1));
  183. memset(key3, 0xaa, sizeof(key3));
  184. memset(data3, 0xdd, sizeof(data3));
  185. // RFC 2202 test vectors
  186. test(hmac, key1, 16, data1, sizeof(data1));
  187. test(hmac, key2, sizeof(key2), data2, sizeof(data2));
  188. test(hmac, key3, 16, data3, sizeof(data3));
  189. test(hmac, key3, 80, data4, sizeof(data4));
  190. test(hmac, key3, 80, data5, sizeof(data5));
  191. av_hmac_free(hmac);
  192. /* SHA-1 */
  193. hmac = av_hmac_alloc(AV_HMAC_SHA1);
  194. if (!hmac)
  195. return 1;
  196. // RFC 2202 test vectors
  197. test(hmac, key1, sizeof(key1), data1, sizeof(data1));
  198. test(hmac, key2, sizeof(key2), data2, sizeof(data2));
  199. test(hmac, key3, 20, data3, sizeof(data3));
  200. test(hmac, key3, 80, data4, sizeof(data4));
  201. test(hmac, key3, 80, data5, sizeof(data5));
  202. av_hmac_free(hmac);
  203. /* SHA-2 */
  204. while (i <= AV_HMAC_SHA256) {
  205. hmac = av_hmac_alloc(i);
  206. // RFC 4231 test vectors
  207. test(hmac, key1, sizeof(key1), data1, sizeof(data1));
  208. test(hmac, key2, sizeof(key2), data2, sizeof(data2));
  209. test(hmac, key3, 20, data3, sizeof(data3));
  210. test(hmac, key3, sizeof(key3), data4, sizeof(data4));
  211. test(hmac, key3, sizeof(key3), data6, sizeof(data6));
  212. av_hmac_free(hmac);
  213. i++;
  214. }
  215. return 0;
  216. }
  217. #endif /* TEST */