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/sha2/sha2.c
          +++ new/usr/src/common/crypto/sha2/sha2.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 SHA2, based on the FIPS 180-2
  32      - * standard, available at http://www.itl.nist.gov/div897/pubs/fip180-2.htm
       30 + * standard, available at
       31 + * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
  33   32   * Not as fast as one would like -- further optimizations are encouraged
  34   33   * and appreciated.
  35   34   */
  36   35  
  37   36  #include <sys/types.h>
  38   37  #include <sys/param.h>
  39   38  #include <sys/systm.h>
  40   39  #include <sys/sysmacros.h>
  41   40  #define _SHA2_IMPL
  42   41  #include <sys/sha2.h>
↓ open down ↓ 10 lines elided ↑ open up ↑
  53   52  #pragma weak SHA256Update = SHA2Update
  54   53  #pragma weak SHA384Update = SHA2Update
  55   54  #pragma weak SHA512Update = SHA2Update
  56   55  
  57   56  #pragma weak SHA256Final = SHA2Final
  58   57  #pragma weak SHA384Final = SHA2Final
  59   58  #pragma weak SHA512Final = SHA2Final
  60   59  
  61   60  #endif  /* _KERNEL */
  62   61  
       62 +#ifdef _LITTLE_ENDIAN
       63 +#include <sys/byteorder.h>
       64 +#define HAVE_HTONL
       65 +#endif
       66 +
  63   67  static void Encode(uint8_t *, uint32_t *, size_t);
  64   68  static void Encode64(uint8_t *, uint64_t *, size_t);
  65   69  
  66   70  #if     defined(__amd64)
  67   71  #define SHA512Transform(ctx, in) SHA512TransformBlocks((ctx), (in), 1)
  68   72  #define SHA256Transform(ctx, in) SHA256TransformBlocks((ctx), (in), 1)
  69   73  
  70   74  void SHA512TransformBlocks(SHA2_CTX *ctx, const void *in, size_t num);
  71   75  void SHA256TransformBlocks(SHA2_CTX *ctx, const void *in, size_t num);
  72   76  
↓ open down ↓ 41 lines elided ↑ open up ↑
 114  118  /*
 115  119   * sparc optimization:
 116  120   *
 117  121   * on the sparc, we can load big endian 32-bit data easily.  note that
 118  122   * special care must be taken to ensure the address is 32-bit aligned.
 119  123   * in the interest of speed, we don't check to make sure, since
 120  124   * careful programming can guarantee this for us.
 121  125   */
 122  126  
 123  127  #if     defined(_BIG_ENDIAN)
 124      -
 125  128  #define LOAD_BIG_32(addr)       (*(uint32_t *)(addr))
      129 +#define LOAD_BIG_64(addr)       (*(uint64_t *)(addr))
 126  130  
 127      -#else   /* little endian -- will work on big endian, but slowly */
      131 +#elif   defined(HAVE_HTONL)
      132 +#define LOAD_BIG_32(addr) htonl(*((uint32_t *)(addr)))
      133 +#define LOAD_BIG_64(addr) htonll(*((uint64_t *)(addr)))
 128  134  
      135 +#else
      136 +/* little endian -- will work on big endian, but slowly */
 129  137  #define LOAD_BIG_32(addr)       \
 130  138          (((addr)[0] << 24) | ((addr)[1] << 16) | ((addr)[2] << 8) | (addr)[3])
 131      -#endif
 132      -
 133      -
 134      -#if     defined(_BIG_ENDIAN)
 135      -
 136      -#define LOAD_BIG_64(addr)       (*(uint64_t *)(addr))
 137      -
 138      -#else   /* little endian -- will work on big endian, but slowly */
 139      -
 140  139  #define LOAD_BIG_64(addr)       \
 141  140          (((uint64_t)(addr)[0] << 56) | ((uint64_t)(addr)[1] << 48) |    \
 142  141              ((uint64_t)(addr)[2] << 40) | ((uint64_t)(addr)[3] << 32) | \
 143  142              ((uint64_t)(addr)[4] << 24) | ((uint64_t)(addr)[5] << 16) | \
 144  143              ((uint64_t)(addr)[6] << 8) | (uint64_t)(addr)[7])
 145      -#endif
      144 +#endif  /* _BIG_ENDIAN */
 146  145  
 147  146  
 148  147  #if     !defined(__amd64)
 149  148  /* SHA256 Transform */
 150  149  
 151  150  static void
 152  151  SHA256Transform(SHA2_CTX *ctx, const uint8_t *blk)
 153  152  {
 154  153          uint32_t a = ctx->state.s32[0];
 155  154          uint32_t b = ctx->state.s32[1];
↓ open down ↓ 552 lines elided ↑ open up ↑
 708  707                  ctx->state.s64[1] = 0xbb67ae8584caa73bULL;
 709  708                  ctx->state.s64[2] = 0x3c6ef372fe94f82bULL;
 710  709                  ctx->state.s64[3] = 0xa54ff53a5f1d36f1ULL;
 711  710                  ctx->state.s64[4] = 0x510e527fade682d1ULL;
 712  711                  ctx->state.s64[5] = 0x9b05688c2b3e6c1fULL;
 713  712                  ctx->state.s64[6] = 0x1f83d9abfb41bd6bULL;
 714  713                  ctx->state.s64[7] = 0x5be0cd19137e2179ULL;
 715  714                  break;
 716  715  #ifdef _KERNEL
 717  716          default:
 718      -                cmn_err(CE_PANIC, "sha2_init: "
 719      -                    "failed to find a supported algorithm: 0x%x",
      717 +                cmn_err(CE_PANIC,
      718 +                    "sha2_init: failed to find a supported algorithm: 0x%x",
 720  719                      (uint32_t)mech);
 721  720  
 722  721  #endif /* _KERNEL */
 723  722          }
 724  723  
 725  724          ctx->algotype = mech;
 726  725          ctx->count.c64[0] = ctx->count.c64[1] = 0;
 727  726  }
 728  727  
 729  728  #ifndef _KERNEL
↓ open down ↓ 191 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX