2016-08-08 09:29:28 +00:00
|
|
|
/*
|
2016-09-02 08:36:26 +00:00
|
|
|
* ESP32 hardware accelerated SHA1/256/512 implementation
|
|
|
|
* based on mbedTLS FIPS-197 compliant version.
|
2016-08-08 09:29:28 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
2016-09-02 08:36:26 +00:00
|
|
|
* Additions Copyright (C) 2016, Espressif Systems (Shanghai) PTE Ltd
|
2016-08-08 09:29:28 +00: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-09-02 08:36:26 +00:00
|
|
|
#include <sys/lock.h>
|
|
|
|
#include "hwcrypto/sha.h"
|
|
|
|
#include "rom/ets_sys.h"
|
2016-08-08 09:29:28 +00:00
|
|
|
|
|
|
|
|
2016-09-02 08:36:26 +00:00
|
|
|
static _lock_t sha_lock;
|
2016-08-08 09:29:28 +00:00
|
|
|
|
2016-09-02 08:36:26 +00:00
|
|
|
void esp_sha_acquire_hardware( void )
|
|
|
|
{
|
|
|
|
/* newlib locks lazy initialize on ESP-IDF */
|
|
|
|
_lock_acquire(&sha_lock);
|
2016-09-02 03:31:38 +00:00
|
|
|
ets_sha_enable();
|
2016-09-02 08:36:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void esp_sha_release_hardware( void )
|
|
|
|
{
|
|
|
|
/* Want to empty internal SHA buffers where possible,
|
|
|
|
need to check if this is sufficient for this. */
|
|
|
|
SHA_CTX zero = { 0 };
|
|
|
|
ets_sha_init(&zero);
|
|
|
|
ets_sha_disable();
|
|
|
|
_lock_release(&sha_lock);
|
|
|
|
}
|
|
|
|
|
2016-09-06 01:05:56 +00:00
|
|
|
/* Generic esp_shaX_update implementation */
|
|
|
|
static void esp_sha_update( esp_sha_context *ctx, const unsigned char *input, size_t ilen, size_t block_size)
|
|
|
|
{
|
|
|
|
/* Feed the SHA engine one block at a time */
|
|
|
|
while(ilen > 0) {
|
|
|
|
size_t chunk_len = (ilen > block_size) ? block_size : ilen;
|
|
|
|
ets_sha_update(&ctx->context, ctx->context_type, input, chunk_len * 8);
|
|
|
|
input += chunk_len;
|
|
|
|
ilen -= chunk_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-06 00:38:12 +00:00
|
|
|
void esp_sha1_init( esp_sha_context *ctx )
|
2016-09-02 08:36:26 +00:00
|
|
|
{
|
2016-09-06 00:38:12 +00:00
|
|
|
bzero( ctx, sizeof( esp_sha_context ) );
|
2016-08-08 09:29:28 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 00:38:12 +00:00
|
|
|
void esp_sha1_free( esp_sha_context *ctx )
|
2016-08-08 09:29:28 +00:00
|
|
|
{
|
2016-09-02 03:31:38 +00:00
|
|
|
if ( ctx == NULL ) {
|
2016-08-08 09:29:28 +00:00
|
|
|
return;
|
2016-09-02 03:31:38 +00:00
|
|
|
}
|
2016-08-08 09:29:28 +00:00
|
|
|
|
2016-09-06 00:38:12 +00:00
|
|
|
bzero( ctx, sizeof( esp_sha_context ) );
|
2016-08-08 09:29:28 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 00:38:12 +00:00
|
|
|
void esp_sha1_clone( esp_sha_context *dst, const esp_sha_context *src )
|
2016-08-08 09:29:28 +00:00
|
|
|
{
|
|
|
|
*dst = *src;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SHA-1 context setup
|
|
|
|
*/
|
2016-09-06 00:38:12 +00:00
|
|
|
void esp_sha1_start( esp_sha_context *ctx )
|
2016-08-08 09:29:28 +00:00
|
|
|
{
|
2016-09-02 08:36:26 +00:00
|
|
|
esp_sha_acquire_hardware();
|
2016-09-02 03:31:38 +00:00
|
|
|
ctx->context_type = SHA1;
|
2016-08-08 09:29:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SHA-1 process buffer
|
|
|
|
*/
|
2016-09-06 00:38:12 +00:00
|
|
|
void esp_sha1_update( esp_sha_context *ctx, const unsigned char *input, size_t ilen )
|
2016-08-08 09:29:28 +00:00
|
|
|
{
|
2016-09-06 01:05:56 +00:00
|
|
|
esp_sha_update(ctx, input, ilen, 64);
|
2016-08-08 09:29:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SHA-1 final digest
|
|
|
|
*/
|
2016-09-06 00:38:12 +00:00
|
|
|
void esp_sha1_finish( esp_sha_context *ctx, unsigned char output[20] )
|
2016-08-08 09:29:28 +00:00
|
|
|
{
|
2016-09-02 03:31:38 +00:00
|
|
|
ets_sha_finish(&ctx->context, ctx->context_type, output);
|
2016-09-02 08:36:26 +00:00
|
|
|
esp_sha_release_hardware();
|
2016-08-08 09:29:28 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 00:38:12 +00:00
|
|
|
/* Full SHA-1 calculation */
|
|
|
|
void esp_sha1( const unsigned char *input, size_t ilen, unsigned char output[20] )
|
2016-08-08 09:29:28 +00:00
|
|
|
{
|
2016-09-06 00:38:12 +00:00
|
|
|
esp_sha_context ctx;
|
2016-08-08 09:29:28 +00:00
|
|
|
|
|
|
|
esp_sha1_init( &ctx );
|
2016-08-31 03:43:48 +00:00
|
|
|
esp_sha1_start( &ctx );
|
2016-08-08 09:29:28 +00:00
|
|
|
esp_sha1_update( &ctx, input, ilen );
|
|
|
|
esp_sha1_finish( &ctx, output );
|
|
|
|
esp_sha1_free( &ctx );
|
|
|
|
}
|
|
|
|
|
2016-09-06 00:38:12 +00:00
|
|
|
void esp_sha256_init( esp_sha_context *ctx )
|
2016-08-08 09:29:28 +00:00
|
|
|
{
|
2016-09-06 00:38:12 +00:00
|
|
|
bzero( ctx, sizeof( esp_sha_context ) );
|
2016-08-08 09:29:28 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 00:38:12 +00:00
|
|
|
void esp_sha256_free( esp_sha_context *ctx )
|
2016-08-08 09:29:28 +00:00
|
|
|
{
|
2016-09-02 03:31:38 +00:00
|
|
|
if ( ctx == NULL ) {
|
2016-08-08 09:29:28 +00:00
|
|
|
return;
|
2016-09-02 03:31:38 +00:00
|
|
|
}
|
2016-08-08 09:29:28 +00:00
|
|
|
|
2016-09-06 00:38:12 +00:00
|
|
|
bzero( ctx, sizeof( esp_sha_context ) );
|
2016-08-08 09:29:28 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 00:38:12 +00:00
|
|
|
void esp_sha256_clone( esp_sha_context *dst, const esp_sha_context *src )
|
2016-08-08 09:29:28 +00:00
|
|
|
{
|
|
|
|
*dst = *src;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SHA-256 context setup
|
|
|
|
*/
|
2016-09-06 00:38:12 +00:00
|
|
|
void esp_sha256_start( esp_sha_context *ctx, int is224 )
|
2016-08-08 09:29:28 +00:00
|
|
|
{
|
2016-09-02 08:36:26 +00:00
|
|
|
esp_sha_acquire_hardware();
|
2016-08-08 09:29:28 +00:00
|
|
|
ets_sha_init(&ctx->context);
|
|
|
|
|
2016-09-02 03:31:38 +00:00
|
|
|
if ( is224 == 0 ) {
|
2016-08-08 09:29:28 +00:00
|
|
|
/* SHA-256 */
|
2016-09-05 00:36:25 +00:00
|
|
|
ctx->context_type = SHA2_256;
|
2016-09-02 03:31:38 +00:00
|
|
|
} else {
|
2016-09-05 00:36:25 +00:00
|
|
|
/* SHA-224 is not supported! */
|
|
|
|
ctx->context_type = SHA_INVALID;
|
2016-09-02 03:31:38 +00:00
|
|
|
}
|
2016-08-08 09:29:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SHA-256 process buffer
|
|
|
|
*/
|
2016-09-06 00:38:12 +00:00
|
|
|
void esp_sha256_update( esp_sha_context *ctx, const unsigned char *input, size_t ilen )
|
2016-08-08 09:29:28 +00:00
|
|
|
{
|
2016-09-06 01:05:56 +00:00
|
|
|
esp_sha_update(ctx, input, ilen, 64);
|
2016-08-08 09:29:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SHA-256 final digest
|
|
|
|
*/
|
2016-09-06 00:38:12 +00:00
|
|
|
void esp_sha256_finish( esp_sha_context *ctx, unsigned char output[32] )
|
2016-08-08 09:29:28 +00:00
|
|
|
{
|
2016-09-05 00:36:25 +00:00
|
|
|
if ( ctx->context_type == SHA2_256 ) {
|
|
|
|
ets_sha_finish(&ctx->context, ctx->context_type, output);
|
|
|
|
} else {
|
|
|
|
/* No hardware SHA-224 support, but mbedTLS API doesn't allow failure.
|
|
|
|
For now, zero the output to make it clear it's not valid. */
|
|
|
|
bzero( output, 28 );
|
|
|
|
}
|
2016-09-02 08:36:26 +00:00
|
|
|
esp_sha_release_hardware();
|
2016-08-08 09:29:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-09-06 00:38:12 +00:00
|
|
|
* Full SHA-256 calculation
|
2016-08-08 09:29:28 +00:00
|
|
|
*/
|
2016-09-06 00:38:12 +00:00
|
|
|
void esp_sha256( const unsigned char *input, size_t ilen, unsigned char output[32], int is224 )
|
2016-08-08 09:29:28 +00:00
|
|
|
{
|
2016-09-06 00:38:12 +00:00
|
|
|
esp_sha_context ctx;
|
2016-08-08 09:29:28 +00:00
|
|
|
|
|
|
|
esp_sha256_init( &ctx );
|
2016-08-31 03:43:48 +00:00
|
|
|
esp_sha256_start( &ctx, is224 );
|
2016-08-08 09:29:28 +00:00
|
|
|
esp_sha256_update( &ctx, input, ilen );
|
|
|
|
esp_sha256_finish( &ctx, output );
|
|
|
|
esp_sha256_free( &ctx );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/////
|
2016-09-06 00:38:12 +00:00
|
|
|
void esp_sha512_init( esp_sha_context *ctx )
|
2016-08-08 09:29:28 +00:00
|
|
|
{
|
2016-09-06 00:38:12 +00:00
|
|
|
memset( ctx, 0, sizeof( esp_sha_context ) );
|
2016-08-30 12:40:58 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 00:38:12 +00:00
|
|
|
void esp_sha512_free( esp_sha_context *ctx )
|
2016-08-08 09:29:28 +00:00
|
|
|
{
|
2016-09-02 03:31:38 +00:00
|
|
|
if ( ctx == NULL ) {
|
2016-08-08 09:29:28 +00:00
|
|
|
return;
|
2016-09-02 03:31:38 +00:00
|
|
|
}
|
2016-08-08 09:29:28 +00:00
|
|
|
|
2016-09-06 00:38:12 +00:00
|
|
|
bzero( ctx, sizeof( esp_sha_context ) );
|
2016-08-08 09:29:28 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 00:38:12 +00:00
|
|
|
void esp_sha512_clone( esp_sha_context *dst, const esp_sha_context *src )
|
2016-08-08 09:29:28 +00:00
|
|
|
{
|
|
|
|
*dst = *src;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SHA-512 context setup
|
|
|
|
*/
|
2016-09-06 00:38:12 +00:00
|
|
|
void esp_sha512_start( esp_sha_context *ctx, int is384 )
|
2016-08-08 09:29:28 +00:00
|
|
|
{
|
2016-09-02 08:36:26 +00:00
|
|
|
esp_sha_acquire_hardware();
|
2016-09-02 03:31:38 +00:00
|
|
|
ets_sha_init(&ctx->context);
|
2016-08-15 13:04:57 +00:00
|
|
|
|
2016-09-02 03:31:38 +00:00
|
|
|
if ( is384 == 0 ) {
|
2016-08-08 09:29:28 +00:00
|
|
|
/* SHA-512 */
|
|
|
|
ctx->context_type = SHA2_512;
|
2016-09-02 03:31:38 +00:00
|
|
|
} else {
|
2016-08-08 09:29:28 +00:00
|
|
|
/* SHA-384 */
|
|
|
|
ctx->context_type = SHA2_384;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SHA-512 process buffer
|
|
|
|
*/
|
2016-09-06 00:38:12 +00:00
|
|
|
void esp_sha512_update( esp_sha_context *ctx, const unsigned char *input, size_t ilen )
|
2016-08-08 09:29:28 +00:00
|
|
|
{
|
2016-09-06 01:05:56 +00:00
|
|
|
esp_sha_update(ctx, input, ilen, 128);
|
2016-08-08 09:29:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SHA-512 final digest
|
|
|
|
*/
|
2016-09-06 00:38:12 +00:00
|
|
|
void esp_sha512_finish( esp_sha_context *ctx, unsigned char output[64] )
|
2016-08-08 09:29:28 +00:00
|
|
|
{
|
|
|
|
ets_sha_finish(&ctx->context, ctx->context_type, output);
|
2016-09-02 08:36:26 +00:00
|
|
|
esp_sha_release_hardware();
|
2016-08-08 09:29:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-09-06 00:38:12 +00:00
|
|
|
* Full SHA-512 calculation
|
2016-08-08 09:29:28 +00:00
|
|
|
*/
|
2016-09-06 00:38:12 +00:00
|
|
|
void esp_sha512( const unsigned char *input, size_t ilen, unsigned char output[64], int is384 )
|
2016-08-08 09:29:28 +00:00
|
|
|
{
|
2016-09-06 00:38:12 +00:00
|
|
|
esp_sha_context ctx;
|
2016-08-08 09:29:28 +00:00
|
|
|
|
|
|
|
esp_sha512_init( &ctx );
|
2016-08-31 03:43:48 +00:00
|
|
|
esp_sha512_start( &ctx, is384 );
|
2016-08-08 09:29:28 +00:00
|
|
|
esp_sha512_update( &ctx, input, ilen );
|
|
|
|
esp_sha512_finish( &ctx, output );
|
|
|
|
esp_sha512_free( &ctx );
|
|
|
|
}
|
|
|
|
|
|
|
|
////
|
|
|
|
|