2023-08-28 06:14:47 -04:00
|
|
|
/*
|
2024-03-01 03:57:09 -05:00
|
|
|
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
|
2023-08-28 06:14:47 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: CC0-1.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2024-03-01 03:57:09 -05:00
|
|
|
#include "esp_private/esp_crypto_lock_internal.h"
|
2023-08-28 06:14:47 -04:00
|
|
|
#include "hal/aes_types.h"
|
|
|
|
#include "hal/aes_hal.h"
|
2024-03-01 03:57:09 -05:00
|
|
|
#include "hal/aes_ll.h"
|
2023-08-28 06:14:47 -04:00
|
|
|
|
|
|
|
#if SOC_AES_SUPPORTED
|
|
|
|
|
|
|
|
#include "aes_block.h"
|
|
|
|
|
|
|
|
void aes_crypt_cbc_block(int mode,
|
|
|
|
uint8_t key_bytes,
|
|
|
|
const uint8_t key[32],
|
|
|
|
size_t length,
|
|
|
|
unsigned char iv[16],
|
|
|
|
const unsigned char *input,
|
|
|
|
unsigned char *output)
|
|
|
|
{
|
|
|
|
uint32_t *output_words = (uint32_t *)output;
|
|
|
|
const uint32_t *input_words = (const uint32_t *)input;
|
|
|
|
uint32_t *iv_words = (uint32_t *)iv;
|
|
|
|
unsigned char temp[16];
|
|
|
|
|
2024-03-01 03:57:09 -05:00
|
|
|
AES_RCC_ATOMIC() {
|
|
|
|
aes_ll_enable_bus_clock(true);
|
|
|
|
aes_ll_reset_register();
|
|
|
|
}
|
2023-08-28 06:14:47 -04:00
|
|
|
|
|
|
|
/* Sets the key used for AES encryption/decryption */
|
|
|
|
aes_hal_setkey(key, key_bytes, mode);
|
|
|
|
|
|
|
|
if (mode == ESP_AES_DECRYPT) {
|
|
|
|
while ( length > 0 ) {
|
|
|
|
memcpy(temp, input_words, 16);
|
|
|
|
aes_hal_transform_block(input_words, output_words);
|
|
|
|
|
|
|
|
output_words[0] = output_words[0] ^ iv_words[0];
|
|
|
|
output_words[1] = output_words[1] ^ iv_words[1];
|
|
|
|
output_words[2] = output_words[2] ^ iv_words[2];
|
|
|
|
output_words[3] = output_words[3] ^ iv_words[3];
|
|
|
|
|
|
|
|
memcpy( iv_words, temp, 16 );
|
|
|
|
|
|
|
|
input_words += 4;
|
|
|
|
output_words += 4;
|
|
|
|
length -= 16;
|
|
|
|
}
|
|
|
|
} else { // ESP_AES_ENCRYPT
|
|
|
|
while ( length > 0 ) {
|
|
|
|
|
|
|
|
output_words[0] = input_words[0] ^ iv_words[0];
|
|
|
|
output_words[1] = input_words[1] ^ iv_words[1];
|
|
|
|
output_words[2] = input_words[2] ^ iv_words[2];
|
|
|
|
output_words[3] = input_words[3] ^ iv_words[3];
|
|
|
|
|
|
|
|
aes_hal_transform_block(output_words, output_words);
|
|
|
|
memcpy( iv_words, output_words, 16 );
|
|
|
|
|
|
|
|
input_words += 4;
|
|
|
|
output_words += 4;
|
|
|
|
length -= 16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-01 03:57:09 -05:00
|
|
|
AES_RCC_ATOMIC() {
|
|
|
|
aes_ll_enable_bus_clock(false);
|
|
|
|
}
|
2023-08-28 06:14:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void aes_crypt_ctr_block(uint8_t key_bytes,
|
|
|
|
const uint8_t key[32],
|
|
|
|
size_t length,
|
|
|
|
size_t *nc_off,
|
|
|
|
unsigned char nonce_counter[16],
|
|
|
|
unsigned char stream_block[16],
|
|
|
|
const unsigned char *input,
|
|
|
|
unsigned char *output )
|
|
|
|
{
|
|
|
|
int c, i;
|
|
|
|
size_t n = *nc_off;
|
|
|
|
|
2024-03-01 03:57:09 -05:00
|
|
|
AES_RCC_ATOMIC() {
|
|
|
|
aes_ll_enable_bus_clock(true);
|
|
|
|
aes_ll_reset_register();
|
|
|
|
}
|
2023-08-28 06:14:47 -04:00
|
|
|
|
|
|
|
/* Sets the key used for AES encryption/decryption */
|
|
|
|
aes_hal_setkey(key, key_bytes, ESP_AES_ENCRYPT);
|
|
|
|
|
|
|
|
while (length--) {
|
|
|
|
if ( n == 0 ) {
|
|
|
|
aes_hal_transform_block(nonce_counter, stream_block);
|
|
|
|
for ( i = 16; i > 0; i-- ) {
|
|
|
|
if ( ++nonce_counter[i - 1] != 0 ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c = *input++;
|
|
|
|
*output++ = (unsigned char)( c ^ stream_block[n] );
|
|
|
|
n = ( n + 1 ) & 0x0F;
|
|
|
|
}
|
|
|
|
|
|
|
|
*nc_off = n;
|
|
|
|
|
2024-03-01 03:57:09 -05:00
|
|
|
AES_RCC_ATOMIC() {
|
|
|
|
aes_ll_enable_bus_clock(false);
|
|
|
|
}
|
2023-08-28 06:14:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|