gabor-mezei-arm | db9a38c | 2021-09-27 11:28:54 +0200 | [diff] [blame] | 1 | /** |
| 2 | * Constant-time functions |
| 3 | * |
| 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 | |
Gabor Mezei | 291df7b | 2021-10-19 11:27:17 +0200 | [diff] [blame] | 20 | #ifndef MBEDTLS_CONSTANT_TIME_INTERNAL_H |
| 21 | #define MBEDTLS_CONSTANT_TIME_INTERNAL_H |
| 22 | |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 23 | #include <stdint.h> |
| 24 | #include <stddef.h> |
| 25 | |
gabor-mezei-arm | db9a38c | 2021-09-27 11:28:54 +0200 | [diff] [blame] | 26 | #include "common.h" |
| 27 | |
gabor-mezei-arm | 3f90fd5 | 2021-09-27 12:55:33 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_BIGNUM_C) |
| 29 | #include "mbedtls/bignum.h" |
| 30 | #endif |
| 31 | |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 32 | /* The constant-time interface provides various operations that are likely |
| 33 | * to result in constant-time code that does not branch or use conditional |
| 34 | * instructions for secret data (for secret pointers, this also applies to |
| 35 | * the data pointed to). |
| 36 | * |
| 37 | * It has three main parts: |
| 38 | * |
Dave Rodgman | f27727b | 2023-05-13 12:12:02 +0100 | [diff] [blame] | 39 | * - boolean operations |
Dave Rodgman | 38b227c | 2023-08-10 12:18:27 +0100 | [diff] [blame^] | 40 | * These are all named mbedtls_ct_<type>_<operation>. |
| 41 | * They operate over <type> and return mbedtls_ct_condition_t. |
Dave Rodgman | f27727b | 2023-05-13 12:12:02 +0100 | [diff] [blame] | 42 | * All arguments are considered secret. |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 43 | * example: bool x = y | z => x = mbedtls_ct_bool_or(y, z) |
Dave Rodgman | b7825ce | 2023-08-10 11:58:18 +0100 | [diff] [blame] | 44 | * example: bool x = y == z => x = mbedtls_ct_uint_eq(y, z) |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 45 | * |
| 46 | * - conditional data selection |
| 47 | * These are all named mbedtls_ct_<type>_if and mbedtls_ct_<type>_if0 |
| 48 | * All arguments are considered secret. |
| 49 | * example: size_t a = x ? b : c => a = mbedtls_ct_size_if(x, b, c) |
| 50 | * example: unsigned a = x ? b : 0 => a = mbedtls_ct_uint_if0(x, b) |
| 51 | * |
| 52 | * - block memory operations |
| 53 | * Only some arguments are considered secret, as documented for each |
| 54 | * function. |
| 55 | * example: if (x) memcpy(...) => mbedtls_ct_memcpy_if(x, ...) |
| 56 | * |
Dave Rodgman | 93cec45 | 2023-07-31 12:30:26 +0100 | [diff] [blame] | 57 | * mbedtls_ct_condition_t must be treated as opaque and only created and |
| 58 | * manipulated via the functions in this header. The compiler should never |
| 59 | * be able to prove anything about its value at compile-time. |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 60 | * |
| 61 | * mbedtls_ct_uint_t is an unsigned integer type over which constant time |
| 62 | * operations may be performed via the functions in this header. It is as big |
| 63 | * as the larger of size_t and mbedtls_mpi_uint, i.e. it is safe to cast |
| 64 | * to/from "unsigned int", "size_t", and "mbedtls_mpi_uint" (and any other |
| 65 | * not-larger integer types). |
| 66 | * |
Dave Rodgman | f27727b | 2023-05-13 12:12:02 +0100 | [diff] [blame] | 67 | * For Arm (32-bit, 64-bit and Thumb), x86 and x86-64, assembly implementations |
| 68 | * are used to ensure that the generated code is constant time. For other |
Dave Rodgman | 0172de8 | 2023-07-31 12:32:23 +0100 | [diff] [blame] | 69 | * architectures, it uses a plain C fallback designed to yield constant-time code |
| 70 | * (this has been observed to be constant-time on latest gcc, clang and MSVC |
| 71 | * as of May 2023). |
Dave Rodgman | 205295c | 2023-08-01 14:10:56 +0100 | [diff] [blame] | 72 | * |
| 73 | * For readability, the static inline definitions are separated out into |
| 74 | * constant_time_impl.h. |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 75 | */ |
| 76 | |
| 77 | #if (SIZE_MAX > 0xffffffffffffffffULL) |
| 78 | /* Pointer size > 64-bit */ |
| 79 | typedef size_t mbedtls_ct_condition_t; |
| 80 | typedef size_t mbedtls_ct_uint_t; |
| 81 | typedef ptrdiff_t mbedtls_ct_int_t; |
Dave Rodgman | fba5598 | 2023-07-14 13:44:22 +0100 | [diff] [blame] | 82 | #define MBEDTLS_CT_TRUE ((mbedtls_ct_condition_t) mbedtls_ct_compiler_opaque(SIZE_MAX)) |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 83 | #elif (SIZE_MAX > 0xffffffff) || defined(MBEDTLS_HAVE_INT64) |
Dave Rodgman | c882adf | 2023-06-21 07:37:56 +0100 | [diff] [blame] | 84 | /* 32-bit < pointer size <= 64-bit, or 64-bit MPI */ |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 85 | typedef uint64_t mbedtls_ct_condition_t; |
| 86 | typedef uint64_t mbedtls_ct_uint_t; |
| 87 | typedef int64_t mbedtls_ct_int_t; |
Dave Rodgman | fba5598 | 2023-07-14 13:44:22 +0100 | [diff] [blame] | 88 | #define MBEDTLS_CT_TRUE ((mbedtls_ct_condition_t) mbedtls_ct_compiler_opaque(UINT64_MAX)) |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 89 | #else |
Dave Rodgman | c882adf | 2023-06-21 07:37:56 +0100 | [diff] [blame] | 90 | /* Pointer size <= 32-bit, and no 64-bit MPIs */ |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 91 | typedef uint32_t mbedtls_ct_condition_t; |
| 92 | typedef uint32_t mbedtls_ct_uint_t; |
| 93 | typedef int32_t mbedtls_ct_int_t; |
Dave Rodgman | fba5598 | 2023-07-14 13:44:22 +0100 | [diff] [blame] | 94 | #define MBEDTLS_CT_TRUE ((mbedtls_ct_condition_t) mbedtls_ct_compiler_opaque(UINT32_MAX)) |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 95 | #endif |
Dave Rodgman | fba5598 | 2023-07-14 13:44:22 +0100 | [diff] [blame] | 96 | #define MBEDTLS_CT_FALSE ((mbedtls_ct_condition_t) mbedtls_ct_compiler_opaque(0)) |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 97 | |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 98 | /* ============================================================================ |
| 99 | * Boolean operations |
| 100 | */ |
| 101 | |
| 102 | /** Convert a number into a mbedtls_ct_condition_t. |
| 103 | * |
| 104 | * \param x Number to convert. |
| 105 | * |
| 106 | * \return MBEDTLS_CT_TRUE if \p x != 0, or MBEDTLS_CT_FALSE if \p x == 0 |
| 107 | * |
| 108 | */ |
| 109 | static inline mbedtls_ct_condition_t mbedtls_ct_bool(mbedtls_ct_uint_t x); |
| 110 | |
| 111 | /** Boolean "not equal" operation. |
| 112 | * |
| 113 | * Functionally equivalent to: |
| 114 | * |
| 115 | * \p x != \p y |
| 116 | * |
| 117 | * \param x The first value to analyze. |
| 118 | * \param y The second value to analyze. |
| 119 | * |
| 120 | * \return MBEDTLS_CT_TRUE if \p x != \p y, otherwise MBEDTLS_CT_FALSE. |
| 121 | */ |
Dave Rodgman | b7825ce | 2023-08-10 11:58:18 +0100 | [diff] [blame] | 122 | 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] | 123 | |
| 124 | /** Boolean "equals" operation. |
| 125 | * |
| 126 | * Functionally equivalent to: |
| 127 | * |
| 128 | * \p x == \p y |
| 129 | * |
| 130 | * \param x The first value to analyze. |
| 131 | * \param y The second value to analyze. |
| 132 | * |
| 133 | * \return MBEDTLS_CT_TRUE if \p x == \p y, otherwise MBEDTLS_CT_FALSE. |
| 134 | */ |
Dave Rodgman | b7825ce | 2023-08-10 11:58:18 +0100 | [diff] [blame] | 135 | static inline mbedtls_ct_condition_t mbedtls_ct_uint_eq(mbedtls_ct_uint_t x, |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 136 | mbedtls_ct_uint_t y); |
| 137 | |
| 138 | /** Boolean "less than" operation. |
| 139 | * |
| 140 | * Functionally equivalent to: |
| 141 | * |
| 142 | * \p x < \p y |
| 143 | * |
| 144 | * \param x The first value to analyze. |
| 145 | * \param y The second value to analyze. |
| 146 | * |
| 147 | * \return MBEDTLS_CT_TRUE if \p x < \p y, otherwise MBEDTLS_CT_FALSE. |
| 148 | */ |
Dave Rodgman | b7825ce | 2023-08-10 11:58:18 +0100 | [diff] [blame] | 149 | 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] | 150 | |
| 151 | /** Boolean "greater than" operation. |
| 152 | * |
| 153 | * Functionally equivalent to: |
| 154 | * |
| 155 | * \p x > \p y |
| 156 | * |
| 157 | * \param x The first value to analyze. |
| 158 | * \param y The second value to analyze. |
| 159 | * |
| 160 | * \return MBEDTLS_CT_TRUE if \p x > \p y, otherwise MBEDTLS_CT_FALSE. |
| 161 | */ |
Dave Rodgman | b7825ce | 2023-08-10 11:58:18 +0100 | [diff] [blame] | 162 | 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] | 163 | mbedtls_ct_uint_t y); |
| 164 | |
| 165 | /** Boolean "greater or equal" operation. |
| 166 | * |
| 167 | * Functionally equivalent to: |
| 168 | * |
| 169 | * \p x >= \p y |
| 170 | * |
| 171 | * \param x The first value to analyze. |
| 172 | * \param y The second value to analyze. |
| 173 | * |
| 174 | * \return MBEDTLS_CT_TRUE if \p x >= \p y, |
| 175 | * otherwise MBEDTLS_CT_FALSE. |
| 176 | */ |
Dave Rodgman | b7825ce | 2023-08-10 11:58:18 +0100 | [diff] [blame] | 177 | 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] | 178 | mbedtls_ct_uint_t y); |
| 179 | |
| 180 | /** Boolean "less than or equal" operation. |
| 181 | * |
| 182 | * Functionally equivalent to: |
| 183 | * |
| 184 | * \p x <= \p y |
| 185 | * |
| 186 | * \param x The first value to analyze. |
| 187 | * \param y The second value to analyze. |
| 188 | * |
| 189 | * \return MBEDTLS_CT_TRUE if \p x <= \p y, |
| 190 | * otherwise MBEDTLS_CT_FALSE. |
| 191 | */ |
Dave Rodgman | b7825ce | 2023-08-10 11:58:18 +0100 | [diff] [blame] | 192 | 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] | 193 | mbedtls_ct_uint_t y); |
| 194 | |
| 195 | /** Boolean "xor" operation. |
| 196 | * |
| 197 | * Functionally equivalent to: |
| 198 | * |
| 199 | * \p x ^ \p y |
| 200 | * |
| 201 | * \param x The first value to analyze. |
| 202 | * \param y The second value to analyze. |
| 203 | * |
| 204 | * \note This is more efficient than mbedtls_ct_bool_ne if both arguments are |
| 205 | * mbedtls_ct_condition_t. |
| 206 | * |
| 207 | * \return MBEDTLS_CT_TRUE if \p x ^ \p y, |
| 208 | * otherwise MBEDTLS_CT_FALSE. |
| 209 | */ |
| 210 | static inline mbedtls_ct_condition_t mbedtls_ct_bool_xor(mbedtls_ct_condition_t x, |
| 211 | mbedtls_ct_condition_t y); |
| 212 | |
| 213 | /** Boolean "and" operation. |
| 214 | * |
| 215 | * Functionally equivalent to: |
| 216 | * |
| 217 | * \p x && \p y |
| 218 | * |
| 219 | * \param x The first value to analyze. |
| 220 | * \param y The second value to analyze. |
| 221 | * |
| 222 | * \return MBEDTLS_CT_TRUE if \p x && \p y, |
| 223 | * otherwise MBEDTLS_CT_FALSE. |
| 224 | */ |
| 225 | static inline mbedtls_ct_condition_t mbedtls_ct_bool_and(mbedtls_ct_condition_t x, |
| 226 | mbedtls_ct_condition_t y); |
| 227 | |
| 228 | /** Boolean "or" operation. |
| 229 | * |
| 230 | * Functionally equivalent to: |
| 231 | * |
| 232 | * \p x || \p y |
| 233 | * |
| 234 | * \param x The first value to analyze. |
| 235 | * \param y The second value to analyze. |
| 236 | * |
| 237 | * \return MBEDTLS_CT_TRUE if \p x || \p y, |
| 238 | * otherwise MBEDTLS_CT_FALSE. |
| 239 | */ |
| 240 | static inline mbedtls_ct_condition_t mbedtls_ct_bool_or(mbedtls_ct_condition_t x, |
| 241 | mbedtls_ct_condition_t y); |
| 242 | |
| 243 | /** Boolean "not" operation. |
| 244 | * |
| 245 | * Functionally equivalent to: |
| 246 | * |
| 247 | * ! \p x |
| 248 | * |
| 249 | * \param x The value to invert |
| 250 | * |
| 251 | * \return MBEDTLS_CT_FALSE if \p x, otherwise MBEDTLS_CT_TRUE. |
| 252 | */ |
| 253 | static inline mbedtls_ct_condition_t mbedtls_ct_bool_not(mbedtls_ct_condition_t x); |
| 254 | |
| 255 | |
| 256 | /* ============================================================================ |
| 257 | * Data selection operations |
| 258 | */ |
| 259 | |
| 260 | /** Choose between two size_t values. |
| 261 | * |
| 262 | * Functionally equivalent to: |
| 263 | * |
| 264 | * condition ? if1 : if0. |
| 265 | * |
| 266 | * \param condition Condition to test. |
| 267 | * \param if1 Value to use if \p condition == MBEDTLS_CT_TRUE. |
| 268 | * \param if0 Value to use if \p condition == MBEDTLS_CT_FALSE. |
| 269 | * |
| 270 | * \return \c if1 if \p condition == MBEDTLS_CT_TRUE, otherwise \c if0. |
| 271 | */ |
| 272 | static inline size_t mbedtls_ct_size_if(mbedtls_ct_condition_t condition, |
| 273 | size_t if1, |
| 274 | size_t if0); |
| 275 | |
| 276 | /** Choose between two unsigned values. |
| 277 | * |
| 278 | * Functionally equivalent to: |
| 279 | * |
| 280 | * condition ? if1 : if0. |
| 281 | * |
| 282 | * \param condition Condition to test. |
| 283 | * \param if1 Value to use if \p condition == MBEDTLS_CT_TRUE. |
| 284 | * \param if0 Value to use if \p condition == MBEDTLS_CT_FALSE. |
| 285 | * |
| 286 | * \return \c if1 if \p condition == MBEDTLS_CT_TRUE, otherwise \c if0. |
| 287 | */ |
Dave Rodgman | 2b4486a | 2023-05-17 15:51:59 +0100 | [diff] [blame] | 288 | static inline unsigned mbedtls_ct_uint_if(mbedtls_ct_condition_t condition, |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 289 | unsigned if1, |
| 290 | unsigned if0); |
| 291 | |
| 292 | #if defined(MBEDTLS_BIGNUM_C) |
| 293 | |
| 294 | /** Choose between two mbedtls_mpi_uint values. |
| 295 | * |
| 296 | * Functionally equivalent to: |
| 297 | * |
| 298 | * condition ? if1 : if0. |
| 299 | * |
| 300 | * \param condition Condition to test. |
| 301 | * \param if1 Value to use if \p condition == MBEDTLS_CT_TRUE. |
| 302 | * \param if0 Value to use if \p condition == MBEDTLS_CT_FALSE. |
| 303 | * |
| 304 | * \return \c if1 if \p condition == MBEDTLS_CT_TRUE, otherwise \c if0. |
| 305 | */ |
| 306 | static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if(mbedtls_ct_condition_t condition, \ |
| 307 | mbedtls_mpi_uint if1, \ |
| 308 | mbedtls_mpi_uint if0); |
| 309 | |
| 310 | #endif |
| 311 | |
| 312 | /** Choose between an unsigned value and 0. |
| 313 | * |
| 314 | * Functionally equivalent to: |
| 315 | * |
| 316 | * condition ? if1 : 0. |
| 317 | * |
Dave Rodgman | 065f912 | 2023-08-10 12:11:58 +0100 | [diff] [blame] | 318 | * Functionally equivalent to mbedtls_ct_uint_if(condition, if1, 0) but |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 319 | * results in smaller code size. |
| 320 | * |
| 321 | * \param condition Condition to test. |
| 322 | * \param if1 Value to use if \p condition == MBEDTLS_CT_TRUE. |
| 323 | * |
| 324 | * \return \c if1 if \p condition == MBEDTLS_CT_TRUE, otherwise 0. |
| 325 | */ |
Dave Rodgman | 98ddc01 | 2023-08-10 12:11:31 +0100 | [diff] [blame] | 326 | 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] | 327 | |
Dave Rodgman | ac69b45 | 2023-08-10 12:13:27 +0100 | [diff] [blame] | 328 | /** Choose between an unsigned value and 0. |
| 329 | * |
| 330 | * Functionally equivalent to: |
| 331 | * |
| 332 | * condition ? if1 : 0. |
| 333 | * |
| 334 | * Functionally equivalent to mbedtls_ct_size_if(condition, if1, 0) but |
| 335 | * results in smaller code size. |
| 336 | * |
| 337 | * \param condition Condition to test. |
| 338 | * \param if1 Value to use if \p condition == MBEDTLS_CT_TRUE. |
| 339 | * |
| 340 | * \return \c if1 if \p condition == MBEDTLS_CT_TRUE, otherwise 0. |
| 341 | */ |
| 342 | static inline size_t mbedtls_ct_size_if_else_0(mbedtls_ct_condition_t condition, size_t if1); |
| 343 | |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 344 | #if defined(MBEDTLS_BIGNUM_C) |
| 345 | |
| 346 | /** Choose between an mbedtls_mpi_uint value and 0. |
| 347 | * |
| 348 | * Functionally equivalent to: |
| 349 | * |
| 350 | * condition ? if1 : 0. |
| 351 | * |
Dave Rodgman | 065f912 | 2023-08-10 12:11:58 +0100 | [diff] [blame] | 352 | * Functionally equivalent to mbedtls_ct_mpi_uint_if(condition, if1, 0) but |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 353 | * results in smaller code size. |
| 354 | * |
| 355 | * \param condition Condition to test. |
| 356 | * \param if1 Value to use if \p condition == MBEDTLS_CT_TRUE. |
| 357 | * |
| 358 | * \return \c if1 if \p condition == MBEDTLS_CT_TRUE, otherwise 0. |
| 359 | */ |
Dave Rodgman | 98ddc01 | 2023-08-10 12:11:31 +0100 | [diff] [blame] | 360 | static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if_else_0(mbedtls_ct_condition_t condition, |
| 361 | mbedtls_mpi_uint if1); |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 362 | |
| 363 | #endif |
| 364 | |
| 365 | /** Constant-flow char selection |
| 366 | * |
| 367 | * \param low Secret. Bottom of range |
| 368 | * \param high Secret. Top of range |
| 369 | * \param c Secret. Value to compare to range |
| 370 | * \param t Secret. Value to return, if in range |
| 371 | * |
| 372 | * \return \p t if \p low <= \p c <= \p high, 0 otherwise. |
| 373 | */ |
| 374 | static inline unsigned char mbedtls_ct_uchar_in_range_if(unsigned char low, |
| 375 | unsigned char high, |
| 376 | unsigned char c, |
| 377 | unsigned char t); |
| 378 | |
| 379 | |
| 380 | /* ============================================================================ |
| 381 | * Block memory operations |
| 382 | */ |
| 383 | |
| 384 | #if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT) |
| 385 | |
| 386 | /** Conditionally set a block of memory to zero. |
| 387 | * |
| 388 | * Regardless of the condition, every byte will be read once and written to |
| 389 | * once. |
| 390 | * |
| 391 | * \param condition Secret. Condition to test. |
| 392 | * \param buf Secret. Pointer to the start of the buffer. |
| 393 | * \param len Number of bytes to set to zero. |
| 394 | * |
| 395 | * \warning Unlike mbedtls_platform_zeroize, this does not have the same guarantees |
| 396 | * about not being optimised away if the memory is never read again. |
| 397 | */ |
| 398 | void mbedtls_ct_zeroize_if(mbedtls_ct_condition_t condition, void *buf, size_t len); |
| 399 | |
| 400 | /** Shift some data towards the left inside a buffer. |
| 401 | * |
| 402 | * Functionally equivalent to: |
| 403 | * |
| 404 | * memmove(start, start + offset, total - offset); |
| 405 | * memset(start + (total - offset), 0, offset); |
| 406 | * |
| 407 | * Timing independence comes at the expense of performance. |
| 408 | * |
| 409 | * \param start Secret. Pointer to the start of the buffer. |
| 410 | * \param total Total size of the buffer. |
| 411 | * \param offset Secret. Offset from which to copy \p total - \p offset bytes. |
| 412 | */ |
| 413 | void mbedtls_ct_memmove_left(void *start, |
| 414 | size_t total, |
| 415 | size_t offset); |
| 416 | |
| 417 | #endif /* defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT) */ |
| 418 | |
| 419 | /** Conditional memcpy. |
| 420 | * |
| 421 | * Functionally equivalent to: |
| 422 | * |
| 423 | * if (condition) { |
| 424 | * memcpy(dest, src1, len); |
| 425 | * } else { |
| 426 | * if (src2 != NULL) |
| 427 | * memcpy(dest, src2, len); |
| 428 | * } |
| 429 | * |
| 430 | * It will always read len bytes from src1. |
| 431 | * If src2 != NULL, it will always read len bytes from src2. |
| 432 | * If src2 == NULL, it will instead read len bytes from dest (as if src2 == dest). |
| 433 | * |
| 434 | * \param condition The condition |
| 435 | * \param dest Secret. Destination pointer. |
Dave Rodgman | 3108645 | 2023-05-18 13:47:13 +0100 | [diff] [blame] | 436 | * \param src1 Secret. Pointer to copy from (if \p condition == MBEDTLS_CT_TRUE). |
| 437 | * This may be equal to \p dest, but may not overlap in other ways. |
Dave Rodgman | 741d423 | 2023-07-31 12:31:01 +0100 | [diff] [blame] | 438 | * \param src2 Secret (contents only - may branch to determine if this parameter is NULL). |
Dave Rodgman | 3108645 | 2023-05-18 13:47:13 +0100 | [diff] [blame] | 439 | * Pointer to copy from (if \p condition == MBEDTLS_CT_FALSE and \p src2 is not NULL). May be NULL. |
| 440 | * This may be equal to \p dest, but may not overlap it in other ways. It may overlap with \p src1. |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 441 | * \param len Number of bytes to copy. |
| 442 | */ |
| 443 | void mbedtls_ct_memcpy_if(mbedtls_ct_condition_t condition, |
| 444 | unsigned char *dest, |
| 445 | const unsigned char *src1, |
| 446 | const unsigned char *src2, |
| 447 | size_t len |
| 448 | ); |
| 449 | |
| 450 | /** Copy data from a secret position. |
| 451 | * |
| 452 | * Functionally equivalent to: |
| 453 | * |
| 454 | * memcpy(dst, src + offset, len) |
| 455 | * |
| 456 | * This function copies \p len bytes from \p src_base + \p offset to \p |
| 457 | * dst, with a code flow and memory access pattern that does not depend on |
| 458 | * \p offset, but only on \p offset_min, \p offset_max and \p len. |
| 459 | * |
| 460 | * \note This function reads from \p dest, but the value that |
| 461 | * is read does not influence the result and this |
| 462 | * function's behavior is well-defined regardless of the |
| 463 | * contents of the buffers. This may result in false |
| 464 | * positives from static or dynamic analyzers, especially |
| 465 | * if \p dest is not initialized. |
| 466 | * |
| 467 | * \param dest Secret. The destination buffer. This must point to a writable |
| 468 | * buffer of at least \p len bytes. |
| 469 | * \param src Secret. The base of the source buffer. This must point to a |
| 470 | * readable buffer of at least \p offset_max + \p len |
Dave Rodgman | 3108645 | 2023-05-18 13:47:13 +0100 | [diff] [blame] | 471 | * bytes. Shouldn't overlap with \p dest |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 472 | * \param offset Secret. The offset in the source buffer from which to copy. |
| 473 | * This must be no less than \p offset_min and no greater |
| 474 | * than \p offset_max. |
| 475 | * \param offset_min The minimal value of \p offset. |
| 476 | * \param offset_max The maximal value of \p offset. |
| 477 | * \param len The number of bytes to copy. |
| 478 | */ |
| 479 | void mbedtls_ct_memcpy_offset(unsigned char *dest, |
| 480 | const unsigned char *src, |
| 481 | size_t offset, |
| 482 | size_t offset_min, |
| 483 | size_t offset_max, |
| 484 | size_t len); |
| 485 | |
Dave Rodgman | 9ee0e1f | 2023-07-31 12:33:36 +0100 | [diff] [blame] | 486 | /* Documented in include/mbedtls/constant_time.h. a and b are secret. |
| 487 | |
| 488 | int mbedtls_ct_memcmp(const void *a, |
| 489 | const void *b, |
| 490 | size_t n); |
| 491 | */ |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame] | 492 | |
Dave Rodgman | 205295c | 2023-08-01 14:10:56 +0100 | [diff] [blame] | 493 | /* Include the implementation of static inline functions above. */ |
| 494 | #include "constant_time_impl.h" |
Dave Rodgman | a02b368 | 2023-07-14 13:43:39 +0100 | [diff] [blame] | 495 | |
Gabor Mezei | 291df7b | 2021-10-19 11:27:17 +0200 | [diff] [blame] | 496 | #endif /* MBEDTLS_CONSTANT_TIME_INTERNAL_H */ |