Merge pull request #1076 from daverodgman/more-ct

Use CT module more consistently
This commit is contained in:
Dave Rodgman 2023-09-25 11:50:10 +01:00 committed by GitHub
commit 025bed9eb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 273 additions and 58 deletions

View File

@ -83,7 +83,7 @@ int mbedtls_mpi_lt_mpi_ct(const mbedtls_mpi *X,
* That is if X is negative (X_is_negative == 1), then X < Y is true and it * 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). * is false if X is positive (X_is_negative == 0).
*/ */
different_sign = mbedtls_ct_bool_xor(X_is_negative, Y_is_negative); // non-zero if different sign different_sign = mbedtls_ct_bool_ne(X_is_negative, Y_is_negative); // true if different sign
result = mbedtls_ct_bool_and(different_sign, X_is_negative); result = mbedtls_ct_bool_and(different_sign, X_is_negative);
/* /*

View File

@ -30,6 +30,7 @@
#include "mbedtls/platform_util.h" #include "mbedtls/platform_util.h"
#include "mbedtls/error.h" #include "mbedtls/error.h"
#include "mbedtls/constant_time.h" #include "mbedtls/constant_time.h"
#include "constant_time_internal.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -848,7 +849,7 @@ static int get_pkcs_padding(unsigned char *input, size_t input_len,
size_t *data_len) size_t *data_len)
{ {
size_t i, pad_idx; size_t i, pad_idx;
unsigned char padding_len, bad = 0; unsigned char padding_len;
if (NULL == input || NULL == data_len) { if (NULL == input || NULL == data_len) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
@ -857,18 +858,19 @@ static int get_pkcs_padding(unsigned char *input, size_t input_len,
padding_len = input[input_len - 1]; padding_len = input[input_len - 1];
*data_len = input_len - padding_len; *data_len = input_len - padding_len;
/* Avoid logical || since it results in a branch */ mbedtls_ct_condition_t bad = mbedtls_ct_uint_gt(padding_len, input_len);
bad |= padding_len > input_len; bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_eq(padding_len, 0));
bad |= padding_len == 0;
/* The number of bytes checked must be independent of padding_len, /* The number of bytes checked must be independent of padding_len,
* so pick input_len, which is usually 8 or 16 (one block) */ * so pick input_len, which is usually 8 or 16 (one block) */
pad_idx = input_len - padding_len; pad_idx = input_len - padding_len;
for (i = 0; i < input_len; i++) { for (i = 0; i < input_len; i++) {
bad |= (input[i] ^ padding_len) * (i >= pad_idx); mbedtls_ct_condition_t in_padding = mbedtls_ct_uint_ge(i, pad_idx);
mbedtls_ct_condition_t different = mbedtls_ct_uint_ne(input[i], padding_len);
bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool_and(in_padding, different));
} }
return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0); return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING);
} }
#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */ #endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
@ -891,24 +893,28 @@ static void add_one_and_zeros_padding(unsigned char *output,
static int get_one_and_zeros_padding(unsigned char *input, size_t input_len, static int get_one_and_zeros_padding(unsigned char *input, size_t input_len,
size_t *data_len) size_t *data_len)
{ {
size_t i;
unsigned char done = 0, prev_done, bad;
if (NULL == input || NULL == data_len) { if (NULL == input || NULL == data_len) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
} }
bad = 0x80; mbedtls_ct_condition_t in_padding = MBEDTLS_CT_TRUE;
mbedtls_ct_condition_t bad = MBEDTLS_CT_TRUE;
*data_len = 0; *data_len = 0;
for (i = input_len; i > 0; i--) {
prev_done = done; for (ptrdiff_t i = (ptrdiff_t) (input_len) - 1; i >= 0; i--) {
done |= (input[i - 1] != 0); mbedtls_ct_condition_t is_nonzero = mbedtls_ct_bool(input[i]);
*data_len |= (i - 1) * (done != prev_done);
bad ^= input[i - 1] * (done != prev_done); mbedtls_ct_condition_t hit_first_nonzero = mbedtls_ct_bool_and(is_nonzero, in_padding);
*data_len = mbedtls_ct_size_if(hit_first_nonzero, i, *data_len);
bad = mbedtls_ct_bool_if(hit_first_nonzero, mbedtls_ct_uint_ne(input[i], 0x80), bad);
in_padding = mbedtls_ct_bool_and(in_padding, mbedtls_ct_bool_not(is_nonzero));
} }
return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0); return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING);
} }
#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */ #endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
@ -932,7 +938,8 @@ static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
size_t *data_len) size_t *data_len)
{ {
size_t i, pad_idx; size_t i, pad_idx;
unsigned char padding_len, bad = 0; unsigned char padding_len;
mbedtls_ct_condition_t bad;
if (NULL == input || NULL == data_len) { if (NULL == input || NULL == data_len) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
@ -942,16 +949,19 @@ static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
*data_len = input_len - padding_len; *data_len = input_len - padding_len;
/* Avoid logical || since it results in a branch */ /* Avoid logical || since it results in a branch */
bad |= padding_len > input_len; bad = mbedtls_ct_uint_gt(padding_len, input_len);
bad |= padding_len == 0; bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_eq(padding_len, 0));
/* The number of bytes checked must be independent of padding_len */ /* The number of bytes checked must be independent of padding_len */
pad_idx = input_len - padding_len; pad_idx = input_len - padding_len;
for (i = 0; i < input_len - 1; i++) { for (i = 0; i < input_len - 1; i++) {
bad |= input[i] * (i >= pad_idx); mbedtls_ct_condition_t is_padding = mbedtls_ct_uint_ge(i, pad_idx);
mbedtls_ct_condition_t nonzero_pad_byte;
nonzero_pad_byte = mbedtls_ct_bool_if_else_0(is_padding, mbedtls_ct_bool(input[i]));
bad = mbedtls_ct_bool_or(bad, nonzero_pad_byte);
} }
return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0); return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING);
} }
#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */ #endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
@ -962,18 +972,14 @@ static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
static void add_zeros_padding(unsigned char *output, static void add_zeros_padding(unsigned char *output,
size_t output_len, size_t data_len) size_t output_len, size_t data_len)
{ {
size_t i; memset(output + data_len, 0, output_len - data_len);
for (i = data_len; i < output_len; i++) {
output[i] = 0x00;
}
} }
static int get_zeros_padding(unsigned char *input, size_t input_len, static int get_zeros_padding(unsigned char *input, size_t input_len,
size_t *data_len) size_t *data_len)
{ {
size_t i; size_t i;
unsigned char done = 0, prev_done; mbedtls_ct_condition_t done = MBEDTLS_CT_FALSE, prev_done;
if (NULL == input || NULL == data_len) { if (NULL == input || NULL == data_len) {
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
@ -982,8 +988,8 @@ static int get_zeros_padding(unsigned char *input, size_t input_len,
*data_len = 0; *data_len = 0;
for (i = input_len; i > 0; i--) { for (i = input_len; i > 0; i--) {
prev_done = done; prev_done = done;
done |= (input[i-1] != 0); done = mbedtls_ct_bool_or(done, mbedtls_ct_uint_ne(input[i-1], 0));
*data_len |= i * (done != prev_done); *data_len = mbedtls_ct_size_if(mbedtls_ct_bool_ne(done, prev_done), i, *data_len);
} }
return 0; return 0;

View File

@ -331,7 +331,6 @@ static inline unsigned char mbedtls_ct_uchar_in_range_if(unsigned char low,
return (unsigned char) (~(low_mask | high_mask)) & to; return (unsigned char) (~(low_mask | high_mask)) & to;
} }
/* ============================================================================ /* ============================================================================
* Everything below here is trivial wrapper functions * Everything below here is trivial wrapper functions
*/ */
@ -350,6 +349,14 @@ static inline unsigned mbedtls_ct_uint_if(mbedtls_ct_condition_t condition,
return (unsigned) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1, (mbedtls_ct_uint_t) if0); return (unsigned) mbedtls_ct_if(condition, (mbedtls_ct_uint_t) if1, (mbedtls_ct_uint_t) if0);
} }
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);
}
#if defined(MBEDTLS_BIGNUM_C) #if defined(MBEDTLS_BIGNUM_C)
static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if(mbedtls_ct_condition_t condition, static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if(mbedtls_ct_condition_t condition,
@ -373,6 +380,12 @@ static inline unsigned mbedtls_ct_uint_if_else_0(mbedtls_ct_condition_t conditio
return (unsigned) (condition & if1); return (unsigned) (condition & if1);
} }
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);
}
#if defined(MBEDTLS_BIGNUM_C) #if defined(MBEDTLS_BIGNUM_C)
static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if_else_0(mbedtls_ct_condition_t condition, static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if_else_0(mbedtls_ct_condition_t condition,
@ -383,6 +396,23 @@ static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if_else_0(mbedtls_ct_conditio
#endif /* MBEDTLS_BIGNUM_C */ #endif /* MBEDTLS_BIGNUM_C */
static inline int mbedtls_ct_error_if(mbedtls_ct_condition_t condition, int if1, int if0)
{
/* 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.
*/
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)));
}
static inline mbedtls_ct_condition_t mbedtls_ct_uint_eq(mbedtls_ct_uint_t x, static inline mbedtls_ct_condition_t mbedtls_ct_uint_eq(mbedtls_ct_uint_t x,
mbedtls_ct_uint_t y) mbedtls_ct_uint_t y)
{ {
@ -407,8 +437,8 @@ static inline mbedtls_ct_condition_t mbedtls_ct_uint_le(mbedtls_ct_uint_t x,
return ~mbedtls_ct_uint_gt(x, y); return ~mbedtls_ct_uint_gt(x, y);
} }
static inline mbedtls_ct_condition_t mbedtls_ct_bool_xor(mbedtls_ct_condition_t x, static inline mbedtls_ct_condition_t mbedtls_ct_bool_ne(mbedtls_ct_condition_t x,
mbedtls_ct_condition_t y) mbedtls_ct_condition_t y)
{ {
return (mbedtls_ct_condition_t) (x ^ y); return (mbedtls_ct_condition_t) (x ^ y);
} }

View File

@ -194,11 +194,11 @@ static inline mbedtls_ct_condition_t mbedtls_ct_uint_ge(mbedtls_ct_uint_t x,
static inline mbedtls_ct_condition_t mbedtls_ct_uint_le(mbedtls_ct_uint_t x, static inline mbedtls_ct_condition_t mbedtls_ct_uint_le(mbedtls_ct_uint_t x,
mbedtls_ct_uint_t y); mbedtls_ct_uint_t y);
/** Boolean "xor" operation. /** Boolean not-equals operation.
* *
* Functionally equivalent to: * Functionally equivalent to:
* *
* \p x ^ \p y * \p x != \p y
* *
* \param x The first value to analyze. * \param x The first value to analyze.
* \param y The second value to analyze. * \param y The second value to analyze.
@ -206,11 +206,11 @@ static inline mbedtls_ct_condition_t mbedtls_ct_uint_le(mbedtls_ct_uint_t x,
* \note This is more efficient than mbedtls_ct_uint_ne if both arguments are * \note This is more efficient than mbedtls_ct_uint_ne if both arguments are
* mbedtls_ct_condition_t. * mbedtls_ct_condition_t.
* *
* \return MBEDTLS_CT_TRUE if \p x ^ \p y, * \return MBEDTLS_CT_TRUE if \p x != \p y,
* otherwise MBEDTLS_CT_FALSE. * otherwise MBEDTLS_CT_FALSE.
*/ */
static inline mbedtls_ct_condition_t mbedtls_ct_bool_xor(mbedtls_ct_condition_t x, static inline mbedtls_ct_condition_t mbedtls_ct_bool_ne(mbedtls_ct_condition_t x,
mbedtls_ct_condition_t y); mbedtls_ct_condition_t y);
/** Boolean "and" operation. /** Boolean "and" operation.
* *
@ -291,6 +291,22 @@ static inline unsigned mbedtls_ct_uint_if(mbedtls_ct_condition_t condition,
unsigned if1, unsigned if1,
unsigned if0); unsigned if0);
/** Choose between two mbedtls_ct_condition_t values.
*
* Functionally equivalent to:
*
* condition ? if1 : if0.
*
* \param condition Condition to test.
* \param if1 Value to use if \p condition == MBEDTLS_CT_TRUE.
* \param if0 Value to use if \p condition == MBEDTLS_CT_FALSE.
*
* \return \c if1 if \p condition == MBEDTLS_CT_TRUE, otherwise \c if0.
*/
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);
#if defined(MBEDTLS_BIGNUM_C) #if defined(MBEDTLS_BIGNUM_C)
/** Choose between two mbedtls_mpi_uint values. /** Choose between two mbedtls_mpi_uint values.
@ -327,6 +343,23 @@ static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if(mbedtls_ct_condition_t con
*/ */
static inline unsigned mbedtls_ct_uint_if_else_0(mbedtls_ct_condition_t condition, unsigned if1); static inline unsigned mbedtls_ct_uint_if_else_0(mbedtls_ct_condition_t condition, unsigned if1);
/** Choose between an mbedtls_ct_condition_t and 0.
*
* Functionally equivalent to:
*
* condition ? if1 : 0.
*
* Functionally equivalent to mbedtls_ct_bool_if(condition, if1, 0) but
* results in smaller code size.
*
* \param condition Condition to test.
* \param if1 Value to use if \p condition == MBEDTLS_CT_TRUE.
*
* \return \c if1 if \p condition == MBEDTLS_CT_TRUE, otherwise 0.
*/
static inline mbedtls_ct_condition_t mbedtls_ct_bool_if_else_0(mbedtls_ct_condition_t condition,
mbedtls_ct_condition_t if1);
/** Choose between a size_t value and 0. /** Choose between a size_t value and 0.
* *
* Functionally equivalent to: * Functionally equivalent to:
@ -378,6 +411,35 @@ static inline unsigned char mbedtls_ct_uchar_in_range_if(unsigned char low,
unsigned char c, unsigned char c,
unsigned char t); unsigned char t);
/** Choose between two error values. The values must be in the range [-32767..0].
*
* Functionally equivalent to:
*
* condition ? if1 : if0.
*
* \param condition Condition to test.
* \param if1 Value to use if \p condition == MBEDTLS_CT_TRUE.
* \param if0 Value to use if \p condition == MBEDTLS_CT_FALSE.
*
* \return \c if1 if \p condition == MBEDTLS_CT_TRUE, otherwise \c if0.
*/
static inline int mbedtls_ct_error_if(mbedtls_ct_condition_t condition, int if1, int if0);
/** Choose between an error value and 0. The error value must be in the range [-32767..0].
*
* Functionally equivalent to:
*
* condition ? if1 : 0.
*
* Functionally equivalent to mbedtls_ct_error_if(condition, if1, 0) but
* results in smaller code size.
*
* \param condition Condition to test.
* \param if1 Value to use if \p condition == MBEDTLS_CT_TRUE.
*
* \return \c if1 if \p condition == MBEDTLS_CT_TRUE, otherwise 0.
*/
static inline int mbedtls_ct_error_if_else_0(mbedtls_ct_condition_t condition, int if1);
/* ============================================================================ /* ============================================================================
* Block memory operations * Block memory operations

View File

@ -421,8 +421,8 @@ int mbedtls_nist_kw_unwrap(mbedtls_nist_kw_context *ctx,
* larger than 8, because of the type wrap around. * larger than 8, because of the type wrap around.
*/ */
padlen = in_len - KW_SEMIBLOCK_LENGTH - Plen; padlen = in_len - KW_SEMIBLOCK_LENGTH - Plen;
ret = -((int) mbedtls_ct_uint_if(mbedtls_ct_uint_gt(padlen, 7), ret = mbedtls_ct_error_if(mbedtls_ct_uint_gt(padlen, 7),
-MBEDTLS_ERR_CIPHER_AUTH_FAILED, -ret)); MBEDTLS_ERR_CIPHER_AUTH_FAILED, ret);
padlen &= 7; padlen &= 7;
/* Check padding in "constant-time" */ /* Check padding in "constant-time" */

View File

@ -158,12 +158,10 @@ static int mbedtls_ct_rsaes_pkcs1_v15_unpadding(unsigned char *input,
* - OUTPUT_TOO_LARGE if the padding is good but the decrypted * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
* plaintext does not fit in the output buffer. * plaintext does not fit in the output buffer.
* - 0 if the padding is correct. */ * - 0 if the padding is correct. */
ret = -(int) mbedtls_ct_uint_if( ret = mbedtls_ct_error_if(
bad, bad,
(unsigned) (-(MBEDTLS_ERR_RSA_INVALID_PADDING)), MBEDTLS_ERR_RSA_INVALID_PADDING,
mbedtls_ct_uint_if_else_0( mbedtls_ct_error_if_else_0(output_too_large, MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE)
output_too_large,
(unsigned) (-(MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE)))
); );
/* If the padding is bad or the plaintext is too large, zero the /* If the padding is bad or the plaintext is too large, zero the
@ -1541,8 +1539,8 @@ int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
{ {
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t ilen, i, pad_len; size_t ilen, i, pad_len;
unsigned char *p, pad_done; unsigned char *p;
int bad; mbedtls_ct_condition_t bad, in_padding;
unsigned char buf[MBEDTLS_MPI_MAX_SIZE]; unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
unsigned char lhash[MBEDTLS_MD_MAX_SIZE]; unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
unsigned int hlen; unsigned int hlen;
@ -1602,27 +1600,26 @@ int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
* Check contents, in "constant-time" * Check contents, in "constant-time"
*/ */
p = buf; p = buf;
bad = 0;
bad |= *p++; /* First byte must be 0 */ bad = mbedtls_ct_bool(*p++); /* First byte must be 0 */
p += hlen; /* Skip seed */ p += hlen; /* Skip seed */
/* Check lHash */ /* Check lHash */
bad |= mbedtls_ct_memcmp(lhash, p, hlen); bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool(mbedtls_ct_memcmp(lhash, p, hlen)));
p += hlen; p += hlen;
/* Get zero-padding len, but always read till end of buffer /* Get zero-padding len, but always read till end of buffer
* (minus one, for the 01 byte) */ * (minus one, for the 01 byte) */
pad_len = 0; pad_len = 0;
pad_done = 0; in_padding = MBEDTLS_CT_TRUE;
for (i = 0; i < ilen - 2 * hlen - 2; i++) { for (i = 0; i < ilen - 2 * hlen - 2; i++) {
pad_done |= p[i]; in_padding = mbedtls_ct_bool_and(in_padding, mbedtls_ct_uint_eq(p[i], 0));
pad_len += ((pad_done | (unsigned char) -pad_done) >> 7) ^ 1; pad_len += mbedtls_ct_uint_if_else_0(in_padding, 1);
} }
p += pad_len; p += pad_len;
bad |= *p++ ^ 0x01; bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(*p++, 0x01));
/* /*
* The only information "leaked" is whether the padding was correct or not * The only information "leaked" is whether the padding was correct or not
@ -1630,7 +1627,7 @@ int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
* recommendations in PKCS#1 v2.2: an opponent cannot distinguish between * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
* the different error conditions. * the different error conditions.
*/ */
if (bad != 0) { if (bad != MBEDTLS_CT_FALSE) {
ret = MBEDTLS_ERR_RSA_INVALID_PADDING; ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
goto cleanup; goto cleanup;
} }

View File

@ -646,6 +646,102 @@ mbedtls_ct_if:"0xffffffffffffffff":"0xffffffffffffffff":"0x7fffffffffffffff"
mbedtls_ct_if 0xffffffffffffffff 0xffffffffffffffff 0xffffffffffffffff mbedtls_ct_if 0xffffffffffffffff 0xffffffffffffffff 0xffffffffffffffff
mbedtls_ct_if:"0xffffffffffffffff":"0xffffffffffffffff":"0xffffffffffffffff" mbedtls_ct_if:"0xffffffffffffffff":"0xffffffffffffffff":"0xffffffffffffffff"
mbedtls_ct_error_if 0 0 0
mbedtls_ct_error_if:0:0:0
mbedtls_ct_error_if 0 0 -1
mbedtls_ct_error_if:0:0:-1
mbedtls_ct_error_if 0 0 -32766
mbedtls_ct_error_if:0:0:-32766
mbedtls_ct_error_if 0 0 -32767
mbedtls_ct_error_if:0:0:-32767
mbedtls_ct_error_if 0 -1 0
mbedtls_ct_error_if:0:-1:0
mbedtls_ct_error_if 0 -1 -1
mbedtls_ct_error_if:0:-1:-1
mbedtls_ct_error_if 0 -1 -32766
mbedtls_ct_error_if:0:-1:-32766
mbedtls_ct_error_if 0 -1 -32767
mbedtls_ct_error_if:0:-1:-32767
mbedtls_ct_error_if 0 -32766 0
mbedtls_ct_error_if:0:-32766:0
mbedtls_ct_error_if 0 -32766 -1
mbedtls_ct_error_if:0:-32766:-1
mbedtls_ct_error_if 0 -32766 -32766
mbedtls_ct_error_if:0:-32766:-32766
mbedtls_ct_error_if 0 -32766 -32767
mbedtls_ct_error_if:0:-32766:-32767
mbedtls_ct_error_if 0 -32767 0
mbedtls_ct_error_if:0:-32767:0
mbedtls_ct_error_if 0 -32767 -1
mbedtls_ct_error_if:0:-32767:-1
mbedtls_ct_error_if 0 -32767 -32766
mbedtls_ct_error_if:0:-32767:-32766
mbedtls_ct_error_if 0 -32767 -32767
mbedtls_ct_error_if:0:-32767:-32767
mbedtls_ct_error_if 1 0 0
mbedtls_ct_error_if:1:0:0
mbedtls_ct_error_if 1 0 -1
mbedtls_ct_error_if:1:0:-1
mbedtls_ct_error_if 1 0 -32766
mbedtls_ct_error_if:1:0:-32766
mbedtls_ct_error_if 1 0 -32767
mbedtls_ct_error_if:1:0:-32767
mbedtls_ct_error_if 1 -1 0
mbedtls_ct_error_if:1:-1:0
mbedtls_ct_error_if 1 -1 -1
mbedtls_ct_error_if:1:-1:-1
mbedtls_ct_error_if 1 -1 -32766
mbedtls_ct_error_if:1:-1:-32766
mbedtls_ct_error_if 1 -1 -32767
mbedtls_ct_error_if:1:-1:-32767
mbedtls_ct_error_if 1 -32766 0
mbedtls_ct_error_if:1:-32766:0
mbedtls_ct_error_if 1 -32766 -1
mbedtls_ct_error_if:1:-32766:-1
mbedtls_ct_error_if 1 -32766 -32766
mbedtls_ct_error_if:1:-32766:-32766
mbedtls_ct_error_if 1 -32766 -32767
mbedtls_ct_error_if:1:-32766:-32767
mbedtls_ct_error_if 1 -32767 0
mbedtls_ct_error_if:1:-32767:0
mbedtls_ct_error_if 1 -32767 -1
mbedtls_ct_error_if:1:-32767:-1
mbedtls_ct_error_if 1 -32767 -32766
mbedtls_ct_error_if:1:-32767:-32766
mbedtls_ct_error_if 1 -32767 -32767
mbedtls_ct_error_if:1:-32767:-32767
mbedtls_ct_zeroize_if 0x0 0 mbedtls_ct_zeroize_if 0x0 0
mbedtls_ct_zeroize_if:"0x0":0 mbedtls_ct_zeroize_if:"0x0":0

View File

@ -77,8 +77,8 @@ void mbedtls_ct_bool_xxx(char *x_str, char *y_str)
expected = x1 <= y1 ? MBEDTLS_CT_TRUE : MBEDTLS_CT_FALSE; expected = x1 <= y1 ? MBEDTLS_CT_TRUE : MBEDTLS_CT_FALSE;
TEST_EQUAL(mbedtls_ct_uint_le(x, y), expected); TEST_EQUAL(mbedtls_ct_uint_le(x, y), expected);
expected = (!!x1) ^ (!!y1) ? MBEDTLS_CT_TRUE : MBEDTLS_CT_FALSE; expected = (!!x1) != (!!y1) ? MBEDTLS_CT_TRUE : MBEDTLS_CT_FALSE;
TEST_EQUAL(mbedtls_ct_bool_xor(mbedtls_ct_bool(x), mbedtls_ct_bool(y)), expected); TEST_EQUAL(mbedtls_ct_bool_ne(mbedtls_ct_bool(x), mbedtls_ct_bool(y)), expected);
expected = (!!x1) && (!!y1) ? MBEDTLS_CT_TRUE : MBEDTLS_CT_FALSE; expected = (!!x1) && (!!y1) ? MBEDTLS_CT_TRUE : MBEDTLS_CT_FALSE;
TEST_EQUAL(mbedtls_ct_bool_and(mbedtls_ct_bool(x), mbedtls_ct_bool(y)), expected); TEST_EQUAL(mbedtls_ct_bool_and(mbedtls_ct_bool(x), mbedtls_ct_bool(y)), expected);
@ -114,6 +114,27 @@ void mbedtls_ct_uchar_in_range_if(int li, int hi, int ti)
} }
/* END_CASE */ /* END_CASE */
/* BEGIN_CASE */
void mbedtls_ct_error_if(int cond, int t, int f)
{
mbedtls_ct_condition_t c = mbedtls_ct_bool(cond);
int expected = c ? t : f;
int expected0 = c ? t : 0;
TEST_CF_SECRET(&c, sizeof(c));
TEST_CF_SECRET(&t, sizeof(t));
TEST_CF_SECRET(&f, sizeof(f));
TEST_EQUAL(mbedtls_ct_error_if(c, t, f), expected);
TEST_EQUAL(mbedtls_ct_error_if_else_0(c, t), expected0);
TEST_CF_PUBLIC(&c, sizeof(c));
TEST_CF_PUBLIC(&t, sizeof(t));
TEST_CF_PUBLIC(&f, sizeof(f));
}
/* END_CASE */
/* BEGIN_CASE */ /* BEGIN_CASE */
void mbedtls_ct_if(char *c_str, char *t_str, char *f_str) void mbedtls_ct_if(char *c_str, char *t_str, char *f_str)
{ {
@ -131,12 +152,15 @@ void mbedtls_ct_if(char *c_str, char *t_str, char *f_str)
TEST_EQUAL(mbedtls_ct_if(c, t, f), expected); TEST_EQUAL(mbedtls_ct_if(c, t, f), expected);
TEST_EQUAL(mbedtls_ct_size_if(c, t, f), (size_t) expected); TEST_EQUAL(mbedtls_ct_size_if(c, t, f), (size_t) expected);
TEST_EQUAL(mbedtls_ct_uint_if(c, t, f), (unsigned) expected); TEST_EQUAL(mbedtls_ct_uint_if(c, t, f), (unsigned) expected);
TEST_EQUAL(mbedtls_ct_bool_if(c, mbedtls_ct_bool(t), mbedtls_ct_bool(f)),
mbedtls_ct_bool(expected));
#if defined(MBEDTLS_BIGNUM_C) #if defined(MBEDTLS_BIGNUM_C)
TEST_EQUAL(mbedtls_ct_mpi_uint_if(c, t, f), (mbedtls_mpi_uint) expected); TEST_EQUAL(mbedtls_ct_mpi_uint_if(c, t, f), (mbedtls_mpi_uint) expected);
#endif #endif
TEST_EQUAL(mbedtls_ct_uint_if_else_0(c, t), (unsigned) expected0); TEST_EQUAL(mbedtls_ct_uint_if_else_0(c, t), (unsigned) expected0);
TEST_EQUAL(mbedtls_ct_size_if_else_0(c, (size_t) t), (size_t) expected0); TEST_EQUAL(mbedtls_ct_size_if_else_0(c, (size_t) t), (size_t) expected0);
TEST_EQUAL(mbedtls_ct_bool_if_else_0(c, mbedtls_ct_bool(t)), mbedtls_ct_bool(expected0));
#if defined(MBEDTLS_BIGNUM_C) #if defined(MBEDTLS_BIGNUM_C)
TEST_EQUAL(mbedtls_ct_mpi_uint_if_else_0(c, t), (mbedtls_mpi_uint) expected0); TEST_EQUAL(mbedtls_ct_mpi_uint_if_else_0(c, t), (mbedtls_mpi_uint) expected0);
#endif #endif