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