mirror of
https://github.com/espressif/esp-idf.git
synced 2025-10-17 17:25:23 +02:00
@@ -39,100 +39,112 @@ void esp_aes_init( AES_CTX *ctx )
|
||||
|
||||
AES_LOCK();
|
||||
AES_TAKE();
|
||||
ets_aes_enable();
|
||||
AES_UNLOCK();
|
||||
ets_aes_enable();
|
||||
AES_UNLOCK();
|
||||
}
|
||||
|
||||
void esp_aes_free( AES_CTX *ctx )
|
||||
{
|
||||
if( ctx == NULL )
|
||||
if ( ctx == NULL ) {
|
||||
return;
|
||||
}
|
||||
|
||||
bzero( ctx, sizeof( AES_CTX ) );
|
||||
|
||||
AES_LOCK();
|
||||
AES_GIVE();
|
||||
if (false == AES_IS_USED())
|
||||
ets_aes_disable();
|
||||
AES_UNLOCK();
|
||||
|
||||
if (false == AES_IS_USED()) {
|
||||
ets_aes_disable();
|
||||
}
|
||||
|
||||
AES_UNLOCK();
|
||||
}
|
||||
|
||||
/*
|
||||
* AES key schedule (encryption)
|
||||
*/
|
||||
int esp_aes_setkey_enc( AES_CTX *ctx, const unsigned char *key,
|
||||
unsigned int keybits )
|
||||
unsigned int keybits )
|
||||
{
|
||||
enum AES_BITS keybit;
|
||||
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->enc.keyflag == false){
|
||||
ctx->enc.keyflag = true;
|
||||
ctx->enc.keybits = keybits;
|
||||
memset(ctx->enc.key, 0, sizeof(ctx->enc.key));
|
||||
memcpy(ctx->enc.key, key, keybyte);
|
||||
} else {
|
||||
ets_aes_setkey_enc(key, keybit);
|
||||
}
|
||||
return 0;
|
||||
enum AES_BITS keybit;
|
||||
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->enc.keyflag == false) {
|
||||
ctx->enc.keyflag = true;
|
||||
ctx->enc.keybits = keybits;
|
||||
memset(ctx->enc.key, 0, sizeof(ctx->enc.key));
|
||||
memcpy(ctx->enc.key, key, keybyte);
|
||||
} else {
|
||||
ets_aes_setkey_enc(key, keybit);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* AES key schedule (decryption)
|
||||
*/
|
||||
int esp_aes_setkey_dec( AES_CTX *ctx, const unsigned char *key,
|
||||
unsigned int keybits )
|
||||
unsigned int keybits )
|
||||
{
|
||||
enum AES_BITS keybit;
|
||||
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;
|
||||
enum AES_BITS keybit;
|
||||
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;
|
||||
}
|
||||
|
||||
static void esp_aes_process_enable(AES_CTX *ctx, int mode)
|
||||
{
|
||||
if( mode == AES_ENCRYPT ){
|
||||
esp_aes_setkey_enc(ctx, ctx->enc.key, ctx->enc.keybits);
|
||||
}else{
|
||||
esp_aes_setkey_dec(ctx, ctx->dec.key, ctx->dec.keybits);
|
||||
}
|
||||
return;
|
||||
if ( mode == AES_ENCRYPT ) {
|
||||
esp_aes_setkey_enc(ctx, ctx->enc.key, ctx->enc.keybits);
|
||||
} else {
|
||||
esp_aes_setkey_dec(ctx, ctx->dec.key, ctx->dec.keybits);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
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,
|
||||
const unsigned char input[16],
|
||||
unsigned char output[16] )
|
||||
const unsigned char input[16],
|
||||
unsigned char output[16] )
|
||||
{
|
||||
ets_aes_crypt(input, output);
|
||||
return ;
|
||||
ets_aes_crypt(input, output);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
|
||||
@@ -153,11 +166,12 @@ void esp_aes_encrypt( AES_CTX *ctx,
|
||||
*/
|
||||
|
||||
void esp_aes_decrypt( AES_CTX *ctx,
|
||||
const unsigned char input[16],
|
||||
unsigned char output[16] )
|
||||
const unsigned char input[16],
|
||||
unsigned char output[16] )
|
||||
{
|
||||
ets_aes_crypt(input, output);
|
||||
return ;
|
||||
ets_aes_crypt(input, output);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
|
||||
@@ -165,24 +179,25 @@ void esp_aes_decrypt( AES_CTX *ctx,
|
||||
* AES-ECB block encryption/decryption
|
||||
*/
|
||||
int esp_aes_crypt_ecb( AES_CTX *ctx,
|
||||
int mode,
|
||||
const unsigned char input[16],
|
||||
unsigned char output[16] )
|
||||
int mode,
|
||||
const unsigned char input[16],
|
||||
unsigned char output[16] )
|
||||
{
|
||||
AES_LOCK();
|
||||
|
||||
esp_aes_process_enable(ctx, mode);
|
||||
AES_LOCK();
|
||||
|
||||
if( mode == AES_ENCRYPT )
|
||||
esp_aes_process_enable(ctx, mode);
|
||||
|
||||
if ( mode == AES_ENCRYPT ) {
|
||||
esp_aes_encrypt( ctx, input, output );
|
||||
else
|
||||
} else {
|
||||
esp_aes_decrypt( ctx, input, output );
|
||||
}
|
||||
|
||||
esp_aes_process_disable(ctx, mode);
|
||||
|
||||
AES_UNLOCK();
|
||||
|
||||
return 0;
|
||||
esp_aes_process_disable(ctx, mode);
|
||||
|
||||
AES_UNLOCK();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -190,27 +205,27 @@ int esp_aes_crypt_ecb( AES_CTX *ctx,
|
||||
* AES-CBC buffer encryption/decryption
|
||||
*/
|
||||
int esp_aes_crypt_cbc( AES_CTX *ctx,
|
||||
int mode,
|
||||
size_t length,
|
||||
unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output )
|
||||
int mode,
|
||||
size_t length,
|
||||
unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output )
|
||||
{
|
||||
int i;
|
||||
int i;
|
||||
unsigned char temp[16];
|
||||
|
||||
if( length % 16 )
|
||||
return( ERR_AES_INVALID_INPUT_LENGTH );
|
||||
if ( length % 16 ) {
|
||||
return ( ERR_AES_INVALID_INPUT_LENGTH );
|
||||
}
|
||||
|
||||
if( mode == AES_DECRYPT )
|
||||
{
|
||||
while( length > 0 )
|
||||
{
|
||||
if ( mode == AES_DECRYPT ) {
|
||||
while ( length > 0 ) {
|
||||
memcpy( temp, input, 16 );
|
||||
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] );
|
||||
}
|
||||
|
||||
memcpy( iv, temp, 16 );
|
||||
|
||||
@@ -218,13 +233,11 @@ int esp_aes_crypt_cbc( AES_CTX *ctx,
|
||||
output += 16;
|
||||
length -= 16;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while( length > 0 )
|
||||
{
|
||||
for( i = 0; i < 16; i++ )
|
||||
} else {
|
||||
while ( length > 0 ) {
|
||||
for ( i = 0; i < 16; i++ ) {
|
||||
output[i] = (unsigned char)( input[i] ^ iv[i] );
|
||||
}
|
||||
|
||||
esp_aes_crypt_ecb( ctx, mode, output, output );
|
||||
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
|
||||
*/
|
||||
int esp_aes_crypt_cfb128( AES_CTX *ctx,
|
||||
int mode,
|
||||
size_t length,
|
||||
size_t *iv_off,
|
||||
unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output )
|
||||
int mode,
|
||||
size_t length,
|
||||
size_t *iv_off,
|
||||
unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output )
|
||||
{
|
||||
int c;
|
||||
size_t n = *iv_off;
|
||||
int c;
|
||||
size_t n = *iv_off;
|
||||
|
||||
if( mode == AES_DECRYPT )
|
||||
{
|
||||
while( length-- )
|
||||
{
|
||||
if( n == 0 )
|
||||
esp_aes_crypt_ecb( ctx, AES_ENCRYPT, iv, iv );
|
||||
if ( mode == AES_DECRYPT ) {
|
||||
while ( length-- ) {
|
||||
if ( n == 0 ) {
|
||||
esp_aes_crypt_ecb( ctx, AES_ENCRYPT, iv, iv );
|
||||
}
|
||||
|
||||
c = *input++;
|
||||
*output++ = (unsigned char)( c ^ iv[n] );
|
||||
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;
|
||||
c = *input++;
|
||||
*output++ = (unsigned char)( c ^ iv[n] );
|
||||
iv[n] = (unsigned char) c;
|
||||
|
||||
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
|
||||
*/
|
||||
int esp_aes_crypt_cfb8( AES_CTX *ctx,
|
||||
int mode,
|
||||
size_t length,
|
||||
unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output )
|
||||
int mode,
|
||||
size_t length,
|
||||
unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output )
|
||||
{
|
||||
unsigned char c;
|
||||
unsigned char ov[17];
|
||||
unsigned char c;
|
||||
unsigned char ov[17];
|
||||
|
||||
while( length-- )
|
||||
{
|
||||
memcpy( ov, iv, 16 );
|
||||
esp_aes_crypt_ecb( ctx, AES_ENCRYPT, iv, iv );
|
||||
|
||||
if( mode == AES_DECRYPT )
|
||||
ov[16] = *input;
|
||||
|
||||
c = *output++ = (unsigned char)( iv[0] ^ *input++ );
|
||||
while ( length-- ) {
|
||||
memcpy( ov, iv, 16 );
|
||||
esp_aes_crypt_ecb( ctx, AES_ENCRYPT, iv, iv );
|
||||
|
||||
if( mode == AES_ENCRYPT )
|
||||
ov[16] = c;
|
||||
|
||||
memcpy( iv, ov + 1, 16 );
|
||||
}
|
||||
if ( mode == AES_DECRYPT ) {
|
||||
ov[16] = *input;
|
||||
}
|
||||
|
||||
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],
|
||||
const unsigned char *input,
|
||||
unsigned char *output )
|
||||
{
|
||||
int c, i;
|
||||
{
|
||||
int c, i;
|
||||
size_t n = *nc_off;
|
||||
|
||||
while( length-- )
|
||||
{
|
||||
if( n == 0 ) {
|
||||
while ( length-- ) {
|
||||
if ( n == 0 ) {
|
||||
esp_aes_crypt_ecb( ctx, AES_ENCRYPT, nonce_counter, stream_block );
|
||||
|
||||
for( i = 16; i > 0; i-- )
|
||||
if( ++nonce_counter[i - 1] != 0 )
|
||||
for ( i = 16; i > 0; i-- )
|
||||
if ( ++nonce_counter[i - 1] != 0 ) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
c = *input++;
|
||||
*output++ = (unsigned char)( c ^ stream_block[n] );
|
||||
@@ -347,6 +358,6 @@ int esp_aes_crypt_ctr( AES_CTX *ctx,
|
||||
|
||||
*nc_off = n;
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@@ -6,59 +6,61 @@ static SemaphoreHandle_t esp_crypto_mutex[MUTEX_MAX_NUM];
|
||||
static int esp_crypto_sig[MUTEX_MAX_NUM];
|
||||
|
||||
#if 0
|
||||
#define ESP_DEBUG ets_printf
|
||||
#define ESP_DEBUG ets_printf
|
||||
#else
|
||||
#define ESP_DEBUG(...)
|
||||
#define ESP_DEBUG(...)
|
||||
#endif
|
||||
|
||||
int esp_crypto_init(void)
|
||||
{
|
||||
int i;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < MUTEX_MAX_NUM; i++) {
|
||||
esp_crypto_mutex[i] = xSemaphoreCreateMutex();
|
||||
ESP_DEBUG("init num %d mutex %p\n", i, esp_crypto_mutex[i]);
|
||||
if (!esp_crypto_mutex[i]) {
|
||||
goto failed1;
|
||||
}
|
||||
esp_crypto_sig[i] = 0;
|
||||
}
|
||||
for (i = 0; i < MUTEX_MAX_NUM; i++) {
|
||||
esp_crypto_mutex[i] = xSemaphoreCreateMutex();
|
||||
ESP_DEBUG("init num %d mutex %p\n", i, esp_crypto_mutex[i]);
|
||||
if (!esp_crypto_mutex[i]) {
|
||||
goto failed1;
|
||||
}
|
||||
esp_crypto_sig[i] = 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
|
||||
failed1:
|
||||
ESP_DEBUG("esp_crypto_init failed\n");
|
||||
for (i--; i >= 0; i--)
|
||||
vQueueDelete(esp_crypto_mutex[i]);
|
||||
ESP_DEBUG("esp_crypto_init failed\n");
|
||||
for (i--; i >= 0; i--) {
|
||||
vQueueDelete(esp_crypto_mutex[i]);
|
||||
}
|
||||
|
||||
return -1;
|
||||
return -1;
|
||||
}
|
||||
|
||||
void esp_crypto_lock(unsigned int num)
|
||||
{
|
||||
ESP_DEBUG("1num %d, mutex %p\n", num, esp_crypto_mutex[num]);
|
||||
xSemaphoreTake(esp_crypto_mutex[num], portMAX_DELAY);
|
||||
ESP_DEBUG("1num %d, mutex %p\n", num, esp_crypto_mutex[num]);
|
||||
xSemaphoreTake(esp_crypto_mutex[num], portMAX_DELAY);
|
||||
}
|
||||
|
||||
void esp_crypto_unlock(unsigned int num)
|
||||
{
|
||||
ESP_DEBUG("2num %d, mutex %p\n", num, esp_crypto_mutex[num]);
|
||||
xSemaphoreGive(esp_crypto_mutex[num]);
|
||||
ESP_DEBUG("2num %d, mutex %p\n", num, esp_crypto_mutex[num]);
|
||||
xSemaphoreGive(esp_crypto_mutex[num]);
|
||||
}
|
||||
|
||||
void esp_crypto_take(unsigned int num)
|
||||
{
|
||||
esp_crypto_sig[num]++;
|
||||
esp_crypto_sig[num]++;
|
||||
}
|
||||
|
||||
void esp_crypto_give(unsigned int num)
|
||||
{
|
||||
if (esp_crypto_sig[num])
|
||||
esp_crypto_sig[num]--;
|
||||
if (esp_crypto_sig[num]) {
|
||||
esp_crypto_sig[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;
|
||||
}
|
||||
|
||||
|
@@ -18,9 +18,9 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef 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_INPUT_LENGTH -0x0022 /**< Invalid data input length. */
|
||||
|
||||
typedef struct{
|
||||
bool keyflag;
|
||||
uint16_t keybits;
|
||||
typedef struct {
|
||||
bool keyflag;
|
||||
uint16_t keybits;
|
||||
uint8_t key[32];
|
||||
}key_context, KEY_CTX;
|
||||
} key_context, KEY_CTX;
|
||||
|
||||
/**
|
||||
* \brief AES context structure
|
||||
@@ -53,13 +53,12 @@ typedef struct{
|
||||
* - to simplify key expansion in the 256-bit case by
|
||||
* generating an extra round key
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
typedef struct {
|
||||
int nr; /*!< number of rounds */
|
||||
uint32_t *rk; /*!< AES round keys */
|
||||
KEY_CTX enc;
|
||||
KEY_CTX dec;
|
||||
}aes_context, AES_CTX;
|
||||
KEY_CTX enc;
|
||||
KEY_CTX dec;
|
||||
} aes_context, AES_CTX;
|
||||
|
||||
/**
|
||||
* \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
|
||||
*/
|
||||
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)
|
||||
@@ -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
|
||||
*/
|
||||
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
|
||||
@@ -107,7 +106,7 @@ int esp_aes_setkey_dec( AES_CTX *ctx, const unsigned char *key,unsigned int keyb
|
||||
*
|
||||
* \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
|
||||
@@ -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
|
||||
*/
|
||||
int esp_aes_crypt_cbc( AES_CTX *ctx,
|
||||
int mode,
|
||||
size_t length,
|
||||
unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output );
|
||||
int mode,
|
||||
size_t length,
|
||||
unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output );
|
||||
|
||||
|
||||
/**
|
||||
@@ -165,12 +164,12 @@ int esp_aes_crypt_cbc( AES_CTX *ctx,
|
||||
* \return 0 if successful
|
||||
*/
|
||||
int esp_aes_crypt_cfb128( AES_CTX *ctx,
|
||||
int mode,
|
||||
size_t length,
|
||||
size_t *iv_off,
|
||||
unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output );
|
||||
int mode,
|
||||
size_t length,
|
||||
size_t *iv_off,
|
||||
unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output );
|
||||
|
||||
/**
|
||||
* \brief AES-CFB8 buffer encryption/decryption.
|
||||
@@ -197,11 +196,11 @@ int esp_aes_crypt_cfb128( AES_CTX *ctx,
|
||||
* \return 0 if successful
|
||||
*/
|
||||
int esp_aes_crypt_cfb8( AES_CTX *ctx,
|
||||
int mode,
|
||||
size_t length,
|
||||
unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output );
|
||||
int mode,
|
||||
size_t length,
|
||||
unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output );
|
||||
|
||||
/**
|
||||
* \brief AES-CTR buffer encryption/decryption
|
||||
@@ -243,7 +242,7 @@ int esp_aes_crypt_ctr( AES_CTX *ctx,
|
||||
* \param input Plaintext 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
|
||||
|
@@ -9,11 +9,11 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
enum {
|
||||
AES_MUTEX = 0,
|
||||
BIGNUM_MUTEX,
|
||||
SHA_MUTEX,
|
||||
AES_MUTEX = 0,
|
||||
BIGNUM_MUTEX,
|
||||
SHA_MUTEX,
|
||||
|
||||
MUTEX_MAX_NUM,
|
||||
MUTEX_MAX_NUM,
|
||||
};
|
||||
|
||||
int esp_crypto_init(void);
|
||||
@@ -25,29 +25,29 @@ void esp_crypto_take(unsigned int num);
|
||||
void esp_crypto_give(unsigned int num);
|
||||
bool esp_crypto_is_used(unsigned int num);
|
||||
|
||||
#define MUTEX_LOCK(num) esp_crypto_lock(num)
|
||||
#define MUTEX_UNLOCK(num) esp_crypto_unlock(num)
|
||||
#define MUTEX_LOCK(num) esp_crypto_lock(num)
|
||||
#define MUTEX_UNLOCK(num) esp_crypto_unlock(num)
|
||||
|
||||
#define SIG_TAKE(num) esp_crypto_take(num)
|
||||
#define SIG_GIVE(num) esp_crypto_give(num)
|
||||
#define SIG_IS_USED(num) esp_crypto_is_used(num)
|
||||
#define SIG_TAKE(num) esp_crypto_take(num)
|
||||
#define SIG_GIVE(num) esp_crypto_give(num)
|
||||
#define SIG_IS_USED(num) esp_crypto_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_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)
|
||||
#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
|
||||
}
|
||||
|
@@ -26,9 +26,9 @@ extern "C" {
|
||||
/**
|
||||
* \brief SHA-1 context structure
|
||||
*/
|
||||
typedef struct{
|
||||
SHA_CTX context;
|
||||
int context_type;
|
||||
typedef struct {
|
||||
SHA_CTX context;
|
||||
int context_type;
|
||||
} sha_context;
|
||||
|
||||
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] );
|
||||
|
||||
///
|
||||
#define SHA256 SHA2_256
|
||||
#define SHA224 4
|
||||
#define SHA256 SHA2_256
|
||||
#define SHA224 4
|
||||
|
||||
/**
|
||||
* \brief SHA-256 context structure
|
||||
|
@@ -38,22 +38,26 @@ void esp_sha1_init( SHA1_CTX *ctx )
|
||||
|
||||
SHA_LOCK();
|
||||
SHA_TAKE();
|
||||
ets_sha_enable();
|
||||
SHA_UNLOCK();
|
||||
ets_sha_enable();
|
||||
SHA_UNLOCK();
|
||||
}
|
||||
|
||||
void esp_sha1_free( SHA1_CTX *ctx )
|
||||
{
|
||||
if( ctx == NULL )
|
||||
if ( ctx == NULL ) {
|
||||
return;
|
||||
}
|
||||
|
||||
bzero( ctx, sizeof( SHA1_CTX ) );
|
||||
|
||||
SHA_LOCK();
|
||||
SHA_GIVE();
|
||||
if (false == SHA_IS_USED())
|
||||
ets_sha_disable();
|
||||
SHA_UNLOCK();
|
||||
|
||||
if (false == SHA_IS_USED()) {
|
||||
ets_sha_disable();
|
||||
}
|
||||
|
||||
SHA_UNLOCK();
|
||||
}
|
||||
|
||||
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 )
|
||||
{
|
||||
SHA_LOCK();
|
||||
ets_sha_init(&ctx->context);
|
||||
SHA_LOCK();
|
||||
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 )
|
||||
{
|
||||
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] )
|
||||
{
|
||||
ets_sha_finish(&ctx->context, ctx->context_type, output);
|
||||
SHA_UNLOCK();
|
||||
ets_sha_finish(&ctx->context, ctx->context_type, output);
|
||||
SHA_UNLOCK();
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -116,8 +120,8 @@ void esp_sha256_init( SHA256_CTX *ctx )
|
||||
|
||||
SHA_LOCK();
|
||||
SHA_TAKE();
|
||||
ets_sha_enable();
|
||||
SHA_UNLOCK();
|
||||
ets_sha_enable();
|
||||
SHA_UNLOCK();
|
||||
}
|
||||
|
||||
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 )
|
||||
{
|
||||
if( ctx == NULL )
|
||||
if ( ctx == NULL ) {
|
||||
return;
|
||||
}
|
||||
|
||||
bzero( ctx, sizeof( SHA256_CTX ) );
|
||||
|
||||
SHA_LOCK();
|
||||
SHA_GIVE();
|
||||
if (false == SHA_IS_USED())
|
||||
ets_sha_disable();
|
||||
SHA_UNLOCK();
|
||||
|
||||
if (false == SHA_IS_USED()) {
|
||||
ets_sha_disable();
|
||||
}
|
||||
|
||||
SHA_UNLOCK();
|
||||
}
|
||||
|
||||
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 )
|
||||
{
|
||||
SHA_LOCK();
|
||||
SHA_LOCK();
|
||||
ets_sha_init(&ctx->context);
|
||||
|
||||
if( is224 == 0 )
|
||||
{
|
||||
if ( is224 == 0 ) {
|
||||
/* SHA-256 */
|
||||
ctx->context_type = SHA256;
|
||||
}else{
|
||||
/* SHA-224 */
|
||||
ctx->context_type = SHA224;
|
||||
}
|
||||
ctx->context_type = SHA256;
|
||||
} else {
|
||||
/* SHA-224 */
|
||||
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 )
|
||||
{
|
||||
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] )
|
||||
{
|
||||
ets_sha_finish(&ctx->context, ctx->context_type, output);
|
||||
SHA_UNLOCK();
|
||||
ets_sha_finish(&ctx->context, ctx->context_type, output);
|
||||
SHA_UNLOCK();
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -201,8 +208,8 @@ void esp_sha512_init( SHA512_CTX *ctx )
|
||||
|
||||
SHA_LOCK();
|
||||
SHA_TAKE();
|
||||
ets_sha_enable();
|
||||
SHA_UNLOCK();
|
||||
ets_sha_enable();
|
||||
SHA_UNLOCK();
|
||||
}
|
||||
|
||||
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 )
|
||||
{
|
||||
if( ctx == NULL )
|
||||
if ( ctx == NULL ) {
|
||||
return;
|
||||
}
|
||||
|
||||
bzero( ctx, sizeof( SHA512_CTX ) );
|
||||
|
||||
SHA_LOCK();
|
||||
SHA_GIVE();
|
||||
if (false == SHA_IS_USED())
|
||||
ets_sha_disable();
|
||||
SHA_UNLOCK();
|
||||
|
||||
if (false == SHA_IS_USED()) {
|
||||
ets_sha_disable();
|
||||
}
|
||||
|
||||
SHA_UNLOCK();
|
||||
}
|
||||
|
||||
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 )
|
||||
{
|
||||
SHA_LOCK();
|
||||
ets_sha_init(&ctx->context);
|
||||
SHA_LOCK();
|
||||
ets_sha_init(&ctx->context);
|
||||
|
||||
if( is384 == 0 )
|
||||
{
|
||||
if ( is384 == 0 ) {
|
||||
/* SHA-512 */
|
||||
ctx->context_type = SHA2_512;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
/* SHA-384 */
|
||||
ctx->context_type = SHA2_384;
|
||||
}
|
||||
@@ -252,7 +260,7 @@ void esp_sha512_start( SHA512_CTX *ctx, int is384 )
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
@@ -269,7 +277,7 @@ void esp_sha512_finish( SHA512_CTX *ctx, unsigned char output[64] )
|
||||
/*
|
||||
* 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;
|
||||
|
||||
|
Reference in New Issue
Block a user