blob: 83329b175b03a8c3b56cca2e0be5671e90da216d [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 "less than" 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 less than \p y, otherwise zero.
121 */
gabor-mezei-arm04087df2021-09-27 16:29:52 +0200122size_t mbedtls_cf_size_mask_lt( size_t x,
123 size_t y );
gabor-mezei-arma2bcabc2021-09-27 11:58:31 +0200124
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200125/** Constant-flow mask generation for "greater or equal" comparison:
126 * - if \p x >= \p y, return all-bits 1, that is (size_t) -1
127 * - otherwise, return all bits 0, that is 0
128 *
129 * This function can be used to write constant-time code by replacing branches
130 * with bit operations using masks.
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 All-bits-one if \p x is greater or equal than \p y,
139 * otherwise zero.
140 */
gabor-mezei-arm04087df2021-09-27 16:29:52 +0200141size_t mbedtls_cf_size_mask_ge( size_t x,
142 size_t y );
gabor-mezei-arm96584dd2021-09-27 12:15:19 +0200143
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200144/** Constant-flow boolean "equal" comparison:
145 * return x == y
146 *
147 * This is equivalent to \p x == \p y, but is likely to be compiled
148 * to code using bitwise operation rather than a branch.
149 *
150 * This function is implemented without using comparison operators, as those
151 * might be translated to branches by some compilers on some platforms.
152 *
153 * \param x The first value to analyze.
154 * \param y The second value to analyze.
155 *
156 * \return 1 if \p x equals to \p y, otherwise 0.
157 */
gabor-mezei-arm1ffd0cc2021-08-11 17:28:49 +0200158unsigned mbedtls_cf_size_bool_eq( size_t x,
159 size_t y );
gabor-mezei-arm9d7bf092021-09-27 12:25:07 +0200160
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200161/** Constant-flow "greater than" comparison:
162 * return x > y
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 greater than \p y, otherwise 0.
174 */
gabor-mezei-arm5e488242021-08-10 20:36:09 +0200175unsigned mbedtls_cf_size_gt( size_t x,
176 size_t y );
gabor-mezei-arm097d4f52021-09-27 12:55:33 +0200177
178#if defined(MBEDTLS_BIGNUM_C)
179
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200180/** Decide if an integer is less than the other, without branches.
181 *
182 * This is equivalent to \p x < \p y, 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 x The first value to analyze.
189 * \param y The second value to analyze.
190 *
191 * \return 1 if \p x is less than \p y, otherwise 0.
192 */
gabor-mezei-arm097d4f52021-09-27 12:55:33 +0200193unsigned mbedtls_cf_mpi_uint_lt( const mbedtls_mpi_uint x,
194 const mbedtls_mpi_uint y );
195
196#endif /* MBEDTLS_BIGNUM_C */
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 +0200212unsigned mbedtls_cf_uint_if( unsigned condition,
gabor-mezei-arm04087df2021-09-27 16:29:52 +0200213 unsigned if1,
214 unsigned if0 );
gabor-mezei-arm75332532021-09-27 12:59:30 +0200215
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200216/** Choose between two integer values without branches.
217 *
218 * This is equivalent to `condition ? if1 : if0`, but is likely to be compiled
219 * to code using bitwise operation rather than a branch.
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 Value to use if \p condition is nonzero.
226 * \param if0 Value to use if \p condition is zero.
227 *
228 * \return \c if1 if \p condition is nonzero, otherwise \c if0.
229 */
gabor-mezei-arm5e488242021-08-10 20:36:09 +0200230size_t mbedtls_cf_size_if( unsigned condition,
gabor-mezei-arm04087df2021-09-27 16:29:52 +0200231 size_t if1,
232 size_t if0 );
gabor-mezei-armbc3a2882021-09-27 15:47:00 +0200233
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200234/** Select between two sign values witout branches.
235 *
236 * This is functionally equivalent to `condition ? if1 : if0` but uses only bit
237 * operations in order to avoid branches.
238 *
239 * This function is implemented without using comparison operators, as those
240 * might be translated to branches by some compilers on some platforms.
241 *
242 * \param condition Condition to test.
243 * \param if1 The first sign; must be either +1 or -1.
244 * \param if0 The second sign; must be either +1 or -1.
245 *
246 * \return \c if1 if \p condition is nonzero, otherwise \c if0. */
gabor-mezei-arm5e488242021-08-10 20:36:09 +0200247int mbedtls_cf_cond_select_sign( unsigned char condition,
248 int if1,
249 int if0 );
gabor-mezei-arm043192d2021-09-27 13:17:15 +0200250
251#if defined(MBEDTLS_BIGNUM_C)
252
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200253/** Conditionally assign a value without branches.
254 *
255 * This is equivalent to `if ( condition ) dest = src`, but is likely
256 * to be compiled to code using bitwise operation rather than a branch.
257 *
258 * This function is implemented without using comparison operators, as those
259 * might be translated to branches by some compilers on some platforms.
260 *
261 * \param n \p dest and \p src must be arrays of limbs of size n.
262 * \param dest The MPI to conditionally assign to. This must point
263 * to an initialized MPI.
264 * \param src The MPI to be assigned from. This must point to an
265 * initialized MPI.
266 * \param condition Condition to test, must be 0 or 1.
267 */
gabor-mezei-arm043192d2021-09-27 13:17:15 +0200268void mbedtls_cf_mpi_uint_cond_assign( size_t n,
269 mbedtls_mpi_uint *dest,
270 const mbedtls_mpi_uint *src,
gabor-mezei-arm5e488242021-08-10 20:36:09 +0200271 unsigned char condition );
gabor-mezei-arm043192d2021-09-27 13:17:15 +0200272
273#endif /* MBEDTLS_BIGNUM_C */
gabor-mezei-arm7b23c0b2021-09-27 13:31:06 +0200274
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200275
276/** Shift some data towards the left inside a buffer.
277 *
278 * `mbedtls_cf_mem_move_to_left(start, total, offset)` is functionally
279 * equivalent to
280 * ```
281 * memmove(start, start + offset, total - offset);
282 * memset(start + offset, 0, total - offset);
283 * ```
284 * but it strives to use a memory access pattern (and thus total timing)
285 * that does not depend on \p offset. This timing independence comes at
286 * the expense of performance.
287 *
288 * \param start Pointer to the start of the buffer.
289 * \param total Total size of the buffer.
290 * \param offset Offset from which to copy \p total - \p offset bytes.
291 */
gabor-mezei-arm7b23c0b2021-09-27 13:31:06 +0200292void mbedtls_cf_mem_move_to_left( void *start,
293 size_t total,
294 size_t offset );
gabor-mezei-armee06feb2021-09-27 13:34:25 +0200295
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200296/** Conditional memcpy without branches.
297 *
298 * This is equivalent to `if ( c1 == c2 ) memcpy(dst, src, len)`, but is likely
299 * to be compiled to code using bitwise operation rather than a branch.
300 *
301 * This function is implemented without using comparison operators, as those
302 * might be translated to branches by some compilers on some platforms.
303 *
304 * \param dest The pointer to conditionally copy to.
305 * \param src The pointer to copy from.
306 * \param len The number of bytes to copy.
307 * \param c1 The first value to analyze in the condition.
308 * \param c2 The second value to analyze in the condition.
309 */
gabor-mezei-arm5e488242021-08-10 20:36:09 +0200310void mbedtls_cf_memcpy_if_eq( unsigned char *dest,
gabor-mezei-armee06feb2021-09-27 13:34:25 +0200311 const unsigned char *src,
312 size_t len,
313 size_t c1, size_t c2 );
gabor-mezei-arm0f7b9e42021-09-27 13:57:45 +0200314
315/** Copy data from a secret position with constant flow.
316 *
317 * This function copies \p len bytes from \p src_base + \p offset_secret to \p
318 * dst, with a code flow and memory access pattern that does not depend on \p
319 * offset_secret, but only on \p offset_min, \p offset_max and \p len.
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200320 * Functionally equivalent to memcpy(dst, src + offset_secret, len).
gabor-mezei-arm0f7b9e42021-09-27 13:57:45 +0200321 *
322 * \param dst The destination buffer. This must point to a writable
323 * buffer of at least \p len bytes.
324 * \param src_base The base of the source buffer. This must point to a
325 * readable buffer of at least \p offset_max + \p len
326 * bytes.
327 * \param offset_secret The offset in the source buffer from which to copy.
328 * This must be no less than \p offset_min and no greater
329 * than \p offset_max.
330 * \param offset_min The minimal value of \p offset_secret.
331 * \param offset_max The maximal value of \p offset_secret.
332 * \param len The number of bytes to copy.
333 */
334void mbedtls_cf_memcpy_offset( unsigned char *dst,
335 const unsigned char *src_base,
336 size_t offset_secret,
gabor-mezei-arm04087df2021-09-27 16:29:52 +0200337 size_t offset_min,
338 size_t offset_max,
gabor-mezei-arm0f7b9e42021-09-27 13:57:45 +0200339 size_t len );
gabor-mezei-armcb4317b2021-09-27 14:28:31 +0200340
341#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
342
343/** Compute the HMAC of variable-length data with constant flow.
344 *
345 * This function computes the HMAC of the concatenation of \p add_data and \p
346 * data, and does with a code flow and memory access pattern that does not
347 * depend on \p data_len_secret, but only on \p min_data_len and \p
348 * max_data_len. In particular, this function always reads exactly \p
349 * max_data_len bytes from \p data.
350 *
351 * \param ctx The HMAC context. It must have keys configured
352 * with mbedtls_md_hmac_starts() and use one of the
353 * following hashes: SHA-384, SHA-256, SHA-1 or MD-5.
354 * It is reset using mbedtls_md_hmac_reset() after
355 * the computation is complete to prepare for the
356 * next computation.
357 * \param add_data The additional data prepended to \p data. This
358 * must point to a readable buffer of \p add_data_len
359 * bytes.
360 * \param add_data_len The length of \p add_data in bytes.
361 * \param data The data appended to \p add_data. This must point
362 * to a readable buffer of \p max_data_len bytes.
363 * \param data_len_secret The length of the data to process in \p data.
364 * This must be no less than \p min_data_len and no
365 * greater than \p max_data_len.
366 * \param min_data_len The minimal length of \p data in bytes.
367 * \param max_data_len The maximal length of \p data in bytes.
368 * \param output The HMAC will be written here. This must point to
369 * a writable buffer of sufficient size to hold the
370 * HMAC value.
371 *
372 * \retval 0 on success.
373 * \retval MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED
374 * The hardware accelerator failed.
375 */
gabor-mezei-arm04087df2021-09-27 16:29:52 +0200376int mbedtls_cf_hmac( mbedtls_md_context_t *ctx,
377 const unsigned char *add_data,
378 size_t add_data_len,
379 const unsigned char *data,
380 size_t data_len_secret,
381 size_t min_data_len,
382 size_t max_data_len,
383 unsigned char *output );
gabor-mezei-armcb4317b2021-09-27 14:28:31 +0200384
385#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
gabor-mezei-armf52941e2021-09-27 16:11:12 +0200386
387#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
388
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200389/** This function performs the unpadding part of a PKCS#1 v1.5 decryption
390 * operation (RSAES-PKCS1-v1_5-DECRYPT).
391 *
392 * \note The output buffer length \c output_max_len should be
393 * as large as the size \p ctx->len of \p ctx->N, for example,
394 * 128 Bytes if RSA-1024 is used, to be able to hold an
395 * arbitrary decrypted message. If it is not large enough to
396 * hold the decryption of the particular ciphertext provided,
397 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
398 *
399 * \param mode The mode of operation. This must be either
400 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
401 * \param ilen The length of the ciphertext.
402 * \param olen The address at which to store the length of
403 * the plaintext. This must not be \c NULL.
404 * \param output The buffer used to hold the plaintext. This must
405 * be a writable buffer of length \p output_max_len Bytes.
406 * \param output_max_len The length in Bytes of the output buffer \p output.
407 * \param buf The input buffer for the unpadding operation.
408 *
409 * \return \c 0 on success.
410 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
411 */
gabor-mezei-armf52941e2021-09-27 16:11:12 +0200412int mbedtls_cf_rsaes_pkcs1_v15_unpadding( int mode,
413 size_t ilen,
414 size_t *olen,
415 unsigned char *output,
416 size_t output_max_len,
417 unsigned char *buf );
418
419#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */