blob: 010cfad9dbb7e644743fcba2d2dfa4b6e61db355 [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/** Constant-time buffer comparison without branches.
37 *
38 * This is equivalent to the standard memncmp function, but is likely to be
39 * compiled to code using bitwise operation rather than a branch.
40 *
41 * This function can be used to write constant-time code by replacing branches
42 * with bit operations using masks.
43 *
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +020044 * \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-arm46025642021-07-19 15:19:19 +020051int mbedtls_cf_memcmp( const void *a,
52 const void *b,
53 size_t n );
gabor-mezei-arm340948e2021-09-27 11:40:03 +020054
gabor-mezei-arm90d96cc2021-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
Gabor Mezeia316fc82021-10-18 16:28:27 +020057 * - otherwise, return the all-bits 1 mask, aka (unsigned) -1
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +020058 *
59 * This function can be used to write constant-time code by replacing branches
60 * with bit operations using masks.
61 *
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +020062 * \param value The value to analyze.
63 *
64 * \return Zero if \p value is zero, otherwise all-bits-one.
65 */
gabor-mezei-arm340948e2021-09-27 11:40:03 +020066unsigned mbedtls_cf_uint_mask( unsigned value );
gabor-mezei-arm3733bf82021-09-27 11:49:42 +020067
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
70 * - otherwise, return the all-bits 1 mask, aka (size_t) -1
71 *
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-mezei-arm396438c2021-08-10 20:56:21 +020079size_t mbedtls_cf_size_mask( size_t value );
gabor-mezei-armc76227d2021-09-27 11:53:54 +020080
gabor-mezei-arm9cb55692021-08-11 15:07:02 +020081#if defined(MBEDTLS_BIGNUM_C)
82
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +020083/** Turn a value into a mask:
84 * - if \p value == 0, return the all-bits 0 mask, aka 0
Gabor Mezeia316fc82021-10-18 16:28:27 +020085 * - otherwise, return the all-bits 1 mask, aka (mbedtls_mpi_uint) -1
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +020086 *
87 * This function can be used to write constant-time code by replacing branches
88 * with bit operations using masks.
89 *
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +020090 * \param value The value to analyze.
91 *
92 * \return Zero if \p value is zero, otherwise all-bits-one.
93 */
gabor-mezei-arm9cb55692021-08-11 15:07:02 +020094mbedtls_mpi_uint mbedtls_cf_mpi_uint_mask( mbedtls_mpi_uint value );
95
96#endif /* MBEDTLS_BIGNUM_C */
97
Gabor Mezeie2123792021-10-18 17:05:06 +020098#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
99
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200100/** Constant-flow mask generation for "greater or equal" comparison:
101 * - if \p x >= \p y, return all-bits 1, that is (size_t) -1
102 * - otherwise, return all bits 0, that is 0
103 *
104 * This function can be used to write constant-time code by replacing branches
105 * with bit operations using masks.
106 *
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200107 * \param x The first value to analyze.
108 * \param y The second value to analyze.
109 *
110 * \return All-bits-one if \p x is greater or equal than \p y,
111 * otherwise zero.
112 */
gabor-mezei-arm2dcd7682021-09-27 16:29:52 +0200113size_t mbedtls_cf_size_mask_ge( size_t x,
114 size_t y );
gabor-mezei-arm8d1d5fd2021-09-27 12:15:19 +0200115
Gabor Mezeie2123792021-10-18 17:05:06 +0200116#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
117
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200118/** Constant-flow boolean "equal" comparison:
119 * return x == y
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 equals to \p y, otherwise 0.
128 */
gabor-mezei-armb11a56e2021-08-11 17:28:49 +0200129unsigned mbedtls_cf_size_bool_eq( size_t x,
130 size_t y );
gabor-mezei-arm5a854422021-09-27 12:25:07 +0200131
gabor-mezei-arm3f90fd52021-09-27 12:55:33 +0200132#if defined(MBEDTLS_BIGNUM_C)
133
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200134/** Decide if an integer is less than the other, without branches.
135 *
136 * This is equivalent to \p x < \p y, 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 x The first value to analyze.
140 * \param y The second value to analyze.
141 *
142 * \return 1 if \p x is less than \p y, otherwise 0.
143 */
gabor-mezei-arm3f90fd52021-09-27 12:55:33 +0200144unsigned mbedtls_cf_mpi_uint_lt( const mbedtls_mpi_uint x,
145 const mbedtls_mpi_uint y );
146
147#endif /* MBEDTLS_BIGNUM_C */
gabor-mezei-armb2dbf2c2021-09-27 12:59:30 +0200148
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200149/** Choose between two integer values without branches.
150 *
151 * This is equivalent to `condition ? if1 : if0`, but is likely to be compiled
152 * to code using bitwise operation rather than a branch.
153 *
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200154 * \param condition Condition to test.
155 * \param if1 Value to use if \p condition is nonzero.
156 * \param if0 Value to use if \p condition is zero.
157 *
158 * \return \c if1 if \p condition is nonzero, otherwise \c if0.
159 */
gabor-mezei-arm87ac5be2021-08-10 20:36:09 +0200160unsigned mbedtls_cf_uint_if( unsigned condition,
gabor-mezei-arm2dcd7682021-09-27 16:29:52 +0200161 unsigned if1,
162 unsigned if0 );
gabor-mezei-armb2dbf2c2021-09-27 12:59:30 +0200163
gabor-mezei-armbe8d98b2021-09-27 13:17:15 +0200164#if defined(MBEDTLS_BIGNUM_C)
165
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200166/** Conditionally assign a value without branches.
167 *
168 * This is equivalent to `if ( condition ) dest = src`, but is likely
169 * to be compiled to code using bitwise operation rather than a branch.
170 *
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200171 * \param n \p dest and \p src must be arrays of limbs of size n.
172 * \param dest The MPI to conditionally assign to. This must point
173 * to an initialized MPI.
174 * \param src The MPI to be assigned from. This must point to an
175 * initialized MPI.
176 * \param condition Condition to test, must be 0 or 1.
177 */
gabor-mezei-armbe8d98b2021-09-27 13:17:15 +0200178void mbedtls_cf_mpi_uint_cond_assign( size_t n,
179 mbedtls_mpi_uint *dest,
180 const mbedtls_mpi_uint *src,
gabor-mezei-arm87ac5be2021-08-10 20:36:09 +0200181 unsigned char condition );
gabor-mezei-armbe8d98b2021-09-27 13:17:15 +0200182
183#endif /* MBEDTLS_BIGNUM_C */
gabor-mezei-arm394aeaa2021-09-27 13:31:06 +0200184
Gabor Mezeie2123792021-10-18 17:05:06 +0200185#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200186
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200187/** Conditional memcpy without branches.
188 *
189 * This is equivalent to `if ( c1 == c2 ) memcpy(dst, src, len)`, but is likely
190 * to be compiled to code using bitwise operation rather than a branch.
191 *
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200192 * \param dest The pointer to conditionally copy to.
Gabor Mezeia316fc82021-10-18 16:28:27 +0200193 * \param src The pointer to copy from. Shouldn't overlap with \p dest.
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200194 * \param len The number of bytes to copy.
195 * \param c1 The first value to analyze in the condition.
196 * \param c2 The second value to analyze in the condition.
197 */
gabor-mezei-arm87ac5be2021-08-10 20:36:09 +0200198void mbedtls_cf_memcpy_if_eq( unsigned char *dest,
gabor-mezei-armdee0fd32021-09-27 13:34:25 +0200199 const unsigned char *src,
200 size_t len,
201 size_t c1, size_t c2 );
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200202
203/** Copy data from a secret position with constant flow.
204 *
205 * This function copies \p len bytes from \p src_base + \p offset_secret to \p
206 * dst, with a code flow and memory access pattern that does not depend on \p
207 * offset_secret, but only on \p offset_min, \p offset_max and \p len.
Gabor Mezeia316fc82021-10-18 16:28:27 +0200208 * Functionally equivalent to `memcpy(dst, src + offset_secret, len)`.
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200209 *
Gabor Mezei63bbba52021-10-18 16:17:57 +0200210 * \param dest The destination buffer. This must point to a writable
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200211 * buffer of at least \p len bytes.
Gabor Mezei63bbba52021-10-18 16:17:57 +0200212 * \param src The base of the source buffer. This must point to a
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200213 * readable buffer of at least \p offset_max + \p len
Gabor Mezei63bbba52021-10-18 16:17:57 +0200214 * bytes. Shouldn't overlap with \p dest.
215 * \param offset The offset in the source buffer from which to copy.
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200216 * This must be no less than \p offset_min and no greater
217 * than \p offset_max.
Gabor Mezei63bbba52021-10-18 16:17:57 +0200218 * \param offset_min The minimal value of \p offset.
219 * \param offset_max The maximal value of \p offset.
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200220 * \param len The number of bytes to copy.
221 */
Gabor Mezei63bbba52021-10-18 16:17:57 +0200222void mbedtls_cf_memcpy_offset( unsigned char *dest,
223 const unsigned char *src,
224 size_t offset,
gabor-mezei-arm2dcd7682021-09-27 16:29:52 +0200225 size_t offset_min,
226 size_t offset_max,
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200227 size_t len );
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +0200228
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +0200229/** Compute the HMAC of variable-length data with constant flow.
230 *
231 * This function computes the HMAC of the concatenation of \p add_data and \p
232 * data, and does with a code flow and memory access pattern that does not
233 * depend on \p data_len_secret, but only on \p min_data_len and \p
234 * max_data_len. In particular, this function always reads exactly \p
235 * max_data_len bytes from \p data.
236 *
237 * \param ctx The HMAC context. It must have keys configured
238 * with mbedtls_md_hmac_starts() and use one of the
239 * following hashes: SHA-384, SHA-256, SHA-1 or MD-5.
240 * It is reset using mbedtls_md_hmac_reset() after
241 * the computation is complete to prepare for the
242 * next computation.
Gabor Mezeia316fc82021-10-18 16:28:27 +0200243 * \param add_data The first part of the message whose HMAC is being
244 * calculated. This must point to a readable buffer
245 * of \p add_data_len bytes.
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +0200246 * \param add_data_len The length of \p add_data in bytes.
Gabor Mezeia316fc82021-10-18 16:28:27 +0200247 * \param data The buffer containing the second part of the
248 * message. This must point to a readable buffer
249 * of \p max_data_len bytes.
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +0200250 * \param data_len_secret The length of the data to process in \p data.
251 * This must be no less than \p min_data_len and no
252 * greater than \p max_data_len.
Gabor Mezeia316fc82021-10-18 16:28:27 +0200253 * \param min_data_len The minimal length of the second part of the
254 * message, read from /p data.
255 * \param max_data_len The maximal length of the second part of the
256 * message, read from /p data.
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +0200257 * \param output The HMAC will be written here. This must point to
258 * a writable buffer of sufficient size to hold the
259 * HMAC value.
260 *
261 * \retval 0 on success.
Gabor Mezeia316fc82021-10-18 16:28:27 +0200262 * \retval #MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +0200263 * The hardware accelerator failed.
264 */
gabor-mezei-arm2dcd7682021-09-27 16:29:52 +0200265int mbedtls_cf_hmac( mbedtls_md_context_t *ctx,
266 const unsigned char *add_data,
267 size_t add_data_len,
268 const unsigned char *data,
269 size_t data_len_secret,
270 size_t min_data_len,
271 size_t max_data_len,
272 unsigned char *output );
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +0200273
274#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
gabor-mezei-armfdb71182021-09-27 16:11:12 +0200275
276#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
277
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200278/** This function performs the unpadding part of a PKCS#1 v1.5 decryption
Gabor Mezeia316fc82021-10-18 16:28:27 +0200279 * operation (EME-PKCS1-v1_5 decoding).
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200280 *
Gabor Mezeia316fc82021-10-18 16:28:27 +0200281 * \note The return value from this function is a sensitive value
282 * (this is unusual). #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE shouldn't happen
283 * in a well-written application, but 0 vs #MBEDTLS_ERR_RSA_INVALID_PADDING
284 * is often a situation that an attacker can provoke and leaking which
285 * one is the result is precisely the information the attacker wants.
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200286 *
Gabor Mezeia316fc82021-10-18 16:28:27 +0200287 * \param input The input buffer which is the payload inside PKCS#1v1.5
288 * encryption padding, called the "encoded message EM"
289 * by the terminology.
290 * \param ilen The length of the payload in the \p input buffer.
291 * \param output The buffer for the payload, called "message M" by the
292 * PKCS#1 terminology. This must be a writable buffer of
293 * length \p output_max_len bytes.
Gabor Mezei63bbba52021-10-18 16:17:57 +0200294 * \param olen The address at which to store the length of
Gabor Mezeia316fc82021-10-18 16:28:27 +0200295 * the payload. This must not be \c NULL.
296 * \param output_max_len The length in bytes of the output buffer \p output.
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200297 *
Gabor Mezeia316fc82021-10-18 16:28:27 +0200298 * \return \c 0 on success.
299 * \return #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE
300 * The output buffer is too small for the unpadded payload.
301 * \return #MBEDTLS_ERR_RSA_INVALID_PADDING
302 * The input doesn't contain properly formatted padding.
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200303 */
Gabor Mezei63bbba52021-10-18 16:17:57 +0200304int mbedtls_cf_rsaes_pkcs1_v15_unpadding( unsigned char *input,
305 size_t ilen,
gabor-mezei-armfdb71182021-09-27 16:11:12 +0200306 unsigned char *output,
307 size_t output_max_len,
Gabor Mezei63bbba52021-10-18 16:17:57 +0200308 size_t *olen );
gabor-mezei-armfdb71182021-09-27 16:11:12 +0200309
310#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */
Gabor Mezei291df7b2021-10-19 11:27:17 +0200311
312#endif /* MBEDTLS_CONSTANT_TIME_INTERNAL_H */