Skip to content

Commit dee2f27

Browse files
cyliangtwccli8
authored andcommitted
aes_alt support GCC
1 parent 0ab2d44 commit dee2f27

File tree

1 file changed

+63
-62
lines changed
  • hal/targets/hal/TARGET_NUVOTON/TARGET_NUC472/crypto/aes

1 file changed

+63
-62
lines changed

hal/targets/hal/TARGET_NUVOTON/TARGET_NUC472/crypto/aes/aes_alt.c

Lines changed: 63 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ static unsigned char channel_flag[4]={0x00,0x00,0x00,0x00}; // 0: idle, 1: busy
9797
static int channel_alloc()
9898
{
9999
int i;
100-
for(i=0; i< sizeof(channel_flag); i++)
100+
for(i=0; i< (int)sizeof(channel_flag); i++)
101101
{
102102
if( channel_flag[i] == 0x00 )
103103
{
@@ -110,7 +110,7 @@ static int channel_alloc()
110110

111111
static void channel_free(int i)
112112
{
113-
if( i >=0 && i < sizeof(channel_flag) )
113+
if( i >=0 && i < (int)sizeof(channel_flag) )
114114
channel_flag[i] = 0x00;
115115
}
116116

@@ -142,13 +142,13 @@ void mbedtls_aes_init( mbedtls_aes_context *ctx )
142142

143143
NVIC_EnableIRQ(CRPT_IRQn);
144144
AES_ENABLE_INT();
145-
mbedtls_trace("=== %s channel[%d]\r\n", __FUNCTION__, ctx->channel);
145+
mbedtls_trace("=== %s channel[%d]\r\n", __FUNCTION__, (int)ctx->channel);
146146
}
147147

148148
void mbedtls_aes_free( mbedtls_aes_context *ctx )
149149
{
150150

151-
mbedtls_trace("=== %s channel[%d]\r\n", __FUNCTION__,ctx->channel);
151+
mbedtls_trace("=== %s channel[%d]\r\n", __FUNCTION__,(int)ctx->channel);
152152

153153
if( ctx == NULL )
154154
return;
@@ -248,7 +248,7 @@ static void __nvt_aes_crypt( mbedtls_aes_context *ctx,
248248
memcpy(au8InputData, input, dataSize);
249249
pIn = au8InputData;
250250
}else{
251-
pIn = input;
251+
pIn = (unsigned char*)input;
252252
}
253253
if( (((uint32_t)output) & 0x03) || (dataSize%4)) // HW CFB output byte count must be multiple of word
254254
{
@@ -338,7 +338,7 @@ int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
338338
unsigned char temp[16];
339339
int length = len;
340340
int blockChainLen;
341-
mbedtls_trace("=== %s \r\n", __FUNCTION__);
341+
mbedtls_trace("=== %s [0x%x]\r\n", __FUNCTION__,length);
342342
if( length % 16 )
343343
return( MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
344344

@@ -383,6 +383,63 @@ int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
383383
/*
384384
* AES-CFB128 buffer encryption/decryption
385385
*/
386+
/* Support partial block encryption/decryption */
387+
static int __nvt_aes_crypt_partial_block_cfb128( mbedtls_aes_context *ctx,
388+
int mode,
389+
size_t length,
390+
size_t *iv_off,
391+
unsigned char iv[16],
392+
const unsigned char *input,
393+
unsigned char *output )
394+
{
395+
int c;
396+
size_t n = *iv_off;
397+
unsigned char iv_tmp[16];
398+
mbedtls_trace("=== %s \r\n", __FUNCTION__);
399+
if( mode == MBEDTLS_AES_DECRYPT )
400+
{
401+
while( length-- )
402+
{
403+
if( n == 0)
404+
mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv );
405+
else if( ctx->opMode == AES_MODE_CFB) // For previous cryption is CFB mode
406+
{
407+
memcpy(iv_tmp, iv, n);
408+
mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, ctx->prv_iv, iv );
409+
memcpy(iv, iv_tmp, n);
410+
}
411+
412+
c = *input++;
413+
*output++ = (unsigned char)( c ^ iv[n] );
414+
iv[n] = (unsigned char) c;
415+
416+
n = ( n + 1 ) & 0x0F;
417+
}
418+
}
419+
else
420+
{
421+
while( length-- )
422+
{
423+
if( n == 0 )
424+
mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv );
425+
else if( ctx->opMode == AES_MODE_CFB) // For previous cryption is CFB mode
426+
{
427+
memcpy(iv_tmp, iv, n);
428+
mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, ctx->prv_iv, iv );
429+
memcpy(iv, iv_tmp, n);
430+
}
431+
432+
iv[n] = *output++ = (unsigned char)( iv[n] ^ *input++ );
433+
434+
n = ( n + 1 ) & 0x0F;
435+
}
436+
}
437+
438+
*iv_off = n;
439+
440+
return( 0 );
441+
}
442+
386443
int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
387444
int mode,
388445
size_t len,
@@ -456,62 +513,6 @@ int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
456513
return( 0 );
457514
}
458515

459-
/* Support partial block encryption/decryption */
460-
static int __nvt_aes_crypt_partial_block_cfb128( mbedtls_aes_context *ctx,
461-
int mode,
462-
size_t length,
463-
size_t *iv_off,
464-
unsigned char iv[16],
465-
const unsigned char *input,
466-
unsigned char *output )
467-
{
468-
int c;
469-
size_t n = *iv_off;
470-
unsigned char iv_tmp[16];
471-
mbedtls_trace("=== %s \r\n", __FUNCTION__);
472-
if( mode == MBEDTLS_AES_DECRYPT )
473-
{
474-
while( length-- )
475-
{
476-
if( n == 0)
477-
mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv );
478-
else if( ctx->opMode == AES_MODE_CFB) // For previous cryption is CFB mode
479-
{
480-
memcpy(iv_tmp, iv, n);
481-
mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, ctx->prv_iv, iv );
482-
memcpy(iv, iv_tmp, n);
483-
}
484-
485-
c = *input++;
486-
*output++ = (unsigned char)( c ^ iv[n] );
487-
iv[n] = (unsigned char) c;
488-
489-
n = ( n + 1 ) & 0x0F;
490-
}
491-
}
492-
else
493-
{
494-
while( length-- )
495-
{
496-
if( n == 0 )
497-
mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv );
498-
else if( ctx->opMode == AES_MODE_CFB) // For previous cryption is CFB mode
499-
{
500-
memcpy(iv_tmp, iv, n);
501-
mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, ctx->prv_iv, iv );
502-
memcpy(iv, iv_tmp, n);
503-
}
504-
505-
iv[n] = *output++ = (unsigned char)( iv[n] ^ *input++ );
506-
507-
n = ( n + 1 ) & 0x0F;
508-
}
509-
}
510-
511-
*iv_off = n;
512-
513-
return( 0 );
514-
}
515516

516517
/*
517518
* AES-CFB8 buffer encryption/decryption

0 commit comments

Comments
 (0)