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.

161 lines
4.2KB

  1. /*
  2. * Copyright (C) 2012 Martin Storsjo
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; 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 "mem.h"
  26. #define MAX_HASHLEN 32
  27. #define MAX_BLOCKLEN 64
  28. struct AVHMAC {
  29. void *hash;
  30. int blocklen, hashlen;
  31. void (*final)(void*, uint8_t*);
  32. void (*update)(void*, const uint8_t*, int len);
  33. void (*init)(void*);
  34. uint8_t key[MAX_BLOCKLEN];
  35. int keylen;
  36. };
  37. #define DEFINE_SHA(bits) \
  38. static av_cold void sha ## bits ##_init(void *ctx) \
  39. { \
  40. av_sha_init(ctx, bits); \
  41. }
  42. DEFINE_SHA(160)
  43. DEFINE_SHA(224)
  44. DEFINE_SHA(256)
  45. AVHMAC *av_hmac_alloc(enum AVHMACType type)
  46. {
  47. AVHMAC *c = av_mallocz(sizeof(*c));
  48. if (!c)
  49. return NULL;
  50. switch (type) {
  51. case AV_HMAC_MD5:
  52. c->blocklen = 64;
  53. c->hashlen = 16;
  54. c->init = av_md5_init;
  55. c->update = av_md5_update;
  56. c->final = av_md5_final;
  57. c->hash = av_md5_alloc();
  58. break;
  59. case AV_HMAC_SHA1:
  60. c->blocklen = 64;
  61. c->hashlen = 20;
  62. c->init = sha160_init;
  63. c->update = av_sha_update;
  64. c->final = av_sha_final;
  65. c->hash = av_sha_alloc();
  66. break;
  67. case AV_HMAC_SHA224:
  68. c->blocklen = 64;
  69. c->hashlen = 28;
  70. c->init = sha224_init;
  71. c->update = av_sha_update;
  72. c->final = av_sha_final;
  73. c->hash = av_sha_alloc();
  74. break;
  75. case AV_HMAC_SHA256:
  76. c->blocklen = 64;
  77. c->hashlen = 32;
  78. c->init = sha256_init;
  79. c->update = av_sha_update;
  80. c->final = av_sha_final;
  81. c->hash = av_sha_alloc();
  82. break;
  83. default:
  84. av_free(c);
  85. return NULL;
  86. }
  87. if (!c->hash) {
  88. av_free(c);
  89. return NULL;
  90. }
  91. return c;
  92. }
  93. void av_hmac_free(AVHMAC *c)
  94. {
  95. if (!c)
  96. return;
  97. av_free(c->hash);
  98. av_free(c);
  99. }
  100. void av_hmac_init(AVHMAC *c, const uint8_t *key, unsigned int keylen)
  101. {
  102. int i;
  103. uint8_t block[MAX_BLOCKLEN];
  104. if (keylen > c->blocklen) {
  105. c->init(c->hash);
  106. c->update(c->hash, key, keylen);
  107. c->final(c->hash, c->key);
  108. c->keylen = c->hashlen;
  109. } else {
  110. memcpy(c->key, key, keylen);
  111. c->keylen = keylen;
  112. }
  113. c->init(c->hash);
  114. for (i = 0; i < c->keylen; i++)
  115. block[i] = c->key[i] ^ 0x36;
  116. for (i = c->keylen; i < c->blocklen; i++)
  117. block[i] = 0x36;
  118. c->update(c->hash, block, c->blocklen);
  119. }
  120. void av_hmac_update(AVHMAC *c, const uint8_t *data, unsigned int len)
  121. {
  122. c->update(c->hash, data, len);
  123. }
  124. int av_hmac_final(AVHMAC *c, uint8_t *out, unsigned int outlen)
  125. {
  126. uint8_t block[MAX_BLOCKLEN];
  127. int i;
  128. if (outlen < c->hashlen)
  129. return AVERROR(EINVAL);
  130. c->final(c->hash, out);
  131. c->init(c->hash);
  132. for (i = 0; i < c->keylen; i++)
  133. block[i] = c->key[i] ^ 0x5C;
  134. for (i = c->keylen; i < c->blocklen; i++)
  135. block[i] = 0x5C;
  136. c->update(c->hash, block, c->blocklen);
  137. c->update(c->hash, out, c->hashlen);
  138. c->final(c->hash, out);
  139. return c->hashlen;
  140. }
  141. int av_hmac_calc(AVHMAC *c, const uint8_t *data, unsigned int len,
  142. const uint8_t *key, unsigned int keylen,
  143. uint8_t *out, unsigned int outlen)
  144. {
  145. av_hmac_init(c, key, keylen);
  146. av_hmac_update(c, data, len);
  147. return av_hmac_final(c, out, outlen);
  148. }