constant_time_impl.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /**
  2. * Constant-time functions
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. */
  7. #ifndef MBEDTLS_CONSTANT_TIME_IMPL_H
  8. #define MBEDTLS_CONSTANT_TIME_IMPL_H
  9. #include <stddef.h>
  10. #include "common.h"
  11. #if defined(MBEDTLS_BIGNUM_C)
  12. #include "mbedtls/bignum.h"
  13. #endif
  14. /*
  15. * To improve readability of constant_time_internal.h, the static inline
  16. * definitions are here, and constant_time_internal.h has only the declarations.
  17. *
  18. * This results in duplicate declarations of the form:
  19. * static inline void f(); // from constant_time_internal.h
  20. * static inline void f() { ... } // from constant_time_impl.h
  21. * when constant_time_internal.h is included.
  22. *
  23. * This appears to behave as if the declaration-without-definition was not present
  24. * (except for warnings if gcc -Wredundant-decls or similar is used).
  25. *
  26. * Disable -Wredundant-decls so that gcc does not warn about this. This is re-enabled
  27. * at the bottom of this file.
  28. */
  29. #if defined(MBEDTLS_COMPILER_IS_GCC) && (__GNUC__ > 4)
  30. #pragma GCC diagnostic push
  31. #pragma GCC diagnostic ignored "-Wredundant-decls"
  32. #endif
  33. /* Disable asm under Memsan because it confuses Memsan and generates false errors.
  34. *
  35. * We also disable under Valgrind by default, because it's more useful
  36. * for Valgrind to test the plain C implementation. MBEDTLS_TEST_CONSTANT_FLOW_ASM //no-check-names
  37. * may be set to permit building asm under Valgrind.
  38. */
  39. #if defined(MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN) || \
  40. (defined(MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND) && !defined(MBEDTLS_TEST_CONSTANT_FLOW_ASM)) //no-check-names
  41. #define MBEDTLS_CT_NO_ASM
  42. #elif defined(__has_feature)
  43. #if __has_feature(memory_sanitizer)
  44. #define MBEDTLS_CT_NO_ASM
  45. #endif
  46. #endif
  47. /* armcc5 --gnu defines __GNUC__ but doesn't support GNU's extended asm */
  48. #if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && (!defined(__ARMCC_VERSION) || \
  49. __ARMCC_VERSION >= 6000000) && !defined(MBEDTLS_CT_NO_ASM)
  50. #define MBEDTLS_CT_ASM
  51. #if (defined(__arm__) || defined(__thumb__) || defined(__thumb2__))
  52. #define MBEDTLS_CT_ARM_ASM
  53. #elif defined(__aarch64__)
  54. #define MBEDTLS_CT_AARCH64_ASM
  55. #elif defined(__amd64__) || defined(__x86_64__)
  56. #define MBEDTLS_CT_X86_64_ASM
  57. #elif defined(__i386__)
  58. #define MBEDTLS_CT_X86_ASM
  59. #endif
  60. #endif
  61. #define MBEDTLS_CT_SIZE (sizeof(mbedtls_ct_uint_t) * 8)
  62. /* ============================================================================
  63. * Core const-time primitives
  64. */
  65. /* Ensure that the compiler cannot know the value of x (i.e., cannot optimise
  66. * based on its value) after this function is called.
  67. *
  68. * If we are not using assembly, this will be fairly inefficient, so its use
  69. * should be minimised.
  70. */
  71. #if !defined(MBEDTLS_CT_ASM)
  72. extern volatile mbedtls_ct_uint_t mbedtls_ct_zero;
  73. #endif
  74. /**
  75. * \brief Ensure that a value cannot be known at compile time.
  76. *
  77. * \param x The value to hide from the compiler.
  78. * \return The same value that was passed in, such that the compiler
  79. * cannot prove its value (even for calls of the form
  80. * x = mbedtls_ct_compiler_opaque(1), x will be unknown).
  81. *
  82. * \note This is mainly used in constructing mbedtls_ct_condition_t
  83. * values and performing operations over them, to ensure that
  84. * there is no way for the compiler to ever know anything about
  85. * the value of an mbedtls_ct_condition_t.
  86. */
  87. static inline mbedtls_ct_uint_t mbedtls_ct_compiler_opaque(mbedtls_ct_uint_t x)
  88. {
  89. #if defined(MBEDTLS_CT_ASM)
  90. asm volatile ("" : [x] "+r" (x) :);
  91. return x;
  92. #else
  93. return x ^ mbedtls_ct_zero;
  94. #endif
  95. }
  96. /*
  97. * Selecting unified syntax is needed for gcc, and harmless on clang.
  98. *
  99. * This is needed because on Thumb 1, condition flags are always set, so
  100. * e.g. "negs" is supported but "neg" is not (on Thumb 2, both exist).
  101. *
  102. * Under Thumb 1 unified syntax, only the "negs" form is accepted, and
  103. * under divided syntax, only the "neg" form is accepted. clang only
  104. * supports unified syntax.
  105. *
  106. * On Thumb 2 and Arm, both compilers are happy with the "s" suffix,
  107. * although we don't actually care about setting the flags.
  108. *
  109. * For old versions of gcc (see #8516 for details), restore divided
  110. * syntax afterwards - otherwise old versions of gcc seem to apply
  111. * unified syntax globally, which breaks other asm code.
  112. */
  113. #if defined(MBEDTLS_COMPILER_IS_GCC) && defined(__thumb__) && !defined(__thumb2__) && \
  114. (__GNUC__ < 11) && !defined(__ARM_ARCH_2__)
  115. #define RESTORE_ASM_SYNTAX ".syntax divided \n\t"
  116. #else
  117. #define RESTORE_ASM_SYNTAX
  118. #endif
  119. /* Convert a number into a condition in constant time. */
  120. static inline mbedtls_ct_condition_t mbedtls_ct_bool(mbedtls_ct_uint_t x)
  121. {
  122. /*
  123. * Define mask-generation code that, as far as possible, will not use branches or conditional instructions.
  124. *
  125. * For some platforms / type sizes, we define assembly to assure this.
  126. *
  127. * Otherwise, we define a plain C fallback which (in May 2023) does not get optimised into
  128. * conditional instructions or branches by trunk clang, gcc, or MSVC v19.
  129. */
  130. #if defined(MBEDTLS_CT_AARCH64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
  131. mbedtls_ct_uint_t s;
  132. asm volatile ("neg %x[s], %x[x] \n\t"
  133. "orr %x[x], %x[s], %x[x] \n\t"
  134. "asr %x[x], %x[x], 63 \n\t"
  135. :
  136. [s] "=&r" (s),
  137. [x] "+&r" (x)
  138. :
  139. :
  140. );
  141. return (mbedtls_ct_condition_t) x;
  142. #elif defined(MBEDTLS_CT_ARM_ASM) && defined(MBEDTLS_CT_SIZE_32)
  143. uint32_t s;
  144. asm volatile (".syntax unified \n\t"
  145. "negs %[s], %[x] \n\t"
  146. "orrs %[x], %[x], %[s] \n\t"
  147. "asrs %[x], %[x], #31 \n\t"
  148. RESTORE_ASM_SYNTAX
  149. :
  150. [s] "=&l" (s),
  151. [x] "+&l" (x)
  152. :
  153. :
  154. "cc" /* clobbers flag bits */
  155. );
  156. return (mbedtls_ct_condition_t) x;
  157. #elif defined(MBEDTLS_CT_X86_64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
  158. uint64_t s;
  159. asm volatile ("mov %[x], %[s] \n\t"
  160. "neg %[s] \n\t"
  161. "or %[x], %[s] \n\t"
  162. "sar $63, %[s] \n\t"
  163. :
  164. [s] "=&a" (s)
  165. :
  166. [x] "D" (x)
  167. :
  168. );
  169. return (mbedtls_ct_condition_t) s;
  170. #elif defined(MBEDTLS_CT_X86_ASM) && defined(MBEDTLS_CT_SIZE_32)
  171. uint32_t s;
  172. asm volatile ("mov %[x], %[s] \n\t"
  173. "neg %[s] \n\t"
  174. "or %[s], %[x] \n\t"
  175. "sar $31, %[x] \n\t"
  176. :
  177. [s] "=&c" (s),
  178. [x] "+&a" (x)
  179. :
  180. :
  181. );
  182. return (mbedtls_ct_condition_t) x;
  183. #else
  184. const mbedtls_ct_uint_t xo = mbedtls_ct_compiler_opaque(x);
  185. #if defined(_MSC_VER)
  186. /* MSVC has a warning about unary minus on unsigned, but this is
  187. * well-defined and precisely what we want to do here */
  188. #pragma warning( push )
  189. #pragma warning( disable : 4146 )
  190. #endif
  191. // y is negative (i.e., top bit set) iff x is non-zero
  192. mbedtls_ct_int_t y = (-xo) | -(xo >> 1);
  193. // extract only the sign bit of y so that y == 1 (if x is non-zero) or 0 (if x is zero)
  194. y = (((mbedtls_ct_uint_t) y) >> (MBEDTLS_CT_SIZE - 1));
  195. // -y has all bits set (if x is non-zero), or all bits clear (if x is zero)
  196. return (mbedtls_ct_condition_t) (-y);
  197. #if defined(_MSC_VER)
  198. #pragma warning( pop )
  199. #endif
  200. #endif
  201. }
  202. static inline mbedtls_ct_uint_t mbedtls_ct_if(mbedtls_ct_condition_t condition,
  203. mbedtls_ct_uint_t if1,
  204. mbedtls_ct_uint_t if0)
  205. {
  206. #if defined(MBEDTLS_CT_AARCH64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
  207. asm volatile ("and %x[if1], %x[if1], %x[condition] \n\t"
  208. "mvn %x[condition], %x[condition] \n\t"
  209. "and %x[condition], %x[condition], %x[if0] \n\t"
  210. "orr %x[condition], %x[if1], %x[condition]"
  211. :
  212. [condition] "+&r" (condition),
  213. [if1] "+&r" (if1)
  214. :
  215. [if0] "r" (if0)
  216. :
  217. );
  218. return (mbedtls_ct_uint_t) condition;
  219. #elif defined(MBEDTLS_CT_ARM_ASM) && defined(MBEDTLS_CT_SIZE_32)
  220. asm volatile (".syntax unified \n\t"
  221. "ands %[if1], %[if1], %[condition] \n\t"
  222. "mvns %[condition], %[condition] \n\t"
  223. "ands %[condition], %[condition], %[if0] \n\t"
  224. "orrs %[condition], %[if1], %[condition] \n\t"
  225. RESTORE_ASM_SYNTAX
  226. :
  227. [condition] "+&l" (condition),
  228. [if1] "+&l" (if1)
  229. :
  230. [if0] "l" (if0)
  231. :
  232. "cc"
  233. );
  234. return (mbedtls_ct_uint_t) condition;
  235. #elif defined(MBEDTLS_CT_X86_64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
  236. asm volatile ("and %[condition], %[if1] \n\t"
  237. "not %[condition] \n\t"
  238. "and %[condition], %[if0] \n\t"
  239. "or %[if1], %[if0] \n\t"
  240. :
  241. [condition] "+&D" (condition),
  242. [if1] "+&S" (if1),
  243. [if0] "+&a" (if0)
  244. :
  245. :
  246. );
  247. return if0;
  248. #elif defined(MBEDTLS_CT_X86_ASM) && defined(MBEDTLS_CT_SIZE_32)
  249. asm volatile ("and %[condition], %[if1] \n\t"
  250. "not %[condition] \n\t"
  251. "and %[if0], %[condition] \n\t"
  252. "or %[condition], %[if1] \n\t"
  253. :
  254. [condition] "+&c" (condition),
  255. [if1] "+&a" (if1)
  256. :
  257. [if0] "b" (if0)
  258. :
  259. );
  260. return if1;
  261. #else
  262. mbedtls_ct_condition_t not_cond =
  263. (mbedtls_ct_condition_t) (~mbedtls_ct_compiler_opaque(condition));
  264. return (mbedtls_ct_uint_t) ((condition & if1) | (not_cond & if0));
  265. #endif
  266. }
  267. static inline mbedtls_ct_condition_t mbedtls_ct_uint_lt(mbedtls_ct_uint_t x, mbedtls_ct_uint_t y)
  268. {
  269. #if defined(MBEDTLS_CT_AARCH64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
  270. uint64_t s1;
  271. asm volatile ("eor %x[s1], %x[y], %x[x] \n\t"
  272. "sub %x[x], %x[x], %x[y] \n\t"
  273. "bic %x[x], %x[x], %x[s1] \n\t"
  274. "and %x[s1], %x[s1], %x[y] \n\t"
  275. "orr %x[s1], %x[x], %x[s1] \n\t"
  276. "asr %x[x], %x[s1], 63"
  277. :
  278. [s1] "=&r" (s1),
  279. [x] "+&r" (x)
  280. :
  281. [y] "r" (y)
  282. :
  283. );
  284. return (mbedtls_ct_condition_t) x;
  285. #elif defined(MBEDTLS_CT_ARM_ASM) && defined(MBEDTLS_CT_SIZE_32)
  286. uint32_t s1;
  287. asm volatile (
  288. ".syntax unified \n\t"
  289. #if defined(__thumb__) && !defined(__thumb2__)
  290. "movs %[s1], %[x] \n\t"
  291. "eors %[s1], %[s1], %[y] \n\t"
  292. #else
  293. "eors %[s1], %[x], %[y] \n\t"
  294. #endif
  295. "subs %[x], %[x], %[y] \n\t"
  296. "bics %[x], %[x], %[s1] \n\t"
  297. "ands %[y], %[s1], %[y] \n\t"
  298. "orrs %[x], %[x], %[y] \n\t"
  299. "asrs %[x], %[x], #31 \n\t"
  300. RESTORE_ASM_SYNTAX
  301. :
  302. [s1] "=&l" (s1),
  303. [x] "+&l" (x),
  304. [y] "+&l" (y)
  305. :
  306. :
  307. "cc"
  308. );
  309. return (mbedtls_ct_condition_t) x;
  310. #elif defined(MBEDTLS_CT_X86_64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
  311. uint64_t s;
  312. asm volatile ("mov %[x], %[s] \n\t"
  313. "xor %[y], %[s] \n\t"
  314. "sub %[y], %[x] \n\t"
  315. "and %[s], %[y] \n\t"
  316. "not %[s] \n\t"
  317. "and %[s], %[x] \n\t"
  318. "or %[y], %[x] \n\t"
  319. "sar $63, %[x] \n\t"
  320. :
  321. [s] "=&a" (s),
  322. [x] "+&D" (x),
  323. [y] "+&S" (y)
  324. :
  325. :
  326. );
  327. return (mbedtls_ct_condition_t) x;
  328. #elif defined(MBEDTLS_CT_X86_ASM) && defined(MBEDTLS_CT_SIZE_32)
  329. uint32_t s;
  330. asm volatile ("mov %[x], %[s] \n\t"
  331. "xor %[y], %[s] \n\t"
  332. "sub %[y], %[x] \n\t"
  333. "and %[s], %[y] \n\t"
  334. "not %[s] \n\t"
  335. "and %[s], %[x] \n\t"
  336. "or %[y], %[x] \n\t"
  337. "sar $31, %[x] \n\t"
  338. :
  339. [s] "=&b" (s),
  340. [x] "+&a" (x),
  341. [y] "+&c" (y)
  342. :
  343. :
  344. );
  345. return (mbedtls_ct_condition_t) x;
  346. #else
  347. /* Ensure that the compiler cannot optimise the following operations over x and y,
  348. * even if it knows the value of x and y.
  349. */
  350. const mbedtls_ct_uint_t xo = mbedtls_ct_compiler_opaque(x);
  351. const mbedtls_ct_uint_t yo = mbedtls_ct_compiler_opaque(y);
  352. /*
  353. * Check if the most significant bits (MSB) of the operands are different.
  354. * cond is true iff the MSBs differ.
  355. */
  356. mbedtls_ct_condition_t cond = mbedtls_ct_bool((xo ^ yo) >> (MBEDTLS_CT_SIZE - 1));
  357. /*
  358. * If the MSB are the same then the difference x-y will be negative (and
  359. * have its MSB set to 1 during conversion to unsigned) if and only if x<y.
  360. *
  361. * If the MSB are different, then the operand with the MSB of 1 is the
  362. * bigger. (That is if y has MSB of 1, then x<y is true and it is false if
  363. * the MSB of y is 0.)
  364. */
  365. // Select either y, or x - y
  366. mbedtls_ct_uint_t ret = mbedtls_ct_if(cond, yo, (mbedtls_ct_uint_t) (xo - yo));
  367. // Extract only the MSB of ret
  368. ret = ret >> (MBEDTLS_CT_SIZE - 1);
  369. // Convert to a condition (i.e., all bits set iff non-zero)
  370. return mbedtls_ct_bool(ret);
  371. #endif
  372. }
  373. static inline mbedtls_ct_condition_t mbedtls_ct_uint_ne(mbedtls_ct_uint_t x, mbedtls_ct_uint_t y)
  374. {
  375. /* diff = 0 if x == y, non-zero otherwise */
  376. const mbedtls_ct_uint_t diff = mbedtls_ct_compiler_opaque(x) ^ mbedtls_ct_compiler_opaque(y);
  377. /* all ones if x != y, 0 otherwise */
  378. return mbedtls_ct_bool(diff);
  379. }
  380. static inline unsigned char mbedtls_ct_uchar_in_range_if(unsigned char low,
  381. unsigned char high,
  382. unsigned char c,
  383. unsigned char t)
  384. {
  385. const unsigned char co = (unsigned char) mbedtls_ct_compiler_opaque(c);
  386. const unsigned char to = (unsigned char) mbedtls_ct_compiler_opaque(t);
  387. /* low_mask is: 0 if low <= c, 0x...ff if low > c */
  388. unsigned low_mask = ((unsigned) co - low) >> 8;
  389. /* high_mask is: 0 if c <= high, 0x...ff if c > high */
  390. unsigned high_mask = ((unsigned) high - co) >> 8;
  391. return (unsigned char) (~(low_mask | high_mask)) & to;
  392. }
  393. /* ============================================================================
  394. * Everything below here is trivial wrapper functions
  395. */
  396. static inline size_t mbedtls_ct_size_if(mbedtls_ct_condition_t condition,
  397. size_t if1,
  398. size_t if0)
  399. {
  400. return (size_t) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1, (mbedtls_ct_uint_t) if0);
  401. }
  402. static inline unsigned mbedtls_ct_uint_if(mbedtls_ct_condition_t condition,
  403. unsigned if1,
  404. unsigned if0)
  405. {
  406. return (unsigned) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1, (mbedtls_ct_uint_t) if0);
  407. }
  408. static inline mbedtls_ct_condition_t mbedtls_ct_bool_if(mbedtls_ct_condition_t condition,
  409. mbedtls_ct_condition_t if1,
  410. mbedtls_ct_condition_t if0)
  411. {
  412. return (mbedtls_ct_condition_t) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1,
  413. (mbedtls_ct_uint_t) if0);
  414. }
  415. #if defined(MBEDTLS_BIGNUM_C)
  416. static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if(mbedtls_ct_condition_t condition,
  417. mbedtls_mpi_uint if1,
  418. mbedtls_mpi_uint if0)
  419. {
  420. return (mbedtls_mpi_uint) mbedtls_ct_if(condition,
  421. (mbedtls_ct_uint_t) if1,
  422. (mbedtls_ct_uint_t) if0);
  423. }
  424. #endif
  425. static inline size_t mbedtls_ct_size_if_else_0(mbedtls_ct_condition_t condition, size_t if1)
  426. {
  427. return (size_t) (condition & if1);
  428. }
  429. static inline unsigned mbedtls_ct_uint_if_else_0(mbedtls_ct_condition_t condition, unsigned if1)
  430. {
  431. return (unsigned) (condition & if1);
  432. }
  433. static inline mbedtls_ct_condition_t mbedtls_ct_bool_if_else_0(mbedtls_ct_condition_t condition,
  434. mbedtls_ct_condition_t if1)
  435. {
  436. return (mbedtls_ct_condition_t) (condition & if1);
  437. }
  438. #if defined(MBEDTLS_BIGNUM_C)
  439. static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if_else_0(mbedtls_ct_condition_t condition,
  440. mbedtls_mpi_uint if1)
  441. {
  442. return (mbedtls_mpi_uint) (condition & if1);
  443. }
  444. #endif /* MBEDTLS_BIGNUM_C */
  445. static inline int mbedtls_ct_error_if(mbedtls_ct_condition_t condition, int if1, int if0)
  446. {
  447. /* Coverting int -> uint -> int here is safe, because we require if1 and if0 to be
  448. * in the range -32767..0, and we require 32-bit int and uint types.
  449. *
  450. * This means that (0 <= -if0 < INT_MAX), so negating if0 is safe, and similarly for
  451. * converting back to int.
  452. */
  453. return -((int) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) (-if1),
  454. (mbedtls_ct_uint_t) (-if0)));
  455. }
  456. static inline int mbedtls_ct_error_if_else_0(mbedtls_ct_condition_t condition, int if1)
  457. {
  458. return -((int) (condition & (-if1)));
  459. }
  460. static inline mbedtls_ct_condition_t mbedtls_ct_uint_eq(mbedtls_ct_uint_t x,
  461. mbedtls_ct_uint_t y)
  462. {
  463. return ~mbedtls_ct_uint_ne(x, y);
  464. }
  465. static inline mbedtls_ct_condition_t mbedtls_ct_uint_gt(mbedtls_ct_uint_t x,
  466. mbedtls_ct_uint_t y)
  467. {
  468. return mbedtls_ct_uint_lt(y, x);
  469. }
  470. static inline mbedtls_ct_condition_t mbedtls_ct_uint_ge(mbedtls_ct_uint_t x,
  471. mbedtls_ct_uint_t y)
  472. {
  473. return ~mbedtls_ct_uint_lt(x, y);
  474. }
  475. static inline mbedtls_ct_condition_t mbedtls_ct_uint_le(mbedtls_ct_uint_t x,
  476. mbedtls_ct_uint_t y)
  477. {
  478. return ~mbedtls_ct_uint_gt(x, y);
  479. }
  480. static inline mbedtls_ct_condition_t mbedtls_ct_bool_ne(mbedtls_ct_condition_t x,
  481. mbedtls_ct_condition_t y)
  482. {
  483. return (mbedtls_ct_condition_t) (x ^ y);
  484. }
  485. static inline mbedtls_ct_condition_t mbedtls_ct_bool_and(mbedtls_ct_condition_t x,
  486. mbedtls_ct_condition_t y)
  487. {
  488. return (mbedtls_ct_condition_t) (x & y);
  489. }
  490. static inline mbedtls_ct_condition_t mbedtls_ct_bool_or(mbedtls_ct_condition_t x,
  491. mbedtls_ct_condition_t y)
  492. {
  493. return (mbedtls_ct_condition_t) (x | y);
  494. }
  495. static inline mbedtls_ct_condition_t mbedtls_ct_bool_not(mbedtls_ct_condition_t x)
  496. {
  497. return (mbedtls_ct_condition_t) (~x);
  498. }
  499. #if defined(MBEDTLS_COMPILER_IS_GCC) && (__GNUC__ > 4)
  500. /* Restore warnings for -Wredundant-decls on gcc */
  501. #pragma GCC diagnostic pop
  502. #endif
  503. #endif /* MBEDTLS_CONSTANT_TIME_IMPL_H */