hmac.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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_freep(&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. }