TARGET_STM mbedtls astyle

pull/7351/head
jeromecoutant 2018-06-27 15:00:04 +02:00
parent 78410e7032
commit 057142167b
11 changed files with 331 additions and 294 deletions

View File

@ -51,12 +51,14 @@ static int aes_set_key( mbedtls_aes_context *ctx, const unsigned char *key, unsi
ctx->hcryp_aes.Init.KeySize = CRYP_KEYSIZE_256B; ctx->hcryp_aes.Init.KeySize = CRYP_KEYSIZE_256B;
memcpy(ctx->aes_key, key, 32); memcpy(ctx->aes_key, key, 32);
break; break;
default : return( MBEDTLS_ERR_AES_INVALID_KEY_LENGTH ); default :
return (MBEDTLS_ERR_AES_INVALID_KEY_LENGTH);
} }
/* Deinitializes the CRYP peripheral */ /* Deinitializes the CRYP peripheral */
if (HAL_CRYP_DeInit(&ctx->hcryp_aes) == HAL_ERROR) if (HAL_CRYP_DeInit(&ctx->hcryp_aes) == HAL_ERROR) {
return (HAL_ERROR); return (HAL_ERROR);
}
ctx->hcryp_aes.Init.DataType = CRYP_DATATYPE_8B; ctx->hcryp_aes.Init.DataType = CRYP_DATATYPE_8B;
ctx->hcryp_aes.Instance = CRYP; ctx->hcryp_aes.Instance = CRYP;
@ -67,8 +69,9 @@ static int aes_set_key( mbedtls_aes_context *ctx, const unsigned char *key, unsi
#if defined (TARGET_STM32L486xG) || defined (TARGET_STM32L443xC) #if defined (TARGET_STM32L486xG) || defined (TARGET_STM32L443xC)
ctx->hcryp_aes.Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; ctx->hcryp_aes.Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
#endif #endif
if (HAL_CRYP_Init(&ctx->hcryp_aes) == HAL_ERROR) if (HAL_CRYP_Init(&ctx->hcryp_aes) == HAL_ERROR) {
return (HAL_ERROR); return (HAL_ERROR);
}
/* allow multi-instance of CRYP use: save context for CRYP HW module CR */ /* allow multi-instance of CRYP use: save context for CRYP HW module CR */
ctx->ctx_save_cr = ctx->hcryp_aes.Instance->CR; ctx->ctx_save_cr = ctx->hcryp_aes.Instance->CR;
@ -79,7 +82,10 @@ static int aes_set_key( mbedtls_aes_context *ctx, const unsigned char *key, unsi
/* Implementation that should never be optimized out by the compiler */ /* Implementation that should never be optimized out by the compiler */
static void mbedtls_zeroize(void *v, size_t n) static void mbedtls_zeroize(void *v, size_t n)
{ {
volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0; volatile unsigned char *p = (unsigned char *)v;
while (n--) {
*p++ = 0;
}
} }
@ -92,8 +98,9 @@ void mbedtls_aes_init( mbedtls_aes_context *ctx )
void mbedtls_aes_free(mbedtls_aes_context *ctx) void mbedtls_aes_free(mbedtls_aes_context *ctx)
{ {
if( ctx == NULL ) if (ctx == NULL) {
return; return;
}
/* Force the CRYP Periheral Clock Reset */ /* Force the CRYP Periheral Clock Reset */
__HAL_RCC_CRYP_FORCE_RESET(); __HAL_RCC_CRYP_FORCE_RESET();
@ -150,7 +157,8 @@ int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
#if defined(MBEDTLS_CIPHER_MODE_CBC) #if defined(MBEDTLS_CIPHER_MODE_CBC)
#if defined (TARGET_STM32L486xG) || defined (TARGET_STM32L443xC) #if defined (TARGET_STM32L486xG) || defined (TARGET_STM32L443xC)
static int st_cbc_restore_context(mbedtls_aes_context *ctx){ static int st_cbc_restore_context(mbedtls_aes_context *ctx)
{
uint32_t tickstart; uint32_t tickstart;
tickstart = HAL_GetTick(); tickstart = HAL_GetTick();
while ((ctx->hcryp_aes.Instance->SR & AES_SR_BUSY) != 0) { while ((ctx->hcryp_aes.Instance->SR & AES_SR_BUSY) != 0) {
@ -169,28 +177,34 @@ static int st_hal_cryp_cbc( mbedtls_aes_context *ctx, uint32_t opmode, size_t le
ctx->hcryp_aes.Init.pInitVect = &iv[0]; // used in process, not in the init ctx->hcryp_aes.Init.pInitVect = &iv[0]; // used in process, not in the init
/* At this moment only, we know we have CBC mode: Re-initialize AES /* At this moment only, we know we have CBC mode: Re-initialize AES
IP with proper parameters and apply key and IV for multi context usecase */ IP with proper parameters and apply key and IV for multi context usecase */
if (HAL_CRYP_DeInit(&ctx->hcryp_aes) != HAL_OK) if (HAL_CRYP_DeInit(&ctx->hcryp_aes) != HAL_OK) {
return ST_ERR_AES_BUSY; return ST_ERR_AES_BUSY;
}
ctx->hcryp_aes.Init.OperatingMode = opmode; ctx->hcryp_aes.Init.OperatingMode = opmode;
ctx->hcryp_aes.Init.ChainingMode = CRYP_CHAINMODE_AES_CBC; ctx->hcryp_aes.Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
ctx->hcryp_aes.Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE; ctx->hcryp_aes.Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
if (HAL_CRYP_Init(&ctx->hcryp_aes) != HAL_OK) if (HAL_CRYP_Init(&ctx->hcryp_aes) != HAL_OK) {
return ST_ERR_AES_BUSY; return ST_ERR_AES_BUSY;
}
if(HAL_CRYPEx_AES(&ctx->hcryp_aes, input, length, output, 10) != 0) if (HAL_CRYPEx_AES(&ctx->hcryp_aes, input, length, output, 10) != 0) {
return ST_ERR_AES_BUSY; return ST_ERR_AES_BUSY;
}
return 0; return 0;
} }
#else /* STM32F4 and STM32F7 */ #else /* STM32F4 and STM32F7 */
static int st_cbc_restore_context(mbedtls_aes_context *ctx){ static int st_cbc_restore_context(mbedtls_aes_context *ctx)
{
/* allow multi-instance of CRYP use: restore context for CRYP hw module */ /* allow multi-instance of CRYP use: restore context for CRYP hw module */
ctx->hcryp_aes.Instance->CR = ctx->ctx_save_cr; ctx->hcryp_aes.Instance->CR = ctx->ctx_save_cr;
/* Re-initialize AES processor with proper parameters /* Re-initialize AES processor with proper parameters
and (re-)apply key and IV for multi context usecases */ and (re-)apply key and IV for multi context usecases */
if (HAL_CRYP_DeInit(&ctx->hcryp_aes) != HAL_OK) if (HAL_CRYP_DeInit(&ctx->hcryp_aes) != HAL_OK) {
return ST_ERR_AES_BUSY; return ST_ERR_AES_BUSY;
if (HAL_CRYP_Init(&ctx->hcryp_aes) != HAL_OK) }
if (HAL_CRYP_Init(&ctx->hcryp_aes) != HAL_OK) {
return ST_ERR_AES_BUSY; return ST_ERR_AES_BUSY;
}
return 0; return 0;
} }
@ -205,17 +219,20 @@ int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
{ {
uint32_t tickstart; uint32_t tickstart;
uint32_t *iv_ptr = (uint32_t *)&iv[0]; uint32_t *iv_ptr = (uint32_t *)&iv[0];
if( length % 16 ) if (length % 16) {
return (MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH); return (MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH);
}
ctx->hcryp_aes.Init.pInitVect = &iv[0]; ctx->hcryp_aes.Init.pInitVect = &iv[0];
if (st_cbc_restore_context(ctx) != 0) if (st_cbc_restore_context(ctx) != 0) {
return (ST_ERR_AES_BUSY); return (ST_ERR_AES_BUSY);
}
#if defined (TARGET_STM32L486xG) || defined (TARGET_STM32L443xC) #if defined (TARGET_STM32L486xG) || defined (TARGET_STM32L443xC)
if (mode == MBEDTLS_AES_DECRYPT) { if (mode == MBEDTLS_AES_DECRYPT) {
if (st_hal_cryp_cbc(ctx, CRYP_ALGOMODE_KEYDERIVATION_DECRYPT, length, iv, (uint8_t *)input, (uint8_t *)output) != 0) if (st_hal_cryp_cbc(ctx, CRYP_ALGOMODE_KEYDERIVATION_DECRYPT, length, iv, (uint8_t *)input, (uint8_t *)output) != 0) {
return ST_ERR_AES_BUSY; return ST_ERR_AES_BUSY;
}
/* Save the internal IV vector for multi context purpose */ /* Save the internal IV vector for multi context purpose */
tickstart = HAL_GetTick(); tickstart = HAL_GetTick();
while ((ctx->hcryp_aes.Instance->SR & AES_SR_BUSY) != 0) { while ((ctx->hcryp_aes.Instance->SR & AES_SR_BUSY) != 0) {
@ -230,8 +247,9 @@ int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
*iv_ptr++ = ctx->hcryp_aes.Instance->IVR1; *iv_ptr++ = ctx->hcryp_aes.Instance->IVR1;
*iv_ptr++ = ctx->hcryp_aes.Instance->IVR0; *iv_ptr++ = ctx->hcryp_aes.Instance->IVR0;
} else { } else {
if (st_hal_cryp_cbc(ctx, CRYP_ALGOMODE_ENCRYPT, length, iv, (uint8_t *)input, (uint8_t *)output) != 0) if (st_hal_cryp_cbc(ctx, CRYP_ALGOMODE_ENCRYPT, length, iv, (uint8_t *)input, (uint8_t *)output) != 0) {
return ST_ERR_AES_BUSY; return ST_ERR_AES_BUSY;
}
memcpy(iv, output, 16); /* current output is the IV vector for the next call */ memcpy(iv, output, 16); /* current output is the IV vector for the next call */
ctx->ctx_save_cr = ctx->hcryp_aes.Instance->CR; ctx->ctx_save_cr = ctx->hcryp_aes.Instance->CR;
} }
@ -239,8 +257,9 @@ int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
#else #else
if (mode == MBEDTLS_AES_DECRYPT) { if (mode == MBEDTLS_AES_DECRYPT) {
if (HAL_CRYP_AESCBC_Decrypt(&ctx->hcryp_aes, (uint8_t *)input, length, (uint8_t *)output, 10) != HAL_OK) if (HAL_CRYP_AESCBC_Decrypt(&ctx->hcryp_aes, (uint8_t *)input, length, (uint8_t *)output, 10) != HAL_OK) {
return ST_ERR_AES_BUSY; return ST_ERR_AES_BUSY;
}
/* Save the internal IV vector for multi context purpose */ /* Save the internal IV vector for multi context purpose */
tickstart = HAL_GetTick(); tickstart = HAL_GetTick();
while ((ctx->hcryp_aes.Instance->SR & (CRYP_SR_IFEM | CRYP_SR_OFNE | CRYP_SR_BUSY)) != CRYP_SR_IFEM) { while ((ctx->hcryp_aes.Instance->SR & (CRYP_SR_IFEM | CRYP_SR_OFNE | CRYP_SR_BUSY)) != CRYP_SR_IFEM) {
@ -255,8 +274,9 @@ int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
*iv_ptr++ = ctx->hcryp_aes.Instance->IV1LR; *iv_ptr++ = ctx->hcryp_aes.Instance->IV1LR;
*iv_ptr++ = ctx->hcryp_aes.Instance->IV1RR; *iv_ptr++ = ctx->hcryp_aes.Instance->IV1RR;
} else { } else {
if (HAL_CRYP_AESCBC_Encrypt(&ctx->hcryp_aes, (uint8_t *)input, length, (uint8_t *)output, 10) != HAL_OK) if (HAL_CRYP_AESCBC_Encrypt(&ctx->hcryp_aes, (uint8_t *)input, length, (uint8_t *)output, 10) != HAL_OK) {
return ST_ERR_AES_BUSY; return ST_ERR_AES_BUSY;
}
memcpy(iv, output, 16); /* current output is the IV vector for the next call */ memcpy(iv, output, 16); /* current output is the IV vector for the next call */
ctx->ctx_save_cr = ctx->hcryp_aes.Instance->CR; ctx->ctx_save_cr = ctx->hcryp_aes.Instance->CR;
} }
@ -281,8 +301,9 @@ int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
if (mode == MBEDTLS_AES_DECRYPT) { if (mode == MBEDTLS_AES_DECRYPT) {
while (length--) { while (length--) {
if (n == 0) if (n == 0)
if (mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv ) != 0) if (mbedtls_aes_crypt_ecb(ctx, MBEDTLS_AES_ENCRYPT, iv, iv) != 0) {
return ST_ERR_AES_BUSY; return ST_ERR_AES_BUSY;
}
c = *input++; c = *input++;
*output++ = (unsigned char)(c ^ iv[n]); *output++ = (unsigned char)(c ^ iv[n]);
@ -293,8 +314,9 @@ int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
} else { } else {
while (length--) { while (length--) {
if (n == 0) if (n == 0)
if (mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv ) != 0) if (mbedtls_aes_crypt_ecb(ctx, MBEDTLS_AES_ENCRYPT, iv, iv) != 0) {
return ST_ERR_AES_BUSY; return ST_ERR_AES_BUSY;
}
iv[n] = *output++ = (unsigned char)(iv[n] ^ *input++); iv[n] = *output++ = (unsigned char)(iv[n] ^ *input++);
@ -320,16 +342,19 @@ int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
while (length--) { while (length--) {
memcpy(ov, iv, 16); memcpy(ov, iv, 16);
if (mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv ) != 0) if (mbedtls_aes_crypt_ecb(ctx, MBEDTLS_AES_ENCRYPT, iv, iv) != 0) {
return ST_ERR_AES_BUSY; return ST_ERR_AES_BUSY;
}
if( mode == MBEDTLS_AES_DECRYPT ) if (mode == MBEDTLS_AES_DECRYPT) {
ov[16] = *input; ov[16] = *input;
}
c = *output++ = (unsigned char)(iv[0] ^ *input++); c = *output++ = (unsigned char)(iv[0] ^ *input++);
if( mode == MBEDTLS_AES_ENCRYPT ) if (mode == MBEDTLS_AES_ENCRYPT) {
ov[16] = c; ov[16] = c;
}
memcpy(iv, ov + 1, 16); memcpy(iv, ov + 1, 16);
} }
@ -351,16 +376,17 @@ int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
int c, i; int c, i;
size_t n = *nc_off; size_t n = *nc_off;
while( length-- ) while (length--) {
{
if (n == 0) { if (n == 0) {
if (mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, nonce_counter, stream_block ) != 0) if (mbedtls_aes_crypt_ecb(ctx, MBEDTLS_AES_ENCRYPT, nonce_counter, stream_block) != 0) {
return ST_ERR_AES_BUSY; return ST_ERR_AES_BUSY;
}
for (i = 16; i > 0; i--) for (i = 16; i > 0; i--)
if( ++nonce_counter[i - 1] != 0 ) if (++nonce_counter[i - 1] != 0) {
break; break;
} }
}
c = *input++; c = *input++;
*output++ = (unsigned char)(c ^ stream_block[n]); *output++ = (unsigned char)(c ^ stream_block[n]);

View File

@ -41,8 +41,7 @@ extern "C" {
* - to simplify key expansion in the 256-bit case by * - to simplify key expansion in the 256-bit case by
* generating an extra round key * generating an extra round key
*/ */
typedef struct typedef struct {
{
unsigned char aes_key[32]; /* Decryption key */ unsigned char aes_key[32]; /* Decryption key */
CRYP_HandleTypeDef hcryp_aes; CRYP_HandleTypeDef hcryp_aes;
uint32_t ctx_save_cr; /* save context for multi-instance */ uint32_t ctx_save_cr; /* save context for multi-instance */

View File

@ -24,8 +24,12 @@
#include "mbedtls/platform.h" #include "mbedtls/platform.h"
/* Implementation that should never be optimized out by the compiler */ /* Implementation that should never be optimized out by the compiler */
static void mbedtls_zeroize( void *v, size_t n ) { static void mbedtls_zeroize(void *v, size_t n)
volatile unsigned char *p = v; while( n-- ) *p++ = 0; {
volatile unsigned char *p = v;
while (n--) {
*p++ = 0;
}
} }
static int st_md5_restore_hw_context(mbedtls_md5_context *ctx) static int st_md5_restore_hw_context(mbedtls_md5_context *ctx)
@ -79,8 +83,9 @@ void mbedtls_md5_init( mbedtls_md5_context *ctx )
void mbedtls_md5_free(mbedtls_md5_context *ctx) void mbedtls_md5_free(mbedtls_md5_context *ctx)
{ {
if( ctx == NULL ) if (ctx == NULL) {
return; return;
}
mbedtls_zeroize(ctx, sizeof(mbedtls_md5_context)); mbedtls_zeroize(ctx, sizeof(mbedtls_md5_context));
} }

View File

@ -41,8 +41,7 @@ extern "C" {
* ST_MD5_BLOCK_SIZE bytes per ST_MD5_BLOCK_SIZE bytes * ST_MD5_BLOCK_SIZE bytes per ST_MD5_BLOCK_SIZE bytes
* If MD5_finish is called and sbuf_len>0, the remaining bytes are accumulated prior to the call to HAL_HASH_MD5_Finish * If MD5_finish is called and sbuf_len>0, the remaining bytes are accumulated prior to the call to HAL_HASH_MD5_Finish
*/ */
typedef struct typedef struct {
{
HASH_HandleTypeDef hhash_md5;/*!< ST HAL HASH struct */ HASH_HandleTypeDef hhash_md5;/*!< ST HAL HASH struct */
unsigned char sbuf[ST_MD5_BLOCK_SIZE]; /*!< MBEDTLS_MD5_BLOCK_SIZE buffer to store values so that algorithm is caled once the buffer is filled */ unsigned char sbuf[ST_MD5_BLOCK_SIZE]; /*!< MBEDTLS_MD5_BLOCK_SIZE buffer to store values so that algorithm is caled once the buffer is filled */
unsigned char sbuf_len; /*!< number of bytes to be processed in sbuf */ unsigned char sbuf_len; /*!< number of bytes to be processed in sbuf */

View File

@ -22,8 +22,12 @@
#include "mbedtls/platform.h" #include "mbedtls/platform.h"
/* Implementation that should never be optimized out by the compiler */ /* Implementation that should never be optimized out by the compiler */
static void mbedtls_zeroize( void *v, size_t n ) { static void mbedtls_zeroize(void *v, size_t n)
volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0; {
volatile unsigned char *p = (unsigned char *)v;
while (n--) {
*p++ = 0;
}
} }
static int st_sha1_restore_hw_context(mbedtls_sha1_context *ctx) static int st_sha1_restore_hw_context(mbedtls_sha1_context *ctx)
@ -77,8 +81,9 @@ void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
void mbedtls_sha1_free(mbedtls_sha1_context *ctx) void mbedtls_sha1_free(mbedtls_sha1_context *ctx)
{ {
if( ctx == NULL ) if (ctx == NULL) {
return; return;
}
mbedtls_zeroize(ctx, sizeof(mbedtls_sha1_context)); mbedtls_zeroize(ctx, sizeof(mbedtls_sha1_context));
} }

View File

@ -41,8 +41,7 @@ extern "C" {
* multiples of ST_SHA1_BLOCK_SIZE bytes * multiples of ST_SHA1_BLOCK_SIZE bytes
* If SHA1_finish is called and sbuf_len>0, the remaining bytes are accumulated prior to the call to HAL_HASH_SHA1_Finish * If SHA1_finish is called and sbuf_len>0, the remaining bytes are accumulated prior to the call to HAL_HASH_SHA1_Finish
*/ */
typedef struct typedef struct {
{
unsigned char sbuf[ST_SHA1_BLOCK_SIZE]; /*!< MBEDTLS_SHA1_BLOCK_SIZE buffer to store values so that algorithm is caled once the buffer is filled */ unsigned char sbuf[ST_SHA1_BLOCK_SIZE]; /*!< MBEDTLS_SHA1_BLOCK_SIZE buffer to store values so that algorithm is caled once the buffer is filled */
unsigned char sbuf_len; /*!< number of bytes remaining in sbuf to be processed */ unsigned char sbuf_len; /*!< number of bytes remaining in sbuf to be processed */
HASH_HandleTypeDef hhash_sha1; /*!< ST HAL HASH struct */ HASH_HandleTypeDef hhash_sha1; /*!< ST HAL HASH struct */

View File

@ -23,8 +23,12 @@
#include "mbedtls/platform.h" #include "mbedtls/platform.h"
/* Implementation that should never be optimized out by the compiler */ /* Implementation that should never be optimized out by the compiler */
static void mbedtls_zeroize( void *v, size_t n ) { static void mbedtls_zeroize(void *v, size_t n)
volatile unsigned char *p = v; while( n-- ) *p++ = 0; {
volatile unsigned char *p = v;
while (n--) {
*p++ = 0;
}
} }
static int st_sha256_restore_hw_context(mbedtls_sha256_context *ctx) static int st_sha256_restore_hw_context(mbedtls_sha256_context *ctx)
@ -77,8 +81,9 @@ void mbedtls_sha256_init( mbedtls_sha256_context *ctx )
void mbedtls_sha256_free(mbedtls_sha256_context *ctx) void mbedtls_sha256_free(mbedtls_sha256_context *ctx)
{ {
if( ctx == NULL ) if (ctx == NULL) {
return; return;
}
mbedtls_zeroize(ctx, sizeof(mbedtls_sha256_context)); mbedtls_zeroize(ctx, sizeof(mbedtls_sha256_context));
} }

View File

@ -40,8 +40,7 @@ extern "C" {
* ST_SHA256_BLOCK_SIZE bytes per ST_SHA256_BLOCK_SIZE bytes * ST_SHA256_BLOCK_SIZE bytes per ST_SHA256_BLOCK_SIZE bytes
* If sha256_finish is called and sbuf_len>0, the remaining bytes are accumulated prior to the call to HAL_HASH_SHA256_Finish * If sha256_finish is called and sbuf_len>0, the remaining bytes are accumulated prior to the call to HAL_HASH_SHA256_Finish
*/ */
typedef struct typedef struct {
{
int is224; /*!< 0 => SHA-256, else SHA-224 */ int is224; /*!< 0 => SHA-256, else SHA-224 */
HASH_HandleTypeDef hhash_sha256; HASH_HandleTypeDef hhash_sha256;
unsigned char sbuf[ST_SHA256_BLOCK_SIZE]; /*!< ST_SHA256_BLOCK_SIZE buffer to store values so that algorithm is called once the buffer is filled */ unsigned char sbuf[ST_SHA256_BLOCK_SIZE]; /*!< ST_SHA256_BLOCK_SIZE buffer to store values so that algorithm is called once the buffer is filled */