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 | |
gabor-mezei-arm | 1349ffd | 2021-09-27 14:28:31 +0200 | [diff] [blame] | 32 | #if defined(MBEDTLS_SSL_TLS_C) |
| 33 | #include "ssl_misc.h" |
| 34 | #endif |
| 35 | |
gabor-mezei-arm | db9a38c | 2021-09-27 11:28:54 +0200 | [diff] [blame] | 36 | |
gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 37 | /** Turn a value into a mask: |
| 38 | * - if \p value == 0, return the all-bits 0 mask, aka 0 |
Gabor Mezei | a316fc8 | 2021-10-18 16:28:27 +0200 | [diff] [blame] | 39 | * - otherwise, return the all-bits 1 mask, aka (unsigned) -1 |
gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 40 | * |
| 41 | * This function can be used to write constant-time code by replacing branches |
| 42 | * with bit operations using masks. |
| 43 | * |
gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 44 | * \param value The value to analyze. |
| 45 | * |
| 46 | * \return Zero if \p value is zero, otherwise all-bits-one. |
| 47 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 48 | unsigned mbedtls_ct_uint_mask(unsigned value); |
gabor-mezei-arm | 3733bf8 | 2021-09-27 11:49:42 +0200 | [diff] [blame] | 49 | |
Andrzej Kurek | 084334c | 2022-09-27 14:19:50 -0400 | [diff] [blame] | 50 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Gabor Mezei | 6a426c9 | 2021-10-20 11:17:43 +0200 | [diff] [blame] | 51 | |
gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 52 | /** Turn a value into a mask: |
| 53 | * - if \p value == 0, return the all-bits 0 mask, aka 0 |
| 54 | * - otherwise, return the all-bits 1 mask, aka (size_t) -1 |
| 55 | * |
| 56 | * This function can be used to write constant-time code by replacing branches |
| 57 | * with bit operations using masks. |
| 58 | * |
gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 59 | * \param value The value to analyze. |
| 60 | * |
| 61 | * \return Zero if \p value is zero, otherwise all-bits-one. |
| 62 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 63 | size_t mbedtls_ct_size_mask(size_t value); |
gabor-mezei-arm | c76227d | 2021-09-27 11:53:54 +0200 | [diff] [blame] | 64 | |
Andrzej Kurek | 084334c | 2022-09-27 14:19:50 -0400 | [diff] [blame] | 65 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Gabor Mezei | 6a426c9 | 2021-10-20 11:17:43 +0200 | [diff] [blame] | 66 | |
gabor-mezei-arm | 9cb5569 | 2021-08-11 15:07:02 +0200 | [diff] [blame] | 67 | #if defined(MBEDTLS_BIGNUM_C) |
| 68 | |
gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 69 | /** Turn a value into a mask: |
| 70 | * - if \p value == 0, return the all-bits 0 mask, aka 0 |
Gabor Mezei | a316fc8 | 2021-10-18 16:28:27 +0200 | [diff] [blame] | 71 | * - otherwise, return the all-bits 1 mask, aka (mbedtls_mpi_uint) -1 |
gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 72 | * |
| 73 | * This function can be used to write constant-time code by replacing branches |
| 74 | * with bit operations using masks. |
| 75 | * |
gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 76 | * \param value The value to analyze. |
| 77 | * |
| 78 | * \return Zero if \p value is zero, otherwise all-bits-one. |
| 79 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 80 | mbedtls_mpi_uint mbedtls_ct_mpi_uint_mask(mbedtls_mpi_uint value); |
gabor-mezei-arm | 9cb5569 | 2021-08-11 15:07:02 +0200 | [diff] [blame] | 81 | |
| 82 | #endif /* MBEDTLS_BIGNUM_C */ |
| 83 | |
Gabor Mezei | e212379 | 2021-10-18 17:05:06 +0200 | [diff] [blame] | 84 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC) |
| 85 | |
gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 86 | /** Constant-flow mask generation for "greater or equal" comparison: |
| 87 | * - if \p x >= \p y, return all-bits 1, that is (size_t) -1 |
| 88 | * - otherwise, return all bits 0, that is 0 |
| 89 | * |
| 90 | * This function can be used to write constant-time code by replacing branches |
| 91 | * with bit operations using masks. |
| 92 | * |
gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 93 | * \param x The first value to analyze. |
| 94 | * \param y The second value to analyze. |
| 95 | * |
| 96 | * \return All-bits-one if \p x is greater or equal than \p y, |
| 97 | * otherwise zero. |
| 98 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 99 | size_t mbedtls_ct_size_mask_ge(size_t x, |
| 100 | size_t y); |
gabor-mezei-arm | 8d1d5fd | 2021-09-27 12:15:19 +0200 | [diff] [blame] | 101 | |
Gabor Mezei | e212379 | 2021-10-18 17:05:06 +0200 | [diff] [blame] | 102 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */ |
| 103 | |
gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 104 | /** Constant-flow boolean "equal" comparison: |
| 105 | * return x == y |
| 106 | * |
| 107 | * This is equivalent to \p x == \p y, but is likely to be compiled |
| 108 | * to code using bitwise operation rather than a branch. |
| 109 | * |
gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 110 | * \param x The first value to analyze. |
| 111 | * \param y The second value to analyze. |
| 112 | * |
| 113 | * \return 1 if \p x equals to \p y, otherwise 0. |
| 114 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 115 | unsigned mbedtls_ct_size_bool_eq(size_t x, |
| 116 | size_t y); |
gabor-mezei-arm | 5a85442 | 2021-09-27 12:25:07 +0200 | [diff] [blame] | 117 | |
gabor-mezei-arm | 3f90fd5 | 2021-09-27 12:55:33 +0200 | [diff] [blame] | 118 | #if defined(MBEDTLS_BIGNUM_C) |
| 119 | |
gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 120 | /** Decide if an integer is less than the other, without branches. |
| 121 | * |
| 122 | * This is equivalent to \p x < \p y, but is likely to be compiled |
| 123 | * to code using bitwise operation rather than a branch. |
| 124 | * |
gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 125 | * \param x The first value to analyze. |
| 126 | * \param y The second value to analyze. |
| 127 | * |
| 128 | * \return 1 if \p x is less than \p y, otherwise 0. |
| 129 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 130 | unsigned mbedtls_ct_mpi_uint_lt(const mbedtls_mpi_uint x, |
| 131 | const mbedtls_mpi_uint y); |
gabor-mezei-arm | 3f90fd5 | 2021-09-27 12:55:33 +0200 | [diff] [blame] | 132 | |
| 133 | #endif /* MBEDTLS_BIGNUM_C */ |
gabor-mezei-arm | b2dbf2c | 2021-09-27 12:59:30 +0200 | [diff] [blame] | 134 | |
gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 135 | /** Choose between two integer values without branches. |
| 136 | * |
| 137 | * This is equivalent to `condition ? if1 : if0`, but is likely to be compiled |
| 138 | * to code using bitwise operation rather than a branch. |
| 139 | * |
gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 140 | * \param condition Condition to test. |
| 141 | * \param if1 Value to use if \p condition is nonzero. |
| 142 | * \param if0 Value to use if \p condition is zero. |
| 143 | * |
| 144 | * \return \c if1 if \p condition is nonzero, otherwise \c if0. |
| 145 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 146 | unsigned mbedtls_ct_uint_if(unsigned condition, |
| 147 | unsigned if1, |
| 148 | unsigned if0); |
gabor-mezei-arm | b2dbf2c | 2021-09-27 12:59:30 +0200 | [diff] [blame] | 149 | |
gabor-mezei-arm | be8d98b | 2021-09-27 13:17:15 +0200 | [diff] [blame] | 150 | #if defined(MBEDTLS_BIGNUM_C) |
| 151 | |
gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 152 | /** Conditionally assign a value without branches. |
| 153 | * |
| 154 | * This is equivalent to `if ( condition ) dest = src`, but is likely |
| 155 | * to be compiled to code using bitwise operation rather than a branch. |
| 156 | * |
gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 157 | * \param n \p dest and \p src must be arrays of limbs of size n. |
| 158 | * \param dest The MPI to conditionally assign to. This must point |
| 159 | * to an initialized MPI. |
| 160 | * \param src The MPI to be assigned from. This must point to an |
| 161 | * initialized MPI. |
| 162 | * \param condition Condition to test, must be 0 or 1. |
| 163 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 164 | void mbedtls_ct_mpi_uint_cond_assign(size_t n, |
| 165 | mbedtls_mpi_uint *dest, |
| 166 | const mbedtls_mpi_uint *src, |
| 167 | unsigned char condition); |
gabor-mezei-arm | be8d98b | 2021-09-27 13:17:15 +0200 | [diff] [blame] | 168 | |
| 169 | #endif /* MBEDTLS_BIGNUM_C */ |
gabor-mezei-arm | 394aeaa | 2021-09-27 13:31:06 +0200 | [diff] [blame] | 170 | |
Przemek Stekiel | 89ad623 | 2022-09-27 13:36:12 +0200 | [diff] [blame] | 171 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 172 | |
gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 173 | /** Conditional memcpy without branches. |
| 174 | * |
Gabor Mezei | 642eeb2 | 2021-11-03 16:13:32 +0100 | [diff] [blame] | 175 | * This is equivalent to `if ( c1 == c2 ) memcpy(dest, src, len)`, but is likely |
gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 176 | * to be compiled to code using bitwise operation rather than a branch. |
| 177 | * |
gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 178 | * \param dest The pointer to conditionally copy to. |
Gabor Mezei | a316fc8 | 2021-10-18 16:28:27 +0200 | [diff] [blame] | 179 | * \param src The pointer to copy from. Shouldn't overlap with \p dest. |
gabor-mezei-arm | 90d96cc | 2021-08-11 16:40:35 +0200 | [diff] [blame] | 180 | * \param len The number of bytes to copy. |
| 181 | * \param c1 The first value to analyze in the condition. |
| 182 | * \param c2 The second value to analyze in the condition. |
| 183 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 184 | void mbedtls_ct_memcpy_if_eq(unsigned char *dest, |
| 185 | const unsigned char *src, |
| 186 | size_t len, |
| 187 | size_t c1, size_t c2); |
gabor-mezei-arm | 0e7f71e | 2021-09-27 13:57:45 +0200 | [diff] [blame] | 188 | |
| 189 | /** Copy data from a secret position with constant flow. |
| 190 | * |
| 191 | * This function copies \p len bytes from \p src_base + \p offset_secret to \p |
| 192 | * dst, with a code flow and memory access pattern that does not depend on \p |
| 193 | * offset_secret, but only on \p offset_min, \p offset_max and \p len. |
Gabor Mezei | a316fc8 | 2021-10-18 16:28:27 +0200 | [diff] [blame] | 194 | * Functionally equivalent to `memcpy(dst, src + offset_secret, len)`. |
gabor-mezei-arm | 0e7f71e | 2021-09-27 13:57:45 +0200 | [diff] [blame] | 195 | * |
Paul Elliott | 5260ce2 | 2022-05-09 18:15:54 +0100 | [diff] [blame] | 196 | * \note This function reads from \p dest, but the value that |
| 197 | * is read does not influence the result and this |
| 198 | * function's behavior is well-defined regardless of the |
| 199 | * contents of the buffers. This may result in false |
| 200 | * positives from static or dynamic analyzers, especially |
| 201 | * if \p dest is not initialized. |
| 202 | * |
Gabor Mezei | 63bbba5 | 2021-10-18 16:17:57 +0200 | [diff] [blame] | 203 | * \param dest The destination buffer. This must point to a writable |
gabor-mezei-arm | 0e7f71e | 2021-09-27 13:57:45 +0200 | [diff] [blame] | 204 | * buffer of at least \p len bytes. |
Gabor Mezei | 63bbba5 | 2021-10-18 16:17:57 +0200 | [diff] [blame] | 205 | * \param src The base of the source buffer. This must point to a |
gabor-mezei-arm | 0e7f71e | 2021-09-27 13:57:45 +0200 | [diff] [blame] | 206 | * readable buffer of at least \p offset_max + \p len |
Gabor Mezei | 63bbba5 | 2021-10-18 16:17:57 +0200 | [diff] [blame] | 207 | * bytes. Shouldn't overlap with \p dest. |
| 208 | * \param offset The offset in the source buffer from which to copy. |
gabor-mezei-arm | 0e7f71e | 2021-09-27 13:57:45 +0200 | [diff] [blame] | 209 | * This must be no less than \p offset_min and no greater |
| 210 | * than \p offset_max. |
Gabor Mezei | 63bbba5 | 2021-10-18 16:17:57 +0200 | [diff] [blame] | 211 | * \param offset_min The minimal value of \p offset. |
| 212 | * \param offset_max The maximal value of \p offset. |
gabor-mezei-arm | 0e7f71e | 2021-09-27 13:57:45 +0200 | [diff] [blame] | 213 | * \param len The number of bytes to copy. |
| 214 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 215 | void mbedtls_ct_memcpy_offset(unsigned char *dest, |
| 216 | const unsigned char *src, |
| 217 | size_t offset, |
| 218 | size_t offset_min, |
| 219 | size_t offset_max, |
| 220 | size_t len); |
gabor-mezei-arm | 1349ffd | 2021-09-27 14:28:31 +0200 | [diff] [blame] | 221 | |
Przemek Stekiel | 89ad623 | 2022-09-27 13:36:12 +0200 | [diff] [blame] | 222 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
gabor-mezei-arm | fdb7118 | 2021-09-27 16:11:12 +0200 | [diff] [blame] | 223 | |
Dave Rodgman | 0afe001 | 2023-05-09 11:09:52 +0100 | [diff] [blame] | 224 | #if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT) |
| 225 | |
| 226 | /** Constant-flow "greater than" comparison: |
| 227 | * return x > y |
| 228 | * |
| 229 | * This is equivalent to \p x > \p y, but is likely to be compiled |
| 230 | * to code using bitwise operation rather than a branch. |
| 231 | * |
| 232 | * \param x The first value to analyze. |
| 233 | * \param y The second value to analyze. |
| 234 | * |
| 235 | * \return 1 if \p x greater than \p y, otherwise 0. |
| 236 | */ |
| 237 | unsigned mbedtls_ct_size_gt(size_t x, size_t y); |
| 238 | |
| 239 | /** Shift some data towards the left inside a buffer. |
| 240 | * |
| 241 | * `mbedtls_ct_mem_move_to_left(start, total, offset)` is functionally |
| 242 | * equivalent to |
| 243 | * ``` |
| 244 | * memmove(start, start + offset, total - offset); |
| 245 | * memset(start + offset, 0, total - offset); |
| 246 | * ``` |
| 247 | * but it strives to use a memory access pattern (and thus total timing) |
| 248 | * that does not depend on \p offset. This timing independence comes at |
| 249 | * the expense of performance. |
| 250 | * |
| 251 | * \param start Pointer to the start of the buffer. |
| 252 | * \param total Total size of the buffer. |
| 253 | * \param offset Offset from which to copy \p total - \p offset bytes. |
| 254 | */ |
| 255 | void mbedtls_ct_mem_move_to_left(void *start, |
Dave Rodgman | 19e8cd0 | 2023-05-09 11:10:21 +0100 | [diff] [blame] | 256 | size_t total, |
| 257 | size_t offset); |
Dave Rodgman | 0afe001 | 2023-05-09 11:09:52 +0100 | [diff] [blame] | 258 | |
| 259 | #endif /* defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT) */ |
| 260 | |
Dave Rodgman | 40a41d0 | 2023-05-17 11:59:56 +0100 | [diff] [blame^] | 261 | |
| 262 | |
| 263 | /* The constant-time interface provides various operations that are likely |
| 264 | * to result in constant-time code that does not branch or use conditional |
| 265 | * instructions for secret data (for secret pointers, this also applies to |
| 266 | * the data pointed to). |
| 267 | * |
| 268 | * It has three main parts: |
| 269 | * |
| 270 | * - boolean operations (and a few non-boolean operations) |
| 271 | * These are all named mbedtls_ct_bool_<operation>, and operate over |
| 272 | * mbedtls_ct_condition_t. |
| 273 | * All arguments to these operations are considered secret. |
| 274 | * example: bool x = y | z => x = mbedtls_ct_bool_or(y, z) |
| 275 | * |
| 276 | * - conditional data selection |
| 277 | * These are all named mbedtls_ct_<type>_if and mbedtls_ct_<type>_if0 |
| 278 | * All arguments are considered secret. |
| 279 | * example: size_t a = x ? b : c => a = mbedtls_ct_size_if(x, b, c) |
| 280 | * example: unsigned a = x ? b : 0 => a = mbedtls_ct_uint_if0(x, b) |
| 281 | * |
| 282 | * - block memory operations |
| 283 | * Only some arguments are considered secret, as documented for each |
| 284 | * function. |
| 285 | * example: if (x) memcpy(...) => mbedtls_ct_memcpy_if(x, ...) |
| 286 | * |
| 287 | * mbedtls_ct_condition_t should be treated as opaque and only manipulated |
| 288 | * via the functions in this header. |
| 289 | * |
| 290 | * mbedtls_ct_uint_t is an unsigned integer type over which constant time |
| 291 | * operations may be performed via the functions in this header. It is as big |
| 292 | * as the larger of size_t and mbedtls_mpi_uint, i.e. it is safe to cast |
| 293 | * to/from "unsigned int", "size_t", and "mbedtls_mpi_uint" (and any other |
| 294 | * not-larger integer types). |
| 295 | * |
| 296 | * For Arm (32-bit, 64-bit and Thumb), assembly implementations are used |
| 297 | * to ensure that the generated code is constant time. For other architectures, |
| 298 | * a plain C fallback designed to yield constant-time code (this has been |
| 299 | * observed to be constant-time on latest gcc, clang and MSVC as of May 2023). |
| 300 | */ |
| 301 | |
| 302 | #if (SIZE_MAX > 0xffffffffffffffffULL) |
| 303 | /* Pointer size > 64-bit */ |
| 304 | typedef size_t mbedtls_ct_condition_t; |
| 305 | typedef size_t mbedtls_ct_uint_t; |
| 306 | typedef ptrdiff_t mbedtls_ct_int_t; |
| 307 | #define MBEDTLS_CT_TRUE ((mbedtls_ct_condition_t) SIZE_MAX) |
| 308 | #elif (SIZE_MAX > 0xffffffff) || defined(MBEDTLS_HAVE_INT64) |
| 309 | /* 32-bit < pointer size < 64-bit, or 64-bit MPI */ |
| 310 | typedef uint64_t mbedtls_ct_condition_t; |
| 311 | typedef uint64_t mbedtls_ct_uint_t; |
| 312 | typedef int64_t mbedtls_ct_int_t; |
| 313 | #define MBEDTLS_CT_TRUE ((mbedtls_ct_condition_t) UINT64_MAX) |
| 314 | #else |
| 315 | /* Pointer size < 32-bit, and no 64-bit MPIs */ |
| 316 | typedef uint32_t mbedtls_ct_condition_t; |
| 317 | typedef uint32_t mbedtls_ct_uint_t; |
| 318 | typedef int32_t mbedtls_ct_int_t; |
| 319 | #define MBEDTLS_CT_TRUE ((mbedtls_ct_condition_t) UINT32_MAX) |
| 320 | #endif |
| 321 | #define MBEDTLS_CT_FALSE ((mbedtls_ct_condition_t) 0) |
| 322 | |
| 323 | /* constant_time_impl.h contains all the static inline implementations, |
| 324 | * so that constant_time_internal.h is more readable. |
| 325 | */ |
| 326 | #include "constant_time_impl.h" |
| 327 | |
| 328 | |
| 329 | /* ============================================================================ |
| 330 | * Boolean operations |
| 331 | */ |
| 332 | |
| 333 | /** Convert a number into a mbedtls_ct_condition_t. |
| 334 | * |
| 335 | * \param x Number to convert. |
| 336 | * |
| 337 | * \return MBEDTLS_CT_TRUE if \p x != 0, or MBEDTLS_CT_FALSE if \p x == 0 |
| 338 | * |
| 339 | */ |
| 340 | static inline mbedtls_ct_condition_t mbedtls_ct_bool(mbedtls_ct_uint_t x); |
| 341 | |
| 342 | /** Boolean "not equal" operation. |
| 343 | * |
| 344 | * Functionally equivalent to: |
| 345 | * |
| 346 | * \p x != \p y |
| 347 | * |
| 348 | * \param x The first value to analyze. |
| 349 | * \param y The second value to analyze. |
| 350 | * |
| 351 | * \return MBEDTLS_CT_TRUE if \p x != \p y, otherwise MBEDTLS_CT_FALSE. |
| 352 | */ |
| 353 | static inline mbedtls_ct_condition_t mbedtls_ct_bool_ne(mbedtls_ct_uint_t x, mbedtls_ct_uint_t y); |
| 354 | |
| 355 | /** Boolean "equals" operation. |
| 356 | * |
| 357 | * Functionally equivalent to: |
| 358 | * |
| 359 | * \p x == \p y |
| 360 | * |
| 361 | * \param x The first value to analyze. |
| 362 | * \param y The second value to analyze. |
| 363 | * |
| 364 | * \return MBEDTLS_CT_TRUE if \p x == \p y, otherwise MBEDTLS_CT_FALSE. |
| 365 | */ |
| 366 | static inline mbedtls_ct_condition_t mbedtls_ct_bool_eq(mbedtls_ct_uint_t x, |
| 367 | mbedtls_ct_uint_t y); |
| 368 | |
| 369 | /** Boolean "less than" operation. |
| 370 | * |
| 371 | * Functionally equivalent to: |
| 372 | * |
| 373 | * \p x < \p y |
| 374 | * |
| 375 | * \param x The first value to analyze. |
| 376 | * \param y The second value to analyze. |
| 377 | * |
| 378 | * \return MBEDTLS_CT_TRUE if \p x < \p y, otherwise MBEDTLS_CT_FALSE. |
| 379 | */ |
| 380 | static inline mbedtls_ct_condition_t mbedtls_ct_bool_lt(mbedtls_ct_uint_t x, mbedtls_ct_uint_t y); |
| 381 | |
| 382 | /** Boolean "greater than" operation. |
| 383 | * |
| 384 | * Functionally equivalent to: |
| 385 | * |
| 386 | * \p x > \p y |
| 387 | * |
| 388 | * \param x The first value to analyze. |
| 389 | * \param y The second value to analyze. |
| 390 | * |
| 391 | * \return MBEDTLS_CT_TRUE if \p x > \p y, otherwise MBEDTLS_CT_FALSE. |
| 392 | */ |
| 393 | static inline mbedtls_ct_condition_t mbedtls_ct_bool_gt(mbedtls_ct_uint_t x, |
| 394 | mbedtls_ct_uint_t y); |
| 395 | |
| 396 | /** Boolean "greater or equal" operation. |
| 397 | * |
| 398 | * Functionally equivalent to: |
| 399 | * |
| 400 | * \p x >= \p y |
| 401 | * |
| 402 | * \param x The first value to analyze. |
| 403 | * \param y The second value to analyze. |
| 404 | * |
| 405 | * \return MBEDTLS_CT_TRUE if \p x >= \p y, |
| 406 | * otherwise MBEDTLS_CT_FALSE. |
| 407 | */ |
| 408 | static inline mbedtls_ct_condition_t mbedtls_ct_bool_ge(mbedtls_ct_uint_t x, |
| 409 | mbedtls_ct_uint_t y); |
| 410 | |
| 411 | /** Boolean "less than or equal" operation. |
| 412 | * |
| 413 | * Functionally equivalent to: |
| 414 | * |
| 415 | * \p x <= \p y |
| 416 | * |
| 417 | * \param x The first value to analyze. |
| 418 | * \param y The second value to analyze. |
| 419 | * |
| 420 | * \return MBEDTLS_CT_TRUE if \p x <= \p y, |
| 421 | * otherwise MBEDTLS_CT_FALSE. |
| 422 | */ |
| 423 | static inline mbedtls_ct_condition_t mbedtls_ct_bool_le(mbedtls_ct_uint_t x, |
| 424 | mbedtls_ct_uint_t y); |
| 425 | |
| 426 | /** Boolean "xor" operation. |
| 427 | * |
| 428 | * Functionally equivalent to: |
| 429 | * |
| 430 | * \p x ^ \p y |
| 431 | * |
| 432 | * \param x The first value to analyze. |
| 433 | * \param y The second value to analyze. |
| 434 | * |
| 435 | * \note This is more efficient than mbedtls_ct_bool_ne if both arguments are |
| 436 | * mbedtls_ct_condition_t. |
| 437 | * |
| 438 | * \return MBEDTLS_CT_TRUE if \p x ^ \p y, |
| 439 | * otherwise MBEDTLS_CT_FALSE. |
| 440 | */ |
| 441 | static inline mbedtls_ct_condition_t mbedtls_ct_bool_xor(mbedtls_ct_condition_t x, |
| 442 | mbedtls_ct_condition_t y); |
| 443 | |
| 444 | /** Boolean "and" operation. |
| 445 | * |
| 446 | * Functionally equivalent to: |
| 447 | * |
| 448 | * \p x && \p y |
| 449 | * |
| 450 | * \param x The first value to analyze. |
| 451 | * \param y The second value to analyze. |
| 452 | * |
| 453 | * \return MBEDTLS_CT_TRUE if \p x && \p y, |
| 454 | * otherwise MBEDTLS_CT_FALSE. |
| 455 | */ |
| 456 | static inline mbedtls_ct_condition_t mbedtls_ct_bool_and(mbedtls_ct_condition_t x, |
| 457 | mbedtls_ct_condition_t y); |
| 458 | |
| 459 | /** Boolean "or" operation. |
| 460 | * |
| 461 | * Functionally equivalent to: |
| 462 | * |
| 463 | * \p x || \p y |
| 464 | * |
| 465 | * \param x The first value to analyze. |
| 466 | * \param y The second value to analyze. |
| 467 | * |
| 468 | * \return MBEDTLS_CT_TRUE if \p x || \p y, |
| 469 | * otherwise MBEDTLS_CT_FALSE. |
| 470 | */ |
| 471 | static inline mbedtls_ct_condition_t mbedtls_ct_bool_or(mbedtls_ct_condition_t x, |
| 472 | mbedtls_ct_condition_t y); |
| 473 | |
| 474 | /** Boolean "not" operation. |
| 475 | * |
| 476 | * Functionally equivalent to: |
| 477 | * |
| 478 | * ! \p x |
| 479 | * |
| 480 | * \param x The value to invert |
| 481 | * |
| 482 | * \return MBEDTLS_CT_FALSE if \p x, otherwise MBEDTLS_CT_TRUE. |
| 483 | */ |
| 484 | static inline mbedtls_ct_condition_t mbedtls_ct_bool_not(mbedtls_ct_condition_t x); |
| 485 | |
| 486 | |
| 487 | /* ============================================================================ |
| 488 | * Data selection operations |
| 489 | */ |
| 490 | |
| 491 | /** Choose between two size_t values. |
| 492 | * |
| 493 | * Functionally equivalent to: |
| 494 | * |
| 495 | * condition ? if1 : if0. |
| 496 | * |
| 497 | * \param condition Condition to test. |
| 498 | * \param if1 Value to use if \p condition == MBEDTLS_CT_TRUE. |
| 499 | * \param if0 Value to use if \p condition == MBEDTLS_CT_FALSE. |
| 500 | * |
| 501 | * \return \c if1 if \p condition == MBEDTLS_CT_TRUE, otherwise \c if0. |
| 502 | */ |
| 503 | static inline size_t mbedtls_ct_size_if(mbedtls_ct_condition_t condition, |
| 504 | size_t if1, |
| 505 | size_t if0); |
| 506 | |
| 507 | /** Choose between two unsigned values. |
| 508 | * |
| 509 | * Functionally equivalent to: |
| 510 | * |
| 511 | * condition ? if1 : if0. |
| 512 | * |
| 513 | * \param condition Condition to test. |
| 514 | * \param if1 Value to use if \p condition == MBEDTLS_CT_TRUE. |
| 515 | * \param if0 Value to use if \p condition == MBEDTLS_CT_FALSE. |
| 516 | * |
| 517 | * \return \c if1 if \p condition == MBEDTLS_CT_TRUE, otherwise \c if0. |
| 518 | */ |
| 519 | static inline unsigned mbedtls_ct_uint_if_new(mbedtls_ct_condition_t condition, |
| 520 | unsigned if1, |
| 521 | unsigned if0); |
| 522 | |
| 523 | #if defined(MBEDTLS_BIGNUM_C) |
| 524 | |
| 525 | /** Choose between two mbedtls_mpi_uint values. |
| 526 | * |
| 527 | * Functionally equivalent to: |
| 528 | * |
| 529 | * condition ? if1 : if0. |
| 530 | * |
| 531 | * \param condition Condition to test. |
| 532 | * \param if1 Value to use if \p condition == MBEDTLS_CT_TRUE. |
| 533 | * \param if0 Value to use if \p condition == MBEDTLS_CT_FALSE. |
| 534 | * |
| 535 | * \return \c if1 if \p condition == MBEDTLS_CT_TRUE, otherwise \c if0. |
| 536 | */ |
| 537 | static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if(mbedtls_ct_condition_t condition, \ |
| 538 | mbedtls_mpi_uint if1, \ |
| 539 | mbedtls_mpi_uint if0); |
| 540 | |
| 541 | #endif |
| 542 | |
| 543 | /** Choose between an unsigned value and 0. |
| 544 | * |
| 545 | * Functionally equivalent to: |
| 546 | * |
| 547 | * condition ? if1 : 0. |
| 548 | * |
| 549 | * Functionally equivalent tombedtls_ct_uint_if(condition, if1, 0) but |
| 550 | * results in smaller code size. |
| 551 | * |
| 552 | * \param condition Condition to test. |
| 553 | * \param if1 Value to use if \p condition == MBEDTLS_CT_TRUE. |
| 554 | * |
| 555 | * \return \c if1 if \p condition == MBEDTLS_CT_TRUE, otherwise 0. |
| 556 | */ |
| 557 | static inline unsigned mbedtls_ct_uint_if0(mbedtls_ct_condition_t condition, unsigned if1); |
| 558 | |
| 559 | #if defined(MBEDTLS_BIGNUM_C) |
| 560 | |
| 561 | /** Choose between an mbedtls_mpi_uint value and 0. |
| 562 | * |
| 563 | * Functionally equivalent to: |
| 564 | * |
| 565 | * condition ? if1 : 0. |
| 566 | * |
| 567 | * Functionally equivalent tombedtls_ct_mpi_uint_if(condition, if1, 0) but |
| 568 | * results in smaller code size. |
| 569 | * |
| 570 | * \param condition Condition to test. |
| 571 | * \param if1 Value to use if \p condition == MBEDTLS_CT_TRUE. |
| 572 | * |
| 573 | * \return \c if1 if \p condition == MBEDTLS_CT_TRUE, otherwise 0. |
| 574 | */ |
| 575 | static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if0(mbedtls_ct_condition_t condition, |
| 576 | mbedtls_mpi_uint if1); |
| 577 | |
| 578 | #endif |
| 579 | |
| 580 | /** Constant-flow char selection |
| 581 | * |
| 582 | * \param low Secret. Bottom of range |
| 583 | * \param high Secret. Top of range |
| 584 | * \param c Secret. Value to compare to range |
| 585 | * \param t Secret. Value to return, if in range |
| 586 | * |
| 587 | * \return \p t if \p low <= \p c <= \p high, 0 otherwise. |
| 588 | */ |
| 589 | static inline unsigned char mbedtls_ct_uchar_in_range_if(unsigned char low, |
| 590 | unsigned char high, |
| 591 | unsigned char c, |
| 592 | unsigned char t); |
| 593 | |
| 594 | |
| 595 | /* ============================================================================ |
| 596 | * Block memory operations |
| 597 | */ |
| 598 | |
| 599 | #if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT) |
| 600 | |
| 601 | /** Conditionally set a block of memory to zero. |
| 602 | * |
| 603 | * Regardless of the condition, every byte will be read once and written to |
| 604 | * once. |
| 605 | * |
| 606 | * \param condition Secret. Condition to test. |
| 607 | * \param buf Secret. Pointer to the start of the buffer. |
| 608 | * \param len Number of bytes to set to zero. |
| 609 | * |
| 610 | * \warning Unlike mbedtls_platform_zeroize, this does not have the same guarantees |
| 611 | * about not being optimised away if the memory is never read again. |
| 612 | */ |
| 613 | void mbedtls_ct_zeroize_if(mbedtls_ct_condition_t condition, void *buf, size_t len); |
| 614 | |
| 615 | /** Shift some data towards the left inside a buffer. |
| 616 | * |
| 617 | * Functionally equivalent to: |
| 618 | * |
| 619 | * memmove(start, start + offset, total - offset); |
| 620 | * memset(start + (total - offset), 0, offset); |
| 621 | * |
| 622 | * Timing independence comes at the expense of performance. |
| 623 | * |
| 624 | * \param start Secret. Pointer to the start of the buffer. |
| 625 | * \param total Total size of the buffer. |
| 626 | * \param offset Secret. Offset from which to copy \p total - \p offset bytes. |
| 627 | */ |
| 628 | void mbedtls_ct_memmove_left(void *start, |
| 629 | size_t total, |
| 630 | size_t offset); |
| 631 | |
| 632 | #endif /* defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT) */ |
| 633 | |
| 634 | /** Conditional memcpy. |
| 635 | * |
| 636 | * Functionally equivalent to: |
| 637 | * |
| 638 | * if (condition) { |
| 639 | * memcpy(dest, src1, len); |
| 640 | * } else { |
| 641 | * if (src2 != NULL) |
| 642 | * memcpy(dest, src2, len); |
| 643 | * } |
| 644 | * |
| 645 | * It will always read len bytes from src1. |
| 646 | * If src2 != NULL, it will always read len bytes from src2. |
| 647 | * If src2 == NULL, it will instead read len bytes from dest (as if src2 == dest). |
| 648 | * |
| 649 | * \param condition The condition |
| 650 | * \param dest Secret. Destination pointer. |
| 651 | * \param src1 Secret. Pointer to copy from (if \p condition == MBEDTLS_CT_TRUE). Shouldn't overlap with \p dest. |
| 652 | * \param src2 Secret (contents only - may branch to test if src2 == NULL). |
| 653 | * Pointer to copy from (if \p condition == MBEDTLS_CT_FALSE and \p src2 is not NULL). Shouldn't overlap with \p dest. May be NULL. |
| 654 | * \param len Number of bytes to copy. |
| 655 | */ |
| 656 | void mbedtls_ct_memcpy_if(mbedtls_ct_condition_t condition, |
| 657 | unsigned char *dest, |
| 658 | const unsigned char *src1, |
| 659 | const unsigned char *src2, |
| 660 | size_t len |
| 661 | ); |
| 662 | |
| 663 | /** Copy data from a secret position. |
| 664 | * |
| 665 | * Functionally equivalent to: |
| 666 | * |
| 667 | * memcpy(dst, src + offset, len) |
| 668 | * |
| 669 | * This function copies \p len bytes from \p src_base + \p offset to \p |
| 670 | * dst, with a code flow and memory access pattern that does not depend on |
| 671 | * \p offset, but only on \p offset_min, \p offset_max and \p len. |
| 672 | * |
| 673 | * \note This function reads from \p dest, but the value that |
| 674 | * is read does not influence the result and this |
| 675 | * function's behavior is well-defined regardless of the |
| 676 | * contents of the buffers. This may result in false |
| 677 | * positives from static or dynamic analyzers, especially |
| 678 | * if \p dest is not initialized. |
| 679 | * |
| 680 | * \param dest Secret. The destination buffer. This must point to a writable |
| 681 | * buffer of at least \p len bytes. |
| 682 | * \param src Secret. The base of the source buffer. This must point to a |
| 683 | * readable buffer of at least \p offset_max + \p len |
| 684 | * bytes. Shouldn't overlap with \p dest. |
| 685 | * \param offset Secret. The offset in the source buffer from which to copy. |
| 686 | * This must be no less than \p offset_min and no greater |
| 687 | * than \p offset_max. |
| 688 | * \param offset_min The minimal value of \p offset. |
| 689 | * \param offset_max The maximal value of \p offset. |
| 690 | * \param len The number of bytes to copy. |
| 691 | */ |
| 692 | void mbedtls_ct_memcpy_offset(unsigned char *dest, |
| 693 | const unsigned char *src, |
| 694 | size_t offset, |
| 695 | size_t offset_min, |
| 696 | size_t offset_max, |
| 697 | size_t len); |
| 698 | |
| 699 | /* Documented in include/mbedtls/constant_time.h. a and b are secret. */ |
| 700 | int mbedtls_ct_memcmp(const void *a, |
| 701 | const void *b, |
| 702 | size_t n); |
| 703 | |
Gabor Mezei | 291df7b | 2021-10-19 11:27:17 +0200 | [diff] [blame] | 704 | #endif /* MBEDTLS_CONSTANT_TIME_INTERNAL_H */ |