Search Code
About crypto

Published 29 Dec 2009.

Last change message: N/A

Import this program

crypto

Published 29 Dec 2009, by   user Anders Rundgren   tag No tags
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers _shacommon.h Source File

_shacommon.h

00001 #define DATA_ORDER_IS_BIG_ENDIAN
00002 
00003 /*
00004  * Engage compiler specific rotate intrinsic function if available.
00005  */
00006 #undef ROTATE
00007 #ifndef PEDANTIC
00008 # if defined(_MSC_VER) || defined(__ICC)
00009 #  define ROTATE(a,n)   _lrotl(a,n)
00010 # elif defined(__MWERKS__)
00011 #  if defined(__POWERPC__)
00012 #   define ROTATE(a,n)  __rlwinm(a,n,0,31)
00013 #  elif defined(__MC68K__)
00014     /* Motorola specific tweak. <appro@fy.chalmers.se> */
00015 #   define ROTATE(a,n)  ( n<24 ? __rol(a,n) : __ror(a,32-n) )
00016 #  else
00017 #   define ROTATE(a,n)  __rol(a,n)
00018 #  endif
00019 # elif defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
00020   /*
00021    * Some GNU C inline assembler templates. Note that these are
00022    * rotates by *constant* number of bits! But that's exactly
00023    * what we need here...
00024    *                    <appro@fy.chalmers.se>
00025    */
00026 #  if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
00027 #   define ROTATE(a,n)  ({ register unsigned int ret;   \
00028                 asm (           \
00029                 "roll %1,%0"        \
00030                 : "=r"(ret)     \
00031                 : "I"(n), "0"(a)    \
00032                 : "cc");        \
00033                ret;             \
00034             })
00035 #  elif defined(_ARCH_PPC) || defined(_ARCH_PPC64) || \
00036     defined(__powerpc) || defined(__ppc__) || defined(__powerpc64__)
00037 #   define ROTATE(a,n)  ({ register unsigned int ret;   \
00038                 asm (           \
00039                 "rlwinm %0,%1,%2,0,31"  \
00040                 : "=r"(ret)     \
00041                 : "r"(a), "I"(n));  \
00042                ret;             \
00043             })
00044 #  elif defined(__s390x__)
00045 #   define ROTATE(a,n) ({ register unsigned int ret;    \
00046                 asm ("rll %0,%1,%2" \
00047                 : "=r"(ret)     \
00048                 : "r"(a), "I"(n));  \
00049               ret;              \
00050             })
00051 #  endif
00052 # endif
00053 #endif /* PEDANTIC */
00054 
00055 #ifndef ROTATE
00056 #define ROTATE(a,n)     (((a)<<(n))|(((a)&0xffffffff)>>(32-(n))))
00057 #endif
00058 
00059 #if defined(DATA_ORDER_IS_BIG_ENDIAN)
00060 
00061 #ifndef PEDANTIC
00062 # if defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
00063 #  if ((defined(__i386) || defined(__i386__)) && !defined(I386_ONLY)) || \
00064       (defined(__x86_64) || defined(__x86_64__))
00065 #   if !defined(B_ENDIAN)
00066     /*
00067      * This gives ~30-40% performance improvement in SHA-256 compiled
00068      * with gcc [on P4]. Well, first macro to be frank. We can pull
00069      * this trick on x86* platforms only, because these CPUs can fetch
00070      * unaligned data without raising an exception.
00071      */
00072 #   define HOST_c2l(c,l)    ({ unsigned int r=*((const unsigned int *)(c)); \
00073                    asm ("bswapl %0":"=r"(r):"0"(r));    \
00074                    (c)+=4; (l)=r;           })
00075 #   define HOST_l2c(l,c)    ({ unsigned int r=(l);          \
00076                    asm ("bswapl %0":"=r"(r):"0"(r));    \
00077                    *((unsigned int *)(c))=r; (c)+=4; r; })
00078 #   endif
00079 #  endif
00080 # endif
00081 #endif
00082 #if defined(__s390__) || defined(__s390x__)
00083 # define HOST_c2l(c,l) ((l)=*((const unsigned int *)(c)), (c)+=4, (l))
00084 # define HOST_l2c(l,c) (*((unsigned int *)(c))=(l), (c)+=4, (l))
00085 #endif
00086 
00087 #ifndef HOST_c2l
00088 #define HOST_c2l(c,l)   (l =(((unsigned long)(*((c)++)))<<24),      \
00089              l|=(((unsigned long)(*((c)++)))<<16),      \
00090              l|=(((unsigned long)(*((c)++)))<< 8),      \
00091              l|=(((unsigned long)(*((c)++)))    ),      \
00092              l)
00093 #endif
00094 #ifndef HOST_l2c
00095 #define HOST_l2c(l,c)   (*((c)++)=(unsigned char)(((l)>>24)&0xff),  \
00096              *((c)++)=(unsigned char)(((l)>>16)&0xff),  \
00097              *((c)++)=(unsigned char)(((l)>> 8)&0xff),  \
00098              *((c)++)=(unsigned char)(((l)    )&0xff),  \
00099              l)
00100 #endif
00101 
00102 #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
00103 
00104 #ifndef PEDANTIC
00105 # if defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
00106 #  if defined(__s390x__)
00107 #   define HOST_c2l(c,l)    ({ asm ("lrv    %0,0(%1)"       \
00108                     :"=r"(l) : "r"(c));     \
00109                    (c)+=4; (l);             })
00110 #   define HOST_l2c(l,c)    ({ asm ("strv   %0,0(%1)"       \
00111                     : : "r"(l),"r"(c) : "memory");  \
00112                    (c)+=4; (l);             })
00113 #  endif
00114 # endif
00115 #endif
00116 #if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
00117 # ifndef B_ENDIAN
00118    /* See comment in DATA_ORDER_IS_BIG_ENDIAN section. */
00119 #  define HOST_c2l(c,l) ((l)=*((const unsigned int *)(c)), (c)+=4, l)
00120 #  define HOST_l2c(l,c) (*((unsigned int *)(c))=(l), (c)+=4, l)
00121 # endif
00122 #endif
00123 
00124 #ifndef HOST_c2l
00125 #define HOST_c2l(c,l)   (l =(((unsigned long)(*((c)++)))    ),      \
00126              l|=(((unsigned long)(*((c)++)))<< 8),      \
00127              l|=(((unsigned long)(*((c)++)))<<16),      \
00128              l|=(((unsigned long)(*((c)++)))<<24),      \
00129              l)
00130 #endif
00131 #ifndef HOST_l2c
00132 #define HOST_l2c(l,c)   (*((c)++)=(unsigned char)(((l)    )&0xff),  \
00133              *((c)++)=(unsigned char)(((l)>> 8)&0xff),  \
00134              *((c)++)=(unsigned char)(((l)>>16)&0xff),  \
00135              *((c)++)=(unsigned char)(((l)>>24)&0xff),  \
00136              l)
00137 #endif
00138 
00139 #endif