2023-05-17 12:59:56 +02:00
|
|
|
/**
|
|
|
|
* Constant-time functions
|
|
|
|
*
|
|
|
|
* Copyright The Mbed TLS Contributors
|
2023-11-02 20:47:20 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2023-05-17 12:59:56 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MBEDTLS_CONSTANT_TIME_IMPL_H
|
|
|
|
#define MBEDTLS_CONSTANT_TIME_IMPL_H
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_BIGNUM_C)
|
|
|
|
#include "mbedtls/bignum.h"
|
|
|
|
#endif
|
|
|
|
|
2023-08-09 15:10:14 +02:00
|
|
|
/*
|
|
|
|
* To improve readability of constant_time_internal.h, the static inline
|
|
|
|
* definitions are here, and constant_time_internal.h has only the declarations.
|
|
|
|
*
|
|
|
|
* This results in duplicate declarations of the form:
|
|
|
|
* static inline void f(); // from constant_time_internal.h
|
|
|
|
* static inline void f() { ... } // from constant_time_impl.h
|
|
|
|
* when constant_time_internal.h is included.
|
|
|
|
*
|
|
|
|
* This appears to behave as if the declaration-without-definition was not present
|
|
|
|
* (except for warnings if gcc -Wredundant-decls or similar is used).
|
2023-08-01 15:10:56 +02:00
|
|
|
*
|
2023-08-09 15:10:14 +02:00
|
|
|
* Disable -Wredundant-decls so that gcc does not warn about this. This is re-enabled
|
|
|
|
* at the bottom of this file.
|
2023-08-01 15:10:56 +02:00
|
|
|
*/
|
|
|
|
#ifdef __GNUC__
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wredundant-decls"
|
|
|
|
#endif
|
|
|
|
|
2023-07-31 19:07:44 +02:00
|
|
|
/* Disable asm under Memsan because it confuses Memsan and generates false errors.
|
|
|
|
*
|
|
|
|
* We also disable under Valgrind by default, because it's more useful
|
|
|
|
* for Valgrind to test the plain C implementation. MBEDTLS_TEST_CONSTANT_FLOW_ASM //no-check-names
|
|
|
|
* may be set to permit building asm under Valgrind.
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN) || \
|
|
|
|
(defined(MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND) && !defined(MBEDTLS_TEST_CONSTANT_FLOW_ASM)) //no-check-names
|
2023-07-31 17:54:00 +02:00
|
|
|
#define MBEDTLS_CT_NO_ASM
|
|
|
|
#elif defined(__has_feature)
|
|
|
|
#if __has_feature(memory_sanitizer)
|
|
|
|
#define MBEDTLS_CT_NO_ASM
|
|
|
|
#endif
|
|
|
|
#endif
|
2023-05-17 12:59:56 +02:00
|
|
|
|
|
|
|
/* armcc5 --gnu defines __GNUC__ but doesn't support GNU's extended asm */
|
|
|
|
#if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && (!defined(__ARMCC_VERSION) || \
|
2023-07-31 17:54:00 +02:00
|
|
|
__ARMCC_VERSION >= 6000000) && !defined(MBEDTLS_CT_NO_ASM)
|
2023-05-17 12:59:56 +02:00
|
|
|
#define MBEDTLS_CT_ASM
|
|
|
|
#if (defined(__arm__) || defined(__thumb__) || defined(__thumb2__))
|
|
|
|
#define MBEDTLS_CT_ARM_ASM
|
|
|
|
#elif defined(__aarch64__)
|
|
|
|
#define MBEDTLS_CT_AARCH64_ASM
|
2023-05-12 13:11:37 +02:00
|
|
|
#elif defined(__amd64__) || defined(__x86_64__)
|
|
|
|
#define MBEDTLS_CT_X86_64_ASM
|
2023-05-13 13:32:09 +02:00
|
|
|
#elif defined(__i386__)
|
|
|
|
#define MBEDTLS_CT_X86_ASM
|
2023-05-17 12:59:56 +02:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MBEDTLS_CT_SIZE (sizeof(mbedtls_ct_uint_t) * 8)
|
|
|
|
|
|
|
|
|
|
|
|
/* ============================================================================
|
|
|
|
* Core const-time primitives
|
|
|
|
*/
|
|
|
|
|
2023-06-08 18:52:21 +02:00
|
|
|
/* Ensure that the compiler cannot know the value of x (i.e., cannot optimise
|
2023-05-17 12:59:56 +02:00
|
|
|
* based on its value) after this function is called.
|
|
|
|
*
|
|
|
|
* If we are not using assembly, this will be fairly inefficient, so its use
|
|
|
|
* should be minimised.
|
|
|
|
*/
|
2023-06-08 18:52:21 +02:00
|
|
|
|
|
|
|
#if !defined(MBEDTLS_CT_ASM)
|
2023-06-12 19:19:46 +02:00
|
|
|
extern volatile mbedtls_ct_uint_t mbedtls_ct_zero;
|
2023-06-08 18:52:21 +02:00
|
|
|
#endif
|
|
|
|
|
2023-07-31 13:30:26 +02:00
|
|
|
/**
|
|
|
|
* \brief Ensure that a value cannot be known at compile time.
|
|
|
|
*
|
|
|
|
* \param x The value to hide from the compiler.
|
|
|
|
* \return The same value that was passed in, such that the compiler
|
|
|
|
* cannot prove its value (even for calls of the form
|
|
|
|
* x = mbedtls_ct_compiler_opaque(1), x will be unknown).
|
|
|
|
*
|
|
|
|
* \note This is mainly used in constructing mbedtls_ct_condition_t
|
|
|
|
* values and performing operations over them, to ensure that
|
|
|
|
* there is no way for the compiler to ever know anything about
|
|
|
|
* the value of an mbedtls_ct_condition_t.
|
|
|
|
*/
|
2023-05-17 12:59:56 +02:00
|
|
|
static inline mbedtls_ct_uint_t mbedtls_ct_compiler_opaque(mbedtls_ct_uint_t x)
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_CT_ASM)
|
|
|
|
asm volatile ("" : [x] "+r" (x) :);
|
|
|
|
return x;
|
|
|
|
#else
|
2023-06-08 18:52:21 +02:00
|
|
|
return x ^ mbedtls_ct_zero;
|
2023-05-17 12:59:56 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-08-21 08:54:11 +02:00
|
|
|
/*
|
|
|
|
* Selecting unified syntax is needed for gcc, and harmless on clang.
|
|
|
|
*
|
|
|
|
* This is needed because on Thumb 1, condition flags are always set, so
|
|
|
|
* e.g. "negs" is supported but "neg" is not (on Thumb 2, both exist).
|
|
|
|
*
|
|
|
|
* Under Thumb 1 unified syntax, only the "negs" form is accepted, and
|
|
|
|
* under divided syntax, only the "neg" form is accepted. clang only
|
|
|
|
* supports unified syntax.
|
|
|
|
*
|
|
|
|
* On Thumb 2 and Arm, both compilers are happy with the "s" suffix,
|
|
|
|
* although we don't actually care about setting the flags.
|
|
|
|
*
|
|
|
|
* For gcc, restore divided syntax afterwards - otherwise old versions of gcc
|
|
|
|
* seem to apply unified syntax globally, which breaks other asm code.
|
|
|
|
*/
|
|
|
|
#if !defined(__clang__)
|
2023-05-12 17:29:48 +02:00
|
|
|
#define RESTORE_ASM_SYNTAX ".syntax divided \n\t"
|
2023-08-21 08:54:11 +02:00
|
|
|
#else
|
|
|
|
#define RESTORE_ASM_SYNTAX
|
|
|
|
#endif
|
|
|
|
|
2023-05-17 12:59:56 +02:00
|
|
|
/* Convert a number into a condition in constant time. */
|
|
|
|
static inline mbedtls_ct_condition_t mbedtls_ct_bool(mbedtls_ct_uint_t x)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Define mask-generation code that, as far as possible, will not use branches or conditional instructions.
|
|
|
|
*
|
|
|
|
* For some platforms / type sizes, we define assembly to assure this.
|
|
|
|
*
|
|
|
|
* Otherwise, we define a plain C fallback which (in May 2023) does not get optimised into
|
|
|
|
* conditional instructions or branches by trunk clang, gcc, or MSVC v19.
|
|
|
|
*/
|
2023-05-13 13:47:02 +02:00
|
|
|
#if defined(MBEDTLS_CT_AARCH64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
|
|
|
|
mbedtls_ct_uint_t s;
|
2023-05-12 17:29:48 +02:00
|
|
|
asm volatile ("neg %x[s], %x[x] \n\t"
|
|
|
|
"orr %x[x], %x[s], %x[x] \n\t"
|
|
|
|
"asr %x[x], %x[x], 63 \n\t"
|
2023-05-13 13:47:02 +02:00
|
|
|
:
|
|
|
|
[s] "=&r" (s),
|
|
|
|
[x] "+&r" (x)
|
|
|
|
:
|
|
|
|
:
|
|
|
|
);
|
|
|
|
return (mbedtls_ct_condition_t) x;
|
2023-05-13 13:48:02 +02:00
|
|
|
#elif defined(MBEDTLS_CT_ARM_ASM) && defined(MBEDTLS_CT_SIZE_32)
|
|
|
|
uint32_t s;
|
2023-05-12 17:29:48 +02:00
|
|
|
asm volatile (".syntax unified \n\t"
|
|
|
|
"negs %[s], %[x] \n\t"
|
|
|
|
"orrs %[x], %[x], %[s] \n\t"
|
|
|
|
"asrs %[x], %[x], #31 \n\t"
|
2023-06-12 16:38:49 +02:00
|
|
|
RESTORE_ASM_SYNTAX
|
2023-05-13 13:48:02 +02:00
|
|
|
:
|
|
|
|
[s] "=&l" (s),
|
|
|
|
[x] "+&l" (x)
|
|
|
|
:
|
|
|
|
:
|
2023-06-12 16:38:49 +02:00
|
|
|
"cc" /* clobbers flag bits */
|
2023-05-13 13:48:02 +02:00
|
|
|
);
|
|
|
|
return (mbedtls_ct_condition_t) x;
|
2023-05-12 13:11:37 +02:00
|
|
|
#elif defined(MBEDTLS_CT_X86_64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
|
|
|
|
uint64_t s;
|
|
|
|
asm volatile ("mov %[x], %[s] \n\t"
|
|
|
|
"neg %[s] \n\t"
|
|
|
|
"or %[x], %[s] \n\t"
|
|
|
|
"sar $63, %[s] \n\t"
|
|
|
|
:
|
|
|
|
[s] "=&a" (s)
|
|
|
|
:
|
|
|
|
[x] "D" (x)
|
|
|
|
:
|
|
|
|
);
|
|
|
|
return (mbedtls_ct_condition_t) s;
|
2023-05-13 13:32:09 +02:00
|
|
|
#elif defined(MBEDTLS_CT_X86_ASM) && defined(MBEDTLS_CT_SIZE_32)
|
|
|
|
uint32_t s;
|
|
|
|
asm volatile ("mov %[x], %[s] \n\t"
|
|
|
|
"neg %[s] \n\t"
|
|
|
|
"or %[s], %[x] \n\t"
|
|
|
|
"sar $31, %[x] \n\t"
|
|
|
|
:
|
|
|
|
[s] "=&c" (s),
|
|
|
|
[x] "+&a" (x)
|
|
|
|
:
|
|
|
|
:
|
|
|
|
);
|
|
|
|
return (mbedtls_ct_condition_t) x;
|
2023-05-13 13:47:02 +02:00
|
|
|
#else
|
2023-05-17 12:59:56 +02:00
|
|
|
const mbedtls_ct_uint_t xo = mbedtls_ct_compiler_opaque(x);
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
/* MSVC has a warning about unary minus on unsigned, but this is
|
|
|
|
* well-defined and precisely what we want to do here */
|
|
|
|
#pragma warning( push )
|
|
|
|
#pragma warning( disable : 4146 )
|
|
|
|
#endif
|
2023-08-21 18:06:24 +02:00
|
|
|
// y is negative (i.e., top bit set) iff x is non-zero
|
|
|
|
mbedtls_ct_int_t y = (-xo) | -(xo >> 1);
|
|
|
|
|
|
|
|
// extract only the sign bit of y so that y == 1 (if x is non-zero) or 0 (if x is zero)
|
|
|
|
y = (((mbedtls_ct_uint_t) y) >> (MBEDTLS_CT_SIZE - 1));
|
|
|
|
|
|
|
|
// -y has all bits set (if x is non-zero), or all bits clear (if x is zero)
|
|
|
|
return (mbedtls_ct_condition_t) (-y);
|
2023-05-17 12:59:56 +02:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning( pop )
|
|
|
|
#endif
|
2023-05-13 13:47:02 +02:00
|
|
|
#endif
|
2023-05-17 12:59:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline mbedtls_ct_uint_t mbedtls_ct_if(mbedtls_ct_condition_t condition,
|
|
|
|
mbedtls_ct_uint_t if1,
|
|
|
|
mbedtls_ct_uint_t if0)
|
|
|
|
{
|
2023-05-13 13:47:02 +02:00
|
|
|
#if defined(MBEDTLS_CT_AARCH64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
|
2023-05-12 17:29:48 +02:00
|
|
|
asm volatile ("and %x[if1], %x[if1], %x[condition] \n\t"
|
|
|
|
"mvn %x[condition], %x[condition] \n\t"
|
|
|
|
"and %x[condition], %x[condition], %x[if0] \n\t"
|
2023-05-13 13:47:02 +02:00
|
|
|
"orr %x[condition], %x[if1], %x[condition]"
|
|
|
|
:
|
|
|
|
[condition] "+&r" (condition),
|
|
|
|
[if1] "+&r" (if1)
|
|
|
|
:
|
|
|
|
[if0] "r" (if0)
|
|
|
|
:
|
|
|
|
);
|
|
|
|
return (mbedtls_ct_uint_t) condition;
|
2023-05-13 13:48:02 +02:00
|
|
|
#elif defined(MBEDTLS_CT_ARM_ASM) && defined(MBEDTLS_CT_SIZE_32)
|
2023-05-12 17:29:48 +02:00
|
|
|
asm volatile (".syntax unified \n\t"
|
|
|
|
"ands %[if1], %[if1], %[condition] \n\t"
|
|
|
|
"mvns %[condition], %[condition] \n\t"
|
|
|
|
"ands %[condition], %[condition], %[if0] \n\t"
|
|
|
|
"orrs %[condition], %[if1], %[condition] \n\t"
|
2023-06-12 16:38:49 +02:00
|
|
|
RESTORE_ASM_SYNTAX
|
2023-05-13 13:48:02 +02:00
|
|
|
:
|
|
|
|
[condition] "+&l" (condition),
|
|
|
|
[if1] "+&l" (if1)
|
|
|
|
:
|
|
|
|
[if0] "l" (if0)
|
|
|
|
:
|
2023-06-12 16:38:49 +02:00
|
|
|
"cc"
|
2023-05-13 13:48:02 +02:00
|
|
|
);
|
|
|
|
return (mbedtls_ct_uint_t) condition;
|
2023-05-12 13:11:37 +02:00
|
|
|
#elif defined(MBEDTLS_CT_X86_64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
|
|
|
|
asm volatile ("and %[condition], %[if1] \n\t"
|
|
|
|
"not %[condition] \n\t"
|
|
|
|
"and %[condition], %[if0] \n\t"
|
|
|
|
"or %[if1], %[if0] \n\t"
|
|
|
|
:
|
|
|
|
[condition] "+&D" (condition),
|
|
|
|
[if1] "+&S" (if1),
|
|
|
|
[if0] "+&a" (if0)
|
|
|
|
:
|
|
|
|
:
|
|
|
|
);
|
|
|
|
return if0;
|
2023-05-13 13:32:09 +02:00
|
|
|
#elif defined(MBEDTLS_CT_X86_ASM) && defined(MBEDTLS_CT_SIZE_32)
|
|
|
|
asm volatile ("and %[condition], %[if1] \n\t"
|
|
|
|
"not %[condition] \n\t"
|
|
|
|
"and %[if0], %[condition] \n\t"
|
|
|
|
"or %[condition], %[if1] \n\t"
|
|
|
|
:
|
|
|
|
[condition] "+&c" (condition),
|
|
|
|
[if1] "+&a" (if1)
|
|
|
|
:
|
|
|
|
[if0] "b" (if0)
|
|
|
|
:
|
|
|
|
);
|
|
|
|
return if1;
|
2023-05-13 13:47:02 +02:00
|
|
|
#else
|
2023-05-17 13:22:59 +02:00
|
|
|
mbedtls_ct_condition_t not_cond =
|
2023-05-17 12:59:56 +02:00
|
|
|
(mbedtls_ct_condition_t) (~mbedtls_ct_compiler_opaque(condition));
|
2023-05-17 13:22:59 +02:00
|
|
|
return (mbedtls_ct_uint_t) ((condition & if1) | (not_cond & if0));
|
2023-05-13 13:47:02 +02:00
|
|
|
#endif
|
2023-05-17 12:59:56 +02:00
|
|
|
}
|
|
|
|
|
2023-08-10 12:58:18 +02:00
|
|
|
static inline mbedtls_ct_condition_t mbedtls_ct_uint_lt(mbedtls_ct_uint_t x, mbedtls_ct_uint_t y)
|
2023-05-17 12:59:56 +02:00
|
|
|
{
|
2023-05-13 13:47:02 +02:00
|
|
|
#if defined(MBEDTLS_CT_AARCH64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
|
2023-08-21 08:58:50 +02:00
|
|
|
uint64_t s1;
|
2023-05-12 17:29:48 +02:00
|
|
|
asm volatile ("eor %x[s1], %x[y], %x[x] \n\t"
|
|
|
|
"sub %x[x], %x[x], %x[y] \n\t"
|
|
|
|
"bic %x[x], %x[x], %x[s1] \n\t"
|
|
|
|
"and %x[s1], %x[s1], %x[y] \n\t"
|
|
|
|
"orr %x[s1], %x[x], %x[s1] \n\t"
|
2023-05-13 13:47:02 +02:00
|
|
|
"asr %x[x], %x[s1], 63"
|
2023-05-12 17:29:48 +02:00
|
|
|
:
|
|
|
|
[s1] "=&r" (s1),
|
|
|
|
[x] "+&r" (x)
|
|
|
|
:
|
|
|
|
[y] "r" (y)
|
2023-05-13 13:47:02 +02:00
|
|
|
:
|
|
|
|
);
|
|
|
|
return (mbedtls_ct_condition_t) x;
|
2023-05-13 13:48:02 +02:00
|
|
|
#elif defined(MBEDTLS_CT_ARM_ASM) && defined(MBEDTLS_CT_SIZE_32)
|
|
|
|
uint32_t s1;
|
|
|
|
asm volatile (
|
2023-05-12 17:29:48 +02:00
|
|
|
".syntax unified \n\t"
|
2023-05-13 13:48:02 +02:00
|
|
|
#if defined(__thumb__) && !defined(__thumb2__)
|
2023-05-12 17:29:48 +02:00
|
|
|
"movs %[s1], %[x] \n\t"
|
|
|
|
"eors %[s1], %[s1], %[y] \n\t"
|
2023-05-13 13:48:02 +02:00
|
|
|
#else
|
2023-05-12 17:29:48 +02:00
|
|
|
"eors %[s1], %[x], %[y] \n\t"
|
2023-05-13 13:48:02 +02:00
|
|
|
#endif
|
2023-05-12 17:29:48 +02:00
|
|
|
"subs %[x], %[x], %[y] \n\t"
|
|
|
|
"bics %[x], %[x], %[s1] \n\t"
|
|
|
|
"ands %[y], %[s1], %[y] \n\t"
|
|
|
|
"orrs %[x], %[x], %[y] \n\t"
|
|
|
|
"asrs %[x], %[x], #31 \n\t"
|
2023-06-12 16:38:49 +02:00
|
|
|
RESTORE_ASM_SYNTAX
|
2023-05-12 17:29:48 +02:00
|
|
|
:
|
|
|
|
[s1] "=&l" (s1),
|
|
|
|
[x] "+&l" (x),
|
|
|
|
[y] "+&l" (y)
|
2023-05-13 13:48:02 +02:00
|
|
|
:
|
|
|
|
:
|
2023-06-12 16:38:49 +02:00
|
|
|
"cc"
|
2023-05-13 13:48:02 +02:00
|
|
|
);
|
|
|
|
return (mbedtls_ct_condition_t) x;
|
2023-05-12 13:11:37 +02:00
|
|
|
#elif defined(MBEDTLS_CT_X86_64_ASM) && (defined(MBEDTLS_CT_SIZE_32) || defined(MBEDTLS_CT_SIZE_64))
|
2023-09-08 18:19:32 +02:00
|
|
|
uint64_t s;
|
|
|
|
asm volatile ("mov %[x], %[s] \n\t"
|
|
|
|
"xor %[y], %[s] \n\t"
|
2023-05-12 13:11:37 +02:00
|
|
|
"sub %[y], %[x] \n\t"
|
2023-09-08 18:19:32 +02:00
|
|
|
"and %[s], %[y] \n\t"
|
|
|
|
"not %[s] \n\t"
|
|
|
|
"and %[s], %[x] \n\t"
|
2023-05-12 13:11:37 +02:00
|
|
|
"or %[y], %[x] \n\t"
|
2023-09-08 18:18:04 +02:00
|
|
|
"sar $63, %[x] \n\t"
|
2023-05-12 13:11:37 +02:00
|
|
|
:
|
2023-09-08 18:19:32 +02:00
|
|
|
[s] "=&a" (s),
|
2023-09-08 18:18:29 +02:00
|
|
|
[x] "+&D" (x),
|
|
|
|
[y] "+&S" (y)
|
2023-05-12 13:11:37 +02:00
|
|
|
:
|
|
|
|
:
|
|
|
|
);
|
2023-09-08 18:18:04 +02:00
|
|
|
return (mbedtls_ct_condition_t) x;
|
2023-05-13 13:32:09 +02:00
|
|
|
#elif defined(MBEDTLS_CT_X86_ASM) && defined(MBEDTLS_CT_SIZE_32)
|
|
|
|
uint32_t s;
|
|
|
|
asm volatile ("mov %[x], %[s] \n\t"
|
|
|
|
"xor %[y], %[s] \n\t"
|
|
|
|
"sub %[y], %[x] \n\t"
|
2023-09-08 18:26:18 +02:00
|
|
|
"and %[s], %[y] \n\t"
|
2023-05-13 13:32:09 +02:00
|
|
|
"not %[s] \n\t"
|
|
|
|
"and %[s], %[x] \n\t"
|
2023-09-08 18:26:18 +02:00
|
|
|
"or %[y], %[x] \n\t"
|
2023-05-13 13:32:09 +02:00
|
|
|
"sar $31, %[x] \n\t"
|
|
|
|
:
|
|
|
|
[s] "=&b" (s),
|
2023-09-08 18:57:40 +02:00
|
|
|
[x] "+&a" (x),
|
|
|
|
[y] "+&c" (y)
|
2023-05-13 13:32:09 +02:00
|
|
|
:
|
|
|
|
:
|
|
|
|
);
|
|
|
|
return (mbedtls_ct_condition_t) x;
|
2023-05-13 13:47:02 +02:00
|
|
|
#else
|
2023-05-17 12:59:56 +02:00
|
|
|
/* Ensure that the compiler cannot optimise the following operations over x and y,
|
|
|
|
* even if it knows the value of x and y.
|
|
|
|
*/
|
2023-05-17 13:21:32 +02:00
|
|
|
const mbedtls_ct_uint_t xo = mbedtls_ct_compiler_opaque(x);
|
2023-05-17 12:59:56 +02:00
|
|
|
const mbedtls_ct_uint_t yo = mbedtls_ct_compiler_opaque(y);
|
|
|
|
/*
|
|
|
|
* Check if the most significant bits (MSB) of the operands are different.
|
|
|
|
* cond is true iff the MSBs differ.
|
|
|
|
*/
|
2023-05-17 13:21:32 +02:00
|
|
|
mbedtls_ct_condition_t cond = mbedtls_ct_bool((xo ^ yo) >> (MBEDTLS_CT_SIZE - 1));
|
2023-05-17 12:59:56 +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.
|
|
|
|
*
|
|
|
|
* 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.)
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Select either y, or x - y
|
2023-05-17 13:21:32 +02:00
|
|
|
mbedtls_ct_uint_t ret = mbedtls_ct_if(cond, yo, (mbedtls_ct_uint_t) (xo - yo));
|
2023-05-17 12:59:56 +02:00
|
|
|
|
|
|
|
// Extract only the MSB of ret
|
|
|
|
ret = ret >> (MBEDTLS_CT_SIZE - 1);
|
|
|
|
|
|
|
|
// Convert to a condition (i.e., all bits set iff non-zero)
|
|
|
|
return mbedtls_ct_bool(ret);
|
2023-05-13 13:47:02 +02:00
|
|
|
#endif
|
2023-05-17 12:59:56 +02:00
|
|
|
}
|
|
|
|
|
2023-08-10 12:58:18 +02:00
|
|
|
static inline mbedtls_ct_condition_t mbedtls_ct_uint_ne(mbedtls_ct_uint_t x, mbedtls_ct_uint_t y)
|
2023-05-17 12:59:56 +02:00
|
|
|
{
|
|
|
|
/* diff = 0 if x == y, non-zero otherwise */
|
2023-05-17 18:45:17 +02:00
|
|
|
const mbedtls_ct_uint_t diff = mbedtls_ct_compiler_opaque(x) ^ mbedtls_ct_compiler_opaque(y);
|
2023-05-17 12:59:56 +02:00
|
|
|
|
|
|
|
/* all ones if x != y, 0 otherwise */
|
|
|
|
return mbedtls_ct_bool(diff);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned char mbedtls_ct_uchar_in_range_if(unsigned char low,
|
|
|
|
unsigned char high,
|
|
|
|
unsigned char c,
|
|
|
|
unsigned char t)
|
|
|
|
{
|
2023-08-17 11:00:01 +02:00
|
|
|
const unsigned char co = (unsigned char) mbedtls_ct_compiler_opaque(c);
|
|
|
|
const unsigned char to = (unsigned char) mbedtls_ct_compiler_opaque(t);
|
2023-05-17 12:59:56 +02:00
|
|
|
|
|
|
|
/* low_mask is: 0 if low <= c, 0x...ff if low > c */
|
|
|
|
unsigned low_mask = ((unsigned) co - low) >> 8;
|
|
|
|
/* high_mask is: 0 if c <= high, 0x...ff if c > high */
|
|
|
|
unsigned high_mask = ((unsigned) high - co) >> 8;
|
|
|
|
|
|
|
|
return (unsigned char) (~(low_mask | high_mask)) & to;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ============================================================================
|
|
|
|
* Everything below here is trivial wrapper functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
static inline size_t mbedtls_ct_size_if(mbedtls_ct_condition_t condition,
|
|
|
|
size_t if1,
|
|
|
|
size_t if0)
|
|
|
|
{
|
|
|
|
return (size_t) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1, (mbedtls_ct_uint_t) if0);
|
|
|
|
}
|
|
|
|
|
2023-05-17 16:51:59 +02:00
|
|
|
static inline unsigned mbedtls_ct_uint_if(mbedtls_ct_condition_t condition,
|
2023-05-17 12:59:56 +02:00
|
|
|
unsigned if1,
|
|
|
|
unsigned if0)
|
|
|
|
{
|
|
|
|
return (unsigned) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1, (mbedtls_ct_uint_t) if0);
|
|
|
|
}
|
|
|
|
|
2023-09-19 22:51:13 +02:00
|
|
|
static inline mbedtls_ct_condition_t mbedtls_ct_bool_if(mbedtls_ct_condition_t condition,
|
|
|
|
mbedtls_ct_condition_t if1,
|
|
|
|
mbedtls_ct_condition_t if0)
|
|
|
|
{
|
|
|
|
return (mbedtls_ct_condition_t) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1,
|
|
|
|
(mbedtls_ct_uint_t) if0);
|
|
|
|
}
|
|
|
|
|
2023-05-17 12:59:56 +02:00
|
|
|
#if defined(MBEDTLS_BIGNUM_C)
|
|
|
|
|
2023-05-17 18:45:33 +02:00
|
|
|
static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if(mbedtls_ct_condition_t condition,
|
|
|
|
mbedtls_mpi_uint if1,
|
2023-05-17 12:59:56 +02:00
|
|
|
mbedtls_mpi_uint if0)
|
|
|
|
{
|
|
|
|
return (mbedtls_mpi_uint) mbedtls_ct_if(condition,
|
|
|
|
(mbedtls_ct_uint_t) if1,
|
|
|
|
(mbedtls_ct_uint_t) if0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2023-08-10 13:11:31 +02:00
|
|
|
static inline size_t mbedtls_ct_size_if_else_0(mbedtls_ct_condition_t condition, size_t if1)
|
2023-05-17 12:59:56 +02:00
|
|
|
{
|
2023-05-17 18:45:17 +02:00
|
|
|
return (size_t) (condition & if1);
|
2023-05-17 12:59:56 +02:00
|
|
|
}
|
|
|
|
|
2023-08-10 13:11:31 +02:00
|
|
|
static inline unsigned mbedtls_ct_uint_if_else_0(mbedtls_ct_condition_t condition, unsigned if1)
|
2023-05-17 12:59:56 +02:00
|
|
|
{
|
2023-05-17 18:45:17 +02:00
|
|
|
return (unsigned) (condition & if1);
|
2023-05-17 12:59:56 +02:00
|
|
|
}
|
|
|
|
|
2023-09-19 22:51:13 +02:00
|
|
|
static inline mbedtls_ct_condition_t mbedtls_ct_bool_if_else_0(mbedtls_ct_condition_t condition,
|
|
|
|
mbedtls_ct_condition_t if1)
|
|
|
|
{
|
|
|
|
return (mbedtls_ct_condition_t) (condition & if1);
|
|
|
|
}
|
|
|
|
|
2023-05-17 12:59:56 +02:00
|
|
|
#if defined(MBEDTLS_BIGNUM_C)
|
|
|
|
|
2023-08-10 13:11:31 +02:00
|
|
|
static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if_else_0(mbedtls_ct_condition_t condition,
|
|
|
|
mbedtls_mpi_uint if1)
|
2023-05-17 12:59:56 +02:00
|
|
|
{
|
2023-05-17 18:45:17 +02:00
|
|
|
return (mbedtls_mpi_uint) (condition & if1);
|
2023-05-17 12:59:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MBEDTLS_BIGNUM_C */
|
|
|
|
|
2023-09-22 10:43:49 +02:00
|
|
|
static inline int mbedtls_ct_error_if(mbedtls_ct_condition_t condition, int if1, int if0)
|
|
|
|
{
|
2023-09-22 11:54:43 +02:00
|
|
|
/* Coverting int -> uint -> int here is safe, because we require if1 and if0 to be
|
|
|
|
* in the range -32767..0, and we require 32-bit int and uint types.
|
|
|
|
*
|
|
|
|
* This means that (0 <= -if0 < INT_MAX), so negating if0 is safe, and similarly for
|
|
|
|
* converting back to int.
|
|
|
|
*/
|
2023-09-22 10:43:49 +02:00
|
|
|
return -((int) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) (-if1),
|
|
|
|
(mbedtls_ct_uint_t) (-if0)));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int mbedtls_ct_error_if_else_0(mbedtls_ct_condition_t condition, int if1)
|
|
|
|
{
|
|
|
|
return -((int) (condition & (-if1)));
|
|
|
|
}
|
|
|
|
|
2023-08-10 12:58:18 +02:00
|
|
|
static inline mbedtls_ct_condition_t mbedtls_ct_uint_eq(mbedtls_ct_uint_t x,
|
2023-05-17 18:45:33 +02:00
|
|
|
mbedtls_ct_uint_t y)
|
|
|
|
{
|
2023-08-10 12:58:18 +02:00
|
|
|
return ~mbedtls_ct_uint_ne(x, y);
|
2023-05-17 18:45:33 +02:00
|
|
|
}
|
|
|
|
|
2023-08-10 12:58:18 +02:00
|
|
|
static inline mbedtls_ct_condition_t mbedtls_ct_uint_gt(mbedtls_ct_uint_t x,
|
2023-05-17 12:59:56 +02:00
|
|
|
mbedtls_ct_uint_t y)
|
|
|
|
{
|
2023-08-10 12:58:18 +02:00
|
|
|
return mbedtls_ct_uint_lt(y, x);
|
2023-05-17 12:59:56 +02:00
|
|
|
}
|
|
|
|
|
2023-08-10 12:58:18 +02:00
|
|
|
static inline mbedtls_ct_condition_t mbedtls_ct_uint_ge(mbedtls_ct_uint_t x,
|
2023-05-17 12:59:56 +02:00
|
|
|
mbedtls_ct_uint_t y)
|
|
|
|
{
|
2023-08-10 12:58:18 +02:00
|
|
|
return ~mbedtls_ct_uint_lt(x, y);
|
2023-05-17 12:59:56 +02:00
|
|
|
}
|
|
|
|
|
2023-08-10 12:58:18 +02:00
|
|
|
static inline mbedtls_ct_condition_t mbedtls_ct_uint_le(mbedtls_ct_uint_t x,
|
2023-05-17 12:59:56 +02:00
|
|
|
mbedtls_ct_uint_t y)
|
|
|
|
{
|
2023-08-10 12:58:18 +02:00
|
|
|
return ~mbedtls_ct_uint_gt(x, y);
|
2023-05-17 12:59:56 +02:00
|
|
|
}
|
|
|
|
|
2023-09-19 17:18:59 +02:00
|
|
|
static inline mbedtls_ct_condition_t mbedtls_ct_bool_ne(mbedtls_ct_condition_t x,
|
|
|
|
mbedtls_ct_condition_t y)
|
2023-05-17 12:59:56 +02:00
|
|
|
{
|
2023-05-17 18:45:17 +02:00
|
|
|
return (mbedtls_ct_condition_t) (x ^ y);
|
2023-05-17 12:59:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline mbedtls_ct_condition_t mbedtls_ct_bool_and(mbedtls_ct_condition_t x,
|
|
|
|
mbedtls_ct_condition_t y)
|
|
|
|
{
|
2023-05-17 18:45:17 +02:00
|
|
|
return (mbedtls_ct_condition_t) (x & y);
|
2023-05-17 12:59:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline mbedtls_ct_condition_t mbedtls_ct_bool_or(mbedtls_ct_condition_t x,
|
|
|
|
mbedtls_ct_condition_t y)
|
|
|
|
{
|
2023-05-17 18:45:17 +02:00
|
|
|
return (mbedtls_ct_condition_t) (x | y);
|
2023-05-17 12:59:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline mbedtls_ct_condition_t mbedtls_ct_bool_not(mbedtls_ct_condition_t x)
|
|
|
|
{
|
2023-05-17 18:45:17 +02:00
|
|
|
return (mbedtls_ct_condition_t) (~x);
|
2023-05-17 12:59:56 +02:00
|
|
|
}
|
|
|
|
|
2023-08-01 15:10:56 +02:00
|
|
|
#ifdef __GNUC__
|
2023-08-09 15:10:14 +02:00
|
|
|
/* Restore warnings for -Wredundant-decls on gcc */
|
2023-08-01 15:10:56 +02:00
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
|
|
|
|
2023-05-17 12:59:56 +02:00
|
|
|
#endif /* MBEDTLS_CONSTANT_TIME_IMPL_H */
|