da_md5.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Copyright [2021] JD.com, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "da_hashkit.h"
  17. #include <string.h>
  18. typedef unsigned int MD5_u32plus;
  19. typedef struct {
  20. MD5_u32plus lo, hi;
  21. MD5_u32plus a, b, c, d;
  22. unsigned char buffer[64];
  23. MD5_u32plus block[16];
  24. } MD5_CTX;
  25. /*
  26. * The basic MD5 functions.
  27. *
  28. * F and G are optimized compared to their RFC 1321 definitions for
  29. * architectures that lack an AND-NOT instruction, just like in Colin Plumb's
  30. * implementation.
  31. */
  32. #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
  33. #define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y))))
  34. #define H(x, y, z) ((x) ^ (y) ^ (z))
  35. #define I(x, y, z) ((y) ^ ((x) | ~(z)))
  36. /*
  37. * The MD5 transformation for all four rounds.
  38. */
  39. #define STEP(f, a, b, c, d, x, t, s) \
  40. (a) += f((b), (c), (d)) + (x) + (t); \
  41. (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \
  42. (a) += (b);
  43. /*
  44. * SET reads 4 input bytes in little-endian byte order and stores them
  45. * in a properly aligned word in host byte order.
  46. *
  47. * The check for little-endian architectures that tolerate unaligned
  48. * memory accesses is just an optimization. Nothing will break if it
  49. * doesn't work.
  50. */
  51. #if defined(__i386__) || defined(__x86_64__) || defined(__vax__)
  52. #define SET(n) \
  53. (*(MD5_u32plus *)&ptr[(n) * 4])
  54. #define GET(n) \
  55. SET(n)
  56. #else
  57. #define SET(n) \
  58. (ctx->block[(n)] = \
  59. (MD5_u32plus)ptr[(n) * 4] | \
  60. ((MD5_u32plus)ptr[(n) * 4 + 1] << 8) | \
  61. ((MD5_u32plus)ptr[(n) * 4 + 2] << 16) | \
  62. ((MD5_u32plus)ptr[(n) * 4 + 3] << 24))
  63. #define GET(n) \
  64. (ctx->block[(n)])
  65. #endif
  66. /*
  67. * This processes one or more 64-byte data blocks, but does NOT update
  68. * the bit counters. There are no alignment requirements.
  69. */
  70. static void *
  71. body(MD5_CTX *ctx, void *data, unsigned long size)
  72. {
  73. unsigned char *ptr;
  74. MD5_u32plus a, b, c, d;
  75. MD5_u32plus saved_a, saved_b, saved_c, saved_d;
  76. ptr = data;
  77. a = ctx->a;
  78. b = ctx->b;
  79. c = ctx->c;
  80. d = ctx->d;
  81. do {
  82. saved_a = a;
  83. saved_b = b;
  84. saved_c = c;
  85. saved_d = d;
  86. /* Round 1 */
  87. STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7)
  88. STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12)
  89. STEP(F, c, d, a, b, SET(2), 0x242070db, 17)
  90. STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22)
  91. STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7)
  92. STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12)
  93. STEP(F, c, d, a, b, SET(6), 0xa8304613, 17)
  94. STEP(F, b, c, d, a, SET(7), 0xfd469501, 22)
  95. STEP(F, a, b, c, d, SET(8), 0x698098d8, 7)
  96. STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12)
  97. STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17)
  98. STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22)
  99. STEP(F, a, b, c, d, SET(12), 0x6b901122, 7)
  100. STEP(F, d, a, b, c, SET(13), 0xfd987193, 12)
  101. STEP(F, c, d, a, b, SET(14), 0xa679438e, 17)
  102. STEP(F, b, c, d, a, SET(15), 0x49b40821, 22)
  103. /* Round 2 */
  104. STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5)
  105. STEP(G, d, a, b, c, GET(6), 0xc040b340, 9)
  106. STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14)
  107. STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20)
  108. STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5)
  109. STEP(G, d, a, b, c, GET(10), 0x02441453, 9)
  110. STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14)
  111. STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20)
  112. STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5)
  113. STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9)
  114. STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14)
  115. STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20)
  116. STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5)
  117. STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9)
  118. STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14)
  119. STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20)
  120. /* Round 3 */
  121. STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4)
  122. STEP(H, d, a, b, c, GET(8), 0x8771f681, 11)
  123. STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16)
  124. STEP(H, b, c, d, a, GET(14), 0xfde5380c, 23)
  125. STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4)
  126. STEP(H, d, a, b, c, GET(4), 0x4bdecfa9, 11)
  127. STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16)
  128. STEP(H, b, c, d, a, GET(10), 0xbebfbc70, 23)
  129. STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4)
  130. STEP(H, d, a, b, c, GET(0), 0xeaa127fa, 11)
  131. STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16)
  132. STEP(H, b, c, d, a, GET(6), 0x04881d05, 23)
  133. STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4)
  134. STEP(H, d, a, b, c, GET(12), 0xe6db99e5, 11)
  135. STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16)
  136. STEP(H, b, c, d, a, GET(2), 0xc4ac5665, 23)
  137. /* Round 4 */
  138. STEP(I, a, b, c, d, GET(0), 0xf4292244, 6)
  139. STEP(I, d, a, b, c, GET(7), 0x432aff97, 10)
  140. STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15)
  141. STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21)
  142. STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6)
  143. STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10)
  144. STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15)
  145. STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21)
  146. STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6)
  147. STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10)
  148. STEP(I, c, d, a, b, GET(6), 0xa3014314, 15)
  149. STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21)
  150. STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6)
  151. STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10)
  152. STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15)
  153. STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21)
  154. a += saved_a;
  155. b += saved_b;
  156. c += saved_c;
  157. d += saved_d;
  158. ptr += 64;
  159. } while (size -= 64);
  160. ctx->a = a;
  161. ctx->b = b;
  162. ctx->c = c;
  163. ctx->d = d;
  164. return ptr;
  165. }
  166. void
  167. MD5_Init(MD5_CTX *ctx)
  168. {
  169. ctx->a = 0x67452301;
  170. ctx->b = 0xefcdab89;
  171. ctx->c = 0x98badcfe;
  172. ctx->d = 0x10325476;
  173. ctx->lo = 0;
  174. ctx->hi = 0;
  175. }
  176. void
  177. MD5_Update(MD5_CTX *ctx, void *data, unsigned long size)
  178. {
  179. MD5_u32plus saved_lo;
  180. unsigned long used, free;
  181. saved_lo = ctx->lo;
  182. if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo) {
  183. ctx->hi++;
  184. }
  185. ctx->hi += size >> 29;
  186. used = saved_lo & 0x3f;
  187. if (used) {
  188. free = 64 - used;
  189. if (size < free) {
  190. memcpy(&ctx->buffer[used], data, size);
  191. return;
  192. }
  193. memcpy(&ctx->buffer[used], data, free);
  194. data = (unsigned char *)data + free;
  195. size -= free;
  196. body(ctx, ctx->buffer, 64);
  197. }
  198. if (size >= 64) {
  199. data = body(ctx, data, size & ~(unsigned long)0x3f);
  200. size &= 0x3f;
  201. }
  202. memcpy(ctx->buffer, data, size);
  203. }
  204. void
  205. MD5_Final(unsigned char *result, MD5_CTX *ctx)
  206. {
  207. unsigned long used, free;
  208. used = ctx->lo & 0x3f;
  209. ctx->buffer[used++] = 0x80;
  210. free = 64 - used;
  211. if (free < 8) {
  212. memset(&ctx->buffer[used], 0, free);
  213. body(ctx, ctx->buffer, 64);
  214. used = 0;
  215. free = 64;
  216. }
  217. memset(&ctx->buffer[used], 0, free - 8);
  218. ctx->lo <<= 3;
  219. ctx->buffer[56] = ctx->lo;
  220. ctx->buffer[57] = ctx->lo >> 8;
  221. ctx->buffer[58] = ctx->lo >> 16;
  222. ctx->buffer[59] = ctx->lo >> 24;
  223. ctx->buffer[60] = ctx->hi;
  224. ctx->buffer[61] = ctx->hi >> 8;
  225. ctx->buffer[62] = ctx->hi >> 16;
  226. ctx->buffer[63] = ctx->hi >> 24;
  227. body(ctx, ctx->buffer, 64);
  228. result[0] = ctx->a;
  229. result[1] = ctx->a >> 8;
  230. result[2] = ctx->a >> 16;
  231. result[3] = ctx->a >> 24;
  232. result[4] = ctx->b;
  233. result[5] = ctx->b >> 8;
  234. result[6] = ctx->b >> 16;
  235. result[7] = ctx->b >> 24;
  236. result[8] = ctx->c;
  237. result[9] = ctx->c >> 8;
  238. result[10] = ctx->c >> 16;
  239. result[11] = ctx->c >> 24;
  240. result[12] = ctx->d;
  241. result[13] = ctx->d >> 8;
  242. result[14] = ctx->d >> 16;
  243. result[15] = ctx->d >> 24;
  244. memset(ctx, 0, sizeof(*ctx));
  245. }
  246. /*
  247. * Just a simple method for getting the signature
  248. * result must be == 16
  249. */
  250. void md5_signature(const unsigned char *key, unsigned int length, unsigned char *result)
  251. {
  252. MD5_CTX my_md5;
  253. MD5_Init(&my_md5);
  254. (void)MD5_Update(&my_md5,(void *)key, length);
  255. MD5_Final(result, &my_md5);
  256. }
  257. uint32_t
  258. hash_md5(const char *key, size_t key_length)
  259. {
  260. unsigned char results[16];
  261. md5_signature((unsigned char*)key, (unsigned long)key_length, results);
  262. return ((uint32_t) (results[3] & 0xFF) << 24) |
  263. ((uint32_t) (results[2] & 0xFF) << 16) |
  264. ((uint32_t) (results[1] & 0xFF) << 8) |
  265. (results[0] & 0xFF);
  266. }