blob: e085478d69f857ec56981ccd79e189312c1f1cd8 [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
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +020023#include "common.h"
24
gabor-mezei-arm3f90fd52021-09-27 12:55:33 +020025#if defined(MBEDTLS_BIGNUM_C)
26#include "mbedtls/bignum.h"
27#endif
28
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +020029#if defined(MBEDTLS_SSL_TLS_C)
30#include "ssl_misc.h"
31#endif
32
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +020033#include <stddef.h>
34
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +020035
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +020036/** Turn a value into a mask:
37 * - if \p value == 0, return the all-bits 0 mask, aka 0
Gabor Mezeia316fc82021-10-18 16:28:27 +020038 * - otherwise, return the all-bits 1 mask, aka (unsigned) -1
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +020039 *
40 * This function can be used to write constant-time code by replacing branches
41 * with bit operations using masks.
42 *
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +020043 * \param value The value to analyze.
44 *
45 * \return Zero if \p value is zero, otherwise all-bits-one.
46 */
Gilles Peskine449bd832023-01-11 14:50:10 +010047unsigned mbedtls_ct_uint_mask(unsigned value);
gabor-mezei-arm3733bf82021-09-27 11:49:42 +020048
Andrzej Kurek084334c2022-09-27 14:19:50 -040049#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gabor Mezei6a426c92021-10-20 11:17:43 +020050
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +020051/** Turn a value into a mask:
52 * - if \p value == 0, return the all-bits 0 mask, aka 0
53 * - otherwise, return the all-bits 1 mask, aka (size_t) -1
54 *
55 * This function can be used to write constant-time code by replacing branches
56 * with bit operations using masks.
57 *
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +020058 * \param value The value to analyze.
59 *
60 * \return Zero if \p value is zero, otherwise all-bits-one.
61 */
Gilles Peskine449bd832023-01-11 14:50:10 +010062size_t mbedtls_ct_size_mask(size_t value);
gabor-mezei-armc76227d2021-09-27 11:53:54 +020063
Andrzej Kurek084334c2022-09-27 14:19:50 -040064#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Gabor Mezei6a426c92021-10-20 11:17:43 +020065
gabor-mezei-arm9cb55692021-08-11 15:07:02 +020066#if defined(MBEDTLS_BIGNUM_C)
67
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +020068/** Turn a value into a mask:
69 * - if \p value == 0, return the all-bits 0 mask, aka 0
Gabor Mezeia316fc82021-10-18 16:28:27 +020070 * - otherwise, return the all-bits 1 mask, aka (mbedtls_mpi_uint) -1
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +020071 *
72 * This function can be used to write constant-time code by replacing branches
73 * with bit operations using masks.
74 *
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +020075 * \param value The value to analyze.
76 *
77 * \return Zero if \p value is zero, otherwise all-bits-one.
78 */
Gilles Peskine449bd832023-01-11 14:50:10 +010079mbedtls_mpi_uint mbedtls_ct_mpi_uint_mask(mbedtls_mpi_uint value);
gabor-mezei-arm9cb55692021-08-11 15:07:02 +020080
81#endif /* MBEDTLS_BIGNUM_C */
82
Gabor Mezeie2123792021-10-18 17:05:06 +020083#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
84
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +020085/** Constant-flow mask generation for "greater or equal" comparison:
86 * - if \p x >= \p y, return all-bits 1, that is (size_t) -1
87 * - otherwise, return all bits 0, that is 0
88 *
89 * This function can be used to write constant-time code by replacing branches
90 * with bit operations using masks.
91 *
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +020092 * \param x The first value to analyze.
93 * \param y The second value to analyze.
94 *
95 * \return All-bits-one if \p x is greater or equal than \p y,
96 * otherwise zero.
97 */
Gilles Peskine449bd832023-01-11 14:50:10 +010098size_t mbedtls_ct_size_mask_ge(size_t x,
99 size_t y);
gabor-mezei-arm8d1d5fd2021-09-27 12:15:19 +0200100
Gabor Mezeie2123792021-10-18 17:05:06 +0200101#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
102
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200103/** Constant-flow boolean "equal" comparison:
104 * return x == y
105 *
106 * This is equivalent to \p x == \p y, but is likely to be compiled
107 * to code using bitwise operation rather than a branch.
108 *
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200109 * \param x The first value to analyze.
110 * \param y The second value to analyze.
111 *
112 * \return 1 if \p x equals to \p y, otherwise 0.
113 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100114unsigned mbedtls_ct_size_bool_eq(size_t x,
115 size_t y);
gabor-mezei-arm5a854422021-09-27 12:25:07 +0200116
gabor-mezei-arm3f90fd52021-09-27 12:55:33 +0200117#if defined(MBEDTLS_BIGNUM_C)
118
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200119/** Decide if an integer is less than the other, without branches.
120 *
121 * This is equivalent to \p x < \p y, but is likely to be compiled
122 * to code using bitwise operation rather than a branch.
123 *
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200124 * \param x The first value to analyze.
125 * \param y The second value to analyze.
126 *
127 * \return 1 if \p x is less than \p y, otherwise 0.
128 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100129unsigned mbedtls_ct_mpi_uint_lt(const mbedtls_mpi_uint x,
130 const mbedtls_mpi_uint y);
gabor-mezei-arm3f90fd52021-09-27 12:55:33 +0200131
132#endif /* MBEDTLS_BIGNUM_C */
gabor-mezei-armb2dbf2c2021-09-27 12:59:30 +0200133
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200134/** Choose between two integer values without branches.
135 *
136 * This is equivalent to `condition ? if1 : if0`, but is likely to be compiled
137 * to code using bitwise operation rather than a branch.
138 *
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200139 * \param condition Condition to test.
140 * \param if1 Value to use if \p condition is nonzero.
141 * \param if0 Value to use if \p condition is zero.
142 *
143 * \return \c if1 if \p condition is nonzero, otherwise \c if0.
144 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100145unsigned mbedtls_ct_uint_if(unsigned condition,
146 unsigned if1,
147 unsigned if0);
gabor-mezei-armb2dbf2c2021-09-27 12:59:30 +0200148
gabor-mezei-armbe8d98b2021-09-27 13:17:15 +0200149#if defined(MBEDTLS_BIGNUM_C)
150
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200151/** Conditionally assign a value without branches.
152 *
153 * This is equivalent to `if ( condition ) dest = src`, but is likely
154 * to be compiled to code using bitwise operation rather than a branch.
155 *
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200156 * \param n \p dest and \p src must be arrays of limbs of size n.
157 * \param dest The MPI to conditionally assign to. This must point
158 * to an initialized MPI.
159 * \param src The MPI to be assigned from. This must point to an
160 * initialized MPI.
161 * \param condition Condition to test, must be 0 or 1.
162 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100163void mbedtls_ct_mpi_uint_cond_assign(size_t n,
164 mbedtls_mpi_uint *dest,
165 const mbedtls_mpi_uint *src,
166 unsigned char condition);
gabor-mezei-armbe8d98b2021-09-27 13:17:15 +0200167
168#endif /* MBEDTLS_BIGNUM_C */
gabor-mezei-arm394aeaa2021-09-27 13:31:06 +0200169
Przemek Stekiel89ad6232022-09-27 13:36:12 +0200170#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200171
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200172/** Conditional memcpy without branches.
173 *
Gabor Mezei642eeb22021-11-03 16:13:32 +0100174 * This is equivalent to `if ( c1 == c2 ) memcpy(dest, src, len)`, but is likely
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200175 * to be compiled to code using bitwise operation rather than a branch.
176 *
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200177 * \param dest The pointer to conditionally copy to.
Gabor Mezeia316fc82021-10-18 16:28:27 +0200178 * \param src The pointer to copy from. Shouldn't overlap with \p dest.
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200179 * \param len The number of bytes to copy.
180 * \param c1 The first value to analyze in the condition.
181 * \param c2 The second value to analyze in the condition.
182 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100183void mbedtls_ct_memcpy_if_eq(unsigned char *dest,
184 const unsigned char *src,
185 size_t len,
186 size_t c1, size_t c2);
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200187
188/** Copy data from a secret position with constant flow.
189 *
190 * This function copies \p len bytes from \p src_base + \p offset_secret to \p
191 * dst, with a code flow and memory access pattern that does not depend on \p
192 * offset_secret, but only on \p offset_min, \p offset_max and \p len.
Gabor Mezeia316fc82021-10-18 16:28:27 +0200193 * Functionally equivalent to `memcpy(dst, src + offset_secret, len)`.
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200194 *
Paul Elliott5260ce22022-05-09 18:15:54 +0100195 * \note This function reads from \p dest, but the value that
196 * is read does not influence the result and this
197 * function's behavior is well-defined regardless of the
198 * contents of the buffers. This may result in false
199 * positives from static or dynamic analyzers, especially
200 * if \p dest is not initialized.
201 *
Gabor Mezei63bbba52021-10-18 16:17:57 +0200202 * \param dest The destination buffer. This must point to a writable
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200203 * buffer of at least \p len bytes.
Gabor Mezei63bbba52021-10-18 16:17:57 +0200204 * \param src The base of the source buffer. This must point to a
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200205 * readable buffer of at least \p offset_max + \p len
Gabor Mezei63bbba52021-10-18 16:17:57 +0200206 * bytes. Shouldn't overlap with \p dest.
207 * \param offset The offset in the source buffer from which to copy.
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200208 * This must be no less than \p offset_min and no greater
209 * than \p offset_max.
Gabor Mezei63bbba52021-10-18 16:17:57 +0200210 * \param offset_min The minimal value of \p offset.
211 * \param offset_max The maximal value of \p offset.
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200212 * \param len The number of bytes to copy.
213 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100214void mbedtls_ct_memcpy_offset(unsigned char *dest,
215 const unsigned char *src,
216 size_t offset,
217 size_t offset_min,
218 size_t offset_max,
219 size_t len);
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +0200220
Przemek Stekiel89ad6232022-09-27 13:36:12 +0200221#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
gabor-mezei-armfdb71182021-09-27 16:11:12 +0200222
Dave Rodgman0ee96832023-05-09 09:49:01 +0100223#if defined(MBEDTLS_BASE64_C)
224
Dave Rodgman8c94e212023-05-09 10:39:03 +0100225/** Constant-flow char selection
Dave Rodgman0ee96832023-05-09 09:49:01 +0100226 *
Dave Rodgman8c94e212023-05-09 10:39:03 +0100227 * \param low Bottom of range
228 * \param high Top of range
229 * \param c Value to compare to range
230 * \param t Value to return, if in range
231 *
232 * \return \p t if \p low <= \p c <= \p high, 0 otherwise.
Dave Rodgman0ee96832023-05-09 09:49:01 +0100233 */
Dave Rodgman8c94e212023-05-09 10:39:03 +0100234static inline unsigned char mbedtls_ct_uchar_in_range_if(unsigned char low,
235 unsigned char high,
236 unsigned char c,
237 unsigned char t)
238{
239 /* low_mask is: 0 if low <= c, 0x...ff if low > c */
240 unsigned low_mask = ((unsigned) c - low) >> 8;
241 /* high_mask is: 0 if c <= high, 0x...ff if c > high */
242 unsigned high_mask = ((unsigned) high - c) >> 8;
243 return (unsigned char)
244 mbedtls_ct_uint_if(~mbedtls_ct_mpi_uint_mask(low_mask | high_mask), t, 0);
245}
Dave Rodgman0ee96832023-05-09 09:49:01 +0100246
247#endif /* MBEDTLS_BASE64_C */
248
Dave Rodgman0afe0012023-05-09 11:09:52 +0100249
250#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
251
252/** Constant-flow "greater than" comparison:
253 * return x > y
254 *
255 * This is equivalent to \p x > \p y, but is likely to be compiled
256 * to code using bitwise operation rather than a branch.
257 *
258 * \param x The first value to analyze.
259 * \param y The second value to analyze.
260 *
261 * \return 1 if \p x greater than \p y, otherwise 0.
262 */
263unsigned mbedtls_ct_size_gt(size_t x, size_t y);
264
265/** Shift some data towards the left inside a buffer.
266 *
267 * `mbedtls_ct_mem_move_to_left(start, total, offset)` is functionally
268 * equivalent to
269 * ```
270 * memmove(start, start + offset, total - offset);
271 * memset(start + offset, 0, total - offset);
272 * ```
273 * but it strives to use a memory access pattern (and thus total timing)
274 * that does not depend on \p offset. This timing independence comes at
275 * the expense of performance.
276 *
277 * \param start Pointer to the start of the buffer.
278 * \param total Total size of the buffer.
279 * \param offset Offset from which to copy \p total - \p offset bytes.
280 */
281void mbedtls_ct_mem_move_to_left(void *start,
Dave Rodgman19e8cd02023-05-09 11:10:21 +0100282 size_t total,
283 size_t offset);
Dave Rodgman0afe0012023-05-09 11:09:52 +0100284
285#endif /* defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT) */
286
Gabor Mezei291df7b2021-10-19 11:27:17 +0200287#endif /* MBEDTLS_CONSTANT_TIME_INTERNAL_H */