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.

273 lines
8.0KB

  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. #if FF_API_HMAC
  77. case AV_HMAC_SHA224_DEPRECATED:
  78. #endif
  79. c->blocklen = 64;
  80. c->hashlen = 28;
  81. c->init = sha224_init;
  82. c->update = (void*)av_sha_update;
  83. c->final = (void*)av_sha_final;
  84. c->hash = av_sha_alloc();
  85. break;
  86. case AV_HMAC_SHA256:
  87. #if FF_API_HMAC
  88. case AV_HMAC_SHA256_DEPRECATED:
  89. #endif
  90. c->blocklen = 64;
  91. c->hashlen = 32;
  92. c->init = sha256_init;
  93. c->update = (void*)av_sha_update;
  94. c->final = (void*)av_sha_final;
  95. c->hash = av_sha_alloc();
  96. break;
  97. case AV_HMAC_SHA384:
  98. c->blocklen = 128;
  99. c->hashlen = 48;
  100. c->init = sha384_init;
  101. c->update = (void*)av_sha512_update;
  102. c->final = (void*)av_sha512_final;
  103. c->hash = av_sha512_alloc();
  104. break;
  105. case AV_HMAC_SHA512:
  106. c->blocklen = 128;
  107. c->hashlen = 64;
  108. c->init = sha512_init;
  109. c->update = (void*)av_sha512_update;
  110. c->final = (void*)av_sha512_final;
  111. c->hash = av_sha512_alloc();
  112. break;
  113. default:
  114. av_free(c);
  115. return NULL;
  116. }
  117. if (!c->hash) {
  118. av_free(c);
  119. return NULL;
  120. }
  121. return c;
  122. }
  123. void av_hmac_free(AVHMAC *c)
  124. {
  125. if (!c)
  126. return;
  127. av_freep(&c->hash);
  128. av_free(c);
  129. }
  130. void av_hmac_init(AVHMAC *c, const uint8_t *key, unsigned int keylen)
  131. {
  132. int i;
  133. uint8_t block[MAX_BLOCKLEN];
  134. if (keylen > c->blocklen) {
  135. c->init(c->hash);
  136. c->update(c->hash, key, keylen);
  137. c->final(c->hash, c->key);
  138. c->keylen = c->hashlen;
  139. } else {
  140. memcpy(c->key, key, keylen);
  141. c->keylen = keylen;
  142. }
  143. c->init(c->hash);
  144. for (i = 0; i < c->keylen; i++)
  145. block[i] = c->key[i] ^ 0x36;
  146. for (i = c->keylen; i < c->blocklen; i++)
  147. block[i] = 0x36;
  148. c->update(c->hash, block, c->blocklen);
  149. }
  150. void av_hmac_update(AVHMAC *c, const uint8_t *data, unsigned int len)
  151. {
  152. c->update(c->hash, data, len);
  153. }
  154. int av_hmac_final(AVHMAC *c, uint8_t *out, unsigned int outlen)
  155. {
  156. uint8_t block[MAX_BLOCKLEN];
  157. int i;
  158. if (outlen < c->hashlen)
  159. return AVERROR(EINVAL);
  160. c->final(c->hash, out);
  161. c->init(c->hash);
  162. for (i = 0; i < c->keylen; i++)
  163. block[i] = c->key[i] ^ 0x5C;
  164. for (i = c->keylen; i < c->blocklen; i++)
  165. block[i] = 0x5C;
  166. c->update(c->hash, block, c->blocklen);
  167. c->update(c->hash, out, c->hashlen);
  168. c->final(c->hash, out);
  169. return c->hashlen;
  170. }
  171. int av_hmac_calc(AVHMAC *c, const uint8_t *data, unsigned int len,
  172. const uint8_t *key, unsigned int keylen,
  173. uint8_t *out, unsigned int outlen)
  174. {
  175. av_hmac_init(c, key, keylen);
  176. av_hmac_update(c, data, len);
  177. return av_hmac_final(c, out, outlen);
  178. }
  179. #ifdef TEST
  180. #include <stdio.h>
  181. static void test(AVHMAC *hmac, const uint8_t *key, int keylen,
  182. const uint8_t *data, int datalen)
  183. {
  184. uint8_t buf[MAX_HASHLEN];
  185. int out, i;
  186. // Some of the test vectors are strings, where sizeof() includes the
  187. // trailing null byte - remove that.
  188. if (!key[keylen - 1])
  189. keylen--;
  190. if (!data[datalen - 1])
  191. datalen--;
  192. out = av_hmac_calc(hmac, data, datalen, key, keylen, buf, sizeof(buf));
  193. for (i = 0; i < out; i++)
  194. printf("%02x", buf[i]);
  195. printf("\n");
  196. }
  197. int main(void)
  198. {
  199. uint8_t key1[20], key3[131], data3[50];
  200. AVHMAC *hmac;
  201. enum AVHMACType i;
  202. static const uint8_t key2[] = "Jefe";
  203. static const uint8_t data1[] = "Hi There";
  204. static const uint8_t data2[] = "what do ya want for nothing?";
  205. static const uint8_t data4[] = "Test Using Larger Than Block-Size Key - Hash Key First";
  206. static const uint8_t data5[] = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data";
  207. static const uint8_t data6[] = "This is a test using a larger than block-size key and a larger "
  208. "than block-size data. The key needs to be hashed before being used"
  209. " by the HMAC algorithm.";
  210. memset(key1, 0x0b, sizeof(key1));
  211. memset(key3, 0xaa, sizeof(key3));
  212. memset(data3, 0xdd, sizeof(data3));
  213. /* MD5, SHA-1 */
  214. for (i = AV_HMAC_MD5; i <= AV_HMAC_SHA1; i++) {
  215. hmac = av_hmac_alloc(i);
  216. if (!hmac)
  217. return 1;
  218. // RFC 2202 test vectors
  219. test(hmac, key1, hmac->hashlen, data1, sizeof(data1));
  220. test(hmac, key2, sizeof(key2), data2, sizeof(data2));
  221. test(hmac, key3, hmac->hashlen, data3, sizeof(data3));
  222. test(hmac, key3, 80, data4, sizeof(data4));
  223. test(hmac, key3, 80, data5, sizeof(data5));
  224. av_hmac_free(hmac);
  225. }
  226. /* SHA-2 */
  227. for (i = AV_HMAC_SHA224; i <= AV_HMAC_SHA256; i++) {
  228. hmac = av_hmac_alloc(i);
  229. if (!hmac)
  230. return 1;
  231. // RFC 4231 test vectors
  232. test(hmac, key1, sizeof(key1), data1, sizeof(data1));
  233. test(hmac, key2, sizeof(key2), data2, sizeof(data2));
  234. test(hmac, key3, 20, data3, sizeof(data3));
  235. test(hmac, key3, sizeof(key3), data4, sizeof(data4));
  236. test(hmac, key3, sizeof(key3), data6, sizeof(data6));
  237. av_hmac_free(hmac);
  238. }
  239. for (i = AV_HMAC_SHA384; i <= AV_HMAC_SHA512; i++) {
  240. hmac = av_hmac_alloc(i);
  241. if (!hmac)
  242. return 1;
  243. // RFC 4231 test vectors
  244. test(hmac, key1, sizeof(key1), data1, sizeof(data1));
  245. test(hmac, key2, sizeof(key2), data2, sizeof(data2));
  246. test(hmac, key3, 20, data3, sizeof(data3));
  247. test(hmac, key3, sizeof(key3), data4, sizeof(data4));
  248. test(hmac, key3, sizeof(key3), data6, sizeof(data6));
  249. av_hmac_free(hmac);
  250. }
  251. return 0;
  252. }
  253. #endif /* TEST */