2016-09-05 20:38:12 -04:00
|
|
|
/**
|
|
|
|
* \brief AES block cipher, ESP32 hardware accelerated version
|
|
|
|
* 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"
|
2016-09-02 04:36:26 -04:00
|
|
|
#include "hwcrypto/aes.h"
|
2019-05-17 15:54:40 -04:00
|
|
|
#include "mbedtls/platform_util.h"
|
2016-11-22 02:09:32 -05:00
|
|
|
#include "soc/dport_reg.h"
|
2017-08-15 18:58:33 -04:00
|
|
|
#include "soc/hwcrypto_reg.h"
|
2016-09-02 04:36:26 -04:00
|
|
|
#include <sys/lock.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
|
|
|
static inline bool valid_key_length(const esp_aes_context *ctx)
|
|
|
|
{
|
|
|
|
return ctx->key_bytes == 128/8 || ctx->key_bytes == 192/8 || ctx->key_bytes == 256/8;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-09-05 20:38:12 -04:00
|
|
|
void esp_aes_init( esp_aes_context *ctx )
|
2016-09-02 04:36:26 -04:00
|
|
|
{
|
2016-09-05 20:38:12 -04:00
|
|
|
bzero( ctx, sizeof( esp_aes_context ) );
|
2016-08-05 05:40:32 -04:00
|
|
|
}
|
|
|
|
|
2016-09-05 20:38:12 -04:00
|
|
|
void esp_aes_free( esp_aes_context *ctx )
|
2016-08-05 05:40:32 -04:00
|
|
|
{
|
2016-09-01 23:31:38 -04:00
|
|
|
if ( ctx == NULL ) {
|
2016-08-05 05:40:32 -04:00
|
|
|
return;
|
2016-09-01 23:31:38 -04:00
|
|
|
}
|
2016-08-05 05:40:32 -04:00
|
|
|
|
2016-09-05 20:38:12 -04:00
|
|
|
bzero( ctx, sizeof( esp_aes_context ) );
|
2016-08-05 05:40:32 -04:00
|
|
|
}
|
|
|
|
|
2018-07-02 07:10:43 -04:00
|
|
|
|
|
|
|
|
2016-09-02 04:36:26 -04:00
|
|
|
/*
|
2017-08-15 18:58:33 -04:00
|
|
|
* AES key schedule (same for encryption or decryption, as hardware handles schedule)
|
2016-09-02 04:36:26 -04:00
|
|
|
*
|
|
|
|
*/
|
2017-08-15 18:58:33 -04:00
|
|
|
int esp_aes_setkey( esp_aes_context *ctx, const unsigned char *key,
|
|
|
|
unsigned int keybits )
|
2016-09-02 04:36:26 -04:00
|
|
|
{
|
2017-08-15 18:58:33 -04:00
|
|
|
if (keybits != 128 && keybits != 192 && keybits != 256) {
|
|
|
|
return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
|
2016-09-01 23:31:38 -04:00
|
|
|
}
|
2017-08-15 18:58:33 -04:00
|
|
|
ctx->key_bytes = keybits / 8;
|
|
|
|
memcpy(ctx->key, key, ctx->key_bytes);
|
2019-05-21 04:12:42 -04:00
|
|
|
ctx->key_in_hardware = 0;
|
2016-09-01 23:31:38 -04:00
|
|
|
return 0;
|
2016-08-05 05:40:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-08-15 18:58:33 -04:00
|
|
|
* Helper function to copy key from esp_aes_context buffer
|
|
|
|
* to hardware key registers.
|
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-08-05 05:40:32 -04:00
|
|
|
*/
|
2019-05-21 04:12:42 -04:00
|
|
|
static void esp_aes_setkey_hardware(esp_aes_context *ctx, int mode)
|
2016-08-05 05:40:32 -04:00
|
|
|
{
|
2017-08-15 18:58:33 -04:00
|
|
|
const uint32_t MODE_DECRYPT_BIT = 4;
|
|
|
|
unsigned mode_reg_base = (mode == ESP_AES_ENCRYPT) ? 0 : MODE_DECRYPT_BIT;
|
|
|
|
|
2019-05-21 04:12:42 -04:00
|
|
|
ctx->key_in_hardware = 0;
|
|
|
|
|
2018-10-29 11:55:02 -04:00
|
|
|
for (int i = 0; i < ctx->key_bytes/4; ++i) {
|
|
|
|
DPORT_REG_WRITE(AES_KEY_BASE + i * 4, *(((uint32_t *)ctx->key) + i));
|
2019-05-21 04:12:42 -04:00
|
|
|
ctx->key_in_hardware += 4;
|
2018-10-29 11:55:02 -04:00
|
|
|
}
|
|
|
|
|
2017-08-15 18:58:33 -04:00
|
|
|
DPORT_REG_WRITE(AES_MODE_REG, mode_reg_base + ((ctx->key_bytes / 8) - 2));
|
2019-05-21 04:12:42 -04:00
|
|
|
|
|
|
|
/* Fault injection check: all words of key data should have been written to hardware */
|
|
|
|
if (ctx->key_in_hardware < 16
|
|
|
|
|| ctx->key_in_hardware != ctx->key_bytes) {
|
|
|
|
abort();
|
|
|
|
}
|
2016-08-05 05:40:32 -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
|
|
|
{
|
2017-08-15 18:58:33 -04:00
|
|
|
const uint32_t *input_words = (const uint32_t *)input;
|
2019-05-17 15:54:40 -04:00
|
|
|
uint32_t i0, i1, i2, i3;
|
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
|
|
|
/* Storing i0,i1,i2,i3 in registers not an array
|
|
|
|
helps a lot with optimisations at -Os level */
|
|
|
|
i0 = input_words[0];
|
|
|
|
DPORT_REG_WRITE(AES_TEXT_BASE, i0);
|
|
|
|
|
|
|
|
i1 = input_words[1];
|
|
|
|
DPORT_REG_WRITE(AES_TEXT_BASE + 4, i1);
|
|
|
|
|
|
|
|
i2 = input_words[2];
|
|
|
|
DPORT_REG_WRITE(AES_TEXT_BASE + 8, i2);
|
|
|
|
|
|
|
|
i3 = input_words[3];
|
|
|
|
DPORT_REG_WRITE(AES_TEXT_BASE + 12, i3);
|
2016-08-08 01:56:36 -04:00
|
|
|
|
2017-08-15 18:58:33 -04:00
|
|
|
DPORT_REG_WRITE(AES_START_REG, 1);
|
2019-05-17 15:54:40 -04:00
|
|
|
|
2018-03-22 08:39:59 -04:00
|
|
|
while (DPORT_REG_READ(AES_IDLE_REG) != 1) { }
|
2019-05-17 15:54:40 -04:00
|
|
|
|
|
|
|
esp_dport_access_read_buffer(output_words, AES_TEXT_BASE, 4);
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-08-05 05:40:32 -04:00
|
|
|
/*
|
|
|
|
* AES-ECB block encryption
|
|
|
|
*/
|
2018-05-08 11:50:22 -04:00
|
|
|
int esp_internal_aes_encrypt( esp_aes_context *ctx,
|
2016-09-01 23:31:38 -04:00
|
|
|
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;
|
2016-09-07 00:48:20 -04:00
|
|
|
esp_aes_setkey_hardware(ctx, 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
|
|
|
}
|
|
|
|
|
|
|
|
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 decryption
|
|
|
|
*/
|
|
|
|
|
2018-05-08 11:50:22 -04:00
|
|
|
int esp_internal_aes_decrypt( esp_aes_context *ctx,
|
2016-09-01 23:31:38 -04:00
|
|
|
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;
|
2016-09-07 00:48:20 -04:00
|
|
|
esp_aes_setkey_hardware(ctx, 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
|
|
|
}
|
|
|
|
|
|
|
|
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 encryption/decryption
|
|
|
|
*/
|
2016-09-05 20:38:12 -04:00
|
|
|
int esp_aes_crypt_ecb( esp_aes_context *ctx,
|
2016-09-01 23:31:38 -04:00
|
|
|
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;
|
2016-09-07 00:48:20 -04:00
|
|
|
esp_aes_setkey_hardware(ctx, 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
|
|
|
|
*/
|
2016-09-05 20:38:12 -04:00
|
|
|
int esp_aes_crypt_cbc( esp_aes_context *ctx,
|
2016-09-01 23:31:38 -04:00
|
|
|
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
|
|
|
int i;
|
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;
|
2017-08-15 18:58:33 -04:00
|
|
|
|
2016-09-07 00:48:20 -04:00
|
|
|
esp_aes_setkey_hardware(ctx, mode);
|
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
|
|
|
|
2017-08-15 18:58:33 -04:00
|
|
|
for ( i = 0; i < 4; i++ ) {
|
|
|
|
output_words[i] = output_words[i] ^ iv_words[i];
|
2016-09-01 23:31:38 -04:00
|
|
|
}
|
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
|
|
|
|
|
|
|
for ( i = 0; i < 4; i++ ) {
|
|
|
|
output_words[i] = input_words[i] ^ iv_words[i];
|
2016-09-01 23:31:38 -04:00
|
|
|
}
|
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
|
|
|
|
*/
|
2016-09-05 20:38:12 -04:00
|
|
|
int esp_aes_crypt_cfb128( esp_aes_context *ctx,
|
2016-09-01 23:31:38 -04:00
|
|
|
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;
|
2017-08-15 18:58:33 -04:00
|
|
|
|
2016-09-08 03:06:27 -04:00
|
|
|
esp_aes_setkey_hardware(ctx, 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
|
|
|
|
*/
|
2016-09-05 20:38:12 -04:00
|
|
|
int esp_aes_crypt_cfb8( esp_aes_context *ctx,
|
2016-09-01 23:31:38 -04:00
|
|
|
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;
|
2017-08-15 18:58:33 -04:00
|
|
|
|
2016-09-08 03:06:27 -04:00
|
|
|
esp_aes_setkey_hardware(ctx, ESP_AES_ENCRYPT);
|
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
|
|
|
|
*/
|
2016-09-05 20:38:12 -04:00
|
|
|
int esp_aes_crypt_ctr( esp_aes_context *ctx,
|
2016-08-05 05:40:32 -04:00
|
|
|
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;
|
2017-08-15 18:58:33 -04:00
|
|
|
|
2016-09-07 00:48:20 -04:00
|
|
|
esp_aes_setkey_hardware(ctx, ESP_AES_ENCRYPT);
|
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
|
|
|
|
2016-09-01 23:31:38 -04:00
|
|
|
for ( i = 16; i > 0; i-- )
|
|
|
|
if ( ++nonce_counter[i - 1] != 0 ) {
|
2016-08-05 05:40:32 -04:00
|
|
|
break;
|
2016-09-01 23:31:38 -04: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
|
|
|
|
|
|
|
/* Below XTS implementation is copied aes.c of mbedtls library.
|
|
|
|
* When MBEDTLS_AES_ALT is defined mbedtls expects alternate
|
|
|
|
* definition of XTS functions to be available. Even if this
|
|
|
|
* could have been avoided, it is done for consistency reason.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void esp_aes_xts_init( esp_aes_xts_context *ctx )
|
|
|
|
{
|
|
|
|
esp_aes_init( &ctx->crypt );
|
|
|
|
esp_aes_init( &ctx->tweak );
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_aes_xts_free( esp_aes_xts_context *ctx )
|
|
|
|
{
|
|
|
|
esp_aes_free( &ctx->crypt );
|
|
|
|
esp_aes_free( &ctx->tweak );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int esp_aes_xts_decode_keys( const unsigned char *key,
|
|
|
|
unsigned int keybits,
|
|
|
|
const unsigned char **key1,
|
|
|
|
unsigned int *key1bits,
|
|
|
|
const unsigned char **key2,
|
|
|
|
unsigned int *key2bits )
|
|
|
|
{
|
|
|
|
const unsigned int half_keybits = keybits / 2;
|
|
|
|
const unsigned int half_keybytes = half_keybits / 8;
|
|
|
|
|
|
|
|
switch( keybits )
|
|
|
|
{
|
|
|
|
case 256: break;
|
|
|
|
case 512: break;
|
|
|
|
default : return( MBEDTLS_ERR_AES_INVALID_KEY_LENGTH );
|
|
|
|
}
|
|
|
|
|
|
|
|
*key1bits = half_keybits;
|
|
|
|
*key2bits = half_keybits;
|
|
|
|
*key1 = &key[0];
|
|
|
|
*key2 = &key[half_keybytes];
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-17 09:53:46 -04:00
|
|
|
int esp_aes_xts_setkey_enc( esp_aes_xts_context *ctx,
|
2018-07-02 07:10:43 -04:00
|
|
|
const unsigned char *key,
|
|
|
|
unsigned int keybits)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
const unsigned char *key1, *key2;
|
|
|
|
unsigned int key1bits, key2bits;
|
|
|
|
|
|
|
|
ret = esp_aes_xts_decode_keys( key, keybits, &key1, &key1bits,
|
|
|
|
&key2, &key2bits );
|
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
/* Set the tweak key. Always set tweak key for the encryption mode. */
|
|
|
|
ret = esp_aes_setkey( &ctx->tweak, key2, key2bits );
|
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
/* Set crypt key for encryption. */
|
|
|
|
return esp_aes_setkey( &ctx->crypt, key1, key1bits );
|
|
|
|
}
|
|
|
|
|
2018-10-17 09:53:46 -04:00
|
|
|
int esp_aes_xts_setkey_dec( esp_aes_xts_context *ctx,
|
2018-07-02 07:10:43 -04:00
|
|
|
const unsigned char *key,
|
|
|
|
unsigned int keybits)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
const unsigned char *key1, *key2;
|
|
|
|
unsigned int key1bits, key2bits;
|
|
|
|
|
|
|
|
ret = esp_aes_xts_decode_keys( key, keybits, &key1, &key1bits,
|
|
|
|
&key2, &key2bits );
|
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
/* Set the tweak key. Always set tweak key for encryption. */
|
|
|
|
ret = esp_aes_setkey( &ctx->tweak, key2, key2bits );
|
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
/* Set crypt key for decryption. */
|
|
|
|
return esp_aes_setkey( &ctx->crypt, key1, key1bits );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Endianess with 64 bits values */
|
|
|
|
#ifndef GET_UINT64_LE
|
|
|
|
#define GET_UINT64_LE(n,b,i) \
|
|
|
|
{ \
|
|
|
|
(n) = ( (uint64_t) (b)[(i) + 7] << 56 ) \
|
|
|
|
| ( (uint64_t) (b)[(i) + 6] << 48 ) \
|
|
|
|
| ( (uint64_t) (b)[(i) + 5] << 40 ) \
|
|
|
|
| ( (uint64_t) (b)[(i) + 4] << 32 ) \
|
|
|
|
| ( (uint64_t) (b)[(i) + 3] << 24 ) \
|
|
|
|
| ( (uint64_t) (b)[(i) + 2] << 16 ) \
|
|
|
|
| ( (uint64_t) (b)[(i) + 1] << 8 ) \
|
|
|
|
| ( (uint64_t) (b)[(i) ] ); \
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef PUT_UINT64_LE
|
|
|
|
#define PUT_UINT64_LE(n,b,i) \
|
|
|
|
{ \
|
|
|
|
(b)[(i) + 7] = (unsigned char) ( (n) >> 56 ); \
|
|
|
|
(b)[(i) + 6] = (unsigned char) ( (n) >> 48 ); \
|
|
|
|
(b)[(i) + 5] = (unsigned char) ( (n) >> 40 ); \
|
|
|
|
(b)[(i) + 4] = (unsigned char) ( (n) >> 32 ); \
|
|
|
|
(b)[(i) + 3] = (unsigned char) ( (n) >> 24 ); \
|
|
|
|
(b)[(i) + 2] = (unsigned char) ( (n) >> 16 ); \
|
|
|
|
(b)[(i) + 1] = (unsigned char) ( (n) >> 8 ); \
|
|
|
|
(b)[(i) ] = (unsigned char) ( (n) ); \
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef unsigned char esp_be128[16];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* GF(2^128) multiplication function
|
|
|
|
*
|
|
|
|
* This function multiplies a field element by x in the polynomial field
|
|
|
|
* representation. It uses 64-bit word operations to gain speed but compensates
|
|
|
|
* for machine endianess and hence works correctly on both big and little
|
|
|
|
* endian machines.
|
|
|
|
*/
|
|
|
|
static void esp_gf128mul_x_ble( unsigned char r[16],
|
|
|
|
const unsigned char x[16] )
|
|
|
|
{
|
|
|
|
uint64_t a, b, ra, rb;
|
|
|
|
|
|
|
|
GET_UINT64_LE( a, x, 0 );
|
|
|
|
GET_UINT64_LE( b, x, 8 );
|
|
|
|
|
|
|
|
ra = ( a << 1 ) ^ 0x0087 >> ( 8 - ( ( b >> 63 ) << 3 ) );
|
|
|
|
rb = ( a >> 63 ) | ( b << 1 );
|
|
|
|
|
|
|
|
PUT_UINT64_LE( ra, r, 0 );
|
|
|
|
PUT_UINT64_LE( rb, r, 8 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* AES-XTS buffer encryption/decryption
|
|
|
|
*/
|
2018-10-17 09:53:46 -04:00
|
|
|
int esp_aes_crypt_xts( esp_aes_xts_context *ctx,
|
2018-07-02 07:10:43 -04:00
|
|
|
int mode,
|
|
|
|
size_t length,
|
|
|
|
const unsigned char data_unit[16],
|
|
|
|
const unsigned char *input,
|
|
|
|
unsigned char *output )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t blocks = length / 16;
|
|
|
|
size_t leftover = length % 16;
|
|
|
|
unsigned char tweak[16];
|
|
|
|
unsigned char prev_tweak[16];
|
|
|
|
unsigned char tmp[16];
|
|
|
|
|
|
|
|
/* Sectors must be at least 16 bytes. */
|
|
|
|
if( length < 16 )
|
|
|
|
return MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH;
|
|
|
|
|
|
|
|
/* NIST SP 80-38E disallows data units larger than 2**20 blocks. */
|
|
|
|
if( length > ( 1 << 20 ) * 16 )
|
|
|
|
return MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH;
|
|
|
|
|
|
|
|
/* Compute the tweak. */
|
|
|
|
ret = esp_aes_crypt_ecb( &ctx->tweak, MBEDTLS_AES_ENCRYPT,
|
|
|
|
data_unit, tweak );
|
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
while( blocks-- )
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if( leftover && ( mode == MBEDTLS_AES_DECRYPT ) && blocks == 0 )
|
|
|
|
{
|
|
|
|
/* We are on the last block in a decrypt operation that has
|
|
|
|
* leftover bytes, so we need to use the next tweak for this block,
|
|
|
|
* and this tweak for the lefover bytes. Save the current tweak for
|
|
|
|
* the leftovers and then update the current tweak for use on this,
|
|
|
|
* the last full block. */
|
|
|
|
memcpy( prev_tweak, tweak, sizeof( tweak ) );
|
|
|
|
esp_gf128mul_x_ble( tweak, tweak );
|
|
|
|
}
|
|
|
|
|
|
|
|
for( i = 0; i < 16; i++ )
|
|
|
|
tmp[i] = input[i] ^ tweak[i];
|
|
|
|
|
|
|
|
ret = esp_aes_crypt_ecb( &ctx->crypt, mode, tmp, tmp );
|
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
for( i = 0; i < 16; i++ )
|
|
|
|
output[i] = tmp[i] ^ tweak[i];
|
|
|
|
|
|
|
|
/* Update the tweak for the next block. */
|
|
|
|
esp_gf128mul_x_ble( tweak, tweak );
|
|
|
|
|
|
|
|
output += 16;
|
|
|
|
input += 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( leftover )
|
|
|
|
{
|
|
|
|
/* If we are on the leftover bytes in a decrypt operation, we need to
|
|
|
|
* use the previous tweak for these bytes (as saved in prev_tweak). */
|
|
|
|
unsigned char *t = mode == MBEDTLS_AES_DECRYPT ? prev_tweak : tweak;
|
|
|
|
|
|
|
|
/* We are now on the final part of the data unit, which doesn't divide
|
|
|
|
* evenly by 16. It's time for ciphertext stealing. */
|
|
|
|
size_t i;
|
|
|
|
unsigned char *prev_output = output - 16;
|
|
|
|
|
|
|
|
/* Copy ciphertext bytes from the previous block to our output for each
|
|
|
|
* byte of cyphertext we won't steal. At the same time, copy the
|
|
|
|
* remainder of the input for this final round (since the loop bounds
|
|
|
|
* are the same). */
|
|
|
|
for( i = 0; i < leftover; i++ )
|
|
|
|
{
|
|
|
|
output[i] = prev_output[i];
|
|
|
|
tmp[i] = input[i] ^ t[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy ciphertext bytes from the previous block for input in this
|
|
|
|
* round. */
|
|
|
|
for( ; i < 16; i++ )
|
|
|
|
tmp[i] = prev_output[i] ^ t[i];
|
|
|
|
|
|
|
|
ret = esp_aes_crypt_ecb( &ctx->crypt, mode, tmp, tmp );
|
|
|
|
if( ret != 0 )
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* Write the result back to the previous block, overriding the previous
|
|
|
|
* output we copied. */
|
|
|
|
for( i = 0; i < 16; i++ )
|
|
|
|
prev_output[i] = tmp[i] ^ t[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|