blob: 4838d05e9cf61e1e11a12d542103ed534caf3535 [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
132#endif /* MBEDTLS_BIGNUM_C */
gabor-mezei-armb2dbf2c2021-09-27 12:59:30 +0200133
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200134/** Choose between two integer values without branches.
135 *
136 * This is equivalent to `condition ? if1 : if0`, 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 condition Condition to test.
140 * \param if1 Value to use if \p condition is nonzero.
141 * \param if0 Value to use if \p condition is zero.
142 *
143 * \return \c if1 if \p condition is nonzero, otherwise \c if0.
144 */
Gabor Mezei90437e32021-10-20 11:59:27 +0200145unsigned mbedtls_ct_uint_if( unsigned condition,
gabor-mezei-arm2dcd7682021-09-27 16:29:52 +0200146 unsigned if1,
147 unsigned if0 );
gabor-mezei-armb2dbf2c2021-09-27 12:59:30 +0200148
gabor-mezei-armbe8d98b2021-09-27 13:17:15 +0200149#if defined(MBEDTLS_BIGNUM_C)
150
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200151/** Conditionally assign a value without branches.
152 *
153 * This is equivalent to `if ( condition ) dest = src`, but is likely
154 * to be compiled to code using bitwise operation rather than a branch.
155 *
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200156 * \param n \p dest and \p src must be arrays of limbs of size n.
157 * \param dest The MPI to conditionally assign to. This must point
158 * to an initialized MPI.
159 * \param src The MPI to be assigned from. This must point to an
160 * initialized MPI.
161 * \param condition Condition to test, must be 0 or 1.
162 */
Gabor Mezei90437e32021-10-20 11:59:27 +0200163void mbedtls_ct_mpi_uint_cond_assign( size_t n,
gabor-mezei-armbe8d98b2021-09-27 13:17:15 +0200164 mbedtls_mpi_uint *dest,
165 const mbedtls_mpi_uint *src,
gabor-mezei-arm87ac5be2021-08-10 20:36:09 +0200166 unsigned char condition );
gabor-mezei-armbe8d98b2021-09-27 13:17:15 +0200167
168#endif /* MBEDTLS_BIGNUM_C */
gabor-mezei-arm394aeaa2021-09-27 13:31:06 +0200169
Gabor Mezei9a4074a2021-11-15 16:18:54 +0100170#if defined(MBEDTLS_BASE64_C)
171
Gabor Mezeia0969752021-11-24 16:26:33 +0100172/** Given a value in the range 0..63, return the corresponding Base64 digit.
173 *
174 * The implementation assumes that letters are consecutive (e.g. ASCII
175 * but not EBCDIC).
176 *
177 * \param value A value in the range 0..63.
178 *
179 * \return A base64 digit converted from \p value.
180 */
Gabor Mezei14d5fac2021-11-24 15:51:39 +0100181unsigned char mbedtls_ct_base64_enc_char( unsigned char value );
Gabor Mezei9a4074a2021-11-15 16:18:54 +0100182
Gabor Mezeia0969752021-11-24 16:26:33 +0100183/** Given a Base64 digit, return its value.
184 *
185 * If c is not a Base64 digit ('A'..'Z', 'a'..'z', '0'..'9', '+' or '/'),
186 * return -1.
187 *
188 * The implementation assumes that letters are consecutive (e.g. ASCII
189 * but not EBCDIC).
190 *
191 * \param c A base64 digit.
192 *
193 * \return The value of the base64 digit \p c.
194 */
Gabor Mezei358829a2021-11-15 16:22:37 +0100195signed char mbedtls_ct_base64_dec_value( unsigned char c );
196
Gabor Mezei9a4074a2021-11-15 16:18:54 +0100197#endif /* MBEDTLS_BASE64_C */
198
Gabor Mezeie2123792021-10-18 17:05:06 +0200199#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200200
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200201/** Conditional memcpy without branches.
202 *
Gabor Mezei642eeb22021-11-03 16:13:32 +0100203 * This is equivalent to `if ( c1 == c2 ) memcpy(dest, src, len)`, but is likely
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200204 * to be compiled to code using bitwise operation rather than a branch.
205 *
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200206 * \param dest The pointer to conditionally copy to.
Gabor Mezeia316fc82021-10-18 16:28:27 +0200207 * \param src The pointer to copy from. Shouldn't overlap with \p dest.
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200208 * \param len The number of bytes to copy.
209 * \param c1 The first value to analyze in the condition.
210 * \param c2 The second value to analyze in the condition.
211 */
Gabor Mezei90437e32021-10-20 11:59:27 +0200212void mbedtls_ct_memcpy_if_eq( unsigned char *dest,
gabor-mezei-armdee0fd32021-09-27 13:34:25 +0200213 const unsigned char *src,
214 size_t len,
215 size_t c1, size_t c2 );
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200216
217/** Copy data from a secret position with constant flow.
218 *
219 * This function copies \p len bytes from \p src_base + \p offset_secret to \p
220 * dst, with a code flow and memory access pattern that does not depend on \p
221 * offset_secret, but only on \p offset_min, \p offset_max and \p len.
Gabor Mezeia316fc82021-10-18 16:28:27 +0200222 * Functionally equivalent to `memcpy(dst, src + offset_secret, len)`.
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200223 *
Gabor Mezei63bbba52021-10-18 16:17:57 +0200224 * \param dest The destination buffer. This must point to a writable
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200225 * buffer of at least \p len bytes.
Gabor Mezei63bbba52021-10-18 16:17:57 +0200226 * \param src The base of the source buffer. This must point to a
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200227 * readable buffer of at least \p offset_max + \p len
Gabor Mezei63bbba52021-10-18 16:17:57 +0200228 * bytes. Shouldn't overlap with \p dest.
229 * \param offset The offset in the source buffer from which to copy.
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200230 * This must be no less than \p offset_min and no greater
231 * than \p offset_max.
Gabor Mezei63bbba52021-10-18 16:17:57 +0200232 * \param offset_min The minimal value of \p offset.
233 * \param offset_max The maximal value of \p offset.
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200234 * \param len The number of bytes to copy.
235 */
Gabor Mezei90437e32021-10-20 11:59:27 +0200236void mbedtls_ct_memcpy_offset( unsigned char *dest,
Gabor Mezei63bbba52021-10-18 16:17:57 +0200237 const unsigned char *src,
238 size_t offset,
gabor-mezei-arm2dcd7682021-09-27 16:29:52 +0200239 size_t offset_min,
240 size_t offset_max,
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200241 size_t len );
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +0200242
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +0200243/** Compute the HMAC of variable-length data with constant flow.
244 *
245 * This function computes the HMAC of the concatenation of \p add_data and \p
246 * data, and does with a code flow and memory access pattern that does not
247 * depend on \p data_len_secret, but only on \p min_data_len and \p
248 * max_data_len. In particular, this function always reads exactly \p
249 * max_data_len bytes from \p data.
250 *
251 * \param ctx The HMAC context. It must have keys configured
252 * with mbedtls_md_hmac_starts() and use one of the
253 * following hashes: SHA-384, SHA-256, SHA-1 or MD-5.
254 * It is reset using mbedtls_md_hmac_reset() after
255 * the computation is complete to prepare for the
256 * next computation.
Gabor Mezeia316fc82021-10-18 16:28:27 +0200257 * \param add_data The first part of the message whose HMAC is being
258 * calculated. This must point to a readable buffer
259 * of \p add_data_len bytes.
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +0200260 * \param add_data_len The length of \p add_data in bytes.
Gabor Mezeia316fc82021-10-18 16:28:27 +0200261 * \param data The buffer containing the second part of the
262 * message. This must point to a readable buffer
263 * of \p max_data_len bytes.
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +0200264 * \param data_len_secret The length of the data to process in \p data.
265 * This must be no less than \p min_data_len and no
266 * greater than \p max_data_len.
Gabor Mezeia316fc82021-10-18 16:28:27 +0200267 * \param min_data_len The minimal length of the second part of the
Gabor Mezei116cd6a2021-10-20 11:18:37 +0200268 * message, read from \p data.
Gabor Mezeia316fc82021-10-18 16:28:27 +0200269 * \param max_data_len The maximal length of the second part of the
Gabor Mezei116cd6a2021-10-20 11:18:37 +0200270 * message, read from \p data.
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +0200271 * \param output The HMAC will be written here. This must point to
272 * a writable buffer of sufficient size to hold the
273 * HMAC value.
274 *
275 * \retval 0 on success.
Gabor Mezeia316fc82021-10-18 16:28:27 +0200276 * \retval #MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +0200277 * The hardware accelerator failed.
278 */
Neil Armstrong2968d302022-02-25 15:09:36 +0100279#if defined(MBEDTLS_USE_PSA_CRYPTO)
280int mbedtls_ct_hmac( mbedtls_svc_key_id_t key,
281 psa_algorithm_t alg,
282 const unsigned char *add_data,
283 size_t add_data_len,
284 const unsigned char *data,
285 size_t data_len_secret,
286 size_t min_data_len,
287 size_t max_data_len,
288 unsigned char *output );
289#else
Gabor Mezei90437e32021-10-20 11:59:27 +0200290int mbedtls_ct_hmac( mbedtls_md_context_t *ctx,
gabor-mezei-arm2dcd7682021-09-27 16:29:52 +0200291 const unsigned char *add_data,
292 size_t add_data_len,
293 const unsigned char *data,
294 size_t data_len_secret,
295 size_t min_data_len,
296 size_t max_data_len,
297 unsigned char *output );
Neil Armstrong2968d302022-02-25 15:09:36 +0100298#endif /* MBEDTLS_USE_PSA_CRYPTO */
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +0200299
300#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
gabor-mezei-armfdb71182021-09-27 16:11:12 +0200301
302#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
303
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200304/** This function performs the unpadding part of a PKCS#1 v1.5 decryption
Gabor Mezeia316fc82021-10-18 16:28:27 +0200305 * operation (EME-PKCS1-v1_5 decoding).
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200306 *
Gabor Mezeia316fc82021-10-18 16:28:27 +0200307 * \note The return value from this function is a sensitive value
308 * (this is unusual). #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE shouldn't happen
309 * in a well-written application, but 0 vs #MBEDTLS_ERR_RSA_INVALID_PADDING
310 * is often a situation that an attacker can provoke and leaking which
311 * one is the result is precisely the information the attacker wants.
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200312 *
Gabor Mezeia316fc82021-10-18 16:28:27 +0200313 * \param input The input buffer which is the payload inside PKCS#1v1.5
314 * encryption padding, called the "encoded message EM"
315 * by the terminology.
316 * \param ilen The length of the payload in the \p input buffer.
317 * \param output The buffer for the payload, called "message M" by the
318 * PKCS#1 terminology. This must be a writable buffer of
319 * length \p output_max_len bytes.
Gabor Mezei63bbba52021-10-18 16:17:57 +0200320 * \param olen The address at which to store the length of
Gabor Mezeia316fc82021-10-18 16:28:27 +0200321 * the payload. This must not be \c NULL.
322 * \param output_max_len The length in bytes of the output buffer \p output.
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200323 *
Gabor Mezeia316fc82021-10-18 16:28:27 +0200324 * \return \c 0 on success.
325 * \return #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE
326 * The output buffer is too small for the unpadded payload.
327 * \return #MBEDTLS_ERR_RSA_INVALID_PADDING
328 * The input doesn't contain properly formatted padding.
gabor-mezei-arm90d96cc2021-08-11 16:40:35 +0200329 */
Gabor Mezei90437e32021-10-20 11:59:27 +0200330int mbedtls_ct_rsaes_pkcs1_v15_unpadding( unsigned char *input,
Gabor Mezei63bbba52021-10-18 16:17:57 +0200331 size_t ilen,
gabor-mezei-armfdb71182021-09-27 16:11:12 +0200332 unsigned char *output,
333 size_t output_max_len,
Gabor Mezei63bbba52021-10-18 16:17:57 +0200334 size_t *olen );
gabor-mezei-armfdb71182021-09-27 16:11:12 +0200335
336#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */
Gabor Mezei291df7b2021-10-19 11:27:17 +0200337
338#endif /* MBEDTLS_CONSTANT_TIME_INTERNAL_H */