Skip to content

Commit cd1a18f

Browse files
committed
Use new interface of mbedtls instead of deprecated functions
1 parent c1fcae6 commit cd1a18f

File tree

2 files changed

+81
-21
lines changed

2 files changed

+81
-21
lines changed

features/mbedtls/targets/TARGET_STM/aes_alt.c

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Hardware aes collector for the STM32F4 family
2+
* Hardware aes collector for the STM32F4 F7 and L4 family
33
*******************************************************************************
44
* Copyright (c) 2017, STMicroelectronics
55
* SPDX-License-Identifier: Apache-2.0
@@ -130,15 +130,17 @@ int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
130130
/* allow multi-instance of CRYP use: restore context for CRYP hw module */
131131
ctx->hcryp_aes.Instance->CR = ctx->ctx_save_cr;
132132
ctx->hcryp_aes.Phase = HAL_CRYP_PHASE_READY;
133+
ctx->hcryp_aes.Init.DataType = CRYP_DATATYPE_8B;
134+
ctx->hcryp_aes.Init.pKey = ctx->aes_key;
133135

134136
if(mode == MBEDTLS_AES_DECRYPT) { /* AES decryption */
135-
ctx->hcryp_aes.Init.DataType = CRYP_DATATYPE_8B;
136-
ctx->hcryp_aes.Init.pKey = ctx->aes_key;
137-
mbedtls_aes_decrypt( ctx, input, output );
137+
if (mbedtls_internal_aes_decrypt( ctx, input, output )){
138+
return 1;
139+
}
138140
} else { /* AES encryption */
139-
ctx->hcryp_aes.Init.DataType = CRYP_DATATYPE_8B;
140-
ctx->hcryp_aes.Init.pKey = ctx->aes_key;
141-
mbedtls_aes_encrypt( ctx, input, output );
141+
if (mbedtls_internal_aes_encrypt( ctx, input, output )) {
142+
return 1;
143+
}
142144
}
143145
/* allow multi-instance of CRYP use: save context for CRYP HW module CR */
144146
ctx->ctx_save_cr = ctx->hcryp_aes.Instance->CR;
@@ -243,12 +245,12 @@ int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
243245
unsigned char *output )
244246
{
245247
int status = 0;
246-
uint32_t *iv_ptr = (uint32_t *)&iv[0];
247248
if( length % 16 )
248249
return( MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
249250
ctx->hcryp_aes.Init.pInitVect = &iv[0];
250251
status |= st_cbc_restore_context(ctx);
251252
#if defined (TARGET_STM32L486xG)
253+
uint32_t *iv_ptr = (uint32_t *)&iv[0];
252254
if( mode == MBEDTLS_AES_DECRYPT ) {
253255
status |= st_hal_cryp_cbc(ctx, CRYP_ALGOMODE_KEYDERIVATION_DECRYPT, length, iv, (uint8_t *)input, (uint8_t *)output);
254256
// update IV
@@ -387,24 +389,41 @@ int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
387389
}
388390
#endif /* MBEDTLS_CIPHER_MODE_CTR */
389391

390-
void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
392+
int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
391393
const unsigned char input[16],
392394
unsigned char output[16] )
393395
{
394396
if (HAL_CRYP_AESECB_Encrypt(&ctx->hcryp_aes, (uint8_t *)input, 16, (uint8_t *)output, 10) !=0) {
395-
// error found to be returned
397+
// error found
398+
return 1;
396399
}
400+
return 0;
397401

398402
}
399403

400-
void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
401-
const unsigned char input[16],
402-
unsigned char output[16] )
404+
int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
405+
const unsigned char input[16],
406+
unsigned char output[16] )
403407
{
404408
if(HAL_CRYP_AESECB_Decrypt(&ctx->hcryp_aes, (uint8_t *)input, 16, (uint8_t *)output, 10)) {
405-
// error found to be returned
409+
// error found
410+
return 1;
406411
}
412+
return 0;
407413
}
408414

415+
void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
416+
const unsigned char input[16],
417+
unsigned char output[16] )
418+
{
419+
mbedtls_internal_aes_encrypt( ctx, input, output );
420+
}
421+
422+
void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
423+
const unsigned char input[16],
424+
unsigned char output[16] )
425+
{
426+
mbedtls_internal_aes_decrypt( ctx, input, output );
427+
}
409428

410429
#endif /*MBEDTLS_AES_ALT*/

features/mbedtls/targets/TARGET_STM/aes_alt.h

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* aes_alt.h AES block cipher
33
*******************************************************************************
4-
* Copyright (c) 2016, STMicroelectronics
4+
* Copyright (c) 2017, STMicroelectronics
55
* SPDX-License-Identifier: Apache-2.0
66
*
77
* Licensed under the Apache License, Version 2.0 (the "License"); you may
@@ -239,10 +239,12 @@ int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
239239
* \param ctx AES context
240240
* \param input Plaintext block
241241
* \param output Output (ciphertext) block
242+
*
243+
* \return 0 if successful
242244
*/
243-
void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
244-
const unsigned char input[16],
245-
unsigned char output[16] );
245+
int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
246+
const unsigned char input[16],
247+
unsigned char output[16] );
246248

247249
/**
248250
* \brief Internal AES block decryption function
@@ -252,10 +254,49 @@ void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
252254
* \param ctx AES context
253255
* \param input Ciphertext block
254256
* \param output Output (plaintext) block
257+
*
258+
* \return 0 if successful
259+
*/
260+
int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
261+
const unsigned char input[16],
262+
unsigned char output[16] );
263+
264+
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
265+
#if defined(MBEDTLS_DEPRECATED_WARNING)
266+
#define MBEDTLS_DEPRECATED __attribute__((deprecated))
267+
#else
268+
#define MBEDTLS_DEPRECATED
269+
#endif
270+
/**
271+
* \brief Deprecated internal AES block encryption function
272+
* without return value.
273+
*
274+
* \deprecated Superseded by mbedtls_aes_encrypt_ext() in 2.5.0
275+
*
276+
* \param ctx AES context
277+
* \param input Plaintext block
278+
* \param output Output (ciphertext) block
279+
*/
280+
MBEDTLS_DEPRECATED void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
281+
const unsigned char input[16],
282+
unsigned char output[16] );
283+
284+
/**
285+
* \brief Deprecated internal AES block decryption function
286+
* without return value.
287+
*
288+
* \deprecated Superseded by mbedtls_aes_decrypt_ext() in 2.5.0
289+
*
290+
* \param ctx AES context
291+
* \param input Ciphertext block
292+
* \param output Output (plaintext) block
255293
*/
256-
void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
257-
const unsigned char input[16],
258-
unsigned char output[16] );
294+
MBEDTLS_DEPRECATED void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
295+
const unsigned char input[16],
296+
unsigned char output[16] );
297+
298+
#undef MBEDTLS_DEPRECATED
299+
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
259300

260301
#ifdef __cplusplus
261302
}

0 commit comments

Comments
 (0)