blob: c5255851e1da2e32912eec32cbbc680c649dd634 [file] [log] [blame]
gabor-mezei-armd1125342021-07-12 16:31:22 +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
Gilles Peskine449bd832023-01-11 14:50:10 +010020/*
Gabor Mezei642eeb22021-11-03 16:13:32 +010021 * The following functions are implemented without using comparison operators, as those
Gabor Mezeieab90bc2021-10-18 16:09:41 +020022 * might be translated to branches by some compilers on some platforms.
23 */
24
Dave Rodgman40a41d02023-05-17 11:59:56 +010025#include <limits.h>
26
gabor-mezei-armd1125342021-07-12 16:31:22 +020027#include "common.h"
Gabor Mezei22c9a6f2021-10-20 12:09:35 +020028#include "constant_time_internal.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020029#include "mbedtls/constant_time.h"
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +020030#include "mbedtls/error.h"
gabor-mezei-arm5b3a32d2021-09-29 10:50:31 +020031#include "mbedtls/platform_util.h"
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +020032
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +020033#if defined(MBEDTLS_SSL_TLS_C)
34#include "ssl_misc.h"
35#endif
36
gabor-mezei-arm5b3a32d2021-09-29 10:50:31 +020037#if defined(MBEDTLS_RSA_C)
38#include "mbedtls/rsa.h"
39#endif
40
gabor-mezei-armfdb71182021-09-27 16:11:12 +020041#include <string.h>
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050042#if defined(MBEDTLS_USE_PSA_CRYPTO)
43#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
44 psa_to_ssl_errors, \
45 psa_generic_status_to_mbedtls)
46#endif
gabor-mezei-arm3f90fd52021-09-27 12:55:33 +020047
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000048/*
Dave Rodgman051225d2022-12-30 21:25:35 +000049 * Define MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS where assembly is present to
50 * perform fast unaligned access to volatile data.
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000051 *
52 * This is needed because mbedtls_get_unaligned_uintXX etc don't support volatile
53 * memory accesses.
54 *
Dave Rodgman051225d2022-12-30 21:25:35 +000055 * Some of these definitions could be moved into alignment.h but for now they are
56 * only used here.
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000057 */
Dave Rodgman40a41d02023-05-17 11:59:56 +010058#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS) && \
59 (defined(MBEDTLS_CT_ARM_ASM) || defined(MBEDTLS_CT_AARCH64_ASM))
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000060
Dave Rodgman40a41d02023-05-17 11:59:56 +010061#define MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS
62
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000063static inline uint32_t mbedtls_get_unaligned_volatile_uint32(volatile const unsigned char *p)
64{
65 /* This is UB, even where it's safe:
66 * return *((volatile uint32_t*)p);
67 * so instead the same thing is expressed in assembly below.
68 */
69 uint32_t r;
Dave Rodgman40a41d02023-05-17 11:59:56 +010070#if defined(MBEDTLS_CT_ARM_ASM)
Dave Rodgman4610d4b2023-01-30 09:26:48 +000071 asm volatile ("ldr %0, [%1]" : "=r" (r) : "r" (p) :);
Dave Rodgman40a41d02023-05-17 11:59:56 +010072#elif defined(MBEDTLS_CT_AARCH64_ASM)
Dave Rodgman4610d4b2023-01-30 09:26:48 +000073 asm volatile ("ldr %w0, [%1]" : "=r" (r) : "r" (p) :);
Dave Rodgman40a41d02023-05-17 11:59:56 +010074#else
75#error No assembly defined for mbedtls_get_unaligned_volatile_uint32
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000076#endif
Dave Rodgman051225d2022-12-30 21:25:35 +000077 return r;
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000078}
Dave Rodgman40a41d02023-05-17 11:59:56 +010079#endif /* defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS) &&
80 (defined(MBEDTLS_CT_ARM_ASM) || defined(MBEDTLS_CT_AARCH64_ASM)) */
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000081
Gilles Peskine449bd832023-01-11 14:50:10 +010082int mbedtls_ct_memcmp(const void *a,
83 const void *b,
84 size_t n)
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +020085{
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000086 size_t i = 0;
Dave Rodgman7658b632023-01-11 17:39:33 +000087 /*
88 * `A` and `B` are cast to volatile to ensure that the compiler
89 * generates code that always fully reads both buffers.
90 * Otherwise it could generate a test to exit early if `diff` has all
91 * bits set early in the loop.
92 */
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +020093 volatile const unsigned char *A = (volatile const unsigned char *) a;
94 volatile const unsigned char *B = (volatile const unsigned char *) b;
Dave Rodgman7658b632023-01-11 17:39:33 +000095 uint32_t diff = 0;
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +020096
Dave Rodgman051225d2022-12-30 21:25:35 +000097#if defined(MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS)
Dave Rodgman36dfc5a2022-12-22 15:04:43 +000098 for (; (i + 4) <= n; i += 4) {
99 uint32_t x = mbedtls_get_unaligned_volatile_uint32(A + i);
100 uint32_t y = mbedtls_get_unaligned_volatile_uint32(B + i);
101 diff |= x ^ y;
102 }
103#endif
104
105 for (; i < n; i++) {
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +0200106 /* Read volatile data in order before computing diff.
107 * This avoids IAR compiler warning:
108 * 'the order of volatile accesses is undefined ..' */
109 unsigned char x = A[i], y = B[i];
110 diff |= x ^ y;
111 }
112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 return (int) diff;
gabor-mezei-armdb9a38c2021-09-27 11:28:54 +0200114}
115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116unsigned mbedtls_ct_uint_mask(unsigned value)
gabor-mezei-arm340948e2021-09-27 11:40:03 +0200117{
118 /* MSVC has a warning about unary minus on unsigned, but this is
119 * well-defined and precisely what we want to do here */
120#if defined(_MSC_VER)
121#pragma warning( push )
122#pragma warning( disable : 4146 )
123#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 return -((value | -value) >> (sizeof(value) * 8 - 1));
gabor-mezei-arm340948e2021-09-27 11:40:03 +0200125#if defined(_MSC_VER)
126#pragma warning( pop )
127#endif
128}
gabor-mezei-arm3733bf82021-09-27 11:49:42 +0200129
Przemek Stekiel89ad6232022-09-27 13:36:12 +0200130#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gabor Mezei6a426c92021-10-20 11:17:43 +0200131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132size_t mbedtls_ct_size_mask(size_t value)
gabor-mezei-arm3733bf82021-09-27 11:49:42 +0200133{
134 /* MSVC has a warning about unary minus on unsigned integer types,
135 * but this is well-defined and precisely what we want to do here. */
136#if defined(_MSC_VER)
137#pragma warning( push )
138#pragma warning( disable : 4146 )
139#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 return -((value | -value) >> (sizeof(value) * 8 - 1));
gabor-mezei-arm3733bf82021-09-27 11:49:42 +0200141#if defined(_MSC_VER)
142#pragma warning( pop )
143#endif
144}
gabor-mezei-armc76227d2021-09-27 11:53:54 +0200145
Przemek Stekiel89ad6232022-09-27 13:36:12 +0200146#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Gabor Mezei6a426c92021-10-20 11:17:43 +0200147
gabor-mezei-arm9cb55692021-08-11 15:07:02 +0200148#if defined(MBEDTLS_BIGNUM_C)
149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150mbedtls_mpi_uint mbedtls_ct_mpi_uint_mask(mbedtls_mpi_uint value)
gabor-mezei-arm9cb55692021-08-11 15:07:02 +0200151{
152 /* MSVC has a warning about unary minus on unsigned, but this is
153 * well-defined and precisely what we want to do here */
154#if defined(_MSC_VER)
155#pragma warning( push )
156#pragma warning( disable : 4146 )
157#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 return -((value | -value) >> (sizeof(value) * 8 - 1));
gabor-mezei-arm9cb55692021-08-11 15:07:02 +0200159#if defined(_MSC_VER)
160#pragma warning( pop )
161#endif
162}
163
164#endif /* MBEDTLS_BIGNUM_C */
165
Gabor Mezeie2123792021-10-18 17:05:06 +0200166#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
167
Gabor Mezei1e642612021-10-18 16:05:50 +0200168/** Constant-flow mask generation for "less than" comparison:
169 * - if \p x < \p y, return all-bits 1, that is (size_t) -1
170 * - otherwise, return all bits 0, that is 0
171 *
172 * This function can be used to write constant-time code by replacing branches
173 * with bit operations using masks.
174 *
175 * \param x The first value to analyze.
176 * \param y The second value to analyze.
177 *
178 * \return All-bits-one if \p x is less than \p y, otherwise zero.
179 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100180static size_t mbedtls_ct_size_mask_lt(size_t x,
181 size_t y)
gabor-mezei-armc76227d2021-09-27 11:53:54 +0200182{
183 /* This has the most significant bit set if and only if x < y */
184 const size_t sub = x - y;
185
186 /* sub1 = (x < y) ? 1 : 0 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 const size_t sub1 = sub >> (sizeof(sub) * 8 - 1);
gabor-mezei-armc76227d2021-09-27 11:53:54 +0200188
189 /* mask = (x < y) ? 0xff... : 0x00... */
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 const size_t mask = mbedtls_ct_size_mask(sub1);
gabor-mezei-armc76227d2021-09-27 11:53:54 +0200191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 return mask;
gabor-mezei-armc76227d2021-09-27 11:53:54 +0200193}
gabor-mezei-arm16fc57b2021-09-27 11:58:31 +0200194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195size_t mbedtls_ct_size_mask_ge(size_t x,
196 size_t y)
gabor-mezei-arm16fc57b2021-09-27 11:58:31 +0200197{
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 return ~mbedtls_ct_size_mask_lt(x, y);
gabor-mezei-arm16fc57b2021-09-27 11:58:31 +0200199}
gabor-mezei-arm8d1d5fd2021-09-27 12:15:19 +0200200
Gabor Mezeie2123792021-10-18 17:05:06 +0200201#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
202
Gabor Mezei28d61152021-11-15 16:13:01 +0100203#if defined(MBEDTLS_BASE64_C)
204
205/* Return 0xff if low <= c <= high, 0 otherwise.
206 *
207 * Constant flow with respect to c.
208 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100209unsigned char mbedtls_ct_uchar_mask_of_range(unsigned char low,
210 unsigned char high,
211 unsigned char c)
Gabor Mezei28d61152021-11-15 16:13:01 +0100212{
213 /* low_mask is: 0 if low <= c, 0x...ff if low > c */
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 unsigned low_mask = ((unsigned) c - low) >> 8;
Gabor Mezei28d61152021-11-15 16:13:01 +0100215 /* high_mask is: 0 if c <= high, 0x...ff if c > high */
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 unsigned high_mask = ((unsigned) high - c) >> 8;
217 return ~(low_mask | high_mask) & 0xff;
Gabor Mezei28d61152021-11-15 16:13:01 +0100218}
219
220#endif /* MBEDTLS_BASE64_C */
221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222unsigned mbedtls_ct_size_bool_eq(size_t x,
223 size_t y)
gabor-mezei-arm8d1d5fd2021-09-27 12:15:19 +0200224{
225 /* diff = 0 if x == y, non-zero otherwise */
226 const size_t diff = x ^ y;
227
228 /* MSVC has a warning about unary minus on unsigned integer types,
229 * but this is well-defined and precisely what we want to do here. */
230#if defined(_MSC_VER)
231#pragma warning( push )
232#pragma warning( disable : 4146 )
233#endif
234
235 /* diff_msb's most significant bit is equal to x != y */
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 const size_t diff_msb = (diff | (size_t) -diff);
gabor-mezei-arm8d1d5fd2021-09-27 12:15:19 +0200237
238#if defined(_MSC_VER)
239#pragma warning( pop )
240#endif
241
242 /* diff1 = (x != y) ? 1 : 0 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 const unsigned diff1 = diff_msb >> (sizeof(diff_msb) * 8 - 1);
gabor-mezei-arm8d1d5fd2021-09-27 12:15:19 +0200244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 return 1 ^ diff1;
gabor-mezei-arm8d1d5fd2021-09-27 12:15:19 +0200246}
gabor-mezei-arm5a854422021-09-27 12:25:07 +0200247
Gabor Mezeie2123792021-10-18 17:05:06 +0200248#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
249
Dave Rodgman0afe0012023-05-09 11:09:52 +0100250unsigned mbedtls_ct_size_gt(size_t x, size_t y)
gabor-mezei-arm5a854422021-09-27 12:25:07 +0200251{
gabor-mezei-arm87ac5be2021-08-10 20:36:09 +0200252 /* Return the sign bit (1 for negative) of (y - x). */
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 return (y - x) >> (sizeof(size_t) * 8 - 1);
gabor-mezei-arm5a854422021-09-27 12:25:07 +0200254}
gabor-mezei-arm3f90fd52021-09-27 12:55:33 +0200255
Gabor Mezeie2123792021-10-18 17:05:06 +0200256#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */
257
gabor-mezei-arm3f90fd52021-09-27 12:55:33 +0200258#if defined(MBEDTLS_BIGNUM_C)
259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260unsigned mbedtls_ct_mpi_uint_lt(const mbedtls_mpi_uint x,
261 const mbedtls_mpi_uint y)
gabor-mezei-arm3f90fd52021-09-27 12:55:33 +0200262{
263 mbedtls_mpi_uint ret;
264 mbedtls_mpi_uint cond;
265
266 /*
267 * Check if the most significant bits (MSB) of the operands are different.
268 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 cond = (x ^ y);
gabor-mezei-arm3f90fd52021-09-27 12:55:33 +0200270 /*
271 * If the MSB are the same then the difference x-y will be negative (and
272 * have its MSB set to 1 during conversion to unsigned) if and only if x<y.
273 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 ret = (x - y) & ~cond;
gabor-mezei-arm3f90fd52021-09-27 12:55:33 +0200275 /*
276 * If the MSB are different, then the operand with the MSB of 1 is the
277 * bigger. (That is if y has MSB of 1, then x<y is true and it is false if
278 * the MSB of y is 0.)
279 */
280 ret |= y & cond;
281
282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 ret = ret >> (sizeof(mbedtls_mpi_uint) * 8 - 1);
gabor-mezei-arm3f90fd52021-09-27 12:55:33 +0200284
285 return (unsigned) ret;
286}
287
288#endif /* MBEDTLS_BIGNUM_C */
gabor-mezei-armb2dbf2c2021-09-27 12:59:30 +0200289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290unsigned mbedtls_ct_uint_if(unsigned condition,
291 unsigned if1,
292 unsigned if0)
gabor-mezei-armb2dbf2c2021-09-27 12:59:30 +0200293{
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 unsigned mask = mbedtls_ct_uint_mask(condition);
295 return (mask & if1) | (~mask & if0);
gabor-mezei-armb2dbf2c2021-09-27 12:59:30 +0200296}
gabor-mezei-armd3230d52021-09-27 13:03:57 +0200297
Gabor Mezeie2123792021-10-18 17:05:06 +0200298#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
299
Dave Rodgman15c142b2023-05-17 12:20:11 +0100300void mbedtls_ct_memmove_left(void *start, size_t total, size_t offset)
gabor-mezei-arm394aeaa2021-09-27 13:31:06 +0200301{
302 volatile unsigned char *buf = start;
Dave Rodgman15c142b2023-05-17 12:20:11 +0100303 for (size_t i = 0; i < total; i++) {
304 mbedtls_ct_condition_t no_op = mbedtls_ct_bool_gt(total - offset, i);
gabor-mezei-arm394aeaa2021-09-27 13:31:06 +0200305 /* The first `total - offset` passes are a no-op. The last
306 * `offset` passes shift the data one byte to the left and
307 * zero out the last byte. */
Dave Rodgman15c142b2023-05-17 12:20:11 +0100308 for (size_t n = 0; n < total - 1; n++) {
gabor-mezei-arm394aeaa2021-09-27 13:31:06 +0200309 unsigned char current = buf[n];
Dave Rodgman15c142b2023-05-17 12:20:11 +0100310 unsigned char next = buf[n+1];
311 buf[n] = mbedtls_ct_uint_if_new(no_op, current, next);
gabor-mezei-arm394aeaa2021-09-27 13:31:06 +0200312 }
Dave Rodgman15c142b2023-05-17 12:20:11 +0100313 buf[total-1] = mbedtls_ct_uint_if0(no_op, buf[total-1]);
gabor-mezei-arm394aeaa2021-09-27 13:31:06 +0200314 }
315}
gabor-mezei-armdee0fd32021-09-27 13:34:25 +0200316
Gabor Mezeie2123792021-10-18 17:05:06 +0200317#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */
318
Dave Rodgman7fe6e6f2023-05-17 12:34:56 +0100319void mbedtls_ct_memcpy_if(mbedtls_ct_condition_t condition,
320 unsigned char *dest,
321 const unsigned char *src1,
322 const unsigned char *src2,
323 size_t len)
324{
325 const uint32_t mask = (uint32_t) condition;
326 const uint32_t not_mask = (uint32_t) ~mbedtls_ct_compiler_opaque(condition);
327
328 /* If src2 is NULL and condition == 0, then this function has no effect.
329 * In this case, copy from dest back into dest. */
330 if (src2 == NULL) {
331 src2 = dest;
332 }
333
334 /* dest[i] = c1 == c2 ? src[i] : dest[i] */
335 size_t i = 0;
336#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS)
337 for (; (i + 4) <= len; i += 4) {
338 uint32_t a = mbedtls_get_unaligned_uint32(src1 + i) & mask;
339 uint32_t b = mbedtls_get_unaligned_uint32(src2 + i) & not_mask;
340 mbedtls_put_unaligned_uint32(dest + i, a | b);
341 }
342#endif /* MBEDTLS_EFFICIENT_UNALIGNED_ACCESS */
343 for (; i < len; i++) {
344 dest[i] = (src1[i] & mask) | (src2[i] & not_mask);
345 }
346}
347
Przemek Stekiel89ad6232022-09-27 13:36:12 +0200348#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gabor Mezeie2123792021-10-18 17:05:06 +0200349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350void mbedtls_ct_memcpy_if_eq(unsigned char *dest,
351 const unsigned char *src,
352 size_t len,
353 size_t c1,
354 size_t c2)
gabor-mezei-armdee0fd32021-09-27 13:34:25 +0200355{
356 /* mask = c1 == c2 ? 0xff : 0x00 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 const size_t equal = mbedtls_ct_size_bool_eq(c1, c2);
gabor-mezei-armdee0fd32021-09-27 13:34:25 +0200358
gabor-mezei-arm87ac5be2021-08-10 20:36:09 +0200359 /* dest[i] = c1 == c2 ? src[i] : dest[i] */
Dave Rodgman36dfc5a2022-12-22 15:04:43 +0000360 size_t i = 0;
361#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS)
362 const uint32_t mask32 = (uint32_t) mbedtls_ct_size_mask(equal);
363 const unsigned char mask = (unsigned char) mask32 & 0xff;
364
365 for (; (i + 4) <= len; i += 4) {
366 uint32_t a = mbedtls_get_unaligned_uint32(src + i) & mask32;
367 uint32_t b = mbedtls_get_unaligned_uint32(dest + i) & ~mask32;
368 mbedtls_put_unaligned_uint32(dest + i, a | b);
369 }
370#else
371 const unsigned char mask = (unsigned char) mbedtls_ct_size_mask(equal);
372#endif /* MBEDTLS_EFFICIENT_UNALIGNED_ACCESS */
373 for (; i < len; i++) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 dest[i] = (src[i] & mask) | (dest[i] & ~mask);
375 }
gabor-mezei-armdee0fd32021-09-27 13:34:25 +0200376}
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200377
Gilles Peskine449bd832023-01-11 14:50:10 +0100378void mbedtls_ct_memcpy_offset(unsigned char *dest,
379 const unsigned char *src,
380 size_t offset,
381 size_t offset_min,
382 size_t offset_max,
383 size_t len)
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200384{
Gabor Mezei63bbba52021-10-18 16:17:57 +0200385 size_t offsetval;
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200386
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 for (offsetval = offset_min; offsetval <= offset_max; offsetval++) {
388 mbedtls_ct_memcpy_if_eq(dest, src + offsetval, len,
389 offsetval, offset);
gabor-mezei-arm0e7f71e2021-09-27 13:57:45 +0200390 }
391}
gabor-mezei-arm1349ffd2021-09-27 14:28:31 +0200392
Przemek Stekiel89ad6232022-09-27 13:36:12 +0200393#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
gabor-mezei-arm40a49252021-09-27 15:33:35 +0200394
Dave Rodgmandebf8672023-05-17 12:12:44 +0100395#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
396
397void mbedtls_ct_zeroize_if(mbedtls_ct_condition_t condition, void *buf, size_t len)
398{
399 uint32_t mask = (uint32_t) ~condition;
400 uint8_t *p = (uint8_t *) buf;
401 size_t i = 0;
402#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS)
403 for (; (i + 4) <= len; i += 4) {
404 mbedtls_put_unaligned_uint32((void *) (p + i),
405 mbedtls_get_unaligned_uint32((void *) (p + i)) & mask);
406 }
407#endif
408 for (; i < len; i++) {
409 p[i] = p[i] & mask;
410 }
411}
412
413#endif /* defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT) */