md5.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * RFC 1321 compliant MD5 implementation
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * This file is part of mbed TLS (https://tls.mbed.org)
  20. */
  21. /*
  22. * The MD5 algorithm was designed by Ron Rivest in 1991.
  23. *
  24. * http://www.ietf.org/rfc/rfc1321.txt
  25. */
  26. #if !defined(MBEDTLS_CONFIG_FILE)
  27. #include "mbedtls/config.h"
  28. #else
  29. #include MBEDTLS_CONFIG_FILE
  30. #endif
  31. #if defined(MBEDTLS_MD5_C)
  32. #include "mbedtls/md5.h"
  33. #include <string.h>
  34. #if defined(MBEDTLS_SELF_TEST)
  35. #if defined(MBEDTLS_PLATFORM_C)
  36. #include "mbedtls/platform.h"
  37. #else
  38. #include <stdio.h>
  39. #define mbedtls_printf printf
  40. #endif /* MBEDTLS_PLATFORM_C */
  41. #endif /* MBEDTLS_SELF_TEST */
  42. #if !defined(MBEDTLS_MD5_ALT)
  43. /* Implementation that should never be optimized out by the compiler */
  44. static void mbedtls_zeroize( void *v, size_t n ) {
  45. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  46. }
  47. /*
  48. * 32-bit integer manipulation macros (little endian)
  49. */
  50. #ifndef GET_UINT32_LE
  51. #define GET_UINT32_LE(n,b,i) \
  52. { \
  53. (n) = ( (uint32_t) (b)[(i) ] ) \
  54. | ( (uint32_t) (b)[(i) + 1] << 8 ) \
  55. | ( (uint32_t) (b)[(i) + 2] << 16 ) \
  56. | ( (uint32_t) (b)[(i) + 3] << 24 ); \
  57. }
  58. #endif
  59. #ifndef PUT_UINT32_LE
  60. #define PUT_UINT32_LE(n,b,i) \
  61. { \
  62. (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
  63. (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
  64. (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
  65. (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
  66. }
  67. #endif
  68. void mbedtls_md5_init( mbedtls_md5_context *ctx )
  69. {
  70. memset( ctx, 0, sizeof( mbedtls_md5_context ) );
  71. }
  72. void mbedtls_md5_free( mbedtls_md5_context *ctx )
  73. {
  74. if( ctx == NULL )
  75. return;
  76. mbedtls_zeroize( ctx, sizeof( mbedtls_md5_context ) );
  77. }
  78. void mbedtls_md5_clone( mbedtls_md5_context *dst,
  79. const mbedtls_md5_context *src )
  80. {
  81. *dst = *src;
  82. }
  83. /*
  84. * MD5 context setup
  85. */
  86. int mbedtls_md5_starts_ret( mbedtls_md5_context *ctx )
  87. {
  88. ctx->total[0] = 0;
  89. ctx->total[1] = 0;
  90. ctx->state[0] = 0x67452301;
  91. ctx->state[1] = 0xEFCDAB89;
  92. ctx->state[2] = 0x98BADCFE;
  93. ctx->state[3] = 0x10325476;
  94. return( 0 );
  95. }
  96. #if !defined(MBEDTLS_MD5_PROCESS_ALT)
  97. int mbedtls_internal_md5_process( mbedtls_md5_context *ctx,
  98. const unsigned char data[64] )
  99. {
  100. uint32_t X[16], A, B, C, D;
  101. GET_UINT32_LE( X[ 0], data, 0 );
  102. GET_UINT32_LE( X[ 1], data, 4 );
  103. GET_UINT32_LE( X[ 2], data, 8 );
  104. GET_UINT32_LE( X[ 3], data, 12 );
  105. GET_UINT32_LE( X[ 4], data, 16 );
  106. GET_UINT32_LE( X[ 5], data, 20 );
  107. GET_UINT32_LE( X[ 6], data, 24 );
  108. GET_UINT32_LE( X[ 7], data, 28 );
  109. GET_UINT32_LE( X[ 8], data, 32 );
  110. GET_UINT32_LE( X[ 9], data, 36 );
  111. GET_UINT32_LE( X[10], data, 40 );
  112. GET_UINT32_LE( X[11], data, 44 );
  113. GET_UINT32_LE( X[12], data, 48 );
  114. GET_UINT32_LE( X[13], data, 52 );
  115. GET_UINT32_LE( X[14], data, 56 );
  116. GET_UINT32_LE( X[15], data, 60 );
  117. #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
  118. #define P(a,b,c,d,k,s,t) \
  119. { \
  120. a += F(b,c,d) + X[k] + t; a = S(a,s) + b; \
  121. }
  122. A = ctx->state[0];
  123. B = ctx->state[1];
  124. C = ctx->state[2];
  125. D = ctx->state[3];
  126. #define F(x,y,z) (z ^ (x & (y ^ z)))
  127. P( A, B, C, D, 0, 7, 0xD76AA478 );
  128. P( D, A, B, C, 1, 12, 0xE8C7B756 );
  129. P( C, D, A, B, 2, 17, 0x242070DB );
  130. P( B, C, D, A, 3, 22, 0xC1BDCEEE );
  131. P( A, B, C, D, 4, 7, 0xF57C0FAF );
  132. P( D, A, B, C, 5, 12, 0x4787C62A );
  133. P( C, D, A, B, 6, 17, 0xA8304613 );
  134. P( B, C, D, A, 7, 22, 0xFD469501 );
  135. P( A, B, C, D, 8, 7, 0x698098D8 );
  136. P( D, A, B, C, 9, 12, 0x8B44F7AF );
  137. P( C, D, A, B, 10, 17, 0xFFFF5BB1 );
  138. P( B, C, D, A, 11, 22, 0x895CD7BE );
  139. P( A, B, C, D, 12, 7, 0x6B901122 );
  140. P( D, A, B, C, 13, 12, 0xFD987193 );
  141. P( C, D, A, B, 14, 17, 0xA679438E );
  142. P( B, C, D, A, 15, 22, 0x49B40821 );
  143. #undef F
  144. #define F(x,y,z) (y ^ (z & (x ^ y)))
  145. P( A, B, C, D, 1, 5, 0xF61E2562 );
  146. P( D, A, B, C, 6, 9, 0xC040B340 );
  147. P( C, D, A, B, 11, 14, 0x265E5A51 );
  148. P( B, C, D, A, 0, 20, 0xE9B6C7AA );
  149. P( A, B, C, D, 5, 5, 0xD62F105D );
  150. P( D, A, B, C, 10, 9, 0x02441453 );
  151. P( C, D, A, B, 15, 14, 0xD8A1E681 );
  152. P( B, C, D, A, 4, 20, 0xE7D3FBC8 );
  153. P( A, B, C, D, 9, 5, 0x21E1CDE6 );
  154. P( D, A, B, C, 14, 9, 0xC33707D6 );
  155. P( C, D, A, B, 3, 14, 0xF4D50D87 );
  156. P( B, C, D, A, 8, 20, 0x455A14ED );
  157. P( A, B, C, D, 13, 5, 0xA9E3E905 );
  158. P( D, A, B, C, 2, 9, 0xFCEFA3F8 );
  159. P( C, D, A, B, 7, 14, 0x676F02D9 );
  160. P( B, C, D, A, 12, 20, 0x8D2A4C8A );
  161. #undef F
  162. #define F(x,y,z) (x ^ y ^ z)
  163. P( A, B, C, D, 5, 4, 0xFFFA3942 );
  164. P( D, A, B, C, 8, 11, 0x8771F681 );
  165. P( C, D, A, B, 11, 16, 0x6D9D6122 );
  166. P( B, C, D, A, 14, 23, 0xFDE5380C );
  167. P( A, B, C, D, 1, 4, 0xA4BEEA44 );
  168. P( D, A, B, C, 4, 11, 0x4BDECFA9 );
  169. P( C, D, A, B, 7, 16, 0xF6BB4B60 );
  170. P( B, C, D, A, 10, 23, 0xBEBFBC70 );
  171. P( A, B, C, D, 13, 4, 0x289B7EC6 );
  172. P( D, A, B, C, 0, 11, 0xEAA127FA );
  173. P( C, D, A, B, 3, 16, 0xD4EF3085 );
  174. P( B, C, D, A, 6, 23, 0x04881D05 );
  175. P( A, B, C, D, 9, 4, 0xD9D4D039 );
  176. P( D, A, B, C, 12, 11, 0xE6DB99E5 );
  177. P( C, D, A, B, 15, 16, 0x1FA27CF8 );
  178. P( B, C, D, A, 2, 23, 0xC4AC5665 );
  179. #undef F
  180. #define F(x,y,z) (y ^ (x | ~z))
  181. P( A, B, C, D, 0, 6, 0xF4292244 );
  182. P( D, A, B, C, 7, 10, 0x432AFF97 );
  183. P( C, D, A, B, 14, 15, 0xAB9423A7 );
  184. P( B, C, D, A, 5, 21, 0xFC93A039 );
  185. P( A, B, C, D, 12, 6, 0x655B59C3 );
  186. P( D, A, B, C, 3, 10, 0x8F0CCC92 );
  187. P( C, D, A, B, 10, 15, 0xFFEFF47D );
  188. P( B, C, D, A, 1, 21, 0x85845DD1 );
  189. P( A, B, C, D, 8, 6, 0x6FA87E4F );
  190. P( D, A, B, C, 15, 10, 0xFE2CE6E0 );
  191. P( C, D, A, B, 6, 15, 0xA3014314 );
  192. P( B, C, D, A, 13, 21, 0x4E0811A1 );
  193. P( A, B, C, D, 4, 6, 0xF7537E82 );
  194. P( D, A, B, C, 11, 10, 0xBD3AF235 );
  195. P( C, D, A, B, 2, 15, 0x2AD7D2BB );
  196. P( B, C, D, A, 9, 21, 0xEB86D391 );
  197. #undef F
  198. ctx->state[0] += A;
  199. ctx->state[1] += B;
  200. ctx->state[2] += C;
  201. ctx->state[3] += D;
  202. return( 0 );
  203. }
  204. #endif /* !MBEDTLS_MD5_PROCESS_ALT */
  205. /*
  206. * MD5 process buffer
  207. */
  208. int mbedtls_md5_update_ret( mbedtls_md5_context *ctx,
  209. const unsigned char *input,
  210. size_t ilen )
  211. {
  212. int ret;
  213. size_t fill;
  214. uint32_t left;
  215. if( ilen == 0 )
  216. return( 0 );
  217. left = ctx->total[0] & 0x3F;
  218. fill = 64 - left;
  219. ctx->total[0] += (uint32_t) ilen;
  220. ctx->total[0] &= 0xFFFFFFFF;
  221. if( ctx->total[0] < (uint32_t) ilen )
  222. ctx->total[1]++;
  223. if( left && ilen >= fill )
  224. {
  225. memcpy( (void *) (ctx->buffer + left), input, fill );
  226. if( ( ret = mbedtls_internal_md5_process( ctx, ctx->buffer ) ) != 0 )
  227. return( ret );
  228. input += fill;
  229. ilen -= fill;
  230. left = 0;
  231. }
  232. while( ilen >= 64 )
  233. {
  234. if( ( ret = mbedtls_internal_md5_process( ctx, input ) ) != 0 )
  235. return( ret );
  236. input += 64;
  237. ilen -= 64;
  238. }
  239. if( ilen > 0 )
  240. {
  241. memcpy( (void *) (ctx->buffer + left), input, ilen );
  242. }
  243. return( 0 );
  244. }
  245. static const unsigned char md5_padding[64] =
  246. {
  247. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  248. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  249. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  250. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  251. };
  252. /*
  253. * MD5 final digest
  254. */
  255. int mbedtls_md5_finish_ret( mbedtls_md5_context *ctx,
  256. unsigned char output[16] )
  257. {
  258. int ret;
  259. uint32_t last, padn;
  260. uint32_t high, low;
  261. unsigned char msglen[8];
  262. high = ( ctx->total[0] >> 29 )
  263. | ( ctx->total[1] << 3 );
  264. low = ( ctx->total[0] << 3 );
  265. PUT_UINT32_LE( low, msglen, 0 );
  266. PUT_UINT32_LE( high, msglen, 4 );
  267. last = ctx->total[0] & 0x3F;
  268. padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
  269. if( ( ret = mbedtls_md5_update_ret( ctx, md5_padding, padn ) ) != 0 )
  270. return( ret );
  271. if( ( ret = mbedtls_md5_update_ret( ctx, msglen, 8 ) ) != 0 )
  272. return( ret );
  273. PUT_UINT32_LE( ctx->state[0], output, 0 );
  274. PUT_UINT32_LE( ctx->state[1], output, 4 );
  275. PUT_UINT32_LE( ctx->state[2], output, 8 );
  276. PUT_UINT32_LE( ctx->state[3], output, 12 );
  277. return( 0 );
  278. }
  279. #endif /* !MBEDTLS_MD5_ALT */
  280. /*
  281. * output = MD5( input buffer )
  282. */
  283. int mbedtls_md5_ret( const unsigned char *input,
  284. size_t ilen,
  285. unsigned char output[16] )
  286. {
  287. int ret;
  288. mbedtls_md5_context ctx;
  289. mbedtls_md5_init( &ctx );
  290. if( ( ret = mbedtls_md5_starts_ret( &ctx ) ) != 0 )
  291. goto exit;
  292. if( ( ret = mbedtls_md5_update_ret( &ctx, input, ilen ) ) != 0 )
  293. goto exit;
  294. if( ( ret = mbedtls_md5_finish_ret( &ctx, output ) ) != 0 )
  295. goto exit;
  296. exit:
  297. mbedtls_md5_free( &ctx );
  298. return( ret );
  299. }
  300. #if defined(MBEDTLS_SELF_TEST)
  301. /*
  302. * RFC 1321 test vectors
  303. */
  304. static const unsigned char md5_test_buf[7][81] =
  305. {
  306. { "" },
  307. { "a" },
  308. { "abc" },
  309. { "message digest" },
  310. { "abcdefghijklmnopqrstuvwxyz" },
  311. { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" },
  312. { "12345678901234567890123456789012345678901234567890123456789012"
  313. "345678901234567890" }
  314. };
  315. static const size_t md5_test_buflen[7] =
  316. {
  317. 0, 1, 3, 14, 26, 62, 80
  318. };
  319. static const unsigned char md5_test_sum[7][16] =
  320. {
  321. { 0xD4, 0x1D, 0x8C, 0xD9, 0x8F, 0x00, 0xB2, 0x04,
  322. 0xE9, 0x80, 0x09, 0x98, 0xEC, 0xF8, 0x42, 0x7E },
  323. { 0x0C, 0xC1, 0x75, 0xB9, 0xC0, 0xF1, 0xB6, 0xA8,
  324. 0x31, 0xC3, 0x99, 0xE2, 0x69, 0x77, 0x26, 0x61 },
  325. { 0x90, 0x01, 0x50, 0x98, 0x3C, 0xD2, 0x4F, 0xB0,
  326. 0xD6, 0x96, 0x3F, 0x7D, 0x28, 0xE1, 0x7F, 0x72 },
  327. { 0xF9, 0x6B, 0x69, 0x7D, 0x7C, 0xB7, 0x93, 0x8D,
  328. 0x52, 0x5A, 0x2F, 0x31, 0xAA, 0xF1, 0x61, 0xD0 },
  329. { 0xC3, 0xFC, 0xD3, 0xD7, 0x61, 0x92, 0xE4, 0x00,
  330. 0x7D, 0xFB, 0x49, 0x6C, 0xCA, 0x67, 0xE1, 0x3B },
  331. { 0xD1, 0x74, 0xAB, 0x98, 0xD2, 0x77, 0xD9, 0xF5,
  332. 0xA5, 0x61, 0x1C, 0x2C, 0x9F, 0x41, 0x9D, 0x9F },
  333. { 0x57, 0xED, 0xF4, 0xA2, 0x2B, 0xE3, 0xC9, 0x55,
  334. 0xAC, 0x49, 0xDA, 0x2E, 0x21, 0x07, 0xB6, 0x7A }
  335. };
  336. /*
  337. * Checkup routine
  338. */
  339. int mbedtls_md5_self_test( int verbose )
  340. {
  341. int i, ret = 0;
  342. unsigned char md5sum[16];
  343. for( i = 0; i < 7; i++ )
  344. {
  345. if( verbose != 0 )
  346. mbedtls_printf( " MD5 test #%d: ", i + 1 );
  347. ret = mbedtls_md5_ret( md5_test_buf[i], md5_test_buflen[i], md5sum );
  348. if( ret != 0 )
  349. goto fail;
  350. if( memcmp( md5sum, md5_test_sum[i], 16 ) != 0 )
  351. {
  352. ret = 1;
  353. goto fail;
  354. }
  355. if( verbose != 0 )
  356. mbedtls_printf( "passed\n" );
  357. }
  358. if( verbose != 0 )
  359. mbedtls_printf( "\n" );
  360. return( 0 );
  361. fail:
  362. if( verbose != 0 )
  363. mbedtls_printf( "failed\n" );
  364. return( ret );
  365. }
  366. #endif /* MBEDTLS_SELF_TEST */
  367. #endif /* MBEDTLS_MD5_C */