blob: d78dd70f591508ee2af7c3a9fb68cfcd420e7717 [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
Gabor Mezei6e0e9902021-10-19 11:27:17 +020020#ifndef MBEDTLS_CONSTANT_TIME_INTERNAL_H
21#define MBEDTLS_CONSTANT_TIME_INTERNAL_H
22
gabor-mezei-arm944c1072021-09-27 11:28:54 +020023#include "common.h"
24
gabor-mezei-arm097d4f52021-09-27 12:55:33 +020025#if defined(MBEDTLS_BIGNUM_C)
26#include "mbedtls/bignum.h"
27#endif
28
gabor-mezei-armcb4317b2021-09-27 14:28:31 +020029#if defined(MBEDTLS_SSL_TLS_C)
30#include "mbedtls/ssl_internal.h"
31#endif
32
gabor-mezei-arm944c1072021-09-27 11:28:54 +020033#include <stddef.h>
34
gabor-mezei-arm944c1072021-09-27 11:28:54 +020035
gabor-mezei-arm7e6a1ea2021-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 Mezei4b4e4d82021-10-18 16:28:27 +020038 * - otherwise, return the all-bits 1 mask, aka (unsigned) -1
gabor-mezei-arm7e6a1ea2021-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-arm7e6a1ea2021-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-mezei-armc11cac92021-09-27 11:40:03 +020047unsigned mbedtls_cf_uint_mask( unsigned value );
gabor-mezei-armd361ccd2021-09-27 11:49:42 +020048
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +020049/** Turn a value into a mask:
50 * - if \p value == 0, return the all-bits 0 mask, aka 0
51 * - otherwise, return the all-bits 1 mask, aka (size_t) -1
52 *
53 * This function can be used to write constant-time code by replacing branches
54 * with bit operations using masks.
55 *
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +020056 * \param value The value to analyze.
57 *
58 * \return Zero if \p value is zero, otherwise all-bits-one.
59 */
gabor-mezei-arm2f2c0be2021-08-10 20:56:21 +020060size_t mbedtls_cf_size_mask( size_t value );
gabor-mezei-arm4d6b1462021-09-27 11:53:54 +020061
gabor-mezei-arm60febd52021-08-11 15:07:02 +020062#if defined(MBEDTLS_BIGNUM_C)
63
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +020064/** Turn a value into a mask:
65 * - if \p value == 0, return the all-bits 0 mask, aka 0
Gabor Mezei4b4e4d82021-10-18 16:28:27 +020066 * - otherwise, return the all-bits 1 mask, aka (mbedtls_mpi_uint) -1
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +020067 *
68 * This function can be used to write constant-time code by replacing branches
69 * with bit operations using masks.
70 *
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +020071 * \param value The value to analyze.
72 *
73 * \return Zero if \p value is zero, otherwise all-bits-one.
74 */
gabor-mezei-arm60febd52021-08-11 15:07:02 +020075mbedtls_mpi_uint mbedtls_cf_mpi_uint_mask( mbedtls_mpi_uint value );
76
77#endif /* MBEDTLS_BIGNUM_C */
78
Gabor Mezei2b358802021-10-18 17:05:06 +020079#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
80
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +020081/** Constant-flow mask generation for "greater or equal" comparison:
82 * - if \p x >= \p y, return all-bits 1, that is (size_t) -1
83 * - otherwise, return all bits 0, that is 0
84 *
85 * This function can be used to write constant-time code by replacing branches
86 * with bit operations using masks.
87 *
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +020088 * \param x The first value to analyze.
89 * \param y The second value to analyze.
90 *
91 * \return All-bits-one if \p x is greater or equal than \p y,
92 * otherwise zero.
93 */
gabor-mezei-arm04087df2021-09-27 16:29:52 +020094size_t mbedtls_cf_size_mask_ge( size_t x,
95 size_t y );
gabor-mezei-arm96584dd2021-09-27 12:15:19 +020096
Gabor Mezei2b358802021-10-18 17:05:06 +020097#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
98
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +020099/** Constant-flow boolean "equal" comparison:
100 * return x == y
101 *
102 * This is equivalent to \p x == \p y, but is likely to be compiled
103 * to code using bitwise operation rather than a branch.
104 *
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200105 * \param x The first value to analyze.
106 * \param y The second value to analyze.
107 *
108 * \return 1 if \p x equals to \p y, otherwise 0.
109 */
gabor-mezei-arm1ffd0cc2021-08-11 17:28:49 +0200110unsigned mbedtls_cf_size_bool_eq( size_t x,
111 size_t y );
gabor-mezei-arm9d7bf092021-09-27 12:25:07 +0200112
gabor-mezei-arm097d4f52021-09-27 12:55:33 +0200113#if defined(MBEDTLS_BIGNUM_C)
114
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200115/** Decide if an integer is less than the other, without branches.
116 *
117 * This is equivalent to \p x < \p y, but is likely to be compiled
118 * to code using bitwise operation rather than a branch.
119 *
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200120 * \param x The first value to analyze.
121 * \param y The second value to analyze.
122 *
123 * \return 1 if \p x is less than \p y, otherwise 0.
124 */
gabor-mezei-arm097d4f52021-09-27 12:55:33 +0200125unsigned mbedtls_cf_mpi_uint_lt( const mbedtls_mpi_uint x,
126 const mbedtls_mpi_uint y );
127
128#endif /* MBEDTLS_BIGNUM_C */
gabor-mezei-arm75332532021-09-27 12:59:30 +0200129
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200130/** Choose between two integer values without branches.
131 *
132 * This is equivalent to `condition ? if1 : if0`, but is likely to be compiled
133 * to code using bitwise operation rather than a branch.
134 *
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200135 * \param condition Condition to test.
136 * \param if1 Value to use if \p condition is nonzero.
137 * \param if0 Value to use if \p condition is zero.
138 *
139 * \return \c if1 if \p condition is nonzero, otherwise \c if0.
140 */
gabor-mezei-arm5e488242021-08-10 20:36:09 +0200141unsigned mbedtls_cf_uint_if( unsigned condition,
gabor-mezei-arm04087df2021-09-27 16:29:52 +0200142 unsigned if1,
143 unsigned if0 );
gabor-mezei-arm75332532021-09-27 12:59:30 +0200144
gabor-mezei-arm043192d2021-09-27 13:17:15 +0200145#if defined(MBEDTLS_BIGNUM_C)
146
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200147/** Conditionally assign a value without branches.
148 *
149 * This is equivalent to `if ( condition ) dest = src`, but is likely
150 * to be compiled to code using bitwise operation rather than a branch.
151 *
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200152 * \param n \p dest and \p src must be arrays of limbs of size n.
153 * \param dest The MPI to conditionally assign to. This must point
154 * to an initialized MPI.
155 * \param src The MPI to be assigned from. This must point to an
156 * initialized MPI.
157 * \param condition Condition to test, must be 0 or 1.
158 */
gabor-mezei-arm043192d2021-09-27 13:17:15 +0200159void mbedtls_cf_mpi_uint_cond_assign( size_t n,
160 mbedtls_mpi_uint *dest,
161 const mbedtls_mpi_uint *src,
gabor-mezei-arm5e488242021-08-10 20:36:09 +0200162 unsigned char condition );
gabor-mezei-arm043192d2021-09-27 13:17:15 +0200163
164#endif /* MBEDTLS_BIGNUM_C */
gabor-mezei-arm7b23c0b2021-09-27 13:31:06 +0200165
Gabor Mezei2b358802021-10-18 17:05:06 +0200166#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200167
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200168/** Conditional memcpy without branches.
169 *
170 * This is equivalent to `if ( c1 == c2 ) memcpy(dst, src, len)`, but is likely
171 * to be compiled to code using bitwise operation rather than a branch.
172 *
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200173 * \param dest The pointer to conditionally copy to.
Gabor Mezei4b4e4d82021-10-18 16:28:27 +0200174 * \param src The pointer to copy from. Shouldn't overlap with \p dest.
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200175 * \param len The number of bytes to copy.
176 * \param c1 The first value to analyze in the condition.
177 * \param c2 The second value to analyze in the condition.
178 */
gabor-mezei-arm5e488242021-08-10 20:36:09 +0200179void mbedtls_cf_memcpy_if_eq( unsigned char *dest,
gabor-mezei-armee06feb2021-09-27 13:34:25 +0200180 const unsigned char *src,
181 size_t len,
182 size_t c1, size_t c2 );
gabor-mezei-arm0f7b9e42021-09-27 13:57:45 +0200183
184/** Copy data from a secret position with constant flow.
185 *
186 * This function copies \p len bytes from \p src_base + \p offset_secret to \p
187 * dst, with a code flow and memory access pattern that does not depend on \p
188 * offset_secret, but only on \p offset_min, \p offset_max and \p len.
Gabor Mezei4b4e4d82021-10-18 16:28:27 +0200189 * Functionally equivalent to `memcpy(dst, src + offset_secret, len)`.
gabor-mezei-arm0f7b9e42021-09-27 13:57:45 +0200190 *
Gabor Mezei91deea72021-10-18 16:17:57 +0200191 * \param dest The destination buffer. This must point to a writable
gabor-mezei-arm0f7b9e42021-09-27 13:57:45 +0200192 * buffer of at least \p len bytes.
Gabor Mezei91deea72021-10-18 16:17:57 +0200193 * \param src The base of the source buffer. This must point to a
gabor-mezei-arm0f7b9e42021-09-27 13:57:45 +0200194 * readable buffer of at least \p offset_max + \p len
Gabor Mezei91deea72021-10-18 16:17:57 +0200195 * bytes. Shouldn't overlap with \p dest.
196 * \param offset The offset in the source buffer from which to copy.
gabor-mezei-arm0f7b9e42021-09-27 13:57:45 +0200197 * This must be no less than \p offset_min and no greater
198 * than \p offset_max.
Gabor Mezei91deea72021-10-18 16:17:57 +0200199 * \param offset_min The minimal value of \p offset.
200 * \param offset_max The maximal value of \p offset.
gabor-mezei-arm0f7b9e42021-09-27 13:57:45 +0200201 * \param len The number of bytes to copy.
202 */
Gabor Mezei91deea72021-10-18 16:17:57 +0200203void mbedtls_cf_memcpy_offset( unsigned char *dest,
204 const unsigned char *src,
205 size_t offset,
gabor-mezei-arm04087df2021-09-27 16:29:52 +0200206 size_t offset_min,
207 size_t offset_max,
gabor-mezei-arm0f7b9e42021-09-27 13:57:45 +0200208 size_t len );
gabor-mezei-armcb4317b2021-09-27 14:28:31 +0200209
gabor-mezei-armcb4317b2021-09-27 14:28:31 +0200210/** Compute the HMAC of variable-length data with constant flow.
211 *
212 * This function computes the HMAC of the concatenation of \p add_data and \p
213 * data, and does with a code flow and memory access pattern that does not
214 * depend on \p data_len_secret, but only on \p min_data_len and \p
215 * max_data_len. In particular, this function always reads exactly \p
216 * max_data_len bytes from \p data.
217 *
218 * \param ctx The HMAC context. It must have keys configured
219 * with mbedtls_md_hmac_starts() and use one of the
220 * following hashes: SHA-384, SHA-256, SHA-1 or MD-5.
221 * It is reset using mbedtls_md_hmac_reset() after
222 * the computation is complete to prepare for the
223 * next computation.
Gabor Mezei4b4e4d82021-10-18 16:28:27 +0200224 * \param add_data The first part of the message whose HMAC is being
225 * calculated. This must point to a readable buffer
226 * of \p add_data_len bytes.
gabor-mezei-armcb4317b2021-09-27 14:28:31 +0200227 * \param add_data_len The length of \p add_data in bytes.
Gabor Mezei4b4e4d82021-10-18 16:28:27 +0200228 * \param data The buffer containing the second part of the
229 * message. This must point to a readable buffer
230 * of \p max_data_len bytes.
gabor-mezei-armcb4317b2021-09-27 14:28:31 +0200231 * \param data_len_secret The length of the data to process in \p data.
232 * This must be no less than \p min_data_len and no
233 * greater than \p max_data_len.
Gabor Mezei4b4e4d82021-10-18 16:28:27 +0200234 * \param min_data_len The minimal length of the second part of the
235 * message, read from /p data.
236 * \param max_data_len The maximal length of the second part of the
237 * message, read from /p data.
gabor-mezei-armcb4317b2021-09-27 14:28:31 +0200238 * \param output The HMAC will be written here. This must point to
239 * a writable buffer of sufficient size to hold the
240 * HMAC value.
241 *
242 * \retval 0 on success.
Gabor Mezei4b4e4d82021-10-18 16:28:27 +0200243 * \retval #MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED
gabor-mezei-armcb4317b2021-09-27 14:28:31 +0200244 * The hardware accelerator failed.
245 */
gabor-mezei-arm04087df2021-09-27 16:29:52 +0200246int mbedtls_cf_hmac( mbedtls_md_context_t *ctx,
247 const unsigned char *add_data,
248 size_t add_data_len,
249 const unsigned char *data,
250 size_t data_len_secret,
251 size_t min_data_len,
252 size_t max_data_len,
253 unsigned char *output );
gabor-mezei-armcb4317b2021-09-27 14:28:31 +0200254
255#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
gabor-mezei-armf52941e2021-09-27 16:11:12 +0200256
257#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
258
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200259/** This function performs the unpadding part of a PKCS#1 v1.5 decryption
Gabor Mezei4b4e4d82021-10-18 16:28:27 +0200260 * operation (EME-PKCS1-v1_5 decoding).
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200261 *
Gabor Mezei4b4e4d82021-10-18 16:28:27 +0200262 * \note The return value from this function is a sensitive value
263 * (this is unusual). #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE shouldn't happen
264 * in a well-written application, but 0 vs #MBEDTLS_ERR_RSA_INVALID_PADDING
265 * is often a situation that an attacker can provoke and leaking which
266 * one is the result is precisely the information the attacker wants.
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200267 *
268 * \param mode The mode of operation. This must be either
269 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Gabor Mezei4b4e4d82021-10-18 16:28:27 +0200270 * \param input The input buffer which is the payload inside PKCS#1v1.5
271 * encryption padding, called the "encoded message EM"
272 * by the terminology.
273 * \param ilen The length of the payload in the \p input buffer.
274 * \param output The buffer for the payload, called "message M" by the
275 * PKCS#1 terminology. This must be a writable buffer of
276 * length \p output_max_len bytes.
Gabor Mezei91deea72021-10-18 16:17:57 +0200277 * \param olen The address at which to store the length of
Gabor Mezei4b4e4d82021-10-18 16:28:27 +0200278 * the payload. This must not be \c NULL.
279 * \param output_max_len The length in bytes of the output buffer \p output.
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200280 *
Gabor Mezei4b4e4d82021-10-18 16:28:27 +0200281 * \return \c 0 on success.
282 * \return #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE
283 * The output buffer is too small for the unpadded payload.
284 * \return #MBEDTLS_ERR_RSA_INVALID_PADDING
285 * The input doesn't contain properly formatted padding.
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200286 */
gabor-mezei-armf52941e2021-09-27 16:11:12 +0200287int mbedtls_cf_rsaes_pkcs1_v15_unpadding( int mode,
Gabor Mezei91deea72021-10-18 16:17:57 +0200288 unsigned char *input,
gabor-mezei-armf52941e2021-09-27 16:11:12 +0200289 size_t ilen,
gabor-mezei-armf52941e2021-09-27 16:11:12 +0200290 unsigned char *output,
291 size_t output_max_len,
Gabor Mezei91deea72021-10-18 16:17:57 +0200292 size_t *olen );
gabor-mezei-armf52941e2021-09-27 16:11:12 +0200293
294#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */
Gabor Mezei6e0e9902021-10-19 11:27:17 +0200295
296#endif /* MBEDTLS_CONSTANT_TIME_INTERNAL_H */