2021-10-29 00:39:53 -04:00
|
|
|
/*
|
|
|
|
* AES block cipher, ESP hardware accelerated version, common
|
2020-11-12 02:11:38 -05:00
|
|
|
* Based on mbedTLS FIPS-197 compliant version.
|
|
|
|
*
|
2021-10-29 00:39:53 -04:00
|
|
|
* SPDX-FileCopyrightText: The Mbed TLS Contributors
|
2020-11-12 02:11:38 -05:00
|
|
|
*
|
2021-10-29 00:39:53 -04:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2020-11-12 02:11:38 -05:00
|
|
|
*
|
2023-10-20 02:02:35 -04:00
|
|
|
* SPDX-FileContributor: 2016-2023 Espressif Systems (Shanghai) CO LTD
|
2020-11-12 02:11:38 -05:00
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* The AES block cipher was designed by Vincent Rijmen and Joan Daemen.
|
|
|
|
*
|
|
|
|
* http://csrc.nist.gov/encryption/aes/rijndael/Rijndael.pdf
|
|
|
|
* http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf
|
|
|
|
*/
|
2023-10-20 02:02:35 -04:00
|
|
|
#include "sdkconfig.h"
|
2020-11-12 02:11:38 -05:00
|
|
|
#include "aes/esp_aes_internal.h"
|
|
|
|
#include "mbedtls/aes.h"
|
|
|
|
#include "hal/aes_hal.h"
|
|
|
|
#include "hal/aes_types.h"
|
|
|
|
#include "soc/soc_caps.h"
|
2021-05-28 09:13:32 -04:00
|
|
|
#include "mbedtls/error.h"
|
2020-11-12 02:11:38 -05:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2023-10-20 02:02:35 -04:00
|
|
|
#if SOC_AES_SUPPORT_DMA
|
2021-01-14 02:25:06 -05:00
|
|
|
#include "esp_aes_dma_priv.h"
|
|
|
|
#endif
|
|
|
|
|
2020-11-12 02:11:38 -05:00
|
|
|
bool valid_key_length(const esp_aes_context *ctx)
|
|
|
|
{
|
|
|
|
bool valid_len = (ctx->key_bytes == AES_128_KEY_BYTES) || (ctx->key_bytes == AES_256_KEY_BYTES);
|
|
|
|
|
|
|
|
#if SOC_AES_SUPPORT_AES_192
|
|
|
|
valid_len |= ctx->key_bytes == AES_192_KEY_BYTES;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return valid_len;
|
|
|
|
}
|
|
|
|
|
2023-10-20 02:02:35 -04:00
|
|
|
void esp_aes_init(esp_aes_context *ctx)
|
2020-11-12 02:11:38 -05:00
|
|
|
{
|
2023-10-20 02:02:35 -04:00
|
|
|
bzero(ctx, sizeof(esp_aes_context));
|
|
|
|
#if SOC_AES_SUPPORT_DMA && CONFIG_MBEDTLS_AES_USE_INTERRUPT
|
|
|
|
esp_aes_intr_alloc();
|
|
|
|
#endif
|
2020-11-12 02:11:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void esp_aes_free( esp_aes_context *ctx )
|
|
|
|
{
|
|
|
|
if ( ctx == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bzero( ctx, sizeof( esp_aes_context ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* AES key schedule (same for encryption or decryption, as hardware handles schedule)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
int esp_aes_setkey( esp_aes_context *ctx, const unsigned char *key,
|
|
|
|
unsigned int keybits )
|
|
|
|
{
|
|
|
|
#if !SOC_AES_SUPPORT_AES_192
|
|
|
|
if (keybits == 192) {
|
|
|
|
return MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (keybits != 128 && keybits != 192 && keybits != 256) {
|
|
|
|
return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
|
|
|
|
}
|
|
|
|
ctx->key_bytes = keybits / 8;
|
|
|
|
memcpy(ctx->key, key, ctx->key_bytes);
|
|
|
|
ctx->key_in_hardware = 0;
|
|
|
|
return 0;
|
|
|
|
}
|