utils_md5.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * Tencent is pleased to support the open source community by making IoT Hub
  3. available.
  4. * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
  5. * Licensed under the MIT License (the "License"); you may not use this file
  6. except in
  7. * compliance with the License. You may obtain a copy of the License at
  8. * http://opensource.org/licenses/MIT
  9. * Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is
  11. * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  12. KIND,
  13. * either express or implied. See the License for the specific language
  14. governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. #include "utils_md5.h"
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include "qcloud_iot_export.h"
  25. #include "qcloud_iot_import.h"
  26. #define MD5_DIGEST_SIZE 16
  27. /* Implementation that should never be optimized out by the compiler */
  28. static void _utils_md5_zeroize(void *v, size_t n)
  29. {
  30. volatile unsigned char *p = v;
  31. while (n--) *p++ = 0;
  32. }
  33. /*
  34. * 32-bit integer manipulation macros (little endian)
  35. */
  36. #ifndef IOT_MD5_GET_UINT32_LE
  37. #define IOT_MD5_GET_UINT32_LE(n, b, i) \
  38. { \
  39. (n) = ((uint32_t)(b)[(i)]) | ((uint32_t)(b)[(i) + 1] << 8) | ((uint32_t)(b)[(i) + 2] << 16) | \
  40. ((uint32_t)(b)[(i) + 3] << 24); \
  41. }
  42. #endif
  43. #ifndef IOT_MD5_PUT_UINT32_LE
  44. #define IOT_MD5_PUT_UINT32_LE(n, b, i) \
  45. { \
  46. (b)[(i)] = (unsigned char)(((n)) & 0xFF); \
  47. (b)[(i) + 1] = (unsigned char)(((n) >> 8) & 0xFF); \
  48. (b)[(i) + 2] = (unsigned char)(((n) >> 16) & 0xFF); \
  49. (b)[(i) + 3] = (unsigned char)(((n) >> 24) & 0xFF); \
  50. }
  51. #endif
  52. void utils_md5_init(iot_md5_context *ctx)
  53. {
  54. memset(ctx, 0, sizeof(iot_md5_context));
  55. }
  56. void utils_md5_free(iot_md5_context *ctx)
  57. {
  58. if (ctx == NULL) {
  59. return;
  60. }
  61. _utils_md5_zeroize(ctx, sizeof(iot_md5_context));
  62. }
  63. void utils_md5_clone(iot_md5_context *dst, const iot_md5_context *src)
  64. {
  65. *dst = *src;
  66. }
  67. /*
  68. * MD5 context setup
  69. */
  70. void utils_md5_starts(iot_md5_context *ctx)
  71. {
  72. ctx->total[0] = 0;
  73. ctx->total[1] = 0;
  74. ctx->state[0] = 0x67452301;
  75. ctx->state[1] = 0xEFCDAB89;
  76. ctx->state[2] = 0x98BADCFE;
  77. ctx->state[3] = 0x10325476;
  78. }
  79. void utils_md5_process(iot_md5_context *ctx, const unsigned char data[64])
  80. {
  81. uint32_t X[16], A, B, C, D;
  82. IOT_MD5_GET_UINT32_LE(X[0], data, 0);
  83. IOT_MD5_GET_UINT32_LE(X[1], data, 4);
  84. IOT_MD5_GET_UINT32_LE(X[2], data, 8);
  85. IOT_MD5_GET_UINT32_LE(X[3], data, 12);
  86. IOT_MD5_GET_UINT32_LE(X[4], data, 16);
  87. IOT_MD5_GET_UINT32_LE(X[5], data, 20);
  88. IOT_MD5_GET_UINT32_LE(X[6], data, 24);
  89. IOT_MD5_GET_UINT32_LE(X[7], data, 28);
  90. IOT_MD5_GET_UINT32_LE(X[8], data, 32);
  91. IOT_MD5_GET_UINT32_LE(X[9], data, 36);
  92. IOT_MD5_GET_UINT32_LE(X[10], data, 40);
  93. IOT_MD5_GET_UINT32_LE(X[11], data, 44);
  94. IOT_MD5_GET_UINT32_LE(X[12], data, 48);
  95. IOT_MD5_GET_UINT32_LE(X[13], data, 52);
  96. IOT_MD5_GET_UINT32_LE(X[14], data, 56);
  97. IOT_MD5_GET_UINT32_LE(X[15], data, 60);
  98. #define S(x, n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
  99. #define P(a, b, c, d, k, s, t) \
  100. { \
  101. a += F(b, c, d) + X[k] + t; \
  102. a = S(a, s) + b; \
  103. }
  104. A = ctx->state[0];
  105. B = ctx->state[1];
  106. C = ctx->state[2];
  107. D = ctx->state[3];
  108. #define F(x, y, z) (z ^ (x & (y ^ z)))
  109. P(A, B, C, D, 0, 7, 0xD76AA478);
  110. P(D, A, B, C, 1, 12, 0xE8C7B756);
  111. P(C, D, A, B, 2, 17, 0x242070DB);
  112. P(B, C, D, A, 3, 22, 0xC1BDCEEE);
  113. P(A, B, C, D, 4, 7, 0xF57C0FAF);
  114. P(D, A, B, C, 5, 12, 0x4787C62A);
  115. P(C, D, A, B, 6, 17, 0xA8304613);
  116. P(B, C, D, A, 7, 22, 0xFD469501);
  117. P(A, B, C, D, 8, 7, 0x698098D8);
  118. P(D, A, B, C, 9, 12, 0x8B44F7AF);
  119. P(C, D, A, B, 10, 17, 0xFFFF5BB1);
  120. P(B, C, D, A, 11, 22, 0x895CD7BE);
  121. P(A, B, C, D, 12, 7, 0x6B901122);
  122. P(D, A, B, C, 13, 12, 0xFD987193);
  123. P(C, D, A, B, 14, 17, 0xA679438E);
  124. P(B, C, D, A, 15, 22, 0x49B40821);
  125. #undef F
  126. #define F(x, y, z) (y ^ (z & (x ^ y)))
  127. P(A, B, C, D, 1, 5, 0xF61E2562);
  128. P(D, A, B, C, 6, 9, 0xC040B340);
  129. P(C, D, A, B, 11, 14, 0x265E5A51);
  130. P(B, C, D, A, 0, 20, 0xE9B6C7AA);
  131. P(A, B, C, D, 5, 5, 0xD62F105D);
  132. P(D, A, B, C, 10, 9, 0x02441453);
  133. P(C, D, A, B, 15, 14, 0xD8A1E681);
  134. P(B, C, D, A, 4, 20, 0xE7D3FBC8);
  135. P(A, B, C, D, 9, 5, 0x21E1CDE6);
  136. P(D, A, B, C, 14, 9, 0xC33707D6);
  137. P(C, D, A, B, 3, 14, 0xF4D50D87);
  138. P(B, C, D, A, 8, 20, 0x455A14ED);
  139. P(A, B, C, D, 13, 5, 0xA9E3E905);
  140. P(D, A, B, C, 2, 9, 0xFCEFA3F8);
  141. P(C, D, A, B, 7, 14, 0x676F02D9);
  142. P(B, C, D, A, 12, 20, 0x8D2A4C8A);
  143. #undef F
  144. #define F(x, y, z) (x ^ y ^ z)
  145. P(A, B, C, D, 5, 4, 0xFFFA3942);
  146. P(D, A, B, C, 8, 11, 0x8771F681);
  147. P(C, D, A, B, 11, 16, 0x6D9D6122);
  148. P(B, C, D, A, 14, 23, 0xFDE5380C);
  149. P(A, B, C, D, 1, 4, 0xA4BEEA44);
  150. P(D, A, B, C, 4, 11, 0x4BDECFA9);
  151. P(C, D, A, B, 7, 16, 0xF6BB4B60);
  152. P(B, C, D, A, 10, 23, 0xBEBFBC70);
  153. P(A, B, C, D, 13, 4, 0x289B7EC6);
  154. P(D, A, B, C, 0, 11, 0xEAA127FA);
  155. P(C, D, A, B, 3, 16, 0xD4EF3085);
  156. P(B, C, D, A, 6, 23, 0x04881D05);
  157. P(A, B, C, D, 9, 4, 0xD9D4D039);
  158. P(D, A, B, C, 12, 11, 0xE6DB99E5);
  159. P(C, D, A, B, 15, 16, 0x1FA27CF8);
  160. P(B, C, D, A, 2, 23, 0xC4AC5665);
  161. #undef F
  162. #define F(x, y, z) (y ^ (x | ~z))
  163. P(A, B, C, D, 0, 6, 0xF4292244);
  164. P(D, A, B, C, 7, 10, 0x432AFF97);
  165. P(C, D, A, B, 14, 15, 0xAB9423A7);
  166. P(B, C, D, A, 5, 21, 0xFC93A039);
  167. P(A, B, C, D, 12, 6, 0x655B59C3);
  168. P(D, A, B, C, 3, 10, 0x8F0CCC92);
  169. P(C, D, A, B, 10, 15, 0xFFEFF47D);
  170. P(B, C, D, A, 1, 21, 0x85845DD1);
  171. P(A, B, C, D, 8, 6, 0x6FA87E4F);
  172. P(D, A, B, C, 15, 10, 0xFE2CE6E0);
  173. P(C, D, A, B, 6, 15, 0xA3014314);
  174. P(B, C, D, A, 13, 21, 0x4E0811A1);
  175. P(A, B, C, D, 4, 6, 0xF7537E82);
  176. P(D, A, B, C, 11, 10, 0xBD3AF235);
  177. P(C, D, A, B, 2, 15, 0x2AD7D2BB);
  178. P(B, C, D, A, 9, 21, 0xEB86D391);
  179. #undef F
  180. ctx->state[0] += A;
  181. ctx->state[1] += B;
  182. ctx->state[2] += C;
  183. ctx->state[3] += D;
  184. }
  185. /*
  186. * MD5 process buffer
  187. */
  188. void utils_md5_update(iot_md5_context *ctx, const unsigned char *input, size_t ilen)
  189. {
  190. size_t fill;
  191. uint32_t left;
  192. if (ilen == 0) {
  193. return;
  194. }
  195. left = ctx->total[0] & 0x3F;
  196. fill = 64 - left;
  197. ctx->total[0] += (uint32_t)ilen;
  198. ctx->total[0] &= 0xFFFFFFFF;
  199. if (ctx->total[0] < (uint32_t)ilen) {
  200. ctx->total[1]++;
  201. }
  202. if (left && ilen >= fill) {
  203. memcpy((void *)(ctx->buffer + left), input, fill);
  204. utils_md5_process(ctx, ctx->buffer);
  205. input += fill;
  206. ilen -= fill;
  207. left = 0;
  208. }
  209. while (ilen >= 64) {
  210. utils_md5_process(ctx, input);
  211. input += 64;
  212. ilen -= 64;
  213. }
  214. if (ilen > 0) {
  215. memcpy((void *)(ctx->buffer + left), input, ilen);
  216. }
  217. }
  218. static const unsigned char iot_md5_padding[64] = {0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  219. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  220. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  221. };
  222. /*
  223. * MD5 final digest
  224. */
  225. void utils_md5_finish(iot_md5_context *ctx, unsigned char output[16])
  226. {
  227. uint32_t last, padn;
  228. uint32_t high, low;
  229. unsigned char msglen[8];
  230. high = (ctx->total[0] >> 29) | (ctx->total[1] << 3);
  231. low = (ctx->total[0] << 3);
  232. IOT_MD5_PUT_UINT32_LE(low, msglen, 0);
  233. IOT_MD5_PUT_UINT32_LE(high, msglen, 4);
  234. last = ctx->total[0] & 0x3F;
  235. padn = (last < 56) ? (56 - last) : (120 - last);
  236. utils_md5_update(ctx, iot_md5_padding, padn);
  237. utils_md5_update(ctx, msglen, 8);
  238. IOT_MD5_PUT_UINT32_LE(ctx->state[0], output, 0);
  239. IOT_MD5_PUT_UINT32_LE(ctx->state[1], output, 4);
  240. IOT_MD5_PUT_UINT32_LE(ctx->state[2], output, 8);
  241. IOT_MD5_PUT_UINT32_LE(ctx->state[3], output, 12);
  242. }
  243. /*
  244. * output = MD5( input buffer )
  245. */
  246. void utils_md5(const unsigned char *input, size_t ilen, unsigned char output[16])
  247. {
  248. iot_md5_context ctx;
  249. utils_md5_init(&ctx);
  250. utils_md5_starts(&ctx);
  251. utils_md5_update(&ctx, input, ilen);
  252. utils_md5_finish(&ctx, output);
  253. utils_md5_free(&ctx);
  254. }
  255. int8_t utils_hb2hex(uint8_t hb)
  256. {
  257. hb = hb & 0xF;
  258. return (int8_t)(hb < 10 ? '0' + hb : hb - 10 + 'a');
  259. }
  260. void utils_md5_str(const unsigned char *input, size_t ilen, unsigned char *output)
  261. {
  262. int i;
  263. unsigned char buf_out[16];
  264. utils_md5(input, ilen, buf_out);
  265. //hex to string
  266. for (i = 0; i < 16; ++i) {
  267. output[i * 2] = utils_hb2hex(buf_out[i] >> 4);
  268. output[i * 2 + 1] = utils_hb2hex(buf_out[i]);
  269. }
  270. output[32] = '\0';
  271. }
  272. #ifdef __cplusplus
  273. }
  274. #endif