1 /* 2 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 3 * Use is subject to license terms. 4 */ 5 6 #pragma ident "%Z%%M% %I% %E% SMI" 7 8 /* 9 * The basic framework for this code came from the reference 10 * implementation for MD5. That implementation is Copyright (C) 11 * 1991-2, RSA Data Security, Inc. Created 1991. All rights reserved. 12 * 13 * License to copy and use this software is granted provided that it 14 * is identified as the "RSA Data Security, Inc. MD5 Message-Digest 15 * Algorithm" in all material mentioning or referencing this software 16 * or this function. 17 * 18 * License is also granted to make and use derivative works provided 19 * that such works are identified as "derived from the RSA Data 20 * Security, Inc. MD5 Message-Digest Algorithm" in all material 21 * mentioning or referencing the derived work. 22 * 23 * RSA Data Security, Inc. makes no representations concerning either 24 * the merchantability of this software or the suitability of this 25 * software for any particular purpose. It is provided "as is" 26 * without express or implied warranty of any kind. 27 * 28 * These notices must be retained in any copies of any part of this 29 * documentation and/or software. 30 * 31 * NOTE: Cleaned-up and optimized, version of SHA1, based on the FIPS 180-1 32 * standard, available at http://www.itl.nist.gov/div897/pubs/fip180-1.htm 33 * Not as fast as one would like -- further optimizations are encouraged 34 * and appreciated. 35 */ 36 37 #include <sys/types.h> 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/sysmacros.h> 41 #include <sys/sha1.h> 42 #include <sys/sha1_consts.h> 43 44 #ifndef _KERNEL 45 #include <strings.h> 46 #include <stdlib.h> 47 #include <errno.h> 48 #include <sys/systeminfo.h> 49 #endif /* !_KERNEL */ 50 51 static void Encode(uint8_t *, const uint32_t *, size_t); 52 53 #if defined(__sparc) 54 55 #define SHA1_TRANSFORM(ctx, in) \ 56 SHA1Transform((ctx)->state[0], (ctx)->state[1], (ctx)->state[2], \ 57 (ctx)->state[3], (ctx)->state[4], (ctx), (in)) 58 59 static void SHA1Transform(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, 60 SHA1_CTX *, const uint8_t *); 61 62 #else 63 64 #define SHA1_TRANSFORM(ctx, in) SHA1Transform((ctx), (in)) 65 66 static void SHA1Transform(SHA1_CTX *, const uint8_t *); 67 68 #endif 69 70 71 static uint8_t PADDING[64] = { 0x80, /* all zeros */ }; 72 73 /* 74 * F, G, and H are the basic SHA1 functions. 75 */ 76 #define F(b, c, d) (((b) & (c)) | ((~b) & (d))) 77 #define G(b, c, d) ((b) ^ (c) ^ (d)) 78 #define H(b, c, d) (((b) & (c)) | (((b)|(c)) & (d))) 79 80 /* 81 * ROTATE_LEFT rotates x left n bits. 82 */ 83 84 #if defined(__GNUC__) && defined(_LP64) 85 static __inline__ uint64_t 86 ROTATE_LEFT(uint64_t value, uint32_t n) 87 { 88 uint32_t t32; 89 90 t32 = (uint32_t)value; 91 return ((t32 << n) | (t32 >> (32 - n))); 92 } 93 94 #else 95 96 #define ROTATE_LEFT(x, n) \ 97 (((x) << (n)) | ((x) >> ((sizeof (x) * NBBY)-(n)))) 98 99 #endif 100 101 #if defined(__GNUC__) && (defined(__i386) || defined(__amd64)) 102 103 #define HAVE_BSWAP 104 105 extern __inline__ uint32_t bswap(uint32_t value) 106 { 107 __asm__("bswap %0" : "+r" (value)); 108 return (value); 109 } 110 111 #endif 112 113 /* 114 * SHA1Init() 115 * 116 * purpose: initializes the sha1 context and begins and sha1 digest operation 117 * input: SHA1_CTX * : the context to initializes. 118 * output: void 119 */ 120 121 void 122 SHA1Init(SHA1_CTX *ctx) 123 { 124 ctx->count[0] = ctx->count[1] = 0; 125 126 /* 127 * load magic initialization constants. Tell lint 128 * that these constants are unsigned by using U. 129 */ 130 131 ctx->state[0] = 0x67452301U; 132 ctx->state[1] = 0xefcdab89U; 133 ctx->state[2] = 0x98badcfeU; 134 ctx->state[3] = 0x10325476U; 135 ctx->state[4] = 0xc3d2e1f0U; 136 } 137 138 #ifdef VIS_SHA1 139 #ifdef _KERNEL 140 141 #include <sys/regset.h> 142 #include <sys/vis.h> 143 #include <sys/fpu/fpusystm.h> 144 145 /* the alignment for block stores to save fp registers */ 146 #define VIS_ALIGN (64) 147 148 extern int sha1_savefp(kfpu_t *, int); 149 extern void sha1_restorefp(kfpu_t *); 150 151 uint32_t vis_sha1_svfp_threshold = 128; 152 153 #endif /* _KERNEL */ 154 155 /* 156 * VIS SHA-1 consts. 157 */ 158 static uint64_t VIS[] = { 159 0x8000000080000000ULL, 160 0x0002000200020002ULL, 161 0x5a8279996ed9eba1ULL, 162 0x8f1bbcdcca62c1d6ULL, 163 0x012389ab456789abULL}; 164 165 extern void SHA1TransformVIS(uint64_t *, uint32_t *, uint32_t *, uint64_t *); 166 167 168 /* 169 * SHA1Update() 170 * 171 * purpose: continues an sha1 digest operation, using the message block 172 * to update the context. 173 * input: SHA1_CTX * : the context to update 174 * void * : the message block 175 * size_t : the length of the message block in bytes 176 * output: void 177 */ 178 179 void 180 SHA1Update(SHA1_CTX *ctx, const void *inptr, size_t input_len) 181 { 182 uint32_t i, buf_index, buf_len; 183 uint64_t X0[40], input64[8]; 184 const uint8_t *input = inptr; 185 #ifdef _KERNEL 186 int usevis = 0; 187 #else 188 int usevis = 1; 189 #endif /* _KERNEL */ 190 191 /* check for noop */ 192 if (input_len == 0) 193 return; 194 195 /* compute number of bytes mod 64 */ 196 buf_index = (ctx->count[1] >> 3) & 0x3F; 197 198 /* update number of bits */ 199 if ((ctx->count[1] += (input_len << 3)) < (input_len << 3)) 200 ctx->count[0]++; 201 202 ctx->count[0] += (input_len >> 29); 203 204 buf_len = 64 - buf_index; 205 206 /* transform as many times as possible */ 207 i = 0; 208 if (input_len >= buf_len) { 209 #ifdef _KERNEL 210 kfpu_t *fpu; 211 if (fpu_exists) { 212 uint8_t fpua[sizeof (kfpu_t) + GSR_SIZE + VIS_ALIGN]; 213 uint32_t len = (input_len + buf_index) & ~0x3f; 214 int svfp_ok; 215 216 fpu = (kfpu_t *)P2ROUNDUP((uintptr_t)fpua, 64); 217 svfp_ok = ((len >= vis_sha1_svfp_threshold) ? 1 : 0); 218 usevis = fpu_exists && sha1_savefp(fpu, svfp_ok); 219 } else { 220 usevis = 0; 221 } 222 #endif /* _KERNEL */ 223 224 /* 225 * general optimization: 226 * 227 * only do initial bcopy() and SHA1Transform() if 228 * buf_index != 0. if buf_index == 0, we're just 229 * wasting our time doing the bcopy() since there 230 * wasn't any data left over from a previous call to 231 * SHA1Update(). 232 */ 233 234 if (buf_index) { 235 bcopy(input, &ctx->buf_un.buf8[buf_index], buf_len); 236 if (usevis) { 237 SHA1TransformVIS(X0, 238 ctx->buf_un.buf32, 239 &ctx->state[0], VIS); 240 } else { 241 SHA1_TRANSFORM(ctx, ctx->buf_un.buf8); 242 } 243 i = buf_len; 244 } 245 246 /* 247 * VIS SHA-1: uses the VIS 1.0 instructions to accelerate 248 * SHA-1 processing. This is achieved by "offloading" the 249 * computation of the message schedule (MS) to the VIS units. 250 * This allows the VIS computation of the message schedule 251 * to be performed in parallel with the standard integer 252 * processing of the remainder of the SHA-1 computation. 253 * performance by up to around 1.37X, compared to an optimized 254 * integer-only implementation. 255 * 256 * The VIS implementation of SHA1Transform has a different API 257 * to the standard integer version: 258 * 259 * void SHA1TransformVIS( 260 * uint64_t *, // Pointer to MS for ith block 261 * uint32_t *, // Pointer to ith block of message data 262 * uint32_t *, // Pointer to SHA state i.e ctx->state 263 * uint64_t *, // Pointer to various VIS constants 264 * ) 265 * 266 * Note: the message data must by 4-byte aligned. 267 * 268 * Function requires VIS 1.0 support. 269 * 270 * Handling is provided to deal with arbitrary byte alingment 271 * of the input data but the performance gains are reduced 272 * for alignments other than 4-bytes. 273 */ 274 if (usevis) { 275 if (!IS_P2ALIGNED(&input[i], sizeof (uint32_t))) { 276 /* 277 * Main processing loop - input misaligned 278 */ 279 for (; i + 63 < input_len; i += 64) { 280 bcopy(&input[i], input64, 64); 281 SHA1TransformVIS(X0, (uint32_t *)input64, 282 &ctx->state[0], VIS); 283 } 284 } else { 285 /* 286 * Main processing loop - input 8-byte aligned 287 */ 288 for (; i + 63 < input_len; i += 64) { 289 SHA1TransformVIS(X0, 290 /* LINTED E_BAD_PTR_CAST_ALIGN */ 291 (uint32_t *)&input[i], 292 &ctx->state[0], VIS); 293 } 294 295 } 296 #ifdef _KERNEL 297 sha1_restorefp(fpu); 298 #endif /* _KERNEL */ 299 } else { 300 for (; i + 63 < input_len; i += 64) { 301 SHA1_TRANSFORM(ctx, &input[i]); 302 } 303 } 304 305 /* 306 * general optimization: 307 * 308 * if i and input_len are the same, return now instead 309 * of calling bcopy(), since the bcopy() in this case 310 * will be an expensive nop. 311 */ 312 313 if (input_len == i) 314 return; 315 316 buf_index = 0; 317 } 318 319 /* buffer remaining input */ 320 bcopy(&input[i], &ctx->buf_un.buf8[buf_index], input_len - i); 321 } 322 323 #else /* VIS_SHA1 */ 324 325 void 326 SHA1Update(SHA1_CTX *ctx, const void *inptr, size_t input_len) 327 { 328 uint32_t i, buf_index, buf_len; 329 const uint8_t *input = inptr; 330 331 /* check for noop */ 332 if (input_len == 0) 333 return; 334 335 /* compute number of bytes mod 64 */ 336 buf_index = (ctx->count[1] >> 3) & 0x3F; 337 338 /* update number of bits */ 339 if ((ctx->count[1] += (input_len << 3)) < (input_len << 3)) 340 ctx->count[0]++; 341 342 ctx->count[0] += (input_len >> 29); 343 344 buf_len = 64 - buf_index; 345 346 /* transform as many times as possible */ 347 i = 0; 348 if (input_len >= buf_len) { 349 350 /* 351 * general optimization: 352 * 353 * only do initial bcopy() and SHA1Transform() if 354 * buf_index != 0. if buf_index == 0, we're just 355 * wasting our time doing the bcopy() since there 356 * wasn't any data left over from a previous call to 357 * SHA1Update(). 358 */ 359 360 if (buf_index) { 361 bcopy(input, &ctx->buf_un.buf8[buf_index], buf_len); 362 SHA1_TRANSFORM(ctx, ctx->buf_un.buf8); 363 i = buf_len; 364 } 365 366 for (; i + 63 < input_len; i += 64) 367 SHA1_TRANSFORM(ctx, &input[i]); 368 369 /* 370 * general optimization: 371 * 372 * if i and input_len are the same, return now instead 373 * of calling bcopy(), since the bcopy() in this case 374 * will be an expensive nop. 375 */ 376 377 if (input_len == i) 378 return; 379 380 buf_index = 0; 381 } 382 383 /* buffer remaining input */ 384 bcopy(&input[i], &ctx->buf_un.buf8[buf_index], input_len - i); 385 } 386 387 #endif /* VIS_SHA1 */ 388 389 /* 390 * SHA1Final() 391 * 392 * purpose: ends an sha1 digest operation, finalizing the message digest and 393 * zeroing the context. 394 * input: uchar_t * : a buffer to store the digest in 395 * : The function actually uses void* because many 396 * : callers pass things other than uchar_t here. 397 * SHA1_CTX * : the context to finalize, save, and zero 398 * output: void 399 */ 400 401 void 402 SHA1Final(void *digest, SHA1_CTX *ctx) 403 { 404 uint8_t bitcount_be[sizeof (ctx->count)]; 405 uint32_t index = (ctx->count[1] >> 3) & 0x3f; 406 407 /* store bit count, big endian */ 408 Encode(bitcount_be, ctx->count, sizeof (bitcount_be)); 409 410 /* pad out to 56 mod 64 */ 411 SHA1Update(ctx, PADDING, ((index < 56) ? 56 : 120) - index); 412 413 /* append length (before padding) */ 414 SHA1Update(ctx, bitcount_be, sizeof (bitcount_be)); 415 416 /* store state in digest */ 417 Encode(digest, ctx->state, sizeof (ctx->state)); 418 419 /* zeroize sensitive information */ 420 bzero(ctx, sizeof (*ctx)); 421 } 422 423 typedef uint32_t sha1word; 424 425 /* 426 * sparc optimization: 427 * 428 * on the sparc, we can load big endian 32-bit data easily. note that 429 * special care must be taken to ensure the address is 32-bit aligned. 430 * in the interest of speed, we don't check to make sure, since 431 * careful programming can guarantee this for us. 432 */ 433 434 #if defined(_BIG_ENDIAN) 435 436 #define LOAD_BIG_32(addr) (*(uint32_t *)(addr)) 437 438 #else /* !defined(_BIG_ENDIAN) */ 439 440 #if defined(HAVE_BSWAP) 441 442 #define LOAD_BIG_32(addr) bswap(*((uint32_t *)(addr))) 443 444 #else /* !defined(HAVE_BSWAP) */ 445 446 /* little endian -- will work on big endian, but slowly */ 447 #define LOAD_BIG_32(addr) \ 448 (((addr)[0] << 24) | ((addr)[1] << 16) | ((addr)[2] << 8) | (addr)[3]) 449 450 #endif /* !defined(HAVE_BSWAP) */ 451 452 #endif /* !defined(_BIG_ENDIAN) */ 453 454 /* 455 * SHA1Transform() 456 */ 457 #if defined(W_ARRAY) 458 #define W(n) w[n] 459 #else /* !defined(W_ARRAY) */ 460 #define W(n) w_ ## n 461 #endif /* !defined(W_ARRAY) */ 462 463 464 #if defined(__sparc) 465 466 /* 467 * sparc register window optimization: 468 * 469 * `a', `b', `c', `d', and `e' are passed into SHA1Transform 470 * explicitly since it increases the number of registers available to 471 * the compiler. under this scheme, these variables can be held in 472 * %i0 - %i4, which leaves more local and out registers available. 473 * 474 * purpose: sha1 transformation -- updates the digest based on `block' 475 * input: uint32_t : bytes 1 - 4 of the digest 476 * uint32_t : bytes 5 - 8 of the digest 477 * uint32_t : bytes 9 - 12 of the digest 478 * uint32_t : bytes 12 - 16 of the digest 479 * uint32_t : bytes 16 - 20 of the digest 480 * SHA1_CTX * : the context to update 481 * uint8_t [64]: the block to use to update the digest 482 * output: void 483 */ 484 485 void 486 SHA1Transform(uint32_t a, uint32_t b, uint32_t c, uint32_t d, uint32_t e, 487 SHA1_CTX *ctx, const uint8_t blk[64]) 488 { 489 /* 490 * sparc optimization: 491 * 492 * while it is somewhat counter-intuitive, on sparc, it is 493 * more efficient to place all the constants used in this 494 * function in an array and load the values out of the array 495 * than to manually load the constants. this is because 496 * setting a register to a 32-bit value takes two ops in most 497 * cases: a `sethi' and an `or', but loading a 32-bit value 498 * from memory only takes one `ld' (or `lduw' on v9). while 499 * this increases memory usage, the compiler can find enough 500 * other things to do while waiting to keep the pipeline does 501 * not stall. additionally, it is likely that many of these 502 * constants are cached so that later accesses do not even go 503 * out to the bus. 504 * 505 * this array is declared `static' to keep the compiler from 506 * having to bcopy() this array onto the stack frame of 507 * SHA1Transform() each time it is called -- which is 508 * unacceptably expensive. 509 * 510 * the `const' is to ensure that callers are good citizens and 511 * do not try to munge the array. since these routines are 512 * going to be called from inside multithreaded kernelland, 513 * this is a good safety check. -- `sha1_consts' will end up in 514 * .rodata. 515 * 516 * unfortunately, loading from an array in this manner hurts 517 * performance under intel. so, there is a macro, 518 * SHA1_CONST(), used in SHA1Transform(), that either expands to 519 * a reference to this array, or to the actual constant, 520 * depending on what platform this code is compiled for. 521 */ 522 523 static const uint32_t sha1_consts[] = { 524 SHA1_CONST_0, SHA1_CONST_1, SHA1_CONST_2, SHA1_CONST_3, 525 }; 526 527 /* 528 * general optimization: 529 * 530 * use individual integers instead of using an array. this is a 531 * win, although the amount it wins by seems to vary quite a bit. 532 */ 533 534 uint32_t w_0, w_1, w_2, w_3, w_4, w_5, w_6, w_7; 535 uint32_t w_8, w_9, w_10, w_11, w_12, w_13, w_14, w_15; 536 537 /* 538 * sparc optimization: 539 * 540 * if `block' is already aligned on a 4-byte boundary, use 541 * LOAD_BIG_32() directly. otherwise, bcopy() into a 542 * buffer that *is* aligned on a 4-byte boundary and then do 543 * the LOAD_BIG_32() on that buffer. benchmarks have shown 544 * that using the bcopy() is better than loading the bytes 545 * individually and doing the endian-swap by hand. 546 * 547 * even though it's quite tempting to assign to do: 548 * 549 * blk = bcopy(ctx->buf_un.buf32, blk, sizeof (ctx->buf_un.buf32)); 550 * 551 * and only have one set of LOAD_BIG_32()'s, the compiler 552 * *does not* like that, so please resist the urge. 553 */ 554 555 if ((uintptr_t)blk & 0x3) { /* not 4-byte aligned? */ 556 bcopy(blk, ctx->buf_un.buf32, sizeof (ctx->buf_un.buf32)); 557 w_15 = LOAD_BIG_32(ctx->buf_un.buf32 + 15); 558 w_14 = LOAD_BIG_32(ctx->buf_un.buf32 + 14); 559 w_13 = LOAD_BIG_32(ctx->buf_un.buf32 + 13); 560 w_12 = LOAD_BIG_32(ctx->buf_un.buf32 + 12); 561 w_11 = LOAD_BIG_32(ctx->buf_un.buf32 + 11); 562 w_10 = LOAD_BIG_32(ctx->buf_un.buf32 + 10); 563 w_9 = LOAD_BIG_32(ctx->buf_un.buf32 + 9); 564 w_8 = LOAD_BIG_32(ctx->buf_un.buf32 + 8); 565 w_7 = LOAD_BIG_32(ctx->buf_un.buf32 + 7); 566 w_6 = LOAD_BIG_32(ctx->buf_un.buf32 + 6); 567 w_5 = LOAD_BIG_32(ctx->buf_un.buf32 + 5); 568 w_4 = LOAD_BIG_32(ctx->buf_un.buf32 + 4); 569 w_3 = LOAD_BIG_32(ctx->buf_un.buf32 + 3); 570 w_2 = LOAD_BIG_32(ctx->buf_un.buf32 + 2); 571 w_1 = LOAD_BIG_32(ctx->buf_un.buf32 + 1); 572 w_0 = LOAD_BIG_32(ctx->buf_un.buf32 + 0); 573 } else { 574 /*LINTED*/ 575 w_15 = LOAD_BIG_32(blk + 60); 576 /*LINTED*/ 577 w_14 = LOAD_BIG_32(blk + 56); 578 /*LINTED*/ 579 w_13 = LOAD_BIG_32(blk + 52); 580 /*LINTED*/ 581 w_12 = LOAD_BIG_32(blk + 48); 582 /*LINTED*/ 583 w_11 = LOAD_BIG_32(blk + 44); 584 /*LINTED*/ 585 w_10 = LOAD_BIG_32(blk + 40); 586 /*LINTED*/ 587 w_9 = LOAD_BIG_32(blk + 36); 588 /*LINTED*/ 589 w_8 = LOAD_BIG_32(blk + 32); 590 /*LINTED*/ 591 w_7 = LOAD_BIG_32(blk + 28); 592 /*LINTED*/ 593 w_6 = LOAD_BIG_32(blk + 24); 594 /*LINTED*/ 595 w_5 = LOAD_BIG_32(blk + 20); 596 /*LINTED*/ 597 w_4 = LOAD_BIG_32(blk + 16); 598 /*LINTED*/ 599 w_3 = LOAD_BIG_32(blk + 12); 600 /*LINTED*/ 601 w_2 = LOAD_BIG_32(blk + 8); 602 /*LINTED*/ 603 w_1 = LOAD_BIG_32(blk + 4); 604 /*LINTED*/ 605 w_0 = LOAD_BIG_32(blk + 0); 606 } 607 #else /* !defined(__sparc) */ 608 609 void 610 SHA1Transform(SHA1_CTX *ctx, const uint8_t blk[64]) 611 { 612 sha1word a = ctx->state[0]; 613 sha1word b = ctx->state[1]; 614 sha1word c = ctx->state[2]; 615 sha1word d = ctx->state[3]; 616 sha1word e = ctx->state[4]; 617 618 #if defined(W_ARRAY) 619 sha1word w[16]; 620 #else /* !defined(W_ARRAY) */ 621 sha1word w_0, w_1, w_2, w_3, w_4, w_5, w_6, w_7; 622 sha1word w_8, w_9, w_10, w_11, w_12, w_13, w_14, w_15; 623 #endif /* !defined(W_ARRAY) */ 624 625 W(0) = LOAD_BIG_32(blk + 0); 626 W(1) = LOAD_BIG_32(blk + 4); 627 W(2) = LOAD_BIG_32(blk + 8); 628 W(3) = LOAD_BIG_32(blk + 12); 629 W(4) = LOAD_BIG_32(blk + 16); 630 W(5) = LOAD_BIG_32(blk + 20); 631 W(6) = LOAD_BIG_32(blk + 24); 632 W(7) = LOAD_BIG_32(blk + 28); 633 W(8) = LOAD_BIG_32(blk + 32); 634 W(9) = LOAD_BIG_32(blk + 36); 635 W(10) = LOAD_BIG_32(blk + 40); 636 W(11) = LOAD_BIG_32(blk + 44); 637 W(12) = LOAD_BIG_32(blk + 48); 638 W(13) = LOAD_BIG_32(blk + 52); 639 W(14) = LOAD_BIG_32(blk + 56); 640 W(15) = LOAD_BIG_32(blk + 60); 641 642 #endif /* !defined(__sparc) */ 643 644 /* 645 * general optimization: 646 * 647 * even though this approach is described in the standard as 648 * being slower algorithmically, it is 30-40% faster than the 649 * "faster" version under SPARC, because this version has more 650 * of the constraints specified at compile-time and uses fewer 651 * variables (and therefore has better register utilization) 652 * than its "speedier" brother. (i've tried both, trust me) 653 * 654 * for either method given in the spec, there is an "assignment" 655 * phase where the following takes place: 656 * 657 * tmp = (main_computation); 658 * e = d; d = c; c = rotate_left(b, 30); b = a; a = tmp; 659 * 660 * we can make the algorithm go faster by not doing this work, 661 * but just pretending that `d' is now `e', etc. this works 662 * really well and obviates the need for a temporary variable. 663 * however, we still explictly perform the rotate action, 664 * since it is cheaper on SPARC to do it once than to have to 665 * do it over and over again. 666 */ 667 668 /* round 1 */ 669 e = ROTATE_LEFT(a, 5) + F(b, c, d) + e + W(0) + SHA1_CONST(0); /* 0 */ 670 b = ROTATE_LEFT(b, 30); 671 672 d = ROTATE_LEFT(e, 5) + F(a, b, c) + d + W(1) + SHA1_CONST(0); /* 1 */ 673 a = ROTATE_LEFT(a, 30); 674 675 c = ROTATE_LEFT(d, 5) + F(e, a, b) + c + W(2) + SHA1_CONST(0); /* 2 */ 676 e = ROTATE_LEFT(e, 30); 677 678 b = ROTATE_LEFT(c, 5) + F(d, e, a) + b + W(3) + SHA1_CONST(0); /* 3 */ 679 d = ROTATE_LEFT(d, 30); 680 681 a = ROTATE_LEFT(b, 5) + F(c, d, e) + a + W(4) + SHA1_CONST(0); /* 4 */ 682 c = ROTATE_LEFT(c, 30); 683 684 e = ROTATE_LEFT(a, 5) + F(b, c, d) + e + W(5) + SHA1_CONST(0); /* 5 */ 685 b = ROTATE_LEFT(b, 30); 686 687 d = ROTATE_LEFT(e, 5) + F(a, b, c) + d + W(6) + SHA1_CONST(0); /* 6 */ 688 a = ROTATE_LEFT(a, 30); 689 690 c = ROTATE_LEFT(d, 5) + F(e, a, b) + c + W(7) + SHA1_CONST(0); /* 7 */ 691 e = ROTATE_LEFT(e, 30); 692 693 b = ROTATE_LEFT(c, 5) + F(d, e, a) + b + W(8) + SHA1_CONST(0); /* 8 */ 694 d = ROTATE_LEFT(d, 30); 695 696 a = ROTATE_LEFT(b, 5) + F(c, d, e) + a + W(9) + SHA1_CONST(0); /* 9 */ 697 c = ROTATE_LEFT(c, 30); 698 699 e = ROTATE_LEFT(a, 5) + F(b, c, d) + e + W(10) + SHA1_CONST(0); /* 10 */ 700 b = ROTATE_LEFT(b, 30); 701 702 d = ROTATE_LEFT(e, 5) + F(a, b, c) + d + W(11) + SHA1_CONST(0); /* 11 */ 703 a = ROTATE_LEFT(a, 30); 704 705 c = ROTATE_LEFT(d, 5) + F(e, a, b) + c + W(12) + SHA1_CONST(0); /* 12 */ 706 e = ROTATE_LEFT(e, 30); 707 708 b = ROTATE_LEFT(c, 5) + F(d, e, a) + b + W(13) + SHA1_CONST(0); /* 13 */ 709 d = ROTATE_LEFT(d, 30); 710 711 a = ROTATE_LEFT(b, 5) + F(c, d, e) + a + W(14) + SHA1_CONST(0); /* 14 */ 712 c = ROTATE_LEFT(c, 30); 713 714 e = ROTATE_LEFT(a, 5) + F(b, c, d) + e + W(15) + SHA1_CONST(0); /* 15 */ 715 b = ROTATE_LEFT(b, 30); 716 717 W(0) = ROTATE_LEFT((W(13) ^ W(8) ^ W(2) ^ W(0)), 1); /* 16 */ 718 d = ROTATE_LEFT(e, 5) + F(a, b, c) + d + W(0) + SHA1_CONST(0); 719 a = ROTATE_LEFT(a, 30); 720 721 W(1) = ROTATE_LEFT((W(14) ^ W(9) ^ W(3) ^ W(1)), 1); /* 17 */ 722 c = ROTATE_LEFT(d, 5) + F(e, a, b) + c + W(1) + SHA1_CONST(0); 723 e = ROTATE_LEFT(e, 30); 724 725 W(2) = ROTATE_LEFT((W(15) ^ W(10) ^ W(4) ^ W(2)), 1); /* 18 */ 726 b = ROTATE_LEFT(c, 5) + F(d, e, a) + b + W(2) + SHA1_CONST(0); 727 d = ROTATE_LEFT(d, 30); 728 729 W(3) = ROTATE_LEFT((W(0) ^ W(11) ^ W(5) ^ W(3)), 1); /* 19 */ 730 a = ROTATE_LEFT(b, 5) + F(c, d, e) + a + W(3) + SHA1_CONST(0); 731 c = ROTATE_LEFT(c, 30); 732 733 /* round 2 */ 734 W(4) = ROTATE_LEFT((W(1) ^ W(12) ^ W(6) ^ W(4)), 1); /* 20 */ 735 e = ROTATE_LEFT(a, 5) + G(b, c, d) + e + W(4) + SHA1_CONST(1); 736 b = ROTATE_LEFT(b, 30); 737 738 W(5) = ROTATE_LEFT((W(2) ^ W(13) ^ W(7) ^ W(5)), 1); /* 21 */ 739 d = ROTATE_LEFT(e, 5) + G(a, b, c) + d + W(5) + SHA1_CONST(1); 740 a = ROTATE_LEFT(a, 30); 741 742 W(6) = ROTATE_LEFT((W(3) ^ W(14) ^ W(8) ^ W(6)), 1); /* 22 */ 743 c = ROTATE_LEFT(d, 5) + G(e, a, b) + c + W(6) + SHA1_CONST(1); 744 e = ROTATE_LEFT(e, 30); 745 746 W(7) = ROTATE_LEFT((W(4) ^ W(15) ^ W(9) ^ W(7)), 1); /* 23 */ 747 b = ROTATE_LEFT(c, 5) + G(d, e, a) + b + W(7) + SHA1_CONST(1); 748 d = ROTATE_LEFT(d, 30); 749 750 W(8) = ROTATE_LEFT((W(5) ^ W(0) ^ W(10) ^ W(8)), 1); /* 24 */ 751 a = ROTATE_LEFT(b, 5) + G(c, d, e) + a + W(8) + SHA1_CONST(1); 752 c = ROTATE_LEFT(c, 30); 753 754 W(9) = ROTATE_LEFT((W(6) ^ W(1) ^ W(11) ^ W(9)), 1); /* 25 */ 755 e = ROTATE_LEFT(a, 5) + G(b, c, d) + e + W(9) + SHA1_CONST(1); 756 b = ROTATE_LEFT(b, 30); 757 758 W(10) = ROTATE_LEFT((W(7) ^ W(2) ^ W(12) ^ W(10)), 1); /* 26 */ 759 d = ROTATE_LEFT(e, 5) + G(a, b, c) + d + W(10) + SHA1_CONST(1); 760 a = ROTATE_LEFT(a, 30); 761 762 W(11) = ROTATE_LEFT((W(8) ^ W(3) ^ W(13) ^ W(11)), 1); /* 27 */ 763 c = ROTATE_LEFT(d, 5) + G(e, a, b) + c + W(11) + SHA1_CONST(1); 764 e = ROTATE_LEFT(e, 30); 765 766 W(12) = ROTATE_LEFT((W(9) ^ W(4) ^ W(14) ^ W(12)), 1); /* 28 */ 767 b = ROTATE_LEFT(c, 5) + G(d, e, a) + b + W(12) + SHA1_CONST(1); 768 d = ROTATE_LEFT(d, 30); 769 770 W(13) = ROTATE_LEFT((W(10) ^ W(5) ^ W(15) ^ W(13)), 1); /* 29 */ 771 a = ROTATE_LEFT(b, 5) + G(c, d, e) + a + W(13) + SHA1_CONST(1); 772 c = ROTATE_LEFT(c, 30); 773 774 W(14) = ROTATE_LEFT((W(11) ^ W(6) ^ W(0) ^ W(14)), 1); /* 30 */ 775 e = ROTATE_LEFT(a, 5) + G(b, c, d) + e + W(14) + SHA1_CONST(1); 776 b = ROTATE_LEFT(b, 30); 777 778 W(15) = ROTATE_LEFT((W(12) ^ W(7) ^ W(1) ^ W(15)), 1); /* 31 */ 779 d = ROTATE_LEFT(e, 5) + G(a, b, c) + d + W(15) + SHA1_CONST(1); 780 a = ROTATE_LEFT(a, 30); 781 782 W(0) = ROTATE_LEFT((W(13) ^ W(8) ^ W(2) ^ W(0)), 1); /* 32 */ 783 c = ROTATE_LEFT(d, 5) + G(e, a, b) + c + W(0) + SHA1_CONST(1); 784 e = ROTATE_LEFT(e, 30); 785 786 W(1) = ROTATE_LEFT((W(14) ^ W(9) ^ W(3) ^ W(1)), 1); /* 33 */ 787 b = ROTATE_LEFT(c, 5) + G(d, e, a) + b + W(1) + SHA1_CONST(1); 788 d = ROTATE_LEFT(d, 30); 789 790 W(2) = ROTATE_LEFT((W(15) ^ W(10) ^ W(4) ^ W(2)), 1); /* 34 */ 791 a = ROTATE_LEFT(b, 5) + G(c, d, e) + a + W(2) + SHA1_CONST(1); 792 c = ROTATE_LEFT(c, 30); 793 794 W(3) = ROTATE_LEFT((W(0) ^ W(11) ^ W(5) ^ W(3)), 1); /* 35 */ 795 e = ROTATE_LEFT(a, 5) + G(b, c, d) + e + W(3) + SHA1_CONST(1); 796 b = ROTATE_LEFT(b, 30); 797 798 W(4) = ROTATE_LEFT((W(1) ^ W(12) ^ W(6) ^ W(4)), 1); /* 36 */ 799 d = ROTATE_LEFT(e, 5) + G(a, b, c) + d + W(4) + SHA1_CONST(1); 800 a = ROTATE_LEFT(a, 30); 801 802 W(5) = ROTATE_LEFT((W(2) ^ W(13) ^ W(7) ^ W(5)), 1); /* 37 */ 803 c = ROTATE_LEFT(d, 5) + G(e, a, b) + c + W(5) + SHA1_CONST(1); 804 e = ROTATE_LEFT(e, 30); 805 806 W(6) = ROTATE_LEFT((W(3) ^ W(14) ^ W(8) ^ W(6)), 1); /* 38 */ 807 b = ROTATE_LEFT(c, 5) + G(d, e, a) + b + W(6) + SHA1_CONST(1); 808 d = ROTATE_LEFT(d, 30); 809 810 W(7) = ROTATE_LEFT((W(4) ^ W(15) ^ W(9) ^ W(7)), 1); /* 39 */ 811 a = ROTATE_LEFT(b, 5) + G(c, d, e) + a + W(7) + SHA1_CONST(1); 812 c = ROTATE_LEFT(c, 30); 813 814 /* round 3 */ 815 W(8) = ROTATE_LEFT((W(5) ^ W(0) ^ W(10) ^ W(8)), 1); /* 40 */ 816 e = ROTATE_LEFT(a, 5) + H(b, c, d) + e + W(8) + SHA1_CONST(2); 817 b = ROTATE_LEFT(b, 30); 818 819 W(9) = ROTATE_LEFT((W(6) ^ W(1) ^ W(11) ^ W(9)), 1); /* 41 */ 820 d = ROTATE_LEFT(e, 5) + H(a, b, c) + d + W(9) + SHA1_CONST(2); 821 a = ROTATE_LEFT(a, 30); 822 823 W(10) = ROTATE_LEFT((W(7) ^ W(2) ^ W(12) ^ W(10)), 1); /* 42 */ 824 c = ROTATE_LEFT(d, 5) + H(e, a, b) + c + W(10) + SHA1_CONST(2); 825 e = ROTATE_LEFT(e, 30); 826 827 W(11) = ROTATE_LEFT((W(8) ^ W(3) ^ W(13) ^ W(11)), 1); /* 43 */ 828 b = ROTATE_LEFT(c, 5) + H(d, e, a) + b + W(11) + SHA1_CONST(2); 829 d = ROTATE_LEFT(d, 30); 830 831 W(12) = ROTATE_LEFT((W(9) ^ W(4) ^ W(14) ^ W(12)), 1); /* 44 */ 832 a = ROTATE_LEFT(b, 5) + H(c, d, e) + a + W(12) + SHA1_CONST(2); 833 c = ROTATE_LEFT(c, 30); 834 835 W(13) = ROTATE_LEFT((W(10) ^ W(5) ^ W(15) ^ W(13)), 1); /* 45 */ 836 e = ROTATE_LEFT(a, 5) + H(b, c, d) + e + W(13) + SHA1_CONST(2); 837 b = ROTATE_LEFT(b, 30); 838 839 W(14) = ROTATE_LEFT((W(11) ^ W(6) ^ W(0) ^ W(14)), 1); /* 46 */ 840 d = ROTATE_LEFT(e, 5) + H(a, b, c) + d + W(14) + SHA1_CONST(2); 841 a = ROTATE_LEFT(a, 30); 842 843 W(15) = ROTATE_LEFT((W(12) ^ W(7) ^ W(1) ^ W(15)), 1); /* 47 */ 844 c = ROTATE_LEFT(d, 5) + H(e, a, b) + c + W(15) + SHA1_CONST(2); 845 e = ROTATE_LEFT(e, 30); 846 847 W(0) = ROTATE_LEFT((W(13) ^ W(8) ^ W(2) ^ W(0)), 1); /* 48 */ 848 b = ROTATE_LEFT(c, 5) + H(d, e, a) + b + W(0) + SHA1_CONST(2); 849 d = ROTATE_LEFT(d, 30); 850 851 W(1) = ROTATE_LEFT((W(14) ^ W(9) ^ W(3) ^ W(1)), 1); /* 49 */ 852 a = ROTATE_LEFT(b, 5) + H(c, d, e) + a + W(1) + SHA1_CONST(2); 853 c = ROTATE_LEFT(c, 30); 854 855 W(2) = ROTATE_LEFT((W(15) ^ W(10) ^ W(4) ^ W(2)), 1); /* 50 */ 856 e = ROTATE_LEFT(a, 5) + H(b, c, d) + e + W(2) + SHA1_CONST(2); 857 b = ROTATE_LEFT(b, 30); 858 859 W(3) = ROTATE_LEFT((W(0) ^ W(11) ^ W(5) ^ W(3)), 1); /* 51 */ 860 d = ROTATE_LEFT(e, 5) + H(a, b, c) + d + W(3) + SHA1_CONST(2); 861 a = ROTATE_LEFT(a, 30); 862 863 W(4) = ROTATE_LEFT((W(1) ^ W(12) ^ W(6) ^ W(4)), 1); /* 52 */ 864 c = ROTATE_LEFT(d, 5) + H(e, a, b) + c + W(4) + SHA1_CONST(2); 865 e = ROTATE_LEFT(e, 30); 866 867 W(5) = ROTATE_LEFT((W(2) ^ W(13) ^ W(7) ^ W(5)), 1); /* 53 */ 868 b = ROTATE_LEFT(c, 5) + H(d, e, a) + b + W(5) + SHA1_CONST(2); 869 d = ROTATE_LEFT(d, 30); 870 871 W(6) = ROTATE_LEFT((W(3) ^ W(14) ^ W(8) ^ W(6)), 1); /* 54 */ 872 a = ROTATE_LEFT(b, 5) + H(c, d, e) + a + W(6) + SHA1_CONST(2); 873 c = ROTATE_LEFT(c, 30); 874 875 W(7) = ROTATE_LEFT((W(4) ^ W(15) ^ W(9) ^ W(7)), 1); /* 55 */ 876 e = ROTATE_LEFT(a, 5) + H(b, c, d) + e + W(7) + SHA1_CONST(2); 877 b = ROTATE_LEFT(b, 30); 878 879 W(8) = ROTATE_LEFT((W(5) ^ W(0) ^ W(10) ^ W(8)), 1); /* 56 */ 880 d = ROTATE_LEFT(e, 5) + H(a, b, c) + d + W(8) + SHA1_CONST(2); 881 a = ROTATE_LEFT(a, 30); 882 883 W(9) = ROTATE_LEFT((W(6) ^ W(1) ^ W(11) ^ W(9)), 1); /* 57 */ 884 c = ROTATE_LEFT(d, 5) + H(e, a, b) + c + W(9) + SHA1_CONST(2); 885 e = ROTATE_LEFT(e, 30); 886 887 W(10) = ROTATE_LEFT((W(7) ^ W(2) ^ W(12) ^ W(10)), 1); /* 58 */ 888 b = ROTATE_LEFT(c, 5) + H(d, e, a) + b + W(10) + SHA1_CONST(2); 889 d = ROTATE_LEFT(d, 30); 890 891 W(11) = ROTATE_LEFT((W(8) ^ W(3) ^ W(13) ^ W(11)), 1); /* 59 */ 892 a = ROTATE_LEFT(b, 5) + H(c, d, e) + a + W(11) + SHA1_CONST(2); 893 c = ROTATE_LEFT(c, 30); 894 895 /* round 4 */ 896 W(12) = ROTATE_LEFT((W(9) ^ W(4) ^ W(14) ^ W(12)), 1); /* 60 */ 897 e = ROTATE_LEFT(a, 5) + G(b, c, d) + e + W(12) + SHA1_CONST(3); 898 b = ROTATE_LEFT(b, 30); 899 900 W(13) = ROTATE_LEFT((W(10) ^ W(5) ^ W(15) ^ W(13)), 1); /* 61 */ 901 d = ROTATE_LEFT(e, 5) + G(a, b, c) + d + W(13) + SHA1_CONST(3); 902 a = ROTATE_LEFT(a, 30); 903 904 W(14) = ROTATE_LEFT((W(11) ^ W(6) ^ W(0) ^ W(14)), 1); /* 62 */ 905 c = ROTATE_LEFT(d, 5) + G(e, a, b) + c + W(14) + SHA1_CONST(3); 906 e = ROTATE_LEFT(e, 30); 907 908 W(15) = ROTATE_LEFT((W(12) ^ W(7) ^ W(1) ^ W(15)), 1); /* 63 */ 909 b = ROTATE_LEFT(c, 5) + G(d, e, a) + b + W(15) + SHA1_CONST(3); 910 d = ROTATE_LEFT(d, 30); 911 912 W(0) = ROTATE_LEFT((W(13) ^ W(8) ^ W(2) ^ W(0)), 1); /* 64 */ 913 a = ROTATE_LEFT(b, 5) + G(c, d, e) + a + W(0) + SHA1_CONST(3); 914 c = ROTATE_LEFT(c, 30); 915 916 W(1) = ROTATE_LEFT((W(14) ^ W(9) ^ W(3) ^ W(1)), 1); /* 65 */ 917 e = ROTATE_LEFT(a, 5) + G(b, c, d) + e + W(1) + SHA1_CONST(3); 918 b = ROTATE_LEFT(b, 30); 919 920 W(2) = ROTATE_LEFT((W(15) ^ W(10) ^ W(4) ^ W(2)), 1); /* 66 */ 921 d = ROTATE_LEFT(e, 5) + G(a, b, c) + d + W(2) + SHA1_CONST(3); 922 a = ROTATE_LEFT(a, 30); 923 924 W(3) = ROTATE_LEFT((W(0) ^ W(11) ^ W(5) ^ W(3)), 1); /* 67 */ 925 c = ROTATE_LEFT(d, 5) + G(e, a, b) + c + W(3) + SHA1_CONST(3); 926 e = ROTATE_LEFT(e, 30); 927 928 W(4) = ROTATE_LEFT((W(1) ^ W(12) ^ W(6) ^ W(4)), 1); /* 68 */ 929 b = ROTATE_LEFT(c, 5) + G(d, e, a) + b + W(4) + SHA1_CONST(3); 930 d = ROTATE_LEFT(d, 30); 931 932 W(5) = ROTATE_LEFT((W(2) ^ W(13) ^ W(7) ^ W(5)), 1); /* 69 */ 933 a = ROTATE_LEFT(b, 5) + G(c, d, e) + a + W(5) + SHA1_CONST(3); 934 c = ROTATE_LEFT(c, 30); 935 936 W(6) = ROTATE_LEFT((W(3) ^ W(14) ^ W(8) ^ W(6)), 1); /* 70 */ 937 e = ROTATE_LEFT(a, 5) + G(b, c, d) + e + W(6) + SHA1_CONST(3); 938 b = ROTATE_LEFT(b, 30); 939 940 W(7) = ROTATE_LEFT((W(4) ^ W(15) ^ W(9) ^ W(7)), 1); /* 71 */ 941 d = ROTATE_LEFT(e, 5) + G(a, b, c) + d + W(7) + SHA1_CONST(3); 942 a = ROTATE_LEFT(a, 30); 943 944 W(8) = ROTATE_LEFT((W(5) ^ W(0) ^ W(10) ^ W(8)), 1); /* 72 */ 945 c = ROTATE_LEFT(d, 5) + G(e, a, b) + c + W(8) + SHA1_CONST(3); 946 e = ROTATE_LEFT(e, 30); 947 948 W(9) = ROTATE_LEFT((W(6) ^ W(1) ^ W(11) ^ W(9)), 1); /* 73 */ 949 b = ROTATE_LEFT(c, 5) + G(d, e, a) + b + W(9) + SHA1_CONST(3); 950 d = ROTATE_LEFT(d, 30); 951 952 W(10) = ROTATE_LEFT((W(7) ^ W(2) ^ W(12) ^ W(10)), 1); /* 74 */ 953 a = ROTATE_LEFT(b, 5) + G(c, d, e) + a + W(10) + SHA1_CONST(3); 954 c = ROTATE_LEFT(c, 30); 955 956 W(11) = ROTATE_LEFT((W(8) ^ W(3) ^ W(13) ^ W(11)), 1); /* 75 */ 957 e = ROTATE_LEFT(a, 5) + G(b, c, d) + e + W(11) + SHA1_CONST(3); 958 b = ROTATE_LEFT(b, 30); 959 960 W(12) = ROTATE_LEFT((W(9) ^ W(4) ^ W(14) ^ W(12)), 1); /* 76 */ 961 d = ROTATE_LEFT(e, 5) + G(a, b, c) + d + W(12) + SHA1_CONST(3); 962 a = ROTATE_LEFT(a, 30); 963 964 W(13) = ROTATE_LEFT((W(10) ^ W(5) ^ W(15) ^ W(13)), 1); /* 77 */ 965 c = ROTATE_LEFT(d, 5) + G(e, a, b) + c + W(13) + SHA1_CONST(3); 966 e = ROTATE_LEFT(e, 30); 967 968 W(14) = ROTATE_LEFT((W(11) ^ W(6) ^ W(0) ^ W(14)), 1); /* 78 */ 969 b = ROTATE_LEFT(c, 5) + G(d, e, a) + b + W(14) + SHA1_CONST(3); 970 d = ROTATE_LEFT(d, 30); 971 972 W(15) = ROTATE_LEFT((W(12) ^ W(7) ^ W(1) ^ W(15)), 1); /* 79 */ 973 974 ctx->state[0] += ROTATE_LEFT(b, 5) + G(c, d, e) + a + W(15) + 975 SHA1_CONST(3); 976 ctx->state[1] += b; 977 ctx->state[2] += ROTATE_LEFT(c, 30); 978 ctx->state[3] += d; 979 ctx->state[4] += e; 980 981 /* zeroize sensitive information */ 982 W(0) = W(1) = W(2) = W(3) = W(4) = W(5) = W(6) = W(7) = W(8) = 0; 983 W(9) = W(10) = W(11) = W(12) = W(13) = W(14) = W(15) = 0; 984 } 985 986 /* 987 * Encode() 988 * 989 * purpose: to convert a list of numbers from little endian to big endian 990 * input: uint8_t * : place to store the converted big endian numbers 991 * uint32_t * : place to get numbers to convert from 992 * size_t : the length of the input in bytes 993 * output: void 994 */ 995 996 static void 997 Encode(uint8_t *_RESTRICT_KYWD output, const uint32_t *_RESTRICT_KYWD input, 998 size_t len) 999 { 1000 size_t i, j; 1001 1002 #if defined(__sparc) 1003 if (IS_P2ALIGNED(output, sizeof (uint32_t))) { 1004 for (i = 0, j = 0; j < len; i++, j += 4) { 1005 /* LINTED: pointer alignment */ 1006 *((uint32_t *)(output + j)) = input[i]; 1007 } 1008 } else { 1009 #endif /* little endian -- will work on big endian, but slowly */ 1010 for (i = 0, j = 0; j < len; i++, j += 4) { 1011 output[j] = (input[i] >> 24) & 0xff; 1012 output[j + 1] = (input[i] >> 16) & 0xff; 1013 output[j + 2] = (input[i] >> 8) & 0xff; 1014 output[j + 3] = input[i] & 0xff; 1015 } 1016 #if defined(__sparc) 1017 } 1018 #endif 1019 } 1020