Add AES_CBC mode

pull/3691/head
adustm 2017-02-06 15:33:17 +01:00
parent 35bf8e17e6
commit fa8f6e4a86
1 changed files with 13 additions and 40 deletions

View File

@ -220,58 +220,31 @@ int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
const unsigned char *input, const unsigned char *input,
unsigned char *output ) unsigned char *output )
{ {
int i; int status=0;
unsigned char temp[16];
if( length % 16 ) if( length % 16 )
return( MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH ); return( MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
#if defined(MBEDTLS_PADLOCK_C) && defined(MBEDTLS_HAVE_X86) switch( ctx->nr )
if( aes_padlock_ace )
{ {
if( mbedtls_padlock_xcryptcbc( ctx, mode, length, iv, input, output ) == 0 ) case 10: hcryp_aes.Init.KeySize = CRYP_KEYSIZE_128B; break;
return( 0 ); case 12: hcryp_aes.Init.KeySize = CRYP_KEYSIZE_192B; break;
case 14: hcryp_aes.Init.KeySize = CRYP_KEYSIZE_256B; break;
// If padlock data misaligned, we just fall back to default : return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
// unaccelerated mode
//
} }
#endif
if( mode == MBEDTLS_AES_DECRYPT ) if( mode == MBEDTLS_AES_DECRYPT )
{ {
while( length > 0 ) hcryp_aes.Init.pInitVect = &iv[0]; // used in process, not in the init
{
memcpy( temp, input, 16 );
mbedtls_aes_crypt_ecb( ctx, mode, input, output );
for( i = 0; i < 16; i++ ) status = HAL_CRYP_AESCBC_Decrypt(&hcryp_aes, (uint8_t *)input, length, (uint8_t *)output, 10);
output[i] = (unsigned char)( output[i] ^ iv[i] );
memcpy( iv, temp, 16 );
input += 16;
output += 16;
length -= 16;
}
} }
else else
{ {
while( length > 0 ) hcryp_aes.Init.pInitVect = &iv[0]; // used in process, not in the init
{
for( i = 0; i < 16; i++ )
output[i] = (unsigned char)( input[i] ^ iv[i] );
mbedtls_aes_crypt_ecb( ctx, mode, output, output ); status = HAL_CRYP_AESCBC_Encrypt(&hcryp_aes, (uint8_t *)input, length, (uint8_t *)output, 10);
memcpy( iv, output, 16 );
input += 16;
output += 16;
length -= 16;
} }
} return( status );
return( 0 );
} }
#endif /* MBEDTLS_CIPHER_MODE_CBC */ #endif /* MBEDTLS_CIPHER_MODE_CBC */