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.

139 lines
3.5KB

  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 "hmac.h"
  22. #include "md5.h"
  23. #include "sha.h"
  24. #include "mem.h"
  25. #define MAX_HASHLEN 20
  26. #define MAX_BLOCKLEN 64
  27. struct AVHMAC {
  28. void *hash;
  29. int blocklen, hashlen;
  30. void (*final)(void*, uint8_t*);
  31. void (*update)(void*, const uint8_t*, int len);
  32. void (*init)(void*);
  33. uint8_t key[MAX_BLOCKLEN];
  34. int keylen;
  35. };
  36. static void sha1_init(void *ctx)
  37. {
  38. av_sha_init(ctx, 160);
  39. }
  40. AVHMAC *av_hmac_alloc(enum AVHMACType type)
  41. {
  42. AVHMAC *c = av_mallocz(sizeof(*c));
  43. if (!c)
  44. return NULL;
  45. switch (type) {
  46. case AV_HMAC_MD5:
  47. c->blocklen = 64;
  48. c->hashlen = 16;
  49. c->init = av_md5_init;
  50. c->update = av_md5_update;
  51. c->final = av_md5_final;
  52. c->hash = av_md5_alloc();
  53. break;
  54. case AV_HMAC_SHA1:
  55. c->blocklen = 64;
  56. c->hashlen = 20;
  57. c->init = sha1_init;
  58. c->update = av_sha_update;
  59. c->final = av_sha_final;
  60. c->hash = av_sha_alloc();
  61. break;
  62. default:
  63. av_free(c);
  64. return NULL;
  65. }
  66. if (!c->hash) {
  67. av_free(c);
  68. return NULL;
  69. }
  70. return c;
  71. }
  72. void av_hmac_free(AVHMAC *c)
  73. {
  74. if (!c)
  75. return;
  76. av_free(c->hash);
  77. av_free(c);
  78. }
  79. void av_hmac_init(AVHMAC *c, const uint8_t *key, unsigned int keylen)
  80. {
  81. int i;
  82. uint8_t block[MAX_BLOCKLEN];
  83. if (keylen > c->blocklen) {
  84. c->init(c->hash);
  85. c->update(c->hash, key, keylen);
  86. c->final(c->hash, c->key);
  87. c->keylen = c->hashlen;
  88. } else {
  89. memcpy(c->key, key, keylen);
  90. c->keylen = keylen;
  91. }
  92. c->init(c->hash);
  93. for (i = 0; i < c->keylen; i++)
  94. block[i] = c->key[i] ^ 0x36;
  95. for (i = c->keylen; i < c->blocklen; i++)
  96. block[i] = 0x36;
  97. c->update(c->hash, block, c->blocklen);
  98. }
  99. void av_hmac_update(AVHMAC *c, const uint8_t *data, unsigned int len)
  100. {
  101. c->update(c->hash, data, len);
  102. }
  103. int av_hmac_final(AVHMAC *c, uint8_t *out, unsigned int outlen)
  104. {
  105. uint8_t block[MAX_BLOCKLEN];
  106. int i;
  107. if (outlen < c->hashlen)
  108. return AVERROR(EINVAL);
  109. c->final(c->hash, out);
  110. c->init(c->hash);
  111. for (i = 0; i < c->keylen; i++)
  112. block[i] = c->key[i] ^ 0x5C;
  113. for (i = c->keylen; i < c->blocklen; i++)
  114. block[i] = 0x5C;
  115. c->update(c->hash, block, c->blocklen);
  116. c->update(c->hash, out, c->hashlen);
  117. c->final(c->hash, out);
  118. return c->hashlen;
  119. }
  120. int av_hmac_calc(AVHMAC *c, const uint8_t *data, unsigned int len,
  121. const uint8_t *key, unsigned int keylen,
  122. uint8_t *out, unsigned int outlen)
  123. {
  124. av_hmac_init(c, key, keylen);
  125. av_hmac_update(c, data, len);
  126. return av_hmac_final(c, out, outlen);
  127. }