* 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 ) );
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();
/*
* 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;
/*
* 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;
}
-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)
{
}
* 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] )
{
* 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] )
{
/*
* 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;
}
/*
* 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],
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] );
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;
length -= 16;
}
}
- aes_process_disable(ctx, mode);
+ esp_aes_process_disable(ctx, mode);
AES_UNLOCK();
/*
* 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,
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] );
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++ );
}
*iv_off = n;
- aes_process_disable(ctx, mode);
+ esp_aes_process_disable(ctx, mode);
AES_UNLOCK();
/*
* 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],
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;
memcpy( iv, ov + 1, 16 );
}
- aes_process_disable(ctx, mode);
+ esp_aes_process_disable(ctx, mode);
AES_UNLOCK();
/*
* 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],
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 )
}
*nc_off = n;
- aes_process_disable(ctx, AES_ENCRYPT);
+ esp_aes_process_disable(ctx, AES_ENCRYPT);
AES_UNLOCK();
return 0;
}
-#endif /* AES_ALT_C */
-
* https://gmplib.org/manual/index.html
*
*/
-#include "port/bignum_alt.h"
+#include "bignum.h"
#if defined(ESP_BIGNUM_ALT)
#include <string.h>
#include <stdlib.h>
-#include "multi_thread.h"
+#include "esp_thread.h"
/* Implementation that should never be optimized out by the compiler */
-static void mpi_zeroize( void *v, size_t n ) {
+static void esp_mpi_zeroize( void *v, size_t n ) {
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
}
-#define ciL (sizeof(mpi_uint)) /* chars in limb */
+#define ciL (sizeof(esp_mpi_uint)) /* chars in limb */
#define biL (ciL << 3) /* bits in limb */
#define biH (ciL << 2) /* half limb size */
/*
* Initialize one MPI
*/
-void mpi_init( mpi *X )
+void esp_mpi_init( mpi *X )
{
if( X == NULL )
return;
/*
* Unallocate one MPI
*/
-void mpi_free( mpi *X )
+void esp_mpi_free( mpi *X )
{
if( X == NULL )
return;
if( X->p != NULL )
{
- mpi_zeroize( X->p, X->n * ciL );
+ esp_mpi_zeroize( X->p, X->n * ciL );
free( X->p );
}
/*
* Enlarge to the specified number of limbs
*/
-int mpi_grow( mpi *X, size_t nblimbs )
+int esp_mpi_grow( mpi *X, size_t nblimbs )
{
- mpi_uint *p;
+ esp_mpi_uint *p;
if( nblimbs > MPI_MAX_LIMBS )
return( ERR_MPI_ALLOC_FAILED );
if( X->p != NULL )
{
memcpy( p, X->p, X->n * ciL );
- mpi_zeroize( X->p, X->n * ciL );
+ esp_mpi_zeroize( X->p, X->n * ciL );
free( X->p );
}
* Resize down as much as possible,
* while keeping at least the specified number of limbs
*/
-int mpi_shrink( mpi *X, size_t nblimbs )
+int esp_mpi_shrink( mpi *X, size_t nblimbs )
{
- mpi_uint *p;
+ esp_mpi_uint *p;
size_t i;
/* Actually resize up in this case */
if( X->n <= nblimbs )
- return( mpi_grow( X, nblimbs ) );
+ return( esp_mpi_grow( X, nblimbs ) );
for( i = X->n - 1; i > 0; i-- )
if( X->p[i] != 0 )
if( X->p != NULL )
{
memcpy( p, X->p, i * ciL );
- mpi_zeroize( X->p, X->n * ciL );
+ esp_mpi_zeroize( X->p, X->n * ciL );
free( X->p );
}
/*
* Copy the contents of Y into X
*/
-int mpi_copy( mpi *X, const mpi *Y )
+int esp_mpi_copy( mpi *X, const mpi *Y )
{
int ret;
size_t i;
if( Y->p == NULL )
{
- mpi_free( X );
+ esp_mpi_free( X );
return( 0 );
}
X->s = Y->s;
- MPI_CHK( mpi_grow( X, i ) );
+ MPI_CHK( esp_mpi_grow( X, i ) );
memset( X->p, 0, X->n * ciL );
memcpy( X->p, Y->p, i * ciL );
/*
* Swap the contents of X and Y
*/
-void mpi_swap( mpi *X, mpi *Y )
+void esp_mpi_swap( mpi *X, mpi *Y )
{
mpi T;
* about whether the assignment was made or not.
* (Leaking information about the respective sizes of X and Y is ok however.)
*/
-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 )
{
int ret = 0;
size_t i;
/* make sure assign is 0 or 1 in a time-constant manner */
assign = (assign | (unsigned char)-assign) >> 7;
- MPI_CHK( mpi_grow( X, Y->n ) );
+ MPI_CHK( esp_mpi_grow( X, Y->n ) );
X->s = X->s * ( 1 - assign ) + Y->s * assign;
* Here it is not ok to simply swap the pointers, which whould lead to
* different memory access patterns when X and Y are used afterwards.
*/
-int mpi_safe_cond_swap( mpi *X, mpi *Y, unsigned char swap )
+int esp_mpi_safe_cond_swap( mpi *X, mpi *Y, unsigned char swap )
{
int ret, s;
size_t i;
- mpi_uint tmp;
+ esp_mpi_uint tmp;
if( X == Y )
return( 0 );
/* make sure swap is 0 or 1 in a time-constant manner */
swap = (swap | (unsigned char)-swap) >> 7;
- MPI_CHK( mpi_grow( X, Y->n ) );
- MPI_CHK( mpi_grow( Y, X->n ) );
+ MPI_CHK( esp_mpi_grow( X, Y->n ) );
+ MPI_CHK( esp_mpi_grow( Y, X->n ) );
s = X->s;
X->s = X->s * ( 1 - swap ) + Y->s * swap;
/*
* Set value from integer
*/
-int mpi_lset( mpi *X, mpi_sint z )
+int esp_mpi_lset( mpi *X, esp_mpi_sint z )
{
int ret;
- MPI_CHK( mpi_grow( X, 1 ) );
+ MPI_CHK( esp_mpi_grow( X, 1 ) );
memset( X->p, 0, X->n * ciL );
X->p[0] = ( z < 0 ) ? -z : z;
/*
* Get a specific bit
*/
-int mpi_get_bit( const mpi *X, size_t pos )
+int esp_mpi_get_bit( const mpi *X, size_t pos )
{
if( X->n * biL <= pos )
return( 0 );
/*
* Set a bit to a specific value of 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 )
{
int ret = 0;
size_t off = pos / biL;
if( val == 0 )
return( 0 );
- MPI_CHK( mpi_grow( X, off + 1 ) );
+ MPI_CHK( esp_mpi_grow( X, off + 1 ) );
}
- X->p[off] &= ~( (mpi_uint) 0x01 << idx );
- X->p[off] |= (mpi_uint) val << idx;
+ X->p[off] &= ~( (esp_mpi_uint) 0x01 << idx );
+ X->p[off] |= (esp_mpi_uint) val << idx;
cleanup:
/*
* Return the number of less significant zero-bits
*/
-size_t mpi_lsb( const mpi *X )
+size_t esp_mpi_lsb( const mpi *X )
{
size_t i, j, count = 0;
/*
* Count leading zero bits in a given integer
*/
-static size_t clz( const mpi_uint x )
+static size_t clz( const esp_mpi_uint x )
{
size_t j;
- mpi_uint mask = (mpi_uint) 1 << (biL - 1);
+ esp_mpi_uint mask = (esp_mpi_uint) 1 << (biL - 1);
for( j = 0; j < biL; j++ )
{
/*
* Return the number of bits
*/
-size_t mpi_bitlen( const mpi *X )
+size_t esp_mpi_bitlen( const mpi *X )
{
size_t i, j;
/*
* Return the total size in bytes
*/
-size_t mpi_size( const mpi *X )
+size_t esp_mpi_size( const mpi *X )
{
- return( ( mpi_bitlen( X ) + 7 ) >> 3 );
+ return( ( esp_mpi_bitlen( X ) + 7 ) >> 3 );
}
/*
* Convert an ASCII character to digit value
*/
-static int mpi_get_digit( mpi_uint *d, int radix, char c )
+static int esp_mpi_get_digit( esp_mpi_uint *d, int radix, char c )
{
*d = 255;
if( c >= 0x41 && c <= 0x46 ) *d = c - 0x37;
if( c >= 0x61 && c <= 0x66 ) *d = c - 0x57;
- if( *d >= (mpi_uint) radix )
+ if( *d >= (esp_mpi_uint) radix )
return( ERR_MPI_INVALID_CHARACTER );
return( 0 );
/*
* Import from an ASCII string
*/
-int mpi_read_string( mpi *X, int radix, const char *s )
+int esp_mpi_read_string( mpi *X, int radix, const char *s )
{
int ret;
size_t i, j, slen, n;
- mpi_uint d;
+ esp_mpi_uint d;
mpi T;
if( radix < 2 || radix > 16 )
return( ERR_MPI_BAD_INPUT_DATA );
- mpi_init( &T );
+ esp_mpi_init( &T );
slen = strlen( s );
n = BITS_TO_LIMBS( slen << 2 );
- MPI_CHK( mpi_grow( X, n ) );
- MPI_CHK( mpi_lset( X, 0 ) );
+ MPI_CHK( esp_mpi_grow( X, n ) );
+ MPI_CHK( esp_mpi_lset( X, 0 ) );
for( i = slen, j = 0; i > 0; i--, j++ )
{
break;
}
- MPI_CHK( mpi_get_digit( &d, radix, s[i - 1] ) );
+ MPI_CHK( esp_mpi_get_digit( &d, radix, s[i - 1] ) );
X->p[j / ( 2 * ciL )] |= d << ( ( j % ( 2 * ciL ) ) << 2 );
}
}
else
{
- MPI_CHK( mpi_lset( X, 0 ) );
+ MPI_CHK( esp_mpi_lset( X, 0 ) );
for( i = 0; i < slen; i++ )
{
continue;
}
- MPI_CHK( mpi_get_digit( &d, radix, s[i] ) );
- MPI_CHK( mpi_mul_int( &T, X, radix ) );
+ MPI_CHK( esp_mpi_get_digit( &d, radix, s[i] ) );
+ MPI_CHK( esp_mpi_mul_int( &T, X, radix ) );
if( X->s == 1 )
{
- MPI_CHK( mpi_add_int( X, &T, d ) );
+ MPI_CHK( esp_mpi_add_int( X, &T, d ) );
}
else
{
- MPI_CHK( mpi_sub_int( X, &T, d ) );
+ MPI_CHK( esp_mpi_sub_int( X, &T, d ) );
}
}
}
cleanup:
- mpi_free( &T );
+ esp_mpi_free( &T );
return( ret );
}
/*
* Helper to write the digits high-order first
*/
-static int mpi_write_hlp( mpi *X, int radix, char **p )
+static int esp_mpi_write_hlp( mpi *X, int radix, char **p )
{
int ret;
- mpi_uint r;
+ esp_mpi_uint r;
if( radix < 2 || radix > 16 )
return( ERR_MPI_BAD_INPUT_DATA );
- MPI_CHK( mpi_mod_int( &r, X, radix ) );
- MPI_CHK( mpi_div_int( X, NULL, X, radix ) );
+ MPI_CHK( esp_mpi_mod_int( &r, X, radix ) );
+ MPI_CHK( esp_mpi_div_int( X, NULL, X, radix ) );
- if( mpi_cmp_int( X, 0 ) != 0 )
- MPI_CHK( mpi_write_hlp( X, radix, p ) );
+ if( esp_mpi_cmp_int( X, 0 ) != 0 )
+ MPI_CHK( esp_mpi_write_hlp( X, radix, p ) );
if( r < 10 )
*(*p)++ = (char)( r + 0x30 );
/*
* Export into an ASCII string
*/
-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 )
{
int ret = 0;
if( radix < 2 || radix > 16 )
return( ERR_MPI_BAD_INPUT_DATA );
- n = mpi_bitlen( X );
+ n = esp_mpi_bitlen( X );
if( radix >= 4 ) n >>= 1;
if( radix >= 16 ) n >>= 1;
n += 3;
}
p = buf;
- mpi_init( &T );
+ esp_mpi_init( &T );
if( X->s == -1 )
*p++ = '-';
}
else
{
- MPI_CHK( mpi_copy( &T, X ) );
+ MPI_CHK( esp_mpi_copy( &T, X ) );
if( T.s == -1 )
T.s = 1;
- MPI_CHK( mpi_write_hlp( &T, radix, &p ) );
+ MPI_CHK( esp_mpi_write_hlp( &T, radix, &p ) );
}
*p++ = '\0';
cleanup:
- mpi_free( &T );
+ esp_mpi_free( &T );
return( ret );
}
/*
* Import X from unsigned binary data, big endian
*/
-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 )
{
int ret;
size_t i, j, n;
if( buf[n] != 0 )
break;
- MPI_CHK( mpi_grow( X, CHARS_TO_LIMBS( buflen - n ) ) );
- MPI_CHK( mpi_lset( X, 0 ) );
+ MPI_CHK( esp_mpi_grow( X, CHARS_TO_LIMBS( buflen - n ) ) );
+ MPI_CHK( esp_mpi_lset( X, 0 ) );
for( i = buflen, j = 0; i > n; i--, j++ )
- X->p[j / ciL] |= ((mpi_uint) buf[i - 1]) << ((j % ciL) << 3);
+ X->p[j / ciL] |= ((esp_mpi_uint) buf[i - 1]) << ((j % ciL) << 3);
cleanup:
/*
* Export X into unsigned binary data, big endian
*/
-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 )
{
size_t i, j, n;
- n = mpi_size( X );
+ n = esp_mpi_size( X );
if( buflen < n )
return( ERR_MPI_BUFFER_TOO_SMALL );
/*
* Left-shift: X <<= count
*/
-int mpi_shift_l( mpi *X, size_t count )
+int esp_mpi_shift_l( mpi *X, size_t count )
{
int ret;
size_t i, v0, t1;
- mpi_uint r0 = 0, r1;
+ esp_mpi_uint r0 = 0, r1;
v0 = count / (biL );
t1 = count & (biL - 1);
- i = mpi_bitlen( X ) + count;
+ i = esp_mpi_bitlen( X ) + count;
if( X->n * biL < i )
- MPI_CHK( mpi_grow( X, BITS_TO_LIMBS( i ) ) );
+ MPI_CHK( esp_mpi_grow( X, BITS_TO_LIMBS( i ) ) );
ret = 0;
/*
* Right-shift: X >>= count
*/
-int mpi_shift_r( mpi *X, size_t count )
+int esp_mpi_shift_r( mpi *X, size_t count )
{
size_t i, v0, v1;
- mpi_uint r0 = 0, r1;
+ esp_mpi_uint r0 = 0, r1;
v0 = count / biL;
v1 = count & (biL - 1);
if( v0 > X->n || ( v0 == X->n && v1 > 0 ) )
- return mpi_lset( X, 0 );
+ return esp_mpi_lset( X, 0 );
/*
* shift by count / limb_size
/*
* Compare unsigned values
*/
-int mpi_cmp_abs( const mpi *X, const mpi *Y )
+int esp_mpi_cmp_abs( const mpi *X, const mpi *Y )
{
size_t i, j;
/*
* Compare signed values
*/
-int mpi_cmp_mpi( const mpi *X, const mpi *Y )
+int esp_mpi_cmp_mpi( const mpi *X, const mpi *Y )
{
size_t i, j;
/*
* Compare signed values
*/
-int mpi_cmp_int( const mpi *X, mpi_sint z )
+int esp_mpi_cmp_int( const mpi *X, esp_mpi_sint z )
{
mpi Y;
- mpi_uint p[1];
+ esp_mpi_uint p[1];
*p = ( z < 0 ) ? -z : z;
Y.s = ( z < 0 ) ? -1 : 1;
Y.n = 1;
Y.p = p;
- return( mpi_cmp_mpi( X, &Y ) );
+ return( esp_mpi_cmp_mpi( X, &Y ) );
}
/*
* Unsigned addition: X = |A| + |B| (HAC 14.7)
*/
-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 )
{
int ret;
size_t i, j;
- mpi_uint *o, *p, c;
+ esp_mpi_uint *o, *p, c;
if( X == B )
{
}
if( X != A )
- MPI_CHK( mpi_copy( X, A ) );
+ MPI_CHK( esp_mpi_copy( X, A ) );
/*
* X should always be positive as a result of unsigned additions.
if( B->p[j - 1] != 0 )
break;
- MPI_CHK( mpi_grow( X, j ) );
+ MPI_CHK( esp_mpi_grow( X, j ) );
o = B->p; p = X->p; c = 0;
{
if( i >= X->n )
{
- MPI_CHK( mpi_grow( X, i + 1 ) );
+ MPI_CHK( esp_mpi_grow( X, i + 1 ) );
p = X->p + i;
}
/*
* Helper for mpi subtraction
*/
-static void mpi_sub_hlp( size_t n, mpi_uint *s, mpi_uint *d )
+static void esp_mpi_sub_hlp( size_t n, esp_mpi_uint *s, esp_mpi_uint *d )
{
size_t i;
- mpi_uint c, z;
+ esp_mpi_uint c, z;
for( i = c = 0; i < n; i++, s++, d++ )
{
/*
* Unsigned subtraction: X = |A| - |B| (HAC 14.9)
*/
-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 )
{
mpi TB;
int ret;
size_t n;
- if( mpi_cmp_abs( A, B ) < 0 )
+ if( esp_mpi_cmp_abs( A, B ) < 0 )
return( ERR_MPI_NEGATIVE_VALUE );
- mpi_init( &TB );
+ esp_mpi_init( &TB );
if( X == B )
{
- MPI_CHK( mpi_copy( &TB, B ) );
+ MPI_CHK( esp_mpi_copy( &TB, B ) );
B = &TB;
}
if( X != A )
- MPI_CHK( mpi_copy( X, A ) );
+ MPI_CHK( esp_mpi_copy( X, A ) );
/*
* X should always be positive as a result of unsigned subtractions.
if( B->p[n - 1] != 0 )
break;
- mpi_sub_hlp( n, B->p, X->p );
+ esp_mpi_sub_hlp( n, B->p, X->p );
cleanup:
- mpi_free( &TB );
+ esp_mpi_free( &TB );
return( ret );
}
/*
* Signed addition: X = A + B
*/
-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 )
{
int ret, s = A->s;
if( A->s * B->s < 0 )
{
- if( mpi_cmp_abs( A, B ) >= 0 )
+ if( esp_mpi_cmp_abs( A, B ) >= 0 )
{
- MPI_CHK( mpi_sub_abs( X, A, B ) );
+ MPI_CHK( esp_mpi_sub_abs( X, A, B ) );
X->s = s;
}
else
{
- MPI_CHK( mpi_sub_abs( X, B, A ) );
+ MPI_CHK( esp_mpi_sub_abs( X, B, A ) );
X->s = -s;
}
}
else
{
- MPI_CHK( mpi_add_abs( X, A, B ) );
+ MPI_CHK( esp_mpi_add_abs( X, A, B ) );
X->s = s;
}
/*
* Signed subtraction: X = A - B
*/
-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 )
{
int ret, s = A->s;
if( A->s * B->s > 0 )
{
- if( mpi_cmp_abs( A, B ) >= 0 )
+ if( esp_mpi_cmp_abs( A, B ) >= 0 )
{
- MPI_CHK( mpi_sub_abs( X, A, B ) );
+ MPI_CHK( esp_mpi_sub_abs( X, A, B ) );
X->s = s;
}
else
{
- MPI_CHK( mpi_sub_abs( X, B, A ) );
+ MPI_CHK( esp_mpi_sub_abs( X, B, A ) );
X->s = -s;
}
}
else
{
- MPI_CHK( mpi_add_abs( X, A, B ) );
+ MPI_CHK( esp_mpi_add_abs( X, A, B ) );
X->s = s;
}
/*
* Signed addition: X = A + b
*/
-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 )
{
mpi _B;
- mpi_uint p[1];
+ esp_mpi_uint p[1];
p[0] = ( b < 0 ) ? -b : b;
_B.s = ( b < 0 ) ? -1 : 1;
_B.n = 1;
_B.p = p;
- return( mpi_add_mpi( X, A, &_B ) );
+ return( esp_mpi_add_mpi( X, A, &_B ) );
}
/*
* Signed subtraction: X = A - b
*/
-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 )
{
mpi _B;
- mpi_uint p[1];
+ esp_mpi_uint p[1];
p[0] = ( b < 0 ) ? -b : b;
_B.s = ( b < 0 ) ? -1 : 1;
_B.n = 1;
_B.p = p;
- return( mpi_sub_mpi( X, A, &_B ) );
+ return( esp_mpi_sub_mpi( X, A, &_B ) );
}
/*
* Helper for mpi multiplication
*/
-static void mpi_mul_hlp( size_t i, mpi_uint *s, mpi_uint *d, mpi_uint b )
+static void esp_mpi_mul_hlp( size_t i, esp_mpi_uint *s, esp_mpi_uint *d, esp_mpi_uint b )
{
}
/*
* Baseline multiplication: X = A * B (HAC 14.12)
*/
-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 )
{
int ret;
size_t i, j;
mpi TA, TB;
- mpi_init( &TA ); mpi_init( &TB );
+ esp_mpi_init( &TA ); esp_mpi_init( &TB );
- if( X == A ) { MPI_CHK( mpi_copy( &TA, A ) ); A = &TA; }
- if( X == B ) { MPI_CHK( mpi_copy( &TB, B ) ); B = &TB; }
+ if( X == A ) { MPI_CHK( esp_mpi_copy( &TA, A ) ); A = &TA; }
+ if( X == B ) { MPI_CHK( esp_mpi_copy( &TB, B ) ); B = &TB; }
for( i = A->n; i > 0; i-- )
if( A->p[i - 1] != 0 )
if( B->p[j - 1] != 0 )
break;
- MPI_CHK( mpi_grow( X, i + j ) );
- MPI_CHK( mpi_lset( X, 0 ) );
+ MPI_CHK( esp_mpi_grow( X, i + j ) );
+ MPI_CHK( esp_mpi_lset( X, 0 ) );
n = j;
// for( i++; j > 0; j-- )
- mpi_mul_hlp( i - 1, A->p, X->p + j - 1, B->p[j - 1] );
+ esp_mpi_mul_hlp( i - 1, A->p, X->p + j - 1, B->p[j - 1] );
BIGNUM_LOCK();
if (ets_bigint_mult_prepare(A->p, B->p, n)){
ets_bigint_wait_finish();
ets_bigint_mult_getz(X->p, n);
} else{
- mpi_printf("Baseline multiplication failed\n");
+ esp_mpi_printf("Baseline multiplication failed\n");
}
BIGNUM_UNLOCK();
cleanup:
- mpi_free( &TB ); mpi_free( &TA );
+ esp_mpi_free( &TB ); esp_mpi_free( &TA );
return( ret );
}
/*
* Baseline multiplication: X = A * b
*/
-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 )
{
mpi _B;
- mpi_uint p[1];
+ esp_mpi_uint p[1];
_B.s = 1;
_B.n = 1;
_B.p = p;
p[0] = b;
- return( mpi_mul_mpi( X, A, &_B ) );
+ return( esp_mpi_mul_mpi( X, A, &_B ) );
}
/*
- * Unsigned integer divide - double mpi_uint dividend, u1/u0, and
- * mpi_uint divisor, d
+ * Unsigned integer divide - double esp_mpi_uint dividend, u1/u0, and
+ * esp_mpi_uint divisor, d
*/
-static mpi_uint int_div_int( mpi_uint u1,
- mpi_uint u0, mpi_uint d, mpi_uint *r )
+static esp_mpi_uint int_div_int( esp_mpi_uint u1,
+ esp_mpi_uint u0, esp_mpi_uint d, esp_mpi_uint *r )
{
#if defined(HAVE_UDBL)
t_udbl dividend, quotient;
#else
- const mpi_uint radix = (mpi_uint) 1 << biH;
- const mpi_uint uint_halfword_mask = ( (mpi_uint) 1 << biH ) - 1;
- mpi_uint d0, d1, q0, q1, rAX, r0, quotient;
- mpi_uint u0_msw, u0_lsw;
+ const esp_mpi_uint radix = (esp_mpi_uint) 1 << biH;
+ const esp_mpi_uint uint_halfword_mask = ( (esp_mpi_uint) 1 << biH ) - 1;
+ esp_mpi_uint d0, d1, q0, q1, rAX, r0, quotient;
+ esp_mpi_uint u0_msw, u0_lsw;
size_t s;
#endif
quotient = ( (t_udbl) 1 << biL ) - 1;
if( r != NULL )
- *r = (mpi_uint)( dividend - (quotient * d ) );
+ *r = (esp_mpi_uint)( dividend - (quotient * d ) );
- return (mpi_uint) quotient;
+ return (esp_mpi_uint) quotient;
#else
/*
d = d << s;
u1 = u1 << s;
- u1 |= ( u0 >> ( biL - s ) ) & ( -(mpi_sint)s >> ( biL - 1 ) );
+ u1 |= ( u0 >> ( biL - s ) ) & ( -(esp_mpi_sint)s >> ( biL - 1 ) );
u0 = u0 << s;
d1 = d >> biH;
/*
* Division by mpi: A = Q * B + R (HAC 14.20)
*/
-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 )
{
int ret;
size_t i, n, t, k;
mpi X, Y, Z, T1, T2;
- if( mpi_cmp_int( B, 0 ) == 0 )
+ if( esp_mpi_cmp_int( B, 0 ) == 0 )
return( ERR_MPI_DIVISION_BY_ZERO );
- mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
- mpi_init( &T1 ); mpi_init( &T2 );
+ esp_mpi_init( &X ); esp_mpi_init( &Y ); esp_mpi_init( &Z );
+ esp_mpi_init( &T1 ); esp_mpi_init( &T2 );
- if( mpi_cmp_abs( A, B ) < 0 )
+ if( esp_mpi_cmp_abs( A, B ) < 0 )
{
- if( Q != NULL ) MPI_CHK( mpi_lset( Q, 0 ) );
- if( R != NULL ) MPI_CHK( mpi_copy( R, A ) );
+ if( Q != NULL ) MPI_CHK( esp_mpi_lset( Q, 0 ) );
+ if( R != NULL ) MPI_CHK( esp_mpi_copy( R, A ) );
return( 0 );
}
- MPI_CHK( mpi_copy( &X, A ) );
- MPI_CHK( mpi_copy( &Y, B ) );
+ MPI_CHK( esp_mpi_copy( &X, A ) );
+ MPI_CHK( esp_mpi_copy( &Y, B ) );
X.s = Y.s = 1;
- MPI_CHK( mpi_grow( &Z, A->n + 2 ) );
- MPI_CHK( mpi_lset( &Z, 0 ) );
- MPI_CHK( mpi_grow( &T1, 2 ) );
- MPI_CHK( mpi_grow( &T2, 3 ) );
+ MPI_CHK( esp_mpi_grow( &Z, A->n + 2 ) );
+ MPI_CHK( esp_mpi_lset( &Z, 0 ) );
+ MPI_CHK( esp_mpi_grow( &T1, 2 ) );
+ MPI_CHK( esp_mpi_grow( &T2, 3 ) );
- k = mpi_bitlen( &Y ) % biL;
+ k = esp_mpi_bitlen( &Y ) % biL;
if( k < biL - 1 )
{
k = biL - 1 - k;
- MPI_CHK( mpi_shift_l( &X, k ) );
- MPI_CHK( mpi_shift_l( &Y, k ) );
+ MPI_CHK( esp_mpi_shift_l( &X, k ) );
+ MPI_CHK( esp_mpi_shift_l( &Y, k ) );
}
else k = 0;
n = X.n - 1;
t = Y.n - 1;
- MPI_CHK( mpi_shift_l( &Y, biL * ( n - t ) ) );
+ MPI_CHK( esp_mpi_shift_l( &Y, biL * ( n - t ) ) );
- while( mpi_cmp_mpi( &X, &Y ) >= 0 )
+ while( esp_mpi_cmp_mpi( &X, &Y ) >= 0 )
{
Z.p[n - t]++;
- MPI_CHK( mpi_sub_mpi( &X, &X, &Y ) );
+ MPI_CHK( esp_mpi_sub_mpi( &X, &X, &Y ) );
}
- MPI_CHK( mpi_shift_r( &Y, biL * ( n - t ) ) );
+ MPI_CHK( esp_mpi_shift_r( &Y, biL * ( n - t ) ) );
for( i = n; i > t ; i-- )
{
{
Z.p[i - t - 1]--;
- MPI_CHK( mpi_lset( &T1, 0 ) );
+ MPI_CHK( esp_mpi_lset( &T1, 0 ) );
T1.p[0] = ( t < 1 ) ? 0 : Y.p[t - 1];
T1.p[1] = Y.p[t];
- MPI_CHK( mpi_mul_int( &T1, &T1, Z.p[i - t - 1] ) );
+ MPI_CHK( esp_mpi_mul_int( &T1, &T1, Z.p[i - t - 1] ) );
- MPI_CHK( mpi_lset( &T2, 0 ) );
+ MPI_CHK( esp_mpi_lset( &T2, 0 ) );
T2.p[0] = ( i < 2 ) ? 0 : X.p[i - 2];
T2.p[1] = ( i < 1 ) ? 0 : X.p[i - 1];
T2.p[2] = X.p[i];
}
- while( mpi_cmp_mpi( &T1, &T2 ) > 0 );
+ while( esp_mpi_cmp_mpi( &T1, &T2 ) > 0 );
- MPI_CHK( mpi_mul_int( &T1, &Y, Z.p[i - t - 1] ) );
- MPI_CHK( mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
- MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) );
+ MPI_CHK( esp_mpi_mul_int( &T1, &Y, Z.p[i - t - 1] ) );
+ MPI_CHK( esp_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
+ MPI_CHK( esp_mpi_sub_mpi( &X, &X, &T1 ) );
- if( mpi_cmp_int( &X, 0 ) < 0 )
+ if( esp_mpi_cmp_int( &X, 0 ) < 0 )
{
- MPI_CHK( mpi_copy( &T1, &Y ) );
- MPI_CHK( mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
- MPI_CHK( mpi_add_mpi( &X, &X, &T1 ) );
+ MPI_CHK( esp_mpi_copy( &T1, &Y ) );
+ MPI_CHK( esp_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
+ MPI_CHK( esp_mpi_add_mpi( &X, &X, &T1 ) );
Z.p[i - t - 1]--;
}
}
if( Q != NULL )
{
- MPI_CHK( mpi_copy( Q, &Z ) );
+ MPI_CHK( esp_mpi_copy( Q, &Z ) );
Q->s = A->s * B->s;
}
if( R != NULL )
{
- MPI_CHK( mpi_shift_r( &X, k ) );
+ MPI_CHK( esp_mpi_shift_r( &X, k ) );
X.s = A->s;
- MPI_CHK( mpi_copy( R, &X ) );
+ MPI_CHK( esp_mpi_copy( R, &X ) );
- if( mpi_cmp_int( R, 0 ) == 0 )
+ if( esp_mpi_cmp_int( R, 0 ) == 0 )
R->s = 1;
}
cleanup:
- mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
- mpi_free( &T1 ); mpi_free( &T2 );
+ esp_mpi_free( &X ); esp_mpi_free( &Y ); esp_mpi_free( &Z );
+ esp_mpi_free( &T1 ); esp_mpi_free( &T2 );
return( ret );
}
/*
* Division by int: A = Q * b + R
*/
-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 )
{
mpi _B;
- mpi_uint p[1];
+ esp_mpi_uint p[1];
p[0] = ( b < 0 ) ? -b : b;
_B.s = ( b < 0 ) ? -1 : 1;
_B.n = 1;
_B.p = p;
- return( mpi_div_mpi( Q, R, A, &_B ) );
+ return( esp_mpi_div_mpi( Q, R, A, &_B ) );
}
/*
* Modulo: R = A mod B
*/
-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 )
{
int ret;
- if( mpi_cmp_int( B, 0 ) < 0 )
+ if( esp_mpi_cmp_int( B, 0 ) < 0 )
return( ERR_MPI_NEGATIVE_VALUE );
- MPI_CHK( mpi_div_mpi( NULL, R, A, B ) );
+ MPI_CHK( esp_mpi_div_mpi( NULL, R, A, B ) );
- while( mpi_cmp_int( R, 0 ) < 0 )
- MPI_CHK( mpi_add_mpi( R, R, B ) );
+ while( esp_mpi_cmp_int( R, 0 ) < 0 )
+ MPI_CHK( esp_mpi_add_mpi( R, R, B ) );
- while( mpi_cmp_mpi( R, B ) >= 0 )
- MPI_CHK( mpi_sub_mpi( R, R, B ) );
+ while( esp_mpi_cmp_mpi( R, B ) >= 0 )
+ MPI_CHK( esp_mpi_sub_mpi( R, R, B ) );
cleanup:
/*
* Modulo: r = A mod b
*/
-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 )
{
size_t i;
- mpi_uint x, y, z;
+ esp_mpi_uint x, y, z;
if( b == 0 )
return( ERR_MPI_DIVISION_BY_ZERO );
/*
* Fast Montgomery initialization (thanks to Tom St Denis)
*/
-static void mpi_montg_init( mpi_uint *mm, const mpi *N )
+static void esp_mpi_montg_init( esp_mpi_uint *mm, const mpi *N )
{
- mpi_uint x, m0 = N->p[0];
+ esp_mpi_uint x, m0 = N->p[0];
unsigned int i;
x = m0;
/*
* Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36)
*/
-static void mpi_montmul( mpi *A, const mpi *B, const mpi *N, mpi_uint mm,
+static void esp_mpi_montmul( mpi *A, const mpi *B, const mpi *N, esp_mpi_uint mm,
const mpi *T )
{
size_t n, m;
- mpi_uint *d = NULL;
+ esp_mpi_uint *d = NULL;
memset( T->p, 0, T->n * ciL );
ets_bigint_montgomery_mult_getz(A->p, n);
} else{
- mpi_printf("Montgomery multiplication failed\n");
+ esp_mpi_printf("Montgomery multiplication failed\n");
}
BIGNUM_UNLOCK();
/*
* Montgomery reduction: A = A * R^-1 mod N
*/
-static void mpi_montred( mpi *A, const mpi *N, mpi_uint mm, const mpi *T )
+static void esp_mpi_montred( mpi *A, const mpi *N, esp_mpi_uint mm, const mpi *T )
{
- mpi_uint z = 1;
+ esp_mpi_uint z = 1;
mpi U;
U.n = U.s = (int) z;
U.p = &z;
- mpi_montmul( A, &U, N, mm, T );
+ esp_mpi_montmul( A, &U, N, mm, T );
}
/*
* Sliding-window exponentiation: X = A^E mod N (HAC 14.85)
*/
-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 )
{
int ret;
size_t wbits, wsize, one = 1;
size_t i, j, nblimbs;
size_t bufsize, nbits;
- mpi_uint ei, mm, state;
+ esp_mpi_uint ei, mm, state;
mpi RR, T, W[ 2 << MPI_WINDOW_SIZE ], Apos;
int neg;
- if( mpi_cmp_int( N, 0 ) < 0 || ( N->p[0] & 1 ) == 0 )
+ if( esp_mpi_cmp_int( N, 0 ) < 0 || ( N->p[0] & 1 ) == 0 )
return( ERR_MPI_BAD_INPUT_DATA );
- if( mpi_cmp_int( E, 0 ) < 0 )
+ if( esp_mpi_cmp_int( E, 0 ) < 0 )
return( ERR_MPI_BAD_INPUT_DATA );
/*
* Init temps and window size
*/
- mpi_montg_init( &mm, N );
- mpi_init( &RR ); mpi_init( &T );
- mpi_init( &Apos );
+ esp_mpi_montg_init( &mm, N );
+ esp_mpi_init( &RR ); esp_mpi_init( &T );
+ esp_mpi_init( &Apos );
memset( W, 0, sizeof( W ) );
- i = mpi_bitlen( E );
+ i = esp_mpi_bitlen( E );
wsize = ( i > 671 ) ? 6 : ( i > 239 ) ? 5 :
( i > 79 ) ? 4 : ( i > 23 ) ? 3 : 1;
wsize = MPI_WINDOW_SIZE;
j = N->n + 1;
- MPI_CHK( mpi_grow( X, j ) );
- MPI_CHK( mpi_grow( &W[1], j ) );
- MPI_CHK( mpi_grow( &T, j * 2 ) );
+ MPI_CHK( esp_mpi_grow( X, j ) );
+ MPI_CHK( esp_mpi_grow( &W[1], j ) );
+ MPI_CHK( esp_mpi_grow( &T, j * 2 ) );
/*
* Compensate for negative A (and correct at the end)
neg = ( A->s == -1 );
if( neg )
{
- MPI_CHK( mpi_copy( &Apos, A ) );
+ MPI_CHK( esp_mpi_copy( &Apos, A ) );
Apos.s = 1;
A = &Apos;
}
*/
if( _RR == NULL || _RR->p == NULL )
{
- MPI_CHK( mpi_lset( &RR, 1 ) );
- MPI_CHK( mpi_shift_l( &RR, N->n * 2 * biL ) );
- MPI_CHK( mpi_mod_mpi( &RR, &RR, N ) );
+ MPI_CHK( esp_mpi_lset( &RR, 1 ) );
+ MPI_CHK( esp_mpi_shift_l( &RR, N->n * 2 * biL ) );
+ MPI_CHK( esp_mpi_mod_mpi( &RR, &RR, N ) );
if( _RR != NULL )
memcpy( _RR, &RR, sizeof( mpi ) );
/*
* W[1] = A * R^2 * R^-1 mod N = A * R mod N
*/
- if( mpi_cmp_mpi( A, N ) >= 0 )
- MPI_CHK( mpi_mod_mpi( &W[1], A, N ) );
+ if( esp_mpi_cmp_mpi( A, N ) >= 0 )
+ MPI_CHK( esp_mpi_mod_mpi( &W[1], A, N ) );
else
- MPI_CHK( mpi_copy( &W[1], A ) );
+ MPI_CHK( esp_mpi_copy( &W[1], A ) );
- mpi_montmul( &W[1], &RR, N, mm, &T );
+ esp_mpi_montmul( &W[1], &RR, N, mm, &T );
/*
* X = R^2 * R^-1 mod N = R mod N
*/
- MPI_CHK( mpi_copy( X, &RR ) );
- mpi_montred( X, N, mm, &T );
+ MPI_CHK( esp_mpi_copy( X, &RR ) );
+ esp_mpi_montred( X, N, mm, &T );
if( wsize > 1 )
{
*/
j = one << ( wsize - 1 );
- MPI_CHK( mpi_grow( &W[j], N->n + 1 ) );
- MPI_CHK( mpi_copy( &W[j], &W[1] ) );
+ MPI_CHK( esp_mpi_grow( &W[j], N->n + 1 ) );
+ MPI_CHK( esp_mpi_copy( &W[j], &W[1] ) );
for( i = 0; i < wsize - 1; i++ )
- mpi_montmul( &W[j], &W[j], N, mm, &T );
+ esp_mpi_montmul( &W[j], &W[j], N, mm, &T );
/*
* W[i] = W[i - 1] * W[1]
*/
for( i = j + 1; i < ( one << wsize ); i++ )
{
- MPI_CHK( mpi_grow( &W[i], N->n + 1 ) );
- MPI_CHK( mpi_copy( &W[i], &W[i - 1] ) );
+ MPI_CHK( esp_mpi_grow( &W[i], N->n + 1 ) );
+ MPI_CHK( esp_mpi_copy( &W[i], &W[i - 1] ) );
- mpi_montmul( &W[i], &W[1], N, mm, &T );
+ esp_mpi_montmul( &W[i], &W[1], N, mm, &T );
}
}
nblimbs--;
- bufsize = sizeof( mpi_uint ) << 3;
+ bufsize = sizeof( esp_mpi_uint ) << 3;
}
bufsize--;
/*
* out of window, square X
*/
- mpi_montmul( X, X, N, mm, &T );
+ esp_mpi_montmul( X, X, N, mm, &T );
continue;
}
* X = X^wsize R^-1 mod N
*/
for( i = 0; i < wsize; i++ )
- mpi_montmul( X, X, N, mm, &T );
+ esp_mpi_montmul( X, X, N, mm, &T );
/*
* X = X * W[wbits] R^-1 mod N
*/
- mpi_montmul( X, &W[wbits], N, mm, &T );
+ esp_mpi_montmul( X, &W[wbits], N, mm, &T );
state--;
nbits = 0;
*/
for( i = 0; i < nbits; i++ )
{
- mpi_montmul( X, X, N, mm, &T );
+ esp_mpi_montmul( X, X, N, mm, &T );
wbits <<= 1;
if( ( wbits & ( one << wsize ) ) != 0 )
- mpi_montmul( X, &W[1], N, mm, &T );
+ esp_mpi_montmul( X, &W[1], N, mm, &T );
}
/*
* X = A^E * R * R^-1 mod N = A^E mod N
*/
- mpi_montred( X, N, mm, &T );
+ esp_mpi_montred( X, N, mm, &T );
if( neg )
{
X->s = -1;
- MPI_CHK( mpi_add_mpi( X, N, X ) );
+ MPI_CHK( esp_mpi_add_mpi( X, N, X ) );
}
cleanup:
for( i = ( one << ( wsize - 1 ) ); i < ( one << wsize ); i++ )
- mpi_free( &W[i] );
+ esp_mpi_free( &W[i] );
- mpi_free( &W[1] ); mpi_free( &T ); mpi_free( &Apos );
+ esp_mpi_free( &W[1] ); esp_mpi_free( &T ); esp_mpi_free( &Apos );
if( _RR == NULL || _RR->p == NULL )
- mpi_free( &RR );
+ esp_mpi_free( &RR );
return( ret );
}
/*
* Greatest common divisor: G = gcd(A, B) (HAC 14.54)
*/
-int mpi_gcd( mpi *G, const mpi *A, const mpi *B )
+int esp_mpi_gcd( mpi *G, const mpi *A, const mpi *B )
{
int ret;
size_t lz, lzt;
mpi TG, TA, TB;
- mpi_init( &TG ); mpi_init( &TA ); mpi_init( &TB );
+ esp_mpi_init( &TG ); esp_mpi_init( &TA ); esp_mpi_init( &TB );
- MPI_CHK( mpi_copy( &TA, A ) );
- MPI_CHK( mpi_copy( &TB, B ) );
+ MPI_CHK( esp_mpi_copy( &TA, A ) );
+ MPI_CHK( esp_mpi_copy( &TB, B ) );
- lz = mpi_lsb( &TA );
- lzt = mpi_lsb( &TB );
+ lz = esp_mpi_lsb( &TA );
+ lzt = esp_mpi_lsb( &TB );
if( lzt < lz )
lz = lzt;
- MPI_CHK( mpi_shift_r( &TA, lz ) );
- MPI_CHK( mpi_shift_r( &TB, lz ) );
+ MPI_CHK( esp_mpi_shift_r( &TA, lz ) );
+ MPI_CHK( esp_mpi_shift_r( &TB, lz ) );
TA.s = TB.s = 1;
- while( mpi_cmp_int( &TA, 0 ) != 0 )
+ while( esp_mpi_cmp_int( &TA, 0 ) != 0 )
{
- MPI_CHK( mpi_shift_r( &TA, mpi_lsb( &TA ) ) );
- MPI_CHK( mpi_shift_r( &TB, mpi_lsb( &TB ) ) );
+ MPI_CHK( esp_mpi_shift_r( &TA, esp_mpi_lsb( &TA ) ) );
+ MPI_CHK( esp_mpi_shift_r( &TB, esp_mpi_lsb( &TB ) ) );
- if( mpi_cmp_mpi( &TA, &TB ) >= 0 )
+ if( esp_mpi_cmp_mpi( &TA, &TB ) >= 0 )
{
- MPI_CHK( mpi_sub_abs( &TA, &TA, &TB ) );
- MPI_CHK( mpi_shift_r( &TA, 1 ) );
+ MPI_CHK( esp_mpi_sub_abs( &TA, &TA, &TB ) );
+ MPI_CHK( esp_mpi_shift_r( &TA, 1 ) );
}
else
{
- MPI_CHK( mpi_sub_abs( &TB, &TB, &TA ) );
- MPI_CHK( mpi_shift_r( &TB, 1 ) );
+ MPI_CHK( esp_mpi_sub_abs( &TB, &TB, &TA ) );
+ MPI_CHK( esp_mpi_shift_r( &TB, 1 ) );
}
}
- MPI_CHK( mpi_shift_l( &TB, lz ) );
- MPI_CHK( mpi_copy( G, &TB ) );
+ MPI_CHK( esp_mpi_shift_l( &TB, lz ) );
+ MPI_CHK( esp_mpi_copy( G, &TB ) );
cleanup:
- mpi_free( &TG ); mpi_free( &TA ); mpi_free( &TB );
+ esp_mpi_free( &TG ); esp_mpi_free( &TA ); esp_mpi_free( &TB );
return( ret );
}
* regardless of the platform endianness (useful when f_rng is actually
* deterministic, eg for tests).
*/
-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 )
{
return( ERR_MPI_BAD_INPUT_DATA );
MPI_CHK( f_rng( p_rng, buf, size ) );
- MPI_CHK( mpi_read_binary( X, buf, size ) );
+ MPI_CHK( esp_mpi_read_binary( X, buf, size ) );
cleanup:
return( ret );
/*
* Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64)
*/
-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 )
{
int ret;
mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
- if( mpi_cmp_int( N, 0 ) <= 0 )
+ if( esp_mpi_cmp_int( N, 0 ) <= 0 )
return( ERR_MPI_BAD_INPUT_DATA );
- mpi_init( &TA ); mpi_init( &TU ); mpi_init( &U1 ); mpi_init( &U2 );
- mpi_init( &G ); mpi_init( &TB ); mpi_init( &TV );
- mpi_init( &V1 ); mpi_init( &V2 );
+ esp_mpi_init( &TA ); esp_mpi_init( &TU ); esp_mpi_init( &U1 ); esp_mpi_init( &U2 );
+ esp_mpi_init( &G ); esp_mpi_init( &TB ); esp_mpi_init( &TV );
+ esp_mpi_init( &V1 ); esp_mpi_init( &V2 );
- MPI_CHK( mpi_gcd( &G, A, N ) );
+ MPI_CHK( esp_mpi_gcd( &G, A, N ) );
- if( mpi_cmp_int( &G, 1 ) != 0 )
+ if( esp_mpi_cmp_int( &G, 1 ) != 0 )
{
ret = ERR_MPI_NOT_ACCEPTABLE;
goto cleanup;
}
- MPI_CHK( mpi_mod_mpi( &TA, A, N ) );
- MPI_CHK( mpi_copy( &TU, &TA ) );
- MPI_CHK( mpi_copy( &TB, N ) );
- MPI_CHK( mpi_copy( &TV, N ) );
+ MPI_CHK( esp_mpi_mod_mpi( &TA, A, N ) );
+ MPI_CHK( esp_mpi_copy( &TU, &TA ) );
+ MPI_CHK( esp_mpi_copy( &TB, N ) );
+ MPI_CHK( esp_mpi_copy( &TV, N ) );
- MPI_CHK( mpi_lset( &U1, 1 ) );
- MPI_CHK( mpi_lset( &U2, 0 ) );
- MPI_CHK( mpi_lset( &V1, 0 ) );
- MPI_CHK( mpi_lset( &V2, 1 ) );
+ MPI_CHK( esp_mpi_lset( &U1, 1 ) );
+ MPI_CHK( esp_mpi_lset( &U2, 0 ) );
+ MPI_CHK( esp_mpi_lset( &V1, 0 ) );
+ MPI_CHK( esp_mpi_lset( &V2, 1 ) );
do
{
while( ( TU.p[0] & 1 ) == 0 )
{
- MPI_CHK( mpi_shift_r( &TU, 1 ) );
+ MPI_CHK( esp_mpi_shift_r( &TU, 1 ) );
if( ( U1.p[0] & 1 ) != 0 || ( U2.p[0] & 1 ) != 0 )
{
- MPI_CHK( mpi_add_mpi( &U1, &U1, &TB ) );
- MPI_CHK( mpi_sub_mpi( &U2, &U2, &TA ) );
+ MPI_CHK( esp_mpi_add_mpi( &U1, &U1, &TB ) );
+ MPI_CHK( esp_mpi_sub_mpi( &U2, &U2, &TA ) );
}
- MPI_CHK( mpi_shift_r( &U1, 1 ) );
- MPI_CHK( mpi_shift_r( &U2, 1 ) );
+ MPI_CHK( esp_mpi_shift_r( &U1, 1 ) );
+ MPI_CHK( esp_mpi_shift_r( &U2, 1 ) );
}
while( ( TV.p[0] & 1 ) == 0 )
{
- MPI_CHK( mpi_shift_r( &TV, 1 ) );
+ MPI_CHK( esp_mpi_shift_r( &TV, 1 ) );
if( ( V1.p[0] & 1 ) != 0 || ( V2.p[0] & 1 ) != 0 )
{
- MPI_CHK( mpi_add_mpi( &V1, &V1, &TB ) );
- MPI_CHK( mpi_sub_mpi( &V2, &V2, &TA ) );
+ MPI_CHK( esp_mpi_add_mpi( &V1, &V1, &TB ) );
+ MPI_CHK( esp_mpi_sub_mpi( &V2, &V2, &TA ) );
}
- MPI_CHK( mpi_shift_r( &V1, 1 ) );
- MPI_CHK( mpi_shift_r( &V2, 1 ) );
+ MPI_CHK( esp_mpi_shift_r( &V1, 1 ) );
+ MPI_CHK( esp_mpi_shift_r( &V2, 1 ) );
}
- if( mpi_cmp_mpi( &TU, &TV ) >= 0 )
+ if( esp_mpi_cmp_mpi( &TU, &TV ) >= 0 )
{
- MPI_CHK( mpi_sub_mpi( &TU, &TU, &TV ) );
- MPI_CHK( mpi_sub_mpi( &U1, &U1, &V1 ) );
- MPI_CHK( mpi_sub_mpi( &U2, &U2, &V2 ) );
+ MPI_CHK( esp_mpi_sub_mpi( &TU, &TU, &TV ) );
+ MPI_CHK( esp_mpi_sub_mpi( &U1, &U1, &V1 ) );
+ MPI_CHK( esp_mpi_sub_mpi( &U2, &U2, &V2 ) );
}
else
{
- MPI_CHK( mpi_sub_mpi( &TV, &TV, &TU ) );
- MPI_CHK( mpi_sub_mpi( &V1, &V1, &U1 ) );
- MPI_CHK( mpi_sub_mpi( &V2, &V2, &U2 ) );
+ MPI_CHK( esp_mpi_sub_mpi( &TV, &TV, &TU ) );
+ MPI_CHK( esp_mpi_sub_mpi( &V1, &V1, &U1 ) );
+ MPI_CHK( esp_mpi_sub_mpi( &V2, &V2, &U2 ) );
}
}
- while( mpi_cmp_int( &TU, 0 ) != 0 );
+ while( esp_mpi_cmp_int( &TU, 0 ) != 0 );
- while( mpi_cmp_int( &V1, 0 ) < 0 )
- MPI_CHK( mpi_add_mpi( &V1, &V1, N ) );
+ while( esp_mpi_cmp_int( &V1, 0 ) < 0 )
+ MPI_CHK( esp_mpi_add_mpi( &V1, &V1, N ) );
- while( mpi_cmp_mpi( &V1, N ) >= 0 )
- MPI_CHK( mpi_sub_mpi( &V1, &V1, N ) );
+ while( esp_mpi_cmp_mpi( &V1, N ) >= 0 )
+ MPI_CHK( esp_mpi_sub_mpi( &V1, &V1, N ) );
- MPI_CHK( mpi_copy( X, &V1 ) );
+ MPI_CHK( esp_mpi_copy( X, &V1 ) );
cleanup:
- mpi_free( &TA ); mpi_free( &TU ); mpi_free( &U1 ); mpi_free( &U2 );
- mpi_free( &G ); mpi_free( &TB ); mpi_free( &TV );
- mpi_free( &V1 ); mpi_free( &V2 );
+ esp_mpi_free( &TA ); esp_mpi_free( &TU ); esp_mpi_free( &U1 ); esp_mpi_free( &U2 );
+ esp_mpi_free( &G ); esp_mpi_free( &TB ); esp_mpi_free( &TV );
+ esp_mpi_free( &V1 ); esp_mpi_free( &V2 );
return( ret );
}
* ERR_MPI_NOT_ACCEPTABLE: certain non-prime
* other negative: error
*/
-static int mpi_check_small_factors( const mpi *X )
+static int esp_mpi_check_small_factors( const mpi *X )
{
int ret = 0;
size_t i;
- mpi_uint r;
+ esp_mpi_uint r;
if( ( X->p[0] & 1 ) == 0 )
return( ERR_MPI_NOT_ACCEPTABLE );
for( i = 0; small_prime[i] > 0; i++ )
{
- if( mpi_cmp_int( X, small_prime[i] ) <= 0 )
+ if( esp_mpi_cmp_int( X, small_prime[i] ) <= 0 )
return( 1 );
- MPI_CHK( mpi_mod_int( &r, X, small_prime[i] ) );
+ MPI_CHK( esp_mpi_mod_int( &r, X, small_prime[i] ) );
if( r == 0 )
return( ERR_MPI_NOT_ACCEPTABLE );
/*
* Miller-Rabin pseudo-primality test (HAC 4.24)
*/
-static int mpi_miller_rabin( const mpi *X,
+static int esp_mpi_miller_rabin( const mpi *X,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng )
{
size_t i, j, k, n, s;
mpi W, R, T, A, RR;
- mpi_init( &W ); mpi_init( &R ); mpi_init( &T ); mpi_init( &A );
- mpi_init( &RR );
+ esp_mpi_init( &W ); esp_mpi_init( &R ); esp_mpi_init( &T ); esp_mpi_init( &A );
+ esp_mpi_init( &RR );
/*
* W = |X| - 1
* R = W >> lsb( W )
*/
- MPI_CHK( mpi_sub_int( &W, X, 1 ) );
- s = mpi_lsb( &W );
- MPI_CHK( mpi_copy( &R, &W ) );
- MPI_CHK( mpi_shift_r( &R, s ) );
+ MPI_CHK( esp_mpi_sub_int( &W, X, 1 ) );
+ s = esp_mpi_lsb( &W );
+ MPI_CHK( esp_mpi_copy( &R, &W ) );
+ MPI_CHK( esp_mpi_shift_r( &R, s ) );
- i = mpi_bitlen( X );
+ i = esp_mpi_bitlen( X );
/*
* HAC, table 4.4
*/
/*
* pick a random A, 1 < A < |X| - 1
*/
- MPI_CHK( mpi_fill_random( &A, X->n * ciL, f_rng, p_rng ) );
+ MPI_CHK( esp_mpi_fill_random( &A, X->n * ciL, f_rng, p_rng ) );
- if( mpi_cmp_mpi( &A, &W ) >= 0 )
+ if( esp_mpi_cmp_mpi( &A, &W ) >= 0 )
{
- j = mpi_bitlen( &A ) - mpi_bitlen( &W );
- MPI_CHK( mpi_shift_r( &A, j + 1 ) );
+ j = esp_mpi_bitlen( &A ) - esp_mpi_bitlen( &W );
+ MPI_CHK( esp_mpi_shift_r( &A, j + 1 ) );
}
A.p[0] |= 3;
count = 0;
do {
- MPI_CHK( mpi_fill_random( &A, X->n * ciL, f_rng, p_rng ) );
+ MPI_CHK( esp_mpi_fill_random( &A, X->n * ciL, f_rng, p_rng ) );
- j = mpi_bitlen( &A );
- k = mpi_bitlen( &W );
+ j = esp_mpi_bitlen( &A );
+ k = esp_mpi_bitlen( &W );
if (j > k) {
- MPI_CHK( mpi_shift_r( &A, j - k ) );
+ MPI_CHK( esp_mpi_shift_r( &A, j - k ) );
}
if (count++ > 30) {
return ERR_MPI_NOT_ACCEPTABLE;
}
- } while ( mpi_cmp_mpi( &A, &W ) >= 0 ||
- mpi_cmp_int( &A, 1 ) <= 0 );
+ } while ( esp_mpi_cmp_mpi( &A, &W ) >= 0 ||
+ esp_mpi_cmp_int( &A, 1 ) <= 0 );
/*
* A = A^R mod |X|
*/
- MPI_CHK( mpi_exp_mod( &A, &A, &R, X, &RR ) );
+ MPI_CHK( esp_mpi_exp_mod( &A, &A, &R, X, &RR ) );
- if( mpi_cmp_mpi( &A, &W ) == 0 ||
- mpi_cmp_int( &A, 1 ) == 0 )
+ if( esp_mpi_cmp_mpi( &A, &W ) == 0 ||
+ esp_mpi_cmp_int( &A, 1 ) == 0 )
continue;
j = 1;
- while( j < s && mpi_cmp_mpi( &A, &W ) != 0 )
+ while( j < s && esp_mpi_cmp_mpi( &A, &W ) != 0 )
{
/*
* A = A * A mod |X|
*/
- MPI_CHK( mpi_mul_mpi( &T, &A, &A ) );
- MPI_CHK( mpi_mod_mpi( &A, &T, X ) );
+ MPI_CHK( esp_mpi_mul_mpi( &T, &A, &A ) );
+ MPI_CHK( esp_mpi_mod_mpi( &A, &T, X ) );
- if( mpi_cmp_int( &A, 1 ) == 0 )
+ if( esp_mpi_cmp_int( &A, 1 ) == 0 )
break;
j++;
/*
* not prime if A != |X| - 1 or A == 1
*/
- if( mpi_cmp_mpi( &A, &W ) != 0 ||
- mpi_cmp_int( &A, 1 ) == 0 )
+ if( esp_mpi_cmp_mpi( &A, &W ) != 0 ||
+ esp_mpi_cmp_int( &A, 1 ) == 0 )
{
ret = ERR_MPI_NOT_ACCEPTABLE;
break;
}
cleanup:
- mpi_free( &W ); mpi_free( &R ); mpi_free( &T ); mpi_free( &A );
- mpi_free( &RR );
+ esp_mpi_free( &W ); esp_mpi_free( &R ); esp_mpi_free( &T ); esp_mpi_free( &A );
+ esp_mpi_free( &RR );
return( ret );
}
/*
* Pseudo-primality test: small factors, then Miller-Rabin
*/
-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 )
{
XX.n = X->n;
XX.p = X->p;
- if( mpi_cmp_int( &XX, 0 ) == 0 ||
- mpi_cmp_int( &XX, 1 ) == 0 )
+ if( esp_mpi_cmp_int( &XX, 0 ) == 0 ||
+ esp_mpi_cmp_int( &XX, 1 ) == 0 )
return( ERR_MPI_NOT_ACCEPTABLE );
- if( mpi_cmp_int( &XX, 2 ) == 0 )
+ if( esp_mpi_cmp_int( &XX, 2 ) == 0 )
return( 0 );
- if( ( ret = mpi_check_small_factors( &XX ) ) != 0 )
+ if( ( ret = esp_mpi_check_small_factors( &XX ) ) != 0 )
{
if( ret == 1 )
return( 0 );
return( ret );
}
- return( mpi_miller_rabin( &XX, f_rng, p_rng ) );
+ return( esp_mpi_miller_rabin( &XX, f_rng, p_rng ) );
}
/*
* Prime number generation
*/
-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 )
{
int ret;
size_t k, n;
- mpi_uint r;
+ esp_mpi_uint r;
mpi Y;
if( nbits < 3 || nbits > MPI_MAX_BITS )
return( ERR_MPI_BAD_INPUT_DATA );
- mpi_init( &Y );
+ esp_mpi_init( &Y );
n = BITS_TO_LIMBS( nbits );
- MPI_CHK( mpi_fill_random( X, n * ciL, f_rng, p_rng ) );
+ MPI_CHK( esp_mpi_fill_random( X, n * ciL, f_rng, p_rng ) );
- k = mpi_bitlen( X );
- if( k > nbits ) MPI_CHK( mpi_shift_r( X, k - nbits + 1 ) );
+ k = esp_mpi_bitlen( X );
+ if( k > nbits ) MPI_CHK( esp_mpi_shift_r( X, k - nbits + 1 ) );
- mpi_set_bit( X, nbits-1, 1 );
+ esp_mpi_set_bit( X, nbits-1, 1 );
X->p[0] |= 1;
if( dh_flag == 0 )
{
- while( ( ret = mpi_is_prime( X, f_rng, p_rng ) ) != 0 )
+ while( ( ret = esp_mpi_is_prime( X, f_rng, p_rng ) ) != 0 )
{
if( ret != ERR_MPI_NOT_ACCEPTABLE )
goto cleanup;
- MPI_CHK( mpi_add_int( X, X, 2 ) );
+ MPI_CHK( esp_mpi_add_int( X, X, 2 ) );
}
}
else
X->p[0] |= 2;
- MPI_CHK( mpi_mod_int( &r, X, 3 ) );
+ MPI_CHK( esp_mpi_mod_int( &r, X, 3 ) );
if( r == 0 )
- MPI_CHK( mpi_add_int( X, X, 8 ) );
+ MPI_CHK( esp_mpi_add_int( X, X, 8 ) );
else if( r == 1 )
- MPI_CHK( mpi_add_int( X, X, 4 ) );
+ MPI_CHK( esp_mpi_add_int( X, X, 4 ) );
/* Set Y = (X-1) / 2, which is X / 2 because X is odd */
- MPI_CHK( mpi_copy( &Y, X ) );
- MPI_CHK( mpi_shift_r( &Y, 1 ) );
+ MPI_CHK( esp_mpi_copy( &Y, X ) );
+ MPI_CHK( esp_mpi_shift_r( &Y, 1 ) );
while( 1 )
{
* First, check small factors for X and Y
* before doing Miller-Rabin on any of them
*/
- if( ( ret = mpi_check_small_factors( X ) ) == 0 &&
- ( ret = mpi_check_small_factors( &Y ) ) == 0 &&
- ( ret = mpi_miller_rabin( X, f_rng, p_rng ) ) == 0 &&
- ( ret = mpi_miller_rabin( &Y, f_rng, p_rng ) ) == 0 )
+ if( ( ret = esp_mpi_check_small_factors( X ) ) == 0 &&
+ ( ret = esp_mpi_check_small_factors( &Y ) ) == 0 &&
+ ( ret = esp_mpi_miller_rabin( X, f_rng, p_rng ) ) == 0 &&
+ ( ret = esp_mpi_miller_rabin( &Y, f_rng, p_rng ) ) == 0 )
{
break;
}
* Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3)
* so up Y by 6 and X by 12.
*/
- MPI_CHK( mpi_add_int( X, X, 12 ) );
- MPI_CHK( mpi_add_int( &Y, &Y, 6 ) );
+ MPI_CHK( esp_mpi_add_int( X, X, 12 ) );
+ MPI_CHK( esp_mpi_add_int( &Y, &Y, 6 ) );
}
}
cleanup:
- mpi_free( &Y );
+ esp_mpi_free( &Y );
return( ret );
}
--- /dev/null
+#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;
+}
+
/**
- * \file aes_alt.h
+ * \file esp_aes.h
*
* \brief AES block cipher
*
*
*/
-#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"
extern "C" {
#endif
-#define ESP_AES_C
-
/* padlock.c and aesni.c rely on these values! */
#define AES_ENCRYPT 1
#define AES_DECRYPT 0
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)
*
* \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)
*
* \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
*
* \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
*
* \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],
*
* 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
*
* \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,
*
* 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
*
* \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],
*
* 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
*
* \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],
* \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
* \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
}
*
*/
-#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"
#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.
#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'.
*
#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__) && ( \
(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 */
{
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)
*
* \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
* \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
* \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
* \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
* \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
* 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
* 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
* \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
*
* \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
* 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
*
* \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
*
* \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
*
* \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
* \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)
* 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
*
* \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 */
/**
* \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.
* \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
* \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
* \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
* -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
* -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
* -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|
* \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|
* \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
* \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
* \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
* \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
* \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
* \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
* \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
*
* \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
*
* \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
* 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
*
* 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
* 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
* \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 );
* \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
* 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
* 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 );
* 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
--- /dev/null
+#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 */
--- /dev/null
+/*
+ * 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
+
--- /dev/null
+/*
+ * 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 );
+}
+
+////
+
#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
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 */
/**
//#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 */
#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
#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
#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
+++ /dev/null
-#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 */
+++ /dev/null
-/*
- * 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
-
+++ /dev/null
-/*
- * 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 */
+++ /dev/null
-/*
- * 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 */
--- /dev/null
+/**
+ * \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 */
--- /dev/null
+/**
+ * \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
+
--- /dev/null
+/*
+ * 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
+
--- /dev/null
+/*
+ * 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 */
--- /dev/null
+/*
+ * 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 */
+++ /dev/null
-#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;
-}
-
+++ /dev/null
-/*
- * 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 */
-
-
+++ /dev/null
-/*
- * 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 */
+++ /dev/null
-/*
- * 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 */