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