Skip to content

Commit fa8f6e4

Browse files
committed
Add AES_CBC mode
1 parent 35bf8e1 commit fa8f6e4

File tree

1 file changed

+13
-40
lines changed

1 file changed

+13
-40
lines changed

features/mbedtls/targets/TARGET_STM/aes_alt.c

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -220,58 +220,31 @@ int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
220220
const unsigned char *input,
221221
unsigned char *output )
222222
{
223-
int i;
224-
unsigned char temp[16];
225-
223+
int status=0;
226224
if( length % 16 )
227225
return( MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
228226

229-
#if defined(MBEDTLS_PADLOCK_C) && defined(MBEDTLS_HAVE_X86)
230-
if( aes_padlock_ace )
227+
switch( ctx->nr )
231228
{
232-
if( mbedtls_padlock_xcryptcbc( ctx, mode, length, iv, input, output ) == 0 )
233-
return( 0 );
234-
235-
// If padlock data misaligned, we just fall back to
236-
// unaccelerated mode
237-
//
229+
case 10: hcryp_aes.Init.KeySize = CRYP_KEYSIZE_128B; break;
230+
case 12: hcryp_aes.Init.KeySize = CRYP_KEYSIZE_192B; break;
231+
case 14: hcryp_aes.Init.KeySize = CRYP_KEYSIZE_256B; break;
232+
default : return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
238233
}
239-
#endif
240-
234+
241235
if( mode == MBEDTLS_AES_DECRYPT )
242236
{
243-
while( length > 0 )
244-
{
245-
memcpy( temp, input, 16 );
246-
mbedtls_aes_crypt_ecb( ctx, mode, input, output );
247-
248-
for( i = 0; i < 16; i++ )
249-
output[i] = (unsigned char)( output[i] ^ iv[i] );
250-
251-
memcpy( iv, temp, 16 );
237+
hcryp_aes.Init.pInitVect = &iv[0]; // used in process, not in the init
252238

253-
input += 16;
254-
output += 16;
255-
length -= 16;
256-
}
239+
status = HAL_CRYP_AESCBC_Decrypt(&hcryp_aes, (uint8_t *)input, length, (uint8_t *)output, 10);
257240
}
258241
else
259242
{
260-
while( length > 0 )
261-
{
262-
for( i = 0; i < 16; i++ )
263-
output[i] = (unsigned char)( input[i] ^ iv[i] );
264-
265-
mbedtls_aes_crypt_ecb( ctx, mode, output, output );
266-
memcpy( iv, output, 16 );
267-
268-
input += 16;
269-
output += 16;
270-
length -= 16;
271-
}
243+
hcryp_aes.Init.pInitVect = &iv[0]; // used in process, not in the init
244+
245+
status = HAL_CRYP_AESCBC_Encrypt(&hcryp_aes, (uint8_t *)input, length, (uint8_t *)output, 10);
272246
}
273-
274-
return( 0 );
247+
return( status );
275248
}
276249
#endif /* MBEDTLS_CIPHER_MODE_CBC */
277250

0 commit comments

Comments
 (0)