blob: 74d2732e742467cdff1783ef85afa8eeff7d9ac7 [file] [log] [blame]
Dave Rodgman40a41d02023-05-17 11:59:56 +01001/**
2 * Constant-time functions
3 *
Dave Rodgman40a41d02023-05-17 11:59:56 +01004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Dave Rodgman40a41d02023-05-17 11:59:56 +01006 */
7
8#ifndef MBEDTLS_CONSTANT_TIME_IMPL_H
9#define MBEDTLS_CONSTANT_TIME_IMPL_H
10
11#include <stddef.h>
12
13#include "common.h"
14
15#if defined(MBEDTLS_BIGNUM_C)
16#include "mbedtls/bignum.h"
17#endif
18
Dave Rodgmand44dd962023-08-09 14:10:14 +010019/*
20 * To improve readability of constant_time_internal.h, the static inline
21 * definitions are here, and constant_time_internal.h has only the declarations.
Dave Rodgman205295c2023-08-01 14:10:56 +010022 *
Dave Rodgmand44dd962023-08-09 14:10:14 +010023 * This results in duplicate declarations of the form:
24 * static inline void f(); // from constant_time_internal.h
25 * static inline void f() { ... } // from constant_time_impl.h
26 * when constant_time_internal.h is included.
27 *
28 * This appears to behave as if the declaration-without-definition was not present
29 * (except for warnings if gcc -Wredundant-decls or similar is used).
30 *
31 * Disable -Wredundant-decls so that gcc does not warn about this. This is re-enabled
32 * at the bottom of this file.
Dave Rodgman205295c2023-08-01 14:10:56 +010033 */
Matthias Schulzee10b842023-11-09 15:19:28 +010034#ifdef __GNUC__ > 4
Dave Rodgman205295c2023-08-01 14:10:56 +010035 #pragma GCC diagnostic push
36 #pragma GCC diagnostic ignored "-Wredundant-decls"
37#endif
38
Dave Rodgman246210e2023-07-31 18:07:44 +010039/* Disable asm under Memsan because it confuses Memsan and generates false errors.
40 *
41 * We also disable under Valgrind by default, because it's more useful
42 * for Valgrind to test the plain C implementation. MBEDTLS_TEST_CONSTANT_FLOW_ASM //no-check-names
43 * may be set to permit building asm under Valgrind.
44 */
45#if defined(MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN) || \
46 (defined(MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND) && !defined(MBEDTLS_TEST_CONSTANT_FLOW_ASM)) //no-check-names
Dave Rodgman3d574da2023-07-31 16:54:00 +010047#define MBEDTLS_CT_NO_ASM
48#elif defined(__has_feature)
49#if __has_feature(memory_sanitizer)
50#define MBEDTLS_CT_NO_ASM
51#endif
52#endif
Dave Rodgman40a41d02023-05-17 11:59:56 +010053
54/* armcc5 --gnu defines __GNUC__ but doesn't support GNU's extended asm */
55#if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && (!defined(__ARMCC_VERSION) || \
Dave Rodgman3d574da2023-07-31 16:54:00 +010056 __ARMCC_VERSION >= 6000000) && !defined(MBEDTLS_CT_NO_ASM)
Dave Rodgman40a41d02023-05-17 11:59:56 +010057#define MBEDTLS_CT_ASM
58#if (defined(__arm__) || defined(__thumb__) || defined(__thumb2__))
59#define MBEDTLS_CT_ARM_ASM
60#elif defined(__aarch64__)
61#define MBEDTLS_CT_AARCH64_ASM
Dave Rodgman664fea42023-05-12 12:11:37 +010062#elif defined(__amd64__) || defined(__x86_64__)
63#define MBEDTLS_CT_X86_64_ASM
Dave Rodgman81673bb2023-05-13 12:32:09 +010064#elif defined(__i386__)
65#define MBEDTLS_CT_X86_ASM
Dave Rodgman40a41d02023-05-17 11:59:56 +010066#endif
67#endif
68
69#define MBEDTLS_CT_SIZE (sizeof(mbedtls_ct_uint_t) * 8)
70
71
72/* ============================================================================
73 * Core const-time primitives
74 */
75
Dave Rodgman2894d002023-06-08 17:52:21 +010076/* Ensure that the compiler cannot know the value of x (i.e., cannot optimise
Dave Rodgman40a41d02023-05-17 11:59:56 +010077 * based on its value) after this function is called.
78 *
79 * If we are not using assembly, this will be fairly inefficient, so its use
80 * should be minimised.
81 */
Dave Rodgman2894d002023-06-08 17:52:21 +010082
83#if !defined(MBEDTLS_CT_ASM)
Dave Rodgman58c80f42023-06-12 18:19:46 +010084extern volatile mbedtls_ct_uint_t mbedtls_ct_zero;
Dave Rodgman2894d002023-06-08 17:52:21 +010085#endif
86
Dave Rodgman93cec452023-07-31 12:30:26 +010087/**
88 * \brief Ensure that a value cannot be known at compile time.
89 *
90 * \param x The value to hide from the compiler.
91 * \return The same value that was passed in, such that the compiler
92 * cannot prove its value (even for calls of the form
93 * x = mbedtls_ct_compiler_opaque(1), x will be unknown).
94 *
95 * \note This is mainly used in constructing mbedtls_ct_condition_t
96 * values and performing operations over them, to ensure that
97 * there is no way for the compiler to ever know anything about
98 * the value of an mbedtls_ct_condition_t.
99 */
Dave Rodgman40a41d02023-05-17 11:59:56 +0100100static inline mbedtls_ct_uint_t mbedtls_ct_compiler_opaque(mbedtls_ct_uint_t x)
101{
102#if defined(MBEDTLS_CT_ASM)
103 asm volatile ("" : [x] "+r" (x) :);
104 return x;
105#else
Dave Rodgman2894d002023-06-08 17:52:21 +0100106 return x ^ mbedtls_ct_zero;
Dave Rodgman40a41d02023-05-17 11:59:56 +0100107#endif
108}
109
Dave Rodgman3ab114e2023-08-21 07:54:11 +0100110/*
111 * Selecting unified syntax is needed for gcc, and harmless on clang.
112 *
113 * This is needed because on Thumb 1, condition flags are always set, so
114 * e.g. "negs" is supported but "neg" is not (on Thumb 2, both exist).
115 *
116 * Under Thumb 1 unified syntax, only the "negs" form is accepted, and
117 * under divided syntax, only the "neg" form is accepted. clang only
118 * supports unified syntax.
119 *
120 * On Thumb 2 and Arm, both compilers are happy with the "s" suffix,
121 * although we don't actually care about setting the flags.
122 *
123 * For gcc, restore divided syntax afterwards - otherwise old versions of gcc
124 * seem to apply unified syntax globally, which breaks other asm code.
125 */
126#if !defined(__clang__)
Dave Rodgman0cf9dd12023-05-12 16:29:48 +0100127#define RESTORE_ASM_SYNTAX ".syntax divided \n\t"
Dave Rodgman3ab114e2023-08-21 07:54:11 +0100128#else
129#define RESTORE_ASM_SYNTAX
130#endif
131
Dave Rodgman40a41d02023-05-17 11:59:56 +0100132/* Convert a number into a condition in constant time. */
133static inline mbedtls_ct_condition_t mbedtls_ct_bool(mbedtls_ct_uint_t x)
134{
135 /*
136 * Define mask-generation code that, as far as possible, will not use branches or conditional instructions.
137 *
138 * For some platforms / type sizes, we define assembly to assure this.
139 *
140 * Otherwise, we define a plain C fallback which (in May 2023) does not get optimised into
141 * conditional instructions or branches by trunk clang, gcc, or MSVC v19.
142 */
Dave Rodgmanc9ed5de2023-05-13 12:47:02 +0100143#if defined(MBEDTLS_CT_AARCH64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
144 mbedtls_ct_uint_t s;
Dave Rodgman0cf9dd12023-05-12 16:29:48 +0100145 asm volatile ("neg %x[s], %x[x] \n\t"
146 "orr %x[x], %x[s], %x[x] \n\t"
147 "asr %x[x], %x[x], 63 \n\t"
Dave Rodgmanc9ed5de2023-05-13 12:47:02 +0100148 :
149 [s] "=&r" (s),
150 [x] "+&r" (x)
151 :
152 :
153 );
154 return (mbedtls_ct_condition_t) x;
Dave Rodgmanef252792023-05-13 12:48:02 +0100155#elif defined(MBEDTLS_CT_ARM_ASM) && defined(MBEDTLS_CT_SIZE_32)
156 uint32_t s;
Dave Rodgman0cf9dd12023-05-12 16:29:48 +0100157 asm volatile (".syntax unified \n\t"
158 "negs %[s], %[x] \n\t"
159 "orrs %[x], %[x], %[s] \n\t"
160 "asrs %[x], %[x], #31 \n\t"
Dave Rodgman822c9c72023-06-12 15:38:49 +0100161 RESTORE_ASM_SYNTAX
Dave Rodgmanef252792023-05-13 12:48:02 +0100162 :
163 [s] "=&l" (s),
164 [x] "+&l" (x)
165 :
166 :
Dave Rodgman822c9c72023-06-12 15:38:49 +0100167 "cc" /* clobbers flag bits */
Dave Rodgmanef252792023-05-13 12:48:02 +0100168 );
169 return (mbedtls_ct_condition_t) x;
Dave Rodgman664fea42023-05-12 12:11:37 +0100170#elif defined(MBEDTLS_CT_X86_64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
171 uint64_t s;
172 asm volatile ("mov %[x], %[s] \n\t"
173 "neg %[s] \n\t"
174 "or %[x], %[s] \n\t"
175 "sar $63, %[s] \n\t"
176 :
177 [s] "=&a" (s)
178 :
179 [x] "D" (x)
180 :
181 );
182 return (mbedtls_ct_condition_t) s;
Dave Rodgman81673bb2023-05-13 12:32:09 +0100183#elif defined(MBEDTLS_CT_X86_ASM) && defined(MBEDTLS_CT_SIZE_32)
184 uint32_t s;
185 asm volatile ("mov %[x], %[s] \n\t"
186 "neg %[s] \n\t"
187 "or %[s], %[x] \n\t"
188 "sar $31, %[x] \n\t"
189 :
190 [s] "=&c" (s),
191 [x] "+&a" (x)
192 :
193 :
194 );
195 return (mbedtls_ct_condition_t) x;
Dave Rodgmanc9ed5de2023-05-13 12:47:02 +0100196#else
Dave Rodgman40a41d02023-05-17 11:59:56 +0100197 const mbedtls_ct_uint_t xo = mbedtls_ct_compiler_opaque(x);
198#if defined(_MSC_VER)
199 /* MSVC has a warning about unary minus on unsigned, but this is
200 * well-defined and precisely what we want to do here */
201#pragma warning( push )
202#pragma warning( disable : 4146 )
203#endif
Dave Rodgman0c99a902023-08-21 17:06:24 +0100204 // y is negative (i.e., top bit set) iff x is non-zero
205 mbedtls_ct_int_t y = (-xo) | -(xo >> 1);
206
207 // extract only the sign bit of y so that y == 1 (if x is non-zero) or 0 (if x is zero)
208 y = (((mbedtls_ct_uint_t) y) >> (MBEDTLS_CT_SIZE - 1));
209
210 // -y has all bits set (if x is non-zero), or all bits clear (if x is zero)
211 return (mbedtls_ct_condition_t) (-y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100212#if defined(_MSC_VER)
213#pragma warning( pop )
214#endif
Dave Rodgmanc9ed5de2023-05-13 12:47:02 +0100215#endif
Dave Rodgman40a41d02023-05-17 11:59:56 +0100216}
217
218static inline mbedtls_ct_uint_t mbedtls_ct_if(mbedtls_ct_condition_t condition,
219 mbedtls_ct_uint_t if1,
220 mbedtls_ct_uint_t if0)
221{
Dave Rodgmanc9ed5de2023-05-13 12:47:02 +0100222#if defined(MBEDTLS_CT_AARCH64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
Dave Rodgman0cf9dd12023-05-12 16:29:48 +0100223 asm volatile ("and %x[if1], %x[if1], %x[condition] \n\t"
224 "mvn %x[condition], %x[condition] \n\t"
225 "and %x[condition], %x[condition], %x[if0] \n\t"
Dave Rodgmanc9ed5de2023-05-13 12:47:02 +0100226 "orr %x[condition], %x[if1], %x[condition]"
227 :
228 [condition] "+&r" (condition),
229 [if1] "+&r" (if1)
230 :
231 [if0] "r" (if0)
232 :
233 );
234 return (mbedtls_ct_uint_t) condition;
Dave Rodgmanef252792023-05-13 12:48:02 +0100235#elif defined(MBEDTLS_CT_ARM_ASM) && defined(MBEDTLS_CT_SIZE_32)
Dave Rodgman0cf9dd12023-05-12 16:29:48 +0100236 asm volatile (".syntax unified \n\t"
237 "ands %[if1], %[if1], %[condition] \n\t"
238 "mvns %[condition], %[condition] \n\t"
239 "ands %[condition], %[condition], %[if0] \n\t"
240 "orrs %[condition], %[if1], %[condition] \n\t"
Dave Rodgman822c9c72023-06-12 15:38:49 +0100241 RESTORE_ASM_SYNTAX
Dave Rodgmanef252792023-05-13 12:48:02 +0100242 :
243 [condition] "+&l" (condition),
244 [if1] "+&l" (if1)
245 :
246 [if0] "l" (if0)
247 :
Dave Rodgman822c9c72023-06-12 15:38:49 +0100248 "cc"
Dave Rodgmanef252792023-05-13 12:48:02 +0100249 );
250 return (mbedtls_ct_uint_t) condition;
Dave Rodgman664fea42023-05-12 12:11:37 +0100251#elif defined(MBEDTLS_CT_X86_64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
252 asm volatile ("and %[condition], %[if1] \n\t"
253 "not %[condition] \n\t"
254 "and %[condition], %[if0] \n\t"
255 "or %[if1], %[if0] \n\t"
256 :
257 [condition] "+&D" (condition),
258 [if1] "+&S" (if1),
259 [if0] "+&a" (if0)
260 :
261 :
262 );
263 return if0;
Dave Rodgman81673bb2023-05-13 12:32:09 +0100264#elif defined(MBEDTLS_CT_X86_ASM) && defined(MBEDTLS_CT_SIZE_32)
265 asm volatile ("and %[condition], %[if1] \n\t"
266 "not %[condition] \n\t"
267 "and %[if0], %[condition] \n\t"
268 "or %[condition], %[if1] \n\t"
269 :
270 [condition] "+&c" (condition),
271 [if1] "+&a" (if1)
272 :
273 [if0] "b" (if0)
274 :
275 );
276 return if1;
Dave Rodgmanc9ed5de2023-05-13 12:47:02 +0100277#else
Dave Rodgman1c4eaa12023-05-17 12:22:59 +0100278 mbedtls_ct_condition_t not_cond =
Dave Rodgman40a41d02023-05-17 11:59:56 +0100279 (mbedtls_ct_condition_t) (~mbedtls_ct_compiler_opaque(condition));
Dave Rodgman1c4eaa12023-05-17 12:22:59 +0100280 return (mbedtls_ct_uint_t) ((condition & if1) | (not_cond & if0));
Dave Rodgmanc9ed5de2023-05-13 12:47:02 +0100281#endif
Dave Rodgman40a41d02023-05-17 11:59:56 +0100282}
283
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100284static inline mbedtls_ct_condition_t mbedtls_ct_uint_lt(mbedtls_ct_uint_t x, mbedtls_ct_uint_t y)
Dave Rodgman40a41d02023-05-17 11:59:56 +0100285{
Dave Rodgmanc9ed5de2023-05-13 12:47:02 +0100286#if defined(MBEDTLS_CT_AARCH64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
Dave Rodgman0ce0fbc2023-08-21 07:58:50 +0100287 uint64_t s1;
Dave Rodgman0cf9dd12023-05-12 16:29:48 +0100288 asm volatile ("eor %x[s1], %x[y], %x[x] \n\t"
289 "sub %x[x], %x[x], %x[y] \n\t"
290 "bic %x[x], %x[x], %x[s1] \n\t"
291 "and %x[s1], %x[s1], %x[y] \n\t"
292 "orr %x[s1], %x[x], %x[s1] \n\t"
Dave Rodgmanc9ed5de2023-05-13 12:47:02 +0100293 "asr %x[x], %x[s1], 63"
Dave Rodgman0cf9dd12023-05-12 16:29:48 +0100294 :
295 [s1] "=&r" (s1),
296 [x] "+&r" (x)
297 :
298 [y] "r" (y)
Dave Rodgmanc9ed5de2023-05-13 12:47:02 +0100299 :
300 );
301 return (mbedtls_ct_condition_t) x;
Dave Rodgmanef252792023-05-13 12:48:02 +0100302#elif defined(MBEDTLS_CT_ARM_ASM) && defined(MBEDTLS_CT_SIZE_32)
303 uint32_t s1;
304 asm volatile (
Dave Rodgman0cf9dd12023-05-12 16:29:48 +0100305 ".syntax unified \n\t"
Dave Rodgmanef252792023-05-13 12:48:02 +0100306#if defined(__thumb__) && !defined(__thumb2__)
Dave Rodgman0cf9dd12023-05-12 16:29:48 +0100307 "movs %[s1], %[x] \n\t"
308 "eors %[s1], %[s1], %[y] \n\t"
Dave Rodgmanef252792023-05-13 12:48:02 +0100309#else
Dave Rodgman0cf9dd12023-05-12 16:29:48 +0100310 "eors %[s1], %[x], %[y] \n\t"
Dave Rodgmanef252792023-05-13 12:48:02 +0100311#endif
Dave Rodgman0cf9dd12023-05-12 16:29:48 +0100312 "subs %[x], %[x], %[y] \n\t"
313 "bics %[x], %[x], %[s1] \n\t"
314 "ands %[y], %[s1], %[y] \n\t"
315 "orrs %[x], %[x], %[y] \n\t"
316 "asrs %[x], %[x], #31 \n\t"
Dave Rodgman822c9c72023-06-12 15:38:49 +0100317 RESTORE_ASM_SYNTAX
Dave Rodgman0cf9dd12023-05-12 16:29:48 +0100318 :
319 [s1] "=&l" (s1),
320 [x] "+&l" (x),
321 [y] "+&l" (y)
Dave Rodgmanef252792023-05-13 12:48:02 +0100322 :
323 :
Dave Rodgman822c9c72023-06-12 15:38:49 +0100324 "cc"
Dave Rodgmanef252792023-05-13 12:48:02 +0100325 );
326 return (mbedtls_ct_condition_t) x;
Dave Rodgman664fea42023-05-12 12:11:37 +0100327#elif defined(MBEDTLS_CT_X86_64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
Dave Rodgmanb6b8f6c2023-09-08 17:19:32 +0100328 uint64_t s;
329 asm volatile ("mov %[x], %[s] \n\t"
330 "xor %[y], %[s] \n\t"
Dave Rodgman664fea42023-05-12 12:11:37 +0100331 "sub %[y], %[x] \n\t"
Dave Rodgmanb6b8f6c2023-09-08 17:19:32 +0100332 "and %[s], %[y] \n\t"
333 "not %[s] \n\t"
334 "and %[s], %[x] \n\t"
Dave Rodgman664fea42023-05-12 12:11:37 +0100335 "or %[y], %[x] \n\t"
Dave Rodgman99f0cdc2023-09-08 17:18:04 +0100336 "sar $63, %[x] \n\t"
Dave Rodgman664fea42023-05-12 12:11:37 +0100337 :
Dave Rodgmanb6b8f6c2023-09-08 17:19:32 +0100338 [s] "=&a" (s),
Dave Rodgman5f249852023-09-08 17:18:29 +0100339 [x] "+&D" (x),
340 [y] "+&S" (y)
Dave Rodgman664fea42023-05-12 12:11:37 +0100341 :
342 :
343 );
Dave Rodgman99f0cdc2023-09-08 17:18:04 +0100344 return (mbedtls_ct_condition_t) x;
Dave Rodgman81673bb2023-05-13 12:32:09 +0100345#elif defined(MBEDTLS_CT_X86_ASM) && defined(MBEDTLS_CT_SIZE_32)
346 uint32_t s;
347 asm volatile ("mov %[x], %[s] \n\t"
348 "xor %[y], %[s] \n\t"
349 "sub %[y], %[x] \n\t"
Dave Rodgman4a97e732023-09-08 17:26:18 +0100350 "and %[s], %[y] \n\t"
Dave Rodgman81673bb2023-05-13 12:32:09 +0100351 "not %[s] \n\t"
352 "and %[s], %[x] \n\t"
Dave Rodgman4a97e732023-09-08 17:26:18 +0100353 "or %[y], %[x] \n\t"
Dave Rodgman81673bb2023-05-13 12:32:09 +0100354 "sar $31, %[x] \n\t"
355 :
356 [s] "=&b" (s),
Dave Rodgman3f8e4832023-09-08 17:57:40 +0100357 [x] "+&a" (x),
358 [y] "+&c" (y)
Dave Rodgman81673bb2023-05-13 12:32:09 +0100359 :
Dave Rodgman81673bb2023-05-13 12:32:09 +0100360 :
361 );
362 return (mbedtls_ct_condition_t) x;
Dave Rodgmanc9ed5de2023-05-13 12:47:02 +0100363#else
Dave Rodgman40a41d02023-05-17 11:59:56 +0100364 /* Ensure that the compiler cannot optimise the following operations over x and y,
365 * even if it knows the value of x and y.
366 */
Dave Rodgman74e18eb2023-05-17 12:21:32 +0100367 const mbedtls_ct_uint_t xo = mbedtls_ct_compiler_opaque(x);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100368 const mbedtls_ct_uint_t yo = mbedtls_ct_compiler_opaque(y);
369 /*
370 * Check if the most significant bits (MSB) of the operands are different.
371 * cond is true iff the MSBs differ.
372 */
Dave Rodgman74e18eb2023-05-17 12:21:32 +0100373 mbedtls_ct_condition_t cond = mbedtls_ct_bool((xo ^ yo) >> (MBEDTLS_CT_SIZE - 1));
Dave Rodgman40a41d02023-05-17 11:59:56 +0100374
375 /*
376 * If the MSB are the same then the difference x-y will be negative (and
377 * have its MSB set to 1 during conversion to unsigned) if and only if x<y.
378 *
379 * If the MSB are different, then the operand with the MSB of 1 is the
380 * bigger. (That is if y has MSB of 1, then x<y is true and it is false if
381 * the MSB of y is 0.)
382 */
383
384 // Select either y, or x - y
Dave Rodgman74e18eb2023-05-17 12:21:32 +0100385 mbedtls_ct_uint_t ret = mbedtls_ct_if(cond, yo, (mbedtls_ct_uint_t) (xo - yo));
Dave Rodgman40a41d02023-05-17 11:59:56 +0100386
387 // Extract only the MSB of ret
388 ret = ret >> (MBEDTLS_CT_SIZE - 1);
389
390 // Convert to a condition (i.e., all bits set iff non-zero)
391 return mbedtls_ct_bool(ret);
Dave Rodgmanc9ed5de2023-05-13 12:47:02 +0100392#endif
Dave Rodgman40a41d02023-05-17 11:59:56 +0100393}
394
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100395static inline mbedtls_ct_condition_t mbedtls_ct_uint_ne(mbedtls_ct_uint_t x, mbedtls_ct_uint_t y)
Dave Rodgman40a41d02023-05-17 11:59:56 +0100396{
397 /* diff = 0 if x == y, non-zero otherwise */
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100398 const mbedtls_ct_uint_t diff = mbedtls_ct_compiler_opaque(x) ^ mbedtls_ct_compiler_opaque(y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100399
400 /* all ones if x != y, 0 otherwise */
401 return mbedtls_ct_bool(diff);
402}
403
404static inline unsigned char mbedtls_ct_uchar_in_range_if(unsigned char low,
405 unsigned char high,
406 unsigned char c,
407 unsigned char t)
408{
Agathiyan Bragadeesh9ebfa7f2023-08-17 10:00:01 +0100409 const unsigned char co = (unsigned char) mbedtls_ct_compiler_opaque(c);
410 const unsigned char to = (unsigned char) mbedtls_ct_compiler_opaque(t);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100411
412 /* low_mask is: 0 if low <= c, 0x...ff if low > c */
413 unsigned low_mask = ((unsigned) co - low) >> 8;
414 /* high_mask is: 0 if c <= high, 0x...ff if c > high */
415 unsigned high_mask = ((unsigned) high - co) >> 8;
416
417 return (unsigned char) (~(low_mask | high_mask)) & to;
418}
419
Dave Rodgman40a41d02023-05-17 11:59:56 +0100420/* ============================================================================
421 * Everything below here is trivial wrapper functions
422 */
423
Dave Rodgman40a41d02023-05-17 11:59:56 +0100424static inline size_t mbedtls_ct_size_if(mbedtls_ct_condition_t condition,
425 size_t if1,
426 size_t if0)
427{
428 return (size_t) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1, (mbedtls_ct_uint_t) if0);
429}
430
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100431static inline unsigned mbedtls_ct_uint_if(mbedtls_ct_condition_t condition,
Dave Rodgman40a41d02023-05-17 11:59:56 +0100432 unsigned if1,
433 unsigned if0)
434{
435 return (unsigned) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1, (mbedtls_ct_uint_t) if0);
436}
437
Dave Rodgman143f5f72023-09-19 21:51:13 +0100438static inline mbedtls_ct_condition_t mbedtls_ct_bool_if(mbedtls_ct_condition_t condition,
439 mbedtls_ct_condition_t if1,
440 mbedtls_ct_condition_t if0)
441{
442 return (mbedtls_ct_condition_t) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1,
443 (mbedtls_ct_uint_t) if0);
444}
445
Dave Rodgman40a41d02023-05-17 11:59:56 +0100446#if defined(MBEDTLS_BIGNUM_C)
447
Dave Rodgman585f7f72023-05-17 17:45:33 +0100448static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if(mbedtls_ct_condition_t condition,
449 mbedtls_mpi_uint if1,
Dave Rodgman40a41d02023-05-17 11:59:56 +0100450 mbedtls_mpi_uint if0)
451{
452 return (mbedtls_mpi_uint) mbedtls_ct_if(condition,
453 (mbedtls_ct_uint_t) if1,
454 (mbedtls_ct_uint_t) if0);
455}
456
457#endif
458
Dave Rodgman98ddc012023-08-10 12:11:31 +0100459static inline size_t mbedtls_ct_size_if_else_0(mbedtls_ct_condition_t condition, size_t if1)
Dave Rodgman40a41d02023-05-17 11:59:56 +0100460{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100461 return (size_t) (condition & if1);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100462}
463
Dave Rodgman98ddc012023-08-10 12:11:31 +0100464static inline unsigned mbedtls_ct_uint_if_else_0(mbedtls_ct_condition_t condition, unsigned if1)
Dave Rodgman40a41d02023-05-17 11:59:56 +0100465{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100466 return (unsigned) (condition & if1);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100467}
468
Dave Rodgman143f5f72023-09-19 21:51:13 +0100469static inline mbedtls_ct_condition_t mbedtls_ct_bool_if_else_0(mbedtls_ct_condition_t condition,
470 mbedtls_ct_condition_t if1)
471{
472 return (mbedtls_ct_condition_t) (condition & if1);
473}
474
Dave Rodgman40a41d02023-05-17 11:59:56 +0100475#if defined(MBEDTLS_BIGNUM_C)
476
Dave Rodgman98ddc012023-08-10 12:11:31 +0100477static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if_else_0(mbedtls_ct_condition_t condition,
478 mbedtls_mpi_uint if1)
Dave Rodgman40a41d02023-05-17 11:59:56 +0100479{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100480 return (mbedtls_mpi_uint) (condition & if1);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100481}
482
483#endif /* MBEDTLS_BIGNUM_C */
484
Dave Rodgmanfbe74a92023-09-22 09:43:49 +0100485static inline int mbedtls_ct_error_if(mbedtls_ct_condition_t condition, int if1, int if0)
486{
Dave Rodgmanc0633bc2023-09-22 10:54:43 +0100487 /* Coverting int -> uint -> int here is safe, because we require if1 and if0 to be
488 * in the range -32767..0, and we require 32-bit int and uint types.
489 *
490 * This means that (0 <= -if0 < INT_MAX), so negating if0 is safe, and similarly for
491 * converting back to int.
492 */
Dave Rodgmanfbe74a92023-09-22 09:43:49 +0100493 return -((int) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) (-if1),
494 (mbedtls_ct_uint_t) (-if0)));
495}
496
497static inline int mbedtls_ct_error_if_else_0(mbedtls_ct_condition_t condition, int if1)
498{
499 return -((int) (condition & (-if1)));
500}
501
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100502static inline mbedtls_ct_condition_t mbedtls_ct_uint_eq(mbedtls_ct_uint_t x,
Dave Rodgman585f7f72023-05-17 17:45:33 +0100503 mbedtls_ct_uint_t y)
504{
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100505 return ~mbedtls_ct_uint_ne(x, y);
Dave Rodgman585f7f72023-05-17 17:45:33 +0100506}
507
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100508static inline mbedtls_ct_condition_t mbedtls_ct_uint_gt(mbedtls_ct_uint_t x,
Dave Rodgman40a41d02023-05-17 11:59:56 +0100509 mbedtls_ct_uint_t y)
510{
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100511 return mbedtls_ct_uint_lt(y, x);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100512}
513
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100514static inline mbedtls_ct_condition_t mbedtls_ct_uint_ge(mbedtls_ct_uint_t x,
Dave Rodgman40a41d02023-05-17 11:59:56 +0100515 mbedtls_ct_uint_t y)
516{
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100517 return ~mbedtls_ct_uint_lt(x, y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100518}
519
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100520static inline mbedtls_ct_condition_t mbedtls_ct_uint_le(mbedtls_ct_uint_t x,
Dave Rodgman40a41d02023-05-17 11:59:56 +0100521 mbedtls_ct_uint_t y)
522{
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100523 return ~mbedtls_ct_uint_gt(x, y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100524}
525
Dave Rodgman1cfc43c2023-09-19 16:18:59 +0100526static inline mbedtls_ct_condition_t mbedtls_ct_bool_ne(mbedtls_ct_condition_t x,
527 mbedtls_ct_condition_t y)
Dave Rodgman40a41d02023-05-17 11:59:56 +0100528{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100529 return (mbedtls_ct_condition_t) (x ^ y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100530}
531
532static inline mbedtls_ct_condition_t mbedtls_ct_bool_and(mbedtls_ct_condition_t x,
533 mbedtls_ct_condition_t y)
534{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100535 return (mbedtls_ct_condition_t) (x & y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100536}
537
538static inline mbedtls_ct_condition_t mbedtls_ct_bool_or(mbedtls_ct_condition_t x,
539 mbedtls_ct_condition_t y)
540{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100541 return (mbedtls_ct_condition_t) (x | y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100542}
543
544static inline mbedtls_ct_condition_t mbedtls_ct_bool_not(mbedtls_ct_condition_t x)
545{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100546 return (mbedtls_ct_condition_t) (~x);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100547}
548
Dave Rodgman205295c2023-08-01 14:10:56 +0100549#ifdef __GNUC__
Dave Rodgmand44dd962023-08-09 14:10:14 +0100550/* Restore warnings for -Wredundant-decls on gcc */
Dave Rodgman205295c2023-08-01 14:10:56 +0100551 #pragma GCC diagnostic pop
552#endif
553
Dave Rodgman40a41d02023-05-17 11:59:56 +0100554#endif /* MBEDTLS_CONSTANT_TIME_IMPL_H */