diff --git a/components/mbedtls/port/aes_alt.c b/components/esp32/aes.c similarity index 79% rename from components/mbedtls/port/aes_alt.c rename to components/esp32/aes.c index 504a2a7db8..10a5025a56 100644 --- a/components/mbedtls/port/aes_alt.c +++ b/components/esp32/aes.c @@ -25,19 +25,17 @@ * http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf */ -#include "port/aes_alt.h" - -#if defined(ESP_AES_C) +#include "aes.h" #include -#include "multi_thread.h" +#include "esp_thread.h" /* Implementation that should never be optimized out by the compiler */ -static void aes_zeroize( void *v, size_t n ) { +static void esp_aes_zeroize( void *v, size_t n ) { volatile unsigned char *p = v; while( n-- ) *p++ = 0; } -void aes_init( AES_CTX *ctx ) +void esp_aes_init( AES_CTX *ctx ) { memset( ctx, 0, sizeof( AES_CTX ) ); @@ -47,12 +45,12 @@ void aes_init( AES_CTX *ctx ) AES_UNLOCK(); } -void aes_free( AES_CTX *ctx ) +void esp_aes_free( AES_CTX *ctx ) { if( ctx == NULL ) return; - aes_zeroize( ctx, sizeof( AES_CTX ) ); + esp_aes_zeroize( ctx, sizeof( AES_CTX ) ); AES_LOCK(); AES_GIVE(); @@ -64,7 +62,7 @@ void aes_free( AES_CTX *ctx ) /* * AES key schedule (encryption) */ -int aes_setkey_enc( AES_CTX *ctx, const unsigned char *key, +int esp_aes_setkey_enc( AES_CTX *ctx, const unsigned char *key, unsigned int keybits ) { enum AES_BITS keybit; @@ -94,7 +92,7 @@ int aes_setkey_enc( AES_CTX *ctx, const unsigned char *key, /* * AES key schedule (decryption) */ -int aes_setkey_dec( AES_CTX *ctx, const unsigned char *key, +int esp_aes_setkey_dec( AES_CTX *ctx, const unsigned char *key, unsigned int keybits ) { enum AES_BITS keybit; @@ -122,17 +120,17 @@ int aes_setkey_dec( AES_CTX *ctx, const unsigned char *key, } -static void aes_process_enable(AES_CTX *ctx, int mode) +static void esp_aes_process_enable(AES_CTX *ctx, int mode) { if( mode == AES_ENCRYPT ){ - aes_setkey_enc(ctx, ctx->enc.key, ctx->enc.keybites); + esp_aes_setkey_enc(ctx, ctx->enc.key, ctx->enc.keybites); }else{ - aes_setkey_dec(ctx, ctx->dec.key, ctx->dec.keybites); + esp_aes_setkey_dec(ctx, ctx->dec.key, ctx->dec.keybites); } return; } -static void aes_process_disable(AES_CTX *ctx, int mode) +static void esp_aes_process_disable(AES_CTX *ctx, int mode) { } @@ -141,7 +139,7 @@ static void aes_process_disable(AES_CTX *ctx, int mode) * AES-ECB block encryption */ -void aes_encrypt( AES_CTX *ctx, +void esp_aes_encrypt( AES_CTX *ctx, const unsigned char input[16], unsigned char output[16] ) { @@ -154,7 +152,7 @@ void aes_encrypt( AES_CTX *ctx, * AES-ECB block decryption */ -void aes_decrypt( AES_CTX *ctx, +void esp_aes_decrypt( AES_CTX *ctx, const unsigned char input[16], unsigned char output[16] ) { @@ -166,15 +164,15 @@ void aes_decrypt( AES_CTX *ctx, /* * AES-ECB block encryption/decryption */ -int aes_crypt_ecb( AES_CTX *ctx, +int esp_aes_crypt_ecb( AES_CTX *ctx, int mode, const unsigned char input[16], unsigned char output[16] ) { if( mode == AES_ENCRYPT ) - aes_encrypt( ctx, input, output ); + esp_aes_encrypt( ctx, input, output ); else - aes_decrypt( ctx, input, output ); + esp_aes_decrypt( ctx, input, output ); return 0; } @@ -182,7 +180,7 @@ int aes_crypt_ecb( AES_CTX *ctx, /* * AES-CBC buffer encryption/decryption */ -int aes_crypt_cbc( AES_CTX *ctx, +int esp_aes_crypt_cbc( AES_CTX *ctx, int mode, size_t length, unsigned char iv[16], @@ -197,13 +195,13 @@ int aes_crypt_cbc( AES_CTX *ctx, AES_LOCK(); - aes_process_enable(ctx, mode); + esp_aes_process_enable(ctx, mode); if( mode == AES_DECRYPT ) { while( length > 0 ) { memcpy( temp, input, 16 ); - aes_crypt_ecb( ctx, mode, input, output ); + esp_aes_crypt_ecb( ctx, mode, input, output ); for( i = 0; i < 16; i++ ) output[i] = (unsigned char)( output[i] ^ iv[i] ); @@ -222,7 +220,7 @@ int aes_crypt_cbc( AES_CTX *ctx, for( i = 0; i < 16; i++ ) output[i] = (unsigned char)( input[i] ^ iv[i] ); - aes_crypt_ecb( ctx, mode, output, output ); + esp_aes_crypt_ecb( ctx, mode, output, output ); memcpy( iv, output, 16 ); input += 16; @@ -230,7 +228,7 @@ int aes_crypt_cbc( AES_CTX *ctx, length -= 16; } } - aes_process_disable(ctx, mode); + esp_aes_process_disable(ctx, mode); AES_UNLOCK(); @@ -240,7 +238,7 @@ int aes_crypt_cbc( AES_CTX *ctx, /* * AES-CFB128 buffer encryption/decryption */ -int aes_crypt_cfb128( AES_CTX *ctx, +int esp_aes_crypt_cfb128( AES_CTX *ctx, int mode, size_t length, size_t *iv_off, @@ -253,13 +251,13 @@ int aes_crypt_cfb128( AES_CTX *ctx, AES_LOCK(); - aes_process_enable(ctx, mode); + esp_aes_process_enable(ctx, mode); if( mode == AES_DECRYPT ) { while( length-- ) { if( n == 0 ) - aes_crypt_ecb( ctx, AES_ENCRYPT, iv, iv ); + esp_aes_crypt_ecb( ctx, AES_ENCRYPT, iv, iv ); c = *input++; *output++ = (unsigned char)( c ^ iv[n] ); @@ -273,7 +271,7 @@ int aes_crypt_cfb128( AES_CTX *ctx, while( length-- ) { if( n == 0 ) - aes_crypt_ecb( ctx, AES_ENCRYPT, iv, iv ); + esp_aes_crypt_ecb( ctx, AES_ENCRYPT, iv, iv ); iv[n] = *output++ = (unsigned char)( iv[n] ^ *input++ ); @@ -282,7 +280,7 @@ int aes_crypt_cfb128( AES_CTX *ctx, } *iv_off = n; - aes_process_disable(ctx, mode); + esp_aes_process_disable(ctx, mode); AES_UNLOCK(); @@ -292,7 +290,7 @@ int aes_crypt_cfb128( AES_CTX *ctx, /* * AES-CFB8 buffer encryption/decryption */ -int aes_crypt_cfb8( AES_CTX *ctx, +int esp_aes_crypt_cfb8( AES_CTX *ctx, int mode, size_t length, unsigned char iv[16], @@ -304,11 +302,11 @@ int aes_crypt_cfb8( AES_CTX *ctx, AES_LOCK(); - aes_process_enable(ctx, mode); + esp_aes_process_enable(ctx, mode); while( length-- ) { memcpy( ov, iv, 16 ); - aes_crypt_ecb( ctx, AES_ENCRYPT, iv, iv ); + esp_aes_crypt_ecb( ctx, AES_ENCRYPT, iv, iv ); if( mode == AES_DECRYPT ) ov[16] = *input; @@ -320,7 +318,7 @@ int aes_crypt_cfb8( AES_CTX *ctx, memcpy( iv, ov + 1, 16 ); } - aes_process_disable(ctx, mode); + esp_aes_process_disable(ctx, mode); AES_UNLOCK(); @@ -330,7 +328,7 @@ int aes_crypt_cfb8( AES_CTX *ctx, /* * AES-CTR buffer encryption/decryption */ -int aes_crypt_ctr( AES_CTX *ctx, +int esp_aes_crypt_ctr( AES_CTX *ctx, size_t length, size_t *nc_off, unsigned char nonce_counter[16], @@ -343,12 +341,12 @@ int aes_crypt_ctr( AES_CTX *ctx, AES_LOCK(); - aes_process_enable(ctx, AES_ENCRYPT); + esp_aes_process_enable(ctx, AES_ENCRYPT); while( length-- ) { if( n == 0 ) { - aes_crypt_ecb( ctx, AES_ENCRYPT, nonce_counter, stream_block ); + esp_aes_crypt_ecb( ctx, AES_ENCRYPT, nonce_counter, stream_block ); for( i = 16; i > 0; i-- ) if( ++nonce_counter[i - 1] != 0 ) @@ -361,12 +359,10 @@ int aes_crypt_ctr( AES_CTX *ctx, } *nc_off = n; - aes_process_disable(ctx, AES_ENCRYPT); + esp_aes_process_disable(ctx, AES_ENCRYPT); AES_UNLOCK(); return 0; } -#endif /* AES_ALT_C */ - diff --git a/components/mbedtls/port/bignum_alt.c b/components/esp32/bignum.c similarity index 63% rename from components/mbedtls/port/bignum_alt.c rename to components/esp32/bignum.c index 2da6e71693..02d5a9c3d2 100644 --- a/components/mbedtls/port/bignum_alt.c +++ b/components/esp32/bignum.c @@ -33,19 +33,19 @@ * https://gmplib.org/manual/index.html * */ -#include "port/bignum_alt.h" +#include "bignum.h" #if defined(ESP_BIGNUM_ALT) #include #include -#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 */ @@ -61,7 +61,7 @@ static void mpi_zeroize( void *v, size_t n ) { /* * Initialize one MPI */ -void mpi_init( mpi *X ) +void esp_mpi_init( mpi *X ) { if( X == NULL ) return; @@ -78,14 +78,14 @@ void mpi_init( mpi *X ) /* * 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 ); } @@ -102,9 +102,9 @@ void mpi_free( mpi *X ) /* * 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 ); @@ -117,7 +117,7 @@ int mpi_grow( mpi *X, size_t nblimbs ) 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 ); } @@ -132,14 +132,14 @@ int mpi_grow( mpi *X, size_t nblimbs ) * 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 ) @@ -155,7 +155,7 @@ int mpi_shrink( mpi *X, size_t nblimbs ) 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 ); } @@ -168,7 +168,7 @@ int mpi_shrink( mpi *X, size_t nblimbs ) /* * 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; @@ -178,7 +178,7 @@ int mpi_copy( mpi *X, const mpi *Y ) if( Y->p == NULL ) { - mpi_free( X ); + esp_mpi_free( X ); return( 0 ); } @@ -189,7 +189,7 @@ int mpi_copy( mpi *X, const mpi *Y ) 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 ); @@ -202,7 +202,7 @@ cleanup: /* * Swap the contents of X and Y */ -void mpi_swap( mpi *X, mpi *Y ) +void esp_mpi_swap( mpi *X, mpi *Y ) { mpi T; @@ -216,7 +216,7 @@ void mpi_swap( mpi *X, mpi *Y ) * 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; @@ -224,7 +224,7 @@ int mpi_safe_cond_assign( mpi *X, const mpi *Y, unsigned char assign ) /* 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; @@ -244,11 +244,11 @@ cleanup: * 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 ); @@ -256,8 +256,8 @@ int mpi_safe_cond_swap( mpi *X, mpi *Y, unsigned char swap ) /* 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; @@ -278,11 +278,11 @@ cleanup: /* * 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; @@ -296,7 +296,7 @@ cleanup: /* * 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 ); @@ -307,7 +307,7 @@ int mpi_get_bit( const mpi *X, size_t pos ) /* * 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; @@ -321,11 +321,11 @@ int mpi_set_bit( mpi *X, size_t pos, unsigned char val ) 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: @@ -335,7 +335,7 @@ 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; @@ -350,10 +350,10 @@ size_t mpi_lsb( const mpi *X ) /* * 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++ ) { @@ -368,7 +368,7 @@ static size_t clz( const mpi_uint x ) /* * Return the number of bits */ -size_t mpi_bitlen( const mpi *X ) +size_t esp_mpi_bitlen( const mpi *X ) { size_t i, j; @@ -387,15 +387,15 @@ size_t mpi_bitlen( const mpi *X ) /* * 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; @@ -403,7 +403,7 @@ static int mpi_get_digit( mpi_uint *d, int radix, char c ) 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 ); @@ -412,17 +412,17 @@ static int mpi_get_digit( mpi_uint *d, int radix, char c ) /* * 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 ); @@ -433,8 +433,8 @@ int mpi_read_string( mpi *X, int radix, const char *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++ ) { @@ -444,13 +444,13 @@ int mpi_read_string( mpi *X, int radix, const char *s ) 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++ ) { @@ -460,23 +460,23 @@ int mpi_read_string( mpi *X, int radix, const char *s ) 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 ); } @@ -484,19 +484,19 @@ cleanup: /* * 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 ); @@ -511,7 +511,7 @@ cleanup: /* * 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; @@ -522,7 +522,7 @@ int mpi_write_string( const mpi *X, int radix, 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; @@ -534,7 +534,7 @@ int mpi_write_string( const mpi *X, int radix, } p = buf; - mpi_init( &T ); + esp_mpi_init( &T ); if( X->s == -1 ) *p++ = '-'; @@ -561,12 +561,12 @@ int mpi_write_string( const mpi *X, int radix, } 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'; @@ -574,7 +574,7 @@ int mpi_write_string( const mpi *X, int radix, cleanup: - mpi_free( &T ); + esp_mpi_free( &T ); return( ret ); } @@ -582,7 +582,7 @@ cleanup: /* * 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; @@ -591,11 +591,11 @@ int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen ) 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: @@ -605,11 +605,11 @@ 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 ); @@ -625,19 +625,19 @@ int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen ) /* * 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; @@ -675,16 +675,16 @@ cleanup: /* * 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 @@ -718,7 +718,7 @@ int mpi_shift_r( mpi *X, size_t count ) /* * 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; @@ -748,7 +748,7 @@ int mpi_cmp_abs( const mpi *X, const mpi *Y ) /* * 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; @@ -781,27 +781,27 @@ int mpi_cmp_mpi( const mpi *X, const mpi *Y ) /* * 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 ) { @@ -809,7 +809,7 @@ int mpi_add_abs( mpi *X, const mpi *A, const mpi *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. @@ -820,7 +820,7 @@ int mpi_add_abs( mpi *X, const mpi *A, const mpi *B ) 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; @@ -834,7 +834,7 @@ int mpi_add_abs( mpi *X, const mpi *A, const mpi *B ) { if( i >= X->n ) { - MPI_CHK( mpi_grow( X, i + 1 ) ); + MPI_CHK( esp_mpi_grow( X, i + 1 ) ); p = X->p + i; } @@ -849,10 +849,10 @@ cleanup: /* * 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++ ) { @@ -870,25 +870,25 @@ static void mpi_sub_hlp( size_t n, mpi_uint *s, mpi_uint *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. @@ -901,11 +901,11 @@ int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B ) 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 ); } @@ -913,26 +913,26 @@ cleanup: /* * 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; } @@ -944,26 +944,26 @@ cleanup: /* * 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; } @@ -975,39 +975,39 @@ cleanup: /* * 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 ) { } @@ -1015,7 +1015,7 @@ static void mpi_mul_hlp( size_t i, mpi_uint *s, mpi_uint *d, 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; @@ -1023,10 +1023,10 @@ int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B ) 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 ) @@ -1036,19 +1036,19 @@ int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B ) 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(); @@ -1056,7 +1056,7 @@ int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B ) cleanup: - mpi_free( &TB ); mpi_free( &TA ); + esp_mpi_free( &TB ); esp_mpi_free( &TA ); return( ret ); } @@ -1064,33 +1064,33 @@ cleanup: /* * 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 @@ -1112,9 +1112,9 @@ static mpi_uint int_div_int( mpi_uint u1, 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 /* @@ -1129,7 +1129,7 @@ static mpi_uint int_div_int( mpi_uint u1, 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; @@ -1176,53 +1176,53 @@ static mpi_uint int_div_int( mpi_uint u1, /* * 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-- ) { @@ -1239,51 +1239,51 @@ int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B ) { 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 ); } @@ -1291,36 +1291,36 @@ cleanup: /* * 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: @@ -1330,10 +1330,10 @@ 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 ); @@ -1387,9 +1387,9 @@ int mpi_mod_int( mpi_uint *r, const mpi *A, mpi_sint b ) /* * 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; @@ -1404,11 +1404,11 @@ static void mpi_montg_init( mpi_uint *mm, const mpi *N ) /* * 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 ); @@ -1422,7 +1422,7 @@ static void mpi_montmul( mpi *A, const mpi *B, const mpi *N, mpi_uint mm, ets_bigint_montgomery_mult_getz(A->p, n); } else{ - mpi_printf("Montgomery multiplication failed\n"); + esp_mpi_printf("Montgomery multiplication failed\n"); } BIGNUM_UNLOCK(); @@ -1431,45 +1431,45 @@ static void mpi_montmul( mpi *A, const mpi *B, const mpi *N, mpi_uint mm, /* * 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; @@ -1478,9 +1478,9 @@ int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR ) 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) @@ -1488,7 +1488,7 @@ int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR ) neg = ( A->s == -1 ); if( neg ) { - MPI_CHK( mpi_copy( &Apos, A ) ); + MPI_CHK( esp_mpi_copy( &Apos, A ) ); Apos.s = 1; A = &Apos; } @@ -1498,9 +1498,9 @@ int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR ) */ 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 ) ); @@ -1511,18 +1511,18 @@ int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR ) /* * 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 ) { @@ -1531,21 +1531,21 @@ int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR ) */ 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 ); } } @@ -1564,7 +1564,7 @@ int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR ) nblimbs--; - bufsize = sizeof( mpi_uint ) << 3; + bufsize = sizeof( esp_mpi_uint ) << 3; } bufsize--; @@ -1582,7 +1582,7 @@ int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR ) /* * out of window, square X */ - mpi_montmul( X, X, N, mm, &T ); + esp_mpi_montmul( X, X, N, mm, &T ); continue; } @@ -1600,12 +1600,12 @@ int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR ) * 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; @@ -1618,34 +1618,34 @@ int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR ) */ 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 ); } @@ -1653,51 +1653,51 @@ cleanup: /* * 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 ); } @@ -1709,7 +1709,7 @@ cleanup: * 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 ) { @@ -1720,7 +1720,7 @@ int mpi_fill_random( mpi *X, size_t size, 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 ); @@ -1729,94 +1729,94 @@ cleanup: /* * 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 ); } @@ -1855,21 +1855,21 @@ static const int small_prime[] = * 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 ); @@ -1882,7 +1882,7 @@ cleanup: /* * 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 ) { @@ -1890,19 +1890,19 @@ static int mpi_miller_rabin( const mpi *X, 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 */ @@ -1915,51 +1915,51 @@ static int mpi_miller_rabin( const mpi *X, /* * 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++; @@ -1968,8 +1968,8 @@ static int mpi_miller_rabin( const mpi *X, /* * 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; @@ -1977,8 +1977,8 @@ static int mpi_miller_rabin( const mpi *X, } 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 ); } @@ -1986,7 +1986,7 @@ cleanup: /* * 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 ) { @@ -1997,14 +1997,14 @@ int mpi_is_prime( const mpi *X, 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 ); @@ -2012,45 +2012,45 @@ int mpi_is_prime( const mpi *X, 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 @@ -2063,15 +2063,15 @@ int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag, 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 ) { @@ -2079,10 +2079,10 @@ int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag, * 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; } @@ -2095,14 +2095,14 @@ int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag, * 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 ); } diff --git a/components/esp32/esp_thread.c b/components/esp32/esp_thread.c new file mode 100644 index 0000000000..e72dc996bb --- /dev/null +++ b/components/esp32/esp_thread.c @@ -0,0 +1,53 @@ +#include "esp_thread.h" +#include "freertos/FreeRTOS.h" +#include "freertos/semphr.h" + +static xSemaphoreHandle esp_thread_mutex[MUTEX_MAX_NUM]; +static int esp_thread_sig[MUTEX_MAX_NUM]; + +int esp_thread_init(void) +{ + int i; + + for (i = 0; i < MUTEX_MAX_NUM; i++) { + esp_thread_mutex[i] = xSemaphoreCreateMutex(); + if (!esp_thread_mutex[i]) { + goto failed1; + } + esp_thread_sig[i] = 0; + } + + return 0; + +failed1: + for (i--; i >= 0; i--) + vQueueDelete(esp_thread_mutex[i]); + + return -1; +} + +void esp_thread_lock(unsigned int num) +{ + xSemaphoreTake(esp_thread_mutex[num], portMAX_DELAY); +} + +void esp_thread_unlock(unsigned int num) +{ + xSemaphoreGive(esp_thread_mutex[num]); +} + +void esp_thread_take(unsigned int num) +{ + esp_thread_sig[num]++; +} + +void esp_thread_give(unsigned int num) +{ + esp_thread_sig[num]--; +} + +bool esp_thread_is_used(unsigned int num) +{ + return (esp_thread_sig[num] != 0) ? true : false; +} + diff --git a/components/mbedtls/include/port/aes_alt.h b/components/esp32/include/aes.h similarity index 89% rename from components/mbedtls/include/port/aes_alt.h rename to components/esp32/include/aes.h index a02f32f299..aa81e1b5f6 100644 --- a/components/mbedtls/include/port/aes_alt.h +++ b/components/esp32/include/aes.h @@ -1,5 +1,5 @@ /** - * \file aes_alt.h + * \file esp_aes.h * * \brief AES block cipher * @@ -21,8 +21,8 @@ * */ -#ifndef AES_ALT_H -#define AES_ALT_H +#ifndef ESP_AES_H +#define ESP_AES_H #include "c_types.h" #include "rom/ets_sys.h" @@ -32,8 +32,6 @@ extern "C" { #endif -#define ESP_AES_C - /* padlock.c and aesni.c rely on these values! */ #define AES_ENCRYPT 1 #define AES_DECRYPT 0 @@ -61,23 +59,21 @@ typedef struct uint32_t *rk; /*!< AES round keys */ KEY_CTX enc; KEY_CTX dec; -}aes_context; - -typedef aes_context AES_CTX; +}aes_context, AES_CTX; /** * \brief Initialize AES context * * \param ctx AES context to be initialized */ -void aes_init( AES_CTX *ctx ); +void esp_aes_init( AES_CTX *ctx ); /** * \brief Clear AES context * * \param ctx AES context to be cleared */ -void aes_free( AES_CTX *ctx ); +void esp_aes_free( AES_CTX *ctx ); /** * \brief AES key schedule (encryption) @@ -88,7 +84,7 @@ void aes_free( AES_CTX *ctx ); * * \return 0 if successful, or ERR_AES_INVALID_KEY_LENGTH */ -int aes_setkey_enc( AES_CTX *ctx, const unsigned char *key,unsigned int keybits ); +int esp_aes_setkey_enc( AES_CTX *ctx, const unsigned char *key,unsigned int keybits ); /** * \brief AES key schedule (decryption) @@ -99,7 +95,7 @@ int aes_setkey_enc( AES_CTX *ctx, const unsigned char *key,unsigned int keybits * * \return 0 if successful, or ERR_AES_INVALID_KEY_LENGTH */ -int aes_setkey_dec( AES_CTX *ctx, const unsigned char *key,unsigned int keybits ); +int esp_aes_setkey_dec( AES_CTX *ctx, const unsigned char *key,unsigned int keybits ); /** * \brief AES-ECB block encryption/decryption @@ -111,7 +107,7 @@ int aes_setkey_dec( AES_CTX *ctx, const unsigned char *key,unsigned int keybits * * \return 0 if successful */ -int aes_crypt_ecb( AES_CTX *ctx,int mode,const unsigned char input[16],unsigned char output[16] ); +int esp_aes_crypt_ecb( AES_CTX *ctx,int mode,const unsigned char input[16],unsigned char output[16] ); /** * \brief AES-CBC buffer encryption/decryption @@ -135,7 +131,7 @@ int aes_crypt_ecb( AES_CTX *ctx,int mode,const unsigned char input[16],unsigned * * \return 0 if successful, or ERR_AES_INVALID_INPUT_LENGTH */ -int aes_crypt_cbc( AES_CTX *ctx, +int esp_aes_crypt_cbc( AES_CTX *ctx, int mode, size_t length, unsigned char iv[16], @@ -148,7 +144,7 @@ int aes_crypt_cbc( AES_CTX *ctx, * * Note: Due to the nature of CFB you should use the same key schedule for * both encryption and decryption. So a context initialized with - * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT. + * esp_aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT. * * \note Upon exit, the content of the IV is updated so that you can * call the function same function again on the following @@ -168,7 +164,7 @@ int aes_crypt_cbc( AES_CTX *ctx, * * \return 0 if successful */ -int aes_crypt_cfb128( AES_CTX *ctx, +int esp_aes_crypt_cfb128( AES_CTX *ctx, int mode, size_t length, size_t *iv_off, @@ -181,7 +177,7 @@ int aes_crypt_cfb128( AES_CTX *ctx, * * Note: Due to the nature of CFB you should use the same key schedule for * both encryption and decryption. So a context initialized with - * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT. + * esp_aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT. * * \note Upon exit, the content of the IV is updated so that you can * call the function same function again on the following @@ -200,7 +196,7 @@ int aes_crypt_cfb128( AES_CTX *ctx, * * \return 0 if successful */ -int aes_crypt_cfb8( AES_CTX *ctx, +int esp_aes_crypt_cfb8( AES_CTX *ctx, int mode, size_t length, unsigned char iv[16], @@ -214,7 +210,7 @@ int aes_crypt_cfb8( AES_CTX *ctx, * * Note: Due to the nature of CTR you should use the same key schedule for * both encryption and decryption. So a context initialized with - * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT. + * esp_aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT. * * \param ctx AES context * \param length The length of the data @@ -229,7 +225,7 @@ int aes_crypt_cfb8( AES_CTX *ctx, * * \return 0 if successful */ -int aes_crypt_ctr( AES_CTX *ctx, +int esp_aes_crypt_ctr( AES_CTX *ctx, size_t length, size_t *nc_off, unsigned char nonce_counter[16], @@ -247,7 +243,7 @@ int aes_crypt_ctr( AES_CTX *ctx, * \param input Plaintext block * \param output Output (ciphertext) block */ -void aes_encrypt( AES_CTX *ctx, const unsigned char input[16],unsigned char output[16] ); +void esp_aes_encrypt( AES_CTX *ctx, const unsigned char input[16],unsigned char output[16] ); /** * \brief Internal AES block decryption function @@ -258,7 +254,7 @@ void aes_encrypt( AES_CTX *ctx, const unsigned char input[16],unsigned char outp * \param input Ciphertext block * \param output Output (plaintext) block */ -void aes_decrypt( AES_CTX *ctx, const unsigned char input[16], unsigned char output[16] ); +void esp_aes_decrypt( AES_CTX *ctx, const unsigned char input[16], unsigned char output[16] ); #ifdef __cplusplus } diff --git a/components/mbedtls/include/port/bignum_alt.h b/components/esp32/include/bignum.h similarity index 86% rename from components/mbedtls/include/port/bignum_alt.h rename to components/esp32/include/bignum.h index 64d492a989..4cb1ca88f1 100644 --- a/components/mbedtls/include/port/bignum_alt.h +++ b/components/esp32/include/bignum.h @@ -20,8 +20,8 @@ * */ -#ifndef BIGNUM_ALT_H -#define BIGNUM_ALT_H +#ifndef _ESP_BIGNUM_H +#define _ESP_BIGNUM_H #include "c_types.h" #include "rom/ets_sys.h" @@ -42,9 +42,9 @@ #define MPI_CHK(f) do { if( ( ret = f ) != 0 ) goto cleanup; } while( 0 ) #if defined(MPI_DEBUG_ALT) -#define mpi_printf ets_printf +#define esp_mpi_printf ets_printf #else -#define mpi_printf +#define esp_mpi_printf #endif /* * Maximum size MPIs are allowed to grow to in number of limbs. @@ -78,8 +78,8 @@ #define MPI_MAX_BITS ( 8 * MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */ /* - * When reading from files with mpi_read_file() and writing to files with - * mpi_write_file() the buffer should have space + * When reading from files with esp_mpi_read_file() and writing to files with + * esp_mpi_write_file() the buffer should have space * for a (short) label, the MPI (in the provided radix), the newline * characters and the '\0'. * @@ -108,8 +108,8 @@ #if ( ! defined(HAVE_INT32) && \ defined(_MSC_VER) && defined(_M_AMD64) ) #define HAVE_INT64 - typedef int64_t mpi_sint; - typedef uint64_t mpi_uint; + typedef int64_t esp_mpi_sint; + typedef uint64_t esp_mpi_uint; #else #if ( ! defined(HAVE_INT32) && \ defined(__GNUC__) && ( \ @@ -119,15 +119,15 @@ (defined(__sparc__) && defined(__arch64__)) || \ defined(__s390x__) || defined(__mips64) ) ) #define HAVE_INT64 - typedef int64_t mpi_sint; - typedef uint64_t mpi_uint; + typedef int64_t esp_mpi_sint; + typedef uint64_t esp_mpi_uint; /* t_udbl defined as 128-bit unsigned int */ typedef unsigned int t_udbl __attribute__((mode(TI))); #define HAVE_UDBL #else #define HAVE_INT32 - typedef int32_t mpi_sint; - typedef uint32_t mpi_uint; + typedef int32_t esp_mpi_sint; + typedef uint32_t esp_mpi_uint; typedef uint64_t t_udbl; #define HAVE_UDBL #endif /* !HAVE_INT32 && __GNUC__ && 64-bit platform */ @@ -144,9 +144,8 @@ typedef struct { int s; /*!< integer sign */ size_t n; /*!< total # of limbs */ - mpi_uint *p; /*!< pointer to limbs */ -} -mpi; + esp_mpi_uint *p; /*!< pointer to limbs */ +}mpi, MPI_CTX; /** * \brief Initialize one MPI (make internal references valid) @@ -155,14 +154,14 @@ mpi; * * \param X One MPI to initialize. */ -void mpi_init( mpi *X ); +void esp_mpi_init( mpi *X ); /** * \brief Unallocate one MPI * * \param X One MPI to unallocate. */ -void mpi_free( mpi *X ); +void esp_mpi_free( mpi *X ); /** * \brief Enlarge to the specified number of limbs @@ -173,7 +172,7 @@ void mpi_free( mpi *X ); * \return 0 if successful, * ERR_MPI_ALLOC_FAILED if memory allocation failed */ -int mpi_grow( mpi *X, size_t nblimbs ); +int esp_mpi_grow( mpi *X, size_t nblimbs ); /** * \brief Resize down, keeping at least the specified number of limbs @@ -184,7 +183,7 @@ int mpi_grow( mpi *X, size_t nblimbs ); * \return 0 if successful, * ERR_MPI_ALLOC_FAILED if memory allocation failed */ -int mpi_shrink( mpi *X, size_t nblimbs ); +int esp_mpi_shrink( mpi *X, size_t nblimbs ); /** * \brief Copy the contents of Y into X @@ -195,7 +194,7 @@ int mpi_shrink( mpi *X, size_t nblimbs ); * \return 0 if successful, * ERR_MPI_ALLOC_FAILED if memory allocation failed */ -int mpi_copy( mpi *X, const mpi *Y ); +int esp_mpi_copy( mpi *X, const mpi *Y ); /** * \brief Swap the contents of X and Y @@ -203,7 +202,7 @@ int mpi_copy( mpi *X, const mpi *Y ); * \param X First MPI value * \param Y Second MPI value */ -void mpi_swap( mpi *X, mpi *Y ); +void esp_mpi_swap( mpi *X, mpi *Y ); /** * \brief Safe conditional assignement X = Y if assign is 1 @@ -216,13 +215,13 @@ void mpi_swap( mpi *X, mpi *Y ); * ERR_MPI_ALLOC_FAILED if memory allocation failed, * * \note This function is equivalent to - * if( assign ) mpi_copy( X, Y ); + * if( assign ) esp_mpi_copy( X, Y ); * except that it avoids leaking any information about whether * the assignment was done or not (the above code may leak * information through branch prediction and/or memory access * patterns analysis). */ -int mpi_safe_cond_assign( mpi *X, const mpi *Y, unsigned char assign ); +int esp_mpi_safe_cond_assign( mpi *X, const mpi *Y, unsigned char assign ); /** * \brief Safe conditional swap X <-> Y if swap is 1 @@ -235,13 +234,13 @@ int mpi_safe_cond_assign( mpi *X, const mpi *Y, unsigned char assign ); * ERR_MPI_ALLOC_FAILED if memory allocation failed, * * \note This function is equivalent to - * if( assign ) mpi_swap( X, Y ); + * if( assign ) esp_mpi_swap( X, Y ); * except that it avoids leaking any information about whether * the assignment was done or not (the above code may leak * information through branch prediction and/or memory access * patterns analysis). */ -int mpi_safe_cond_swap( mpi *X, mpi *Y, unsigned char assign ); +int esp_mpi_safe_cond_swap( mpi *X, mpi *Y, unsigned char assign ); /** * \brief Set value from integer @@ -252,7 +251,7 @@ int mpi_safe_cond_swap( mpi *X, mpi *Y, unsigned char assign ); * \return 0 if successful, * ERR_MPI_ALLOC_FAILED if memory allocation failed */ -int mpi_lset( mpi *X, mpi_sint z ); +int esp_mpi_lset( mpi *X, esp_mpi_sint z ); /** * \brief Get a specific bit from X @@ -262,7 +261,7 @@ int mpi_lset( mpi *X, mpi_sint z ); * * \return Either a 0 or a 1 */ -int mpi_get_bit( const mpi *X, size_t pos ); +int esp_mpi_get_bit( const mpi *X, size_t pos ); /** * \brief Set a bit of X to a specific value of 0 or 1 @@ -278,7 +277,7 @@ int mpi_get_bit( const mpi *X, size_t pos ); * ERR_MPI_ALLOC_FAILED if memory allocation failed, * ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1 */ -int mpi_set_bit( mpi *X, size_t pos, unsigned char val ); +int esp_mpi_set_bit( mpi *X, size_t pos, unsigned char val ); /** * \brief Return the number of zero-bits before the least significant @@ -288,7 +287,7 @@ int mpi_set_bit( mpi *X, size_t pos, unsigned char val ); * * \param X MPI to use */ -size_t mpi_lsb( const mpi *X ); +size_t esp_mpi_lsb( const mpi *X ); /** * \brief Return the number of bits up to and including the most @@ -298,14 +297,14 @@ size_t mpi_lsb( const mpi *X ); * * \param X MPI to use */ -size_t mpi_bitlen( const mpi *X ); +size_t esp_mpi_bitlen( const mpi *X ); /** * \brief Return the total size in bytes * * \param X MPI to use */ -size_t mpi_size( const mpi *X ); +size_t esp_mpi_size( const mpi *X ); /** * \brief Import from an ASCII string @@ -316,7 +315,7 @@ size_t mpi_size( const mpi *X ); * * \return 0 if successful, or a ERR_MPI_XXX error code */ -int mpi_read_string( mpi *X, int radix, const char *s ); +int esp_mpi_read_string( mpi *X, int radix, const char *s ); /** * \brief Export into an ASCII string @@ -334,7 +333,7 @@ int mpi_read_string( mpi *X, int radix, const char *s ); * \note Call this function with buflen = 0 to obtain the * minimum required buffer size in *olen. */ -int mpi_write_string( const mpi *X, int radix, +int esp_mpi_write_string( const mpi *X, int radix, char *buf, size_t buflen, size_t *olen ); #if defined(FS_IO) @@ -349,7 +348,7 @@ int mpi_write_string( const mpi *X, int radix, * the file read buffer is too small or a * ERR_MPI_XXX error code */ -int mpi_read_file( mpi *X, int radix, FILE *fin ); +int esp_mpi_read_file( mpi *X, int radix, FILE *fin ); /** * \brief Write X into an opened file, or stdout if fout is NULL @@ -363,7 +362,7 @@ int mpi_read_file( mpi *X, int radix, FILE *fin ); * * \note Set fout == NULL to print X on the console. */ -int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout ); +int esp_mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout ); #endif /* FS_IO */ /** @@ -376,7 +375,7 @@ int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout ); * \return 0 if successful, * ERR_MPI_ALLOC_FAILED if memory allocation failed */ -int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen ); +int esp_mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen ); /** * \brief Export X into unsigned binary data, big endian. @@ -390,7 +389,7 @@ int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen ); * \return 0 if successful, * ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough */ -int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen ); +int esp_mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen ); /** * \brief Left-shift: X <<= count @@ -401,7 +400,7 @@ int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen ); * \return 0 if successful, * ERR_MPI_ALLOC_FAILED if memory allocation failed */ -int mpi_shift_l( mpi *X, size_t count ); +int esp_mpi_shift_l( mpi *X, size_t count ); /** * \brief Right-shift: X >>= count @@ -412,7 +411,7 @@ int mpi_shift_l( mpi *X, size_t count ); * \return 0 if successful, * ERR_MPI_ALLOC_FAILED if memory allocation failed */ -int mpi_shift_r( mpi *X, size_t count ); +int esp_mpi_shift_r( mpi *X, size_t count ); /** * \brief Compare unsigned values @@ -424,7 +423,7 @@ int mpi_shift_r( mpi *X, size_t count ); * -1 if |X| is lesser than |Y| or * 0 if |X| is equal to |Y| */ -int mpi_cmp_abs( const mpi *X, const mpi *Y ); +int esp_mpi_cmp_abs( const mpi *X, const mpi *Y ); /** * \brief Compare signed values @@ -436,7 +435,7 @@ int mpi_cmp_abs( const mpi *X, const mpi *Y ); * -1 if X is lesser than Y or * 0 if X is equal to Y */ -int mpi_cmp_mpi( const mpi *X, const mpi *Y ); +int esp_mpi_cmp_mpi( const mpi *X, const mpi *Y ); /** * \brief Compare signed values @@ -448,7 +447,7 @@ int mpi_cmp_mpi( const mpi *X, const mpi *Y ); * -1 if X is lesser than z or * 0 if X is equal to z */ -int mpi_cmp_int( const mpi *X, mpi_sint z ); +int esp_mpi_cmp_int( const mpi *X, esp_mpi_sint z ); /** * \brief Unsigned addition: X = |A| + |B| @@ -460,7 +459,7 @@ int mpi_cmp_int( const mpi *X, mpi_sint z ); * \return 0 if successful, * ERR_MPI_ALLOC_FAILED if memory allocation failed */ -int mpi_add_abs( mpi *X, const mpi *A, const mpi *B ); +int esp_mpi_add_abs( mpi *X, const mpi *A, const mpi *B ); /** * \brief Unsigned subtraction: X = |A| - |B| @@ -472,7 +471,7 @@ int mpi_add_abs( mpi *X, const mpi *A, const mpi *B ); * \return 0 if successful, * ERR_MPI_NEGATIVE_VALUE if B is greater than A */ -int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B ); +int esp_mpi_sub_abs( mpi *X, const mpi *A, const mpi *B ); /** * \brief Signed addition: X = A + B @@ -484,7 +483,7 @@ int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B ); * \return 0 if successful, * ERR_MPI_ALLOC_FAILED if memory allocation failed */ -int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B ); +int esp_mpi_add_mpi( mpi *X, const mpi *A, const mpi *B ); /** * \brief Signed subtraction: X = A - B @@ -496,7 +495,7 @@ int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B ); * \return 0 if successful, * ERR_MPI_ALLOC_FAILED if memory allocation failed */ -int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B ); +int esp_mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B ); /** * \brief Signed addition: X = A + b @@ -508,7 +507,7 @@ int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B ); * \return 0 if successful, * ERR_MPI_ALLOC_FAILED if memory allocation failed */ -int mpi_add_int( mpi *X, const mpi *A, mpi_sint b ); +int esp_mpi_add_int( mpi *X, const mpi *A, esp_mpi_sint b ); /** * \brief Signed subtraction: X = A - b @@ -520,7 +519,7 @@ int mpi_add_int( mpi *X, const mpi *A, mpi_sint b ); * \return 0 if successful, * ERR_MPI_ALLOC_FAILED if memory allocation failed */ -int mpi_sub_int( mpi *X, const mpi *A, mpi_sint b ); +int esp_mpi_sub_int( mpi *X, const mpi *A, esp_mpi_sint b ); /** * \brief Baseline multiplication: X = A * B @@ -532,7 +531,7 @@ int mpi_sub_int( mpi *X, const mpi *A, mpi_sint b ); * \return 0 if successful, * ERR_MPI_ALLOC_FAILED if memory allocation failed */ -int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B ); +int esp_mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B ); /** * \brief Baseline multiplication: X = A * b @@ -546,7 +545,7 @@ int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B ); * \return 0 if successful, * ERR_MPI_ALLOC_FAILED if memory allocation failed */ -int mpi_mul_int( mpi *X, const mpi *A, mpi_uint b ); +int esp_mpi_mul_int( mpi *X, const mpi *A, esp_mpi_uint b ); /** * \brief Division by mpi: A = Q * B + R @@ -562,7 +561,7 @@ int mpi_mul_int( mpi *X, const mpi *A, mpi_uint b ); * * \note Either Q or R can be NULL. */ -int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B ); +int esp_mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B ); /** * \brief Division by int: A = Q * b + R @@ -578,7 +577,7 @@ int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B ); * * \note Either Q or R can be NULL. */ -int mpi_div_int( mpi *Q, mpi *R, const mpi *A, mpi_sint b ); +int esp_mpi_div_int( mpi *Q, mpi *R, const mpi *A, esp_mpi_sint b ); /** * \brief Modulo: R = A mod B @@ -592,12 +591,12 @@ int mpi_div_int( mpi *Q, mpi *R, const mpi *A, mpi_sint b ); * ERR_MPI_DIVISION_BY_ZERO if B == 0, * ERR_MPI_NEGATIVE_VALUE if B < 0 */ -int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B ); +int esp_mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B ); /** * \brief Modulo: r = A mod b * - * \param r Destination mpi_uint + * \param r Destination esp_mpi_uint * \param A Left-hand MPI * \param b Integer to divide by * @@ -606,7 +605,7 @@ int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B ); * ERR_MPI_DIVISION_BY_ZERO if b == 0, * ERR_MPI_NEGATIVE_VALUE if b < 0 */ -int mpi_mod_int( mpi_uint *r, const mpi *A, mpi_sint b ); +int esp_mpi_mod_int( esp_mpi_uint *r, const mpi *A, esp_mpi_sint b ); /** * \brief Sliding-window exponentiation: X = A^E mod N @@ -626,7 +625,7 @@ int mpi_mod_int( mpi_uint *r, const mpi *A, mpi_sint b ); * multiple calls, which speeds up things a bit. It can * be set to NULL if the extra performance is unneeded. */ -int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR ); +int esp_mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR ); /** * \brief Fill an MPI X with size bytes of random @@ -639,7 +638,7 @@ int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR ); * \return 0 if successful, * ERR_MPI_ALLOC_FAILED if memory allocation failed */ -int mpi_fill_random( mpi *X, size_t size, +int esp_mpi_fill_random( mpi *X, size_t size, int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); @@ -653,7 +652,7 @@ int mpi_fill_random( mpi *X, size_t size, * \return 0 if successful, * ERR_MPI_ALLOC_FAILED if memory allocation failed */ -int mpi_gcd( mpi *G, const mpi *A, const mpi *B ); +int esp_mpi_gcd( mpi *G, const mpi *A, const mpi *B ); /** * \brief Modular inverse: X = A^-1 mod N @@ -667,7 +666,7 @@ int mpi_gcd( mpi *G, const mpi *A, const mpi *B ); * ERR_MPI_BAD_INPUT_DATA if N is negative or nil ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N */ -int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N ); +int esp_mpi_inv_mod( mpi *X, const mpi *A, const mpi *N ); /** * \brief Miller-Rabin primality test @@ -680,7 +679,7 @@ int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N ); * ERR_MPI_ALLOC_FAILED if memory allocation failed, * ERR_MPI_NOT_ACCEPTABLE if X is not prime */ -int mpi_is_prime( const mpi *X, +int esp_mpi_is_prime( const mpi *X, int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); @@ -698,7 +697,7 @@ int mpi_is_prime( const mpi *X, * ERR_MPI_ALLOC_FAILED if memory allocation failed, * ERR_MPI_BAD_INPUT_DATA if nbits is < 3 */ -int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag, +int esp_mpi_gen_prime( mpi *X, size_t nbits, int dh_flag, int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); #endif diff --git a/components/esp32/include/esp_thread.h b/components/esp32/include/esp_thread.h new file mode 100644 index 0000000000..7716f00f19 --- /dev/null +++ b/components/esp32/include/esp_thread.h @@ -0,0 +1,56 @@ +#ifndef _MULTI_THREAD_H_ +#define _MULTI_THREAD_H_ + +#include "c_types.h" +#include "rom/ets_sys.h" + +#ifdef __cplusplus +extern "C" { +#endif + +enum { + AES_MUTEX = 0, + BIGNUM_MUTEX, + SHA_MUTEX, + + MUTEX_MAX_NUM, +}; + +int esp_thread_init(void); + +void esp_thread_lock(unsigned int num); +void esp_thread_unlock(unsigned int num); + +void esp_thread_take(unsigned int num); +void esp_thread_give(unsigned int num); +bool esp_thread_is_used(unsigned int num); + +#define MUTEX_LOCK(num) esp_thread_lock(num) +#define MUTEX_UNLOCK(num) esp_thread_unlock(num) + +#define SIG_TAKE(num) esp_thread_take(num) +#define SIG_GIVE(num) esp_thread_give(num) +#define SIG_IS_USED(num) esp_thread_is_used(num) + +#define AES_LOCK() MUTEX_LOCK(AES_MUTEX) +#define AES_UNLOCK() MUTEX_UNLOCK(AES_MUTEX) +#define BIGNUM_LOCK() MUTEX_LOCK(BIGNUM_MUTEX) +#define BIGNUM_UNLOCK() MUTEX_UNLOCK(BIGNUM_MUTEX) +#define SHA_LOCK() MUTEX_LOCK(SHA_MUTEX) +#define SHA_UNLOCK() MUTEX_UNLOCK(SHA_MUTEX) + +#define AES_TAKE() SIG_TAKE(AES_MUTEX) +#define AES_GIVE() SIG_GIVE(AES_MUTEX) +#define AES_IS_USED() SIG_IS_USED(AES_MUTEX) +#define BIGNUM_TAKE() SIG_TAKE(BIGNUM_MUTEX) +#define BIGNUM_GIVE() SIG_GIVE(BIGNUM_MUTEX) +#define BIGNUM_IS_USED() SIG_IS_USED(BIGNUM_MUTEX) +#define SHA_TAKE() SIG_TAKE(SHA_MUTEX) +#define SHA_GIVE() SIG_GIVE(SHA_MUTEX) +#define SHA_IS_USED() SIG_IS_USED(SHA_MUTEX) + +#ifdef __cplusplus +} +#endif + +#endif /* esp_thread.h */ diff --git a/components/esp32/include/sha.h b/components/esp32/include/sha.h new file mode 100644 index 0000000000..661354c1de --- /dev/null +++ b/components/esp32/include/sha.h @@ -0,0 +1,224 @@ +/* + * copyright (c) 2010 - 2012 Espressif System + * + * esf Link List Descriptor + */ +#ifndef _ESP_SHA_H_ +#define _ESP_SHA_H_ + +#include "c_types.h" +#include "rom/ets_sys.h" +#include "rom/sha.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * \brief SHA-1 context structure + */ +typedef struct{ + SHA_CTX context; + int context_type; +} sha_context; + +typedef sha_context SHA1_CTX; + +/** + * \brief Initialize SHA-1 context + * + * \param ctx SHA-1 context to be initialized + */ +void esp_sha1_init( SHA1_CTX *ctx ); + +/** + * \brief Clear SHA-1 context + * + * \param ctx SHA-1 context to be cleared + */ +void esp_sha1_free( SHA1_CTX *ctx ); + +/** + * \brief Clone (the state of) a SHA-1 context + * + * \param dst The destination context + * \param src The context to be cloned + */ +void esp_sha1_clone( SHA1_CTX *dst, const SHA1_CTX *src ); + +void esp_sha1_process(SHA1_CTX *ctx, const unsigned char data[64]); + +/** + * \brief SHA-1 context setup + * + * \param ctx context to be initialized + */ +void esp_sha1_starts( SHA1_CTX *ctx ); + +/** + * \brief SHA-1 process buffer + * + * \param ctx SHA-1 context + * \param input buffer holding the data + * \param ilen length of the input data + */ +void esp_sha1_update( SHA1_CTX *ctx, const unsigned char *input, size_t ilen ); + +/** + * \brief SHA-1 final digest + * + * \param ctx SHA-1 context + * \param output SHA-1 checksum result + */ +void esp_sha1_finish( SHA1_CTX *ctx, unsigned char output[20] ); + +/** + * \brief Output = SHA-1( input buffer ) + * + * \param input buffer holding the data + * \param ilen length of the input data + * \param output SHA-1 checksum result + */ +void esp_sha1_output( const unsigned char *input, size_t ilen, unsigned char output[20] ); + +/// +#define SHA256 SHA2_256 +#define SHA224 4 + +/** + * \brief SHA-256 context structure + */ + +typedef sha_context SHA256_CTX; + +/** + * \brief Initialize SHA-256 context + * + * \param ctx SHA-256 context to be initialized + */ +void esp_sha256_init( SHA256_CTX *ctx ); + +/** + * \brief Clear SHA-256 context + * + * \param ctx SHA-256 context to be cleared + */ +void esp_sha256_free( SHA256_CTX *ctx ); +void esp_sha256_process(SHA256_CTX *ctx, const unsigned char data[64]); + +/** + * \brief Clone (the state of) a SHA-256 context + * + * \param dst The destination context + * \param src The context to be cloned + */ +void esp_sha256_clone( SHA256_CTX *dst, const SHA256_CTX *src ); + +/** + * \brief SHA-256 context setup + * + * \param ctx context to be initialized + * \param is224 0 = use SHA256, 1 = use SHA224 + */ +void esp_sha256_starts( SHA256_CTX *ctx, int is224 ); + +/** + * \brief SHA-256 process buffer + * + * \param ctx SHA-256 context + * \param input buffer holding the data + * \param ilen length of the input data + */ +void esp_sha256_update( SHA256_CTX *ctx, const unsigned char *input, size_t ilen ); + +/** + * \brief SHA-256 final digest + * + * \param ctx SHA-256 context + * \param output SHA-224/256 checksum result + */ +void esp_sha256_finish( SHA256_CTX *ctx, unsigned char output[32] ); + +/** + * \brief Output = SHA-256( input buffer ) + * + * \param input buffer holding the data + * \param ilen length of the input data + * \param output SHA-224/256 checksum result + * \param is224 0 = use SHA256, 1 = use SHA224 + */ +void esp_sha256_output( const unsigned char *input, size_t ilen, unsigned char output[32], int is224 ); + +// + +/** + * \brief SHA-512 context structure + */ + +typedef sha_context SHA512_CTX; + +/** + * \brief Initialize SHA-512 context + * + * \param ctx SHA-512 context to be initialized + */ +void esp_sha512_init( SHA512_CTX *ctx ); + +/** + * \brief Clear SHA-512 context + * + * \param ctx SHA-512 context to be cleared + */ +void esp_sha512_free( SHA512_CTX *ctx ); + +/** + * \brief Clone (the state of) a SHA-512 context + * + * \param dst The destination context + * \param src The context to be cloned + */ +void esp_sha512_clone( SHA512_CTX *dst, const SHA512_CTX *src ); + +/** + * \brief SHA-512 context setup + * + * \param ctx context to be initialized + * \param is384 0 = use SHA512, 1 = use SHA384 + */ +void esp_sha512_starts( SHA512_CTX *ctx, int is384 ); + +/** + * \brief SHA-512 process buffer + * + * \param ctx SHA-512 context + * \param input buffer holding the data + * \param ilen length of the input data + */ +void esp_sha512_update( SHA512_CTX *ctx, const unsigned char *input, size_t ilen ); + +/** + * \brief SHA-512 final digest + * + * \param ctx SHA-512 context + * \param output SHA-384/512 checksum result + */ +void esp_sha512_finish( SHA512_CTX *ctx, unsigned char output[64] ); + +/** + * \brief Output = SHA-512( input buffer ) + * + * \param input buffer holding the data + * \param ilen length of the input data + * \param output SHA-384/512 checksum result + * \param is384 0 = use SHA512, 1 = use SHA384 + */ +void esp_sha512_output( const unsigned char *input, size_t ilen, unsigned char output[64], int is384 ); + +// + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/components/esp32/sha.c b/components/esp32/sha.c new file mode 100644 index 0000000000..9c28ca9c44 --- /dev/null +++ b/components/esp32/sha.c @@ -0,0 +1,285 @@ +/* + * FIPS-180-1 compliant SHA-1 implementation + * + * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +/* + * The SHA-1 standard was published by NIST in 1993. + * + * http://www.itl.nist.gov/fipspubs/fip180-1.htm + */ + +#include "sha.h" + +#include +#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 ); +} + +//// + diff --git a/components/mbedtls/Makefile b/components/mbedtls/Makefile old mode 100755 new mode 100644 diff --git a/components/mbedtls/include/mbedtls/aes.h b/components/mbedtls/include/mbedtls/aes.h index f5e1600b36..a36e825a2e 100644 --- a/components/mbedtls/include/mbedtls/aes.h +++ b/components/mbedtls/include/mbedtls/aes.h @@ -276,27 +276,7 @@ void mbedtls_aes_decrypt( mbedtls_aes_context *ctx, #endif #else /* MBEDTLS_AES_ALT */ -#include "port/aes_alt.h" - -typedef AES_CTX mbedtls_aes_context; - -#define mbedtls_aes_init aes_init -#define mbedtls_aes_free aes_free -#define mbedtls_aes_setkey_enc aes_setkey_enc -#define mbedtls_aes_setkey_dec aes_setkey_dec -#define mbedtls_aes_crypt_ecb aes_crypt_ecb -#if defined(MBEDTLS_CIPHER_MODE_CBC) -#define mbedtls_aes_crypt_cbc aes_crypt_cbc -#endif -#if defined(MBEDTLS_CIPHER_MODE_CFB) -#define mbedtls_aes_crypt_cfb128 aes_crypt_cfb128 -#define mbedtls_aes_crypt_cfb8 aes_crypt_cfb8 -#endif -#if defined(MBEDTLS_CIPHER_MODE_CTR) -#define mbedtls_aes_crypt_ctr aes_crypt_ctr -#endif -#define mbedtls_aes_encrypt aes_encrypt -#define mbedtls_aes_decrypt aes_decrypt +#include "aes_alt.h" #endif /* MBEDTLS_AES_ALT */ #ifdef __cplusplus diff --git a/components/mbedtls/include/mbedtls/bignum.h b/components/mbedtls/include/mbedtls/bignum.h index d062a75112..46f2507625 100644 --- a/components/mbedtls/include/mbedtls/bignum.h +++ b/components/mbedtls/include/mbedtls/bignum.h @@ -705,52 +705,7 @@ int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int dh_flag, int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); #else /* MBEDTLS_BIGNUM_ALT */ -#include "port/bignum_alt.h" - -typedef mpi mbedtls_mpi; - -#define mbedtls_mpi_init mpi_init -#define mbedtls_mpi_free mpi_free -#define mbedtls_mpi_grow mpi_grow -#define mbedtls_mpi_shrink mpi_shrink -#define mbedtls_mpi_copy mpi_copy -#define mbedtls_mpi_swap mpi_swap -#define mbedtls_mpi_safe_cond_assign mpi_safe_cond_assign -#define mbedtls_mpi_safe_cond_swap mpi_safe_cond_swap -#define mbedtls_mpi_lset mpi_lset -#define mbedtls_mpi_get_bit mpi_get_bit -#define mbedtls_mpi_set_bit mpi_set_bit -#define mbedtls_mpi_lsb mpi_lsb -#define mbedtls_mpi_bitlen mpi_bitlen -#define mbedtls_mpi_size mpi_size -#define mbedtls_mpi_read_string mpi_read_string -#define mbedtls_mpi_write_string mpi_write_string -#define mbedtls_mpi_read_binary mpi_read_binary -#define mbedtls_mpi_write_binary mpi_write_binary -#define mbedtls_mpi_shift_l mpi_shift_l -#define mbedtls_mpi_shift_r mpi_shift_r -#define mbedtls_mpi_cmp_abs mpi_cmp_abs -#define mbedtls_mpi_cmp_mpi mpi_cmp_mpi -#define mbedtls_mpi_cmp_int mpi_cmp_int -#define mbedtls_mpi_add_abs mpi_add_abs -#define mbedtls_mpi_sub_abs mpi_sub_abs -#define mbedtls_mpi_add_mpi mpi_add_mpi -#define mbedtls_mpi_sub_mpi mpi_sub_mpi -#define mbedtls_mpi_add_int mpi_add_int -#define mbedtls_mpi_sub_int mpi_sub_int -#define mbedtls_mpi_mul_mpi mpi_mul_mpi -#define mbedtls_mpi_mul_int mpi_mul_int -#define mbedtls_mpi_div_mpi mpi_div_mpi -#define mbedtls_mpi_div_int mpi_div_int -#define mbedtls_mpi_mod_mpi mpi_mod_mpi -#define mbedtls_mpi_mod_int mpi_mod_int -#define mbedtls_mpi_exp_mod mpi_exp_mod -#define mbedtls_mpi_fill_random mpi_fill_random -#define mbedtls_mpi_gcd mpi_gcd -#define mbedtls_mpi_inv_mod mpi_inv_mod -#define mbedtls_mpi_is_prime mpi_is_prime -#define mbedtls_mpi_gen_prime mpi_gen_prime - +#include "bignum_alt.h" #endif /* MBEDTLS_BIGNUM_ALT */ /** diff --git a/components/mbedtls/include/mbedtls/esp_config.h b/components/mbedtls/include/mbedtls/esp_config.h index 614e8018bb..1ea6076813 100644 --- a/components/mbedtls/include/mbedtls/esp_config.h +++ b/components/mbedtls/include/mbedtls/esp_config.h @@ -2466,8 +2466,7 @@ //#define MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES 50 /**< Maximum entries in cache */ /* SSL options */ -extern unsigned int max_content_len; -#define MBEDTLS_SSL_MAX_CONTENT_LEN max_content_len /**< Maxium fragment length in bytes, determines the size of each of the two internal I/O buffers */ +#define MBEDTLS_SSL_MAX_CONTENT_LEN 3072 /**< Maxium fragment length in bytes, determines the size of each of the two internal I/O buffers */ //#define MBEDTLS_SSL_DEFAULT_TICKET_LIFETIME 86400 /**< Lifetime of session tickets (if enabled) */ //#define MBEDTLS_PSK_MAX_LEN 32 /**< Max size of TLS pre-shared keys, in bytes (default 256 bits) */ //#define MBEDTLS_SSL_COOKIE_TIMEOUT 60 /**< Default expiration delay of DTLS cookies, in seconds if HAVE_TIME, or in number of cookies issued */ diff --git a/components/mbedtls/include/mbedtls/sha1.h b/components/mbedtls/include/mbedtls/sha1.h index 2d85a59f78..7a67c6c1fb 100644 --- a/components/mbedtls/include/mbedtls/sha1.h +++ b/components/mbedtls/include/mbedtls/sha1.h @@ -106,17 +106,7 @@ void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[6 #endif #else /* MBEDTLS_SHA1_ALT */ -#include "port/sha1_alt.h" - -typedef SHA1_CTX mbedtls_sha1_context; - -#define mbedtls_sha1_init sha1_init -#define mbedtls_sha1_starts sha1_starts -#define mbedtls_sha1_clone sha1_clone -#define mbedtls_sha1_update sha1_update -#define mbedtls_sha1_finish sha1_finish -#define mbedtls_sha1_free sha1_free -#define mbedtls_sha1_process sha1_process +#include "sha1_alt.h" #endif /* MBEDTLS_SHA1_ALT */ #ifdef __cplusplus diff --git a/components/mbedtls/include/mbedtls/sha256.h b/components/mbedtls/include/mbedtls/sha256.h index 4ab444e8d1..f8041adf08 100644 --- a/components/mbedtls/include/mbedtls/sha256.h +++ b/components/mbedtls/include/mbedtls/sha256.h @@ -109,17 +109,7 @@ void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char da #endif #else /* MBEDTLS_SHA256_ALT */ -#include "port/sha256_alt.h" - -typedef SHA256_CTX mbedtls_sha256_context; - -#define mbedtls_sha256_init sha256_init -#define mbedtls_sha256_clone sha256_clone -#define mbedtls_sha256_starts sha256_starts -#define mbedtls_sha256_update sha256_update -#define mbedtls_sha256_finish sha256_finish -#define mbedtls_sha256_free sha256_free -#define mbedtls_sha256_process sha256_process +#include "sha256_alt.h" #endif /* MBEDTLS_SHA256_ALT */ #ifdef __cplusplus diff --git a/components/mbedtls/include/mbedtls/sha512.h b/components/mbedtls/include/mbedtls/sha512.h index 0ebc092f43..627694f425 100644 --- a/components/mbedtls/include/mbedtls/sha512.h +++ b/components/mbedtls/include/mbedtls/sha512.h @@ -106,17 +106,7 @@ void mbedtls_sha512_finish( mbedtls_sha512_context *ctx, unsigned char output[64 #endif #else /* MBEDTLS_SHA512_ALT */ -#include "port/sha512_alt.h" - -typedef SHA512_CTX mbedtls_sha512_context; - -#define mbedtls_sha512_init sha512_init -#define mbedtls_sha512_clone sha512_clone -#define mbedtls_sha512_starts sha512_starts -#define mbedtls_sha512_update sha512_update -#define mbedtls_sha512_finish sha512_finish -#define mbedtls_sha512_free sha512_free - +#include "sha512_alt.h" #endif /* MBEDTLS_SHA512_ALT */ #ifdef __cplusplus diff --git a/components/mbedtls/include/port/multi_thread.h b/components/mbedtls/include/port/multi_thread.h deleted file mode 100644 index 6732eeddef..0000000000 --- a/components/mbedtls/include/port/multi_thread.h +++ /dev/null @@ -1,63 +0,0 @@ -#ifndef _MULTI_THREAD_H_ -#define _MULTI_THREAD_H_ - -#ifdef __cplusplus -extern "C" { -#endif - -enum { - AES_MUTEX = 0, - BIGNUM_MUTEX, - SHA_MUTEX, - - MUTEX_MAX_NUM, -}; - -int multi_thread_init(void); - -void multi_thread_lock(unsigned int num); -void multi_thread_unlock(unsigned int num); - -void multi_thread_take(unsigned int num); -void multi_thread_give(unsigned int num); -bool multi_thread_is_used(num); - -#define MUTEX_LOCK(num) multi_thread_lock(num) -#define MUTEX_UNLOCK(num) multi_thread_unlock(num) - -#define SIG_TAKE(num) multi_thread_take(num) -#define SIG_GIVE(num) multi_thread_give(num) -#define SIG_IS_USED(num) multi_thread_is_used(num) - -#define AES_LOCK() MUTEX_LOCK(AES_MUTEX) -#define AES_UNLOCK() MUTEX_UNLOCK(AES_MUTEX) -#define BIGNUM_LOCK() MUTEX_LOCK(BIGNUM_MUTEX) -#define BIGNUM_UNLOCK() MUTEX_UNLOCK(BIGNUM_MUTEX) -#define SHA1_LOCK() MUTEX_LOCK(SHA_MUTEX) -#define SHA1_UNLOCK() MUTEX_UNLOCK(SHA_MUTEX) -#define SHA256_LOCK() MUTEX_LOCK(SHA_MUTEX) -#define SHA256_UNLOCK() MUTEX_UNLOCK(SHA_MUTEX) -#define SHA512_LOCK() MUTEX_LOCK(SHA_MUTEX) -#define SHA512_UNLOCK() MUTEX_UNLOCK(SHA_MUTEX) - -#define AES_TAKE() SIG_TAKE(AES_MUTEX) -#define AES_GIVE() SIG_GIVE(AES_MUTEX) -#define AES_IS_USED() SIG_IS_USED(AES_MUTEX) -#define BIGNUM_TAKE() SIG_TAKE(BIGNUM_MUTEX) -#define BIGNUM_GIVE() SIG_GIVE(BIGNUM_MUTEX) -#define BIGNUM_IS_USED() SIG_IS_USED(BIGNUM_MUTEX) -#define SHA1_TAKE() SIG_TAKE(SHA_MUTEX) -#define SHA1_GIVE() SIG_GIVE(SHA_MUTEX) -#define SHA1_IS_USED() SIG_IS_USED(SHA_MUTEX) -#define SHA256_TAKE() SIG_TAKE(SHA_MUTEX) -#define SHA256_GIVE() SIG_GIVE(SHA_MUTEX) -#define SHA256_IS_USED() SIG_IS_USED(SHA_MUTEX) -#define SHA512_TAKE() SIG_TAKE(SHA_MUTEX) -#define SHA512_GIVE() SIG_GIVE(SHA_MUTEX) -#define SHA512_IS_USED() SIG_IS_USED(SHA_MUTEX) - -#ifdef __cplusplus -} -#endif - -#endif /* multi_thread.h */ diff --git a/components/mbedtls/include/port/sha1_alt.h b/components/mbedtls/include/port/sha1_alt.h deleted file mode 100644 index 9187986277..0000000000 --- a/components/mbedtls/include/port/sha1_alt.h +++ /dev/null @@ -1,93 +0,0 @@ -/* - * copyright (c) 2010 - 2012 Espressif System - * - * esf Link List Descriptor - */ -#ifndef _SHA1_H_ -#define _SHA1_H_ - -#include "c_types.h" -#include "rom/ets_sys.h" -#include "rom/sha.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#define ESP_SHA1_C - -#define SHA1 0 - -/** - * \brief SHA-1 context structure - */ -typedef struct{ - SHA_CTX context; - int context_type; -} sha1_context; - -typedef sha1_context SHA1_CTX; - -/** - * \brief Initialize SHA-1 context - * - * \param ctx SHA-1 context to be initialized - */ -void sha1_init( SHA1_CTX *ctx ); - -/** - * \brief Clear SHA-1 context - * - * \param ctx SHA-1 context to be cleared - */ -void sha1_free( SHA1_CTX *ctx ); - -/** - * \brief Clone (the state of) a SHA-1 context - * - * \param dst The destination context - * \param src The context to be cloned - */ -void sha1_clone( SHA1_CTX *dst, const SHA1_CTX *src ); - -void sha1_process(SHA1_CTX *ctx, const unsigned char data[64]); - -/** - * \brief SHA-1 context setup - * - * \param ctx context to be initialized - */ -void sha1_starts( SHA1_CTX *ctx ); - -/** - * \brief SHA-1 process buffer - * - * \param ctx SHA-1 context - * \param input buffer holding the data - * \param ilen length of the input data - */ -void sha1_update( SHA1_CTX *ctx, const unsigned char *input, size_t ilen ); - -/** - * \brief SHA-1 final digest - * - * \param ctx SHA-1 context - * \param output SHA-1 checksum result - */ -void sha1_finish( SHA1_CTX *ctx, unsigned char output[20] ); - -/** - * \brief Output = SHA-1( input buffer ) - * - * \param input buffer holding the data - * \param ilen length of the input data - * \param output SHA-1 checksum result - */ -void sha1_output( const unsigned char *input, size_t ilen, unsigned char output[20] ); - -#ifdef __cplusplus -} -#endif - -#endif - diff --git a/components/mbedtls/include/port/sha256_alt.h b/components/mbedtls/include/port/sha256_alt.h deleted file mode 100644 index bc661d3932..0000000000 --- a/components/mbedtls/include/port/sha256_alt.h +++ /dev/null @@ -1,95 +0,0 @@ -/* - * copyright (c) 2010 - 2012 Espressif System - * - * esf Link List Descriptor - */ - -#ifndef _SHA256_H_ -#define _SHA256_H_ - -#include "c_types.h" -#include "rom/ets_sys.h" -#include "rom/sha.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#define ESP_SHA256_C - -#define SHA256 SHA2_256 -#define SHA224 4 - -/** - * \brief SHA-256 context structure - */ -typedef struct{ - SHA_CTX context; - int context_type; -}sha256_context; - -typedef sha256_context SHA256_CTX; - -/** - * \brief Initialize SHA-256 context - * - * \param ctx SHA-256 context to be initialized - */ -void sha256_init( SHA256_CTX *ctx ); - -/** - * \brief Clear SHA-256 context - * - * \param ctx SHA-256 context to be cleared - */ -void sha256_free( SHA256_CTX *ctx ); -void sha256_process(SHA256_CTX *ctx, const unsigned char data[64]); - -/** - * \brief Clone (the state of) a SHA-256 context - * - * \param dst The destination context - * \param src The context to be cloned - */ -void sha256_clone( SHA256_CTX *dst, const SHA256_CTX *src ); - -/** - * \brief SHA-256 context setup - * - * \param ctx context to be initialized - * \param is224 0 = use SHA256, 1 = use SHA224 - */ -void sha256_starts( SHA256_CTX *ctx, int is224 ); - -/** - * \brief SHA-256 process buffer - * - * \param ctx SHA-256 context - * \param input buffer holding the data - * \param ilen length of the input data - */ -void sha256_update( SHA256_CTX *ctx, const unsigned char *input, size_t ilen ); - -/** - * \brief SHA-256 final digest - * - * \param ctx SHA-256 context - * \param output SHA-224/256 checksum result - */ -void sha256_finish( SHA256_CTX *ctx, unsigned char output[32] ); - -/** - * \brief Output = SHA-256( input buffer ) - * - * \param input buffer holding the data - * \param ilen length of the input data - * \param output SHA-224/256 checksum result - * \param is224 0 = use SHA256, 1 = use SHA224 - */ -void sha256_output( const unsigned char *input, size_t ilen, unsigned char output[32], int is224 ); - -#ifdef __cplusplus -} -#endif - -#endif /* sha256.h */ diff --git a/components/mbedtls/include/port/sha512_alt.h b/components/mbedtls/include/port/sha512_alt.h deleted file mode 100644 index a3e1c50c64..0000000000 --- a/components/mbedtls/include/port/sha512_alt.h +++ /dev/null @@ -1,92 +0,0 @@ -/* - * copyright (c) 2010 - 2012 Espressif System - * - * esf Link List Descriptor - */ - -#ifndef _SHA512_H_ -#define _SHA512_H_ - -#include "c_types.h" -#include "rom/ets_sys.h" -#include "rom/sha.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#define ESP_SHA512_C - -/** - * \brief SHA-512 context structure - */ -typedef struct{ - SHA_CTX context; - int context_type; -}sha512_context; - -typedef sha512_context SHA512_CTX; - -/** - * \brief Initialize SHA-512 context - * - * \param ctx SHA-512 context to be initialized - */ -void sha512_init( SHA512_CTX *ctx ); - -/** - * \brief Clear SHA-512 context - * - * \param ctx SHA-512 context to be cleared - */ -void sha512_free( SHA512_CTX *ctx ); - -/** - * \brief Clone (the state of) a SHA-512 context - * - * \param dst The destination context - * \param src The context to be cloned - */ -void sha512_clone( SHA512_CTX *dst, const SHA512_CTX *src ); - -/** - * \brief SHA-512 context setup - * - * \param ctx context to be initialized - * \param is384 0 = use SHA512, 1 = use SHA384 - */ -void sha512_starts( SHA512_CTX *ctx, int is384 ); - -/** - * \brief SHA-512 process buffer - * - * \param ctx SHA-512 context - * \param input buffer holding the data - * \param ilen length of the input data - */ -void sha512_update( SHA512_CTX *ctx, const unsigned char *input, size_t ilen ); - -/** - * \brief SHA-512 final digest - * - * \param ctx SHA-512 context - * \param output SHA-384/512 checksum result - */ -void sha512_finish( SHA512_CTX *ctx, unsigned char output[64] ); - -/** - * \brief Output = SHA-512( input buffer ) - * - * \param input buffer holding the data - * \param ilen length of the input data - * \param output SHA-384/512 checksum result - * \param is384 0 = use SHA512, 1 = use SHA384 - */ -void sha512_output( const unsigned char *input, size_t ilen, unsigned char output[64], int is384 ); - - -#ifdef __cplusplus -} -#endif - -#endif /* sha512.h */ diff --git a/components/mbedtls/port/include/aes_alt.h b/components/mbedtls/port/include/aes_alt.h new file mode 100644 index 0000000000..90e659483c --- /dev/null +++ b/components/mbedtls/port/include/aes_alt.h @@ -0,0 +1,59 @@ +/** + * \file aes_alt.h + * + * \brief AES block cipher + * + * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + */ + +#ifndef AES_ALT_H +#define AES_ALT_H + +#ifdef __cplusplus +extern "C" { +#endif + +#if defined(MBEDTLS_AES_ALT) +#include "aes.h" + +typedef AES_CTX mbedtls_aes_context; + +#define mbedtls_aes_init esp_aes_init +#define mbedtls_aes_free esp_aes_free +#define mbedtls_aes_setkey_enc esp_aes_setkey_enc +#define mbedtls_aes_setkey_dec esp_aes_setkey_dec +#define mbedtls_aes_crypt_ecb esp_aes_crypt_ecb +#if defined(MBEDTLS_CIPHER_MODE_CBC) +#define mbedtls_aes_crypt_cbc esp_aes_crypt_cbc +#endif +#if defined(MBEDTLS_CIPHER_MODE_CFB) +#define mbedtls_aes_crypt_cfb128 esp_aes_crypt_cfb128 +#define mbedtls_aes_crypt_cfb8 esp_aes_crypt_cfb8 +#endif +#if defined(MBEDTLS_CIPHER_MODE_CTR) +#define mbedtls_aes_crypt_ctr esp_aes_crypt_ctr +#endif +#define mbedtls_aes_encrypt esp_aes_encrypt +#define mbedtls_aes_decrypt esp_aes_decrypt +#endif /* MBEDTLS_AES_ALT */ + +#ifdef __cplusplus +} +#endif + +#endif /* aes.h */ diff --git a/components/mbedtls/port/include/bignum_alt.h b/components/mbedtls/port/include/bignum_alt.h new file mode 100644 index 0000000000..a4ac0db3ef --- /dev/null +++ b/components/mbedtls/port/include/bignum_alt.h @@ -0,0 +1,77 @@ +/** + * \file bignum_alt.h + * + * \brief Multi-precision integer library + * + * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef BIGNUM_ALT_H +#define BIGNUM_ALT_H + +#include "bignum.h" + +#if defined(MBEDTLS_BIGNUM_ALT) + +typedef MPI_CTX mbedtls_mpi; + +#define mbedtls_mpi_init esp_mpi_init +#define mbedtls_mpi_free esp_mpi_free +#define mbedtls_mpi_grow esp_mpi_grow +#define mbedtls_mpi_shrink esp_mpi_shrink +#define mbedtls_mpi_copy esp_mpi_copy +#define mbedtls_mpi_swap esp_mpi_swap +#define mbedtls_mpi_safe_cond_assign esp_mpi_safe_cond_assign +#define mbedtls_mpi_safe_cond_swap esp_mpi_safe_cond_swap +#define mbedtls_mpi_lset esp_mpi_lset +#define mbedtls_mpi_get_bit esp_mpi_get_bit +#define mbedtls_mpi_set_bit esp_mpi_set_bit +#define mbedtls_mpi_lsb esp_mpi_lsb +#define mbedtls_mpi_bitlen esp_mpi_bitlen +#define mbedtls_mpi_size esp_mpi_size +#define mbedtls_mpi_read_string esp_mpi_read_string +#define mbedtls_mpi_write_string esp_mpi_write_string +#define mbedtls_mpi_read_binary esp_mpi_read_binary +#define mbedtls_mpi_write_binary esp_mpi_write_binary +#define mbedtls_mpi_shift_l esp_mpi_shift_l +#define mbedtls_mpi_shift_r esp_mpi_shift_r +#define mbedtls_mpi_cmp_abs esp_mpi_cmp_abs +#define mbedtls_mpi_cmp_mpi esp_mpi_cmp_mpi +#define mbedtls_mpi_cmp_int esp_mpi_cmp_int +#define mbedtls_mpi_add_abs esp_mpi_add_abs +#define mbedtls_mpi_sub_abs esp_mpi_sub_abs +#define mbedtls_mpi_add_mpi esp_mpi_add_mpi +#define mbedtls_mpi_sub_mpi esp_mpi_sub_mpi +#define mbedtls_mpi_add_int esp_mpi_add_int +#define mbedtls_mpi_sub_int esp_mpi_sub_int +#define mbedtls_mpi_mul_mpi esp_mpi_mul_mpi +#define mbedtls_mpi_mul_int esp_mpi_mul_int +#define mbedtls_mpi_div_mpi esp_mpi_div_mpi +#define mbedtls_mpi_div_int esp_mpi_div_int +#define mbedtls_mpi_mod_mpi esp_mpi_mod_mpi +#define mbedtls_mpi_mod_int esp_mpi_mod_int +#define mbedtls_mpi_exp_mod esp_mpi_exp_mod +#define mbedtls_mpi_fill_random esp_mpi_fill_random +#define mbedtls_mpi_gcd esp_mpi_gcd +#define mbedtls_mpi_inv_mod esp_mpi_inv_mod +#define mbedtls_mpi_is_prime esp_mpi_is_prime +#define mbedtls_mpi_gen_prime esp_mpi_gen_prime + +#endif + +#endif + diff --git a/components/mbedtls/port/include/sha1_alt.h b/components/mbedtls/port/include/sha1_alt.h new file mode 100644 index 0000000000..2cb0e926de --- /dev/null +++ b/components/mbedtls/port/include/sha1_alt.h @@ -0,0 +1,34 @@ +/* + * copyright (c) 2010 - 2012 Espressif System + * + * esf Link List Descriptor + */ +#ifndef _SHA1_ALT_H_ +#define _SHA1_ALT_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#if defined(MBEDTLS_SHA1_ALT) + +#include "sha.h" + +typedef SHA1_CTX mbedtls_sha1_context; + +#define mbedtls_sha1_init esp_sha1_init +#define mbedtls_sha1_starts esp_sha1_starts +#define mbedtls_sha1_clone esp_sha1_clone +#define mbedtls_sha1_update esp_sha1_update +#define mbedtls_sha1_finish esp_sha1_finish +#define mbedtls_sha1_free esp_sha1_free +#define mbedtls_sha1_process esp_sha1_process + +#endif + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/components/mbedtls/port/include/sha256_alt.h b/components/mbedtls/port/include/sha256_alt.h new file mode 100644 index 0000000000..00beb2d28c --- /dev/null +++ b/components/mbedtls/port/include/sha256_alt.h @@ -0,0 +1,34 @@ +/* + * copyright (c) 2010 - 2012 Espressif System + * + * esf Link List Descriptor + */ + +#ifndef _SHA256_ALT_H_ +#define _SHA256_ALT_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#if defined(MBEDTLS_SHA256_ALT) + +#include "sha.h" + +typedef SHA256_CTX mbedtls_sha256_context; + +#define mbedtls_sha256_init esp_sha256_init +#define mbedtls_sha256_clone esp_sha256_clone +#define mbedtls_sha256_starts esp_sha256_starts +#define mbedtls_sha256_update esp_sha256_update +#define mbedtls_sha256_finish esp_sha256_finish +#define mbedtls_sha256_free esp_sha256_free +#define mbedtls_sha256_process esp_sha256_process + +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* sha256.h */ diff --git a/components/mbedtls/port/include/sha512_alt.h b/components/mbedtls/port/include/sha512_alt.h new file mode 100644 index 0000000000..b4e17259e4 --- /dev/null +++ b/components/mbedtls/port/include/sha512_alt.h @@ -0,0 +1,32 @@ +/* + * copyright (c) 2010 - 2012 Espressif System + * + * esf Link List Descriptor + */ + +#ifndef _SHA512_ALT_H_ +#define _SHA512_ALT_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#if defined(MBEDTLS_SHA512_ALT) +#include "sha.h" + +typedef SHA512_CTX mbedtls_sha512_context; + +#define mbedtls_sha512_init esp_sha512_init +#define mbedtls_sha512_clone esp_sha512_clone +#define mbedtls_sha512_starts esp_sha512_starts +#define mbedtls_sha512_update esp_sha512_update +#define mbedtls_sha512_finish esp_sha512_finish +#define mbedtls_sha512_free esp_sha512_free + +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* sha512.h */ diff --git a/components/mbedtls/port/multi_thread.c b/components/mbedtls/port/multi_thread.c deleted file mode 100644 index 31cf1ea6da..0000000000 --- a/components/mbedtls/port/multi_thread.c +++ /dev/null @@ -1,53 +0,0 @@ -#include "multi_thread.h" -#include "freertos/FreeRTOS.h" -#include "freertos/semphr.h" - -static xSemaphoreHandle multi_thread_mutex[MUTEX_MAX_NUM]; -static int multi_thread_sig[MUTEX_MAX_NUM]; - -int multi_thread_init(void) -{ - int i; - - for (i = 0; i < MUTEX_MAX_NUM; i++) { - multi_thread_mutex[i] = xSemaphoreCreateMutex(); - if (!multi_thread_mutex[i]) { - goto failed1; - } - multi_thread_sig[i] = 0; - } - - return 0; - -failed1: - for (i--; i >= 0; i--) - vQueueDelete(multi_thread_mutex[i]); - - return -1; -} - -void multi_thread_lock(unsigned int num) -{ - xSemaphoreTake(multi_thread_mutex[num], portMAX_DELAY); -} - -void multi_thread_unlock(unsigned int num) -{ - xSemaphoreGive(multi_thread_mutex[num]); -} - -void multi_thread_take(unsigned int num) -{ - multi_thread_sig[num]++; -} - -void multi_thread_give(unsigned int num) -{ - multi_thread_sig[num]--; -} - -bool multi_thread_is_used(num) -{ - return (multi_thread_sig[num] != 0) ? true : false; -} - diff --git a/components/mbedtls/port/sha1_alt.c b/components/mbedtls/port/sha1_alt.c deleted file mode 100644 index b73d03ffa5..0000000000 --- a/components/mbedtls/port/sha1_alt.c +++ /dev/null @@ -1,117 +0,0 @@ -/* - * FIPS-180-1 compliant SHA-1 implementation - * - * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -/* - * The SHA-1 standard was published by NIST in 1993. - * - * http://www.itl.nist.gov/fipspubs/fip180-1.htm - */ - -#include "port/sha1_alt.h" - -#if defined(ESP_SHA1_C) -#include -#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 */ - - diff --git a/components/mbedtls/port/sha256_alt.c b/components/mbedtls/port/sha256_alt.c deleted file mode 100644 index fc8769d1ba..0000000000 --- a/components/mbedtls/port/sha256_alt.c +++ /dev/null @@ -1,122 +0,0 @@ -/* - * FIPS-180-2 compliant SHA-256 implementation - * - * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -/* - * The SHA-256 Secure Hash Standard was published by NIST in 2002. - * - * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf - */ - -#include "port/sha256_alt.h" - -#if defined(ESP_SHA256_C) -#include -#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 */ diff --git a/components/mbedtls/port/sha512_alt.c b/components/mbedtls/port/sha512_alt.c deleted file mode 100644 index 02d315681e..0000000000 --- a/components/mbedtls/port/sha512_alt.c +++ /dev/null @@ -1,117 +0,0 @@ -/* - * FIPS-180-2 compliant SHA-384/512 implementation - * - * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -/* - * The SHA-512 Secure Hash Standard was published by NIST in 2002. - * - * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf - */ -#include "port/sha512_alt.h" - -#if defined(ESP_SHA512_C) -#include -#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 */