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 | 0869167 | 2023-07-28 16:17:57 +0100 | [diff] [blame] | 40 | #include "../tests/include/test/constant_flow.h" |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 41 | |
| 42 | /* armcc5 --gnu defines __GNUC__ but doesn't support GNU's extended asm */ |
| 43 | #if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && (!defined(__ARMCC_VERSION) || \ |
Dave Rodgman | 983448e | 2023-07-28 17:30:52 +0100 | [diff] [blame] | 44 | __ARMCC_VERSION >= 6000000) |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 45 | #define MBEDTLS_CT_ASM |
| 46 | #if (defined(__arm__) || defined(__thumb__) || defined(__thumb2__)) |
| 47 | #define MBEDTLS_CT_ARM_ASM |
| 48 | #elif defined(__aarch64__) |
| 49 | #define MBEDTLS_CT_AARCH64_ASM |
| 50 | #endif |
| 51 | #endif |
| 52 | |
| 53 | #define MBEDTLS_CT_SIZE (sizeof(mbedtls_ct_uint_t) * 8) |
| 54 | |
| 55 | |
| 56 | /* ============================================================================ |
| 57 | * Core const-time primitives |
| 58 | */ |
| 59 | |
Dave Rodgman | 2894d00 | 2023-06-08 17:52:21 +0100 | [diff] [blame] | 60 | /* 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] | 61 | * based on its value) after this function is called. |
| 62 | * |
| 63 | * If we are not using assembly, this will be fairly inefficient, so its use |
| 64 | * should be minimised. |
| 65 | */ |
Dave Rodgman | 2894d00 | 2023-06-08 17:52:21 +0100 | [diff] [blame] | 66 | |
| 67 | #if !defined(MBEDTLS_CT_ASM) |
Dave Rodgman | 58c80f4 | 2023-06-12 18:19:46 +0100 | [diff] [blame] | 68 | extern volatile mbedtls_ct_uint_t mbedtls_ct_zero; |
Dave Rodgman | 2894d00 | 2023-06-08 17:52:21 +0100 | [diff] [blame] | 69 | #endif |
| 70 | |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 71 | static inline mbedtls_ct_uint_t mbedtls_ct_compiler_opaque(mbedtls_ct_uint_t x) |
| 72 | { |
| 73 | #if defined(MBEDTLS_CT_ASM) |
Dave Rodgman | 0869167 | 2023-07-28 16:17:57 +0100 | [diff] [blame] | 74 | /* Prevent false positives from Memsan - otherwise it will report the asm as |
| 75 | * accessing secret data. */ |
Dave Rodgman | 2d28c46 | 2023-07-28 18:22:56 +0100 | [diff] [blame] | 76 | TEST_CF_SAVE_SECRET(x); |
Dave Rodgman | 0869167 | 2023-07-28 16:17:57 +0100 | [diff] [blame] | 77 | |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 78 | asm volatile ("" : [x] "+r" (x) :); |
Dave Rodgman | 0869167 | 2023-07-28 16:17:57 +0100 | [diff] [blame] | 79 | |
Dave Rodgman | 2d28c46 | 2023-07-28 18:22:56 +0100 | [diff] [blame] | 80 | /* Mark the return value as secret (if it was previously marked secret). |
| 81 | * This is needed so that code of the form: |
Dave Rodgman | 0869167 | 2023-07-28 16:17:57 +0100 | [diff] [blame] | 82 | * |
| 83 | * if (mbedtls_ct_compiler_opaque(secret)) { ... } |
| 84 | * |
| 85 | * will fail const-flow tests. |
| 86 | */ |
Dave Rodgman | 2d28c46 | 2023-07-28 18:22:56 +0100 | [diff] [blame] | 87 | TEST_CF_RESTORE_SECRET(x); |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 88 | return x; |
| 89 | #else |
Dave Rodgman | 2894d00 | 2023-06-08 17:52:21 +0100 | [diff] [blame] | 90 | return x ^ mbedtls_ct_zero; |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 91 | #endif |
| 92 | } |
| 93 | |
| 94 | /* Convert a number into a condition in constant time. */ |
| 95 | static inline mbedtls_ct_condition_t mbedtls_ct_bool(mbedtls_ct_uint_t x) |
| 96 | { |
| 97 | /* |
| 98 | * Define mask-generation code that, as far as possible, will not use branches or conditional instructions. |
| 99 | * |
| 100 | * For some platforms / type sizes, we define assembly to assure this. |
| 101 | * |
| 102 | * Otherwise, we define a plain C fallback which (in May 2023) does not get optimised into |
| 103 | * conditional instructions or branches by trunk clang, gcc, or MSVC v19. |
| 104 | */ |
| 105 | const mbedtls_ct_uint_t xo = mbedtls_ct_compiler_opaque(x); |
| 106 | #if defined(_MSC_VER) |
| 107 | /* MSVC has a warning about unary minus on unsigned, but this is |
| 108 | * well-defined and precisely what we want to do here */ |
| 109 | #pragma warning( push ) |
| 110 | #pragma warning( disable : 4146 ) |
| 111 | #endif |
| 112 | return (mbedtls_ct_condition_t) (((mbedtls_ct_int_t) ((-xo) | -(xo >> 1))) >> |
| 113 | (MBEDTLS_CT_SIZE - 1)); |
| 114 | #if defined(_MSC_VER) |
| 115 | #pragma warning( pop ) |
| 116 | #endif |
| 117 | } |
| 118 | |
| 119 | static inline mbedtls_ct_uint_t mbedtls_ct_if(mbedtls_ct_condition_t condition, |
| 120 | mbedtls_ct_uint_t if1, |
| 121 | mbedtls_ct_uint_t if0) |
| 122 | { |
Dave Rodgman | 1c4eaa1 | 2023-05-17 12:22:59 +0100 | [diff] [blame] | 123 | mbedtls_ct_condition_t not_cond = |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 124 | (mbedtls_ct_condition_t) (~mbedtls_ct_compiler_opaque(condition)); |
Dave Rodgman | 1c4eaa1 | 2023-05-17 12:22:59 +0100 | [diff] [blame] | 125 | return (mbedtls_ct_uint_t) ((condition & if1) | (not_cond & if0)); |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static inline mbedtls_ct_condition_t mbedtls_ct_bool_lt(mbedtls_ct_uint_t x, mbedtls_ct_uint_t y) |
| 129 | { |
| 130 | /* Ensure that the compiler cannot optimise the following operations over x and y, |
| 131 | * even if it knows the value of x and y. |
| 132 | */ |
Dave Rodgman | 74e18eb | 2023-05-17 12:21:32 +0100 | [diff] [blame] | 133 | const mbedtls_ct_uint_t xo = mbedtls_ct_compiler_opaque(x); |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 134 | const mbedtls_ct_uint_t yo = mbedtls_ct_compiler_opaque(y); |
| 135 | /* |
| 136 | * Check if the most significant bits (MSB) of the operands are different. |
| 137 | * cond is true iff the MSBs differ. |
| 138 | */ |
Dave Rodgman | 74e18eb | 2023-05-17 12:21:32 +0100 | [diff] [blame] | 139 | 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] | 140 | |
| 141 | /* |
| 142 | * If the MSB are the same then the difference x-y will be negative (and |
| 143 | * have its MSB set to 1 during conversion to unsigned) if and only if x<y. |
| 144 | * |
| 145 | * If the MSB are different, then the operand with the MSB of 1 is the |
| 146 | * bigger. (That is if y has MSB of 1, then x<y is true and it is false if |
| 147 | * the MSB of y is 0.) |
| 148 | */ |
| 149 | |
| 150 | // Select either y, or x - y |
Dave Rodgman | 74e18eb | 2023-05-17 12:21:32 +0100 | [diff] [blame] | 151 | 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] | 152 | |
| 153 | // Extract only the MSB of ret |
| 154 | ret = ret >> (MBEDTLS_CT_SIZE - 1); |
| 155 | |
| 156 | // Convert to a condition (i.e., all bits set iff non-zero) |
| 157 | return mbedtls_ct_bool(ret); |
| 158 | } |
| 159 | |
| 160 | static inline mbedtls_ct_condition_t mbedtls_ct_bool_ne(mbedtls_ct_uint_t x, mbedtls_ct_uint_t y) |
| 161 | { |
| 162 | /* diff = 0 if x == y, non-zero otherwise */ |
Dave Rodgman | fe76af2 | 2023-05-17 17:45:17 +0100 | [diff] [blame] | 163 | 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] | 164 | |
| 165 | /* all ones if x != y, 0 otherwise */ |
| 166 | return mbedtls_ct_bool(diff); |
| 167 | } |
| 168 | |
| 169 | static inline unsigned char mbedtls_ct_uchar_in_range_if(unsigned char low, |
| 170 | unsigned char high, |
| 171 | unsigned char c, |
| 172 | unsigned char t) |
| 173 | { |
| 174 | const unsigned char co = (const unsigned char) mbedtls_ct_compiler_opaque(c); |
| 175 | const unsigned char to = (const unsigned char) mbedtls_ct_compiler_opaque(t); |
| 176 | |
| 177 | /* low_mask is: 0 if low <= c, 0x...ff if low > c */ |
| 178 | unsigned low_mask = ((unsigned) co - low) >> 8; |
| 179 | /* high_mask is: 0 if c <= high, 0x...ff if c > high */ |
| 180 | unsigned high_mask = ((unsigned) high - co) >> 8; |
| 181 | |
| 182 | return (unsigned char) (~(low_mask | high_mask)) & to; |
| 183 | } |
| 184 | |
| 185 | |
| 186 | /* ============================================================================ |
| 187 | * Everything below here is trivial wrapper functions |
| 188 | */ |
| 189 | |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 190 | static inline size_t mbedtls_ct_size_if(mbedtls_ct_condition_t condition, |
| 191 | size_t if1, |
| 192 | size_t if0) |
| 193 | { |
| 194 | return (size_t) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1, (mbedtls_ct_uint_t) if0); |
| 195 | } |
| 196 | |
Dave Rodgman | 2b4486a | 2023-05-17 15:51:59 +0100 | [diff] [blame] | 197 | static inline unsigned mbedtls_ct_uint_if(mbedtls_ct_condition_t condition, |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 198 | unsigned if1, |
| 199 | unsigned if0) |
| 200 | { |
| 201 | return (unsigned) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1, (mbedtls_ct_uint_t) if0); |
| 202 | } |
| 203 | |
| 204 | #if defined(MBEDTLS_BIGNUM_C) |
| 205 | |
Dave Rodgman | 585f7f7 | 2023-05-17 17:45:33 +0100 | [diff] [blame] | 206 | static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if(mbedtls_ct_condition_t condition, |
| 207 | mbedtls_mpi_uint if1, |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 208 | mbedtls_mpi_uint if0) |
| 209 | { |
| 210 | return (mbedtls_mpi_uint) mbedtls_ct_if(condition, |
| 211 | (mbedtls_ct_uint_t) if1, |
| 212 | (mbedtls_ct_uint_t) if0); |
| 213 | } |
| 214 | |
| 215 | #endif |
| 216 | |
| 217 | static inline size_t mbedtls_ct_size_if0(mbedtls_ct_condition_t condition, size_t if1) |
| 218 | { |
Dave Rodgman | fe76af2 | 2023-05-17 17:45:17 +0100 | [diff] [blame] | 219 | return (size_t) (condition & if1); |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | static inline unsigned mbedtls_ct_uint_if0(mbedtls_ct_condition_t condition, unsigned if1) |
| 223 | { |
Dave Rodgman | fe76af2 | 2023-05-17 17:45:17 +0100 | [diff] [blame] | 224 | return (unsigned) (condition & if1); |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | #if defined(MBEDTLS_BIGNUM_C) |
| 228 | |
| 229 | static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if0(mbedtls_ct_condition_t condition, |
| 230 | mbedtls_mpi_uint if1) |
| 231 | { |
Dave Rodgman | fe76af2 | 2023-05-17 17:45:17 +0100 | [diff] [blame] | 232 | return (mbedtls_mpi_uint) (condition & if1); |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | #endif /* MBEDTLS_BIGNUM_C */ |
| 236 | |
Dave Rodgman | 585f7f7 | 2023-05-17 17:45:33 +0100 | [diff] [blame] | 237 | static inline mbedtls_ct_condition_t mbedtls_ct_bool_eq(mbedtls_ct_uint_t x, |
| 238 | mbedtls_ct_uint_t y) |
| 239 | { |
| 240 | return ~mbedtls_ct_bool_ne(x, y); |
| 241 | } |
| 242 | |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 243 | static inline mbedtls_ct_condition_t mbedtls_ct_bool_gt(mbedtls_ct_uint_t x, |
| 244 | mbedtls_ct_uint_t y) |
| 245 | { |
| 246 | return mbedtls_ct_bool_lt(y, x); |
| 247 | } |
| 248 | |
| 249 | static inline mbedtls_ct_condition_t mbedtls_ct_bool_ge(mbedtls_ct_uint_t x, |
| 250 | mbedtls_ct_uint_t y) |
| 251 | { |
| 252 | return ~mbedtls_ct_bool_lt(x, y); |
| 253 | } |
| 254 | |
| 255 | static inline mbedtls_ct_condition_t mbedtls_ct_bool_le(mbedtls_ct_uint_t x, |
| 256 | mbedtls_ct_uint_t y) |
| 257 | { |
| 258 | return ~mbedtls_ct_bool_gt(x, y); |
| 259 | } |
| 260 | |
| 261 | static inline mbedtls_ct_condition_t mbedtls_ct_bool_xor(mbedtls_ct_condition_t x, |
| 262 | mbedtls_ct_condition_t y) |
| 263 | { |
Dave Rodgman | fe76af2 | 2023-05-17 17:45:17 +0100 | [diff] [blame] | 264 | return (mbedtls_ct_condition_t) (x ^ y); |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | static inline mbedtls_ct_condition_t mbedtls_ct_bool_and(mbedtls_ct_condition_t x, |
| 268 | mbedtls_ct_condition_t y) |
| 269 | { |
Dave Rodgman | fe76af2 | 2023-05-17 17:45:17 +0100 | [diff] [blame] | 270 | return (mbedtls_ct_condition_t) (x & y); |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | static inline mbedtls_ct_condition_t mbedtls_ct_bool_or(mbedtls_ct_condition_t x, |
| 274 | mbedtls_ct_condition_t y) |
| 275 | { |
Dave Rodgman | fe76af2 | 2023-05-17 17:45:17 +0100 | [diff] [blame] | 276 | return (mbedtls_ct_condition_t) (x | y); |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | static inline mbedtls_ct_condition_t mbedtls_ct_bool_not(mbedtls_ct_condition_t x) |
| 280 | { |
Dave Rodgman | fe76af2 | 2023-05-17 17:45:17 +0100 | [diff] [blame] | 281 | return (mbedtls_ct_condition_t) (~x); |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | #endif /* MBEDTLS_CONSTANT_TIME_IMPL_H */ |