blob: 3877272d2124882e1d7bad53df2fe02705644e37 [file] [log] [blame]
gabor-mezei-arm944c1072021-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
20#include "common.h"
21
gabor-mezei-arm097d4f52021-09-27 12:55:33 +020022#if defined(MBEDTLS_BIGNUM_C)
23#include "mbedtls/bignum.h"
24#endif
25
gabor-mezei-armcb4317b2021-09-27 14:28:31 +020026#if defined(MBEDTLS_SSL_TLS_C)
27#include "mbedtls/ssl_internal.h"
28#endif
29
gabor-mezei-arm944c1072021-09-27 11:28:54 +020030#include <stddef.h>
31
gabor-mezei-arm944c1072021-09-27 11:28:54 +020032
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +020033/** Constant-time buffer comparison without branches.
34 *
35 * This is equivalent to the standard memncmp function, but is likely to be
36 * compiled to code using bitwise operation rather than a branch.
37 *
38 * This function can be used to write constant-time code by replacing branches
39 * with bit operations using masks.
40 *
41 * This function is implemented without using comparison operators, as those
42 * might be translated to branches by some compilers on some platforms.
43 *
44 * \param a Pointer to the first buffer.
45 * \param b Pointer to the second buffer.
46 * \param n The number of bytes to compare in the buffer.
47 *
48 * \return Zero if the content of the two buffer is the same,
49 * otherwise non-zero.
50 */
gabor-mezei-arm378e7eb2021-07-19 15:19:19 +020051int mbedtls_cf_memcmp( const void *a,
52 const void *b,
53 size_t n );
gabor-mezei-armc11cac92021-09-27 11:40:03 +020054
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +020055/** Turn a value into a mask:
56 * - if \p value == 0, return the all-bits 0 mask, aka 0
57 * - otherwise, return the all-bits 1 mask, aka (size_t) -1
58 *
59 * This function can be used to write constant-time code by replacing branches
60 * with bit operations using masks.
61 *
62 * This function is implemented without using comparison operators, as those
63 * might be translated to branches by some compilers on some platforms.
64 *
65 * \param value The value to analyze.
66 *
67 * \return Zero if \p value is zero, otherwise all-bits-one.
68 */
gabor-mezei-armc11cac92021-09-27 11:40:03 +020069unsigned mbedtls_cf_uint_mask( unsigned value );
gabor-mezei-armd361ccd2021-09-27 11:49:42 +020070
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +020071/** Turn a value into a mask:
72 * - if \p value == 0, return the all-bits 0 mask, aka 0
73 * - otherwise, return the all-bits 1 mask, aka (size_t) -1
74 *
75 * This function can be used to write constant-time code by replacing branches
76 * with bit operations using masks.
77 *
78 * This function is implemented without using comparison operators, as those
79 * might be translated to branches by some compilers on some platforms.
80 *
81 * \param value The value to analyze.
82 *
83 * \return Zero if \p value is zero, otherwise all-bits-one.
84 */
gabor-mezei-arm2f2c0be2021-08-10 20:56:21 +020085size_t mbedtls_cf_size_mask( size_t value );
gabor-mezei-arm4d6b1462021-09-27 11:53:54 +020086
gabor-mezei-arm60febd52021-08-11 15:07:02 +020087#if defined(MBEDTLS_BIGNUM_C)
88
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +020089/** Turn a value into a mask:
90 * - if \p value == 0, return the all-bits 0 mask, aka 0
91 * - otherwise, return the all-bits 1 mask, aka (size_t) -1
92 *
93 * This function can be used to write constant-time code by replacing branches
94 * with bit operations using masks.
95 *
96 * This function is implemented without using comparison operators, as those
97 * might be translated to branches by some compilers on some platforms.
98 *
99 * \param value The value to analyze.
100 *
101 * \return Zero if \p value is zero, otherwise all-bits-one.
102 */
gabor-mezei-arm60febd52021-08-11 15:07:02 +0200103mbedtls_mpi_uint mbedtls_cf_mpi_uint_mask( mbedtls_mpi_uint value );
104
105#endif /* MBEDTLS_BIGNUM_C */
106
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200107/** Constant-flow mask generation for "greater or equal" comparison:
108 * - if \p x >= \p y, return all-bits 1, that is (size_t) -1
109 * - otherwise, return all bits 0, that is 0
110 *
111 * This function can be used to write constant-time code by replacing branches
112 * with bit operations using masks.
113 *
114 * This function is implemented without using comparison operators, as those
115 * might be translated to branches by some compilers on some platforms.
116 *
117 * \param x The first value to analyze.
118 * \param y The second value to analyze.
119 *
120 * \return All-bits-one if \p x is greater or equal than \p y,
121 * otherwise zero.
122 */
gabor-mezei-arm04087df2021-09-27 16:29:52 +0200123size_t mbedtls_cf_size_mask_ge( size_t x,
124 size_t y );
gabor-mezei-arm96584dd2021-09-27 12:15:19 +0200125
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200126/** Constant-flow boolean "equal" comparison:
127 * return x == y
128 *
129 * This is equivalent to \p x == \p y, but is likely to be compiled
130 * to code using bitwise operation rather than a branch.
131 *
132 * This function is implemented without using comparison operators, as those
133 * might be translated to branches by some compilers on some platforms.
134 *
135 * \param x The first value to analyze.
136 * \param y The second value to analyze.
137 *
138 * \return 1 if \p x equals to \p y, otherwise 0.
139 */
gabor-mezei-arm1ffd0cc2021-08-11 17:28:49 +0200140unsigned mbedtls_cf_size_bool_eq( size_t x,
141 size_t y );
gabor-mezei-arm9d7bf092021-09-27 12:25:07 +0200142
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200143/** Constant-flow "greater than" comparison:
144 * return x > y
145 *
146 * This is equivalent to \p x > \p y, but is likely to be compiled
147 * to code using bitwise operation rather than a branch.
148 *
149 * This function is implemented without using comparison operators, as those
150 * might be translated to branches by some compilers on some platforms.
151 *
152 * \param x The first value to analyze.
153 * \param y The second value to analyze.
154 *
155 * \return 1 if \p x greater than \p y, otherwise 0.
156 */
gabor-mezei-arm5e488242021-08-10 20:36:09 +0200157unsigned mbedtls_cf_size_gt( size_t x,
158 size_t y );
gabor-mezei-arm097d4f52021-09-27 12:55:33 +0200159
160#if defined(MBEDTLS_BIGNUM_C)
161
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200162/** Decide if an integer is less than the other, without branches.
163 *
164 * This is equivalent to \p x < \p y, but is likely to be compiled
165 * to code using bitwise operation rather than a branch.
166 *
167 * This function is implemented without using comparison operators, as those
168 * might be translated to branches by some compilers on some platforms.
169 *
170 * \param x The first value to analyze.
171 * \param y The second value to analyze.
172 *
173 * \return 1 if \p x is less than \p y, otherwise 0.
174 */
gabor-mezei-arm097d4f52021-09-27 12:55:33 +0200175unsigned mbedtls_cf_mpi_uint_lt( const mbedtls_mpi_uint x,
176 const mbedtls_mpi_uint y );
177
178#endif /* MBEDTLS_BIGNUM_C */
gabor-mezei-arm75332532021-09-27 12:59:30 +0200179
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200180/** Choose between two integer values without branches.
181 *
182 * This is equivalent to `condition ? if1 : if0`, but is likely to be compiled
183 * to code using bitwise operation rather than a branch.
184 *
185 * This function is implemented without using comparison operators, as those
186 * might be translated to branches by some compilers on some platforms.
187 *
188 * \param condition Condition to test.
189 * \param if1 Value to use if \p condition is nonzero.
190 * \param if0 Value to use if \p condition is zero.
191 *
192 * \return \c if1 if \p condition is nonzero, otherwise \c if0.
193 */
gabor-mezei-arm5e488242021-08-10 20:36:09 +0200194unsigned mbedtls_cf_uint_if( unsigned condition,
gabor-mezei-arm04087df2021-09-27 16:29:52 +0200195 unsigned if1,
196 unsigned if0 );
gabor-mezei-arm75332532021-09-27 12:59:30 +0200197
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200198/** Choose between two integer values without branches.
199 *
200 * This is equivalent to `condition ? if1 : if0`, but is likely to be compiled
201 * to code using bitwise operation rather than a branch.
202 *
203 * This function is implemented without using comparison operators, as those
204 * might be translated to branches by some compilers on some platforms.
205 *
206 * \param condition Condition to test.
207 * \param if1 Value to use if \p condition is nonzero.
208 * \param if0 Value to use if \p condition is zero.
209 *
210 * \return \c if1 if \p condition is nonzero, otherwise \c if0.
211 */
gabor-mezei-arm5e488242021-08-10 20:36:09 +0200212size_t mbedtls_cf_size_if( unsigned condition,
gabor-mezei-arm04087df2021-09-27 16:29:52 +0200213 size_t if1,
214 size_t if0 );
gabor-mezei-armbc3a2882021-09-27 15:47:00 +0200215
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200216/** Select between two sign values witout branches.
217 *
218 * This is functionally equivalent to `condition ? if1 : if0` but uses only bit
219 * operations in order to avoid branches.
220 *
221 * This function is implemented without using comparison operators, as those
222 * might be translated to branches by some compilers on some platforms.
223 *
224 * \param condition Condition to test.
225 * \param if1 The first sign; must be either +1 or -1.
226 * \param if0 The second sign; must be either +1 or -1.
227 *
228 * \return \c if1 if \p condition is nonzero, otherwise \c if0. */
gabor-mezei-arm5e488242021-08-10 20:36:09 +0200229int mbedtls_cf_cond_select_sign( unsigned char condition,
230 int if1,
231 int if0 );
gabor-mezei-arm043192d2021-09-27 13:17:15 +0200232
233#if defined(MBEDTLS_BIGNUM_C)
234
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200235/** Conditionally assign a value without branches.
236 *
237 * This is equivalent to `if ( condition ) dest = src`, but is likely
238 * to be compiled to code using bitwise operation rather than a branch.
239 *
240 * This function is implemented without using comparison operators, as those
241 * might be translated to branches by some compilers on some platforms.
242 *
243 * \param n \p dest and \p src must be arrays of limbs of size n.
244 * \param dest The MPI to conditionally assign to. This must point
245 * to an initialized MPI.
246 * \param src The MPI to be assigned from. This must point to an
247 * initialized MPI.
248 * \param condition Condition to test, must be 0 or 1.
249 */
gabor-mezei-arm043192d2021-09-27 13:17:15 +0200250void mbedtls_cf_mpi_uint_cond_assign( size_t n,
251 mbedtls_mpi_uint *dest,
252 const mbedtls_mpi_uint *src,
gabor-mezei-arm5e488242021-08-10 20:36:09 +0200253 unsigned char condition );
gabor-mezei-arm043192d2021-09-27 13:17:15 +0200254
255#endif /* MBEDTLS_BIGNUM_C */
gabor-mezei-arm7b23c0b2021-09-27 13:31:06 +0200256
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200257
258/** Shift some data towards the left inside a buffer.
259 *
260 * `mbedtls_cf_mem_move_to_left(start, total, offset)` is functionally
261 * equivalent to
262 * ```
263 * memmove(start, start + offset, total - offset);
264 * memset(start + offset, 0, total - offset);
265 * ```
266 * but it strives to use a memory access pattern (and thus total timing)
267 * that does not depend on \p offset. This timing independence comes at
268 * the expense of performance.
269 *
270 * \param start Pointer to the start of the buffer.
271 * \param total Total size of the buffer.
272 * \param offset Offset from which to copy \p total - \p offset bytes.
273 */
gabor-mezei-arm7b23c0b2021-09-27 13:31:06 +0200274void mbedtls_cf_mem_move_to_left( void *start,
275 size_t total,
276 size_t offset );
gabor-mezei-armee06feb2021-09-27 13:34:25 +0200277
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200278/** Conditional memcpy without branches.
279 *
280 * This is equivalent to `if ( c1 == c2 ) memcpy(dst, src, len)`, but is likely
281 * to be compiled to code using bitwise operation rather than a branch.
282 *
283 * This function is implemented without using comparison operators, as those
284 * might be translated to branches by some compilers on some platforms.
285 *
286 * \param dest The pointer to conditionally copy to.
287 * \param src The pointer to copy from.
288 * \param len The number of bytes to copy.
289 * \param c1 The first value to analyze in the condition.
290 * \param c2 The second value to analyze in the condition.
291 */
gabor-mezei-arm5e488242021-08-10 20:36:09 +0200292void mbedtls_cf_memcpy_if_eq( unsigned char *dest,
gabor-mezei-armee06feb2021-09-27 13:34:25 +0200293 const unsigned char *src,
294 size_t len,
295 size_t c1, size_t c2 );
gabor-mezei-arm0f7b9e42021-09-27 13:57:45 +0200296
297/** Copy data from a secret position with constant flow.
298 *
299 * This function copies \p len bytes from \p src_base + \p offset_secret to \p
300 * dst, with a code flow and memory access pattern that does not depend on \p
301 * offset_secret, but only on \p offset_min, \p offset_max and \p len.
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200302 * Functionally equivalent to memcpy(dst, src + offset_secret, len).
gabor-mezei-arm0f7b9e42021-09-27 13:57:45 +0200303 *
304 * \param dst The destination buffer. This must point to a writable
305 * buffer of at least \p len bytes.
306 * \param src_base The base of the source buffer. This must point to a
307 * readable buffer of at least \p offset_max + \p len
308 * bytes.
309 * \param offset_secret The offset in the source buffer from which to copy.
310 * This must be no less than \p offset_min and no greater
311 * than \p offset_max.
312 * \param offset_min The minimal value of \p offset_secret.
313 * \param offset_max The maximal value of \p offset_secret.
314 * \param len The number of bytes to copy.
315 */
316void mbedtls_cf_memcpy_offset( unsigned char *dst,
317 const unsigned char *src_base,
318 size_t offset_secret,
gabor-mezei-arm04087df2021-09-27 16:29:52 +0200319 size_t offset_min,
320 size_t offset_max,
gabor-mezei-arm0f7b9e42021-09-27 13:57:45 +0200321 size_t len );
gabor-mezei-armcb4317b2021-09-27 14:28:31 +0200322
323#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
324
325/** Compute the HMAC of variable-length data with constant flow.
326 *
327 * This function computes the HMAC of the concatenation of \p add_data and \p
328 * data, and does with a code flow and memory access pattern that does not
329 * depend on \p data_len_secret, but only on \p min_data_len and \p
330 * max_data_len. In particular, this function always reads exactly \p
331 * max_data_len bytes from \p data.
332 *
333 * \param ctx The HMAC context. It must have keys configured
334 * with mbedtls_md_hmac_starts() and use one of the
335 * following hashes: SHA-384, SHA-256, SHA-1 or MD-5.
336 * It is reset using mbedtls_md_hmac_reset() after
337 * the computation is complete to prepare for the
338 * next computation.
339 * \param add_data The additional data prepended to \p data. This
340 * must point to a readable buffer of \p add_data_len
341 * bytes.
342 * \param add_data_len The length of \p add_data in bytes.
343 * \param data The data appended to \p add_data. This must point
344 * to a readable buffer of \p max_data_len bytes.
345 * \param data_len_secret The length of the data to process in \p data.
346 * This must be no less than \p min_data_len and no
347 * greater than \p max_data_len.
348 * \param min_data_len The minimal length of \p data in bytes.
349 * \param max_data_len The maximal length of \p data in bytes.
350 * \param output The HMAC will be written here. This must point to
351 * a writable buffer of sufficient size to hold the
352 * HMAC value.
353 *
354 * \retval 0 on success.
355 * \retval MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED
356 * The hardware accelerator failed.
357 */
gabor-mezei-arm04087df2021-09-27 16:29:52 +0200358int mbedtls_cf_hmac( mbedtls_md_context_t *ctx,
359 const unsigned char *add_data,
360 size_t add_data_len,
361 const unsigned char *data,
362 size_t data_len_secret,
363 size_t min_data_len,
364 size_t max_data_len,
365 unsigned char *output );
gabor-mezei-armcb4317b2021-09-27 14:28:31 +0200366
367#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
gabor-mezei-armf52941e2021-09-27 16:11:12 +0200368
369#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
370
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200371/** This function performs the unpadding part of a PKCS#1 v1.5 decryption
372 * operation (RSAES-PKCS1-v1_5-DECRYPT).
373 *
374 * \note The output buffer length \c output_max_len should be
375 * as large as the size \p ctx->len of \p ctx->N, for example,
376 * 128 Bytes if RSA-1024 is used, to be able to hold an
377 * arbitrary decrypted message. If it is not large enough to
378 * hold the decryption of the particular ciphertext provided,
379 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
380 *
381 * \param mode The mode of operation. This must be either
382 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
383 * \param ilen The length of the ciphertext.
384 * \param olen The address at which to store the length of
385 * the plaintext. This must not be \c NULL.
386 * \param output The buffer used to hold the plaintext. This must
387 * be a writable buffer of length \p output_max_len Bytes.
388 * \param output_max_len The length in Bytes of the output buffer \p output.
389 * \param buf The input buffer for the unpadding operation.
390 *
391 * \return \c 0 on success.
392 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
393 */
gabor-mezei-armf52941e2021-09-27 16:11:12 +0200394int mbedtls_cf_rsaes_pkcs1_v15_unpadding( int mode,
395 size_t ilen,
396 size_t *olen,
397 unsigned char *output,
398 size_t output_max_len,
399 unsigned char *buf );
400
401#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */