mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-02 12:14:32 +02:00
hwcrypto: Use DPORT-safe accesses with AES
This commit is contained in:
committed by
Angus Gratton
parent
99c663a6e9
commit
e256fb6d96
@@ -37,23 +37,33 @@ void esp_aes_acquire_hardware( void )
|
|||||||
{
|
{
|
||||||
/* newlib locks lazy initialize on ESP-IDF */
|
/* newlib locks lazy initialize on ESP-IDF */
|
||||||
_lock_acquire(&aes_lock);
|
_lock_acquire(&aes_lock);
|
||||||
/* Enable AES hardware */
|
|
||||||
DPORT_REG_SET_BIT(DPORT_PERI_CLK_EN_REG, DPORT_PERI_EN_AES);
|
DPORT_STALL_OTHER_CPU_START();
|
||||||
/* Clear reset on digital signature & secure boot units,
|
{
|
||||||
otherwise AES unit is held in reset also. */
|
/* Enable AES hardware */
|
||||||
DPORT_REG_CLR_BIT(DPORT_PERI_RST_EN_REG,
|
_DPORT_REG_SET_BIT(DPORT_PERI_CLK_EN_REG, DPORT_PERI_EN_AES);
|
||||||
DPORT_PERI_EN_AES
|
/* Clear reset on digital signature & secure boot units,
|
||||||
| DPORT_PERI_EN_DIGITAL_SIGNATURE
|
otherwise AES unit is held in reset also. */
|
||||||
| DPORT_PERI_EN_SECUREBOOT);
|
_DPORT_REG_CLR_BIT(DPORT_PERI_RST_EN_REG,
|
||||||
|
DPORT_PERI_EN_AES
|
||||||
|
| DPORT_PERI_EN_DIGITAL_SIGNATURE
|
||||||
|
| DPORT_PERI_EN_SECUREBOOT);
|
||||||
|
}
|
||||||
|
DPORT_STALL_OTHER_CPU_END();
|
||||||
}
|
}
|
||||||
|
|
||||||
void esp_aes_release_hardware( void )
|
void esp_aes_release_hardware( void )
|
||||||
{
|
{
|
||||||
/* Disable AES hardware */
|
DPORT_STALL_OTHER_CPU_START();
|
||||||
DPORT_REG_SET_BIT(DPORT_PERI_RST_EN_REG, DPORT_PERI_EN_AES);
|
{
|
||||||
/* Don't return other units to reset, as this pulls
|
/* Disable AES hardware */
|
||||||
reset on RSA & SHA units, respectively. */
|
_DPORT_REG_SET_BIT(DPORT_PERI_RST_EN_REG, DPORT_PERI_EN_AES);
|
||||||
DPORT_REG_CLR_BIT(DPORT_PERI_CLK_EN_REG, DPORT_PERI_EN_AES);
|
/* Don't return other units to reset, as this pulls
|
||||||
|
reset on RSA & SHA units, respectively. */
|
||||||
|
_DPORT_REG_CLR_BIT(DPORT_PERI_CLK_EN_REG, DPORT_PERI_EN_AES);
|
||||||
|
}
|
||||||
|
DPORT_STALL_OTHER_CPU_END();
|
||||||
|
|
||||||
_lock_release(&aes_lock);
|
_lock_release(&aes_lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,14 +141,25 @@ int esp_aes_setkey_dec( esp_aes_context *ctx, const unsigned char *key,
|
|||||||
*/
|
*/
|
||||||
static inline int esp_aes_setkey_hardware( esp_aes_context *ctx, int mode)
|
static inline int esp_aes_setkey_hardware( esp_aes_context *ctx, int mode)
|
||||||
{
|
{
|
||||||
if ( mode == ESP_AES_ENCRYPT ) {
|
DPORT_ACCESS_BLOCK() {
|
||||||
ets_aes_setkey_enc(ctx->enc.key, ctx->enc.aesbits);
|
// ROM AES functions access DPORT, so need to be protected as such
|
||||||
} else {
|
if ( mode == ESP_AES_ENCRYPT ) {
|
||||||
ets_aes_setkey_dec(ctx->dec.key, ctx->dec.aesbits);
|
ets_aes_setkey_enc(ctx->enc.key, ctx->enc.aesbits);
|
||||||
|
} else {
|
||||||
|
ets_aes_setkey_dec(ctx->dec.key, ctx->dec.aesbits);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline void esp_aes_block(const uint8_t input[16], uint8_t output[16])
|
||||||
|
{
|
||||||
|
DPORT_ACCESS_BLOCK() {
|
||||||
|
// ROM AES functions access DPORT, so need to be protected as such
|
||||||
|
ets_aes_crypt(input, output);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* AES-ECB block encryption
|
* AES-ECB block encryption
|
||||||
*/
|
*/
|
||||||
@@ -148,7 +169,7 @@ void esp_aes_encrypt( esp_aes_context *ctx,
|
|||||||
{
|
{
|
||||||
esp_aes_acquire_hardware();
|
esp_aes_acquire_hardware();
|
||||||
esp_aes_setkey_hardware(ctx, ESP_AES_ENCRYPT);
|
esp_aes_setkey_hardware(ctx, ESP_AES_ENCRYPT);
|
||||||
ets_aes_crypt(input, output);
|
esp_aes_block(input, output);
|
||||||
esp_aes_release_hardware();
|
esp_aes_release_hardware();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,7 +183,7 @@ void esp_aes_decrypt( esp_aes_context *ctx,
|
|||||||
{
|
{
|
||||||
esp_aes_acquire_hardware();
|
esp_aes_acquire_hardware();
|
||||||
esp_aes_setkey_hardware(ctx, ESP_AES_DECRYPT);
|
esp_aes_setkey_hardware(ctx, ESP_AES_DECRYPT);
|
||||||
ets_aes_crypt(input, output);
|
esp_aes_block(input, output);
|
||||||
esp_aes_release_hardware();
|
esp_aes_release_hardware();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -177,7 +198,7 @@ int esp_aes_crypt_ecb( esp_aes_context *ctx,
|
|||||||
{
|
{
|
||||||
esp_aes_acquire_hardware();
|
esp_aes_acquire_hardware();
|
||||||
esp_aes_setkey_hardware(ctx, mode);
|
esp_aes_setkey_hardware(ctx, mode);
|
||||||
ets_aes_crypt(input, output);
|
esp_aes_block(input, output);
|
||||||
esp_aes_release_hardware();
|
esp_aes_release_hardware();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -206,7 +227,7 @@ int esp_aes_crypt_cbc( esp_aes_context *ctx,
|
|||||||
if ( mode == ESP_AES_DECRYPT ) {
|
if ( mode == ESP_AES_DECRYPT ) {
|
||||||
while ( length > 0 ) {
|
while ( length > 0 ) {
|
||||||
memcpy( temp, input, 16 );
|
memcpy( temp, input, 16 );
|
||||||
ets_aes_crypt(input, output);
|
esp_aes_block(input, output);
|
||||||
|
|
||||||
for ( i = 0; i < 16; i++ ) {
|
for ( i = 0; i < 16; i++ ) {
|
||||||
output[i] = (unsigned char)( output[i] ^ iv[i] );
|
output[i] = (unsigned char)( output[i] ^ iv[i] );
|
||||||
@@ -218,13 +239,13 @@ int esp_aes_crypt_cbc( esp_aes_context *ctx,
|
|||||||
output += 16;
|
output += 16;
|
||||||
length -= 16;
|
length -= 16;
|
||||||
}
|
}
|
||||||
} else {
|
} else { // ESP_AES_ENCRYPT
|
||||||
while ( length > 0 ) {
|
while ( length > 0 ) {
|
||||||
for ( i = 0; i < 16; i++ ) {
|
for ( i = 0; i < 16; i++ ) {
|
||||||
output[i] = (unsigned char)( input[i] ^ iv[i] );
|
output[i] = (unsigned char)( input[i] ^ iv[i] );
|
||||||
}
|
}
|
||||||
|
|
||||||
ets_aes_crypt(output, output);
|
esp_aes_block(output, output);
|
||||||
memcpy( iv, output, 16 );
|
memcpy( iv, output, 16 );
|
||||||
|
|
||||||
input += 16;
|
input += 16;
|
||||||
@@ -258,7 +279,7 @@ int esp_aes_crypt_cfb128( esp_aes_context *ctx,
|
|||||||
if ( mode == ESP_AES_DECRYPT ) {
|
if ( mode == ESP_AES_DECRYPT ) {
|
||||||
while ( length-- ) {
|
while ( length-- ) {
|
||||||
if ( n == 0 ) {
|
if ( n == 0 ) {
|
||||||
ets_aes_crypt(iv, iv );
|
esp_aes_block(iv, iv );
|
||||||
}
|
}
|
||||||
|
|
||||||
c = *input++;
|
c = *input++;
|
||||||
@@ -270,7 +291,7 @@ int esp_aes_crypt_cfb128( esp_aes_context *ctx,
|
|||||||
} else {
|
} else {
|
||||||
while ( length-- ) {
|
while ( length-- ) {
|
||||||
if ( n == 0 ) {
|
if ( n == 0 ) {
|
||||||
ets_aes_crypt(iv, iv );
|
esp_aes_block(iv, iv );
|
||||||
}
|
}
|
||||||
|
|
||||||
iv[n] = *output++ = (unsigned char)( iv[n] ^ *input++ );
|
iv[n] = *output++ = (unsigned char)( iv[n] ^ *input++ );
|
||||||
@@ -304,7 +325,7 @@ int esp_aes_crypt_cfb8( esp_aes_context *ctx,
|
|||||||
|
|
||||||
while ( length-- ) {
|
while ( length-- ) {
|
||||||
memcpy( ov, iv, 16 );
|
memcpy( ov, iv, 16 );
|
||||||
ets_aes_crypt(iv, iv);
|
esp_aes_block(iv, iv);
|
||||||
|
|
||||||
if ( mode == ESP_AES_DECRYPT ) {
|
if ( mode == ESP_AES_DECRYPT ) {
|
||||||
ov[16] = *input;
|
ov[16] = *input;
|
||||||
@@ -343,7 +364,7 @@ int esp_aes_crypt_ctr( esp_aes_context *ctx,
|
|||||||
|
|
||||||
while ( length-- ) {
|
while ( length-- ) {
|
||||||
if ( n == 0 ) {
|
if ( n == 0 ) {
|
||||||
ets_aes_crypt(nonce_counter, stream_block);
|
esp_aes_block(nonce_counter, stream_block);
|
||||||
|
|
||||||
for ( i = 16; i > 0; i-- )
|
for ( i = 16; i > 0; i-- )
|
||||||
if ( ++nonce_counter[i - 1] != 0 ) {
|
if ( ++nonce_counter[i - 1] != 0 ) {
|
||||||
|
Reference in New Issue
Block a user