recompile crypto and bignum function

This commit is contained in:
liuhan 2016-08-08 17:29:28 +08:00 committed by Wu Jian Gang
parent d9b660f6d4
commit 98021903a2
28 changed files with 1325 additions and 1328 deletions

View file

@ -25,19 +25,17 @@
* http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf
*/
#include "port/aes_alt.h"
#if defined(ESP_AES_C)
#include "aes.h"
#include <string.h>
#include "multi_thread.h"
#include "esp_thread.h"
/* Implementation that should never be optimized out by the compiler */
static void aes_zeroize( void *v, size_t n ) {
static void esp_aes_zeroize( void *v, size_t n ) {
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
}
void aes_init( AES_CTX *ctx )
void esp_aes_init( AES_CTX *ctx )
{
memset( ctx, 0, sizeof( AES_CTX ) );
@ -47,12 +45,12 @@ void aes_init( AES_CTX *ctx )
AES_UNLOCK();
}
void aes_free( AES_CTX *ctx )
void esp_aes_free( AES_CTX *ctx )
{
if( ctx == NULL )
return;
aes_zeroize( ctx, sizeof( AES_CTX ) );
esp_aes_zeroize( ctx, sizeof( AES_CTX ) );
AES_LOCK();
AES_GIVE();
@ -64,7 +62,7 @@ void aes_free( AES_CTX *ctx )
/*
* AES key schedule (encryption)
*/
int aes_setkey_enc( AES_CTX *ctx, const unsigned char *key,
int esp_aes_setkey_enc( AES_CTX *ctx, const unsigned char *key,
unsigned int keybits )
{
enum AES_BITS keybit;
@ -94,7 +92,7 @@ int aes_setkey_enc( AES_CTX *ctx, const unsigned char *key,
/*
* AES key schedule (decryption)
*/
int aes_setkey_dec( AES_CTX *ctx, const unsigned char *key,
int esp_aes_setkey_dec( AES_CTX *ctx, const unsigned char *key,
unsigned int keybits )
{
enum AES_BITS keybit;
@ -122,17 +120,17 @@ int aes_setkey_dec( AES_CTX *ctx, const unsigned char *key,
}
static void aes_process_enable(AES_CTX *ctx, int mode)
static void esp_aes_process_enable(AES_CTX *ctx, int mode)
{
if( mode == AES_ENCRYPT ){
aes_setkey_enc(ctx, ctx->enc.key, ctx->enc.keybites);
esp_aes_setkey_enc(ctx, ctx->enc.key, ctx->enc.keybites);
}else{
aes_setkey_dec(ctx, ctx->dec.key, ctx->dec.keybites);
esp_aes_setkey_dec(ctx, ctx->dec.key, ctx->dec.keybites);
}
return;
}
static void aes_process_disable(AES_CTX *ctx, int mode)
static void esp_aes_process_disable(AES_CTX *ctx, int mode)
{
}
@ -141,7 +139,7 @@ static void aes_process_disable(AES_CTX *ctx, int mode)
* AES-ECB block encryption
*/
void aes_encrypt( AES_CTX *ctx,
void esp_aes_encrypt( AES_CTX *ctx,
const unsigned char input[16],
unsigned char output[16] )
{
@ -154,7 +152,7 @@ void aes_encrypt( AES_CTX *ctx,
* AES-ECB block decryption
*/
void aes_decrypt( AES_CTX *ctx,
void esp_aes_decrypt( AES_CTX *ctx,
const unsigned char input[16],
unsigned char output[16] )
{
@ -166,15 +164,15 @@ void aes_decrypt( AES_CTX *ctx,
/*
* AES-ECB block encryption/decryption
*/
int aes_crypt_ecb( AES_CTX *ctx,
int esp_aes_crypt_ecb( AES_CTX *ctx,
int mode,
const unsigned char input[16],
unsigned char output[16] )
{
if( mode == AES_ENCRYPT )
aes_encrypt( ctx, input, output );
esp_aes_encrypt( ctx, input, output );
else
aes_decrypt( ctx, input, output );
esp_aes_decrypt( ctx, input, output );
return 0;
}
@ -182,7 +180,7 @@ int aes_crypt_ecb( AES_CTX *ctx,
/*
* AES-CBC buffer encryption/decryption
*/
int aes_crypt_cbc( AES_CTX *ctx,
int esp_aes_crypt_cbc( AES_CTX *ctx,
int mode,
size_t length,
unsigned char iv[16],
@ -197,13 +195,13 @@ int aes_crypt_cbc( AES_CTX *ctx,
AES_LOCK();
aes_process_enable(ctx, mode);
esp_aes_process_enable(ctx, mode);
if( mode == AES_DECRYPT )
{
while( length > 0 )
{
memcpy( temp, input, 16 );
aes_crypt_ecb( ctx, mode, input, output );
esp_aes_crypt_ecb( ctx, mode, input, output );
for( i = 0; i < 16; i++ )
output[i] = (unsigned char)( output[i] ^ iv[i] );
@ -222,7 +220,7 @@ int aes_crypt_cbc( AES_CTX *ctx,
for( i = 0; i < 16; i++ )
output[i] = (unsigned char)( input[i] ^ iv[i] );
aes_crypt_ecb( ctx, mode, output, output );
esp_aes_crypt_ecb( ctx, mode, output, output );
memcpy( iv, output, 16 );
input += 16;
@ -230,7 +228,7 @@ int aes_crypt_cbc( AES_CTX *ctx,
length -= 16;
}
}
aes_process_disable(ctx, mode);
esp_aes_process_disable(ctx, mode);
AES_UNLOCK();
@ -240,7 +238,7 @@ int aes_crypt_cbc( AES_CTX *ctx,
/*
* AES-CFB128 buffer encryption/decryption
*/
int aes_crypt_cfb128( AES_CTX *ctx,
int esp_aes_crypt_cfb128( AES_CTX *ctx,
int mode,
size_t length,
size_t *iv_off,
@ -253,13 +251,13 @@ int aes_crypt_cfb128( AES_CTX *ctx,
AES_LOCK();
aes_process_enable(ctx, mode);
esp_aes_process_enable(ctx, mode);
if( mode == AES_DECRYPT )
{
while( length-- )
{
if( n == 0 )
aes_crypt_ecb( ctx, AES_ENCRYPT, iv, iv );
esp_aes_crypt_ecb( ctx, AES_ENCRYPT, iv, iv );
c = *input++;
*output++ = (unsigned char)( c ^ iv[n] );
@ -273,7 +271,7 @@ int aes_crypt_cfb128( AES_CTX *ctx,
while( length-- )
{
if( n == 0 )
aes_crypt_ecb( ctx, AES_ENCRYPT, iv, iv );
esp_aes_crypt_ecb( ctx, AES_ENCRYPT, iv, iv );
iv[n] = *output++ = (unsigned char)( iv[n] ^ *input++ );
@ -282,7 +280,7 @@ int aes_crypt_cfb128( AES_CTX *ctx,
}
*iv_off = n;
aes_process_disable(ctx, mode);
esp_aes_process_disable(ctx, mode);
AES_UNLOCK();
@ -292,7 +290,7 @@ int aes_crypt_cfb128( AES_CTX *ctx,
/*
* AES-CFB8 buffer encryption/decryption
*/
int aes_crypt_cfb8( AES_CTX *ctx,
int esp_aes_crypt_cfb8( AES_CTX *ctx,
int mode,
size_t length,
unsigned char iv[16],
@ -304,11 +302,11 @@ int aes_crypt_cfb8( AES_CTX *ctx,
AES_LOCK();
aes_process_enable(ctx, mode);
esp_aes_process_enable(ctx, mode);
while( length-- )
{
memcpy( ov, iv, 16 );
aes_crypt_ecb( ctx, AES_ENCRYPT, iv, iv );
esp_aes_crypt_ecb( ctx, AES_ENCRYPT, iv, iv );
if( mode == AES_DECRYPT )
ov[16] = *input;
@ -320,7 +318,7 @@ int aes_crypt_cfb8( AES_CTX *ctx,
memcpy( iv, ov + 1, 16 );
}
aes_process_disable(ctx, mode);
esp_aes_process_disable(ctx, mode);
AES_UNLOCK();
@ -330,7 +328,7 @@ int aes_crypt_cfb8( AES_CTX *ctx,
/*
* AES-CTR buffer encryption/decryption
*/
int aes_crypt_ctr( AES_CTX *ctx,
int esp_aes_crypt_ctr( AES_CTX *ctx,
size_t length,
size_t *nc_off,
unsigned char nonce_counter[16],
@ -343,12 +341,12 @@ int aes_crypt_ctr( AES_CTX *ctx,
AES_LOCK();
aes_process_enable(ctx, AES_ENCRYPT);
esp_aes_process_enable(ctx, AES_ENCRYPT);
while( length-- )
{
if( n == 0 ) {
aes_crypt_ecb( ctx, AES_ENCRYPT, nonce_counter, stream_block );
esp_aes_crypt_ecb( ctx, AES_ENCRYPT, nonce_counter, stream_block );
for( i = 16; i > 0; i-- )
if( ++nonce_counter[i - 1] != 0 )
@ -361,12 +359,10 @@ int aes_crypt_ctr( AES_CTX *ctx,
}
*nc_off = n;
aes_process_disable(ctx, AES_ENCRYPT);
esp_aes_process_disable(ctx, AES_ENCRYPT);
AES_UNLOCK();
return 0;
}
#endif /* AES_ALT_C */

View file

@ -0,0 +1,53 @@
#include "esp_thread.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
static xSemaphoreHandle esp_thread_mutex[MUTEX_MAX_NUM];
static int esp_thread_sig[MUTEX_MAX_NUM];
int esp_thread_init(void)
{
int i;
for (i = 0; i < MUTEX_MAX_NUM; i++) {
esp_thread_mutex[i] = xSemaphoreCreateMutex();
if (!esp_thread_mutex[i]) {
goto failed1;
}
esp_thread_sig[i] = 0;
}
return 0;
failed1:
for (i--; i >= 0; i--)
vQueueDelete(esp_thread_mutex[i]);
return -1;
}
void esp_thread_lock(unsigned int num)
{
xSemaphoreTake(esp_thread_mutex[num], portMAX_DELAY);
}
void esp_thread_unlock(unsigned int num)
{
xSemaphoreGive(esp_thread_mutex[num]);
}
void esp_thread_take(unsigned int num)
{
esp_thread_sig[num]++;
}
void esp_thread_give(unsigned int num)
{
esp_thread_sig[num]--;
}
bool esp_thread_is_used(unsigned int num)
{
return (esp_thread_sig[num] != 0) ? true : false;
}

View file

@ -1,5 +1,5 @@
/**
* \file aes_alt.h
* \file esp_aes.h
*
* \brief AES block cipher
*
@ -21,8 +21,8 @@
*
*/
#ifndef AES_ALT_H
#define AES_ALT_H
#ifndef ESP_AES_H
#define ESP_AES_H
#include "c_types.h"
#include "rom/ets_sys.h"
@ -32,8 +32,6 @@
extern "C" {
#endif
#define ESP_AES_C
/* padlock.c and aesni.c rely on these values! */
#define AES_ENCRYPT 1
#define AES_DECRYPT 0
@ -61,23 +59,21 @@ typedef struct
uint32_t *rk; /*!< AES round keys */
KEY_CTX enc;
KEY_CTX dec;
}aes_context;
typedef aes_context AES_CTX;
}aes_context, AES_CTX;
/**
* \brief Initialize AES context
*
* \param ctx AES context to be initialized
*/
void aes_init( AES_CTX *ctx );
void esp_aes_init( AES_CTX *ctx );
/**
* \brief Clear AES context
*
* \param ctx AES context to be cleared
*/
void aes_free( AES_CTX *ctx );
void esp_aes_free( AES_CTX *ctx );
/**
* \brief AES key schedule (encryption)
@ -88,7 +84,7 @@ void aes_free( AES_CTX *ctx );
*
* \return 0 if successful, or ERR_AES_INVALID_KEY_LENGTH
*/
int aes_setkey_enc( AES_CTX *ctx, const unsigned char *key,unsigned int keybits );
int esp_aes_setkey_enc( AES_CTX *ctx, const unsigned char *key,unsigned int keybits );
/**
* \brief AES key schedule (decryption)
@ -99,7 +95,7 @@ int aes_setkey_enc( AES_CTX *ctx, const unsigned char *key,unsigned int keybits
*
* \return 0 if successful, or ERR_AES_INVALID_KEY_LENGTH
*/
int aes_setkey_dec( AES_CTX *ctx, const unsigned char *key,unsigned int keybits );
int esp_aes_setkey_dec( AES_CTX *ctx, const unsigned char *key,unsigned int keybits );
/**
* \brief AES-ECB block encryption/decryption
@ -111,7 +107,7 @@ int aes_setkey_dec( AES_CTX *ctx, const unsigned char *key,unsigned int keybits
*
* \return 0 if successful
*/
int aes_crypt_ecb( AES_CTX *ctx,int mode,const unsigned char input[16],unsigned char output[16] );
int esp_aes_crypt_ecb( AES_CTX *ctx,int mode,const unsigned char input[16],unsigned char output[16] );
/**
* \brief AES-CBC buffer encryption/decryption
@ -135,7 +131,7 @@ int aes_crypt_ecb( AES_CTX *ctx,int mode,const unsigned char input[16],unsigned
*
* \return 0 if successful, or ERR_AES_INVALID_INPUT_LENGTH
*/
int aes_crypt_cbc( AES_CTX *ctx,
int esp_aes_crypt_cbc( AES_CTX *ctx,
int mode,
size_t length,
unsigned char iv[16],
@ -148,7 +144,7 @@ int aes_crypt_cbc( AES_CTX *ctx,
*
* Note: Due to the nature of CFB you should use the same key schedule for
* both encryption and decryption. So a context initialized with
* aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
* esp_aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
*
* \note Upon exit, the content of the IV is updated so that you can
* call the function same function again on the following
@ -168,7 +164,7 @@ int aes_crypt_cbc( AES_CTX *ctx,
*
* \return 0 if successful
*/
int aes_crypt_cfb128( AES_CTX *ctx,
int esp_aes_crypt_cfb128( AES_CTX *ctx,
int mode,
size_t length,
size_t *iv_off,
@ -181,7 +177,7 @@ int aes_crypt_cfb128( AES_CTX *ctx,
*
* Note: Due to the nature of CFB you should use the same key schedule for
* both encryption and decryption. So a context initialized with
* aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
* esp_aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
*
* \note Upon exit, the content of the IV is updated so that you can
* call the function same function again on the following
@ -200,7 +196,7 @@ int aes_crypt_cfb128( AES_CTX *ctx,
*
* \return 0 if successful
*/
int aes_crypt_cfb8( AES_CTX *ctx,
int esp_aes_crypt_cfb8( AES_CTX *ctx,
int mode,
size_t length,
unsigned char iv[16],
@ -214,7 +210,7 @@ int aes_crypt_cfb8( AES_CTX *ctx,
*
* Note: Due to the nature of CTR you should use the same key schedule for
* both encryption and decryption. So a context initialized with
* aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
* esp_aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
*
* \param ctx AES context
* \param length The length of the data
@ -229,7 +225,7 @@ int aes_crypt_cfb8( AES_CTX *ctx,
*
* \return 0 if successful
*/
int aes_crypt_ctr( AES_CTX *ctx,
int esp_aes_crypt_ctr( AES_CTX *ctx,
size_t length,
size_t *nc_off,
unsigned char nonce_counter[16],
@ -247,7 +243,7 @@ int aes_crypt_ctr( AES_CTX *ctx,
* \param input Plaintext block
* \param output Output (ciphertext) block
*/
void aes_encrypt( AES_CTX *ctx, const unsigned char input[16],unsigned char output[16] );
void esp_aes_encrypt( AES_CTX *ctx, const unsigned char input[16],unsigned char output[16] );
/**
* \brief Internal AES block decryption function
@ -258,7 +254,7 @@ void aes_encrypt( AES_CTX *ctx, const unsigned char input[16],unsigned char outp
* \param input Ciphertext block
* \param output Output (plaintext) block
*/
void aes_decrypt( AES_CTX *ctx, const unsigned char input[16], unsigned char output[16] );
void esp_aes_decrypt( AES_CTX *ctx, const unsigned char input[16], unsigned char output[16] );
#ifdef __cplusplus
}

View file

@ -20,8 +20,8 @@
*
*/
#ifndef BIGNUM_ALT_H
#define BIGNUM_ALT_H
#ifndef _ESP_BIGNUM_H
#define _ESP_BIGNUM_H
#include "c_types.h"
#include "rom/ets_sys.h"
@ -42,9 +42,9 @@
#define MPI_CHK(f) do { if( ( ret = f ) != 0 ) goto cleanup; } while( 0 )
#if defined(MPI_DEBUG_ALT)
#define mpi_printf ets_printf
#define esp_mpi_printf ets_printf
#else
#define mpi_printf
#define esp_mpi_printf
#endif
/*
* Maximum size MPIs are allowed to grow to in number of limbs.
@ -78,8 +78,8 @@
#define MPI_MAX_BITS ( 8 * MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
/*
* When reading from files with mpi_read_file() and writing to files with
* mpi_write_file() the buffer should have space
* When reading from files with esp_mpi_read_file() and writing to files with
* esp_mpi_write_file() the buffer should have space
* for a (short) label, the MPI (in the provided radix), the newline
* characters and the '\0'.
*
@ -108,8 +108,8 @@
#if ( ! defined(HAVE_INT32) && \
defined(_MSC_VER) && defined(_M_AMD64) )
#define HAVE_INT64
typedef int64_t mpi_sint;
typedef uint64_t mpi_uint;
typedef int64_t esp_mpi_sint;
typedef uint64_t esp_mpi_uint;
#else
#if ( ! defined(HAVE_INT32) && \
defined(__GNUC__) && ( \
@ -119,15 +119,15 @@
(defined(__sparc__) && defined(__arch64__)) || \
defined(__s390x__) || defined(__mips64) ) )
#define HAVE_INT64
typedef int64_t mpi_sint;
typedef uint64_t mpi_uint;
typedef int64_t esp_mpi_sint;
typedef uint64_t esp_mpi_uint;
/* t_udbl defined as 128-bit unsigned int */
typedef unsigned int t_udbl __attribute__((mode(TI)));
#define HAVE_UDBL
#else
#define HAVE_INT32
typedef int32_t mpi_sint;
typedef uint32_t mpi_uint;
typedef int32_t esp_mpi_sint;
typedef uint32_t esp_mpi_uint;
typedef uint64_t t_udbl;
#define HAVE_UDBL
#endif /* !HAVE_INT32 && __GNUC__ && 64-bit platform */
@ -144,9 +144,8 @@ typedef struct
{
int s; /*!< integer sign */
size_t n; /*!< total # of limbs */
mpi_uint *p; /*!< pointer to limbs */
}
mpi;
esp_mpi_uint *p; /*!< pointer to limbs */
}mpi, MPI_CTX;
/**
* \brief Initialize one MPI (make internal references valid)
@ -155,14 +154,14 @@ mpi;
*
* \param X One MPI to initialize.
*/
void mpi_init( mpi *X );
void esp_mpi_init( mpi *X );
/**
* \brief Unallocate one MPI
*
* \param X One MPI to unallocate.
*/
void mpi_free( mpi *X );
void esp_mpi_free( mpi *X );
/**
* \brief Enlarge to the specified number of limbs
@ -173,7 +172,7 @@ void mpi_free( mpi *X );
* \return 0 if successful,
* ERR_MPI_ALLOC_FAILED if memory allocation failed
*/
int mpi_grow( mpi *X, size_t nblimbs );
int esp_mpi_grow( mpi *X, size_t nblimbs );
/**
* \brief Resize down, keeping at least the specified number of limbs
@ -184,7 +183,7 @@ int mpi_grow( mpi *X, size_t nblimbs );
* \return 0 if successful,
* ERR_MPI_ALLOC_FAILED if memory allocation failed
*/
int mpi_shrink( mpi *X, size_t nblimbs );
int esp_mpi_shrink( mpi *X, size_t nblimbs );
/**
* \brief Copy the contents of Y into X
@ -195,7 +194,7 @@ int mpi_shrink( mpi *X, size_t nblimbs );
* \return 0 if successful,
* ERR_MPI_ALLOC_FAILED if memory allocation failed
*/
int mpi_copy( mpi *X, const mpi *Y );
int esp_mpi_copy( mpi *X, const mpi *Y );
/**
* \brief Swap the contents of X and Y
@ -203,7 +202,7 @@ int mpi_copy( mpi *X, const mpi *Y );
* \param X First MPI value
* \param Y Second MPI value
*/
void mpi_swap( mpi *X, mpi *Y );
void esp_mpi_swap( mpi *X, mpi *Y );
/**
* \brief Safe conditional assignement X = Y if assign is 1
@ -216,13 +215,13 @@ void mpi_swap( mpi *X, mpi *Y );
* ERR_MPI_ALLOC_FAILED if memory allocation failed,
*
* \note This function is equivalent to
* if( assign ) mpi_copy( X, Y );
* if( assign ) esp_mpi_copy( X, Y );
* except that it avoids leaking any information about whether
* the assignment was done or not (the above code may leak
* information through branch prediction and/or memory access
* patterns analysis).
*/
int mpi_safe_cond_assign( mpi *X, const mpi *Y, unsigned char assign );
int esp_mpi_safe_cond_assign( mpi *X, const mpi *Y, unsigned char assign );
/**
* \brief Safe conditional swap X <-> Y if swap is 1
@ -235,13 +234,13 @@ int mpi_safe_cond_assign( mpi *X, const mpi *Y, unsigned char assign );
* ERR_MPI_ALLOC_FAILED if memory allocation failed,
*
* \note This function is equivalent to
* if( assign ) mpi_swap( X, Y );
* if( assign ) esp_mpi_swap( X, Y );
* except that it avoids leaking any information about whether
* the assignment was done or not (the above code may leak
* information through branch prediction and/or memory access
* patterns analysis).
*/
int mpi_safe_cond_swap( mpi *X, mpi *Y, unsigned char assign );
int esp_mpi_safe_cond_swap( mpi *X, mpi *Y, unsigned char assign );
/**
* \brief Set value from integer
@ -252,7 +251,7 @@ int mpi_safe_cond_swap( mpi *X, mpi *Y, unsigned char assign );
* \return 0 if successful,
* ERR_MPI_ALLOC_FAILED if memory allocation failed
*/
int mpi_lset( mpi *X, mpi_sint z );
int esp_mpi_lset( mpi *X, esp_mpi_sint z );
/**
* \brief Get a specific bit from X
@ -262,7 +261,7 @@ int mpi_lset( mpi *X, mpi_sint z );
*
* \return Either a 0 or a 1
*/
int mpi_get_bit( const mpi *X, size_t pos );
int esp_mpi_get_bit( const mpi *X, size_t pos );
/**
* \brief Set a bit of X to a specific value of 0 or 1
@ -278,7 +277,7 @@ int mpi_get_bit( const mpi *X, size_t pos );
* ERR_MPI_ALLOC_FAILED if memory allocation failed,
* ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
*/
int mpi_set_bit( mpi *X, size_t pos, unsigned char val );
int esp_mpi_set_bit( mpi *X, size_t pos, unsigned char val );
/**
* \brief Return the number of zero-bits before the least significant
@ -288,7 +287,7 @@ int mpi_set_bit( mpi *X, size_t pos, unsigned char val );
*
* \param X MPI to use
*/
size_t mpi_lsb( const mpi *X );
size_t esp_mpi_lsb( const mpi *X );
/**
* \brief Return the number of bits up to and including the most
@ -298,14 +297,14 @@ size_t mpi_lsb( const mpi *X );
*
* \param X MPI to use
*/
size_t mpi_bitlen( const mpi *X );
size_t esp_mpi_bitlen( const mpi *X );
/**
* \brief Return the total size in bytes
*
* \param X MPI to use
*/
size_t mpi_size( const mpi *X );
size_t esp_mpi_size( const mpi *X );
/**
* \brief Import from an ASCII string
@ -316,7 +315,7 @@ size_t mpi_size( const mpi *X );
*
* \return 0 if successful, or a ERR_MPI_XXX error code
*/
int mpi_read_string( mpi *X, int radix, const char *s );
int esp_mpi_read_string( mpi *X, int radix, const char *s );
/**
* \brief Export into an ASCII string
@ -334,7 +333,7 @@ int mpi_read_string( mpi *X, int radix, const char *s );
* \note Call this function with buflen = 0 to obtain the
* minimum required buffer size in *olen.
*/
int mpi_write_string( const mpi *X, int radix,
int esp_mpi_write_string( const mpi *X, int radix,
char *buf, size_t buflen, size_t *olen );
#if defined(FS_IO)
@ -349,7 +348,7 @@ int mpi_write_string( const mpi *X, int radix,
* the file read buffer is too small or a
* ERR_MPI_XXX error code
*/
int mpi_read_file( mpi *X, int radix, FILE *fin );
int esp_mpi_read_file( mpi *X, int radix, FILE *fin );
/**
* \brief Write X into an opened file, or stdout if fout is NULL
@ -363,7 +362,7 @@ int mpi_read_file( mpi *X, int radix, FILE *fin );
*
* \note Set fout == NULL to print X on the console.
*/
int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
int esp_mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
#endif /* FS_IO */
/**
@ -376,7 +375,7 @@ int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
* \return 0 if successful,
* ERR_MPI_ALLOC_FAILED if memory allocation failed
*/
int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen );
int esp_mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen );
/**
* \brief Export X into unsigned binary data, big endian.
@ -390,7 +389,7 @@ int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen );
* \return 0 if successful,
* ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
*/
int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen );
int esp_mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen );
/**
* \brief Left-shift: X <<= count
@ -401,7 +400,7 @@ int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen );
* \return 0 if successful,
* ERR_MPI_ALLOC_FAILED if memory allocation failed
*/
int mpi_shift_l( mpi *X, size_t count );
int esp_mpi_shift_l( mpi *X, size_t count );
/**
* \brief Right-shift: X >>= count
@ -412,7 +411,7 @@ int mpi_shift_l( mpi *X, size_t count );
* \return 0 if successful,
* ERR_MPI_ALLOC_FAILED if memory allocation failed
*/
int mpi_shift_r( mpi *X, size_t count );
int esp_mpi_shift_r( mpi *X, size_t count );
/**
* \brief Compare unsigned values
@ -424,7 +423,7 @@ int mpi_shift_r( mpi *X, size_t count );
* -1 if |X| is lesser than |Y| or
* 0 if |X| is equal to |Y|
*/
int mpi_cmp_abs( const mpi *X, const mpi *Y );
int esp_mpi_cmp_abs( const mpi *X, const mpi *Y );
/**
* \brief Compare signed values
@ -436,7 +435,7 @@ int mpi_cmp_abs( const mpi *X, const mpi *Y );
* -1 if X is lesser than Y or
* 0 if X is equal to Y
*/
int mpi_cmp_mpi( const mpi *X, const mpi *Y );
int esp_mpi_cmp_mpi( const mpi *X, const mpi *Y );
/**
* \brief Compare signed values
@ -448,7 +447,7 @@ int mpi_cmp_mpi( const mpi *X, const mpi *Y );
* -1 if X is lesser than z or
* 0 if X is equal to z
*/
int mpi_cmp_int( const mpi *X, mpi_sint z );
int esp_mpi_cmp_int( const mpi *X, esp_mpi_sint z );
/**
* \brief Unsigned addition: X = |A| + |B|
@ -460,7 +459,7 @@ int mpi_cmp_int( const mpi *X, mpi_sint z );
* \return 0 if successful,
* ERR_MPI_ALLOC_FAILED if memory allocation failed
*/
int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
int esp_mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
/**
* \brief Unsigned subtraction: X = |A| - |B|
@ -472,7 +471,7 @@ int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
* \return 0 if successful,
* ERR_MPI_NEGATIVE_VALUE if B is greater than A
*/
int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
int esp_mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
/**
* \brief Signed addition: X = A + B
@ -484,7 +483,7 @@ int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
* \return 0 if successful,
* ERR_MPI_ALLOC_FAILED if memory allocation failed
*/
int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
int esp_mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
/**
* \brief Signed subtraction: X = A - B
@ -496,7 +495,7 @@ int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
* \return 0 if successful,
* ERR_MPI_ALLOC_FAILED if memory allocation failed
*/
int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
int esp_mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
/**
* \brief Signed addition: X = A + b
@ -508,7 +507,7 @@ int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
* \return 0 if successful,
* ERR_MPI_ALLOC_FAILED if memory allocation failed
*/
int mpi_add_int( mpi *X, const mpi *A, mpi_sint b );
int esp_mpi_add_int( mpi *X, const mpi *A, esp_mpi_sint b );
/**
* \brief Signed subtraction: X = A - b
@ -520,7 +519,7 @@ int mpi_add_int( mpi *X, const mpi *A, mpi_sint b );
* \return 0 if successful,
* ERR_MPI_ALLOC_FAILED if memory allocation failed
*/
int mpi_sub_int( mpi *X, const mpi *A, mpi_sint b );
int esp_mpi_sub_int( mpi *X, const mpi *A, esp_mpi_sint b );
/**
* \brief Baseline multiplication: X = A * B
@ -532,7 +531,7 @@ int mpi_sub_int( mpi *X, const mpi *A, mpi_sint b );
* \return 0 if successful,
* ERR_MPI_ALLOC_FAILED if memory allocation failed
*/
int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
int esp_mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
/**
* \brief Baseline multiplication: X = A * b
@ -546,7 +545,7 @@ int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
* \return 0 if successful,
* ERR_MPI_ALLOC_FAILED if memory allocation failed
*/
int mpi_mul_int( mpi *X, const mpi *A, mpi_uint b );
int esp_mpi_mul_int( mpi *X, const mpi *A, esp_mpi_uint b );
/**
* \brief Division by mpi: A = Q * B + R
@ -562,7 +561,7 @@ int mpi_mul_int( mpi *X, const mpi *A, mpi_uint b );
*
* \note Either Q or R can be NULL.
*/
int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
int esp_mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
/**
* \brief Division by int: A = Q * b + R
@ -578,7 +577,7 @@ int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
*
* \note Either Q or R can be NULL.
*/
int mpi_div_int( mpi *Q, mpi *R, const mpi *A, mpi_sint b );
int esp_mpi_div_int( mpi *Q, mpi *R, const mpi *A, esp_mpi_sint b );
/**
* \brief Modulo: R = A mod B
@ -592,12 +591,12 @@ int mpi_div_int( mpi *Q, mpi *R, const mpi *A, mpi_sint b );
* ERR_MPI_DIVISION_BY_ZERO if B == 0,
* ERR_MPI_NEGATIVE_VALUE if B < 0
*/
int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
int esp_mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
/**
* \brief Modulo: r = A mod b
*
* \param r Destination mpi_uint
* \param r Destination esp_mpi_uint
* \param A Left-hand MPI
* \param b Integer to divide by
*
@ -606,7 +605,7 @@ int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
* ERR_MPI_DIVISION_BY_ZERO if b == 0,
* ERR_MPI_NEGATIVE_VALUE if b < 0
*/
int mpi_mod_int( mpi_uint *r, const mpi *A, mpi_sint b );
int esp_mpi_mod_int( esp_mpi_uint *r, const mpi *A, esp_mpi_sint b );
/**
* \brief Sliding-window exponentiation: X = A^E mod N
@ -626,7 +625,7 @@ int mpi_mod_int( mpi_uint *r, const mpi *A, mpi_sint b );
* multiple calls, which speeds up things a bit. It can
* be set to NULL if the extra performance is unneeded.
*/
int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
int esp_mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
/**
* \brief Fill an MPI X with size bytes of random
@ -639,7 +638,7 @@ int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
* \return 0 if successful,
* ERR_MPI_ALLOC_FAILED if memory allocation failed
*/
int mpi_fill_random( mpi *X, size_t size,
int esp_mpi_fill_random( mpi *X, size_t size,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng );
@ -653,7 +652,7 @@ int mpi_fill_random( mpi *X, size_t size,
* \return 0 if successful,
* ERR_MPI_ALLOC_FAILED if memory allocation failed
*/
int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
int esp_mpi_gcd( mpi *G, const mpi *A, const mpi *B );
/**
* \brief Modular inverse: X = A^-1 mod N
@ -667,7 +666,7 @@ int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
* ERR_MPI_BAD_INPUT_DATA if N is negative or nil
ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
*/
int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
int esp_mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
/**
* \brief Miller-Rabin primality test
@ -680,7 +679,7 @@ int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
* ERR_MPI_ALLOC_FAILED if memory allocation failed,
* ERR_MPI_NOT_ACCEPTABLE if X is not prime
*/
int mpi_is_prime( const mpi *X,
int esp_mpi_is_prime( const mpi *X,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng );
@ -698,7 +697,7 @@ int mpi_is_prime( const mpi *X,
* ERR_MPI_ALLOC_FAILED if memory allocation failed,
* ERR_MPI_BAD_INPUT_DATA if nbits is < 3
*/
int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
int esp_mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng );
#endif

View file

@ -0,0 +1,56 @@
#ifndef _MULTI_THREAD_H_
#define _MULTI_THREAD_H_
#include "c_types.h"
#include "rom/ets_sys.h"
#ifdef __cplusplus
extern "C" {
#endif
enum {
AES_MUTEX = 0,
BIGNUM_MUTEX,
SHA_MUTEX,
MUTEX_MAX_NUM,
};
int esp_thread_init(void);
void esp_thread_lock(unsigned int num);
void esp_thread_unlock(unsigned int num);
void esp_thread_take(unsigned int num);
void esp_thread_give(unsigned int num);
bool esp_thread_is_used(unsigned int num);
#define MUTEX_LOCK(num) esp_thread_lock(num)
#define MUTEX_UNLOCK(num) esp_thread_unlock(num)
#define SIG_TAKE(num) esp_thread_take(num)
#define SIG_GIVE(num) esp_thread_give(num)
#define SIG_IS_USED(num) esp_thread_is_used(num)
#define AES_LOCK() MUTEX_LOCK(AES_MUTEX)
#define AES_UNLOCK() MUTEX_UNLOCK(AES_MUTEX)
#define BIGNUM_LOCK() MUTEX_LOCK(BIGNUM_MUTEX)
#define BIGNUM_UNLOCK() MUTEX_UNLOCK(BIGNUM_MUTEX)
#define SHA_LOCK() MUTEX_LOCK(SHA_MUTEX)
#define SHA_UNLOCK() MUTEX_UNLOCK(SHA_MUTEX)
#define AES_TAKE() SIG_TAKE(AES_MUTEX)
#define AES_GIVE() SIG_GIVE(AES_MUTEX)
#define AES_IS_USED() SIG_IS_USED(AES_MUTEX)
#define BIGNUM_TAKE() SIG_TAKE(BIGNUM_MUTEX)
#define BIGNUM_GIVE() SIG_GIVE(BIGNUM_MUTEX)
#define BIGNUM_IS_USED() SIG_IS_USED(BIGNUM_MUTEX)
#define SHA_TAKE() SIG_TAKE(SHA_MUTEX)
#define SHA_GIVE() SIG_GIVE(SHA_MUTEX)
#define SHA_IS_USED() SIG_IS_USED(SHA_MUTEX)
#ifdef __cplusplus
}
#endif
#endif /* esp_thread.h */

View file

@ -0,0 +1,224 @@
/*
* copyright (c) 2010 - 2012 Espressif System
*
* esf Link List Descriptor
*/
#ifndef _ESP_SHA_H_
#define _ESP_SHA_H_
#include "c_types.h"
#include "rom/ets_sys.h"
#include "rom/sha.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* \brief SHA-1 context structure
*/
typedef struct{
SHA_CTX context;
int context_type;
} sha_context;
typedef sha_context SHA1_CTX;
/**
* \brief Initialize SHA-1 context
*
* \param ctx SHA-1 context to be initialized
*/
void esp_sha1_init( SHA1_CTX *ctx );
/**
* \brief Clear SHA-1 context
*
* \param ctx SHA-1 context to be cleared
*/
void esp_sha1_free( SHA1_CTX *ctx );
/**
* \brief Clone (the state of) a SHA-1 context
*
* \param dst The destination context
* \param src The context to be cloned
*/
void esp_sha1_clone( SHA1_CTX *dst, const SHA1_CTX *src );
void esp_sha1_process(SHA1_CTX *ctx, const unsigned char data[64]);
/**
* \brief SHA-1 context setup
*
* \param ctx context to be initialized
*/
void esp_sha1_starts( SHA1_CTX *ctx );
/**
* \brief SHA-1 process buffer
*
* \param ctx SHA-1 context
* \param input buffer holding the data
* \param ilen length of the input data
*/
void esp_sha1_update( SHA1_CTX *ctx, const unsigned char *input, size_t ilen );
/**
* \brief SHA-1 final digest
*
* \param ctx SHA-1 context
* \param output SHA-1 checksum result
*/
void esp_sha1_finish( SHA1_CTX *ctx, unsigned char output[20] );
/**
* \brief Output = SHA-1( input buffer )
*
* \param input buffer holding the data
* \param ilen length of the input data
* \param output SHA-1 checksum result
*/
void esp_sha1_output( const unsigned char *input, size_t ilen, unsigned char output[20] );
///
#define SHA256 SHA2_256
#define SHA224 4
/**
* \brief SHA-256 context structure
*/
typedef sha_context SHA256_CTX;
/**
* \brief Initialize SHA-256 context
*
* \param ctx SHA-256 context to be initialized
*/
void esp_sha256_init( SHA256_CTX *ctx );
/**
* \brief Clear SHA-256 context
*
* \param ctx SHA-256 context to be cleared
*/
void esp_sha256_free( SHA256_CTX *ctx );
void esp_sha256_process(SHA256_CTX *ctx, const unsigned char data[64]);
/**
* \brief Clone (the state of) a SHA-256 context
*
* \param dst The destination context
* \param src The context to be cloned
*/
void esp_sha256_clone( SHA256_CTX *dst, const SHA256_CTX *src );
/**
* \brief SHA-256 context setup
*
* \param ctx context to be initialized
* \param is224 0 = use SHA256, 1 = use SHA224
*/
void esp_sha256_starts( SHA256_CTX *ctx, int is224 );
/**
* \brief SHA-256 process buffer
*
* \param ctx SHA-256 context
* \param input buffer holding the data
* \param ilen length of the input data
*/
void esp_sha256_update( SHA256_CTX *ctx, const unsigned char *input, size_t ilen );
/**
* \brief SHA-256 final digest
*
* \param ctx SHA-256 context
* \param output SHA-224/256 checksum result
*/
void esp_sha256_finish( SHA256_CTX *ctx, unsigned char output[32] );
/**
* \brief Output = SHA-256( input buffer )
*
* \param input buffer holding the data
* \param ilen length of the input data
* \param output SHA-224/256 checksum result
* \param is224 0 = use SHA256, 1 = use SHA224
*/
void esp_sha256_output( const unsigned char *input, size_t ilen, unsigned char output[32], int is224 );
//
/**
* \brief SHA-512 context structure
*/
typedef sha_context SHA512_CTX;
/**
* \brief Initialize SHA-512 context
*
* \param ctx SHA-512 context to be initialized
*/
void esp_sha512_init( SHA512_CTX *ctx );
/**
* \brief Clear SHA-512 context
*
* \param ctx SHA-512 context to be cleared
*/
void esp_sha512_free( SHA512_CTX *ctx );
/**
* \brief Clone (the state of) a SHA-512 context
*
* \param dst The destination context
* \param src The context to be cloned
*/
void esp_sha512_clone( SHA512_CTX *dst, const SHA512_CTX *src );
/**
* \brief SHA-512 context setup
*
* \param ctx context to be initialized
* \param is384 0 = use SHA512, 1 = use SHA384
*/
void esp_sha512_starts( SHA512_CTX *ctx, int is384 );
/**
* \brief SHA-512 process buffer
*
* \param ctx SHA-512 context
* \param input buffer holding the data
* \param ilen length of the input data
*/
void esp_sha512_update( SHA512_CTX *ctx, const unsigned char *input, size_t ilen );
/**
* \brief SHA-512 final digest
*
* \param ctx SHA-512 context
* \param output SHA-384/512 checksum result
*/
void esp_sha512_finish( SHA512_CTX *ctx, unsigned char output[64] );
/**
* \brief Output = SHA-512( input buffer )
*
* \param input buffer holding the data
* \param ilen length of the input data
* \param output SHA-384/512 checksum result
* \param is384 0 = use SHA512, 1 = use SHA384
*/
void esp_sha512_output( const unsigned char *input, size_t ilen, unsigned char output[64], int is384 );
//
#ifdef __cplusplus
}
#endif
#endif

285
components/esp32/sha.c Normal file
View file

@ -0,0 +1,285 @@
/*
* FIPS-180-1 compliant SHA-1 implementation
*
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
* 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 "sha.h"
#include <string.h>
#include "esp_thread.h"
/* Implementation that should never be optimized out by the compiler */
static void esp_sha_zeroize( void *v, size_t n ) {
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
}
void esp_sha1_init( SHA1_CTX *ctx )
{
memset( ctx, 0, sizeof( SHA1_CTX ) );
SHA_LOCK();
SHA_TAKE();
ets_sha_enable();
SHA_UNLOCK();
}
void esp_sha1_free( SHA1_CTX *ctx )
{
if( ctx == NULL )
return;
esp_sha_zeroize( ctx, sizeof( SHA1_CTX ) );
SHA_LOCK();
SHA_GIVE();
if (false == SHA_IS_USED())
ets_sha_disable();
SHA_UNLOCK();
}
void esp_sha1_clone( SHA1_CTX *dst, const SHA1_CTX *src )
{
*dst = *src;
}
void esp_sha1_process(SHA1_CTX *ctx, const unsigned char data[64])
{
}
/*
* SHA-1 context setup
*/
void esp_sha1_starts( SHA1_CTX *ctx )
{
SHA_LOCK();
ets_sha_init(&ctx->context);
SHA_UNLOCK();
ctx->context_type = SHA1;
}
/*
* SHA-1 process buffer
*/
void esp_sha1_update( SHA1_CTX *ctx, const unsigned char *input, size_t ilen )
{
SHA_LOCK();
ets_sha_update(&ctx->context, ctx->context_type, input, ilen * 8);
}
/*
* SHA-1 final digest
*/
void esp_sha1_finish( SHA1_CTX *ctx, unsigned char output[20] )
{
ets_sha_finish(&ctx->context, ctx->context_type, output);
SHA_UNLOCK();
}
/*
* output = SHA-1( input buffer )
*/
void esp_sha1_output( const unsigned char *input, size_t ilen, unsigned char output[20] )
{
SHA1_CTX ctx;
esp_sha1_init( &ctx );
esp_sha1_starts( &ctx );
esp_sha1_update( &ctx, input, ilen );
esp_sha1_finish( &ctx, output );
esp_sha1_free( &ctx );
}
/////
/* Implementation that should never be optimized out by the compiler */
void esp_sha256_init( SHA256_CTX *ctx )
{
memset( ctx, 0, sizeof( SHA256_CTX ) );
SHA_LOCK();
SHA_TAKE();
ets_sha_enable();
SHA_UNLOCK();
}
void esp_sha256_process(SHA256_CTX *ctx, const unsigned char data[64])
{
}
void esp_sha256_free( SHA256_CTX *ctx )
{
if( ctx == NULL )
return;
esp_sha_zeroize( ctx, sizeof( SHA256_CTX ) );
SHA_LOCK();
SHA_GIVE();
if (false == SHA_IS_USED())
ets_sha_disable();
SHA_UNLOCK();
}
void esp_sha256_clone( SHA256_CTX *dst, const SHA256_CTX *src )
{
*dst = *src;
}
/*
* SHA-256 context setup
*/
void esp_sha256_starts( SHA256_CTX *ctx, int is224 )
{
SHA_LOCK();
ets_sha_init(&ctx->context);
SHA_UNLOCK();
if( is224 == 0 )
{
/* SHA-256 */
ctx->context_type = SHA256;
}else{
/* SHA-224 */
ctx->context_type = SHA224;
}
}
/*
* SHA-256 process buffer
*/
void esp_sha256_update( SHA256_CTX *ctx, const unsigned char *input, size_t ilen )
{
SHA_LOCK();
ets_sha_update(&ctx->context, ctx->context_type, input, ilen * 8);
}
/*
* SHA-256 final digest
*/
void esp_sha256_finish( SHA256_CTX *ctx, unsigned char output[32] )
{
ets_sha_finish(&ctx->context, ctx->context_type, output);
SHA_UNLOCK();
}
/*
* output = SHA-256( input buffer )
*/
void esp_sha256_output( const unsigned char *input, size_t ilen, unsigned char output[32], int is224 )
{
SHA256_CTX ctx;
esp_sha256_init( &ctx );
esp_sha256_starts( &ctx, is224 );
esp_sha256_update( &ctx, input, ilen );
esp_sha256_finish( &ctx, output );
esp_sha256_free( &ctx );
}
/////
void esp_sha512_init( SHA512_CTX *ctx )
{
memset( ctx, 0, sizeof( SHA512_CTX ) );
SHA_LOCK();
SHA_TAKE();
ets_sha_enable();
SHA_UNLOCK();
}
void esp_sha512_free( SHA512_CTX *ctx )
{
if( ctx == NULL )
return;
esp_sha_zeroize( ctx, sizeof( SHA512_CTX ) );
SHA_LOCK();
SHA_GIVE();
if (false == SHA_IS_USED())
ets_sha_disable();
SHA_UNLOCK();
}
void esp_sha512_clone( SHA512_CTX *dst, const SHA512_CTX *src )
{
*dst = *src;
}
/*
* SHA-512 context setup
*/
void esp_sha512_starts( SHA512_CTX *ctx, int is384 )
{
SHA_LOCK();
ets_sha_init(&ctx->context);
SHA_UNLOCK();
if( is384 == 0 )
{
/* SHA-512 */
ctx->context_type = SHA2_512;
}
else
{
/* SHA-384 */
ctx->context_type = SHA2_384;
}
}
/*
* SHA-512 process buffer
*/
void esp_sha512_update( SHA512_CTX *ctx, const unsigned char *input,size_t ilen )
{
SHA_LOCK();
ets_sha_update(&ctx->context, ctx->context_type, input, ilen * 8);
}
/*
* SHA-512 final digest
*/
void esp_sha512_finish( SHA512_CTX *ctx, unsigned char output[64] )
{
ets_sha_finish(&ctx->context, ctx->context_type, output);
SHA_UNLOCK();
}
/*
* output = SHA-512( input buffer )
*/
void esp_sha512_output( const unsigned char *input, size_t ilen,unsigned char output[64], int is384 )
{
SHA512_CTX ctx;
esp_sha512_init( &ctx );
esp_sha512_starts( &ctx, is384 );
esp_sha512_update( &ctx, input, ilen );
esp_sha512_finish( &ctx, output );
esp_sha512_free( &ctx );
}
////

0
components/mbedtls/Makefile Executable file → Normal file
View file

View file

@ -276,27 +276,7 @@ void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
#endif
#else /* MBEDTLS_AES_ALT */
#include "port/aes_alt.h"
typedef AES_CTX mbedtls_aes_context;
#define mbedtls_aes_init aes_init
#define mbedtls_aes_free aes_free
#define mbedtls_aes_setkey_enc aes_setkey_enc
#define mbedtls_aes_setkey_dec aes_setkey_dec
#define mbedtls_aes_crypt_ecb aes_crypt_ecb
#if defined(MBEDTLS_CIPHER_MODE_CBC)
#define mbedtls_aes_crypt_cbc aes_crypt_cbc
#endif
#if defined(MBEDTLS_CIPHER_MODE_CFB)
#define mbedtls_aes_crypt_cfb128 aes_crypt_cfb128
#define mbedtls_aes_crypt_cfb8 aes_crypt_cfb8
#endif
#if defined(MBEDTLS_CIPHER_MODE_CTR)
#define mbedtls_aes_crypt_ctr aes_crypt_ctr
#endif
#define mbedtls_aes_encrypt aes_encrypt
#define mbedtls_aes_decrypt aes_decrypt
#include "aes_alt.h"
#endif /* MBEDTLS_AES_ALT */
#ifdef __cplusplus

View file

@ -705,52 +705,7 @@ int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int dh_flag,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng );
#else /* MBEDTLS_BIGNUM_ALT */
#include "port/bignum_alt.h"
typedef mpi mbedtls_mpi;
#define mbedtls_mpi_init mpi_init
#define mbedtls_mpi_free mpi_free
#define mbedtls_mpi_grow mpi_grow
#define mbedtls_mpi_shrink mpi_shrink
#define mbedtls_mpi_copy mpi_copy
#define mbedtls_mpi_swap mpi_swap
#define mbedtls_mpi_safe_cond_assign mpi_safe_cond_assign
#define mbedtls_mpi_safe_cond_swap mpi_safe_cond_swap
#define mbedtls_mpi_lset mpi_lset
#define mbedtls_mpi_get_bit mpi_get_bit
#define mbedtls_mpi_set_bit mpi_set_bit
#define mbedtls_mpi_lsb mpi_lsb
#define mbedtls_mpi_bitlen mpi_bitlen
#define mbedtls_mpi_size mpi_size
#define mbedtls_mpi_read_string mpi_read_string
#define mbedtls_mpi_write_string mpi_write_string
#define mbedtls_mpi_read_binary mpi_read_binary
#define mbedtls_mpi_write_binary mpi_write_binary
#define mbedtls_mpi_shift_l mpi_shift_l
#define mbedtls_mpi_shift_r mpi_shift_r
#define mbedtls_mpi_cmp_abs mpi_cmp_abs
#define mbedtls_mpi_cmp_mpi mpi_cmp_mpi
#define mbedtls_mpi_cmp_int mpi_cmp_int
#define mbedtls_mpi_add_abs mpi_add_abs
#define mbedtls_mpi_sub_abs mpi_sub_abs
#define mbedtls_mpi_add_mpi mpi_add_mpi
#define mbedtls_mpi_sub_mpi mpi_sub_mpi
#define mbedtls_mpi_add_int mpi_add_int
#define mbedtls_mpi_sub_int mpi_sub_int
#define mbedtls_mpi_mul_mpi mpi_mul_mpi
#define mbedtls_mpi_mul_int mpi_mul_int
#define mbedtls_mpi_div_mpi mpi_div_mpi
#define mbedtls_mpi_div_int mpi_div_int
#define mbedtls_mpi_mod_mpi mpi_mod_mpi
#define mbedtls_mpi_mod_int mpi_mod_int
#define mbedtls_mpi_exp_mod mpi_exp_mod
#define mbedtls_mpi_fill_random mpi_fill_random
#define mbedtls_mpi_gcd mpi_gcd
#define mbedtls_mpi_inv_mod mpi_inv_mod
#define mbedtls_mpi_is_prime mpi_is_prime
#define mbedtls_mpi_gen_prime mpi_gen_prime
#include "bignum_alt.h"
#endif /* MBEDTLS_BIGNUM_ALT */
/**

View file

@ -2466,8 +2466,7 @@
//#define MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES 50 /**< Maximum entries in cache */
/* SSL options */
extern unsigned int max_content_len;
#define MBEDTLS_SSL_MAX_CONTENT_LEN max_content_len /**< Maxium fragment length in bytes, determines the size of each of the two internal I/O buffers */
#define MBEDTLS_SSL_MAX_CONTENT_LEN 3072 /**< Maxium fragment length in bytes, determines the size of each of the two internal I/O buffers */
//#define MBEDTLS_SSL_DEFAULT_TICKET_LIFETIME 86400 /**< Lifetime of session tickets (if enabled) */
//#define MBEDTLS_PSK_MAX_LEN 32 /**< Max size of TLS pre-shared keys, in bytes (default 256 bits) */
//#define MBEDTLS_SSL_COOKIE_TIMEOUT 60 /**< Default expiration delay of DTLS cookies, in seconds if HAVE_TIME, or in number of cookies issued */

View file

@ -106,17 +106,7 @@ void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[6
#endif
#else /* MBEDTLS_SHA1_ALT */
#include "port/sha1_alt.h"
typedef SHA1_CTX mbedtls_sha1_context;
#define mbedtls_sha1_init sha1_init
#define mbedtls_sha1_starts sha1_starts
#define mbedtls_sha1_clone sha1_clone
#define mbedtls_sha1_update sha1_update
#define mbedtls_sha1_finish sha1_finish
#define mbedtls_sha1_free sha1_free
#define mbedtls_sha1_process sha1_process
#include "sha1_alt.h"
#endif /* MBEDTLS_SHA1_ALT */
#ifdef __cplusplus

View file

@ -109,17 +109,7 @@ void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char da
#endif
#else /* MBEDTLS_SHA256_ALT */
#include "port/sha256_alt.h"
typedef SHA256_CTX mbedtls_sha256_context;
#define mbedtls_sha256_init sha256_init
#define mbedtls_sha256_clone sha256_clone
#define mbedtls_sha256_starts sha256_starts
#define mbedtls_sha256_update sha256_update
#define mbedtls_sha256_finish sha256_finish
#define mbedtls_sha256_free sha256_free
#define mbedtls_sha256_process sha256_process
#include "sha256_alt.h"
#endif /* MBEDTLS_SHA256_ALT */
#ifdef __cplusplus

View file

@ -106,17 +106,7 @@ void mbedtls_sha512_finish( mbedtls_sha512_context *ctx, unsigned char output[64
#endif
#else /* MBEDTLS_SHA512_ALT */
#include "port/sha512_alt.h"
typedef SHA512_CTX mbedtls_sha512_context;
#define mbedtls_sha512_init sha512_init
#define mbedtls_sha512_clone sha512_clone
#define mbedtls_sha512_starts sha512_starts
#define mbedtls_sha512_update sha512_update
#define mbedtls_sha512_finish sha512_finish
#define mbedtls_sha512_free sha512_free
#include "sha512_alt.h"
#endif /* MBEDTLS_SHA512_ALT */
#ifdef __cplusplus

View file

@ -1,63 +0,0 @@
#ifndef _MULTI_THREAD_H_
#define _MULTI_THREAD_H_
#ifdef __cplusplus
extern "C" {
#endif
enum {
AES_MUTEX = 0,
BIGNUM_MUTEX,
SHA_MUTEX,
MUTEX_MAX_NUM,
};
int multi_thread_init(void);
void multi_thread_lock(unsigned int num);
void multi_thread_unlock(unsigned int num);
void multi_thread_take(unsigned int num);
void multi_thread_give(unsigned int num);
bool multi_thread_is_used(num);
#define MUTEX_LOCK(num) multi_thread_lock(num)
#define MUTEX_UNLOCK(num) multi_thread_unlock(num)
#define SIG_TAKE(num) multi_thread_take(num)
#define SIG_GIVE(num) multi_thread_give(num)
#define SIG_IS_USED(num) multi_thread_is_used(num)
#define AES_LOCK() MUTEX_LOCK(AES_MUTEX)
#define AES_UNLOCK() MUTEX_UNLOCK(AES_MUTEX)
#define BIGNUM_LOCK() MUTEX_LOCK(BIGNUM_MUTEX)
#define BIGNUM_UNLOCK() MUTEX_UNLOCK(BIGNUM_MUTEX)
#define SHA1_LOCK() MUTEX_LOCK(SHA_MUTEX)
#define SHA1_UNLOCK() MUTEX_UNLOCK(SHA_MUTEX)
#define SHA256_LOCK() MUTEX_LOCK(SHA_MUTEX)
#define SHA256_UNLOCK() MUTEX_UNLOCK(SHA_MUTEX)
#define SHA512_LOCK() MUTEX_LOCK(SHA_MUTEX)
#define SHA512_UNLOCK() MUTEX_UNLOCK(SHA_MUTEX)
#define AES_TAKE() SIG_TAKE(AES_MUTEX)
#define AES_GIVE() SIG_GIVE(AES_MUTEX)
#define AES_IS_USED() SIG_IS_USED(AES_MUTEX)
#define BIGNUM_TAKE() SIG_TAKE(BIGNUM_MUTEX)
#define BIGNUM_GIVE() SIG_GIVE(BIGNUM_MUTEX)
#define BIGNUM_IS_USED() SIG_IS_USED(BIGNUM_MUTEX)
#define SHA1_TAKE() SIG_TAKE(SHA_MUTEX)
#define SHA1_GIVE() SIG_GIVE(SHA_MUTEX)
#define SHA1_IS_USED() SIG_IS_USED(SHA_MUTEX)
#define SHA256_TAKE() SIG_TAKE(SHA_MUTEX)
#define SHA256_GIVE() SIG_GIVE(SHA_MUTEX)
#define SHA256_IS_USED() SIG_IS_USED(SHA_MUTEX)
#define SHA512_TAKE() SIG_TAKE(SHA_MUTEX)
#define SHA512_GIVE() SIG_GIVE(SHA_MUTEX)
#define SHA512_IS_USED() SIG_IS_USED(SHA_MUTEX)
#ifdef __cplusplus
}
#endif
#endif /* multi_thread.h */

View file

@ -1,93 +0,0 @@
/*
* copyright (c) 2010 - 2012 Espressif System
*
* esf Link List Descriptor
*/
#ifndef _SHA1_H_
#define _SHA1_H_
#include "c_types.h"
#include "rom/ets_sys.h"
#include "rom/sha.h"
#ifdef __cplusplus
extern "C" {
#endif
#define ESP_SHA1_C
#define SHA1 0
/**
* \brief SHA-1 context structure
*/
typedef struct{
SHA_CTX context;
int context_type;
} sha1_context;
typedef sha1_context SHA1_CTX;
/**
* \brief Initialize SHA-1 context
*
* \param ctx SHA-1 context to be initialized
*/
void sha1_init( SHA1_CTX *ctx );
/**
* \brief Clear SHA-1 context
*
* \param ctx SHA-1 context to be cleared
*/
void sha1_free( SHA1_CTX *ctx );
/**
* \brief Clone (the state of) a SHA-1 context
*
* \param dst The destination context
* \param src The context to be cloned
*/
void sha1_clone( SHA1_CTX *dst, const SHA1_CTX *src );
void sha1_process(SHA1_CTX *ctx, const unsigned char data[64]);
/**
* \brief SHA-1 context setup
*
* \param ctx context to be initialized
*/
void sha1_starts( SHA1_CTX *ctx );
/**
* \brief SHA-1 process buffer
*
* \param ctx SHA-1 context
* \param input buffer holding the data
* \param ilen length of the input data
*/
void sha1_update( SHA1_CTX *ctx, const unsigned char *input, size_t ilen );
/**
* \brief SHA-1 final digest
*
* \param ctx SHA-1 context
* \param output SHA-1 checksum result
*/
void sha1_finish( SHA1_CTX *ctx, unsigned char output[20] );
/**
* \brief Output = SHA-1( input buffer )
*
* \param input buffer holding the data
* \param ilen length of the input data
* \param output SHA-1 checksum result
*/
void sha1_output( const unsigned char *input, size_t ilen, unsigned char output[20] );
#ifdef __cplusplus
}
#endif
#endif

View file

@ -1,95 +0,0 @@
/*
* copyright (c) 2010 - 2012 Espressif System
*
* esf Link List Descriptor
*/
#ifndef _SHA256_H_
#define _SHA256_H_
#include "c_types.h"
#include "rom/ets_sys.h"
#include "rom/sha.h"
#ifdef __cplusplus
extern "C" {
#endif
#define ESP_SHA256_C
#define SHA256 SHA2_256
#define SHA224 4
/**
* \brief SHA-256 context structure
*/
typedef struct{
SHA_CTX context;
int context_type;
}sha256_context;
typedef sha256_context SHA256_CTX;
/**
* \brief Initialize SHA-256 context
*
* \param ctx SHA-256 context to be initialized
*/
void sha256_init( SHA256_CTX *ctx );
/**
* \brief Clear SHA-256 context
*
* \param ctx SHA-256 context to be cleared
*/
void sha256_free( SHA256_CTX *ctx );
void sha256_process(SHA256_CTX *ctx, const unsigned char data[64]);
/**
* \brief Clone (the state of) a SHA-256 context
*
* \param dst The destination context
* \param src The context to be cloned
*/
void sha256_clone( SHA256_CTX *dst, const SHA256_CTX *src );
/**
* \brief SHA-256 context setup
*
* \param ctx context to be initialized
* \param is224 0 = use SHA256, 1 = use SHA224
*/
void sha256_starts( SHA256_CTX *ctx, int is224 );
/**
* \brief SHA-256 process buffer
*
* \param ctx SHA-256 context
* \param input buffer holding the data
* \param ilen length of the input data
*/
void sha256_update( SHA256_CTX *ctx, const unsigned char *input, size_t ilen );
/**
* \brief SHA-256 final digest
*
* \param ctx SHA-256 context
* \param output SHA-224/256 checksum result
*/
void sha256_finish( SHA256_CTX *ctx, unsigned char output[32] );
/**
* \brief Output = SHA-256( input buffer )
*
* \param input buffer holding the data
* \param ilen length of the input data
* \param output SHA-224/256 checksum result
* \param is224 0 = use SHA256, 1 = use SHA224
*/
void sha256_output( const unsigned char *input, size_t ilen, unsigned char output[32], int is224 );
#ifdef __cplusplus
}
#endif
#endif /* sha256.h */

View file

@ -1,92 +0,0 @@
/*
* copyright (c) 2010 - 2012 Espressif System
*
* esf Link List Descriptor
*/
#ifndef _SHA512_H_
#define _SHA512_H_
#include "c_types.h"
#include "rom/ets_sys.h"
#include "rom/sha.h"
#ifdef __cplusplus
extern "C" {
#endif
#define ESP_SHA512_C
/**
* \brief SHA-512 context structure
*/
typedef struct{
SHA_CTX context;
int context_type;
}sha512_context;
typedef sha512_context SHA512_CTX;
/**
* \brief Initialize SHA-512 context
*
* \param ctx SHA-512 context to be initialized
*/
void sha512_init( SHA512_CTX *ctx );
/**
* \brief Clear SHA-512 context
*
* \param ctx SHA-512 context to be cleared
*/
void sha512_free( SHA512_CTX *ctx );
/**
* \brief Clone (the state of) a SHA-512 context
*
* \param dst The destination context
* \param src The context to be cloned
*/
void sha512_clone( SHA512_CTX *dst, const SHA512_CTX *src );
/**
* \brief SHA-512 context setup
*
* \param ctx context to be initialized
* \param is384 0 = use SHA512, 1 = use SHA384
*/
void sha512_starts( SHA512_CTX *ctx, int is384 );
/**
* \brief SHA-512 process buffer
*
* \param ctx SHA-512 context
* \param input buffer holding the data
* \param ilen length of the input data
*/
void sha512_update( SHA512_CTX *ctx, const unsigned char *input, size_t ilen );
/**
* \brief SHA-512 final digest
*
* \param ctx SHA-512 context
* \param output SHA-384/512 checksum result
*/
void sha512_finish( SHA512_CTX *ctx, unsigned char output[64] );
/**
* \brief Output = SHA-512( input buffer )
*
* \param input buffer holding the data
* \param ilen length of the input data
* \param output SHA-384/512 checksum result
* \param is384 0 = use SHA512, 1 = use SHA384
*/
void sha512_output( const unsigned char *input, size_t ilen, unsigned char output[64], int is384 );
#ifdef __cplusplus
}
#endif
#endif /* sha512.h */

View file

@ -0,0 +1,59 @@
/**
* \file aes_alt.h
*
* \brief AES block cipher
*
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
* 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.
*
*
*/
#ifndef AES_ALT_H
#define AES_ALT_H
#ifdef __cplusplus
extern "C" {
#endif
#if defined(MBEDTLS_AES_ALT)
#include "aes.h"
typedef AES_CTX mbedtls_aes_context;
#define mbedtls_aes_init esp_aes_init
#define mbedtls_aes_free esp_aes_free
#define mbedtls_aes_setkey_enc esp_aes_setkey_enc
#define mbedtls_aes_setkey_dec esp_aes_setkey_dec
#define mbedtls_aes_crypt_ecb esp_aes_crypt_ecb
#if defined(MBEDTLS_CIPHER_MODE_CBC)
#define mbedtls_aes_crypt_cbc esp_aes_crypt_cbc
#endif
#if defined(MBEDTLS_CIPHER_MODE_CFB)
#define mbedtls_aes_crypt_cfb128 esp_aes_crypt_cfb128
#define mbedtls_aes_crypt_cfb8 esp_aes_crypt_cfb8
#endif
#if defined(MBEDTLS_CIPHER_MODE_CTR)
#define mbedtls_aes_crypt_ctr esp_aes_crypt_ctr
#endif
#define mbedtls_aes_encrypt esp_aes_encrypt
#define mbedtls_aes_decrypt esp_aes_decrypt
#endif /* MBEDTLS_AES_ALT */
#ifdef __cplusplus
}
#endif
#endif /* aes.h */

View file

@ -0,0 +1,77 @@
/**
* \file bignum_alt.h
*
* \brief Multi-precision integer library
*
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
* 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.
*
*/
#ifndef BIGNUM_ALT_H
#define BIGNUM_ALT_H
#include "bignum.h"
#if defined(MBEDTLS_BIGNUM_ALT)
typedef MPI_CTX mbedtls_mpi;
#define mbedtls_mpi_init esp_mpi_init
#define mbedtls_mpi_free esp_mpi_free
#define mbedtls_mpi_grow esp_mpi_grow
#define mbedtls_mpi_shrink esp_mpi_shrink
#define mbedtls_mpi_copy esp_mpi_copy
#define mbedtls_mpi_swap esp_mpi_swap
#define mbedtls_mpi_safe_cond_assign esp_mpi_safe_cond_assign
#define mbedtls_mpi_safe_cond_swap esp_mpi_safe_cond_swap
#define mbedtls_mpi_lset esp_mpi_lset
#define mbedtls_mpi_get_bit esp_mpi_get_bit
#define mbedtls_mpi_set_bit esp_mpi_set_bit
#define mbedtls_mpi_lsb esp_mpi_lsb
#define mbedtls_mpi_bitlen esp_mpi_bitlen
#define mbedtls_mpi_size esp_mpi_size
#define mbedtls_mpi_read_string esp_mpi_read_string
#define mbedtls_mpi_write_string esp_mpi_write_string
#define mbedtls_mpi_read_binary esp_mpi_read_binary
#define mbedtls_mpi_write_binary esp_mpi_write_binary
#define mbedtls_mpi_shift_l esp_mpi_shift_l
#define mbedtls_mpi_shift_r esp_mpi_shift_r
#define mbedtls_mpi_cmp_abs esp_mpi_cmp_abs
#define mbedtls_mpi_cmp_mpi esp_mpi_cmp_mpi
#define mbedtls_mpi_cmp_int esp_mpi_cmp_int
#define mbedtls_mpi_add_abs esp_mpi_add_abs
#define mbedtls_mpi_sub_abs esp_mpi_sub_abs
#define mbedtls_mpi_add_mpi esp_mpi_add_mpi
#define mbedtls_mpi_sub_mpi esp_mpi_sub_mpi
#define mbedtls_mpi_add_int esp_mpi_add_int
#define mbedtls_mpi_sub_int esp_mpi_sub_int
#define mbedtls_mpi_mul_mpi esp_mpi_mul_mpi
#define mbedtls_mpi_mul_int esp_mpi_mul_int
#define mbedtls_mpi_div_mpi esp_mpi_div_mpi
#define mbedtls_mpi_div_int esp_mpi_div_int
#define mbedtls_mpi_mod_mpi esp_mpi_mod_mpi
#define mbedtls_mpi_mod_int esp_mpi_mod_int
#define mbedtls_mpi_exp_mod esp_mpi_exp_mod
#define mbedtls_mpi_fill_random esp_mpi_fill_random
#define mbedtls_mpi_gcd esp_mpi_gcd
#define mbedtls_mpi_inv_mod esp_mpi_inv_mod
#define mbedtls_mpi_is_prime esp_mpi_is_prime
#define mbedtls_mpi_gen_prime esp_mpi_gen_prime
#endif
#endif

View file

@ -0,0 +1,34 @@
/*
* copyright (c) 2010 - 2012 Espressif System
*
* esf Link List Descriptor
*/
#ifndef _SHA1_ALT_H_
#define _SHA1_ALT_H_
#ifdef __cplusplus
extern "C" {
#endif
#if defined(MBEDTLS_SHA1_ALT)
#include "sha.h"
typedef SHA1_CTX mbedtls_sha1_context;
#define mbedtls_sha1_init esp_sha1_init
#define mbedtls_sha1_starts esp_sha1_starts
#define mbedtls_sha1_clone esp_sha1_clone
#define mbedtls_sha1_update esp_sha1_update
#define mbedtls_sha1_finish esp_sha1_finish
#define mbedtls_sha1_free esp_sha1_free
#define mbedtls_sha1_process esp_sha1_process
#endif
#ifdef __cplusplus
}
#endif
#endif

View file

@ -0,0 +1,34 @@
/*
* copyright (c) 2010 - 2012 Espressif System
*
* esf Link List Descriptor
*/
#ifndef _SHA256_ALT_H_
#define _SHA256_ALT_H_
#ifdef __cplusplus
extern "C" {
#endif
#if defined(MBEDTLS_SHA256_ALT)
#include "sha.h"
typedef SHA256_CTX mbedtls_sha256_context;
#define mbedtls_sha256_init esp_sha256_init
#define mbedtls_sha256_clone esp_sha256_clone
#define mbedtls_sha256_starts esp_sha256_starts
#define mbedtls_sha256_update esp_sha256_update
#define mbedtls_sha256_finish esp_sha256_finish
#define mbedtls_sha256_free esp_sha256_free
#define mbedtls_sha256_process esp_sha256_process
#endif
#ifdef __cplusplus
}
#endif
#endif /* sha256.h */

View file

@ -0,0 +1,32 @@
/*
* copyright (c) 2010 - 2012 Espressif System
*
* esf Link List Descriptor
*/
#ifndef _SHA512_ALT_H_
#define _SHA512_ALT_H_
#ifdef __cplusplus
extern "C" {
#endif
#if defined(MBEDTLS_SHA512_ALT)
#include "sha.h"
typedef SHA512_CTX mbedtls_sha512_context;
#define mbedtls_sha512_init esp_sha512_init
#define mbedtls_sha512_clone esp_sha512_clone
#define mbedtls_sha512_starts esp_sha512_starts
#define mbedtls_sha512_update esp_sha512_update
#define mbedtls_sha512_finish esp_sha512_finish
#define mbedtls_sha512_free esp_sha512_free
#endif
#ifdef __cplusplus
}
#endif
#endif /* sha512.h */

View file

@ -1,53 +0,0 @@
#include "multi_thread.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
static xSemaphoreHandle multi_thread_mutex[MUTEX_MAX_NUM];
static int multi_thread_sig[MUTEX_MAX_NUM];
int multi_thread_init(void)
{
int i;
for (i = 0; i < MUTEX_MAX_NUM; i++) {
multi_thread_mutex[i] = xSemaphoreCreateMutex();
if (!multi_thread_mutex[i]) {
goto failed1;
}
multi_thread_sig[i] = 0;
}
return 0;
failed1:
for (i--; i >= 0; i--)
vQueueDelete(multi_thread_mutex[i]);
return -1;
}
void multi_thread_lock(unsigned int num)
{
xSemaphoreTake(multi_thread_mutex[num], portMAX_DELAY);
}
void multi_thread_unlock(unsigned int num)
{
xSemaphoreGive(multi_thread_mutex[num]);
}
void multi_thread_take(unsigned int num)
{
multi_thread_sig[num]++;
}
void multi_thread_give(unsigned int num)
{
multi_thread_sig[num]--;
}
bool multi_thread_is_used(num)
{
return (multi_thread_sig[num] != 0) ? true : false;
}

View file

@ -1,117 +0,0 @@
/*
* FIPS-180-1 compliant SHA-1 implementation
*
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
* 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 "port/sha1_alt.h"
#if defined(ESP_SHA1_C)
#include <string.h>
#include "multi_thread.h"
/* Implementation that should never be optimized out by the compiler */
static void sha1_zeroize( void *v, size_t n ) {
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
}
void sha1_init( SHA1_CTX *ctx )
{
memset( ctx, 0, sizeof( SHA1_CTX ) );
SHA1_LOCK();
SHA1_TAKE();
ets_sha_enable();
SHA1_UNLOCK();
}
void sha1_free( SHA1_CTX *ctx )
{
if( ctx == NULL )
return;
sha1_zeroize( ctx, sizeof( SHA1_CTX ) );
SHA1_LOCK();
SHA1_GIVE();
if (false == SHA1_IS_USED())
ets_sha_disable();
SHA1_UNLOCK();
}
void sha1_clone( SHA1_CTX *dst, const SHA1_CTX *src )
{
*dst = *src;
}
void sha1_process(SHA1_CTX *ctx, const unsigned char data[64])
{
}
/*
* SHA-1 context setup
*/
void sha1_starts( SHA1_CTX *ctx )
{
SHA1_LOCK();
ets_sha_init(&ctx->context);
SHA1_UNLOCK();
ctx->context_type = SHA1;
}
/*
* SHA-1 process buffer
*/
void sha1_update( SHA1_CTX *ctx, const unsigned char *input, size_t ilen )
{
SHA1_LOCK();
ets_sha_update(&ctx->context, ctx->context_type, input, ilen * 8);
}
/*
* SHA-1 final digest
*/
void sha1_finish( SHA1_CTX *ctx, unsigned char output[20] )
{
ets_sha_finish(&ctx->context, ctx->context_type, output);
SHA1_UNLOCK();
}
/*
* output = SHA-1( input buffer )
*/
void sha1_output( const unsigned char *input, size_t ilen, unsigned char output[20] )
{
SHA1_CTX ctx;
sha1_init( &ctx );
sha1_starts( &ctx );
sha1_update( &ctx, input, ilen );
sha1_finish( &ctx, output );
sha1_free( &ctx );
}
#endif /* _SHA1_C */

View file

@ -1,122 +0,0 @@
/*
* FIPS-180-2 compliant SHA-256 implementation
*
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
* 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
*/
#include "port/sha256_alt.h"
#if defined(ESP_SHA256_C)
#include <string.h>
#include "multi_thread.h"
/* Implementation that should never be optimized out by the compiler */
static void sha256_zeroize( void *v, size_t n ) {
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
}
void sha256_init( SHA256_CTX *ctx )
{
memset( ctx, 0, sizeof( SHA256_CTX ) );
SHA256_LOCK();
SHA256_TAKE();
ets_sha_enable();
SHA256_UNLOCK();
}
void sha256_process(SHA256_CTX *ctx, const unsigned char data[64])
{
}
void sha256_free( SHA256_CTX *ctx )
{
if( ctx == NULL )
return;
sha256_zeroize( ctx, sizeof( SHA256_CTX ) );
SHA256_LOCK();
SHA256_GIVE();
if (false == SHA256_IS_USED())
ets_sha_disable();
SHA256_UNLOCK();
}
void sha256_clone( SHA256_CTX *dst, const SHA256_CTX *src )
{
*dst = *src;
}
/*
* SHA-256 context setup
*/
void sha256_starts( SHA256_CTX *ctx, int is224 )
{
SHA256_LOCK();
ets_sha_init(&ctx->context);
SHA256_UNLOCK();
if( is224 == 0 )
{
/* SHA-256 */
ctx->context_type = SHA256;
}else{
/* SHA-224 */
ctx->context_type = SHA224;
}
}
/*
* SHA-256 process buffer
*/
void sha256_update( SHA256_CTX *ctx, const unsigned char *input, size_t ilen )
{
SHA256_LOCK();
ets_sha_update(&ctx->context, ctx->context_type, input, ilen * 8);
}
/*
* SHA-256 final digest
*/
void sha256_finish( SHA256_CTX *ctx, unsigned char output[32] )
{
ets_sha_finish(&ctx->context, ctx->context_type, output);
SHA256_UNLOCK();
}
/*
* output = SHA-256( input buffer )
*/
void sha256_output( const unsigned char *input, size_t ilen, unsigned char output[32], int is224 )
{
SHA256_CTX ctx;
sha256_init( &ctx );
sha256_starts( &ctx, is224 );
sha256_update( &ctx, input, ilen );
sha256_finish( &ctx, output );
sha256_free( &ctx );
}
#endif /* SHA256_C */

View file

@ -1,117 +0,0 @@
/*
* FIPS-180-2 compliant SHA-384/512 implementation
*
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
* 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
*/
#include "port/sha512_alt.h"
#if defined(ESP_SHA512_C)
#include <string.h>
#include "multi_thread.h"
/* Implementation that should never be optimized out by the compiler */
static void sha512_zeroize( void *v, size_t n ) {
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
}
void sha512_init( SHA512_CTX *ctx )
{
memset( ctx, 0, sizeof( SHA512_CTX ) );
SHA512_LOCK();
SHA512_TAKE();
ets_sha_enable();
SHA512_UNLOCK();
}
void sha512_free( SHA512_CTX *ctx )
{
if( ctx == NULL )
return;
sha512_zeroize( ctx, sizeof( SHA512_CTX ) );
SHA512_LOCK();
SHA512_GIVE();
if (false == SHA512_IS_USED())
ets_sha_disable();
SHA512_UNLOCK();
}
void sha512_clone( SHA512_CTX *dst, const SHA512_CTX *src )
{
*dst = *src;
}
/*
* SHA-512 context setup
*/
void sha512_starts( SHA512_CTX *ctx, int is384 )
{
SHA512_LOCK();
ets_sha_init(&ctx->context);
SHA512_UNLOCK();
if( is384 == 0 )
{
/* SHA-512 */
ctx->context_type = SHA2_512;
}
else
{
/* SHA-384 */
ctx->context_type = SHA2_384;
}
}
/*
* SHA-512 process buffer
*/
void sha512_update( SHA512_CTX *ctx, const unsigned char *input,size_t ilen )
{
SHA512_LOCK();
ets_sha_update(&ctx->context, ctx->context_type, input, ilen * 8);
}
/*
* SHA-512 final digest
*/
void sha512_finish( SHA512_CTX *ctx, unsigned char output[64] )
{
ets_sha_finish(&ctx->context, ctx->context_type, output);
SHA512_UNLOCK();
}
/*
* output = SHA-512( input buffer )
*/
void sha512_output( const unsigned char *input, size_t ilen,unsigned char output[64], int is384 )
{
SHA512_CTX ctx;
sha512_init( &ctx );
sha512_starts( &ctx, is384 );
sha512_update( &ctx, input, ilen );
sha512_finish( &ctx, output );
sha512_free( &ctx );
}
#endif /* SHA512_C */