Merge branch 'baremetal' into masked-aes

This commit is contained in:
Shelly Liberman 2020-11-25 18:32:19 +02:00 committed by GitHub
commit 88da3c245b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 184 additions and 75 deletions

View file

@ -2970,6 +2970,18 @@
*/
//#define MBEDTLS_MEMORY_BUFFER_ALLOC_C
/**
* \def MBEDTLS_PLATFORM_FAULT_CALLBACKS
*
* Uncomment to provide your own alternate implementation for mbedtls_platform_fault(),
* used in library/platform_util.c to signal a fault injection in either
* mbedtls_platform_memcpy or mbedtls_platform_memset.
*
* You will need to provide a header "platform_fault.h" and an implementation at
* compile time.
*/
//#define MBEDTLS_PLATFORM_FAULT_CALLBACKS
/**
* \def MBEDTLS_NET_C
*

View file

@ -276,9 +276,9 @@ uint32_t mbedtls_platform_random_uint32( void );
* cryptographically secure RNG, but provide an RNG for utility
* functions.
*
* \note If the given range is [0, 0), 0 is returned.
*
* \param num Max-value for the generated random number, exclusive.
* Must be greater than zero, otherwise an undefined behavior
* will occur on "num % 0".
* The generated number will be on range [0, num).
*
* \return The generated random number.

View file

@ -114,6 +114,41 @@ void mbedtls_ccm_free( mbedtls_ccm_context *ctx )
mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ccm_context ) );
}
/* Durstenfeld's version of Fisher-Yates shuffle */
static void mbedtls_generate_permutation( unsigned char* table, size_t size )
{
size_t i, j;
for( i = 0; i < size; i++ )
{
table[i] = (unsigned char) i;
}
if( size < 2 )
{
return;
}
for( i = size - 1; i > 0; i-- )
{
unsigned char tmp;
j = mbedtls_platform_random_in_range( (uint32_t) i + 1 );
tmp = table[i];
table[i] = table[j];
table[j] = tmp;
}
}
static void mbedtls_generate_masks( unsigned char* table, size_t size )
{
size_t i;
for( i = 0; i < size; i++ )
{
table[i] = mbedtls_platform_random_in_range( 256 );
}
}
/*
* Macros for common operations.
* Results in smaller compiled code than static inline functions.
@ -122,30 +157,55 @@ void mbedtls_ccm_free( mbedtls_ccm_context *ctx )
/*
* Update the CBC-MAC state in y using a block in b
* (Always using b as the source helps the compiler optimise a bit better.)
* Initial b masking happens outside of this macro due to various sources of it.
*/
#define UPDATE_CBC_MAC \
for( i = 0; i < 16; i++ ) \
y[i] ^= b[i]; \
{ \
y[perm_table[i]] ^= b[perm_table[i]]; \
y[perm_table[i]] ^= mask_table[perm_table[i]]; \
} \
\
if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, y, 16, y, &olen ) ) != 0 ) \
return( ret );
/*
* Copy src to dst starting at a random offset, while masking the whole dst buffer.
*/
#define COPY_MASK( dst, src, mask, len_src, len_dst ) \
do \
{ \
unsigned j, offset = mbedtls_platform_random_in_range( 256 ); \
for( i = 0; i < len_src; i++ ) \
{ \
j = (i + offset) % len_src; \
(dst)[j] = (src)[j] ^ (mask)[j]; \
} \
for( ; i < len_dst; i++ ) \
(dst)[i] ^= (mask)[i]; \
} while( 0 )
/*
* Encrypt or decrypt a partial block with CTR
* Warning: using b for temporary storage! src and dst must not be b!
* This avoids allocating one more 16 bytes buffer while allowing src == dst.
*/
#define CTR_CRYPT( dst, src, len ) \
#define CTR_CRYPT( dst, src, len ) \
do \
{ \
mbedtls_generate_permutation( perm_table, len ); \
mbedtls_generate_masks( mask_table, len ); \
if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctr, \
16, b, &olen ) ) != 0 ) \
{ \
return( ret ); \
} \
\
for( i = 0; i < (len); i++ ) \
(dst)[i] = (src)[i] ^ b[i]; \
for( i = 0; i < len; i++ ) \
{ \
(dst)[perm_table[i]] = (src)[perm_table[i]] ^ mask_table[perm_table[i]];\
(dst)[perm_table[i]] ^= b[perm_table[i]]; \
(dst)[perm_table[i]] ^= mask_table[perm_table[i]]; \
} \
} while( 0 )
/*
@ -164,6 +224,8 @@ static int ccm_auth_crypt( mbedtls_ccm_context *ctx, int mode, size_t length,
unsigned char b[16];
unsigned char y[16];
unsigned char ctr[16];
unsigned char perm_table[16];
unsigned char mask_table[16];
const unsigned char *src;
unsigned char *dst;
@ -184,6 +246,10 @@ static int ccm_auth_crypt( mbedtls_ccm_context *ctx, int mode, size_t length,
if( add_len > 0xFF00 )
return( MBEDTLS_ERR_CCM_BAD_INPUT );
mbedtls_platform_zeroize( b, 16 );
mbedtls_platform_zeroize( y, 16 );
mbedtls_platform_zeroize( ctr, 16 );
q = (uint_fast8_t) (16 - 1 - iv_len);
/*
@ -198,15 +264,16 @@ static int ccm_auth_crypt( mbedtls_ccm_context *ctx, int mode, size_t length,
* 5 .. 3 (t - 2) / 2
* 2 .. 0 q - 1
*/
b[0] = 0;
b[0] |= ( add_len > 0 ) << 6;
b[0] |= ( ( tag_len - 2 ) / 2 ) << 3;
b[0] |= q - 1;
mbedtls_platform_memcpy( b + 1, iv, iv_len );
mbedtls_generate_masks( mask_table, 16 );
mbedtls_generate_permutation( perm_table, 16 );
b[0] = (unsigned char) ( ( ( add_len > 0 ) << 6 ) |
( ( ( tag_len - 2 ) / 2 ) << 3 ) |
( q - 1 ) ) ^ mask_table[0];
for( i = 0; i < iv_len; i++ )
b[i+1] = iv[i] ^ mask_table[i+1];
for( i = 0, len_left = length; i < q; i++, len_left >>= 8 )
b[15-i] = (unsigned char)( len_left & 0xFF );
b[15-i] = (unsigned char)( ( len_left & 0xFF ) ) ^ mask_table[15-i];
if( len_left > 0 )
return( MBEDTLS_ERR_CCM_BAD_INPUT );
@ -226,12 +293,16 @@ static int ccm_auth_crypt( mbedtls_ccm_context *ctx, int mode, size_t length,
len_left = add_len;
src = add;
mbedtls_generate_masks( mask_table, 16 );
mbedtls_generate_permutation( perm_table, 16 );
mbedtls_platform_memset( b, 0, 16 );
b[0] = (unsigned char)( ( add_len >> 8 ) & 0xFF );
b[1] = (unsigned char)( ( add_len ) & 0xFF );
b[0] = (unsigned char)( ( ( add_len >> 8 ) & 0xFF ) ^ mask_table[0] );
b[1] = (unsigned char)( ( ( add_len ) & 0xFF ) ^ mask_table[1] );
use_len = len_left < 16 - 2 ? len_left : 16 - 2;
mbedtls_platform_memcpy( b + 2, src, use_len );
COPY_MASK( b+2, src, mask_table+2, use_len, 14 );
len_left -= use_len;
src += use_len;
@ -239,10 +310,12 @@ static int ccm_auth_crypt( mbedtls_ccm_context *ctx, int mode, size_t length,
while( len_left > 0 )
{
mbedtls_generate_masks( mask_table, 16 );
mbedtls_generate_permutation( perm_table, 16 );
use_len = len_left > 16 ? 16 : len_left;
mbedtls_platform_memset( b, 0, 16 );
mbedtls_platform_memcpy( b, src, use_len );
COPY_MASK( b, src, mask_table, use_len, 16);
UPDATE_CBC_MAC;
len_left -= use_len;
@ -281,8 +354,10 @@ static int ccm_auth_crypt( mbedtls_ccm_context *ctx, int mode, size_t length,
if( mode == CCM_ENCRYPT )
{
mbedtls_generate_masks( mask_table, 16 );
mbedtls_generate_permutation( perm_table, 16 );
mbedtls_platform_memset( b, 0, 16 );
mbedtls_platform_memcpy( b, src, use_len );
COPY_MASK( b, src, mask_table, use_len, 16 );
UPDATE_CBC_MAC;
}
@ -290,8 +365,10 @@ static int ccm_auth_crypt( mbedtls_ccm_context *ctx, int mode, size_t length,
if( mode == CCM_DECRYPT )
{
mbedtls_generate_masks( mask_table, 16 );
mbedtls_generate_permutation( perm_table, 16 );
mbedtls_platform_memset( b, 0, 16 );
mbedtls_platform_memcpy( b, dst, use_len );
COPY_MASK( b, dst, mask_table, use_len, 16 );
UPDATE_CBC_MAC;
}
@ -317,6 +394,10 @@ static int ccm_auth_crypt( mbedtls_ccm_context *ctx, int mode, size_t length,
CTR_CRYPT( y, y, 16 );
mbedtls_platform_memcpy( tag, y, tag_len );
mbedtls_platform_zeroize( b, 16 );
mbedtls_platform_zeroize( y, 16 );
mbedtls_platform_zeroize( ctr, 16 );
return( ret );
}

View file

@ -48,6 +48,12 @@
#include "mbedtls/entropy_poll.h"
#endif
#if defined(MBEDTLS_PLATFORM_FAULT_CALLBACKS)
#include "platform_fault.h"
#else
static void mbedtls_platform_fault(){}
#endif
#include <stddef.h>
#include <string.h>
@ -119,43 +125,45 @@ void *mbedtls_platform_zeroize( void *buf, size_t len )
void *mbedtls_platform_memset( void *ptr, int value, size_t num )
{
size_t i, start_offset;
size_t i, start_offset = 0;
volatile size_t flow_counter = 0;
volatile char *b = ptr;
char rnd_data;
start_offset = (size_t) mbedtls_platform_random_in_range( (uint32_t) num );
rnd_data = (char) mbedtls_platform_random_in_range( 256 );
/* Perform a memset operations with random data and start from a random
* location */
for( i = start_offset; i < num; ++i )
if( num > 0 )
{
b[i] = rnd_data;
flow_counter++;
}
start_offset = (size_t) mbedtls_platform_random_in_range( (uint32_t) num );
/* Start from a random location with target data */
for( i = start_offset; i < num; ++i )
{
b[i] = value;
flow_counter++;
}
rnd_data = (char) mbedtls_platform_random_in_range( 256 );
/* Second memset operation with random data */
for( i = 0; i < start_offset; ++i )
{
b[i] = rnd_data;
flow_counter++;
}
/* Perform a memset operations with random data and start from a random
* location */
for( i = start_offset; i < num; ++i )
{
b[i] = rnd_data;
flow_counter++;
}
/* Finish memset operation with correct data */
for( i = 0; i < start_offset; ++i )
{
b[i] = value;
flow_counter++;
}
/* Start from a random location with target data */
for( i = start_offset; i < num; ++i )
{
b[i] = value;
flow_counter++;
}
/* Second memset operation with random data */
for( i = 0; i < start_offset; ++i )
{
b[i] = rnd_data;
flow_counter++;
}
/* Finish memset operation with correct data */
for( i = 0; i < start_offset; ++i )
{
b[i] = value;
flow_counter++;
}
}
/* check the correct number of iterations */
if( flow_counter == 2 * num )
{
@ -165,6 +173,7 @@ void *mbedtls_platform_memset( void *ptr, int value, size_t num )
return ptr;
}
}
mbedtls_platform_fault();
return NULL;
}
@ -204,6 +213,7 @@ void *mbedtls_platform_memcpy( void *dst, const void *src, size_t num )
return dst;
}
}
mbedtls_platform_fault();
return NULL;
}
@ -245,22 +255,25 @@ int mbedtls_platform_memequal( const void *buf1, const void *buf2, size_t num )
/* Start from a random location and check the correct number of iterations */
size_t i, flow_counter = 0;
size_t start_offset = (size_t) mbedtls_platform_random_in_range( (uint32_t) num );
for( i = start_offset; i < num; i++ )
size_t start_offset = 0;
if( num > 0 )
{
unsigned char x = A[i], y = B[i];
flow_counter++;
diff |= x ^ y;
}
start_offset = (size_t) mbedtls_platform_random_in_range( (uint32_t) num );
for( i = 0; i < start_offset; i++ )
{
unsigned char x = A[i], y = B[i];
flow_counter++;
diff |= x ^ y;
}
for( i = start_offset; i < num; i++ )
{
unsigned char x = A[i], y = B[i];
flow_counter++;
diff |= x ^ y;
}
for( i = 0; i < start_offset; i++ )
{
unsigned char x = A[i], y = B[i];
flow_counter++;
diff |= x ^ y;
}
}
/* Return 0 only when diff is 0 and flow_counter is equal to num */
return( (int) diff | (int) ( flow_counter ^ num ) );
}
@ -340,18 +353,7 @@ void mbedtls_platform_random_buf( uint8_t *buf, size_t len )
uint32_t mbedtls_platform_random_in_range( uint32_t num )
{
uint32_t result;
if( num <= 1 )
{
result = 0;
}
else
{
result = mbedtls_platform_random_uint32() % num;
}
return( result );
return mbedtls_platform_random_uint32() % num;
}
void mbedtls_platform_random_delay( void )

View file

@ -933,8 +933,8 @@ int tls_prf_generic( mbedtls_md_type_t md_type,
{
size_t nb;
size_t i, j, k, md_len;
unsigned char tmp[128];
unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
unsigned char tmp[128] = {0};
unsigned char h_i[MBEDTLS_MD_MAX_SIZE] = {0};
mbedtls_md_handle_t md_info;
mbedtls_md_context_t md_ctx;
int ret;
@ -12709,6 +12709,7 @@ void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
#endif
mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
mbedtls_free( ssl->out_buf );
ssl->out_buf = NULL;
}
if( ssl->in_buf != NULL )
@ -12720,6 +12721,7 @@ void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
#endif
mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
mbedtls_free( ssl->in_buf );
ssl->in_buf = NULL;
}
#if defined(MBEDTLS_ZLIB_SUPPORT)

View file

@ -732,6 +732,9 @@ static const char *features[] = {
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
"MBEDTLS_MEMORY_BUFFER_ALLOC_C",
#endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */
#if defined(MBEDTLS_PLATFORM_FAULT_CALLBACKS)
"MBEDTLS_PLATFORM_FAULT_CALLBACKS",
#endif /* MBEDTLS_PLATFORM_FAULT_CALLBACKS */
#if defined(MBEDTLS_NET_C)
"MBEDTLS_NET_C",
#endif /* MBEDTLS_NET_C */

View file

@ -1994,6 +1994,14 @@ int query_config( const char *config )
}
#endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */
#if defined(MBEDTLS_PLATFORM_FAULT_CALLBACKS)
if( strcmp( "MBEDTLS_PLATFORM_FAULT_CALLBACKS", config ) == 0 )
{
MACRO_EXPANSION_TO_STR( MBEDTLS_PLATFORM_FAULT_CALLBACKS );
return( 0 );
}
#endif /* MBEDTLS_PLATFORM_FAULT_CALLBACKS */
#if defined(MBEDTLS_NET_C)
if( strcmp( "MBEDTLS_NET_C", config ) == 0 )
{

View file

@ -148,6 +148,7 @@ MBEDTLS_SSL_TRANSFORM_OPTIMIZE_CIPHERS
MBEDTLS_VALIDATE_SSL_KEYS_INTEGRITY
MBEDTLS_OPTIMIZE_TINYCRYPT_ASM
MBEDTLS_AES_128_BIT_MASKED
MBEDTLS_PLATFORM_FAULT_CALLBACKS
_ALT\s*$
);