blob: 7255b2a857b27d087777caca840009d221afbb40 [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 */
Gabor Mezei90437e32021-10-20 11:59:27 +020047unsigned mbedtls_ct_uint_mask( unsigned value );
gabor-mezei-arm3733bf82021-09-27 11:49:42 +020048
Gabor Mezei6a426c92021-10-20 11:17:43 +020049#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
50
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 */
Gabor Mezei90437e32021-10-20 11:59:27 +020062size_t mbedtls_ct_size_mask( size_t value );
gabor-mezei-armc76227d2021-09-27 11:53:54 +020063
Gabor Mezei6a426c92021-10-20 11:17:43 +020064#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
65
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 */
Gabor Mezei90437e32021-10-20 11:59:27 +020079mbedtls_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 */
Gabor Mezei90437e32021-10-20 11:59:27 +020098size_t mbedtls_ct_size_mask_ge( size_t x,
gabor-mezei-arm2dcd7682021-09-27 16:29:52 +020099 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 */
Gabor Mezei90437e32021-10-20 11:59:27 +0200114unsigned mbedtls_ct_size_bool_eq( size_t x,
gabor-mezei-armb11a56e2021-08-11 17:28:49 +0200115 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 */
Gabor Mezei90437e32021-10-20 11:59:27 +0200129unsigned mbedtls_ct_mpi_uint_lt( const mbedtls_mpi_uint x,
gabor-mezei-arm3f90fd52021-09-27 12:55:33 +0200130 const mbedtls_mpi_uint y );
131
Janos Follath23bdeca2022-07-22 18:24:06 +0100132/**
133 * \brief Check if an MPI is less than the other in constant time.
134 *
135 * \param X The left-hand MPI. This must point to an array of limbs
136 * with the same allocated length as Y.
137 * \param Y The right-hand MPI. This must point to an array of limbs
138 * with the same allocated length as X.
139 * \param len The number of limbs in X and Y.
140 *
141 * \return The result of the comparison:
142 * \c 1 if \p X is less than \p Y.
143 * \c 0 if \p X is greater than or equal to \p Y.
144 */
145unsigned mbedtls_mpi_core_lt_ct( const mbedtls_mpi_uint *X,
146 const mbedtls_mpi_uint *Y,
147 size_t len );
gabor-mezei-arm3f90fd52021-09-27 12:55:33 +0200148#endif /* MBEDTLS_BIGNUM_C */
gabor-mezei-armb2dbf2c2021-09-27 12:59:30 +0200149
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200150/** Choose between two integer values without branches.
151 *
152 * This is equivalent to `condition ? if1 : if0`, but is likely to be compiled
153 * to code using bitwise operation rather than a branch.
154 *
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200155 * \param condition Condition to test.
156 * \param if1 Value to use if \p condition is nonzero.
157 * \param if0 Value to use if \p condition is zero.
158 *
159 * \return \c if1 if \p condition is nonzero, otherwise \c if0.
160 */
Gabor Mezei90437e32021-10-20 11:59:27 +0200161unsigned mbedtls_ct_uint_if( unsigned condition,
gabor-mezei-arm2dcd7682021-09-27 16:29:52 +0200162 unsigned if1,
163 unsigned if0 );
gabor-mezei-armb2dbf2c2021-09-27 12:59:30 +0200164
gabor-mezei-armbe8d98b2021-09-27 13:17:15 +0200165#if defined(MBEDTLS_BIGNUM_C)
166
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200167/** Conditionally assign a value without branches.
168 *
169 * This is equivalent to `if ( condition ) dest = src`, but is likely
170 * to be compiled to code using bitwise operation rather than a branch.
171 *
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200172 * \param n \p dest and \p src must be arrays of limbs of size n.
173 * \param dest The MPI to conditionally assign to. This must point
174 * to an initialized MPI.
175 * \param src The MPI to be assigned from. This must point to an
176 * initialized MPI.
177 * \param condition Condition to test, must be 0 or 1.
178 */
Gabor Mezei90437e32021-10-20 11:59:27 +0200179void mbedtls_ct_mpi_uint_cond_assign( size_t n,
gabor-mezei-armbe8d98b2021-09-27 13:17:15 +0200180 mbedtls_mpi_uint *dest,
181 const mbedtls_mpi_uint *src,
gabor-mezei-arm87ac5be2021-08-10 20:36:09 +0200182 unsigned char condition );
gabor-mezei-armbe8d98b2021-09-27 13:17:15 +0200183
184#endif /* MBEDTLS_BIGNUM_C */
gabor-mezei-arm394aeaa2021-09-27 13:31:06 +0200185
Gabor Mezei9a4074a2021-11-15 16:18:54 +0100186#if defined(MBEDTLS_BASE64_C)
187
Gabor Mezeia0969752021-11-24 16:26:33 +0100188/** Given a value in the range 0..63, return the corresponding Base64 digit.
189 *
190 * The implementation assumes that letters are consecutive (e.g. ASCII
191 * but not EBCDIC).
192 *
193 * \param value A value in the range 0..63.
194 *
195 * \return A base64 digit converted from \p value.
196 */
Gabor Mezei14d5fac2021-11-24 15:51:39 +0100197unsigned char mbedtls_ct_base64_enc_char( unsigned char value );
Gabor Mezei9a4074a2021-11-15 16:18:54 +0100198
Gabor Mezeia0969752021-11-24 16:26:33 +0100199/** Given a Base64 digit, return its value.
200 *
201 * If c is not a Base64 digit ('A'..'Z', 'a'..'z', '0'..'9', '+' or '/'),
202 * return -1.
203 *
204 * The implementation assumes that letters are consecutive (e.g. ASCII
205 * but not EBCDIC).
206 *
207 * \param c A base64 digit.
208 *
209 * \return The value of the base64 digit \p c.
210 */
Gabor Mezei358829a2021-11-15 16:22:37 +0100211signed char mbedtls_ct_base64_dec_value( unsigned char c );
212
Gabor Mezei9a4074a2021-11-15 16:18:54 +0100213#endif /* MBEDTLS_BASE64_C */
214
Gabor Mezeie2123792021-10-18 17:05:06 +0200215#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200216
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200217/** Conditional memcpy without branches.
218 *
Gabor Mezei642eeb22021-11-03 16:13:32 +0100219 * This is equivalent to `if ( c1 == c2 ) memcpy(dest, src, len)`, but is likely
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200220 * to be compiled to code using bitwise operation rather than a branch.
221 *
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200222 * \param dest The pointer to conditionally copy to.
Gabor Mezeia316fc82021-10-18 16:28:27 +0200223 * \param src The pointer to copy from. Shouldn't overlap with \p dest.
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200224 * \param len The number of bytes to copy.
225 * \param c1 The first value to analyze in the condition.
226 * \param c2 The second value to analyze in the condition.
227 */
Gabor Mezei90437e32021-10-20 11:59:27 +0200228void mbedtls_ct_memcpy_if_eq( unsigned char *dest,
gabor-mezei-armdee0fd32021-09-27 13:34:25 +0200229 const unsigned char *src,
230 size_t len,
231 size_t c1, size_t c2 );
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200232
233/** Copy data from a secret position with constant flow.
234 *
235 * This function copies \p len bytes from \p src_base + \p offset_secret to \p
236 * dst, with a code flow and memory access pattern that does not depend on \p
237 * offset_secret, but only on \p offset_min, \p offset_max and \p len.
Gabor Mezeia316fc82021-10-18 16:28:27 +0200238 * Functionally equivalent to `memcpy(dst, src + offset_secret, len)`.
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200239 *
Paul Elliott5260ce22022-05-09 18:15:54 +0100240 * \note This function reads from \p dest, but the value that
241 * is read does not influence the result and this
242 * function's behavior is well-defined regardless of the
243 * contents of the buffers. This may result in false
244 * positives from static or dynamic analyzers, especially
245 * if \p dest is not initialized.
246 *
Gabor Mezei63bbba52021-10-18 16:17:57 +0200247 * \param dest The destination buffer. This must point to a writable
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200248 * buffer of at least \p len bytes.
Gabor Mezei63bbba52021-10-18 16:17:57 +0200249 * \param src The base of the source buffer. This must point to a
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200250 * readable buffer of at least \p offset_max + \p len
Gabor Mezei63bbba52021-10-18 16:17:57 +0200251 * bytes. Shouldn't overlap with \p dest.
252 * \param offset The offset in the source buffer from which to copy.
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200253 * This must be no less than \p offset_min and no greater
254 * than \p offset_max.
Gabor Mezei63bbba52021-10-18 16:17:57 +0200255 * \param offset_min The minimal value of \p offset.
256 * \param offset_max The maximal value of \p offset.
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200257 * \param len The number of bytes to copy.
258 */
Gabor Mezei90437e32021-10-20 11:59:27 +0200259void mbedtls_ct_memcpy_offset( unsigned char *dest,
Gabor Mezei63bbba52021-10-18 16:17:57 +0200260 const unsigned char *src,
261 size_t offset,
gabor-mezei-arm2dcd7682021-09-27 16:29:52 +0200262 size_t offset_min,
263 size_t offset_max,
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200264 size_t len );
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +0200265
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +0200266/** Compute the HMAC of variable-length data with constant flow.
267 *
268 * This function computes the HMAC of the concatenation of \p add_data and \p
269 * data, and does with a code flow and memory access pattern that does not
270 * depend on \p data_len_secret, but only on \p min_data_len and \p
271 * max_data_len. In particular, this function always reads exactly \p
272 * max_data_len bytes from \p data.
273 *
274 * \param ctx The HMAC context. It must have keys configured
275 * with mbedtls_md_hmac_starts() and use one of the
276 * following hashes: SHA-384, SHA-256, SHA-1 or MD-5.
277 * It is reset using mbedtls_md_hmac_reset() after
278 * the computation is complete to prepare for the
279 * next computation.
Gabor Mezeia316fc82021-10-18 16:28:27 +0200280 * \param add_data The first part of the message whose HMAC is being
281 * calculated. This must point to a readable buffer
282 * of \p add_data_len bytes.
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +0200283 * \param add_data_len The length of \p add_data in bytes.
Gabor Mezeia316fc82021-10-18 16:28:27 +0200284 * \param data The buffer containing the second part of the
285 * message. This must point to a readable buffer
286 * of \p max_data_len bytes.
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +0200287 * \param data_len_secret The length of the data to process in \p data.
288 * This must be no less than \p min_data_len and no
289 * greater than \p max_data_len.
Gabor Mezeia316fc82021-10-18 16:28:27 +0200290 * \param min_data_len The minimal length of the second part of the
Gabor Mezei116cd6a2021-10-20 11:18:37 +0200291 * message, read from \p data.
Gabor Mezeia316fc82021-10-18 16:28:27 +0200292 * \param max_data_len The maximal length of the second part of the
Gabor Mezei116cd6a2021-10-20 11:18:37 +0200293 * message, read from \p data.
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +0200294 * \param output The HMAC will be written here. This must point to
295 * a writable buffer of sufficient size to hold the
296 * HMAC value.
297 *
298 * \retval 0 on success.
Gabor Mezeia316fc82021-10-18 16:28:27 +0200299 * \retval #MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +0200300 * The hardware accelerator failed.
301 */
Neil Armstrong2968d302022-02-25 15:09:36 +0100302#if defined(MBEDTLS_USE_PSA_CRYPTO)
303int mbedtls_ct_hmac( mbedtls_svc_key_id_t key,
304 psa_algorithm_t alg,
305 const unsigned char *add_data,
306 size_t add_data_len,
307 const unsigned char *data,
308 size_t data_len_secret,
309 size_t min_data_len,
310 size_t max_data_len,
311 unsigned char *output );
312#else
Gabor Mezei90437e32021-10-20 11:59:27 +0200313int mbedtls_ct_hmac( mbedtls_md_context_t *ctx,
gabor-mezei-arm2dcd7682021-09-27 16:29:52 +0200314 const unsigned char *add_data,
315 size_t add_data_len,
316 const unsigned char *data,
317 size_t data_len_secret,
318 size_t min_data_len,
319 size_t max_data_len,
320 unsigned char *output );
Neil Armstrong2968d302022-02-25 15:09:36 +0100321#endif /* MBEDTLS_USE_PSA_CRYPTO */
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +0200322
323#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
gabor-mezei-armfdb71182021-09-27 16:11:12 +0200324
325#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
326
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200327/** This function performs the unpadding part of a PKCS#1 v1.5 decryption
Gabor Mezeia316fc82021-10-18 16:28:27 +0200328 * operation (EME-PKCS1-v1_5 decoding).
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200329 *
Gabor Mezeia316fc82021-10-18 16:28:27 +0200330 * \note The return value from this function is a sensitive value
331 * (this is unusual). #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE shouldn't happen
332 * in a well-written application, but 0 vs #MBEDTLS_ERR_RSA_INVALID_PADDING
333 * is often a situation that an attacker can provoke and leaking which
334 * one is the result is precisely the information the attacker wants.
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200335 *
Gabor Mezeia316fc82021-10-18 16:28:27 +0200336 * \param input The input buffer which is the payload inside PKCS#1v1.5
337 * encryption padding, called the "encoded message EM"
338 * by the terminology.
339 * \param ilen The length of the payload in the \p input buffer.
340 * \param output The buffer for the payload, called "message M" by the
341 * PKCS#1 terminology. This must be a writable buffer of
342 * length \p output_max_len bytes.
Gabor Mezei63bbba52021-10-18 16:17:57 +0200343 * \param olen The address at which to store the length of
Gabor Mezeia316fc82021-10-18 16:28:27 +0200344 * the payload. This must not be \c NULL.
345 * \param output_max_len The length in bytes of the output buffer \p output.
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200346 *
Gabor Mezeia316fc82021-10-18 16:28:27 +0200347 * \return \c 0 on success.
348 * \return #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE
349 * The output buffer is too small for the unpadded payload.
350 * \return #MBEDTLS_ERR_RSA_INVALID_PADDING
351 * The input doesn't contain properly formatted padding.
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200352 */
Gabor Mezei90437e32021-10-20 11:59:27 +0200353int mbedtls_ct_rsaes_pkcs1_v15_unpadding( unsigned char *input,
Gabor Mezei63bbba52021-10-18 16:17:57 +0200354 size_t ilen,
gabor-mezei-armfdb71182021-09-27 16:11:12 +0200355 unsigned char *output,
356 size_t output_max_len,
Gabor Mezei63bbba52021-10-18 16:17:57 +0200357 size_t *olen );
gabor-mezei-armfdb71182021-09-27 16:11:12 +0200358
359#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */
Gabor Mezei291df7b2021-10-19 11:27:17 +0200360
361#endif /* MBEDTLS_CONSTANT_TIME_INTERNAL_H */