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