2021-07-12 16:31:22 +02:00
|
|
|
/**
|
|
|
|
* Constant-time functions
|
|
|
|
*
|
|
|
|
* Copyright The Mbed TLS Contributors
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
/*
|
2021-11-03 16:13:32 +01:00
|
|
|
* The following functions are implemented without using comparison operators, as those
|
2021-10-18 16:09:41 +02:00
|
|
|
* might be translated to branches by some compilers on some platforms.
|
|
|
|
*/
|
|
|
|
|
2023-05-17 11:59:56 +01:00
|
|
|
#include <limits.h>
|
|
|
|
|
2021-07-12 16:31:22 +02:00
|
|
|
#include "common.h"
|
2021-10-20 12:09:35 +02:00
|
|
|
#include "constant_time_internal.h"
|
2021-10-19 12:22:25 +02:00
|
|
|
#include "mbedtls/constant_time.h"
|
2021-09-27 14:28:31 +02:00
|
|
|
#include "mbedtls/error.h"
|
2021-09-29 10:50:31 +02:00
|
|
|
#include "mbedtls/platform_util.h"
|
2021-09-27 11:28:54 +02:00
|
|
|
|
2021-09-27 14:28:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS_C)
|
|
|
|
#include "ssl_misc.h"
|
|
|
|
#endif
|
|
|
|
|
2021-09-29 10:50:31 +02:00
|
|
|
#if defined(MBEDTLS_RSA_C)
|
|
|
|
#include "mbedtls/rsa.h"
|
|
|
|
#endif
|
|
|
|
|
2021-09-27 16:11:12 +02:00
|
|
|
#include <string.h>
|
2022-12-23 11:00:06 -05:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
|
|
|
|
psa_to_ssl_errors, \
|
|
|
|
psa_generic_status_to_mbedtls)
|
|
|
|
#endif
|
2021-09-27 12:55:33 +02:00
|
|
|
|
2022-12-30 21:25:35 +00:00
|
|
|
/*
|
|
|
|
* Define MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS where assembly is present to
|
|
|
|
* perform fast unaligned access to volatile data.
|
2022-12-22 15:04:43 +00:00
|
|
|
*
|
|
|
|
* This is needed because mbedtls_get_unaligned_uintXX etc don't support volatile
|
|
|
|
* memory accesses.
|
|
|
|
*
|
2022-12-30 21:25:35 +00:00
|
|
|
* Some of these definitions could be moved into alignment.h but for now they are
|
|
|
|
* only used here.
|
2022-12-22 15:04:43 +00:00
|
|
|
*/
|
2023-05-17 11:59:56 +01:00
|
|
|
#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS) && \
|
|
|
|
(defined(MBEDTLS_CT_ARM_ASM) || defined(MBEDTLS_CT_AARCH64_ASM))
|
|
|
|
|
2022-12-30 21:25:35 +00:00
|
|
|
#define MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS
|
2022-12-22 15:04:43 +00:00
|
|
|
|
|
|
|
static inline uint32_t mbedtls_get_unaligned_volatile_uint32(volatile const unsigned char *p)
|
|
|
|
{
|
|
|
|
/* This is UB, even where it's safe:
|
|
|
|
* return *((volatile uint32_t*)p);
|
|
|
|
* so instead the same thing is expressed in assembly below.
|
|
|
|
*/
|
|
|
|
uint32_t r;
|
2023-05-17 11:59:56 +01:00
|
|
|
#if defined(MBEDTLS_CT_ARM_ASM)
|
2023-01-30 09:26:48 +00:00
|
|
|
asm volatile ("ldr %0, [%1]" : "=r" (r) : "r" (p) :);
|
2023-05-17 11:59:56 +01:00
|
|
|
#elif defined(MBEDTLS_CT_AARCH64_ASM)
|
2023-01-30 09:26:48 +00:00
|
|
|
asm volatile ("ldr %w0, [%1]" : "=r" (r) : "r" (p) :);
|
2023-05-17 11:59:56 +01:00
|
|
|
#else
|
|
|
|
#error No assembly defined for mbedtls_get_unaligned_volatile_uint32
|
2022-12-22 15:04:43 +00:00
|
|
|
#endif
|
2022-12-30 21:25:35 +00:00
|
|
|
return r;
|
2022-12-22 15:04:43 +00:00
|
|
|
}
|
2023-05-17 11:59:56 +01:00
|
|
|
#endif /* defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS) &&
|
|
|
|
(defined(MBEDTLS_CT_ARM_ASM) || defined(MBEDTLS_CT_AARCH64_ASM)) */
|
2022-12-22 15:04:43 +00:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ct_memcmp(const void *a,
|
|
|
|
const void *b,
|
|
|
|
size_t n)
|
2021-09-27 11:28:54 +02:00
|
|
|
{
|
2022-12-22 15:04:43 +00:00
|
|
|
size_t i = 0;
|
2023-01-11 17:39:33 +00:00
|
|
|
/*
|
|
|
|
* `A` and `B` are cast to volatile to ensure that the compiler
|
|
|
|
* generates code that always fully reads both buffers.
|
|
|
|
* Otherwise it could generate a test to exit early if `diff` has all
|
|
|
|
* bits set early in the loop.
|
|
|
|
*/
|
2021-09-27 11:28:54 +02:00
|
|
|
volatile const unsigned char *A = (volatile const unsigned char *) a;
|
|
|
|
volatile const unsigned char *B = (volatile const unsigned char *) b;
|
2023-01-11 17:39:33 +00:00
|
|
|
uint32_t diff = 0;
|
2021-09-27 11:28:54 +02:00
|
|
|
|
2022-12-30 21:25:35 +00:00
|
|
|
#if defined(MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS)
|
2022-12-22 15:04:43 +00:00
|
|
|
for (; (i + 4) <= n; i += 4) {
|
|
|
|
uint32_t x = mbedtls_get_unaligned_volatile_uint32(A + i);
|
|
|
|
uint32_t y = mbedtls_get_unaligned_volatile_uint32(B + i);
|
|
|
|
diff |= x ^ y;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for (; i < n; i++) {
|
2021-09-27 11:28:54 +02:00
|
|
|
/* Read volatile data in order before computing diff.
|
|
|
|
* This avoids IAR compiler warning:
|
|
|
|
* 'the order of volatile accesses is undefined ..' */
|
|
|
|
unsigned char x = A[i], y = B[i];
|
|
|
|
diff |= x ^ y;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return (int) diff;
|
2021-09-27 11:28:54 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned mbedtls_ct_uint_mask(unsigned value)
|
2021-09-27 11:40:03 +02:00
|
|
|
{
|
|
|
|
/* MSVC has a warning about unary minus on unsigned, but this is
|
|
|
|
* well-defined and precisely what we want to do here */
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning( push )
|
|
|
|
#pragma warning( disable : 4146 )
|
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
return -((value | -value) >> (sizeof(value) * 8 - 1));
|
2021-09-27 11:40:03 +02:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning( pop )
|
|
|
|
#endif
|
|
|
|
}
|
2021-09-27 11:49:42 +02:00
|
|
|
|
2022-09-27 13:36:12 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
|
2021-10-20 11:17:43 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t mbedtls_ct_size_mask(size_t value)
|
2021-09-27 11:49:42 +02:00
|
|
|
{
|
|
|
|
/* MSVC has a warning about unary minus on unsigned integer types,
|
|
|
|
* but this is well-defined and precisely what we want to do here. */
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning( push )
|
|
|
|
#pragma warning( disable : 4146 )
|
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
return -((value | -value) >> (sizeof(value) * 8 - 1));
|
2021-09-27 11:49:42 +02:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning( pop )
|
|
|
|
#endif
|
|
|
|
}
|
2021-09-27 11:53:54 +02:00
|
|
|
|
2022-09-27 13:36:12 +02:00
|
|
|
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
|
2021-10-20 11:17:43 +02:00
|
|
|
|
2021-08-11 15:07:02 +02:00
|
|
|
#if defined(MBEDTLS_BIGNUM_C)
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_uint mbedtls_ct_mpi_uint_mask(mbedtls_mpi_uint value)
|
2021-08-11 15:07:02 +02:00
|
|
|
{
|
|
|
|
/* MSVC has a warning about unary minus on unsigned, but this is
|
|
|
|
* well-defined and precisely what we want to do here */
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning( push )
|
|
|
|
#pragma warning( disable : 4146 )
|
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
return -((value | -value) >> (sizeof(value) * 8 - 1));
|
2021-08-11 15:07:02 +02:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning( pop )
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MBEDTLS_BIGNUM_C */
|
|
|
|
|
2021-10-18 17:05:06 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
|
|
|
|
|
2021-10-18 16:05:50 +02:00
|
|
|
/** Constant-flow mask generation for "less than" comparison:
|
|
|
|
* - if \p x < \p y, return all-bits 1, that is (size_t) -1
|
|
|
|
* - otherwise, return all bits 0, that is 0
|
|
|
|
*
|
|
|
|
* This function can be used to write constant-time code by replacing branches
|
|
|
|
* with bit operations using masks.
|
|
|
|
*
|
|
|
|
* \param x The first value to analyze.
|
|
|
|
* \param y The second value to analyze.
|
|
|
|
*
|
|
|
|
* \return All-bits-one if \p x is less than \p y, otherwise zero.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static size_t mbedtls_ct_size_mask_lt(size_t x,
|
|
|
|
size_t y)
|
2021-09-27 11:53:54 +02:00
|
|
|
{
|
|
|
|
/* This has the most significant bit set if and only if x < y */
|
|
|
|
const size_t sub = x - y;
|
|
|
|
|
|
|
|
/* sub1 = (x < y) ? 1 : 0 */
|
2023-01-11 14:50:10 +01:00
|
|
|
const size_t sub1 = sub >> (sizeof(sub) * 8 - 1);
|
2021-09-27 11:53:54 +02:00
|
|
|
|
|
|
|
/* mask = (x < y) ? 0xff... : 0x00... */
|
2023-01-11 14:50:10 +01:00
|
|
|
const size_t mask = mbedtls_ct_size_mask(sub1);
|
2021-09-27 11:53:54 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return mask;
|
2021-09-27 11:53:54 +02:00
|
|
|
}
|
2021-09-27 11:58:31 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t mbedtls_ct_size_mask_ge(size_t x,
|
|
|
|
size_t y)
|
2021-09-27 11:58:31 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return ~mbedtls_ct_size_mask_lt(x, y);
|
2021-09-27 11:58:31 +02:00
|
|
|
}
|
2021-09-27 12:15:19 +02:00
|
|
|
|
2021-10-18 17:05:06 +02:00
|
|
|
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
|
|
|
|
|
2021-11-15 16:13:01 +01:00
|
|
|
#if defined(MBEDTLS_BASE64_C)
|
|
|
|
|
|
|
|
/* Return 0xff if low <= c <= high, 0 otherwise.
|
|
|
|
*
|
|
|
|
* Constant flow with respect to c.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned char mbedtls_ct_uchar_mask_of_range(unsigned char low,
|
|
|
|
unsigned char high,
|
|
|
|
unsigned char c)
|
2021-11-15 16:13:01 +01:00
|
|
|
{
|
|
|
|
/* low_mask is: 0 if low <= c, 0x...ff if low > c */
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned low_mask = ((unsigned) c - low) >> 8;
|
2021-11-15 16:13:01 +01:00
|
|
|
/* high_mask is: 0 if c <= high, 0x...ff if c > high */
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned high_mask = ((unsigned) high - c) >> 8;
|
|
|
|
return ~(low_mask | high_mask) & 0xff;
|
2021-11-15 16:13:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MBEDTLS_BASE64_C */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned mbedtls_ct_size_bool_eq(size_t x,
|
|
|
|
size_t y)
|
2021-09-27 12:15:19 +02:00
|
|
|
{
|
|
|
|
/* diff = 0 if x == y, non-zero otherwise */
|
|
|
|
const size_t diff = x ^ y;
|
|
|
|
|
|
|
|
/* MSVC has a warning about unary minus on unsigned integer types,
|
|
|
|
* but this is well-defined and precisely what we want to do here. */
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning( push )
|
|
|
|
#pragma warning( disable : 4146 )
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* diff_msb's most significant bit is equal to x != y */
|
2023-01-11 14:50:10 +01:00
|
|
|
const size_t diff_msb = (diff | (size_t) -diff);
|
2021-09-27 12:15:19 +02:00
|
|
|
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning( pop )
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* diff1 = (x != y) ? 1 : 0 */
|
2023-01-11 14:50:10 +01:00
|
|
|
const unsigned diff1 = diff_msb >> (sizeof(diff_msb) * 8 - 1);
|
2021-09-27 12:15:19 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 1 ^ diff1;
|
2021-09-27 12:15:19 +02:00
|
|
|
}
|
2021-09-27 12:25:07 +02:00
|
|
|
|
2021-10-18 17:05:06 +02:00
|
|
|
#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
|
|
|
|
|
2023-05-09 11:09:52 +01:00
|
|
|
unsigned mbedtls_ct_size_gt(size_t x, size_t y)
|
2021-09-27 12:25:07 +02:00
|
|
|
{
|
2021-08-10 20:36:09 +02:00
|
|
|
/* Return the sign bit (1 for negative) of (y - x). */
|
2023-01-11 14:50:10 +01:00
|
|
|
return (y - x) >> (sizeof(size_t) * 8 - 1);
|
2021-09-27 12:25:07 +02:00
|
|
|
}
|
2021-09-27 12:55:33 +02:00
|
|
|
|
2021-10-18 17:05:06 +02:00
|
|
|
#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */
|
|
|
|
|
2021-09-27 12:55:33 +02:00
|
|
|
#if defined(MBEDTLS_BIGNUM_C)
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned mbedtls_ct_mpi_uint_lt(const mbedtls_mpi_uint x,
|
|
|
|
const mbedtls_mpi_uint y)
|
2021-09-27 12:55:33 +02:00
|
|
|
{
|
|
|
|
mbedtls_mpi_uint ret;
|
|
|
|
mbedtls_mpi_uint cond;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if the most significant bits (MSB) of the operands are different.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
cond = (x ^ y);
|
2021-09-27 12:55:33 +02:00
|
|
|
/*
|
|
|
|
* If the MSB are the same then the difference x-y will be negative (and
|
|
|
|
* have its MSB set to 1 during conversion to unsigned) if and only if x<y.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = (x - y) & ~cond;
|
2021-09-27 12:55:33 +02:00
|
|
|
/*
|
|
|
|
* If the MSB are different, then the operand with the MSB of 1 is the
|
|
|
|
* bigger. (That is if y has MSB of 1, then x<y is true and it is false if
|
|
|
|
* the MSB of y is 0.)
|
|
|
|
*/
|
|
|
|
ret |= y & cond;
|
|
|
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ret >> (sizeof(mbedtls_mpi_uint) * 8 - 1);
|
2021-09-27 12:55:33 +02:00
|
|
|
|
|
|
|
return (unsigned) ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MBEDTLS_BIGNUM_C */
|
2021-09-27 12:59:30 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned mbedtls_ct_uint_if(unsigned condition,
|
|
|
|
unsigned if1,
|
|
|
|
unsigned if0)
|
2021-09-27 12:59:30 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned mask = mbedtls_ct_uint_mask(condition);
|
|
|
|
return (mask & if1) | (~mask & if0);
|
2021-09-27 12:59:30 +02:00
|
|
|
}
|
2021-09-27 13:03:57 +02:00
|
|
|
|
2021-10-18 17:05:06 +02:00
|
|
|
#if defined(MBEDTLS_BIGNUM_C)
|
2021-09-27 15:47:00 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ct_mpi_uint_cond_assign(size_t n,
|
|
|
|
mbedtls_mpi_uint *dest,
|
|
|
|
const mbedtls_mpi_uint *src,
|
|
|
|
unsigned char condition)
|
2021-09-27 13:17:15 +02:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
/* MSVC has a warning about unary minus on unsigned integer types,
|
|
|
|
* but this is well-defined and precisely what we want to do here. */
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning( push )
|
|
|
|
#pragma warning( disable : 4146 )
|
|
|
|
#endif
|
|
|
|
|
2021-08-10 20:36:09 +02:00
|
|
|
/* all-bits 1 if condition is 1, all-bits 0 if condition is 0 */
|
|
|
|
const mbedtls_mpi_uint mask = -condition;
|
2021-09-27 13:17:15 +02:00
|
|
|
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning( pop )
|
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
dest[i] = (src[i] & mask) | (dest[i] & ~mask);
|
|
|
|
}
|
2021-09-27 13:17:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MBEDTLS_BIGNUM_C */
|
2021-09-27 13:31:06 +02:00
|
|
|
|
2021-10-18 17:05:06 +02:00
|
|
|
#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
|
|
|
|
|
2023-05-17 12:16:29 +01:00
|
|
|
void mbedtls_ct_memmove_left(void *start,
|
2023-05-09 11:10:21 +01:00
|
|
|
size_t total,
|
|
|
|
size_t offset)
|
2021-09-27 13:31:06 +02:00
|
|
|
{
|
|
|
|
volatile unsigned char *buf = start;
|
|
|
|
size_t i, n;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (total == 0) {
|
2021-09-27 13:31:06 +02:00
|
|
|
return;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
for (i = 0; i < total; i++) {
|
|
|
|
unsigned no_op = mbedtls_ct_size_gt(total - offset, i);
|
2021-09-27 13:31:06 +02:00
|
|
|
/* The first `total - offset` passes are a no-op. The last
|
|
|
|
* `offset` passes shift the data one byte to the left and
|
|
|
|
* zero out the last byte. */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (n = 0; n < total - 1; n++) {
|
2021-09-27 13:31:06 +02:00
|
|
|
unsigned char current = buf[n];
|
|
|
|
unsigned char next = buf[n+1];
|
2023-01-11 14:50:10 +01:00
|
|
|
buf[n] = mbedtls_ct_uint_if(no_op, current, next);
|
2021-09-27 13:31:06 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
buf[total-1] = mbedtls_ct_uint_if(no_op, buf[total-1], 0);
|
2021-09-27 13:31:06 +02:00
|
|
|
}
|
|
|
|
}
|
2021-09-27 13:34:25 +02:00
|
|
|
|
2021-10-18 17:05:06 +02:00
|
|
|
#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */
|
|
|
|
|
2022-09-27 13:36:12 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
|
2021-10-18 17:05:06 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ct_memcpy_if_eq(unsigned char *dest,
|
|
|
|
const unsigned char *src,
|
|
|
|
size_t len,
|
|
|
|
size_t c1,
|
|
|
|
size_t c2)
|
2021-09-27 13:34:25 +02:00
|
|
|
{
|
|
|
|
/* mask = c1 == c2 ? 0xff : 0x00 */
|
2023-01-11 14:50:10 +01:00
|
|
|
const size_t equal = mbedtls_ct_size_bool_eq(c1, c2);
|
2021-09-27 13:34:25 +02:00
|
|
|
|
2021-08-10 20:36:09 +02:00
|
|
|
/* dest[i] = c1 == c2 ? src[i] : dest[i] */
|
2022-12-22 15:04:43 +00:00
|
|
|
size_t i = 0;
|
|
|
|
#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS)
|
|
|
|
const uint32_t mask32 = (uint32_t) mbedtls_ct_size_mask(equal);
|
|
|
|
const unsigned char mask = (unsigned char) mask32 & 0xff;
|
|
|
|
|
|
|
|
for (; (i + 4) <= len; i += 4) {
|
|
|
|
uint32_t a = mbedtls_get_unaligned_uint32(src + i) & mask32;
|
|
|
|
uint32_t b = mbedtls_get_unaligned_uint32(dest + i) & ~mask32;
|
|
|
|
mbedtls_put_unaligned_uint32(dest + i, a | b);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
const unsigned char mask = (unsigned char) mbedtls_ct_size_mask(equal);
|
|
|
|
#endif /* MBEDTLS_EFFICIENT_UNALIGNED_ACCESS */
|
|
|
|
for (; i < len; i++) {
|
2023-01-11 14:50:10 +01:00
|
|
|
dest[i] = (src[i] & mask) | (dest[i] & ~mask);
|
|
|
|
}
|
2021-09-27 13:34:25 +02:00
|
|
|
}
|
2021-09-27 13:57:45 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ct_memcpy_offset(unsigned char *dest,
|
|
|
|
const unsigned char *src,
|
|
|
|
size_t offset,
|
|
|
|
size_t offset_min,
|
|
|
|
size_t offset_max,
|
|
|
|
size_t len)
|
2021-09-27 13:57:45 +02:00
|
|
|
{
|
2021-10-18 16:17:57 +02:00
|
|
|
size_t offsetval;
|
2021-09-27 13:57:45 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (offsetval = offset_min; offsetval <= offset_max; offsetval++) {
|
|
|
|
mbedtls_ct_memcpy_if_eq(dest, src + offsetval, len,
|
|
|
|
offsetval, offset);
|
2021-09-27 13:57:45 +02:00
|
|
|
}
|
|
|
|
}
|
2021-09-27 14:28:31 +02:00
|
|
|
|
2022-09-27 13:36:12 +02:00
|
|
|
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
|
2021-09-27 15:33:35 +02:00
|
|
|
|
2023-05-17 12:12:44 +01:00
|
|
|
#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
|
|
|
|
|
|
|
|
void mbedtls_ct_zeroize_if(mbedtls_ct_condition_t condition, void *buf, size_t len)
|
|
|
|
{
|
|
|
|
uint32_t mask = (uint32_t) ~condition;
|
|
|
|
uint8_t *p = (uint8_t *) buf;
|
|
|
|
size_t i = 0;
|
|
|
|
#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS)
|
|
|
|
for (; (i + 4) <= len; i += 4) {
|
|
|
|
mbedtls_put_unaligned_uint32((void *) (p + i),
|
|
|
|
mbedtls_get_unaligned_uint32((void *) (p + i)) & mask);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
for (; i < len; i++) {
|
|
|
|
p[i] = p[i] & mask;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT) */
|