blob: f2cb4871b17100e50bb8b022abbfbba830da89a5 [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
Przemek Stekiel89ad6232022-09-27 13:36:12 +0200150#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200151
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200152/** Conditional memcpy without branches.
153 *
Gabor Mezei642eeb22021-11-03 16:13:32 +0100154 * This is equivalent to `if ( c1 == c2 ) memcpy(dest, src, len)`, but is likely
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200155 * to be compiled to code using bitwise operation rather than a branch.
156 *
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200157 * \param dest The pointer to conditionally copy to.
Gabor Mezeia316fc82021-10-18 16:28:27 +0200158 * \param src The pointer to copy from. Shouldn't overlap with \p dest.
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200159 * \param len The number of bytes to copy.
160 * \param c1 The first value to analyze in the condition.
161 * \param c2 The second value to analyze in the condition.
162 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100163void mbedtls_ct_memcpy_if_eq(unsigned char *dest,
164 const unsigned char *src,
165 size_t len,
166 size_t c1, size_t c2);
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200167
168/** Copy data from a secret position with constant flow.
169 *
170 * This function copies \p len bytes from \p src_base + \p offset_secret to \p
171 * dst, with a code flow and memory access pattern that does not depend on \p
172 * offset_secret, but only on \p offset_min, \p offset_max and \p len.
Gabor Mezeia316fc82021-10-18 16:28:27 +0200173 * Functionally equivalent to `memcpy(dst, src + offset_secret, len)`.
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200174 *
Paul Elliott5260ce22022-05-09 18:15:54 +0100175 * \note This function reads from \p dest, but the value that
176 * is read does not influence the result and this
177 * function's behavior is well-defined regardless of the
178 * contents of the buffers. This may result in false
179 * positives from static or dynamic analyzers, especially
180 * if \p dest is not initialized.
181 *
Gabor Mezei63bbba52021-10-18 16:17:57 +0200182 * \param dest The destination buffer. This must point to a writable
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200183 * buffer of at least \p len bytes.
Gabor Mezei63bbba52021-10-18 16:17:57 +0200184 * \param src The base of the source buffer. This must point to a
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200185 * readable buffer of at least \p offset_max + \p len
Gabor Mezei63bbba52021-10-18 16:17:57 +0200186 * bytes. Shouldn't overlap with \p dest.
187 * \param offset The offset in the source buffer from which to copy.
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200188 * This must be no less than \p offset_min and no greater
189 * than \p offset_max.
Gabor Mezei63bbba52021-10-18 16:17:57 +0200190 * \param offset_min The minimal value of \p offset.
191 * \param offset_max The maximal value of \p offset.
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200192 * \param len The number of bytes to copy.
193 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100194void mbedtls_ct_memcpy_offset(unsigned char *dest,
195 const unsigned char *src,
196 size_t offset,
197 size_t offset_min,
198 size_t offset_max,
199 size_t len);
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +0200200
Przemek Stekiel89ad6232022-09-27 13:36:12 +0200201#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
gabor-mezei-armfdb71182021-09-27 16:11:12 +0200202
Dave Rodgman0afe0012023-05-09 11:09:52 +0100203#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
204
205/** Constant-flow "greater than" comparison:
206 * return x > y
207 *
208 * This is equivalent to \p x > \p y, but is likely to be compiled
209 * to code using bitwise operation rather than a branch.
210 *
211 * \param x The first value to analyze.
212 * \param y The second value to analyze.
213 *
214 * \return 1 if \p x greater than \p y, otherwise 0.
215 */
216unsigned mbedtls_ct_size_gt(size_t x, size_t y);
217
Dave Rodgman0afe0012023-05-09 11:09:52 +0100218#endif /* defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT) */
219
Dave Rodgman40a41d02023-05-17 11:59:56 +0100220
221
222/* The constant-time interface provides various operations that are likely
223 * to result in constant-time code that does not branch or use conditional
224 * instructions for secret data (for secret pointers, this also applies to
225 * the data pointed to).
226 *
227 * It has three main parts:
228 *
229 * - boolean operations (and a few non-boolean operations)
230 * These are all named mbedtls_ct_bool_<operation>, and operate over
231 * mbedtls_ct_condition_t.
232 * All arguments to these operations are considered secret.
233 * example: bool x = y | z => x = mbedtls_ct_bool_or(y, z)
234 *
235 * - conditional data selection
236 * These are all named mbedtls_ct_<type>_if and mbedtls_ct_<type>_if0
237 * All arguments are considered secret.
238 * example: size_t a = x ? b : c => a = mbedtls_ct_size_if(x, b, c)
239 * example: unsigned a = x ? b : 0 => a = mbedtls_ct_uint_if0(x, b)
240 *
241 * - block memory operations
242 * Only some arguments are considered secret, as documented for each
243 * function.
244 * example: if (x) memcpy(...) => mbedtls_ct_memcpy_if(x, ...)
245 *
246 * mbedtls_ct_condition_t should be treated as opaque and only manipulated
247 * via the functions in this header.
248 *
249 * mbedtls_ct_uint_t is an unsigned integer type over which constant time
250 * operations may be performed via the functions in this header. It is as big
251 * as the larger of size_t and mbedtls_mpi_uint, i.e. it is safe to cast
252 * to/from "unsigned int", "size_t", and "mbedtls_mpi_uint" (and any other
253 * not-larger integer types).
254 *
255 * For Arm (32-bit, 64-bit and Thumb), assembly implementations are used
256 * to ensure that the generated code is constant time. For other architectures,
257 * a plain C fallback designed to yield constant-time code (this has been
258 * observed to be constant-time on latest gcc, clang and MSVC as of May 2023).
259 */
260
261#if (SIZE_MAX > 0xffffffffffffffffULL)
262/* Pointer size > 64-bit */
263typedef size_t mbedtls_ct_condition_t;
264typedef size_t mbedtls_ct_uint_t;
265typedef ptrdiff_t mbedtls_ct_int_t;
266#define MBEDTLS_CT_TRUE ((mbedtls_ct_condition_t) SIZE_MAX)
267#elif (SIZE_MAX > 0xffffffff) || defined(MBEDTLS_HAVE_INT64)
268/* 32-bit < pointer size < 64-bit, or 64-bit MPI */
269typedef uint64_t mbedtls_ct_condition_t;
270typedef uint64_t mbedtls_ct_uint_t;
271typedef int64_t mbedtls_ct_int_t;
272#define MBEDTLS_CT_TRUE ((mbedtls_ct_condition_t) UINT64_MAX)
273#else
274/* Pointer size < 32-bit, and no 64-bit MPIs */
275typedef uint32_t mbedtls_ct_condition_t;
276typedef uint32_t mbedtls_ct_uint_t;
277typedef int32_t mbedtls_ct_int_t;
278#define MBEDTLS_CT_TRUE ((mbedtls_ct_condition_t) UINT32_MAX)
279#endif
280#define MBEDTLS_CT_FALSE ((mbedtls_ct_condition_t) 0)
281
282/* constant_time_impl.h contains all the static inline implementations,
283 * so that constant_time_internal.h is more readable.
284 */
285#include "constant_time_impl.h"
286
287
288/* ============================================================================
289 * Boolean operations
290 */
291
292/** Convert a number into a mbedtls_ct_condition_t.
293 *
294 * \param x Number to convert.
295 *
296 * \return MBEDTLS_CT_TRUE if \p x != 0, or MBEDTLS_CT_FALSE if \p x == 0
297 *
298 */
299static inline mbedtls_ct_condition_t mbedtls_ct_bool(mbedtls_ct_uint_t x);
300
301/** Boolean "not equal" operation.
302 *
303 * Functionally equivalent to:
304 *
305 * \p x != \p y
306 *
307 * \param x The first value to analyze.
308 * \param y The second value to analyze.
309 *
310 * \return MBEDTLS_CT_TRUE if \p x != \p y, otherwise MBEDTLS_CT_FALSE.
311 */
312static inline mbedtls_ct_condition_t mbedtls_ct_bool_ne(mbedtls_ct_uint_t x, mbedtls_ct_uint_t y);
313
314/** Boolean "equals" operation.
315 *
316 * Functionally equivalent to:
317 *
318 * \p x == \p y
319 *
320 * \param x The first value to analyze.
321 * \param y The second value to analyze.
322 *
323 * \return MBEDTLS_CT_TRUE if \p x == \p y, otherwise MBEDTLS_CT_FALSE.
324 */
325static inline mbedtls_ct_condition_t mbedtls_ct_bool_eq(mbedtls_ct_uint_t x,
326 mbedtls_ct_uint_t y);
327
328/** Boolean "less than" operation.
329 *
330 * Functionally equivalent to:
331 *
332 * \p x < \p y
333 *
334 * \param x The first value to analyze.
335 * \param y The second value to analyze.
336 *
337 * \return MBEDTLS_CT_TRUE if \p x < \p y, otherwise MBEDTLS_CT_FALSE.
338 */
339static inline mbedtls_ct_condition_t mbedtls_ct_bool_lt(mbedtls_ct_uint_t x, mbedtls_ct_uint_t y);
340
341/** Boolean "greater than" operation.
342 *
343 * Functionally equivalent to:
344 *
345 * \p x > \p y
346 *
347 * \param x The first value to analyze.
348 * \param y The second value to analyze.
349 *
350 * \return MBEDTLS_CT_TRUE if \p x > \p y, otherwise MBEDTLS_CT_FALSE.
351 */
352static inline mbedtls_ct_condition_t mbedtls_ct_bool_gt(mbedtls_ct_uint_t x,
353 mbedtls_ct_uint_t y);
354
355/** Boolean "greater or equal" 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,
365 * otherwise MBEDTLS_CT_FALSE.
366 */
367static inline mbedtls_ct_condition_t mbedtls_ct_bool_ge(mbedtls_ct_uint_t x,
368 mbedtls_ct_uint_t y);
369
370/** Boolean "less than or equal" operation.
371 *
372 * Functionally equivalent to:
373 *
374 * \p x <= \p y
375 *
376 * \param x The first value to analyze.
377 * \param y The second value to analyze.
378 *
379 * \return MBEDTLS_CT_TRUE if \p x <= \p y,
380 * otherwise MBEDTLS_CT_FALSE.
381 */
382static inline mbedtls_ct_condition_t mbedtls_ct_bool_le(mbedtls_ct_uint_t x,
383 mbedtls_ct_uint_t y);
384
385/** Boolean "xor" operation.
386 *
387 * Functionally equivalent to:
388 *
389 * \p x ^ \p y
390 *
391 * \param x The first value to analyze.
392 * \param y The second value to analyze.
393 *
394 * \note This is more efficient than mbedtls_ct_bool_ne if both arguments are
395 * mbedtls_ct_condition_t.
396 *
397 * \return MBEDTLS_CT_TRUE if \p x ^ \p y,
398 * otherwise MBEDTLS_CT_FALSE.
399 */
400static inline mbedtls_ct_condition_t mbedtls_ct_bool_xor(mbedtls_ct_condition_t x,
401 mbedtls_ct_condition_t y);
402
403/** Boolean "and" operation.
404 *
405 * Functionally equivalent to:
406 *
407 * \p x && \p y
408 *
409 * \param x The first value to analyze.
410 * \param y The second value to analyze.
411 *
412 * \return MBEDTLS_CT_TRUE if \p x && \p y,
413 * otherwise MBEDTLS_CT_FALSE.
414 */
415static inline mbedtls_ct_condition_t mbedtls_ct_bool_and(mbedtls_ct_condition_t x,
416 mbedtls_ct_condition_t y);
417
418/** Boolean "or" operation.
419 *
420 * Functionally equivalent to:
421 *
422 * \p x || \p y
423 *
424 * \param x The first value to analyze.
425 * \param y The second value to analyze.
426 *
427 * \return MBEDTLS_CT_TRUE if \p x || \p y,
428 * otherwise MBEDTLS_CT_FALSE.
429 */
430static inline mbedtls_ct_condition_t mbedtls_ct_bool_or(mbedtls_ct_condition_t x,
431 mbedtls_ct_condition_t y);
432
433/** Boolean "not" operation.
434 *
435 * Functionally equivalent to:
436 *
437 * ! \p x
438 *
439 * \param x The value to invert
440 *
441 * \return MBEDTLS_CT_FALSE if \p x, otherwise MBEDTLS_CT_TRUE.
442 */
443static inline mbedtls_ct_condition_t mbedtls_ct_bool_not(mbedtls_ct_condition_t x);
444
445
446/* ============================================================================
447 * Data selection operations
448 */
449
450/** Choose between two size_t values.
451 *
452 * Functionally equivalent to:
453 *
454 * condition ? if1 : if0.
455 *
456 * \param condition Condition to test.
457 * \param if1 Value to use if \p condition == MBEDTLS_CT_TRUE.
458 * \param if0 Value to use if \p condition == MBEDTLS_CT_FALSE.
459 *
460 * \return \c if1 if \p condition == MBEDTLS_CT_TRUE, otherwise \c if0.
461 */
462static inline size_t mbedtls_ct_size_if(mbedtls_ct_condition_t condition,
463 size_t if1,
464 size_t if0);
465
466/** Choose between two unsigned values.
467 *
468 * Functionally equivalent to:
469 *
470 * condition ? if1 : if0.
471 *
472 * \param condition Condition to test.
473 * \param if1 Value to use if \p condition == MBEDTLS_CT_TRUE.
474 * \param if0 Value to use if \p condition == MBEDTLS_CT_FALSE.
475 *
476 * \return \c if1 if \p condition == MBEDTLS_CT_TRUE, otherwise \c if0.
477 */
478static inline unsigned mbedtls_ct_uint_if_new(mbedtls_ct_condition_t condition,
479 unsigned if1,
480 unsigned if0);
481
482#if defined(MBEDTLS_BIGNUM_C)
483
484/** Choose between two mbedtls_mpi_uint values.
485 *
486 * Functionally equivalent to:
487 *
488 * condition ? if1 : if0.
489 *
490 * \param condition Condition to test.
491 * \param if1 Value to use if \p condition == MBEDTLS_CT_TRUE.
492 * \param if0 Value to use if \p condition == MBEDTLS_CT_FALSE.
493 *
494 * \return \c if1 if \p condition == MBEDTLS_CT_TRUE, otherwise \c if0.
495 */
496static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if(mbedtls_ct_condition_t condition, \
497 mbedtls_mpi_uint if1, \
498 mbedtls_mpi_uint if0);
499
500#endif
501
502/** Choose between an unsigned value and 0.
503 *
504 * Functionally equivalent to:
505 *
506 * condition ? if1 : 0.
507 *
508 * Functionally equivalent tombedtls_ct_uint_if(condition, if1, 0) but
509 * results in smaller code size.
510 *
511 * \param condition Condition to test.
512 * \param if1 Value to use if \p condition == MBEDTLS_CT_TRUE.
513 *
514 * \return \c if1 if \p condition == MBEDTLS_CT_TRUE, otherwise 0.
515 */
516static inline unsigned mbedtls_ct_uint_if0(mbedtls_ct_condition_t condition, unsigned if1);
517
518#if defined(MBEDTLS_BIGNUM_C)
519
520/** Choose between an mbedtls_mpi_uint value and 0.
521 *
522 * Functionally equivalent to:
523 *
524 * condition ? if1 : 0.
525 *
526 * Functionally equivalent tombedtls_ct_mpi_uint_if(condition, if1, 0) but
527 * results in smaller code size.
528 *
529 * \param condition Condition to test.
530 * \param if1 Value to use if \p condition == MBEDTLS_CT_TRUE.
531 *
532 * \return \c if1 if \p condition == MBEDTLS_CT_TRUE, otherwise 0.
533 */
534static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if0(mbedtls_ct_condition_t condition,
535 mbedtls_mpi_uint if1);
536
537#endif
538
539/** Constant-flow char selection
540 *
541 * \param low Secret. Bottom of range
542 * \param high Secret. Top of range
543 * \param c Secret. Value to compare to range
544 * \param t Secret. Value to return, if in range
545 *
546 * \return \p t if \p low <= \p c <= \p high, 0 otherwise.
547 */
548static inline unsigned char mbedtls_ct_uchar_in_range_if(unsigned char low,
549 unsigned char high,
550 unsigned char c,
551 unsigned char t);
552
553
554/* ============================================================================
555 * Block memory operations
556 */
557
558#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
559
560/** Conditionally set a block of memory to zero.
561 *
562 * Regardless of the condition, every byte will be read once and written to
563 * once.
564 *
565 * \param condition Secret. Condition to test.
566 * \param buf Secret. Pointer to the start of the buffer.
567 * \param len Number of bytes to set to zero.
568 *
569 * \warning Unlike mbedtls_platform_zeroize, this does not have the same guarantees
570 * about not being optimised away if the memory is never read again.
571 */
572void mbedtls_ct_zeroize_if(mbedtls_ct_condition_t condition, void *buf, size_t len);
573
574/** Shift some data towards the left inside a buffer.
575 *
576 * Functionally equivalent to:
577 *
578 * memmove(start, start + offset, total - offset);
579 * memset(start + (total - offset), 0, offset);
580 *
581 * Timing independence comes at the expense of performance.
582 *
583 * \param start Secret. Pointer to the start of the buffer.
584 * \param total Total size of the buffer.
585 * \param offset Secret. Offset from which to copy \p total - \p offset bytes.
586 */
587void mbedtls_ct_memmove_left(void *start,
588 size_t total,
589 size_t offset);
590
591#endif /* defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT) */
592
593/** Conditional memcpy.
594 *
595 * Functionally equivalent to:
596 *
597 * if (condition) {
598 * memcpy(dest, src1, len);
599 * } else {
600 * if (src2 != NULL)
601 * memcpy(dest, src2, len);
602 * }
603 *
604 * It will always read len bytes from src1.
605 * If src2 != NULL, it will always read len bytes from src2.
606 * If src2 == NULL, it will instead read len bytes from dest (as if src2 == dest).
607 *
608 * \param condition The condition
609 * \param dest Secret. Destination pointer.
610 * \param src1 Secret. Pointer to copy from (if \p condition == MBEDTLS_CT_TRUE). Shouldn't overlap with \p dest.
611 * \param src2 Secret (contents only - may branch to test if src2 == NULL).
612 * 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.
613 * \param len Number of bytes to copy.
614 */
615void mbedtls_ct_memcpy_if(mbedtls_ct_condition_t condition,
616 unsigned char *dest,
617 const unsigned char *src1,
618 const unsigned char *src2,
619 size_t len
620 );
621
622/** Copy data from a secret position.
623 *
624 * Functionally equivalent to:
625 *
626 * memcpy(dst, src + offset, len)
627 *
628 * This function copies \p len bytes from \p src_base + \p offset to \p
629 * dst, with a code flow and memory access pattern that does not depend on
630 * \p offset, but only on \p offset_min, \p offset_max and \p len.
631 *
632 * \note This function reads from \p dest, but the value that
633 * is read does not influence the result and this
634 * function's behavior is well-defined regardless of the
635 * contents of the buffers. This may result in false
636 * positives from static or dynamic analyzers, especially
637 * if \p dest is not initialized.
638 *
639 * \param dest Secret. The destination buffer. This must point to a writable
640 * buffer of at least \p len bytes.
641 * \param src Secret. The base of the source buffer. This must point to a
642 * readable buffer of at least \p offset_max + \p len
643 * bytes. Shouldn't overlap with \p dest.
644 * \param offset Secret. The offset in the source buffer from which to copy.
645 * This must be no less than \p offset_min and no greater
646 * than \p offset_max.
647 * \param offset_min The minimal value of \p offset.
648 * \param offset_max The maximal value of \p offset.
649 * \param len The number of bytes to copy.
650 */
651void mbedtls_ct_memcpy_offset(unsigned char *dest,
652 const unsigned char *src,
653 size_t offset,
654 size_t offset_min,
655 size_t offset_max,
656 size_t len);
657
658/* Documented in include/mbedtls/constant_time.h. a and b are secret. */
659int mbedtls_ct_memcmp(const void *a,
660 const void *b,
661 size_t n);
662
Gabor Mezei291df7b2021-10-19 11:27:17 +0200663#endif /* MBEDTLS_CONSTANT_TIME_INTERNAL_H */