2016-09-05 20:38:12 -04:00
|
|
|
/**
|
2020-11-12 02:11:38 -05:00
|
|
|
* \brief AES block cipher, ESP block hardware accelerated version
|
2016-09-05 20:38:12 -04:00
|
|
|
* Based on mbedTLS FIPS-197 compliant version.
|
2016-08-05 05:40:32 -04:00
|
|
|
*
|
|
|
|
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
2017-08-15 18:58:33 -04:00
|
|
|
* Additions Copyright (C) 2016-2017, Espressif Systems (Shanghai) PTE Ltd
|
2016-08-05 05:40:32 -04:00
|
|
|
* 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>
|
2017-08-15 18:58:33 -04:00
|
|
|
#include "mbedtls/aes.h"
|
2019-05-17 15:54:40 -04:00
|
|
|
#include "mbedtls/platform_util.h"
|
2020-11-12 02:11:38 -05:00
|
|
|
#include "aes/esp_aes.h"
|
2019-05-13 06:02:45 -04:00
|
|
|
#include "soc/hwcrypto_periph.h"
|
2016-09-02 04:36:26 -04:00
|
|
|
#include <sys/lock.h>
|
2020-11-12 02:11:38 -05:00
|
|
|
#include "hal/aes_hal.h"
|
|
|
|
#include "aes/esp_aes_internal.h"
|
2016-08-05 05:40:32 -04:00
|
|
|
|
2017-08-15 19:06:52 -04:00
|
|
|
#include <freertos/FreeRTOS.h>
|
|
|
|
|
|
|
|
#include "soc/cpu.h"
|
|
|
|
#include <stdio.h>
|
2018-10-29 11:55:02 -04:00
|
|
|
#include "driver/periph_ctrl.h"
|
2017-08-15 19:06:52 -04:00
|
|
|
|
|
|
|
|
|
|
|
/* AES uses a spinlock mux not a lock as the underlying block operation
|
|
|
|
only takes 208 cycles (to write key & compute block), +600 cycles
|
|
|
|
for DPORT protection but +3400 cycles again if you use a full sized lock.
|
|
|
|
|
|
|
|
For CBC, CFB, etc. this may mean that interrupts are disabled for a longer
|
|
|
|
period of time for bigger lengths. However at the moment this has to happen
|
|
|
|
anyway due to DPORT protection...
|
|
|
|
*/
|
|
|
|
static portMUX_TYPE aes_spinlock = portMUX_INITIALIZER_UNLOCKED;
|
2016-08-05 05:40:32 -04:00
|
|
|
|
2019-05-21 04:12:42 -04:00
|
|
|
|
2016-09-02 04:36:26 -04:00
|
|
|
void esp_aes_acquire_hardware( void )
|
2016-08-05 05:40:32 -04:00
|
|
|
{
|
2017-08-15 19:06:52 -04:00
|
|
|
portENTER_CRITICAL(&aes_spinlock);
|
2017-08-15 02:45:55 -04:00
|
|
|
|
2018-03-22 08:39:59 -04:00
|
|
|
/* Enable AES hardware */
|
2018-10-29 11:55:02 -04:00
|
|
|
periph_module_enable(PERIPH_AES_MODULE);
|
2016-09-02 04:36:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void esp_aes_release_hardware( void )
|
|
|
|
{
|
2018-03-22 08:39:59 -04:00
|
|
|
/* Disable AES hardware */
|
2018-10-29 11:55:02 -04:00
|
|
|
periph_module_disable(PERIPH_AES_MODULE);
|
2017-08-15 02:45:55 -04:00
|
|
|
|
2017-08-15 19:06:52 -04:00
|
|
|
portEXIT_CRITICAL(&aes_spinlock);
|
2016-09-02 04:36:26 -04:00
|
|
|
}
|
|
|
|
|
2018-07-02 07:10:43 -04:00
|
|
|
|
|
|
|
|
2017-08-15 18:58:33 -04:00
|
|
|
/* Run a single 16 byte block of AES, using the hardware engine.
|
2016-09-02 04:36:26 -04:00
|
|
|
*
|
2017-08-15 18:58:33 -04:00
|
|
|
* Call only while holding esp_aes_acquire_hardware().
|
2016-09-02 04:36:26 -04:00
|
|
|
*/
|
2019-05-21 04:12:42 -04:00
|
|
|
static int esp_aes_block(esp_aes_context *ctx, const void *input, void *output)
|
2016-08-08 01:56:36 -04:00
|
|
|
{
|
2019-05-17 15:54:40 -04:00
|
|
|
uint32_t i0, i1, i2, i3;
|
2020-11-12 02:11:38 -05:00
|
|
|
const uint32_t *input_words = (uint32_t *)input;
|
2017-08-15 18:58:33 -04:00
|
|
|
uint32_t *output_words = (uint32_t *)output;
|
|
|
|
|
2019-05-21 04:12:42 -04:00
|
|
|
/* If no key is written to hardware yet, either the user hasn't called
|
|
|
|
mbedtls_aes_setkey_enc/mbedtls_aes_setkey_dec - meaning we also don't
|
|
|
|
know which mode to use - or a fault skipped the
|
|
|
|
key write to hardware. Treat this as a fatal error and zero the output block.
|
|
|
|
*/
|
|
|
|
if (ctx->key_in_hardware != ctx->key_bytes) {
|
|
|
|
bzero(output, 16);
|
|
|
|
return MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH;
|
|
|
|
}
|
2019-05-17 15:54:40 -04:00
|
|
|
i0 = input_words[0];
|
|
|
|
i1 = input_words[1];
|
|
|
|
i2 = input_words[2];
|
|
|
|
i3 = input_words[3];
|
|
|
|
|
2020-11-12 02:11:38 -05:00
|
|
|
aes_hal_transform_block(input, output);
|
2019-05-17 15:54:40 -04:00
|
|
|
|
|
|
|
/* Physical security check: Verify the AES accelerator actually ran, and wasn't
|
|
|
|
skipped due to external fault injection while starting the peripheral.
|
|
|
|
|
|
|
|
Note that i0,i1,i2,i3 are copied from input buffer in case input==output.
|
|
|
|
|
|
|
|
Bypassing this check requires at least one additional fault.
|
|
|
|
*/
|
2020-11-12 02:11:38 -05:00
|
|
|
if (i0 == output_words[0] && i1 == output_words[1] && i2 == output_words[2] && i3 == output_words[3]) {
|
2019-05-21 04:12:42 -04:00
|
|
|
// calling zeroing functions to narrow the
|
|
|
|
// window for a double-fault of the abort step, here
|
2019-05-17 15:54:40 -04:00
|
|
|
memset(output, 0, 16);
|
|
|
|
mbedtls_platform_zeroize(output, 16);
|
2019-05-21 04:12:42 -04:00
|
|
|
abort();
|
2019-05-17 15:54:40 -04:00
|
|
|
}
|
2019-05-21 04:12:42 -04:00
|
|
|
|
|
|
|
return 0;
|
2017-08-15 02:45:55 -04:00
|
|
|
}
|
|
|
|
|
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] )
|
|
|
|
{
|
|
|
|
esp_internal_aes_encrypt(ctx, input, output);
|
|
|
|
}
|
|
|
|
|
2016-08-05 05:40:32 -04:00
|
|
|
/*
|
|
|
|
* AES-ECB 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] )
|
2016-08-05 05:40:32 -04:00
|
|
|
{
|
2019-05-21 04:12:42 -04:00
|
|
|
int r;
|
|
|
|
|
|
|
|
if (!valid_key_length(ctx)) {
|
|
|
|
return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
|
|
|
|
}
|
|
|
|
|
2016-09-02 04:36:26 -04:00
|
|
|
esp_aes_acquire_hardware();
|
2019-05-21 04:12:42 -04:00
|
|
|
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);
|
2019-05-21 04:12:42 -04:00
|
|
|
r = esp_aes_block(ctx, input, output);
|
2016-09-02 04:36:26 -04:00
|
|
|
esp_aes_release_hardware();
|
2019-05-21 04:12:42 -04:00
|
|
|
return r;
|
2018-05-08 11:50:22 -04:00
|
|
|
}
|
|
|
|
|
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] )
|
|
|
|
{
|
|
|
|
esp_internal_aes_decrypt(ctx, input, output);
|
|
|
|
}
|
|
|
|
|
2016-08-05 05:40:32 -04:00
|
|
|
/*
|
|
|
|
* AES-ECB 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] )
|
2016-08-05 05:40:32 -04:00
|
|
|
{
|
2019-05-21 04:12:42 -04:00
|
|
|
int r;
|
|
|
|
|
|
|
|
if (!valid_key_length(ctx)) {
|
|
|
|
return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
|
|
|
|
}
|
|
|
|
|
2016-09-02 04:36:26 -04:00
|
|
|
esp_aes_acquire_hardware();
|
2019-05-21 04:12:42 -04:00
|
|
|
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);
|
2019-05-21 04:12:42 -04:00
|
|
|
r = esp_aes_block(ctx, input, output);
|
2016-09-02 04:36:26 -04:00
|
|
|
esp_aes_release_hardware();
|
2019-05-21 04:12:42 -04:00
|
|
|
return r;
|
2018-05-08 11:50:22 -04:00
|
|
|
}
|
|
|
|
|
2016-08-05 05:40:32 -04:00
|
|
|
/*
|
|
|
|
* 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] )
|
2016-08-05 05:40:32 -04:00
|
|
|
{
|
2019-05-21 04:12:42 -04:00
|
|
|
int r;
|
|
|
|
|
|
|
|
if (!valid_key_length(ctx)) {
|
|
|
|
return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
|
|
|
|
}
|
|
|
|
|
2016-09-02 04:36:26 -04:00
|
|
|
esp_aes_acquire_hardware();
|
2019-05-21 04:12:42 -04:00
|
|
|
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);
|
2019-05-21 04:12:42 -04:00
|
|
|
r = esp_aes_block(ctx, input, output);
|
2016-09-02 04:36:26 -04:00
|
|
|
esp_aes_release_hardware();
|
2017-08-15 19:06:52 -04:00
|
|
|
|
2019-05-21 04:12:42 -04:00
|
|
|
return r;
|
2016-08-05 05:40:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 )
|
2016-08-05 05:40:32 -04:00
|
|
|
{
|
2017-08-15 18:58:33 -04:00
|
|
|
uint32_t *output_words = (uint32_t *)output;
|
|
|
|
const uint32_t *input_words = (const uint32_t *)input;
|
|
|
|
uint32_t *iv_words = (uint32_t *)iv;
|
2016-08-05 05:40:32 -04:00
|
|
|
unsigned char temp[16];
|
|
|
|
|
2016-09-01 23:31:38 -04:00
|
|
|
if ( length % 16 ) {
|
2016-09-05 20:38:12 -04:00
|
|
|
return ( ERR_ESP_AES_INVALID_INPUT_LENGTH );
|
2016-09-01 23:31:38 -04:00
|
|
|
}
|
2016-08-08 01:56:36 -04:00
|
|
|
|
2019-05-21 04:12:42 -04:00
|
|
|
if (!valid_key_length(ctx)) {
|
|
|
|
return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
|
|
|
|
}
|
|
|
|
|
2016-09-02 04:36:26 -04:00
|
|
|
esp_aes_acquire_hardware();
|
2019-05-21 04:12:42 -04:00
|
|
|
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);
|
2017-08-15 18:58:33 -04:00
|
|
|
|
2016-09-02 04:36:26 -04:00
|
|
|
|
2016-09-05 20:38:12 -04:00
|
|
|
if ( mode == ESP_AES_DECRYPT ) {
|
2016-09-01 23:31:38 -04:00
|
|
|
while ( length > 0 ) {
|
2017-08-15 18:58:33 -04:00
|
|
|
memcpy(temp, input_words, 16);
|
2019-05-21 04:12:42 -04:00
|
|
|
esp_aes_block(ctx, input_words, output_words);
|
2016-08-05 05:40:32 -04:00
|
|
|
|
2020-11-12 02:11:38 -05:00
|
|
|
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];
|
2016-08-05 05:40:32 -04:00
|
|
|
|
2017-08-15 18:58:33 -04:00
|
|
|
memcpy( iv_words, temp, 16 );
|
2016-08-05 05:40:32 -04:00
|
|
|
|
2017-08-15 18:58:33 -04:00
|
|
|
input_words += 4;
|
|
|
|
output_words += 4;
|
2016-08-05 05:40:32 -04:00
|
|
|
length -= 16;
|
|
|
|
}
|
2017-08-15 02:45:55 -04:00
|
|
|
} else { // ESP_AES_ENCRYPT
|
2016-09-01 23:31:38 -04:00
|
|
|
while ( length > 0 ) {
|
2017-08-15 18:58:33 -04:00
|
|
|
|
2020-11-12 02:11:38 -05:00
|
|
|
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];
|
2016-08-05 05:40:32 -04:00
|
|
|
|
2019-05-21 04:12:42 -04:00
|
|
|
esp_aes_block(ctx, output_words, output_words);
|
2017-08-15 18:58:33 -04:00
|
|
|
memcpy( iv_words, output_words, 16 );
|
2016-08-05 05:40:32 -04:00
|
|
|
|
2017-08-15 18:58:33 -04:00
|
|
|
input_words += 4;
|
|
|
|
output_words += 4;
|
2016-08-05 05:40:32 -04:00
|
|
|
length -= 16;
|
|
|
|
}
|
|
|
|
}
|
2016-08-08 01:56:36 -04:00
|
|
|
|
2016-09-02 04:36:26 -04:00
|
|
|
esp_aes_release_hardware();
|
|
|
|
|
2016-09-01 23:31:38 -04:00
|
|
|
return 0;
|
2016-08-05 05:40:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 )
|
2016-08-05 05:40:32 -04:00
|
|
|
{
|
2016-09-01 23:31:38 -04:00
|
|
|
int c;
|
|
|
|
size_t n = *iv_off;
|
|
|
|
|
2019-05-21 04:12:42 -04:00
|
|
|
if (!valid_key_length(ctx)) {
|
|
|
|
return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
|
|
|
|
}
|
|
|
|
|
2016-09-02 04:36:26 -04:00
|
|
|
esp_aes_acquire_hardware();
|
2019-05-21 04:12:42 -04:00
|
|
|
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);
|
2016-09-02 04:36:26 -04:00
|
|
|
|
2016-09-05 20:38:12 -04:00
|
|
|
if ( mode == ESP_AES_DECRYPT ) {
|
2016-09-01 23:31:38 -04:00
|
|
|
while ( length-- ) {
|
|
|
|
if ( n == 0 ) {
|
2019-05-21 04:12:42 -04:00
|
|
|
esp_aes_block(ctx, iv, iv);
|
2016-09-01 23:31:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
c = *input++;
|
|
|
|
*output++ = (unsigned char)( c ^ iv[n] );
|
|
|
|
iv[n] = (unsigned char) c;
|
|
|
|
|
|
|
|
n = ( n + 1 ) & 0x0F;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while ( length-- ) {
|
|
|
|
if ( n == 0 ) {
|
2019-05-21 04:12:42 -04:00
|
|
|
esp_aes_block(ctx, iv, iv);
|
2016-09-01 23:31:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
iv[n] = *output++ = (unsigned char)( iv[n] ^ *input++ );
|
|
|
|
|
|
|
|
n = ( n + 1 ) & 0x0F;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*iv_off = n;
|
|
|
|
|
2016-09-02 04:36:26 -04:00
|
|
|
esp_aes_release_hardware();
|
|
|
|
|
2016-09-01 23:31:38 -04:00
|
|
|
return 0;
|
2016-08-05 05:40:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 )
|
2016-08-05 05:40:32 -04:00
|
|
|
{
|
2016-09-01 23:31:38 -04:00
|
|
|
unsigned char c;
|
|
|
|
unsigned char ov[17];
|
|
|
|
|
2019-05-21 04:12:42 -04:00
|
|
|
if (!valid_key_length(ctx)) {
|
|
|
|
return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
|
|
|
|
}
|
|
|
|
|
2016-09-02 04:36:26 -04:00
|
|
|
esp_aes_acquire_hardware();
|
2019-05-21 04:12:42 -04:00
|
|
|
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);
|
2017-08-15 18:58:33 -04:00
|
|
|
|
2016-09-02 04:36:26 -04:00
|
|
|
|
2016-09-01 23:31:38 -04:00
|
|
|
while ( length-- ) {
|
|
|
|
memcpy( ov, iv, 16 );
|
2019-05-21 04:12:42 -04:00
|
|
|
esp_aes_block(ctx, iv, iv);
|
2016-09-01 23:31:38 -04:00
|
|
|
|
2016-09-05 20:38:12 -04:00
|
|
|
if ( mode == ESP_AES_DECRYPT ) {
|
2016-09-01 23:31:38 -04:00
|
|
|
ov[16] = *input;
|
|
|
|
}
|
|
|
|
|
|
|
|
c = *output++ = (unsigned char)( iv[0] ^ *input++ );
|
|
|
|
|
2016-09-05 20:38:12 -04:00
|
|
|
if ( mode == ESP_AES_ENCRYPT ) {
|
2016-09-01 23:31:38 -04:00
|
|
|
ov[16] = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy( iv, ov + 1, 16 );
|
|
|
|
}
|
|
|
|
|
2016-09-02 04:36:26 -04:00
|
|
|
esp_aes_release_hardware();
|
|
|
|
|
2016-09-01 23:31:38 -04:00
|
|
|
return 0;
|
2016-08-05 05:40:32 -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 )
|
2016-09-01 23:31:38 -04:00
|
|
|
{
|
|
|
|
int c, i;
|
2016-08-05 05:40:32 -04:00
|
|
|
size_t n = *nc_off;
|
|
|
|
|
2019-05-21 04:12:42 -04:00
|
|
|
if (!valid_key_length(ctx)) {
|
|
|
|
return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
|
|
|
|
}
|
|
|
|
|
2016-09-02 04:36:26 -04:00
|
|
|
esp_aes_acquire_hardware();
|
2019-05-21 04:12:42 -04:00
|
|
|
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);
|
2017-08-15 18:58:33 -04:00
|
|
|
|
2016-09-02 04:36:26 -04:00
|
|
|
|
2016-09-01 23:31:38 -04:00
|
|
|
while ( length-- ) {
|
|
|
|
if ( n == 0 ) {
|
2019-05-21 04:12:42 -04:00
|
|
|
esp_aes_block(ctx, nonce_counter, stream_block);
|
2016-08-05 05:40:32 -04:00
|
|
|
|
2020-11-12 02:11:38 -05:00
|
|
|
for ( i = 16; i > 0; i-- ) {
|
2016-09-01 23:31:38 -04:00
|
|
|
if ( ++nonce_counter[i - 1] != 0 ) {
|
2016-08-05 05:40:32 -04:00
|
|
|
break;
|
2016-09-01 23:31:38 -04:00
|
|
|
}
|
2020-11-12 02:11:38 -05:00
|
|
|
}
|
2016-08-05 05:40:32 -04:00
|
|
|
}
|
|
|
|
c = *input++;
|
|
|
|
*output++ = (unsigned char)( c ^ stream_block[n] );
|
|
|
|
|
|
|
|
n = ( n + 1 ) & 0x0F;
|
|
|
|
}
|
|
|
|
|
|
|
|
*nc_off = n;
|
2016-08-08 01:56:36 -04:00
|
|
|
|
2016-09-02 04:36:26 -04:00
|
|
|
esp_aes_release_hardware();
|
|
|
|
|
2016-09-01 23:31:38 -04:00
|
|
|
return 0;
|
2016-08-05 05:40:32 -04:00
|
|
|
}
|
2018-07-02 07:10:43 -04:00
|
|
|
|
2019-06-26 05:51:30 -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 )
|
2019-06-26 05:51:30 -04:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
size_t n;
|
|
|
|
|
2020-11-12 02:11:38 -05:00
|
|
|
if (ctx == NULL || iv_off == NULL || iv == NULL ||
|
|
|
|
input == NULL || output == NULL ) {
|
|
|
|
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
|
2019-06-26 05:51:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
n = *iv_off;
|
|
|
|
|
2020-11-12 02:11:38 -05:00
|
|
|
if (n > 15) {
|
|
|
|
return (MBEDTLS_ERR_AES_BAD_INPUT_DATA);
|
2019-06-26 05:51:30 -04:00
|
|
|
}
|
|
|
|
|
2019-05-21 04:12:42 -04:00
|
|
|
if (!valid_key_length(ctx)) {
|
|
|
|
return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
|
|
|
|
}
|
|
|
|
|
2019-06-26 05:51:30 -04:00
|
|
|
esp_aes_acquire_hardware();
|
2020-11-12 02:11:38 -05:00
|
|
|
ctx->key_in_hardware = 0;
|
|
|
|
ctx->key_in_hardware = aes_hal_setkey(ctx->key, ctx->key_bytes, ESP_AES_ENCRYPT);
|
2019-06-26 05:51:30 -04:00
|
|
|
|
|
|
|
|
2020-11-12 02:11:38 -05:00
|
|
|
while (length--) {
|
|
|
|
if ( n == 0 ) {
|
2019-05-21 04:12:42 -04:00
|
|
|
esp_aes_block(ctx, iv, iv);
|
2019-06-26 05:51:30 -04:00
|
|
|
}
|
|
|
|
*output++ = *input++ ^ iv[n];
|
|
|
|
|
|
|
|
n = ( n + 1 ) & 0x0F;
|
|
|
|
}
|
|
|
|
|
|
|
|
*iv_off = n;
|
|
|
|
|
|
|
|
esp_aes_release_hardware();
|
|
|
|
|
2020-11-12 02:11:38 -05:00
|
|
|
return ( ret );
|
2020-11-10 02:40:01 -05:00
|
|
|
}
|