Print this page
5007142 Add ntohll and htonll to sys/byteorder.h
6717509 Need to use bswap/bswapq for byte swap of 64-bit integer on x32/x64
PSARC 2008/474

Split Close
Expand all
Collapse all
          --- old/usr/src/common/crypto/sha1/sha1.c
          +++ new/usr/src/common/crypto/sha1/sha1.c
   1    1  /*
   2    2   * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
   3    3   * Use is subject to license terms.
   4    4   */
   5    5  
   6      -#pragma ident   "%Z%%M% %I%     %E% SMI"
   7      -
   8    6  /*
   9    7   * The basic framework for this code came from the reference
  10    8   * implementation for MD5.  That implementation is Copyright (C)
  11    9   * 1991-2, RSA Data Security, Inc. Created 1991. All rights reserved.
  12   10   *
  13   11   * License to copy and use this software is granted provided that it
  14   12   * is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  15   13   * Algorithm" in all material mentioning or referencing this software
  16   14   * or this function.
  17   15   *
↓ open down ↓ 4 lines elided ↑ open up ↑
  22   20   *
  23   21   * RSA Data Security, Inc. makes no representations concerning either
  24   22   * the merchantability of this software or the suitability of this
  25   23   * software for any particular purpose. It is provided "as is"
  26   24   * without express or implied warranty of any kind.
  27   25   *
  28   26   * These notices must be retained in any copies of any part of this
  29   27   * documentation and/or software.
  30   28   *
  31   29   * 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
       30 + * standard, available at http://www.itl.nist.gov/fipspubs/fip180-1.htm
  33   31   * Not as fast as one would like -- further optimizations are encouraged
  34   32   * and appreciated.
  35   33   */
  36   34  
  37   35  #include <sys/types.h>
  38   36  #include <sys/param.h>
  39   37  #include <sys/systm.h>
  40   38  #include <sys/sysmacros.h>
  41   39  #include <sys/sha1.h>
  42   40  #include <sys/sha1_consts.h>
  43   41  
  44   42  #ifndef _KERNEL
  45   43  #include <strings.h>
  46   44  #include <stdlib.h>
  47   45  #include <errno.h>
  48   46  #include <sys/systeminfo.h>
  49   47  #endif  /* !_KERNEL */
  50   48  
       49 +#ifdef _LITTLE_ENDIAN
       50 +#include <sys/byteorder.h>
       51 +#define HAVE_HTONL
       52 +#endif
       53 +
  51   54  static void Encode(uint8_t *, const uint32_t *, size_t);
  52   55  
  53   56  #if     defined(__sparc)
  54   57  
  55   58  #define SHA1_TRANSFORM(ctx, in) \
  56   59          SHA1Transform((ctx)->state[0], (ctx)->state[1], (ctx)->state[2], \
  57   60                  (ctx)->state[3], (ctx)->state[4], (ctx), (in))
  58   61  
  59   62  static void SHA1Transform(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t,
  60   63      SHA1_CTX *, const uint8_t *);
↓ open down ↓ 38 lines elided ↑ open up ↑
  99  102          return ((t32 << n) | (t32 >> (32 - n)));
 100  103  }
 101  104  
 102  105  #else
 103  106  
 104  107  #define ROTATE_LEFT(x, n)       \
 105  108          (((x) << (n)) | ((x) >> ((sizeof (x) * NBBY)-(n))))
 106  109  
 107  110  #endif
 108  111  
 109      -#if     defined(__GNUC__) && (defined(__i386) || defined(__amd64))
 110  112  
 111      -#define HAVE_BSWAP
 112      -
 113      -extern __inline__ uint32_t bswap(uint32_t value)
 114      -{
 115      -        __asm__("bswap %0" : "+r" (value));
 116      -        return (value);
 117      -}
 118      -
 119      -#endif
 120      -
 121  113  /*
 122  114   * SHA1Init()
 123  115   *
 124  116   * purpose: initializes the sha1 context and begins and sha1 digest operation
 125  117   *   input: SHA1_CTX *  : the context to initializes.
 126  118   *  output: void
 127  119   */
 128  120  
 129  121  void
 130  122  SHA1Init(SHA1_CTX *ctx)
↓ open down ↓ 158 lines elided ↑ open up ↑
 289  281                                          SHA1TransformVIS(X0,
 290  282                                              (uint32_t *)input64,
 291  283                                              &ctx->state[0], VIS);
 292  284                                  }
 293  285                          } else {
 294  286                                  /*
 295  287                                   * Main processing loop - input 8-byte aligned
 296  288                                   */
 297  289                                  for (; i + 63 < input_len; i += 64) {
 298  290                                          SHA1TransformVIS(X0,
 299      -                                        /* LINTED E_BAD_PTR_CAST_ALIGN */
 300      -                                            (uint32_t *)&input[i],
      291 +                                            /* LINTED E_BAD_PTR_CAST_ALIGN */
      292 +                                            (uint32_t *)&input[i], /* CSTYLED */
 301  293                                              &ctx->state[0], VIS);
 302  294                                  }
 303  295  
 304  296                          }
 305  297  #ifdef _KERNEL
 306  298                          sha1_restorefp(fpu);
 307  299  #endif /* _KERNEL */
 308  300                  } else {
 309  301                          for (; i + 63 < input_len; i += 64) {
 310  302                                  SHA1_TRANSFORM(ctx, &input[i]);
↓ open down ↓ 137 lines elided ↑ open up ↑
 448  440  /*
 449  441   * sparc optimization:
 450  442   *
 451  443   * on the sparc, we can load big endian 32-bit data easily.  note that
 452  444   * special care must be taken to ensure the address is 32-bit aligned.
 453  445   * in the interest of speed, we don't check to make sure, since
 454  446   * careful programming can guarantee this for us.
 455  447   */
 456  448  
 457  449  #if     defined(_BIG_ENDIAN)
 458      -
 459  450  #define LOAD_BIG_32(addr)       (*(uint32_t *)(addr))
 460  451  
 461      -#else   /* !defined(_BIG_ENDIAN) */
      452 +#elif   defined(HAVE_HTONL)
      453 +#define LOAD_BIG_32(addr) htonl(*((uint32_t *)(addr)))
 462  454  
 463      -#if     defined(HAVE_BSWAP)
 464      -
 465      -#define LOAD_BIG_32(addr) bswap(*((uint32_t *)(addr)))
 466      -
 467      -#else   /* !defined(HAVE_BSWAP) */
 468      -
      455 +#else
 469  456  /* little endian -- will work on big endian, but slowly */
 470  457  #define LOAD_BIG_32(addr)       \
 471  458          (((addr)[0] << 24) | ((addr)[1] << 16) | ((addr)[2] << 8) | (addr)[3])
      459 +#endif  /* _BIG_ENDIAN */
 472  460  
 473      -#endif  /* !defined(HAVE_BSWAP) */
 474      -
 475      -#endif  /* !defined(_BIG_ENDIAN) */
 476      -
 477  461  /*
 478  462   * SHA1Transform()
 479  463   */
 480  464  #if     defined(W_ARRAY)
 481  465  #define W(n) w[n]
 482  466  #else   /* !defined(W_ARRAY) */
 483  467  #define W(n) w_ ## n
 484  468  #endif  /* !defined(W_ARRAY) */
 485  469  
 486  470  
↓ open down ↓ 43 lines elided ↑ open up ↑
 530  514           * SHA1Transform() each time it is called -- which is
 531  515           * unacceptably expensive.
 532  516           *
 533  517           * the `const' is to ensure that callers are good citizens and
 534  518           * do not try to munge the array.  since these routines are
 535  519           * going to be called from inside multithreaded kernelland,
 536  520           * this is a good safety check. -- `sha1_consts' will end up in
 537  521           * .rodata.
 538  522           *
 539  523           * unfortunately, loading from an array in this manner hurts
 540      -         * performance under intel.  so, there is a macro,
      524 +         * performance under Intel.  So, there is a macro,
 541  525           * SHA1_CONST(), used in SHA1Transform(), that either expands to
 542  526           * a reference to this array, or to the actual constant,
 543  527           * depending on what platform this code is compiled for.
 544  528           */
 545  529  
 546  530          static const uint32_t sha1_consts[] = {
 547      -                SHA1_CONST_0,   SHA1_CONST_1,   SHA1_CONST_2,   SHA1_CONST_3,
      531 +                SHA1_CONST_0, SHA1_CONST_1, SHA1_CONST_2, SHA1_CONST_3
 548  532          };
 549  533  
 550  534          /*
 551  535           * general optimization:
 552  536           *
 553  537           * use individual integers instead of using an array.  this is a
 554  538           * win, although the amount it wins by seems to vary quite a bit.
 555  539           */
 556  540  
 557  541          uint32_t        w_0, w_1, w_2,  w_3,  w_4,  w_5,  w_6,  w_7;
↓ open down ↓ 64 lines elided ↑ open up ↑
 622  606                  w_3  = LOAD_BIG_32(blk + 12);
 623  607                  /*LINTED*/
 624  608                  w_2  = LOAD_BIG_32(blk +  8);
 625  609                  /*LINTED*/
 626  610                  w_1  = LOAD_BIG_32(blk +  4);
 627  611                  /*LINTED*/
 628  612                  w_0  = LOAD_BIG_32(blk +  0);
 629  613          }
 630  614  #else   /* !defined(__sparc) */
 631  615  
 632      -void
      616 +void /* CSTYLED */
 633  617  SHA1Transform(SHA1_CTX *ctx, const uint8_t blk[64])
 634  618  {
      619 +        /* CSTYLED */
 635  620          sha1word a = ctx->state[0];
 636  621          sha1word b = ctx->state[1];
 637  622          sha1word c = ctx->state[2];
 638  623          sha1word d = ctx->state[3];
 639  624          sha1word e = ctx->state[4];
 640  625  
 641  626  #if     defined(W_ARRAY)
 642  627          sha1word        w[16];
 643  628  #else   /* !defined(W_ARRAY) */
 644  629          sha1word        w_0, w_1, w_2,  w_3,  w_4,  w_5,  w_6,  w_7;
↓ open down ↓ 400 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX