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.

238 lines
6.8KB

  1. /*
  2. * Copyright (C) 2007 Michael Niedermayer <michaelni@gmx.at>
  3. * based on public domain SHA-1 code by Steve Reid <steve@edmweb.com>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "common.h"
  22. #include "avutil.h"
  23. #include "bswap.h"
  24. #include "sha.h"
  25. /** hash context */
  26. typedef struct AVSHA {
  27. uint8_t digest_len; ///< digest length in 32-bit words
  28. uint64_t count; ///< number of bytes in buffer
  29. uint8_t buffer[64]; ///< 512-bit buffer of input values used in hash updating
  30. uint32_t state[8]; ///< current hash value
  31. /** function used to update hash for 512-bit input block */
  32. void (*transform)(uint32_t *state, const uint8_t buffer[64]);
  33. } AVSHA;
  34. const int av_sha_size = sizeof(AVSHA);
  35. #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
  36. /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
  37. #define blk0(i) (block[i] = be2me_32(((const uint32_t*)buffer)[i]))
  38. #define blk(i) (block[i] = rol(block[i-3] ^ block[i-8] ^ block[i-14] ^ block[i-16], 1))
  39. #define R0(v,w,x,y,z,i) z += ((w&(x^y))^y) + blk0(i) + 0x5A827999 + rol(v, 5); w = rol(w, 30);
  40. #define R1(v,w,x,y,z,i) z += ((w&(x^y))^y) + blk (i) + 0x5A827999 + rol(v, 5); w = rol(w, 30);
  41. #define R2(v,w,x,y,z,i) z += ( w^x ^y) + blk (i) + 0x6ED9EBA1 + rol(v, 5); w = rol(w, 30);
  42. #define R3(v,w,x,y,z,i) z += (((w|x)&y)|(w&x)) + blk (i) + 0x8F1BBCDC + rol(v, 5); w = rol(w, 30);
  43. #define R4(v,w,x,y,z,i) z += ( w^x ^y) + blk (i) + 0xCA62C1D6 + rol(v, 5); w = rol(w, 30);
  44. /* Hash a single 512-bit block. This is the core of the algorithm. */
  45. static void sha1_transform(uint32_t state[5], const uint8_t buffer[64])
  46. {
  47. uint32_t block[80];
  48. unsigned int i, a, b, c, d, e;
  49. a = state[0];
  50. b = state[1];
  51. c = state[2];
  52. d = state[3];
  53. e = state[4];
  54. #if CONFIG_SMALL
  55. for (i = 0; i < 80; i++) {
  56. int t;
  57. if (i < 16)
  58. t = be2me_32(((uint32_t*)buffer)[i]);
  59. else
  60. t = rol(block[i-3] ^ block[i-8] ^ block[i-14] ^ block[i-16], 1);
  61. block[i] = t;
  62. t += e + rol(a, 5);
  63. if (i < 40) {
  64. if (i < 20)
  65. t += ((b&(c^d))^d) + 0x5A827999;
  66. else
  67. t += ( b^c ^d) + 0x6ED9EBA1;
  68. } else {
  69. if (i < 60)
  70. t += (((b|c)&d)|(b&c)) + 0x8F1BBCDC;
  71. else
  72. t += ( b^c ^d) + 0xCA62C1D6;
  73. }
  74. e = d;
  75. d = c;
  76. c = rol(b, 30);
  77. b = a;
  78. a = t;
  79. }
  80. #else
  81. for (i = 0; i < 15; i += 5) {
  82. R0(a, b, c, d, e, 0 + i);
  83. R0(e, a, b, c, d, 1 + i);
  84. R0(d, e, a, b, c, 2 + i);
  85. R0(c, d, e, a, b, 3 + i);
  86. R0(b, c, d, e, a, 4 + i);
  87. }
  88. R0(a, b, c, d, e, 15);
  89. R1(e, a, b, c, d, 16);
  90. R1(d, e, a, b, c, 17);
  91. R1(c, d, e, a, b, 18);
  92. R1(b, c, d, e, a, 19);
  93. for (i = 20; i < 40; i += 5) {
  94. R2(a, b, c, d, e, 0 + i);
  95. R2(e, a, b, c, d, 1 + i);
  96. R2(d, e, a, b, c, 2 + i);
  97. R2(c, d, e, a, b, 3 + i);
  98. R2(b, c, d, e, a, 4 + i);
  99. }
  100. for (; i < 60; i += 5) {
  101. R3(a, b, c, d, e, 0 + i);
  102. R3(e, a, b, c, d, 1 + i);
  103. R3(d, e, a, b, c, 2 + i);
  104. R3(c, d, e, a, b, 3 + i);
  105. R3(b, c, d, e, a, 4 + i);
  106. }
  107. for (; i < 80; i += 5) {
  108. R4(a, b, c, d, e, 0 + i);
  109. R4(e, a, b, c, d, 1 + i);
  110. R4(d, e, a, b, c, 2 + i);
  111. R4(c, d, e, a, b, 3 + i);
  112. R4(b, c, d, e, a, 4 + i);
  113. }
  114. #endif
  115. state[0] += a;
  116. state[1] += b;
  117. state[2] += c;
  118. state[3] += d;
  119. state[4] += e;
  120. }
  121. int av_sha_init(AVSHA* ctx, int bits)
  122. {
  123. ctx->state[0] = 0x67452301;
  124. ctx->state[1] = 0xEFCDAB89;
  125. ctx->state[2] = 0x98BADCFE;
  126. ctx->state[3] = 0x10325476;
  127. ctx->state[4] = 0xC3D2E1F0;
  128. ctx->transform = sha1_transform;
  129. ctx->count = 0;
  130. return 0;
  131. }
  132. void av_sha_update(AVSHA* ctx, const uint8_t* data, unsigned int len)
  133. {
  134. unsigned int i, j;
  135. j = ctx->count & 63;
  136. ctx->count += len;
  137. #if CONFIG_SMALL
  138. for (i = 0; i < len; i++) {
  139. ctx->buffer[j++] = data[i];
  140. if (64 == j) {
  141. ctx->transform(ctx->state, ctx->buffer);
  142. j = 0;
  143. }
  144. }
  145. #else
  146. if ((j + len) > 63) {
  147. memcpy(&ctx->buffer[j], data, (i = 64 - j));
  148. ctx->transform(ctx->state, ctx->buffer);
  149. for (; i + 63 < len; i += 64)
  150. ctx->transform(ctx->state, &data[i]);
  151. j = 0;
  152. } else
  153. i = 0;
  154. memcpy(&ctx->buffer[j], &data[i], len - i);
  155. #endif
  156. }
  157. void av_sha_final(AVSHA* ctx, uint8_t *digest)
  158. {
  159. int i;
  160. uint64_t finalcount = be2me_64(ctx->count << 3);
  161. av_sha_update(ctx, "\200", 1);
  162. while ((ctx->count & 63) != 56)
  163. av_sha_update(ctx, "", 1);
  164. av_sha_update(ctx, (uint8_t *)&finalcount, 8); /* Should cause a transform() */
  165. for (i = 0; i < 5; i++)
  166. ((uint32_t*)digest)[i] = be2me_32(ctx->state[i]);
  167. }
  168. #if LIBAVUTIL_VERSION_MAJOR < 51
  169. struct AVSHA1 {
  170. AVSHA sha;
  171. };
  172. const int av_sha1_size = sizeof(struct AVSHA1);
  173. void av_sha1_init(struct AVSHA1* context)
  174. {
  175. av_sha_init(&context->sha, 160);
  176. }
  177. void av_sha1_update(struct AVSHA1* context, const uint8_t* data, unsigned int len)
  178. {
  179. av_sha_update(&context->sha, data, len);
  180. }
  181. void av_sha1_final(struct AVSHA1* context, uint8_t digest[20])
  182. {
  183. av_sha_final(&context->sha, digest);
  184. }
  185. #endif
  186. #ifdef TEST
  187. #include <stdio.h>
  188. #undef printf
  189. int main(void)
  190. {
  191. int i, k;
  192. AVSHA ctx;
  193. unsigned char digest[20];
  194. for (k = 0; k < 3; k++) {
  195. av_sha_init(&ctx, 160);
  196. if (k == 0)
  197. av_sha_update(&ctx, "abc", 3);
  198. else if (k == 1)
  199. av_sha_update(&ctx, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56);
  200. else
  201. for (i = 0; i < 1000*1000; i++)
  202. av_sha_update(&ctx, "a", 1);
  203. av_sha_final(&ctx, digest);
  204. for (i = 0; i < 20; i++)
  205. printf("%02X", digest[i]);
  206. putchar('\n');
  207. }
  208. //test vectors (from FIPS PUB 180-1)
  209. printf("A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D\n"
  210. "84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1\n"
  211. "34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F\n");
  212. return 0;
  213. }
  214. #endif