2016-08-08 05:29:28 -04:00
|
|
|
/*
|
2016-09-02 04:36:26 -04:00
|
|
|
* ESP32 hardware accelerated SHA1/256/512 implementation
|
|
|
|
* based on mbedTLS FIPS-197 compliant version.
|
2016-08-08 05:29:28 -04:00
|
|
|
*
|
|
|
|
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
2016-09-02 04:36:26 -04:00
|
|
|
* Additions Copyright (C) 2016, Espressif Systems (Shanghai) PTE Ltd
|
2016-08-08 05:29:28 -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>
|
2016-11-20 00:29:29 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <byteswap.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2018-12-20 23:37:57 -05:00
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/semphr.h"
|
|
|
|
|
2016-09-02 04:36:26 -04:00
|
|
|
#include "hwcrypto/sha.h"
|
|
|
|
#include "rom/ets_sys.h"
|
2016-11-20 00:29:29 -05:00
|
|
|
#include "soc/dport_reg.h"
|
|
|
|
#include "soc/hwcrypto_reg.h"
|
2018-10-29 11:55:02 -04:00
|
|
|
#include "driver/periph_ctrl.h"
|
2016-08-08 05:29:28 -04:00
|
|
|
|
2016-11-20 00:29:29 -05:00
|
|
|
inline static uint32_t SHA_LOAD_REG(esp_sha_type sha_type) {
|
|
|
|
return SHA_1_LOAD_REG + sha_type * 0x10;
|
|
|
|
}
|
2016-08-08 05:29:28 -04:00
|
|
|
|
2016-11-20 00:29:29 -05:00
|
|
|
inline static uint32_t SHA_BUSY_REG(esp_sha_type sha_type) {
|
|
|
|
return SHA_1_BUSY_REG + sha_type * 0x10;
|
|
|
|
}
|
2016-08-08 05:29:28 -04:00
|
|
|
|
2016-11-20 00:29:29 -05:00
|
|
|
inline static uint32_t SHA_START_REG(esp_sha_type sha_type) {
|
|
|
|
return SHA_1_START_REG + sha_type * 0x10;
|
2016-09-02 04:36:26 -04:00
|
|
|
}
|
|
|
|
|
2016-11-20 00:29:29 -05:00
|
|
|
inline static uint32_t SHA_CONTINUE_REG(esp_sha_type sha_type) {
|
|
|
|
return SHA_1_CONTINUE_REG + sha_type * 0x10;
|
2016-09-02 04:36:26 -04:00
|
|
|
}
|
|
|
|
|
2018-12-21 00:16:16 -05:00
|
|
|
/* Single spinlock for SHA engine memory block
|
2016-11-20 00:29:29 -05:00
|
|
|
*/
|
2018-12-21 00:16:16 -05:00
|
|
|
static portMUX_TYPE memory_block_lock = portMUX_INITIALIZER_UNLOCKED;
|
2016-11-20 00:29:29 -05:00
|
|
|
|
|
|
|
|
2018-12-20 23:37:57 -05:00
|
|
|
/* Binary semaphore managing the state of each concurrent SHA engine.
|
|
|
|
|
|
|
|
Available = noone is using this SHA engine
|
|
|
|
Taken = a SHA session is running on this SHA engine
|
2016-11-20 00:29:29 -05:00
|
|
|
|
|
|
|
Indexes:
|
|
|
|
0 = SHA1
|
|
|
|
1 = SHA2_256
|
|
|
|
2 = SHA2_384 or SHA2_512
|
|
|
|
*/
|
2018-12-20 23:37:57 -05:00
|
|
|
static SemaphoreHandle_t engine_states[3];
|
|
|
|
|
|
|
|
static uint8_t engines_in_use;
|
|
|
|
|
2018-12-21 00:16:16 -05:00
|
|
|
/* Spinlock for engines_in_use counter
|
2018-12-20 23:37:57 -05:00
|
|
|
*/
|
2018-12-21 00:16:16 -05:00
|
|
|
static portMUX_TYPE engines_in_use_lock = portMUX_INITIALIZER_UNLOCKED;
|
2016-11-20 00:29:29 -05:00
|
|
|
|
2018-12-20 23:37:57 -05:00
|
|
|
/* Index into the engine_states array */
|
2016-11-20 00:29:29 -05:00
|
|
|
inline static size_t sha_engine_index(esp_sha_type type) {
|
|
|
|
switch(type) {
|
|
|
|
case SHA1:
|
|
|
|
return 0;
|
|
|
|
case SHA2_256:
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
return 2;
|
2016-09-05 21:05:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-25 03:07:19 -05:00
|
|
|
/* Return digest length (in bytes) for a given SHA type */
|
2016-11-20 00:29:29 -05:00
|
|
|
inline static size_t sha_length(esp_sha_type type) {
|
|
|
|
switch(type) {
|
|
|
|
case SHA1:
|
|
|
|
return 20;
|
|
|
|
case SHA2_256:
|
|
|
|
return 32;
|
|
|
|
case SHA2_384:
|
2016-11-25 03:07:19 -05:00
|
|
|
return 48;
|
2016-11-20 00:29:29 -05:00
|
|
|
case SHA2_512:
|
|
|
|
return 64;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
2016-08-08 05:29:28 -04:00
|
|
|
}
|
|
|
|
|
2016-11-20 00:29:29 -05:00
|
|
|
/* Return block size (in bytes) for a given SHA type */
|
|
|
|
inline static size_t block_length(esp_sha_type type) {
|
|
|
|
switch(type) {
|
|
|
|
case SHA1:
|
|
|
|
case SHA2_256:
|
|
|
|
return 64;
|
|
|
|
case SHA2_384:
|
|
|
|
case SHA2_512:
|
|
|
|
return 128;
|
|
|
|
default:
|
|
|
|
return 0;
|
2016-09-01 23:31:38 -04:00
|
|
|
}
|
2016-08-08 05:29:28 -04:00
|
|
|
}
|
|
|
|
|
2016-11-20 00:29:29 -05:00
|
|
|
void esp_sha_lock_memory_block(void)
|
2016-08-08 05:29:28 -04:00
|
|
|
{
|
2018-12-21 00:16:16 -05:00
|
|
|
portENTER_CRITICAL(&memory_block_lock);
|
2016-08-08 05:29:28 -04:00
|
|
|
}
|
|
|
|
|
2016-11-20 00:29:29 -05:00
|
|
|
void esp_sha_unlock_memory_block(void)
|
2016-08-08 05:29:28 -04:00
|
|
|
{
|
2018-12-21 00:16:16 -05:00
|
|
|
portEXIT_CRITICAL(&memory_block_lock);
|
2016-08-08 05:29:28 -04:00
|
|
|
}
|
|
|
|
|
2018-12-20 23:37:57 -05:00
|
|
|
static SemaphoreHandle_t sha_get_engine_state(esp_sha_type sha_type)
|
|
|
|
{
|
|
|
|
unsigned idx = sha_engine_index(sha_type);
|
|
|
|
volatile SemaphoreHandle_t *engine = &engine_states[idx];
|
|
|
|
SemaphoreHandle_t result = *engine;
|
|
|
|
|
|
|
|
if (result == NULL) {
|
|
|
|
// Create a new semaphore for 'in use' flag
|
|
|
|
SemaphoreHandle_t new_engine = xSemaphoreCreateBinary();
|
|
|
|
assert(new_engine != NULL);
|
|
|
|
xSemaphoreGive(new_engine); // start available
|
|
|
|
|
|
|
|
// try to atomically set the previously NULL *engine to new_engine
|
|
|
|
uint32_t set_engine = (uint32_t)new_engine;
|
|
|
|
uxPortCompareSet((volatile uint32_t *)engine, 0, &set_engine);
|
|
|
|
|
|
|
|
if (set_engine != 0) { // we lost a race setting *engine
|
|
|
|
vSemaphoreDelete(new_engine);
|
|
|
|
}
|
|
|
|
result = *engine;
|
|
|
|
}
|
|
|
|
return result;
|
2016-08-08 05:29:28 -04:00
|
|
|
}
|
|
|
|
|
2018-12-20 23:37:57 -05:00
|
|
|
static bool esp_sha_lock_engine_common(esp_sha_type sha_type, TickType_t ticks_to_wait);
|
2016-11-22 04:56:36 -05:00
|
|
|
|
2016-11-20 00:29:29 -05:00
|
|
|
bool esp_sha_try_lock_engine(esp_sha_type sha_type)
|
2016-08-08 05:29:28 -04:00
|
|
|
{
|
2018-12-20 23:37:57 -05:00
|
|
|
return esp_sha_lock_engine_common(sha_type, 0);
|
2016-11-22 04:56:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void esp_sha_lock_engine(esp_sha_type sha_type)
|
|
|
|
{
|
2018-12-20 23:37:57 -05:00
|
|
|
esp_sha_lock_engine_common(sha_type, portMAX_DELAY);
|
2016-11-22 04:56:36 -05:00
|
|
|
}
|
2016-08-08 05:29:28 -04:00
|
|
|
|
2018-12-20 23:37:57 -05:00
|
|
|
static bool esp_sha_lock_engine_common(esp_sha_type sha_type, TickType_t ticks_to_wait)
|
2016-11-22 04:56:36 -05:00
|
|
|
{
|
2018-12-20 23:37:57 -05:00
|
|
|
SemaphoreHandle_t engine_state = sha_get_engine_state(sha_type);
|
|
|
|
BaseType_t result = xSemaphoreTake(engine_state, ticks_to_wait);
|
|
|
|
|
|
|
|
if (result == pdFALSE) {
|
|
|
|
// failed to take semaphore
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-21 00:16:16 -05:00
|
|
|
portENTER_CRITICAL(&engines_in_use_lock);
|
2016-08-08 05:29:28 -04:00
|
|
|
|
2018-12-20 23:37:57 -05:00
|
|
|
if (engines_in_use == 0) {
|
|
|
|
/* Just locked first engine,
|
|
|
|
so enable SHA hardware */
|
2018-10-29 11:55:02 -04:00
|
|
|
periph_module_enable(PERIPH_SHA_MODULE);
|
2016-09-01 23:31:38 -04:00
|
|
|
}
|
2016-08-08 05:29:28 -04:00
|
|
|
|
2018-12-20 23:37:57 -05:00
|
|
|
engines_in_use++;
|
|
|
|
assert(engines_in_use <= 3);
|
|
|
|
|
2018-12-21 00:16:16 -05:00
|
|
|
portEXIT_CRITICAL(&engines_in_use_lock);
|
2017-08-17 01:48:43 -04:00
|
|
|
|
2018-12-20 23:37:57 -05:00
|
|
|
return true;
|
2016-08-08 05:29:28 -04:00
|
|
|
}
|
|
|
|
|
2016-11-22 04:56:36 -05:00
|
|
|
|
2016-11-20 00:29:29 -05:00
|
|
|
void esp_sha_unlock_engine(esp_sha_type sha_type)
|
2016-08-08 05:29:28 -04:00
|
|
|
{
|
2018-12-20 23:37:57 -05:00
|
|
|
SemaphoreHandle_t *engine_state = sha_get_engine_state(sha_type);
|
2016-08-08 05:29:28 -04:00
|
|
|
|
2018-12-21 00:16:16 -05:00
|
|
|
portENTER_CRITICAL(&engines_in_use_lock);
|
2016-11-20 00:29:29 -05:00
|
|
|
|
2018-12-20 23:37:57 -05:00
|
|
|
engines_in_use--;
|
2016-11-20 00:29:29 -05:00
|
|
|
|
2018-12-20 23:37:57 -05:00
|
|
|
if (engines_in_use == 0) {
|
|
|
|
/* About to release last engine, so
|
|
|
|
disable SHA hardware */
|
2018-10-29 11:55:02 -04:00
|
|
|
periph_module_disable(PERIPH_SHA_MODULE);
|
2016-09-09 00:27:34 -04:00
|
|
|
}
|
2016-11-20 00:29:29 -05:00
|
|
|
|
2018-12-21 00:16:16 -05:00
|
|
|
portEXIT_CRITICAL(&engines_in_use_lock);
|
2016-11-20 00:29:29 -05:00
|
|
|
|
2018-12-20 23:37:57 -05:00
|
|
|
xSemaphoreGive(engine_state);
|
2016-08-08 05:29:28 -04:00
|
|
|
}
|
|
|
|
|
2016-11-20 00:29:29 -05:00
|
|
|
void esp_sha_wait_idle(void)
|
2016-08-08 05:29:28 -04:00
|
|
|
{
|
2017-08-15 22:15:37 -04:00
|
|
|
while(1) {
|
2018-03-22 08:39:59 -04:00
|
|
|
if(DPORT_REG_READ(SHA_1_BUSY_REG) == 0
|
|
|
|
&& DPORT_REG_READ(SHA_256_BUSY_REG) == 0
|
|
|
|
&& DPORT_REG_READ(SHA_384_BUSY_REG) == 0
|
|
|
|
&& DPORT_REG_READ(SHA_512_BUSY_REG) == 0) {
|
2017-08-15 22:15:37 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-08-08 05:29:28 -04:00
|
|
|
}
|
|
|
|
|
2016-11-20 00:29:29 -05:00
|
|
|
void esp_sha_read_digest_state(esp_sha_type sha_type, void *digest_state)
|
2016-08-08 05:29:28 -04:00
|
|
|
{
|
2018-12-20 23:37:57 -05:00
|
|
|
#ifndef NDEBUG
|
|
|
|
{
|
|
|
|
SemaphoreHandle_t *engine_state = sha_get_engine_state(sha_type);
|
|
|
|
assert(uxSemaphoreGetCount(engine_state) == 0 &&
|
|
|
|
"SHA engine should be locked" );
|
|
|
|
}
|
|
|
|
#endif
|
2016-08-08 05:29:28 -04:00
|
|
|
|
2018-12-21 00:16:16 -05:00
|
|
|
// preemptively do this before entering the critical section, then re-check once in it
|
|
|
|
esp_sha_wait_idle();
|
|
|
|
|
2016-11-20 00:29:29 -05:00
|
|
|
esp_sha_lock_memory_block();
|
2016-08-08 05:29:28 -04:00
|
|
|
|
2018-03-22 08:39:59 -04:00
|
|
|
esp_sha_wait_idle();
|
|
|
|
|
|
|
|
DPORT_REG_WRITE(SHA_LOAD_REG(sha_type), 1);
|
|
|
|
while(DPORT_REG_READ(SHA_BUSY_REG(sha_type)) == 1) { }
|
|
|
|
uint32_t *digest_state_words = (uint32_t *)digest_state;
|
|
|
|
uint32_t *reg_addr_buf = (uint32_t *)(SHA_TEXT_BASE);
|
|
|
|
if(sha_type == SHA2_384 || sha_type == SHA2_512) {
|
|
|
|
/* for these ciphers using 64-bit states, swap each pair of words */
|
|
|
|
DPORT_INTERRUPT_DISABLE(); // Disable interrupt only on current CPU.
|
|
|
|
for(int i = 0; i < sha_length(sha_type)/4; i += 2) {
|
|
|
|
digest_state_words[i+1] = DPORT_SEQUENCE_REG_READ((uint32_t)®_addr_buf[i]);
|
|
|
|
digest_state_words[i] = DPORT_SEQUENCE_REG_READ((uint32_t)®_addr_buf[i+1]);
|
2016-11-20 00:29:29 -05:00
|
|
|
}
|
2018-03-22 08:39:59 -04:00
|
|
|
DPORT_INTERRUPT_RESTORE(); // restore the previous interrupt level
|
|
|
|
} else {
|
|
|
|
esp_dport_access_read_buffer(digest_state_words, (uint32_t)®_addr_buf[0], sha_length(sha_type)/4);
|
2016-09-01 23:31:38 -04:00
|
|
|
}
|
2016-11-20 00:29:29 -05:00
|
|
|
esp_sha_unlock_memory_block();
|
2016-08-08 05:29:28 -04:00
|
|
|
}
|
|
|
|
|
2016-11-20 00:29:29 -05:00
|
|
|
void esp_sha_block(esp_sha_type sha_type, const void *data_block, bool is_first_block)
|
2016-08-08 05:29:28 -04:00
|
|
|
{
|
2018-12-20 23:37:57 -05:00
|
|
|
#ifndef NDEBUG
|
|
|
|
{
|
|
|
|
SemaphoreHandle_t *engine_state = sha_get_engine_state(sha_type);
|
|
|
|
assert(uxSemaphoreGetCount(engine_state) == 0 &&
|
|
|
|
"SHA engine should be locked" );
|
|
|
|
}
|
|
|
|
#endif
|
2016-08-08 05:29:28 -04:00
|
|
|
|
2018-12-21 00:16:16 -05:00
|
|
|
// preemptively do this before entering the critical section, then re-check once in it
|
|
|
|
esp_sha_wait_idle();
|
|
|
|
|
2016-11-20 00:29:29 -05:00
|
|
|
esp_sha_lock_memory_block();
|
|
|
|
|
|
|
|
esp_sha_wait_idle();
|
|
|
|
|
|
|
|
/* Fill the data block */
|
|
|
|
uint32_t *reg_addr_buf = (uint32_t *)(SHA_TEXT_BASE);
|
|
|
|
uint32_t *data_words = (uint32_t *)data_block;
|
|
|
|
for (int i = 0; i < block_length(sha_type) / 4; i++) {
|
|
|
|
reg_addr_buf[i] = __bswap_32(data_words[i]);
|
|
|
|
}
|
|
|
|
asm volatile ("memw");
|
|
|
|
|
|
|
|
if(is_first_block) {
|
2017-08-15 22:15:37 -04:00
|
|
|
DPORT_REG_WRITE(SHA_START_REG(sha_type), 1);
|
2016-09-01 23:31:38 -04:00
|
|
|
} else {
|
2017-08-15 22:15:37 -04:00
|
|
|
DPORT_REG_WRITE(SHA_CONTINUE_REG(sha_type), 1);
|
2016-08-08 05:29:28 -04:00
|
|
|
}
|
|
|
|
|
2016-11-20 00:29:29 -05:00
|
|
|
esp_sha_unlock_memory_block();
|
2016-08-08 05:29:28 -04:00
|
|
|
|
2016-11-20 00:29:29 -05:00
|
|
|
/* Note: deliberately not waiting for this operation to complete,
|
|
|
|
as a performance tweak - delay waiting until the next time we need the SHA
|
|
|
|
unit, instead.
|
|
|
|
*/
|
2016-08-08 05:29:28 -04:00
|
|
|
}
|
|
|
|
|
2016-11-20 00:29:29 -05:00
|
|
|
void esp_sha(esp_sha_type sha_type, const unsigned char *input, size_t ilen, unsigned char *output)
|
2016-08-08 05:29:28 -04:00
|
|
|
{
|
2016-11-20 00:29:29 -05:00
|
|
|
size_t block_len = block_length(sha_type);
|
2016-08-08 05:29:28 -04:00
|
|
|
|
2019-03-11 03:24:32 -04:00
|
|
|
// Max number of blocks to pass per each call to esp_sha_lock_memory_block()
|
|
|
|
// (keep low enough to avoid starving interrupt handlers, especially if reading
|
|
|
|
// data into SHA via flash cache, but high enough that spinlock overhead is low)
|
|
|
|
const size_t BLOCKS_PER_CHUNK = 100;
|
|
|
|
const size_t MAX_CHUNK_LEN = BLOCKS_PER_CHUNK * block_len;
|
|
|
|
|
2016-11-22 04:56:36 -05:00
|
|
|
esp_sha_lock_engine(sha_type);
|
2016-08-08 05:29:28 -04:00
|
|
|
|
2016-11-20 00:29:29 -05:00
|
|
|
SHA_CTX ctx;
|
|
|
|
ets_sha_init(&ctx);
|
2019-03-08 00:14:15 -05:00
|
|
|
|
2019-03-11 03:24:32 -04:00
|
|
|
while (ilen > 0) {
|
|
|
|
size_t chunk_len = (ilen > MAX_CHUNK_LEN) ? MAX_CHUNK_LEN : ilen;
|
2019-03-08 00:14:15 -05:00
|
|
|
|
|
|
|
// Wait for idle before entering critical section
|
2019-03-11 03:24:32 -04:00
|
|
|
// (to reduce time spent in it), then check again after
|
2019-03-08 00:14:15 -05:00
|
|
|
esp_sha_wait_idle();
|
|
|
|
esp_sha_lock_memory_block();
|
2016-11-20 00:29:29 -05:00
|
|
|
esp_sha_wait_idle();
|
2019-03-08 00:14:15 -05:00
|
|
|
|
2017-08-15 22:15:37 -04:00
|
|
|
DPORT_STALL_OTHER_CPU_START();
|
2019-03-11 03:24:32 -04:00
|
|
|
while (chunk_len > 0) {
|
2017-08-15 22:15:37 -04:00
|
|
|
// This SHA ROM function reads DPORT regs
|
2019-03-11 03:24:32 -04:00
|
|
|
// (can accept max one SHA block each call)
|
|
|
|
size_t update_len = (chunk_len > block_len) ? block_len : chunk_len;
|
|
|
|
ets_sha_update(&ctx, sha_type, input, update_len * 8);
|
|
|
|
|
|
|
|
input += update_len;
|
|
|
|
chunk_len -= update_len;
|
|
|
|
ilen -= update_len;
|
2017-08-15 22:15:37 -04:00
|
|
|
}
|
|
|
|
DPORT_STALL_OTHER_CPU_END();
|
2019-03-08 00:14:15 -05:00
|
|
|
|
|
|
|
if (ilen == 0) {
|
|
|
|
/* Finish the last block before releasing the memory
|
|
|
|
block lock, as ets_sha_update() may have written data to
|
|
|
|
the memory block already (partial last block) and hardware
|
|
|
|
hasn't yet processed it.
|
|
|
|
*/
|
|
|
|
DPORT_STALL_OTHER_CPU_START();
|
|
|
|
{
|
|
|
|
// This SHA ROM function also reads DPORT regs
|
|
|
|
ets_sha_finish(&ctx, sha_type, output);
|
|
|
|
}
|
|
|
|
DPORT_STALL_OTHER_CPU_END();
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_sha_unlock_memory_block();
|
2016-11-20 00:29:29 -05:00
|
|
|
}
|
2016-08-08 05:29:28 -04:00
|
|
|
|
2016-11-20 00:29:29 -05:00
|
|
|
esp_sha_unlock_engine(sha_type);
|
|
|
|
}
|