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

@@ -1,12 +1,10 @@
 /*
  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  * Use is subject to license terms.
  */
 
-#pragma ident   "%Z%%M% %I%     %E% SMI"
-
 /*
  * The basic framework for this code came from the reference
  * implementation for MD5.  That implementation is Copyright (C)
  * 1991-2, RSA Data Security, Inc. Created 1991. All rights reserved.
  *

@@ -27,11 +25,11 @@
  *
  * These notices must be retained in any copies of any part of this
  * documentation and/or software.
  *
  * NOTE: Cleaned-up and optimized, version of SHA1, based on the FIPS 180-1
- * standard, available at http://www.itl.nist.gov/div897/pubs/fip180-1.htm
+ * standard, available at http://www.itl.nist.gov/fipspubs/fip180-1.htm
  * Not as fast as one would like -- further optimizations are encouraged
  * and appreciated.
  */
 
 #include <sys/types.h>

@@ -46,10 +44,15 @@
 #include <stdlib.h>
 #include <errno.h>
 #include <sys/systeminfo.h>
 #endif  /* !_KERNEL */
 
+#ifdef _LITTLE_ENDIAN
+#include <sys/byteorder.h>
+#define HAVE_HTONL
+#endif
+
 static void Encode(uint8_t *, const uint32_t *, size_t);
 
 #if     defined(__sparc)
 
 #define SHA1_TRANSFORM(ctx, in) \

@@ -104,22 +107,11 @@
 #define ROTATE_LEFT(x, n)       \
         (((x) << (n)) | ((x) >> ((sizeof (x) * NBBY)-(n))))
 
 #endif
 
-#if     defined(__GNUC__) && (defined(__i386) || defined(__amd64))
 
-#define HAVE_BSWAP
-
-extern __inline__ uint32_t bswap(uint32_t value)
-{
-        __asm__("bswap %0" : "+r" (value));
-        return (value);
-}
-
-#endif
-
 /*
  * SHA1Init()
  *
  * purpose: initializes the sha1 context and begins and sha1 digest operation
  *   input: SHA1_CTX *  : the context to initializes.

@@ -295,11 +287,11 @@
                                  * Main processing loop - input 8-byte aligned
                                  */
                                 for (; i + 63 < input_len; i += 64) {
                                         SHA1TransformVIS(X0,
                                         /* LINTED E_BAD_PTR_CAST_ALIGN */
-                                            (uint32_t *)&input[i],
+                                            (uint32_t *)&input[i], /* CSTYLED */
                                             &ctx->state[0], VIS);
                                 }
 
                         }
 #ifdef _KERNEL

@@ -453,29 +445,21 @@
  * in the interest of speed, we don't check to make sure, since
  * careful programming can guarantee this for us.
  */
 
 #if     defined(_BIG_ENDIAN)
-
 #define LOAD_BIG_32(addr)       (*(uint32_t *)(addr))
 
-#else   /* !defined(_BIG_ENDIAN) */
+#elif   defined(HAVE_HTONL)
+#define LOAD_BIG_32(addr) htonl(*((uint32_t *)(addr)))
 
-#if     defined(HAVE_BSWAP)
-
-#define LOAD_BIG_32(addr) bswap(*((uint32_t *)(addr)))
-
-#else   /* !defined(HAVE_BSWAP) */
-
+#else
 /* little endian -- will work on big endian, but slowly */
 #define LOAD_BIG_32(addr)       \
         (((addr)[0] << 24) | ((addr)[1] << 16) | ((addr)[2] << 8) | (addr)[3])
+#endif  /* _BIG_ENDIAN */
 
-#endif  /* !defined(HAVE_BSWAP) */
-
-#endif  /* !defined(_BIG_ENDIAN) */
-
 /*
  * SHA1Transform()
  */
 #if     defined(W_ARRAY)
 #define W(n) w[n]

@@ -535,18 +519,18 @@
          * going to be called from inside multithreaded kernelland,
          * this is a good safety check. -- `sha1_consts' will end up in
          * .rodata.
          *
          * unfortunately, loading from an array in this manner hurts
-         * performance under intel.  so, there is a macro,
+         * performance under Intel.  So, there is a macro,
          * SHA1_CONST(), used in SHA1Transform(), that either expands to
          * a reference to this array, or to the actual constant,
          * depending on what platform this code is compiled for.
          */
 
         static const uint32_t sha1_consts[] = {
-                SHA1_CONST_0,   SHA1_CONST_1,   SHA1_CONST_2,   SHA1_CONST_3,
+                SHA1_CONST_0, SHA1_CONST_1, SHA1_CONST_2, SHA1_CONST_3
         };
 
         /*
          * general optimization:
          *

@@ -627,13 +611,14 @@
                 /*LINTED*/
                 w_0  = LOAD_BIG_32(blk +  0);
         }
 #else   /* !defined(__sparc) */
 
-void
+void /* CSTYLED */
 SHA1Transform(SHA1_CTX *ctx, const uint8_t blk[64])
 {
+        /* CSTYLED */
         sha1word a = ctx->state[0];
         sha1word b = ctx->state[1];
         sha1word c = ctx->state[2];
         sha1word d = ctx->state[3];
         sha1word e = ctx->state[4];