hmac.c 7.6 KB

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