md5.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*******************************************************************************
  2. * Portions COPYRIGHT 2015 STMicroelectronics *
  3. * Portions Copyright (C) 2006-2013, Brainspark B.V. *
  4. *******************************************************************************/
  5. /*
  6. * RFC 1321 compliant MD5 implementation
  7. *
  8. * Copyright (C) 2006-2013, Brainspark B.V.
  9. *
  10. * This file is part of PolarSSL (http://www.polarssl.org)
  11. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  12. *
  13. * All rights reserved.
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License along
  26. * with this program; if not, write to the Free Software Foundation, Inc.,
  27. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  28. */
  29. /*
  30. * The MD5 algorithm was designed by Ron Rivest in 1991.
  31. *
  32. * http://www.ietf.org/rfc/rfc1321.txt
  33. */
  34. /**
  35. ******************************************************************************
  36. * @file md5.c
  37. * @author MCD Application Team
  38. * @brief This file has been modified to support the hardware Cryptographic and
  39. * Hash processors embedded in STM32F415xx/417xx/437xx/439xx/756xx devices.
  40. * This support is activated by defining the "USE_STM32F4XX_HW_CRYPTO"
  41. * or "USE_STM32F7XX_HW_CRYPTO" macro in PolarSSL config.h file.
  42. ******************************************************************************
  43. * @attention
  44. *
  45. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  46. * You may not use this file except in compliance with the License.
  47. * You may obtain a copy of the License at:
  48. *
  49. * http://www.st.com/software_license_agreement_liberty_v2
  50. *
  51. * Unless required by applicable law or agreed to in writing, software
  52. * distributed under the License is distributed on an "AS IS" BASIS,
  53. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  54. * See the License for the specific language governing permissions and
  55. * limitations under the License.
  56. *
  57. ******************************************************************************
  58. */
  59. #include "md5.h"
  60. /*
  61. * 32-bit integer manipulation macros (little endian)
  62. */
  63. #ifndef GET_UINT32_LE
  64. #define GET_UINT32_LE(n, b, i) \
  65. { \
  66. (n) = ((uint32_t)(b)[(i)]) | ((uint32_t)(b)[(i) + 1] << 8) | \
  67. ((uint32_t)(b)[(i) + 2] << 16) | ((uint32_t)(b)[(i) + 3] << 24); \
  68. }
  69. #endif
  70. #ifndef PUT_UINT32_LE
  71. #define PUT_UINT32_LE(n, b, i) \
  72. { \
  73. (b)[(i)] = (unsigned char)((n)); \
  74. (b)[(i) + 1] = (unsigned char)((n) >> 8); \
  75. (b)[(i) + 2] = (unsigned char)((n) >> 16); \
  76. (b)[(i) + 3] = (unsigned char)((n) >> 24); \
  77. }
  78. #endif
  79. /*
  80. * MD5 context setup
  81. */
  82. void md5_starts(md5_context* ctx) {
  83. ctx->total[0] = 0;
  84. ctx->total[1] = 0;
  85. ctx->state[0] = 0x67452301;
  86. ctx->state[1] = 0xEFCDAB89;
  87. ctx->state[2] = 0x98BADCFE;
  88. ctx->state[3] = 0x10325476;
  89. }
  90. void md5_process(md5_context* ctx, const unsigned char data[64]) {
  91. uint32_t X[16], A, B, C, D;
  92. GET_UINT32_LE(X[0], data, 0);
  93. GET_UINT32_LE(X[1], data, 4);
  94. GET_UINT32_LE(X[2], data, 8);
  95. GET_UINT32_LE(X[3], data, 12);
  96. GET_UINT32_LE(X[4], data, 16);
  97. GET_UINT32_LE(X[5], data, 20);
  98. GET_UINT32_LE(X[6], data, 24);
  99. GET_UINT32_LE(X[7], data, 28);
  100. GET_UINT32_LE(X[8], data, 32);
  101. GET_UINT32_LE(X[9], data, 36);
  102. GET_UINT32_LE(X[10], data, 40);
  103. GET_UINT32_LE(X[11], data, 44);
  104. GET_UINT32_LE(X[12], data, 48);
  105. GET_UINT32_LE(X[13], data, 52);
  106. GET_UINT32_LE(X[14], data, 56);
  107. GET_UINT32_LE(X[15], data, 60);
  108. #define S(x, n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
  109. #define P(a, b, c, d, k, s, t) \
  110. { \
  111. a += F(b, c, d) + X[k] + t; \
  112. a = S(a, s) + b; \
  113. }
  114. A = ctx->state[0];
  115. B = ctx->state[1];
  116. C = ctx->state[2];
  117. D = ctx->state[3];
  118. #define F(x, y, z) (z ^ (x & (y ^ z)))
  119. P(A, B, C, D, 0, 7, 0xD76AA478);
  120. P(D, A, B, C, 1, 12, 0xE8C7B756);
  121. P(C, D, A, B, 2, 17, 0x242070DB);
  122. P(B, C, D, A, 3, 22, 0xC1BDCEEE);
  123. P(A, B, C, D, 4, 7, 0xF57C0FAF);
  124. P(D, A, B, C, 5, 12, 0x4787C62A);
  125. P(C, D, A, B, 6, 17, 0xA8304613);
  126. P(B, C, D, A, 7, 22, 0xFD469501);
  127. P(A, B, C, D, 8, 7, 0x698098D8);
  128. P(D, A, B, C, 9, 12, 0x8B44F7AF);
  129. P(C, D, A, B, 10, 17, 0xFFFF5BB1);
  130. P(B, C, D, A, 11, 22, 0x895CD7BE);
  131. P(A, B, C, D, 12, 7, 0x6B901122);
  132. P(D, A, B, C, 13, 12, 0xFD987193);
  133. P(C, D, A, B, 14, 17, 0xA679438E);
  134. P(B, C, D, A, 15, 22, 0x49B40821);
  135. #undef F
  136. #define F(x, y, z) (y ^ (z & (x ^ y)))
  137. P(A, B, C, D, 1, 5, 0xF61E2562);
  138. P(D, A, B, C, 6, 9, 0xC040B340);
  139. P(C, D, A, B, 11, 14, 0x265E5A51);
  140. P(B, C, D, A, 0, 20, 0xE9B6C7AA);
  141. P(A, B, C, D, 5, 5, 0xD62F105D);
  142. P(D, A, B, C, 10, 9, 0x02441453);
  143. P(C, D, A, B, 15, 14, 0xD8A1E681);
  144. P(B, C, D, A, 4, 20, 0xE7D3FBC8);
  145. P(A, B, C, D, 9, 5, 0x21E1CDE6);
  146. P(D, A, B, C, 14, 9, 0xC33707D6);
  147. P(C, D, A, B, 3, 14, 0xF4D50D87);
  148. P(B, C, D, A, 8, 20, 0x455A14ED);
  149. P(A, B, C, D, 13, 5, 0xA9E3E905);
  150. P(D, A, B, C, 2, 9, 0xFCEFA3F8);
  151. P(C, D, A, B, 7, 14, 0x676F02D9);
  152. P(B, C, D, A, 12, 20, 0x8D2A4C8A);
  153. #undef F
  154. #define F(x, y, z) (x ^ y ^ z)
  155. P(A, B, C, D, 5, 4, 0xFFFA3942);
  156. P(D, A, B, C, 8, 11, 0x8771F681);
  157. P(C, D, A, B, 11, 16, 0x6D9D6122);
  158. P(B, C, D, A, 14, 23, 0xFDE5380C);
  159. P(A, B, C, D, 1, 4, 0xA4BEEA44);
  160. P(D, A, B, C, 4, 11, 0x4BDECFA9);
  161. P(C, D, A, B, 7, 16, 0xF6BB4B60);
  162. P(B, C, D, A, 10, 23, 0xBEBFBC70);
  163. P(A, B, C, D, 13, 4, 0x289B7EC6);
  164. P(D, A, B, C, 0, 11, 0xEAA127FA);
  165. P(C, D, A, B, 3, 16, 0xD4EF3085);
  166. P(B, C, D, A, 6, 23, 0x04881D05);
  167. P(A, B, C, D, 9, 4, 0xD9D4D039);
  168. P(D, A, B, C, 12, 11, 0xE6DB99E5);
  169. P(C, D, A, B, 15, 16, 0x1FA27CF8);
  170. P(B, C, D, A, 2, 23, 0xC4AC5665);
  171. #undef F
  172. #define F(x, y, z) (y ^ (x | ~z))
  173. P(A, B, C, D, 0, 6, 0xF4292244);
  174. P(D, A, B, C, 7, 10, 0x432AFF97);
  175. P(C, D, A, B, 14, 15, 0xAB9423A7);
  176. P(B, C, D, A, 5, 21, 0xFC93A039);
  177. P(A, B, C, D, 12, 6, 0x655B59C3);
  178. P(D, A, B, C, 3, 10, 0x8F0CCC92);
  179. P(C, D, A, B, 10, 15, 0xFFEFF47D);
  180. P(B, C, D, A, 1, 21, 0x85845DD1);
  181. P(A, B, C, D, 8, 6, 0x6FA87E4F);
  182. P(D, A, B, C, 15, 10, 0xFE2CE6E0);
  183. P(C, D, A, B, 6, 15, 0xA3014314);
  184. P(B, C, D, A, 13, 21, 0x4E0811A1);
  185. P(A, B, C, D, 4, 6, 0xF7537E82);
  186. P(D, A, B, C, 11, 10, 0xBD3AF235);
  187. P(C, D, A, B, 2, 15, 0x2AD7D2BB);
  188. P(B, C, D, A, 9, 21, 0xEB86D391);
  189. #undef F
  190. ctx->state[0] += A;
  191. ctx->state[1] += B;
  192. ctx->state[2] += C;
  193. ctx->state[3] += D;
  194. }
  195. /*
  196. * MD5 process buffer
  197. */
  198. void md5_update(md5_context* ctx, const unsigned char* input, size_t ilen) {
  199. size_t fill;
  200. uint32_t left;
  201. if(ilen <= 0) return;
  202. left = ctx->total[0] & 0x3F;
  203. fill = 64 - left;
  204. ctx->total[0] += (uint32_t)ilen;
  205. ctx->total[0] &= 0xFFFFFFFF;
  206. if(ctx->total[0] < (uint32_t)ilen) ctx->total[1]++;
  207. if(left && ilen >= fill) {
  208. memcpy((void*)(ctx->buffer + left), input, fill);
  209. md5_process(ctx, ctx->buffer);
  210. input += fill;
  211. ilen -= fill;
  212. left = 0;
  213. }
  214. while(ilen >= 64) {
  215. md5_process(ctx, input);
  216. input += 64;
  217. ilen -= 64;
  218. }
  219. if(ilen > 0) {
  220. memcpy((void*)(ctx->buffer + left), input, ilen);
  221. }
  222. }
  223. static const unsigned char md5_padding[64] = {0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  224. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  225. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  226. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  227. /*
  228. * MD5 final digest
  229. */
  230. void md5_finish(md5_context* ctx, unsigned char output[16]) {
  231. uint32_t last, padn;
  232. uint32_t high, low;
  233. unsigned char msglen[8];
  234. high = (ctx->total[0] >> 29) | (ctx->total[1] << 3);
  235. low = (ctx->total[0] << 3);
  236. PUT_UINT32_LE(low, msglen, 0);
  237. PUT_UINT32_LE(high, msglen, 4);
  238. last = ctx->total[0] & 0x3F;
  239. padn = (last < 56) ? (56 - last) : (120 - last);
  240. md5_update(ctx, md5_padding, padn);
  241. md5_update(ctx, msglen, 8);
  242. PUT_UINT32_LE(ctx->state[0], output, 0);
  243. PUT_UINT32_LE(ctx->state[1], output, 4);
  244. PUT_UINT32_LE(ctx->state[2], output, 8);
  245. PUT_UINT32_LE(ctx->state[3], output, 12);
  246. }
  247. /*
  248. * output = MD5( input buffer )
  249. */
  250. void md5(const unsigned char* input, size_t ilen, unsigned char output[16]) {
  251. md5_context ctx;
  252. md5_starts(&ctx);
  253. md5_update(&ctx, input, ilen);
  254. md5_finish(&ctx, output);
  255. memset(&ctx, 0, sizeof(md5_context));
  256. }