|
1 | 1 | /*
|
2 |
| - * Hardware aes collector for the STM32F4 family |
| 2 | + * Hardware aes collector for the STM32F4 F7 and L4 family |
3 | 3 | *******************************************************************************
|
4 | 4 | * Copyright (c) 2017, STMicroelectronics
|
5 | 5 | * SPDX-License-Identifier: Apache-2.0
|
@@ -130,15 +130,17 @@ int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
|
130 | 130 | /* allow multi-instance of CRYP use: restore context for CRYP hw module */
|
131 | 131 | ctx->hcryp_aes.Instance->CR = ctx->ctx_save_cr;
|
132 | 132 | 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; |
133 | 135 |
|
134 | 136 | 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 | + } |
138 | 140 | } 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 | + } |
142 | 144 | }
|
143 | 145 | /* allow multi-instance of CRYP use: save context for CRYP HW module CR */
|
144 | 146 | ctx->ctx_save_cr = ctx->hcryp_aes.Instance->CR;
|
@@ -243,12 +245,12 @@ int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
|
243 | 245 | unsigned char *output )
|
244 | 246 | {
|
245 | 247 | int status = 0;
|
246 |
| - uint32_t *iv_ptr = (uint32_t *)&iv[0]; |
247 | 248 | if( length % 16 )
|
248 | 249 | return( MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
|
249 | 250 | ctx->hcryp_aes.Init.pInitVect = &iv[0];
|
250 | 251 | status |= st_cbc_restore_context(ctx);
|
251 | 252 | #if defined (TARGET_STM32L486xG)
|
| 253 | + uint32_t *iv_ptr = (uint32_t *)&iv[0]; |
252 | 254 | if( mode == MBEDTLS_AES_DECRYPT ) {
|
253 | 255 | status |= st_hal_cryp_cbc(ctx, CRYP_ALGOMODE_KEYDERIVATION_DECRYPT, length, iv, (uint8_t *)input, (uint8_t *)output);
|
254 | 256 | // update IV
|
@@ -387,24 +389,41 @@ int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
|
387 | 389 | }
|
388 | 390 | #endif /* MBEDTLS_CIPHER_MODE_CTR */
|
389 | 391 |
|
390 |
| -void mbedtls_aes_encrypt( mbedtls_aes_context *ctx, |
| 392 | +int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx, |
391 | 393 | const unsigned char input[16],
|
392 | 394 | unsigned char output[16] )
|
393 | 395 | {
|
394 | 396 | 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; |
396 | 399 | }
|
| 400 | + return 0; |
397 | 401 |
|
398 | 402 | }
|
399 | 403 |
|
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] ) |
403 | 407 | {
|
404 | 408 | 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; |
406 | 411 | }
|
| 412 | + return 0; |
407 | 413 | }
|
408 | 414 |
|
| 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 | +} |
409 | 428 |
|
410 | 429 | #endif /*MBEDTLS_AES_ALT*/
|
0 commit comments