Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 1 | /** |
| 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 Rodgman | 205295c | 2023-08-01 14:10:56 +0100 | [diff] [blame] | 40 | /* 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 Rodgman | 3d574da | 2023-07-31 16:54:00 +0100 | [diff] [blame] | 51 | /* Disable asm under Memsan because it confuses Memsan and generates false errors */ |
| 52 | #if defined(MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN) |
| 53 | #define MBEDTLS_CT_NO_ASM |
| 54 | #elif defined(__has_feature) |
| 55 | #if __has_feature(memory_sanitizer) |
| 56 | #define MBEDTLS_CT_NO_ASM |
| 57 | #endif |
| 58 | #endif |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 59 | |
| 60 | /* armcc5 --gnu defines __GNUC__ but doesn't support GNU's extended asm */ |
| 61 | #if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && (!defined(__ARMCC_VERSION) || \ |
Dave Rodgman | 3d574da | 2023-07-31 16:54:00 +0100 | [diff] [blame] | 62 | __ARMCC_VERSION >= 6000000) && !defined(MBEDTLS_CT_NO_ASM) |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 63 | #define MBEDTLS_CT_ASM |
| 64 | #if (defined(__arm__) || defined(__thumb__) || defined(__thumb2__)) |
| 65 | #define MBEDTLS_CT_ARM_ASM |
| 66 | #elif defined(__aarch64__) |
| 67 | #define MBEDTLS_CT_AARCH64_ASM |
| 68 | #endif |
| 69 | #endif |
| 70 | |
| 71 | #define MBEDTLS_CT_SIZE (sizeof(mbedtls_ct_uint_t) * 8) |
| 72 | |
| 73 | |
| 74 | /* ============================================================================ |
| 75 | * Core const-time primitives |
| 76 | */ |
| 77 | |
Dave Rodgman | 2894d00 | 2023-06-08 17:52:21 +0100 | [diff] [blame] | 78 | /* Ensure that the compiler cannot know the value of x (i.e., cannot optimise |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 79 | * based on its value) after this function is called. |
| 80 | * |
| 81 | * If we are not using assembly, this will be fairly inefficient, so its use |
| 82 | * should be minimised. |
| 83 | */ |
Dave Rodgman | 2894d00 | 2023-06-08 17:52:21 +0100 | [diff] [blame] | 84 | |
| 85 | #if !defined(MBEDTLS_CT_ASM) |
Dave Rodgman | 58c80f4 | 2023-06-12 18:19:46 +0100 | [diff] [blame] | 86 | extern volatile mbedtls_ct_uint_t mbedtls_ct_zero; |
Dave Rodgman | 2894d00 | 2023-06-08 17:52:21 +0100 | [diff] [blame] | 87 | #endif |
| 88 | |
Dave Rodgman | 93cec45 | 2023-07-31 12:30:26 +0100 | [diff] [blame] | 89 | /** |
| 90 | * \brief Ensure that a value cannot be known at compile time. |
| 91 | * |
| 92 | * \param x The value to hide from the compiler. |
| 93 | * \return The same value that was passed in, such that the compiler |
| 94 | * cannot prove its value (even for calls of the form |
| 95 | * x = mbedtls_ct_compiler_opaque(1), x will be unknown). |
| 96 | * |
| 97 | * \note This is mainly used in constructing mbedtls_ct_condition_t |
| 98 | * values and performing operations over them, to ensure that |
| 99 | * there is no way for the compiler to ever know anything about |
| 100 | * the value of an mbedtls_ct_condition_t. |
| 101 | */ |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 102 | static inline mbedtls_ct_uint_t mbedtls_ct_compiler_opaque(mbedtls_ct_uint_t x) |
| 103 | { |
| 104 | #if defined(MBEDTLS_CT_ASM) |
| 105 | asm volatile ("" : [x] "+r" (x) :); |
| 106 | return x; |
| 107 | #else |
Dave Rodgman | 2894d00 | 2023-06-08 17:52:21 +0100 | [diff] [blame] | 108 | return x ^ mbedtls_ct_zero; |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 109 | #endif |
| 110 | } |
| 111 | |
| 112 | /* Convert a number into a condition in constant time. */ |
| 113 | static inline mbedtls_ct_condition_t mbedtls_ct_bool(mbedtls_ct_uint_t x) |
| 114 | { |
| 115 | /* |
| 116 | * Define mask-generation code that, as far as possible, will not use branches or conditional instructions. |
| 117 | * |
| 118 | * For some platforms / type sizes, we define assembly to assure this. |
| 119 | * |
| 120 | * Otherwise, we define a plain C fallback which (in May 2023) does not get optimised into |
| 121 | * conditional instructions or branches by trunk clang, gcc, or MSVC v19. |
| 122 | */ |
Dave Rodgman | c9ed5de | 2023-05-13 12:47:02 +0100 | [diff] [blame] | 123 | #if defined(MBEDTLS_CT_AARCH64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64)) |
| 124 | mbedtls_ct_uint_t s; |
| 125 | asm volatile ("neg %x[s], %x[x] \n\t" |
| 126 | "orr %x[x], %x[s], %x[x] \n\t" |
| 127 | "asr %x[x], %x[x], 63" |
| 128 | : |
| 129 | [s] "=&r" (s), |
| 130 | [x] "+&r" (x) |
| 131 | : |
| 132 | : |
| 133 | ); |
| 134 | return (mbedtls_ct_condition_t) x; |
Dave Rodgman | ef25279 | 2023-05-13 12:48:02 +0100 | [diff] [blame] | 135 | #elif defined(MBEDTLS_CT_ARM_ASM) && defined(MBEDTLS_CT_SIZE_32) |
| 136 | uint32_t s; |
Dave Rodgman | 822c9c7 | 2023-06-12 15:38:49 +0100 | [diff] [blame^] | 137 | /* |
| 138 | * Selecting unified syntax is needed for gcc, and harmless on clang. |
| 139 | * |
| 140 | * This is needed because on Thumb 1, condition flags are always set, so |
| 141 | * e.g. "negs" is supported but "neg" is not (on Thumb 2, both exist). |
| 142 | * |
| 143 | * Under Thumb 1 unified syntax, only the "negs" form is accepted, and |
| 144 | * under divided syntax, only the "neg" form is accepted. clang only |
| 145 | * supports unified syntax. |
| 146 | * |
| 147 | * On Thumb 2 and Arm, both compilers are happy with the "s" suffix, |
| 148 | * although we don't actually care about setting the flags. |
| 149 | * |
| 150 | * For gcc, restore divided syntax afterwards - otherwise old versions of gcc |
| 151 | * seem to apply unified syntax globally, which breaks other asm code. |
| 152 | */ |
| 153 | #if !defined(__clang__) |
| 154 | #define RESTORE_ASM_SYNTAX ".syntax divided \n\t" |
| 155 | #else |
| 156 | #define RESTORE_ASM_SYNTAX |
| 157 | #endif |
| 158 | |
| 159 | asm volatile (".syntax unified \n\t" |
| 160 | "negs %[s], %[x] \n\t" |
| 161 | "orrs %[x], %[x], %[s] \n\t" |
| 162 | "asrs %[x], %[x], #31 \n\t" |
| 163 | RESTORE_ASM_SYNTAX |
Dave Rodgman | ef25279 | 2023-05-13 12:48:02 +0100 | [diff] [blame] | 164 | : |
| 165 | [s] "=&l" (s), |
| 166 | [x] "+&l" (x) |
| 167 | : |
| 168 | : |
Dave Rodgman | 822c9c7 | 2023-06-12 15:38:49 +0100 | [diff] [blame^] | 169 | "cc" /* clobbers flag bits */ |
Dave Rodgman | ef25279 | 2023-05-13 12:48:02 +0100 | [diff] [blame] | 170 | ); |
| 171 | return (mbedtls_ct_condition_t) x; |
Dave Rodgman | c9ed5de | 2023-05-13 12:47:02 +0100 | [diff] [blame] | 172 | #else |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 173 | const mbedtls_ct_uint_t xo = mbedtls_ct_compiler_opaque(x); |
| 174 | #if defined(_MSC_VER) |
| 175 | /* MSVC has a warning about unary minus on unsigned, but this is |
| 176 | * well-defined and precisely what we want to do here */ |
| 177 | #pragma warning( push ) |
| 178 | #pragma warning( disable : 4146 ) |
| 179 | #endif |
| 180 | return (mbedtls_ct_condition_t) (((mbedtls_ct_int_t) ((-xo) | -(xo >> 1))) >> |
| 181 | (MBEDTLS_CT_SIZE - 1)); |
| 182 | #if defined(_MSC_VER) |
| 183 | #pragma warning( pop ) |
| 184 | #endif |
Dave Rodgman | c9ed5de | 2023-05-13 12:47:02 +0100 | [diff] [blame] | 185 | #endif |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | static inline mbedtls_ct_uint_t mbedtls_ct_if(mbedtls_ct_condition_t condition, |
| 189 | mbedtls_ct_uint_t if1, |
| 190 | mbedtls_ct_uint_t if0) |
| 191 | { |
Dave Rodgman | c9ed5de | 2023-05-13 12:47:02 +0100 | [diff] [blame] | 192 | #if defined(MBEDTLS_CT_AARCH64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64)) |
| 193 | asm volatile ("and %x[if1], %x[if1], %x[condition] \n\t" |
| 194 | "mvn %x[condition], %x[condition] \n\t" |
| 195 | "and %x[condition], %x[condition], %x[if0] \n\t" |
| 196 | "orr %x[condition], %x[if1], %x[condition]" |
| 197 | : |
| 198 | [condition] "+&r" (condition), |
| 199 | [if1] "+&r" (if1) |
| 200 | : |
| 201 | [if0] "r" (if0) |
| 202 | : |
| 203 | ); |
| 204 | return (mbedtls_ct_uint_t) condition; |
Dave Rodgman | ef25279 | 2023-05-13 12:48:02 +0100 | [diff] [blame] | 205 | #elif defined(MBEDTLS_CT_ARM_ASM) && defined(MBEDTLS_CT_SIZE_32) |
Dave Rodgman | 822c9c7 | 2023-06-12 15:38:49 +0100 | [diff] [blame^] | 206 | asm volatile (".syntax unified \n\t" |
| 207 | "ands %[if1], %[if1], %[condition] \n\t" |
| 208 | "mvns %[condition], %[condition] \n\t" |
| 209 | "ands %[condition], %[condition], %[if0] \n\t" |
| 210 | "orrs %[condition], %[if1], %[condition] \n\t" |
| 211 | RESTORE_ASM_SYNTAX |
Dave Rodgman | ef25279 | 2023-05-13 12:48:02 +0100 | [diff] [blame] | 212 | : |
| 213 | [condition] "+&l" (condition), |
| 214 | [if1] "+&l" (if1) |
| 215 | : |
| 216 | [if0] "l" (if0) |
| 217 | : |
Dave Rodgman | 822c9c7 | 2023-06-12 15:38:49 +0100 | [diff] [blame^] | 218 | "cc" |
Dave Rodgman | ef25279 | 2023-05-13 12:48:02 +0100 | [diff] [blame] | 219 | ); |
| 220 | return (mbedtls_ct_uint_t) condition; |
Dave Rodgman | c9ed5de | 2023-05-13 12:47:02 +0100 | [diff] [blame] | 221 | #else |
Dave Rodgman | 1c4eaa1 | 2023-05-17 12:22:59 +0100 | [diff] [blame] | 222 | mbedtls_ct_condition_t not_cond = |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 223 | (mbedtls_ct_condition_t) (~mbedtls_ct_compiler_opaque(condition)); |
Dave Rodgman | 1c4eaa1 | 2023-05-17 12:22:59 +0100 | [diff] [blame] | 224 | return (mbedtls_ct_uint_t) ((condition & if1) | (not_cond & if0)); |
Dave Rodgman | c9ed5de | 2023-05-13 12:47:02 +0100 | [diff] [blame] | 225 | #endif |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 226 | } |
| 227 | |
Dave Rodgman | b7825ce | 2023-08-10 11:58:18 +0100 | [diff] [blame] | 228 | static inline mbedtls_ct_condition_t mbedtls_ct_uint_lt(mbedtls_ct_uint_t x, mbedtls_ct_uint_t y) |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 229 | { |
Dave Rodgman | c9ed5de | 2023-05-13 12:47:02 +0100 | [diff] [blame] | 230 | #if defined(MBEDTLS_CT_AARCH64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64)) |
| 231 | uint64_t s1, s2; |
| 232 | asm volatile ("eor %x[s1], %x[y], %x[x] \n\t" |
| 233 | "sub %x[s2], %x[x], %x[y] \n\t" |
| 234 | "bic %x[s2], %x[s2], %[s1] \n\t" |
| 235 | "and %x[s1], %x[s1], %x[y] \n\t" |
| 236 | "orr %x[s1], %x[s2], %x[s1] \n\t" |
| 237 | "asr %x[x], %x[s1], 63" |
| 238 | : [s1] "=&r" (s1), [s2] "=&r" (s2), [x] "+r" (x) |
| 239 | : [y] "r" (y) |
| 240 | : |
| 241 | ); |
| 242 | return (mbedtls_ct_condition_t) x; |
Dave Rodgman | ef25279 | 2023-05-13 12:48:02 +0100 | [diff] [blame] | 243 | #elif defined(MBEDTLS_CT_ARM_ASM) && defined(MBEDTLS_CT_SIZE_32) |
| 244 | uint32_t s1; |
| 245 | asm volatile ( |
Dave Rodgman | 822c9c7 | 2023-06-12 15:38:49 +0100 | [diff] [blame^] | 246 | ".syntax unified \n\t" |
Dave Rodgman | ef25279 | 2023-05-13 12:48:02 +0100 | [diff] [blame] | 247 | #if defined(__thumb__) && !defined(__thumb2__) |
Dave Rodgman | 822c9c7 | 2023-06-12 15:38:49 +0100 | [diff] [blame^] | 248 | "movs %[s1], %[x] \n\t" |
| 249 | "eors %[s1], %[s1], %[y] \n\t" |
Dave Rodgman | ef25279 | 2023-05-13 12:48:02 +0100 | [diff] [blame] | 250 | #else |
Dave Rodgman | 822c9c7 | 2023-06-12 15:38:49 +0100 | [diff] [blame^] | 251 | "eors %[s1], %[x], %[y] \n\t" |
Dave Rodgman | ef25279 | 2023-05-13 12:48:02 +0100 | [diff] [blame] | 252 | #endif |
Dave Rodgman | 822c9c7 | 2023-06-12 15:38:49 +0100 | [diff] [blame^] | 253 | "subs %[x], %[x], %[y] \n\t" |
| 254 | "bics %[x], %[x], %[s1] \n\t" |
| 255 | "ands %[y], %[s1], %[y] \n\t" |
| 256 | "orrs %[x], %[x], %[y] \n\t" |
| 257 | "asrs %[x], %[x], #31 \n\t" |
| 258 | RESTORE_ASM_SYNTAX |
Dave Rodgman | ef25279 | 2023-05-13 12:48:02 +0100 | [diff] [blame] | 259 | : [s1] "=&l" (s1), [x] "+&l" (x), [y] "+&l" (y) |
| 260 | : |
| 261 | : |
Dave Rodgman | 822c9c7 | 2023-06-12 15:38:49 +0100 | [diff] [blame^] | 262 | "cc" |
Dave Rodgman | ef25279 | 2023-05-13 12:48:02 +0100 | [diff] [blame] | 263 | ); |
| 264 | return (mbedtls_ct_condition_t) x; |
Dave Rodgman | c9ed5de | 2023-05-13 12:47:02 +0100 | [diff] [blame] | 265 | #else |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 266 | /* Ensure that the compiler cannot optimise the following operations over x and y, |
| 267 | * even if it knows the value of x and y. |
| 268 | */ |
Dave Rodgman | 74e18eb | 2023-05-17 12:21:32 +0100 | [diff] [blame] | 269 | const mbedtls_ct_uint_t xo = mbedtls_ct_compiler_opaque(x); |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 270 | const mbedtls_ct_uint_t yo = mbedtls_ct_compiler_opaque(y); |
| 271 | /* |
| 272 | * Check if the most significant bits (MSB) of the operands are different. |
| 273 | * cond is true iff the MSBs differ. |
| 274 | */ |
Dave Rodgman | 74e18eb | 2023-05-17 12:21:32 +0100 | [diff] [blame] | 275 | mbedtls_ct_condition_t cond = mbedtls_ct_bool((xo ^ yo) >> (MBEDTLS_CT_SIZE - 1)); |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 276 | |
| 277 | /* |
| 278 | * If the MSB are the same then the difference x-y will be negative (and |
| 279 | * have its MSB set to 1 during conversion to unsigned) if and only if x<y. |
| 280 | * |
| 281 | * If the MSB are different, then the operand with the MSB of 1 is the |
| 282 | * bigger. (That is if y has MSB of 1, then x<y is true and it is false if |
| 283 | * the MSB of y is 0.) |
| 284 | */ |
| 285 | |
| 286 | // Select either y, or x - y |
Dave Rodgman | 74e18eb | 2023-05-17 12:21:32 +0100 | [diff] [blame] | 287 | mbedtls_ct_uint_t ret = mbedtls_ct_if(cond, yo, (mbedtls_ct_uint_t) (xo - yo)); |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 288 | |
| 289 | // Extract only the MSB of ret |
| 290 | ret = ret >> (MBEDTLS_CT_SIZE - 1); |
| 291 | |
| 292 | // Convert to a condition (i.e., all bits set iff non-zero) |
| 293 | return mbedtls_ct_bool(ret); |
Dave Rodgman | c9ed5de | 2023-05-13 12:47:02 +0100 | [diff] [blame] | 294 | #endif |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 295 | } |
| 296 | |
Dave Rodgman | b7825ce | 2023-08-10 11:58:18 +0100 | [diff] [blame] | 297 | static inline mbedtls_ct_condition_t mbedtls_ct_uint_ne(mbedtls_ct_uint_t x, mbedtls_ct_uint_t y) |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 298 | { |
| 299 | /* diff = 0 if x == y, non-zero otherwise */ |
Dave Rodgman | fe76af2 | 2023-05-17 17:45:17 +0100 | [diff] [blame] | 300 | const mbedtls_ct_uint_t diff = mbedtls_ct_compiler_opaque(x) ^ mbedtls_ct_compiler_opaque(y); |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 301 | |
| 302 | /* all ones if x != y, 0 otherwise */ |
| 303 | return mbedtls_ct_bool(diff); |
| 304 | } |
| 305 | |
| 306 | static inline unsigned char mbedtls_ct_uchar_in_range_if(unsigned char low, |
| 307 | unsigned char high, |
| 308 | unsigned char c, |
| 309 | unsigned char t) |
| 310 | { |
| 311 | const unsigned char co = (const unsigned char) mbedtls_ct_compiler_opaque(c); |
| 312 | const unsigned char to = (const unsigned char) mbedtls_ct_compiler_opaque(t); |
| 313 | |
| 314 | /* low_mask is: 0 if low <= c, 0x...ff if low > c */ |
| 315 | unsigned low_mask = ((unsigned) co - low) >> 8; |
| 316 | /* high_mask is: 0 if c <= high, 0x...ff if c > high */ |
| 317 | unsigned high_mask = ((unsigned) high - co) >> 8; |
| 318 | |
| 319 | return (unsigned char) (~(low_mask | high_mask)) & to; |
| 320 | } |
| 321 | |
| 322 | |
| 323 | /* ============================================================================ |
| 324 | * Everything below here is trivial wrapper functions |
| 325 | */ |
| 326 | |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 327 | static inline size_t mbedtls_ct_size_if(mbedtls_ct_condition_t condition, |
| 328 | size_t if1, |
| 329 | size_t if0) |
| 330 | { |
| 331 | return (size_t) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1, (mbedtls_ct_uint_t) if0); |
| 332 | } |
| 333 | |
Dave Rodgman | 2b4486a | 2023-05-17 15:51:59 +0100 | [diff] [blame] | 334 | static inline unsigned mbedtls_ct_uint_if(mbedtls_ct_condition_t condition, |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 335 | unsigned if1, |
| 336 | unsigned if0) |
| 337 | { |
| 338 | return (unsigned) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1, (mbedtls_ct_uint_t) if0); |
| 339 | } |
| 340 | |
| 341 | #if defined(MBEDTLS_BIGNUM_C) |
| 342 | |
Dave Rodgman | 585f7f7 | 2023-05-17 17:45:33 +0100 | [diff] [blame] | 343 | static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if(mbedtls_ct_condition_t condition, |
| 344 | mbedtls_mpi_uint if1, |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 345 | mbedtls_mpi_uint if0) |
| 346 | { |
| 347 | return (mbedtls_mpi_uint) mbedtls_ct_if(condition, |
| 348 | (mbedtls_ct_uint_t) if1, |
| 349 | (mbedtls_ct_uint_t) if0); |
| 350 | } |
| 351 | |
| 352 | #endif |
| 353 | |
Dave Rodgman | 98ddc01 | 2023-08-10 12:11:31 +0100 | [diff] [blame] | 354 | static inline size_t mbedtls_ct_size_if_else_0(mbedtls_ct_condition_t condition, size_t if1) |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 355 | { |
Dave Rodgman | fe76af2 | 2023-05-17 17:45:17 +0100 | [diff] [blame] | 356 | return (size_t) (condition & if1); |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 357 | } |
| 358 | |
Dave Rodgman | 98ddc01 | 2023-08-10 12:11:31 +0100 | [diff] [blame] | 359 | static inline unsigned mbedtls_ct_uint_if_else_0(mbedtls_ct_condition_t condition, unsigned if1) |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 360 | { |
Dave Rodgman | fe76af2 | 2023-05-17 17:45:17 +0100 | [diff] [blame] | 361 | return (unsigned) (condition & if1); |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | #if defined(MBEDTLS_BIGNUM_C) |
| 365 | |
Dave Rodgman | 98ddc01 | 2023-08-10 12:11:31 +0100 | [diff] [blame] | 366 | static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if_else_0(mbedtls_ct_condition_t condition, |
| 367 | mbedtls_mpi_uint if1) |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 368 | { |
Dave Rodgman | fe76af2 | 2023-05-17 17:45:17 +0100 | [diff] [blame] | 369 | return (mbedtls_mpi_uint) (condition & if1); |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | #endif /* MBEDTLS_BIGNUM_C */ |
| 373 | |
Dave Rodgman | b7825ce | 2023-08-10 11:58:18 +0100 | [diff] [blame] | 374 | static inline mbedtls_ct_condition_t mbedtls_ct_uint_eq(mbedtls_ct_uint_t x, |
Dave Rodgman | 585f7f7 | 2023-05-17 17:45:33 +0100 | [diff] [blame] | 375 | mbedtls_ct_uint_t y) |
| 376 | { |
Dave Rodgman | b7825ce | 2023-08-10 11:58:18 +0100 | [diff] [blame] | 377 | return ~mbedtls_ct_uint_ne(x, y); |
Dave Rodgman | 585f7f7 | 2023-05-17 17:45:33 +0100 | [diff] [blame] | 378 | } |
| 379 | |
Dave Rodgman | b7825ce | 2023-08-10 11:58:18 +0100 | [diff] [blame] | 380 | static inline mbedtls_ct_condition_t mbedtls_ct_uint_gt(mbedtls_ct_uint_t x, |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 381 | mbedtls_ct_uint_t y) |
| 382 | { |
Dave Rodgman | b7825ce | 2023-08-10 11:58:18 +0100 | [diff] [blame] | 383 | return mbedtls_ct_uint_lt(y, x); |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 384 | } |
| 385 | |
Dave Rodgman | b7825ce | 2023-08-10 11:58:18 +0100 | [diff] [blame] | 386 | static inline mbedtls_ct_condition_t mbedtls_ct_uint_ge(mbedtls_ct_uint_t x, |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 387 | mbedtls_ct_uint_t y) |
| 388 | { |
Dave Rodgman | b7825ce | 2023-08-10 11:58:18 +0100 | [diff] [blame] | 389 | return ~mbedtls_ct_uint_lt(x, y); |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 390 | } |
| 391 | |
Dave Rodgman | b7825ce | 2023-08-10 11:58:18 +0100 | [diff] [blame] | 392 | static inline mbedtls_ct_condition_t mbedtls_ct_uint_le(mbedtls_ct_uint_t x, |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 393 | mbedtls_ct_uint_t y) |
| 394 | { |
Dave Rodgman | b7825ce | 2023-08-10 11:58:18 +0100 | [diff] [blame] | 395 | return ~mbedtls_ct_uint_gt(x, y); |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | static inline mbedtls_ct_condition_t mbedtls_ct_bool_xor(mbedtls_ct_condition_t x, |
| 399 | mbedtls_ct_condition_t y) |
| 400 | { |
Dave Rodgman | fe76af2 | 2023-05-17 17:45:17 +0100 | [diff] [blame] | 401 | return (mbedtls_ct_condition_t) (x ^ y); |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | static inline mbedtls_ct_condition_t mbedtls_ct_bool_and(mbedtls_ct_condition_t x, |
| 405 | mbedtls_ct_condition_t y) |
| 406 | { |
Dave Rodgman | fe76af2 | 2023-05-17 17:45:17 +0100 | [diff] [blame] | 407 | return (mbedtls_ct_condition_t) (x & y); |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | static inline mbedtls_ct_condition_t mbedtls_ct_bool_or(mbedtls_ct_condition_t x, |
| 411 | mbedtls_ct_condition_t y) |
| 412 | { |
Dave Rodgman | fe76af2 | 2023-05-17 17:45:17 +0100 | [diff] [blame] | 413 | return (mbedtls_ct_condition_t) (x | y); |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | static inline mbedtls_ct_condition_t mbedtls_ct_bool_not(mbedtls_ct_condition_t x) |
| 417 | { |
Dave Rodgman | fe76af2 | 2023-05-17 17:45:17 +0100 | [diff] [blame] | 418 | return (mbedtls_ct_condition_t) (~x); |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 419 | } |
| 420 | |
Dave Rodgman | 205295c | 2023-08-01 14:10:56 +0100 | [diff] [blame] | 421 | #ifdef __GNUC__ |
| 422 | #pragma GCC diagnostic pop |
| 423 | #endif |
| 424 | |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 425 | #endif /* MBEDTLS_CONSTANT_TIME_IMPL_H */ |