md5.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /*
  2. * RFC 1321 compliant MD5 implementation
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. *
  7. * This file is provided under the Apache License 2.0, or the
  8. * GNU General Public License v2.0 or later.
  9. *
  10. * **********
  11. * Apache License 2.0:
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. * **********
  26. *
  27. * **********
  28. * GNU General Public License v2.0 or later:
  29. *
  30. * This program is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version.
  34. *
  35. * This program is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License along
  41. * with this program; if not, write to the Free Software Foundation, Inc.,
  42. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  43. *
  44. * **********
  45. */
  46. /*
  47. * The MD5 algorithm was designed by Ron Rivest in 1991.
  48. *
  49. * http://www.ietf.org/rfc/rfc1321.txt
  50. */
  51. #if !defined(MBEDTLS_CONFIG_FILE)
  52. #include "mbedtls/config.h"
  53. #else
  54. #include MBEDTLS_CONFIG_FILE
  55. #endif
  56. #if defined(MBEDTLS_MD5_C)
  57. #include "mbedtls/md5.h"
  58. #include "mbedtls/platform_util.h"
  59. #include <string.h>
  60. #if defined(MBEDTLS_SELF_TEST)
  61. #if defined(MBEDTLS_PLATFORM_C)
  62. #include "mbedtls/platform.h"
  63. #else
  64. #include <stdio.h>
  65. #define mbedtls_printf printf
  66. #endif /* MBEDTLS_PLATFORM_C */
  67. #endif /* MBEDTLS_SELF_TEST */
  68. #if !defined(MBEDTLS_MD5_ALT)
  69. /*
  70. * 32-bit integer manipulation macros (little endian)
  71. */
  72. #ifndef GET_UINT32_LE
  73. #define GET_UINT32_LE(n,b,i) \
  74. { \
  75. (n) = ( (uint32_t) (b)[(i) ] ) \
  76. | ( (uint32_t) (b)[(i) + 1] << 8 ) \
  77. | ( (uint32_t) (b)[(i) + 2] << 16 ) \
  78. | ( (uint32_t) (b)[(i) + 3] << 24 ); \
  79. }
  80. #endif
  81. #ifndef PUT_UINT32_LE
  82. #define PUT_UINT32_LE(n,b,i) \
  83. { \
  84. (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
  85. (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
  86. (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
  87. (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
  88. }
  89. #endif
  90. void mbedtls_md5_init( mbedtls_md5_context *ctx )
  91. {
  92. memset( ctx, 0, sizeof( mbedtls_md5_context ) );
  93. }
  94. void mbedtls_md5_free( mbedtls_md5_context *ctx )
  95. {
  96. if( ctx == NULL )
  97. return;
  98. mbedtls_platform_zeroize( ctx, sizeof( mbedtls_md5_context ) );
  99. }
  100. void mbedtls_md5_clone( mbedtls_md5_context *dst,
  101. const mbedtls_md5_context *src )
  102. {
  103. *dst = *src;
  104. }
  105. /*
  106. * MD5 context setup
  107. */
  108. int mbedtls_md5_starts_ret( mbedtls_md5_context *ctx )
  109. {
  110. ctx->total[0] = 0;
  111. ctx->total[1] = 0;
  112. ctx->state[0] = 0x67452301;
  113. ctx->state[1] = 0xEFCDAB89;
  114. ctx->state[2] = 0x98BADCFE;
  115. ctx->state[3] = 0x10325476;
  116. return( 0 );
  117. }
  118. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  119. void mbedtls_md5_starts( mbedtls_md5_context *ctx )
  120. {
  121. mbedtls_md5_starts_ret( ctx );
  122. }
  123. #endif
  124. #if !defined(MBEDTLS_MD5_PROCESS_ALT)
  125. int mbedtls_internal_md5_process( mbedtls_md5_context *ctx,
  126. const unsigned char data[64] )
  127. {
  128. struct
  129. {
  130. uint32_t X[16], A, B, C, D;
  131. } local;
  132. GET_UINT32_LE( local.X[ 0], data, 0 );
  133. GET_UINT32_LE( local.X[ 1], data, 4 );
  134. GET_UINT32_LE( local.X[ 2], data, 8 );
  135. GET_UINT32_LE( local.X[ 3], data, 12 );
  136. GET_UINT32_LE( local.X[ 4], data, 16 );
  137. GET_UINT32_LE( local.X[ 5], data, 20 );
  138. GET_UINT32_LE( local.X[ 6], data, 24 );
  139. GET_UINT32_LE( local.X[ 7], data, 28 );
  140. GET_UINT32_LE( local.X[ 8], data, 32 );
  141. GET_UINT32_LE( local.X[ 9], data, 36 );
  142. GET_UINT32_LE( local.X[10], data, 40 );
  143. GET_UINT32_LE( local.X[11], data, 44 );
  144. GET_UINT32_LE( local.X[12], data, 48 );
  145. GET_UINT32_LE( local.X[13], data, 52 );
  146. GET_UINT32_LE( local.X[14], data, 56 );
  147. GET_UINT32_LE( local.X[15], data, 60 );
  148. #define S(x,n) \
  149. ( ( (x) << (n) ) | ( ( (x) & 0xFFFFFFFF) >> ( 32 - (n) ) ) )
  150. #define P(a,b,c,d,k,s,t) \
  151. do \
  152. { \
  153. (a) += F((b),(c),(d)) + local.X[(k)] + (t); \
  154. (a) = S((a),(s)) + (b); \
  155. } while( 0 )
  156. local.A = ctx->state[0];
  157. local.B = ctx->state[1];
  158. local.C = ctx->state[2];
  159. local.D = ctx->state[3];
  160. #define F(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
  161. P( local.A, local.B, local.C, local.D, 0, 7, 0xD76AA478 );
  162. P( local.D, local.A, local.B, local.C, 1, 12, 0xE8C7B756 );
  163. P( local.C, local.D, local.A, local.B, 2, 17, 0x242070DB );
  164. P( local.B, local.C, local.D, local.A, 3, 22, 0xC1BDCEEE );
  165. P( local.A, local.B, local.C, local.D, 4, 7, 0xF57C0FAF );
  166. P( local.D, local.A, local.B, local.C, 5, 12, 0x4787C62A );
  167. P( local.C, local.D, local.A, local.B, 6, 17, 0xA8304613 );
  168. P( local.B, local.C, local.D, local.A, 7, 22, 0xFD469501 );
  169. P( local.A, local.B, local.C, local.D, 8, 7, 0x698098D8 );
  170. P( local.D, local.A, local.B, local.C, 9, 12, 0x8B44F7AF );
  171. P( local.C, local.D, local.A, local.B, 10, 17, 0xFFFF5BB1 );
  172. P( local.B, local.C, local.D, local.A, 11, 22, 0x895CD7BE );
  173. P( local.A, local.B, local.C, local.D, 12, 7, 0x6B901122 );
  174. P( local.D, local.A, local.B, local.C, 13, 12, 0xFD987193 );
  175. P( local.C, local.D, local.A, local.B, 14, 17, 0xA679438E );
  176. P( local.B, local.C, local.D, local.A, 15, 22, 0x49B40821 );
  177. #undef F
  178. #define F(x,y,z) ((y) ^ ((z) & ((x) ^ (y))))
  179. P( local.A, local.B, local.C, local.D, 1, 5, 0xF61E2562 );
  180. P( local.D, local.A, local.B, local.C, 6, 9, 0xC040B340 );
  181. P( local.C, local.D, local.A, local.B, 11, 14, 0x265E5A51 );
  182. P( local.B, local.C, local.D, local.A, 0, 20, 0xE9B6C7AA );
  183. P( local.A, local.B, local.C, local.D, 5, 5, 0xD62F105D );
  184. P( local.D, local.A, local.B, local.C, 10, 9, 0x02441453 );
  185. P( local.C, local.D, local.A, local.B, 15, 14, 0xD8A1E681 );
  186. P( local.B, local.C, local.D, local.A, 4, 20, 0xE7D3FBC8 );
  187. P( local.A, local.B, local.C, local.D, 9, 5, 0x21E1CDE6 );
  188. P( local.D, local.A, local.B, local.C, 14, 9, 0xC33707D6 );
  189. P( local.C, local.D, local.A, local.B, 3, 14, 0xF4D50D87 );
  190. P( local.B, local.C, local.D, local.A, 8, 20, 0x455A14ED );
  191. P( local.A, local.B, local.C, local.D, 13, 5, 0xA9E3E905 );
  192. P( local.D, local.A, local.B, local.C, 2, 9, 0xFCEFA3F8 );
  193. P( local.C, local.D, local.A, local.B, 7, 14, 0x676F02D9 );
  194. P( local.B, local.C, local.D, local.A, 12, 20, 0x8D2A4C8A );
  195. #undef F
  196. #define F(x,y,z) ((x) ^ (y) ^ (z))
  197. P( local.A, local.B, local.C, local.D, 5, 4, 0xFFFA3942 );
  198. P( local.D, local.A, local.B, local.C, 8, 11, 0x8771F681 );
  199. P( local.C, local.D, local.A, local.B, 11, 16, 0x6D9D6122 );
  200. P( local.B, local.C, local.D, local.A, 14, 23, 0xFDE5380C );
  201. P( local.A, local.B, local.C, local.D, 1, 4, 0xA4BEEA44 );
  202. P( local.D, local.A, local.B, local.C, 4, 11, 0x4BDECFA9 );
  203. P( local.C, local.D, local.A, local.B, 7, 16, 0xF6BB4B60 );
  204. P( local.B, local.C, local.D, local.A, 10, 23, 0xBEBFBC70 );
  205. P( local.A, local.B, local.C, local.D, 13, 4, 0x289B7EC6 );
  206. P( local.D, local.A, local.B, local.C, 0, 11, 0xEAA127FA );
  207. P( local.C, local.D, local.A, local.B, 3, 16, 0xD4EF3085 );
  208. P( local.B, local.C, local.D, local.A, 6, 23, 0x04881D05 );
  209. P( local.A, local.B, local.C, local.D, 9, 4, 0xD9D4D039 );
  210. P( local.D, local.A, local.B, local.C, 12, 11, 0xE6DB99E5 );
  211. P( local.C, local.D, local.A, local.B, 15, 16, 0x1FA27CF8 );
  212. P( local.B, local.C, local.D, local.A, 2, 23, 0xC4AC5665 );
  213. #undef F
  214. #define F(x,y,z) ((y) ^ ((x) | ~(z)))
  215. P( local.A, local.B, local.C, local.D, 0, 6, 0xF4292244 );
  216. P( local.D, local.A, local.B, local.C, 7, 10, 0x432AFF97 );
  217. P( local.C, local.D, local.A, local.B, 14, 15, 0xAB9423A7 );
  218. P( local.B, local.C, local.D, local.A, 5, 21, 0xFC93A039 );
  219. P( local.A, local.B, local.C, local.D, 12, 6, 0x655B59C3 );
  220. P( local.D, local.A, local.B, local.C, 3, 10, 0x8F0CCC92 );
  221. P( local.C, local.D, local.A, local.B, 10, 15, 0xFFEFF47D );
  222. P( local.B, local.C, local.D, local.A, 1, 21, 0x85845DD1 );
  223. P( local.A, local.B, local.C, local.D, 8, 6, 0x6FA87E4F );
  224. P( local.D, local.A, local.B, local.C, 15, 10, 0xFE2CE6E0 );
  225. P( local.C, local.D, local.A, local.B, 6, 15, 0xA3014314 );
  226. P( local.B, local.C, local.D, local.A, 13, 21, 0x4E0811A1 );
  227. P( local.A, local.B, local.C, local.D, 4, 6, 0xF7537E82 );
  228. P( local.D, local.A, local.B, local.C, 11, 10, 0xBD3AF235 );
  229. P( local.C, local.D, local.A, local.B, 2, 15, 0x2AD7D2BB );
  230. P( local.B, local.C, local.D, local.A, 9, 21, 0xEB86D391 );
  231. #undef F
  232. ctx->state[0] += local.A;
  233. ctx->state[1] += local.B;
  234. ctx->state[2] += local.C;
  235. ctx->state[3] += local.D;
  236. /* Zeroise variables to clear sensitive data from memory. */
  237. mbedtls_platform_zeroize( &local, sizeof( local ) );
  238. return( 0 );
  239. }
  240. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  241. void mbedtls_md5_process( mbedtls_md5_context *ctx,
  242. const unsigned char data[64] )
  243. {
  244. mbedtls_internal_md5_process( ctx, data );
  245. }
  246. #endif
  247. #endif /* !MBEDTLS_MD5_PROCESS_ALT */
  248. /*
  249. * MD5 process buffer
  250. */
  251. int mbedtls_md5_update_ret( mbedtls_md5_context *ctx,
  252. const unsigned char *input,
  253. size_t ilen )
  254. {
  255. int ret;
  256. size_t fill;
  257. uint32_t left;
  258. if( ilen == 0 )
  259. return( 0 );
  260. left = ctx->total[0] & 0x3F;
  261. fill = 64 - left;
  262. ctx->total[0] += (uint32_t) ilen;
  263. ctx->total[0] &= 0xFFFFFFFF;
  264. if( ctx->total[0] < (uint32_t) ilen )
  265. ctx->total[1]++;
  266. if( left && ilen >= fill )
  267. {
  268. memcpy( (void *) (ctx->buffer + left), input, fill );
  269. if( ( ret = mbedtls_internal_md5_process( ctx, ctx->buffer ) ) != 0 )
  270. return( ret );
  271. input += fill;
  272. ilen -= fill;
  273. left = 0;
  274. }
  275. while( ilen >= 64 )
  276. {
  277. if( ( ret = mbedtls_internal_md5_process( ctx, input ) ) != 0 )
  278. return( ret );
  279. input += 64;
  280. ilen -= 64;
  281. }
  282. if( ilen > 0 )
  283. {
  284. memcpy( (void *) (ctx->buffer + left), input, ilen );
  285. }
  286. return( 0 );
  287. }
  288. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  289. void mbedtls_md5_update( mbedtls_md5_context *ctx,
  290. const unsigned char *input,
  291. size_t ilen )
  292. {
  293. mbedtls_md5_update_ret( ctx, input, ilen );
  294. }
  295. #endif
  296. /*
  297. * MD5 final digest
  298. */
  299. int mbedtls_md5_finish_ret( mbedtls_md5_context *ctx,
  300. unsigned char output[16] )
  301. {
  302. int ret;
  303. uint32_t used;
  304. uint32_t high, low;
  305. /*
  306. * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
  307. */
  308. used = ctx->total[0] & 0x3F;
  309. ctx->buffer[used++] = 0x80;
  310. if( used <= 56 )
  311. {
  312. /* Enough room for padding + length in current block */
  313. memset( ctx->buffer + used, 0, 56 - used );
  314. }
  315. else
  316. {
  317. /* We'll need an extra block */
  318. memset( ctx->buffer + used, 0, 64 - used );
  319. if( ( ret = mbedtls_internal_md5_process( ctx, ctx->buffer ) ) != 0 )
  320. return( ret );
  321. memset( ctx->buffer, 0, 56 );
  322. }
  323. /*
  324. * Add message length
  325. */
  326. high = ( ctx->total[0] >> 29 )
  327. | ( ctx->total[1] << 3 );
  328. low = ( ctx->total[0] << 3 );
  329. PUT_UINT32_LE( low, ctx->buffer, 56 );
  330. PUT_UINT32_LE( high, ctx->buffer, 60 );
  331. if( ( ret = mbedtls_internal_md5_process( ctx, ctx->buffer ) ) != 0 )
  332. return( ret );
  333. /*
  334. * Output final state
  335. */
  336. PUT_UINT32_LE( ctx->state[0], output, 0 );
  337. PUT_UINT32_LE( ctx->state[1], output, 4 );
  338. PUT_UINT32_LE( ctx->state[2], output, 8 );
  339. PUT_UINT32_LE( ctx->state[3], output, 12 );
  340. return( 0 );
  341. }
  342. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  343. void mbedtls_md5_finish( mbedtls_md5_context *ctx,
  344. unsigned char output[16] )
  345. {
  346. mbedtls_md5_finish_ret( ctx, output );
  347. }
  348. #endif
  349. #endif /* !MBEDTLS_MD5_ALT */
  350. /*
  351. * output = MD5( input buffer )
  352. */
  353. int mbedtls_md5_ret( const unsigned char *input,
  354. size_t ilen,
  355. unsigned char output[16] )
  356. {
  357. int ret;
  358. mbedtls_md5_context ctx;
  359. mbedtls_md5_init( &ctx );
  360. if( ( ret = mbedtls_md5_starts_ret( &ctx ) ) != 0 )
  361. goto exit;
  362. if( ( ret = mbedtls_md5_update_ret( &ctx, input, ilen ) ) != 0 )
  363. goto exit;
  364. if( ( ret = mbedtls_md5_finish_ret( &ctx, output ) ) != 0 )
  365. goto exit;
  366. exit:
  367. mbedtls_md5_free( &ctx );
  368. return( ret );
  369. }
  370. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  371. void mbedtls_md5( const unsigned char *input,
  372. size_t ilen,
  373. unsigned char output[16] )
  374. {
  375. mbedtls_md5_ret( input, ilen, output );
  376. }
  377. #endif
  378. #if defined(MBEDTLS_SELF_TEST)
  379. /*
  380. * RFC 1321 test vectors
  381. */
  382. static const unsigned char md5_test_buf[7][81] =
  383. {
  384. { "" },
  385. { "a" },
  386. { "abc" },
  387. { "message digest" },
  388. { "abcdefghijklmnopqrstuvwxyz" },
  389. { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" },
  390. { "12345678901234567890123456789012345678901234567890123456789012"
  391. "345678901234567890" }
  392. };
  393. static const size_t md5_test_buflen[7] =
  394. {
  395. 0, 1, 3, 14, 26, 62, 80
  396. };
  397. static const unsigned char md5_test_sum[7][16] =
  398. {
  399. { 0xD4, 0x1D, 0x8C, 0xD9, 0x8F, 0x00, 0xB2, 0x04,
  400. 0xE9, 0x80, 0x09, 0x98, 0xEC, 0xF8, 0x42, 0x7E },
  401. { 0x0C, 0xC1, 0x75, 0xB9, 0xC0, 0xF1, 0xB6, 0xA8,
  402. 0x31, 0xC3, 0x99, 0xE2, 0x69, 0x77, 0x26, 0x61 },
  403. { 0x90, 0x01, 0x50, 0x98, 0x3C, 0xD2, 0x4F, 0xB0,
  404. 0xD6, 0x96, 0x3F, 0x7D, 0x28, 0xE1, 0x7F, 0x72 },
  405. { 0xF9, 0x6B, 0x69, 0x7D, 0x7C, 0xB7, 0x93, 0x8D,
  406. 0x52, 0x5A, 0x2F, 0x31, 0xAA, 0xF1, 0x61, 0xD0 },
  407. { 0xC3, 0xFC, 0xD3, 0xD7, 0x61, 0x92, 0xE4, 0x00,
  408. 0x7D, 0xFB, 0x49, 0x6C, 0xCA, 0x67, 0xE1, 0x3B },
  409. { 0xD1, 0x74, 0xAB, 0x98, 0xD2, 0x77, 0xD9, 0xF5,
  410. 0xA5, 0x61, 0x1C, 0x2C, 0x9F, 0x41, 0x9D, 0x9F },
  411. { 0x57, 0xED, 0xF4, 0xA2, 0x2B, 0xE3, 0xC9, 0x55,
  412. 0xAC, 0x49, 0xDA, 0x2E, 0x21, 0x07, 0xB6, 0x7A }
  413. };
  414. /*
  415. * Checkup routine
  416. */
  417. int mbedtls_md5_self_test( int verbose )
  418. {
  419. int i, ret = 0;
  420. unsigned char md5sum[16];
  421. for( i = 0; i < 7; i++ )
  422. {
  423. if( verbose != 0 )
  424. mbedtls_printf( " MD5 test #%d: ", i + 1 );
  425. ret = mbedtls_md5_ret( md5_test_buf[i], md5_test_buflen[i], md5sum );
  426. if( ret != 0 )
  427. goto fail;
  428. if( memcmp( md5sum, md5_test_sum[i], 16 ) != 0 )
  429. {
  430. ret = 1;
  431. goto fail;
  432. }
  433. if( verbose != 0 )
  434. mbedtls_printf( "passed\n" );
  435. }
  436. if( verbose != 0 )
  437. mbedtls_printf( "\n" );
  438. return( 0 );
  439. fail:
  440. if( verbose != 0 )
  441. mbedtls_printf( "failed\n" );
  442. return( ret );
  443. }
  444. #endif /* MBEDTLS_SELF_TEST */
  445. #endif /* MBEDTLS_MD5_C */