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