common.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #ifndef common_H
  2. #define common_H 1
  3. #if !defined(_MSC_VER) && !defined(DEV_MODE) && 0
  4. # warning *** This is unstable, untested, development code.
  5. # warning It might not compile. It might not work as expected.
  6. # warning It might be totally insecure.
  7. # warning Do not use this except if you are planning to contribute code.
  8. # warning Use releases available at https://download.libsodium.org/libsodium/releases/ instead.
  9. # warning Alternatively, use the "stable" branch in the git repository.
  10. #endif
  11. #if !defined(_MSC_VER) && (!defined(CONFIGURED) || CONFIGURED != 1)
  12. # warning *** The library is being compiled using an undocumented method.
  13. # warning This is not supported. It has not been tested, it might not
  14. # warning work as expected, and performance is likely to be suboptimal.
  15. #endif
  16. #include <stdint.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #define COMPILER_ASSERT(X) (void) sizeof(char[(X) ? 1 : -1])
  20. #ifdef HAVE_TI_MODE
  21. # if defined(__SIZEOF_INT128__)
  22. typedef unsigned __int128 uint128_t;
  23. # else
  24. typedef unsigned uint128_t __attribute__((mode(TI)));
  25. # endif
  26. #endif
  27. #define ROTL32(X, B) rotl32((X), (B))
  28. static inline uint32_t
  29. rotl32(const uint32_t x, const int b)
  30. {
  31. return (x << b) | (x >> (32 - b));
  32. }
  33. #define ROTL64(X, B) rotl64((X), (B))
  34. static inline uint64_t
  35. rotl64(const uint64_t x, const int b)
  36. {
  37. return (x << b) | (x >> (64 - b));
  38. }
  39. #define ROTR32(X, B) rotr32((X), (B))
  40. static inline uint32_t
  41. rotr32(const uint32_t x, const int b)
  42. {
  43. return (x >> b) | (x << (32 - b));
  44. }
  45. #define ROTR64(X, B) rotr64((X), (B))
  46. static inline uint64_t
  47. rotr64(const uint64_t x, const int b)
  48. {
  49. return (x >> b) | (x << (64 - b));
  50. }
  51. #define LOAD64_LE(SRC) load64_le(SRC)
  52. static inline uint64_t
  53. load64_le(const uint8_t src[8])
  54. {
  55. #ifdef NATIVE_LITTLE_ENDIAN
  56. uint64_t w;
  57. memcpy(&w, src, sizeof w);
  58. return w;
  59. #else
  60. uint64_t w = (uint64_t) src[0];
  61. w |= (uint64_t) src[1] << 8;
  62. w |= (uint64_t) src[2] << 16;
  63. w |= (uint64_t) src[3] << 24;
  64. w |= (uint64_t) src[4] << 32;
  65. w |= (uint64_t) src[5] << 40;
  66. w |= (uint64_t) src[6] << 48;
  67. w |= (uint64_t) src[7] << 56;
  68. return w;
  69. #endif
  70. }
  71. #define STORE64_LE(DST, W) store64_le((DST), (W))
  72. static inline void
  73. store64_le(uint8_t dst[8], uint64_t w)
  74. {
  75. #ifdef NATIVE_LITTLE_ENDIAN
  76. memcpy(dst, &w, sizeof w);
  77. #else
  78. dst[0] = (uint8_t) w; w >>= 8;
  79. dst[1] = (uint8_t) w; w >>= 8;
  80. dst[2] = (uint8_t) w; w >>= 8;
  81. dst[3] = (uint8_t) w; w >>= 8;
  82. dst[4] = (uint8_t) w; w >>= 8;
  83. dst[5] = (uint8_t) w; w >>= 8;
  84. dst[6] = (uint8_t) w; w >>= 8;
  85. dst[7] = (uint8_t) w;
  86. #endif
  87. }
  88. #define LOAD32_LE(SRC) load32_le(SRC)
  89. static inline uint32_t
  90. load32_le(const uint8_t src[4])
  91. {
  92. #ifdef NATIVE_LITTLE_ENDIAN
  93. uint32_t w;
  94. memcpy(&w, src, sizeof w);
  95. return w;
  96. #else
  97. uint32_t w = (uint32_t) src[0];
  98. w |= (uint32_t) src[1] << 8;
  99. w |= (uint32_t) src[2] << 16;
  100. w |= (uint32_t) src[3] << 24;
  101. return w;
  102. #endif
  103. }
  104. #define STORE32_LE(DST, W) store32_le((DST), (W))
  105. static inline void
  106. store32_le(uint8_t dst[4], uint32_t w)
  107. {
  108. #ifdef NATIVE_LITTLE_ENDIAN
  109. memcpy(dst, &w, sizeof w);
  110. #else
  111. dst[0] = (uint8_t) w; w >>= 8;
  112. dst[1] = (uint8_t) w; w >>= 8;
  113. dst[2] = (uint8_t) w; w >>= 8;
  114. dst[3] = (uint8_t) w;
  115. #endif
  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. #define STORE64_BE(DST, W) store64_be((DST), (W))
  139. static inline void
  140. store64_be(uint8_t dst[8], uint64_t w)
  141. {
  142. #ifdef NATIVE_BIG_ENDIAN
  143. memcpy(dst, &w, sizeof w);
  144. #else
  145. dst[7] = (uint8_t) w; w >>= 8;
  146. dst[6] = (uint8_t) w; w >>= 8;
  147. dst[5] = (uint8_t) w; w >>= 8;
  148. dst[4] = (uint8_t) w; w >>= 8;
  149. dst[3] = (uint8_t) w; w >>= 8;
  150. dst[2] = (uint8_t) w; w >>= 8;
  151. dst[1] = (uint8_t) w; w >>= 8;
  152. dst[0] = (uint8_t) w;
  153. #endif
  154. }
  155. #define LOAD32_BE(SRC) load32_be(SRC)
  156. static inline uint32_t
  157. load32_be(const uint8_t src[4])
  158. {
  159. #ifdef NATIVE_BIG_ENDIAN
  160. uint32_t w;
  161. memcpy(&w, src, sizeof w);
  162. return w;
  163. #else
  164. uint32_t w = (uint32_t) src[3];
  165. w |= (uint32_t) src[2] << 8;
  166. w |= (uint32_t) src[1] << 16;
  167. w |= (uint32_t) src[0] << 24;
  168. return w;
  169. #endif
  170. }
  171. #define STORE32_BE(DST, W) store32_be((DST), (W))
  172. static inline void
  173. store32_be(uint8_t dst[4], uint32_t w)
  174. {
  175. #ifdef NATIVE_BIG_ENDIAN
  176. memcpy(dst, &w, sizeof w);
  177. #else
  178. dst[3] = (uint8_t) w; w >>= 8;
  179. dst[2] = (uint8_t) w; w >>= 8;
  180. dst[1] = (uint8_t) w; w >>= 8;
  181. dst[0] = (uint8_t) w;
  182. #endif
  183. }
  184. #define XOR_BUF(OUT, IN, N) xor_buf((OUT), (IN), (N))
  185. static inline void
  186. xor_buf(unsigned char *out, const unsigned char *in, size_t n)
  187. {
  188. size_t i;
  189. for (i = 0; i < n; i++) {
  190. out[i] ^= in[i];
  191. }
  192. }
  193. #if !defined(__clang__) && !defined(__GNUC__)
  194. # ifdef __attribute__
  195. # undef __attribute__
  196. # endif
  197. # define __attribute__(a)
  198. #endif
  199. #ifndef CRYPTO_ALIGN
  200. # if defined(__INTEL_COMPILER) || defined(_MSC_VER)
  201. # define CRYPTO_ALIGN(x) __declspec(align(x))
  202. # else
  203. # define CRYPTO_ALIGN(x) __attribute__ ((aligned(x)))
  204. # endif
  205. #endif
  206. #if defined(_MSC_VER) && \
  207. (defined(_M_X64) || defined(_M_AMD64) || defined(_M_IX86))
  208. # include <intrin.h>
  209. # define HAVE_INTRIN_H 1
  210. # define HAVE_MMINTRIN_H 1
  211. # define HAVE_EMMINTRIN_H 1
  212. # define HAVE_PMMINTRIN_H 1
  213. # define HAVE_TMMINTRIN_H 1
  214. # define HAVE_SMMINTRIN_H 1
  215. # define HAVE_AVXINTRIN_H 1
  216. # if _MSC_VER >= 1600
  217. # define HAVE_WMMINTRIN_H 1
  218. # endif
  219. # if _MSC_VER >= 1700 && defined(_M_X64)
  220. # define HAVE_AVX2INTRIN_H 1
  221. # endif
  222. #elif defined(HAVE_INTRIN_H)
  223. # include <intrin.h>
  224. #endif
  225. #ifdef HAVE_LIBCTGRIND
  226. extern void ct_poison (const void *, size_t);
  227. extern void ct_unpoison(const void *, size_t);
  228. # define POISON(X, L) ct_poison((X), (L))
  229. # define UNPOISON(X, L) ct_unpoison((X), (L))
  230. #else
  231. # define POISON(X, L) (void) 0
  232. # define UNPOISON(X, L) (void) 0
  233. #endif
  234. #endif