esp-idf/components/mbedtls/port/esp32s2/sha.c

362 lines
10 KiB
C
Raw Normal View History

2019-05-09 23:34:06 -04:00
/*
* ESP32 hardware accelerated SHA1/256/512 implementation
* based on mbedTLS FIPS-197 compliant version.
*
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
* 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>
#include "esp_log.h"
2020-02-07 07:08:34 -05:00
#include "esp_crypto_lock.h"
#include "esp32s2/rom/cache.h"
#include "esp32s2/rom/lldesc.h"
2020-01-16 22:47:08 -05:00
#include "esp32s2/rom/ets_sys.h"
2020-02-07 07:08:34 -05:00
#include "soc/crypto_dma_reg.h"
2019-05-09 23:34:06 -04:00
#include "soc/dport_reg.h"
#include "soc/hwcrypto_reg.h"
#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
#include "driver/periph_ctrl.h"
#include "sys/param.h"
2020-02-07 07:08:34 -05:00
#include "esp32s2/sha.h"
/* Max amount of bytes in a single DMA operation is 4095,
for SHA this means that the biggest safe amount of bytes is
31 blocks of 128 bytes = 3968
2019-05-09 23:34:06 -04:00
*/
#define SHA_DMA_MAX_BYTES 3968
const static char *TAG = "esp-sha";
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;
}
}
/* Return state size (in bytes) for a given SHA type */
2019-12-26 02:25:24 -05:00
inline static size_t state_length(esp_sha_type type)
{
switch (type) {
2019-05-09 23:34:06 -04:00
case SHA1:
2019-12-26 02:25:24 -05:00
return 160 / 8;
2019-05-09 23:34:06 -04:00
case SHA2_224:
case SHA2_256:
2019-12-26 02:25:24 -05:00
return 256 / 8;
2019-05-09 23:34:06 -04:00
case SHA2_384:
case SHA2_512:
2019-12-26 02:25:24 -05:00
case SHA2_512224:
case SHA2_512256:
case SHA2_512T:
return 512 / 8;
2019-05-09 23:34:06 -04:00
default:
return 0;
}
}
/* Enable SHA peripheral and then lock it */
void esp_sha_acquire_hardware()
2019-05-09 23:34:06 -04:00
{
esp_crypto_dma_lock_acquire();
2019-12-26 02:25:24 -05:00
/* Enable SHA and DMA hardware */
periph_module_enable(PERIPH_SHA_DMA_MODULE);
2019-12-26 02:25:24 -05:00
/* DMA for SHA */
REG_WRITE(CRYPTO_DMA_AES_SHA_SELECT_REG, 1);
2019-05-09 23:34:06 -04:00
}
/* Disable SHA peripheral block and then release it */
void esp_sha_release_hardware()
2019-05-09 23:34:06 -04:00
{
/* Disable SHA and DMA hardware */
periph_module_disable(PERIPH_SHA_DMA_MODULE);
2019-12-26 02:25:24 -05:00
esp_crypto_dma_lock_release();
2019-12-26 02:25:24 -05:00
}
/* Busy wait until SHA is idle */
static void esp_sha_wait_idle(void)
2019-12-26 02:25:24 -05:00
{
while (DPORT_REG_READ(SHA_BUSY_REG) != 0) {
2019-12-26 02:25:24 -05:00
}
}
void esp_sha_write_digest_state(esp_sha_type sha_type, void *digest_state)
2019-05-09 23:34:06 -04:00
{
uint32_t *digest_state_words = (uint32_t *)digest_state;
uint32_t *reg_addr_buf = (uint32_t *)(SHA_H_BASE);
for (int i = 0; i < state_length(sha_type) / 4; i++) {
REG_WRITE(&reg_addr_buf[i], digest_state_words[i]);
}
2019-05-09 23:34:06 -04:00
}
2019-12-26 02:25:24 -05:00
/* Read the SHA digest from hardware */
2019-05-09 23:34:06 -04:00
void esp_sha_read_digest_state(esp_sha_type sha_type, void *digest_state)
{
uint32_t *digest_state_words = (uint32_t *)digest_state;
int word_len = state_length(sha_type) / 4;
esp_dport_access_read_buffer(digest_state_words, SHA_H_BASE, word_len);
/* Fault injection check: verify SHA engine actually ran,
state is not all zeroes.
*/
for (int i = 0; i < word_len; i++) {
if (digest_state_words[i] != 0) {
return;
}
}
abort(); // SHA peripheral returned all zero state, probably due to fault injection
2019-05-09 23:34:06 -04: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
{
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;
}
REG_WRITE(SHA_T_LENGTH_REG, t_len);
REG_WRITE(SHA_T_STRING_REG, t_string);
REG_WRITE(SHA_MODE_REG, SHA2_512T);
REG_WRITE(SHA_START_REG, 1);
esp_sha_wait_idle();
return 0;
2019-12-26 02:25:24 -05:00
}
static int esp_sha_dma_process(esp_sha_type sha_type, const void *input, uint32_t ilen,
const void *buf, uint32_t buf_len, bool is_first_block);
/* 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;
const void *dma_input;
unsigned char *non_icache_input = NULL;
unsigned char *non_icache_buf = NULL;
int dma_op_num;
size_t dma_max_chunk_len = SHA_DMA_MAX_BYTES;
2019-12-26 02:25:24 -05:00
if (buf_len > 128) {
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
}
#if (CONFIG_SPIRAM_USE_CAPS_ALLOC || CONFIG_SPIRAM_USE_MALLOC)
if (esp_ptr_external_ram(input) || esp_ptr_external_ram(buf)) {
Cache_WriteBack_All();
}
#endif
2019-05-09 23:34:06 -04:00
if (!esp_ptr_dma_ext_capable(buf) && !esp_ptr_dma_capable(buf) && (buf_len != 0)) {
non_icache_buf = heap_caps_malloc(sizeof(unsigned char) * buf_len, MALLOC_CAP_DMA);
if (non_icache_buf == NULL) {
ESP_LOGE(TAG, "Failed to allocate buf memory");
ret = ESP_ERR_NO_MEM;
goto cleanup;
}
memcpy(non_icache_buf, buf, buf_len);
buf = non_icache_buf;
2019-12-26 02:25:24 -05:00
}
2019-05-09 23:34:06 -04:00
/* DMA cannot access memory in the iCache range, copy data to temporary buffers before transfer */
if (!esp_ptr_dma_ext_capable(input) && !esp_ptr_dma_capable(input) && (ilen != 0)) {
non_icache_input = heap_caps_malloc(sizeof(unsigned char) * MIN(ilen, dma_max_chunk_len), MALLOC_CAP_DMA);
if (non_icache_input == NULL) {
/* Allocate biggest available heap */
size_t max_alloc_len = heap_caps_get_largest_free_block(MALLOC_CAP_DMA);
dma_max_chunk_len = max_alloc_len - max_alloc_len % block_length(sha_type);
non_icache_input = heap_caps_malloc(sizeof(unsigned char) * MIN(ilen, dma_max_chunk_len), MALLOC_CAP_DMA);
if (non_icache_input == NULL) {
ESP_LOGE(TAG, "Failed to allocate input memory");
ret = ESP_ERR_NO_MEM;
goto cleanup;
}
}
}
/* 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 */
dma_op_num = ( ilen / (dma_max_chunk_len + 1) ) + 1;
for (int i = 0; i < dma_op_num; i++) {
int dma_chunk_len = MIN(ilen, dma_max_chunk_len);
/* Input depends on if it's a temp alloc buffer or supplied by user */
if (non_icache_input != NULL) {
memcpy(non_icache_input, input, dma_chunk_len);
dma_input = non_icache_input;
} else {
dma_input = input;
}
ret = esp_sha_dma_process(sha_type, dma_input, dma_chunk_len, buf, buf_len, is_first_block);
if (ret != 0) {
goto cleanup;
}
2019-12-26 02:25:24 -05:00
is_first_block = false;
ilen -= dma_chunk_len;
input += dma_chunk_len;
2019-12-26 02:25:24 -05:00
// Only append buf to the first operation
buf_len = 0;
2019-12-26 02:25:24 -05:00
}
cleanup:
free(non_icache_input);
free(non_icache_buf);
return ret;
}
static void esp_sha_dma_init(lldesc_t *input)
{
2019-12-26 02:25:24 -05:00
/* Reset DMA */
SET_PERI_REG_MASK(CRYPTO_DMA_CONF0_REG, CONF0_REG_AHBM_RST | CONF0_REG_OUT_RST | CONF0_REG_AHBM_FIFO_RST);
CLEAR_PERI_REG_MASK(CRYPTO_DMA_CONF0_REG, CONF0_REG_AHBM_RST | CONF0_REG_OUT_RST | CONF0_REG_AHBM_FIFO_RST);
/* Set descriptors */
CLEAR_PERI_REG_MASK(CRYPTO_DMA_OUT_LINK_REG, OUT_LINK_REG_OUTLINK_ADDR);
SET_PERI_REG_MASK(CRYPTO_DMA_OUT_LINK_REG, ((uint32_t)(input))&OUT_LINK_REG_OUTLINK_ADDR);
2019-12-26 02:25:24 -05:00
/* Start transfer */
SET_PERI_REG_MASK(CRYPTO_DMA_OUT_LINK_REG, OUT_LINK_REG_OUTLINK_START);
}
/* 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,
const void *buf, uint32_t buf_len, bool is_first_block)
{
size_t blk_len = 0;
int ret = 0;
lldesc_t dma_descr_input = {};
lldesc_t dma_descr_buf = {};
lldesc_t *dma_descr_head;
blk_len = block_length(sha_type);
2019-12-26 02:25:24 -05:00
REG_WRITE(SHA_MODE_REG, sha_type);
REG_WRITE(SHA_BLOCK_NUM_REG, ((ilen + buf_len) / blk_len));
/* 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;
dma_descr_input.buf = input;
dma_descr_head = &dma_descr_input;
2019-12-26 02:25:24 -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;
dma_descr_buf.buf = buf;
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);
}
esp_sha_dma_init(dma_descr_head);
2019-05-09 23:34:06 -04:00
/* Start hashing */
2019-05-09 23:34:06 -04:00
if (is_first_block) {
2019-12-26 02:25:24 -05:00
REG_WRITE(SHA_DMA_START_REG, 1);
2019-05-09 23:34:06 -04:00
} else {
2019-12-26 02:25:24 -05:00
REG_WRITE(SHA_DMA_CONTINUE_REG, 1);
}
esp_sha_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
}