| 1 | /* -*- Mode: C; c-file-style: "stroustrup" -*- */ |
| 2 | |
| 3 | /* |
| 4 | * Implementation of Federal Information Processing Standards Publication |
| 5 | * FIPS 180-1 (17 Apr 1995) which supersedes FIPS 180 (11 May 1993) |
| 6 | * |
| 7 | * Speed hack version optimised for speed (see also reference version) |
| 8 | * uses macros so you need to recompile, not just relink |
| 9 | * |
| 10 | * Adam Back <[email protected]> |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | /* define VERBOSE to get output as in fip180-1.txt */ |
| 15 | #if defined( VERBOSE ) |
| 16 | #include <stdio.h> |
| 17 | #endif |
| 18 | #include <string.h> |
| 19 | #include "sha1.h" |
| 20 | |
| 21 | static int swap_endian32( void*, size_t ); |
| 22 | |
| 23 | /* A run time endian test. |
| 24 | |
| 25 | little_endian is the broken one: 80x86s, VAXs |
| 26 | big_endian is: most unix machines, RISC chips, 68000, etc |
| 27 | |
| 28 | The endianess is stored in macros: |
| 29 | |
| 30 | little_endian |
| 31 | and big_endian |
| 32 | |
| 33 | These boolean values can be checked in your code in C expressions. |
| 34 | |
| 35 | They should NOT be tested with conditional macro statements (#ifdef |
| 36 | etc). |
| 37 | */ |
| 38 | |
| 39 | static const int endian_test = 1; |
| 40 | |
| 41 | #define little_endian ( *(char*)&endian_test == 1 ) |
| 42 | |
| 43 | #define big_endian ( ! little_endian ) |
| 44 | |
| 45 | #define make_big_endian32( data, len ) \ |
| 46 | ( little_endian ? swap_endian32( data, len ) : 0 ) |
| 47 | |
| 48 | #define make_little_endian32( data, len ) \ |
| 49 | ( little_endian ? 0 : swap_endian32( data, len ) ) |
| 50 | |
| 51 | #define make_local_endian32( data, len ) \ |
| 52 | ( little_endian ? swap_endian32( data, len ) : 0 ) |
| 53 | |
| 54 | #if defined( OPENSSL ) |
| 55 | |
| 56 | void SHA1_Xform( word32* iv, const byte* data ) { |
| 57 | SHA1_ctx c; |
| 58 | byte d[SHA1_INPUT_BYTES]; |
| 59 | |
| 60 | c.h0=iv[0]; c.h1=iv[1]; c.h2=iv[2]; c.h3=iv[3]; c.h4=iv[4]; |
| 61 | |
| 62 | /* openSSL SHA1_Transform is in data order, trying to be helpful */ |
| 63 | /* #undef SHA1_Transform */ |
| 64 | /* SHA1_Transform( &c, data ); */ |
| 65 | |
| 66 | /* but they offer a host order version */ |
| 67 | /* but they don't export it :-( */ |
| 68 | /* sha1_block_asm_host_order( &c, data ); */ |
| 69 | |
| 70 | /* plan C, copy & convert the data on input */ |
| 71 | #undef SHA1_Transform |
| 72 | if ( little_endian ) { |
| 73 | memcpy( d, data, SHA1_INPUT_BYTES ); |
| 74 | make_local_endian32( d, SHA1_INPUT_WORDS ); |
| 75 | SHA1_Transform( &c, d ); |
| 76 | } else { /* not necessary on big endian */ |
| 77 | SHA1_Transform( &c, data ); |
| 78 | } |
| 79 | iv[0]=c.h0; iv[1]=c.h1; iv[2]=c.h2; iv[3]=c.h3; iv[4]=c.h4; |
| 80 | } |
| 81 | |
| 82 | #else |
| 83 | |
| 84 | #define min( x, y ) ( ( x ) < ( y ) ? ( x ) : ( y ) ) |
| 85 | |
| 86 | /********************* function used for rounds 0..19 ***********/ |
| 87 | |
| 88 | /* #define F1( B, C, D ) ( ( (B) & (C) ) | ( ~(B) & (D) ) ) */ |
| 89 | |
| 90 | /* equivalent, one less operation: */ |
| 91 | #define F1( B, C, D ) ( (D) ^ ( (B) & ( (C) ^ (D) ) ) ) |
| 92 | |
| 93 | |
| 94 | /********************* function used for rounds 20..39 ***********/ |
| 95 | |
| 96 | #define F2( B, C, D ) ( (B) ^ (C) ^ (D) ) |
| 97 | |
| 98 | /********************* function used for rounds 40..59 ***********/ |
| 99 | |
| 100 | /* #define F3( B, C, D ) ( (B) & (C) ) | ( (C) & (D) ) | ( (C) & (D) ) */ |
| 101 | |
| 102 | /* equivalent, one less operation */ |
| 103 | |
| 104 | #define F3( B, C, D ) ( ( (B) & ( (C) | (D) )) | ( (C) & (D) ) ) |
| 105 | |
| 106 | /********************* function used for rounds 60..79 ***********/ |
| 107 | |
| 108 | #define F4( B, C, D ) ( (B) ^ (C) ^ (D) ) |
| 109 | |
| 110 | #define K1 0x5A827999 /* constant used for rounds 0..19 */ |
| 111 | #define K2 0x6ED9EBA1 /* constant used for rounds 20..39 */ |
| 112 | #define K3 0x8F1BBCDC /* constant used for rounds 40..59 */ |
| 113 | #define K4 0xCA62C1D6 /* constant used for rounds 60..79 */ |
| 114 | |
| 115 | /* magic constants */ |
| 116 | |
| 117 | #define H0 0x67452301 |
| 118 | #define H1 0xEFCDAB89 |
| 119 | #define H2 0x98BADCFE |
| 120 | #define H3 0x10325476 |
| 121 | #define H4 0xC3D2E1F0 |
| 122 | |
| 123 | word32 SHA1_IV[ 5 ] = { H0, H1, H2, H3, H4 }; |
| 124 | |
| 125 | /* rotate X n bits left ( X <<< n ) */ |
| 126 | |
| 127 | #define S(n, X) ( ( (X) << (n) ) | ( (X) >> ( 32 - (n) ) ) ) |
| 128 | |
| 129 | #if defined( word64 ) |
| 130 | #define SHA1_zero_bitcount( ctx ) \ |
| 131 | (ctx)->bits = 0; |
| 132 | #else |
| 133 | #define SHA1_zero_bitcount( ctx ) \ |
| 134 | (ctx)->lbits = 0; \ |
| 135 | (ctx)->hbits = 0; |
| 136 | #endif |
| 137 | |
| 138 | void SHA1_Init( SHA1_ctx* ctx ) |
| 139 | { |
| 140 | SHA1_zero_bitcount( ctx ); |
| 141 | memcpy( ctx->H, SHA1_IV, SHA1_DIGEST_BYTES ); |
| 142 | } |
| 143 | |
| 144 | /* this is only used if you want to modify the IV */ |
| 145 | /* ignore this function for purposes of the standard */ |
| 146 | |
| 147 | void SHA1_Init_With_IV( SHA1_ctx* ctx, |
| 148 | const byte user_IV[ SHA1_DIGEST_BYTES ] ) |
| 149 | { |
| 150 | SHA1_zero_bitcount( ctx ); |
| 151 | memcpy( ctx->H, user_IV, SHA1_DIGEST_BYTES ); |
| 152 | make_local_endian32( ctx->H, SHA1_DIGEST_WORDS ); |
| 153 | } |
| 154 | |
| 155 | void SHA1_Transform( word32 H[ SHA1_DIGEST_WORDS ], |
| 156 | const byte M[ SHA1_INPUT_BYTES ] ) |
| 157 | { |
| 158 | #ifdef COMPACT |
| 159 | int t = 0 ; |
| 160 | #endif |
| 161 | word32 A = H[ 0 ]; |
| 162 | word32 B = H[ 1 ]; |
| 163 | word32 C = H[ 2 ]; |
| 164 | word32 D = H[ 3 ]; |
| 165 | word32 E = H[ 4 ]; |
| 166 | #if !defined( COMPACT ) |
| 167 | word32 W[ 16 ] = {0}; |
| 168 | #else |
| 169 | word32 W[ 80 ] = {0}; |
| 170 | #endif |
| 171 | |
| 172 | memcpy( W, M, SHA1_INPUT_BYTES ); |
| 173 | |
| 174 | /* Use method B from FIPS-180 (see fip-180.txt) where the use of |
| 175 | temporary array W of 80 word32s is avoided by working in a circular |
| 176 | buffer of size 16 word32s. |
| 177 | |
| 178 | (Chromatix: this is unreasonably slow on x86 due to register |
| 179 | pressure - going back to method A) |
| 180 | */ |
| 181 | |
| 182 | /********************* define some macros *********************/ |
| 183 | |
| 184 | /* Wc = access W as 16 word circular buffer */ |
| 185 | |
| 186 | #if !defined( COMPACT ) |
| 187 | #define Wc( t ) ( W[ (t) & 0x0F ] ) |
| 188 | #else |
| 189 | #define Wc( t ) ( W[ (t) ] ) |
| 190 | #endif |
| 191 | |
| 192 | /* Calculate access to W array on the fly for entries 16 .. 79 */ |
| 193 | |
| 194 | #if !defined( COMPACT ) |
| 195 | #define Wf( t ) \ |
| 196 | ( Wc( t ) = S( 1, Wc( t ) ^ Wc( t - 14 ) ^ Wc( t - 8 ) ^ Wc( t - 3 ) ) ) |
| 197 | #else |
| 198 | #define Wf( t ) \ |
| 199 | ( Wc( t ) = S( 1, Wc( t - 16 ) ^ Wc( t - 14 ) ^ Wc( t - 8 ) ^ Wc( t - 3 ) ) ) |
| 200 | #endif |
| 201 | |
| 202 | /* Calculate access to W virtual array calculating access to W on the fly */ |
| 203 | |
| 204 | #if !defined( COMPACT ) |
| 205 | #define Wfly( t ) ( (t) < 16 ? Wc( (t) ) : Wf( (t) ) ) |
| 206 | #else |
| 207 | #define Wfly( t ) ( Wc( (t) ) ) |
| 208 | #endif |
| 209 | |
| 210 | #if defined( VERBOSE ) |
| 211 | #define REPORT( t, A, B, C, D, E ) \ |
| 212 | fprintf( stderr, "t = %2d: %08X %08X %08X %08X %08X\n",\ |
| 213 | t, A, B, C, D, E ); |
| 214 | #else |
| 215 | #define REPORT( t, A, B, C, D, E ) |
| 216 | #endif |
| 217 | |
| 218 | #define ROUND( t, A, B, C, D, E, Func, K ) \ |
| 219 | E += S( 5, A ) + Func( B, C, D ) + Wfly( t ) + K;\ |
| 220 | B = S( 30, B ); REPORT( t, E, A, B, C, D ) |
| 221 | |
| 222 | /* Remove rotatation E' = D; D' = C; C' = B; B' = A; A' = E; by |
| 223 | completely unrolling and rotating the arguments to the macro ROUND |
| 224 | manually so the rotation is compiled in. |
| 225 | */ |
| 226 | |
| 227 | #define ROUND5( t, Func, K ) \ |
| 228 | ROUND( t + 0, A, B, C, D, E, Func, K );\ |
| 229 | ROUND( t + 1, E, A, B, C, D, Func, K );\ |
| 230 | ROUND( t + 2, D, E, A, B, C, Func, K );\ |
| 231 | ROUND( t + 3, C, D, E, A, B, Func, K );\ |
| 232 | ROUND( t + 4, B, C, D, E, A, Func, K ) |
| 233 | |
| 234 | #define ROUND20( t, Func, K )\ |
| 235 | ROUND5( t + 0, Func, K );\ |
| 236 | ROUND5( t + 5, Func, K );\ |
| 237 | ROUND5( t + 10, Func, K );\ |
| 238 | ROUND5( t + 15, Func, K ) |
| 239 | |
| 240 | /********************* use the macros *********************/ |
| 241 | |
| 242 | #if defined( VERBOSE ) && !defined( COMPACT ) |
| 243 | for ( t = 0; t < 16; t++ ) { |
| 244 | fprintf( stderr, "W[%2d] = %08x\n", t, W[ t ] ); |
| 245 | } |
| 246 | fprintf( stderr, |
| 247 | " A B C D E\n\n" ); |
| 248 | #endif |
| 249 | |
| 250 | #if defined( COMPACT ) |
| 251 | /* initialise W buffer */ |
| 252 | for ( t = 16; t < 80; t++ ) { |
| 253 | Wf( t ); |
| 254 | } |
| 255 | #endif |
| 256 | |
| 257 | /* rounds 0..19 */ |
| 258 | |
| 259 | ROUND20( 0, F1, K1 ); |
| 260 | |
| 261 | /* rounds 21..39 */ |
| 262 | |
| 263 | ROUND20( 20, F2, K2 ); |
| 264 | |
| 265 | /* rounds 40..59 */ |
| 266 | |
| 267 | ROUND20( 40, F3, K3 ); |
| 268 | |
| 269 | /* rounds 60..79 */ |
| 270 | |
| 271 | ROUND20( 60, F4, K4 ); |
| 272 | |
| 273 | H[ 0 ] += A; |
| 274 | H[ 1 ] += B; |
| 275 | H[ 2 ] += C; |
| 276 | H[ 3 ] += D; |
| 277 | H[ 4 ] += E; |
| 278 | } |
| 279 | |
| 280 | void SHA1_Update( SHA1_ctx* ctx, const void* pdata, size_t data_len ) |
| 281 | { |
| 282 | const byte* data = (const byte*)pdata; |
| 283 | unsigned use = 0 ; |
| 284 | unsigned mlen = 0 ; |
| 285 | #if !defined( word64 ) |
| 286 | word32 low_bits = 0 ; |
| 287 | #endif |
| 288 | |
| 289 | /* convert data_len to bits and add to the 64-bit bit count */ |
| 290 | |
| 291 | #if defined( word64 ) |
| 292 | mlen = (unsigned)( ( ctx->bits >> 3 ) % SHA1_INPUT_BYTES ); |
| 293 | ctx->bits += ( (word64) data_len ) << 3; |
| 294 | #else |
| 295 | mlen = (unsigned)( ( ctx->lbits >> 3 ) % SHA1_INPUT_BYTES ); |
| 296 | ctx->hbits += data_len >> 29; /* simulate 64 bit addition */ |
| 297 | low_bits = data_len << 3; |
| 298 | ctx->lbits += low_bits; |
| 299 | if ( ctx->lbits < low_bits ) { ctx->hbits++; } |
| 300 | #endif |
| 301 | |
| 302 | /* deal with first block */ |
| 303 | |
| 304 | use = (unsigned)min( (size_t)(SHA1_INPUT_BYTES - mlen), data_len ); |
| 305 | memcpy( ctx->M + mlen, data, use ); |
| 306 | mlen += use; |
| 307 | data_len -= use; |
| 308 | data += use; |
| 309 | |
| 310 | while ( mlen == SHA1_INPUT_BYTES ) { |
| 311 | make_big_endian32( (word32*)ctx->M, SHA1_INPUT_WORDS ); |
| 312 | SHA1_Transform( ctx->H, ctx->M ); |
| 313 | use = (unsigned)min( SHA1_INPUT_BYTES, data_len ); |
| 314 | memcpy( ctx->M, data, use ); |
| 315 | mlen = use; |
| 316 | data_len -= use; |
| 317 | data += use; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | void SHA1_Final( SHA1_ctx* ctx, byte digest[ SHA1_DIGEST_BYTES ] ) |
| 322 | { |
| 323 | unsigned mlen = 0 ; |
| 324 | unsigned padding = 0 ; |
| 325 | #if defined( word64 ) |
| 326 | word64 temp = 0 ; |
| 327 | #endif |
| 328 | |
| 329 | #if defined( word64 ) |
| 330 | mlen = (unsigned)(( ctx->bits >> 3 ) % SHA1_INPUT_BYTES); |
| 331 | #else |
| 332 | mlen = (unsigned)(( ctx->lbits >> 3 ) % SHA1_INPUT_BYTES); |
| 333 | #endif |
| 334 | |
| 335 | ctx->M[ mlen ] = 0x80; mlen++; /* append a 1 bit */ |
| 336 | padding = SHA1_INPUT_BYTES - mlen; |
| 337 | |
| 338 | #define BIT_COUNT_WORDS 2 |
| 339 | #define BIT_COUNT_BYTES ( BIT_COUNT_WORDS * sizeof( word32 ) ) |
| 340 | |
| 341 | if ( (unsigned)padding >= BIT_COUNT_BYTES ) { |
| 342 | memset( ctx->M + mlen, 0x00, padding - BIT_COUNT_BYTES ); |
| 343 | make_big_endian32( ctx->M, SHA1_INPUT_WORDS - BIT_COUNT_WORDS ); |
| 344 | } else { |
| 345 | memset( ctx->M + mlen, 0x00, SHA1_INPUT_BYTES - mlen ); |
| 346 | make_big_endian32( ctx->M, SHA1_INPUT_WORDS ); |
| 347 | SHA1_Transform( ctx->H, ctx->M ); |
| 348 | memset( ctx->M, 0x00, SHA1_INPUT_BYTES - BIT_COUNT_BYTES ); |
| 349 | } |
| 350 | |
| 351 | #if defined( word64 ) |
| 352 | if ( little_endian ) { |
| 353 | temp = ( ctx->bits << 32 | ctx->bits >> 32 ); |
| 354 | } else { |
| 355 | temp = ctx->bits; |
| 356 | } |
| 357 | memcpy( ctx->M + SHA1_INPUT_BYTES - BIT_COUNT_BYTES, &temp, |
| 358 | BIT_COUNT_BYTES ); |
| 359 | #else |
| 360 | memcpy( ctx->M + SHA1_INPUT_BYTES - BIT_COUNT_BYTES, &(ctx->hbits), |
| 361 | BIT_COUNT_BYTES ); |
| 362 | #endif |
| 363 | SHA1_Transform( ctx->H, ctx->M ); |
| 364 | |
| 365 | memcpy( digest, ctx->H, SHA1_DIGEST_BYTES ); |
| 366 | make_big_endian32( digest, SHA1_DIGEST_WORDS ); |
| 367 | } |
| 368 | |
| 369 | #endif |
| 370 | |
| 371 | static int swap_endian32( void* data, size_t len ) |
| 372 | { |
| 373 | word32 tmp32 = 0 ; |
| 374 | byte* tmp32_as_bytes = (byte*) &tmp32; |
| 375 | word32* data_as_word32s = (word32*) data; |
| 376 | byte* data_as_bytes = NULL ; |
| 377 | size_t i = 0 ; |
| 378 | |
| 379 | for ( i = 0; i < len; i++ ) { |
| 380 | tmp32 = data_as_word32s[ i ]; |
| 381 | data_as_bytes = (byte*) &( data_as_word32s[ i ] ); |
| 382 | |
| 383 | data_as_bytes[ 0 ] = tmp32_as_bytes[ 3 ]; |
| 384 | data_as_bytes[ 1 ] = tmp32_as_bytes[ 2 ]; |
| 385 | data_as_bytes[ 2 ] = tmp32_as_bytes[ 1 ]; |
| 386 | data_as_bytes[ 3 ] = tmp32_as_bytes[ 0 ]; |
| 387 | } |
| 388 | return 1; |
| 389 | } |