Skip to content

Commit d974e28

Browse files
committed
Add hardware accelerator support for internal aes crypto functions
`mbedtls_internal_aes_[de|en]crypt` use plain c only. When `MBEDTLS_AES_USE_HARDWARY_ONLY` enabled, plain C code is not removed from binary. And the functions should use accelerator when they are available. Signed-off-by: Jerry Yu <jerry.h.yu@arm.com>
1 parent 4c91d9b commit d974e28

File tree

1 file changed

+78
-7
lines changed

1 file changed

+78
-7
lines changed

library/aes.c

+78-7
Original file line numberDiff line numberDiff line change
@@ -928,9 +928,10 @@ int mbedtls_aes_xts_setkey_dec(mbedtls_aes_xts_context *ctx,
928928
* AES-ECB block encryption
929929
*/
930930
#if !defined(MBEDTLS_AES_ENCRYPT_ALT)
931-
int mbedtls_internal_aes_encrypt(mbedtls_aes_context *ctx,
932-
const unsigned char input[16],
933-
unsigned char output[16])
931+
#if !defined(MBEDTLS_AES_USE_HARDWARE_ONLY)
932+
static int internal_aes_encrypt(mbedtls_aes_context *ctx,
933+
const unsigned char input[16],
934+
unsigned char output[16])
934935
{
935936
int i;
936937
uint32_t *RK = ctx->buf + ctx->rk_offset;
@@ -984,15 +985,50 @@ int mbedtls_internal_aes_encrypt(mbedtls_aes_context *ctx,
984985

985986
return 0;
986987
}
987-
#endif /* !MBEDTLS_AES_ENCRYPT_ALT */
988+
#endif
988989

990+
int mbedtls_internal_aes_encrypt(mbedtls_aes_context *ctx,
991+
const unsigned char input[16],
992+
unsigned char output[16])
993+
{
994+
int mode = MBEDTLS_AES_ENCRYPT;
995+
996+
(void) mode;
997+
#if defined(MAY_NEED_TO_ALIGN)
998+
aes_maybe_realign(ctx);
999+
#endif
1000+
1001+
#if defined(MBEDTLS_AESNI_HAVE_CODE)
1002+
if (mbedtls_aesni_has_support(MBEDTLS_AESNI_AES)) {
1003+
return mbedtls_aesni_crypt_ecb(ctx, mode, input, output);
1004+
}
1005+
#endif
1006+
1007+
#if defined(MBEDTLS_AESCE_HAVE_CODE)
1008+
if (MBEDTLS_AESCE_HAS_SUPPORT()) {
1009+
return mbedtls_aesce_crypt_ecb(ctx, mode, input, output);
1010+
}
1011+
#endif
1012+
1013+
#if defined(MBEDTLS_VIA_PADLOCK_HAVE_CODE)
1014+
if (aes_padlock_ace > 0) {
1015+
return mbedtls_padlock_xcryptecb(ctx, mode, input, output);
1016+
}
1017+
#endif
1018+
1019+
#if !defined(MBEDTLS_AES_USE_HARDWARE_ONLY)
1020+
return internal_aes_encrypt(ctx, input, output);
1021+
#endif
1022+
}
1023+
#endif /* !MBEDTLS_AES_ENCRYPT_ALT */
9891024
/*
9901025
* AES-ECB block decryption
9911026
*/
9921027
#if !defined(MBEDTLS_AES_DECRYPT_ALT)
993-
int mbedtls_internal_aes_decrypt(mbedtls_aes_context *ctx,
994-
const unsigned char input[16],
995-
unsigned char output[16])
1028+
#if !defined(MBEDTLS_AES_USE_HARDWARE_ONLY)
1029+
static int internal_aes_decrypt(mbedtls_aes_context *ctx,
1030+
const unsigned char input[16],
1031+
unsigned char output[16])
9961032
{
9971033
int i;
9981034
uint32_t *RK = ctx->buf + ctx->rk_offset;
@@ -1046,6 +1082,41 @@ int mbedtls_internal_aes_decrypt(mbedtls_aes_context *ctx,
10461082

10471083
return 0;
10481084
}
1085+
#endif
1086+
1087+
int mbedtls_internal_aes_decrypt(mbedtls_aes_context *ctx,
1088+
const unsigned char input[16],
1089+
unsigned char output[16])
1090+
{
1091+
int mode = MBEDTLS_AES_DECRYPT;
1092+
1093+
(void) mode;
1094+
#if defined(MAY_NEED_TO_ALIGN)
1095+
aes_maybe_realign(ctx);
1096+
#endif
1097+
1098+
#if defined(MBEDTLS_AESNI_HAVE_CODE)
1099+
if (mbedtls_aesni_has_support(MBEDTLS_AESNI_AES)) {
1100+
return mbedtls_aesni_crypt_ecb(ctx, mode, input, output);
1101+
}
1102+
#endif
1103+
1104+
#if defined(MBEDTLS_AESCE_HAVE_CODE)
1105+
if (MBEDTLS_AESCE_HAS_SUPPORT()) {
1106+
return mbedtls_aesce_crypt_ecb(ctx, mode, input, output);
1107+
}
1108+
#endif
1109+
1110+
#if defined(MBEDTLS_VIA_PADLOCK_HAVE_CODE)
1111+
if (aes_padlock_ace > 0) {
1112+
return mbedtls_padlock_xcryptecb(ctx, mode, input, output);
1113+
}
1114+
#endif
1115+
1116+
#if !defined(MBEDTLS_AES_USE_HARDWARE_ONLY)
1117+
return internal_aes_decrypt(ctx, input, output);
1118+
#endif
1119+
}
10491120
#endif /* !MBEDTLS_AES_DECRYPT_ALT */
10501121

10511122
#if defined(MAY_NEED_TO_ALIGN)

0 commit comments

Comments
 (0)