Skip to content

Commit

Permalink
aesce: re-write runtime detection base on new structure
Browse files Browse the repository at this point in the history
Signed-off-by: Jerry Yu <[email protected]>
  • Loading branch information
yuhaoth committed Apr 4, 2023
1 parent 89c1e02 commit 9042866
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 40 deletions.
8 changes: 4 additions & 4 deletions library/aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx, const unsigned char *key,
}
#endif

#if defined(MBEDTLS_AESCE_C) && defined(MBEDTLS_HAVE_ARM64)
#if MBEDTLS_AES_HAVE_AESCE
if (mbedtls_aesce_has_support()) {
return mbedtls_aesce_setkey_enc((unsigned char *) RK, key, keybits);
}
Expand Down Expand Up @@ -689,7 +689,7 @@ int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx, const unsigned char *key,
}
#endif

#if defined(MBEDTLS_AESCE_C) && defined(MBEDTLS_HAVE_ARM64)
#if MBEDTLS_AES_HAVE_AESCE
if (mbedtls_aesce_has_support()) {
mbedtls_aesce_inverse_key(
(unsigned char *) RK,
Expand Down Expand Up @@ -1015,7 +1015,7 @@ int mbedtls_aes_crypt_ecb(mbedtls_aes_context *ctx,
}
#endif

#if defined(MBEDTLS_AESCE_C) && defined(MBEDTLS_HAVE_ARM64)
#if MBEDTLS_AES_HAVE_AESCE
if (mbedtls_aesce_has_support()) {
return mbedtls_aesce_crypt_ecb(ctx, mode, input, output);
}
Expand Down Expand Up @@ -1789,7 +1789,7 @@ int mbedtls_aes_self_test(int verbose)
mbedtls_printf(" AES note: using AESNI.\n");
} else
#endif
#if defined(MBEDTLS_AESCE_C) && defined(MBEDTLS_HAVE_ARM64)
#if MBEDTLS_AES_HAVE_AESCE
if (mbedtls_aesce_has_support()) {
mbedtls_printf(" AES note: using AESCE.\n");
} else
Expand Down
24 changes: 2 additions & 22 deletions library/aesce.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,9 @@
#include <string.h>
#include "common.h"

#if defined(MBEDTLS_AESCE_C)

#include "aesce.h"

#if defined(MBEDTLS_HAVE_ARM64)
#if MBEDTLS_AES_HAVE_AESCE

#if !defined(__ARM_FEATURE_AES) || defined(MBEDTLS_ENABLE_ARM_CRYPTO_EXTENSIONS_COMPILER_FLAG)
# if defined(__clang__)
Expand Down Expand Up @@ -74,23 +72,6 @@
#include <sys/auxv.h>
#endif

#if !defined(MBEDTLS_AES_HAS_NO_BUILTIN)
/*
* AES instruction support detection routine
*/
int mbedtls_aesce_has_support(void)
{
#if defined(__linux__)
unsigned long auxval = getauxval(AT_HWCAP);
return (auxval & (HWCAP_ASIMD | HWCAP_AES)) ==
(HWCAP_ASIMD | HWCAP_AES);
#else
/* Assume AES instructions are supported. */
return 1;
#endif
}
#endif

static uint8x16_t aesce_encrypt_block(uint8x16_t block,
unsigned char *keys,
int rounds)
Expand Down Expand Up @@ -408,6 +389,5 @@ void mbedtls_aesce_gcm_mult(unsigned char c[16],
#undef MBEDTLS_POP_TARGET_PRAGMA
#endif

#endif /* MBEDTLS_HAVE_ARM64 */

#endif /* MBEDTLS_AESCE_C */
#endif /* MBEDTLS_AES_HAVE_AESCE */
20 changes: 9 additions & 11 deletions library/aesce.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,11 @@
#define MBEDTLS_AESCE_H

#include "mbedtls/build_info.h"

#include "mbedtls/aes.h"

#include "runtime_internal.h"

#if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && \
defined(__aarch64__) && !defined(MBEDTLS_HAVE_ARM64)
#define MBEDTLS_HAVE_ARM64
#endif

#if defined(MBEDTLS_HAVE_ARM64)
#if MBEDTLS_AES_HAVE_AESCE

#ifdef __cplusplus
extern "C" {
Expand All @@ -47,10 +42,13 @@ extern "C" {
*
* \return 1 if CPU has support for the feature, 0 otherwise
*/
#if !defined(MBEDTLS_AES_HAS_NO_BUILTIN)
int mbedtls_aesce_has_support(void);
#if MBEDTLS_AES_ACCELERATOR_NUM > 1
static inline int mbedtls_aesce_has_support(void)
{
return mbedtls_cpu_has_features(MBEDTLS_HWCAP_AES | MBEDTLS_HWCAP_PMULL, 0);
}
#else
#define /* no-check-names */ mbedtls_aesce_has_support() 1
#define /* no-check-names */ mbedtls_aesce_has_support() true
#endif


Expand Down Expand Up @@ -116,6 +114,6 @@ int mbedtls_aesce_setkey_enc(unsigned char *rk,
}
#endif

#endif /* MBEDTLS_HAVE_ARM64 */
#endif /* MBEDTLS_AES_HAVE_AESCE */

#endif /* MBEDTLS_AESCE_H */
6 changes: 3 additions & 3 deletions library/gcm.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ static int gcm_gen_table(mbedtls_gcm_context *ctx)
}
#endif

#if defined(MBEDTLS_AESCE_C) && defined(MBEDTLS_HAVE_ARM64)
#if MBEDTLS_AES_HAVE_AESCE
if (mbedtls_aesce_has_support()) {
return 0;
}
Expand Down Expand Up @@ -208,7 +208,7 @@ static void gcm_mult(mbedtls_gcm_context *ctx, const unsigned char x[16],
}
#endif /* MBEDTLS_AESNI_HAVE_CODE */

#if defined(MBEDTLS_AESCE_C) && defined(MBEDTLS_HAVE_ARM64)
#if MBEDTLS_AES_HAVE_AESCE
if (mbedtls_aesce_has_support()) {
unsigned char h[16];

Expand Down Expand Up @@ -881,7 +881,7 @@ int mbedtls_gcm_self_test(int verbose)
} else
#endif

#if defined(MBEDTLS_AESCE_C) && defined(MBEDTLS_HAVE_ARM64)
#if MBEDTLS_AES_HAVE_AESCE
if (mbedtls_aesce_has_support()) {
mbedtls_printf(" GCM note: using AESCE.\n");
} else
Expand Down

0 comments on commit 9042866

Please sign in to comment.