2019-05-09 23:34:06 -04:00
|
|
|
/*
|
2020-08-13 04:30:59 -04:00
|
|
|
* ESP hardware accelerated SHA1/256/512 implementation
|
2019-05-09 23:34:06 -04:00
|
|
|
* based on mbedTLS FIPS-197 compliant version.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
2020-01-16 01:31:10 -05:00
|
|
|
* Additions Copyright (C) 2016-2020, Espressif Systems (Shanghai) PTE Ltd
|
2019-05-09 23:34:06 -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 SHA-1 standard was published by NIST in 1993.
|
|
|
|
*
|
|
|
|
* http://www.itl.nist.gov/fipspubs/fip180-1.htm
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/lock.h>
|
2020-01-16 01:31:10 -05:00
|
|
|
|
|
|
|
#include "esp_log.h"
|
2020-02-07 07:08:34 -05:00
|
|
|
#include "esp_crypto_lock.h"
|
2020-08-13 04:30:59 -04:00
|
|
|
#include "soc/lldesc.h"
|
2019-05-09 23:34:06 -04:00
|
|
|
#include "soc/dport_reg.h"
|
2020-01-16 01:31:10 -05:00
|
|
|
#include "soc/cache_memory.h"
|
2020-02-07 07:08:34 -05:00
|
|
|
#include "soc/periph_defs.h"
|
2019-12-26 02:25:24 -05:00
|
|
|
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/semphr.h"
|
2019-05-09 23:34:06 -04:00
|
|
|
|
2020-01-16 01:31:10 -05:00
|
|
|
#include "driver/periph_ctrl.h"
|
|
|
|
#include "sys/param.h"
|
|
|
|
|
2020-08-13 04:30:59 -04:00
|
|
|
#include "sha/sha_dma.h"
|
|
|
|
#include "hal/sha_hal.h"
|
2020-10-28 22:51:36 -04:00
|
|
|
#include "soc/soc_caps.h"
|
2020-01-16 01:31:10 -05:00
|
|
|
|
2020-08-13 04:30:59 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32S2
|
|
|
|
#include "esp32s2/rom/cache.h"
|
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S3
|
|
|
|
#include "esp32s3/rom/cache.h"
|
|
|
|
#endif
|
2020-01-16 01:31:10 -05:00
|
|
|
|
2020-08-13 04:30:59 -04:00
|
|
|
#if SOC_SHA_GENERAL_DMA
|
|
|
|
#define SHA_LOCK() esp_crypto_sha_lock_acquire()
|
|
|
|
#define SHA_RELEASE() esp_crypto_sha_lock_release()
|
|
|
|
#elif SOC_SHA_CRYPTO_DMA
|
|
|
|
#define SHA_LOCK() esp_crypto_dma_lock_acquire()
|
|
|
|
#define SHA_RELEASE() esp_crypto_dma_lock_release()
|
|
|
|
#else
|
|
|
|
#define SHA_LOCK() ()
|
|
|
|
#endif
|
2020-04-08 04:37:51 -04:00
|
|
|
|
2020-01-16 01:31:10 -05:00
|
|
|
const static char *TAG = "esp-sha";
|
2019-05-09 23:34:06 -04:00
|
|
|
|
2020-08-13 04:30:59 -04:00
|
|
|
|
|
|
|
void esp_sha_write_digest_state(esp_sha_type sha_type, void *digest_state)
|
|
|
|
{
|
|
|
|
sha_hal_write_digest(sha_type, digest_state);
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_sha_read_digest_state(esp_sha_type sha_type, void *digest_state)
|
|
|
|
{
|
|
|
|
sha_hal_read_digest(sha_type, digest_state);
|
|
|
|
}
|
|
|
|
|
2019-05-09 23:34:06 -04:00
|
|
|
/* Return block size (in bytes) for a given SHA type */
|
2019-12-26 02:25:24 -05:00
|
|
|
inline static size_t block_length(esp_sha_type type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
2019-05-09 23:34:06 -04:00
|
|
|
case SHA1:
|
|
|
|
case SHA2_224:
|
|
|
|
case SHA2_256:
|
|
|
|
return 64;
|
|
|
|
case SHA2_384:
|
|
|
|
case SHA2_512:
|
2019-12-26 02:25:24 -05:00
|
|
|
case SHA2_512224:
|
|
|
|
case SHA2_512256:
|
|
|
|
case SHA2_512T:
|
2019-05-09 23:34:06 -04:00
|
|
|
return 128;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-16 01:31:10 -05:00
|
|
|
/* Enable SHA peripheral and then lock it */
|
|
|
|
void esp_sha_acquire_hardware()
|
2019-05-09 23:34:06 -04:00
|
|
|
{
|
2020-08-13 04:30:59 -04:00
|
|
|
SHA_LOCK(); /* Released when releasing hw with esp_sha_release_hardware() */
|
2019-12-26 02:25:24 -05:00
|
|
|
|
2020-01-16 01:31:10 -05:00
|
|
|
/* Enable SHA and DMA hardware */
|
2020-08-13 04:30:59 -04:00
|
|
|
#if SOC_SHA_CRYPTO_DMA
|
2020-01-16 01:31:10 -05:00
|
|
|
periph_module_enable(PERIPH_SHA_DMA_MODULE);
|
2020-08-13 04:30:59 -04:00
|
|
|
#elif SOC_SHA_GENERAL_DMA
|
|
|
|
periph_module_enable(PERIPH_SHA_MODULE);
|
|
|
|
periph_module_enable(PERIPH_GDMA_MODULE);
|
|
|
|
#endif
|
2019-05-09 23:34:06 -04:00
|
|
|
}
|
|
|
|
|
2020-01-16 01:31:10 -05:00
|
|
|
/* Disable SHA peripheral block and then release it */
|
|
|
|
void esp_sha_release_hardware()
|
2019-05-09 23:34:06 -04:00
|
|
|
{
|
2020-01-16 01:31:10 -05:00
|
|
|
/* Disable SHA and DMA hardware */
|
2020-08-13 04:30:59 -04:00
|
|
|
#if SOC_SHA_CRYPTO_DMA
|
2020-01-16 01:31:10 -05:00
|
|
|
periph_module_disable(PERIPH_SHA_DMA_MODULE);
|
2020-08-13 04:30:59 -04:00
|
|
|
#elif SOC_SHA_GENERAL_DMA
|
|
|
|
periph_module_disable(PERIPH_SHA_MODULE);
|
|
|
|
periph_module_disable(PERIPH_GDMA_MODULE);
|
|
|
|
#endif
|
2019-12-26 02:25:24 -05:00
|
|
|
|
2020-08-13 04:30:59 -04:00
|
|
|
SHA_RELEASE();
|
2019-05-09 23:34:06 -04:00
|
|
|
}
|
|
|
|
|
2020-08-13 04:30:59 -04:00
|
|
|
#if SOC_SHA_SUPPORT_SHA512_T
|
2020-01-16 01:31:10 -05:00
|
|
|
/* The initial hash value for SHA512/t is generated according to the
|
|
|
|
algorithm described in the TRM, chapter SHA-Accelerator
|
|
|
|
*/
|
|
|
|
int esp_sha_512_t_init_hash(uint16_t t)
|
2019-05-09 23:34:06 -04:00
|
|
|
{
|
2020-01-16 01:31:10 -05:00
|
|
|
uint32_t t_string = 0;
|
|
|
|
uint8_t t0, t1, t2, t_len;
|
|
|
|
|
|
|
|
if (t == 384) {
|
|
|
|
ESP_LOGE(TAG, "Invalid t for SHA512/t, t = %u,cannot be 384", t);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (t <= 9) {
|
|
|
|
t_string = (uint32_t)((1 << 23) | ((0x30 + t) << 24));
|
|
|
|
t_len = 0x48;
|
|
|
|
} else if (t <= 99) {
|
|
|
|
t0 = t % 10;
|
|
|
|
t1 = (t / 10) % 10;
|
|
|
|
t_string = (uint32_t)((1 << 15) | ((0x30 + t0) << 16) |
|
|
|
|
(((0x30 + t1) << 24)));
|
|
|
|
t_len = 0x50;
|
|
|
|
} else if (t <= 512) {
|
|
|
|
t0 = t % 10;
|
|
|
|
t1 = (t / 10) % 10;
|
|
|
|
t2 = t / 100;
|
|
|
|
t_string = (uint32_t)((1 << 7) | ((0x30 + t0) << 8) |
|
|
|
|
(((0x30 + t1) << 16) + ((0x30 + t2) << 24)));
|
|
|
|
t_len = 0x58;
|
|
|
|
} else {
|
|
|
|
ESP_LOGE(TAG, "Invalid t for SHA512/t, t = %u, must equal or less than 512", t);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-08-13 04:30:59 -04:00
|
|
|
sha_hal_sha512_init_hash(t_string, t_len);
|
2020-01-16 01:31:10 -05:00
|
|
|
|
|
|
|
return 0;
|
2019-12-26 02:25:24 -05:00
|
|
|
}
|
|
|
|
|
2020-08-13 04:30:59 -04:00
|
|
|
#endif //SOC_SHA_SUPPORT_SHA512_T
|
2020-04-08 04:37:51 -04:00
|
|
|
|
|
|
|
|
|
|
|
/* Hash the input block by block, using non-DMA mode */
|
|
|
|
static void esp_sha_block_mode(esp_sha_type sha_type, const uint8_t *input, uint32_t ilen,
|
2020-08-13 04:30:59 -04:00
|
|
|
const uint8_t *buf, uint32_t buf_len, bool is_first_block)
|
2020-04-08 04:37:51 -04:00
|
|
|
{
|
|
|
|
size_t blk_len = 0;
|
2020-08-13 04:30:59 -04:00
|
|
|
size_t blk_word_len = 0;
|
2020-04-08 04:37:51 -04:00
|
|
|
int num_block = 0;
|
|
|
|
|
|
|
|
blk_len = block_length(sha_type);
|
2020-08-13 04:30:59 -04:00
|
|
|
blk_word_len = blk_len / 4;
|
2020-04-08 04:37:51 -04:00
|
|
|
num_block = ilen / blk_len;
|
|
|
|
|
|
|
|
if (buf_len != 0) {
|
2020-08-13 04:30:59 -04:00
|
|
|
sha_hal_hash_block(sha_type, buf, blk_word_len, is_first_block);
|
2020-04-08 04:37:51 -04:00
|
|
|
is_first_block = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < num_block; i++) {
|
2020-08-13 04:30:59 -04:00
|
|
|
sha_hal_hash_block(sha_type, input + blk_len * i, blk_word_len, is_first_block);
|
2020-04-08 04:37:51 -04:00
|
|
|
is_first_block = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-16 01:31:10 -05:00
|
|
|
|
|
|
|
static int esp_sha_dma_process(esp_sha_type sha_type, const void *input, uint32_t ilen,
|
2020-08-13 04:30:59 -04:00
|
|
|
const void *buf, uint32_t buf_len, bool is_first_block);
|
2020-01-16 01:31:10 -05:00
|
|
|
|
|
|
|
/* Performs SHA on multiple blocks at a time using DMA
|
|
|
|
splits up into smaller operations for inputs that exceed a single DMA list
|
|
|
|
*/
|
|
|
|
int esp_sha_dma(esp_sha_type sha_type, const void *input, uint32_t ilen,
|
|
|
|
const void *buf, uint32_t buf_len, bool is_first_block)
|
2019-12-26 02:25:24 -05:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2020-04-08 04:37:51 -04:00
|
|
|
unsigned char *dma_cap_buf = NULL;
|
2020-08-13 04:30:59 -04:00
|
|
|
int dma_op_num = ( ilen / (SOC_SHA_DMA_MAX_BUFFER_SIZE + 1) ) + 1;
|
2019-12-26 02:25:24 -05:00
|
|
|
|
2020-04-08 04:37:51 -04:00
|
|
|
if (buf_len > block_length(sha_type)) {
|
2020-01-16 01:31:10 -05:00
|
|
|
ESP_LOGE(TAG, "SHA DMA buf_len cannot exceed max size for a single block");
|
|
|
|
return -1;
|
2019-12-26 02:25:24 -05:00
|
|
|
}
|
|
|
|
|
2020-08-13 04:30:59 -04:00
|
|
|
/* DMA cannot access memory in flash, hash block by block instead of using DMA */
|
2020-04-08 04:37:51 -04:00
|
|
|
if (!esp_ptr_dma_ext_capable(input) && !esp_ptr_dma_capable(input) && (ilen != 0)) {
|
|
|
|
esp_sha_block_mode(sha_type, input, ilen, buf, buf_len, is_first_block);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-13 04:30:59 -04:00
|
|
|
#if (CONFIG_SPIRAM_USE_CAPS_ALLOC || CONFIG_SPIRAM_USE_MALLOC)
|
2020-04-08 04:37:51 -04:00
|
|
|
if (esp_ptr_external_ram(input)) {
|
|
|
|
Cache_WriteBack_Addr((uint32_t)input, ilen);
|
|
|
|
}
|
|
|
|
if (esp_ptr_external_ram(buf)) {
|
|
|
|
Cache_WriteBack_Addr((uint32_t)buf, buf_len);
|
2020-01-16 01:31:10 -05:00
|
|
|
}
|
|
|
|
#endif
|
2019-05-09 23:34:06 -04:00
|
|
|
|
2020-04-08 04:37:51 -04:00
|
|
|
/* Copy to internal buf if buf is in non DMA capable memory */
|
2020-03-16 07:29:59 -04:00
|
|
|
if (!esp_ptr_dma_ext_capable(buf) && !esp_ptr_dma_capable(buf) && (buf_len != 0)) {
|
2020-11-17 23:22:47 -05:00
|
|
|
dma_cap_buf = heap_caps_malloc(sizeof(unsigned char) * buf_len, MALLOC_CAP_8BIT|MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL);
|
2020-04-08 04:37:51 -04:00
|
|
|
if (dma_cap_buf == NULL) {
|
2020-03-16 07:29:59 -04:00
|
|
|
ESP_LOGE(TAG, "Failed to allocate buf memory");
|
2020-04-08 04:37:51 -04:00
|
|
|
ret = -1;
|
2020-01-16 01:31:10 -05:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2020-04-08 04:37:51 -04:00
|
|
|
memcpy(dma_cap_buf, buf, buf_len);
|
|
|
|
buf = dma_cap_buf;
|
2020-03-30 22:54:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-16 01:31:10 -05:00
|
|
|
/* The max amount of blocks in a single hardware operation is 2^6 - 1 = 63
|
|
|
|
Thus we only do a single DMA input list + dma buf list,
|
|
|
|
which is max 3968/64 + 64/64 = 63 blocks */
|
|
|
|
for (int i = 0; i < dma_op_num; i++) {
|
|
|
|
|
2020-08-13 04:30:59 -04:00
|
|
|
int dma_chunk_len = MIN(ilen, SOC_SHA_DMA_MAX_BUFFER_SIZE);
|
2020-01-16 01:31:10 -05:00
|
|
|
|
2020-04-08 04:37:51 -04:00
|
|
|
ret = esp_sha_dma_process(sha_type, input, dma_chunk_len, buf, buf_len, is_first_block);
|
2020-01-16 01:31:10 -05:00
|
|
|
|
|
|
|
if (ret != 0) {
|
2020-03-30 22:54:22 -04:00
|
|
|
goto cleanup;
|
2020-01-16 01:31:10 -05:00
|
|
|
}
|
2019-12-26 02:25:24 -05:00
|
|
|
|
2020-01-16 01:31:10 -05:00
|
|
|
ilen -= dma_chunk_len;
|
|
|
|
input += dma_chunk_len;
|
2019-12-26 02:25:24 -05:00
|
|
|
|
2020-01-16 01:31:10 -05:00
|
|
|
// Only append buf to the first operation
|
|
|
|
buf_len = 0;
|
2020-04-08 04:37:51 -04:00
|
|
|
is_first_block = false;
|
2019-12-26 02:25:24 -05:00
|
|
|
}
|
|
|
|
|
2020-01-16 01:31:10 -05:00
|
|
|
cleanup:
|
2020-04-08 04:37:51 -04:00
|
|
|
free(dma_cap_buf);
|
2020-01-16 01:31:10 -05:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Performs SHA on multiple blocks at a time */
|
|
|
|
static esp_err_t esp_sha_dma_process(esp_sha_type sha_type, const void *input, uint32_t ilen,
|
2020-08-13 04:30:59 -04:00
|
|
|
const void *buf, uint32_t buf_len, bool is_first_block)
|
2020-01-16 01:31:10 -05:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
lldesc_t dma_descr_input = {};
|
|
|
|
lldesc_t dma_descr_buf = {};
|
|
|
|
lldesc_t *dma_descr_head;
|
2020-08-13 04:30:59 -04:00
|
|
|
size_t num_blks = (ilen + buf_len) / block_length(sha_type);
|
2020-01-16 01:31:10 -05:00
|
|
|
|
|
|
|
/* DMA descriptor for Memory to DMA-SHA transfer */
|
|
|
|
if (ilen) {
|
|
|
|
dma_descr_input.length = ilen;
|
|
|
|
dma_descr_input.size = ilen;
|
|
|
|
dma_descr_input.owner = 1;
|
|
|
|
dma_descr_input.eof = 1;
|
2020-08-13 04:30:59 -04:00
|
|
|
dma_descr_input.buf = (uint8_t *)input;
|
2020-01-16 01:31:10 -05:00
|
|
|
dma_descr_head = &dma_descr_input;
|
2019-12-26 02:25:24 -05:00
|
|
|
}
|
2020-01-16 01:31:10 -05:00
|
|
|
/* Check after input to overide head if there is any buf*/
|
|
|
|
if (buf_len) {
|
|
|
|
dma_descr_buf.length = buf_len;
|
|
|
|
dma_descr_buf.size = buf_len;
|
|
|
|
dma_descr_buf.owner = 1;
|
|
|
|
dma_descr_buf.eof = 1;
|
2020-08-13 04:30:59 -04:00
|
|
|
dma_descr_buf.buf = (uint8_t *)buf;
|
2020-01-16 01:31:10 -05:00
|
|
|
dma_descr_head = &dma_descr_buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Link DMA lists */
|
|
|
|
if (buf_len && ilen) {
|
|
|
|
dma_descr_buf.eof = 0;
|
|
|
|
dma_descr_buf.empty = (uint32_t)(&dma_descr_input);
|
|
|
|
}
|
|
|
|
|
2020-08-13 04:30:59 -04:00
|
|
|
sha_hal_hash_dma(sha_type, dma_descr_head, num_blks, is_first_block);
|
2019-12-26 02:25:24 -05:00
|
|
|
|
2020-11-17 23:22:47 -05:00
|
|
|
sha_hal_wait_idle();
|
2019-05-09 23:34:06 -04:00
|
|
|
|
2019-12-26 02:25:24 -05:00
|
|
|
return ret;
|
2019-05-09 23:34:06 -04:00
|
|
|
}
|