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.

246 lines
6.8KB

  1. /*
  2. * Copyright (C) 2013 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
  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 "hash.h"
  23. #include "adler32.h"
  24. #include "crc.h"
  25. #include "md5.h"
  26. #include "murmur3.h"
  27. #include "ripemd.h"
  28. #include "sha.h"
  29. #include "sha512.h"
  30. #include "avstring.h"
  31. #include "base64.h"
  32. #include "error.h"
  33. #include "intreadwrite.h"
  34. #include "mem.h"
  35. enum hashtype {
  36. MD5,
  37. MURMUR3,
  38. RIPEMD128,
  39. RIPEMD160,
  40. RIPEMD256,
  41. RIPEMD320,
  42. SHA160,
  43. SHA224,
  44. SHA256,
  45. SHA512_224,
  46. SHA512_256,
  47. SHA384,
  48. SHA512,
  49. CRC32,
  50. ADLER32,
  51. NUM_HASHES
  52. };
  53. typedef struct AVHashContext {
  54. void *ctx;
  55. enum hashtype type;
  56. const AVCRC *crctab;
  57. uint32_t crc;
  58. } AVHashContext;
  59. static const struct {
  60. const char *name;
  61. int size;
  62. } hashdesc[] = {
  63. [MD5] = {"MD5", 16},
  64. [MURMUR3] = {"murmur3", 16},
  65. [RIPEMD128] = {"RIPEMD128", 16},
  66. [RIPEMD160] = {"RIPEMD160", 20},
  67. [RIPEMD256] = {"RIPEMD256", 32},
  68. [RIPEMD320] = {"RIPEMD320", 40},
  69. [SHA160] = {"SHA160", 20},
  70. [SHA224] = {"SHA224", 28},
  71. [SHA256] = {"SHA256", 32},
  72. [SHA512_224] = {"SHA512/224", 28},
  73. [SHA512_256] = {"SHA512/256", 32},
  74. [SHA384] = {"SHA384", 48},
  75. [SHA512] = {"SHA512", 64},
  76. [CRC32] = {"CRC32", 4},
  77. [ADLER32] = {"adler32", 4},
  78. };
  79. const char *av_hash_names(int i)
  80. {
  81. if (i < 0 || i >= NUM_HASHES) return NULL;
  82. return hashdesc[i].name;
  83. }
  84. const char *av_hash_get_name(const AVHashContext *ctx)
  85. {
  86. return hashdesc[ctx->type].name;
  87. }
  88. int av_hash_get_size(const AVHashContext *ctx)
  89. {
  90. return hashdesc[ctx->type].size;
  91. }
  92. int av_hash_alloc(AVHashContext **ctx, const char *name)
  93. {
  94. AVHashContext *res;
  95. int i;
  96. *ctx = NULL;
  97. for (i = 0; i < NUM_HASHES; i++)
  98. if (av_strcasecmp(name, hashdesc[i].name) == 0)
  99. break;
  100. if (i >= NUM_HASHES) return AVERROR(EINVAL);
  101. res = av_mallocz(sizeof(*res));
  102. if (!res) return AVERROR(ENOMEM);
  103. res->type = i;
  104. switch (i) {
  105. case MD5: res->ctx = av_md5_alloc(); break;
  106. case MURMUR3: res->ctx = av_murmur3_alloc(); break;
  107. case RIPEMD128:
  108. case RIPEMD160:
  109. case RIPEMD256:
  110. case RIPEMD320: res->ctx = av_ripemd_alloc(); break;
  111. case SHA160:
  112. case SHA224:
  113. case SHA256: res->ctx = av_sha_alloc(); break;
  114. case SHA512_224:
  115. case SHA512_256:
  116. case SHA384:
  117. case SHA512: res->ctx = av_sha512_alloc(); break;
  118. case CRC32: res->crctab = av_crc_get_table(AV_CRC_32_IEEE_LE); break;
  119. case ADLER32: break;
  120. }
  121. if (i != ADLER32 && i != CRC32 && !res->ctx) {
  122. av_free(res);
  123. return AVERROR(ENOMEM);
  124. }
  125. *ctx = res;
  126. return 0;
  127. }
  128. void av_hash_init(AVHashContext *ctx)
  129. {
  130. switch (ctx->type) {
  131. case MD5: av_md5_init(ctx->ctx); break;
  132. case MURMUR3: av_murmur3_init(ctx->ctx); break;
  133. case RIPEMD128: av_ripemd_init(ctx->ctx, 128); break;
  134. case RIPEMD160: av_ripemd_init(ctx->ctx, 160); break;
  135. case RIPEMD256: av_ripemd_init(ctx->ctx, 256); break;
  136. case RIPEMD320: av_ripemd_init(ctx->ctx, 320); break;
  137. case SHA160: av_sha_init(ctx->ctx, 160); break;
  138. case SHA224: av_sha_init(ctx->ctx, 224); break;
  139. case SHA256: av_sha_init(ctx->ctx, 256); break;
  140. case SHA512_224: av_sha512_init(ctx->ctx, 224); break;
  141. case SHA512_256: av_sha512_init(ctx->ctx, 256); break;
  142. case SHA384: av_sha512_init(ctx->ctx, 384); break;
  143. case SHA512: av_sha512_init(ctx->ctx, 512); break;
  144. case CRC32: ctx->crc = UINT32_MAX; break;
  145. case ADLER32: ctx->crc = 1; break;
  146. }
  147. }
  148. #if FF_API_CRYPTO_SIZE_T
  149. void av_hash_update(AVHashContext *ctx, const uint8_t *src, int len)
  150. #else
  151. void av_hash_update(AVHashContext *ctx, const uint8_t *src, size_t len)
  152. #endif
  153. {
  154. switch (ctx->type) {
  155. case MD5: av_md5_update(ctx->ctx, src, len); break;
  156. case MURMUR3: av_murmur3_update(ctx->ctx, src, len); break;
  157. case RIPEMD128:
  158. case RIPEMD160:
  159. case RIPEMD256:
  160. case RIPEMD320: av_ripemd_update(ctx->ctx, src, len); break;
  161. case SHA160:
  162. case SHA224:
  163. case SHA256: av_sha_update(ctx->ctx, src, len); break;
  164. case SHA512_224:
  165. case SHA512_256:
  166. case SHA384:
  167. case SHA512: av_sha512_update(ctx->ctx, src, len); break;
  168. case CRC32: ctx->crc = av_crc(ctx->crctab, ctx->crc, src, len); break;
  169. case ADLER32: ctx->crc = av_adler32_update(ctx->crc, src, len); break;
  170. }
  171. }
  172. void av_hash_final(AVHashContext *ctx, uint8_t *dst)
  173. {
  174. switch (ctx->type) {
  175. case MD5: av_md5_final(ctx->ctx, dst); break;
  176. case MURMUR3: av_murmur3_final(ctx->ctx, dst); break;
  177. case RIPEMD128:
  178. case RIPEMD160:
  179. case RIPEMD256:
  180. case RIPEMD320: av_ripemd_final(ctx->ctx, dst); break;
  181. case SHA160:
  182. case SHA224:
  183. case SHA256: av_sha_final(ctx->ctx, dst); break;
  184. case SHA512_224:
  185. case SHA512_256:
  186. case SHA384:
  187. case SHA512: av_sha512_final(ctx->ctx, dst); break;
  188. case CRC32: AV_WB32(dst, ctx->crc ^ UINT32_MAX); break;
  189. case ADLER32: AV_WB32(dst, ctx->crc); break;
  190. }
  191. }
  192. void av_hash_final_bin(struct AVHashContext *ctx, uint8_t *dst, int size)
  193. {
  194. uint8_t buf[AV_HASH_MAX_SIZE];
  195. unsigned rsize = av_hash_get_size(ctx);
  196. av_hash_final(ctx, buf);
  197. memcpy(dst, buf, FFMIN(size, rsize));
  198. if (size > rsize)
  199. memset(dst + rsize, 0, size - rsize);
  200. }
  201. void av_hash_final_hex(struct AVHashContext *ctx, uint8_t *dst, int size)
  202. {
  203. uint8_t buf[AV_HASH_MAX_SIZE];
  204. unsigned rsize = av_hash_get_size(ctx), i;
  205. av_hash_final(ctx, buf);
  206. for (i = 0; i < FFMIN(rsize, size / 2); i++)
  207. snprintf(dst + i * 2, size - i * 2, "%02x", buf[i]);
  208. }
  209. void av_hash_final_b64(struct AVHashContext *ctx, uint8_t *dst, int size)
  210. {
  211. uint8_t buf[AV_HASH_MAX_SIZE], b64[AV_BASE64_SIZE(AV_HASH_MAX_SIZE)];
  212. unsigned rsize = av_hash_get_size(ctx), osize;
  213. av_hash_final(ctx, buf);
  214. av_base64_encode(b64, sizeof(b64), buf, rsize);
  215. osize = AV_BASE64_SIZE(rsize);
  216. memcpy(dst, b64, FFMIN(osize, size));
  217. if (size < osize)
  218. dst[size - 1] = 0;
  219. }
  220. void av_hash_freep(AVHashContext **ctx)
  221. {
  222. if (*ctx)
  223. av_freep(&(*ctx)->ctx);
  224. av_freep(ctx);
  225. }