2020-08-19 02:39:12 -04:00
|
|
|
/**
|
2020-11-12 02:11:38 -05:00
|
|
|
* \brief AES block cipher, ESP DMA hardware accelerated version
|
2020-08-19 02:39:12 -04:00
|
|
|
* Based on mbedTLS FIPS-197 compliant version.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
|
|
|
* Additions Copyright (C) 2016-2020, Espressif Systems (Shanghai) PTE Ltd
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include "mbedtls/aes.h"
|
|
|
|
#include "esp_log.h"
|
2020-11-12 02:11:38 -05:00
|
|
|
#include "esp_crypto_lock.h"
|
|
|
|
#include "hal/aes_hal.h"
|
2024-03-01 03:57:09 -05:00
|
|
|
#include "hal/aes_ll.h"
|
2023-10-12 05:21:04 -04:00
|
|
|
#include "esp_aes_internal.h"
|
2024-03-01 03:57:09 -05:00
|
|
|
#include "esp_private/esp_crypto_lock_internal.h"
|
2020-08-19 02:39:12 -04:00
|
|
|
|
2021-01-14 02:25:06 -05:00
|
|
|
#if SOC_AES_GDMA
|
2021-02-25 02:06:41 -05:00
|
|
|
#define AES_LOCK() esp_crypto_sha_aes_lock_acquire()
|
|
|
|
#define AES_RELEASE() esp_crypto_sha_aes_lock_release()
|
2020-11-12 02:11:38 -05:00
|
|
|
#elif SOC_AES_CRYPTO_DMA
|
|
|
|
#define AES_LOCK() esp_crypto_dma_lock_acquire()
|
|
|
|
#define AES_RELEASE() esp_crypto_dma_lock_release()
|
2024-03-01 03:57:09 -05:00
|
|
|
#include "hal/crypto_dma_ll.h"
|
2020-11-12 02:11:38 -05:00
|
|
|
#endif
|
2020-08-19 02:39:12 -04:00
|
|
|
|
|
|
|
static const char *TAG = "esp-aes";
|
|
|
|
|
2020-11-12 02:11:38 -05:00
|
|
|
void esp_aes_acquire_hardware( void )
|
2020-08-19 02:39:12 -04:00
|
|
|
{
|
2020-11-12 02:11:38 -05:00
|
|
|
/* Released by esp_aes_release_hardware()*/
|
|
|
|
AES_LOCK();
|
|
|
|
|
2024-03-01 03:57:09 -05:00
|
|
|
AES_RCC_ATOMIC() {
|
|
|
|
aes_ll_enable_bus_clock(true);
|
2020-11-12 02:11:38 -05:00
|
|
|
#if SOC_AES_CRYPTO_DMA
|
2024-03-01 03:57:09 -05:00
|
|
|
crypto_dma_ll_enable_bus_clock(true);
|
2020-11-12 02:11:38 -05:00
|
|
|
#endif
|
2024-03-01 03:57:09 -05:00
|
|
|
aes_ll_reset_register();
|
|
|
|
#if SOC_AES_CRYPTO_DMA
|
|
|
|
crypto_dma_ll_reset_register();
|
|
|
|
#endif
|
|
|
|
}
|
2020-08-19 02:39:12 -04:00
|
|
|
}
|
|
|
|
|
2020-11-12 02:11:38 -05:00
|
|
|
/* Function to disable AES and Crypto DMA clocks and release locks */
|
|
|
|
void esp_aes_release_hardware( void )
|
2020-08-19 02:39:12 -04:00
|
|
|
{
|
2024-03-01 03:57:09 -05:00
|
|
|
AES_RCC_ATOMIC() {
|
|
|
|
aes_ll_enable_bus_clock(false);
|
2020-11-12 02:11:38 -05:00
|
|
|
#if SOC_AES_CRYPTO_DMA
|
2024-03-01 03:57:09 -05:00
|
|
|
crypto_dma_ll_enable_bus_clock(false);
|
2020-11-12 02:11:38 -05:00
|
|
|
#endif
|
2024-03-01 03:57:09 -05:00
|
|
|
}
|
2020-08-19 02:39:12 -04:00
|
|
|
|
2020-11-12 02:11:38 -05:00
|
|
|
AES_RELEASE();
|
2020-08-19 02:39:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int esp_aes_validate_input(esp_aes_context *ctx, const unsigned char *input,
|
|
|
|
unsigned char *output )
|
|
|
|
{
|
|
|
|
if (!ctx) {
|
|
|
|
ESP_LOGE(TAG, "No AES context supplied");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!input) {
|
|
|
|
ESP_LOGE(TAG, "No input supplied");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!output) {
|
|
|
|
ESP_LOGE(TAG, "No output supplied");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* AES-ECB single block encryption
|
|
|
|
*/
|
2020-11-12 02:11:38 -05:00
|
|
|
int esp_internal_aes_encrypt(esp_aes_context *ctx,
|
|
|
|
const unsigned char input[16],
|
|
|
|
unsigned char output[16] )
|
2020-08-19 02:39:12 -04:00
|
|
|
{
|
2023-07-12 05:08:46 -04:00
|
|
|
int r = -1;
|
2020-08-19 02:39:12 -04:00
|
|
|
|
|
|
|
if (esp_aes_validate_input(ctx, input, output)) {
|
2023-01-10 06:00:53 -05:00
|
|
|
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
|
2020-08-19 02:39:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!valid_key_length(ctx)) {
|
|
|
|
return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_aes_acquire_hardware();
|
|
|
|
ctx->key_in_hardware = 0;
|
2020-11-12 02:11:38 -05:00
|
|
|
ctx->key_in_hardware = aes_hal_setkey(ctx->key, ctx->key_bytes, ESP_AES_ENCRYPT);
|
|
|
|
aes_hal_mode_init(ESP_AES_BLOCK_MODE_ECB);
|
2020-08-19 02:39:12 -04:00
|
|
|
r = esp_aes_process_dma(ctx, input, output, AES_BLOCK_BYTES, NULL);
|
|
|
|
esp_aes_release_hardware();
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2020-11-12 02:11:38 -05:00
|
|
|
void esp_aes_encrypt(esp_aes_context *ctx,
|
|
|
|
const unsigned char input[16],
|
|
|
|
unsigned char output[16] )
|
2020-08-19 02:39:12 -04:00
|
|
|
{
|
|
|
|
esp_internal_aes_encrypt(ctx, input, output);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* AES-ECB single block decryption
|
|
|
|
*/
|
2020-11-12 02:11:38 -05:00
|
|
|
int esp_internal_aes_decrypt(esp_aes_context *ctx,
|
|
|
|
const unsigned char input[16],
|
|
|
|
unsigned char output[16] )
|
2020-08-19 02:39:12 -04:00
|
|
|
{
|
2023-07-12 05:08:46 -04:00
|
|
|
int r = -1;
|
2020-08-19 02:39:12 -04:00
|
|
|
|
|
|
|
if (esp_aes_validate_input(ctx, input, output)) {
|
2023-01-10 06:00:53 -05:00
|
|
|
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
|
2020-08-19 02:39:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!valid_key_length(ctx)) {
|
|
|
|
return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_aes_acquire_hardware();
|
|
|
|
ctx->key_in_hardware = 0;
|
2020-11-12 02:11:38 -05:00
|
|
|
ctx->key_in_hardware = aes_hal_setkey(ctx->key, ctx->key_bytes, ESP_AES_DECRYPT);
|
|
|
|
aes_hal_mode_init(ESP_AES_BLOCK_MODE_ECB);
|
2020-08-19 02:39:12 -04:00
|
|
|
r = esp_aes_process_dma(ctx, input, output, AES_BLOCK_BYTES, NULL);
|
|
|
|
esp_aes_release_hardware();
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2020-11-12 02:11:38 -05:00
|
|
|
void esp_aes_decrypt(esp_aes_context *ctx,
|
|
|
|
const unsigned char input[16],
|
|
|
|
unsigned char output[16] )
|
2020-08-19 02:39:12 -04:00
|
|
|
{
|
|
|
|
esp_internal_aes_decrypt(ctx, input, output);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* AES-ECB block encryption/decryption
|
|
|
|
*/
|
2020-11-12 02:11:38 -05:00
|
|
|
int esp_aes_crypt_ecb(esp_aes_context *ctx,
|
|
|
|
int mode,
|
|
|
|
const unsigned char input[16],
|
|
|
|
unsigned char output[16] )
|
2020-08-19 02:39:12 -04:00
|
|
|
{
|
2023-07-12 05:08:46 -04:00
|
|
|
int r = -1;
|
2020-08-19 02:39:12 -04:00
|
|
|
|
|
|
|
if (esp_aes_validate_input(ctx, input, output)) {
|
2023-01-10 06:00:53 -05:00
|
|
|
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
|
2020-08-19 02:39:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!valid_key_length(ctx)) {
|
|
|
|
return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_aes_acquire_hardware();
|
|
|
|
ctx->key_in_hardware = 0;
|
2020-11-12 02:11:38 -05:00
|
|
|
ctx->key_in_hardware = aes_hal_setkey(ctx->key, ctx->key_bytes, mode);
|
|
|
|
aes_hal_mode_init(ESP_AES_BLOCK_MODE_ECB);
|
2020-08-19 02:39:12 -04:00
|
|
|
r = esp_aes_process_dma(ctx, input, output, AES_BLOCK_BYTES, NULL);
|
|
|
|
esp_aes_release_hardware();
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* AES-CBC buffer encryption/decryption
|
|
|
|
*/
|
2020-11-12 02:11:38 -05:00
|
|
|
int esp_aes_crypt_cbc(esp_aes_context *ctx,
|
|
|
|
int mode,
|
|
|
|
size_t length,
|
|
|
|
unsigned char iv[16],
|
|
|
|
const unsigned char *input,
|
|
|
|
unsigned char *output )
|
2020-08-19 02:39:12 -04:00
|
|
|
{
|
2023-07-12 05:08:46 -04:00
|
|
|
int r = -1;
|
2020-08-19 02:39:12 -04:00
|
|
|
if (esp_aes_validate_input(ctx, input, output)) {
|
2023-01-10 06:00:53 -05:00
|
|
|
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
|
2020-08-19 02:39:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!iv) {
|
|
|
|
ESP_LOGE(TAG, "No IV supplied");
|
2023-01-10 06:00:53 -05:00
|
|
|
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
|
2020-08-19 02:39:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* For CBC input length should be multiple of
|
|
|
|
* AES BLOCK BYTES
|
|
|
|
* */
|
|
|
|
if ( (length % AES_BLOCK_BYTES) || (length == 0) ) {
|
|
|
|
return ERR_ESP_AES_INVALID_INPUT_LENGTH;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!valid_key_length(ctx)) {
|
|
|
|
return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_aes_acquire_hardware();
|
|
|
|
ctx->key_in_hardware = 0;
|
2020-11-12 02:11:38 -05:00
|
|
|
ctx->key_in_hardware = aes_hal_setkey(ctx->key, ctx->key_bytes, mode);
|
|
|
|
aes_hal_mode_init(ESP_AES_BLOCK_MODE_CBC);
|
|
|
|
aes_hal_set_iv(iv);
|
2020-08-19 02:39:12 -04:00
|
|
|
|
|
|
|
r = esp_aes_process_dma(ctx, input, output, length, NULL);
|
|
|
|
if (r != 0) {
|
2023-07-12 05:08:46 -04:00
|
|
|
goto cleanup;
|
2020-08-19 02:39:12 -04:00
|
|
|
}
|
|
|
|
|
2020-11-12 02:11:38 -05:00
|
|
|
aes_hal_read_iv(iv);
|
2020-08-19 02:39:12 -04:00
|
|
|
|
2023-07-12 05:08:46 -04:00
|
|
|
cleanup:
|
|
|
|
esp_aes_release_hardware();
|
2020-08-19 02:39:12 -04:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* AES-CFB8 buffer encryption/decryption
|
|
|
|
*/
|
2020-11-12 02:11:38 -05:00
|
|
|
int esp_aes_crypt_cfb8(esp_aes_context *ctx,
|
|
|
|
int mode,
|
|
|
|
size_t length,
|
|
|
|
unsigned char iv[16],
|
|
|
|
const unsigned char *input,
|
|
|
|
unsigned char *output )
|
2020-08-19 02:39:12 -04:00
|
|
|
{
|
2023-07-12 05:08:46 -04:00
|
|
|
int r = -1;
|
2020-08-19 02:39:12 -04:00
|
|
|
unsigned char c;
|
|
|
|
unsigned char ov[17];
|
|
|
|
size_t block_bytes = length - (length % AES_BLOCK_BYTES);
|
|
|
|
|
|
|
|
if (esp_aes_validate_input(ctx, input, output)) {
|
2023-01-10 06:00:53 -05:00
|
|
|
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
|
2020-08-19 02:39:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!iv) {
|
|
|
|
ESP_LOGE(TAG, "No IV supplied");
|
2023-01-10 06:00:53 -05:00
|
|
|
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
|
2020-08-19 02:39:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!valid_key_length(ctx)) {
|
|
|
|
return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The DMA engine will only output correct IV if it runs
|
|
|
|
full blocks of input in CFB8 mode
|
|
|
|
*/
|
|
|
|
esp_aes_acquire_hardware();
|
|
|
|
|
|
|
|
if (block_bytes > 0) {
|
|
|
|
|
|
|
|
ctx->key_in_hardware = 0;
|
2020-11-12 02:11:38 -05:00
|
|
|
ctx->key_in_hardware = aes_hal_setkey(ctx->key, ctx->key_bytes, mode);
|
|
|
|
aes_hal_mode_init(ESP_AES_BLOCK_MODE_CFB8);
|
|
|
|
aes_hal_set_iv(iv);
|
2020-08-19 02:39:12 -04:00
|
|
|
r = esp_aes_process_dma(ctx, input, output, block_bytes, NULL);
|
|
|
|
if (r != 0) {
|
2023-07-12 05:08:46 -04:00
|
|
|
goto cleanup;
|
2020-08-19 02:39:12 -04:00
|
|
|
}
|
|
|
|
|
2023-07-12 04:37:43 -04:00
|
|
|
aes_hal_read_iv(iv);
|
|
|
|
|
2020-08-19 02:39:12 -04:00
|
|
|
length -= block_bytes;
|
|
|
|
input += block_bytes;
|
|
|
|
output += block_bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process remaining bytes block-at-a-time in ECB mode
|
|
|
|
if (length > 0) {
|
|
|
|
ctx->key_in_hardware = 0;
|
2020-11-12 02:11:38 -05:00
|
|
|
ctx->key_in_hardware = aes_hal_setkey(ctx->key, ctx->key_bytes, MBEDTLS_AES_ENCRYPT);
|
|
|
|
aes_hal_mode_init(ESP_AES_BLOCK_MODE_ECB);
|
2020-08-19 02:39:12 -04:00
|
|
|
|
|
|
|
while ( length-- ) {
|
|
|
|
memcpy( ov, iv, 16 );
|
|
|
|
|
|
|
|
r = esp_aes_process_dma(ctx, iv, iv, AES_BLOCK_BYTES, NULL);
|
|
|
|
if (r != 0) {
|
2023-07-12 05:08:46 -04:00
|
|
|
goto cleanup;
|
2020-08-19 02:39:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( mode == MBEDTLS_AES_DECRYPT ) {
|
|
|
|
ov[16] = *input;
|
|
|
|
}
|
|
|
|
|
|
|
|
c = *output++ = ( iv[0] ^ *input++ );
|
|
|
|
|
|
|
|
if ( mode == MBEDTLS_AES_ENCRYPT ) {
|
|
|
|
ov[16] = c;
|
|
|
|
}
|
|
|
|
memcpy( iv, ov + 1, 16 );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2023-07-12 05:08:46 -04:00
|
|
|
r = 0;
|
2020-08-19 02:39:12 -04:00
|
|
|
|
2023-07-12 05:08:46 -04:00
|
|
|
cleanup:
|
|
|
|
esp_aes_release_hardware();
|
2020-08-19 02:39:12 -04:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* AES-CFB128 buffer encryption/decryption
|
|
|
|
*/
|
2020-11-12 02:11:38 -05:00
|
|
|
int esp_aes_crypt_cfb128(esp_aes_context *ctx,
|
|
|
|
int mode,
|
|
|
|
size_t length,
|
|
|
|
size_t *iv_off,
|
|
|
|
unsigned char iv[16],
|
|
|
|
const unsigned char *input,
|
|
|
|
unsigned char *output )
|
2020-08-19 02:39:12 -04:00
|
|
|
|
|
|
|
{
|
|
|
|
uint8_t c;
|
|
|
|
size_t stream_bytes = 0;
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
if (esp_aes_validate_input(ctx, input, output)) {
|
2023-01-10 06:00:53 -05:00
|
|
|
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
|
2020-08-19 02:39:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!iv) {
|
|
|
|
ESP_LOGE(TAG, "No IV supplied");
|
2023-01-10 06:00:53 -05:00
|
|
|
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
|
2020-08-19 02:39:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!iv_off) {
|
|
|
|
ESP_LOGE(TAG, "No IV offset supplied");
|
2023-01-10 06:00:53 -05:00
|
|
|
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
|
2020-08-19 02:39:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!valid_key_length(ctx)) {
|
|
|
|
return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = *iv_off;
|
|
|
|
|
|
|
|
/* First process the *iv_off bytes
|
|
|
|
* which are pending from the previous call to this API
|
|
|
|
*/
|
|
|
|
while (n > 0 && length > 0) {
|
|
|
|
if (mode == MBEDTLS_AES_ENCRYPT) {
|
|
|
|
iv[n] = *output++ = *input++ ^ iv[n];
|
|
|
|
} else {
|
|
|
|
c = *input++;
|
|
|
|
*output++ = c ^ iv[n];
|
|
|
|
iv[n] = c;
|
|
|
|
}
|
|
|
|
n = (n + 1) % AES_BLOCK_BYTES;
|
|
|
|
length--;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (length > 0) {
|
|
|
|
stream_bytes = length % AES_BLOCK_BYTES;
|
|
|
|
esp_aes_acquire_hardware();
|
|
|
|
ctx->key_in_hardware = 0;
|
2020-11-12 02:11:38 -05:00
|
|
|
ctx->key_in_hardware = aes_hal_setkey(ctx->key, ctx->key_bytes, mode);
|
|
|
|
aes_hal_mode_init(ESP_AES_BLOCK_MODE_CFB128);
|
|
|
|
aes_hal_set_iv(iv);
|
2020-08-19 02:39:12 -04:00
|
|
|
|
2023-07-12 05:08:46 -04:00
|
|
|
int r = esp_aes_process_dma(ctx, input, output, length, iv);
|
2020-08-19 02:39:12 -04:00
|
|
|
if (r != 0) {
|
|
|
|
esp_aes_release_hardware();
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stream_bytes == 0) {
|
|
|
|
// if we didn't need the partial 'stream block' then the new IV is in the IV register
|
2020-11-12 02:11:38 -05:00
|
|
|
aes_hal_read_iv(iv);
|
2020-08-19 02:39:12 -04:00
|
|
|
} else {
|
|
|
|
// if we did process a final partial block the new IV is already processed via DMA (and has some bytes of output in it),
|
|
|
|
// In decrypt mode any partial bytes are output plaintext (iv ^ c) and need to be swapped back to ciphertext (as the next
|
|
|
|
// block uses ciphertext as its IV input)
|
|
|
|
//
|
|
|
|
// Note: It may be more efficient to not process the partial block via DMA in this case.
|
|
|
|
if (mode == MBEDTLS_AES_DECRYPT) {
|
|
|
|
memcpy(iv, input + length - stream_bytes, stream_bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
esp_aes_release_hardware();
|
|
|
|
}
|
|
|
|
|
|
|
|
*iv_off = n + stream_bytes;
|
2023-07-12 05:08:46 -04:00
|
|
|
return 0;
|
2020-08-19 02:39:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* AES-OFB (Output Feedback Mode) buffer encryption/decryption
|
|
|
|
*/
|
|
|
|
|
2020-11-12 02:11:38 -05:00
|
|
|
int esp_aes_crypt_ofb(esp_aes_context *ctx,
|
|
|
|
size_t length,
|
|
|
|
size_t *iv_off,
|
|
|
|
unsigned char iv[16],
|
|
|
|
const unsigned char *input,
|
|
|
|
unsigned char *output )
|
2020-08-19 02:39:12 -04:00
|
|
|
{
|
|
|
|
size_t n;
|
|
|
|
size_t stream_bytes = 0;
|
|
|
|
|
|
|
|
if (esp_aes_validate_input(ctx, input, output)) {
|
2023-01-10 06:00:53 -05:00
|
|
|
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
|
2020-08-19 02:39:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!iv) {
|
|
|
|
ESP_LOGE(TAG, "No IV supplied");
|
2023-01-10 06:00:53 -05:00
|
|
|
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
|
2020-08-19 02:39:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!iv_off) {
|
|
|
|
ESP_LOGE(TAG, "No IV offset supplied");
|
2023-01-10 06:00:53 -05:00
|
|
|
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
|
2020-08-19 02:39:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
n = *iv_off;
|
|
|
|
|
|
|
|
/* If there is an offset then use the output of the previous AES block
|
|
|
|
(the updated IV) to calculate the new output */
|
|
|
|
while (n > 0 && length > 0) {
|
|
|
|
*output++ = (*input++ ^ iv[n]);
|
|
|
|
n = (n + 1) & 0xF;
|
|
|
|
length--;
|
|
|
|
}
|
|
|
|
if (length > 0) {
|
|
|
|
stream_bytes = (length % AES_BLOCK_BYTES);
|
|
|
|
|
|
|
|
esp_aes_acquire_hardware();
|
|
|
|
ctx->key_in_hardware = 0;
|
2020-11-12 02:11:38 -05:00
|
|
|
ctx->key_in_hardware = aes_hal_setkey(ctx->key, ctx->key_bytes, ESP_AES_DECRYPT);
|
|
|
|
aes_hal_mode_init(ESP_AES_BLOCK_MODE_OFB);
|
|
|
|
aes_hal_set_iv(iv);
|
2020-08-19 02:39:12 -04:00
|
|
|
|
2023-07-12 05:08:46 -04:00
|
|
|
int r = esp_aes_process_dma(ctx, input, output, length, iv);
|
2020-08-19 02:39:12 -04:00
|
|
|
if (r != 0) {
|
|
|
|
esp_aes_release_hardware();
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2020-11-12 02:11:38 -05:00
|
|
|
aes_hal_read_iv(iv);
|
2020-08-19 02:39:12 -04:00
|
|
|
esp_aes_release_hardware();
|
|
|
|
}
|
|
|
|
|
|
|
|
*iv_off = n + stream_bytes;
|
|
|
|
|
2023-07-12 05:08:46 -04:00
|
|
|
return 0;
|
2020-08-19 02:39:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* AES-CTR buffer encryption/decryption
|
|
|
|
*/
|
2020-11-12 02:11:38 -05:00
|
|
|
int esp_aes_crypt_ctr(esp_aes_context *ctx,
|
|
|
|
size_t length,
|
|
|
|
size_t *nc_off,
|
|
|
|
unsigned char nonce_counter[16],
|
|
|
|
unsigned char stream_block[16],
|
|
|
|
const unsigned char *input,
|
|
|
|
unsigned char *output )
|
2020-08-19 02:39:12 -04:00
|
|
|
{
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
if (esp_aes_validate_input(ctx, input, output)) {
|
2023-01-10 06:00:53 -05:00
|
|
|
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
|
2020-08-19 02:39:12 -04:00
|
|
|
}
|
|
|
|
|
2023-01-10 05:54:52 -05:00
|
|
|
if (!stream_block) {
|
|
|
|
ESP_LOGE(TAG, "No stream supplied");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-08-19 02:39:12 -04:00
|
|
|
if (!nonce_counter) {
|
|
|
|
ESP_LOGE(TAG, "No nonce supplied");
|
2023-01-10 06:00:53 -05:00
|
|
|
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
|
2020-08-19 02:39:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!nc_off) {
|
|
|
|
ESP_LOGE(TAG, "No nonce offset supplied");
|
2023-01-10 06:00:53 -05:00
|
|
|
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
|
2020-08-19 02:39:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
n = *nc_off;
|
|
|
|
|
|
|
|
if (!valid_key_length(ctx)) {
|
|
|
|
return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process any unprocessed bytes left in stream block from
|
|
|
|
last operation */
|
|
|
|
while (n > 0 && length > 0) {
|
|
|
|
*output++ = (unsigned char)(*input++ ^ stream_block[n]);
|
|
|
|
n = (n + 1) & 0xF;
|
|
|
|
length--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length > 0) {
|
|
|
|
|
|
|
|
esp_aes_acquire_hardware();
|
|
|
|
ctx->key_in_hardware = 0;
|
2020-11-12 02:11:38 -05:00
|
|
|
ctx->key_in_hardware = aes_hal_setkey(ctx->key, ctx->key_bytes, ESP_AES_DECRYPT);
|
2020-08-19 02:39:12 -04:00
|
|
|
|
2020-11-12 02:11:38 -05:00
|
|
|
aes_hal_mode_init(ESP_AES_BLOCK_MODE_CTR);
|
|
|
|
aes_hal_set_iv(nonce_counter);
|
2020-08-19 02:39:12 -04:00
|
|
|
|
2023-07-12 05:08:46 -04:00
|
|
|
int r = esp_aes_process_dma(ctx, input, output, length, stream_block);
|
2020-08-19 02:39:12 -04:00
|
|
|
|
|
|
|
if (r != 0) {
|
|
|
|
esp_aes_release_hardware();
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2020-11-12 02:11:38 -05:00
|
|
|
aes_hal_read_iv(nonce_counter);
|
2020-08-19 02:39:12 -04:00
|
|
|
|
|
|
|
esp_aes_release_hardware();
|
|
|
|
|
|
|
|
}
|
|
|
|
*nc_off = n + (length % AES_BLOCK_BYTES);
|
|
|
|
|
2023-07-12 05:08:46 -04:00
|
|
|
return 0;
|
2020-08-19 02:39:12 -04:00
|
|
|
}
|