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