common.h 5.4 KB

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