1 #ifndef common_H 2 #define common_H 1 3 4 #include <stdint.h> 5 #include <stdlib.h> 6 #include <string.h> 7 8 #define COMPILER_ASSERT(X) (void) sizeof(char[(X) ? 1 : -1]) 9 10 #ifdef HAVE_TI_MODE 11 # if defined(__SIZEOF_INT128__) 12 typedef unsigned __int128 uint128_t; 13 # else 14 typedef unsigned uint128_t __attribute__((mode(TI))); 15 # endif 16 #endif 17 18 #define ROTL32(X, B) rotl32((X), (B)) 19 static inline uint32_t 20 rotl32(const uint32_t x, const int b) 21 { 22 return (x << b) | (x >> (32 - b)); 23 } 24 25 #define ROTL64(X, B) rotl64((X), (B)) 26 static inline uint64_t 27 rotl64(const uint64_t x, const int b) 28 { 29 return (x << b) | (x >> (64 - b)); 30 } 31 32 #define ROTR32(X, B) rotr32((X), (B)) 33 static inline uint32_t 34 rotr32(const uint32_t x, const int b) 35 { 36 return (x >> b) | (x << (32 - b)); 37 } 38 39 #define ROTR64(X, B) rotr64((X), (B)) 40 static inline uint64_t 41 rotr64(const uint64_t x, const int b) 42 { 43 return (x >> b) | (x << (64 - b)); 44 } 45 46 #define LOAD64_LE(SRC) load64_le(SRC) 47 static inline uint64_t 48 load64_le(const uint8_t src[8]) 49 { 50 #ifdef NATIVE_LITTLE_ENDIAN 51 uint64_t w; 52 memcpy(&w, src, sizeof w); 53 return w; 54 #else 55 uint64_t w = (uint64_t) src[0]; 56 w |= (uint64_t) src[1] << 8; 57 w |= (uint64_t) src[2] << 16; 58 w |= (uint64_t) src[3] << 24; 59 w |= (uint64_t) src[4] << 32; 60 w |= (uint64_t) src[5] << 40; 61 w |= (uint64_t) src[6] << 48; 62 w |= (uint64_t) src[7] << 56; 63 return w; 64 #endif 65 } 66 67 #define STORE64_LE(DST, W) store64_le((DST), (W)) 68 static inline void 69 store64_le(uint8_t dst[8], uint64_t w) 70 { 71 #ifdef NATIVE_LITTLE_ENDIAN 72 memcpy(dst, &w, sizeof w); 73 #else 74 dst[0] = (uint8_t) w; w >>= 8; 75 dst[1] = (uint8_t) w; w >>= 8; 76 dst[2] = (uint8_t) w; w >>= 8; 77 dst[3] = (uint8_t) w; w >>= 8; 78 dst[4] = (uint8_t) w; w >>= 8; 79 dst[5] = (uint8_t) w; w >>= 8; 80 dst[6] = (uint8_t) w; w >>= 8; 81 dst[7] = (uint8_t) w; 82 #endif 83 } 84 85 #define LOAD32_LE(SRC) load32_le(SRC) 86 static inline uint32_t 87 load32_le(const uint8_t src[4]) 88 { 89 #ifdef NATIVE_LITTLE_ENDIAN 90 uint32_t w; 91 memcpy(&w, src, sizeof w); 92 return w; 93 #else 94 uint32_t w = (uint32_t) src[0]; 95 w |= (uint32_t) src[1] << 8; 96 w |= (uint32_t) src[2] << 16; 97 w |= (uint32_t) src[3] << 24; 98 return w; 99 #endif 100 } 101 102 #define STORE32_LE(DST, W) store32_le((DST), (W)) 103 static inline void 104 store32_le(uint8_t dst[4], uint32_t w) 105 { 106 #ifdef NATIVE_LITTLE_ENDIAN 107 memcpy(dst, &w, sizeof w); 108 #else 109 dst[0] = (uint8_t) w; w >>= 8; 110 dst[1] = (uint8_t) w; w >>= 8; 111 dst[2] = (uint8_t) w; w >>= 8; 112 dst[3] = (uint8_t) w; 113 #endif 114 } 115 116 /* ----- */ 117 118 #define LOAD64_BE(SRC) load64_be(SRC) 119 static inline uint64_t 120 load64_be(const uint8_t src[8]) 121 { 122 #ifdef NATIVE_BIG_ENDIAN 123 uint64_t w; 124 memcpy(&w, src, sizeof w); 125 return w; 126 #else 127 uint64_t w = (uint64_t) src[7]; 128 w |= (uint64_t) src[6] << 8; 129 w |= (uint64_t) src[5] << 16; 130 w |= (uint64_t) src[4] << 24; 131 w |= (uint64_t) src[3] << 32; 132 w |= (uint64_t) src[2] << 40; 133 w |= (uint64_t) src[1] << 48; 134 w |= (uint64_t) src[0] << 56; 135 return w; 136 #endif 137 } 138 139 #define STORE64_BE(DST, W) store64_be((DST), (W)) 140 static inline void 141 store64_be(uint8_t dst[8], uint64_t w) 142 { 143 #ifdef NATIVE_BIG_ENDIAN 144 memcpy(dst, &w, sizeof w); 145 #else 146 dst[7] = (uint8_t) w; w >>= 8; 147 dst[6] = (uint8_t) w; w >>= 8; 148 dst[5] = (uint8_t) w; w >>= 8; 149 dst[4] = (uint8_t) w; w >>= 8; 150 dst[3] = (uint8_t) w; w >>= 8; 151 dst[2] = (uint8_t) w; w >>= 8; 152 dst[1] = (uint8_t) w; w >>= 8; 153 dst[0] = (uint8_t) w; 154 #endif 155 } 156 157 #define LOAD32_BE(SRC) load32_be(SRC) 158 static inline uint32_t 159 load32_be(const uint8_t src[4]) 160 { 161 #ifdef NATIVE_BIG_ENDIAN 162 uint32_t w; 163 memcpy(&w, src, sizeof w); 164 return w; 165 #else 166 uint32_t w = (uint32_t) src[3]; 167 w |= (uint32_t) src[2] << 8; 168 w |= (uint32_t) src[1] << 16; 169 w |= (uint32_t) src[0] << 24; 170 return w; 171 #endif 172 } 173 174 #define STORE32_BE(DST, W) store32_be((DST), (W)) 175 static inline void 176 store32_be(uint8_t dst[4], uint32_t w) 177 { 178 #ifdef NATIVE_BIG_ENDIAN 179 memcpy(dst, &w, sizeof w); 180 #else 181 dst[3] = (uint8_t) w; w >>= 8; 182 dst[2] = (uint8_t) w; w >>= 8; 183 dst[1] = (uint8_t) w; w >>= 8; 184 dst[0] = (uint8_t) w; 185 #endif 186 } 187 188 #define XOR_BUF(OUT, IN, N) xor_buf((OUT), (IN), (N)) 189 static inline void 190 xor_buf(unsigned char *out, const unsigned char *in, size_t n) 191 { 192 size_t i; 193 194 for (i = 0; i < n; i++) { 195 out[i] ^= in[i]; 196 } 197 } 198 199 #if !defined(__clang__) && !defined(__GNUC__) 200 # ifdef __attribute__ 201 # undef __attribute__ 202 # endif 203 # define __attribute__(a) 204 #endif 205 206 #ifndef CRYPTO_ALIGN 207 # if defined(__INTEL_COMPILER) || defined(_MSC_VER) 208 # define CRYPTO_ALIGN(x) __declspec(align(x)) 209 # else 210 # define CRYPTO_ALIGN(x) __attribute__ ((aligned(x))) 211 # endif 212 #endif 213 214 #if defined(_MSC_VER) && \ 215 (defined(_M_X64) || defined(_M_AMD64) || defined(_M_IX86)) 216 217 # include <intrin.h> 218 219 # define HAVE_INTRIN_H 1 220 # define HAVE_MMINTRIN_H 1 221 # define HAVE_EMMINTRIN_H 1 222 # define HAVE_PMMINTRIN_H 1 223 # define HAVE_TMMINTRIN_H 1 224 # define HAVE_SMMINTRIN_H 1 225 # define HAVE_AVXINTRIN_H 1 226 # if _MSC_VER >= 1600 227 # define HAVE_WMMINTRIN_H 1 228 # endif 229 # if _MSC_VER >= 1700 && defined(_M_X64) 230 # define HAVE_AVX2INTRIN_H 1 231 # endif 232 #elif defined(HAVE_INTRIN_H) 233 # include <intrin.h> 234 #endif 235 236 #ifdef HAVE_LIBCTGRIND 237 extern void ct_poison (const void *, size_t); 238 extern void ct_unpoison(const void *, size_t); 239 # define POISON(X, L) ct_poison((X), (L)) 240 # define UNPOISON(X, L) ct_unpoison((X), (L)) 241 #else 242 # define POISON(X, L) (void) 0 243 # define UNPOISON(X, L) (void) 0 244 #endif 245 246 #endif 247