1 /* 2 * FIPS-180-1 compliant SHA-1 implementation 3 * 4 * Copyright The Mbed TLS Contributors 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 /* 20 * The SHA-1 standard was published by NIST in 1993. 21 * 22 * http://www.itl.nist.gov/fipspubs/fip180-1.htm 23 */ 24 25 #include "common.h" 26 27 #if defined(MBEDTLS_SHA1_C) 28 29 #include "mbedtls/sha1.h" 30 #include "mbedtls/platform_util.h" 31 #include "mbedtls/error.h" 32 33 #include <string.h> 34 35 #if defined(MBEDTLS_SELF_TEST) 36 #if defined(MBEDTLS_PLATFORM_C) 37 #include "mbedtls/platform.h" 38 #else 39 #include <stdio.h> 40 #define mbedtls_printf printf 41 #endif /* MBEDTLS_PLATFORM_C */ 42 #endif /* MBEDTLS_SELF_TEST */ 43 44 #define SHA1_VALIDATE_RET(cond) \ 45 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_SHA1_BAD_INPUT_DATA ) 46 47 #define SHA1_VALIDATE(cond) MBEDTLS_INTERNAL_VALIDATE( cond ) 48 49 #if !defined(MBEDTLS_SHA1_ALT) 50 51 /* 52 * 32-bit integer manipulation macros (big endian) 53 */ 54 #ifndef GET_UINT32_BE 55 #define GET_UINT32_BE(n,b,i) \ 56 { \ 57 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ 58 | ( (uint32_t) (b)[(i) + 1] << 16 ) \ 59 | ( (uint32_t) (b)[(i) + 2] << 8 ) \ 60 | ( (uint32_t) (b)[(i) + 3] ); \ 61 } 62 #endif 63 64 #ifndef PUT_UINT32_BE 65 #define PUT_UINT32_BE(n,b,i) \ 66 { \ 67 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ 68 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ 69 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ 70 (b)[(i) + 3] = (unsigned char) ( (n) ); \ 71 } 72 #endif 73 74 void mbedtls_sha1_init( mbedtls_sha1_context *ctx ) 75 { 76 SHA1_VALIDATE( ctx != NULL ); 77 78 memset( ctx, 0, sizeof( mbedtls_sha1_context ) ); 79 } 80 81 void mbedtls_sha1_free( mbedtls_sha1_context *ctx ) 82 { 83 if( ctx == NULL ) 84 return; 85 86 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_sha1_context ) ); 87 } 88 89 void mbedtls_sha1_clone( mbedtls_sha1_context *dst, 90 const mbedtls_sha1_context *src ) 91 { 92 SHA1_VALIDATE( dst != NULL ); 93 SHA1_VALIDATE( src != NULL ); 94 95 *dst = *src; 96 } 97 98 /* 99 * SHA-1 context setup 100 */ 101 int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx ) 102 { 103 SHA1_VALIDATE_RET( ctx != NULL ); 104 105 ctx->total[0] = 0; 106 ctx->total[1] = 0; 107 108 ctx->state[0] = 0x67452301; 109 ctx->state[1] = 0xEFCDAB89; 110 ctx->state[2] = 0x98BADCFE; 111 ctx->state[3] = 0x10325476; 112 ctx->state[4] = 0xC3D2E1F0; 113 114 return( 0 ); 115 } 116 117 #if !defined(MBEDTLS_DEPRECATED_REMOVED) 118 void mbedtls_sha1_starts( mbedtls_sha1_context *ctx ) 119 { 120 mbedtls_sha1_starts_ret( ctx ); 121 } 122 #endif 123 124 #if !defined(MBEDTLS_SHA1_PROCESS_ALT) 125 int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx, 126 const unsigned char data[64] ) 127 { 128 struct 129 { 130 uint32_t temp, W[16], A, B, C, D, E; 131 } local; 132 133 SHA1_VALIDATE_RET( ctx != NULL ); 134 SHA1_VALIDATE_RET( (const unsigned char *)data != NULL ); 135 136 GET_UINT32_BE( local.W[ 0], data, 0 ); 137 GET_UINT32_BE( local.W[ 1], data, 4 ); 138 GET_UINT32_BE( local.W[ 2], data, 8 ); 139 GET_UINT32_BE( local.W[ 3], data, 12 ); 140 GET_UINT32_BE( local.W[ 4], data, 16 ); 141 GET_UINT32_BE( local.W[ 5], data, 20 ); 142 GET_UINT32_BE( local.W[ 6], data, 24 ); 143 GET_UINT32_BE( local.W[ 7], data, 28 ); 144 GET_UINT32_BE( local.W[ 8], data, 32 ); 145 GET_UINT32_BE( local.W[ 9], data, 36 ); 146 GET_UINT32_BE( local.W[10], data, 40 ); 147 GET_UINT32_BE( local.W[11], data, 44 ); 148 GET_UINT32_BE( local.W[12], data, 48 ); 149 GET_UINT32_BE( local.W[13], data, 52 ); 150 GET_UINT32_BE( local.W[14], data, 56 ); 151 GET_UINT32_BE( local.W[15], data, 60 ); 152 153 #define S(x,n) (((x) << (n)) | (((x) & 0xFFFFFFFF) >> (32 - (n)))) 154 155 #define R(t) \ 156 ( \ 157 local.temp = local.W[( (t) - 3 ) & 0x0F] ^ \ 158 local.W[( (t) - 8 ) & 0x0F] ^ \ 159 local.W[( (t) - 14 ) & 0x0F] ^ \ 160 local.W[ (t) & 0x0F], \ 161 ( local.W[(t) & 0x0F] = S(local.temp,1) ) \ 162 ) 163 164 #define P(a,b,c,d,e,x) \ 165 do \ 166 { \ 167 (e) += S((a),5) + F((b),(c),(d)) + K + (x); \ 168 (b) = S((b),30); \ 169 } while( 0 ) 170 171 local.A = ctx->state[0]; 172 local.B = ctx->state[1]; 173 local.C = ctx->state[2]; 174 local.D = ctx->state[3]; 175 local.E = ctx->state[4]; 176 177 #define F(x,y,z) ((z) ^ ((x) & ((y) ^ (z)))) 178 #define K 0x5A827999 179 180 P( local.A, local.B, local.C, local.D, local.E, local.W[0] ); 181 P( local.E, local.A, local.B, local.C, local.D, local.W[1] ); 182 P( local.D, local.E, local.A, local.B, local.C, local.W[2] ); 183 P( local.C, local.D, local.E, local.A, local.B, local.W[3] ); 184 P( local.B, local.C, local.D, local.E, local.A, local.W[4] ); 185 P( local.A, local.B, local.C, local.D, local.E, local.W[5] ); 186 P( local.E, local.A, local.B, local.C, local.D, local.W[6] ); 187 P( local.D, local.E, local.A, local.B, local.C, local.W[7] ); 188 P( local.C, local.D, local.E, local.A, local.B, local.W[8] ); 189 P( local.B, local.C, local.D, local.E, local.A, local.W[9] ); 190 P( local.A, local.B, local.C, local.D, local.E, local.W[10] ); 191 P( local.E, local.A, local.B, local.C, local.D, local.W[11] ); 192 P( local.D, local.E, local.A, local.B, local.C, local.W[12] ); 193 P( local.C, local.D, local.E, local.A, local.B, local.W[13] ); 194 P( local.B, local.C, local.D, local.E, local.A, local.W[14] ); 195 P( local.A, local.B, local.C, local.D, local.E, local.W[15] ); 196 P( local.E, local.A, local.B, local.C, local.D, R(16) ); 197 P( local.D, local.E, local.A, local.B, local.C, R(17) ); 198 P( local.C, local.D, local.E, local.A, local.B, R(18) ); 199 P( local.B, local.C, local.D, local.E, local.A, R(19) ); 200 201 #undef K 202 #undef F 203 204 #define F(x,y,z) ((x) ^ (y) ^ (z)) 205 #define K 0x6ED9EBA1 206 207 P( local.A, local.B, local.C, local.D, local.E, R(20) ); 208 P( local.E, local.A, local.B, local.C, local.D, R(21) ); 209 P( local.D, local.E, local.A, local.B, local.C, R(22) ); 210 P( local.C, local.D, local.E, local.A, local.B, R(23) ); 211 P( local.B, local.C, local.D, local.E, local.A, R(24) ); 212 P( local.A, local.B, local.C, local.D, local.E, R(25) ); 213 P( local.E, local.A, local.B, local.C, local.D, R(26) ); 214 P( local.D, local.E, local.A, local.B, local.C, R(27) ); 215 P( local.C, local.D, local.E, local.A, local.B, R(28) ); 216 P( local.B, local.C, local.D, local.E, local.A, R(29) ); 217 P( local.A, local.B, local.C, local.D, local.E, R(30) ); 218 P( local.E, local.A, local.B, local.C, local.D, R(31) ); 219 P( local.D, local.E, local.A, local.B, local.C, R(32) ); 220 P( local.C, local.D, local.E, local.A, local.B, R(33) ); 221 P( local.B, local.C, local.D, local.E, local.A, R(34) ); 222 P( local.A, local.B, local.C, local.D, local.E, R(35) ); 223 P( local.E, local.A, local.B, local.C, local.D, R(36) ); 224 P( local.D, local.E, local.A, local.B, local.C, R(37) ); 225 P( local.C, local.D, local.E, local.A, local.B, R(38) ); 226 P( local.B, local.C, local.D, local.E, local.A, R(39) ); 227 228 #undef K 229 #undef F 230 231 #define F(x,y,z) (((x) & (y)) | ((z) & ((x) | (y)))) 232 #define K 0x8F1BBCDC 233 234 P( local.A, local.B, local.C, local.D, local.E, R(40) ); 235 P( local.E, local.A, local.B, local.C, local.D, R(41) ); 236 P( local.D, local.E, local.A, local.B, local.C, R(42) ); 237 P( local.C, local.D, local.E, local.A, local.B, R(43) ); 238 P( local.B, local.C, local.D, local.E, local.A, R(44) ); 239 P( local.A, local.B, local.C, local.D, local.E, R(45) ); 240 P( local.E, local.A, local.B, local.C, local.D, R(46) ); 241 P( local.D, local.E, local.A, local.B, local.C, R(47) ); 242 P( local.C, local.D, local.E, local.A, local.B, R(48) ); 243 P( local.B, local.C, local.D, local.E, local.A, R(49) ); 244 P( local.A, local.B, local.C, local.D, local.E, R(50) ); 245 P( local.E, local.A, local.B, local.C, local.D, R(51) ); 246 P( local.D, local.E, local.A, local.B, local.C, R(52) ); 247 P( local.C, local.D, local.E, local.A, local.B, R(53) ); 248 P( local.B, local.C, local.D, local.E, local.A, R(54) ); 249 P( local.A, local.B, local.C, local.D, local.E, R(55) ); 250 P( local.E, local.A, local.B, local.C, local.D, R(56) ); 251 P( local.D, local.E, local.A, local.B, local.C, R(57) ); 252 P( local.C, local.D, local.E, local.A, local.B, R(58) ); 253 P( local.B, local.C, local.D, local.E, local.A, R(59) ); 254 255 #undef K 256 #undef F 257 258 #define F(x,y,z) ((x) ^ (y) ^ (z)) 259 #define K 0xCA62C1D6 260 261 P( local.A, local.B, local.C, local.D, local.E, R(60) ); 262 P( local.E, local.A, local.B, local.C, local.D, R(61) ); 263 P( local.D, local.E, local.A, local.B, local.C, R(62) ); 264 P( local.C, local.D, local.E, local.A, local.B, R(63) ); 265 P( local.B, local.C, local.D, local.E, local.A, R(64) ); 266 P( local.A, local.B, local.C, local.D, local.E, R(65) ); 267 P( local.E, local.A, local.B, local.C, local.D, R(66) ); 268 P( local.D, local.E, local.A, local.B, local.C, R(67) ); 269 P( local.C, local.D, local.E, local.A, local.B, R(68) ); 270 P( local.B, local.C, local.D, local.E, local.A, R(69) ); 271 P( local.A, local.B, local.C, local.D, local.E, R(70) ); 272 P( local.E, local.A, local.B, local.C, local.D, R(71) ); 273 P( local.D, local.E, local.A, local.B, local.C, R(72) ); 274 P( local.C, local.D, local.E, local.A, local.B, R(73) ); 275 P( local.B, local.C, local.D, local.E, local.A, R(74) ); 276 P( local.A, local.B, local.C, local.D, local.E, R(75) ); 277 P( local.E, local.A, local.B, local.C, local.D, R(76) ); 278 P( local.D, local.E, local.A, local.B, local.C, R(77) ); 279 P( local.C, local.D, local.E, local.A, local.B, R(78) ); 280 P( local.B, local.C, local.D, local.E, local.A, R(79) ); 281 282 #undef K 283 #undef F 284 285 ctx->state[0] += local.A; 286 ctx->state[1] += local.B; 287 ctx->state[2] += local.C; 288 ctx->state[3] += local.D; 289 ctx->state[4] += local.E; 290 291 /* Zeroise buffers and variables to clear sensitive data from memory. */ 292 mbedtls_platform_zeroize( &local, sizeof( local ) ); 293 294 return( 0 ); 295 } 296 297 #if !defined(MBEDTLS_DEPRECATED_REMOVED) 298 void mbedtls_sha1_process( mbedtls_sha1_context *ctx, 299 const unsigned char data[64] ) 300 { 301 mbedtls_internal_sha1_process( ctx, data ); 302 } 303 #endif 304 #endif /* !MBEDTLS_SHA1_PROCESS_ALT */ 305 306 /* 307 * SHA-1 process buffer 308 */ 309 int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx, 310 const unsigned char *input, 311 size_t ilen ) 312 { 313 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 314 size_t fill; 315 uint32_t left; 316 317 SHA1_VALIDATE_RET( ctx != NULL ); 318 SHA1_VALIDATE_RET( ilen == 0 || input != NULL ); 319 320 if( ilen == 0 ) 321 return( 0 ); 322 323 left = ctx->total[0] & 0x3F; 324 fill = 64 - left; 325 326 ctx->total[0] += (uint32_t) ilen; 327 ctx->total[0] &= 0xFFFFFFFF; 328 329 if( ctx->total[0] < (uint32_t) ilen ) 330 ctx->total[1]++; 331 332 if( left && ilen >= fill ) 333 { 334 memcpy( (void *) (ctx->buffer + left), input, fill ); 335 336 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 ) 337 return( ret ); 338 339 input += fill; 340 ilen -= fill; 341 left = 0; 342 } 343 344 while( ilen >= 64 ) 345 { 346 if( ( ret = mbedtls_internal_sha1_process( ctx, input ) ) != 0 ) 347 return( ret ); 348 349 input += 64; 350 ilen -= 64; 351 } 352 353 if( ilen > 0 ) 354 memcpy( (void *) (ctx->buffer + left), input, ilen ); 355 356 return( 0 ); 357 } 358 359 #if !defined(MBEDTLS_DEPRECATED_REMOVED) 360 void mbedtls_sha1_update( mbedtls_sha1_context *ctx, 361 const unsigned char *input, 362 size_t ilen ) 363 { 364 mbedtls_sha1_update_ret( ctx, input, ilen ); 365 } 366 #endif 367 368 /* 369 * SHA-1 final digest 370 */ 371 int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx, 372 unsigned char output[20] ) 373 { 374 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 375 uint32_t used; 376 uint32_t high, low; 377 378 SHA1_VALIDATE_RET( ctx != NULL ); 379 SHA1_VALIDATE_RET( (unsigned char *)output != NULL ); 380 381 /* 382 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length 383 */ 384 used = ctx->total[0] & 0x3F; 385 386 ctx->buffer[used++] = 0x80; 387 388 if( used <= 56 ) 389 { 390 /* Enough room for padding + length in current block */ 391 memset( ctx->buffer + used, 0, 56 - used ); 392 } 393 else 394 { 395 /* We'll need an extra block */ 396 memset( ctx->buffer + used, 0, 64 - used ); 397 398 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 ) 399 return( ret ); 400 401 memset( ctx->buffer, 0, 56 ); 402 } 403 404 /* 405 * Add message length 406 */ 407 high = ( ctx->total[0] >> 29 ) 408 | ( ctx->total[1] << 3 ); 409 low = ( ctx->total[0] << 3 ); 410 411 PUT_UINT32_BE( high, ctx->buffer, 56 ); 412 PUT_UINT32_BE( low, ctx->buffer, 60 ); 413 414 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 ) 415 return( ret ); 416 417 /* 418 * Output final state 419 */ 420 PUT_UINT32_BE( ctx->state[0], output, 0 ); 421 PUT_UINT32_BE( ctx->state[1], output, 4 ); 422 PUT_UINT32_BE( ctx->state[2], output, 8 ); 423 PUT_UINT32_BE( ctx->state[3], output, 12 ); 424 PUT_UINT32_BE( ctx->state[4], output, 16 ); 425 426 return( 0 ); 427 } 428 429 #if !defined(MBEDTLS_DEPRECATED_REMOVED) 430 void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, 431 unsigned char output[20] ) 432 { 433 mbedtls_sha1_finish_ret( ctx, output ); 434 } 435 #endif 436 437 #endif /* !MBEDTLS_SHA1_ALT */ 438 439 /* 440 * output = SHA-1( input buffer ) 441 */ 442 int mbedtls_sha1_ret( const unsigned char *input, 443 size_t ilen, 444 unsigned char output[20] ) 445 { 446 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 447 mbedtls_sha1_context ctx; 448 449 SHA1_VALIDATE_RET( ilen == 0 || input != NULL ); 450 SHA1_VALIDATE_RET( (unsigned char *)output != NULL ); 451 452 mbedtls_sha1_init( &ctx ); 453 454 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 ) 455 goto exit; 456 457 if( ( ret = mbedtls_sha1_update_ret( &ctx, input, ilen ) ) != 0 ) 458 goto exit; 459 460 if( ( ret = mbedtls_sha1_finish_ret( &ctx, output ) ) != 0 ) 461 goto exit; 462 463 exit: 464 mbedtls_sha1_free( &ctx ); 465 466 return( ret ); 467 } 468 469 #if !defined(MBEDTLS_DEPRECATED_REMOVED) 470 void mbedtls_sha1( const unsigned char *input, 471 size_t ilen, 472 unsigned char output[20] ) 473 { 474 mbedtls_sha1_ret( input, ilen, output ); 475 } 476 #endif 477 478 #if defined(MBEDTLS_SELF_TEST) 479 /* 480 * FIPS-180-1 test vectors 481 */ 482 static const unsigned char sha1_test_buf[3][57] = 483 { 484 { "abc" }, 485 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" }, 486 { "" } 487 }; 488 489 static const size_t sha1_test_buflen[3] = 490 { 491 3, 56, 1000 492 }; 493 494 static const unsigned char sha1_test_sum[3][20] = 495 { 496 { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E, 497 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D }, 498 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE, 499 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 }, 500 { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E, 501 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F } 502 }; 503 504 /* 505 * Checkup routine 506 */ 507 int mbedtls_sha1_self_test( int verbose ) 508 { 509 int i, j, buflen, ret = 0; 510 unsigned char buf[1024]; 511 unsigned char sha1sum[20]; 512 mbedtls_sha1_context ctx; 513 514 mbedtls_sha1_init( &ctx ); 515 516 /* 517 * SHA-1 518 */ 519 for( i = 0; i < 3; i++ ) 520 { 521 if( verbose != 0 ) 522 mbedtls_printf( " SHA-1 test #%d: ", i + 1 ); 523 524 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 ) 525 goto fail; 526 527 if( i == 2 ) 528 { 529 memset( buf, 'a', buflen = 1000 ); 530 531 for( j = 0; j < 1000; j++ ) 532 { 533 ret = mbedtls_sha1_update_ret( &ctx, buf, buflen ); 534 if( ret != 0 ) 535 goto fail; 536 } 537 } 538 else 539 { 540 ret = mbedtls_sha1_update_ret( &ctx, sha1_test_buf[i], 541 sha1_test_buflen[i] ); 542 if( ret != 0 ) 543 goto fail; 544 } 545 546 if( ( ret = mbedtls_sha1_finish_ret( &ctx, sha1sum ) ) != 0 ) 547 goto fail; 548 549 if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 ) 550 { 551 ret = 1; 552 goto fail; 553 } 554 555 if( verbose != 0 ) 556 mbedtls_printf( "passed\n" ); 557 } 558 559 if( verbose != 0 ) 560 mbedtls_printf( "\n" ); 561 562 goto exit; 563 564 fail: 565 if( verbose != 0 ) 566 mbedtls_printf( "failed\n" ); 567 568 exit: 569 mbedtls_sha1_free( &ctx ); 570 571 return( ret ); 572 } 573 574 #endif /* MBEDTLS_SELF_TEST */ 575 576 #endif /* MBEDTLS_SHA1_C */ 577