hmac.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 <stddef.h>
  21. #include <stdint.h>
  22. #include <string.h>
  23. #include "attributes.h"
  24. #include "hmac.h"
  25. #include "md5.h"
  26. #include "sha.h"
  27. #include "sha512.h"
  28. #include "mem.h"
  29. #include "version.h"
  30. #define MAX_HASHLEN 64
  31. #define MAX_BLOCKLEN 128
  32. typedef void (*hmac_final)(void *ctx, uint8_t *dst);
  33. #if FF_API_CRYPTO_SIZE_T
  34. typedef void (*hmac_update)(void *ctx, const uint8_t *src, int len);
  35. #else
  36. typedef void (*hmac_update)(void *ctx, const uint8_t *src, size_t len);
  37. #endif
  38. typedef void (*hmac_init)(void *ctx);
  39. struct AVHMAC {
  40. void *hash;
  41. int blocklen, hashlen;
  42. hmac_final final;
  43. hmac_update update;
  44. hmac_init init;
  45. uint8_t key[MAX_BLOCKLEN];
  46. int keylen;
  47. };
  48. #define DEFINE_SHA(bits) \
  49. static av_cold void sha ## bits ##_init(void *ctx) \
  50. { \
  51. av_sha_init(ctx, bits); \
  52. }
  53. #define DEFINE_SHA512(bits) \
  54. static av_cold void sha ## bits ##_init(void *ctx) \
  55. { \
  56. av_sha512_init(ctx, bits); \
  57. }
  58. DEFINE_SHA(160)
  59. DEFINE_SHA(224)
  60. DEFINE_SHA(256)
  61. DEFINE_SHA512(384)
  62. DEFINE_SHA512(512)
  63. AVHMAC *av_hmac_alloc(enum AVHMACType type)
  64. {
  65. AVHMAC *c = av_mallocz(sizeof(*c));
  66. if (!c)
  67. return NULL;
  68. switch (type) {
  69. case AV_HMAC_MD5:
  70. c->blocklen = 64;
  71. c->hashlen = 16;
  72. c->init = (hmac_init) av_md5_init;
  73. c->update = (hmac_update) av_md5_update;
  74. c->final = (hmac_final) av_md5_final;
  75. c->hash = av_md5_alloc();
  76. break;
  77. case AV_HMAC_SHA1:
  78. c->blocklen = 64;
  79. c->hashlen = 20;
  80. c->init = sha160_init;
  81. c->update = (hmac_update) av_sha_update;
  82. c->final = (hmac_final) av_sha_final;
  83. c->hash = av_sha_alloc();
  84. break;
  85. case AV_HMAC_SHA224:
  86. c->blocklen = 64;
  87. c->hashlen = 28;
  88. c->init = sha224_init;
  89. c->update = (hmac_update) av_sha_update;
  90. c->final = (hmac_final) av_sha_final;
  91. c->hash = av_sha_alloc();
  92. break;
  93. case AV_HMAC_SHA256:
  94. c->blocklen = 64;
  95. c->hashlen = 32;
  96. c->init = sha256_init;
  97. c->update = (hmac_update) av_sha_update;
  98. c->final = (hmac_final) av_sha_final;
  99. c->hash = av_sha_alloc();
  100. break;
  101. case AV_HMAC_SHA384:
  102. c->blocklen = 128;
  103. c->hashlen = 48;
  104. c->init = sha384_init;
  105. c->update = (hmac_update) av_sha512_update;
  106. c->final = (hmac_final) av_sha512_final;
  107. c->hash = av_sha512_alloc();
  108. break;
  109. case AV_HMAC_SHA512:
  110. c->blocklen = 128;
  111. c->hashlen = 64;
  112. c->init = sha512_init;
  113. c->update = (hmac_update) av_sha512_update;
  114. c->final = (hmac_final) av_sha512_final;
  115. c->hash = av_sha512_alloc();
  116. break;
  117. default:
  118. av_free(c);
  119. return NULL;
  120. }
  121. if (!c->hash) {
  122. av_free(c);
  123. return NULL;
  124. }
  125. return c;
  126. }
  127. void av_hmac_free(AVHMAC *c)
  128. {
  129. if (!c)
  130. return;
  131. av_freep(&c->hash);
  132. av_free(c);
  133. }
  134. void av_hmac_init(AVHMAC *c, const uint8_t *key, unsigned int keylen)
  135. {
  136. int i;
  137. uint8_t block[MAX_BLOCKLEN];
  138. if (keylen > c->blocklen) {
  139. c->init(c->hash);
  140. c->update(c->hash, key, keylen);
  141. c->final(c->hash, c->key);
  142. c->keylen = c->hashlen;
  143. } else {
  144. memcpy(c->key, key, keylen);
  145. c->keylen = keylen;
  146. }
  147. c->init(c->hash);
  148. for (i = 0; i < c->keylen; i++)
  149. block[i] = c->key[i] ^ 0x36;
  150. for (i = c->keylen; i < c->blocklen; i++)
  151. block[i] = 0x36;
  152. c->update(c->hash, block, c->blocklen);
  153. }
  154. void av_hmac_update(AVHMAC *c, const uint8_t *data, unsigned int len)
  155. {
  156. c->update(c->hash, data, len);
  157. }
  158. int av_hmac_final(AVHMAC *c, uint8_t *out, unsigned int outlen)
  159. {
  160. uint8_t block[MAX_BLOCKLEN];
  161. int i;
  162. if (outlen < c->hashlen)
  163. return AVERROR(EINVAL);
  164. c->final(c->hash, out);
  165. c->init(c->hash);
  166. for (i = 0; i < c->keylen; i++)
  167. block[i] = c->key[i] ^ 0x5C;
  168. for (i = c->keylen; i < c->blocklen; i++)
  169. block[i] = 0x5C;
  170. c->update(c->hash, block, c->blocklen);
  171. c->update(c->hash, out, c->hashlen);
  172. c->final(c->hash, out);
  173. return c->hashlen;
  174. }
  175. int av_hmac_calc(AVHMAC *c, const uint8_t *data, unsigned int len,
  176. const uint8_t *key, unsigned int keylen,
  177. uint8_t *out, unsigned int outlen)
  178. {
  179. av_hmac_init(c, key, keylen);
  180. av_hmac_update(c, data, len);
  181. return av_hmac_final(c, out, outlen);
  182. }