Applied @yanesca and @andresag01 comments (#4825)

pull/4825/head
stcoorem 2017-10-19 12:00:09 +02:00
parent e07ab46f32
commit 2e2fb6ff26
5 changed files with 115 additions and 92 deletions

View File

@ -19,13 +19,11 @@
*/
#include "mbedtls/aes.h"
#if defined(MBEDTLS_AES_C)
#if defined(MBEDTLS_AES_ALT)
#include "em_device.h"
#if defined(AES_PRESENT) && (AES_COUNT == 1)
#if defined(MBEDTLS_AES_C)
#if defined(MBEDTLS_AES_ALT)
#include "em_aes.h"
#include "em_cmu.h"
#include "em_bus.h"
@ -143,51 +141,21 @@ int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx,
/*
* AES-ECB block encryption
*/
void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
const unsigned char input[16],
unsigned char output[16] )
int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
const unsigned char input[16],
unsigned char output[16] )
{
switch ( ctx->keybits )
{
case 128:
aes_lock();
AES_ECB128( output, input, 16, ctx->key, true );
aes_unlock();
break;
case 256:
aes_lock();
AES_ECB256( output, input, 16, ctx->key, true );
aes_unlock();
break;
default:
// Error
break;
}
return mbedtls_aes_crypt_ecb(ctx, MBEDTLS_AES_ENCRYPT, input, output);
}
/*
* AES-ECB block decryption
*/
void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
const unsigned char input[16],
unsigned char output[16] )
int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
const unsigned char input[16],
unsigned char output[16] )
{
switch ( ctx->keybits )
{
case 128:
aes_lock();
AES_ECB128( output, input, 16, ctx->key, false );
aes_unlock();
break;
case 256:
aes_lock();
AES_ECB256( output, input, 16, ctx->key, false );
aes_unlock();
break;
default:
// Error
break;
}
return mbedtls_aes_crypt_ecb(ctx, MBEDTLS_AES_DECRYPT, input, output);
}
/*
@ -299,9 +267,9 @@ int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
const unsigned char *input,
unsigned char *output )
{
size_t n = iv_off ? *iv_off : 0;
size_t n = ( iv_off != NULL ) ? *iv_off : 0;
if ( n || ( length & 0xf ) )
if ( ( n > 0 ) || ( length & 0xf ) )
{
// IV offset or length not aligned to block size
int c;
@ -410,7 +378,7 @@ int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
/*
* AES-CTR Nonce update function
*/
void aes_ctr_update_nonce( uint8_t *nonce_counter )
static void aes_ctr_update_nonce( uint8_t *nonce_counter )
{
for( size_t i = 16; i > 0; i-- )
if( ++nonce_counter[i - 1] != 0 )
@ -428,9 +396,9 @@ int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
const unsigned char *input,
unsigned char *output )
{
size_t n = nc_off ? *nc_off : 0;
size_t n = ( nc_off != NULL ) ? *nc_off : 0;
if ( n || ( length & 0xf ) || ctx->keybits == 192 )
if ( ( n > 0 ) || ( length & 0xf ) )
{
// IV offset or length not aligned to block size
int c, i;
@ -493,6 +461,6 @@ int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
}
#endif /* MBEDTLS_CIPHER_MODE_CTR */
#endif /* AES_PRESENT && (AES_COUNT == 1) */
#endif /* MBEDTLS_AES_ALT */
#endif /* MBEDTLS_AES_C */
#endif /* AES_PRESENT && (AES_COUNT == 1) */

View File

@ -237,10 +237,12 @@ int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
* \param ctx AES context
* \param input Plaintext block
* \param output Output (ciphertext) block
*
* \return 0 if successful
*/
void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
const unsigned char input[16],
unsigned char output[16] );
int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
const unsigned char input[16],
unsigned char output[16] );
/**
* \brief Internal AES block decryption function
@ -250,10 +252,59 @@ void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
* \param ctx AES context
* \param input Ciphertext block
* \param output Output (plaintext) block
*
* \return 0 if successful
*/
void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
const unsigned char input[16],
unsigned char output[16] );
int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
const unsigned char input[16],
unsigned char output[16] );
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
#if defined(MBEDTLS_DEPRECATED_WARNING)
#define MBEDTLS_DEPRECATED __attribute__((deprecated))
#else
#define MBEDTLS_DEPRECATED
#endif
/**
* \brief Internal AES block encryption function
* (Only exposed to allow overriding it,
* see MBEDTLS_AES_ENCRYPT_ALT)
*
* \deprecated Superseded by mbedtls_aes_encrypt_ext() in 2.5.0
*
* \param ctx AES context
* \param input Plaintext block
* \param output Output (ciphertext) block
*/
MBEDTLS_DEPRECATED static inline void mbedtls_aes_encrypt(
mbedtls_aes_context *ctx,
const unsigned char input[16],
unsigned char output[16] )
{
mbedtls_internal_aes_encrypt( ctx, input, output );
}
/**
* \brief Internal AES block decryption function
* (Only exposed to allow overriding it,
* see MBEDTLS_AES_DECRYPT_ALT)
*
* \deprecated Superseded by mbedtls_aes_decrypt_ext() in 2.5.0
*
* \param ctx AES context
* \param input Ciphertext block
* \param output Output (plaintext) block
*/
MBEDTLS_DEPRECATED static inline void mbedtls_aes_decrypt(
mbedtls_aes_context *ctx,
const unsigned char input[16],
unsigned char output[16] )
{
mbedtls_internal_aes_decrypt( ctx, input, output );
}
#undef MBEDTLS_DEPRECATED
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
#ifdef __cplusplus
}

View File

@ -31,13 +31,11 @@
*/
#include "mbedtls/aes.h"
#if defined(MBEDTLS_AES_C)
#if defined(MBEDTLS_AES_ALT)
#include "em_device.h"
#if defined(CRYPTO_PRESENT)
#if defined(MBEDTLS_AES_C)
#if defined(MBEDTLS_AES_ALT)
#include "crypto_management.h"
#include "em_crypto.h"
@ -45,7 +43,7 @@
#include <string.h>
__STATIC_INLINE void CRYPTO_DataReadUnaligned(volatile uint32_t * reg,
const uint8_t * val)
uint8_t * const val)
{
/* Check data is 32bit aligned, if not, read into temporary buffer and
then move to user buffer. */
@ -53,16 +51,16 @@ __STATIC_INLINE void CRYPTO_DataReadUnaligned(volatile uint32_t * reg,
{
uint32_t temp[4];
CRYPTO_DataRead(reg, temp);
memcpy((void*)val, temp, 16);
memcpy(val, temp, 16);
}
else
{
CRYPTO_DataRead(reg, (uint32_t*)val);
CRYPTO_DataRead(reg, (uint32_t* const)val);
}
}
__STATIC_INLINE void CRYPTO_DataWriteUnaligned(volatile uint32_t * reg,
const uint8_t * val)
uint8_t * const val)
{
/* Check data is 32bit aligned, if not move to temporary buffer before
writing.*/
@ -74,7 +72,7 @@ __STATIC_INLINE void CRYPTO_DataWriteUnaligned(volatile uint32_t * reg,
}
else
{
CRYPTO_DataWrite(reg, (uint32_t*)val);
CRYPTO_DataWrite(reg, (uint32_t* const)val);
}
}
@ -113,8 +111,6 @@ int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx,
return( MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
}
memset( ctx, 0, sizeof( mbedtls_aes_context ) );
if ( ( 128UL != keybits ) && ( 256UL != keybits ) ) {
/* Unsupported key size */
return( MBEDTLS_ERR_AES_INVALID_KEY_LENGTH );
@ -139,8 +135,6 @@ int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx,
return ( MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
}
memset( ctx, 0, sizeof( mbedtls_aes_context ) );
if ( ( 128UL != keybits ) && ( 256UL != keybits ) ) {
/* Unsupported key size */
return( MBEDTLS_ERR_AES_INVALID_KEY_LENGTH );
@ -173,6 +167,26 @@ int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx,
* functions with in-place implemented functions, to get much shorter
* critical sections */
/*
* AES-ECB block encryption
*/
int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
const unsigned char input[16],
unsigned char output[16] )
{
return mbedtls_aes_crypt_ecb(ctx, MBEDTLS_AES_ENCRYPT, input, output);
}
/*
* AES-ECB block decryption
*/
int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
const unsigned char input[16],
unsigned char output[16] )
{
return mbedtls_aes_crypt_ecb(ctx, MBEDTLS_AES_DECRYPT, input, output);
}
/*
* AES-ECB block encryption/decryption
*/
@ -538,8 +552,6 @@ int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
}
#endif /* MBEDTLS_CIPHER_MODE_CTR */
#endif /* CRYPTO_PRESENT */
#endif /* MBEDTLS_AES_ALT */
#endif /* MBEDTLS_AES_C */
#endif /* CRYPTO_PRESENT */

View File

@ -48,14 +48,16 @@
#include MBEDTLS_CONFIG_FILE
#endif
#if defined( MBEDTLS_ECP_C )
#if defined( MBEDTLS_ECP_INTERNAL_ALT )
#include "em_device.h"
#if defined( CRYPTO_PRESENT )
#if defined( MBEDTLS_ECP_C )
#if defined( MBEDTLS_ECP_INTERNAL_ALT )
#include "mbedtls/ecp.h"
#include "mbedtls/ecp_internal.h"
#include "mbedtls/platform.h"
#include "em_crypto.h"
#include "em_core.h"
#include "crypto_management.h"
@ -63,14 +65,6 @@
#include <stdbool.h>
#include <string.h>
#if defined(MBEDTLS_PLATFORM_C)
#include "mbedtls/platform.h"
#else
#include <stdlib.h>
#define mbedtls_calloc calloc
#define mbedtls_free free
#endif
/** ECC big integer type. */
#define ECC_BIGINT_SIZE_IN_BITS (256)
#define ECC_BIGINT_SIZE_IN_BYTES (ECC_BIGINT_SIZE_IN_BITS/8)
@ -394,7 +388,7 @@ static void mbedtls_mpi_div_mod(CRYPTO_TypeDef *crypto,
* @brief
* Enable CRYPTO by setting up control registers for given ecc curve.
******************************************************************************/
int mbedtls_ecp_device_init( CRYPTO_TypeDef *device, const mbedtls_ecp_group *grp)
static int crypto_device_init( CRYPTO_TypeDef *device, const mbedtls_ecp_group *grp)
{
int ret = 0;
@ -641,8 +635,6 @@ void mbedtls_internal_ecp_free( const mbedtls_ecp_group *grp )
(void) grp;
}
#if defined(ECP_SHORTWEIERSTRASS)
#if defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT)
/**
* \brief Randomize jacobian coordinates:
@ -686,7 +678,7 @@ int mbedtls_internal_ecp_randomize_jac( const mbedtls_ecp_group *grp,
}
crypto = crypto_management_acquire();
mbedtls_ecp_device_init(crypto, grp);
crypto_device_init(crypto, grp);
CORE_ENTER_CRITICAL();
CRYPTO_DDataWrite(&crypto->DDATA1, l);
@ -781,7 +773,7 @@ int mbedtls_internal_ecp_add_mixed( const mbedtls_ecp_group *grp,
CORE_DECLARE_IRQ_STATE;
CRYPTO_TypeDef *crypto = crypto_management_acquire();
mbedtls_ecp_device_init(crypto, grp);
crypto_device_init(crypto, grp);
/*
STEP 1:
@ -1138,7 +1130,7 @@ int mbedtls_internal_ecp_double_jac( const mbedtls_ecp_group *grp,
CORE_DECLARE_IRQ_STATE;
CRYPTO_TypeDef *crypto = crypto_management_acquire();
mbedtls_ecp_device_init(crypto, grp);
crypto_device_init(crypto, grp);
ecc_bigint_t _2YY;
/*
@ -1491,7 +1483,7 @@ int mbedtls_internal_ecp_normalize_jac_many( const mbedtls_ecp_group *grp,
MPI_TO_BIGINT( cc[0], &T[0]->Z );
CRYPTO_TypeDef *crypto = crypto_management_acquire();
mbedtls_ecp_device_init(crypto, grp);
crypto_device_init(crypto, grp);
for( i = 1; i < t_len; i++ )
{
@ -1633,7 +1625,7 @@ int mbedtls_internal_ecp_normalize_jac( const mbedtls_ecp_group *grp,
CORE_DECLARE_IRQ_STATE;
CRYPTO_TypeDef *crypto = crypto_management_acquire();
mbedtls_ecp_device_init(crypto, grp);
crypto_device_init(crypto, grp);
ecc_bigint_t one;
ecc_bigint_t Z;
@ -1723,10 +1715,8 @@ int mbedtls_internal_ecp_normalize_jac( const mbedtls_ecp_group *grp,
}
#endif
#endif /* ECP_SHORTWEIERSTRASS */
#endif /* #if defined( CRYPTO_PRESENT ) */
#endif /* #if defined( MBEDTLS_ECP_INTERNAL_ALT ) */
#endif /* #if defined( MBEDTLS_ECP_C ) */
#endif /* #if defined( CRYPTO_PRESENT ) */

View File

@ -34,6 +34,8 @@
#if defined(MBEDTLS_SHA1_ALT)
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif