blob: 6dfac8b3fb2fe243c62ed90229aa2e9e655d6c5f [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
20#include "common.h"
21
gabor-mezei-arm3f90fd52021-09-27 12:55:33 +020022#if defined(MBEDTLS_BIGNUM_C)
23#include "mbedtls/bignum.h"
24#endif
25
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +020026#if defined(MBEDTLS_SSL_TLS_C)
27#include "ssl_misc.h"
28#endif
29
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +020030#include <stddef.h>
31
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +020032
gabor-mezei-arm90d96cc2021-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 *
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +020041 * \param a Pointer to the first buffer.
42 * \param b Pointer to the second buffer.
43 * \param n The number of bytes to compare in the buffer.
44 *
45 * \return Zero if the content of the two buffer is the same,
46 * otherwise non-zero.
47 */
gabor-mezei-arm46025642021-07-19 15:19:19 +020048int mbedtls_cf_memcmp( const void *a,
49 const void *b,
50 size_t n );
gabor-mezei-arm340948e2021-09-27 11:40:03 +020051
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +020052/** Turn a value into a mask:
53 * - if \p value == 0, return the all-bits 0 mask, aka 0
Gabor Mezeia316fc82021-10-18 16:28:27 +020054 * - otherwise, return the all-bits 1 mask, aka (unsigned) -1
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +020055 *
56 * This function can be used to write constant-time code by replacing branches
57 * with bit operations using masks.
58 *
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +020059 * \param value The value to analyze.
60 *
61 * \return Zero if \p value is zero, otherwise all-bits-one.
62 */
gabor-mezei-arm340948e2021-09-27 11:40:03 +020063unsigned mbedtls_cf_uint_mask( unsigned value );
gabor-mezei-arm3733bf82021-09-27 11:49:42 +020064
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +020065/** Turn a value into a mask:
66 * - if \p value == 0, return the all-bits 0 mask, aka 0
67 * - otherwise, return the all-bits 1 mask, aka (size_t) -1
68 *
69 * This function can be used to write constant-time code by replacing branches
70 * with bit operations using masks.
71 *
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +020072 * \param value The value to analyze.
73 *
74 * \return Zero if \p value is zero, otherwise all-bits-one.
75 */
gabor-mezei-arm396438c2021-08-10 20:56:21 +020076size_t mbedtls_cf_size_mask( size_t value );
gabor-mezei-armc76227d2021-09-27 11:53:54 +020077
gabor-mezei-arm9cb55692021-08-11 15:07:02 +020078#if defined(MBEDTLS_BIGNUM_C)
79
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +020080/** Turn a value into a mask:
81 * - if \p value == 0, return the all-bits 0 mask, aka 0
Gabor Mezeia316fc82021-10-18 16:28:27 +020082 * - otherwise, return the all-bits 1 mask, aka (mbedtls_mpi_uint) -1
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +020083 *
84 * This function can be used to write constant-time code by replacing branches
85 * with bit operations using masks.
86 *
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +020087 * \param value The value to analyze.
88 *
89 * \return Zero if \p value is zero, otherwise all-bits-one.
90 */
gabor-mezei-arm9cb55692021-08-11 15:07:02 +020091mbedtls_mpi_uint mbedtls_cf_mpi_uint_mask( mbedtls_mpi_uint value );
92
93#endif /* MBEDTLS_BIGNUM_C */
94
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +020095/** Constant-flow mask generation for "greater or equal" comparison:
96 * - if \p x >= \p y, return all-bits 1, that is (size_t) -1
97 * - otherwise, return all bits 0, that is 0
98 *
99 * This function can be used to write constant-time code by replacing branches
100 * with bit operations using masks.
101 *
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200102 * \param x The first value to analyze.
103 * \param y The second value to analyze.
104 *
105 * \return All-bits-one if \p x is greater or equal than \p y,
106 * otherwise zero.
107 */
gabor-mezei-arm2dcd7682021-09-27 16:29:52 +0200108size_t mbedtls_cf_size_mask_ge( size_t x,
109 size_t y );
gabor-mezei-arm8d1d5fd2021-09-27 12:15:19 +0200110
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200111/** Constant-flow boolean "equal" comparison:
112 * return x == y
113 *
114 * This is equivalent to \p x == \p y, but is likely to be compiled
115 * to code using bitwise operation rather than a branch.
116 *
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200117 * \param x The first value to analyze.
118 * \param y The second value to analyze.
119 *
120 * \return 1 if \p x equals to \p y, otherwise 0.
121 */
gabor-mezei-armb11a56e2021-08-11 17:28:49 +0200122unsigned mbedtls_cf_size_bool_eq( size_t x,
123 size_t y );
gabor-mezei-arm5a854422021-09-27 12:25:07 +0200124
gabor-mezei-arm3f90fd52021-09-27 12:55:33 +0200125
126#if defined(MBEDTLS_BIGNUM_C)
127
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200128/** Decide if an integer is less than the other, without branches.
129 *
130 * This is equivalent to \p x < \p y, but is likely to be compiled
131 * to code using bitwise operation rather than a branch.
132 *
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200133 * \param x The first value to analyze.
134 * \param y The second value to analyze.
135 *
136 * \return 1 if \p x is less than \p y, otherwise 0.
137 */
gabor-mezei-arm3f90fd52021-09-27 12:55:33 +0200138unsigned mbedtls_cf_mpi_uint_lt( const mbedtls_mpi_uint x,
139 const mbedtls_mpi_uint y );
140
141#endif /* MBEDTLS_BIGNUM_C */
gabor-mezei-armb2dbf2c2021-09-27 12:59:30 +0200142
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200143/** Choose between two integer values without branches.
144 *
145 * This is equivalent to `condition ? if1 : if0`, but is likely to be compiled
146 * to code using bitwise operation rather than a branch.
147 *
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200148 * \param condition Condition to test.
149 * \param if1 Value to use if \p condition is nonzero.
150 * \param if0 Value to use if \p condition is zero.
151 *
152 * \return \c if1 if \p condition is nonzero, otherwise \c if0.
153 */
gabor-mezei-arm87ac5be2021-08-10 20:36:09 +0200154unsigned mbedtls_cf_uint_if( unsigned condition,
gabor-mezei-arm2dcd7682021-09-27 16:29:52 +0200155 unsigned if1,
156 unsigned if0 );
gabor-mezei-armb2dbf2c2021-09-27 12:59:30 +0200157
gabor-mezei-armbe8d98b2021-09-27 13:17:15 +0200158#if defined(MBEDTLS_BIGNUM_C)
159
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200160/** Conditionally assign a value without branches.
161 *
162 * This is equivalent to `if ( condition ) dest = src`, but is likely
163 * to be compiled to code using bitwise operation rather than a branch.
164 *
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200165 * \param n \p dest and \p src must be arrays of limbs of size n.
166 * \param dest The MPI to conditionally assign to. This must point
167 * to an initialized MPI.
168 * \param src The MPI to be assigned from. This must point to an
169 * initialized MPI.
170 * \param condition Condition to test, must be 0 or 1.
171 */
gabor-mezei-armbe8d98b2021-09-27 13:17:15 +0200172void mbedtls_cf_mpi_uint_cond_assign( size_t n,
173 mbedtls_mpi_uint *dest,
174 const mbedtls_mpi_uint *src,
gabor-mezei-arm87ac5be2021-08-10 20:36:09 +0200175 unsigned char condition );
gabor-mezei-armbe8d98b2021-09-27 13:17:15 +0200176
177#endif /* MBEDTLS_BIGNUM_C */
gabor-mezei-arm394aeaa2021-09-27 13:31:06 +0200178
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200179
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200180/** Conditional memcpy without branches.
181 *
182 * This is equivalent to `if ( c1 == c2 ) memcpy(dst, src, len)`, but is likely
183 * to be compiled to code using bitwise operation rather than a branch.
184 *
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200185 * \param dest The pointer to conditionally copy to.
Gabor Mezeia316fc82021-10-18 16:28:27 +0200186 * \param src The pointer to copy from. Shouldn't overlap with \p dest.
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200187 * \param len The number of bytes to copy.
188 * \param c1 The first value to analyze in the condition.
189 * \param c2 The second value to analyze in the condition.
190 */
gabor-mezei-arm87ac5be2021-08-10 20:36:09 +0200191void mbedtls_cf_memcpy_if_eq( unsigned char *dest,
gabor-mezei-armdee0fd32021-09-27 13:34:25 +0200192 const unsigned char *src,
193 size_t len,
194 size_t c1, size_t c2 );
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200195
196/** Copy data from a secret position with constant flow.
197 *
198 * This function copies \p len bytes from \p src_base + \p offset_secret to \p
199 * dst, with a code flow and memory access pattern that does not depend on \p
200 * offset_secret, but only on \p offset_min, \p offset_max and \p len.
Gabor Mezeia316fc82021-10-18 16:28:27 +0200201 * Functionally equivalent to `memcpy(dst, src + offset_secret, len)`.
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200202 *
Gabor Mezei63bbba52021-10-18 16:17:57 +0200203 * \param dest The destination buffer. This must point to a writable
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200204 * buffer of at least \p len bytes.
Gabor Mezei63bbba52021-10-18 16:17:57 +0200205 * \param src The base of the source buffer. This must point to a
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200206 * readable buffer of at least \p offset_max + \p len
Gabor Mezei63bbba52021-10-18 16:17:57 +0200207 * bytes. Shouldn't overlap with \p dest.
208 * \param offset The offset in the source buffer from which to copy.
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200209 * This must be no less than \p offset_min and no greater
210 * than \p offset_max.
Gabor Mezei63bbba52021-10-18 16:17:57 +0200211 * \param offset_min The minimal value of \p offset.
212 * \param offset_max The maximal value of \p offset.
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200213 * \param len The number of bytes to copy.
214 */
Gabor Mezei63bbba52021-10-18 16:17:57 +0200215void mbedtls_cf_memcpy_offset( unsigned char *dest,
216 const unsigned char *src,
217 size_t offset,
gabor-mezei-arm2dcd7682021-09-27 16:29:52 +0200218 size_t offset_min,
219 size_t offset_max,
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200220 size_t len );
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +0200221
222#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
223
224/** Compute the HMAC of variable-length data with constant flow.
225 *
226 * This function computes the HMAC of the concatenation of \p add_data and \p
227 * data, and does with a code flow and memory access pattern that does not
228 * depend on \p data_len_secret, but only on \p min_data_len and \p
229 * max_data_len. In particular, this function always reads exactly \p
230 * max_data_len bytes from \p data.
231 *
232 * \param ctx The HMAC context. It must have keys configured
233 * with mbedtls_md_hmac_starts() and use one of the
234 * following hashes: SHA-384, SHA-256, SHA-1 or MD-5.
235 * It is reset using mbedtls_md_hmac_reset() after
236 * the computation is complete to prepare for the
237 * next computation.
Gabor Mezeia316fc82021-10-18 16:28:27 +0200238 * \param add_data The first part of the message whose HMAC is being
239 * calculated. This must point to a readable buffer
240 * of \p add_data_len bytes.
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +0200241 * \param add_data_len The length of \p add_data in bytes.
Gabor Mezeia316fc82021-10-18 16:28:27 +0200242 * \param data The buffer containing the second part of the
243 * message. This must point to a readable buffer
244 * of \p max_data_len bytes.
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +0200245 * \param data_len_secret The length of the data to process in \p data.
246 * This must be no less than \p min_data_len and no
247 * greater than \p max_data_len.
Gabor Mezeia316fc82021-10-18 16:28:27 +0200248 * \param min_data_len The minimal length of the second part of the
249 * message, read from /p data.
250 * \param max_data_len The maximal length of the second part of the
251 * message, read from /p data.
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +0200252 * \param output The HMAC will be written here. This must point to
253 * a writable buffer of sufficient size to hold the
254 * HMAC value.
255 *
256 * \retval 0 on success.
Gabor Mezeia316fc82021-10-18 16:28:27 +0200257 * \retval #MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +0200258 * The hardware accelerator failed.
259 */
gabor-mezei-arm2dcd7682021-09-27 16:29:52 +0200260int mbedtls_cf_hmac( mbedtls_md_context_t *ctx,
261 const unsigned char *add_data,
262 size_t add_data_len,
263 const unsigned char *data,
264 size_t data_len_secret,
265 size_t min_data_len,
266 size_t max_data_len,
267 unsigned char *output );
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +0200268
269#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
gabor-mezei-armfdb71182021-09-27 16:11:12 +0200270
271#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
272
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200273/** This function performs the unpadding part of a PKCS#1 v1.5 decryption
Gabor Mezeia316fc82021-10-18 16:28:27 +0200274 * operation (EME-PKCS1-v1_5 decoding).
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200275 *
Gabor Mezeia316fc82021-10-18 16:28:27 +0200276 * \note The return value from this function is a sensitive value
277 * (this is unusual). #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE shouldn't happen
278 * in a well-written application, but 0 vs #MBEDTLS_ERR_RSA_INVALID_PADDING
279 * is often a situation that an attacker can provoke and leaking which
280 * one is the result is precisely the information the attacker wants.
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200281 *
Gabor Mezeia316fc82021-10-18 16:28:27 +0200282 * \param input The input buffer which is the payload inside PKCS#1v1.5
283 * encryption padding, called the "encoded message EM"
284 * by the terminology.
285 * \param ilen The length of the payload in the \p input buffer.
286 * \param output The buffer for the payload, called "message M" by the
287 * PKCS#1 terminology. This must be a writable buffer of
288 * length \p output_max_len bytes.
Gabor Mezei63bbba52021-10-18 16:17:57 +0200289 * \param olen The address at which to store the length of
Gabor Mezeia316fc82021-10-18 16:28:27 +0200290 * the payload. This must not be \c NULL.
291 * \param output_max_len The length in bytes of the output buffer \p output.
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200292 *
Gabor Mezeia316fc82021-10-18 16:28:27 +0200293 * \return \c 0 on success.
294 * \return #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE
295 * The output buffer is too small for the unpadded payload.
296 * \return #MBEDTLS_ERR_RSA_INVALID_PADDING
297 * The input doesn't contain properly formatted padding.
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200298 */
Gabor Mezei63bbba52021-10-18 16:17:57 +0200299int mbedtls_cf_rsaes_pkcs1_v15_unpadding( unsigned char *input,
300 size_t ilen,
gabor-mezei-armfdb71182021-09-27 16:11:12 +0200301 unsigned char *output,
302 size_t output_max_len,
Gabor Mezei63bbba52021-10-18 16:17:57 +0200303 size_t *olen );
gabor-mezei-armfdb71182021-09-27 16:11:12 +0200304
305#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */