security/nss/lib/freebl/sha_fast.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 #ifndef _SHA_FAST_H_
     6 #define _SHA_FAST_H_
     8 #include "prlong.h"
    10 #define SHA1_INPUT_LEN 64
    12 #if defined(IS_64) && !defined(__sparc) 
    13 typedef PRUint64 SHA_HW_t;
    14 #define SHA1_USING_64_BIT 1
    15 #else
    16 typedef PRUint32 SHA_HW_t;
    17 #endif
    19 struct SHA1ContextStr {
    20   union {
    21     PRUint32 w[16];		/* input buffer */
    22     PRUint8  b[64];
    23   } u;
    24   PRUint64 size;          	/* count of hashed bytes. */
    25   SHA_HW_t H[22];		/* 5 state variables, 16 tmp values, 1 extra */
    26 };
    28 #if defined(_MSC_VER)
    29 #include <stdlib.h>
    30 #if defined(IS_LITTLE_ENDIAN) 
    31 #if (_MSC_VER >= 1300)
    32 #pragma intrinsic(_byteswap_ulong)
    33 #define SHA_HTONL(x) _byteswap_ulong(x)
    34 #elif defined(NSS_X86_OR_X64)
    35 #ifndef FORCEINLINE
    36 #if (_MSC_VER >= 1200)
    37 #define FORCEINLINE __forceinline
    38 #else
    39 #define FORCEINLINE __inline
    40 #endif /* _MSC_VER */
    41 #endif /* !defined FORCEINLINE */
    42 #define FASTCALL __fastcall
    44 static FORCEINLINE PRUint32 FASTCALL 
    45 swap4b(PRUint32 dwd) 
    46 {
    47     __asm {
    48     	mov   eax,dwd
    49 	bswap eax
    50     }
    51 }
    53 #define SHA_HTONL(x) swap4b(x)
    54 #endif /* NSS_X86_OR_X64 */
    55 #endif /* IS_LITTLE_ENDIAN */
    57 #pragma intrinsic (_lrotr, _lrotl) 
    58 #define SHA_ROTL(x,n) _lrotl(x,n)
    59 #define SHA_ROTL_IS_DEFINED 1
    60 #endif /* _MSC_VER */
    62 #if defined(__GNUC__) 
    63 /* __x86_64__  and __x86_64 are defined by GCC on x86_64 CPUs */
    64 #if defined( SHA1_USING_64_BIT )
    65 static __inline__ PRUint64 SHA_ROTL(PRUint64 x, PRUint32 n)
    66 {
    67     PRUint32 t = (PRUint32)x;
    68     return ((t << n) | (t >> (32 - n)));
    69 }
    70 #else 
    71 static __inline__ PRUint32 SHA_ROTL(PRUint32 t, PRUint32 n)
    72 {
    73     return ((t << n) | (t >> (32 - n)));
    74 }
    75 #endif
    76 #define SHA_ROTL_IS_DEFINED 1
    78 #if defined(NSS_X86_OR_X64)
    79 static __inline__ PRUint32 swap4b(PRUint32 value)
    80 {
    81     __asm__("bswap %0" : "+r" (value));
    82     return (value);
    83 }
    84 #define SHA_HTONL(x) swap4b(x)
    86 #elif defined(__thumb2__) || \
    87       (!defined(__thumb__) && \
    88        (defined(__ARM_ARCH_6__) || \
    89         defined(__ARM_ARCH_6J__) || \
    90         defined(__ARM_ARCH_6K__) || \
    91         defined(__ARM_ARCH_6Z__) || \
    92         defined(__ARM_ARCH_6ZK__) || \
    93         defined(__ARM_ARCH_6T2__) || \
    94         defined(__ARM_ARCH_7__) || \
    95         defined(__ARM_ARCH_7A__) || \
    96         defined(__ARM_ARCH_7R__)))
    97 static __inline__ PRUint32 swap4b(PRUint32 value)
    98 {
    99     PRUint32 ret;
   100     __asm__("rev %0, %1" : "=r" (ret) : "r"(value));
   101     return ret;
   102 }
   103 #define SHA_HTONL(x) swap4b(x)
   105 #endif /* x86 family */
   107 #endif /* __GNUC__ */
   109 #if !defined(SHA_ROTL_IS_DEFINED)
   110 #define SHA_NEED_TMP_VARIABLE 1
   111 #define SHA_ROTL(X,n) (tmp = (X), ((tmp) << (n)) | ((tmp) >> (32-(n))))
   112 #endif
   114 #if defined(NSS_X86_OR_X64)
   115 #define SHA_ALLOW_UNALIGNED_ACCESS 1
   116 #endif
   118 #if !defined(SHA_HTONL)
   119 #define SHA_MASK      0x00FF00FF
   120 #if defined(IS_LITTLE_ENDIAN)
   121 #undef  SHA_NEED_TMP_VARIABLE 
   122 #define SHA_NEED_TMP_VARIABLE 1
   123 #define SHA_HTONL(x)  (tmp = (x), tmp = (tmp << 16) | (tmp >> 16), \
   124                        ((tmp & SHA_MASK) << 8) | ((tmp >> 8) & SHA_MASK))
   125 #else
   126 #define SHA_HTONL(x)  (x)
   127 #endif
   128 #endif
   130 #define SHA_BYTESWAP(x) x = SHA_HTONL(x)
   132 #define SHA_STORE(n) ((PRUint32*)hashout)[n] = SHA_HTONL(ctx->H[n])
   133 #if defined(SHA_ALLOW_UNALIGNED_ACCESS)
   134 #define SHA_STORE_RESULT \
   135   SHA_STORE(0); \
   136   SHA_STORE(1); \
   137   SHA_STORE(2); \
   138   SHA_STORE(3); \
   139   SHA_STORE(4);
   141 #elif defined(IS_LITTLE_ENDIAN) || defined( SHA1_USING_64_BIT )
   142 #define SHA_STORE_RESULT \
   143   if (!((ptrdiff_t)hashout % sizeof(PRUint32))) { \
   144     SHA_STORE(0); \
   145     SHA_STORE(1); \
   146     SHA_STORE(2); \
   147     SHA_STORE(3); \
   148     SHA_STORE(4); \
   149   } else { \
   150     tmpbuf[0] = SHA_HTONL(ctx->H[0]); \
   151     tmpbuf[1] = SHA_HTONL(ctx->H[1]); \
   152     tmpbuf[2] = SHA_HTONL(ctx->H[2]); \
   153     tmpbuf[3] = SHA_HTONL(ctx->H[3]); \
   154     tmpbuf[4] = SHA_HTONL(ctx->H[4]); \
   155     memcpy(hashout, tmpbuf, SHA1_LENGTH); \
   156   }
   158 #else
   159 #define SHA_STORE_RESULT \
   160   if (!((ptrdiff_t)hashout % sizeof(PRUint32))) { \
   161     SHA_STORE(0); \
   162     SHA_STORE(1); \
   163     SHA_STORE(2); \
   164     SHA_STORE(3); \
   165     SHA_STORE(4); \
   166   } else { \
   167     memcpy(hashout, ctx->H, SHA1_LENGTH); \
   168   }
   169 #endif 
   171 #endif /* _SHA_FAST_H_ */

mercurial