blob: 5e0d9c45f71727e1843558657feb0c24001d6fbb [file] [log] [blame]
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +02001/**
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 Mezei291df7b2021-10-19 11:27:17 +020020#ifndef MBEDTLS_CONSTANT_TIME_INTERNAL_H
21#define MBEDTLS_CONSTANT_TIME_INTERNAL_H
22
Dave Rodgman40a41d02023-05-17 11:59:56 +010023#include <stdint.h>
24#include <stddef.h>
25
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +020026#include "common.h"
27
gabor-mezei-arm3f90fd52021-09-27 12:55:33 +020028#if defined(MBEDTLS_BIGNUM_C)
29#include "mbedtls/bignum.h"
30#endif
31
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +020032#if defined(MBEDTLS_SSL_TLS_C)
33#include "ssl_misc.h"
34#endif
35
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +020036
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +020037/** Turn a value into a mask:
38 * - if \p value == 0, return the all-bits 0 mask, aka 0
Gabor Mezeia316fc82021-10-18 16:28:27 +020039 * - otherwise, return the all-bits 1 mask, aka (unsigned) -1
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +020040 *
41 * This function can be used to write constant-time code by replacing branches
42 * with bit operations using masks.
43 *
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +020044 * \param value The value to analyze.
45 *
46 * \return Zero if \p value is zero, otherwise all-bits-one.
47 */
Gilles Peskine449bd832023-01-11 14:50:10 +010048unsigned mbedtls_ct_uint_mask(unsigned value);
gabor-mezei-arm3733bf82021-09-27 11:49:42 +020049
Andrzej Kurek084334c2022-09-27 14:19:50 -040050#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gabor Mezei6a426c92021-10-20 11:17:43 +020051
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +020052/** 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-arm90d96cc2021-08-11 16:40:35 +020059 * \param value The value to analyze.
60 *
61 * \return Zero if \p value is zero, otherwise all-bits-one.
62 */
Gilles Peskine449bd832023-01-11 14:50:10 +010063size_t mbedtls_ct_size_mask(size_t value);
gabor-mezei-armc76227d2021-09-27 11:53:54 +020064
Andrzej Kurek084334c2022-09-27 14:19:50 -040065#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Gabor Mezei6a426c92021-10-20 11:17:43 +020066
gabor-mezei-arm9cb55692021-08-11 15:07:02 +020067#if defined(MBEDTLS_BIGNUM_C)
68
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +020069/** Turn a value into a mask:
70 * - if \p value == 0, return the all-bits 0 mask, aka 0
Gabor Mezeia316fc82021-10-18 16:28:27 +020071 * - otherwise, return the all-bits 1 mask, aka (mbedtls_mpi_uint) -1
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +020072 *
73 * This function can be used to write constant-time code by replacing branches
74 * with bit operations using masks.
75 *
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +020076 * \param value The value to analyze.
77 *
78 * \return Zero if \p value is zero, otherwise all-bits-one.
79 */
Gilles Peskine449bd832023-01-11 14:50:10 +010080mbedtls_mpi_uint mbedtls_ct_mpi_uint_mask(mbedtls_mpi_uint value);
gabor-mezei-arm9cb55692021-08-11 15:07:02 +020081
82#endif /* MBEDTLS_BIGNUM_C */
83
Gabor Mezeie2123792021-10-18 17:05:06 +020084#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
85
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +020086/** 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-arm90d96cc2021-08-11 16:40:35 +020093 * \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 Peskine449bd832023-01-11 14:50:10 +010099size_t mbedtls_ct_size_mask_ge(size_t x,
100 size_t y);
gabor-mezei-arm8d1d5fd2021-09-27 12:15:19 +0200101
Gabor Mezeie2123792021-10-18 17:05:06 +0200102#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
103
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200104/** 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-arm90d96cc2021-08-11 16:40:35 +0200110 * \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 Peskine449bd832023-01-11 14:50:10 +0100115unsigned mbedtls_ct_size_bool_eq(size_t x,
116 size_t y);
gabor-mezei-arm5a854422021-09-27 12:25:07 +0200117
gabor-mezei-arm3f90fd52021-09-27 12:55:33 +0200118#if defined(MBEDTLS_BIGNUM_C)
119
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200120/** 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-arm90d96cc2021-08-11 16:40:35 +0200125 * \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 Peskine449bd832023-01-11 14:50:10 +0100130unsigned mbedtls_ct_mpi_uint_lt(const mbedtls_mpi_uint x,
131 const mbedtls_mpi_uint y);
gabor-mezei-arm3f90fd52021-09-27 12:55:33 +0200132
133#endif /* MBEDTLS_BIGNUM_C */
gabor-mezei-armb2dbf2c2021-09-27 12:59:30 +0200134
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200135/** 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-arm90d96cc2021-08-11 16:40:35 +0200140 * \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 Peskine449bd832023-01-11 14:50:10 +0100146unsigned mbedtls_ct_uint_if(unsigned condition,
147 unsigned if1,
148 unsigned if0);
gabor-mezei-armb2dbf2c2021-09-27 12:59:30 +0200149
gabor-mezei-armbe8d98b2021-09-27 13:17:15 +0200150#if defined(MBEDTLS_BIGNUM_C)
151
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200152/** 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-arm90d96cc2021-08-11 16:40:35 +0200157 * \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 Peskine449bd832023-01-11 14:50:10 +0100164void 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-armbe8d98b2021-09-27 13:17:15 +0200168
169#endif /* MBEDTLS_BIGNUM_C */
gabor-mezei-arm394aeaa2021-09-27 13:31:06 +0200170
Przemek Stekiel89ad6232022-09-27 13:36:12 +0200171#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200172
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200173/** Conditional memcpy without branches.
174 *
Gabor Mezei642eeb22021-11-03 16:13:32 +0100175 * This is equivalent to `if ( c1 == c2 ) memcpy(dest, src, len)`, but is likely
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200176 * to be compiled to code using bitwise operation rather than a branch.
177 *
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200178 * \param dest The pointer to conditionally copy to.
Gabor Mezeia316fc82021-10-18 16:28:27 +0200179 * \param src The pointer to copy from. Shouldn't overlap with \p dest.
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200180 * \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 Peskine449bd832023-01-11 14:50:10 +0100184void 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-arm0e7f71e2021-09-27 13:57:45 +0200188
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 Mezeia316fc82021-10-18 16:28:27 +0200194 * Functionally equivalent to `memcpy(dst, src + offset_secret, len)`.
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200195 *
Paul Elliott5260ce22022-05-09 18:15:54 +0100196 * \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 Mezei63bbba52021-10-18 16:17:57 +0200203 * \param dest The destination buffer. This must point to a writable
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200204 * buffer of at least \p len bytes.
Gabor Mezei63bbba52021-10-18 16:17:57 +0200205 * \param src The base of the source buffer. This must point to a
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200206 * readable buffer of at least \p offset_max + \p len
Gabor Mezei63bbba52021-10-18 16:17:57 +0200207 * bytes. Shouldn't overlap with \p dest.
208 * \param offset The offset in the source buffer from which to copy.
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200209 * This must be no less than \p offset_min and no greater
210 * than \p offset_max.
Gabor Mezei63bbba52021-10-18 16:17:57 +0200211 * \param offset_min The minimal value of \p offset.
212 * \param offset_max The maximal value of \p offset.
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200213 * \param len The number of bytes to copy.
214 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100215void 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-arm1349ffd2021-09-27 14:28:31 +0200221
Przemek Stekiel89ad6232022-09-27 13:36:12 +0200222#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
gabor-mezei-armfdb71182021-09-27 16:11:12 +0200223
Dave Rodgman0afe0012023-05-09 11:09:52 +0100224#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 */
237unsigned mbedtls_ct_size_gt(size_t x, size_t y);
238
Dave Rodgman0afe0012023-05-09 11:09:52 +0100239#endif /* defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT) */
240
Dave Rodgman40a41d02023-05-17 11:59:56 +0100241
242
243/* The constant-time interface provides various operations that are likely
244 * to result in constant-time code that does not branch or use conditional
245 * instructions for secret data (for secret pointers, this also applies to
246 * the data pointed to).
247 *
248 * It has three main parts:
249 *
250 * - boolean operations (and a few non-boolean operations)
251 * These are all named mbedtls_ct_bool_<operation>, and operate over
252 * mbedtls_ct_condition_t.
253 * All arguments to these operations are considered secret.
254 * example: bool x = y | z => x = mbedtls_ct_bool_or(y, z)
255 *
256 * - conditional data selection
257 * These are all named mbedtls_ct_<type>_if and mbedtls_ct_<type>_if0
258 * All arguments are considered secret.
259 * example: size_t a = x ? b : c => a = mbedtls_ct_size_if(x, b, c)
260 * example: unsigned a = x ? b : 0 => a = mbedtls_ct_uint_if0(x, b)
261 *
262 * - block memory operations
263 * Only some arguments are considered secret, as documented for each
264 * function.
265 * example: if (x) memcpy(...) => mbedtls_ct_memcpy_if(x, ...)
266 *
267 * mbedtls_ct_condition_t should be treated as opaque and only manipulated
268 * via the functions in this header.
269 *
270 * mbedtls_ct_uint_t is an unsigned integer type over which constant time
271 * operations may be performed via the functions in this header. It is as big
272 * as the larger of size_t and mbedtls_mpi_uint, i.e. it is safe to cast
273 * to/from "unsigned int", "size_t", and "mbedtls_mpi_uint" (and any other
274 * not-larger integer types).
275 *
276 * For Arm (32-bit, 64-bit and Thumb), assembly implementations are used
277 * to ensure that the generated code is constant time. For other architectures,
278 * a plain C fallback designed to yield constant-time code (this has been
279 * observed to be constant-time on latest gcc, clang and MSVC as of May 2023).
280 */
281
282#if (SIZE_MAX > 0xffffffffffffffffULL)
283/* Pointer size > 64-bit */
284typedef size_t mbedtls_ct_condition_t;
285typedef size_t mbedtls_ct_uint_t;
286typedef ptrdiff_t mbedtls_ct_int_t;
287#define MBEDTLS_CT_TRUE ((mbedtls_ct_condition_t) SIZE_MAX)
288#elif (SIZE_MAX > 0xffffffff) || defined(MBEDTLS_HAVE_INT64)
289/* 32-bit < pointer size < 64-bit, or 64-bit MPI */
290typedef uint64_t mbedtls_ct_condition_t;
291typedef uint64_t mbedtls_ct_uint_t;
292typedef int64_t mbedtls_ct_int_t;
293#define MBEDTLS_CT_TRUE ((mbedtls_ct_condition_t) UINT64_MAX)
294#else
295/* Pointer size < 32-bit, and no 64-bit MPIs */
296typedef uint32_t mbedtls_ct_condition_t;
297typedef uint32_t mbedtls_ct_uint_t;
298typedef int32_t mbedtls_ct_int_t;
299#define MBEDTLS_CT_TRUE ((mbedtls_ct_condition_t) UINT32_MAX)
300#endif
301#define MBEDTLS_CT_FALSE ((mbedtls_ct_condition_t) 0)
302
303/* constant_time_impl.h contains all the static inline implementations,
304 * so that constant_time_internal.h is more readable.
305 */
306#include "constant_time_impl.h"
307
308
309/* ============================================================================
310 * Boolean operations
311 */
312
313/** Convert a number into a mbedtls_ct_condition_t.
314 *
315 * \param x Number to convert.
316 *
317 * \return MBEDTLS_CT_TRUE if \p x != 0, or MBEDTLS_CT_FALSE if \p x == 0
318 *
319 */
320static inline mbedtls_ct_condition_t mbedtls_ct_bool(mbedtls_ct_uint_t x);
321
322/** Boolean "not equal" operation.
323 *
324 * Functionally equivalent to:
325 *
326 * \p x != \p y
327 *
328 * \param x The first value to analyze.
329 * \param y The second value to analyze.
330 *
331 * \return MBEDTLS_CT_TRUE if \p x != \p y, otherwise MBEDTLS_CT_FALSE.
332 */
333static inline mbedtls_ct_condition_t mbedtls_ct_bool_ne(mbedtls_ct_uint_t x, mbedtls_ct_uint_t y);
334
335/** Boolean "equals" operation.
336 *
337 * Functionally equivalent to:
338 *
339 * \p x == \p y
340 *
341 * \param x The first value to analyze.
342 * \param y The second value to analyze.
343 *
344 * \return MBEDTLS_CT_TRUE if \p x == \p y, otherwise MBEDTLS_CT_FALSE.
345 */
346static inline mbedtls_ct_condition_t mbedtls_ct_bool_eq(mbedtls_ct_uint_t x,
347 mbedtls_ct_uint_t y);
348
349/** Boolean "less than" operation.
350 *
351 * Functionally equivalent to:
352 *
353 * \p x < \p y
354 *
355 * \param x The first value to analyze.
356 * \param y The second value to analyze.
357 *
358 * \return MBEDTLS_CT_TRUE if \p x < \p y, otherwise MBEDTLS_CT_FALSE.
359 */
360static inline mbedtls_ct_condition_t mbedtls_ct_bool_lt(mbedtls_ct_uint_t x, mbedtls_ct_uint_t y);
361
362/** Boolean "greater than" operation.
363 *
364 * Functionally equivalent to:
365 *
366 * \p x > \p y
367 *
368 * \param x The first value to analyze.
369 * \param y The second value to analyze.
370 *
371 * \return MBEDTLS_CT_TRUE if \p x > \p y, otherwise MBEDTLS_CT_FALSE.
372 */
373static inline mbedtls_ct_condition_t mbedtls_ct_bool_gt(mbedtls_ct_uint_t x,
374 mbedtls_ct_uint_t y);
375
376/** Boolean "greater or equal" operation.
377 *
378 * Functionally equivalent to:
379 *
380 * \p x >= \p y
381 *
382 * \param x The first value to analyze.
383 * \param y The second value to analyze.
384 *
385 * \return MBEDTLS_CT_TRUE if \p x >= \p y,
386 * otherwise MBEDTLS_CT_FALSE.
387 */
388static inline mbedtls_ct_condition_t mbedtls_ct_bool_ge(mbedtls_ct_uint_t x,
389 mbedtls_ct_uint_t y);
390
391/** Boolean "less than or equal" operation.
392 *
393 * Functionally equivalent to:
394 *
395 * \p x <= \p y
396 *
397 * \param x The first value to analyze.
398 * \param y The second value to analyze.
399 *
400 * \return MBEDTLS_CT_TRUE if \p x <= \p y,
401 * otherwise MBEDTLS_CT_FALSE.
402 */
403static inline mbedtls_ct_condition_t mbedtls_ct_bool_le(mbedtls_ct_uint_t x,
404 mbedtls_ct_uint_t y);
405
406/** Boolean "xor" operation.
407 *
408 * Functionally equivalent to:
409 *
410 * \p x ^ \p y
411 *
412 * \param x The first value to analyze.
413 * \param y The second value to analyze.
414 *
415 * \note This is more efficient than mbedtls_ct_bool_ne if both arguments are
416 * mbedtls_ct_condition_t.
417 *
418 * \return MBEDTLS_CT_TRUE if \p x ^ \p y,
419 * otherwise MBEDTLS_CT_FALSE.
420 */
421static inline mbedtls_ct_condition_t mbedtls_ct_bool_xor(mbedtls_ct_condition_t x,
422 mbedtls_ct_condition_t y);
423
424/** Boolean "and" operation.
425 *
426 * Functionally equivalent to:
427 *
428 * \p x && \p y
429 *
430 * \param x The first value to analyze.
431 * \param y The second value to analyze.
432 *
433 * \return MBEDTLS_CT_TRUE if \p x && \p y,
434 * otherwise MBEDTLS_CT_FALSE.
435 */
436static inline mbedtls_ct_condition_t mbedtls_ct_bool_and(mbedtls_ct_condition_t x,
437 mbedtls_ct_condition_t y);
438
439/** Boolean "or" operation.
440 *
441 * Functionally equivalent to:
442 *
443 * \p x || \p y
444 *
445 * \param x The first value to analyze.
446 * \param y The second value to analyze.
447 *
448 * \return MBEDTLS_CT_TRUE if \p x || \p y,
449 * otherwise MBEDTLS_CT_FALSE.
450 */
451static inline mbedtls_ct_condition_t mbedtls_ct_bool_or(mbedtls_ct_condition_t x,
452 mbedtls_ct_condition_t y);
453
454/** Boolean "not" operation.
455 *
456 * Functionally equivalent to:
457 *
458 * ! \p x
459 *
460 * \param x The value to invert
461 *
462 * \return MBEDTLS_CT_FALSE if \p x, otherwise MBEDTLS_CT_TRUE.
463 */
464static inline mbedtls_ct_condition_t mbedtls_ct_bool_not(mbedtls_ct_condition_t x);
465
466
467/* ============================================================================
468 * Data selection operations
469 */
470
471/** Choose between two size_t values.
472 *
473 * Functionally equivalent to:
474 *
475 * condition ? if1 : if0.
476 *
477 * \param condition Condition to test.
478 * \param if1 Value to use if \p condition == MBEDTLS_CT_TRUE.
479 * \param if0 Value to use if \p condition == MBEDTLS_CT_FALSE.
480 *
481 * \return \c if1 if \p condition == MBEDTLS_CT_TRUE, otherwise \c if0.
482 */
483static inline size_t mbedtls_ct_size_if(mbedtls_ct_condition_t condition,
484 size_t if1,
485 size_t if0);
486
487/** Choose between two unsigned values.
488 *
489 * Functionally equivalent to:
490 *
491 * condition ? if1 : if0.
492 *
493 * \param condition Condition to test.
494 * \param if1 Value to use if \p condition == MBEDTLS_CT_TRUE.
495 * \param if0 Value to use if \p condition == MBEDTLS_CT_FALSE.
496 *
497 * \return \c if1 if \p condition == MBEDTLS_CT_TRUE, otherwise \c if0.
498 */
499static inline unsigned mbedtls_ct_uint_if_new(mbedtls_ct_condition_t condition,
500 unsigned if1,
501 unsigned if0);
502
503#if defined(MBEDTLS_BIGNUM_C)
504
505/** Choose between two mbedtls_mpi_uint values.
506 *
507 * Functionally equivalent to:
508 *
509 * condition ? if1 : if0.
510 *
511 * \param condition Condition to test.
512 * \param if1 Value to use if \p condition == MBEDTLS_CT_TRUE.
513 * \param if0 Value to use if \p condition == MBEDTLS_CT_FALSE.
514 *
515 * \return \c if1 if \p condition == MBEDTLS_CT_TRUE, otherwise \c if0.
516 */
517static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if(mbedtls_ct_condition_t condition, \
518 mbedtls_mpi_uint if1, \
519 mbedtls_mpi_uint if0);
520
521#endif
522
523/** Choose between an unsigned value and 0.
524 *
525 * Functionally equivalent to:
526 *
527 * condition ? if1 : 0.
528 *
529 * Functionally equivalent tombedtls_ct_uint_if(condition, if1, 0) but
530 * results in smaller code size.
531 *
532 * \param condition Condition to test.
533 * \param if1 Value to use if \p condition == MBEDTLS_CT_TRUE.
534 *
535 * \return \c if1 if \p condition == MBEDTLS_CT_TRUE, otherwise 0.
536 */
537static inline unsigned mbedtls_ct_uint_if0(mbedtls_ct_condition_t condition, unsigned if1);
538
539#if defined(MBEDTLS_BIGNUM_C)
540
541/** Choose between an mbedtls_mpi_uint value and 0.
542 *
543 * Functionally equivalent to:
544 *
545 * condition ? if1 : 0.
546 *
547 * Functionally equivalent tombedtls_ct_mpi_uint_if(condition, if1, 0) but
548 * results in smaller code size.
549 *
550 * \param condition Condition to test.
551 * \param if1 Value to use if \p condition == MBEDTLS_CT_TRUE.
552 *
553 * \return \c if1 if \p condition == MBEDTLS_CT_TRUE, otherwise 0.
554 */
555static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if0(mbedtls_ct_condition_t condition,
556 mbedtls_mpi_uint if1);
557
558#endif
559
560/** Constant-flow char selection
561 *
562 * \param low Secret. Bottom of range
563 * \param high Secret. Top of range
564 * \param c Secret. Value to compare to range
565 * \param t Secret. Value to return, if in range
566 *
567 * \return \p t if \p low <= \p c <= \p high, 0 otherwise.
568 */
569static inline unsigned char mbedtls_ct_uchar_in_range_if(unsigned char low,
570 unsigned char high,
571 unsigned char c,
572 unsigned char t);
573
574
575/* ============================================================================
576 * Block memory operations
577 */
578
579#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
580
581/** Conditionally set a block of memory to zero.
582 *
583 * Regardless of the condition, every byte will be read once and written to
584 * once.
585 *
586 * \param condition Secret. Condition to test.
587 * \param buf Secret. Pointer to the start of the buffer.
588 * \param len Number of bytes to set to zero.
589 *
590 * \warning Unlike mbedtls_platform_zeroize, this does not have the same guarantees
591 * about not being optimised away if the memory is never read again.
592 */
593void mbedtls_ct_zeroize_if(mbedtls_ct_condition_t condition, void *buf, size_t len);
594
595/** Shift some data towards the left inside a buffer.
596 *
597 * Functionally equivalent to:
598 *
599 * memmove(start, start + offset, total - offset);
600 * memset(start + (total - offset), 0, offset);
601 *
602 * Timing independence comes at the expense of performance.
603 *
604 * \param start Secret. Pointer to the start of the buffer.
605 * \param total Total size of the buffer.
606 * \param offset Secret. Offset from which to copy \p total - \p offset bytes.
607 */
608void mbedtls_ct_memmove_left(void *start,
609 size_t total,
610 size_t offset);
611
612#endif /* defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT) */
613
614/** Conditional memcpy.
615 *
616 * Functionally equivalent to:
617 *
618 * if (condition) {
619 * memcpy(dest, src1, len);
620 * } else {
621 * if (src2 != NULL)
622 * memcpy(dest, src2, len);
623 * }
624 *
625 * It will always read len bytes from src1.
626 * If src2 != NULL, it will always read len bytes from src2.
627 * If src2 == NULL, it will instead read len bytes from dest (as if src2 == dest).
628 *
629 * \param condition The condition
630 * \param dest Secret. Destination pointer.
631 * \param src1 Secret. Pointer to copy from (if \p condition == MBEDTLS_CT_TRUE). Shouldn't overlap with \p dest.
632 * \param src2 Secret (contents only - may branch to test if src2 == NULL).
633 * 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.
634 * \param len Number of bytes to copy.
635 */
636void mbedtls_ct_memcpy_if(mbedtls_ct_condition_t condition,
637 unsigned char *dest,
638 const unsigned char *src1,
639 const unsigned char *src2,
640 size_t len
641 );
642
643/** Copy data from a secret position.
644 *
645 * Functionally equivalent to:
646 *
647 * memcpy(dst, src + offset, len)
648 *
649 * This function copies \p len bytes from \p src_base + \p offset to \p
650 * dst, with a code flow and memory access pattern that does not depend on
651 * \p offset, but only on \p offset_min, \p offset_max and \p len.
652 *
653 * \note This function reads from \p dest, but the value that
654 * is read does not influence the result and this
655 * function's behavior is well-defined regardless of the
656 * contents of the buffers. This may result in false
657 * positives from static or dynamic analyzers, especially
658 * if \p dest is not initialized.
659 *
660 * \param dest Secret. The destination buffer. This must point to a writable
661 * buffer of at least \p len bytes.
662 * \param src Secret. The base of the source buffer. This must point to a
663 * readable buffer of at least \p offset_max + \p len
664 * bytes. Shouldn't overlap with \p dest.
665 * \param offset Secret. The offset in the source buffer from which to copy.
666 * This must be no less than \p offset_min and no greater
667 * than \p offset_max.
668 * \param offset_min The minimal value of \p offset.
669 * \param offset_max The maximal value of \p offset.
670 * \param len The number of bytes to copy.
671 */
672void mbedtls_ct_memcpy_offset(unsigned char *dest,
673 const unsigned char *src,
674 size_t offset,
675 size_t offset_min,
676 size_t offset_max,
677 size_t len);
678
679/* Documented in include/mbedtls/constant_time.h. a and b are secret. */
680int mbedtls_ct_memcmp(const void *a,
681 const void *b,
682 size_t n);
683
Gabor Mezei291df7b2021-10-19 11:27:17 +0200684#endif /* MBEDTLS_CONSTANT_TIME_INTERNAL_H */