blob: 73e91ccefec5dd80cf6d838c032e76bb0a335b58 [file] [log] [blame]
Dave Rodgman40a41d02023-05-17 11:59:56 +01001/**
2 * Constant-time functions
3 *
4 * For readability, the static inline definitions are here, and
5 * constant_time_internal.h has only the declarations.
6 *
7 * This results in duplicate declarations of the form:
8 * static inline void f() { ... }
9 * static inline void f();
10 * when constant_time_internal.h is included. This appears to behave
11 * exactly as if the declaration-without-definition was not present.
12 *
13 * Copyright The Mbed TLS Contributors
14 * SPDX-License-Identifier: Apache-2.0
15 *
16 * Licensed under the Apache License, Version 2.0 (the "License"); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
27 */
28
29#ifndef MBEDTLS_CONSTANT_TIME_IMPL_H
30#define MBEDTLS_CONSTANT_TIME_IMPL_H
31
32#include <stddef.h>
33
34#include "common.h"
35
36#if defined(MBEDTLS_BIGNUM_C)
37#include "mbedtls/bignum.h"
38#endif
39
Dave Rodgman205295c2023-08-01 14:10:56 +010040/* constant_time_impl.h contains all the static inline implementations,
41 * so that constant_time_internal.h is more readable.
42 *
43 * gcc generates warnings about duplicate declarations, so disable this
44 * warning.
45 */
46#ifdef __GNUC__
47 #pragma GCC diagnostic push
48 #pragma GCC diagnostic ignored "-Wredundant-decls"
49#endif
50
Dave Rodgman246210e2023-07-31 18:07:44 +010051/* Disable asm under Memsan because it confuses Memsan and generates false errors.
52 *
53 * We also disable under Valgrind by default, because it's more useful
54 * for Valgrind to test the plain C implementation. MBEDTLS_TEST_CONSTANT_FLOW_ASM //no-check-names
55 * may be set to permit building asm under Valgrind.
56 */
57#if defined(MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN) || \
58 (defined(MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND) && !defined(MBEDTLS_TEST_CONSTANT_FLOW_ASM)) //no-check-names
Dave Rodgman3d574da2023-07-31 16:54:00 +010059#define MBEDTLS_CT_NO_ASM
60#elif defined(__has_feature)
61#if __has_feature(memory_sanitizer)
62#define MBEDTLS_CT_NO_ASM
63#endif
64#endif
Dave Rodgman40a41d02023-05-17 11:59:56 +010065
66/* armcc5 --gnu defines __GNUC__ but doesn't support GNU's extended asm */
67#if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && (!defined(__ARMCC_VERSION) || \
Dave Rodgman3d574da2023-07-31 16:54:00 +010068 __ARMCC_VERSION >= 6000000) && !defined(MBEDTLS_CT_NO_ASM)
Dave Rodgman40a41d02023-05-17 11:59:56 +010069#define MBEDTLS_CT_ASM
70#if (defined(__arm__) || defined(__thumb__) || defined(__thumb2__))
71#define MBEDTLS_CT_ARM_ASM
72#elif defined(__aarch64__)
73#define MBEDTLS_CT_AARCH64_ASM
Dave Rodgman664fea42023-05-12 12:11:37 +010074#elif defined(__amd64__) || defined(__x86_64__)
75#define MBEDTLS_CT_X86_64_ASM
Dave Rodgman40a41d02023-05-17 11:59:56 +010076#endif
77#endif
78
79#define MBEDTLS_CT_SIZE (sizeof(mbedtls_ct_uint_t) * 8)
80
81
82/* ============================================================================
83 * Core const-time primitives
84 */
85
Dave Rodgman2894d002023-06-08 17:52:21 +010086/* Ensure that the compiler cannot know the value of x (i.e., cannot optimise
Dave Rodgman40a41d02023-05-17 11:59:56 +010087 * based on its value) after this function is called.
88 *
89 * If we are not using assembly, this will be fairly inefficient, so its use
90 * should be minimised.
91 */
Dave Rodgman2894d002023-06-08 17:52:21 +010092
93#if !defined(MBEDTLS_CT_ASM)
Dave Rodgman58c80f42023-06-12 18:19:46 +010094extern volatile mbedtls_ct_uint_t mbedtls_ct_zero;
Dave Rodgman2894d002023-06-08 17:52:21 +010095#endif
96
Dave Rodgman93cec452023-07-31 12:30:26 +010097/**
98 * \brief Ensure that a value cannot be known at compile time.
99 *
100 * \param x The value to hide from the compiler.
101 * \return The same value that was passed in, such that the compiler
102 * cannot prove its value (even for calls of the form
103 * x = mbedtls_ct_compiler_opaque(1), x will be unknown).
104 *
105 * \note This is mainly used in constructing mbedtls_ct_condition_t
106 * values and performing operations over them, to ensure that
107 * there is no way for the compiler to ever know anything about
108 * the value of an mbedtls_ct_condition_t.
109 */
Dave Rodgman40a41d02023-05-17 11:59:56 +0100110static inline mbedtls_ct_uint_t mbedtls_ct_compiler_opaque(mbedtls_ct_uint_t x)
111{
112#if defined(MBEDTLS_CT_ASM)
113 asm volatile ("" : [x] "+r" (x) :);
114 return x;
115#else
Dave Rodgman2894d002023-06-08 17:52:21 +0100116 return x ^ mbedtls_ct_zero;
Dave Rodgman40a41d02023-05-17 11:59:56 +0100117#endif
118}
119
Dave Rodgman3ab114e2023-08-21 07:54:11 +0100120/*
121 * Selecting unified syntax is needed for gcc, and harmless on clang.
122 *
123 * This is needed because on Thumb 1, condition flags are always set, so
124 * e.g. "negs" is supported but "neg" is not (on Thumb 2, both exist).
125 *
126 * Under Thumb 1 unified syntax, only the "negs" form is accepted, and
127 * under divided syntax, only the "neg" form is accepted. clang only
128 * supports unified syntax.
129 *
130 * On Thumb 2 and Arm, both compilers are happy with the "s" suffix,
131 * although we don't actually care about setting the flags.
132 *
133 * For gcc, restore divided syntax afterwards - otherwise old versions of gcc
134 * seem to apply unified syntax globally, which breaks other asm code.
135 */
136#if !defined(__clang__)
137#define RESTORE_ASM_SYNTAX ".syntax divided \n\t"
138#else
139#define RESTORE_ASM_SYNTAX
140#endif
141
Dave Rodgman40a41d02023-05-17 11:59:56 +0100142/* Convert a number into a condition in constant time. */
143static inline mbedtls_ct_condition_t mbedtls_ct_bool(mbedtls_ct_uint_t x)
144{
145 /*
146 * Define mask-generation code that, as far as possible, will not use branches or conditional instructions.
147 *
148 * For some platforms / type sizes, we define assembly to assure this.
149 *
150 * Otherwise, we define a plain C fallback which (in May 2023) does not get optimised into
151 * conditional instructions or branches by trunk clang, gcc, or MSVC v19.
152 */
Dave Rodgmanc9ed5de2023-05-13 12:47:02 +0100153#if defined(MBEDTLS_CT_AARCH64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
154 mbedtls_ct_uint_t s;
155 asm volatile ("neg %x[s], %x[x] \n\t"
156 "orr %x[x], %x[s], %x[x] \n\t"
157 "asr %x[x], %x[x], 63"
158 :
159 [s] "=&r" (s),
160 [x] "+&r" (x)
161 :
162 :
163 );
164 return (mbedtls_ct_condition_t) x;
Dave Rodgmanef252792023-05-13 12:48:02 +0100165#elif defined(MBEDTLS_CT_ARM_ASM) && defined(MBEDTLS_CT_SIZE_32)
166 uint32_t s;
Dave Rodgman822c9c72023-06-12 15:38:49 +0100167 asm volatile (".syntax unified \n\t"
168 "negs %[s], %[x] \n\t"
169 "orrs %[x], %[x], %[s] \n\t"
170 "asrs %[x], %[x], #31 \n\t"
171 RESTORE_ASM_SYNTAX
Dave Rodgmanef252792023-05-13 12:48:02 +0100172 :
173 [s] "=&l" (s),
174 [x] "+&l" (x)
175 :
176 :
Dave Rodgman822c9c72023-06-12 15:38:49 +0100177 "cc" /* clobbers flag bits */
Dave Rodgmanef252792023-05-13 12:48:02 +0100178 );
179 return (mbedtls_ct_condition_t) x;
Dave Rodgman664fea42023-05-12 12:11:37 +0100180#elif defined(MBEDTLS_CT_X86_64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
181 uint64_t s;
182 asm volatile ("mov %[x], %[s] \n\t"
183 "neg %[s] \n\t"
184 "or %[x], %[s] \n\t"
185 "sar $63, %[s] \n\t"
186 :
187 [s] "=&a" (s)
188 :
189 [x] "D" (x)
190 :
191 );
192 return (mbedtls_ct_condition_t) s;
Dave Rodgmanc9ed5de2023-05-13 12:47:02 +0100193#else
Dave Rodgman40a41d02023-05-17 11:59:56 +0100194 const mbedtls_ct_uint_t xo = mbedtls_ct_compiler_opaque(x);
195#if defined(_MSC_VER)
196 /* MSVC has a warning about unary minus on unsigned, but this is
197 * well-defined and precisely what we want to do here */
198#pragma warning( push )
199#pragma warning( disable : 4146 )
200#endif
Dave Rodgman0c99a902023-08-21 17:06:24 +0100201 // y is negative (i.e., top bit set) iff x is non-zero
202 mbedtls_ct_int_t y = (-xo) | -(xo >> 1);
203
204 // extract only the sign bit of y so that y == 1 (if x is non-zero) or 0 (if x is zero)
205 y = (((mbedtls_ct_uint_t) y) >> (MBEDTLS_CT_SIZE - 1));
206
207 // -y has all bits set (if x is non-zero), or all bits clear (if x is zero)
208 return (mbedtls_ct_condition_t) (-y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100209#if defined(_MSC_VER)
210#pragma warning( pop )
211#endif
Dave Rodgmanc9ed5de2023-05-13 12:47:02 +0100212#endif
Dave Rodgman40a41d02023-05-17 11:59:56 +0100213}
214
215static inline mbedtls_ct_uint_t mbedtls_ct_if(mbedtls_ct_condition_t condition,
216 mbedtls_ct_uint_t if1,
217 mbedtls_ct_uint_t if0)
218{
Dave Rodgmanc9ed5de2023-05-13 12:47:02 +0100219#if defined(MBEDTLS_CT_AARCH64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
220 asm volatile ("and %x[if1], %x[if1], %x[condition] \n\t"
221 "mvn %x[condition], %x[condition] \n\t"
222 "and %x[condition], %x[condition], %x[if0] \n\t"
223 "orr %x[condition], %x[if1], %x[condition]"
224 :
225 [condition] "+&r" (condition),
226 [if1] "+&r" (if1)
227 :
228 [if0] "r" (if0)
229 :
230 );
231 return (mbedtls_ct_uint_t) condition;
Dave Rodgmanef252792023-05-13 12:48:02 +0100232#elif defined(MBEDTLS_CT_ARM_ASM) && defined(MBEDTLS_CT_SIZE_32)
Dave Rodgman822c9c72023-06-12 15:38:49 +0100233 asm volatile (".syntax unified \n\t"
234 "ands %[if1], %[if1], %[condition] \n\t"
235 "mvns %[condition], %[condition] \n\t"
236 "ands %[condition], %[condition], %[if0] \n\t"
237 "orrs %[condition], %[if1], %[condition] \n\t"
238 RESTORE_ASM_SYNTAX
Dave Rodgmanef252792023-05-13 12:48:02 +0100239 :
240 [condition] "+&l" (condition),
241 [if1] "+&l" (if1)
242 :
243 [if0] "l" (if0)
244 :
Dave Rodgman822c9c72023-06-12 15:38:49 +0100245 "cc"
Dave Rodgmanef252792023-05-13 12:48:02 +0100246 );
247 return (mbedtls_ct_uint_t) condition;
Dave Rodgman664fea42023-05-12 12:11:37 +0100248#elif defined(MBEDTLS_CT_X86_64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
249 asm volatile ("and %[condition], %[if1] \n\t"
250 "not %[condition] \n\t"
251 "and %[condition], %[if0] \n\t"
252 "or %[if1], %[if0] \n\t"
253 :
254 [condition] "+&D" (condition),
255 [if1] "+&S" (if1),
256 [if0] "+&a" (if0)
257 :
258 :
259 );
260 return if0;
Dave Rodgmanc9ed5de2023-05-13 12:47:02 +0100261#else
Dave Rodgman1c4eaa12023-05-17 12:22:59 +0100262 mbedtls_ct_condition_t not_cond =
Dave Rodgman40a41d02023-05-17 11:59:56 +0100263 (mbedtls_ct_condition_t) (~mbedtls_ct_compiler_opaque(condition));
Dave Rodgman1c4eaa12023-05-17 12:22:59 +0100264 return (mbedtls_ct_uint_t) ((condition & if1) | (not_cond & if0));
Dave Rodgmanc9ed5de2023-05-13 12:47:02 +0100265#endif
Dave Rodgman40a41d02023-05-17 11:59:56 +0100266}
267
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100268static 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 +0100269{
Dave Rodgmanc9ed5de2023-05-13 12:47:02 +0100270#if defined(MBEDTLS_CT_AARCH64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
Dave Rodgman0ce0fbc2023-08-21 07:58:50 +0100271 uint64_t s1;
Dave Rodgmanc9ed5de2023-05-13 12:47:02 +0100272 asm volatile ("eor %x[s1], %x[y], %x[x] \n\t"
Dave Rodgman0ce0fbc2023-08-21 07:58:50 +0100273 "sub %x[x], %x[x], %x[y] \n\t"
Dave Rodgmane20d6882023-08-22 08:46:18 +0100274 "bic %x[x], %x[x], %x[s1] \n\t"
Dave Rodgmanc9ed5de2023-05-13 12:47:02 +0100275 "and %x[s1], %x[s1], %x[y] \n\t"
Dave Rodgman0ce0fbc2023-08-21 07:58:50 +0100276 "orr %x[s1], %x[x], %x[s1] \n\t"
Dave Rodgmanc9ed5de2023-05-13 12:47:02 +0100277 "asr %x[x], %x[s1], 63"
Dave Rodgman0ce0fbc2023-08-21 07:58:50 +0100278 : [s1] "=&r" (s1), [x] "+&r" (x)
Dave Rodgmanc9ed5de2023-05-13 12:47:02 +0100279 : [y] "r" (y)
280 :
281 );
282 return (mbedtls_ct_condition_t) x;
Dave Rodgmanef252792023-05-13 12:48:02 +0100283#elif defined(MBEDTLS_CT_ARM_ASM) && defined(MBEDTLS_CT_SIZE_32)
284 uint32_t s1;
285 asm volatile (
Dave Rodgman822c9c72023-06-12 15:38:49 +0100286 ".syntax unified \n\t"
Dave Rodgmanef252792023-05-13 12:48:02 +0100287#if defined(__thumb__) && !defined(__thumb2__)
Dave Rodgman822c9c72023-06-12 15:38:49 +0100288 "movs %[s1], %[x] \n\t"
289 "eors %[s1], %[s1], %[y] \n\t"
Dave Rodgmanef252792023-05-13 12:48:02 +0100290#else
Dave Rodgman822c9c72023-06-12 15:38:49 +0100291 "eors %[s1], %[x], %[y] \n\t"
Dave Rodgmanef252792023-05-13 12:48:02 +0100292#endif
Dave Rodgman822c9c72023-06-12 15:38:49 +0100293 "subs %[x], %[x], %[y] \n\t"
294 "bics %[x], %[x], %[s1] \n\t"
295 "ands %[y], %[s1], %[y] \n\t"
296 "orrs %[x], %[x], %[y] \n\t"
297 "asrs %[x], %[x], #31 \n\t"
298 RESTORE_ASM_SYNTAX
Dave Rodgmanef252792023-05-13 12:48:02 +0100299 : [s1] "=&l" (s1), [x] "+&l" (x), [y] "+&l" (y)
300 :
301 :
Dave Rodgman822c9c72023-06-12 15:38:49 +0100302 "cc"
Dave Rodgmanef252792023-05-13 12:48:02 +0100303 );
304 return (mbedtls_ct_condition_t) x;
Dave Rodgman664fea42023-05-12 12:11:37 +0100305#elif defined(MBEDTLS_CT_X86_64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
306 uint64_t mask;
307 asm volatile ("mov %[x], %[mask] \n\t"
308 "xor %[y], %[mask] \n\t"
309 "sub %[y], %[x] \n\t"
310 "and %[mask], %[y] \n\t"
311 "not %[mask] \n\t"
312 "and %[mask], %[x] \n\t"
313 "or %[y], %[x] \n\t"
314 "mov %[x], %[mask] \n\t"
315 "sar $63, %[mask] \n\t"
316 :
317 [mask] "=&a" (mask),
318 [x] "+&S" (x),
319 [y] "+&D" (y)
320 :
321 :
322 );
323 return (mbedtls_ct_condition_t) mask;
Dave Rodgmanc9ed5de2023-05-13 12:47:02 +0100324#else
Dave Rodgman40a41d02023-05-17 11:59:56 +0100325 /* Ensure that the compiler cannot optimise the following operations over x and y,
326 * even if it knows the value of x and y.
327 */
Dave Rodgman74e18eb2023-05-17 12:21:32 +0100328 const mbedtls_ct_uint_t xo = mbedtls_ct_compiler_opaque(x);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100329 const mbedtls_ct_uint_t yo = mbedtls_ct_compiler_opaque(y);
330 /*
331 * Check if the most significant bits (MSB) of the operands are different.
332 * cond is true iff the MSBs differ.
333 */
Dave Rodgman74e18eb2023-05-17 12:21:32 +0100334 mbedtls_ct_condition_t cond = mbedtls_ct_bool((xo ^ yo) >> (MBEDTLS_CT_SIZE - 1));
Dave Rodgman40a41d02023-05-17 11:59:56 +0100335
336 /*
337 * If the MSB are the same then the difference x-y will be negative (and
338 * have its MSB set to 1 during conversion to unsigned) if and only if x<y.
339 *
340 * If the MSB are different, then the operand with the MSB of 1 is the
341 * bigger. (That is if y has MSB of 1, then x<y is true and it is false if
342 * the MSB of y is 0.)
343 */
344
345 // Select either y, or x - y
Dave Rodgman74e18eb2023-05-17 12:21:32 +0100346 mbedtls_ct_uint_t ret = mbedtls_ct_if(cond, yo, (mbedtls_ct_uint_t) (xo - yo));
Dave Rodgman40a41d02023-05-17 11:59:56 +0100347
348 // Extract only the MSB of ret
349 ret = ret >> (MBEDTLS_CT_SIZE - 1);
350
351 // Convert to a condition (i.e., all bits set iff non-zero)
352 return mbedtls_ct_bool(ret);
Dave Rodgmanc9ed5de2023-05-13 12:47:02 +0100353#endif
Dave Rodgman40a41d02023-05-17 11:59:56 +0100354}
355
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100356static 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 +0100357{
358 /* diff = 0 if x == y, non-zero otherwise */
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100359 const mbedtls_ct_uint_t diff = mbedtls_ct_compiler_opaque(x) ^ mbedtls_ct_compiler_opaque(y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100360
361 /* all ones if x != y, 0 otherwise */
362 return mbedtls_ct_bool(diff);
363}
364
365static inline unsigned char mbedtls_ct_uchar_in_range_if(unsigned char low,
366 unsigned char high,
367 unsigned char c,
368 unsigned char t)
369{
Agathiyan Bragadeesh9ebfa7f2023-08-17 10:00:01 +0100370 const unsigned char co = (unsigned char) mbedtls_ct_compiler_opaque(c);
371 const unsigned char to = (unsigned char) mbedtls_ct_compiler_opaque(t);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100372
373 /* low_mask is: 0 if low <= c, 0x...ff if low > c */
374 unsigned low_mask = ((unsigned) co - low) >> 8;
375 /* high_mask is: 0 if c <= high, 0x...ff if c > high */
376 unsigned high_mask = ((unsigned) high - co) >> 8;
377
378 return (unsigned char) (~(low_mask | high_mask)) & to;
379}
380
381
382/* ============================================================================
383 * Everything below here is trivial wrapper functions
384 */
385
Dave Rodgman40a41d02023-05-17 11:59:56 +0100386static inline size_t mbedtls_ct_size_if(mbedtls_ct_condition_t condition,
387 size_t if1,
388 size_t if0)
389{
390 return (size_t) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1, (mbedtls_ct_uint_t) if0);
391}
392
Dave Rodgman2b4486a2023-05-17 15:51:59 +0100393static inline unsigned mbedtls_ct_uint_if(mbedtls_ct_condition_t condition,
Dave Rodgman40a41d02023-05-17 11:59:56 +0100394 unsigned if1,
395 unsigned if0)
396{
397 return (unsigned) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1, (mbedtls_ct_uint_t) if0);
398}
399
400#if defined(MBEDTLS_BIGNUM_C)
401
Dave Rodgman585f7f72023-05-17 17:45:33 +0100402static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if(mbedtls_ct_condition_t condition,
403 mbedtls_mpi_uint if1,
Dave Rodgman40a41d02023-05-17 11:59:56 +0100404 mbedtls_mpi_uint if0)
405{
406 return (mbedtls_mpi_uint) mbedtls_ct_if(condition,
407 (mbedtls_ct_uint_t) if1,
408 (mbedtls_ct_uint_t) if0);
409}
410
411#endif
412
Dave Rodgman98ddc012023-08-10 12:11:31 +0100413static inline size_t mbedtls_ct_size_if_else_0(mbedtls_ct_condition_t condition, size_t if1)
Dave Rodgman40a41d02023-05-17 11:59:56 +0100414{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100415 return (size_t) (condition & if1);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100416}
417
Dave Rodgman98ddc012023-08-10 12:11:31 +0100418static inline unsigned mbedtls_ct_uint_if_else_0(mbedtls_ct_condition_t condition, unsigned if1)
Dave Rodgman40a41d02023-05-17 11:59:56 +0100419{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100420 return (unsigned) (condition & if1);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100421}
422
423#if defined(MBEDTLS_BIGNUM_C)
424
Dave Rodgman98ddc012023-08-10 12:11:31 +0100425static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if_else_0(mbedtls_ct_condition_t condition,
426 mbedtls_mpi_uint if1)
Dave Rodgman40a41d02023-05-17 11:59:56 +0100427{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100428 return (mbedtls_mpi_uint) (condition & if1);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100429}
430
431#endif /* MBEDTLS_BIGNUM_C */
432
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100433static inline mbedtls_ct_condition_t mbedtls_ct_uint_eq(mbedtls_ct_uint_t x,
Dave Rodgman585f7f72023-05-17 17:45:33 +0100434 mbedtls_ct_uint_t y)
435{
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100436 return ~mbedtls_ct_uint_ne(x, y);
Dave Rodgman585f7f72023-05-17 17:45:33 +0100437}
438
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100439static inline mbedtls_ct_condition_t mbedtls_ct_uint_gt(mbedtls_ct_uint_t x,
Dave Rodgman40a41d02023-05-17 11:59:56 +0100440 mbedtls_ct_uint_t y)
441{
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100442 return mbedtls_ct_uint_lt(y, x);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100443}
444
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100445static inline mbedtls_ct_condition_t mbedtls_ct_uint_ge(mbedtls_ct_uint_t x,
Dave Rodgman40a41d02023-05-17 11:59:56 +0100446 mbedtls_ct_uint_t y)
447{
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100448 return ~mbedtls_ct_uint_lt(x, y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100449}
450
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100451static inline mbedtls_ct_condition_t mbedtls_ct_uint_le(mbedtls_ct_uint_t x,
Dave Rodgman40a41d02023-05-17 11:59:56 +0100452 mbedtls_ct_uint_t y)
453{
Dave Rodgmanb7825ce2023-08-10 11:58:18 +0100454 return ~mbedtls_ct_uint_gt(x, y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100455}
456
457static inline mbedtls_ct_condition_t mbedtls_ct_bool_xor(mbedtls_ct_condition_t x,
458 mbedtls_ct_condition_t y)
459{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100460 return (mbedtls_ct_condition_t) (x ^ y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100461}
462
463static inline mbedtls_ct_condition_t mbedtls_ct_bool_and(mbedtls_ct_condition_t x,
464 mbedtls_ct_condition_t y)
465{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100466 return (mbedtls_ct_condition_t) (x & y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100467}
468
469static inline mbedtls_ct_condition_t mbedtls_ct_bool_or(mbedtls_ct_condition_t x,
470 mbedtls_ct_condition_t y)
471{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100472 return (mbedtls_ct_condition_t) (x | y);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100473}
474
475static inline mbedtls_ct_condition_t mbedtls_ct_bool_not(mbedtls_ct_condition_t x)
476{
Dave Rodgmanfe76af22023-05-17 17:45:17 +0100477 return (mbedtls_ct_condition_t) (~x);
Dave Rodgman40a41d02023-05-17 11:59:56 +0100478}
479
Dave Rodgman205295c2023-08-01 14:10:56 +0100480#ifdef __GNUC__
481 #pragma GCC diagnostic pop
482#endif
483
Dave Rodgman40a41d02023-05-17 11:59:56 +0100484#endif /* MBEDTLS_CONSTANT_TIME_IMPL_H */