mirror of
https://github.com/espressif/esp-idf.git
synced 2025-11-26 20:53:11 +00:00
crypto: SHA and AES accelerator bring up for S2
Brings up, fixes and enables AES and SHA hardware acceleration. Closes IDF-714 Closes IDF-716
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -223,8 +223,8 @@ static inline void start_op(uint32_t op_reg)
|
||||
*/
|
||||
static inline void wait_op_complete(uint32_t op_reg)
|
||||
{
|
||||
while(DPORT_REG_READ(RSA_QUERY_INTERRUPT_REG) != 1)
|
||||
{ }
|
||||
while (DPORT_REG_READ(RSA_QUERY_INTERRUPT_REG) != 1)
|
||||
{ }
|
||||
|
||||
/* clear the interrupt */
|
||||
DPORT_REG_WRITE(RSA_CLEAR_INTERRUPT_REG, 1);
|
||||
@@ -258,7 +258,7 @@ int esp_mpi_mul_mpi_mod(mbedtls_mpi *Z, const mbedtls_mpi *X, const mbedtls_mpi
|
||||
|
||||
esp_mpi_acquire_hardware();
|
||||
|
||||
DPORT_REG_WRITE(RSA_LENGTH_REG, (num_words-1));
|
||||
DPORT_REG_WRITE(RSA_LENGTH_REG, (num_words - 1));
|
||||
DPORT_REG_WRITE(RSA_M_DASH_REG, (uint32_t)Mprime);
|
||||
|
||||
/* Load M, X, Rinv, Mprime (Mprime is mod 2^32) */
|
||||
@@ -404,17 +404,17 @@ int mbedtls_mpi_mul_mpi( mbedtls_mpi *Z, const mbedtls_mpi *X, const mbedtls_mpi
|
||||
size_t num_words = MAX(x_words, y_words);
|
||||
size_t z_words = x_words + y_words;
|
||||
|
||||
/* Short-circuit eval if either argument is 0 or 1.
|
||||
/* Short-circuit eval if either argument is 0 or 1.
|
||||
|
||||
This is needed as the mpi modular division
|
||||
argument will sometimes call in here when one
|
||||
argument is too large for the hardware unit, but the other
|
||||
argument is zero or one.
|
||||
This is needed as the mpi modular division
|
||||
argument will sometimes call in here when one
|
||||
argument is too large for the hardware unit, but the other
|
||||
argument is zero or one.
|
||||
|
||||
This leaks some timing information, although overall there is a
|
||||
lot less timing variation than a software MPI approach.
|
||||
This leaks some timing information, although overall there is a
|
||||
lot less timing variation than a software MPI approach.
|
||||
*/
|
||||
if (x_bits == 0 || y_bits== 0) {
|
||||
if (x_bits == 0 || y_bits == 0) {
|
||||
mbedtls_mpi_lset(Z, 0);
|
||||
return 0;
|
||||
}
|
||||
@@ -449,7 +449,7 @@ int mbedtls_mpi_mul_mpi( mbedtls_mpi *Z, const mbedtls_mpi *X, const mbedtls_mpi
|
||||
} else {
|
||||
/* Still too long for the hardware unit... */
|
||||
mbedtls_mpi_grow(Z, z_words);
|
||||
if(y_words > x_words) {
|
||||
if (y_words > x_words) {
|
||||
return mpi_mult_mpi_overlong(Z, X, Y, y_words, z_words);
|
||||
} else {
|
||||
return mpi_mult_mpi_overlong(Z, Y, X, x_words, z_words);
|
||||
@@ -468,7 +468,7 @@ int mbedtls_mpi_mul_mpi( mbedtls_mpi *Z, const mbedtls_mpi *X, const mbedtls_mpi
|
||||
*/
|
||||
|
||||
DPORT_REG_WRITE(RSA_M_DASH_REG, 0);
|
||||
DPORT_REG_WRITE(RSA_LENGTH_REG, (num_words*2 - 1));
|
||||
DPORT_REG_WRITE(RSA_LENGTH_REG, (num_words * 2 - 1));
|
||||
start_op(RSA_MULT_START_REG);
|
||||
|
||||
wait_op_complete(RSA_MULT_START_REG);
|
||||
|
||||
255
components/mbedtls/port/esp32s2/esp_sha1.c
Normal file
255
components/mbedtls/port/esp32s2/esp_sha1.c
Normal file
@@ -0,0 +1,255 @@
|
||||
/*
|
||||
* SHA-1 implementation with hardware ESP32 support added.
|
||||
* Uses mbedTLS software implementation for failover when concurrent
|
||||
* SHA operations are in use.
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* Additions Copyright (C) 2016-2020, Espressif Systems (Shanghai) PTE LTD
|
||||
* 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
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_SHA1_ALT)
|
||||
|
||||
#include "mbedtls/sha1.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#if defined(MBEDTLS_SELF_TEST)
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_printf printf
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
#endif /* MBEDTLS_SELF_TEST */
|
||||
|
||||
#include "esp32s2/sha.h"
|
||||
|
||||
/* Implementation that should never be optimized out by the compiler */
|
||||
static void mbedtls_zeroize( void *v, size_t n )
|
||||
{
|
||||
volatile unsigned char *p = (unsigned char *)v; while ( n-- ) *p++ = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* 32-bit integer manipulation macros (big endian)
|
||||
*/
|
||||
|
||||
#ifndef PUT_UINT32_BE
|
||||
#define PUT_UINT32_BE(n,b,i) \
|
||||
{ \
|
||||
(b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
|
||||
(b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
|
||||
(b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
|
||||
(b)[(i) + 3] = (unsigned char) ( (n) ); \
|
||||
}
|
||||
#endif
|
||||
|
||||
void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
|
||||
{
|
||||
memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
|
||||
}
|
||||
|
||||
void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
|
||||
{
|
||||
if ( ctx == NULL ) {
|
||||
return;
|
||||
}
|
||||
|
||||
mbedtls_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
|
||||
}
|
||||
|
||||
void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
|
||||
const mbedtls_sha1_context *src )
|
||||
{
|
||||
memcpy(dst, src, sizeof(mbedtls_sha1_context));
|
||||
}
|
||||
|
||||
/*
|
||||
* SHA-1 context setup
|
||||
*/
|
||||
int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx )
|
||||
{
|
||||
ctx->total[0] = 0;
|
||||
ctx->total[1] = 0;
|
||||
|
||||
memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
|
||||
ctx->mode = SHA1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
|
||||
{
|
||||
mbedtls_sha1_starts_ret( ctx );
|
||||
}
|
||||
#endif
|
||||
|
||||
static int esp_internal_sha1_dma_process(mbedtls_sha1_context *ctx,
|
||||
const uint8_t *data, size_t len,
|
||||
uint8_t *buf, size_t buf_len)
|
||||
{
|
||||
return esp_sha_dma(SHA1, data, len, buf, buf_len, ctx->first_block);
|
||||
}
|
||||
|
||||
int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[64] )
|
||||
{
|
||||
int ret;
|
||||
esp_sha_acquire_hardware();
|
||||
ret = esp_sha_dma(ctx->mode, data, 64, 0, 0, ctx->first_block);
|
||||
esp_sha_release_hardware();
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
|
||||
const unsigned char data[64] )
|
||||
{
|
||||
mbedtls_internal_sha1_process( ctx, data );
|
||||
}
|
||||
#endif
|
||||
|
||||
int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen )
|
||||
{
|
||||
int ret;
|
||||
size_t fill;
|
||||
uint32_t left, len, local_len = 0;
|
||||
|
||||
if ( !ilen || (input == NULL)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
left = ctx->total[0] & 0x3F;
|
||||
fill = 64 - left;
|
||||
|
||||
ctx->total[0] += (uint32_t) ilen;
|
||||
ctx->total[0] &= 0xFFFFFFFF;
|
||||
|
||||
if ( ctx->total[0] < (uint32_t) ilen ) {
|
||||
ctx->total[1]++;
|
||||
}
|
||||
|
||||
if ( left && ilen >= fill ) {
|
||||
memcpy( (void *) (ctx->buffer + left), input, fill );
|
||||
|
||||
input += fill;
|
||||
ilen -= fill;
|
||||
left = 0;
|
||||
local_len = 64;
|
||||
}
|
||||
|
||||
len = (ilen / 64) * 64;
|
||||
if ( len || local_len) {
|
||||
|
||||
esp_sha_acquire_hardware();
|
||||
|
||||
if (ctx->sha_state == ESP_SHA1_STATE_INIT) {
|
||||
ctx->first_block = true;
|
||||
ctx->sha_state = ESP_SHA1_STATE_IN_PROCESS;
|
||||
} else if (ctx->sha_state == ESP_SHA1_STATE_IN_PROCESS) {
|
||||
ctx->first_block = false;
|
||||
esp_sha_write_digest_state(SHA1, ctx->state);
|
||||
}
|
||||
|
||||
ret = esp_internal_sha1_dma_process(ctx, input, len, ctx->buffer, local_len);
|
||||
|
||||
esp_sha_read_digest_state(SHA1, ctx->state);
|
||||
|
||||
esp_sha_release_hardware();
|
||||
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if ( ilen > 0 ) {
|
||||
memcpy( (void *) (ctx->buffer + left), input + len, ilen - len );
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
|
||||
const unsigned char *input,
|
||||
size_t ilen )
|
||||
{
|
||||
mbedtls_sha1_update_ret( ctx, input, ilen );
|
||||
}
|
||||
#endif
|
||||
|
||||
static const unsigned char sha1_padding[64] = {
|
||||
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||
};
|
||||
|
||||
/*
|
||||
* SHA-1 final digest
|
||||
*/
|
||||
int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx, unsigned char output[20] )
|
||||
{
|
||||
int ret;
|
||||
uint32_t last, padn;
|
||||
uint32_t high, low;
|
||||
unsigned char msglen[8];
|
||||
|
||||
high = ( ctx->total[0] >> 29 )
|
||||
| ( ctx->total[1] << 3 );
|
||||
low = ( ctx->total[0] << 3 );
|
||||
|
||||
PUT_UINT32_BE( high, msglen, 0 );
|
||||
PUT_UINT32_BE( low, msglen, 4 );
|
||||
|
||||
last = ctx->total[0] & 0x3F;
|
||||
padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
|
||||
|
||||
|
||||
if ( ( ret = mbedtls_sha1_update_ret( ctx, sha1_padding, padn ) ) != 0 ) {
|
||||
return ret;
|
||||
}
|
||||
if ( ( ret = mbedtls_sha1_update_ret( ctx, msglen, 8 ) ) != 0 ) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
memcpy(output, ctx->state, 20);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
|
||||
unsigned char output[20] )
|
||||
{
|
||||
mbedtls_sha1_finish_ret( ctx, output );
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* MBEDTLS_SHA1_C && MBEDTLS_SHA1_ALT */
|
||||
267
components/mbedtls/port/esp32s2/esp_sha256.c
Normal file
267
components/mbedtls/port/esp32s2/esp_sha256.c
Normal file
@@ -0,0 +1,267 @@
|
||||
/*
|
||||
* SHA-256 implementation with hardware ESP32 support added.
|
||||
* Uses mbedTLS software implementation for failover when concurrent
|
||||
* SHA operations are in use.
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* Additions Copyright (C) 2016-2020, Espressif Systems (Shanghai) PTE LTD
|
||||
* 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-256 Secure Hash Standard was published by NIST in 2002.
|
||||
*
|
||||
* http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SHA256_C) && defined(MBEDTLS_SHA256_ALT)
|
||||
|
||||
#include "mbedtls/sha256.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#if defined(MBEDTLS_SELF_TEST)
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_printf printf
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
#endif /* MBEDTLS_SELF_TEST */
|
||||
|
||||
#include "esp32s2/sha.h"
|
||||
|
||||
/* Implementation that should never be optimized out by the compiler */
|
||||
static void mbedtls_zeroize( void *v, size_t n )
|
||||
{
|
||||
volatile unsigned char *p = v; while ( n-- ) *p++ = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* 32-bit integer manipulation macros (big endian)
|
||||
*/
|
||||
#ifndef GET_UINT32_BE
|
||||
#define GET_UINT32_BE(n,b,i) \
|
||||
do { \
|
||||
(n) = ( (uint32_t) (b)[(i) ] << 24 ) \
|
||||
| ( (uint32_t) (b)[(i) + 1] << 16 ) \
|
||||
| ( (uint32_t) (b)[(i) + 2] << 8 ) \
|
||||
| ( (uint32_t) (b)[(i) + 3] ); \
|
||||
} while( 0 )
|
||||
#endif
|
||||
|
||||
#ifndef PUT_UINT32_BE
|
||||
#define PUT_UINT32_BE(n,b,i) \
|
||||
do { \
|
||||
(b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
|
||||
(b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
|
||||
(b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
|
||||
(b)[(i) + 3] = (unsigned char) ( (n) ); \
|
||||
} while( 0 )
|
||||
#endif
|
||||
|
||||
void mbedtls_sha256_init( mbedtls_sha256_context *ctx )
|
||||
{
|
||||
memset( ctx, 0, sizeof( mbedtls_sha256_context ) );
|
||||
}
|
||||
|
||||
void mbedtls_sha256_free( mbedtls_sha256_context *ctx )
|
||||
{
|
||||
if ( ctx == NULL ) {
|
||||
return;
|
||||
}
|
||||
|
||||
mbedtls_zeroize( ctx, sizeof( mbedtls_sha256_context ) );
|
||||
}
|
||||
|
||||
void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
|
||||
const mbedtls_sha256_context *src )
|
||||
{
|
||||
*dst = *src;
|
||||
}
|
||||
|
||||
/*
|
||||
* SHA-256 context setup
|
||||
*/
|
||||
int mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int is224 )
|
||||
{
|
||||
memset( ctx, 0, sizeof( mbedtls_sha256_context ) );
|
||||
|
||||
if ( is224 ) {
|
||||
ctx->mode = SHA2_224;
|
||||
} else {
|
||||
ctx->mode = SHA2_256;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_sha256_starts( mbedtls_sha256_context *ctx,
|
||||
int is224 )
|
||||
{
|
||||
mbedtls_sha256_starts_ret( ctx, is224 );
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[64] )
|
||||
{
|
||||
int ret;
|
||||
esp_sha_acquire_hardware();
|
||||
ret = esp_sha_dma(ctx->mode, data, 64, 0, 0, ctx->first_block);
|
||||
esp_sha_release_hardware();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_sha256_process( mbedtls_sha256_context *ctx,
|
||||
const unsigned char data[64] )
|
||||
{
|
||||
mbedtls_internal_sha256_process( ctx, data );
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* SHA-256 process buffer
|
||||
*/
|
||||
int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx, const unsigned char *input,
|
||||
size_t ilen )
|
||||
{
|
||||
int ret = 0;
|
||||
size_t fill;
|
||||
uint32_t left, len, local_len = 0;
|
||||
|
||||
if ( ilen == 0 ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
left = ctx->total[0] & 0x3F;
|
||||
fill = 64 - left;
|
||||
|
||||
ctx->total[0] += (uint32_t) ilen;
|
||||
ctx->total[0] &= 0xFFFFFFFF;
|
||||
|
||||
if ( ctx->total[0] < (uint32_t) ilen ) {
|
||||
ctx->total[1]++;
|
||||
}
|
||||
|
||||
/* Check if any data pending from previous call to this API */
|
||||
if ( left && ilen >= fill ) {
|
||||
memcpy( (void *) (ctx->buffer + left), input, fill );
|
||||
|
||||
input += fill;
|
||||
ilen -= fill;
|
||||
left = 0;
|
||||
local_len = 64;
|
||||
}
|
||||
|
||||
len = (ilen / 64) * 64;
|
||||
|
||||
if ( len || local_len) {
|
||||
esp_sha_acquire_hardware();
|
||||
|
||||
if (ctx->sha_state == ESP_SHA256_STATE_INIT) {
|
||||
ctx->first_block = true;
|
||||
ctx->sha_state = ESP_SHA256_STATE_IN_PROCESS;
|
||||
} else if (ctx->sha_state == ESP_SHA256_STATE_IN_PROCESS) {
|
||||
ctx->first_block = false;
|
||||
esp_sha_write_digest_state(ctx->mode, ctx->state);
|
||||
}
|
||||
|
||||
ret = esp_sha_dma(ctx->mode, input, len, ctx->buffer, local_len, ctx->first_block);
|
||||
|
||||
esp_sha_read_digest_state(ctx->mode, ctx->state);
|
||||
|
||||
esp_sha_release_hardware();
|
||||
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ilen > 0 ) {
|
||||
memcpy( (void *) (ctx->buffer + left), input + len, ilen - len );
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_sha256_update( mbedtls_sha256_context *ctx,
|
||||
const unsigned char *input,
|
||||
size_t ilen )
|
||||
{
|
||||
mbedtls_sha256_update_ret( ctx, input, ilen );
|
||||
}
|
||||
#endif
|
||||
|
||||
static const unsigned char sha256_padding[64] = {
|
||||
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||
};
|
||||
|
||||
/*
|
||||
* SHA-256 final digest
|
||||
*/
|
||||
int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx, unsigned char output[32] )
|
||||
{
|
||||
int ret;
|
||||
uint32_t last, padn;
|
||||
uint32_t high, low;
|
||||
unsigned char msglen[8];
|
||||
|
||||
high = ( ctx->total[0] >> 29 )
|
||||
| ( ctx->total[1] << 3 );
|
||||
low = ( ctx->total[0] << 3 );
|
||||
|
||||
PUT_UINT32_BE( high, msglen, 0 );
|
||||
PUT_UINT32_BE( low, msglen, 4 );
|
||||
|
||||
last = ctx->total[0] & 0x3F;
|
||||
padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
|
||||
|
||||
if ( ( ret = mbedtls_sha256_update_ret( ctx, sha256_padding, padn ) ) != 0 ) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
if ( ( ret = mbedtls_sha256_update_ret( ctx, msglen, 8 ) ) != 0 ) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
memcpy(output, ctx->state, 32);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_sha256_finish( mbedtls_sha256_context *ctx,
|
||||
unsigned char output[32] )
|
||||
{
|
||||
mbedtls_sha256_finish_ret( ctx, output );
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* MBEDTLS_SHA256_C && MBEDTLS_SHA256_ALT */
|
||||
317
components/mbedtls/port/esp32s2/esp_sha512.c
Normal file
317
components/mbedtls/port/esp32s2/esp_sha512.c
Normal file
@@ -0,0 +1,317 @@
|
||||
/*
|
||||
* SHA-512 implementation with hardware ESP32 support added.
|
||||
* Uses mbedTLS software implementation for failover when concurrent
|
||||
* SHA operations are in use.
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* Additions Copyright (C) 2016-2020, Espressif Systems (Shanghai) PTE LTD
|
||||
* 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-512 Secure Hash Standard was published by NIST in 2002.
|
||||
*
|
||||
* http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SHA512_C) && defined(MBEDTLS_SHA512_ALT)
|
||||
|
||||
#include "mbedtls/sha512.h"
|
||||
|
||||
#if defined(_MSC_VER) || defined(__WATCOMC__)
|
||||
#define UL64(x) x##ui64
|
||||
#else
|
||||
#define UL64(x) x##ULL
|
||||
#endif
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#if defined(MBEDTLS_SELF_TEST)
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_printf printf
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
#endif /* MBEDTLS_SELF_TEST */
|
||||
|
||||
#include "esp32s2/sha.h"
|
||||
|
||||
/* Implementation that should never be optimized out by the compiler */
|
||||
static void mbedtls_zeroize( void *v, size_t n )
|
||||
{
|
||||
volatile unsigned char *p = v; while ( n-- ) *p++ = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* 64-bit integer manipulation macros (big endian)
|
||||
*/
|
||||
#ifndef PUT_UINT64_BE
|
||||
#define PUT_UINT64_BE(n,b,i) \
|
||||
{ \
|
||||
(b)[(i) ] = (unsigned char) ( (n) >> 56 ); \
|
||||
(b)[(i) + 1] = (unsigned char) ( (n) >> 48 ); \
|
||||
(b)[(i) + 2] = (unsigned char) ( (n) >> 40 ); \
|
||||
(b)[(i) + 3] = (unsigned char) ( (n) >> 32 ); \
|
||||
(b)[(i) + 4] = (unsigned char) ( (n) >> 24 ); \
|
||||
(b)[(i) + 5] = (unsigned char) ( (n) >> 16 ); \
|
||||
(b)[(i) + 6] = (unsigned char) ( (n) >> 8 ); \
|
||||
(b)[(i) + 7] = (unsigned char) ( (n) ); \
|
||||
}
|
||||
#endif /* PUT_UINT64_BE */
|
||||
|
||||
void esp_sha512_set_mode(mbedtls_sha512_context *ctx, esp_sha_type type)
|
||||
{
|
||||
switch (type) {
|
||||
case SHA2_384:
|
||||
case SHA2_512224:
|
||||
case SHA2_512256:
|
||||
case SHA2_512T:
|
||||
ctx->mode = type;
|
||||
break;
|
||||
default:
|
||||
ctx->mode = SHA2_512;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* For SHA512/t mode the intial hash value will depend on t */
|
||||
void esp_sha512_set_t( mbedtls_sha512_context *ctx, uint16_t t_val)
|
||||
{
|
||||
ctx->t_val = t_val;
|
||||
}
|
||||
|
||||
void mbedtls_sha512_init( mbedtls_sha512_context *ctx )
|
||||
{
|
||||
memset( ctx, 0, sizeof( mbedtls_sha512_context ) );
|
||||
}
|
||||
|
||||
void mbedtls_sha512_free( mbedtls_sha512_context *ctx )
|
||||
{
|
||||
if ( ctx == NULL ) {
|
||||
return;
|
||||
}
|
||||
|
||||
mbedtls_zeroize( ctx, sizeof( mbedtls_sha512_context ) );
|
||||
}
|
||||
|
||||
void mbedtls_sha512_clone( mbedtls_sha512_context *dst,
|
||||
const mbedtls_sha512_context *src )
|
||||
{
|
||||
memcpy(dst, src, sizeof(mbedtls_sha512_context));
|
||||
}
|
||||
|
||||
/*
|
||||
* SHA-512 context setup
|
||||
*/
|
||||
int mbedtls_sha512_starts_ret( mbedtls_sha512_context *ctx, int is384 )
|
||||
{
|
||||
mbedtls_zeroize( ctx, sizeof( mbedtls_sha512_context ) );
|
||||
|
||||
if ( is384 ) {
|
||||
ctx->mode = SHA2_384;
|
||||
} else {
|
||||
ctx->mode = SHA2_512;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_sha512_starts( mbedtls_sha512_context *ctx,
|
||||
int is384 )
|
||||
{
|
||||
mbedtls_sha512_starts_ret( ctx, is384 );
|
||||
}
|
||||
#endif
|
||||
|
||||
static int esp_internal_sha512_dma_process(mbedtls_sha512_context *ctx,
|
||||
const uint8_t *data, size_t len,
|
||||
uint8_t *buf, size_t buf_len)
|
||||
{
|
||||
|
||||
|
||||
return esp_sha_dma(ctx->mode, data, len, buf, buf_len, ctx->first_block);
|
||||
|
||||
|
||||
}
|
||||
|
||||
int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx, const unsigned char data[128] )
|
||||
{
|
||||
int ret;
|
||||
esp_sha_acquire_hardware();
|
||||
ret = esp_internal_sha512_dma_process(ctx, data, 128, 0, 0);
|
||||
esp_sha_release_hardware();
|
||||
|
||||
return ret;
|
||||
|
||||
}
|
||||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_sha512_process( mbedtls_sha512_context *ctx,
|
||||
const unsigned char data[128] )
|
||||
{
|
||||
mbedtls_internal_sha512_process( ctx, data );
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* SHA-512 process buffer
|
||||
*/
|
||||
int mbedtls_sha512_update_ret( mbedtls_sha512_context *ctx, const unsigned char *input,
|
||||
size_t ilen )
|
||||
{
|
||||
int ret;
|
||||
size_t fill;
|
||||
unsigned int left, len, local_len = 0;
|
||||
|
||||
if ( ilen == 0 ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
left = (unsigned int) (ctx->total[0] & 0x7F);
|
||||
fill = 128 - left;
|
||||
|
||||
ctx->total[0] += (uint64_t) ilen;
|
||||
|
||||
if ( ctx->total[0] < (uint64_t) ilen ) {
|
||||
ctx->total[1]++;
|
||||
}
|
||||
|
||||
if ( left && ilen >= fill ) {
|
||||
memcpy( (void *) (ctx->buffer + left), input, fill );
|
||||
|
||||
input += fill;
|
||||
ilen -= fill;
|
||||
left = 0;
|
||||
local_len = 128;
|
||||
}
|
||||
|
||||
len = (ilen / 128) * 128;
|
||||
|
||||
if ( len || local_len) {
|
||||
|
||||
esp_sha_acquire_hardware();
|
||||
|
||||
if (ctx->sha_state == ESP_SHA512_STATE_INIT) {
|
||||
|
||||
if (ctx->mode == SHA2_512T) {
|
||||
esp_sha_512_t_init_hash(ctx->t_val);
|
||||
ctx->first_block = false;
|
||||
} else {
|
||||
ctx->first_block = true;
|
||||
}
|
||||
ctx->sha_state = ESP_SHA512_STATE_IN_PROCESS;
|
||||
|
||||
} else if (ctx->sha_state == ESP_SHA512_STATE_IN_PROCESS) {
|
||||
ctx->first_block = false;
|
||||
esp_sha_write_digest_state(ctx->mode, ctx->state);
|
||||
}
|
||||
|
||||
ret = esp_internal_sha512_dma_process(ctx, input, len, ctx->buffer, local_len);
|
||||
|
||||
esp_sha_read_digest_state(ctx->mode, ctx->state);
|
||||
|
||||
esp_sha_release_hardware();
|
||||
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
if ( ilen > 0 ) {
|
||||
memcpy( (void *) (ctx->buffer + left), input + len, ilen - len );
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_sha512_update( mbedtls_sha512_context *ctx,
|
||||
const unsigned char *input,
|
||||
size_t ilen )
|
||||
{
|
||||
mbedtls_sha512_update_ret( ctx, input, ilen );
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
static const unsigned char sha512_padding[128] = {
|
||||
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||
};
|
||||
|
||||
/*
|
||||
* SHA-512 final digest
|
||||
*/
|
||||
int mbedtls_sha512_finish_ret( mbedtls_sha512_context *ctx, unsigned char output[64] )
|
||||
{
|
||||
int ret;
|
||||
size_t last, padn;
|
||||
uint64_t high, low;
|
||||
unsigned char msglen[16];
|
||||
|
||||
high = ( ctx->total[0] >> 61 )
|
||||
| ( ctx->total[1] << 3 );
|
||||
low = ( ctx->total[0] << 3 );
|
||||
|
||||
PUT_UINT64_BE( high, msglen, 0 );
|
||||
PUT_UINT64_BE( low, msglen, 8 );
|
||||
|
||||
last = (size_t)( ctx->total[0] & 0x7F );
|
||||
padn = ( last < 112 ) ? ( 112 - last ) : ( 240 - last );
|
||||
|
||||
if ( ( ret = mbedtls_sha512_update_ret( ctx, sha512_padding, padn ) ) != 0 ) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
if ( ( ret = mbedtls_sha512_update_ret( ctx, msglen, 16 ) ) != 0 ) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (ctx->mode == SHA2_384) {
|
||||
memcpy(output, ctx->state, 48);
|
||||
} else {
|
||||
memcpy(output, ctx->state, 64);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_sha512_finish( mbedtls_sha512_context *ctx,
|
||||
unsigned char output[64] )
|
||||
{
|
||||
mbedtls_sha512_finish_ret( ctx, output );
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* MBEDTLS_SHA512_C && MBEDTLS_SHA512_ALT */
|
||||
@@ -3,7 +3,7 @@
|
||||
* based on mbedTLS FIPS-197 compliant version.
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* Additions Copyright (C) 2016, Espressif Systems (Shanghai) PTE Ltd
|
||||
* Additions Copyright (C) 2016-2020, Espressif Systems (Shanghai) PTE Ltd
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
@@ -28,34 +28,38 @@
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/lock.h>
|
||||
#include <assert.h>
|
||||
#include "soc/soc.h"
|
||||
#include "esp32s2/crypto_dma.h"
|
||||
#include "esp32s2/sha.h"
|
||||
#include "soc/crypto_dma_reg.h"
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "esp32s2/rom/ets_sys.h"
|
||||
#include "soc/dport_reg.h"
|
||||
#include "soc/hwcrypto_reg.h"
|
||||
#include "esp32s2/rom/lldesc.h"
|
||||
|
||||
#include "esp32s2/rom/cache.h"
|
||||
#include "esp_intr_alloc.h"
|
||||
#include "esp_log.h"
|
||||
#include "soc/periph_defs.h"
|
||||
|
||||
#include "soc/cache_memory.h"
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/semphr.h"
|
||||
|
||||
/* Single lock for SHA engine
|
||||
#include "esp32s2/sha.h"
|
||||
#include "esp32s2/crypto_dma.h"
|
||||
#include "esp32s2/rom/lldesc.h"
|
||||
#include "soc/periph_defs.h"
|
||||
#include "soc/crypto_dma_reg.h"
|
||||
#include "driver/periph_ctrl.h"
|
||||
#include "sys/param.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
|
||||
*/
|
||||
#define SHA_DMA_MAX_BYTES 3968
|
||||
|
||||
/* Lock for SHA engine */
|
||||
static _lock_t s_sha_lock;
|
||||
|
||||
/* Enable if want to use SHA interrupt */
|
||||
//#define CONFIG_MBEDTLS_SHA_USE_INTERRUPT
|
||||
|
||||
#if defined(CONFIG_MBEDTLS_SHA_USE_INTERRUPT)
|
||||
static SemaphoreHandle_t op_complete_sem;
|
||||
#endif
|
||||
const static char *TAG = "esp-sha";
|
||||
|
||||
/* Return block size (in bytes) for a given SHA type */
|
||||
inline static size_t block_length(esp_sha_type type)
|
||||
@@ -96,190 +100,265 @@ inline static size_t state_length(esp_sha_type type)
|
||||
}
|
||||
}
|
||||
|
||||
/* This API was designed for ESP32, which has seperate
|
||||
engines for SHA1,256,512. ESP32C has a single engine.
|
||||
*/
|
||||
static void esp_sha_lock_engine_inner(void);
|
||||
|
||||
bool esp_sha_try_lock_engine(esp_sha_type sha_type)
|
||||
{
|
||||
if (_lock_try_acquire(&s_sha_lock) != 0) {
|
||||
/* SHA engine is already in use */
|
||||
return false;
|
||||
} else {
|
||||
esp_sha_lock_engine_inner();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
void esp_sha_lock_engine(esp_sha_type sha_type)
|
||||
{
|
||||
_lock_acquire(&s_sha_lock);
|
||||
esp_sha_lock_engine_inner();
|
||||
}
|
||||
|
||||
/* Enable SHA block and then lock it */
|
||||
static void esp_sha_lock_engine_inner(void)
|
||||
/* Enable SHA peripheral and then lock it */
|
||||
void esp_sha_acquire_hardware()
|
||||
{
|
||||
/* Need to lock DMA since it is shared with AES block */
|
||||
portENTER_CRITICAL(&crypto_dma_spinlock);
|
||||
_lock_acquire(&crypto_dma_lock);
|
||||
_lock_acquire(&s_sha_lock);
|
||||
|
||||
REG_SET_BIT(DPORT_PERIP_CLK_EN1_REG, DPORT_CRYPTO_SHA_CLK_EN | DPORT_CRYPTO_DMA_CLK_EN);
|
||||
REG_CLR_BIT(DPORT_PERIP_RST_EN1_REG, DPORT_CRYPTO_SHA_RST | DPORT_CRYPTO_HMAC_RST |
|
||||
DPORT_CRYPTO_DMA_RST | DPORT_CRYPTO_DS_RST);
|
||||
/* Enable SHA and DMA hardware */
|
||||
periph_module_enable(PERIPH_SHA_DMA_MODULE);
|
||||
|
||||
/* DMA for SHA */
|
||||
REG_WRITE(CRYPTO_DMA_AES_SHA_SELECT_REG, 1);
|
||||
}
|
||||
|
||||
/* Disable SHA block and then unlock it */
|
||||
void esp_sha_unlock_engine(esp_sha_type sha_type)
|
||||
/* Disable SHA peripheral block and then release it */
|
||||
void esp_sha_release_hardware()
|
||||
{
|
||||
REG_WRITE(CRYPTO_DMA_AES_SHA_SELECT_REG, 0);
|
||||
|
||||
REG_SET_BIT(DPORT_PERIP_RST_EN1_REG, DPORT_CRYPTO_SHA_RST | DPORT_CRYPTO_DMA_RST |
|
||||
DPORT_CRYPTO_DS_RST);
|
||||
REG_CLR_BIT(DPORT_PERIP_CLK_EN1_REG, DPORT_CRYPTO_SHA_CLK_EN | DPORT_CRYPTO_DMA_CLK_EN);
|
||||
|
||||
portEXIT_CRITICAL(&crypto_dma_spinlock);
|
||||
/* Disable SHA and DMA hardware */
|
||||
periph_module_disable(PERIPH_SHA_DMA_MODULE);
|
||||
|
||||
/* Need to lock DMA since it is shared with AES block */
|
||||
_lock_release(&s_sha_lock);
|
||||
_lock_release(&crypto_dma_lock);
|
||||
|
||||
}
|
||||
|
||||
#if defined (CONFIG_MBEDTLS_SHA_USE_INTERRUPT)
|
||||
static IRAM_ATTR void esp_sha_dma_isr(void *arg)
|
||||
|
||||
/* Busy wait until SHA is idle */
|
||||
static void esp_sha_wait_idle(void)
|
||||
{
|
||||
BaseType_t higher_woken;
|
||||
REG_WRITE(SHA_CLEAR_IRQ_REG, 1);
|
||||
xSemaphoreGiveFromISR(op_complete_sem, &higher_woken);
|
||||
if (higher_woken) {
|
||||
portYIELD_FROM_ISR();
|
||||
while (DPORT_REG_READ(SHA_BUSY_REG) != 0) {
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Check if SHA operation completed */
|
||||
static int esp_sha_dma_complete(void)
|
||||
|
||||
void esp_sha_write_digest_state(esp_sha_type sha_type, void *digest_state)
|
||||
{
|
||||
#if defined (CONFIG_MBEDTLS_SHA_USE_INTERRUPT)
|
||||
if (!xSemaphoreTake(op_complete_sem, 2000 / portTICK_PERIOD_MS)) {
|
||||
ESP_LOGE("SHA", "Timed out waiting for completion of SHA Interrupt");
|
||||
return -1;
|
||||
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(®_addr_buf[i], digest_state_words[i]);
|
||||
}
|
||||
#else
|
||||
esp_sha_wait_idle();
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Wait until SHA is busy */
|
||||
void esp_sha_wait_idle(void)
|
||||
{
|
||||
while (DPORT_REG_READ(SHA_BUSY_REG) != 0) { }
|
||||
}
|
||||
|
||||
/* Read the SHA digest from hardware */
|
||||
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
|
||||
}
|
||||
|
||||
/* 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)
|
||||
{
|
||||
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();
|
||||
memcpy(digest_state, (void *)SHA_H_BASE, state_length(sha_type));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Internally calls DMA API for single block */
|
||||
void esp_sha_block(esp_sha_type sha_type, const void *data_block, bool is_first_block)
|
||||
{
|
||||
esp_sha_dma(sha_type, data_block, block_length(sha_type), is_first_block);
|
||||
}
|
||||
|
||||
/* Performs SHA on multiple blocks at a time */
|
||||
int esp_sha_dma(esp_sha_type sha_type, const void *data_block, uint32_t ilen, bool is_first_block)
|
||||
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)
|
||||
{
|
||||
size_t blk_len = 0;
|
||||
const uint8_t *local_buf = data_block;
|
||||
int ret = 0;
|
||||
volatile lldesc_t dma_descr;
|
||||
const void *dma_input;
|
||||
unsigned char *non_icache_input = NULL;
|
||||
unsigned char *non_icache_buf = NULL;
|
||||
int dma_op_num = ( ilen / (SHA_DMA_MAX_BYTES + 1) ) + 1;
|
||||
|
||||
if (ilen == 0) {
|
||||
return ret;
|
||||
if (buf_len > 128) {
|
||||
ESP_LOGE(TAG, "SHA DMA buf_len cannot exceed max size for a single block");
|
||||
return -1;
|
||||
}
|
||||
|
||||
blk_len = block_length(sha_type);
|
||||
|
||||
REG_WRITE(SHA_MODE_REG, sha_type);
|
||||
if ((sha_type == SHA2_512T) && (is_first_block == true)) {
|
||||
REG_WRITE(SHA_START_REG, 1);
|
||||
}
|
||||
|
||||
REG_WRITE(SHA_BLOCK_NUM_REG, (ilen / blk_len));
|
||||
|
||||
if ((sha_type == SHA2_512T) && (is_first_block == true)) {
|
||||
esp_sha_wait_idle();
|
||||
is_first_block = false;
|
||||
}
|
||||
|
||||
bzero( (void *)&dma_descr, sizeof( dma_descr ) );
|
||||
|
||||
/* DMA descriptor for Memory to DMA-AES transfer */
|
||||
dma_descr.length = ilen;
|
||||
dma_descr.size = ilen;
|
||||
dma_descr.owner = 1;
|
||||
dma_descr.eof = 1;
|
||||
dma_descr.buf = local_buf;
|
||||
dma_descr.sosf = 0;
|
||||
dma_descr.empty = 0;
|
||||
|
||||
#if (CONFIG_SPIRAM_USE_CAPS_ALLOC || CONFIG_SPIRAM_USE_MALLOC)
|
||||
if ((unsigned int)data_block >= SOC_EXTRAM_DATA_LOW && (unsigned int)data_block <= SOC_EXTRAM_DATA_HIGH) {
|
||||
if (esp_ptr_external_ram(input) || esp_ptr_external_ram(buf)) {
|
||||
Cache_WriteBack_All();
|
||||
}
|
||||
#endif
|
||||
|
||||
/* 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)) {
|
||||
non_icache_input = malloc(sizeof(unsigned char) * MIN(ilen, SHA_DMA_MAX_BYTES));
|
||||
if (non_icache_input == NULL) {
|
||||
ESP_LOGE(TAG, "Failed to allocate memory");
|
||||
ret = ESP_ERR_NO_MEM;
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
|
||||
if (!esp_ptr_dma_ext_capable(buf) && !esp_ptr_dma_capable(buf)) {
|
||||
non_icache_buf = malloc(sizeof(unsigned char) * buf_len);
|
||||
if (non_icache_buf == NULL) {
|
||||
ESP_LOGE(TAG, "Failed to allocate memory");
|
||||
ret = ESP_ERR_NO_MEM;
|
||||
goto cleanup;
|
||||
}
|
||||
memcpy(non_icache_buf, buf, buf_len);
|
||||
buf = non_icache_buf;
|
||||
}
|
||||
|
||||
/* 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++) {
|
||||
int dma_chunk_len = MIN(ilen, SHA_DMA_MAX_BYTES);
|
||||
|
||||
|
||||
/* 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) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
is_first_block = false;
|
||||
|
||||
|
||||
ilen -= dma_chunk_len;
|
||||
input += dma_chunk_len;
|
||||
|
||||
// Only append buf to the first operation
|
||||
buf_len = 0;
|
||||
}
|
||||
|
||||
cleanup:
|
||||
free(non_icache_input);
|
||||
free(non_icache_buf);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void esp_sha_dma_init(lldesc_t *input)
|
||||
{
|
||||
/* 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)(&dma_descr))&OUT_LINK_REG_OUTLINK_ADDR);
|
||||
SET_PERI_REG_MASK(CRYPTO_DMA_OUT_LINK_REG, ((uint32_t)(input))&OUT_LINK_REG_OUTLINK_ADDR);
|
||||
/* Start transfer */
|
||||
SET_PERI_REG_MASK(CRYPTO_DMA_OUT_LINK_REG, OUT_LINK_REG_OUTLINK_START);
|
||||
}
|
||||
|
||||
#if defined (CONFIG_MBEDTLS_SHA_USE_INTERRUPT)
|
||||
REG_WRITE(SHA_CLEAR_IRQ_REG, 1);
|
||||
if (op_complete_sem == NULL) {
|
||||
op_complete_sem = xSemaphoreCreateBinary();
|
||||
esp_intr_alloc(ETS_SHA_INTR_SOURCE, 0, esp_sha_dma_isr, 0, 0);
|
||||
/* 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);
|
||||
|
||||
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;
|
||||
}
|
||||
/* 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;
|
||||
}
|
||||
REG_WRITE(SHA_INT_ENA_REG, 1);
|
||||
#endif
|
||||
|
||||
/* 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);
|
||||
|
||||
/* Start hashing */
|
||||
if (is_first_block) {
|
||||
REG_WRITE(SHA_DMA_START_REG, 1);
|
||||
} else {
|
||||
REG_WRITE(SHA_DMA_CONTINUE_REG, 1);
|
||||
}
|
||||
|
||||
ret = esp_sha_dma_complete();
|
||||
|
||||
#if (CONFIG_SPIRAM_USE_CAPS_ALLOC || CONFIG_SPIRAM_USE_MALLOC)
|
||||
if ((unsigned int)data_block >= SOC_EXTRAM_DATA_LOW && (unsigned int)data_block <= SOC_EXTRAM_DATA_HIGH) {
|
||||
Cache_Invalidate_DCache_All();
|
||||
}
|
||||
#endif
|
||||
esp_sha_wait_idle();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void esp_sha(esp_sha_type sha_type, const unsigned char *input, size_t ilen, unsigned char *output)
|
||||
{
|
||||
SHA_CTX ctx;
|
||||
|
||||
esp_sha_lock_engine(sha_type);
|
||||
|
||||
ets_sha_init(&ctx, sha_type);
|
||||
ets_sha_starts(&ctx, 0);
|
||||
ets_sha_update(&ctx, input, ilen, false);
|
||||
ets_sha_finish(&ctx, output);
|
||||
|
||||
esp_sha_unlock_engine(sha_type);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user