blob: 5b6223d65c5032ed78e1f40957473c25a6694959 [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-arm7e6a1ea2021-08-11 16:40:35 +020035/** Turn a value into a mask:
36 * - if \p value == 0, return the all-bits 0 mask, aka 0
Gabor Mezei4b4e4d82021-10-18 16:28:27 +020037 * - otherwise, return the all-bits 1 mask, aka (unsigned) -1
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +020038 *
39 * This function can be used to write constant-time code by replacing branches
40 * with bit operations using masks.
41 *
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +020042 * \param value The value to analyze.
43 *
44 * \return Zero if \p value is zero, otherwise all-bits-one.
45 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010046unsigned mbedtls_ct_uint_mask(unsigned value);
gabor-mezei-armd361ccd2021-09-27 11:49:42 +020047
Dave Rodgman1924adb2023-09-20 17:22:00 +010048#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) || defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC) || \
49 defined(MBEDTLS_NIST_KW_C) || defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gabor Mezei61bf64f2021-10-20 11:17:43 +020050
gabor-mezei-arm7e6a1ea2021-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-arm7e6a1ea2021-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 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010062size_t mbedtls_ct_size_mask(size_t value);
gabor-mezei-arm4d6b1462021-09-27 11:53:54 +020063
Dave Rodgman1924adb2023-09-20 17:22:00 +010064#endif /* defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) || defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC) ||
65 defined(MBEDTLS_NIST_KW_C) || defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) */
Gabor Mezei61bf64f2021-10-20 11:17:43 +020066
gabor-mezei-arm60febd52021-08-11 15:07:02 +020067#if defined(MBEDTLS_BIGNUM_C)
68
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +020069/** Turn a value into a mask:
70 * - if \p value == 0, return the all-bits 0 mask, aka 0
Gabor Mezei4b4e4d82021-10-18 16:28:27 +020071 * - otherwise, return the all-bits 1 mask, aka (mbedtls_mpi_uint) -1
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +020072 *
73 * This function can be used to write constant-time code by replacing branches
74 * with bit operations using masks.
75 *
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +020076 * \param value The value to analyze.
77 *
78 * \return Zero if \p value is zero, otherwise all-bits-one.
79 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010080mbedtls_mpi_uint mbedtls_ct_mpi_uint_mask(mbedtls_mpi_uint value);
gabor-mezei-arm60febd52021-08-11 15:07:02 +020081
82#endif /* MBEDTLS_BIGNUM_C */
83
Dave Rodgman1d523682023-09-20 16:26:49 +010084#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC) || defined(MBEDTLS_NIST_KW_C) || \
Dave Rodgman1924adb2023-09-20 17:22:00 +010085 defined(MBEDTLS_CIPHER_MODE_CBC)
Gabor Mezei2b358802021-10-18 17:05:06 +020086
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +020087/** Constant-flow mask generation for "greater or equal" comparison:
88 * - if \p x >= \p y, return all-bits 1, that is (size_t) -1
89 * - otherwise, return all bits 0, that is 0
90 *
91 * This function can be used to write constant-time code by replacing branches
92 * with bit operations using masks.
93 *
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +020094 * \param x The first value to analyze.
95 * \param y The second value to analyze.
96 *
97 * \return All-bits-one if \p x is greater or equal than \p y,
98 * otherwise zero.
99 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100100size_t mbedtls_ct_size_mask_ge(size_t x,
101 size_t y);
gabor-mezei-arm96584dd2021-09-27 12:15:19 +0200102
Dave Rodgman1d523682023-09-20 16:26:49 +0100103#endif /* defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC) || defined(MBEDTLS_NIST_KW_C) ||
Dave Rodgman1924adb2023-09-20 17:22:00 +0100104 defined(MBEDTLS_CIPHER_MODE_CBC) */
Gabor Mezei2b358802021-10-18 17:05:06 +0200105
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200106/** Constant-flow boolean "equal" comparison:
107 * return x == y
108 *
109 * This is equivalent to \p x == \p y, but is likely to be compiled
110 * to code using bitwise operation rather than a branch.
111 *
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200112 * \param x The first value to analyze.
113 * \param y The second value to analyze.
114 *
115 * \return 1 if \p x equals to \p y, otherwise 0.
116 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100117unsigned mbedtls_ct_size_bool_eq(size_t x,
118 size_t y);
gabor-mezei-arm9d7bf092021-09-27 12:25:07 +0200119
gabor-mezei-arm097d4f52021-09-27 12:55:33 +0200120#if defined(MBEDTLS_BIGNUM_C)
121
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200122/** Decide if an integer is less than the other, without branches.
123 *
124 * This is equivalent to \p x < \p y, but is likely to be compiled
125 * to code using bitwise operation rather than a branch.
126 *
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200127 * \param x The first value to analyze.
128 * \param y The second value to analyze.
129 *
130 * \return 1 if \p x is less than \p y, otherwise 0.
131 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100132unsigned mbedtls_ct_mpi_uint_lt(const mbedtls_mpi_uint x,
133 const mbedtls_mpi_uint y);
gabor-mezei-arm097d4f52021-09-27 12:55:33 +0200134
135#endif /* MBEDTLS_BIGNUM_C */
gabor-mezei-arm75332532021-09-27 12:59:30 +0200136
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200137/** Choose between two integer values without branches.
138 *
139 * This is equivalent to `condition ? if1 : if0`, but is likely to be compiled
140 * to code using bitwise operation rather than a branch.
141 *
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200142 * \param condition Condition to test.
143 * \param if1 Value to use if \p condition is nonzero.
144 * \param if0 Value to use if \p condition is zero.
145 *
146 * \return \c if1 if \p condition is nonzero, otherwise \c if0.
147 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100148unsigned mbedtls_ct_uint_if(unsigned condition,
149 unsigned if1,
150 unsigned if0);
gabor-mezei-arm75332532021-09-27 12:59:30 +0200151
gabor-mezei-arm043192d2021-09-27 13:17:15 +0200152#if defined(MBEDTLS_BIGNUM_C)
153
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200154/** Conditionally assign a value without branches.
155 *
156 * This is equivalent to `if ( condition ) dest = src`, but is likely
157 * to be compiled to code using bitwise operation rather than a branch.
158 *
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200159 * \param n \p dest and \p src must be arrays of limbs of size n.
160 * \param dest The MPI to conditionally assign to. This must point
161 * to an initialized MPI.
162 * \param src The MPI to be assigned from. This must point to an
163 * initialized MPI.
164 * \param condition Condition to test, must be 0 or 1.
165 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100166void mbedtls_ct_mpi_uint_cond_assign(size_t n,
167 mbedtls_mpi_uint *dest,
168 const mbedtls_mpi_uint *src,
169 unsigned char condition);
gabor-mezei-arm043192d2021-09-27 13:17:15 +0200170
171#endif /* MBEDTLS_BIGNUM_C */
gabor-mezei-arm7b23c0b2021-09-27 13:31:06 +0200172
Gabor Mezei200708d2021-11-15 16:18:54 +0100173#if defined(MBEDTLS_BASE64_C)
174
Gabor Mezei3a755f52021-11-24 16:26:33 +0100175/** Given a value in the range 0..63, return the corresponding Base64 digit.
176 *
177 * The implementation assumes that letters are consecutive (e.g. ASCII
178 * but not EBCDIC).
179 *
180 * \param value A value in the range 0..63.
181 *
182 * \return A base64 digit converted from \p value.
183 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100184unsigned char mbedtls_ct_base64_enc_char(unsigned char value);
Gabor Mezei200708d2021-11-15 16:18:54 +0100185
Gabor Mezei3a755f52021-11-24 16:26:33 +0100186/** Given a Base64 digit, return its value.
187 *
188 * If c is not a Base64 digit ('A'..'Z', 'a'..'z', '0'..'9', '+' or '/'),
189 * return -1.
190 *
191 * The implementation assumes that letters are consecutive (e.g. ASCII
192 * but not EBCDIC).
193 *
194 * \param c A base64 digit.
195 *
196 * \return The value of the base64 digit \p c.
197 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100198signed char mbedtls_ct_base64_dec_value(unsigned char c);
Gabor Mezei3d4dba82021-11-15 16:22:37 +0100199
Gabor Mezei200708d2021-11-15 16:18:54 +0100200#endif /* MBEDTLS_BASE64_C */
201
Przemek Stekiel169554c2022-10-06 10:53:20 +0200202#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200203
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200204/** Conditional memcpy without branches.
205 *
Gabor Mezeidbe0f892021-11-03 16:13:32 +0100206 * This is equivalent to `if ( c1 == c2 ) memcpy(dest, src, len)`, but is likely
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200207 * to be compiled to code using bitwise operation rather than a branch.
208 *
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200209 * \param dest The pointer to conditionally copy to.
Gabor Mezei4b4e4d82021-10-18 16:28:27 +0200210 * \param src The pointer to copy from. Shouldn't overlap with \p dest.
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200211 * \param len The number of bytes to copy.
212 * \param c1 The first value to analyze in the condition.
213 * \param c2 The second value to analyze in the condition.
214 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100215void mbedtls_ct_memcpy_if_eq(unsigned char *dest,
216 const unsigned char *src,
217 size_t len,
218 size_t c1, size_t c2);
gabor-mezei-arm0f7b9e42021-09-27 13:57:45 +0200219
220/** Copy data from a secret position with constant flow.
221 *
222 * This function copies \p len bytes from \p src_base + \p offset_secret to \p
223 * dst, with a code flow and memory access pattern that does not depend on \p
224 * offset_secret, but only on \p offset_min, \p offset_max and \p len.
Gabor Mezei4b4e4d82021-10-18 16:28:27 +0200225 * Functionally equivalent to `memcpy(dst, src + offset_secret, len)`.
gabor-mezei-arm0f7b9e42021-09-27 13:57:45 +0200226 *
Paul Elliottb8300282022-05-19 18:31:35 +0100227 * \note This function reads from \p dest, but the value that
228 * is read does not influence the result and this
229 * function's behavior is well-defined regardless of the
230 * contents of the buffers. This may result in false
231 * positives from static or dynamic analyzers, especially
232 * if \p dest is not initialized.
233 *
Gabor Mezei91deea72021-10-18 16:17:57 +0200234 * \param dest The destination buffer. This must point to a writable
gabor-mezei-arm0f7b9e42021-09-27 13:57:45 +0200235 * buffer of at least \p len bytes.
Gabor Mezei91deea72021-10-18 16:17:57 +0200236 * \param src The base of the source buffer. This must point to a
gabor-mezei-arm0f7b9e42021-09-27 13:57:45 +0200237 * readable buffer of at least \p offset_max + \p len
Gabor Mezei91deea72021-10-18 16:17:57 +0200238 * bytes. Shouldn't overlap with \p dest.
239 * \param offset The offset in the source buffer from which to copy.
gabor-mezei-arm0f7b9e42021-09-27 13:57:45 +0200240 * This must be no less than \p offset_min and no greater
241 * than \p offset_max.
Gabor Mezei91deea72021-10-18 16:17:57 +0200242 * \param offset_min The minimal value of \p offset.
243 * \param offset_max The maximal value of \p offset.
gabor-mezei-arm0f7b9e42021-09-27 13:57:45 +0200244 * \param len The number of bytes to copy.
245 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100246void mbedtls_ct_memcpy_offset(unsigned char *dest,
247 const unsigned char *src,
248 size_t offset,
249 size_t offset_min,
250 size_t offset_max,
251 size_t len);
gabor-mezei-armcb4317b2021-09-27 14:28:31 +0200252
gabor-mezei-armcb4317b2021-09-27 14:28:31 +0200253/** Compute the HMAC of variable-length data with constant flow.
254 *
255 * This function computes the HMAC of the concatenation of \p add_data and \p
256 * data, and does with a code flow and memory access pattern that does not
257 * depend on \p data_len_secret, but only on \p min_data_len and \p
258 * max_data_len. In particular, this function always reads exactly \p
259 * max_data_len bytes from \p data.
260 *
261 * \param ctx The HMAC context. It must have keys configured
262 * with mbedtls_md_hmac_starts() and use one of the
263 * following hashes: SHA-384, SHA-256, SHA-1 or MD-5.
264 * It is reset using mbedtls_md_hmac_reset() after
265 * the computation is complete to prepare for the
266 * next computation.
Gabor Mezei4b4e4d82021-10-18 16:28:27 +0200267 * \param add_data The first part of the message whose HMAC is being
268 * calculated. This must point to a readable buffer
269 * of \p add_data_len bytes.
gabor-mezei-armcb4317b2021-09-27 14:28:31 +0200270 * \param add_data_len The length of \p add_data in bytes.
Gabor Mezei4b4e4d82021-10-18 16:28:27 +0200271 * \param data The buffer containing the second part of the
272 * message. This must point to a readable buffer
273 * of \p max_data_len bytes.
gabor-mezei-armcb4317b2021-09-27 14:28:31 +0200274 * \param data_len_secret The length of the data to process in \p data.
275 * This must be no less than \p min_data_len and no
276 * greater than \p max_data_len.
Gabor Mezei4b4e4d82021-10-18 16:28:27 +0200277 * \param min_data_len The minimal length of the second part of the
Gabor Mezeida206512021-10-20 11:18:37 +0200278 * message, read from \p data.
Gabor Mezei4b4e4d82021-10-18 16:28:27 +0200279 * \param max_data_len The maximal length of the second part of the
Gabor Mezeida206512021-10-20 11:18:37 +0200280 * message, read from \p data.
gabor-mezei-armcb4317b2021-09-27 14:28:31 +0200281 * \param output The HMAC will be written here. This must point to
282 * a writable buffer of sufficient size to hold the
283 * HMAC value.
284 *
285 * \retval 0 on success.
Gabor Mezei4b4e4d82021-10-18 16:28:27 +0200286 * \retval #MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED
gabor-mezei-armcb4317b2021-09-27 14:28:31 +0200287 * The hardware accelerator failed.
288 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100289int mbedtls_ct_hmac(mbedtls_md_context_t *ctx,
290 const unsigned char *add_data,
291 size_t add_data_len,
292 const unsigned char *data,
293 size_t data_len_secret,
294 size_t min_data_len,
295 size_t max_data_len,
296 unsigned char *output);
gabor-mezei-armcb4317b2021-09-27 14:28:31 +0200297
Przemek Stekiel169554c2022-10-06 10:53:20 +0200298#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
gabor-mezei-armf52941e2021-09-27 16:11:12 +0200299
300#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
301
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200302/** This function performs the unpadding part of a PKCS#1 v1.5 decryption
Gabor Mezei4b4e4d82021-10-18 16:28:27 +0200303 * operation (EME-PKCS1-v1_5 decoding).
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200304 *
Gabor Mezei4b4e4d82021-10-18 16:28:27 +0200305 * \note The return value from this function is a sensitive value
306 * (this is unusual). #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE shouldn't happen
307 * in a well-written application, but 0 vs #MBEDTLS_ERR_RSA_INVALID_PADDING
308 * is often a situation that an attacker can provoke and leaking which
309 * one is the result is precisely the information the attacker wants.
gabor-mezei-arm7e6a1ea2021-08-11 16:40:35 +0200310 *
311 * \param mode The mode of operation. This must be either
312 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
Gabor Mezei4b4e4d82021-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 Mezei91deea72021-10-18 16:17:57 +0200320 * \param olen The address at which to store the length of
Gabor Mezei4b4e4d82021-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-arm7e6a1ea2021-08-11 16:40:35 +0200323 *
Gabor Mezei4b4e4d82021-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-arm7e6a1ea2021-08-11 16:40:35 +0200329 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100330int mbedtls_ct_rsaes_pkcs1_v15_unpadding(int mode,
331 unsigned char *input,
332 size_t ilen,
333 unsigned char *output,
334 size_t output_max_len,
335 size_t *olen);
gabor-mezei-armf52941e2021-09-27 16:11:12 +0200336
337#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */
Gabor Mezei6e0e9902021-10-19 11:27:17 +0200338
339#endif /* MBEDTLS_CONSTANT_TIME_INTERNAL_H */