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