8#ifndef MBEDTLS_CONSTANT_TIME_IMPL_H
9#define MBEDTLS_CONSTANT_TIME_IMPL_H
15#if defined(MBEDTLS_BIGNUM_C)
34#if defined(MBEDTLS_COMPILER_IS_GCC) && (__GNUC__ > 4)
35 #pragma GCC diagnostic push
36 #pragma GCC diagnostic ignored "-Wredundant-decls"
40#if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && (!defined(__ARMCC_VERSION) || \
41 __ARMCC_VERSION >= 6000000)
43#if (defined(__arm__) || defined(__thumb__) || defined(__thumb2__))
44#define MBEDTLS_CT_ARM_ASM
45#elif defined(__aarch64__)
46#define MBEDTLS_CT_AARCH64_ASM
47#elif defined(__amd64__) || defined(__x86_64__)
48#define MBEDTLS_CT_X86_64_ASM
49#elif defined(__i386__)
50#define MBEDTLS_CT_X86_ASM
54#define MBEDTLS_CT_SIZE (sizeof(mbedtls_ct_uint_t) * 8)
68#if !defined(MBEDTLS_CT_ASM)
87#if defined(MBEDTLS_CT_ASM)
88 asm volatile (
"" : [
x]
"+r" (
x) :);
112#if defined(MBEDTLS_COMPILER_IS_GCC) && defined(__thumb__) && !defined(__thumb2__) && \
113 (__GNUC__ < 11) && !defined(__ARM_ARCH_2__)
114#define RESTORE_ASM_SYNTAX ".syntax divided \n\t"
116#define RESTORE_ASM_SYNTAX
130#if defined(MBEDTLS_CT_AARCH64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
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"
142#elif defined(MBEDTLS_CT_ARM_ASM) && defined(MBEDTLS_CT_SIZE_32)
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"
157#elif defined(MBEDTLS_CT_X86_64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
159 asm volatile (
"mov %[x], %[s] \n\t"
170#elif defined(MBEDTLS_CT_X86_ASM) && defined(MBEDTLS_CT_SIZE_32)
172 asm volatile (
"mov %[x], %[s] \n\t"
188#pragma warning( push )
189#pragma warning( disable : 4146 )
200#pragma warning( pop )
209#if defined(MBEDTLS_CT_AARCH64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
210 asm volatile (
"and %x[if1], %x[if1], %x[condition] \n\t"
211 "mvn %x[condition], %x[condition] \n\t"
212 "and %x[condition], %x[condition], %x[if0] \n\t"
213 "orr %x[condition], %x[if1], %x[condition]"
215 [condition]
"+&r" (condition),
222#elif defined(MBEDTLS_CT_ARM_ASM) && defined(MBEDTLS_CT_SIZE_32)
223 asm volatile (
".syntax unified \n\t"
224 "ands %[if1], %[if1], %[condition] \n\t"
225 "mvns %[condition], %[condition] \n\t"
226 "ands %[condition], %[condition], %[if0] \n\t"
227 "orrs %[condition], %[if1], %[condition] \n\t"
230 [condition]
"+&l" (condition),
238#elif defined(MBEDTLS_CT_X86_64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
239 asm volatile (
"and %[condition], %[if1] \n\t"
240 "not %[condition] \n\t"
241 "and %[condition], %[if0] \n\t"
242 "or %[if1], %[if0] \n\t"
244 [condition]
"+&D" (condition),
251#elif defined(MBEDTLS_CT_X86_ASM) && defined(MBEDTLS_CT_SIZE_32)
252 asm volatile (
"and %[condition], %[if1] \n\t"
253 "not %[condition] \n\t"
254 "and %[if0], %[condition] \n\t"
255 "or %[condition], %[if1] \n\t"
257 [condition]
"+&c" (condition),
273#if defined(MBEDTLS_CT_AARCH64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
275 asm volatile (
"eor %x[s1], %x[y], %x[x] \n\t"
276 "sub %x[x], %x[x], %x[y] \n\t"
277 "bic %x[x], %x[x], %x[s1] \n\t"
278 "and %x[s1], %x[s1], %x[y] \n\t"
279 "orr %x[s1], %x[x], %x[s1] \n\t"
280 "asr %x[x], %x[s1], 63"
289#elif defined(MBEDTLS_CT_ARM_ASM) && defined(MBEDTLS_CT_SIZE_32)
292 ".syntax unified \n\t"
293#if defined(__thumb__) && !defined(__thumb2__)
294 "movs %[s1], %[x] \n\t"
295 "eors %[s1], %[s1], %[y] \n\t"
297 "eors %[s1], %[x], %[y] \n\t"
299 "subs %[x], %[x], %[y] \n\t"
300 "bics %[x], %[x], %[s1] \n\t"
301 "ands %[y], %[s1], %[y] \n\t"
302 "orrs %[x], %[x], %[y] \n\t"
303 "asrs %[x], %[x], #31 \n\t"
314#elif defined(MBEDTLS_CT_X86_64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
316 asm volatile (
"mov %[x], %[s] \n\t"
317 "xor %[y], %[s] \n\t"
318 "sub %[y], %[x] \n\t"
319 "and %[s], %[y] \n\t"
321 "and %[s], %[x] \n\t"
332#elif defined(MBEDTLS_CT_X86_ASM) && defined(MBEDTLS_CT_SIZE_32)
334 asm volatile (
"mov %[x], %[s] \n\t"
335 "xor %[y], %[s] \n\t"
336 "sub %[y], %[x] \n\t"
337 "and %[s], %[y] \n\t"
339 "and %[s], %[x] \n\t"
378 return mbedtls_ct_bool(ret);
385 const mbedtls_ct_uint_t diff = mbedtls_ct_compiler_opaque(x) ^ mbedtls_ct_compiler_opaque(y);
388 return mbedtls_ct_bool(diff);
391static inline unsigned char mbedtls_ct_uchar_in_range_if(
unsigned char low,
396 const unsigned char co = (
unsigned char) mbedtls_ct_compiler_opaque(c);
397 const unsigned char to = (
unsigned char) mbedtls_ct_compiler_opaque(
t);
400 unsigned low_mask = ((unsigned) co - low) >> 8;
402 unsigned high_mask = ((unsigned) high - co) >> 8;
404 return (
unsigned char) (~(low_mask | high_mask)) & to;
433#if defined(MBEDTLS_BIGNUM_C)
448 return (
size_t) (condition & if1);
453 return (
unsigned) (condition & if1);
462#if defined(MBEDTLS_BIGNUM_C)
486 return -((int) (condition & (-if1)));
492 return ~mbedtls_ct_uint_ne(x, y);
498 return mbedtls_ct_uint_lt(y, x);
504 return ~mbedtls_ct_uint_lt(x, y);
510 return ~mbedtls_ct_uint_gt(x, y);
536#if defined(MBEDTLS_COMPILER_IS_GCC) && (__GNUC__ > 4)
538 #pragma GCC diagnostic pop
XETime t
Definition main.cpp:53
unsigned int uint32_t
Definition acefiex.h:163
COMPILER_DEPENDENT_UINT64 uint64_t
Definition acefiex.h:165
Multi-precision integer library.
uint32_t mbedtls_mpi_uint
The type of machine digits in a bignum, called limbs.
Definition bignum.h:170
Utility macros for internal use in the library.
#define RESTORE_ASM_SYNTAX
Definition constant_time_impl.h:116
#define MBEDTLS_CT_SIZE
Definition constant_time_impl.h:54
volatile mbedtls_ct_uint_t mbedtls_ct_zero
Definition constant_time.c:30
size_t mbedtls_ct_condition_t
Definition constant_time_internal.h:67
size_t mbedtls_ct_uint_t
Definition constant_time_internal.h:68
ptrdiff_t mbedtls_ct_int_t
Definition constant_time_internal.h:69
int y
Definition layout.c:118
int x
Definition layout.c:118