utils_md5.c 8.8 KB

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