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.
|
|
|
|
*/
|
|
|
|
|
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 12:55:33 +02:00
|
|
|
#if defined(MBEDTLS_BIGNUM_C)
|
|
|
|
#include "mbedtls/bignum.h"
|
2022-09-15 20:02:36 +02:00
|
|
|
#include "bignum_core.h"
|
2021-09-27 12:55:33 +02:00
|
|
|
#endif
|
|
|
|
|
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-11-15 16:13:01 +01:00
|
|
|
#if defined(MBEDTLS_BASE64_C)
|
|
|
|
#include "constant_time_invasive.h"
|
|
|
|
#endif
|
|
|
|
|
2021-09-27 16:11:12 +02:00
|
|
|
#include <string.h>
|
2021-09-27 12:55:33 +02:00
|
|
|
|
2022-12-30 22:25:35 +01:00
|
|
|
/*
|
|
|
|
* Define MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS where assembly is present to
|
|
|
|
* perform fast unaligned access to volatile data.
|
2022-12-22 16:04:43 +01:00
|
|
|
*
|
|
|
|
* This is needed because mbedtls_get_unaligned_uintXX etc don't support volatile
|
|
|
|
* memory accesses.
|
|
|
|
*
|
2022-12-30 22:25:35 +01:00
|
|
|
* Some of these definitions could be moved into alignment.h but for now they are
|
|
|
|
* only used here.
|
2022-12-22 16:04:43 +01:00
|
|
|
*/
|
2022-12-30 22:25:35 +01:00
|
|
|
#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS) && defined(MBEDTLS_HAVE_ASM)
|
|
|
|
#if defined(__arm__) || defined(__thumb__) || defined(__thumb2__) || defined(__aarch64__)
|
|
|
|
#define MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS
|
|
|
|
#endif
|
2022-12-22 16:04:43 +01:00
|
|
|
#endif
|
|
|
|
|
2022-12-30 22:25:35 +01:00
|
|
|
#if defined(MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS)
|
2022-12-22 16:04:43 +01: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;
|
|
|
|
#if defined(__arm__) || defined(__thumb__) || defined(__thumb2__)
|
2023-01-30 10:26:48 +01:00
|
|
|
asm volatile ("ldr %0, [%1]" : "=r" (r) : "r" (p) :);
|
2022-12-30 22:25:35 +01:00
|
|
|
#elif defined(__aarch64__)
|
2023-01-30 10:26:48 +01:00
|
|
|
asm volatile ("ldr %w0, [%1]" : "=r" (r) : "r" (p) :);
|
2022-12-22 16:04:43 +01:00
|
|
|
#endif
|
2022-12-30 22:25:35 +01:00
|
|
|
return r;
|
2022-12-22 16:04:43 +01:00
|
|
|
}
|
2022-12-30 22:25:35 +01:00
|
|
|
#endif /* MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS */
|
2022-12-22 16:04:43 +01: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 16:04:43 +01:00
|
|
|
size_t i = 0;
|
2023-01-11 18:39:33 +01: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 18:39:33 +01:00
|
|
|
uint32_t diff = 0;
|
2021-09-27 11:28:54 +02:00
|
|
|
|
2022-12-30 22:25:35 +01:00
|
|
|
#if defined(MBEDTLS_EFFICIENT_UNALIGNED_VOLATILE_ACCESS)
|
2022-12-22 16:04:43 +01: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.
|
|
|
|
*/
|
2021-11-26 17:20:36 +01:00
|
|
|
MBEDTLS_STATIC_TESTABLE
|
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)
|
|
|
|
|
2021-10-18 16:35:23 +02:00
|
|
|
/** Constant-flow "greater than" comparison:
|
|
|
|
* return x > y
|
|
|
|
*
|
|
|
|
* This is equivalent to \p x > \p y, but is likely to be compiled
|
|
|
|
* to code using bitwise operation rather than a branch.
|
|
|
|
*
|
|
|
|
* \param x The first value to analyze.
|
|
|
|
* \param y The second value to analyze.
|
|
|
|
*
|
|
|
|
* \return 1 if \p x greater than \p y, otherwise 0.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static 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
|
|
|
|
2021-11-03 16:13:32 +01:00
|
|
|
/** Select between two sign values without branches.
|
2021-10-18 16:35:23 +02:00
|
|
|
*
|
|
|
|
* This is functionally equivalent to `condition ? if1 : if0` but uses only bit
|
|
|
|
* operations in order to avoid branches.
|
|
|
|
*
|
|
|
|
* \note if1 and if0 must be either 1 or -1, otherwise the result
|
|
|
|
* is undefined.
|
|
|
|
*
|
2022-08-18 15:09:18 +02:00
|
|
|
* \param condition Condition to test; must be either 0 or 1.
|
2021-10-18 16:35:23 +02:00
|
|
|
* \param if1 The first sign; must be either +1 or -1.
|
|
|
|
* \param if0 The second sign; must be either +1 or -1.
|
|
|
|
*
|
|
|
|
* \return \c if1 if \p condition is nonzero, otherwise \c if0.
|
|
|
|
* */
|
2023-01-11 14:50:10 +01:00
|
|
|
static int mbedtls_ct_cond_select_sign(unsigned char condition,
|
|
|
|
int if1,
|
|
|
|
int if0)
|
2021-09-27 13:03:57 +02:00
|
|
|
{
|
2021-11-03 16:13:32 +01:00
|
|
|
/* In order to avoid questions about what we can reasonably assume about
|
2021-09-27 13:03:57 +02:00
|
|
|
* the representations of signed integers, move everything to unsigned
|
2021-10-18 16:28:27 +02:00
|
|
|
* by taking advantage of the fact that if1 and if0 are either +1 or -1. */
|
2021-08-10 20:36:09 +02:00
|
|
|
unsigned uif1 = if1 + 1;
|
|
|
|
unsigned uif0 = if0 + 1;
|
2021-09-27 13:03:57 +02:00
|
|
|
|
2021-10-18 16:28:27 +02:00
|
|
|
/* condition was 0 or 1, mask is 0 or 2 as are uif1 and uif0 */
|
2021-08-10 20:36:09 +02:00
|
|
|
const unsigned mask = condition << 1;
|
2021-09-27 13:03:57 +02:00
|
|
|
|
2021-10-18 16:28:27 +02:00
|
|
|
/* select uif1 or uif0 */
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned ur = (uif0 & ~mask) | (uif1 & mask);
|
2021-09-27 13:03:57 +02:00
|
|
|
|
|
|
|
/* ur is now 0 or 2, convert back to -1 or +1 */
|
2023-01-11 14:50:10 +01:00
|
|
|
return (int) ur - 1;
|
2021-09-27 13:03:57 +02:00
|
|
|
}
|
2021-09-27 13:17:15 +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-11-15 16:18:54 +01:00
|
|
|
#if defined(MBEDTLS_BASE64_C)
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned char mbedtls_ct_base64_enc_char(unsigned char value)
|
2021-11-15 16:18:54 +01:00
|
|
|
{
|
|
|
|
unsigned char digit = 0;
|
2021-11-24 15:51:39 +01:00
|
|
|
/* For each range of values, if value is in that range, mask digit with
|
|
|
|
* the corresponding value. Since value can only be in a single range,
|
2021-11-15 16:18:54 +01:00
|
|
|
* only at most one masking will change digit. */
|
2023-01-11 14:50:10 +01:00
|
|
|
digit |= mbedtls_ct_uchar_mask_of_range(0, 25, value) & ('A' + value);
|
|
|
|
digit |= mbedtls_ct_uchar_mask_of_range(26, 51, value) & ('a' + value - 26);
|
|
|
|
digit |= mbedtls_ct_uchar_mask_of_range(52, 61, value) & ('0' + value - 52);
|
|
|
|
digit |= mbedtls_ct_uchar_mask_of_range(62, 62, value) & '+';
|
|
|
|
digit |= mbedtls_ct_uchar_mask_of_range(63, 63, value) & '/';
|
|
|
|
return digit;
|
2021-11-15 16:18:54 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
signed char mbedtls_ct_base64_dec_value(unsigned char c)
|
2021-11-15 16:22:37 +01:00
|
|
|
{
|
|
|
|
unsigned char val = 0;
|
|
|
|
/* For each range of digits, if c is in that range, mask val with
|
|
|
|
* the corresponding value. Since c can only be in a single range,
|
|
|
|
* only at most one masking will change val. Set val to one plus
|
|
|
|
* the desired value so that it stays 0 if c is in none of the ranges. */
|
2023-01-11 14:50:10 +01:00
|
|
|
val |= mbedtls_ct_uchar_mask_of_range('A', 'Z', c) & (c - 'A' + 0 + 1);
|
|
|
|
val |= mbedtls_ct_uchar_mask_of_range('a', 'z', c) & (c - 'a' + 26 + 1);
|
|
|
|
val |= mbedtls_ct_uchar_mask_of_range('0', '9', c) & (c - '0' + 52 + 1);
|
|
|
|
val |= mbedtls_ct_uchar_mask_of_range('+', '+', c) & (c - '+' + 62 + 1);
|
|
|
|
val |= mbedtls_ct_uchar_mask_of_range('/', '/', c) & (c - '/' + 63 + 1);
|
2021-11-15 16:22:37 +01:00
|
|
|
/* At this point, val is 0 if c is an invalid digit and v+1 if c is
|
|
|
|
* a digit with the value v. */
|
2023-01-11 14:50:10 +01:00
|
|
|
return val - 1;
|
2021-11-15 16:22:37 +01:00
|
|
|
}
|
|
|
|
|
2021-11-15 16:18:54 +01:00
|
|
|
#endif /* MBEDTLS_BASE64_C */
|
|
|
|
|
2021-10-18 17:05:06 +02:00
|
|
|
#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
|
|
|
|
|
2021-10-18 16:35:23 +02:00
|
|
|
/** Shift some data towards the left inside a buffer.
|
|
|
|
*
|
2021-10-20 11:59:27 +02:00
|
|
|
* `mbedtls_ct_mem_move_to_left(start, total, offset)` is functionally
|
2021-10-18 16:35:23 +02:00
|
|
|
* equivalent to
|
|
|
|
* ```
|
|
|
|
* memmove(start, start + offset, total - offset);
|
|
|
|
* memset(start + offset, 0, total - offset);
|
|
|
|
* ```
|
|
|
|
* but it strives to use a memory access pattern (and thus total timing)
|
|
|
|
* that does not depend on \p offset. This timing independence comes at
|
|
|
|
* the expense of performance.
|
|
|
|
*
|
|
|
|
* \param start Pointer to the start of the buffer.
|
|
|
|
* \param total Total size of the buffer.
|
|
|
|
* \param offset Offset from which to copy \p total - \p offset bytes.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static void mbedtls_ct_mem_move_to_left(void *start,
|
|
|
|
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 16:04:43 +01: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-02-25 15:09:36 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2022-03-17 16:36:52 +01:00
|
|
|
|
|
|
|
#if defined(PSA_WANT_ALG_SHA_384)
|
2023-01-11 14:50:10 +01:00
|
|
|
#define MAX_HASH_BLOCK_LENGTH PSA_HASH_BLOCK_LENGTH(PSA_ALG_SHA_384)
|
2022-03-17 16:36:52 +01:00
|
|
|
#elif defined(PSA_WANT_ALG_SHA_256)
|
2023-01-11 14:50:10 +01:00
|
|
|
#define MAX_HASH_BLOCK_LENGTH PSA_HASH_BLOCK_LENGTH(PSA_ALG_SHA_256)
|
2022-03-17 16:36:52 +01:00
|
|
|
#else /* See check_config.h */
|
2023-01-11 14:50:10 +01:00
|
|
|
#define MAX_HASH_BLOCK_LENGTH PSA_HASH_BLOCK_LENGTH(PSA_ALG_SHA_1)
|
2022-03-17 16:36:52 +01:00
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ct_hmac(mbedtls_svc_key_id_t key,
|
|
|
|
psa_algorithm_t mac_alg,
|
|
|
|
const unsigned char *add_data,
|
|
|
|
size_t add_data_len,
|
|
|
|
const unsigned char *data,
|
|
|
|
size_t data_len_secret,
|
|
|
|
size_t min_data_len,
|
|
|
|
size_t max_data_len,
|
|
|
|
unsigned char *output)
|
2022-02-25 15:09:36 +01:00
|
|
|
{
|
|
|
|
/*
|
2022-03-17 16:33:27 +01:00
|
|
|
* This function breaks the HMAC abstraction and uses psa_hash_clone()
|
|
|
|
* extension in order to get constant-flow behaviour.
|
2022-02-25 15:09:36 +01:00
|
|
|
*
|
|
|
|
* HMAC(msg) is defined as HASH(okey + HASH(ikey + msg)) where + means
|
|
|
|
* concatenation, and okey/ikey are the XOR of the key with some fixed bit
|
|
|
|
* patterns (see RFC 2104, sec. 2).
|
|
|
|
*
|
|
|
|
* We'll first compute ikey/okey, then inner_hash = HASH(ikey + msg) by
|
|
|
|
* hashing up to minlen, then cloning the context, and for each byte up
|
|
|
|
* to maxlen finishing up the hash computation, keeping only the
|
|
|
|
* correct result.
|
|
|
|
*
|
|
|
|
* Then we only need to compute HASH(okey + inner_hash) and we're done.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH(mac_alg);
|
|
|
|
const size_t block_size = PSA_HASH_BLOCK_LENGTH(hash_alg);
|
2022-03-17 17:04:37 +01:00
|
|
|
unsigned char key_buf[MAX_HASH_BLOCK_LENGTH];
|
2023-01-11 14:50:10 +01:00
|
|
|
const size_t hash_size = PSA_HASH_LENGTH(hash_alg);
|
2022-02-25 15:09:36 +01:00
|
|
|
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
|
|
|
|
size_t hash_length;
|
|
|
|
|
2022-03-17 16:36:52 +01:00
|
|
|
unsigned char aux_out[PSA_HASH_MAX_SIZE];
|
2022-02-25 15:09:36 +01:00
|
|
|
psa_hash_operation_t aux_operation = PSA_HASH_OPERATION_INIT;
|
|
|
|
size_t offset;
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
|
|
size_t mac_key_length;
|
|
|
|
size_t i;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
#define PSA_CHK(func_call) \
|
2022-02-25 15:09:36 +01:00
|
|
|
do { \
|
|
|
|
status = (func_call); \
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status != PSA_SUCCESS) \
|
|
|
|
goto cleanup; \
|
|
|
|
} while (0)
|
2022-02-25 15:09:36 +01:00
|
|
|
|
2022-03-17 16:39:10 +01:00
|
|
|
/* Export MAC key
|
|
|
|
* We assume key length is always exactly the output size
|
|
|
|
* which is never more than the block size, thus we use block_size
|
|
|
|
* as the key buffer size.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_CHK(psa_export_key(key, key_buf, block_size, &mac_key_length));
|
2022-02-25 15:09:36 +01:00
|
|
|
|
2022-03-17 17:04:37 +01:00
|
|
|
/* Calculate ikey */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < mac_key_length; i++) {
|
|
|
|
key_buf[i] = (unsigned char) (key_buf[i] ^ 0x36);
|
|
|
|
}
|
|
|
|
for (; i < block_size; ++i) {
|
2022-03-17 17:04:37 +01:00
|
|
|
key_buf[i] = 0x36;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-25 15:09:36 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_CHK(psa_hash_setup(&operation, hash_alg));
|
2022-02-25 15:09:36 +01:00
|
|
|
|
|
|
|
/* Now compute inner_hash = HASH(ikey + msg) */
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_CHK(psa_hash_update(&operation, key_buf, block_size));
|
|
|
|
PSA_CHK(psa_hash_update(&operation, add_data, add_data_len));
|
|
|
|
PSA_CHK(psa_hash_update(&operation, data, min_data_len));
|
2022-02-25 15:09:36 +01:00
|
|
|
|
2022-05-09 19:15:54 +02:00
|
|
|
/* Fill the hash buffer in advance with something that is
|
|
|
|
* not a valid hash (barring an attack on the hash and
|
|
|
|
* deliberately-crafted input), in case the caller doesn't
|
|
|
|
* check the return status properly. */
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(output, '!', hash_size);
|
2022-05-09 19:15:54 +02:00
|
|
|
|
2022-02-25 15:09:36 +01:00
|
|
|
/* For each possible length, compute the hash up to that point */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (offset = min_data_len; offset <= max_data_len; offset++) {
|
|
|
|
PSA_CHK(psa_hash_clone(&operation, &aux_operation));
|
|
|
|
PSA_CHK(psa_hash_finish(&aux_operation, aux_out,
|
|
|
|
PSA_HASH_MAX_SIZE, &hash_length));
|
2022-02-25 15:09:36 +01:00
|
|
|
/* Keep only the correct inner_hash in the output buffer */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ct_memcpy_if_eq(output, aux_out, hash_size,
|
|
|
|
offset, data_len_secret);
|
2022-02-25 15:09:36 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (offset < max_data_len) {
|
|
|
|
PSA_CHK(psa_hash_update(&operation, data + offset, 1));
|
|
|
|
}
|
2022-02-25 15:09:36 +01:00
|
|
|
}
|
|
|
|
|
2022-03-17 16:33:27 +01:00
|
|
|
/* Abort current operation to prepare for final operation */
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_CHK(psa_hash_abort(&operation));
|
2022-02-25 15:09:36 +01:00
|
|
|
|
2022-03-17 17:04:37 +01:00
|
|
|
/* Calculate okey */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < mac_key_length; i++) {
|
|
|
|
key_buf[i] = (unsigned char) ((key_buf[i] ^ 0x36) ^ 0x5C);
|
|
|
|
}
|
|
|
|
for (; i < block_size; ++i) {
|
2022-03-17 17:04:37 +01:00
|
|
|
key_buf[i] = 0x5C;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-03-17 17:04:37 +01:00
|
|
|
|
2022-02-25 15:09:36 +01:00
|
|
|
/* Now compute HASH(okey + inner_hash) */
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_CHK(psa_hash_setup(&operation, hash_alg));
|
|
|
|
PSA_CHK(psa_hash_update(&operation, key_buf, block_size));
|
|
|
|
PSA_CHK(psa_hash_update(&operation, output, hash_size));
|
|
|
|
PSA_CHK(psa_hash_finish(&operation, output, hash_size, &hash_length));
|
2022-02-25 15:09:36 +01:00
|
|
|
|
|
|
|
#undef PSA_CHK
|
|
|
|
|
|
|
|
cleanup:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(key_buf, MAX_HASH_BLOCK_LENGTH);
|
|
|
|
mbedtls_platform_zeroize(aux_out, PSA_HASH_MAX_SIZE);
|
2022-03-17 16:36:52 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_hash_abort(&operation);
|
|
|
|
psa_hash_abort(&aux_operation);
|
|
|
|
return psa_ssl_status_to_mbedtls(status);
|
2022-02-25 15:09:36 +01:00
|
|
|
}
|
2022-03-17 16:36:52 +01:00
|
|
|
|
|
|
|
#undef MAX_HASH_BLOCK_LENGTH
|
|
|
|
|
2022-02-25 15:09:36 +01:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ct_hmac(mbedtls_md_context_t *ctx,
|
|
|
|
const unsigned char *add_data,
|
|
|
|
size_t add_data_len,
|
|
|
|
const unsigned char *data,
|
|
|
|
size_t data_len_secret,
|
|
|
|
size_t min_data_len,
|
|
|
|
size_t max_data_len,
|
|
|
|
unsigned char *output)
|
2021-09-27 14:28:31 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* This function breaks the HMAC abstraction and uses the md_clone()
|
|
|
|
* extension to the MD API in order to get constant-flow behaviour.
|
|
|
|
*
|
|
|
|
* HMAC(msg) is defined as HASH(okey + HASH(ikey + msg)) where + means
|
|
|
|
* concatenation, and okey/ikey are the XOR of the key with some fixed bit
|
|
|
|
* patterns (see RFC 2104, sec. 2), which are stored in ctx->hmac_ctx.
|
|
|
|
*
|
|
|
|
* We'll first compute inner_hash = HASH(ikey + msg) by hashing up to
|
|
|
|
* minlen, then cloning the context, and for each byte up to maxlen
|
|
|
|
* finishing up the hash computation, keeping only the correct result.
|
|
|
|
*
|
|
|
|
* Then we only need to compute HASH(okey + inner_hash) and we're done.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
const mbedtls_md_type_t md_alg = mbedtls_md_get_type(ctx->md_info);
|
2021-09-27 14:28:31 +02:00
|
|
|
/* TLS 1.2 only supports SHA-384, SHA-256, SHA-1, MD-5,
|
|
|
|
* all of which have the same block size except SHA-384. */
|
|
|
|
const size_t block_size = md_alg == MBEDTLS_MD_SHA384 ? 128 : 64;
|
|
|
|
const unsigned char * const ikey = ctx->hmac_ctx;
|
|
|
|
const unsigned char * const okey = ikey + block_size;
|
2023-01-11 14:50:10 +01:00
|
|
|
const size_t hash_size = mbedtls_md_get_size(ctx->md_info);
|
2021-09-27 14:28:31 +02:00
|
|
|
|
|
|
|
unsigned char aux_out[MBEDTLS_MD_MAX_SIZE];
|
|
|
|
mbedtls_md_context_t aux;
|
|
|
|
size_t offset;
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_md_init(&aux);
|
2021-09-27 14:28:31 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
#define MD_CHK(func_call) \
|
2021-09-27 14:28:31 +02:00
|
|
|
do { \
|
|
|
|
ret = (func_call); \
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) \
|
|
|
|
goto cleanup; \
|
|
|
|
} while (0)
|
2021-09-27 14:28:31 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MD_CHK(mbedtls_md_setup(&aux, ctx->md_info, 0));
|
2021-09-27 14:28:31 +02:00
|
|
|
|
|
|
|
/* After hmac_start() of hmac_reset(), ikey has already been hashed,
|
|
|
|
* so we can start directly with the message */
|
2023-01-11 14:50:10 +01:00
|
|
|
MD_CHK(mbedtls_md_update(ctx, add_data, add_data_len));
|
|
|
|
MD_CHK(mbedtls_md_update(ctx, data, min_data_len));
|
2021-09-27 14:28:31 +02:00
|
|
|
|
2022-05-09 19:15:54 +02:00
|
|
|
/* Fill the hash buffer in advance with something that is
|
|
|
|
* not a valid hash (barring an attack on the hash and
|
|
|
|
* deliberately-crafted input), in case the caller doesn't
|
|
|
|
* check the return status properly. */
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(output, '!', hash_size);
|
2022-05-09 19:15:54 +02:00
|
|
|
|
2021-09-27 14:28:31 +02:00
|
|
|
/* For each possible length, compute the hash up to that point */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (offset = min_data_len; offset <= max_data_len; offset++) {
|
|
|
|
MD_CHK(mbedtls_md_clone(&aux, ctx));
|
|
|
|
MD_CHK(mbedtls_md_finish(&aux, aux_out));
|
2021-09-27 14:28:31 +02:00
|
|
|
/* Keep only the correct inner_hash in the output buffer */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ct_memcpy_if_eq(output, aux_out, hash_size,
|
|
|
|
offset, data_len_secret);
|
2021-09-27 14:28:31 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (offset < max_data_len) {
|
|
|
|
MD_CHK(mbedtls_md_update(ctx, data + offset, 1));
|
|
|
|
}
|
2021-09-27 14:28:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The context needs to finish() before it starts() again */
|
2023-01-11 14:50:10 +01:00
|
|
|
MD_CHK(mbedtls_md_finish(ctx, aux_out));
|
2021-09-27 14:28:31 +02:00
|
|
|
|
|
|
|
/* Now compute HASH(okey + inner_hash) */
|
2023-01-11 14:50:10 +01:00
|
|
|
MD_CHK(mbedtls_md_starts(ctx));
|
|
|
|
MD_CHK(mbedtls_md_update(ctx, okey, block_size));
|
|
|
|
MD_CHK(mbedtls_md_update(ctx, output, hash_size));
|
|
|
|
MD_CHK(mbedtls_md_finish(ctx, output));
|
2021-09-27 14:28:31 +02:00
|
|
|
|
|
|
|
/* Done, get ready for next time */
|
2023-01-11 14:50:10 +01:00
|
|
|
MD_CHK(mbedtls_md_hmac_reset(ctx));
|
2021-09-27 14:28:31 +02:00
|
|
|
|
|
|
|
#undef MD_CHK
|
|
|
|
|
|
|
|
cleanup:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_md_free(&aux);
|
|
|
|
return ret;
|
2021-09-27 14:28:31 +02:00
|
|
|
}
|
2022-02-25 15:09:36 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
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
|
|
|
|
|
|
|
#if defined(MBEDTLS_BIGNUM_C)
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
#define MPI_VALIDATE_RET(cond) \
|
|
|
|
MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_MPI_BAD_INPUT_DATA)
|
2021-09-27 15:33:35 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Conditionally assign X = Y, without leaking information
|
|
|
|
* about whether the assignment was made or not.
|
|
|
|
* (Leaking information about the respective sizes of X and Y is ok however.)
|
|
|
|
*/
|
2022-01-31 22:34:01 +01:00
|
|
|
#if defined(_MSC_VER) && defined(_M_ARM64) && (_MSC_FULL_VER < 193131103)
|
2022-01-27 00:33:27 +01:00
|
|
|
/*
|
2022-01-31 22:34:01 +01:00
|
|
|
* MSVC miscompiles this function if it's inlined prior to Visual Studio 2022 version 17.1. See:
|
2022-01-27 00:33:27 +01:00
|
|
|
* https://developercommunity.visualstudio.com/t/c-compiler-miscompiles-part-of-mbedtls-library-on/1646989
|
|
|
|
*/
|
|
|
|
__declspec(noinline)
|
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_safe_cond_assign(mbedtls_mpi *X,
|
|
|
|
const mbedtls_mpi *Y,
|
|
|
|
unsigned char assign)
|
2021-09-27 15:33:35 +02:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(X != NULL);
|
|
|
|
MPI_VALIDATE_RET(Y != NULL);
|
2021-09-27 15:33:35 +02:00
|
|
|
|
|
|
|
/* all-bits 1 if assign is 1, all-bits 0 if assign is 0 */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_uint limb_mask = mbedtls_ct_mpi_uint_mask(assign);
|
2021-09-27 15:33:35 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, Y->n));
|
2021-09-27 15:33:35 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
X->s = mbedtls_ct_cond_select_sign(assign, Y->s, X->s);
|
2021-09-27 15:33:35 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_core_cond_assign(X->p, Y->p, Y->n, assign);
|
2021-09-27 15:33:35 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = Y->n; i < X->n; i++) {
|
2021-09-27 15:33:35 +02:00
|
|
|
X->p[i] &= ~limb_mask;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-09-27 15:33:35 +02:00
|
|
|
|
|
|
|
cleanup:
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2021-09-27 15:33:35 +02:00
|
|
|
}
|
|
|
|
|
2021-09-27 15:37:50 +02:00
|
|
|
/*
|
|
|
|
* Conditionally swap X and Y, without leaking information
|
|
|
|
* about whether the swap was made or not.
|
2021-12-21 06:14:10 +01:00
|
|
|
* Here it is not ok to simply swap the pointers, which would lead to
|
2021-09-27 15:37:50 +02:00
|
|
|
* different memory access patterns when X and Y are used afterwards.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_safe_cond_swap(mbedtls_mpi *X,
|
|
|
|
mbedtls_mpi *Y,
|
|
|
|
unsigned char swap)
|
2021-09-27 15:37:50 +02:00
|
|
|
{
|
2022-10-10 14:32:09 +02:00
|
|
|
int ret = 0;
|
|
|
|
int s;
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(X != NULL);
|
|
|
|
MPI_VALIDATE_RET(Y != NULL);
|
2021-09-27 15:37:50 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (X == Y) {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-09-27 15:37:50 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, Y->n));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(Y, X->n));
|
2021-09-27 15:37:50 +02:00
|
|
|
|
|
|
|
s = X->s;
|
2023-01-11 14:50:10 +01:00
|
|
|
X->s = mbedtls_ct_cond_select_sign(swap, Y->s, X->s);
|
|
|
|
Y->s = mbedtls_ct_cond_select_sign(swap, s, Y->s);
|
2021-09-27 15:37:50 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_core_cond_swap(X->p, Y->p, X->n, swap);
|
2021-09-27 15:37:50 +02:00
|
|
|
|
|
|
|
cleanup:
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2021-09-27 15:37:50 +02:00
|
|
|
}
|
|
|
|
|
2022-07-22 19:24:06 +02:00
|
|
|
/*
|
|
|
|
* Compare unsigned values in constant time
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned mbedtls_mpi_core_lt_ct(const mbedtls_mpi_uint *A,
|
|
|
|
const mbedtls_mpi_uint *B,
|
|
|
|
size_t limbs)
|
2022-07-22 19:24:06 +02:00
|
|
|
{
|
|
|
|
unsigned ret, cond, done;
|
|
|
|
|
2022-08-11 18:42:59 +02:00
|
|
|
/* The value of any of these variables is either 0 or 1 for the rest of
|
|
|
|
* their scope. */
|
2022-07-22 19:24:06 +02:00
|
|
|
ret = cond = done = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = limbs; i > 0; i--) {
|
2022-07-22 19:24:06 +02:00
|
|
|
/*
|
2022-08-19 13:24:40 +02:00
|
|
|
* If B[i - 1] < A[i - 1] then A < B is false and the result must
|
2022-07-22 19:24:06 +02:00
|
|
|
* remain 0.
|
|
|
|
*
|
|
|
|
* Again even if we can make a decision, we just mark the result and
|
|
|
|
* the fact that we are done and continue looping.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
cond = mbedtls_ct_mpi_uint_lt(B[i - 1], A[i - 1]);
|
2022-07-22 19:24:06 +02:00
|
|
|
done |= cond;
|
|
|
|
|
|
|
|
/*
|
2022-08-19 13:24:40 +02:00
|
|
|
* If A[i - 1] < B[i - 1] then A < B is true.
|
2022-07-22 19:24:06 +02:00
|
|
|
*
|
|
|
|
* Again even if we can make a decision, we just mark the result and
|
|
|
|
* the fact that we are done and continue looping.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
cond = mbedtls_ct_mpi_uint_lt(A[i - 1], B[i - 1]);
|
|
|
|
ret |= cond & (1 - done);
|
2022-07-22 19:24:06 +02:00
|
|
|
done |= cond;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2022-08-19 13:24:40 +02:00
|
|
|
* If all the limbs were equal, then the numbers are equal, A < B is false
|
2022-07-22 19:24:06 +02:00
|
|
|
* and leaving the result 0 is correct.
|
|
|
|
*/
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-07-22 19:24:06 +02:00
|
|
|
}
|
|
|
|
|
2021-09-27 15:41:30 +02:00
|
|
|
/*
|
|
|
|
* Compare signed values in constant time
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_lt_mpi_ct(const mbedtls_mpi *X,
|
|
|
|
const mbedtls_mpi *Y,
|
|
|
|
unsigned *ret)
|
2021-09-27 15:41:30 +02:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
/* The value of any of these variables is either 0 or 1 at all times. */
|
|
|
|
unsigned cond, done, X_is_negative, Y_is_negative;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MPI_VALIDATE_RET(X != NULL);
|
|
|
|
MPI_VALIDATE_RET(Y != NULL);
|
|
|
|
MPI_VALIDATE_RET(ret != NULL);
|
2021-09-27 15:41:30 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (X->n != Y->n) {
|
2021-09-27 15:41:30 +02:00
|
|
|
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-09-27 15:41:30 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set sign_N to 1 if N >= 0, 0 if N < 0.
|
|
|
|
* We know that N->s == 1 if N >= 0 and N->s == -1 if N < 0.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
X_is_negative = (X->s & 2) >> 1;
|
|
|
|
Y_is_negative = (Y->s & 2) >> 1;
|
2021-09-27 15:41:30 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the signs are different, then the positive operand is the bigger.
|
|
|
|
* That is if X is negative (X_is_negative == 1), then X < Y is true and it
|
|
|
|
* is false if X is positive (X_is_negative == 0).
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
cond = (X_is_negative ^ Y_is_negative);
|
2021-09-27 15:41:30 +02:00
|
|
|
*ret = cond & X_is_negative;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is a constant-time function. We might have the result, but we still
|
|
|
|
* need to go through the loop. Record if we have the result already.
|
|
|
|
*/
|
|
|
|
done = cond;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = X->n; i > 0; i--) {
|
2021-09-27 15:41:30 +02:00
|
|
|
/*
|
|
|
|
* If Y->p[i - 1] < X->p[i - 1] then X < Y is true if and only if both
|
|
|
|
* X and Y are negative.
|
|
|
|
*
|
|
|
|
* Again even if we can make a decision, we just mark the result and
|
|
|
|
* the fact that we are done and continue looping.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
cond = mbedtls_ct_mpi_uint_lt(Y->p[i - 1], X->p[i - 1]);
|
|
|
|
*ret |= cond & (1 - done) & X_is_negative;
|
2021-09-27 15:41:30 +02:00
|
|
|
done |= cond;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If X->p[i - 1] < Y->p[i - 1] then X < Y is true if and only if both
|
|
|
|
* X and Y are positive.
|
|
|
|
*
|
|
|
|
* Again even if we can make a decision, we just mark the result and
|
|
|
|
* the fact that we are done and continue looping.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
cond = mbedtls_ct_mpi_uint_lt(X->p[i - 1], Y->p[i - 1]);
|
|
|
|
*ret |= cond & (1 - done) & (1 - X_is_negative);
|
2021-09-27 15:41:30 +02:00
|
|
|
done |= cond;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2021-09-27 15:41:30 +02:00
|
|
|
}
|
|
|
|
|
2021-09-27 15:33:35 +02:00
|
|
|
#endif /* MBEDTLS_BIGNUM_C */
|
2021-09-27 16:11:12 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_RSA_ALT)
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ct_rsaes_pkcs1_v15_unpadding(unsigned char *input,
|
|
|
|
size_t ilen,
|
|
|
|
unsigned char *output,
|
|
|
|
size_t output_max_len,
|
|
|
|
size_t *olen)
|
2021-09-27 16:11:12 +02:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
size_t i, plaintext_max_size;
|
|
|
|
|
|
|
|
/* The following variables take sensitive values: their value must
|
|
|
|
* not leak into the observable behavior of the function other than
|
|
|
|
* the designated outputs (output, olen, return value). Otherwise
|
|
|
|
* this would open the execution of the function to
|
|
|
|
* side-channel-based variants of the Bleichenbacher padding oracle
|
|
|
|
* attack. Potential side channels include overall timing, memory
|
|
|
|
* access patterns (especially visible to an adversary who has access
|
|
|
|
* to a shared memory cache), and branches (especially visible to
|
|
|
|
* an adversary who has access to a shared code cache or to a shared
|
|
|
|
* branch predictor). */
|
|
|
|
size_t pad_count = 0;
|
|
|
|
unsigned bad = 0;
|
|
|
|
unsigned char pad_done = 0;
|
|
|
|
size_t plaintext_size = 0;
|
|
|
|
unsigned output_too_large;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
plaintext_max_size = (output_max_len > ilen - 11) ? ilen - 11
|
2021-10-18 16:12:45 +02:00
|
|
|
: output_max_len;
|
2021-09-27 16:11:12 +02:00
|
|
|
|
|
|
|
/* Check and get padding length in constant time and constant
|
|
|
|
* memory trace. The first byte must be 0. */
|
2021-10-18 16:17:57 +02:00
|
|
|
bad |= input[0];
|
2021-09-27 16:11:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
|
2021-10-18 16:17:57 +02:00
|
|
|
* where PS must be at least 8 nonzero bytes. */
|
|
|
|
bad |= input[1] ^ MBEDTLS_RSA_CRYPT;
|
2021-09-27 16:11:12 +02:00
|
|
|
|
|
|
|
/* Read the whole buffer. Set pad_done to nonzero if we find
|
2021-10-18 16:17:57 +02:00
|
|
|
* the 0x00 byte and remember the padding length in pad_count. */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 2; i < ilen; i++) {
|
|
|
|
pad_done |= ((input[i] | (unsigned char) -input[i]) >> 7) ^ 1;
|
|
|
|
pad_count += ((pad_done | (unsigned char) -pad_done) >> 7) ^ 1;
|
2021-09-27 16:11:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* If pad_done is still zero, there's no data, only unfinished padding. */
|
2023-01-11 14:50:10 +01:00
|
|
|
bad |= mbedtls_ct_uint_if(pad_done, 0, 1);
|
2021-09-27 16:11:12 +02:00
|
|
|
|
|
|
|
/* There must be at least 8 bytes of padding. */
|
2023-01-11 14:50:10 +01:00
|
|
|
bad |= mbedtls_ct_size_gt(8, pad_count);
|
2021-09-27 16:11:12 +02:00
|
|
|
|
|
|
|
/* If the padding is valid, set plaintext_size to the number of
|
|
|
|
* remaining bytes after stripping the padding. If the padding
|
|
|
|
* is invalid, avoid leaking this fact through the size of the
|
|
|
|
* output: use the maximum message size that fits in the output
|
|
|
|
* buffer. Do it without branches to avoid leaking the padding
|
|
|
|
* validity through timing. RSA keys are small enough that all the
|
|
|
|
* size_t values involved fit in unsigned int. */
|
2021-10-20 11:59:27 +02:00
|
|
|
plaintext_size = mbedtls_ct_uint_if(
|
2023-01-11 14:50:10 +01:00
|
|
|
bad, (unsigned) plaintext_max_size,
|
|
|
|
(unsigned) (ilen - pad_count - 3));
|
2021-09-27 16:11:12 +02:00
|
|
|
|
|
|
|
/* Set output_too_large to 0 if the plaintext fits in the output
|
|
|
|
* buffer and to 1 otherwise. */
|
2023-01-11 14:50:10 +01:00
|
|
|
output_too_large = mbedtls_ct_size_gt(plaintext_size,
|
|
|
|
plaintext_max_size);
|
2021-09-27 16:11:12 +02:00
|
|
|
|
|
|
|
/* Set ret without branches to avoid timing attacks. Return:
|
|
|
|
* - INVALID_PADDING if the padding is bad (bad != 0).
|
|
|
|
* - OUTPUT_TOO_LARGE if the padding is good but the decrypted
|
|
|
|
* plaintext does not fit in the output buffer.
|
|
|
|
* - 0 if the padding is correct. */
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = -(int) mbedtls_ct_uint_if(
|
|
|
|
bad, -MBEDTLS_ERR_RSA_INVALID_PADDING,
|
|
|
|
mbedtls_ct_uint_if(output_too_large,
|
|
|
|
-MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE,
|
|
|
|
0));
|
2021-09-27 16:11:12 +02:00
|
|
|
|
|
|
|
/* If the padding is bad or the plaintext is too large, zero the
|
|
|
|
* data that we're about to copy to the output buffer.
|
|
|
|
* We need to copy the same amount of data
|
|
|
|
* from the same buffer whether the padding is good or not to
|
|
|
|
* avoid leaking the padding validity through overall timing or
|
|
|
|
* through memory or cache access patterns. */
|
2023-01-11 14:50:10 +01:00
|
|
|
bad = mbedtls_ct_uint_mask(bad | output_too_large);
|
|
|
|
for (i = 11; i < ilen; i++) {
|
2021-10-18 16:17:57 +02:00
|
|
|
input[i] &= ~bad;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-09-27 16:11:12 +02:00
|
|
|
|
|
|
|
/* If the plaintext is too large, truncate it to the buffer size.
|
|
|
|
* Copy anyway to avoid revealing the length through timing, because
|
|
|
|
* revealing the length is as bad as revealing the padding validity
|
|
|
|
* for a Bleichenbacher attack. */
|
2023-01-11 14:50:10 +01:00
|
|
|
plaintext_size = mbedtls_ct_uint_if(output_too_large,
|
|
|
|
(unsigned) plaintext_max_size,
|
|
|
|
(unsigned) plaintext_size);
|
2021-09-27 16:11:12 +02:00
|
|
|
|
|
|
|
/* Move the plaintext to the leftmost position where it can start in
|
|
|
|
* the working buffer, i.e. make it start plaintext_max_size from
|
|
|
|
* the end of the buffer. Do this with a memory access trace that
|
|
|
|
* does not depend on the plaintext size. After this move, the
|
|
|
|
* starting location of the plaintext is no longer sensitive
|
|
|
|
* information. */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ct_mem_move_to_left(input + ilen - plaintext_max_size,
|
|
|
|
plaintext_max_size,
|
|
|
|
plaintext_max_size - plaintext_size);
|
2021-09-27 16:11:12 +02:00
|
|
|
|
|
|
|
/* Finally copy the decrypted plaintext plus trailing zeros into the output
|
|
|
|
* buffer. If output_max_len is 0, then output may be an invalid pointer
|
|
|
|
* and the result of memcpy() would be undefined; prevent undefined
|
|
|
|
* behavior making sure to depend only on output_max_len (the size of the
|
|
|
|
* user-provided output buffer), which is independent from plaintext
|
|
|
|
* length, validity of padding, success of the decryption, and other
|
|
|
|
* secrets. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (output_max_len != 0) {
|
|
|
|
memcpy(output, input + ilen - plaintext_max_size, plaintext_max_size);
|
|
|
|
}
|
2021-09-27 16:11:12 +02:00
|
|
|
|
|
|
|
/* Report the amount of data we copied to the output buffer. In case
|
|
|
|
* of errors (bad padding or output too large), the value of *olen
|
|
|
|
* when this function returns is not specified. Making it equivalent
|
|
|
|
* to the good case limits the risks of leaking the padding validity. */
|
|
|
|
*olen = plaintext_size;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2021-09-27 16:11:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C && ! MBEDTLS_RSA_ALT */
|