mbedtls: just format related files

method from !46
This commit is contained in:
Wu Jian Gang 2016-09-02 11:31:38 +08:00
parent f4ff32977d
commit fc2bfc1f49
13 changed files with 574 additions and 548 deletions

View File

@ -39,100 +39,112 @@ void esp_aes_init( AES_CTX *ctx )
AES_LOCK(); AES_LOCK();
AES_TAKE(); AES_TAKE();
ets_aes_enable(); ets_aes_enable();
AES_UNLOCK(); AES_UNLOCK();
} }
void esp_aes_free( AES_CTX *ctx ) void esp_aes_free( AES_CTX *ctx )
{ {
if( ctx == NULL ) if ( ctx == NULL ) {
return; return;
}
bzero( ctx, sizeof( AES_CTX ) ); bzero( ctx, sizeof( AES_CTX ) );
AES_LOCK(); AES_LOCK();
AES_GIVE(); AES_GIVE();
if (false == AES_IS_USED())
ets_aes_disable(); if (false == AES_IS_USED()) {
AES_UNLOCK(); ets_aes_disable();
}
AES_UNLOCK();
} }
/* /*
* AES key schedule (encryption) * AES key schedule (encryption)
*/ */
int esp_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 ) unsigned int keybits )
{ {
enum AES_BITS keybit; enum AES_BITS keybit;
uint16_t keybyte = keybits / 8; uint16_t keybyte = keybits / 8;
switch (keybits){
case 128: switch (keybits) {
keybit = AES128; case 128:
break; keybit = AES128;
case 192: break;
keybit = AES192; case 192:
break; keybit = AES192;
case 256: break;
keybit = AES256; case 256:
break; keybit = AES256;
default : return( ERR_AES_INVALID_KEY_LENGTH ); break;
} default:
if (ctx->enc.keyflag == false){ return ( ERR_AES_INVALID_KEY_LENGTH );
ctx->enc.keyflag = true; }
ctx->enc.keybits = keybits;
memset(ctx->enc.key, 0, sizeof(ctx->enc.key)); if (ctx->enc.keyflag == false) {
memcpy(ctx->enc.key, key, keybyte); ctx->enc.keyflag = true;
} else { ctx->enc.keybits = keybits;
ets_aes_setkey_enc(key, keybit); memset(ctx->enc.key, 0, sizeof(ctx->enc.key));
} memcpy(ctx->enc.key, key, keybyte);
return 0; } else {
ets_aes_setkey_enc(key, keybit);
}
return 0;
} }
/* /*
* AES key schedule (decryption) * AES key schedule (decryption)
*/ */
int esp_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 ) unsigned int keybits )
{ {
enum AES_BITS keybit; enum AES_BITS keybit;
uint16_t keybyte = keybits / 8; uint16_t keybyte = keybits / 8;
switch (keybits){
case 128:
keybit = AES128;
break;
case 192:
keybit = AES192;
break;
case 256:
keybit = AES256;
break;
default : return( ERR_AES_INVALID_KEY_LENGTH );
}
if (ctx->dec.keyflag == false){
ctx->dec.keyflag = true;
ctx->dec.keybits = keybits;
memset(ctx->dec.key, 0, sizeof(ctx->dec.key));
memcpy(ctx->dec.key, key, keybyte);
} else {
ets_aes_setkey_dec(key, keybit);
}
return 0;
switch (keybits) {
case 128:
keybit = AES128;
break;
case 192:
keybit = AES192;
break;
case 256:
keybit = AES256;
break;
default:
return ( ERR_AES_INVALID_KEY_LENGTH );
}
if (ctx->dec.keyflag == false) {
ctx->dec.keyflag = true;
ctx->dec.keybits = keybits;
memset(ctx->dec.key, 0, sizeof(ctx->dec.key));
memcpy(ctx->dec.key, key, keybyte);
} else {
ets_aes_setkey_dec(key, keybit);
}
return 0;
} }
static void esp_aes_process_enable(AES_CTX *ctx, int mode) static void esp_aes_process_enable(AES_CTX *ctx, int mode)
{ {
if( mode == AES_ENCRYPT ){ if ( mode == AES_ENCRYPT ) {
esp_aes_setkey_enc(ctx, ctx->enc.key, ctx->enc.keybits); esp_aes_setkey_enc(ctx, ctx->enc.key, ctx->enc.keybits);
}else{ } else {
esp_aes_setkey_dec(ctx, ctx->dec.key, ctx->dec.keybits); esp_aes_setkey_dec(ctx, ctx->dec.key, ctx->dec.keybits);
} }
return;
return;
} }
static void esp_aes_process_disable(AES_CTX *ctx, int mode) static void esp_aes_process_disable(AES_CTX *ctx, int mode)
{ {
} }
/* /*
@ -140,11 +152,12 @@ static void esp_aes_process_disable(AES_CTX *ctx, int mode)
*/ */
void esp_aes_encrypt( AES_CTX *ctx, void esp_aes_encrypt( AES_CTX *ctx,
const unsigned char input[16], const unsigned char input[16],
unsigned char output[16] ) unsigned char output[16] )
{ {
ets_aes_crypt(input, output); ets_aes_crypt(input, output);
return ;
return ;
} }
@ -153,11 +166,12 @@ void esp_aes_encrypt( AES_CTX *ctx,
*/ */
void esp_aes_decrypt( AES_CTX *ctx, void esp_aes_decrypt( AES_CTX *ctx,
const unsigned char input[16], const unsigned char input[16],
unsigned char output[16] ) unsigned char output[16] )
{ {
ets_aes_crypt(input, output); ets_aes_crypt(input, output);
return ;
return ;
} }
@ -165,24 +179,25 @@ void esp_aes_decrypt( AES_CTX *ctx,
* AES-ECB block encryption/decryption * AES-ECB block encryption/decryption
*/ */
int esp_aes_crypt_ecb( AES_CTX *ctx, int esp_aes_crypt_ecb( AES_CTX *ctx,
int mode, int mode,
const unsigned char input[16], const unsigned char input[16],
unsigned char output[16] ) unsigned char output[16] )
{ {
AES_LOCK(); AES_LOCK();
esp_aes_process_enable(ctx, mode);
if( mode == AES_ENCRYPT ) esp_aes_process_enable(ctx, mode);
if ( mode == AES_ENCRYPT ) {
esp_aes_encrypt( ctx, input, output ); esp_aes_encrypt( ctx, input, output );
else } else {
esp_aes_decrypt( ctx, input, output ); esp_aes_decrypt( ctx, input, output );
}
esp_aes_process_disable(ctx, mode); esp_aes_process_disable(ctx, mode);
AES_UNLOCK(); AES_UNLOCK();
return 0; return 0;
} }
@ -190,27 +205,27 @@ int esp_aes_crypt_ecb( AES_CTX *ctx,
* AES-CBC buffer encryption/decryption * AES-CBC buffer encryption/decryption
*/ */
int esp_aes_crypt_cbc( AES_CTX *ctx, int esp_aes_crypt_cbc( AES_CTX *ctx,
int mode, int mode,
size_t length, size_t length,
unsigned char iv[16], unsigned char iv[16],
const unsigned char *input, const unsigned char *input,
unsigned char *output ) unsigned char *output )
{ {
int i; int i;
unsigned char temp[16]; unsigned char temp[16];
if( length % 16 ) if ( length % 16 ) {
return( ERR_AES_INVALID_INPUT_LENGTH ); return ( ERR_AES_INVALID_INPUT_LENGTH );
}
if( mode == AES_DECRYPT ) if ( mode == AES_DECRYPT ) {
{ while ( length > 0 ) {
while( length > 0 )
{
memcpy( temp, input, 16 ); memcpy( temp, input, 16 );
esp_aes_crypt_ecb( ctx, mode, input, output ); esp_aes_crypt_ecb( ctx, mode, input, output );
for( i = 0; i < 16; i++ ) for ( i = 0; i < 16; i++ ) {
output[i] = (unsigned char)( output[i] ^ iv[i] ); output[i] = (unsigned char)( output[i] ^ iv[i] );
}
memcpy( iv, temp, 16 ); memcpy( iv, temp, 16 );
@ -218,13 +233,11 @@ int esp_aes_crypt_cbc( AES_CTX *ctx,
output += 16; output += 16;
length -= 16; length -= 16;
} }
} } else {
else while ( length > 0 ) {
{ for ( i = 0; i < 16; i++ ) {
while( length > 0 )
{
for( i = 0; i < 16; i++ )
output[i] = (unsigned char)( input[i] ^ iv[i] ); output[i] = (unsigned char)( input[i] ^ iv[i] );
}
esp_aes_crypt_ecb( ctx, mode, output, output ); esp_aes_crypt_ecb( ctx, mode, output, output );
memcpy( iv, output, 16 ); memcpy( iv, output, 16 );
@ -235,85 +248,83 @@ int esp_aes_crypt_cbc( AES_CTX *ctx,
} }
} }
return 0; return 0;
} }
/* /*
* AES-CFB128 buffer encryption/decryption * AES-CFB128 buffer encryption/decryption
*/ */
int esp_aes_crypt_cfb128( AES_CTX *ctx, int esp_aes_crypt_cfb128( AES_CTX *ctx,
int mode, int mode,
size_t length, size_t length,
size_t *iv_off, size_t *iv_off,
unsigned char iv[16], unsigned char iv[16],
const unsigned char *input, const unsigned char *input,
unsigned char *output ) unsigned char *output )
{ {
int c; int c;
size_t n = *iv_off; size_t n = *iv_off;
if( mode == AES_DECRYPT ) if ( mode == AES_DECRYPT ) {
{ while ( length-- ) {
while( length-- ) if ( n == 0 ) {
{ esp_aes_crypt_ecb( ctx, AES_ENCRYPT, iv, iv );
if( n == 0 ) }
esp_aes_crypt_ecb( ctx, AES_ENCRYPT, iv, iv );
c = *input++; c = *input++;
*output++ = (unsigned char)( c ^ iv[n] ); *output++ = (unsigned char)( c ^ iv[n] );
iv[n] = (unsigned char) c; iv[n] = (unsigned char) c;
n = ( n + 1 ) & 0x0F;
}
}
else
{
while( length-- )
{
if( n == 0 )
esp_aes_crypt_ecb( ctx, AES_ENCRYPT, iv, iv );
iv[n] = *output++ = (unsigned char)( iv[n] ^ *input++ );
n = ( n + 1 ) & 0x0F;
}
}
*iv_off = n;
return 0; n = ( n + 1 ) & 0x0F;
}
} else {
while ( length-- ) {
if ( n == 0 ) {
esp_aes_crypt_ecb( ctx, AES_ENCRYPT, iv, iv );
}
iv[n] = *output++ = (unsigned char)( iv[n] ^ *input++ );
n = ( n + 1 ) & 0x0F;
}
}
*iv_off = n;
return 0;
} }
/* /*
* AES-CFB8 buffer encryption/decryption * AES-CFB8 buffer encryption/decryption
*/ */
int esp_aes_crypt_cfb8( AES_CTX *ctx, int esp_aes_crypt_cfb8( AES_CTX *ctx,
int mode, int mode,
size_t length, size_t length,
unsigned char iv[16], unsigned char iv[16],
const unsigned char *input, const unsigned char *input,
unsigned char *output ) unsigned char *output )
{ {
unsigned char c; unsigned char c;
unsigned char ov[17]; unsigned char ov[17];
while( length-- ) while ( length-- ) {
{ memcpy( ov, iv, 16 );
memcpy( ov, iv, 16 ); esp_aes_crypt_ecb( ctx, AES_ENCRYPT, iv, iv );
esp_aes_crypt_ecb( ctx, AES_ENCRYPT, iv, iv );
if( mode == AES_DECRYPT )
ov[16] = *input;
c = *output++ = (unsigned char)( iv[0] ^ *input++ );
if( mode == AES_ENCRYPT ) if ( mode == AES_DECRYPT ) {
ov[16] = c; ov[16] = *input;
}
memcpy( iv, ov + 1, 16 );
}
return 0; c = *output++ = (unsigned char)( iv[0] ^ *input++ );
if ( mode == AES_ENCRYPT ) {
ov[16] = c;
}
memcpy( iv, ov + 1, 16 );
}
return 0;
} }
/* /*
@ -326,18 +337,18 @@ int esp_aes_crypt_ctr( AES_CTX *ctx,
unsigned char stream_block[16], unsigned char stream_block[16],
const unsigned char *input, const unsigned char *input,
unsigned char *output ) unsigned char *output )
{ {
int c, i; int c, i;
size_t n = *nc_off; size_t n = *nc_off;
while( length-- ) while ( length-- ) {
{ if ( n == 0 ) {
if( n == 0 ) {
esp_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-- ) for ( i = 16; i > 0; i-- )
if( ++nonce_counter[i - 1] != 0 ) if ( ++nonce_counter[i - 1] != 0 ) {
break; break;
}
} }
c = *input++; c = *input++;
*output++ = (unsigned char)( c ^ stream_block[n] ); *output++ = (unsigned char)( c ^ stream_block[n] );
@ -347,6 +358,6 @@ int esp_aes_crypt_ctr( AES_CTX *ctx,
*nc_off = n; *nc_off = n;
return 0; return 0;
} }

View File

@ -6,59 +6,61 @@ static SemaphoreHandle_t esp_crypto_mutex[MUTEX_MAX_NUM];
static int esp_crypto_sig[MUTEX_MAX_NUM]; static int esp_crypto_sig[MUTEX_MAX_NUM];
#if 0 #if 0
#define ESP_DEBUG ets_printf #define ESP_DEBUG ets_printf
#else #else
#define ESP_DEBUG(...) #define ESP_DEBUG(...)
#endif #endif
int esp_crypto_init(void) int esp_crypto_init(void)
{ {
int i; int i;
for (i = 0; i < MUTEX_MAX_NUM; i++) { for (i = 0; i < MUTEX_MAX_NUM; i++) {
esp_crypto_mutex[i] = xSemaphoreCreateMutex(); esp_crypto_mutex[i] = xSemaphoreCreateMutex();
ESP_DEBUG("init num %d mutex %p\n", i, esp_crypto_mutex[i]); ESP_DEBUG("init num %d mutex %p\n", i, esp_crypto_mutex[i]);
if (!esp_crypto_mutex[i]) { if (!esp_crypto_mutex[i]) {
goto failed1; goto failed1;
} }
esp_crypto_sig[i] = 0; esp_crypto_sig[i] = 0;
} }
return 0; return 0;
failed1: failed1:
ESP_DEBUG("esp_crypto_init failed\n"); ESP_DEBUG("esp_crypto_init failed\n");
for (i--; i >= 0; i--) for (i--; i >= 0; i--) {
vQueueDelete(esp_crypto_mutex[i]); vQueueDelete(esp_crypto_mutex[i]);
}
return -1; return -1;
} }
void esp_crypto_lock(unsigned int num) void esp_crypto_lock(unsigned int num)
{ {
ESP_DEBUG("1num %d, mutex %p\n", num, esp_crypto_mutex[num]); ESP_DEBUG("1num %d, mutex %p\n", num, esp_crypto_mutex[num]);
xSemaphoreTake(esp_crypto_mutex[num], portMAX_DELAY); xSemaphoreTake(esp_crypto_mutex[num], portMAX_DELAY);
} }
void esp_crypto_unlock(unsigned int num) void esp_crypto_unlock(unsigned int num)
{ {
ESP_DEBUG("2num %d, mutex %p\n", num, esp_crypto_mutex[num]); ESP_DEBUG("2num %d, mutex %p\n", num, esp_crypto_mutex[num]);
xSemaphoreGive(esp_crypto_mutex[num]); xSemaphoreGive(esp_crypto_mutex[num]);
} }
void esp_crypto_take(unsigned int num) void esp_crypto_take(unsigned int num)
{ {
esp_crypto_sig[num]++; esp_crypto_sig[num]++;
} }
void esp_crypto_give(unsigned int num) void esp_crypto_give(unsigned int num)
{ {
if (esp_crypto_sig[num]) if (esp_crypto_sig[num]) {
esp_crypto_sig[num]--; esp_crypto_sig[num]--;
}
} }
bool esp_crypto_is_used(unsigned int num) bool esp_crypto_is_used(unsigned int num)
{ {
return (esp_crypto_sig[num] != 0) ? true : false; return (esp_crypto_sig[num] != 0) ? true : false;
} }

View File

@ -18,9 +18,9 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* *
*/ */
#ifndef ESP_AES_H #ifndef ESP_AES_H
#define ESP_AES_H #define ESP_AES_H
@ -39,11 +39,11 @@ extern "C" {
#define ERR_AES_INVALID_KEY_LENGTH -0x0020 /**< Invalid key length. */ #define ERR_AES_INVALID_KEY_LENGTH -0x0020 /**< Invalid key length. */
#define ERR_AES_INVALID_INPUT_LENGTH -0x0022 /**< Invalid data input length. */ #define ERR_AES_INVALID_INPUT_LENGTH -0x0022 /**< Invalid data input length. */
typedef struct{ typedef struct {
bool keyflag; bool keyflag;
uint16_t keybits; uint16_t keybits;
uint8_t key[32]; uint8_t key[32];
}key_context, KEY_CTX; } key_context, KEY_CTX;
/** /**
* \brief AES context structure * \brief AES context structure
@ -53,13 +53,12 @@ typedef struct{
* - to simplify key expansion in the 256-bit case by * - to simplify key expansion in the 256-bit case by
* generating an extra round key * generating an extra round key
*/ */
typedef struct typedef struct {
{
int nr; /*!< number of rounds */ int nr; /*!< number of rounds */
uint32_t *rk; /*!< AES round keys */ uint32_t *rk; /*!< AES round keys */
KEY_CTX enc; KEY_CTX enc;
KEY_CTX dec; KEY_CTX dec;
}aes_context, AES_CTX; } aes_context, AES_CTX;
/** /**
* \brief Initialize AES context * \brief Initialize AES context
@ -84,7 +83,7 @@ void esp_aes_free( AES_CTX *ctx );
* *
* \return 0 if successful, or ERR_AES_INVALID_KEY_LENGTH * \return 0 if successful, or ERR_AES_INVALID_KEY_LENGTH
*/ */
int esp_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) * \brief AES key schedule (decryption)
@ -95,7 +94,7 @@ int esp_aes_setkey_enc( AES_CTX *ctx, const unsigned char *key,unsigned int keyb
* *
* \return 0 if successful, or ERR_AES_INVALID_KEY_LENGTH * \return 0 if successful, or ERR_AES_INVALID_KEY_LENGTH
*/ */
int esp_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 * \brief AES-ECB block encryption/decryption
@ -107,7 +106,7 @@ int esp_aes_setkey_dec( AES_CTX *ctx, const unsigned char *key,unsigned int keyb
* *
* \return 0 if successful * \return 0 if successful
*/ */
int esp_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 * \brief AES-CBC buffer encryption/decryption
@ -132,11 +131,11 @@ int esp_aes_crypt_ecb( AES_CTX *ctx,int mode,const unsigned char input[16],unsig
* \return 0 if successful, or ERR_AES_INVALID_INPUT_LENGTH * \return 0 if successful, or ERR_AES_INVALID_INPUT_LENGTH
*/ */
int esp_aes_crypt_cbc( AES_CTX *ctx, int esp_aes_crypt_cbc( AES_CTX *ctx,
int mode, int mode,
size_t length, size_t length,
unsigned char iv[16], unsigned char iv[16],
const unsigned char *input, const unsigned char *input,
unsigned char *output ); unsigned char *output );
/** /**
@ -165,12 +164,12 @@ int esp_aes_crypt_cbc( AES_CTX *ctx,
* \return 0 if successful * \return 0 if successful
*/ */
int esp_aes_crypt_cfb128( AES_CTX *ctx, int esp_aes_crypt_cfb128( AES_CTX *ctx,
int mode, int mode,
size_t length, size_t length,
size_t *iv_off, size_t *iv_off,
unsigned char iv[16], unsigned char iv[16],
const unsigned char *input, const unsigned char *input,
unsigned char *output ); unsigned char *output );
/** /**
* \brief AES-CFB8 buffer encryption/decryption. * \brief AES-CFB8 buffer encryption/decryption.
@ -197,11 +196,11 @@ int esp_aes_crypt_cfb128( AES_CTX *ctx,
* \return 0 if successful * \return 0 if successful
*/ */
int esp_aes_crypt_cfb8( AES_CTX *ctx, int esp_aes_crypt_cfb8( AES_CTX *ctx,
int mode, int mode,
size_t length, size_t length,
unsigned char iv[16], unsigned char iv[16],
const unsigned char *input, const unsigned char *input,
unsigned char *output ); unsigned char *output );
/** /**
* \brief AES-CTR buffer encryption/decryption * \brief AES-CTR buffer encryption/decryption
@ -243,7 +242,7 @@ int esp_aes_crypt_ctr( AES_CTX *ctx,
* \param input Plaintext block * \param input Plaintext block
* \param output Output (ciphertext) block * \param output Output (ciphertext) block
*/ */
void esp_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 * \brief Internal AES block decryption function

View File

@ -9,11 +9,11 @@ extern "C" {
#endif #endif
enum { enum {
AES_MUTEX = 0, AES_MUTEX = 0,
BIGNUM_MUTEX, BIGNUM_MUTEX,
SHA_MUTEX, SHA_MUTEX,
MUTEX_MAX_NUM, MUTEX_MAX_NUM,
}; };
int esp_crypto_init(void); int esp_crypto_init(void);
@ -25,29 +25,29 @@ void esp_crypto_take(unsigned int num);
void esp_crypto_give(unsigned int num); void esp_crypto_give(unsigned int num);
bool esp_crypto_is_used(unsigned int num); bool esp_crypto_is_used(unsigned int num);
#define MUTEX_LOCK(num) esp_crypto_lock(num) #define MUTEX_LOCK(num) esp_crypto_lock(num)
#define MUTEX_UNLOCK(num) esp_crypto_unlock(num) #define MUTEX_UNLOCK(num) esp_crypto_unlock(num)
#define SIG_TAKE(num) esp_crypto_take(num) #define SIG_TAKE(num) esp_crypto_take(num)
#define SIG_GIVE(num) esp_crypto_give(num) #define SIG_GIVE(num) esp_crypto_give(num)
#define SIG_IS_USED(num) esp_crypto_is_used(num) #define SIG_IS_USED(num) esp_crypto_is_used(num)
#define AES_LOCK() MUTEX_LOCK(AES_MUTEX) #define AES_LOCK() MUTEX_LOCK(AES_MUTEX)
#define AES_UNLOCK() MUTEX_UNLOCK(AES_MUTEX) #define AES_UNLOCK() MUTEX_UNLOCK(AES_MUTEX)
#define BIGNUM_LOCK() MUTEX_LOCK(BIGNUM_MUTEX) #define BIGNUM_LOCK() MUTEX_LOCK(BIGNUM_MUTEX)
#define BIGNUM_UNLOCK() MUTEX_UNLOCK(BIGNUM_MUTEX) #define BIGNUM_UNLOCK() MUTEX_UNLOCK(BIGNUM_MUTEX)
#define SHA_LOCK() MUTEX_LOCK(SHA_MUTEX) #define SHA_LOCK() MUTEX_LOCK(SHA_MUTEX)
#define SHA_UNLOCK() MUTEX_UNLOCK(SHA_MUTEX) #define SHA_UNLOCK() MUTEX_UNLOCK(SHA_MUTEX)
#define AES_TAKE() SIG_TAKE(AES_MUTEX) #define AES_TAKE() SIG_TAKE(AES_MUTEX)
#define AES_GIVE() SIG_GIVE(AES_MUTEX) #define AES_GIVE() SIG_GIVE(AES_MUTEX)
#define AES_IS_USED() SIG_IS_USED(AES_MUTEX) #define AES_IS_USED() SIG_IS_USED(AES_MUTEX)
#define BIGNUM_TAKE() SIG_TAKE(BIGNUM_MUTEX) #define BIGNUM_TAKE() SIG_TAKE(BIGNUM_MUTEX)
#define BIGNUM_GIVE() SIG_GIVE(BIGNUM_MUTEX) #define BIGNUM_GIVE() SIG_GIVE(BIGNUM_MUTEX)
#define BIGNUM_IS_USED() SIG_IS_USED(BIGNUM_MUTEX) #define BIGNUM_IS_USED() SIG_IS_USED(BIGNUM_MUTEX)
#define SHA_TAKE() SIG_TAKE(SHA_MUTEX) #define SHA_TAKE() SIG_TAKE(SHA_MUTEX)
#define SHA_GIVE() SIG_GIVE(SHA_MUTEX) #define SHA_GIVE() SIG_GIVE(SHA_MUTEX)
#define SHA_IS_USED() SIG_IS_USED(SHA_MUTEX) #define SHA_IS_USED() SIG_IS_USED(SHA_MUTEX)
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -26,9 +26,9 @@ extern "C" {
/** /**
* \brief SHA-1 context structure * \brief SHA-1 context structure
*/ */
typedef struct{ typedef struct {
SHA_CTX context; SHA_CTX context;
int context_type; int context_type;
} sha_context; } sha_context;
typedef sha_context SHA1_CTX; typedef sha_context SHA1_CTX;
@ -91,8 +91,8 @@ void esp_sha1_finish( SHA1_CTX *ctx, unsigned char output[20] );
void esp_sha1_output( const unsigned char *input, size_t ilen, unsigned char output[20] ); void esp_sha1_output( const unsigned char *input, size_t ilen, unsigned char output[20] );
/// ///
#define SHA256 SHA2_256 #define SHA256 SHA2_256
#define SHA224 4 #define SHA224 4
/** /**
* \brief SHA-256 context structure * \brief SHA-256 context structure

View File

@ -38,22 +38,26 @@ void esp_sha1_init( SHA1_CTX *ctx )
SHA_LOCK(); SHA_LOCK();
SHA_TAKE(); SHA_TAKE();
ets_sha_enable(); ets_sha_enable();
SHA_UNLOCK(); SHA_UNLOCK();
} }
void esp_sha1_free( SHA1_CTX *ctx ) void esp_sha1_free( SHA1_CTX *ctx )
{ {
if( ctx == NULL ) if ( ctx == NULL ) {
return; return;
}
bzero( ctx, sizeof( SHA1_CTX ) ); bzero( ctx, sizeof( SHA1_CTX ) );
SHA_LOCK(); SHA_LOCK();
SHA_GIVE(); SHA_GIVE();
if (false == SHA_IS_USED())
ets_sha_disable(); if (false == SHA_IS_USED()) {
SHA_UNLOCK(); ets_sha_disable();
}
SHA_UNLOCK();
} }
void esp_sha1_clone( SHA1_CTX *dst, const SHA1_CTX *src ) void esp_sha1_clone( SHA1_CTX *dst, const SHA1_CTX *src )
@ -71,10 +75,10 @@ void esp_sha1_process(SHA1_CTX *ctx, const unsigned char data[64])
*/ */
void esp_sha1_start( SHA1_CTX *ctx ) void esp_sha1_start( SHA1_CTX *ctx )
{ {
SHA_LOCK(); SHA_LOCK();
ets_sha_init(&ctx->context); ets_sha_init(&ctx->context);
ctx->context_type = SHA1; ctx->context_type = SHA1;
} }
/* /*
@ -82,7 +86,7 @@ void esp_sha1_start( SHA1_CTX *ctx )
*/ */
void esp_sha1_update( SHA1_CTX *ctx, const unsigned char *input, size_t ilen ) void esp_sha1_update( SHA1_CTX *ctx, const unsigned char *input, size_t ilen )
{ {
ets_sha_update(&ctx->context, ctx->context_type, input, ilen * 8); ets_sha_update(&ctx->context, ctx->context_type, input, ilen * 8);
} }
/* /*
@ -90,8 +94,8 @@ void esp_sha1_update( SHA1_CTX *ctx, const unsigned char *input, size_t ilen )
*/ */
void esp_sha1_finish( SHA1_CTX *ctx, unsigned char output[20] ) void esp_sha1_finish( SHA1_CTX *ctx, unsigned char output[20] )
{ {
ets_sha_finish(&ctx->context, ctx->context_type, output); ets_sha_finish(&ctx->context, ctx->context_type, output);
SHA_UNLOCK(); SHA_UNLOCK();
} }
/* /*
@ -116,8 +120,8 @@ void esp_sha256_init( SHA256_CTX *ctx )
SHA_LOCK(); SHA_LOCK();
SHA_TAKE(); SHA_TAKE();
ets_sha_enable(); ets_sha_enable();
SHA_UNLOCK(); SHA_UNLOCK();
} }
void esp_sha256_process(SHA256_CTX *ctx, const unsigned char data[64]) void esp_sha256_process(SHA256_CTX *ctx, const unsigned char data[64])
@ -127,16 +131,20 @@ void esp_sha256_process(SHA256_CTX *ctx, const unsigned char data[64])
void esp_sha256_free( SHA256_CTX *ctx ) void esp_sha256_free( SHA256_CTX *ctx )
{ {
if( ctx == NULL ) if ( ctx == NULL ) {
return; return;
}
bzero( ctx, sizeof( SHA256_CTX ) ); bzero( ctx, sizeof( SHA256_CTX ) );
SHA_LOCK(); SHA_LOCK();
SHA_GIVE(); SHA_GIVE();
if (false == SHA_IS_USED())
ets_sha_disable(); if (false == SHA_IS_USED()) {
SHA_UNLOCK(); ets_sha_disable();
}
SHA_UNLOCK();
} }
void esp_sha256_clone( SHA256_CTX *dst, const SHA256_CTX *src ) void esp_sha256_clone( SHA256_CTX *dst, const SHA256_CTX *src )
@ -149,17 +157,16 @@ void esp_sha256_clone( SHA256_CTX *dst, const SHA256_CTX *src )
*/ */
void esp_sha256_start( SHA256_CTX *ctx, int is224 ) void esp_sha256_start( SHA256_CTX *ctx, int is224 )
{ {
SHA_LOCK(); SHA_LOCK();
ets_sha_init(&ctx->context); ets_sha_init(&ctx->context);
if( is224 == 0 ) if ( is224 == 0 ) {
{
/* SHA-256 */ /* SHA-256 */
ctx->context_type = SHA256; ctx->context_type = SHA256;
}else{ } else {
/* SHA-224 */ /* SHA-224 */
ctx->context_type = SHA224; ctx->context_type = SHA224;
} }
} }
/* /*
@ -167,7 +174,7 @@ void esp_sha256_start( SHA256_CTX *ctx, int is224 )
*/ */
void esp_sha256_update( SHA256_CTX *ctx, const unsigned char *input, size_t ilen ) void esp_sha256_update( SHA256_CTX *ctx, const unsigned char *input, size_t ilen )
{ {
ets_sha_update(&ctx->context, ctx->context_type, input, ilen * 8); ets_sha_update(&ctx->context, ctx->context_type, input, ilen * 8);
} }
/* /*
@ -175,8 +182,8 @@ void esp_sha256_update( SHA256_CTX *ctx, const unsigned char *input, size_t ilen
*/ */
void esp_sha256_finish( SHA256_CTX *ctx, unsigned char output[32] ) void esp_sha256_finish( SHA256_CTX *ctx, unsigned char output[32] )
{ {
ets_sha_finish(&ctx->context, ctx->context_type, output); ets_sha_finish(&ctx->context, ctx->context_type, output);
SHA_UNLOCK(); SHA_UNLOCK();
} }
/* /*
@ -201,8 +208,8 @@ void esp_sha512_init( SHA512_CTX *ctx )
SHA_LOCK(); SHA_LOCK();
SHA_TAKE(); SHA_TAKE();
ets_sha_enable(); ets_sha_enable();
SHA_UNLOCK(); SHA_UNLOCK();
} }
void esp_sha512_process( SHA512_CTX *ctx, const unsigned char data[128] ) void esp_sha512_process( SHA512_CTX *ctx, const unsigned char data[128] )
@ -212,16 +219,20 @@ void esp_sha512_process( SHA512_CTX *ctx, const unsigned char data[128] )
void esp_sha512_free( SHA512_CTX *ctx ) void esp_sha512_free( SHA512_CTX *ctx )
{ {
if( ctx == NULL ) if ( ctx == NULL ) {
return; return;
}
bzero( ctx, sizeof( SHA512_CTX ) ); bzero( ctx, sizeof( SHA512_CTX ) );
SHA_LOCK(); SHA_LOCK();
SHA_GIVE(); SHA_GIVE();
if (false == SHA_IS_USED())
ets_sha_disable(); if (false == SHA_IS_USED()) {
SHA_UNLOCK(); ets_sha_disable();
}
SHA_UNLOCK();
} }
void esp_sha512_clone( SHA512_CTX *dst, const SHA512_CTX *src ) void esp_sha512_clone( SHA512_CTX *dst, const SHA512_CTX *src )
@ -234,16 +245,13 @@ void esp_sha512_clone( SHA512_CTX *dst, const SHA512_CTX *src )
*/ */
void esp_sha512_start( SHA512_CTX *ctx, int is384 ) void esp_sha512_start( SHA512_CTX *ctx, int is384 )
{ {
SHA_LOCK(); SHA_LOCK();
ets_sha_init(&ctx->context); ets_sha_init(&ctx->context);
if( is384 == 0 ) if ( is384 == 0 ) {
{
/* SHA-512 */ /* SHA-512 */
ctx->context_type = SHA2_512; ctx->context_type = SHA2_512;
} } else {
else
{
/* SHA-384 */ /* SHA-384 */
ctx->context_type = SHA2_384; ctx->context_type = SHA2_384;
} }
@ -252,7 +260,7 @@ void esp_sha512_start( SHA512_CTX *ctx, int is384 )
/* /*
* SHA-512 process buffer * SHA-512 process buffer
*/ */
void esp_sha512_update( SHA512_CTX *ctx, const unsigned char *input,size_t ilen ) void esp_sha512_update( SHA512_CTX *ctx, const unsigned char *input, size_t ilen )
{ {
ets_sha_update(&ctx->context, ctx->context_type, input, ilen * 8); ets_sha_update(&ctx->context, ctx->context_type, input, ilen * 8);
} }
@ -269,7 +277,7 @@ void esp_sha512_finish( SHA512_CTX *ctx, unsigned char output[64] )
/* /*
* output = SHA-512( input buffer ) * output = SHA-512( input buffer )
*/ */
void esp_sha512_output( const unsigned char *input, size_t ilen,unsigned char output[64], int is384 ) void esp_sha512_output( const unsigned char *input, size_t ilen, unsigned char output[64], int is384 )
{ {
SHA512_CTX ctx; SHA512_CTX ctx;

View File

@ -7,6 +7,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT) #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
/** /**
* \brief Entropy poll callback for a hardware source * \brief Entropy poll callback for a hardware source
@ -18,28 +19,31 @@
*/ */
static int os_get_random(unsigned char *buf, size_t len) static int os_get_random(unsigned char *buf, size_t len)
{ {
int i, j; int i, j;
unsigned long tmp; unsigned long tmp;
for (i = 0; i < ((len + 3) & ~3) / 4; i ++){
tmp = rand();
for (j = 0; j < 4; j ++){
if ((i * 4 + j) < len){
buf[i * 4 + j] = (unsigned char)(tmp >> (j * 8));
}else{
break;
}
}
} for (i = 0; i < ((len + 3) & ~3) / 4; i ++) {
return 0; tmp = rand();
for (j = 0; j < 4; j ++) {
if ((i * 4 + j) < len) {
buf[i * 4 + j] = (unsigned char)(tmp >> (j * 8));
} else {
break;
}
}
}
return 0;
} }
int mbedtls_hardware_poll( void *data, int mbedtls_hardware_poll( void *data,
unsigned char *output, size_t len, size_t *olen ) unsigned char *output, size_t len, size_t *olen )
{ {
os_get_random(output, len); os_get_random(output, len);
*olen = len; *olen = len;
return 0;
return 0;
} }
#endif #endif

View File

@ -18,9 +18,9 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* *
*/ */
#ifndef AES_ALT_H #ifndef AES_ALT_H
#define AES_ALT_H #define AES_ALT_H
@ -33,23 +33,23 @@ extern "C" {
typedef AES_CTX mbedtls_aes_context; typedef AES_CTX mbedtls_aes_context;
#define mbedtls_aes_init esp_aes_init #define mbedtls_aes_init esp_aes_init
#define mbedtls_aes_free esp_aes_free #define mbedtls_aes_free esp_aes_free
#define mbedtls_aes_setkey_enc esp_aes_setkey_enc #define mbedtls_aes_setkey_enc esp_aes_setkey_enc
#define mbedtls_aes_setkey_dec esp_aes_setkey_dec #define mbedtls_aes_setkey_dec esp_aes_setkey_dec
#define mbedtls_aes_crypt_ecb esp_aes_crypt_ecb #define mbedtls_aes_crypt_ecb esp_aes_crypt_ecb
#if defined(MBEDTLS_CIPHER_MODE_CBC) #if defined(MBEDTLS_CIPHER_MODE_CBC)
#define mbedtls_aes_crypt_cbc esp_aes_crypt_cbc #define mbedtls_aes_crypt_cbc esp_aes_crypt_cbc
#endif #endif
#if defined(MBEDTLS_CIPHER_MODE_CFB) #if defined(MBEDTLS_CIPHER_MODE_CFB)
#define mbedtls_aes_crypt_cfb128 esp_aes_crypt_cfb128 #define mbedtls_aes_crypt_cfb128 esp_aes_crypt_cfb128
#define mbedtls_aes_crypt_cfb8 esp_aes_crypt_cfb8 #define mbedtls_aes_crypt_cfb8 esp_aes_crypt_cfb8
#endif #endif
#if defined(MBEDTLS_CIPHER_MODE_CTR) #if defined(MBEDTLS_CIPHER_MODE_CTR)
#define mbedtls_aes_crypt_ctr esp_aes_crypt_ctr #define mbedtls_aes_crypt_ctr esp_aes_crypt_ctr
#endif #endif
#define mbedtls_aes_encrypt esp_aes_encrypt #define mbedtls_aes_encrypt esp_aes_encrypt
#define mbedtls_aes_decrypt esp_aes_decrypt #define mbedtls_aes_decrypt esp_aes_decrypt
#endif /* MBEDTLS_AES_ALT */ #endif /* MBEDTLS_AES_ALT */
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -19,7 +19,7 @@
* limitations under the License. * limitations under the License.
* *
*/ */
#ifndef BIGNUM_ALT_H #ifndef BIGNUM_ALT_H
#define BIGNUM_ALT_H #define BIGNUM_ALT_H
@ -27,50 +27,50 @@
#if defined(MBEDTLS_BIGNUM_ALT) #if defined(MBEDTLS_BIGNUM_ALT)
typedef MPI_CTX mbedtls_mpi; typedef MPI_CTX mbedtls_mpi;
#define mbedtls_mpi_init esp_mpi_init #define mbedtls_mpi_init esp_mpi_init
#define mbedtls_mpi_free esp_mpi_free #define mbedtls_mpi_free esp_mpi_free
#define mbedtls_mpi_grow esp_mpi_grow #define mbedtls_mpi_grow esp_mpi_grow
#define mbedtls_mpi_shrink esp_mpi_shrink #define mbedtls_mpi_shrink esp_mpi_shrink
#define mbedtls_mpi_copy esp_mpi_copy #define mbedtls_mpi_copy esp_mpi_copy
#define mbedtls_mpi_swap esp_mpi_swap #define mbedtls_mpi_swap esp_mpi_swap
#define mbedtls_mpi_safe_cond_assign esp_mpi_safe_cond_assign #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_safe_cond_swap esp_mpi_safe_cond_swap
#define mbedtls_mpi_lset esp_mpi_lset #define mbedtls_mpi_lset esp_mpi_lset
#define mbedtls_mpi_get_bit esp_mpi_get_bit #define mbedtls_mpi_get_bit esp_mpi_get_bit
#define mbedtls_mpi_set_bit esp_mpi_set_bit #define mbedtls_mpi_set_bit esp_mpi_set_bit
#define mbedtls_mpi_lsb esp_mpi_lsb #define mbedtls_mpi_lsb esp_mpi_lsb
#define mbedtls_mpi_bitlen esp_mpi_bitlen #define mbedtls_mpi_bitlen esp_mpi_bitlen
#define mbedtls_mpi_size esp_mpi_size #define mbedtls_mpi_size esp_mpi_size
#define mbedtls_mpi_read_string esp_mpi_read_string #define mbedtls_mpi_read_string esp_mpi_read_string
#define mbedtls_mpi_write_string esp_mpi_write_string #define mbedtls_mpi_write_string esp_mpi_write_string
#define mbedtls_mpi_read_binary esp_mpi_read_binary #define mbedtls_mpi_read_binary esp_mpi_read_binary
#define mbedtls_mpi_write_binary esp_mpi_write_binary #define mbedtls_mpi_write_binary esp_mpi_write_binary
#define mbedtls_mpi_shift_l esp_mpi_shift_l #define mbedtls_mpi_shift_l esp_mpi_shift_l
#define mbedtls_mpi_shift_r esp_mpi_shift_r #define mbedtls_mpi_shift_r esp_mpi_shift_r
#define mbedtls_mpi_cmp_abs esp_mpi_cmp_abs #define mbedtls_mpi_cmp_abs esp_mpi_cmp_abs
#define mbedtls_mpi_cmp_mpi esp_mpi_cmp_mpi #define mbedtls_mpi_cmp_mpi esp_mpi_cmp_mpi
#define mbedtls_mpi_cmp_int esp_mpi_cmp_int #define mbedtls_mpi_cmp_int esp_mpi_cmp_int
#define mbedtls_mpi_add_abs esp_mpi_add_abs #define mbedtls_mpi_add_abs esp_mpi_add_abs
#define mbedtls_mpi_sub_abs esp_mpi_sub_abs #define mbedtls_mpi_sub_abs esp_mpi_sub_abs
#define mbedtls_mpi_add_mpi esp_mpi_add_mpi #define mbedtls_mpi_add_mpi esp_mpi_add_mpi
#define mbedtls_mpi_sub_mpi esp_mpi_sub_mpi #define mbedtls_mpi_sub_mpi esp_mpi_sub_mpi
#define mbedtls_mpi_add_int esp_mpi_add_int #define mbedtls_mpi_add_int esp_mpi_add_int
#define mbedtls_mpi_sub_int esp_mpi_sub_int #define mbedtls_mpi_sub_int esp_mpi_sub_int
#define mbedtls_mpi_mul_mpi esp_mpi_mul_mpi #define mbedtls_mpi_mul_mpi esp_mpi_mul_mpi
#define mbedtls_mpi_mul_int esp_mpi_mul_int #define mbedtls_mpi_mul_int esp_mpi_mul_int
#define mbedtls_mpi_div_mpi esp_mpi_div_mpi #define mbedtls_mpi_div_mpi esp_mpi_div_mpi
#define mbedtls_mpi_div_int esp_mpi_div_int #define mbedtls_mpi_div_int esp_mpi_div_int
#define mbedtls_mpi_mod_mpi esp_mpi_mod_mpi #define mbedtls_mpi_mod_mpi esp_mpi_mod_mpi
#define mbedtls_mpi_mod_int esp_mpi_mod_int #define mbedtls_mpi_mod_int esp_mpi_mod_int
#define mbedtls_mpi_exp_mod esp_mpi_exp_mod #define mbedtls_mpi_exp_mod esp_mpi_exp_mod
#define mbedtls_mpi_fill_random esp_mpi_fill_random #define mbedtls_mpi_fill_random esp_mpi_fill_random
#define mbedtls_mpi_gcd esp_mpi_gcd #define mbedtls_mpi_gcd esp_mpi_gcd
#define mbedtls_mpi_inv_mod esp_mpi_inv_mod #define mbedtls_mpi_inv_mod esp_mpi_inv_mod
#define mbedtls_mpi_is_prime esp_mpi_is_prime #define mbedtls_mpi_is_prime esp_mpi_is_prime
#define mbedtls_mpi_gen_prime esp_mpi_gen_prime #define mbedtls_mpi_gen_prime esp_mpi_gen_prime
#endif #endif
#endif #endif

View File

@ -1,6 +1,6 @@
/* /*
* copyright (c) 2010 - 2012 Espressif System * copyright (c) 2010 - 2012 Espressif System
* *
* esf Link List Descriptor * esf Link List Descriptor
*/ */
#ifndef _SHA1_ALT_H_ #ifndef _SHA1_ALT_H_
@ -16,13 +16,13 @@ extern "C" {
typedef SHA1_CTX mbedtls_sha1_context; typedef SHA1_CTX mbedtls_sha1_context;
#define mbedtls_sha1_init esp_sha1_init #define mbedtls_sha1_init esp_sha1_init
#define mbedtls_sha1_starts esp_sha1_start #define mbedtls_sha1_starts esp_sha1_start
#define mbedtls_sha1_clone esp_sha1_clone #define mbedtls_sha1_clone esp_sha1_clone
#define mbedtls_sha1_update esp_sha1_update #define mbedtls_sha1_update esp_sha1_update
#define mbedtls_sha1_finish esp_sha1_finish #define mbedtls_sha1_finish esp_sha1_finish
#define mbedtls_sha1_free esp_sha1_free #define mbedtls_sha1_free esp_sha1_free
#define mbedtls_sha1_process esp_sha1_process #define mbedtls_sha1_process esp_sha1_process
#endif #endif

View File

@ -1,6 +1,6 @@
/* /*
* copyright (c) 2010 - 2012 Espressif System * copyright (c) 2010 - 2012 Espressif System
* *
* esf Link List Descriptor * esf Link List Descriptor
*/ */
@ -17,13 +17,13 @@ extern "C" {
typedef SHA256_CTX mbedtls_sha256_context; typedef SHA256_CTX mbedtls_sha256_context;
#define mbedtls_sha256_init esp_sha256_init #define mbedtls_sha256_init esp_sha256_init
#define mbedtls_sha256_clone esp_sha256_clone #define mbedtls_sha256_clone esp_sha256_clone
#define mbedtls_sha256_starts esp_sha256_start #define mbedtls_sha256_starts esp_sha256_start
#define mbedtls_sha256_update esp_sha256_update #define mbedtls_sha256_update esp_sha256_update
#define mbedtls_sha256_finish esp_sha256_finish #define mbedtls_sha256_finish esp_sha256_finish
#define mbedtls_sha256_free esp_sha256_free #define mbedtls_sha256_free esp_sha256_free
#define mbedtls_sha256_process esp_sha256_process #define mbedtls_sha256_process esp_sha256_process
#endif #endif

View File

@ -1,6 +1,6 @@
/* /*
* copyright (c) 2010 - 2012 Espressif System * copyright (c) 2010 - 2012 Espressif System
* *
* esf Link List Descriptor * esf Link List Descriptor
*/ */
@ -14,15 +14,15 @@ extern "C" {
#if defined(MBEDTLS_SHA512_ALT) #if defined(MBEDTLS_SHA512_ALT)
#include "sha.h" #include "sha.h"
typedef SHA512_CTX mbedtls_sha512_context; typedef SHA512_CTX mbedtls_sha512_context;
#define mbedtls_sha512_init esp_sha512_init #define mbedtls_sha512_init esp_sha512_init
#define mbedtls_sha512_process esp_sha512_process #define mbedtls_sha512_process esp_sha512_process
#define mbedtls_sha512_clone esp_sha512_clone #define mbedtls_sha512_clone esp_sha512_clone
#define mbedtls_sha512_starts esp_sha512_start #define mbedtls_sha512_starts esp_sha512_start
#define mbedtls_sha512_update esp_sha512_update #define mbedtls_sha512_update esp_sha512_update
#define mbedtls_sha512_finish esp_sha512_finish #define mbedtls_sha512_finish esp_sha512_finish
#define mbedtls_sha512_free esp_sha512_free #define mbedtls_sha512_free esp_sha512_free
#endif #endif

View File

@ -56,24 +56,26 @@ static int net_prepare( void )
!defined(EFI32) !defined(EFI32)
WSADATA wsaData; WSADATA wsaData;
if( wsa_init_done == 0 ) if ( wsa_init_done == 0 ) {
{ if ( WSAStartup( MAKEWORD(2, 0), &wsaData ) != 0 ) {
if( WSAStartup( MAKEWORD(2,0), &wsaData ) != 0 ) return ( MBEDTLS_ERR_NET_SOCKET_FAILED );
return( MBEDTLS_ERR_NET_SOCKET_FAILED ); }
wsa_init_done = 1; wsa_init_done = 1;
} }
#else #else
#endif #endif
return( 0 ); return ( 0 );
} }
static int mbedtls_net_errno(int fd) static int mbedtls_net_errno(int fd)
{ {
int sock_errno = 0; int sock_errno = 0;
u32_t optlen = sizeof(sock_errno); u32_t optlen = sizeof(sock_errno);
getsockopt(fd, SOL_SOCKET, SO_ERROR, &sock_errno, &optlen);
return sock_errno; getsockopt(fd, SOL_SOCKET, SO_ERROR, &sock_errno, &optlen);
return sock_errno;
} }
/* /*
@ -92,8 +94,9 @@ int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host, const char
int ret; int ret;
struct addrinfo hints, *addr_list, *cur; struct addrinfo hints, *addr_list, *cur;
if( ( ret = net_prepare() ) != 0 ) if ( ( ret = net_prepare() ) != 0 ) {
return( ret ); return ( ret );
}
/* Do name resolution with both IPv6 and IPv4 */ /* Do name resolution with both IPv6 and IPv4 */
memset( &hints, 0, sizeof( hints ) ); memset( &hints, 0, sizeof( hints ) );
@ -101,23 +104,21 @@ int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host, const char
hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM; hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP; hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
if( getaddrinfo( host, port, &hints, &addr_list ) != 0 ) if ( getaddrinfo( host, port, &hints, &addr_list ) != 0 ) {
return( MBEDTLS_ERR_NET_UNKNOWN_HOST ); return ( MBEDTLS_ERR_NET_UNKNOWN_HOST );
}
/* Try the sockaddrs until a connection succeeds */ /* Try the sockaddrs until a connection succeeds */
ret = MBEDTLS_ERR_NET_UNKNOWN_HOST; ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
for( cur = addr_list; cur != NULL; cur = cur->ai_next ) for ( cur = addr_list; cur != NULL; cur = cur->ai_next ) {
{
ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype, ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype,
cur->ai_protocol ); cur->ai_protocol );
if( ctx->fd < 0 ) if ( ctx->fd < 0 ) {
{
ret = MBEDTLS_ERR_NET_SOCKET_FAILED; ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
continue; continue;
} }
if( connect( ctx->fd, cur->ai_addr, cur->ai_addrlen ) == 0 ) if ( connect( ctx->fd, cur->ai_addr, cur->ai_addrlen ) == 0 ) {
{
ret = 0; ret = 0;
break; break;
} }
@ -128,7 +129,7 @@ int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host, const char
freeaddrinfo( addr_list ); freeaddrinfo( addr_list );
return( ret ); return ( ret );
} }
/* /*
@ -139,8 +140,9 @@ int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char
int n, ret; int n, ret;
struct addrinfo hints, *addr_list, *cur; struct addrinfo hints, *addr_list, *cur;
if( ( ret = net_prepare() ) != 0 ) if ( ( ret = net_prepare() ) != 0 ) {
return( ret ); return ( ret );
}
/* Bind to IPv6 and/or IPv4, but only in the desired protocol */ /* Bind to IPv6 and/or IPv4, but only in the desired protocol */
memset( &hints, 0, sizeof( hints ) ); memset( &hints, 0, sizeof( hints ) );
@ -148,48 +150,43 @@ int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char
hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM; hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP; hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
if( getaddrinfo( bind_ip, port, &hints, &addr_list ) != 0 ) if ( getaddrinfo( bind_ip, port, &hints, &addr_list ) != 0 ) {
return( MBEDTLS_ERR_NET_UNKNOWN_HOST ); return ( MBEDTLS_ERR_NET_UNKNOWN_HOST );
}
/* Try the sockaddrs until a binding succeeds */ /* Try the sockaddrs until a binding succeeds */
ret = MBEDTLS_ERR_NET_UNKNOWN_HOST; ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
for( cur = addr_list; cur != NULL; cur = cur->ai_next ) for ( cur = addr_list; cur != NULL; cur = cur->ai_next ) {
{
ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype, ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype,
cur->ai_protocol ); cur->ai_protocol );
if( ctx->fd < 0 ) if ( ctx->fd < 0 ) {
{
ret = MBEDTLS_ERR_NET_SOCKET_FAILED; ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
continue; continue;
} }
/*SO_REUSEADDR option dafault is disable in source code(lwip)*/ /*SO_REUSEADDR option dafault is disable in source code(lwip)*/
#if SO_REUSE #if SO_REUSE
n = 1; n = 1;
if( setsockopt( ctx->fd, SOL_SOCKET, SO_REUSEADDR, if ( setsockopt( ctx->fd, SOL_SOCKET, SO_REUSEADDR,
(const char *) &n, sizeof( n ) ) != 0 ) (const char *) &n, sizeof( n ) ) != 0 ) {
{
close( ctx->fd ); close( ctx->fd );
ret = MBEDTLS_ERR_NET_SOCKET_FAILED; ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
continue; continue;
} }
#endif #endif
/*bind interface dafault don't process the addr is 0xffffffff for TCP Protocol*/ /*bind interface dafault don't process the addr is 0xffffffff for TCP Protocol*/
struct sockaddr_in *serv_addr = NULL; struct sockaddr_in *serv_addr = NULL;
serv_addr = (struct sockaddr_in *)cur->ai_addr; serv_addr = (struct sockaddr_in *)cur->ai_addr;
serv_addr->sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */ serv_addr->sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
if( bind( ctx->fd, (struct sockaddr *)serv_addr, cur->ai_addrlen ) != 0 ) if ( bind( ctx->fd, (struct sockaddr *)serv_addr, cur->ai_addrlen ) != 0 ) {
{
close( ctx->fd ); close( ctx->fd );
ret = MBEDTLS_ERR_NET_BIND_FAILED; ret = MBEDTLS_ERR_NET_BIND_FAILED;
continue; continue;
} }
/* Listen only makes sense for TCP */ /* Listen only makes sense for TCP */
if( proto == MBEDTLS_NET_PROTO_TCP ) if ( proto == MBEDTLS_NET_PROTO_TCP ) {
{ if ( listen( ctx->fd, MBEDTLS_NET_LISTEN_BACKLOG ) != 0 ) {
if( listen( ctx->fd, MBEDTLS_NET_LISTEN_BACKLOG ) != 0 )
{
close( ctx->fd ); close( ctx->fd );
ret = MBEDTLS_ERR_NET_LISTEN_FAILED; ret = MBEDTLS_ERR_NET_LISTEN_FAILED;
continue; continue;
@ -203,7 +200,7 @@ int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char
freeaddrinfo( addr_list ); freeaddrinfo( addr_list );
return( ret ); return ( ret );
} }
@ -216,7 +213,7 @@ int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char
static int net_would_block( const mbedtls_net_context *ctx ) static int net_would_block( const mbedtls_net_context *ctx )
{ {
((void) ctx); ((void) ctx);
return( WSAGetLastError() == WSAEWOULDBLOCK ); return ( WSAGetLastError() == WSAEWOULDBLOCK );
} }
#else #else
/* /*
@ -230,22 +227,22 @@ static int net_would_block( const mbedtls_net_context *ctx )
/* /*
* Never return 'WOULD BLOCK' on a non-blocking socket * Never return 'WOULD BLOCK' on a non-blocking socket
*/ */
if( ( fcntl( ctx->fd, F_GETFL, 0) & O_NONBLOCK ) != O_NONBLOCK ) if ( ( fcntl( ctx->fd, F_GETFL, 0) & O_NONBLOCK ) != O_NONBLOCK ) {
return( 0 ); return ( 0 );
}
int error = mbedtls_net_errno(ctx->fd); int error = mbedtls_net_errno(ctx->fd);
switch( error ) switch ( error ) {
{
#if defined EAGAIN #if defined EAGAIN
case EAGAIN: case EAGAIN:
#endif #endif
#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN #if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
case EWOULDBLOCK: case EWOULDBLOCK:
#endif #endif
return( 1 ); return ( 1 );
} }
return( 0 ); return ( 0 );
} }
#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */ #endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
@ -265,21 +262,17 @@ int mbedtls_net_accept( mbedtls_net_context *bind_ctx,
socklen_t type_len = (socklen_t) sizeof( type ); socklen_t type_len = (socklen_t) sizeof( type );
/* Is this a TCP or UDP socket? */ /* Is this a TCP or UDP socket? */
if( getsockopt( bind_ctx->fd, SOL_SOCKET, SO_TYPE, if ( getsockopt( bind_ctx->fd, SOL_SOCKET, SO_TYPE,
(void *) &type, (socklen_t *) &type_len ) != 0 || (void *) &type, (socklen_t *) &type_len ) != 0 ||
( type != SOCK_STREAM && type != SOCK_DGRAM ) ) ( type != SOCK_STREAM && type != SOCK_DGRAM ) ) {
{ return ( MBEDTLS_ERR_NET_ACCEPT_FAILED );
return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
} }
if( type == SOCK_STREAM ) if ( type == SOCK_STREAM ) {
{
/* TCP: actual accept() */ /* TCP: actual accept() */
ret = client_ctx->fd = (int) accept( bind_ctx->fd, ret = client_ctx->fd = (int) accept( bind_ctx->fd,
(struct sockaddr *) &client_addr, &n ); (struct sockaddr *) &client_addr, &n );
} } else {
else
{
/* UDP: wait for a message, but keep it in the queue */ /* UDP: wait for a message, but keep it in the queue */
char buf[1] = { 0 }; char buf[1] = { 0 };
@ -287,65 +280,62 @@ int mbedtls_net_accept( mbedtls_net_context *bind_ctx,
(struct sockaddr *) &client_addr, &n ); (struct sockaddr *) &client_addr, &n );
#if defined(_WIN32) #if defined(_WIN32)
if( ret == SOCKET_ERROR && if ( ret == SOCKET_ERROR &&
WSAGetLastError() == WSAEMSGSIZE ) WSAGetLastError() == WSAEMSGSIZE ) {
{
/* We know buf is too small, thanks, just peeking here */ /* We know buf is too small, thanks, just peeking here */
ret = 0; ret = 0;
} }
#endif #endif
} }
if( ret < 0 ) if ( ret < 0 ) {
{ if ( net_would_block( bind_ctx ) != 0 ) {
if( net_would_block( bind_ctx ) != 0 ) return ( MBEDTLS_ERR_SSL_WANT_READ );
return( MBEDTLS_ERR_SSL_WANT_READ ); }
return( MBEDTLS_ERR_NET_ACCEPT_FAILED ); return ( MBEDTLS_ERR_NET_ACCEPT_FAILED );
} }
/* UDP: hijack the listening socket to communicate with the client, /* UDP: hijack the listening socket to communicate with the client,
* then bind a new socket to accept new connections */ * then bind a new socket to accept new connections */
if( type != SOCK_STREAM ) if ( type != SOCK_STREAM ) {
{
struct sockaddr_in local_addr; struct sockaddr_in local_addr;
int one = 1; int one = 1;
if( connect( bind_ctx->fd, (struct sockaddr *) &client_addr, n ) != 0 ) if ( connect( bind_ctx->fd, (struct sockaddr *) &client_addr, n ) != 0 ) {
return( MBEDTLS_ERR_NET_ACCEPT_FAILED ); return ( MBEDTLS_ERR_NET_ACCEPT_FAILED );
}
client_ctx->fd = bind_ctx->fd; client_ctx->fd = bind_ctx->fd;
bind_ctx->fd = -1; /* In case we exit early */ bind_ctx->fd = -1; /* In case we exit early */
n = sizeof( struct sockaddr_in ); n = sizeof( struct sockaddr_in );
if( getsockname( client_ctx->fd, if ( getsockname( client_ctx->fd,
(struct sockaddr *) &local_addr, &n ) != 0 || (struct sockaddr *) &local_addr, &n ) != 0 ||
( bind_ctx->fd = (int) socket( AF_INET, ( bind_ctx->fd = (int) socket( AF_INET,
SOCK_DGRAM, IPPROTO_UDP ) ) < 0 || SOCK_DGRAM, IPPROTO_UDP ) ) < 0 ||
setsockopt( bind_ctx->fd, SOL_SOCKET, SO_REUSEADDR, setsockopt( bind_ctx->fd, SOL_SOCKET, SO_REUSEADDR,
(const char *) &one, sizeof( one ) ) != 0 ) (const char *) &one, sizeof( one ) ) != 0 ) {
{ return ( MBEDTLS_ERR_NET_SOCKET_FAILED );
return( MBEDTLS_ERR_NET_SOCKET_FAILED );
} }
if( bind( bind_ctx->fd, (struct sockaddr *) &local_addr, n ) != 0 ) if ( bind( bind_ctx->fd, (struct sockaddr *) &local_addr, n ) != 0 ) {
{ return ( MBEDTLS_ERR_NET_BIND_FAILED );
return( MBEDTLS_ERR_NET_BIND_FAILED );
} }
} }
if( client_ip != NULL ) if ( client_ip != NULL ) {
{ struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr; *ip_len = sizeof( addr4->sin_addr.s_addr );
*ip_len = sizeof( addr4->sin_addr.s_addr );
if( buf_size < *ip_len ) if ( buf_size < *ip_len ) {
return( MBEDTLS_ERR_NET_BUFFER_TOO_SMALL ); return ( MBEDTLS_ERR_NET_BUFFER_TOO_SMALL );
}
memcpy( client_ip, &addr4->sin_addr.s_addr, *ip_len ); memcpy( client_ip, &addr4->sin_addr.s_addr, *ip_len );
} }
return( 0 ); return ( 0 );
} }
/* /*
@ -356,9 +346,9 @@ int mbedtls_net_set_block( mbedtls_net_context *ctx )
#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \ #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
!defined(EFI32) !defined(EFI32)
u_long n = 0; u_long n = 0;
return( ioctlsocket( ctx->fd, FIONBIO, &n ) ); return ( ioctlsocket( ctx->fd, FIONBIO, &n ) );
#else #else
return( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL, 0 ) & ~O_NONBLOCK ) ); return ( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL, 0 ) & ~O_NONBLOCK ) );
#endif #endif
} }
@ -367,9 +357,9 @@ int mbedtls_net_set_nonblock( mbedtls_net_context *ctx )
#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \ #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
!defined(EFI32) !defined(EFI32)
u_long n = 1; u_long n = 1;
return( ioctlsocket( ctx->fd, FIONBIO, &n ) ); return ( ioctlsocket( ctx->fd, FIONBIO, &n ) );
#else #else
return( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL, 0 ) | O_NONBLOCK ) ); return ( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL, 0 ) | O_NONBLOCK ) );
#endif #endif
} }
@ -401,49 +391,54 @@ int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len )
int ret; int ret;
int fd = ((mbedtls_net_context *) ctx)->fd; int fd = ((mbedtls_net_context *) ctx)->fd;
int error = 0; int error = 0;
if( fd < 0 ) if ( fd < 0 ) {
return( MBEDTLS_ERR_NET_INVALID_CONTEXT ); return ( MBEDTLS_ERR_NET_INVALID_CONTEXT );
}
ret = (int) read( fd, buf, len ); ret = (int) read( fd, buf, len );
if( ret < 0 ) if ( ret < 0 ) {
{ if ( net_would_block( ctx ) != 0 ) {
if( net_would_block( ctx ) != 0 ) return ( MBEDTLS_ERR_SSL_WANT_READ );
return( MBEDTLS_ERR_SSL_WANT_READ ); }
error = mbedtls_net_errno(fd); error = mbedtls_net_errno(fd);
#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \ #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
!defined(EFI32) !defined(EFI32)
if( WSAGetLastError() == WSAECONNRESET ) if ( WSAGetLastError() == WSAECONNRESET ) {
return( MBEDTLS_ERR_NET_CONN_RESET ); return ( MBEDTLS_ERR_NET_CONN_RESET );
}
#else #else
if( error == EPIPE || error == ECONNRESET ) if ( error == EPIPE || error == ECONNRESET ) {
return( MBEDTLS_ERR_NET_CONN_RESET ); return ( MBEDTLS_ERR_NET_CONN_RESET );
}
if( error == EINTR ) if ( error == EINTR ) {
return( MBEDTLS_ERR_SSL_WANT_READ ); return ( MBEDTLS_ERR_SSL_WANT_READ );
}
#endif #endif
return( MBEDTLS_ERR_NET_RECV_FAILED ); return ( MBEDTLS_ERR_NET_RECV_FAILED );
} }
return( ret ); return ( ret );
} }
/* /*
* Read at most 'len' characters, blocking for at most 'timeout' ms * Read at most 'len' characters, blocking for at most 'timeout' ms
*/ */
int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf, size_t len, int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf, size_t len,
uint32_t timeout ) uint32_t timeout )
{ {
int ret; int ret;
struct timeval tv; struct timeval tv;
fd_set read_fds; fd_set read_fds;
int fd = ((mbedtls_net_context *) ctx)->fd; int fd = ((mbedtls_net_context *) ctx)->fd;
if( fd < 0 ) if ( fd < 0 ) {
return( MBEDTLS_ERR_NET_INVALID_CONTEXT ); return ( MBEDTLS_ERR_NET_INVALID_CONTEXT );
}
FD_ZERO( &read_fds ); FD_ZERO( &read_fds );
FD_SET( fd, &read_fds ); FD_SET( fd, &read_fds );
@ -454,25 +449,27 @@ int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf, size_t len,
ret = select( fd + 1, &read_fds, NULL, NULL, timeout == 0 ? NULL : &tv ); ret = select( fd + 1, &read_fds, NULL, NULL, timeout == 0 ? NULL : &tv );
/* Zero fds ready means we timed out */ /* Zero fds ready means we timed out */
if( ret == 0 ) if ( ret == 0 ) {
return( MBEDTLS_ERR_SSL_TIMEOUT ); return ( MBEDTLS_ERR_SSL_TIMEOUT );
}
if( ret < 0 ) if ( ret < 0 ) {
{
#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \ #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
!defined(EFI32) !defined(EFI32)
if( WSAGetLastError() == WSAEINTR ) if ( WSAGetLastError() == WSAEINTR ) {
return( MBEDTLS_ERR_SSL_WANT_READ ); return ( MBEDTLS_ERR_SSL_WANT_READ );
}
#else #else
if( errno == EINTR ) if ( errno == EINTR ) {
return( MBEDTLS_ERR_SSL_WANT_READ ); return ( MBEDTLS_ERR_SSL_WANT_READ );
}
#endif #endif
return( MBEDTLS_ERR_NET_RECV_FAILED ); return ( MBEDTLS_ERR_NET_RECV_FAILED );
} }
/* This call will not block */ /* This call will not block */
return( mbedtls_net_recv( ctx, buf, len ) ); return ( mbedtls_net_recv( ctx, buf, len ) );
} }
/* /*
@ -483,35 +480,39 @@ int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len )
int ret; int ret;
int fd = ((mbedtls_net_context *) ctx)->fd; int fd = ((mbedtls_net_context *) ctx)->fd;
int error = 0; int error = 0;
if( fd < 0 ) if ( fd < 0 ) {
return( MBEDTLS_ERR_NET_INVALID_CONTEXT ); return ( MBEDTLS_ERR_NET_INVALID_CONTEXT );
}
ret = (int) write( fd, buf, len ); ret = (int) write( fd, buf, len );
if( ret < 0 ) if ( ret < 0 ) {
{ if ( net_would_block( ctx ) != 0 ) {
if( net_would_block( ctx ) != 0 ) return ( MBEDTLS_ERR_SSL_WANT_WRITE );
return( MBEDTLS_ERR_SSL_WANT_WRITE ); }
error = mbedtls_net_errno(fd); error = mbedtls_net_errno(fd);
#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \ #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
!defined(EFI32) !defined(EFI32)
if( WSAGetLastError() == WSAECONNRESET ) if ( WSAGetLastError() == WSAECONNRESET ) {
return( MBEDTLS_ERR_NET_CONN_RESET ); return ( MBEDTLS_ERR_NET_CONN_RESET );
}
#else #else
if( error == EPIPE || error == ECONNRESET ) if ( error == EPIPE || error == ECONNRESET ) {
return( MBEDTLS_ERR_NET_CONN_RESET ); return ( MBEDTLS_ERR_NET_CONN_RESET );
}
if( error == EINTR ) if ( error == EINTR ) {
return( MBEDTLS_ERR_SSL_WANT_WRITE ); return ( MBEDTLS_ERR_SSL_WANT_WRITE );
}
#endif #endif
return( MBEDTLS_ERR_NET_SEND_FAILED ); return ( MBEDTLS_ERR_NET_SEND_FAILED );
} }
return( ret ); return ( ret );
} }
/* /*
@ -519,8 +520,9 @@ int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len )
*/ */
void mbedtls_net_free( mbedtls_net_context *ctx ) void mbedtls_net_free( mbedtls_net_context *ctx )
{ {
if( ctx->fd == -1 ) if ( ctx->fd == -1 ) {
return; return;
}
shutdown( ctx->fd, 2 ); shutdown( ctx->fd, 2 );
close( ctx->fd ); close( ctx->fd );